118334Speter/* Handle initialization things in C++.
290075Sobrien   Copyright (C) 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3169689Skan   1999, 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
418334Speter   Contributed by Michael Tiemann (tiemann@cygnus.com)
518334Speter
6132718SkanThis file is part of GCC.
718334Speter
8132718SkanGCC is free software; you can redistribute it and/or modify
918334Speterit under the terms of the GNU General Public License as published by
1018334Speterthe Free Software Foundation; either version 2, or (at your option)
1118334Speterany later version.
1218334Speter
13132718SkanGCC is distributed in the hope that it will be useful,
1418334Speterbut WITHOUT ANY WARRANTY; without even the implied warranty of
1518334SpeterMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1618334SpeterGNU General Public License for more details.
1718334Speter
1818334SpeterYou should have received a copy of the GNU General Public License
19132718Skanalong with GCC; see the file COPYING.  If not, write to
20169689Skanthe Free Software Foundation, 51 Franklin Street, Fifth Floor,
21169689SkanBoston, MA 02110-1301, USA.  */
2218334Speter
2350397Sobrien/* High-level class interface.  */
2418334Speter
2518334Speter#include "config.h"
2650397Sobrien#include "system.h"
27132718Skan#include "coretypes.h"
28132718Skan#include "tm.h"
2918334Speter#include "tree.h"
3018334Speter#include "rtl.h"
3190075Sobrien#include "expr.h"
3218334Speter#include "cp-tree.h"
3318334Speter#include "flags.h"
3418334Speter#include "output.h"
3550397Sobrien#include "except.h"
3650397Sobrien#include "toplev.h"
37169689Skan#include "target.h"
3818334Speter
39132718Skanstatic bool begin_init_stmts (tree *, tree *);
40132718Skanstatic tree finish_init_stmts (bool, tree, tree);
41117395Skanstatic void construct_virtual_base (tree, tree);
42132718Skanstatic void expand_aggr_init_1 (tree, tree, tree, tree, int);
43132718Skanstatic void expand_default_init (tree, tree, tree, tree, int);
44132718Skanstatic tree build_vec_delete_1 (tree, tree, tree, special_function_kind, int);
45117395Skanstatic void perform_member_init (tree, tree);
46132718Skanstatic tree build_builtin_delete_call (tree);
47132718Skanstatic int member_init_ok_or_else (tree, tree, tree);
48132718Skanstatic void expand_virtual_init (tree, tree);
49117395Skanstatic tree sort_mem_initializers (tree, tree);
50132718Skanstatic tree initializing_context (tree);
51132718Skanstatic void expand_cleanup_for_base (tree, tree);
52132718Skanstatic tree get_temp_regvar (tree, tree);
53132718Skanstatic tree dfs_initialize_vtbl_ptrs (tree, void *);
54132718Skanstatic tree build_default_init (tree, tree);
55132718Skanstatic tree build_dtor_call (tree, special_function_kind, int);
56132718Skanstatic tree build_field_list (tree, tree, int *);
57132718Skanstatic tree build_vtbl_address (tree);
5818334Speter
5990075Sobrien/* We are about to generate some complex initialization code.
6090075Sobrien   Conceptually, it is all a single expression.  However, we may want
6190075Sobrien   to include conditionals, loops, and other such statement-level
6290075Sobrien   constructs.  Therefore, we build the initialization code inside a
6390075Sobrien   statement-expression.  This function starts such an expression.
6490075Sobrien   STMT_EXPR_P and COMPOUND_STMT_P are filled in by this function;
6590075Sobrien   pass them back to finish_init_stmts when the expression is
6690075Sobrien   complete.  */
6718334Speter
68132718Skanstatic bool
69132718Skanbegin_init_stmts (tree *stmt_expr_p, tree *compound_stmt_p)
7090075Sobrien{
71132718Skan  bool is_global = !building_stmt_tree ();
72169689Skan
73132718Skan  *stmt_expr_p = begin_stmt_expr ();
74169689Skan  *compound_stmt_p = begin_compound_stmt (BCS_NO_SCOPE);
75132718Skan
76132718Skan  return is_global;
7790075Sobrien}
7818334Speter
7990075Sobrien/* Finish out the statement-expression begun by the previous call to
8090075Sobrien   begin_init_stmts.  Returns the statement-expression itself.  */
8118334Speter
82132718Skanstatic tree
83132718Skanfinish_init_stmts (bool is_global, tree stmt_expr, tree compound_stmt)
84169689Skan{
85132718Skan  finish_compound_stmt (compound_stmt);
86169689Skan
87132718Skan  stmt_expr = finish_stmt_expr (stmt_expr, true);
88132718Skan
89169689Skan  gcc_assert (!building_stmt_tree () == is_global);
90169689Skan
9190075Sobrien  return stmt_expr;
9290075Sobrien}
9390075Sobrien
9490075Sobrien/* Constructors */
9590075Sobrien
9690075Sobrien/* Called from initialize_vtbl_ptrs via dfs_walk.  BINFO is the base
9790075Sobrien   which we want to initialize the vtable pointer for, DATA is
9890075Sobrien   TREE_LIST whose TREE_VALUE is the this ptr expression.  */
9990075Sobrien
10090075Sobrienstatic tree
101132718Skandfs_initialize_vtbl_ptrs (tree binfo, void *data)
10290075Sobrien{
103169689Skan  if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
104169689Skan    return dfs_skip_bases;
105169689Skan
106169689Skan  if (!BINFO_PRIMARY_P (binfo) || BINFO_VIRTUAL_P (binfo))
10790075Sobrien    {
10890075Sobrien      tree base_ptr = TREE_VALUE ((tree) data);
10990075Sobrien
11090075Sobrien      base_ptr = build_base_path (PLUS_EXPR, base_ptr, binfo, /*nonnull=*/1);
11190075Sobrien
11290075Sobrien      expand_virtual_init (binfo, base_ptr);
11390075Sobrien    }
11490075Sobrien
11590075Sobrien  return NULL_TREE;
11690075Sobrien}
11790075Sobrien
11890075Sobrien/* Initialize all the vtable pointers in the object pointed to by
11990075Sobrien   ADDR.  */
12090075Sobrien
12118334Spetervoid
122132718Skaninitialize_vtbl_ptrs (tree addr)
12318334Speter{
12490075Sobrien  tree list;
12590075Sobrien  tree type;
12618334Speter
12790075Sobrien  type = TREE_TYPE (TREE_TYPE (addr));
12890075Sobrien  list = build_tree_list (type, addr);
12990075Sobrien
13090075Sobrien  /* Walk through the hierarchy, initializing the vptr in each base
131117395Skan     class.  We do these in pre-order because we can't find the virtual
13290075Sobrien     bases for a class until we've initialized the vtbl for that
13390075Sobrien     class.  */
134169689Skan  dfs_walk_once (TYPE_BINFO (type), dfs_initialize_vtbl_ptrs, NULL, list);
13590075Sobrien}
13690075Sobrien
137117395Skan/* Return an expression for the zero-initialization of an object with
138117395Skan   type T.  This expression will either be a constant (in the case
139117395Skan   that T is a scalar), or a CONSTRUCTOR (in the case that T is an
140117395Skan   aggregate).  In either case, the value can be used as DECL_INITIAL
141117395Skan   for a decl of the indicated TYPE; it is a valid static initializer.
142117395Skan   If NELTS is non-NULL, and TYPE is an ARRAY_TYPE, NELTS is the
143117395Skan   number of elements in the array.  If STATIC_STORAGE_P is TRUE,
144117395Skan   initializers are only generated for entities for which
145117395Skan   zero-initialization does not simply mean filling the storage with
146117395Skan   zero bytes.  */
147102780Skan
148102780Skantree
149117395Skanbuild_zero_init (tree type, tree nelts, bool static_storage_p)
150102780Skan{
151117395Skan  tree init = NULL_TREE;
152102780Skan
153117395Skan  /* [dcl.init]
154117395Skan
155117395Skan     To zero-initialization storage for an object of type T means:
156117395Skan
157117395Skan     -- if T is a scalar type, the storage is set to the value of zero
158169689Skan	converted to T.
159117395Skan
160117395Skan     -- if T is a non-union class type, the storage for each nonstatic
161169689Skan	data member and each base-class subobject is zero-initialized.
162117395Skan
163117395Skan     -- if T is a union type, the storage for its first data member is
164169689Skan	zero-initialized.
165117395Skan
166117395Skan     -- if T is an array type, the storage for each element is
167169689Skan	zero-initialized.
168117395Skan
169117395Skan     -- if T is a reference type, no initialization is performed.  */
170117395Skan
171169689Skan  gcc_assert (nelts == NULL_TREE || TREE_CODE (nelts) == INTEGER_CST);
172117395Skan
173117395Skan  if (type == error_mark_node)
174117395Skan    ;
175117395Skan  else if (static_storage_p && zero_init_p (type))
176117395Skan    /* In order to save space, we do not explicitly build initializers
177117395Skan       for items that do not need them.  GCC's semantics are that
178117395Skan       items with static storage duration that are not otherwise
179117395Skan       initialized are initialized to zero.  */
180117395Skan    ;
181171825Skan  else if (SCALAR_TYPE_P (type))
182117395Skan    init = convert (type, integer_zero_node);
183117395Skan  else if (CLASS_TYPE_P (type))
184102780Skan    {
185117395Skan      tree field;
186169689Skan      VEC(constructor_elt,gc) *v = NULL;
187117395Skan
188117395Skan      /* Iterate over the fields, building initializations.  */
189117395Skan      for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
190117395Skan	{
191117395Skan	  if (TREE_CODE (field) != FIELD_DECL)
192117395Skan	    continue;
193117395Skan
194117395Skan	  /* Note that for class types there will be FIELD_DECLs
195117395Skan	     corresponding to base classes as well.  Thus, iterating
196117395Skan	     over TYPE_FIELDs will result in correct initialization of
197117395Skan	     all of the subobjects.  */
198171825Skan	  if (!static_storage_p || !zero_init_p (TREE_TYPE (field)))
199169689Skan	    {
200169689Skan	      tree value = build_zero_init (TREE_TYPE (field),
201169689Skan					    /*nelts=*/NULL_TREE,
202169689Skan					    static_storage_p);
203169689Skan	      CONSTRUCTOR_APPEND_ELT(v, field, value);
204169689Skan	    }
205117395Skan
206117395Skan	  /* For unions, only the first field is initialized.  */
207117395Skan	  if (TREE_CODE (type) == UNION_TYPE)
208117395Skan	    break;
209117395Skan	}
210169689Skan
211169689Skan	/* Build a constructor to contain the initializations.  */
212169689Skan	init = build_constructor (type, v);
213102780Skan    }
214117395Skan  else if (TREE_CODE (type) == ARRAY_TYPE)
215117395Skan    {
216117395Skan      tree max_index;
217169689Skan      VEC(constructor_elt,gc) *v = NULL;
218117395Skan
219117395Skan      /* Iterate over the array elements, building initializations.  */
220169689Skan      if (nelts)
221169689Skan	max_index = fold_build2 (MINUS_EXPR, TREE_TYPE (nelts),
222169689Skan				 nelts, integer_one_node);
223169689Skan      else
224169689Skan	max_index = array_type_nelts (type);
225117395Skan
226169689Skan      /* If we have an error_mark here, we should just return error mark
227169689Skan	 as we don't know the size of the array yet.  */
228169689Skan      if (max_index == error_mark_node)
229169689Skan	return error_mark_node;
230169689Skan      gcc_assert (TREE_CODE (max_index) == INTEGER_CST);
231169689Skan
232132718Skan      /* A zero-sized array, which is accepted as an extension, will
233132718Skan	 have an upper bound of -1.  */
234132718Skan      if (!tree_int_cst_equal (max_index, integer_minus_one_node))
235146895Skan	{
236169689Skan	  constructor_elt *ce;
237169689Skan
238169689Skan	  v = VEC_alloc (constructor_elt, gc, 1);
239169689Skan	  ce = VEC_quick_push (constructor_elt, v, NULL);
240169689Skan
241169689Skan	  /* If this is a one element array, we just use a regular init.  */
242169689Skan	  if (tree_int_cst_equal (size_zero_node, max_index))
243169689Skan	    ce->index = size_zero_node;
244169689Skan	  else
245169689Skan	    ce->index = build2 (RANGE_EXPR, sizetype, size_zero_node,
246169689Skan				max_index);
247169689Skan
248169689Skan	  ce->value = build_zero_init (TREE_TYPE (type),
249169689Skan				       /*nelts=*/NULL_TREE,
250169689Skan				       static_storage_p);
251146895Skan	}
252169689Skan
253169689Skan      /* Build a constructor to contain the initializations.  */
254169689Skan      init = build_constructor (type, v);
255117395Skan    }
256169689Skan  else if (TREE_CODE (type) == VECTOR_TYPE)
257169689Skan    init = fold_convert (type, integer_zero_node);
258102780Skan  else
259169689Skan    gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
260102780Skan
261117395Skan  /* In all cases, the initializer is a constant.  */
262117395Skan  if (init)
263169689Skan    {
264169689Skan      TREE_CONSTANT (init) = 1;
265169689Skan      TREE_INVARIANT (init) = 1;
266169689Skan    }
267102780Skan
268102780Skan  return init;
269102780Skan}
270102780Skan
271117395Skan/* Build an expression for the default-initialization of an object of
272117395Skan   the indicated TYPE.  If NELTS is non-NULL, and TYPE is an
273117395Skan   ARRAY_TYPE, NELTS is the number of elements in the array.  If
274117395Skan   initialization of TYPE requires calling constructors, this function
275117395Skan   returns NULL_TREE; the caller is responsible for arranging for the
276117395Skan   constructors to be called.  */
27790075Sobrien
278117395Skanstatic tree
279132718Skanbuild_default_init (tree type, tree nelts)
280117395Skan{
281117395Skan  /* [dcl.init]:
28290075Sobrien
283117395Skan    To default-initialize an object of type T means:
28490075Sobrien
285117395Skan    --if T is a non-POD class type (clause _class_), the default construc-
286117395Skan      tor  for  T is called (and the initialization is ill-formed if T has
287117395Skan      no accessible default constructor);
28890075Sobrien
289117395Skan    --if T is an array type, each element is default-initialized;
29090075Sobrien
291117395Skan    --otherwise, the storage for the object is zero-initialized.
29290075Sobrien
293117395Skan    A program that calls for default-initialization of an entity of refer-
294117395Skan    ence type is ill-formed.  */
29590075Sobrien
296117395Skan  /* If TYPE_NEEDS_CONSTRUCTING is true, the caller is responsible for
297117395Skan     performing the initialization.  This is confusing in that some
298117395Skan     non-PODs do not have TYPE_NEEDS_CONSTRUCTING set.  (For example,
299117395Skan     a class with a pointer-to-data member as a non-static data member
300117395Skan     does not have TYPE_NEEDS_CONSTRUCTING set.)  Therefore, we end up
301117395Skan     passing non-PODs to build_zero_init below, which is contrary to
302169689Skan     the semantics quoted above from [dcl.init].
303117395Skan
304117395Skan     It happens, however, that the behavior of the constructor the
305117395Skan     standard says we should have generated would be precisely the
306117395Skan     same as that obtained by calling build_zero_init below, so things
307117395Skan     work out OK.  */
308117395Skan  if (TYPE_NEEDS_CONSTRUCTING (type)
309117395Skan      || (nelts && TREE_CODE (nelts) != INTEGER_CST))
31090075Sobrien    return NULL_TREE;
311169689Skan
312117395Skan  /* At this point, TYPE is either a POD class type, an array of POD
313132718Skan     classes, or something even more innocuous.  */
314117395Skan  return build_zero_init (type, nelts, /*static_storage_p=*/false);
31518334Speter}
31690075Sobrien
317117395Skan/* Initialize MEMBER, a FIELD_DECL, with INIT, a TREE_LIST of
318117395Skan   arguments.  If TREE_LIST is void_type_node, an empty initializer
319117395Skan   list was given; if NULL_TREE no initializer was given.  */
32050397Sobrien
32118334Speterstatic void
322117395Skanperform_member_init (tree member, tree init)
32318334Speter{
32418334Speter  tree decl;
32518334Speter  tree type = TREE_TYPE (member);
326117395Skan  bool explicit;
32718334Speter
328117395Skan  explicit = (init != NULL_TREE);
32950397Sobrien
330117395Skan  /* Effective C++ rule 12 requires that all data members be
331117395Skan     initialized.  */
332117395Skan  if (warn_ecpp && !explicit && TREE_CODE (type) != ARRAY_TYPE)
333169689Skan    warning (OPT_Weffc__, "%J%qD should be initialized in the member initialization "
334169689Skan	     "list", current_function_decl, member);
335117395Skan
336117395Skan  if (init == void_type_node)
337117395Skan    init = NULL_TREE;
338117395Skan
339117395Skan  /* Get an lvalue for the data member.  */
340117395Skan  decl = build_class_member_access_expr (current_class_ref, member,
341117395Skan					 /*access_path=*/NULL_TREE,
342117395Skan					 /*preserve_reference=*/true);
34390075Sobrien  if (decl == error_mark_node)
34490075Sobrien    return;
34590075Sobrien
34690075Sobrien  /* Deal with this here, as we will get confused if we try to call the
34790075Sobrien     assignment op for an anonymous union.  This can happen in a
34890075Sobrien     synthesized copy constructor.  */
34990075Sobrien  if (ANON_AGGR_TYPE_P (type))
35018334Speter    {
35190075Sobrien      if (init)
35290075Sobrien	{
353169689Skan	  init = build2 (INIT_EXPR, type, decl, TREE_VALUE (init));
35490075Sobrien	  finish_expr_stmt (init);
35590075Sobrien	}
35690075Sobrien    }
357169689Skan  else if (TYPE_NEEDS_CONSTRUCTING (type))
35890075Sobrien    {
35918334Speter      if (explicit
36018334Speter	  && TREE_CODE (type) == ARRAY_TYPE
36118334Speter	  && init != NULL_TREE
36218334Speter	  && TREE_CHAIN (init) == NULL_TREE
36318334Speter	  && TREE_CODE (TREE_TYPE (TREE_VALUE (init))) == ARRAY_TYPE)
36418334Speter	{
36518334Speter	  /* Initialization of one array from another.  */
366117395Skan	  finish_expr_stmt (build_vec_init (decl, NULL_TREE, TREE_VALUE (init),
367169689Skan					    /*explicit_default_init_p=*/false,
368117395Skan					    /* from_array=*/1));
36918334Speter	}
37018334Speter      else
37190075Sobrien	finish_expr_stmt (build_aggr_init (decl, init, 0));
37218334Speter    }
37318334Speter  else
37418334Speter    {
37518334Speter      if (init == NULL_TREE)
37618334Speter	{
37718334Speter	  if (explicit)
37818334Speter	    {
379117395Skan	      init = build_default_init (type, /*nelts=*/NULL_TREE);
38090075Sobrien	      if (TREE_CODE (type) == REFERENCE_TYPE)
381169689Skan		warning (0, "%Jdefault-initialization of %q#D, "
382169689Skan			 "which has reference type",
383169689Skan			 current_function_decl, member);
38418334Speter	    }
38518334Speter	  /* member traversal: note it leaves init NULL */
38690075Sobrien	  else if (TREE_CODE (type) == REFERENCE_TYPE)
387169689Skan	    pedwarn ("%Juninitialized reference member %qD",
388169689Skan		     current_function_decl, member);
389132718Skan	  else if (CP_TYPE_CONST_P (type))
390169689Skan	    pedwarn ("%Juninitialized member %qD with %<const%> type %qT",
391169689Skan		     current_function_decl, member, type);
39218334Speter	}
39318334Speter      else if (TREE_CODE (init) == TREE_LIST)
394132718Skan	/* There was an explicit member initialization.  Do some work
395132718Skan	   in that case.  */
396132718Skan	init = build_x_compound_expr_from_list (init, "member initializer");
39718334Speter
39890075Sobrien      if (init)
39990075Sobrien	finish_expr_stmt (build_modify_expr (decl, INIT_EXPR, init));
40018334Speter    }
40118334Speter
40290075Sobrien  if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
40318334Speter    {
40450397Sobrien      tree expr;
40550397Sobrien
406117395Skan      expr = build_class_member_access_expr (current_class_ref, member,
407117395Skan					     /*access_path=*/NULL_TREE,
408117395Skan					     /*preserve_reference=*/false);
40990075Sobrien      expr = build_delete (type, expr, sfk_complete_destructor,
41018334Speter			   LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR, 0);
41118334Speter
41218334Speter      if (expr != error_mark_node)
413117395Skan	finish_eh_cleanup (expr);
41490075Sobrien    }
41590075Sobrien}
41650397Sobrien
41790075Sobrien/* Returns a TREE_LIST containing (as the TREE_PURPOSE of each node) all
41890075Sobrien   the FIELD_DECLs on the TYPE_FIELDS list for T, in reverse order.  */
41990075Sobrien
420169689Skanstatic tree
421132718Skanbuild_field_list (tree t, tree list, int *uses_unions_p)
42290075Sobrien{
42390075Sobrien  tree fields;
42490075Sobrien
425107590Sobrien  *uses_unions_p = 0;
426107590Sobrien
42790075Sobrien  /* Note whether or not T is a union.  */
42890075Sobrien  if (TREE_CODE (t) == UNION_TYPE)
42990075Sobrien    *uses_unions_p = 1;
43090075Sobrien
43190075Sobrien  for (fields = TYPE_FIELDS (t); fields; fields = TREE_CHAIN (fields))
43290075Sobrien    {
43390075Sobrien      /* Skip CONST_DECLs for enumeration constants and so forth.  */
434117395Skan      if (TREE_CODE (fields) != FIELD_DECL || DECL_ARTIFICIAL (fields))
43590075Sobrien	continue;
436169689Skan
43790075Sobrien      /* Keep track of whether or not any fields are unions.  */
43890075Sobrien      if (TREE_CODE (TREE_TYPE (fields)) == UNION_TYPE)
43990075Sobrien	*uses_unions_p = 1;
44090075Sobrien
44190075Sobrien      /* For an anonymous struct or union, we must recursively
44290075Sobrien	 consider the fields of the anonymous type.  They can be
44390075Sobrien	 directly initialized from the constructor.  */
44490075Sobrien      if (ANON_AGGR_TYPE_P (TREE_TYPE (fields)))
44590075Sobrien	{
44690075Sobrien	  /* Add this field itself.  Synthesized copy constructors
44790075Sobrien	     initialize the entire aggregate.  */
44890075Sobrien	  list = tree_cons (fields, NULL_TREE, list);
44990075Sobrien	  /* And now add the fields in the anonymous aggregate.  */
450169689Skan	  list = build_field_list (TREE_TYPE (fields), list,
45190075Sobrien				   uses_unions_p);
45290075Sobrien	}
45390075Sobrien      /* Add this field.  */
45490075Sobrien      else if (DECL_NAME (fields))
45590075Sobrien	list = tree_cons (fields, NULL_TREE, list);
45618334Speter    }
45790075Sobrien
45890075Sobrien  return list;
45918334Speter}
46018334Speter
461117395Skan/* The MEM_INITS are a TREE_LIST.  The TREE_PURPOSE of each list gives
462117395Skan   a FIELD_DECL or BINFO in T that needs initialization.  The
463117395Skan   TREE_VALUE gives the initializer, or list of initializer arguments.
46418334Speter
465117395Skan   Return a TREE_LIST containing all of the initializations required
466117395Skan   for T, in the order in which they should be performed.  The output
467117395Skan   list has the same format as the input.  */
468117395Skan
46918334Speterstatic tree
470117395Skansort_mem_initializers (tree t, tree mem_inits)
47118334Speter{
47290075Sobrien  tree init;
473169689Skan  tree base, binfo, base_binfo;
474117395Skan  tree sorted_inits;
475117395Skan  tree next_subobject;
476169689Skan  VEC(tree,gc) *vbases;
477117395Skan  int i;
47890075Sobrien  int uses_unions_p;
47918334Speter
480117395Skan  /* Build up a list of initializations.  The TREE_PURPOSE of entry
481117395Skan     will be the subobject (a FIELD_DECL or BINFO) to initialize.  The
482117395Skan     TREE_VALUE will be the constructor arguments, or NULL if no
483117395Skan     explicit initialization was provided.  */
484117395Skan  sorted_inits = NULL_TREE;
485169689Skan
486117395Skan  /* Process the virtual bases.  */
487169689Skan  for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
488169689Skan       VEC_iterate (tree, vbases, i, base); i++)
489169689Skan    sorted_inits = tree_cons (base, NULL_TREE, sorted_inits);
490169689Skan
491117395Skan  /* Process the direct bases.  */
492169689Skan  for (binfo = TYPE_BINFO (t), i = 0;
493169689Skan       BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
494169689Skan    if (!BINFO_VIRTUAL_P (base_binfo))
495169689Skan      sorted_inits = tree_cons (base_binfo, NULL_TREE, sorted_inits);
496169689Skan
497117395Skan  /* Process the non-static data members.  */
498117395Skan  sorted_inits = build_field_list (t, sorted_inits, &uses_unions_p);
499117395Skan  /* Reverse the entire list of initializations, so that they are in
500117395Skan     the order that they will actually be performed.  */
501117395Skan  sorted_inits = nreverse (sorted_inits);
50290075Sobrien
503117395Skan  /* If the user presented the initializers in an order different from
504117395Skan     that in which they will actually occur, we issue a warning.  Keep
505117395Skan     track of the next subobject which can be explicitly initialized
506117395Skan     without issuing a warning.  */
507117395Skan  next_subobject = sorted_inits;
508117395Skan
509117395Skan  /* Go through the explicit initializers, filling in TREE_PURPOSE in
510117395Skan     the SORTED_INITS.  */
511117395Skan  for (init = mem_inits; init; init = TREE_CHAIN (init))
51218334Speter    {
513117395Skan      tree subobject;
514117395Skan      tree subobject_init;
51518334Speter
516117395Skan      subobject = TREE_PURPOSE (init);
51718334Speter
518117395Skan      /* If the explicit initializers are in sorted order, then
519169689Skan	 SUBOBJECT will be NEXT_SUBOBJECT, or something following
520117395Skan	 it.  */
521169689Skan      for (subobject_init = next_subobject;
522169689Skan	   subobject_init;
523117395Skan	   subobject_init = TREE_CHAIN (subobject_init))
524117395Skan	if (TREE_PURPOSE (subobject_init) == subobject)
52590075Sobrien	  break;
52690075Sobrien
527117395Skan      /* Issue a warning if the explicit initializer order does not
528169689Skan	 match that which will actually occur.
529169689Skan	 ??? Are all these on the correct lines?  */
530117395Skan      if (warn_reorder && !subobject_init)
53118334Speter	{
532117395Skan	  if (TREE_CODE (TREE_PURPOSE (next_subobject)) == FIELD_DECL)
533169689Skan	    warning (OPT_Wreorder, "%q+D will be initialized after",
534169689Skan		     TREE_PURPOSE (next_subobject));
535117395Skan	  else
536169689Skan	    warning (OPT_Wreorder, "base %qT will be initialized after",
537117395Skan		     TREE_PURPOSE (next_subobject));
538117395Skan	  if (TREE_CODE (subobject) == FIELD_DECL)
539169689Skan	    warning (OPT_Wreorder, "  %q+#D", subobject);
540117395Skan	  else
541169689Skan	    warning (OPT_Wreorder, "  base %qT", subobject);
542169689Skan	  warning (OPT_Wreorder, "%J  when initialized here", current_function_decl);
54390075Sobrien	}
54490075Sobrien
545117395Skan      /* Look again, from the beginning of the list.  */
546117395Skan      if (!subobject_init)
54790075Sobrien	{
548117395Skan	  subobject_init = sorted_inits;
549117395Skan	  while (TREE_PURPOSE (subobject_init) != subobject)
550117395Skan	    subobject_init = TREE_CHAIN (subobject_init);
55190075Sobrien	}
552169689Skan
553117395Skan      /* It is invalid to initialize the same subobject more than
554117395Skan	 once.  */
555117395Skan      if (TREE_VALUE (subobject_init))
55690075Sobrien	{
557117395Skan	  if (TREE_CODE (subobject) == FIELD_DECL)
558169689Skan	    error ("%Jmultiple initializations given for %qD",
559169689Skan		   current_function_decl, subobject);
560117395Skan	  else
561169689Skan	    error ("%Jmultiple initializations given for base %qT",
562169689Skan		   current_function_decl, subobject);
56390075Sobrien	}
56490075Sobrien
565117395Skan      /* Record the initialization.  */
566117395Skan      TREE_VALUE (subobject_init) = TREE_VALUE (init);
567117395Skan      next_subobject = subobject_init;
56890075Sobrien    }
56990075Sobrien
57090075Sobrien  /* [class.base.init]
57190075Sobrien
57290075Sobrien     If a ctor-initializer specifies more than one mem-initializer for
57390075Sobrien     multiple members of the same union (including members of
57490075Sobrien     anonymous unions), the ctor-initializer is ill-formed.  */
57590075Sobrien  if (uses_unions_p)
57690075Sobrien    {
577117395Skan      tree last_field = NULL_TREE;
578117395Skan      for (init = sorted_inits; init; init = TREE_CHAIN (init))
57990075Sobrien	{
58090075Sobrien	  tree field;
58190075Sobrien	  tree field_type;
58290075Sobrien	  int done;
58390075Sobrien
584117395Skan	  /* Skip uninitialized members and base classes.  */
585169689Skan	  if (!TREE_VALUE (init)
586117395Skan	      || TREE_CODE (TREE_PURPOSE (init)) != FIELD_DECL)
58718334Speter	    continue;
58890075Sobrien	  /* See if this field is a member of a union, or a member of a
58990075Sobrien	     structure contained in a union, etc.  */
59090075Sobrien	  field = TREE_PURPOSE (init);
59190075Sobrien	  for (field_type = DECL_CONTEXT (field);
59290075Sobrien	       !same_type_p (field_type, t);
59390075Sobrien	       field_type = TYPE_CONTEXT (field_type))
59490075Sobrien	    if (TREE_CODE (field_type) == UNION_TYPE)
59590075Sobrien	      break;
59690075Sobrien	  /* If this field is not a member of a union, skip it.  */
59790075Sobrien	  if (TREE_CODE (field_type) != UNION_TYPE)
59890075Sobrien	    continue;
59918334Speter
60090075Sobrien	  /* It's only an error if we have two initializers for the same
60190075Sobrien	     union type.  */
60290075Sobrien	  if (!last_field)
60390075Sobrien	    {
60490075Sobrien	      last_field = field;
60590075Sobrien	      continue;
60690075Sobrien	    }
60718334Speter
60890075Sobrien	  /* See if LAST_FIELD and the field initialized by INIT are
60990075Sobrien	     members of the same union.  If so, there's a problem,
61090075Sobrien	     unless they're actually members of the same structure
61190075Sobrien	     which is itself a member of a union.  For example, given:
61218334Speter
61390075Sobrien	       union { struct { int i; int j; }; };
61490075Sobrien
61590075Sobrien	     initializing both `i' and `j' makes sense.  */
61690075Sobrien	  field_type = DECL_CONTEXT (field);
61790075Sobrien	  done = 0;
61890075Sobrien	  do
61918334Speter	    {
62090075Sobrien	      tree last_field_type;
62190075Sobrien
62290075Sobrien	      last_field_type = DECL_CONTEXT (last_field);
62390075Sobrien	      while (1)
62418334Speter		{
62590075Sobrien		  if (same_type_p (last_field_type, field_type))
62618334Speter		    {
62790075Sobrien		      if (TREE_CODE (field_type) == UNION_TYPE)
628169689Skan			error ("%Jinitializations for multiple members of %qT",
629169689Skan			       current_function_decl, last_field_type);
63090075Sobrien		      done = 1;
63190075Sobrien		      break;
63218334Speter		    }
63390075Sobrien
63490075Sobrien		  if (same_type_p (last_field_type, t))
63590075Sobrien		    break;
63690075Sobrien
63790075Sobrien		  last_field_type = TYPE_CONTEXT (last_field_type);
63818334Speter		}
639169689Skan
64090075Sobrien	      /* If we've reached the outermost class, then we're
64190075Sobrien		 done.  */
64290075Sobrien	      if (same_type_p (field_type, t))
64390075Sobrien		break;
64418334Speter
64590075Sobrien	      field_type = TYPE_CONTEXT (field_type);
64618334Speter	    }
64790075Sobrien	  while (!done);
64818334Speter
64990075Sobrien	  last_field = field;
65018334Speter	}
65118334Speter    }
65218334Speter
653117395Skan  return sorted_inits;
65418334Speter}
65518334Speter
656117395Skan/* Initialize all bases and members of CURRENT_CLASS_TYPE.  MEM_INITS
657117395Skan   is a TREE_LIST giving the explicit mem-initializer-list for the
658117395Skan   constructor.  The TREE_PURPOSE of each entry is a subobject (a
659117395Skan   FIELD_DECL or a BINFO) of the CURRENT_CLASS_TYPE.  The TREE_VALUE
660117395Skan   is a TREE_LIST giving the arguments to the constructor or
661117395Skan   void_type_node for an empty list of arguments.  */
66290075Sobrien
66318334Spetervoid
664117395Skanemit_mem_initializers (tree mem_inits)
66518334Speter{
666169689Skan  /* We will already have issued an error message about the fact that
667169689Skan     the type is incomplete.  */
668169689Skan  if (!COMPLETE_TYPE_P (current_class_type))
669169689Skan    return;
670169689Skan
671117395Skan  /* Sort the mem-initializers into the order in which the
672117395Skan     initializations should be performed.  */
673117395Skan  mem_inits = sort_mem_initializers (current_class_type, mem_inits);
67418334Speter
675117395Skan  in_base_initializer = 1;
676169689Skan
677117395Skan  /* Initialize base classes.  */
678169689Skan  while (mem_inits
679117395Skan	 && TREE_CODE (TREE_PURPOSE (mem_inits)) != FIELD_DECL)
68018334Speter    {
681117395Skan      tree subobject = TREE_PURPOSE (mem_inits);
682117395Skan      tree arguments = TREE_VALUE (mem_inits);
68318334Speter
684117395Skan      /* If these initializations are taking place in a copy
685117395Skan	 constructor, the base class should probably be explicitly
686117395Skan	 initialized.  */
687169689Skan      if (extra_warnings && !arguments
688117395Skan	  && DECL_COPY_CONSTRUCTOR_P (current_function_decl)
689117395Skan	  && TYPE_NEEDS_CONSTRUCTING (BINFO_TYPE (subobject)))
690169689Skan	warning (OPT_Wextra, "%Jbase class %q#T should be explicitly initialized in the "
691117395Skan		 "copy constructor",
692169689Skan		 current_function_decl, BINFO_TYPE (subobject));
69318334Speter
694117395Skan      /* If an explicit -- but empty -- initializer list was present,
695117395Skan	 treat it just like default initialization at this point.  */
696117395Skan      if (arguments == void_type_node)
697117395Skan	arguments = NULL_TREE;
69818334Speter
699117395Skan      /* Initialize the base.  */
700169689Skan      if (BINFO_VIRTUAL_P (subobject))
701117395Skan	construct_virtual_base (subobject, arguments);
702117395Skan      else
70350397Sobrien	{
704117395Skan	  tree base_addr;
705169689Skan
706117395Skan	  base_addr = build_base_path (PLUS_EXPR, current_class_ptr,
707117395Skan				       subobject, 1);
708117395Skan	  expand_aggr_init_1 (subobject, NULL_TREE,
709169689Skan			      build_indirect_ref (base_addr, NULL),
710117395Skan			      arguments,
71152284Sobrien			      LOOKUP_NORMAL);
712117395Skan	  expand_cleanup_for_base (subobject, NULL_TREE);
71318334Speter	}
71418334Speter
715117395Skan      mem_inits = TREE_CHAIN (mem_inits);
71618334Speter    }
717117395Skan  in_base_initializer = 0;
71818334Speter
719117395Skan  /* Initialize the vptrs.  */
72090075Sobrien  initialize_vtbl_ptrs (current_class_ptr);
721169689Skan
722117395Skan  /* Initialize the data members.  */
723117395Skan  while (mem_inits)
72418334Speter    {
725117395Skan      perform_member_init (TREE_PURPOSE (mem_inits),
726117395Skan			   TREE_VALUE (mem_inits));
727117395Skan      mem_inits = TREE_CHAIN (mem_inits);
72818334Speter    }
72990075Sobrien}
73018334Speter
73190075Sobrien/* Returns the address of the vtable (i.e., the value that should be
73290075Sobrien   assigned to the vptr) for BINFO.  */
73318334Speter
73490075Sobrienstatic tree
735132718Skanbuild_vtbl_address (tree binfo)
73690075Sobrien{
73790075Sobrien  tree binfo_for = binfo;
73890075Sobrien  tree vtbl;
73918334Speter
740169689Skan  if (BINFO_VPTR_INDEX (binfo) && BINFO_VIRTUAL_P (binfo))
74190075Sobrien    /* If this is a virtual primary base, then the vtable we want to store
74290075Sobrien       is that for the base this is being used as the primary base of.  We
74390075Sobrien       can't simply skip the initialization, because we may be expanding the
74490075Sobrien       inits of a subobject constructor where the virtual base layout
74590075Sobrien       can be different.  */
746169689Skan    while (BINFO_PRIMARY_P (binfo_for))
747169689Skan      binfo_for = BINFO_INHERITANCE_CHAIN (binfo_for);
74818334Speter
74990075Sobrien  /* Figure out what vtable BINFO's vtable is based on, and mark it as
75090075Sobrien     used.  */
75190075Sobrien  vtbl = get_vtbl_decl_for_binfo (binfo_for);
75290075Sobrien  assemble_external (vtbl);
75390075Sobrien  TREE_USED (vtbl) = 1;
75418334Speter
75590075Sobrien  /* Now compute the address to use when initializing the vptr.  */
756169689Skan  vtbl = unshare_expr (BINFO_VTABLE (binfo_for));
75790075Sobrien  if (TREE_CODE (vtbl) == VAR_DECL)
758169689Skan    vtbl = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (vtbl)), vtbl);
75918334Speter
76090075Sobrien  return vtbl;
76118334Speter}
76218334Speter
76318334Speter/* This code sets up the virtual function tables appropriate for
76418334Speter   the pointer DECL.  It is a one-ply initialization.
76518334Speter
76618334Speter   BINFO is the exact type that DECL is supposed to be.  In
76718334Speter   multiple inheritance, this might mean "C's A" if C : A, B.  */
76850397Sobrien
76918334Speterstatic void
770132718Skanexpand_virtual_init (tree binfo, tree decl)
77118334Speter{
77218334Speter  tree vtbl, vtbl_ptr;
77390075Sobrien  tree vtt_index;
77418334Speter
77590075Sobrien  /* Compute the initializer for vptr.  */
77690075Sobrien  vtbl = build_vtbl_address (binfo);
77718334Speter
77890075Sobrien  /* We may get this vptr from a VTT, if this is a subobject
77990075Sobrien     constructor or subobject destructor.  */
78090075Sobrien  vtt_index = BINFO_VPTR_INDEX (binfo);
78190075Sobrien  if (vtt_index)
78290075Sobrien    {
78390075Sobrien      tree vtbl2;
78490075Sobrien      tree vtt_parm;
78590075Sobrien
78690075Sobrien      /* Compute the value to use, when there's a VTT.  */
78790075Sobrien      vtt_parm = current_vtt_parm;
788169689Skan      vtbl2 = build2 (PLUS_EXPR,
789169689Skan		      TREE_TYPE (vtt_parm),
790169689Skan		      vtt_parm,
791169689Skan		      vtt_index);
792169689Skan      vtbl2 = build_indirect_ref (vtbl2, NULL);
793169689Skan      vtbl2 = convert (TREE_TYPE (vtbl), vtbl2);
79490075Sobrien
79590075Sobrien      /* The actual initializer is the VTT value only in the subobject
79690075Sobrien	 constructor.  In maybe_clone_body we'll substitute NULL for
79790075Sobrien	 the vtt_parm in the case of the non-subobject constructor.  */
798169689Skan      vtbl = build3 (COND_EXPR,
799169689Skan		     TREE_TYPE (vtbl),
800169689Skan		     build2 (EQ_EXPR, boolean_type_node,
801169689Skan			     current_in_charge_parm, integer_zero_node),
802169689Skan		     vtbl2,
803169689Skan		     vtbl);
80490075Sobrien    }
80590075Sobrien
80690075Sobrien  /* Compute the location of the vtpr.  */
80790075Sobrien  vtbl_ptr = build_vfield_ref (build_indirect_ref (decl, NULL),
80890075Sobrien			       TREE_TYPE (binfo));
809169689Skan  gcc_assert (vtbl_ptr != error_mark_node);
81090075Sobrien
81190075Sobrien  /* Assign the vtable to the vptr.  */
81218334Speter  vtbl = convert_force (TREE_TYPE (vtbl_ptr), vtbl, 0);
81390075Sobrien  finish_expr_stmt (build_modify_expr (vtbl_ptr, NOP_EXPR, vtbl));
81418334Speter}
81518334Speter
81652284Sobrien/* If an exception is thrown in a constructor, those base classes already
81752284Sobrien   constructed must be destroyed.  This function creates the cleanup
81852284Sobrien   for BINFO, which has just been constructed.  If FLAG is non-NULL,
819117395Skan   it is a DECL which is nonzero when this base needs to be
82052284Sobrien   destroyed.  */
82152284Sobrien
82252284Sobrienstatic void
823132718Skanexpand_cleanup_for_base (tree binfo, tree flag)
82452284Sobrien{
82552284Sobrien  tree expr;
82652284Sobrien
82790075Sobrien  if (TYPE_HAS_TRIVIAL_DESTRUCTOR (BINFO_TYPE (binfo)))
82890075Sobrien    return;
82952284Sobrien
83090075Sobrien  /* Call the destructor.  */
831169689Skan  expr = build_special_member_call (current_class_ref,
832117395Skan				    base_dtor_identifier,
833117395Skan				    NULL_TREE,
834117395Skan				    binfo,
835117395Skan				    LOOKUP_NORMAL | LOOKUP_NONVIRTUAL);
83690075Sobrien  if (flag)
837169689Skan    expr = fold_build3 (COND_EXPR, void_type_node,
838117395Skan			c_common_truthvalue_conversion (flag),
839169689Skan			expr, integer_zero_node);
84052284Sobrien
841117395Skan  finish_eh_cleanup (expr);
84252284Sobrien}
84352284Sobrien
844117395Skan/* Construct the virtual base-class VBASE passing the ARGUMENTS to its
845117395Skan   constructor.  */
84650397Sobrien
84718334Speterstatic void
848117395Skanconstruct_virtual_base (tree vbase, tree arguments)
84918334Speter{
850117395Skan  tree inner_if_stmt;
851117395Skan  tree exp;
852169689Skan  tree flag;
85350397Sobrien
854117395Skan  /* If there are virtual base classes with destructors, we need to
855117395Skan     emit cleanups to destroy them if an exception is thrown during
856117395Skan     the construction process.  These exception regions (i.e., the
857117395Skan     period during which the cleanups must occur) begin from the time
858117395Skan     the construction is complete to the end of the function.  If we
859117395Skan     create a conditional block in which to initialize the
860117395Skan     base-classes, then the cleanup region for the virtual base begins
861117395Skan     inside a block, and ends outside of that block.  This situation
862117395Skan     confuses the sjlj exception-handling code.  Therefore, we do not
863117395Skan     create a single conditional block, but one for each
864117395Skan     initialization.  (That way the cleanup regions always begin
865117395Skan     in the outer block.)  We trust the back-end to figure out
866117395Skan     that the FLAG will not change across initializations, and
867117395Skan     avoid doing multiple tests.  */
868117395Skan  flag = TREE_CHAIN (DECL_ARGUMENTS (current_function_decl));
869117395Skan  inner_if_stmt = begin_if_stmt ();
870117395Skan  finish_if_stmt_cond (flag, inner_if_stmt);
87118334Speter
872117395Skan  /* Compute the location of the virtual base.  If we're
873117395Skan     constructing virtual bases, then we must be the most derived
874117395Skan     class.  Therefore, we don't have to look up the virtual base;
875117395Skan     we already know where it is.  */
876119256Skan  exp = convert_to_base_statically (current_class_ref, vbase);
87718334Speter
878169689Skan  expand_aggr_init_1 (vbase, current_class_ref, exp, arguments,
879119256Skan		      LOOKUP_COMPLAIN);
880117395Skan  finish_then_clause (inner_if_stmt);
881169689Skan  finish_if_stmt (inner_if_stmt);
88218334Speter
883117395Skan  expand_cleanup_for_base (vbase, flag);
88418334Speter}
88518334Speter
88650397Sobrien/* Find the context in which this FIELD can be initialized.  */
88750397Sobrien
88850397Sobrienstatic tree
889132718Skaninitializing_context (tree field)
89018334Speter{
89150397Sobrien  tree t = DECL_CONTEXT (field);
89218334Speter
89350397Sobrien  /* Anonymous union members can be initialized in the first enclosing
89450397Sobrien     non-anonymous union context.  */
89590075Sobrien  while (t && ANON_AGGR_TYPE_P (t))
89650397Sobrien    t = TYPE_CONTEXT (t);
89750397Sobrien  return t;
89818334Speter}
89918334Speter
90018334Speter/* Function to give error message if member initialization specification
90118334Speter   is erroneous.  FIELD is the member we decided to initialize.
90218334Speter   TYPE is the type for which the initialization is being performed.
90350397Sobrien   FIELD must be a member of TYPE.
904169689Skan
90518334Speter   MEMBER_NAME is the name of the member.  */
90618334Speter
90718334Speterstatic int
908132718Skanmember_init_ok_or_else (tree field, tree type, tree member_name)
90918334Speter{
91018334Speter  if (field == error_mark_node)
91118334Speter    return 0;
912132718Skan  if (!field)
91318334Speter    {
914169689Skan      error ("class %qT does not have any field named %qD", type,
915132718Skan	     member_name);
91618334Speter      return 0;
91718334Speter    }
918132718Skan  if (TREE_CODE (field) == VAR_DECL)
91918334Speter    {
920169689Skan      error ("%q#D is a static data member; it can only be "
921132718Skan	     "initialized at its definition",
922132718Skan	     field);
92318334Speter      return 0;
92418334Speter    }
925132718Skan  if (TREE_CODE (field) != FIELD_DECL)
926132718Skan    {
927169689Skan      error ("%q#D is not a non-static data member of %qT",
928132718Skan	     field, type);
929132718Skan      return 0;
930132718Skan    }
931132718Skan  if (initializing_context (field) != type)
932132718Skan    {
933169689Skan      error ("class %qT does not have any field named %qD", type,
934132718Skan		member_name);
935132718Skan      return 0;
936132718Skan    }
93718334Speter
93818334Speter  return 1;
93918334Speter}
94018334Speter
941117395Skan/* NAME is a FIELD_DECL, an IDENTIFIER_NODE which names a field, or it
942117395Skan   is a _TYPE node or TYPE_DECL which names a base for that type.
943117395Skan   Check the validity of NAME, and return either the base _TYPE, base
944117395Skan   binfo, or the FIELD_DECL of the member.  If NAME is invalid, return
945117395Skan   NULL_TREE and issue a diagnostic.
94618334Speter
94790075Sobrien   An old style unnamed direct single base construction is permitted,
94890075Sobrien   where NAME is NULL.  */
94918334Speter
95090075Sobrientree
951117395Skanexpand_member_init (tree name)
95218334Speter{
953117395Skan  tree basetype;
954117395Skan  tree field;
95518334Speter
956117395Skan  if (!current_class_ref)
95790075Sobrien    return NULL_TREE;
95818334Speter
95990075Sobrien  if (!name)
96050397Sobrien    {
96190075Sobrien      /* This is an obsolete unnamed base class initializer.  The
96290075Sobrien	 parser will already have warned about its use.  */
963169689Skan      switch (BINFO_N_BASE_BINFOS (TYPE_BINFO (current_class_type)))
96490075Sobrien	{
96590075Sobrien	case 0:
966169689Skan	  error ("unnamed initializer for %qT, which has no base classes",
967117395Skan		 current_class_type);
96890075Sobrien	  return NULL_TREE;
96990075Sobrien	case 1:
970169689Skan	  basetype = BINFO_TYPE
971169689Skan	    (BINFO_BASE_BINFO (TYPE_BINFO (current_class_type), 0));
97290075Sobrien	  break;
97390075Sobrien	default:
974169689Skan	  error ("unnamed initializer for %qT, which uses multiple inheritance",
975117395Skan		 current_class_type);
97690075Sobrien	  return NULL_TREE;
97790075Sobrien      }
97850397Sobrien    }
97990075Sobrien  else if (TYPE_P (name))
98090075Sobrien    {
981110611Skan      basetype = TYPE_MAIN_VARIANT (name);
98290075Sobrien      name = TYPE_NAME (name);
98390075Sobrien    }
98490075Sobrien  else if (TREE_CODE (name) == TYPE_DECL)
98590075Sobrien    basetype = TYPE_MAIN_VARIANT (TREE_TYPE (name));
986117395Skan  else
987117395Skan    basetype = NULL_TREE;
98850397Sobrien
98990075Sobrien  if (basetype)
99018334Speter    {
991132718Skan      tree class_binfo;
992132718Skan      tree direct_binfo;
993132718Skan      tree virtual_binfo;
994132718Skan      int i;
995117395Skan
99690075Sobrien      if (current_template_parms)
997117395Skan	return basetype;
998117395Skan
999132718Skan      class_binfo = TYPE_BINFO (current_class_type);
1000132718Skan      direct_binfo = NULL_TREE;
1001132718Skan      virtual_binfo = NULL_TREE;
1002132718Skan
1003132718Skan      /* Look for a direct base.  */
1004169689Skan      for (i = 0; BINFO_BASE_ITERATE (class_binfo, i, direct_binfo); ++i)
1005169689Skan	if (SAME_BINFO_TYPE_P (BINFO_TYPE (direct_binfo), basetype))
1006169689Skan	  break;
1007169689Skan
1008132718Skan      /* Look for a virtual base -- unless the direct base is itself
1009132718Skan	 virtual.  */
1010169689Skan      if (!direct_binfo || !BINFO_VIRTUAL_P (direct_binfo))
1011169689Skan	virtual_binfo = binfo_for_vbase (basetype, current_class_type);
1012132718Skan
1013132718Skan      /* [class.base.init]
1014169689Skan
1015169689Skan	 If a mem-initializer-id is ambiguous because it designates
1016132718Skan	 both a direct non-virtual base class and an inherited virtual
1017132718Skan	 base class, the mem-initializer is ill-formed.  */
1018132718Skan      if (direct_binfo && virtual_binfo)
1019117395Skan	{
1020169689Skan	  error ("%qD is both a direct base and an indirect virtual base",
1021132718Skan		 basetype);
1022132718Skan	  return NULL_TREE;
1023132718Skan	}
1024132718Skan
1025132718Skan      if (!direct_binfo && !virtual_binfo)
1026132718Skan	{
1027169689Skan	  if (CLASSTYPE_VBASECLASSES (current_class_type))
1028169689Skan	    error ("type %qT is not a direct or virtual base of %qT",
1029161651Skan		   basetype, current_class_type);
103050397Sobrien	  else
1031169689Skan	    error ("type %qT is not a direct base of %qT",
1032161651Skan		   basetype, current_class_type);
103390075Sobrien	  return NULL_TREE;
103450397Sobrien	}
103518334Speter
1036132718Skan      return direct_binfo ? direct_binfo : virtual_binfo;
103750397Sobrien    }
103850397Sobrien  else
103950397Sobrien    {
1040104752Skan      if (TREE_CODE (name) == IDENTIFIER_NODE)
1041132718Skan	field = lookup_field (current_class_type, name, 1, false);
1042104752Skan      else
1043104752Skan	field = name;
104418334Speter
1045117395Skan      if (member_init_ok_or_else (field, current_class_type, name))
1046117395Skan	return field;
104790075Sobrien    }
104850397Sobrien
1049117395Skan  return NULL_TREE;
105018334Speter}
105118334Speter
105218334Speter/* This is like `expand_member_init', only it stores one aggregate
105318334Speter   value into another.
105418334Speter
105518334Speter   INIT comes in two flavors: it is either a value which
105618334Speter   is to be stored in EXP, or it is a parameter list
105718334Speter   to go to a constructor, which will operate on EXP.
105818334Speter   If INIT is not a parameter list for a constructor, then set
105918334Speter   LOOKUP_ONLYCONVERTING.
106018334Speter   If FLAGS is LOOKUP_ONLYCONVERTING then it is the = init form of
106118334Speter   the initializer, if FLAGS is 0, then it is the (init) form.
106218334Speter   If `init' is a CONSTRUCTOR, then we emit a warning message,
106318334Speter   explaining that such initializations are invalid.
106418334Speter
106518334Speter   If INIT resolves to a CALL_EXPR which happens to return
106618334Speter   something of the type we are looking for, then we know
106718334Speter   that we can safely use that call to perform the
106818334Speter   initialization.
106918334Speter
107018334Speter   The virtual function table pointer cannot be set up here, because
107118334Speter   we do not really know its type.
107218334Speter
107318334Speter   This never calls operator=().
107418334Speter
107518334Speter   When initializing, nothing is CONST.
107618334Speter
107718334Speter   A default copy constructor may have to be used to perform the
107818334Speter   initialization.
107918334Speter
108018334Speter   A constructor or a conversion operator may have to be used to
108150397Sobrien   perform the initialization, but not both, as it would be ambiguous.  */
108218334Speter
108390075Sobrientree
1084132718Skanbuild_aggr_init (tree exp, tree init, int flags)
108518334Speter{
108690075Sobrien  tree stmt_expr;
108790075Sobrien  tree compound_stmt;
108890075Sobrien  int destroy_temps;
108918334Speter  tree type = TREE_TYPE (exp);
109018334Speter  int was_const = TREE_READONLY (exp);
109118334Speter  int was_volatile = TREE_THIS_VOLATILE (exp);
1092132718Skan  int is_global;
109318334Speter
109418334Speter  if (init == error_mark_node)
109590075Sobrien    return error_mark_node;
109618334Speter
109718334Speter  TREE_READONLY (exp) = 0;
109818334Speter  TREE_THIS_VOLATILE (exp) = 0;
109918334Speter
110018334Speter  if (init && TREE_CODE (init) != TREE_LIST)
110118334Speter    flags |= LOOKUP_ONLYCONVERTING;
110218334Speter
110318334Speter  if (TREE_CODE (type) == ARRAY_TYPE)
110418334Speter    {
1105169689Skan      tree itype;
1106169689Skan
1107169689Skan      /* An array may not be initialized use the parenthesized
1108169689Skan	 initialization form -- unless the initializer is "()".  */
1109169689Skan      if (init && TREE_CODE (init) == TREE_LIST)
111018334Speter	{
111118334Speter	  error ("bad array initializer");
111290075Sobrien	  return error_mark_node;
111318334Speter	}
1114169689Skan      /* Must arrange to initialize each element of EXP
1115169689Skan	 from elements of INIT.  */
1116169689Skan      itype = init ? TREE_TYPE (init) : NULL_TREE;
111790075Sobrien      if (cp_type_quals (type) != TYPE_UNQUALIFIED)
1118102780Skan	TREE_TYPE (exp) = TYPE_MAIN_VARIANT (type);
1119102780Skan      if (itype && cp_type_quals (itype) != TYPE_UNQUALIFIED)
1120169689Skan	itype = TREE_TYPE (init) = TYPE_MAIN_VARIANT (itype);
1121117395Skan      stmt_expr = build_vec_init (exp, NULL_TREE, init,
1122169689Skan				  /*explicit_default_init_p=*/false,
1123169689Skan				  itype && same_type_p (itype,
1124169689Skan							TREE_TYPE (exp)));
112518334Speter      TREE_READONLY (exp) = was_const;
112618334Speter      TREE_THIS_VOLATILE (exp) = was_volatile;
112718334Speter      TREE_TYPE (exp) = type;
112818334Speter      if (init)
112918334Speter	TREE_TYPE (init) = itype;
113090075Sobrien      return stmt_expr;
113118334Speter    }
113218334Speter
113318334Speter  if (TREE_CODE (exp) == VAR_DECL || TREE_CODE (exp) == PARM_DECL)
1134132718Skan    /* Just know that we've seen something for this node.  */
113518334Speter    TREE_USED (exp) = 1;
113618334Speter
113718334Speter  TREE_TYPE (exp) = TYPE_MAIN_VARIANT (type);
1138132718Skan  is_global = begin_init_stmts (&stmt_expr, &compound_stmt);
113990075Sobrien  destroy_temps = stmts_are_full_exprs_p ();
114090075Sobrien  current_stmt_tree ()->stmts_are_full_exprs_p = 0;
114118334Speter  expand_aggr_init_1 (TYPE_BINFO (type), exp, exp,
114252284Sobrien		      init, LOOKUP_NORMAL|flags);
1143132718Skan  stmt_expr = finish_init_stmts (is_global, stmt_expr, compound_stmt);
114490075Sobrien  current_stmt_tree ()->stmts_are_full_exprs_p = destroy_temps;
114518334Speter  TREE_TYPE (exp) = type;
114618334Speter  TREE_READONLY (exp) = was_const;
114718334Speter  TREE_THIS_VOLATILE (exp) = was_volatile;
114890075Sobrien
114990075Sobrien  return stmt_expr;
115018334Speter}
115118334Speter
115218334Speterstatic void
1153132718Skanexpand_default_init (tree binfo, tree true_exp, tree exp, tree init, int flags)
115418334Speter{
115550397Sobrien  tree type = TREE_TYPE (exp);
115690075Sobrien  tree ctor_name;
115750397Sobrien
115818334Speter  /* It fails because there may not be a constructor which takes
115918334Speter     its own type as the first (or only parameter), but which does
116018334Speter     take other types via a conversion.  So, if the thing initializing
116118334Speter     the expression is a unit element of type X, first try X(X&),
116218334Speter     followed by initialization by X.  If neither of these work
116318334Speter     out, then look hard.  */
116418334Speter  tree rval;
116518334Speter  tree parms;
116618334Speter
116750397Sobrien  if (init && TREE_CODE (init) != TREE_LIST
116850397Sobrien      && (flags & LOOKUP_ONLYCONVERTING))
116950397Sobrien    {
117050397Sobrien      /* Base subobjects should only get direct-initialization.  */
1171169689Skan      gcc_assert (true_exp == exp);
117250397Sobrien
117352284Sobrien      if (flags & DIRECT_BIND)
117452284Sobrien	/* Do nothing.  We hit this in two cases:  Reference initialization,
117552284Sobrien	   where we aren't initializing a real variable, so we don't want
117652284Sobrien	   to run a new constructor; and catching an exception, where we
117752284Sobrien	   have already built up the constructor call so we could wrap it
117852284Sobrien	   in an exception region.  */;
1179169689Skan      else if (BRACE_ENCLOSED_INITIALIZER_P (init))
1180117395Skan	{
1181117395Skan	  /* A brace-enclosed initializer for an aggregate.  */
1182169689Skan	  gcc_assert (CP_AGGREGATE_TYPE_P (type));
1183169689Skan	  init = digest_init (type, init);
1184117395Skan	}
118552284Sobrien      else
118650397Sobrien	init = ocp_convert (type, init, CONV_IMPLICIT|CONV_FORCE_TEMP, flags);
118750397Sobrien
1188132718Skan      if (TREE_CODE (init) == MUST_NOT_THROW_EXPR)
1189132718Skan	/* We need to protect the initialization of a catch parm with a
1190132718Skan	   call to terminate(), which shows up as a MUST_NOT_THROW_EXPR
119150397Sobrien	   around the TARGET_EXPR for the copy constructor.  See
1192132718Skan	   initialize_handler_parm.  */
1193132718Skan	{
1194169689Skan	  TREE_OPERAND (init, 0) = build2 (INIT_EXPR, TREE_TYPE (exp), exp,
1195169689Skan					   TREE_OPERAND (init, 0));
1196132718Skan	  TREE_TYPE (init) = void_type_node;
1197132718Skan	}
119850397Sobrien      else
1199169689Skan	init = build2 (INIT_EXPR, TREE_TYPE (exp), exp, init);
120050397Sobrien      TREE_SIDE_EFFECTS (init) = 1;
120190075Sobrien      finish_expr_stmt (init);
120250397Sobrien      return;
120350397Sobrien    }
120450397Sobrien
120518334Speter  if (init == NULL_TREE
120618334Speter      || (TREE_CODE (init) == TREE_LIST && ! TREE_TYPE (init)))
120718334Speter    {
120818334Speter      parms = init;
120918334Speter      if (parms)
121018334Speter	init = TREE_VALUE (parms);
121118334Speter    }
121218334Speter  else
121390075Sobrien    parms = build_tree_list (NULL_TREE, init);
121418334Speter
121590075Sobrien  if (true_exp == exp)
121690075Sobrien    ctor_name = complete_ctor_identifier;
121790075Sobrien  else
121890075Sobrien    ctor_name = base_ctor_identifier;
121990075Sobrien
1220117395Skan  rval = build_special_member_call (exp, ctor_name, parms, binfo, flags);
122190075Sobrien  if (TREE_SIDE_EFFECTS (rval))
1222132718Skan    finish_expr_stmt (convert_to_void (rval, NULL));
122318334Speter}
122418334Speter
122518334Speter/* This function is responsible for initializing EXP with INIT
122618334Speter   (if any).
122718334Speter
122818334Speter   BINFO is the binfo of the type for who we are performing the
122918334Speter   initialization.  For example, if W is a virtual base class of A and B,
123018334Speter   and C : A, B.
123118334Speter   If we are initializing B, then W must contain B's W vtable, whereas
123218334Speter   were we initializing C, W must contain C's W vtable.
123318334Speter
123418334Speter   TRUE_EXP is nonzero if it is the true expression being initialized.
123518334Speter   In this case, it may be EXP, or may just contain EXP.  The reason we
123618334Speter   need this is because if EXP is a base element of TRUE_EXP, we
123718334Speter   don't necessarily know by looking at EXP where its virtual
123818334Speter   baseclass fields should really be pointing.  But we do know
123918334Speter   from TRUE_EXP.  In constructors, we don't know anything about
124018334Speter   the value being initialized.
124118334Speter
1242132718Skan   FLAGS is just passed to `build_new_method_call'.  See that function
1243132718Skan   for its description.  */
124418334Speter
124518334Speterstatic void
1246132718Skanexpand_aggr_init_1 (tree binfo, tree true_exp, tree exp, tree init, int flags)
124718334Speter{
124818334Speter  tree type = TREE_TYPE (exp);
124918334Speter
1250169689Skan  gcc_assert (init != error_mark_node && type != error_mark_node);
1251169689Skan  gcc_assert (building_stmt_tree ());
125218334Speter
125318334Speter  /* Use a function returning the desired type to initialize EXP for us.
125418334Speter     If the function is a constructor, and its first argument is
125518334Speter     NULL_TREE, know that it was meant for us--just slide exp on
125618334Speter     in and expand the constructor.  Constructors now come
125718334Speter     as TARGET_EXPRs.  */
125850397Sobrien
125950397Sobrien  if (init && TREE_CODE (exp) == VAR_DECL
1260169689Skan      && COMPOUND_LITERAL_P (init))
126118334Speter    {
126290075Sobrien      /* If store_init_value returns NULL_TREE, the INIT has been
1263169689Skan	 recorded as the DECL_INITIAL for EXP.  That means there's
126490075Sobrien	 nothing more we have to do.  */
1265132718Skan      init = store_init_value (exp, init);
1266132718Skan      if (init)
1267132718Skan	finish_expr_stmt (init);
126850397Sobrien      return;
126918334Speter    }
127018334Speter
127150397Sobrien  /* We know that expand_default_init can handle everything we want
127250397Sobrien     at this point.  */
127352284Sobrien  expand_default_init (binfo, true_exp, exp, init, flags);
127418334Speter}
127518334Speter
127650397Sobrien/* Report an error if TYPE is not a user-defined, aggregate type.  If
127750397Sobrien   OR_ELSE is nonzero, give an error message.  */
127850397Sobrien
127950397Sobrienint
1280132718Skanis_aggr_type (tree type, int or_else)
128150397Sobrien{
128250397Sobrien  if (type == error_mark_node)
128350397Sobrien    return 0;
128450397Sobrien
128550397Sobrien  if (! IS_AGGR_TYPE (type)
128650397Sobrien      && TREE_CODE (type) != TEMPLATE_TYPE_PARM
128790075Sobrien      && TREE_CODE (type) != BOUND_TEMPLATE_TEMPLATE_PARM)
128850397Sobrien    {
128950397Sobrien      if (or_else)
1290169689Skan	error ("%qT is not an aggregate type", type);
129150397Sobrien      return 0;
129250397Sobrien    }
129350397Sobrien  return 1;
129450397Sobrien}
129550397Sobrien
129618334Spetertree
1297132718Skanget_type_value (tree name)
129818334Speter{
129918334Speter  if (name == error_mark_node)
130018334Speter    return NULL_TREE;
130118334Speter
130218334Speter  if (IDENTIFIER_HAS_TYPE_VALUE (name))
130318334Speter    return IDENTIFIER_TYPE_VALUE (name);
130418334Speter  else
130518334Speter    return NULL_TREE;
130618334Speter}
130790075Sobrien
1308132718Skan/* Build a reference to a member of an aggregate.  This is not a C++
1309132718Skan   `&', but really something which can have its address taken, and
1310132718Skan   then act as a pointer to member, for example TYPE :: FIELD can have
1311132718Skan   its address taken by saying & TYPE :: FIELD.  ADDRESS_P is true if
1312132718Skan   this expression is the operand of "&".
131318334Speter
131418334Speter   @@ Prints out lousy diagnostics for operator <typename>
131518334Speter   @@ fields.
131618334Speter
131718334Speter   @@ This function should be rewritten and placed in search.c.  */
131850397Sobrien
131918334Spetertree
1320169689Skanbuild_offset_ref (tree type, tree member, bool address_p)
132118334Speter{
1322132718Skan  tree decl;
132350397Sobrien  tree basebinfo = NULL_TREE;
132418334Speter
132550397Sobrien  /* class templates can come in as TEMPLATE_DECLs here.  */
1326169689Skan  if (TREE_CODE (member) == TEMPLATE_DECL)
1327169689Skan    return member;
132818334Speter
1329169689Skan  if (dependent_type_p (type) || type_dependent_expression_p (member))
1330169689Skan    return build_qualified_name (NULL_TREE, type, member,
1331169689Skan				 /*template_p=*/false);
133250397Sobrien
1333169689Skan  gcc_assert (TYPE_P (type));
133490075Sobrien  if (! is_aggr_type (type, 1))
133518334Speter    return error_mark_node;
133618334Speter
1337169689Skan  gcc_assert (DECL_P (member) || BASELINK_P (member));
1338169689Skan  /* Callers should call mark_used before this point.  */
1339169689Skan  gcc_assert (!DECL_P (member) || TREE_USED (member));
134018334Speter
134190075Sobrien  if (!COMPLETE_TYPE_P (complete_type (type))
134252284Sobrien      && !TYPE_BEING_DEFINED (type))
134318334Speter    {
1344169689Skan      error ("incomplete type %qT does not have member %qD", type, member);
134518334Speter      return error_mark_node;
134618334Speter    }
134718334Speter
1348169689Skan  /* Entities other than non-static members need no further
1349169689Skan     processing.  */
1350132718Skan  if (TREE_CODE (member) == TYPE_DECL)
1351169689Skan    return member;
1352132718Skan  if (TREE_CODE (member) == VAR_DECL || TREE_CODE (member) == CONST_DECL)
1353169689Skan    return convert_from_reference (member);
1354132718Skan
1355132718Skan  if (TREE_CODE (member) == FIELD_DECL && DECL_C_BIT_FIELD (member))
1356132718Skan    {
1357169689Skan      error ("invalid pointer to bit-field %qD", member);
1358132718Skan      return error_mark_node;
1359132718Skan    }
1360132718Skan
1361169689Skan  /* Set up BASEBINFO for member lookup.  */
1362169689Skan  decl = maybe_dummy_object (type, &basebinfo);
1363169689Skan
136490075Sobrien  /* A lot of this logic is now handled in lookup_member.  */
1365132718Skan  if (BASELINK_P (member))
136618334Speter    {
136718334Speter      /* Go from the TREE_BASELINK to the member function info.  */
1368169689Skan      tree t = BASELINK_FUNCTIONS (member);
136918334Speter
1370117395Skan      if (TREE_CODE (t) != TEMPLATE_ID_EXPR && !really_overloaded_fn (t))
137118334Speter	{
1372132718Skan	  /* Get rid of a potential OVERLOAD around it.  */
137350397Sobrien	  t = OVL_CURRENT (t);
137450397Sobrien
1375132718Skan	  /* Unique functions are handled easily.  */
1376132718Skan
1377132718Skan	  /* For non-static member of base class, we need a special rule
1378132718Skan	     for access checking [class.protected]:
1379132718Skan
1380132718Skan	       If the access is to form a pointer to member, the
1381132718Skan	       nested-name-specifier shall name the derived class
1382132718Skan	       (or any class derived from that class).  */
1383132718Skan	  if (address_p && DECL_P (t)
1384132718Skan	      && DECL_NONSTATIC_MEMBER_P (t))
1385169689Skan	    perform_or_defer_access_check (TYPE_BINFO (type), t, t);
1386132718Skan	  else
1387169689Skan	    perform_or_defer_access_check (basebinfo, t, t);
1388132718Skan
138952284Sobrien	  if (DECL_STATIC_FUNCTION_P (t))
139052284Sobrien	    return t;
1391132718Skan	  member = t;
139250397Sobrien	}
1393132718Skan      else
1394169689Skan	TREE_TYPE (member) = unknown_type_node;
139518334Speter    }
1396132718Skan  else if (address_p && TREE_CODE (member) == FIELD_DECL)
1397132718Skan    /* We need additional test besides the one in
1398132718Skan       check_accessibility_of_qualified_id in case it is
1399132718Skan       a pointer to non-static member.  */
1400169689Skan    perform_or_defer_access_check (TYPE_BINFO (type), member, member);
140118334Speter
1402132718Skan  if (!address_p)
140318334Speter    {
1404132718Skan      /* If MEMBER is non-static, then the program has fallen afoul of
1405132718Skan	 [expr.prim]:
140618334Speter
1407132718Skan	   An id-expression that denotes a nonstatic data member or
1408132718Skan	   nonstatic member function of a class can only be used:
140918334Speter
1410132718Skan	   -- as part of a class member access (_expr.ref_) in which the
1411132718Skan	   object-expression refers to the member's class or a class
1412132718Skan	   derived from that class, or
141318334Speter
1414132718Skan	   -- to form a pointer to member (_expr.unary.op_), or
141518334Speter
1416132718Skan	   -- in the body of a nonstatic member function of that class or
1417132718Skan	   of a class derived from that class (_class.mfct.nonstatic_), or
141818334Speter
1419132718Skan	   -- in a mem-initializer for a constructor for that class or for
1420132718Skan	   a class derived from that class (_class.base.init_).  */
1421132718Skan      if (DECL_NONSTATIC_MEMBER_FUNCTION_P (member))
142218334Speter	{
1423132718Skan	  /* Build a representation of a the qualified name suitable
1424132718Skan	     for use as the operand to "&" -- even though the "&" is
1425132718Skan	     not actually present.  */
1426169689Skan	  member = build2 (OFFSET_REF, TREE_TYPE (member), decl, member);
1427132718Skan	  /* In Microsoft mode, treat a non-static member function as if
1428132718Skan	     it were a pointer-to-member.  */
1429132718Skan	  if (flag_ms_extensions)
1430132718Skan	    {
1431132718Skan	      PTRMEM_OK_P (member) = 1;
1432132718Skan	      return build_unary_op (ADDR_EXPR, member, 0);
1433132718Skan	    }
1434169689Skan	  error ("invalid use of non-static member function %qD",
1435132718Skan		 TREE_OPERAND (member, 1));
1436169689Skan	  return error_mark_node;
1437132718Skan	}
1438132718Skan      else if (TREE_CODE (member) == FIELD_DECL)
1439132718Skan	{
1440169689Skan	  error ("invalid use of non-static data member %qD", member);
144118334Speter	  return error_mark_node;
144218334Speter	}
144318334Speter      return member;
144418334Speter    }
144518334Speter
1446169689Skan  member = build2 (OFFSET_REF, TREE_TYPE (member), decl, member);
1447132718Skan  PTRMEM_OK_P (member) = 1;
1448132718Skan  return member;
1449132718Skan}
145050397Sobrien
1451169689Skan/* If DECL is a scalar enumeration constant or variable with a
1452169689Skan   constant initializer, return the initializer (or, its initializers,
1453169689Skan   recursively); otherwise, return DECL.  If INTEGRAL_P, the
1454169689Skan   initializer is only returned if DECL is an integral
1455169689Skan   constant-expression.  */
145618334Speter
145750397Sobrienstatic tree
1458169689Skanconstant_value_1 (tree decl, bool integral_p)
145918334Speter{
1460169689Skan  while (TREE_CODE (decl) == CONST_DECL
1461169689Skan	 || (integral_p
1462169689Skan	     ? DECL_INTEGRAL_CONSTANT_VAR_P (decl)
1463169689Skan	     : (TREE_CODE (decl) == VAR_DECL
1464169689Skan		&& CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (decl)))))
146518334Speter    {
1466169689Skan      tree init;
1467169689Skan      /* Static data members in template classes may have
1468169689Skan	 non-dependent initializers.  References to such non-static
1469169689Skan	 data members are not value-dependent, so we must retrieve the
1470169689Skan	 initializer here.  The DECL_INITIAL will have the right type,
1471169689Skan	 but will not have been folded because that would prevent us
1472169689Skan	 from performing all appropriate semantic checks at
1473169689Skan	 instantiation time.  */
1474169689Skan      if (DECL_CLASS_SCOPE_P (decl)
1475169689Skan	  && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
1476169689Skan	  && uses_template_parms (CLASSTYPE_TI_ARGS
1477169689Skan				  (DECL_CONTEXT (decl))))
147818334Speter	{
1479169689Skan	  ++processing_template_decl;
1480169689Skan	  init = fold_non_dependent_expr (DECL_INITIAL (decl));
1481169689Skan	  --processing_template_decl;
148218334Speter	}
148318334Speter      else
148418334Speter	{
1485169689Skan	  /* If DECL is a static data member in a template
1486169689Skan	     specialization, we must instantiate it here.  The
1487169689Skan	     initializer for the static data member is not processed
1488169689Skan	     until needed; we need it now.  */
1489169689Skan	  mark_used (decl);
1490169689Skan	  init = DECL_INITIAL (decl);
149118334Speter	}
1492169689Skan      if (init == error_mark_node)
1493169689Skan	return decl;
1494169689Skan      if (!init
1495169689Skan	  || !TREE_TYPE (init)
1496169689Skan	  || (integral_p
1497169689Skan	      ? !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (init))
1498169689Skan	      : (!TREE_CONSTANT (init)
1499169689Skan		 /* Do not return an aggregate constant (of which
1500169689Skan		    string literals are a special case), as we do not
1501169689Skan		    want to make inadvertent copies of such entities,
1502169689Skan		    and we must be sure that their addresses are the
1503169689Skan		    same everywhere.  */
1504169689Skan		 || TREE_CODE (init) == CONSTRUCTOR
1505169689Skan		 || TREE_CODE (init) == STRING_CST)))
1506169689Skan	break;
1507169689Skan      decl = unshare_expr (init);
150818334Speter    }
1509169689Skan  return decl;
1510169689Skan}
151118334Speter
1512169689Skan/* If DECL is a CONST_DECL, or a constant VAR_DECL initialized by
1513169689Skan   constant of integral or enumeration type, then return that value.
1514169689Skan   These are those variables permitted in constant expressions by
1515169689Skan   [5.19/1].  */
151650397Sobrien
1517169689Skantree
1518169689Skanintegral_constant_value (tree decl)
1519169689Skan{
1520169689Skan  return constant_value_1 (decl, /*integral_p=*/true);
152150397Sobrien}
152250397Sobrien
1523169689Skan/* A more relaxed version of integral_constant_value, used by the
1524169689Skan   common C/C++ code and by the C++ front-end for optimization
1525169689Skan   purposes.  */
152652284Sobrien
152790075Sobrientree
1528169689Skandecl_constant_value (tree decl)
152952284Sobrien{
1530169689Skan  return constant_value_1 (decl,
1531169689Skan			   /*integral_p=*/processing_template_decl);
1532169689Skan}
1533169689Skan
1534169689Skan/* Common subroutines of build_new and build_vec_delete.  */
153590075Sobrien
1536169689Skan/* Call the global __builtin_delete to delete ADDR.  */
153790075Sobrien
1538169689Skanstatic tree
1539169689Skanbuild_builtin_delete_call (tree addr)
1540169689Skan{
1541169689Skan  mark_used (global_delete_fndecl);
1542169689Skan  return build_call (global_delete_fndecl, build_tree_list (NULL_TREE, addr));
154352284Sobrien}
1544169689Skan
1545169689Skan/* Build and return a NEW_EXPR.  If NELTS is non-NULL, TYPE[NELTS] is
1546169689Skan   the type of the object being allocated; otherwise, it's just TYPE.
1547169689Skan   INIT is the initializer, if any.  USE_GLOBAL_NEW is true if the
1548169689Skan   user explicitly wrote "::operator new".  PLACEMENT, if non-NULL, is
1549169689Skan   the TREE_LIST of arguments to be provided as arguments to a
1550169689Skan   placement new operator.  This routine performs no semantic checks;
1551169689Skan   it just creates and returns a NEW_EXPR.  */
155252284Sobrien
155390075Sobrienstatic tree
1554169689Skanbuild_raw_new_expr (tree placement, tree type, tree nelts, tree init,
1555169689Skan		    int use_global_new)
155690075Sobrien{
1557169689Skan  tree new_expr;
155890075Sobrien
1559169689Skan  new_expr = build4 (NEW_EXPR, build_pointer_type (type), placement, type,
1560169689Skan		     nelts, init);
1561169689Skan  NEW_EXPR_USE_GLOBAL (new_expr) = use_global_new;
1562169689Skan  TREE_SIDE_EFFECTS (new_expr) = 1;
156390075Sobrien
1564169689Skan  return new_expr;
156590075Sobrien}
156690075Sobrien
1567169689Skan/* Generate code for a new-expression, including calling the "operator
1568169689Skan   new" function, initializing the object, and, if an exception occurs
1569169689Skan   during construction, cleaning up.  The arguments are as for
1570169689Skan   build_raw_new_expr.  */
157150397Sobrien
157290075Sobrienstatic tree
1573169689Skanbuild_new_1 (tree placement, tree type, tree nelts, tree init,
1574169689Skan	     bool globally_qualified_p)
157550397Sobrien{
1576169689Skan  tree size, rval;
1577169689Skan  /* True iff this is a call to "operator new[]" instead of just
1578169689Skan     "operator new".  */
1579169689Skan  bool array_p = false;
1580169689Skan  /* True iff ARRAY_P is true and the bound of the array type is
1581169689Skan     not necessarily a compile time constant.  For example, VLA_P is
1582169689Skan     true for "new int[f()]".  */
1583169689Skan  bool vla_p = false;
1584169689Skan  /* The type being allocated.  If ARRAY_P is true, this will be an
1585169689Skan     ARRAY_TYPE.  */
1586169689Skan  tree full_type;
1587169689Skan  /* If ARRAY_P is true, the element type of the array.  This is an
1588169689Skan     never ARRAY_TYPE; for something like "new int[3][4]", the
1589169689Skan     ELT_TYPE is "int".  If ARRAY_P is false, this is the same type as
1590169689Skan     FULL_TYPE.  */
1591169689Skan  tree elt_type;
1592132718Skan  /* The type of the new-expression.  (This type is always a pointer
1593132718Skan     type.)  */
1594132718Skan  tree pointer_type;
1595169689Skan  /* A pointer type pointing to the FULL_TYPE.  */
1596132718Skan  tree full_pointer_type;
1597117395Skan  tree outer_nelts = NULL_TREE;
1598132718Skan  tree alloc_call, alloc_expr;
1599132718Skan  /* The address returned by the call to "operator new".  This node is
1600132718Skan     a VAR_DECL and is therefore reusable.  */
1601132718Skan  tree alloc_node;
160296263Sobrien  tree alloc_fn;
160390075Sobrien  tree cookie_expr, init_expr;
1604132718Skan  int nothrow, check_new;
160552284Sobrien  int use_java_new = 0;
160690075Sobrien  /* If non-NULL, the number of extra bytes to allocate at the
160790075Sobrien     beginning of the storage allocated for an array-new expression in
160890075Sobrien     order to store the number of elements.  */
160990075Sobrien  tree cookie_size = NULL_TREE;
161090075Sobrien  /* True if the function we are calling is a placement allocation
161190075Sobrien     function.  */
161290075Sobrien  bool placement_allocation_fn_p;
1613132718Skan  tree args = NULL_TREE;
1614132718Skan  /* True if the storage must be initialized, either by a constructor
1615132718Skan     or due to an explicit new-initializer.  */
1616132718Skan  bool is_initialized;
1617132718Skan  /* The address of the thing allocated, not including any cookie.  In
1618132718Skan     particular, if an array cookie is in use, DATA_ADDR is the
1619132718Skan     address of the first array element.  This node is a VAR_DECL, and
1620132718Skan     is therefore reusable.  */
1621132718Skan  tree data_addr;
1622132718Skan  tree init_preeval_expr = NULL_TREE;
162350397Sobrien
1624169689Skan  if (nelts)
162550397Sobrien    {
1626169689Skan      tree index;
162790075Sobrien
1628169689Skan      outer_nelts = nelts;
1629169689Skan      array_p = true;
1630169689Skan
1631169689Skan      /* ??? The middle-end will error on us for building a VLA outside a
1632169689Skan	 function context.  Methinks that's not it's purvey.  So we'll do
1633169689Skan	 our own VLA layout later.  */
1634169689Skan      vla_p = true;
1635169689Skan      index = convert (sizetype, nelts);
1636169689Skan      index = size_binop (MINUS_EXPR, index, size_one_node);
1637169689Skan      index = build_index_type (index);
1638117395Skan      full_type = build_cplus_array_type (type, NULL_TREE);
1639169689Skan      /* We need a copy of the type as build_array_type will return a shared copy
1640169689Skan         of the incomplete array type.  */
1641169689Skan      full_type = build_distinct_type_copy (full_type);
1642169689Skan      TYPE_DOMAIN (full_type) = index;
164350397Sobrien    }
164490075Sobrien  else
1645169689Skan    {
1646169689Skan      full_type = type;
1647169689Skan      if (TREE_CODE (type) == ARRAY_TYPE)
1648169689Skan	{
1649169689Skan	  array_p = true;
1650169689Skan	  nelts = array_type_nelts_top (type);
1651169689Skan	  outer_nelts = nelts;
1652169689Skan	  type = TREE_TYPE (type);
1653169689Skan	}
1654169689Skan    }
165590075Sobrien
1656169689Skan  if (!complete_type_or_else (type, NULL_TREE))
1657169689Skan    return error_mark_node;
165850397Sobrien
165918334Speter  /* If our base type is an array, then make sure we know how many elements
166018334Speter     it has.  */
1661169689Skan  for (elt_type = type;
1662169689Skan       TREE_CODE (elt_type) == ARRAY_TYPE;
1663169689Skan       elt_type = TREE_TYPE (elt_type))
1664169689Skan    nelts = cp_build_binary_op (MULT_EXPR, nelts,
1665169689Skan				array_type_nelts_top (elt_type));
1666169689Skan
1667169689Skan  if (TREE_CODE (elt_type) == VOID_TYPE)
166818334Speter    {
1669169689Skan      error ("invalid type %<void%> for new");
1670169689Skan      return error_mark_node;
167118334Speter    }
167250397Sobrien
1673169689Skan  if (abstract_virtuals_error (NULL_TREE, elt_type))
167450397Sobrien    return error_mark_node;
167550397Sobrien
1676169689Skan  is_initialized = (TYPE_NEEDS_CONSTRUCTING (elt_type) || init);
1677169689Skan  if (CP_TYPE_CONST_P (elt_type) && !is_initialized)
167818334Speter    {
1679169689Skan      error ("uninitialized const in %<new%> of %q#T", elt_type);
168018334Speter      return error_mark_node;
168118334Speter    }
168218334Speter
1683169689Skan  size = size_in_bytes (elt_type);
1684169689Skan  if (array_p)
1685169689Skan    {
1686169689Skan      size = size_binop (MULT_EXPR, size, convert (sizetype, nelts));
1687169689Skan      if (vla_p)
1688169689Skan	{
1689169689Skan	  tree n, bitsize;
169018334Speter
1691169689Skan	  /* Do our own VLA layout.  Setting TYPE_SIZE/_UNIT is
1692169689Skan	     necessary in order for the <INIT_EXPR <*foo> <CONSTRUCTOR
1693169689Skan	     ...>> to be valid.  */
1694169689Skan	  TYPE_SIZE_UNIT (full_type) = size;
1695169689Skan	  n = convert (bitsizetype, nelts);
1696169689Skan	  bitsize = size_binop (MULT_EXPR, TYPE_SIZE (elt_type), n);
1697169689Skan	  TYPE_SIZE (full_type) = bitsize;
1698169689Skan	}
169918334Speter    }
170018334Speter
1701169689Skan  alloc_fn = NULL_TREE;
1702132718Skan
170350397Sobrien  /* Allocate the object.  */
1704169689Skan  if (! placement && TYPE_FOR_JAVA (elt_type))
170518334Speter    {
1706169689Skan      tree class_addr;
1707169689Skan      tree class_decl = build_java_class_ref (elt_type);
170890075Sobrien      static const char alloc_name[] = "_Jv_AllocObject";
1709169689Skan
1710169689Skan      if (class_decl == error_mark_node)
1711169689Skan	return error_mark_node;
1712169689Skan
171352284Sobrien      use_java_new = 1;
1714169689Skan      if (!get_global_value_if_present (get_identifier (alloc_name),
1715169689Skan					&alloc_fn))
1716132718Skan	{
1717169689Skan	  error ("call to Java constructor with %qs undefined", alloc_name);
1718132718Skan	  return error_mark_node;
1719132718Skan	}
1720169689Skan      else if (really_overloaded_fn (alloc_fn))
1721132718Skan	{
1722169689Skan	  error ("%qD should never be overloaded", alloc_fn);
1723132718Skan	  return error_mark_node;
1724132718Skan	}
1725169689Skan      alloc_fn = OVL_CURRENT (alloc_fn);
172652284Sobrien      class_addr = build1 (ADDR_EXPR, jclass_node, class_decl);
172790075Sobrien      alloc_call = (build_function_call
1728169689Skan		    (alloc_fn,
1729169689Skan		     build_tree_list (NULL_TREE, class_addr)));
173052284Sobrien    }
173118334Speter  else
173218334Speter    {
173390075Sobrien      tree fnname;
1734132718Skan      tree fns;
173550397Sobrien
1736169689Skan      fnname = ansi_opname (array_p ? VEC_NEW_EXPR : NEW_EXPR);
173750397Sobrien
1738169689Skan      if (!globally_qualified_p
1739169689Skan	  && CLASS_TYPE_P (elt_type)
1740169689Skan	  && (array_p
1741169689Skan	      ? TYPE_HAS_ARRAY_NEW_OPERATOR (elt_type)
1742169689Skan	      : TYPE_HAS_NEW_OPERATOR (elt_type)))
1743132718Skan	{
1744132718Skan	  /* Use a class-specific operator new.  */
1745132718Skan	  /* If a cookie is required, add some extra space.  */
1746169689Skan	  if (array_p && TYPE_VEC_NEW_USES_COOKIE (elt_type))
1747132718Skan	    {
1748169689Skan	      cookie_size = targetm.cxx.get_cookie_size (elt_type);
1749132718Skan	      size = size_binop (PLUS_EXPR, size, cookie_size);
1750132718Skan	    }
1751132718Skan	  /* Create the argument list.  */
1752132718Skan	  args = tree_cons (NULL_TREE, size, placement);
1753132718Skan	  /* Do name-lookup to find the appropriate operator.  */
1754169689Skan	  fns = lookup_fnfields (elt_type, fnname, /*protect=*/2);
1755169689Skan	  if (fns == NULL_TREE)
1756132718Skan	    {
1757169689Skan	      error ("no suitable %qD found in class %qT", fnname, elt_type);
1758132718Skan	      return error_mark_node;
1759132718Skan	    }
1760132718Skan	  if (TREE_CODE (fns) == TREE_LIST)
1761132718Skan	    {
1762169689Skan	      error ("request for member %qD is ambiguous", fnname);
1763132718Skan	      print_candidates (fns);
1764132718Skan	      return error_mark_node;
1765132718Skan	    }
1766169689Skan	  alloc_call = build_new_method_call (build_dummy_object (elt_type),
1767132718Skan					      fns, args,
1768132718Skan					      /*conversion_path=*/NULL_TREE,
1769169689Skan					      LOOKUP_NORMAL,
1770169689Skan					      &alloc_fn);
1771132718Skan	}
177290075Sobrien      else
1773132718Skan	{
1774132718Skan	  /* Use a global operator new.  */
1775132718Skan	  /* See if a cookie might be required.  */
1776169689Skan	  if (array_p && TYPE_VEC_NEW_USES_COOKIE (elt_type))
1777169689Skan	    cookie_size = targetm.cxx.get_cookie_size (elt_type);
1778132718Skan	  else
1779132718Skan	    cookie_size = NULL_TREE;
1780132718Skan
1781169689Skan	  alloc_call = build_operator_new_call (fnname, placement,
1782169689Skan						&size, &cookie_size,
1783169689Skan						&alloc_fn);
1784132718Skan	}
178518334Speter    }
178618334Speter
178790075Sobrien  if (alloc_call == error_mark_node)
178890075Sobrien    return error_mark_node;
178990075Sobrien
1790169689Skan  gcc_assert (alloc_fn != NULL_TREE);
1791169689Skan
1792132718Skan  /* In the simple case, we can stop now.  */
1793132718Skan  pointer_type = build_pointer_type (type);
1794132718Skan  if (!cookie_size && !is_initialized)
1795132718Skan    return build_nop (pointer_type, alloc_call);
1796132718Skan
1797132718Skan  /* While we're working, use a pointer to the type we've actually
1798132718Skan     allocated. Store the result of the call in a variable so that we
1799132718Skan     can use it more than once.  */
1800132718Skan  full_pointer_type = build_pointer_type (full_type);
1801132718Skan  alloc_expr = get_target_expr (build_nop (full_pointer_type, alloc_call));
1802132718Skan  alloc_node = TARGET_EXPR_SLOT (alloc_expr);
1803132718Skan
1804132718Skan  /* Strip any COMPOUND_EXPRs from ALLOC_CALL.  */
1805169689Skan  while (TREE_CODE (alloc_call) == COMPOUND_EXPR)
1806132718Skan    alloc_call = TREE_OPERAND (alloc_call, 1);
1807132718Skan
180890075Sobrien  /* Now, check to see if this function is actually a placement
180990075Sobrien     allocation function.  This can happen even when PLACEMENT is NULL
181090075Sobrien     because we might have something like:
181190075Sobrien
181290075Sobrien       struct S { void* operator new (size_t, int i = 0); };
181390075Sobrien
181490075Sobrien     A call to `new S' will get this allocation function, even though
181590075Sobrien     there is no explicit placement argument.  If there is more than
181690075Sobrien     one argument, or there are variable arguments, then this is a
181790075Sobrien     placement allocation function.  */
1818169689Skan  placement_allocation_fn_p
1819169689Skan    = (type_num_arguments (TREE_TYPE (alloc_fn)) > 1
182096263Sobrien       || varargs_function_p (alloc_fn));
182190075Sobrien
1822132718Skan  /* Preevaluate the placement args so that we don't reevaluate them for a
1823132718Skan     placement delete.  */
1824132718Skan  if (placement_allocation_fn_p)
1825132718Skan    {
1826132718Skan      tree inits;
1827132718Skan      stabilize_call (alloc_call, &inits);
1828132718Skan      if (inits)
1829169689Skan	alloc_expr = build2 (COMPOUND_EXPR, TREE_TYPE (alloc_expr), inits,
1830169689Skan			     alloc_expr);
1831132718Skan    }
1832132718Skan
183350397Sobrien  /*        unless an allocation function is declared with an empty  excep-
183450397Sobrien     tion-specification  (_except.spec_),  throw(), it indicates failure to
183550397Sobrien     allocate storage by throwing a bad_alloc exception  (clause  _except_,
183650397Sobrien     _lib.bad.alloc_); it returns a non-null pointer otherwise If the allo-
183750397Sobrien     cation function is declared  with  an  empty  exception-specification,
183850397Sobrien     throw(), it returns null to indicate failure to allocate storage and a
183950397Sobrien     non-null pointer otherwise.
184050397Sobrien
184150397Sobrien     So check for a null exception spec on the op new we just called.  */
184250397Sobrien
184396263Sobrien  nothrow = TYPE_NOTHROW_P (TREE_TYPE (alloc_fn));
184452284Sobrien  check_new = (flag_check_new || nothrow) && ! use_java_new;
184550397Sobrien
1846132718Skan  if (cookie_size)
184750397Sobrien    {
184890075Sobrien      tree cookie;
1849169689Skan      tree cookie_ptr;
185090075Sobrien
1851132718Skan      /* Adjust so we're pointing to the start of the object.  */
1852169689Skan      data_addr = get_target_expr (build2 (PLUS_EXPR, full_pointer_type,
1853169689Skan					   alloc_node, cookie_size));
1854132718Skan
185590075Sobrien      /* Store the number of bytes allocated so that we can know how
185690075Sobrien	 many elements to destroy later.  We use the last sizeof
185790075Sobrien	 (size_t) bytes to store the number of elements.  */
1858169689Skan      cookie_ptr = build2 (MINUS_EXPR, build_pointer_type (sizetype),
1859169689Skan			   data_addr, size_in_bytes (sizetype));
1860169689Skan      cookie = build_indirect_ref (cookie_ptr, NULL);
186190075Sobrien
1862169689Skan      cookie_expr = build2 (MODIFY_EXPR, sizetype, cookie, nelts);
1863169689Skan
1864169689Skan      if (targetm.cxx.cookie_has_size ())
1865169689Skan	{
1866169689Skan	  /* Also store the element size.  */
1867169689Skan	  cookie_ptr = build2 (MINUS_EXPR, build_pointer_type (sizetype),
1868169689Skan			       cookie_ptr, size_in_bytes (sizetype));
1869169689Skan	  cookie = build_indirect_ref (cookie_ptr, NULL);
1870169689Skan	  cookie = build2 (MODIFY_EXPR, sizetype, cookie,
1871169689Skan			   size_in_bytes(elt_type));
1872169689Skan	  cookie_expr = build2 (COMPOUND_EXPR, TREE_TYPE (cookie_expr),
1873169689Skan				cookie, cookie_expr);
1874169689Skan	}
1875132718Skan      data_addr = TARGET_EXPR_SLOT (data_addr);
187650397Sobrien    }
187718334Speter  else
1878132718Skan    {
1879132718Skan      cookie_expr = NULL_TREE;
1880132718Skan      data_addr = alloc_node;
1881132718Skan    }
188218334Speter
1883132718Skan  /* Now initialize the allocated object.  Note that we preevaluate the
1884132718Skan     initialization expression, apart from the actual constructor call or
1885132718Skan     assignment--we do this because we want to delay the allocation as long
1886132718Skan     as possible in order to minimize the size of the exception region for
1887132718Skan     placement delete.  */
1888132718Skan  if (is_initialized)
188918334Speter    {
1890132718Skan      bool stable;
189118334Speter
1892132718Skan      init_expr = build_indirect_ref (data_addr, NULL);
1893132718Skan
1894169689Skan      if (array_p)
1895169689Skan	{
1896169689Skan	  bool explicit_default_init_p = false;
189718334Speter
1898169689Skan	  if (init == void_zero_node)
1899169689Skan	    {
1900169689Skan	      init = NULL_TREE;
1901169689Skan	      explicit_default_init_p = true;
1902169689Skan	    }
1903169689Skan	  else if (init)
1904169689Skan	    pedwarn ("ISO C++ forbids initialization in array new");
1905169689Skan
1906132718Skan	  init_expr
1907132718Skan	    = build_vec_init (init_expr,
1908132718Skan			      cp_build_binary_op (MINUS_EXPR, outer_nelts,
1909132718Skan						  integer_one_node),
1910169689Skan			      init,
1911169689Skan			      explicit_default_init_p,
1912169689Skan			      /*from_array=*/0);
1913132718Skan
1914132718Skan	  /* An array initialization is stable because the initialization
1915132718Skan	     of each element is a full-expression, so the temporaries don't
1916132718Skan	     leak out.  */
1917132718Skan	  stable = true;
1918132718Skan	}
191990075Sobrien      else
192018334Speter	{
1921169689Skan	  if (init == void_zero_node)
1922169689Skan	    init = build_default_init (full_type, nelts);
192318334Speter
1924169689Skan	  if (TYPE_NEEDS_CONSTRUCTING (type))
1925169689Skan	    {
1926169689Skan	      init_expr = build_special_member_call (init_expr,
1927169689Skan						     complete_ctor_identifier,
1928169689Skan						     init, elt_type,
1929169689Skan						     LOOKUP_NORMAL);
1930169689Skan	      stable = stabilize_init (init_expr, &init_preeval_expr);
1931169689Skan	    }
1932169689Skan	  else
1933169689Skan	    {
1934169689Skan	      /* We are processing something like `new int (10)', which
1935169689Skan		 means allocate an int, and initialize it with 10.  */
1936132718Skan
1937169689Skan	      if (TREE_CODE (init) == TREE_LIST)
1938169689Skan		init = build_x_compound_expr_from_list (init,
1939169689Skan							"new initializer");
1940169689Skan	      else
1941169689Skan		gcc_assert (TREE_CODE (init) != CONSTRUCTOR
1942169689Skan			    || TREE_TYPE (init) != NULL_TREE);
194318334Speter
1944169689Skan	      init_expr = build_modify_expr (init_expr, INIT_EXPR, init);
1945169689Skan	      stable = stabilize_init (init_expr, &init_preeval_expr);
1946169689Skan	    }
194790075Sobrien	}
194818334Speter
194990075Sobrien      if (init_expr == error_mark_node)
195090075Sobrien	return error_mark_node;
195118334Speter
195252284Sobrien      /* If any part of the object initialization terminates by throwing an
195352284Sobrien	 exception and a suitable deallocation function can be found, the
195452284Sobrien	 deallocation function is called to free the memory in which the
195552284Sobrien	 object was being constructed, after which the exception continues
195652284Sobrien	 to propagate in the context of the new-expression. If no
195752284Sobrien	 unambiguous matching deallocation function can be found,
195852284Sobrien	 propagating the exception does not cause the object's memory to be
195952284Sobrien	 freed.  */
196090075Sobrien      if (flag_exceptions && ! use_java_new)
196118334Speter	{
1962169689Skan	  enum tree_code dcode = array_p ? VEC_DELETE_EXPR : DELETE_EXPR;
196390075Sobrien	  tree cleanup;
196418334Speter
196590075Sobrien	  /* The Standard is unclear here, but the right thing to do
1966132718Skan	     is to use the same method for finding deallocation
1967132718Skan	     functions that we use for finding allocation functions.  */
1968169689Skan	  cleanup = build_op_delete_call (dcode, alloc_node, size,
1969169689Skan					  globally_qualified_p,
1970169689Skan					  (placement_allocation_fn_p
1971169689Skan					   ? alloc_call : NULL_TREE),
1972171825Skan					  alloc_fn);
197350397Sobrien
1974132718Skan	  if (!cleanup)
1975132718Skan	    /* We're done.  */;
1976132718Skan	  else if (stable)
1977132718Skan	    /* This is much simpler if we were able to preevaluate all of
1978132718Skan	       the arguments to the constructor call.  */
1979169689Skan	    init_expr = build2 (TRY_CATCH_EXPR, void_type_node,
1980169689Skan				init_expr, cleanup);
1981132718Skan	  else
1982132718Skan	    /* Ack!  First we allocate the memory.  Then we set our sentry
1983132718Skan	       variable to true, and expand a cleanup that deletes the
1984132718Skan	       memory if sentry is true.  Then we run the constructor, and
1985132718Skan	       finally clear the sentry.
198618334Speter
1987132718Skan	       We need to do this because we allocate the space first, so
1988132718Skan	       if there are any temporaries with cleanups in the
1989132718Skan	       constructor args and we weren't able to preevaluate them, we
1990132718Skan	       need this EH region to extend until end of full-expression
1991132718Skan	       to preserve nesting.  */
199250397Sobrien	    {
199390075Sobrien	      tree end, sentry, begin;
199418334Speter
199550397Sobrien	      begin = get_target_expr (boolean_true_node);
1996117395Skan	      CLEANUP_EH_ONLY (begin) = 1;
199750397Sobrien
1998117395Skan	      sentry = TARGET_EXPR_SLOT (begin);
1999117395Skan
2000117395Skan	      TARGET_EXPR_CLEANUP (begin)
2001169689Skan		= build3 (COND_EXPR, void_type_node, sentry,
2002169689Skan			  cleanup, void_zero_node);
200350397Sobrien
2004169689Skan	      end = build2 (MODIFY_EXPR, TREE_TYPE (sentry),
2005169689Skan			    sentry, boolean_false_node);
200650397Sobrien
200790075Sobrien	      init_expr
2008169689Skan		= build2 (COMPOUND_EXPR, void_type_node, begin,
2009169689Skan			  build2 (COMPOUND_EXPR, void_type_node, init_expr,
2010169689Skan				  end));
201118334Speter	    }
2012169689Skan
201318334Speter	}
201418334Speter    }
2015132718Skan  else
2016132718Skan    init_expr = NULL_TREE;
201718334Speter
201890075Sobrien  /* Now build up the return value in reverse order.  */
201918334Speter
2020132718Skan  rval = data_addr;
202190075Sobrien
202290075Sobrien  if (init_expr)
2023169689Skan    rval = build2 (COMPOUND_EXPR, TREE_TYPE (rval), init_expr, rval);
202490075Sobrien  if (cookie_expr)
2025169689Skan    rval = build2 (COMPOUND_EXPR, TREE_TYPE (rval), cookie_expr, rval);
202690075Sobrien
202790075Sobrien  if (rval == alloc_node)
2028132718Skan    /* If we don't have an initializer or a cookie, strip the TARGET_EXPR
2029132718Skan       and return the call (which doesn't need to be adjusted).  */
2030132718Skan    rval = TARGET_EXPR_INITIAL (alloc_expr);
203190075Sobrien  else
203218334Speter    {
203390075Sobrien      if (check_new)
203490075Sobrien	{
2035132718Skan	  tree ifexp = cp_build_binary_op (NE_EXPR, alloc_node,
2036132718Skan					   integer_zero_node);
203790075Sobrien	  rval = build_conditional_expr (ifexp, rval, alloc_node);
203890075Sobrien	}
203918334Speter
2040132718Skan      /* Perform the allocation before anything else, so that ALLOC_NODE
2041132718Skan	 has been initialized before we start using it.  */
2042169689Skan      rval = build2 (COMPOUND_EXPR, TREE_TYPE (rval), alloc_expr, rval);
204318334Speter    }
204418334Speter
2045132718Skan  if (init_preeval_expr)
2046169689Skan    rval = build2 (COMPOUND_EXPR, TREE_TYPE (rval), init_preeval_expr, rval);
204718334Speter
2048132718Skan  /* Convert to the final type.  */
2049132718Skan  rval = build_nop (pointer_type, rval);
2050132718Skan
2051122180Skan  /* A new-expression is never an lvalue.  */
2052169689Skan  gcc_assert (!lvalue_p (rval));
2053122180Skan
205418334Speter  return rval;
205518334Speter}
2056169689Skan
2057169689Skan/* Generate a representation for a C++ "new" expression.  PLACEMENT is
2058169689Skan   a TREE_LIST of placement-new arguments (or NULL_TREE if none).  If
2059169689Skan   NELTS is NULL, TYPE is the type of the storage to be allocated.  If
2060169689Skan   NELTS is not NULL, then this is an array-new allocation; TYPE is
2061169689Skan   the type of the elements in the array and NELTS is the number of
2062169689Skan   elements in the array.  INIT, if non-NULL, is the initializer for
2063169689Skan   the new object, or void_zero_node to indicate an initializer of
2064169689Skan   "()".  If USE_GLOBAL_NEW is true, then the user explicitly wrote
2065169689Skan   "::new" rather than just "new".  */
2066169689Skan
2067169689Skantree
2068169689Skanbuild_new (tree placement, tree type, tree nelts, tree init,
2069169689Skan	   int use_global_new)
2070169689Skan{
2071169689Skan  tree rval;
2072169689Skan  tree orig_placement;
2073169689Skan  tree orig_nelts;
2074169689Skan  tree orig_init;
2075169689Skan
2076169689Skan  if (placement == error_mark_node || type == error_mark_node
2077169689Skan      || init == error_mark_node)
2078169689Skan    return error_mark_node;
2079169689Skan
2080169689Skan  orig_placement = placement;
2081169689Skan  orig_nelts = nelts;
2082169689Skan  orig_init = init;
2083169689Skan
2084169689Skan  if (processing_template_decl)
2085169689Skan    {
2086169689Skan      if (dependent_type_p (type)
2087169689Skan	  || any_type_dependent_arguments_p (placement)
2088169689Skan	  || (nelts && type_dependent_expression_p (nelts))
2089169689Skan	  || (init != void_zero_node
2090169689Skan	      && any_type_dependent_arguments_p (init)))
2091169689Skan	return build_raw_new_expr (placement, type, nelts, init,
2092169689Skan				   use_global_new);
2093169689Skan      placement = build_non_dependent_args (placement);
2094169689Skan      if (nelts)
2095169689Skan	nelts = build_non_dependent_expr (nelts);
2096169689Skan      if (init != void_zero_node)
2097169689Skan	init = build_non_dependent_args (init);
2098169689Skan    }
2099169689Skan
2100169689Skan  if (nelts)
2101169689Skan    {
2102169689Skan      if (!build_expr_type_conversion (WANT_INT | WANT_ENUM, nelts, false))
2103169689Skan	pedwarn ("size in array new must have integral type");
2104169689Skan      nelts = cp_save_expr (cp_convert (sizetype, nelts));
2105169689Skan      /* It is valid to allocate a zero-element array:
2106169689Skan
2107169689Skan	   [expr.new]
2108169689Skan
2109169689Skan	   When the value of the expression in a direct-new-declarator
2110169689Skan	   is zero, the allocation function is called to allocate an
2111169689Skan	   array with no elements.  The pointer returned by the
2112169689Skan	   new-expression is non-null.  [Note: If the library allocation
2113169689Skan	   function is called, the pointer returned is distinct from the
2114169689Skan	   pointer to any other object.]
2115169689Skan
2116169689Skan	 However, that is not generally useful, so we issue a
2117169689Skan	 warning.  */
2118169689Skan      if (integer_zerop (nelts))
2119169689Skan	warning (0, "allocating zero-element array");
2120169689Skan    }
2121169689Skan
2122169689Skan  /* ``A reference cannot be created by the new operator.  A reference
2123169689Skan     is not an object (8.2.2, 8.4.3), so a pointer to it could not be
2124169689Skan     returned by new.'' ARM 5.3.3 */
2125169689Skan  if (TREE_CODE (type) == REFERENCE_TYPE)
2126169689Skan    {
2127169689Skan      error ("new cannot be applied to a reference type");
2128169689Skan      type = TREE_TYPE (type);
2129169689Skan    }
2130169689Skan
2131169689Skan  if (TREE_CODE (type) == FUNCTION_TYPE)
2132169689Skan    {
2133169689Skan      error ("new cannot be applied to a function type");
2134169689Skan      return error_mark_node;
2135169689Skan    }
2136169689Skan
2137169689Skan  rval = build_new_1 (placement, type, nelts, init, use_global_new);
2138169689Skan  if (rval == error_mark_node)
2139169689Skan    return error_mark_node;
2140169689Skan
2141169689Skan  if (processing_template_decl)
2142169689Skan    return build_raw_new_expr (orig_placement, type, orig_nelts, orig_init,
2143169689Skan			       use_global_new);
2144169689Skan
2145169689Skan  /* Wrap it in a NOP_EXPR so warn_if_unused_value doesn't complain.  */
2146169689Skan  rval = build1 (NOP_EXPR, TREE_TYPE (rval), rval);
2147169689Skan  TREE_NO_WARNING (rval) = 1;
2148169689Skan
2149169689Skan  return rval;
2150169689Skan}
2151169689Skan
2152169689Skan/* Given a Java class, return a decl for the corresponding java.lang.Class.  */
2153169689Skan
2154169689Skantree
2155169689Skanbuild_java_class_ref (tree type)
2156169689Skan{
2157169689Skan  tree name = NULL_TREE, class_decl;
2158169689Skan  static tree CL_suffix = NULL_TREE;
2159169689Skan  if (CL_suffix == NULL_TREE)
2160169689Skan    CL_suffix = get_identifier("class$");
2161169689Skan  if (jclass_node == NULL_TREE)
2162169689Skan    {
2163169689Skan      jclass_node = IDENTIFIER_GLOBAL_VALUE (get_identifier ("jclass"));
2164169689Skan      if (jclass_node == NULL_TREE)
2165169689Skan	{
2166169689Skan	  error ("call to Java constructor, while %<jclass%> undefined");
2167169689Skan	  return error_mark_node;
2168169689Skan	}
2169169689Skan      jclass_node = TREE_TYPE (jclass_node);
2170169689Skan    }
2171169689Skan
2172169689Skan  /* Mangle the class$ field.  */
2173169689Skan  {
2174169689Skan    tree field;
2175169689Skan    for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
2176169689Skan      if (DECL_NAME (field) == CL_suffix)
2177169689Skan	{
2178169689Skan	  mangle_decl (field);
2179169689Skan	  name = DECL_ASSEMBLER_NAME (field);
2180169689Skan	  break;
2181169689Skan	}
2182169689Skan    if (!field)
2183169689Skan      {
2184169689Skan	error ("can't find %<class$%> in %qT", type);
2185169689Skan	return error_mark_node;
2186169689Skan      }
2187169689Skan  }
2188169689Skan
2189169689Skan  class_decl = IDENTIFIER_GLOBAL_VALUE (name);
2190169689Skan  if (class_decl == NULL_TREE)
2191169689Skan    {
2192169689Skan      class_decl = build_decl (VAR_DECL, name, TREE_TYPE (jclass_node));
2193169689Skan      TREE_STATIC (class_decl) = 1;
2194169689Skan      DECL_EXTERNAL (class_decl) = 1;
2195169689Skan      TREE_PUBLIC (class_decl) = 1;
2196169689Skan      DECL_ARTIFICIAL (class_decl) = 1;
2197169689Skan      DECL_IGNORED_P (class_decl) = 1;
2198169689Skan      pushdecl_top_level (class_decl);
2199169689Skan      make_decl_rtl (class_decl);
2200169689Skan    }
2201169689Skan  return class_decl;
2202169689Skan}
220318334Speter
220418334Speterstatic tree
2205132718Skanbuild_vec_delete_1 (tree base, tree maxindex, tree type,
2206132718Skan    special_function_kind auto_delete_vec, int use_global_delete)
220718334Speter{
220818334Speter  tree virtual_size;
220950397Sobrien  tree ptype = build_pointer_type (type = complete_type (type));
221018334Speter  tree size_exp = size_in_bytes (type);
221118334Speter
221218334Speter  /* Temporary variables used by the loop.  */
221318334Speter  tree tbase, tbase_init;
221418334Speter
221518334Speter  /* This is the body of the loop that implements the deletion of a
221618334Speter     single element, and moves temp variables to next elements.  */
221718334Speter  tree body;
221818334Speter
221918334Speter  /* This is the LOOP_EXPR that governs the deletion of the elements.  */
2220132718Skan  tree loop = 0;
222118334Speter
222218334Speter  /* This is the thing that governs what to do after the loop has run.  */
222318334Speter  tree deallocate_expr = 0;
222418334Speter
222518334Speter  /* This is the BIND_EXPR which holds the outermost iterator of the
222618334Speter     loop.  It is convenient to set this variable up and test it before
222718334Speter     executing any other code in the loop.
222818334Speter     This is also the containing expression returned by this function.  */
222918334Speter  tree controller = NULL_TREE;
223018334Speter
2231102780Skan  /* We should only have 1-D arrays here.  */
2232169689Skan  gcc_assert (TREE_CODE (type) != ARRAY_TYPE);
2233102780Skan
223490075Sobrien  if (! IS_AGGR_TYPE (type) || TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
2235132718Skan    goto no_destructor;
223618334Speter
223790075Sobrien  /* The below is short by the cookie size.  */
223890075Sobrien  virtual_size = size_binop (MULT_EXPR, size_exp,
223990075Sobrien			     convert (sizetype, maxindex));
224018334Speter
224190075Sobrien  tbase = create_temporary_var (ptype);
224218334Speter  tbase_init = build_modify_expr (tbase, NOP_EXPR,
2243169689Skan				  fold_build2 (PLUS_EXPR, ptype,
224418334Speter					       base,
2245169689Skan					       virtual_size));
224618334Speter  DECL_REGISTER (tbase) = 1;
2247169689Skan  controller = build3 (BIND_EXPR, void_type_node, tbase,
2248169689Skan		       NULL_TREE, NULL_TREE);
224918334Speter  TREE_SIDE_EFFECTS (controller) = 1;
225018334Speter
2251169689Skan  body = build1 (EXIT_EXPR, void_type_node,
2252169689Skan		 build2 (EQ_EXPR, boolean_type_node, tbase,
2253169689Skan			 fold_convert (ptype, base)));
2254132718Skan  body = build_compound_expr
2255132718Skan    (body, build_modify_expr (tbase, NOP_EXPR,
2256169689Skan			      build2 (MINUS_EXPR, ptype, tbase, size_exp)));
2257132718Skan  body = build_compound_expr
2258132718Skan    (body, build_delete (ptype, tbase, sfk_complete_destructor,
2259132718Skan			 LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 1));
226018334Speter
2261169689Skan  loop = build1 (LOOP_EXPR, void_type_node, body);
2262132718Skan  loop = build_compound_expr (tbase_init, loop);
226318334Speter
226418334Speter no_destructor:
226518334Speter  /* If the delete flag is one, or anything else with the low bit set,
226618334Speter     delete the storage.  */
226790075Sobrien  if (auto_delete_vec != sfk_base_destructor)
226818334Speter    {
226918334Speter      tree base_tbd;
227018334Speter
227190075Sobrien      /* The below is short by the cookie size.  */
227290075Sobrien      virtual_size = size_binop (MULT_EXPR, size_exp,
227390075Sobrien				 convert (sizetype, maxindex));
227418334Speter
227518334Speter      if (! TYPE_VEC_NEW_USES_COOKIE (type))
227618334Speter	/* no header */
227718334Speter	base_tbd = base;
227818334Speter      else
227918334Speter	{
228090075Sobrien	  tree cookie_size;
228190075Sobrien
2282169689Skan	  cookie_size = targetm.cxx.get_cookie_size (type);
2283169689Skan	  base_tbd
228490075Sobrien	    = cp_convert (ptype,
228590075Sobrien			  cp_build_binary_op (MINUS_EXPR,
2286169689Skan					      cp_convert (string_type_node,
228790075Sobrien							  base),
228890075Sobrien					      cookie_size));
228950397Sobrien	  /* True size with header.  */
229090075Sobrien	  virtual_size = size_binop (PLUS_EXPR, virtual_size, cookie_size);
229118334Speter	}
229290075Sobrien
229390075Sobrien      if (auto_delete_vec == sfk_deleting_destructor)
2294169689Skan	deallocate_expr = build_op_delete_call (VEC_DELETE_EXPR,
2295169689Skan						base_tbd, virtual_size,
2296169689Skan						use_global_delete & 1,
2297169689Skan						/*placement=*/NULL_TREE,
2298169689Skan						/*alloc_fn=*/NULL_TREE);
229918334Speter    }
230018334Speter
2301132718Skan  body = loop;
2302132718Skan  if (!deallocate_expr)
2303132718Skan    ;
2304132718Skan  else if (!body)
2305132718Skan    body = deallocate_expr;
230618334Speter  else
2307132718Skan    body = build_compound_expr (body, deallocate_expr);
2308169689Skan
2309132718Skan  if (!body)
2310132718Skan    body = integer_zero_node;
2311169689Skan
231218334Speter  /* Outermost wrapper: If pointer is null, punt.  */
2313169689Skan  body = fold_build3 (COND_EXPR, void_type_node,
2314169689Skan		      fold_build2 (NE_EXPR, boolean_type_node, base,
2315169689Skan				   convert (TREE_TYPE (base),
2316169689Skan					    integer_zero_node)),
2317169689Skan		      body, integer_zero_node);
231818334Speter  body = build1 (NOP_EXPR, void_type_node, body);
231918334Speter
232018334Speter  if (controller)
232118334Speter    {
232218334Speter      TREE_OPERAND (controller, 1) = body;
2323132718Skan      body = controller;
232418334Speter    }
2325132718Skan
2326132718Skan  if (TREE_CODE (base) == SAVE_EXPR)
2327132718Skan    /* Pre-evaluate the SAVE_EXPR outside of the BIND_EXPR.  */
2328169689Skan    body = build2 (COMPOUND_EXPR, void_type_node, base, body);
2329132718Skan
2330132718Skan  return convert_to_void (body, /*implicit=*/NULL);
233118334Speter}
233218334Speter
2333169689Skan/* Create an unnamed variable of the indicated TYPE.  */
233450397Sobrien
233590075Sobrientree
2336132718Skancreate_temporary_var (tree type)
233718334Speter{
233890075Sobrien  tree decl;
2339169689Skan
234090075Sobrien  decl = build_decl (VAR_DECL, NULL_TREE, type);
234190075Sobrien  TREE_USED (decl) = 1;
234290075Sobrien  DECL_ARTIFICIAL (decl) = 1;
2343169689Skan  DECL_IGNORED_P (decl) = 1;
2344132718Skan  DECL_SOURCE_LOCATION (decl) = input_location;
234590075Sobrien  DECL_CONTEXT (decl) = current_function_decl;
234652284Sobrien
234790075Sobrien  return decl;
234890075Sobrien}
234952284Sobrien
235090075Sobrien/* Create a new temporary variable of the indicated TYPE, initialized
235190075Sobrien   to INIT.
235252284Sobrien
235390075Sobrien   It is not entered into current_binding_level, because that breaks
235490075Sobrien   things when it comes time to do final cleanups (which take place
235590075Sobrien   "outside" the binding contour of the function).  */
235652284Sobrien
235790075Sobrienstatic tree
2358132718Skanget_temp_regvar (tree type, tree init)
235952284Sobrien{
236090075Sobrien  tree decl;
236152284Sobrien
236290075Sobrien  decl = create_temporary_var (type);
2363169689Skan  add_decl_expr (decl);
2364169689Skan
236590075Sobrien  finish_expr_stmt (build_modify_expr (decl, INIT_EXPR, init));
236652284Sobrien
236790075Sobrien  return decl;
236852284Sobrien}
236952284Sobrien
237090075Sobrien/* `build_vec_init' returns tree structure that performs
237190075Sobrien   initialization of a vector of aggregate types.
237218334Speter
237390075Sobrien   BASE is a reference to the vector, of ARRAY_TYPE.
2374117395Skan   MAXINDEX is the maximum index of the array (one less than the
2375117395Skan     number of elements).  It is only used if
2376117395Skan     TYPE_DOMAIN (TREE_TYPE (BASE)) == NULL_TREE.
2377169689Skan
237818334Speter   INIT is the (possibly NULL) initializer.
237918334Speter
2380169689Skan   If EXPLICIT_DEFAULT_INIT_P is true, then INIT must be NULL.  All
2381169689Skan   elements in the array are default-initialized.
2382169689Skan
238318334Speter   FROM_ARRAY is 0 if we should init everything with INIT
238418334Speter   (i.e., every element initialized from INIT).
238518334Speter   FROM_ARRAY is 1 if we should index into INIT in parallel
238618334Speter   with initialization of DECL.
238718334Speter   FROM_ARRAY is 2 if we should index into INIT in parallel,
238818334Speter   but use assignment instead of initialization.  */
238918334Speter
239018334Spetertree
2391169689Skanbuild_vec_init (tree base, tree maxindex, tree init,
2392169689Skan		bool explicit_default_init_p,
2393169689Skan		int from_array)
239418334Speter{
239518334Speter  tree rval;
239652284Sobrien  tree base2 = NULL_TREE;
239718334Speter  tree size;
239852284Sobrien  tree itype = NULL_TREE;
239952284Sobrien  tree iterator;
240090075Sobrien  /* The type of the array.  */
240190075Sobrien  tree atype = TREE_TYPE (base);
240290075Sobrien  /* The type of an element in the array.  */
240390075Sobrien  tree type = TREE_TYPE (atype);
2404169689Skan  /* The element type reached after removing all outer array
2405146895Skan     types.  */
2406146895Skan  tree inner_elt_type;
240790075Sobrien  /* The type of a pointer to an element in the array.  */
240890075Sobrien  tree ptype;
240990075Sobrien  tree stmt_expr;
241090075Sobrien  tree compound_stmt;
241190075Sobrien  int destroy_temps;
241290075Sobrien  tree try_block = NULL_TREE;
241352284Sobrien  int num_initialized_elts = 0;
2414132718Skan  bool is_global;
2415169689Skan
2416117395Skan  if (TYPE_DOMAIN (atype))
2417117395Skan    maxindex = array_type_nelts (atype);
2418117395Skan
2419117395Skan  if (maxindex == NULL_TREE || maxindex == error_mark_node)
242018334Speter    return error_mark_node;
242118334Speter
2422169689Skan  if (explicit_default_init_p)
2423169689Skan    gcc_assert (!init);
2424169689Skan
2425146895Skan  inner_elt_type = strip_array_types (atype);
2426117395Skan  if (init
2427117395Skan      && (from_array == 2
2428169689Skan	  ? (!CLASS_TYPE_P (inner_elt_type)
2429146895Skan	     || !TYPE_HAS_COMPLEX_ASSIGN_REF (inner_elt_type))
2430117395Skan	  : !TYPE_NEEDS_CONSTRUCTING (type))
243190075Sobrien      && ((TREE_CODE (init) == CONSTRUCTOR
243290075Sobrien	   /* Don't do this if the CONSTRUCTOR might contain something
243390075Sobrien	      that might throw and require us to clean up.  */
2434169689Skan	   && (VEC_empty (constructor_elt, CONSTRUCTOR_ELTS (init))
2435146895Skan	       || ! TYPE_HAS_NONTRIVIAL_DESTRUCTOR (inner_elt_type)))
243690075Sobrien	  || from_array))
243718334Speter    {
243890075Sobrien      /* Do non-default initialization of POD arrays resulting from
243990075Sobrien	 brace-enclosed initializers.  In this case, digest_init and
244090075Sobrien	 store_constructor will handle the semantics for us.  */
244190075Sobrien
2442169689Skan      stmt_expr = build2 (INIT_EXPR, atype, base, init);
244390075Sobrien      return stmt_expr;
244418334Speter    }
244518334Speter
244690075Sobrien  maxindex = cp_convert (ptrdiff_type_node, maxindex);
244790075Sobrien  ptype = build_pointer_type (type);
244818334Speter  size = size_in_bytes (type);
244990075Sobrien  if (TREE_CODE (TREE_TYPE (base)) == ARRAY_TYPE)
2450132718Skan    base = cp_convert (ptype, decay_conversion (base));
245118334Speter
245290075Sobrien  /* The code we are generating looks like:
2453132718Skan     ({
245490075Sobrien       T* t1 = (T*) base;
245590075Sobrien       T* rval = t1;
245690075Sobrien       ptrdiff_t iterator = maxindex;
245790075Sobrien       try {
2458132718Skan	 for (; iterator != -1; --iterator) {
245990075Sobrien	   ... initialize *t1 ...
246090075Sobrien	   ++t1;
2461132718Skan	 }
246290075Sobrien       } catch (...) {
2463169689Skan	 ... destroy elements that were constructed ...
246490075Sobrien       }
2465132718Skan       rval;
2466132718Skan     })
2467169689Skan
246890075Sobrien     We can omit the try and catch blocks if we know that the
246990075Sobrien     initialization will never throw an exception, or if the array
247090075Sobrien     elements do not have destructors.  We can omit the loop completely if
2471169689Skan     the elements of the array do not have constructors.
247290075Sobrien
247390075Sobrien     We actually wrap the entire body of the above in a STMT_EXPR, for
2474169689Skan     tidiness.
247590075Sobrien
247690075Sobrien     When copying from array to another, when the array elements have
247790075Sobrien     only trivial copy constructors, we should use __builtin_memcpy
247890075Sobrien     rather than generating a loop.  That way, we could take advantage
247990075Sobrien     of whatever cleverness the back-end has for dealing with copies
248090075Sobrien     of blocks of memory.  */
248190075Sobrien
2482132718Skan  is_global = begin_init_stmts (&stmt_expr, &compound_stmt);
248390075Sobrien  destroy_temps = stmts_are_full_exprs_p ();
248490075Sobrien  current_stmt_tree ()->stmts_are_full_exprs_p = 0;
248590075Sobrien  rval = get_temp_regvar (ptype, base);
248690075Sobrien  base = get_temp_regvar (ptype, rval);
248752284Sobrien  iterator = get_temp_regvar (ptrdiff_type_node, maxindex);
248818334Speter
248952284Sobrien  /* Protect the entire array initialization so that we can destroy
249090075Sobrien     the partially constructed array if an exception is thrown.
249190075Sobrien     But don't do this if we're assigning.  */
249290075Sobrien  if (flag_exceptions && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
249390075Sobrien      && from_array != 2)
249490075Sobrien    {
249590075Sobrien      try_block = begin_try_block ();
249690075Sobrien    }
249752284Sobrien
249890075Sobrien  if (init != NULL_TREE && TREE_CODE (init) == CONSTRUCTOR)
249918334Speter    {
250090075Sobrien      /* Do non-default initialization of non-POD arrays resulting from
250190075Sobrien	 brace-enclosed initializers.  */
2502169689Skan      unsigned HOST_WIDE_INT idx;
2503169689Skan      tree elt;
250452284Sobrien      from_array = 0;
250552284Sobrien
2506169689Skan      FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (init), idx, elt)
250718334Speter	{
250890075Sobrien	  tree baseref = build1 (INDIRECT_REF, type, base);
250918334Speter
251052284Sobrien	  num_initialized_elts++;
251118334Speter
2512132718Skan	  current_stmt_tree ()->stmts_are_full_exprs_p = 1;
251352284Sobrien	  if (IS_AGGR_TYPE (type) || TREE_CODE (type) == ARRAY_TYPE)
251490075Sobrien	    finish_expr_stmt (build_aggr_init (baseref, elt, 0));
251552284Sobrien	  else
251690075Sobrien	    finish_expr_stmt (build_modify_expr (baseref, NOP_EXPR,
251790075Sobrien						 elt));
2518132718Skan	  current_stmt_tree ()->stmts_are_full_exprs_p = 0;
251952284Sobrien
252090075Sobrien	  finish_expr_stmt (build_unary_op (PREINCREMENT_EXPR, base, 0));
252190075Sobrien	  finish_expr_stmt (build_unary_op (PREDECREMENT_EXPR, iterator, 0));
252218334Speter	}
252318334Speter
252452284Sobrien      /* Clear out INIT so that we don't get confused below.  */
252552284Sobrien      init = NULL_TREE;
252618334Speter    }
252752284Sobrien  else if (from_array)
252818334Speter    {
252952284Sobrien      /* If initializing one array from another, initialize element by
253052284Sobrien	 element.  We rely upon the below calls the do argument
2531169689Skan	 checking.  */
253252284Sobrien      if (init)
253352284Sobrien	{
2534132718Skan	  base2 = decay_conversion (init);
253552284Sobrien	  itype = TREE_TYPE (base2);
253652284Sobrien	  base2 = get_temp_regvar (itype, base2);
253752284Sobrien	  itype = TREE_TYPE (itype);
253852284Sobrien	}
253952284Sobrien      else if (TYPE_LANG_SPECIFIC (type)
254052284Sobrien	       && TYPE_NEEDS_CONSTRUCTING (type)
254152284Sobrien	       && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
254252284Sobrien	{
254352284Sobrien	  error ("initializer ends prematurely");
254452284Sobrien	  return error_mark_node;
254552284Sobrien	}
254652284Sobrien    }
254718334Speter
254852284Sobrien  /* Now, default-initialize any remaining elements.  We don't need to
254952284Sobrien     do that if a) the type does not need constructing, or b) we've
255052284Sobrien     already initialized all the elements.
255118334Speter
255252284Sobrien     We do need to keep going if we're copying an array.  */
255318334Speter
255452284Sobrien  if (from_array
2555169689Skan      || ((TYPE_NEEDS_CONSTRUCTING (type) || explicit_default_init_p)
255690075Sobrien	  && ! (host_integerp (maxindex, 0)
255790075Sobrien		&& (num_initialized_elts
255890075Sobrien		    == tree_low_cst (maxindex, 0) + 1))))
255952284Sobrien    {
256052284Sobrien      /* If the ITERATOR is equal to -1, then we don't have to loop;
256152284Sobrien	 we've already initialized all the elements.  */
2562132718Skan      tree for_stmt;
256390075Sobrien      tree elt_init;
2564169689Skan      tree to;
256518334Speter
2566132718Skan      for_stmt = begin_for_stmt ();
2567132718Skan      finish_for_init_stmt (for_stmt);
2568169689Skan      finish_for_cond (build2 (NE_EXPR, boolean_type_node, iterator,
2569169689Skan			       build_int_cst (TREE_TYPE (iterator), -1)),
2570132718Skan		       for_stmt);
2571132718Skan      finish_for_expr (build_unary_op (PREDECREMENT_EXPR, iterator, 0),
2572132718Skan		       for_stmt);
257390075Sobrien
2574169689Skan      to = build1 (INDIRECT_REF, type, base);
257550397Sobrien
257618334Speter      if (from_array)
257718334Speter	{
257818334Speter	  tree from;
257918334Speter
258018334Speter	  if (base2)
258118334Speter	    from = build1 (INDIRECT_REF, itype, base2);
258218334Speter	  else
258318334Speter	    from = NULL_TREE;
258418334Speter
258518334Speter	  if (from_array == 2)
258690075Sobrien	    elt_init = build_modify_expr (to, NOP_EXPR, from);
258718334Speter	  else if (TYPE_NEEDS_CONSTRUCTING (type))
258890075Sobrien	    elt_init = build_aggr_init (to, from, 0);
258918334Speter	  else if (from)
259090075Sobrien	    elt_init = build_modify_expr (to, NOP_EXPR, from);
259118334Speter	  else
2592169689Skan	    gcc_unreachable ();
259318334Speter	}
259418334Speter      else if (TREE_CODE (type) == ARRAY_TYPE)
259518334Speter	{
259618334Speter	  if (init != 0)
259790075Sobrien	    sorry
259890075Sobrien	      ("cannot initialize multi-dimensional array with initializer");
259990075Sobrien	  elt_init = build_vec_init (build1 (INDIRECT_REF, type, base),
2600169689Skan				     0, 0,
2601169689Skan				     /*explicit_default_init_p=*/false,
2602169689Skan				     0);
260318334Speter	}
2604169689Skan      else if (!TYPE_NEEDS_CONSTRUCTING (type))
2605169689Skan	elt_init = (build_modify_expr
2606169689Skan		    (to, INIT_EXPR,
2607169689Skan		     build_zero_init (type, size_one_node,
2608169689Skan				      /*static_storage_p=*/false)));
260918334Speter      else
2610169689Skan	elt_init = build_aggr_init (to, init, 0);
2611169689Skan
2612132718Skan      current_stmt_tree ()->stmts_are_full_exprs_p = 1;
2613132718Skan      finish_expr_stmt (elt_init);
2614132718Skan      current_stmt_tree ()->stmts_are_full_exprs_p = 0;
261518334Speter
261690075Sobrien      finish_expr_stmt (build_unary_op (PREINCREMENT_EXPR, base, 0));
261718334Speter      if (base2)
261890075Sobrien	finish_expr_stmt (build_unary_op (PREINCREMENT_EXPR, base2, 0));
261950397Sobrien
2620132718Skan      finish_for_stmt (for_stmt);
262152284Sobrien    }
262250397Sobrien
262352284Sobrien  /* Make sure to cleanup any partially constructed elements.  */
262490075Sobrien  if (flag_exceptions && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
262590075Sobrien      && from_array != 2)
262690075Sobrien    {
262790075Sobrien      tree e;
2628102780Skan      tree m = cp_build_binary_op (MINUS_EXPR, maxindex, iterator);
262950397Sobrien
2630102780Skan      /* Flatten multi-dimensional array since build_vec_delete only
2631102780Skan	 expects one-dimensional array.  */
2632102780Skan      if (TREE_CODE (type) == ARRAY_TYPE)
2633169689Skan	m = cp_build_binary_op (MULT_EXPR, m,
2634169689Skan				array_type_nelts_total (type));
2635102780Skan
263690075Sobrien      finish_cleanup_try_block (try_block);
2637169689Skan      e = build_vec_delete_1 (rval, m,
2638169689Skan			      inner_elt_type, sfk_base_destructor,
263990075Sobrien			      /*use_global_delete=*/0);
264090075Sobrien      finish_cleanup (e, try_block);
264118334Speter    }
264218334Speter
2643132718Skan  /* The value of the array initialization is the array itself, RVAL
2644132718Skan     is a pointer to the first element.  */
2645169689Skan  finish_stmt_expr_expr (rval, stmt_expr);
264690075Sobrien
2647132718Skan  stmt_expr = finish_init_stmts (is_global, stmt_expr, compound_stmt);
2648132718Skan
2649132718Skan  /* Now convert make the result have the correct type.  */
2650132718Skan  atype = build_pointer_type (atype);
2651132718Skan  stmt_expr = build1 (NOP_EXPR, atype, stmt_expr);
2652132718Skan  stmt_expr = build_indirect_ref (stmt_expr, NULL);
2653169689Skan
265490075Sobrien  current_stmt_tree ()->stmts_are_full_exprs_p = destroy_temps;
265590075Sobrien  return stmt_expr;
265618334Speter}
265718334Speter
265890075Sobrien/* Call the DTOR_KIND destructor for EXP.  FLAGS are as for
265990075Sobrien   build_delete.  */
266090075Sobrien
266190075Sobrienstatic tree
2662132718Skanbuild_dtor_call (tree exp, special_function_kind dtor_kind, int flags)
266390075Sobrien{
266490075Sobrien  tree name;
2665132718Skan  tree fn;
266690075Sobrien  switch (dtor_kind)
266790075Sobrien    {
266890075Sobrien    case sfk_complete_destructor:
266990075Sobrien      name = complete_dtor_identifier;
267090075Sobrien      break;
267190075Sobrien
267290075Sobrien    case sfk_base_destructor:
267390075Sobrien      name = base_dtor_identifier;
267490075Sobrien      break;
267590075Sobrien
267690075Sobrien    case sfk_deleting_destructor:
267790075Sobrien      name = deleting_dtor_identifier;
267890075Sobrien      break;
267990075Sobrien
268090075Sobrien    default:
2681169689Skan      gcc_unreachable ();
268290075Sobrien    }
2683132718Skan  fn = lookup_fnfields (TREE_TYPE (exp), name, /*protect=*/2);
2684169689Skan  return build_new_method_call (exp, fn,
2685132718Skan				/*args=*/NULL_TREE,
2686132718Skan				/*conversion_path=*/NULL_TREE,
2687169689Skan				flags,
2688169689Skan				/*fn_p=*/NULL);
268990075Sobrien}
269090075Sobrien
269118334Speter/* Generate a call to a destructor. TYPE is the type to cast ADDR to.
269218334Speter   ADDR is an expression which yields the store to be destroyed.
269390075Sobrien   AUTO_DELETE is the name of the destructor to call, i.e., either
269490075Sobrien   sfk_complete_destructor, sfk_base_destructor, or
269590075Sobrien   sfk_deleting_destructor.
269618334Speter
269718334Speter   FLAGS is the logical disjunction of zero or more LOOKUP_
269890075Sobrien   flags.  See cp-tree.h for more info.  */
269918334Speter
270018334Spetertree
2701132718Skanbuild_delete (tree type, tree addr, special_function_kind auto_delete,
2702132718Skan    int flags, int use_global_delete)
270318334Speter{
270418334Speter  tree expr;
270518334Speter
270618334Speter  if (addr == error_mark_node)
270718334Speter    return error_mark_node;
270818334Speter
270918334Speter  /* Can happen when CURRENT_EXCEPTION_OBJECT gets its type
271018334Speter     set to `error_mark_node' before it gets properly cleaned up.  */
271118334Speter  if (type == error_mark_node)
271218334Speter    return error_mark_node;
271318334Speter
271418334Speter  type = TYPE_MAIN_VARIANT (type);
271518334Speter
271618334Speter  if (TREE_CODE (type) == POINTER_TYPE)
271718334Speter    {
2718132718Skan      bool complete_p = true;
2719132718Skan
272018334Speter      type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
272118334Speter      if (TREE_CODE (type) == ARRAY_TYPE)
272218334Speter	goto handle_array;
2723117395Skan
2724132718Skan      /* We don't want to warn about delete of void*, only other
2725132718Skan	  incomplete types.  Deleting other incomplete types
2726132718Skan	  invokes undefined behavior, but it is not ill-formed, so
2727132718Skan	  compile to something that would even do The Right Thing
2728132718Skan	  (TM) should the type have a trivial dtor and no delete
2729132718Skan	  operator.  */
2730132718Skan      if (!VOID_TYPE_P (type))
273118334Speter	{
2732132718Skan	  complete_type (type);
2733132718Skan	  if (!COMPLETE_TYPE_P (type))
2734132718Skan	    {
2735169689Skan	      warning (0, "possible problem detected in invocation of "
2736132718Skan		       "delete operator:");
2737132718Skan	      cxx_incomplete_type_diagnostic (addr, type, 1);
2738132718Skan	      inform ("neither the destructor nor the class-specific "
2739132718Skan		      "operator delete will be called, even if they are "
2740132718Skan		      "declared when the class is defined.");
2741132718Skan	      complete_p = false;
2742132718Skan	    }
274318334Speter	}
2744132718Skan      if (VOID_TYPE_P (type) || !complete_p || !IS_AGGR_TYPE (type))
2745132718Skan	/* Call the builtin operator delete.  */
2746132718Skan	return build_builtin_delete_call (addr);
274718334Speter      if (TREE_SIDE_EFFECTS (addr))
274818334Speter	addr = save_expr (addr);
274918334Speter
2750132718Skan      /* Throw away const and volatile on target type of addr.  */
275118334Speter      addr = convert_force (build_pointer_type (type), addr, 0);
275218334Speter    }
275318334Speter  else if (TREE_CODE (type) == ARRAY_TYPE)
275418334Speter    {
275518334Speter    handle_array:
2756169689Skan
275718334Speter      if (TYPE_DOMAIN (type) == NULL_TREE)
275818334Speter	{
275918334Speter	  error ("unknown array size in delete");
276018334Speter	  return error_mark_node;
276118334Speter	}
276218334Speter      return build_vec_delete (addr, array_type_nelts (type),
276390075Sobrien			       auto_delete, use_global_delete);
276418334Speter    }
276518334Speter  else
276618334Speter    {
276718334Speter      /* Don't check PROTECT here; leave that decision to the
276818334Speter	 destructor.  If the destructor is accessible, call it,
276918334Speter	 else report error.  */
277018334Speter      addr = build_unary_op (ADDR_EXPR, addr, 0);
277118334Speter      if (TREE_SIDE_EFFECTS (addr))
277218334Speter	addr = save_expr (addr);
277318334Speter
277490075Sobrien      addr = convert_force (build_pointer_type (type), addr, 0);
277518334Speter    }
277618334Speter
2777169689Skan  gcc_assert (IS_AGGR_TYPE (type));
277818334Speter
277990075Sobrien  if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
278018334Speter    {
278190075Sobrien      if (auto_delete != sfk_deleting_destructor)
278218334Speter	return void_zero_node;
278318334Speter
2784169689Skan      return build_op_delete_call (DELETE_EXPR, addr,
2785169689Skan				   cxx_sizeof_nowarn (type),
2786169689Skan				   use_global_delete,
2787169689Skan				   /*placement=*/NULL_TREE,
2788169689Skan				   /*alloc_fn=*/NULL_TREE);
278918334Speter    }
279090075Sobrien  else
279118334Speter    {
279218334Speter      tree do_delete = NULL_TREE;
279350397Sobrien      tree ifexp;
279418334Speter
2795169689Skan      if (CLASSTYPE_LAZY_DESTRUCTOR (type))
2796169689Skan	lazily_declare_fn (sfk_destructor, type);
279790075Sobrien
279890075Sobrien      /* For `::delete x', we must not use the deleting destructor
279990075Sobrien	 since then we would not be sure to get the global `operator
280090075Sobrien	 delete'.  */
280190075Sobrien      if (use_global_delete && auto_delete == sfk_deleting_destructor)
280218334Speter	{
280390075Sobrien	  /* We will use ADDR multiple times so we must save it.  */
280490075Sobrien	  addr = save_expr (addr);
2805117395Skan	  /* Delete the object.  */
280690075Sobrien	  do_delete = build_builtin_delete_call (addr);
280790075Sobrien	  /* Otherwise, treat this like a complete object destructor
280890075Sobrien	     call.  */
280990075Sobrien	  auto_delete = sfk_complete_destructor;
281018334Speter	}
281190075Sobrien      /* If the destructor is non-virtual, there is no deleting
281290075Sobrien	 variant.  Instead, we must explicitly call the appropriate
281390075Sobrien	 `operator delete' here.  */
281490075Sobrien      else if (!DECL_VIRTUAL_P (CLASSTYPE_DESTRUCTORS (type))
281590075Sobrien	       && auto_delete == sfk_deleting_destructor)
281660967Sobrien	{
281790075Sobrien	  /* We will use ADDR multiple times so we must save it.  */
281890075Sobrien	  addr = save_expr (addr);
281990075Sobrien	  /* Build the call.  */
282090075Sobrien	  do_delete = build_op_delete_call (DELETE_EXPR,
282190075Sobrien					    addr,
2822117395Skan					    cxx_sizeof_nowarn (type),
2823169689Skan					    /*global_p=*/false,
2824169689Skan					    /*placement=*/NULL_TREE,
2825169689Skan					    /*alloc_fn=*/NULL_TREE);
282690075Sobrien	  /* Call the complete object destructor.  */
282790075Sobrien	  auto_delete = sfk_complete_destructor;
282860967Sobrien	}
282996263Sobrien      else if (auto_delete == sfk_deleting_destructor
283096263Sobrien	       && TYPE_GETS_REG_DELETE (type))
283196263Sobrien	{
283296263Sobrien	  /* Make sure we have access to the member op delete, even though
283396263Sobrien	     we'll actually be calling it from the destructor.  */
2834117395Skan	  build_op_delete_call (DELETE_EXPR, addr, cxx_sizeof_nowarn (type),
2835169689Skan				/*global_p=*/false,
2836169689Skan				/*placement=*/NULL_TREE,
2837169689Skan				/*alloc_fn=*/NULL_TREE);
283896263Sobrien	}
283918334Speter
284090075Sobrien      expr = build_dtor_call (build_indirect_ref (addr, NULL),
284190075Sobrien			      auto_delete, flags);
284250397Sobrien      if (do_delete)
2843169689Skan	expr = build2 (COMPOUND_EXPR, void_type_node, expr, do_delete);
284418334Speter
284550397Sobrien      if (flags & LOOKUP_DESTRUCTOR)
284650397Sobrien	/* Explicit destructor call; don't check for null pointer.  */
284750397Sobrien	ifexp = integer_one_node;
284818334Speter      else
284950397Sobrien	/* Handle deleting a null pointer.  */
285090075Sobrien	ifexp = fold (cp_build_binary_op (NE_EXPR, addr, integer_zero_node));
285118334Speter
285250397Sobrien      if (ifexp != integer_one_node)
2853169689Skan	expr = build3 (COND_EXPR, void_type_node,
2854169689Skan		       ifexp, expr, void_zero_node);
285518334Speter
285618334Speter      return expr;
285718334Speter    }
285890075Sobrien}
285918334Speter
2860117395Skan/* At the beginning of a destructor, push cleanups that will call the
2861117395Skan   destructors for our base classes and members.
286252284Sobrien
2863117395Skan   Called from begin_destructor_body.  */
286418334Speter
286590075Sobrienvoid
2866132718Skanpush_base_cleanups (void)
286790075Sobrien{
2868169689Skan  tree binfo, base_binfo;
2869169689Skan  int i;
287090075Sobrien  tree member;
287190075Sobrien  tree expr;
2872169689Skan  VEC(tree,gc) *vbases;
287318334Speter
287490075Sobrien  /* Run destructors for all virtual baseclasses.  */
2875169689Skan  if (CLASSTYPE_VBASECLASSES (current_class_type))
287690075Sobrien    {
287790075Sobrien      tree cond = (condition_conversion
2878169689Skan		   (build2 (BIT_AND_EXPR, integer_type_node,
2879169689Skan			    current_in_charge_parm,
2880169689Skan			    integer_two_node)));
288190075Sobrien
2882169689Skan      /* The CLASSTYPE_VBASECLASSES vector is in initialization
288390075Sobrien	 order, which is also the right order for pushing cleanups.  */
2884169689Skan      for (vbases = CLASSTYPE_VBASECLASSES (current_class_type), i = 0;
2885169689Skan	   VEC_iterate (tree, vbases, i, base_binfo); i++)
288618334Speter	{
2887169689Skan	  if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (BINFO_TYPE (base_binfo)))
288890075Sobrien	    {
2889169689Skan	      expr = build_special_member_call (current_class_ref,
2890117395Skan						base_dtor_identifier,
2891117395Skan						NULL_TREE,
2892169689Skan						base_binfo,
2893169689Skan						(LOOKUP_NORMAL
2894117395Skan						 | LOOKUP_NONVIRTUAL));
2895169689Skan	      expr = build3 (COND_EXPR, void_type_node, cond,
2896169689Skan			     expr, void_zero_node);
2897117395Skan	      finish_decl_cleanup (NULL_TREE, expr);
289890075Sobrien	    }
289918334Speter	}
290090075Sobrien    }
2901117395Skan
2902117395Skan  /* Take care of the remaining baseclasses.  */
2903169689Skan  for (binfo = TYPE_BINFO (current_class_type), i = 0;
2904169689Skan       BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
2905117395Skan    {
2906117395Skan      if (TYPE_HAS_TRIVIAL_DESTRUCTOR (BINFO_TYPE (base_binfo))
2907169689Skan	  || BINFO_VIRTUAL_P (base_binfo))
2908117395Skan	continue;
2909117395Skan
2910169689Skan      expr = build_special_member_call (current_class_ref,
2911117395Skan					base_dtor_identifier,
2912169689Skan					NULL_TREE, base_binfo,
2913117395Skan					LOOKUP_NORMAL | LOOKUP_NONVIRTUAL);
2914117395Skan      finish_decl_cleanup (NULL_TREE, expr);
2915117395Skan    }
2916117395Skan
2917117395Skan  for (member = TYPE_FIELDS (current_class_type); member;
2918117395Skan       member = TREE_CHAIN (member))
2919117395Skan    {
2920169689Skan      if (TREE_TYPE (member) == error_mark_node
2921169689Skan	  || TREE_CODE (member) != FIELD_DECL
2922169689Skan	  || DECL_ARTIFICIAL (member))
2923117395Skan	continue;
2924117395Skan      if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (member)))
2925117395Skan	{
2926169689Skan	  tree this_member = (build_class_member_access_expr
2927169689Skan			      (current_class_ref, member,
2928117395Skan			       /*access_path=*/NULL_TREE,
2929117395Skan			       /*preserve_reference=*/false));
2930117395Skan	  tree this_type = TREE_TYPE (member);
2931117395Skan	  expr = build_delete (this_type, this_member,
2932117395Skan			       sfk_complete_destructor,
2933117395Skan			       LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR|LOOKUP_NORMAL,
2934117395Skan			       0);
2935117395Skan	  finish_decl_cleanup (NULL_TREE, expr);
2936117395Skan	}
2937117395Skan    }
293818334Speter}
293918334Speter
294018334Speter/* Build a C++ vector delete expression.
294118334Speter   MAXINDEX is the number of elements to be deleted.
294218334Speter   ELT_SIZE is the nominal size of each element in the vector.
294318334Speter   BASE is the expression that should yield the store to be deleted.
294418334Speter   This function expands (or synthesizes) these calls itself.
294518334Speter   AUTO_DELETE_VEC says whether the container (vector) should be deallocated.
294618334Speter
294718334Speter   This also calls delete for virtual baseclasses of elements of the vector.
294818334Speter
294918334Speter   Update: MAXINDEX is no longer needed.  The size can be extracted from the
295018334Speter   start of the vector for pointers, and from the type for arrays.  We still
295118334Speter   use MAXINDEX for arrays because it happens to already have one of the
295218334Speter   values we'd have to extract.  (We could use MAXINDEX with pointers to
295318334Speter   confirm the size, and trap if the numbers differ; not clear that it'd
295418334Speter   be worth bothering.)  */
295550397Sobrien
295618334Spetertree
2957132718Skanbuild_vec_delete (tree base, tree maxindex,
2958132718Skan    special_function_kind auto_delete_vec, int use_global_delete)
295918334Speter{
296018334Speter  tree type;
2961132718Skan  tree rval;
2962132718Skan  tree base_init = NULL_TREE;
296318334Speter
296418334Speter  type = TREE_TYPE (base);
296518334Speter
296618334Speter  if (TREE_CODE (type) == POINTER_TYPE)
296718334Speter    {
296818334Speter      /* Step back one from start of vector, and read dimension.  */
296990075Sobrien      tree cookie_addr;
297090075Sobrien
2971117395Skan      if (TREE_SIDE_EFFECTS (base))
2972132718Skan	{
2973132718Skan	  base_init = get_target_expr (base);
2974132718Skan	  base = TARGET_EXPR_SLOT (base_init);
2975132718Skan	}
297690075Sobrien      type = strip_array_types (TREE_TYPE (type));
2977169689Skan      cookie_addr = build2 (MINUS_EXPR,
2978169689Skan			    build_pointer_type (sizetype),
2979169689Skan			    base,
2980169689Skan			    TYPE_SIZE_UNIT (sizetype));
298190075Sobrien      maxindex = build_indirect_ref (cookie_addr, NULL);
298218334Speter    }
298318334Speter  else if (TREE_CODE (type) == ARRAY_TYPE)
298418334Speter    {
2985132718Skan      /* Get the total number of things in the array, maxindex is a
2986132718Skan	 bad name.  */
298718334Speter      maxindex = array_type_nelts_total (type);
298890075Sobrien      type = strip_array_types (type);
298918334Speter      base = build_unary_op (ADDR_EXPR, base, 1);
2990117395Skan      if (TREE_SIDE_EFFECTS (base))
2991132718Skan	{
2992132718Skan	  base_init = get_target_expr (base);
2993132718Skan	  base = TARGET_EXPR_SLOT (base_init);
2994132718Skan	}
299518334Speter    }
299618334Speter  else
299718334Speter    {
299850397Sobrien      if (base != error_mark_node)
299950397Sobrien	error ("type to vector delete is neither pointer or array type");
300018334Speter      return error_mark_node;
300118334Speter    }
300218334Speter
3003132718Skan  rval = build_vec_delete_1 (base, maxindex, type, auto_delete_vec,
300418334Speter			     use_global_delete);
3005132718Skan  if (base_init)
3006169689Skan    rval = build2 (COMPOUND_EXPR, TREE_TYPE (rval), base_init, rval);
3007132718Skan
3008132718Skan  return rval;
300918334Speter}
3010