* cp-tree.h (make_temp_vec): Remove.
[official-gcc.git] / gcc / cp / init.c
blobd1af4d786559d4bf3d0a05e5f6e6fb2edcbf9eed
1 /* Handle initialization things in C++.
2 Copyright (C) 1987, 89, 92-98, 1999 Free Software Foundation, Inc.
3 Contributed by Michael Tiemann (tiemann@cygnus.com)
5 This file is part of GNU CC.
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 /* High-level class interface. */
24 #include "config.h"
25 #include "system.h"
26 #include "tree.h"
27 #include "rtl.h"
28 #include "cp-tree.h"
29 #include "flags.h"
30 #include "output.h"
31 #include "except.h"
32 #include "expr.h"
33 #include "toplev.h"
34 #include "ggc.h"
36 static void expand_aggr_vbase_init_1 PROTO((tree, tree, tree, tree));
37 static void construct_virtual_bases PROTO((tree, tree, tree, tree, tree));
38 static void expand_aggr_init_1 PROTO((tree, tree, tree, tree, int));
39 static void expand_default_init PROTO((tree, tree, tree, tree, int));
40 static tree build_vec_delete_1 PROTO((tree, tree, tree, tree, tree,
41 int));
42 static void perform_member_init PROTO((tree, tree, tree, int));
43 static void sort_base_init PROTO((tree, tree *, tree *));
44 static tree build_builtin_delete_call PROTO((tree));
45 static int member_init_ok_or_else PROTO((tree, tree, const char *));
46 static void expand_virtual_init PROTO((tree, tree));
47 static tree sort_member_init PROTO((tree));
48 static tree initializing_context PROTO((tree));
49 static tree build_java_class_ref PROTO((tree));
50 static void expand_cleanup_for_base PROTO((tree, tree));
51 static tree get_temp_regvar PROTO((tree, tree));
53 /* Set up local variable for this file. MUST BE CALLED AFTER
54 INIT_DECL_PROCESSING. */
56 static tree BI_header_type, BI_header_size;
58 void init_init_processing ()
60 tree fields[1];
62 minus_one_node = build_int_2 (-1, -1);
64 /* Define the structure that holds header information for
65 arrays allocated via operator new. */
66 BI_header_type = make_lang_type (RECORD_TYPE);
67 nelts_identifier = get_identifier ("nelts");
68 fields[0] = build_lang_decl (FIELD_DECL, nelts_identifier, sizetype);
69 finish_builtin_type (BI_header_type, "__new_cookie", fields,
70 0, double_type_node);
71 BI_header_size = size_in_bytes (BI_header_type);
73 ggc_add_tree_root (&BI_header_type, 1);
74 ggc_add_tree_root (&BI_header_size, 1);
77 /* Subroutine of emit_base_init. For BINFO, initialize all the
78 virtual function table pointers, except those that come from
79 virtual base classes. Initialize binfo's vtable pointer, if
80 INIT_SELF is true. CAN_ELIDE is true when we know that all virtual
81 function table pointers in all bases have been initialized already,
82 probably because their constructors have just be run. ADDR is the
83 pointer to the object whos vtables we are going to initialize.
85 REAL_BINFO is usually the same as BINFO, except when addr is not of
86 pointer to the type of the real derived type that we want to
87 initialize for. This is the case when addr is a pointer to a sub
88 object of a complete object, and we only want to do part of the
89 complete object's initialization of vtable pointers. This is done
90 for all virtual table pointers in virtual base classes. REAL_BINFO
91 is used to find the BINFO_VTABLE that we initialize with. BINFO is
92 used for conversions of addr to subobjects.
94 BINFO_TYPE (real_binfo) must be BINFO_TYPE (binfo).
96 Relies upon binfo being inside TYPE_BINFO (TREE_TYPE (TREE_TYPE
97 (addr))). */
99 void
100 expand_direct_vtbls_init (real_binfo, binfo, init_self, can_elide, addr)
101 tree real_binfo, binfo, addr;
102 int init_self, can_elide;
104 tree real_binfos = BINFO_BASETYPES (real_binfo);
105 tree binfos = BINFO_BASETYPES (binfo);
106 int i, n_baselinks = real_binfos ? TREE_VEC_LENGTH (real_binfos) : 0;
108 for (i = 0; i < n_baselinks; i++)
110 tree real_base_binfo = TREE_VEC_ELT (real_binfos, i);
111 tree base_binfo = TREE_VEC_ELT (binfos, i);
112 int is_not_base_vtable
113 = i != CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (real_binfo));
114 if (! TREE_VIA_VIRTUAL (real_base_binfo))
115 expand_direct_vtbls_init (real_base_binfo, base_binfo,
116 is_not_base_vtable, can_elide, addr);
118 #if 0
119 /* Before turning this on, make sure it is correct. */
120 if (can_elide && ! BINFO_MODIFIED (binfo))
121 return;
122 #endif
123 /* Should we use something besides CLASSTYPE_VFIELDS? */
124 if (init_self && CLASSTYPE_VFIELDS (BINFO_TYPE (real_binfo)))
126 tree base_ptr = convert_pointer_to_real (binfo, addr);
127 expand_virtual_init (real_binfo, base_ptr);
131 /* 348 - 351 */
132 /* Subroutine of emit_base_init. */
134 static void
135 perform_member_init (member, name, init, explicit)
136 tree member, name, init;
137 int explicit;
139 tree decl;
140 tree type = TREE_TYPE (member);
142 decl = build_component_ref (current_class_ref, name, NULL_TREE, explicit);
144 /* Deal with this here, as we will get confused if we try to call the
145 assignment op for an anonymous union. This can happen in a
146 synthesized copy constructor. */
147 if (ANON_AGGR_TYPE_P (type))
149 init = build (INIT_EXPR, type, decl, TREE_VALUE (init));
150 finish_expr_stmt (init);
152 else if (TYPE_NEEDS_CONSTRUCTING (type)
153 || (init && TYPE_HAS_CONSTRUCTOR (type)))
155 /* Since `init' is already a TREE_LIST on the current_member_init_list,
156 only build it into one if we aren't already a list. */
157 if (init != NULL_TREE && TREE_CODE (init) != TREE_LIST)
158 init = build_expr_list (NULL_TREE, init);
160 if (explicit
161 && TREE_CODE (type) == ARRAY_TYPE
162 && init != NULL_TREE
163 && TREE_CHAIN (init) == NULL_TREE
164 && TREE_CODE (TREE_TYPE (TREE_VALUE (init))) == ARRAY_TYPE)
166 /* Initialization of one array from another. */
167 finish_expr_stmt
168 (build_vec_init (TREE_OPERAND (decl, 1), decl,
169 array_type_nelts (type), TREE_VALUE (init), 1));
171 else
172 finish_expr_stmt (build_aggr_init (decl, init, 0));
174 else
176 if (init == NULL_TREE)
178 if (explicit)
180 /* default-initialization. */
181 if (AGGREGATE_TYPE_P (type))
183 /* This is a default initialization of an aggregate,
184 but not one of non-POD class type. We cleverly
185 notice that the initialization rules in such a
186 case are the same as for initialization with an
187 empty brace-initialization list. We don't want
188 to call build_modify_expr as that will go looking
189 for constructors and such. */
190 tree e = build (CONSTRUCTOR, type, NULL_TREE, NULL_TREE);
191 TREE_SIDE_EFFECTS (e) = 1;
192 finish_expr_stmt (build (INIT_EXPR, type, decl, e));
194 else if (TREE_CODE (type) == REFERENCE_TYPE)
195 cp_error ("default-initialization of `%#D', which has reference type",
196 member);
197 else
198 init = integer_zero_node;
200 /* member traversal: note it leaves init NULL */
201 else if (TREE_CODE (TREE_TYPE (member)) == REFERENCE_TYPE)
202 cp_pedwarn ("uninitialized reference member `%D'", member);
204 else if (TREE_CODE (init) == TREE_LIST)
206 /* There was an explicit member initialization. Do some
207 work in that case. */
208 if (TREE_CHAIN (init))
210 warning ("initializer list treated as compound expression");
211 init = build_compound_expr (init);
213 else
214 init = TREE_VALUE (init);
217 if (init)
218 finish_expr_stmt (build_modify_expr (decl, INIT_EXPR, init));
221 if (TYPE_NEEDS_DESTRUCTOR (type))
223 tree expr;
225 expr = build_component_ref (current_class_ref, name, NULL_TREE,
226 explicit);
227 expr = build_delete (type, expr, integer_zero_node,
228 LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR, 0);
230 if (expr != error_mark_node)
231 finish_subobject (expr);
235 extern int warn_reorder;
237 /* Subroutine of emit_member_init. */
239 static tree
240 sort_member_init (t)
241 tree t;
243 tree x, member, name, field;
244 tree init_list = NULL_TREE;
245 int last_pos = 0;
246 tree last_field = NULL_TREE;
248 for (member = TYPE_FIELDS (t); member ; member = TREE_CHAIN (member))
250 int pos;
252 /* member could be, for example, a CONST_DECL for an enumerated
253 tag; we don't want to try to initialize that, since it already
254 has a value. */
255 if (TREE_CODE (member) != FIELD_DECL || !DECL_NAME (member))
256 continue;
258 for (x = current_member_init_list, pos = 0; x; x = TREE_CHAIN (x), ++pos)
260 /* If we cleared this out, then pay no attention to it. */
261 if (TREE_PURPOSE (x) == NULL_TREE)
262 continue;
263 name = TREE_PURPOSE (x);
265 if (TREE_CODE (name) == IDENTIFIER_NODE)
266 field = IDENTIFIER_CLASS_VALUE (name);
267 else
269 my_friendly_assert (TREE_CODE (name) == FIELD_DECL, 348);
270 field = name;
273 /* If one member shadows another, get the outermost one. */
274 if (TREE_CODE (field) == TREE_LIST)
275 field = TREE_VALUE (field);
277 if (field == member)
279 if (warn_reorder)
281 if (pos < last_pos)
283 cp_warning_at ("member initializers for `%#D'", last_field);
284 cp_warning_at (" and `%#D'", field);
285 warning (" will be re-ordered to match declaration order");
287 last_pos = pos;
288 last_field = field;
291 /* Make sure we won't try to work on this init again. */
292 TREE_PURPOSE (x) = NULL_TREE;
293 x = build_tree_list (name, TREE_VALUE (x));
294 goto got_it;
298 /* If we didn't find MEMBER in the list, create a dummy entry
299 so the two lists (INIT_LIST and the list of members) will be
300 symmetrical. */
301 x = build_tree_list (NULL_TREE, NULL_TREE);
302 got_it:
303 init_list = chainon (init_list, x);
306 /* Initializers for base members go at the end. */
307 for (x = current_member_init_list ; x ; x = TREE_CHAIN (x))
309 name = TREE_PURPOSE (x);
310 if (name)
312 if (purpose_member (name, init_list))
314 cp_error ("multiple initializations given for member `%D'",
315 IDENTIFIER_CLASS_VALUE (name));
316 continue;
319 init_list = chainon (init_list,
320 build_tree_list (name, TREE_VALUE (x)));
321 TREE_PURPOSE (x) = NULL_TREE;
325 return init_list;
328 static void
329 sort_base_init (t, rbase_ptr, vbase_ptr)
330 tree t, *rbase_ptr, *vbase_ptr;
332 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
333 int n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
335 int i;
336 tree x;
337 tree last;
339 /* For warn_reorder. */
340 int last_pos = 0;
341 tree last_base = NULL_TREE;
343 tree rbases = NULL_TREE;
344 tree vbases = NULL_TREE;
346 /* First walk through and splice out vbase and invalid initializers.
347 Also replace names with binfos. */
349 last = tree_cons (NULL_TREE, NULL_TREE, current_base_init_list);
350 for (x = TREE_CHAIN (last); x; x = TREE_CHAIN (x))
352 tree basetype = TREE_PURPOSE (x);
353 tree binfo = NULL_TREE;
355 if (basetype == NULL_TREE)
357 /* Initializer for single base class. Must not
358 use multiple inheritance or this is ambiguous. */
359 switch (n_baseclasses)
361 case 0:
362 cp_error ("`%T' does not have a base class to initialize",
363 current_class_type);
364 return;
365 case 1:
366 break;
367 default:
368 cp_error ("unnamed initializer ambiguous for `%T' which uses multiple inheritance",
369 current_class_type);
370 return;
372 binfo = TREE_VEC_ELT (binfos, 0);
374 else if (is_aggr_type (basetype, 1))
376 binfo = binfo_or_else (basetype, t);
377 if (binfo == NULL_TREE)
378 continue;
380 /* Virtual base classes are special cases. Their initializers
381 are recorded with this constructor, and they are used when
382 this constructor is the top-level constructor called. */
383 if (TREE_VIA_VIRTUAL (binfo))
385 tree v = CLASSTYPE_VBASECLASSES (t);
386 while (BINFO_TYPE (v) != BINFO_TYPE (binfo))
387 v = TREE_CHAIN (v);
389 vbases = tree_cons (v, TREE_VALUE (x), vbases);
390 continue;
392 else
394 /* Otherwise, if it is not an immediate base class, complain. */
395 for (i = n_baseclasses-1; i >= 0; i--)
396 if (BINFO_TYPE (binfo) == BINFO_TYPE (TREE_VEC_ELT (binfos, i)))
397 break;
398 if (i < 0)
400 cp_error ("`%T' is not an immediate base class of `%T'",
401 basetype, current_class_type);
402 continue;
406 else
407 my_friendly_abort (365);
409 TREE_PURPOSE (x) = binfo;
410 TREE_CHAIN (last) = x;
411 last = x;
413 TREE_CHAIN (last) = NULL_TREE;
415 /* Now walk through our regular bases and make sure they're initialized. */
417 for (i = 0; i < n_baseclasses; ++i)
419 tree base_binfo = TREE_VEC_ELT (binfos, i);
420 int pos;
422 if (TREE_VIA_VIRTUAL (base_binfo))
423 continue;
425 for (x = current_base_init_list, pos = 0; x; x = TREE_CHAIN (x), ++pos)
427 tree binfo = TREE_PURPOSE (x);
429 if (binfo == NULL_TREE)
430 continue;
432 if (binfo == base_binfo)
434 if (warn_reorder)
436 if (pos < last_pos)
438 cp_warning_at ("base initializers for `%#T'", last_base);
439 cp_warning_at (" and `%#T'", BINFO_TYPE (binfo));
440 warning (" will be re-ordered to match inheritance order");
442 last_pos = pos;
443 last_base = BINFO_TYPE (binfo);
446 /* Make sure we won't try to work on this init again. */
447 TREE_PURPOSE (x) = NULL_TREE;
448 x = build_tree_list (binfo, TREE_VALUE (x));
449 goto got_it;
453 /* If we didn't find BASE_BINFO in the list, create a dummy entry
454 so the two lists (RBASES and the list of bases) will be
455 symmetrical. */
456 x = build_tree_list (NULL_TREE, NULL_TREE);
457 got_it:
458 rbases = chainon (rbases, x);
461 *rbase_ptr = rbases;
462 *vbase_ptr = vbases;
465 /* Perform whatever initializations have yet to be done on the base
466 class of the class variable. These actions are in the global
467 variable CURRENT_BASE_INIT_LIST. Such an action could be
468 NULL_TREE, meaning that the user has explicitly called the base
469 class constructor with no arguments.
471 If there is a need for a call to a constructor, we must surround
472 that call with a pushlevel/poplevel pair, since we are technically
473 at the PARM level of scope.
475 Argument IMMEDIATELY, if zero, forces a new sequence to be
476 generated to contain these new insns, so it can be emitted later.
477 This sequence is saved in the global variable BASE_INIT_EXPR.
478 Otherwise, the insns are emitted into the current sequence.
480 Note that emit_base_init does *not* initialize virtual base
481 classes. That is done specially, elsewhere. */
483 tree
484 emit_base_init (t)
485 tree t;
487 tree member;
488 tree mem_init_list;
489 tree rbase_init_list, vbase_init_list;
490 tree t_binfo = TYPE_BINFO (t);
491 tree binfos = BINFO_BASETYPES (t_binfo);
492 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
493 tree stmt_expr;
494 tree compound_stmt;
496 mem_init_list = sort_member_init (t);
497 current_member_init_list = NULL_TREE;
499 sort_base_init (t, &rbase_init_list, &vbase_init_list);
500 current_base_init_list = NULL_TREE;
502 begin_init_stmts (&stmt_expr, &compound_stmt);
504 /* First, initialize the virtual base classes, if we are
505 constructing the most-derived object. */
506 if (TYPE_USES_VIRTUAL_BASECLASSES (t))
508 tree first_arg = TREE_CHAIN (DECL_ARGUMENTS (current_function_decl));
509 construct_virtual_bases (t, current_class_ref, current_class_ptr,
510 vbase_init_list, first_arg);
513 /* Now, perform initialization of non-virtual base classes. */
514 for (i = 0; i < n_baseclasses; i++)
516 tree base_binfo = TREE_VEC_ELT (binfos, i);
517 tree init = void_list_node;
519 if (TREE_VIA_VIRTUAL (base_binfo))
520 continue;
522 my_friendly_assert (BINFO_INHERITANCE_CHAIN (base_binfo) == t_binfo,
523 999);
525 if (TREE_PURPOSE (rbase_init_list))
526 init = TREE_VALUE (rbase_init_list);
527 else if (TYPE_NEEDS_CONSTRUCTING (BINFO_TYPE (base_binfo)))
529 init = NULL_TREE;
530 if (extra_warnings && copy_args_p (current_function_decl))
531 cp_warning ("base class `%#T' should be explicitly initialized in the copy constructor",
532 BINFO_TYPE (base_binfo));
535 if (init != void_list_node)
537 member = convert_pointer_to_real (base_binfo, current_class_ptr);
538 expand_aggr_init_1 (base_binfo, NULL_TREE,
539 build_indirect_ref (member, NULL_PTR), init,
540 LOOKUP_NORMAL);
543 expand_cleanup_for_base (base_binfo, NULL_TREE);
544 rbase_init_list = TREE_CHAIN (rbase_init_list);
547 /* Initialize all the virtual function table fields that
548 do come from virtual base classes. */
549 if (TYPE_USES_VIRTUAL_BASECLASSES (t))
550 expand_indirect_vtbls_init (t_binfo, current_class_ref, current_class_ptr);
552 /* Initialize all the virtual function table fields that
553 do not come from virtual base classes. */
554 expand_direct_vtbls_init (t_binfo, t_binfo, 1, 1, current_class_ptr);
556 for (member = TYPE_FIELDS (t); member; member = TREE_CHAIN (member))
558 tree init, name;
559 int from_init_list;
561 /* member could be, for example, a CONST_DECL for an enumerated
562 tag; we don't want to try to initialize that, since it already
563 has a value. */
564 if (TREE_CODE (member) != FIELD_DECL || !DECL_NAME (member))
565 continue;
567 /* See if we had a user-specified member initialization. */
568 if (TREE_PURPOSE (mem_init_list))
570 name = TREE_PURPOSE (mem_init_list);
571 init = TREE_VALUE (mem_init_list);
572 from_init_list = 1;
574 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE
575 || TREE_CODE (name) == FIELD_DECL, 349);
577 else
579 name = DECL_NAME (member);
580 init = DECL_INITIAL (member);
582 from_init_list = 0;
584 /* Effective C++ rule 12. */
585 if (warn_ecpp && init == NULL_TREE
586 && !DECL_ARTIFICIAL (member)
587 && TREE_CODE (TREE_TYPE (member)) != ARRAY_TYPE)
588 cp_warning ("`%D' should be initialized in the member initialization list", member);
591 perform_member_init (member, name, init, from_init_list);
592 mem_init_list = TREE_CHAIN (mem_init_list);
595 /* Now initialize any members from our bases. */
596 while (mem_init_list)
598 tree name, init, field;
600 if (TREE_PURPOSE (mem_init_list))
602 name = TREE_PURPOSE (mem_init_list);
603 init = TREE_VALUE (mem_init_list);
605 if (TREE_CODE (name) == IDENTIFIER_NODE)
606 field = IDENTIFIER_CLASS_VALUE (name);
607 else
608 field = name;
610 /* If one member shadows another, get the outermost one. */
611 if (TREE_CODE (field) == TREE_LIST)
613 field = TREE_VALUE (field);
614 if (decl_type_context (field) != current_class_type)
615 cp_error ("field `%D' not in immediate context", field);
618 perform_member_init (field, name, init, 1);
620 mem_init_list = TREE_CHAIN (mem_init_list);
623 /* All the implicit try blocks we built up will be zapped
624 when we come to a real binding contour boundary. */
625 return finish_init_stmts (stmt_expr, compound_stmt);
628 /* Check that all fields are properly initialized after
629 an assignment to `this'. Called only when such an assignment
630 is actually noted. */
632 void
633 check_base_init (t)
634 tree t;
636 tree member;
637 for (member = TYPE_FIELDS (t); member; member = TREE_CHAIN (member))
638 if (DECL_NAME (member) && TREE_USED (member))
639 cp_error ("field `%D' used before initialized (after assignment to `this')",
640 member);
643 /* This code sets up the virtual function tables appropriate for
644 the pointer DECL. It is a one-ply initialization.
646 BINFO is the exact type that DECL is supposed to be. In
647 multiple inheritance, this might mean "C's A" if C : A, B. */
649 static void
650 expand_virtual_init (binfo, decl)
651 tree binfo, decl;
653 tree type = BINFO_TYPE (binfo);
654 tree vtbl, vtbl_ptr;
655 tree vtype, vtype_binfo;
657 /* This code is crusty. Should be simple, like:
658 vtbl = BINFO_VTABLE (binfo);
660 vtype = DECL_CONTEXT (TYPE_VFIELD (type));
661 vtype_binfo = get_binfo (vtype, TREE_TYPE (TREE_TYPE (decl)), 0);
662 vtbl = BINFO_VTABLE (binfo_value (DECL_FIELD_CONTEXT (TYPE_VFIELD (type)), binfo));
663 assemble_external (vtbl);
664 TREE_USED (vtbl) = 1;
665 vtbl = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (vtbl)), vtbl);
666 decl = convert_pointer_to_real (vtype_binfo, decl);
667 vtbl_ptr = build_vfield_ref (build_indirect_ref (decl, NULL_PTR), vtype);
668 if (vtbl_ptr == error_mark_node)
669 return;
671 /* Have to convert VTBL since array sizes may be different. */
672 vtbl = convert_force (TREE_TYPE (vtbl_ptr), vtbl, 0);
673 finish_expr_stmt (build_modify_expr (vtbl_ptr, NOP_EXPR, vtbl));
676 /* If an exception is thrown in a constructor, those base classes already
677 constructed must be destroyed. This function creates the cleanup
678 for BINFO, which has just been constructed. If FLAG is non-NULL,
679 it is a DECL which is non-zero when this base needs to be
680 destroyed. */
682 static void
683 expand_cleanup_for_base (binfo, flag)
684 tree binfo;
685 tree flag;
687 tree expr;
689 if (!TYPE_NEEDS_DESTRUCTOR (BINFO_TYPE (binfo)))
690 return;
692 /* Call the destructor. */
693 expr = (build_scoped_method_call
694 (current_class_ref, binfo, dtor_identifier,
695 build_expr_list (NULL_TREE, integer_zero_node)));
696 if (flag)
697 expr = fold (build (COND_EXPR, void_type_node,
698 truthvalue_conversion (flag),
699 expr, integer_zero_node));
701 finish_subobject (expr);
704 /* Subroutine of `expand_aggr_vbase_init'.
705 BINFO is the binfo of the type that is being initialized.
706 INIT_LIST is the list of initializers for the virtual baseclass. */
708 static void
709 expand_aggr_vbase_init_1 (binfo, exp, addr, init_list)
710 tree binfo, exp, addr, init_list;
712 tree init = purpose_member (binfo, init_list);
713 tree ref = build_indirect_ref (addr, NULL_PTR);
715 if (init)
716 init = TREE_VALUE (init);
717 /* Call constructors, but don't set up vtables. */
718 expand_aggr_init_1 (binfo, exp, ref, init, LOOKUP_COMPLAIN);
721 /* Construct the virtual base-classes of THIS_REF (whose address is
722 THIS_PTR). The object has the indicated TYPE. The construction
723 actually takes place only if FLAG is non-zero. INIT_LIST is list
724 of initialization for constructor to perform. */
726 static void
727 construct_virtual_bases (type, this_ref, this_ptr, init_list, flag)
728 tree type;
729 tree this_ref;
730 tree this_ptr;
731 tree init_list;
732 tree flag;
734 tree vbases;
735 tree result;
736 tree if_stmt;
738 /* If there are no virtual baseclasses, we shouldn't even be here. */
739 my_friendly_assert (TYPE_USES_VIRTUAL_BASECLASSES (type), 19990621);
741 /* First set the pointers in our object that tell us where to find
742 our virtual baseclasses. */
743 if_stmt = begin_if_stmt ();
744 finish_if_stmt_cond (flag, if_stmt);
745 result = init_vbase_pointers (type, this_ptr);
746 if (result)
747 finish_expr_stmt (build_compound_expr (result));
748 finish_then_clause (if_stmt);
749 finish_if_stmt ();
751 /* Now, run through the baseclasses, initializing each. */
752 for (vbases = CLASSTYPE_VBASECLASSES (type); vbases;
753 vbases = TREE_CHAIN (vbases))
755 tree tmp = purpose_member (vbases, result);
756 tree inner_if_stmt;
757 tree compound_stmt;
759 /* If there are virtual base classes with destructors, we need to
760 emit cleanups to destroy them if an exception is thrown during
761 the construction process. These exception regions (i.e., the
762 period during which the cleanups must occur) begin from the time
763 the construction is complete to the end of the function. If we
764 create a conditional block in which to initialize the
765 base-classes, then the cleanup region for the virtual base begins
766 inside a block, and ends outside of that block. This situation
767 confuses the sjlj exception-handling code. Therefore, we do not
768 create a single conditional block, but one for each
769 initialization. (That way the cleanup regions always begin
770 in the outer block.) We trust the back-end to figure out
771 that the FLAG will not change across initializations, and
772 avoid doing multiple tests. */
773 inner_if_stmt = begin_if_stmt ();
774 finish_if_stmt_cond (flag, inner_if_stmt);
775 compound_stmt = begin_compound_stmt (/*has_no_scope=*/1);
776 expand_aggr_vbase_init_1 (vbases, this_ref,
777 TREE_OPERAND (TREE_VALUE (tmp), 0),
778 init_list);
779 finish_compound_stmt (/*has_no_scope=*/1, compound_stmt);
780 finish_then_clause (inner_if_stmt);
781 finish_if_stmt ();
783 expand_cleanup_for_base (vbases, flag);
787 /* Find the context in which this FIELD can be initialized. */
789 static tree
790 initializing_context (field)
791 tree field;
793 tree t = DECL_CONTEXT (field);
795 /* Anonymous union members can be initialized in the first enclosing
796 non-anonymous union context. */
797 while (t && ANON_AGGR_TYPE_P (t))
798 t = TYPE_CONTEXT (t);
799 return t;
802 /* Function to give error message if member initialization specification
803 is erroneous. FIELD is the member we decided to initialize.
804 TYPE is the type for which the initialization is being performed.
805 FIELD must be a member of TYPE.
807 MEMBER_NAME is the name of the member. */
809 static int
810 member_init_ok_or_else (field, type, member_name)
811 tree field;
812 tree type;
813 const char *member_name;
815 if (field == error_mark_node)
816 return 0;
817 if (field == NULL_TREE || initializing_context (field) != type)
819 cp_error ("class `%T' does not have any field named `%s'", type,
820 member_name);
821 return 0;
823 if (TREE_STATIC (field))
825 cp_error ("field `%#D' is static; only point of initialization is its declaration",
826 field);
827 return 0;
830 return 1;
833 /* If NAME is a viable field name for the aggregate DECL,
834 and PARMS is a viable parameter list, then expand an _EXPR
835 which describes this initialization.
837 Note that we do not need to chase through the class's base classes
838 to look for NAME, because if it's in that list, it will be handled
839 by the constructor for that base class.
841 We do not yet have a fixed-point finder to instantiate types
842 being fed to overloaded constructors. If there is a unique
843 constructor, then argument types can be got from that one.
845 If INIT is non-NULL, then it the initialization should
846 be placed in `current_base_init_list', where it will be processed
847 by `emit_base_init'. */
849 void
850 expand_member_init (exp, name, init)
851 tree exp, name, init;
853 tree basetype = NULL_TREE, field;
854 tree type;
856 if (exp == NULL_TREE)
857 return; /* complain about this later */
859 type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
861 if (name && TREE_CODE (name) == TYPE_DECL)
863 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (name));
864 name = DECL_NAME (name);
867 if (name == NULL_TREE && IS_AGGR_TYPE (type))
868 switch (CLASSTYPE_N_BASECLASSES (type))
870 case 0:
871 error ("base class initializer specified, but no base class to initialize");
872 return;
873 case 1:
874 basetype = TYPE_BINFO_BASETYPE (type, 0);
875 break;
876 default:
877 error ("initializer for unnamed base class ambiguous");
878 cp_error ("(type `%T' uses multiple inheritance)", type);
879 return;
882 my_friendly_assert (init != NULL_TREE, 0);
884 /* The grammar should not allow fields which have names that are
885 TYPENAMEs. Therefore, if the field has a non-NULL TREE_TYPE, we
886 may assume that this is an attempt to initialize a base class
887 member of the current type. Otherwise, it is an attempt to
888 initialize a member field. */
890 if (init == void_type_node)
891 init = NULL_TREE;
893 if (name == NULL_TREE || basetype)
895 tree base_init;
897 if (name == NULL_TREE)
899 #if 0
900 if (basetype)
901 name = TYPE_IDENTIFIER (basetype);
902 else
904 error ("no base class to initialize");
905 return;
907 #endif
909 else if (basetype != type
910 && ! current_template_parms
911 && ! vec_binfo_member (basetype,
912 TYPE_BINFO_BASETYPES (type))
913 && ! binfo_member (basetype, CLASSTYPE_VBASECLASSES (type)))
915 if (IDENTIFIER_CLASS_VALUE (name))
916 goto try_member;
917 if (TYPE_USES_VIRTUAL_BASECLASSES (type))
918 cp_error ("type `%T' is not an immediate or virtual basetype for `%T'",
919 basetype, type);
920 else
921 cp_error ("type `%T' is not an immediate basetype for `%T'",
922 basetype, type);
923 return;
926 if (purpose_member (basetype, current_base_init_list))
928 cp_error ("base class `%T' already initialized", basetype);
929 return;
932 if (warn_reorder && current_member_init_list)
934 cp_warning ("base initializer for `%T'", basetype);
935 warning (" will be re-ordered to precede member initializations");
938 base_init = build_tree_list (basetype, init);
939 current_base_init_list = chainon (current_base_init_list, base_init);
941 else
943 tree member_init;
945 try_member:
946 field = lookup_field (type, name, 1, 0);
948 if (! member_init_ok_or_else (field, type, IDENTIFIER_POINTER (name)))
949 return;
951 if (purpose_member (name, current_member_init_list))
953 cp_error ("field `%D' already initialized", field);
954 return;
957 member_init = build_tree_list (name, init);
958 current_member_init_list = chainon (current_member_init_list, member_init);
962 /* We are about to generate some complex initialization code.
963 Conceptually, it is all a single expression. However, we may want
964 to include conditionals, loops, and other such statement-level
965 constructs. Therefore, we build the initialization code inside a
966 statement-expression. This function starts such an expression.
967 STMT_EXPR_P and COMPOUND_STMT_P are filled in by this function;
968 pass them back to finish_init_stmts when the expression is
969 complete. */
971 void
972 begin_init_stmts (stmt_expr_p, compound_stmt_p)
973 tree *stmt_expr_p;
974 tree *compound_stmt_p;
976 *stmt_expr_p = begin_stmt_expr ();
977 *compound_stmt_p = begin_compound_stmt (/*has_no_scope=*/1);
980 /* Finish out the statement-expression begun by the previous call to
981 begin_init_stmts. Returns the statement-expression itself. */
983 tree
984 finish_init_stmts (stmt_expr, compound_stmt)
985 tree stmt_expr;
986 tree compound_stmt;
988 finish_compound_stmt (/*has_no_scope=*/1, compound_stmt);
989 stmt_expr = finish_stmt_expr (stmt_expr);
991 /* To avoid spurious warnings about unused values, we set
992 TREE_USED. */
993 if (stmt_expr)
994 TREE_USED (stmt_expr) = 1;
996 return stmt_expr;
999 /* This is like `expand_member_init', only it stores one aggregate
1000 value into another.
1002 INIT comes in two flavors: it is either a value which
1003 is to be stored in EXP, or it is a parameter list
1004 to go to a constructor, which will operate on EXP.
1005 If INIT is not a parameter list for a constructor, then set
1006 LOOKUP_ONLYCONVERTING.
1007 If FLAGS is LOOKUP_ONLYCONVERTING then it is the = init form of
1008 the initializer, if FLAGS is 0, then it is the (init) form.
1009 If `init' is a CONSTRUCTOR, then we emit a warning message,
1010 explaining that such initializations are invalid.
1012 ALIAS_THIS is nonzero iff we are initializing something which is
1013 essentially an alias for current_class_ref. In this case, the base
1014 constructor may move it on us, and we must keep track of such
1015 deviations.
1017 If INIT resolves to a CALL_EXPR which happens to return
1018 something of the type we are looking for, then we know
1019 that we can safely use that call to perform the
1020 initialization.
1022 The virtual function table pointer cannot be set up here, because
1023 we do not really know its type.
1025 Virtual baseclass pointers are also set up here.
1027 This never calls operator=().
1029 When initializing, nothing is CONST.
1031 A default copy constructor may have to be used to perform the
1032 initialization.
1034 A constructor or a conversion operator may have to be used to
1035 perform the initialization, but not both, as it would be ambiguous. */
1037 tree
1038 build_aggr_init (exp, init, flags)
1039 tree exp, init;
1040 int flags;
1042 tree stmt_expr;
1043 tree compound_stmt;
1044 int destroy_temps;
1045 tree type = TREE_TYPE (exp);
1046 int was_const = TREE_READONLY (exp);
1047 int was_volatile = TREE_THIS_VOLATILE (exp);
1049 if (init == error_mark_node)
1050 return error_mark_node;
1052 TREE_READONLY (exp) = 0;
1053 TREE_THIS_VOLATILE (exp) = 0;
1055 if (init && TREE_CODE (init) != TREE_LIST)
1056 flags |= LOOKUP_ONLYCONVERTING;
1058 if (TREE_CODE (type) == ARRAY_TYPE)
1060 /* Must arrange to initialize each element of EXP
1061 from elements of INIT. */
1062 tree itype = init ? TREE_TYPE (init) : NULL_TREE;
1063 if (CP_TYPE_QUALS (type) != TYPE_UNQUALIFIED)
1065 TREE_TYPE (exp) = TYPE_MAIN_VARIANT (type);
1066 if (init)
1067 TREE_TYPE (init) = TYPE_MAIN_VARIANT (itype);
1069 if (init && TREE_TYPE (init) == NULL_TREE)
1071 /* Handle bad initializers like:
1072 class COMPLEX {
1073 public:
1074 double re, im;
1075 COMPLEX(double r = 0.0, double i = 0.0) {re = r; im = i;};
1076 ~COMPLEX() {};
1079 int main(int argc, char **argv) {
1080 COMPLEX zees(1.0, 0.0)[10];
1083 error ("bad array initializer");
1084 return error_mark_node;
1086 stmt_expr = build_vec_init (exp, exp, array_type_nelts (type), init,
1087 init && same_type_p (TREE_TYPE (init),
1088 TREE_TYPE (exp)));
1089 TREE_READONLY (exp) = was_const;
1090 TREE_THIS_VOLATILE (exp) = was_volatile;
1091 TREE_TYPE (exp) = type;
1092 if (init)
1093 TREE_TYPE (init) = itype;
1094 return stmt_expr;
1097 if (TREE_CODE (exp) == VAR_DECL || TREE_CODE (exp) == PARM_DECL)
1098 /* just know that we've seen something for this node */
1099 TREE_USED (exp) = 1;
1101 TREE_TYPE (exp) = TYPE_MAIN_VARIANT (type);
1102 begin_init_stmts (&stmt_expr, &compound_stmt);
1103 destroy_temps = stmts_are_full_exprs_p;
1104 stmts_are_full_exprs_p = 0;
1105 expand_aggr_init_1 (TYPE_BINFO (type), exp, exp,
1106 init, LOOKUP_NORMAL|flags);
1107 stmt_expr = finish_init_stmts (stmt_expr, compound_stmt);
1108 stmts_are_full_exprs_p = destroy_temps;
1109 TREE_TYPE (exp) = type;
1110 TREE_READONLY (exp) = was_const;
1111 TREE_THIS_VOLATILE (exp) = was_volatile;
1113 return stmt_expr;
1116 static void
1117 expand_default_init (binfo, true_exp, exp, init, flags)
1118 tree binfo;
1119 tree true_exp, exp;
1120 tree init;
1121 int flags;
1123 tree type = TREE_TYPE (exp);
1125 /* It fails because there may not be a constructor which takes
1126 its own type as the first (or only parameter), but which does
1127 take other types via a conversion. So, if the thing initializing
1128 the expression is a unit element of type X, first try X(X&),
1129 followed by initialization by X. If neither of these work
1130 out, then look hard. */
1131 tree rval;
1132 tree parms;
1134 if (init && TREE_CODE (init) != TREE_LIST
1135 && (flags & LOOKUP_ONLYCONVERTING))
1137 /* Base subobjects should only get direct-initialization. */
1138 if (true_exp != exp)
1139 abort ();
1141 if (flags & DIRECT_BIND)
1142 /* Do nothing. We hit this in two cases: Reference initialization,
1143 where we aren't initializing a real variable, so we don't want
1144 to run a new constructor; and catching an exception, where we
1145 have already built up the constructor call so we could wrap it
1146 in an exception region. */;
1147 else if (TREE_CODE (init) == CONSTRUCTOR)
1148 /* A brace-enclosed initializer has whatever type is
1149 required. There's no need to convert it. */
1151 else
1152 init = ocp_convert (type, init, CONV_IMPLICIT|CONV_FORCE_TEMP, flags);
1154 if (TREE_CODE (init) == TRY_CATCH_EXPR)
1155 /* We need to protect the initialization of a catch parm
1156 with a call to terminate(), which shows up as a TRY_CATCH_EXPR
1157 around the TARGET_EXPR for the copy constructor. See
1158 expand_start_catch_block. */
1159 TREE_OPERAND (init, 0) = build (INIT_EXPR, TREE_TYPE (exp), exp,
1160 TREE_OPERAND (init, 0));
1161 else
1162 init = build (INIT_EXPR, TREE_TYPE (exp), exp, init);
1163 TREE_SIDE_EFFECTS (init) = 1;
1164 finish_expr_stmt (init);
1165 return;
1168 if (init == NULL_TREE
1169 || (TREE_CODE (init) == TREE_LIST && ! TREE_TYPE (init)))
1171 parms = init;
1172 if (parms)
1173 init = TREE_VALUE (parms);
1175 else
1176 parms = build_expr_list (NULL_TREE, init);
1178 if (TYPE_USES_VIRTUAL_BASECLASSES (type))
1180 if (true_exp == exp)
1181 parms = tree_cons (NULL_TREE, integer_one_node, parms);
1182 else
1183 parms = tree_cons (NULL_TREE, integer_zero_node, parms);
1184 flags |= LOOKUP_HAS_IN_CHARGE;
1187 rval = build_method_call (exp, ctor_identifier,
1188 parms, binfo, flags);
1189 if (TREE_SIDE_EFFECTS (rval))
1190 finish_expr_stmt (rval);
1193 /* This function is responsible for initializing EXP with INIT
1194 (if any).
1196 BINFO is the binfo of the type for who we are performing the
1197 initialization. For example, if W is a virtual base class of A and B,
1198 and C : A, B.
1199 If we are initializing B, then W must contain B's W vtable, whereas
1200 were we initializing C, W must contain C's W vtable.
1202 TRUE_EXP is nonzero if it is the true expression being initialized.
1203 In this case, it may be EXP, or may just contain EXP. The reason we
1204 need this is because if EXP is a base element of TRUE_EXP, we
1205 don't necessarily know by looking at EXP where its virtual
1206 baseclass fields should really be pointing. But we do know
1207 from TRUE_EXP. In constructors, we don't know anything about
1208 the value being initialized.
1210 ALIAS_THIS serves the same purpose it serves for expand_aggr_init.
1212 FLAGS is just passes to `build_method_call'. See that function for
1213 its description. */
1215 static void
1216 expand_aggr_init_1 (binfo, true_exp, exp, init, flags)
1217 tree binfo;
1218 tree true_exp, exp;
1219 tree init;
1220 int flags;
1222 tree type = TREE_TYPE (exp);
1224 my_friendly_assert (init != error_mark_node && type != error_mark_node, 211);
1226 /* Use a function returning the desired type to initialize EXP for us.
1227 If the function is a constructor, and its first argument is
1228 NULL_TREE, know that it was meant for us--just slide exp on
1229 in and expand the constructor. Constructors now come
1230 as TARGET_EXPRs. */
1232 if (init && TREE_CODE (exp) == VAR_DECL
1233 && TREE_CODE (init) == CONSTRUCTOR
1234 && TREE_HAS_CONSTRUCTOR (init))
1236 /* If store_init_value returns NULL_TREE, the INIT has been
1237 record in the DECL_INITIAL for EXP. That means there's
1238 nothing more we have to do. */
1239 if (!store_init_value (exp, init))
1241 if (!building_stmt_tree ())
1242 expand_decl_init (exp);
1244 else
1245 finish_expr_stmt (build (INIT_EXPR, type, exp, init));
1246 return;
1249 /* We know that expand_default_init can handle everything we want
1250 at this point. */
1251 expand_default_init (binfo, true_exp, exp, init, flags);
1254 /* Report an error if NAME is not the name of a user-defined,
1255 aggregate type. If OR_ELSE is nonzero, give an error message. */
1258 is_aggr_typedef (name, or_else)
1259 tree name;
1260 int or_else;
1262 tree type;
1264 if (name == error_mark_node)
1265 return 0;
1267 if (IDENTIFIER_HAS_TYPE_VALUE (name))
1268 type = IDENTIFIER_TYPE_VALUE (name);
1269 else
1271 if (or_else)
1272 cp_error ("`%T' is not an aggregate typedef", name);
1273 return 0;
1276 if (! IS_AGGR_TYPE (type)
1277 && TREE_CODE (type) != TEMPLATE_TYPE_PARM
1278 && TREE_CODE (type) != TEMPLATE_TEMPLATE_PARM)
1280 if (or_else)
1281 cp_error ("`%T' is not an aggregate type", type);
1282 return 0;
1284 return 1;
1287 /* Report an error if TYPE is not a user-defined, aggregate type. If
1288 OR_ELSE is nonzero, give an error message. */
1291 is_aggr_type (type, or_else)
1292 tree type;
1293 int or_else;
1295 if (type == error_mark_node)
1296 return 0;
1298 if (! IS_AGGR_TYPE (type)
1299 && TREE_CODE (type) != TEMPLATE_TYPE_PARM
1300 && TREE_CODE (type) != TEMPLATE_TEMPLATE_PARM)
1302 if (or_else)
1303 cp_error ("`%T' is not an aggregate type", type);
1304 return 0;
1306 return 1;
1309 /* Like is_aggr_typedef, but returns typedef if successful. */
1311 tree
1312 get_aggr_from_typedef (name, or_else)
1313 tree name;
1314 int or_else;
1316 tree type;
1318 if (name == error_mark_node)
1319 return NULL_TREE;
1321 if (IDENTIFIER_HAS_TYPE_VALUE (name))
1322 type = IDENTIFIER_TYPE_VALUE (name);
1323 else
1325 if (or_else)
1326 cp_error ("`%T' fails to be an aggregate typedef", name);
1327 return NULL_TREE;
1330 if (! IS_AGGR_TYPE (type)
1331 && TREE_CODE (type) != TEMPLATE_TYPE_PARM
1332 && TREE_CODE (type) != TEMPLATE_TEMPLATE_PARM)
1334 if (or_else)
1335 cp_error ("type `%T' is of non-aggregate type", type);
1336 return NULL_TREE;
1338 return type;
1341 tree
1342 get_type_value (name)
1343 tree name;
1345 if (name == error_mark_node)
1346 return NULL_TREE;
1348 if (IDENTIFIER_HAS_TYPE_VALUE (name))
1349 return IDENTIFIER_TYPE_VALUE (name);
1350 else
1351 return NULL_TREE;
1355 /* This code could just as well go in `class.c', but is placed here for
1356 modularity. */
1358 /* For an expression of the form TYPE :: NAME (PARMLIST), build
1359 the appropriate function call. */
1361 tree
1362 build_member_call (type, name, parmlist)
1363 tree type, name, parmlist;
1365 tree t;
1366 tree method_name;
1367 int dtor = 0;
1368 tree basetype_path, decl;
1370 if (TREE_CODE (name) == TEMPLATE_ID_EXPR
1371 && TREE_CODE (type) == NAMESPACE_DECL)
1373 /* 'name' already refers to the decls from the namespace, since we
1374 hit do_identifier for template_ids. */
1375 method_name = TREE_OPERAND (name, 0);
1376 /* FIXME: Since we don't do independent names right yet, the
1377 name might also be a LOOKUP_EXPR. Once we resolve this to a
1378 real decl earlier, this can go. This may happen during
1379 tsubst'ing. */
1380 if (TREE_CODE (method_name) == LOOKUP_EXPR)
1382 method_name = lookup_namespace_name
1383 (type, TREE_OPERAND (method_name, 0));
1384 TREE_OPERAND (name, 0) = method_name;
1386 my_friendly_assert (is_overloaded_fn (method_name), 980519);
1387 return build_x_function_call (name, parmlist, current_class_ref);
1390 if (type == std_node)
1391 return build_x_function_call (do_scoped_id (name, 0), parmlist,
1392 current_class_ref);
1393 if (TREE_CODE (type) == NAMESPACE_DECL)
1394 return build_x_function_call (lookup_namespace_name (type, name),
1395 parmlist, current_class_ref);
1397 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
1399 method_name = TREE_OPERAND (name, 0);
1400 if (TREE_CODE (method_name) == COMPONENT_REF)
1401 method_name = TREE_OPERAND (method_name, 1);
1402 if (is_overloaded_fn (method_name))
1403 method_name = DECL_NAME (OVL_CURRENT (method_name));
1404 TREE_OPERAND (name, 0) = method_name;
1406 else
1407 method_name = name;
1409 if (TREE_CODE (method_name) == BIT_NOT_EXPR)
1411 method_name = TREE_OPERAND (method_name, 0);
1412 dtor = 1;
1415 /* This shouldn't be here, and build_member_call shouldn't appear in
1416 parse.y! (mrs) */
1417 if (type && TREE_CODE (type) == IDENTIFIER_NODE
1418 && get_aggr_from_typedef (type, 0) == 0)
1420 tree ns = lookup_name (type, 0);
1421 if (ns && TREE_CODE (ns) == NAMESPACE_DECL)
1423 return build_x_function_call (build_offset_ref (type, name), parmlist, current_class_ref);
1427 if (type == NULL_TREE || ! is_aggr_type (type, 1))
1428 return error_mark_node;
1430 /* An operator we did not like. */
1431 if (name == NULL_TREE)
1432 return error_mark_node;
1434 if (dtor)
1436 cp_error ("cannot call destructor `%T::~%T' without object", type,
1437 method_name);
1438 return error_mark_node;
1441 decl = maybe_dummy_object (type, &basetype_path);
1443 /* Convert 'this' to the specified type to disambiguate conversion
1444 to the function's context. Apparently Standard C++ says that we
1445 shouldn't do this. */
1446 if (decl == current_class_ref
1447 && ! pedantic
1448 && ACCESSIBLY_UNIQUELY_DERIVED_P (type, current_class_type))
1450 tree olddecl = current_class_ptr;
1451 tree oldtype = TREE_TYPE (TREE_TYPE (olddecl));
1452 if (oldtype != type)
1454 tree newtype = build_qualified_type (type, TYPE_QUALS (oldtype));
1455 decl = convert_force (build_pointer_type (newtype), olddecl, 0);
1456 decl = build_indirect_ref (decl, NULL_PTR);
1460 if (method_name == constructor_name (type)
1461 || method_name == constructor_name_full (type))
1462 return build_functional_cast (type, parmlist);
1463 if (lookup_fnfields (basetype_path, method_name, 0))
1464 return build_method_call (decl,
1465 TREE_CODE (name) == TEMPLATE_ID_EXPR
1466 ? name : method_name,
1467 parmlist, basetype_path,
1468 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL);
1469 if (TREE_CODE (name) == IDENTIFIER_NODE
1470 && ((t = lookup_field (TYPE_BINFO (type), name, 1, 0))))
1472 if (t == error_mark_node)
1473 return error_mark_node;
1474 if (TREE_CODE (t) == FIELD_DECL)
1476 if (is_dummy_object (decl))
1478 cp_error ("invalid use of non-static field `%D'", t);
1479 return error_mark_node;
1481 decl = build (COMPONENT_REF, TREE_TYPE (t), decl, t);
1483 else if (TREE_CODE (t) == VAR_DECL)
1484 decl = t;
1485 else
1487 cp_error ("invalid use of member `%D'", t);
1488 return error_mark_node;
1490 if (TYPE_LANG_SPECIFIC (TREE_TYPE (decl)))
1491 return build_opfncall (CALL_EXPR, LOOKUP_NORMAL, decl,
1492 parmlist, NULL_TREE);
1493 return build_function_call (decl, parmlist);
1495 else
1497 cp_error ("no method `%T::%D'", type, name);
1498 return error_mark_node;
1502 /* Build a reference to a member of an aggregate. This is not a
1503 C++ `&', but really something which can have its address taken,
1504 and then act as a pointer to member, for example TYPE :: FIELD
1505 can have its address taken by saying & TYPE :: FIELD.
1507 @@ Prints out lousy diagnostics for operator <typename>
1508 @@ fields.
1510 @@ This function should be rewritten and placed in search.c. */
1512 tree
1513 build_offset_ref (type, name)
1514 tree type, name;
1516 tree decl, t = error_mark_node;
1517 tree member;
1518 tree basebinfo = NULL_TREE;
1519 tree orig_name = name;
1521 /* class templates can come in as TEMPLATE_DECLs here. */
1522 if (TREE_CODE (name) == TEMPLATE_DECL)
1523 return name;
1525 if (type == std_node)
1526 return do_scoped_id (name, 0);
1528 if (processing_template_decl || uses_template_parms (type))
1529 return build_min_nt (SCOPE_REF, type, name);
1531 /* Handle namespace names fully here. */
1532 if (TREE_CODE (type) == NAMESPACE_DECL)
1534 t = lookup_namespace_name (type, name);
1535 if (t != error_mark_node && ! type_unknown_p (t))
1537 mark_used (t);
1538 t = convert_from_reference (t);
1540 return t;
1543 if (type == NULL_TREE || ! is_aggr_type (type, 1))
1544 return error_mark_node;
1546 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
1548 /* If the NAME is a TEMPLATE_ID_EXPR, we are looking at
1549 something like `a.template f<int>' or the like. For the most
1550 part, we treat this just like a.f. We do remember, however,
1551 the template-id that was used. */
1552 name = TREE_OPERAND (orig_name, 0);
1554 if (TREE_CODE (name) == LOOKUP_EXPR)
1555 /* This can happen during tsubst'ing. */
1556 name = TREE_OPERAND (name, 0);
1558 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 0);
1561 if (TREE_CODE (name) == BIT_NOT_EXPR)
1563 if (! check_dtor_name (type, name))
1564 cp_error ("qualified type `%T' does not match destructor name `~%T'",
1565 type, TREE_OPERAND (name, 0));
1566 name = dtor_identifier;
1568 #if 0
1569 /* I think this is wrong, but the draft is unclear. --jason 6/15/98 */
1570 else if (name == constructor_name_full (type)
1571 || name == constructor_name (type))
1572 name = ctor_identifier;
1573 #endif
1575 if (TYPE_SIZE (complete_type (type)) == 0
1576 && !TYPE_BEING_DEFINED (type))
1578 cp_error ("incomplete type `%T' does not have member `%D'", type,
1579 name);
1580 return error_mark_node;
1583 decl = maybe_dummy_object (type, &basebinfo);
1585 member = lookup_member (basebinfo, name, 1, 0);
1587 if (member == error_mark_node)
1588 return error_mark_node;
1590 /* A lot of this logic is now handled in lookup_field and
1591 lookup_fnfield. */
1592 if (member && BASELINK_P (member))
1594 /* Go from the TREE_BASELINK to the member function info. */
1595 tree fnfields = member;
1596 t = TREE_VALUE (fnfields);
1598 if (TREE_CODE (orig_name) == TEMPLATE_ID_EXPR)
1600 /* The FNFIELDS are going to contain functions that aren't
1601 necessarily templates, and templates that don't
1602 necessarily match the explicit template parameters. We
1603 save all the functions, and the explicit parameters, and
1604 then figure out exactly what to instantiate with what
1605 arguments in instantiate_type. */
1607 if (TREE_CODE (t) != OVERLOAD)
1608 /* The code in instantiate_type which will process this
1609 expects to encounter OVERLOADs, not raw functions. */
1610 t = ovl_cons (t, NULL_TREE);
1612 return build (OFFSET_REF,
1613 unknown_type_node,
1614 decl,
1615 build (TEMPLATE_ID_EXPR,
1616 TREE_TYPE (t),
1618 TREE_OPERAND (orig_name, 1)));
1621 if (!really_overloaded_fn (t))
1623 /* Get rid of a potential OVERLOAD around it */
1624 t = OVL_CURRENT (t);
1626 /* unique functions are handled easily. */
1627 basebinfo = TREE_PURPOSE (fnfields);
1628 if (!enforce_access (basebinfo, t))
1629 return error_mark_node;
1630 mark_used (t);
1631 if (DECL_STATIC_FUNCTION_P (t))
1632 return t;
1633 return build (OFFSET_REF, TREE_TYPE (t), decl, t);
1636 TREE_TYPE (fnfields) = unknown_type_node;
1637 return build (OFFSET_REF, unknown_type_node, decl, fnfields);
1640 t = member;
1642 if (t == NULL_TREE)
1644 cp_error ("`%D' is not a member of type `%T'", name, type);
1645 return error_mark_node;
1648 if (TREE_CODE (t) == TYPE_DECL)
1650 TREE_USED (t) = 1;
1651 return t;
1653 /* static class members and class-specific enum
1654 values can be returned without further ado. */
1655 if (TREE_CODE (t) == VAR_DECL || TREE_CODE (t) == CONST_DECL)
1657 mark_used (t);
1658 return convert_from_reference (t);
1661 if (TREE_CODE (t) == FIELD_DECL && DECL_C_BIT_FIELD (t))
1663 cp_error ("illegal pointer to bit field `%D'", t);
1664 return error_mark_node;
1667 /* static class functions too. */
1668 if (TREE_CODE (t) == FUNCTION_DECL
1669 && TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
1670 my_friendly_abort (53);
1672 /* In member functions, the form `type::name' is no longer
1673 equivalent to `this->type::name', at least not until
1674 resolve_offset_ref. */
1675 return build (OFFSET_REF, build_offset_type (type, TREE_TYPE (t)), decl, t);
1678 /* If a OFFSET_REF made it through to here, then it did
1679 not have its address taken. */
1681 tree
1682 resolve_offset_ref (exp)
1683 tree exp;
1685 tree type = TREE_TYPE (exp);
1686 tree base = NULL_TREE;
1687 tree member;
1688 tree basetype, addr;
1690 if (TREE_CODE (exp) == OFFSET_REF)
1692 member = TREE_OPERAND (exp, 1);
1693 base = TREE_OPERAND (exp, 0);
1695 else
1697 my_friendly_assert (TREE_CODE (type) == OFFSET_TYPE, 214);
1698 if (TYPE_OFFSET_BASETYPE (type) != current_class_type)
1700 error ("object missing in use of pointer-to-member construct");
1701 return error_mark_node;
1703 member = exp;
1704 type = TREE_TYPE (type);
1705 base = current_class_ref;
1708 if (BASELINK_P (member))
1710 if (! flag_ms_extensions)
1711 cp_pedwarn ("assuming & on overloaded member function");
1712 return build_unary_op (ADDR_EXPR, exp, 0);
1715 if (TREE_CODE (TREE_TYPE (member)) == METHOD_TYPE)
1717 if (! flag_ms_extensions)
1718 cp_pedwarn ("assuming & on `%E'", member);
1719 return build_unary_op (ADDR_EXPR, exp, 0);
1722 if ((TREE_CODE (member) == VAR_DECL
1723 && ! TYPE_PTRMEMFUNC_P (TREE_TYPE (member))
1724 && ! TYPE_PTRMEM_P (TREE_TYPE (member)))
1725 || TREE_CODE (TREE_TYPE (member)) == FUNCTION_TYPE)
1727 /* These were static members. */
1728 if (mark_addressable (member) == 0)
1729 return error_mark_node;
1730 return member;
1733 if (TREE_CODE (TREE_TYPE (member)) == POINTER_TYPE
1734 && TREE_CODE (TREE_TYPE (TREE_TYPE (member))) == METHOD_TYPE)
1735 return member;
1737 /* Syntax error can cause a member which should
1738 have been seen as static to be grok'd as non-static. */
1739 if (TREE_CODE (member) == FIELD_DECL && current_class_ref == NULL_TREE)
1741 if (TREE_ADDRESSABLE (member) == 0)
1743 cp_error_at ("member `%D' is non-static but referenced as a static member",
1744 member);
1745 error ("at this point in file");
1746 TREE_ADDRESSABLE (member) = 1;
1748 return error_mark_node;
1751 /* The first case is really just a reference to a member of `this'. */
1752 if (TREE_CODE (member) == FIELD_DECL
1753 && (base == current_class_ref || is_dummy_object (base)))
1755 tree basetype_path;
1756 tree expr;
1758 if (TREE_CODE (exp) == OFFSET_REF && TREE_CODE (type) == OFFSET_TYPE)
1759 basetype = TYPE_OFFSET_BASETYPE (type);
1760 else
1761 basetype = DECL_CONTEXT (member);
1763 base = current_class_ptr;
1765 if (get_base_distance (basetype, TREE_TYPE (TREE_TYPE (base)), 0, &basetype_path) < 0)
1767 error_not_base_type (basetype, TREE_TYPE (TREE_TYPE (base)));
1768 return error_mark_node;
1770 /* Kludge: we need to use basetype_path now, because
1771 convert_pointer_to will bash it. */
1772 enforce_access (basetype_path, member);
1773 addr = convert_pointer_to (basetype, base);
1775 /* Even in the case of illegal access, we form the
1776 COMPONENT_REF; that will allow better error recovery than
1777 just feeding back error_mark_node. */
1778 expr = build (COMPONENT_REF, TREE_TYPE (member),
1779 build_indirect_ref (addr, NULL_PTR), member);
1780 return convert_from_reference (expr);
1783 /* Ensure that we have an object. */
1784 if (is_dummy_object (base))
1785 addr = error_mark_node;
1786 else
1787 /* If this is a reference to a member function, then return the
1788 address of the member function (which may involve going
1789 through the object's vtable), otherwise, return an expression
1790 for the dereferenced pointer-to-member construct. */
1791 addr = build_unary_op (ADDR_EXPR, base, 0);
1793 if (TYPE_PTRMEM_P (TREE_TYPE (member)))
1795 if (addr == error_mark_node)
1797 cp_error ("object missing in `%E'", exp);
1798 return error_mark_node;
1801 basetype = TYPE_OFFSET_BASETYPE (TREE_TYPE (TREE_TYPE (member)));
1802 addr = convert_pointer_to (basetype, addr);
1803 member = cp_convert (ptrdiff_type_node, member);
1805 /* Pointer to data members are offset by one, so that a null
1806 pointer with a real value of 0 is distinguishable from an
1807 offset of the first member of a structure. */
1808 member = build_binary_op (MINUS_EXPR, member,
1809 cp_convert (ptrdiff_type_node, integer_one_node));
1811 return build1 (INDIRECT_REF, type,
1812 build (PLUS_EXPR, build_pointer_type (type),
1813 addr, member));
1815 else if (TYPE_PTRMEMFUNC_P (TREE_TYPE (member)))
1817 return get_member_function_from_ptrfunc (&addr, member);
1819 my_friendly_abort (56);
1820 /* NOTREACHED */
1821 return NULL_TREE;
1824 /* Return either DECL or its known constant value (if it has one). */
1826 tree
1827 decl_constant_value (decl)
1828 tree decl;
1830 if (! TREE_THIS_VOLATILE (decl)
1831 && DECL_INITIAL (decl)
1832 && DECL_INITIAL (decl) != error_mark_node
1833 /* This is invalid if initial value is not constant.
1834 If it has either a function call, a memory reference,
1835 or a variable, then re-evaluating it could give different results. */
1836 && TREE_CONSTANT (DECL_INITIAL (decl))
1837 /* Check for cases where this is sub-optimal, even though valid. */
1838 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1839 return DECL_INITIAL (decl);
1840 return decl;
1843 /* Common subroutines of build_new and build_vec_delete. */
1845 /* Call the global __builtin_delete to delete ADDR. */
1847 static tree
1848 build_builtin_delete_call (addr)
1849 tree addr;
1851 mark_used (global_delete_fndecl);
1852 return build_call (global_delete_fndecl,
1853 void_type_node, build_expr_list (NULL_TREE, addr));
1856 /* Generate a C++ "new" expression. DECL is either a TREE_LIST
1857 (which needs to go through some sort of groktypename) or it
1858 is the name of the class we are newing. INIT is an initialization value.
1859 It is either an EXPRLIST, an EXPR_NO_COMMAS, or something in braces.
1860 If INIT is void_type_node, it means do *not* call a constructor
1861 for this instance.
1863 For types with constructors, the data returned is initialized
1864 by the appropriate constructor.
1866 Whether the type has a constructor or not, if it has a pointer
1867 to a virtual function table, then that pointer is set up
1868 here.
1870 Unless I am mistaken, a call to new () will return initialized
1871 data regardless of whether the constructor itself is private or
1872 not. NOPE; new fails if the constructor is private (jcm).
1874 Note that build_new does nothing to assure that any special
1875 alignment requirements of the type are met. Rather, it leaves
1876 it up to malloc to do the right thing. Otherwise, folding to
1877 the right alignment cal cause problems if the user tries to later
1878 free the memory returned by `new'.
1880 PLACEMENT is the `placement' list for user-defined operator new (). */
1882 extern int flag_check_new;
1884 tree
1885 build_new (placement, decl, init, use_global_new)
1886 tree placement;
1887 tree decl, init;
1888 int use_global_new;
1890 tree type, rval;
1891 tree nelts = NULL_TREE, t;
1892 int has_array = 0;
1894 if (decl == error_mark_node)
1895 return error_mark_node;
1897 if (TREE_CODE (decl) == TREE_LIST)
1899 tree absdcl = TREE_VALUE (decl);
1900 tree last_absdcl = NULL_TREE;
1902 if (current_function_decl
1903 && DECL_CONSTRUCTOR_P (current_function_decl))
1904 my_friendly_assert (immediate_size_expand == 0, 19990926);
1906 nelts = integer_one_node;
1908 if (absdcl && TREE_CODE (absdcl) == CALL_EXPR)
1909 my_friendly_abort (215);
1910 while (absdcl && TREE_CODE (absdcl) == INDIRECT_REF)
1912 last_absdcl = absdcl;
1913 absdcl = TREE_OPERAND (absdcl, 0);
1916 if (absdcl && TREE_CODE (absdcl) == ARRAY_REF)
1918 /* probably meant to be a vec new */
1919 tree this_nelts;
1921 while (TREE_OPERAND (absdcl, 0)
1922 && TREE_CODE (TREE_OPERAND (absdcl, 0)) == ARRAY_REF)
1924 last_absdcl = absdcl;
1925 absdcl = TREE_OPERAND (absdcl, 0);
1928 has_array = 1;
1929 this_nelts = TREE_OPERAND (absdcl, 1);
1930 if (this_nelts != error_mark_node)
1932 if (this_nelts == NULL_TREE)
1933 error ("new of array type fails to specify size");
1934 else if (processing_template_decl)
1936 nelts = this_nelts;
1937 absdcl = TREE_OPERAND (absdcl, 0);
1939 else
1941 int flags = pedantic ? WANT_INT : (WANT_INT | WANT_ENUM);
1942 if (build_expr_type_conversion (flags, this_nelts, 0)
1943 == NULL_TREE)
1944 pedwarn ("size in array new must have integral type");
1946 this_nelts = save_expr (cp_convert (sizetype, this_nelts));
1947 absdcl = TREE_OPERAND (absdcl, 0);
1948 if (this_nelts == integer_zero_node)
1950 warning ("zero size array reserves no space");
1951 nelts = integer_zero_node;
1953 else
1954 nelts = build_binary_op (MULT_EXPR, nelts, this_nelts);
1957 else
1958 nelts = integer_zero_node;
1961 if (last_absdcl)
1962 TREE_OPERAND (last_absdcl, 0) = absdcl;
1963 else
1964 TREE_VALUE (decl) = absdcl;
1966 type = groktypename (decl);
1967 if (! type || type == error_mark_node)
1968 return error_mark_node;
1970 else if (TREE_CODE (decl) == IDENTIFIER_NODE)
1972 if (IDENTIFIER_HAS_TYPE_VALUE (decl))
1974 /* An aggregate type. */
1975 type = IDENTIFIER_TYPE_VALUE (decl);
1976 decl = TYPE_MAIN_DECL (type);
1978 else
1980 /* A builtin type. */
1981 decl = lookup_name (decl, 1);
1982 my_friendly_assert (TREE_CODE (decl) == TYPE_DECL, 215);
1983 type = TREE_TYPE (decl);
1986 else if (TREE_CODE (decl) == TYPE_DECL)
1988 type = TREE_TYPE (decl);
1990 else
1992 type = decl;
1993 decl = TYPE_MAIN_DECL (type);
1996 if (processing_template_decl)
1998 if (has_array)
1999 t = min_tree_cons (min_tree_cons (NULL_TREE, type, NULL_TREE),
2000 build_min_nt (ARRAY_REF, NULL_TREE, nelts),
2001 NULL_TREE);
2002 else
2003 t = type;
2005 rval = build_min_nt (NEW_EXPR, placement, t, init);
2006 NEW_EXPR_USE_GLOBAL (rval) = use_global_new;
2007 return rval;
2010 /* ``A reference cannot be created by the new operator. A reference
2011 is not an object (8.2.2, 8.4.3), so a pointer to it could not be
2012 returned by new.'' ARM 5.3.3 */
2013 if (TREE_CODE (type) == REFERENCE_TYPE)
2015 error ("new cannot be applied to a reference type");
2016 type = TREE_TYPE (type);
2019 if (TREE_CODE (type) == FUNCTION_TYPE)
2021 error ("new cannot be applied to a function type");
2022 return error_mark_node;
2025 /* When the object being created is an array, the new-expression yields a
2026 pointer to the initial element (if any) of the array. For example,
2027 both new int and new int[10] return an int*. 5.3.4. */
2028 if (TREE_CODE (type) == ARRAY_TYPE && has_array == 0)
2030 nelts = array_type_nelts_top (type);
2031 has_array = 1;
2032 type = TREE_TYPE (type);
2035 if (has_array)
2036 t = build_nt (ARRAY_REF, type, nelts);
2037 else
2038 t = type;
2040 rval = build (NEW_EXPR, build_pointer_type (type), placement, t, init);
2041 NEW_EXPR_USE_GLOBAL (rval) = use_global_new;
2042 TREE_SIDE_EFFECTS (rval) = 1;
2043 rval = build_new_1 (rval);
2044 if (rval == error_mark_node)
2045 return error_mark_node;
2047 /* Wrap it in a NOP_EXPR so warn_if_unused_value doesn't complain. */
2048 rval = build1 (NOP_EXPR, TREE_TYPE (rval), rval);
2049 TREE_NO_UNUSED_WARNING (rval) = 1;
2051 return rval;
2054 /* Given a Java class, return a decl for the corresponding java.lang.Class. */
2056 static tree
2057 build_java_class_ref (type)
2058 tree type;
2060 tree name, class_decl;
2061 static tree CL_prefix = NULL_TREE;
2062 if (CL_prefix == NULL_TREE)
2063 CL_prefix = get_identifier("_CL_");
2064 if (jclass_node == NULL_TREE)
2066 jclass_node = IDENTIFIER_GLOBAL_VALUE (get_identifier("jclass"));
2067 if (jclass_node == NULL_TREE)
2068 fatal("call to Java constructor, while `jclass' undefined");
2069 jclass_node = TREE_TYPE (jclass_node);
2071 name = build_overload_with_type (CL_prefix, type);
2072 class_decl = IDENTIFIER_GLOBAL_VALUE (name);
2073 if (class_decl == NULL_TREE)
2075 push_permanent_obstack ();
2076 class_decl = build_decl (VAR_DECL, name, TREE_TYPE (jclass_node));
2077 TREE_STATIC (class_decl) = 1;
2078 DECL_EXTERNAL (class_decl) = 1;
2079 TREE_PUBLIC (class_decl) = 1;
2080 DECL_ARTIFICIAL (class_decl) = 1;
2081 DECL_IGNORED_P (class_decl) = 1;
2082 pushdecl_top_level (class_decl);
2083 make_decl_rtl (class_decl, NULL_PTR, 1);
2084 pop_obstacks ();
2086 return class_decl;
2089 /* Called from cplus_expand_expr when expanding a NEW_EXPR. The return
2090 value is immediately handed to expand_expr. */
2092 tree
2093 build_new_1 (exp)
2094 tree exp;
2096 tree placement, init;
2097 tree type, true_type, size, rval;
2098 tree nelts = NULL_TREE;
2099 tree alloc_expr, alloc_node = NULL_TREE;
2100 int has_array = 0;
2101 enum tree_code code = NEW_EXPR;
2102 int use_cookie, nothrow, check_new;
2103 int use_global_new;
2104 int use_java_new = 0;
2106 placement = TREE_OPERAND (exp, 0);
2107 type = TREE_OPERAND (exp, 1);
2108 init = TREE_OPERAND (exp, 2);
2109 use_global_new = NEW_EXPR_USE_GLOBAL (exp);
2111 if (TREE_CODE (type) == ARRAY_REF)
2113 has_array = 1;
2114 nelts = TREE_OPERAND (type, 1);
2115 type = TREE_OPERAND (type, 0);
2117 true_type = type;
2119 if (CP_TYPE_QUALS (type))
2120 type = TYPE_MAIN_VARIANT (type);
2122 /* If our base type is an array, then make sure we know how many elements
2123 it has. */
2124 while (TREE_CODE (true_type) == ARRAY_TYPE)
2126 tree this_nelts = array_type_nelts_top (true_type);
2127 nelts = build_binary_op (MULT_EXPR, nelts, this_nelts);
2128 true_type = TREE_TYPE (true_type);
2131 if (!complete_type_or_else (true_type, exp))
2132 return error_mark_node;
2134 if (has_array)
2135 size = fold (build_binary_op (MULT_EXPR, size_in_bytes (true_type),
2136 nelts));
2137 else
2138 size = size_in_bytes (type);
2140 if (TREE_CODE (true_type) == VOID_TYPE)
2142 error ("invalid type `void' for new");
2143 return error_mark_node;
2146 if (abstract_virtuals_error (NULL_TREE, true_type))
2147 return error_mark_node;
2149 /* When we allocate an array, and the corresponding deallocation
2150 function takes a second argument of type size_t, and that's the
2151 "usual deallocation function", we allocate some extra space at
2152 the beginning of the array to store the size of the array.
2154 Well, that's what we should do. For backwards compatibility, we
2155 have to do this whenever there's a two-argument array-delete
2156 operator.
2158 FIXME: For -fnew-abi, we don't have to maintain backwards
2159 compatibility and we should fix this. */
2160 use_cookie = (has_array && TYPE_VEC_NEW_USES_COOKIE (true_type)
2161 && ! (placement && ! TREE_CHAIN (placement)
2162 && TREE_TYPE (TREE_VALUE (placement)) == ptr_type_node));
2164 if (use_cookie)
2165 size = size_binop (PLUS_EXPR, size, BI_header_size);
2167 if (has_array)
2169 code = VEC_NEW_EXPR;
2171 if (init && pedantic)
2172 cp_pedwarn ("initialization in array new");
2175 /* Allocate the object. */
2177 if (! placement && TYPE_FOR_JAVA (true_type))
2179 tree class_addr, alloc_decl;
2180 tree class_decl = build_java_class_ref (true_type);
2181 tree class_size = size_in_bytes (true_type);
2182 static char alloc_name[] = "_Jv_AllocObject";
2183 use_java_new = 1;
2184 alloc_decl = IDENTIFIER_GLOBAL_VALUE (get_identifier (alloc_name));
2185 if (alloc_decl == NULL_TREE)
2186 fatal("call to Java constructor, while `%s' undefined", alloc_name);
2187 class_addr = build1 (ADDR_EXPR, jclass_node, class_decl);
2188 rval = build_function_call (alloc_decl,
2189 tree_cons (NULL_TREE, class_addr,
2190 build_tree_list (NULL_TREE,
2191 class_size)));
2192 rval = cp_convert (build_pointer_type (true_type), rval);
2194 else
2196 int susp = 0;
2198 if (flag_exceptions)
2199 /* We will use RVAL when generating an exception handler for
2200 this new-expression, so we must save it. */
2201 susp = suspend_momentary ();
2203 rval = build_op_new_call
2204 (code, true_type, tree_cons (NULL_TREE, size, placement),
2205 LOOKUP_NORMAL | (use_global_new * LOOKUP_GLOBAL));
2206 rval = cp_convert (build_pointer_type (true_type), rval);
2208 if (flag_exceptions)
2209 resume_momentary (susp);
2212 /* unless an allocation function is declared with an empty excep-
2213 tion-specification (_except.spec_), throw(), it indicates failure to
2214 allocate storage by throwing a bad_alloc exception (clause _except_,
2215 _lib.bad.alloc_); it returns a non-null pointer otherwise If the allo-
2216 cation function is declared with an empty exception-specification,
2217 throw(), it returns null to indicate failure to allocate storage and a
2218 non-null pointer otherwise.
2220 So check for a null exception spec on the op new we just called. */
2222 nothrow = 0;
2223 if (rval)
2225 /* The CALL_EXPR. */
2226 tree t = TREE_OPERAND (rval, 0);
2227 /* The function. */
2228 t = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
2229 nothrow = TYPE_NOTHROW_P (TREE_TYPE (t));
2231 check_new = (flag_check_new || nothrow) && ! use_java_new;
2233 if ((check_new || flag_exceptions) && rval)
2235 alloc_expr = get_target_expr (rval);
2236 alloc_node = rval = TREE_OPERAND (alloc_expr, 0);
2238 else
2239 alloc_expr = NULL_TREE;
2241 /* if rval is NULL_TREE I don't have to allocate it, but are we totally
2242 sure we have some extra bytes in that case for the BI_header_size
2243 cookies? And how does that interact with the code below? (mrs) */
2244 /* Finish up some magic for new'ed arrays */
2245 if (use_cookie && rval != NULL_TREE)
2247 tree extra = BI_header_size;
2248 tree cookie, exp1;
2249 rval = convert (string_type_node, rval); /* for ptr arithmetic */
2250 rval = save_expr (build_binary_op (PLUS_EXPR, rval, extra));
2251 /* Store header info. */
2252 cookie = build_indirect_ref (build (MINUS_EXPR,
2253 build_pointer_type (BI_header_type),
2254 rval, extra), NULL_PTR);
2255 exp1 = build (MODIFY_EXPR, void_type_node,
2256 build_component_ref (cookie, nelts_identifier,
2257 NULL_TREE, 0),
2258 nelts);
2259 rval = cp_convert (build_pointer_type (true_type), rval);
2260 rval = build_compound_expr
2261 (tree_cons (NULL_TREE, exp1,
2262 build_expr_list (NULL_TREE, rval)));
2265 if (rval == error_mark_node)
2266 return error_mark_node;
2268 /* Don't call any constructors or do any initialization. */
2269 if (init == void_type_node)
2270 goto done;
2272 if (TYPE_NEEDS_CONSTRUCTING (type) || init)
2274 if (! TYPE_NEEDS_CONSTRUCTING (type)
2275 && ! IS_AGGR_TYPE (type) && ! has_array)
2277 /* We are processing something like `new int (10)', which
2278 means allocate an int, and initialize it with 10. */
2279 tree deref;
2280 tree deref_type;
2282 /* At present RVAL is a temporary variable, created to hold
2283 the value from the call to `operator new'. We transform
2284 it to (*RVAL = INIT, RVAL). */
2285 rval = save_expr (rval);
2286 deref = build_indirect_ref (rval, NULL_PTR);
2288 /* Even for something like `new const int (10)' we must
2289 allow the expression to be non-const while we do the
2290 initialization. */
2291 deref_type = TREE_TYPE (deref);
2292 if (CP_TYPE_CONST_P (deref_type))
2293 TREE_TYPE (deref)
2294 = cp_build_qualified_type (deref_type,
2295 CP_TYPE_QUALS (deref_type)
2296 & ~TYPE_QUAL_CONST);
2297 TREE_READONLY (deref) = 0;
2299 if (TREE_CHAIN (init) != NULL_TREE)
2300 pedwarn ("initializer list being treated as compound expression");
2301 else if (TREE_CODE (init) == CONSTRUCTOR)
2303 pedwarn ("initializer list appears where operand should be used");
2304 init = TREE_OPERAND (init, 1);
2306 init = build_compound_expr (init);
2308 init = convert_for_initialization (deref, type, init, LOOKUP_NORMAL,
2309 "new", NULL_TREE, 0);
2310 rval = build (COMPOUND_EXPR, TREE_TYPE (rval),
2311 build_modify_expr (deref, NOP_EXPR, init),
2312 rval);
2313 TREE_NO_UNUSED_WARNING (rval) = 1;
2314 TREE_SIDE_EFFECTS (rval) = 1;
2316 else if (! has_array)
2318 tree newrval;
2319 /* Constructors are never virtual. If it has an initialization, we
2320 need to complain if we aren't allowed to use the ctor that took
2321 that argument. */
2322 int flags = LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_COMPLAIN;
2324 if (rval && TYPE_USES_VIRTUAL_BASECLASSES (true_type))
2326 init = tree_cons (NULL_TREE, integer_one_node, init);
2327 flags |= LOOKUP_HAS_IN_CHARGE;
2330 if (use_java_new)
2331 rval = save_expr (rval);
2332 newrval = rval;
2334 if (newrval && TREE_CODE (TREE_TYPE (newrval)) == POINTER_TYPE)
2335 newrval = build_indirect_ref (newrval, NULL_PTR);
2337 newrval = build_method_call (newrval, ctor_identifier,
2338 init, TYPE_BINFO (true_type), flags);
2340 if (newrval == NULL_TREE || newrval == error_mark_node)
2341 return error_mark_node;
2343 /* Java constructors compiled by jc1 do not return this. */
2344 if (use_java_new)
2345 newrval = build (COMPOUND_EXPR, TREE_TYPE (newrval),
2346 newrval, rval);
2347 rval = newrval;
2348 TREE_HAS_CONSTRUCTOR (rval) = 1;
2350 else
2351 rval = (build_vec_init
2352 (NULL_TREE,
2353 save_expr (rval),
2354 build_binary_op (MINUS_EXPR, nelts, integer_one_node),
2355 init,
2356 /*from_array=*/0));
2358 /* If any part of the object initialization terminates by throwing an
2359 exception and a suitable deallocation function can be found, the
2360 deallocation function is called to free the memory in which the
2361 object was being constructed, after which the exception continues
2362 to propagate in the context of the new-expression. If no
2363 unambiguous matching deallocation function can be found,
2364 propagating the exception does not cause the object's memory to be
2365 freed. */
2366 if (flag_exceptions && alloc_expr && ! use_java_new)
2368 enum tree_code dcode = has_array ? VEC_DELETE_EXPR : DELETE_EXPR;
2369 tree cleanup, fn = NULL_TREE;
2370 int flags = LOOKUP_NORMAL | (use_global_new * LOOKUP_GLOBAL);
2372 /* All cleanups must last longer than normal. */
2373 int yes = suspend_momentary ();
2375 /* The Standard is unclear here, but the right thing to do
2376 is to use the same method for finding deallocation
2377 functions that we use for finding allocation functions. */
2378 flags |= LOOKUP_SPECULATIVELY;
2380 /* We expect alloc_expr to look like a TARGET_EXPR around
2381 a NOP_EXPR around the CALL_EXPR we want. */
2382 fn = TREE_OPERAND (alloc_expr, 1);
2383 fn = TREE_OPERAND (fn, 0);
2385 cleanup = build_op_delete_call (dcode, alloc_node, size, flags, fn);
2387 resume_momentary (yes);
2389 /* Ack! First we allocate the memory. Then we set our sentry
2390 variable to true, and expand a cleanup that deletes the memory
2391 if sentry is true. Then we run the constructor and store the
2392 returned pointer in buf. Then we clear sentry and return buf. */
2394 if (cleanup)
2396 tree end, sentry, begin, buf, t = TREE_TYPE (rval);
2398 begin = get_target_expr (boolean_true_node);
2399 sentry = TREE_OPERAND (begin, 0);
2401 yes = suspend_momentary ();
2402 TREE_OPERAND (begin, 2)
2403 = build (COND_EXPR, void_type_node, sentry,
2404 cleanup, void_zero_node);
2405 resume_momentary (yes);
2407 rval = get_target_expr (rval);
2409 end = build (MODIFY_EXPR, TREE_TYPE (sentry),
2410 sentry, boolean_false_node);
2412 buf = TREE_OPERAND (rval, 0);
2414 rval = build (COMPOUND_EXPR, t, begin,
2415 build (COMPOUND_EXPR, t, rval,
2416 build (COMPOUND_EXPR, t, end, buf)));
2420 else if (CP_TYPE_CONST_P (true_type))
2421 cp_error ("uninitialized const in `new' of `%#T'", true_type);
2423 done:
2425 if (alloc_expr && rval == alloc_node)
2427 rval = TREE_OPERAND (alloc_expr, 1);
2428 alloc_expr = NULL_TREE;
2431 if (check_new && alloc_expr)
2433 /* Did we modify the storage? */
2434 tree ifexp = build_binary_op (NE_EXPR, alloc_node,
2435 integer_zero_node);
2436 rval = build_conditional_expr (ifexp, rval, alloc_node);
2439 if (alloc_expr)
2440 rval = build (COMPOUND_EXPR, TREE_TYPE (rval), alloc_expr, rval);
2442 if (rval && TREE_TYPE (rval) != build_pointer_type (type))
2444 /* The type of new int [3][3] is not int *, but int [3] * */
2445 rval = build_c_cast (build_pointer_type (type), rval);
2448 return rval;
2451 static tree
2452 build_vec_delete_1 (base, maxindex, type, auto_delete_vec, auto_delete,
2453 use_global_delete)
2454 tree base, maxindex, type;
2455 tree auto_delete_vec, auto_delete;
2456 int use_global_delete;
2458 tree virtual_size;
2459 tree ptype = build_pointer_type (type = complete_type (type));
2460 tree size_exp = size_in_bytes (type);
2462 /* Temporary variables used by the loop. */
2463 tree tbase, tbase_init;
2465 /* This is the body of the loop that implements the deletion of a
2466 single element, and moves temp variables to next elements. */
2467 tree body;
2469 /* This is the LOOP_EXPR that governs the deletion of the elements. */
2470 tree loop;
2472 /* This is the thing that governs what to do after the loop has run. */
2473 tree deallocate_expr = 0;
2475 /* This is the BIND_EXPR which holds the outermost iterator of the
2476 loop. It is convenient to set this variable up and test it before
2477 executing any other code in the loop.
2478 This is also the containing expression returned by this function. */
2479 tree controller = NULL_TREE;
2481 if (! IS_AGGR_TYPE (type) || ! TYPE_NEEDS_DESTRUCTOR (type))
2483 loop = integer_zero_node;
2484 goto no_destructor;
2487 /* The below is short by BI_header_size */
2488 virtual_size = fold (size_binop (MULT_EXPR, size_exp, maxindex));
2490 tbase = build_decl (VAR_DECL, NULL_TREE, ptype);
2491 tbase_init = build_modify_expr (tbase, NOP_EXPR,
2492 fold (build (PLUS_EXPR, ptype,
2493 base,
2494 virtual_size)));
2495 DECL_REGISTER (tbase) = 1;
2496 controller = build (BIND_EXPR, void_type_node, tbase, NULL_TREE, NULL_TREE);
2497 TREE_SIDE_EFFECTS (controller) = 1;
2499 if (auto_delete != integer_zero_node
2500 && auto_delete != integer_two_node)
2502 tree base_tbd = cp_convert (ptype,
2503 build_binary_op (MINUS_EXPR,
2504 cp_convert (ptr_type_node, base),
2505 BI_header_size));
2506 /* This is the real size */
2507 virtual_size = size_binop (PLUS_EXPR, virtual_size, BI_header_size);
2508 body = build_expr_list (NULL_TREE,
2509 build_x_delete (base_tbd,
2510 2 | use_global_delete,
2511 virtual_size));
2512 body = fold (build (COND_EXPR, void_type_node,
2513 fold (build (BIT_AND_EXPR, integer_type_node,
2514 auto_delete, integer_one_node)),
2515 body, integer_zero_node));
2517 else
2518 body = NULL_TREE;
2520 body = tree_cons (NULL_TREE,
2521 build_delete (ptype, tbase, auto_delete,
2522 LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 1),
2523 body);
2525 body = tree_cons (NULL_TREE,
2526 build_modify_expr (tbase, NOP_EXPR, build (MINUS_EXPR, ptype, tbase, size_exp)),
2527 body);
2529 body = tree_cons (NULL_TREE,
2530 build (EXIT_EXPR, void_type_node,
2531 build (EQ_EXPR, boolean_type_node, base, tbase)),
2532 body);
2534 loop = build (LOOP_EXPR, void_type_node, build_compound_expr (body));
2536 loop = tree_cons (NULL_TREE, tbase_init,
2537 tree_cons (NULL_TREE, loop, NULL_TREE));
2538 loop = build_compound_expr (loop);
2540 no_destructor:
2541 /* If the delete flag is one, or anything else with the low bit set,
2542 delete the storage. */
2543 if (auto_delete_vec == integer_zero_node)
2544 deallocate_expr = integer_zero_node;
2545 else
2547 tree base_tbd;
2549 /* The below is short by BI_header_size */
2550 virtual_size = fold (size_binop (MULT_EXPR, size_exp, maxindex));
2552 if (! TYPE_VEC_NEW_USES_COOKIE (type))
2553 /* no header */
2554 base_tbd = base;
2555 else
2557 base_tbd = cp_convert (ptype,
2558 build_binary_op (MINUS_EXPR,
2559 cp_convert (string_type_node, base),
2560 BI_header_size));
2561 /* True size with header. */
2562 virtual_size = size_binop (PLUS_EXPR, virtual_size, BI_header_size);
2564 deallocate_expr = build_x_delete (base_tbd,
2565 2 | use_global_delete,
2566 virtual_size);
2567 deallocate_expr = fold (build (COND_EXPR, void_type_node,
2568 fold (build (BIT_AND_EXPR,
2569 integer_type_node,
2570 auto_delete_vec,
2571 integer_one_node)),
2572 deallocate_expr, integer_zero_node));
2575 if (loop && deallocate_expr != integer_zero_node)
2577 body = tree_cons (NULL_TREE, loop,
2578 tree_cons (NULL_TREE, deallocate_expr, NULL_TREE));
2579 body = build_compound_expr (body);
2581 else
2582 body = loop;
2584 /* Outermost wrapper: If pointer is null, punt. */
2585 body = fold (build (COND_EXPR, void_type_node,
2586 fold (build (NE_EXPR, boolean_type_node, base,
2587 integer_zero_node)),
2588 body, integer_zero_node));
2589 body = build1 (NOP_EXPR, void_type_node, body);
2591 if (controller)
2593 TREE_OPERAND (controller, 1) = body;
2594 return controller;
2596 else
2597 return cp_convert (void_type_node, body);
2600 tree
2601 create_temporary_var (type)
2602 tree type;
2604 tree decl;
2606 decl = build_decl (VAR_DECL, NULL_TREE, type);
2607 TREE_USED (decl) = 1;
2608 DECL_ARTIFICIAL (decl) = 1;
2609 DECL_SOURCE_FILE (decl) = input_filename;
2610 DECL_SOURCE_LINE (decl) = lineno;
2611 DECL_IGNORED_P (decl) = 1;
2612 DECL_CONTEXT (decl) = current_function_decl;
2614 return decl;
2617 /* Create a new temporary variable of the indicated TYPE, initialized
2618 to INIT.
2620 It is not entered into current_binding_level, because that breaks
2621 things when it comes time to do final cleanups (which take place
2622 "outside" the binding contour of the function). */
2624 static tree
2625 get_temp_regvar (type, init)
2626 tree type, init;
2628 tree decl;
2630 decl = create_temporary_var (type);
2631 if (building_stmt_tree ())
2632 add_decl_stmt (decl);
2633 if (!building_stmt_tree ())
2634 DECL_RTL (decl) = assign_temp (type, 2, 0, 1);
2635 finish_expr_stmt (build_modify_expr (decl, INIT_EXPR, init));
2637 return decl;
2640 /* `build_vec_init' returns tree structure that performs
2641 initialization of a vector of aggregate types.
2643 DECL is passed only for error reporting, and provides line number
2644 and source file name information.
2645 BASE is the space where the vector will be. For a vector of Ts,
2646 the type of BASE is `T*'.
2647 MAXINDEX is the maximum index of the array (one less than the
2648 number of elements).
2649 INIT is the (possibly NULL) initializer.
2651 FROM_ARRAY is 0 if we should init everything with INIT
2652 (i.e., every element initialized from INIT).
2653 FROM_ARRAY is 1 if we should index into INIT in parallel
2654 with initialization of DECL.
2655 FROM_ARRAY is 2 if we should index into INIT in parallel,
2656 but use assignment instead of initialization. */
2658 tree
2659 build_vec_init (decl, base, maxindex, init, from_array)
2660 tree decl, base, maxindex, init;
2661 int from_array;
2663 tree rval;
2664 tree base2 = NULL_TREE;
2665 tree size;
2666 tree itype = NULL_TREE;
2667 tree iterator;
2668 /* The type of an element in the array. */
2669 tree type;
2670 /* The type of a pointer to an element in the array. */
2671 tree ptype;
2672 tree stmt_expr;
2673 tree compound_stmt;
2674 int destroy_temps;
2675 tree try_block = NULL_TREE;
2676 tree try_body;
2677 int num_initialized_elts = 0;
2679 maxindex = cp_convert (ptrdiff_type_node, maxindex);
2680 if (maxindex == error_mark_node)
2681 return error_mark_node;
2683 type = TREE_TYPE (TREE_TYPE (base));
2684 ptype = build_pointer_type (type);
2685 size = size_in_bytes (type);
2687 /* The code we are generating looks like:
2689 T* t1 = (T*) base;
2690 T* rval = base;
2691 ptrdiff_t iterator = maxindex;
2692 try {
2693 ... initializations from CONSTRUCTOR ...
2694 if (iterator != -1) {
2695 do {
2696 ... initialize *base ...
2697 ++base;
2698 } while (--iterator != -1);
2700 } catch (...) {
2701 ... destroy elements that were constructed ...
2704 We can omit the try and catch blocks if we know that the
2705 initialization will never throw an exception, or if the array
2706 elements do not have destructors. If we have a CONSTRUCTOR to
2707 give us initialization information, we emit code to initialize
2708 each of the elements before the loop in the try block, and then
2709 iterate over fewer elements. We can omit the loop completely if
2710 the elements of the array do not have constructors.
2712 We actually wrap the entire body of the above in a STMT_EXPR, for
2713 tidiness.
2715 When copying from array to another, when the array elements have
2716 only trivial copy constructors, we should use __builtin_memcpy
2717 rather than generating a loop. That way, we could take advantage
2718 of whatever cleverness the back-end has for dealing with copies
2719 of blocks of memory. */
2721 begin_init_stmts (&stmt_expr, &compound_stmt);
2722 destroy_temps = stmts_are_full_exprs_p;
2723 stmts_are_full_exprs_p = 0;
2724 rval = get_temp_regvar (ptype,
2725 cp_convert (ptype, default_conversion (base)));
2726 base = get_temp_regvar (ptype, rval);
2727 iterator = get_temp_regvar (ptrdiff_type_node, maxindex);
2729 /* Protect the entire array initialization so that we can destroy
2730 the partially constructed array if an exception is thrown. */
2731 if (flag_exceptions && TYPE_NEEDS_DESTRUCTOR (type))
2733 try_block = begin_try_block ();
2734 try_body = begin_compound_stmt (/*has_no_scope=*/1);
2737 if (init != NULL_TREE && TREE_CODE (init) == CONSTRUCTOR
2738 && (!decl || same_type_p (TREE_TYPE (init), TREE_TYPE (decl))))
2740 /* Do non-default initialization resulting from brace-enclosed
2741 initializers. */
2743 tree elts;
2744 from_array = 0;
2746 for (elts = CONSTRUCTOR_ELTS (init); elts; elts = TREE_CHAIN (elts))
2748 tree elt = TREE_VALUE (elts);
2749 tree baseref = build1 (INDIRECT_REF, type, base);
2751 num_initialized_elts++;
2753 if (IS_AGGR_TYPE (type) || TREE_CODE (type) == ARRAY_TYPE)
2754 finish_expr_stmt (build_aggr_init (baseref, elt, 0));
2755 else
2756 finish_expr_stmt (build_modify_expr (baseref, NOP_EXPR,
2757 elt));
2759 finish_expr_stmt (build_modify_expr
2760 (base,
2761 NOP_EXPR,
2762 build (PLUS_EXPR, build_pointer_type (type),
2763 base, size)));
2764 finish_expr_stmt (build_modify_expr
2765 (iterator,
2766 NOP_EXPR,
2767 build (MINUS_EXPR, ptrdiff_type_node,
2768 iterator, integer_one_node)));
2771 /* Clear out INIT so that we don't get confused below. */
2772 init = NULL_TREE;
2774 else if (from_array)
2776 /* If initializing one array from another, initialize element by
2777 element. We rely upon the below calls the do argument
2778 checking. */
2779 if (decl == NULL_TREE)
2781 sorry ("initialization of array from dissimilar array type");
2782 return error_mark_node;
2784 if (init)
2786 base2 = default_conversion (init);
2787 itype = TREE_TYPE (base2);
2788 base2 = get_temp_regvar (itype, base2);
2789 itype = TREE_TYPE (itype);
2791 else if (TYPE_LANG_SPECIFIC (type)
2792 && TYPE_NEEDS_CONSTRUCTING (type)
2793 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
2795 error ("initializer ends prematurely");
2796 return error_mark_node;
2800 /* Now, default-initialize any remaining elements. We don't need to
2801 do that if a) the type does not need constructing, or b) we've
2802 already initialized all the elements.
2804 We do need to keep going if we're copying an array. */
2806 if (from_array
2807 || (TYPE_NEEDS_CONSTRUCTING (type)
2808 && !(TREE_CODE (maxindex) == INTEGER_CST
2809 && num_initialized_elts == TREE_INT_CST_LOW (maxindex) + 1)))
2811 /* If the ITERATOR is equal to -1, then we don't have to loop;
2812 we've already initialized all the elements. */
2813 tree if_stmt;
2814 tree do_stmt;
2815 tree do_body;
2816 tree elt_init;
2818 if_stmt = begin_if_stmt ();
2819 finish_if_stmt_cond (build (NE_EXPR, boolean_type_node,
2820 iterator, minus_one_node),
2821 if_stmt);
2823 /* Otherwise, loop through the elements. */
2824 do_stmt = begin_do_stmt ();
2825 do_body = begin_compound_stmt (/*has_no_scope=*/1);
2827 /* When we're not building a statement-tree, things are a little
2828 complicated. If, when we recursively call build_aggr_init,
2829 an expression containing a TARGET_EXPR is expanded, then it
2830 may get a cleanup. Then, the result of that expression is
2831 passed to finish_expr_stmt, which will call
2832 expand_start_target_temps/expand_end_target_temps. However,
2833 the latter call will not cause the cleanup to run because
2834 that block will still be on the block stack. So, we call
2835 expand_start_target_temps here manually; the corresponding
2836 call to expand_end_target_temps below will cause the cleanup
2837 to be performed. */
2838 if (!building_stmt_tree ())
2839 expand_start_target_temps ();
2841 if (from_array)
2843 tree to = build1 (INDIRECT_REF, type, base);
2844 tree from;
2846 if (base2)
2847 from = build1 (INDIRECT_REF, itype, base2);
2848 else
2849 from = NULL_TREE;
2851 if (from_array == 2)
2852 elt_init = build_modify_expr (to, NOP_EXPR, from);
2853 else if (TYPE_NEEDS_CONSTRUCTING (type))
2854 elt_init = build_aggr_init (to, from, 0);
2855 else if (from)
2856 elt_init = build_modify_expr (to, NOP_EXPR, from);
2857 else
2858 my_friendly_abort (57);
2860 else if (TREE_CODE (type) == ARRAY_TYPE)
2862 if (init != 0)
2863 sorry ("cannot initialize multi-dimensional array with initializer");
2864 elt_init = (build_vec_init
2865 (decl,
2866 build1 (NOP_EXPR,
2867 build_pointer_type (TREE_TYPE (type)),
2868 base),
2869 array_type_nelts (type), 0, 0));
2871 else
2872 elt_init = build_aggr_init (build1 (INDIRECT_REF, type, base),
2873 init, 0);
2875 /* The initialization of each array element is a
2876 full-expression. */
2877 if (!building_stmt_tree ())
2879 finish_expr_stmt (elt_init);
2880 expand_end_target_temps ();
2882 else
2884 stmts_are_full_exprs_p = 1;
2885 finish_expr_stmt (elt_init);
2886 stmts_are_full_exprs_p = 0;
2889 finish_expr_stmt (build_modify_expr
2890 (base,
2891 NOP_EXPR,
2892 build (PLUS_EXPR, build_pointer_type (type),
2893 base, size)));
2894 if (base2)
2895 finish_expr_stmt (build_modify_expr
2896 (base2,
2897 NOP_EXPR,
2898 build (PLUS_EXPR, build_pointer_type (type),
2899 base2, size)));
2901 finish_compound_stmt (/*has_no_scope=*/1, do_body);
2902 finish_do_body (do_stmt);
2903 finish_do_stmt (build (NE_EXPR, boolean_type_node,
2904 build (PREDECREMENT_EXPR,
2905 ptrdiff_type_node,
2906 iterator,
2907 integer_one_node),
2908 minus_one_node),
2909 do_stmt);
2911 finish_then_clause (if_stmt);
2912 finish_if_stmt ();
2915 /* Make sure to cleanup any partially constructed elements. */
2916 if (flag_exceptions && TYPE_NEEDS_DESTRUCTOR (type))
2918 tree e;
2920 finish_compound_stmt (/*has_no_scope=*/1, try_body);
2921 finish_cleanup_try_block (try_block);
2922 e = build_vec_delete_1 (rval,
2923 build_binary_op (MINUS_EXPR, maxindex,
2924 iterator),
2925 type,
2926 /*auto_delete_vec=*/integer_zero_node,
2927 /*auto_delete=*/integer_zero_node,
2928 /*use_global_delete=*/0);
2929 finish_cleanup (e, try_block);
2932 /* The value of the array initialization is the address of the
2933 first element in the array. */
2934 finish_expr_stmt (rval);
2936 stmt_expr = finish_init_stmts (stmt_expr, compound_stmt);
2937 stmts_are_full_exprs_p = destroy_temps;
2938 return stmt_expr;
2941 /* Free up storage of type TYPE, at address ADDR.
2943 TYPE is a POINTER_TYPE and can be ptr_type_node for no special type
2944 of pointer.
2946 VIRTUAL_SIZE is the amount of storage that was allocated, and is
2947 used as the second argument to operator delete. It can include
2948 things like padding and magic size cookies. It has virtual in it,
2949 because if you have a base pointer and you delete through a virtual
2950 destructor, it should be the size of the dynamic object, not the
2951 static object, see Free Store 12.5 ANSI C++ WP.
2953 This does not call any destructors. */
2955 tree
2956 build_x_delete (addr, which_delete, virtual_size)
2957 tree addr;
2958 int which_delete;
2959 tree virtual_size;
2961 int use_global_delete = which_delete & 1;
2962 int use_vec_delete = !!(which_delete & 2);
2963 enum tree_code code = use_vec_delete ? VEC_DELETE_EXPR : DELETE_EXPR;
2964 int flags = LOOKUP_NORMAL | (use_global_delete * LOOKUP_GLOBAL);
2966 return build_op_delete_call (code, addr, virtual_size, flags, NULL_TREE);
2969 /* Generate a call to a destructor. TYPE is the type to cast ADDR to.
2970 ADDR is an expression which yields the store to be destroyed.
2971 AUTO_DELETE is nonzero if a call to DELETE should be made or not.
2972 If in the program, (AUTO_DELETE & 2) is non-zero, we tear down the
2973 virtual baseclasses.
2974 If in the program, (AUTO_DELETE & 1) is non-zero, then we deallocate.
2976 FLAGS is the logical disjunction of zero or more LOOKUP_
2977 flags. See cp-tree.h for more info.
2979 This function does not delete an object's virtual base classes. */
2981 tree
2982 build_delete (type, addr, auto_delete, flags, use_global_delete)
2983 tree type, addr;
2984 tree auto_delete;
2985 int flags;
2986 int use_global_delete;
2988 tree member;
2989 tree expr;
2990 tree ref;
2992 if (addr == error_mark_node)
2993 return error_mark_node;
2995 /* Can happen when CURRENT_EXCEPTION_OBJECT gets its type
2996 set to `error_mark_node' before it gets properly cleaned up. */
2997 if (type == error_mark_node)
2998 return error_mark_node;
3000 type = TYPE_MAIN_VARIANT (type);
3002 if (TREE_CODE (type) == POINTER_TYPE)
3004 type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
3005 if (type != void_type_node && !complete_type_or_else (type, addr))
3006 return error_mark_node;
3007 if (TREE_CODE (type) == ARRAY_TYPE)
3008 goto handle_array;
3009 if (! IS_AGGR_TYPE (type))
3011 /* Call the builtin operator delete. */
3012 return build_builtin_delete_call (addr);
3014 if (TREE_SIDE_EFFECTS (addr))
3015 addr = save_expr (addr);
3017 /* throw away const and volatile on target type of addr */
3018 addr = convert_force (build_pointer_type (type), addr, 0);
3019 ref = build_indirect_ref (addr, NULL_PTR);
3021 else if (TREE_CODE (type) == ARRAY_TYPE)
3023 handle_array:
3024 if (TREE_SIDE_EFFECTS (addr))
3025 addr = save_expr (addr);
3026 if (TYPE_DOMAIN (type) == NULL_TREE)
3028 error ("unknown array size in delete");
3029 return error_mark_node;
3031 return build_vec_delete (addr, array_type_nelts (type),
3032 auto_delete, integer_zero_node,
3033 use_global_delete);
3035 else
3037 /* Don't check PROTECT here; leave that decision to the
3038 destructor. If the destructor is accessible, call it,
3039 else report error. */
3040 addr = build_unary_op (ADDR_EXPR, addr, 0);
3041 if (TREE_SIDE_EFFECTS (addr))
3042 addr = save_expr (addr);
3044 if (TREE_CONSTANT (addr))
3045 addr = convert_pointer_to (type, addr);
3046 else
3047 addr = convert_force (build_pointer_type (type), addr, 0);
3049 ref = build_indirect_ref (addr, NULL_PTR);
3052 my_friendly_assert (IS_AGGR_TYPE (type), 220);
3054 if (! TYPE_NEEDS_DESTRUCTOR (type))
3056 if (auto_delete == integer_zero_node)
3057 return void_zero_node;
3059 return build_op_delete_call
3060 (DELETE_EXPR, addr, c_sizeof_nowarn (type),
3061 LOOKUP_NORMAL | (use_global_delete * LOOKUP_GLOBAL),
3062 NULL_TREE);
3065 /* Below, we will reverse the order in which these calls are made.
3066 If we have a destructor, then that destructor will take care
3067 of the base classes; otherwise, we must do that here. */
3068 if (TYPE_HAS_DESTRUCTOR (type))
3070 tree passed_auto_delete;
3071 tree do_delete = NULL_TREE;
3072 tree ifexp;
3074 if (use_global_delete)
3076 tree cond = fold (build (BIT_AND_EXPR, integer_type_node,
3077 auto_delete, integer_one_node));
3078 tree call = build_builtin_delete_call (addr);
3080 cond = fold (build (COND_EXPR, void_type_node, cond,
3081 call, void_zero_node));
3082 if (cond != void_zero_node)
3083 do_delete = cond;
3085 passed_auto_delete = fold (build (BIT_AND_EXPR, integer_type_node,
3086 auto_delete, integer_two_node));
3088 else
3089 passed_auto_delete = auto_delete;
3091 expr = build_method_call
3092 (ref, dtor_identifier, build_expr_list (NULL_TREE, passed_auto_delete),
3093 NULL_TREE, flags);
3095 if (do_delete)
3096 expr = build (COMPOUND_EXPR, void_type_node, expr, do_delete);
3098 if (flags & LOOKUP_DESTRUCTOR)
3099 /* Explicit destructor call; don't check for null pointer. */
3100 ifexp = integer_one_node;
3101 else
3102 /* Handle deleting a null pointer. */
3103 ifexp = fold (build_binary_op (NE_EXPR, addr, integer_zero_node));
3105 if (ifexp != integer_one_node)
3106 expr = build (COND_EXPR, void_type_node,
3107 ifexp, expr, void_zero_node);
3109 return expr;
3111 else
3113 /* We only get here from finish_function for a destructor. */
3114 tree binfos = BINFO_BASETYPES (TYPE_BINFO (type));
3115 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
3116 tree base_binfo = n_baseclasses > 0 ? TREE_VEC_ELT (binfos, 0) : NULL_TREE;
3117 tree exprstmt = NULL_TREE;
3118 tree parent_auto_delete = auto_delete;
3119 tree cond;
3121 /* Set this again before we call anything, as we might get called
3122 recursively. */
3123 TYPE_HAS_DESTRUCTOR (type) = 1;
3125 /* If we have member delete or vbases, we call delete in
3126 finish_function. */
3127 if (auto_delete == integer_zero_node)
3128 cond = NULL_TREE;
3129 else if (base_binfo == NULL_TREE
3130 || ! TYPE_NEEDS_DESTRUCTOR (BINFO_TYPE (base_binfo)))
3132 cond = build (COND_EXPR, void_type_node,
3133 build (BIT_AND_EXPR, integer_type_node, auto_delete, integer_one_node),
3134 build_builtin_delete_call (addr),
3135 void_zero_node);
3137 else
3138 cond = NULL_TREE;
3140 if (cond)
3141 exprstmt = build_expr_list (NULL_TREE, cond);
3143 if (base_binfo
3144 && ! TREE_VIA_VIRTUAL (base_binfo)
3145 && TYPE_NEEDS_DESTRUCTOR (BINFO_TYPE (base_binfo)))
3147 tree this_auto_delete;
3149 if (BINFO_OFFSET_ZEROP (base_binfo))
3150 this_auto_delete = parent_auto_delete;
3151 else
3152 this_auto_delete = integer_zero_node;
3154 expr = build_scoped_method_call
3155 (ref, base_binfo, dtor_identifier,
3156 build_expr_list (NULL_TREE, this_auto_delete));
3157 exprstmt = tree_cons (NULL_TREE, expr, exprstmt);
3160 /* Take care of the remaining baseclasses. */
3161 for (i = 1; i < n_baseclasses; i++)
3163 base_binfo = TREE_VEC_ELT (binfos, i);
3164 if (! TYPE_NEEDS_DESTRUCTOR (BINFO_TYPE (base_binfo))
3165 || TREE_VIA_VIRTUAL (base_binfo))
3166 continue;
3168 expr = build_scoped_method_call
3169 (ref, base_binfo, dtor_identifier,
3170 build_expr_list (NULL_TREE, integer_zero_node));
3172 exprstmt = tree_cons (NULL_TREE, expr, exprstmt);
3175 for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member))
3177 if (TREE_CODE (member) != FIELD_DECL)
3178 continue;
3179 if (TYPE_NEEDS_DESTRUCTOR (TREE_TYPE (member)))
3181 tree this_member = build_component_ref (ref, DECL_NAME (member), NULL_TREE, 0);
3182 tree this_type = TREE_TYPE (member);
3183 expr = build_delete (this_type, this_member, integer_two_node, flags, 0);
3184 exprstmt = tree_cons (NULL_TREE, expr, exprstmt);
3188 if (exprstmt)
3189 return build_compound_expr (exprstmt);
3190 /* Virtual base classes make this function do nothing. */
3191 return void_zero_node;
3195 /* For type TYPE, delete the virtual baseclass objects of DECL. */
3197 tree
3198 build_vbase_delete (type, decl)
3199 tree type, decl;
3201 tree vbases = CLASSTYPE_VBASECLASSES (type);
3202 tree result = NULL_TREE;
3203 tree addr = build_unary_op (ADDR_EXPR, decl, 0);
3205 my_friendly_assert (addr != error_mark_node, 222);
3207 while (vbases)
3209 tree this_addr = convert_force (build_pointer_type (BINFO_TYPE (vbases)),
3210 addr, 0);
3211 result = tree_cons (NULL_TREE,
3212 build_delete (TREE_TYPE (this_addr), this_addr,
3213 integer_zero_node,
3214 LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 0),
3215 result);
3216 vbases = TREE_CHAIN (vbases);
3218 return build_compound_expr (nreverse (result));
3221 /* Build a C++ vector delete expression.
3222 MAXINDEX is the number of elements to be deleted.
3223 ELT_SIZE is the nominal size of each element in the vector.
3224 BASE is the expression that should yield the store to be deleted.
3225 This function expands (or synthesizes) these calls itself.
3226 AUTO_DELETE_VEC says whether the container (vector) should be deallocated.
3227 AUTO_DELETE say whether each item in the container should be deallocated.
3229 This also calls delete for virtual baseclasses of elements of the vector.
3231 Update: MAXINDEX is no longer needed. The size can be extracted from the
3232 start of the vector for pointers, and from the type for arrays. We still
3233 use MAXINDEX for arrays because it happens to already have one of the
3234 values we'd have to extract. (We could use MAXINDEX with pointers to
3235 confirm the size, and trap if the numbers differ; not clear that it'd
3236 be worth bothering.) */
3238 tree
3239 build_vec_delete (base, maxindex, auto_delete_vec, auto_delete,
3240 use_global_delete)
3241 tree base, maxindex;
3242 tree auto_delete_vec, auto_delete;
3243 int use_global_delete;
3245 tree type;
3247 if (TREE_CODE (base) == OFFSET_REF)
3248 base = resolve_offset_ref (base);
3250 type = TREE_TYPE (base);
3252 base = stabilize_reference (base);
3254 /* Since we can use base many times, save_expr it. */
3255 if (TREE_SIDE_EFFECTS (base))
3256 base = save_expr (base);
3258 if (TREE_CODE (type) == POINTER_TYPE)
3260 /* Step back one from start of vector, and read dimension. */
3261 tree cookie_addr = build (MINUS_EXPR, build_pointer_type (BI_header_type),
3262 base, BI_header_size);
3263 tree cookie = build_indirect_ref (cookie_addr, NULL_PTR);
3264 maxindex = build_component_ref (cookie, nelts_identifier, NULL_TREE, 0);
3266 type = TREE_TYPE (type);
3267 while (TREE_CODE (type) == ARRAY_TYPE);
3269 else if (TREE_CODE (type) == ARRAY_TYPE)
3271 /* get the total number of things in the array, maxindex is a bad name */
3272 maxindex = array_type_nelts_total (type);
3273 while (TREE_CODE (type) == ARRAY_TYPE)
3274 type = TREE_TYPE (type);
3275 base = build_unary_op (ADDR_EXPR, base, 1);
3277 else
3279 if (base != error_mark_node)
3280 error ("type to vector delete is neither pointer or array type");
3281 return error_mark_node;
3284 return build_vec_delete_1 (base, maxindex, type, auto_delete_vec, auto_delete,
3285 use_global_delete);