]>
Commit | Line | Data |
---|---|---|
8d08fdba | 1 | /* Handle initialization things in C++. |
d6a8bdff | 2 | Copyright (C) 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998, |
521cc508 | 3 | 1999, 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc. |
8d08fdba MS |
4 | Contributed by Michael Tiemann (tiemann@cygnus.com) |
5 | ||
f5adbb8d | 6 | This file is part of GCC. |
8d08fdba | 7 | |
f5adbb8d | 8 | GCC is free software; you can redistribute it and/or modify |
8d08fdba MS |
9 | it under the terms of the GNU General Public License as published by |
10 | the Free Software Foundation; either version 2, or (at your option) | |
11 | any later version. | |
12 | ||
f5adbb8d | 13 | GCC is distributed in the hope that it will be useful, |
8d08fdba MS |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
f5adbb8d | 19 | along with GCC; see the file COPYING. If not, write to |
e9fa0c7c RK |
20 | the Free Software Foundation, 59 Temple Place - Suite 330, |
21 | Boston, MA 02111-1307, USA. */ | |
8d08fdba | 22 | |
e92cc029 | 23 | /* High-level class interface. */ |
8d08fdba MS |
24 | |
25 | #include "config.h" | |
8d052bc7 | 26 | #include "system.h" |
4977bab6 ZW |
27 | #include "coretypes.h" |
28 | #include "tm.h" | |
8d08fdba MS |
29 | #include "tree.h" |
30 | #include "rtl.h" | |
8f17b5c5 | 31 | #include "expr.h" |
8d08fdba MS |
32 | #include "cp-tree.h" |
33 | #include "flags.h" | |
e8abc66f | 34 | #include "output.h" |
eb66be0e | 35 | #include "except.h" |
54f92bfb | 36 | #include "toplev.h" |
46e995e0 | 37 | #include "target.h" |
8d08fdba | 38 | |
2a3398e1 NS |
39 | static bool begin_init_stmts (tree *, tree *); |
40 | static tree finish_init_stmts (bool, tree, tree); | |
2282d28d | 41 | static void construct_virtual_base (tree, tree); |
362efdc1 NN |
42 | static void expand_aggr_init_1 (tree, tree, tree, tree, int); |
43 | static void expand_default_init (tree, tree, tree, tree, int); | |
44 | static tree build_vec_delete_1 (tree, tree, tree, special_function_kind, int); | |
2282d28d | 45 | static void perform_member_init (tree, tree); |
362efdc1 NN |
46 | static tree build_builtin_delete_call (tree); |
47 | static int member_init_ok_or_else (tree, tree, tree); | |
48 | static void expand_virtual_init (tree, tree); | |
2282d28d | 49 | static tree sort_mem_initializers (tree, tree); |
362efdc1 NN |
50 | static tree initializing_context (tree); |
51 | static void expand_cleanup_for_base (tree, tree); | |
52 | static tree get_temp_regvar (tree, tree); | |
53 | static tree dfs_initialize_vtbl_ptrs (tree, void *); | |
54 | static tree build_default_init (tree, tree); | |
55 | static tree build_new_1 (tree); | |
362efdc1 NN |
56 | static tree build_dtor_call (tree, special_function_kind, int); |
57 | static tree build_field_list (tree, tree, int *); | |
58 | static tree build_vtbl_address (tree); | |
8d08fdba | 59 | |
3dbc07b6 MM |
60 | /* We are about to generate some complex initialization code. |
61 | Conceptually, it is all a single expression. However, we may want | |
62 | to include conditionals, loops, and other such statement-level | |
63 | constructs. Therefore, we build the initialization code inside a | |
64 | statement-expression. This function starts such an expression. | |
65 | STMT_EXPR_P and COMPOUND_STMT_P are filled in by this function; | |
66 | pass them back to finish_init_stmts when the expression is | |
67 | complete. */ | |
68 | ||
2a3398e1 | 69 | static bool |
362efdc1 | 70 | begin_init_stmts (tree *stmt_expr_p, tree *compound_stmt_p) |
3dbc07b6 | 71 | { |
2a3398e1 | 72 | bool is_global = !building_stmt_tree (); |
35b1567d | 73 | |
2a3398e1 | 74 | *stmt_expr_p = begin_stmt_expr (); |
325c3691 | 75 | *compound_stmt_p = begin_compound_stmt (BCS_NO_SCOPE); |
2a3398e1 NS |
76 | |
77 | return is_global; | |
3dbc07b6 MM |
78 | } |
79 | ||
80 | /* Finish out the statement-expression begun by the previous call to | |
81 | begin_init_stmts. Returns the statement-expression itself. */ | |
82 | ||
2a3398e1 NS |
83 | static tree |
84 | finish_init_stmts (bool is_global, tree stmt_expr, tree compound_stmt) | |
35b1567d | 85 | { |
7a3397c7 | 86 | finish_compound_stmt (compound_stmt); |
35b1567d | 87 | |
303b7406 | 88 | stmt_expr = finish_stmt_expr (stmt_expr, true); |
3dbc07b6 | 89 | |
50bc768d | 90 | gcc_assert (!building_stmt_tree () == is_global); |
2a3398e1 | 91 | |
3dbc07b6 MM |
92 | return stmt_expr; |
93 | } | |
94 | ||
95 | /* Constructors */ | |
96 | ||
338d90b8 NS |
97 | /* Called from initialize_vtbl_ptrs via dfs_walk. BINFO is the base |
98 | which we want to initialize the vtable pointer for, DATA is | |
99 | TREE_LIST whose TREE_VALUE is the this ptr expression. */ | |
7177d104 | 100 | |
d569399b | 101 | static tree |
362efdc1 | 102 | dfs_initialize_vtbl_ptrs (tree binfo, void *data) |
d569399b | 103 | { |
809e3e7f | 104 | if ((!BINFO_PRIMARY_P (binfo) || BINFO_VIRTUAL_P (binfo)) |
bcb1079e | 105 | && TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo))) |
d569399b MM |
106 | { |
107 | tree base_ptr = TREE_VALUE ((tree) data); | |
7177d104 | 108 | |
338d90b8 | 109 | base_ptr = build_base_path (PLUS_EXPR, base_ptr, binfo, /*nonnull=*/1); |
d569399b MM |
110 | |
111 | expand_virtual_init (binfo, base_ptr); | |
112 | } | |
7177d104 | 113 | |
dbbf88d1 | 114 | BINFO_MARKED (binfo) = 1; |
d569399b MM |
115 | |
116 | return NULL_TREE; | |
117 | } | |
118 | ||
cf2e003b MM |
119 | /* Initialize all the vtable pointers in the object pointed to by |
120 | ADDR. */ | |
e92cc029 | 121 | |
8d08fdba | 122 | void |
362efdc1 | 123 | initialize_vtbl_ptrs (tree addr) |
8d08fdba | 124 | { |
cf2e003b MM |
125 | tree list; |
126 | tree type; | |
127 | ||
128 | type = TREE_TYPE (TREE_TYPE (addr)); | |
129 | list = build_tree_list (type, addr); | |
d569399b | 130 | |
bbd15aac | 131 | /* Walk through the hierarchy, initializing the vptr in each base |
1f5a253a | 132 | class. We do these in pre-order because we can't find the virtual |
3461fba7 NS |
133 | bases for a class until we've initialized the vtbl for that |
134 | class. */ | |
dbbf88d1 NS |
135 | dfs_walk_real (TYPE_BINFO (type), dfs_initialize_vtbl_ptrs, |
136 | NULL, unmarkedp, list); | |
137 | dfs_walk (TYPE_BINFO (type), dfs_unmark, markedp, type); | |
8d08fdba | 138 | } |
d569399b | 139 | |
17bbb839 MM |
140 | /* Return an expression for the zero-initialization of an object with |
141 | type T. This expression will either be a constant (in the case | |
142 | that T is a scalar), or a CONSTRUCTOR (in the case that T is an | |
143 | aggregate). In either case, the value can be used as DECL_INITIAL | |
144 | for a decl of the indicated TYPE; it is a valid static initializer. | |
1cb8292f MM |
145 | If NELTS is non-NULL, and TYPE is an ARRAY_TYPE, NELTS is the |
146 | number of elements in the array. If STATIC_STORAGE_P is TRUE, | |
147 | initializers are only generated for entities for which | |
148 | zero-initialization does not simply mean filling the storage with | |
149 | zero bytes. */ | |
94e6e4c4 AO |
150 | |
151 | tree | |
1cb8292f | 152 | build_zero_init (tree type, tree nelts, bool static_storage_p) |
94e6e4c4 | 153 | { |
17bbb839 MM |
154 | tree init = NULL_TREE; |
155 | ||
156 | /* [dcl.init] | |
157 | ||
158 | To zero-initialization storage for an object of type T means: | |
159 | ||
160 | -- if T is a scalar type, the storage is set to the value of zero | |
161 | converted to T. | |
162 | ||
163 | -- if T is a non-union class type, the storage for each nonstatic | |
164 | data member and each base-class subobject is zero-initialized. | |
165 | ||
166 | -- if T is a union type, the storage for its first data member is | |
167 | zero-initialized. | |
168 | ||
169 | -- if T is an array type, the storage for each element is | |
170 | zero-initialized. | |
171 | ||
172 | -- if T is a reference type, no initialization is performed. */ | |
94e6e4c4 | 173 | |
50bc768d | 174 | gcc_assert (nelts == NULL_TREE || TREE_CODE (nelts) == INTEGER_CST); |
7a1d37e9 | 175 | |
17bbb839 MM |
176 | if (type == error_mark_node) |
177 | ; | |
178 | else if (static_storage_p && zero_init_p (type)) | |
179 | /* In order to save space, we do not explicitly build initializers | |
180 | for items that do not need them. GCC's semantics are that | |
181 | items with static storage duration that are not otherwise | |
182 | initialized are initialized to zero. */ | |
183 | ; | |
184 | else if (SCALAR_TYPE_P (type)) | |
185 | init = convert (type, integer_zero_node); | |
186 | else if (CLASS_TYPE_P (type)) | |
187 | { | |
188 | tree field; | |
189 | tree inits; | |
190 | ||
191 | /* Build a constructor to contain the initializations. */ | |
dcf92453 | 192 | init = build_constructor (type, NULL_TREE); |
17bbb839 MM |
193 | /* Iterate over the fields, building initializations. */ |
194 | inits = NULL_TREE; | |
195 | for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field)) | |
196 | { | |
197 | if (TREE_CODE (field) != FIELD_DECL) | |
198 | continue; | |
199 | ||
200 | /* Note that for class types there will be FIELD_DECLs | |
201 | corresponding to base classes as well. Thus, iterating | |
202 | over TYPE_FIELDs will result in correct initialization of | |
203 | all of the subobjects. */ | |
204 | if (static_storage_p && !zero_init_p (TREE_TYPE (field))) | |
205 | inits = tree_cons (field, | |
206 | build_zero_init (TREE_TYPE (field), | |
1cb8292f | 207 | /*nelts=*/NULL_TREE, |
17bbb839 MM |
208 | static_storage_p), |
209 | inits); | |
210 | ||
211 | /* For unions, only the first field is initialized. */ | |
212 | if (TREE_CODE (type) == UNION_TYPE) | |
213 | break; | |
214 | } | |
215 | CONSTRUCTOR_ELTS (init) = nreverse (inits); | |
216 | } | |
217 | else if (TREE_CODE (type) == ARRAY_TYPE) | |
94e6e4c4 | 218 | { |
17bbb839 MM |
219 | tree index; |
220 | tree max_index; | |
221 | tree inits; | |
222 | ||
223 | /* Build a constructor to contain the initializations. */ | |
dcf92453 | 224 | init = build_constructor (type, NULL_TREE); |
17bbb839 MM |
225 | /* Iterate over the array elements, building initializations. */ |
226 | inits = NULL_TREE; | |
1cb8292f | 227 | max_index = nelts ? nelts : array_type_nelts (type); |
50bc768d | 228 | gcc_assert (TREE_CODE (max_index) == INTEGER_CST); |
7a1d37e9 | 229 | |
a8e6c82a MM |
230 | /* A zero-sized array, which is accepted as an extension, will |
231 | have an upper bound of -1. */ | |
232 | if (!tree_int_cst_equal (max_index, integer_minus_one_node)) | |
233 | for (index = size_zero_node; | |
234 | !tree_int_cst_lt (max_index, index); | |
235 | index = size_binop (PLUS_EXPR, index, size_one_node)) | |
236 | inits = tree_cons (index, | |
237 | build_zero_init (TREE_TYPE (type), | |
238 | /*nelts=*/NULL_TREE, | |
239 | static_storage_p), | |
240 | inits); | |
17bbb839 | 241 | CONSTRUCTOR_ELTS (init) = nreverse (inits); |
94e6e4c4 | 242 | } |
94e6e4c4 | 243 | else |
8dc2b103 | 244 | gcc_assert (TREE_CODE (type) == REFERENCE_TYPE); |
94e6e4c4 | 245 | |
17bbb839 MM |
246 | /* In all cases, the initializer is a constant. */ |
247 | if (init) | |
6de9cd9a DN |
248 | { |
249 | TREE_CONSTANT (init) = 1; | |
250 | TREE_INVARIANT (init) = 1; | |
251 | } | |
94e6e4c4 AO |
252 | |
253 | return init; | |
254 | } | |
255 | ||
1cb8292f MM |
256 | /* Build an expression for the default-initialization of an object of |
257 | the indicated TYPE. If NELTS is non-NULL, and TYPE is an | |
258 | ARRAY_TYPE, NELTS is the number of elements in the array. If | |
259 | initialization of TYPE requires calling constructors, this function | |
260 | returns NULL_TREE; the caller is responsible for arranging for the | |
261 | constructors to be called. */ | |
f30efcb7 | 262 | |
17bbb839 | 263 | static tree |
362efdc1 | 264 | build_default_init (tree type, tree nelts) |
17bbb839 MM |
265 | { |
266 | /* [dcl.init]: | |
f30efcb7 | 267 | |
17bbb839 | 268 | To default-initialize an object of type T means: |
f30efcb7 | 269 | |
17bbb839 MM |
270 | --if T is a non-POD class type (clause _class_), the default construc- |
271 | tor for T is called (and the initialization is ill-formed if T has | |
272 | no accessible default constructor); | |
f30efcb7 | 273 | |
17bbb839 | 274 | --if T is an array type, each element is default-initialized; |
f30efcb7 | 275 | |
17bbb839 | 276 | --otherwise, the storage for the object is zero-initialized. |
f30efcb7 | 277 | |
17bbb839 MM |
278 | A program that calls for default-initialization of an entity of refer- |
279 | ence type is ill-formed. */ | |
280 | ||
281 | /* If TYPE_NEEDS_CONSTRUCTING is true, the caller is responsible for | |
282 | performing the initialization. This is confusing in that some | |
283 | non-PODs do not have TYPE_NEEDS_CONSTRUCTING set. (For example, | |
284 | a class with a pointer-to-data member as a non-static data member | |
285 | does not have TYPE_NEEDS_CONSTRUCTING set.) Therefore, we end up | |
286 | passing non-PODs to build_zero_init below, which is contrary to | |
287 | the semantics quoted above from [dcl.init]. | |
288 | ||
289 | It happens, however, that the behavior of the constructor the | |
290 | standard says we should have generated would be precisely the | |
291 | same as that obtained by calling build_zero_init below, so things | |
292 | work out OK. */ | |
7a1d37e9 MA |
293 | if (TYPE_NEEDS_CONSTRUCTING (type) |
294 | || (nelts && TREE_CODE (nelts) != INTEGER_CST)) | |
f30efcb7 | 295 | return NULL_TREE; |
17bbb839 MM |
296 | |
297 | /* At this point, TYPE is either a POD class type, an array of POD | |
cd0be382 | 298 | classes, or something even more innocuous. */ |
1cb8292f | 299 | return build_zero_init (type, nelts, /*static_storage_p=*/false); |
f30efcb7 JM |
300 | } |
301 | ||
2282d28d MM |
302 | /* Initialize MEMBER, a FIELD_DECL, with INIT, a TREE_LIST of |
303 | arguments. If TREE_LIST is void_type_node, an empty initializer | |
304 | list was given; if NULL_TREE no initializer was given. */ | |
e92cc029 | 305 | |
8d08fdba | 306 | static void |
2282d28d | 307 | perform_member_init (tree member, tree init) |
8d08fdba MS |
308 | { |
309 | tree decl; | |
310 | tree type = TREE_TYPE (member); | |
2282d28d | 311 | bool explicit; |
eb66be0e | 312 | |
2282d28d MM |
313 | explicit = (init != NULL_TREE); |
314 | ||
315 | /* Effective C++ rule 12 requires that all data members be | |
316 | initialized. */ | |
317 | if (warn_ecpp && !explicit && TREE_CODE (type) != ARRAY_TYPE) | |
2cfe82fe ZW |
318 | warning ("%J%qD should be initialized in the member initialization " |
319 | "list", current_function_decl, member); | |
2282d28d MM |
320 | |
321 | if (init == void_type_node) | |
322 | init = NULL_TREE; | |
323 | ||
324 | /* Get an lvalue for the data member. */ | |
50ad9642 MM |
325 | decl = build_class_member_access_expr (current_class_ref, member, |
326 | /*access_path=*/NULL_TREE, | |
327 | /*preserve_reference=*/true); | |
2fbfe9b8 MS |
328 | if (decl == error_mark_node) |
329 | return; | |
330 | ||
6bdb8141 JM |
331 | /* Deal with this here, as we will get confused if we try to call the |
332 | assignment op for an anonymous union. This can happen in a | |
333 | synthesized copy constructor. */ | |
334 | if (ANON_AGGR_TYPE_P (type)) | |
335 | { | |
ff9f1a5d MM |
336 | if (init) |
337 | { | |
f293ce4b | 338 | init = build2 (INIT_EXPR, type, decl, TREE_VALUE (init)); |
ff9f1a5d MM |
339 | finish_expr_stmt (init); |
340 | } | |
6bdb8141 | 341 | } |
92a62aad | 342 | else if (TYPE_NEEDS_CONSTRUCTING (type)) |
8d08fdba | 343 | { |
8d08fdba MS |
344 | if (explicit |
345 | && TREE_CODE (type) == ARRAY_TYPE | |
346 | && init != NULL_TREE | |
347 | && TREE_CHAIN (init) == NULL_TREE | |
348 | && TREE_CODE (TREE_TYPE (TREE_VALUE (init))) == ARRAY_TYPE) | |
349 | { | |
350 | /* Initialization of one array from another. */ | |
a48cccea JM |
351 | finish_expr_stmt (build_vec_init (decl, NULL_TREE, TREE_VALUE (init), |
352 | /* from_array=*/1)); | |
8d08fdba MS |
353 | } |
354 | else | |
f1dedc31 | 355 | finish_expr_stmt (build_aggr_init (decl, init, 0)); |
8d08fdba MS |
356 | } |
357 | else | |
358 | { | |
359 | if (init == NULL_TREE) | |
360 | { | |
361 | if (explicit) | |
362 | { | |
1cb8292f | 363 | init = build_default_init (type, /*nelts=*/NULL_TREE); |
f30efcb7 | 364 | if (TREE_CODE (type) == REFERENCE_TYPE) |
2cfe82fe ZW |
365 | warning ("%Jdefault-initialization of %q#D, " |
366 | "which has reference type", | |
367 | current_function_decl, member); | |
8d08fdba MS |
368 | } |
369 | /* member traversal: note it leaves init NULL */ | |
f30efcb7 | 370 | else if (TREE_CODE (type) == REFERENCE_TYPE) |
2cfe82fe ZW |
371 | pedwarn ("%Juninitialized reference member %qD", |
372 | current_function_decl, member); | |
58ec3cc5 | 373 | else if (CP_TYPE_CONST_P (type)) |
2cfe82fe ZW |
374 | pedwarn ("%Juninitialized member %qD with %<const%> type %qT", |
375 | current_function_decl, member, type); | |
8d08fdba MS |
376 | } |
377 | else if (TREE_CODE (init) == TREE_LIST) | |
c7b62f14 NS |
378 | /* There was an explicit member initialization. Do some work |
379 | in that case. */ | |
380 | init = build_x_compound_expr_from_list (init, "member initializer"); | |
8d08fdba | 381 | |
4f0aa416 | 382 | if (init) |
f1dedc31 | 383 | finish_expr_stmt (build_modify_expr (decl, INIT_EXPR, init)); |
8d08fdba | 384 | } |
eb66be0e | 385 | |
834c6dff | 386 | if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)) |
b7484fbe | 387 | { |
de22184b MS |
388 | tree expr; |
389 | ||
50ad9642 MM |
390 | expr = build_class_member_access_expr (current_class_ref, member, |
391 | /*access_path=*/NULL_TREE, | |
392 | /*preserve_reference=*/false); | |
3ec6bad3 | 393 | expr = build_delete (type, expr, sfk_complete_destructor, |
b7484fbe MS |
394 | LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR, 0); |
395 | ||
396 | if (expr != error_mark_node) | |
659e5a7a | 397 | finish_eh_cleanup (expr); |
b7484fbe | 398 | } |
8d08fdba MS |
399 | } |
400 | ||
ff9f1a5d MM |
401 | /* Returns a TREE_LIST containing (as the TREE_PURPOSE of each node) all |
402 | the FIELD_DECLs on the TYPE_FIELDS list for T, in reverse order. */ | |
403 | ||
404 | static tree | |
362efdc1 | 405 | build_field_list (tree t, tree list, int *uses_unions_p) |
ff9f1a5d MM |
406 | { |
407 | tree fields; | |
408 | ||
01c3fb15 JM |
409 | *uses_unions_p = 0; |
410 | ||
ff9f1a5d MM |
411 | /* Note whether or not T is a union. */ |
412 | if (TREE_CODE (t) == UNION_TYPE) | |
413 | *uses_unions_p = 1; | |
414 | ||
415 | for (fields = TYPE_FIELDS (t); fields; fields = TREE_CHAIN (fields)) | |
416 | { | |
417 | /* Skip CONST_DECLs for enumeration constants and so forth. */ | |
17bbb839 | 418 | if (TREE_CODE (fields) != FIELD_DECL || DECL_ARTIFICIAL (fields)) |
ff9f1a5d MM |
419 | continue; |
420 | ||
421 | /* Keep track of whether or not any fields are unions. */ | |
422 | if (TREE_CODE (TREE_TYPE (fields)) == UNION_TYPE) | |
423 | *uses_unions_p = 1; | |
424 | ||
425 | /* For an anonymous struct or union, we must recursively | |
426 | consider the fields of the anonymous type. They can be | |
427 | directly initialized from the constructor. */ | |
428 | if (ANON_AGGR_TYPE_P (TREE_TYPE (fields))) | |
429 | { | |
430 | /* Add this field itself. Synthesized copy constructors | |
431 | initialize the entire aggregate. */ | |
432 | list = tree_cons (fields, NULL_TREE, list); | |
433 | /* And now add the fields in the anonymous aggregate. */ | |
434 | list = build_field_list (TREE_TYPE (fields), list, | |
435 | uses_unions_p); | |
436 | } | |
437 | /* Add this field. */ | |
438 | else if (DECL_NAME (fields)) | |
439 | list = tree_cons (fields, NULL_TREE, list); | |
440 | } | |
441 | ||
442 | return list; | |
443 | } | |
444 | ||
2282d28d MM |
445 | /* The MEM_INITS are a TREE_LIST. The TREE_PURPOSE of each list gives |
446 | a FIELD_DECL or BINFO in T that needs initialization. The | |
447 | TREE_VALUE gives the initializer, or list of initializer arguments. | |
448 | ||
449 | Return a TREE_LIST containing all of the initializations required | |
450 | for T, in the order in which they should be performed. The output | |
451 | list has the same format as the input. */ | |
e92cc029 | 452 | |
8d08fdba | 453 | static tree |
2282d28d | 454 | sort_mem_initializers (tree t, tree mem_inits) |
8d08fdba | 455 | { |
ff9f1a5d | 456 | tree init; |
fa743e8c | 457 | tree base, binfo, base_binfo; |
2282d28d MM |
458 | tree sorted_inits; |
459 | tree next_subobject; | |
9ba5ff0f | 460 | VEC (tree) *vbases; |
2282d28d | 461 | int i; |
ff9f1a5d MM |
462 | int uses_unions_p; |
463 | ||
2282d28d MM |
464 | /* Build up a list of initializations. The TREE_PURPOSE of entry |
465 | will be the subobject (a FIELD_DECL or BINFO) to initialize. The | |
466 | TREE_VALUE will be the constructor arguments, or NULL if no | |
467 | explicit initialization was provided. */ | |
468 | sorted_inits = NULL_TREE; | |
58c42dc2 | 469 | |
2282d28d | 470 | /* Process the virtual bases. */ |
9ba5ff0f NS |
471 | for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0; |
472 | VEC_iterate (tree, vbases, i, base); i++) | |
58c42dc2 NS |
473 | sorted_inits = tree_cons (base, NULL_TREE, sorted_inits); |
474 | ||
2282d28d | 475 | /* Process the direct bases. */ |
fa743e8c NS |
476 | for (binfo = TYPE_BINFO (t), i = 0; |
477 | BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i) | |
478 | if (!BINFO_VIRTUAL_P (base_binfo)) | |
479 | sorted_inits = tree_cons (base_binfo, NULL_TREE, sorted_inits); | |
480 | ||
2282d28d MM |
481 | /* Process the non-static data members. */ |
482 | sorted_inits = build_field_list (t, sorted_inits, &uses_unions_p); | |
483 | /* Reverse the entire list of initializations, so that they are in | |
484 | the order that they will actually be performed. */ | |
485 | sorted_inits = nreverse (sorted_inits); | |
486 | ||
487 | /* If the user presented the initializers in an order different from | |
488 | that in which they will actually occur, we issue a warning. Keep | |
489 | track of the next subobject which can be explicitly initialized | |
490 | without issuing a warning. */ | |
491 | next_subobject = sorted_inits; | |
492 | ||
493 | /* Go through the explicit initializers, filling in TREE_PURPOSE in | |
494 | the SORTED_INITS. */ | |
495 | for (init = mem_inits; init; init = TREE_CHAIN (init)) | |
496 | { | |
497 | tree subobject; | |
498 | tree subobject_init; | |
499 | ||
500 | subobject = TREE_PURPOSE (init); | |
501 | ||
502 | /* If the explicit initializers are in sorted order, then | |
503 | SUBOBJECT will be NEXT_SUBOBJECT, or something following | |
504 | it. */ | |
505 | for (subobject_init = next_subobject; | |
506 | subobject_init; | |
507 | subobject_init = TREE_CHAIN (subobject_init)) | |
508 | if (TREE_PURPOSE (subobject_init) == subobject) | |
ff9f1a5d MM |
509 | break; |
510 | ||
2282d28d | 511 | /* Issue a warning if the explicit initializer order does not |
2cfe82fe ZW |
512 | match that which will actually occur. |
513 | ??? Are all these on the correct lines? */ | |
2282d28d | 514 | if (warn_reorder && !subobject_init) |
ff9f1a5d | 515 | { |
2282d28d | 516 | if (TREE_CODE (TREE_PURPOSE (next_subobject)) == FIELD_DECL) |
15a7ee29 | 517 | cp_warning_at ("%qD will be initialized after", |
2282d28d MM |
518 | TREE_PURPOSE (next_subobject)); |
519 | else | |
15a7ee29 | 520 | warning ("base %qT will be initialized after", |
2282d28d MM |
521 | TREE_PURPOSE (next_subobject)); |
522 | if (TREE_CODE (subobject) == FIELD_DECL) | |
15a7ee29 | 523 | cp_warning_at (" %q#D", subobject); |
2282d28d | 524 | else |
15a7ee29 | 525 | warning (" base %qT", subobject); |
2cfe82fe | 526 | warning ("%J when initialized here", current_function_decl); |
ff9f1a5d | 527 | } |
b7484fbe | 528 | |
2282d28d MM |
529 | /* Look again, from the beginning of the list. */ |
530 | if (!subobject_init) | |
ff9f1a5d | 531 | { |
2282d28d MM |
532 | subobject_init = sorted_inits; |
533 | while (TREE_PURPOSE (subobject_init) != subobject) | |
534 | subobject_init = TREE_CHAIN (subobject_init); | |
ff9f1a5d | 535 | } |
2282d28d MM |
536 | |
537 | /* It is invalid to initialize the same subobject more than | |
538 | once. */ | |
539 | if (TREE_VALUE (subobject_init)) | |
ff9f1a5d | 540 | { |
2282d28d | 541 | if (TREE_CODE (subobject) == FIELD_DECL) |
2cfe82fe ZW |
542 | error ("%Jmultiple initializations given for %qD", |
543 | current_function_decl, subobject); | |
2282d28d | 544 | else |
2cfe82fe ZW |
545 | error ("%Jmultiple initializations given for base %qT", |
546 | current_function_decl, subobject); | |
ff9f1a5d MM |
547 | } |
548 | ||
2282d28d MM |
549 | /* Record the initialization. */ |
550 | TREE_VALUE (subobject_init) = TREE_VALUE (init); | |
551 | next_subobject = subobject_init; | |
ff9f1a5d MM |
552 | } |
553 | ||
554 | /* [class.base.init] | |
b7484fbe | 555 | |
ff9f1a5d MM |
556 | If a ctor-initializer specifies more than one mem-initializer for |
557 | multiple members of the same union (including members of | |
558 | anonymous unions), the ctor-initializer is ill-formed. */ | |
559 | if (uses_unions_p) | |
560 | { | |
2282d28d MM |
561 | tree last_field = NULL_TREE; |
562 | for (init = sorted_inits; init; init = TREE_CHAIN (init)) | |
8d08fdba | 563 | { |
ff9f1a5d MM |
564 | tree field; |
565 | tree field_type; | |
566 | int done; | |
567 | ||
2282d28d MM |
568 | /* Skip uninitialized members and base classes. */ |
569 | if (!TREE_VALUE (init) | |
570 | || TREE_CODE (TREE_PURPOSE (init)) != FIELD_DECL) | |
ff9f1a5d MM |
571 | continue; |
572 | /* See if this field is a member of a union, or a member of a | |
573 | structure contained in a union, etc. */ | |
574 | field = TREE_PURPOSE (init); | |
575 | for (field_type = DECL_CONTEXT (field); | |
576 | !same_type_p (field_type, t); | |
577 | field_type = TYPE_CONTEXT (field_type)) | |
578 | if (TREE_CODE (field_type) == UNION_TYPE) | |
579 | break; | |
580 | /* If this field is not a member of a union, skip it. */ | |
581 | if (TREE_CODE (field_type) != UNION_TYPE) | |
8d08fdba | 582 | continue; |
8d08fdba | 583 | |
ff9f1a5d MM |
584 | /* It's only an error if we have two initializers for the same |
585 | union type. */ | |
586 | if (!last_field) | |
6bdb8141 | 587 | { |
ff9f1a5d MM |
588 | last_field = field; |
589 | continue; | |
6bdb8141 | 590 | } |
8d08fdba | 591 | |
ff9f1a5d MM |
592 | /* See if LAST_FIELD and the field initialized by INIT are |
593 | members of the same union. If so, there's a problem, | |
594 | unless they're actually members of the same structure | |
595 | which is itself a member of a union. For example, given: | |
8d08fdba | 596 | |
ff9f1a5d MM |
597 | union { struct { int i; int j; }; }; |
598 | ||
599 | initializing both `i' and `j' makes sense. */ | |
600 | field_type = DECL_CONTEXT (field); | |
601 | done = 0; | |
602 | do | |
8d08fdba | 603 | { |
ff9f1a5d MM |
604 | tree last_field_type; |
605 | ||
606 | last_field_type = DECL_CONTEXT (last_field); | |
607 | while (1) | |
00595019 | 608 | { |
ff9f1a5d | 609 | if (same_type_p (last_field_type, field_type)) |
00595019 | 610 | { |
ff9f1a5d | 611 | if (TREE_CODE (field_type) == UNION_TYPE) |
2cfe82fe ZW |
612 | error ("%Jinitializations for multiple members of %qT", |
613 | current_function_decl, last_field_type); | |
ff9f1a5d MM |
614 | done = 1; |
615 | break; | |
00595019 | 616 | } |
8d08fdba | 617 | |
ff9f1a5d MM |
618 | if (same_type_p (last_field_type, t)) |
619 | break; | |
8d08fdba | 620 | |
ff9f1a5d MM |
621 | last_field_type = TYPE_CONTEXT (last_field_type); |
622 | } | |
623 | ||
624 | /* If we've reached the outermost class, then we're | |
625 | done. */ | |
626 | if (same_type_p (field_type, t)) | |
627 | break; | |
8d08fdba | 628 | |
ff9f1a5d | 629 | field_type = TYPE_CONTEXT (field_type); |
8d08fdba | 630 | } |
ff9f1a5d MM |
631 | while (!done); |
632 | ||
633 | last_field = field; | |
b7484fbe MS |
634 | } |
635 | } | |
8d08fdba | 636 | |
2282d28d | 637 | return sorted_inits; |
b7484fbe MS |
638 | } |
639 | ||
2282d28d MM |
640 | /* Initialize all bases and members of CURRENT_CLASS_TYPE. MEM_INITS |
641 | is a TREE_LIST giving the explicit mem-initializer-list for the | |
642 | constructor. The TREE_PURPOSE of each entry is a subobject (a | |
643 | FIELD_DECL or a BINFO) of the CURRENT_CLASS_TYPE. The TREE_VALUE | |
644 | is a TREE_LIST giving the arguments to the constructor or | |
645 | void_type_node for an empty list of arguments. */ | |
a9aedbc2 | 646 | |
3dbc07b6 | 647 | void |
2282d28d | 648 | emit_mem_initializers (tree mem_inits) |
8d08fdba | 649 | { |
2282d28d MM |
650 | /* Sort the mem-initializers into the order in which the |
651 | initializations should be performed. */ | |
652 | mem_inits = sort_mem_initializers (current_class_type, mem_inits); | |
8d08fdba | 653 | |
1f5a253a NS |
654 | in_base_initializer = 1; |
655 | ||
2282d28d MM |
656 | /* Initialize base classes. */ |
657 | while (mem_inits | |
658 | && TREE_CODE (TREE_PURPOSE (mem_inits)) != FIELD_DECL) | |
8d08fdba | 659 | { |
2282d28d MM |
660 | tree subobject = TREE_PURPOSE (mem_inits); |
661 | tree arguments = TREE_VALUE (mem_inits); | |
662 | ||
663 | /* If these initializations are taking place in a copy | |
664 | constructor, the base class should probably be explicitly | |
665 | initialized. */ | |
666 | if (extra_warnings && !arguments | |
667 | && DECL_COPY_CONSTRUCTOR_P (current_function_decl) | |
668 | && TYPE_NEEDS_CONSTRUCTING (BINFO_TYPE (subobject))) | |
15a7ee29 | 669 | warning ("%Jbase class %q#T should be explicitly initialized in the " |
2282d28d | 670 | "copy constructor", |
2cfe82fe | 671 | current_function_decl, BINFO_TYPE (subobject)); |
2282d28d MM |
672 | |
673 | /* If an explicit -- but empty -- initializer list was present, | |
674 | treat it just like default initialization at this point. */ | |
675 | if (arguments == void_type_node) | |
676 | arguments = NULL_TREE; | |
677 | ||
678 | /* Initialize the base. */ | |
809e3e7f | 679 | if (BINFO_VIRTUAL_P (subobject)) |
2282d28d MM |
680 | construct_virtual_base (subobject, arguments); |
681 | else | |
b7484fbe | 682 | { |
2282d28d MM |
683 | tree base_addr; |
684 | ||
685 | base_addr = build_base_path (PLUS_EXPR, current_class_ptr, | |
686 | subobject, 1); | |
687 | expand_aggr_init_1 (subobject, NULL_TREE, | |
688 | build_indirect_ref (base_addr, NULL), | |
689 | arguments, | |
b370501f | 690 | LOOKUP_NORMAL); |
2282d28d | 691 | expand_cleanup_for_base (subobject, NULL_TREE); |
8d08fdba | 692 | } |
8d08fdba | 693 | |
2282d28d | 694 | mem_inits = TREE_CHAIN (mem_inits); |
8d08fdba | 695 | } |
1f5a253a | 696 | in_base_initializer = 0; |
8d08fdba | 697 | |
2282d28d | 698 | /* Initialize the vptrs. */ |
cf2e003b | 699 | initialize_vtbl_ptrs (current_class_ptr); |
1f5a253a | 700 | |
2282d28d MM |
701 | /* Initialize the data members. */ |
702 | while (mem_inits) | |
8d08fdba | 703 | { |
2282d28d MM |
704 | perform_member_init (TREE_PURPOSE (mem_inits), |
705 | TREE_VALUE (mem_inits)); | |
706 | mem_inits = TREE_CHAIN (mem_inits); | |
b7484fbe | 707 | } |
8d08fdba MS |
708 | } |
709 | ||
3ec6bad3 MM |
710 | /* Returns the address of the vtable (i.e., the value that should be |
711 | assigned to the vptr) for BINFO. */ | |
712 | ||
713 | static tree | |
362efdc1 | 714 | build_vtbl_address (tree binfo) |
3ec6bad3 | 715 | { |
9965d119 | 716 | tree binfo_for = binfo; |
3ec6bad3 MM |
717 | tree vtbl; |
718 | ||
fc6633e0 | 719 | if (BINFO_VPTR_INDEX (binfo) && BINFO_VIRTUAL_P (binfo)) |
9965d119 NS |
720 | /* If this is a virtual primary base, then the vtable we want to store |
721 | is that for the base this is being used as the primary base of. We | |
722 | can't simply skip the initialization, because we may be expanding the | |
723 | inits of a subobject constructor where the virtual base layout | |
724 | can be different. */ | |
fc6633e0 NS |
725 | while (BINFO_PRIMARY_P (binfo_for)) |
726 | binfo_for = BINFO_INHERITANCE_CHAIN (binfo_for); | |
9965d119 | 727 | |
3ec6bad3 MM |
728 | /* Figure out what vtable BINFO's vtable is based on, and mark it as |
729 | used. */ | |
9965d119 | 730 | vtbl = get_vtbl_decl_for_binfo (binfo_for); |
3ec6bad3 MM |
731 | assemble_external (vtbl); |
732 | TREE_USED (vtbl) = 1; | |
733 | ||
734 | /* Now compute the address to use when initializing the vptr. */ | |
6de9cd9a | 735 | vtbl = unshare_expr (BINFO_VTABLE (binfo_for)); |
3ec6bad3 | 736 | if (TREE_CODE (vtbl) == VAR_DECL) |
6de9cd9a | 737 | vtbl = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (vtbl)), vtbl); |
3ec6bad3 MM |
738 | |
739 | return vtbl; | |
740 | } | |
741 | ||
8d08fdba MS |
742 | /* This code sets up the virtual function tables appropriate for |
743 | the pointer DECL. It is a one-ply initialization. | |
744 | ||
745 | BINFO is the exact type that DECL is supposed to be. In | |
746 | multiple inheritance, this might mean "C's A" if C : A, B. */ | |
e92cc029 | 747 | |
8926095f | 748 | static void |
362efdc1 | 749 | expand_virtual_init (tree binfo, tree decl) |
8d08fdba | 750 | { |
8d08fdba | 751 | tree vtbl, vtbl_ptr; |
3ec6bad3 | 752 | tree vtt_index; |
8d08fdba | 753 | |
3ec6bad3 MM |
754 | /* Compute the initializer for vptr. */ |
755 | vtbl = build_vtbl_address (binfo); | |
756 | ||
3461fba7 NS |
757 | /* We may get this vptr from a VTT, if this is a subobject |
758 | constructor or subobject destructor. */ | |
3ec6bad3 MM |
759 | vtt_index = BINFO_VPTR_INDEX (binfo); |
760 | if (vtt_index) | |
761 | { | |
762 | tree vtbl2; | |
763 | tree vtt_parm; | |
764 | ||
765 | /* Compute the value to use, when there's a VTT. */ | |
e0fff4b3 | 766 | vtt_parm = current_vtt_parm; |
f293ce4b RS |
767 | vtbl2 = build2 (PLUS_EXPR, |
768 | TREE_TYPE (vtt_parm), | |
769 | vtt_parm, | |
770 | vtt_index); | |
6de9cd9a DN |
771 | vtbl2 = build_indirect_ref (vtbl2, NULL); |
772 | vtbl2 = convert (TREE_TYPE (vtbl), vtbl2); | |
3ec6bad3 MM |
773 | |
774 | /* The actual initializer is the VTT value only in the subobject | |
775 | constructor. In maybe_clone_body we'll substitute NULL for | |
776 | the vtt_parm in the case of the non-subobject constructor. */ | |
f293ce4b RS |
777 | vtbl = build3 (COND_EXPR, |
778 | TREE_TYPE (vtbl), | |
779 | build2 (EQ_EXPR, boolean_type_node, | |
780 | current_in_charge_parm, integer_zero_node), | |
781 | vtbl2, | |
782 | vtbl); | |
3ec6bad3 | 783 | } |
70ae3201 MM |
784 | |
785 | /* Compute the location of the vtpr. */ | |
338d90b8 NS |
786 | vtbl_ptr = build_vfield_ref (build_indirect_ref (decl, NULL), |
787 | TREE_TYPE (binfo)); | |
50bc768d | 788 | gcc_assert (vtbl_ptr != error_mark_node); |
8d08fdba | 789 | |
70ae3201 | 790 | /* Assign the vtable to the vptr. */ |
6060a796 | 791 | vtbl = convert_force (TREE_TYPE (vtbl_ptr), vtbl, 0); |
f1dedc31 | 792 | finish_expr_stmt (build_modify_expr (vtbl_ptr, NOP_EXPR, vtbl)); |
8d08fdba MS |
793 | } |
794 | ||
f33e32a8 MM |
795 | /* If an exception is thrown in a constructor, those base classes already |
796 | constructed must be destroyed. This function creates the cleanup | |
0b8a1e58 | 797 | for BINFO, which has just been constructed. If FLAG is non-NULL, |
838dfd8a | 798 | it is a DECL which is nonzero when this base needs to be |
0b8a1e58 | 799 | destroyed. */ |
f33e32a8 MM |
800 | |
801 | static void | |
362efdc1 | 802 | expand_cleanup_for_base (tree binfo, tree flag) |
f33e32a8 MM |
803 | { |
804 | tree expr; | |
805 | ||
834c6dff | 806 | if (TYPE_HAS_TRIVIAL_DESTRUCTOR (BINFO_TYPE (binfo))) |
f33e32a8 MM |
807 | return; |
808 | ||
0b8a1e58 | 809 | /* Call the destructor. */ |
4ba126e4 MM |
810 | expr = build_special_member_call (current_class_ref, |
811 | base_dtor_identifier, | |
812 | NULL_TREE, | |
813 | binfo, | |
814 | LOOKUP_NORMAL | LOOKUP_NONVIRTUAL); | |
0b8a1e58 | 815 | if (flag) |
f293ce4b RS |
816 | expr = fold (build3 (COND_EXPR, void_type_node, |
817 | c_common_truthvalue_conversion (flag), | |
818 | expr, integer_zero_node)); | |
0b8a1e58 | 819 | |
659e5a7a | 820 | finish_eh_cleanup (expr); |
f33e32a8 MM |
821 | } |
822 | ||
2282d28d MM |
823 | /* Construct the virtual base-class VBASE passing the ARGUMENTS to its |
824 | constructor. */ | |
e92cc029 | 825 | |
8d08fdba | 826 | static void |
2282d28d | 827 | construct_virtual_base (tree vbase, tree arguments) |
8d08fdba | 828 | { |
2282d28d | 829 | tree inner_if_stmt; |
2282d28d MM |
830 | tree exp; |
831 | tree flag; | |
832 | ||
833 | /* If there are virtual base classes with destructors, we need to | |
834 | emit cleanups to destroy them if an exception is thrown during | |
835 | the construction process. These exception regions (i.e., the | |
836 | period during which the cleanups must occur) begin from the time | |
837 | the construction is complete to the end of the function. If we | |
838 | create a conditional block in which to initialize the | |
839 | base-classes, then the cleanup region for the virtual base begins | |
840 | inside a block, and ends outside of that block. This situation | |
841 | confuses the sjlj exception-handling code. Therefore, we do not | |
842 | create a single conditional block, but one for each | |
843 | initialization. (That way the cleanup regions always begin | |
844 | in the outer block.) We trust the back-end to figure out | |
845 | that the FLAG will not change across initializations, and | |
846 | avoid doing multiple tests. */ | |
847 | flag = TREE_CHAIN (DECL_ARGUMENTS (current_function_decl)); | |
848 | inner_if_stmt = begin_if_stmt (); | |
849 | finish_if_stmt_cond (flag, inner_if_stmt); | |
2282d28d MM |
850 | |
851 | /* Compute the location of the virtual base. If we're | |
852 | constructing virtual bases, then we must be the most derived | |
853 | class. Therefore, we don't have to look up the virtual base; | |
854 | we already know where it is. */ | |
22ed7e5f MM |
855 | exp = convert_to_base_statically (current_class_ref, vbase); |
856 | ||
857 | expand_aggr_init_1 (vbase, current_class_ref, exp, arguments, | |
858 | LOOKUP_COMPLAIN); | |
2282d28d | 859 | finish_then_clause (inner_if_stmt); |
325c3691 | 860 | finish_if_stmt (inner_if_stmt); |
2282d28d MM |
861 | |
862 | expand_cleanup_for_base (vbase, flag); | |
8d08fdba MS |
863 | } |
864 | ||
2ee887f2 | 865 | /* Find the context in which this FIELD can be initialized. */ |
e92cc029 | 866 | |
2ee887f2 | 867 | static tree |
362efdc1 | 868 | initializing_context (tree field) |
2ee887f2 MS |
869 | { |
870 | tree t = DECL_CONTEXT (field); | |
871 | ||
872 | /* Anonymous union members can be initialized in the first enclosing | |
873 | non-anonymous union context. */ | |
6bdb8141 | 874 | while (t && ANON_AGGR_TYPE_P (t)) |
2ee887f2 MS |
875 | t = TYPE_CONTEXT (t); |
876 | return t; | |
877 | } | |
878 | ||
8d08fdba MS |
879 | /* Function to give error message if member initialization specification |
880 | is erroneous. FIELD is the member we decided to initialize. | |
881 | TYPE is the type for which the initialization is being performed. | |
72b7eeff | 882 | FIELD must be a member of TYPE. |
8d08fdba MS |
883 | |
884 | MEMBER_NAME is the name of the member. */ | |
885 | ||
886 | static int | |
362efdc1 | 887 | member_init_ok_or_else (tree field, tree type, tree member_name) |
8d08fdba MS |
888 | { |
889 | if (field == error_mark_node) | |
890 | return 0; | |
a723baf1 | 891 | if (!field) |
8d08fdba | 892 | { |
15a7ee29 | 893 | error ("class %qT does not have any field named %qD", type, |
a723baf1 | 894 | member_name); |
8d08fdba MS |
895 | return 0; |
896 | } | |
a723baf1 | 897 | if (TREE_CODE (field) == VAR_DECL) |
b7484fbe | 898 | { |
15a7ee29 | 899 | error ("%q#D is a static data member; it can only be " |
a723baf1 MM |
900 | "initialized at its definition", |
901 | field); | |
902 | return 0; | |
903 | } | |
904 | if (TREE_CODE (field) != FIELD_DECL) | |
905 | { | |
15a7ee29 | 906 | error ("%q#D is not a non-static data member of %qT", |
a723baf1 MM |
907 | field, type); |
908 | return 0; | |
909 | } | |
910 | if (initializing_context (field) != type) | |
911 | { | |
15a7ee29 | 912 | error ("class %qT does not have any field named %qD", type, |
a723baf1 | 913 | member_name); |
b7484fbe MS |
914 | return 0; |
915 | } | |
916 | ||
8d08fdba MS |
917 | return 1; |
918 | } | |
919 | ||
2282d28d MM |
920 | /* NAME is a FIELD_DECL, an IDENTIFIER_NODE which names a field, or it |
921 | is a _TYPE node or TYPE_DECL which names a base for that type. | |
1f5a253a NS |
922 | Check the validity of NAME, and return either the base _TYPE, base |
923 | binfo, or the FIELD_DECL of the member. If NAME is invalid, return | |
2282d28d | 924 | NULL_TREE and issue a diagnostic. |
8d08fdba | 925 | |
36a68fe7 NS |
926 | An old style unnamed direct single base construction is permitted, |
927 | where NAME is NULL. */ | |
8d08fdba | 928 | |
fd74ca0b | 929 | tree |
1f5a253a | 930 | expand_member_init (tree name) |
8d08fdba | 931 | { |
2282d28d MM |
932 | tree basetype; |
933 | tree field; | |
8d08fdba | 934 | |
2282d28d | 935 | if (!current_class_ref) |
fd74ca0b | 936 | return NULL_TREE; |
8d08fdba | 937 | |
36a68fe7 | 938 | if (!name) |
90418208 | 939 | { |
36a68fe7 NS |
940 | /* This is an obsolete unnamed base class initializer. The |
941 | parser will already have warned about its use. */ | |
604a3205 | 942 | switch (BINFO_N_BASE_BINFOS (TYPE_BINFO (current_class_type))) |
36a68fe7 NS |
943 | { |
944 | case 0: | |
15a7ee29 | 945 | error ("unnamed initializer for %qT, which has no base classes", |
2282d28d | 946 | current_class_type); |
36a68fe7 NS |
947 | return NULL_TREE; |
948 | case 1: | |
604a3205 NS |
949 | basetype = BINFO_TYPE |
950 | (BINFO_BASE_BINFO (TYPE_BINFO (current_class_type), 0)); | |
36a68fe7 NS |
951 | break; |
952 | default: | |
15a7ee29 | 953 | error ("unnamed initializer for %qT, which uses multiple inheritance", |
2282d28d | 954 | current_class_type); |
36a68fe7 NS |
955 | return NULL_TREE; |
956 | } | |
90418208 | 957 | } |
36a68fe7 | 958 | else if (TYPE_P (name)) |
be99da77 | 959 | { |
a82d6da5 | 960 | basetype = TYPE_MAIN_VARIANT (name); |
36a68fe7 | 961 | name = TYPE_NAME (name); |
be99da77 | 962 | } |
36a68fe7 NS |
963 | else if (TREE_CODE (name) == TYPE_DECL) |
964 | basetype = TYPE_MAIN_VARIANT (TREE_TYPE (name)); | |
2282d28d MM |
965 | else |
966 | basetype = NULL_TREE; | |
8d08fdba | 967 | |
36a68fe7 | 968 | if (basetype) |
41efda8f | 969 | { |
d9148cf4 MM |
970 | tree class_binfo; |
971 | tree direct_binfo; | |
972 | tree virtual_binfo; | |
973 | int i; | |
2282d28d | 974 | |
36a68fe7 | 975 | if (current_template_parms) |
1f5a253a | 976 | return basetype; |
2282d28d | 977 | |
d9148cf4 MM |
978 | class_binfo = TYPE_BINFO (current_class_type); |
979 | direct_binfo = NULL_TREE; | |
980 | virtual_binfo = NULL_TREE; | |
981 | ||
982 | /* Look for a direct base. */ | |
fa743e8c NS |
983 | for (i = 0; BINFO_BASE_ITERATE (class_binfo, i, direct_binfo); ++i) |
984 | if (same_type_p (basetype, BINFO_TYPE (direct_binfo))) | |
985 | break; | |
986 | ||
d9148cf4 MM |
987 | /* Look for a virtual base -- unless the direct base is itself |
988 | virtual. */ | |
809e3e7f | 989 | if (!direct_binfo || !BINFO_VIRTUAL_P (direct_binfo)) |
58c42dc2 | 990 | virtual_binfo = binfo_for_vbase (basetype, current_class_type); |
d9148cf4 MM |
991 | |
992 | /* [class.base.init] | |
993 | ||
994 | If a mem-initializer-id is ambiguous because it designates | |
995 | both a direct non-virtual base class and an inherited virtual | |
996 | base class, the mem-initializer is ill-formed. */ | |
997 | if (direct_binfo && virtual_binfo) | |
998 | { | |
15a7ee29 | 999 | error ("%qD is both a direct base and an indirect virtual base", |
d9148cf4 MM |
1000 | basetype); |
1001 | return NULL_TREE; | |
1002 | } | |
1003 | ||
1004 | if (!direct_binfo && !virtual_binfo) | |
8d08fdba | 1005 | { |
5775a06a | 1006 | if (CLASSTYPE_VBASECLASSES (current_class_type)) |
15a7ee29 | 1007 | error ("type %qD is not a direct or virtual base of %qT", |
2282d28d | 1008 | name, current_class_type); |
41efda8f | 1009 | else |
15a7ee29 | 1010 | error ("type %qD is not a direct base of %qT", |
2282d28d | 1011 | name, current_class_type); |
fd74ca0b | 1012 | return NULL_TREE; |
41efda8f | 1013 | } |
d9148cf4 MM |
1014 | |
1015 | return direct_binfo ? direct_binfo : virtual_binfo; | |
41efda8f MM |
1016 | } |
1017 | else | |
1018 | { | |
2282d28d | 1019 | if (TREE_CODE (name) == IDENTIFIER_NODE) |
86ac0575 | 1020 | field = lookup_field (current_class_type, name, 1, false); |
2282d28d MM |
1021 | else |
1022 | field = name; | |
8d08fdba | 1023 | |
2282d28d | 1024 | if (member_init_ok_or_else (field, current_class_type, name)) |
1f5a253a | 1025 | return field; |
41efda8f | 1026 | } |
fd74ca0b | 1027 | |
2282d28d | 1028 | return NULL_TREE; |
8d08fdba MS |
1029 | } |
1030 | ||
1031 | /* This is like `expand_member_init', only it stores one aggregate | |
1032 | value into another. | |
1033 | ||
1034 | INIT comes in two flavors: it is either a value which | |
1035 | is to be stored in EXP, or it is a parameter list | |
1036 | to go to a constructor, which will operate on EXP. | |
f30432d7 MS |
1037 | If INIT is not a parameter list for a constructor, then set |
1038 | LOOKUP_ONLYCONVERTING. | |
6060a796 MS |
1039 | If FLAGS is LOOKUP_ONLYCONVERTING then it is the = init form of |
1040 | the initializer, if FLAGS is 0, then it is the (init) form. | |
8d08fdba | 1041 | If `init' is a CONSTRUCTOR, then we emit a warning message, |
59be0cdd | 1042 | explaining that such initializations are invalid. |
8d08fdba | 1043 | |
8d08fdba MS |
1044 | If INIT resolves to a CALL_EXPR which happens to return |
1045 | something of the type we are looking for, then we know | |
1046 | that we can safely use that call to perform the | |
1047 | initialization. | |
1048 | ||
1049 | The virtual function table pointer cannot be set up here, because | |
1050 | we do not really know its type. | |
1051 | ||
8d08fdba MS |
1052 | This never calls operator=(). |
1053 | ||
1054 | When initializing, nothing is CONST. | |
1055 | ||
1056 | A default copy constructor may have to be used to perform the | |
1057 | initialization. | |
1058 | ||
1059 | A constructor or a conversion operator may have to be used to | |
e92cc029 | 1060 | perform the initialization, but not both, as it would be ambiguous. */ |
8d08fdba | 1061 | |
f1dedc31 | 1062 | tree |
362efdc1 | 1063 | build_aggr_init (tree exp, tree init, int flags) |
8d08fdba | 1064 | { |
f1dedc31 MM |
1065 | tree stmt_expr; |
1066 | tree compound_stmt; | |
1067 | int destroy_temps; | |
8d08fdba MS |
1068 | tree type = TREE_TYPE (exp); |
1069 | int was_const = TREE_READONLY (exp); | |
f30432d7 | 1070 | int was_volatile = TREE_THIS_VOLATILE (exp); |
2a3398e1 | 1071 | int is_global; |
8d08fdba MS |
1072 | |
1073 | if (init == error_mark_node) | |
f1dedc31 | 1074 | return error_mark_node; |
8d08fdba MS |
1075 | |
1076 | TREE_READONLY (exp) = 0; | |
f30432d7 MS |
1077 | TREE_THIS_VOLATILE (exp) = 0; |
1078 | ||
1079 | if (init && TREE_CODE (init) != TREE_LIST) | |
1080 | flags |= LOOKUP_ONLYCONVERTING; | |
8d08fdba MS |
1081 | |
1082 | if (TREE_CODE (type) == ARRAY_TYPE) | |
1083 | { | |
671cb993 MM |
1084 | tree itype; |
1085 | ||
92a62aad MM |
1086 | /* An array may not be initialized use the parenthesized |
1087 | initialization form -- unless the initializer is "()". */ | |
1088 | if (init && TREE_CODE (init) == TREE_LIST) | |
8d08fdba | 1089 | { |
33bd39a2 | 1090 | error ("bad array initializer"); |
f1dedc31 | 1091 | return error_mark_node; |
8d08fdba | 1092 | } |
92a62aad MM |
1093 | /* Must arrange to initialize each element of EXP |
1094 | from elements of INIT. */ | |
671cb993 | 1095 | itype = init ? TREE_TYPE (init) : NULL_TREE; |
89d684bb | 1096 | if (cp_type_quals (type) != TYPE_UNQUALIFIED) |
b2153b98 KL |
1097 | TREE_TYPE (exp) = TYPE_MAIN_VARIANT (type); |
1098 | if (itype && cp_type_quals (itype) != TYPE_UNQUALIFIED) | |
92a62aad | 1099 | itype = TREE_TYPE (init) = TYPE_MAIN_VARIANT (itype); |
a48cccea | 1100 | stmt_expr = build_vec_init (exp, NULL_TREE, init, |
92a62aad MM |
1101 | itype && same_type_p (itype, |
1102 | TREE_TYPE (exp))); | |
8d08fdba | 1103 | TREE_READONLY (exp) = was_const; |
f30432d7 | 1104 | TREE_THIS_VOLATILE (exp) = was_volatile; |
8d08fdba | 1105 | TREE_TYPE (exp) = type; |
f376e137 MS |
1106 | if (init) |
1107 | TREE_TYPE (init) = itype; | |
f1dedc31 | 1108 | return stmt_expr; |
8d08fdba MS |
1109 | } |
1110 | ||
1111 | if (TREE_CODE (exp) == VAR_DECL || TREE_CODE (exp) == PARM_DECL) | |
f4f206f4 | 1112 | /* Just know that we've seen something for this node. */ |
8d08fdba MS |
1113 | TREE_USED (exp) = 1; |
1114 | ||
e7843f33 | 1115 | TREE_TYPE (exp) = TYPE_MAIN_VARIANT (type); |
2a3398e1 | 1116 | is_global = begin_init_stmts (&stmt_expr, &compound_stmt); |
f2c5f623 | 1117 | destroy_temps = stmts_are_full_exprs_p (); |
ae499cce | 1118 | current_stmt_tree ()->stmts_are_full_exprs_p = 0; |
8d08fdba | 1119 | expand_aggr_init_1 (TYPE_BINFO (type), exp, exp, |
b370501f | 1120 | init, LOOKUP_NORMAL|flags); |
2a3398e1 | 1121 | stmt_expr = finish_init_stmts (is_global, stmt_expr, compound_stmt); |
ae499cce | 1122 | current_stmt_tree ()->stmts_are_full_exprs_p = destroy_temps; |
e7843f33 | 1123 | TREE_TYPE (exp) = type; |
8d08fdba | 1124 | TREE_READONLY (exp) = was_const; |
f30432d7 | 1125 | TREE_THIS_VOLATILE (exp) = was_volatile; |
f1dedc31 MM |
1126 | |
1127 | return stmt_expr; | |
8d08fdba MS |
1128 | } |
1129 | ||
6f30f1f1 JM |
1130 | /* Like build_aggr_init, but not just for aggregates. */ |
1131 | ||
1132 | tree | |
362efdc1 | 1133 | build_init (tree decl, tree init, int flags) |
6f30f1f1 JM |
1134 | { |
1135 | tree expr; | |
1136 | ||
6de9cd9a | 1137 | if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE) |
6f30f1f1 | 1138 | expr = build_aggr_init (decl, init, flags); |
6de9cd9a DN |
1139 | else if (CLASS_TYPE_P (TREE_TYPE (decl))) |
1140 | expr = build_special_member_call (decl, complete_ctor_identifier, | |
1141 | build_tree_list (NULL_TREE, init), | |
cad7e87b | 1142 | TREE_TYPE (decl), |
6de9cd9a | 1143 | LOOKUP_NORMAL|flags); |
6f30f1f1 | 1144 | else |
f293ce4b | 1145 | expr = build2 (INIT_EXPR, TREE_TYPE (decl), decl, init); |
8e3df2de | 1146 | |
6f30f1f1 JM |
1147 | return expr; |
1148 | } | |
1149 | ||
8d08fdba | 1150 | static void |
362efdc1 | 1151 | expand_default_init (tree binfo, tree true_exp, tree exp, tree init, int flags) |
8d08fdba | 1152 | { |
fc378698 | 1153 | tree type = TREE_TYPE (exp); |
9eb71d8c | 1154 | tree ctor_name; |
fc378698 | 1155 | |
8d08fdba MS |
1156 | /* It fails because there may not be a constructor which takes |
1157 | its own type as the first (or only parameter), but which does | |
1158 | take other types via a conversion. So, if the thing initializing | |
1159 | the expression is a unit element of type X, first try X(X&), | |
1160 | followed by initialization by X. If neither of these work | |
1161 | out, then look hard. */ | |
1162 | tree rval; | |
1163 | tree parms; | |
8d08fdba | 1164 | |
277294d7 | 1165 | if (init && TREE_CODE (init) != TREE_LIST |
faf5394a MS |
1166 | && (flags & LOOKUP_ONLYCONVERTING)) |
1167 | { | |
1168 | /* Base subobjects should only get direct-initialization. */ | |
8dc2b103 | 1169 | gcc_assert (true_exp == exp); |
faf5394a | 1170 | |
c37dc68e JM |
1171 | if (flags & DIRECT_BIND) |
1172 | /* Do nothing. We hit this in two cases: Reference initialization, | |
1173 | where we aren't initializing a real variable, so we don't want | |
1174 | to run a new constructor; and catching an exception, where we | |
1175 | have already built up the constructor call so we could wrap it | |
1176 | in an exception region. */; | |
92a62aad | 1177 | else if (BRACE_ENCLOSED_INITIALIZER_P (init)) |
8e3df2de | 1178 | { |
b216f69b | 1179 | /* A brace-enclosed initializer for an aggregate. */ |
50bc768d | 1180 | gcc_assert (CP_AGGREGATE_TYPE_P (type)); |
b216f69b | 1181 | init = digest_init (type, init, (tree *)NULL); |
8e3df2de | 1182 | } |
c37dc68e | 1183 | else |
37c46b43 | 1184 | init = ocp_convert (type, init, CONV_IMPLICIT|CONV_FORCE_TEMP, flags); |
faf5394a | 1185 | |
4e8dca1c JM |
1186 | if (TREE_CODE (init) == MUST_NOT_THROW_EXPR) |
1187 | /* We need to protect the initialization of a catch parm with a | |
1188 | call to terminate(), which shows up as a MUST_NOT_THROW_EXPR | |
c7ae64f2 | 1189 | around the TARGET_EXPR for the copy constructor. See |
4e8dca1c JM |
1190 | initialize_handler_parm. */ |
1191 | { | |
f293ce4b RS |
1192 | TREE_OPERAND (init, 0) = build2 (INIT_EXPR, TREE_TYPE (exp), exp, |
1193 | TREE_OPERAND (init, 0)); | |
4e8dca1c JM |
1194 | TREE_TYPE (init) = void_type_node; |
1195 | } | |
c7ae64f2 | 1196 | else |
f293ce4b | 1197 | init = build2 (INIT_EXPR, TREE_TYPE (exp), exp, init); |
c7ae64f2 | 1198 | TREE_SIDE_EFFECTS (init) = 1; |
f1dedc31 | 1199 | finish_expr_stmt (init); |
faf5394a MS |
1200 | return; |
1201 | } | |
1202 | ||
b7484fbe MS |
1203 | if (init == NULL_TREE |
1204 | || (TREE_CODE (init) == TREE_LIST && ! TREE_TYPE (init))) | |
8d08fdba MS |
1205 | { |
1206 | parms = init; | |
db5ae43f MS |
1207 | if (parms) |
1208 | init = TREE_VALUE (parms); | |
8d08fdba | 1209 | } |
8d08fdba | 1210 | else |
051e6fd7 | 1211 | parms = build_tree_list (NULL_TREE, init); |
8d08fdba | 1212 | |
9eb71d8c MM |
1213 | if (true_exp == exp) |
1214 | ctor_name = complete_ctor_identifier; | |
1215 | else | |
1216 | ctor_name = base_ctor_identifier; | |
8d08fdba | 1217 | |
4ba126e4 | 1218 | rval = build_special_member_call (exp, ctor_name, parms, binfo, flags); |
25eb19ff | 1219 | if (TREE_SIDE_EFFECTS (rval)) |
e895113a | 1220 | finish_expr_stmt (convert_to_void (rval, NULL)); |
8d08fdba MS |
1221 | } |
1222 | ||
1223 | /* This function is responsible for initializing EXP with INIT | |
1224 | (if any). | |
1225 | ||
1226 | BINFO is the binfo of the type for who we are performing the | |
1227 | initialization. For example, if W is a virtual base class of A and B, | |
1228 | and C : A, B. | |
1229 | If we are initializing B, then W must contain B's W vtable, whereas | |
1230 | were we initializing C, W must contain C's W vtable. | |
1231 | ||
1232 | TRUE_EXP is nonzero if it is the true expression being initialized. | |
1233 | In this case, it may be EXP, or may just contain EXP. The reason we | |
1234 | need this is because if EXP is a base element of TRUE_EXP, we | |
1235 | don't necessarily know by looking at EXP where its virtual | |
1236 | baseclass fields should really be pointing. But we do know | |
1237 | from TRUE_EXP. In constructors, we don't know anything about | |
1238 | the value being initialized. | |
1239 | ||
9f880ef9 MM |
1240 | FLAGS is just passed to `build_new_method_call'. See that function |
1241 | for its description. */ | |
8d08fdba MS |
1242 | |
1243 | static void | |
362efdc1 | 1244 | expand_aggr_init_1 (tree binfo, tree true_exp, tree exp, tree init, int flags) |
8d08fdba MS |
1245 | { |
1246 | tree type = TREE_TYPE (exp); | |
8d08fdba | 1247 | |
50bc768d NS |
1248 | gcc_assert (init != error_mark_node && type != error_mark_node); |
1249 | gcc_assert (building_stmt_tree ()); | |
8d08fdba MS |
1250 | |
1251 | /* Use a function returning the desired type to initialize EXP for us. | |
1252 | If the function is a constructor, and its first argument is | |
1253 | NULL_TREE, know that it was meant for us--just slide exp on | |
1254 | in and expand the constructor. Constructors now come | |
1255 | as TARGET_EXPRs. */ | |
faf5394a MS |
1256 | |
1257 | if (init && TREE_CODE (exp) == VAR_DECL | |
1258 | && TREE_CODE (init) == CONSTRUCTOR | |
1259 | && TREE_HAS_CONSTRUCTOR (init)) | |
1260 | { | |
f1dedc31 MM |
1261 | /* If store_init_value returns NULL_TREE, the INIT has been |
1262 | record in the DECL_INITIAL for EXP. That means there's | |
1263 | nothing more we have to do. */ | |
25ebb82a RH |
1264 | init = store_init_value (exp, init); |
1265 | if (init) | |
1266 | finish_expr_stmt (init); | |
faf5394a MS |
1267 | return; |
1268 | } | |
1269 | ||
9e9ff709 MS |
1270 | /* We know that expand_default_init can handle everything we want |
1271 | at this point. */ | |
b370501f | 1272 | expand_default_init (binfo, true_exp, exp, init, flags); |
8d08fdba MS |
1273 | } |
1274 | ||
be99da77 MS |
1275 | /* Report an error if TYPE is not a user-defined, aggregate type. If |
1276 | OR_ELSE is nonzero, give an error message. */ | |
e92cc029 | 1277 | |
be99da77 | 1278 | int |
362efdc1 | 1279 | is_aggr_type (tree type, int or_else) |
be99da77 MS |
1280 | { |
1281 | if (type == error_mark_node) | |
1282 | return 0; | |
1283 | ||
1284 | if (! IS_AGGR_TYPE (type) | |
73b0fce8 | 1285 | && TREE_CODE (type) != TEMPLATE_TYPE_PARM |
a1281f45 | 1286 | && TREE_CODE (type) != BOUND_TEMPLATE_TEMPLATE_PARM) |
be99da77 MS |
1287 | { |
1288 | if (or_else) | |
15a7ee29 | 1289 | error ("%qT is not an aggregate type", type); |
be99da77 MS |
1290 | return 0; |
1291 | } | |
1292 | return 1; | |
1293 | } | |
1294 | ||
8d08fdba | 1295 | tree |
362efdc1 | 1296 | get_type_value (tree name) |
8d08fdba | 1297 | { |
8d08fdba MS |
1298 | if (name == error_mark_node) |
1299 | return NULL_TREE; | |
1300 | ||
1301 | if (IDENTIFIER_HAS_TYPE_VALUE (name)) | |
1302 | return IDENTIFIER_TYPE_VALUE (name); | |
8d08fdba MS |
1303 | else |
1304 | return NULL_TREE; | |
1305 | } | |
051e6fd7 | 1306 | |
a5ac359a MM |
1307 | /* Build a reference to a member of an aggregate. This is not a C++ |
1308 | `&', but really something which can have its address taken, and | |
1309 | then act as a pointer to member, for example TYPE :: FIELD can have | |
1310 | its address taken by saying & TYPE :: FIELD. ADDRESS_P is true if | |
1311 | this expression is the operand of "&". | |
8d08fdba MS |
1312 | |
1313 | @@ Prints out lousy diagnostics for operator <typename> | |
1314 | @@ fields. | |
1315 | ||
51c184be | 1316 | @@ This function should be rewritten and placed in search.c. */ |
e92cc029 | 1317 | |
8d08fdba | 1318 | tree |
a5ac359a | 1319 | build_offset_ref (tree type, tree name, bool address_p) |
8d08fdba | 1320 | { |
8d245821 | 1321 | tree decl; |
d6479fe7 | 1322 | tree member; |
fc378698 | 1323 | tree basebinfo = NULL_TREE; |
2a238a97 | 1324 | tree orig_name = name; |
8d08fdba | 1325 | |
5f311aec | 1326 | /* class templates can come in as TEMPLATE_DECLs here. */ |
874503bc | 1327 | if (TREE_CODE (name) == TEMPLATE_DECL) |
93cdc044 JM |
1328 | return name; |
1329 | ||
3601f003 | 1330 | if (dependent_type_p (type) || type_dependent_expression_p (name)) |
53b22f3d | 1331 | return build_min_nt (SCOPE_REF, type, name); |
5566b478 | 1332 | |
2a238a97 MM |
1333 | if (TREE_CODE (name) == TEMPLATE_ID_EXPR) |
1334 | { | |
1335 | /* If the NAME is a TEMPLATE_ID_EXPR, we are looking at | |
1336 | something like `a.template f<int>' or the like. For the most | |
1337 | part, we treat this just like a.f. We do remember, however, | |
1338 | the template-id that was used. */ | |
1339 | name = TREE_OPERAND (orig_name, 0); | |
e4a84209 | 1340 | |
c65a922c TP |
1341 | if (DECL_P (name)) |
1342 | name = DECL_NAME (name); | |
1343 | else | |
1344 | { | |
10b1d5e7 MM |
1345 | if (TREE_CODE (name) == COMPONENT_REF) |
1346 | name = TREE_OPERAND (name, 1); | |
1347 | if (TREE_CODE (name) == OVERLOAD) | |
1348 | name = DECL_NAME (OVL_CURRENT (name)); | |
c65a922c | 1349 | } |
e4a84209 | 1350 | |
50bc768d | 1351 | gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE); |
2a238a97 MM |
1352 | } |
1353 | ||
c833d2be NS |
1354 | if (type == NULL_TREE) |
1355 | return error_mark_node; | |
1356 | ||
1357 | /* Handle namespace names fully here. */ | |
1358 | if (TREE_CODE (type) == NAMESPACE_DECL) | |
1359 | { | |
8d245821 | 1360 | tree t = lookup_namespace_name (type, name); |
c833d2be NS |
1361 | if (t == error_mark_node) |
1362 | return t; | |
1363 | if (TREE_CODE (orig_name) == TEMPLATE_ID_EXPR) | |
1364 | /* Reconstruct the TEMPLATE_ID_EXPR. */ | |
f293ce4b RS |
1365 | t = build2 (TEMPLATE_ID_EXPR, TREE_TYPE (t), |
1366 | t, TREE_OPERAND (orig_name, 1)); | |
c833d2be NS |
1367 | if (! type_unknown_p (t)) |
1368 | { | |
1369 | mark_used (t); | |
1370 | t = convert_from_reference (t); | |
1371 | } | |
1372 | return t; | |
1373 | } | |
1374 | ||
1375 | if (! is_aggr_type (type, 1)) | |
1376 | return error_mark_node; | |
1377 | ||
8d08fdba MS |
1378 | if (TREE_CODE (name) == BIT_NOT_EXPR) |
1379 | { | |
1c2c08a5 | 1380 | if (! check_dtor_name (type, name)) |
15a7ee29 | 1381 | error ("qualified type %qT does not match destructor name %<~%T%>", |
1c2c08a5 JM |
1382 | type, TREE_OPERAND (name, 0)); |
1383 | name = dtor_identifier; | |
8d08fdba | 1384 | } |
be99da77 | 1385 | |
d0f062fb | 1386 | if (!COMPLETE_TYPE_P (complete_type (type)) |
61a127b3 | 1387 | && !TYPE_BEING_DEFINED (type)) |
8d08fdba | 1388 | { |
15a7ee29 | 1389 | error ("incomplete type %qT does not have member %qD", type, name); |
8d08fdba MS |
1390 | return error_mark_node; |
1391 | } | |
1392 | ||
3601f003 | 1393 | /* Set up BASEBINFO for member lookup. */ |
51924768 | 1394 | decl = maybe_dummy_object (type, &basebinfo); |
8d08fdba | 1395 | |
a723baf1 | 1396 | if (BASELINK_P (name) || DECL_P (name)) |
50ad9642 MM |
1397 | member = name; |
1398 | else | |
1399 | { | |
1400 | member = lookup_member (basebinfo, name, 1, 0); | |
1401 | ||
1402 | if (member == error_mark_node) | |
1403 | return error_mark_node; | |
1404 | } | |
00595019 | 1405 | |
a5ac359a MM |
1406 | if (!member) |
1407 | { | |
15a7ee29 | 1408 | error ("%qD is not a member of type %qT", name, type); |
a5ac359a MM |
1409 | return error_mark_node; |
1410 | } | |
1411 | ||
3601f003 KL |
1412 | if (processing_template_decl) |
1413 | { | |
1414 | if (TREE_CODE (orig_name) == TEMPLATE_ID_EXPR) | |
1415 | return build_min (SCOPE_REF, TREE_TYPE (member), type, orig_name); | |
1416 | else | |
1417 | return build_min (SCOPE_REF, TREE_TYPE (member), type, name); | |
1418 | } | |
1419 | ||
a5ac359a MM |
1420 | if (TREE_CODE (member) == TYPE_DECL) |
1421 | { | |
1422 | TREE_USED (member) = 1; | |
1423 | return member; | |
1424 | } | |
1425 | /* static class members and class-specific enum | |
1426 | values can be returned without further ado. */ | |
1427 | if (TREE_CODE (member) == VAR_DECL || TREE_CODE (member) == CONST_DECL) | |
1428 | { | |
1429 | mark_used (member); | |
1430 | return convert_from_reference (member); | |
1431 | } | |
1432 | ||
1433 | if (TREE_CODE (member) == FIELD_DECL && DECL_C_BIT_FIELD (member)) | |
1434 | { | |
15a7ee29 | 1435 | error ("invalid pointer to bit-field %qD", member); |
a5ac359a MM |
1436 | return error_mark_node; |
1437 | } | |
1438 | ||
aa52c1ff | 1439 | /* A lot of this logic is now handled in lookup_member. */ |
a5ac359a | 1440 | if (BASELINK_P (member)) |
8d08fdba | 1441 | { |
8d08fdba | 1442 | /* Go from the TREE_BASELINK to the member function info. */ |
d6479fe7 | 1443 | tree fnfields = member; |
8d245821 | 1444 | tree t = BASELINK_FUNCTIONS (fnfields); |
8d08fdba | 1445 | |
2a238a97 MM |
1446 | if (TREE_CODE (orig_name) == TEMPLATE_ID_EXPR) |
1447 | { | |
1448 | /* The FNFIELDS are going to contain functions that aren't | |
1449 | necessarily templates, and templates that don't | |
1450 | necessarily match the explicit template parameters. We | |
1451 | save all the functions, and the explicit parameters, and | |
1452 | then figure out exactly what to instantiate with what | |
1453 | arguments in instantiate_type. */ | |
1454 | ||
1455 | if (TREE_CODE (t) != OVERLOAD) | |
1456 | /* The code in instantiate_type which will process this | |
1457 | expects to encounter OVERLOADs, not raw functions. */ | |
1458 | t = ovl_cons (t, NULL_TREE); | |
051e6fd7 | 1459 | |
f293ce4b RS |
1460 | t = build2 (TEMPLATE_ID_EXPR, TREE_TYPE (t), t, |
1461 | TREE_OPERAND (orig_name, 1)); | |
1462 | t = build2 (OFFSET_REF, unknown_type_node, decl, t); | |
19420d00 NS |
1463 | |
1464 | PTRMEM_OK_P (t) = 1; | |
1465 | ||
1466 | return t; | |
2a238a97 MM |
1467 | } |
1468 | ||
50ad9642 | 1469 | if (TREE_CODE (t) != TEMPLATE_ID_EXPR && !really_overloaded_fn (t)) |
8d08fdba | 1470 | { |
f4f206f4 | 1471 | /* Get rid of a potential OVERLOAD around it. */ |
2c73f9f5 ML |
1472 | t = OVL_CURRENT (t); |
1473 | ||
b54f5338 KL |
1474 | /* Unique functions are handled easily. */ |
1475 | ||
1476 | /* For non-static member of base class, we need a special rule | |
1477 | for access checking [class.protected]: | |
1478 | ||
1479 | If the access is to form a pointer to member, the | |
1480 | nested-name-specifier shall name the derived class | |
1481 | (or any class derived from that class). */ | |
1482 | if (address_p && DECL_P (t) | |
1483 | && DECL_NONSTATIC_MEMBER_P (t)) | |
1484 | perform_or_defer_access_check (TYPE_BINFO (type), t); | |
1485 | else | |
1486 | perform_or_defer_access_check (basebinfo, t); | |
1487 | ||
fc378698 | 1488 | mark_used (t); |
848b92e1 JM |
1489 | if (DECL_STATIC_FUNCTION_P (t)) |
1490 | return t; | |
a5ac359a MM |
1491 | member = t; |
1492 | } | |
1493 | else | |
1494 | { | |
1495 | TREE_TYPE (fnfields) = unknown_type_node; | |
1496 | member = fnfields; | |
8d08fdba | 1497 | } |
8d08fdba | 1498 | } |
b54f5338 KL |
1499 | else if (address_p && TREE_CODE (member) == FIELD_DECL) |
1500 | /* We need additional test besides the one in | |
1501 | check_accessibility_of_qualified_id in case it is | |
1502 | a pointer to non-static member. */ | |
1503 | perform_or_defer_access_check (TYPE_BINFO (type), member); | |
8d08fdba | 1504 | |
a5ac359a | 1505 | if (!address_p) |
8d08fdba | 1506 | { |
a5ac359a MM |
1507 | /* If MEMBER is non-static, then the program has fallen afoul of |
1508 | [expr.prim]: | |
8d08fdba | 1509 | |
a5ac359a MM |
1510 | An id-expression that denotes a nonstatic data member or |
1511 | nonstatic member function of a class can only be used: | |
8d08fdba | 1512 | |
a5ac359a MM |
1513 | -- as part of a class member access (_expr.ref_) in which the |
1514 | object-expression refers to the member's class or a class | |
1515 | derived from that class, or | |
b7484fbe | 1516 | |
a5ac359a MM |
1517 | -- to form a pointer to member (_expr.unary.op_), or |
1518 | ||
1519 | -- in the body of a nonstatic member function of that class or | |
1520 | of a class derived from that class (_class.mfct.nonstatic_), or | |
1521 | ||
1522 | -- in a mem-initializer for a constructor for that class or for | |
1523 | a class derived from that class (_class.base.init_). */ | |
1524 | if (DECL_NONSTATIC_MEMBER_FUNCTION_P (member)) | |
1525 | { | |
e9525111 MM |
1526 | /* Build a representation of a the qualified name suitable |
1527 | for use as the operand to "&" -- even though the "&" is | |
1528 | not actually present. */ | |
f293ce4b | 1529 | member = build2 (OFFSET_REF, TREE_TYPE (member), decl, member); |
a5ac359a MM |
1530 | /* In Microsoft mode, treat a non-static member function as if |
1531 | it were a pointer-to-member. */ | |
1532 | if (flag_ms_extensions) | |
1533 | { | |
a5ac359a MM |
1534 | PTRMEM_OK_P (member) = 1; |
1535 | return build_unary_op (ADDR_EXPR, member, 0); | |
1536 | } | |
15a7ee29 | 1537 | error ("invalid use of non-static member function %qD", |
e9525111 MM |
1538 | TREE_OPERAND (member, 1)); |
1539 | return member; | |
a5ac359a MM |
1540 | } |
1541 | else if (TREE_CODE (member) == FIELD_DECL) | |
1542 | { | |
15a7ee29 | 1543 | error ("invalid use of non-static data member %qD", member); |
a5ac359a MM |
1544 | return error_mark_node; |
1545 | } | |
1546 | return member; | |
1547 | } | |
8d08fdba | 1548 | |
be99da77 MS |
1549 | /* In member functions, the form `type::name' is no longer |
1550 | equivalent to `this->type::name', at least not until | |
1551 | resolve_offset_ref. */ | |
f293ce4b | 1552 | member = build2 (OFFSET_REF, TREE_TYPE (member), decl, member); |
8d245821 MM |
1553 | PTRMEM_OK_P (member) = 1; |
1554 | return member; | |
8d08fdba MS |
1555 | } |
1556 | ||
fc611ce0 MM |
1557 | /* If DECL is a `const' declaration, and its value is a known |
1558 | constant, then return that value. */ | |
8d08fdba MS |
1559 | |
1560 | tree | |
362efdc1 | 1561 | decl_constant_value (tree decl) |
8d08fdba | 1562 | { |
a1652802 MM |
1563 | /* When we build a COND_EXPR, we don't know whether it will be used |
1564 | as an lvalue or as an rvalue. If it is an lvalue, it's not safe | |
1565 | to replace the second and third operands with their | |
1566 | initializers. So, we do that here. */ | |
1567 | if (TREE_CODE (decl) == COND_EXPR) | |
1568 | { | |
1569 | tree d1; | |
1570 | tree d2; | |
1571 | ||
1572 | d1 = decl_constant_value (TREE_OPERAND (decl, 1)); | |
1573 | d2 = decl_constant_value (TREE_OPERAND (decl, 2)); | |
1574 | ||
1575 | if (d1 != TREE_OPERAND (decl, 1) || d2 != TREE_OPERAND (decl, 2)) | |
f293ce4b RS |
1576 | return build3 (COND_EXPR, |
1577 | TREE_TYPE (decl), | |
1578 | TREE_OPERAND (decl, 0), d1, d2); | |
a1652802 MM |
1579 | } |
1580 | ||
c353b8e3 MM |
1581 | if (DECL_P (decl) |
1582 | && (/* Enumeration constants are constant. */ | |
1583 | TREE_CODE (decl) == CONST_DECL | |
1584 | /* And so are variables with a 'const' type -- unless they | |
1585 | are also 'volatile'. */ | |
1586 | || CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (decl))) | |
4f976745 | 1587 | && TREE_CODE (decl) != PARM_DECL |
61a127b3 | 1588 | && DECL_INITIAL (decl) |
bd6dd845 | 1589 | && DECL_INITIAL (decl) != error_mark_node |
8d08fdba MS |
1590 | /* This is invalid if initial value is not constant. |
1591 | If it has either a function call, a memory reference, | |
1592 | or a variable, then re-evaluating it could give different results. */ | |
1593 | && TREE_CONSTANT (DECL_INITIAL (decl)) | |
1594 | /* Check for cases where this is sub-optimal, even though valid. */ | |
61a127b3 | 1595 | && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR) |
8d08fdba MS |
1596 | return DECL_INITIAL (decl); |
1597 | return decl; | |
1598 | } | |
1599 | \f | |
8d08fdba MS |
1600 | /* Common subroutines of build_new and build_vec_delete. */ |
1601 | ||
c787dd82 | 1602 | /* Call the global __builtin_delete to delete ADDR. */ |
8d08fdba | 1603 | |
bd6dd845 | 1604 | static tree |
362efdc1 | 1605 | build_builtin_delete_call (tree addr) |
8d08fdba | 1606 | { |
a6ecf8b6 | 1607 | mark_used (global_delete_fndecl); |
0c11ada6 | 1608 | return build_call (global_delete_fndecl, build_tree_list (NULL_TREE, addr)); |
8d08fdba MS |
1609 | } |
1610 | \f | |
1611 | /* Generate a C++ "new" expression. DECL is either a TREE_LIST | |
1612 | (which needs to go through some sort of groktypename) or it | |
1613 | is the name of the class we are newing. INIT is an initialization value. | |
1614 | It is either an EXPRLIST, an EXPR_NO_COMMAS, or something in braces. | |
1615 | If INIT is void_type_node, it means do *not* call a constructor | |
1616 | for this instance. | |
1617 | ||
1618 | For types with constructors, the data returned is initialized | |
1619 | by the appropriate constructor. | |
1620 | ||
1621 | Whether the type has a constructor or not, if it has a pointer | |
1622 | to a virtual function table, then that pointer is set up | |
1623 | here. | |
1624 | ||
1625 | Unless I am mistaken, a call to new () will return initialized | |
1626 | data regardless of whether the constructor itself is private or | |
8926095f | 1627 | not. NOPE; new fails if the constructor is private (jcm). |
8d08fdba MS |
1628 | |
1629 | Note that build_new does nothing to assure that any special | |
1630 | alignment requirements of the type are met. Rather, it leaves | |
1631 | it up to malloc to do the right thing. Otherwise, folding to | |
1632 | the right alignment cal cause problems if the user tries to later | |
1633 | free the memory returned by `new'. | |
1634 | ||
1635 | PLACEMENT is the `placement' list for user-defined operator new (). */ | |
1636 | ||
1637 | tree | |
058b15c1 MM |
1638 | build_new (tree placement, tree type, tree nelts, tree init, |
1639 | int use_global_new) | |
8d08fdba | 1640 | { |
058b15c1 | 1641 | tree rval; |
8d08fdba | 1642 | |
058b15c1 | 1643 | if (type == error_mark_node) |
8d08fdba MS |
1644 | return error_mark_node; |
1645 | ||
5156628f | 1646 | if (processing_template_decl) |
5566b478 | 1647 | { |
c006d942 | 1648 | rval = build_min (NEW_EXPR, build_pointer_type (type), |
058b15c1 | 1649 | placement, type, nelts, init); |
5566b478 | 1650 | NEW_EXPR_USE_GLOBAL (rval) = use_global_new; |
c1cca8d4 | 1651 | TREE_SIDE_EFFECTS (rval) = 1; |
5566b478 MS |
1652 | return rval; |
1653 | } | |
1654 | ||
8926095f MS |
1655 | /* ``A reference cannot be created by the new operator. A reference |
1656 | is not an object (8.2.2, 8.4.3), so a pointer to it could not be | |
1657 | returned by new.'' ARM 5.3.3 */ | |
1658 | if (TREE_CODE (type) == REFERENCE_TYPE) | |
8d08fdba | 1659 | { |
8251199e | 1660 | error ("new cannot be applied to a reference type"); |
a0d5fba7 | 1661 | type = TREE_TYPE (type); |
8d08fdba MS |
1662 | } |
1663 | ||
b7484fbe MS |
1664 | if (TREE_CODE (type) == FUNCTION_TYPE) |
1665 | { | |
8251199e | 1666 | error ("new cannot be applied to a function type"); |
b7484fbe MS |
1667 | return error_mark_node; |
1668 | } | |
1669 | ||
f293ce4b RS |
1670 | rval = build4 (NEW_EXPR, build_pointer_type (type), placement, type, |
1671 | nelts, init); | |
a0d5fba7 JM |
1672 | NEW_EXPR_USE_GLOBAL (rval) = use_global_new; |
1673 | TREE_SIDE_EFFECTS (rval) = 1; | |
b3ab27f3 MM |
1674 | rval = build_new_1 (rval); |
1675 | if (rval == error_mark_node) | |
1676 | return error_mark_node; | |
a0d5fba7 JM |
1677 | |
1678 | /* Wrap it in a NOP_EXPR so warn_if_unused_value doesn't complain. */ | |
1679 | rval = build1 (NOP_EXPR, TREE_TYPE (rval), rval); | |
6de9cd9a | 1680 | TREE_NO_WARNING (rval) = 1; |
a0d5fba7 | 1681 | |
a0d5fba7 JM |
1682 | return rval; |
1683 | } | |
1684 | ||
c6002625 | 1685 | /* Given a Java class, return a decl for the corresponding java.lang.Class. */ |
743f140d | 1686 | |
e97f22c9 | 1687 | tree |
362efdc1 | 1688 | build_java_class_ref (tree type) |
743f140d | 1689 | { |
ae0ed63a | 1690 | tree name = NULL_TREE, class_decl; |
d1a458c4 TT |
1691 | static tree CL_suffix = NULL_TREE; |
1692 | if (CL_suffix == NULL_TREE) | |
1693 | CL_suffix = get_identifier("class$"); | |
743f140d PB |
1694 | if (jclass_node == NULL_TREE) |
1695 | { | |
400500c4 | 1696 | jclass_node = IDENTIFIER_GLOBAL_VALUE (get_identifier ("jclass")); |
743f140d | 1697 | if (jclass_node == NULL_TREE) |
15a7ee29 | 1698 | fatal_error ("call to Java constructor, while %<jclass%> undefined"); |
400500c4 | 1699 | |
743f140d PB |
1700 | jclass_node = TREE_TYPE (jclass_node); |
1701 | } | |
23d4e4cc | 1702 | |
f4f206f4 | 1703 | /* Mangle the class$ field. */ |
1f84ec23 MM |
1704 | { |
1705 | tree field; | |
1706 | for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field)) | |
1707 | if (DECL_NAME (field) == CL_suffix) | |
1708 | { | |
92643fea MM |
1709 | mangle_decl (field); |
1710 | name = DECL_ASSEMBLER_NAME (field); | |
1f84ec23 MM |
1711 | break; |
1712 | } | |
1713 | if (!field) | |
1f978f5f | 1714 | internal_error ("can't find class$"); |
23d4e4cc | 1715 | } |
23d4e4cc | 1716 | |
743f140d PB |
1717 | class_decl = IDENTIFIER_GLOBAL_VALUE (name); |
1718 | if (class_decl == NULL_TREE) | |
1719 | { | |
743f140d PB |
1720 | class_decl = build_decl (VAR_DECL, name, TREE_TYPE (jclass_node)); |
1721 | TREE_STATIC (class_decl) = 1; | |
1722 | DECL_EXTERNAL (class_decl) = 1; | |
1723 | TREE_PUBLIC (class_decl) = 1; | |
1724 | DECL_ARTIFICIAL (class_decl) = 1; | |
1725 | DECL_IGNORED_P (class_decl) = 1; | |
1726 | pushdecl_top_level (class_decl); | |
0e6df31e | 1727 | make_decl_rtl (class_decl); |
743f140d PB |
1728 | } |
1729 | return class_decl; | |
1730 | } | |
1731 | ||
834c6dff | 1732 | |
a0d5fba7 JM |
1733 | /* Called from cplus_expand_expr when expanding a NEW_EXPR. The return |
1734 | value is immediately handed to expand_expr. */ | |
1735 | ||
834c6dff | 1736 | static tree |
362efdc1 | 1737 | build_new_1 (tree exp) |
a0d5fba7 | 1738 | { |
56c5d8bf | 1739 | tree placement, init; |
6de9cd9a | 1740 | tree true_type, size, rval; |
f4f4610e MM |
1741 | /* The type of the new-expression. (This type is always a pointer |
1742 | type.) */ | |
1743 | tree pointer_type; | |
1744 | /* The type pointed to by POINTER_TYPE. */ | |
1745 | tree type; | |
1746 | /* The type being allocated. For "new T[...]" this will be an | |
1747 | ARRAY_TYPE. */ | |
f30efcb7 | 1748 | tree full_type; |
f4f4610e MM |
1749 | /* A pointer type pointing to to the FULL_TYPE. */ |
1750 | tree full_pointer_type; | |
a48cccea | 1751 | tree outer_nelts = NULL_TREE; |
a703fb38 | 1752 | tree nelts = NULL_TREE; |
f4f4610e MM |
1753 | tree alloc_call, alloc_expr; |
1754 | /* The address returned by the call to "operator new". This node is | |
1755 | a VAR_DECL and is therefore reusable. */ | |
1756 | tree alloc_node; | |
46ff5047 | 1757 | tree alloc_fn; |
8b5e2ce4 | 1758 | tree cookie_expr, init_expr; |
a0d5fba7 | 1759 | int has_array = 0; |
834c6dff | 1760 | enum tree_code code; |
089d6ea7 | 1761 | int nothrow, check_new; |
834c6dff MM |
1762 | /* Nonzero if the user wrote `::new' rather than just `new'. */ |
1763 | int globally_qualified_p; | |
743f140d | 1764 | int use_java_new = 0; |
834c6dff MM |
1765 | /* If non-NULL, the number of extra bytes to allocate at the |
1766 | beginning of the storage allocated for an array-new expression in | |
1767 | order to store the number of elements. */ | |
1768 | tree cookie_size = NULL_TREE; | |
3f41ffd8 MM |
1769 | /* True if the function we are calling is a placement allocation |
1770 | function. */ | |
1771 | bool placement_allocation_fn_p; | |
4f649415 | 1772 | tree args = NULL_TREE; |
f4f4610e | 1773 | /* True if the storage must be initialized, either by a constructor |
34cd5ae7 | 1774 | or due to an explicit new-initializer. */ |
f4f4610e MM |
1775 | bool is_initialized; |
1776 | /* The address of the thing allocated, not including any cookie. In | |
1777 | particular, if an array cookie is in use, DATA_ADDR is the | |
1778 | address of the first array element. This node is a VAR_DECL, and | |
1779 | is therefore reusable. */ | |
1780 | tree data_addr; | |
6de9cd9a | 1781 | tree init_preeval_expr = NULL_TREE; |
a0d5fba7 JM |
1782 | |
1783 | placement = TREE_OPERAND (exp, 0); | |
1784 | type = TREE_OPERAND (exp, 1); | |
058b15c1 MM |
1785 | nelts = TREE_OPERAND (exp, 2); |
1786 | init = TREE_OPERAND (exp, 3); | |
834c6dff | 1787 | globally_qualified_p = NEW_EXPR_USE_GLOBAL (exp); |
a0d5fba7 | 1788 | |
058b15c1 | 1789 | if (nelts) |
a0d5fba7 | 1790 | { |
9117ccad RH |
1791 | tree index; |
1792 | ||
a0d5fba7 | 1793 | has_array = 1; |
058b15c1 | 1794 | outer_nelts = nelts; |
9117ccad RH |
1795 | |
1796 | /* ??? The middle-end will error on us for building a VLA outside a | |
1797 | function context. Methinks that's not it's purvey. So we'll do | |
1798 | our own VLA layout later. */ | |
1799 | ||
a48cccea | 1800 | full_type = build_cplus_array_type (type, NULL_TREE); |
9117ccad RH |
1801 | |
1802 | index = convert (sizetype, nelts); | |
1803 | index = size_binop (MINUS_EXPR, index, size_one_node); | |
1804 | TYPE_DOMAIN (full_type) = build_index_type (index); | |
a0d5fba7 | 1805 | } |
f30efcb7 JM |
1806 | else |
1807 | full_type = type; | |
1808 | ||
a0d5fba7 JM |
1809 | true_type = type; |
1810 | ||
834c6dff MM |
1811 | code = has_array ? VEC_NEW_EXPR : NEW_EXPR; |
1812 | ||
8d08fdba MS |
1813 | /* If our base type is an array, then make sure we know how many elements |
1814 | it has. */ | |
1815 | while (TREE_CODE (true_type) == ARRAY_TYPE) | |
1816 | { | |
1817 | tree this_nelts = array_type_nelts_top (true_type); | |
ab76ca54 | 1818 | nelts = cp_build_binary_op (MULT_EXPR, nelts, this_nelts); |
8d08fdba MS |
1819 | true_type = TREE_TYPE (true_type); |
1820 | } | |
5566b478 | 1821 | |
66543169 | 1822 | if (!complete_type_or_else (true_type, exp)) |
8f259df3 | 1823 | return error_mark_node; |
5566b478 | 1824 | |
cc600f33 | 1825 | if (TREE_CODE (true_type) == VOID_TYPE) |
e1cd6e56 | 1826 | { |
15a7ee29 | 1827 | error ("invalid type %<void%> for new"); |
e1cd6e56 MS |
1828 | return error_mark_node; |
1829 | } | |
1830 | ||
a7a64a77 MM |
1831 | if (abstract_virtuals_error (NULL_TREE, true_type)) |
1832 | return error_mark_node; | |
8926095f | 1833 | |
f4f4610e MM |
1834 | is_initialized = (TYPE_NEEDS_CONSTRUCTING (type) || init); |
1835 | if (CP_TYPE_CONST_P (true_type) && !is_initialized) | |
1836 | { | |
15a7ee29 | 1837 | error ("uninitialized const in %<new%> of %q#T", true_type); |
f4f4610e MM |
1838 | return error_mark_node; |
1839 | } | |
1840 | ||
089d6ea7 MM |
1841 | size = size_in_bytes (true_type); |
1842 | if (has_array) | |
9117ccad RH |
1843 | { |
1844 | tree n, bitsize; | |
1845 | ||
1846 | /* Do our own VLA layout. Setting TYPE_SIZE/_UNIT is necessary in | |
1847 | order for the <INIT_EXPR <*foo> <CONSTRUCTOR ...>> to be valid. */ | |
1848 | ||
1849 | n = convert (sizetype, nelts); | |
1850 | size = size_binop (MULT_EXPR, size, n); | |
1851 | TYPE_SIZE_UNIT (full_type) = size; | |
1852 | ||
1853 | n = convert (bitsizetype, nelts); | |
1854 | bitsize = size_binop (MULT_EXPR, TYPE_SIZE (true_type), n); | |
1855 | TYPE_SIZE (full_type) = bitsize; | |
1856 | } | |
a28e3c7f | 1857 | |
e92cc029 | 1858 | /* Allocate the object. */ |
9bfadf57 | 1859 | if (! placement && TYPE_FOR_JAVA (true_type)) |
743f140d | 1860 | { |
8c1bd4f5 | 1861 | tree class_addr, alloc_decl; |
743f140d | 1862 | tree class_decl = build_java_class_ref (true_type); |
8b60264b | 1863 | static const char alloc_name[] = "_Jv_AllocObject"; |
6de9cd9a | 1864 | |
743f140d | 1865 | use_java_new = 1; |
6de9cd9a | 1866 | alloc_decl = NULL; |
6961a592 GB |
1867 | if (!get_global_value_if_present (get_identifier (alloc_name), |
1868 | &alloc_decl)) | |
b1e5b86c | 1869 | { |
15a7ee29 | 1870 | error ("call to Java constructor with %qs undefined", alloc_name); |
6961a592 GB |
1871 | return error_mark_node; |
1872 | } | |
1873 | else if (really_overloaded_fn (alloc_decl)) | |
b1e5b86c | 1874 | { |
15a7ee29 | 1875 | error ("%qD should never be overloaded", alloc_decl); |
6961a592 GB |
1876 | return error_mark_node; |
1877 | } | |
1878 | alloc_decl = OVL_CURRENT (alloc_decl); | |
743f140d | 1879 | class_addr = build1 (ADDR_EXPR, jclass_node, class_decl); |
96790071 JM |
1880 | alloc_call = (build_function_call |
1881 | (alloc_decl, | |
245c3c04 | 1882 | build_tree_list (NULL_TREE, class_addr))); |
743f140d | 1883 | } |
8d08fdba MS |
1884 | else |
1885 | { | |
834c6dff | 1886 | tree fnname; |
9f880ef9 | 1887 | tree fns; |
834c6dff | 1888 | |
596ea4e5 | 1889 | fnname = ansi_opname (code); |
834c6dff | 1890 | |
089d6ea7 MM |
1891 | if (!globally_qualified_p |
1892 | && CLASS_TYPE_P (true_type) | |
1893 | && (has_array | |
1894 | ? TYPE_HAS_ARRAY_NEW_OPERATOR (true_type) | |
1895 | : TYPE_HAS_NEW_OPERATOR (true_type))) | |
1896 | { | |
1897 | /* Use a class-specific operator new. */ | |
1898 | /* If a cookie is required, add some extra space. */ | |
1899 | if (has_array && TYPE_VEC_NEW_USES_COOKIE (true_type)) | |
1900 | { | |
46e995e0 | 1901 | cookie_size = targetm.cxx.get_cookie_size (true_type); |
089d6ea7 MM |
1902 | size = size_binop (PLUS_EXPR, size, cookie_size); |
1903 | } | |
1904 | /* Create the argument list. */ | |
1905 | args = tree_cons (NULL_TREE, size, placement); | |
9f880ef9 MM |
1906 | /* Do name-lookup to find the appropriate operator. */ |
1907 | fns = lookup_fnfields (true_type, fnname, /*protect=*/2); | |
1908 | if (TREE_CODE (fns) == TREE_LIST) | |
1909 | { | |
15a7ee29 | 1910 | error ("request for member %qD is ambiguous", fnname); |
9f880ef9 MM |
1911 | print_candidates (fns); |
1912 | return error_mark_node; | |
1913 | } | |
1914 | alloc_call = build_new_method_call (build_dummy_object (true_type), | |
1915 | fns, args, | |
1916 | /*conversion_path=*/NULL_TREE, | |
1917 | LOOKUP_NORMAL); | |
089d6ea7 | 1918 | } |
834c6dff | 1919 | else |
089d6ea7 MM |
1920 | { |
1921 | /* Use a global operator new. */ | |
125e6594 MM |
1922 | /* See if a cookie might be required. */ |
1923 | if (has_array && TYPE_VEC_NEW_USES_COOKIE (true_type)) | |
46e995e0 | 1924 | cookie_size = targetm.cxx.get_cookie_size (true_type); |
125e6594 MM |
1925 | else |
1926 | cookie_size = NULL_TREE; | |
1927 | ||
1928 | alloc_call = build_operator_new_call (fnname, placement, | |
1929 | &size, &cookie_size); | |
089d6ea7 | 1930 | } |
8d08fdba MS |
1931 | } |
1932 | ||
96790071 | 1933 | if (alloc_call == error_mark_node) |
2bb5d995 JM |
1934 | return error_mark_node; |
1935 | ||
a6111661 JM |
1936 | /* In the simple case, we can stop now. */ |
1937 | pointer_type = build_pointer_type (type); | |
1938 | if (!cookie_size && !is_initialized) | |
1939 | return build_nop (pointer_type, alloc_call); | |
1940 | ||
1941 | /* While we're working, use a pointer to the type we've actually | |
1942 | allocated. Store the result of the call in a variable so that we | |
1943 | can use it more than once. */ | |
1944 | full_pointer_type = build_pointer_type (full_type); | |
1945 | alloc_expr = get_target_expr (build_nop (full_pointer_type, alloc_call)); | |
1946 | alloc_node = TARGET_EXPR_SLOT (alloc_expr); | |
1947 | ||
1948 | /* Strip any COMPOUND_EXPRs from ALLOC_CALL. */ | |
1949 | while (TREE_CODE (alloc_call) == COMPOUND_EXPR) | |
1950 | alloc_call = TREE_OPERAND (alloc_call, 1); | |
1951 | alloc_fn = get_callee_fndecl (alloc_call); | |
50bc768d | 1952 | gcc_assert (alloc_fn != NULL_TREE); |
089d6ea7 | 1953 | |
3f41ffd8 MM |
1954 | /* Now, check to see if this function is actually a placement |
1955 | allocation function. This can happen even when PLACEMENT is NULL | |
1956 | because we might have something like: | |
1957 | ||
1958 | struct S { void* operator new (size_t, int i = 0); }; | |
1959 | ||
1960 | A call to `new S' will get this allocation function, even though | |
1961 | there is no explicit placement argument. If there is more than | |
1962 | one argument, or there are variable arguments, then this is a | |
1963 | placement allocation function. */ | |
1964 | placement_allocation_fn_p | |
46ff5047 MM |
1965 | = (type_num_arguments (TREE_TYPE (alloc_fn)) > 1 |
1966 | || varargs_function_p (alloc_fn)); | |
96790071 | 1967 | |
a6111661 JM |
1968 | /* Preevaluate the placement args so that we don't reevaluate them for a |
1969 | placement delete. */ | |
1970 | if (placement_allocation_fn_p) | |
1971 | { | |
6de9cd9a DN |
1972 | tree inits; |
1973 | stabilize_call (alloc_call, &inits); | |
a6111661 | 1974 | if (inits) |
f293ce4b RS |
1975 | alloc_expr = build2 (COMPOUND_EXPR, TREE_TYPE (alloc_expr), inits, |
1976 | alloc_expr); | |
a6111661 JM |
1977 | } |
1978 | ||
047f64a3 JM |
1979 | /* unless an allocation function is declared with an empty excep- |
1980 | tion-specification (_except.spec_), throw(), it indicates failure to | |
1981 | allocate storage by throwing a bad_alloc exception (clause _except_, | |
1982 | _lib.bad.alloc_); it returns a non-null pointer otherwise If the allo- | |
1983 | cation function is declared with an empty exception-specification, | |
1984 | throw(), it returns null to indicate failure to allocate storage and a | |
1985 | non-null pointer otherwise. | |
1986 | ||
1987 | So check for a null exception spec on the op new we just called. */ | |
1988 | ||
46ff5047 | 1989 | nothrow = TYPE_NOTHROW_P (TREE_TYPE (alloc_fn)); |
743f140d | 1990 | check_new = (flag_check_new || nothrow) && ! use_java_new; |
047f64a3 | 1991 | |
089d6ea7 | 1992 | if (cookie_size) |
8d08fdba | 1993 | { |
96790071 | 1994 | tree cookie; |
46e995e0 | 1995 | tree cookie_ptr; |
f4f4610e MM |
1996 | |
1997 | /* Adjust so we're pointing to the start of the object. */ | |
f293ce4b RS |
1998 | data_addr = get_target_expr (build2 (PLUS_EXPR, full_pointer_type, |
1999 | alloc_node, cookie_size)); | |
96790071 | 2000 | |
834c6dff | 2001 | /* Store the number of bytes allocated so that we can know how |
3461fba7 NS |
2002 | many elements to destroy later. We use the last sizeof |
2003 | (size_t) bytes to store the number of elements. */ | |
f293ce4b RS |
2004 | cookie_ptr = build2 (MINUS_EXPR, build_pointer_type (sizetype), |
2005 | data_addr, size_in_bytes (sizetype)); | |
46e995e0 | 2006 | cookie = build_indirect_ref (cookie_ptr, NULL); |
1f84ec23 | 2007 | |
f293ce4b | 2008 | cookie_expr = build2 (MODIFY_EXPR, sizetype, cookie, nelts); |
46e995e0 PB |
2009 | |
2010 | if (targetm.cxx.cookie_has_size ()) | |
2011 | { | |
2012 | /* Also store the element size. */ | |
f293ce4b RS |
2013 | cookie_ptr = build2 (MINUS_EXPR, build_pointer_type (sizetype), |
2014 | cookie_ptr, size_in_bytes (sizetype)); | |
46e995e0 | 2015 | cookie = build_indirect_ref (cookie_ptr, NULL); |
f293ce4b RS |
2016 | cookie = build2 (MODIFY_EXPR, sizetype, cookie, |
2017 | size_in_bytes(true_type)); | |
2018 | cookie_expr = build2 (COMPOUND_EXPR, TREE_TYPE (cookie_expr), | |
2019 | cookie, cookie_expr); | |
46e995e0 | 2020 | } |
8b5e2ce4 | 2021 | data_addr = TARGET_EXPR_SLOT (data_addr); |
8d08fdba | 2022 | } |
96790071 | 2023 | else |
8b5e2ce4 JM |
2024 | { |
2025 | cookie_expr = NULL_TREE; | |
2026 | data_addr = alloc_node; | |
2027 | } | |
8d08fdba | 2028 | |
6de9cd9a DN |
2029 | /* Now initialize the allocated object. Note that we preevaluate the |
2030 | initialization expression, apart from the actual constructor call or | |
2031 | assignment--we do this because we want to delay the allocation as long | |
2032 | as possible in order to minimize the size of the exception region for | |
2033 | placement delete. */ | |
f4f4610e | 2034 | if (is_initialized) |
8d08fdba | 2035 | { |
6de9cd9a DN |
2036 | bool stable; |
2037 | ||
f4f4610e | 2038 | init_expr = build_indirect_ref (data_addr, NULL); |
f30efcb7 JM |
2039 | |
2040 | if (init == void_zero_node) | |
1cb8292f | 2041 | init = build_default_init (full_type, nelts); |
6de9cd9a | 2042 | else if (init && has_array) |
33bd39a2 | 2043 | pedwarn ("ISO C++ forbids initialization in array new"); |
f30efcb7 JM |
2044 | |
2045 | if (has_array) | |
6de9cd9a DN |
2046 | { |
2047 | init_expr | |
2048 | = build_vec_init (init_expr, | |
2049 | cp_build_binary_op (MINUS_EXPR, outer_nelts, | |
2050 | integer_one_node), | |
2051 | init, /*from_array=*/0); | |
2052 | ||
2053 | /* An array initialization is stable because the initialization | |
2054 | of each element is a full-expression, so the temporaries don't | |
2055 | leak out. */ | |
2056 | stable = true; | |
2057 | } | |
f30efcb7 | 2058 | else if (TYPE_NEEDS_CONSTRUCTING (type)) |
6de9cd9a DN |
2059 | { |
2060 | init_expr = build_special_member_call (init_expr, | |
2061 | complete_ctor_identifier, | |
cad7e87b | 2062 | init, true_type, |
6de9cd9a DN |
2063 | LOOKUP_NORMAL); |
2064 | stable = stabilize_init (init_expr, &init_preeval_expr); | |
2065 | } | |
f30efcb7 | 2066 | else |
8d08fdba | 2067 | { |
01240200 MM |
2068 | /* We are processing something like `new int (10)', which |
2069 | means allocate an int, and initialize it with 10. */ | |
f30efcb7 JM |
2070 | |
2071 | if (TREE_CODE (init) == TREE_LIST) | |
c7b62f14 | 2072 | init = build_x_compound_expr_from_list (init, "new initializer"); |
6de9cd9a | 2073 | |
8dc2b103 NS |
2074 | else |
2075 | gcc_assert (TREE_CODE (init) != CONSTRUCTOR | |
2076 | || TREE_TYPE (init) != NULL_TREE); | |
80170791 | 2077 | |
f30efcb7 | 2078 | init_expr = build_modify_expr (init_expr, INIT_EXPR, init); |
6de9cd9a | 2079 | stable = stabilize_init (init_expr, &init_preeval_expr); |
96790071 JM |
2080 | } |
2081 | ||
2082 | if (init_expr == error_mark_node) | |
2083 | return error_mark_node; | |
1f109f0f | 2084 | |
20c39572 JM |
2085 | /* If any part of the object initialization terminates by throwing an |
2086 | exception and a suitable deallocation function can be found, the | |
2087 | deallocation function is called to free the memory in which the | |
2088 | object was being constructed, after which the exception continues | |
2089 | to propagate in the context of the new-expression. If no | |
2090 | unambiguous matching deallocation function can be found, | |
2091 | propagating the exception does not cause the object's memory to be | |
2092 | freed. */ | |
96790071 | 2093 | if (flag_exceptions && ! use_java_new) |
1f109f0f | 2094 | { |
2face519 | 2095 | enum tree_code dcode = has_array ? VEC_DELETE_EXPR : DELETE_EXPR; |
96790071 | 2096 | tree cleanup; |
a7d87521 | 2097 | |
5355deec | 2098 | /* The Standard is unclear here, but the right thing to do |
f4f4610e MM |
2099 | is to use the same method for finding deallocation |
2100 | functions that we use for finding allocation functions. */ | |
5bd61841 MM |
2101 | cleanup = build_op_delete_call (dcode, alloc_node, size, |
2102 | globally_qualified_p, | |
3f41ffd8 MM |
2103 | (placement_allocation_fn_p |
2104 | ? alloc_call : NULL_TREE)); | |
2bb14213 | 2105 | |
6de9cd9a DN |
2106 | if (!cleanup) |
2107 | /* We're done. */; | |
2108 | else if (stable) | |
2109 | /* This is much simpler if we were able to preevaluate all of | |
2110 | the arguments to the constructor call. */ | |
f293ce4b RS |
2111 | init_expr = build2 (TRY_CATCH_EXPR, void_type_node, |
2112 | init_expr, cleanup); | |
6de9cd9a DN |
2113 | else |
2114 | /* Ack! First we allocate the memory. Then we set our sentry | |
2115 | variable to true, and expand a cleanup that deletes the | |
2116 | memory if sentry is true. Then we run the constructor, and | |
2117 | finally clear the sentry. | |
2118 | ||
2119 | We need to do this because we allocate the space first, so | |
2120 | if there are any temporaries with cleanups in the | |
2121 | constructor args and we weren't able to preevaluate them, we | |
2122 | need this EH region to extend until end of full-expression | |
2123 | to preserve nesting. */ | |
da4768fe | 2124 | { |
96790071 | 2125 | tree end, sentry, begin; |
2face519 JM |
2126 | |
2127 | begin = get_target_expr (boolean_true_node); | |
659e5a7a | 2128 | CLEANUP_EH_ONLY (begin) = 1; |
2face519 | 2129 | |
659e5a7a JM |
2130 | sentry = TARGET_EXPR_SLOT (begin); |
2131 | ||
2132 | TARGET_EXPR_CLEANUP (begin) | |
f293ce4b RS |
2133 | = build3 (COND_EXPR, void_type_node, sentry, |
2134 | cleanup, void_zero_node); | |
2face519 | 2135 | |
f293ce4b RS |
2136 | end = build2 (MODIFY_EXPR, TREE_TYPE (sentry), |
2137 | sentry, boolean_false_node); | |
2face519 | 2138 | |
96790071 | 2139 | init_expr |
f293ce4b RS |
2140 | = build2 (COMPOUND_EXPR, void_type_node, begin, |
2141 | build2 (COMPOUND_EXPR, void_type_node, init_expr, | |
2142 | end)); | |
da4768fe | 2143 | } |
6de9cd9a | 2144 | |
1f109f0f | 2145 | } |
f4f4610e | 2146 | } |
8b5e2ce4 JM |
2147 | else |
2148 | init_expr = NULL_TREE; | |
2149 | ||
2150 | /* Now build up the return value in reverse order. */ | |
96790071 | 2151 | |
8b5e2ce4 | 2152 | rval = data_addr; |
2face519 | 2153 | |
8b5e2ce4 | 2154 | if (init_expr) |
f293ce4b | 2155 | rval = build2 (COMPOUND_EXPR, TREE_TYPE (rval), init_expr, rval); |
8b5e2ce4 | 2156 | if (cookie_expr) |
f293ce4b | 2157 | rval = build2 (COMPOUND_EXPR, TREE_TYPE (rval), cookie_expr, rval); |
8b5e2ce4 JM |
2158 | |
2159 | if (rval == alloc_node) | |
2160 | /* If we don't have an initializer or a cookie, strip the TARGET_EXPR | |
2161 | and return the call (which doesn't need to be adjusted). */ | |
2162 | rval = TARGET_EXPR_INITIAL (alloc_expr); | |
2163 | else | |
d18c083e | 2164 | { |
8b5e2ce4 JM |
2165 | if (check_new) |
2166 | { | |
2167 | tree ifexp = cp_build_binary_op (NE_EXPR, alloc_node, | |
2168 | integer_zero_node); | |
2169 | rval = build_conditional_expr (ifexp, rval, alloc_node); | |
2170 | } | |
d18c083e | 2171 | |
8b5e2ce4 JM |
2172 | /* Perform the allocation before anything else, so that ALLOC_NODE |
2173 | has been initialized before we start using it. */ | |
f293ce4b | 2174 | rval = build2 (COMPOUND_EXPR, TREE_TYPE (rval), alloc_expr, rval); |
8b5e2ce4 | 2175 | } |
51c184be | 2176 | |
6de9cd9a | 2177 | if (init_preeval_expr) |
f293ce4b | 2178 | rval = build2 (COMPOUND_EXPR, TREE_TYPE (rval), init_preeval_expr, rval); |
6de9cd9a | 2179 | |
f4f4610e | 2180 | /* Convert to the final type. */ |
d04a575f MM |
2181 | rval = build_nop (pointer_type, rval); |
2182 | ||
2183 | /* A new-expression is never an lvalue. */ | |
2184 | if (real_lvalue_p (rval)) | |
2185 | rval = build1 (NON_LVALUE_EXPR, TREE_TYPE (rval), rval); | |
2186 | ||
2187 | return rval; | |
8d08fdba MS |
2188 | } |
2189 | \f | |
f30432d7 | 2190 | static tree |
362efdc1 NN |
2191 | build_vec_delete_1 (tree base, tree maxindex, tree type, |
2192 | special_function_kind auto_delete_vec, int use_global_delete) | |
f30432d7 MS |
2193 | { |
2194 | tree virtual_size; | |
e92cc029 | 2195 | tree ptype = build_pointer_type (type = complete_type (type)); |
f30432d7 MS |
2196 | tree size_exp = size_in_bytes (type); |
2197 | ||
2198 | /* Temporary variables used by the loop. */ | |
2199 | tree tbase, tbase_init; | |
2200 | ||
2201 | /* This is the body of the loop that implements the deletion of a | |
2202 | single element, and moves temp variables to next elements. */ | |
2203 | tree body; | |
2204 | ||
2205 | /* This is the LOOP_EXPR that governs the deletion of the elements. */ | |
c7b62f14 | 2206 | tree loop = 0; |
f30432d7 MS |
2207 | |
2208 | /* This is the thing that governs what to do after the loop has run. */ | |
2209 | tree deallocate_expr = 0; | |
2210 | ||
2211 | /* This is the BIND_EXPR which holds the outermost iterator of the | |
2212 | loop. It is convenient to set this variable up and test it before | |
2213 | executing any other code in the loop. | |
2214 | This is also the containing expression returned by this function. */ | |
2215 | tree controller = NULL_TREE; | |
2216 | ||
b2153b98 | 2217 | /* We should only have 1-D arrays here. */ |
8dc2b103 | 2218 | gcc_assert (TREE_CODE (type) != ARRAY_TYPE); |
b2153b98 | 2219 | |
834c6dff | 2220 | if (! IS_AGGR_TYPE (type) || TYPE_HAS_TRIVIAL_DESTRUCTOR (type)) |
c7b62f14 | 2221 | goto no_destructor; |
f30432d7 | 2222 | |
708cae97 | 2223 | /* The below is short by the cookie size. */ |
fed3cef0 RK |
2224 | virtual_size = size_binop (MULT_EXPR, size_exp, |
2225 | convert (sizetype, maxindex)); | |
f30432d7 | 2226 | |
46e8c075 | 2227 | tbase = create_temporary_var (ptype); |
f30432d7 | 2228 | tbase_init = build_modify_expr (tbase, NOP_EXPR, |
f293ce4b RS |
2229 | fold (build2 (PLUS_EXPR, ptype, |
2230 | base, | |
2231 | virtual_size))); | |
f30432d7 | 2232 | DECL_REGISTER (tbase) = 1; |
f293ce4b RS |
2233 | controller = build3 (BIND_EXPR, void_type_node, tbase, |
2234 | NULL_TREE, NULL_TREE); | |
f30432d7 | 2235 | TREE_SIDE_EFFECTS (controller) = 1; |
f30432d7 | 2236 | |
f293ce4b RS |
2237 | body = build1 (EXIT_EXPR, void_type_node, |
2238 | build2 (EQ_EXPR, boolean_type_node, base, tbase)); | |
c7b62f14 NS |
2239 | body = build_compound_expr |
2240 | (body, build_modify_expr (tbase, NOP_EXPR, | |
f293ce4b | 2241 | build2 (MINUS_EXPR, ptype, tbase, size_exp))); |
c7b62f14 NS |
2242 | body = build_compound_expr |
2243 | (body, build_delete (ptype, tbase, sfk_complete_destructor, | |
2244 | LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 1)); | |
f30432d7 | 2245 | |
f293ce4b | 2246 | loop = build1 (LOOP_EXPR, void_type_node, body); |
c7b62f14 | 2247 | loop = build_compound_expr (tbase_init, loop); |
f30432d7 MS |
2248 | |
2249 | no_destructor: | |
2250 | /* If the delete flag is one, or anything else with the low bit set, | |
2251 | delete the storage. */ | |
86f45d2c | 2252 | if (auto_delete_vec != sfk_base_destructor) |
f30432d7 MS |
2253 | { |
2254 | tree base_tbd; | |
2255 | ||
708cae97 | 2256 | /* The below is short by the cookie size. */ |
fed3cef0 RK |
2257 | virtual_size = size_binop (MULT_EXPR, size_exp, |
2258 | convert (sizetype, maxindex)); | |
f30432d7 MS |
2259 | |
2260 | if (! TYPE_VEC_NEW_USES_COOKIE (type)) | |
2261 | /* no header */ | |
2262 | base_tbd = base; | |
2263 | else | |
2264 | { | |
834c6dff MM |
2265 | tree cookie_size; |
2266 | ||
46e995e0 | 2267 | cookie_size = targetm.cxx.get_cookie_size (type); |
834c6dff MM |
2268 | base_tbd |
2269 | = cp_convert (ptype, | |
ab76ca54 MM |
2270 | cp_build_binary_op (MINUS_EXPR, |
2271 | cp_convert (string_type_node, | |
2272 | base), | |
2273 | cookie_size)); | |
e92cc029 | 2274 | /* True size with header. */ |
834c6dff | 2275 | virtual_size = size_binop (PLUS_EXPR, virtual_size, cookie_size); |
f30432d7 | 2276 | } |
86f45d2c MM |
2277 | |
2278 | if (auto_delete_vec == sfk_deleting_destructor) | |
2279 | deallocate_expr = build_x_delete (base_tbd, | |
2280 | 2 | use_global_delete, | |
2281 | virtual_size); | |
f30432d7 MS |
2282 | } |
2283 | ||
c7b62f14 NS |
2284 | body = loop; |
2285 | if (!deallocate_expr) | |
2286 | ; | |
2287 | else if (!body) | |
2288 | body = deallocate_expr; | |
f30432d7 | 2289 | else |
c7b62f14 NS |
2290 | body = build_compound_expr (body, deallocate_expr); |
2291 | ||
2292 | if (!body) | |
2293 | body = integer_zero_node; | |
2294 | ||
f30432d7 | 2295 | /* Outermost wrapper: If pointer is null, punt. */ |
f293ce4b RS |
2296 | body = fold (build3 (COND_EXPR, void_type_node, |
2297 | fold (build2 (NE_EXPR, boolean_type_node, base, | |
2298 | convert (TREE_TYPE (base), | |
2299 | integer_zero_node))), | |
2300 | body, integer_zero_node)); | |
f30432d7 MS |
2301 | body = build1 (NOP_EXPR, void_type_node, body); |
2302 | ||
2303 | if (controller) | |
2304 | { | |
2305 | TREE_OPERAND (controller, 1) = body; | |
4e8dca1c | 2306 | body = controller; |
f30432d7 | 2307 | } |
4e8dca1c JM |
2308 | |
2309 | if (TREE_CODE (base) == SAVE_EXPR) | |
2310 | /* Pre-evaluate the SAVE_EXPR outside of the BIND_EXPR. */ | |
f293ce4b | 2311 | body = build2 (COMPOUND_EXPR, void_type_node, base, body); |
4e8dca1c | 2312 | |
8d245821 | 2313 | return convert_to_void (body, /*implicit=*/NULL); |
f30432d7 MS |
2314 | } |
2315 | ||
c395453c MM |
2316 | /* Create an unnamed variable of the indicated TYPE. */ |
2317 | ||
f1dedc31 | 2318 | tree |
362efdc1 | 2319 | create_temporary_var (tree type) |
8a72a046 | 2320 | { |
f1dedc31 MM |
2321 | tree decl; |
2322 | ||
2323 | decl = build_decl (VAR_DECL, NULL_TREE, type); | |
2324 | TREE_USED (decl) = 1; | |
2325 | DECL_ARTIFICIAL (decl) = 1; | |
f31686a3 | 2326 | DECL_SOURCE_LOCATION (decl) = input_location; |
f1dedc31 | 2327 | DECL_IGNORED_P (decl) = 1; |
b35d4555 | 2328 | DECL_CONTEXT (decl) = current_function_decl; |
f1dedc31 | 2329 | |
f1dedc31 | 2330 | return decl; |
8a72a046 MM |
2331 | } |
2332 | ||
f1dedc31 MM |
2333 | /* Create a new temporary variable of the indicated TYPE, initialized |
2334 | to INIT. | |
8a72a046 | 2335 | |
f1dedc31 MM |
2336 | It is not entered into current_binding_level, because that breaks |
2337 | things when it comes time to do final cleanups (which take place | |
2338 | "outside" the binding contour of the function). */ | |
2339 | ||
2340 | static tree | |
362efdc1 | 2341 | get_temp_regvar (tree type, tree init) |
f30432d7 | 2342 | { |
f1dedc31 | 2343 | tree decl; |
8a72a046 | 2344 | |
f1dedc31 | 2345 | decl = create_temporary_var (type); |
350fae66 | 2346 | add_decl_expr (decl); |
2a3398e1 | 2347 | |
f1dedc31 | 2348 | finish_expr_stmt (build_modify_expr (decl, INIT_EXPR, init)); |
8a72a046 | 2349 | |
f1dedc31 | 2350 | return decl; |
f30432d7 MS |
2351 | } |
2352 | ||
f1dedc31 MM |
2353 | /* `build_vec_init' returns tree structure that performs |
2354 | initialization of a vector of aggregate types. | |
8d08fdba | 2355 | |
f30efcb7 | 2356 | BASE is a reference to the vector, of ARRAY_TYPE. |
a48cccea JM |
2357 | MAXINDEX is the maximum index of the array (one less than the |
2358 | number of elements). It is only used if | |
2359 | TYPE_DOMAIN (TREE_TYPE (BASE)) == NULL_TREE. | |
8d08fdba MS |
2360 | INIT is the (possibly NULL) initializer. |
2361 | ||
2362 | FROM_ARRAY is 0 if we should init everything with INIT | |
2363 | (i.e., every element initialized from INIT). | |
2364 | FROM_ARRAY is 1 if we should index into INIT in parallel | |
2365 | with initialization of DECL. | |
2366 | FROM_ARRAY is 2 if we should index into INIT in parallel, | |
2367 | but use assignment instead of initialization. */ | |
2368 | ||
2369 | tree | |
362efdc1 | 2370 | build_vec_init (tree base, tree maxindex, tree init, int from_array) |
8d08fdba MS |
2371 | { |
2372 | tree rval; | |
8a72a046 | 2373 | tree base2 = NULL_TREE; |
8d08fdba | 2374 | tree size; |
e833cb11 | 2375 | tree itype = NULL_TREE; |
8a72a046 | 2376 | tree iterator; |
f30efcb7 JM |
2377 | /* The type of the array. */ |
2378 | tree atype = TREE_TYPE (base); | |
f1dedc31 | 2379 | /* The type of an element in the array. */ |
f30efcb7 | 2380 | tree type = TREE_TYPE (atype); |
f1dedc31 MM |
2381 | /* The type of a pointer to an element in the array. */ |
2382 | tree ptype; | |
2383 | tree stmt_expr; | |
2384 | tree compound_stmt; | |
2385 | int destroy_temps; | |
f5984164 | 2386 | tree try_block = NULL_TREE; |
8a72a046 | 2387 | int num_initialized_elts = 0; |
2a3398e1 NS |
2388 | bool is_global; |
2389 | ||
a48cccea JM |
2390 | if (TYPE_DOMAIN (atype)) |
2391 | maxindex = array_type_nelts (atype); | |
2392 | ||
2393 | if (maxindex == NULL_TREE || maxindex == error_mark_node) | |
8d08fdba MS |
2394 | return error_mark_node; |
2395 | ||
c8a3d889 AO |
2396 | if (init |
2397 | && (from_array == 2 | |
2398 | ? (!CLASS_TYPE_P (type) || !TYPE_HAS_COMPLEX_ASSIGN_REF (type)) | |
2399 | : !TYPE_NEEDS_CONSTRUCTING (type)) | |
f30efcb7 JM |
2400 | && ((TREE_CODE (init) == CONSTRUCTOR |
2401 | /* Don't do this if the CONSTRUCTOR might contain something | |
2402 | that might throw and require us to clean up. */ | |
2403 | && (CONSTRUCTOR_ELTS (init) == NULL_TREE | |
2404 | || ! TYPE_HAS_NONTRIVIAL_DESTRUCTOR (target_type (type)))) | |
2405 | || from_array)) | |
2406 | { | |
2407 | /* Do non-default initialization of POD arrays resulting from | |
2408 | brace-enclosed initializers. In this case, digest_init and | |
2409 | store_constructor will handle the semantics for us. */ | |
2410 | ||
f293ce4b | 2411 | stmt_expr = build2 (INIT_EXPR, atype, base, init); |
f30efcb7 JM |
2412 | return stmt_expr; |
2413 | } | |
2414 | ||
2415 | maxindex = cp_convert (ptrdiff_type_node, maxindex); | |
f1dedc31 | 2416 | ptype = build_pointer_type (type); |
8d08fdba | 2417 | size = size_in_bytes (type); |
f30efcb7 | 2418 | if (TREE_CODE (TREE_TYPE (base)) == ARRAY_TYPE) |
0a72704b | 2419 | base = cp_convert (ptype, decay_conversion (base)); |
8d08fdba | 2420 | |
f1dedc31 | 2421 | /* The code we are generating looks like: |
303b7406 | 2422 | ({ |
f1dedc31 | 2423 | T* t1 = (T*) base; |
f30efcb7 | 2424 | T* rval = t1; |
f1dedc31 MM |
2425 | ptrdiff_t iterator = maxindex; |
2426 | try { | |
4977bab6 | 2427 | for (; iterator != -1; --iterator) { |
f30efcb7 JM |
2428 | ... initialize *t1 ... |
2429 | ++t1; | |
4977bab6 | 2430 | } |
f1dedc31 MM |
2431 | } catch (...) { |
2432 | ... destroy elements that were constructed ... | |
2433 | } | |
303b7406 NS |
2434 | rval; |
2435 | }) | |
f1dedc31 MM |
2436 | |
2437 | We can omit the try and catch blocks if we know that the | |
2438 | initialization will never throw an exception, or if the array | |
f30efcb7 | 2439 | elements do not have destructors. We can omit the loop completely if |
f1dedc31 MM |
2440 | the elements of the array do not have constructors. |
2441 | ||
2442 | We actually wrap the entire body of the above in a STMT_EXPR, for | |
2443 | tidiness. | |
2444 | ||
2445 | When copying from array to another, when the array elements have | |
2446 | only trivial copy constructors, we should use __builtin_memcpy | |
2447 | rather than generating a loop. That way, we could take advantage | |
2448 | of whatever cleverness the back-end has for dealing with copies | |
2449 | of blocks of memory. */ | |
2450 | ||
2a3398e1 | 2451 | is_global = begin_init_stmts (&stmt_expr, &compound_stmt); |
f2c5f623 | 2452 | destroy_temps = stmts_are_full_exprs_p (); |
ae499cce | 2453 | current_stmt_tree ()->stmts_are_full_exprs_p = 0; |
f30efcb7 | 2454 | rval = get_temp_regvar (ptype, base); |
f1dedc31 | 2455 | base = get_temp_regvar (ptype, rval); |
8a72a046 | 2456 | iterator = get_temp_regvar (ptrdiff_type_node, maxindex); |
8d08fdba | 2457 | |
8a72a046 | 2458 | /* Protect the entire array initialization so that we can destroy |
f30efcb7 JM |
2459 | the partially constructed array if an exception is thrown. |
2460 | But don't do this if we're assigning. */ | |
2461 | if (flag_exceptions && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type) | |
2462 | && from_array != 2) | |
ed5511d9 MM |
2463 | { |
2464 | try_block = begin_try_block (); | |
ed5511d9 | 2465 | } |
8a72a046 | 2466 | |
f30efcb7 | 2467 | if (init != NULL_TREE && TREE_CODE (init) == CONSTRUCTOR) |
8d08fdba | 2468 | { |
f30efcb7 JM |
2469 | /* Do non-default initialization of non-POD arrays resulting from |
2470 | brace-enclosed initializers. */ | |
8a72a046 MM |
2471 | |
2472 | tree elts; | |
094fe153 JM |
2473 | from_array = 0; |
2474 | ||
8a72a046 | 2475 | for (elts = CONSTRUCTOR_ELTS (init); elts; elts = TREE_CHAIN (elts)) |
8d08fdba | 2476 | { |
8a72a046 | 2477 | tree elt = TREE_VALUE (elts); |
f1dedc31 | 2478 | tree baseref = build1 (INDIRECT_REF, type, base); |
8d08fdba | 2479 | |
8a72a046 | 2480 | num_initialized_elts++; |
8d08fdba | 2481 | |
67c03833 | 2482 | current_stmt_tree ()->stmts_are_full_exprs_p = 1; |
8a72a046 | 2483 | if (IS_AGGR_TYPE (type) || TREE_CODE (type) == ARRAY_TYPE) |
f1dedc31 | 2484 | finish_expr_stmt (build_aggr_init (baseref, elt, 0)); |
8a72a046 | 2485 | else |
f1dedc31 MM |
2486 | finish_expr_stmt (build_modify_expr (baseref, NOP_EXPR, |
2487 | elt)); | |
67c03833 | 2488 | current_stmt_tree ()->stmts_are_full_exprs_p = 0; |
8a72a046 | 2489 | |
f30efcb7 JM |
2490 | finish_expr_stmt (build_unary_op (PREINCREMENT_EXPR, base, 0)); |
2491 | finish_expr_stmt (build_unary_op (PREDECREMENT_EXPR, iterator, 0)); | |
8d08fdba | 2492 | } |
8d08fdba | 2493 | |
8a72a046 MM |
2494 | /* Clear out INIT so that we don't get confused below. */ |
2495 | init = NULL_TREE; | |
8d08fdba | 2496 | } |
8a72a046 | 2497 | else if (from_array) |
8d08fdba | 2498 | { |
8a72a046 MM |
2499 | /* If initializing one array from another, initialize element by |
2500 | element. We rely upon the below calls the do argument | |
2501 | checking. */ | |
8a72a046 MM |
2502 | if (init) |
2503 | { | |
0a72704b | 2504 | base2 = decay_conversion (init); |
8a72a046 MM |
2505 | itype = TREE_TYPE (base2); |
2506 | base2 = get_temp_regvar (itype, base2); | |
2507 | itype = TREE_TYPE (itype); | |
2508 | } | |
2509 | else if (TYPE_LANG_SPECIFIC (type) | |
2510 | && TYPE_NEEDS_CONSTRUCTING (type) | |
2511 | && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (type)) | |
2512 | { | |
2513 | error ("initializer ends prematurely"); | |
2514 | return error_mark_node; | |
2515 | } | |
2516 | } | |
8d08fdba | 2517 | |
8a72a046 MM |
2518 | /* Now, default-initialize any remaining elements. We don't need to |
2519 | do that if a) the type does not need constructing, or b) we've | |
094fe153 JM |
2520 | already initialized all the elements. |
2521 | ||
2522 | We do need to keep going if we're copying an array. */ | |
2523 | ||
2524 | if (from_array | |
2525 | || (TYPE_NEEDS_CONSTRUCTING (type) | |
665f2503 | 2526 | && ! (host_integerp (maxindex, 0) |
05bccae2 | 2527 | && (num_initialized_elts |
665f2503 | 2528 | == tree_low_cst (maxindex, 0) + 1)))) |
8a72a046 | 2529 | { |
37e05cd5 | 2530 | /* If the ITERATOR is equal to -1, then we don't have to loop; |
8a72a046 | 2531 | we've already initialized all the elements. */ |
4977bab6 | 2532 | tree for_stmt; |
f1dedc31 MM |
2533 | tree elt_init; |
2534 | ||
4977bab6 ZW |
2535 | for_stmt = begin_for_stmt (); |
2536 | finish_for_init_stmt (for_stmt); | |
f293ce4b RS |
2537 | finish_for_cond (build2 (NE_EXPR, boolean_type_node, |
2538 | iterator, integer_minus_one_node), | |
4977bab6 ZW |
2539 | for_stmt); |
2540 | finish_for_expr (build_unary_op (PREDECREMENT_EXPR, iterator, 0), | |
2541 | for_stmt); | |
8d08fdba MS |
2542 | |
2543 | if (from_array) | |
2544 | { | |
2545 | tree to = build1 (INDIRECT_REF, type, base); | |
2546 | tree from; | |
2547 | ||
2548 | if (base2) | |
2549 | from = build1 (INDIRECT_REF, itype, base2); | |
2550 | else | |
2551 | from = NULL_TREE; | |
2552 | ||
2553 | if (from_array == 2) | |
f1dedc31 | 2554 | elt_init = build_modify_expr (to, NOP_EXPR, from); |
8d08fdba | 2555 | else if (TYPE_NEEDS_CONSTRUCTING (type)) |
f1dedc31 | 2556 | elt_init = build_aggr_init (to, from, 0); |
8d08fdba | 2557 | else if (from) |
f1dedc31 | 2558 | elt_init = build_modify_expr (to, NOP_EXPR, from); |
8d08fdba | 2559 | else |
8dc2b103 | 2560 | gcc_unreachable (); |
8d08fdba MS |
2561 | } |
2562 | else if (TREE_CODE (type) == ARRAY_TYPE) | |
2563 | { | |
2564 | if (init != 0) | |
f30efcb7 JM |
2565 | sorry |
2566 | ("cannot initialize multi-dimensional array with initializer"); | |
2567 | elt_init = build_vec_init (build1 (INDIRECT_REF, type, base), | |
a48cccea | 2568 | 0, 0, 0); |
f1dedc31 MM |
2569 | } |
2570 | else | |
2571 | elt_init = build_aggr_init (build1 (INDIRECT_REF, type, base), | |
2572 | init, 0); | |
2573 | ||
2a3398e1 NS |
2574 | current_stmt_tree ()->stmts_are_full_exprs_p = 1; |
2575 | finish_expr_stmt (elt_init); | |
2576 | current_stmt_tree ()->stmts_are_full_exprs_p = 0; | |
8d08fdba | 2577 | |
f30efcb7 | 2578 | finish_expr_stmt (build_unary_op (PREINCREMENT_EXPR, base, 0)); |
8d08fdba | 2579 | if (base2) |
f30efcb7 | 2580 | finish_expr_stmt (build_unary_op (PREINCREMENT_EXPR, base2, 0)); |
0fac6b0b | 2581 | |
4977bab6 | 2582 | finish_for_stmt (for_stmt); |
8d08fdba | 2583 | } |
8a72a046 MM |
2584 | |
2585 | /* Make sure to cleanup any partially constructed elements. */ | |
f30efcb7 JM |
2586 | if (flag_exceptions && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type) |
2587 | && from_array != 2) | |
f1dedc31 MM |
2588 | { |
2589 | tree e; | |
b2153b98 KL |
2590 | tree m = cp_build_binary_op (MINUS_EXPR, maxindex, iterator); |
2591 | ||
2592 | /* Flatten multi-dimensional array since build_vec_delete only | |
2593 | expects one-dimensional array. */ | |
2594 | if (TREE_CODE (type) == ARRAY_TYPE) | |
2595 | { | |
2596 | m = cp_build_binary_op (MULT_EXPR, m, | |
2597 | array_type_nelts_total (type)); | |
2598 | type = strip_array_types (type); | |
2599 | } | |
8d08fdba | 2600 | |
ed5511d9 | 2601 | finish_cleanup_try_block (try_block); |
303b7406 | 2602 | e = build_vec_delete_1 (rval, m, type, sfk_base_destructor, |
f1dedc31 | 2603 | /*use_global_delete=*/0); |
f1dedc31 MM |
2604 | finish_cleanup (e, try_block); |
2605 | } | |
2606 | ||
303b7406 NS |
2607 | /* The value of the array initialization is the array itself, RVAL |
2608 | is a pointer to the first element. */ | |
325c3691 | 2609 | finish_stmt_expr_expr (rval, stmt_expr); |
f1dedc31 | 2610 | |
2a3398e1 | 2611 | stmt_expr = finish_init_stmts (is_global, stmt_expr, compound_stmt); |
303b7406 | 2612 | |
04c06002 | 2613 | /* Now convert make the result have the correct type. */ |
303b7406 NS |
2614 | atype = build_pointer_type (atype); |
2615 | stmt_expr = build1 (NOP_EXPR, atype, stmt_expr); | |
2616 | stmt_expr = build_indirect_ref (stmt_expr, NULL); | |
2617 | ||
ae499cce | 2618 | current_stmt_tree ()->stmts_are_full_exprs_p = destroy_temps; |
f1dedc31 | 2619 | return stmt_expr; |
8d08fdba MS |
2620 | } |
2621 | ||
2622 | /* Free up storage of type TYPE, at address ADDR. | |
2623 | ||
2624 | TYPE is a POINTER_TYPE and can be ptr_type_node for no special type | |
2625 | of pointer. | |
2626 | ||
2627 | VIRTUAL_SIZE is the amount of storage that was allocated, and is | |
2628 | used as the second argument to operator delete. It can include | |
2629 | things like padding and magic size cookies. It has virtual in it, | |
2630 | because if you have a base pointer and you delete through a virtual | |
2631 | destructor, it should be the size of the dynamic object, not the | |
cab1f180 | 2632 | static object, see Free Store 12.5 ISO C++. |
8d08fdba MS |
2633 | |
2634 | This does not call any destructors. */ | |
e92cc029 | 2635 | |
8d08fdba | 2636 | tree |
362efdc1 | 2637 | build_x_delete (tree addr, int which_delete, tree virtual_size) |
8d08fdba | 2638 | { |
a28e3c7f MS |
2639 | int use_global_delete = which_delete & 1; |
2640 | int use_vec_delete = !!(which_delete & 2); | |
a28e3c7f | 2641 | enum tree_code code = use_vec_delete ? VEC_DELETE_EXPR : DELETE_EXPR; |
8d08fdba | 2642 | |
5bd61841 MM |
2643 | return build_op_delete_call (code, addr, virtual_size, use_global_delete, |
2644 | NULL_TREE); | |
8d08fdba MS |
2645 | } |
2646 | ||
86f45d2c MM |
2647 | /* Call the DTOR_KIND destructor for EXP. FLAGS are as for |
2648 | build_delete. */ | |
298d6f60 MM |
2649 | |
2650 | static tree | |
362efdc1 | 2651 | build_dtor_call (tree exp, special_function_kind dtor_kind, int flags) |
298d6f60 | 2652 | { |
86f45d2c | 2653 | tree name; |
ee76b931 | 2654 | tree fn; |
86f45d2c MM |
2655 | switch (dtor_kind) |
2656 | { | |
2657 | case sfk_complete_destructor: | |
2658 | name = complete_dtor_identifier; | |
2659 | break; | |
2660 | ||
2661 | case sfk_base_destructor: | |
2662 | name = base_dtor_identifier; | |
2663 | break; | |
2664 | ||
2665 | case sfk_deleting_destructor: | |
2666 | name = deleting_dtor_identifier; | |
2667 | break; | |
2668 | ||
2669 | default: | |
8dc2b103 | 2670 | gcc_unreachable (); |
86f45d2c | 2671 | } |
ee76b931 MM |
2672 | |
2673 | exp = convert_from_reference (exp); | |
2674 | fn = lookup_fnfields (TREE_TYPE (exp), name, /*protect=*/2); | |
2675 | return build_new_method_call (exp, fn, | |
2676 | /*args=*/NULL_TREE, | |
2677 | /*conversion_path=*/NULL_TREE, | |
2678 | flags); | |
298d6f60 MM |
2679 | } |
2680 | ||
8d08fdba MS |
2681 | /* Generate a call to a destructor. TYPE is the type to cast ADDR to. |
2682 | ADDR is an expression which yields the store to be destroyed. | |
86f45d2c MM |
2683 | AUTO_DELETE is the name of the destructor to call, i.e., either |
2684 | sfk_complete_destructor, sfk_base_destructor, or | |
2685 | sfk_deleting_destructor. | |
8d08fdba MS |
2686 | |
2687 | FLAGS is the logical disjunction of zero or more LOOKUP_ | |
ade3dc07 | 2688 | flags. See cp-tree.h for more info. */ |
e92cc029 | 2689 | |
8d08fdba | 2690 | tree |
362efdc1 NN |
2691 | build_delete (tree type, tree addr, special_function_kind auto_delete, |
2692 | int flags, int use_global_delete) | |
8d08fdba | 2693 | { |
8d08fdba | 2694 | tree expr; |
8d08fdba MS |
2695 | |
2696 | if (addr == error_mark_node) | |
2697 | return error_mark_node; | |
2698 | ||
2699 | /* Can happen when CURRENT_EXCEPTION_OBJECT gets its type | |
2700 | set to `error_mark_node' before it gets properly cleaned up. */ | |
2701 | if (type == error_mark_node) | |
2702 | return error_mark_node; | |
2703 | ||
2704 | type = TYPE_MAIN_VARIANT (type); | |
2705 | ||
2706 | if (TREE_CODE (type) == POINTER_TYPE) | |
2707 | { | |
b1e5b86c GB |
2708 | bool complete_p = true; |
2709 | ||
2986ae00 | 2710 | type = TYPE_MAIN_VARIANT (TREE_TYPE (type)); |
8d08fdba MS |
2711 | if (TREE_CODE (type) == ARRAY_TYPE) |
2712 | goto handle_array; | |
23b4deba | 2713 | |
b1e5b86c GB |
2714 | /* We don't want to warn about delete of void*, only other |
2715 | incomplete types. Deleting other incomplete types | |
2716 | invokes undefined behavior, but it is not ill-formed, so | |
2717 | compile to something that would even do The Right Thing | |
2718 | (TM) should the type have a trivial dtor and no delete | |
2719 | operator. */ | |
2720 | if (!VOID_TYPE_P (type)) | |
8d08fdba | 2721 | { |
b1e5b86c GB |
2722 | complete_type (type); |
2723 | if (!COMPLETE_TYPE_P (type)) | |
2724 | { | |
2725 | warning ("possible problem detected in invocation of " | |
2726 | "delete operator:"); | |
2727 | cxx_incomplete_type_diagnostic (addr, type, 1); | |
061cae1f NS |
2728 | inform ("neither the destructor nor the class-specific " |
2729 | "operator delete will be called, even if they are " | |
b1e5b86c GB |
2730 | "declared when the class is defined."); |
2731 | complete_p = false; | |
2732 | } | |
8d08fdba | 2733 | } |
b1e5b86c GB |
2734 | if (VOID_TYPE_P (type) || !complete_p || !IS_AGGR_TYPE (type)) |
2735 | /* Call the builtin operator delete. */ | |
2736 | return build_builtin_delete_call (addr); | |
8d08fdba MS |
2737 | if (TREE_SIDE_EFFECTS (addr)) |
2738 | addr = save_expr (addr); | |
2986ae00 | 2739 | |
f4f206f4 | 2740 | /* Throw away const and volatile on target type of addr. */ |
6060a796 | 2741 | addr = convert_force (build_pointer_type (type), addr, 0); |
8d08fdba MS |
2742 | } |
2743 | else if (TREE_CODE (type) == ARRAY_TYPE) | |
2744 | { | |
2745 | handle_array: | |
6742d92b | 2746 | |
c407792d RK |
2747 | if (TYPE_DOMAIN (type) == NULL_TREE) |
2748 | { | |
8251199e | 2749 | error ("unknown array size in delete"); |
c407792d RK |
2750 | return error_mark_node; |
2751 | } | |
8d08fdba | 2752 | return build_vec_delete (addr, array_type_nelts (type), |
c7edeea3 | 2753 | auto_delete, use_global_delete); |
8d08fdba MS |
2754 | } |
2755 | else | |
2756 | { | |
2757 | /* Don't check PROTECT here; leave that decision to the | |
2758 | destructor. If the destructor is accessible, call it, | |
2759 | else report error. */ | |
2760 | addr = build_unary_op (ADDR_EXPR, addr, 0); | |
2761 | if (TREE_SIDE_EFFECTS (addr)) | |
2762 | addr = save_expr (addr); | |
2763 | ||
60696c53 | 2764 | addr = convert_force (build_pointer_type (type), addr, 0); |
8d08fdba MS |
2765 | } |
2766 | ||
50bc768d | 2767 | gcc_assert (IS_AGGR_TYPE (type)); |
8d08fdba | 2768 | |
834c6dff | 2769 | if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type)) |
8d08fdba | 2770 | { |
60696c53 | 2771 | if (auto_delete != sfk_deleting_destructor) |
8d08fdba MS |
2772 | return void_zero_node; |
2773 | ||
da4768fe | 2774 | return build_op_delete_call |
5bd61841 | 2775 | (DELETE_EXPR, addr, cxx_sizeof_nowarn (type), use_global_delete, |
519ebd1e | 2776 | NULL_TREE); |
8d08fdba | 2777 | } |
ade3dc07 | 2778 | else |
8d08fdba | 2779 | { |
700f8a87 | 2780 | tree do_delete = NULL_TREE; |
bd6dd845 | 2781 | tree ifexp; |
700f8a87 | 2782 | |
50bc768d | 2783 | gcc_assert (TYPE_HAS_DESTRUCTOR (type)); |
ade3dc07 | 2784 | |
52682a1b MM |
2785 | /* For `::delete x', we must not use the deleting destructor |
2786 | since then we would not be sure to get the global `operator | |
2787 | delete'. */ | |
86f45d2c | 2788 | if (use_global_delete && auto_delete == sfk_deleting_destructor) |
700f8a87 | 2789 | { |
1b4a93f7 MM |
2790 | /* We will use ADDR multiple times so we must save it. */ |
2791 | addr = save_expr (addr); | |
c6002625 | 2792 | /* Delete the object. */ |
86f45d2c MM |
2793 | do_delete = build_builtin_delete_call (addr); |
2794 | /* Otherwise, treat this like a complete object destructor | |
2795 | call. */ | |
2796 | auto_delete = sfk_complete_destructor; | |
700f8a87 | 2797 | } |
52682a1b MM |
2798 | /* If the destructor is non-virtual, there is no deleting |
2799 | variant. Instead, we must explicitly call the appropriate | |
2800 | `operator delete' here. */ | |
2801 | else if (!DECL_VIRTUAL_P (CLASSTYPE_DESTRUCTORS (type)) | |
2802 | && auto_delete == sfk_deleting_destructor) | |
2803 | { | |
1b4a93f7 MM |
2804 | /* We will use ADDR multiple times so we must save it. */ |
2805 | addr = save_expr (addr); | |
2806 | /* Build the call. */ | |
52682a1b MM |
2807 | do_delete = build_op_delete_call (DELETE_EXPR, |
2808 | addr, | |
ea793912 | 2809 | cxx_sizeof_nowarn (type), |
5bd61841 | 2810 | /*global_p=*/false, |
52682a1b MM |
2811 | NULL_TREE); |
2812 | /* Call the complete object destructor. */ | |
2813 | auto_delete = sfk_complete_destructor; | |
2814 | } | |
e3fe84e5 JM |
2815 | else if (auto_delete == sfk_deleting_destructor |
2816 | && TYPE_GETS_REG_DELETE (type)) | |
2817 | { | |
2818 | /* Make sure we have access to the member op delete, even though | |
2819 | we'll actually be calling it from the destructor. */ | |
ea793912 | 2820 | build_op_delete_call (DELETE_EXPR, addr, cxx_sizeof_nowarn (type), |
5bd61841 | 2821 | /*global_p=*/false, NULL_TREE); |
e3fe84e5 | 2822 | } |
8d08fdba | 2823 | |
3e411c3f | 2824 | expr = build_dtor_call (build_indirect_ref (addr, NULL), |
1b4a93f7 | 2825 | auto_delete, flags); |
bd6dd845 | 2826 | if (do_delete) |
f293ce4b | 2827 | expr = build2 (COMPOUND_EXPR, void_type_node, expr, do_delete); |
9e9ff709 | 2828 | |
bd6dd845 MS |
2829 | if (flags & LOOKUP_DESTRUCTOR) |
2830 | /* Explicit destructor call; don't check for null pointer. */ | |
2831 | ifexp = integer_one_node; | |
8d08fdba | 2832 | else |
bd6dd845 | 2833 | /* Handle deleting a null pointer. */ |
ab76ca54 | 2834 | ifexp = fold (cp_build_binary_op (NE_EXPR, addr, integer_zero_node)); |
8d08fdba | 2835 | |
bd6dd845 | 2836 | if (ifexp != integer_one_node) |
f293ce4b RS |
2837 | expr = build3 (COND_EXPR, void_type_node, |
2838 | ifexp, expr, void_zero_node); | |
8d08fdba | 2839 | |
8d08fdba MS |
2840 | return expr; |
2841 | } | |
ade3dc07 | 2842 | } |
8d08fdba | 2843 | |
ade3dc07 JM |
2844 | /* At the beginning of a destructor, push cleanups that will call the |
2845 | destructors for our base classes and members. | |
2a2480e1 | 2846 | |
a29e1034 | 2847 | Called from begin_destructor_body. */ |
8d08fdba | 2848 | |
ade3dc07 | 2849 | void |
edaf3e03 | 2850 | push_base_cleanups (void) |
ade3dc07 | 2851 | { |
fa743e8c NS |
2852 | tree binfo, base_binfo; |
2853 | int i; | |
ade3dc07 JM |
2854 | tree member; |
2855 | tree expr; | |
9ba5ff0f | 2856 | VEC (tree) *vbases; |
8d08fdba | 2857 | |
ade3dc07 | 2858 | /* Run destructors for all virtual baseclasses. */ |
5775a06a | 2859 | if (CLASSTYPE_VBASECLASSES (current_class_type)) |
ade3dc07 | 2860 | { |
ade3dc07 | 2861 | tree cond = (condition_conversion |
f293ce4b RS |
2862 | (build2 (BIT_AND_EXPR, integer_type_node, |
2863 | current_in_charge_parm, | |
2864 | integer_two_node))); | |
8d08fdba | 2865 | |
58c42dc2 | 2866 | /* The CLASSTYPE_VBASECLASSES vector is in initialization |
ade3dc07 | 2867 | order, which is also the right order for pushing cleanups. */ |
9ba5ff0f NS |
2868 | for (vbases = CLASSTYPE_VBASECLASSES (current_class_type), i = 0; |
2869 | VEC_iterate (tree, vbases, i, base_binfo); i++) | |
8d08fdba | 2870 | { |
9ba5ff0f | 2871 | if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (BINFO_TYPE (base_binfo))) |
8d08fdba | 2872 | { |
4ba126e4 MM |
2873 | expr = build_special_member_call (current_class_ref, |
2874 | base_dtor_identifier, | |
2875 | NULL_TREE, | |
9ba5ff0f | 2876 | base_binfo, |
4ba126e4 MM |
2877 | (LOOKUP_NORMAL |
2878 | | LOOKUP_NONVIRTUAL)); | |
f293ce4b RS |
2879 | expr = build3 (COND_EXPR, void_type_node, cond, |
2880 | expr, void_zero_node); | |
ade3dc07 | 2881 | finish_decl_cleanup (NULL_TREE, expr); |
8d08fdba MS |
2882 | } |
2883 | } | |
ade3dc07 JM |
2884 | } |
2885 | ||
ade3dc07 | 2886 | /* Take care of the remaining baseclasses. */ |
fa743e8c NS |
2887 | for (binfo = TYPE_BINFO (current_class_type), i = 0; |
2888 | BINFO_BASE_ITERATE (binfo, i, base_binfo); i++) | |
ade3dc07 | 2889 | { |
ade3dc07 | 2890 | if (TYPE_HAS_TRIVIAL_DESTRUCTOR (BINFO_TYPE (base_binfo)) |
809e3e7f | 2891 | || BINFO_VIRTUAL_P (base_binfo)) |
ade3dc07 JM |
2892 | continue; |
2893 | ||
4ba126e4 MM |
2894 | expr = build_special_member_call (current_class_ref, |
2895 | base_dtor_identifier, | |
2896 | NULL_TREE, base_binfo, | |
2897 | LOOKUP_NORMAL | LOOKUP_NONVIRTUAL); | |
ade3dc07 JM |
2898 | finish_decl_cleanup (NULL_TREE, expr); |
2899 | } | |
2900 | ||
2901 | for (member = TYPE_FIELDS (current_class_type); member; | |
2902 | member = TREE_CHAIN (member)) | |
2903 | { | |
17bbb839 | 2904 | if (TREE_CODE (member) != FIELD_DECL || DECL_ARTIFICIAL (member)) |
ade3dc07 JM |
2905 | continue; |
2906 | if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (member))) | |
2907 | { | |
50ad9642 MM |
2908 | tree this_member = (build_class_member_access_expr |
2909 | (current_class_ref, member, | |
2910 | /*access_path=*/NULL_TREE, | |
2911 | /*preserve_reference=*/false)); | |
ade3dc07 JM |
2912 | tree this_type = TREE_TYPE (member); |
2913 | expr = build_delete (this_type, this_member, | |
2914 | sfk_complete_destructor, | |
2915 | LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR|LOOKUP_NORMAL, | |
2916 | 0); | |
2917 | finish_decl_cleanup (NULL_TREE, expr); | |
2918 | } | |
8d08fdba MS |
2919 | } |
2920 | } | |
2921 | ||
2922 | /* For type TYPE, delete the virtual baseclass objects of DECL. */ | |
2923 | ||
2924 | tree | |
362efdc1 | 2925 | build_vbase_delete (tree type, tree decl) |
8d08fdba | 2926 | { |
58c42dc2 NS |
2927 | unsigned ix; |
2928 | tree binfo; | |
c7b62f14 | 2929 | tree result; |
9ba5ff0f | 2930 | VEC (tree) *vbases; |
8d08fdba MS |
2931 | tree addr = build_unary_op (ADDR_EXPR, decl, 0); |
2932 | ||
50bc768d | 2933 | gcc_assert (addr != error_mark_node); |
8d08fdba | 2934 | |
58c42dc2 | 2935 | result = convert_to_void (integer_zero_node, NULL); |
9ba5ff0f NS |
2936 | for (vbases = CLASSTYPE_VBASECLASSES (type), ix = 0; |
2937 | VEC_iterate (tree, vbases, ix, binfo); ix++) | |
8d08fdba | 2938 | { |
c7b62f14 | 2939 | tree base_addr = convert_force |
58c42dc2 | 2940 | (build_pointer_type (BINFO_TYPE (binfo)), addr, 0); |
c7b62f14 NS |
2941 | tree base_delete = build_delete |
2942 | (TREE_TYPE (base_addr), base_addr, sfk_base_destructor, | |
2943 | LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 0); | |
2944 | ||
2945 | result = build_compound_expr (result, base_delete); | |
8d08fdba | 2946 | } |
c7b62f14 | 2947 | return result; |
8d08fdba MS |
2948 | } |
2949 | ||
2950 | /* Build a C++ vector delete expression. | |
2951 | MAXINDEX is the number of elements to be deleted. | |
2952 | ELT_SIZE is the nominal size of each element in the vector. | |
2953 | BASE is the expression that should yield the store to be deleted. | |
8d08fdba MS |
2954 | This function expands (or synthesizes) these calls itself. |
2955 | AUTO_DELETE_VEC says whether the container (vector) should be deallocated. | |
8d08fdba MS |
2956 | |
2957 | This also calls delete for virtual baseclasses of elements of the vector. | |
2958 | ||
2959 | Update: MAXINDEX is no longer needed. The size can be extracted from the | |
2960 | start of the vector for pointers, and from the type for arrays. We still | |
2961 | use MAXINDEX for arrays because it happens to already have one of the | |
2962 | values we'd have to extract. (We could use MAXINDEX with pointers to | |
2963 | confirm the size, and trap if the numbers differ; not clear that it'd | |
2964 | be worth bothering.) */ | |
e92cc029 | 2965 | |
8d08fdba | 2966 | tree |
362efdc1 NN |
2967 | build_vec_delete (tree base, tree maxindex, |
2968 | special_function_kind auto_delete_vec, int use_global_delete) | |
8d08fdba | 2969 | { |
f30432d7 | 2970 | tree type; |
49b7aacb JM |
2971 | tree rval; |
2972 | tree base_init = NULL_TREE; | |
8d08fdba | 2973 | |
f30432d7 | 2974 | type = TREE_TYPE (base); |
c407792d | 2975 | |
f30432d7 | 2976 | if (TREE_CODE (type) == POINTER_TYPE) |
8d08fdba MS |
2977 | { |
2978 | /* Step back one from start of vector, and read dimension. */ | |
834c6dff MM |
2979 | tree cookie_addr; |
2980 | ||
6742d92b | 2981 | if (TREE_SIDE_EFFECTS (base)) |
49b7aacb JM |
2982 | { |
2983 | base_init = get_target_expr (base); | |
2984 | base = TARGET_EXPR_SLOT (base_init); | |
2985 | } | |
708cae97 | 2986 | type = strip_array_types (TREE_TYPE (type)); |
f293ce4b RS |
2987 | cookie_addr = build2 (MINUS_EXPR, |
2988 | build_pointer_type (sizetype), | |
2989 | base, | |
2990 | TYPE_SIZE_UNIT (sizetype)); | |
3e411c3f | 2991 | maxindex = build_indirect_ref (cookie_addr, NULL); |
8d08fdba | 2992 | } |
f30432d7 | 2993 | else if (TREE_CODE (type) == ARRAY_TYPE) |
8d08fdba | 2994 | { |
f4f206f4 KH |
2995 | /* Get the total number of things in the array, maxindex is a |
2996 | bad name. */ | |
f30432d7 | 2997 | maxindex = array_type_nelts_total (type); |
834c6dff | 2998 | type = strip_array_types (type); |
8d08fdba | 2999 | base = build_unary_op (ADDR_EXPR, base, 1); |
6742d92b | 3000 | if (TREE_SIDE_EFFECTS (base)) |
49b7aacb JM |
3001 | { |
3002 | base_init = get_target_expr (base); | |
3003 | base = TARGET_EXPR_SLOT (base_init); | |
3004 | } | |
8d08fdba MS |
3005 | } |
3006 | else | |
3007 | { | |
9e9ff709 | 3008 | if (base != error_mark_node) |
8251199e | 3009 | error ("type to vector delete is neither pointer or array type"); |
8d08fdba MS |
3010 | return error_mark_node; |
3011 | } | |
8d08fdba | 3012 | |
49b7aacb | 3013 | rval = build_vec_delete_1 (base, maxindex, type, auto_delete_vec, |
f30432d7 | 3014 | use_global_delete); |
49b7aacb | 3015 | if (base_init) |
f293ce4b | 3016 | rval = build2 (COMPOUND_EXPR, TREE_TYPE (rval), base_init, rval); |
49b7aacb JM |
3017 | |
3018 | return rval; | |
8d08fdba | 3019 | } |