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