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