OSDN Git Service

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