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