OSDN Git Service

80th Cygnus<->FSF merge
[pf3gnuchains/gcc-fork.git] / gcc / cp / class.c
1 /* Functions related to building classes and their related objects.
2    Copyright (C) 1987, 1992, 1993, 1994, 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, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.  */
21
22
23 /* High-level class interface. */
24
25 #include "config.h"
26 #include "tree.h"
27 #include <stdio.h>
28 #include "cp-tree.h"
29 #include "flags.h"
30 #include "rtl.h"
31 #include "output.h"
32
33 #include "obstack.h"
34 #define obstack_chunk_alloc xmalloc
35 #define obstack_chunk_free free
36
37 extern struct obstack permanent_obstack;
38
39 /* This is how we tell when two virtual member functions are really the
40    same. */
41 #define SAME_FN(FN1DECL, FN2DECL) (DECL_ASSEMBLER_NAME (FN1DECL) == DECL_ASSEMBLER_NAME (FN2DECL))
42
43 extern void set_class_shadows PROTO ((tree));
44
45 /* Way of stacking class types.  */
46 static tree *current_class_base, *current_class_stack;
47 static int current_class_stacksize;
48 int current_class_depth;
49
50 struct class_level
51 {
52   /* The previous class level.  */
53   struct class_level *level_chain;
54
55   /* The class instance variable, as a PARM_DECL.  */
56   tree decl;
57   /* The class instance variable, as an object.  */
58   tree object;
59   /* The virtual function table pointer
60      for the class instance variable.  */
61   tree vtable_decl;
62
63   /* Name of the current class.  */
64   tree name;
65   /* Type of the current class.  */
66   tree type;
67
68   /* Flags for this class level.  */
69   int this_is_variable;
70   int memoized_lookups;
71   int save_memoized;
72   int unused;
73 };
74
75 tree current_class_decl, C_C_D; /* PARM_DECL: the class instance variable */
76
77 /* The following two can be derived from the previous one */
78 tree current_class_name;        /* IDENTIFIER_NODE: name of current class */
79 tree current_class_type;        /* _TYPE: the type of the current class */
80 tree previous_class_type;       /* _TYPE: the previous type that was a class */
81 tree previous_class_values;             /* TREE_LIST: copy of the class_shadowed list
82                                    when leaving an outermost class scope.  */
83 static tree get_vfield_name PROTO((tree));
84 tree the_null_vtable_entry;
85
86 /* Way of stacking language names.  */
87 tree *current_lang_base, *current_lang_stack;
88 int current_lang_stacksize;
89
90 /* Names of languages we recognize.  */
91 tree lang_name_c, lang_name_cplusplus;
92 tree current_lang_name;
93
94 char *dont_allow_type_definitions;
95
96 /* When layout out an aggregate type, the size of the
97    basetypes (virtual and non-virtual) is passed to layout_record
98    via this node.  */
99 static tree base_layout_decl;
100
101 /* Constants used for access control.  */
102 tree access_default_node; /* 0 */
103 tree access_public_node; /* 1 */
104 tree access_protected_node; /* 2 */
105 tree access_private_node; /* 3 */
106 tree access_default_virtual_node; /* 4 */
107 tree access_public_virtual_node; /* 5 */
108 tree access_private_virtual_node; /* 6 */
109
110 /* Variables shared between class.c and call.c.  */
111
112 int n_vtables = 0;
113 int n_vtable_entries = 0;
114 int n_vtable_searches = 0;
115 int n_vtable_elems = 0;
116 int n_convert_harshness = 0;
117 int n_compute_conversion_costs = 0;
118 int n_build_method_call = 0;
119 int n_inner_fields_searched = 0;
120
121 /* Virtual baseclass things.  */
122 tree
123 build_vbase_pointer (exp, type)
124      tree exp, type;
125 {
126   char *name;
127
128   name = (char *) alloca (TYPE_NAME_LENGTH (type) + sizeof (VBASE_NAME) + 1);
129   sprintf (name, VBASE_NAME_FORMAT, TYPE_NAME_STRING (type));
130   return build_component_ref (exp, get_identifier (name), 0, 0);
131 }
132
133 /* Is the type of the EXPR, the complete type of the object?
134    If we are going to be wrong, we must be conservative, and return 0. */
135 int
136 complete_type_p (expr)
137      tree expr;
138 {
139   tree type = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
140   while (1)
141     {
142       switch (TREE_CODE (expr))
143         {
144         case SAVE_EXPR:
145         case INDIRECT_REF:
146         case ADDR_EXPR:
147         case NOP_EXPR:
148         case CONVERT_EXPR:
149           expr = TREE_OPERAND (expr, 0);
150           continue;
151
152         case CALL_EXPR: 
153           if (! TREE_HAS_CONSTRUCTOR (expr))
154             break;
155           /* fall through... */
156         case VAR_DECL:
157         case FIELD_DECL:
158           if (TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE
159               && IS_AGGR_TYPE (TREE_TYPE (TREE_TYPE (expr)))
160               && TYPE_MAIN_VARIANT (TREE_TYPE (expr)) == type)
161             return 1;
162           /* fall through... */
163         case TARGET_EXPR:
164         case PARM_DECL:
165           if (IS_AGGR_TYPE (TREE_TYPE (expr))
166               && TYPE_MAIN_VARIANT (TREE_TYPE (expr)) == type)
167             return 1;
168           /* fall through... */
169         case PLUS_EXPR:
170         default:
171           break;
172         }
173       break;
174     }
175   return 0;
176 }
177
178 /* Build multi-level access to EXPR using hierarchy path PATH.
179    CODE is PLUS_EXPR if we are going with the grain,
180    and MINUS_EXPR if we are not (in which case, we cannot traverse
181    virtual baseclass links).
182
183    TYPE is the type we want this path to have on exit.
184
185    ALIAS_THIS is non-zero if EXPR in an expression involving `this'.  */
186 tree
187 build_vbase_path (code, type, expr, path, alias_this)
188      enum tree_code code;
189      tree type, expr, path;
190      int alias_this;
191 {
192   register int changed = 0;
193   tree last = NULL_TREE, last_virtual = NULL_TREE;
194   int nonnull = 0;
195   int fixed_type_p = resolves_to_fixed_type_p (expr, &nonnull);
196   tree null_expr = 0, nonnull_expr;
197   tree basetype;
198   tree offset = integer_zero_node;
199
200   if (nonnull == 0 && (alias_this && flag_this_is_variable <= 0))
201     nonnull = 1;
202
203   /* We need additional logic to convert back to the unconverted type
204      (the static type of the complete object), and then convert back
205      to the type we want.  Until that is done, or until we can
206      recognize when that is, we cannot do the short cut logic. (mrs) */
207   /* Do this, until we can undo any previous conversions.  See net35.C
208      for a testcase. */
209   fixed_type_p = complete_type_p (expr);
210
211   if (!fixed_type_p && TREE_SIDE_EFFECTS (expr))
212     expr = save_expr (expr);
213   nonnull_expr = expr;
214
215   if (BINFO_INHERITANCE_CHAIN (path))
216     {
217       tree reverse_path = NULL_TREE;
218
219       while (path)
220         {
221           tree r = copy_node (path);
222           BINFO_INHERITANCE_CHAIN (r) = reverse_path;
223           reverse_path = r;
224           path = BINFO_INHERITANCE_CHAIN (path);
225         }
226       path = reverse_path;
227     }
228
229   basetype = BINFO_TYPE (path);
230
231   while (path)
232     {
233       if (TREE_VIA_VIRTUAL (path))
234         {
235           last_virtual = BINFO_TYPE (path);
236           if (code == PLUS_EXPR)
237             {
238               changed = ! fixed_type_p;
239
240               if (changed)
241                 {
242                   extern int flag_assume_nonnull_objects;
243                   tree ind;
244
245                   /* We already check for ambiguous things in the caller, just
246                      find a path. */
247                   if (last)
248                     {
249                       tree binfo = get_binfo (last, TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (nonnull_expr))), 0);
250                       nonnull_expr = convert_pointer_to_real (binfo, nonnull_expr);
251                     }
252                   ind = build_indirect_ref (nonnull_expr, NULL_PTR);
253                   nonnull_expr = build_vbase_pointer (ind, last_virtual);
254                   if (nonnull == 0
255                       && (TREE_CODE (type) == POINTER_TYPE
256                           || !flag_assume_nonnull_objects)
257                       && null_expr == NULL_TREE)
258                     {
259                       null_expr = build1 (NOP_EXPR, build_pointer_type (last_virtual), integer_zero_node);
260                       expr = build (COND_EXPR, build_pointer_type (last_virtual),
261                                     build (EQ_EXPR, boolean_type_node, expr,
262                                            integer_zero_node),
263                                     null_expr, nonnull_expr);
264                     }
265                 }
266               /* else we'll figure out the offset below.  */
267
268               /* Happens in the case of parse errors.  */
269               if (nonnull_expr == error_mark_node)
270                 return error_mark_node;
271             }
272           else
273             {
274               cp_error ("cannot cast up from virtual baseclass `%T'",
275                           last_virtual);
276               return error_mark_node;
277             }
278         }
279       last = path;
280       path = BINFO_INHERITANCE_CHAIN (path);
281     }
282   /* LAST is now the last basetype assoc on the path.  */
283
284   /* A pointer to a virtual base member of a non-null object
285      is non-null.  Therefore, we only need to test for zeroness once.
286      Make EXPR the canonical expression to deal with here.  */
287   if (null_expr)
288     {
289       TREE_OPERAND (expr, 2) = nonnull_expr;
290       TREE_TYPE (TREE_OPERAND (expr, 1)) = TREE_TYPE (nonnull_expr);
291     }
292   else
293     expr = nonnull_expr;
294
295   /* If we go through any virtual base pointers, make sure that
296      casts to BASETYPE from the last virtual base class use
297      the right value for BASETYPE.  */
298   if (changed)
299     {
300       tree intype = TREE_TYPE (TREE_TYPE (expr));
301       if (TYPE_MAIN_VARIANT (intype) != BINFO_TYPE (last))
302         {
303           tree binfo = get_binfo (last, TYPE_MAIN_VARIANT (intype), 0);
304           offset = BINFO_OFFSET (binfo);
305         }
306     }
307   else
308     {
309       if (last_virtual)
310         {
311           offset = BINFO_OFFSET (binfo_member (last_virtual,
312                                                CLASSTYPE_VBASECLASSES (basetype)));
313           offset = size_binop (PLUS_EXPR, offset, BINFO_OFFSET (last));
314         }
315       else
316         offset = BINFO_OFFSET (last);
317     }
318
319   if (TREE_INT_CST_LOW (offset))
320     {
321       /* Bash types to make the backend happy.  */
322       offset = convert (type, offset);
323       expr = build1 (NOP_EXPR, type, expr);
324
325       /* For multiple inheritance: if `this' can be set by any
326          function, then it could be 0 on entry to any function.
327          Preserve such zeroness here.  Otherwise, only in the
328          case of constructors need we worry, and in those cases,
329          it will be zero, or initialized to some valid value to
330          which we may add.  */
331       if (nonnull == 0)
332         {
333           if (null_expr)
334             TREE_TYPE (null_expr) = type;
335           else
336             null_expr = build1 (NOP_EXPR, type, integer_zero_node);
337           if (TREE_SIDE_EFFECTS (expr))
338             expr = save_expr (expr);
339
340           return build (COND_EXPR, type,
341                         build (EQ_EXPR, boolean_type_node, expr, integer_zero_node),
342                         null_expr,
343                         build (code, type, expr, offset));
344         }
345       else return build (code, type, expr, offset);
346     }
347
348   /* Cannot change the TREE_TYPE of a NOP_EXPR here, since it may
349      be used multiple times in initialization of multiple inheritance.  */
350   if (null_expr)
351     {
352       TREE_TYPE (expr) = type;
353       return expr;
354     }
355   else
356     return build1 (NOP_EXPR, type, expr);
357 }
358
359 /* Virtual function things.  */
360
361 /* Virtual functions to be dealt with after laying out our base
362    classes.  We do all overrides after we layout virtual base classes.
363    */
364 static tree pending_hard_virtuals;
365 static int doing_hard_virtuals;
366
367 /* Build an entry in the virtual function table.
368    DELTA is the offset for the `this' pointer.
369    PFN is an ADDR_EXPR containing a pointer to the virtual function.
370    Note that the index (DELTA2) in the virtual function table
371    is always 0.  */
372 tree
373 build_vtable_entry (delta, pfn)
374      tree delta, pfn;
375 {
376
377   if (flag_vtable_thunks)
378     {
379       HOST_WIDE_INT idelta = TREE_INT_CST_LOW (delta);
380       extern tree make_thunk ();
381       if (idelta)
382         {
383           pfn = build1 (ADDR_EXPR, vtable_entry_type,
384                         make_thunk (pfn, idelta));
385           TREE_READONLY (pfn) = 1;
386           TREE_CONSTANT (pfn) = 1;
387         }
388 #ifdef GATHER_STATISTICS
389       n_vtable_entries += 1;
390 #endif
391       return pfn;
392     }
393   else
394     {
395       extern int flag_huge_objects;
396       tree elems = tree_cons (NULL_TREE, delta,
397                               tree_cons (NULL_TREE, integer_zero_node,
398                                          build_tree_list (NULL_TREE, pfn)));
399       tree entry = build (CONSTRUCTOR, vtable_entry_type, NULL_TREE, elems);
400
401       /* DELTA is constructed by `size_int', which means it may be an
402          unsigned quantity on some platforms.  Therefore, we cannot use
403          `int_fits_type_p', because when DELTA is really negative,
404          `force_fit_type' will make it look like a very large number.  */
405
406       if ((TREE_INT_CST_LOW (TYPE_MAX_VALUE (delta_type_node))
407            < TREE_INT_CST_LOW (delta))
408           || (TREE_INT_CST_LOW (delta)
409               < TREE_INT_CST_LOW (TYPE_MIN_VALUE (delta_type_node))))
410         if (flag_huge_objects)
411           sorry ("object size exceeds built-in limit for virtual function table implementation");
412         else
413           sorry ("object size exceeds normal limit for virtual function table implementation, recompile all source and use -fhuge-objects");
414
415       TREE_CONSTANT (entry) = 1;
416       TREE_STATIC (entry) = 1;
417       TREE_READONLY (entry) = 1;
418
419 #ifdef GATHER_STATISTICS
420       n_vtable_entries += 1;
421 #endif
422
423       return entry;
424     }
425 }
426
427 /* Given an object INSTANCE, return an expression which yields the
428    virtual function corresponding to INDEX.  There are many special
429    cases for INSTANCE which we take care of here, mainly to avoid
430    creating extra tree nodes when we don't have to.  */
431 tree
432 build_vfn_ref (ptr_to_instptr, instance, idx)
433      tree *ptr_to_instptr, instance;
434      tree idx;
435 {
436   tree vtbl, aref;
437   tree basetype = TREE_TYPE (instance);
438
439   if (TREE_CODE (basetype) == REFERENCE_TYPE)
440     basetype = TREE_TYPE (basetype);
441
442   if (instance == C_C_D)
443     vtbl = build_indirect_ref (build_vfield_ref (instance, basetype),
444                                NULL_PTR);
445   else
446     {
447       if (optimize)
448         {
449           /* Try to figure out what a reference refers to, and
450              access its virtual function table directly.  */
451           tree ref = NULL_TREE;
452
453           if (TREE_CODE (instance) == INDIRECT_REF
454               && TREE_CODE (TREE_TYPE (TREE_OPERAND (instance, 0))) == REFERENCE_TYPE)
455             ref = TREE_OPERAND (instance, 0);
456           else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
457             ref = instance;
458
459           if (ref && TREE_CODE (ref) == VAR_DECL
460               && DECL_INITIAL (ref))
461             {
462               tree init = DECL_INITIAL (ref);
463
464               while (TREE_CODE (init) == NOP_EXPR
465                      || TREE_CODE (init) == NON_LVALUE_EXPR)
466                 init = TREE_OPERAND (init, 0);
467               if (TREE_CODE (init) == ADDR_EXPR)
468                 {
469                   init = TREE_OPERAND (init, 0);
470                   if (IS_AGGR_TYPE (TREE_TYPE (init))
471                       && (TREE_CODE (init) == PARM_DECL
472                           || TREE_CODE (init) == VAR_DECL))
473                     instance = init;
474                 }
475             }
476         }
477
478       if (IS_AGGR_TYPE (TREE_TYPE (instance))
479           && (TREE_CODE (instance) == RESULT_DECL
480               || TREE_CODE (instance) == PARM_DECL
481               || TREE_CODE (instance) == VAR_DECL))
482         vtbl = TYPE_BINFO_VTABLE (basetype);
483       else
484         vtbl = build_indirect_ref (build_vfield_ref (instance, basetype),
485                                    NULL_PTR);
486     }
487   assemble_external (vtbl);
488   aref = build_array_ref (vtbl, idx);
489
490   /* Save the intermediate result in a SAVE_EXPR so we don't have to
491      compute each component of the virtual function pointer twice.  */ 
492   if (TREE_CODE (aref) == INDIRECT_REF)
493     TREE_OPERAND (aref, 0) = save_expr (TREE_OPERAND (aref, 0));
494
495   if (flag_vtable_thunks)
496     return aref;
497   else
498     {
499       if (ptr_to_instptr)
500         *ptr_to_instptr
501           = build (PLUS_EXPR, TREE_TYPE (*ptr_to_instptr),
502                    *ptr_to_instptr,
503                    convert (ptrdiff_type_node,
504                             build_component_ref (aref, delta_identifier, 0, 0)));
505       return build_component_ref (aref, pfn_identifier, 0, 0);
506     }
507 }
508
509 /* Return the name of the virtual function table (as an IDENTIFIER_NODE)
510    for the given TYPE.  */
511 static tree
512 get_vtable_name (type)
513      tree type;
514 {
515   tree type_id = build_typename_overload (type);
516   char *buf = (char *)alloca (strlen (VTABLE_NAME_FORMAT)
517                               + IDENTIFIER_LENGTH (type_id) + 2);
518   char *ptr = IDENTIFIER_POINTER (type_id);
519   int i;
520   for (i = 0; ptr[i] == OPERATOR_TYPENAME_FORMAT[i]; i++) ;
521 #if 0
522   /* We don't take off the numbers; prepare_fresh_vtable uses the
523      DECL_ASSEMBLER_NAME for the type, which includes the number
524      in `3foo'.  If we were to pull them off here, we'd end up with
525      something like `_vt.foo.3bar', instead of a uniform definition.  */
526   while (ptr[i] >= '0' && ptr[i] <= '9')
527     i += 1;
528 #endif
529   sprintf (buf, VTABLE_NAME_FORMAT, ptr+i);
530   return get_identifier (buf);
531 }
532
533 /* Build a virtual function for type TYPE.
534    If BINFO is non-NULL, build the vtable starting with the initial
535    approximation that it is the same as the one which is the head of
536    the association list.  */
537 static tree
538 build_vtable (binfo, type)
539      tree binfo, type;
540 {
541   tree name = get_vtable_name (type);
542   tree virtuals, decl;
543
544   if (binfo)
545     {
546       virtuals = copy_list (BINFO_VIRTUALS (binfo));
547       decl = build_decl (VAR_DECL, name, TREE_TYPE (BINFO_VTABLE (binfo)));
548     }
549   else
550     {
551       virtuals = NULL_TREE;
552       decl = build_decl (VAR_DECL, name, void_type_node);
553     }
554
555 #ifdef GATHER_STATISTICS
556   n_vtables += 1;
557   n_vtable_elems += list_length (virtuals);
558 #endif
559
560   /* Set TREE_PUBLIC and TREE_EXTERN as appropriate.  */
561   import_export_vtable (decl, type, 0);
562
563   IDENTIFIER_GLOBAL_VALUE (name) = decl = pushdecl_top_level (decl);
564   /* Initialize the association list for this type, based
565      on our first approximation.  */
566   TYPE_BINFO_VTABLE (type) = decl;
567   TYPE_BINFO_VIRTUALS (type) = virtuals;
568
569   TREE_STATIC (decl) = 1;
570 #ifndef WRITABLE_VTABLES
571   /* Make them READONLY by default. (mrs) */
572   TREE_READONLY (decl) = 1;
573 #endif
574   /* At one time the vtable info was grabbed 2 words at a time.  This
575      fails on sparc unless you have 8-byte alignment.  (tiemann) */
576   DECL_ALIGN (decl) = MAX (TYPE_ALIGN (double_type_node),
577                            DECL_ALIGN (decl));
578
579   /* Why is this conditional? (mrs) */
580   if (binfo && write_virtuals >= 0)
581     DECL_VIRTUAL_P (decl) = 1;
582   DECL_CONTEXT (decl) = type;
583
584   binfo = TYPE_BINFO (type);
585   SET_BINFO_NEW_VTABLE_MARKED (binfo);
586   return decl;
587 }
588
589 /* Given a base type PARENT, and a derived type TYPE, build
590    a name which distinguishes exactly the PARENT member of TYPE's type.
591
592    FORMAT is a string which controls how sprintf formats the name
593    we have generated.
594
595    For example, given
596
597         class A; class B; class C : A, B;
598
599    it is possible to distinguish "A" from "C's A".  And given
600
601         class L;
602         class A : L; class B : L; class C : A, B;
603
604    it is possible to distinguish "L" from "A's L", and also from
605    "C's L from A".
606
607    Make sure to use the DECL_ASSEMBLER_NAME of the TYPE_NAME of the
608    type, as template have DECL_NAMEs like: X<int>, whereas the
609    DECL_ASSEMBLER_NAME is set to be something the assembler can handle.
610   */
611 static tree
612 build_type_pathname (format, parent, type)
613      char *format;
614      tree parent, type;
615 {
616   extern struct obstack temporary_obstack;
617   char *first, *base, *name;
618   int i;
619   tree id;
620
621   parent = TYPE_MAIN_VARIANT (parent);
622
623   /* Remember where to cut the obstack to.  */
624   first = obstack_base (&temporary_obstack);
625
626   /* Put on TYPE+PARENT.  */
627   obstack_grow (&temporary_obstack,
628                 TYPE_ASSEMBLER_NAME_STRING (type),
629                 TYPE_ASSEMBLER_NAME_LENGTH (type));
630 #ifdef JOINER
631   obstack_1grow (&temporary_obstack, JOINER);
632 #else
633   obstack_1grow (&temporary_obstack, '_');
634 #endif
635   obstack_grow0 (&temporary_obstack,
636                  TYPE_ASSEMBLER_NAME_STRING (parent),
637                  TYPE_ASSEMBLER_NAME_LENGTH (parent));
638   i = obstack_object_size (&temporary_obstack);
639   base = obstack_base (&temporary_obstack);
640   obstack_finish (&temporary_obstack);
641
642   /* Put on FORMAT+TYPE+PARENT.  */
643   obstack_blank (&temporary_obstack, strlen (format) + i + 1);
644   name = obstack_base (&temporary_obstack);
645   sprintf (name, format, base);
646   id = get_identifier (name);
647   obstack_free (&temporary_obstack, first);
648
649   return id;
650 }
651
652 /* Update the rtti info for this class.  */
653 static void
654 set_rtti_entry (virtuals, offset, type)
655      tree virtuals, offset, type;
656 {
657   if (! flag_vtable_thunks)
658     TREE_VALUE (virtuals)
659       = build_vtable_entry (offset,
660                             (flag_rtti
661                              ? build_t_desc (type, 0)
662                              : integer_zero_node));
663   else
664     {
665       tree vfn = build1 (NOP_EXPR, vfunc_ptr_type_node, offset);
666       TREE_CONSTANT (vfn) = 1;
667
668       TREE_VALUE (virtuals)
669         = build_vtable_entry (integer_zero_node, vfn);
670       /* The second slot is for the tdesc pointer when thunks are used.  */
671       vfn = flag_rtti
672              ? build_t_desc (type, 0)
673              : integer_zero_node;
674       vfn = build1 (NOP_EXPR, vfunc_ptr_type_node, vfn);
675       TREE_CONSTANT (vfn) = 1;
676
677       TREE_VALUE (TREE_CHAIN (virtuals))
678         = build_vtable_entry (integer_zero_node, vfn);
679     }
680 }
681
682 /* Give TYPE a new virtual function table which is initialized
683    with a skeleton-copy of its original initialization.  The only
684    entry that changes is the `delta' entry, so we can really
685    share a lot of structure.
686
687    FOR_TYPE is the derived type which caused this table to
688    be needed.
689
690    BINFO is the type association which provided TYPE for FOR_TYPE.  */
691 static void
692 prepare_fresh_vtable (binfo, for_type)
693      tree binfo, for_type;
694 {
695   tree basetype = BINFO_TYPE (binfo);
696   tree orig_decl = BINFO_VTABLE (binfo);
697   /* This name is too simplistic.  We can have multiple basetypes for
698      for_type, and we really want different names.  (mrs) */
699   tree name = build_type_pathname (VTABLE_NAME_FORMAT, basetype, for_type);
700   tree new_decl = build_decl (VAR_DECL, name, TREE_TYPE (orig_decl));
701   tree path, offset;
702   int result;
703
704   /* Remember which class this vtable is really for.  */
705   DECL_CONTEXT (new_decl) = for_type;
706
707   TREE_STATIC (new_decl) = 1;
708   BINFO_VTABLE (binfo) = pushdecl_top_level (new_decl);
709   DECL_VIRTUAL_P (new_decl) = 1;
710 #ifndef WRITABLE_VTABLES
711   /* Make them READONLY by default. (mrs) */
712   TREE_READONLY (new_decl) = 1;
713 #endif
714   DECL_ALIGN (new_decl) = DECL_ALIGN (orig_decl);
715
716   /* Make fresh virtual list, so we can smash it later.  */
717   BINFO_VIRTUALS (binfo) = copy_list (BINFO_VIRTUALS (binfo));
718
719   if (TREE_VIA_VIRTUAL (binfo))
720     offset = BINFO_OFFSET (binfo_member (BINFO_TYPE (binfo), 
721                             CLASSTYPE_VBASECLASSES (for_type)));
722   else
723     offset = BINFO_OFFSET (binfo);
724
725   set_rtti_entry (BINFO_VIRTUALS (binfo),
726                   size_binop (MINUS_EXPR, integer_zero_node, offset),
727                   for_type);
728
729 #ifdef GATHER_STATISTICS
730   n_vtables += 1;
731   n_vtable_elems += list_length (BINFO_VIRTUALS (binfo));
732 #endif
733
734   /* Set TREE_PUBLIC and TREE_EXTERN as appropriate.  */
735   import_export_vtable (new_decl, for_type, 0);
736
737   if (TREE_VIA_VIRTUAL (binfo))
738     my_friendly_assert (binfo == binfo_member (BINFO_TYPE (binfo),
739                                    CLASSTYPE_VBASECLASSES (current_class_type)),
740                         170);
741   SET_BINFO_NEW_VTABLE_MARKED (binfo);
742 }
743
744 /* Access the virtual function table entry that logically
745    contains BASE_FNDECL.  VIRTUALS is the virtual function table's
746    initializer.  We can run off the end, when dealing with virtual
747    destructors in MI situations, return NULL_TREE in that case.  */
748 static tree
749 get_vtable_entry (virtuals, base_fndecl)
750      tree virtuals, base_fndecl;
751 {
752   unsigned HOST_WIDE_INT n = (HOST_BITS_PER_WIDE_INT >= BITS_PER_WORD
753            ? (TREE_INT_CST_LOW (DECL_VINDEX (base_fndecl))
754               & (((unsigned HOST_WIDE_INT)1<<(BITS_PER_WORD-1))-1))
755            : TREE_INT_CST_LOW (DECL_VINDEX (base_fndecl)));
756
757 #ifdef GATHER_STATISTICS
758   n_vtable_searches += n;
759 #endif
760
761   while (n > 0 && virtuals)
762     {
763       --n;
764       virtuals = TREE_CHAIN (virtuals);
765     }
766   return virtuals;
767 }
768
769 /* Put new entry ENTRY into virtual function table initializer
770    VIRTUALS.
771
772    Also update DECL_VINDEX (FNDECL).  */
773
774 static void
775 modify_vtable_entry (old_entry_in_list, new_entry, fndecl)
776      tree old_entry_in_list, new_entry, fndecl;
777 {
778   tree base_fndecl = TREE_OPERAND (FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (old_entry_in_list)), 0);
779
780 #ifdef NOTQUITE
781   cp_warning ("replaced %D with %D", DECL_ASSEMBLER_NAME (base_fndecl),
782               DECL_ASSEMBLER_NAME (fndecl));
783 #endif
784   TREE_VALUE (old_entry_in_list) = new_entry;
785
786   /* Now assign virtual dispatch information, if unset.  */
787   /* We can dispatch this, through any overridden base function. */
788   if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
789     {
790       DECL_VINDEX (fndecl) = DECL_VINDEX (base_fndecl);
791       DECL_CONTEXT (fndecl) = DECL_CONTEXT (base_fndecl);
792     }
793 }
794
795 /* Access the virtual function table entry i.  VIRTUALS is the virtual
796    function table's initializer.  */
797 static tree
798 get_vtable_entry_n (virtuals, n)
799      tree virtuals;
800      unsigned HOST_WIDE_INT n;
801 {
802   while (n > 0)
803     {
804       --n;
805       virtuals = TREE_CHAIN (virtuals);
806     }
807   return virtuals;
808 }
809
810 /* Add a virtual function to all the appropriate vtables for the class
811    T.  DECL_VINDEX(X) should be error_mark_node, if we want to
812    allocate a new slot in our table.  If it is error_mark_node, we
813    know that no other function from another vtable is overridden by X.
814    HAS_VIRTUAL keeps track of how many virtuals there are in our main
815    vtable for the type, and we build upon the PENDING_VIRTUALS list
816    and return it.  */
817 static tree
818 add_virtual_function (pending_virtuals, has_virtual, fndecl, t)
819      tree pending_virtuals;
820      int *has_virtual;
821      tree fndecl;
822      tree t; /* Structure type. */
823 {
824   /* FUNCTION_TYPEs and OFFSET_TYPEs no longer freely
825      convert to void *.  Make such a conversion here.  */
826   tree vfn = build1 (ADDR_EXPR, vfunc_ptr_type_node, fndecl);
827   TREE_CONSTANT (vfn) = 1;
828
829 #ifndef DUMB_USER
830   if (current_class_type == 0)
831     cp_warning ("internal problem, current_class_type is zero when adding `%D', please report",
832                 fndecl);
833   if (current_class_type && t != current_class_type)
834     cp_warning ("internal problem, current_class_type differs when adding `%D', please report",
835                 fndecl);
836 #endif
837
838   /* If the virtual function is a redefinition of a prior one,
839      figure out in which base class the new definition goes,
840      and if necessary, make a fresh virtual function table
841      to hold that entry.  */
842   if (DECL_VINDEX (fndecl) == error_mark_node)
843     {
844       tree entry;
845
846       if (flag_rtti && *has_virtual == 0)
847         {
848           /* CLASSTYPE_RTTI is only used as a Boolean (NULL or not). */
849           CLASSTYPE_RTTI (t) = integer_one_node;
850         }
851
852       /* If we are using thunks, use two slots at the front, one
853          for the offset pointer, one for the tdesc pointer.  */
854       if (*has_virtual == 0 && flag_vtable_thunks)
855         {
856           *has_virtual = 1;
857         }
858
859       /* Build a new INT_CST for this DECL_VINDEX.  */
860       {
861         static tree index_table[256];
862         tree index;
863         /* We skip a slot for the offset/tdesc entry.  */
864         int i = ++(*has_virtual);
865
866         if (i >= 256 || index_table[i] == 0)
867           {
868             index = build_int_2 (i, 0);
869             if (i < 256)
870               index_table[i] = index;
871           }
872         else
873           index = index_table[i];
874
875         /* Now assign virtual dispatch information. */
876         DECL_VINDEX (fndecl) = index;
877         DECL_CONTEXT (fndecl) = t;
878       }
879       entry = build_vtable_entry (integer_zero_node, vfn);
880       pending_virtuals = tree_cons (DECL_VINDEX (fndecl), entry, pending_virtuals);
881     }
882   /* Might already be INTEGER_CST if declared twice in class.  We will
883      give error later or we've already given it.  */
884   else if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
885     {
886       /* Need an entry in some other virtual function table.
887          Deal with this after we have laid out our virtual base classes.  */
888       pending_hard_virtuals = temp_tree_cons (fndecl, vfn, pending_hard_virtuals);
889     }
890   return pending_virtuals;
891 }
892 \f
893 /* Obstack on which to build the vector of class methods.  */
894 struct obstack class_obstack;
895 extern struct obstack *current_obstack;
896
897 /* Add method METHOD to class TYPE.  This is used when a method
898    has been defined which did not initially appear in the class definition,
899    and helps cut down on spurious error messages.
900
901    FIELDS is the entry in the METHOD_VEC vector entry of the class type where
902    the method should be added.  */
903 void
904 add_method (type, fields, method)
905      tree type, *fields, method;
906 {
907   /* We must make a copy of METHOD here, since we must be sure that
908      we have exclusive title to this method's DECL_CHAIN.  */
909   tree decl;
910
911   push_obstacks (&permanent_obstack, &permanent_obstack);
912   {
913     decl = copy_node (method);
914     if (DECL_RTL (decl) == 0
915         && (!processing_template_decl
916             || !uses_template_parms (decl)))
917       {
918         make_function_rtl (decl);
919         DECL_RTL (method) = DECL_RTL (decl);
920       }
921   }
922
923   if (fields && *fields)
924     {
925       /* Take care not to hide destructor.  */
926       DECL_CHAIN (decl) = DECL_CHAIN (*fields);
927       DECL_CHAIN (*fields) = decl;
928     }
929   else if (CLASSTYPE_METHOD_VEC (type) == 0)
930     {
931       tree method_vec = make_node (TREE_VEC);
932       if (TYPE_IDENTIFIER (type) == DECL_NAME (decl))
933         {
934           TREE_VEC_ELT (method_vec, 0) = decl;
935           TREE_VEC_LENGTH (method_vec) = 1;
936         }
937       else
938         {
939           /* ??? Is it possible for there to have been enough room in the
940              current chunk for the tree_vec structure but not a tree_vec
941              plus a tree*?  Will this work in that case?  */
942           obstack_free (current_obstack, method_vec);
943           obstack_blank (current_obstack, sizeof (struct tree_vec) + sizeof (tree *));
944           TREE_VEC_ELT (method_vec, 1) = decl;
945           TREE_VEC_LENGTH (method_vec) = 2;
946           obstack_finish (current_obstack);
947         }
948       CLASSTYPE_METHOD_VEC (type) = method_vec;
949     }
950   else
951     {
952       tree method_vec = CLASSTYPE_METHOD_VEC (type);
953       int len = TREE_VEC_LENGTH (method_vec);
954
955       /* Adding a new ctor or dtor.  This is easy because our
956          METHOD_VEC always has a slot for such entries.  */
957       if (TYPE_IDENTIFIER (type) == DECL_NAME (decl))
958         {
959           /* TREE_VEC_ELT (method_vec, 0) = decl; */
960           if (decl != TREE_VEC_ELT (method_vec, 0))
961             {
962               DECL_CHAIN (decl) = TREE_VEC_ELT (method_vec, 0);
963               TREE_VEC_ELT (method_vec, 0) = decl;
964             }
965         }
966       else
967         {
968           /* This is trickier.  We try to extend the TREE_VEC in-place,
969              but if that does not work, we copy all its data to a new
970              TREE_VEC that's large enough.  */
971           struct obstack *ob = &class_obstack;
972           tree *end = (tree *)obstack_next_free (ob);
973
974           if (end != TREE_VEC_END (method_vec))
975             {
976               ob = current_obstack;
977               TREE_VEC_LENGTH (method_vec) += 1;
978               TREE_VEC_ELT (method_vec, len) = NULL_TREE;
979               method_vec = copy_node (method_vec);
980               TREE_VEC_LENGTH (method_vec) -= 1;
981             }
982           else
983             {
984               tree tmp_vec = (tree) obstack_base (ob);
985               if (obstack_room (ob) < sizeof (tree))
986                 {
987                   obstack_blank (ob, sizeof (struct tree_common)
988                                  + tree_code_length[(int) TREE_VEC]
989                                    * sizeof (char *)
990                                  + len * sizeof (tree));
991                   tmp_vec = (tree) obstack_base (ob);
992                   bcopy ((char *) method_vec, (char *) tmp_vec,
993                          (sizeof (struct tree_common)
994                           + tree_code_length[(int) TREE_VEC] * sizeof (char *)
995                           + (len-1) * sizeof (tree)));
996                   method_vec = tmp_vec;
997                 }
998               else
999                 obstack_blank (ob, sizeof (tree));
1000             }
1001
1002           obstack_finish (ob);
1003           TREE_VEC_ELT (method_vec, len) = decl;
1004           TREE_VEC_LENGTH (method_vec) = len + 1;
1005           CLASSTYPE_METHOD_VEC (type) = method_vec;
1006
1007           if (TYPE_BINFO_BASETYPES (type) && CLASSTYPE_BASELINK_VEC (type))
1008             {
1009               /* ??? May be better to know whether these can be extended?  */
1010               tree baselink_vec = CLASSTYPE_BASELINK_VEC (type);
1011
1012               TREE_VEC_LENGTH (baselink_vec) += 1;
1013               CLASSTYPE_BASELINK_VEC (type) = copy_node (baselink_vec);
1014               TREE_VEC_LENGTH (baselink_vec) -= 1;
1015
1016               TREE_VEC_ELT (CLASSTYPE_BASELINK_VEC (type), len) = 0;
1017             }
1018         }
1019     }
1020   DECL_CONTEXT (decl) = type;
1021   DECL_CLASS_CONTEXT (decl) = type;
1022
1023   pop_obstacks ();
1024 }
1025
1026 /* Subroutines of finish_struct.  */
1027
1028 /* Look through the list of fields for this struct, deleting
1029    duplicates as we go.  This must be recursive to handle
1030    anonymous unions.
1031
1032    FIELD is the field which may not appear anywhere in FIELDS.
1033    FIELD_PTR, if non-null, is the starting point at which
1034    chained deletions may take place.
1035    The value returned is the first acceptable entry found
1036    in FIELDS.
1037
1038    Note that anonymous fields which are not of UNION_TYPE are
1039    not duplicates, they are just anonymous fields.  This happens
1040    when we have unnamed bitfields, for example.  */
1041 static tree
1042 delete_duplicate_fields_1 (field, fields)
1043      tree field, fields;
1044 {
1045   tree x;
1046   tree prev = 0;
1047   if (DECL_NAME (field) == 0)
1048     {
1049       if (TREE_CODE (TREE_TYPE (field)) != UNION_TYPE)
1050         return fields;
1051
1052       for (x = TYPE_FIELDS (TREE_TYPE (field)); x; x = TREE_CHAIN (x))
1053         fields = delete_duplicate_fields_1 (x, fields);
1054       return fields;
1055     }
1056   else
1057     {
1058       for (x = fields; x; prev = x, x = TREE_CHAIN (x))
1059         {
1060           if (DECL_NAME (x) == 0)
1061             {
1062               if (TREE_CODE (TREE_TYPE (x)) != UNION_TYPE)
1063                 continue;
1064               TYPE_FIELDS (TREE_TYPE (x))
1065                 = delete_duplicate_fields_1 (field, TYPE_FIELDS (TREE_TYPE (x)));
1066               if (TYPE_FIELDS (TREE_TYPE (x)) == 0)
1067                 {
1068                   if (prev == 0)
1069                     fields = TREE_CHAIN (fields);
1070                   else
1071                     TREE_CHAIN (prev) = TREE_CHAIN (x);
1072                 }
1073             }
1074           else
1075             {
1076               if (DECL_NAME (field) == DECL_NAME (x))
1077                 {
1078                   if (TREE_CODE (field) == CONST_DECL
1079                       && TREE_CODE (x) == CONST_DECL)
1080                     cp_error_at ("duplicate enum value `%D'", x);
1081                   else if (TREE_CODE (field) == CONST_DECL
1082                            || TREE_CODE (x) == CONST_DECL)
1083                     cp_error_at ("duplicate field `%D' (as enum and non-enum)",
1084                                 x);
1085                   else if (TREE_CODE (field) == TYPE_DECL
1086                            && TREE_CODE (x) == TYPE_DECL)
1087                     cp_error_at ("duplicate nested type `%D'", x);
1088                   else if (TREE_CODE (field) == TYPE_DECL
1089                            || TREE_CODE (x) == TYPE_DECL)
1090                     cp_error_at ("duplicate field `%D' (as type and non-type)",
1091                                 x);
1092                   else
1093                     cp_error_at ("duplicate member `%D'", x);
1094                   if (prev == 0)
1095                     fields = TREE_CHAIN (fields);
1096                   else
1097                     TREE_CHAIN (prev) = TREE_CHAIN (x);
1098                 }
1099             }
1100         }
1101     }
1102   return fields;
1103 }
1104
1105 static void
1106 delete_duplicate_fields (fields)
1107      tree fields;
1108 {
1109   tree x;
1110   for (x = fields; x && TREE_CHAIN (x); x = TREE_CHAIN (x))
1111     TREE_CHAIN (x) = delete_duplicate_fields_1 (x, TREE_CHAIN (x));
1112 }
1113
1114 /* Change the access of FDECL to ACCESS in T.
1115    Return 1 if change was legit, otherwise return 0.  */
1116 static int
1117 alter_access (t, fdecl, access)
1118      tree t;
1119      tree fdecl;
1120      tree access;
1121 {
1122   tree elem = purpose_member (t, DECL_ACCESS (fdecl));
1123   if (elem && TREE_VALUE (elem) != access)
1124     {
1125       if (TREE_CODE (TREE_TYPE (fdecl)) == FUNCTION_DECL)
1126         {
1127           cp_error_at ("conflicting access specifications for method `%D', ignored", TREE_TYPE (fdecl));
1128         }
1129       else
1130         error ("conflicting access specifications for field `%s', ignored",
1131                IDENTIFIER_POINTER (DECL_NAME (fdecl)));
1132     }
1133   else if (TREE_PRIVATE (fdecl))
1134     {
1135       if (access != access_private_node)
1136         cp_error_at ("cannot make private `%D' non-private", fdecl);
1137       goto alter;
1138     }
1139   else if (TREE_PROTECTED (fdecl))
1140     {
1141       if (access != access_protected_node)
1142         cp_error_at ("cannot make protected `%D' non-protected", fdecl);
1143       goto alter;
1144     }
1145   /* ARM 11.3: an access declaration may not be used to restrict access
1146      to a member that is accessible in the base class.  */
1147   else if (access != access_public_node)
1148     cp_error_at ("cannot reduce access of public member `%D'", fdecl);
1149   else if (elem == NULL_TREE)
1150     {
1151     alter:
1152       DECL_ACCESS (fdecl) = tree_cons (t, access, DECL_ACCESS (fdecl));
1153       return 1;
1154     }
1155   return 0;
1156 }
1157
1158 /* Return the offset to the main vtable for a given base BINFO.  */
1159 tree
1160 get_vfield_offset (binfo)
1161      tree binfo;
1162 {
1163   return size_binop (PLUS_EXPR,
1164                      size_binop (FLOOR_DIV_EXPR,
1165                                  DECL_FIELD_BITPOS (CLASSTYPE_VFIELD (BINFO_TYPE (binfo))),
1166                                  size_int (BITS_PER_UNIT)),
1167                      BINFO_OFFSET (binfo));
1168 }
1169
1170 /* Get the offset to the start of the original binfo that we derived
1171    this binfo from.  If we find TYPE first, return the offset only
1172    that far.  The shortened search is useful because the this pointer
1173    on method calling is expected to point to a DECL_CONTEXT (fndecl)
1174    object, and not a baseclass of it.  */
1175 static tree
1176 get_derived_offset (binfo, type)
1177      tree binfo, type;
1178 {
1179   tree offset1 = get_vfield_offset (TYPE_BINFO (BINFO_TYPE (binfo)));
1180   tree offset2;
1181   int i;
1182   while (BINFO_BASETYPES (binfo)
1183          && (i=CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo))) != -1)
1184     {
1185       tree binfos = BINFO_BASETYPES (binfo);
1186       if (BINFO_TYPE (binfo) == type)
1187         break;
1188       binfo = TREE_VEC_ELT (binfos, i);
1189     }
1190   offset2 = get_vfield_offset (TYPE_BINFO (BINFO_TYPE (binfo)));
1191   return size_binop (MINUS_EXPR, offset1, offset2);
1192 }
1193
1194 /* If FOR_TYPE needs to reinitialize virtual function table pointers
1195    for TYPE's sub-objects, add such reinitializations to BASE_INIT_LIST.
1196    Returns BASE_INIT_LIST appropriately modified.  */
1197
1198 static tree
1199 maybe_fixup_vptrs (for_type, binfo, base_init_list)
1200      tree for_type, binfo, base_init_list;
1201 {
1202   /* Now reinitialize any slots that don't fall under our virtual
1203      function table pointer.  */
1204   tree vfields = CLASSTYPE_VFIELDS (BINFO_TYPE (binfo));
1205   while (vfields)
1206     {
1207       tree basetype = VF_NORMAL_VALUE (vfields)
1208         ? TYPE_MAIN_VARIANT (VF_NORMAL_VALUE (vfields))
1209           : VF_BASETYPE_VALUE (vfields);
1210
1211       tree base_binfo = get_binfo (basetype, for_type, 0);
1212       /* Punt until this is implemented. */
1213       if (1 /* BINFO_MODIFIED (base_binfo) */)
1214         {
1215           tree base_offset = get_vfield_offset (base_binfo);
1216           if (! tree_int_cst_equal (base_offset, get_vfield_offset (TYPE_BINFO (for_type)))
1217               && ! tree_int_cst_equal (base_offset, get_vfield_offset (binfo)))
1218             base_init_list = tree_cons (error_mark_node, base_binfo,
1219                                         base_init_list);
1220         }
1221       vfields = TREE_CHAIN (vfields);
1222     }
1223   return base_init_list;
1224 }
1225
1226 /* If TYPE does not have a constructor, then the compiler must
1227    manually deal with all of the initialization this type requires.
1228
1229    If a base initializer exists only to fill in the virtual function
1230    table pointer, then we mark that fact with the TREE_VIRTUAL bit.
1231    This way, we avoid multiple initializations of the same field by
1232    each virtual function table up the class hierarchy.
1233
1234    Virtual base class pointers are not initialized here.  They are
1235    initialized only at the "top level" of object creation.  If we
1236    initialized them here, we would have to skip a lot of work.  */
1237
1238 static void
1239 build_class_init_list (type)
1240      tree type;
1241 {
1242   tree base_init_list = NULL_TREE;
1243   tree member_init_list = NULL_TREE;
1244
1245   /* Since we build member_init_list and base_init_list using
1246      tree_cons, backwards fields the all through work.  */
1247   tree x;
1248   tree binfos = BINFO_BASETYPES (TYPE_BINFO (type));
1249   int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
1250
1251   for (x = TYPE_FIELDS (type); x; x = TREE_CHAIN (x))
1252     {
1253       if (TREE_CODE (x) != FIELD_DECL)
1254         continue;
1255
1256       if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (x))
1257           || DECL_INITIAL (x) != NULL_TREE)
1258         member_init_list = tree_cons (x, type, member_init_list);
1259     }
1260   member_init_list = nreverse (member_init_list);
1261
1262   /* We will end up doing this last.  Need special marker
1263      to avoid infinite regress.  */
1264   if (TYPE_VIRTUAL_P (type))
1265     {
1266       base_init_list = build_tree_list (error_mark_node, TYPE_BINFO (type));
1267       if (CLASSTYPE_NEEDS_VIRTUAL_REINIT (type) == 0)
1268         TREE_VALUE (base_init_list) = NULL_TREE;
1269       TREE_ADDRESSABLE (base_init_list) = 1;
1270     }
1271
1272   /* Each base class which needs to have initialization
1273      of some kind gets to make such requests known here.  */
1274   for (i = n_baseclasses-1; i >= 0; i--)
1275     {
1276       tree base_binfo = TREE_VEC_ELT (binfos, i);
1277       tree blist;
1278
1279       /* Don't initialize virtual baseclasses this way.  */
1280       if (TREE_VIA_VIRTUAL (base_binfo))
1281         continue;
1282
1283       if (TYPE_HAS_CONSTRUCTOR (BINFO_TYPE (base_binfo)))
1284         {
1285           /* ...and the last shall come first...  */
1286           base_init_list = maybe_fixup_vptrs (type, base_binfo, base_init_list);
1287           base_init_list = tree_cons (NULL_TREE, base_binfo, base_init_list);
1288           continue;
1289         }
1290
1291       if ((blist = CLASSTYPE_BASE_INIT_LIST (BINFO_TYPE (base_binfo))) == NULL_TREE)
1292         /* Nothing to initialize.  */
1293         continue;
1294
1295       /* ...ditto...  */
1296       base_init_list = maybe_fixup_vptrs (type, base_binfo, base_init_list);
1297
1298       /* This is normally true for single inheritance.
1299          The win is we can shrink the chain of initializations
1300          to be done by only converting to the actual type
1301          we are interested in.  */
1302       if (TREE_VALUE (blist)
1303           && TREE_CODE (TREE_VALUE (blist)) == TREE_VEC
1304           && tree_int_cst_equal (BINFO_OFFSET (base_binfo),
1305                                  BINFO_OFFSET (TREE_VALUE (blist))))
1306         {
1307           if (base_init_list)
1308             {
1309               /* Does it do more than just fill in a
1310                  virtual function table pointer?  */
1311               if (! TREE_ADDRESSABLE (blist))
1312                 base_init_list = build_tree_list (blist, base_init_list);
1313               /* Can we get by just with the virtual function table
1314                  pointer that it fills in?  */
1315               else if (TREE_ADDRESSABLE (base_init_list)
1316                        && TREE_VALUE (base_init_list) == 0)
1317                 base_init_list = blist;
1318               /* Maybe, but it is not obvious as the previous case.  */
1319               else if (! CLASSTYPE_NEEDS_VIRTUAL_REINIT (type))
1320                 {
1321                   tree last = tree_last (base_init_list);
1322                   while (TREE_VALUE (last)
1323                          && TREE_CODE (TREE_VALUE (last)) == TREE_LIST)
1324                     last = tree_last (TREE_VALUE (last));
1325                   if (TREE_VALUE (last) == 0)
1326                     base_init_list = build_tree_list (blist, base_init_list);
1327                 }
1328             }
1329           else
1330             base_init_list = blist;
1331         }
1332       else
1333         {
1334           /* The function expand_aggr_init knows how to do the
1335              initialization of `basetype' without getting
1336              an explicit `blist'.  */
1337           if (base_init_list)
1338             base_init_list = tree_cons (NULL_TREE, base_binfo, base_init_list);
1339           else
1340             base_init_list = CLASSTYPE_BINFO_AS_LIST (BINFO_TYPE (base_binfo));
1341         }
1342     }
1343
1344   if (base_init_list)
1345     if (member_init_list)
1346       CLASSTYPE_BASE_INIT_LIST (type) = build_tree_list (base_init_list, member_init_list);
1347     else
1348       CLASSTYPE_BASE_INIT_LIST (type) = base_init_list;
1349   else if (member_init_list)
1350     CLASSTYPE_BASE_INIT_LIST (type) = member_init_list;
1351 }
1352 \f
1353 struct base_info
1354 {
1355   int has_virtual;
1356   int max_has_virtual;
1357   int n_ancestors;
1358   tree vfield;
1359   tree vfields;
1360   char cant_have_default_ctor;
1361   char cant_have_const_ctor;
1362   char cant_synth_copy_ctor;
1363   char cant_synth_asn_ref;
1364   char no_const_asn_ref;
1365   char needs_virtual_dtor;
1366 };
1367
1368 /* Record information about type T derived from its base classes.
1369    Store most of that information in T itself, and place the
1370    remaining information in the struct BASE_INFO.
1371
1372    Propagate basetype offsets throughout the lattice.  Note that the
1373    lattice topped by T is really a pair: it's a DAG that gives the
1374    structure of the derivation hierarchy, and it's a list of the
1375    virtual baseclasses that appear anywhere in the DAG.  When a vbase
1376    type appears in the DAG, it's offset is 0, and it's children start
1377    their offsets from that point.  When a vbase type appears in the list,
1378    its offset is the offset it has in the hierarchy, and its children's
1379    offsets include that offset in theirs.
1380
1381    Returns the index of the first base class to have virtual functions,
1382    or -1 if no such base class.
1383
1384    Note that at this point TYPE_BINFO (t) != t_binfo.  */
1385
1386 static int
1387 finish_base_struct (t, b, t_binfo)
1388      tree t;
1389      struct base_info *b;
1390      tree t_binfo;
1391 {
1392   tree binfos = BINFO_BASETYPES (t_binfo);
1393   int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
1394   int first_vfn_base_index = -1;
1395   bzero ((char *) b, sizeof (struct base_info));
1396
1397   for (i = 0; i < n_baseclasses; i++)
1398     {
1399       tree base_binfo = TREE_VEC_ELT (binfos, i);
1400       tree basetype = BINFO_TYPE (base_binfo);
1401
1402       /* If the type of basetype is incomplete, then
1403          we already complained about that fact
1404          (and we should have fixed it up as well).  */
1405       if (TYPE_SIZE (basetype) == 0)
1406         {
1407           int j;
1408           /* The base type is of incomplete type.  It is
1409              probably best to pretend that it does not
1410              exist.  */
1411           if (i == n_baseclasses-1)
1412             TREE_VEC_ELT (binfos, i) = NULL_TREE;
1413           TREE_VEC_LENGTH (binfos) -= 1;
1414           n_baseclasses -= 1;
1415           for (j = i; j+1 < n_baseclasses; j++)
1416             TREE_VEC_ELT (binfos, j) = TREE_VEC_ELT (binfos, j+1);
1417         }
1418
1419       if (TYPE_HAS_INIT_REF (basetype)
1420           && !TYPE_HAS_CONST_INIT_REF (basetype))
1421         b->cant_have_const_ctor = 1;
1422       if (! TYPE_HAS_INIT_REF (basetype)
1423           || (TYPE_HAS_NONPUBLIC_CTOR (basetype) == 2
1424               && ! is_friend_type (t, basetype)))
1425         b->cant_synth_copy_ctor = 1;
1426
1427       if (TYPE_HAS_CONSTRUCTOR (basetype)
1428           && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype))
1429         {
1430           b->cant_have_default_ctor = 1;
1431           if (! TYPE_HAS_CONSTRUCTOR (t))
1432             {
1433               cp_pedwarn ("base `%T' with only non-default constructor",
1434                           basetype);
1435               cp_pedwarn ("in class without a constructor");
1436             }
1437         }
1438
1439       if (TYPE_HAS_ASSIGN_REF (basetype)
1440           && !TYPE_HAS_CONST_ASSIGN_REF (basetype))
1441         b->no_const_asn_ref = 1;
1442       if (! TYPE_HAS_ASSIGN_REF (basetype)
1443           || TYPE_HAS_ABSTRACT_ASSIGN_REF (basetype)
1444           || (TYPE_HAS_NONPUBLIC_ASSIGN_REF (basetype) == 2
1445               && ! is_friend_type (t, basetype)))
1446         b->cant_synth_asn_ref = 1;
1447
1448       b->n_ancestors += CLASSTYPE_N_SUPERCLASSES (basetype);
1449       TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (basetype);
1450       TYPE_NEEDS_DESTRUCTOR (t) |= TYPE_NEEDS_DESTRUCTOR (basetype);
1451       TYPE_HAS_COMPLEX_ASSIGN_REF (t) |= TYPE_HAS_COMPLEX_ASSIGN_REF (basetype);
1452       TYPE_HAS_COMPLEX_INIT_REF (t) |= TYPE_HAS_COMPLEX_INIT_REF (basetype);
1453
1454       TYPE_OVERLOADS_CALL_EXPR (t) |= TYPE_OVERLOADS_CALL_EXPR (basetype);
1455       TYPE_OVERLOADS_ARRAY_REF (t) |= TYPE_OVERLOADS_ARRAY_REF (basetype);
1456       TYPE_OVERLOADS_ARROW (t) |= TYPE_OVERLOADS_ARROW (basetype);
1457
1458       if (! TREE_VIA_VIRTUAL (base_binfo)
1459 #if 0
1460           /* This cannot be done, as prepare_fresh_vtable wants to modify
1461              binfos associated with vfields anywhere in the hierarchy, not
1462              just immediate base classes.  Due to unsharing, the compiler
1463              might consume 3% more memory on a real program.
1464              */
1465           && ! BINFO_OFFSET_ZEROP (base_binfo)
1466 #endif
1467           && BINFO_BASETYPES (base_binfo))
1468         {
1469           tree base_binfos = BINFO_BASETYPES (base_binfo);
1470           tree chain = NULL_TREE;
1471           int j;
1472
1473           /* Now unshare the structure beneath BASE_BINFO.  */
1474           for (j = TREE_VEC_LENGTH (base_binfos)-1;
1475                j >= 0; j--)
1476             {
1477               tree base_base_binfo = TREE_VEC_ELT (base_binfos, j);
1478               if (! TREE_VIA_VIRTUAL (base_base_binfo))
1479                 TREE_VEC_ELT (base_binfos, j)
1480                   = make_binfo (BINFO_OFFSET (base_base_binfo),
1481                                 base_base_binfo,
1482                                 BINFO_VTABLE (base_base_binfo),
1483                                 BINFO_VIRTUALS (base_base_binfo),
1484                                 chain);
1485               chain = TREE_VEC_ELT (base_binfos, j);
1486               TREE_VIA_PUBLIC (chain) = TREE_VIA_PUBLIC (base_base_binfo);
1487               TREE_VIA_PROTECTED (chain) = TREE_VIA_PROTECTED (base_base_binfo);
1488               BINFO_INHERITANCE_CHAIN (chain) = base_binfo;
1489             }
1490
1491           /* Completely unshare potentially shared data, and
1492              update what is ours.  */
1493           propagate_binfo_offsets (base_binfo, BINFO_OFFSET (base_binfo));
1494         }
1495
1496       if (! TREE_VIA_VIRTUAL (base_binfo))
1497         CLASSTYPE_N_SUPERCLASSES (t) += 1;
1498
1499       if (TYPE_VIRTUAL_P (basetype))
1500         {
1501           /* If there's going to be a destructor needed, make
1502              sure it will be virtual.  */
1503           b->needs_virtual_dtor = 1;
1504
1505           /* Don't borrow virtuals from virtual baseclasses.  */
1506           if (TREE_VIA_VIRTUAL (base_binfo))
1507             continue;
1508
1509           if (first_vfn_base_index < 0)
1510             {
1511               tree vfields;
1512               first_vfn_base_index = i;
1513
1514               /* Update these two, now that we know what vtable we are
1515                  going to extend.  This is so that we can add virtual
1516                  functions, and override them properly.  */
1517               BINFO_VTABLE (t_binfo) = TYPE_BINFO_VTABLE (basetype);
1518               BINFO_VIRTUALS (t_binfo) = TYPE_BINFO_VIRTUALS (basetype);
1519               b->has_virtual = CLASSTYPE_VSIZE (basetype);
1520               b->vfield = CLASSTYPE_VFIELD (basetype);
1521               b->vfields = copy_list (CLASSTYPE_VFIELDS (basetype));
1522               vfields = b->vfields;
1523               while (vfields)
1524                 {
1525                   if (VF_BINFO_VALUE (vfields) == NULL_TREE
1526                       || ! TREE_VIA_VIRTUAL (VF_BINFO_VALUE (vfields)))
1527                     {
1528                       tree value = VF_BASETYPE_VALUE (vfields);
1529                       if (DECL_NAME (CLASSTYPE_VFIELD (value))
1530                           == DECL_NAME (CLASSTYPE_VFIELD (basetype)))
1531                         VF_NORMAL_VALUE (b->vfields) = basetype;
1532                       else
1533                         VF_NORMAL_VALUE (b->vfields) = VF_NORMAL_VALUE (vfields);
1534                     }
1535                   vfields = TREE_CHAIN (vfields);
1536                 }
1537               CLASSTYPE_VFIELD (t) = b->vfield;
1538             }
1539           else
1540             {
1541               /* Only add unique vfields, and flatten them out as we go.  */
1542               tree vfields = CLASSTYPE_VFIELDS (basetype);
1543               while (vfields)
1544                 {
1545                   if (VF_BINFO_VALUE (vfields) == NULL_TREE
1546                       || ! TREE_VIA_VIRTUAL (VF_BINFO_VALUE (vfields)))
1547                     {
1548                       tree value = VF_BASETYPE_VALUE (vfields);
1549                       b->vfields = tree_cons (base_binfo, value, b->vfields);
1550                       if (DECL_NAME (CLASSTYPE_VFIELD (value))
1551                           == DECL_NAME (CLASSTYPE_VFIELD (basetype)))
1552                         VF_NORMAL_VALUE (b->vfields) = basetype;
1553                       else
1554                         VF_NORMAL_VALUE (b->vfields) = VF_NORMAL_VALUE (vfields);
1555                     }
1556                   vfields = TREE_CHAIN (vfields);
1557                 }
1558
1559               if (b->has_virtual == 0)
1560                 {
1561                   first_vfn_base_index = i;
1562
1563                   /* Update these two, now that we know what vtable we are
1564                      going to extend.  This is so that we can add virtual
1565                      functions, and override them properly.  */
1566                   BINFO_VTABLE (t_binfo) = TYPE_BINFO_VTABLE (basetype);
1567                   BINFO_VIRTUALS (t_binfo) = TYPE_BINFO_VIRTUALS (basetype);
1568                   b->has_virtual = CLASSTYPE_VSIZE (basetype);
1569                   b->vfield = CLASSTYPE_VFIELD (basetype);
1570                   CLASSTYPE_VFIELD (t) = b->vfield;
1571                   /* When we install the first one, set the VF_NORMAL_VALUE
1572                      to be the current class, as this it is the most derived
1573                      class.  Hopefully, this is not set to something else
1574                      later.  (mrs) */
1575                   vfields = b->vfields;
1576                   while (vfields)
1577                     {
1578                       if (DECL_NAME (CLASSTYPE_VFIELD (t))
1579                           == DECL_NAME (CLASSTYPE_VFIELD (basetype)))
1580                         {
1581                           VF_NORMAL_VALUE (vfields) = t;
1582                           /* There should only be one of them!  And it should
1583                              always be found, if we get into here.  (mrs)  */
1584                           break;
1585                         }
1586                       vfields = TREE_CHAIN (vfields);
1587                     }
1588                 }
1589             }
1590         }
1591     }
1592
1593   /* Must come after offsets are fixed for all bases.  */
1594   for (i = 0; i < n_baseclasses; i++)
1595     {
1596       tree base_binfo = TREE_VEC_ELT (binfos, i);
1597       tree basetype = BINFO_TYPE (base_binfo);
1598
1599       if (get_base_distance (basetype, t_binfo, 0, (tree*)0) == -2)
1600         {
1601           cp_warning ("direct base `%T' inaccessible in `%T' due to ambiguity",
1602                       basetype, t);
1603           b->cant_synth_asn_ref = 1;
1604           b->cant_synth_copy_ctor = 1;
1605         }
1606     }
1607   {
1608     tree v = get_vbase_types (t_binfo);
1609
1610     for (; v; v = TREE_CHAIN (v))
1611       {
1612         tree basetype = BINFO_TYPE (v);
1613         if (get_base_distance (basetype, t_binfo, 0, (tree*)0) == -2)
1614           {
1615             if (extra_warnings)
1616               cp_warning ("virtual base `%T' inaccessible in `%T' due to ambiguity",
1617                           basetype, t);
1618             b->cant_synth_asn_ref = 1;
1619             b->cant_synth_copy_ctor = 1;
1620           }
1621       }
1622   }    
1623
1624   {
1625     tree vfields;
1626     /* Find the base class with the largest number of virtual functions.  */
1627     for (vfields = b->vfields; vfields; vfields = TREE_CHAIN (vfields))
1628       {
1629         if (CLASSTYPE_VSIZE (VF_BASETYPE_VALUE (vfields)) > b->max_has_virtual)
1630           b->max_has_virtual = CLASSTYPE_VSIZE (VF_BASETYPE_VALUE (vfields));
1631         if (VF_DERIVED_VALUE (vfields)
1632             && CLASSTYPE_VSIZE (VF_DERIVED_VALUE (vfields)) > b->max_has_virtual)
1633           b->max_has_virtual = CLASSTYPE_VSIZE (VF_DERIVED_VALUE (vfields));
1634       }
1635   }
1636
1637   if (b->vfield == 0)
1638     /* If all virtual functions come only from virtual baseclasses.  */
1639     return -1;
1640   return first_vfn_base_index;
1641 }
1642
1643 static int
1644 typecode_p (type, code)
1645      tree type;
1646      enum tree_code code;
1647 {
1648   return (TREE_CODE (type) == code
1649           || (TREE_CODE (type) == REFERENCE_TYPE
1650               && TREE_CODE (TREE_TYPE (type)) == code));
1651 }
1652 \f
1653 /* Set memoizing fields and bits of T (and its variants) for later use.
1654    MAX_HAS_VIRTUAL is the largest size of any T's virtual function tables.  */
1655 static void
1656 finish_struct_bits (t, max_has_virtual)
1657      tree t;
1658      int max_has_virtual;
1659 {
1660   int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
1661   tree method_vec = CLASSTYPE_METHOD_VEC (t);
1662
1663   /* Fix up variants (if any).  */
1664   tree variants = TYPE_NEXT_VARIANT (t);
1665   while (variants)
1666     {
1667       /* These fields are in the _TYPE part of the node, not in
1668          the TYPE_LANG_SPECIFIC component, so they are not shared.  */
1669       TYPE_HAS_CONSTRUCTOR (variants) = TYPE_HAS_CONSTRUCTOR (t);
1670       TYPE_HAS_DESTRUCTOR (variants) = TYPE_HAS_DESTRUCTOR (t);
1671       TYPE_NEEDS_CONSTRUCTING (variants) = TYPE_NEEDS_CONSTRUCTING (t);
1672       TYPE_NEEDS_DESTRUCTOR (variants) = TYPE_NEEDS_DESTRUCTOR (t);
1673
1674       TYPE_USES_COMPLEX_INHERITANCE (variants) = TYPE_USES_COMPLEX_INHERITANCE (t);
1675       TYPE_VIRTUAL_P (variants) = TYPE_VIRTUAL_P (t);
1676       TYPE_USES_VIRTUAL_BASECLASSES (variants) = TYPE_USES_VIRTUAL_BASECLASSES (t);
1677       /* Copy whatever these are holding today.  */
1678       TYPE_MIN_VALUE (variants) = TYPE_MIN_VALUE (t);
1679       TYPE_MAX_VALUE (variants) = TYPE_MAX_VALUE (t);
1680       variants = TYPE_NEXT_VARIANT (variants);
1681     }
1682
1683   if (n_baseclasses && max_has_virtual)
1684     {
1685       /* Done by `finish_struct' for classes without baseclasses.  */
1686       int might_have_abstract_virtuals = CLASSTYPE_ABSTRACT_VIRTUALS (t) != 0;
1687       tree binfos = TYPE_BINFO_BASETYPES (t);
1688       for (i = n_baseclasses-1; i >= 0; i--)
1689         {
1690           might_have_abstract_virtuals
1691             |= (CLASSTYPE_ABSTRACT_VIRTUALS (BINFO_TYPE (TREE_VEC_ELT (binfos, i))) != 0);
1692           if (might_have_abstract_virtuals)
1693             break;
1694         }
1695       if (might_have_abstract_virtuals)
1696         {
1697           /* We use error_mark_node from override_one_vtable to signal
1698              an artificial abstract. */
1699           if (CLASSTYPE_ABSTRACT_VIRTUALS (t) == error_mark_node)
1700             CLASSTYPE_ABSTRACT_VIRTUALS (t) = NULL_TREE;
1701           CLASSTYPE_ABSTRACT_VIRTUALS (t) = get_abstract_virtuals (t);
1702         }
1703     }
1704
1705   if (n_baseclasses)
1706     {
1707       /* Notice whether this class has type conversion functions defined.  */
1708       tree binfo = TYPE_BINFO (t);
1709       tree binfos = BINFO_BASETYPES (binfo);
1710       tree basetype;
1711
1712       for (i = n_baseclasses-1; i >= 0; i--)
1713         {
1714           basetype = BINFO_TYPE (TREE_VEC_ELT (binfos, i));
1715
1716           if (TYPE_HAS_CONVERSION (basetype))
1717             {
1718               TYPE_HAS_CONVERSION (t) = 1;
1719               TYPE_HAS_INT_CONVERSION (t) |= TYPE_HAS_INT_CONVERSION (basetype);
1720               TYPE_HAS_REAL_CONVERSION (t) |= TYPE_HAS_REAL_CONVERSION (basetype);
1721             }
1722           if (CLASSTYPE_MAX_DEPTH (basetype) >= CLASSTYPE_MAX_DEPTH (t))
1723             CLASSTYPE_MAX_DEPTH (t) = CLASSTYPE_MAX_DEPTH (basetype) + 1;
1724         }
1725     }
1726
1727   /* If this type has a copy constructor, force its mode to be BLKmode, and
1728      force its TREE_ADDRESSABLE bit to be nonzero.  This will cause it to
1729      be passed by invisible reference and prevent it from being returned in
1730      a register.
1731
1732      Also do this if the class has BLKmode but can still be returned in
1733      registers, since function_cannot_inline_p won't let us inline
1734      functions returning such a type.  This affects the HP-PA.  */
1735   if (! TYPE_HAS_TRIVIAL_INIT_REF (t)
1736       || (TYPE_MODE (t) == BLKmode && ! aggregate_value_p (t)
1737           && CLASSTYPE_NON_AGGREGATE (t)))
1738     {
1739       tree variants;
1740       if (TREE_CODE (TYPE_NAME (t)) == TYPE_DECL)
1741         DECL_MODE (TYPE_NAME (t)) = BLKmode;
1742       for (variants = t; variants; variants = TYPE_NEXT_VARIANT (variants))
1743         {
1744           TYPE_MODE (variants) = BLKmode;
1745           TREE_ADDRESSABLE (variants) = 1;
1746         }
1747     }
1748 }
1749
1750 /* Add FN to the method_vec growing on the class_obstack.  Used by
1751    finish_struct_methods.  */
1752 static void
1753 grow_method (fn, method_vec_ptr)
1754      tree fn;
1755      tree *method_vec_ptr;
1756 {
1757   tree method_vec = (tree)obstack_base (&class_obstack);
1758   tree *testp = &TREE_VEC_ELT (method_vec, 0);
1759   if (*testp == NULL_TREE)
1760     testp++;
1761   while (((HOST_WIDE_INT) testp
1762           < (HOST_WIDE_INT) obstack_next_free (&class_obstack))
1763          && DECL_NAME (*testp) != DECL_NAME (fn))
1764     testp++;
1765   if ((HOST_WIDE_INT) testp
1766       < (HOST_WIDE_INT) obstack_next_free (&class_obstack))
1767     {
1768       tree x, prev_x;
1769
1770       for (x = *testp; x; x = DECL_CHAIN (x))
1771         {
1772           if (DECL_NAME (fn) == ansi_opname[(int) DELETE_EXPR]
1773               || DECL_NAME (fn) == ansi_opname[(int) VEC_DELETE_EXPR])
1774             {
1775               /* ANSI C++ June 5 1992 WP 12.5.5.1 */
1776               cp_error_at ("`%D' overloaded", fn);
1777               cp_error_at ("previous declaration as `%D' here", x);
1778             }
1779           if (DECL_ASSEMBLER_NAME (fn)==DECL_ASSEMBLER_NAME (x))
1780             {
1781               /* We complain about multiple destructors on sight,
1782                  so we do not repeat the warning here.  Friend-friend
1783                  ambiguities are warned about outside this loop.  */
1784               if (!DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (fn)))
1785                 cp_error_at ("ambiguous method `%#D' in structure", fn);
1786               break;
1787             }
1788           prev_x = x;
1789         }
1790       if (x == 0)
1791         {
1792           if (*testp)
1793             DECL_CHAIN (prev_x) = fn;
1794           else
1795             *testp = fn;
1796         }
1797     }
1798   else
1799     {
1800       obstack_ptr_grow (&class_obstack, fn);
1801       *method_vec_ptr = (tree)obstack_base (&class_obstack);
1802     }
1803 }
1804
1805 /* Warn about duplicate methods in fn_fields.  Also compact method
1806    lists so that lookup can be made faster.
1807
1808    Algorithm: Outer loop builds lists by method name.  Inner loop
1809    checks for redundant method names within a list.
1810
1811    Data Structure: List of method lists.  The outer list is a
1812    TREE_LIST, whose TREE_PURPOSE field is the field name and the
1813    TREE_VALUE is the DECL_CHAIN of the FUNCTION_DECLs.  TREE_CHAIN
1814    links the entire list of methods for TYPE_METHODS.  Friends are
1815    chained in the same way as member functions (? TREE_CHAIN or
1816    DECL_CHAIN), but they live in the TREE_TYPE field of the outer
1817    list.  That allows them to be quickly deleted, and requires no
1818    extra storage.
1819
1820    If there are any constructors/destructors, they are moved to the
1821    front of the list.  This makes pushclass more efficient.
1822
1823    We also link each field which has shares a name with its baseclass
1824    to the head of the list of fields for that base class.  This allows
1825    us to reduce search time in places like `build_method_call' to
1826    consider only reasonably likely functions.  */
1827
1828 tree
1829 finish_struct_methods (t, fn_fields, nonprivate_method)
1830      tree t;
1831      tree fn_fields;
1832      int nonprivate_method;
1833 {
1834   tree method_vec;
1835   tree save_fn_fields = fn_fields;
1836   tree name = constructor_name (t);
1837   int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
1838
1839   /* Now prepare to gather fn_fields into vector.  */
1840   struct obstack *ambient_obstack = current_obstack;
1841   current_obstack = &class_obstack;
1842   method_vec = make_node (TREE_VEC);
1843   /* Room has been saved for constructors and destructors.  */
1844   current_obstack = ambient_obstack;
1845   /* Now make this a live vector.  */
1846   obstack_free (&class_obstack, method_vec);
1847   obstack_blank (&class_obstack, sizeof (struct tree_vec));
1848
1849   /* First fill in entry 0 with the constructors, and the next few with
1850      type conversion operators (if any).  */
1851
1852   for (; fn_fields; fn_fields = TREE_CHAIN (fn_fields))
1853     {
1854       tree fn_name = DECL_NAME (fn_fields);
1855       if (fn_name == NULL_TREE)
1856         fn_name = name;
1857
1858       /* Clear out this flag.
1859
1860          @@ Doug may figure out how to break
1861          @@ this with nested classes and friends.  */
1862       DECL_IN_AGGR_P (fn_fields) = 0;
1863
1864       /* Note here that a copy ctor is private, so we don't dare generate
1865          a default copy constructor for a class that has a member
1866          of this type without making sure they have access to it.  */
1867       if (fn_name == name)
1868         {
1869           tree parmtypes = FUNCTION_ARG_CHAIN (fn_fields);
1870           tree parmtype = parmtypes ? TREE_VALUE (parmtypes) : void_type_node;
1871           
1872           if (TREE_CODE (parmtype) == REFERENCE_TYPE
1873               && TYPE_MAIN_VARIANT (TREE_TYPE (parmtype)) == t)
1874             {
1875               if (TREE_CHAIN (parmtypes) == NULL_TREE
1876                   || TREE_CHAIN (parmtypes) == void_list_node
1877                   || TREE_PURPOSE (TREE_CHAIN (parmtypes)))
1878                 {
1879                   if (TREE_PROTECTED (fn_fields))
1880                     TYPE_HAS_NONPUBLIC_CTOR (t) = 1;
1881                   else if (TREE_PRIVATE (fn_fields))
1882                     TYPE_HAS_NONPUBLIC_CTOR (t) = 2;
1883                 }
1884             }
1885           /* Constructors are handled easily in search routines.  */
1886           DECL_CHAIN (fn_fields) = TREE_VEC_ELT (method_vec, 0);
1887           TREE_VEC_ELT (method_vec, 0) = fn_fields;
1888         }
1889       else if (IDENTIFIER_TYPENAME_P (fn_name))
1890         {
1891           tree return_type = TREE_TYPE (TREE_TYPE (fn_fields));
1892
1893           if (typecode_p (return_type, INTEGER_TYPE)
1894               || typecode_p (return_type, BOOLEAN_TYPE)
1895               || typecode_p (return_type, ENUMERAL_TYPE))
1896             TYPE_HAS_INT_CONVERSION (t) = 1;
1897           else if (typecode_p (return_type, REAL_TYPE))
1898             TYPE_HAS_REAL_CONVERSION (t) = 1;
1899
1900           grow_method (fn_fields, &method_vec);
1901         }
1902     }
1903
1904   fn_fields = save_fn_fields;
1905   for (; fn_fields; fn_fields = TREE_CHAIN (fn_fields))
1906     {
1907       tree fn_name = DECL_NAME (fn_fields);
1908       if (fn_name == NULL_TREE)
1909         fn_name = name;
1910
1911       if (fn_name == name || IDENTIFIER_TYPENAME_P (fn_name))
1912         continue;
1913
1914       if (fn_name == ansi_opname[(int) MODIFY_EXPR])
1915         {
1916           tree parmtype = TREE_VALUE (FUNCTION_ARG_CHAIN (fn_fields));
1917
1918           if (copy_assignment_arg_p (parmtype, DECL_VIRTUAL_P (fn_fields)))
1919             {
1920               if (TREE_PROTECTED (fn_fields))
1921                 TYPE_HAS_NONPUBLIC_ASSIGN_REF (t) = 1;
1922               else if (TREE_PRIVATE (fn_fields))
1923                 TYPE_HAS_NONPUBLIC_ASSIGN_REF (t) = 2;
1924             }
1925         }
1926
1927       grow_method (fn_fields, &method_vec);
1928     }
1929
1930   TREE_VEC_LENGTH (method_vec) = (tree *)obstack_next_free (&class_obstack)
1931     - (&TREE_VEC_ELT (method_vec, 0));
1932   obstack_finish (&class_obstack);
1933   CLASSTYPE_METHOD_VEC (t) = method_vec;
1934
1935   if (nonprivate_method == 0
1936       && CLASSTYPE_FRIEND_CLASSES (t) == NULL_TREE
1937       && DECL_FRIENDLIST (TYPE_NAME (t)) == NULL_TREE)
1938     {
1939       tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
1940       for (i = 0; i < n_baseclasses; i++)
1941         if (TREE_VIA_PUBLIC (TREE_VEC_ELT (binfos, i))
1942             || TREE_VIA_PROTECTED (TREE_VEC_ELT (binfos, i)))
1943           {
1944             nonprivate_method = 1;
1945             break;
1946           }
1947       if (nonprivate_method == 0)
1948         cp_warning ("all member functions in class `%T' are private", t);
1949     }
1950
1951   /* If there are constructors (and destructors), they are at the
1952      front.  Place destructors at very front.  Also warn if all
1953      constructors and/or destructors are private (in which case this
1954      class is effectively unusable.  */
1955   if (TYPE_HAS_DESTRUCTOR (t))
1956     {
1957       tree dtor, prev;
1958
1959       for (dtor = TREE_VEC_ELT (method_vec, 0);
1960            dtor;
1961            prev = dtor, dtor = DECL_CHAIN (dtor))
1962         {
1963           if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (dtor)))
1964             {
1965               if (TREE_PRIVATE (dtor)
1966                   && CLASSTYPE_FRIEND_CLASSES (t) == NULL_TREE
1967                   && DECL_FRIENDLIST (TYPE_NAME (t)) == NULL_TREE
1968                   && warn_ctor_dtor_privacy)
1969                 cp_warning ("`%#T' only defines a private destructor and has no friends",
1970                             t);
1971               break;
1972             }
1973         }
1974
1975       /* Wild parse errors can cause this to happen.  */
1976       if (dtor == NULL_TREE)
1977         TYPE_HAS_DESTRUCTOR (t) = 0;
1978       else if (dtor != TREE_VEC_ELT (method_vec, 0))
1979         {
1980           DECL_CHAIN (prev) = DECL_CHAIN (dtor);
1981           DECL_CHAIN (dtor) = TREE_VEC_ELT (method_vec, 0);
1982           TREE_VEC_ELT (method_vec, 0) = dtor;
1983         }
1984     }
1985
1986   /* Now for each member function (except for constructors and
1987      destructors), compute where member functions of the same
1988      name reside in base classes.  */
1989   if (n_baseclasses != 0
1990       && TREE_VEC_LENGTH (method_vec) > 1)
1991     {
1992       int len = TREE_VEC_LENGTH (method_vec);
1993       tree baselink_vec = make_tree_vec (len);
1994       int any_links = 0;
1995       tree baselink_binfo = build_tree_list (NULL_TREE, TYPE_BINFO (t));
1996
1997       for (i = 1; i < len; i++)
1998         {
1999           TREE_VEC_ELT (baselink_vec, i)
2000             = get_baselinks (baselink_binfo, t, DECL_NAME (TREE_VEC_ELT (method_vec, i)));
2001           if (TREE_VEC_ELT (baselink_vec, i) != 0)
2002             any_links = 1;
2003         }
2004       if (any_links != 0)
2005         CLASSTYPE_BASELINK_VEC (t) = baselink_vec;
2006       else
2007         obstack_free (current_obstack, baselink_vec);
2008     }
2009
2010 #if 0
2011   /* Now add the methods to the TYPE_METHODS of T, arranged in a chain.  */
2012   {
2013     tree x, last_x = NULL_TREE;
2014     int limit = TREE_VEC_LENGTH (method_vec);
2015
2016     for (i = 1; i < limit; i++)
2017       {
2018         for (x = TREE_VEC_ELT (method_vec, i); x; x = DECL_CHAIN (x))
2019           {
2020             if (last_x != NULL_TREE)
2021               TREE_CHAIN (last_x) = x;
2022             last_x = x;
2023           }
2024       }
2025
2026     /* Put ctors and dtors at the front of the list.  */
2027     x = TREE_VEC_ELT (method_vec, 0);
2028     if (x)
2029       {
2030         while (DECL_CHAIN (x))
2031           {
2032             /* Let's avoid being circular about this.  */
2033             if (x == DECL_CHAIN (x))
2034               break;
2035             TREE_CHAIN (x) = DECL_CHAIN (x);
2036             x = DECL_CHAIN (x);
2037           }
2038         if (TREE_VEC_LENGTH (method_vec) > 1)
2039           TREE_CHAIN (x) = TREE_VEC_ELT (method_vec, 1);
2040         else
2041           TREE_CHAIN (x) = NULL_TREE;
2042       }
2043   }
2044
2045   TYPE_METHODS (t) = method_vec;
2046 #endif
2047
2048   return method_vec;
2049 }
2050
2051 /* Emit error when a duplicate definition of a type is seen.  Patch up. */
2052
2053 void
2054 duplicate_tag_error (t)
2055      tree t;
2056 {
2057   cp_error ("redefinition of `%#T'", t);
2058   cp_error_at ("previous definition here", t);
2059
2060   /* Pretend we haven't defined this type.  */
2061
2062   /* All of the component_decl's were TREE_CHAINed together in the parser.
2063      finish_struct_methods walks these chains and assembles all methods with
2064      the same base name into DECL_CHAINs. Now we don't need the parser chains
2065      anymore, so we unravel them.
2066    */
2067   /*
2068    * This used to be in finish_struct, but it turns out that the
2069    * TREE_CHAIN is used by dbxout_type_methods and perhaps some other things...
2070    */
2071   if (CLASSTYPE_METHOD_VEC(t)) 
2072     {
2073       tree tv = CLASSTYPE_METHOD_VEC(t);
2074       int i, len  = TREE_VEC_LENGTH (tv);
2075       for (i = 0; i < len; i++)
2076         {
2077           tree unchain = TREE_VEC_ELT (tv, i);
2078           while (unchain != NULL_TREE) 
2079             {
2080               TREE_CHAIN (unchain) = NULL_TREE;
2081               unchain = DECL_CHAIN(unchain);
2082             }
2083         }
2084     }
2085
2086   if (TYPE_LANG_SPECIFIC (t))
2087     {
2088       tree as_list = CLASSTYPE_AS_LIST (t);
2089       tree binfo = TYPE_BINFO (t);
2090       tree binfo_as_list = CLASSTYPE_BINFO_AS_LIST (t);
2091       int interface_only = CLASSTYPE_INTERFACE_ONLY (t);
2092       int interface_unknown = CLASSTYPE_INTERFACE_UNKNOWN (t);
2093
2094       bzero ((char *) TYPE_LANG_SPECIFIC (t), sizeof (struct lang_type));
2095       BINFO_BASETYPES(binfo) = NULL_TREE;
2096
2097       CLASSTYPE_AS_LIST (t) = as_list;
2098       TYPE_BINFO (t) = binfo;
2099       CLASSTYPE_BINFO_AS_LIST (t) = binfo_as_list;
2100       CLASSTYPE_INTERFACE_ONLY (t) = interface_only;
2101       SET_CLASSTYPE_INTERFACE_UNKNOWN_X (t, interface_unknown);
2102       CLASSTYPE_VBASE_SIZE (t) = integer_zero_node;
2103       TYPE_REDEFINED (t) = 1;
2104     }
2105   TYPE_SIZE (t) = NULL_TREE;
2106   TYPE_MODE (t) = VOIDmode;
2107   TYPE_FIELDS (t) = NULL_TREE;
2108   TYPE_METHODS (t) = NULL_TREE;
2109   TYPE_VFIELD (t) = NULL_TREE;
2110   TYPE_CONTEXT (t) = NULL_TREE;
2111 }
2112
2113 /* finish up all new vtables. */
2114 static void
2115 finish_vtbls (binfo, do_self, t)
2116      tree binfo, t;
2117      int do_self;
2118 {
2119   tree binfos = BINFO_BASETYPES (binfo);
2120   int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2121
2122   /* Should we use something besides CLASSTYPE_VFIELDS? */
2123   if (do_self && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
2124     {
2125       if (BINFO_NEW_VTABLE_MARKED (binfo))
2126         {
2127           tree decl, context;
2128
2129           decl = BINFO_VTABLE (binfo);
2130           context = DECL_CONTEXT (decl);
2131           DECL_CONTEXT (decl) = 0;
2132           if (write_virtuals >= 0
2133               && DECL_INITIAL (decl) != BINFO_VIRTUALS (binfo))
2134             DECL_INITIAL (decl) = build_nt (CONSTRUCTOR, NULL_TREE,
2135                                             BINFO_VIRTUALS (binfo));
2136           cp_finish_decl (decl, DECL_INITIAL (decl), NULL_TREE, 0, 0);
2137           DECL_CONTEXT (decl) = context;
2138         }
2139       CLEAR_BINFO_NEW_VTABLE_MARKED (binfo);
2140     }
2141
2142   for (i = 0; i < n_baselinks; i++)
2143     {
2144       tree base_binfo = TREE_VEC_ELT (binfos, i);
2145       int is_not_base_vtable =
2146         i != CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo));
2147       if (TREE_VIA_VIRTUAL (base_binfo))
2148         {
2149           base_binfo = binfo_member (BINFO_TYPE (base_binfo), CLASSTYPE_VBASECLASSES (t));
2150         }
2151       finish_vtbls (base_binfo, is_not_base_vtable, t);
2152     }
2153 }
2154
2155 /* True if we should override the given BASE_FNDECL with the given
2156    FNDECL.  */
2157 static int
2158 overrides (fndecl, base_fndecl)
2159      tree fndecl, base_fndecl;
2160 {
2161   /* Destructors have special names. */
2162   if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (base_fndecl)) &&
2163       DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (fndecl)))
2164     return 1;
2165   if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (base_fndecl)) ||
2166       DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (fndecl)))
2167     return 0;
2168   if (DECL_NAME (fndecl) == DECL_NAME (base_fndecl))
2169     {
2170       tree rettype, base_rettype, types, base_types;
2171 #if 0
2172       retypes = TREE_TYPE (TREE_TYPE (fndecl));
2173       base_retypes = TREE_TYPE (TREE_TYPE (base_fndecl));
2174 #endif
2175       types = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
2176       base_types = TYPE_ARG_TYPES (TREE_TYPE (base_fndecl));
2177       if ((TYPE_READONLY (TREE_TYPE (TREE_VALUE (base_types)))
2178            == TYPE_READONLY (TREE_TYPE (TREE_VALUE (types))))
2179           && compparms (TREE_CHAIN (base_types), TREE_CHAIN (types), 3))
2180         return 1;
2181     }
2182   return 0;
2183 }
2184
2185 static tree
2186 get_class_offset_1 (parent, binfo, context, t, fndecl)
2187      tree parent, binfo, context, t, fndecl;
2188 {
2189   tree binfos = BINFO_BASETYPES (binfo);
2190   int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2191   tree rval = NULL_TREE;
2192
2193   if (binfo == parent)
2194     return error_mark_node;
2195
2196   for (i = 0; i < n_baselinks; i++)
2197     {
2198       tree base_binfo = TREE_VEC_ELT (binfos, i);
2199       tree nrval;
2200
2201       if (TREE_VIA_VIRTUAL (base_binfo))
2202         base_binfo = binfo_member (BINFO_TYPE (base_binfo),
2203                                    CLASSTYPE_VBASECLASSES (t));
2204       nrval = get_class_offset_1 (parent, base_binfo, context, t, fndecl);
2205       /* See if we have a new value */
2206       if (nrval && (nrval != error_mark_node || rval==0))
2207         {
2208           /* Only compare if we have two offsets */
2209           if (rval && rval != error_mark_node
2210               && ! tree_int_cst_equal (nrval, rval))
2211             {
2212               /* Only give error if the two offsets are different */
2213               error ("every virtual function must have a unique final overrider");
2214               cp_error ("  found two (or more) `%T' class subobjects in `%T'", context, t);
2215               cp_error ("  with virtual `%D' from virtual base class", fndecl);
2216               return rval;
2217             }
2218           rval = nrval;
2219         }
2220         
2221       if (rval && BINFO_TYPE (binfo) == context)
2222         {
2223           my_friendly_assert (rval == error_mark_node
2224                               || tree_int_cst_equal (rval, BINFO_OFFSET (binfo)), 999);
2225           rval = BINFO_OFFSET (binfo);
2226         }
2227     }
2228   return rval;
2229 }
2230
2231 /* Get the offset to the CONTEXT subobject that is related to the
2232    given BINFO.  */
2233 static tree
2234 get_class_offset (context, t, binfo, fndecl)
2235      tree context, t, binfo, fndecl;
2236 {
2237   tree first_binfo = binfo;
2238   tree offset;
2239   int i;
2240
2241   if (context == t)
2242     return integer_zero_node;
2243
2244   if (BINFO_TYPE (binfo) == context)
2245     return BINFO_OFFSET (binfo);
2246
2247   /* Check less derived binfos first.  */
2248   while (BINFO_BASETYPES (binfo)
2249          && (i=CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo))) != -1)
2250     {
2251       tree binfos = BINFO_BASETYPES (binfo);
2252       binfo = TREE_VEC_ELT (binfos, i);
2253       if (BINFO_TYPE (binfo) == context)
2254         return BINFO_OFFSET (binfo);
2255     }
2256
2257   /* Ok, not found in the less derived binfos, now check the more
2258      derived binfos. */
2259   offset = get_class_offset_1 (first_binfo, TYPE_BINFO (t), context, t, fndecl);
2260   if (offset==0 || TREE_CODE (offset) != INTEGER_CST)
2261     my_friendly_abort (999);    /* we have to find it.  */
2262   return offset;
2263 }
2264
2265 /* Skip RTTI information at the front of the virtual list.  */
2266 unsigned HOST_WIDE_INT
2267 skip_rtti_stuff (virtuals)
2268      tree *virtuals;
2269 {
2270   int n;
2271
2272   n = 0;
2273   if (*virtuals)
2274     {
2275       /* We always reserve a slot for the offset/tdesc entry.  */
2276       ++n;
2277       *virtuals = TREE_CHAIN (*virtuals);
2278     }
2279   if (flag_vtable_thunks && *virtuals)
2280     {
2281       /* The second slot is reserved for the tdesc pointer when thunks
2282          are used.  */
2283       ++n;
2284       *virtuals = TREE_CHAIN (*virtuals);
2285     }
2286   return n;
2287 }
2288
2289 static void
2290 modify_one_vtable (binfo, t, fndecl, pfn)
2291      tree binfo, t, fndecl, pfn;
2292 {
2293   tree virtuals = BINFO_VIRTUALS (binfo);
2294   tree old_rtti;
2295   unsigned HOST_WIDE_INT n;
2296   
2297   /* update rtti entry */
2298   if (flag_rtti)
2299     {
2300       if (binfo == TYPE_BINFO (t))
2301         {
2302           if (! BINFO_NEW_VTABLE_MARKED (binfo))
2303             build_vtable (TYPE_BINFO (DECL_CONTEXT (CLASSTYPE_VFIELD (t))), t);
2304         }
2305       else
2306         {
2307           if (! BINFO_NEW_VTABLE_MARKED (binfo))
2308             prepare_fresh_vtable (binfo, t);
2309         }
2310     }
2311   if (fndecl == NULL_TREE)
2312     return;
2313
2314   n = skip_rtti_stuff (&virtuals);
2315
2316   while (virtuals)
2317     {
2318       tree current_fndecl = TREE_VALUE (virtuals);
2319       current_fndecl = FNADDR_FROM_VTABLE_ENTRY (current_fndecl);
2320       current_fndecl = TREE_OPERAND (current_fndecl, 0);
2321       if (current_fndecl && overrides (fndecl, current_fndecl))
2322         {
2323           tree base_offset, offset;
2324           tree context = DECL_CLASS_CONTEXT (fndecl);
2325           tree vfield = CLASSTYPE_VFIELD (t);
2326           tree this_offset;
2327
2328           offset = get_class_offset (context, t, binfo, fndecl);
2329
2330           /* Find the right offset for the this pointer based on the
2331              base class we just found.  We have to take into
2332              consideration the virtual base class pointers that we
2333              stick in before the virtual function table pointer.
2334
2335              Also, we want just the delta between the most base class
2336              that we derived this vfield from and us.  */
2337           base_offset = size_binop (PLUS_EXPR,
2338                                     get_derived_offset (binfo, DECL_CONTEXT (current_fndecl)),
2339                                     BINFO_OFFSET (binfo));
2340           this_offset = size_binop (MINUS_EXPR, offset, base_offset);
2341
2342           /* Make sure we can modify the derived association with immunity.  */
2343           if (TREE_USED (binfo))
2344             my_friendly_assert (0, 999);
2345
2346           if (binfo == TYPE_BINFO (t))
2347             {
2348               /* In this case, it is *type*'s vtable we are modifying.
2349                  We start with the approximation that it's vtable is that
2350                  of the immediate base class.  */
2351               if (! BINFO_NEW_VTABLE_MARKED (binfo))
2352                 build_vtable (TYPE_BINFO (DECL_CONTEXT (vfield)), t);
2353             }
2354           else
2355             {
2356               /* This is our very own copy of `basetype' to play with.
2357                  Later, we will fill in all the virtual functions
2358                  that override the virtual functions in these base classes
2359                  which are not defined by the current type.  */
2360               if (! BINFO_NEW_VTABLE_MARKED (binfo))
2361                 prepare_fresh_vtable (binfo, t);
2362             }
2363
2364 #ifdef NOTQUITE
2365           cp_warning ("in %D", DECL_NAME (BINFO_VTABLE (binfo)));
2366 #endif
2367           modify_vtable_entry (get_vtable_entry_n (BINFO_VIRTUALS (binfo), n),
2368                                build_vtable_entry (this_offset, pfn),
2369                                fndecl);
2370         }
2371       ++n;
2372       virtuals = TREE_CHAIN (virtuals);
2373     }
2374 }
2375
2376 /* These are the ones that are not through virtual base classes. */
2377 static void
2378 modify_all_direct_vtables (binfo, do_self, t, fndecl, pfn)
2379      tree binfo, t, fndecl, pfn;
2380      int do_self;
2381 {
2382   tree binfos = BINFO_BASETYPES (binfo);
2383   int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2384
2385   /* Should we use something besides CLASSTYPE_VFIELDS? */
2386   if (do_self && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
2387     {
2388       modify_one_vtable (binfo, t, fndecl, pfn);
2389     }
2390
2391   for (i = 0; i < n_baselinks; i++)
2392     {
2393       tree base_binfo = TREE_VEC_ELT (binfos, i);
2394       int is_not_base_vtable =
2395         i != CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo));
2396       if (! TREE_VIA_VIRTUAL (base_binfo))
2397         modify_all_direct_vtables (base_binfo, is_not_base_vtable, t, fndecl, pfn);
2398     }
2399 }
2400
2401 /* Fixup all the delta entries in this one vtable that need updating.  */
2402 static void
2403 fixup_vtable_deltas1 (binfo, t)
2404      tree binfo, t;
2405 {
2406   tree virtuals = BINFO_VIRTUALS (binfo);
2407   unsigned HOST_WIDE_INT n;
2408   
2409   n = skip_rtti_stuff (&virtuals);
2410
2411   while (virtuals)
2412     {
2413       tree fndecl = TREE_VALUE (virtuals);
2414       tree pfn = FNADDR_FROM_VTABLE_ENTRY (fndecl);
2415       tree delta = DELTA_FROM_VTABLE_ENTRY (fndecl);
2416       fndecl = TREE_OPERAND (pfn, 0);
2417       if (fndecl)
2418         {
2419           tree base_offset, offset;
2420           tree context = DECL_CLASS_CONTEXT (fndecl);
2421           tree vfield = CLASSTYPE_VFIELD (t);
2422           tree this_offset;
2423
2424           offset = get_class_offset (context, t, binfo, fndecl);
2425
2426           /* Find the right offset for the this pointer based on the
2427              base class we just found.  We have to take into
2428              consideration the virtual base class pointers that we
2429              stick in before the virtual function table pointer.
2430
2431              Also, we want just the delta between the most base class
2432              that we derived this vfield from and us.  */
2433           base_offset = size_binop (PLUS_EXPR,
2434                                     get_derived_offset (binfo, DECL_CONTEXT (fndecl)),
2435                                     BINFO_OFFSET (binfo));
2436           this_offset = size_binop (MINUS_EXPR, offset, base_offset);
2437
2438           if (! tree_int_cst_equal (this_offset, delta))
2439             {
2440               /* Make sure we can modify the derived association with immunity.  */
2441               if (TREE_USED (binfo))
2442                 my_friendly_assert (0, 999);
2443
2444               if (binfo == TYPE_BINFO (t))
2445                 {
2446                   /* In this case, it is *type*'s vtable we are modifying.
2447                      We start with the approximation that it's vtable is that
2448                      of the immediate base class.  */
2449                   if (! BINFO_NEW_VTABLE_MARKED (binfo))
2450                     build_vtable (TYPE_BINFO (DECL_CONTEXT (vfield)), t);
2451                 }
2452               else
2453                 {
2454                   /* This is our very own copy of `basetype' to play with.
2455                      Later, we will fill in all the virtual functions
2456                      that override the virtual functions in these base classes
2457                      which are not defined by the current type.  */
2458                   if (! BINFO_NEW_VTABLE_MARKED (binfo))
2459                     prepare_fresh_vtable (binfo, t);
2460                 }
2461
2462               modify_vtable_entry (get_vtable_entry_n (BINFO_VIRTUALS (binfo), n),
2463                                    build_vtable_entry (this_offset, pfn),
2464                                    fndecl);
2465             }
2466         }
2467       ++n;
2468       virtuals = TREE_CHAIN (virtuals);
2469     }
2470 }
2471
2472 /* Fixup all the delta entries in all the direct vtables that need updating.
2473    This happens when we have non-overridden virtual functions from a
2474    virtual base class, that are at a different offset, in the new
2475    hierarchy, because the layout of the virtual bases has changed.  */
2476 static void
2477 fixup_vtable_deltas (binfo, init_self, t)
2478      tree binfo, t;
2479      int init_self;
2480 {
2481   tree binfos = BINFO_BASETYPES (binfo);
2482   int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2483
2484   for (i = 0; i < n_baselinks; i++)
2485     {
2486       tree base_binfo = TREE_VEC_ELT (binfos, i);
2487       int is_not_base_vtable =
2488         i != CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo));
2489       if (! TREE_VIA_VIRTUAL (base_binfo))
2490         fixup_vtable_deltas (base_binfo, is_not_base_vtable, t);
2491     }
2492   /* Should we use something besides CLASSTYPE_VFIELDS? */
2493   if (init_self && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
2494     {
2495       fixup_vtable_deltas1 (binfo, t);
2496     }
2497 }
2498
2499 /* These are the ones that are through virtual base classes. */
2500 static void
2501 modify_all_indirect_vtables (binfo, do_self, via_virtual, t, fndecl, pfn)
2502      tree binfo, t, fndecl, pfn;
2503      int do_self, via_virtual;
2504 {
2505   tree binfos = BINFO_BASETYPES (binfo);
2506   int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2507
2508   /* Should we use something besides CLASSTYPE_VFIELDS? */
2509   if (do_self && via_virtual && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
2510     {
2511       modify_one_vtable (binfo, t, fndecl, pfn);
2512     }
2513
2514   for (i = 0; i < n_baselinks; i++)
2515     {
2516       tree base_binfo = TREE_VEC_ELT (binfos, i);
2517       int is_not_base_vtable =
2518         i != CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo));
2519       if (TREE_VIA_VIRTUAL (base_binfo))
2520         {
2521           via_virtual = 1;
2522           base_binfo = binfo_member (BINFO_TYPE (base_binfo), CLASSTYPE_VBASECLASSES (t));
2523         }
2524       modify_all_indirect_vtables (base_binfo, is_not_base_vtable, via_virtual, t, fndecl, pfn);
2525     }
2526 }
2527
2528 static void
2529 modify_all_vtables (t, fndecl, vfn)
2530      tree t, fndecl, vfn;
2531 {
2532   /* Do these first, so that we will make use of any non-virtual class's
2533      vtable, over a virtual classes vtable. */
2534   modify_all_direct_vtables (TYPE_BINFO (t), 1, t, fndecl, vfn);
2535   if (TYPE_USES_VIRTUAL_BASECLASSES (t))
2536     modify_all_indirect_vtables (TYPE_BINFO (t), 1, 0, t, fndecl, vfn);
2537 }
2538
2539 /* Here, we already know that they match in every respect.
2540    All we have to check is where they had their declarations.  */
2541 static int 
2542 strictly_overrides (fndecl1, fndecl2)
2543      tree fndecl1, fndecl2;
2544 {
2545   int distance = get_base_distance (DECL_CLASS_CONTEXT (fndecl2),
2546                                     DECL_CLASS_CONTEXT (fndecl1),
2547                                     0, (tree *)0);
2548   if (distance == -2 || distance > 0)
2549     return 1;
2550   return 0;
2551 }
2552
2553 /* Merge overrides for one vtable.
2554    If we want to merge in same function, we are fine.
2555    else
2556      if one has a DECL_CLASS_CONTEXT that is a parent of the
2557        other, than choose the more derived one
2558      else
2559        potentially ill-formed (see 10.3 [class.virtual])
2560        we have to check later to see if there was an
2561        override in this class.  If there was ok, if not
2562        then it is ill-formed.  (mrs)
2563
2564    We take special care to reuse a vtable, if we can.  */
2565 static void
2566 override_one_vtable (binfo, old, t)
2567      tree binfo, old, t;
2568 {
2569   tree virtuals = BINFO_VIRTUALS (binfo);
2570   tree old_virtuals = BINFO_VIRTUALS (old);
2571   enum { REUSE_NEW, REUSE_OLD, UNDECIDED, NEITHER } choose = UNDECIDED;
2572
2573   /* If we have already committed to modifying it, then don't try and
2574      reuse another vtable. */
2575   if (BINFO_NEW_VTABLE_MARKED (binfo))
2576     choose = NEITHER;
2577
2578   skip_rtti_stuff (&virtuals);
2579   skip_rtti_stuff (&old_virtuals);
2580
2581   while (virtuals)
2582     {
2583       tree fndecl = TREE_VALUE (virtuals);
2584       tree old_fndecl = TREE_VALUE (old_virtuals);
2585       fndecl = FNADDR_FROM_VTABLE_ENTRY (fndecl);
2586       old_fndecl = FNADDR_FROM_VTABLE_ENTRY (old_fndecl);
2587       fndecl = TREE_OPERAND (fndecl, 0);
2588       old_fndecl = TREE_OPERAND (old_fndecl, 0);
2589       /* First check to see if they are the same. */
2590       if (DECL_ASSEMBLER_NAME (fndecl) == DECL_ASSEMBLER_NAME (old_fndecl))
2591         {
2592           /* No need to do anything. */
2593         }
2594       else if (strictly_overrides (fndecl, old_fndecl))
2595         {
2596           if (choose == UNDECIDED)
2597             choose = REUSE_NEW;
2598           else if (choose == REUSE_OLD)
2599             {
2600               choose = NEITHER;
2601               if (! BINFO_NEW_VTABLE_MARKED (binfo))
2602                 {
2603                   prepare_fresh_vtable (binfo, t);
2604                   override_one_vtable (binfo, old, t);
2605                   return;
2606                 }
2607             }
2608         }
2609       else if (strictly_overrides (old_fndecl, fndecl))
2610         {
2611           if (choose == UNDECIDED)
2612             choose = REUSE_OLD;
2613           else if (choose == REUSE_NEW)
2614             {
2615               choose = NEITHER;
2616               if (! BINFO_NEW_VTABLE_MARKED (binfo))
2617                 {
2618                   prepare_fresh_vtable (binfo, t);
2619                   override_one_vtable (binfo, old, t);
2620                   return;
2621                 }
2622               TREE_VALUE (virtuals) = TREE_VALUE (old_virtuals);
2623             }
2624           else if (choose == NEITHER)
2625             {
2626               TREE_VALUE (virtuals) = TREE_VALUE (old_virtuals);
2627             }  
2628         }
2629       else
2630         {
2631           choose = NEITHER;
2632           if (! BINFO_NEW_VTABLE_MARKED (binfo))
2633             {
2634               prepare_fresh_vtable (binfo, t);
2635               override_one_vtable (binfo, old, t);
2636               return;
2637             }
2638           {
2639             /* This MUST be overridden, or the class is ill-formed.  */
2640             /* For now, we just make it abstract.  */
2641             tree fndecl = TREE_OPERAND (FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (virtuals)), 0);
2642             tree vfn;
2643
2644             fndecl = copy_node (fndecl);
2645             copy_lang_decl (fndecl);
2646             DECL_ABSTRACT_VIRTUAL_P (fndecl) = 1;
2647             /* Make sure we search for it later. */
2648             if (! CLASSTYPE_ABSTRACT_VIRTUALS (t))
2649               CLASSTYPE_ABSTRACT_VIRTUALS (t) = error_mark_node;
2650
2651             vfn = build1 (ADDR_EXPR, vfunc_ptr_type_node, fndecl);
2652             TREE_CONSTANT (vfn) = 1;
2653             
2654             /* We can use integer_zero_node, as we will will core dump
2655                if this is used anyway. */
2656             TREE_VALUE (virtuals) = build_vtable_entry (integer_zero_node, vfn);
2657           }
2658         }
2659       virtuals = TREE_CHAIN (virtuals);
2660       old_virtuals = TREE_CHAIN (old_virtuals);
2661     }
2662
2663   /* Let's reuse the old vtable. */
2664   if (choose == REUSE_OLD)
2665     {
2666       BINFO_VTABLE (binfo) = BINFO_VTABLE (old);
2667       BINFO_VIRTUALS (binfo) = BINFO_VIRTUALS (old);
2668     }
2669 }
2670
2671 /* Merge in overrides for virtual bases.
2672    BINFO is the hierarchy we want to modify, and OLD has the potential
2673    overrides.  */
2674 static void
2675 merge_overrides (binfo, old, do_self, t)
2676      tree binfo, old, t;
2677      int do_self;
2678 {
2679   tree binfos = BINFO_BASETYPES (binfo);
2680   tree old_binfos = BINFO_BASETYPES (old);
2681   int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2682
2683   /* Should we use something besides CLASSTYPE_VFIELDS? */
2684   if (do_self && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
2685     {
2686       override_one_vtable (binfo, old, t);
2687     }
2688
2689   for (i = 0; i < n_baselinks; i++)
2690     {
2691       tree base_binfo = TREE_VEC_ELT (binfos, i);
2692       tree old_base_binfo = TREE_VEC_ELT (old_binfos, i);
2693       int is_not_base_vtable =
2694         i != CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo));
2695       if (! TREE_VIA_VIRTUAL (base_binfo))
2696         merge_overrides (base_binfo, old_base_binfo, is_not_base_vtable, t);
2697     }
2698 }
2699
2700 /* Get the base virtual function declarations in T that are either
2701    overridden or hidden by FNDECL as a list.  We set TREE_PURPOSE with
2702    the overrider/hider.  */
2703 tree
2704 get_basefndecls (fndecl, t)
2705      tree fndecl, t;
2706 {
2707   tree methods = TYPE_METHODS (t);
2708   tree base_fndecls = NULL_TREE;
2709   tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
2710   int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2711
2712   while (methods)
2713     {
2714       tree purpose = NULL_TREE;
2715
2716       if (TREE_CODE (methods) == FUNCTION_DECL
2717           && DECL_VINDEX (methods) != NULL_TREE
2718           && DECL_NAME (fndecl) == DECL_NAME (methods))
2719         base_fndecls = temp_tree_cons (fndecl, methods, base_fndecls);
2720
2721       methods = TREE_CHAIN (methods);
2722     }
2723
2724   if (base_fndecls)
2725     return base_fndecls;
2726
2727   for (i = 0; i < n_baseclasses; i++)
2728     {
2729       tree base_binfo = TREE_VEC_ELT (binfos, i);
2730       tree basetype = BINFO_TYPE (base_binfo);
2731       tree methods = TYPE_METHODS (basetype);
2732
2733       base_fndecls = chainon (get_basefndecls (fndecl, basetype),
2734                               base_fndecls);
2735     }
2736
2737   return base_fndecls;
2738 }
2739
2740 /* Mark the functions that have been hidden with their overriders.
2741    Since we start out with all functions already marked with a hider,
2742    no need to mark functions that are just hidden.  */
2743 void
2744 mark_overriders (fndecl, base_fndecls)
2745      tree fndecl, base_fndecls;
2746 {
2747   while (base_fndecls)
2748     {
2749       if (overrides (TREE_VALUE (base_fndecls), fndecl))
2750         TREE_PURPOSE (base_fndecls) = fndecl;
2751
2752       base_fndecls = TREE_CHAIN (base_fndecls);
2753     }
2754 }
2755
2756 /* Warn about hidden virtual functions that are not overridden in t.  */
2757 void
2758 warn_hidden (t)
2759      tree t;
2760 {
2761   tree method_vec = CLASSTYPE_METHOD_VEC (t);
2762   int n_methods = method_vec ? TREE_VEC_LENGTH (method_vec) : 0;
2763   int i;
2764
2765   /* We go through each separately named virtual function.  */
2766   for (i = 1; i < n_methods; ++i)
2767     {
2768       tree fndecl = TREE_VEC_ELT (method_vec, i);
2769
2770       tree base_fndecls = NULL_TREE;
2771       tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
2772       int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2773
2774       if (DECL_VINDEX (fndecl) == NULL_TREE)
2775         continue;
2776
2777       /* First we get a list of all possible functions that might be
2778          hidden from each base class.  */
2779       for (i = 0; i < n_baseclasses; i++)
2780         {
2781           tree base_binfo = TREE_VEC_ELT (binfos, i);
2782           tree basetype = BINFO_TYPE (base_binfo);
2783
2784           base_fndecls = chainon (get_basefndecls (fndecl, basetype),
2785                                   base_fndecls);
2786         }
2787
2788       if (TREE_CHAIN (fndecl)
2789           && DECL_NAME (TREE_CHAIN (fndecl)) == DECL_NAME (fndecl))
2790           fndecl = TREE_CHAIN (fndecl);
2791         else
2792           fndecl = NULL_TREE;
2793
2794       /* ...then mark up all the base functions with overriders, preferring
2795          overriders to hiders.  */
2796       if (base_fndecls)
2797         while (fndecl)
2798           {
2799             mark_overriders (fndecl, base_fndecls);
2800             
2801             if (TREE_CHAIN (fndecl)
2802                 && DECL_NAME (TREE_CHAIN (fndecl)) == DECL_NAME (fndecl))
2803               fndecl = TREE_CHAIN (fndecl);
2804             else
2805               fndecl = NULL_TREE;
2806           }
2807
2808       /* Now give a warning for all base functions without overriders,
2809          as they are hidden.  */
2810       while (base_fndecls)
2811         {
2812           if (! overrides (TREE_VALUE (base_fndecls),
2813                            TREE_PURPOSE (base_fndecls)))
2814             {
2815               /* Here we know it is a hider, and no overrider exists.  */
2816               cp_warning_at ("`%D' was hidden", TREE_VALUE (base_fndecls));
2817               cp_warning_at ("  by `%D'", TREE_PURPOSE (base_fndecls));
2818             }
2819
2820           base_fndecls = TREE_CHAIN (base_fndecls);
2821         }
2822     }
2823 }
2824
2825 /* Check for things that are invalid.  There are probably plenty of other
2826    things we should check for also.  */
2827 static void
2828 finish_struct_anon (t)
2829      tree t;
2830 {
2831   tree field;
2832   for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
2833     {
2834       if (TREE_STATIC (field))
2835         continue;
2836       if (TREE_CODE (field) != FIELD_DECL)
2837         continue;
2838
2839       if (DECL_NAME (field) == NULL_TREE
2840           && TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
2841         {
2842           tree* uelt = &TYPE_FIELDS (TREE_TYPE (field));
2843           for (; *uelt; uelt = &TREE_CHAIN (*uelt))
2844             {
2845               tree offset, x;
2846
2847               if (TREE_CODE (*uelt) != FIELD_DECL)
2848                 continue;
2849
2850               if (TREE_PRIVATE (*uelt))
2851                 cp_pedwarn_at ("private member `%#D' in anonymous union",
2852                                *uelt);
2853               else if (TREE_PROTECTED (*uelt))
2854                 cp_pedwarn_at ("protected member `%#D' in anonymous union",
2855                                *uelt);
2856             }
2857         }
2858     }
2859 }
2860
2861 extern int interface_only, interface_unknown;
2862
2863 /* Create a RECORD_TYPE or UNION_TYPE node for a C struct or union declaration
2864    (or C++ class declaration).
2865
2866    For C++, we must handle the building of derived classes.
2867    Also, C++ allows static class members.  The way that this is
2868    handled is to keep the field name where it is (as the DECL_NAME
2869    of the field), and place the overloaded decl in the DECL_FIELD_BITPOS
2870    of the field.  layout_record and layout_union will know about this.
2871
2872    More C++ hair: inline functions have text in their
2873    DECL_PENDING_INLINE_INFO nodes which must somehow be parsed into
2874    meaningful tree structure.  After the struct has been laid out, set
2875    things up so that this can happen.
2876
2877    And still more: virtual functions.  In the case of single inheritance,
2878    when a new virtual function is seen which redefines a virtual function
2879    from the base class, the new virtual function is placed into
2880    the virtual function table at exactly the same address that
2881    it had in the base class.  When this is extended to multiple
2882    inheritance, the same thing happens, except that multiple virtual
2883    function tables must be maintained.  The first virtual function
2884    table is treated in exactly the same way as in the case of single
2885    inheritance.  Additional virtual function tables have different
2886    DELTAs, which tell how to adjust `this' to point to the right thing.
2887
2888    LIST_OF_FIELDLISTS is just that.  The elements of the list are
2889    TREE_LIST elements, whose TREE_PURPOSE field tells what access
2890    the list has, and the TREE_VALUE slot gives the actual fields.
2891
2892    If flag_all_virtual == 1, then we lay all functions into
2893    the virtual function table, as though they were declared
2894    virtual.  Constructors do not lay down in the virtual function table.
2895
2896    If flag_all_virtual == 2, then we lay all functions into
2897    the virtual function table, such that virtual functions
2898    occupy a space by themselves, and then all functions
2899    of the class occupy a space by themselves.  This is illustrated
2900    in the following diagram:
2901
2902    class A; class B : A;
2903
2904         Class A's vtbl:                 Class B's vtbl:
2905     --------------------------------------------------------------------
2906    | A's virtual functions|             | B's virtual functions         |
2907    |                      |             | (may inherit some from A).    |
2908     --------------------------------------------------------------------
2909    | All of A's functions |             | All of A's functions          |
2910    | (such as a->A::f).   |             | (such as b->A::f)             |
2911     --------------------------------------------------------------------
2912                                         | B's new virtual functions     |
2913                                         | (not defined in A.)           |
2914                                          -------------------------------
2915                                         | All of B's functions          |
2916                                         | (such as b->B::f)             |
2917                                          -------------------------------
2918
2919    this allows the program to make references to any function, virtual
2920    or otherwise in a type-consistent manner.  */
2921
2922 tree
2923 finish_struct_1 (t, warn_anon)
2924      tree t;
2925      int warn_anon;
2926 {
2927   int old;
2928   int round_up_size = 1;
2929
2930   tree name = TYPE_IDENTIFIER (t);
2931   enum tree_code code = TREE_CODE (t);
2932   tree fields = TYPE_FIELDS (t);
2933   tree fn_fields = TYPE_METHODS (t);
2934   tree x, last_x, method_vec;
2935   int needs_virtual_dtor;
2936   int all_virtual;
2937   int has_virtual;
2938   int max_has_virtual;
2939   tree pending_virtuals = NULL_TREE;
2940   tree abstract_virtuals = NULL_TREE;
2941   tree vfield;
2942   tree vfields;
2943   int cant_have_default_ctor;
2944   int cant_have_const_ctor;
2945   int cant_synth_copy_ctor;
2946   int cant_synth_asn_ref;
2947   int no_const_asn_ref;
2948
2949   /* The index of the first base class which has virtual
2950      functions.  Only applied to non-virtual baseclasses.  */
2951   int first_vfn_base_index;
2952
2953   int n_baseclasses;
2954   int any_default_members = 0;
2955   int const_sans_init = 0;
2956   int ref_sans_init = 0;
2957   int nonprivate_method = 0;
2958   tree t_binfo = TYPE_BINFO (t);
2959   tree access_decls = NULL_TREE;
2960   int aggregate = 1;
2961
2962   if (warn_anon && code != UNION_TYPE && ANON_AGGRNAME_P (TYPE_IDENTIFIER (t)))
2963     pedwarn ("anonymous class type not used to declare any objects");
2964
2965   if (TYPE_SIZE (t))
2966     {
2967       if (IS_AGGR_TYPE (t))
2968         cp_error ("redefinition of `%#T'", t);
2969       else
2970         my_friendly_abort (172);
2971       popclass (0);
2972       return t;
2973     }
2974
2975   if (dont_allow_type_definitions)
2976     {
2977       pedwarn ("types cannot be defined %s",
2978                dont_allow_type_definitions);
2979     }
2980
2981   GNU_xref_decl (current_function_decl, t);
2982
2983   /* If this type was previously laid out as a forward reference,
2984      make sure we lay it out again.  */
2985
2986   TYPE_SIZE (t) = NULL_TREE;
2987   CLASSTYPE_GOT_SEMICOLON (t) = 0;
2988
2989 #if 0
2990   /* This is in general too late to do this.  I moved the main case up to
2991      left_curly, what else needs to move?  */
2992   if (! IS_SIGNATURE (t))
2993     {
2994       my_friendly_assert (CLASSTYPE_INTERFACE_ONLY (t) == interface_only, 999);
2995       my_friendly_assert (CLASSTYPE_INTERFACE_KNOWN (t) == ! interface_unknown, 999);
2996     }
2997 #endif
2998
2999 #if 0
3000   if (flag_rtti)
3001     build_t_desc (t, 0);
3002 #endif
3003
3004   TYPE_BINFO (t) = NULL_TREE;
3005
3006   old = suspend_momentary ();
3007
3008   /* Install struct as DECL_FIELD_CONTEXT of each field decl.
3009      Also process specified field sizes.
3010      Set DECL_FIELD_SIZE to the specified size, or 0 if none specified.
3011      The specified size is found in the DECL_INITIAL.
3012      Store 0 there, except for ": 0" fields (so we can find them
3013      and delete them, below).  */
3014
3015   if (t_binfo && BINFO_BASETYPES (t_binfo))
3016     n_baseclasses = TREE_VEC_LENGTH (BINFO_BASETYPES (t_binfo));
3017   else
3018     n_baseclasses = 0;
3019
3020   if (n_baseclasses > 0)
3021     {
3022       struct base_info base_info;
3023
3024       /* If using multiple inheritance, this may cause variants of our
3025          basetypes to be used (instead of their canonical forms).  */
3026       tree vf = layout_basetypes (t, BINFO_BASETYPES (t_binfo));
3027       last_x = tree_last (vf);
3028       fields = chainon (vf, fields);
3029
3030       first_vfn_base_index = finish_base_struct (t, &base_info, t_binfo);
3031       /* Remember where we got our vfield from */
3032       CLASSTYPE_VFIELD_PARENT (t) = first_vfn_base_index;
3033       has_virtual = base_info.has_virtual;
3034       max_has_virtual = base_info.max_has_virtual;
3035       CLASSTYPE_N_SUPERCLASSES (t) += base_info.n_ancestors;
3036       vfield = base_info.vfield;
3037       vfields = base_info.vfields;
3038       cant_have_default_ctor = base_info.cant_have_default_ctor;
3039       cant_have_const_ctor = base_info.cant_have_const_ctor;
3040       cant_synth_copy_ctor = base_info.cant_synth_copy_ctor;
3041       cant_synth_asn_ref = base_info.cant_synth_asn_ref;
3042       no_const_asn_ref = base_info.no_const_asn_ref;
3043       needs_virtual_dtor = base_info.needs_virtual_dtor;
3044       n_baseclasses = TREE_VEC_LENGTH (BINFO_BASETYPES (t_binfo));
3045       aggregate = 0;
3046     }
3047   else
3048     {
3049       first_vfn_base_index = -1;
3050       has_virtual = 0;
3051       max_has_virtual = has_virtual;
3052       vfield = NULL_TREE;
3053       vfields = NULL_TREE;
3054       last_x = NULL_TREE;
3055       cant_have_default_ctor = 0;
3056       cant_have_const_ctor = 0;
3057       cant_synth_copy_ctor = 0;
3058       cant_synth_asn_ref = 0;
3059       no_const_asn_ref = 0;
3060       needs_virtual_dtor = 0;
3061     }
3062
3063 #if 0
3064   /* Both of these should be done before now.  */
3065   if (write_virtuals == 3 && CLASSTYPE_INTERFACE_KNOWN (t)
3066       && ! IS_SIGNATURE (t))
3067     {
3068       my_friendly_assert (CLASSTYPE_INTERFACE_ONLY (t) == interface_only, 999);
3069       my_friendly_assert (CLASSTYPE_VTABLE_NEEDS_WRITING (t) == ! interface_only, 999);
3070     }
3071 #endif
3072
3073   /* The three of these are approximations which may later be
3074      modified.  Needed at this point to make add_virtual_function
3075      and modify_vtable_entries work.  */
3076   TREE_CHAIN (t_binfo) = TYPE_BINFO (t);
3077   TYPE_BINFO (t) = t_binfo;
3078   CLASSTYPE_VFIELDS (t) = vfields;
3079   CLASSTYPE_VFIELD (t) = vfield;
3080
3081   if (IS_SIGNATURE (t))
3082     all_virtual = 0;
3083   else if (flag_all_virtual == 1 && TYPE_OVERLOADS_METHOD_CALL_EXPR (t))
3084     all_virtual = 1;
3085   else
3086     all_virtual = 0;
3087
3088   for (x = TYPE_METHODS (t); x; x = TREE_CHAIN (x))
3089     {
3090       GNU_xref_member (current_class_name, x);
3091
3092       nonprivate_method |= ! TREE_PRIVATE (x);
3093
3094       /* If this was an evil function, don't keep it in class.  */
3095       if (IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (x)))
3096         continue;
3097
3098       DECL_CLASS_CONTEXT (x) = t;
3099
3100       /* Do both of these, even though they're in the same union;
3101          if the insn `r' member and the size `i' member are
3102          different sizes, as on the alpha, the larger of the two
3103          will end up with garbage in it.  */
3104       DECL_SAVED_INSNS (x) = NULL_RTX;
3105       DECL_FIELD_SIZE (x) = 0;
3106
3107       /* The name of the field is the original field name
3108          Save this in auxiliary field for later overloading.  */
3109       if (DECL_VINDEX (x)
3110           || (all_virtual == 1 && ! DECL_CONSTRUCTOR_P (x)))
3111         {
3112           pending_virtuals = add_virtual_function (pending_virtuals,
3113                                                    &has_virtual, x, t);
3114           if (DECL_ABSTRACT_VIRTUAL_P (x))
3115             abstract_virtuals = tree_cons (NULL_TREE, x, abstract_virtuals);
3116           else
3117             TREE_USED (x) = 1;
3118         }
3119     }
3120
3121   for (x = TYPE_FIELDS (t); x; x = TREE_CHAIN (x))
3122     {
3123       GNU_xref_member (current_class_name, x);
3124
3125       /* Handle access declarations.  */
3126       if (DECL_NAME (x) && TREE_CODE (DECL_NAME (x)) == SCOPE_REF)
3127         {
3128           tree fdecl = TREE_OPERAND (DECL_NAME (x), 1);
3129           tree access
3130             = TREE_PRIVATE (x) ? access_private_node :
3131               TREE_PROTECTED (x) ? access_protected_node : access_public_node;
3132
3133           if (last_x)
3134             TREE_CHAIN (last_x) = TREE_CHAIN (x);
3135           else
3136             fields = TREE_CHAIN (x);
3137
3138           access_decls = tree_cons (access, fdecl, access_decls);
3139           continue;
3140         }
3141
3142       last_x = x;
3143
3144       if (TREE_CODE (x) == TYPE_DECL)
3145         continue;
3146
3147       /* If we've gotten this far, it's a data member, possibly static,
3148          or an enumerator. */
3149
3150       DECL_FIELD_CONTEXT (x) = t;
3151
3152       /* ``A local class cannot have static data members.'' ARM 9.4 */
3153       if (current_function_decl && TREE_STATIC (x))
3154         cp_error_at ("field `%D' in local class cannot be static", x);
3155
3156       /* Perform error checking that did not get done in
3157          grokdeclarator.  */
3158       if (TREE_CODE (TREE_TYPE (x)) == FUNCTION_TYPE)
3159         {
3160           cp_error_at ("field `%D' invalidly declared function type",
3161                        x);
3162           TREE_TYPE (x) = build_pointer_type (TREE_TYPE (x));
3163         }
3164       else if (TREE_CODE (TREE_TYPE (x)) == METHOD_TYPE)
3165         {
3166           cp_error_at ("field `%D' invalidly declared method type", x);
3167           TREE_TYPE (x) = build_pointer_type (TREE_TYPE (x));
3168         }
3169       else if (TREE_CODE (TREE_TYPE (x)) == OFFSET_TYPE)
3170         {
3171           cp_error_at ("field `%D' invalidly declared offset type", x);
3172           TREE_TYPE (x) = build_pointer_type (TREE_TYPE (x));
3173         }
3174
3175 #if 0
3176       if (DECL_NAME (x) == constructor_name (t))
3177         cant_have_default_ctor = cant_synth_copy_ctor = 1;
3178 #endif
3179
3180       if (TREE_TYPE (x) == error_mark_node)
3181         continue;
3182           
3183       DECL_SAVED_INSNS (x) = NULL_RTX;
3184       DECL_FIELD_SIZE (x) = 0;
3185
3186       /* When this goes into scope, it will be a non-local reference.  */
3187       DECL_NONLOCAL (x) = 1;
3188
3189       if (TREE_CODE (x) == CONST_DECL)
3190         continue;
3191
3192       if (TREE_CODE (x) == VAR_DECL)
3193         {
3194           if (TREE_CODE (t) == UNION_TYPE)
3195             /* Unions cannot have static members.  */
3196             cp_error_at ("field `%D' declared static in union", x);
3197               
3198           continue;
3199         }
3200
3201       /* Now it can only be a FIELD_DECL.  */
3202
3203       if (TREE_PRIVATE (x) || TREE_PROTECTED (x))
3204         aggregate = 0;
3205
3206       /* If this is of reference type, check if it needs an init.
3207          Also do a little ANSI jig if necessary.  */
3208       if (TREE_CODE (TREE_TYPE (x)) == REFERENCE_TYPE)
3209         {
3210           if (DECL_INITIAL (x) == NULL_TREE)
3211             ref_sans_init = 1;
3212
3213           /* ARM $12.6.2: [A member initializer list] (or, for an
3214              aggregate, initialization by a brace-enclosed list) is the
3215              only way to initialize nonstatic const and reference
3216              members.  */
3217           cant_synth_asn_ref = 1;
3218           cant_have_default_ctor = 1;
3219
3220           if (! TYPE_HAS_CONSTRUCTOR (t) && extra_warnings)
3221             {
3222               if (DECL_NAME (x))
3223                 cp_warning_at ("non-static reference `%#D' in class without a constructor", x);
3224               else
3225                 cp_warning_at ("non-static reference in class without a constructor", x);
3226             }
3227         }
3228
3229       /* If any field is const, the structure type is pseudo-const.  */
3230       if (TREE_READONLY (x))
3231         {
3232           C_TYPE_FIELDS_READONLY (t) = 1;
3233           if (DECL_INITIAL (x) == NULL_TREE)
3234             const_sans_init = 1;
3235
3236           /* ARM $12.6.2: [A member initializer list] (or, for an
3237              aggregate, initialization by a brace-enclosed list) is the
3238              only way to initialize nonstatic const and reference
3239              members.  */
3240           cant_synth_asn_ref = 1;
3241           cant_have_default_ctor = 1;
3242
3243           if (! TYPE_HAS_CONSTRUCTOR (t) && !IS_SIGNATURE (t)
3244               && extra_warnings)
3245             {
3246               if (DECL_NAME (x))
3247                 cp_warning_at ("non-static const member `%#D' in class without a constructor", x);
3248               else
3249                 cp_warning_at ("non-static const member in class without a constructor", x);
3250             }
3251         }
3252       else
3253         {
3254           /* A field that is pseudo-const makes the structure
3255              likewise.  */
3256           tree t1 = TREE_TYPE (x);
3257           while (TREE_CODE (t1) == ARRAY_TYPE)
3258             t1 = TREE_TYPE (t1);
3259           if (IS_AGGR_TYPE (t1))
3260             {
3261               if (C_TYPE_FIELDS_READONLY (t1))
3262                 C_TYPE_FIELDS_READONLY (t) = 1;
3263               if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (t1))
3264                 const_sans_init = 1;
3265             }
3266         }
3267
3268       /* We set DECL_BIT_FIELD tentatively in grokbitfield.
3269          If the type and width are valid, we'll keep it set.
3270          Otherwise, the flag is cleared.  */
3271       if (DECL_BIT_FIELD (x))
3272         {
3273           DECL_BIT_FIELD (x) = 0;
3274           /* Invalid bit-field size done by grokfield.  */
3275           /* Detect invalid bit-field type.  */
3276           if (DECL_INITIAL (x)
3277               && ! INTEGRAL_TYPE_P (TREE_TYPE (x)))
3278             {
3279               cp_error_at ("bit-field `%#D' with non-integral type", x);
3280               DECL_INITIAL (x) = NULL;
3281             }
3282
3283           /* Detect and ignore out of range field width.  */
3284           if (DECL_INITIAL (x))
3285             {
3286               register int width = TREE_INT_CST_LOW (DECL_INITIAL (x));
3287
3288               if (width < 0)
3289                 {
3290                   DECL_INITIAL (x) = NULL;
3291                   cp_error_at ("negative width in bit-field `%D'", x);
3292                 }
3293               else if (width == 0 && DECL_NAME (x) != 0)
3294                 {
3295                   DECL_INITIAL (x) = NULL;
3296                   cp_error_at ("zero width for bit-field `%D'", x);
3297                 }
3298               else if (width
3299                        > TYPE_PRECISION (long_long_unsigned_type_node))
3300                 {
3301                   /* The backend will dump if you try to use something
3302                      too big; avoid that.  */
3303                   DECL_INITIAL (x) = NULL;
3304                   sorry ("bit-fields larger than %d bits",
3305                          TYPE_PRECISION (long_long_unsigned_type_node));
3306                   cp_error_at ("  in declaration of `%D'", x);
3307                 }
3308               else if (width > TYPE_PRECISION (TREE_TYPE (x))
3309                        && TREE_CODE (TREE_TYPE (x)) != ENUMERAL_TYPE)
3310                 {
3311                   cp_warning_at ("width of `%D' exceeds its type", x);
3312                 }
3313               else if (TREE_CODE (TREE_TYPE (x)) == ENUMERAL_TYPE
3314                        && ((min_precision (TYPE_MIN_VALUE (TREE_TYPE (x)),
3315                                            TREE_UNSIGNED (TREE_TYPE (x))) > width)
3316                            || (min_precision (TYPE_MAX_VALUE (TREE_TYPE (x)),
3317                                               TREE_UNSIGNED (TREE_TYPE (x))) > width)))
3318                 {
3319                   cp_warning_at ("`%D' is too small to hold all values of `%#T'",
3320                                  x, TREE_TYPE (x));
3321                 }
3322             }
3323
3324           /* Process valid field width.  */
3325           if (DECL_INITIAL (x))
3326             {
3327               register int width = TREE_INT_CST_LOW (DECL_INITIAL (x));
3328
3329               if (width == 0)
3330                 {
3331 #ifdef EMPTY_FIELD_BOUNDARY
3332                   /* field size 0 => mark following field as "aligned" */
3333                   if (TREE_CHAIN (x))
3334                     DECL_ALIGN (TREE_CHAIN (x))
3335                       = MAX (DECL_ALIGN (TREE_CHAIN (x)), EMPTY_FIELD_BOUNDARY);
3336                   /* field of size 0 at the end => round up the size.  */
3337                   else
3338                     round_up_size = EMPTY_FIELD_BOUNDARY;
3339 #endif
3340 #ifdef PCC_BITFIELD_TYPE_MATTERS
3341                   DECL_ALIGN (x) = MAX (DECL_ALIGN (x),
3342                                         TYPE_ALIGN (TREE_TYPE (x)));
3343 #endif
3344                 }
3345               else
3346                 {
3347                   DECL_INITIAL (x) = NULL_TREE;
3348                   DECL_FIELD_SIZE (x) = width;
3349                   DECL_BIT_FIELD (x) = 1;
3350                   /* Traditionally a bit field is unsigned
3351                      even if declared signed.  */
3352                   if (flag_traditional
3353                       && TREE_CODE (TREE_TYPE (x)) == INTEGER_TYPE)
3354                     TREE_TYPE (x) = unsigned_type_node;
3355                 }
3356             }
3357           else
3358             /* Non-bit-fields are aligned for their type.  */
3359             DECL_ALIGN (x) = MAX (DECL_ALIGN (x), TYPE_ALIGN (TREE_TYPE (x)));
3360         }
3361       else
3362         {
3363           tree type = TREE_TYPE (x);
3364
3365           while (TREE_CODE (type) == ARRAY_TYPE)
3366             type = TREE_TYPE (type);
3367
3368           if (TYPE_LANG_SPECIFIC (type) && ! ANON_UNION_P (x)
3369               && ! TYPE_PTRMEMFUNC_P (type))
3370             {
3371               /* Never let anything with uninheritable virtuals
3372                  make it through without complaint.  */
3373               if (CLASSTYPE_ABSTRACT_VIRTUALS (type))
3374                 abstract_virtuals_error (x, type);
3375                       
3376               /* Don't let signatures make it through either.  */
3377               if (IS_SIGNATURE (type))
3378                 signature_error (x, type);
3379                       
3380               if (code == UNION_TYPE)
3381                 {
3382                   char *fie = NULL;
3383                   if (TYPE_NEEDS_CONSTRUCTING (type))
3384                     fie = "constructor";
3385                   else if (TYPE_NEEDS_DESTRUCTOR (type))
3386                     fie = "destructor";
3387                   else if (TYPE_HAS_REAL_ASSIGNMENT (type))
3388                     fie = "assignment operator";
3389                   if (fie)
3390                     cp_error_at ("member `%#D' with %s not allowed in union", x,
3391                                  fie);
3392                 }
3393               else
3394                 {
3395                   TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type);
3396                   TYPE_NEEDS_DESTRUCTOR (t) |= TYPE_NEEDS_DESTRUCTOR (type);
3397                   TYPE_HAS_COMPLEX_ASSIGN_REF (t) |= TYPE_HAS_COMPLEX_ASSIGN_REF (type);
3398                   TYPE_HAS_COMPLEX_INIT_REF (t) |= TYPE_HAS_COMPLEX_INIT_REF (type);
3399                 }
3400
3401               if (! TYPE_HAS_INIT_REF (type)
3402                   || (TYPE_HAS_NONPUBLIC_CTOR (type)
3403                       && ! is_friend (t, type)))
3404                 cant_synth_copy_ctor = 1;
3405               else if (!TYPE_HAS_CONST_INIT_REF (type))
3406                 cant_have_const_ctor = 1;
3407
3408               if (! TYPE_HAS_ASSIGN_REF (type)
3409                   || (TYPE_HAS_NONPUBLIC_ASSIGN_REF (type)
3410                       && ! is_friend (t, type)))
3411                 cant_synth_asn_ref = 1;
3412               else if (!TYPE_HAS_CONST_ASSIGN_REF (type))
3413                 no_const_asn_ref = 1;
3414
3415               if (TYPE_HAS_CONSTRUCTOR (type)
3416                   && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
3417                 {
3418                   cant_have_default_ctor = 1;
3419 #if 0
3420                   /* This is wrong for aggregates.  */
3421                   if (! TYPE_HAS_CONSTRUCTOR (t))
3422                     {
3423                       if (DECL_NAME (x))
3424                         cp_pedwarn_at ("member `%#D' with only non-default constructor", x);
3425                       else
3426                         cp_pedwarn_at ("member with only non-default constructor", x);
3427                       cp_pedwarn_at ("in class without a constructor",
3428                                      x);
3429                     }
3430 #endif
3431                 }
3432             }
3433           if (DECL_INITIAL (x) != NULL_TREE)
3434             {
3435               /* `build_class_init_list' does not recognize
3436                  non-FIELD_DECLs.  */
3437               if (code == UNION_TYPE && any_default_members != 0)
3438                 cp_error_at ("multiple fields in union `%T' initialized");
3439               any_default_members = 1;
3440             }
3441         }
3442     }
3443
3444   /* If this type has any constant members which did not come
3445      with their own initialization, mark that fact here.  It is
3446      not an error here, since such types can be saved either by their
3447      constructors, or by fortuitous initialization.  */
3448   CLASSTYPE_READONLY_FIELDS_NEED_INIT (t) = const_sans_init;
3449   CLASSTYPE_REF_FIELDS_NEED_INIT (t) = ref_sans_init;
3450   CLASSTYPE_ABSTRACT_VIRTUALS (t) = abstract_virtuals;
3451
3452   /* Synthesize any needed methods.  Note that methods will be synthesized
3453      for anonymous unions; grok_x_components undoes that.  */
3454
3455   if (! fn_fields)
3456     nonprivate_method = 1;
3457
3458   if (TYPE_NEEDS_DESTRUCTOR (t) && !TYPE_HAS_DESTRUCTOR (t)
3459       && !IS_SIGNATURE (t))
3460     {
3461       /* Here we must cons up a destructor on the fly.  */
3462       tree dtor = cons_up_default_function (t, name, needs_virtual_dtor != 0);
3463
3464       /* If we couldn't make it work, then pretend we didn't need it.  */
3465       if (dtor == void_type_node)
3466         TYPE_NEEDS_DESTRUCTOR (t) = 0;
3467       else
3468         {
3469           /* Link dtor onto end of fn_fields. */
3470
3471           TREE_CHAIN (dtor) = fn_fields;
3472           fn_fields = dtor;
3473
3474           if (DECL_VINDEX (dtor) == NULL_TREE
3475               && (needs_virtual_dtor
3476                   || pending_virtuals != NULL_TREE
3477                   || pending_hard_virtuals != NULL_TREE))
3478             DECL_VINDEX (dtor) = error_mark_node;
3479           if (DECL_VINDEX (dtor))
3480             pending_virtuals = add_virtual_function (pending_virtuals,
3481                                                      &has_virtual, dtor, t);
3482           nonprivate_method = 1;
3483         }
3484     }
3485
3486   TYPE_NEEDS_DESTRUCTOR (t) |= TYPE_HAS_DESTRUCTOR (t);
3487   if (flag_rtti && (max_has_virtual > 0 || needs_virtual_dtor) && 
3488         has_virtual == 0)
3489     has_virtual = 1;
3490
3491   TYPE_HAS_COMPLEX_INIT_REF (t)
3492     |= (TYPE_HAS_INIT_REF (t) || TYPE_USES_VIRTUAL_BASECLASSES (t)
3493         || any_default_members);
3494   TYPE_NEEDS_CONSTRUCTING (t)
3495     |= (TYPE_HAS_CONSTRUCTOR (t) || TYPE_USES_VIRTUAL_BASECLASSES (t)
3496         || has_virtual || any_default_members || first_vfn_base_index >= 0);
3497   if (! IS_SIGNATURE (t))
3498     CLASSTYPE_NON_AGGREGATE (t)
3499       = ! aggregate || has_virtual || TYPE_HAS_CONSTRUCTOR (t);
3500
3501   /* ARM $12.1: A default constructor will be generated for a class X
3502      only if no constructor has been declared for class X.  So we
3503      check TYPE_HAS_CONSTRUCTOR also, to make sure we don't generate
3504      one if they declared a constructor in this class.  */
3505   if (! TYPE_HAS_CONSTRUCTOR (t) && ! cant_have_default_ctor
3506       && ! IS_SIGNATURE (t))
3507     {
3508       tree default_fn = cons_up_default_function (t, name, 2);
3509       TREE_CHAIN (default_fn) = fn_fields;
3510       fn_fields = default_fn;
3511     }
3512
3513   /* Create default copy constructor, if needed.  */
3514   if (! TYPE_HAS_INIT_REF (t) && ! cant_synth_copy_ctor
3515       && ! IS_SIGNATURE (t))
3516     {
3517       /* ARM 12.18: You get either X(X&) or X(const X&), but
3518          not both.  --Chip  */
3519       tree default_fn = cons_up_default_function (t, name,
3520                                                   3 + cant_have_const_ctor);
3521       TREE_CHAIN (default_fn) = fn_fields;
3522       fn_fields = default_fn;
3523     }
3524
3525   TYPE_HAS_REAL_ASSIGNMENT (t) |= TYPE_HAS_ASSIGNMENT (t);
3526   TYPE_HAS_REAL_ASSIGN_REF (t) |= TYPE_HAS_ASSIGN_REF (t);
3527   TYPE_HAS_COMPLEX_ASSIGN_REF (t)
3528     |= TYPE_HAS_ASSIGN_REF (t) || TYPE_USES_VIRTUAL_BASECLASSES (t);
3529
3530   if (! TYPE_HAS_ASSIGN_REF (t) && ! cant_synth_asn_ref
3531       && ! IS_SIGNATURE (t))
3532     {
3533       tree default_fn = cons_up_default_function (t, name,
3534                                                   5 + no_const_asn_ref);
3535       TREE_CHAIN (default_fn) = fn_fields;
3536       fn_fields = default_fn;
3537     }
3538
3539   if (fn_fields)
3540     {
3541       method_vec = finish_struct_methods (t, fn_fields, nonprivate_method);
3542
3543       if (TYPE_HAS_CONSTRUCTOR (t)
3544           && CLASSTYPE_FRIEND_CLASSES (t) == NULL_TREE
3545           && DECL_FRIENDLIST (TYPE_NAME (t)) == NULL_TREE)
3546         {
3547           int nonprivate_ctor = 0;
3548           tree ctor;
3549
3550           for (ctor = TREE_VEC_ELT (method_vec, 0);
3551                ctor;
3552                ctor = DECL_CHAIN (ctor))
3553             if (! TREE_PRIVATE (ctor))
3554               {
3555                 nonprivate_ctor = 1;
3556                 break;
3557               }
3558
3559           if (nonprivate_ctor == 0 && warn_ctor_dtor_privacy)
3560             cp_warning ("`%#T' only defines private constructors and has no friends",
3561                         t);
3562         }
3563     }
3564   else
3565     {
3566       method_vec = 0;
3567
3568       /* Just in case these got accidentally
3569          filled in by syntax errors.  */
3570       TYPE_HAS_CONSTRUCTOR (t) = 0;
3571       TYPE_HAS_DESTRUCTOR (t) = 0;
3572     }
3573
3574   {
3575     int n_methods = method_vec ? TREE_VEC_LENGTH (method_vec) : 0;
3576     
3577     for (access_decls = nreverse (access_decls); access_decls;
3578          access_decls = TREE_CHAIN (access_decls))
3579       {
3580         tree fdecl = TREE_VALUE (access_decls);
3581         tree flist = NULL_TREE;
3582         tree name;
3583         tree access = TREE_PURPOSE(access_decls);
3584         int i = TREE_VEC_ELT (method_vec, 0) ? 0 : 1;
3585         tree tmp;
3586
3587         if (TREE_CODE (fdecl) == TREE_LIST)
3588           {
3589             flist = fdecl;
3590             fdecl = TREE_VALUE (flist);
3591           }
3592
3593         name = DECL_NAME (fdecl);
3594
3595         for (; i < n_methods; i++)
3596           if (DECL_NAME (TREE_VEC_ELT (method_vec, i)) == name)
3597             {
3598               cp_error ("cannot adjust access to `%#D' in `%#T'", fdecl, t);
3599               cp_error_at ("  because of local method `%#D' with same name",
3600                            TREE_VEC_ELT (method_vec, i));
3601               fdecl = NULL_TREE;
3602               break;
3603             }
3604
3605         if (! fdecl)
3606           continue;
3607         
3608         for (tmp = fields; tmp; tmp = TREE_CHAIN (tmp))
3609           if (DECL_NAME (tmp) == name)
3610             {
3611               cp_error ("cannot adjust access to `%#D' in `%#T'", fdecl, t);
3612               cp_error_at ("  because of local field `%#D' with same name", tmp);
3613               fdecl = NULL_TREE;
3614               break;
3615             }
3616
3617         if (!fdecl)
3618           continue;
3619         
3620         /* Make type T see field decl FDECL with access ACCESS.*/
3621         if (flist)
3622           {
3623             fdecl = TREE_VALUE (flist);
3624             while (fdecl)
3625               {
3626                 if (alter_access (t, fdecl, access) == 0)
3627                   break;
3628                 fdecl = DECL_CHAIN (fdecl);
3629               }
3630           }
3631         else
3632           alter_access (t, fdecl, access);
3633       }
3634     
3635   }
3636
3637   if (vfield == NULL_TREE && has_virtual)
3638     {
3639       /* We build this decl with ptr_type_node, and
3640          change the type when we know what it should be.  */
3641       vfield = build_lang_field_decl (FIELD_DECL, get_vfield_name (t),
3642                                       ptr_type_node);
3643       /* If you change any of the below, take a look at all the
3644          other VFIELD_BASEs and VTABLE_BASEs in the code, and change
3645          them too. */
3646       DECL_ASSEMBLER_NAME (vfield) = get_identifier (VFIELD_BASE);
3647       CLASSTYPE_VFIELD (t) = vfield;
3648       DECL_VIRTUAL_P (vfield) = 1;
3649       DECL_FIELD_CONTEXT (vfield) = t;
3650       DECL_CLASS_CONTEXT (vfield) = t;
3651       DECL_FCONTEXT (vfield) = t;
3652       DECL_SAVED_INSNS (vfield) = NULL_RTX;
3653       DECL_FIELD_SIZE (vfield) = 0;
3654       DECL_ALIGN (vfield) = TYPE_ALIGN (ptr_type_node);
3655       if (CLASSTYPE_RTTI (t))
3656         {
3657           /* vfield is always first entry in structure.  */
3658           TREE_CHAIN (vfield) = fields;
3659           fields = vfield;
3660         }
3661       else if (last_x)
3662         {
3663           my_friendly_assert (TREE_CHAIN (last_x) == NULL_TREE, 175);
3664           TREE_CHAIN (last_x) = vfield;
3665           last_x = vfield;
3666         }
3667       else
3668         fields = vfield;
3669       vfields = chainon (vfields, CLASSTYPE_AS_LIST (t));
3670     }
3671
3672   /* Now DECL_INITIAL is null on all members except for zero-width bit-fields.
3673      And they have already done their work.
3674
3675      C++: maybe we will support default field initialization some day...  */
3676
3677   /* Delete all zero-width bit-fields from the front of the fieldlist */
3678   while (fields && DECL_BIT_FIELD (fields)
3679          && DECL_INITIAL (fields))
3680     fields = TREE_CHAIN (fields);
3681   /* Delete all such fields from the rest of the fields.  */
3682   for (x = fields; x;)
3683     {
3684       if (TREE_CHAIN (x) && DECL_BIT_FIELD (TREE_CHAIN (x))
3685           && DECL_INITIAL (TREE_CHAIN (x)))
3686         TREE_CHAIN (x) = TREE_CHAIN (TREE_CHAIN (x));
3687       else
3688         x = TREE_CHAIN (x);
3689     }
3690   /* Delete all duplicate fields from the fields */
3691   delete_duplicate_fields (fields);
3692
3693   /* Catch function/field name conflict.  We don't need to do this for a
3694      signature, since it can only contain the fields constructed in
3695      append_signature_fields.  */
3696   if (! IS_SIGNATURE (t))
3697     {
3698       int n_methods = method_vec ? TREE_VEC_LENGTH (method_vec) : 0;
3699       for (x = fields; x; x = TREE_CHAIN (x))
3700         {
3701           tree name = DECL_NAME (x);
3702           int i = /*TREE_VEC_ELT (method_vec, 0) ? 0 : */ 1;
3703           for (; i < n_methods; ++i)
3704             if (DECL_NAME (TREE_VEC_ELT (method_vec, i)) == name)
3705               {
3706                 cp_error_at ("data member `%#D' conflicts with", x);
3707                 cp_error_at ("function member `%#D'",
3708                              TREE_VEC_ELT (method_vec, i));
3709                 break;
3710               }
3711         }
3712     }
3713
3714   /* Now we have the final fieldlist for the data fields.  Record it,
3715      then lay out the structure or union (including the fields).  */
3716
3717   TYPE_FIELDS (t) = fields;
3718
3719   /* If there's a :0 field at the end, round the size to the
3720      EMPTY_FIELD_BOUNDARY.  */
3721   TYPE_ALIGN (t) = round_up_size;
3722
3723   /* Pass layout information about base classes to layout_type, if any.  */
3724   if (n_baseclasses)
3725     {
3726       tree pseudo_basetype = TREE_TYPE (base_layout_decl);
3727
3728       TREE_CHAIN (base_layout_decl) = TYPE_FIELDS (t);
3729       TYPE_FIELDS (t) = base_layout_decl;
3730
3731       TYPE_SIZE (pseudo_basetype) = CLASSTYPE_SIZE (t);
3732       TYPE_MODE (pseudo_basetype) = TYPE_MODE (t);
3733       TYPE_ALIGN (pseudo_basetype) = CLASSTYPE_ALIGN (t);
3734       DECL_ALIGN (base_layout_decl) = TYPE_ALIGN (pseudo_basetype);
3735       /* Don't re-use old size. */
3736       DECL_SIZE (base_layout_decl) = NULL_TREE;
3737     }
3738
3739   layout_type (t);
3740
3741   finish_struct_anon (t);
3742
3743   if (n_baseclasses)
3744     TYPE_FIELDS (t) = TREE_CHAIN (TYPE_FIELDS (t));
3745
3746   /* C++: do not let empty structures exist.  */
3747   if (integer_zerop (TYPE_SIZE (t)))
3748     TYPE_SIZE (t) = TYPE_SIZE (char_type_node);
3749
3750   /* Set the TYPE_DECL for this type to contain the right
3751      value for DECL_OFFSET, so that we can use it as part
3752      of a COMPONENT_REF for multiple inheritance.  */
3753
3754   if (TREE_CODE (TYPE_NAME (t)) == TYPE_DECL)
3755     layout_decl (TYPE_NAME (t), 0);
3756
3757   /* Now fix up any virtual base class types that we left lying
3758      around.  We must get these done before we try to lay out the
3759      virtual function table.  */
3760   doing_hard_virtuals = 1;
3761   pending_hard_virtuals = nreverse (pending_hard_virtuals);
3762
3763   if (TYPE_USES_VIRTUAL_BASECLASSES (t))
3764     {
3765       tree vbases;
3766
3767       max_has_virtual = layout_vbasetypes (t, max_has_virtual);
3768       vbases = CLASSTYPE_VBASECLASSES (t);
3769       CLASSTYPE_N_VBASECLASSES (t) = list_length (vbases);
3770
3771       /* The rtti code should do this.  (mrs) */
3772 #if 0 
3773       while (vbases)
3774         {
3775           /* Update rtti info with offsets for virtual baseclasses.  */
3776           if (flag_rtti && ! BINFO_NEW_VTABLE_MARKED (vbases))
3777             prepare_fresh_vtable (vbases, t);
3778           vbases = TREE_CHAIN (vbases);
3779         }
3780 #endif
3781
3782       {
3783         /* Now fixup overrides of all functions in vtables from all
3784            direct or indirect virtual base classes.  */
3785         tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
3786         int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
3787
3788         for (i = 0; i < n_baseclasses; i++)
3789           {
3790             tree base_binfo = TREE_VEC_ELT (binfos, i);
3791             tree basetype = BINFO_TYPE (base_binfo);
3792             tree vbases;
3793
3794             vbases = CLASSTYPE_VBASECLASSES (basetype);
3795             while (vbases)
3796               {
3797                 merge_overrides (binfo_member (BINFO_TYPE (vbases),
3798                                                CLASSTYPE_VBASECLASSES (t)),
3799                                  vbases, 1, t);
3800                 vbases = TREE_CHAIN (vbases);
3801               }
3802           }
3803         }
3804     }
3805
3806   /* Set up the DECL_FIELD_BITPOS of the vfield if we need to, as we
3807      might need to know it for setting up the offsets in the vtable
3808      (or in thunks) below.  */
3809   if (vfield != NULL_TREE
3810       && DECL_FIELD_CONTEXT (vfield) != t)
3811     {
3812       tree binfo = get_binfo (DECL_FIELD_CONTEXT (vfield), t, 0);
3813       tree offset = BINFO_OFFSET (binfo);
3814
3815       vfield = copy_node (vfield);
3816       copy_lang_decl (vfield);
3817
3818       if (! integer_zerop (offset))
3819         offset = size_binop (MULT_EXPR, offset, size_int (BITS_PER_UNIT));
3820       DECL_FIELD_CONTEXT (vfield) = t;
3821       DECL_CLASS_CONTEXT (vfield) = t;
3822       DECL_FIELD_BITPOS (vfield)
3823         = size_binop (PLUS_EXPR, offset, DECL_FIELD_BITPOS (vfield));
3824       CLASSTYPE_VFIELD (t) = vfield;
3825     }
3826     
3827 #ifdef NOTQUITE
3828   cp_warning ("Doing hard virtuals for %T...", t);
3829 #endif
3830
3831   if (has_virtual > max_has_virtual)
3832     max_has_virtual = has_virtual;
3833   if (max_has_virtual > 0)
3834     TYPE_VIRTUAL_P (t) = 1;
3835
3836   if (flag_rtti && TYPE_VIRTUAL_P (t) && !pending_hard_virtuals)
3837     modify_all_vtables (t, NULL_TREE, NULL_TREE);
3838
3839   while (pending_hard_virtuals)
3840     {
3841       modify_all_vtables (t,
3842                           TREE_PURPOSE (pending_hard_virtuals),
3843                           TREE_VALUE (pending_hard_virtuals));
3844       pending_hard_virtuals = TREE_CHAIN (pending_hard_virtuals);
3845     }
3846   
3847   if (TYPE_USES_VIRTUAL_BASECLASSES (t))
3848     {
3849       tree vbases;
3850       /* Now fixup any virtual function entries from virtual bases
3851          that have different deltas.  This has to come after we do the
3852          pending hard virtuals, as we might have a function that comes
3853          from multiple virtual base instances that is only overridden
3854          by a hard virtual above.  */
3855       vbases = CLASSTYPE_VBASECLASSES (t);
3856       while (vbases)
3857         {
3858           /* We might be able to shorten the amount of work we do by
3859              only doing this for vtables that come from virtual bases
3860              that have differing offsets, but don't want to miss any
3861              entries.  */
3862           fixup_vtable_deltas (vbases, 1, t);
3863           vbases = TREE_CHAIN (vbases);
3864         }
3865     }
3866
3867   doing_hard_virtuals = 0;
3868
3869   /* Under our model of GC, every C++ class gets its own virtual
3870      function table, at least virtually.  */
3871   if (pending_virtuals || (flag_rtti && TYPE_VIRTUAL_P (t)))
3872     {
3873       pending_virtuals = nreverse (pending_virtuals);
3874       /* We must enter these virtuals into the table.  */
3875       if (first_vfn_base_index < 0)
3876         {
3877           /* The first slot is for the rtti offset.  */
3878           pending_virtuals = tree_cons (NULL_TREE, NULL_TREE, pending_virtuals);
3879
3880           /* The second slot is for the tdesc pointer when thunks are used.  */
3881           if (flag_vtable_thunks)
3882             pending_virtuals = tree_cons (NULL_TREE, NULL_TREE, pending_virtuals);
3883
3884           set_rtti_entry (pending_virtuals, integer_zero_node, t);
3885           build_vtable (NULL_TREE, t);
3886         }
3887       else
3888         {
3889           tree offset;
3890           /* Here we know enough to change the type of our virtual
3891              function table, but we will wait until later this function.  */
3892
3893           if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t)))
3894             build_vtable (TREE_VEC_ELT (TYPE_BINFO_BASETYPES (t), first_vfn_base_index), t);
3895
3896           offset = get_derived_offset (TYPE_BINFO (t), NULL_TREE);
3897           offset = size_binop (MINUS_EXPR, integer_zero_node, offset);
3898           set_rtti_entry (TYPE_BINFO_VIRTUALS (t), offset, t);
3899         }
3900
3901       /* If this type has basetypes with constructors, then those
3902          constructors might clobber the virtual function table.  But
3903          they don't if the derived class shares the exact vtable of the base
3904          class.  */
3905
3906       CLASSTYPE_NEEDS_VIRTUAL_REINIT (t) = 1;
3907     }
3908   else if (first_vfn_base_index >= 0)
3909     {
3910       tree binfo = TREE_VEC_ELT (TYPE_BINFO_BASETYPES (t), first_vfn_base_index);
3911       /* This class contributes nothing new to the virtual function
3912          table.  However, it may have declared functions which
3913          went into the virtual function table "inherited" from the
3914          base class.  If so, we grab a copy of those updated functions,
3915          and pretend they are ours.  */
3916
3917       /* See if we should steal the virtual info from base class.  */
3918       if (TYPE_BINFO_VTABLE (t) == NULL_TREE)
3919         TYPE_BINFO_VTABLE (t) = BINFO_VTABLE (binfo);
3920       if (TYPE_BINFO_VIRTUALS (t) == NULL_TREE)
3921         TYPE_BINFO_VIRTUALS (t) = BINFO_VIRTUALS (binfo);
3922       if (TYPE_BINFO_VTABLE (t) != BINFO_VTABLE (binfo))
3923         CLASSTYPE_NEEDS_VIRTUAL_REINIT (t) = 1;
3924     }
3925
3926   if (max_has_virtual || first_vfn_base_index >= 0)
3927     {
3928       CLASSTYPE_VSIZE (t) = has_virtual;
3929       if (first_vfn_base_index >= 0)
3930         {
3931           if (pending_virtuals)
3932             TYPE_BINFO_VIRTUALS (t) = chainon (TYPE_BINFO_VIRTUALS (t),
3933                                                 pending_virtuals);
3934         }
3935       else if (has_virtual)
3936         {
3937           TYPE_BINFO_VIRTUALS (t) = pending_virtuals;
3938           if (write_virtuals >= 0)
3939             DECL_VIRTUAL_P (TYPE_BINFO_VTABLE (t)) = 1;
3940         }
3941     }
3942
3943   /* Now lay out the virtual function table.  */
3944   if (has_virtual)
3945     {
3946       tree atype, itype;
3947
3948       if (TREE_TYPE (vfield) == ptr_type_node)
3949         {
3950           /* We must create a pointer to this table because
3951              the one inherited from base class does not exist.
3952              We will fill in the type when we know what it
3953              should really be.  Use `size_int' so values are memoized
3954              in common cases.  */
3955           itype = build_index_type (size_int (has_virtual));
3956           atype = build_array_type (vtable_entry_type, itype);
3957           layout_type (atype);
3958           TREE_TYPE (vfield) = build_pointer_type (atype);
3959         }
3960       else
3961         {
3962           atype = TREE_TYPE (TREE_TYPE (vfield));
3963
3964           if (has_virtual != TREE_INT_CST_LOW (TYPE_MAX_VALUE (TYPE_DOMAIN (atype))))
3965             {
3966               /* We must extend (or create) the boundaries on this array,
3967                  because we picked up virtual functions from multiple
3968                  base classes.  */
3969               itype = build_index_type (size_int (has_virtual));
3970               atype = build_array_type (vtable_entry_type, itype);
3971               layout_type (atype);
3972               vfield = copy_node (vfield);
3973               TREE_TYPE (vfield) = build_pointer_type (atype);
3974             }
3975         }
3976
3977       CLASSTYPE_VFIELD (t) = vfield;
3978       if (TREE_TYPE (TYPE_BINFO_VTABLE (t)) != atype)
3979         {
3980           TREE_TYPE (TYPE_BINFO_VTABLE (t)) = atype;
3981           DECL_SIZE (TYPE_BINFO_VTABLE (t)) = 0;
3982           layout_decl (TYPE_BINFO_VTABLE (t), 0);
3983           /* At one time the vtable info was grabbed 2 words at a time.  This
3984              fails on sparc unless you have 8-byte alignment.  (tiemann) */
3985           DECL_ALIGN (TYPE_BINFO_VTABLE (t))
3986             = MAX (TYPE_ALIGN (double_type_node),
3987                    DECL_ALIGN (TYPE_BINFO_VTABLE (t)));
3988         }
3989     }
3990   else if (first_vfn_base_index >= 0)
3991     CLASSTYPE_VFIELD (t) = vfield;
3992   CLASSTYPE_VFIELDS (t) = vfields;
3993
3994   finish_struct_bits (t, max_has_virtual);
3995
3996   /* Complete the rtl for any static member objects of the type we're
3997      working on.  */
3998   for (x = fields; x; x = TREE_CHAIN (x))
3999     {
4000       if (TREE_CODE (x) == VAR_DECL && TREE_STATIC (x)
4001           && TREE_TYPE (x) == t)
4002         {
4003           DECL_MODE (x) = TYPE_MODE (t);
4004           make_decl_rtl (x, NULL, 0);
4005         }
4006     }
4007
4008   /* Now add the tags, if any, to the list of TYPE_DECLs
4009      defined for this type.  */
4010   if (CLASSTYPE_TAGS (t))
4011     {
4012       x = CLASSTYPE_TAGS (t);
4013       last_x = tree_last (TYPE_FIELDS (t));
4014       while (x)
4015         {
4016           tree tag = TYPE_NAME (TREE_VALUE (x));
4017
4018           /* Check to see if it is already there.  This will be the case if
4019              was do enum { red; } color; */
4020           if (chain_member (tag, TYPE_FIELDS (t)))
4021               {
4022                 x = TREE_CHAIN (x);
4023                 continue;
4024               }
4025
4026 #ifdef DWARF_DEBUGGING_INFO
4027           if (write_symbols == DWARF_DEBUG)
4028             {
4029               /* Notify dwarfout.c that this TYPE_DECL node represent a
4030                  gratuitous typedef.  */
4031               DECL_IGNORED_P (tag) = 1;
4032             }
4033 #endif /* DWARF_DEBUGGING_INFO */
4034
4035           TREE_NONLOCAL_FLAG (TREE_VALUE (x)) = 0;
4036           x = TREE_CHAIN (x);
4037           last_x = chainon (last_x, tag);
4038         }
4039       if (TYPE_FIELDS (t) == NULL_TREE)
4040         TYPE_FIELDS (t) = last_x;
4041       CLASSTYPE_LOCAL_TYPEDECLS (t) = 1;
4042     }
4043
4044   if (TYPE_HAS_CONSTRUCTOR (t))
4045     {
4046       tree vfields = CLASSTYPE_VFIELDS (t);
4047
4048       while (vfields)
4049         {
4050           /* Mark the fact that constructor for T
4051              could affect anybody inheriting from T
4052              who wants to initialize vtables for VFIELDS's type.  */
4053           if (VF_DERIVED_VALUE (vfields))
4054             TREE_ADDRESSABLE (vfields) = 1;
4055           vfields = TREE_CHAIN (vfields);
4056         }
4057       if (any_default_members != 0)
4058         build_class_init_list (t);
4059     }
4060   else if (TYPE_NEEDS_CONSTRUCTING (t))
4061     build_class_init_list (t);
4062
4063   if (! IS_SIGNATURE (t))
4064     embrace_waiting_friends (t);
4065
4066   /* Write out inline function definitions.  */
4067   do_inline_function_hair (t, CLASSTYPE_INLINE_FRIENDS (t));
4068   CLASSTYPE_INLINE_FRIENDS (t) = 0;
4069
4070   if (CLASSTYPE_VSIZE (t) != 0)
4071     {
4072 #if 0
4073       /* This is now done above. */
4074       if (DECL_FIELD_CONTEXT (vfield) != t)
4075         {
4076           tree binfo = get_binfo (DECL_FIELD_CONTEXT (vfield), t, 0);
4077           tree offset = BINFO_OFFSET (binfo);
4078
4079           vfield = copy_node (vfield);
4080           copy_lang_decl (vfield);
4081
4082           if (! integer_zerop (offset))
4083             offset = size_binop (MULT_EXPR, offset, size_int (BITS_PER_UNIT));
4084           DECL_FIELD_CONTEXT (vfield) = t;
4085           DECL_CLASS_CONTEXT (vfield) = t;
4086           DECL_FIELD_BITPOS (vfield)
4087             = size_binop (PLUS_EXPR, offset, DECL_FIELD_BITPOS (vfield));
4088           CLASSTYPE_VFIELD (t) = vfield;
4089         }
4090 #endif
4091
4092       /* In addition to this one, all the other vfields should be listed. */
4093       /* Before that can be done, we have to have FIELD_DECLs for them, and
4094          a place to find them.  */
4095       TYPE_NONCOPIED_PARTS (t) = build_tree_list (default_conversion (TYPE_BINFO_VTABLE (t)), vfield);
4096
4097       if (warn_nonvdtor && TYPE_HAS_DESTRUCTOR (t)
4098           && DECL_VINDEX (TREE_VEC_ELT (method_vec, 0)) == NULL_TREE)
4099         cp_warning ("`%#T' has virtual functions but non-virtual destructor",
4100                     t);
4101     }
4102
4103   /* Make the rtl for any new vtables we have created, and unmark
4104      the base types we marked.  */
4105   finish_vtbls (TYPE_BINFO (t), 1, t);
4106   TYPE_BEING_DEFINED (t) = 0;
4107   hack_incomplete_structures (t);
4108
4109 #if 0
4110   if (TYPE_NAME (t) && TYPE_IDENTIFIER (t))
4111     undo_template_name_overload (TYPE_IDENTIFIER (t), 1);
4112 #endif
4113   if (current_class_type)
4114     popclass (0);
4115   else
4116     error ("trying to finish struct, but kicked out due to previous parse errors.");
4117
4118   resume_momentary (old);
4119
4120   if (warn_overloaded_virtual)
4121     warn_hidden (t);
4122
4123 #if 0
4124   /* This has to be done after we have sorted out what to do with
4125      the enclosing type.  */
4126   if (write_symbols != DWARF_DEBUG)
4127     {
4128       /* Be smarter about nested classes here.  If a type is nested,
4129          only output it if we would output the enclosing type.  */
4130       if (DECL_CONTEXT (TYPE_NAME (t))
4131           && TREE_CODE_CLASS (TREE_CODE (DECL_CONTEXT (TYPE_NAME (t)))) == 't')
4132         DECL_IGNORED_P (TYPE_NAME (t)) = TREE_ASM_WRITTEN (TYPE_NAME (t));
4133     }
4134 #endif
4135
4136   if (write_symbols != DWARF_DEBUG)
4137     {
4138       /* If the type has methods, we want to think about cutting down
4139          the amount of symbol table stuff we output.  The value stored in
4140          the TYPE_DECL's DECL_IGNORED_P slot is a first approximation.
4141          For example, if a member function is seen and we decide to
4142          write out that member function, then we can change the value
4143          of the DECL_IGNORED_P slot, and the type will be output when
4144          that member function's debug info is written out.  */
4145       if (CLASSTYPE_METHOD_VEC (t))
4146         {
4147           extern tree pending_vtables;
4148
4149           /* Don't output full info about any type
4150              which does not have its implementation defined here.  */
4151           if (TYPE_VIRTUAL_P (t) && write_virtuals == 2)
4152             TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t))
4153               = (value_member (TYPE_IDENTIFIER (t), pending_vtables) == 0);
4154           else if (CLASSTYPE_INTERFACE_ONLY (t))
4155             TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = 1;
4156           else if (CLASSTYPE_INTERFACE_UNKNOWN (t))
4157             /* Only a first approximation!  */
4158             TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = 1;
4159         }
4160       else if (CLASSTYPE_INTERFACE_ONLY (t))
4161         TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = 1;
4162     }
4163
4164   /* Finish debugging output for this type.  */
4165   rest_of_type_compilation (t, toplevel_bindings_p ());
4166
4167   return t;
4168 }
4169
4170 tree
4171 finish_struct (t, list_of_fieldlists, warn_anon)
4172      tree t;
4173      tree list_of_fieldlists;
4174      int warn_anon;
4175 {
4176   tree fields = NULL_TREE;
4177   tree *tail = &TYPE_METHODS (t);
4178   tree name = TYPE_NAME (t);
4179   tree x, last_x = NULL_TREE;
4180   tree access;
4181
4182   if (TREE_CODE (name) == TYPE_DECL)
4183     {
4184       extern int lineno;
4185           
4186       DECL_SOURCE_FILE (name) = input_filename;
4187       /* For TYPE_DECL that are not typedefs (those marked with a line
4188          number of zero, we don't want to mark them as real typedefs.
4189          If this fails one needs to make sure real typedefs have a
4190          previous line number, even if it is wrong, that way the below
4191          will fill in the right line number.  (mrs) */
4192       if (DECL_SOURCE_LINE (name))
4193         DECL_SOURCE_LINE (name) = lineno;
4194       CLASSTYPE_SOURCE_LINE (t) = lineno;
4195       name = DECL_NAME (name);
4196     }
4197
4198   /* Append the fields we need for constructing signature tables.  */
4199   if (IS_SIGNATURE (t))
4200     append_signature_fields (list_of_fieldlists);
4201
4202   if (last_x && list_of_fieldlists)
4203     TREE_CHAIN (last_x) = TREE_VALUE (list_of_fieldlists);
4204
4205   /* For signatures, we made all methods `public' in the parser and
4206      reported an error if a access specifier was used.  */
4207   if (CLASSTYPE_DECLARED_CLASS (t) == 0)
4208     {
4209       if (list_of_fieldlists
4210           && TREE_PURPOSE (list_of_fieldlists) == access_default_node)
4211         TREE_PURPOSE (list_of_fieldlists) = access_public_node;
4212     }
4213   else if (list_of_fieldlists
4214            && TREE_PURPOSE (list_of_fieldlists) == access_default_node)
4215     TREE_PURPOSE (list_of_fieldlists) = access_private_node;
4216
4217   while (list_of_fieldlists)
4218     {
4219       access = TREE_PURPOSE (list_of_fieldlists);
4220
4221       for (x = TREE_VALUE (list_of_fieldlists); x; x = TREE_CHAIN (x))
4222         {
4223           TREE_PRIVATE (x) = access == access_private_node;
4224           TREE_PROTECTED (x) = access == access_protected_node;
4225
4226           /* Check for inconsistent use of this name in the class body.
4227              Enums, types and static vars have already been checked.  */
4228           if (TREE_CODE (x) != TYPE_DECL
4229               && TREE_CODE (x) != CONST_DECL && TREE_CODE (x) != VAR_DECL)
4230             {
4231               tree name = DECL_NAME (x);
4232               tree icv;
4233
4234               /* Don't get confused by access decls.  */
4235               if (name && TREE_CODE (name) == IDENTIFIER_NODE)
4236                 icv = IDENTIFIER_CLASS_VALUE (name);
4237               else
4238                 icv = NULL_TREE;
4239
4240               if (icv
4241                   /* Don't complain about constructors.  */
4242                   && name != constructor_name (current_class_type)
4243                   /* Or inherited names.  */
4244                   && id_in_current_class (name)
4245                   /* Or shadowed tags.  */
4246                   && !(TREE_CODE (icv) == TYPE_DECL
4247                        && DECL_CONTEXT (icv) == t))
4248                 {
4249                   cp_error_at ("declaration of identifier `%D' as `%+#D'",
4250                                name, x);
4251                   cp_error_at ("conflicts with other use in class as `%#D'",
4252                                icv);
4253                 }
4254             }
4255
4256           if (TREE_CODE (x) == FUNCTION_DECL)
4257             {
4258               if (last_x)
4259                 TREE_CHAIN (last_x) = TREE_CHAIN (x);
4260               /* Link x onto end of TYPE_METHODS. */
4261               *tail = x;
4262               tail = &TREE_CHAIN (x);
4263               continue;
4264             }
4265
4266 #if 0
4267           /* Handle access declarations.  */
4268           if (DECL_NAME (x) && TREE_CODE (DECL_NAME (x)) == SCOPE_REF)
4269             {
4270               tree n = DECL_NAME (x);
4271               x = build_decl
4272                 (USING_DECL, DECL_NAME (TREE_OPERAND (n, 1)), TREE_TYPE (x));
4273               DECL_RESULT (x) = n;
4274             }
4275 #endif
4276
4277           if (! fields)
4278             fields = x;
4279           last_x = x;
4280         }
4281       list_of_fieldlists = TREE_CHAIN (list_of_fieldlists);
4282       /* link the tail while we have it! */
4283       if (last_x)
4284         {
4285           TREE_CHAIN (last_x) = NULL_TREE;
4286
4287           if (list_of_fieldlists
4288               && TREE_VALUE (list_of_fieldlists)
4289               && TREE_CODE (TREE_VALUE (list_of_fieldlists)) != FUNCTION_DECL)
4290             TREE_CHAIN (last_x) = TREE_VALUE (list_of_fieldlists);
4291         }
4292     }
4293
4294   *tail = NULL_TREE;
4295   TYPE_FIELDS (t) = fields;
4296
4297   if (0 && processing_template_defn)
4298     {
4299       CLASSTYPE_METHOD_VEC (t)
4300         = finish_struct_methods (t, TYPE_METHODS (t), 1);
4301       return t;
4302     }
4303   else
4304     return finish_struct_1 (t, warn_anon);
4305 }
4306 \f
4307 /* Return non-zero if the effective type of INSTANCE is static.
4308    Used to determine whether the virtual function table is needed
4309    or not.
4310
4311    *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
4312    of our knowledge of its type.  */
4313 int
4314 resolves_to_fixed_type_p (instance, nonnull)
4315      tree instance;
4316      int *nonnull;
4317 {
4318   switch (TREE_CODE (instance))
4319     {
4320     case INDIRECT_REF:
4321       /* Check that we are not going through a cast of some sort.  */
4322       if (TREE_TYPE (instance)
4323           == TREE_TYPE (TREE_TYPE (TREE_OPERAND (instance, 0))))
4324         instance = TREE_OPERAND (instance, 0);
4325       /* fall through...  */
4326     case CALL_EXPR:
4327       /* This is a call to a constructor, hence it's never zero.  */
4328       if (TREE_HAS_CONSTRUCTOR (instance))
4329         {
4330           if (nonnull)
4331             *nonnull = 1;
4332           return 1;
4333         }
4334       return 0;
4335
4336     case SAVE_EXPR:
4337       /* This is a call to a constructor, hence it's never zero.  */
4338       if (TREE_HAS_CONSTRUCTOR (instance))
4339         {
4340           if (nonnull)
4341             *nonnull = 1;
4342           return 1;
4343         }
4344       return resolves_to_fixed_type_p (TREE_OPERAND (instance, 0), nonnull);
4345
4346     case RTL_EXPR:
4347       /* This is a call to `new', hence it's never zero.  */
4348       if (TREE_CALLS_NEW (instance))
4349         {
4350           if (nonnull)
4351             *nonnull = 1;
4352           return 1;
4353         }
4354       return 0;
4355
4356     case PLUS_EXPR:
4357     case MINUS_EXPR:
4358       if (TREE_CODE (TREE_OPERAND (instance, 1)) == INTEGER_CST)
4359         /* Propagate nonnull.  */
4360         resolves_to_fixed_type_p (TREE_OPERAND (instance, 0), nonnull);
4361       if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR)
4362         return resolves_to_fixed_type_p (TREE_OPERAND (instance, 0), nonnull);
4363       return 0;
4364
4365     case NOP_EXPR:
4366     case CONVERT_EXPR:
4367       return resolves_to_fixed_type_p (TREE_OPERAND (instance, 0), nonnull);
4368
4369     case ADDR_EXPR:
4370       if (nonnull)
4371         *nonnull = 1;
4372       return resolves_to_fixed_type_p (TREE_OPERAND (instance, 0), nonnull);
4373
4374     case COMPONENT_REF:
4375       return resolves_to_fixed_type_p (TREE_OPERAND (instance, 1), nonnull);
4376
4377     case WITH_CLEANUP_EXPR:
4378       if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR)
4379         return resolves_to_fixed_type_p (TREE_OPERAND (instance, 0), nonnull);
4380       /* fall through... */
4381     case VAR_DECL:
4382     case FIELD_DECL:
4383       if (TREE_CODE (TREE_TYPE (instance)) == ARRAY_TYPE
4384           && IS_AGGR_TYPE (TREE_TYPE (TREE_TYPE (instance))))
4385         {
4386           if (nonnull)
4387             *nonnull = 1;
4388           return 1;
4389         }
4390       /* fall through... */
4391     case TARGET_EXPR:
4392     case PARM_DECL:
4393       if (IS_AGGR_TYPE (TREE_TYPE (instance)))
4394         {
4395           if (nonnull)
4396             *nonnull = 1;
4397           return 1;
4398         }
4399       else if (nonnull)
4400         {
4401           if (instance == current_class_decl
4402               && flag_this_is_variable <= 0)
4403             {
4404               /* Some people still use `this = 0' inside destructors.  */
4405               *nonnull = ! DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (current_function_decl));
4406               /* In a constructor, we know our type.  */
4407               if (flag_this_is_variable < 0)
4408                 return 1;
4409             }
4410           else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
4411             /* Reference variables should be references to objects.  */
4412             *nonnull = 1;
4413         }
4414       return 0;
4415
4416     default:
4417       return 0;
4418     }
4419 }
4420 \f
4421 void
4422 init_class_processing ()
4423 {
4424   current_class_depth = 0;
4425   current_class_stacksize = 10;
4426   current_class_base = (tree *)xmalloc(current_class_stacksize * sizeof (tree));
4427   current_class_stack = current_class_base;
4428
4429   current_lang_stacksize = 10;
4430   current_lang_base = (tree *)xmalloc(current_lang_stacksize * sizeof (tree));
4431   current_lang_stack = current_lang_base;
4432
4433   access_default_node = build_int_2 (0, 0);
4434   access_public_node = build_int_2 (1, 0);
4435   access_protected_node = build_int_2 (2, 0);
4436   access_private_node = build_int_2 (3, 0);
4437   access_default_virtual_node = build_int_2 (4, 0);
4438   access_public_virtual_node = build_int_2 (5, 0);
4439   access_private_virtual_node = build_int_2 (6, 0);
4440
4441   /* Keep these values lying around.  */
4442   the_null_vtable_entry = build_vtable_entry (integer_zero_node, integer_zero_node);
4443   base_layout_decl = build_lang_field_decl (FIELD_DECL, NULL_TREE, error_mark_node);
4444   TREE_TYPE (base_layout_decl) = make_node (RECORD_TYPE);
4445
4446   gcc_obstack_init (&class_obstack);
4447 }
4448
4449 /* Set current scope to NAME. CODE tells us if this is a
4450    STRUCT, UNION, or ENUM environment.
4451
4452    NAME may end up being NULL_TREE if this is an anonymous or
4453    late-bound struct (as in "struct { ... } foo;")  */
4454
4455 /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE to
4456    appropriate values, found by looking up the type definition of
4457    NAME (as a CODE).
4458
4459    If MODIFY is 1, we set IDENTIFIER_CLASS_VALUE's of names
4460    which can be seen locally to the class.  They are shadowed by
4461    any subsequent local declaration (including parameter names).
4462
4463    If MODIFY is 2, we set IDENTIFIER_CLASS_VALUE's of names
4464    which have static meaning (i.e., static members, static
4465    member functions, enum declarations, etc).
4466
4467    If MODIFY is 3, we set IDENTIFIER_CLASS_VALUE of names
4468    which can be seen locally to the class (as in 1), but
4469    know that we are doing this for declaration purposes
4470    (i.e. friend foo::bar (int)).
4471
4472    So that we may avoid calls to lookup_name, we cache the _TYPE
4473    nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
4474
4475    For multiple inheritance, we perform a two-pass depth-first search
4476    of the type lattice.  The first pass performs a pre-order search,
4477    marking types after the type has had its fields installed in
4478    the appropriate IDENTIFIER_CLASS_VALUE slot.  The second pass merely
4479    unmarks the marked types.  If a field or member function name
4480    appears in an ambiguous way, the IDENTIFIER_CLASS_VALUE of
4481    that name becomes `error_mark_node'.  */
4482
4483 void
4484 pushclass (type, modify)
4485      tree type;
4486      int modify;
4487 {
4488   push_memoized_context (type, modify);
4489
4490   current_class_depth++;
4491   *current_class_stack++ = current_class_name;
4492   *current_class_stack++ = current_class_type;
4493   if (current_class_stack >= current_class_base + current_class_stacksize)
4494     {
4495       current_class_base =
4496         (tree *)xrealloc (current_class_base,
4497                           sizeof (tree) * (current_class_stacksize + 10));
4498       current_class_stack = current_class_base + current_class_stacksize;
4499       current_class_stacksize += 10;
4500     }
4501
4502   current_class_name = TYPE_NAME (type);
4503   if (TREE_CODE (current_class_name) == TYPE_DECL)
4504     current_class_name = DECL_NAME (current_class_name);
4505   current_class_type = type;
4506
4507   if (previous_class_type != NULL_TREE
4508       && (type != previous_class_type || TYPE_SIZE (previous_class_type) == NULL_TREE)
4509       && current_class_depth == 1)
4510     {
4511       /* Forcibly remove any old class remnants.  */
4512       popclass (-1);
4513       previous_class_type = NULL_TREE;
4514     }
4515
4516   pushlevel_class ();
4517
4518   if (modify)
4519     {
4520       tree tags;
4521       tree this_fndecl = current_function_decl;
4522
4523       if (current_function_decl
4524           && DECL_CONTEXT (current_function_decl)
4525           && TREE_CODE (DECL_CONTEXT (current_function_decl)) == FUNCTION_DECL)
4526         current_function_decl = DECL_CONTEXT (current_function_decl);
4527       else
4528         current_function_decl = NULL_TREE;
4529
4530       if (TREE_CODE (type) == UNINSTANTIATED_P_TYPE)
4531         declare_uninstantiated_type_level ();
4532       else if (type != previous_class_type || current_class_depth > 1)
4533         {
4534           build_mi_matrix (type);
4535           push_class_decls (type);
4536           free_mi_matrix ();
4537           if (current_class_depth == 1)
4538             previous_class_type = type;
4539         }
4540       else
4541         {
4542           tree item;
4543
4544           /* Hooray, we successfully cached; let's just install the
4545              cached class_shadowed list, and walk through it to get the
4546              IDENTIFIER_TYPE_VALUEs correct.  */
4547           set_class_shadows (previous_class_values);
4548           for (item = previous_class_values; item; item = TREE_CHAIN (item))
4549             {
4550               tree id = TREE_PURPOSE (item);
4551               tree decl = IDENTIFIER_CLASS_VALUE (id);
4552
4553               if (TREE_CODE (decl) == TYPE_DECL)
4554                 set_identifier_type_value (id, TREE_TYPE (decl));
4555             }
4556           unuse_fields (type);
4557         }
4558
4559       if (IDENTIFIER_TEMPLATE (TYPE_IDENTIFIER (type)))
4560         overload_template_name (current_class_name, 0);
4561
4562       for (tags = CLASSTYPE_TAGS (type); tags; tags = TREE_CHAIN (tags))
4563         {
4564           TREE_NONLOCAL_FLAG (TREE_VALUE (tags)) = 1;
4565           if (! TREE_PURPOSE (tags))
4566             continue;
4567           pushtag (TREE_PURPOSE (tags), TREE_VALUE (tags), 0);
4568         }
4569
4570       current_function_decl = this_fndecl;
4571     }
4572 }
4573  
4574 /* Get out of the current class scope. If we were in a class scope
4575    previously, that is the one popped to.  The flag MODIFY tells whether
4576    the current scope declarations needs to be modified as a result of
4577    popping to the previous scope.  0 is used for class definitions.  */
4578 void
4579 popclass (modify)
4580      int modify;
4581 {
4582   if (modify < 0)
4583     {
4584       /* Back this old class out completely.  */
4585       tree tags = CLASSTYPE_TAGS (previous_class_type);
4586       tree t;
4587
4588       /* This code can be seen as a cache miss.  When we've cached a
4589          class' scope's bindings and we can't use them, we need to reset
4590          them.  This is it!  */
4591       for (t = previous_class_values; t; t = TREE_CHAIN (t))
4592         IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (t)) = NULL_TREE;
4593       while (tags)
4594         {
4595           TREE_NONLOCAL_FLAG (TREE_VALUE (tags)) = 0;
4596           tags = TREE_CHAIN (tags);
4597         }
4598       goto ret;
4599     }
4600
4601   if (modify)
4602     {
4603       /* Just remove from this class what didn't make
4604          it into IDENTIFIER_CLASS_VALUE.  */
4605       tree tags = CLASSTYPE_TAGS (current_class_type);
4606
4607       while (tags)
4608         {
4609           TREE_NONLOCAL_FLAG (TREE_VALUE (tags)) = 0;
4610           tags = TREE_CHAIN (tags);
4611         }
4612       if (IDENTIFIER_TEMPLATE (TYPE_IDENTIFIER (current_class_type)))
4613         undo_template_name_overload (current_class_name, 0);
4614     }
4615
4616   /* Force clearing of IDENTIFIER_CLASS_VALUEs after a class definition,
4617      since not all class decls make it there currently.  */
4618   poplevel_class (! modify);
4619
4620   /* Since poplevel_class does the popping of class decls nowadays,
4621      this really only frees the obstack used for these decls.
4622      That's why it had to be moved down here.  */
4623   if (modify)
4624     pop_class_decls (current_class_type);
4625
4626   current_class_depth--;
4627   current_class_type = *--current_class_stack;
4628   current_class_name = *--current_class_stack;
4629
4630   pop_memoized_context (modify);
4631
4632  ret:
4633   ;
4634 }
4635
4636 /* When entering a class scope, all enclosing class scopes' names with
4637    static meaning (static variables, static functions, types and enumerators)
4638    have to be visible.  This recursive function calls pushclass for all
4639    enclosing class contexts until global or a local scope is reached.
4640    TYPE is the enclosed class and MODIFY is equivalent with the pushclass
4641    formal of the same name.  */
4642
4643 void
4644 push_nested_class (type, modify)
4645      tree type;
4646      int modify;
4647 {
4648   tree context;
4649
4650   if (type == NULL_TREE || type == error_mark_node || ! IS_AGGR_TYPE (type))
4651     return;
4652   
4653   context = DECL_CONTEXT (TYPE_NAME (type));
4654
4655   if (context && TREE_CODE (context) == RECORD_TYPE)
4656     push_nested_class (context, 2);
4657   pushclass (type, modify);
4658 }
4659
4660 /* Undoes a push_nested_class call.  MODIFY is passed on to popclass.  */
4661
4662 void
4663 pop_nested_class (modify)
4664      int modify;
4665 {
4666   tree context = DECL_CONTEXT (TYPE_NAME (current_class_type));
4667
4668   popclass (modify);
4669   if (context && TREE_CODE (context) == RECORD_TYPE)
4670     pop_nested_class (modify);
4671 }
4672
4673 /* Set global variables CURRENT_LANG_NAME to appropriate value
4674    so that behavior of name-mangling machinery is correct.  */
4675
4676 void
4677 push_lang_context (name)
4678      tree name;
4679 {
4680   *current_lang_stack++ = current_lang_name;
4681   if (current_lang_stack >= current_lang_base + current_lang_stacksize)
4682     {
4683       current_lang_base =
4684         (tree *)xrealloc (current_lang_base,
4685                           sizeof (tree) * (current_lang_stacksize + 10));
4686       current_lang_stack = current_lang_base + current_lang_stacksize;
4687       current_lang_stacksize += 10;
4688     }
4689
4690   if (name == lang_name_cplusplus)
4691     {
4692       strict_prototype = strict_prototypes_lang_cplusplus;
4693       current_lang_name = name;
4694     }
4695   else if (name == lang_name_c)
4696     {
4697       strict_prototype = strict_prototypes_lang_c;
4698       current_lang_name = name;
4699     }
4700   else
4701     error ("language string `\"%s\"' not recognized", IDENTIFIER_POINTER (name));
4702 }
4703   
4704 /* Get out of the current language scope.  */
4705 void
4706 pop_lang_context ()
4707 {
4708   current_lang_name = *--current_lang_stack;
4709   if (current_lang_name == lang_name_cplusplus)
4710     strict_prototype = strict_prototypes_lang_cplusplus;
4711   else if (current_lang_name == lang_name_c)
4712     strict_prototype = strict_prototypes_lang_c;
4713 }
4714
4715 int
4716 root_lang_context_p ()
4717 {
4718   return current_lang_stack == current_lang_base;
4719 }
4720 \f
4721 /* Type instantiation routines.  */
4722
4723 /* This function will instantiate the type of the expression given
4724    in RHS to match the type of LHSTYPE.  If LHSTYPE is NULL_TREE,
4725    or other errors exist, the TREE_TYPE of RHS will be ERROR_MARK_NODE.
4726
4727    This function is used in build_modify_expr, convert_arguments,
4728    build_c_cast, and compute_conversion_costs.  */
4729 tree
4730 instantiate_type (lhstype, rhs, complain)
4731      tree lhstype, rhs;
4732      int complain;
4733 {
4734   if (TREE_CODE (lhstype) == UNKNOWN_TYPE)
4735     {
4736       if (complain)
4737         error ("not enough type information");
4738       return error_mark_node;
4739     }
4740
4741   if (TREE_TYPE (rhs) != NULL_TREE && ! (type_unknown_p (rhs)))
4742     return rhs;
4743
4744   /* This should really only be used when attempting to distinguish
4745      what sort of a pointer to function we have.  For now, any
4746      arithmetic operation which is not supported on pointers
4747      is rejected as an error.  */
4748
4749   switch (TREE_CODE (rhs))
4750     {
4751     case TYPE_EXPR:
4752     case CONVERT_EXPR:
4753     case SAVE_EXPR:
4754     case CONSTRUCTOR:
4755     case BUFFER_REF:
4756       my_friendly_abort (177);
4757       return error_mark_node;
4758
4759     case INDIRECT_REF:
4760     case ARRAY_REF:
4761       TREE_TYPE (rhs) = lhstype;
4762       lhstype = build_pointer_type (lhstype);
4763       TREE_OPERAND (rhs, 0)
4764         = instantiate_type (lhstype, TREE_OPERAND (rhs, 0), complain);
4765       if (TREE_OPERAND (rhs, 0) == error_mark_node)
4766         return error_mark_node;
4767
4768       return rhs;
4769
4770     case NOP_EXPR:
4771       rhs = copy_node (TREE_OPERAND (rhs, 0));
4772       TREE_TYPE (rhs) = unknown_type_node;
4773       return instantiate_type (lhstype, rhs, complain);
4774
4775     case COMPONENT_REF:
4776       {
4777         tree field = TREE_OPERAND (rhs, 1);
4778         if (TREE_CODE (field) == TREE_LIST)
4779           {
4780             tree function = instantiate_type (lhstype, field, complain);
4781             if (function == error_mark_node)
4782               return error_mark_node;
4783             my_friendly_assert (TREE_CODE (function) == FUNCTION_DECL, 185);
4784             if (DECL_VINDEX (function))
4785               {
4786                 tree base = TREE_OPERAND (rhs, 0);
4787                 tree base_ptr = build_unary_op (ADDR_EXPR, base, 0);
4788                 if (base_ptr == error_mark_node)
4789                   return error_mark_node;
4790                 base_ptr = convert_pointer_to (DECL_CONTEXT (function), base_ptr);
4791                 if (base_ptr == error_mark_node)
4792                   return error_mark_node;
4793                 return build_vfn_ref (&base_ptr, base, DECL_VINDEX (function));
4794               }
4795             mark_used (function);
4796             return function;
4797           }
4798
4799         my_friendly_assert (TREE_CODE (field) == FIELD_DECL, 178);
4800         my_friendly_assert (!(TREE_CODE (TREE_TYPE (field)) == FUNCTION_TYPE
4801                               || TREE_CODE (TREE_TYPE (field)) == METHOD_TYPE),
4802                             179);
4803
4804         TREE_TYPE (rhs) = lhstype;
4805         /* First look for an exact match  */
4806
4807         while (field && TREE_TYPE (field) != lhstype)
4808           field = DECL_CHAIN (field);
4809         if (field)
4810           {
4811             TREE_OPERAND (rhs, 1) = field;
4812             mark_used (field);
4813             return rhs;
4814           }
4815
4816         /* No exact match found, look for a compatible function.  */
4817         field = TREE_OPERAND (rhs, 1);
4818         while (field && ! comptypes (lhstype, TREE_TYPE (field), 0))
4819           field = DECL_CHAIN (field);
4820         if (field)
4821           {
4822             TREE_OPERAND (rhs, 1) = field;
4823             field = DECL_CHAIN (field);
4824             while (field && ! comptypes (lhstype, TREE_TYPE (field), 0))
4825               field = DECL_CHAIN (field);
4826             if (field)
4827               {
4828                 if (complain)
4829                   error ("ambiguous overload for COMPONENT_REF requested");
4830                 return error_mark_node;
4831               }
4832           }
4833         else
4834           {
4835             if (complain)
4836               error ("no appropriate overload exists for COMPONENT_REF");
4837             return error_mark_node;
4838           }
4839         return rhs;
4840       }
4841
4842     case TREE_LIST:
4843       {
4844         tree elem, baselink, name;
4845         int globals = overloaded_globals_p (rhs);
4846
4847 #if 0 /* obsolete */
4848         /* If there's only one function we know about, return that.  */
4849         if (globals > 0 && TREE_CHAIN (rhs) == NULL_TREE)
4850           return TREE_VALUE (rhs);
4851 #endif
4852
4853         /* First look for an exact match.  Search either overloaded
4854            functions or member functions.  May have to undo what
4855            `default_conversion' might do to lhstype.  */
4856
4857         if (TYPE_PTRMEMFUNC_P (lhstype))
4858           lhstype = TYPE_PTRMEMFUNC_FN_TYPE (lhstype);
4859
4860         if (TREE_CODE (lhstype) == POINTER_TYPE)
4861           if (TREE_CODE (TREE_TYPE (lhstype)) == FUNCTION_TYPE
4862               || TREE_CODE (TREE_TYPE (lhstype)) == METHOD_TYPE)
4863             lhstype = TREE_TYPE (lhstype);
4864           else
4865             {
4866               if (complain)
4867                 error ("invalid type combination for overload");
4868               return error_mark_node;
4869             }
4870
4871         if (TREE_CODE (lhstype) != FUNCTION_TYPE && globals > 0)
4872           {
4873             if (complain)
4874               cp_error ("cannot resolve overloaded function `%D' based on non-function type",
4875                      TREE_PURPOSE (rhs));
4876             return error_mark_node;
4877           }
4878
4879         if (globals > 0)
4880           {
4881             elem = get_first_fn (rhs);
4882             while (elem)
4883               if (! comptypes (lhstype, TREE_TYPE (elem), 1))
4884                 elem = DECL_CHAIN (elem);
4885               else
4886                 {
4887                   mark_used (elem);
4888                   return elem;
4889                 }
4890
4891             /* No exact match found, look for a compatible template.  */
4892             {
4893               tree save_elem = 0;
4894               for (elem = get_first_fn (rhs); elem; elem = DECL_CHAIN (elem))
4895                 if (TREE_CODE (elem) == TEMPLATE_DECL)
4896                   {
4897                     int n = TREE_VEC_LENGTH (DECL_TEMPLATE_PARMS (elem));
4898                     tree *t = (tree *) alloca (sizeof (tree) * n);
4899                     int i, d = 0;
4900                     i = type_unification (DECL_TEMPLATE_PARMS (elem), t,
4901                                           TYPE_ARG_TYPES (TREE_TYPE (elem)),
4902                                           TYPE_ARG_TYPES (lhstype), &d, 0);
4903                     if (i == 0)
4904                       {
4905                         if (save_elem)
4906                           {
4907                             cp_error ("ambiguous template instantiation converting to `%#T'", lhstype);
4908                             return error_mark_node;
4909                           }
4910                         save_elem = instantiate_template (elem, t);
4911                         /* Check the return type.  */
4912                         if (! comptypes (TREE_TYPE (lhstype),
4913                                          TREE_TYPE (TREE_TYPE (save_elem)), 1))
4914                           save_elem = 0;
4915                       }
4916                   }
4917               if (save_elem)
4918                 return save_elem;
4919             }
4920
4921             /* No match found, look for a compatible function.  */
4922             elem = get_first_fn (rhs);
4923             while (elem && comp_target_types (lhstype,
4924                                               TREE_TYPE (elem), 1) <= 0)
4925               elem = DECL_CHAIN (elem);
4926             if (elem)
4927               {
4928                 tree save_elem = elem;
4929                 elem = DECL_CHAIN (elem);
4930                 while (elem && comp_target_types (lhstype,
4931                                                   TREE_TYPE (elem), 0) <= 0)
4932                   elem = DECL_CHAIN (elem);
4933                 if (elem)
4934                   {
4935                     if (complain)
4936                       {
4937                         cp_error ("cannot resolve overload to target type `%#T'",
4938                                   lhstype);
4939                         cp_error_at ("  ambiguity between `%#D'", save_elem);
4940                         cp_error_at ("  and `%#D', at least", elem);
4941                       }
4942                     return error_mark_node;
4943                   }
4944                 mark_used (save_elem);
4945                 return save_elem;
4946               }
4947             if (complain)
4948               {
4949                 cp_error ("cannot resolve overload to target type `%#T'",
4950                           lhstype);
4951                 cp_error ("  because no suitable overload of function `%D' exists",
4952                           TREE_PURPOSE (rhs));
4953               }
4954             return error_mark_node;
4955           }
4956
4957         if (TREE_NONLOCAL_FLAG (rhs))
4958           {
4959             /* Got to get it as a baselink.  */
4960             rhs = lookup_fnfields (TYPE_BINFO (current_class_type),
4961                                    TREE_PURPOSE (rhs), 0);
4962           }
4963         else
4964           {
4965             my_friendly_assert (TREE_CHAIN (rhs) == NULL_TREE, 181);
4966             if (TREE_CODE (TREE_VALUE (rhs)) == TREE_LIST)
4967               rhs = TREE_VALUE (rhs);
4968             my_friendly_assert (TREE_CODE (TREE_VALUE (rhs)) == FUNCTION_DECL,
4969                                 182);
4970           }
4971
4972         for (baselink = rhs; baselink;
4973              baselink = next_baselink (baselink))
4974           {
4975             elem = TREE_VALUE (baselink);
4976             while (elem)
4977               if (comptypes (lhstype, TREE_TYPE (elem), 1))
4978                 {
4979                   mark_used (elem);
4980                   return elem;
4981                 }
4982               else
4983                 elem = DECL_CHAIN (elem);
4984           }
4985
4986         /* No exact match found, look for a compatible method.  */
4987         for (baselink = rhs; baselink;
4988              baselink = next_baselink (baselink))
4989           {
4990             elem = TREE_VALUE (baselink);
4991             while (elem && comp_target_types (lhstype,
4992                                               TREE_TYPE (elem), 1) <= 0)
4993               elem = DECL_CHAIN (elem);
4994             if (elem)
4995               {
4996                 tree save_elem = elem;
4997                 elem = DECL_CHAIN (elem);
4998                 while (elem && comp_target_types (lhstype,
4999                                                   TREE_TYPE (elem), 0) <= 0)
5000                   elem = DECL_CHAIN (elem);
5001                 if (elem)
5002                   {
5003                     if (complain)
5004                       error ("ambiguous overload for overloaded method requested");
5005                     return error_mark_node;
5006                   }
5007                 mark_used (save_elem);
5008                 return save_elem;
5009               }
5010             name = DECL_NAME (TREE_VALUE (rhs));
5011 #if 0
5012             if (TREE_CODE (lhstype) == FUNCTION_TYPE && globals < 0)
5013               {
5014                 /* Try to instantiate from non-member functions.  */
5015                 rhs = lookup_name_nonclass (name);
5016                 if (rhs && TREE_CODE (rhs) == TREE_LIST)
5017                   {
5018                     /* This code seems to be missing a `return'.  */
5019                     my_friendly_abort (4);
5020                     instantiate_type (lhstype, rhs, complain);
5021                   }
5022               }
5023 #endif
5024           }
5025         if (complain)
5026           cp_error ("no compatible member functions named `%D'", name);
5027         return error_mark_node;
5028       }
5029
5030     case CALL_EXPR:
5031       /* This is too hard for now.  */
5032       my_friendly_abort (183);
5033       return error_mark_node;
5034
5035     case PLUS_EXPR:
5036     case MINUS_EXPR:
5037     case COMPOUND_EXPR:
5038       TREE_OPERAND (rhs, 0)
5039         = instantiate_type (lhstype, TREE_OPERAND (rhs, 0), complain);
5040       if (TREE_OPERAND (rhs, 0) == error_mark_node)
5041         return error_mark_node;
5042       TREE_OPERAND (rhs, 1)
5043         = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), complain);
5044       if (TREE_OPERAND (rhs, 1) == error_mark_node)
5045         return error_mark_node;
5046
5047       TREE_TYPE (rhs) = lhstype;
5048       return rhs;
5049
5050     case MULT_EXPR:
5051     case TRUNC_DIV_EXPR:
5052     case FLOOR_DIV_EXPR:
5053     case CEIL_DIV_EXPR:
5054     case ROUND_DIV_EXPR:
5055     case RDIV_EXPR:
5056     case TRUNC_MOD_EXPR:
5057     case FLOOR_MOD_EXPR:
5058     case CEIL_MOD_EXPR:
5059     case ROUND_MOD_EXPR:
5060     case FIX_ROUND_EXPR:
5061     case FIX_FLOOR_EXPR:
5062     case FIX_CEIL_EXPR:
5063     case FIX_TRUNC_EXPR:
5064     case FLOAT_EXPR:
5065     case NEGATE_EXPR:
5066     case ABS_EXPR:
5067     case MAX_EXPR:
5068     case MIN_EXPR:
5069     case FFS_EXPR:
5070
5071     case BIT_AND_EXPR:
5072     case BIT_IOR_EXPR:
5073     case BIT_XOR_EXPR:
5074     case LSHIFT_EXPR:
5075     case RSHIFT_EXPR:
5076     case LROTATE_EXPR:
5077     case RROTATE_EXPR:
5078
5079     case PREINCREMENT_EXPR:
5080     case PREDECREMENT_EXPR:
5081     case POSTINCREMENT_EXPR:
5082     case POSTDECREMENT_EXPR:
5083       if (complain)
5084         error ("invalid operation on uninstantiated type");
5085       return error_mark_node;
5086
5087     case TRUTH_AND_EXPR:
5088     case TRUTH_OR_EXPR:
5089     case TRUTH_XOR_EXPR:
5090     case LT_EXPR:
5091     case LE_EXPR:
5092     case GT_EXPR:
5093     case GE_EXPR:
5094     case EQ_EXPR:
5095     case NE_EXPR:
5096     case TRUTH_ANDIF_EXPR:
5097     case TRUTH_ORIF_EXPR:
5098     case TRUTH_NOT_EXPR:
5099       if (complain)
5100         error ("not enough type information");
5101       return error_mark_node;
5102
5103     case COND_EXPR:
5104       if (type_unknown_p (TREE_OPERAND (rhs, 0)))
5105         {
5106           if (complain)
5107             error ("not enough type information");
5108           return error_mark_node;
5109         }
5110       TREE_OPERAND (rhs, 1)
5111         = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), complain);
5112       if (TREE_OPERAND (rhs, 1) == error_mark_node)
5113         return error_mark_node;
5114       TREE_OPERAND (rhs, 2)
5115         = instantiate_type (lhstype, TREE_OPERAND (rhs, 2), complain);
5116       if (TREE_OPERAND (rhs, 2) == error_mark_node)
5117         return error_mark_node;
5118
5119       TREE_TYPE (rhs) = lhstype;
5120       return rhs;
5121
5122     case MODIFY_EXPR:
5123       TREE_OPERAND (rhs, 1)
5124         = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), complain);
5125       if (TREE_OPERAND (rhs, 1) == error_mark_node)
5126         return error_mark_node;
5127
5128       TREE_TYPE (rhs) = lhstype;
5129       return rhs;
5130       
5131     case ADDR_EXPR:
5132       if (TYPE_PTRMEMFUNC_P (lhstype))
5133         lhstype = TYPE_PTRMEMFUNC_FN_TYPE (lhstype);
5134       else if (TREE_CODE (lhstype) != POINTER_TYPE)
5135         {
5136           if (complain)
5137             error ("type for resolving address of overloaded function must be pointer type");
5138           return error_mark_node;
5139         }
5140       TREE_TYPE (rhs) = lhstype;
5141       lhstype = TREE_TYPE (lhstype);
5142       {
5143         tree fn = instantiate_type (lhstype, TREE_OPERAND (rhs, 0), complain);
5144         if (fn == error_mark_node)
5145           return error_mark_node;
5146         mark_addressable (fn);
5147         TREE_OPERAND (rhs, 0) = fn;
5148         TREE_CONSTANT (rhs) = staticp (fn);
5149       }
5150       return rhs;
5151
5152     case ENTRY_VALUE_EXPR:
5153       my_friendly_abort (184);
5154       return error_mark_node;
5155
5156     case ERROR_MARK:
5157       return error_mark_node;
5158
5159     default:
5160       my_friendly_abort (185);
5161       return error_mark_node;
5162     }
5163 }
5164 \f
5165 /* Return the name of the virtual function pointer field
5166    (as an IDENTIFIER_NODE) for the given TYPE.  Note that
5167    this may have to look back through base types to find the
5168    ultimate field name.  (For single inheritance, these could
5169    all be the same name.  Who knows for multiple inheritance).  */
5170 static tree
5171 get_vfield_name (type)
5172      tree type;
5173 {
5174   tree binfo = TYPE_BINFO (type);
5175   char *buf;
5176
5177   while (BINFO_BASETYPES (binfo)
5178          && TYPE_VIRTUAL_P (BINFO_TYPE (BINFO_BASETYPE (binfo, 0)))
5179          && ! TREE_VIA_VIRTUAL (BINFO_BASETYPE (binfo, 0)))
5180     binfo = BINFO_BASETYPE (binfo, 0);
5181
5182   type = BINFO_TYPE (binfo);
5183   buf = (char *)alloca (sizeof (VFIELD_NAME_FORMAT)
5184                         + TYPE_NAME_LENGTH (type) + 2);
5185   sprintf (buf, VFIELD_NAME_FORMAT, TYPE_NAME_STRING (type));
5186   return get_identifier (buf);
5187 }
5188
5189 void
5190 print_class_statistics ()
5191 {
5192 #ifdef GATHER_STATISTICS
5193   fprintf (stderr, "convert_harshness = %d\n", n_convert_harshness);
5194   fprintf (stderr, "compute_conversion_costs = %d\n", n_compute_conversion_costs);
5195   fprintf (stderr, "build_method_call = %d (inner = %d)\n",
5196            n_build_method_call, n_inner_fields_searched);
5197   if (n_vtables)
5198     {
5199       fprintf (stderr, "vtables = %d; vtable searches = %d\n",
5200                n_vtables, n_vtable_searches);
5201       fprintf (stderr, "vtable entries = %d; vtable elems = %d\n",
5202                n_vtable_entries, n_vtable_elems);
5203     }
5204 #endif
5205 }
5206
5207 /* Push an obstack which is sufficiently long-lived to hold such class
5208    decls that may be cached in the previous_class_values list.  For now, let's
5209    use the permanent obstack, later we may create a dedicated obstack just
5210    for this purpose.  The effect is undone by pop_obstacks.  */
5211 void
5212 maybe_push_cache_obstack ()
5213 {
5214   push_obstacks_nochange ();
5215   if (current_class_depth == 1)
5216     current_obstack = &permanent_obstack;
5217 }