Merge branch 'master' of ssh://crater.dragonflybsd.org/repository/git/dragonfly
[dragonfly.git] / contrib / gcc-3.4 / gcc / cp / init.c
blob4e3cd4bef427e4fb9c81d4cdcf6faa53e3c562fe
1 /* Handle initialization things in C++.
2 Copyright (C) 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
4 Contributed by Michael Tiemann (tiemann@cygnus.com)
6 This file is part of GCC.
8 GCC 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 GCC 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 GCC; 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 "coretypes.h"
28 #include "tm.h"
29 #include "tree.h"
30 #include "rtl.h"
31 #include "expr.h"
32 #include "cp-tree.h"
33 #include "flags.h"
34 #include "output.h"
35 #include "except.h"
36 #include "toplev.h"
38 static bool begin_init_stmts (tree *, tree *);
39 static tree finish_init_stmts (bool, tree, tree);
40 static void construct_virtual_base (tree, tree);
41 static void expand_aggr_init_1 (tree, tree, tree, tree, int);
42 static void expand_default_init (tree, tree, tree, tree, int);
43 static tree build_vec_delete_1 (tree, tree, tree, special_function_kind, int);
44 static void perform_member_init (tree, tree);
45 static tree build_builtin_delete_call (tree);
46 static int member_init_ok_or_else (tree, tree, tree);
47 static void expand_virtual_init (tree, tree);
48 static tree sort_mem_initializers (tree, tree);
49 static tree initializing_context (tree);
50 static void expand_cleanup_for_base (tree, tree);
51 static tree get_temp_regvar (tree, tree);
52 static tree dfs_initialize_vtbl_ptrs (tree, void *);
53 static tree build_default_init (tree, tree);
54 static tree build_new_1 (tree);
55 static tree get_cookie_size (tree);
56 static tree build_dtor_call (tree, special_function_kind, int);
57 static tree build_field_list (tree, tree, int *);
58 static tree build_vtbl_address (tree);
60 /* We are about to generate some complex initialization code.
61 Conceptually, it is all a single expression. However, we may want
62 to include conditionals, loops, and other such statement-level
63 constructs. Therefore, we build the initialization code inside a
64 statement-expression. This function starts such an expression.
65 STMT_EXPR_P and COMPOUND_STMT_P are filled in by this function;
66 pass them back to finish_init_stmts when the expression is
67 complete. */
69 static bool
70 begin_init_stmts (tree *stmt_expr_p, tree *compound_stmt_p)
72 bool is_global = !building_stmt_tree ();
74 *stmt_expr_p = begin_stmt_expr ();
75 *compound_stmt_p = begin_compound_stmt (/*has_no_scope=*/true);
77 return is_global;
80 /* Finish out the statement-expression begun by the previous call to
81 begin_init_stmts. Returns the statement-expression itself. */
83 static tree
84 finish_init_stmts (bool is_global, tree stmt_expr, tree compound_stmt)
86 finish_compound_stmt (compound_stmt);
88 stmt_expr = finish_stmt_expr (stmt_expr, true);
90 my_friendly_assert (!building_stmt_tree () == is_global, 20030726);
92 return stmt_expr;
95 /* Constructors */
97 /* Called from initialize_vtbl_ptrs via dfs_walk. BINFO is the base
98 which we want to initialize the vtable pointer for, DATA is
99 TREE_LIST whose TREE_VALUE is the this ptr expression. */
101 static tree
102 dfs_initialize_vtbl_ptrs (tree binfo, void *data)
104 if ((!BINFO_PRIMARY_P (binfo) || TREE_VIA_VIRTUAL (binfo))
105 && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
107 tree base_ptr = TREE_VALUE ((tree) data);
109 base_ptr = build_base_path (PLUS_EXPR, base_ptr, binfo, /*nonnull=*/1);
111 expand_virtual_init (binfo, base_ptr);
114 BINFO_MARKED (binfo) = 1;
116 return NULL_TREE;
119 /* Initialize all the vtable pointers in the object pointed to by
120 ADDR. */
122 void
123 initialize_vtbl_ptrs (tree addr)
125 tree list;
126 tree type;
128 type = TREE_TYPE (TREE_TYPE (addr));
129 list = build_tree_list (type, addr);
131 /* Walk through the hierarchy, initializing the vptr in each base
132 class. We do these in pre-order because we can't find the virtual
133 bases for a class until we've initialized the vtbl for that
134 class. */
135 dfs_walk_real (TYPE_BINFO (type), dfs_initialize_vtbl_ptrs,
136 NULL, unmarkedp, list);
137 dfs_walk (TYPE_BINFO (type), dfs_unmark, markedp, type);
140 /* Return an expression for the zero-initialization of an object with
141 type T. This expression will either be a constant (in the case
142 that T is a scalar), or a CONSTRUCTOR (in the case that T is an
143 aggregate). In either case, the value can be used as DECL_INITIAL
144 for a decl of the indicated TYPE; it is a valid static initializer.
145 If NELTS is non-NULL, and TYPE is an ARRAY_TYPE, NELTS is the
146 number of elements in the array. If STATIC_STORAGE_P is TRUE,
147 initializers are only generated for entities for which
148 zero-initialization does not simply mean filling the storage with
149 zero bytes. */
151 tree
152 build_zero_init (tree type, tree nelts, bool static_storage_p)
154 tree init = NULL_TREE;
156 /* [dcl.init]
158 To zero-initialization storage for an object of type T means:
160 -- if T is a scalar type, the storage is set to the value of zero
161 converted to T.
163 -- if T is a non-union class type, the storage for each nonstatic
164 data member and each base-class subobject is zero-initialized.
166 -- if T is a union type, the storage for its first data member is
167 zero-initialized.
169 -- if T is an array type, the storage for each element is
170 zero-initialized.
172 -- if T is a reference type, no initialization is performed. */
174 my_friendly_assert (nelts == NULL_TREE || TREE_CODE (nelts) == INTEGER_CST,
175 20030618);
177 if (type == error_mark_node)
179 else if (static_storage_p && zero_init_p (type))
180 /* In order to save space, we do not explicitly build initializers
181 for items that do not need them. GCC's semantics are that
182 items with static storage duration that are not otherwise
183 initialized are initialized to zero. */
185 else if (SCALAR_TYPE_P (type))
186 init = convert (type, integer_zero_node);
187 else if (CLASS_TYPE_P (type))
189 tree field;
190 tree inits;
192 /* Build a constructor to contain the initializations. */
193 init = build_constructor (type, NULL_TREE);
194 /* Iterate over the fields, building initializations. */
195 inits = NULL_TREE;
196 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
198 if (TREE_CODE (field) != FIELD_DECL)
199 continue;
201 /* Note that for class types there will be FIELD_DECLs
202 corresponding to base classes as well. Thus, iterating
203 over TYPE_FIELDs will result in correct initialization of
204 all of the subobjects. */
205 if (static_storage_p && !zero_init_p (TREE_TYPE (field)))
206 inits = tree_cons (field,
207 build_zero_init (TREE_TYPE (field),
208 /*nelts=*/NULL_TREE,
209 static_storage_p),
210 inits);
212 /* For unions, only the first field is initialized. */
213 if (TREE_CODE (type) == UNION_TYPE)
214 break;
216 CONSTRUCTOR_ELTS (init) = nreverse (inits);
218 else if (TREE_CODE (type) == ARRAY_TYPE)
220 tree max_index;
221 tree inits;
223 /* Build a constructor to contain the initializations. */
224 init = build_constructor (type, NULL_TREE);
225 /* Iterate over the array elements, building initializations. */
226 inits = NULL_TREE;
227 max_index = nelts ? nelts : array_type_nelts (type);
228 my_friendly_assert (TREE_CODE (max_index) == INTEGER_CST, 20030618);
230 /* A zero-sized array, which is accepted as an extension, will
231 have an upper bound of -1. */
232 if (!tree_int_cst_equal (max_index, integer_minus_one_node))
234 tree elt_init = build_zero_init (TREE_TYPE (type),
235 /*nelts=*/NULL_TREE,
236 static_storage_p);
237 tree range = build (RANGE_EXPR,
238 sizetype, size_zero_node, max_index);
240 inits = tree_cons (range, elt_init, inits);
243 CONSTRUCTOR_ELTS (init) = nreverse (inits);
245 else if (TREE_CODE (type) == REFERENCE_TYPE)
247 else
248 abort ();
250 /* In all cases, the initializer is a constant. */
251 if (init)
252 TREE_CONSTANT (init) = 1;
254 return init;
257 /* Build an expression for the default-initialization of an object of
258 the indicated TYPE. If NELTS is non-NULL, and TYPE is an
259 ARRAY_TYPE, NELTS is the number of elements in the array. If
260 initialization of TYPE requires calling constructors, this function
261 returns NULL_TREE; the caller is responsible for arranging for the
262 constructors to be called. */
264 static tree
265 build_default_init (tree type, tree nelts)
267 /* [dcl.init]:
269 To default-initialize an object of type T means:
271 --if T is a non-POD class type (clause _class_), the default construc-
272 tor for T is called (and the initialization is ill-formed if T has
273 no accessible default constructor);
275 --if T is an array type, each element is default-initialized;
277 --otherwise, the storage for the object is zero-initialized.
279 A program that calls for default-initialization of an entity of refer-
280 ence type is ill-formed. */
282 /* If TYPE_NEEDS_CONSTRUCTING is true, the caller is responsible for
283 performing the initialization. This is confusing in that some
284 non-PODs do not have TYPE_NEEDS_CONSTRUCTING set. (For example,
285 a class with a pointer-to-data member as a non-static data member
286 does not have TYPE_NEEDS_CONSTRUCTING set.) Therefore, we end up
287 passing non-PODs to build_zero_init below, which is contrary to
288 the semantics quoted above from [dcl.init].
290 It happens, however, that the behavior of the constructor the
291 standard says we should have generated would be precisely the
292 same as that obtained by calling build_zero_init below, so things
293 work out OK. */
294 if (TYPE_NEEDS_CONSTRUCTING (type)
295 || (nelts && TREE_CODE (nelts) != INTEGER_CST))
296 return NULL_TREE;
298 /* At this point, TYPE is either a POD class type, an array of POD
299 classes, or something even more innocuous. */
300 return build_zero_init (type, nelts, /*static_storage_p=*/false);
303 /* Initialize MEMBER, a FIELD_DECL, with INIT, a TREE_LIST of
304 arguments. If TREE_LIST is void_type_node, an empty initializer
305 list was given; if NULL_TREE no initializer was given. */
307 static void
308 perform_member_init (tree member, tree init)
310 tree decl;
311 tree type = TREE_TYPE (member);
312 bool explicit;
314 explicit = (init != NULL_TREE);
316 /* Effective C++ rule 12 requires that all data members be
317 initialized. */
318 if (warn_ecpp && !explicit && TREE_CODE (type) != ARRAY_TYPE)
319 warning ("`%D' should be initialized in the member initialization "
320 "list",
321 member);
323 if (init == void_type_node)
324 init = NULL_TREE;
326 /* Get an lvalue for the data member. */
327 decl = build_class_member_access_expr (current_class_ref, member,
328 /*access_path=*/NULL_TREE,
329 /*preserve_reference=*/true);
330 if (decl == error_mark_node)
331 return;
333 /* Deal with this here, as we will get confused if we try to call the
334 assignment op for an anonymous union. This can happen in a
335 synthesized copy constructor. */
336 if (ANON_AGGR_TYPE_P (type))
338 if (init)
340 init = build (INIT_EXPR, type, decl, TREE_VALUE (init));
341 finish_expr_stmt (init);
344 else if (TYPE_NEEDS_CONSTRUCTING (type)
345 || (init && TYPE_HAS_CONSTRUCTOR (type)))
347 if (explicit
348 && TREE_CODE (type) == ARRAY_TYPE
349 && init != NULL_TREE
350 && TREE_CHAIN (init) == NULL_TREE
351 && TREE_CODE (TREE_TYPE (TREE_VALUE (init))) == ARRAY_TYPE)
353 /* Initialization of one array from another. */
354 finish_expr_stmt (build_vec_init (decl, NULL_TREE, TREE_VALUE (init),
355 /* from_array=*/1));
357 else
358 finish_expr_stmt (build_aggr_init (decl, init, 0));
360 else
362 if (init == NULL_TREE)
364 if (explicit)
366 init = build_default_init (type, /*nelts=*/NULL_TREE);
367 if (TREE_CODE (type) == REFERENCE_TYPE)
368 warning
369 ("default-initialization of `%#D', which has reference type",
370 member);
372 /* member traversal: note it leaves init NULL */
373 else if (TREE_CODE (type) == REFERENCE_TYPE)
374 pedwarn ("uninitialized reference member `%D'", member);
375 else if (CP_TYPE_CONST_P (type))
376 pedwarn ("uninitialized member `%D' with `const' type `%T'",
377 member, type);
379 else if (TREE_CODE (init) == TREE_LIST)
380 /* There was an explicit member initialization. Do some work
381 in that case. */
382 init = build_x_compound_expr_from_list (init, "member initializer");
384 if (init)
385 finish_expr_stmt (build_modify_expr (decl, INIT_EXPR, init));
388 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
390 tree expr;
392 expr = build_class_member_access_expr (current_class_ref, member,
393 /*access_path=*/NULL_TREE,
394 /*preserve_reference=*/false);
395 expr = build_delete (type, expr, sfk_complete_destructor,
396 LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR, 0);
398 if (expr != error_mark_node)
399 finish_eh_cleanup (expr);
403 /* Returns a TREE_LIST containing (as the TREE_PURPOSE of each node) all
404 the FIELD_DECLs on the TYPE_FIELDS list for T, in reverse order. */
406 static tree
407 build_field_list (tree t, tree list, int *uses_unions_p)
409 tree fields;
411 *uses_unions_p = 0;
413 /* Note whether or not T is a union. */
414 if (TREE_CODE (t) == UNION_TYPE)
415 *uses_unions_p = 1;
417 for (fields = TYPE_FIELDS (t); fields; fields = TREE_CHAIN (fields))
419 /* Skip CONST_DECLs for enumeration constants and so forth. */
420 if (TREE_CODE (fields) != FIELD_DECL || DECL_ARTIFICIAL (fields))
421 continue;
423 /* Keep track of whether or not any fields are unions. */
424 if (TREE_CODE (TREE_TYPE (fields)) == UNION_TYPE)
425 *uses_unions_p = 1;
427 /* For an anonymous struct or union, we must recursively
428 consider the fields of the anonymous type. They can be
429 directly initialized from the constructor. */
430 if (ANON_AGGR_TYPE_P (TREE_TYPE (fields)))
432 /* Add this field itself. Synthesized copy constructors
433 initialize the entire aggregate. */
434 list = tree_cons (fields, NULL_TREE, list);
435 /* And now add the fields in the anonymous aggregate. */
436 list = build_field_list (TREE_TYPE (fields), list,
437 uses_unions_p);
439 /* Add this field. */
440 else if (DECL_NAME (fields))
441 list = tree_cons (fields, NULL_TREE, list);
444 return list;
447 /* The MEM_INITS are a TREE_LIST. The TREE_PURPOSE of each list gives
448 a FIELD_DECL or BINFO in T that needs initialization. The
449 TREE_VALUE gives the initializer, or list of initializer arguments.
451 Return a TREE_LIST containing all of the initializations required
452 for T, in the order in which they should be performed. The output
453 list has the same format as the input. */
455 static tree
456 sort_mem_initializers (tree t, tree mem_inits)
458 tree init;
459 tree base;
460 tree sorted_inits;
461 tree next_subobject;
462 int i;
463 int uses_unions_p;
465 /* Build up a list of initializations. The TREE_PURPOSE of entry
466 will be the subobject (a FIELD_DECL or BINFO) to initialize. The
467 TREE_VALUE will be the constructor arguments, or NULL if no
468 explicit initialization was provided. */
469 sorted_inits = NULL_TREE;
470 /* Process the virtual bases. */
471 for (base = CLASSTYPE_VBASECLASSES (t); base; base = TREE_CHAIN (base))
472 sorted_inits = tree_cons (TREE_VALUE (base), NULL_TREE, sorted_inits);
473 /* Process the direct bases. */
474 for (i = 0; i < CLASSTYPE_N_BASECLASSES (t); ++i)
476 base = BINFO_BASETYPE (TYPE_BINFO (t), i);
477 if (!TREE_VIA_VIRTUAL (base))
478 sorted_inits = tree_cons (base, NULL_TREE, sorted_inits);
480 /* Process the non-static data members. */
481 sorted_inits = build_field_list (t, sorted_inits, &uses_unions_p);
482 /* Reverse the entire list of initializations, so that they are in
483 the order that they will actually be performed. */
484 sorted_inits = nreverse (sorted_inits);
486 /* If the user presented the initializers in an order different from
487 that in which they will actually occur, we issue a warning. Keep
488 track of the next subobject which can be explicitly initialized
489 without issuing a warning. */
490 next_subobject = sorted_inits;
492 /* Go through the explicit initializers, filling in TREE_PURPOSE in
493 the SORTED_INITS. */
494 for (init = mem_inits; init; init = TREE_CHAIN (init))
496 tree subobject;
497 tree subobject_init;
499 subobject = TREE_PURPOSE (init);
501 /* If the explicit initializers are in sorted order, then
502 SUBOBJECT will be NEXT_SUBOBJECT, or something following
503 it. */
504 for (subobject_init = next_subobject;
505 subobject_init;
506 subobject_init = TREE_CHAIN (subobject_init))
507 if (TREE_PURPOSE (subobject_init) == subobject)
508 break;
510 /* Issue a warning if the explicit initializer order does not
511 match that which will actually occur. */
512 if (warn_reorder && !subobject_init)
514 if (TREE_CODE (TREE_PURPOSE (next_subobject)) == FIELD_DECL)
515 cp_warning_at ("`%D' will be initialized after",
516 TREE_PURPOSE (next_subobject));
517 else
518 warning ("base `%T' will be initialized after",
519 TREE_PURPOSE (next_subobject));
520 if (TREE_CODE (subobject) == FIELD_DECL)
521 cp_warning_at (" `%#D'", subobject);
522 else
523 warning (" base `%T'", subobject);
524 warning (" when initialized here");
527 /* Look again, from the beginning of the list. */
528 if (!subobject_init)
530 subobject_init = sorted_inits;
531 while (TREE_PURPOSE (subobject_init) != subobject)
532 subobject_init = TREE_CHAIN (subobject_init);
535 /* It is invalid to initialize the same subobject more than
536 once. */
537 if (TREE_VALUE (subobject_init))
539 if (TREE_CODE (subobject) == FIELD_DECL)
540 error ("multiple initializations given for `%D'", subobject);
541 else
542 error ("multiple initializations given for base `%T'",
543 subobject);
546 /* Record the initialization. */
547 TREE_VALUE (subobject_init) = TREE_VALUE (init);
548 next_subobject = subobject_init;
551 /* [class.base.init]
553 If a ctor-initializer specifies more than one mem-initializer for
554 multiple members of the same union (including members of
555 anonymous unions), the ctor-initializer is ill-formed. */
556 if (uses_unions_p)
558 tree last_field = NULL_TREE;
559 for (init = sorted_inits; init; init = TREE_CHAIN (init))
561 tree field;
562 tree field_type;
563 int done;
565 /* Skip uninitialized members and base classes. */
566 if (!TREE_VALUE (init)
567 || TREE_CODE (TREE_PURPOSE (init)) != FIELD_DECL)
568 continue;
569 /* See if this field is a member of a union, or a member of a
570 structure contained in a union, etc. */
571 field = TREE_PURPOSE (init);
572 for (field_type = DECL_CONTEXT (field);
573 !same_type_p (field_type, t);
574 field_type = TYPE_CONTEXT (field_type))
575 if (TREE_CODE (field_type) == UNION_TYPE)
576 break;
577 /* If this field is not a member of a union, skip it. */
578 if (TREE_CODE (field_type) != UNION_TYPE)
579 continue;
581 /* It's only an error if we have two initializers for the same
582 union type. */
583 if (!last_field)
585 last_field = field;
586 continue;
589 /* See if LAST_FIELD and the field initialized by INIT are
590 members of the same union. If so, there's a problem,
591 unless they're actually members of the same structure
592 which is itself a member of a union. For example, given:
594 union { struct { int i; int j; }; };
596 initializing both `i' and `j' makes sense. */
597 field_type = DECL_CONTEXT (field);
598 done = 0;
601 tree last_field_type;
603 last_field_type = DECL_CONTEXT (last_field);
604 while (1)
606 if (same_type_p (last_field_type, field_type))
608 if (TREE_CODE (field_type) == UNION_TYPE)
609 error ("initializations for multiple members of `%T'",
610 last_field_type);
611 done = 1;
612 break;
615 if (same_type_p (last_field_type, t))
616 break;
618 last_field_type = TYPE_CONTEXT (last_field_type);
621 /* If we've reached the outermost class, then we're
622 done. */
623 if (same_type_p (field_type, t))
624 break;
626 field_type = TYPE_CONTEXT (field_type);
628 while (!done);
630 last_field = field;
634 return sorted_inits;
637 /* Initialize all bases and members of CURRENT_CLASS_TYPE. MEM_INITS
638 is a TREE_LIST giving the explicit mem-initializer-list for the
639 constructor. The TREE_PURPOSE of each entry is a subobject (a
640 FIELD_DECL or a BINFO) of the CURRENT_CLASS_TYPE. The TREE_VALUE
641 is a TREE_LIST giving the arguments to the constructor or
642 void_type_node for an empty list of arguments. */
644 void
645 emit_mem_initializers (tree mem_inits)
647 /* Sort the mem-initializers into the order in which the
648 initializations should be performed. */
649 mem_inits = sort_mem_initializers (current_class_type, mem_inits);
651 in_base_initializer = 1;
653 /* Initialize base classes. */
654 while (mem_inits
655 && TREE_CODE (TREE_PURPOSE (mem_inits)) != FIELD_DECL)
657 tree subobject = TREE_PURPOSE (mem_inits);
658 tree arguments = TREE_VALUE (mem_inits);
660 /* If these initializations are taking place in a copy
661 constructor, the base class should probably be explicitly
662 initialized. */
663 if (extra_warnings && !arguments
664 && DECL_COPY_CONSTRUCTOR_P (current_function_decl)
665 && TYPE_NEEDS_CONSTRUCTING (BINFO_TYPE (subobject)))
666 warning ("base class `%#T' should be explicitly initialized in the "
667 "copy constructor",
668 BINFO_TYPE (subobject));
670 /* If an explicit -- but empty -- initializer list was present,
671 treat it just like default initialization at this point. */
672 if (arguments == void_type_node)
673 arguments = NULL_TREE;
675 /* Initialize the base. */
676 if (TREE_VIA_VIRTUAL (subobject))
677 construct_virtual_base (subobject, arguments);
678 else
680 tree base_addr;
682 base_addr = build_base_path (PLUS_EXPR, current_class_ptr,
683 subobject, 1);
684 expand_aggr_init_1 (subobject, NULL_TREE,
685 build_indirect_ref (base_addr, NULL),
686 arguments,
687 LOOKUP_NORMAL);
688 expand_cleanup_for_base (subobject, NULL_TREE);
691 mem_inits = TREE_CHAIN (mem_inits);
693 in_base_initializer = 0;
695 /* Initialize the vptrs. */
696 initialize_vtbl_ptrs (current_class_ptr);
698 /* Initialize the data members. */
699 while (mem_inits)
701 perform_member_init (TREE_PURPOSE (mem_inits),
702 TREE_VALUE (mem_inits));
703 mem_inits = TREE_CHAIN (mem_inits);
707 /* Returns the address of the vtable (i.e., the value that should be
708 assigned to the vptr) for BINFO. */
710 static tree
711 build_vtbl_address (tree binfo)
713 tree binfo_for = binfo;
714 tree vtbl;
716 if (BINFO_VPTR_INDEX (binfo) && TREE_VIA_VIRTUAL (binfo)
717 && BINFO_PRIMARY_P (binfo))
718 /* If this is a virtual primary base, then the vtable we want to store
719 is that for the base this is being used as the primary base of. We
720 can't simply skip the initialization, because we may be expanding the
721 inits of a subobject constructor where the virtual base layout
722 can be different. */
723 while (BINFO_PRIMARY_BASE_OF (binfo_for))
724 binfo_for = BINFO_PRIMARY_BASE_OF (binfo_for);
726 /* Figure out what vtable BINFO's vtable is based on, and mark it as
727 used. */
728 vtbl = get_vtbl_decl_for_binfo (binfo_for);
729 assemble_external (vtbl);
730 TREE_USED (vtbl) = 1;
732 /* Now compute the address to use when initializing the vptr. */
733 vtbl = BINFO_VTABLE (binfo_for);
734 if (TREE_CODE (vtbl) == VAR_DECL)
736 vtbl = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (vtbl)), vtbl);
737 TREE_CONSTANT (vtbl) = 1;
740 return vtbl;
743 /* This code sets up the virtual function tables appropriate for
744 the pointer DECL. It is a one-ply initialization.
746 BINFO is the exact type that DECL is supposed to be. In
747 multiple inheritance, this might mean "C's A" if C : A, B. */
749 static void
750 expand_virtual_init (tree binfo, tree decl)
752 tree vtbl, vtbl_ptr;
753 tree vtt_index;
755 /* Compute the initializer for vptr. */
756 vtbl = build_vtbl_address (binfo);
758 /* We may get this vptr from a VTT, if this is a subobject
759 constructor or subobject destructor. */
760 vtt_index = BINFO_VPTR_INDEX (binfo);
761 if (vtt_index)
763 tree vtbl2;
764 tree vtt_parm;
766 /* Compute the value to use, when there's a VTT. */
767 vtt_parm = current_vtt_parm;
768 vtbl2 = build (PLUS_EXPR,
769 TREE_TYPE (vtt_parm),
770 vtt_parm,
771 vtt_index);
772 vtbl2 = build1 (INDIRECT_REF, TREE_TYPE (vtbl), vtbl2);
774 /* The actual initializer is the VTT value only in the subobject
775 constructor. In maybe_clone_body we'll substitute NULL for
776 the vtt_parm in the case of the non-subobject constructor. */
777 vtbl = build (COND_EXPR,
778 TREE_TYPE (vtbl),
779 build (EQ_EXPR, boolean_type_node,
780 current_in_charge_parm, integer_zero_node),
781 vtbl2,
782 vtbl);
785 /* Compute the location of the vtpr. */
786 vtbl_ptr = build_vfield_ref (build_indirect_ref (decl, NULL),
787 TREE_TYPE (binfo));
788 my_friendly_assert (vtbl_ptr != error_mark_node, 20010730);
790 /* Assign the vtable to the vptr. */
791 vtbl = convert_force (TREE_TYPE (vtbl_ptr), vtbl, 0);
792 finish_expr_stmt (build_modify_expr (vtbl_ptr, NOP_EXPR, vtbl));
795 /* If an exception is thrown in a constructor, those base classes already
796 constructed must be destroyed. This function creates the cleanup
797 for BINFO, which has just been constructed. If FLAG is non-NULL,
798 it is a DECL which is nonzero when this base needs to be
799 destroyed. */
801 static void
802 expand_cleanup_for_base (tree binfo, tree flag)
804 tree expr;
806 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (BINFO_TYPE (binfo)))
807 return;
809 /* Call the destructor. */
810 expr = build_special_member_call (current_class_ref,
811 base_dtor_identifier,
812 NULL_TREE,
813 binfo,
814 LOOKUP_NORMAL | LOOKUP_NONVIRTUAL);
815 if (flag)
816 expr = fold (build (COND_EXPR, void_type_node,
817 c_common_truthvalue_conversion (flag),
818 expr, integer_zero_node));
820 finish_eh_cleanup (expr);
823 /* Construct the virtual base-class VBASE passing the ARGUMENTS to its
824 constructor. */
826 static void
827 construct_virtual_base (tree vbase, tree arguments)
829 tree inner_if_stmt;
830 tree compound_stmt;
831 tree exp;
832 tree flag;
834 /* If there are virtual base classes with destructors, we need to
835 emit cleanups to destroy them if an exception is thrown during
836 the construction process. These exception regions (i.e., the
837 period during which the cleanups must occur) begin from the time
838 the construction is complete to the end of the function. If we
839 create a conditional block in which to initialize the
840 base-classes, then the cleanup region for the virtual base begins
841 inside a block, and ends outside of that block. This situation
842 confuses the sjlj exception-handling code. Therefore, we do not
843 create a single conditional block, but one for each
844 initialization. (That way the cleanup regions always begin
845 in the outer block.) We trust the back-end to figure out
846 that the FLAG will not change across initializations, and
847 avoid doing multiple tests. */
848 flag = TREE_CHAIN (DECL_ARGUMENTS (current_function_decl));
849 inner_if_stmt = begin_if_stmt ();
850 finish_if_stmt_cond (flag, inner_if_stmt);
851 compound_stmt = begin_compound_stmt (/*has_no_scope=*/true);
853 /* Compute the location of the virtual base. If we're
854 constructing virtual bases, then we must be the most derived
855 class. Therefore, we don't have to look up the virtual base;
856 we already know where it is. */
857 exp = convert_to_base_statically (current_class_ref, vbase);
859 expand_aggr_init_1 (vbase, current_class_ref, exp, arguments,
860 LOOKUP_COMPLAIN);
861 finish_compound_stmt (compound_stmt);
862 finish_then_clause (inner_if_stmt);
863 finish_if_stmt ();
865 expand_cleanup_for_base (vbase, flag);
868 /* Find the context in which this FIELD can be initialized. */
870 static tree
871 initializing_context (tree field)
873 tree t = DECL_CONTEXT (field);
875 /* Anonymous union members can be initialized in the first enclosing
876 non-anonymous union context. */
877 while (t && ANON_AGGR_TYPE_P (t))
878 t = TYPE_CONTEXT (t);
879 return t;
882 /* Function to give error message if member initialization specification
883 is erroneous. FIELD is the member we decided to initialize.
884 TYPE is the type for which the initialization is being performed.
885 FIELD must be a member of TYPE.
887 MEMBER_NAME is the name of the member. */
889 static int
890 member_init_ok_or_else (tree field, tree type, tree member_name)
892 if (field == error_mark_node)
893 return 0;
894 if (!field)
896 error ("class `%T' does not have any field named `%D'", type,
897 member_name);
898 return 0;
900 if (TREE_CODE (field) == VAR_DECL)
902 error ("`%#D' is a static data member; it can only be "
903 "initialized at its definition",
904 field);
905 return 0;
907 if (TREE_CODE (field) != FIELD_DECL)
909 error ("`%#D' is not a non-static data member of `%T'",
910 field, type);
911 return 0;
913 if (initializing_context (field) != type)
915 error ("class `%T' does not have any field named `%D'", type,
916 member_name);
917 return 0;
920 return 1;
923 /* NAME is a FIELD_DECL, an IDENTIFIER_NODE which names a field, or it
924 is a _TYPE node or TYPE_DECL which names a base for that type.
925 Check the validity of NAME, and return either the base _TYPE, base
926 binfo, or the FIELD_DECL of the member. If NAME is invalid, return
927 NULL_TREE and issue a diagnostic.
929 An old style unnamed direct single base construction is permitted,
930 where NAME is NULL. */
932 tree
933 expand_member_init (tree name)
935 tree basetype;
936 tree field;
938 if (!current_class_ref)
939 return NULL_TREE;
941 if (!name)
943 /* This is an obsolete unnamed base class initializer. The
944 parser will already have warned about its use. */
945 switch (CLASSTYPE_N_BASECLASSES (current_class_type))
947 case 0:
948 error ("unnamed initializer for `%T', which has no base classes",
949 current_class_type);
950 return NULL_TREE;
951 case 1:
952 basetype = TYPE_BINFO_BASETYPE (current_class_type, 0);
953 break;
954 default:
955 error ("unnamed initializer for `%T', which uses multiple inheritance",
956 current_class_type);
957 return NULL_TREE;
960 else if (TYPE_P (name))
962 basetype = TYPE_MAIN_VARIANT (name);
963 name = TYPE_NAME (name);
965 else if (TREE_CODE (name) == TYPE_DECL)
966 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (name));
967 else
968 basetype = NULL_TREE;
970 if (basetype)
972 tree class_binfo;
973 tree direct_binfo;
974 tree virtual_binfo;
975 int i;
977 if (current_template_parms)
978 return basetype;
980 class_binfo = TYPE_BINFO (current_class_type);
981 direct_binfo = NULL_TREE;
982 virtual_binfo = NULL_TREE;
984 /* Look for a direct base. */
985 for (i = 0; i < BINFO_N_BASETYPES (class_binfo); ++i)
986 if (same_type_p (basetype,
987 TYPE_BINFO_BASETYPE (current_class_type, i)))
989 direct_binfo = BINFO_BASETYPE (class_binfo, i);
990 break;
992 /* Look for a virtual base -- unless the direct base is itself
993 virtual. */
994 if (!direct_binfo || !TREE_VIA_VIRTUAL (direct_binfo))
996 virtual_binfo
997 = purpose_member (basetype,
998 CLASSTYPE_VBASECLASSES (current_class_type));
999 if (virtual_binfo)
1000 virtual_binfo = TREE_VALUE (virtual_binfo);
1003 /* [class.base.init]
1005 If a mem-initializer-id is ambiguous because it designates
1006 both a direct non-virtual base class and an inherited virtual
1007 base class, the mem-initializer is ill-formed. */
1008 if (direct_binfo && virtual_binfo)
1010 error ("'%D' is both a direct base and an indirect virtual base",
1011 basetype);
1012 return NULL_TREE;
1015 if (!direct_binfo && !virtual_binfo)
1017 if (TYPE_USES_VIRTUAL_BASECLASSES (current_class_type))
1018 error ("type `%T' is not a direct or virtual base of `%T'",
1019 basetype, current_class_type);
1020 else
1021 error ("type `%T' is not a direct base of `%T'",
1022 basetype, current_class_type);
1023 return NULL_TREE;
1026 return direct_binfo ? direct_binfo : virtual_binfo;
1028 else
1030 if (TREE_CODE (name) == IDENTIFIER_NODE)
1031 field = lookup_field (current_class_type, name, 1, false);
1032 else
1033 field = name;
1035 if (member_init_ok_or_else (field, current_class_type, name))
1036 return field;
1039 return NULL_TREE;
1042 /* This is like `expand_member_init', only it stores one aggregate
1043 value into another.
1045 INIT comes in two flavors: it is either a value which
1046 is to be stored in EXP, or it is a parameter list
1047 to go to a constructor, which will operate on EXP.
1048 If INIT is not a parameter list for a constructor, then set
1049 LOOKUP_ONLYCONVERTING.
1050 If FLAGS is LOOKUP_ONLYCONVERTING then it is the = init form of
1051 the initializer, if FLAGS is 0, then it is the (init) form.
1052 If `init' is a CONSTRUCTOR, then we emit a warning message,
1053 explaining that such initializations are invalid.
1055 If INIT resolves to a CALL_EXPR which happens to return
1056 something of the type we are looking for, then we know
1057 that we can safely use that call to perform the
1058 initialization.
1060 The virtual function table pointer cannot be set up here, because
1061 we do not really know its type.
1063 This never calls operator=().
1065 When initializing, nothing is CONST.
1067 A default copy constructor may have to be used to perform the
1068 initialization.
1070 A constructor or a conversion operator may have to be used to
1071 perform the initialization, but not both, as it would be ambiguous. */
1073 tree
1074 build_aggr_init (tree exp, tree init, int flags)
1076 tree stmt_expr;
1077 tree compound_stmt;
1078 int destroy_temps;
1079 tree type = TREE_TYPE (exp);
1080 int was_const = TREE_READONLY (exp);
1081 int was_volatile = TREE_THIS_VOLATILE (exp);
1082 int is_global;
1084 if (init == error_mark_node)
1085 return error_mark_node;
1087 TREE_READONLY (exp) = 0;
1088 TREE_THIS_VOLATILE (exp) = 0;
1090 if (init && TREE_CODE (init) != TREE_LIST)
1091 flags |= LOOKUP_ONLYCONVERTING;
1093 if (TREE_CODE (type) == ARRAY_TYPE)
1095 /* Must arrange to initialize each element of EXP
1096 from elements of INIT. */
1097 tree itype = init ? TREE_TYPE (init) : NULL_TREE;
1099 if (init && !itype)
1101 /* Handle bad initializers like:
1102 class COMPLEX {
1103 public:
1104 double re, im;
1105 COMPLEX(double r = 0.0, double i = 0.0) {re = r; im = i;};
1106 ~COMPLEX() {};
1109 int main(int argc, char **argv) {
1110 COMPLEX zees(1.0, 0.0)[10];
1113 error ("bad array initializer");
1114 return error_mark_node;
1116 if (cp_type_quals (type) != TYPE_UNQUALIFIED)
1117 TREE_TYPE (exp) = TYPE_MAIN_VARIANT (type);
1118 if (itype && cp_type_quals (itype) != TYPE_UNQUALIFIED)
1119 TREE_TYPE (init) = TYPE_MAIN_VARIANT (itype);
1120 stmt_expr = build_vec_init (exp, NULL_TREE, init,
1121 init && same_type_p (TREE_TYPE (init),
1122 TREE_TYPE (exp)));
1123 TREE_READONLY (exp) = was_const;
1124 TREE_THIS_VOLATILE (exp) = was_volatile;
1125 TREE_TYPE (exp) = type;
1126 if (init)
1127 TREE_TYPE (init) = itype;
1128 return stmt_expr;
1131 if (TREE_CODE (exp) == VAR_DECL || TREE_CODE (exp) == PARM_DECL)
1132 /* Just know that we've seen something for this node. */
1133 TREE_USED (exp) = 1;
1135 TREE_TYPE (exp) = TYPE_MAIN_VARIANT (type);
1136 is_global = begin_init_stmts (&stmt_expr, &compound_stmt);
1137 destroy_temps = stmts_are_full_exprs_p ();
1138 current_stmt_tree ()->stmts_are_full_exprs_p = 0;
1139 expand_aggr_init_1 (TYPE_BINFO (type), exp, exp,
1140 init, LOOKUP_NORMAL|flags);
1141 stmt_expr = finish_init_stmts (is_global, stmt_expr, compound_stmt);
1142 current_stmt_tree ()->stmts_are_full_exprs_p = destroy_temps;
1143 TREE_TYPE (exp) = type;
1144 TREE_READONLY (exp) = was_const;
1145 TREE_THIS_VOLATILE (exp) = was_volatile;
1147 return stmt_expr;
1150 /* Like build_aggr_init, but not just for aggregates. */
1152 tree
1153 build_init (tree decl, tree init, int flags)
1155 tree expr;
1157 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
1158 expr = build_aggr_init (decl, init, flags);
1159 else if (CLASS_TYPE_P (TREE_TYPE (decl)))
1160 expr = build_special_member_call (decl, complete_ctor_identifier,
1161 build_tree_list (NULL_TREE, init),
1162 TYPE_BINFO (TREE_TYPE (decl)),
1163 LOOKUP_NORMAL|flags);
1164 else
1165 expr = build (INIT_EXPR, TREE_TYPE (decl), decl, init);
1167 return expr;
1170 static void
1171 expand_default_init (tree binfo, tree true_exp, tree exp, tree init, int flags)
1173 tree type = TREE_TYPE (exp);
1174 tree ctor_name;
1176 /* It fails because there may not be a constructor which takes
1177 its own type as the first (or only parameter), but which does
1178 take other types via a conversion. So, if the thing initializing
1179 the expression is a unit element of type X, first try X(X&),
1180 followed by initialization by X. If neither of these work
1181 out, then look hard. */
1182 tree rval;
1183 tree parms;
1185 if (init && TREE_CODE (init) != TREE_LIST
1186 && (flags & LOOKUP_ONLYCONVERTING))
1188 /* Base subobjects should only get direct-initialization. */
1189 if (true_exp != exp)
1190 abort ();
1192 if (flags & DIRECT_BIND)
1193 /* Do nothing. We hit this in two cases: Reference initialization,
1194 where we aren't initializing a real variable, so we don't want
1195 to run a new constructor; and catching an exception, where we
1196 have already built up the constructor call so we could wrap it
1197 in an exception region. */;
1198 else if (TREE_CODE (init) == CONSTRUCTOR
1199 && TREE_HAS_CONSTRUCTOR (init))
1201 /* A brace-enclosed initializer for an aggregate. */
1202 my_friendly_assert (CP_AGGREGATE_TYPE_P (type), 20021016);
1203 init = digest_init (type, init, (tree *)NULL);
1205 else
1206 init = ocp_convert (type, init, CONV_IMPLICIT|CONV_FORCE_TEMP, flags);
1208 if (TREE_CODE (init) == MUST_NOT_THROW_EXPR)
1209 /* We need to protect the initialization of a catch parm with a
1210 call to terminate(), which shows up as a MUST_NOT_THROW_EXPR
1211 around the TARGET_EXPR for the copy constructor. See
1212 initialize_handler_parm. */
1214 TREE_OPERAND (init, 0) = build (INIT_EXPR, TREE_TYPE (exp), exp,
1215 TREE_OPERAND (init, 0));
1216 TREE_TYPE (init) = void_type_node;
1218 else
1219 init = build (INIT_EXPR, TREE_TYPE (exp), exp, init);
1220 TREE_SIDE_EFFECTS (init) = 1;
1221 finish_expr_stmt (init);
1222 return;
1225 if (init == NULL_TREE
1226 || (TREE_CODE (init) == TREE_LIST && ! TREE_TYPE (init)))
1228 parms = init;
1229 if (parms)
1230 init = TREE_VALUE (parms);
1232 else
1233 parms = build_tree_list (NULL_TREE, init);
1235 if (true_exp == exp)
1236 ctor_name = complete_ctor_identifier;
1237 else
1238 ctor_name = base_ctor_identifier;
1240 rval = build_special_member_call (exp, ctor_name, parms, binfo, flags);
1241 if (TREE_SIDE_EFFECTS (rval))
1242 finish_expr_stmt (convert_to_void (rval, NULL));
1245 /* This function is responsible for initializing EXP with INIT
1246 (if any).
1248 BINFO is the binfo of the type for who we are performing the
1249 initialization. For example, if W is a virtual base class of A and B,
1250 and C : A, B.
1251 If we are initializing B, then W must contain B's W vtable, whereas
1252 were we initializing C, W must contain C's W vtable.
1254 TRUE_EXP is nonzero if it is the true expression being initialized.
1255 In this case, it may be EXP, or may just contain EXP. The reason we
1256 need this is because if EXP is a base element of TRUE_EXP, we
1257 don't necessarily know by looking at EXP where its virtual
1258 baseclass fields should really be pointing. But we do know
1259 from TRUE_EXP. In constructors, we don't know anything about
1260 the value being initialized.
1262 FLAGS is just passed to `build_new_method_call'. See that function
1263 for its description. */
1265 static void
1266 expand_aggr_init_1 (tree binfo, tree true_exp, tree exp, tree init, int flags)
1268 tree type = TREE_TYPE (exp);
1270 my_friendly_assert (init != error_mark_node && type != error_mark_node, 211);
1271 my_friendly_assert (building_stmt_tree (), 20021010);
1273 /* Use a function returning the desired type to initialize EXP for us.
1274 If the function is a constructor, and its first argument is
1275 NULL_TREE, know that it was meant for us--just slide exp on
1276 in and expand the constructor. Constructors now come
1277 as TARGET_EXPRs. */
1279 if (init && TREE_CODE (exp) == VAR_DECL
1280 && TREE_CODE (init) == CONSTRUCTOR
1281 && TREE_HAS_CONSTRUCTOR (init))
1283 /* If store_init_value returns NULL_TREE, the INIT has been
1284 record in the DECL_INITIAL for EXP. That means there's
1285 nothing more we have to do. */
1286 init = store_init_value (exp, init);
1287 if (init)
1288 finish_expr_stmt (init);
1289 return;
1292 /* We know that expand_default_init can handle everything we want
1293 at this point. */
1294 expand_default_init (binfo, true_exp, exp, init, flags);
1297 /* Report an error if TYPE is not a user-defined, aggregate type. If
1298 OR_ELSE is nonzero, give an error message. */
1301 is_aggr_type (tree type, int or_else)
1303 if (type == error_mark_node)
1304 return 0;
1306 if (! IS_AGGR_TYPE (type)
1307 && TREE_CODE (type) != TEMPLATE_TYPE_PARM
1308 && TREE_CODE (type) != BOUND_TEMPLATE_TEMPLATE_PARM)
1310 if (or_else)
1311 error ("`%T' is not an aggregate type", type);
1312 return 0;
1314 return 1;
1317 /* Like is_aggr_typedef, but returns typedef if successful. */
1319 tree
1320 get_aggr_from_typedef (tree name, int or_else)
1322 tree type;
1324 if (name == error_mark_node)
1325 return NULL_TREE;
1327 if (IDENTIFIER_HAS_TYPE_VALUE (name))
1328 type = IDENTIFIER_TYPE_VALUE (name);
1329 else
1331 if (or_else)
1332 error ("`%T' fails to be an aggregate typedef", name);
1333 return NULL_TREE;
1336 if (! IS_AGGR_TYPE (type)
1337 && TREE_CODE (type) != TEMPLATE_TYPE_PARM
1338 && TREE_CODE (type) != BOUND_TEMPLATE_TEMPLATE_PARM)
1340 if (or_else)
1341 error ("type `%T' is of non-aggregate type", type);
1342 return NULL_TREE;
1344 return type;
1347 tree
1348 get_type_value (tree name)
1350 if (name == error_mark_node)
1351 return NULL_TREE;
1353 if (IDENTIFIER_HAS_TYPE_VALUE (name))
1354 return IDENTIFIER_TYPE_VALUE (name);
1355 else
1356 return NULL_TREE;
1359 /* Build a reference to a member of an aggregate. This is not a C++
1360 `&', but really something which can have its address taken, and
1361 then act as a pointer to member, for example TYPE :: FIELD can have
1362 its address taken by saying & TYPE :: FIELD. ADDRESS_P is true if
1363 this expression is the operand of "&".
1365 @@ Prints out lousy diagnostics for operator <typename>
1366 @@ fields.
1368 @@ This function should be rewritten and placed in search.c. */
1370 tree
1371 build_offset_ref (tree type, tree name, bool address_p)
1373 tree decl;
1374 tree member;
1375 tree basebinfo = NULL_TREE;
1376 tree orig_name = name;
1378 /* class templates can come in as TEMPLATE_DECLs here. */
1379 if (TREE_CODE (name) == TEMPLATE_DECL)
1380 return name;
1382 if (dependent_type_p (type) || type_dependent_expression_p (name))
1383 return build_min_nt (SCOPE_REF, type, name);
1385 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
1387 /* If the NAME is a TEMPLATE_ID_EXPR, we are looking at
1388 something like `a.template f<int>' or the like. For the most
1389 part, we treat this just like a.f. We do remember, however,
1390 the template-id that was used. */
1391 name = TREE_OPERAND (orig_name, 0);
1393 if (DECL_P (name))
1394 name = DECL_NAME (name);
1395 else
1397 if (TREE_CODE (name) == COMPONENT_REF)
1398 name = TREE_OPERAND (name, 1);
1399 if (TREE_CODE (name) == OVERLOAD)
1400 name = DECL_NAME (OVL_CURRENT (name));
1403 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 0);
1406 if (type == NULL_TREE)
1407 return error_mark_node;
1409 /* Handle namespace names fully here. */
1410 if (TREE_CODE (type) == NAMESPACE_DECL)
1412 tree t = lookup_namespace_name (type, name);
1413 if (t == error_mark_node)
1414 return t;
1415 if (TREE_CODE (orig_name) == TEMPLATE_ID_EXPR)
1416 /* Reconstruct the TEMPLATE_ID_EXPR. */
1417 t = build (TEMPLATE_ID_EXPR, TREE_TYPE (t),
1418 t, TREE_OPERAND (orig_name, 1));
1419 if (! type_unknown_p (t))
1421 mark_used (t);
1422 t = convert_from_reference (t);
1424 return t;
1427 if (! is_aggr_type (type, 1))
1428 return error_mark_node;
1430 if (TREE_CODE (name) == BIT_NOT_EXPR)
1432 if (! check_dtor_name (type, name))
1433 error ("qualified type `%T' does not match destructor name `~%T'",
1434 type, TREE_OPERAND (name, 0));
1435 name = dtor_identifier;
1438 if (!COMPLETE_TYPE_P (complete_type (type))
1439 && !TYPE_BEING_DEFINED (type))
1441 error ("incomplete type `%T' does not have member `%D'", type,
1442 name);
1443 return error_mark_node;
1446 /* Set up BASEBINFO for member lookup. */
1447 decl = maybe_dummy_object (type, &basebinfo);
1449 if (BASELINK_P (name) || DECL_P (name))
1450 member = name;
1451 else
1453 member = lookup_member (basebinfo, name, 1, 0);
1455 if (member == error_mark_node)
1456 return error_mark_node;
1459 if (!member)
1461 error ("`%D' is not a member of type `%T'", name, type);
1462 return error_mark_node;
1465 if (TREE_CODE (member) == TYPE_DECL)
1467 TREE_USED (member) = 1;
1468 return member;
1470 /* static class members and class-specific enum
1471 values can be returned without further ado. */
1472 if (TREE_CODE (member) == VAR_DECL || TREE_CODE (member) == CONST_DECL)
1474 mark_used (member);
1475 return convert_from_reference (member);
1478 if (TREE_CODE (member) == FIELD_DECL && DECL_C_BIT_FIELD (member))
1480 error ("invalid pointer to bit-field `%D'", member);
1481 return error_mark_node;
1484 /* A lot of this logic is now handled in lookup_member. */
1485 if (BASELINK_P (member))
1487 /* Go from the TREE_BASELINK to the member function info. */
1488 tree fnfields = member;
1489 tree t = BASELINK_FUNCTIONS (fnfields);
1491 if (TREE_CODE (orig_name) == TEMPLATE_ID_EXPR)
1493 /* The FNFIELDS are going to contain functions that aren't
1494 necessarily templates, and templates that don't
1495 necessarily match the explicit template parameters. We
1496 save all the functions, and the explicit parameters, and
1497 then figure out exactly what to instantiate with what
1498 arguments in instantiate_type. */
1500 if (TREE_CODE (t) != OVERLOAD)
1501 /* The code in instantiate_type which will process this
1502 expects to encounter OVERLOADs, not raw functions. */
1503 t = ovl_cons (t, NULL_TREE);
1505 t = build (TEMPLATE_ID_EXPR, TREE_TYPE (t), t,
1506 TREE_OPERAND (orig_name, 1));
1507 t = build (OFFSET_REF, unknown_type_node, decl, t);
1509 PTRMEM_OK_P (t) = 1;
1511 return t;
1514 if (TREE_CODE (t) != TEMPLATE_ID_EXPR && !really_overloaded_fn (t))
1516 /* Get rid of a potential OVERLOAD around it. */
1517 t = OVL_CURRENT (t);
1519 /* Unique functions are handled easily. */
1521 /* For non-static member of base class, we need a special rule
1522 for access checking [class.protected]:
1524 If the access is to form a pointer to member, the
1525 nested-name-specifier shall name the derived class
1526 (or any class derived from that class). */
1527 if (address_p && DECL_P (t)
1528 && DECL_NONSTATIC_MEMBER_P (t))
1529 perform_or_defer_access_check (TYPE_BINFO (type), t);
1530 else
1531 perform_or_defer_access_check (basebinfo, t);
1533 mark_used (t);
1534 if (DECL_STATIC_FUNCTION_P (t))
1535 return t;
1536 member = t;
1538 else
1540 TREE_TYPE (fnfields) = unknown_type_node;
1541 member = fnfields;
1544 else if (address_p && TREE_CODE (member) == FIELD_DECL)
1545 /* We need additional test besides the one in
1546 check_accessibility_of_qualified_id in case it is
1547 a pointer to non-static member. */
1548 perform_or_defer_access_check (TYPE_BINFO (type), member);
1550 if (!address_p)
1552 /* If MEMBER is non-static, then the program has fallen afoul of
1553 [expr.prim]:
1555 An id-expression that denotes a nonstatic data member or
1556 nonstatic member function of a class can only be used:
1558 -- as part of a class member access (_expr.ref_) in which the
1559 object-expression refers to the member's class or a class
1560 derived from that class, or
1562 -- to form a pointer to member (_expr.unary.op_), or
1564 -- in the body of a nonstatic member function of that class or
1565 of a class derived from that class (_class.mfct.nonstatic_), or
1567 -- in a mem-initializer for a constructor for that class or for
1568 a class derived from that class (_class.base.init_). */
1569 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (member))
1571 /* Build a representation of a the qualified name suitable
1572 for use as the operand to "&" -- even though the "&" is
1573 not actually present. */
1574 member = build (OFFSET_REF, TREE_TYPE (member), decl, member);
1575 /* In Microsoft mode, treat a non-static member function as if
1576 it were a pointer-to-member. */
1577 if (flag_ms_extensions)
1579 PTRMEM_OK_P (member) = 1;
1580 return build_unary_op (ADDR_EXPR, member, 0);
1582 error ("invalid use of non-static member function `%D'",
1583 TREE_OPERAND (member, 1));
1584 return member;
1586 else if (TREE_CODE (member) == FIELD_DECL)
1588 error ("invalid use of non-static data member `%D'", member);
1589 return error_mark_node;
1591 return member;
1594 /* In member functions, the form `type::name' is no longer
1595 equivalent to `this->type::name', at least not until
1596 resolve_offset_ref. */
1597 member = build (OFFSET_REF, TREE_TYPE (member), decl, member);
1598 PTRMEM_OK_P (member) = 1;
1599 return member;
1602 /* If DECL is a `const' declaration, and its value is a known
1603 constant, then return that value. */
1605 tree
1606 decl_constant_value (tree decl)
1608 /* When we build a COND_EXPR, we don't know whether it will be used
1609 as an lvalue or as an rvalue. If it is an lvalue, it's not safe
1610 to replace the second and third operands with their
1611 initializers. So, we do that here. */
1612 if (TREE_CODE (decl) == COND_EXPR)
1614 tree d1;
1615 tree d2;
1617 d1 = decl_constant_value (TREE_OPERAND (decl, 1));
1618 d2 = decl_constant_value (TREE_OPERAND (decl, 2));
1620 if (d1 != TREE_OPERAND (decl, 1) || d2 != TREE_OPERAND (decl, 2))
1621 return build (COND_EXPR,
1622 TREE_TYPE (decl),
1623 TREE_OPERAND (decl, 0), d1, d2);
1626 while (DECL_P (decl)
1627 && (/* Enumeration constants are constant. */
1628 TREE_CODE (decl) == CONST_DECL
1629 /* And so are variables with a 'const' type -- unless they
1630 are also 'volatile'. */
1631 || CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (decl)))
1632 && DECL_INITIAL (decl)
1633 && DECL_INITIAL (decl) != error_mark_node
1634 /* This is invalid if initial value is not constant. If it
1635 has either a function call, a memory reference, or a
1636 variable, then re-evaluating it could give different
1637 results. */
1638 && TREE_CONSTANT (DECL_INITIAL (decl))
1639 /* Check for cases where this is sub-optimal, even though
1640 valid. */
1641 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1642 decl = DECL_INITIAL (decl);
1643 return decl;
1646 /* Common subroutines of build_new and build_vec_delete. */
1648 /* Call the global __builtin_delete to delete ADDR. */
1650 static tree
1651 build_builtin_delete_call (tree addr)
1653 mark_used (global_delete_fndecl);
1654 return build_call (global_delete_fndecl, build_tree_list (NULL_TREE, addr));
1657 /* Generate a C++ "new" expression. DECL is either a TREE_LIST
1658 (which needs to go through some sort of groktypename) or it
1659 is the name of the class we are newing. INIT is an initialization value.
1660 It is either an EXPRLIST, an EXPR_NO_COMMAS, or something in braces.
1661 If INIT is void_type_node, it means do *not* call a constructor
1662 for this instance.
1664 For types with constructors, the data returned is initialized
1665 by the appropriate constructor.
1667 Whether the type has a constructor or not, if it has a pointer
1668 to a virtual function table, then that pointer is set up
1669 here.
1671 Unless I am mistaken, a call to new () will return initialized
1672 data regardless of whether the constructor itself is private or
1673 not. NOPE; new fails if the constructor is private (jcm).
1675 Note that build_new does nothing to assure that any special
1676 alignment requirements of the type are met. Rather, it leaves
1677 it up to malloc to do the right thing. Otherwise, folding to
1678 the right alignment cal cause problems if the user tries to later
1679 free the memory returned by `new'.
1681 PLACEMENT is the `placement' list for user-defined operator new (). */
1683 tree
1684 build_new (tree placement, tree decl, tree init, int use_global_new)
1686 tree type, rval;
1687 tree nelts = NULL_TREE, t;
1688 int has_array = 0;
1690 if (decl == error_mark_node)
1691 return error_mark_node;
1693 if (TREE_CODE (decl) == TREE_LIST)
1695 tree absdcl = TREE_VALUE (decl);
1696 tree last_absdcl = NULL_TREE;
1698 if (current_function_decl
1699 && DECL_CONSTRUCTOR_P (current_function_decl))
1700 my_friendly_assert (immediate_size_expand == 0, 19990926);
1702 nelts = integer_one_node;
1704 if (absdcl && TREE_CODE (absdcl) == CALL_EXPR)
1705 abort ();
1706 while (absdcl && TREE_CODE (absdcl) == INDIRECT_REF)
1708 last_absdcl = absdcl;
1709 absdcl = TREE_OPERAND (absdcl, 0);
1712 if (absdcl && TREE_CODE (absdcl) == ARRAY_REF)
1714 /* Probably meant to be a vec new. */
1715 tree this_nelts;
1717 while (TREE_OPERAND (absdcl, 0)
1718 && TREE_CODE (TREE_OPERAND (absdcl, 0)) == ARRAY_REF)
1720 last_absdcl = absdcl;
1721 absdcl = TREE_OPERAND (absdcl, 0);
1724 has_array = 1;
1725 this_nelts = TREE_OPERAND (absdcl, 1);
1726 if (this_nelts != error_mark_node)
1728 if (this_nelts == NULL_TREE)
1729 error ("new of array type fails to specify size");
1730 else if (processing_template_decl)
1732 nelts = this_nelts;
1733 absdcl = TREE_OPERAND (absdcl, 0);
1735 else
1737 if (build_expr_type_conversion (WANT_INT | WANT_ENUM,
1738 this_nelts, false)
1739 == NULL_TREE)
1740 pedwarn ("size in array new must have integral type");
1742 this_nelts = save_expr (cp_convert (sizetype, this_nelts));
1743 absdcl = TREE_OPERAND (absdcl, 0);
1744 if (this_nelts == integer_zero_node)
1746 warning ("zero size array reserves no space");
1747 nelts = integer_zero_node;
1749 else
1750 nelts = cp_build_binary_op (MULT_EXPR, nelts, this_nelts);
1753 else
1754 nelts = integer_zero_node;
1757 if (last_absdcl)
1758 TREE_OPERAND (last_absdcl, 0) = absdcl;
1759 else
1760 TREE_VALUE (decl) = absdcl;
1762 type = groktypename (decl);
1763 if (! type || type == error_mark_node)
1764 return error_mark_node;
1766 else if (TREE_CODE (decl) == IDENTIFIER_NODE)
1768 if (IDENTIFIER_HAS_TYPE_VALUE (decl))
1770 /* An aggregate type. */
1771 type = IDENTIFIER_TYPE_VALUE (decl);
1772 decl = TYPE_MAIN_DECL (type);
1774 else
1776 /* A builtin type. */
1777 decl = lookup_name (decl, 1);
1778 my_friendly_assert (TREE_CODE (decl) == TYPE_DECL, 215);
1779 type = TREE_TYPE (decl);
1782 else if (TREE_CODE (decl) == TYPE_DECL)
1784 type = TREE_TYPE (decl);
1786 else
1788 type = decl;
1789 decl = TYPE_MAIN_DECL (type);
1792 if (processing_template_decl)
1794 if (has_array)
1795 t = tree_cons (tree_cons (NULL_TREE, type, NULL_TREE),
1796 build_min_nt (ARRAY_REF, NULL_TREE, nelts),
1797 NULL_TREE);
1798 else
1799 t = type;
1801 rval = build_min (NEW_EXPR, build_pointer_type (type),
1802 placement, t, init);
1803 NEW_EXPR_USE_GLOBAL (rval) = use_global_new;
1804 TREE_SIDE_EFFECTS (rval) = 1;
1805 return rval;
1808 /* ``A reference cannot be created by the new operator. A reference
1809 is not an object (8.2.2, 8.4.3), so a pointer to it could not be
1810 returned by new.'' ARM 5.3.3 */
1811 if (TREE_CODE (type) == REFERENCE_TYPE)
1813 error ("new cannot be applied to a reference type");
1814 type = TREE_TYPE (type);
1817 if (TREE_CODE (type) == FUNCTION_TYPE)
1819 error ("new cannot be applied to a function type");
1820 return error_mark_node;
1823 /* When the object being created is an array, the new-expression yields a
1824 pointer to the initial element (if any) of the array. For example,
1825 both new int and new int[10] return an int*. 5.3.4. */
1826 if (TREE_CODE (type) == ARRAY_TYPE && has_array == 0)
1828 nelts = array_type_nelts_top (type);
1829 has_array = 1;
1830 type = TREE_TYPE (type);
1833 if (has_array)
1834 t = build_nt (ARRAY_REF, type, nelts);
1835 else
1836 t = type;
1838 rval = build (NEW_EXPR, build_pointer_type (type), placement, t, init);
1839 NEW_EXPR_USE_GLOBAL (rval) = use_global_new;
1840 TREE_SIDE_EFFECTS (rval) = 1;
1841 rval = build_new_1 (rval);
1842 if (rval == error_mark_node)
1843 return error_mark_node;
1845 /* Wrap it in a NOP_EXPR so warn_if_unused_value doesn't complain. */
1846 rval = build1 (NOP_EXPR, TREE_TYPE (rval), rval);
1847 TREE_NO_UNUSED_WARNING (rval) = 1;
1849 return rval;
1852 /* Given a Java class, return a decl for the corresponding java.lang.Class. */
1854 tree
1855 build_java_class_ref (tree type)
1857 tree name = NULL_TREE, class_decl;
1858 static tree CL_suffix = NULL_TREE;
1859 if (CL_suffix == NULL_TREE)
1860 CL_suffix = get_identifier("class$");
1861 if (jclass_node == NULL_TREE)
1863 jclass_node = IDENTIFIER_GLOBAL_VALUE (get_identifier ("jclass"));
1864 if (jclass_node == NULL_TREE)
1865 fatal_error ("call to Java constructor, while `jclass' undefined");
1867 jclass_node = TREE_TYPE (jclass_node);
1870 /* Mangle the class$ field. */
1872 tree field;
1873 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1874 if (DECL_NAME (field) == CL_suffix)
1876 mangle_decl (field);
1877 name = DECL_ASSEMBLER_NAME (field);
1878 break;
1880 if (!field)
1881 internal_error ("can't find class$");
1884 class_decl = IDENTIFIER_GLOBAL_VALUE (name);
1885 if (class_decl == NULL_TREE)
1887 class_decl = build_decl (VAR_DECL, name, TREE_TYPE (jclass_node));
1888 TREE_STATIC (class_decl) = 1;
1889 DECL_EXTERNAL (class_decl) = 1;
1890 TREE_PUBLIC (class_decl) = 1;
1891 DECL_ARTIFICIAL (class_decl) = 1;
1892 DECL_IGNORED_P (class_decl) = 1;
1893 pushdecl_top_level (class_decl);
1894 make_decl_rtl (class_decl, NULL);
1896 return class_decl;
1899 /* Returns the size of the cookie to use when allocating an array
1900 whose elements have the indicated TYPE. Assumes that it is already
1901 known that a cookie is needed. */
1903 static tree
1904 get_cookie_size (tree type)
1906 tree cookie_size;
1908 /* We need to allocate an additional max (sizeof (size_t), alignof
1909 (true_type)) bytes. */
1910 tree sizetype_size;
1911 tree type_align;
1913 sizetype_size = size_in_bytes (sizetype);
1914 type_align = size_int (TYPE_ALIGN_UNIT (type));
1915 if (INT_CST_LT_UNSIGNED (type_align, sizetype_size))
1916 cookie_size = sizetype_size;
1917 else
1918 cookie_size = type_align;
1920 return cookie_size;
1923 /* Called from cplus_expand_expr when expanding a NEW_EXPR. The return
1924 value is immediately handed to expand_expr. */
1926 static tree
1927 build_new_1 (tree exp)
1929 tree placement, init;
1930 tree true_type, size, rval;
1931 /* The type of the new-expression. (This type is always a pointer
1932 type.) */
1933 tree pointer_type;
1934 /* The type pointed to by POINTER_TYPE. */
1935 tree type;
1936 /* The type being allocated. For "new T[...]" this will be an
1937 ARRAY_TYPE. */
1938 tree full_type;
1939 /* A pointer type pointing to to the FULL_TYPE. */
1940 tree full_pointer_type;
1941 tree outer_nelts = NULL_TREE;
1942 tree nelts = NULL_TREE;
1943 tree alloc_call, alloc_expr;
1944 /* The address returned by the call to "operator new". This node is
1945 a VAR_DECL and is therefore reusable. */
1946 tree alloc_node;
1947 tree alloc_fn;
1948 tree cookie_expr, init_expr;
1949 int has_array = 0;
1950 enum tree_code code;
1951 int nothrow, check_new;
1952 /* Nonzero if the user wrote `::new' rather than just `new'. */
1953 int globally_qualified_p;
1954 int use_java_new = 0;
1955 /* If non-NULL, the number of extra bytes to allocate at the
1956 beginning of the storage allocated for an array-new expression in
1957 order to store the number of elements. */
1958 tree cookie_size = NULL_TREE;
1959 /* True if the function we are calling is a placement allocation
1960 function. */
1961 bool placement_allocation_fn_p;
1962 tree args = NULL_TREE;
1963 /* True if the storage must be initialized, either by a constructor
1964 or due to an explicit new-initializer. */
1965 bool is_initialized;
1966 /* The address of the thing allocated, not including any cookie. In
1967 particular, if an array cookie is in use, DATA_ADDR is the
1968 address of the first array element. This node is a VAR_DECL, and
1969 is therefore reusable. */
1970 tree data_addr;
1971 tree init_preeval_expr = NULL_TREE;
1973 placement = TREE_OPERAND (exp, 0);
1974 type = TREE_OPERAND (exp, 1);
1975 init = TREE_OPERAND (exp, 2);
1976 globally_qualified_p = NEW_EXPR_USE_GLOBAL (exp);
1978 if (TREE_CODE (type) == ARRAY_REF)
1980 has_array = 1;
1981 nelts = outer_nelts = TREE_OPERAND (type, 1);
1982 type = TREE_OPERAND (type, 0);
1984 /* Use an incomplete array type to avoid VLA headaches. */
1985 full_type = build_cplus_array_type (type, NULL_TREE);
1987 else
1988 full_type = type;
1990 true_type = type;
1992 code = has_array ? VEC_NEW_EXPR : NEW_EXPR;
1994 /* If our base type is an array, then make sure we know how many elements
1995 it has. */
1996 while (TREE_CODE (true_type) == ARRAY_TYPE)
1998 tree this_nelts = array_type_nelts_top (true_type);
1999 nelts = cp_build_binary_op (MULT_EXPR, nelts, this_nelts);
2000 true_type = TREE_TYPE (true_type);
2003 if (!complete_type_or_else (true_type, exp))
2004 return error_mark_node;
2006 if (TREE_CODE (true_type) == VOID_TYPE)
2008 error ("invalid type `void' for new");
2009 return error_mark_node;
2012 if (abstract_virtuals_error (NULL_TREE, true_type))
2013 return error_mark_node;
2015 is_initialized = (TYPE_NEEDS_CONSTRUCTING (type) || init);
2016 if (CP_TYPE_CONST_P (true_type) && !is_initialized)
2018 error ("uninitialized const in `new' of `%#T'", true_type);
2019 return error_mark_node;
2022 size = size_in_bytes (true_type);
2023 if (has_array)
2024 size = size_binop (MULT_EXPR, size, convert (sizetype, nelts));
2026 /* Allocate the object. */
2027 if (! placement && TYPE_FOR_JAVA (true_type))
2029 tree class_addr, alloc_decl;
2030 tree class_decl = build_java_class_ref (true_type);
2031 tree class_size = size_in_bytes (true_type);
2032 static const char alloc_name[] = "_Jv_AllocObject";
2033 use_java_new = 1;
2034 if (!get_global_value_if_present (get_identifier (alloc_name),
2035 &alloc_decl))
2037 error ("call to Java constructor with `%s' undefined", alloc_name);
2038 return error_mark_node;
2040 else if (really_overloaded_fn (alloc_decl))
2042 error ("`%D' should never be overloaded", alloc_decl);
2043 return error_mark_node;
2045 alloc_decl = OVL_CURRENT (alloc_decl);
2046 class_addr = build1 (ADDR_EXPR, jclass_node, class_decl);
2047 alloc_call = (build_function_call
2048 (alloc_decl,
2049 tree_cons (NULL_TREE, class_addr,
2050 build_tree_list (NULL_TREE, class_size))));
2052 else
2054 tree fnname;
2055 tree fns;
2057 fnname = ansi_opname (code);
2059 if (!globally_qualified_p
2060 && CLASS_TYPE_P (true_type)
2061 && (has_array
2062 ? TYPE_HAS_ARRAY_NEW_OPERATOR (true_type)
2063 : TYPE_HAS_NEW_OPERATOR (true_type)))
2065 /* Use a class-specific operator new. */
2066 /* If a cookie is required, add some extra space. */
2067 if (has_array && TYPE_VEC_NEW_USES_COOKIE (true_type))
2069 cookie_size = get_cookie_size (true_type);
2070 size = size_binop (PLUS_EXPR, size, cookie_size);
2072 /* Create the argument list. */
2073 args = tree_cons (NULL_TREE, size, placement);
2074 /* Do name-lookup to find the appropriate operator. */
2075 fns = lookup_fnfields (true_type, fnname, /*protect=*/2);
2076 if (!fns)
2078 error ("no suitable or ambiguous `%D' found in class `%T'",
2079 fnname, true_type);
2080 return error_mark_node;
2082 if (TREE_CODE (fns) == TREE_LIST)
2084 error ("request for member `%D' is ambiguous", fnname);
2085 print_candidates (fns);
2086 return error_mark_node;
2088 alloc_call = build_new_method_call (build_dummy_object (true_type),
2089 fns, args,
2090 /*conversion_path=*/NULL_TREE,
2091 LOOKUP_NORMAL);
2093 else
2095 /* Use a global operator new. */
2096 /* See if a cookie might be required. */
2097 if (has_array && TYPE_VEC_NEW_USES_COOKIE (true_type))
2098 cookie_size = get_cookie_size (true_type);
2099 else
2100 cookie_size = NULL_TREE;
2102 alloc_call = build_operator_new_call (fnname, placement,
2103 &size, &cookie_size);
2107 if (alloc_call == error_mark_node)
2108 return error_mark_node;
2110 /* In the simple case, we can stop now. */
2111 pointer_type = build_pointer_type (type);
2112 if (!cookie_size && !is_initialized)
2113 return build_nop (pointer_type, alloc_call);
2115 /* While we're working, use a pointer to the type we've actually
2116 allocated. Store the result of the call in a variable so that we
2117 can use it more than once. */
2118 full_pointer_type = build_pointer_type (full_type);
2119 alloc_expr = get_target_expr (build_nop (full_pointer_type, alloc_call));
2120 alloc_node = TARGET_EXPR_SLOT (alloc_expr);
2122 /* Strip any COMPOUND_EXPRs from ALLOC_CALL. */
2123 while (TREE_CODE (alloc_call) == COMPOUND_EXPR)
2124 alloc_call = TREE_OPERAND (alloc_call, 1);
2125 alloc_fn = get_callee_fndecl (alloc_call);
2126 my_friendly_assert (alloc_fn != NULL_TREE, 20020325);
2128 /* Now, check to see if this function is actually a placement
2129 allocation function. This can happen even when PLACEMENT is NULL
2130 because we might have something like:
2132 struct S { void* operator new (size_t, int i = 0); };
2134 A call to `new S' will get this allocation function, even though
2135 there is no explicit placement argument. If there is more than
2136 one argument, or there are variable arguments, then this is a
2137 placement allocation function. */
2138 placement_allocation_fn_p
2139 = (type_num_arguments (TREE_TYPE (alloc_fn)) > 1
2140 || varargs_function_p (alloc_fn));
2142 /* Preevaluate the placement args so that we don't reevaluate them for a
2143 placement delete. */
2144 if (placement_allocation_fn_p)
2146 tree inits;
2147 stabilize_call (alloc_call, &inits);
2148 if (inits)
2149 alloc_expr = build (COMPOUND_EXPR, TREE_TYPE (alloc_expr), inits,
2150 alloc_expr);
2153 /* unless an allocation function is declared with an empty excep-
2154 tion-specification (_except.spec_), throw(), it indicates failure to
2155 allocate storage by throwing a bad_alloc exception (clause _except_,
2156 _lib.bad.alloc_); it returns a non-null pointer otherwise If the allo-
2157 cation function is declared with an empty exception-specification,
2158 throw(), it returns null to indicate failure to allocate storage and a
2159 non-null pointer otherwise.
2161 So check for a null exception spec on the op new we just called. */
2163 nothrow = TYPE_NOTHROW_P (TREE_TYPE (alloc_fn));
2164 check_new = (flag_check_new || nothrow) && ! use_java_new;
2166 if (cookie_size)
2168 tree cookie;
2170 /* Adjust so we're pointing to the start of the object. */
2171 data_addr = get_target_expr (build (PLUS_EXPR, full_pointer_type,
2172 alloc_node, cookie_size));
2174 /* Store the number of bytes allocated so that we can know how
2175 many elements to destroy later. We use the last sizeof
2176 (size_t) bytes to store the number of elements. */
2177 cookie = build (MINUS_EXPR, build_pointer_type (sizetype),
2178 data_addr, size_in_bytes (sizetype));
2179 cookie = build_indirect_ref (cookie, NULL);
2181 cookie_expr = build (MODIFY_EXPR, sizetype, cookie, nelts);
2182 data_addr = TARGET_EXPR_SLOT (data_addr);
2184 else
2186 cookie_expr = NULL_TREE;
2187 data_addr = alloc_node;
2190 /* Now initialize the allocated object. Note that we preevaluate the
2191 initialization expression, apart from the actual constructor call or
2192 assignment--we do this because we want to delay the allocation as long
2193 as possible in order to minimize the size of the exception region for
2194 placement delete. */
2195 if (is_initialized)
2197 bool stable;
2199 init_expr = build_indirect_ref (data_addr, NULL);
2201 if (init == void_zero_node)
2202 init = build_default_init (full_type, nelts);
2203 else if (init && has_array)
2204 pedwarn ("ISO C++ forbids initialization in array new");
2206 if (has_array)
2208 init_expr
2209 = build_vec_init (init_expr,
2210 cp_build_binary_op (MINUS_EXPR, outer_nelts,
2211 integer_one_node),
2212 init, /*from_array=*/0);
2214 /* An array initialization is stable because the initialization
2215 of each element is a full-expression, so the temporaries don't
2216 leak out. */
2217 stable = true;
2219 else if (TYPE_NEEDS_CONSTRUCTING (type))
2221 init_expr = build_special_member_call (init_expr,
2222 complete_ctor_identifier,
2223 init, TYPE_BINFO (true_type),
2224 LOOKUP_NORMAL);
2225 stable = stabilize_init (init_expr, &init_preeval_expr);
2227 else
2229 /* We are processing something like `new int (10)', which
2230 means allocate an int, and initialize it with 10. */
2232 if (TREE_CODE (init) == TREE_LIST)
2233 init = build_x_compound_expr_from_list (init, "new initializer");
2235 else if (TREE_CODE (init) == CONSTRUCTOR
2236 && TREE_TYPE (init) == NULL_TREE)
2237 abort ();
2239 init_expr = build_modify_expr (init_expr, INIT_EXPR, init);
2240 stable = stabilize_init (init_expr, &init_preeval_expr);
2243 if (init_expr == error_mark_node)
2244 return error_mark_node;
2246 /* If any part of the object initialization terminates by throwing an
2247 exception and a suitable deallocation function can be found, the
2248 deallocation function is called to free the memory in which the
2249 object was being constructed, after which the exception continues
2250 to propagate in the context of the new-expression. If no
2251 unambiguous matching deallocation function can be found,
2252 propagating the exception does not cause the object's memory to be
2253 freed. */
2254 if (flag_exceptions && ! use_java_new)
2256 enum tree_code dcode = has_array ? VEC_DELETE_EXPR : DELETE_EXPR;
2257 tree cleanup;
2258 int flags = (LOOKUP_NORMAL
2259 | (globally_qualified_p * LOOKUP_GLOBAL));
2261 /* The Standard is unclear here, but the right thing to do
2262 is to use the same method for finding deallocation
2263 functions that we use for finding allocation functions. */
2264 flags |= LOOKUP_SPECULATIVELY;
2266 cleanup = build_op_delete_call (dcode, alloc_node, size, flags,
2267 (placement_allocation_fn_p
2268 ? alloc_call : NULL_TREE));
2270 if (!cleanup)
2271 /* We're done. */;
2272 else if (stable)
2273 /* This is much simpler if we were able to preevaluate all of
2274 the arguments to the constructor call. */
2275 init_expr = build (TRY_CATCH_EXPR, void_type_node,
2276 init_expr, cleanup);
2277 else
2278 /* Ack! First we allocate the memory. Then we set our sentry
2279 variable to true, and expand a cleanup that deletes the
2280 memory if sentry is true. Then we run the constructor, and
2281 finally clear the sentry.
2283 We need to do this because we allocate the space first, so
2284 if there are any temporaries with cleanups in the
2285 constructor args and we weren't able to preevaluate them, we
2286 need this EH region to extend until end of full-expression
2287 to preserve nesting. */
2289 tree end, sentry, begin;
2291 begin = get_target_expr (boolean_true_node);
2292 CLEANUP_EH_ONLY (begin) = 1;
2294 sentry = TARGET_EXPR_SLOT (begin);
2296 TARGET_EXPR_CLEANUP (begin)
2297 = build (COND_EXPR, void_type_node, sentry,
2298 cleanup, void_zero_node);
2300 end = build (MODIFY_EXPR, TREE_TYPE (sentry),
2301 sentry, boolean_false_node);
2303 init_expr
2304 = build (COMPOUND_EXPR, void_type_node, begin,
2305 build (COMPOUND_EXPR, void_type_node, init_expr,
2306 end));
2311 else
2312 init_expr = NULL_TREE;
2314 /* Now build up the return value in reverse order. */
2316 rval = data_addr;
2318 if (init_expr)
2319 rval = build (COMPOUND_EXPR, TREE_TYPE (rval), init_expr, rval);
2320 if (cookie_expr)
2321 rval = build (COMPOUND_EXPR, TREE_TYPE (rval), cookie_expr, rval);
2323 if (rval == alloc_node)
2324 /* If we don't have an initializer or a cookie, strip the TARGET_EXPR
2325 and return the call (which doesn't need to be adjusted). */
2326 rval = TARGET_EXPR_INITIAL (alloc_expr);
2327 else
2329 if (check_new)
2331 tree ifexp = cp_build_binary_op (NE_EXPR, alloc_node,
2332 integer_zero_node);
2333 rval = build_conditional_expr (ifexp, rval, alloc_node);
2336 /* Perform the allocation before anything else, so that ALLOC_NODE
2337 has been initialized before we start using it. */
2338 rval = build (COMPOUND_EXPR, TREE_TYPE (rval), alloc_expr, rval);
2341 if (init_preeval_expr)
2342 rval = build (COMPOUND_EXPR, TREE_TYPE (rval), init_preeval_expr, rval);
2344 /* Convert to the final type. */
2345 rval = build_nop (pointer_type, rval);
2347 /* A new-expression is never an lvalue. */
2348 if (real_lvalue_p (rval))
2349 rval = build1 (NON_LVALUE_EXPR, TREE_TYPE (rval), rval);
2351 return rval;
2354 static tree
2355 build_vec_delete_1 (tree base, tree maxindex, tree type,
2356 special_function_kind auto_delete_vec, int use_global_delete)
2358 tree virtual_size;
2359 tree ptype = build_pointer_type (type = complete_type (type));
2360 tree size_exp = size_in_bytes (type);
2362 /* Temporary variables used by the loop. */
2363 tree tbase, tbase_init;
2365 /* This is the body of the loop that implements the deletion of a
2366 single element, and moves temp variables to next elements. */
2367 tree body;
2369 /* This is the LOOP_EXPR that governs the deletion of the elements. */
2370 tree loop = 0;
2372 /* This is the thing that governs what to do after the loop has run. */
2373 tree deallocate_expr = 0;
2375 /* This is the BIND_EXPR which holds the outermost iterator of the
2376 loop. It is convenient to set this variable up and test it before
2377 executing any other code in the loop.
2378 This is also the containing expression returned by this function. */
2379 tree controller = NULL_TREE;
2381 /* We should only have 1-D arrays here. */
2382 if (TREE_CODE (type) == ARRAY_TYPE)
2383 abort ();
2385 if (! IS_AGGR_TYPE (type) || TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
2386 goto no_destructor;
2388 /* The below is short by the cookie size. */
2389 virtual_size = size_binop (MULT_EXPR, size_exp,
2390 convert (sizetype, maxindex));
2392 tbase = create_temporary_var (ptype);
2393 tbase_init = build_modify_expr (tbase, NOP_EXPR,
2394 fold (build (PLUS_EXPR, ptype,
2395 base,
2396 virtual_size)));
2397 DECL_REGISTER (tbase) = 1;
2398 controller = build (BIND_EXPR, void_type_node, tbase, NULL_TREE, NULL_TREE);
2399 TREE_SIDE_EFFECTS (controller) = 1;
2401 body = build (EXIT_EXPR, void_type_node,
2402 build (EQ_EXPR, boolean_type_node, base, tbase));
2403 body = build_compound_expr
2404 (body, build_modify_expr (tbase, NOP_EXPR,
2405 build (MINUS_EXPR, ptype, tbase, size_exp)));
2406 body = build_compound_expr
2407 (body, build_delete (ptype, tbase, sfk_complete_destructor,
2408 LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 1));
2410 loop = build (LOOP_EXPR, void_type_node, body);
2411 loop = build_compound_expr (tbase_init, loop);
2413 no_destructor:
2414 /* If the delete flag is one, or anything else with the low bit set,
2415 delete the storage. */
2416 if (auto_delete_vec != sfk_base_destructor)
2418 tree base_tbd;
2420 /* The below is short by the cookie size. */
2421 virtual_size = size_binop (MULT_EXPR, size_exp,
2422 convert (sizetype, maxindex));
2424 if (! TYPE_VEC_NEW_USES_COOKIE (type))
2425 /* no header */
2426 base_tbd = base;
2427 else
2429 tree cookie_size;
2431 cookie_size = get_cookie_size (type);
2432 base_tbd
2433 = cp_convert (ptype,
2434 cp_build_binary_op (MINUS_EXPR,
2435 cp_convert (string_type_node,
2436 base),
2437 cookie_size));
2438 /* True size with header. */
2439 virtual_size = size_binop (PLUS_EXPR, virtual_size, cookie_size);
2442 if (auto_delete_vec == sfk_deleting_destructor)
2443 deallocate_expr = build_x_delete (base_tbd,
2444 2 | use_global_delete,
2445 virtual_size);
2448 body = loop;
2449 if (!deallocate_expr)
2451 else if (!body)
2452 body = deallocate_expr;
2453 else
2454 body = build_compound_expr (body, deallocate_expr);
2456 if (!body)
2457 body = integer_zero_node;
2459 /* Outermost wrapper: If pointer is null, punt. */
2460 body = fold (build (COND_EXPR, void_type_node,
2461 fold (build (NE_EXPR, boolean_type_node, base,
2462 integer_zero_node)),
2463 body, integer_zero_node));
2464 body = build1 (NOP_EXPR, void_type_node, body);
2466 if (controller)
2468 TREE_OPERAND (controller, 1) = body;
2469 body = controller;
2472 if (TREE_CODE (base) == SAVE_EXPR)
2473 /* Pre-evaluate the SAVE_EXPR outside of the BIND_EXPR. */
2474 body = build (COMPOUND_EXPR, void_type_node, base, body);
2476 return convert_to_void (body, /*implicit=*/NULL);
2479 /* Create an unnamed variable of the indicated TYPE. */
2481 tree
2482 create_temporary_var (tree type)
2484 tree decl;
2486 decl = build_decl (VAR_DECL, NULL_TREE, type);
2487 TREE_USED (decl) = 1;
2488 DECL_ARTIFICIAL (decl) = 1;
2489 DECL_SOURCE_LOCATION (decl) = input_location;
2490 DECL_IGNORED_P (decl) = 1;
2491 DECL_CONTEXT (decl) = current_function_decl;
2493 return decl;
2496 /* Create a new temporary variable of the indicated TYPE, initialized
2497 to INIT.
2499 It is not entered into current_binding_level, because that breaks
2500 things when it comes time to do final cleanups (which take place
2501 "outside" the binding contour of the function). */
2503 static tree
2504 get_temp_regvar (tree type, tree init)
2506 tree decl;
2508 decl = create_temporary_var (type);
2509 add_decl_stmt (decl);
2511 finish_expr_stmt (build_modify_expr (decl, INIT_EXPR, init));
2513 return decl;
2516 /* `build_vec_init' returns tree structure that performs
2517 initialization of a vector of aggregate types.
2519 BASE is a reference to the vector, of ARRAY_TYPE.
2520 MAXINDEX is the maximum index of the array (one less than the
2521 number of elements). It is only used if
2522 TYPE_DOMAIN (TREE_TYPE (BASE)) == NULL_TREE.
2523 INIT is the (possibly NULL) initializer.
2525 FROM_ARRAY is 0 if we should init everything with INIT
2526 (i.e., every element initialized from INIT).
2527 FROM_ARRAY is 1 if we should index into INIT in parallel
2528 with initialization of DECL.
2529 FROM_ARRAY is 2 if we should index into INIT in parallel,
2530 but use assignment instead of initialization. */
2532 tree
2533 build_vec_init (tree base, tree maxindex, tree init, int from_array)
2535 tree rval;
2536 tree base2 = NULL_TREE;
2537 tree size;
2538 tree itype = NULL_TREE;
2539 tree iterator;
2540 /* The type of the array. */
2541 tree atype = TREE_TYPE (base);
2542 /* The type of an element in the array. */
2543 tree type = TREE_TYPE (atype);
2544 /* The element type reached after removing all outer array
2545 types. */
2546 tree inner_elt_type;
2547 /* The type of a pointer to an element in the array. */
2548 tree ptype;
2549 tree stmt_expr;
2550 tree compound_stmt;
2551 int destroy_temps;
2552 tree try_block = NULL_TREE;
2553 tree try_body = NULL_TREE;
2554 int num_initialized_elts = 0;
2555 bool is_global;
2557 if (TYPE_DOMAIN (atype))
2558 maxindex = array_type_nelts (atype);
2560 if (maxindex == NULL_TREE || maxindex == error_mark_node)
2561 return error_mark_node;
2563 inner_elt_type = strip_array_types (atype);
2564 if (init
2565 && (from_array == 2
2566 ? (!CLASS_TYPE_P (inner_elt_type)
2567 || !TYPE_HAS_COMPLEX_ASSIGN_REF (inner_elt_type))
2568 : !TYPE_NEEDS_CONSTRUCTING (type))
2569 && ((TREE_CODE (init) == CONSTRUCTOR
2570 /* Don't do this if the CONSTRUCTOR might contain something
2571 that might throw and require us to clean up. */
2572 && (CONSTRUCTOR_ELTS (init) == NULL_TREE
2573 || ! TYPE_HAS_NONTRIVIAL_DESTRUCTOR (inner_elt_type)))
2574 || from_array))
2576 /* Do non-default initialization of POD arrays resulting from
2577 brace-enclosed initializers. In this case, digest_init and
2578 store_constructor will handle the semantics for us. */
2580 stmt_expr = build (INIT_EXPR, atype, base, init);
2581 return stmt_expr;
2584 maxindex = cp_convert (ptrdiff_type_node, maxindex);
2585 ptype = build_pointer_type (type);
2586 size = size_in_bytes (type);
2587 if (TREE_CODE (TREE_TYPE (base)) == ARRAY_TYPE)
2588 base = cp_convert (ptype, decay_conversion (base));
2590 /* The code we are generating looks like:
2592 T* t1 = (T*) base;
2593 T* rval = t1;
2594 ptrdiff_t iterator = maxindex;
2595 try {
2596 for (; iterator != -1; --iterator) {
2597 ... initialize *t1 ...
2598 ++t1;
2600 } catch (...) {
2601 ... destroy elements that were constructed ...
2603 rval;
2606 We can omit the try and catch blocks if we know that the
2607 initialization will never throw an exception, or if the array
2608 elements do not have destructors. We can omit the loop completely if
2609 the elements of the array do not have constructors.
2611 We actually wrap the entire body of the above in a STMT_EXPR, for
2612 tidiness.
2614 When copying from array to another, when the array elements have
2615 only trivial copy constructors, we should use __builtin_memcpy
2616 rather than generating a loop. That way, we could take advantage
2617 of whatever cleverness the back-end has for dealing with copies
2618 of blocks of memory. */
2620 is_global = begin_init_stmts (&stmt_expr, &compound_stmt);
2621 destroy_temps = stmts_are_full_exprs_p ();
2622 current_stmt_tree ()->stmts_are_full_exprs_p = 0;
2623 rval = get_temp_regvar (ptype, base);
2624 base = get_temp_regvar (ptype, rval);
2625 iterator = get_temp_regvar (ptrdiff_type_node, maxindex);
2627 /* Protect the entire array initialization so that we can destroy
2628 the partially constructed array if an exception is thrown.
2629 But don't do this if we're assigning. */
2630 if (flag_exceptions && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
2631 && from_array != 2)
2633 try_block = begin_try_block ();
2634 try_body = begin_compound_stmt (/*has_no_scope=*/true);
2637 if (init != NULL_TREE && TREE_CODE (init) == CONSTRUCTOR)
2639 /* Do non-default initialization of non-POD arrays resulting from
2640 brace-enclosed initializers. */
2642 tree elts;
2643 from_array = 0;
2645 for (elts = CONSTRUCTOR_ELTS (init); elts; elts = TREE_CHAIN (elts))
2647 tree elt = TREE_VALUE (elts);
2648 tree baseref = build1 (INDIRECT_REF, type, base);
2650 num_initialized_elts++;
2652 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
2653 if (IS_AGGR_TYPE (type) || TREE_CODE (type) == ARRAY_TYPE)
2654 finish_expr_stmt (build_aggr_init (baseref, elt, 0));
2655 else
2656 finish_expr_stmt (build_modify_expr (baseref, NOP_EXPR,
2657 elt));
2658 current_stmt_tree ()->stmts_are_full_exprs_p = 0;
2660 finish_expr_stmt (build_unary_op (PREINCREMENT_EXPR, base, 0));
2661 finish_expr_stmt (build_unary_op (PREDECREMENT_EXPR, iterator, 0));
2664 /* Clear out INIT so that we don't get confused below. */
2665 init = NULL_TREE;
2667 else if (from_array)
2669 /* If initializing one array from another, initialize element by
2670 element. We rely upon the below calls the do argument
2671 checking. */
2672 if (init)
2674 base2 = decay_conversion (init);
2675 itype = TREE_TYPE (base2);
2676 base2 = get_temp_regvar (itype, base2);
2677 itype = TREE_TYPE (itype);
2679 else if (TYPE_LANG_SPECIFIC (type)
2680 && TYPE_NEEDS_CONSTRUCTING (type)
2681 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
2683 error ("initializer ends prematurely");
2684 return error_mark_node;
2688 /* Now, default-initialize any remaining elements. We don't need to
2689 do that if a) the type does not need constructing, or b) we've
2690 already initialized all the elements.
2692 We do need to keep going if we're copying an array. */
2694 if (from_array
2695 || (TYPE_NEEDS_CONSTRUCTING (type)
2696 && ! (host_integerp (maxindex, 0)
2697 && (num_initialized_elts
2698 == tree_low_cst (maxindex, 0) + 1))))
2700 /* If the ITERATOR is equal to -1, then we don't have to loop;
2701 we've already initialized all the elements. */
2702 tree for_stmt;
2703 tree for_body;
2704 tree elt_init;
2706 for_stmt = begin_for_stmt ();
2707 finish_for_init_stmt (for_stmt);
2708 finish_for_cond (build (NE_EXPR, boolean_type_node,
2709 iterator, integer_minus_one_node),
2710 for_stmt);
2711 finish_for_expr (build_unary_op (PREDECREMENT_EXPR, iterator, 0),
2712 for_stmt);
2714 /* Otherwise, loop through the elements. */
2715 for_body = begin_compound_stmt (/*has_no_scope=*/true);
2717 if (from_array)
2719 tree to = build1 (INDIRECT_REF, type, base);
2720 tree from;
2722 if (base2)
2723 from = build1 (INDIRECT_REF, itype, base2);
2724 else
2725 from = NULL_TREE;
2727 if (from_array == 2)
2728 elt_init = build_modify_expr (to, NOP_EXPR, from);
2729 else if (TYPE_NEEDS_CONSTRUCTING (type))
2730 elt_init = build_aggr_init (to, from, 0);
2731 else if (from)
2732 elt_init = build_modify_expr (to, NOP_EXPR, from);
2733 else
2734 abort ();
2736 else if (TREE_CODE (type) == ARRAY_TYPE)
2738 if (init != 0)
2739 sorry
2740 ("cannot initialize multi-dimensional array with initializer");
2741 elt_init = build_vec_init (build1 (INDIRECT_REF, type, base),
2742 0, 0, 0);
2744 else
2745 elt_init = build_aggr_init (build1 (INDIRECT_REF, type, base),
2746 init, 0);
2748 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
2749 finish_expr_stmt (elt_init);
2750 current_stmt_tree ()->stmts_are_full_exprs_p = 0;
2752 finish_expr_stmt (build_unary_op (PREINCREMENT_EXPR, base, 0));
2753 if (base2)
2754 finish_expr_stmt (build_unary_op (PREINCREMENT_EXPR, base2, 0));
2756 finish_compound_stmt (for_body);
2757 finish_for_stmt (for_stmt);
2760 /* Make sure to cleanup any partially constructed elements. */
2761 if (flag_exceptions && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
2762 && from_array != 2)
2764 tree e;
2765 tree m = cp_build_binary_op (MINUS_EXPR, maxindex, iterator);
2767 /* Flatten multi-dimensional array since build_vec_delete only
2768 expects one-dimensional array. */
2769 if (TREE_CODE (type) == ARRAY_TYPE)
2771 m = cp_build_binary_op (MULT_EXPR, m,
2772 array_type_nelts_total (type));
2773 type = strip_array_types (type);
2776 finish_compound_stmt (try_body);
2777 finish_cleanup_try_block (try_block);
2778 e = build_vec_delete_1 (rval, m, type, sfk_base_destructor,
2779 /*use_global_delete=*/0);
2780 finish_cleanup (e, try_block);
2783 /* The value of the array initialization is the array itself, RVAL
2784 is a pointer to the first element. */
2785 finish_stmt_expr_expr (rval);
2787 stmt_expr = finish_init_stmts (is_global, stmt_expr, compound_stmt);
2789 /* Now convert make the result have the correct type. */
2790 atype = build_pointer_type (atype);
2791 stmt_expr = build1 (NOP_EXPR, atype, stmt_expr);
2792 stmt_expr = build_indirect_ref (stmt_expr, NULL);
2794 current_stmt_tree ()->stmts_are_full_exprs_p = destroy_temps;
2795 return stmt_expr;
2798 /* Free up storage of type TYPE, at address ADDR.
2800 TYPE is a POINTER_TYPE and can be ptr_type_node for no special type
2801 of pointer.
2803 VIRTUAL_SIZE is the amount of storage that was allocated, and is
2804 used as the second argument to operator delete. It can include
2805 things like padding and magic size cookies. It has virtual in it,
2806 because if you have a base pointer and you delete through a virtual
2807 destructor, it should be the size of the dynamic object, not the
2808 static object, see Free Store 12.5 ISO C++.
2810 This does not call any destructors. */
2812 tree
2813 build_x_delete (tree addr, int which_delete, tree virtual_size)
2815 int use_global_delete = which_delete & 1;
2816 int use_vec_delete = !!(which_delete & 2);
2817 enum tree_code code = use_vec_delete ? VEC_DELETE_EXPR : DELETE_EXPR;
2818 int flags = LOOKUP_NORMAL | (use_global_delete * LOOKUP_GLOBAL);
2820 return build_op_delete_call (code, addr, virtual_size, flags, NULL_TREE);
2823 /* Call the DTOR_KIND destructor for EXP. FLAGS are as for
2824 build_delete. */
2826 static tree
2827 build_dtor_call (tree exp, special_function_kind dtor_kind, int flags)
2829 tree name;
2830 tree fn;
2831 switch (dtor_kind)
2833 case sfk_complete_destructor:
2834 name = complete_dtor_identifier;
2835 break;
2837 case sfk_base_destructor:
2838 name = base_dtor_identifier;
2839 break;
2841 case sfk_deleting_destructor:
2842 name = deleting_dtor_identifier;
2843 break;
2845 default:
2846 abort ();
2849 exp = convert_from_reference (exp);
2850 fn = lookup_fnfields (TREE_TYPE (exp), name, /*protect=*/2);
2851 return build_new_method_call (exp, fn,
2852 /*args=*/NULL_TREE,
2853 /*conversion_path=*/NULL_TREE,
2854 flags);
2857 /* Generate a call to a destructor. TYPE is the type to cast ADDR to.
2858 ADDR is an expression which yields the store to be destroyed.
2859 AUTO_DELETE is the name of the destructor to call, i.e., either
2860 sfk_complete_destructor, sfk_base_destructor, or
2861 sfk_deleting_destructor.
2863 FLAGS is the logical disjunction of zero or more LOOKUP_
2864 flags. See cp-tree.h for more info. */
2866 tree
2867 build_delete (tree type, tree addr, special_function_kind auto_delete,
2868 int flags, int use_global_delete)
2870 tree expr;
2872 if (addr == error_mark_node)
2873 return error_mark_node;
2875 /* Can happen when CURRENT_EXCEPTION_OBJECT gets its type
2876 set to `error_mark_node' before it gets properly cleaned up. */
2877 if (type == error_mark_node)
2878 return error_mark_node;
2880 type = TYPE_MAIN_VARIANT (type);
2882 if (TREE_CODE (type) == POINTER_TYPE)
2884 bool complete_p = true;
2886 type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
2887 if (TREE_CODE (type) == ARRAY_TYPE)
2888 goto handle_array;
2890 /* We don't want to warn about delete of void*, only other
2891 incomplete types. Deleting other incomplete types
2892 invokes undefined behavior, but it is not ill-formed, so
2893 compile to something that would even do The Right Thing
2894 (TM) should the type have a trivial dtor and no delete
2895 operator. */
2896 if (!VOID_TYPE_P (type))
2898 complete_type (type);
2899 if (!COMPLETE_TYPE_P (type))
2901 warning ("possible problem detected in invocation of "
2902 "delete operator:");
2903 cxx_incomplete_type_diagnostic (addr, type, 1);
2904 inform ("neither the destructor nor the class-specific "
2905 "operator delete will be called, even if they are "
2906 "declared when the class is defined.");
2907 complete_p = false;
2910 if (VOID_TYPE_P (type) || !complete_p || !IS_AGGR_TYPE (type))
2911 /* Call the builtin operator delete. */
2912 return build_builtin_delete_call (addr);
2913 if (TREE_SIDE_EFFECTS (addr))
2914 addr = save_expr (addr);
2916 /* Throw away const and volatile on target type of addr. */
2917 addr = convert_force (build_pointer_type (type), addr, 0);
2919 else if (TREE_CODE (type) == ARRAY_TYPE)
2921 handle_array:
2923 if (TYPE_DOMAIN (type) == NULL_TREE)
2925 error ("unknown array size in delete");
2926 return error_mark_node;
2928 return build_vec_delete (addr, array_type_nelts (type),
2929 auto_delete, use_global_delete);
2931 else
2933 /* Don't check PROTECT here; leave that decision to the
2934 destructor. If the destructor is accessible, call it,
2935 else report error. */
2936 addr = build_unary_op (ADDR_EXPR, addr, 0);
2937 if (TREE_SIDE_EFFECTS (addr))
2938 addr = save_expr (addr);
2940 addr = convert_force (build_pointer_type (type), addr, 0);
2943 my_friendly_assert (IS_AGGR_TYPE (type), 220);
2945 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
2947 if (auto_delete != sfk_deleting_destructor)
2948 return void_zero_node;
2950 return build_op_delete_call
2951 (DELETE_EXPR, addr, cxx_sizeof_nowarn (type),
2952 LOOKUP_NORMAL | (use_global_delete * LOOKUP_GLOBAL),
2953 NULL_TREE);
2955 else
2957 tree do_delete = NULL_TREE;
2958 tree ifexp;
2960 my_friendly_assert (TYPE_HAS_DESTRUCTOR (type), 20011213);
2962 /* For `::delete x', we must not use the deleting destructor
2963 since then we would not be sure to get the global `operator
2964 delete'. */
2965 if (use_global_delete && auto_delete == sfk_deleting_destructor)
2967 /* We will use ADDR multiple times so we must save it. */
2968 addr = save_expr (addr);
2969 /* Delete the object. */
2970 do_delete = build_builtin_delete_call (addr);
2971 /* Otherwise, treat this like a complete object destructor
2972 call. */
2973 auto_delete = sfk_complete_destructor;
2975 /* If the destructor is non-virtual, there is no deleting
2976 variant. Instead, we must explicitly call the appropriate
2977 `operator delete' here. */
2978 else if (!DECL_VIRTUAL_P (CLASSTYPE_DESTRUCTORS (type))
2979 && auto_delete == sfk_deleting_destructor)
2981 /* We will use ADDR multiple times so we must save it. */
2982 addr = save_expr (addr);
2983 /* Build the call. */
2984 do_delete = build_op_delete_call (DELETE_EXPR,
2985 addr,
2986 cxx_sizeof_nowarn (type),
2987 LOOKUP_NORMAL,
2988 NULL_TREE);
2989 /* Call the complete object destructor. */
2990 auto_delete = sfk_complete_destructor;
2992 else if (auto_delete == sfk_deleting_destructor
2993 && TYPE_GETS_REG_DELETE (type))
2995 /* Make sure we have access to the member op delete, even though
2996 we'll actually be calling it from the destructor. */
2997 build_op_delete_call (DELETE_EXPR, addr, cxx_sizeof_nowarn (type),
2998 LOOKUP_NORMAL, NULL_TREE);
3001 expr = build_dtor_call (build_indirect_ref (addr, NULL),
3002 auto_delete, flags);
3003 if (do_delete)
3004 expr = build (COMPOUND_EXPR, void_type_node, expr, do_delete);
3006 if (flags & LOOKUP_DESTRUCTOR)
3007 /* Explicit destructor call; don't check for null pointer. */
3008 ifexp = integer_one_node;
3009 else
3010 /* Handle deleting a null pointer. */
3011 ifexp = fold (cp_build_binary_op (NE_EXPR, addr, integer_zero_node));
3013 if (ifexp != integer_one_node)
3014 expr = build (COND_EXPR, void_type_node,
3015 ifexp, expr, void_zero_node);
3017 return expr;
3021 /* At the beginning of a destructor, push cleanups that will call the
3022 destructors for our base classes and members.
3024 Called from begin_destructor_body. */
3026 void
3027 push_base_cleanups (void)
3029 tree binfos;
3030 int i, n_baseclasses;
3031 tree member;
3032 tree expr;
3034 /* Run destructors for all virtual baseclasses. */
3035 if (TYPE_USES_VIRTUAL_BASECLASSES (current_class_type))
3037 tree vbases;
3038 tree cond = (condition_conversion
3039 (build (BIT_AND_EXPR, integer_type_node,
3040 current_in_charge_parm,
3041 integer_two_node)));
3043 vbases = CLASSTYPE_VBASECLASSES (current_class_type);
3044 /* The CLASSTYPE_VBASECLASSES list is in initialization
3045 order, which is also the right order for pushing cleanups. */
3046 for (; vbases;
3047 vbases = TREE_CHAIN (vbases))
3049 tree vbase = TREE_VALUE (vbases);
3050 tree base_type = BINFO_TYPE (vbase);
3052 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (base_type))
3054 expr = build_special_member_call (current_class_ref,
3055 base_dtor_identifier,
3056 NULL_TREE,
3057 vbase,
3058 (LOOKUP_NORMAL
3059 | LOOKUP_NONVIRTUAL));
3060 expr = build (COND_EXPR, void_type_node, cond,
3061 expr, void_zero_node);
3062 finish_decl_cleanup (NULL_TREE, expr);
3067 binfos = BINFO_BASETYPES (TYPE_BINFO (current_class_type));
3068 n_baseclasses = CLASSTYPE_N_BASECLASSES (current_class_type);
3070 /* Take care of the remaining baseclasses. */
3071 for (i = 0; i < n_baseclasses; i++)
3073 tree base_binfo = TREE_VEC_ELT (binfos, i);
3074 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (BINFO_TYPE (base_binfo))
3075 || TREE_VIA_VIRTUAL (base_binfo))
3076 continue;
3078 expr = build_special_member_call (current_class_ref,
3079 base_dtor_identifier,
3080 NULL_TREE, base_binfo,
3081 LOOKUP_NORMAL | LOOKUP_NONVIRTUAL);
3082 finish_decl_cleanup (NULL_TREE, expr);
3085 for (member = TYPE_FIELDS (current_class_type); member;
3086 member = TREE_CHAIN (member))
3088 if (TREE_CODE (member) != FIELD_DECL || DECL_ARTIFICIAL (member))
3089 continue;
3090 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (member)))
3092 tree this_member = (build_class_member_access_expr
3093 (current_class_ref, member,
3094 /*access_path=*/NULL_TREE,
3095 /*preserve_reference=*/false));
3096 tree this_type = TREE_TYPE (member);
3097 expr = build_delete (this_type, this_member,
3098 sfk_complete_destructor,
3099 LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR|LOOKUP_NORMAL,
3101 finish_decl_cleanup (NULL_TREE, expr);
3106 /* For type TYPE, delete the virtual baseclass objects of DECL. */
3108 tree
3109 build_vbase_delete (tree type, tree decl)
3111 tree vbases = CLASSTYPE_VBASECLASSES (type);
3112 tree result;
3113 tree addr = build_unary_op (ADDR_EXPR, decl, 0);
3115 my_friendly_assert (addr != error_mark_node, 222);
3117 for (result = convert_to_void (integer_zero_node, NULL);
3118 vbases; vbases = TREE_CHAIN (vbases))
3120 tree base_addr = convert_force
3121 (build_pointer_type (BINFO_TYPE (TREE_VALUE (vbases))), addr, 0);
3122 tree base_delete = build_delete
3123 (TREE_TYPE (base_addr), base_addr, sfk_base_destructor,
3124 LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 0);
3126 result = build_compound_expr (result, base_delete);
3128 return result;
3131 /* Build a C++ vector delete expression.
3132 MAXINDEX is the number of elements to be deleted.
3133 ELT_SIZE is the nominal size of each element in the vector.
3134 BASE is the expression that should yield the store to be deleted.
3135 This function expands (or synthesizes) these calls itself.
3136 AUTO_DELETE_VEC says whether the container (vector) should be deallocated.
3138 This also calls delete for virtual baseclasses of elements of the vector.
3140 Update: MAXINDEX is no longer needed. The size can be extracted from the
3141 start of the vector for pointers, and from the type for arrays. We still
3142 use MAXINDEX for arrays because it happens to already have one of the
3143 values we'd have to extract. (We could use MAXINDEX with pointers to
3144 confirm the size, and trap if the numbers differ; not clear that it'd
3145 be worth bothering.) */
3147 tree
3148 build_vec_delete (tree base, tree maxindex,
3149 special_function_kind auto_delete_vec, int use_global_delete)
3151 tree type;
3152 tree rval;
3153 tree base_init = NULL_TREE;
3155 type = TREE_TYPE (base);
3157 if (TREE_CODE (type) == POINTER_TYPE)
3159 /* Step back one from start of vector, and read dimension. */
3160 tree cookie_addr;
3162 if (TREE_SIDE_EFFECTS (base))
3164 base_init = get_target_expr (base);
3165 base = TARGET_EXPR_SLOT (base_init);
3167 type = strip_array_types (TREE_TYPE (type));
3168 cookie_addr = build (MINUS_EXPR,
3169 build_pointer_type (sizetype),
3170 base,
3171 TYPE_SIZE_UNIT (sizetype));
3172 maxindex = build_indirect_ref (cookie_addr, NULL);
3174 else if (TREE_CODE (type) == ARRAY_TYPE)
3176 /* Get the total number of things in the array, maxindex is a
3177 bad name. */
3178 maxindex = array_type_nelts_total (type);
3179 type = strip_array_types (type);
3180 base = build_unary_op (ADDR_EXPR, base, 1);
3181 if (TREE_SIDE_EFFECTS (base))
3183 base_init = get_target_expr (base);
3184 base = TARGET_EXPR_SLOT (base_init);
3187 else
3189 if (base != error_mark_node)
3190 error ("type to vector delete is neither pointer or array type");
3191 return error_mark_node;
3194 rval = build_vec_delete_1 (base, maxindex, type, auto_delete_vec,
3195 use_global_delete);
3196 if (base_init)
3197 rval = build (COMPOUND_EXPR, TREE_TYPE (rval), base_init, rval);
3199 return rval;