OSDN Git Service

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