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