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