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