* init.c (build_default_init): Remove unused variable.
[official-gcc.git] / gcc / cp / init.c
blob526d68f390b2b396276789ab45b84cd5a7e07f0a
1 /* Handle initialization things in C++.
2 Copyright (C) 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002 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 /* Types containing pointers to data members cannot be
160 zero-initialized with zeros, because the NULL value for such
161 pointers is -1.
163 TYPE is a type that requires such zero initialization. The
164 returned value is the initializer. */
166 tree
167 build_forced_zero_init (type)
168 tree type;
170 tree init = NULL;
172 if (AGGREGATE_TYPE_P (type) && !TYPE_PTRMEMFUNC_P (type))
174 /* This is a default initialization of an aggregate, but not one of
175 non-POD class type. We cleverly notice that the initialization
176 rules in such a case are the same as for initialization with an
177 empty brace-initialization list. */
178 init = build (CONSTRUCTOR, NULL_TREE, NULL_TREE, NULL_TREE);
180 else if (TREE_CODE (type) == REFERENCE_TYPE)
181 /* --if T is a reference type, no initialization is performed. */
182 return NULL_TREE;
183 else
185 init = integer_zero_node;
187 if (TREE_CODE (type) == ENUMERAL_TYPE)
188 /* We must make enumeral types the right type. */
189 init = fold (build1 (NOP_EXPR, type, init));
192 init = digest_init (type, init, 0);
194 return init;
197 /* [dcl.init]:
199 To default-initialize an object of type T means:
201 --if T is a non-POD class type (clause _class_), the default construc-
202 tor for T is called (and the initialization is ill-formed if T has
203 no accessible default constructor);
205 --if T is an array type, each element is default-initialized;
207 --otherwise, the storage for the object is zero-initialized.
209 A program that calls for default-initialization of an entity of refer-
210 ence type is ill-formed. */
212 static tree
213 build_default_init (type)
214 tree type;
216 if (TYPE_NEEDS_CONSTRUCTING (type))
217 /* Other code will handle running the default constructor. We can't do
218 anything with a CONSTRUCTOR for arrays here, as that would imply
219 copy-initialization. */
220 return NULL_TREE;
222 return build_forced_zero_init (type);
225 /* Subroutine of emit_base_init. */
227 static void
228 perform_member_init (member, init, explicit)
229 tree member, init;
230 int explicit;
232 tree decl;
233 tree type = TREE_TYPE (member);
235 decl = build_component_ref (current_class_ref, member, NULL_TREE, explicit);
237 if (decl == error_mark_node)
238 return;
240 /* Deal with this here, as we will get confused if we try to call the
241 assignment op for an anonymous union. This can happen in a
242 synthesized copy constructor. */
243 if (ANON_AGGR_TYPE_P (type))
245 if (init)
247 init = build (INIT_EXPR, type, decl, TREE_VALUE (init));
248 finish_expr_stmt (init);
251 else if (TYPE_NEEDS_CONSTRUCTING (type)
252 || (init && TYPE_HAS_CONSTRUCTOR (type)))
254 /* Since `init' is already a TREE_LIST on the member_init_list,
255 only build it into one if we aren't already a list. */
256 if (init != NULL_TREE && TREE_CODE (init) != TREE_LIST)
257 init = build_tree_list (NULL_TREE, init);
259 if (explicit
260 && TREE_CODE (type) == ARRAY_TYPE
261 && init != NULL_TREE
262 && TREE_CHAIN (init) == NULL_TREE
263 && TREE_CODE (TREE_TYPE (TREE_VALUE (init))) == ARRAY_TYPE)
265 /* Initialization of one array from another. */
266 finish_expr_stmt (build_vec_init (decl, TREE_VALUE (init), 1));
268 else
269 finish_expr_stmt (build_aggr_init (decl, init, 0));
271 else
273 if (init == NULL_TREE)
275 if (explicit)
277 init = build_default_init (type);
278 if (TREE_CODE (type) == REFERENCE_TYPE)
279 warning
280 ("default-initialization of `%#D', which has reference type",
281 member);
283 /* member traversal: note it leaves init NULL */
284 else if (TREE_CODE (type) == REFERENCE_TYPE)
285 pedwarn ("uninitialized reference member `%D'", member);
287 else if (TREE_CODE (init) == TREE_LIST)
289 /* There was an explicit member initialization. Do some
290 work in that case. */
291 if (TREE_CHAIN (init))
293 warning ("initializer list treated as compound expression");
294 init = build_compound_expr (init);
296 else
297 init = TREE_VALUE (init);
300 if (init)
301 finish_expr_stmt (build_modify_expr (decl, INIT_EXPR, init));
304 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
306 tree expr;
308 expr = build_component_ref (current_class_ref, member, NULL_TREE,
309 explicit);
310 expr = build_delete (type, expr, sfk_complete_destructor,
311 LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR, 0);
313 if (expr != error_mark_node)
314 finish_eh_cleanup (expr);
318 /* Returns a TREE_LIST containing (as the TREE_PURPOSE of each node) all
319 the FIELD_DECLs on the TYPE_FIELDS list for T, in reverse order. */
321 static tree
322 build_field_list (t, list, uses_unions_p)
323 tree t;
324 tree list;
325 int *uses_unions_p;
327 tree fields;
329 /* Note whether or not T is a union. */
330 if (TREE_CODE (t) == UNION_TYPE)
331 *uses_unions_p = 1;
333 for (fields = TYPE_FIELDS (t); fields; fields = TREE_CHAIN (fields))
335 /* Skip CONST_DECLs for enumeration constants and so forth. */
336 if (TREE_CODE (fields) != FIELD_DECL)
337 continue;
339 /* Keep track of whether or not any fields are unions. */
340 if (TREE_CODE (TREE_TYPE (fields)) == UNION_TYPE)
341 *uses_unions_p = 1;
343 /* For an anonymous struct or union, we must recursively
344 consider the fields of the anonymous type. They can be
345 directly initialized from the constructor. */
346 if (ANON_AGGR_TYPE_P (TREE_TYPE (fields)))
348 /* Add this field itself. Synthesized copy constructors
349 initialize the entire aggregate. */
350 list = tree_cons (fields, NULL_TREE, list);
351 /* And now add the fields in the anonymous aggregate. */
352 list = build_field_list (TREE_TYPE (fields), list,
353 uses_unions_p);
355 /* Add this field. */
356 else if (DECL_NAME (fields))
357 list = tree_cons (fields, NULL_TREE, list);
360 return list;
363 /* The MEMBER_INIT_LIST is a TREE_LIST. The TREE_PURPOSE of each list
364 gives a FIELD_DECL in T that needs initialization. The TREE_VALUE
365 gives the initializer, or list of initializer arguments. Sort the
366 MEMBER_INIT_LIST, returning a version that contains the same
367 information but in the order that the fields should actually be
368 initialized. Perform error-checking in the process. */
370 static tree
371 sort_member_init (t, member_init_list)
372 tree t;
373 tree member_init_list;
375 tree init_list;
376 tree last_field;
377 tree init;
378 int uses_unions_p;
380 /* Build up a list of the various fields, in sorted order. */
381 init_list = nreverse (build_field_list (t, NULL_TREE, &uses_unions_p));
383 /* Go through the explicit initializers, adding them to the
384 INIT_LIST. */
385 last_field = init_list;
386 for (init = member_init_list; init; init = TREE_CHAIN (init))
388 tree f;
389 tree initialized_field;
391 initialized_field = TREE_PURPOSE (init);
392 my_friendly_assert (TREE_CODE (initialized_field) == FIELD_DECL,
393 20000516);
395 /* If the explicit initializers are in sorted order, then the
396 INITIALIZED_FIELD will be for a field following the
397 LAST_FIELD. */
398 for (f = last_field; f; f = TREE_CHAIN (f))
399 if (TREE_PURPOSE (f) == initialized_field)
400 break;
402 /* Give a warning, if appropriate. */
403 if (warn_reorder && !f)
405 cp_warning_at ("member initializers for `%#D'",
406 TREE_PURPOSE (last_field));
407 cp_warning_at (" and `%#D'", initialized_field);
408 warning (" will be re-ordered to match declaration order");
411 /* Look again, from the beginning of the list. We must find the
412 field on this loop. */
413 if (!f)
415 f = init_list;
416 while (TREE_PURPOSE (f) != initialized_field)
417 f = TREE_CHAIN (f);
420 /* If there was already an explicit initializer for this field,
421 issue an error. */
422 if (TREE_TYPE (f))
423 error ("multiple initializations given for member `%D'",
424 initialized_field);
425 else
427 /* Mark the field as explicitly initialized. */
428 TREE_TYPE (f) = error_mark_node;
429 /* And insert the initializer. */
430 TREE_VALUE (f) = TREE_VALUE (init);
433 /* Remember the location of the last explicitly initialized
434 field. */
435 last_field = f;
438 /* [class.base.init]
440 If a ctor-initializer specifies more than one mem-initializer for
441 multiple members of the same union (including members of
442 anonymous unions), the ctor-initializer is ill-formed. */
443 if (uses_unions_p)
445 last_field = NULL_TREE;
446 for (init = init_list; init; init = TREE_CHAIN (init))
448 tree field;
449 tree field_type;
450 int done;
452 /* Skip uninitialized members. */
453 if (!TREE_TYPE (init))
454 continue;
455 /* See if this field is a member of a union, or a member of a
456 structure contained in a union, etc. */
457 field = TREE_PURPOSE (init);
458 for (field_type = DECL_CONTEXT (field);
459 !same_type_p (field_type, t);
460 field_type = TYPE_CONTEXT (field_type))
461 if (TREE_CODE (field_type) == UNION_TYPE)
462 break;
463 /* If this field is not a member of a union, skip it. */
464 if (TREE_CODE (field_type) != UNION_TYPE)
465 continue;
467 /* It's only an error if we have two initializers for the same
468 union type. */
469 if (!last_field)
471 last_field = field;
472 continue;
475 /* See if LAST_FIELD and the field initialized by INIT are
476 members of the same union. If so, there's a problem,
477 unless they're actually members of the same structure
478 which is itself a member of a union. For example, given:
480 union { struct { int i; int j; }; };
482 initializing both `i' and `j' makes sense. */
483 field_type = DECL_CONTEXT (field);
484 done = 0;
487 tree last_field_type;
489 last_field_type = DECL_CONTEXT (last_field);
490 while (1)
492 if (same_type_p (last_field_type, field_type))
494 if (TREE_CODE (field_type) == UNION_TYPE)
495 error ("initializations for multiple members of `%T'",
496 last_field_type);
497 done = 1;
498 break;
501 if (same_type_p (last_field_type, t))
502 break;
504 last_field_type = TYPE_CONTEXT (last_field_type);
507 /* If we've reached the outermost class, then we're
508 done. */
509 if (same_type_p (field_type, t))
510 break;
512 field_type = TYPE_CONTEXT (field_type);
514 while (!done);
516 last_field = field;
520 return init_list;
523 /* Like sort_member_init, but used for initializers of base classes.
524 *RBASE_PTR is filled in with the initializers for non-virtual bases;
525 vbase_ptr gets the virtual bases. */
527 static void
528 sort_base_init (t, base_init_list, rbase_ptr, vbase_ptr)
529 tree t;
530 tree base_init_list;
531 tree *rbase_ptr, *vbase_ptr;
533 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
534 int n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
536 int i;
537 tree x;
538 tree last;
540 /* For warn_reorder. */
541 int last_pos = 0;
542 tree last_base = NULL_TREE;
544 tree rbases = NULL_TREE;
545 tree vbases = NULL_TREE;
547 /* First walk through and splice out vbase and invalid initializers.
548 Also replace types with binfos. */
550 last = tree_cons (NULL_TREE, NULL_TREE, base_init_list);
551 for (x = TREE_CHAIN (last); x; x = TREE_CHAIN (x))
553 tree basetype = TREE_PURPOSE (x);
554 tree binfo = (TREE_CODE (basetype) == TREE_VEC
555 ? basetype : binfo_or_else (basetype, t));
557 if (binfo == NULL_TREE)
558 /* BASETYPE might be an inaccessible direct base (because it
559 is also an indirect base). */
560 continue;
562 if (TREE_VIA_VIRTUAL (binfo))
564 /* Virtual base classes are special cases. Their
565 initializers are recorded with this constructor, and they
566 are used when this constructor is the top-level
567 constructor called. */
568 tree v = binfo_for_vbase (BINFO_TYPE (binfo), t);
569 vbases = tree_cons (v, TREE_VALUE (x), vbases);
571 else
573 /* Otherwise, it must be an immediate base class. */
574 my_friendly_assert
575 (same_type_p (BINFO_TYPE (BINFO_INHERITANCE_CHAIN (binfo)),
576 t), 20011113);
578 TREE_PURPOSE (x) = binfo;
579 TREE_CHAIN (last) = x;
580 last = x;
583 TREE_CHAIN (last) = NULL_TREE;
585 /* Now walk through our regular bases and make sure they're initialized. */
587 for (i = 0; i < n_baseclasses; ++i)
589 /* The base for which we're currently initializing. */
590 tree base_binfo = TREE_VEC_ELT (binfos, i);
591 /* The initializer for BASE_BINFO. */
592 tree init;
593 int pos;
595 if (TREE_VIA_VIRTUAL (base_binfo))
596 continue;
598 /* We haven't found the BASE_BINFO yet. */
599 init = NULL_TREE;
600 /* Loop through all the explicitly initialized bases, looking
601 for an appropriate initializer. */
602 for (x = base_init_list, pos = 0; x; x = TREE_CHAIN (x), ++pos)
604 tree binfo = TREE_PURPOSE (x);
606 if (binfo == base_binfo && !init)
608 if (warn_reorder)
610 if (pos < last_pos)
612 cp_warning_at ("base initializers for `%#T'", last_base);
613 cp_warning_at (" and `%#T'", BINFO_TYPE (binfo));
614 warning (" will be re-ordered to match inheritance order");
616 last_pos = pos;
617 last_base = BINFO_TYPE (binfo);
620 /* Make sure we won't try to work on this init again. */
621 TREE_PURPOSE (x) = NULL_TREE;
622 init = build_tree_list (binfo, TREE_VALUE (x));
624 else if (binfo == base_binfo)
626 error ("base class `%T' already initialized",
627 BINFO_TYPE (binfo));
628 break;
632 /* If we didn't find BASE_BINFO in the list, create a dummy entry
633 so the two lists (RBASES and the list of bases) will be
634 symmetrical. */
635 if (!init)
636 init = build_tree_list (NULL_TREE, NULL_TREE);
637 rbases = chainon (rbases, init);
640 *rbase_ptr = rbases;
641 *vbase_ptr = vbases;
644 /* Perform whatever initializations have yet to be done on the base
645 class, and non-static data members, of the CURRENT_CLASS_TYPE.
646 These actions are given by the BASE_INIT_LIST and MEM_INIT_LIST,
647 respectively.
649 If there is a need for a call to a constructor, we must surround
650 that call with a pushlevel/poplevel pair, since we are technically
651 at the PARM level of scope. */
653 void
654 emit_base_init (mem_init_list, base_init_list)
655 tree mem_init_list;
656 tree base_init_list;
658 tree member;
659 tree rbase_init_list, vbase_init_list;
660 tree t = current_class_type;
661 tree t_binfo = TYPE_BINFO (t);
662 tree binfos = BINFO_BASETYPES (t_binfo);
663 int i;
664 int n_baseclasses = BINFO_N_BASETYPES (t_binfo);
666 mem_init_list = sort_member_init (t, mem_init_list);
667 sort_base_init (t, base_init_list, &rbase_init_list, &vbase_init_list);
669 /* First, initialize the virtual base classes, if we are
670 constructing the most-derived object. */
671 if (TYPE_USES_VIRTUAL_BASECLASSES (t))
673 tree first_arg = TREE_CHAIN (DECL_ARGUMENTS (current_function_decl));
674 construct_virtual_bases (t, current_class_ref, current_class_ptr,
675 vbase_init_list, first_arg);
678 /* Now, perform initialization of non-virtual base classes. */
679 for (i = 0; i < n_baseclasses; i++)
681 tree base_binfo = TREE_VEC_ELT (binfos, i);
682 tree init = void_list_node;
684 if (TREE_VIA_VIRTUAL (base_binfo))
685 continue;
687 my_friendly_assert (BINFO_INHERITANCE_CHAIN (base_binfo) == t_binfo,
688 999);
690 if (TREE_PURPOSE (rbase_init_list))
691 init = TREE_VALUE (rbase_init_list);
692 else if (TYPE_NEEDS_CONSTRUCTING (BINFO_TYPE (base_binfo)))
694 init = NULL_TREE;
695 if (extra_warnings
696 && DECL_COPY_CONSTRUCTOR_P (current_function_decl))
697 warning ("base class `%#T' should be explicitly initialized in the copy constructor",
698 BINFO_TYPE (base_binfo));
701 if (init != void_list_node)
703 member = build_base_path (PLUS_EXPR, current_class_ptr,
704 base_binfo, 1);
705 expand_aggr_init_1 (base_binfo, NULL_TREE,
706 build_indirect_ref (member, NULL), init,
707 LOOKUP_NORMAL);
710 expand_cleanup_for_base (base_binfo, NULL_TREE);
711 rbase_init_list = TREE_CHAIN (rbase_init_list);
714 /* Initialize the vtable pointers for the class. */
715 initialize_vtbl_ptrs (current_class_ptr);
717 while (mem_init_list)
719 tree init;
720 tree member;
721 int from_init_list;
723 member = TREE_PURPOSE (mem_init_list);
725 /* See if we had a user-specified member initialization. */
726 if (TREE_TYPE (mem_init_list))
728 init = TREE_VALUE (mem_init_list);
729 from_init_list = 1;
731 else
733 init = DECL_INITIAL (member);
734 from_init_list = 0;
736 /* Effective C++ rule 12. */
737 if (warn_ecpp && init == NULL_TREE
738 && !DECL_ARTIFICIAL (member)
739 && TREE_CODE (TREE_TYPE (member)) != ARRAY_TYPE)
740 warning ("`%D' should be initialized in the member initialization list", member);
743 perform_member_init (member, init, from_init_list);
744 mem_init_list = TREE_CHAIN (mem_init_list);
748 /* Returns the address of the vtable (i.e., the value that should be
749 assigned to the vptr) for BINFO. */
751 static tree
752 build_vtbl_address (binfo)
753 tree binfo;
755 tree binfo_for = binfo;
756 tree vtbl;
758 if (BINFO_VPTR_INDEX (binfo) && TREE_VIA_VIRTUAL (binfo)
759 && BINFO_PRIMARY_P (binfo))
760 /* If this is a virtual primary base, then the vtable we want to store
761 is that for the base this is being used as the primary base of. We
762 can't simply skip the initialization, because we may be expanding the
763 inits of a subobject constructor where the virtual base layout
764 can be different. */
765 while (BINFO_PRIMARY_BASE_OF (binfo_for))
766 binfo_for = BINFO_PRIMARY_BASE_OF (binfo_for);
768 /* Figure out what vtable BINFO's vtable is based on, and mark it as
769 used. */
770 vtbl = get_vtbl_decl_for_binfo (binfo_for);
771 assemble_external (vtbl);
772 TREE_USED (vtbl) = 1;
774 /* Now compute the address to use when initializing the vptr. */
775 vtbl = BINFO_VTABLE (binfo_for);
776 if (TREE_CODE (vtbl) == VAR_DECL)
778 vtbl = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (vtbl)), vtbl);
779 TREE_CONSTANT (vtbl) = 1;
782 return vtbl;
785 /* This code sets up the virtual function tables appropriate for
786 the pointer DECL. It is a one-ply initialization.
788 BINFO is the exact type that DECL is supposed to be. In
789 multiple inheritance, this might mean "C's A" if C : A, B. */
791 static void
792 expand_virtual_init (binfo, decl)
793 tree binfo, decl;
795 tree vtbl, vtbl_ptr;
796 tree vtt_index;
798 /* Compute the initializer for vptr. */
799 vtbl = build_vtbl_address (binfo);
801 /* We may get this vptr from a VTT, if this is a subobject
802 constructor or subobject destructor. */
803 vtt_index = BINFO_VPTR_INDEX (binfo);
804 if (vtt_index)
806 tree vtbl2;
807 tree vtt_parm;
809 /* Compute the value to use, when there's a VTT. */
810 vtt_parm = current_vtt_parm;
811 vtbl2 = build (PLUS_EXPR,
812 TREE_TYPE (vtt_parm),
813 vtt_parm,
814 vtt_index);
815 vtbl2 = build1 (INDIRECT_REF, TREE_TYPE (vtbl), vtbl2);
817 /* The actual initializer is the VTT value only in the subobject
818 constructor. In maybe_clone_body we'll substitute NULL for
819 the vtt_parm in the case of the non-subobject constructor. */
820 vtbl = build (COND_EXPR,
821 TREE_TYPE (vtbl),
822 build (EQ_EXPR, boolean_type_node,
823 current_in_charge_parm, integer_zero_node),
824 vtbl2,
825 vtbl);
828 /* Compute the location of the vtpr. */
829 vtbl_ptr = build_vfield_ref (build_indirect_ref (decl, NULL),
830 TREE_TYPE (binfo));
831 my_friendly_assert (vtbl_ptr != error_mark_node, 20010730);
833 /* Assign the vtable to the vptr. */
834 vtbl = convert_force (TREE_TYPE (vtbl_ptr), vtbl, 0);
835 finish_expr_stmt (build_modify_expr (vtbl_ptr, NOP_EXPR, vtbl));
838 /* If an exception is thrown in a constructor, those base classes already
839 constructed must be destroyed. This function creates the cleanup
840 for BINFO, which has just been constructed. If FLAG is non-NULL,
841 it is a DECL which is non-zero when this base needs to be
842 destroyed. */
844 static void
845 expand_cleanup_for_base (binfo, flag)
846 tree binfo;
847 tree flag;
849 tree expr;
851 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (BINFO_TYPE (binfo)))
852 return;
854 /* Call the destructor. */
855 expr = (build_scoped_method_call
856 (current_class_ref, binfo, base_dtor_identifier, NULL_TREE));
857 if (flag)
858 expr = fold (build (COND_EXPR, void_type_node,
859 c_common_truthvalue_conversion (flag),
860 expr, integer_zero_node));
862 finish_eh_cleanup (expr);
865 /* Subroutine of `expand_aggr_vbase_init'.
866 BINFO is the binfo of the type that is being initialized.
867 INIT_LIST is the list of initializers for the virtual baseclass. */
869 static void
870 expand_aggr_vbase_init_1 (binfo, exp, addr, init_list)
871 tree binfo, exp, addr, init_list;
873 tree init = purpose_member (binfo, init_list);
874 tree ref = build_indirect_ref (addr, NULL);
876 if (init)
877 init = TREE_VALUE (init);
878 /* Call constructors, but don't set up vtables. */
879 expand_aggr_init_1 (binfo, exp, ref, init, LOOKUP_COMPLAIN);
882 /* Construct the virtual base-classes of THIS_REF (whose address is
883 THIS_PTR). The object has the indicated TYPE. The construction
884 actually takes place only if FLAG is non-zero. INIT_LIST is list
885 of initializations for constructors to perform. */
887 static void
888 construct_virtual_bases (type, this_ref, this_ptr, init_list, flag)
889 tree type;
890 tree this_ref;
891 tree this_ptr;
892 tree init_list;
893 tree flag;
895 tree vbases;
897 /* If there are no virtual baseclasses, we shouldn't even be here. */
898 my_friendly_assert (TYPE_USES_VIRTUAL_BASECLASSES (type), 19990621);
900 /* Now, run through the baseclasses, initializing each. */
901 for (vbases = CLASSTYPE_VBASECLASSES (type); vbases;
902 vbases = TREE_CHAIN (vbases))
904 tree inner_if_stmt;
905 tree compound_stmt;
906 tree exp;
907 tree vbase;
909 /* If there are virtual base classes with destructors, we need to
910 emit cleanups to destroy them if an exception is thrown during
911 the construction process. These exception regions (i.e., the
912 period during which the cleanups must occur) begin from the time
913 the construction is complete to the end of the function. If we
914 create a conditional block in which to initialize the
915 base-classes, then the cleanup region for the virtual base begins
916 inside a block, and ends outside of that block. This situation
917 confuses the sjlj exception-handling code. Therefore, we do not
918 create a single conditional block, but one for each
919 initialization. (That way the cleanup regions always begin
920 in the outer block.) We trust the back-end to figure out
921 that the FLAG will not change across initializations, and
922 avoid doing multiple tests. */
923 inner_if_stmt = begin_if_stmt ();
924 finish_if_stmt_cond (flag, inner_if_stmt);
925 compound_stmt = begin_compound_stmt (/*has_no_scope=*/1);
927 /* Compute the location of the virtual base. If we're
928 constructing virtual bases, then we must be the most derived
929 class. Therefore, we don't have to look up the virtual base;
930 we already know where it is. */
931 vbase = TREE_VALUE (vbases);
932 exp = build (PLUS_EXPR,
933 TREE_TYPE (this_ptr),
934 this_ptr,
935 fold (build1 (NOP_EXPR, TREE_TYPE (this_ptr),
936 BINFO_OFFSET (vbase))));
937 exp = build1 (NOP_EXPR,
938 build_pointer_type (BINFO_TYPE (vbase)),
939 exp);
941 expand_aggr_vbase_init_1 (vbase, this_ref, exp, init_list);
942 finish_compound_stmt (/*has_no_scope=*/1, compound_stmt);
943 finish_then_clause (inner_if_stmt);
944 finish_if_stmt ();
946 expand_cleanup_for_base (vbase, flag);
950 /* Find the context in which this FIELD can be initialized. */
952 static tree
953 initializing_context (field)
954 tree field;
956 tree t = DECL_CONTEXT (field);
958 /* Anonymous union members can be initialized in the first enclosing
959 non-anonymous union context. */
960 while (t && ANON_AGGR_TYPE_P (t))
961 t = TYPE_CONTEXT (t);
962 return t;
965 /* Function to give error message if member initialization specification
966 is erroneous. FIELD is the member we decided to initialize.
967 TYPE is the type for which the initialization is being performed.
968 FIELD must be a member of TYPE.
970 MEMBER_NAME is the name of the member. */
972 static int
973 member_init_ok_or_else (field, type, member_name)
974 tree field;
975 tree type;
976 tree member_name;
978 if (field == error_mark_node)
979 return 0;
980 if (field == NULL_TREE || initializing_context (field) != type)
982 error ("class `%T' does not have any field named `%D'", type,
983 member_name);
984 return 0;
986 if (TREE_STATIC (field))
988 error ("field `%#D' is static; the only point of initialization is its definition",
989 field);
990 return 0;
993 return 1;
996 /* EXP is an expression of aggregate type. NAME is an IDENTIFIER_NODE
997 which names a field, or it is a _TYPE node or TYPE_DECL which names
998 a base for that type. INIT is a parameter list for that field's or
999 base's constructor. Check the validity of NAME, and return a
1000 TREE_LIST of the base _TYPE or FIELD_DECL and the INIT. EXP is used
1001 only to get its type. If NAME is invalid, return NULL_TREE and
1002 issue a diagnostic.
1004 An old style unnamed direct single base construction is permitted,
1005 where NAME is NULL. */
1007 tree
1008 expand_member_init (exp, name, init)
1009 tree exp, name, init;
1011 tree basetype = NULL_TREE, field;
1012 tree type;
1014 if (exp == NULL_TREE)
1015 return NULL_TREE;
1017 type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
1018 my_friendly_assert (IS_AGGR_TYPE (type), 20011113);
1020 if (!name)
1022 /* This is an obsolete unnamed base class initializer. The
1023 parser will already have warned about its use. */
1024 switch (CLASSTYPE_N_BASECLASSES (type))
1026 case 0:
1027 error ("unnamed initializer for `%T', which has no base classes",
1028 type);
1029 return NULL_TREE;
1030 case 1:
1031 basetype = TYPE_BINFO_BASETYPE (type, 0);
1032 break;
1033 default:
1034 error ("unnamed initializer for `%T', which uses multiple inheritance",
1035 type);
1036 return NULL_TREE;
1039 else if (TYPE_P (name))
1041 basetype = name;
1042 name = TYPE_NAME (name);
1044 else if (TREE_CODE (name) == TYPE_DECL)
1045 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (name));
1047 my_friendly_assert (init != NULL_TREE, 0);
1049 if (init == void_type_node)
1050 init = NULL_TREE;
1052 if (basetype)
1054 if (current_template_parms)
1056 else if (vec_binfo_member (basetype, TYPE_BINFO_BASETYPES (type)))
1057 /* A direct base. */;
1058 else if (binfo_for_vbase (basetype, type))
1059 /* A virtual base. */;
1060 else
1062 if (TYPE_USES_VIRTUAL_BASECLASSES (type))
1063 error ("type `%D' is not a direct or virtual base of `%T'",
1064 name, type);
1065 else
1066 error ("type `%D' is not a direct base of `%T'",
1067 name, type);
1068 return NULL_TREE;
1071 init = build_tree_list (basetype, init);
1073 else
1075 field = lookup_field (type, name, 1, 0);
1077 if (! member_init_ok_or_else (field, type, name))
1078 return NULL_TREE;
1080 init = build_tree_list (field, init);
1083 return init;
1086 /* This is like `expand_member_init', only it stores one aggregate
1087 value into another.
1089 INIT comes in two flavors: it is either a value which
1090 is to be stored in EXP, or it is a parameter list
1091 to go to a constructor, which will operate on EXP.
1092 If INIT is not a parameter list for a constructor, then set
1093 LOOKUP_ONLYCONVERTING.
1094 If FLAGS is LOOKUP_ONLYCONVERTING then it is the = init form of
1095 the initializer, if FLAGS is 0, then it is the (init) form.
1096 If `init' is a CONSTRUCTOR, then we emit a warning message,
1097 explaining that such initializations are invalid.
1099 If INIT resolves to a CALL_EXPR which happens to return
1100 something of the type we are looking for, then we know
1101 that we can safely use that call to perform the
1102 initialization.
1104 The virtual function table pointer cannot be set up here, because
1105 we do not really know its type.
1107 Virtual baseclass pointers are also set up here.
1109 This never calls operator=().
1111 When initializing, nothing is CONST.
1113 A default copy constructor may have to be used to perform the
1114 initialization.
1116 A constructor or a conversion operator may have to be used to
1117 perform the initialization, but not both, as it would be ambiguous. */
1119 tree
1120 build_aggr_init (exp, init, flags)
1121 tree exp, init;
1122 int flags;
1124 tree stmt_expr;
1125 tree compound_stmt;
1126 int destroy_temps;
1127 tree type = TREE_TYPE (exp);
1128 int was_const = TREE_READONLY (exp);
1129 int was_volatile = TREE_THIS_VOLATILE (exp);
1131 if (init == error_mark_node)
1132 return error_mark_node;
1134 TREE_READONLY (exp) = 0;
1135 TREE_THIS_VOLATILE (exp) = 0;
1137 if (init && TREE_CODE (init) != TREE_LIST)
1138 flags |= LOOKUP_ONLYCONVERTING;
1140 if (TREE_CODE (type) == ARRAY_TYPE)
1142 /* Must arrange to initialize each element of EXP
1143 from elements of INIT. */
1144 tree itype = init ? TREE_TYPE (init) : NULL_TREE;
1146 if (init && !itype)
1148 /* Handle bad initializers like:
1149 class COMPLEX {
1150 public:
1151 double re, im;
1152 COMPLEX(double r = 0.0, double i = 0.0) {re = r; im = i;};
1153 ~COMPLEX() {};
1156 int main(int argc, char **argv) {
1157 COMPLEX zees(1.0, 0.0)[10];
1160 error ("bad array initializer");
1161 return error_mark_node;
1163 if (cp_type_quals (type) != TYPE_UNQUALIFIED)
1165 TREE_TYPE (exp) = TYPE_MAIN_VARIANT (type);
1166 if (init)
1167 TREE_TYPE (init) = TYPE_MAIN_VARIANT (itype);
1169 stmt_expr = build_vec_init (exp, init,
1170 init && same_type_p (TREE_TYPE (init),
1171 TREE_TYPE (exp)));
1172 TREE_READONLY (exp) = was_const;
1173 TREE_THIS_VOLATILE (exp) = was_volatile;
1174 TREE_TYPE (exp) = type;
1175 if (init)
1176 TREE_TYPE (init) = itype;
1177 return stmt_expr;
1180 if (TREE_CODE (exp) == VAR_DECL || TREE_CODE (exp) == PARM_DECL)
1181 /* just know that we've seen something for this node */
1182 TREE_USED (exp) = 1;
1184 TREE_TYPE (exp) = TYPE_MAIN_VARIANT (type);
1185 begin_init_stmts (&stmt_expr, &compound_stmt);
1186 destroy_temps = stmts_are_full_exprs_p ();
1187 current_stmt_tree ()->stmts_are_full_exprs_p = 0;
1188 expand_aggr_init_1 (TYPE_BINFO (type), exp, exp,
1189 init, LOOKUP_NORMAL|flags);
1190 stmt_expr = finish_init_stmts (stmt_expr, compound_stmt);
1191 current_stmt_tree ()->stmts_are_full_exprs_p = destroy_temps;
1192 TREE_TYPE (exp) = type;
1193 TREE_READONLY (exp) = was_const;
1194 TREE_THIS_VOLATILE (exp) = was_volatile;
1196 return stmt_expr;
1199 /* Like build_aggr_init, but not just for aggregates. */
1201 tree
1202 build_init (decl, init, flags)
1203 tree decl, init;
1204 int flags;
1206 tree expr;
1208 if (IS_AGGR_TYPE (TREE_TYPE (decl))
1209 || TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
1210 expr = build_aggr_init (decl, init, flags);
1211 else
1213 expr = build (INIT_EXPR, TREE_TYPE (decl), decl, init);
1214 TREE_SIDE_EFFECTS (expr) = 1;
1216 return expr;
1219 static void
1220 expand_default_init (binfo, true_exp, exp, init, flags)
1221 tree binfo;
1222 tree true_exp, exp;
1223 tree init;
1224 int flags;
1226 tree type = TREE_TYPE (exp);
1227 tree ctor_name;
1229 /* It fails because there may not be a constructor which takes
1230 its own type as the first (or only parameter), but which does
1231 take other types via a conversion. So, if the thing initializing
1232 the expression is a unit element of type X, first try X(X&),
1233 followed by initialization by X. If neither of these work
1234 out, then look hard. */
1235 tree rval;
1236 tree parms;
1238 if (init && TREE_CODE (init) != TREE_LIST
1239 && (flags & LOOKUP_ONLYCONVERTING))
1241 /* Base subobjects should only get direct-initialization. */
1242 if (true_exp != exp)
1243 abort ();
1245 if (flags & DIRECT_BIND)
1246 /* Do nothing. We hit this in two cases: Reference initialization,
1247 where we aren't initializing a real variable, so we don't want
1248 to run a new constructor; and catching an exception, where we
1249 have already built up the constructor call so we could wrap it
1250 in an exception region. */;
1251 else if (TREE_CODE (init) == CONSTRUCTOR)
1252 /* A brace-enclosed initializer has whatever type is
1253 required. There's no need to convert it. */
1255 else
1256 init = ocp_convert (type, init, CONV_IMPLICIT|CONV_FORCE_TEMP, flags);
1258 if (TREE_CODE (init) == TRY_CATCH_EXPR)
1259 /* We need to protect the initialization of a catch parm
1260 with a call to terminate(), which shows up as a TRY_CATCH_EXPR
1261 around the TARGET_EXPR for the copy constructor. See
1262 expand_start_catch_block. */
1263 TREE_OPERAND (init, 0) = build (INIT_EXPR, TREE_TYPE (exp), exp,
1264 TREE_OPERAND (init, 0));
1265 else
1266 init = build (INIT_EXPR, TREE_TYPE (exp), exp, init);
1267 TREE_SIDE_EFFECTS (init) = 1;
1268 finish_expr_stmt (init);
1269 return;
1272 if (init == NULL_TREE
1273 || (TREE_CODE (init) == TREE_LIST && ! TREE_TYPE (init)))
1275 parms = init;
1276 if (parms)
1277 init = TREE_VALUE (parms);
1279 else
1280 parms = build_tree_list (NULL_TREE, init);
1282 if (true_exp == exp)
1283 ctor_name = complete_ctor_identifier;
1284 else
1285 ctor_name = base_ctor_identifier;
1287 rval = build_method_call (exp, ctor_name, parms, binfo, flags);
1288 if (TREE_SIDE_EFFECTS (rval))
1290 if (building_stmt_tree ())
1291 finish_expr_stmt (rval);
1292 else
1293 genrtl_expr_stmt (rval);
1297 /* This function is responsible for initializing EXP with INIT
1298 (if any).
1300 BINFO is the binfo of the type for who we are performing the
1301 initialization. For example, if W is a virtual base class of A and B,
1302 and C : A, B.
1303 If we are initializing B, then W must contain B's W vtable, whereas
1304 were we initializing C, W must contain C's W vtable.
1306 TRUE_EXP is nonzero if it is the true expression being initialized.
1307 In this case, it may be EXP, or may just contain EXP. The reason we
1308 need this is because if EXP is a base element of TRUE_EXP, we
1309 don't necessarily know by looking at EXP where its virtual
1310 baseclass fields should really be pointing. But we do know
1311 from TRUE_EXP. In constructors, we don't know anything about
1312 the value being initialized.
1314 FLAGS is just passes to `build_method_call'. See that function for
1315 its description. */
1317 static void
1318 expand_aggr_init_1 (binfo, true_exp, exp, init, flags)
1319 tree binfo;
1320 tree true_exp, exp;
1321 tree init;
1322 int flags;
1324 tree type = TREE_TYPE (exp);
1326 my_friendly_assert (init != error_mark_node && type != error_mark_node, 211);
1328 /* Use a function returning the desired type to initialize EXP for us.
1329 If the function is a constructor, and its first argument is
1330 NULL_TREE, know that it was meant for us--just slide exp on
1331 in and expand the constructor. Constructors now come
1332 as TARGET_EXPRs. */
1334 if (init && TREE_CODE (exp) == VAR_DECL
1335 && TREE_CODE (init) == CONSTRUCTOR
1336 && TREE_HAS_CONSTRUCTOR (init))
1338 /* If store_init_value returns NULL_TREE, the INIT has been
1339 record in the DECL_INITIAL for EXP. That means there's
1340 nothing more we have to do. */
1341 if (!store_init_value (exp, init))
1343 if (!building_stmt_tree ())
1344 expand_decl_init (exp);
1346 else
1347 finish_expr_stmt (build (INIT_EXPR, type, exp, init));
1348 return;
1351 /* We know that expand_default_init can handle everything we want
1352 at this point. */
1353 expand_default_init (binfo, true_exp, exp, init, flags);
1356 /* Report an error if TYPE is not a user-defined, aggregate type. If
1357 OR_ELSE is nonzero, give an error message. */
1360 is_aggr_type (type, or_else)
1361 tree type;
1362 int or_else;
1364 if (type == error_mark_node)
1365 return 0;
1367 if (! IS_AGGR_TYPE (type)
1368 && TREE_CODE (type) != TEMPLATE_TYPE_PARM
1369 && TREE_CODE (type) != BOUND_TEMPLATE_TEMPLATE_PARM)
1371 if (or_else)
1372 error ("`%T' is not an aggregate type", type);
1373 return 0;
1375 return 1;
1378 /* Like is_aggr_typedef, but returns typedef if successful. */
1380 tree
1381 get_aggr_from_typedef (name, or_else)
1382 tree name;
1383 int or_else;
1385 tree type;
1387 if (name == error_mark_node)
1388 return NULL_TREE;
1390 if (IDENTIFIER_HAS_TYPE_VALUE (name))
1391 type = IDENTIFIER_TYPE_VALUE (name);
1392 else
1394 if (or_else)
1395 error ("`%T' fails to be an aggregate typedef", name);
1396 return NULL_TREE;
1399 if (! IS_AGGR_TYPE (type)
1400 && TREE_CODE (type) != TEMPLATE_TYPE_PARM
1401 && TREE_CODE (type) != BOUND_TEMPLATE_TEMPLATE_PARM)
1403 if (or_else)
1404 error ("type `%T' is of non-aggregate type", type);
1405 return NULL_TREE;
1407 return type;
1410 tree
1411 get_type_value (name)
1412 tree name;
1414 if (name == error_mark_node)
1415 return NULL_TREE;
1417 if (IDENTIFIER_HAS_TYPE_VALUE (name))
1418 return IDENTIFIER_TYPE_VALUE (name);
1419 else
1420 return NULL_TREE;
1424 /* This code could just as well go in `class.c', but is placed here for
1425 modularity. */
1427 /* For an expression of the form TYPE :: NAME (PARMLIST), build
1428 the appropriate function call. */
1430 tree
1431 build_member_call (type, name, parmlist)
1432 tree type, name, parmlist;
1434 tree t;
1435 tree method_name;
1436 int dtor = 0;
1437 tree basetype_path, decl;
1439 if (TREE_CODE (name) == TEMPLATE_ID_EXPR
1440 && TREE_CODE (type) == NAMESPACE_DECL)
1442 /* 'name' already refers to the decls from the namespace, since we
1443 hit do_identifier for template_ids. */
1444 method_name = TREE_OPERAND (name, 0);
1445 /* FIXME: Since we don't do independent names right yet, the
1446 name might also be a LOOKUP_EXPR. Once we resolve this to a
1447 real decl earlier, this can go. This may happen during
1448 tsubst'ing. */
1449 if (TREE_CODE (method_name) == LOOKUP_EXPR)
1451 method_name = lookup_namespace_name
1452 (type, TREE_OPERAND (method_name, 0));
1453 TREE_OPERAND (name, 0) = method_name;
1455 my_friendly_assert (is_overloaded_fn (method_name), 980519);
1456 return build_x_function_call (name, parmlist, current_class_ref);
1459 if (DECL_P (name))
1460 name = DECL_NAME (name);
1462 if (TREE_CODE (type) == NAMESPACE_DECL)
1463 return build_x_function_call (lookup_namespace_name (type, name),
1464 parmlist, current_class_ref);
1466 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
1468 method_name = TREE_OPERAND (name, 0);
1469 if (TREE_CODE (method_name) == COMPONENT_REF)
1470 method_name = TREE_OPERAND (method_name, 1);
1471 if (is_overloaded_fn (method_name))
1472 method_name = DECL_NAME (OVL_CURRENT (method_name));
1473 TREE_OPERAND (name, 0) = method_name;
1475 else
1476 method_name = name;
1478 if (TREE_CODE (method_name) == BIT_NOT_EXPR)
1480 method_name = TREE_OPERAND (method_name, 0);
1481 dtor = 1;
1484 /* This shouldn't be here, and build_member_call shouldn't appear in
1485 parse.y! (mrs) */
1486 if (type && TREE_CODE (type) == IDENTIFIER_NODE
1487 && get_aggr_from_typedef (type, 0) == 0)
1489 tree ns = lookup_name (type, 0);
1490 if (ns && TREE_CODE (ns) == NAMESPACE_DECL)
1492 return build_x_function_call (build_offset_ref (type, name),
1493 parmlist, current_class_ref);
1497 if (type == NULL_TREE || ! is_aggr_type (type, 1))
1498 return error_mark_node;
1500 /* An operator we did not like. */
1501 if (name == NULL_TREE)
1502 return error_mark_node;
1504 if (dtor)
1506 error ("cannot call destructor `%T::~%T' without object", type,
1507 method_name);
1508 return error_mark_node;
1511 decl = maybe_dummy_object (type, &basetype_path);
1513 /* Convert 'this' to the specified type to disambiguate conversion
1514 to the function's context. */
1515 if (decl == current_class_ref
1516 /* ??? this is wrong, but if this conversion is invalid we need to
1517 defer it until we know whether we are calling a static or
1518 non-static member function. Be conservative for now. */
1519 && ACCESSIBLY_UNIQUELY_DERIVED_P (type, current_class_type))
1521 basetype_path = NULL_TREE;
1522 decl = build_scoped_ref (decl, type, &basetype_path);
1523 if (decl == error_mark_node)
1524 return error_mark_node;
1527 if (method_name == constructor_name (type)
1528 || method_name == constructor_name_full (type))
1529 return build_functional_cast (type, parmlist);
1530 if (lookup_fnfields (basetype_path, method_name, 0))
1531 return build_method_call (decl,
1532 TREE_CODE (name) == TEMPLATE_ID_EXPR
1533 ? name : method_name,
1534 parmlist, basetype_path,
1535 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL);
1536 if (TREE_CODE (name) == IDENTIFIER_NODE
1537 && ((t = lookup_field (TYPE_BINFO (type), name, 1, 0))))
1539 if (t == error_mark_node)
1540 return error_mark_node;
1541 if (TREE_CODE (t) == FIELD_DECL)
1543 if (is_dummy_object (decl))
1545 error ("invalid use of non-static field `%D'", t);
1546 return error_mark_node;
1548 decl = build (COMPONENT_REF, TREE_TYPE (t), decl, t);
1550 else if (TREE_CODE (t) == VAR_DECL)
1551 decl = t;
1552 else
1554 error ("invalid use of member `%D'", t);
1555 return error_mark_node;
1557 if (TYPE_LANG_SPECIFIC (TREE_TYPE (decl)))
1558 return build_opfncall (CALL_EXPR, LOOKUP_NORMAL, decl,
1559 parmlist, NULL_TREE);
1560 return build_function_call (decl, parmlist);
1562 else
1564 error ("no method `%T::%D'", type, name);
1565 return error_mark_node;
1569 /* Build a reference to a member of an aggregate. This is not a
1570 C++ `&', but really something which can have its address taken,
1571 and then act as a pointer to member, for example TYPE :: FIELD
1572 can have its address taken by saying & TYPE :: FIELD.
1574 @@ Prints out lousy diagnostics for operator <typename>
1575 @@ fields.
1577 @@ This function should be rewritten and placed in search.c. */
1579 tree
1580 build_offset_ref (type, name)
1581 tree type, name;
1583 tree decl, t = error_mark_node;
1584 tree member;
1585 tree basebinfo = NULL_TREE;
1586 tree orig_name = name;
1588 /* class templates can come in as TEMPLATE_DECLs here. */
1589 if (TREE_CODE (name) == TEMPLATE_DECL)
1590 return name;
1592 if (processing_template_decl || uses_template_parms (type))
1593 return build_min_nt (SCOPE_REF, type, name);
1595 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
1597 /* If the NAME is a TEMPLATE_ID_EXPR, we are looking at
1598 something like `a.template f<int>' or the like. For the most
1599 part, we treat this just like a.f. We do remember, however,
1600 the template-id that was used. */
1601 name = TREE_OPERAND (orig_name, 0);
1603 if (DECL_P (name))
1604 name = DECL_NAME (name);
1605 else
1607 if (TREE_CODE (name) == LOOKUP_EXPR)
1608 /* This can happen during tsubst'ing. */
1609 name = TREE_OPERAND (name, 0);
1610 else
1612 if (TREE_CODE (name) == COMPONENT_REF)
1613 name = TREE_OPERAND (name, 1);
1614 if (TREE_CODE (name) == OVERLOAD)
1615 name = DECL_NAME (OVL_CURRENT (name));
1619 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 0);
1622 if (type == NULL_TREE)
1623 return error_mark_node;
1625 /* Handle namespace names fully here. */
1626 if (TREE_CODE (type) == NAMESPACE_DECL)
1628 t = lookup_namespace_name (type, name);
1629 if (t == error_mark_node)
1630 return t;
1631 if (TREE_CODE (orig_name) == TEMPLATE_ID_EXPR)
1632 /* Reconstruct the TEMPLATE_ID_EXPR. */
1633 t = build (TEMPLATE_ID_EXPR, TREE_TYPE (t),
1634 t, TREE_OPERAND (orig_name, 1));
1635 if (! type_unknown_p (t))
1637 mark_used (t);
1638 t = convert_from_reference (t);
1640 return t;
1643 if (! is_aggr_type (type, 1))
1644 return error_mark_node;
1646 if (TREE_CODE (name) == BIT_NOT_EXPR)
1648 if (! check_dtor_name (type, name))
1649 error ("qualified type `%T' does not match destructor name `~%T'",
1650 type, TREE_OPERAND (name, 0));
1651 name = dtor_identifier;
1654 if (!COMPLETE_TYPE_P (complete_type (type))
1655 && !TYPE_BEING_DEFINED (type))
1657 error ("incomplete type `%T' does not have member `%D'", type,
1658 name);
1659 return error_mark_node;
1662 decl = maybe_dummy_object (type, &basebinfo);
1664 member = lookup_member (basebinfo, name, 1, 0);
1666 if (member == error_mark_node)
1667 return error_mark_node;
1669 /* A lot of this logic is now handled in lookup_member. */
1670 if (member && BASELINK_P (member))
1672 /* Go from the TREE_BASELINK to the member function info. */
1673 tree fnfields = member;
1674 t = TREE_VALUE (fnfields);
1676 if (TREE_CODE (orig_name) == TEMPLATE_ID_EXPR)
1678 /* The FNFIELDS are going to contain functions that aren't
1679 necessarily templates, and templates that don't
1680 necessarily match the explicit template parameters. We
1681 save all the functions, and the explicit parameters, and
1682 then figure out exactly what to instantiate with what
1683 arguments in instantiate_type. */
1685 if (TREE_CODE (t) != OVERLOAD)
1686 /* The code in instantiate_type which will process this
1687 expects to encounter OVERLOADs, not raw functions. */
1688 t = ovl_cons (t, NULL_TREE);
1690 t = build (TEMPLATE_ID_EXPR, TREE_TYPE (t), t,
1691 TREE_OPERAND (orig_name, 1));
1692 t = build (OFFSET_REF, unknown_type_node, decl, t);
1694 PTRMEM_OK_P (t) = 1;
1696 return t;
1699 if (!really_overloaded_fn (t))
1701 /* Get rid of a potential OVERLOAD around it */
1702 t = OVL_CURRENT (t);
1704 /* unique functions are handled easily. */
1705 if (!enforce_access (basebinfo, t))
1706 return error_mark_node;
1707 mark_used (t);
1708 if (DECL_STATIC_FUNCTION_P (t))
1709 return t;
1710 t = build (OFFSET_REF, TREE_TYPE (t), decl, t);
1711 PTRMEM_OK_P (t) = 1;
1712 return t;
1715 TREE_TYPE (fnfields) = unknown_type_node;
1717 t = build (OFFSET_REF, unknown_type_node, decl, fnfields);
1718 PTRMEM_OK_P (t) = 1;
1719 return t;
1722 t = member;
1724 if (t == NULL_TREE)
1726 error ("`%D' is not a member of type `%T'", name, type);
1727 return error_mark_node;
1730 if (TREE_CODE (t) == TYPE_DECL)
1732 TREE_USED (t) = 1;
1733 return t;
1735 /* static class members and class-specific enum
1736 values can be returned without further ado. */
1737 if (TREE_CODE (t) == VAR_DECL || TREE_CODE (t) == CONST_DECL)
1739 mark_used (t);
1740 return convert_from_reference (t);
1743 if (TREE_CODE (t) == FIELD_DECL && DECL_C_BIT_FIELD (t))
1745 error ("illegal pointer to bit-field `%D'", t);
1746 return error_mark_node;
1749 /* static class functions too. */
1750 if (TREE_CODE (t) == FUNCTION_DECL
1751 && TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
1752 abort ();
1754 /* In member functions, the form `type::name' is no longer
1755 equivalent to `this->type::name', at least not until
1756 resolve_offset_ref. */
1757 t = build (OFFSET_REF, build_offset_type (type, TREE_TYPE (t)), decl, t);
1758 PTRMEM_OK_P (t) = 1;
1759 return t;
1762 /* If a OFFSET_REF made it through to here, then it did
1763 not have its address taken. */
1765 tree
1766 resolve_offset_ref (exp)
1767 tree exp;
1769 tree type = TREE_TYPE (exp);
1770 tree base = NULL_TREE;
1771 tree member;
1772 tree basetype, addr;
1774 if (TREE_CODE (exp) == OFFSET_REF)
1776 member = TREE_OPERAND (exp, 1);
1777 base = TREE_OPERAND (exp, 0);
1779 else
1781 my_friendly_assert (TREE_CODE (type) == OFFSET_TYPE, 214);
1782 if (TYPE_OFFSET_BASETYPE (type) != current_class_type)
1784 error ("object missing in use of pointer-to-member construct");
1785 return error_mark_node;
1787 member = exp;
1788 type = TREE_TYPE (type);
1789 base = current_class_ref;
1792 if (BASELINK_P (member) || TREE_CODE (member) == TEMPLATE_ID_EXPR)
1793 return build_unary_op (ADDR_EXPR, exp, 0);
1795 if (TREE_CODE (TREE_TYPE (member)) == METHOD_TYPE)
1797 if (!flag_ms_extensions)
1798 /* A single non-static member, make sure we don't allow a
1799 pointer-to-member. */
1800 exp = ovl_cons (member, NULL_TREE);
1802 return build_unary_op (ADDR_EXPR, exp, 0);
1805 if ((TREE_CODE (member) == VAR_DECL
1806 && ! TYPE_PTRMEMFUNC_P (TREE_TYPE (member))
1807 && ! TYPE_PTRMEM_P (TREE_TYPE (member)))
1808 || TREE_CODE (TREE_TYPE (member)) == FUNCTION_TYPE)
1810 /* These were static members. */
1811 if (!cxx_mark_addressable (member))
1812 return error_mark_node;
1813 return member;
1816 if (TREE_CODE (TREE_TYPE (member)) == POINTER_TYPE
1817 && TREE_CODE (TREE_TYPE (TREE_TYPE (member))) == METHOD_TYPE)
1818 return member;
1820 /* Syntax error can cause a member which should
1821 have been seen as static to be grok'd as non-static. */
1822 if (TREE_CODE (member) == FIELD_DECL && current_class_ref == NULL_TREE)
1824 cp_error_at ("member `%D' is non-static but referenced as a static member",
1825 member);
1826 error ("at this point in file");
1827 return error_mark_node;
1830 /* The first case is really just a reference to a member of `this'. */
1831 if (TREE_CODE (member) == FIELD_DECL
1832 && (base == current_class_ref || is_dummy_object (base)))
1834 tree binfo = NULL_TREE;
1836 /* Try to get to basetype from 'this'; if that doesn't work,
1837 nothing will. */
1838 base = current_class_ref;
1840 /* First convert to the intermediate base specified, if appropriate. */
1841 if (TREE_CODE (exp) == OFFSET_REF && TREE_CODE (type) == OFFSET_TYPE)
1842 base = build_scoped_ref (base, TYPE_OFFSET_BASETYPE (type), &binfo);
1844 return build_component_ref (base, member, binfo, 1);
1847 /* Ensure that we have an object. */
1848 if (is_dummy_object (base))
1849 addr = error_mark_node;
1850 else
1851 /* If this is a reference to a member function, then return the
1852 address of the member function (which may involve going
1853 through the object's vtable), otherwise, return an expression
1854 for the dereferenced pointer-to-member construct. */
1855 addr = build_unary_op (ADDR_EXPR, base, 0);
1857 if (TYPE_PTRMEM_P (TREE_TYPE (member)))
1859 if (addr == error_mark_node)
1861 error ("object missing in `%E'", exp);
1862 return error_mark_node;
1865 basetype = TYPE_OFFSET_BASETYPE (TREE_TYPE (TREE_TYPE (member)));
1866 basetype = lookup_base (TREE_TYPE (TREE_TYPE (addr)),
1867 basetype, ba_check, NULL);
1868 addr = build_base_path (PLUS_EXPR, addr, basetype, 1);
1870 member = cp_convert (ptrdiff_type_node, member);
1872 addr = build (PLUS_EXPR, build_pointer_type (type), addr, member);
1873 return build_indirect_ref (addr, 0);
1875 else if (TYPE_PTRMEMFUNC_P (TREE_TYPE (member)))
1877 return get_member_function_from_ptrfunc (&addr, member);
1879 abort ();
1880 /* NOTREACHED */
1881 return NULL_TREE;
1884 /* If DECL is a `const' declaration, and its value is a known
1885 constant, then return that value. */
1887 tree
1888 decl_constant_value (decl)
1889 tree decl;
1891 if (TREE_READONLY_DECL_P (decl)
1892 && ! TREE_THIS_VOLATILE (decl)
1893 && DECL_INITIAL (decl)
1894 && DECL_INITIAL (decl) != error_mark_node
1895 /* This is invalid if initial value is not constant.
1896 If it has either a function call, a memory reference,
1897 or a variable, then re-evaluating it could give different results. */
1898 && TREE_CONSTANT (DECL_INITIAL (decl))
1899 /* Check for cases where this is sub-optimal, even though valid. */
1900 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1901 return DECL_INITIAL (decl);
1902 return decl;
1905 /* Common subroutines of build_new and build_vec_delete. */
1907 /* Call the global __builtin_delete to delete ADDR. */
1909 static tree
1910 build_builtin_delete_call (addr)
1911 tree addr;
1913 mark_used (global_delete_fndecl);
1914 return build_call (global_delete_fndecl, build_tree_list (NULL_TREE, addr));
1917 /* Generate a C++ "new" expression. DECL is either a TREE_LIST
1918 (which needs to go through some sort of groktypename) or it
1919 is the name of the class we are newing. INIT is an initialization value.
1920 It is either an EXPRLIST, an EXPR_NO_COMMAS, or something in braces.
1921 If INIT is void_type_node, it means do *not* call a constructor
1922 for this instance.
1924 For types with constructors, the data returned is initialized
1925 by the appropriate constructor.
1927 Whether the type has a constructor or not, if it has a pointer
1928 to a virtual function table, then that pointer is set up
1929 here.
1931 Unless I am mistaken, a call to new () will return initialized
1932 data regardless of whether the constructor itself is private or
1933 not. NOPE; new fails if the constructor is private (jcm).
1935 Note that build_new does nothing to assure that any special
1936 alignment requirements of the type are met. Rather, it leaves
1937 it up to malloc to do the right thing. Otherwise, folding to
1938 the right alignment cal cause problems if the user tries to later
1939 free the memory returned by `new'.
1941 PLACEMENT is the `placement' list for user-defined operator new (). */
1943 tree
1944 build_new (placement, decl, init, use_global_new)
1945 tree placement;
1946 tree decl, init;
1947 int use_global_new;
1949 tree type, rval;
1950 tree nelts = NULL_TREE, t;
1951 int has_array = 0;
1953 if (decl == error_mark_node)
1954 return error_mark_node;
1956 if (TREE_CODE (decl) == TREE_LIST)
1958 tree absdcl = TREE_VALUE (decl);
1959 tree last_absdcl = NULL_TREE;
1961 if (current_function_decl
1962 && DECL_CONSTRUCTOR_P (current_function_decl))
1963 my_friendly_assert (immediate_size_expand == 0, 19990926);
1965 nelts = integer_one_node;
1967 if (absdcl && TREE_CODE (absdcl) == CALL_EXPR)
1968 abort ();
1969 while (absdcl && TREE_CODE (absdcl) == INDIRECT_REF)
1971 last_absdcl = absdcl;
1972 absdcl = TREE_OPERAND (absdcl, 0);
1975 if (absdcl && TREE_CODE (absdcl) == ARRAY_REF)
1977 /* probably meant to be a vec new */
1978 tree this_nelts;
1980 while (TREE_OPERAND (absdcl, 0)
1981 && TREE_CODE (TREE_OPERAND (absdcl, 0)) == ARRAY_REF)
1983 last_absdcl = absdcl;
1984 absdcl = TREE_OPERAND (absdcl, 0);
1987 has_array = 1;
1988 this_nelts = TREE_OPERAND (absdcl, 1);
1989 if (this_nelts != error_mark_node)
1991 if (this_nelts == NULL_TREE)
1992 error ("new of array type fails to specify size");
1993 else if (processing_template_decl)
1995 nelts = this_nelts;
1996 absdcl = TREE_OPERAND (absdcl, 0);
1998 else
2000 if (build_expr_type_conversion (WANT_INT | WANT_ENUM,
2001 this_nelts, 0)
2002 == NULL_TREE)
2003 pedwarn ("size in array new must have integral type");
2005 this_nelts = save_expr (cp_convert (sizetype, this_nelts));
2006 absdcl = TREE_OPERAND (absdcl, 0);
2007 if (this_nelts == integer_zero_node)
2009 warning ("zero size array reserves no space");
2010 nelts = integer_zero_node;
2012 else
2013 nelts = cp_build_binary_op (MULT_EXPR, nelts, this_nelts);
2016 else
2017 nelts = integer_zero_node;
2020 if (last_absdcl)
2021 TREE_OPERAND (last_absdcl, 0) = absdcl;
2022 else
2023 TREE_VALUE (decl) = absdcl;
2025 type = groktypename (decl);
2026 if (! type || type == error_mark_node)
2027 return error_mark_node;
2029 else if (TREE_CODE (decl) == IDENTIFIER_NODE)
2031 if (IDENTIFIER_HAS_TYPE_VALUE (decl))
2033 /* An aggregate type. */
2034 type = IDENTIFIER_TYPE_VALUE (decl);
2035 decl = TYPE_MAIN_DECL (type);
2037 else
2039 /* A builtin type. */
2040 decl = lookup_name (decl, 1);
2041 my_friendly_assert (TREE_CODE (decl) == TYPE_DECL, 215);
2042 type = TREE_TYPE (decl);
2045 else if (TREE_CODE (decl) == TYPE_DECL)
2047 type = TREE_TYPE (decl);
2049 else
2051 type = decl;
2052 decl = TYPE_MAIN_DECL (type);
2055 if (processing_template_decl)
2057 if (has_array)
2058 t = tree_cons (tree_cons (NULL_TREE, type, NULL_TREE),
2059 build_min_nt (ARRAY_REF, NULL_TREE, nelts),
2060 NULL_TREE);
2061 else
2062 t = type;
2064 rval = build_min_nt (NEW_EXPR, placement, t, init);
2065 NEW_EXPR_USE_GLOBAL (rval) = use_global_new;
2066 return rval;
2069 /* ``A reference cannot be created by the new operator. A reference
2070 is not an object (8.2.2, 8.4.3), so a pointer to it could not be
2071 returned by new.'' ARM 5.3.3 */
2072 if (TREE_CODE (type) == REFERENCE_TYPE)
2074 error ("new cannot be applied to a reference type");
2075 type = TREE_TYPE (type);
2078 if (TREE_CODE (type) == FUNCTION_TYPE)
2080 error ("new cannot be applied to a function type");
2081 return error_mark_node;
2084 /* When the object being created is an array, the new-expression yields a
2085 pointer to the initial element (if any) of the array. For example,
2086 both new int and new int[10] return an int*. 5.3.4. */
2087 if (TREE_CODE (type) == ARRAY_TYPE && has_array == 0)
2089 nelts = array_type_nelts_top (type);
2090 has_array = 1;
2091 type = TREE_TYPE (type);
2094 if (has_array)
2095 t = build_nt (ARRAY_REF, type, nelts);
2096 else
2097 t = type;
2099 rval = build (NEW_EXPR, build_pointer_type (type), placement, t, init);
2100 NEW_EXPR_USE_GLOBAL (rval) = use_global_new;
2101 TREE_SIDE_EFFECTS (rval) = 1;
2102 rval = build_new_1 (rval);
2103 if (rval == error_mark_node)
2104 return error_mark_node;
2106 /* Wrap it in a NOP_EXPR so warn_if_unused_value doesn't complain. */
2107 rval = build1 (NOP_EXPR, TREE_TYPE (rval), rval);
2108 TREE_NO_UNUSED_WARNING (rval) = 1;
2110 return rval;
2113 /* Given a Java class, return a decl for the corresponding java.lang.Class. */
2115 tree
2116 build_java_class_ref (type)
2117 tree type;
2119 tree name = NULL_TREE, class_decl;
2120 static tree CL_suffix = NULL_TREE;
2121 if (CL_suffix == NULL_TREE)
2122 CL_suffix = get_identifier("class$");
2123 if (jclass_node == NULL_TREE)
2125 jclass_node = IDENTIFIER_GLOBAL_VALUE (get_identifier ("jclass"));
2126 if (jclass_node == NULL_TREE)
2127 fatal_error ("call to Java constructor, while `jclass' undefined");
2129 jclass_node = TREE_TYPE (jclass_node);
2132 /* Mangle the class$ field */
2134 tree field;
2135 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
2136 if (DECL_NAME (field) == CL_suffix)
2138 mangle_decl (field);
2139 name = DECL_ASSEMBLER_NAME (field);
2140 break;
2142 if (!field)
2143 internal_error ("can't find class$");
2146 class_decl = IDENTIFIER_GLOBAL_VALUE (name);
2147 if (class_decl == NULL_TREE)
2149 class_decl = build_decl (VAR_DECL, name, TREE_TYPE (jclass_node));
2150 TREE_STATIC (class_decl) = 1;
2151 DECL_EXTERNAL (class_decl) = 1;
2152 TREE_PUBLIC (class_decl) = 1;
2153 DECL_ARTIFICIAL (class_decl) = 1;
2154 DECL_IGNORED_P (class_decl) = 1;
2155 pushdecl_top_level (class_decl);
2156 make_decl_rtl (class_decl, NULL);
2158 return class_decl;
2161 /* Returns the size of the cookie to use when allocating an array
2162 whose elements have the indicated TYPE. Assumes that it is already
2163 known that a cookie is needed. */
2165 static tree
2166 get_cookie_size (type)
2167 tree type;
2169 tree cookie_size;
2171 /* We need to allocate an additional max (sizeof (size_t), alignof
2172 (true_type)) bytes. */
2173 tree sizetype_size;
2174 tree type_align;
2176 sizetype_size = size_in_bytes (sizetype);
2177 type_align = size_int (TYPE_ALIGN_UNIT (type));
2178 if (INT_CST_LT_UNSIGNED (type_align, sizetype_size))
2179 cookie_size = sizetype_size;
2180 else
2181 cookie_size = type_align;
2183 return cookie_size;
2186 /* Called from cplus_expand_expr when expanding a NEW_EXPR. The return
2187 value is immediately handed to expand_expr. */
2189 static tree
2190 build_new_1 (exp)
2191 tree exp;
2193 tree placement, init;
2194 tree type, true_type, size, rval, t;
2195 tree full_type;
2196 tree nelts = NULL_TREE;
2197 tree alloc_call, alloc_expr, alloc_node;
2198 tree alloc_fn;
2199 tree cookie_expr, init_expr;
2200 int has_array = 0;
2201 enum tree_code code;
2202 int use_cookie, nothrow, check_new;
2203 /* Nonzero if the user wrote `::new' rather than just `new'. */
2204 int globally_qualified_p;
2205 /* Nonzero if we're going to call a global operator new, rather than
2206 a class-specific version. */
2207 int use_global_new;
2208 int use_java_new = 0;
2209 /* If non-NULL, the number of extra bytes to allocate at the
2210 beginning of the storage allocated for an array-new expression in
2211 order to store the number of elements. */
2212 tree cookie_size = NULL_TREE;
2213 /* True if the function we are calling is a placement allocation
2214 function. */
2215 bool placement_allocation_fn_p;
2217 placement = TREE_OPERAND (exp, 0);
2218 type = TREE_OPERAND (exp, 1);
2219 init = TREE_OPERAND (exp, 2);
2220 globally_qualified_p = NEW_EXPR_USE_GLOBAL (exp);
2222 if (TREE_CODE (type) == ARRAY_REF)
2224 has_array = 1;
2225 nelts = TREE_OPERAND (type, 1);
2226 type = TREE_OPERAND (type, 0);
2228 full_type = cp_build_binary_op (MINUS_EXPR, nelts, integer_one_node);
2229 full_type = build_index_type (full_type);
2230 full_type = build_cplus_array_type (type, full_type);
2232 else
2233 full_type = type;
2235 true_type = type;
2237 code = has_array ? VEC_NEW_EXPR : NEW_EXPR;
2239 /* If our base type is an array, then make sure we know how many elements
2240 it has. */
2241 while (TREE_CODE (true_type) == ARRAY_TYPE)
2243 tree this_nelts = array_type_nelts_top (true_type);
2244 nelts = cp_build_binary_op (MULT_EXPR, nelts, this_nelts);
2245 true_type = TREE_TYPE (true_type);
2248 if (!complete_type_or_else (true_type, exp))
2249 return error_mark_node;
2251 size = size_in_bytes (true_type);
2252 if (has_array)
2253 size = size_binop (MULT_EXPR, size, convert (sizetype, nelts));
2255 if (TREE_CODE (true_type) == VOID_TYPE)
2257 error ("invalid type `void' for new");
2258 return error_mark_node;
2261 if (abstract_virtuals_error (NULL_TREE, true_type))
2262 return error_mark_node;
2264 /* Figure out whether or not we're going to use the global operator
2265 new. */
2266 if (!globally_qualified_p
2267 && IS_AGGR_TYPE (true_type)
2268 && (has_array
2269 ? TYPE_HAS_ARRAY_NEW_OPERATOR (true_type)
2270 : TYPE_HAS_NEW_OPERATOR (true_type)))
2271 use_global_new = 0;
2272 else
2273 use_global_new = 1;
2275 /* We only need cookies for arrays containing types for which we
2276 need cookies. */
2277 if (!has_array || !TYPE_VEC_NEW_USES_COOKIE (true_type))
2278 use_cookie = 0;
2279 /* When using placement new, users may not realize that they need
2280 the extra storage. We require that the operator called be
2281 the global placement operator new[]. */
2282 else if (placement && !TREE_CHAIN (placement)
2283 && same_type_p (TREE_TYPE (TREE_VALUE (placement)),
2284 ptr_type_node))
2285 use_cookie = !use_global_new;
2286 /* Otherwise, we need the cookie. */
2287 else
2288 use_cookie = 1;
2290 /* Compute the number of extra bytes to allocate, now that we know
2291 whether or not we need the cookie. */
2292 if (use_cookie)
2294 cookie_size = get_cookie_size (true_type);
2295 size = size_binop (PLUS_EXPR, size, cookie_size);
2298 /* Allocate the object. */
2300 if (! placement && TYPE_FOR_JAVA (true_type))
2302 tree class_addr, alloc_decl;
2303 tree class_decl = build_java_class_ref (true_type);
2304 tree class_size = size_in_bytes (true_type);
2305 static const char alloc_name[] = "_Jv_AllocObject";
2306 use_java_new = 1;
2307 alloc_decl = IDENTIFIER_GLOBAL_VALUE (get_identifier (alloc_name));
2308 if (alloc_decl == NULL_TREE)
2309 fatal_error ("call to Java constructor with `%s' undefined",
2310 alloc_name);
2312 class_addr = build1 (ADDR_EXPR, jclass_node, class_decl);
2313 alloc_call = (build_function_call
2314 (alloc_decl,
2315 tree_cons (NULL_TREE, class_addr,
2316 build_tree_list (NULL_TREE, class_size))));
2318 else
2320 tree fnname;
2321 tree args;
2323 args = tree_cons (NULL_TREE, size, placement);
2324 fnname = ansi_opname (code);
2326 if (use_global_new)
2327 alloc_call = (build_new_function_call
2328 (lookup_function_nonclass (fnname, args),
2329 args));
2330 else
2331 alloc_call = build_method_call (build_dummy_object (true_type),
2332 fnname, args, NULL_TREE,
2333 LOOKUP_NORMAL);
2336 if (alloc_call == error_mark_node)
2337 return error_mark_node;
2339 /* The ALLOC_CALL should be a CALL_EXPR -- or a COMPOUND_EXPR whose
2340 right-hand-side is ultimately a CALL_EXPR -- and the first
2341 operand should be the address of a known FUNCTION_DECL. */
2342 t = alloc_call;
2343 while (TREE_CODE (t) == COMPOUND_EXPR)
2344 t = TREE_OPERAND (t, 1);
2345 alloc_fn = get_callee_fndecl (t);
2346 my_friendly_assert (alloc_fn != NULL_TREE, 20020325);
2347 /* Now, check to see if this function is actually a placement
2348 allocation function. This can happen even when PLACEMENT is NULL
2349 because we might have something like:
2351 struct S { void* operator new (size_t, int i = 0); };
2353 A call to `new S' will get this allocation function, even though
2354 there is no explicit placement argument. If there is more than
2355 one argument, or there are variable arguments, then this is a
2356 placement allocation function. */
2357 placement_allocation_fn_p
2358 = (type_num_arguments (TREE_TYPE (alloc_fn)) > 1
2359 || varargs_function_p (alloc_fn));
2361 /* unless an allocation function is declared with an empty excep-
2362 tion-specification (_except.spec_), throw(), it indicates failure to
2363 allocate storage by throwing a bad_alloc exception (clause _except_,
2364 _lib.bad.alloc_); it returns a non-null pointer otherwise If the allo-
2365 cation function is declared with an empty exception-specification,
2366 throw(), it returns null to indicate failure to allocate storage and a
2367 non-null pointer otherwise.
2369 So check for a null exception spec on the op new we just called. */
2371 nothrow = TYPE_NOTHROW_P (TREE_TYPE (alloc_fn));
2372 check_new = (flag_check_new || nothrow) && ! use_java_new;
2374 alloc_expr = alloc_call;
2376 if (use_cookie)
2377 /* Adjust so we're pointing to the start of the object. */
2378 alloc_expr = build (PLUS_EXPR, TREE_TYPE (alloc_expr),
2379 alloc_expr, cookie_size);
2381 /* While we're working, use a pointer to the type we've actually
2382 allocated. */
2383 alloc_expr = convert (build_pointer_type (full_type), alloc_expr);
2385 /* Now save the allocation expression so we only evaluate it once. */
2386 alloc_expr = get_target_expr (alloc_expr);
2387 alloc_node = TREE_OPERAND (alloc_expr, 0);
2389 /* Now initialize the cookie. */
2390 if (use_cookie)
2392 tree cookie;
2394 /* Store the number of bytes allocated so that we can know how
2395 many elements to destroy later. We use the last sizeof
2396 (size_t) bytes to store the number of elements. */
2397 cookie = build (MINUS_EXPR, build_pointer_type (sizetype),
2398 alloc_node, size_in_bytes (sizetype));
2399 cookie = build_indirect_ref (cookie, NULL);
2401 cookie_expr = build (MODIFY_EXPR, void_type_node, cookie, nelts);
2402 TREE_SIDE_EFFECTS (cookie_expr) = 1;
2404 else
2405 cookie_expr = NULL_TREE;
2407 /* Now initialize the allocated object. */
2408 init_expr = NULL_TREE;
2409 if (TYPE_NEEDS_CONSTRUCTING (type) || init)
2411 init_expr = build_indirect_ref (alloc_node, NULL);
2413 if (init == void_zero_node)
2414 init = build_default_init (full_type);
2415 else if (init && pedantic && has_array)
2416 pedwarn ("ISO C++ forbids initialization in array new");
2418 if (has_array)
2419 init_expr = build_vec_init (init_expr, init, 0);
2420 else if (TYPE_NEEDS_CONSTRUCTING (type))
2421 init_expr = build_method_call (init_expr,
2422 complete_ctor_identifier,
2423 init, TYPE_BINFO (true_type),
2424 LOOKUP_NORMAL);
2425 else
2427 /* We are processing something like `new int (10)', which
2428 means allocate an int, and initialize it with 10. */
2430 if (TREE_CODE (init) == TREE_LIST)
2432 if (TREE_CHAIN (init) != NULL_TREE)
2433 pedwarn
2434 ("initializer list being treated as compound expression");
2435 init = build_compound_expr (init);
2437 else if (TREE_CODE (init) == CONSTRUCTOR
2438 && TREE_TYPE (init) == NULL_TREE)
2440 pedwarn ("ISO C++ forbids aggregate initializer to new");
2441 init = digest_init (type, init, 0);
2444 init_expr = build_modify_expr (init_expr, INIT_EXPR, init);
2447 if (init_expr == error_mark_node)
2448 return error_mark_node;
2450 /* If any part of the object initialization terminates by throwing an
2451 exception and a suitable deallocation function can be found, the
2452 deallocation function is called to free the memory in which the
2453 object was being constructed, after which the exception continues
2454 to propagate in the context of the new-expression. If no
2455 unambiguous matching deallocation function can be found,
2456 propagating the exception does not cause the object's memory to be
2457 freed. */
2458 if (flag_exceptions && ! use_java_new)
2460 enum tree_code dcode = has_array ? VEC_DELETE_EXPR : DELETE_EXPR;
2461 tree cleanup;
2462 int flags = (LOOKUP_NORMAL
2463 | (globally_qualified_p * LOOKUP_GLOBAL));
2464 tree delete_node;
2466 if (use_cookie)
2467 /* Subtract the padding back out to get to the pointer returned
2468 from operator new. */
2469 delete_node = fold (build (MINUS_EXPR, TREE_TYPE (alloc_node),
2470 alloc_node, cookie_size));
2471 else
2472 delete_node = alloc_node;
2474 /* The Standard is unclear here, but the right thing to do
2475 is to use the same method for finding deallocation
2476 functions that we use for finding allocation functions. */
2477 flags |= LOOKUP_SPECULATIVELY;
2479 cleanup = build_op_delete_call (dcode, delete_node, size, flags,
2480 (placement_allocation_fn_p
2481 ? alloc_call : NULL_TREE));
2483 /* Ack! First we allocate the memory. Then we set our sentry
2484 variable to true, and expand a cleanup that deletes the memory
2485 if sentry is true. Then we run the constructor, and finally
2486 clear the sentry.
2488 It would be nice to be able to handle this without the sentry
2489 variable, perhaps with a TRY_CATCH_EXPR, but this doesn't
2490 work. We allocate the space first, so if there are any
2491 temporaries with cleanups in the constructor args we need this
2492 EH region to extend until end of full-expression to preserve
2493 nesting.
2495 If the backend had some mechanism so that we could force the
2496 allocation to be expanded after all the other args to the
2497 constructor, that would fix the nesting problem and we could
2498 do away with this complexity. But that would complicate other
2499 things; in particular, it would make it difficult to bail out
2500 if the allocation function returns null. */
2502 if (cleanup)
2504 tree end, sentry, begin;
2506 begin = get_target_expr (boolean_true_node);
2507 CLEANUP_EH_ONLY (begin) = 1;
2509 sentry = TARGET_EXPR_SLOT (begin);
2511 TARGET_EXPR_CLEANUP (begin)
2512 = build (COND_EXPR, void_type_node, sentry,
2513 cleanup, void_zero_node);
2515 end = build (MODIFY_EXPR, TREE_TYPE (sentry),
2516 sentry, boolean_false_node);
2518 init_expr
2519 = build (COMPOUND_EXPR, void_type_node, begin,
2520 build (COMPOUND_EXPR, void_type_node, init_expr,
2521 end));
2525 else if (CP_TYPE_CONST_P (true_type))
2526 error ("uninitialized const in `new' of `%#T'", true_type);
2528 /* Now build up the return value in reverse order. */
2530 rval = alloc_node;
2532 if (init_expr)
2533 rval = build (COMPOUND_EXPR, TREE_TYPE (rval), init_expr, rval);
2534 if (cookie_expr)
2535 rval = build (COMPOUND_EXPR, TREE_TYPE (rval), cookie_expr, rval);
2537 if (rval == alloc_node)
2538 /* If we didn't modify anything, strip the TARGET_EXPR and return the
2539 (adjusted) call. */
2540 rval = TREE_OPERAND (alloc_expr, 1);
2541 else
2543 if (check_new)
2545 tree ifexp = cp_build_binary_op (NE_EXPR, alloc_node,
2546 integer_zero_node);
2547 rval = build_conditional_expr (ifexp, rval, alloc_node);
2550 rval = build (COMPOUND_EXPR, TREE_TYPE (rval), alloc_expr, rval);
2553 /* Now strip the outer ARRAY_TYPE, so we return a pointer to the first
2554 element. */
2555 rval = convert (build_pointer_type (type), rval);
2557 return rval;
2560 static tree
2561 build_vec_delete_1 (base, maxindex, type, auto_delete_vec, use_global_delete)
2562 tree base, maxindex, type;
2563 special_function_kind auto_delete_vec;
2564 int use_global_delete;
2566 tree virtual_size;
2567 tree ptype = build_pointer_type (type = complete_type (type));
2568 tree size_exp = size_in_bytes (type);
2570 /* Temporary variables used by the loop. */
2571 tree tbase, tbase_init;
2573 /* This is the body of the loop that implements the deletion of a
2574 single element, and moves temp variables to next elements. */
2575 tree body;
2577 /* This is the LOOP_EXPR that governs the deletion of the elements. */
2578 tree loop;
2580 /* This is the thing that governs what to do after the loop has run. */
2581 tree deallocate_expr = 0;
2583 /* This is the BIND_EXPR which holds the outermost iterator of the
2584 loop. It is convenient to set this variable up and test it before
2585 executing any other code in the loop.
2586 This is also the containing expression returned by this function. */
2587 tree controller = NULL_TREE;
2589 if (! IS_AGGR_TYPE (type) || TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
2591 loop = integer_zero_node;
2592 goto no_destructor;
2595 /* The below is short by the cookie size. */
2596 virtual_size = size_binop (MULT_EXPR, size_exp,
2597 convert (sizetype, maxindex));
2599 tbase = create_temporary_var (ptype);
2600 tbase_init = build_modify_expr (tbase, NOP_EXPR,
2601 fold (build (PLUS_EXPR, ptype,
2602 base,
2603 virtual_size)));
2604 DECL_REGISTER (tbase) = 1;
2605 controller = build (BIND_EXPR, void_type_node, tbase, NULL_TREE, NULL_TREE);
2606 TREE_SIDE_EFFECTS (controller) = 1;
2608 body = NULL_TREE;
2610 body = tree_cons (NULL_TREE,
2611 build_delete (ptype, tbase, sfk_complete_destructor,
2612 LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 1),
2613 body);
2615 body = tree_cons (NULL_TREE,
2616 build_modify_expr (tbase, NOP_EXPR, build (MINUS_EXPR, ptype, tbase, size_exp)),
2617 body);
2619 body = tree_cons (NULL_TREE,
2620 build (EXIT_EXPR, void_type_node,
2621 build (EQ_EXPR, boolean_type_node, base, tbase)),
2622 body);
2624 loop = build (LOOP_EXPR, void_type_node, build_compound_expr (body));
2626 loop = tree_cons (NULL_TREE, tbase_init,
2627 tree_cons (NULL_TREE, loop, NULL_TREE));
2628 loop = build_compound_expr (loop);
2630 no_destructor:
2631 /* If the delete flag is one, or anything else with the low bit set,
2632 delete the storage. */
2633 deallocate_expr = integer_zero_node;
2634 if (auto_delete_vec != sfk_base_destructor)
2636 tree base_tbd;
2638 /* The below is short by the cookie size. */
2639 virtual_size = size_binop (MULT_EXPR, size_exp,
2640 convert (sizetype, maxindex));
2642 if (! TYPE_VEC_NEW_USES_COOKIE (type))
2643 /* no header */
2644 base_tbd = base;
2645 else
2647 tree cookie_size;
2649 cookie_size = get_cookie_size (type);
2650 base_tbd
2651 = cp_convert (ptype,
2652 cp_build_binary_op (MINUS_EXPR,
2653 cp_convert (string_type_node,
2654 base),
2655 cookie_size));
2656 /* True size with header. */
2657 virtual_size = size_binop (PLUS_EXPR, virtual_size, cookie_size);
2660 if (auto_delete_vec == sfk_deleting_destructor)
2661 deallocate_expr = build_x_delete (base_tbd,
2662 2 | use_global_delete,
2663 virtual_size);
2666 if (loop && deallocate_expr != integer_zero_node)
2668 body = tree_cons (NULL_TREE, loop,
2669 tree_cons (NULL_TREE, deallocate_expr, NULL_TREE));
2670 body = build_compound_expr (body);
2672 else
2673 body = loop;
2675 /* Outermost wrapper: If pointer is null, punt. */
2676 body = fold (build (COND_EXPR, void_type_node,
2677 fold (build (NE_EXPR, boolean_type_node, base,
2678 integer_zero_node)),
2679 body, integer_zero_node));
2680 body = build1 (NOP_EXPR, void_type_node, body);
2682 if (controller)
2684 TREE_OPERAND (controller, 1) = body;
2685 return controller;
2687 else
2688 return cp_convert (void_type_node, body);
2691 /* Create an unnamed variable of the indicated TYPE. */
2693 tree
2694 create_temporary_var (type)
2695 tree type;
2697 tree decl;
2699 decl = build_decl (VAR_DECL, NULL_TREE, type);
2700 TREE_USED (decl) = 1;
2701 DECL_ARTIFICIAL (decl) = 1;
2702 DECL_SOURCE_FILE (decl) = input_filename;
2703 DECL_SOURCE_LINE (decl) = lineno;
2704 DECL_IGNORED_P (decl) = 1;
2705 DECL_CONTEXT (decl) = current_function_decl;
2707 return decl;
2710 /* Create a new temporary variable of the indicated TYPE, initialized
2711 to INIT.
2713 It is not entered into current_binding_level, because that breaks
2714 things when it comes time to do final cleanups (which take place
2715 "outside" the binding contour of the function). */
2717 static tree
2718 get_temp_regvar (type, init)
2719 tree type, init;
2721 tree decl;
2723 decl = create_temporary_var (type);
2724 if (building_stmt_tree ())
2725 add_decl_stmt (decl);
2726 if (!building_stmt_tree ())
2727 SET_DECL_RTL (decl, assign_temp (type, 2, 0, 1));
2728 finish_expr_stmt (build_modify_expr (decl, INIT_EXPR, init));
2730 return decl;
2733 /* `build_vec_init' returns tree structure that performs
2734 initialization of a vector of aggregate types.
2736 BASE is a reference to the vector, of ARRAY_TYPE.
2737 INIT is the (possibly NULL) initializer.
2739 FROM_ARRAY is 0 if we should init everything with INIT
2740 (i.e., every element initialized from INIT).
2741 FROM_ARRAY is 1 if we should index into INIT in parallel
2742 with initialization of DECL.
2743 FROM_ARRAY is 2 if we should index into INIT in parallel,
2744 but use assignment instead of initialization. */
2746 tree
2747 build_vec_init (base, init, from_array)
2748 tree base, init;
2749 int from_array;
2751 tree rval;
2752 tree base2 = NULL_TREE;
2753 tree size;
2754 tree itype = NULL_TREE;
2755 tree iterator;
2756 /* The type of the array. */
2757 tree atype = TREE_TYPE (base);
2758 /* The type of an element in the array. */
2759 tree type = TREE_TYPE (atype);
2760 /* The type of a pointer to an element in the array. */
2761 tree ptype;
2762 tree stmt_expr;
2763 tree compound_stmt;
2764 int destroy_temps;
2765 tree try_block = NULL_TREE;
2766 tree try_body = NULL_TREE;
2767 int num_initialized_elts = 0;
2768 tree maxindex = array_type_nelts (TREE_TYPE (base));
2770 if (maxindex == error_mark_node)
2771 return error_mark_node;
2773 /* For g++.ext/arrnew.C. */
2774 if (init && TREE_CODE (init) == CONSTRUCTOR && TREE_TYPE (init) == NULL_TREE)
2775 init = digest_init (atype, init, 0);
2777 if (init && !TYPE_NEEDS_CONSTRUCTING (type)
2778 && ((TREE_CODE (init) == CONSTRUCTOR
2779 /* Don't do this if the CONSTRUCTOR might contain something
2780 that might throw and require us to clean up. */
2781 && (CONSTRUCTOR_ELTS (init) == NULL_TREE
2782 || ! TYPE_HAS_NONTRIVIAL_DESTRUCTOR (target_type (type))))
2783 || from_array))
2785 /* Do non-default initialization of POD arrays resulting from
2786 brace-enclosed initializers. In this case, digest_init and
2787 store_constructor will handle the semantics for us. */
2789 stmt_expr = build (INIT_EXPR, atype, base, init);
2790 TREE_SIDE_EFFECTS (stmt_expr) = 1;
2791 return stmt_expr;
2794 maxindex = cp_convert (ptrdiff_type_node, maxindex);
2795 ptype = build_pointer_type (type);
2796 size = size_in_bytes (type);
2797 if (TREE_CODE (TREE_TYPE (base)) == ARRAY_TYPE)
2798 base = cp_convert (ptype, default_conversion (base));
2800 /* The code we are generating looks like:
2802 T* t1 = (T*) base;
2803 T* rval = t1;
2804 ptrdiff_t iterator = maxindex;
2805 try {
2806 do {
2807 ... initialize *t1 ...
2808 ++t1;
2809 } while (--iterator != -1);
2810 } catch (...) {
2811 ... destroy elements that were constructed ...
2813 return rval;
2815 We can omit the try and catch blocks if we know that the
2816 initialization will never throw an exception, or if the array
2817 elements do not have destructors. We can omit the loop completely if
2818 the elements of the array do not have constructors.
2820 We actually wrap the entire body of the above in a STMT_EXPR, for
2821 tidiness.
2823 When copying from array to another, when the array elements have
2824 only trivial copy constructors, we should use __builtin_memcpy
2825 rather than generating a loop. That way, we could take advantage
2826 of whatever cleverness the back-end has for dealing with copies
2827 of blocks of memory. */
2829 begin_init_stmts (&stmt_expr, &compound_stmt);
2830 destroy_temps = stmts_are_full_exprs_p ();
2831 current_stmt_tree ()->stmts_are_full_exprs_p = 0;
2832 rval = get_temp_regvar (ptype, base);
2833 base = get_temp_regvar (ptype, rval);
2834 iterator = get_temp_regvar (ptrdiff_type_node, maxindex);
2836 /* Protect the entire array initialization so that we can destroy
2837 the partially constructed array if an exception is thrown.
2838 But don't do this if we're assigning. */
2839 if (flag_exceptions && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
2840 && from_array != 2)
2842 try_block = begin_try_block ();
2843 try_body = begin_compound_stmt (/*has_no_scope=*/1);
2846 if (init != NULL_TREE && TREE_CODE (init) == CONSTRUCTOR)
2848 /* Do non-default initialization of non-POD arrays resulting from
2849 brace-enclosed initializers. */
2851 tree elts;
2852 from_array = 0;
2854 for (elts = CONSTRUCTOR_ELTS (init); elts; elts = TREE_CHAIN (elts))
2856 tree elt = TREE_VALUE (elts);
2857 tree baseref = build1 (INDIRECT_REF, type, base);
2859 num_initialized_elts++;
2861 if (IS_AGGR_TYPE (type) || TREE_CODE (type) == ARRAY_TYPE)
2862 finish_expr_stmt (build_aggr_init (baseref, elt, 0));
2863 else
2864 finish_expr_stmt (build_modify_expr (baseref, NOP_EXPR,
2865 elt));
2867 finish_expr_stmt (build_unary_op (PREINCREMENT_EXPR, base, 0));
2868 finish_expr_stmt (build_unary_op (PREDECREMENT_EXPR, iterator, 0));
2871 /* Clear out INIT so that we don't get confused below. */
2872 init = NULL_TREE;
2874 else if (from_array)
2876 /* If initializing one array from another, initialize element by
2877 element. We rely upon the below calls the do argument
2878 checking. */
2879 if (init)
2881 base2 = default_conversion (init);
2882 itype = TREE_TYPE (base2);
2883 base2 = get_temp_regvar (itype, base2);
2884 itype = TREE_TYPE (itype);
2886 else if (TYPE_LANG_SPECIFIC (type)
2887 && TYPE_NEEDS_CONSTRUCTING (type)
2888 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
2890 error ("initializer ends prematurely");
2891 return error_mark_node;
2895 /* Now, default-initialize any remaining elements. We don't need to
2896 do that if a) the type does not need constructing, or b) we've
2897 already initialized all the elements.
2899 We do need to keep going if we're copying an array. */
2901 if (from_array
2902 || (TYPE_NEEDS_CONSTRUCTING (type)
2903 && ! (host_integerp (maxindex, 0)
2904 && (num_initialized_elts
2905 == tree_low_cst (maxindex, 0) + 1))))
2907 /* If the ITERATOR is equal to -1, then we don't have to loop;
2908 we've already initialized all the elements. */
2909 tree if_stmt;
2910 tree do_stmt;
2911 tree do_body;
2912 tree elt_init;
2914 if_stmt = begin_if_stmt ();
2915 finish_if_stmt_cond (build (NE_EXPR, boolean_type_node,
2916 iterator, integer_minus_one_node),
2917 if_stmt);
2919 /* Otherwise, loop through the elements. */
2920 do_stmt = begin_do_stmt ();
2921 do_body = begin_compound_stmt (/*has_no_scope=*/1);
2923 /* When we're not building a statement-tree, things are a little
2924 complicated. If, when we recursively call build_aggr_init,
2925 an expression containing a TARGET_EXPR is expanded, then it
2926 may get a cleanup. Then, the result of that expression is
2927 passed to finish_expr_stmt, which will call
2928 expand_start_target_temps/expand_end_target_temps. However,
2929 the latter call will not cause the cleanup to run because
2930 that block will still be on the block stack. So, we call
2931 expand_start_target_temps here manually; the corresponding
2932 call to expand_end_target_temps below will cause the cleanup
2933 to be performed. */
2934 if (!building_stmt_tree ())
2935 expand_start_target_temps ();
2937 if (from_array)
2939 tree to = build1 (INDIRECT_REF, type, base);
2940 tree from;
2942 if (base2)
2943 from = build1 (INDIRECT_REF, itype, base2);
2944 else
2945 from = NULL_TREE;
2947 if (from_array == 2)
2948 elt_init = build_modify_expr (to, NOP_EXPR, from);
2949 else if (TYPE_NEEDS_CONSTRUCTING (type))
2950 elt_init = build_aggr_init (to, from, 0);
2951 else if (from)
2952 elt_init = build_modify_expr (to, NOP_EXPR, from);
2953 else
2954 abort ();
2956 else if (TREE_CODE (type) == ARRAY_TYPE)
2958 if (init != 0)
2959 sorry
2960 ("cannot initialize multi-dimensional array with initializer");
2961 elt_init = build_vec_init (build1 (INDIRECT_REF, type, base),
2962 0, 0);
2964 else
2965 elt_init = build_aggr_init (build1 (INDIRECT_REF, type, base),
2966 init, 0);
2968 /* The initialization of each array element is a
2969 full-expression, as per core issue 124. */
2970 if (!building_stmt_tree ())
2972 genrtl_expr_stmt (elt_init);
2973 expand_end_target_temps ();
2975 else
2977 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
2978 finish_expr_stmt (elt_init);
2979 current_stmt_tree ()->stmts_are_full_exprs_p = 0;
2982 finish_expr_stmt (build_unary_op (PREINCREMENT_EXPR, base, 0));
2983 if (base2)
2984 finish_expr_stmt (build_unary_op (PREINCREMENT_EXPR, base2, 0));
2986 finish_compound_stmt (/*has_no_scope=*/1, do_body);
2987 finish_do_body (do_stmt);
2988 finish_do_stmt (build (NE_EXPR, boolean_type_node,
2989 build_unary_op (PREDECREMENT_EXPR, iterator, 0),
2990 integer_minus_one_node),
2991 do_stmt);
2993 finish_then_clause (if_stmt);
2994 finish_if_stmt ();
2997 /* Make sure to cleanup any partially constructed elements. */
2998 if (flag_exceptions && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
2999 && from_array != 2)
3001 tree e;
3003 finish_compound_stmt (/*has_no_scope=*/1, try_body);
3004 finish_cleanup_try_block (try_block);
3005 e = build_vec_delete_1 (rval,
3006 cp_build_binary_op (MINUS_EXPR, maxindex,
3007 iterator),
3008 type,
3009 sfk_base_destructor,
3010 /*use_global_delete=*/0);
3011 finish_cleanup (e, try_block);
3014 /* The value of the array initialization is the address of the
3015 first element in the array. */
3016 finish_expr_stmt (rval);
3018 stmt_expr = finish_init_stmts (stmt_expr, compound_stmt);
3019 current_stmt_tree ()->stmts_are_full_exprs_p = destroy_temps;
3020 return stmt_expr;
3023 /* Free up storage of type TYPE, at address ADDR.
3025 TYPE is a POINTER_TYPE and can be ptr_type_node for no special type
3026 of pointer.
3028 VIRTUAL_SIZE is the amount of storage that was allocated, and is
3029 used as the second argument to operator delete. It can include
3030 things like padding and magic size cookies. It has virtual in it,
3031 because if you have a base pointer and you delete through a virtual
3032 destructor, it should be the size of the dynamic object, not the
3033 static object, see Free Store 12.5 ISO C++.
3035 This does not call any destructors. */
3037 tree
3038 build_x_delete (addr, which_delete, virtual_size)
3039 tree addr;
3040 int which_delete;
3041 tree virtual_size;
3043 int use_global_delete = which_delete & 1;
3044 int use_vec_delete = !!(which_delete & 2);
3045 enum tree_code code = use_vec_delete ? VEC_DELETE_EXPR : DELETE_EXPR;
3046 int flags = LOOKUP_NORMAL | (use_global_delete * LOOKUP_GLOBAL);
3048 return build_op_delete_call (code, addr, virtual_size, flags, NULL_TREE);
3051 /* Call the DTOR_KIND destructor for EXP. FLAGS are as for
3052 build_delete. */
3054 static tree
3055 build_dtor_call (exp, dtor_kind, flags)
3056 tree exp;
3057 special_function_kind dtor_kind;
3058 int flags;
3060 tree name;
3062 switch (dtor_kind)
3064 case sfk_complete_destructor:
3065 name = complete_dtor_identifier;
3066 break;
3068 case sfk_base_destructor:
3069 name = base_dtor_identifier;
3070 break;
3072 case sfk_deleting_destructor:
3073 name = deleting_dtor_identifier;
3074 break;
3076 default:
3077 abort ();
3079 return build_method_call (exp, name, NULL_TREE, NULL_TREE, flags);
3082 /* Generate a call to a destructor. TYPE is the type to cast ADDR to.
3083 ADDR is an expression which yields the store to be destroyed.
3084 AUTO_DELETE is the name of the destructor to call, i.e., either
3085 sfk_complete_destructor, sfk_base_destructor, or
3086 sfk_deleting_destructor.
3088 FLAGS is the logical disjunction of zero or more LOOKUP_
3089 flags. See cp-tree.h for more info. */
3091 tree
3092 build_delete (type, addr, auto_delete, flags, use_global_delete)
3093 tree type, addr;
3094 special_function_kind auto_delete;
3095 int flags;
3096 int use_global_delete;
3098 tree expr;
3100 if (addr == error_mark_node)
3101 return error_mark_node;
3103 /* Can happen when CURRENT_EXCEPTION_OBJECT gets its type
3104 set to `error_mark_node' before it gets properly cleaned up. */
3105 if (type == error_mark_node)
3106 return error_mark_node;
3108 type = TYPE_MAIN_VARIANT (type);
3110 if (TREE_CODE (type) == POINTER_TYPE)
3112 type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
3113 if (TREE_CODE (type) == ARRAY_TYPE)
3114 goto handle_array;
3116 if (VOID_TYPE_P (type)
3117 /* We don't want to warn about delete of void*, only other
3118 incomplete types. Deleting other incomplete types
3119 invokes undefined behavior, but it is not ill-formed, so
3120 compile to something that would even do The Right Thing
3121 (TM) should the type have a trivial dtor and no delete
3122 operator. */
3123 || !complete_type_or_diagnostic (type, addr, 1)
3124 || !IS_AGGR_TYPE (type))
3126 /* Call the builtin operator delete. */
3127 return build_builtin_delete_call (addr);
3129 if (TREE_SIDE_EFFECTS (addr))
3130 addr = save_expr (addr);
3132 /* throw away const and volatile on target type of addr */
3133 addr = convert_force (build_pointer_type (type), addr, 0);
3135 else if (TREE_CODE (type) == ARRAY_TYPE)
3137 handle_array:
3138 if (TREE_SIDE_EFFECTS (addr))
3139 addr = save_expr (addr);
3140 if (TYPE_DOMAIN (type) == NULL_TREE)
3142 error ("unknown array size in delete");
3143 return error_mark_node;
3145 return build_vec_delete (addr, array_type_nelts (type),
3146 auto_delete, use_global_delete);
3148 else
3150 /* Don't check PROTECT here; leave that decision to the
3151 destructor. If the destructor is accessible, call it,
3152 else report error. */
3153 addr = build_unary_op (ADDR_EXPR, addr, 0);
3154 if (TREE_SIDE_EFFECTS (addr))
3155 addr = save_expr (addr);
3157 addr = convert_force (build_pointer_type (type), addr, 0);
3160 my_friendly_assert (IS_AGGR_TYPE (type), 220);
3162 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
3164 if (auto_delete != sfk_deleting_destructor)
3165 return void_zero_node;
3167 return build_op_delete_call
3168 (DELETE_EXPR, addr, c_sizeof_nowarn (type),
3169 LOOKUP_NORMAL | (use_global_delete * LOOKUP_GLOBAL),
3170 NULL_TREE);
3172 else
3174 tree do_delete = NULL_TREE;
3175 tree ifexp;
3177 my_friendly_assert (TYPE_HAS_DESTRUCTOR (type), 20011213);
3179 /* For `::delete x', we must not use the deleting destructor
3180 since then we would not be sure to get the global `operator
3181 delete'. */
3182 if (use_global_delete && auto_delete == sfk_deleting_destructor)
3184 /* We will use ADDR multiple times so we must save it. */
3185 addr = save_expr (addr);
3186 /* Delete the object. */
3187 do_delete = build_builtin_delete_call (addr);
3188 /* Otherwise, treat this like a complete object destructor
3189 call. */
3190 auto_delete = sfk_complete_destructor;
3192 /* If the destructor is non-virtual, there is no deleting
3193 variant. Instead, we must explicitly call the appropriate
3194 `operator delete' here. */
3195 else if (!DECL_VIRTUAL_P (CLASSTYPE_DESTRUCTORS (type))
3196 && auto_delete == sfk_deleting_destructor)
3198 /* We will use ADDR multiple times so we must save it. */
3199 addr = save_expr (addr);
3200 /* Build the call. */
3201 do_delete = build_op_delete_call (DELETE_EXPR,
3202 addr,
3203 c_sizeof_nowarn (type),
3204 LOOKUP_NORMAL,
3205 NULL_TREE);
3206 /* Call the complete object destructor. */
3207 auto_delete = sfk_complete_destructor;
3209 else if (auto_delete == sfk_deleting_destructor
3210 && TYPE_GETS_REG_DELETE (type))
3212 /* Make sure we have access to the member op delete, even though
3213 we'll actually be calling it from the destructor. */
3214 build_op_delete_call (DELETE_EXPR, addr, c_sizeof_nowarn (type),
3215 LOOKUP_NORMAL, NULL_TREE);
3218 expr = build_dtor_call (build_indirect_ref (addr, NULL),
3219 auto_delete, flags);
3220 if (do_delete)
3221 expr = build (COMPOUND_EXPR, void_type_node, expr, do_delete);
3223 if (flags & LOOKUP_DESTRUCTOR)
3224 /* Explicit destructor call; don't check for null pointer. */
3225 ifexp = integer_one_node;
3226 else
3227 /* Handle deleting a null pointer. */
3228 ifexp = fold (cp_build_binary_op (NE_EXPR, addr, integer_zero_node));
3230 if (ifexp != integer_one_node)
3231 expr = build (COND_EXPR, void_type_node,
3232 ifexp, expr, void_zero_node);
3234 return expr;
3238 /* At the beginning of a destructor, push cleanups that will call the
3239 destructors for our base classes and members.
3241 Called from begin_destructor_body. */
3243 void
3244 push_base_cleanups ()
3246 tree binfos;
3247 int i, n_baseclasses;
3248 tree member;
3249 tree expr;
3251 /* Run destructors for all virtual baseclasses. */
3252 if (TYPE_USES_VIRTUAL_BASECLASSES (current_class_type))
3254 tree vbases;
3255 tree cond = (condition_conversion
3256 (build (BIT_AND_EXPR, integer_type_node,
3257 current_in_charge_parm,
3258 integer_two_node)));
3260 vbases = CLASSTYPE_VBASECLASSES (current_class_type);
3261 /* The CLASSTYPE_VBASECLASSES list is in initialization
3262 order, which is also the right order for pushing cleanups. */
3263 for (; vbases;
3264 vbases = TREE_CHAIN (vbases))
3266 tree vbase = TREE_VALUE (vbases);
3267 tree base_type = BINFO_TYPE (vbase);
3269 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (base_type))
3271 expr = build_scoped_method_call (current_class_ref, vbase,
3272 base_dtor_identifier,
3273 NULL_TREE);
3274 expr = build (COND_EXPR, void_type_node, cond,
3275 expr, void_zero_node);
3276 finish_decl_cleanup (NULL_TREE, expr);
3281 binfos = BINFO_BASETYPES (TYPE_BINFO (current_class_type));
3282 n_baseclasses = CLASSTYPE_N_BASECLASSES (current_class_type);
3284 /* Take care of the remaining baseclasses. */
3285 for (i = 0; i < n_baseclasses; i++)
3287 tree base_binfo = TREE_VEC_ELT (binfos, i);
3288 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (BINFO_TYPE (base_binfo))
3289 || TREE_VIA_VIRTUAL (base_binfo))
3290 continue;
3292 expr = build_scoped_method_call (current_class_ref, base_binfo,
3293 base_dtor_identifier,
3294 NULL_TREE);
3296 finish_decl_cleanup (NULL_TREE, expr);
3299 for (member = TYPE_FIELDS (current_class_type); member;
3300 member = TREE_CHAIN (member))
3302 if (TREE_CODE (member) != FIELD_DECL)
3303 continue;
3304 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (member)))
3306 tree this_member = (build_component_ref
3307 (current_class_ref, DECL_NAME (member),
3308 NULL_TREE, 0));
3309 tree this_type = TREE_TYPE (member);
3310 expr = build_delete (this_type, this_member,
3311 sfk_complete_destructor,
3312 LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR|LOOKUP_NORMAL,
3314 finish_decl_cleanup (NULL_TREE, expr);
3319 /* For type TYPE, delete the virtual baseclass objects of DECL. */
3321 tree
3322 build_vbase_delete (type, decl)
3323 tree type, decl;
3325 tree vbases = CLASSTYPE_VBASECLASSES (type);
3326 tree result = NULL_TREE;
3327 tree addr = build_unary_op (ADDR_EXPR, decl, 0);
3329 my_friendly_assert (addr != error_mark_node, 222);
3331 while (vbases)
3333 tree this_addr
3334 = convert_force (build_pointer_type (BINFO_TYPE (TREE_VALUE (vbases))),
3335 addr, 0);
3336 result = tree_cons (NULL_TREE,
3337 build_delete (TREE_TYPE (this_addr), this_addr,
3338 sfk_base_destructor,
3339 LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 0),
3340 result);
3341 vbases = TREE_CHAIN (vbases);
3343 return build_compound_expr (nreverse (result));
3346 /* Build a C++ vector delete expression.
3347 MAXINDEX is the number of elements to be deleted.
3348 ELT_SIZE is the nominal size of each element in the vector.
3349 BASE is the expression that should yield the store to be deleted.
3350 This function expands (or synthesizes) these calls itself.
3351 AUTO_DELETE_VEC says whether the container (vector) should be deallocated.
3353 This also calls delete for virtual baseclasses of elements of the vector.
3355 Update: MAXINDEX is no longer needed. The size can be extracted from the
3356 start of the vector for pointers, and from the type for arrays. We still
3357 use MAXINDEX for arrays because it happens to already have one of the
3358 values we'd have to extract. (We could use MAXINDEX with pointers to
3359 confirm the size, and trap if the numbers differ; not clear that it'd
3360 be worth bothering.) */
3362 tree
3363 build_vec_delete (base, maxindex, auto_delete_vec, use_global_delete)
3364 tree base, maxindex;
3365 special_function_kind auto_delete_vec;
3366 int use_global_delete;
3368 tree type;
3370 if (TREE_CODE (base) == OFFSET_REF)
3371 base = resolve_offset_ref (base);
3373 type = TREE_TYPE (base);
3375 base = stabilize_reference (base);
3377 /* Since we can use base many times, save_expr it. */
3378 if (TREE_SIDE_EFFECTS (base))
3379 base = save_expr (base);
3381 if (TREE_CODE (type) == POINTER_TYPE)
3383 /* Step back one from start of vector, and read dimension. */
3384 tree cookie_addr;
3386 type = strip_array_types (TREE_TYPE (type));
3387 cookie_addr = build (MINUS_EXPR,
3388 build_pointer_type (sizetype),
3389 base,
3390 TYPE_SIZE_UNIT (sizetype));
3391 maxindex = build_indirect_ref (cookie_addr, NULL);
3393 else if (TREE_CODE (type) == ARRAY_TYPE)
3395 /* get the total number of things in the array, maxindex is a bad name */
3396 maxindex = array_type_nelts_total (type);
3397 type = strip_array_types (type);
3398 base = build_unary_op (ADDR_EXPR, base, 1);
3400 else
3402 if (base != error_mark_node)
3403 error ("type to vector delete is neither pointer or array type");
3404 return error_mark_node;
3407 return build_vec_delete_1 (base, maxindex, type, auto_delete_vec,
3408 use_global_delete);