OSDN Git Service

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