OSDN Git Service

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