OSDN Git Service

31th Cygnus<->FSF merge.
[pf3gnuchains/gcc-fork.git] / gcc / cp / init.c
1 /* Handle initialization things in C++.
2    Copyright (C) 1987, 1989, 1992, 1993, 1994 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, 675 Mass Ave, Cambridge, MA 02139, USA.  */
20
21
22 /* High-level class interface. */
23
24 #include "config.h"
25 #include "tree.h"
26 #include "rtl.h"
27 #include "cp-tree.h"
28 #include "flags.h"
29
30 #undef NULL
31 #define NULL 0
32
33 /* In C++, structures with well-defined constructors are initialized by
34    those constructors, unasked.  CURRENT_BASE_INIT_LIST
35    holds a list of stmts for a BASE_INIT term in the grammar.
36    This list has one element for each base class which must be
37    initialized.  The list elements are [basename, init], with
38    type basetype.  This allows the possibly anachronistic form
39    (assuming d : a, b, c) "d (int a) : c(a+5), b (a-4), a (a+3)"
40    where each successive term can be handed down the constructor
41    line.  Perhaps this was not intended.  */
42 tree current_base_init_list, current_member_init_list;
43
44 void emit_base_init ();
45 void check_base_init ();
46 static void expand_aggr_vbase_init ();
47 void expand_member_init ();
48 void expand_aggr_init ();
49
50 static void expand_aggr_init_1 ();
51 static void expand_recursive_init_1 ();
52 static void expand_recursive_init ();
53 static void expand_virtual_init PROTO((tree, tree, tree));
54 tree expand_vec_init ();
55 tree build_vec_delete ();
56
57 static void add_friend (), add_friends ();
58
59 /* Cache _builtin_new and _builtin_delete exprs.  */
60 static tree BIN, BID;
61
62 /* Cache the identifier nodes for the two magic field of a new cookie.  */
63 static tree nc_nelts_field_id;
64 #if 0
65 static tree nc_ptr_2comp_field_id;
66 #endif
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 tree BI_header_type, BI_header_size;
74
75 void init_init_processing ()
76 {
77   tree fields[1];
78
79   /* Define implicit `operator new' and `operator delete' functions.  */
80   BIN = default_conversion (get_first_fn (IDENTIFIER_GLOBAL_VALUE (ansi_opname[(int) NEW_EXPR])));
81   TREE_USED (TREE_OPERAND (BIN, 0)) = 0;
82   BID = default_conversion (get_first_fn (IDENTIFIER_GLOBAL_VALUE (ansi_opname[(int) DELETE_EXPR])));
83   TREE_USED (TREE_OPERAND (BID, 0)) = 0;
84   minus_one = build_int_2 (-1, -1);
85
86   /* Define the structure that holds header information for
87      arrays allocated via operator new.  */
88   BI_header_type = make_lang_type (RECORD_TYPE);
89   nc_nelts_field_id = get_identifier ("nelts");
90   fields[0] = build_lang_field_decl (FIELD_DECL, nc_nelts_field_id, sizetype);
91   finish_builtin_type (BI_header_type, "__new_cookie", fields,
92                        0, double_type_node);
93   BI_header_size = size_in_bytes (BI_header_type);
94 }
95
96 /* Subroutine of emit_base_init.  For BINFO, initialize all the
97    virtual function table pointers, except those that come from
98    virtual base classes.  Initialize binfo's vtable pointer, if
99    INIT_SELF is true.  CAN_ELIDE is true when we know that all virtual
100    function table pointers in all bases have been initialized already,
101    probably because their constructors have just be run.  */
102 void
103 init_vtbl_ptrs (binfo, init_self, can_elide)
104      tree binfo;
105      int init_self, can_elide;
106 {
107   tree vfields;
108   tree binfos = BINFO_BASETYPES (binfo);
109   int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
110
111   for (i = 0; i < n_baselinks; i++)
112     {
113       tree base_binfo = TREE_VEC_ELT (binfos, i);
114       int is_not_base_vtable =
115         i != CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo));
116       if (! TREE_VIA_VIRTUAL (base_binfo))
117         init_vtbl_ptrs (base_binfo, is_not_base_vtable, can_elide);
118     }
119 #if 0
120   /* Before turning this on, make sure it is correct.  */
121   if (can_elide  && ! BINFO_MODIFIED (binfo))
122     return;
123 #endif
124   /* Should we use something besides CLASSTYPE_VFIELDS? */
125   if (init_self && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
126     {
127       tree base_ptr = convert_pointer_to_real (binfo, current_class_decl);
128       expand_virtual_init (binfo, binfo, base_ptr);
129     }
130 }
131 \f
132 /* 348 - 351 */
133 /* Subroutine of emit_base_init.  */
134 static void
135 perform_member_init (member, name, init, explicit)
136      tree member, name, init;
137      int explicit;
138 {
139   tree decl;
140   tree type = TREE_TYPE (member);
141
142   if (TYPE_NEEDS_CONSTRUCTING (type)
143       || (init && TYPE_HAS_CONSTRUCTOR (type)))
144     {
145       /* Since `init' is already a TREE_LIST on the current_member_init_list,
146          only build it into one if we aren't already a list.  */
147       if (init != NULL_TREE && TREE_CODE (init) != TREE_LIST)
148         init = build_tree_list (NULL_TREE, init);
149
150       decl = build_component_ref (C_C_D, name, 0, explicit);
151
152       if (explicit
153           && TREE_CODE (type) == ARRAY_TYPE
154           && init != NULL_TREE
155           && TREE_CHAIN (init) == NULL_TREE
156           && TREE_CODE (TREE_TYPE (TREE_VALUE (init))) == ARRAY_TYPE)
157         {
158           /* Initialization of one array from another.  */
159           expand_vec_init (TREE_OPERAND (decl, 1), decl,
160                            array_type_nelts (type), TREE_VALUE (init), 1);
161         }
162       else
163         expand_aggr_init (decl, init, 0);
164     }
165   else
166     {
167       if (init == NULL_TREE)
168         {
169           if (explicit)
170             {
171               cp_error ("incomplete initializer for member `%D' of class `%T' which has no constructor",
172                         member, current_class_type);
173               init = error_mark_node;
174             }
175           /* member traversal: note it leaves init NULL */
176           else if (TREE_CODE (TREE_TYPE (member)) == REFERENCE_TYPE)
177             cp_pedwarn ("uninitialized reference member `%D'", member);
178         }
179       else if (TREE_CODE (init) == TREE_LIST)
180         {
181           /* There was an explicit member initialization.  Do some
182              work in that case.  */
183           if (TREE_CHAIN (init))
184             {
185               warning ("initializer list treated as compound expression");
186               init = build_compound_expr (init);
187             }
188           else
189             init = TREE_VALUE (init);
190         }
191
192       /* We only build this with a null init if we got it from the
193          current_member_init_list.  */
194       if (init || explicit)
195         {
196           decl = build_component_ref (C_C_D, name, 0, explicit);
197           expand_expr_stmt (build_modify_expr (decl, INIT_EXPR, init));
198         }
199     }
200
201   if (flag_handle_exceptions == 2 && TYPE_NEEDS_DESTRUCTOR (type))
202     {
203       cplus_expand_start_try (1);
204       push_exception_cleanup (build_unary_op (ADDR_EXPR, decl, 0));
205     }
206 }
207
208 /* Subroutine of emit_member_init.  */
209 static tree
210 sort_member_init (t)
211      tree t;
212 {
213   tree x, member, name, field, init;
214   tree init_list = NULL_TREE;
215   tree fields_to_unmark = NULL_TREE;
216   int found;
217
218   for (member = TYPE_FIELDS (t); member ; member = TREE_CHAIN (member))
219     {
220       found = 0;
221       for (x = current_member_init_list ; x ; x = TREE_CHAIN (x))
222         {
223           /* If we cleared this out, then pay no attention to it.  */
224           if (TREE_PURPOSE (x) == NULL_TREE)
225             continue;
226           name = TREE_PURPOSE (x);
227
228 #if 0
229           field = (TREE_CODE (name) == COMPONENT_REF
230                    ? TREE_OPERAND (name, 1) : IDENTIFIER_CLASS_VALUE (name));
231 #else
232           /* Let's find out when this happens.  */
233           my_friendly_assert (TREE_CODE (name) != COMPONENT_REF, 348);
234           field = IDENTIFIER_CLASS_VALUE (name);
235 #endif
236
237           /* If one member shadows another, get the outermost one.  */
238           if (TREE_CODE (field) == TREE_LIST)
239             field = TREE_VALUE (field);
240
241           if (field == member)
242             {
243               /* See if we already found an initializer for this field.  */
244               if (found)
245                 {
246                   if (DECL_NAME (field))
247                     cp_error ("multiple initializations given for member `%D'",
248                               field);
249                   continue;
250                 }
251
252               init_list = chainon (init_list,
253                                    build_tree_list (name, TREE_VALUE (x)));
254               /* Make sure we won't try to work on this init again.  */
255               TREE_PURPOSE (x) = NULL_TREE;
256               found = 1;
257               break;
258             }
259         }
260
261       /* If we didn't find MEMBER in the list, create a dummy entry
262          so the two lists (INIT_LIST and the list of members) will be
263          symmetrical.  */
264       if (! found)
265         init_list = chainon (init_list, build_tree_list (NULL_TREE, NULL_TREE));
266     }
267
268   for (x = current_member_init_list ; x ; x = TREE_CHAIN (x))
269     {
270       if (TREE_PURPOSE (x))
271         {
272           name = TREE_PURPOSE (x);
273           init = TREE_VALUE (x);
274           /* XXX: this may need the COMPONENT_REF operand 0 check if
275              it turns out we actually get them.  */
276           field = IDENTIFIER_CLASS_VALUE (name);
277
278           /* If one member shadows another, get the outermost one.  */
279           if (TREE_CODE (field) == TREE_LIST)
280             {
281               field = TREE_VALUE (field);
282               if (decl_type_context (field) != current_class_type)
283                 cp_error ("field `%D' not in immediate context", field);
284             }
285
286 #if 0
287           /* It turns out if you have an anonymous union in the
288              class, a member from it can end up not being on the
289              list of fields (rather, the type is), and therefore
290              won't be seen by the for loop above.  */
291
292           /* The code in this for loop is derived from a general loop
293              which had this check in it.  Theoretically, we've hit
294              every initialization for the list of members in T, so
295              we shouldn't have anything but these left in this list.  */
296           my_friendly_assert (DECL_FIELD_CONTEXT (field) != t, 351);
297 #endif
298
299           if (TREE_HAS_CONSTRUCTOR (field))
300             {
301               if (DECL_NAME (field))
302                 error ("multiple initializations given for member `%s'",
303                        IDENTIFIER_POINTER (DECL_NAME (field)));
304               continue;
305             }
306
307           TREE_HAS_CONSTRUCTOR (field) = 1;
308           fields_to_unmark = tree_cons (NULL_TREE, field, fields_to_unmark);
309
310           perform_member_init (field, name, init, 1);
311           TREE_PURPOSE (x) = NULL_TREE;
312         }
313     }
314
315   /* Unmark fields which are initialized for the base class.  */
316   while (fields_to_unmark)
317     {
318       TREE_HAS_CONSTRUCTOR (TREE_VALUE (fields_to_unmark)) = 0;
319       /* XXX is this a memory leak? */
320       fields_to_unmark = TREE_CHAIN (fields_to_unmark);
321     }
322
323   return init_list;
324 }
325
326 /* Perform whatever initializations have yet to be done on the base
327    class of the class variable.  These actions are in the global
328    variable CURRENT_BASE_INIT_LIST.  Such an action could be
329    NULL_TREE, meaning that the user has explicitly called the base
330    class constructor with no arguments.
331
332    If there is a need for a call to a constructor, we must surround
333    that call with a pushlevel/poplevel pair, since we are technically
334    at the PARM level of scope.
335
336    Argument IMMEDIATELY, if zero, forces a new sequence to be
337    generated to contain these new insns, so it can be emitted later.
338    This sequence is saved in the global variable BASE_INIT_INSNS.
339    Otherwise, the insns are emitted into the current sequence.
340
341    Note that emit_base_init does *not* initialize virtual base
342    classes.  That is done specially, elsewhere.  */
343    
344 void
345 emit_base_init (t, immediately)
346      tree t;
347      int immediately;
348 {
349   extern tree in_charge_identifier;
350
351   tree member, vbases;
352   tree init_list;
353   int pass, start;
354   tree t_binfo = TYPE_BINFO (t);
355   tree binfos = BINFO_BASETYPES (t_binfo);
356   int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
357   int have_init_list = 0, from_init_list;
358
359   if (! immediately)
360     {
361       do_pending_stack_adjust ();
362       start_sequence ();
363     }
364
365   if (write_symbols == NO_DEBUG)
366     /* As a matter of principle, `start_sequence' should do this.  */
367     emit_note (0, -1);
368   else
369     /* Always emit a line number note so we can step into constructors.  */
370     emit_line_note_force (DECL_SOURCE_FILE (current_function_decl),
371                           DECL_SOURCE_LINE (current_function_decl));
372
373   /* In this case, we always need IN_CHARGE_NODE, because we have
374      to know whether to deallocate or not before exiting.  */
375   if (flag_handle_exceptions == 2
376       && lookup_name (in_charge_identifier, 0) == NULL_TREE)
377     {
378       tree in_charge_node = pushdecl (build_decl (VAR_DECL, in_charge_identifier,
379                                                   integer_type_node));
380       store_init_value (in_charge_node, build (EQ_EXPR, integer_type_node,
381                                                current_class_decl,
382                                                integer_zero_node));
383       expand_decl (in_charge_node);
384       expand_decl_init (in_charge_node);
385     }
386
387   start = ! TYPE_USES_VIRTUAL_BASECLASSES (t);
388   for (pass = start; pass < 2; pass++)
389     {
390       tree vbase_init_list = NULL_TREE;
391
392       for (init_list = current_base_init_list; init_list;
393            init_list = TREE_CHAIN (init_list))
394         {
395           tree basename = TREE_PURPOSE (init_list);
396           tree binfo;
397           tree init = TREE_VALUE (init_list);
398
399           if (basename == NULL_TREE)
400             {
401               /* Initializer for single base class.  Must not
402                  use multiple inheritance or this is ambiguous.  */
403               switch (n_baseclasses)
404                 {
405                 case 0:
406                   error ("type `%s' does not have a base class to initialize",
407                          IDENTIFIER_POINTER (current_class_name));
408                   return;
409                 case 1:
410                   break;
411                 default:
412                   error ("unnamed initializer ambiguous for type `%s' which uses multiple inheritance", IDENTIFIER_POINTER (current_class_name));
413                   return;
414                 }
415               binfo = TREE_VEC_ELT (binfos, 0);
416             }
417           else if (is_aggr_typedef (basename, 1))
418             {
419               binfo = binfo_or_else (IDENTIFIER_TYPE_VALUE (basename), t);
420               if (binfo == NULL_TREE)
421                 continue;
422
423               /* Virtual base classes are special cases.  Their initializers
424                  are recorded with this constructor, and they are used when
425                  this constructor is the top-level constructor called.  */
426               if (! TREE_VIA_VIRTUAL (binfo))
427                 {
428                   /* Otherwise, if it is not an immediate base class, complain.  */
429                   for (i = n_baseclasses-1; i >= 0; i--)
430                     if (BINFO_TYPE (binfo) == BINFO_TYPE (TREE_VEC_ELT (binfos, i)))
431                       break;
432                   if (i < 0)
433                     {
434                       error ("type `%s' is not an immediate base class of type `%s'",
435                              IDENTIFIER_POINTER (basename),
436                              IDENTIFIER_POINTER (current_class_name));
437                       continue;
438                     }
439                 }
440             }
441           else
442             continue;
443
444           /* The base initialization list goes up to the first
445              base class which can actually use it.  */
446
447           if (pass == start)
448             {
449               char *msgp = (! TYPE_HAS_CONSTRUCTOR (BINFO_TYPE (binfo)))
450                 ? "cannot pass initialization up to class `%s'" : 0;
451
452               while (! TYPE_HAS_CONSTRUCTOR (BINFO_TYPE (binfo))
453                      && BINFO_BASETYPES (binfo) != NULL_TREE
454                      && TREE_VEC_LENGTH (BINFO_BASETYPES (binfo)) == 1)
455                 {
456                   /* ?? This should be fixed in RENO by forcing
457                      default constructors to exist.  */
458                   SET_BINFO_BASEINIT_MARKED (binfo);
459                   binfo = BINFO_BASETYPE (binfo, 0);
460                 }
461
462               /* We used to give an error if this wasn't true, saying that
463                  there's no constructor for the initialization of basename.
464                  This turned out to be incorrect---it should use the
465                  default constructor, since a user could try to initialize
466                  the class in a derived class's base initializer list.  */
467               if (TYPE_HAS_CONSTRUCTOR (BINFO_TYPE (binfo)))
468                 {
469                   if (msgp)
470                     {
471                       if (pedantic)
472                         error_with_aggr_type (binfo, msgp);
473                       else
474                         msgp = NULL;
475                     }
476                 }
477
478               if (BINFO_BASEINIT_MARKED (binfo))
479                 {
480                   msgp = "class `%s' initializer already specified";
481                   error (msgp, IDENTIFIER_POINTER (basename));
482                 }
483
484               if (msgp)
485                 continue;
486
487               SET_BINFO_BASEINIT_MARKED (binfo);
488               if (TREE_VIA_VIRTUAL (binfo))
489                 {
490                   vbase_init_list = tree_cons (init, BINFO_TYPE (binfo),
491                                                vbase_init_list);
492                   continue;
493                 }
494               if (pass == 0)
495                 continue;
496             }
497           else if (TREE_VIA_VIRTUAL (binfo))
498             continue;
499
500           member = convert_pointer_to (binfo, current_class_decl);
501           expand_aggr_init_1 (t_binfo, 0,
502                               build_indirect_ref (member, NULL_PTR), init,
503                               BINFO_OFFSET_ZEROP (binfo), LOOKUP_COMPLAIN);
504           if (flag_handle_exceptions == 2 && TYPE_NEEDS_DESTRUCTOR (BINFO_TYPE (binfo)))
505             {
506               cplus_expand_start_try (1);
507               push_exception_cleanup (member);
508             }
509         }
510
511       if (pass == 0)
512         {
513           tree first_arg = TREE_CHAIN (DECL_ARGUMENTS (current_function_decl));
514           tree vbases;
515
516           if (DECL_NAME (current_function_decl) == NULL_TREE
517               && TREE_CHAIN (first_arg) != NULL_TREE)
518             {
519               /* If there are virtual baseclasses without initialization
520                  specified, and this is a default X(X&) constructor,
521                  build the initialization list so that each virtual baseclass
522                  of the new object is initialized from the virtual baseclass
523                  of the incoming arg.  */
524               tree init_arg = build_unary_op (ADDR_EXPR, TREE_CHAIN (first_arg), 0);
525               for (vbases = CLASSTYPE_VBASECLASSES (t);
526                    vbases; vbases = TREE_CHAIN (vbases))
527                 {
528                   if (BINFO_BASEINIT_MARKED (vbases) == 0)
529                     {
530                       member = convert_pointer_to (vbases, init_arg);
531                       if (member == init_arg)
532                         member = TREE_CHAIN (first_arg);
533                       else
534                         TREE_TYPE (member) = build_reference_type (BINFO_TYPE (vbases));
535                       vbase_init_list = tree_cons (convert_from_reference (member),
536                                                    vbases, vbase_init_list);
537                       SET_BINFO_BASEINIT_MARKED (vbases);
538                     }
539                 }
540             }
541           expand_start_cond (first_arg, 0);
542           expand_aggr_vbase_init (t_binfo, C_C_D, current_class_decl,
543                                   vbase_init_list);
544           expand_end_cond ();
545         }
546     }
547   current_base_init_list = NULL_TREE;
548
549   /* Now, perform default initialization of all base classes which
550      have not yet been initialized, and unmark baseclasses which
551      have been initialized.  */
552   for (i = 0; i < n_baseclasses; i++)
553     {
554       tree base = current_class_decl;
555       tree base_binfo = TREE_VEC_ELT (binfos, i);
556
557       if (TYPE_NEEDS_CONSTRUCTING (BINFO_TYPE (base_binfo)))
558         {
559           if (! TREE_VIA_VIRTUAL (base_binfo)
560               && ! BINFO_BASEINIT_MARKED (base_binfo))
561             {
562               tree ref;
563
564               if (BINFO_OFFSET_ZEROP (base_binfo))
565                 base = build1 (NOP_EXPR,
566                                TYPE_POINTER_TO (BINFO_TYPE (base_binfo)),
567                                current_class_decl);
568               else
569                 base = build (PLUS_EXPR,
570                               TYPE_POINTER_TO (BINFO_TYPE (base_binfo)),
571                               current_class_decl, BINFO_OFFSET (base_binfo));
572
573               ref = build_indirect_ref (base, NULL_PTR);
574               expand_aggr_init_1 (t_binfo, 0, ref, NULL_TREE,
575                                   BINFO_OFFSET_ZEROP (base_binfo),
576                                   LOOKUP_COMPLAIN);
577               if (flag_handle_exceptions == 2
578                   && TYPE_NEEDS_DESTRUCTOR (BINFO_TYPE (base_binfo)))
579                 {
580                   cplus_expand_start_try (1);
581                   push_exception_cleanup (base);
582                 }
583             }
584         }
585       CLEAR_BINFO_BASEINIT_MARKED (base_binfo);
586
587       if (! TYPE_USES_VIRTUAL_BASECLASSES (t))
588         {
589           while (! TYPE_HAS_CONSTRUCTOR (BINFO_TYPE (base_binfo))
590                  && BINFO_BASETYPES (base_binfo) != NULL_TREE
591                  && TREE_VEC_LENGTH (BINFO_BASETYPES (base_binfo)) == 1)
592             {
593               /* ?? This should be fixed in RENO by forcing
594                  default constructors to exist.  It is needed for symmetry
595                  with code above.  */
596               base_binfo = BINFO_BASETYPE (base_binfo, 0);
597               CLEAR_BINFO_BASEINIT_MARKED (base_binfo);
598             }
599         }
600     }
601
602   /* Initialize all the virtual function table fields that
603      do come from virtual base classes. */
604   if (TYPE_USES_VIRTUAL_BASECLASSES (t))
605     expand_vbase_vtables_init (t_binfo, t_binfo,
606                                 C_C_D, current_class_decl, 0);
607   for (vbases = CLASSTYPE_VBASECLASSES (t); vbases; vbases = TREE_CHAIN (vbases))
608     CLEAR_BINFO_BASEINIT_MARKED (vbases);
609
610   /* Initialize all the virtual function table fields that
611      do not come from virtual base classes.  */
612   init_vtbl_ptrs (t_binfo, 0, 1);
613
614   if (CLASSTYPE_NEEDS_VIRTUAL_REINIT (t))
615     expand_virtual_init (TYPE_BINFO (t), t, current_class_decl);
616
617   if (current_member_init_list)
618     {
619       init_list = sort_member_init (t);
620       have_init_list = 1;
621     }
622
623   for (member = TYPE_FIELDS (t); member; member = TREE_CHAIN (member))
624     {
625       tree init, name;
626       from_init_list = 0;
627
628       /* See if we had a user-specified member initialization.  */
629       if (have_init_list)
630         {
631           if (TREE_PURPOSE (init_list))
632             {
633               name = TREE_PURPOSE (init_list);
634               init = TREE_VALUE (init_list);
635               from_init_list = 1;
636
637               if (TREE_STATIC (member))
638                 {
639                   error_with_aggr_type (DECL_FIELD_CONTEXT (member),
640                                         "field `%s::%s' is static; only point of initialization is its declaration",
641                                         IDENTIFIER_POINTER (TREE_PURPOSE (init_list)));
642                   continue;
643                 }
644
645               /* Also see if it's ever a COMPONENT_REF here.  If it is, we
646                  need to do `expand_assignment (name, init, 0, 0);' and
647                  a continue.  */
648               my_friendly_assert (TREE_CODE (name) != COMPONENT_REF, 349);
649             }
650
651           init_list = TREE_CHAIN (init_list);
652         }
653
654       if (! from_init_list)
655         {
656           /* member could be, for example, a CONST_DECL for an enumerated
657              tag; we don't want to try to initialize that, since it already
658              has a value.  */
659           if (TREE_CODE (member) != FIELD_DECL)
660             continue;
661
662           name = DECL_NAME (member);
663           init = DECL_INITIAL (member);
664         }
665
666       perform_member_init (member, name, init, from_init_list);
667     }
668
669   current_member_init_list = NULL_TREE;
670
671   /* It is possible for the initializers to need cleanups.
672      Expand those cleanups now that all the initialization
673      has been done.  */
674   expand_cleanups_to (NULL_TREE);
675
676   if (! immediately)
677     {
678       extern rtx base_init_insns;
679
680       do_pending_stack_adjust ();
681       my_friendly_assert (base_init_insns == 0, 207);
682       base_init_insns = get_insns ();
683       end_sequence ();
684     }
685
686   /* All the implicit try blocks we built up will be zapped
687      when we come to a real binding contour boundary.  */
688 }
689
690 /* Check that all fields are properly initialized after
691    an assignment to `this'.  */
692 void
693 check_base_init (t)
694      tree t;
695 {
696   tree member;
697   for (member = TYPE_FIELDS (t); member; member = TREE_CHAIN (member))
698     if (DECL_NAME (member) && TREE_USED (member))
699       cp_error ("field `%D' used before initialized (after assignment to `this')",
700                 member);
701 }
702
703 /* This code sets up the virtual function tables appropriate for
704    the pointer DECL.  It is a one-ply initialization.
705
706    BINFO is the exact type that DECL is supposed to be.  In
707    multiple inheritance, this might mean "C's A" if C : A, B.  */
708 static void
709 expand_virtual_init (main_binfo, binfo, decl)
710      tree main_binfo, binfo;
711      tree decl;
712 {
713   tree type;
714   tree vtbl, vtbl_ptr;
715   tree vtype, vtype_binfo;
716
717   if (TREE_CODE (binfo) == TREE_VEC)
718     type = BINFO_TYPE (binfo);
719   else if (TREE_CODE (binfo) == RECORD_TYPE)
720     {
721       type = binfo;
722       binfo = TYPE_BINFO (type);
723     }
724   else
725     my_friendly_abort (46);
726
727   vtype = DECL_CONTEXT (CLASSTYPE_VFIELD (type));
728   vtype_binfo = get_binfo (vtype, TREE_TYPE (TREE_TYPE (decl)), 0);
729 #if 0
730   /* This code suggests that it's time to rewrite how we handle
731      replicated baseclasses in G++.  */
732   if (get_base_distance (vtype, TREE_TYPE (TREE_TYPE (decl)),
733                          0, (tree *) 0) == -2)
734     {
735       tree binfos = TYPE_BINFO_BASETYPES (TREE_TYPE (TREE_TYPE (decl)));
736       int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
737
738       for (i = n_baselinks-1; i >= 0; i--)
739         {
740           tree base_binfo = TREE_VEC_ELT (binfos, i);
741           tree this_decl;
742
743           if (get_base_distance (vtype, BINFO_TYPE (base_binfo), 0, 0) == -1)
744             continue;
745
746           if (TREE_VIA_VIRTUAL (base_binfo))
747             this_decl = build_vbase_pointer (build_indirect_ref (decl, NULL_PTR), BINFO_TYPE (base_binfo));
748           else if (BINFO_OFFSET_ZEROP (base_binfo))
749             this_decl = build1 (NOP_EXPR, TYPE_POINTER_TO (BINFO_TYPE (base_binfo)),
750                                 decl);
751           else
752             this_decl = build (PLUS_EXPR, TYPE_POINTER_TO (BINFO_TYPE (base_binfo)),
753                                decl, BINFO_OFFSET (base_binfo));
754           expand_virtual_init (main_binfo, base_binfo, this_decl);
755         }
756       return;
757     }
758 #endif
759
760     {
761 #if 1
762 #if 1
763       vtbl = BINFO_VTABLE (binfo_value (DECL_FIELD_CONTEXT (CLASSTYPE_VFIELD (type)), binfo));
764 #else
765       /* The below does not work when we have to step through the
766          vfield, on our way down to the most base class for the
767          vfield. */
768       vtbl = BINFO_VTABLE (binfo_value (DECL_FIELD_CONTEXT (CLASSTYPE_VFIELD (type)),
769                                         BINFO_TYPE (main_binfo)));
770 #endif
771 #else
772       my_friendly_assert (BINFO_TYPE (main_binfo) == BINFO_TYPE (binfo), 208);
773       vtbl = BINFO_VTABLE (main_binfo);
774 #endif /* 1 */
775       assemble_external (vtbl);
776       TREE_USED (vtbl) = 1;
777       vtbl = build1 (ADDR_EXPR, TYPE_POINTER_TO (TREE_TYPE (vtbl)), vtbl);
778     }
779   decl = convert_pointer_to_real (vtype_binfo, decl);
780   vtbl_ptr = build_vfield_ref (build_indirect_ref (decl, NULL_PTR), vtype);
781   if (vtbl_ptr == error_mark_node)
782     return;
783
784   /* Have to convert VTBL since array sizes may be different.  */
785   expand_expr_stmt (build_modify_expr (vtbl_ptr, NOP_EXPR,
786                                        convert (TREE_TYPE (vtbl_ptr), vtbl)));
787 }
788
789 /* Subroutine of `expand_aggr_vbase_init'.
790    BINFO is the binfo of the type that is being initialized.
791    INIT_LIST is the list of initializers for the virtual baseclass.  */
792 static void
793 expand_aggr_vbase_init_1 (binfo, exp, addr, init_list)
794      tree binfo, exp, addr, init_list;
795 {
796   tree init = value_member (BINFO_TYPE (binfo), init_list);
797   tree ref = build_indirect_ref (addr, NULL_PTR);
798   if (init)
799     init = TREE_PURPOSE (init);
800   /* Call constructors, but don't set up vtables.  */
801   expand_aggr_init_1 (binfo, exp, ref, init, 0,
802                       LOOKUP_COMPLAIN|LOOKUP_SPECULATIVELY);
803   CLEAR_BINFO_VBASE_INIT_MARKED (binfo);
804 }
805
806 /* Initialize this object's virtual base class pointers.  This must be
807    done only at the top-level of the object being constructed.
808
809    INIT_LIST is list of initialization for constructor to perform.  */
810 static void
811 expand_aggr_vbase_init (binfo, exp, addr, init_list)
812      tree binfo;
813      tree exp;
814      tree addr;
815      tree init_list;
816 {
817   tree type = BINFO_TYPE (binfo);
818
819   if (TYPE_USES_VIRTUAL_BASECLASSES (type))
820     {
821       tree result = init_vbase_pointers (type, addr);
822       tree vbases;
823
824       if (result)
825         expand_expr_stmt (build_compound_expr (result));
826
827       /* Mark everything as having an initializer
828          (either explicit or default).  */
829       for (vbases = CLASSTYPE_VBASECLASSES (type);
830            vbases; vbases = TREE_CHAIN (vbases))
831         SET_BINFO_VBASE_INIT_MARKED (vbases);
832
833       /* First, initialize baseclasses which could be baseclasses
834          for other virtual baseclasses.  */
835       for (vbases = CLASSTYPE_VBASECLASSES (type);
836            vbases; vbases = TREE_CHAIN (vbases))
837         /* Don't initialize twice.  */
838         if (BINFO_VBASE_INIT_MARKED (vbases))
839           {
840             tree tmp = result;
841
842             while (BINFO_TYPE (vbases) != BINFO_TYPE (TREE_PURPOSE (tmp)))
843               tmp = TREE_CHAIN (tmp);
844             expand_aggr_vbase_init_1 (vbases, exp,
845                                       TREE_OPERAND (TREE_VALUE (tmp), 0),
846                                       init_list);
847           }
848
849       /* Now initialize the baseclasses which don't have virtual baseclasses.  */
850       for (; result; result = TREE_CHAIN (result))
851         /* Don't initialize twice.  */
852         if (BINFO_VBASE_INIT_MARKED (TREE_PURPOSE (result)))
853           {
854             my_friendly_abort (47);
855             expand_aggr_vbase_init_1 (TREE_PURPOSE (result), exp,
856                                       TREE_OPERAND (TREE_VALUE (result), 0),
857                                       init_list);
858           }
859     }
860 }
861
862 /* Subroutine to perform parser actions for member initialization.
863    S_ID is the scoped identifier.
864    NAME is the name of the member.
865    INIT is the initializer, or `void_type_node' if none.  */
866 void
867 do_member_init (s_id, name, init)
868      tree s_id, name, init;
869 {
870   tree binfo, base;
871
872   if (current_class_type == NULL_TREE
873       || ! is_aggr_typedef (s_id, 1))
874     return;
875   binfo = get_binfo (IDENTIFIER_TYPE_VALUE (s_id),
876                           current_class_type, 1);
877   if (binfo == error_mark_node)
878     return;
879   if (binfo == 0)
880     {
881       error_not_base_type (IDENTIFIER_TYPE_VALUE (s_id), current_class_type);
882       return;
883     }
884
885   base = convert_pointer_to (binfo, current_class_decl);
886   expand_member_init (build_indirect_ref (base, NULL_PTR), name, init);
887 }
888
889 /* Function to give error message if member initialization specification
890    is erroneous.  FIELD is the member we decided to initialize.
891    TYPE is the type for which the initialization is being performed.
892    FIELD must be a member of TYPE, or the base type from which FIELD
893    comes must not need a constructor.
894    
895    MEMBER_NAME is the name of the member.  */
896
897 static int
898 member_init_ok_or_else (field, type, member_name)
899      tree field;
900      tree type;
901      char *member_name;
902 {
903   if (field == error_mark_node)
904     return 0;
905   if (field == NULL_TREE)
906     {
907       cp_error ("class `%T' does not have any field named `%s'", type,
908                   member_name);
909       return 0;
910     }
911   if (DECL_CONTEXT (field) != type
912       && TYPE_NEEDS_CONSTRUCTING (DECL_CONTEXT (field)))
913     {
914       cp_error ("member `%D' comes from base class needing constructor",
915                 field);
916       return 0;
917     }
918   return 1;
919 }
920
921 /* If NAME is a viable field name for the aggregate DECL,
922    and PARMS is a viable parameter list, then expand an _EXPR
923    which describes this initialization.
924
925    Note that we do not need to chase through the class's base classes
926    to look for NAME, because if it's in that list, it will be handled
927    by the constructor for that base class.
928
929    We do not yet have a fixed-point finder to instantiate types
930    being fed to overloaded constructors.  If there is a unique
931    constructor, then argument types can be got from that one.
932
933    If INIT is non-NULL, then it the initialization should
934    be placed in `current_base_init_list', where it will be processed
935    by `emit_base_init'.  */
936 void
937 expand_member_init (exp, name, init)
938      tree exp, name, init;
939 {
940   extern tree ptr_type_node;    /* should be in tree.h */
941
942   tree basetype = NULL_TREE, field;
943   tree parm;
944   tree rval, type;
945   tree actual_name;
946
947   if (exp == NULL_TREE)
948     return;                     /* complain about this later */
949
950   type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
951
952   if (name == NULL_TREE && IS_AGGR_TYPE (type))
953     switch (CLASSTYPE_N_BASECLASSES (type))
954       {
955       case 0:
956         error ("base class initializer specified, but no base class to initialize");
957         return;
958       case 1:
959         basetype = TYPE_BINFO_BASETYPE (type, 0);
960         break;
961       default:
962         error ("initializer for unnamed base class ambiguous");
963         cp_error ("(type `%T' uses multiple inheritance)", type);
964         return;
965       }
966
967   if (init)
968     {
969       /* The grammar should not allow fields which have names
970          that are TYPENAMEs.  Therefore, if the field has
971          a non-NULL TREE_TYPE, we may assume that this is an
972          attempt to initialize a base class member of the current
973          type.  Otherwise, it is an attempt to initialize a
974          member field.  */
975
976       if (init == void_type_node)
977         init = NULL_TREE;
978
979       if (name == NULL_TREE || IDENTIFIER_HAS_TYPE_VALUE (name))
980         {
981           tree base_init;
982
983           if (name == NULL_TREE)
984             {
985 /*
986               if (basetype)
987                 name = TYPE_IDENTIFIER (basetype);
988               else
989                 {
990                   error ("no base class to initialize");
991                   return;
992                 }
993 */
994             }
995           else
996             {
997               basetype = IDENTIFIER_TYPE_VALUE (name);
998               if (basetype != type
999                   && ! binfo_member (basetype, TYPE_BINFO (type))
1000                   && ! binfo_member (basetype, CLASSTYPE_VBASECLASSES (type)))
1001                 {
1002                   if (IDENTIFIER_CLASS_VALUE (name))
1003                     goto try_member;
1004                   if (TYPE_USES_VIRTUAL_BASECLASSES (type))
1005                     error ("type `%s' is not an immediate or virtual basetype for `%s'",
1006                            IDENTIFIER_POINTER (name),
1007                            TYPE_NAME_STRING (type));
1008                   else
1009                     error ("type `%s' is not an immediate basetype for `%s'",
1010                            IDENTIFIER_POINTER (name),
1011                            TYPE_NAME_STRING (type));
1012                   return;
1013                 }
1014             }
1015
1016           if (purpose_member (name, current_base_init_list))
1017             {
1018               error ("base class `%s' already initialized",
1019                      IDENTIFIER_POINTER (name));
1020               return;
1021             }
1022
1023           base_init = build_tree_list (name, init);
1024           TREE_TYPE (base_init) = basetype;
1025           current_base_init_list = chainon (current_base_init_list, base_init);
1026         }
1027       else
1028         {
1029           tree member_init;
1030
1031         try_member:
1032           field = lookup_field (type, name, 1, 0);
1033
1034           if (! member_init_ok_or_else (field, type, IDENTIFIER_POINTER (name)))
1035             return;
1036
1037           if (purpose_member (name, current_member_init_list))
1038             {
1039               error ("field `%s' already initialized", IDENTIFIER_POINTER (name));
1040               return;
1041             }
1042
1043           member_init = build_tree_list (name, init);
1044           TREE_TYPE (member_init) = TREE_TYPE (field);
1045           current_member_init_list = chainon (current_member_init_list, member_init);
1046         }
1047       return;
1048     }
1049   else if (name == NULL_TREE)
1050     {
1051       compiler_error ("expand_member_init: name == NULL_TREE");
1052       return;
1053     }
1054
1055   basetype = type;
1056   field = lookup_field (basetype, name, 0, 0);
1057
1058   if (! member_init_ok_or_else (field, basetype, IDENTIFIER_POINTER (name)))
1059     return;
1060
1061   /* now see if there is a constructor for this type
1062      which will take these args. */
1063
1064   if (TYPE_HAS_CONSTRUCTOR (TREE_TYPE (field)))
1065     {
1066       tree parmtypes, fndecl;
1067
1068       if (TREE_CODE (exp) == VAR_DECL || TREE_CODE (exp) == PARM_DECL)
1069         {
1070           /* just know that we've seen something for this node */
1071           DECL_INITIAL (exp) = error_mark_node;
1072           TREE_USED (exp) = 1;
1073         }
1074       type = TYPE_MAIN_VARIANT (TREE_TYPE (field));
1075       actual_name = TYPE_IDENTIFIER (type);
1076       parm = build_component_ref (exp, name, 0, 0);
1077
1078       /* Now get to the constructor.  */
1079       fndecl = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), 0);
1080       /* Get past destructor, if any.  */
1081       if (TYPE_HAS_DESTRUCTOR (type))
1082         fndecl = DECL_CHAIN (fndecl);
1083
1084       if (fndecl)
1085         my_friendly_assert (TREE_CODE (fndecl) == FUNCTION_DECL, 209);
1086
1087       /* If the field is unique, we can use the parameter
1088          types to guide possible type instantiation.  */
1089       if (DECL_CHAIN (fndecl) == NULL_TREE)
1090         {
1091           /* There was a confusion here between
1092              FIELD and FNDECL.  The following code
1093              should be correct, but abort is here
1094              to make sure.  */
1095           my_friendly_abort (48);
1096           parmtypes = FUNCTION_ARG_CHAIN (fndecl);
1097         }
1098       else
1099         {
1100           parmtypes = NULL_TREE;
1101           fndecl = NULL_TREE;
1102         }
1103
1104       init = convert_arguments (parm, parmtypes, NULL_TREE, fndecl, LOOKUP_NORMAL);
1105       if (init == NULL_TREE || TREE_TYPE (init) != error_mark_node)
1106         rval = build_method_call (NULL_TREE, actual_name, init, NULL_TREE, LOOKUP_NORMAL);
1107       else
1108         return;
1109
1110       if (rval != error_mark_node)
1111         {
1112           /* Now, fill in the first parm with our guy */
1113           TREE_VALUE (TREE_OPERAND (rval, 1))
1114             = build_unary_op (ADDR_EXPR, parm, 0);
1115           TREE_TYPE (rval) = ptr_type_node;
1116           TREE_SIDE_EFFECTS (rval) = 1;
1117         }
1118     }
1119   else if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (field)))
1120     {
1121       parm = build_component_ref (exp, name, 0, 0);
1122       expand_aggr_init (parm, NULL_TREE, 0);
1123       rval = error_mark_node;
1124     }
1125
1126   /* Now initialize the member.  It does not have to
1127      be of aggregate type to receive initialization.  */
1128   if (rval != error_mark_node)
1129     expand_expr_stmt (rval);
1130 }
1131
1132 /* This is like `expand_member_init', only it stores one aggregate
1133    value into another.
1134
1135    INIT comes in two flavors: it is either a value which
1136    is to be stored in EXP, or it is a parameter list
1137    to go to a constructor, which will operate on EXP.
1138    If `init' is a CONSTRUCTOR, then we emit a warning message,
1139    explaining that such initializations are illegal.
1140
1141    ALIAS_THIS is nonzero iff we are initializing something which is
1142    essentially an alias for C_C_D.  In this case, the base constructor
1143    may move it on us, and we must keep track of such deviations.
1144
1145    If INIT resolves to a CALL_EXPR which happens to return
1146    something of the type we are looking for, then we know
1147    that we can safely use that call to perform the
1148    initialization.
1149
1150    The virtual function table pointer cannot be set up here, because
1151    we do not really know its type.
1152
1153    Virtual baseclass pointers are also set up here.
1154
1155    This never calls operator=().
1156
1157    When initializing, nothing is CONST.
1158
1159    A default copy constructor may have to be used to perform the
1160    initialization.
1161
1162    A constructor or a conversion operator may have to be used to
1163    perform the initialization, but not both, as it would be ambiguous.
1164    */
1165
1166 void
1167 expand_aggr_init (exp, init, alias_this)
1168      tree exp, init;
1169      int alias_this;
1170 {
1171   tree type = TREE_TYPE (exp);
1172   int was_const = TREE_READONLY (exp);
1173
1174   if (init == error_mark_node)
1175     return;
1176
1177   TREE_READONLY (exp) = 0;
1178
1179   if (TREE_CODE (type) == ARRAY_TYPE)
1180     {
1181       /* Must arrange to initialize each element of EXP
1182          from elements of INIT.  */
1183       int was_const_elts = TYPE_READONLY (TREE_TYPE (type));
1184       tree itype = init ? TREE_TYPE (init) : NULL_TREE;
1185       if (was_const_elts)
1186         {
1187           tree atype = build_cplus_array_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
1188                                                TYPE_DOMAIN (type));
1189           if (init && (TREE_TYPE (exp) == TREE_TYPE (init)))
1190             TREE_TYPE (init) = atype;
1191           TREE_TYPE (exp) = atype;
1192         }
1193       if (init && TREE_TYPE (init) == NULL_TREE)
1194         {
1195           /* Handle bad initializers like:
1196              class COMPLEX {
1197              public:
1198                double re, im;
1199                COMPLEX(double r = 0.0, double i = 0.0) {re = r; im = i;};
1200                ~COMPLEX() {};
1201              };
1202
1203              int main(int argc, char **argv) {
1204                COMPLEX zees(1.0, 0.0)[10];
1205              }
1206           */
1207           error ("bad array initializer");
1208           return;
1209         }
1210       expand_vec_init (exp, exp, array_type_nelts (type), init,
1211                        init && comptypes (TREE_TYPE (init), TREE_TYPE (exp), 1));
1212       TREE_READONLY (exp) = was_const;
1213       TREE_TYPE (exp) = type;
1214       if (init) TREE_TYPE (init) = itype;
1215       return;
1216     }
1217
1218   if (TREE_CODE (exp) == VAR_DECL || TREE_CODE (exp) == PARM_DECL)
1219     /* just know that we've seen something for this node */
1220     TREE_USED (exp) = 1;
1221
1222 #if 0
1223   /* If initializing from a GNU C CONSTRUCTOR, consider the elts in the
1224      constructor as parameters to an implicit GNU C++ constructor.  */
1225   if (init && TREE_CODE (init) == CONSTRUCTOR
1226       && TYPE_HAS_CONSTRUCTOR (type)
1227       && TREE_TYPE (init) == type)
1228     init = CONSTRUCTOR_ELTS (init);
1229 #endif
1230   expand_aggr_init_1 (TYPE_BINFO (type), exp, exp,
1231                       init, alias_this, LOOKUP_NORMAL);
1232   TREE_READONLY (exp) = was_const;
1233 }
1234
1235 static void
1236 expand_default_init (binfo, true_exp, exp, type, init, alias_this, flags)
1237      tree binfo;
1238      tree true_exp, exp;
1239      tree type;
1240      tree init;
1241      int alias_this;
1242      int flags;
1243 {
1244   /* It fails because there may not be a constructor which takes
1245      its own type as the first (or only parameter), but which does
1246      take other types via a conversion.  So, if the thing initializing
1247      the expression is a unit element of type X, first try X(X&),
1248      followed by initialization by X.  If neither of these work
1249      out, then look hard.  */
1250   tree rval;
1251   tree parms;
1252   int xxref_init_possible;
1253
1254   if (init == NULL_TREE || TREE_CODE (init) == TREE_LIST)
1255     {
1256       parms = init;
1257       if (parms) init = TREE_VALUE (parms);
1258     }
1259   else if (TREE_CODE (init) == INDIRECT_REF && TREE_HAS_CONSTRUCTOR (init))
1260     {
1261       rval = convert_for_initialization (exp, type, init, 0, 0, 0, 0);
1262       expand_expr_stmt (rval);
1263       return;
1264     }
1265   else
1266     parms = build_tree_list (NULL_TREE, init);
1267
1268   if (TYPE_HAS_INIT_REF (type)
1269       || init == NULL_TREE
1270       || TREE_CHAIN (parms) != NULL_TREE)
1271     xxref_init_possible = 0;
1272   else
1273     {
1274       xxref_init_possible = LOOKUP_SPECULATIVELY;
1275       flags &= ~LOOKUP_COMPLAIN;
1276     }
1277
1278   if (TYPE_USES_VIRTUAL_BASECLASSES (type))
1279     {
1280       if (true_exp == exp)
1281         parms = tree_cons (NULL_TREE, integer_one_node, parms);
1282       else
1283         parms = tree_cons (NULL_TREE, integer_zero_node, parms);
1284       flags |= LOOKUP_HAS_IN_CHARGE;
1285     }
1286
1287   rval = build_method_call (exp, constructor_name_full (type),
1288                             parms, binfo, flags|xxref_init_possible);
1289   if (rval == NULL_TREE && xxref_init_possible)
1290     {
1291       /* It is an error to implement a default copy constructor if
1292          (see ARM 12.8 for details) ... one case being if another
1293          copy constructor already exists. */
1294       tree init_type = TREE_TYPE (init);
1295       if (TREE_CODE (init_type) == REFERENCE_TYPE)
1296         init_type = TREE_TYPE (init_type);
1297       if (TYPE_MAIN_VARIANT (init_type) == TYPE_MAIN_VARIANT (type)
1298           || (IS_AGGR_TYPE (init_type)
1299               && UNIQUELY_DERIVED_FROM_P (type, init_type)))
1300         {
1301           if (type == BINFO_TYPE (binfo)
1302               && TYPE_USES_VIRTUAL_BASECLASSES (type))
1303             {
1304               tree addr = build_unary_op (ADDR_EXPR, exp, 0);
1305               expand_aggr_vbase_init (binfo, exp, addr, NULL_TREE);
1306
1307               expand_vbase_vtables_init (binfo, binfo, exp, addr, 1);
1308             }
1309           expand_expr_stmt (build_modify_expr (exp, INIT_EXPR, init));
1310           return;
1311         }
1312       else
1313         rval = build_method_call (exp, constructor_name_full (type), parms,
1314                                   binfo, flags);
1315     }
1316
1317   /* Private, protected, or otherwise unavailable.  */
1318   if (rval == error_mark_node && (flags&LOOKUP_COMPLAIN))
1319     cp_error ("in base initialization for class `%T'", binfo);
1320   /* A valid initialization using constructor.  */
1321   else if (rval != error_mark_node && rval != NULL_TREE)
1322     {
1323       /* p. 222: if the base class assigns to `this', then that
1324          value is used in the derived class.  */
1325       if ((flag_this_is_variable & 1) && alias_this)
1326         {
1327           TREE_TYPE (rval) = TREE_TYPE (current_class_decl);
1328           expand_assignment (current_class_decl, rval, 0, 0);
1329         }
1330       else
1331         expand_expr_stmt (rval);
1332     }
1333   else if (parms && TREE_CHAIN (parms) == NULL_TREE)
1334     {
1335       /* If we are initializing one aggregate value
1336          from another, and though there are constructors,
1337          and none accept the initializer, just do a bitwise
1338          copy.
1339
1340          The above sounds wrong, ``If a class has any copy
1341          constructor defined, the default copy constructor will
1342          not be generated.'' 12.8 Copying Class Objects  (mrs)
1343
1344          @@ This should reject initializer which a constructor
1345          @@ rejected on access gounds, but there is
1346          @@ no way right now to recognize that case with
1347          @@ just `error_mark_node'.  */
1348       tree itype;
1349       init = TREE_VALUE (parms);
1350       itype = TREE_TYPE (init);
1351       if (TREE_CODE (itype) == REFERENCE_TYPE)
1352         {
1353           init = convert_from_reference (init);
1354           itype = TREE_TYPE (init);
1355         }
1356       itype = TYPE_MAIN_VARIANT (itype);
1357
1358       /* This is currently how the default X(X&) constructor
1359          is implemented.  */
1360       if (comptypes (TYPE_MAIN_VARIANT (type), itype, 0))
1361         {
1362 #if 0
1363           warning ("bitwise copy in initialization of type `%s'",
1364                    TYPE_NAME_STRING (type));
1365 #endif
1366           rval = build (INIT_EXPR, type, exp, init);
1367           expand_expr_stmt (rval);
1368         }
1369       else
1370         {
1371           cp_error ("in base initialization for class `%T',", binfo);
1372           cp_error ("invalid initializer to constructor for type `%T'", type);
1373           return;
1374         }
1375     }
1376   else
1377     {
1378       if (init == NULL_TREE)
1379         my_friendly_assert (parms == NULL_TREE, 210);
1380       if (parms == NULL_TREE && TREE_VIA_VIRTUAL (binfo))
1381         cp_error ("virtual baseclass `%T' does not have default initializer", binfo);
1382       else
1383         {
1384           cp_error ("in base initialization for class `%T',", binfo);
1385           /* This will make an error message for us.  */
1386           build_method_call (exp, constructor_name_full (type), parms, binfo,
1387                              (TYPE_USES_VIRTUAL_BASECLASSES (type)
1388                               ? LOOKUP_NORMAL|LOOKUP_HAS_IN_CHARGE
1389                               : LOOKUP_NORMAL));
1390         }
1391       return;
1392     }
1393   /* Constructor has been called, but vtables may be for TYPE
1394      rather than for FOR_TYPE.  */
1395 }
1396
1397 /* This function is responsible for initializing EXP with INIT
1398    (if any).
1399
1400    BINFO is the binfo of the type for who we are performing the
1401    initialization.  For example, if W is a virtual base class of A and B,
1402    and C : A, B.
1403    If we are initializing B, then W must contain B's W vtable, whereas
1404    were we initializing C, W must contain C's W vtable.
1405
1406    TRUE_EXP is nonzero if it is the true expression being initialized.
1407    In this case, it may be EXP, or may just contain EXP.  The reason we
1408    need this is because if EXP is a base element of TRUE_EXP, we
1409    don't necessarily know by looking at EXP where its virtual
1410    baseclass fields should really be pointing.  But we do know
1411    from TRUE_EXP.  In constructors, we don't know anything about
1412    the value being initialized.
1413
1414    ALIAS_THIS serves the same purpose it serves for expand_aggr_init.
1415
1416    FLAGS is just passes to `build_method_call'.  See that function for
1417    its description.  */
1418
1419 static void
1420 expand_aggr_init_1 (binfo, true_exp, exp, init, alias_this, flags)
1421      tree binfo;
1422      tree true_exp, exp;
1423      tree init;
1424      int alias_this;
1425      int flags;
1426 {
1427   tree type = TREE_TYPE (exp);
1428   tree init_type = NULL_TREE;
1429
1430   my_friendly_assert (init != error_mark_node && type != error_mark_node, 211);
1431
1432   /* Use a function returning the desired type to initialize EXP for us.
1433      If the function is a constructor, and its first argument is
1434      NULL_TREE, know that it was meant for us--just slide exp on
1435      in and expand the constructor.  Constructors now come
1436      as TARGET_EXPRs.  */
1437   if (init)
1438     {
1439       tree init_list = NULL_TREE;
1440
1441       if (TREE_CODE (init) == TREE_LIST)
1442         {
1443           init_list = init;
1444           if (TREE_CHAIN (init) == NULL_TREE)
1445             init = TREE_VALUE (init);
1446         }
1447
1448       init_type = TREE_TYPE (init);
1449
1450       if (TREE_CODE (init) != TREE_LIST)
1451         {
1452           if (TREE_CODE (init_type) == ERROR_MARK)
1453             return;
1454
1455 #if 0
1456           /* These lines are found troublesome 5/11/89.  */
1457           if (TREE_CODE (init_type) == REFERENCE_TYPE)
1458             init_type = TREE_TYPE (init_type);
1459 #endif
1460
1461           /* This happens when we use C++'s functional cast notation.
1462              If the types match, then just use the TARGET_EXPR
1463              directly.  Otherwise, we need to create the initializer
1464              separately from the object being initialized.  */
1465           if (TREE_CODE (init) == TARGET_EXPR)
1466             {
1467               if (init_type == type)
1468                 {
1469                   if (TREE_CODE (exp) == VAR_DECL
1470                       || TREE_CODE (exp) == RESULT_DECL)
1471                     /* Unify the initialization targets.  */
1472                     DECL_RTL (TREE_OPERAND (init, 0)) = DECL_RTL (exp);
1473                   else
1474                     DECL_RTL (TREE_OPERAND (init, 0)) = expand_expr (exp, NULL_RTX, 0, 0);
1475
1476                   expand_expr_stmt (init);
1477                   return;
1478                 }
1479               else
1480                 {
1481                   init = TREE_OPERAND (init, 1);
1482                   init = build (CALL_EXPR, init_type,
1483                                 TREE_OPERAND (init, 0), TREE_OPERAND (init, 1), 0);
1484                   TREE_SIDE_EFFECTS (init) = 1;
1485 #if 0
1486                   TREE_RAISES (init) = ??
1487 #endif
1488                     if (init_list)
1489                       TREE_VALUE (init_list) = init;
1490                 }
1491             }
1492
1493           if (init_type == type && TREE_CODE (init) == CALL_EXPR
1494 #if 0
1495               /* It is legal to directly initialize from a CALL_EXPR
1496                  without going through X(X&), apparently.  */
1497               && ! TYPE_GETS_INIT_REF (type)
1498 #endif
1499               )
1500             {
1501               /* A CALL_EXPR is a legitimate form of initialization, so
1502                  we should not print this warning message.  */
1503 #if 0
1504               /* Should have gone away due to 5/11/89 change.  */
1505               if (TREE_CODE (TREE_TYPE (init)) == REFERENCE_TYPE)
1506                 init = convert_from_reference (init);
1507 #endif
1508               expand_assignment (exp, init, 0, 0);
1509               if (exp == DECL_RESULT (current_function_decl))
1510                 {
1511                   /* Failing this assertion means that the return value
1512                      from receives multiple initializations.  */
1513                   my_friendly_assert (DECL_INITIAL (exp) == NULL_TREE
1514                                       || DECL_INITIAL (exp) == error_mark_node,
1515                                       212);
1516                   DECL_INITIAL (exp) = init;
1517                 }
1518               return;
1519             }
1520           else if (init_type == type
1521                    && TREE_CODE (init) == COND_EXPR)
1522             {
1523               /* Push value to be initialized into the cond, where possible.
1524                  Avoid spurious warning messages when initializing the
1525                  result of this function.  */
1526               TREE_OPERAND (init, 1)
1527                 = build_modify_expr (exp, INIT_EXPR, TREE_OPERAND (init, 1));
1528               if (exp == DECL_RESULT (current_function_decl))
1529                 DECL_INITIAL (exp) = NULL_TREE;
1530               TREE_OPERAND (init, 2)
1531                 = build_modify_expr (exp, INIT_EXPR, TREE_OPERAND (init, 2));
1532               if (exp == DECL_RESULT (current_function_decl))
1533                 DECL_INITIAL (exp) = init;
1534               TREE_SIDE_EFFECTS (init) = 1;
1535               expand_expr (init, const0_rtx, VOIDmode, 0);
1536               free_temp_slots ();
1537               return;
1538             }
1539         }
1540
1541       /* We did not know what we were initializing before.  Now we do.  */
1542       if (TREE_CODE (init) == TARGET_EXPR)
1543         {
1544           tree tmp = TREE_OPERAND (TREE_OPERAND (init, 1), 1);
1545
1546           if (TREE_CODE (TREE_VALUE (tmp)) == NOP_EXPR
1547               && TREE_OPERAND (TREE_VALUE (tmp), 0) == integer_zero_node)
1548             {
1549               /* In order for this to work for RESULT_DECLs, if their
1550                  type has a constructor, then they must be BLKmode
1551                  so that they will be meaningfully addressable.  */
1552               tree arg = build_unary_op (ADDR_EXPR, exp, 0);
1553               init = TREE_OPERAND (init, 1);
1554               init = build (CALL_EXPR, build_pointer_type (TREE_TYPE (init)),
1555                             TREE_OPERAND (init, 0), TREE_OPERAND (init, 1), 0);
1556               TREE_SIDE_EFFECTS (init) = 1;
1557 #if 0
1558               TREE_RAISES (init) = ??
1559 #endif
1560               TREE_VALUE (TREE_OPERAND (init, 1))
1561                 = convert_pointer_to (TREE_TYPE (TREE_TYPE (TREE_VALUE (tmp))), arg);
1562
1563               if (alias_this)
1564                 {
1565                   expand_assignment (current_function_decl, init, 0, 0);
1566                   return;
1567                 }
1568               if (exp == DECL_RESULT (current_function_decl))
1569                 {
1570                   if (DECL_INITIAL (DECL_RESULT (current_function_decl)))
1571                     fatal ("return value from function receives multiple initializations");
1572                   DECL_INITIAL (exp) = init;
1573                 }
1574               expand_expr_stmt (init);
1575               return;
1576             }
1577         }
1578
1579       if (TREE_CODE (exp) == VAR_DECL
1580           && TREE_CODE (init) == CONSTRUCTOR
1581           && TREE_HAS_CONSTRUCTOR (init))
1582         {
1583           tree t = store_init_value (exp, init);
1584           if (!t)
1585             {
1586               expand_decl_init (exp);
1587               return;
1588             }
1589           t = build (INIT_EXPR, type, exp, init);
1590           TREE_SIDE_EFFECTS (t) = 1;
1591           expand_expr_stmt (t);
1592           return;
1593         }
1594
1595       /* Handle this case: when calling a constructor: xyzzy foo(bar);
1596          which really means:  xyzzy foo = bar; Ugh!
1597
1598          More useful for this case: xyzzy *foo = new xyzzy (bar);  */
1599
1600       if (! TYPE_NEEDS_CONSTRUCTING (type) && ! IS_AGGR_TYPE (type))
1601         {
1602           if (init_list && TREE_CHAIN (init_list))
1603             {
1604               warning ("initializer list being treated as compound expression");
1605               init = convert (type, build_compound_expr (init_list));
1606               if (init == error_mark_node)
1607                 return;
1608             }
1609
1610           expand_assignment (exp, init, 0, 0);
1611
1612           return;
1613         }
1614       /* See whether we can go through a type conversion operator.
1615          This wins over going through a non-existent constructor.  If
1616          there is a constructor, it is ambiguous.  */
1617       if (TREE_CODE (init) != TREE_LIST)
1618         {
1619           tree ttype = TREE_CODE (init_type) == REFERENCE_TYPE
1620             ? TREE_TYPE (init_type) : init_type;
1621
1622           if (ttype != type && IS_AGGR_TYPE (ttype))
1623             {
1624               tree rval = build_type_conversion (CONVERT_EXPR, type, init, 0);
1625
1626               if (rval)
1627                 {
1628                   /* See if there is a constructor for``type'' that takes a
1629                      ``ttype''-typed object. */
1630                   tree parms = build_tree_list (NULL_TREE, init);
1631                   tree as_cons = NULL_TREE;
1632                   if (TYPE_HAS_CONSTRUCTOR (type))
1633                     as_cons = build_method_call (exp, constructor_name_full (type),
1634                                                  parms, binfo,
1635                                                  LOOKUP_SPECULATIVELY|LOOKUP_NO_CONVERSION);
1636                   if (as_cons != NULL_TREE && as_cons != error_mark_node)
1637                     /* ANSI C++ June 5 1992 WP 12.3.2.6.1 */
1638                     cp_error ("ambiguity between conversion to `%T' and constructor",
1639                               type);
1640                   else
1641                     expand_assignment (exp, rval, 0, 0);
1642                   return;
1643                 }
1644             }
1645         }
1646     }
1647
1648   /* Handle default copy constructors here, does not matter if there is
1649      a constructor or not.  */
1650   if (type == init_type && IS_AGGR_TYPE (type)
1651       && init && TREE_CODE (init) != TREE_LIST)
1652     expand_default_init (binfo, true_exp, exp, type, init, alias_this, flags);
1653   /* Not sure why this is here... */
1654   else if (TYPE_HAS_CONSTRUCTOR (type))
1655     expand_default_init (binfo, true_exp, exp, type, init, alias_this, flags);
1656   else if (TREE_CODE (type) == ARRAY_TYPE)
1657     {
1658       if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (type)))
1659         expand_vec_init (exp, exp, array_type_nelts (type), init, 0);
1660       else if (TYPE_VIRTUAL_P (TREE_TYPE (type)))
1661         sorry ("arrays of objects with virtual functions but no constructors");
1662     }
1663   else
1664     expand_recursive_init (binfo, true_exp, exp, init,
1665                            CLASSTYPE_BASE_INIT_LIST (type), alias_this);
1666 }
1667
1668 /* A pointer which holds the initializer.  First call to
1669    expand_aggr_init gets this value pointed to, and sets it to init_null.  */
1670 static tree *init_ptr, init_null;
1671
1672 /* Subroutine of expand_recursive_init:
1673
1674    ADDR is the address of the expression being initialized.
1675    INIT_LIST is the cons-list of initializations to be performed.
1676    ALIAS_THIS is its same, lovable self.  */
1677 static void
1678 expand_recursive_init_1 (binfo, true_exp, addr, init_list, alias_this)
1679      tree binfo, true_exp, addr;
1680      tree init_list;
1681      int alias_this;
1682 {
1683   while (init_list)
1684     {
1685       if (TREE_PURPOSE (init_list))
1686         {
1687           if (TREE_CODE (TREE_PURPOSE (init_list)) == FIELD_DECL)
1688             {
1689               tree member = TREE_PURPOSE (init_list);
1690               tree subexp = build_indirect_ref (convert_pointer_to (TREE_VALUE (init_list), addr), NULL_PTR);
1691               tree member_base = build (COMPONENT_REF, TREE_TYPE (member), subexp, member);
1692               if (IS_AGGR_TYPE (TREE_TYPE (member)))
1693                 expand_aggr_init (member_base, DECL_INITIAL (member), 0);
1694               else if (TREE_CODE (TREE_TYPE (member)) == ARRAY_TYPE
1695                        && TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (member)))
1696                 {
1697                   member_base = save_expr (default_conversion (member_base));
1698                   expand_vec_init (member, member_base,
1699                                    array_type_nelts (TREE_TYPE (member)),
1700                                    DECL_INITIAL (member), 0);
1701                 }
1702               else
1703                 expand_expr_stmt (build_modify_expr (member_base, INIT_EXPR, DECL_INITIAL (member)));
1704             }
1705           else if (TREE_CODE (TREE_PURPOSE (init_list)) == TREE_LIST)
1706             {
1707               expand_recursive_init_1 (binfo, true_exp, addr, TREE_PURPOSE (init_list), alias_this);
1708               expand_recursive_init_1 (binfo, true_exp, addr, TREE_VALUE (init_list), alias_this);
1709             }
1710           else if (TREE_CODE (TREE_PURPOSE (init_list)) == ERROR_MARK)
1711             {
1712               /* Only initialize the virtual function tables if we
1713                  are initializing the ultimate users of those vtables.  */
1714               if (TREE_VALUE (init_list))
1715                 {
1716                   /* We have to ensure that the second argment to
1717                      expand_virtual_init is in binfo's hierarchy.  */
1718                   expand_virtual_init (binfo,
1719                                       get_binfo (TREE_VALUE (init_list), binfo, 0),
1720                                       addr);
1721                   if (TREE_VALUE (init_list) == binfo
1722                       && TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo)))
1723                     expand_vbase_vtables_init (binfo,binfo, true_exp, addr, 1);
1724                 }
1725             }
1726           else
1727             my_friendly_abort (49);
1728         }
1729       else if (TREE_VALUE (init_list)
1730                && TREE_CODE (TREE_VALUE (init_list)) == TREE_VEC)
1731         {
1732           tree subexp = build_indirect_ref (convert_pointer_to (TREE_VALUE (init_list), addr), NULL_PTR);
1733           expand_aggr_init_1 (binfo, true_exp, subexp, *init_ptr,
1734                               alias_this && BINFO_OFFSET_ZEROP (TREE_VALUE (init_list)),
1735                               LOOKUP_COMPLAIN);
1736
1737           /* INIT_PTR is used up.  */
1738           init_ptr = &init_null;
1739         }
1740       else
1741         my_friendly_abort (50);
1742       init_list = TREE_CHAIN (init_list);
1743     }
1744 }
1745
1746 /* Initialize EXP with INIT.  Type EXP does not have a constructor,
1747    but it has a baseclass with a constructor or a virtual function
1748    table which needs initializing.
1749
1750    INIT_LIST is a cons-list describing what parts of EXP actually
1751    need to be initialized.  INIT is given to the *unique*, first
1752    constructor within INIT_LIST.  If there are multiple first
1753    constructors, such as with multiple inheritance, INIT must
1754    be zero or an ambiguity error is reported.
1755
1756    ALIAS_THIS is passed from `expand_aggr_init'.  See comments
1757    there.  */
1758
1759 static void
1760 expand_recursive_init (binfo, true_exp, exp, init, init_list, alias_this)
1761      tree binfo, true_exp, exp, init;
1762      tree init_list;
1763      int alias_this;
1764 {
1765   tree *old_init_ptr = init_ptr;
1766   tree addr = build_unary_op (ADDR_EXPR, exp, 0);
1767   init_ptr = &init;
1768
1769   if (true_exp == exp && TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo)))
1770     {
1771       expand_aggr_vbase_init (binfo, exp, addr, init_list);
1772       expand_vbase_vtables_init (binfo, binfo, true_exp, addr, 1);
1773     }
1774   expand_recursive_init_1 (binfo, true_exp, addr, init_list, alias_this);
1775
1776   if (*init_ptr)
1777     {
1778       tree type = TREE_TYPE (exp);
1779
1780       if (TREE_CODE (type) == REFERENCE_TYPE)
1781         type = TREE_TYPE (type);
1782       if (IS_AGGR_TYPE (type))
1783         cp_error ("unexpected argument to constructor `%T'", type);
1784       else
1785         error ("unexpected argument to constructor");
1786     }
1787   init_ptr = old_init_ptr;
1788 }
1789
1790 /* Report an error if NAME is not the name of a user-defined,
1791    aggregate type.  If OR_ELSE is nonzero, give an error message.  */
1792 int
1793 is_aggr_typedef (name, or_else)
1794      tree name;
1795      int or_else;
1796 {
1797   tree type;
1798
1799   if (name == error_mark_node)
1800     return 0;
1801
1802   if (IDENTIFIER_HAS_TYPE_VALUE (name))
1803     type = IDENTIFIER_TYPE_VALUE (name);
1804   else if (IDENTIFIER_HAS_CLASS_TYPE_VALUE (name))
1805     type = IDENTIFIER_CLASS_TYPE_VALUE (name);
1806   else
1807     {
1808       if (or_else)
1809         cp_error ("`%T' fails to be an aggregate typedef", name);
1810       return 0;
1811     }
1812
1813   if (! IS_AGGR_TYPE (type)
1814       && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
1815     {
1816       if (or_else)
1817         cp_error ("type `%T' is of non-aggregate type", type);
1818       return 0;
1819     }
1820   return 1;
1821 }
1822
1823 /* Like is_aggr_typedef, but returns typedef if successful.  */
1824 tree
1825 get_aggr_from_typedef (name, or_else)
1826      tree name;
1827      int or_else;
1828 {
1829   tree type;
1830
1831   if (name == error_mark_node)
1832     return NULL_TREE;
1833
1834   if (IDENTIFIER_HAS_TYPE_VALUE (name))
1835     type = IDENTIFIER_TYPE_VALUE (name);
1836   else if (IDENTIFIER_HAS_CLASS_TYPE_VALUE (name))
1837     type = IDENTIFIER_CLASS_TYPE_VALUE (name);
1838   else
1839     {
1840       if (or_else)
1841         cp_error ("`%T' fails to be an aggregate typedef", name);
1842       return NULL_TREE;
1843     }
1844
1845   if (! IS_AGGR_TYPE (type)
1846       && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
1847     {
1848       if (or_else)
1849         cp_error ("type `%T' is of non-aggregate type", type);
1850       return NULL_TREE;
1851     }
1852   return type;
1853 }
1854
1855 tree
1856 get_type_value (name)
1857      tree name;
1858 {
1859   if (name == error_mark_node)
1860     return NULL_TREE;
1861
1862   if (IDENTIFIER_HAS_TYPE_VALUE (name))
1863     return IDENTIFIER_TYPE_VALUE (name);
1864   else if (IDENTIFIER_CLASS_VALUE (name))
1865     return IDENTIFIER_CLASS_TYPE_VALUE (name);
1866   else
1867     return NULL_TREE;
1868 }
1869   
1870 \f
1871 /* This code could just as well go in `class.c', but is placed here for
1872    modularity.  */
1873
1874 /* For an expression of the form CNAME :: NAME (PARMLIST), build
1875    the appropriate function call.  */
1876 tree
1877 build_member_call (cname, name, parmlist)
1878      tree cname, name, parmlist;
1879 {
1880   tree type, t;
1881   tree method_name = name;
1882   int dtor = 0;
1883   int dont_use_this = 0;
1884   tree basetype_path, decl;
1885
1886   if (TREE_CODE (method_name) == BIT_NOT_EXPR)
1887     {
1888       method_name = TREE_OPERAND (method_name, 0);
1889       dtor = 1;
1890     }
1891
1892   if (TREE_CODE (cname) == SCOPE_REF)
1893     cname = resolve_scope_to_name (NULL_TREE, cname);
1894
1895   if (cname == NULL_TREE || ! (type = get_aggr_from_typedef (cname, 1)))
1896     return error_mark_node;
1897
1898   /* An operator we did not like.  */
1899   if (name == NULL_TREE)
1900     return error_mark_node;
1901
1902   if (dtor)
1903     {
1904 #if 0
1905       /* Everything can explicitly call a destructor; see 12.4 */
1906       if (! TYPE_HAS_DESTRUCTOR (type))
1907         cp_error ("type `%#T' does not have a destructor", type);
1908       else
1909 #endif
1910       cp_error ("cannot call destructor `%T::~%T' without object", type,
1911                 method_name);
1912       return error_mark_node;
1913     }
1914
1915   /* No object?  Then just fake one up, and let build_method_call
1916      figure out what to do.  */
1917   if (current_class_type == 0
1918       || get_base_distance (type, current_class_type, 0, &basetype_path) == -1)
1919     dont_use_this = 1;
1920
1921   if (dont_use_this)
1922     {
1923       basetype_path = NULL_TREE;
1924       decl = build1 (NOP_EXPR, TYPE_POINTER_TO (type), error_mark_node);
1925     }
1926   else if (current_class_decl == 0)
1927     {
1928       dont_use_this = 1;
1929       decl = build1 (NOP_EXPR, TYPE_POINTER_TO (type), error_mark_node);
1930     }
1931   else
1932     {
1933       tree olddecl = current_class_decl;
1934       tree oldtype = TREE_TYPE (TREE_TYPE (olddecl));
1935       if (oldtype != type)
1936         {
1937           tree newtype = build_type_variant (type, TYPE_READONLY (oldtype),
1938                                              TYPE_VOLATILE (oldtype));
1939           decl = convert_force (TYPE_POINTER_TO (newtype), olddecl);
1940         }
1941       else
1942         decl = olddecl;
1943     }
1944
1945   decl = build_indirect_ref (decl, NULL_PTR);
1946
1947   if (t = lookup_fnfields (TYPE_BINFO (type), method_name, 0))
1948     return build_method_call (decl, method_name, parmlist, basetype_path,
1949                               LOOKUP_NORMAL|LOOKUP_NONVIRTUAL);
1950   if (TREE_CODE (name) == IDENTIFIER_NODE
1951       && ((t = lookup_field (TYPE_BINFO (type), name, 1, 0))))
1952     {
1953       if (t == error_mark_node)
1954         return error_mark_node;
1955       if (TREE_CODE (t) == FIELD_DECL)
1956         {
1957           if (dont_use_this)
1958             {
1959               cp_error ("invalid use of non-static field `%D'", t);
1960               return error_mark_node;
1961             }
1962           decl = build (COMPONENT_REF, TREE_TYPE (t), decl, t);
1963         }
1964       else if (TREE_CODE (t) == VAR_DECL)
1965         decl = t;
1966       else
1967         {
1968           cp_error ("invalid use of member `%D'", t);
1969           return error_mark_node;
1970         }
1971       if (TYPE_LANG_SPECIFIC (TREE_TYPE (decl))
1972           && TYPE_OVERLOADS_CALL_EXPR (TREE_TYPE (decl)))
1973         return build_opfncall (CALL_EXPR, LOOKUP_NORMAL, decl, parmlist, NULL_TREE);
1974       return build_function_call (decl, parmlist);
1975     }
1976   else
1977     {
1978       cp_error ("no method `%T::%D'", type, name);
1979       return error_mark_node;
1980     }
1981 }
1982
1983 /* Build a reference to a member of an aggregate.  This is not a
1984    C++ `&', but really something which can have its address taken,
1985    and then act as a pointer to member, for example CNAME :: FIELD
1986    can have its address taken by saying & CNAME :: FIELD.
1987
1988    @@ Prints out lousy diagnostics for operator <typename>
1989    @@ fields.
1990
1991    @@ This function should be rewritten and placed in search.c.  */
1992 tree
1993 build_offset_ref (cname, name)
1994      tree cname, name;
1995 {
1996   tree decl, type, fnfields, fields, t = error_mark_node;
1997   tree basetypes = NULL_TREE;
1998   int dtor = 0;
1999
2000   if (TREE_CODE (cname) == SCOPE_REF)
2001     cname = resolve_scope_to_name (NULL_TREE, cname);
2002
2003   if (cname == NULL_TREE || ! is_aggr_typedef (cname, 1))
2004     return error_mark_node;
2005
2006   type = IDENTIFIER_TYPE_VALUE (cname);
2007
2008   if (TREE_CODE (name) == BIT_NOT_EXPR)
2009     {
2010       dtor = 1;
2011       name = TREE_OPERAND (name, 0);
2012     }
2013
2014   if (TYPE_SIZE (type) == 0)
2015     {
2016       t = IDENTIFIER_CLASS_VALUE (name);
2017       if (t == 0)
2018         {
2019           cp_error ("incomplete type `%T' does not have member `%D'", type,
2020                       name);
2021           return error_mark_node;
2022         }
2023       if (TREE_CODE (t) == TYPE_DECL || TREE_CODE (t) == VAR_DECL
2024           || TREE_CODE (t) == CONST_DECL)
2025         {
2026           TREE_USED (t) = 1;
2027           return t;
2028         }
2029       if (TREE_CODE (t) == FIELD_DECL)
2030         sorry ("use of member in incomplete aggregate type");
2031       else if (TREE_CODE (t) == FUNCTION_DECL)
2032         sorry ("use of member function in incomplete aggregate type");
2033       else
2034         my_friendly_abort (52);
2035       return error_mark_node;
2036     }
2037
2038   if (TREE_CODE (name) == TYPE_EXPR)
2039     /* Pass a TYPE_DECL to build_component_type_expr.  */
2040     return build_component_type_expr (TYPE_NAME (TREE_TYPE (cname)),
2041                                       name, NULL_TREE, 1);
2042
2043   fnfields = lookup_fnfields (TYPE_BINFO (type), name, 1);
2044   fields = lookup_field (type, name, 0, 0);
2045
2046   if (fields == error_mark_node || fnfields == error_mark_node)
2047     return error_mark_node;
2048
2049   if (current_class_type == 0
2050       || get_base_distance (type, current_class_type, 0, &basetypes) == -1)
2051     {
2052       basetypes = TYPE_BINFO (type);
2053       decl = build1 (NOP_EXPR,
2054                      IDENTIFIER_TYPE_VALUE (cname),
2055                      error_mark_node);
2056     }
2057   else if (current_class_decl == 0)
2058     decl = build1 (NOP_EXPR, IDENTIFIER_TYPE_VALUE (cname),
2059                    error_mark_node);
2060   else
2061     decl = C_C_D;
2062
2063   /* A lot of this logic is now handled in lookup_field and
2064      lookup_fnfield. */
2065   if (fnfields)
2066     {
2067       basetypes = TREE_PURPOSE (fnfields);
2068
2069       /* Go from the TREE_BASELINK to the member function info.  */
2070       t = TREE_VALUE (fnfields);
2071
2072       if (fields)
2073         {
2074           if (DECL_FIELD_CONTEXT (fields) == DECL_FIELD_CONTEXT (t))
2075             {
2076               error ("ambiguous member reference: member `%s' defined as both field and function",
2077                      IDENTIFIER_POINTER (name));
2078               return error_mark_node;
2079             }
2080           if (UNIQUELY_DERIVED_FROM_P (DECL_FIELD_CONTEXT (fields), DECL_FIELD_CONTEXT (t)))
2081             ;
2082           else if (UNIQUELY_DERIVED_FROM_P (DECL_FIELD_CONTEXT (t), DECL_FIELD_CONTEXT (fields)))
2083             t = fields;
2084           else
2085             {
2086               error ("ambiguous member reference: member `%s' derives from distinct classes in multiple inheritance lattice");
2087               return error_mark_node;
2088             }
2089         }
2090
2091       if (t == TREE_VALUE (fnfields))
2092         {
2093           extern int flag_save_memoized_contexts;
2094
2095           /* This does not handle access checking yet.  */
2096           if (DECL_CHAIN (t) == NULL_TREE || dtor)
2097             {
2098               enum access_type access;
2099
2100               /* unique functions are handled easily.  */
2101             unique:
2102               access = compute_access (basetypes, t);
2103               if (access == access_protected)
2104                 {
2105                   cp_error_at ("member function `%#D' is protected", t);
2106                   error ("in this context");
2107                   return error_mark_node;
2108                 }
2109               if (access == access_private)
2110                 {
2111                   cp_error_at ("member function `%#D' is private", t);
2112                   error ("in this context");
2113                   return error_mark_node;
2114                 }
2115               assemble_external (t);
2116               return build (OFFSET_REF, TREE_TYPE (t), decl, t);
2117             }
2118
2119           /* overloaded functions may need more work.  */
2120           if (cname == name)
2121             {
2122               if (TYPE_HAS_DESTRUCTOR (type)
2123                   && DECL_CHAIN (DECL_CHAIN (t)) == NULL_TREE)
2124                 {
2125                   t = DECL_CHAIN (t);
2126                   goto unique;
2127                 }
2128             }
2129           /* FNFIELDS is most likely allocated on the search_obstack,
2130              which will go away after this class scope.  If we need
2131              to save this value for later (either for memoization
2132              or for use as an initializer for a static variable), then
2133              do so here.
2134
2135              ??? The smart thing to do for the case of saving initializers
2136              is to resolve them before we're done with this scope.  */
2137           if (!TREE_PERMANENT (fnfields)
2138               && ((flag_save_memoized_contexts && global_bindings_p ())
2139                   || ! allocation_temporary_p ()))
2140             fnfields = copy_list (fnfields);
2141           t = build_tree_list (error_mark_node, fnfields);
2142           TREE_TYPE (t) = build_offset_type (type, unknown_type_node);
2143           return t;
2144         }
2145     }
2146
2147   /* Now that we know we are looking for a field, see if we
2148      have access to that field.  Lookup_field will give us the
2149      error message.  */
2150
2151   t = lookup_field (basetypes, name, 1, 0);
2152
2153   if (t == error_mark_node)
2154     return error_mark_node;
2155
2156   if (t == NULL_TREE)
2157     {
2158       cp_error ("`%D' is not a member of type `%T'", name,
2159                   IDENTIFIER_TYPE_VALUE (cname));
2160       return error_mark_node;
2161     }
2162
2163   if (TREE_CODE (t) == TYPE_DECL)
2164     {
2165       TREE_USED (t) = 1;
2166       return t;
2167     }
2168   /* static class members and class-specific enum
2169      values can be returned without further ado.  */
2170   if (TREE_CODE (t) == VAR_DECL || TREE_CODE (t) == CONST_DECL)
2171     {
2172       assemble_external (t);
2173       TREE_USED (t) = 1;
2174       return t;
2175     }
2176
2177   /* static class functions too.  */
2178   if (TREE_CODE (t) == FUNCTION_DECL && TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
2179     my_friendly_abort (53);
2180
2181   /* In member functions, the form `cname::name' is no longer
2182      equivalent to `this->cname::name'.  */
2183   return build (OFFSET_REF, build_offset_type (type, TREE_TYPE (t)), decl, t);
2184 }
2185
2186 /* Given an object EXP and a member function reference MEMBER,
2187    return the address of the actual member function.  */
2188 tree
2189 get_member_function (exp_addr_ptr, exp, member)
2190      tree *exp_addr_ptr;
2191      tree exp, member;
2192 {
2193   tree ctype = TREE_TYPE (exp);
2194   tree function = save_expr (build_unary_op (ADDR_EXPR, member, 0));
2195
2196   if (TYPE_VIRTUAL_P (ctype)
2197       || (flag_all_virtual == 1 && TYPE_OVERLOADS_METHOD_CALL_EXPR (ctype)))
2198     {
2199       tree e0, e1, e3;
2200       tree exp_addr;
2201
2202       /* Save away the unadulterated `this' pointer.  */
2203       exp_addr = save_expr (*exp_addr_ptr);
2204
2205       /* Cast function to signed integer.  */
2206       e0 = build1 (NOP_EXPR, integer_type_node, function);
2207
2208 #ifdef VTABLE_USES_MASK
2209       /* If we are willing to limit the number of
2210          virtual functions a class may have to some
2211          *small* number, then if, for a function address,
2212          we are passed some small number, we know that
2213          it is a virtual function index, and work from there.  */
2214       e1 = build (BIT_AND_EXPR, integer_type_node, e0, vtbl_mask);
2215 #else
2216       /* There is a hack here that takes advantage of
2217          twos complement arithmetic, and the fact that
2218          there are more than one UNITS to the WORD.
2219          If the high bit is set for the `function',
2220          then we pretend it is a virtual function,
2221          and the array indexing will knock this bit
2222          out the top, leaving a valid index.  */
2223       if (UNITS_PER_WORD <= 1)
2224         my_friendly_abort (54);
2225
2226       e1 = build (GT_EXPR, integer_type_node, e0, integer_zero_node);
2227       e1 = build_compound_expr (tree_cons (NULL_TREE, exp_addr,
2228                                            build_tree_list (NULL_TREE, e1)));
2229       e1 = save_expr (e1);
2230 #endif
2231
2232       if (TREE_SIDE_EFFECTS (*exp_addr_ptr))
2233         {
2234           exp = build_indirect_ref (exp_addr, NULL_PTR);
2235           *exp_addr_ptr = exp_addr;
2236         }
2237
2238       /* This is really hairy: if the function pointer is a pointer
2239          to a non-virtual member function, then we can't go mucking
2240          with the `this' pointer (any more than we already have to
2241          this point).  If it is a pointer to a virtual member function,
2242          then we have to adjust the `this' pointer according to
2243          what the virtual function table tells us.  */
2244
2245       e3 = build_vfn_ref (exp_addr_ptr, exp, e0);
2246       my_friendly_assert (e3 != error_mark_node, 213);
2247
2248       /* Change this pointer type from `void *' to the
2249          type it is really supposed to be.  */
2250       TREE_TYPE (e3) = TREE_TYPE (function);
2251
2252       /* If non-virtual, use what we had originally.  Otherwise,
2253          use the value we get from the virtual function table.  */
2254       *exp_addr_ptr = build_conditional_expr (e1, exp_addr, *exp_addr_ptr);
2255
2256       function = build_conditional_expr (e1, function, e3);
2257     }
2258   return build_indirect_ref (function, NULL_PTR);
2259 }
2260
2261 /* If a OFFSET_REF made it through to here, then it did
2262    not have its address taken.  */
2263
2264 tree
2265 resolve_offset_ref (exp)
2266      tree exp;
2267 {
2268   tree type = TREE_TYPE (exp);
2269   tree base = NULL_TREE;
2270   tree member;
2271   tree basetype, addr;
2272
2273   if (TREE_CODE (exp) == TREE_LIST)
2274     return build_unary_op (ADDR_EXPR, exp, 0);
2275
2276   if (TREE_CODE (exp) != OFFSET_REF)
2277     {
2278       my_friendly_assert (TREE_CODE (type) == OFFSET_TYPE, 214);
2279       if (TYPE_OFFSET_BASETYPE (type) != current_class_type)
2280         {
2281           error ("object missing in use of pointer-to-member construct");
2282           return error_mark_node;
2283         }
2284       member = exp;
2285       type = TREE_TYPE (type);
2286       base = C_C_D;
2287     }
2288   else
2289     {
2290       member = TREE_OPERAND (exp, 1);
2291       base = TREE_OPERAND (exp, 0);
2292     }
2293
2294   if ((TREE_CODE (member) == VAR_DECL
2295        && ! TYPE_PTRMEMFUNC_P (TREE_TYPE (member)))
2296       || TREE_CODE (TREE_TYPE (member)) == FUNCTION_TYPE)
2297     {
2298       /* These were static members.  */
2299       if (mark_addressable (member) == 0)
2300         return error_mark_node;
2301       return member;
2302     }
2303
2304   /* Syntax error can cause a member which should
2305      have been seen as static to be grok'd as non-static.  */
2306   if (TREE_CODE (member) == FIELD_DECL && C_C_D == NULL_TREE)
2307     {
2308       if (TREE_ADDRESSABLE (member) == 0)
2309         {
2310           cp_error_at ("member `%D' is non-static in static member function context", member);
2311           error ("at this point in file");
2312           TREE_ADDRESSABLE (member) = 1;
2313         }
2314       return error_mark_node;
2315     }
2316
2317   /* The first case is really just a reference to a member of `this'.  */
2318   if (TREE_CODE (member) == FIELD_DECL
2319       && (base == C_C_D
2320           || (TREE_CODE (base) == NOP_EXPR
2321               && TREE_OPERAND (base, 0) == error_mark_node)))
2322     {
2323       tree basetype_path;
2324       enum access_type access;
2325
2326       if (TREE_CODE (exp) == OFFSET_REF && TREE_CODE (type) == OFFSET_TYPE)
2327         {
2328           basetype = TYPE_OFFSET_BASETYPE (type);
2329           base = convert_pointer_to (basetype, current_class_decl);
2330         }
2331       else
2332         base = current_class_decl;
2333       basetype = DECL_CONTEXT (member);
2334       
2335       if (get_base_distance (basetype, TREE_TYPE (TREE_TYPE (base)), 0, &basetype_path) < 0)
2336         {
2337           error_not_base_type (basetype, TREE_TYPE (TREE_TYPE (base)));
2338           return error_mark_node;
2339         }
2340       addr = convert_pointer_to (basetype, base);
2341       access = compute_access (basetype_path, member);
2342       if (access == access_public)
2343         return build (COMPONENT_REF, TREE_TYPE (member),
2344                       build_indirect_ref (addr, NULL_PTR), member);
2345       if (access == access_protected)
2346         {
2347           cp_error_at ("member `%D' is protected", member);
2348           error ("in this context");
2349           return error_mark_node;
2350         }
2351       if (access == access_private)
2352         {
2353           cp_error_at ("member `%D' is private", member);
2354           error ("in this context");
2355           return error_mark_node;
2356         }
2357       my_friendly_abort (55);
2358     }
2359
2360   /* If this is a reference to a member function, then return
2361      the address of the member function (which may involve going
2362      through the object's vtable), otherwise, return an expression
2363      for the dereferenced pointer-to-member construct.  */
2364   addr = build_unary_op (ADDR_EXPR, base, 0);
2365
2366   if (TREE_CODE (TREE_TYPE (member)) == METHOD_TYPE)
2367     {
2368       basetype = DECL_CLASS_CONTEXT (member);
2369       addr = convert_pointer_to (basetype, addr);
2370       return build_unary_op (ADDR_EXPR, get_member_function (&addr, build_indirect_ref (addr, NULL_PTR), member), 0);
2371     }
2372   else if (TREE_CODE (TREE_TYPE (member)) == OFFSET_TYPE)
2373     {
2374       basetype = TYPE_OFFSET_BASETYPE (TREE_TYPE (member));
2375       addr = convert_pointer_to (basetype, addr);
2376       member = convert (ptr_type_node, build_unary_op (ADDR_EXPR, member, 0));
2377       return build1 (INDIRECT_REF, type,
2378                      build (PLUS_EXPR, ptr_type_node, addr, member));
2379     }
2380   else if (TYPE_PTRMEMFUNC_P (TREE_TYPE (member)))
2381     {
2382       return get_member_function_from_ptrfunc (&addr, base, member);
2383     }
2384   my_friendly_abort (56);
2385   /* NOTREACHED */
2386   return NULL_TREE;
2387 }
2388
2389 /* Return either DECL or its known constant value (if it has one).  */
2390
2391 tree
2392 decl_constant_value (decl)
2393      tree decl;
2394 {
2395   if (! TREE_THIS_VOLATILE (decl)
2396 #if 0
2397       /* These may be necessary for C, but they break C++.  */
2398       ! TREE_PUBLIC (decl)
2399       /* Don't change a variable array bound or initial value to a constant
2400          in a place where a variable is invalid.  */
2401       && ! pedantic
2402 #endif /* 0 */
2403       && DECL_INITIAL (decl) != 0
2404       && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
2405       /* This is invalid if initial value is not constant.
2406          If it has either a function call, a memory reference,
2407          or a variable, then re-evaluating it could give different results.  */
2408       && TREE_CONSTANT (DECL_INITIAL (decl))
2409       /* Check for cases where this is sub-optimal, even though valid.  */
2410       && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR
2411 #if 0
2412       /* We must allow this to work outside of functions so that
2413          static constants can be used for array sizes.  */
2414       && current_function_decl != 0
2415       && DECL_MODE (decl) != BLKmode
2416 #endif
2417       )
2418     return DECL_INITIAL (decl);
2419   return decl;
2420 }
2421 \f
2422 /* Friend handling routines.  */
2423 /* Friend data structures:
2424
2425    Lists of friend functions come from TYPE_DECL nodes.  Since all
2426    aggregate types are automatically typedef'd, these nodes are guaranteed
2427    to exist.
2428
2429    The TREE_PURPOSE of a friend list is the name of the friend,
2430    and its TREE_VALUE is another list.
2431
2432    For each element of that list, either the TREE_VALUE or the TREE_PURPOSE
2433    will be filled in, but not both.  The TREE_VALUE of that list is an
2434    individual function which is a friend.  The TREE_PURPOSE of that list
2435    indicates a type in which all functions by that name are friends.
2436
2437    Lists of friend classes come from _TYPE nodes.  Love that consistency
2438    thang.  */
2439
2440 int
2441 is_friend_type (type1, type2)
2442      tree type1, type2;
2443 {
2444   return is_friend (type1, type2);
2445 }
2446
2447 int
2448 is_friend (type, supplicant)
2449      tree type, supplicant;
2450 {
2451   int declp;
2452   register tree list;
2453
2454   if (supplicant == NULL_TREE || type == NULL_TREE)
2455     return 0;
2456
2457   declp = (TREE_CODE_CLASS (TREE_CODE (supplicant)) == 'd');
2458
2459   if (declp)
2460     /* It's a function decl.  */
2461     {
2462       tree list = DECL_FRIENDLIST (TYPE_NAME (type));
2463       tree name = DECL_NAME (supplicant);
2464       tree ctype = DECL_CLASS_CONTEXT (supplicant);
2465       for (; list ; list = TREE_CHAIN (list))
2466         {
2467           if (name == TREE_PURPOSE (list))
2468             {
2469               tree friends = TREE_VALUE (list);
2470               name = DECL_ASSEMBLER_NAME (supplicant);
2471               for (; friends ; friends = TREE_CHAIN (friends))
2472                 {
2473                   if (ctype == TREE_PURPOSE (friends))
2474                     return 1;
2475                   if (name == DECL_ASSEMBLER_NAME (TREE_VALUE (friends)))
2476                     return 1;
2477                 }
2478               break;
2479             }
2480         }
2481     }
2482   else
2483     /* It's a type. */
2484     {
2485       if (type == supplicant)
2486         return 1;
2487       
2488       list = CLASSTYPE_FRIEND_CLASSES (TREE_TYPE (TYPE_NAME (type)));
2489       for (; list ; list = TREE_CHAIN (list))
2490         if (supplicant == TREE_VALUE (list))
2491           return 1;
2492     }      
2493
2494   {
2495     tree context = declp ? DECL_CLASS_CONTEXT (supplicant)
2496                          : DECL_CONTEXT (TYPE_NAME (supplicant));
2497
2498     if (context)
2499       return is_friend (type, context);
2500   }
2501
2502   return 0;
2503 }
2504
2505 /* Add a new friend to the friends of the aggregate type TYPE.
2506    DECL is the FUNCTION_DECL of the friend being added.  */
2507 static void
2508 add_friend (type, decl)
2509      tree type, decl;
2510 {
2511   tree typedecl = TYPE_NAME (type);
2512   tree list = DECL_FRIENDLIST (typedecl);
2513   tree name = DECL_NAME (decl);
2514
2515   while (list)
2516     {
2517       if (name == TREE_PURPOSE (list))
2518         {
2519           tree friends = TREE_VALUE (list);
2520           for (; friends ; friends = TREE_CHAIN (friends))
2521             {
2522               if (decl == TREE_VALUE (friends))
2523                 {
2524                   cp_pedwarn ("`%D' is already a friend of class `%T'",
2525                               decl, type);
2526                   cp_pedwarn_at ("previous friend declaration of `%D'",
2527                                  TREE_VALUE (friends));
2528                   return;
2529                 }
2530             }
2531           TREE_VALUE (list) = tree_cons (error_mark_node, decl,
2532                                          TREE_VALUE (list));
2533           return;
2534         }
2535       list = TREE_CHAIN (list);
2536     }
2537   DECL_FRIENDLIST (typedecl)
2538     = tree_cons (DECL_NAME (decl), build_tree_list (error_mark_node, decl),
2539                  DECL_FRIENDLIST (typedecl));
2540   if (DECL_NAME (decl) == ansi_opname[(int) MODIFY_EXPR])
2541     {
2542       tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
2543       TYPE_HAS_ASSIGNMENT (TREE_TYPE (typedecl)) = 1;
2544       if (parmtypes && TREE_CHAIN (parmtypes))
2545         {
2546           tree parmtype = TREE_VALUE (TREE_CHAIN (parmtypes));
2547           if (TREE_CODE (parmtype) == REFERENCE_TYPE
2548               && TREE_TYPE (parmtypes) == TREE_TYPE (typedecl))
2549             TYPE_HAS_ASSIGN_REF (TREE_TYPE (typedecl)) = 1;
2550         }
2551     }
2552 }
2553
2554 /* Declare that every member function NAME in FRIEND_TYPE
2555    (which may be NULL_TREE) is a friend of type TYPE.  */
2556 static void
2557 add_friends (type, name, friend_type)
2558      tree type, name, friend_type;
2559 {
2560   tree typedecl = TYPE_NAME (type);
2561   tree list = DECL_FRIENDLIST (typedecl);
2562
2563   while (list)
2564     {
2565       if (name == TREE_PURPOSE (list))
2566         {
2567           tree friends = TREE_VALUE (list);
2568           while (friends && TREE_PURPOSE (friends) != friend_type)
2569             friends = TREE_CHAIN (friends);
2570           if (friends)
2571             if (friend_type)
2572               warning ("method `%s::%s' is already a friend of class",
2573                        TYPE_NAME_STRING (friend_type),
2574                        IDENTIFIER_POINTER (name));
2575             else
2576               warning ("function `%s' is already a friend of class `%s'",
2577                        IDENTIFIER_POINTER (name),
2578                        IDENTIFIER_POINTER (DECL_NAME (typedecl)));
2579           else
2580             TREE_VALUE (list) = tree_cons (friend_type, NULL_TREE,
2581                                            TREE_VALUE (list));
2582           return;
2583         }
2584       list = TREE_CHAIN (list);
2585     }
2586   DECL_FRIENDLIST (typedecl) =
2587     tree_cons (name,
2588                build_tree_list (friend_type, NULL_TREE),
2589                DECL_FRIENDLIST (typedecl));
2590   if (! strncmp (IDENTIFIER_POINTER (name),
2591                  IDENTIFIER_POINTER (ansi_opname[(int) MODIFY_EXPR]),
2592                  strlen (IDENTIFIER_POINTER (ansi_opname[(int) MODIFY_EXPR]))))
2593     {
2594       TYPE_HAS_ASSIGNMENT (TREE_TYPE (typedecl)) = 1;
2595       sorry ("declaring \"friend operator =\" will not find \"operator = (X&)\" if it exists");
2596     }
2597 }
2598
2599 /* Set up a cross reference so that type TYPE will make member function
2600    CTYPE::DECL a friend when CTYPE is finally defined.  For more than
2601    one, set up a cross reference so that functions with the name DECL
2602    and type CTYPE know that they are friends of TYPE.  */
2603 static void
2604 xref_friend (type, decl, ctype)
2605      tree type, decl, ctype;
2606 {
2607   tree friend_decl = TYPE_NAME (ctype);
2608 #if 0
2609   tree typedecl = TYPE_NAME (type);
2610   tree t = tree_cons (NULL_TREE, ctype, DECL_UNDEFINED_FRIENDS (typedecl));
2611
2612   DECL_UNDEFINED_FRIENDS (typedecl) = t;
2613 #else
2614   tree t = 0;
2615 #endif
2616   SET_DECL_WAITING_FRIENDS (friend_decl,
2617                             tree_cons (type, t,
2618                                        DECL_WAITING_FRIENDS (friend_decl)));
2619   TREE_TYPE (DECL_WAITING_FRIENDS (friend_decl)) = decl;
2620 }
2621
2622 /* Make FRIEND_TYPE a friend class to TYPE.  If FRIEND_TYPE has already
2623    been defined, we make all of its member functions friends of
2624    TYPE.  If not, we make it a pending friend, which can later be added
2625    when its definition is seen.  If a type is defined, then its TYPE_DECL's
2626    DECL_UNDEFINED_FRIENDS contains a (possibly empty) list of friend
2627    classes that are not defined.  If a type has not yet been defined,
2628    then the DECL_WAITING_FRIENDS contains a list of types
2629    waiting to make it their friend.  Note that these two can both
2630    be in use at the same time!  */
2631 void
2632 make_friend_class (type, friend_type)
2633      tree type, friend_type;
2634 {
2635   tree classes;
2636
2637   if (IS_SIGNATURE (type))
2638     {
2639       error ("`friend' declaration in signature definition");
2640       return;
2641     }
2642   if (IS_SIGNATURE (friend_type))
2643     {
2644       error ("signature type `%s' declared `friend'",
2645              IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (friend_type))));
2646       return;
2647     }
2648   if (type == friend_type)
2649     {
2650       warning ("class `%s' is implicitly friends with itself",
2651                TYPE_NAME_STRING (type));
2652       return;
2653     }
2654
2655   GNU_xref_hier (TYPE_NAME_STRING (type),
2656                  TYPE_NAME_STRING (friend_type), 0, 0, 1);
2657
2658   classes = CLASSTYPE_FRIEND_CLASSES (type);
2659   while (classes && TREE_VALUE (classes) != friend_type)
2660     classes = TREE_CHAIN (classes);
2661   if (classes)
2662     warning ("class `%s' is already friends with class `%s'",
2663              TYPE_NAME_STRING (TREE_VALUE (classes)), TYPE_NAME_STRING (type));
2664   else
2665     {
2666       CLASSTYPE_FRIEND_CLASSES (type)
2667         = tree_cons (NULL_TREE, friend_type, CLASSTYPE_FRIEND_CLASSES (type));
2668     }
2669 }
2670
2671 /* Main friend processor.  This is large, and for modularity purposes,
2672    has been removed from grokdeclarator.  It returns `void_type_node'
2673    to indicate that something happened, though a FIELD_DECL is
2674    not returned.
2675
2676    CTYPE is the class this friend belongs to.
2677
2678    DECLARATOR is the name of the friend.
2679
2680    DECL is the FUNCTION_DECL that the friend is.
2681
2682    In case we are parsing a friend which is part of an inline
2683    definition, we will need to store PARM_DECL chain that comes
2684    with it into the DECL_ARGUMENTS slot of the FUNCTION_DECL.
2685
2686    FLAGS is just used for `grokclassfn'.
2687
2688    QUALS say what special qualifies should apply to the object
2689    pointed to by `this'.  */
2690 tree
2691 do_friend (ctype, declarator, decl, parmdecls, flags, quals)
2692      tree ctype, declarator, decl, parmdecls;
2693      enum overload_flags flags;
2694      tree quals;
2695 {
2696   /* first, lets find out if what we are making a friend needs overloading */
2697   tree previous_decl;
2698   int was_c_linkage = 0;
2699
2700   /* Every decl that gets here is a friend of something.  */
2701   DECL_FRIEND_P (decl) = 1;
2702
2703   /* If we find something in scope, let see if it has extern "C" linkage.  */
2704   /* This code is pretty general and should be ripped out and reused
2705      as a separate function. */
2706   if (DECL_NAME (decl))
2707     {
2708       previous_decl = lookup_name (DECL_NAME (decl), 0);
2709       if (previous_decl && TREE_CODE (previous_decl) == TREE_LIST)
2710         {
2711           do
2712             {
2713               if (TREE_TYPE (TREE_VALUE (previous_decl)) == TREE_TYPE (decl))
2714                 {
2715                   previous_decl = TREE_VALUE (previous_decl);
2716                   break;
2717                 }
2718               previous_decl = TREE_CHAIN (previous_decl);
2719             }
2720           while (previous_decl);
2721         }
2722
2723       /* It had extern "C" linkage, so don't overload this.  */
2724       if (previous_decl && TREE_CODE (previous_decl) == FUNCTION_DECL
2725           && TREE_TYPE (decl) == TREE_TYPE (previous_decl)
2726           && DECL_LANGUAGE (previous_decl) == lang_c)
2727         was_c_linkage = 1;
2728     }
2729           
2730   if (ctype)
2731     {
2732       tree cname = TYPE_NAME (ctype);
2733       if (TREE_CODE (cname) == TYPE_DECL)
2734         cname = DECL_NAME (cname);
2735
2736       /* A method friend.  */
2737       if (TREE_CODE (decl) == FUNCTION_DECL)
2738         {
2739           if (flags == NO_SPECIAL && ctype && declarator == cname)
2740             DECL_CONSTRUCTOR_P (decl) = 1;
2741
2742           /* This will set up DECL_ARGUMENTS for us.  */
2743           grokclassfn (ctype, cname, decl, flags, quals);
2744           if (TYPE_SIZE (ctype) != 0)
2745             check_classfn (ctype, cname, decl);
2746
2747           if (TREE_TYPE (decl) != error_mark_node)
2748             {
2749               if (TYPE_SIZE (ctype))
2750                 {
2751                   /* We don't call pushdecl here yet, or ever on this
2752                      actual FUNCTION_DECL.  We must preserve its TREE_CHAIN
2753                      until the end.  */
2754                   make_decl_rtl (decl, NULL_PTR, 1);
2755                   add_friend (current_class_type, decl);
2756                 }
2757               else
2758                 {
2759                   register char *classname
2760                     = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (ctype)));
2761
2762                   error ("member declared as friend before type `%s' defined",
2763                          classname);
2764                 }
2765             }
2766         }
2767       else
2768         {
2769           /* Possibly a bunch of method friends.  */
2770
2771           /* Get the class they belong to.  */
2772           tree ctype = IDENTIFIER_TYPE_VALUE (cname);
2773
2774           /* This class is defined, use its methods now.  */
2775           if (TYPE_SIZE (ctype))
2776             {
2777               tree fields = lookup_fnfields (TYPE_BINFO (ctype), declarator, 0);
2778               if (fields)
2779                 add_friends (current_class_type, declarator, ctype);
2780               else
2781                 error ("method `%s' is not a member of class `%s'",
2782                        IDENTIFIER_POINTER (declarator),
2783                        IDENTIFIER_POINTER (cname));
2784             }
2785           else
2786             /* Note: DECLARATOR actually has more than one; in this
2787                case, we're making sure that fns with the name DECLARATOR
2788                and type CTYPE know they are friends of the current
2789                class type.  */
2790             xref_friend (current_class_type, declarator, ctype);
2791           decl = void_type_node;
2792         }
2793     }
2794   /* never overload C functions */
2795   else if (TREE_CODE (decl) == FUNCTION_DECL
2796            && ((IDENTIFIER_LENGTH (declarator) == 4
2797                 && IDENTIFIER_POINTER (declarator)[0] == 'm'
2798                 && ! strcmp (IDENTIFIER_POINTER (declarator), "main"))
2799                || (IDENTIFIER_LENGTH (declarator) > 10
2800                    && IDENTIFIER_POINTER (declarator)[0] == '_'
2801                    && IDENTIFIER_POINTER (declarator)[1] == '_'
2802                    && strncmp (IDENTIFIER_POINTER (declarator)+2,
2803                                "builtin_", 8) == 0)
2804                || was_c_linkage))
2805     {
2806       /* raw "main", and builtin functions never gets overloaded,
2807          but they can become friends.  */
2808       TREE_PUBLIC (decl) = 1;
2809       add_friend (current_class_type, decl);
2810       DECL_FRIEND_P (decl) = 1;
2811       if (IDENTIFIER_POINTER (declarator)[0] == '_')
2812         {
2813           if (! strcmp (IDENTIFIER_POINTER (declarator)+10, "new"))
2814             TREE_GETS_NEW (current_class_type) = 0;
2815           else if (! strcmp (IDENTIFIER_POINTER (declarator)+10, "delete"))
2816             TREE_GETS_DELETE (current_class_type) = 0;
2817         }
2818       decl = void_type_node;
2819     }
2820   /* A global friend.
2821      @@ or possibly a friend from a base class ?!?  */
2822   else if (TREE_CODE (decl) == FUNCTION_DECL)
2823     {
2824       /* Friends must all go through the overload machinery,
2825          even though they may not technically be overloaded.
2826
2827          Note that because classes all wind up being top-level
2828          in their scope, their friend wind up in top-level scope as well.  */
2829       DECL_ASSEMBLER_NAME (decl)
2830         = build_decl_overload (declarator, TYPE_ARG_TYPES (TREE_TYPE (decl)),
2831                                TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE);
2832       DECL_ARGUMENTS (decl) = parmdecls;
2833       DECL_CLASS_CONTEXT (decl) = current_class_type;
2834
2835       /* We can call pushdecl here, because the TREE_CHAIN of this
2836          FUNCTION_DECL is not needed for other purposes.  */
2837       decl = pushdecl_top_level (decl);
2838
2839       make_decl_rtl (decl, NULL_PTR, 1);
2840       add_friend (current_class_type, decl);
2841
2842       DECL_FRIEND_P (decl) = 1;
2843       TREE_OVERLOADED (declarator) = 1;
2844     }
2845   else
2846     {
2847       /* @@ Should be able to ingest later definitions of this function
2848          before use.  */
2849       tree decl = IDENTIFIER_GLOBAL_VALUE (declarator);
2850       if (decl == NULL_TREE)
2851         {
2852           warning ("implicitly declaring `%s' as struct",
2853                    IDENTIFIER_POINTER (declarator));
2854           decl = xref_tag (record_type_node, declarator, NULL_TREE, 1);
2855           decl = TYPE_NAME (decl);
2856         }
2857
2858       /* Allow abbreviated declarations of overloaded functions,
2859          but not if those functions are really class names.  */
2860       if (TREE_CODE (decl) == TREE_LIST && TREE_TYPE (TREE_PURPOSE (decl)))
2861         {
2862           warning ("`friend %s' archaic, use `friend class %s' instead",
2863                    IDENTIFIER_POINTER (declarator),
2864                    IDENTIFIER_POINTER (declarator));
2865           decl = TREE_TYPE (TREE_PURPOSE (decl));
2866         }
2867
2868       if (TREE_CODE (decl) == TREE_LIST)
2869         add_friends (current_class_type, TREE_PURPOSE (decl), NULL_TREE);
2870       else
2871         make_friend_class (current_class_type, TREE_TYPE (decl));
2872       decl = void_type_node;
2873     }
2874   return decl;
2875 }
2876
2877 /* TYPE has now been defined.  It may, however, have a number of things
2878    waiting make make it their friend.  We resolve these references
2879    here.  */
2880 void
2881 embrace_waiting_friends (type)
2882      tree type;
2883 {
2884   tree decl = TYPE_NAME (type);
2885   tree waiters;
2886
2887   if (TREE_CODE (decl) != TYPE_DECL)
2888     return;
2889
2890   for (waiters = DECL_WAITING_FRIENDS (decl); waiters;
2891        waiters = TREE_CHAIN (waiters))
2892     {
2893       tree waiter = TREE_PURPOSE (waiters);
2894 #if 0
2895       tree waiter_prev = TREE_VALUE (waiters);
2896 #endif
2897       tree decl = TREE_TYPE (waiters);
2898       tree name = decl ? (TREE_CODE (decl) == IDENTIFIER_NODE
2899                           ? decl : DECL_NAME (decl)) : NULL_TREE;
2900       if (name)
2901         {
2902           /* @@ There may be work to be done since we have not verified
2903              @@ consistency between original and friend declarations
2904              @@ of the functions waiting to become friends.  */
2905           tree field = lookup_fnfields (TYPE_BINFO (type), name, 0);
2906           if (field)
2907             if (decl == name)
2908               add_friends (waiter, name, type);
2909             else
2910               add_friend (waiter, decl);
2911           else
2912             error_with_file_and_line (DECL_SOURCE_FILE (TYPE_NAME (waiter)),
2913                                       DECL_SOURCE_LINE (TYPE_NAME (waiter)),
2914                                       "no method `%s' defined in class `%s' to be friend",
2915                                       IDENTIFIER_POINTER (DECL_NAME (TREE_TYPE (waiters))),
2916                                       TYPE_NAME_STRING (type));
2917         }
2918       else
2919         make_friend_class (type, waiter);
2920
2921 #if 0
2922       if (TREE_CHAIN (waiter_prev))
2923         TREE_CHAIN (waiter_prev) = TREE_CHAIN (TREE_CHAIN (waiter_prev));
2924       else
2925         DECL_UNDEFINED_FRIENDS (TYPE_NAME (waiter)) = NULL_TREE;
2926 #endif
2927     }
2928 }
2929 \f
2930 /* Common subroutines of build_new and build_vec_delete.  */
2931
2932 /* Common interface for calling "builtin" functions that are not
2933    really builtin.  */
2934
2935 tree
2936 build_builtin_call (type, node, arglist)
2937      tree type;
2938      tree node;
2939      tree arglist;
2940 {
2941   tree rval = build (CALL_EXPR, type, node, arglist, 0);
2942   TREE_SIDE_EFFECTS (rval) = 1;
2943   assemble_external (TREE_OPERAND (node, 0));
2944   TREE_USED (TREE_OPERAND (node, 0)) = 1;
2945   return rval;
2946 }
2947 \f
2948 /* Generate a C++ "new" expression. DECL is either a TREE_LIST
2949    (which needs to go through some sort of groktypename) or it
2950    is the name of the class we are newing. INIT is an initialization value.
2951    It is either an EXPRLIST, an EXPR_NO_COMMAS, or something in braces.
2952    If INIT is void_type_node, it means do *not* call a constructor
2953    for this instance.
2954
2955    For types with constructors, the data returned is initialized
2956    by the appropriate constructor.
2957
2958    Whether the type has a constructor or not, if it has a pointer
2959    to a virtual function table, then that pointer is set up
2960    here.
2961
2962    Unless I am mistaken, a call to new () will return initialized
2963    data regardless of whether the constructor itself is private or
2964    not.  NOPE; new fails if the constructor is private (jcm).
2965
2966    Note that build_new does nothing to assure that any special
2967    alignment requirements of the type are met.  Rather, it leaves
2968    it up to malloc to do the right thing.  Otherwise, folding to
2969    the right alignment cal cause problems if the user tries to later
2970    free the memory returned by `new'.
2971
2972    PLACEMENT is the `placement' list for user-defined operator new ().  */
2973
2974 tree
2975 build_new (placement, decl, init, use_global_new)
2976      tree placement;
2977      tree decl, init;
2978      int use_global_new;
2979 {
2980   tree type, true_type, size, rval;
2981   tree nelts;
2982   int has_array = 0;
2983
2984   tree pending_sizes = NULL_TREE;
2985
2986   if (decl == error_mark_node)
2987     return error_mark_node;
2988
2989   if (TREE_CODE (decl) == TREE_LIST)
2990     {
2991       tree absdcl = TREE_VALUE (decl);
2992       tree last_absdcl = NULL_TREE;
2993       int old_immediate_size_expand;
2994
2995       if (current_function_decl
2996           && DECL_CONSTRUCTOR_P (current_function_decl))
2997         {
2998           old_immediate_size_expand = immediate_size_expand;
2999           immediate_size_expand = 0;
3000         }
3001
3002       nelts = integer_one_node;
3003
3004       if (absdcl && TREE_CODE (absdcl) == CALL_EXPR)
3005         my_friendly_abort (215);
3006       while (absdcl && TREE_CODE (absdcl) == INDIRECT_REF)
3007         {
3008           last_absdcl = absdcl;
3009           absdcl = TREE_OPERAND (absdcl, 0);
3010         }
3011
3012       if (absdcl && TREE_CODE (absdcl) == ARRAY_REF)
3013         {
3014           /* probably meant to be a vec new */
3015           tree this_nelts;
3016
3017           while (TREE_OPERAND (absdcl, 0)
3018                  && TREE_CODE (TREE_OPERAND (absdcl, 0)) == ARRAY_REF)
3019             {
3020               last_absdcl = absdcl;
3021               absdcl = TREE_OPERAND (absdcl, 0);
3022             }
3023
3024           has_array = 1;
3025           this_nelts = TREE_OPERAND (absdcl, 1);
3026           if (this_nelts != error_mark_node)
3027             {
3028               if (this_nelts == NULL_TREE)
3029                 error ("new of array type fails to specify size");
3030               else
3031                 {
3032                   this_nelts = save_expr (convert (sizetype, this_nelts));
3033                   absdcl = TREE_OPERAND (absdcl, 0);
3034                   if (this_nelts == integer_zero_node)
3035                     {
3036                       warning ("zero size array reserves no space");
3037                       nelts = integer_zero_node;
3038                     }
3039                   else
3040                     nelts = build_binary_op (MULT_EXPR, nelts, this_nelts, 1);
3041                 }
3042             }
3043           else
3044             nelts = integer_zero_node;
3045         }
3046
3047       if (last_absdcl)
3048         TREE_OPERAND (last_absdcl, 0) = absdcl;
3049       else
3050         TREE_VALUE (decl) = absdcl;
3051
3052       type = true_type = groktypename (decl);
3053       if (! type || type == error_mark_node)
3054         {
3055           immediate_size_expand = old_immediate_size_expand;
3056           return error_mark_node;
3057         }
3058
3059       if (current_function_decl
3060           && DECL_CONSTRUCTOR_P (current_function_decl))
3061         {
3062           pending_sizes = get_pending_sizes ();
3063           immediate_size_expand = old_immediate_size_expand;
3064         }
3065     }
3066   else if (TREE_CODE (decl) == IDENTIFIER_NODE)
3067     {
3068       if (IDENTIFIER_HAS_TYPE_VALUE (decl))
3069         {
3070           /* An aggregate type.  */
3071           type = IDENTIFIER_TYPE_VALUE (decl);
3072           decl = TYPE_NAME (type);
3073         }
3074       else
3075         {
3076           /* A builtin type.  */
3077           decl = lookup_name (decl, 1);
3078           my_friendly_assert (TREE_CODE (decl) == TYPE_DECL, 215);
3079           type = TREE_TYPE (decl);
3080         }
3081       true_type = type;
3082     }
3083   else if (TREE_CODE (decl) == TYPE_DECL)
3084     {
3085       type = TREE_TYPE (decl);
3086       true_type = type;
3087     }
3088   else
3089     {
3090       type = decl;
3091       true_type = type;
3092       decl = TYPE_NAME (type);
3093     }
3094
3095   /* ``A reference cannot be created by the new operator.  A reference
3096      is not an object (8.2.2, 8.4.3), so a pointer to it could not be
3097      returned by new.'' ARM 5.3.3 */
3098   if (TREE_CODE (type) == REFERENCE_TYPE)
3099     {
3100       error ("new cannot be applied to a reference type");
3101       type = true_type = TREE_TYPE (type);
3102     }
3103
3104   /* When the object being created is an array, the new-expression yields a
3105      pointer to the initial element (if any) of the array.  For example,
3106      both new int and new int[10] return an int*.  5.3.4.  */
3107   if (TREE_CODE (type) == ARRAY_TYPE && has_array == 0)
3108     {
3109       nelts = array_type_nelts_top (type);
3110       has_array = 1;
3111       type = true_type = TREE_TYPE (type);
3112     }
3113
3114   if (TYPE_READONLY (type) || TYPE_VOLATILE (type))
3115     {
3116       pedwarn ("const and volatile types cannot be created with operator new");
3117       type = true_type = TYPE_MAIN_VARIANT (type);
3118     }
3119   
3120   /* If our base type is an array, then make sure we know how many elements
3121      it has.  */
3122   while (TREE_CODE (true_type) == ARRAY_TYPE)
3123     {
3124       tree this_nelts = array_type_nelts_top (true_type);
3125       nelts = build_binary_op (MULT_EXPR, nelts, this_nelts, 1);
3126       true_type = TREE_TYPE (true_type);
3127     }
3128   if (has_array)
3129     size = fold (build_binary_op (MULT_EXPR, size_in_bytes (true_type),
3130                                   nelts, 1));
3131   else
3132     size = size_in_bytes (type);
3133
3134   if (TYPE_SIZE (true_type) == 0)
3135     {
3136       if (true_type == void_type_node)
3137         error ("invalid type for new: `void'");
3138       else
3139         incomplete_type_error (0, true_type);
3140       return error_mark_node;
3141     }
3142
3143   if (TYPE_LANG_SPECIFIC (true_type)
3144       && CLASSTYPE_ABSTRACT_VIRTUALS (true_type))
3145     {
3146       abstract_virtuals_error (NULL_TREE, true_type);
3147       return error_mark_node;
3148     }
3149
3150   if (TYPE_LANG_SPECIFIC (true_type) && IS_SIGNATURE (true_type))
3151     {
3152       signature_error (NULL_TREE, true_type);
3153       return error_mark_node;
3154     }
3155
3156   /* Get a little extra space to store a couple of things before the new'ed
3157      array. */
3158   if (has_array && TYPE_NEEDS_DESTRUCTOR (true_type))
3159     {
3160       tree extra = BI_header_size;
3161
3162       size = size_binop (PLUS_EXPR, size, extra);
3163     }
3164
3165   /* Allocate the object. */
3166   if (TYPE_LANG_SPECIFIC (true_type)
3167       && (TREE_GETS_NEW (true_type) || TREE_GETS_PLACED_NEW (true_type))
3168       && !use_global_new
3169       && !has_array)
3170     rval = build_opfncall (NEW_EXPR, LOOKUP_NORMAL,
3171                            TYPE_POINTER_TO (true_type), size, placement);
3172   else if (placement)
3173     {
3174       rval = build_opfncall (NEW_EXPR, LOOKUP_GLOBAL|LOOKUP_COMPLAIN,
3175                              ptr_type_node, size, placement);
3176       rval = convert (TYPE_POINTER_TO (true_type), rval);
3177     }
3178   else if (flag_this_is_variable > 0
3179            && TYPE_HAS_CONSTRUCTOR (true_type) && init != void_type_node)
3180     {
3181       if (init == NULL_TREE || TREE_CODE (init) == TREE_LIST)
3182         rval = NULL_TREE;
3183       else
3184         {
3185           error ("constructors take parameter lists");
3186           return error_mark_node;
3187         }
3188     }
3189   else
3190     {
3191       rval = build_builtin_call (build_pointer_type (true_type),
3192                                  BIN, build_tree_list (NULL_TREE, size));
3193 #if 0
3194       /* See comment above as to why this is disabled.  */
3195       if (alignment)
3196         {
3197           rval = build (PLUS_EXPR, TYPE_POINTER_TO (true_type), rval,
3198                         alignment);
3199           rval = build (BIT_AND_EXPR, TYPE_POINTER_TO (true_type),
3200                         rval, build1 (BIT_NOT_EXPR, integer_type_node,
3201                                       alignment));
3202         }
3203 #endif
3204       TREE_CALLS_NEW (rval) = 1;
3205       TREE_SIDE_EFFECTS (rval) = 1;
3206     }
3207
3208   /* if rval is NULL_TREE I don't have to allocate it, but are we totally
3209      sure we have some extra bytes in that case for the BI_header_size
3210      cookies? And how does that interact with the code below? (mrs) */
3211   /* Finish up some magic for new'ed arrays */
3212   if (has_array && TYPE_NEEDS_DESTRUCTOR (true_type) && rval != NULL_TREE)
3213     {
3214       tree extra = BI_header_size;
3215       tree cookie, exp1;
3216       rval = convert (ptr_type_node, rval);    /* convert to void * first */
3217       rval = convert (string_type_node, rval); /* lets not add void* and ints */
3218       rval = save_expr (build_binary_op (PLUS_EXPR, rval, extra, 1));
3219       /* Store header info.  */
3220       cookie = build_indirect_ref (build (MINUS_EXPR, TYPE_POINTER_TO (BI_header_type),
3221                                           rval, extra), NULL_PTR);
3222       exp1 = build (MODIFY_EXPR, void_type_node,
3223                     build_component_ref (cookie, nc_nelts_field_id, 0, 0),
3224                     nelts);
3225       TREE_SIDE_EFFECTS (exp1) = 1;
3226       rval = convert (build_pointer_type (true_type), rval);
3227       TREE_CALLS_NEW (rval) = 1;
3228       TREE_SIDE_EFFECTS (rval) = 1;
3229       rval = build_compound_expr (tree_cons (NULL_TREE, exp1,
3230                                              build_tree_list (NULL_TREE, rval)));
3231     }
3232
3233   /* We've figured out where the allocation is to go.
3234      If we're not eliding constructors, then if a constructor
3235      is defined, we must go through it.  */
3236   if (!has_array && (rval == NULL_TREE || !flag_elide_constructors)
3237       && TYPE_HAS_CONSTRUCTOR (true_type) && init != void_type_node)
3238     {
3239       tree newrval;
3240       /* Constructors are never virtual. If it has an initialization, we
3241          need to complain if we aren't allowed to use the ctor that took
3242          that argument.  */
3243       int flags = LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_COMPLAIN;
3244
3245       /* If a copy constructor might work, set things up so that we can
3246          try that after this.  We deliberately don't clear LOOKUP_COMPLAIN
3247          any more, since that would make it impossible to rationally use
3248          the access of a constructor that matches perfectly.  */
3249 #if 0
3250       if (rval != NULL_TREE)
3251         flags |= LOOKUP_SPECULATIVELY;
3252 #endif
3253
3254       if (rval && TYPE_USES_VIRTUAL_BASECLASSES (true_type))
3255         {
3256           init = tree_cons (NULL_TREE, integer_one_node, init);
3257           flags |= LOOKUP_HAS_IN_CHARGE;
3258         }
3259
3260       {
3261         tree tmp = rval;
3262         
3263         if (TREE_CODE (TREE_TYPE (tmp)) == POINTER_TYPE)
3264           tmp = build_indirect_ref (tmp, NULL_PTR);
3265       
3266         newrval = build_method_call (tmp, constructor_name_full (true_type),
3267                                      init, NULL_TREE, flags);
3268       }
3269       
3270       if (newrval)
3271         {
3272           rval = newrval;
3273           TREE_HAS_CONSTRUCTOR (rval) = 1;
3274         }
3275       else
3276         rval = error_mark_node;
3277       goto done;
3278     }
3279
3280   if (rval == error_mark_node)
3281     return error_mark_node;
3282   rval = save_expr (rval);
3283   TREE_HAS_CONSTRUCTOR (rval) = 1;
3284
3285   /* Don't call any constructors or do any initialization.  */
3286   if (init == void_type_node)
3287     goto done;
3288
3289   if (TYPE_NEEDS_CONSTRUCTING (type) || init)
3290     {
3291       if (! TYPE_NEEDS_CONSTRUCTING (type) && ! IS_AGGR_TYPE (type))
3292         {
3293           /* New 2.0 interpretation: `new int (10)' means
3294              allocate an int, and initialize it with 10.  */
3295
3296           init = build_c_cast (type, init);
3297           rval = build (COMPOUND_EXPR, TREE_TYPE (rval),
3298                         build_modify_expr (build_indirect_ref (rval, NULL_PTR),
3299                                            NOP_EXPR, init),
3300                         rval);
3301           TREE_SIDE_EFFECTS (rval) = 1;
3302           TREE_CALLS_NEW (rval) = 1;
3303         }
3304       else if (current_function_decl == NULL_TREE)
3305         {
3306           extern tree static_aggregates;
3307
3308           /* In case of static initialization, SAVE_EXPR is good enough.  */
3309           init = copy_to_permanent (init);
3310           rval = copy_to_permanent (rval);
3311           static_aggregates = perm_tree_cons (init, rval, static_aggregates);
3312         }
3313       else
3314         {
3315           /* Have to wrap this in RTL_EXPR for two cases:
3316              in base or member initialization and if we
3317              are a branch of a ?: operator.  Since we
3318              can't easily know the latter, just do it always.  */
3319           tree xval = make_node (RTL_EXPR);
3320
3321           TREE_TYPE (xval) = TREE_TYPE (rval);
3322           do_pending_stack_adjust ();
3323           start_sequence_for_rtl_expr (xval);
3324
3325           /* As a matter of principle, `start_sequence' should do this.  */
3326           emit_note (0, -1);
3327
3328           if (has_array)
3329             rval = expand_vec_init (decl, rval,
3330                                     build_binary_op (MINUS_EXPR, nelts, integer_one_node, 1),
3331                                     init, 0);
3332           else
3333             expand_aggr_init (build_indirect_ref (rval, NULL_PTR), init, 0);
3334
3335           do_pending_stack_adjust ();
3336
3337           TREE_SIDE_EFFECTS (xval) = 1;
3338           TREE_CALLS_NEW (xval) = 1;
3339           RTL_EXPR_SEQUENCE (xval) = get_insns ();
3340           end_sequence ();
3341
3342           if (TREE_CODE (rval) == SAVE_EXPR)
3343             {
3344               /* Errors may cause this to not get evaluated.  */
3345               if (SAVE_EXPR_RTL (rval) == 0)
3346                 SAVE_EXPR_RTL (rval) = const0_rtx;
3347               RTL_EXPR_RTL (xval) = SAVE_EXPR_RTL (rval);
3348             }
3349           else
3350             {
3351               my_friendly_assert (TREE_CODE (rval) == VAR_DECL, 217);
3352               RTL_EXPR_RTL (xval) = DECL_RTL (rval);
3353             }
3354           rval = xval;
3355         }
3356     }
3357  done:
3358   if (rval && TREE_TYPE (rval) != build_pointer_type (type))
3359     {
3360       /* The type of new int [3][3] is not int *, but int [3] * */
3361       rval = build_c_cast (build_pointer_type (type), rval);
3362     }
3363
3364   if (pending_sizes)
3365     rval = build_compound_expr (chainon (pending_sizes,
3366                                          build_tree_list (NULL_TREE, rval)));
3367
3368   if (flag_gc)
3369     {
3370       extern tree gc_visible;
3371       tree objbits;
3372       tree update_expr;
3373
3374       rval = save_expr (rval);
3375       /* We don't need a `headof' operation to do this because
3376          we know where the object starts.  */
3377       objbits = build1 (INDIRECT_REF, unsigned_type_node,
3378                         build (MINUS_EXPR, ptr_type_node,
3379                                rval, c_sizeof_nowarn (unsigned_type_node)));
3380       update_expr = build_modify_expr (objbits, BIT_IOR_EXPR, gc_visible);
3381       rval = build_compound_expr (tree_cons (NULL_TREE, rval,
3382                                              tree_cons (NULL_TREE, update_expr,
3383                                                         build_tree_list (NULL_TREE, rval))));
3384     }
3385
3386   return save_expr (rval);
3387 }
3388 \f
3389 /* `expand_vec_init' performs initialization of a vector of aggregate
3390    types.
3391
3392    DECL is passed only for error reporting, and provides line number
3393    and source file name information.
3394    BASE is the space where the vector will be.
3395    MAXINDEX is the maximum index of the array (one less than the
3396             number of elements).
3397    INIT is the (possibly NULL) initializer.
3398
3399    FROM_ARRAY is 0 if we should init everything with INIT
3400    (i.e., every element initialized from INIT).
3401    FROM_ARRAY is 1 if we should index into INIT in parallel
3402    with initialization of DECL.
3403    FROM_ARRAY is 2 if we should index into INIT in parallel,
3404    but use assignment instead of initialization.  */
3405
3406 tree
3407 expand_vec_init (decl, base, maxindex, init, from_array)
3408      tree decl, base, maxindex, init;
3409      int from_array;
3410 {
3411   tree rval;
3412   tree iterator, base2 = NULL_TREE;
3413   tree type = TREE_TYPE (TREE_TYPE (base));
3414   tree size;
3415
3416   maxindex = convert (integer_type_node, maxindex);
3417   if (maxindex == error_mark_node)
3418     return error_mark_node;
3419
3420   if (current_function_decl == NULL_TREE)
3421     {
3422       rval = make_tree_vec (3);
3423       TREE_VEC_ELT (rval, 0) = base;
3424       TREE_VEC_ELT (rval, 1) = maxindex;
3425       TREE_VEC_ELT (rval, 2) = init;
3426       return rval;
3427     }
3428
3429   size = size_in_bytes (type);
3430
3431   /* Set to zero in case size is <= 0.  Optimizer will delete this if
3432      it is not needed.  */
3433   rval = get_temp_regvar (TYPE_POINTER_TO (type),
3434                           convert (TYPE_POINTER_TO (type), null_pointer_node));
3435   base = default_conversion (base);
3436   base = convert (TYPE_POINTER_TO (type), base);
3437   expand_assignment (rval, base, 0, 0);
3438   base = get_temp_regvar (TYPE_POINTER_TO (type), base);
3439
3440   if (init != NULL_TREE
3441       && TREE_CODE (init) == CONSTRUCTOR
3442       && TREE_TYPE (init) == TREE_TYPE (decl))
3443     {
3444       /* Initialization of array from {...}.  */
3445       tree elts = CONSTRUCTOR_ELTS (init);
3446       tree baseref = build1 (INDIRECT_REF, type, base);
3447       tree baseinc = build (PLUS_EXPR, TYPE_POINTER_TO (type), base, size);
3448       int host_i = TREE_INT_CST_LOW (maxindex);
3449
3450       if (IS_AGGR_TYPE (type))
3451         {
3452           while (elts)
3453             {
3454               host_i -= 1;
3455               expand_aggr_init (baseref, TREE_VALUE (elts), 0);
3456
3457               expand_assignment (base, baseinc, 0, 0);
3458               elts = TREE_CHAIN (elts);
3459             }
3460           /* Initialize any elements by default if possible.  */
3461           if (host_i >= 0)
3462             {
3463               if (TYPE_NEEDS_CONSTRUCTING (type) == 0)
3464                 {
3465                   if (obey_regdecls)
3466                     use_variable (DECL_RTL (base));
3467                   goto done_init;
3468                 }
3469
3470               iterator = get_temp_regvar (integer_type_node,
3471                                           build_int_2 (host_i, 0));
3472               init = NULL_TREE;
3473               goto init_by_default;
3474             }
3475         }
3476       else
3477         while (elts)
3478           {
3479             expand_assignment (baseref, TREE_VALUE (elts), 0, 0);
3480
3481             expand_assignment (base, baseinc, 0, 0);
3482             elts = TREE_CHAIN (elts);
3483           }
3484
3485       if (obey_regdecls)
3486         use_variable (DECL_RTL (base));
3487     }
3488   else
3489     {
3490       tree itype;
3491
3492       iterator = get_temp_regvar (integer_type_node, maxindex);
3493
3494     init_by_default:
3495
3496       /* If initializing one array from another,
3497          initialize element by element.  */
3498       if (from_array)
3499         {
3500           /* We rely upon the below calls the do argument checking */
3501           if (decl == NULL_TREE)
3502             {
3503               sorry ("initialization of array from dissimilar array type");
3504               return error_mark_node;
3505             }
3506           if (init)
3507             {
3508               base2 = default_conversion (init);
3509               itype = TREE_TYPE (base2);
3510               base2 = get_temp_regvar (itype, base2);
3511               itype = TREE_TYPE (itype);
3512             }
3513           else if (TYPE_LANG_SPECIFIC (type)
3514                    && TYPE_NEEDS_CONSTRUCTING (type)
3515                    && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
3516             {
3517               error ("initializer ends prematurely");
3518               return error_mark_node;
3519             }
3520         }
3521
3522       expand_start_cond (build (GE_EXPR, integer_type_node,
3523                                 iterator, integer_zero_node), 0);
3524       expand_start_loop_continue_elsewhere (1);
3525
3526       if (from_array)
3527         {
3528           tree to = build1 (INDIRECT_REF, type, base);
3529           tree from;
3530
3531           if (base2)
3532             from = build1 (INDIRECT_REF, itype, base2);
3533           else
3534             from = NULL_TREE;
3535
3536           if (from_array == 2)
3537             expand_expr_stmt (build_modify_expr (to, NOP_EXPR, from));
3538           else if (TYPE_NEEDS_CONSTRUCTING (type))
3539             expand_aggr_init (to, from, 0);
3540           else if (from)
3541             expand_assignment (to, from, 0, 0);
3542           else
3543             my_friendly_abort (57);
3544         }
3545       else if (TREE_CODE (type) == ARRAY_TYPE)
3546         {
3547           if (init != 0)
3548             sorry ("cannot initialize multi-dimensional array with initializer");
3549           expand_vec_init (decl, build1 (NOP_EXPR, TYPE_POINTER_TO (TREE_TYPE (type)), base),
3550                            array_type_nelts (type), 0, 0);
3551         }
3552       else
3553         expand_aggr_init (build1 (INDIRECT_REF, type, base), init, 0);
3554
3555       expand_assignment (base,
3556                          build (PLUS_EXPR, TYPE_POINTER_TO (type), base, size),
3557                          0, 0);
3558       if (base2)
3559         expand_assignment (base2,
3560                            build (PLUS_EXPR, TYPE_POINTER_TO (type), base2, size), 0, 0);
3561       expand_loop_continue_here ();
3562       expand_exit_loop_if_false (0, build (NE_EXPR, integer_type_node,
3563                                            build (PREDECREMENT_EXPR, integer_type_node, iterator, integer_one_node), minus_one));
3564
3565       if (obey_regdecls)
3566         {
3567           use_variable (DECL_RTL (base));
3568           if (base2)
3569             use_variable (DECL_RTL (base2));
3570         }
3571       expand_end_loop ();
3572       expand_end_cond ();
3573       if (obey_regdecls)
3574         use_variable (DECL_RTL (iterator));
3575     }
3576  done_init:
3577
3578   if (obey_regdecls)
3579     use_variable (DECL_RTL (rval));
3580   return rval;
3581 }
3582
3583 /* Free up storage of type TYPE, at address ADDR.
3584
3585    TYPE is a POINTER_TYPE and can be ptr_type_node for no special type
3586    of pointer.
3587
3588    VIRTUAL_SIZE is the amount of storage that was allocated, and is
3589    used as the second argument to operator delete.  It can include
3590    things like padding and magic size cookies.  It has virtual in it,
3591    because if you have a base pointer and you delete through a virtual
3592    destructor, it should be the size of the dynamic object, not the
3593    static object, see Free Store 12.5 ANSI C++ WP.
3594
3595    This does not call any destructors.  */
3596 tree
3597 build_x_delete (type, addr, use_global_delete, virtual_size)
3598      tree type, addr;
3599      int use_global_delete;
3600      tree virtual_size;
3601 {
3602   tree rval;
3603
3604   if (!use_global_delete
3605       && TYPE_LANG_SPECIFIC (TREE_TYPE (type))
3606       && TREE_GETS_DELETE (TREE_TYPE (type)))
3607     rval = build_opfncall (DELETE_EXPR, LOOKUP_NORMAL, addr,
3608                            virtual_size, NULL_TREE);
3609   else
3610     rval = build_builtin_call (void_type_node, BID,
3611                                build_tree_list (NULL_TREE, addr));
3612   return rval;
3613 }
3614
3615 /* Generate a call to a destructor. TYPE is the type to cast ADDR to.
3616    ADDR is an expression which yields the store to be destroyed.
3617    AUTO_DELETE is nonzero if a call to DELETE should be made or not.
3618    If in the program, (AUTO_DELETE & 2) is non-zero, we tear down the
3619    virtual baseclasses.
3620    If in the program, (AUTO_DELETE & 1) is non-zero, then we deallocate.
3621
3622    FLAGS is the logical disjunction of zero or more LOOKUP_
3623    flags.  See cp-tree.h for more info.
3624
3625    This function does not delete an object's virtual base classes.  */
3626 tree
3627 build_delete (type, addr, auto_delete, flags, use_global_delete)
3628      tree type, addr;
3629      tree auto_delete;
3630      int flags;
3631      int use_global_delete;
3632 {
3633   tree function, parms;
3634   tree member;
3635   tree expr;
3636   tree ref;
3637   int ptr;
3638
3639   if (addr == error_mark_node)
3640     return error_mark_node;
3641
3642   /* Can happen when CURRENT_EXCEPTION_OBJECT gets its type
3643      set to `error_mark_node' before it gets properly cleaned up.  */
3644   if (type == error_mark_node)
3645     return error_mark_node;
3646
3647   type = TYPE_MAIN_VARIANT (type);
3648
3649   if (TREE_CODE (type) == POINTER_TYPE)
3650     {
3651       type = TREE_TYPE (type);
3652       if (TYPE_SIZE (type) == 0)
3653         {
3654           incomplete_type_error (0, type);
3655           return error_mark_node;
3656         }
3657       if (TREE_CODE (type) == ARRAY_TYPE)
3658         goto handle_array;
3659       if (! IS_AGGR_TYPE (type))
3660         {
3661           /* Call the builtin operator delete.  */
3662           return build_builtin_call (void_type_node, BID,
3663                                      build_tree_list (NULL_TREE, addr));
3664         }
3665       if (TREE_SIDE_EFFECTS (addr))
3666         addr = save_expr (addr);
3667       ref = build_indirect_ref (addr, NULL_PTR);
3668       ptr = 1;
3669     }
3670   else if (TREE_CODE (type) == ARRAY_TYPE)
3671     {
3672     handle_array:
3673       if (TREE_SIDE_EFFECTS (addr))
3674         addr = save_expr (addr);
3675       return build_vec_delete (addr, array_type_nelts (type),
3676                                c_sizeof_nowarn (TREE_TYPE (type)),
3677                                NULL_TREE, auto_delete, integer_two_node);
3678     }
3679   else
3680     {
3681       /* Don't check PROTECT here; leave that decision to the
3682          destructor.  If the destructor is accessible, call it,
3683          else report error.  */
3684       addr = build_unary_op (ADDR_EXPR, addr, 0);
3685       if (TREE_SIDE_EFFECTS (addr))
3686         addr = save_expr (addr);
3687
3688       if (TREE_CONSTANT (addr))
3689         addr = convert_pointer_to (type, addr);
3690       else
3691         addr = convert_force (build_pointer_type (type), addr);
3692
3693       if (TREE_CODE (addr) == NOP_EXPR
3694           && TREE_OPERAND (addr, 0) == current_class_decl)
3695         ref = C_C_D;
3696       else
3697         ref = build_indirect_ref (addr, NULL_PTR);
3698       ptr = 0;
3699     }
3700
3701   my_friendly_assert (IS_AGGR_TYPE (type), 220);
3702
3703   if (! TYPE_NEEDS_DESTRUCTOR (type))
3704     {
3705       if (auto_delete == integer_zero_node)
3706         return void_zero_node;
3707
3708       /* Pass the size of the object down to the operator delete() in
3709          addition to the ADDR.  */
3710       if (TREE_GETS_DELETE (type) && !use_global_delete)
3711         {
3712           /* This is probably wrong. It should be the size of the virtual
3713              object being deleted.  */
3714           tree virtual_size = c_sizeof_nowarn (type);
3715           return build_opfncall (DELETE_EXPR, LOOKUP_NORMAL, addr,
3716                                  virtual_size, NULL_TREE);
3717         }
3718
3719       /* Call the builtin operator delete.  */
3720       return build_builtin_call (void_type_node, BID,
3721                                  build_tree_list (NULL_TREE, addr));
3722     }
3723   parms = build_tree_list (NULL_TREE, addr);
3724
3725   /* Below, we will reverse the order in which these calls are made.
3726      If we have a destructor, then that destructor will take care
3727      of the base classes; otherwise, we must do that here.  */
3728   if (TYPE_HAS_DESTRUCTOR (type))
3729     {
3730       tree dtor = DECL_MAIN_VARIANT (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), 0));
3731       tree basetypes = TYPE_BINFO (type);
3732
3733       if (flags & LOOKUP_PROTECT)
3734         {
3735           enum access_type access = compute_access (basetypes, dtor);
3736
3737           if (access == access_private)
3738             {
3739               if (flags & LOOKUP_COMPLAIN)
3740                 cp_error ("destructor for type `%T' is private in this scope", type);
3741               return error_mark_node;
3742             }
3743           else if (access == access_protected)
3744             {
3745               if (flags & LOOKUP_COMPLAIN)
3746                 cp_error ("destructor for type `%T' is protected in this scope", type);
3747               return error_mark_node;
3748             }
3749         }
3750
3751       /* Once we are in a destructor, try not going through
3752          the virtual function table to find the next destructor.  */
3753       if (DECL_VINDEX (dtor)
3754           && ! (flags & LOOKUP_NONVIRTUAL)
3755           && TREE_CODE (auto_delete) != PARM_DECL
3756           && (ptr == 1 || ! resolves_to_fixed_type_p (ref, 0)))
3757         {
3758           tree binfo, basetype;
3759           /* The code below is probably all broken.  See call.c for the
3760              complete right way to do this. this offsets may not be right
3761              in the below.  (mrs) */
3762           /* This destructor must be called via virtual function table.  */
3763           dtor = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (DECL_CONTEXT (dtor)), 0);
3764           basetype = DECL_CLASS_CONTEXT (dtor);
3765           binfo = get_binfo (basetype,
3766                              TREE_TYPE (TREE_TYPE (TREE_VALUE (parms))),
3767                              0);
3768           expr = convert_pointer_to_real (binfo, TREE_VALUE (parms));
3769           if (expr != TREE_VALUE (parms))
3770             {
3771               expr = fold (expr);
3772               ref = build_indirect_ref (expr, NULL_PTR);
3773               TREE_VALUE (parms) = expr;
3774             }
3775           function = build_vfn_ref (&TREE_VALUE (parms), ref, DECL_VINDEX (dtor));
3776           if (function == error_mark_node)
3777             return error_mark_node;
3778           TREE_TYPE (function) = build_pointer_type (TREE_TYPE (dtor));
3779           TREE_CHAIN (parms) = build_tree_list (NULL_TREE, auto_delete);
3780           expr = build_function_call (function, parms);
3781           if (ptr && (flags & LOOKUP_DESTRUCTOR) == 0)
3782             {
3783               /* Handle the case where a virtual destructor is
3784                  being called on an item that is 0.
3785
3786                  @@ Does this really need to be done?  */
3787               tree ifexp = build_binary_op(NE_EXPR, addr, integer_zero_node,1);
3788 #if 0
3789               if (TREE_CODE (ref) == VAR_DECL
3790                   || TREE_CODE (ref) == COMPONENT_REF)
3791                 warning ("losing in build_delete");
3792 #endif
3793               expr = build (COND_EXPR, void_type_node,
3794                             ifexp, expr, void_zero_node);
3795             }
3796         }
3797       else
3798         {
3799           tree ifexp;
3800
3801           if ((flags & LOOKUP_DESTRUCTOR)
3802               || TREE_CODE (ref) == VAR_DECL
3803               || TREE_CODE (ref) == PARM_DECL
3804               || TREE_CODE (ref) == COMPONENT_REF
3805               || TREE_CODE (ref) == ARRAY_REF)
3806             /* These can't be 0.  */
3807             ifexp = integer_one_node;
3808           else
3809             /* Handle the case where a non-virtual destructor is
3810                being called on an item that is 0.  */
3811             ifexp = build_binary_op (NE_EXPR, addr, integer_zero_node, 1);
3812
3813           /* Used to mean that this destructor was known to be empty,
3814              but that's now obsolete.  */
3815           my_friendly_assert (DECL_INITIAL (dtor) != void_type_node, 221);
3816
3817           TREE_CHAIN (parms) = build_tree_list (NULL_TREE, auto_delete);
3818           expr = build_function_call (dtor, parms);
3819
3820           if (ifexp != integer_one_node)
3821             expr = build (COND_EXPR, void_type_node,
3822                           ifexp, expr, void_zero_node);
3823         }
3824       return expr;
3825     }
3826   else
3827     {
3828       /* This can get visibilities wrong.  */
3829       tree binfos = BINFO_BASETYPES (TYPE_BINFO (type));
3830       int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
3831       tree base_binfo = n_baseclasses > 0 ? TREE_VEC_ELT (binfos, 0) : NULL_TREE;
3832       tree exprstmt = NULL_TREE;
3833       tree parent_auto_delete = auto_delete;
3834       tree cond;
3835
3836       /* If this type does not have a destructor, but does have
3837          operator delete, call the parent parent destructor (if any),
3838          but let this node do the deleting.  Otherwise, it is ok
3839          to let the parent destructor do the deleting.  */
3840       if (TREE_GETS_DELETE (type) && !use_global_delete)
3841         {
3842           parent_auto_delete = integer_zero_node;
3843           if (auto_delete == integer_zero_node)
3844             cond = NULL_TREE;
3845           else
3846             {
3847               tree virtual_size;
3848
3849                 /* This is probably wrong. It should be the size of the
3850                    virtual object being deleted.  */
3851               virtual_size = c_sizeof_nowarn (type);
3852
3853               expr = build_opfncall (DELETE_EXPR, LOOKUP_NORMAL, addr,
3854                                      virtual_size, NULL_TREE);
3855               if (expr == error_mark_node)
3856                 return error_mark_node;
3857               if (auto_delete != integer_one_node)
3858                 cond = build (COND_EXPR, void_type_node,
3859                               build (BIT_AND_EXPR, integer_type_node,
3860                                      auto_delete, integer_one_node),
3861                               expr, void_zero_node);
3862               else
3863                 cond = expr;
3864             }
3865         }
3866       else if (base_binfo == NULL_TREE
3867                || (TREE_VIA_VIRTUAL (base_binfo) == 0
3868                    && ! TYPE_NEEDS_DESTRUCTOR (BINFO_TYPE (base_binfo))))
3869         {
3870           tree virtual_size;
3871
3872           /* This is probably wrong. It should be the size of the virtual
3873              object being deleted.  */
3874           virtual_size = c_sizeof_nowarn (type);
3875
3876           cond = build (COND_EXPR, void_type_node,
3877                         build (BIT_AND_EXPR, integer_type_node, auto_delete, integer_one_node),
3878                         build_builtin_call (void_type_node, BID,
3879                                             build_tree_list (NULL_TREE, addr)),
3880                         void_zero_node);
3881         }
3882       else
3883         cond = NULL_TREE;
3884
3885       if (cond)
3886         exprstmt = build_tree_list (NULL_TREE, cond);
3887
3888       if (base_binfo
3889           && ! TREE_VIA_VIRTUAL (base_binfo)
3890           && TYPE_NEEDS_DESTRUCTOR (BINFO_TYPE (base_binfo)))
3891         {
3892           tree this_auto_delete;
3893
3894           if (BINFO_OFFSET_ZEROP (base_binfo))
3895             this_auto_delete = parent_auto_delete;
3896           else
3897             this_auto_delete = integer_zero_node;
3898
3899           expr = build_delete (TYPE_POINTER_TO (BINFO_TYPE (base_binfo)), addr,
3900                                this_auto_delete, flags, 0);
3901           exprstmt = tree_cons (NULL_TREE, expr, exprstmt);
3902         }
3903
3904       /* Take care of the remaining baseclasses.  */
3905       for (i = 1; i < n_baseclasses; i++)
3906         {
3907           base_binfo = TREE_VEC_ELT (binfos, i);
3908           if (! TYPE_NEEDS_DESTRUCTOR (BINFO_TYPE (base_binfo))
3909               || TREE_VIA_VIRTUAL (base_binfo))
3910             continue;
3911
3912           /* May be zero offset if other baseclasses are virtual.  */
3913           expr = fold (build (PLUS_EXPR, TYPE_POINTER_TO (BINFO_TYPE (base_binfo)),
3914                               addr, BINFO_OFFSET (base_binfo)));
3915
3916           expr = build_delete (TYPE_POINTER_TO (BINFO_TYPE (base_binfo)), expr,
3917                                integer_zero_node,
3918                                flags, 0);
3919
3920           exprstmt = tree_cons (NULL_TREE, expr, exprstmt);
3921         }
3922
3923       for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member))
3924         {
3925           if (TREE_CODE (member) != FIELD_DECL)
3926             continue;
3927           if (TYPE_NEEDS_DESTRUCTOR (TREE_TYPE (member)))
3928             {
3929               tree this_member = build_component_ref (ref, DECL_NAME (member), 0, 0);
3930               tree this_type = TREE_TYPE (member);
3931               expr = build_delete (this_type, this_member, integer_two_node, flags, 0);
3932               exprstmt = tree_cons (NULL_TREE, expr, exprstmt);
3933             }
3934         }
3935
3936       if (exprstmt)
3937         return build_compound_expr (exprstmt);
3938       /* Virtual base classes make this function do nothing.  */
3939       return void_zero_node;
3940     }
3941 }
3942
3943 /* For type TYPE, delete the virtual baseclass objects of DECL.  */
3944
3945 tree
3946 build_vbase_delete (type, decl)
3947      tree type, decl;
3948 {
3949   tree vbases = CLASSTYPE_VBASECLASSES (type);
3950   tree result = NULL_TREE;
3951   tree addr = build_unary_op (ADDR_EXPR, decl, 0);
3952
3953   my_friendly_assert (addr != error_mark_node, 222);
3954
3955   while (vbases)
3956     {
3957       tree this_addr = convert_force (TYPE_POINTER_TO (BINFO_TYPE (vbases)),
3958                                       addr);
3959       result = tree_cons (NULL_TREE,
3960                           build_delete (TREE_TYPE (this_addr), this_addr,
3961                                         integer_zero_node,
3962                                         LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 0),
3963                           result);
3964       vbases = TREE_CHAIN (vbases);
3965     }
3966   return build_compound_expr (nreverse (result));
3967 }
3968
3969 /* Build a C++ vector delete expression.
3970    MAXINDEX is the number of elements to be deleted.
3971    ELT_SIZE is the nominal size of each element in the vector.
3972    BASE is the expression that should yield the store to be deleted.
3973    DTOR_DUMMY is a placeholder for a destructor.  The library function
3974    __builtin_vec_delete has a pointer to function in this position.
3975    This function expands (or synthesizes) these calls itself.
3976    AUTO_DELETE_VEC says whether the container (vector) should be deallocated.
3977    AUTO_DELETE say whether each item in the container should be deallocated.
3978
3979    This also calls delete for virtual baseclasses of elements of the vector.
3980
3981    Update: MAXINDEX is no longer needed.  The size can be extracted from the
3982    start of the vector for pointers, and from the type for arrays.  We still
3983    use MAXINDEX for arrays because it happens to already have one of the
3984    values we'd have to extract.  (We could use MAXINDEX with pointers to
3985    confirm the size, and trap if the numbers differ; not clear that it'd
3986    be worth bothering.)  */
3987 tree
3988 build_vec_delete (base, maxindex, elt_size, dtor_dummy, auto_delete_vec, auto_delete)
3989      tree base, maxindex, elt_size;
3990      tree dtor_dummy;
3991      tree auto_delete_vec, auto_delete;
3992 {
3993   tree ptype = TREE_TYPE (base);
3994   tree type;
3995   tree virtual_size;
3996   /* Temporary variables used by the loop.  */
3997   tree tbase, size_exp, tbase_init;
3998
3999   /* This is the body of the loop that implements the deletion of a
4000      single element, and moves temp variables to next elements.  */
4001   tree body;
4002
4003   /* This is the LOOP_EXPR that governs the deletion of the elements.  */
4004   tree loop;
4005
4006   /* This is the thing that governs what to do after the loop has run.  */
4007   tree deallocate_expr = 0;
4008
4009   /* This is the BIND_EXPR which holds the outermost iterator of the
4010      loop.  It is convenient to set this variable up and test it before
4011      executing any other code in the loop.
4012      This is also the containing expression returned by this function.  */
4013   tree controller = NULL_TREE;
4014
4015   /* This is the BLOCK to record the symbol binding for debugging.  */
4016   tree block;
4017
4018   base = stabilize_reference (base);
4019
4020   /* Since we can use base many times, save_expr it. */
4021   if (TREE_SIDE_EFFECTS (base))
4022     base = save_expr (base);
4023
4024   if (TREE_CODE (ptype) == POINTER_TYPE)
4025     {
4026       /* Step back one from start of vector, and read dimension.  */
4027       tree cookie_addr = build (MINUS_EXPR, TYPE_POINTER_TO (BI_header_type),
4028                                 base, BI_header_size);
4029       tree cookie = build_indirect_ref (cookie_addr, NULL_PTR);
4030       maxindex = build_component_ref (cookie, nc_nelts_field_id, 0, 0);
4031       do
4032         ptype = TREE_TYPE (ptype);
4033       while (TREE_CODE (ptype) == ARRAY_TYPE);
4034     }
4035   else if (TREE_CODE (ptype) == ARRAY_TYPE)
4036     {
4037       /* get the total number of things in the array, maxindex is a bad name */
4038       maxindex = array_type_nelts_total (ptype);
4039       while (TREE_CODE (ptype) == ARRAY_TYPE)
4040         ptype = TREE_TYPE (ptype);
4041       base = build_unary_op (ADDR_EXPR, base, 1);
4042     }
4043   else
4044     {
4045       error ("type to vector delete is neither pointer or array type");
4046       return error_mark_node;
4047     }
4048   type = ptype;
4049   ptype = TYPE_POINTER_TO (type);
4050
4051   size_exp = size_in_bytes (type);
4052
4053   if (! IS_AGGR_TYPE (type) || ! TYPE_NEEDS_DESTRUCTOR (type))
4054     {
4055       loop = integer_zero_node;
4056       goto no_destructor;
4057     }
4058
4059   /* The below is short by BI_header_size */
4060   virtual_size = fold (size_binop (MULT_EXPR, size_exp, maxindex));
4061
4062   tbase = build_decl (VAR_DECL, NULL_TREE, ptype);
4063   tbase_init = build_modify_expr (tbase, NOP_EXPR,
4064                                   fold (build (PLUS_EXPR, ptype,
4065                                                base,
4066                                                virtual_size)));
4067   DECL_REGISTER (tbase) = 1;
4068   controller = build (BIND_EXPR, void_type_node, tbase, 0, 0);
4069   TREE_SIDE_EFFECTS (controller) = 1;
4070   block = build_block (tbase, 0, 0, 0, 0);
4071   add_block_current_level (block);
4072
4073   if (auto_delete != integer_zero_node
4074       && auto_delete != integer_two_node)
4075     {
4076       tree base_tbd = convert (ptype,
4077                                build_binary_op (MINUS_EXPR,
4078                                                 convert (ptr_type_node, base),
4079                                                 BI_header_size,
4080                                                 1));
4081       /* This is the real size */
4082       virtual_size = size_binop (PLUS_EXPR, virtual_size, BI_header_size);
4083       body = build_tree_list (NULL_TREE,
4084                               build_x_delete (ptr_type_node, base_tbd, 1,
4085                                               virtual_size));
4086       body = build (COND_EXPR, void_type_node,
4087                     build (BIT_AND_EXPR, integer_type_node,
4088                            auto_delete, integer_one_node),
4089                     body, integer_zero_node);
4090     }
4091   else
4092     body = NULL_TREE;
4093
4094   body = tree_cons (NULL_TREE,
4095                     build_delete (ptype, tbase, auto_delete,
4096                                   LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 1),
4097                     body);
4098
4099   body = tree_cons (NULL_TREE,
4100                     build_modify_expr (tbase, NOP_EXPR, build (MINUS_EXPR, ptype, tbase, size_exp)),
4101                     body);
4102
4103   body = tree_cons (NULL_TREE,
4104                     build (EXIT_EXPR, void_type_node,
4105                            build (EQ_EXPR, integer_type_node, base, tbase)),
4106                     body);
4107
4108   loop = build (LOOP_EXPR, void_type_node, build_compound_expr (body));
4109
4110   loop = tree_cons (NULL_TREE, tbase_init,
4111                     tree_cons (NULL_TREE, loop, NULL_TREE));
4112   loop = build_compound_expr (loop);
4113
4114  no_destructor:
4115   /* If the delete flag is one, or anything else with the low bit set,
4116      delete the storage.  */
4117   if (auto_delete_vec == integer_zero_node
4118       || auto_delete_vec == integer_two_node)
4119     deallocate_expr = integer_zero_node;
4120   else
4121     {
4122       tree base_tbd;
4123
4124       /* The below is short by BI_header_size */
4125       virtual_size = fold (size_binop (MULT_EXPR, size_exp, maxindex));
4126
4127       if (loop == integer_zero_node)
4128         /* no header */
4129         base_tbd = base;
4130       else
4131         {
4132           base_tbd = convert (ptype,
4133                               build_binary_op (MINUS_EXPR,
4134                                                convert (string_type_node, base),
4135                                                BI_header_size,
4136                                                1));
4137           /* True size with header. */
4138           virtual_size = size_binop (PLUS_EXPR, virtual_size, BI_header_size);
4139         }
4140       deallocate_expr = build_x_delete (ptr_type_node, base_tbd, 1,
4141                                         virtual_size);
4142       if (auto_delete_vec != integer_one_node)
4143         deallocate_expr = build (COND_EXPR, void_type_node,
4144                                  build (BIT_AND_EXPR, integer_type_node,
4145                                         auto_delete_vec, integer_one_node),
4146                                  deallocate_expr, integer_zero_node);
4147     }
4148
4149   if (loop && deallocate_expr != integer_zero_node)
4150     {
4151       body = tree_cons (NULL_TREE, loop,
4152                         tree_cons (NULL_TREE, deallocate_expr, NULL_TREE));
4153       body = build_compound_expr (body);
4154     }
4155   else
4156     body = loop;
4157
4158   /* Outermost wrapper: If pointer is null, punt.  */
4159   body = build (COND_EXPR, void_type_node,
4160                 build (NE_EXPR, integer_type_node, base, integer_zero_node),
4161                 body, integer_zero_node);
4162   body = build1 (NOP_EXPR, void_type_node, body);
4163
4164   if (controller)
4165     {
4166       TREE_OPERAND (controller, 1) = body;
4167       return controller;
4168     }
4169   else
4170     return convert (void_type_node, body);
4171 }