OSDN Git Service

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