1 /* Handle initialization things in C++.
2 Copyright (C) 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003 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)
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. */
27 #include "coretypes.h"
38 static void construct_virtual_base (tree, tree);
39 static void expand_aggr_init_1 (tree, tree, tree, tree, int);
40 static void expand_default_init (tree, tree, tree, tree, int);
41 static tree build_vec_delete_1 (tree, tree, tree, special_function_kind, int);
42 static void perform_member_init (tree, tree);
43 static tree build_builtin_delete_call (tree);
44 static int member_init_ok_or_else (tree, tree, tree);
45 static void expand_virtual_init (tree, tree);
46 static tree sort_mem_initializers (tree, tree);
47 static tree initializing_context (tree);
48 static void expand_cleanup_for_base (tree, tree);
49 static tree get_temp_regvar (tree, tree);
50 static tree dfs_initialize_vtbl_ptrs (tree, void *);
51 static tree build_default_init (tree, tree);
52 static tree build_new_1 (tree);
53 static tree get_cookie_size (tree);
54 static tree build_dtor_call (tree, special_function_kind, int);
55 static tree build_field_list (tree, tree, int *);
56 static tree build_vtbl_address (tree);
58 /* We are about to generate some complex initialization code.
59 Conceptually, it is all a single expression. However, we may want
60 to include conditionals, loops, and other such statement-level
61 constructs. Therefore, we build the initialization code inside a
62 statement-expression. This function starts such an expression.
63 STMT_EXPR_P and COMPOUND_STMT_P are filled in by this function;
64 pass them back to finish_init_stmts when the expression is
68 begin_init_stmts (tree *stmt_expr_p, tree *compound_stmt_p)
70 if (building_stmt_tree ())
71 *stmt_expr_p = begin_stmt_expr ();
73 *stmt_expr_p = begin_global_stmt_expr ();
75 if (building_stmt_tree ())
76 *compound_stmt_p = begin_compound_stmt (/*has_no_scope=*/1);
79 /* Finish out the statement-expression begun by the previous call to
80 begin_init_stmts. Returns the statement-expression itself. */
83 finish_init_stmts (tree stmt_expr, tree compound_stmt)
85 if (building_stmt_tree ())
86 finish_compound_stmt (/*has_no_scope=*/1, compound_stmt);
88 if (building_stmt_tree ())
90 stmt_expr = finish_stmt_expr (stmt_expr);
91 STMT_EXPR_NO_SCOPE (stmt_expr) = true;
94 stmt_expr = finish_global_stmt_expr (stmt_expr);
96 /* To avoid spurious warnings about unused values, we set
99 TREE_USED (stmt_expr) = 1;
106 /* Called from initialize_vtbl_ptrs via dfs_walk. BINFO is the base
107 which we want to initialize the vtable pointer for, DATA is
108 TREE_LIST whose TREE_VALUE is the this ptr expression. */
111 dfs_initialize_vtbl_ptrs (tree binfo, void *data)
113 if ((!BINFO_PRIMARY_P (binfo) || TREE_VIA_VIRTUAL (binfo))
114 && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
116 tree base_ptr = TREE_VALUE ((tree) data);
118 base_ptr = build_base_path (PLUS_EXPR, base_ptr, binfo, /*nonnull=*/1);
120 expand_virtual_init (binfo, base_ptr);
123 BINFO_MARKED (binfo) = 1;
128 /* Initialize all the vtable pointers in the object pointed to by
132 initialize_vtbl_ptrs (tree addr)
137 type = TREE_TYPE (TREE_TYPE (addr));
138 list = build_tree_list (type, addr);
140 /* Walk through the hierarchy, initializing the vptr in each base
141 class. We do these in pre-order because we can't find the virtual
142 bases for a class until we've initialized the vtbl for that
144 dfs_walk_real (TYPE_BINFO (type), dfs_initialize_vtbl_ptrs,
145 NULL, unmarkedp, list);
146 dfs_walk (TYPE_BINFO (type), dfs_unmark, markedp, type);
149 /* Return an expression for the zero-initialization of an object with
150 type T. This expression will either be a constant (in the case
151 that T is a scalar), or a CONSTRUCTOR (in the case that T is an
152 aggregate). In either case, the value can be used as DECL_INITIAL
153 for a decl of the indicated TYPE; it is a valid static initializer.
154 If NELTS is non-NULL, and TYPE is an ARRAY_TYPE, NELTS is the
155 number of elements in the array. If STATIC_STORAGE_P is TRUE,
156 initializers are only generated for entities for which
157 zero-initialization does not simply mean filling the storage with
161 build_zero_init (tree type, tree nelts, bool static_storage_p)
163 tree init = NULL_TREE;
167 To zero-initialization storage for an object of type T means:
169 -- if T is a scalar type, the storage is set to the value of zero
172 -- if T is a non-union class type, the storage for each nonstatic
173 data member and each base-class subobject is zero-initialized.
175 -- if T is a union type, the storage for its first data member is
178 -- if T is an array type, the storage for each element is
181 -- if T is a reference type, no initialization is performed. */
183 my_friendly_assert (nelts == NULL_TREE || TREE_CODE (nelts) == INTEGER_CST,
186 if (type == error_mark_node)
188 else if (static_storage_p && zero_init_p (type))
189 /* In order to save space, we do not explicitly build initializers
190 for items that do not need them. GCC's semantics are that
191 items with static storage duration that are not otherwise
192 initialized are initialized to zero. */
194 else if (SCALAR_TYPE_P (type))
195 init = convert (type, integer_zero_node);
196 else if (CLASS_TYPE_P (type))
201 /* Build a constructor to contain the initializations. */
202 init = build_constructor (type, NULL_TREE);
203 /* Iterate over the fields, building initializations. */
205 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
207 if (TREE_CODE (field) != FIELD_DECL)
210 /* Note that for class types there will be FIELD_DECLs
211 corresponding to base classes as well. Thus, iterating
212 over TYPE_FIELDs will result in correct initialization of
213 all of the subobjects. */
214 if (static_storage_p && !zero_init_p (TREE_TYPE (field)))
215 inits = tree_cons (field,
216 build_zero_init (TREE_TYPE (field),
221 /* For unions, only the first field is initialized. */
222 if (TREE_CODE (type) == UNION_TYPE)
225 CONSTRUCTOR_ELTS (init) = nreverse (inits);
227 else if (TREE_CODE (type) == ARRAY_TYPE)
233 /* Build a constructor to contain the initializations. */
234 init = build_constructor (type, NULL_TREE);
235 /* Iterate over the array elements, building initializations. */
237 max_index = nelts ? nelts : array_type_nelts (type);
238 my_friendly_assert (TREE_CODE (max_index) == INTEGER_CST, 20030618);
240 for (index = size_zero_node;
241 !tree_int_cst_lt (max_index, index);
242 index = size_binop (PLUS_EXPR, index, size_one_node))
243 inits = tree_cons (index,
244 build_zero_init (TREE_TYPE (type),
248 CONSTRUCTOR_ELTS (init) = nreverse (inits);
250 else if (TREE_CODE (type) == REFERENCE_TYPE)
255 /* In all cases, the initializer is a constant. */
257 TREE_CONSTANT (init) = 1;
262 /* Build an expression for the default-initialization of an object of
263 the indicated TYPE. If NELTS is non-NULL, and TYPE is an
264 ARRAY_TYPE, NELTS is the number of elements in the array. If
265 initialization of TYPE requires calling constructors, this function
266 returns NULL_TREE; the caller is responsible for arranging for the
267 constructors to be called. */
270 build_default_init (tree type, tree nelts)
274 To default-initialize an object of type T means:
276 --if T is a non-POD class type (clause _class_), the default construc-
277 tor for T is called (and the initialization is ill-formed if T has
278 no accessible default constructor);
280 --if T is an array type, each element is default-initialized;
282 --otherwise, the storage for the object is zero-initialized.
284 A program that calls for default-initialization of an entity of refer-
285 ence type is ill-formed. */
287 /* If TYPE_NEEDS_CONSTRUCTING is true, the caller is responsible for
288 performing the initialization. This is confusing in that some
289 non-PODs do not have TYPE_NEEDS_CONSTRUCTING set. (For example,
290 a class with a pointer-to-data member as a non-static data member
291 does not have TYPE_NEEDS_CONSTRUCTING set.) Therefore, we end up
292 passing non-PODs to build_zero_init below, which is contrary to
293 the semantics quoted above from [dcl.init].
295 It happens, however, that the behavior of the constructor the
296 standard says we should have generated would be precisely the
297 same as that obtained by calling build_zero_init below, so things
299 if (TYPE_NEEDS_CONSTRUCTING (type)
300 || (nelts && TREE_CODE (nelts) != INTEGER_CST))
303 /* At this point, TYPE is either a POD class type, an array of POD
304 classes, or something even more inoccuous. */
305 return build_zero_init (type, nelts, /*static_storage_p=*/false);
308 /* Initialize MEMBER, a FIELD_DECL, with INIT, a TREE_LIST of
309 arguments. If TREE_LIST is void_type_node, an empty initializer
310 list was given; if NULL_TREE no initializer was given. */
313 perform_member_init (tree member, tree init)
316 tree type = TREE_TYPE (member);
319 explicit = (init != NULL_TREE);
321 /* Effective C++ rule 12 requires that all data members be
323 if (warn_ecpp && !explicit && TREE_CODE (type) != ARRAY_TYPE)
324 warning ("`%D' should be initialized in the member initialization "
328 if (init == void_type_node)
331 /* Get an lvalue for the data member. */
332 decl = build_class_member_access_expr (current_class_ref, member,
333 /*access_path=*/NULL_TREE,
334 /*preserve_reference=*/true);
335 if (decl == error_mark_node)
338 /* Deal with this here, as we will get confused if we try to call the
339 assignment op for an anonymous union. This can happen in a
340 synthesized copy constructor. */
341 if (ANON_AGGR_TYPE_P (type))
345 init = build (INIT_EXPR, type, decl, TREE_VALUE (init));
346 finish_expr_stmt (init);
349 else if (TYPE_NEEDS_CONSTRUCTING (type)
350 || (init && TYPE_HAS_CONSTRUCTOR (type)))
353 && TREE_CODE (type) == ARRAY_TYPE
355 && TREE_CHAIN (init) == NULL_TREE
356 && TREE_CODE (TREE_TYPE (TREE_VALUE (init))) == ARRAY_TYPE)
358 /* Initialization of one array from another. */
359 finish_expr_stmt (build_vec_init (decl, NULL_TREE, TREE_VALUE (init),
363 finish_expr_stmt (build_aggr_init (decl, init, 0));
367 if (init == NULL_TREE)
371 init = build_default_init (type, /*nelts=*/NULL_TREE);
372 if (TREE_CODE (type) == REFERENCE_TYPE)
374 ("default-initialization of `%#D', which has reference type",
377 /* member traversal: note it leaves init NULL */
378 else if (TREE_CODE (type) == REFERENCE_TYPE)
379 pedwarn ("uninitialized reference member `%D'", member);
381 else if (TREE_CODE (init) == TREE_LIST)
383 /* There was an explicit member initialization. Do some
384 work in that case. */
385 if (TREE_CHAIN (init))
387 warning ("initializer list treated as compound expression");
388 init = build_compound_expr (init);
391 init = TREE_VALUE (init);
395 finish_expr_stmt (build_modify_expr (decl, INIT_EXPR, init));
398 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
402 expr = build_class_member_access_expr (current_class_ref, member,
403 /*access_path=*/NULL_TREE,
404 /*preserve_reference=*/false);
405 expr = build_delete (type, expr, sfk_complete_destructor,
406 LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR, 0);
408 if (expr != error_mark_node)
409 finish_eh_cleanup (expr);
413 /* Returns a TREE_LIST containing (as the TREE_PURPOSE of each node) all
414 the FIELD_DECLs on the TYPE_FIELDS list for T, in reverse order. */
417 build_field_list (tree t, tree list, int *uses_unions_p)
423 /* Note whether or not T is a union. */
424 if (TREE_CODE (t) == UNION_TYPE)
427 for (fields = TYPE_FIELDS (t); fields; fields = TREE_CHAIN (fields))
429 /* Skip CONST_DECLs for enumeration constants and so forth. */
430 if (TREE_CODE (fields) != FIELD_DECL || DECL_ARTIFICIAL (fields))
433 /* Keep track of whether or not any fields are unions. */
434 if (TREE_CODE (TREE_TYPE (fields)) == UNION_TYPE)
437 /* For an anonymous struct or union, we must recursively
438 consider the fields of the anonymous type. They can be
439 directly initialized from the constructor. */
440 if (ANON_AGGR_TYPE_P (TREE_TYPE (fields)))
442 /* Add this field itself. Synthesized copy constructors
443 initialize the entire aggregate. */
444 list = tree_cons (fields, NULL_TREE, list);
445 /* And now add the fields in the anonymous aggregate. */
446 list = build_field_list (TREE_TYPE (fields), list,
449 /* Add this field. */
450 else if (DECL_NAME (fields))
451 list = tree_cons (fields, NULL_TREE, list);
457 /* The MEM_INITS are a TREE_LIST. The TREE_PURPOSE of each list gives
458 a FIELD_DECL or BINFO in T that needs initialization. The
459 TREE_VALUE gives the initializer, or list of initializer arguments.
461 Return a TREE_LIST containing all of the initializations required
462 for T, in the order in which they should be performed. The output
463 list has the same format as the input. */
466 sort_mem_initializers (tree t, tree mem_inits)
475 /* Build up a list of initializations. The TREE_PURPOSE of entry
476 will be the subobject (a FIELD_DECL or BINFO) to initialize. The
477 TREE_VALUE will be the constructor arguments, or NULL if no
478 explicit initialization was provided. */
479 sorted_inits = NULL_TREE;
480 /* Process the virtual bases. */
481 for (base = CLASSTYPE_VBASECLASSES (t); base; base = TREE_CHAIN (base))
482 sorted_inits = tree_cons (TREE_VALUE (base), NULL_TREE, sorted_inits);
483 /* Process the direct bases. */
484 for (i = 0; i < CLASSTYPE_N_BASECLASSES (t); ++i)
486 base = BINFO_BASETYPE (TYPE_BINFO (t), i);
487 if (!TREE_VIA_VIRTUAL (base))
488 sorted_inits = tree_cons (base, NULL_TREE, sorted_inits);
490 /* Process the non-static data members. */
491 sorted_inits = build_field_list (t, sorted_inits, &uses_unions_p);
492 /* Reverse the entire list of initializations, so that they are in
493 the order that they will actually be performed. */
494 sorted_inits = nreverse (sorted_inits);
496 /* If the user presented the initializers in an order different from
497 that in which they will actually occur, we issue a warning. Keep
498 track of the next subobject which can be explicitly initialized
499 without issuing a warning. */
500 next_subobject = sorted_inits;
502 /* Go through the explicit initializers, filling in TREE_PURPOSE in
504 for (init = mem_inits; init; init = TREE_CHAIN (init))
509 subobject = TREE_PURPOSE (init);
511 /* If the explicit initializers are in sorted order, then
512 SUBOBJECT will be NEXT_SUBOBJECT, or something following
514 for (subobject_init = next_subobject;
516 subobject_init = TREE_CHAIN (subobject_init))
517 if (TREE_PURPOSE (subobject_init) == subobject)
520 /* Issue a warning if the explicit initializer order does not
521 match that which will actually occur. */
522 if (warn_reorder && !subobject_init)
524 if (TREE_CODE (TREE_PURPOSE (next_subobject)) == FIELD_DECL)
525 cp_warning_at ("`%D' will be initialized after",
526 TREE_PURPOSE (next_subobject));
528 warning ("base `%T' will be initialized after",
529 TREE_PURPOSE (next_subobject));
530 if (TREE_CODE (subobject) == FIELD_DECL)
531 cp_warning_at (" `%#D'", subobject);
533 warning (" base `%T'", subobject);
536 /* Look again, from the beginning of the list. */
539 subobject_init = sorted_inits;
540 while (TREE_PURPOSE (subobject_init) != subobject)
541 subobject_init = TREE_CHAIN (subobject_init);
544 /* It is invalid to initialize the same subobject more than
546 if (TREE_VALUE (subobject_init))
548 if (TREE_CODE (subobject) == FIELD_DECL)
549 error ("multiple initializations given for `%D'", subobject);
551 error ("multiple initializations given for base `%T'",
555 /* Record the initialization. */
556 TREE_VALUE (subobject_init) = TREE_VALUE (init);
557 next_subobject = subobject_init;
562 If a ctor-initializer specifies more than one mem-initializer for
563 multiple members of the same union (including members of
564 anonymous unions), the ctor-initializer is ill-formed. */
567 tree last_field = NULL_TREE;
568 for (init = sorted_inits; init; init = TREE_CHAIN (init))
574 /* Skip uninitialized members and base classes. */
575 if (!TREE_VALUE (init)
576 || TREE_CODE (TREE_PURPOSE (init)) != FIELD_DECL)
578 /* See if this field is a member of a union, or a member of a
579 structure contained in a union, etc. */
580 field = TREE_PURPOSE (init);
581 for (field_type = DECL_CONTEXT (field);
582 !same_type_p (field_type, t);
583 field_type = TYPE_CONTEXT (field_type))
584 if (TREE_CODE (field_type) == UNION_TYPE)
586 /* If this field is not a member of a union, skip it. */
587 if (TREE_CODE (field_type) != UNION_TYPE)
590 /* It's only an error if we have two initializers for the same
598 /* See if LAST_FIELD and the field initialized by INIT are
599 members of the same union. If so, there's a problem,
600 unless they're actually members of the same structure
601 which is itself a member of a union. For example, given:
603 union { struct { int i; int j; }; };
605 initializing both `i' and `j' makes sense. */
606 field_type = DECL_CONTEXT (field);
610 tree last_field_type;
612 last_field_type = DECL_CONTEXT (last_field);
615 if (same_type_p (last_field_type, field_type))
617 if (TREE_CODE (field_type) == UNION_TYPE)
618 error ("initializations for multiple members of `%T'",
624 if (same_type_p (last_field_type, t))
627 last_field_type = TYPE_CONTEXT (last_field_type);
630 /* If we've reached the outermost class, then we're
632 if (same_type_p (field_type, t))
635 field_type = TYPE_CONTEXT (field_type);
646 /* Initialize all bases and members of CURRENT_CLASS_TYPE. MEM_INITS
647 is a TREE_LIST giving the explicit mem-initializer-list for the
648 constructor. The TREE_PURPOSE of each entry is a subobject (a
649 FIELD_DECL or a BINFO) of the CURRENT_CLASS_TYPE. The TREE_VALUE
650 is a TREE_LIST giving the arguments to the constructor or
651 void_type_node for an empty list of arguments. */
654 emit_mem_initializers (tree mem_inits)
656 /* Sort the mem-initializers into the order in which the
657 initializations should be performed. */
658 mem_inits = sort_mem_initializers (current_class_type, mem_inits);
660 in_base_initializer = 1;
662 /* Initialize base classes. */
664 && TREE_CODE (TREE_PURPOSE (mem_inits)) != FIELD_DECL)
666 tree subobject = TREE_PURPOSE (mem_inits);
667 tree arguments = TREE_VALUE (mem_inits);
669 /* If these initializations are taking place in a copy
670 constructor, the base class should probably be explicitly
672 if (extra_warnings && !arguments
673 && DECL_COPY_CONSTRUCTOR_P (current_function_decl)
674 && TYPE_NEEDS_CONSTRUCTING (BINFO_TYPE (subobject)))
675 warning ("base class `%#T' should be explicitly initialized in the "
677 BINFO_TYPE (subobject));
679 /* If an explicit -- but empty -- initializer list was present,
680 treat it just like default initialization at this point. */
681 if (arguments == void_type_node)
682 arguments = NULL_TREE;
684 /* Initialize the base. */
685 if (TREE_VIA_VIRTUAL (subobject))
686 construct_virtual_base (subobject, arguments);
691 base_addr = build_base_path (PLUS_EXPR, current_class_ptr,
693 expand_aggr_init_1 (subobject, NULL_TREE,
694 build_indirect_ref (base_addr, NULL),
697 expand_cleanup_for_base (subobject, NULL_TREE);
700 mem_inits = TREE_CHAIN (mem_inits);
702 in_base_initializer = 0;
704 /* Initialize the vptrs. */
705 initialize_vtbl_ptrs (current_class_ptr);
707 /* Initialize the data members. */
710 perform_member_init (TREE_PURPOSE (mem_inits),
711 TREE_VALUE (mem_inits));
712 mem_inits = TREE_CHAIN (mem_inits);
716 /* Returns the address of the vtable (i.e., the value that should be
717 assigned to the vptr) for BINFO. */
720 build_vtbl_address (tree binfo)
722 tree binfo_for = binfo;
725 if (BINFO_VPTR_INDEX (binfo) && TREE_VIA_VIRTUAL (binfo)
726 && BINFO_PRIMARY_P (binfo))
727 /* If this is a virtual primary base, then the vtable we want to store
728 is that for the base this is being used as the primary base of. We
729 can't simply skip the initialization, because we may be expanding the
730 inits of a subobject constructor where the virtual base layout
732 while (BINFO_PRIMARY_BASE_OF (binfo_for))
733 binfo_for = BINFO_PRIMARY_BASE_OF (binfo_for);
735 /* Figure out what vtable BINFO's vtable is based on, and mark it as
737 vtbl = get_vtbl_decl_for_binfo (binfo_for);
738 assemble_external (vtbl);
739 TREE_USED (vtbl) = 1;
741 /* Now compute the address to use when initializing the vptr. */
742 vtbl = BINFO_VTABLE (binfo_for);
743 if (TREE_CODE (vtbl) == VAR_DECL)
745 vtbl = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (vtbl)), vtbl);
746 TREE_CONSTANT (vtbl) = 1;
752 /* This code sets up the virtual function tables appropriate for
753 the pointer DECL. It is a one-ply initialization.
755 BINFO is the exact type that DECL is supposed to be. In
756 multiple inheritance, this might mean "C's A" if C : A, B. */
759 expand_virtual_init (tree binfo, tree decl)
764 /* Compute the initializer for vptr. */
765 vtbl = build_vtbl_address (binfo);
767 /* We may get this vptr from a VTT, if this is a subobject
768 constructor or subobject destructor. */
769 vtt_index = BINFO_VPTR_INDEX (binfo);
775 /* Compute the value to use, when there's a VTT. */
776 vtt_parm = current_vtt_parm;
777 vtbl2 = build (PLUS_EXPR,
778 TREE_TYPE (vtt_parm),
781 vtbl2 = build1 (INDIRECT_REF, TREE_TYPE (vtbl), vtbl2);
783 /* The actual initializer is the VTT value only in the subobject
784 constructor. In maybe_clone_body we'll substitute NULL for
785 the vtt_parm in the case of the non-subobject constructor. */
786 vtbl = build (COND_EXPR,
788 build (EQ_EXPR, boolean_type_node,
789 current_in_charge_parm, integer_zero_node),
794 /* Compute the location of the vtpr. */
795 vtbl_ptr = build_vfield_ref (build_indirect_ref (decl, NULL),
797 my_friendly_assert (vtbl_ptr != error_mark_node, 20010730);
799 /* Assign the vtable to the vptr. */
800 vtbl = convert_force (TREE_TYPE (vtbl_ptr), vtbl, 0);
801 finish_expr_stmt (build_modify_expr (vtbl_ptr, NOP_EXPR, vtbl));
804 /* If an exception is thrown in a constructor, those base classes already
805 constructed must be destroyed. This function creates the cleanup
806 for BINFO, which has just been constructed. If FLAG is non-NULL,
807 it is a DECL which is nonzero when this base needs to be
811 expand_cleanup_for_base (tree binfo, tree flag)
815 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (BINFO_TYPE (binfo)))
818 /* Call the destructor. */
819 expr = build_special_member_call (current_class_ref,
820 base_dtor_identifier,
823 LOOKUP_NORMAL | LOOKUP_NONVIRTUAL);
825 expr = fold (build (COND_EXPR, void_type_node,
826 c_common_truthvalue_conversion (flag),
827 expr, integer_zero_node));
829 finish_eh_cleanup (expr);
832 /* Construct the virtual base-class VBASE passing the ARGUMENTS to its
836 construct_virtual_base (tree vbase, tree arguments)
843 /* If there are virtual base classes with destructors, we need to
844 emit cleanups to destroy them if an exception is thrown during
845 the construction process. These exception regions (i.e., the
846 period during which the cleanups must occur) begin from the time
847 the construction is complete to the end of the function. If we
848 create a conditional block in which to initialize the
849 base-classes, then the cleanup region for the virtual base begins
850 inside a block, and ends outside of that block. This situation
851 confuses the sjlj exception-handling code. Therefore, we do not
852 create a single conditional block, but one for each
853 initialization. (That way the cleanup regions always begin
854 in the outer block.) We trust the back-end to figure out
855 that the FLAG will not change across initializations, and
856 avoid doing multiple tests. */
857 flag = TREE_CHAIN (DECL_ARGUMENTS (current_function_decl));
858 inner_if_stmt = begin_if_stmt ();
859 finish_if_stmt_cond (flag, inner_if_stmt);
860 compound_stmt = begin_compound_stmt (/*has_no_scope=*/1);
862 /* Compute the location of the virtual base. If we're
863 constructing virtual bases, then we must be the most derived
864 class. Therefore, we don't have to look up the virtual base;
865 we already know where it is. */
866 exp = build (PLUS_EXPR,
867 TREE_TYPE (current_class_ptr),
869 fold (build1 (NOP_EXPR, TREE_TYPE (current_class_ptr),
870 BINFO_OFFSET (vbase))));
871 exp = build1 (NOP_EXPR,
872 build_pointer_type (BINFO_TYPE (vbase)),
874 exp = build1 (INDIRECT_REF, BINFO_TYPE (vbase), exp);
876 expand_aggr_init_1 (vbase, current_class_ref, exp,
877 arguments, LOOKUP_COMPLAIN);
878 finish_compound_stmt (/*has_no_scope=*/1, compound_stmt);
879 finish_then_clause (inner_if_stmt);
882 expand_cleanup_for_base (vbase, flag);
885 /* Find the context in which this FIELD can be initialized. */
888 initializing_context (tree field)
890 tree t = DECL_CONTEXT (field);
892 /* Anonymous union members can be initialized in the first enclosing
893 non-anonymous union context. */
894 while (t && ANON_AGGR_TYPE_P (t))
895 t = TYPE_CONTEXT (t);
899 /* Function to give error message if member initialization specification
900 is erroneous. FIELD is the member we decided to initialize.
901 TYPE is the type for which the initialization is being performed.
902 FIELD must be a member of TYPE.
904 MEMBER_NAME is the name of the member. */
907 member_init_ok_or_else (tree field, tree type, tree member_name)
909 if (field == error_mark_node)
913 error ("class `%T' does not have any field named `%D'", type,
917 if (TREE_CODE (field) == VAR_DECL)
919 error ("`%#D' is a static data member; it can only be "
920 "initialized at its definition",
924 if (TREE_CODE (field) != FIELD_DECL)
926 error ("`%#D' is not a non-static data member of `%T'",
930 if (initializing_context (field) != type)
932 error ("class `%T' does not have any field named `%D'", type,
940 /* NAME is a FIELD_DECL, an IDENTIFIER_NODE which names a field, or it
941 is a _TYPE node or TYPE_DECL which names a base for that type.
942 Check the validity of NAME, and return either the base _TYPE, base
943 binfo, or the FIELD_DECL of the member. If NAME is invalid, return
944 NULL_TREE and issue a diagnostic.
946 An old style unnamed direct single base construction is permitted,
947 where NAME is NULL. */
950 expand_member_init (tree name)
955 if (!current_class_ref)
960 /* This is an obsolete unnamed base class initializer. The
961 parser will already have warned about its use. */
962 switch (CLASSTYPE_N_BASECLASSES (current_class_type))
965 error ("unnamed initializer for `%T', which has no base classes",
969 basetype = TYPE_BINFO_BASETYPE (current_class_type, 0);
972 error ("unnamed initializer for `%T', which uses multiple inheritance",
977 else if (TYPE_P (name))
979 basetype = TYPE_MAIN_VARIANT (name);
980 name = TYPE_NAME (name);
982 else if (TREE_CODE (name) == TYPE_DECL)
983 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (name));
985 basetype = NULL_TREE;
991 if (current_template_parms)
994 binfo = lookup_base (current_class_type, basetype,
996 if (!binfo || (!TREE_VIA_VIRTUAL (binfo)
997 && (BINFO_INHERITANCE_CHAIN (binfo)
998 != TYPE_BINFO (current_class_type))))
1000 if (TYPE_USES_VIRTUAL_BASECLASSES (current_class_type))
1001 error ("type `%D' is not a direct or virtual base of `%T'",
1002 name, current_class_type);
1004 error ("type `%D' is not a direct base of `%T'",
1005 name, current_class_type);
1012 if (TREE_CODE (name) == IDENTIFIER_NODE)
1013 field = lookup_field (current_class_type, name, 1, false);
1017 if (member_init_ok_or_else (field, current_class_type, name))
1024 /* This is like `expand_member_init', only it stores one aggregate
1027 INIT comes in two flavors: it is either a value which
1028 is to be stored in EXP, or it is a parameter list
1029 to go to a constructor, which will operate on EXP.
1030 If INIT is not a parameter list for a constructor, then set
1031 LOOKUP_ONLYCONVERTING.
1032 If FLAGS is LOOKUP_ONLYCONVERTING then it is the = init form of
1033 the initializer, if FLAGS is 0, then it is the (init) form.
1034 If `init' is a CONSTRUCTOR, then we emit a warning message,
1035 explaining that such initializations are invalid.
1037 If INIT resolves to a CALL_EXPR which happens to return
1038 something of the type we are looking for, then we know
1039 that we can safely use that call to perform the
1042 The virtual function table pointer cannot be set up here, because
1043 we do not really know its type.
1045 This never calls operator=().
1047 When initializing, nothing is CONST.
1049 A default copy constructor may have to be used to perform the
1052 A constructor or a conversion operator may have to be used to
1053 perform the initialization, but not both, as it would be ambiguous. */
1056 build_aggr_init (tree exp, tree init, int flags)
1061 tree type = TREE_TYPE (exp);
1062 int was_const = TREE_READONLY (exp);
1063 int was_volatile = TREE_THIS_VOLATILE (exp);
1065 if (init == error_mark_node)
1066 return error_mark_node;
1068 TREE_READONLY (exp) = 0;
1069 TREE_THIS_VOLATILE (exp) = 0;
1071 if (init && TREE_CODE (init) != TREE_LIST)
1072 flags |= LOOKUP_ONLYCONVERTING;
1074 if (TREE_CODE (type) == ARRAY_TYPE)
1076 /* Must arrange to initialize each element of EXP
1077 from elements of INIT. */
1078 tree itype = init ? TREE_TYPE (init) : NULL_TREE;
1082 /* Handle bad initializers like:
1086 COMPLEX(double r = 0.0, double i = 0.0) {re = r; im = i;};
1090 int main(int argc, char **argv) {
1091 COMPLEX zees(1.0, 0.0)[10];
1094 error ("bad array initializer");
1095 return error_mark_node;
1097 if (cp_type_quals (type) != TYPE_UNQUALIFIED)
1098 TREE_TYPE (exp) = TYPE_MAIN_VARIANT (type);
1099 if (itype && cp_type_quals (itype) != TYPE_UNQUALIFIED)
1100 TREE_TYPE (init) = TYPE_MAIN_VARIANT (itype);
1101 stmt_expr = build_vec_init (exp, NULL_TREE, init,
1102 init && same_type_p (TREE_TYPE (init),
1104 TREE_READONLY (exp) = was_const;
1105 TREE_THIS_VOLATILE (exp) = was_volatile;
1106 TREE_TYPE (exp) = type;
1108 TREE_TYPE (init) = itype;
1112 if (TREE_CODE (exp) == VAR_DECL || TREE_CODE (exp) == PARM_DECL)
1113 /* just know that we've seen something for this node */
1114 TREE_USED (exp) = 1;
1116 TREE_TYPE (exp) = TYPE_MAIN_VARIANT (type);
1117 begin_init_stmts (&stmt_expr, &compound_stmt);
1118 destroy_temps = stmts_are_full_exprs_p ();
1119 current_stmt_tree ()->stmts_are_full_exprs_p = 0;
1120 expand_aggr_init_1 (TYPE_BINFO (type), exp, exp,
1121 init, LOOKUP_NORMAL|flags);
1122 stmt_expr = finish_init_stmts (stmt_expr, compound_stmt);
1123 current_stmt_tree ()->stmts_are_full_exprs_p = destroy_temps;
1124 TREE_TYPE (exp) = type;
1125 TREE_READONLY (exp) = was_const;
1126 TREE_THIS_VOLATILE (exp) = was_volatile;
1131 /* Like build_aggr_init, but not just for aggregates. */
1134 build_init (tree decl, tree init, int flags)
1138 if (IS_AGGR_TYPE (TREE_TYPE (decl))
1139 || TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
1140 expr = build_aggr_init (decl, init, flags);
1142 expr = build (INIT_EXPR, TREE_TYPE (decl), decl, init);
1148 expand_default_init (tree binfo, tree true_exp, tree exp, tree init, int flags)
1150 tree type = TREE_TYPE (exp);
1153 /* It fails because there may not be a constructor which takes
1154 its own type as the first (or only parameter), but which does
1155 take other types via a conversion. So, if the thing initializing
1156 the expression is a unit element of type X, first try X(X&),
1157 followed by initialization by X. If neither of these work
1158 out, then look hard. */
1162 if (init && TREE_CODE (init) != TREE_LIST
1163 && (flags & LOOKUP_ONLYCONVERTING))
1165 /* Base subobjects should only get direct-initialization. */
1166 if (true_exp != exp)
1169 if (flags & DIRECT_BIND)
1170 /* Do nothing. We hit this in two cases: Reference initialization,
1171 where we aren't initializing a real variable, so we don't want
1172 to run a new constructor; and catching an exception, where we
1173 have already built up the constructor call so we could wrap it
1174 in an exception region. */;
1175 else if (TREE_CODE (init) == CONSTRUCTOR
1176 && TREE_HAS_CONSTRUCTOR (init))
1178 /* A brace-enclosed initializer for an aggregate. */
1179 my_friendly_assert (CP_AGGREGATE_TYPE_P (type), 20021016);
1180 init = digest_init (type, init, (tree *)NULL);
1183 init = ocp_convert (type, init, CONV_IMPLICIT|CONV_FORCE_TEMP, flags);
1185 if (TREE_CODE (init) == MUST_NOT_THROW_EXPR)
1186 /* We need to protect the initialization of a catch parm with a
1187 call to terminate(), which shows up as a MUST_NOT_THROW_EXPR
1188 around the TARGET_EXPR for the copy constructor. See
1189 initialize_handler_parm. */
1191 TREE_OPERAND (init, 0) = build (INIT_EXPR, TREE_TYPE (exp), exp,
1192 TREE_OPERAND (init, 0));
1193 TREE_TYPE (init) = void_type_node;
1196 init = build (INIT_EXPR, TREE_TYPE (exp), exp, init);
1197 TREE_SIDE_EFFECTS (init) = 1;
1198 finish_expr_stmt (init);
1202 if (init == NULL_TREE
1203 || (TREE_CODE (init) == TREE_LIST && ! TREE_TYPE (init)))
1207 init = TREE_VALUE (parms);
1210 parms = build_tree_list (NULL_TREE, init);
1212 if (true_exp == exp)
1213 ctor_name = complete_ctor_identifier;
1215 ctor_name = base_ctor_identifier;
1217 rval = build_special_member_call (exp, ctor_name, parms, binfo, flags);
1218 if (TREE_SIDE_EFFECTS (rval))
1220 if (building_stmt_tree ())
1221 finish_expr_stmt (rval);
1223 genrtl_expr_stmt (rval);
1227 /* This function is responsible for initializing EXP with INIT
1230 BINFO is the binfo of the type for who we are performing the
1231 initialization. For example, if W is a virtual base class of A and B,
1233 If we are initializing B, then W must contain B's W vtable, whereas
1234 were we initializing C, W must contain C's W vtable.
1236 TRUE_EXP is nonzero if it is the true expression being initialized.
1237 In this case, it may be EXP, or may just contain EXP. The reason we
1238 need this is because if EXP is a base element of TRUE_EXP, we
1239 don't necessarily know by looking at EXP where its virtual
1240 baseclass fields should really be pointing. But we do know
1241 from TRUE_EXP. In constructors, we don't know anything about
1242 the value being initialized.
1244 FLAGS is just passes to `build_method_call'. See that function for
1248 expand_aggr_init_1 (tree binfo, tree true_exp, tree exp, tree init, int flags)
1250 tree type = TREE_TYPE (exp);
1252 my_friendly_assert (init != error_mark_node && type != error_mark_node, 211);
1253 my_friendly_assert (building_stmt_tree (), 20021010);
1255 /* Use a function returning the desired type to initialize EXP for us.
1256 If the function is a constructor, and its first argument is
1257 NULL_TREE, know that it was meant for us--just slide exp on
1258 in and expand the constructor. Constructors now come
1261 if (init && TREE_CODE (exp) == VAR_DECL
1262 && TREE_CODE (init) == CONSTRUCTOR
1263 && TREE_HAS_CONSTRUCTOR (init))
1265 /* If store_init_value returns NULL_TREE, the INIT has been
1266 record in the DECL_INITIAL for EXP. That means there's
1267 nothing more we have to do. */
1268 if (store_init_value (exp, init))
1269 finish_expr_stmt (build (INIT_EXPR, type, exp, init));
1273 /* We know that expand_default_init can handle everything we want
1275 expand_default_init (binfo, true_exp, exp, init, flags);
1278 /* Report an error if TYPE is not a user-defined, aggregate type. If
1279 OR_ELSE is nonzero, give an error message. */
1282 is_aggr_type (tree type, int or_else)
1284 if (type == error_mark_node)
1287 if (! IS_AGGR_TYPE (type)
1288 && TREE_CODE (type) != TEMPLATE_TYPE_PARM
1289 && TREE_CODE (type) != BOUND_TEMPLATE_TEMPLATE_PARM)
1292 error ("`%T' is not an aggregate type", type);
1298 /* Like is_aggr_typedef, but returns typedef if successful. */
1301 get_aggr_from_typedef (tree name, int or_else)
1305 if (name == error_mark_node)
1308 if (IDENTIFIER_HAS_TYPE_VALUE (name))
1309 type = IDENTIFIER_TYPE_VALUE (name);
1313 error ("`%T' fails to be an aggregate typedef", name);
1317 if (! IS_AGGR_TYPE (type)
1318 && TREE_CODE (type) != TEMPLATE_TYPE_PARM
1319 && TREE_CODE (type) != BOUND_TEMPLATE_TEMPLATE_PARM)
1322 error ("type `%T' is of non-aggregate type", type);
1329 get_type_value (tree name)
1331 if (name == error_mark_node)
1334 if (IDENTIFIER_HAS_TYPE_VALUE (name))
1335 return IDENTIFIER_TYPE_VALUE (name);
1341 /* This code could just as well go in `class.c', but is placed here for
1344 /* For an expression of the form TYPE :: NAME (PARMLIST), build
1345 the appropriate function call. */
1348 build_member_call (tree type, tree name, tree parmlist)
1354 tree basetype_path, decl;
1356 if (TREE_CODE (name) == TEMPLATE_ID_EXPR
1357 && TREE_CODE (type) == NAMESPACE_DECL)
1359 /* 'name' already refers to the decls from the namespace, since we
1360 hit do_identifier for template_ids. */
1361 method_name = TREE_OPERAND (name, 0);
1362 /* FIXME: Since we don't do independent names right yet, the
1363 name might also be a LOOKUP_EXPR. Once we resolve this to a
1364 real decl earlier, this can go. This may happen during
1366 if (TREE_CODE (method_name) == LOOKUP_EXPR)
1368 method_name = lookup_namespace_name
1369 (type, TREE_OPERAND (method_name, 0));
1370 TREE_OPERAND (name, 0) = method_name;
1372 my_friendly_assert (is_overloaded_fn (method_name), 980519);
1373 return finish_call_expr (name, parmlist, /*disallow_virtual=*/true);
1377 name = DECL_NAME (name);
1379 if (TREE_CODE (type) == NAMESPACE_DECL)
1380 return finish_call_expr (lookup_namespace_name (type, name),
1382 /*disallow_virtual=*/true);
1384 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
1386 method_name = TREE_OPERAND (name, 0);
1387 if (TREE_CODE (method_name) == COMPONENT_REF)
1388 method_name = TREE_OPERAND (method_name, 1);
1389 if (is_overloaded_fn (method_name))
1390 method_name = DECL_NAME (OVL_CURRENT (method_name));
1391 TREE_OPERAND (name, 0) = method_name;
1396 if (TREE_CODE (method_name) == BIT_NOT_EXPR)
1398 method_name = TREE_OPERAND (method_name, 0);
1402 /* This shouldn't be here, and build_member_call shouldn't appear in
1404 if (type && TREE_CODE (type) == IDENTIFIER_NODE
1405 && get_aggr_from_typedef (type, 0) == 0)
1407 tree ns = lookup_name (type, 0);
1408 if (ns && TREE_CODE (ns) == NAMESPACE_DECL)
1409 return finish_call_expr (lookup_namespace_name (ns, name),
1411 /*disallow_virtual=*/true);
1414 if (type == NULL_TREE || ! is_aggr_type (type, 1))
1415 return error_mark_node;
1417 /* An operator we did not like. */
1418 if (name == NULL_TREE)
1419 return error_mark_node;
1423 error ("cannot call destructor `%T::~%T' without object", type,
1425 return error_mark_node;
1428 decl = maybe_dummy_object (type, &basetype_path);
1430 fns = lookup_fnfields (basetype_path, method_name, 0);
1433 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
1434 BASELINK_FUNCTIONS (fns) = build_nt (TEMPLATE_ID_EXPR,
1435 BASELINK_FUNCTIONS (fns),
1436 TREE_OPERAND (name, 1));
1437 return build_new_method_call (decl, fns, parmlist,
1438 /*conversion_path=*/NULL_TREE,
1439 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL);
1442 /* Convert 'this' to the specified type to disambiguate conversion
1443 to the function's context. */
1444 if (decl == current_class_ref
1445 /* ??? this is wrong, but if this conversion is invalid we need to
1446 defer it until we know whether we are calling a static or
1447 non-static member function. Be conservative for now. */
1448 && ACCESSIBLY_UNIQUELY_DERIVED_P (type, current_class_type))
1450 basetype_path = NULL_TREE;
1451 decl = build_scoped_ref (decl, type, &basetype_path);
1452 if (decl == error_mark_node)
1453 return error_mark_node;
1456 if (constructor_name_p (method_name, type))
1457 return build_functional_cast (type, parmlist);
1458 if (TREE_CODE (name) == IDENTIFIER_NODE
1459 && ((t = lookup_field (TYPE_BINFO (type), name, 1, false))))
1461 if (t == error_mark_node)
1462 return error_mark_node;
1463 if (TREE_CODE (t) == FIELD_DECL)
1465 if (is_dummy_object (decl))
1467 error ("invalid use of non-static field `%D'", t);
1468 return error_mark_node;
1470 decl = build (COMPONENT_REF, TREE_TYPE (t), decl, t);
1472 else if (TREE_CODE (t) == VAR_DECL)
1476 error ("invalid use of member `%D'", t);
1477 return error_mark_node;
1479 if (TYPE_LANG_SPECIFIC (TREE_TYPE (decl)))
1480 return build_new_op (CALL_EXPR, LOOKUP_NORMAL, decl,
1481 parmlist, NULL_TREE);
1482 return build_function_call (decl, parmlist);
1486 error ("no method `%T::%D'", type, name);
1487 return error_mark_node;
1491 /* Build a reference to a member of an aggregate. This is not a
1492 C++ `&', but really something which can have its address taken,
1493 and then act as a pointer to member, for example TYPE :: FIELD
1494 can have its address taken by saying & TYPE :: FIELD.
1496 @@ Prints out lousy diagnostics for operator <typename>
1499 @@ This function should be rewritten and placed in search.c. */
1502 build_offset_ref (tree type, tree name)
1506 tree basebinfo = NULL_TREE;
1507 tree orig_name = name;
1509 /* class templates can come in as TEMPLATE_DECLs here. */
1510 if (TREE_CODE (name) == TEMPLATE_DECL)
1513 if (processing_template_decl || uses_template_parms (type))
1514 return build_min_nt (SCOPE_REF, type, name);
1516 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
1518 /* If the NAME is a TEMPLATE_ID_EXPR, we are looking at
1519 something like `a.template f<int>' or the like. For the most
1520 part, we treat this just like a.f. We do remember, however,
1521 the template-id that was used. */
1522 name = TREE_OPERAND (orig_name, 0);
1525 name = DECL_NAME (name);
1528 if (TREE_CODE (name) == LOOKUP_EXPR)
1529 /* This can happen during tsubst'ing. */
1530 name = TREE_OPERAND (name, 0);
1533 if (TREE_CODE (name) == COMPONENT_REF)
1534 name = TREE_OPERAND (name, 1);
1535 if (TREE_CODE (name) == OVERLOAD)
1536 name = DECL_NAME (OVL_CURRENT (name));
1540 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 0);
1543 if (type == NULL_TREE)
1544 return error_mark_node;
1546 /* Handle namespace names fully here. */
1547 if (TREE_CODE (type) == NAMESPACE_DECL)
1549 tree t = lookup_namespace_name (type, name);
1550 if (t == error_mark_node)
1552 if (TREE_CODE (orig_name) == TEMPLATE_ID_EXPR)
1553 /* Reconstruct the TEMPLATE_ID_EXPR. */
1554 t = build (TEMPLATE_ID_EXPR, TREE_TYPE (t),
1555 t, TREE_OPERAND (orig_name, 1));
1556 if (! type_unknown_p (t))
1559 t = convert_from_reference (t);
1564 if (! is_aggr_type (type, 1))
1565 return error_mark_node;
1567 if (TREE_CODE (name) == BIT_NOT_EXPR)
1569 if (! check_dtor_name (type, name))
1570 error ("qualified type `%T' does not match destructor name `~%T'",
1571 type, TREE_OPERAND (name, 0));
1572 name = dtor_identifier;
1575 if (!COMPLETE_TYPE_P (complete_type (type))
1576 && !TYPE_BEING_DEFINED (type))
1578 error ("incomplete type `%T' does not have member `%D'", type,
1580 return error_mark_node;
1583 decl = maybe_dummy_object (type, &basebinfo);
1585 if (BASELINK_P (name) || DECL_P (name))
1589 member = lookup_member (basebinfo, name, 1, 0);
1591 if (member == error_mark_node)
1592 return error_mark_node;
1595 /* A lot of this logic is now handled in lookup_member. */
1596 if (member && BASELINK_P (member))
1598 /* Go from the TREE_BASELINK to the member function info. */
1599 tree fnfields = member;
1600 tree t = BASELINK_FUNCTIONS (fnfields);
1602 if (TREE_CODE (orig_name) == TEMPLATE_ID_EXPR)
1604 /* The FNFIELDS are going to contain functions that aren't
1605 necessarily templates, and templates that don't
1606 necessarily match the explicit template parameters. We
1607 save all the functions, and the explicit parameters, and
1608 then figure out exactly what to instantiate with what
1609 arguments in instantiate_type. */
1611 if (TREE_CODE (t) != OVERLOAD)
1612 /* The code in instantiate_type which will process this
1613 expects to encounter OVERLOADs, not raw functions. */
1614 t = ovl_cons (t, NULL_TREE);
1616 t = build (TEMPLATE_ID_EXPR, TREE_TYPE (t), t,
1617 TREE_OPERAND (orig_name, 1));
1618 t = build (OFFSET_REF, unknown_type_node, decl, t);
1620 PTRMEM_OK_P (t) = 1;
1625 if (TREE_CODE (t) != TEMPLATE_ID_EXPR && !really_overloaded_fn (t))
1627 /* Get rid of a potential OVERLOAD around it */
1628 t = OVL_CURRENT (t);
1630 /* unique functions are handled easily. */
1631 perform_or_defer_access_check (basebinfo, t);
1633 if (DECL_STATIC_FUNCTION_P (t))
1635 t = build (OFFSET_REF, TREE_TYPE (t), decl, t);
1636 PTRMEM_OK_P (t) = 1;
1640 TREE_TYPE (fnfields) = unknown_type_node;
1642 t = build (OFFSET_REF, unknown_type_node, decl, fnfields);
1643 PTRMEM_OK_P (t) = 1;
1647 if (member == NULL_TREE)
1649 error ("`%D' is not a member of type `%T'", name, type);
1650 return error_mark_node;
1653 if (TREE_CODE (member) == TYPE_DECL)
1655 TREE_USED (member) = 1;
1658 /* static class members and class-specific enum
1659 values can be returned without further ado. */
1660 if (TREE_CODE (member) == VAR_DECL || TREE_CODE (member) == CONST_DECL)
1663 return convert_from_reference (member);
1666 if (TREE_CODE (member) == FIELD_DECL && DECL_C_BIT_FIELD (member))
1668 error ("invalid pointer to bit-field `%D'", member);
1669 return error_mark_node;
1672 /* static class functions too. */
1673 if (TREE_CODE (member) == FUNCTION_DECL
1674 && TREE_CODE (TREE_TYPE (member)) == FUNCTION_TYPE)
1677 /* In member functions, the form `type::name' is no longer
1678 equivalent to `this->type::name', at least not until
1679 resolve_offset_ref. */
1680 member = build (OFFSET_REF, build_offset_type (type, TREE_TYPE (member)),
1682 PTRMEM_OK_P (member) = 1;
1686 /* If a OFFSET_REF made it through to here, then it did
1687 not have its address taken. */
1690 resolve_offset_ref (tree exp)
1694 my_friendly_assert (TREE_CODE (exp) == OFFSET_REF, 20030703);
1696 member = TREE_OPERAND (exp, 1);
1698 /* If MEMBER is non-static, then the program has fallen afoul of
1701 An id-expression that denotes a nonstatic data member or
1702 nonstatic member function of a class can only be used:
1704 -- as part of a class member access (_expr.ref_) in which the
1705 object-expression refers to the member's class or a class
1706 derived from that class, or
1708 -- to form a pointer to member (_expr.unary.op_), or
1710 -- in the body of a nonstatic member function of that class or
1711 of a class derived from that class (_class.mfct.nonstatic_), or
1713 -- in a mem-initializer for a constructor for that class or for
1714 a class derived from that class (_class.base.init_). */
1715 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (member))
1717 /* In Microsoft mode, treat a non-static member function as if
1718 it were a pointer-to-member. */
1719 if (flag_ms_extensions)
1720 return build_unary_op (ADDR_EXPR, exp, 0);
1721 error ("invalid use of non-static member function `%D'", member);
1722 return error_mark_node;
1724 else if (TREE_CODE (member) == FIELD_DECL)
1726 error ("invalid use of non-static data member `%D'", member);
1727 return error_mark_node;
1733 /* If DECL is a `const' declaration, and its value is a known
1734 constant, then return that value. */
1737 decl_constant_value (tree decl)
1739 if (TREE_READONLY_DECL_P (decl)
1740 && ! TREE_THIS_VOLATILE (decl)
1741 && DECL_INITIAL (decl)
1742 && DECL_INITIAL (decl) != error_mark_node
1743 /* This is invalid if initial value is not constant.
1744 If it has either a function call, a memory reference,
1745 or a variable, then re-evaluating it could give different results. */
1746 && TREE_CONSTANT (DECL_INITIAL (decl))
1747 /* Check for cases where this is sub-optimal, even though valid. */
1748 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1749 return DECL_INITIAL (decl);
1753 /* Common subroutines of build_new and build_vec_delete. */
1755 /* Call the global __builtin_delete to delete ADDR. */
1758 build_builtin_delete_call (tree addr)
1760 mark_used (global_delete_fndecl);
1761 return build_call (global_delete_fndecl, build_tree_list (NULL_TREE, addr));
1764 /* Generate a C++ "new" expression. DECL is either a TREE_LIST
1765 (which needs to go through some sort of groktypename) or it
1766 is the name of the class we are newing. INIT is an initialization value.
1767 It is either an EXPRLIST, an EXPR_NO_COMMAS, or something in braces.
1768 If INIT is void_type_node, it means do *not* call a constructor
1771 For types with constructors, the data returned is initialized
1772 by the appropriate constructor.
1774 Whether the type has a constructor or not, if it has a pointer
1775 to a virtual function table, then that pointer is set up
1778 Unless I am mistaken, a call to new () will return initialized
1779 data regardless of whether the constructor itself is private or
1780 not. NOPE; new fails if the constructor is private (jcm).
1782 Note that build_new does nothing to assure that any special
1783 alignment requirements of the type are met. Rather, it leaves
1784 it up to malloc to do the right thing. Otherwise, folding to
1785 the right alignment cal cause problems if the user tries to later
1786 free the memory returned by `new'.
1788 PLACEMENT is the `placement' list for user-defined operator new (). */
1791 build_new (tree placement, tree decl, tree init, int use_global_new)
1794 tree nelts = NULL_TREE, t;
1797 if (decl == error_mark_node)
1798 return error_mark_node;
1800 if (TREE_CODE (decl) == TREE_LIST)
1802 tree absdcl = TREE_VALUE (decl);
1803 tree last_absdcl = NULL_TREE;
1805 if (current_function_decl
1806 && DECL_CONSTRUCTOR_P (current_function_decl))
1807 my_friendly_assert (immediate_size_expand == 0, 19990926);
1809 nelts = integer_one_node;
1811 if (absdcl && TREE_CODE (absdcl) == CALL_EXPR)
1813 while (absdcl && TREE_CODE (absdcl) == INDIRECT_REF)
1815 last_absdcl = absdcl;
1816 absdcl = TREE_OPERAND (absdcl, 0);
1819 if (absdcl && TREE_CODE (absdcl) == ARRAY_REF)
1821 /* probably meant to be a vec new */
1824 while (TREE_OPERAND (absdcl, 0)
1825 && TREE_CODE (TREE_OPERAND (absdcl, 0)) == ARRAY_REF)
1827 last_absdcl = absdcl;
1828 absdcl = TREE_OPERAND (absdcl, 0);
1832 this_nelts = TREE_OPERAND (absdcl, 1);
1833 if (this_nelts != error_mark_node)
1835 if (this_nelts == NULL_TREE)
1836 error ("new of array type fails to specify size");
1837 else if (processing_template_decl)
1840 absdcl = TREE_OPERAND (absdcl, 0);
1844 if (build_expr_type_conversion (WANT_INT | WANT_ENUM,
1847 pedwarn ("size in array new must have integral type");
1849 this_nelts = save_expr (cp_convert (sizetype, this_nelts));
1850 absdcl = TREE_OPERAND (absdcl, 0);
1851 if (this_nelts == integer_zero_node)
1853 warning ("zero size array reserves no space");
1854 nelts = integer_zero_node;
1857 nelts = cp_build_binary_op (MULT_EXPR, nelts, this_nelts);
1861 nelts = integer_zero_node;
1865 TREE_OPERAND (last_absdcl, 0) = absdcl;
1867 TREE_VALUE (decl) = absdcl;
1869 type = groktypename (decl);
1870 if (! type || type == error_mark_node)
1871 return error_mark_node;
1873 else if (TREE_CODE (decl) == IDENTIFIER_NODE)
1875 if (IDENTIFIER_HAS_TYPE_VALUE (decl))
1877 /* An aggregate type. */
1878 type = IDENTIFIER_TYPE_VALUE (decl);
1879 decl = TYPE_MAIN_DECL (type);
1883 /* A builtin type. */
1884 decl = lookup_name (decl, 1);
1885 my_friendly_assert (TREE_CODE (decl) == TYPE_DECL, 215);
1886 type = TREE_TYPE (decl);
1889 else if (TREE_CODE (decl) == TYPE_DECL)
1891 type = TREE_TYPE (decl);
1896 decl = TYPE_MAIN_DECL (type);
1899 if (processing_template_decl)
1902 t = tree_cons (tree_cons (NULL_TREE, type, NULL_TREE),
1903 build_min_nt (ARRAY_REF, NULL_TREE, nelts),
1908 rval = build_min (NEW_EXPR, build_pointer_type (type),
1909 placement, t, init);
1910 NEW_EXPR_USE_GLOBAL (rval) = use_global_new;
1914 /* ``A reference cannot be created by the new operator. A reference
1915 is not an object (8.2.2, 8.4.3), so a pointer to it could not be
1916 returned by new.'' ARM 5.3.3 */
1917 if (TREE_CODE (type) == REFERENCE_TYPE)
1919 error ("new cannot be applied to a reference type");
1920 type = TREE_TYPE (type);
1923 if (TREE_CODE (type) == FUNCTION_TYPE)
1925 error ("new cannot be applied to a function type");
1926 return error_mark_node;
1929 /* When the object being created is an array, the new-expression yields a
1930 pointer to the initial element (if any) of the array. For example,
1931 both new int and new int[10] return an int*. 5.3.4. */
1932 if (TREE_CODE (type) == ARRAY_TYPE && has_array == 0)
1934 nelts = array_type_nelts_top (type);
1936 type = TREE_TYPE (type);
1940 t = build_nt (ARRAY_REF, type, nelts);
1944 rval = build (NEW_EXPR, build_pointer_type (type), placement, t, init);
1945 NEW_EXPR_USE_GLOBAL (rval) = use_global_new;
1946 TREE_SIDE_EFFECTS (rval) = 1;
1947 rval = build_new_1 (rval);
1948 if (rval == error_mark_node)
1949 return error_mark_node;
1951 /* Wrap it in a NOP_EXPR so warn_if_unused_value doesn't complain. */
1952 rval = build1 (NOP_EXPR, TREE_TYPE (rval), rval);
1953 TREE_NO_UNUSED_WARNING (rval) = 1;
1958 /* Given a Java class, return a decl for the corresponding java.lang.Class. */
1961 build_java_class_ref (tree type)
1963 tree name = NULL_TREE, class_decl;
1964 static tree CL_suffix = NULL_TREE;
1965 if (CL_suffix == NULL_TREE)
1966 CL_suffix = get_identifier("class$");
1967 if (jclass_node == NULL_TREE)
1969 jclass_node = IDENTIFIER_GLOBAL_VALUE (get_identifier ("jclass"));
1970 if (jclass_node == NULL_TREE)
1971 fatal_error ("call to Java constructor, while `jclass' undefined");
1973 jclass_node = TREE_TYPE (jclass_node);
1976 /* Mangle the class$ field */
1979 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1980 if (DECL_NAME (field) == CL_suffix)
1982 mangle_decl (field);
1983 name = DECL_ASSEMBLER_NAME (field);
1987 internal_error ("can't find class$");
1990 class_decl = IDENTIFIER_GLOBAL_VALUE (name);
1991 if (class_decl == NULL_TREE)
1993 class_decl = build_decl (VAR_DECL, name, TREE_TYPE (jclass_node));
1994 TREE_STATIC (class_decl) = 1;
1995 DECL_EXTERNAL (class_decl) = 1;
1996 TREE_PUBLIC (class_decl) = 1;
1997 DECL_ARTIFICIAL (class_decl) = 1;
1998 DECL_IGNORED_P (class_decl) = 1;
1999 pushdecl_top_level (class_decl);
2000 make_decl_rtl (class_decl, NULL);
2005 /* Returns the size of the cookie to use when allocating an array
2006 whose elements have the indicated TYPE. Assumes that it is already
2007 known that a cookie is needed. */
2010 get_cookie_size (tree type)
2014 /* We need to allocate an additional max (sizeof (size_t), alignof
2015 (true_type)) bytes. */
2019 sizetype_size = size_in_bytes (sizetype);
2020 type_align = size_int (TYPE_ALIGN_UNIT (type));
2021 if (INT_CST_LT_UNSIGNED (type_align, sizetype_size))
2022 cookie_size = sizetype_size;
2024 cookie_size = type_align;
2029 /* Called from cplus_expand_expr when expanding a NEW_EXPR. The return
2030 value is immediately handed to expand_expr. */
2033 build_new_1 (tree exp)
2035 tree placement, init;
2036 tree true_type, size, rval, t;
2037 /* The type of the new-expression. (This type is always a pointer
2040 /* The type pointed to by POINTER_TYPE. */
2042 /* The type being allocated. For "new T[...]" this will be an
2045 /* A pointer type pointing to to the FULL_TYPE. */
2046 tree full_pointer_type;
2047 tree outer_nelts = NULL_TREE;
2048 tree nelts = NULL_TREE;
2049 tree alloc_call, alloc_expr;
2050 /* The address returned by the call to "operator new". This node is
2051 a VAR_DECL and is therefore reusable. */
2054 tree cookie_expr, init_expr;
2056 enum tree_code code;
2057 int nothrow, check_new;
2058 /* Nonzero if the user wrote `::new' rather than just `new'. */
2059 int globally_qualified_p;
2060 int use_java_new = 0;
2061 /* If non-NULL, the number of extra bytes to allocate at the
2062 beginning of the storage allocated for an array-new expression in
2063 order to store the number of elements. */
2064 tree cookie_size = NULL_TREE;
2065 /* True if the function we are calling is a placement allocation
2067 bool placement_allocation_fn_p;
2068 tree args = NULL_TREE;
2069 /* True if the storage must be initialized, either by a constructor
2070 or due to an explicit new-initializer. */
2071 bool is_initialized;
2072 /* The address of the thing allocated, not including any cookie. In
2073 particular, if an array cookie is in use, DATA_ADDR is the
2074 address of the first array element. This node is a VAR_DECL, and
2075 is therefore reusable. */
2078 placement = TREE_OPERAND (exp, 0);
2079 type = TREE_OPERAND (exp, 1);
2080 init = TREE_OPERAND (exp, 2);
2081 globally_qualified_p = NEW_EXPR_USE_GLOBAL (exp);
2083 if (TREE_CODE (type) == ARRAY_REF)
2086 nelts = outer_nelts = TREE_OPERAND (type, 1);
2087 type = TREE_OPERAND (type, 0);
2089 /* Use an incomplete array type to avoid VLA headaches. */
2090 full_type = build_cplus_array_type (type, NULL_TREE);
2097 code = has_array ? VEC_NEW_EXPR : NEW_EXPR;
2099 /* If our base type is an array, then make sure we know how many elements
2101 while (TREE_CODE (true_type) == ARRAY_TYPE)
2103 tree this_nelts = array_type_nelts_top (true_type);
2104 nelts = cp_build_binary_op (MULT_EXPR, nelts, this_nelts);
2105 true_type = TREE_TYPE (true_type);
2108 if (!complete_type_or_else (true_type, exp))
2109 return error_mark_node;
2111 if (TREE_CODE (true_type) == VOID_TYPE)
2113 error ("invalid type `void' for new");
2114 return error_mark_node;
2117 if (abstract_virtuals_error (NULL_TREE, true_type))
2118 return error_mark_node;
2120 is_initialized = (TYPE_NEEDS_CONSTRUCTING (type) || init);
2121 if (CP_TYPE_CONST_P (true_type) && !is_initialized)
2123 error ("uninitialized const in `new' of `%#T'", true_type);
2124 return error_mark_node;
2127 size = size_in_bytes (true_type);
2129 size = size_binop (MULT_EXPR, size, convert (sizetype, nelts));
2131 /* Allocate the object. */
2132 if (! placement && TYPE_FOR_JAVA (true_type))
2134 tree class_addr, alloc_decl;
2135 tree class_decl = build_java_class_ref (true_type);
2136 tree class_size = size_in_bytes (true_type);
2137 static const char alloc_name[] = "_Jv_AllocObject";
2139 alloc_decl = IDENTIFIER_GLOBAL_VALUE (get_identifier (alloc_name));
2140 if (alloc_decl == NULL_TREE)
2141 fatal_error ("call to Java constructor with `%s' undefined",
2144 class_addr = build1 (ADDR_EXPR, jclass_node, class_decl);
2145 alloc_call = (build_function_call
2147 tree_cons (NULL_TREE, class_addr,
2148 build_tree_list (NULL_TREE, class_size))));
2154 fnname = ansi_opname (code);
2156 if (!globally_qualified_p
2157 && CLASS_TYPE_P (true_type)
2159 ? TYPE_HAS_ARRAY_NEW_OPERATOR (true_type)
2160 : TYPE_HAS_NEW_OPERATOR (true_type)))
2162 /* Use a class-specific operator new. */
2163 /* If a cookie is required, add some extra space. */
2164 if (has_array && TYPE_VEC_NEW_USES_COOKIE (true_type))
2166 cookie_size = get_cookie_size (true_type);
2167 size = size_binop (PLUS_EXPR, size, cookie_size);
2169 /* Create the argument list. */
2170 args = tree_cons (NULL_TREE, size, placement);
2171 /* Call the function. */
2172 alloc_call = build_method_call (build_dummy_object (true_type),
2174 TYPE_BINFO (true_type),
2179 /* Use a global operator new. */
2180 /* See if a cookie might be required. */
2181 if (has_array && TYPE_VEC_NEW_USES_COOKIE (true_type))
2182 cookie_size = get_cookie_size (true_type);
2184 cookie_size = NULL_TREE;
2186 alloc_call = build_operator_new_call (fnname, placement,
2187 &size, &cookie_size);
2191 if (alloc_call == error_mark_node)
2192 return error_mark_node;
2194 /* The ALLOC_CALL should be a CALL_EXPR -- or a COMPOUND_EXPR whose
2195 right-hand-side is ultimately a CALL_EXPR -- and the first
2196 operand should be the address of a known FUNCTION_DECL. */
2198 while (TREE_CODE (t) == COMPOUND_EXPR)
2199 t = TREE_OPERAND (t, 1);
2200 alloc_fn = get_callee_fndecl (t);
2201 my_friendly_assert (alloc_fn != NULL_TREE, 20020325);
2203 /* Now, check to see if this function is actually a placement
2204 allocation function. This can happen even when PLACEMENT is NULL
2205 because we might have something like:
2207 struct S { void* operator new (size_t, int i = 0); };
2209 A call to `new S' will get this allocation function, even though
2210 there is no explicit placement argument. If there is more than
2211 one argument, or there are variable arguments, then this is a
2212 placement allocation function. */
2213 placement_allocation_fn_p
2214 = (type_num_arguments (TREE_TYPE (alloc_fn)) > 1
2215 || varargs_function_p (alloc_fn));
2217 /* unless an allocation function is declared with an empty excep-
2218 tion-specification (_except.spec_), throw(), it indicates failure to
2219 allocate storage by throwing a bad_alloc exception (clause _except_,
2220 _lib.bad.alloc_); it returns a non-null pointer otherwise If the allo-
2221 cation function is declared with an empty exception-specification,
2222 throw(), it returns null to indicate failure to allocate storage and a
2223 non-null pointer otherwise.
2225 So check for a null exception spec on the op new we just called. */
2227 nothrow = TYPE_NOTHROW_P (TREE_TYPE (alloc_fn));
2228 check_new = (flag_check_new || nothrow) && ! use_java_new;
2230 /* In the simple case, we can stop now. */
2231 pointer_type = build_pointer_type (type);
2232 if (!cookie_size && !is_initialized)
2233 return build_nop (pointer_type, alloc_call);
2235 /* While we're working, use a pointer to the type we've actually
2236 allocated. Store the result of the call in a variable so that we
2237 can use it more than once. */
2238 full_pointer_type = build_pointer_type (full_type);
2239 alloc_expr = get_target_expr (build_nop (full_pointer_type, alloc_call));
2240 alloc_node = TARGET_EXPR_SLOT (alloc_expr);
2246 /* Adjust so we're pointing to the start of the object. */
2247 data_addr = get_target_expr (build (PLUS_EXPR, full_pointer_type,
2248 alloc_node, cookie_size));
2250 /* Store the number of bytes allocated so that we can know how
2251 many elements to destroy later. We use the last sizeof
2252 (size_t) bytes to store the number of elements. */
2253 cookie = build (MINUS_EXPR, build_pointer_type (sizetype),
2254 data_addr, size_in_bytes (sizetype));
2255 cookie = build_indirect_ref (cookie, NULL);
2257 cookie_expr = build (MODIFY_EXPR, sizetype, cookie, nelts);
2258 data_addr = TARGET_EXPR_SLOT (data_addr);
2262 cookie_expr = NULL_TREE;
2263 data_addr = alloc_node;
2266 /* Now initialize the allocated object. */
2269 init_expr = build_indirect_ref (data_addr, NULL);
2271 if (init == void_zero_node)
2272 init = build_default_init (full_type, nelts);
2273 else if (init && pedantic && has_array)
2274 pedwarn ("ISO C++ forbids initialization in array new");
2278 = build_vec_init (init_expr,
2279 cp_build_binary_op (MINUS_EXPR, outer_nelts,
2281 init, /*from_array=*/0);
2282 else if (TYPE_NEEDS_CONSTRUCTING (type))
2283 init_expr = build_special_member_call (init_expr,
2284 complete_ctor_identifier,
2285 init, TYPE_BINFO (true_type),
2289 /* We are processing something like `new int (10)', which
2290 means allocate an int, and initialize it with 10. */
2292 if (TREE_CODE (init) == TREE_LIST)
2294 if (TREE_CHAIN (init) != NULL_TREE)
2296 ("initializer list being treated as compound expression");
2297 init = build_compound_expr (init);
2299 else if (TREE_CODE (init) == CONSTRUCTOR
2300 && TREE_TYPE (init) == NULL_TREE)
2302 pedwarn ("ISO C++ forbids aggregate initializer to new");
2303 init = digest_init (type, init, 0);
2306 init_expr = build_modify_expr (init_expr, INIT_EXPR, init);
2309 if (init_expr == error_mark_node)
2310 return error_mark_node;
2312 /* If any part of the object initialization terminates by throwing an
2313 exception and a suitable deallocation function can be found, the
2314 deallocation function is called to free the memory in which the
2315 object was being constructed, after which the exception continues
2316 to propagate in the context of the new-expression. If no
2317 unambiguous matching deallocation function can be found,
2318 propagating the exception does not cause the object's memory to be
2320 if (flag_exceptions && ! use_java_new)
2322 enum tree_code dcode = has_array ? VEC_DELETE_EXPR : DELETE_EXPR;
2324 int flags = (LOOKUP_NORMAL
2325 | (globally_qualified_p * LOOKUP_GLOBAL));
2327 /* The Standard is unclear here, but the right thing to do
2328 is to use the same method for finding deallocation
2329 functions that we use for finding allocation functions. */
2330 flags |= LOOKUP_SPECULATIVELY;
2332 cleanup = build_op_delete_call (dcode, alloc_node, size, flags,
2333 (placement_allocation_fn_p
2334 ? alloc_call : NULL_TREE));
2336 /* Ack! First we allocate the memory. Then we set our sentry
2337 variable to true, and expand a cleanup that deletes the memory
2338 if sentry is true. Then we run the constructor, and finally
2341 It would be nice to be able to handle this without the sentry
2342 variable, perhaps with a TRY_CATCH_EXPR, but this doesn't
2343 work. We allocate the space first, so if there are any
2344 temporaries with cleanups in the constructor args we need this
2345 EH region to extend until end of full-expression to preserve
2348 If the backend had some mechanism so that we could force the
2349 allocation to be expanded after all the other args to the
2350 constructor, that would fix the nesting problem and we could
2351 do away with this complexity. But that would complicate other
2352 things; in particular, it would make it difficult to bail out
2353 if the allocation function returns null. Er, no, it wouldn't;
2354 we just don't run the constructor. The standard says it's
2355 unspecified whether or not the args are evaluated.
2357 FIXME FIXME FIXME inline invisible refs as refs. That way we
2358 can preevaluate value parameters. */
2362 tree end, sentry, begin;
2364 begin = get_target_expr (boolean_true_node);
2365 CLEANUP_EH_ONLY (begin) = 1;
2367 sentry = TARGET_EXPR_SLOT (begin);
2369 TARGET_EXPR_CLEANUP (begin)
2370 = build (COND_EXPR, void_type_node, sentry,
2371 cleanup, void_zero_node);
2373 end = build (MODIFY_EXPR, TREE_TYPE (sentry),
2374 sentry, boolean_false_node);
2377 = build (COMPOUND_EXPR, void_type_node, begin,
2378 build (COMPOUND_EXPR, void_type_node, init_expr,
2384 init_expr = NULL_TREE;
2386 /* Now build up the return value in reverse order. */
2391 rval = build (COMPOUND_EXPR, TREE_TYPE (rval), init_expr, rval);
2393 rval = build (COMPOUND_EXPR, TREE_TYPE (rval), cookie_expr, rval);
2395 if (rval == alloc_node)
2396 /* If we don't have an initializer or a cookie, strip the TARGET_EXPR
2397 and return the call (which doesn't need to be adjusted). */
2398 rval = TARGET_EXPR_INITIAL (alloc_expr);
2403 tree ifexp = cp_build_binary_op (NE_EXPR, alloc_node,
2405 rval = build_conditional_expr (ifexp, rval, alloc_node);
2408 /* Perform the allocation before anything else, so that ALLOC_NODE
2409 has been initialized before we start using it. */
2410 rval = build (COMPOUND_EXPR, TREE_TYPE (rval), alloc_expr, rval);
2413 /* Convert to the final type. */
2414 return build_nop (pointer_type, rval);
2418 build_vec_delete_1 (tree base, tree maxindex, tree type,
2419 special_function_kind auto_delete_vec, int use_global_delete)
2422 tree ptype = build_pointer_type (type = complete_type (type));
2423 tree size_exp = size_in_bytes (type);
2425 /* Temporary variables used by the loop. */
2426 tree tbase, tbase_init;
2428 /* This is the body of the loop that implements the deletion of a
2429 single element, and moves temp variables to next elements. */
2432 /* This is the LOOP_EXPR that governs the deletion of the elements. */
2435 /* This is the thing that governs what to do after the loop has run. */
2436 tree deallocate_expr = 0;
2438 /* This is the BIND_EXPR which holds the outermost iterator of the
2439 loop. It is convenient to set this variable up and test it before
2440 executing any other code in the loop.
2441 This is also the containing expression returned by this function. */
2442 tree controller = NULL_TREE;
2444 /* We should only have 1-D arrays here. */
2445 if (TREE_CODE (type) == ARRAY_TYPE)
2448 if (! IS_AGGR_TYPE (type) || TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
2450 loop = integer_zero_node;
2454 /* The below is short by the cookie size. */
2455 virtual_size = size_binop (MULT_EXPR, size_exp,
2456 convert (sizetype, maxindex));
2458 tbase = create_temporary_var (ptype);
2459 tbase_init = build_modify_expr (tbase, NOP_EXPR,
2460 fold (build (PLUS_EXPR, ptype,
2463 DECL_REGISTER (tbase) = 1;
2464 controller = build (BIND_EXPR, void_type_node, tbase, NULL_TREE, NULL_TREE);
2465 TREE_SIDE_EFFECTS (controller) = 1;
2469 body = tree_cons (NULL_TREE,
2470 build_delete (ptype, tbase, sfk_complete_destructor,
2471 LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 1),
2474 body = tree_cons (NULL_TREE,
2475 build_modify_expr (tbase, NOP_EXPR, build (MINUS_EXPR, ptype, tbase, size_exp)),
2478 body = tree_cons (NULL_TREE,
2479 build (EXIT_EXPR, void_type_node,
2480 build (EQ_EXPR, boolean_type_node, base, tbase)),
2483 loop = build (LOOP_EXPR, void_type_node, build_compound_expr (body));
2485 loop = tree_cons (NULL_TREE, tbase_init,
2486 tree_cons (NULL_TREE, loop, NULL_TREE));
2487 loop = build_compound_expr (loop);
2490 /* If the delete flag is one, or anything else with the low bit set,
2491 delete the storage. */
2492 deallocate_expr = integer_zero_node;
2493 if (auto_delete_vec != sfk_base_destructor)
2497 /* The below is short by the cookie size. */
2498 virtual_size = size_binop (MULT_EXPR, size_exp,
2499 convert (sizetype, maxindex));
2501 if (! TYPE_VEC_NEW_USES_COOKIE (type))
2508 cookie_size = get_cookie_size (type);
2510 = cp_convert (ptype,
2511 cp_build_binary_op (MINUS_EXPR,
2512 cp_convert (string_type_node,
2515 /* True size with header. */
2516 virtual_size = size_binop (PLUS_EXPR, virtual_size, cookie_size);
2519 if (auto_delete_vec == sfk_deleting_destructor)
2520 deallocate_expr = build_x_delete (base_tbd,
2521 2 | use_global_delete,
2525 if (loop && deallocate_expr != integer_zero_node)
2527 body = tree_cons (NULL_TREE, loop,
2528 tree_cons (NULL_TREE, deallocate_expr, NULL_TREE));
2529 body = build_compound_expr (body);
2534 /* Outermost wrapper: If pointer is null, punt. */
2535 body = fold (build (COND_EXPR, void_type_node,
2536 fold (build (NE_EXPR, boolean_type_node, base,
2537 integer_zero_node)),
2538 body, integer_zero_node));
2539 body = build1 (NOP_EXPR, void_type_node, body);
2543 TREE_OPERAND (controller, 1) = body;
2547 if (TREE_CODE (base) == SAVE_EXPR)
2548 /* Pre-evaluate the SAVE_EXPR outside of the BIND_EXPR. */
2549 body = build (COMPOUND_EXPR, void_type_node, base, body);
2551 return convert_to_void (body, /*implicit=*/NULL);
2554 /* Create an unnamed variable of the indicated TYPE. */
2557 create_temporary_var (tree type)
2561 decl = build_decl (VAR_DECL, NULL_TREE, type);
2562 TREE_USED (decl) = 1;
2563 DECL_ARTIFICIAL (decl) = 1;
2564 DECL_SOURCE_LOCATION (decl) = input_location;
2565 DECL_IGNORED_P (decl) = 1;
2566 DECL_CONTEXT (decl) = current_function_decl;
2571 /* Create a new temporary variable of the indicated TYPE, initialized
2574 It is not entered into current_binding_level, because that breaks
2575 things when it comes time to do final cleanups (which take place
2576 "outside" the binding contour of the function). */
2579 get_temp_regvar (tree type, tree init)
2583 decl = create_temporary_var (type);
2584 if (building_stmt_tree ())
2585 add_decl_stmt (decl);
2587 SET_DECL_RTL (decl, assign_temp (type, 2, 0, 1));
2588 finish_expr_stmt (build_modify_expr (decl, INIT_EXPR, init));
2593 /* `build_vec_init' returns tree structure that performs
2594 initialization of a vector of aggregate types.
2596 BASE is a reference to the vector, of ARRAY_TYPE.
2597 MAXINDEX is the maximum index of the array (one less than the
2598 number of elements). It is only used if
2599 TYPE_DOMAIN (TREE_TYPE (BASE)) == NULL_TREE.
2600 INIT is the (possibly NULL) initializer.
2602 FROM_ARRAY is 0 if we should init everything with INIT
2603 (i.e., every element initialized from INIT).
2604 FROM_ARRAY is 1 if we should index into INIT in parallel
2605 with initialization of DECL.
2606 FROM_ARRAY is 2 if we should index into INIT in parallel,
2607 but use assignment instead of initialization. */
2610 build_vec_init (tree base, tree maxindex, tree init, int from_array)
2613 tree base2 = NULL_TREE;
2615 tree itype = NULL_TREE;
2617 /* The type of the array. */
2618 tree atype = TREE_TYPE (base);
2619 /* The type of an element in the array. */
2620 tree type = TREE_TYPE (atype);
2621 /* The type of a pointer to an element in the array. */
2626 tree try_block = NULL_TREE;
2627 tree try_body = NULL_TREE;
2628 int num_initialized_elts = 0;
2630 if (TYPE_DOMAIN (atype))
2631 maxindex = array_type_nelts (atype);
2633 if (maxindex == NULL_TREE || maxindex == error_mark_node)
2634 return error_mark_node;
2638 ? (!CLASS_TYPE_P (type) || !TYPE_HAS_COMPLEX_ASSIGN_REF (type))
2639 : !TYPE_NEEDS_CONSTRUCTING (type))
2640 && ((TREE_CODE (init) == CONSTRUCTOR
2641 /* Don't do this if the CONSTRUCTOR might contain something
2642 that might throw and require us to clean up. */
2643 && (CONSTRUCTOR_ELTS (init) == NULL_TREE
2644 || ! TYPE_HAS_NONTRIVIAL_DESTRUCTOR (target_type (type))))
2647 /* Do non-default initialization of POD arrays resulting from
2648 brace-enclosed initializers. In this case, digest_init and
2649 store_constructor will handle the semantics for us. */
2651 stmt_expr = build (INIT_EXPR, atype, base, init);
2655 maxindex = cp_convert (ptrdiff_type_node, maxindex);
2656 ptype = build_pointer_type (type);
2657 size = size_in_bytes (type);
2658 if (TREE_CODE (TREE_TYPE (base)) == ARRAY_TYPE)
2659 base = cp_convert (ptype, default_conversion (base));
2661 /* The code we are generating looks like:
2665 ptrdiff_t iterator = maxindex;
2667 for (; iterator != -1; --iterator) {
2668 ... initialize *t1 ...
2672 ... destroy elements that were constructed ...
2676 We can omit the try and catch blocks if we know that the
2677 initialization will never throw an exception, or if the array
2678 elements do not have destructors. We can omit the loop completely if
2679 the elements of the array do not have constructors.
2681 We actually wrap the entire body of the above in a STMT_EXPR, for
2684 When copying from array to another, when the array elements have
2685 only trivial copy constructors, we should use __builtin_memcpy
2686 rather than generating a loop. That way, we could take advantage
2687 of whatever cleverness the back-end has for dealing with copies
2688 of blocks of memory. */
2690 begin_init_stmts (&stmt_expr, &compound_stmt);
2691 destroy_temps = stmts_are_full_exprs_p ();
2692 current_stmt_tree ()->stmts_are_full_exprs_p = 0;
2693 rval = get_temp_regvar (ptype, base);
2694 base = get_temp_regvar (ptype, rval);
2695 iterator = get_temp_regvar (ptrdiff_type_node, maxindex);
2697 /* Protect the entire array initialization so that we can destroy
2698 the partially constructed array if an exception is thrown.
2699 But don't do this if we're assigning. */
2700 if (flag_exceptions && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
2703 try_block = begin_try_block ();
2704 try_body = begin_compound_stmt (/*has_no_scope=*/1);
2707 if (init != NULL_TREE && TREE_CODE (init) == CONSTRUCTOR)
2709 /* Do non-default initialization of non-POD arrays resulting from
2710 brace-enclosed initializers. */
2715 for (elts = CONSTRUCTOR_ELTS (init); elts; elts = TREE_CHAIN (elts))
2717 tree elt = TREE_VALUE (elts);
2718 tree baseref = build1 (INDIRECT_REF, type, base);
2720 num_initialized_elts++;
2722 if (IS_AGGR_TYPE (type) || TREE_CODE (type) == ARRAY_TYPE)
2723 finish_expr_stmt (build_aggr_init (baseref, elt, 0));
2725 finish_expr_stmt (build_modify_expr (baseref, NOP_EXPR,
2728 finish_expr_stmt (build_unary_op (PREINCREMENT_EXPR, base, 0));
2729 finish_expr_stmt (build_unary_op (PREDECREMENT_EXPR, iterator, 0));
2732 /* Clear out INIT so that we don't get confused below. */
2735 else if (from_array)
2737 /* If initializing one array from another, initialize element by
2738 element. We rely upon the below calls the do argument
2742 base2 = default_conversion (init);
2743 itype = TREE_TYPE (base2);
2744 base2 = get_temp_regvar (itype, base2);
2745 itype = TREE_TYPE (itype);
2747 else if (TYPE_LANG_SPECIFIC (type)
2748 && TYPE_NEEDS_CONSTRUCTING (type)
2749 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
2751 error ("initializer ends prematurely");
2752 return error_mark_node;
2756 /* Now, default-initialize any remaining elements. We don't need to
2757 do that if a) the type does not need constructing, or b) we've
2758 already initialized all the elements.
2760 We do need to keep going if we're copying an array. */
2763 || (TYPE_NEEDS_CONSTRUCTING (type)
2764 && ! (host_integerp (maxindex, 0)
2765 && (num_initialized_elts
2766 == tree_low_cst (maxindex, 0) + 1))))
2768 /* If the ITERATOR is equal to -1, then we don't have to loop;
2769 we've already initialized all the elements. */
2774 for_stmt = begin_for_stmt ();
2775 finish_for_init_stmt (for_stmt);
2776 finish_for_cond (build (NE_EXPR, boolean_type_node,
2777 iterator, integer_minus_one_node),
2779 finish_for_expr (build_unary_op (PREDECREMENT_EXPR, iterator, 0),
2782 /* Otherwise, loop through the elements. */
2783 for_body = begin_compound_stmt (/*has_no_scope=*/1);
2785 /* When we're not building a statement-tree, things are a little
2786 complicated. If, when we recursively call build_aggr_init,
2787 an expression containing a TARGET_EXPR is expanded, then it
2788 may get a cleanup. Then, the result of that expression is
2789 passed to finish_expr_stmt, which will call
2790 expand_start_target_temps/expand_end_target_temps. However,
2791 the latter call will not cause the cleanup to run because
2792 that block will still be on the block stack. So, we call
2793 expand_start_target_temps here manually; the corresponding
2794 call to expand_end_target_temps below will cause the cleanup
2796 if (!building_stmt_tree ())
2797 expand_start_target_temps ();
2801 tree to = build1 (INDIRECT_REF, type, base);
2805 from = build1 (INDIRECT_REF, itype, base2);
2809 if (from_array == 2)
2810 elt_init = build_modify_expr (to, NOP_EXPR, from);
2811 else if (TYPE_NEEDS_CONSTRUCTING (type))
2812 elt_init = build_aggr_init (to, from, 0);
2814 elt_init = build_modify_expr (to, NOP_EXPR, from);
2818 else if (TREE_CODE (type) == ARRAY_TYPE)
2822 ("cannot initialize multi-dimensional array with initializer");
2823 elt_init = build_vec_init (build1 (INDIRECT_REF, type, base),
2827 elt_init = build_aggr_init (build1 (INDIRECT_REF, type, base),
2830 /* The initialization of each array element is a
2831 full-expression, as per core issue 124. */
2832 if (!building_stmt_tree ())
2834 genrtl_expr_stmt (elt_init);
2835 expand_end_target_temps ();
2839 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
2840 finish_expr_stmt (elt_init);
2841 current_stmt_tree ()->stmts_are_full_exprs_p = 0;
2844 finish_expr_stmt (build_unary_op (PREINCREMENT_EXPR, base, 0));
2846 finish_expr_stmt (build_unary_op (PREINCREMENT_EXPR, base2, 0));
2848 finish_compound_stmt (/*has_no_scope=*/1, for_body);
2849 finish_for_stmt (for_stmt);
2852 /* Make sure to cleanup any partially constructed elements. */
2853 if (flag_exceptions && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
2857 tree m = cp_build_binary_op (MINUS_EXPR, maxindex, iterator);
2859 /* Flatten multi-dimensional array since build_vec_delete only
2860 expects one-dimensional array. */
2861 if (TREE_CODE (type) == ARRAY_TYPE)
2863 m = cp_build_binary_op (MULT_EXPR, m,
2864 array_type_nelts_total (type));
2865 type = strip_array_types (type);
2868 finish_compound_stmt (/*has_no_scope=*/1, try_body);
2869 finish_cleanup_try_block (try_block);
2870 e = build_vec_delete_1 (rval, m,
2872 sfk_base_destructor,
2873 /*use_global_delete=*/0);
2874 finish_cleanup (e, try_block);
2877 /* The value of the array initialization is the address of the
2878 first element in the array. */
2879 finish_expr_stmt (rval);
2881 stmt_expr = finish_init_stmts (stmt_expr, compound_stmt);
2882 current_stmt_tree ()->stmts_are_full_exprs_p = destroy_temps;
2886 /* Free up storage of type TYPE, at address ADDR.
2888 TYPE is a POINTER_TYPE and can be ptr_type_node for no special type
2891 VIRTUAL_SIZE is the amount of storage that was allocated, and is
2892 used as the second argument to operator delete. It can include
2893 things like padding and magic size cookies. It has virtual in it,
2894 because if you have a base pointer and you delete through a virtual
2895 destructor, it should be the size of the dynamic object, not the
2896 static object, see Free Store 12.5 ISO C++.
2898 This does not call any destructors. */
2901 build_x_delete (tree addr, int which_delete, tree virtual_size)
2903 int use_global_delete = which_delete & 1;
2904 int use_vec_delete = !!(which_delete & 2);
2905 enum tree_code code = use_vec_delete ? VEC_DELETE_EXPR : DELETE_EXPR;
2906 int flags = LOOKUP_NORMAL | (use_global_delete * LOOKUP_GLOBAL);
2908 return build_op_delete_call (code, addr, virtual_size, flags, NULL_TREE);
2911 /* Call the DTOR_KIND destructor for EXP. FLAGS are as for
2915 build_dtor_call (tree exp, special_function_kind dtor_kind, int flags)
2921 case sfk_complete_destructor:
2922 name = complete_dtor_identifier;
2925 case sfk_base_destructor:
2926 name = base_dtor_identifier;
2929 case sfk_deleting_destructor:
2930 name = deleting_dtor_identifier;
2936 return build_method_call (exp, name, NULL_TREE,
2937 TYPE_BINFO (TREE_TYPE (exp)), flags);
2940 /* Generate a call to a destructor. TYPE is the type to cast ADDR to.
2941 ADDR is an expression which yields the store to be destroyed.
2942 AUTO_DELETE is the name of the destructor to call, i.e., either
2943 sfk_complete_destructor, sfk_base_destructor, or
2944 sfk_deleting_destructor.
2946 FLAGS is the logical disjunction of zero or more LOOKUP_
2947 flags. See cp-tree.h for more info. */
2950 build_delete (tree type, tree addr, special_function_kind auto_delete,
2951 int flags, int use_global_delete)
2955 if (addr == error_mark_node)
2956 return error_mark_node;
2958 /* Can happen when CURRENT_EXCEPTION_OBJECT gets its type
2959 set to `error_mark_node' before it gets properly cleaned up. */
2960 if (type == error_mark_node)
2961 return error_mark_node;
2963 type = TYPE_MAIN_VARIANT (type);
2965 if (TREE_CODE (type) == POINTER_TYPE)
2967 type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
2968 if (TREE_CODE (type) == ARRAY_TYPE)
2971 if (VOID_TYPE_P (type)
2972 /* We don't want to warn about delete of void*, only other
2973 incomplete types. Deleting other incomplete types
2974 invokes undefined behavior, but it is not ill-formed, so
2975 compile to something that would even do The Right Thing
2976 (TM) should the type have a trivial dtor and no delete
2978 || !complete_type_or_diagnostic (type, addr, 1)
2979 || !IS_AGGR_TYPE (type))
2981 /* Call the builtin operator delete. */
2982 return build_builtin_delete_call (addr);
2984 if (TREE_SIDE_EFFECTS (addr))
2985 addr = save_expr (addr);
2987 /* throw away const and volatile on target type of addr */
2988 addr = convert_force (build_pointer_type (type), addr, 0);
2990 else if (TREE_CODE (type) == ARRAY_TYPE)
2994 if (TYPE_DOMAIN (type) == NULL_TREE)
2996 error ("unknown array size in delete");
2997 return error_mark_node;
2999 return build_vec_delete (addr, array_type_nelts (type),
3000 auto_delete, use_global_delete);
3004 /* Don't check PROTECT here; leave that decision to the
3005 destructor. If the destructor is accessible, call it,
3006 else report error. */
3007 addr = build_unary_op (ADDR_EXPR, addr, 0);
3008 if (TREE_SIDE_EFFECTS (addr))
3009 addr = save_expr (addr);
3011 addr = convert_force (build_pointer_type (type), addr, 0);
3014 my_friendly_assert (IS_AGGR_TYPE (type), 220);
3016 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
3018 if (auto_delete != sfk_deleting_destructor)
3019 return void_zero_node;
3021 return build_op_delete_call
3022 (DELETE_EXPR, addr, cxx_sizeof_nowarn (type),
3023 LOOKUP_NORMAL | (use_global_delete * LOOKUP_GLOBAL),
3028 tree do_delete = NULL_TREE;
3031 my_friendly_assert (TYPE_HAS_DESTRUCTOR (type), 20011213);
3033 /* For `::delete x', we must not use the deleting destructor
3034 since then we would not be sure to get the global `operator
3036 if (use_global_delete && auto_delete == sfk_deleting_destructor)
3038 /* We will use ADDR multiple times so we must save it. */
3039 addr = save_expr (addr);
3040 /* Delete the object. */
3041 do_delete = build_builtin_delete_call (addr);
3042 /* Otherwise, treat this like a complete object destructor
3044 auto_delete = sfk_complete_destructor;
3046 /* If the destructor is non-virtual, there is no deleting
3047 variant. Instead, we must explicitly call the appropriate
3048 `operator delete' here. */
3049 else if (!DECL_VIRTUAL_P (CLASSTYPE_DESTRUCTORS (type))
3050 && auto_delete == sfk_deleting_destructor)
3052 /* We will use ADDR multiple times so we must save it. */
3053 addr = save_expr (addr);
3054 /* Build the call. */
3055 do_delete = build_op_delete_call (DELETE_EXPR,
3057 cxx_sizeof_nowarn (type),
3060 /* Call the complete object destructor. */
3061 auto_delete = sfk_complete_destructor;
3063 else if (auto_delete == sfk_deleting_destructor
3064 && TYPE_GETS_REG_DELETE (type))
3066 /* Make sure we have access to the member op delete, even though
3067 we'll actually be calling it from the destructor. */
3068 build_op_delete_call (DELETE_EXPR, addr, cxx_sizeof_nowarn (type),
3069 LOOKUP_NORMAL, NULL_TREE);
3072 expr = build_dtor_call (build_indirect_ref (addr, NULL),
3073 auto_delete, flags);
3075 expr = build (COMPOUND_EXPR, void_type_node, expr, do_delete);
3077 if (flags & LOOKUP_DESTRUCTOR)
3078 /* Explicit destructor call; don't check for null pointer. */
3079 ifexp = integer_one_node;
3081 /* Handle deleting a null pointer. */
3082 ifexp = fold (cp_build_binary_op (NE_EXPR, addr, integer_zero_node));
3084 if (ifexp != integer_one_node)
3085 expr = build (COND_EXPR, void_type_node,
3086 ifexp, expr, void_zero_node);
3092 /* At the beginning of a destructor, push cleanups that will call the
3093 destructors for our base classes and members.
3095 Called from begin_destructor_body. */
3098 push_base_cleanups ()
3101 int i, n_baseclasses;
3105 /* Run destructors for all virtual baseclasses. */
3106 if (TYPE_USES_VIRTUAL_BASECLASSES (current_class_type))
3109 tree cond = (condition_conversion
3110 (build (BIT_AND_EXPR, integer_type_node,
3111 current_in_charge_parm,
3112 integer_two_node)));
3114 vbases = CLASSTYPE_VBASECLASSES (current_class_type);
3115 /* The CLASSTYPE_VBASECLASSES list is in initialization
3116 order, which is also the right order for pushing cleanups. */
3118 vbases = TREE_CHAIN (vbases))
3120 tree vbase = TREE_VALUE (vbases);
3121 tree base_type = BINFO_TYPE (vbase);
3123 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (base_type))
3125 expr = build_special_member_call (current_class_ref,
3126 base_dtor_identifier,
3130 | LOOKUP_NONVIRTUAL));
3131 expr = build (COND_EXPR, void_type_node, cond,
3132 expr, void_zero_node);
3133 finish_decl_cleanup (NULL_TREE, expr);
3138 binfos = BINFO_BASETYPES (TYPE_BINFO (current_class_type));
3139 n_baseclasses = CLASSTYPE_N_BASECLASSES (current_class_type);
3141 /* Take care of the remaining baseclasses. */
3142 for (i = 0; i < n_baseclasses; i++)
3144 tree base_binfo = TREE_VEC_ELT (binfos, i);
3145 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (BINFO_TYPE (base_binfo))
3146 || TREE_VIA_VIRTUAL (base_binfo))
3149 expr = build_special_member_call (current_class_ref,
3150 base_dtor_identifier,
3151 NULL_TREE, base_binfo,
3152 LOOKUP_NORMAL | LOOKUP_NONVIRTUAL);
3153 finish_decl_cleanup (NULL_TREE, expr);
3156 for (member = TYPE_FIELDS (current_class_type); member;
3157 member = TREE_CHAIN (member))
3159 if (TREE_CODE (member) != FIELD_DECL || DECL_ARTIFICIAL (member))
3161 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (member)))
3163 tree this_member = (build_class_member_access_expr
3164 (current_class_ref, member,
3165 /*access_path=*/NULL_TREE,
3166 /*preserve_reference=*/false));
3167 tree this_type = TREE_TYPE (member);
3168 expr = build_delete (this_type, this_member,
3169 sfk_complete_destructor,
3170 LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR|LOOKUP_NORMAL,
3172 finish_decl_cleanup (NULL_TREE, expr);
3177 /* For type TYPE, delete the virtual baseclass objects of DECL. */
3180 build_vbase_delete (tree type, tree decl)
3182 tree vbases = CLASSTYPE_VBASECLASSES (type);
3183 tree result = NULL_TREE;
3184 tree addr = build_unary_op (ADDR_EXPR, decl, 0);
3186 my_friendly_assert (addr != error_mark_node, 222);
3191 = convert_force (build_pointer_type (BINFO_TYPE (TREE_VALUE (vbases))),
3193 result = tree_cons (NULL_TREE,
3194 build_delete (TREE_TYPE (this_addr), this_addr,
3195 sfk_base_destructor,
3196 LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 0),
3198 vbases = TREE_CHAIN (vbases);
3200 return build_compound_expr (nreverse (result));
3203 /* Build a C++ vector delete expression.
3204 MAXINDEX is the number of elements to be deleted.
3205 ELT_SIZE is the nominal size of each element in the vector.
3206 BASE is the expression that should yield the store to be deleted.
3207 This function expands (or synthesizes) these calls itself.
3208 AUTO_DELETE_VEC says whether the container (vector) should be deallocated.
3210 This also calls delete for virtual baseclasses of elements of the vector.
3212 Update: MAXINDEX is no longer needed. The size can be extracted from the
3213 start of the vector for pointers, and from the type for arrays. We still
3214 use MAXINDEX for arrays because it happens to already have one of the
3215 values we'd have to extract. (We could use MAXINDEX with pointers to
3216 confirm the size, and trap if the numbers differ; not clear that it'd
3217 be worth bothering.) */
3220 build_vec_delete (tree base, tree maxindex,
3221 special_function_kind auto_delete_vec, int use_global_delete)
3225 tree base_init = NULL_TREE;
3227 type = TREE_TYPE (base);
3229 if (TREE_CODE (type) == POINTER_TYPE)
3231 /* Step back one from start of vector, and read dimension. */
3234 if (TREE_SIDE_EFFECTS (base))
3236 base_init = get_target_expr (base);
3237 base = TARGET_EXPR_SLOT (base_init);
3239 type = strip_array_types (TREE_TYPE (type));
3240 cookie_addr = build (MINUS_EXPR,
3241 build_pointer_type (sizetype),
3243 TYPE_SIZE_UNIT (sizetype));
3244 maxindex = build_indirect_ref (cookie_addr, NULL);
3246 else if (TREE_CODE (type) == ARRAY_TYPE)
3248 /* get the total number of things in the array, maxindex is a bad name */
3249 maxindex = array_type_nelts_total (type);
3250 type = strip_array_types (type);
3251 base = build_unary_op (ADDR_EXPR, base, 1);
3252 if (TREE_SIDE_EFFECTS (base))
3254 base_init = get_target_expr (base);
3255 base = TARGET_EXPR_SLOT (base_init);
3260 if (base != error_mark_node)
3261 error ("type to vector delete is neither pointer or array type");
3262 return error_mark_node;
3265 rval = build_vec_delete_1 (base, maxindex, type, auto_delete_vec,
3268 rval = build (COMPOUND_EXPR, TREE_TYPE (rval), base_init, rval);