2002-05-02 David S. Miller <davem@redhat.com>
[official-gcc.git] / gcc / cp / init.c
blob49ecf7cc4b334a33ba9bac99ea3eb38f140b04aa
1 /* Handle initialization things in C++.
2 Copyright (C) 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001 Free Software Foundation, Inc.
4 Contributed by Michael Tiemann (tiemann@cygnus.com)
6 This file is part of GNU CC.
8 GNU CC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
13 GNU CC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU CC; see the file COPYING. If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
23 /* High-level class interface. */
25 #include "config.h"
26 #include "system.h"
27 #include "tree.h"
28 #include "rtl.h"
29 #include "expr.h"
30 #include "cp-tree.h"
31 #include "flags.h"
32 #include "output.h"
33 #include "except.h"
34 #include "toplev.h"
35 #include "ggc.h"
37 static void expand_aggr_vbase_init_1 PARAMS ((tree, tree, tree, tree));
38 static void construct_virtual_bases PARAMS ((tree, tree, tree, tree, tree));
39 static void expand_aggr_init_1 PARAMS ((tree, tree, tree, tree, int));
40 static void expand_default_init PARAMS ((tree, tree, tree, tree, int));
41 static tree build_vec_delete_1 PARAMS ((tree, tree, tree, special_function_kind, int));
42 static void perform_member_init PARAMS ((tree, tree, int));
43 static void sort_base_init PARAMS ((tree, tree, tree *, tree *));
44 static tree build_builtin_delete_call PARAMS ((tree));
45 static int member_init_ok_or_else PARAMS ((tree, tree, tree));
46 static void expand_virtual_init PARAMS ((tree, tree));
47 static tree sort_member_init PARAMS ((tree, tree));
48 static tree initializing_context PARAMS ((tree));
49 static void expand_cleanup_for_base PARAMS ((tree, tree));
50 static tree get_temp_regvar PARAMS ((tree, tree));
51 static tree dfs_initialize_vtbl_ptrs PARAMS ((tree, void *));
52 static tree build_default_init PARAMS ((tree));
53 static tree build_new_1 PARAMS ((tree));
54 static tree get_cookie_size PARAMS ((tree));
55 static tree build_dtor_call PARAMS ((tree, special_function_kind, int));
56 static tree build_field_list PARAMS ((tree, tree, int *));
57 static tree build_vtbl_address PARAMS ((tree));
59 /* We are about to generate some complex initialization code.
60 Conceptually, it is all a single expression. However, we may want
61 to include conditionals, loops, and other such statement-level
62 constructs. Therefore, we build the initialization code inside a
63 statement-expression. This function starts such an expression.
64 STMT_EXPR_P and COMPOUND_STMT_P are filled in by this function;
65 pass them back to finish_init_stmts when the expression is
66 complete. */
68 void
69 begin_init_stmts (stmt_expr_p, compound_stmt_p)
70 tree *stmt_expr_p;
71 tree *compound_stmt_p;
73 if (building_stmt_tree ())
74 *stmt_expr_p = begin_stmt_expr ();
75 else
76 *stmt_expr_p = begin_global_stmt_expr ();
78 if (building_stmt_tree ())
79 *compound_stmt_p = begin_compound_stmt (/*has_no_scope=*/1);
82 /* Finish out the statement-expression begun by the previous call to
83 begin_init_stmts. Returns the statement-expression itself. */
85 tree
86 finish_init_stmts (stmt_expr, compound_stmt)
87 tree stmt_expr;
88 tree compound_stmt;
91 if (building_stmt_tree ())
92 finish_compound_stmt (/*has_no_scope=*/1, compound_stmt);
94 if (building_stmt_tree ())
96 stmt_expr = finish_stmt_expr (stmt_expr);
97 STMT_EXPR_NO_SCOPE (stmt_expr) = true;
99 else
100 stmt_expr = finish_global_stmt_expr (stmt_expr);
102 /* To avoid spurious warnings about unused values, we set
103 TREE_USED. */
104 if (stmt_expr)
105 TREE_USED (stmt_expr) = 1;
107 return stmt_expr;
110 /* Constructors */
112 /* Called from initialize_vtbl_ptrs via dfs_walk. BINFO is the base
113 which we want to initialize the vtable pointer for, DATA is
114 TREE_LIST whose TREE_VALUE is the this ptr expression. */
116 static tree
117 dfs_initialize_vtbl_ptrs (binfo, data)
118 tree binfo;
119 void *data;
121 if ((!BINFO_PRIMARY_P (binfo) || TREE_VIA_VIRTUAL (binfo))
122 && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
124 tree base_ptr = TREE_VALUE ((tree) data);
126 base_ptr = build_base_path (PLUS_EXPR, base_ptr, binfo, /*nonnull=*/1);
128 expand_virtual_init (binfo, base_ptr);
131 SET_BINFO_MARKED (binfo);
133 return NULL_TREE;
136 /* Initialize all the vtable pointers in the object pointed to by
137 ADDR. */
139 void
140 initialize_vtbl_ptrs (addr)
141 tree addr;
143 tree list;
144 tree type;
146 type = TREE_TYPE (TREE_TYPE (addr));
147 list = build_tree_list (type, addr);
149 /* Walk through the hierarchy, initializing the vptr in each base
150 class. We do these in pre-order because can't find the virtual
151 bases for a class until we've initialized the vtbl for that
152 class. */
153 dfs_walk_real (TYPE_BINFO (type), dfs_initialize_vtbl_ptrs,
154 NULL, dfs_unmarked_real_bases_queue_p, list);
155 dfs_walk (TYPE_BINFO (type), dfs_unmark,
156 dfs_marked_real_bases_queue_p, type);
159 /* [dcl.init]:
161 To default-initialize an object of type T means:
163 --if T is a non-POD class type (clause _class_), the default construc-
164 tor for T is called (and the initialization is ill-formed if T has
165 no accessible default constructor);
167 --if T is an array type, each element is default-initialized;
169 --otherwise, the storage for the object is zero-initialized.
171 A program that calls for default-initialization of an entity of refer-
172 ence type is ill-formed. */
174 static tree
175 build_default_init (type)
176 tree type;
178 tree init = NULL_TREE;
180 if (TYPE_NEEDS_CONSTRUCTING (type))
181 /* Other code will handle running the default constructor. We can't do
182 anything with a CONSTRUCTOR for arrays here, as that would imply
183 copy-initialization. */
184 return NULL_TREE;
185 else if (AGGREGATE_TYPE_P (type) && !TYPE_PTRMEMFUNC_P (type))
187 /* This is a default initialization of an aggregate, but not one of
188 non-POD class type. We cleverly notice that the initialization
189 rules in such a case are the same as for initialization with an
190 empty brace-initialization list. */
191 init = build (CONSTRUCTOR, NULL_TREE, NULL_TREE, NULL_TREE);
193 else if (TREE_CODE (type) == REFERENCE_TYPE)
194 /* --if T is a reference type, no initialization is performed. */
195 return NULL_TREE;
196 else
198 init = integer_zero_node;
200 if (TREE_CODE (type) == ENUMERAL_TYPE)
201 /* We must make enumeral types the right type. */
202 init = fold (build1 (NOP_EXPR, type, init));
205 init = digest_init (type, init, 0);
206 return init;
209 /* Subroutine of emit_base_init. */
211 static void
212 perform_member_init (member, init, explicit)
213 tree member, init;
214 int explicit;
216 tree decl;
217 tree type = TREE_TYPE (member);
219 decl = build_component_ref (current_class_ref, member, NULL_TREE, explicit);
221 if (decl == error_mark_node)
222 return;
224 /* Deal with this here, as we will get confused if we try to call the
225 assignment op for an anonymous union. This can happen in a
226 synthesized copy constructor. */
227 if (ANON_AGGR_TYPE_P (type))
229 if (init)
231 init = build (INIT_EXPR, type, decl, TREE_VALUE (init));
232 finish_expr_stmt (init);
235 else if (TYPE_NEEDS_CONSTRUCTING (type)
236 || (init && TYPE_HAS_CONSTRUCTOR (type)))
238 /* Since `init' is already a TREE_LIST on the member_init_list,
239 only build it into one if we aren't already a list. */
240 if (init != NULL_TREE && TREE_CODE (init) != TREE_LIST)
241 init = build_tree_list (NULL_TREE, init);
243 if (explicit
244 && TREE_CODE (type) == ARRAY_TYPE
245 && init != NULL_TREE
246 && TREE_CHAIN (init) == NULL_TREE
247 && TREE_CODE (TREE_TYPE (TREE_VALUE (init))) == ARRAY_TYPE)
249 /* Initialization of one array from another. */
250 finish_expr_stmt (build_vec_init (decl, TREE_VALUE (init), 1));
252 else
253 finish_expr_stmt (build_aggr_init (decl, init, 0));
255 else
257 if (init == NULL_TREE)
259 if (explicit)
261 init = build_default_init (type);
262 if (TREE_CODE (type) == REFERENCE_TYPE)
263 warning
264 ("default-initialization of `%#D', which has reference type",
265 member);
267 /* member traversal: note it leaves init NULL */
268 else if (TREE_CODE (type) == REFERENCE_TYPE)
269 pedwarn ("uninitialized reference member `%D'", member);
271 else if (TREE_CODE (init) == TREE_LIST)
273 /* There was an explicit member initialization. Do some
274 work in that case. */
275 if (TREE_CHAIN (init))
277 warning ("initializer list treated as compound expression");
278 init = build_compound_expr (init);
280 else
281 init = TREE_VALUE (init);
284 if (init)
285 finish_expr_stmt (build_modify_expr (decl, INIT_EXPR, init));
288 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
290 tree expr;
292 expr = build_component_ref (current_class_ref, member, NULL_TREE,
293 explicit);
294 expr = build_delete (type, expr, sfk_complete_destructor,
295 LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR, 0);
297 if (expr != error_mark_node)
298 finish_eh_cleanup (expr);
302 /* Returns a TREE_LIST containing (as the TREE_PURPOSE of each node) all
303 the FIELD_DECLs on the TYPE_FIELDS list for T, in reverse order. */
305 static tree
306 build_field_list (t, list, uses_unions_p)
307 tree t;
308 tree list;
309 int *uses_unions_p;
311 tree fields;
313 /* Note whether or not T is a union. */
314 if (TREE_CODE (t) == UNION_TYPE)
315 *uses_unions_p = 1;
317 for (fields = TYPE_FIELDS (t); fields; fields = TREE_CHAIN (fields))
319 /* Skip CONST_DECLs for enumeration constants and so forth. */
320 if (TREE_CODE (fields) != FIELD_DECL)
321 continue;
323 /* Keep track of whether or not any fields are unions. */
324 if (TREE_CODE (TREE_TYPE (fields)) == UNION_TYPE)
325 *uses_unions_p = 1;
327 /* For an anonymous struct or union, we must recursively
328 consider the fields of the anonymous type. They can be
329 directly initialized from the constructor. */
330 if (ANON_AGGR_TYPE_P (TREE_TYPE (fields)))
332 /* Add this field itself. Synthesized copy constructors
333 initialize the entire aggregate. */
334 list = tree_cons (fields, NULL_TREE, list);
335 /* And now add the fields in the anonymous aggregate. */
336 list = build_field_list (TREE_TYPE (fields), list,
337 uses_unions_p);
339 /* Add this field. */
340 else if (DECL_NAME (fields))
341 list = tree_cons (fields, NULL_TREE, list);
344 return list;
347 /* The MEMBER_INIT_LIST is a TREE_LIST. The TREE_PURPOSE of each list
348 gives a FIELD_DECL in T that needs initialization. The TREE_VALUE
349 gives the initializer, or list of initializer arguments. Sort the
350 MEMBER_INIT_LIST, returning a version that contains the same
351 information but in the order that the fields should actually be
352 initialized. Perform error-checking in the process. */
354 static tree
355 sort_member_init (t, member_init_list)
356 tree t;
357 tree member_init_list;
359 tree init_list;
360 tree last_field;
361 tree init;
362 int uses_unions_p;
364 /* Build up a list of the various fields, in sorted order. */
365 init_list = nreverse (build_field_list (t, NULL_TREE, &uses_unions_p));
367 /* Go through the explicit initializers, adding them to the
368 INIT_LIST. */
369 last_field = init_list;
370 for (init = member_init_list; init; init = TREE_CHAIN (init))
372 tree f;
373 tree initialized_field;
375 initialized_field = TREE_PURPOSE (init);
376 my_friendly_assert (TREE_CODE (initialized_field) == FIELD_DECL,
377 20000516);
379 /* If the explicit initializers are in sorted order, then the
380 INITIALIZED_FIELD will be for a field following the
381 LAST_FIELD. */
382 for (f = last_field; f; f = TREE_CHAIN (f))
383 if (TREE_PURPOSE (f) == initialized_field)
384 break;
386 /* Give a warning, if appropriate. */
387 if (warn_reorder && !f)
389 cp_warning_at ("member initializers for `%#D'",
390 TREE_PURPOSE (last_field));
391 cp_warning_at (" and `%#D'", initialized_field);
392 warning (" will be re-ordered to match declaration order");
395 /* Look again, from the beginning of the list. We must find the
396 field on this loop. */
397 if (!f)
399 f = init_list;
400 while (TREE_PURPOSE (f) != initialized_field)
401 f = TREE_CHAIN (f);
404 /* If there was already an explicit initializer for this field,
405 issue an error. */
406 if (TREE_TYPE (f))
407 error ("multiple initializations given for member `%D'",
408 initialized_field);
409 else
411 /* Mark the field as explicitly initialized. */
412 TREE_TYPE (f) = error_mark_node;
413 /* And insert the initializer. */
414 TREE_VALUE (f) = TREE_VALUE (init);
417 /* Remember the location of the last explicitly initialized
418 field. */
419 last_field = f;
422 /* [class.base.init]
424 If a ctor-initializer specifies more than one mem-initializer for
425 multiple members of the same union (including members of
426 anonymous unions), the ctor-initializer is ill-formed. */
427 if (uses_unions_p)
429 last_field = NULL_TREE;
430 for (init = init_list; init; init = TREE_CHAIN (init))
432 tree field;
433 tree field_type;
434 int done;
436 /* Skip uninitialized members. */
437 if (!TREE_TYPE (init))
438 continue;
439 /* See if this field is a member of a union, or a member of a
440 structure contained in a union, etc. */
441 field = TREE_PURPOSE (init);
442 for (field_type = DECL_CONTEXT (field);
443 !same_type_p (field_type, t);
444 field_type = TYPE_CONTEXT (field_type))
445 if (TREE_CODE (field_type) == UNION_TYPE)
446 break;
447 /* If this field is not a member of a union, skip it. */
448 if (TREE_CODE (field_type) != UNION_TYPE)
449 continue;
451 /* It's only an error if we have two initializers for the same
452 union type. */
453 if (!last_field)
455 last_field = field;
456 continue;
459 /* See if LAST_FIELD and the field initialized by INIT are
460 members of the same union. If so, there's a problem,
461 unless they're actually members of the same structure
462 which is itself a member of a union. For example, given:
464 union { struct { int i; int j; }; };
466 initializing both `i' and `j' makes sense. */
467 field_type = DECL_CONTEXT (field);
468 done = 0;
471 tree last_field_type;
473 last_field_type = DECL_CONTEXT (last_field);
474 while (1)
476 if (same_type_p (last_field_type, field_type))
478 if (TREE_CODE (field_type) == UNION_TYPE)
479 error ("initializations for multiple members of `%T'",
480 last_field_type);
481 done = 1;
482 break;
485 if (same_type_p (last_field_type, t))
486 break;
488 last_field_type = TYPE_CONTEXT (last_field_type);
491 /* If we've reached the outermost class, then we're
492 done. */
493 if (same_type_p (field_type, t))
494 break;
496 field_type = TYPE_CONTEXT (field_type);
498 while (!done);
500 last_field = field;
504 return init_list;
507 /* Like sort_member_init, but used for initializers of base classes.
508 *RBASE_PTR is filled in with the initializers for non-virtual bases;
509 vbase_ptr gets the virtual bases. */
511 static void
512 sort_base_init (t, base_init_list, rbase_ptr, vbase_ptr)
513 tree t;
514 tree base_init_list;
515 tree *rbase_ptr, *vbase_ptr;
517 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
518 int n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
520 int i;
521 tree x;
522 tree last;
524 /* For warn_reorder. */
525 int last_pos = 0;
526 tree last_base = NULL_TREE;
528 tree rbases = NULL_TREE;
529 tree vbases = NULL_TREE;
531 /* First walk through and splice out vbase and invalid initializers.
532 Also replace types with binfos. */
534 last = tree_cons (NULL_TREE, NULL_TREE, base_init_list);
535 for (x = TREE_CHAIN (last); x; x = TREE_CHAIN (x))
537 tree basetype = TREE_PURPOSE (x);
538 tree binfo = (TREE_CODE (basetype) == TREE_VEC
539 ? basetype : binfo_or_else (basetype, t));
541 if (binfo == NULL_TREE)
542 /* BASETYPE might be an inaccessible direct base (because it
543 is also an indirect base). */
544 continue;
546 if (TREE_VIA_VIRTUAL (binfo))
548 /* Virtual base classes are special cases. Their
549 initializers are recorded with this constructor, and they
550 are used when this constructor is the top-level
551 constructor called. */
552 tree v = binfo_for_vbase (BINFO_TYPE (binfo), t);
553 vbases = tree_cons (v, TREE_VALUE (x), vbases);
555 else
557 /* Otherwise, it must be an immediate base class. */
558 my_friendly_assert
559 (same_type_p (BINFO_TYPE (BINFO_INHERITANCE_CHAIN (binfo)),
560 t), 20011113);
562 TREE_PURPOSE (x) = binfo;
563 TREE_CHAIN (last) = x;
564 last = x;
567 TREE_CHAIN (last) = NULL_TREE;
569 /* Now walk through our regular bases and make sure they're initialized. */
571 for (i = 0; i < n_baseclasses; ++i)
573 /* The base for which we're currently initializing. */
574 tree base_binfo = TREE_VEC_ELT (binfos, i);
575 /* The initializer for BASE_BINFO. */
576 tree init;
577 int pos;
579 if (TREE_VIA_VIRTUAL (base_binfo))
580 continue;
582 /* We haven't found the BASE_BINFO yet. */
583 init = NULL_TREE;
584 /* Loop through all the explicitly initialized bases, looking
585 for an appropriate initializer. */
586 for (x = base_init_list, pos = 0; x; x = TREE_CHAIN (x), ++pos)
588 tree binfo = TREE_PURPOSE (x);
590 if (binfo == base_binfo && !init)
592 if (warn_reorder)
594 if (pos < last_pos)
596 cp_warning_at ("base initializers for `%#T'", last_base);
597 cp_warning_at (" and `%#T'", BINFO_TYPE (binfo));
598 warning (" will be re-ordered to match inheritance order");
600 last_pos = pos;
601 last_base = BINFO_TYPE (binfo);
604 /* Make sure we won't try to work on this init again. */
605 TREE_PURPOSE (x) = NULL_TREE;
606 init = build_tree_list (binfo, TREE_VALUE (x));
608 else if (binfo == base_binfo)
610 error ("base class `%T' already initialized",
611 BINFO_TYPE (binfo));
612 break;
616 /* If we didn't find BASE_BINFO in the list, create a dummy entry
617 so the two lists (RBASES and the list of bases) will be
618 symmetrical. */
619 if (!init)
620 init = build_tree_list (NULL_TREE, NULL_TREE);
621 rbases = chainon (rbases, init);
624 *rbase_ptr = rbases;
625 *vbase_ptr = vbases;
628 /* Perform whatever initializations have yet to be done on the base
629 class, and non-static data members, of the CURRENT_CLASS_TYPE.
630 These actions are given by the BASE_INIT_LIST and MEM_INIT_LIST,
631 respectively.
633 If there is a need for a call to a constructor, we must surround
634 that call with a pushlevel/poplevel pair, since we are technically
635 at the PARM level of scope. */
637 void
638 emit_base_init (mem_init_list, base_init_list)
639 tree mem_init_list;
640 tree base_init_list;
642 tree member;
643 tree rbase_init_list, vbase_init_list;
644 tree t = current_class_type;
645 tree t_binfo = TYPE_BINFO (t);
646 tree binfos = BINFO_BASETYPES (t_binfo);
647 int i;
648 int n_baseclasses = BINFO_N_BASETYPES (t_binfo);
650 mem_init_list = sort_member_init (t, mem_init_list);
651 sort_base_init (t, base_init_list, &rbase_init_list, &vbase_init_list);
653 /* First, initialize the virtual base classes, if we are
654 constructing the most-derived object. */
655 if (TYPE_USES_VIRTUAL_BASECLASSES (t))
657 tree first_arg = TREE_CHAIN (DECL_ARGUMENTS (current_function_decl));
658 construct_virtual_bases (t, current_class_ref, current_class_ptr,
659 vbase_init_list, first_arg);
662 /* Now, perform initialization of non-virtual base classes. */
663 for (i = 0; i < n_baseclasses; i++)
665 tree base_binfo = TREE_VEC_ELT (binfos, i);
666 tree init = void_list_node;
668 if (TREE_VIA_VIRTUAL (base_binfo))
669 continue;
671 my_friendly_assert (BINFO_INHERITANCE_CHAIN (base_binfo) == t_binfo,
672 999);
674 if (TREE_PURPOSE (rbase_init_list))
675 init = TREE_VALUE (rbase_init_list);
676 else if (TYPE_NEEDS_CONSTRUCTING (BINFO_TYPE (base_binfo)))
678 init = NULL_TREE;
679 if (extra_warnings
680 && DECL_COPY_CONSTRUCTOR_P (current_function_decl))
681 warning ("base class `%#T' should be explicitly initialized in the copy constructor",
682 BINFO_TYPE (base_binfo));
685 if (init != void_list_node)
687 member = build_base_path (PLUS_EXPR, current_class_ptr,
688 base_binfo, 1);
689 expand_aggr_init_1 (base_binfo, NULL_TREE,
690 build_indirect_ref (member, NULL), init,
691 LOOKUP_NORMAL);
694 expand_cleanup_for_base (base_binfo, NULL_TREE);
695 rbase_init_list = TREE_CHAIN (rbase_init_list);
698 /* Initialize the vtable pointers for the class. */
699 initialize_vtbl_ptrs (current_class_ptr);
701 while (mem_init_list)
703 tree init;
704 tree member;
705 int from_init_list;
707 member = TREE_PURPOSE (mem_init_list);
709 /* See if we had a user-specified member initialization. */
710 if (TREE_TYPE (mem_init_list))
712 init = TREE_VALUE (mem_init_list);
713 from_init_list = 1;
715 else
717 init = DECL_INITIAL (member);
718 from_init_list = 0;
720 /* Effective C++ rule 12. */
721 if (warn_ecpp && init == NULL_TREE
722 && !DECL_ARTIFICIAL (member)
723 && TREE_CODE (TREE_TYPE (member)) != ARRAY_TYPE)
724 warning ("`%D' should be initialized in the member initialization list", member);
727 perform_member_init (member, init, from_init_list);
728 mem_init_list = TREE_CHAIN (mem_init_list);
732 /* Returns the address of the vtable (i.e., the value that should be
733 assigned to the vptr) for BINFO. */
735 static tree
736 build_vtbl_address (binfo)
737 tree binfo;
739 tree binfo_for = binfo;
740 tree vtbl;
742 if (BINFO_VPTR_INDEX (binfo) && TREE_VIA_VIRTUAL (binfo)
743 && BINFO_PRIMARY_P (binfo))
744 /* If this is a virtual primary base, then the vtable we want to store
745 is that for the base this is being used as the primary base of. We
746 can't simply skip the initialization, because we may be expanding the
747 inits of a subobject constructor where the virtual base layout
748 can be different. */
749 while (BINFO_PRIMARY_BASE_OF (binfo_for))
750 binfo_for = BINFO_PRIMARY_BASE_OF (binfo_for);
752 /* Figure out what vtable BINFO's vtable is based on, and mark it as
753 used. */
754 vtbl = get_vtbl_decl_for_binfo (binfo_for);
755 assemble_external (vtbl);
756 TREE_USED (vtbl) = 1;
758 /* Now compute the address to use when initializing the vptr. */
759 vtbl = BINFO_VTABLE (binfo_for);
760 if (TREE_CODE (vtbl) == VAR_DECL)
762 vtbl = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (vtbl)), vtbl);
763 TREE_CONSTANT (vtbl) = 1;
766 return vtbl;
769 /* This code sets up the virtual function tables appropriate for
770 the pointer DECL. It is a one-ply initialization.
772 BINFO is the exact type that DECL is supposed to be. In
773 multiple inheritance, this might mean "C's A" if C : A, B. */
775 static void
776 expand_virtual_init (binfo, decl)
777 tree binfo, decl;
779 tree vtbl, vtbl_ptr;
780 tree vtt_index;
782 /* Compute the initializer for vptr. */
783 vtbl = build_vtbl_address (binfo);
785 /* We may get this vptr from a VTT, if this is a subobject
786 constructor or subobject destructor. */
787 vtt_index = BINFO_VPTR_INDEX (binfo);
788 if (vtt_index)
790 tree vtbl2;
791 tree vtt_parm;
793 /* Compute the value to use, when there's a VTT. */
794 vtt_parm = current_vtt_parm;
795 vtbl2 = build (PLUS_EXPR,
796 TREE_TYPE (vtt_parm),
797 vtt_parm,
798 vtt_index);
799 vtbl2 = build1 (INDIRECT_REF, TREE_TYPE (vtbl), vtbl2);
801 /* The actual initializer is the VTT value only in the subobject
802 constructor. In maybe_clone_body we'll substitute NULL for
803 the vtt_parm in the case of the non-subobject constructor. */
804 vtbl = build (COND_EXPR,
805 TREE_TYPE (vtbl),
806 build (EQ_EXPR, boolean_type_node,
807 current_in_charge_parm, integer_zero_node),
808 vtbl2,
809 vtbl);
812 /* Compute the location of the vtpr. */
813 vtbl_ptr = build_vfield_ref (build_indirect_ref (decl, NULL),
814 TREE_TYPE (binfo));
815 my_friendly_assert (vtbl_ptr != error_mark_node, 20010730);
817 /* Assign the vtable to the vptr. */
818 vtbl = convert_force (TREE_TYPE (vtbl_ptr), vtbl, 0);
819 finish_expr_stmt (build_modify_expr (vtbl_ptr, NOP_EXPR, vtbl));
822 /* If an exception is thrown in a constructor, those base classes already
823 constructed must be destroyed. This function creates the cleanup
824 for BINFO, which has just been constructed. If FLAG is non-NULL,
825 it is a DECL which is non-zero when this base needs to be
826 destroyed. */
828 static void
829 expand_cleanup_for_base (binfo, flag)
830 tree binfo;
831 tree flag;
833 tree expr;
835 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (BINFO_TYPE (binfo)))
836 return;
838 /* Call the destructor. */
839 expr = (build_scoped_method_call
840 (current_class_ref, binfo, base_dtor_identifier, NULL_TREE));
841 if (flag)
842 expr = fold (build (COND_EXPR, void_type_node,
843 c_common_truthvalue_conversion (flag),
844 expr, integer_zero_node));
846 finish_eh_cleanup (expr);
849 /* Subroutine of `expand_aggr_vbase_init'.
850 BINFO is the binfo of the type that is being initialized.
851 INIT_LIST is the list of initializers for the virtual baseclass. */
853 static void
854 expand_aggr_vbase_init_1 (binfo, exp, addr, init_list)
855 tree binfo, exp, addr, init_list;
857 tree init = purpose_member (binfo, init_list);
858 tree ref = build_indirect_ref (addr, NULL);
860 if (init)
861 init = TREE_VALUE (init);
862 /* Call constructors, but don't set up vtables. */
863 expand_aggr_init_1 (binfo, exp, ref, init, LOOKUP_COMPLAIN);
866 /* Construct the virtual base-classes of THIS_REF (whose address is
867 THIS_PTR). The object has the indicated TYPE. The construction
868 actually takes place only if FLAG is non-zero. INIT_LIST is list
869 of initializations for constructors to perform. */
871 static void
872 construct_virtual_bases (type, this_ref, this_ptr, init_list, flag)
873 tree type;
874 tree this_ref;
875 tree this_ptr;
876 tree init_list;
877 tree flag;
879 tree vbases;
881 /* If there are no virtual baseclasses, we shouldn't even be here. */
882 my_friendly_assert (TYPE_USES_VIRTUAL_BASECLASSES (type), 19990621);
884 /* Now, run through the baseclasses, initializing each. */
885 for (vbases = CLASSTYPE_VBASECLASSES (type); vbases;
886 vbases = TREE_CHAIN (vbases))
888 tree inner_if_stmt;
889 tree compound_stmt;
890 tree exp;
891 tree vbase;
893 /* If there are virtual base classes with destructors, we need to
894 emit cleanups to destroy them if an exception is thrown during
895 the construction process. These exception regions (i.e., the
896 period during which the cleanups must occur) begin from the time
897 the construction is complete to the end of the function. If we
898 create a conditional block in which to initialize the
899 base-classes, then the cleanup region for the virtual base begins
900 inside a block, and ends outside of that block. This situation
901 confuses the sjlj exception-handling code. Therefore, we do not
902 create a single conditional block, but one for each
903 initialization. (That way the cleanup regions always begin
904 in the outer block.) We trust the back-end to figure out
905 that the FLAG will not change across initializations, and
906 avoid doing multiple tests. */
907 inner_if_stmt = begin_if_stmt ();
908 finish_if_stmt_cond (flag, inner_if_stmt);
909 compound_stmt = begin_compound_stmt (/*has_no_scope=*/1);
911 /* Compute the location of the virtual base. If we're
912 constructing virtual bases, then we must be the most derived
913 class. Therefore, we don't have to look up the virtual base;
914 we already know where it is. */
915 vbase = TREE_VALUE (vbases);
916 exp = build (PLUS_EXPR,
917 TREE_TYPE (this_ptr),
918 this_ptr,
919 fold (build1 (NOP_EXPR, TREE_TYPE (this_ptr),
920 BINFO_OFFSET (vbase))));
921 exp = build1 (NOP_EXPR,
922 build_pointer_type (BINFO_TYPE (vbase)),
923 exp);
925 expand_aggr_vbase_init_1 (vbase, this_ref, exp, init_list);
926 finish_compound_stmt (/*has_no_scope=*/1, compound_stmt);
927 finish_then_clause (inner_if_stmt);
928 finish_if_stmt ();
930 expand_cleanup_for_base (vbase, flag);
934 /* Find the context in which this FIELD can be initialized. */
936 static tree
937 initializing_context (field)
938 tree field;
940 tree t = DECL_CONTEXT (field);
942 /* Anonymous union members can be initialized in the first enclosing
943 non-anonymous union context. */
944 while (t && ANON_AGGR_TYPE_P (t))
945 t = TYPE_CONTEXT (t);
946 return t;
949 /* Function to give error message if member initialization specification
950 is erroneous. FIELD is the member we decided to initialize.
951 TYPE is the type for which the initialization is being performed.
952 FIELD must be a member of TYPE.
954 MEMBER_NAME is the name of the member. */
956 static int
957 member_init_ok_or_else (field, type, member_name)
958 tree field;
959 tree type;
960 tree member_name;
962 if (field == error_mark_node)
963 return 0;
964 if (field == NULL_TREE || initializing_context (field) != type)
966 error ("class `%T' does not have any field named `%D'", type,
967 member_name);
968 return 0;
970 if (TREE_STATIC (field))
972 error ("field `%#D' is static; the only point of initialization is its definition",
973 field);
974 return 0;
977 return 1;
980 /* EXP is an expression of aggregate type. NAME is an IDENTIFIER_NODE
981 which names a field, or it is a _TYPE node or TYPE_DECL which names
982 a base for that type. INIT is a parameter list for that field's or
983 base's constructor. Check the validity of NAME, and return a
984 TREE_LIST of the base _TYPE or FIELD_DECL and the INIT. EXP is used
985 only to get its type. If NAME is invalid, return NULL_TREE and
986 issue a diagnostic.
988 An old style unnamed direct single base construction is permitted,
989 where NAME is NULL. */
991 tree
992 expand_member_init (exp, name, init)
993 tree exp, name, init;
995 tree basetype = NULL_TREE, field;
996 tree type;
998 if (exp == NULL_TREE)
999 return NULL_TREE;
1001 type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
1002 my_friendly_assert (IS_AGGR_TYPE (type), 20011113);
1004 if (!name)
1006 /* This is an obsolete unnamed base class initializer. The
1007 parser will already have warned about its use. */
1008 switch (CLASSTYPE_N_BASECLASSES (type))
1010 case 0:
1011 error ("unnamed initializer for `%T', which has no base classes",
1012 type);
1013 return NULL_TREE;
1014 case 1:
1015 basetype = TYPE_BINFO_BASETYPE (type, 0);
1016 break;
1017 default:
1018 error ("unnamed initializer for `%T', which uses multiple inheritance",
1019 type);
1020 return NULL_TREE;
1023 else if (TYPE_P (name))
1025 basetype = name;
1026 name = TYPE_NAME (name);
1028 else if (TREE_CODE (name) == TYPE_DECL)
1029 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (name));
1031 my_friendly_assert (init != NULL_TREE, 0);
1033 if (init == void_type_node)
1034 init = NULL_TREE;
1036 if (basetype)
1038 if (current_template_parms)
1040 else if (vec_binfo_member (basetype, TYPE_BINFO_BASETYPES (type)))
1041 /* A direct base. */;
1042 else if (binfo_for_vbase (basetype, type))
1043 /* A virtual base. */;
1044 else
1046 if (TYPE_USES_VIRTUAL_BASECLASSES (type))
1047 error ("type `%D' is not a direct or virtual base of `%T'",
1048 name, type);
1049 else
1050 error ("type `%D' is not a direct base of `%T'",
1051 name, type);
1052 return NULL_TREE;
1055 init = build_tree_list (basetype, init);
1057 else
1059 field = lookup_field (type, name, 1, 0);
1061 if (! member_init_ok_or_else (field, type, name))
1062 return NULL_TREE;
1064 init = build_tree_list (field, init);
1067 return init;
1070 /* This is like `expand_member_init', only it stores one aggregate
1071 value into another.
1073 INIT comes in two flavors: it is either a value which
1074 is to be stored in EXP, or it is a parameter list
1075 to go to a constructor, which will operate on EXP.
1076 If INIT is not a parameter list for a constructor, then set
1077 LOOKUP_ONLYCONVERTING.
1078 If FLAGS is LOOKUP_ONLYCONVERTING then it is the = init form of
1079 the initializer, if FLAGS is 0, then it is the (init) form.
1080 If `init' is a CONSTRUCTOR, then we emit a warning message,
1081 explaining that such initializations are invalid.
1083 If INIT resolves to a CALL_EXPR which happens to return
1084 something of the type we are looking for, then we know
1085 that we can safely use that call to perform the
1086 initialization.
1088 The virtual function table pointer cannot be set up here, because
1089 we do not really know its type.
1091 Virtual baseclass pointers are also set up here.
1093 This never calls operator=().
1095 When initializing, nothing is CONST.
1097 A default copy constructor may have to be used to perform the
1098 initialization.
1100 A constructor or a conversion operator may have to be used to
1101 perform the initialization, but not both, as it would be ambiguous. */
1103 tree
1104 build_aggr_init (exp, init, flags)
1105 tree exp, init;
1106 int flags;
1108 tree stmt_expr;
1109 tree compound_stmt;
1110 int destroy_temps;
1111 tree type = TREE_TYPE (exp);
1112 int was_const = TREE_READONLY (exp);
1113 int was_volatile = TREE_THIS_VOLATILE (exp);
1115 if (init == error_mark_node)
1116 return error_mark_node;
1118 TREE_READONLY (exp) = 0;
1119 TREE_THIS_VOLATILE (exp) = 0;
1121 if (init && TREE_CODE (init) != TREE_LIST)
1122 flags |= LOOKUP_ONLYCONVERTING;
1124 if (TREE_CODE (type) == ARRAY_TYPE)
1126 /* Must arrange to initialize each element of EXP
1127 from elements of INIT. */
1128 tree itype = init ? TREE_TYPE (init) : NULL_TREE;
1130 if (init && !itype)
1132 /* Handle bad initializers like:
1133 class COMPLEX {
1134 public:
1135 double re, im;
1136 COMPLEX(double r = 0.0, double i = 0.0) {re = r; im = i;};
1137 ~COMPLEX() {};
1140 int main(int argc, char **argv) {
1141 COMPLEX zees(1.0, 0.0)[10];
1144 error ("bad array initializer");
1145 return error_mark_node;
1147 if (cp_type_quals (type) != TYPE_UNQUALIFIED)
1149 TREE_TYPE (exp) = TYPE_MAIN_VARIANT (type);
1150 if (init)
1151 TREE_TYPE (init) = TYPE_MAIN_VARIANT (itype);
1153 stmt_expr = build_vec_init (exp, init,
1154 init && same_type_p (TREE_TYPE (init),
1155 TREE_TYPE (exp)));
1156 TREE_READONLY (exp) = was_const;
1157 TREE_THIS_VOLATILE (exp) = was_volatile;
1158 TREE_TYPE (exp) = type;
1159 if (init)
1160 TREE_TYPE (init) = itype;
1161 return stmt_expr;
1164 if (TREE_CODE (exp) == VAR_DECL || TREE_CODE (exp) == PARM_DECL)
1165 /* just know that we've seen something for this node */
1166 TREE_USED (exp) = 1;
1168 TREE_TYPE (exp) = TYPE_MAIN_VARIANT (type);
1169 begin_init_stmts (&stmt_expr, &compound_stmt);
1170 destroy_temps = stmts_are_full_exprs_p ();
1171 current_stmt_tree ()->stmts_are_full_exprs_p = 0;
1172 expand_aggr_init_1 (TYPE_BINFO (type), exp, exp,
1173 init, LOOKUP_NORMAL|flags);
1174 stmt_expr = finish_init_stmts (stmt_expr, compound_stmt);
1175 current_stmt_tree ()->stmts_are_full_exprs_p = destroy_temps;
1176 TREE_TYPE (exp) = type;
1177 TREE_READONLY (exp) = was_const;
1178 TREE_THIS_VOLATILE (exp) = was_volatile;
1180 return stmt_expr;
1183 /* Like build_aggr_init, but not just for aggregates. */
1185 tree
1186 build_init (decl, init, flags)
1187 tree decl, init;
1188 int flags;
1190 tree expr;
1192 if (IS_AGGR_TYPE (TREE_TYPE (decl))
1193 || TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
1194 expr = build_aggr_init (decl, init, flags);
1195 else
1197 expr = build (INIT_EXPR, TREE_TYPE (decl), decl, init);
1198 TREE_SIDE_EFFECTS (expr) = 1;
1200 return expr;
1203 static void
1204 expand_default_init (binfo, true_exp, exp, init, flags)
1205 tree binfo;
1206 tree true_exp, exp;
1207 tree init;
1208 int flags;
1210 tree type = TREE_TYPE (exp);
1211 tree ctor_name;
1213 /* It fails because there may not be a constructor which takes
1214 its own type as the first (or only parameter), but which does
1215 take other types via a conversion. So, if the thing initializing
1216 the expression is a unit element of type X, first try X(X&),
1217 followed by initialization by X. If neither of these work
1218 out, then look hard. */
1219 tree rval;
1220 tree parms;
1222 if (init && TREE_CODE (init) != TREE_LIST
1223 && (flags & LOOKUP_ONLYCONVERTING))
1225 /* Base subobjects should only get direct-initialization. */
1226 if (true_exp != exp)
1227 abort ();
1229 if (flags & DIRECT_BIND)
1230 /* Do nothing. We hit this in two cases: Reference initialization,
1231 where we aren't initializing a real variable, so we don't want
1232 to run a new constructor; and catching an exception, where we
1233 have already built up the constructor call so we could wrap it
1234 in an exception region. */;
1235 else if (TREE_CODE (init) == CONSTRUCTOR)
1236 /* A brace-enclosed initializer has whatever type is
1237 required. There's no need to convert it. */
1239 else
1240 init = ocp_convert (type, init, CONV_IMPLICIT|CONV_FORCE_TEMP, flags);
1242 if (TREE_CODE (init) == TRY_CATCH_EXPR)
1243 /* We need to protect the initialization of a catch parm
1244 with a call to terminate(), which shows up as a TRY_CATCH_EXPR
1245 around the TARGET_EXPR for the copy constructor. See
1246 expand_start_catch_block. */
1247 TREE_OPERAND (init, 0) = build (INIT_EXPR, TREE_TYPE (exp), exp,
1248 TREE_OPERAND (init, 0));
1249 else
1250 init = build (INIT_EXPR, TREE_TYPE (exp), exp, init);
1251 TREE_SIDE_EFFECTS (init) = 1;
1252 finish_expr_stmt (init);
1253 return;
1256 if (init == NULL_TREE
1257 || (TREE_CODE (init) == TREE_LIST && ! TREE_TYPE (init)))
1259 parms = init;
1260 if (parms)
1261 init = TREE_VALUE (parms);
1263 else
1264 parms = build_tree_list (NULL_TREE, init);
1266 if (true_exp == exp)
1267 ctor_name = complete_ctor_identifier;
1268 else
1269 ctor_name = base_ctor_identifier;
1271 rval = build_method_call (exp, ctor_name, parms, binfo, flags);
1272 if (TREE_SIDE_EFFECTS (rval))
1274 if (building_stmt_tree ())
1275 finish_expr_stmt (rval);
1276 else
1277 genrtl_expr_stmt (rval);
1281 /* This function is responsible for initializing EXP with INIT
1282 (if any).
1284 BINFO is the binfo of the type for who we are performing the
1285 initialization. For example, if W is a virtual base class of A and B,
1286 and C : A, B.
1287 If we are initializing B, then W must contain B's W vtable, whereas
1288 were we initializing C, W must contain C's W vtable.
1290 TRUE_EXP is nonzero if it is the true expression being initialized.
1291 In this case, it may be EXP, or may just contain EXP. The reason we
1292 need this is because if EXP is a base element of TRUE_EXP, we
1293 don't necessarily know by looking at EXP where its virtual
1294 baseclass fields should really be pointing. But we do know
1295 from TRUE_EXP. In constructors, we don't know anything about
1296 the value being initialized.
1298 FLAGS is just passes to `build_method_call'. See that function for
1299 its description. */
1301 static void
1302 expand_aggr_init_1 (binfo, true_exp, exp, init, flags)
1303 tree binfo;
1304 tree true_exp, exp;
1305 tree init;
1306 int flags;
1308 tree type = TREE_TYPE (exp);
1310 my_friendly_assert (init != error_mark_node && type != error_mark_node, 211);
1312 /* Use a function returning the desired type to initialize EXP for us.
1313 If the function is a constructor, and its first argument is
1314 NULL_TREE, know that it was meant for us--just slide exp on
1315 in and expand the constructor. Constructors now come
1316 as TARGET_EXPRs. */
1318 if (init && TREE_CODE (exp) == VAR_DECL
1319 && TREE_CODE (init) == CONSTRUCTOR
1320 && TREE_HAS_CONSTRUCTOR (init))
1322 /* If store_init_value returns NULL_TREE, the INIT has been
1323 record in the DECL_INITIAL for EXP. That means there's
1324 nothing more we have to do. */
1325 if (!store_init_value (exp, init))
1327 if (!building_stmt_tree ())
1328 expand_decl_init (exp);
1330 else
1331 finish_expr_stmt (build (INIT_EXPR, type, exp, init));
1332 return;
1335 /* We know that expand_default_init can handle everything we want
1336 at this point. */
1337 expand_default_init (binfo, true_exp, exp, init, flags);
1340 /* Report an error if TYPE is not a user-defined, aggregate type. If
1341 OR_ELSE is nonzero, give an error message. */
1344 is_aggr_type (type, or_else)
1345 tree type;
1346 int or_else;
1348 if (type == error_mark_node)
1349 return 0;
1351 if (! IS_AGGR_TYPE (type)
1352 && TREE_CODE (type) != TEMPLATE_TYPE_PARM
1353 && TREE_CODE (type) != BOUND_TEMPLATE_TEMPLATE_PARM)
1355 if (or_else)
1356 error ("`%T' is not an aggregate type", type);
1357 return 0;
1359 return 1;
1362 /* Like is_aggr_typedef, but returns typedef if successful. */
1364 tree
1365 get_aggr_from_typedef (name, or_else)
1366 tree name;
1367 int or_else;
1369 tree type;
1371 if (name == error_mark_node)
1372 return NULL_TREE;
1374 if (IDENTIFIER_HAS_TYPE_VALUE (name))
1375 type = IDENTIFIER_TYPE_VALUE (name);
1376 else
1378 if (or_else)
1379 error ("`%T' fails to be an aggregate typedef", name);
1380 return NULL_TREE;
1383 if (! IS_AGGR_TYPE (type)
1384 && TREE_CODE (type) != TEMPLATE_TYPE_PARM
1385 && TREE_CODE (type) != BOUND_TEMPLATE_TEMPLATE_PARM)
1387 if (or_else)
1388 error ("type `%T' is of non-aggregate type", type);
1389 return NULL_TREE;
1391 return type;
1394 tree
1395 get_type_value (name)
1396 tree name;
1398 if (name == error_mark_node)
1399 return NULL_TREE;
1401 if (IDENTIFIER_HAS_TYPE_VALUE (name))
1402 return IDENTIFIER_TYPE_VALUE (name);
1403 else
1404 return NULL_TREE;
1408 /* This code could just as well go in `class.c', but is placed here for
1409 modularity. */
1411 /* For an expression of the form TYPE :: NAME (PARMLIST), build
1412 the appropriate function call. */
1414 tree
1415 build_member_call (type, name, parmlist)
1416 tree type, name, parmlist;
1418 tree t;
1419 tree method_name;
1420 int dtor = 0;
1421 tree basetype_path, decl;
1423 if (TREE_CODE (name) == TEMPLATE_ID_EXPR
1424 && TREE_CODE (type) == NAMESPACE_DECL)
1426 /* 'name' already refers to the decls from the namespace, since we
1427 hit do_identifier for template_ids. */
1428 method_name = TREE_OPERAND (name, 0);
1429 /* FIXME: Since we don't do independent names right yet, the
1430 name might also be a LOOKUP_EXPR. Once we resolve this to a
1431 real decl earlier, this can go. This may happen during
1432 tsubst'ing. */
1433 if (TREE_CODE (method_name) == LOOKUP_EXPR)
1435 method_name = lookup_namespace_name
1436 (type, TREE_OPERAND (method_name, 0));
1437 TREE_OPERAND (name, 0) = method_name;
1439 my_friendly_assert (is_overloaded_fn (method_name), 980519);
1440 return build_x_function_call (name, parmlist, current_class_ref);
1443 if (DECL_P (name))
1444 name = DECL_NAME (name);
1446 if (TREE_CODE (type) == NAMESPACE_DECL)
1447 return build_x_function_call (lookup_namespace_name (type, name),
1448 parmlist, current_class_ref);
1450 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
1452 method_name = TREE_OPERAND (name, 0);
1453 if (TREE_CODE (method_name) == COMPONENT_REF)
1454 method_name = TREE_OPERAND (method_name, 1);
1455 if (is_overloaded_fn (method_name))
1456 method_name = DECL_NAME (OVL_CURRENT (method_name));
1457 TREE_OPERAND (name, 0) = method_name;
1459 else
1460 method_name = name;
1462 if (TREE_CODE (method_name) == BIT_NOT_EXPR)
1464 method_name = TREE_OPERAND (method_name, 0);
1465 dtor = 1;
1468 /* This shouldn't be here, and build_member_call shouldn't appear in
1469 parse.y! (mrs) */
1470 if (type && TREE_CODE (type) == IDENTIFIER_NODE
1471 && get_aggr_from_typedef (type, 0) == 0)
1473 tree ns = lookup_name (type, 0);
1474 if (ns && TREE_CODE (ns) == NAMESPACE_DECL)
1476 return build_x_function_call (build_offset_ref (type, name),
1477 parmlist, current_class_ref);
1481 if (type == NULL_TREE || ! is_aggr_type (type, 1))
1482 return error_mark_node;
1484 /* An operator we did not like. */
1485 if (name == NULL_TREE)
1486 return error_mark_node;
1488 if (dtor)
1490 error ("cannot call destructor `%T::~%T' without object", type,
1491 method_name);
1492 return error_mark_node;
1495 decl = maybe_dummy_object (type, &basetype_path);
1497 /* Convert 'this' to the specified type to disambiguate conversion
1498 to the function's context. */
1499 if (decl == current_class_ref
1500 /* ??? this is wrong, but if this conversion is invalid we need to
1501 defer it until we know whether we are calling a static or
1502 non-static member function. Be conservative for now. */
1503 && ACCESSIBLY_UNIQUELY_DERIVED_P (type, current_class_type))
1505 basetype_path = NULL_TREE;
1506 decl = build_scoped_ref (decl, type, &basetype_path);
1507 if (decl == error_mark_node)
1508 return error_mark_node;
1511 if (method_name == constructor_name (type)
1512 || method_name == constructor_name_full (type))
1513 return build_functional_cast (type, parmlist);
1514 if (lookup_fnfields (basetype_path, method_name, 0))
1515 return build_method_call (decl,
1516 TREE_CODE (name) == TEMPLATE_ID_EXPR
1517 ? name : method_name,
1518 parmlist, basetype_path,
1519 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL);
1520 if (TREE_CODE (name) == IDENTIFIER_NODE
1521 && ((t = lookup_field (TYPE_BINFO (type), name, 1, 0))))
1523 if (t == error_mark_node)
1524 return error_mark_node;
1525 if (TREE_CODE (t) == FIELD_DECL)
1527 if (is_dummy_object (decl))
1529 error ("invalid use of non-static field `%D'", t);
1530 return error_mark_node;
1532 decl = build (COMPONENT_REF, TREE_TYPE (t), decl, t);
1534 else if (TREE_CODE (t) == VAR_DECL)
1535 decl = t;
1536 else
1538 error ("invalid use of member `%D'", t);
1539 return error_mark_node;
1541 if (TYPE_LANG_SPECIFIC (TREE_TYPE (decl)))
1542 return build_opfncall (CALL_EXPR, LOOKUP_NORMAL, decl,
1543 parmlist, NULL_TREE);
1544 return build_function_call (decl, parmlist);
1546 else
1548 error ("no method `%T::%D'", type, name);
1549 return error_mark_node;
1553 /* Build a reference to a member of an aggregate. This is not a
1554 C++ `&', but really something which can have its address taken,
1555 and then act as a pointer to member, for example TYPE :: FIELD
1556 can have its address taken by saying & TYPE :: FIELD.
1558 @@ Prints out lousy diagnostics for operator <typename>
1559 @@ fields.
1561 @@ This function should be rewritten and placed in search.c. */
1563 tree
1564 build_offset_ref (type, name)
1565 tree type, name;
1567 tree decl, t = error_mark_node;
1568 tree member;
1569 tree basebinfo = NULL_TREE;
1570 tree orig_name = name;
1572 /* class templates can come in as TEMPLATE_DECLs here. */
1573 if (TREE_CODE (name) == TEMPLATE_DECL)
1574 return name;
1576 if (processing_template_decl || uses_template_parms (type))
1577 return build_min_nt (SCOPE_REF, type, name);
1579 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
1581 /* If the NAME is a TEMPLATE_ID_EXPR, we are looking at
1582 something like `a.template f<int>' or the like. For the most
1583 part, we treat this just like a.f. We do remember, however,
1584 the template-id that was used. */
1585 name = TREE_OPERAND (orig_name, 0);
1587 if (DECL_P (name))
1588 name = DECL_NAME (name);
1589 else
1591 if (TREE_CODE (name) == LOOKUP_EXPR)
1592 /* This can happen during tsubst'ing. */
1593 name = TREE_OPERAND (name, 0);
1594 else
1596 if (TREE_CODE (name) == COMPONENT_REF)
1597 name = TREE_OPERAND (name, 1);
1598 if (TREE_CODE (name) == OVERLOAD)
1599 name = DECL_NAME (OVL_CURRENT (name));
1603 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 0);
1606 if (type == NULL_TREE)
1607 return error_mark_node;
1609 /* Handle namespace names fully here. */
1610 if (TREE_CODE (type) == NAMESPACE_DECL)
1612 t = lookup_namespace_name (type, name);
1613 if (t == error_mark_node)
1614 return t;
1615 if (TREE_CODE (orig_name) == TEMPLATE_ID_EXPR)
1616 /* Reconstruct the TEMPLATE_ID_EXPR. */
1617 t = build (TEMPLATE_ID_EXPR, TREE_TYPE (t),
1618 t, TREE_OPERAND (orig_name, 1));
1619 if (! type_unknown_p (t))
1621 mark_used (t);
1622 t = convert_from_reference (t);
1624 return t;
1627 if (! is_aggr_type (type, 1))
1628 return error_mark_node;
1630 if (TREE_CODE (name) == BIT_NOT_EXPR)
1632 if (! check_dtor_name (type, name))
1633 error ("qualified type `%T' does not match destructor name `~%T'",
1634 type, TREE_OPERAND (name, 0));
1635 name = dtor_identifier;
1638 if (!COMPLETE_TYPE_P (complete_type (type))
1639 && !TYPE_BEING_DEFINED (type))
1641 error ("incomplete type `%T' does not have member `%D'", type,
1642 name);
1643 return error_mark_node;
1646 decl = maybe_dummy_object (type, &basebinfo);
1648 member = lookup_member (basebinfo, name, 1, 0);
1650 if (member == error_mark_node)
1651 return error_mark_node;
1653 /* A lot of this logic is now handled in lookup_member. */
1654 if (member && BASELINK_P (member))
1656 /* Go from the TREE_BASELINK to the member function info. */
1657 tree fnfields = member;
1658 t = TREE_VALUE (fnfields);
1660 if (TREE_CODE (orig_name) == TEMPLATE_ID_EXPR)
1662 /* The FNFIELDS are going to contain functions that aren't
1663 necessarily templates, and templates that don't
1664 necessarily match the explicit template parameters. We
1665 save all the functions, and the explicit parameters, and
1666 then figure out exactly what to instantiate with what
1667 arguments in instantiate_type. */
1669 if (TREE_CODE (t) != OVERLOAD)
1670 /* The code in instantiate_type which will process this
1671 expects to encounter OVERLOADs, not raw functions. */
1672 t = ovl_cons (t, NULL_TREE);
1674 t = build (TEMPLATE_ID_EXPR, TREE_TYPE (t), t,
1675 TREE_OPERAND (orig_name, 1));
1676 t = build (OFFSET_REF, unknown_type_node, decl, t);
1678 PTRMEM_OK_P (t) = 1;
1680 return t;
1683 if (!really_overloaded_fn (t))
1685 /* Get rid of a potential OVERLOAD around it */
1686 t = OVL_CURRENT (t);
1688 /* unique functions are handled easily. */
1689 if (!enforce_access (basebinfo, t))
1690 return error_mark_node;
1691 mark_used (t);
1692 if (DECL_STATIC_FUNCTION_P (t))
1693 return t;
1694 t = build (OFFSET_REF, TREE_TYPE (t), decl, t);
1695 PTRMEM_OK_P (t) = 1;
1696 return t;
1699 TREE_TYPE (fnfields) = unknown_type_node;
1701 t = build (OFFSET_REF, unknown_type_node, decl, fnfields);
1702 PTRMEM_OK_P (t) = 1;
1703 return t;
1706 t = member;
1708 if (t == NULL_TREE)
1710 error ("`%D' is not a member of type `%T'", name, type);
1711 return error_mark_node;
1714 if (TREE_CODE (t) == TYPE_DECL)
1716 TREE_USED (t) = 1;
1717 return t;
1719 /* static class members and class-specific enum
1720 values can be returned without further ado. */
1721 if (TREE_CODE (t) == VAR_DECL || TREE_CODE (t) == CONST_DECL)
1723 mark_used (t);
1724 return convert_from_reference (t);
1727 if (TREE_CODE (t) == FIELD_DECL && DECL_C_BIT_FIELD (t))
1729 error ("illegal pointer to bit-field `%D'", t);
1730 return error_mark_node;
1733 /* static class functions too. */
1734 if (TREE_CODE (t) == FUNCTION_DECL
1735 && TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
1736 abort ();
1738 /* In member functions, the form `type::name' is no longer
1739 equivalent to `this->type::name', at least not until
1740 resolve_offset_ref. */
1741 t = build (OFFSET_REF, build_offset_type (type, TREE_TYPE (t)), decl, t);
1742 PTRMEM_OK_P (t) = 1;
1743 return t;
1746 /* If a OFFSET_REF made it through to here, then it did
1747 not have its address taken. */
1749 tree
1750 resolve_offset_ref (exp)
1751 tree exp;
1753 tree type = TREE_TYPE (exp);
1754 tree base = NULL_TREE;
1755 tree member;
1756 tree basetype, addr;
1758 if (TREE_CODE (exp) == OFFSET_REF)
1760 member = TREE_OPERAND (exp, 1);
1761 base = TREE_OPERAND (exp, 0);
1763 else
1765 my_friendly_assert (TREE_CODE (type) == OFFSET_TYPE, 214);
1766 if (TYPE_OFFSET_BASETYPE (type) != current_class_type)
1768 error ("object missing in use of pointer-to-member construct");
1769 return error_mark_node;
1771 member = exp;
1772 type = TREE_TYPE (type);
1773 base = current_class_ref;
1776 if (BASELINK_P (member) || TREE_CODE (member) == TEMPLATE_ID_EXPR)
1777 return build_unary_op (ADDR_EXPR, exp, 0);
1779 if (TREE_CODE (TREE_TYPE (member)) == METHOD_TYPE)
1781 if (!flag_ms_extensions)
1782 /* A single non-static member, make sure we don't allow a
1783 pointer-to-member. */
1784 exp = ovl_cons (member, NULL_TREE);
1786 return build_unary_op (ADDR_EXPR, exp, 0);
1789 if ((TREE_CODE (member) == VAR_DECL
1790 && ! TYPE_PTRMEMFUNC_P (TREE_TYPE (member))
1791 && ! TYPE_PTRMEM_P (TREE_TYPE (member)))
1792 || TREE_CODE (TREE_TYPE (member)) == FUNCTION_TYPE)
1794 /* These were static members. */
1795 if (!cxx_mark_addressable (member))
1796 return error_mark_node;
1797 return member;
1800 if (TREE_CODE (TREE_TYPE (member)) == POINTER_TYPE
1801 && TREE_CODE (TREE_TYPE (TREE_TYPE (member))) == METHOD_TYPE)
1802 return member;
1804 /* Syntax error can cause a member which should
1805 have been seen as static to be grok'd as non-static. */
1806 if (TREE_CODE (member) == FIELD_DECL && current_class_ref == NULL_TREE)
1808 cp_error_at ("member `%D' is non-static but referenced as a static member",
1809 member);
1810 error ("at this point in file");
1811 return error_mark_node;
1814 /* The first case is really just a reference to a member of `this'. */
1815 if (TREE_CODE (member) == FIELD_DECL
1816 && (base == current_class_ref || is_dummy_object (base)))
1818 tree binfo = NULL_TREE;
1820 /* Try to get to basetype from 'this'; if that doesn't work,
1821 nothing will. */
1822 base = current_class_ref;
1824 /* First convert to the intermediate base specified, if appropriate. */
1825 if (TREE_CODE (exp) == OFFSET_REF && TREE_CODE (type) == OFFSET_TYPE)
1826 base = build_scoped_ref (base, TYPE_OFFSET_BASETYPE (type), &binfo);
1828 return build_component_ref (base, member, binfo, 1);
1831 /* Ensure that we have an object. */
1832 if (is_dummy_object (base))
1833 addr = error_mark_node;
1834 else
1835 /* If this is a reference to a member function, then return the
1836 address of the member function (which may involve going
1837 through the object's vtable), otherwise, return an expression
1838 for the dereferenced pointer-to-member construct. */
1839 addr = build_unary_op (ADDR_EXPR, base, 0);
1841 if (TYPE_PTRMEM_P (TREE_TYPE (member)))
1843 if (addr == error_mark_node)
1845 error ("object missing in `%E'", exp);
1846 return error_mark_node;
1849 basetype = TYPE_OFFSET_BASETYPE (TREE_TYPE (TREE_TYPE (member)));
1850 basetype = lookup_base (TREE_TYPE (TREE_TYPE (addr)),
1851 basetype, ba_check, NULL);
1852 addr = build_base_path (PLUS_EXPR, addr, basetype, 1);
1854 member = cp_convert (ptrdiff_type_node, member);
1856 addr = build (PLUS_EXPR, build_pointer_type (type), addr, member);
1857 return build_indirect_ref (addr, 0);
1859 else if (TYPE_PTRMEMFUNC_P (TREE_TYPE (member)))
1861 return get_member_function_from_ptrfunc (&addr, member);
1863 abort ();
1864 /* NOTREACHED */
1865 return NULL_TREE;
1868 /* If DECL is a `const' declaration, and its value is a known
1869 constant, then return that value. */
1871 tree
1872 decl_constant_value (decl)
1873 tree decl;
1875 if (TREE_READONLY_DECL_P (decl)
1876 && ! TREE_THIS_VOLATILE (decl)
1877 && DECL_INITIAL (decl)
1878 && DECL_INITIAL (decl) != error_mark_node
1879 /* This is invalid if initial value is not constant.
1880 If it has either a function call, a memory reference,
1881 or a variable, then re-evaluating it could give different results. */
1882 && TREE_CONSTANT (DECL_INITIAL (decl))
1883 /* Check for cases where this is sub-optimal, even though valid. */
1884 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1885 return DECL_INITIAL (decl);
1886 return decl;
1889 /* Common subroutines of build_new and build_vec_delete. */
1891 /* Call the global __builtin_delete to delete ADDR. */
1893 static tree
1894 build_builtin_delete_call (addr)
1895 tree addr;
1897 mark_used (global_delete_fndecl);
1898 return build_call (global_delete_fndecl, build_tree_list (NULL_TREE, addr));
1901 /* Generate a C++ "new" expression. DECL is either a TREE_LIST
1902 (which needs to go through some sort of groktypename) or it
1903 is the name of the class we are newing. INIT is an initialization value.
1904 It is either an EXPRLIST, an EXPR_NO_COMMAS, or something in braces.
1905 If INIT is void_type_node, it means do *not* call a constructor
1906 for this instance.
1908 For types with constructors, the data returned is initialized
1909 by the appropriate constructor.
1911 Whether the type has a constructor or not, if it has a pointer
1912 to a virtual function table, then that pointer is set up
1913 here.
1915 Unless I am mistaken, a call to new () will return initialized
1916 data regardless of whether the constructor itself is private or
1917 not. NOPE; new fails if the constructor is private (jcm).
1919 Note that build_new does nothing to assure that any special
1920 alignment requirements of the type are met. Rather, it leaves
1921 it up to malloc to do the right thing. Otherwise, folding to
1922 the right alignment cal cause problems if the user tries to later
1923 free the memory returned by `new'.
1925 PLACEMENT is the `placement' list for user-defined operator new (). */
1927 tree
1928 build_new (placement, decl, init, use_global_new)
1929 tree placement;
1930 tree decl, init;
1931 int use_global_new;
1933 tree type, rval;
1934 tree nelts = NULL_TREE, t;
1935 int has_array = 0;
1937 if (decl == error_mark_node)
1938 return error_mark_node;
1940 if (TREE_CODE (decl) == TREE_LIST)
1942 tree absdcl = TREE_VALUE (decl);
1943 tree last_absdcl = NULL_TREE;
1945 if (current_function_decl
1946 && DECL_CONSTRUCTOR_P (current_function_decl))
1947 my_friendly_assert (immediate_size_expand == 0, 19990926);
1949 nelts = integer_one_node;
1951 if (absdcl && TREE_CODE (absdcl) == CALL_EXPR)
1952 abort ();
1953 while (absdcl && TREE_CODE (absdcl) == INDIRECT_REF)
1955 last_absdcl = absdcl;
1956 absdcl = TREE_OPERAND (absdcl, 0);
1959 if (absdcl && TREE_CODE (absdcl) == ARRAY_REF)
1961 /* probably meant to be a vec new */
1962 tree this_nelts;
1964 while (TREE_OPERAND (absdcl, 0)
1965 && TREE_CODE (TREE_OPERAND (absdcl, 0)) == ARRAY_REF)
1967 last_absdcl = absdcl;
1968 absdcl = TREE_OPERAND (absdcl, 0);
1971 has_array = 1;
1972 this_nelts = TREE_OPERAND (absdcl, 1);
1973 if (this_nelts != error_mark_node)
1975 if (this_nelts == NULL_TREE)
1976 error ("new of array type fails to specify size");
1977 else if (processing_template_decl)
1979 nelts = this_nelts;
1980 absdcl = TREE_OPERAND (absdcl, 0);
1982 else
1984 if (build_expr_type_conversion (WANT_INT | WANT_ENUM,
1985 this_nelts, 0)
1986 == NULL_TREE)
1987 pedwarn ("size in array new must have integral type");
1989 this_nelts = save_expr (cp_convert (sizetype, this_nelts));
1990 absdcl = TREE_OPERAND (absdcl, 0);
1991 if (this_nelts == integer_zero_node)
1993 warning ("zero size array reserves no space");
1994 nelts = integer_zero_node;
1996 else
1997 nelts = cp_build_binary_op (MULT_EXPR, nelts, this_nelts);
2000 else
2001 nelts = integer_zero_node;
2004 if (last_absdcl)
2005 TREE_OPERAND (last_absdcl, 0) = absdcl;
2006 else
2007 TREE_VALUE (decl) = absdcl;
2009 type = groktypename (decl);
2010 if (! type || type == error_mark_node)
2011 return error_mark_node;
2013 else if (TREE_CODE (decl) == IDENTIFIER_NODE)
2015 if (IDENTIFIER_HAS_TYPE_VALUE (decl))
2017 /* An aggregate type. */
2018 type = IDENTIFIER_TYPE_VALUE (decl);
2019 decl = TYPE_MAIN_DECL (type);
2021 else
2023 /* A builtin type. */
2024 decl = lookup_name (decl, 1);
2025 my_friendly_assert (TREE_CODE (decl) == TYPE_DECL, 215);
2026 type = TREE_TYPE (decl);
2029 else if (TREE_CODE (decl) == TYPE_DECL)
2031 type = TREE_TYPE (decl);
2033 else
2035 type = decl;
2036 decl = TYPE_MAIN_DECL (type);
2039 if (processing_template_decl)
2041 if (has_array)
2042 t = tree_cons (tree_cons (NULL_TREE, type, NULL_TREE),
2043 build_min_nt (ARRAY_REF, NULL_TREE, nelts),
2044 NULL_TREE);
2045 else
2046 t = type;
2048 rval = build_min_nt (NEW_EXPR, placement, t, init);
2049 NEW_EXPR_USE_GLOBAL (rval) = use_global_new;
2050 return rval;
2053 /* ``A reference cannot be created by the new operator. A reference
2054 is not an object (8.2.2, 8.4.3), so a pointer to it could not be
2055 returned by new.'' ARM 5.3.3 */
2056 if (TREE_CODE (type) == REFERENCE_TYPE)
2058 error ("new cannot be applied to a reference type");
2059 type = TREE_TYPE (type);
2062 if (TREE_CODE (type) == FUNCTION_TYPE)
2064 error ("new cannot be applied to a function type");
2065 return error_mark_node;
2068 /* When the object being created is an array, the new-expression yields a
2069 pointer to the initial element (if any) of the array. For example,
2070 both new int and new int[10] return an int*. 5.3.4. */
2071 if (TREE_CODE (type) == ARRAY_TYPE && has_array == 0)
2073 nelts = array_type_nelts_top (type);
2074 has_array = 1;
2075 type = TREE_TYPE (type);
2078 if (has_array)
2079 t = build_nt (ARRAY_REF, type, nelts);
2080 else
2081 t = type;
2083 rval = build (NEW_EXPR, build_pointer_type (type), placement, t, init);
2084 NEW_EXPR_USE_GLOBAL (rval) = use_global_new;
2085 TREE_SIDE_EFFECTS (rval) = 1;
2086 rval = build_new_1 (rval);
2087 if (rval == error_mark_node)
2088 return error_mark_node;
2090 /* Wrap it in a NOP_EXPR so warn_if_unused_value doesn't complain. */
2091 rval = build1 (NOP_EXPR, TREE_TYPE (rval), rval);
2092 TREE_NO_UNUSED_WARNING (rval) = 1;
2094 return rval;
2097 /* Given a Java class, return a decl for the corresponding java.lang.Class. */
2099 tree
2100 build_java_class_ref (type)
2101 tree type;
2103 tree name = NULL_TREE, class_decl;
2104 static tree CL_suffix = NULL_TREE;
2105 if (CL_suffix == NULL_TREE)
2106 CL_suffix = get_identifier("class$");
2107 if (jclass_node == NULL_TREE)
2109 jclass_node = IDENTIFIER_GLOBAL_VALUE (get_identifier ("jclass"));
2110 if (jclass_node == NULL_TREE)
2111 fatal_error ("call to Java constructor, while `jclass' undefined");
2113 jclass_node = TREE_TYPE (jclass_node);
2116 /* Mangle the class$ field */
2118 tree field;
2119 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
2120 if (DECL_NAME (field) == CL_suffix)
2122 mangle_decl (field);
2123 name = DECL_ASSEMBLER_NAME (field);
2124 break;
2126 if (!field)
2127 internal_error ("can't find class$");
2130 class_decl = IDENTIFIER_GLOBAL_VALUE (name);
2131 if (class_decl == NULL_TREE)
2133 class_decl = build_decl (VAR_DECL, name, TREE_TYPE (jclass_node));
2134 TREE_STATIC (class_decl) = 1;
2135 DECL_EXTERNAL (class_decl) = 1;
2136 TREE_PUBLIC (class_decl) = 1;
2137 DECL_ARTIFICIAL (class_decl) = 1;
2138 DECL_IGNORED_P (class_decl) = 1;
2139 pushdecl_top_level (class_decl);
2140 make_decl_rtl (class_decl, NULL);
2142 return class_decl;
2145 /* Returns the size of the cookie to use when allocating an array
2146 whose elements have the indicated TYPE. Assumes that it is already
2147 known that a cookie is needed. */
2149 static tree
2150 get_cookie_size (type)
2151 tree type;
2153 tree cookie_size;
2155 /* We need to allocate an additional max (sizeof (size_t), alignof
2156 (true_type)) bytes. */
2157 tree sizetype_size;
2158 tree type_align;
2160 sizetype_size = size_in_bytes (sizetype);
2161 type_align = size_int (TYPE_ALIGN_UNIT (type));
2162 if (INT_CST_LT_UNSIGNED (type_align, sizetype_size))
2163 cookie_size = sizetype_size;
2164 else
2165 cookie_size = type_align;
2167 return cookie_size;
2170 /* Called from cplus_expand_expr when expanding a NEW_EXPR. The return
2171 value is immediately handed to expand_expr. */
2173 static tree
2174 build_new_1 (exp)
2175 tree exp;
2177 tree placement, init;
2178 tree type, true_type, size, rval, t;
2179 tree full_type;
2180 tree nelts = NULL_TREE;
2181 tree alloc_call, alloc_expr, alloc_node;
2182 tree alloc_fn;
2183 tree cookie_expr, init_expr;
2184 int has_array = 0;
2185 enum tree_code code;
2186 int use_cookie, nothrow, check_new;
2187 /* Nonzero if the user wrote `::new' rather than just `new'. */
2188 int globally_qualified_p;
2189 /* Nonzero if we're going to call a global operator new, rather than
2190 a class-specific version. */
2191 int use_global_new;
2192 int use_java_new = 0;
2193 /* If non-NULL, the number of extra bytes to allocate at the
2194 beginning of the storage allocated for an array-new expression in
2195 order to store the number of elements. */
2196 tree cookie_size = NULL_TREE;
2197 /* True if the function we are calling is a placement allocation
2198 function. */
2199 bool placement_allocation_fn_p;
2201 placement = TREE_OPERAND (exp, 0);
2202 type = TREE_OPERAND (exp, 1);
2203 init = TREE_OPERAND (exp, 2);
2204 globally_qualified_p = NEW_EXPR_USE_GLOBAL (exp);
2206 if (TREE_CODE (type) == ARRAY_REF)
2208 has_array = 1;
2209 nelts = TREE_OPERAND (type, 1);
2210 type = TREE_OPERAND (type, 0);
2212 full_type = cp_build_binary_op (MINUS_EXPR, nelts, integer_one_node);
2213 full_type = build_index_type (full_type);
2214 full_type = build_cplus_array_type (type, full_type);
2216 else
2217 full_type = type;
2219 true_type = type;
2221 code = has_array ? VEC_NEW_EXPR : NEW_EXPR;
2223 /* If our base type is an array, then make sure we know how many elements
2224 it has. */
2225 while (TREE_CODE (true_type) == ARRAY_TYPE)
2227 tree this_nelts = array_type_nelts_top (true_type);
2228 nelts = cp_build_binary_op (MULT_EXPR, nelts, this_nelts);
2229 true_type = TREE_TYPE (true_type);
2232 if (!complete_type_or_else (true_type, exp))
2233 return error_mark_node;
2235 size = size_in_bytes (true_type);
2236 if (has_array)
2237 size = size_binop (MULT_EXPR, size, convert (sizetype, nelts));
2239 if (TREE_CODE (true_type) == VOID_TYPE)
2241 error ("invalid type `void' for new");
2242 return error_mark_node;
2245 if (abstract_virtuals_error (NULL_TREE, true_type))
2246 return error_mark_node;
2248 /* Figure out whether or not we're going to use the global operator
2249 new. */
2250 if (!globally_qualified_p
2251 && IS_AGGR_TYPE (true_type)
2252 && (has_array
2253 ? TYPE_HAS_ARRAY_NEW_OPERATOR (true_type)
2254 : TYPE_HAS_NEW_OPERATOR (true_type)))
2255 use_global_new = 0;
2256 else
2257 use_global_new = 1;
2259 /* We only need cookies for arrays containing types for which we
2260 need cookies. */
2261 if (!has_array || !TYPE_VEC_NEW_USES_COOKIE (true_type))
2262 use_cookie = 0;
2263 /* When using placement new, users may not realize that they need
2264 the extra storage. We require that the operator called be
2265 the global placement operator new[]. */
2266 else if (placement && !TREE_CHAIN (placement)
2267 && same_type_p (TREE_TYPE (TREE_VALUE (placement)),
2268 ptr_type_node))
2269 use_cookie = !use_global_new;
2270 /* Otherwise, we need the cookie. */
2271 else
2272 use_cookie = 1;
2274 /* Compute the number of extra bytes to allocate, now that we know
2275 whether or not we need the cookie. */
2276 if (use_cookie)
2278 cookie_size = get_cookie_size (true_type);
2279 size = size_binop (PLUS_EXPR, size, cookie_size);
2282 /* Allocate the object. */
2284 if (! placement && TYPE_FOR_JAVA (true_type))
2286 tree class_addr, alloc_decl;
2287 tree class_decl = build_java_class_ref (true_type);
2288 tree class_size = size_in_bytes (true_type);
2289 static const char alloc_name[] = "_Jv_AllocObject";
2290 use_java_new = 1;
2291 alloc_decl = IDENTIFIER_GLOBAL_VALUE (get_identifier (alloc_name));
2292 if (alloc_decl == NULL_TREE)
2293 fatal_error ("call to Java constructor with `%s' undefined",
2294 alloc_name);
2296 class_addr = build1 (ADDR_EXPR, jclass_node, class_decl);
2297 alloc_call = (build_function_call
2298 (alloc_decl,
2299 tree_cons (NULL_TREE, class_addr,
2300 build_tree_list (NULL_TREE, class_size))));
2302 else
2304 tree fnname;
2305 tree args;
2307 args = tree_cons (NULL_TREE, size, placement);
2308 fnname = ansi_opname (code);
2310 if (use_global_new)
2311 alloc_call = (build_new_function_call
2312 (lookup_function_nonclass (fnname, args),
2313 args));
2314 else
2315 alloc_call = build_method_call (build_dummy_object (true_type),
2316 fnname, args, NULL_TREE,
2317 LOOKUP_NORMAL);
2320 if (alloc_call == error_mark_node)
2321 return error_mark_node;
2323 /* The ALLOC_CALL should be a CALL_EXPR -- or a COMPOUND_EXPR whose
2324 right-hand-side is ultimately a CALL_EXPR -- and the first
2325 operand should be the address of a known FUNCTION_DECL. */
2326 t = alloc_call;
2327 while (TREE_CODE (t) == COMPOUND_EXPR)
2328 t = TREE_OPERAND (t, 1);
2329 alloc_fn = get_callee_fndecl (t);
2330 my_friendly_assert (alloc_fn != NULL_TREE, 20020325);
2331 /* Now, check to see if this function is actually a placement
2332 allocation function. This can happen even when PLACEMENT is NULL
2333 because we might have something like:
2335 struct S { void* operator new (size_t, int i = 0); };
2337 A call to `new S' will get this allocation function, even though
2338 there is no explicit placement argument. If there is more than
2339 one argument, or there are variable arguments, then this is a
2340 placement allocation function. */
2341 placement_allocation_fn_p
2342 = (type_num_arguments (TREE_TYPE (alloc_fn)) > 1
2343 || varargs_function_p (alloc_fn));
2345 /* unless an allocation function is declared with an empty excep-
2346 tion-specification (_except.spec_), throw(), it indicates failure to
2347 allocate storage by throwing a bad_alloc exception (clause _except_,
2348 _lib.bad.alloc_); it returns a non-null pointer otherwise If the allo-
2349 cation function is declared with an empty exception-specification,
2350 throw(), it returns null to indicate failure to allocate storage and a
2351 non-null pointer otherwise.
2353 So check for a null exception spec on the op new we just called. */
2355 nothrow = TYPE_NOTHROW_P (TREE_TYPE (alloc_fn));
2356 check_new = (flag_check_new || nothrow) && ! use_java_new;
2358 alloc_expr = alloc_call;
2360 if (use_cookie)
2361 /* Adjust so we're pointing to the start of the object. */
2362 alloc_expr = build (PLUS_EXPR, TREE_TYPE (alloc_expr),
2363 alloc_expr, cookie_size);
2365 /* While we're working, use a pointer to the type we've actually
2366 allocated. */
2367 alloc_expr = convert (build_pointer_type (full_type), alloc_expr);
2369 /* Now save the allocation expression so we only evaluate it once. */
2370 alloc_expr = get_target_expr (alloc_expr);
2371 alloc_node = TREE_OPERAND (alloc_expr, 0);
2373 /* Now initialize the cookie. */
2374 if (use_cookie)
2376 tree cookie;
2378 /* Store the number of bytes allocated so that we can know how
2379 many elements to destroy later. We use the last sizeof
2380 (size_t) bytes to store the number of elements. */
2381 cookie = build (MINUS_EXPR, build_pointer_type (sizetype),
2382 alloc_node, size_in_bytes (sizetype));
2383 cookie = build_indirect_ref (cookie, NULL);
2385 cookie_expr = build (MODIFY_EXPR, void_type_node, cookie, nelts);
2386 TREE_SIDE_EFFECTS (cookie_expr) = 1;
2388 else
2389 cookie_expr = NULL_TREE;
2391 /* Now initialize the allocated object. */
2392 init_expr = NULL_TREE;
2393 if (TYPE_NEEDS_CONSTRUCTING (type) || init)
2395 init_expr = build_indirect_ref (alloc_node, NULL);
2397 if (init == void_zero_node)
2398 init = build_default_init (full_type);
2399 else if (init && pedantic && has_array)
2400 pedwarn ("ISO C++ forbids initialization in array new");
2402 if (has_array)
2403 init_expr = build_vec_init (init_expr, init, 0);
2404 else if (TYPE_NEEDS_CONSTRUCTING (type))
2405 init_expr = build_method_call (init_expr,
2406 complete_ctor_identifier,
2407 init, TYPE_BINFO (true_type),
2408 LOOKUP_NORMAL);
2409 else
2411 /* We are processing something like `new int (10)', which
2412 means allocate an int, and initialize it with 10. */
2414 if (TREE_CODE (init) == TREE_LIST)
2416 if (TREE_CHAIN (init) != NULL_TREE)
2417 pedwarn
2418 ("initializer list being treated as compound expression");
2419 init = build_compound_expr (init);
2421 else if (TREE_CODE (init) == CONSTRUCTOR
2422 && TREE_TYPE (init) == NULL_TREE)
2424 pedwarn ("ISO C++ forbids aggregate initializer to new");
2425 init = digest_init (type, init, 0);
2428 init_expr = build_modify_expr (init_expr, INIT_EXPR, init);
2431 if (init_expr == error_mark_node)
2432 return error_mark_node;
2434 /* If any part of the object initialization terminates by throwing an
2435 exception and a suitable deallocation function can be found, the
2436 deallocation function is called to free the memory in which the
2437 object was being constructed, after which the exception continues
2438 to propagate in the context of the new-expression. If no
2439 unambiguous matching deallocation function can be found,
2440 propagating the exception does not cause the object's memory to be
2441 freed. */
2442 if (flag_exceptions && ! use_java_new)
2444 enum tree_code dcode = has_array ? VEC_DELETE_EXPR : DELETE_EXPR;
2445 tree cleanup;
2446 int flags = (LOOKUP_NORMAL
2447 | (globally_qualified_p * LOOKUP_GLOBAL));
2448 tree delete_node;
2450 if (use_cookie)
2451 /* Subtract the padding back out to get to the pointer returned
2452 from operator new. */
2453 delete_node = fold (build (MINUS_EXPR, TREE_TYPE (alloc_node),
2454 alloc_node, cookie_size));
2455 else
2456 delete_node = alloc_node;
2458 /* The Standard is unclear here, but the right thing to do
2459 is to use the same method for finding deallocation
2460 functions that we use for finding allocation functions. */
2461 flags |= LOOKUP_SPECULATIVELY;
2463 cleanup = build_op_delete_call (dcode, delete_node, size, flags,
2464 (placement_allocation_fn_p
2465 ? alloc_call : NULL_TREE));
2467 /* Ack! First we allocate the memory. Then we set our sentry
2468 variable to true, and expand a cleanup that deletes the memory
2469 if sentry is true. Then we run the constructor, and finally
2470 clear the sentry.
2472 It would be nice to be able to handle this without the sentry
2473 variable, perhaps with a TRY_CATCH_EXPR, but this doesn't
2474 work. We allocate the space first, so if there are any
2475 temporaries with cleanups in the constructor args we need this
2476 EH region to extend until end of full-expression to preserve
2477 nesting.
2479 If the backend had some mechanism so that we could force the
2480 allocation to be expanded after all the other args to the
2481 constructor, that would fix the nesting problem and we could
2482 do away with this complexity. But that would complicate other
2483 things; in particular, it would make it difficult to bail out
2484 if the allocation function returns null. */
2486 if (cleanup)
2488 tree end, sentry, begin;
2490 begin = get_target_expr (boolean_true_node);
2491 CLEANUP_EH_ONLY (begin) = 1;
2493 sentry = TARGET_EXPR_SLOT (begin);
2495 TARGET_EXPR_CLEANUP (begin)
2496 = build (COND_EXPR, void_type_node, sentry,
2497 cleanup, void_zero_node);
2499 end = build (MODIFY_EXPR, TREE_TYPE (sentry),
2500 sentry, boolean_false_node);
2502 init_expr
2503 = build (COMPOUND_EXPR, void_type_node, begin,
2504 build (COMPOUND_EXPR, void_type_node, init_expr,
2505 end));
2509 else if (CP_TYPE_CONST_P (true_type))
2510 error ("uninitialized const in `new' of `%#T'", true_type);
2512 /* Now build up the return value in reverse order. */
2514 rval = alloc_node;
2516 if (init_expr)
2517 rval = build (COMPOUND_EXPR, TREE_TYPE (rval), init_expr, rval);
2518 if (cookie_expr)
2519 rval = build (COMPOUND_EXPR, TREE_TYPE (rval), cookie_expr, rval);
2521 if (rval == alloc_node)
2522 /* If we didn't modify anything, strip the TARGET_EXPR and return the
2523 (adjusted) call. */
2524 rval = TREE_OPERAND (alloc_expr, 1);
2525 else
2527 if (check_new)
2529 tree ifexp = cp_build_binary_op (NE_EXPR, alloc_node,
2530 integer_zero_node);
2531 rval = build_conditional_expr (ifexp, rval, alloc_node);
2534 rval = build (COMPOUND_EXPR, TREE_TYPE (rval), alloc_expr, rval);
2537 /* Now strip the outer ARRAY_TYPE, so we return a pointer to the first
2538 element. */
2539 rval = convert (build_pointer_type (type), rval);
2541 return rval;
2544 static tree
2545 build_vec_delete_1 (base, maxindex, type, auto_delete_vec, use_global_delete)
2546 tree base, maxindex, type;
2547 special_function_kind auto_delete_vec;
2548 int use_global_delete;
2550 tree virtual_size;
2551 tree ptype = build_pointer_type (type = complete_type (type));
2552 tree size_exp = size_in_bytes (type);
2554 /* Temporary variables used by the loop. */
2555 tree tbase, tbase_init;
2557 /* This is the body of the loop that implements the deletion of a
2558 single element, and moves temp variables to next elements. */
2559 tree body;
2561 /* This is the LOOP_EXPR that governs the deletion of the elements. */
2562 tree loop;
2564 /* This is the thing that governs what to do after the loop has run. */
2565 tree deallocate_expr = 0;
2567 /* This is the BIND_EXPR which holds the outermost iterator of the
2568 loop. It is convenient to set this variable up and test it before
2569 executing any other code in the loop.
2570 This is also the containing expression returned by this function. */
2571 tree controller = NULL_TREE;
2573 if (! IS_AGGR_TYPE (type) || TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
2575 loop = integer_zero_node;
2576 goto no_destructor;
2579 /* The below is short by the cookie size. */
2580 virtual_size = size_binop (MULT_EXPR, size_exp,
2581 convert (sizetype, maxindex));
2583 tbase = create_temporary_var (ptype);
2584 tbase_init = build_modify_expr (tbase, NOP_EXPR,
2585 fold (build (PLUS_EXPR, ptype,
2586 base,
2587 virtual_size)));
2588 DECL_REGISTER (tbase) = 1;
2589 controller = build (BIND_EXPR, void_type_node, tbase, NULL_TREE, NULL_TREE);
2590 TREE_SIDE_EFFECTS (controller) = 1;
2592 body = NULL_TREE;
2594 body = tree_cons (NULL_TREE,
2595 build_delete (ptype, tbase, sfk_complete_destructor,
2596 LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 1),
2597 body);
2599 body = tree_cons (NULL_TREE,
2600 build_modify_expr (tbase, NOP_EXPR, build (MINUS_EXPR, ptype, tbase, size_exp)),
2601 body);
2603 body = tree_cons (NULL_TREE,
2604 build (EXIT_EXPR, void_type_node,
2605 build (EQ_EXPR, boolean_type_node, base, tbase)),
2606 body);
2608 loop = build (LOOP_EXPR, void_type_node, build_compound_expr (body));
2610 loop = tree_cons (NULL_TREE, tbase_init,
2611 tree_cons (NULL_TREE, loop, NULL_TREE));
2612 loop = build_compound_expr (loop);
2614 no_destructor:
2615 /* If the delete flag is one, or anything else with the low bit set,
2616 delete the storage. */
2617 deallocate_expr = integer_zero_node;
2618 if (auto_delete_vec != sfk_base_destructor)
2620 tree base_tbd;
2622 /* The below is short by the cookie size. */
2623 virtual_size = size_binop (MULT_EXPR, size_exp,
2624 convert (sizetype, maxindex));
2626 if (! TYPE_VEC_NEW_USES_COOKIE (type))
2627 /* no header */
2628 base_tbd = base;
2629 else
2631 tree cookie_size;
2633 cookie_size = get_cookie_size (type);
2634 base_tbd
2635 = cp_convert (ptype,
2636 cp_build_binary_op (MINUS_EXPR,
2637 cp_convert (string_type_node,
2638 base),
2639 cookie_size));
2640 /* True size with header. */
2641 virtual_size = size_binop (PLUS_EXPR, virtual_size, cookie_size);
2644 if (auto_delete_vec == sfk_deleting_destructor)
2645 deallocate_expr = build_x_delete (base_tbd,
2646 2 | use_global_delete,
2647 virtual_size);
2650 if (loop && deallocate_expr != integer_zero_node)
2652 body = tree_cons (NULL_TREE, loop,
2653 tree_cons (NULL_TREE, deallocate_expr, NULL_TREE));
2654 body = build_compound_expr (body);
2656 else
2657 body = loop;
2659 /* Outermost wrapper: If pointer is null, punt. */
2660 body = fold (build (COND_EXPR, void_type_node,
2661 fold (build (NE_EXPR, boolean_type_node, base,
2662 integer_zero_node)),
2663 body, integer_zero_node));
2664 body = build1 (NOP_EXPR, void_type_node, body);
2666 if (controller)
2668 TREE_OPERAND (controller, 1) = body;
2669 return controller;
2671 else
2672 return cp_convert (void_type_node, body);
2675 /* Create an unnamed variable of the indicated TYPE. */
2677 tree
2678 create_temporary_var (type)
2679 tree type;
2681 tree decl;
2683 decl = build_decl (VAR_DECL, NULL_TREE, type);
2684 TREE_USED (decl) = 1;
2685 DECL_ARTIFICIAL (decl) = 1;
2686 DECL_SOURCE_FILE (decl) = input_filename;
2687 DECL_SOURCE_LINE (decl) = lineno;
2688 DECL_IGNORED_P (decl) = 1;
2689 DECL_CONTEXT (decl) = current_function_decl;
2691 return decl;
2694 /* Create a new temporary variable of the indicated TYPE, initialized
2695 to INIT.
2697 It is not entered into current_binding_level, because that breaks
2698 things when it comes time to do final cleanups (which take place
2699 "outside" the binding contour of the function). */
2701 static tree
2702 get_temp_regvar (type, init)
2703 tree type, init;
2705 tree decl;
2707 decl = create_temporary_var (type);
2708 if (building_stmt_tree ())
2709 add_decl_stmt (decl);
2710 if (!building_stmt_tree ())
2711 SET_DECL_RTL (decl, assign_temp (type, 2, 0, 1));
2712 finish_expr_stmt (build_modify_expr (decl, INIT_EXPR, init));
2714 return decl;
2717 /* `build_vec_init' returns tree structure that performs
2718 initialization of a vector of aggregate types.
2720 BASE is a reference to the vector, of ARRAY_TYPE.
2721 INIT is the (possibly NULL) initializer.
2723 FROM_ARRAY is 0 if we should init everything with INIT
2724 (i.e., every element initialized from INIT).
2725 FROM_ARRAY is 1 if we should index into INIT in parallel
2726 with initialization of DECL.
2727 FROM_ARRAY is 2 if we should index into INIT in parallel,
2728 but use assignment instead of initialization. */
2730 tree
2731 build_vec_init (base, init, from_array)
2732 tree base, init;
2733 int from_array;
2735 tree rval;
2736 tree base2 = NULL_TREE;
2737 tree size;
2738 tree itype = NULL_TREE;
2739 tree iterator;
2740 /* The type of the array. */
2741 tree atype = TREE_TYPE (base);
2742 /* The type of an element in the array. */
2743 tree type = TREE_TYPE (atype);
2744 /* The type of a pointer to an element in the array. */
2745 tree ptype;
2746 tree stmt_expr;
2747 tree compound_stmt;
2748 int destroy_temps;
2749 tree try_block = NULL_TREE;
2750 tree try_body = NULL_TREE;
2751 int num_initialized_elts = 0;
2752 tree maxindex = array_type_nelts (TREE_TYPE (base));
2754 if (maxindex == error_mark_node)
2755 return error_mark_node;
2757 /* For g++.ext/arrnew.C. */
2758 if (init && TREE_CODE (init) == CONSTRUCTOR && TREE_TYPE (init) == NULL_TREE)
2759 init = digest_init (atype, init, 0);
2761 if (init && !TYPE_NEEDS_CONSTRUCTING (type)
2762 && ((TREE_CODE (init) == CONSTRUCTOR
2763 /* Don't do this if the CONSTRUCTOR might contain something
2764 that might throw and require us to clean up. */
2765 && (CONSTRUCTOR_ELTS (init) == NULL_TREE
2766 || ! TYPE_HAS_NONTRIVIAL_DESTRUCTOR (target_type (type))))
2767 || from_array))
2769 /* Do non-default initialization of POD arrays resulting from
2770 brace-enclosed initializers. In this case, digest_init and
2771 store_constructor will handle the semantics for us. */
2773 stmt_expr = build (INIT_EXPR, atype, base, init);
2774 TREE_SIDE_EFFECTS (stmt_expr) = 1;
2775 return stmt_expr;
2778 maxindex = cp_convert (ptrdiff_type_node, maxindex);
2779 ptype = build_pointer_type (type);
2780 size = size_in_bytes (type);
2781 if (TREE_CODE (TREE_TYPE (base)) == ARRAY_TYPE)
2782 base = cp_convert (ptype, default_conversion (base));
2784 /* The code we are generating looks like:
2786 T* t1 = (T*) base;
2787 T* rval = t1;
2788 ptrdiff_t iterator = maxindex;
2789 try {
2790 do {
2791 ... initialize *t1 ...
2792 ++t1;
2793 } while (--iterator != -1);
2794 } catch (...) {
2795 ... destroy elements that were constructed ...
2797 return rval;
2799 We can omit the try and catch blocks if we know that the
2800 initialization will never throw an exception, or if the array
2801 elements do not have destructors. We can omit the loop completely if
2802 the elements of the array do not have constructors.
2804 We actually wrap the entire body of the above in a STMT_EXPR, for
2805 tidiness.
2807 When copying from array to another, when the array elements have
2808 only trivial copy constructors, we should use __builtin_memcpy
2809 rather than generating a loop. That way, we could take advantage
2810 of whatever cleverness the back-end has for dealing with copies
2811 of blocks of memory. */
2813 begin_init_stmts (&stmt_expr, &compound_stmt);
2814 destroy_temps = stmts_are_full_exprs_p ();
2815 current_stmt_tree ()->stmts_are_full_exprs_p = 0;
2816 rval = get_temp_regvar (ptype, base);
2817 base = get_temp_regvar (ptype, rval);
2818 iterator = get_temp_regvar (ptrdiff_type_node, maxindex);
2820 /* Protect the entire array initialization so that we can destroy
2821 the partially constructed array if an exception is thrown.
2822 But don't do this if we're assigning. */
2823 if (flag_exceptions && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
2824 && from_array != 2)
2826 try_block = begin_try_block ();
2827 try_body = begin_compound_stmt (/*has_no_scope=*/1);
2830 if (init != NULL_TREE && TREE_CODE (init) == CONSTRUCTOR)
2832 /* Do non-default initialization of non-POD arrays resulting from
2833 brace-enclosed initializers. */
2835 tree elts;
2836 from_array = 0;
2838 for (elts = CONSTRUCTOR_ELTS (init); elts; elts = TREE_CHAIN (elts))
2840 tree elt = TREE_VALUE (elts);
2841 tree baseref = build1 (INDIRECT_REF, type, base);
2843 num_initialized_elts++;
2845 if (IS_AGGR_TYPE (type) || TREE_CODE (type) == ARRAY_TYPE)
2846 finish_expr_stmt (build_aggr_init (baseref, elt, 0));
2847 else
2848 finish_expr_stmt (build_modify_expr (baseref, NOP_EXPR,
2849 elt));
2851 finish_expr_stmt (build_unary_op (PREINCREMENT_EXPR, base, 0));
2852 finish_expr_stmt (build_unary_op (PREDECREMENT_EXPR, iterator, 0));
2855 /* Clear out INIT so that we don't get confused below. */
2856 init = NULL_TREE;
2858 else if (from_array)
2860 /* If initializing one array from another, initialize element by
2861 element. We rely upon the below calls the do argument
2862 checking. */
2863 if (init)
2865 base2 = default_conversion (init);
2866 itype = TREE_TYPE (base2);
2867 base2 = get_temp_regvar (itype, base2);
2868 itype = TREE_TYPE (itype);
2870 else if (TYPE_LANG_SPECIFIC (type)
2871 && TYPE_NEEDS_CONSTRUCTING (type)
2872 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
2874 error ("initializer ends prematurely");
2875 return error_mark_node;
2879 /* Now, default-initialize any remaining elements. We don't need to
2880 do that if a) the type does not need constructing, or b) we've
2881 already initialized all the elements.
2883 We do need to keep going if we're copying an array. */
2885 if (from_array
2886 || (TYPE_NEEDS_CONSTRUCTING (type)
2887 && ! (host_integerp (maxindex, 0)
2888 && (num_initialized_elts
2889 == tree_low_cst (maxindex, 0) + 1))))
2891 /* If the ITERATOR is equal to -1, then we don't have to loop;
2892 we've already initialized all the elements. */
2893 tree if_stmt;
2894 tree do_stmt;
2895 tree do_body;
2896 tree elt_init;
2898 if_stmt = begin_if_stmt ();
2899 finish_if_stmt_cond (build (NE_EXPR, boolean_type_node,
2900 iterator, integer_minus_one_node),
2901 if_stmt);
2903 /* Otherwise, loop through the elements. */
2904 do_stmt = begin_do_stmt ();
2905 do_body = begin_compound_stmt (/*has_no_scope=*/1);
2907 /* When we're not building a statement-tree, things are a little
2908 complicated. If, when we recursively call build_aggr_init,
2909 an expression containing a TARGET_EXPR is expanded, then it
2910 may get a cleanup. Then, the result of that expression is
2911 passed to finish_expr_stmt, which will call
2912 expand_start_target_temps/expand_end_target_temps. However,
2913 the latter call will not cause the cleanup to run because
2914 that block will still be on the block stack. So, we call
2915 expand_start_target_temps here manually; the corresponding
2916 call to expand_end_target_temps below will cause the cleanup
2917 to be performed. */
2918 if (!building_stmt_tree ())
2919 expand_start_target_temps ();
2921 if (from_array)
2923 tree to = build1 (INDIRECT_REF, type, base);
2924 tree from;
2926 if (base2)
2927 from = build1 (INDIRECT_REF, itype, base2);
2928 else
2929 from = NULL_TREE;
2931 if (from_array == 2)
2932 elt_init = build_modify_expr (to, NOP_EXPR, from);
2933 else if (TYPE_NEEDS_CONSTRUCTING (type))
2934 elt_init = build_aggr_init (to, from, 0);
2935 else if (from)
2936 elt_init = build_modify_expr (to, NOP_EXPR, from);
2937 else
2938 abort ();
2940 else if (TREE_CODE (type) == ARRAY_TYPE)
2942 if (init != 0)
2943 sorry
2944 ("cannot initialize multi-dimensional array with initializer");
2945 elt_init = build_vec_init (build1 (INDIRECT_REF, type, base),
2946 0, 0);
2948 else
2949 elt_init = build_aggr_init (build1 (INDIRECT_REF, type, base),
2950 init, 0);
2952 /* The initialization of each array element is a
2953 full-expression, as per core issue 124. */
2954 if (!building_stmt_tree ())
2956 genrtl_expr_stmt (elt_init);
2957 expand_end_target_temps ();
2959 else
2961 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
2962 finish_expr_stmt (elt_init);
2963 current_stmt_tree ()->stmts_are_full_exprs_p = 0;
2966 finish_expr_stmt (build_unary_op (PREINCREMENT_EXPR, base, 0));
2967 if (base2)
2968 finish_expr_stmt (build_unary_op (PREINCREMENT_EXPR, base2, 0));
2970 finish_compound_stmt (/*has_no_scope=*/1, do_body);
2971 finish_do_body (do_stmt);
2972 finish_do_stmt (build (NE_EXPR, boolean_type_node,
2973 build_unary_op (PREDECREMENT_EXPR, iterator, 0),
2974 integer_minus_one_node),
2975 do_stmt);
2977 finish_then_clause (if_stmt);
2978 finish_if_stmt ();
2981 /* Make sure to cleanup any partially constructed elements. */
2982 if (flag_exceptions && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
2983 && from_array != 2)
2985 tree e;
2987 finish_compound_stmt (/*has_no_scope=*/1, try_body);
2988 finish_cleanup_try_block (try_block);
2989 e = build_vec_delete_1 (rval,
2990 cp_build_binary_op (MINUS_EXPR, maxindex,
2991 iterator),
2992 type,
2993 sfk_base_destructor,
2994 /*use_global_delete=*/0);
2995 finish_cleanup (e, try_block);
2998 /* The value of the array initialization is the address of the
2999 first element in the array. */
3000 finish_expr_stmt (rval);
3002 stmt_expr = finish_init_stmts (stmt_expr, compound_stmt);
3003 current_stmt_tree ()->stmts_are_full_exprs_p = destroy_temps;
3004 return stmt_expr;
3007 /* Free up storage of type TYPE, at address ADDR.
3009 TYPE is a POINTER_TYPE and can be ptr_type_node for no special type
3010 of pointer.
3012 VIRTUAL_SIZE is the amount of storage that was allocated, and is
3013 used as the second argument to operator delete. It can include
3014 things like padding and magic size cookies. It has virtual in it,
3015 because if you have a base pointer and you delete through a virtual
3016 destructor, it should be the size of the dynamic object, not the
3017 static object, see Free Store 12.5 ISO C++.
3019 This does not call any destructors. */
3021 tree
3022 build_x_delete (addr, which_delete, virtual_size)
3023 tree addr;
3024 int which_delete;
3025 tree virtual_size;
3027 int use_global_delete = which_delete & 1;
3028 int use_vec_delete = !!(which_delete & 2);
3029 enum tree_code code = use_vec_delete ? VEC_DELETE_EXPR : DELETE_EXPR;
3030 int flags = LOOKUP_NORMAL | (use_global_delete * LOOKUP_GLOBAL);
3032 return build_op_delete_call (code, addr, virtual_size, flags, NULL_TREE);
3035 /* Call the DTOR_KIND destructor for EXP. FLAGS are as for
3036 build_delete. */
3038 static tree
3039 build_dtor_call (exp, dtor_kind, flags)
3040 tree exp;
3041 special_function_kind dtor_kind;
3042 int flags;
3044 tree name;
3046 switch (dtor_kind)
3048 case sfk_complete_destructor:
3049 name = complete_dtor_identifier;
3050 break;
3052 case sfk_base_destructor:
3053 name = base_dtor_identifier;
3054 break;
3056 case sfk_deleting_destructor:
3057 name = deleting_dtor_identifier;
3058 break;
3060 default:
3061 abort ();
3063 return build_method_call (exp, name, NULL_TREE, NULL_TREE, flags);
3066 /* Generate a call to a destructor. TYPE is the type to cast ADDR to.
3067 ADDR is an expression which yields the store to be destroyed.
3068 AUTO_DELETE is the name of the destructor to call, i.e., either
3069 sfk_complete_destructor, sfk_base_destructor, or
3070 sfk_deleting_destructor.
3072 FLAGS is the logical disjunction of zero or more LOOKUP_
3073 flags. See cp-tree.h for more info. */
3075 tree
3076 build_delete (type, addr, auto_delete, flags, use_global_delete)
3077 tree type, addr;
3078 special_function_kind auto_delete;
3079 int flags;
3080 int use_global_delete;
3082 tree expr;
3084 if (addr == error_mark_node)
3085 return error_mark_node;
3087 /* Can happen when CURRENT_EXCEPTION_OBJECT gets its type
3088 set to `error_mark_node' before it gets properly cleaned up. */
3089 if (type == error_mark_node)
3090 return error_mark_node;
3092 type = TYPE_MAIN_VARIANT (type);
3094 if (TREE_CODE (type) == POINTER_TYPE)
3096 type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
3097 if (!VOID_TYPE_P (type) && !complete_type_or_else (type, addr))
3098 return error_mark_node;
3099 if (TREE_CODE (type) == ARRAY_TYPE)
3100 goto handle_array;
3101 if (! IS_AGGR_TYPE (type))
3103 /* Call the builtin operator delete. */
3104 return build_builtin_delete_call (addr);
3106 if (TREE_SIDE_EFFECTS (addr))
3107 addr = save_expr (addr);
3109 /* throw away const and volatile on target type of addr */
3110 addr = convert_force (build_pointer_type (type), addr, 0);
3112 else if (TREE_CODE (type) == ARRAY_TYPE)
3114 handle_array:
3115 if (TREE_SIDE_EFFECTS (addr))
3116 addr = save_expr (addr);
3117 if (TYPE_DOMAIN (type) == NULL_TREE)
3119 error ("unknown array size in delete");
3120 return error_mark_node;
3122 return build_vec_delete (addr, array_type_nelts (type),
3123 auto_delete, use_global_delete);
3125 else
3127 /* Don't check PROTECT here; leave that decision to the
3128 destructor. If the destructor is accessible, call it,
3129 else report error. */
3130 addr = build_unary_op (ADDR_EXPR, addr, 0);
3131 if (TREE_SIDE_EFFECTS (addr))
3132 addr = save_expr (addr);
3134 addr = convert_force (build_pointer_type (type), addr, 0);
3137 my_friendly_assert (IS_AGGR_TYPE (type), 220);
3139 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
3141 if (auto_delete != sfk_deleting_destructor)
3142 return void_zero_node;
3144 return build_op_delete_call
3145 (DELETE_EXPR, addr, c_sizeof_nowarn (type),
3146 LOOKUP_NORMAL | (use_global_delete * LOOKUP_GLOBAL),
3147 NULL_TREE);
3149 else
3151 tree do_delete = NULL_TREE;
3152 tree ifexp;
3154 my_friendly_assert (TYPE_HAS_DESTRUCTOR (type), 20011213);
3156 /* For `::delete x', we must not use the deleting destructor
3157 since then we would not be sure to get the global `operator
3158 delete'. */
3159 if (use_global_delete && auto_delete == sfk_deleting_destructor)
3161 /* We will use ADDR multiple times so we must save it. */
3162 addr = save_expr (addr);
3163 /* Delete the object. */
3164 do_delete = build_builtin_delete_call (addr);
3165 /* Otherwise, treat this like a complete object destructor
3166 call. */
3167 auto_delete = sfk_complete_destructor;
3169 /* If the destructor is non-virtual, there is no deleting
3170 variant. Instead, we must explicitly call the appropriate
3171 `operator delete' here. */
3172 else if (!DECL_VIRTUAL_P (CLASSTYPE_DESTRUCTORS (type))
3173 && auto_delete == sfk_deleting_destructor)
3175 /* We will use ADDR multiple times so we must save it. */
3176 addr = save_expr (addr);
3177 /* Build the call. */
3178 do_delete = build_op_delete_call (DELETE_EXPR,
3179 addr,
3180 c_sizeof_nowarn (type),
3181 LOOKUP_NORMAL,
3182 NULL_TREE);
3183 /* Call the complete object destructor. */
3184 auto_delete = sfk_complete_destructor;
3186 else if (auto_delete == sfk_deleting_destructor
3187 && TYPE_GETS_REG_DELETE (type))
3189 /* Make sure we have access to the member op delete, even though
3190 we'll actually be calling it from the destructor. */
3191 build_op_delete_call (DELETE_EXPR, addr, c_sizeof_nowarn (type),
3192 LOOKUP_NORMAL, NULL_TREE);
3195 expr = build_dtor_call (build_indirect_ref (addr, NULL),
3196 auto_delete, flags);
3197 if (do_delete)
3198 expr = build (COMPOUND_EXPR, void_type_node, expr, do_delete);
3200 if (flags & LOOKUP_DESTRUCTOR)
3201 /* Explicit destructor call; don't check for null pointer. */
3202 ifexp = integer_one_node;
3203 else
3204 /* Handle deleting a null pointer. */
3205 ifexp = fold (cp_build_binary_op (NE_EXPR, addr, integer_zero_node));
3207 if (ifexp != integer_one_node)
3208 expr = build (COND_EXPR, void_type_node,
3209 ifexp, expr, void_zero_node);
3211 return expr;
3215 /* At the beginning of a destructor, push cleanups that will call the
3216 destructors for our base classes and members.
3218 Called from begin_destructor_body. */
3220 void
3221 push_base_cleanups ()
3223 tree binfos;
3224 int i, n_baseclasses;
3225 tree member;
3226 tree expr;
3228 /* Run destructors for all virtual baseclasses. */
3229 if (TYPE_USES_VIRTUAL_BASECLASSES (current_class_type))
3231 tree vbases;
3232 tree cond = (condition_conversion
3233 (build (BIT_AND_EXPR, integer_type_node,
3234 current_in_charge_parm,
3235 integer_two_node)));
3237 vbases = CLASSTYPE_VBASECLASSES (current_class_type);
3238 /* The CLASSTYPE_VBASECLASSES list is in initialization
3239 order, which is also the right order for pushing cleanups. */
3240 for (; vbases;
3241 vbases = TREE_CHAIN (vbases))
3243 tree vbase = TREE_VALUE (vbases);
3244 tree base_type = BINFO_TYPE (vbase);
3246 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (base_type))
3248 expr = build_scoped_method_call (current_class_ref, vbase,
3249 base_dtor_identifier,
3250 NULL_TREE);
3251 expr = build (COND_EXPR, void_type_node, cond,
3252 expr, void_zero_node);
3253 finish_decl_cleanup (NULL_TREE, expr);
3258 binfos = BINFO_BASETYPES (TYPE_BINFO (current_class_type));
3259 n_baseclasses = CLASSTYPE_N_BASECLASSES (current_class_type);
3261 /* Take care of the remaining baseclasses. */
3262 for (i = 0; i < n_baseclasses; i++)
3264 tree base_binfo = TREE_VEC_ELT (binfos, i);
3265 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (BINFO_TYPE (base_binfo))
3266 || TREE_VIA_VIRTUAL (base_binfo))
3267 continue;
3269 expr = build_scoped_method_call (current_class_ref, base_binfo,
3270 base_dtor_identifier,
3271 NULL_TREE);
3273 finish_decl_cleanup (NULL_TREE, expr);
3276 for (member = TYPE_FIELDS (current_class_type); member;
3277 member = TREE_CHAIN (member))
3279 if (TREE_CODE (member) != FIELD_DECL)
3280 continue;
3281 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (member)))
3283 tree this_member = (build_component_ref
3284 (current_class_ref, DECL_NAME (member),
3285 NULL_TREE, 0));
3286 tree this_type = TREE_TYPE (member);
3287 expr = build_delete (this_type, this_member,
3288 sfk_complete_destructor,
3289 LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR|LOOKUP_NORMAL,
3291 finish_decl_cleanup (NULL_TREE, expr);
3296 /* For type TYPE, delete the virtual baseclass objects of DECL. */
3298 tree
3299 build_vbase_delete (type, decl)
3300 tree type, decl;
3302 tree vbases = CLASSTYPE_VBASECLASSES (type);
3303 tree result = NULL_TREE;
3304 tree addr = build_unary_op (ADDR_EXPR, decl, 0);
3306 my_friendly_assert (addr != error_mark_node, 222);
3308 while (vbases)
3310 tree this_addr
3311 = convert_force (build_pointer_type (BINFO_TYPE (TREE_VALUE (vbases))),
3312 addr, 0);
3313 result = tree_cons (NULL_TREE,
3314 build_delete (TREE_TYPE (this_addr), this_addr,
3315 sfk_base_destructor,
3316 LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 0),
3317 result);
3318 vbases = TREE_CHAIN (vbases);
3320 return build_compound_expr (nreverse (result));
3323 /* Build a C++ vector delete expression.
3324 MAXINDEX is the number of elements to be deleted.
3325 ELT_SIZE is the nominal size of each element in the vector.
3326 BASE is the expression that should yield the store to be deleted.
3327 This function expands (or synthesizes) these calls itself.
3328 AUTO_DELETE_VEC says whether the container (vector) should be deallocated.
3330 This also calls delete for virtual baseclasses of elements of the vector.
3332 Update: MAXINDEX is no longer needed. The size can be extracted from the
3333 start of the vector for pointers, and from the type for arrays. We still
3334 use MAXINDEX for arrays because it happens to already have one of the
3335 values we'd have to extract. (We could use MAXINDEX with pointers to
3336 confirm the size, and trap if the numbers differ; not clear that it'd
3337 be worth bothering.) */
3339 tree
3340 build_vec_delete (base, maxindex, auto_delete_vec, use_global_delete)
3341 tree base, maxindex;
3342 special_function_kind auto_delete_vec;
3343 int use_global_delete;
3345 tree type;
3347 if (TREE_CODE (base) == OFFSET_REF)
3348 base = resolve_offset_ref (base);
3350 type = TREE_TYPE (base);
3352 base = stabilize_reference (base);
3354 /* Since we can use base many times, save_expr it. */
3355 if (TREE_SIDE_EFFECTS (base))
3356 base = save_expr (base);
3358 if (TREE_CODE (type) == POINTER_TYPE)
3360 /* Step back one from start of vector, and read dimension. */
3361 tree cookie_addr;
3363 type = strip_array_types (TREE_TYPE (type));
3364 cookie_addr = build (MINUS_EXPR,
3365 build_pointer_type (sizetype),
3366 base,
3367 TYPE_SIZE_UNIT (sizetype));
3368 maxindex = build_indirect_ref (cookie_addr, NULL);
3370 else if (TREE_CODE (type) == ARRAY_TYPE)
3372 /* get the total number of things in the array, maxindex is a bad name */
3373 maxindex = array_type_nelts_total (type);
3374 type = strip_array_types (type);
3375 base = build_unary_op (ADDR_EXPR, base, 1);
3377 else
3379 if (base != error_mark_node)
3380 error ("type to vector delete is neither pointer or array type");
3381 return error_mark_node;
3384 return build_vec_delete_1 (base, maxindex, type, auto_delete_vec,
3385 use_global_delete);