* cp-tree.h (CLASSTYPE_VFIELD): Remove.
[official-gcc.git] / gcc / cp / init.c
blob5f395f5ba2b2da0ca7aeb7b118a96cc26b8ae80a
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 push_momentary ();
109 for (i = 0; i < n_baselinks; i++)
111 tree real_base_binfo = TREE_VEC_ELT (real_binfos, i);
112 tree base_binfo = TREE_VEC_ELT (binfos, i);
113 int is_not_base_vtable
114 = i != CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (real_binfo));
115 if (! TREE_VIA_VIRTUAL (real_base_binfo))
116 expand_direct_vtbls_init (real_base_binfo, base_binfo,
117 is_not_base_vtable, can_elide, addr);
119 #if 0
120 /* Before turning this on, make sure it is correct. */
121 if (can_elide && ! BINFO_MODIFIED (binfo))
122 return;
123 #endif
124 /* Should we use something besides CLASSTYPE_VFIELDS? */
125 if (init_self && CLASSTYPE_VFIELDS (BINFO_TYPE (real_binfo)))
127 tree base_ptr = convert_pointer_to_real (binfo, addr);
128 expand_virtual_init (real_binfo, base_ptr);
130 pop_momentary ();
133 /* 348 - 351 */
134 /* Subroutine of emit_base_init. */
136 static void
137 perform_member_init (member, name, init, explicit)
138 tree member, name, init;
139 int explicit;
141 tree decl;
142 tree type = TREE_TYPE (member);
144 decl = build_component_ref (current_class_ref, name, NULL_TREE, explicit);
146 /* Deal with this here, as we will get confused if we try to call the
147 assignment op for an anonymous union. This can happen in a
148 synthesized copy constructor. */
149 if (ANON_AGGR_TYPE_P (type))
151 init = build (INIT_EXPR, type, decl, TREE_VALUE (init));
152 finish_expr_stmt (init);
154 else if (TYPE_NEEDS_CONSTRUCTING (type)
155 || (init && TYPE_HAS_CONSTRUCTOR (type)))
157 /* Since `init' is already a TREE_LIST on the current_member_init_list,
158 only build it into one if we aren't already a list. */
159 if (init != NULL_TREE && TREE_CODE (init) != TREE_LIST)
160 init = build_expr_list (NULL_TREE, init);
162 if (explicit
163 && TREE_CODE (type) == ARRAY_TYPE
164 && init != NULL_TREE
165 && TREE_CHAIN (init) == NULL_TREE
166 && TREE_CODE (TREE_TYPE (TREE_VALUE (init))) == ARRAY_TYPE)
168 /* Initialization of one array from another. */
169 finish_expr_stmt
170 (build_vec_init (TREE_OPERAND (decl, 1), decl,
171 array_type_nelts (type), TREE_VALUE (init), 1));
173 else
174 finish_expr_stmt (build_aggr_init (decl, init, 0));
176 else
178 if (init == NULL_TREE)
180 if (explicit)
182 /* default-initialization. */
183 if (AGGREGATE_TYPE_P (type))
185 /* This is a default initialization of an aggregate,
186 but not one of non-POD class type. We cleverly
187 notice that the initialization rules in such a
188 case are the same as for initialization with an
189 empty brace-initialization list. We don't want
190 to call build_modify_expr as that will go looking
191 for constructors and such. */
192 tree e = build (CONSTRUCTOR, type, NULL_TREE, NULL_TREE);
193 TREE_SIDE_EFFECTS (e) = 1;
194 finish_expr_stmt (build (INIT_EXPR, type, decl, e));
196 else if (TREE_CODE (type) == REFERENCE_TYPE)
197 cp_error ("default-initialization of `%#D', which has reference type",
198 member);
199 else
200 init = integer_zero_node;
202 /* member traversal: note it leaves init NULL */
203 else if (TREE_CODE (TREE_TYPE (member)) == REFERENCE_TYPE)
204 cp_pedwarn ("uninitialized reference member `%D'", member);
206 else if (TREE_CODE (init) == TREE_LIST)
208 /* There was an explicit member initialization. Do some
209 work in that case. */
210 if (TREE_CHAIN (init))
212 warning ("initializer list treated as compound expression");
213 init = build_compound_expr (init);
215 else
216 init = TREE_VALUE (init);
219 if (init)
220 finish_expr_stmt (build_modify_expr (decl, INIT_EXPR, init));
223 if (TYPE_NEEDS_DESTRUCTOR (type))
225 tree expr;
227 expr = build_component_ref (current_class_ref, name, NULL_TREE,
228 explicit);
229 expr = build_delete (type, expr, integer_zero_node,
230 LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR, 0);
232 if (expr != error_mark_node)
233 finish_subobject (expr);
237 extern int warn_reorder;
239 /* Subroutine of emit_member_init. */
241 static tree
242 sort_member_init (t)
243 tree t;
245 tree x, member, name, field;
246 tree init_list = NULL_TREE;
247 int last_pos = 0;
248 tree last_field = NULL_TREE;
250 for (member = TYPE_FIELDS (t); member ; member = TREE_CHAIN (member))
252 int pos;
254 /* member could be, for example, a CONST_DECL for an enumerated
255 tag; we don't want to try to initialize that, since it already
256 has a value. */
257 if (TREE_CODE (member) != FIELD_DECL || !DECL_NAME (member))
258 continue;
260 for (x = current_member_init_list, pos = 0; x; x = TREE_CHAIN (x), ++pos)
262 /* If we cleared this out, then pay no attention to it. */
263 if (TREE_PURPOSE (x) == NULL_TREE)
264 continue;
265 name = TREE_PURPOSE (x);
267 if (TREE_CODE (name) == IDENTIFIER_NODE)
268 field = IDENTIFIER_CLASS_VALUE (name);
269 else
271 my_friendly_assert (TREE_CODE (name) == FIELD_DECL, 348);
272 field = name;
275 /* If one member shadows another, get the outermost one. */
276 if (TREE_CODE (field) == TREE_LIST)
277 field = TREE_VALUE (field);
279 if (field == member)
281 if (warn_reorder)
283 if (pos < last_pos)
285 cp_warning_at ("member initializers for `%#D'", last_field);
286 cp_warning_at (" and `%#D'", field);
287 warning (" will be re-ordered to match declaration order");
289 last_pos = pos;
290 last_field = field;
293 /* Make sure we won't try to work on this init again. */
294 TREE_PURPOSE (x) = NULL_TREE;
295 x = build_tree_list (name, TREE_VALUE (x));
296 goto got_it;
300 /* If we didn't find MEMBER in the list, create a dummy entry
301 so the two lists (INIT_LIST and the list of members) will be
302 symmetrical. */
303 x = build_tree_list (NULL_TREE, NULL_TREE);
304 got_it:
305 init_list = chainon (init_list, x);
308 /* Initializers for base members go at the end. */
309 for (x = current_member_init_list ; x ; x = TREE_CHAIN (x))
311 name = TREE_PURPOSE (x);
312 if (name)
314 if (purpose_member (name, init_list))
316 cp_error ("multiple initializations given for member `%D'",
317 IDENTIFIER_CLASS_VALUE (name));
318 continue;
321 init_list = chainon (init_list,
322 build_tree_list (name, TREE_VALUE (x)));
323 TREE_PURPOSE (x) = NULL_TREE;
327 return init_list;
330 static void
331 sort_base_init (t, rbase_ptr, vbase_ptr)
332 tree t, *rbase_ptr, *vbase_ptr;
334 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
335 int n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
337 int i;
338 tree x;
339 tree last;
341 /* For warn_reorder. */
342 int last_pos = 0;
343 tree last_base = NULL_TREE;
345 tree rbases = NULL_TREE;
346 tree vbases = NULL_TREE;
348 /* First walk through and splice out vbase and invalid initializers.
349 Also replace names with binfos. */
351 last = tree_cons (NULL_TREE, NULL_TREE, current_base_init_list);
352 for (x = TREE_CHAIN (last); x; x = TREE_CHAIN (x))
354 tree basetype = TREE_PURPOSE (x);
355 tree binfo = NULL_TREE;
357 if (basetype == NULL_TREE)
359 /* Initializer for single base class. Must not
360 use multiple inheritance or this is ambiguous. */
361 switch (n_baseclasses)
363 case 0:
364 cp_error ("`%T' does not have a base class to initialize",
365 current_class_type);
366 return;
367 case 1:
368 break;
369 default:
370 cp_error ("unnamed initializer ambiguous for `%T' which uses multiple inheritance",
371 current_class_type);
372 return;
374 binfo = TREE_VEC_ELT (binfos, 0);
376 else if (is_aggr_type (basetype, 1))
378 binfo = binfo_or_else (basetype, t);
379 if (binfo == NULL_TREE)
380 continue;
382 /* Virtual base classes are special cases. Their initializers
383 are recorded with this constructor, and they are used when
384 this constructor is the top-level constructor called. */
385 if (TREE_VIA_VIRTUAL (binfo))
387 tree v = CLASSTYPE_VBASECLASSES (t);
388 while (BINFO_TYPE (v) != BINFO_TYPE (binfo))
389 v = TREE_CHAIN (v);
391 vbases = tree_cons (v, TREE_VALUE (x), vbases);
392 continue;
394 else
396 /* Otherwise, if it is not an immediate base class, complain. */
397 for (i = n_baseclasses-1; i >= 0; i--)
398 if (BINFO_TYPE (binfo) == BINFO_TYPE (TREE_VEC_ELT (binfos, i)))
399 break;
400 if (i < 0)
402 cp_error ("`%T' is not an immediate base class of `%T'",
403 basetype, current_class_type);
404 continue;
408 else
409 my_friendly_abort (365);
411 TREE_PURPOSE (x) = binfo;
412 TREE_CHAIN (last) = x;
413 last = x;
415 TREE_CHAIN (last) = NULL_TREE;
417 /* Now walk through our regular bases and make sure they're initialized. */
419 for (i = 0; i < n_baseclasses; ++i)
421 tree base_binfo = TREE_VEC_ELT (binfos, i);
422 int pos;
424 if (TREE_VIA_VIRTUAL (base_binfo))
425 continue;
427 for (x = current_base_init_list, pos = 0; x; x = TREE_CHAIN (x), ++pos)
429 tree binfo = TREE_PURPOSE (x);
431 if (binfo == NULL_TREE)
432 continue;
434 if (binfo == base_binfo)
436 if (warn_reorder)
438 if (pos < last_pos)
440 cp_warning_at ("base initializers for `%#T'", last_base);
441 cp_warning_at (" and `%#T'", BINFO_TYPE (binfo));
442 warning (" will be re-ordered to match inheritance order");
444 last_pos = pos;
445 last_base = BINFO_TYPE (binfo);
448 /* Make sure we won't try to work on this init again. */
449 TREE_PURPOSE (x) = NULL_TREE;
450 x = build_tree_list (binfo, TREE_VALUE (x));
451 goto got_it;
455 /* If we didn't find BASE_BINFO in the list, create a dummy entry
456 so the two lists (RBASES and the list of bases) will be
457 symmetrical. */
458 x = build_tree_list (NULL_TREE, NULL_TREE);
459 got_it:
460 rbases = chainon (rbases, x);
463 *rbase_ptr = rbases;
464 *vbase_ptr = vbases;
467 /* Perform whatever initializations have yet to be done on the base
468 class of the class variable. These actions are in the global
469 variable CURRENT_BASE_INIT_LIST. Such an action could be
470 NULL_TREE, meaning that the user has explicitly called the base
471 class constructor with no arguments.
473 If there is a need for a call to a constructor, we must surround
474 that call with a pushlevel/poplevel pair, since we are technically
475 at the PARM level of scope.
477 Argument IMMEDIATELY, if zero, forces a new sequence to be
478 generated to contain these new insns, so it can be emitted later.
479 This sequence is saved in the global variable BASE_INIT_EXPR.
480 Otherwise, the insns are emitted into the current sequence.
482 Note that emit_base_init does *not* initialize virtual base
483 classes. That is done specially, elsewhere. */
485 tree
486 emit_base_init (t)
487 tree t;
489 tree member;
490 tree mem_init_list;
491 tree rbase_init_list, vbase_init_list;
492 tree t_binfo = TYPE_BINFO (t);
493 tree binfos = BINFO_BASETYPES (t_binfo);
494 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
495 tree stmt_expr;
496 tree compound_stmt;
498 mem_init_list = sort_member_init (t);
499 current_member_init_list = NULL_TREE;
501 sort_base_init (t, &rbase_init_list, &vbase_init_list);
502 current_base_init_list = NULL_TREE;
504 begin_init_stmts (&stmt_expr, &compound_stmt);
506 /* First, initialize the virtual base classes, if we are
507 constructing the most-derived object. */
508 if (TYPE_USES_VIRTUAL_BASECLASSES (t))
510 tree first_arg = TREE_CHAIN (DECL_ARGUMENTS (current_function_decl));
511 construct_virtual_bases (t, current_class_ref, current_class_ptr,
512 vbase_init_list, first_arg);
515 /* Now, perform initialization of non-virtual base classes. */
516 for (i = 0; i < n_baseclasses; i++)
518 tree base_binfo = TREE_VEC_ELT (binfos, i);
519 tree init = void_list_node;
521 if (TREE_VIA_VIRTUAL (base_binfo))
522 continue;
524 my_friendly_assert (BINFO_INHERITANCE_CHAIN (base_binfo) == t_binfo,
525 999);
527 if (TREE_PURPOSE (rbase_init_list))
528 init = TREE_VALUE (rbase_init_list);
529 else if (TYPE_NEEDS_CONSTRUCTING (BINFO_TYPE (base_binfo)))
531 init = NULL_TREE;
532 if (extra_warnings && copy_args_p (current_function_decl))
533 cp_warning ("base class `%#T' should be explicitly initialized in the copy constructor",
534 BINFO_TYPE (base_binfo));
537 if (init != void_list_node)
539 member = convert_pointer_to_real (base_binfo, current_class_ptr);
540 expand_aggr_init_1 (base_binfo, NULL_TREE,
541 build_indirect_ref (member, NULL_PTR), init,
542 LOOKUP_NORMAL);
545 expand_cleanup_for_base (base_binfo, NULL_TREE);
546 rbase_init_list = TREE_CHAIN (rbase_init_list);
549 /* Initialize all the virtual function table fields that
550 do come from virtual base classes. */
551 if (TYPE_USES_VIRTUAL_BASECLASSES (t))
552 expand_indirect_vtbls_init (t_binfo, current_class_ref, current_class_ptr);
554 /* Initialize all the virtual function table fields that
555 do not come from virtual base classes. */
556 expand_direct_vtbls_init (t_binfo, t_binfo, 1, 1, current_class_ptr);
558 for (member = TYPE_FIELDS (t); member; member = TREE_CHAIN (member))
560 tree init, name;
561 int from_init_list;
563 /* member could be, for example, a CONST_DECL for an enumerated
564 tag; we don't want to try to initialize that, since it already
565 has a value. */
566 if (TREE_CODE (member) != FIELD_DECL || !DECL_NAME (member))
567 continue;
569 /* See if we had a user-specified member initialization. */
570 if (TREE_PURPOSE (mem_init_list))
572 name = TREE_PURPOSE (mem_init_list);
573 init = TREE_VALUE (mem_init_list);
574 from_init_list = 1;
576 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE
577 || TREE_CODE (name) == FIELD_DECL, 349);
579 else
581 name = DECL_NAME (member);
582 init = DECL_INITIAL (member);
584 from_init_list = 0;
586 /* Effective C++ rule 12. */
587 if (warn_ecpp && init == NULL_TREE
588 && !DECL_ARTIFICIAL (member)
589 && TREE_CODE (TREE_TYPE (member)) != ARRAY_TYPE)
590 cp_warning ("`%D' should be initialized in the member initialization list", member);
593 perform_member_init (member, name, init, from_init_list);
594 mem_init_list = TREE_CHAIN (mem_init_list);
597 /* Now initialize any members from our bases. */
598 while (mem_init_list)
600 tree name, init, field;
602 if (TREE_PURPOSE (mem_init_list))
604 name = TREE_PURPOSE (mem_init_list);
605 init = TREE_VALUE (mem_init_list);
607 if (TREE_CODE (name) == IDENTIFIER_NODE)
608 field = IDENTIFIER_CLASS_VALUE (name);
609 else
610 field = name;
612 /* If one member shadows another, get the outermost one. */
613 if (TREE_CODE (field) == TREE_LIST)
615 field = TREE_VALUE (field);
616 if (decl_type_context (field) != current_class_type)
617 cp_error ("field `%D' not in immediate context", field);
620 perform_member_init (field, name, init, 1);
622 mem_init_list = TREE_CHAIN (mem_init_list);
625 /* All the implicit try blocks we built up will be zapped
626 when we come to a real binding contour boundary. */
627 return finish_init_stmts (stmt_expr, compound_stmt);
630 /* Check that all fields are properly initialized after
631 an assignment to `this'. Called only when such an assignment
632 is actually noted. */
634 void
635 check_base_init (t)
636 tree t;
638 tree member;
639 for (member = TYPE_FIELDS (t); member; member = TREE_CHAIN (member))
640 if (DECL_NAME (member) && TREE_USED (member))
641 cp_error ("field `%D' used before initialized (after assignment to `this')",
642 member);
645 /* This code sets up the virtual function tables appropriate for
646 the pointer DECL. It is a one-ply initialization.
648 BINFO is the exact type that DECL is supposed to be. In
649 multiple inheritance, this might mean "C's A" if C : A, B. */
651 static void
652 expand_virtual_init (binfo, decl)
653 tree binfo, decl;
655 tree type = BINFO_TYPE (binfo);
656 tree vtbl, vtbl_ptr;
657 tree vtype, vtype_binfo;
659 /* This code is crusty. Should be simple, like:
660 vtbl = BINFO_VTABLE (binfo);
662 vtype = DECL_CONTEXT (TYPE_VFIELD (type));
663 vtype_binfo = get_binfo (vtype, TREE_TYPE (TREE_TYPE (decl)), 0);
664 vtbl = BINFO_VTABLE (binfo_value (DECL_FIELD_CONTEXT (TYPE_VFIELD (type)), binfo));
665 assemble_external (vtbl);
666 TREE_USED (vtbl) = 1;
667 vtbl = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (vtbl)), vtbl);
668 decl = convert_pointer_to_real (vtype_binfo, decl);
669 vtbl_ptr = build_vfield_ref (build_indirect_ref (decl, NULL_PTR), vtype);
670 if (vtbl_ptr == error_mark_node)
671 return;
673 /* Have to convert VTBL since array sizes may be different. */
674 vtbl = convert_force (TREE_TYPE (vtbl_ptr), vtbl, 0);
675 finish_expr_stmt (build_modify_expr (vtbl_ptr, NOP_EXPR, vtbl));
678 /* If an exception is thrown in a constructor, those base classes already
679 constructed must be destroyed. This function creates the cleanup
680 for BINFO, which has just been constructed. If FLAG is non-NULL,
681 it is a DECL which is non-zero when this base needs to be
682 destroyed. */
684 static void
685 expand_cleanup_for_base (binfo, flag)
686 tree binfo;
687 tree flag;
689 tree expr;
691 if (!TYPE_NEEDS_DESTRUCTOR (BINFO_TYPE (binfo)))
692 return;
694 /* Call the destructor. */
695 expr = (build_scoped_method_call
696 (current_class_ref, binfo, dtor_identifier,
697 build_expr_list (NULL_TREE, integer_zero_node)));
698 if (flag)
699 expr = fold (build (COND_EXPR, void_type_node,
700 truthvalue_conversion (flag),
701 expr, integer_zero_node));
703 finish_subobject (expr);
706 /* Subroutine of `expand_aggr_vbase_init'.
707 BINFO is the binfo of the type that is being initialized.
708 INIT_LIST is the list of initializers for the virtual baseclass. */
710 static void
711 expand_aggr_vbase_init_1 (binfo, exp, addr, init_list)
712 tree binfo, exp, addr, init_list;
714 tree init = purpose_member (binfo, init_list);
715 tree ref = build_indirect_ref (addr, NULL_PTR);
717 if (init)
718 init = TREE_VALUE (init);
719 /* Call constructors, but don't set up vtables. */
720 expand_aggr_init_1 (binfo, exp, ref, init, LOOKUP_COMPLAIN);
723 /* Construct the virtual base-classes of THIS_REF (whose address is
724 THIS_PTR). The object has the indicated TYPE. The construction
725 actually takes place only if FLAG is non-zero. INIT_LIST is list
726 of initialization for constructor to perform. */
728 static void
729 construct_virtual_bases (type, this_ref, this_ptr, init_list, flag)
730 tree type;
731 tree this_ref;
732 tree this_ptr;
733 tree init_list;
734 tree flag;
736 tree vbases;
737 tree result;
738 tree if_stmt;
740 /* If there are no virtual baseclasses, we shouldn't even be here. */
741 my_friendly_assert (TYPE_USES_VIRTUAL_BASECLASSES (type), 19990621);
743 /* First set the pointers in our object that tell us where to find
744 our virtual baseclasses. */
745 if_stmt = begin_if_stmt ();
746 finish_if_stmt_cond (flag, if_stmt);
747 result = init_vbase_pointers (type, this_ptr);
748 if (result)
749 finish_expr_stmt (build_compound_expr (result));
750 finish_then_clause (if_stmt);
751 finish_if_stmt ();
753 /* Now, run through the baseclasses, initializing each. */
754 for (vbases = CLASSTYPE_VBASECLASSES (type); vbases;
755 vbases = TREE_CHAIN (vbases))
757 tree tmp = purpose_member (vbases, result);
758 tree inner_if_stmt;
759 tree compound_stmt;
761 /* If there are virtual base classes with destructors, we need to
762 emit cleanups to destroy them if an exception is thrown during
763 the construction process. These exception regions (i.e., the
764 period during which the cleanups must occur) begin from the time
765 the construction is complete to the end of the function. If we
766 create a conditional block in which to initialize the
767 base-classes, then the cleanup region for the virtual base begins
768 inside a block, and ends outside of that block. This situation
769 confuses the sjlj exception-handling code. Therefore, we do not
770 create a single conditional block, but one for each
771 initialization. (That way the cleanup regions always begin
772 in the outer block.) We trust the back-end to figure out
773 that the FLAG will not change across initializations, and
774 avoid doing multiple tests. */
775 inner_if_stmt = begin_if_stmt ();
776 finish_if_stmt_cond (flag, inner_if_stmt);
777 compound_stmt = begin_compound_stmt (/*has_no_scope=*/1);
778 expand_aggr_vbase_init_1 (vbases, this_ref,
779 TREE_OPERAND (TREE_VALUE (tmp), 0),
780 init_list);
781 finish_compound_stmt (/*has_no_scope=*/1, compound_stmt);
782 finish_then_clause (inner_if_stmt);
783 finish_if_stmt ();
785 expand_cleanup_for_base (vbases, flag);
789 /* Find the context in which this FIELD can be initialized. */
791 static tree
792 initializing_context (field)
793 tree field;
795 tree t = DECL_CONTEXT (field);
797 /* Anonymous union members can be initialized in the first enclosing
798 non-anonymous union context. */
799 while (t && ANON_AGGR_TYPE_P (t))
800 t = TYPE_CONTEXT (t);
801 return t;
804 /* Function to give error message if member initialization specification
805 is erroneous. FIELD is the member we decided to initialize.
806 TYPE is the type for which the initialization is being performed.
807 FIELD must be a member of TYPE.
809 MEMBER_NAME is the name of the member. */
811 static int
812 member_init_ok_or_else (field, type, member_name)
813 tree field;
814 tree type;
815 const char *member_name;
817 if (field == error_mark_node)
818 return 0;
819 if (field == NULL_TREE || initializing_context (field) != type)
821 cp_error ("class `%T' does not have any field named `%s'", type,
822 member_name);
823 return 0;
825 if (TREE_STATIC (field))
827 cp_error ("field `%#D' is static; only point of initialization is its declaration",
828 field);
829 return 0;
832 return 1;
835 /* If NAME is a viable field name for the aggregate DECL,
836 and PARMS is a viable parameter list, then expand an _EXPR
837 which describes this initialization.
839 Note that we do not need to chase through the class's base classes
840 to look for NAME, because if it's in that list, it will be handled
841 by the constructor for that base class.
843 We do not yet have a fixed-point finder to instantiate types
844 being fed to overloaded constructors. If there is a unique
845 constructor, then argument types can be got from that one.
847 If INIT is non-NULL, then it the initialization should
848 be placed in `current_base_init_list', where it will be processed
849 by `emit_base_init'. */
851 void
852 expand_member_init (exp, name, init)
853 tree exp, name, init;
855 tree basetype = NULL_TREE, field;
856 tree type;
858 if (exp == NULL_TREE)
859 return; /* complain about this later */
861 type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
863 if (name && TREE_CODE (name) == TYPE_DECL)
865 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (name));
866 name = DECL_NAME (name);
869 if (name == NULL_TREE && IS_AGGR_TYPE (type))
870 switch (CLASSTYPE_N_BASECLASSES (type))
872 case 0:
873 error ("base class initializer specified, but no base class to initialize");
874 return;
875 case 1:
876 basetype = TYPE_BINFO_BASETYPE (type, 0);
877 break;
878 default:
879 error ("initializer for unnamed base class ambiguous");
880 cp_error ("(type `%T' uses multiple inheritance)", type);
881 return;
884 my_friendly_assert (init != NULL_TREE, 0);
886 /* The grammar should not allow fields which have names that are
887 TYPENAMEs. Therefore, if the field has a non-NULL TREE_TYPE, we
888 may assume that this is an attempt to initialize a base class
889 member of the current type. Otherwise, it is an attempt to
890 initialize a member field. */
892 if (init == void_type_node)
893 init = NULL_TREE;
895 if (name == NULL_TREE || basetype)
897 tree base_init;
899 if (name == NULL_TREE)
901 #if 0
902 if (basetype)
903 name = TYPE_IDENTIFIER (basetype);
904 else
906 error ("no base class to initialize");
907 return;
909 #endif
911 else if (basetype != type
912 && ! current_template_parms
913 && ! vec_binfo_member (basetype,
914 TYPE_BINFO_BASETYPES (type))
915 && ! binfo_member (basetype, CLASSTYPE_VBASECLASSES (type)))
917 if (IDENTIFIER_CLASS_VALUE (name))
918 goto try_member;
919 if (TYPE_USES_VIRTUAL_BASECLASSES (type))
920 cp_error ("type `%T' is not an immediate or virtual basetype for `%T'",
921 basetype, type);
922 else
923 cp_error ("type `%T' is not an immediate basetype for `%T'",
924 basetype, type);
925 return;
928 if (purpose_member (basetype, current_base_init_list))
930 cp_error ("base class `%T' already initialized", basetype);
931 return;
934 if (warn_reorder && current_member_init_list)
936 cp_warning ("base initializer for `%T'", basetype);
937 warning (" will be re-ordered to precede member initializations");
940 base_init = build_tree_list (basetype, init);
941 current_base_init_list = chainon (current_base_init_list, base_init);
943 else
945 tree member_init;
947 try_member:
948 field = lookup_field (type, name, 1, 0);
950 if (! member_init_ok_or_else (field, type, IDENTIFIER_POINTER (name)))
951 return;
953 if (purpose_member (name, current_member_init_list))
955 cp_error ("field `%D' already initialized", field);
956 return;
959 member_init = build_tree_list (name, init);
960 current_member_init_list = chainon (current_member_init_list, member_init);
964 /* We are about to generate some complex initialization code.
965 Conceptually, it is all a single expression. However, we may want
966 to include conditionals, loops, and other such statement-level
967 constructs. Therefore, we build the initialization code inside a
968 statement-expression. This function starts such an expression.
969 STMT_EXPR_P and COMPOUND_STMT_P are filled in by this function;
970 pass them back to finish_init_stmts when the expression is
971 complete. */
973 void
974 begin_init_stmts (stmt_expr_p, compound_stmt_p)
975 tree *stmt_expr_p;
976 tree *compound_stmt_p;
978 push_momentary ();
979 *stmt_expr_p = begin_stmt_expr ();
980 *compound_stmt_p = begin_compound_stmt (/*has_no_scope=*/1);
983 /* Finish out the statement-expression begun by the previous call to
984 begin_init_stmts. Returns the statement-expression itself. */
986 tree
987 finish_init_stmts (stmt_expr, compound_stmt)
988 tree stmt_expr;
989 tree compound_stmt;
991 pop_momentary ();
992 finish_compound_stmt (/*has_no_scope=*/1, compound_stmt);
993 stmt_expr = finish_stmt_expr (stmt_expr);
995 /* To avoid spurious warnings about unused values, we set
996 TREE_USED. */
997 if (stmt_expr)
998 TREE_USED (stmt_expr) = 1;
1000 return stmt_expr;
1003 /* This is like `expand_member_init', only it stores one aggregate
1004 value into another.
1006 INIT comes in two flavors: it is either a value which
1007 is to be stored in EXP, or it is a parameter list
1008 to go to a constructor, which will operate on EXP.
1009 If INIT is not a parameter list for a constructor, then set
1010 LOOKUP_ONLYCONVERTING.
1011 If FLAGS is LOOKUP_ONLYCONVERTING then it is the = init form of
1012 the initializer, if FLAGS is 0, then it is the (init) form.
1013 If `init' is a CONSTRUCTOR, then we emit a warning message,
1014 explaining that such initializations are invalid.
1016 ALIAS_THIS is nonzero iff we are initializing something which is
1017 essentially an alias for current_class_ref. In this case, the base
1018 constructor may move it on us, and we must keep track of such
1019 deviations.
1021 If INIT resolves to a CALL_EXPR which happens to return
1022 something of the type we are looking for, then we know
1023 that we can safely use that call to perform the
1024 initialization.
1026 The virtual function table pointer cannot be set up here, because
1027 we do not really know its type.
1029 Virtual baseclass pointers are also set up here.
1031 This never calls operator=().
1033 When initializing, nothing is CONST.
1035 A default copy constructor may have to be used to perform the
1036 initialization.
1038 A constructor or a conversion operator may have to be used to
1039 perform the initialization, but not both, as it would be ambiguous. */
1041 tree
1042 build_aggr_init (exp, init, flags)
1043 tree exp, init;
1044 int flags;
1046 tree stmt_expr;
1047 tree compound_stmt;
1048 int destroy_temps;
1049 tree type = TREE_TYPE (exp);
1050 int was_const = TREE_READONLY (exp);
1051 int was_volatile = TREE_THIS_VOLATILE (exp);
1053 if (init == error_mark_node)
1054 return error_mark_node;
1056 TREE_READONLY (exp) = 0;
1057 TREE_THIS_VOLATILE (exp) = 0;
1059 if (init && TREE_CODE (init) != TREE_LIST)
1060 flags |= LOOKUP_ONLYCONVERTING;
1062 if (TREE_CODE (type) == ARRAY_TYPE)
1064 /* Must arrange to initialize each element of EXP
1065 from elements of INIT. */
1066 tree itype = init ? TREE_TYPE (init) : NULL_TREE;
1067 if (CP_TYPE_QUALS (type) != TYPE_UNQUALIFIED)
1069 TREE_TYPE (exp) = TYPE_MAIN_VARIANT (type);
1070 if (init)
1071 TREE_TYPE (init) = TYPE_MAIN_VARIANT (itype);
1073 if (init && TREE_TYPE (init) == NULL_TREE)
1075 /* Handle bad initializers like:
1076 class COMPLEX {
1077 public:
1078 double re, im;
1079 COMPLEX(double r = 0.0, double i = 0.0) {re = r; im = i;};
1080 ~COMPLEX() {};
1083 int main(int argc, char **argv) {
1084 COMPLEX zees(1.0, 0.0)[10];
1087 error ("bad array initializer");
1088 return error_mark_node;
1090 stmt_expr = build_vec_init (exp, exp, array_type_nelts (type), init,
1091 init && same_type_p (TREE_TYPE (init),
1092 TREE_TYPE (exp)));
1093 TREE_READONLY (exp) = was_const;
1094 TREE_THIS_VOLATILE (exp) = was_volatile;
1095 TREE_TYPE (exp) = type;
1096 if (init)
1097 TREE_TYPE (init) = itype;
1098 return stmt_expr;
1101 if (TREE_CODE (exp) == VAR_DECL || TREE_CODE (exp) == PARM_DECL)
1102 /* just know that we've seen something for this node */
1103 TREE_USED (exp) = 1;
1105 TREE_TYPE (exp) = TYPE_MAIN_VARIANT (type);
1106 begin_init_stmts (&stmt_expr, &compound_stmt);
1107 destroy_temps = stmts_are_full_exprs_p;
1108 stmts_are_full_exprs_p = 0;
1109 expand_aggr_init_1 (TYPE_BINFO (type), exp, exp,
1110 init, LOOKUP_NORMAL|flags);
1111 stmt_expr = finish_init_stmts (stmt_expr, compound_stmt);
1112 stmts_are_full_exprs_p = destroy_temps;
1113 TREE_TYPE (exp) = type;
1114 TREE_READONLY (exp) = was_const;
1115 TREE_THIS_VOLATILE (exp) = was_volatile;
1117 return stmt_expr;
1120 static void
1121 expand_default_init (binfo, true_exp, exp, init, flags)
1122 tree binfo;
1123 tree true_exp, exp;
1124 tree init;
1125 int flags;
1127 tree type = TREE_TYPE (exp);
1129 /* It fails because there may not be a constructor which takes
1130 its own type as the first (or only parameter), but which does
1131 take other types via a conversion. So, if the thing initializing
1132 the expression is a unit element of type X, first try X(X&),
1133 followed by initialization by X. If neither of these work
1134 out, then look hard. */
1135 tree rval;
1136 tree parms;
1138 if (init && TREE_CODE (init) != TREE_LIST
1139 && (flags & LOOKUP_ONLYCONVERTING))
1141 /* Base subobjects should only get direct-initialization. */
1142 if (true_exp != exp)
1143 abort ();
1145 if (flags & DIRECT_BIND)
1146 /* Do nothing. We hit this in two cases: Reference initialization,
1147 where we aren't initializing a real variable, so we don't want
1148 to run a new constructor; and catching an exception, where we
1149 have already built up the constructor call so we could wrap it
1150 in an exception region. */;
1151 else if (TREE_CODE (init) == CONSTRUCTOR)
1152 /* A brace-enclosed initializer has whatever type is
1153 required. There's no need to convert it. */
1155 else
1156 init = ocp_convert (type, init, CONV_IMPLICIT|CONV_FORCE_TEMP, flags);
1158 if (TREE_CODE (init) == TRY_CATCH_EXPR)
1159 /* We need to protect the initialization of a catch parm
1160 with a call to terminate(), which shows up as a TRY_CATCH_EXPR
1161 around the TARGET_EXPR for the copy constructor. See
1162 expand_start_catch_block. */
1163 TREE_OPERAND (init, 0) = build (INIT_EXPR, TREE_TYPE (exp), exp,
1164 TREE_OPERAND (init, 0));
1165 else
1166 init = build (INIT_EXPR, TREE_TYPE (exp), exp, init);
1167 TREE_SIDE_EFFECTS (init) = 1;
1168 finish_expr_stmt (init);
1169 return;
1172 if (init == NULL_TREE
1173 || (TREE_CODE (init) == TREE_LIST && ! TREE_TYPE (init)))
1175 parms = init;
1176 if (parms)
1177 init = TREE_VALUE (parms);
1179 else
1180 parms = build_expr_list (NULL_TREE, init);
1182 if (TYPE_USES_VIRTUAL_BASECLASSES (type))
1184 if (true_exp == exp)
1185 parms = tree_cons (NULL_TREE, integer_one_node, parms);
1186 else
1187 parms = tree_cons (NULL_TREE, integer_zero_node, parms);
1188 flags |= LOOKUP_HAS_IN_CHARGE;
1191 rval = build_method_call (exp, ctor_identifier,
1192 parms, binfo, flags);
1193 if (TREE_SIDE_EFFECTS (rval))
1194 finish_expr_stmt (rval);
1197 /* This function is responsible for initializing EXP with INIT
1198 (if any).
1200 BINFO is the binfo of the type for who we are performing the
1201 initialization. For example, if W is a virtual base class of A and B,
1202 and C : A, B.
1203 If we are initializing B, then W must contain B's W vtable, whereas
1204 were we initializing C, W must contain C's W vtable.
1206 TRUE_EXP is nonzero if it is the true expression being initialized.
1207 In this case, it may be EXP, or may just contain EXP. The reason we
1208 need this is because if EXP is a base element of TRUE_EXP, we
1209 don't necessarily know by looking at EXP where its virtual
1210 baseclass fields should really be pointing. But we do know
1211 from TRUE_EXP. In constructors, we don't know anything about
1212 the value being initialized.
1214 ALIAS_THIS serves the same purpose it serves for expand_aggr_init.
1216 FLAGS is just passes to `build_method_call'. See that function for
1217 its description. */
1219 static void
1220 expand_aggr_init_1 (binfo, true_exp, exp, init, flags)
1221 tree binfo;
1222 tree true_exp, exp;
1223 tree init;
1224 int flags;
1226 tree type = TREE_TYPE (exp);
1228 my_friendly_assert (init != error_mark_node && type != error_mark_node, 211);
1230 /* Use a function returning the desired type to initialize EXP for us.
1231 If the function is a constructor, and its first argument is
1232 NULL_TREE, know that it was meant for us--just slide exp on
1233 in and expand the constructor. Constructors now come
1234 as TARGET_EXPRs. */
1236 if (init && TREE_CODE (exp) == VAR_DECL
1237 && TREE_CODE (init) == CONSTRUCTOR
1238 && TREE_HAS_CONSTRUCTOR (init))
1240 /* If store_init_value returns NULL_TREE, the INIT has been
1241 record in the DECL_INITIAL for EXP. That means there's
1242 nothing more we have to do. */
1243 if (!store_init_value (exp, init))
1245 if (!building_stmt_tree ())
1246 expand_decl_init (exp);
1248 else
1249 finish_expr_stmt (build (INIT_EXPR, type, exp, init));
1250 return;
1253 /* We know that expand_default_init can handle everything we want
1254 at this point. */
1255 expand_default_init (binfo, true_exp, exp, init, flags);
1258 /* Report an error if NAME is not the name of a user-defined,
1259 aggregate type. If OR_ELSE is nonzero, give an error message. */
1262 is_aggr_typedef (name, or_else)
1263 tree name;
1264 int or_else;
1266 tree type;
1268 if (name == error_mark_node)
1269 return 0;
1271 if (IDENTIFIER_HAS_TYPE_VALUE (name))
1272 type = IDENTIFIER_TYPE_VALUE (name);
1273 else
1275 if (or_else)
1276 cp_error ("`%T' is not an aggregate typedef", name);
1277 return 0;
1280 if (! IS_AGGR_TYPE (type)
1281 && TREE_CODE (type) != TEMPLATE_TYPE_PARM
1282 && TREE_CODE (type) != TEMPLATE_TEMPLATE_PARM)
1284 if (or_else)
1285 cp_error ("`%T' is not an aggregate type", type);
1286 return 0;
1288 return 1;
1291 /* Report an error if TYPE is not a user-defined, aggregate type. If
1292 OR_ELSE is nonzero, give an error message. */
1295 is_aggr_type (type, or_else)
1296 tree type;
1297 int or_else;
1299 if (type == error_mark_node)
1300 return 0;
1302 if (! IS_AGGR_TYPE (type)
1303 && TREE_CODE (type) != TEMPLATE_TYPE_PARM
1304 && TREE_CODE (type) != TEMPLATE_TEMPLATE_PARM)
1306 if (or_else)
1307 cp_error ("`%T' is not an aggregate type", type);
1308 return 0;
1310 return 1;
1313 /* Like is_aggr_typedef, but returns typedef if successful. */
1315 tree
1316 get_aggr_from_typedef (name, or_else)
1317 tree name;
1318 int or_else;
1320 tree type;
1322 if (name == error_mark_node)
1323 return NULL_TREE;
1325 if (IDENTIFIER_HAS_TYPE_VALUE (name))
1326 type = IDENTIFIER_TYPE_VALUE (name);
1327 else
1329 if (or_else)
1330 cp_error ("`%T' fails to be an aggregate typedef", name);
1331 return NULL_TREE;
1334 if (! IS_AGGR_TYPE (type)
1335 && TREE_CODE (type) != TEMPLATE_TYPE_PARM
1336 && TREE_CODE (type) != TEMPLATE_TEMPLATE_PARM)
1338 if (or_else)
1339 cp_error ("type `%T' is of non-aggregate type", type);
1340 return NULL_TREE;
1342 return type;
1345 tree
1346 get_type_value (name)
1347 tree name;
1349 if (name == error_mark_node)
1350 return NULL_TREE;
1352 if (IDENTIFIER_HAS_TYPE_VALUE (name))
1353 return IDENTIFIER_TYPE_VALUE (name);
1354 else
1355 return NULL_TREE;
1359 /* This code could just as well go in `class.c', but is placed here for
1360 modularity. */
1362 /* For an expression of the form TYPE :: NAME (PARMLIST), build
1363 the appropriate function call. */
1365 tree
1366 build_member_call (type, name, parmlist)
1367 tree type, name, parmlist;
1369 tree t;
1370 tree method_name;
1371 int dtor = 0;
1372 tree basetype_path, decl;
1374 if (TREE_CODE (name) == TEMPLATE_ID_EXPR
1375 && TREE_CODE (type) == NAMESPACE_DECL)
1377 /* 'name' already refers to the decls from the namespace, since we
1378 hit do_identifier for template_ids. */
1379 method_name = TREE_OPERAND (name, 0);
1380 /* FIXME: Since we don't do independent names right yet, the
1381 name might also be a LOOKUP_EXPR. Once we resolve this to a
1382 real decl earlier, this can go. This may happen during
1383 tsubst'ing. */
1384 if (TREE_CODE (method_name) == LOOKUP_EXPR)
1386 method_name = lookup_namespace_name
1387 (type, TREE_OPERAND (method_name, 0));
1388 TREE_OPERAND (name, 0) = method_name;
1390 my_friendly_assert (is_overloaded_fn (method_name), 980519);
1391 return build_x_function_call (name, parmlist, current_class_ref);
1394 if (type == std_node)
1395 return build_x_function_call (do_scoped_id (name, 0), parmlist,
1396 current_class_ref);
1397 if (TREE_CODE (type) == NAMESPACE_DECL)
1398 return build_x_function_call (lookup_namespace_name (type, name),
1399 parmlist, current_class_ref);
1401 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
1403 method_name = TREE_OPERAND (name, 0);
1404 if (TREE_CODE (method_name) == COMPONENT_REF)
1405 method_name = TREE_OPERAND (method_name, 1);
1406 if (is_overloaded_fn (method_name))
1407 method_name = DECL_NAME (OVL_CURRENT (method_name));
1408 TREE_OPERAND (name, 0) = method_name;
1410 else
1411 method_name = name;
1413 if (TREE_CODE (method_name) == BIT_NOT_EXPR)
1415 method_name = TREE_OPERAND (method_name, 0);
1416 dtor = 1;
1419 /* This shouldn't be here, and build_member_call shouldn't appear in
1420 parse.y! (mrs) */
1421 if (type && TREE_CODE (type) == IDENTIFIER_NODE
1422 && get_aggr_from_typedef (type, 0) == 0)
1424 tree ns = lookup_name (type, 0);
1425 if (ns && TREE_CODE (ns) == NAMESPACE_DECL)
1427 return build_x_function_call (build_offset_ref (type, name), parmlist, current_class_ref);
1431 if (type == NULL_TREE || ! is_aggr_type (type, 1))
1432 return error_mark_node;
1434 /* An operator we did not like. */
1435 if (name == NULL_TREE)
1436 return error_mark_node;
1438 if (dtor)
1440 cp_error ("cannot call destructor `%T::~%T' without object", type,
1441 method_name);
1442 return error_mark_node;
1445 decl = maybe_dummy_object (type, &basetype_path);
1447 /* Convert 'this' to the specified type to disambiguate conversion
1448 to the function's context. Apparently Standard C++ says that we
1449 shouldn't do this. */
1450 if (decl == current_class_ref
1451 && ! pedantic
1452 && ACCESSIBLY_UNIQUELY_DERIVED_P (type, current_class_type))
1454 tree olddecl = current_class_ptr;
1455 tree oldtype = TREE_TYPE (TREE_TYPE (olddecl));
1456 if (oldtype != type)
1458 tree newtype = build_qualified_type (type, TYPE_QUALS (oldtype));
1459 decl = convert_force (build_pointer_type (newtype), olddecl, 0);
1460 decl = build_indirect_ref (decl, NULL_PTR);
1464 if (method_name == constructor_name (type)
1465 || method_name == constructor_name_full (type))
1466 return build_functional_cast (type, parmlist);
1467 if (lookup_fnfields (basetype_path, method_name, 0))
1468 return build_method_call (decl,
1469 TREE_CODE (name) == TEMPLATE_ID_EXPR
1470 ? name : method_name,
1471 parmlist, basetype_path,
1472 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL);
1473 if (TREE_CODE (name) == IDENTIFIER_NODE
1474 && ((t = lookup_field (TYPE_BINFO (type), name, 1, 0))))
1476 if (t == error_mark_node)
1477 return error_mark_node;
1478 if (TREE_CODE (t) == FIELD_DECL)
1480 if (is_dummy_object (decl))
1482 cp_error ("invalid use of non-static field `%D'", t);
1483 return error_mark_node;
1485 decl = build (COMPONENT_REF, TREE_TYPE (t), decl, t);
1487 else if (TREE_CODE (t) == VAR_DECL)
1488 decl = t;
1489 else
1491 cp_error ("invalid use of member `%D'", t);
1492 return error_mark_node;
1494 if (TYPE_LANG_SPECIFIC (TREE_TYPE (decl)))
1495 return build_opfncall (CALL_EXPR, LOOKUP_NORMAL, decl,
1496 parmlist, NULL_TREE);
1497 return build_function_call (decl, parmlist);
1499 else
1501 cp_error ("no method `%T::%D'", type, name);
1502 return error_mark_node;
1506 /* Build a reference to a member of an aggregate. This is not a
1507 C++ `&', but really something which can have its address taken,
1508 and then act as a pointer to member, for example TYPE :: FIELD
1509 can have its address taken by saying & TYPE :: FIELD.
1511 @@ Prints out lousy diagnostics for operator <typename>
1512 @@ fields.
1514 @@ This function should be rewritten and placed in search.c. */
1516 tree
1517 build_offset_ref (type, name)
1518 tree type, name;
1520 tree decl, t = error_mark_node;
1521 tree member;
1522 tree basebinfo = NULL_TREE;
1523 tree orig_name = name;
1525 /* class templates can come in as TEMPLATE_DECLs here. */
1526 if (TREE_CODE (name) == TEMPLATE_DECL)
1527 return name;
1529 if (type == std_node)
1530 return do_scoped_id (name, 0);
1532 if (processing_template_decl || uses_template_parms (type))
1533 return build_min_nt (SCOPE_REF, type, name);
1535 /* Handle namespace names fully here. */
1536 if (TREE_CODE (type) == NAMESPACE_DECL)
1538 t = lookup_namespace_name (type, name);
1539 if (t != error_mark_node && ! type_unknown_p (t))
1541 mark_used (t);
1542 t = convert_from_reference (t);
1544 return t;
1547 if (type == NULL_TREE || ! is_aggr_type (type, 1))
1548 return error_mark_node;
1550 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
1552 /* If the NAME is a TEMPLATE_ID_EXPR, we are looking at
1553 something like `a.template f<int>' or the like. For the most
1554 part, we treat this just like a.f. We do remember, however,
1555 the template-id that was used. */
1556 name = TREE_OPERAND (orig_name, 0);
1558 if (TREE_CODE (name) == LOOKUP_EXPR)
1559 /* This can happen during tsubst'ing. */
1560 name = TREE_OPERAND (name, 0);
1562 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 0);
1565 if (TREE_CODE (name) == BIT_NOT_EXPR)
1567 if (! check_dtor_name (type, name))
1568 cp_error ("qualified type `%T' does not match destructor name `~%T'",
1569 type, TREE_OPERAND (name, 0));
1570 name = dtor_identifier;
1572 #if 0
1573 /* I think this is wrong, but the draft is unclear. --jason 6/15/98 */
1574 else if (name == constructor_name_full (type)
1575 || name == constructor_name (type))
1576 name = ctor_identifier;
1577 #endif
1579 if (TYPE_SIZE (complete_type (type)) == 0
1580 && !TYPE_BEING_DEFINED (type))
1582 cp_error ("incomplete type `%T' does not have member `%D'", type,
1583 name);
1584 return error_mark_node;
1587 decl = maybe_dummy_object (type, &basebinfo);
1589 member = lookup_member (basebinfo, name, 1, 0);
1591 if (member == error_mark_node)
1592 return error_mark_node;
1594 /* A lot of this logic is now handled in lookup_field and
1595 lookup_fnfield. */
1596 if (member && BASELINK_P (member))
1598 /* Go from the TREE_BASELINK to the member function info. */
1599 tree fnfields = member;
1600 t = TREE_VALUE (fnfields);
1602 if (TREE_CODE (orig_name) == TEMPLATE_ID_EXPR)
1604 /* The FNFIELDS are going to contain functions that aren't
1605 necessarily templates, and templates that don't
1606 necessarily match the explicit template parameters. We
1607 save all the functions, and the explicit parameters, and
1608 then figure out exactly what to instantiate with what
1609 arguments in instantiate_type. */
1611 if (TREE_CODE (t) != OVERLOAD)
1612 /* The code in instantiate_type which will process this
1613 expects to encounter OVERLOADs, not raw functions. */
1614 t = ovl_cons (t, NULL_TREE);
1616 return build (OFFSET_REF,
1617 unknown_type_node,
1618 decl,
1619 build (TEMPLATE_ID_EXPR,
1620 TREE_TYPE (t),
1622 TREE_OPERAND (orig_name, 1)));
1625 if (!really_overloaded_fn (t))
1627 /* Get rid of a potential OVERLOAD around it */
1628 t = OVL_CURRENT (t);
1630 /* unique functions are handled easily. */
1631 basebinfo = TREE_PURPOSE (fnfields);
1632 if (!enforce_access (basebinfo, t))
1633 return error_mark_node;
1634 mark_used (t);
1635 if (DECL_STATIC_FUNCTION_P (t))
1636 return t;
1637 return build (OFFSET_REF, TREE_TYPE (t), decl, t);
1640 TREE_TYPE (fnfields) = unknown_type_node;
1641 return build (OFFSET_REF, unknown_type_node, decl, fnfields);
1644 t = member;
1646 if (t == NULL_TREE)
1648 cp_error ("`%D' is not a member of type `%T'", name, type);
1649 return error_mark_node;
1652 if (TREE_CODE (t) == TYPE_DECL)
1654 TREE_USED (t) = 1;
1655 return t;
1657 /* static class members and class-specific enum
1658 values can be returned without further ado. */
1659 if (TREE_CODE (t) == VAR_DECL || TREE_CODE (t) == CONST_DECL)
1661 mark_used (t);
1662 return convert_from_reference (t);
1665 if (TREE_CODE (t) == FIELD_DECL && DECL_C_BIT_FIELD (t))
1667 cp_error ("illegal pointer to bit field `%D'", t);
1668 return error_mark_node;
1671 /* static class functions too. */
1672 if (TREE_CODE (t) == FUNCTION_DECL
1673 && TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
1674 my_friendly_abort (53);
1676 /* In member functions, the form `type::name' is no longer
1677 equivalent to `this->type::name', at least not until
1678 resolve_offset_ref. */
1679 return build (OFFSET_REF, build_offset_type (type, TREE_TYPE (t)), decl, t);
1682 /* If a OFFSET_REF made it through to here, then it did
1683 not have its address taken. */
1685 tree
1686 resolve_offset_ref (exp)
1687 tree exp;
1689 tree type = TREE_TYPE (exp);
1690 tree base = NULL_TREE;
1691 tree member;
1692 tree basetype, addr;
1694 if (TREE_CODE (exp) == OFFSET_REF)
1696 member = TREE_OPERAND (exp, 1);
1697 base = TREE_OPERAND (exp, 0);
1699 else
1701 my_friendly_assert (TREE_CODE (type) == OFFSET_TYPE, 214);
1702 if (TYPE_OFFSET_BASETYPE (type) != current_class_type)
1704 error ("object missing in use of pointer-to-member construct");
1705 return error_mark_node;
1707 member = exp;
1708 type = TREE_TYPE (type);
1709 base = current_class_ref;
1712 if (BASELINK_P (member))
1714 if (! flag_ms_extensions)
1715 cp_pedwarn ("assuming & on overloaded member function");
1716 return build_unary_op (ADDR_EXPR, exp, 0);
1719 if (TREE_CODE (TREE_TYPE (member)) == METHOD_TYPE)
1721 if (! flag_ms_extensions)
1722 cp_pedwarn ("assuming & on `%E'", member);
1723 return build_unary_op (ADDR_EXPR, exp, 0);
1726 if ((TREE_CODE (member) == VAR_DECL
1727 && ! TYPE_PTRMEMFUNC_P (TREE_TYPE (member))
1728 && ! TYPE_PTRMEM_P (TREE_TYPE (member)))
1729 || TREE_CODE (TREE_TYPE (member)) == FUNCTION_TYPE)
1731 /* These were static members. */
1732 if (mark_addressable (member) == 0)
1733 return error_mark_node;
1734 return member;
1737 if (TREE_CODE (TREE_TYPE (member)) == POINTER_TYPE
1738 && TREE_CODE (TREE_TYPE (TREE_TYPE (member))) == METHOD_TYPE)
1739 return member;
1741 /* Syntax error can cause a member which should
1742 have been seen as static to be grok'd as non-static. */
1743 if (TREE_CODE (member) == FIELD_DECL && current_class_ref == NULL_TREE)
1745 if (TREE_ADDRESSABLE (member) == 0)
1747 cp_error_at ("member `%D' is non-static but referenced as a static member",
1748 member);
1749 error ("at this point in file");
1750 TREE_ADDRESSABLE (member) = 1;
1752 return error_mark_node;
1755 /* The first case is really just a reference to a member of `this'. */
1756 if (TREE_CODE (member) == FIELD_DECL
1757 && (base == current_class_ref || is_dummy_object (base)))
1759 tree basetype_path;
1760 tree expr;
1762 if (TREE_CODE (exp) == OFFSET_REF && TREE_CODE (type) == OFFSET_TYPE)
1763 basetype = TYPE_OFFSET_BASETYPE (type);
1764 else
1765 basetype = DECL_CONTEXT (member);
1767 base = current_class_ptr;
1769 if (get_base_distance (basetype, TREE_TYPE (TREE_TYPE (base)), 0, &basetype_path) < 0)
1771 error_not_base_type (basetype, TREE_TYPE (TREE_TYPE (base)));
1772 return error_mark_node;
1774 /* Kludge: we need to use basetype_path now, because
1775 convert_pointer_to will bash it. */
1776 enforce_access (basetype_path, member);
1777 addr = convert_pointer_to (basetype, base);
1779 /* Even in the case of illegal access, we form the
1780 COMPONENT_REF; that will allow better error recovery than
1781 just feeding back error_mark_node. */
1782 expr = build (COMPONENT_REF, TREE_TYPE (member),
1783 build_indirect_ref (addr, NULL_PTR), member);
1784 return convert_from_reference (expr);
1787 /* Ensure that we have an object. */
1788 if (is_dummy_object (base))
1789 addr = error_mark_node;
1790 else
1791 /* If this is a reference to a member function, then return the
1792 address of the member function (which may involve going
1793 through the object's vtable), otherwise, return an expression
1794 for the dereferenced pointer-to-member construct. */
1795 addr = build_unary_op (ADDR_EXPR, base, 0);
1797 if (TYPE_PTRMEM_P (TREE_TYPE (member)))
1799 if (addr == error_mark_node)
1801 cp_error ("object missing in `%E'", exp);
1802 return error_mark_node;
1805 basetype = TYPE_OFFSET_BASETYPE (TREE_TYPE (TREE_TYPE (member)));
1806 addr = convert_pointer_to (basetype, addr);
1807 member = cp_convert (ptrdiff_type_node, member);
1809 /* Pointer to data members are offset by one, so that a null
1810 pointer with a real value of 0 is distinguishable from an
1811 offset of the first member of a structure. */
1812 member = build_binary_op (MINUS_EXPR, member,
1813 cp_convert (ptrdiff_type_node, integer_one_node));
1815 return build1 (INDIRECT_REF, type,
1816 build (PLUS_EXPR, build_pointer_type (type),
1817 addr, member));
1819 else if (TYPE_PTRMEMFUNC_P (TREE_TYPE (member)))
1821 return get_member_function_from_ptrfunc (&addr, member);
1823 my_friendly_abort (56);
1824 /* NOTREACHED */
1825 return NULL_TREE;
1828 /* Return either DECL or its known constant value (if it has one). */
1830 tree
1831 decl_constant_value (decl)
1832 tree decl;
1834 if (! TREE_THIS_VOLATILE (decl)
1835 && DECL_INITIAL (decl)
1836 && DECL_INITIAL (decl) != error_mark_node
1837 /* This is invalid if initial value is not constant.
1838 If it has either a function call, a memory reference,
1839 or a variable, then re-evaluating it could give different results. */
1840 && TREE_CONSTANT (DECL_INITIAL (decl))
1841 /* Check for cases where this is sub-optimal, even though valid. */
1842 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1843 return DECL_INITIAL (decl);
1844 return decl;
1847 /* Common subroutines of build_new and build_vec_delete. */
1849 /* Call the global __builtin_delete to delete ADDR. */
1851 static tree
1852 build_builtin_delete_call (addr)
1853 tree addr;
1855 mark_used (global_delete_fndecl);
1856 return build_call (global_delete_fndecl,
1857 void_type_node, build_expr_list (NULL_TREE, addr));
1860 /* Generate a C++ "new" expression. DECL is either a TREE_LIST
1861 (which needs to go through some sort of groktypename) or it
1862 is the name of the class we are newing. INIT is an initialization value.
1863 It is either an EXPRLIST, an EXPR_NO_COMMAS, or something in braces.
1864 If INIT is void_type_node, it means do *not* call a constructor
1865 for this instance.
1867 For types with constructors, the data returned is initialized
1868 by the appropriate constructor.
1870 Whether the type has a constructor or not, if it has a pointer
1871 to a virtual function table, then that pointer is set up
1872 here.
1874 Unless I am mistaken, a call to new () will return initialized
1875 data regardless of whether the constructor itself is private or
1876 not. NOPE; new fails if the constructor is private (jcm).
1878 Note that build_new does nothing to assure that any special
1879 alignment requirements of the type are met. Rather, it leaves
1880 it up to malloc to do the right thing. Otherwise, folding to
1881 the right alignment cal cause problems if the user tries to later
1882 free the memory returned by `new'.
1884 PLACEMENT is the `placement' list for user-defined operator new (). */
1886 extern int flag_check_new;
1888 tree
1889 build_new (placement, decl, init, use_global_new)
1890 tree placement;
1891 tree decl, init;
1892 int use_global_new;
1894 tree type, rval;
1895 tree nelts = NULL_TREE, t;
1896 int has_array = 0;
1898 if (decl == error_mark_node)
1899 return error_mark_node;
1901 if (TREE_CODE (decl) == TREE_LIST)
1903 tree absdcl = TREE_VALUE (decl);
1904 tree last_absdcl = NULL_TREE;
1906 if (current_function_decl
1907 && DECL_CONSTRUCTOR_P (current_function_decl))
1908 my_friendly_assert (immediate_size_expand == 0, 19990926);
1910 nelts = integer_one_node;
1912 if (absdcl && TREE_CODE (absdcl) == CALL_EXPR)
1913 my_friendly_abort (215);
1914 while (absdcl && TREE_CODE (absdcl) == INDIRECT_REF)
1916 last_absdcl = absdcl;
1917 absdcl = TREE_OPERAND (absdcl, 0);
1920 if (absdcl && TREE_CODE (absdcl) == ARRAY_REF)
1922 /* probably meant to be a vec new */
1923 tree this_nelts;
1925 while (TREE_OPERAND (absdcl, 0)
1926 && TREE_CODE (TREE_OPERAND (absdcl, 0)) == ARRAY_REF)
1928 last_absdcl = absdcl;
1929 absdcl = TREE_OPERAND (absdcl, 0);
1932 has_array = 1;
1933 this_nelts = TREE_OPERAND (absdcl, 1);
1934 if (this_nelts != error_mark_node)
1936 if (this_nelts == NULL_TREE)
1937 error ("new of array type fails to specify size");
1938 else if (processing_template_decl)
1940 nelts = this_nelts;
1941 absdcl = TREE_OPERAND (absdcl, 0);
1943 else
1945 int flags = pedantic ? WANT_INT : (WANT_INT | WANT_ENUM);
1946 if (build_expr_type_conversion (flags, this_nelts, 0)
1947 == NULL_TREE)
1948 pedwarn ("size in array new must have integral type");
1950 this_nelts = save_expr (cp_convert (sizetype, this_nelts));
1951 absdcl = TREE_OPERAND (absdcl, 0);
1952 if (this_nelts == integer_zero_node)
1954 warning ("zero size array reserves no space");
1955 nelts = integer_zero_node;
1957 else
1958 nelts = build_binary_op (MULT_EXPR, nelts, this_nelts);
1961 else
1962 nelts = integer_zero_node;
1965 if (last_absdcl)
1966 TREE_OPERAND (last_absdcl, 0) = absdcl;
1967 else
1968 TREE_VALUE (decl) = absdcl;
1970 type = groktypename (decl);
1971 if (! type || type == error_mark_node)
1972 return error_mark_node;
1974 else if (TREE_CODE (decl) == IDENTIFIER_NODE)
1976 if (IDENTIFIER_HAS_TYPE_VALUE (decl))
1978 /* An aggregate type. */
1979 type = IDENTIFIER_TYPE_VALUE (decl);
1980 decl = TYPE_MAIN_DECL (type);
1982 else
1984 /* A builtin type. */
1985 decl = lookup_name (decl, 1);
1986 my_friendly_assert (TREE_CODE (decl) == TYPE_DECL, 215);
1987 type = TREE_TYPE (decl);
1990 else if (TREE_CODE (decl) == TYPE_DECL)
1992 type = TREE_TYPE (decl);
1994 else
1996 type = decl;
1997 decl = TYPE_MAIN_DECL (type);
2000 if (processing_template_decl)
2002 if (has_array)
2003 t = min_tree_cons (min_tree_cons (NULL_TREE, type, NULL_TREE),
2004 build_min_nt (ARRAY_REF, NULL_TREE, nelts),
2005 NULL_TREE);
2006 else
2007 t = type;
2009 rval = build_min_nt (NEW_EXPR, placement, t, init);
2010 NEW_EXPR_USE_GLOBAL (rval) = use_global_new;
2011 return rval;
2014 /* ``A reference cannot be created by the new operator. A reference
2015 is not an object (8.2.2, 8.4.3), so a pointer to it could not be
2016 returned by new.'' ARM 5.3.3 */
2017 if (TREE_CODE (type) == REFERENCE_TYPE)
2019 error ("new cannot be applied to a reference type");
2020 type = TREE_TYPE (type);
2023 if (TREE_CODE (type) == FUNCTION_TYPE)
2025 error ("new cannot be applied to a function type");
2026 return error_mark_node;
2029 /* When the object being created is an array, the new-expression yields a
2030 pointer to the initial element (if any) of the array. For example,
2031 both new int and new int[10] return an int*. 5.3.4. */
2032 if (TREE_CODE (type) == ARRAY_TYPE && has_array == 0)
2034 nelts = array_type_nelts_top (type);
2035 has_array = 1;
2036 type = TREE_TYPE (type);
2039 if (has_array)
2040 t = build_nt (ARRAY_REF, type, nelts);
2041 else
2042 t = type;
2044 rval = build (NEW_EXPR, build_pointer_type (type), placement, t, init);
2045 NEW_EXPR_USE_GLOBAL (rval) = use_global_new;
2046 TREE_SIDE_EFFECTS (rval) = 1;
2047 rval = build_new_1 (rval);
2048 if (rval == error_mark_node)
2049 return error_mark_node;
2051 /* Wrap it in a NOP_EXPR so warn_if_unused_value doesn't complain. */
2052 rval = build1 (NOP_EXPR, TREE_TYPE (rval), rval);
2053 TREE_NO_UNUSED_WARNING (rval) = 1;
2055 return rval;
2058 /* Given a Java class, return a decl for the corresponding java.lang.Class. */
2060 static tree
2061 build_java_class_ref (type)
2062 tree type;
2064 tree name, class_decl;
2065 static tree CL_prefix = NULL_TREE;
2066 if (CL_prefix == NULL_TREE)
2067 CL_prefix = get_identifier("_CL_");
2068 if (jclass_node == NULL_TREE)
2070 jclass_node = IDENTIFIER_GLOBAL_VALUE (get_identifier("jclass"));
2071 if (jclass_node == NULL_TREE)
2072 fatal("call to Java constructor, while `jclass' undefined");
2073 jclass_node = TREE_TYPE (jclass_node);
2075 name = build_overload_with_type (CL_prefix, type);
2076 class_decl = IDENTIFIER_GLOBAL_VALUE (name);
2077 if (class_decl == NULL_TREE)
2079 push_permanent_obstack ();
2080 class_decl = build_decl (VAR_DECL, name, TREE_TYPE (jclass_node));
2081 TREE_STATIC (class_decl) = 1;
2082 DECL_EXTERNAL (class_decl) = 1;
2083 TREE_PUBLIC (class_decl) = 1;
2084 DECL_ARTIFICIAL (class_decl) = 1;
2085 DECL_IGNORED_P (class_decl) = 1;
2086 pushdecl_top_level (class_decl);
2087 make_decl_rtl (class_decl, NULL_PTR, 1);
2088 pop_obstacks ();
2090 return class_decl;
2093 /* Called from cplus_expand_expr when expanding a NEW_EXPR. The return
2094 value is immediately handed to expand_expr. */
2096 tree
2097 build_new_1 (exp)
2098 tree exp;
2100 tree placement, init;
2101 tree type, true_type, size, rval;
2102 tree nelts = NULL_TREE;
2103 tree alloc_expr, alloc_node = NULL_TREE;
2104 int has_array = 0;
2105 enum tree_code code = NEW_EXPR;
2106 int use_cookie, nothrow, check_new;
2107 int use_global_new;
2108 int use_java_new = 0;
2110 placement = TREE_OPERAND (exp, 0);
2111 type = TREE_OPERAND (exp, 1);
2112 init = TREE_OPERAND (exp, 2);
2113 use_global_new = NEW_EXPR_USE_GLOBAL (exp);
2115 if (TREE_CODE (type) == ARRAY_REF)
2117 has_array = 1;
2118 nelts = TREE_OPERAND (type, 1);
2119 type = TREE_OPERAND (type, 0);
2121 true_type = type;
2123 if (CP_TYPE_QUALS (type))
2124 type = TYPE_MAIN_VARIANT (type);
2126 /* If our base type is an array, then make sure we know how many elements
2127 it has. */
2128 while (TREE_CODE (true_type) == ARRAY_TYPE)
2130 tree this_nelts = array_type_nelts_top (true_type);
2131 nelts = build_binary_op (MULT_EXPR, nelts, this_nelts);
2132 true_type = TREE_TYPE (true_type);
2135 if (!complete_type_or_else (true_type, exp))
2136 return error_mark_node;
2138 if (has_array)
2139 size = fold (build_binary_op (MULT_EXPR, size_in_bytes (true_type),
2140 nelts));
2141 else
2142 size = size_in_bytes (type);
2144 if (TREE_CODE (true_type) == VOID_TYPE)
2146 error ("invalid type `void' for new");
2147 return error_mark_node;
2150 if (abstract_virtuals_error (NULL_TREE, true_type))
2151 return error_mark_node;
2153 /* When we allocate an array, and the corresponding deallocation
2154 function takes a second argument of type size_t, and that's the
2155 "usual deallocation function", we allocate some extra space at
2156 the beginning of the array to store the size of the array.
2158 Well, that's what we should do. For backwards compatibility, we
2159 have to do this whenever there's a two-argument array-delete
2160 operator.
2162 FIXME: For -fnew-abi, we don't have to maintain backwards
2163 compatibility and we should fix this. */
2164 use_cookie = (has_array && TYPE_VEC_NEW_USES_COOKIE (true_type)
2165 && ! (placement && ! TREE_CHAIN (placement)
2166 && TREE_TYPE (TREE_VALUE (placement)) == ptr_type_node));
2168 if (use_cookie)
2169 size = size_binop (PLUS_EXPR, size, BI_header_size);
2171 if (has_array)
2173 code = VEC_NEW_EXPR;
2175 if (init && pedantic)
2176 cp_pedwarn ("initialization in array new");
2179 /* Allocate the object. */
2181 if (! placement && TYPE_FOR_JAVA (true_type))
2183 tree class_addr, alloc_decl;
2184 tree class_decl = build_java_class_ref (true_type);
2185 tree class_size = size_in_bytes (true_type);
2186 static char alloc_name[] = "_Jv_AllocObject";
2187 use_java_new = 1;
2188 alloc_decl = IDENTIFIER_GLOBAL_VALUE (get_identifier (alloc_name));
2189 if (alloc_decl == NULL_TREE)
2190 fatal("call to Java constructor, while `%s' undefined", alloc_name);
2191 class_addr = build1 (ADDR_EXPR, jclass_node, class_decl);
2192 rval = build_function_call (alloc_decl,
2193 tree_cons (NULL_TREE, class_addr,
2194 build_tree_list (NULL_TREE,
2195 class_size)));
2196 rval = cp_convert (build_pointer_type (true_type), rval);
2198 else
2200 int susp = 0;
2202 if (flag_exceptions)
2203 /* We will use RVAL when generating an exception handler for
2204 this new-expression, so we must save it. */
2205 susp = suspend_momentary ();
2207 rval = build_op_new_call
2208 (code, true_type, tree_cons (NULL_TREE, size, placement),
2209 LOOKUP_NORMAL | (use_global_new * LOOKUP_GLOBAL));
2210 rval = cp_convert (build_pointer_type (true_type), rval);
2212 if (flag_exceptions)
2213 resume_momentary (susp);
2216 /* unless an allocation function is declared with an empty excep-
2217 tion-specification (_except.spec_), throw(), it indicates failure to
2218 allocate storage by throwing a bad_alloc exception (clause _except_,
2219 _lib.bad.alloc_); it returns a non-null pointer otherwise If the allo-
2220 cation function is declared with an empty exception-specification,
2221 throw(), it returns null to indicate failure to allocate storage and a
2222 non-null pointer otherwise.
2224 So check for a null exception spec on the op new we just called. */
2226 nothrow = 0;
2227 if (rval)
2229 /* The CALL_EXPR. */
2230 tree t = TREE_OPERAND (rval, 0);
2231 /* The function. */
2232 t = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
2233 nothrow = TYPE_NOTHROW_P (TREE_TYPE (t));
2235 check_new = (flag_check_new || nothrow) && ! use_java_new;
2237 if ((check_new || flag_exceptions) && rval)
2239 alloc_expr = get_target_expr (rval);
2240 alloc_node = rval = TREE_OPERAND (alloc_expr, 0);
2242 else
2243 alloc_expr = NULL_TREE;
2245 /* if rval is NULL_TREE I don't have to allocate it, but are we totally
2246 sure we have some extra bytes in that case for the BI_header_size
2247 cookies? And how does that interact with the code below? (mrs) */
2248 /* Finish up some magic for new'ed arrays */
2249 if (use_cookie && rval != NULL_TREE)
2251 tree extra = BI_header_size;
2252 tree cookie, exp1;
2253 rval = convert (string_type_node, rval); /* for ptr arithmetic */
2254 rval = save_expr (build_binary_op (PLUS_EXPR, rval, extra));
2255 /* Store header info. */
2256 cookie = build_indirect_ref (build (MINUS_EXPR,
2257 build_pointer_type (BI_header_type),
2258 rval, extra), NULL_PTR);
2259 exp1 = build (MODIFY_EXPR, void_type_node,
2260 build_component_ref (cookie, nelts_identifier,
2261 NULL_TREE, 0),
2262 nelts);
2263 rval = cp_convert (build_pointer_type (true_type), rval);
2264 rval = build_compound_expr
2265 (tree_cons (NULL_TREE, exp1,
2266 build_expr_list (NULL_TREE, rval)));
2269 if (rval == error_mark_node)
2270 return error_mark_node;
2272 /* Don't call any constructors or do any initialization. */
2273 if (init == void_type_node)
2274 goto done;
2276 if (TYPE_NEEDS_CONSTRUCTING (type) || init)
2278 if (! TYPE_NEEDS_CONSTRUCTING (type)
2279 && ! IS_AGGR_TYPE (type) && ! has_array)
2281 /* We are processing something like `new int (10)', which
2282 means allocate an int, and initialize it with 10. */
2283 tree deref;
2284 tree deref_type;
2286 /* At present RVAL is a temporary variable, created to hold
2287 the value from the call to `operator new'. We transform
2288 it to (*RVAL = INIT, RVAL). */
2289 rval = save_expr (rval);
2290 deref = build_indirect_ref (rval, NULL_PTR);
2292 /* Even for something like `new const int (10)' we must
2293 allow the expression to be non-const while we do the
2294 initialization. */
2295 deref_type = TREE_TYPE (deref);
2296 if (CP_TYPE_CONST_P (deref_type))
2297 TREE_TYPE (deref)
2298 = cp_build_qualified_type (deref_type,
2299 CP_TYPE_QUALS (deref_type)
2300 & ~TYPE_QUAL_CONST);
2301 TREE_READONLY (deref) = 0;
2303 if (TREE_CHAIN (init) != NULL_TREE)
2304 pedwarn ("initializer list being treated as compound expression");
2305 else if (TREE_CODE (init) == CONSTRUCTOR)
2307 pedwarn ("initializer list appears where operand should be used");
2308 init = TREE_OPERAND (init, 1);
2310 init = build_compound_expr (init);
2312 init = convert_for_initialization (deref, type, init, LOOKUP_NORMAL,
2313 "new", NULL_TREE, 0);
2314 rval = build (COMPOUND_EXPR, TREE_TYPE (rval),
2315 build_modify_expr (deref, NOP_EXPR, init),
2316 rval);
2317 TREE_NO_UNUSED_WARNING (rval) = 1;
2318 TREE_SIDE_EFFECTS (rval) = 1;
2320 else if (! has_array)
2322 tree newrval;
2323 /* Constructors are never virtual. If it has an initialization, we
2324 need to complain if we aren't allowed to use the ctor that took
2325 that argument. */
2326 int flags = LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_COMPLAIN;
2328 if (rval && TYPE_USES_VIRTUAL_BASECLASSES (true_type))
2330 init = tree_cons (NULL_TREE, integer_one_node, init);
2331 flags |= LOOKUP_HAS_IN_CHARGE;
2334 if (use_java_new)
2335 rval = save_expr (rval);
2336 newrval = rval;
2338 if (newrval && TREE_CODE (TREE_TYPE (newrval)) == POINTER_TYPE)
2339 newrval = build_indirect_ref (newrval, NULL_PTR);
2341 newrval = build_method_call (newrval, ctor_identifier,
2342 init, TYPE_BINFO (true_type), flags);
2344 if (newrval == NULL_TREE || newrval == error_mark_node)
2345 return error_mark_node;
2347 /* Java constructors compiled by jc1 do not return this. */
2348 if (use_java_new)
2349 newrval = build (COMPOUND_EXPR, TREE_TYPE (newrval),
2350 newrval, rval);
2351 rval = newrval;
2352 TREE_HAS_CONSTRUCTOR (rval) = 1;
2354 else
2355 rval = (build_vec_init
2356 (NULL_TREE,
2357 save_expr (rval),
2358 build_binary_op (MINUS_EXPR, nelts, integer_one_node),
2359 init,
2360 /*from_array=*/0));
2362 /* If any part of the object initialization terminates by throwing an
2363 exception and a suitable deallocation function can be found, the
2364 deallocation function is called to free the memory in which the
2365 object was being constructed, after which the exception continues
2366 to propagate in the context of the new-expression. If no
2367 unambiguous matching deallocation function can be found,
2368 propagating the exception does not cause the object's memory to be
2369 freed. */
2370 if (flag_exceptions && alloc_expr && ! use_java_new)
2372 enum tree_code dcode = has_array ? VEC_DELETE_EXPR : DELETE_EXPR;
2373 tree cleanup, fn = NULL_TREE;
2374 int flags = LOOKUP_NORMAL | (use_global_new * LOOKUP_GLOBAL);
2376 /* All cleanups must last longer than normal. */
2377 int yes = suspend_momentary ();
2379 /* The Standard is unclear here, but the right thing to do
2380 is to use the same method for finding deallocation
2381 functions that we use for finding allocation functions. */
2382 flags |= LOOKUP_SPECULATIVELY;
2384 /* We expect alloc_expr to look like a TARGET_EXPR around
2385 a NOP_EXPR around the CALL_EXPR we want. */
2386 fn = TREE_OPERAND (alloc_expr, 1);
2387 fn = TREE_OPERAND (fn, 0);
2389 cleanup = build_op_delete_call (dcode, alloc_node, size, flags, fn);
2391 resume_momentary (yes);
2393 /* Ack! First we allocate the memory. Then we set our sentry
2394 variable to true, and expand a cleanup that deletes the memory
2395 if sentry is true. Then we run the constructor and store the
2396 returned pointer in buf. Then we clear sentry and return buf. */
2398 if (cleanup)
2400 tree end, sentry, begin, buf, t = TREE_TYPE (rval);
2402 begin = get_target_expr (boolean_true_node);
2403 sentry = TREE_OPERAND (begin, 0);
2405 yes = suspend_momentary ();
2406 TREE_OPERAND (begin, 2)
2407 = build (COND_EXPR, void_type_node, sentry,
2408 cleanup, void_zero_node);
2409 resume_momentary (yes);
2411 rval = get_target_expr (rval);
2413 end = build (MODIFY_EXPR, TREE_TYPE (sentry),
2414 sentry, boolean_false_node);
2416 buf = TREE_OPERAND (rval, 0);
2418 rval = build (COMPOUND_EXPR, t, begin,
2419 build (COMPOUND_EXPR, t, rval,
2420 build (COMPOUND_EXPR, t, end, buf)));
2424 else if (CP_TYPE_CONST_P (true_type))
2425 cp_error ("uninitialized const in `new' of `%#T'", true_type);
2427 done:
2429 if (alloc_expr && rval == alloc_node)
2431 rval = TREE_OPERAND (alloc_expr, 1);
2432 alloc_expr = NULL_TREE;
2435 if (check_new && alloc_expr)
2437 /* Did we modify the storage? */
2438 tree ifexp = build_binary_op (NE_EXPR, alloc_node,
2439 integer_zero_node);
2440 rval = build_conditional_expr (ifexp, rval, alloc_node);
2443 if (alloc_expr)
2444 rval = build (COMPOUND_EXPR, TREE_TYPE (rval), alloc_expr, rval);
2446 if (rval && TREE_TYPE (rval) != build_pointer_type (type))
2448 /* The type of new int [3][3] is not int *, but int [3] * */
2449 rval = build_c_cast (build_pointer_type (type), rval);
2452 return rval;
2455 static tree
2456 build_vec_delete_1 (base, maxindex, type, auto_delete_vec, auto_delete,
2457 use_global_delete)
2458 tree base, maxindex, type;
2459 tree auto_delete_vec, auto_delete;
2460 int use_global_delete;
2462 tree virtual_size;
2463 tree ptype = build_pointer_type (type = complete_type (type));
2464 tree size_exp = size_in_bytes (type);
2466 /* Temporary variables used by the loop. */
2467 tree tbase, tbase_init;
2469 /* This is the body of the loop that implements the deletion of a
2470 single element, and moves temp variables to next elements. */
2471 tree body;
2473 /* This is the LOOP_EXPR that governs the deletion of the elements. */
2474 tree loop;
2476 /* This is the thing that governs what to do after the loop has run. */
2477 tree deallocate_expr = 0;
2479 /* This is the BIND_EXPR which holds the outermost iterator of the
2480 loop. It is convenient to set this variable up and test it before
2481 executing any other code in the loop.
2482 This is also the containing expression returned by this function. */
2483 tree controller = NULL_TREE;
2485 if (! IS_AGGR_TYPE (type) || ! TYPE_NEEDS_DESTRUCTOR (type))
2487 loop = integer_zero_node;
2488 goto no_destructor;
2491 /* The below is short by BI_header_size */
2492 virtual_size = fold (size_binop (MULT_EXPR, size_exp, maxindex));
2494 tbase = build_decl (VAR_DECL, NULL_TREE, ptype);
2495 tbase_init = build_modify_expr (tbase, NOP_EXPR,
2496 fold (build (PLUS_EXPR, ptype,
2497 base,
2498 virtual_size)));
2499 DECL_REGISTER (tbase) = 1;
2500 controller = build (BIND_EXPR, void_type_node, tbase, NULL_TREE, NULL_TREE);
2501 TREE_SIDE_EFFECTS (controller) = 1;
2503 if (auto_delete != integer_zero_node
2504 && auto_delete != integer_two_node)
2506 tree base_tbd = cp_convert (ptype,
2507 build_binary_op (MINUS_EXPR,
2508 cp_convert (ptr_type_node, base),
2509 BI_header_size));
2510 /* This is the real size */
2511 virtual_size = size_binop (PLUS_EXPR, virtual_size, BI_header_size);
2512 body = build_expr_list (NULL_TREE,
2513 build_x_delete (base_tbd,
2514 2 | use_global_delete,
2515 virtual_size));
2516 body = fold (build (COND_EXPR, void_type_node,
2517 fold (build (BIT_AND_EXPR, integer_type_node,
2518 auto_delete, integer_one_node)),
2519 body, integer_zero_node));
2521 else
2522 body = NULL_TREE;
2524 body = tree_cons (NULL_TREE,
2525 build_delete (ptype, tbase, auto_delete,
2526 LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 1),
2527 body);
2529 body = tree_cons (NULL_TREE,
2530 build_modify_expr (tbase, NOP_EXPR, build (MINUS_EXPR, ptype, tbase, size_exp)),
2531 body);
2533 body = tree_cons (NULL_TREE,
2534 build (EXIT_EXPR, void_type_node,
2535 build (EQ_EXPR, boolean_type_node, base, tbase)),
2536 body);
2538 loop = build (LOOP_EXPR, void_type_node, build_compound_expr (body));
2540 loop = tree_cons (NULL_TREE, tbase_init,
2541 tree_cons (NULL_TREE, loop, NULL_TREE));
2542 loop = build_compound_expr (loop);
2544 no_destructor:
2545 /* If the delete flag is one, or anything else with the low bit set,
2546 delete the storage. */
2547 if (auto_delete_vec == integer_zero_node)
2548 deallocate_expr = integer_zero_node;
2549 else
2551 tree base_tbd;
2553 /* The below is short by BI_header_size */
2554 virtual_size = fold (size_binop (MULT_EXPR, size_exp, maxindex));
2556 if (! TYPE_VEC_NEW_USES_COOKIE (type))
2557 /* no header */
2558 base_tbd = base;
2559 else
2561 base_tbd = cp_convert (ptype,
2562 build_binary_op (MINUS_EXPR,
2563 cp_convert (string_type_node, base),
2564 BI_header_size));
2565 /* True size with header. */
2566 virtual_size = size_binop (PLUS_EXPR, virtual_size, BI_header_size);
2568 deallocate_expr = build_x_delete (base_tbd,
2569 2 | use_global_delete,
2570 virtual_size);
2571 deallocate_expr = fold (build (COND_EXPR, void_type_node,
2572 fold (build (BIT_AND_EXPR,
2573 integer_type_node,
2574 auto_delete_vec,
2575 integer_one_node)),
2576 deallocate_expr, integer_zero_node));
2579 if (loop && deallocate_expr != integer_zero_node)
2581 body = tree_cons (NULL_TREE, loop,
2582 tree_cons (NULL_TREE, deallocate_expr, NULL_TREE));
2583 body = build_compound_expr (body);
2585 else
2586 body = loop;
2588 /* Outermost wrapper: If pointer is null, punt. */
2589 body = fold (build (COND_EXPR, void_type_node,
2590 fold (build (NE_EXPR, boolean_type_node, base,
2591 integer_zero_node)),
2592 body, integer_zero_node));
2593 body = build1 (NOP_EXPR, void_type_node, body);
2595 if (controller)
2597 TREE_OPERAND (controller, 1) = body;
2598 return controller;
2600 else
2601 return cp_convert (void_type_node, body);
2604 tree
2605 create_temporary_var (type)
2606 tree type;
2608 tree decl;
2610 decl = build_decl (VAR_DECL, NULL_TREE, type);
2611 TREE_USED (decl) = 1;
2612 DECL_ARTIFICIAL (decl) = 1;
2613 DECL_SOURCE_FILE (decl) = input_filename;
2614 DECL_SOURCE_LINE (decl) = lineno;
2615 DECL_IGNORED_P (decl) = 1;
2616 DECL_CONTEXT (decl) = current_function_decl;
2618 return decl;
2621 /* Create a new temporary variable of the indicated TYPE, initialized
2622 to INIT.
2624 It is not entered into current_binding_level, because that breaks
2625 things when it comes time to do final cleanups (which take place
2626 "outside" the binding contour of the function). */
2628 static tree
2629 get_temp_regvar (type, init)
2630 tree type, init;
2632 tree decl;
2634 decl = create_temporary_var (type);
2635 if (building_stmt_tree ())
2636 add_decl_stmt (decl);
2637 if (!building_stmt_tree ())
2638 DECL_RTL (decl) = assign_temp (type, 2, 0, 1);
2639 finish_expr_stmt (build_modify_expr (decl, INIT_EXPR, init));
2641 return decl;
2644 /* `build_vec_init' returns tree structure that performs
2645 initialization of a vector of aggregate types.
2647 DECL is passed only for error reporting, and provides line number
2648 and source file name information.
2649 BASE is the space where the vector will be. For a vector of Ts,
2650 the type of BASE is `T*'.
2651 MAXINDEX is the maximum index of the array (one less than the
2652 number of elements).
2653 INIT is the (possibly NULL) initializer.
2655 FROM_ARRAY is 0 if we should init everything with INIT
2656 (i.e., every element initialized from INIT).
2657 FROM_ARRAY is 1 if we should index into INIT in parallel
2658 with initialization of DECL.
2659 FROM_ARRAY is 2 if we should index into INIT in parallel,
2660 but use assignment instead of initialization. */
2662 tree
2663 build_vec_init (decl, base, maxindex, init, from_array)
2664 tree decl, base, maxindex, init;
2665 int from_array;
2667 tree rval;
2668 tree base2 = NULL_TREE;
2669 tree size;
2670 tree itype = NULL_TREE;
2671 tree iterator;
2672 /* The type of an element in the array. */
2673 tree type;
2674 /* The type of a pointer to an element in the array. */
2675 tree ptype;
2676 tree stmt_expr;
2677 tree compound_stmt;
2678 int destroy_temps;
2679 tree try_block = NULL_TREE;
2680 tree try_body;
2681 int num_initialized_elts = 0;
2683 maxindex = cp_convert (ptrdiff_type_node, maxindex);
2684 if (maxindex == error_mark_node)
2685 return error_mark_node;
2687 type = TREE_TYPE (TREE_TYPE (base));
2688 ptype = build_pointer_type (type);
2689 size = size_in_bytes (type);
2691 /* The code we are generating looks like:
2693 T* t1 = (T*) base;
2694 T* rval = base;
2695 ptrdiff_t iterator = maxindex;
2696 try {
2697 ... initializations from CONSTRUCTOR ...
2698 if (iterator != -1) {
2699 do {
2700 ... initialize *base ...
2701 ++base;
2702 } while (--iterator != -1);
2704 } catch (...) {
2705 ... destroy elements that were constructed ...
2708 We can omit the try and catch blocks if we know that the
2709 initialization will never throw an exception, or if the array
2710 elements do not have destructors. If we have a CONSTRUCTOR to
2711 give us initialization information, we emit code to initialize
2712 each of the elements before the loop in the try block, and then
2713 iterate over fewer elements. We can omit the loop completely if
2714 the elements of the array do not have constructors.
2716 We actually wrap the entire body of the above in a STMT_EXPR, for
2717 tidiness.
2719 When copying from array to another, when the array elements have
2720 only trivial copy constructors, we should use __builtin_memcpy
2721 rather than generating a loop. That way, we could take advantage
2722 of whatever cleverness the back-end has for dealing with copies
2723 of blocks of memory. */
2725 begin_init_stmts (&stmt_expr, &compound_stmt);
2726 destroy_temps = stmts_are_full_exprs_p;
2727 stmts_are_full_exprs_p = 0;
2728 rval = get_temp_regvar (ptype,
2729 cp_convert (ptype, default_conversion (base)));
2730 base = get_temp_regvar (ptype, rval);
2731 iterator = get_temp_regvar (ptrdiff_type_node, maxindex);
2733 /* Protect the entire array initialization so that we can destroy
2734 the partially constructed array if an exception is thrown. */
2735 if (flag_exceptions && TYPE_NEEDS_DESTRUCTOR (type))
2737 try_block = begin_try_block ();
2738 try_body = begin_compound_stmt (/*has_no_scope=*/1);
2741 if (init != NULL_TREE && TREE_CODE (init) == CONSTRUCTOR
2742 && (!decl || same_type_p (TREE_TYPE (init), TREE_TYPE (decl))))
2744 /* Do non-default initialization resulting from brace-enclosed
2745 initializers. */
2747 tree elts;
2748 from_array = 0;
2750 for (elts = CONSTRUCTOR_ELTS (init); elts; elts = TREE_CHAIN (elts))
2752 tree elt = TREE_VALUE (elts);
2753 tree baseref = build1 (INDIRECT_REF, type, base);
2755 num_initialized_elts++;
2757 if (IS_AGGR_TYPE (type) || TREE_CODE (type) == ARRAY_TYPE)
2758 finish_expr_stmt (build_aggr_init (baseref, elt, 0));
2759 else
2760 finish_expr_stmt (build_modify_expr (baseref, NOP_EXPR,
2761 elt));
2763 finish_expr_stmt (build_modify_expr
2764 (base,
2765 NOP_EXPR,
2766 build (PLUS_EXPR, build_pointer_type (type),
2767 base, size)));
2768 finish_expr_stmt (build_modify_expr
2769 (iterator,
2770 NOP_EXPR,
2771 build (MINUS_EXPR, ptrdiff_type_node,
2772 iterator, integer_one_node)));
2775 /* Clear out INIT so that we don't get confused below. */
2776 init = NULL_TREE;
2778 else if (from_array)
2780 /* If initializing one array from another, initialize element by
2781 element. We rely upon the below calls the do argument
2782 checking. */
2783 if (decl == NULL_TREE)
2785 sorry ("initialization of array from dissimilar array type");
2786 return error_mark_node;
2788 if (init)
2790 base2 = default_conversion (init);
2791 itype = TREE_TYPE (base2);
2792 base2 = get_temp_regvar (itype, base2);
2793 itype = TREE_TYPE (itype);
2795 else if (TYPE_LANG_SPECIFIC (type)
2796 && TYPE_NEEDS_CONSTRUCTING (type)
2797 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
2799 error ("initializer ends prematurely");
2800 return error_mark_node;
2804 /* Now, default-initialize any remaining elements. We don't need to
2805 do that if a) the type does not need constructing, or b) we've
2806 already initialized all the elements.
2808 We do need to keep going if we're copying an array. */
2810 if (from_array
2811 || (TYPE_NEEDS_CONSTRUCTING (type)
2812 && !(TREE_CODE (maxindex) == INTEGER_CST
2813 && num_initialized_elts == TREE_INT_CST_LOW (maxindex) + 1)))
2815 /* If the ITERATOR is equal to -1, then we don't have to loop;
2816 we've already initialized all the elements. */
2817 tree if_stmt;
2818 tree do_stmt;
2819 tree do_body;
2820 tree elt_init;
2822 if_stmt = begin_if_stmt ();
2823 finish_if_stmt_cond (build (NE_EXPR, boolean_type_node,
2824 iterator, minus_one_node),
2825 if_stmt);
2827 /* Otherwise, loop through the elements. */
2828 do_stmt = begin_do_stmt ();
2829 do_body = begin_compound_stmt (/*has_no_scope=*/1);
2831 /* When we're not building a statement-tree, things are a little
2832 complicated. If, when we recursively call build_aggr_init,
2833 an expression containing a TARGET_EXPR is expanded, then it
2834 may get a cleanup. Then, the result of that expression is
2835 passed to finish_expr_stmt, which will call
2836 expand_start_target_temps/expand_end_target_temps. However,
2837 the latter call will not cause the cleanup to run because
2838 that block will still be on the block stack. So, we call
2839 expand_start_target_temps here manually; the corresponding
2840 call to expand_end_target_temps below will cause the cleanup
2841 to be performed. */
2842 if (!building_stmt_tree ())
2843 expand_start_target_temps ();
2845 if (from_array)
2847 tree to = build1 (INDIRECT_REF, type, base);
2848 tree from;
2850 if (base2)
2851 from = build1 (INDIRECT_REF, itype, base2);
2852 else
2853 from = NULL_TREE;
2855 if (from_array == 2)
2856 elt_init = build_modify_expr (to, NOP_EXPR, from);
2857 else if (TYPE_NEEDS_CONSTRUCTING (type))
2858 elt_init = build_aggr_init (to, from, 0);
2859 else if (from)
2860 elt_init = build_modify_expr (to, NOP_EXPR, from);
2861 else
2862 my_friendly_abort (57);
2864 else if (TREE_CODE (type) == ARRAY_TYPE)
2866 if (init != 0)
2867 sorry ("cannot initialize multi-dimensional array with initializer");
2868 elt_init = (build_vec_init
2869 (decl,
2870 build1 (NOP_EXPR,
2871 build_pointer_type (TREE_TYPE (type)),
2872 base),
2873 array_type_nelts (type), 0, 0));
2875 else
2876 elt_init = build_aggr_init (build1 (INDIRECT_REF, type, base),
2877 init, 0);
2879 /* The initialization of each array element is a
2880 full-expression. */
2881 if (!building_stmt_tree ())
2883 finish_expr_stmt (elt_init);
2884 expand_end_target_temps ();
2886 else
2888 stmts_are_full_exprs_p = 1;
2889 finish_expr_stmt (elt_init);
2890 stmts_are_full_exprs_p = 0;
2893 finish_expr_stmt (build_modify_expr
2894 (base,
2895 NOP_EXPR,
2896 build (PLUS_EXPR, build_pointer_type (type),
2897 base, size)));
2898 if (base2)
2899 finish_expr_stmt (build_modify_expr
2900 (base2,
2901 NOP_EXPR,
2902 build (PLUS_EXPR, build_pointer_type (type),
2903 base2, size)));
2905 finish_compound_stmt (/*has_no_scope=*/1, do_body);
2906 finish_do_body (do_stmt);
2907 finish_do_stmt (build (NE_EXPR, boolean_type_node,
2908 build (PREDECREMENT_EXPR,
2909 ptrdiff_type_node,
2910 iterator,
2911 integer_one_node),
2912 minus_one_node),
2913 do_stmt);
2915 finish_then_clause (if_stmt);
2916 finish_if_stmt ();
2919 /* Make sure to cleanup any partially constructed elements. */
2920 if (flag_exceptions && TYPE_NEEDS_DESTRUCTOR (type))
2922 tree e;
2924 finish_compound_stmt (/*has_no_scope=*/1, try_body);
2925 finish_cleanup_try_block (try_block);
2926 e = build_vec_delete_1 (rval,
2927 build_binary_op (MINUS_EXPR, maxindex,
2928 iterator),
2929 type,
2930 /*auto_delete_vec=*/integer_zero_node,
2931 /*auto_delete=*/integer_zero_node,
2932 /*use_global_delete=*/0);
2933 finish_cleanup (e, try_block);
2936 /* The value of the array initialization is the address of the
2937 first element in the array. */
2938 finish_expr_stmt (rval);
2940 stmt_expr = finish_init_stmts (stmt_expr, compound_stmt);
2941 stmts_are_full_exprs_p = destroy_temps;
2942 return stmt_expr;
2945 /* Free up storage of type TYPE, at address ADDR.
2947 TYPE is a POINTER_TYPE and can be ptr_type_node for no special type
2948 of pointer.
2950 VIRTUAL_SIZE is the amount of storage that was allocated, and is
2951 used as the second argument to operator delete. It can include
2952 things like padding and magic size cookies. It has virtual in it,
2953 because if you have a base pointer and you delete through a virtual
2954 destructor, it should be the size of the dynamic object, not the
2955 static object, see Free Store 12.5 ANSI C++ WP.
2957 This does not call any destructors. */
2959 tree
2960 build_x_delete (addr, which_delete, virtual_size)
2961 tree addr;
2962 int which_delete;
2963 tree virtual_size;
2965 int use_global_delete = which_delete & 1;
2966 int use_vec_delete = !!(which_delete & 2);
2967 enum tree_code code = use_vec_delete ? VEC_DELETE_EXPR : DELETE_EXPR;
2968 int flags = LOOKUP_NORMAL | (use_global_delete * LOOKUP_GLOBAL);
2970 return build_op_delete_call (code, addr, virtual_size, flags, NULL_TREE);
2973 /* Generate a call to a destructor. TYPE is the type to cast ADDR to.
2974 ADDR is an expression which yields the store to be destroyed.
2975 AUTO_DELETE is nonzero if a call to DELETE should be made or not.
2976 If in the program, (AUTO_DELETE & 2) is non-zero, we tear down the
2977 virtual baseclasses.
2978 If in the program, (AUTO_DELETE & 1) is non-zero, then we deallocate.
2980 FLAGS is the logical disjunction of zero or more LOOKUP_
2981 flags. See cp-tree.h for more info.
2983 This function does not delete an object's virtual base classes. */
2985 tree
2986 build_delete (type, addr, auto_delete, flags, use_global_delete)
2987 tree type, addr;
2988 tree auto_delete;
2989 int flags;
2990 int use_global_delete;
2992 tree member;
2993 tree expr;
2994 tree ref;
2996 if (addr == error_mark_node)
2997 return error_mark_node;
2999 /* Can happen when CURRENT_EXCEPTION_OBJECT gets its type
3000 set to `error_mark_node' before it gets properly cleaned up. */
3001 if (type == error_mark_node)
3002 return error_mark_node;
3004 type = TYPE_MAIN_VARIANT (type);
3006 if (TREE_CODE (type) == POINTER_TYPE)
3008 type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
3009 if (type != void_type_node && !complete_type_or_else (type, addr))
3010 return error_mark_node;
3011 if (TREE_CODE (type) == ARRAY_TYPE)
3012 goto handle_array;
3013 if (! IS_AGGR_TYPE (type))
3015 /* Call the builtin operator delete. */
3016 return build_builtin_delete_call (addr);
3018 if (TREE_SIDE_EFFECTS (addr))
3019 addr = save_expr (addr);
3021 /* throw away const and volatile on target type of addr */
3022 addr = convert_force (build_pointer_type (type), addr, 0);
3023 ref = build_indirect_ref (addr, NULL_PTR);
3025 else if (TREE_CODE (type) == ARRAY_TYPE)
3027 handle_array:
3028 if (TREE_SIDE_EFFECTS (addr))
3029 addr = save_expr (addr);
3030 if (TYPE_DOMAIN (type) == NULL_TREE)
3032 error ("unknown array size in delete");
3033 return error_mark_node;
3035 return build_vec_delete (addr, array_type_nelts (type),
3036 auto_delete, integer_zero_node,
3037 use_global_delete);
3039 else
3041 /* Don't check PROTECT here; leave that decision to the
3042 destructor. If the destructor is accessible, call it,
3043 else report error. */
3044 addr = build_unary_op (ADDR_EXPR, addr, 0);
3045 if (TREE_SIDE_EFFECTS (addr))
3046 addr = save_expr (addr);
3048 if (TREE_CONSTANT (addr))
3049 addr = convert_pointer_to (type, addr);
3050 else
3051 addr = convert_force (build_pointer_type (type), addr, 0);
3053 ref = build_indirect_ref (addr, NULL_PTR);
3056 my_friendly_assert (IS_AGGR_TYPE (type), 220);
3058 if (! TYPE_NEEDS_DESTRUCTOR (type))
3060 if (auto_delete == integer_zero_node)
3061 return void_zero_node;
3063 return build_op_delete_call
3064 (DELETE_EXPR, addr, c_sizeof_nowarn (type),
3065 LOOKUP_NORMAL | (use_global_delete * LOOKUP_GLOBAL),
3066 NULL_TREE);
3069 /* Below, we will reverse the order in which these calls are made.
3070 If we have a destructor, then that destructor will take care
3071 of the base classes; otherwise, we must do that here. */
3072 if (TYPE_HAS_DESTRUCTOR (type))
3074 tree passed_auto_delete;
3075 tree do_delete = NULL_TREE;
3076 tree ifexp;
3078 if (use_global_delete)
3080 tree cond = fold (build (BIT_AND_EXPR, integer_type_node,
3081 auto_delete, integer_one_node));
3082 tree call = build_builtin_delete_call (addr);
3084 cond = fold (build (COND_EXPR, void_type_node, cond,
3085 call, void_zero_node));
3086 if (cond != void_zero_node)
3087 do_delete = cond;
3089 passed_auto_delete = fold (build (BIT_AND_EXPR, integer_type_node,
3090 auto_delete, integer_two_node));
3092 else
3093 passed_auto_delete = auto_delete;
3095 expr = build_method_call
3096 (ref, dtor_identifier, build_expr_list (NULL_TREE, passed_auto_delete),
3097 NULL_TREE, flags);
3099 if (do_delete)
3100 expr = build (COMPOUND_EXPR, void_type_node, expr, do_delete);
3102 if (flags & LOOKUP_DESTRUCTOR)
3103 /* Explicit destructor call; don't check for null pointer. */
3104 ifexp = integer_one_node;
3105 else
3106 /* Handle deleting a null pointer. */
3107 ifexp = fold (build_binary_op (NE_EXPR, addr, integer_zero_node));
3109 if (ifexp != integer_one_node)
3110 expr = build (COND_EXPR, void_type_node,
3111 ifexp, expr, void_zero_node);
3113 return expr;
3115 else
3117 /* We only get here from finish_function for a destructor. */
3118 tree binfos = BINFO_BASETYPES (TYPE_BINFO (type));
3119 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
3120 tree base_binfo = n_baseclasses > 0 ? TREE_VEC_ELT (binfos, 0) : NULL_TREE;
3121 tree exprstmt = NULL_TREE;
3122 tree parent_auto_delete = auto_delete;
3123 tree cond;
3125 /* Set this again before we call anything, as we might get called
3126 recursively. */
3127 TYPE_HAS_DESTRUCTOR (type) = 1;
3129 /* If we have member delete or vbases, we call delete in
3130 finish_function. */
3131 if (auto_delete == integer_zero_node)
3132 cond = NULL_TREE;
3133 else if (base_binfo == NULL_TREE
3134 || ! TYPE_NEEDS_DESTRUCTOR (BINFO_TYPE (base_binfo)))
3136 cond = build (COND_EXPR, void_type_node,
3137 build (BIT_AND_EXPR, integer_type_node, auto_delete, integer_one_node),
3138 build_builtin_delete_call (addr),
3139 void_zero_node);
3141 else
3142 cond = NULL_TREE;
3144 if (cond)
3145 exprstmt = build_expr_list (NULL_TREE, cond);
3147 if (base_binfo
3148 && ! TREE_VIA_VIRTUAL (base_binfo)
3149 && TYPE_NEEDS_DESTRUCTOR (BINFO_TYPE (base_binfo)))
3151 tree this_auto_delete;
3153 if (BINFO_OFFSET_ZEROP (base_binfo))
3154 this_auto_delete = parent_auto_delete;
3155 else
3156 this_auto_delete = integer_zero_node;
3158 expr = build_scoped_method_call
3159 (ref, base_binfo, dtor_identifier,
3160 build_expr_list (NULL_TREE, this_auto_delete));
3161 exprstmt = tree_cons (NULL_TREE, expr, exprstmt);
3164 /* Take care of the remaining baseclasses. */
3165 for (i = 1; i < n_baseclasses; i++)
3167 base_binfo = TREE_VEC_ELT (binfos, i);
3168 if (! TYPE_NEEDS_DESTRUCTOR (BINFO_TYPE (base_binfo))
3169 || TREE_VIA_VIRTUAL (base_binfo))
3170 continue;
3172 expr = build_scoped_method_call
3173 (ref, base_binfo, dtor_identifier,
3174 build_expr_list (NULL_TREE, integer_zero_node));
3176 exprstmt = tree_cons (NULL_TREE, expr, exprstmt);
3179 for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member))
3181 if (TREE_CODE (member) != FIELD_DECL)
3182 continue;
3183 if (TYPE_NEEDS_DESTRUCTOR (TREE_TYPE (member)))
3185 tree this_member = build_component_ref (ref, DECL_NAME (member), NULL_TREE, 0);
3186 tree this_type = TREE_TYPE (member);
3187 expr = build_delete (this_type, this_member, integer_two_node, flags, 0);
3188 exprstmt = tree_cons (NULL_TREE, expr, exprstmt);
3192 if (exprstmt)
3193 return build_compound_expr (exprstmt);
3194 /* Virtual base classes make this function do nothing. */
3195 return void_zero_node;
3199 /* For type TYPE, delete the virtual baseclass objects of DECL. */
3201 tree
3202 build_vbase_delete (type, decl)
3203 tree type, decl;
3205 tree vbases = CLASSTYPE_VBASECLASSES (type);
3206 tree result = NULL_TREE;
3207 tree addr = build_unary_op (ADDR_EXPR, decl, 0);
3209 my_friendly_assert (addr != error_mark_node, 222);
3211 while (vbases)
3213 tree this_addr = convert_force (build_pointer_type (BINFO_TYPE (vbases)),
3214 addr, 0);
3215 result = tree_cons (NULL_TREE,
3216 build_delete (TREE_TYPE (this_addr), this_addr,
3217 integer_zero_node,
3218 LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 0),
3219 result);
3220 vbases = TREE_CHAIN (vbases);
3222 return build_compound_expr (nreverse (result));
3225 /* Build a C++ vector delete expression.
3226 MAXINDEX is the number of elements to be deleted.
3227 ELT_SIZE is the nominal size of each element in the vector.
3228 BASE is the expression that should yield the store to be deleted.
3229 This function expands (or synthesizes) these calls itself.
3230 AUTO_DELETE_VEC says whether the container (vector) should be deallocated.
3231 AUTO_DELETE say whether each item in the container should be deallocated.
3233 This also calls delete for virtual baseclasses of elements of the vector.
3235 Update: MAXINDEX is no longer needed. The size can be extracted from the
3236 start of the vector for pointers, and from the type for arrays. We still
3237 use MAXINDEX for arrays because it happens to already have one of the
3238 values we'd have to extract. (We could use MAXINDEX with pointers to
3239 confirm the size, and trap if the numbers differ; not clear that it'd
3240 be worth bothering.) */
3242 tree
3243 build_vec_delete (base, maxindex, auto_delete_vec, auto_delete,
3244 use_global_delete)
3245 tree base, maxindex;
3246 tree auto_delete_vec, auto_delete;
3247 int use_global_delete;
3249 tree type;
3251 if (TREE_CODE (base) == OFFSET_REF)
3252 base = resolve_offset_ref (base);
3254 type = TREE_TYPE (base);
3256 base = stabilize_reference (base);
3258 /* Since we can use base many times, save_expr it. */
3259 if (TREE_SIDE_EFFECTS (base))
3260 base = save_expr (base);
3262 if (TREE_CODE (type) == POINTER_TYPE)
3264 /* Step back one from start of vector, and read dimension. */
3265 tree cookie_addr = build (MINUS_EXPR, build_pointer_type (BI_header_type),
3266 base, BI_header_size);
3267 tree cookie = build_indirect_ref (cookie_addr, NULL_PTR);
3268 maxindex = build_component_ref (cookie, nelts_identifier, NULL_TREE, 0);
3270 type = TREE_TYPE (type);
3271 while (TREE_CODE (type) == ARRAY_TYPE);
3273 else if (TREE_CODE (type) == ARRAY_TYPE)
3275 /* get the total number of things in the array, maxindex is a bad name */
3276 maxindex = array_type_nelts_total (type);
3277 while (TREE_CODE (type) == ARRAY_TYPE)
3278 type = TREE_TYPE (type);
3279 base = build_unary_op (ADDR_EXPR, base, 1);
3281 else
3283 if (base != error_mark_node)
3284 error ("type to vector delete is neither pointer or array type");
3285 return error_mark_node;
3288 return build_vec_delete_1 (base, maxindex, type, auto_delete_vec, auto_delete,
3289 use_global_delete);