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