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