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