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