OSDN Git Service

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