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