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