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