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