1/* Handle initialization things in C++.
2   Copyright (C) 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3   1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
4   Free Software Foundation, Inc.
5   Contributed by Michael Tiemann (tiemann@cygnus.com)
6
7This file is part of GCC.
8
9GCC is free software; you can redistribute it and/or modify
10it under the terms of the GNU General Public License as published by
11the Free Software Foundation; either version 3, or (at your option)
12any later version.
13
14GCC is distributed in the hope that it will be useful,
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
20along with GCC; see the file COPYING3.  If not see
21<http://www.gnu.org/licenses/>.  */
22
23/* High-level class interface.  */
24
25#include "config.h"
26#include "system.h"
27#include "coretypes.h"
28#include "tm.h"
29#include "tree.h"
30#include "rtl.h"
31#include "expr.h"
32#include "cp-tree.h"
33#include "flags.h"
34#include "output.h"
35#include "except.h"
36#include "toplev.h"
37#include "target.h"
38
39static bool begin_init_stmts (tree *, tree *);
40static tree finish_init_stmts (bool, tree, tree);
41static void construct_virtual_base (tree, tree);
42static void expand_aggr_init_1 (tree, tree, tree, tree, int, tsubst_flags_t);
43static void expand_default_init (tree, tree, tree, tree, int, tsubst_flags_t);
44static tree build_vec_delete_1 (tree, tree, tree, special_function_kind, int);
45static void perform_member_init (tree, tree);
46static tree build_builtin_delete_call (tree);
47static int member_init_ok_or_else (tree, tree, tree);
48static void expand_virtual_init (tree, tree);
49static tree sort_mem_initializers (tree, tree);
50static tree initializing_context (tree);
51static void expand_cleanup_for_base (tree, tree);
52static tree get_temp_regvar (tree, tree);
53static tree dfs_initialize_vtbl_ptrs (tree, void *);
54static tree build_dtor_call (tree, special_function_kind, int);
55static tree build_field_list (tree, tree, int *);
56static tree build_vtbl_address (tree);
57
58/* We are about to generate some complex initialization code.
59   Conceptually, it is all a single expression.  However, we may want
60   to include conditionals, loops, and other such statement-level
61   constructs.  Therefore, we build the initialization code inside a
62   statement-expression.  This function starts such an expression.
63   STMT_EXPR_P and COMPOUND_STMT_P are filled in by this function;
64   pass them back to finish_init_stmts when the expression is
65   complete.  */
66
67static bool
68begin_init_stmts (tree *stmt_expr_p, tree *compound_stmt_p)
69{
70  bool is_global = !building_stmt_tree ();
71
72  *stmt_expr_p = begin_stmt_expr ();
73  *compound_stmt_p = begin_compound_stmt (BCS_NO_SCOPE);
74
75  return is_global;
76}
77
78/* Finish out the statement-expression begun by the previous call to
79   begin_init_stmts.  Returns the statement-expression itself.  */
80
81static tree
82finish_init_stmts (bool is_global, tree stmt_expr, tree compound_stmt)
83{
84  finish_compound_stmt (compound_stmt);
85
86  stmt_expr = finish_stmt_expr (stmt_expr, true);
87
88  gcc_assert (!building_stmt_tree () == is_global);
89
90  return stmt_expr;
91}
92
93/* Constructors */
94
95/* Called from initialize_vtbl_ptrs via dfs_walk.  BINFO is the base
96   which we want to initialize the vtable pointer for, DATA is
97   TREE_LIST whose TREE_VALUE is the this ptr expression.  */
98
99static tree
100dfs_initialize_vtbl_ptrs (tree binfo, void *data)
101{
102  if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
103    return dfs_skip_bases;
104
105  if (!BINFO_PRIMARY_P (binfo) || BINFO_VIRTUAL_P (binfo))
106    {
107      tree base_ptr = TREE_VALUE ((tree) data);
108
109      base_ptr = build_base_path (PLUS_EXPR, base_ptr, binfo, /*nonnull=*/1);
110
111      expand_virtual_init (binfo, base_ptr);
112    }
113
114  return NULL_TREE;
115}
116
117/* Initialize all the vtable pointers in the object pointed to by
118   ADDR.  */
119
120void
121initialize_vtbl_ptrs (tree addr)
122{
123  tree list;
124  tree type;
125
126  type = TREE_TYPE (TREE_TYPE (addr));
127  list = build_tree_list (type, addr);
128
129  /* Walk through the hierarchy, initializing the vptr in each base
130     class.  We do these in pre-order because we can't find the virtual
131     bases for a class until we've initialized the vtbl for that
132     class.  */
133  dfs_walk_once (TYPE_BINFO (type), dfs_initialize_vtbl_ptrs, NULL, list);
134}
135
136/* Return an expression for the zero-initialization of an object with
137   type T.  This expression will either be a constant (in the case
138   that T is a scalar), or a CONSTRUCTOR (in the case that T is an
139   aggregate), or NULL (in the case that T does not require
140   initialization).  In either case, the value can be used as
141   DECL_INITIAL for a decl of the indicated TYPE; it is a valid static
142   initializer. If NELTS is non-NULL, and TYPE is an ARRAY_TYPE, NELTS
143   is the number of elements in the array.  If STATIC_STORAGE_P is
144   TRUE, initializers are only generated for entities for which
145   zero-initialization does not simply mean filling the storage with
146   zero bytes.  FIELD_SIZE, if non-NULL, is the bit size of the field,
147   subfields with bit positions at or above that bit size shouldn't
148   be added.  */
149
150static tree
151build_zero_init_1 (tree type, tree nelts, bool static_storage_p,
152		   tree field_size)
153{
154  tree init = NULL_TREE;
155
156  /* [dcl.init]
157
158     To zero-initialize an object of type T means:
159
160     -- if T is a scalar type, the storage is set to the value of zero
161	converted to T.
162
163     -- if T is a non-union class type, the storage for each nonstatic
164	data member and each base-class subobject is zero-initialized.
165
166     -- if T is a union type, the storage for its first data member is
167	zero-initialized.
168
169     -- if T is an array type, the storage for each element is
170	zero-initialized.
171
172     -- if T is a reference type, no initialization is performed.  */
173
174  gcc_assert (nelts == NULL_TREE || TREE_CODE (nelts) == INTEGER_CST);
175
176  if (type == error_mark_node)
177    ;
178  else if (static_storage_p && zero_init_p (type))
179    /* In order to save space, we do not explicitly build initializers
180       for items that do not need them.  GCC's semantics are that
181       items with static storage duration that are not otherwise
182       initialized are initialized to zero.  */
183    ;
184  else if (SCALAR_TYPE_P (type))
185    init = convert (type, integer_zero_node);
186  else if (CLASS_TYPE_P (type))
187    {
188      tree field;
189      VEC(constructor_elt,gc) *v = NULL;
190
191      /* Iterate over the fields, building initializations.  */
192      for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
193	{
194	  if (TREE_CODE (field) != FIELD_DECL)
195	    continue;
196
197	  /* Don't add virtual bases for base classes if they are beyond
198	     the size of the current field, that means it is present
199	     somewhere else in the object.  */
200	  if (field_size)
201	    {
202	      tree bitpos = bit_position (field);
203	      if (TREE_CODE (bitpos) == INTEGER_CST
204		  && !tree_int_cst_lt (bitpos, field_size))
205		continue;
206	    }
207
208	  /* Note that for class types there will be FIELD_DECLs
209	     corresponding to base classes as well.  Thus, iterating
210	     over TYPE_FIELDs will result in correct initialization of
211	     all of the subobjects.  */
212	  if (!static_storage_p || !zero_init_p (TREE_TYPE (field)))
213	    {
214	      tree new_field_size
215		= (DECL_FIELD_IS_BASE (field)
216		   && DECL_SIZE (field)
217		   && TREE_CODE (DECL_SIZE (field)) == INTEGER_CST)
218		  ? DECL_SIZE (field) : NULL_TREE;
219	      tree value = build_zero_init_1 (TREE_TYPE (field),
220					      /*nelts=*/NULL_TREE,
221					      static_storage_p,
222					      new_field_size);
223	      if (value)
224		CONSTRUCTOR_APPEND_ELT(v, field, value);
225	    }
226
227	  /* For unions, only the first field is initialized.  */
228	  if (TREE_CODE (type) == UNION_TYPE)
229	    break;
230	}
231
232      /* Build a constructor to contain the initializations.  */
233      init = build_constructor (type, v);
234    }
235  else if (TREE_CODE (type) == ARRAY_TYPE)
236    {
237      tree max_index;
238      VEC(constructor_elt,gc) *v = NULL;
239
240      /* Iterate over the array elements, building initializations.  */
241      if (nelts)
242	max_index = fold_build2_loc (input_location,
243				 MINUS_EXPR, TREE_TYPE (nelts),
244				 nelts, integer_one_node);
245      else
246	max_index = array_type_nelts (type);
247
248      /* If we have an error_mark here, we should just return error mark
249	 as we don't know the size of the array yet.  */
250      if (max_index == error_mark_node)
251	return error_mark_node;
252      gcc_assert (TREE_CODE (max_index) == INTEGER_CST);
253
254      /* A zero-sized array, which is accepted as an extension, will
255	 have an upper bound of -1.  */
256      if (!tree_int_cst_equal (max_index, integer_minus_one_node))
257	{
258	  constructor_elt *ce;
259
260	  v = VEC_alloc (constructor_elt, gc, 1);
261	  ce = VEC_quick_push (constructor_elt, v, NULL);
262
263	  /* If this is a one element array, we just use a regular init.  */
264	  if (tree_int_cst_equal (size_zero_node, max_index))
265	    ce->index = size_zero_node;
266	  else
267	    ce->index = build2 (RANGE_EXPR, sizetype, size_zero_node,
268				max_index);
269
270	  ce->value = build_zero_init_1 (TREE_TYPE (type),
271					 /*nelts=*/NULL_TREE,
272					 static_storage_p, NULL_TREE);
273	}
274
275      /* Build a constructor to contain the initializations.  */
276      init = build_constructor (type, v);
277    }
278  else if (TREE_CODE (type) == VECTOR_TYPE)
279    init = fold_convert (type, integer_zero_node);
280  else
281    gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
282
283  /* In all cases, the initializer is a constant.  */
284  if (init)
285    TREE_CONSTANT (init) = 1;
286
287  return init;
288}
289
290/* Return an expression for the zero-initialization of an object with
291   type T.  This expression will either be a constant (in the case
292   that T is a scalar), or a CONSTRUCTOR (in the case that T is an
293   aggregate), or NULL (in the case that T does not require
294   initialization).  In either case, the value can be used as
295   DECL_INITIAL for a decl of the indicated TYPE; it is a valid static
296   initializer. If NELTS is non-NULL, and TYPE is an ARRAY_TYPE, NELTS
297   is the number of elements in the array.  If STATIC_STORAGE_P is
298   TRUE, initializers are only generated for entities for which
299   zero-initialization does not simply mean filling the storage with
300   zero bytes.  */
301
302tree
303build_zero_init (tree type, tree nelts, bool static_storage_p)
304{
305  return build_zero_init_1 (type, nelts, static_storage_p, NULL_TREE);
306}
307
308/* Return a suitable initializer for value-initializing an object of type
309   TYPE, as described in [dcl.init].  */
310
311tree
312build_value_init (tree type)
313{
314  /* [dcl.init]
315
316     To value-initialize an object of type T means:
317
318     - if T is a class type (clause 9) with a user-provided constructor
319       (12.1), then the default constructor for T is called (and the
320       initialization is ill-formed if T has no accessible default
321       constructor);
322
323     - if T is a non-union class type without a user-provided constructor,
324       then every non-static data member and base-class component of T is
325       value-initialized;92)
326
327     - if T is an array type, then each element is value-initialized;
328
329     - otherwise, the object is zero-initialized.
330
331     A program that calls for default-initialization or
332     value-initialization of an entity of reference type is ill-formed.
333
334     92) Value-initialization for such a class object may be implemented by
335     zero-initializing the object and then calling the default
336     constructor.  */
337
338  if (CLASS_TYPE_P (type))
339    {
340      if (type_has_user_provided_constructor (type))
341	return build_aggr_init_expr
342	  (type,
343	   build_special_member_call (NULL_TREE, complete_ctor_identifier,
344				      NULL, type, LOOKUP_NORMAL,
345				      tf_warning_or_error));
346      else if (TREE_CODE (type) != UNION_TYPE && TYPE_NEEDS_CONSTRUCTING (type))
347	{
348	  /* This is a class that needs constructing, but doesn't have
349	     a user-provided constructor.  So we need to zero-initialize
350	     the object and then call the implicitly defined ctor.
351	     This will be handled in simplify_aggr_init_expr.  */
352	  tree ctor = build_special_member_call
353	    (NULL_TREE, complete_ctor_identifier,
354	     NULL, type, LOOKUP_NORMAL, tf_warning_or_error);
355
356	  ctor = build_aggr_init_expr (type, ctor);
357	  AGGR_INIT_ZERO_FIRST (ctor) = 1;
358	  return ctor;
359	}
360    }
361  return build_value_init_noctor (type);
362}
363
364/* Like build_value_init, but don't call the constructor for TYPE.  Used
365   for base initializers.  */
366
367tree
368build_value_init_noctor (tree type)
369{
370  if (CLASS_TYPE_P (type))
371    {
372      gcc_assert (!TYPE_NEEDS_CONSTRUCTING (type));
373
374      if (TREE_CODE (type) != UNION_TYPE)
375	{
376	  tree field;
377	  VEC(constructor_elt,gc) *v = NULL;
378
379	  /* Iterate over the fields, building initializations.  */
380	  for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
381	    {
382	      tree ftype, value;
383
384	      if (TREE_CODE (field) != FIELD_DECL)
385		continue;
386
387	      ftype = TREE_TYPE (field);
388
389	      if (TREE_CODE (ftype) == REFERENCE_TYPE)
390		error ("value-initialization of reference");
391
392	      /* We could skip vfields and fields of types with
393		 user-defined constructors, but I think that won't improve
394		 performance at all; it should be simpler in general just
395		 to zero out the entire object than try to only zero the
396		 bits that actually need it.  */
397
398	      /* Note that for class types there will be FIELD_DECLs
399		 corresponding to base classes as well.  Thus, iterating
400		 over TYPE_FIELDs will result in correct initialization of
401		 all of the subobjects.  */
402	      value = build_value_init (ftype);
403
404	      if (value)
405		CONSTRUCTOR_APPEND_ELT(v, field, value);
406	    }
407
408	  /* Build a constructor to contain the zero- initializations.  */
409	  return build_constructor (type, v);
410	}
411    }
412  else if (TREE_CODE (type) == ARRAY_TYPE)
413    {
414      VEC(constructor_elt,gc) *v = NULL;
415
416      /* Iterate over the array elements, building initializations.  */
417      tree max_index = array_type_nelts (type);
418
419      /* If we have an error_mark here, we should just return error mark
420	 as we don't know the size of the array yet.  */
421      if (max_index == error_mark_node)
422	return error_mark_node;
423      gcc_assert (TREE_CODE (max_index) == INTEGER_CST);
424
425      /* A zero-sized array, which is accepted as an extension, will
426	 have an upper bound of -1.  */
427      if (!tree_int_cst_equal (max_index, integer_minus_one_node))
428	{
429	  constructor_elt *ce;
430
431	  v = VEC_alloc (constructor_elt, gc, 1);
432	  ce = VEC_quick_push (constructor_elt, v, NULL);
433
434	  /* If this is a one element array, we just use a regular init.  */
435	  if (tree_int_cst_equal (size_zero_node, max_index))
436	    ce->index = size_zero_node;
437	  else
438	    ce->index = build2 (RANGE_EXPR, sizetype, size_zero_node,
439				max_index);
440
441	  ce->value = build_value_init (TREE_TYPE (type));
442
443	  /* The gimplifier can't deal with a RANGE_EXPR of TARGET_EXPRs.  */
444	  gcc_assert (TREE_CODE (ce->value) != TARGET_EXPR
445		      && TREE_CODE (ce->value) != AGGR_INIT_EXPR);
446	}
447
448      /* Build a constructor to contain the initializations.  */
449      return build_constructor (type, v);
450    }
451
452  return build_zero_init (type, NULL_TREE, /*static_storage_p=*/false);
453}
454
455/* Initialize MEMBER, a FIELD_DECL, with INIT, a TREE_LIST of
456   arguments.  If TREE_LIST is void_type_node, an empty initializer
457   list was given; if NULL_TREE no initializer was given.  */
458
459static void
460perform_member_init (tree member, tree init)
461{
462  tree decl;
463  tree type = TREE_TYPE (member);
464
465  /* Effective C++ rule 12 requires that all data members be
466     initialized.  */
467  if (warn_ecpp && init == NULL_TREE && TREE_CODE (type) != ARRAY_TYPE)
468    warning_at (DECL_SOURCE_LOCATION (current_function_decl), OPT_Weffc__,
469		"%qD should be initialized in the member initialization list",
470		member);
471
472  /* Get an lvalue for the data member.  */
473  decl = build_class_member_access_expr (current_class_ref, member,
474					 /*access_path=*/NULL_TREE,
475					 /*preserve_reference=*/true,
476					 tf_warning_or_error);
477  if (decl == error_mark_node)
478    return;
479
480  if (init == void_type_node)
481    {
482      /* mem() means value-initialization.  */
483      if (TREE_CODE (type) == ARRAY_TYPE)
484	{
485	  init = build_vec_init (decl, NULL_TREE, NULL_TREE,
486				 /*explicit_value_init_p=*/true,
487				 /* from_array=*/0,
488				 tf_warning_or_error);
489	  finish_expr_stmt (init);
490	}
491      else
492	{
493	  if (TREE_CODE (type) == REFERENCE_TYPE)
494	    permerror (DECL_SOURCE_LOCATION (current_function_decl),
495		       "value-initialization of %q#D, which has reference type",
496		       member);
497	  else
498	    {
499	      init = build2 (INIT_EXPR, type, decl, build_value_init (type));
500	      finish_expr_stmt (init);
501	    }
502	}
503    }
504  /* Deal with this here, as we will get confused if we try to call the
505     assignment op for an anonymous union.  This can happen in a
506     synthesized copy constructor.  */
507  else if (ANON_AGGR_TYPE_P (type))
508    {
509      if (init)
510	{
511	  init = build2 (INIT_EXPR, type, decl, TREE_VALUE (init));
512	  finish_expr_stmt (init);
513	}
514    }
515  else if (TYPE_NEEDS_CONSTRUCTING (type))
516    {
517      if (init != NULL_TREE
518	  && TREE_CODE (type) == ARRAY_TYPE
519	  && TREE_CHAIN (init) == NULL_TREE
520	  && TREE_CODE (TREE_TYPE (TREE_VALUE (init))) == ARRAY_TYPE)
521	{
522	  /* Initialization of one array from another.  */
523	  finish_expr_stmt (build_vec_init (decl, NULL_TREE, TREE_VALUE (init),
524					    /*explicit_value_init_p=*/false,
525					    /* from_array=*/1,
526                                            tf_warning_or_error));
527	}
528      else
529	{
530	  if (CP_TYPE_CONST_P (type)
531	      && init == NULL_TREE
532	      && !type_has_user_provided_default_constructor (type))
533	    /* TYPE_NEEDS_CONSTRUCTING can be set just because we have a
534	       vtable; still give this diagnostic.  */
535	    permerror (DECL_SOURCE_LOCATION (current_function_decl),
536		       "uninitialized member %qD with %<const%> type %qT",
537		       member, type);
538	  finish_expr_stmt (build_aggr_init (decl, init, 0,
539					     tf_warning_or_error));
540	}
541    }
542  else
543    {
544      if (init == NULL_TREE)
545	{
546	  /* member traversal: note it leaves init NULL */
547	  if (TREE_CODE (type) == REFERENCE_TYPE)
548	    permerror (DECL_SOURCE_LOCATION (current_function_decl),
549		       "uninitialized reference member %qD",
550		       member);
551	  else if (CP_TYPE_CONST_P (type))
552	    permerror (DECL_SOURCE_LOCATION (current_function_decl),
553		       "uninitialized member %qD with %<const%> type %qT",
554		       member, type);
555	}
556      else if (TREE_CODE (init) == TREE_LIST)
557	/* There was an explicit member initialization.  Do some work
558	   in that case.  */
559	init = build_x_compound_expr_from_list (init, "member initializer");
560
561      if (init)
562	finish_expr_stmt (cp_build_modify_expr (decl, INIT_EXPR, init,
563						tf_warning_or_error));
564    }
565
566  if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
567    {
568      tree expr;
569
570      expr = build_class_member_access_expr (current_class_ref, member,
571					     /*access_path=*/NULL_TREE,
572					     /*preserve_reference=*/false,
573					     tf_warning_or_error);
574      expr = build_delete (type, expr, sfk_complete_destructor,
575			   LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR, 0);
576
577      if (expr != error_mark_node)
578	finish_eh_cleanup (expr);
579    }
580}
581
582/* Returns a TREE_LIST containing (as the TREE_PURPOSE of each node) all
583   the FIELD_DECLs on the TYPE_FIELDS list for T, in reverse order.  */
584
585static tree
586build_field_list (tree t, tree list, int *uses_unions_p)
587{
588  tree fields;
589
590  *uses_unions_p = 0;
591
592  /* Note whether or not T is a union.  */
593  if (TREE_CODE (t) == UNION_TYPE)
594    *uses_unions_p = 1;
595
596  for (fields = TYPE_FIELDS (t); fields; fields = TREE_CHAIN (fields))
597    {
598      /* Skip CONST_DECLs for enumeration constants and so forth.  */
599      if (TREE_CODE (fields) != FIELD_DECL || DECL_ARTIFICIAL (fields))
600	continue;
601
602      /* Keep track of whether or not any fields are unions.  */
603      if (TREE_CODE (TREE_TYPE (fields)) == UNION_TYPE)
604	*uses_unions_p = 1;
605
606      /* For an anonymous struct or union, we must recursively
607	 consider the fields of the anonymous type.  They can be
608	 directly initialized from the constructor.  */
609      if (ANON_AGGR_TYPE_P (TREE_TYPE (fields)))
610	{
611	  /* Add this field itself.  Synthesized copy constructors
612	     initialize the entire aggregate.  */
613	  list = tree_cons (fields, NULL_TREE, list);
614	  /* And now add the fields in the anonymous aggregate.  */
615	  list = build_field_list (TREE_TYPE (fields), list,
616				   uses_unions_p);
617	}
618      /* Add this field.  */
619      else if (DECL_NAME (fields))
620	list = tree_cons (fields, NULL_TREE, list);
621    }
622
623  return list;
624}
625
626/* The MEM_INITS are a TREE_LIST.  The TREE_PURPOSE of each list gives
627   a FIELD_DECL or BINFO in T that needs initialization.  The
628   TREE_VALUE gives the initializer, or list of initializer arguments.
629
630   Return a TREE_LIST containing all of the initializations required
631   for T, in the order in which they should be performed.  The output
632   list has the same format as the input.  */
633
634static tree
635sort_mem_initializers (tree t, tree mem_inits)
636{
637  tree init;
638  tree base, binfo, base_binfo;
639  tree sorted_inits;
640  tree next_subobject;
641  VEC(tree,gc) *vbases;
642  int i;
643  int uses_unions_p;
644
645  /* Build up a list of initializations.  The TREE_PURPOSE of entry
646     will be the subobject (a FIELD_DECL or BINFO) to initialize.  The
647     TREE_VALUE will be the constructor arguments, or NULL if no
648     explicit initialization was provided.  */
649  sorted_inits = NULL_TREE;
650
651  /* Process the virtual bases.  */
652  for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
653       VEC_iterate (tree, vbases, i, base); i++)
654    sorted_inits = tree_cons (base, NULL_TREE, sorted_inits);
655
656  /* Process the direct bases.  */
657  for (binfo = TYPE_BINFO (t), i = 0;
658       BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
659    if (!BINFO_VIRTUAL_P (base_binfo))
660      sorted_inits = tree_cons (base_binfo, NULL_TREE, sorted_inits);
661
662  /* Process the non-static data members.  */
663  sorted_inits = build_field_list (t, sorted_inits, &uses_unions_p);
664  /* Reverse the entire list of initializations, so that they are in
665     the order that they will actually be performed.  */
666  sorted_inits = nreverse (sorted_inits);
667
668  /* If the user presented the initializers in an order different from
669     that in which they will actually occur, we issue a warning.  Keep
670     track of the next subobject which can be explicitly initialized
671     without issuing a warning.  */
672  next_subobject = sorted_inits;
673
674  /* Go through the explicit initializers, filling in TREE_PURPOSE in
675     the SORTED_INITS.  */
676  for (init = mem_inits; init; init = TREE_CHAIN (init))
677    {
678      tree subobject;
679      tree subobject_init;
680
681      subobject = TREE_PURPOSE (init);
682
683      /* If the explicit initializers are in sorted order, then
684	 SUBOBJECT will be NEXT_SUBOBJECT, or something following
685	 it.  */
686      for (subobject_init = next_subobject;
687	   subobject_init;
688	   subobject_init = TREE_CHAIN (subobject_init))
689	if (TREE_PURPOSE (subobject_init) == subobject)
690	  break;
691
692      /* Issue a warning if the explicit initializer order does not
693	 match that which will actually occur.
694	 ??? Are all these on the correct lines?  */
695      if (warn_reorder && !subobject_init)
696	{
697	  if (TREE_CODE (TREE_PURPOSE (next_subobject)) == FIELD_DECL)
698	    warning (OPT_Wreorder, "%q+D will be initialized after",
699		     TREE_PURPOSE (next_subobject));
700	  else
701	    warning (OPT_Wreorder, "base %qT will be initialized after",
702		     TREE_PURPOSE (next_subobject));
703	  if (TREE_CODE (subobject) == FIELD_DECL)
704	    warning (OPT_Wreorder, "  %q+#D", subobject);
705	  else
706	    warning (OPT_Wreorder, "  base %qT", subobject);
707	  warning_at (DECL_SOURCE_LOCATION (current_function_decl),
708		      OPT_Wreorder, "  when initialized here");
709	}
710
711      /* Look again, from the beginning of the list.  */
712      if (!subobject_init)
713	{
714	  subobject_init = sorted_inits;
715	  while (TREE_PURPOSE (subobject_init) != subobject)
716	    subobject_init = TREE_CHAIN (subobject_init);
717	}
718
719      /* It is invalid to initialize the same subobject more than
720	 once.  */
721      if (TREE_VALUE (subobject_init))
722	{
723	  if (TREE_CODE (subobject) == FIELD_DECL)
724	    error_at (DECL_SOURCE_LOCATION (current_function_decl),
725		      "multiple initializations given for %qD",
726		      subobject);
727	  else
728	    error_at (DECL_SOURCE_LOCATION (current_function_decl),
729		      "multiple initializations given for base %qT",
730		      subobject);
731	}
732
733      /* Record the initialization.  */
734      TREE_VALUE (subobject_init) = TREE_VALUE (init);
735      next_subobject = subobject_init;
736    }
737
738  /* [class.base.init]
739
740     If a ctor-initializer specifies more than one mem-initializer for
741     multiple members of the same union (including members of
742     anonymous unions), the ctor-initializer is ill-formed.  */
743  if (uses_unions_p)
744    {
745      tree last_field = NULL_TREE;
746      for (init = sorted_inits; init; init = TREE_CHAIN (init))
747	{
748	  tree field;
749	  tree field_type;
750	  int done;
751
752	  /* Skip uninitialized members and base classes.  */
753	  if (!TREE_VALUE (init)
754	      || TREE_CODE (TREE_PURPOSE (init)) != FIELD_DECL)
755	    continue;
756	  /* See if this field is a member of a union, or a member of a
757	     structure contained in a union, etc.  */
758	  field = TREE_PURPOSE (init);
759	  for (field_type = DECL_CONTEXT (field);
760	       !same_type_p (field_type, t);
761	       field_type = TYPE_CONTEXT (field_type))
762	    if (TREE_CODE (field_type) == UNION_TYPE)
763	      break;
764	  /* If this field is not a member of a union, skip it.  */
765	  if (TREE_CODE (field_type) != UNION_TYPE)
766	    continue;
767
768	  /* It's only an error if we have two initializers for the same
769	     union type.  */
770	  if (!last_field)
771	    {
772	      last_field = field;
773	      continue;
774	    }
775
776	  /* See if LAST_FIELD and the field initialized by INIT are
777	     members of the same union.  If so, there's a problem,
778	     unless they're actually members of the same structure
779	     which is itself a member of a union.  For example, given:
780
781	       union { struct { int i; int j; }; };
782
783	     initializing both `i' and `j' makes sense.  */
784	  field_type = DECL_CONTEXT (field);
785	  done = 0;
786	  do
787	    {
788	      tree last_field_type;
789
790	      last_field_type = DECL_CONTEXT (last_field);
791	      while (1)
792		{
793		  if (same_type_p (last_field_type, field_type))
794		    {
795		      if (TREE_CODE (field_type) == UNION_TYPE)
796			error_at (DECL_SOURCE_LOCATION (current_function_decl),
797				  "initializations for multiple members of %qT",
798				  last_field_type);
799		      done = 1;
800		      break;
801		    }
802
803		  if (same_type_p (last_field_type, t))
804		    break;
805
806		  last_field_type = TYPE_CONTEXT (last_field_type);
807		}
808
809	      /* If we've reached the outermost class, then we're
810		 done.  */
811	      if (same_type_p (field_type, t))
812		break;
813
814	      field_type = TYPE_CONTEXT (field_type);
815	    }
816	  while (!done);
817
818	  last_field = field;
819	}
820    }
821
822  return sorted_inits;
823}
824
825/* Initialize all bases and members of CURRENT_CLASS_TYPE.  MEM_INITS
826   is a TREE_LIST giving the explicit mem-initializer-list for the
827   constructor.  The TREE_PURPOSE of each entry is a subobject (a
828   FIELD_DECL or a BINFO) of the CURRENT_CLASS_TYPE.  The TREE_VALUE
829   is a TREE_LIST giving the arguments to the constructor or
830   void_type_node for an empty list of arguments.  */
831
832void
833emit_mem_initializers (tree mem_inits)
834{
835  /* We will already have issued an error message about the fact that
836     the type is incomplete.  */
837  if (!COMPLETE_TYPE_P (current_class_type))
838    return;
839
840  /* Sort the mem-initializers into the order in which the
841     initializations should be performed.  */
842  mem_inits = sort_mem_initializers (current_class_type, mem_inits);
843
844  in_base_initializer = 1;
845
846  /* Initialize base classes.  */
847  while (mem_inits
848	 && TREE_CODE (TREE_PURPOSE (mem_inits)) != FIELD_DECL)
849    {
850      tree subobject = TREE_PURPOSE (mem_inits);
851      tree arguments = TREE_VALUE (mem_inits);
852
853      /* If these initializations are taking place in a copy constructor,
854	 the base class should probably be explicitly initialized if there
855	 is a user-defined constructor in the base class (other than the
856	 default constructor, which will be called anyway).  */
857      if (extra_warnings && !arguments
858	  && DECL_COPY_CONSTRUCTOR_P (current_function_decl)
859	  && type_has_user_nondefault_constructor (BINFO_TYPE (subobject)))
860	warning_at (DECL_SOURCE_LOCATION (current_function_decl), OPT_Wextra,
861		    "base class %q#T should be explicitly initialized in the "
862		    "copy constructor",
863		    BINFO_TYPE (subobject));
864
865      /* Initialize the base.  */
866      if (BINFO_VIRTUAL_P (subobject))
867	construct_virtual_base (subobject, arguments);
868      else
869	{
870	  tree base_addr;
871
872	  base_addr = build_base_path (PLUS_EXPR, current_class_ptr,
873				       subobject, 1);
874	  expand_aggr_init_1 (subobject, NULL_TREE,
875			      cp_build_indirect_ref (base_addr, RO_NULL,
876                                                     tf_warning_or_error),
877			      arguments,
878			      LOOKUP_NORMAL,
879                              tf_warning_or_error);
880	  expand_cleanup_for_base (subobject, NULL_TREE);
881	}
882
883      mem_inits = TREE_CHAIN (mem_inits);
884    }
885  in_base_initializer = 0;
886
887  /* Initialize the vptrs.  */
888  initialize_vtbl_ptrs (current_class_ptr);
889
890  /* Initialize the data members.  */
891  while (mem_inits)
892    {
893      perform_member_init (TREE_PURPOSE (mem_inits),
894			   TREE_VALUE (mem_inits));
895      mem_inits = TREE_CHAIN (mem_inits);
896    }
897}
898
899/* Returns the address of the vtable (i.e., the value that should be
900   assigned to the vptr) for BINFO.  */
901
902static tree
903build_vtbl_address (tree binfo)
904{
905  tree binfo_for = binfo;
906  tree vtbl;
907
908  if (BINFO_VPTR_INDEX (binfo) && BINFO_VIRTUAL_P (binfo))
909    /* If this is a virtual primary base, then the vtable we want to store
910       is that for the base this is being used as the primary base of.  We
911       can't simply skip the initialization, because we may be expanding the
912       inits of a subobject constructor where the virtual base layout
913       can be different.  */
914    while (BINFO_PRIMARY_P (binfo_for))
915      binfo_for = BINFO_INHERITANCE_CHAIN (binfo_for);
916
917  /* Figure out what vtable BINFO's vtable is based on, and mark it as
918     used.  */
919  vtbl = get_vtbl_decl_for_binfo (binfo_for);
920  TREE_USED (vtbl) = 1;
921
922  /* Now compute the address to use when initializing the vptr.  */
923  vtbl = unshare_expr (BINFO_VTABLE (binfo_for));
924  if (TREE_CODE (vtbl) == VAR_DECL)
925    vtbl = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (vtbl)), vtbl);
926
927  return vtbl;
928}
929
930/* This code sets up the virtual function tables appropriate for
931   the pointer DECL.  It is a one-ply initialization.
932
933   BINFO is the exact type that DECL is supposed to be.  In
934   multiple inheritance, this might mean "C's A" if C : A, B.  */
935
936static void
937expand_virtual_init (tree binfo, tree decl)
938{
939  tree vtbl, vtbl_ptr;
940  tree vtt_index;
941
942  /* Compute the initializer for vptr.  */
943  vtbl = build_vtbl_address (binfo);
944
945  /* We may get this vptr from a VTT, if this is a subobject
946     constructor or subobject destructor.  */
947  vtt_index = BINFO_VPTR_INDEX (binfo);
948  if (vtt_index)
949    {
950      tree vtbl2;
951      tree vtt_parm;
952
953      /* Compute the value to use, when there's a VTT.  */
954      vtt_parm = current_vtt_parm;
955      vtbl2 = build2 (POINTER_PLUS_EXPR,
956		      TREE_TYPE (vtt_parm),
957		      vtt_parm,
958		      vtt_index);
959      vtbl2 = cp_build_indirect_ref (vtbl2, RO_NULL, tf_warning_or_error);
960      vtbl2 = convert (TREE_TYPE (vtbl), vtbl2);
961
962      /* The actual initializer is the VTT value only in the subobject
963	 constructor.  In maybe_clone_body we'll substitute NULL for
964	 the vtt_parm in the case of the non-subobject constructor.  */
965      vtbl = build3 (COND_EXPR,
966		     TREE_TYPE (vtbl),
967		     build2 (EQ_EXPR, boolean_type_node,
968			     current_in_charge_parm, integer_zero_node),
969		     vtbl2,
970		     vtbl);
971    }
972
973  /* Compute the location of the vtpr.  */
974  vtbl_ptr = build_vfield_ref (cp_build_indirect_ref (decl, RO_NULL,
975                                                      tf_warning_or_error),
976			       TREE_TYPE (binfo));
977  gcc_assert (vtbl_ptr != error_mark_node);
978
979  /* Assign the vtable to the vptr.  */
980  vtbl = convert_force (TREE_TYPE (vtbl_ptr), vtbl, 0);
981  finish_expr_stmt (cp_build_modify_expr (vtbl_ptr, NOP_EXPR, vtbl,
982					  tf_warning_or_error));
983}
984
985/* If an exception is thrown in a constructor, those base classes already
986   constructed must be destroyed.  This function creates the cleanup
987   for BINFO, which has just been constructed.  If FLAG is non-NULL,
988   it is a DECL which is nonzero when this base needs to be
989   destroyed.  */
990
991static void
992expand_cleanup_for_base (tree binfo, tree flag)
993{
994  tree expr;
995
996  if (TYPE_HAS_TRIVIAL_DESTRUCTOR (BINFO_TYPE (binfo)))
997    return;
998
999  /* Call the destructor.  */
1000  expr = build_special_member_call (current_class_ref,
1001				    base_dtor_identifier,
1002				    NULL,
1003				    binfo,
1004				    LOOKUP_NORMAL | LOOKUP_NONVIRTUAL,
1005                                    tf_warning_or_error);
1006  if (flag)
1007    expr = fold_build3_loc (input_location,
1008			COND_EXPR, void_type_node,
1009			c_common_truthvalue_conversion (input_location, flag),
1010			expr, integer_zero_node);
1011
1012  finish_eh_cleanup (expr);
1013}
1014
1015/* Construct the virtual base-class VBASE passing the ARGUMENTS to its
1016   constructor.  */
1017
1018static void
1019construct_virtual_base (tree vbase, tree arguments)
1020{
1021  tree inner_if_stmt;
1022  tree exp;
1023  tree flag;
1024
1025  /* If there are virtual base classes with destructors, we need to
1026     emit cleanups to destroy them if an exception is thrown during
1027     the construction process.  These exception regions (i.e., the
1028     period during which the cleanups must occur) begin from the time
1029     the construction is complete to the end of the function.  If we
1030     create a conditional block in which to initialize the
1031     base-classes, then the cleanup region for the virtual base begins
1032     inside a block, and ends outside of that block.  This situation
1033     confuses the sjlj exception-handling code.  Therefore, we do not
1034     create a single conditional block, but one for each
1035     initialization.  (That way the cleanup regions always begin
1036     in the outer block.)  We trust the back end to figure out
1037     that the FLAG will not change across initializations, and
1038     avoid doing multiple tests.  */
1039  flag = TREE_CHAIN (DECL_ARGUMENTS (current_function_decl));
1040  inner_if_stmt = begin_if_stmt ();
1041  finish_if_stmt_cond (flag, inner_if_stmt);
1042
1043  /* Compute the location of the virtual base.  If we're
1044     constructing virtual bases, then we must be the most derived
1045     class.  Therefore, we don't have to look up the virtual base;
1046     we already know where it is.  */
1047  exp = convert_to_base_statically (current_class_ref, vbase);
1048
1049  expand_aggr_init_1 (vbase, current_class_ref, exp, arguments,
1050		      LOOKUP_COMPLAIN, tf_warning_or_error);
1051  finish_then_clause (inner_if_stmt);
1052  finish_if_stmt (inner_if_stmt);
1053
1054  expand_cleanup_for_base (vbase, flag);
1055}
1056
1057/* Find the context in which this FIELD can be initialized.  */
1058
1059static tree
1060initializing_context (tree field)
1061{
1062  tree t = DECL_CONTEXT (field);
1063
1064  /* Anonymous union members can be initialized in the first enclosing
1065     non-anonymous union context.  */
1066  while (t && ANON_AGGR_TYPE_P (t))
1067    t = TYPE_CONTEXT (t);
1068  return t;
1069}
1070
1071/* Function to give error message if member initialization specification
1072   is erroneous.  FIELD is the member we decided to initialize.
1073   TYPE is the type for which the initialization is being performed.
1074   FIELD must be a member of TYPE.
1075
1076   MEMBER_NAME is the name of the member.  */
1077
1078static int
1079member_init_ok_or_else (tree field, tree type, tree member_name)
1080{
1081  if (field == error_mark_node)
1082    return 0;
1083  if (!field)
1084    {
1085      error ("class %qT does not have any field named %qD", type,
1086	     member_name);
1087      return 0;
1088    }
1089  if (TREE_CODE (field) == VAR_DECL)
1090    {
1091      error ("%q#D is a static data member; it can only be "
1092	     "initialized at its definition",
1093	     field);
1094      return 0;
1095    }
1096  if (TREE_CODE (field) != FIELD_DECL)
1097    {
1098      error ("%q#D is not a non-static data member of %qT",
1099	     field, type);
1100      return 0;
1101    }
1102  if (initializing_context (field) != type)
1103    {
1104      error ("class %qT does not have any field named %qD", type,
1105		member_name);
1106      return 0;
1107    }
1108
1109  return 1;
1110}
1111
1112/* NAME is a FIELD_DECL, an IDENTIFIER_NODE which names a field, or it
1113   is a _TYPE node or TYPE_DECL which names a base for that type.
1114   Check the validity of NAME, and return either the base _TYPE, base
1115   binfo, or the FIELD_DECL of the member.  If NAME is invalid, return
1116   NULL_TREE and issue a diagnostic.
1117
1118   An old style unnamed direct single base construction is permitted,
1119   where NAME is NULL.  */
1120
1121tree
1122expand_member_init (tree name)
1123{
1124  tree basetype;
1125  tree field;
1126
1127  if (!current_class_ref)
1128    return NULL_TREE;
1129
1130  if (!name)
1131    {
1132      /* This is an obsolete unnamed base class initializer.  The
1133	 parser will already have warned about its use.  */
1134      switch (BINFO_N_BASE_BINFOS (TYPE_BINFO (current_class_type)))
1135	{
1136	case 0:
1137	  error ("unnamed initializer for %qT, which has no base classes",
1138		 current_class_type);
1139	  return NULL_TREE;
1140	case 1:
1141	  basetype = BINFO_TYPE
1142	    (BINFO_BASE_BINFO (TYPE_BINFO (current_class_type), 0));
1143	  break;
1144	default:
1145	  error ("unnamed initializer for %qT, which uses multiple inheritance",
1146		 current_class_type);
1147	  return NULL_TREE;
1148      }
1149    }
1150  else if (TYPE_P (name))
1151    {
1152      basetype = TYPE_MAIN_VARIANT (name);
1153      name = TYPE_NAME (name);
1154    }
1155  else if (TREE_CODE (name) == TYPE_DECL)
1156    basetype = TYPE_MAIN_VARIANT (TREE_TYPE (name));
1157  else
1158    basetype = NULL_TREE;
1159
1160  if (basetype)
1161    {
1162      tree class_binfo;
1163      tree direct_binfo;
1164      tree virtual_binfo;
1165      int i;
1166
1167      if (current_template_parms)
1168	return basetype;
1169
1170      class_binfo = TYPE_BINFO (current_class_type);
1171      direct_binfo = NULL_TREE;
1172      virtual_binfo = NULL_TREE;
1173
1174      /* Look for a direct base.  */
1175      for (i = 0; BINFO_BASE_ITERATE (class_binfo, i, direct_binfo); ++i)
1176	if (SAME_BINFO_TYPE_P (BINFO_TYPE (direct_binfo), basetype))
1177	  break;
1178
1179      /* Look for a virtual base -- unless the direct base is itself
1180	 virtual.  */
1181      if (!direct_binfo || !BINFO_VIRTUAL_P (direct_binfo))
1182	virtual_binfo = binfo_for_vbase (basetype, current_class_type);
1183
1184      /* [class.base.init]
1185
1186	 If a mem-initializer-id is ambiguous because it designates
1187	 both a direct non-virtual base class and an inherited virtual
1188	 base class, the mem-initializer is ill-formed.  */
1189      if (direct_binfo && virtual_binfo)
1190	{
1191	  error ("%qD is both a direct base and an indirect virtual base",
1192		 basetype);
1193	  return NULL_TREE;
1194	}
1195
1196      if (!direct_binfo && !virtual_binfo)
1197	{
1198	  if (CLASSTYPE_VBASECLASSES (current_class_type))
1199	    error ("type %qT is not a direct or virtual base of %qT",
1200		   basetype, current_class_type);
1201	  else
1202	    error ("type %qT is not a direct base of %qT",
1203		   basetype, current_class_type);
1204	  return NULL_TREE;
1205	}
1206
1207      return direct_binfo ? direct_binfo : virtual_binfo;
1208    }
1209  else
1210    {
1211      if (TREE_CODE (name) == IDENTIFIER_NODE)
1212	field = lookup_field (current_class_type, name, 1, false);
1213      else
1214	field = name;
1215
1216      if (member_init_ok_or_else (field, current_class_type, name))
1217	return field;
1218    }
1219
1220  return NULL_TREE;
1221}
1222
1223/* This is like `expand_member_init', only it stores one aggregate
1224   value into another.
1225
1226   INIT comes in two flavors: it is either a value which
1227   is to be stored in EXP, or it is a parameter list
1228   to go to a constructor, which will operate on EXP.
1229   If INIT is not a parameter list for a constructor, then set
1230   LOOKUP_ONLYCONVERTING.
1231   If FLAGS is LOOKUP_ONLYCONVERTING then it is the = init form of
1232   the initializer, if FLAGS is 0, then it is the (init) form.
1233   If `init' is a CONSTRUCTOR, then we emit a warning message,
1234   explaining that such initializations are invalid.
1235
1236   If INIT resolves to a CALL_EXPR which happens to return
1237   something of the type we are looking for, then we know
1238   that we can safely use that call to perform the
1239   initialization.
1240
1241   The virtual function table pointer cannot be set up here, because
1242   we do not really know its type.
1243
1244   This never calls operator=().
1245
1246   When initializing, nothing is CONST.
1247
1248   A default copy constructor may have to be used to perform the
1249   initialization.
1250
1251   A constructor or a conversion operator may have to be used to
1252   perform the initialization, but not both, as it would be ambiguous.  */
1253
1254tree
1255build_aggr_init (tree exp, tree init, int flags, tsubst_flags_t complain)
1256{
1257  tree stmt_expr;
1258  tree compound_stmt;
1259  int destroy_temps;
1260  tree type = TREE_TYPE (exp);
1261  int was_const = TREE_READONLY (exp);
1262  int was_volatile = TREE_THIS_VOLATILE (exp);
1263  int is_global;
1264
1265  if (init == error_mark_node)
1266    return error_mark_node;
1267
1268  TREE_READONLY (exp) = 0;
1269  TREE_THIS_VOLATILE (exp) = 0;
1270
1271  if (init && TREE_CODE (init) != TREE_LIST
1272      && !(BRACE_ENCLOSED_INITIALIZER_P (init)
1273	   && CONSTRUCTOR_IS_DIRECT_INIT (init)))
1274    flags |= LOOKUP_ONLYCONVERTING;
1275
1276  if (TREE_CODE (type) == ARRAY_TYPE)
1277    {
1278      tree itype;
1279
1280      /* An array may not be initialized use the parenthesized
1281	 initialization form -- unless the initializer is "()".  */
1282      if (init && TREE_CODE (init) == TREE_LIST)
1283	{
1284          if (complain & tf_error)
1285            error ("bad array initializer");
1286	  return error_mark_node;
1287	}
1288      /* Must arrange to initialize each element of EXP
1289	 from elements of INIT.  */
1290      itype = init ? TREE_TYPE (init) : NULL_TREE;
1291      if (cv_qualified_p (type))
1292	TREE_TYPE (exp) = cv_unqualified (type);
1293      if (itype && cv_qualified_p (itype))
1294	TREE_TYPE (init) = cv_unqualified (itype);
1295      stmt_expr = build_vec_init (exp, NULL_TREE, init,
1296				  /*explicit_value_init_p=*/false,
1297				  itype && same_type_p (TREE_TYPE (init),
1298							TREE_TYPE (exp)),
1299                                  complain);
1300      TREE_READONLY (exp) = was_const;
1301      TREE_THIS_VOLATILE (exp) = was_volatile;
1302      TREE_TYPE (exp) = type;
1303      if (init)
1304	TREE_TYPE (init) = itype;
1305      return stmt_expr;
1306    }
1307
1308  if (TREE_CODE (exp) == VAR_DECL || TREE_CODE (exp) == PARM_DECL)
1309    /* Just know that we've seen something for this node.  */
1310    TREE_USED (exp) = 1;
1311
1312  is_global = begin_init_stmts (&stmt_expr, &compound_stmt);
1313  destroy_temps = stmts_are_full_exprs_p ();
1314  current_stmt_tree ()->stmts_are_full_exprs_p = 0;
1315  expand_aggr_init_1 (TYPE_BINFO (type), exp, exp,
1316		      init, LOOKUP_NORMAL|flags, complain);
1317  stmt_expr = finish_init_stmts (is_global, stmt_expr, compound_stmt);
1318  current_stmt_tree ()->stmts_are_full_exprs_p = destroy_temps;
1319  TREE_READONLY (exp) = was_const;
1320  TREE_THIS_VOLATILE (exp) = was_volatile;
1321
1322  return stmt_expr;
1323}
1324
1325static void
1326expand_default_init (tree binfo, tree true_exp, tree exp, tree init, int flags,
1327                     tsubst_flags_t complain)
1328{
1329  tree type = TREE_TYPE (exp);
1330  tree ctor_name;
1331
1332  /* It fails because there may not be a constructor which takes
1333     its own type as the first (or only parameter), but which does
1334     take other types via a conversion.  So, if the thing initializing
1335     the expression is a unit element of type X, first try X(X&),
1336     followed by initialization by X.  If neither of these work
1337     out, then look hard.  */
1338  tree rval;
1339  VEC(tree,gc) *parms;
1340
1341  if (init && TREE_CODE (init) != TREE_LIST
1342      && (flags & LOOKUP_ONLYCONVERTING))
1343    {
1344      /* Base subobjects should only get direct-initialization.  */
1345      gcc_assert (true_exp == exp);
1346
1347      if (flags & DIRECT_BIND)
1348	/* Do nothing.  We hit this in two cases:  Reference initialization,
1349	   where we aren't initializing a real variable, so we don't want
1350	   to run a new constructor; and catching an exception, where we
1351	   have already built up the constructor call so we could wrap it
1352	   in an exception region.  */;
1353      else if (BRACE_ENCLOSED_INITIALIZER_P (init)
1354	       && CP_AGGREGATE_TYPE_P (type))
1355	{
1356	  /* A brace-enclosed initializer for an aggregate.  */
1357	  init = digest_init (type, init);
1358	}
1359      else
1360	init = ocp_convert (type, init, CONV_IMPLICIT|CONV_FORCE_TEMP, flags);
1361
1362      if (TREE_CODE (init) == MUST_NOT_THROW_EXPR)
1363	/* We need to protect the initialization of a catch parm with a
1364	   call to terminate(), which shows up as a MUST_NOT_THROW_EXPR
1365	   around the TARGET_EXPR for the copy constructor.  See
1366	   initialize_handler_parm.  */
1367	{
1368	  TREE_OPERAND (init, 0) = build2 (INIT_EXPR, TREE_TYPE (exp), exp,
1369					   TREE_OPERAND (init, 0));
1370	  TREE_TYPE (init) = void_type_node;
1371	}
1372      else
1373	init = build2 (INIT_EXPR, TREE_TYPE (exp), exp, init);
1374      TREE_SIDE_EFFECTS (init) = 1;
1375      finish_expr_stmt (init);
1376      return;
1377    }
1378
1379  if (init == NULL_TREE)
1380    parms = NULL;
1381  else if (TREE_CODE (init) == TREE_LIST && !TREE_TYPE (init))
1382    {
1383      parms = make_tree_vector ();
1384      for (; init != NULL_TREE; init = TREE_CHAIN (init))
1385	VEC_safe_push (tree, gc, parms, TREE_VALUE (init));
1386    }
1387  else
1388    parms = make_tree_vector_single (init);
1389
1390  if (true_exp == exp)
1391    ctor_name = complete_ctor_identifier;
1392  else
1393    ctor_name = base_ctor_identifier;
1394
1395  rval = build_special_member_call (exp, ctor_name, &parms, binfo, flags,
1396                                    complain);
1397
1398  if (parms != NULL)
1399    release_tree_vector (parms);
1400
1401  if (TREE_SIDE_EFFECTS (rval))
1402    finish_expr_stmt (convert_to_void (rval, NULL, complain));
1403}
1404
1405/* This function is responsible for initializing EXP with INIT
1406   (if any).
1407
1408   BINFO is the binfo of the type for who we are performing the
1409   initialization.  For example, if W is a virtual base class of A and B,
1410   and C : A, B.
1411   If we are initializing B, then W must contain B's W vtable, whereas
1412   were we initializing C, W must contain C's W vtable.
1413
1414   TRUE_EXP is nonzero if it is the true expression being initialized.
1415   In this case, it may be EXP, or may just contain EXP.  The reason we
1416   need this is because if EXP is a base element of TRUE_EXP, we
1417   don't necessarily know by looking at EXP where its virtual
1418   baseclass fields should really be pointing.  But we do know
1419   from TRUE_EXP.  In constructors, we don't know anything about
1420   the value being initialized.
1421
1422   FLAGS is just passed to `build_new_method_call'.  See that function
1423   for its description.  */
1424
1425static void
1426expand_aggr_init_1 (tree binfo, tree true_exp, tree exp, tree init, int flags,
1427                    tsubst_flags_t complain)
1428{
1429  tree type = TREE_TYPE (exp);
1430
1431  gcc_assert (init != error_mark_node && type != error_mark_node);
1432  gcc_assert (building_stmt_tree ());
1433
1434  /* Use a function returning the desired type to initialize EXP for us.
1435     If the function is a constructor, and its first argument is
1436     NULL_TREE, know that it was meant for us--just slide exp on
1437     in and expand the constructor.  Constructors now come
1438     as TARGET_EXPRs.  */
1439
1440  if (init && TREE_CODE (exp) == VAR_DECL
1441      && COMPOUND_LITERAL_P (init))
1442    {
1443      /* If store_init_value returns NULL_TREE, the INIT has been
1444	 recorded as the DECL_INITIAL for EXP.  That means there's
1445	 nothing more we have to do.  */
1446      init = store_init_value (exp, init, flags);
1447      if (init)
1448	finish_expr_stmt (init);
1449      return;
1450    }
1451
1452  /* If an explicit -- but empty -- initializer list was present,
1453     that's value-initialization.  */
1454  if (init == void_type_node)
1455    {
1456      /* If there's a user-provided constructor, we just call that.  */
1457      if (type_has_user_provided_constructor (type))
1458	/* Fall through.  */;
1459      /* If there isn't, but we still need to call the constructor,
1460	 zero out the object first.  */
1461      else if (TYPE_NEEDS_CONSTRUCTING (type))
1462	{
1463	  init = build_zero_init (type, NULL_TREE, /*static_storage_p=*/false);
1464	  init = build2 (INIT_EXPR, type, exp, init);
1465	  finish_expr_stmt (init);
1466	  /* And then call the constructor.  */
1467	}
1468      /* If we don't need to mess with the constructor at all,
1469	 then just zero out the object and we're done.  */
1470      else
1471	{
1472	  init = build2 (INIT_EXPR, type, exp, build_value_init_noctor (type));
1473	  finish_expr_stmt (init);
1474	  return;
1475	}
1476      init = NULL_TREE;
1477    }
1478
1479  /* We know that expand_default_init can handle everything we want
1480     at this point.  */
1481  expand_default_init (binfo, true_exp, exp, init, flags, complain);
1482}
1483
1484/* Report an error if TYPE is not a user-defined, class type.  If
1485   OR_ELSE is nonzero, give an error message.  */
1486
1487int
1488is_class_type (tree type, int or_else)
1489{
1490  if (type == error_mark_node)
1491    return 0;
1492
1493  if (! CLASS_TYPE_P (type))
1494    {
1495      if (or_else)
1496	error ("%qT is not a class type", type);
1497      return 0;
1498    }
1499  return 1;
1500}
1501
1502tree
1503get_type_value (tree name)
1504{
1505  if (name == error_mark_node)
1506    return NULL_TREE;
1507
1508  if (IDENTIFIER_HAS_TYPE_VALUE (name))
1509    return IDENTIFIER_TYPE_VALUE (name);
1510  else
1511    return NULL_TREE;
1512}
1513
1514/* Build a reference to a member of an aggregate.  This is not a C++
1515   `&', but really something which can have its address taken, and
1516   then act as a pointer to member, for example TYPE :: FIELD can have
1517   its address taken by saying & TYPE :: FIELD.  ADDRESS_P is true if
1518   this expression is the operand of "&".
1519
1520   @@ Prints out lousy diagnostics for operator <typename>
1521   @@ fields.
1522
1523   @@ This function should be rewritten and placed in search.c.  */
1524
1525tree
1526build_offset_ref (tree type, tree member, bool address_p)
1527{
1528  tree decl;
1529  tree basebinfo = NULL_TREE;
1530
1531  /* class templates can come in as TEMPLATE_DECLs here.  */
1532  if (TREE_CODE (member) == TEMPLATE_DECL)
1533    return member;
1534
1535  if (dependent_type_p (type) || type_dependent_expression_p (member))
1536    return build_qualified_name (NULL_TREE, type, member,
1537				 /*template_p=*/false);
1538
1539  gcc_assert (TYPE_P (type));
1540  if (! is_class_type (type, 1))
1541    return error_mark_node;
1542
1543  gcc_assert (DECL_P (member) || BASELINK_P (member));
1544  /* Callers should call mark_used before this point.  */
1545  gcc_assert (!DECL_P (member) || TREE_USED (member));
1546
1547  if (!COMPLETE_TYPE_P (complete_type (type))
1548      && !TYPE_BEING_DEFINED (type))
1549    {
1550      error ("incomplete type %qT does not have member %qD", type, member);
1551      return error_mark_node;
1552    }
1553
1554  /* Entities other than non-static members need no further
1555     processing.  */
1556  if (TREE_CODE (member) == TYPE_DECL)
1557    return member;
1558  if (TREE_CODE (member) == VAR_DECL || TREE_CODE (member) == CONST_DECL)
1559    return convert_from_reference (member);
1560
1561  if (TREE_CODE (member) == FIELD_DECL && DECL_C_BIT_FIELD (member))
1562    {
1563      error ("invalid pointer to bit-field %qD", member);
1564      return error_mark_node;
1565    }
1566
1567  /* Set up BASEBINFO for member lookup.  */
1568  decl = maybe_dummy_object (type, &basebinfo);
1569
1570  /* A lot of this logic is now handled in lookup_member.  */
1571  if (BASELINK_P (member))
1572    {
1573      /* Go from the TREE_BASELINK to the member function info.  */
1574      tree t = BASELINK_FUNCTIONS (member);
1575
1576      if (TREE_CODE (t) != TEMPLATE_ID_EXPR && !really_overloaded_fn (t))
1577	{
1578	  /* Get rid of a potential OVERLOAD around it.  */
1579	  t = OVL_CURRENT (t);
1580
1581	  /* Unique functions are handled easily.  */
1582
1583	  /* For non-static member of base class, we need a special rule
1584	     for access checking [class.protected]:
1585
1586	       If the access is to form a pointer to member, the
1587	       nested-name-specifier shall name the derived class
1588	       (or any class derived from that class).  */
1589	  if (address_p && DECL_P (t)
1590	      && DECL_NONSTATIC_MEMBER_P (t))
1591	    perform_or_defer_access_check (TYPE_BINFO (type), t, t);
1592	  else
1593	    perform_or_defer_access_check (basebinfo, t, t);
1594
1595	  if (DECL_STATIC_FUNCTION_P (t))
1596	    return t;
1597	  member = t;
1598	}
1599      else
1600	TREE_TYPE (member) = unknown_type_node;
1601    }
1602  else if (address_p && TREE_CODE (member) == FIELD_DECL)
1603    /* We need additional test besides the one in
1604       check_accessibility_of_qualified_id in case it is
1605       a pointer to non-static member.  */
1606    perform_or_defer_access_check (TYPE_BINFO (type), member, member);
1607
1608  if (!address_p)
1609    {
1610      /* If MEMBER is non-static, then the program has fallen afoul of
1611	 [expr.prim]:
1612
1613	   An id-expression that denotes a nonstatic data member or
1614	   nonstatic member function of a class can only be used:
1615
1616	   -- as part of a class member access (_expr.ref_) in which the
1617	   object-expression refers to the member's class or a class
1618	   derived from that class, or
1619
1620	   -- to form a pointer to member (_expr.unary.op_), or
1621
1622	   -- in the body of a nonstatic member function of that class or
1623	   of a class derived from that class (_class.mfct.nonstatic_), or
1624
1625	   -- in a mem-initializer for a constructor for that class or for
1626	   a class derived from that class (_class.base.init_).  */
1627      if (DECL_NONSTATIC_MEMBER_FUNCTION_P (member))
1628	{
1629	  /* Build a representation of the qualified name suitable
1630	     for use as the operand to "&" -- even though the "&" is
1631	     not actually present.  */
1632	  member = build2 (OFFSET_REF, TREE_TYPE (member), decl, member);
1633	  /* In Microsoft mode, treat a non-static member function as if
1634	     it were a pointer-to-member.  */
1635	  if (flag_ms_extensions)
1636	    {
1637	      PTRMEM_OK_P (member) = 1;
1638	      return cp_build_unary_op (ADDR_EXPR, member, 0,
1639                                        tf_warning_or_error);
1640	    }
1641	  error ("invalid use of non-static member function %qD",
1642		 TREE_OPERAND (member, 1));
1643	  return error_mark_node;
1644	}
1645      else if (TREE_CODE (member) == FIELD_DECL)
1646	{
1647	  error ("invalid use of non-static data member %qD", member);
1648	  return error_mark_node;
1649	}
1650      return member;
1651    }
1652
1653  member = build2 (OFFSET_REF, TREE_TYPE (member), decl, member);
1654  PTRMEM_OK_P (member) = 1;
1655  return member;
1656}
1657
1658/* If DECL is a scalar enumeration constant or variable with a
1659   constant initializer, return the initializer (or, its initializers,
1660   recursively); otherwise, return DECL.  If INTEGRAL_P, the
1661   initializer is only returned if DECL is an integral
1662   constant-expression.  */
1663
1664static tree
1665constant_value_1 (tree decl, bool integral_p)
1666{
1667  while (TREE_CODE (decl) == CONST_DECL
1668	 || (integral_p
1669	     ? DECL_INTEGRAL_CONSTANT_VAR_P (decl)
1670	     : (TREE_CODE (decl) == VAR_DECL
1671		&& CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (decl)))))
1672    {
1673      tree init;
1674      /* Static data members in template classes may have
1675	 non-dependent initializers.  References to such non-static
1676	 data members are not value-dependent, so we must retrieve the
1677	 initializer here.  The DECL_INITIAL will have the right type,
1678	 but will not have been folded because that would prevent us
1679	 from performing all appropriate semantic checks at
1680	 instantiation time.  */
1681      if (DECL_CLASS_SCOPE_P (decl)
1682	  && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
1683	  && uses_template_parms (CLASSTYPE_TI_ARGS
1684				  (DECL_CONTEXT (decl))))
1685	{
1686	  ++processing_template_decl;
1687	  init = fold_non_dependent_expr (DECL_INITIAL (decl));
1688	  --processing_template_decl;
1689	}
1690      else
1691	{
1692	  /* If DECL is a static data member in a template
1693	     specialization, we must instantiate it here.  The
1694	     initializer for the static data member is not processed
1695	     until needed; we need it now.  */
1696	  mark_used (decl);
1697	  init = DECL_INITIAL (decl);
1698	}
1699      if (init == error_mark_node)
1700	return decl;
1701      /* Initializers in templates are generally expanded during
1702	 instantiation, so before that for const int i(2)
1703	 INIT is a TREE_LIST with the actual initializer as
1704	 TREE_VALUE.  */
1705      if (processing_template_decl
1706	  && init
1707	  && TREE_CODE (init) == TREE_LIST
1708	  && TREE_CHAIN (init) == NULL_TREE)
1709	init = TREE_VALUE (init);
1710      if (!init
1711	  || !TREE_TYPE (init)
1712	  || (integral_p
1713	      ? !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (init))
1714	      : (!TREE_CONSTANT (init)
1715		 /* Do not return an aggregate constant (of which
1716		    string literals are a special case), as we do not
1717		    want to make inadvertent copies of such entities,
1718		    and we must be sure that their addresses are the
1719		    same everywhere.  */
1720		 || TREE_CODE (init) == CONSTRUCTOR
1721		 || TREE_CODE (init) == STRING_CST)))
1722	break;
1723      decl = unshare_expr (init);
1724    }
1725  return decl;
1726}
1727
1728/* If DECL is a CONST_DECL, or a constant VAR_DECL initialized by
1729   constant of integral or enumeration type, then return that value.
1730   These are those variables permitted in constant expressions by
1731   [5.19/1].  */
1732
1733tree
1734integral_constant_value (tree decl)
1735{
1736  return constant_value_1 (decl, /*integral_p=*/true);
1737}
1738
1739/* A more relaxed version of integral_constant_value, used by the
1740   common C/C++ code and by the C++ front end for optimization
1741   purposes.  */
1742
1743tree
1744decl_constant_value (tree decl)
1745{
1746  return constant_value_1 (decl,
1747			   /*integral_p=*/processing_template_decl);
1748}
1749
1750/* Common subroutines of build_new and build_vec_delete.  */
1751
1752/* Call the global __builtin_delete to delete ADDR.  */
1753
1754static tree
1755build_builtin_delete_call (tree addr)
1756{
1757  mark_used (global_delete_fndecl);
1758  return build_call_n (global_delete_fndecl, 1, addr);
1759}
1760
1761/* Build and return a NEW_EXPR.  If NELTS is non-NULL, TYPE[NELTS] is
1762   the type of the object being allocated; otherwise, it's just TYPE.
1763   INIT is the initializer, if any.  USE_GLOBAL_NEW is true if the
1764   user explicitly wrote "::operator new".  PLACEMENT, if non-NULL, is
1765   a vector of arguments to be provided as arguments to a placement
1766   new operator.  This routine performs no semantic checks; it just
1767   creates and returns a NEW_EXPR.  */
1768
1769static tree
1770build_raw_new_expr (VEC(tree,gc) *placement, tree type, tree nelts,
1771		    VEC(tree,gc) *init, int use_global_new)
1772{
1773  tree init_list;
1774  tree new_expr;
1775
1776  /* If INIT is NULL, the we want to store NULL_TREE in the NEW_EXPR.
1777     If INIT is not NULL, then we want to store VOID_ZERO_NODE.  This
1778     permits us to distinguish the case of a missing initializer "new
1779     int" from an empty initializer "new int()".  */
1780  if (init == NULL)
1781    init_list = NULL_TREE;
1782  else if (VEC_empty (tree, init))
1783    init_list = void_zero_node;
1784  else
1785    init_list = build_tree_list_vec (init);
1786
1787  new_expr = build4 (NEW_EXPR, build_pointer_type (type),
1788		     build_tree_list_vec (placement), type, nelts,
1789		     init_list);
1790  NEW_EXPR_USE_GLOBAL (new_expr) = use_global_new;
1791  TREE_SIDE_EFFECTS (new_expr) = 1;
1792
1793  return new_expr;
1794}
1795
1796/* Generate code for a new-expression, including calling the "operator
1797   new" function, initializing the object, and, if an exception occurs
1798   during construction, cleaning up.  The arguments are as for
1799   build_raw_new_expr.  This may change PLACEMENT and INIT.  */
1800
1801static tree
1802build_new_1 (VEC(tree,gc) **placement, tree type, tree nelts,
1803	     VEC(tree,gc) **init, bool globally_qualified_p,
1804	     tsubst_flags_t complain)
1805{
1806  tree size, rval;
1807  /* True iff this is a call to "operator new[]" instead of just
1808     "operator new".  */
1809  bool array_p = false;
1810  /* If ARRAY_P is true, the element type of the array.  This is never
1811     an ARRAY_TYPE; for something like "new int[3][4]", the
1812     ELT_TYPE is "int".  If ARRAY_P is false, this is the same type as
1813     TYPE.  */
1814  tree elt_type;
1815  /* The type of the new-expression.  (This type is always a pointer
1816     type.)  */
1817  tree pointer_type;
1818  tree non_const_pointer_type;
1819  tree outer_nelts = NULL_TREE;
1820  tree alloc_call, alloc_expr;
1821  /* The address returned by the call to "operator new".  This node is
1822     a VAR_DECL and is therefore reusable.  */
1823  tree alloc_node;
1824  tree alloc_fn;
1825  tree cookie_expr, init_expr;
1826  int nothrow, check_new;
1827  int use_java_new = 0;
1828  /* If non-NULL, the number of extra bytes to allocate at the
1829     beginning of the storage allocated for an array-new expression in
1830     order to store the number of elements.  */
1831  tree cookie_size = NULL_TREE;
1832  tree placement_first;
1833  tree placement_expr = NULL_TREE;
1834  /* True if the function we are calling is a placement allocation
1835     function.  */
1836  bool placement_allocation_fn_p;
1837  /* True if the storage must be initialized, either by a constructor
1838     or due to an explicit new-initializer.  */
1839  bool is_initialized;
1840  /* The address of the thing allocated, not including any cookie.  In
1841     particular, if an array cookie is in use, DATA_ADDR is the
1842     address of the first array element.  This node is a VAR_DECL, and
1843     is therefore reusable.  */
1844  tree data_addr;
1845  tree init_preeval_expr = NULL_TREE;
1846
1847  if (nelts)
1848    {
1849      outer_nelts = nelts;
1850      array_p = true;
1851    }
1852  else if (TREE_CODE (type) == ARRAY_TYPE)
1853    {
1854      array_p = true;
1855      nelts = array_type_nelts_top (type);
1856      outer_nelts = nelts;
1857      type = TREE_TYPE (type);
1858    }
1859
1860  /* If our base type is an array, then make sure we know how many elements
1861     it has.  */
1862  for (elt_type = type;
1863       TREE_CODE (elt_type) == ARRAY_TYPE;
1864       elt_type = TREE_TYPE (elt_type))
1865    nelts = cp_build_binary_op (input_location,
1866				MULT_EXPR, nelts,
1867				array_type_nelts_top (elt_type),
1868				complain);
1869
1870  if (TREE_CODE (elt_type) == VOID_TYPE)
1871    {
1872      if (complain & tf_error)
1873        error ("invalid type %<void%> for new");
1874      return error_mark_node;
1875    }
1876
1877  if (abstract_virtuals_error (NULL_TREE, elt_type))
1878    return error_mark_node;
1879
1880  is_initialized = (TYPE_NEEDS_CONSTRUCTING (elt_type) || *init != NULL);
1881
1882  if (CP_TYPE_CONST_P (elt_type) && *init == NULL
1883      && !type_has_user_provided_default_constructor (elt_type))
1884    {
1885      if (complain & tf_error)
1886        error ("uninitialized const in %<new%> of %q#T", elt_type);
1887      return error_mark_node;
1888    }
1889
1890  size = size_in_bytes (elt_type);
1891  if (array_p)
1892    size = size_binop (MULT_EXPR, size, convert (sizetype, nelts));
1893
1894  alloc_fn = NULL_TREE;
1895
1896  /* If PLACEMENT is a single simple pointer type not passed by
1897     reference, prepare to capture it in a temporary variable.  Do
1898     this now, since PLACEMENT will change in the calls below.  */
1899  placement_first = NULL_TREE;
1900  if (VEC_length (tree, *placement) == 1
1901      && (TREE_CODE (TREE_TYPE (VEC_index (tree, *placement, 0)))
1902	  == POINTER_TYPE))
1903    placement_first = VEC_index (tree, *placement, 0);
1904
1905  /* Allocate the object.  */
1906  if (VEC_empty (tree, *placement) && TYPE_FOR_JAVA (elt_type))
1907    {
1908      tree class_addr;
1909      tree class_decl = build_java_class_ref (elt_type);
1910      static const char alloc_name[] = "_Jv_AllocObject";
1911
1912      if (class_decl == error_mark_node)
1913	return error_mark_node;
1914
1915      use_java_new = 1;
1916      if (!get_global_value_if_present (get_identifier (alloc_name),
1917					&alloc_fn))
1918	{
1919          if (complain & tf_error)
1920            error ("call to Java constructor with %qs undefined", alloc_name);
1921	  return error_mark_node;
1922	}
1923      else if (really_overloaded_fn (alloc_fn))
1924	{
1925          if (complain & tf_error)
1926            error ("%qD should never be overloaded", alloc_fn);
1927	  return error_mark_node;
1928	}
1929      alloc_fn = OVL_CURRENT (alloc_fn);
1930      class_addr = build1 (ADDR_EXPR, jclass_node, class_decl);
1931      alloc_call = (cp_build_function_call
1932		    (alloc_fn,
1933		     build_tree_list (NULL_TREE, class_addr),
1934		     complain));
1935    }
1936  else if (TYPE_FOR_JAVA (elt_type) && MAYBE_CLASS_TYPE_P (elt_type))
1937    {
1938      error ("Java class %q#T object allocated using placement new", elt_type);
1939      return error_mark_node;
1940    }
1941  else
1942    {
1943      tree fnname;
1944      tree fns;
1945
1946      fnname = ansi_opname (array_p ? VEC_NEW_EXPR : NEW_EXPR);
1947
1948      if (!globally_qualified_p
1949	  && CLASS_TYPE_P (elt_type)
1950	  && (array_p
1951	      ? TYPE_HAS_ARRAY_NEW_OPERATOR (elt_type)
1952	      : TYPE_HAS_NEW_OPERATOR (elt_type)))
1953	{
1954	  /* Use a class-specific operator new.  */
1955	  /* If a cookie is required, add some extra space.  */
1956	  if (array_p && TYPE_VEC_NEW_USES_COOKIE (elt_type))
1957	    {
1958	      cookie_size = targetm.cxx.get_cookie_size (elt_type);
1959	      size = size_binop (PLUS_EXPR, size, cookie_size);
1960	    }
1961	  /* Create the argument list.  */
1962	  VEC_safe_insert (tree, gc, *placement, 0, size);
1963	  /* Do name-lookup to find the appropriate operator.  */
1964	  fns = lookup_fnfields (elt_type, fnname, /*protect=*/2);
1965	  if (fns == NULL_TREE)
1966	    {
1967              if (complain & tf_error)
1968                error ("no suitable %qD found in class %qT", fnname, elt_type);
1969	      return error_mark_node;
1970	    }
1971	  if (TREE_CODE (fns) == TREE_LIST)
1972	    {
1973              if (complain & tf_error)
1974                {
1975                  error ("request for member %qD is ambiguous", fnname);
1976                  print_candidates (fns);
1977                }
1978	      return error_mark_node;
1979	    }
1980	  alloc_call = build_new_method_call (build_dummy_object (elt_type),
1981					      fns, placement,
1982					      /*conversion_path=*/NULL_TREE,
1983					      LOOKUP_NORMAL,
1984					      &alloc_fn,
1985					      complain);
1986	}
1987      else
1988	{
1989	  /* Use a global operator new.  */
1990	  /* See if a cookie might be required.  */
1991	  if (array_p && TYPE_VEC_NEW_USES_COOKIE (elt_type))
1992	    cookie_size = targetm.cxx.get_cookie_size (elt_type);
1993	  else
1994	    cookie_size = NULL_TREE;
1995
1996	  alloc_call = build_operator_new_call (fnname, placement,
1997						&size, &cookie_size,
1998						&alloc_fn);
1999	}
2000    }
2001
2002  if (alloc_call == error_mark_node)
2003    return error_mark_node;
2004
2005  gcc_assert (alloc_fn != NULL_TREE);
2006
2007  /* If we found a simple case of PLACEMENT_EXPR above, then copy it
2008     into a temporary variable.  */
2009  if (!processing_template_decl
2010      && placement_first != NULL_TREE
2011      && TREE_CODE (alloc_call) == CALL_EXPR
2012      && call_expr_nargs (alloc_call) == 2
2013      && TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (alloc_call, 0))) == INTEGER_TYPE
2014      && TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (alloc_call, 1))) == POINTER_TYPE)
2015    {
2016      tree placement_arg = CALL_EXPR_ARG (alloc_call, 1);
2017
2018      if (INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (TREE_TYPE (placement_arg)))
2019	  || VOID_TYPE_P (TREE_TYPE (TREE_TYPE (placement_arg))))
2020	{
2021	  placement_expr = get_target_expr (placement_first);
2022	  CALL_EXPR_ARG (alloc_call, 1)
2023	    = convert (TREE_TYPE (placement_arg), placement_expr);
2024	}
2025    }
2026
2027  /* In the simple case, we can stop now.  */
2028  pointer_type = build_pointer_type (type);
2029  if (!cookie_size && !is_initialized)
2030    return build_nop (pointer_type, alloc_call);
2031
2032  /* Store the result of the allocation call in a variable so that we can
2033     use it more than once.  */
2034  alloc_expr = get_target_expr (alloc_call);
2035  alloc_node = TARGET_EXPR_SLOT (alloc_expr);
2036
2037  /* Strip any COMPOUND_EXPRs from ALLOC_CALL.  */
2038  while (TREE_CODE (alloc_call) == COMPOUND_EXPR)
2039    alloc_call = TREE_OPERAND (alloc_call, 1);
2040
2041  /* Now, check to see if this function is actually a placement
2042     allocation function.  This can happen even when PLACEMENT is NULL
2043     because we might have something like:
2044
2045       struct S { void* operator new (size_t, int i = 0); };
2046
2047     A call to `new S' will get this allocation function, even though
2048     there is no explicit placement argument.  If there is more than
2049     one argument, or there are variable arguments, then this is a
2050     placement allocation function.  */
2051  placement_allocation_fn_p
2052    = (type_num_arguments (TREE_TYPE (alloc_fn)) > 1
2053       || varargs_function_p (alloc_fn));
2054
2055  /* Preevaluate the placement args so that we don't reevaluate them for a
2056     placement delete.  */
2057  if (placement_allocation_fn_p)
2058    {
2059      tree inits;
2060      stabilize_call (alloc_call, &inits);
2061      if (inits)
2062	alloc_expr = build2 (COMPOUND_EXPR, TREE_TYPE (alloc_expr), inits,
2063			     alloc_expr);
2064    }
2065
2066  /*        unless an allocation function is declared with an empty  excep-
2067     tion-specification  (_except.spec_),  throw(), it indicates failure to
2068     allocate storage by throwing a bad_alloc exception  (clause  _except_,
2069     _lib.bad.alloc_); it returns a non-null pointer otherwise If the allo-
2070     cation function is declared  with  an  empty  exception-specification,
2071     throw(), it returns null to indicate failure to allocate storage and a
2072     non-null pointer otherwise.
2073
2074     So check for a null exception spec on the op new we just called.  */
2075
2076  nothrow = TYPE_NOTHROW_P (TREE_TYPE (alloc_fn));
2077  check_new = (flag_check_new || nothrow) && ! use_java_new;
2078
2079  if (cookie_size)
2080    {
2081      tree cookie;
2082      tree cookie_ptr;
2083      tree size_ptr_type;
2084
2085      /* Adjust so we're pointing to the start of the object.  */
2086      data_addr = build2 (POINTER_PLUS_EXPR, TREE_TYPE (alloc_node),
2087			  alloc_node, cookie_size);
2088
2089      /* Store the number of bytes allocated so that we can know how
2090	 many elements to destroy later.  We use the last sizeof
2091	 (size_t) bytes to store the number of elements.  */
2092      cookie_ptr = size_binop (MINUS_EXPR, cookie_size, size_in_bytes (sizetype));
2093      cookie_ptr = fold_build2_loc (input_location,
2094				POINTER_PLUS_EXPR, TREE_TYPE (alloc_node),
2095				alloc_node, cookie_ptr);
2096      size_ptr_type = build_pointer_type (sizetype);
2097      cookie_ptr = fold_convert (size_ptr_type, cookie_ptr);
2098      cookie = cp_build_indirect_ref (cookie_ptr, RO_NULL, complain);
2099
2100      cookie_expr = build2 (MODIFY_EXPR, sizetype, cookie, nelts);
2101
2102      if (targetm.cxx.cookie_has_size ())
2103	{
2104	  /* Also store the element size.  */
2105	  cookie_ptr = build2 (POINTER_PLUS_EXPR, size_ptr_type, cookie_ptr,
2106			       fold_build1_loc (input_location,
2107					    NEGATE_EXPR, sizetype,
2108					    size_in_bytes (sizetype)));
2109
2110	  cookie = cp_build_indirect_ref (cookie_ptr, RO_NULL, complain);
2111	  cookie = build2 (MODIFY_EXPR, sizetype, cookie,
2112			   size_in_bytes (elt_type));
2113	  cookie_expr = build2 (COMPOUND_EXPR, TREE_TYPE (cookie_expr),
2114				cookie, cookie_expr);
2115	}
2116    }
2117  else
2118    {
2119      cookie_expr = NULL_TREE;
2120      data_addr = alloc_node;
2121    }
2122
2123  /* Now use a pointer to the type we've actually allocated.  */
2124
2125  /* But we want to operate on a non-const version to start with,
2126     since we'll be modifying the elements.  */
2127  non_const_pointer_type = build_pointer_type
2128    (cp_build_qualified_type (type, TYPE_QUALS (type) & ~TYPE_QUAL_CONST));
2129
2130  data_addr = fold_convert (non_const_pointer_type, data_addr);
2131  /* Any further uses of alloc_node will want this type, too.  */
2132  alloc_node = fold_convert (non_const_pointer_type, alloc_node);
2133
2134  /* Now initialize the allocated object.  Note that we preevaluate the
2135     initialization expression, apart from the actual constructor call or
2136     assignment--we do this because we want to delay the allocation as long
2137     as possible in order to minimize the size of the exception region for
2138     placement delete.  */
2139  if (is_initialized)
2140    {
2141      bool stable;
2142      bool explicit_value_init_p = false;
2143
2144      if (*init != NULL && VEC_empty (tree, *init))
2145	{
2146	  *init = NULL;
2147	  explicit_value_init_p = true;
2148	}
2149
2150      if (array_p)
2151	{
2152	  tree vecinit = NULL_TREE;
2153	  if (*init && VEC_length (tree, *init) == 1
2154	      && BRACE_ENCLOSED_INITIALIZER_P (VEC_index (tree, *init, 0))
2155	      && CONSTRUCTOR_IS_DIRECT_INIT (VEC_index (tree, *init, 0)))
2156	    {
2157	      tree arraytype, domain;
2158	      vecinit = VEC_index (tree, *init, 0);
2159	      if (TREE_CONSTANT (nelts))
2160		domain = compute_array_index_type (NULL_TREE, nelts);
2161	      else
2162		{
2163		  domain = NULL_TREE;
2164		  if (CONSTRUCTOR_NELTS (vecinit) > 0)
2165		    warning (0, "non-constant array size in new, unable to "
2166			     "verify length of initializer-list");
2167		}
2168	      arraytype = build_cplus_array_type (type, domain);
2169	      vecinit = digest_init (arraytype, vecinit);
2170	    }
2171	  else if (*init)
2172            {
2173              if (complain & tf_error)
2174                permerror (input_location, "ISO C++ forbids initialization in array new");
2175              else
2176                return error_mark_node;
2177	      vecinit = build_tree_list_vec (*init);
2178            }
2179	  init_expr
2180	    = build_vec_init (data_addr,
2181			      cp_build_binary_op (input_location,
2182						  MINUS_EXPR, outer_nelts,
2183						  integer_one_node,
2184						  complain),
2185			      vecinit,
2186			      explicit_value_init_p,
2187			      /*from_array=*/0,
2188                              complain);
2189
2190	  /* An array initialization is stable because the initialization
2191	     of each element is a full-expression, so the temporaries don't
2192	     leak out.  */
2193	  stable = true;
2194	}
2195      else
2196	{
2197	  init_expr = cp_build_indirect_ref (data_addr, RO_NULL, complain);
2198
2199	  if (TYPE_NEEDS_CONSTRUCTING (type)
2200	      && (!explicit_value_init_p || processing_template_decl))
2201	    {
2202	      init_expr = build_special_member_call (init_expr,
2203						     complete_ctor_identifier,
2204						     init, elt_type,
2205						     LOOKUP_NORMAL,
2206                                                     complain);
2207	    }
2208	  else if (explicit_value_init_p)
2209	    {
2210	      if (processing_template_decl)
2211		/* Don't worry about it, we'll handle this properly at
2212		   instantiation time.  */;
2213	      else
2214		/* Something like `new int()'.  */
2215		init_expr = build2 (INIT_EXPR, type,
2216				    init_expr, build_value_init (type));
2217	    }
2218	  else
2219	    {
2220	      tree ie;
2221
2222	      /* We are processing something like `new int (10)', which
2223		 means allocate an int, and initialize it with 10.  */
2224
2225	      ie = build_x_compound_expr_from_vec (*init, "new initializer");
2226	      init_expr = cp_build_modify_expr (init_expr, INIT_EXPR, ie,
2227						complain);
2228	    }
2229	  stable = stabilize_init (init_expr, &init_preeval_expr);
2230	}
2231
2232      if (init_expr == error_mark_node)
2233	return error_mark_node;
2234
2235      /* If any part of the object initialization terminates by throwing an
2236	 exception and a suitable deallocation function can be found, the
2237	 deallocation function is called to free the memory in which the
2238	 object was being constructed, after which the exception continues
2239	 to propagate in the context of the new-expression. If no
2240	 unambiguous matching deallocation function can be found,
2241	 propagating the exception does not cause the object's memory to be
2242	 freed.  */
2243      if (flag_exceptions && ! use_java_new)
2244	{
2245	  enum tree_code dcode = array_p ? VEC_DELETE_EXPR : DELETE_EXPR;
2246	  tree cleanup;
2247
2248	  /* The Standard is unclear here, but the right thing to do
2249	     is to use the same method for finding deallocation
2250	     functions that we use for finding allocation functions.  */
2251	  cleanup = (build_op_delete_call
2252		     (dcode,
2253		      alloc_node,
2254		      size,
2255		      globally_qualified_p,
2256		      placement_allocation_fn_p ? alloc_call : NULL_TREE,
2257		      alloc_fn));
2258
2259	  if (!cleanup)
2260	    /* We're done.  */;
2261	  else if (stable)
2262	    /* This is much simpler if we were able to preevaluate all of
2263	       the arguments to the constructor call.  */
2264	    {
2265	      /* CLEANUP is compiler-generated, so no diagnostics.  */
2266	      TREE_NO_WARNING (cleanup) = true;
2267	      init_expr = build2 (TRY_CATCH_EXPR, void_type_node,
2268				  init_expr, cleanup);
2269	      /* Likewise, this try-catch is compiler-generated.  */
2270	      TREE_NO_WARNING (init_expr) = true;
2271	    }
2272	  else
2273	    /* Ack!  First we allocate the memory.  Then we set our sentry
2274	       variable to true, and expand a cleanup that deletes the
2275	       memory if sentry is true.  Then we run the constructor, and
2276	       finally clear the sentry.
2277
2278	       We need to do this because we allocate the space first, so
2279	       if there are any temporaries with cleanups in the
2280	       constructor args and we weren't able to preevaluate them, we
2281	       need this EH region to extend until end of full-expression
2282	       to preserve nesting.  */
2283	    {
2284	      tree end, sentry, begin;
2285
2286	      begin = get_target_expr (boolean_true_node);
2287	      CLEANUP_EH_ONLY (begin) = 1;
2288
2289	      sentry = TARGET_EXPR_SLOT (begin);
2290
2291	      /* CLEANUP is compiler-generated, so no diagnostics.  */
2292	      TREE_NO_WARNING (cleanup) = true;
2293
2294	      TARGET_EXPR_CLEANUP (begin)
2295		= build3 (COND_EXPR, void_type_node, sentry,
2296			  cleanup, void_zero_node);
2297
2298	      end = build2 (MODIFY_EXPR, TREE_TYPE (sentry),
2299			    sentry, boolean_false_node);
2300
2301	      init_expr
2302		= build2 (COMPOUND_EXPR, void_type_node, begin,
2303			  build2 (COMPOUND_EXPR, void_type_node, init_expr,
2304				  end));
2305	      /* Likewise, this is compiler-generated.  */
2306	      TREE_NO_WARNING (init_expr) = true;
2307	    }
2308	}
2309    }
2310  else
2311    init_expr = NULL_TREE;
2312
2313  /* Now build up the return value in reverse order.  */
2314
2315  rval = data_addr;
2316
2317  if (init_expr)
2318    rval = build2 (COMPOUND_EXPR, TREE_TYPE (rval), init_expr, rval);
2319  if (cookie_expr)
2320    rval = build2 (COMPOUND_EXPR, TREE_TYPE (rval), cookie_expr, rval);
2321
2322  if (rval == data_addr)
2323    /* If we don't have an initializer or a cookie, strip the TARGET_EXPR
2324       and return the call (which doesn't need to be adjusted).  */
2325    rval = TARGET_EXPR_INITIAL (alloc_expr);
2326  else
2327    {
2328      if (check_new)
2329	{
2330	  tree ifexp = cp_build_binary_op (input_location,
2331					   NE_EXPR, alloc_node,
2332					   integer_zero_node,
2333					   complain);
2334	  rval = build_conditional_expr (ifexp, rval, alloc_node,
2335                                         complain);
2336	}
2337
2338      /* Perform the allocation before anything else, so that ALLOC_NODE
2339	 has been initialized before we start using it.  */
2340      rval = build2 (COMPOUND_EXPR, TREE_TYPE (rval), alloc_expr, rval);
2341    }
2342
2343  if (init_preeval_expr)
2344    rval = build2 (COMPOUND_EXPR, TREE_TYPE (rval), init_preeval_expr, rval);
2345
2346  /* A new-expression is never an lvalue.  */
2347  gcc_assert (!lvalue_p (rval));
2348
2349  return convert (pointer_type, rval);
2350}
2351
2352/* Generate a representation for a C++ "new" expression.  *PLACEMENT
2353   is a vector of placement-new arguments (or NULL if none).  If NELTS
2354   is NULL, TYPE is the type of the storage to be allocated.  If NELTS
2355   is not NULL, then this is an array-new allocation; TYPE is the type
2356   of the elements in the array and NELTS is the number of elements in
2357   the array.  *INIT, if non-NULL, is the initializer for the new
2358   object, or an empty vector to indicate an initializer of "()".  If
2359   USE_GLOBAL_NEW is true, then the user explicitly wrote "::new"
2360   rather than just "new".  This may change PLACEMENT and INIT.  */
2361
2362tree
2363build_new (VEC(tree,gc) **placement, tree type, tree nelts,
2364	   VEC(tree,gc) **init, int use_global_new, tsubst_flags_t complain)
2365{
2366  tree rval;
2367  VEC(tree,gc) *orig_placement = NULL;
2368  tree orig_nelts = NULL_TREE;
2369  VEC(tree,gc) *orig_init = NULL;
2370
2371  if (type == error_mark_node)
2372    return error_mark_node;
2373
2374  if (nelts == NULL_TREE && VEC_length (tree, *init) == 1)
2375    {
2376      tree auto_node = type_uses_auto (type);
2377      if (auto_node && describable_type (VEC_index (tree, *init, 0)))
2378	type = do_auto_deduction (type, VEC_index (tree, *init, 0), auto_node);
2379    }
2380
2381  if (processing_template_decl)
2382    {
2383      if (dependent_type_p (type)
2384	  || any_type_dependent_arguments_p (*placement)
2385	  || (nelts && type_dependent_expression_p (nelts))
2386	  || any_type_dependent_arguments_p (*init))
2387	return build_raw_new_expr (*placement, type, nelts, *init,
2388				   use_global_new);
2389
2390      orig_placement = make_tree_vector_copy (*placement);
2391      orig_nelts = nelts;
2392      orig_init = make_tree_vector_copy (*init);
2393
2394      make_args_non_dependent (*placement);
2395      if (nelts)
2396	nelts = build_non_dependent_expr (nelts);
2397      make_args_non_dependent (*init);
2398    }
2399
2400  if (nelts)
2401    {
2402      if (!build_expr_type_conversion (WANT_INT | WANT_ENUM, nelts, false))
2403        {
2404          if (complain & tf_error)
2405            permerror (input_location, "size in array new must have integral type");
2406          else
2407            return error_mark_node;
2408        }
2409      nelts = cp_save_expr (cp_convert (sizetype, nelts));
2410    }
2411
2412  /* ``A reference cannot be created by the new operator.  A reference
2413     is not an object (8.2.2, 8.4.3), so a pointer to it could not be
2414     returned by new.'' ARM 5.3.3 */
2415  if (TREE_CODE (type) == REFERENCE_TYPE)
2416    {
2417      if (complain & tf_error)
2418        error ("new cannot be applied to a reference type");
2419      else
2420        return error_mark_node;
2421      type = TREE_TYPE (type);
2422    }
2423
2424  if (TREE_CODE (type) == FUNCTION_TYPE)
2425    {
2426      if (complain & tf_error)
2427        error ("new cannot be applied to a function type");
2428      return error_mark_node;
2429    }
2430
2431  /* The type allocated must be complete.  If the new-type-id was
2432     "T[N]" then we are just checking that "T" is complete here, but
2433     that is equivalent, since the value of "N" doesn't matter.  */
2434  if (!complete_type_or_else (type, NULL_TREE))
2435    return error_mark_node;
2436
2437  rval = build_new_1 (placement, type, nelts, init, use_global_new, complain);
2438  if (rval == error_mark_node)
2439    return error_mark_node;
2440
2441  if (processing_template_decl)
2442    {
2443      tree ret = build_raw_new_expr (orig_placement, type, orig_nelts,
2444				     orig_init, use_global_new);
2445      release_tree_vector (orig_placement);
2446      release_tree_vector (orig_init);
2447      return ret;
2448    }
2449
2450  /* Wrap it in a NOP_EXPR so warn_if_unused_value doesn't complain.  */
2451  rval = build1 (NOP_EXPR, TREE_TYPE (rval), rval);
2452  TREE_NO_WARNING (rval) = 1;
2453
2454  return rval;
2455}
2456
2457/* Given a Java class, return a decl for the corresponding java.lang.Class.  */
2458
2459tree
2460build_java_class_ref (tree type)
2461{
2462  tree name = NULL_TREE, class_decl;
2463  static tree CL_suffix = NULL_TREE;
2464  if (CL_suffix == NULL_TREE)
2465    CL_suffix = get_identifier("class$");
2466  if (jclass_node == NULL_TREE)
2467    {
2468      jclass_node = IDENTIFIER_GLOBAL_VALUE (get_identifier ("jclass"));
2469      if (jclass_node == NULL_TREE)
2470	{
2471	  error ("call to Java constructor, while %<jclass%> undefined");
2472	  return error_mark_node;
2473	}
2474      jclass_node = TREE_TYPE (jclass_node);
2475    }
2476
2477  /* Mangle the class$ field.  */
2478  {
2479    tree field;
2480    for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
2481      if (DECL_NAME (field) == CL_suffix)
2482	{
2483	  mangle_decl (field);
2484	  name = DECL_ASSEMBLER_NAME (field);
2485	  break;
2486	}
2487    if (!field)
2488      {
2489	error ("can't find %<class$%> in %qT", type);
2490	return error_mark_node;
2491      }
2492  }
2493
2494  class_decl = IDENTIFIER_GLOBAL_VALUE (name);
2495  if (class_decl == NULL_TREE)
2496    {
2497      class_decl = build_decl (input_location,
2498			       VAR_DECL, name, TREE_TYPE (jclass_node));
2499      TREE_STATIC (class_decl) = 1;
2500      DECL_EXTERNAL (class_decl) = 1;
2501      TREE_PUBLIC (class_decl) = 1;
2502      DECL_ARTIFICIAL (class_decl) = 1;
2503      DECL_IGNORED_P (class_decl) = 1;
2504      pushdecl_top_level (class_decl);
2505      make_decl_rtl (class_decl);
2506    }
2507  return class_decl;
2508}
2509
2510static tree
2511build_vec_delete_1 (tree base, tree maxindex, tree type,
2512    special_function_kind auto_delete_vec, int use_global_delete)
2513{
2514  tree virtual_size;
2515  tree ptype = build_pointer_type (type = complete_type (type));
2516  tree size_exp = size_in_bytes (type);
2517
2518  /* Temporary variables used by the loop.  */
2519  tree tbase, tbase_init;
2520
2521  /* This is the body of the loop that implements the deletion of a
2522     single element, and moves temp variables to next elements.  */
2523  tree body;
2524
2525  /* This is the LOOP_EXPR that governs the deletion of the elements.  */
2526  tree loop = 0;
2527
2528  /* This is the thing that governs what to do after the loop has run.  */
2529  tree deallocate_expr = 0;
2530
2531  /* This is the BIND_EXPR which holds the outermost iterator of the
2532     loop.  It is convenient to set this variable up and test it before
2533     executing any other code in the loop.
2534     This is also the containing expression returned by this function.  */
2535  tree controller = NULL_TREE;
2536  tree tmp;
2537
2538  /* We should only have 1-D arrays here.  */
2539  gcc_assert (TREE_CODE (type) != ARRAY_TYPE);
2540
2541  if (! MAYBE_CLASS_TYPE_P (type) || TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
2542    goto no_destructor;
2543
2544  /* The below is short by the cookie size.  */
2545  virtual_size = size_binop (MULT_EXPR, size_exp,
2546			     convert (sizetype, maxindex));
2547
2548  tbase = create_temporary_var (ptype);
2549  tbase_init = cp_build_modify_expr (tbase, NOP_EXPR,
2550				     fold_build2_loc (input_location,
2551						  POINTER_PLUS_EXPR, ptype,
2552						  fold_convert (ptype, base),
2553						  virtual_size),
2554				     tf_warning_or_error);
2555  controller = build3 (BIND_EXPR, void_type_node, tbase,
2556		       NULL_TREE, NULL_TREE);
2557  TREE_SIDE_EFFECTS (controller) = 1;
2558
2559  body = build1 (EXIT_EXPR, void_type_node,
2560		 build2 (EQ_EXPR, boolean_type_node, tbase,
2561			 fold_convert (ptype, base)));
2562  tmp = fold_build1_loc (input_location, NEGATE_EXPR, sizetype, size_exp);
2563  body = build_compound_expr
2564    (input_location,
2565     body, cp_build_modify_expr (tbase, NOP_EXPR,
2566				 build2 (POINTER_PLUS_EXPR, ptype, tbase, tmp),
2567				 tf_warning_or_error));
2568  body = build_compound_expr
2569    (input_location,
2570     body, build_delete (ptype, tbase, sfk_complete_destructor,
2571			 LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 1));
2572
2573  loop = build1 (LOOP_EXPR, void_type_node, body);
2574  loop = build_compound_expr (input_location, tbase_init, loop);
2575
2576 no_destructor:
2577  /* If the delete flag is one, or anything else with the low bit set,
2578     delete the storage.  */
2579  if (auto_delete_vec != sfk_base_destructor)
2580    {
2581      tree base_tbd;
2582
2583      /* The below is short by the cookie size.  */
2584      virtual_size = size_binop (MULT_EXPR, size_exp,
2585				 convert (sizetype, maxindex));
2586
2587      if (! TYPE_VEC_NEW_USES_COOKIE (type))
2588	/* no header */
2589	base_tbd = base;
2590      else
2591	{
2592	  tree cookie_size;
2593
2594	  cookie_size = targetm.cxx.get_cookie_size (type);
2595	  base_tbd
2596	    = cp_convert (ptype,
2597			  cp_build_binary_op (input_location,
2598					      MINUS_EXPR,
2599					      cp_convert (string_type_node,
2600							  base),
2601					      cookie_size,
2602					      tf_warning_or_error));
2603	  /* True size with header.  */
2604	  virtual_size = size_binop (PLUS_EXPR, virtual_size, cookie_size);
2605	}
2606
2607      if (auto_delete_vec == sfk_deleting_destructor)
2608	deallocate_expr = build_op_delete_call (VEC_DELETE_EXPR,
2609						base_tbd, virtual_size,
2610						use_global_delete & 1,
2611						/*placement=*/NULL_TREE,
2612						/*alloc_fn=*/NULL_TREE);
2613    }
2614
2615  body = loop;
2616  if (!deallocate_expr)
2617    ;
2618  else if (!body)
2619    body = deallocate_expr;
2620  else
2621    body = build_compound_expr (input_location, body, deallocate_expr);
2622
2623  if (!body)
2624    body = integer_zero_node;
2625
2626  /* Outermost wrapper: If pointer is null, punt.  */
2627  body = fold_build3_loc (input_location, COND_EXPR, void_type_node,
2628		      fold_build2_loc (input_location,
2629				   NE_EXPR, boolean_type_node, base,
2630				   convert (TREE_TYPE (base),
2631					    integer_zero_node)),
2632		      body, integer_zero_node);
2633  body = build1 (NOP_EXPR, void_type_node, body);
2634
2635  if (controller)
2636    {
2637      TREE_OPERAND (controller, 1) = body;
2638      body = controller;
2639    }
2640
2641  if (TREE_CODE (base) == SAVE_EXPR)
2642    /* Pre-evaluate the SAVE_EXPR outside of the BIND_EXPR.  */
2643    body = build2 (COMPOUND_EXPR, void_type_node, base, body);
2644
2645  return convert_to_void (body, /*implicit=*/NULL, tf_warning_or_error);
2646}
2647
2648/* Create an unnamed variable of the indicated TYPE.  */
2649
2650tree
2651create_temporary_var (tree type)
2652{
2653  tree decl;
2654
2655  decl = build_decl (input_location,
2656		     VAR_DECL, NULL_TREE, type);
2657  TREE_USED (decl) = 1;
2658  DECL_ARTIFICIAL (decl) = 1;
2659  DECL_IGNORED_P (decl) = 1;
2660  DECL_CONTEXT (decl) = current_function_decl;
2661
2662  return decl;
2663}
2664
2665/* Create a new temporary variable of the indicated TYPE, initialized
2666   to INIT.
2667
2668   It is not entered into current_binding_level, because that breaks
2669   things when it comes time to do final cleanups (which take place
2670   "outside" the binding contour of the function).  */
2671
2672static tree
2673get_temp_regvar (tree type, tree init)
2674{
2675  tree decl;
2676
2677  decl = create_temporary_var (type);
2678  add_decl_expr (decl);
2679
2680  finish_expr_stmt (cp_build_modify_expr (decl, INIT_EXPR, init,
2681					  tf_warning_or_error));
2682
2683  return decl;
2684}
2685
2686/* `build_vec_init' returns tree structure that performs
2687   initialization of a vector of aggregate types.
2688
2689   BASE is a reference to the vector, of ARRAY_TYPE, or a pointer
2690     to the first element, of POINTER_TYPE.
2691   MAXINDEX is the maximum index of the array (one less than the
2692     number of elements).  It is only used if BASE is a pointer or
2693     TYPE_DOMAIN (TREE_TYPE (BASE)) == NULL_TREE.
2694
2695   INIT is the (possibly NULL) initializer.
2696
2697   If EXPLICIT_VALUE_INIT_P is true, then INIT must be NULL.  All
2698   elements in the array are value-initialized.
2699
2700   FROM_ARRAY is 0 if we should init everything with INIT
2701   (i.e., every element initialized from INIT).
2702   FROM_ARRAY is 1 if we should index into INIT in parallel
2703   with initialization of DECL.
2704   FROM_ARRAY is 2 if we should index into INIT in parallel,
2705   but use assignment instead of initialization.  */
2706
2707tree
2708build_vec_init (tree base, tree maxindex, tree init,
2709		bool explicit_value_init_p,
2710		int from_array, tsubst_flags_t complain)
2711{
2712  tree rval;
2713  tree base2 = NULL_TREE;
2714  tree itype = NULL_TREE;
2715  tree iterator;
2716  /* The type of BASE.  */
2717  tree atype = TREE_TYPE (base);
2718  /* The type of an element in the array.  */
2719  tree type = TREE_TYPE (atype);
2720  /* The element type reached after removing all outer array
2721     types.  */
2722  tree inner_elt_type;
2723  /* The type of a pointer to an element in the array.  */
2724  tree ptype;
2725  tree stmt_expr;
2726  tree compound_stmt;
2727  int destroy_temps;
2728  tree try_block = NULL_TREE;
2729  int num_initialized_elts = 0;
2730  bool is_global;
2731
2732  if (TREE_CODE (atype) == ARRAY_TYPE && TYPE_DOMAIN (atype))
2733    maxindex = array_type_nelts (atype);
2734
2735  if (maxindex == NULL_TREE || maxindex == error_mark_node)
2736    return error_mark_node;
2737
2738  if (explicit_value_init_p)
2739    gcc_assert (!init);
2740
2741  inner_elt_type = strip_array_types (type);
2742
2743  /* Look through the TARGET_EXPR around a compound literal.  */
2744  if (init && TREE_CODE (init) == TARGET_EXPR
2745      && TREE_CODE (TARGET_EXPR_INITIAL (init)) == CONSTRUCTOR
2746      && from_array != 2)
2747    init = TARGET_EXPR_INITIAL (init);
2748
2749  if (init
2750      && TREE_CODE (atype) == ARRAY_TYPE
2751      && (from_array == 2
2752	  ? (!CLASS_TYPE_P (inner_elt_type)
2753	     || !TYPE_HAS_COMPLEX_ASSIGN_REF (inner_elt_type))
2754	  : !TYPE_NEEDS_CONSTRUCTING (type))
2755      && ((TREE_CODE (init) == CONSTRUCTOR
2756	   /* Don't do this if the CONSTRUCTOR might contain something
2757	      that might throw and require us to clean up.  */
2758	   && (VEC_empty (constructor_elt, CONSTRUCTOR_ELTS (init))
2759	       || ! TYPE_HAS_NONTRIVIAL_DESTRUCTOR (inner_elt_type)))
2760	  || from_array))
2761    {
2762      /* Do non-default initialization of trivial arrays resulting from
2763	 brace-enclosed initializers.  In this case, digest_init and
2764	 store_constructor will handle the semantics for us.  */
2765
2766      stmt_expr = build2 (INIT_EXPR, atype, base, init);
2767      return stmt_expr;
2768    }
2769
2770  maxindex = cp_convert (ptrdiff_type_node, maxindex);
2771  if (TREE_CODE (atype) == ARRAY_TYPE)
2772    {
2773      ptype = build_pointer_type (type);
2774      base = cp_convert (ptype, decay_conversion (base));
2775    }
2776  else
2777    ptype = atype;
2778
2779  /* The code we are generating looks like:
2780     ({
2781       T* t1 = (T*) base;
2782       T* rval = t1;
2783       ptrdiff_t iterator = maxindex;
2784       try {
2785	 for (; iterator != -1; --iterator) {
2786	   ... initialize *t1 ...
2787	   ++t1;
2788	 }
2789       } catch (...) {
2790	 ... destroy elements that were constructed ...
2791       }
2792       rval;
2793     })
2794
2795     We can omit the try and catch blocks if we know that the
2796     initialization will never throw an exception, or if the array
2797     elements do not have destructors.  We can omit the loop completely if
2798     the elements of the array do not have constructors.
2799
2800     We actually wrap the entire body of the above in a STMT_EXPR, for
2801     tidiness.
2802
2803     When copying from array to another, when the array elements have
2804     only trivial copy constructors, we should use __builtin_memcpy
2805     rather than generating a loop.  That way, we could take advantage
2806     of whatever cleverness the back end has for dealing with copies
2807     of blocks of memory.  */
2808
2809  is_global = begin_init_stmts (&stmt_expr, &compound_stmt);
2810  destroy_temps = stmts_are_full_exprs_p ();
2811  current_stmt_tree ()->stmts_are_full_exprs_p = 0;
2812  rval = get_temp_regvar (ptype, base);
2813  base = get_temp_regvar (ptype, rval);
2814  iterator = get_temp_regvar (ptrdiff_type_node, maxindex);
2815
2816  /* If initializing one array from another, initialize element by
2817     element.  We rely upon the below calls to do the argument
2818     checking.  Evaluate the initializer before entering the try block.  */
2819  if (from_array && init && TREE_CODE (init) != CONSTRUCTOR)
2820    {
2821      base2 = decay_conversion (init);
2822      itype = TREE_TYPE (base2);
2823      base2 = get_temp_regvar (itype, base2);
2824      itype = TREE_TYPE (itype);
2825    }
2826
2827  /* Protect the entire array initialization so that we can destroy
2828     the partially constructed array if an exception is thrown.
2829     But don't do this if we're assigning.  */
2830  if (flag_exceptions && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
2831      && from_array != 2)
2832    {
2833      try_block = begin_try_block ();
2834    }
2835
2836  if (init != NULL_TREE && TREE_CODE (init) == CONSTRUCTOR)
2837    {
2838      /* Do non-default initialization of non-trivial arrays resulting from
2839	 brace-enclosed initializers.  */
2840      unsigned HOST_WIDE_INT idx;
2841      tree elt;
2842      from_array = 0;
2843
2844      FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (init), idx, elt)
2845	{
2846	  tree baseref = build1 (INDIRECT_REF, type, base);
2847
2848	  num_initialized_elts++;
2849
2850	  current_stmt_tree ()->stmts_are_full_exprs_p = 1;
2851	  if (MAYBE_CLASS_TYPE_P (type) || TREE_CODE (type) == ARRAY_TYPE)
2852	    finish_expr_stmt (build_aggr_init (baseref, elt, 0, complain));
2853	  else
2854	    finish_expr_stmt (cp_build_modify_expr (baseref, NOP_EXPR,
2855                                                    elt, complain));
2856	  current_stmt_tree ()->stmts_are_full_exprs_p = 0;
2857
2858	  finish_expr_stmt (cp_build_unary_op (PREINCREMENT_EXPR, base, 0,
2859                                               complain));
2860	  finish_expr_stmt (cp_build_unary_op (PREDECREMENT_EXPR, iterator, 0,
2861                                               complain));
2862	}
2863
2864      /* Clear out INIT so that we don't get confused below.  */
2865      init = NULL_TREE;
2866    }
2867  else if (from_array)
2868    {
2869      if (init)
2870	/* OK, we set base2 above.  */;
2871      else if (TYPE_LANG_SPECIFIC (type)
2872	       && TYPE_NEEDS_CONSTRUCTING (type)
2873	       && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
2874	{
2875          if (complain & tf_error)
2876            error ("initializer ends prematurely");
2877	  return error_mark_node;
2878	}
2879    }
2880
2881  /* Now, default-initialize any remaining elements.  We don't need to
2882     do that if a) the type does not need constructing, or b) we've
2883     already initialized all the elements.
2884
2885     We do need to keep going if we're copying an array.  */
2886
2887  if (from_array
2888      || ((TYPE_NEEDS_CONSTRUCTING (type) || explicit_value_init_p)
2889	  && ! (host_integerp (maxindex, 0)
2890		&& (num_initialized_elts
2891		    == tree_low_cst (maxindex, 0) + 1))))
2892    {
2893      /* If the ITERATOR is equal to -1, then we don't have to loop;
2894	 we've already initialized all the elements.  */
2895      tree for_stmt;
2896      tree elt_init;
2897      tree to;
2898
2899      for_stmt = begin_for_stmt ();
2900      finish_for_init_stmt (for_stmt);
2901      finish_for_cond (build2 (NE_EXPR, boolean_type_node, iterator,
2902			       build_int_cst (TREE_TYPE (iterator), -1)),
2903		       for_stmt);
2904      finish_for_expr (cp_build_unary_op (PREDECREMENT_EXPR, iterator, 0,
2905                                          complain),
2906		       for_stmt);
2907
2908      to = build1 (INDIRECT_REF, type, base);
2909
2910      if (from_array)
2911	{
2912	  tree from;
2913
2914	  if (base2)
2915	    from = build1 (INDIRECT_REF, itype, base2);
2916	  else
2917	    from = NULL_TREE;
2918
2919	  if (from_array == 2)
2920	    elt_init = cp_build_modify_expr (to, NOP_EXPR, from,
2921					     complain);
2922	  else if (TYPE_NEEDS_CONSTRUCTING (type))
2923	    elt_init = build_aggr_init (to, from, 0, complain);
2924	  else if (from)
2925	    elt_init = cp_build_modify_expr (to, NOP_EXPR, from,
2926					     complain);
2927	  else
2928	    gcc_unreachable ();
2929	}
2930      else if (TREE_CODE (type) == ARRAY_TYPE)
2931	{
2932	  if (init != 0)
2933	    sorry
2934	      ("cannot initialize multi-dimensional array with initializer");
2935	  elt_init = build_vec_init (build1 (INDIRECT_REF, type, base),
2936				     0, 0,
2937				     explicit_value_init_p,
2938				     0, complain);
2939	}
2940      else if (explicit_value_init_p)
2941	elt_init = build2 (INIT_EXPR, type, to,
2942			   build_value_init (type));
2943      else
2944	{
2945	  gcc_assert (TYPE_NEEDS_CONSTRUCTING (type));
2946	  elt_init = build_aggr_init (to, init, 0, complain);
2947	}
2948
2949      current_stmt_tree ()->stmts_are_full_exprs_p = 1;
2950      finish_expr_stmt (elt_init);
2951      current_stmt_tree ()->stmts_are_full_exprs_p = 0;
2952
2953      finish_expr_stmt (cp_build_unary_op (PREINCREMENT_EXPR, base, 0,
2954                                           complain));
2955      if (base2)
2956	finish_expr_stmt (cp_build_unary_op (PREINCREMENT_EXPR, base2, 0,
2957                                             complain));
2958
2959      finish_for_stmt (for_stmt);
2960    }
2961
2962  /* Make sure to cleanup any partially constructed elements.  */
2963  if (flag_exceptions && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
2964      && from_array != 2)
2965    {
2966      tree e;
2967      tree m = cp_build_binary_op (input_location,
2968				   MINUS_EXPR, maxindex, iterator,
2969				   complain);
2970
2971      /* Flatten multi-dimensional array since build_vec_delete only
2972	 expects one-dimensional array.  */
2973      if (TREE_CODE (type) == ARRAY_TYPE)
2974	m = cp_build_binary_op (input_location,
2975				MULT_EXPR, m,
2976				array_type_nelts_total (type),
2977				complain);
2978
2979      finish_cleanup_try_block (try_block);
2980      e = build_vec_delete_1 (rval, m,
2981			      inner_elt_type, sfk_base_destructor,
2982			      /*use_global_delete=*/0);
2983      finish_cleanup (e, try_block);
2984    }
2985
2986  /* The value of the array initialization is the array itself, RVAL
2987     is a pointer to the first element.  */
2988  finish_stmt_expr_expr (rval, stmt_expr);
2989
2990  stmt_expr = finish_init_stmts (is_global, stmt_expr, compound_stmt);
2991
2992  /* Now make the result have the correct type.  */
2993  if (TREE_CODE (atype) == ARRAY_TYPE)
2994    {
2995      atype = build_pointer_type (atype);
2996      stmt_expr = build1 (NOP_EXPR, atype, stmt_expr);
2997      stmt_expr = cp_build_indirect_ref (stmt_expr, RO_NULL, complain);
2998      TREE_NO_WARNING (stmt_expr) = 1;
2999    }
3000
3001  current_stmt_tree ()->stmts_are_full_exprs_p = destroy_temps;
3002  return stmt_expr;
3003}
3004
3005/* Call the DTOR_KIND destructor for EXP.  FLAGS are as for
3006   build_delete.  */
3007
3008static tree
3009build_dtor_call (tree exp, special_function_kind dtor_kind, int flags)
3010{
3011  tree name;
3012  tree fn;
3013  switch (dtor_kind)
3014    {
3015    case sfk_complete_destructor:
3016      name = complete_dtor_identifier;
3017      break;
3018
3019    case sfk_base_destructor:
3020      name = base_dtor_identifier;
3021      break;
3022
3023    case sfk_deleting_destructor:
3024      name = deleting_dtor_identifier;
3025      break;
3026
3027    default:
3028      gcc_unreachable ();
3029    }
3030  fn = lookup_fnfields (TREE_TYPE (exp), name, /*protect=*/2);
3031  return build_new_method_call (exp, fn,
3032				/*args=*/NULL,
3033				/*conversion_path=*/NULL_TREE,
3034				flags,
3035				/*fn_p=*/NULL,
3036				tf_warning_or_error);
3037}
3038
3039/* Generate a call to a destructor. TYPE is the type to cast ADDR to.
3040   ADDR is an expression which yields the store to be destroyed.
3041   AUTO_DELETE is the name of the destructor to call, i.e., either
3042   sfk_complete_destructor, sfk_base_destructor, or
3043   sfk_deleting_destructor.
3044
3045   FLAGS is the logical disjunction of zero or more LOOKUP_
3046   flags.  See cp-tree.h for more info.  */
3047
3048tree
3049build_delete (tree type, tree addr, special_function_kind auto_delete,
3050    int flags, int use_global_delete)
3051{
3052  tree expr;
3053
3054  if (addr == error_mark_node)
3055    return error_mark_node;
3056
3057  /* Can happen when CURRENT_EXCEPTION_OBJECT gets its type
3058     set to `error_mark_node' before it gets properly cleaned up.  */
3059  if (type == error_mark_node)
3060    return error_mark_node;
3061
3062  type = TYPE_MAIN_VARIANT (type);
3063
3064  if (TREE_CODE (type) == POINTER_TYPE)
3065    {
3066      bool complete_p = true;
3067
3068      type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
3069      if (TREE_CODE (type) == ARRAY_TYPE)
3070	goto handle_array;
3071
3072      /* We don't want to warn about delete of void*, only other
3073	  incomplete types.  Deleting other incomplete types
3074	  invokes undefined behavior, but it is not ill-formed, so
3075	  compile to something that would even do The Right Thing
3076	  (TM) should the type have a trivial dtor and no delete
3077	  operator.  */
3078      if (!VOID_TYPE_P (type))
3079	{
3080	  complete_type (type);
3081	  if (!COMPLETE_TYPE_P (type))
3082	    {
3083	      if (warning (0, "possible problem detected in invocation of "
3084			   "delete operator:"))
3085		{
3086		  cxx_incomplete_type_diagnostic (addr, type, DK_WARNING);
3087		  inform (input_location, "neither the destructor nor the class-specific "
3088			  "operator delete will be called, even if they are "
3089			  "declared when the class is defined.");
3090		}
3091	      complete_p = false;
3092	    }
3093	}
3094      if (VOID_TYPE_P (type) || !complete_p || !MAYBE_CLASS_TYPE_P (type))
3095	/* Call the builtin operator delete.  */
3096	return build_builtin_delete_call (addr);
3097      if (TREE_SIDE_EFFECTS (addr))
3098	addr = save_expr (addr);
3099
3100      /* Throw away const and volatile on target type of addr.  */
3101      addr = convert_force (build_pointer_type (type), addr, 0);
3102    }
3103  else if (TREE_CODE (type) == ARRAY_TYPE)
3104    {
3105    handle_array:
3106
3107      if (TYPE_DOMAIN (type) == NULL_TREE)
3108	{
3109	  error ("unknown array size in delete");
3110	  return error_mark_node;
3111	}
3112      return build_vec_delete (addr, array_type_nelts (type),
3113			       auto_delete, use_global_delete);
3114    }
3115  else
3116    {
3117      /* Don't check PROTECT here; leave that decision to the
3118	 destructor.  If the destructor is accessible, call it,
3119	 else report error.  */
3120      addr = cp_build_unary_op (ADDR_EXPR, addr, 0, tf_warning_or_error);
3121      if (TREE_SIDE_EFFECTS (addr))
3122	addr = save_expr (addr);
3123
3124      addr = convert_force (build_pointer_type (type), addr, 0);
3125    }
3126
3127  gcc_assert (MAYBE_CLASS_TYPE_P (type));
3128
3129  if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
3130    {
3131      if (auto_delete != sfk_deleting_destructor)
3132	return void_zero_node;
3133
3134      return build_op_delete_call (DELETE_EXPR, addr,
3135				   cxx_sizeof_nowarn (type),
3136				   use_global_delete,
3137				   /*placement=*/NULL_TREE,
3138				   /*alloc_fn=*/NULL_TREE);
3139    }
3140  else
3141    {
3142      tree head = NULL_TREE;
3143      tree do_delete = NULL_TREE;
3144      tree ifexp;
3145
3146      if (CLASSTYPE_LAZY_DESTRUCTOR (type))
3147	lazily_declare_fn (sfk_destructor, type);
3148
3149      /* For `::delete x', we must not use the deleting destructor
3150	 since then we would not be sure to get the global `operator
3151	 delete'.  */
3152      if (use_global_delete && auto_delete == sfk_deleting_destructor)
3153	{
3154	  /* We will use ADDR multiple times so we must save it.  */
3155	  addr = save_expr (addr);
3156	  head = get_target_expr (build_headof (addr));
3157	  /* Delete the object.  */
3158	  do_delete = build_builtin_delete_call (head);
3159	  /* Otherwise, treat this like a complete object destructor
3160	     call.  */
3161	  auto_delete = sfk_complete_destructor;
3162	}
3163      /* If the destructor is non-virtual, there is no deleting
3164	 variant.  Instead, we must explicitly call the appropriate
3165	 `operator delete' here.  */
3166      else if (!DECL_VIRTUAL_P (CLASSTYPE_DESTRUCTORS (type))
3167	       && auto_delete == sfk_deleting_destructor)
3168	{
3169	  /* We will use ADDR multiple times so we must save it.  */
3170	  addr = save_expr (addr);
3171	  /* Build the call.  */
3172	  do_delete = build_op_delete_call (DELETE_EXPR,
3173					    addr,
3174					    cxx_sizeof_nowarn (type),
3175					    /*global_p=*/false,
3176					    /*placement=*/NULL_TREE,
3177					    /*alloc_fn=*/NULL_TREE);
3178	  /* Call the complete object destructor.  */
3179	  auto_delete = sfk_complete_destructor;
3180	}
3181      else if (auto_delete == sfk_deleting_destructor
3182	       && TYPE_GETS_REG_DELETE (type))
3183	{
3184	  /* Make sure we have access to the member op delete, even though
3185	     we'll actually be calling it from the destructor.  */
3186	  build_op_delete_call (DELETE_EXPR, addr, cxx_sizeof_nowarn (type),
3187				/*global_p=*/false,
3188				/*placement=*/NULL_TREE,
3189				/*alloc_fn=*/NULL_TREE);
3190	}
3191
3192      expr = build_dtor_call (cp_build_indirect_ref (addr, RO_NULL,
3193                                                     tf_warning_or_error),
3194			      auto_delete, flags);
3195      if (do_delete)
3196	expr = build2 (COMPOUND_EXPR, void_type_node, expr, do_delete);
3197
3198      /* We need to calculate this before the dtor changes the vptr.  */
3199      if (head)
3200	expr = build2 (COMPOUND_EXPR, void_type_node, head, expr);
3201
3202      if (flags & LOOKUP_DESTRUCTOR)
3203	/* Explicit destructor call; don't check for null pointer.  */
3204	ifexp = integer_one_node;
3205      else
3206	/* Handle deleting a null pointer.  */
3207	ifexp = fold (cp_build_binary_op (input_location,
3208					  NE_EXPR, addr, integer_zero_node,
3209					  tf_warning_or_error));
3210
3211      if (ifexp != integer_one_node)
3212	expr = build3 (COND_EXPR, void_type_node,
3213		       ifexp, expr, void_zero_node);
3214
3215      return expr;
3216    }
3217}
3218
3219/* At the beginning of a destructor, push cleanups that will call the
3220   destructors for our base classes and members.
3221
3222   Called from begin_destructor_body.  */
3223
3224void
3225push_base_cleanups (void)
3226{
3227  tree binfo, base_binfo;
3228  int i;
3229  tree member;
3230  tree expr;
3231  VEC(tree,gc) *vbases;
3232
3233  /* Run destructors for all virtual baseclasses.  */
3234  if (CLASSTYPE_VBASECLASSES (current_class_type))
3235    {
3236      tree cond = (condition_conversion
3237		   (build2 (BIT_AND_EXPR, integer_type_node,
3238			    current_in_charge_parm,
3239			    integer_two_node)));
3240
3241      /* The CLASSTYPE_VBASECLASSES vector is in initialization
3242	 order, which is also the right order for pushing cleanups.  */
3243      for (vbases = CLASSTYPE_VBASECLASSES (current_class_type), i = 0;
3244	   VEC_iterate (tree, vbases, i, base_binfo); i++)
3245	{
3246	  if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (BINFO_TYPE (base_binfo)))
3247	    {
3248	      expr = build_special_member_call (current_class_ref,
3249						base_dtor_identifier,
3250						NULL,
3251						base_binfo,
3252						(LOOKUP_NORMAL
3253						 | LOOKUP_NONVIRTUAL),
3254                                                tf_warning_or_error);
3255	      expr = build3 (COND_EXPR, void_type_node, cond,
3256			     expr, void_zero_node);
3257	      finish_decl_cleanup (NULL_TREE, expr);
3258	    }
3259	}
3260    }
3261
3262  /* Take care of the remaining baseclasses.  */
3263  for (binfo = TYPE_BINFO (current_class_type), i = 0;
3264       BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
3265    {
3266      if (TYPE_HAS_TRIVIAL_DESTRUCTOR (BINFO_TYPE (base_binfo))
3267	  || BINFO_VIRTUAL_P (base_binfo))
3268	continue;
3269
3270      expr = build_special_member_call (current_class_ref,
3271					base_dtor_identifier,
3272					NULL, base_binfo,
3273					LOOKUP_NORMAL | LOOKUP_NONVIRTUAL,
3274                                        tf_warning_or_error);
3275      finish_decl_cleanup (NULL_TREE, expr);
3276    }
3277
3278  for (member = TYPE_FIELDS (current_class_type); member;
3279       member = TREE_CHAIN (member))
3280    {
3281      if (TREE_TYPE (member) == error_mark_node
3282	  || TREE_CODE (member) != FIELD_DECL
3283	  || DECL_ARTIFICIAL (member))
3284	continue;
3285      if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (member)))
3286	{
3287	  tree this_member = (build_class_member_access_expr
3288			      (current_class_ref, member,
3289			       /*access_path=*/NULL_TREE,
3290			       /*preserve_reference=*/false,
3291			       tf_warning_or_error));
3292	  tree this_type = TREE_TYPE (member);
3293	  expr = build_delete (this_type, this_member,
3294			       sfk_complete_destructor,
3295			       LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR|LOOKUP_NORMAL,
3296			       0);
3297	  finish_decl_cleanup (NULL_TREE, expr);
3298	}
3299    }
3300}
3301
3302/* Build a C++ vector delete expression.
3303   MAXINDEX is the number of elements to be deleted.
3304   ELT_SIZE is the nominal size of each element in the vector.
3305   BASE is the expression that should yield the store to be deleted.
3306   This function expands (or synthesizes) these calls itself.
3307   AUTO_DELETE_VEC says whether the container (vector) should be deallocated.
3308
3309   This also calls delete for virtual baseclasses of elements of the vector.
3310
3311   Update: MAXINDEX is no longer needed.  The size can be extracted from the
3312   start of the vector for pointers, and from the type for arrays.  We still
3313   use MAXINDEX for arrays because it happens to already have one of the
3314   values we'd have to extract.  (We could use MAXINDEX with pointers to
3315   confirm the size, and trap if the numbers differ; not clear that it'd
3316   be worth bothering.)  */
3317
3318tree
3319build_vec_delete (tree base, tree maxindex,
3320    special_function_kind auto_delete_vec, int use_global_delete)
3321{
3322  tree type;
3323  tree rval;
3324  tree base_init = NULL_TREE;
3325
3326  type = TREE_TYPE (base);
3327
3328  if (TREE_CODE (type) == POINTER_TYPE)
3329    {
3330      /* Step back one from start of vector, and read dimension.  */
3331      tree cookie_addr;
3332      tree size_ptr_type = build_pointer_type (sizetype);
3333
3334      if (TREE_SIDE_EFFECTS (base))
3335	{
3336	  base_init = get_target_expr (base);
3337	  base = TARGET_EXPR_SLOT (base_init);
3338	}
3339      type = strip_array_types (TREE_TYPE (type));
3340      cookie_addr = fold_build1_loc (input_location, NEGATE_EXPR,
3341				 sizetype, TYPE_SIZE_UNIT (sizetype));
3342      cookie_addr = build2 (POINTER_PLUS_EXPR,
3343			    size_ptr_type,
3344			    fold_convert (size_ptr_type, base),
3345			    cookie_addr);
3346      maxindex = cp_build_indirect_ref (cookie_addr, RO_NULL, tf_warning_or_error);
3347    }
3348  else if (TREE_CODE (type) == ARRAY_TYPE)
3349    {
3350      /* Get the total number of things in the array, maxindex is a
3351	 bad name.  */
3352      maxindex = array_type_nelts_total (type);
3353      type = strip_array_types (type);
3354      base = cp_build_unary_op (ADDR_EXPR, base, 1, tf_warning_or_error);
3355      if (TREE_SIDE_EFFECTS (base))
3356	{
3357	  base_init = get_target_expr (base);
3358	  base = TARGET_EXPR_SLOT (base_init);
3359	}
3360    }
3361  else
3362    {
3363      if (base != error_mark_node)
3364	error ("type to vector delete is neither pointer or array type");
3365      return error_mark_node;
3366    }
3367
3368  rval = build_vec_delete_1 (base, maxindex, type, auto_delete_vec,
3369			     use_global_delete);
3370  if (base_init)
3371    rval = build2 (COMPOUND_EXPR, TREE_TYPE (rval), base_init, rval);
3372
3373  return rval;
3374}
3375