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