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