OSDN Git Service

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