OSDN Git Service

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