OSDN Git Service

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