OSDN Git Service

pedwarn
[pf3gnuchains/gcc-fork.git] / gcc / cp / class.c
1 /* Functions related to building classes and their related objects.
2    Copyright (C) 1987, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3    1999, 2000 Free Software Foundation, Inc.
4    Contributed by Michael Tiemann (tiemann@cygnus.com)
5
6 This file is part of GNU CC.
7
8 GNU CC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
12
13 GNU CC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GNU CC; see the file COPYING.  If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA.  */
22
23
24 /* High-level class interface.  */
25
26 #include "config.h"
27 #include "system.h"
28 #include "tree.h"
29 #include "cp-tree.h"
30 #include "flags.h"
31 #include "rtl.h"
32 #include "output.h"
33 #include "toplev.h"
34 #include "ggc.h"
35 #include "lex.h"
36
37 #include "obstack.h"
38 #define obstack_chunk_alloc xmalloc
39 #define obstack_chunk_free free
40
41 /* The number of nested classes being processed.  If we are not in the
42    scope of any class, this is zero.  */
43
44 int current_class_depth;
45
46 /* In order to deal with nested classes, we keep a stack of classes.
47    The topmost entry is the innermost class, and is the entry at index
48    CURRENT_CLASS_DEPTH  */
49
50 typedef struct class_stack_node {
51   /* The name of the class.  */
52   tree name;
53
54   /* The _TYPE node for the class.  */
55   tree type;
56
57   /* The access specifier pending for new declarations in the scope of
58      this class.  */
59   tree access;
60
61   /* If were defining TYPE, the names used in this class.  */
62   splay_tree names_used;
63 }* class_stack_node_t;
64
65 typedef struct vcall_offset_data_s
66 {
67   /* The binfo for the most-derived type.  */
68   tree derived;
69   /* The binfo for the virtual base for which we're building
70      initializers.  */
71   tree vbase;
72   /* The vcall offset initializers built up so far.  */
73   tree inits;
74   /* The vtable index of the next vcall or vbase offset.  */
75   tree index;
76   /* Nonzero if we are building the initializer for the primary
77      vtable.  */
78   int primary_p;
79 } vcall_offset_data;
80
81 /* The stack itself.  This is an dynamically resized array.  The
82    number of elements allocated is CURRENT_CLASS_STACK_SIZE.  */
83 static int current_class_stack_size;
84 static class_stack_node_t current_class_stack;
85
86 static tree get_vfield_name PARAMS ((tree));
87 static void finish_struct_anon PARAMS ((tree));
88 static tree build_vbase_pointer PARAMS ((tree, tree));
89 static tree build_vtable_entry PARAMS ((tree, tree, tree));
90 static tree get_vtable_name PARAMS ((tree));
91 static tree get_derived_offset PARAMS ((tree, tree));
92 static tree get_basefndecls PARAMS ((tree, tree));
93 static int build_primary_vtable PARAMS ((tree, tree));
94 static int build_secondary_vtable PARAMS ((tree, tree));
95 static tree dfs_finish_vtbls PARAMS ((tree, void *));
96 static tree dfs_accumulate_vtbl_inits PARAMS ((tree, void *));
97 static void finish_vtbls PARAMS ((tree));
98 static void modify_vtable_entry PARAMS ((tree, tree, tree, tree, tree *));
99 static void add_virtual_function PARAMS ((tree *, tree *, int *, tree, tree));
100 static tree delete_duplicate_fields_1 PARAMS ((tree, tree));
101 static void delete_duplicate_fields PARAMS ((tree));
102 static void finish_struct_bits PARAMS ((tree));
103 static int alter_access PARAMS ((tree, tree, tree));
104 static void handle_using_decl PARAMS ((tree, tree));
105 static int overrides PARAMS ((tree, tree));
106 static int strictly_overrides PARAMS ((tree, tree));
107 static void mark_overriders PARAMS ((tree, tree));
108 static void check_for_override PARAMS ((tree, tree));
109 static tree dfs_modify_vtables PARAMS ((tree, void *));
110 static tree modify_all_vtables PARAMS ((tree, int *, tree));
111 static void determine_primary_base PARAMS ((tree, int *));
112 static void finish_struct_methods PARAMS ((tree));
113 static void maybe_warn_about_overly_private_class PARAMS ((tree));
114 static int field_decl_cmp PARAMS ((const tree *, const tree *));
115 static int method_name_cmp PARAMS ((const tree *, const tree *));
116 static tree add_implicitly_declared_members PARAMS ((tree, int, int, int));
117 static tree fixed_type_or_null PARAMS ((tree, int *));
118 static tree resolve_address_of_overloaded_function PARAMS ((tree, tree, int,
119                                                           int, tree));
120 static void build_vtable_entry_ref PARAMS ((tree, tree, tree));
121 static tree build_vtbl_initializer PARAMS ((tree, tree, int *));
122 static int count_fields PARAMS ((tree));
123 static int add_fields_to_vec PARAMS ((tree, tree, int));
124 static void check_bitfield_decl PARAMS ((tree));
125 static void check_field_decl PARAMS ((tree, tree, int *, int *, int *, int *));
126 static void check_field_decls PARAMS ((tree, tree *, int *, int *, int *, 
127                                      int *));
128 static void build_base_field PARAMS ((record_layout_info, tree, int *,
129                                       unsigned int *, varray_type *));
130 static varray_type build_base_fields PARAMS ((record_layout_info, int *));
131 static tree build_vbase_pointer_fields PARAMS ((record_layout_info, int *));
132 static tree build_vtbl_or_vbase_field PARAMS ((tree, tree, tree, tree, tree,
133                                                int *));
134 static void check_methods PARAMS ((tree));
135 static void remove_zero_width_bit_fields PARAMS ((tree));
136 static void check_bases PARAMS ((tree, int *, int *, int *));
137 static void check_bases_and_members PARAMS ((tree, int *));
138 static tree create_vtable_ptr PARAMS ((tree, int *, int *, tree *, tree *));
139 static void layout_class_type PARAMS ((tree, int *, int *, tree *, tree *));
140 static void fixup_pending_inline PARAMS ((struct pending_inline *));
141 static void fixup_inline_methods PARAMS ((tree));
142 static void set_primary_base PARAMS ((tree, int, int *));
143 static tree dfs_propagate_binfo_offsets PARAMS ((tree, void *));
144 static void propagate_binfo_offsets PARAMS ((tree, tree));
145 static void layout_virtual_bases PARAMS ((tree, varray_type *));
146 static tree dfs_set_offset_for_shared_vbases PARAMS ((tree, void *));
147 static tree dfs_set_offset_for_unshared_vbases PARAMS ((tree, void *));
148 static void build_vbase_offset_vtbl_entries PARAMS ((tree, vcall_offset_data *));
149 static tree dfs_build_vcall_offset_vtbl_entries PARAMS ((tree, void *));
150 static void build_vcall_offset_vtbl_entries PARAMS ((tree, vcall_offset_data *));
151 static void layout_vtable_decl PARAMS ((tree, int));
152 static tree dfs_find_final_overrider PARAMS ((tree, void *));
153 static tree find_final_overrider PARAMS ((tree, tree, tree));
154 static tree dfs_find_base PARAMS ((tree, void *));
155 static int make_new_vtable PARAMS ((tree, tree));
156 static void dump_class_hierarchy_r PARAMS ((tree, tree, int));
157 extern void dump_class_hierarchy PARAMS ((tree));
158 static tree build_vtable PARAMS ((tree, tree, tree));
159 static void initialize_vtable PARAMS ((tree, tree));
160 static void layout_nonempty_base_or_field PARAMS ((record_layout_info,
161                                                    tree, tree,
162                                                    varray_type));
163 static tree dfs_record_base_offsets PARAMS ((tree, void *));
164 static void record_base_offsets PARAMS ((tree, varray_type *));
165 static tree dfs_search_base_offsets PARAMS ((tree, void *));
166 static int layout_conflict_p PARAMS ((tree, varray_type));
167 static unsigned HOST_WIDE_INT end_of_class PARAMS ((tree, int));
168 static void layout_empty_base PARAMS ((tree, tree, varray_type));
169 static void accumulate_vtbl_inits PARAMS ((tree, tree));
170 static void set_vindex PARAMS ((tree, tree, int *));
171 static tree build_rtti_vtbl_entries PARAMS ((tree, tree));
172 static void build_vcall_and_vbase_vtbl_entries PARAMS ((tree, 
173                                                         vcall_offset_data *));
174 static tree dfs_mark_primary_bases PARAMS ((tree, void *));
175 static void mark_primary_bases PARAMS ((tree));
176 static void clone_constructors_and_destructors PARAMS ((tree));
177 static tree build_clone PARAMS ((tree, tree));
178 static void update_vtable_entry_for_fn PARAMS ((tree, tree, tree, tree *));
179
180 /* Variables shared between class.c and call.c.  */
181
182 #ifdef GATHER_STATISTICS
183 int n_vtables = 0;
184 int n_vtable_entries = 0;
185 int n_vtable_searches = 0;
186 int n_vtable_elems = 0;
187 int n_convert_harshness = 0;
188 int n_compute_conversion_costs = 0;
189 int n_build_method_call = 0;
190 int n_inner_fields_searched = 0;
191 #endif
192
193 /* Virtual base class layout.  */
194
195 /* Returns a list of virtual base class pointers as a chain of
196    FIELD_DECLS.  */
197
198 static tree
199 build_vbase_pointer_fields (rli, empty_p)
200      record_layout_info rli;
201      int *empty_p;
202 {
203   /* Chain to hold all the new FIELD_DECLs which point at virtual
204      base classes.  */
205   tree rec = rli->t;
206   tree vbase_decls = NULL_TREE;
207   tree binfos = TYPE_BINFO_BASETYPES (rec);
208   int n_baseclasses = CLASSTYPE_N_BASECLASSES (rec);
209   tree decl;
210   int i;
211
212   /* Under the new ABI, there are no vbase pointers in the object.
213      Instead, the offsets are stored in the vtable.  */
214   if (vbase_offsets_in_vtable_p ())
215     return NULL_TREE;
216
217   /* Loop over the baseclasses, adding vbase pointers as needed.  */
218   for (i = 0; i < n_baseclasses; i++)
219     {
220       register tree base_binfo = TREE_VEC_ELT (binfos, i);
221       register tree basetype = BINFO_TYPE (base_binfo);
222
223       if (!COMPLETE_TYPE_P (basetype))
224         /* This error is now reported in xref_tag, thus giving better
225            location information.  */
226         continue;
227
228       /* All basetypes are recorded in the association list of the
229          derived type.  */
230
231       if (TREE_VIA_VIRTUAL (base_binfo))
232         {
233           int j;
234           const char *name;
235
236           /* The offset for a virtual base class is only used in computing
237              virtual function tables and for initializing virtual base
238              pointers.  It is built once `get_vbase_types' is called.  */
239
240           /* If this basetype can come from another vbase pointer
241              without an additional indirection, we will share
242              that pointer.  If an indirection is involved, we
243              make our own pointer.  */
244           for (j = 0; j < n_baseclasses; j++)
245             {
246               tree other_base_binfo = TREE_VEC_ELT (binfos, j);
247               if (! TREE_VIA_VIRTUAL (other_base_binfo)
248                   && BINFO_FOR_VBASE (basetype, BINFO_TYPE (other_base_binfo)))
249                 goto got_it;
250             }
251           FORMAT_VBASE_NAME (name, basetype);
252           decl = build_vtbl_or_vbase_field (get_identifier (name), 
253                                             get_identifier (VTABLE_BASE),
254                                             build_pointer_type (basetype),
255                                             rec,
256                                             basetype,
257                                             empty_p);
258           BINFO_VPTR_FIELD (base_binfo) = decl;
259           TREE_CHAIN (decl) = vbase_decls;
260           place_field (rli, decl);
261           vbase_decls = decl;
262           *empty_p = 0;
263
264         got_it:
265           /* The space this decl occupies has already been accounted for.  */
266           ;
267         }
268     }
269
270   return vbase_decls;
271 }
272
273 /* Returns a pointer to the virtual base class of EXP that has the
274    indicated TYPE.  EXP is of class type, not a pointer type.  */
275
276 static tree
277 build_vbase_pointer (exp, type)
278      tree exp, type;
279 {
280   if (vbase_offsets_in_vtable_p ())
281     {
282       tree vbase;
283       tree vbase_ptr;
284
285       /* Find the shared copy of TYPE; that's where the vtable offset
286          is recorded.  */
287       vbase = BINFO_FOR_VBASE (type, TREE_TYPE (exp));
288       /* Find the virtual function table pointer.  */
289       vbase_ptr = build_vfield_ref (exp, TREE_TYPE (exp));
290       /* Compute the location where the offset will lie.  */
291       vbase_ptr = build (PLUS_EXPR, 
292                          TREE_TYPE (vbase_ptr),
293                          vbase_ptr,
294                          BINFO_VPTR_FIELD (vbase));
295       vbase_ptr = build1 (NOP_EXPR, 
296                           build_pointer_type (ptrdiff_type_node),
297                           vbase_ptr);
298       /* Add the contents of this location to EXP.  */
299       return build (PLUS_EXPR,
300                     build_pointer_type (type),
301                     build_unary_op (ADDR_EXPR, exp, /*noconvert=*/0),
302                     build1 (INDIRECT_REF, ptrdiff_type_node, vbase_ptr));
303     }
304   else
305     {
306       char *name;
307       FORMAT_VBASE_NAME (name, type);
308       return build_component_ref (exp, get_identifier (name), NULL_TREE, 0);
309     }
310 }
311
312 /* Build multi-level access to EXPR using hierarchy path PATH.
313    CODE is PLUS_EXPR if we are going with the grain,
314    and MINUS_EXPR if we are not (in which case, we cannot traverse
315    virtual baseclass links).
316
317    TYPE is the type we want this path to have on exit.
318
319    NONNULL is non-zero if  we know (for any reason) that EXPR is
320    not, in fact, zero.  */
321
322 tree
323 build_vbase_path (code, type, expr, path, nonnull)
324      enum tree_code code;
325      tree type, expr, path;
326      int nonnull;
327 {
328   register int changed = 0;
329   tree last = NULL_TREE, last_virtual = NULL_TREE;
330   int fixed_type_p;
331   tree null_expr = 0, nonnull_expr;
332   tree basetype;
333   tree offset = integer_zero_node;
334
335   if (BINFO_INHERITANCE_CHAIN (path) == NULL_TREE)
336     return build1 (NOP_EXPR, type, expr);
337
338   /* We could do better if we had additional logic to convert back to the
339      unconverted type (the static type of the complete object), and then
340      convert back to the type we want.  Until that is done, we only optimize
341      if the complete type is the same type as expr has.  */
342   fixed_type_p = resolves_to_fixed_type_p (expr, &nonnull);
343
344   if (!fixed_type_p && TREE_SIDE_EFFECTS (expr))
345     expr = save_expr (expr);
346   nonnull_expr = expr;
347
348   path = reverse_path (path);
349
350   basetype = BINFO_TYPE (path);
351
352   while (path)
353     {
354       if (TREE_VIA_VIRTUAL (TREE_VALUE (path)))
355         {
356           last_virtual = BINFO_TYPE (TREE_VALUE (path));
357           if (code == PLUS_EXPR)
358             {
359               changed = ! fixed_type_p;
360
361               if (changed)
362                 {
363                   tree ind;
364
365                   /* We already check for ambiguous things in the caller, just
366                      find a path.  */
367                   if (last)
368                     {
369                       tree binfo = get_binfo (last, TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (nonnull_expr))), 0);
370                       nonnull_expr = convert_pointer_to_real (binfo, nonnull_expr);
371                     }
372                   ind = build_indirect_ref (nonnull_expr, NULL_PTR);
373                   nonnull_expr = build_vbase_pointer (ind, last_virtual);
374                   if (nonnull == 0
375                       && TREE_CODE (type) == POINTER_TYPE
376                       && null_expr == NULL_TREE)
377                     {
378                       null_expr = build1 (NOP_EXPR, build_pointer_type (last_virtual), integer_zero_node);
379                       expr = build (COND_EXPR, build_pointer_type (last_virtual),
380                                     build (EQ_EXPR, boolean_type_node, expr,
381                                            integer_zero_node),
382                                     null_expr, nonnull_expr);
383                     }
384                 }
385               /* else we'll figure out the offset below.  */
386
387               /* Happens in the case of parse errors.  */
388               if (nonnull_expr == error_mark_node)
389                 return error_mark_node;
390             }
391           else
392             {
393               cp_error ("cannot cast up from virtual baseclass `%T'",
394                           last_virtual);
395               return error_mark_node;
396             }
397         }
398       last = TREE_VALUE (path);
399       path = TREE_CHAIN (path);
400     }
401   /* LAST is now the last basetype assoc on the path.  */
402
403   /* A pointer to a virtual base member of a non-null object
404      is non-null.  Therefore, we only need to test for zeroness once.
405      Make EXPR the canonical expression to deal with here.  */
406   if (null_expr)
407     {
408       TREE_OPERAND (expr, 2) = nonnull_expr;
409       TREE_TYPE (expr) = TREE_TYPE (TREE_OPERAND (expr, 1))
410         = TREE_TYPE (nonnull_expr);
411     }
412   else
413     expr = nonnull_expr;
414
415   /* If we go through any virtual base pointers, make sure that
416      casts to BASETYPE from the last virtual base class use
417      the right value for BASETYPE.  */
418   if (changed)
419     {
420       tree intype = TREE_TYPE (TREE_TYPE (expr));
421
422       if (TYPE_MAIN_VARIANT (intype) != BINFO_TYPE (last))
423         offset
424           = BINFO_OFFSET (get_binfo (last, TYPE_MAIN_VARIANT (intype), 0));
425     }
426   else
427     offset = BINFO_OFFSET (last);
428
429   if (! integer_zerop (offset))
430     {
431       /* Bash types to make the backend happy.  */
432       offset = cp_convert (type, offset);
433
434       /* If expr might be 0, we need to preserve that zeroness.  */
435       if (nonnull == 0)
436         {
437           if (null_expr)
438             TREE_TYPE (null_expr) = type;
439           else
440             null_expr = build1 (NOP_EXPR, type, integer_zero_node);
441           if (TREE_SIDE_EFFECTS (expr))
442             expr = save_expr (expr);
443
444           return build (COND_EXPR, type,
445                         build (EQ_EXPR, boolean_type_node, expr, integer_zero_node),
446                         null_expr,
447                         build (code, type, expr, offset));
448         }
449       else return build (code, type, expr, offset);
450     }
451
452   /* Cannot change the TREE_TYPE of a NOP_EXPR here, since it may
453      be used multiple times in initialization of multiple inheritance.  */
454   if (null_expr)
455     {
456       TREE_TYPE (expr) = type;
457       return expr;
458     }
459   else
460     return build1 (NOP_EXPR, type, expr);
461 }
462
463 \f
464 /* Virtual function things.  */
465
466 /* We want to give the assembler the vtable identifier as well as
467    the offset to the function pointer.  So we generate
468
469    __asm__ __volatile__ (".vtable_entry %c0, %c1"
470       : : "s"(&class_vtable),
471           "i"((long)&vtbl[idx].pfn - (long)&vtbl[0])); */
472
473 static void
474 build_vtable_entry_ref (basetype, vtbl, idx)
475      tree basetype, vtbl, idx;
476 {
477   static char asm_stmt[] = ".vtable_entry %c0, %c1";
478   tree s, i, i2;
479
480   s = build_unary_op (ADDR_EXPR, get_vtbl_decl_for_binfo (basetype), 0);
481   s = build_tree_list (build_string (1, "s"), s);
482
483   i = build_array_ref (vtbl, idx);
484   if (!flag_vtable_thunks)
485     i = build_component_ref (i, pfn_identifier, vtable_entry_type, 0);
486   i = build_c_cast (ptrdiff_type_node, build_unary_op (ADDR_EXPR, i, 0));
487   i2 = build_array_ref (vtbl, build_int_2(0,0));
488   i2 = build_c_cast (ptrdiff_type_node, build_unary_op (ADDR_EXPR, i2, 0));
489   i = build_binary_op (MINUS_EXPR, i, i2);
490   i = build_tree_list (build_string (1, "i"), i);
491
492   finish_asm_stmt (ridpointers[RID_VOLATILE],
493                    build_string (sizeof(asm_stmt)-1, asm_stmt),
494                    NULL_TREE, chainon (s, i), NULL_TREE);
495 }
496
497 /* Given an object INSTANCE, return an expression which yields the
498    virtual function vtable element corresponding to INDEX.  There are
499    many special cases for INSTANCE which we take care of here, mainly
500    to avoid creating extra tree nodes when we don't have to.  */
501
502 tree
503 build_vtbl_ref (instance, idx)
504      tree instance, idx;
505 {
506   tree vtbl, aref;
507   tree basetype = TREE_TYPE (instance);
508
509   if (TREE_CODE (basetype) == REFERENCE_TYPE)
510     basetype = TREE_TYPE (basetype);
511
512   if (instance == current_class_ref)
513     vtbl = build_vfield_ref (instance, basetype);
514   else
515     {
516       if (optimize)
517         {
518           /* Try to figure out what a reference refers to, and
519              access its virtual function table directly.  */
520           tree ref = NULL_TREE;
521
522           if (TREE_CODE (instance) == INDIRECT_REF
523               && TREE_CODE (TREE_TYPE (TREE_OPERAND (instance, 0))) == REFERENCE_TYPE)
524             ref = TREE_OPERAND (instance, 0);
525           else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
526             ref = instance;
527
528           if (ref && TREE_CODE (ref) == VAR_DECL
529               && DECL_INITIAL (ref))
530             {
531               tree init = DECL_INITIAL (ref);
532
533               while (TREE_CODE (init) == NOP_EXPR
534                      || TREE_CODE (init) == NON_LVALUE_EXPR)
535                 init = TREE_OPERAND (init, 0);
536               if (TREE_CODE (init) == ADDR_EXPR)
537                 {
538                   init = TREE_OPERAND (init, 0);
539                   if (IS_AGGR_TYPE (TREE_TYPE (init))
540                       && (TREE_CODE (init) == PARM_DECL
541                           || TREE_CODE (init) == VAR_DECL))
542                     instance = init;
543                 }
544             }
545         }
546
547       if (IS_AGGR_TYPE (TREE_TYPE (instance))
548           && (TREE_CODE (instance) == RESULT_DECL
549               || TREE_CODE (instance) == PARM_DECL
550               || TREE_CODE (instance) == VAR_DECL))
551         {
552           vtbl = TYPE_BINFO_VTABLE (basetype);
553           /* Knowing the dynamic type of INSTANCE we can easily obtain
554              the correct vtable entry.  In the new ABI, we resolve
555              this back to be in terms of the primary vtable.  */
556           if (TREE_CODE (vtbl) == PLUS_EXPR)
557             {
558               idx = fold (build (PLUS_EXPR,
559                                  TREE_TYPE (idx),
560                                  idx,
561                                  build (EXACT_DIV_EXPR,
562                                         TREE_TYPE (idx),
563                                         TREE_OPERAND (vtbl, 1),
564                                         TYPE_SIZE_UNIT (vtable_entry_type))));
565               vtbl = get_vtbl_decl_for_binfo (TYPE_BINFO (basetype));
566             }
567         }
568       else
569         vtbl = build_vfield_ref (instance, basetype);
570     }
571
572   assemble_external (vtbl);
573
574   if (flag_vtable_gc)
575     build_vtable_entry_ref (basetype, vtbl, idx);
576
577   aref = build_array_ref (vtbl, idx);
578
579   return aref;
580 }
581
582 /* Given an object INSTANCE, return an expression which yields the
583    virtual function corresponding to INDEX.  There are many special
584    cases for INSTANCE which we take care of here, mainly to avoid
585    creating extra tree nodes when we don't have to.  */
586
587 tree
588 build_vfn_ref (ptr_to_instptr, instance, idx)
589      tree *ptr_to_instptr, instance;
590      tree idx;
591 {
592   tree aref = build_vtbl_ref (instance, idx);
593
594   /* When using thunks, there is no extra delta, and we get the pfn
595      directly.  */
596   if (flag_vtable_thunks)
597     return aref;
598
599   if (ptr_to_instptr)
600     {
601       /* Save the intermediate result in a SAVE_EXPR so we don't have to
602          compute each component of the virtual function pointer twice.  */ 
603       if (TREE_CODE (aref) == INDIRECT_REF)
604         TREE_OPERAND (aref, 0) = save_expr (TREE_OPERAND (aref, 0));
605
606       *ptr_to_instptr
607         = build (PLUS_EXPR, TREE_TYPE (*ptr_to_instptr),
608                  *ptr_to_instptr,
609                  cp_convert (ptrdiff_type_node,
610                              build_component_ref (aref, delta_identifier, NULL_TREE, 0)));
611     }
612
613   return build_component_ref (aref, pfn_identifier, NULL_TREE, 0);
614 }
615
616 /* Return the name of the virtual function table (as an IDENTIFIER_NODE)
617    for the given TYPE.  */
618
619 static tree
620 get_vtable_name (type)
621      tree type;
622 {
623   tree type_id = build_typename_overload (type);
624   char *buf = (char *) alloca (strlen (VTABLE_NAME_PREFIX)
625                                + IDENTIFIER_LENGTH (type_id) + 2);
626   const char *ptr = IDENTIFIER_POINTER (type_id);
627   int i;
628   for (i = 0; ptr[i] == OPERATOR_TYPENAME_FORMAT[i]; i++) ;
629 #if 0
630   /* We don't take off the numbers; build_secondary_vtable uses the
631      DECL_ASSEMBLER_NAME for the type, which includes the number
632      in `3foo'.  If we were to pull them off here, we'd end up with
633      something like `_vt.foo.3bar', instead of a uniform definition.  */
634   while (ptr[i] >= '0' && ptr[i] <= '9')
635     i += 1;
636 #endif
637   sprintf (buf, "%s%s", VTABLE_NAME_PREFIX, ptr+i);
638   return get_identifier (buf);
639 }
640
641 /* Return the offset to the main vtable for a given base BINFO.  */
642
643 tree
644 get_vfield_offset (binfo)
645      tree binfo;
646 {
647   return
648     size_binop (PLUS_EXPR, byte_position (TYPE_VFIELD (BINFO_TYPE (binfo))),
649                 BINFO_OFFSET (binfo));
650 }
651
652 /* Get the offset to the start of the original binfo that we derived
653    this binfo from.  If we find TYPE first, return the offset only
654    that far.  The shortened search is useful because the this pointer
655    on method calling is expected to point to a DECL_CONTEXT (fndecl)
656    object, and not a baseclass of it.   */
657
658
659 static tree
660 get_derived_offset (binfo, type)
661      tree binfo, type;
662 {
663   tree offset1 = get_vfield_offset (TYPE_BINFO (BINFO_TYPE (binfo)));
664   tree offset2;
665   int i;
666
667   while (BINFO_BASETYPES (binfo)
668          && (i = CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo))) != -1)
669     {
670       tree binfos = BINFO_BASETYPES (binfo);
671       if (BINFO_TYPE (binfo) == type)
672         break;
673       binfo = TREE_VEC_ELT (binfos, i);
674     }
675
676   offset2 = get_vfield_offset (TYPE_BINFO (BINFO_TYPE (binfo)));
677   return size_binop (MINUS_EXPR, offset1, offset2);
678 }
679
680 /* Create a VAR_DECL for a primary or secondary vtable for
681    CLASS_TYPE.  Use NAME for the name of the vtable, and VTABLE_TYPE
682    for its type.  */
683
684 static tree
685 build_vtable (class_type, name, vtable_type)
686      tree class_type;
687      tree name;
688      tree vtable_type;
689 {
690   tree decl;
691
692   decl = build_lang_decl (VAR_DECL, name, vtable_type);
693   DECL_CONTEXT (decl) = class_type;
694   DECL_ARTIFICIAL (decl) = 1;
695   TREE_STATIC (decl) = 1;
696 #ifndef WRITABLE_VTABLES
697   /* Make them READONLY by default. (mrs) */
698   TREE_READONLY (decl) = 1;
699 #endif
700   DECL_VIRTUAL_P (decl) = 1;
701   import_export_vtable (decl, class_type, 0);
702
703   return decl;
704 }
705
706 /* Get the VAR_DECL of the vtable for TYPE. TYPE need not be polymorphic,
707    or even complete.  If this does not exist, create it.  If COMPLETE is
708    non-zero, then complete the definition of it -- that will render it
709    impossible to actually build the vtable, but is useful to get at those
710    which are known to exist in the runtime.  */
711
712 tree 
713 get_vtable_decl (type, complete)
714      tree type;
715      int complete;
716 {
717   tree name = get_vtable_name (type);
718   tree decl = IDENTIFIER_GLOBAL_VALUE (name);
719   
720   if (decl)
721     {
722       my_friendly_assert (TREE_CODE (decl) == VAR_DECL
723                           && DECL_VIRTUAL_P (decl), 20000118);
724       return decl;
725     }
726   
727   decl = build_vtable (type, name, void_type_node);
728   decl = pushdecl_top_level (decl);
729   SET_IDENTIFIER_GLOBAL_VALUE (name, decl);
730   
731   /* At one time the vtable info was grabbed 2 words at a time.  This
732      fails on sparc unless you have 8-byte alignment.  (tiemann) */
733   DECL_ALIGN (decl) = MAX (TYPE_ALIGN (double_type_node),
734                            DECL_ALIGN (decl));
735
736   if (complete)
737     {
738       DECL_EXTERNAL (decl) = 1;
739       cp_finish_decl (decl, NULL_TREE, NULL_TREE, 0);
740     }
741
742   return decl;
743 }
744
745 /* Build the primary virtual function table for TYPE.  If BINFO is
746    non-NULL, build the vtable starting with the initial approximation
747    that it is the same as the one which is the head of the association
748    list.  Returns a non-zero value if a new vtable is actually
749    created.  */
750
751 static int
752 build_primary_vtable (binfo, type)
753      tree binfo, type;
754 {
755   tree virtuals, decl;
756
757   decl = get_vtable_decl (type, /*complete=*/0);
758   
759   if (binfo)
760     {
761       if (BINFO_NEW_VTABLE_MARKED (binfo, type))
762         /* We have already created a vtable for this base, so there's
763            no need to do it again.  */
764         return 0;
765       
766       virtuals = copy_list (BINFO_VIRTUALS (binfo));
767       TREE_TYPE (decl) = TREE_TYPE (get_vtbl_decl_for_binfo (binfo));
768       DECL_SIZE (decl) = TYPE_SIZE (TREE_TYPE (decl));
769       DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
770     }
771   else
772     {
773       my_friendly_assert (TREE_CODE (TREE_TYPE (decl)) == VOID_TYPE,
774                           20000118);
775       virtuals = NULL_TREE;
776     }
777
778 #ifdef GATHER_STATISTICS
779   n_vtables += 1;
780   n_vtable_elems += list_length (virtuals);
781 #endif
782
783   /* Initialize the association list for this type, based
784      on our first approximation.  */
785   TYPE_BINFO_VTABLE (type) = decl;
786   TYPE_BINFO_VIRTUALS (type) = virtuals;
787
788   binfo = TYPE_BINFO (type);
789   SET_BINFO_NEW_VTABLE_MARKED (binfo, type);
790   return 1;
791 }
792
793 /* Give TYPE a new virtual function table which is initialized
794    with a skeleton-copy of its original initialization.  The only
795    entry that changes is the `delta' entry, so we can really
796    share a lot of structure.
797
798    FOR_TYPE is the derived type which caused this table to
799    be needed.
800
801    BINFO is the type association which provided TYPE for FOR_TYPE.
802
803    The order in which vtables are built (by calling this function) for
804    an object must remain the same, otherwise a binary incompatibility
805    can result.  */
806
807 static int
808 build_secondary_vtable (binfo, for_type)
809      tree binfo, for_type;
810 {
811   tree basetype;
812   tree orig_decl = BINFO_VTABLE (binfo);
813   tree name;
814   tree new_decl;
815   tree offset;
816   tree path = binfo;
817   char *buf, *buf2;
818   char joiner = '_';
819   int i;
820
821 #ifdef JOINER
822   joiner = JOINER;
823 #endif
824
825   if (TREE_VIA_VIRTUAL (binfo))
826     my_friendly_assert (binfo == BINFO_FOR_VBASE (BINFO_TYPE (binfo),
827                                                   current_class_type),
828                         170);
829
830   if (BINFO_NEW_VTABLE_MARKED (binfo, current_class_type))
831     /* We already created a vtable for this base.  There's no need to
832        do it again.  */
833     return 0;
834
835   /* Remember that we've created a vtable for this BINFO, so that we
836      don't try to do so again.  */
837   SET_BINFO_NEW_VTABLE_MARKED (binfo, current_class_type);
838   
839   /* Make fresh virtual list, so we can smash it later.  */
840   BINFO_VIRTUALS (binfo) = copy_list (BINFO_VIRTUALS (binfo));
841
842   if (TREE_VIA_VIRTUAL (binfo))
843     {
844       tree binfo1 = BINFO_FOR_VBASE (BINFO_TYPE (binfo), for_type);
845
846       /* XXX - This should never happen, if it does, the caller should
847          ensure that the binfo is from for_type's binfos, not from any
848          base type's.  We can remove all this code after a while.  */
849       if (binfo1 != binfo)
850         warning ("internal inconsistency: binfo offset error for rtti");
851
852       offset = BINFO_OFFSET (binfo1);
853     }
854   else
855     offset = BINFO_OFFSET (binfo);
856
857   /* In the new ABI, secondary vtables are laid out as part of the
858      same structure as the primary vtable.  */
859   if (merge_primary_and_secondary_vtables_p ())
860     {
861       BINFO_VTABLE (binfo) = NULL_TREE;
862       return 1;
863     }
864
865   /* Create the declaration for the secondary vtable.  */
866   basetype = TYPE_MAIN_VARIANT (BINFO_TYPE (binfo));
867   buf2 = TYPE_ASSEMBLER_NAME_STRING (basetype);
868   i = TYPE_ASSEMBLER_NAME_LENGTH (basetype) + 1;
869
870   /* We know that the vtable that we are going to create doesn't exist
871      yet in the global namespace, and when we finish, it will be
872      pushed into the global namespace.  In complex MI hierarchies, we
873      have to loop while the name we are thinking of adding is globally
874      defined, adding more name components to the vtable name as we
875      loop, until the name is unique.  This is because in complex MI
876      cases, we might have the same base more than once.  This means
877      that the order in which this function is called for vtables must
878      remain the same, otherwise binary compatibility can be
879      compromised.  */
880
881   while (1)
882     {
883       char *buf1 = (char *) alloca (TYPE_ASSEMBLER_NAME_LENGTH (for_type)
884                                     + 1 + i);
885       char *new_buf2;
886
887       sprintf (buf1, "%s%c%s", TYPE_ASSEMBLER_NAME_STRING (for_type), joiner,
888                buf2);
889       buf = (char *) alloca (strlen (VTABLE_NAME_PREFIX) + strlen (buf1) + 1);
890       sprintf (buf, "%s%s", VTABLE_NAME_PREFIX, buf1);
891       name = get_identifier (buf);
892
893       /* If this name doesn't clash, then we can use it, otherwise
894          we add more to the name until it is unique.  */
895
896       if (! IDENTIFIER_GLOBAL_VALUE (name))
897         break;
898
899       /* Set values for next loop through, if the name isn't unique.  */
900
901       path = BINFO_INHERITANCE_CHAIN (path);
902
903       /* We better not run out of stuff to make it unique.  */
904       my_friendly_assert (path != NULL_TREE, 368);
905
906       basetype = TYPE_MAIN_VARIANT (BINFO_TYPE (path));
907
908       if (for_type == basetype)
909         {
910           /* If we run out of basetypes in the path, we have already
911              found created a vtable with that name before, we now
912              resort to tacking on _%d to distinguish them.  */
913           int j = 2;
914           i = TYPE_ASSEMBLER_NAME_LENGTH (basetype) + 1 + i + 1 + 3;
915           buf1 = (char *) alloca (i);
916           do {
917             sprintf (buf1, "%s%c%s%c%d",
918                      TYPE_ASSEMBLER_NAME_STRING (basetype), joiner,
919                      buf2, joiner, j);
920             buf = (char *) alloca (strlen (VTABLE_NAME_PREFIX)
921                                    + strlen (buf1) + 1);
922             sprintf (buf, "%s%s", VTABLE_NAME_PREFIX, buf1);
923             name = get_identifier (buf);
924
925             /* If this name doesn't clash, then we can use it,
926                otherwise we add something different to the name until
927                it is unique.  */
928           } while (++j <= 999 && IDENTIFIER_GLOBAL_VALUE (name));
929
930           /* Hey, they really like MI don't they?  Increase the 3
931              above to 6, and the 999 to 999999.  :-)  */
932           my_friendly_assert (j <= 999, 369);
933
934           break;
935         }
936
937       i = TYPE_ASSEMBLER_NAME_LENGTH (basetype) + 1 + i;
938       new_buf2 = (char *) alloca (i);
939       sprintf (new_buf2, "%s%c%s",
940                TYPE_ASSEMBLER_NAME_STRING (basetype), joiner, buf2);
941       buf2 = new_buf2;
942     }
943
944   new_decl = build_vtable (for_type, name, TREE_TYPE (orig_decl));
945   DECL_ALIGN (new_decl) = DECL_ALIGN (orig_decl);
946   BINFO_VTABLE (binfo) = pushdecl_top_level (new_decl);
947
948 #ifdef GATHER_STATISTICS
949   n_vtables += 1;
950   n_vtable_elems += list_length (BINFO_VIRTUALS (binfo));
951 #endif
952
953   return 1;
954 }
955
956 /* Create a new vtable for BINFO which is the hierarchy dominated by
957    T.  */
958
959 static int
960 make_new_vtable (t, binfo)
961      tree t;
962      tree binfo;
963 {
964   if (binfo == TYPE_BINFO (t))
965     /* In this case, it is *type*'s vtable we are modifying.  We start
966        with the approximation that it's vtable is that of the
967        immediate base class.  */
968     return build_primary_vtable (TYPE_BINFO (DECL_CONTEXT (TYPE_VFIELD (t))), 
969                                  t);
970   else
971     /* This is our very own copy of `basetype' to play with.  Later,
972        we will fill in all the virtual functions that override the
973        virtual functions in these base classes which are not defined
974        by the current type.  */
975     return build_secondary_vtable (binfo, t);
976 }
977
978 /* Make *VIRTUALS, an entry on the BINFO_VIRTUALS list for BINFO
979    (which is in the hierarchy dominated by T) list FNDECL as its
980    BV_FN.  DELTA is the required constant adjustment from the `this'
981    pointer where the vtable entry appears to the `this' required when
982    the function is actually called.  */
983
984 static void
985 modify_vtable_entry (t, binfo, fndecl, delta, virtuals)
986      tree t;
987      tree binfo;
988      tree fndecl;
989      tree delta;
990      tree *virtuals;
991 {
992   tree v;
993
994   v = *virtuals;
995
996   if (fndecl != BV_FN (v)
997       || !tree_int_cst_equal (delta, BV_DELTA (v)))
998     {
999       tree base_fndecl;
1000
1001       /* We need a new vtable for BINFO.  */
1002       if (make_new_vtable (t, binfo))
1003         {
1004           /* If we really did make a new vtable, we also made a copy
1005              of the BINFO_VIRTUALS list.  Now, we have to find the
1006              corresponding entry in that list.  */
1007           *virtuals = BINFO_VIRTUALS (binfo);
1008           while (BV_FN (*virtuals) != BV_FN (v))
1009             *virtuals = TREE_CHAIN (*virtuals);
1010           v = *virtuals;
1011         }
1012
1013       base_fndecl = BV_FN (v);
1014       BV_DELTA (v) = delta;
1015       BV_VCALL_INDEX (v) = integer_zero_node;
1016       BV_FN (v) = fndecl;
1017
1018       /* Now assign virtual dispatch information, if unset.  We can
1019          dispatch this, through any overridden base function.  */
1020       if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
1021         {
1022           DECL_VINDEX (fndecl) = DECL_VINDEX (base_fndecl);
1023           DECL_VIRTUAL_CONTEXT (fndecl) = DECL_VIRTUAL_CONTEXT (base_fndecl);
1024         }
1025     }
1026 }
1027
1028 /* Return the index (in the virtual function table) of the first
1029    virtual function.  */
1030
1031 int
1032 first_vfun_index (t)
1033      tree t;
1034 {
1035   /* Under the old ABI, the offset-to-top and RTTI entries are at
1036      indices zero and one; under the new ABI, the first virtual
1037      function is at index zero.  */
1038   if (!CLASSTYPE_COM_INTERFACE (t) && !flag_new_abi)
1039     return flag_vtable_thunks ? 2 : 1;
1040
1041   return 0;
1042 }
1043
1044 /* Set DECL_VINDEX for DECL.  VINDEX_P is the number of virtual
1045    functions present in the vtable so far.  */
1046
1047 static void
1048 set_vindex (t, decl, vfuns_p)
1049      tree t;
1050      tree decl;
1051      int *vfuns_p;
1052 {
1053   int vindex;
1054
1055   vindex = (*vfuns_p)++;
1056   vindex += first_vfun_index (t);
1057   DECL_VINDEX (decl) = build_shared_int_cst (vindex);
1058 }
1059
1060 /* Add a virtual function to all the appropriate vtables for the class
1061    T.  DECL_VINDEX(X) should be error_mark_node, if we want to
1062    allocate a new slot in our table.  If it is error_mark_node, we
1063    know that no other function from another vtable is overridden by X.
1064    VFUNS_P keeps track of how many virtuals there are in our
1065    main vtable for the type, and we build upon the NEW_VIRTUALS list
1066    and return it.  */
1067
1068 static void
1069 add_virtual_function (new_virtuals_p, overridden_virtuals_p,
1070                       vfuns_p, fndecl, t)
1071      tree *new_virtuals_p;
1072      tree *overridden_virtuals_p;
1073      int *vfuns_p;
1074      tree fndecl;
1075      tree t; /* Structure type.  */
1076 {
1077   tree new_virtual;
1078
1079   /* If this function doesn't override anything from a base class, we
1080      can just assign it a new DECL_VINDEX now.  Otherwise, if it does
1081      override something, we keep it around and assign its DECL_VINDEX
1082      later, in modify_all_vtables.  */
1083   if (TREE_CODE (DECL_VINDEX (fndecl)) == INTEGER_CST)
1084     /* We've already dealt with this function.  */
1085     return;
1086
1087   new_virtual = build_tree_list (NULL_TREE, fndecl);
1088   BV_DELTA (new_virtual) = integer_zero_node;
1089   BV_VCALL_INDEX (new_virtual) = integer_zero_node;
1090
1091   if (DECL_VINDEX (fndecl) == error_mark_node)
1092     {
1093       /* FNDECL is a new virtual function; it doesn't override any
1094          virtual function in a base class.  */
1095
1096       /* We remember that this was the base sub-object for rtti.  */
1097       CLASSTYPE_RTTI (t) = t;
1098
1099       /* Now assign virtual dispatch information.  */
1100       set_vindex (t, fndecl, vfuns_p);
1101       DECL_VIRTUAL_CONTEXT (fndecl) = t;
1102
1103       /* Save the state we've computed on the NEW_VIRTUALS list.  */
1104       TREE_CHAIN (new_virtual) = *new_virtuals_p;
1105       *new_virtuals_p = new_virtual;
1106     }
1107   else
1108     {
1109       /* FNDECL overrides a function from a base class.  */
1110       TREE_CHAIN (new_virtual) = *overridden_virtuals_p;
1111       *overridden_virtuals_p = new_virtual;
1112     }
1113 }
1114 \f
1115 extern struct obstack *current_obstack;
1116
1117 /* Add method METHOD to class TYPE.
1118
1119    If non-NULL, FIELDS is the entry in the METHOD_VEC vector entry of
1120    the class type where the method should be added.  */
1121
1122 void
1123 add_method (type, fields, method)
1124      tree type, *fields, method;
1125 {
1126   int using = (DECL_CONTEXT (method) != type);
1127   
1128   if (fields && *fields)
1129     *fields = build_overload (method, *fields);
1130   else 
1131     {
1132       int len;
1133       int slot;
1134       tree method_vec;
1135
1136       if (!CLASSTYPE_METHOD_VEC (type))
1137         /* Make a new method vector.  We start with 8 entries.  We must
1138            allocate at least two (for constructors and destructors), and
1139            we're going to end up with an assignment operator at some
1140            point as well.  
1141
1142            We could use a TREE_LIST for now, and convert it to a
1143            TREE_VEC in finish_struct, but we would probably waste more
1144            memory making the links in the list than we would by
1145            over-allocating the size of the vector here.  Furthermore,
1146            we would complicate all the code that expects this to be a
1147            vector.  */
1148         CLASSTYPE_METHOD_VEC (type) = make_tree_vec (8);
1149
1150       method_vec = CLASSTYPE_METHOD_VEC (type);
1151       len = TREE_VEC_LENGTH (method_vec);
1152
1153       /* Constructors and destructors go in special slots.  */
1154       if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (method))
1155         slot = CLASSTYPE_CONSTRUCTOR_SLOT;
1156       else if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (method))
1157         slot = CLASSTYPE_DESTRUCTOR_SLOT;
1158       else
1159         {
1160           /* See if we already have an entry with this name.  */
1161           for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT; slot < len; ++slot)
1162             if (!TREE_VEC_ELT (method_vec, slot)
1163                 || (DECL_NAME (OVL_CURRENT (TREE_VEC_ELT (method_vec, 
1164                                                           slot))) 
1165                     == DECL_NAME (method)))
1166               break;
1167                 
1168           if (slot == len)
1169             {
1170               /* We need a bigger method vector.  */
1171               tree new_vec = make_tree_vec (2 * len);
1172               bcopy ((PTR) &TREE_VEC_ELT (method_vec, 0),
1173                      (PTR) &TREE_VEC_ELT (new_vec, 0),
1174                      len * sizeof (tree));
1175               len = 2 * len;
1176               method_vec = CLASSTYPE_METHOD_VEC (type) = new_vec;
1177             }
1178
1179           if (DECL_CONV_FN_P (method) && !TREE_VEC_ELT (method_vec, slot))
1180             {
1181               /* Type conversion operators have to come before
1182                  ordinary methods; add_conversions depends on this to
1183                  speed up looking for conversion operators.  So, if
1184                  necessary, we slide some of the vector elements up.
1185                  In theory, this makes this algorithm O(N^2) but we
1186                  don't expect many conversion operators.  */
1187               for (slot = 2; slot < len; ++slot)
1188                 {
1189                   tree fn = TREE_VEC_ELT (method_vec, slot);
1190   
1191                   if (!fn)
1192                     /* There are no more entries in the vector, so we
1193                        can insert the new conversion operator here.  */
1194                     break;
1195                   
1196                   if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
1197                     /* We can insert the new function right at the
1198                        SLOTth position.  */
1199                     break;
1200                 }
1201   
1202               if (!TREE_VEC_ELT (method_vec, slot))
1203                 /* There is nothing in the Ith slot, so we can avoid
1204                    moving anything.  */
1205                 ; 
1206               else
1207                 {
1208                   /* We know the last slot in the vector is empty
1209                      because we know that at this point there's room
1210                      for a new function.  */
1211                   bcopy ((PTR) &TREE_VEC_ELT (method_vec, slot),
1212                          (PTR) &TREE_VEC_ELT (method_vec, slot + 1),
1213                          (len - slot - 1) * sizeof (tree));
1214                   TREE_VEC_ELT (method_vec, slot) = NULL_TREE;
1215                 }
1216             }
1217         }
1218       
1219       if (template_class_depth (type))
1220         /* TYPE is a template class.  Don't issue any errors now; wait
1221            until instantiation time to complain.  */
1222           ;
1223       else
1224         {
1225           tree fns;
1226
1227           /* Check to see if we've already got this method.  */
1228           for (fns = TREE_VEC_ELT (method_vec, slot);
1229                fns;
1230                fns = OVL_NEXT (fns))
1231             {
1232               tree fn = OVL_CURRENT (fns);
1233                  
1234               if (TREE_CODE (fn) != TREE_CODE (method))
1235                 continue;
1236
1237               if (TREE_CODE (method) != TEMPLATE_DECL)
1238                 {
1239                   /* [over.load] Member function declarations with the
1240                      same name and the same parameter types cannot be
1241                      overloaded if any of them is a static member
1242                      function declaration.  */
1243                   if ((DECL_STATIC_FUNCTION_P (fn)
1244                        != DECL_STATIC_FUNCTION_P (method))
1245                       || using)
1246                     {
1247                       tree parms1 = TYPE_ARG_TYPES (TREE_TYPE (fn));
1248                       tree parms2 = TYPE_ARG_TYPES (TREE_TYPE (method));
1249
1250                       if (! DECL_STATIC_FUNCTION_P (fn))
1251                         parms1 = TREE_CHAIN (parms1);
1252                       if (! DECL_STATIC_FUNCTION_P (method))
1253                         parms2 = TREE_CHAIN (parms2);
1254
1255                       if (compparms (parms1, parms2))
1256                         {
1257                           if (using)
1258                             /* Defer to the local function.  */
1259                             return;
1260                           else
1261                             cp_error ("`%#D' and `%#D' cannot be overloaded",
1262                                       fn, method);
1263                         }
1264                     }
1265
1266                   /* Since this is an ordinary function in a
1267                      non-template class, it's mangled name can be used
1268                      as a unique identifier.  This technique is only
1269                      an optimization; we would get the same results if
1270                      we just used decls_match here.  */
1271                   if (DECL_ASSEMBLER_NAME (fn) 
1272                       != DECL_ASSEMBLER_NAME (method))
1273                     continue;
1274                 }
1275               else if (!decls_match (fn, method))
1276                 continue;
1277
1278               /* There has already been a declaration of this method
1279                  or member template.  */
1280               cp_error_at ("`%D' has already been declared in `%T'", 
1281                            method, type);
1282
1283               /* We don't call duplicate_decls here to merge the
1284                  declarations because that will confuse things if the
1285                  methods have inline definitions.  In particular, we
1286                  will crash while processing the definitions.  */
1287               return;
1288             }
1289         }
1290
1291       /* Actually insert the new method.  */
1292       TREE_VEC_ELT (method_vec, slot) 
1293         = build_overload (method, TREE_VEC_ELT (method_vec, slot));
1294
1295       /* Add the new binding.  */ 
1296       if (!DECL_CONSTRUCTOR_P (method)
1297           && !DECL_DESTRUCTOR_P (method))
1298         push_class_level_binding (DECL_NAME (method),
1299                                   TREE_VEC_ELT (method_vec, slot));
1300     }
1301 }
1302
1303 /* Subroutines of finish_struct.  */
1304
1305 /* Look through the list of fields for this struct, deleting
1306    duplicates as we go.  This must be recursive to handle
1307    anonymous unions.
1308
1309    FIELD is the field which may not appear anywhere in FIELDS.
1310    FIELD_PTR, if non-null, is the starting point at which
1311    chained deletions may take place.
1312    The value returned is the first acceptable entry found
1313    in FIELDS.
1314
1315    Note that anonymous fields which are not of UNION_TYPE are
1316    not duplicates, they are just anonymous fields.  This happens
1317    when we have unnamed bitfields, for example.  */
1318
1319 static tree
1320 delete_duplicate_fields_1 (field, fields)
1321      tree field, fields;
1322 {
1323   tree x;
1324   tree prev = 0;
1325   if (DECL_NAME (field) == 0)
1326     {
1327       if (! ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1328         return fields;
1329
1330       for (x = TYPE_FIELDS (TREE_TYPE (field)); x; x = TREE_CHAIN (x))
1331         fields = delete_duplicate_fields_1 (x, fields);
1332       return fields;
1333     }
1334   else
1335     {
1336       for (x = fields; x; prev = x, x = TREE_CHAIN (x))
1337         {
1338           if (DECL_NAME (x) == 0)
1339             {
1340               if (! ANON_AGGR_TYPE_P (TREE_TYPE (x)))
1341                 continue;
1342               TYPE_FIELDS (TREE_TYPE (x))
1343                 = delete_duplicate_fields_1 (field, TYPE_FIELDS (TREE_TYPE (x)));
1344               if (TYPE_FIELDS (TREE_TYPE (x)) == 0)
1345                 {
1346                   if (prev == 0)
1347                     fields = TREE_CHAIN (fields);
1348                   else
1349                     TREE_CHAIN (prev) = TREE_CHAIN (x);
1350                 }
1351             }
1352           else if (TREE_CODE (field) == USING_DECL)
1353             /* A using declaration may is allowed to appear more than
1354                once.  We'll prune these from the field list later, and
1355                handle_using_decl will complain about invalid multiple
1356                uses.  */
1357             ;
1358           else if (DECL_NAME (field) == DECL_NAME (x))
1359             {
1360               if (TREE_CODE (field) == CONST_DECL
1361                   && TREE_CODE (x) == CONST_DECL)
1362                 cp_error_at ("duplicate enum value `%D'", x);
1363               else if (TREE_CODE (field) == CONST_DECL
1364                        || TREE_CODE (x) == CONST_DECL)
1365                 cp_error_at ("duplicate field `%D' (as enum and non-enum)",
1366                              x);
1367               else if (DECL_DECLARES_TYPE_P (field)
1368                        && DECL_DECLARES_TYPE_P (x))
1369                 {
1370                   if (same_type_p (TREE_TYPE (field), TREE_TYPE (x)))
1371                     continue;
1372                   cp_error_at ("duplicate nested type `%D'", x);
1373                 }
1374               else if (DECL_DECLARES_TYPE_P (field)
1375                        || DECL_DECLARES_TYPE_P (x))
1376                 {
1377                   /* Hide tag decls.  */
1378                   if ((TREE_CODE (field) == TYPE_DECL
1379                        && DECL_ARTIFICIAL (field))
1380                       || (TREE_CODE (x) == TYPE_DECL
1381                           && DECL_ARTIFICIAL (x)))
1382                     continue;
1383                   cp_error_at ("duplicate field `%D' (as type and non-type)",
1384                                x);
1385                 }
1386               else
1387                 cp_error_at ("duplicate member `%D'", x);
1388               if (prev == 0)
1389                 fields = TREE_CHAIN (fields);
1390               else
1391                 TREE_CHAIN (prev) = TREE_CHAIN (x);
1392             }
1393         }
1394     }
1395   return fields;
1396 }
1397
1398 static void
1399 delete_duplicate_fields (fields)
1400      tree fields;
1401 {
1402   tree x;
1403   for (x = fields; x && TREE_CHAIN (x); x = TREE_CHAIN (x))
1404     TREE_CHAIN (x) = delete_duplicate_fields_1 (x, TREE_CHAIN (x));
1405 }
1406
1407 /* Change the access of FDECL to ACCESS in T.  Return 1 if change was
1408    legit, otherwise return 0.  */
1409
1410 static int
1411 alter_access (t, fdecl, access)
1412      tree t;
1413      tree fdecl;
1414      tree access;
1415 {
1416   tree elem = purpose_member (t, DECL_ACCESS (fdecl));
1417   if (elem)
1418     {
1419       if (TREE_VALUE (elem) != access)
1420         {
1421           if (TREE_CODE (TREE_TYPE (fdecl)) == FUNCTION_DECL)
1422             cp_error_at ("conflicting access specifications for method `%D', ignored", TREE_TYPE (fdecl));
1423           else
1424             error ("conflicting access specifications for field `%s', ignored",
1425                    IDENTIFIER_POINTER (DECL_NAME (fdecl)));
1426         }
1427       else
1428         {
1429           /* They're changing the access to the same thing they changed
1430              it to before.  That's OK.  */
1431           ;
1432         }
1433     }
1434   else
1435     {
1436       enforce_access (t, fdecl);
1437       DECL_ACCESS (fdecl) = tree_cons (t, access, DECL_ACCESS (fdecl));
1438       return 1;
1439     }
1440   return 0;
1441 }
1442
1443 /* Process the USING_DECL, which is a member of T.  */
1444
1445 static void
1446 handle_using_decl (using_decl, t)
1447      tree using_decl;
1448      tree t;
1449 {
1450   tree ctype = DECL_INITIAL (using_decl);
1451   tree name = DECL_NAME (using_decl);
1452   tree access
1453     = TREE_PRIVATE (using_decl) ? access_private_node
1454     : TREE_PROTECTED (using_decl) ? access_protected_node
1455     : access_public_node;
1456   tree fdecl, binfo;
1457   tree flist = NULL_TREE;
1458   tree old_value;
1459
1460   binfo = binfo_or_else (ctype, t);
1461   if (! binfo)
1462     return;
1463   
1464   if (name == constructor_name (ctype)
1465       || name == constructor_name_full (ctype))
1466     {
1467       cp_error_at ("using-declaration for constructor", using_decl);
1468       return;
1469     }
1470
1471   fdecl = lookup_member (binfo, name, 0, 0);
1472   
1473   if (!fdecl)
1474     {
1475       cp_error_at ("no members matching `%D' in `%#T'", using_decl, ctype);
1476       return;
1477     }
1478
1479   if (BASELINK_P (fdecl))
1480     /* Ignore base type this came from. */
1481     fdecl = TREE_VALUE (fdecl);
1482
1483   old_value = IDENTIFIER_CLASS_VALUE (name);
1484   if (old_value)
1485     {
1486       if (is_overloaded_fn (old_value))
1487         old_value = OVL_CURRENT (old_value);
1488
1489       if (DECL_P (old_value) && DECL_CONTEXT (old_value) == t)
1490         /* OK */;
1491       else
1492         old_value = NULL_TREE;
1493     }
1494
1495   if (is_overloaded_fn (fdecl))
1496     flist = fdecl;
1497   else if (! DECL_LANG_SPECIFIC (fdecl))
1498     my_friendly_abort (20000221);
1499
1500   if (! old_value)
1501     ;
1502   else if (is_overloaded_fn (old_value))
1503     {
1504       if (flist)
1505         /* It's OK to use functions from a base when there are functions with
1506            the same name already present in the current class.  */;
1507       else
1508         {
1509           cp_error ("`%D' invalid in `%#T'", using_decl, t);
1510           cp_error_at ("  because of local method `%#D' with same name",
1511                        OVL_CURRENT (old_value));
1512           return;
1513         }
1514     }
1515   else
1516     {
1517       cp_error ("`%D' invalid in `%#T'", using_decl, t);
1518       cp_error_at ("  because of local field `%#D' with same name", old_value);
1519       return;
1520     }
1521   
1522   /* Make type T see field decl FDECL with access ACCESS.*/
1523   if (flist)
1524     for (; flist; flist = OVL_NEXT (flist))
1525       {
1526         add_method (t, 0, OVL_CURRENT (flist));
1527         alter_access (t, OVL_CURRENT (flist), access);
1528       }
1529   else
1530     alter_access (t, fdecl, access);
1531 }
1532 \f
1533 /* Run through the base clases of T, updating
1534    CANT_HAVE_DEFAULT_CTOR_P, CANT_HAVE_CONST_CTOR_P, and
1535    NO_CONST_ASN_REF_P.  Also set flag bits in T based on properties of
1536    the bases.  */
1537
1538 static void
1539 check_bases (t, cant_have_default_ctor_p, cant_have_const_ctor_p,
1540              no_const_asn_ref_p)
1541      tree t;
1542      int *cant_have_default_ctor_p;
1543      int *cant_have_const_ctor_p;
1544      int *no_const_asn_ref_p;
1545 {
1546   int n_baseclasses;
1547   int i;
1548   int seen_nearly_empty_base_p;
1549   tree binfos;
1550
1551   binfos = TYPE_BINFO_BASETYPES (t);
1552   n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
1553   seen_nearly_empty_base_p = 0;
1554
1555   /* An aggregate cannot have baseclasses.  */
1556   CLASSTYPE_NON_AGGREGATE (t) |= (n_baseclasses != 0);
1557
1558   for (i = 0; i < n_baseclasses; ++i) 
1559     {
1560       tree base_binfo;
1561       tree basetype;
1562
1563       /* Figure out what base we're looking at.  */
1564       base_binfo = TREE_VEC_ELT (binfos, i);
1565       basetype = TREE_TYPE (base_binfo);
1566
1567       /* If the type of basetype is incomplete, then we already
1568          complained about that fact (and we should have fixed it up as
1569          well).  */
1570       if (!COMPLETE_TYPE_P (basetype))
1571         {
1572           int j;
1573           /* The base type is of incomplete type.  It is
1574              probably best to pretend that it does not
1575              exist.  */
1576           if (i == n_baseclasses-1)
1577             TREE_VEC_ELT (binfos, i) = NULL_TREE;
1578           TREE_VEC_LENGTH (binfos) -= 1;
1579           n_baseclasses -= 1;
1580           for (j = i; j+1 < n_baseclasses; j++)
1581             TREE_VEC_ELT (binfos, j) = TREE_VEC_ELT (binfos, j+1);
1582           continue;
1583         }
1584
1585       /* Effective C++ rule 14.  We only need to check TYPE_POLYMORPHIC_P
1586          here because the case of virtual functions but non-virtual
1587          dtor is handled in finish_struct_1.  */
1588       if (warn_ecpp && ! TYPE_POLYMORPHIC_P (basetype)
1589           && TYPE_HAS_DESTRUCTOR (basetype))
1590         cp_warning ("base class `%#T' has a non-virtual destructor",
1591                     basetype);
1592
1593       /* If the base class doesn't have copy constructors or
1594          assignment operators that take const references, then the
1595          derived class cannot have such a member automatically
1596          generated.  */
1597       if (! TYPE_HAS_CONST_INIT_REF (basetype))
1598         *cant_have_const_ctor_p = 1;
1599       if (TYPE_HAS_ASSIGN_REF (basetype)
1600           && !TYPE_HAS_CONST_ASSIGN_REF (basetype))
1601         *no_const_asn_ref_p = 1;
1602       /* Similarly, if the base class doesn't have a default
1603          constructor, then the derived class won't have an
1604          automatically generated default constructor.  */
1605       if (TYPE_HAS_CONSTRUCTOR (basetype)
1606           && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype))
1607         {
1608           *cant_have_default_ctor_p = 1;
1609           if (! TYPE_HAS_CONSTRUCTOR (t))
1610             cp_pedwarn ("base `%T' with only non-default constructor in class without a constructor",
1611                         basetype);
1612         }
1613
1614       /* If the base class is not empty or nearly empty, then this
1615          class cannot be nearly empty.  */
1616       if (!CLASSTYPE_NEARLY_EMPTY_P (basetype) && !is_empty_class (basetype))
1617         CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1618       /* And if there is more than one nearly empty base, then the
1619          derived class is not nearly empty either.  */
1620       else if (CLASSTYPE_NEARLY_EMPTY_P (basetype) 
1621                && seen_nearly_empty_base_p)
1622         CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1623       /* If this is the first nearly empty base class, then remember
1624          that we saw it.  */
1625       else if (CLASSTYPE_NEARLY_EMPTY_P (basetype))
1626         seen_nearly_empty_base_p = 1;
1627
1628       /* A lot of properties from the bases also apply to the derived
1629          class.  */
1630       TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (basetype);
1631       TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) 
1632         |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype);
1633       TYPE_HAS_COMPLEX_ASSIGN_REF (t) 
1634         |= TYPE_HAS_COMPLEX_ASSIGN_REF (basetype);
1635       TYPE_HAS_COMPLEX_INIT_REF (t) |= TYPE_HAS_COMPLEX_INIT_REF (basetype);
1636       TYPE_OVERLOADS_CALL_EXPR (t) |= TYPE_OVERLOADS_CALL_EXPR (basetype);
1637       TYPE_OVERLOADS_ARRAY_REF (t) |= TYPE_OVERLOADS_ARRAY_REF (basetype);
1638       TYPE_OVERLOADS_ARROW (t) |= TYPE_OVERLOADS_ARROW (basetype);
1639       TYPE_POLYMORPHIC_P (t) |= TYPE_POLYMORPHIC_P (basetype);
1640
1641       /* Derived classes can implicitly become COMified if their bases
1642          are COM.  */
1643       if (CLASSTYPE_COM_INTERFACE (basetype))
1644         CLASSTYPE_COM_INTERFACE (t) = 1;
1645       else if (i == 0 && CLASSTYPE_COM_INTERFACE (t))
1646         {
1647           cp_error 
1648             ("COM interface type `%T' with non-COM leftmost base class `%T'",
1649              t, basetype);
1650           CLASSTYPE_COM_INTERFACE (t) = 0;
1651         }
1652     }
1653 }
1654
1655 /* Called via dfs_walk from mark_primary_bases.  Sets
1656    BINFO_PRIMARY_MARKED_P for BINFO, if appropriate.  */
1657
1658 static tree
1659 dfs_mark_primary_bases (binfo, data)
1660      tree binfo;
1661      void *data;
1662 {
1663   int i;
1664   tree base_binfo;
1665
1666   if (!CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (binfo)))
1667     return NULL_TREE;
1668
1669   i = CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo));
1670   base_binfo = BINFO_BASETYPE (binfo, i);
1671
1672   if (!TREE_VIA_VIRTUAL (base_binfo))
1673     /* Non-virtual base classes are easy.  */
1674     BINFO_PRIMARY_MARKED_P (base_binfo) = 1;
1675   else
1676     {
1677       tree shared_binfo;
1678
1679       shared_binfo 
1680         = BINFO_FOR_VBASE (BINFO_TYPE (base_binfo), (tree) data);
1681
1682       /* If this virtual base is not already primary somewhere else in
1683          the hiearchy, then we'll be using this copy.  */
1684       if (!BINFO_VBASE_PRIMARY_P (shared_binfo))
1685         {
1686           BINFO_VBASE_PRIMARY_P (shared_binfo) = 1;
1687           BINFO_PRIMARY_MARKED_P (base_binfo) = 1;
1688         }
1689     }
1690
1691   return NULL_TREE;
1692 }
1693
1694 /* Set BINFO_PRIMARY_MARKED_P for all binfos in the hierarchy
1695    dominated by BINFO that are primary bases.  */
1696
1697 static void
1698 mark_primary_bases (type)
1699      tree type;
1700 {
1701   tree vbases;
1702
1703   /* Mark the TYPE_BINFO hierarchy.  We need to mark primary bases in
1704      pre-order to deal with primary virtual bases.  (The virtual base
1705      would be skipped if it were not marked as primary, and that
1706      requires getting to dfs_mark_primary_bases before
1707      dfs_skip_nonprimary_vbases_unmarkedp has a chance to skip the
1708      virtual base.)  */
1709   dfs_walk_real (TYPE_BINFO (type), dfs_mark_primary_bases, NULL,
1710                  dfs_skip_nonprimary_vbases_unmarkedp, type);
1711
1712   /* Now go through the virtual base classes in inheritance graph
1713      order.  Any that are not already primary will need to be
1714      allocated in TYPE, and so we need to mark their primary bases.  */
1715   for (vbases = TYPE_BINFO (type); vbases; vbases = TREE_CHAIN (vbases))
1716     {
1717       tree vbase;
1718
1719       /* Make sure that only BINFOs appear on this list.
1720          Historically, the TREE_CHAIN was used for other purposes, and
1721          we want to make sure that none of those uses remain.  */
1722       my_friendly_assert (TREE_CODE (vbases) == TREE_VEC, 20000402);
1723
1724       if (!TREE_VIA_VIRTUAL (vbases))
1725         continue;
1726
1727       vbase = BINFO_FOR_VBASE (BINFO_TYPE (vbases), type);
1728       if (BINFO_VBASE_PRIMARY_P (vbase))
1729         /* This virtual base was already included in the hierarchy, so
1730            there's nothing to do here.  */
1731         continue;
1732
1733       /* Temporarily pretend that VBASE is primary so that its bases
1734          will be walked; this is the real copy of VBASE.  */
1735       BINFO_PRIMARY_MARKED_P (vbase) = 1;
1736
1737       /* Now, walk its bases.  */
1738       dfs_walk_real (vbase, dfs_mark_primary_bases, NULL,
1739                      dfs_skip_nonprimary_vbases_unmarkedp, type);
1740
1741       /* VBASE wasn't really primary.  */
1742       BINFO_PRIMARY_MARKED_P (vbase) = 0;
1743     }
1744 }
1745
1746 /* Make the Ith baseclass of T its primary base.  */
1747
1748 static void
1749 set_primary_base (t, i, vfuns_p)
1750      tree t;
1751      int i;
1752      int *vfuns_p;
1753 {
1754   tree basetype;
1755
1756   CLASSTYPE_VFIELD_PARENT (t) = i;
1757   basetype = BINFO_TYPE (CLASSTYPE_PRIMARY_BINFO (t));
1758   TYPE_BINFO_VTABLE (t) = TYPE_BINFO_VTABLE (basetype);
1759   TYPE_BINFO_VIRTUALS (t) = TYPE_BINFO_VIRTUALS (basetype);
1760   TYPE_VFIELD (t) = TYPE_VFIELD (basetype);
1761   CLASSTYPE_RTTI (t) = CLASSTYPE_RTTI (basetype);
1762   *vfuns_p = CLASSTYPE_VSIZE (basetype);
1763 }
1764
1765 /* Determine the primary class for T.  */
1766
1767 static void
1768 determine_primary_base (t, vfuns_p)
1769      tree t;
1770      int *vfuns_p;
1771 {
1772   int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
1773
1774   /* If there are no baseclasses, there is certainly no primary base.  */
1775   if (n_baseclasses == 0)
1776     return;
1777
1778   *vfuns_p = 0;
1779
1780   for (i = 0; i < n_baseclasses; i++)
1781     {
1782       tree base_binfo = TREE_VEC_ELT (TYPE_BINFO_BASETYPES (t), i);
1783       tree basetype = BINFO_TYPE (base_binfo);
1784
1785       if (TYPE_CONTAINS_VPTR_P (basetype))
1786         {
1787           /* Even a virtual baseclass can contain our RTTI
1788              information.  But, we prefer a non-virtual polymorphic
1789              baseclass.  */
1790           if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
1791             CLASSTYPE_RTTI (t) = CLASSTYPE_RTTI (basetype);
1792
1793           /* A virtual baseclass can't be the primary base under the
1794              old ABI.  And under the new ABI we still prefer a
1795              non-virtual base.  */
1796           if (TREE_VIA_VIRTUAL (base_binfo))
1797             continue;
1798
1799           if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
1800             {
1801               set_primary_base (t, i, vfuns_p);
1802               CLASSTYPE_VFIELDS (t) = copy_list (CLASSTYPE_VFIELDS (basetype));
1803             }
1804           else
1805             {
1806               tree vfields;
1807
1808               /* Only add unique vfields, and flatten them out as we go.  */
1809               for (vfields = CLASSTYPE_VFIELDS (basetype);
1810                    vfields;
1811                    vfields = TREE_CHAIN (vfields))
1812                 if (VF_BINFO_VALUE (vfields) == NULL_TREE
1813                     || ! TREE_VIA_VIRTUAL (VF_BINFO_VALUE (vfields)))
1814                   CLASSTYPE_VFIELDS (t) 
1815                     = tree_cons (base_binfo, 
1816                                  VF_BASETYPE_VALUE (vfields),
1817                                  CLASSTYPE_VFIELDS (t));
1818
1819               if (!flag_new_abi && *vfuns_p == 0)
1820                 set_primary_base (t, i, vfuns_p);
1821             }
1822         }
1823     }
1824
1825   if (!TYPE_VFIELD (t))
1826     CLASSTYPE_VFIELD_PARENT (t) = -1;
1827
1828   /* The new ABI allows for the use of a "nearly-empty" virtual base
1829      class as the primary base class if no non-virtual polymorphic
1830      base can be found.  */
1831   if (flag_new_abi && !CLASSTYPE_HAS_PRIMARY_BASE_P (t))
1832     for (i = 0; i < n_baseclasses; ++i)
1833       {
1834         tree base_binfo = TREE_VEC_ELT (TYPE_BINFO_BASETYPES (t), i);
1835         tree basetype = BINFO_TYPE (base_binfo);
1836
1837         if (TREE_VIA_VIRTUAL (base_binfo) 
1838             && CLASSTYPE_NEARLY_EMPTY_P (basetype))
1839           {
1840             set_primary_base (t, i, vfuns_p);
1841             CLASSTYPE_VFIELDS (t) = copy_list (CLASSTYPE_VFIELDS (basetype));
1842             break;
1843           }
1844       }
1845
1846   /* Mark the primary base classes at this point.  */
1847   mark_primary_bases (t);
1848 }
1849 \f
1850 /* Set memoizing fields and bits of T (and its variants) for later
1851    use.  */
1852
1853 static void
1854 finish_struct_bits (t)
1855      tree t;
1856 {
1857   int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
1858
1859   /* Fix up variants (if any).  */
1860   tree variants = TYPE_NEXT_VARIANT (t);
1861   while (variants)
1862     {
1863       /* These fields are in the _TYPE part of the node, not in
1864          the TYPE_LANG_SPECIFIC component, so they are not shared.  */
1865       TYPE_HAS_CONSTRUCTOR (variants) = TYPE_HAS_CONSTRUCTOR (t);
1866       TYPE_HAS_DESTRUCTOR (variants) = TYPE_HAS_DESTRUCTOR (t);
1867       TYPE_NEEDS_CONSTRUCTING (variants) = TYPE_NEEDS_CONSTRUCTING (t);
1868       TYPE_HAS_NONTRIVIAL_DESTRUCTOR (variants) 
1869         = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
1870
1871       TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (variants) 
1872         = TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (t);
1873       TYPE_POLYMORPHIC_P (variants) = TYPE_POLYMORPHIC_P (t);
1874       TYPE_USES_VIRTUAL_BASECLASSES (variants) = TYPE_USES_VIRTUAL_BASECLASSES (t);
1875       /* Copy whatever these are holding today.  */
1876       TYPE_MIN_VALUE (variants) = TYPE_MIN_VALUE (t);
1877       TYPE_MAX_VALUE (variants) = TYPE_MAX_VALUE (t);
1878       TYPE_FIELDS (variants) = TYPE_FIELDS (t);
1879       TYPE_SIZE (variants) = TYPE_SIZE (t);
1880       TYPE_SIZE_UNIT (variants) = TYPE_SIZE_UNIT (t);
1881       variants = TYPE_NEXT_VARIANT (variants);
1882     }
1883
1884   if (n_baseclasses && TYPE_POLYMORPHIC_P (t))
1885     /* For a class w/o baseclasses, `finish_struct' has set
1886        CLASS_TYPE_ABSTRACT_VIRTUALS correctly (by
1887        definition). Similarly for a class whose base classes do not
1888        have vtables. When neither of these is true, we might have
1889        removed abstract virtuals (by providing a definition), added
1890        some (by declaring new ones), or redeclared ones from a base
1891        class. We need to recalculate what's really an abstract virtual
1892        at this point (by looking in the vtables).  */
1893       get_pure_virtuals (t);
1894
1895   if (n_baseclasses)
1896     {
1897       /* Notice whether this class has type conversion functions defined.  */
1898       tree binfo = TYPE_BINFO (t);
1899       tree binfos = BINFO_BASETYPES (binfo);
1900       tree basetype;
1901
1902       for (i = n_baseclasses-1; i >= 0; i--)
1903         {
1904           basetype = BINFO_TYPE (TREE_VEC_ELT (binfos, i));
1905
1906           TYPE_HAS_CONVERSION (t) |= TYPE_HAS_CONVERSION (basetype);
1907         }
1908     }
1909
1910   /* If this type has a copy constructor, force its mode to be BLKmode, and
1911      force its TREE_ADDRESSABLE bit to be nonzero.  This will cause it to
1912      be passed by invisible reference and prevent it from being returned in
1913      a register.
1914
1915      Also do this if the class has BLKmode but can still be returned in
1916      registers, since function_cannot_inline_p won't let us inline
1917      functions returning such a type.  This affects the HP-PA.  */
1918   if (! TYPE_HAS_TRIVIAL_INIT_REF (t)
1919       || (TYPE_MODE (t) == BLKmode && ! aggregate_value_p (t)
1920           && CLASSTYPE_NON_AGGREGATE (t)))
1921     {
1922       tree variants;
1923       DECL_MODE (TYPE_MAIN_DECL (t)) = BLKmode;
1924       for (variants = t; variants; variants = TYPE_NEXT_VARIANT (variants))
1925         {
1926           TYPE_MODE (variants) = BLKmode;
1927           TREE_ADDRESSABLE (variants) = 1;
1928         }
1929     }
1930 }
1931
1932 /* Issue warnings about T having private constructors, but no friends,
1933    and so forth.  
1934
1935    HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or
1936    static members.  HAS_NONPRIVATE_STATIC_FN is nonzero if T has any
1937    non-private static member functions.  */
1938
1939 static void
1940 maybe_warn_about_overly_private_class (t)
1941      tree t;
1942 {
1943   int has_member_fn = 0;
1944   int has_nonprivate_method = 0;
1945   tree fn;
1946
1947   if (!warn_ctor_dtor_privacy
1948       /* If the class has friends, those entities might create and
1949          access instances, so we should not warn.  */
1950       || (CLASSTYPE_FRIEND_CLASSES (t)
1951           || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
1952       /* We will have warned when the template was declared; there's
1953          no need to warn on every instantiation.  */
1954       || CLASSTYPE_TEMPLATE_INSTANTIATION (t))
1955     /* There's no reason to even consider warning about this 
1956        class.  */
1957     return;
1958     
1959   /* We only issue one warning, if more than one applies, because
1960      otherwise, on code like:
1961
1962      class A {
1963        // Oops - forgot `public:'
1964        A();
1965        A(const A&);
1966        ~A();
1967      };
1968
1969      we warn several times about essentially the same problem.  */
1970
1971   /* Check to see if all (non-constructor, non-destructor) member
1972      functions are private.  (Since there are no friends or
1973      non-private statics, we can't ever call any of the private member
1974      functions.)  */
1975   for (fn = TYPE_METHODS (t); fn; fn = TREE_CHAIN (fn))
1976     /* We're not interested in compiler-generated methods; they don't
1977        provide any way to call private members.  */
1978     if (!DECL_ARTIFICIAL (fn)) 
1979       {
1980         if (!TREE_PRIVATE (fn))
1981           {
1982             if (DECL_STATIC_FUNCTION_P (fn)) 
1983               /* A non-private static member function is just like a
1984                  friend; it can create and invoke private member
1985                  functions, and be accessed without a class
1986                  instance.  */
1987               return;
1988                 
1989             has_nonprivate_method = 1;
1990             break;
1991           }
1992         else if (!DECL_CONSTRUCTOR_P (fn) && !DECL_DESTRUCTOR_P (fn))
1993           has_member_fn = 1;
1994       } 
1995
1996   if (!has_nonprivate_method && has_member_fn) 
1997     {
1998       /* There are no non-private methods, and there's at least one
1999          private member function that isn't a constructor or
2000          destructor.  (If all the private members are
2001          constructors/destructors we want to use the code below that
2002          issues error messages specifically referring to
2003          constructors/destructors.)  */
2004       int i;
2005       tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
2006       for (i = 0; i < CLASSTYPE_N_BASECLASSES (t); i++)
2007         if (TREE_VIA_PUBLIC (TREE_VEC_ELT (binfos, i))
2008             || TREE_VIA_PROTECTED (TREE_VEC_ELT (binfos, i)))
2009           {
2010             has_nonprivate_method = 1;
2011             break;
2012           }
2013       if (!has_nonprivate_method) 
2014         {
2015           cp_warning ("all member functions in class `%T' are private", t);
2016           return;
2017         }
2018     }
2019
2020   /* Even if some of the member functions are non-private, the class
2021      won't be useful for much if all the constructors or destructors
2022      are private: such an object can never be created or destroyed.  */
2023   if (TYPE_HAS_DESTRUCTOR (t))
2024     {
2025       tree dtor = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 1);
2026
2027       if (TREE_PRIVATE (dtor))
2028         {
2029           cp_warning ("`%#T' only defines a private destructor and has no friends",
2030                       t);
2031           return;
2032         }
2033     }
2034
2035   if (TYPE_HAS_CONSTRUCTOR (t))
2036     {
2037       int nonprivate_ctor = 0;
2038           
2039       /* If a non-template class does not define a copy
2040          constructor, one is defined for it, enabling it to avoid
2041          this warning.  For a template class, this does not
2042          happen, and so we would normally get a warning on:
2043
2044            template <class T> class C { private: C(); };  
2045           
2046          To avoid this asymmetry, we check TYPE_HAS_INIT_REF.  All
2047          complete non-template or fully instantiated classes have this
2048          flag set.  */
2049       if (!TYPE_HAS_INIT_REF (t))
2050         nonprivate_ctor = 1;
2051       else 
2052         for (fn = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 0);
2053              fn;
2054              fn = OVL_NEXT (fn)) 
2055           {
2056             tree ctor = OVL_CURRENT (fn);
2057             /* Ideally, we wouldn't count copy constructors (or, in
2058                fact, any constructor that takes an argument of the
2059                class type as a parameter) because such things cannot
2060                be used to construct an instance of the class unless
2061                you already have one.  But, for now at least, we're
2062                more generous.  */
2063             if (! TREE_PRIVATE (ctor))
2064               {
2065                 nonprivate_ctor = 1;
2066                 break;
2067               }
2068           }
2069
2070       if (nonprivate_ctor == 0)
2071         {
2072           cp_warning ("`%#T' only defines private constructors and has no friends",
2073                       t);
2074           return;
2075         }
2076     }
2077 }
2078
2079 /* Function to help qsort sort FIELD_DECLs by name order.  */
2080
2081 static int
2082 field_decl_cmp (x, y)
2083      const tree *x, *y;
2084 {
2085   if (DECL_NAME (*x) == DECL_NAME (*y))
2086     /* A nontype is "greater" than a type.  */
2087     return DECL_DECLARES_TYPE_P (*y) - DECL_DECLARES_TYPE_P (*x);
2088   if (DECL_NAME (*x) == NULL_TREE)
2089     return -1;
2090   if (DECL_NAME (*y) == NULL_TREE)
2091     return 1;
2092   if (DECL_NAME (*x) < DECL_NAME (*y))
2093     return -1;
2094   return 1;
2095 }
2096
2097 /* Comparison function to compare two TYPE_METHOD_VEC entries by name.  */
2098
2099 static int
2100 method_name_cmp (m1, m2)
2101      const tree *m1, *m2;
2102 {
2103   if (*m1 == NULL_TREE && *m2 == NULL_TREE)
2104     return 0;
2105   if (*m1 == NULL_TREE)
2106     return -1;
2107   if (*m2 == NULL_TREE)
2108     return 1;
2109   if (DECL_NAME (OVL_CURRENT (*m1)) < DECL_NAME (OVL_CURRENT (*m2)))
2110     return -1;
2111   return 1;
2112 }
2113
2114 /* Warn about duplicate methods in fn_fields.  Also compact method
2115    lists so that lookup can be made faster.
2116
2117    Data Structure: List of method lists.  The outer list is a
2118    TREE_LIST, whose TREE_PURPOSE field is the field name and the
2119    TREE_VALUE is the DECL_CHAIN of the FUNCTION_DECLs.  TREE_CHAIN
2120    links the entire list of methods for TYPE_METHODS.  Friends are
2121    chained in the same way as member functions (? TREE_CHAIN or
2122    DECL_CHAIN), but they live in the TREE_TYPE field of the outer
2123    list.  That allows them to be quickly deleted, and requires no
2124    extra storage.
2125
2126    Sort methods that are not special (i.e., constructors, destructors,
2127    and type conversion operators) so that we can find them faster in
2128    search.  */
2129
2130 static void
2131 finish_struct_methods (t)
2132      tree t;
2133 {
2134   tree fn_fields;
2135   tree method_vec;
2136   int slot, len;
2137
2138   if (!TYPE_METHODS (t))
2139     {
2140       /* Clear these for safety; perhaps some parsing error could set
2141          these incorrectly.  */
2142       TYPE_HAS_CONSTRUCTOR (t) = 0;
2143       TYPE_HAS_DESTRUCTOR (t) = 0;
2144       CLASSTYPE_METHOD_VEC (t) = NULL_TREE;
2145       return;
2146     }
2147
2148   method_vec = CLASSTYPE_METHOD_VEC (t);
2149   my_friendly_assert (method_vec != NULL_TREE, 19991215);
2150   len = TREE_VEC_LENGTH (method_vec);
2151
2152   /* First fill in entry 0 with the constructors, entry 1 with destructors,
2153      and the next few with type conversion operators (if any).  */
2154   for (fn_fields = TYPE_METHODS (t); fn_fields; 
2155        fn_fields = TREE_CHAIN (fn_fields))
2156     /* Clear out this flag.  */
2157     DECL_IN_AGGR_P (fn_fields) = 0;
2158
2159   if (TYPE_HAS_DESTRUCTOR (t) && !CLASSTYPE_DESTRUCTORS (t))
2160     /* We thought there was a destructor, but there wasn't.  Some
2161        parse errors cause this anomalous situation.  */
2162     TYPE_HAS_DESTRUCTOR (t) = 0;
2163     
2164   /* Issue warnings about private constructors and such.  If there are
2165      no methods, then some public defaults are generated.  */
2166   maybe_warn_about_overly_private_class (t);
2167
2168   /* Now sort the methods.  */
2169   while (len > 2 && TREE_VEC_ELT (method_vec, len-1) == NULL_TREE)
2170     len--;
2171   TREE_VEC_LENGTH (method_vec) = len;
2172
2173   /* The type conversion ops have to live at the front of the vec, so we
2174      can't sort them.  */
2175   for (slot = 2; slot < len; ++slot)
2176     {
2177       tree fn = TREE_VEC_ELT (method_vec, slot);
2178   
2179       if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
2180         break;
2181     }
2182   if (len - slot > 1)
2183     qsort (&TREE_VEC_ELT (method_vec, slot), len-slot, sizeof (tree),
2184            (int (*)(const void *, const void *))method_name_cmp);
2185 }
2186
2187 /* Emit error when a duplicate definition of a type is seen.  Patch up.  */
2188
2189 void
2190 duplicate_tag_error (t)
2191      tree t;
2192 {
2193   cp_error ("redefinition of `%#T'", t);
2194   cp_error_at ("previous definition here", t);
2195
2196   /* Pretend we haven't defined this type.  */
2197
2198   /* All of the component_decl's were TREE_CHAINed together in the parser.
2199      finish_struct_methods walks these chains and assembles all methods with
2200      the same base name into DECL_CHAINs. Now we don't need the parser chains
2201      anymore, so we unravel them.  */
2202
2203   /* This used to be in finish_struct, but it turns out that the
2204      TREE_CHAIN is used by dbxout_type_methods and perhaps some other
2205      things...  */
2206   if (CLASSTYPE_METHOD_VEC (t)) 
2207     {
2208       tree method_vec = CLASSTYPE_METHOD_VEC (t);
2209       int i, len  = TREE_VEC_LENGTH (method_vec);
2210       for (i = 0; i < len; i++)
2211         {
2212           tree unchain = TREE_VEC_ELT (method_vec, i);
2213           while (unchain != NULL_TREE) 
2214             {
2215               TREE_CHAIN (OVL_CURRENT (unchain)) = NULL_TREE;
2216               unchain = OVL_NEXT (unchain);
2217             }
2218         }
2219     }
2220
2221   if (TYPE_LANG_SPECIFIC (t))
2222     {
2223       tree binfo = TYPE_BINFO (t);
2224       int interface_only = CLASSTYPE_INTERFACE_ONLY (t);
2225       int interface_unknown = CLASSTYPE_INTERFACE_UNKNOWN (t);
2226       tree template_info = CLASSTYPE_TEMPLATE_INFO (t);
2227       int use_template = CLASSTYPE_USE_TEMPLATE (t);
2228
2229       bzero ((char *) TYPE_LANG_SPECIFIC (t), sizeof (struct lang_type));
2230       BINFO_BASETYPES(binfo) = NULL_TREE;
2231
2232       TYPE_BINFO (t) = binfo;
2233       CLASSTYPE_INTERFACE_ONLY (t) = interface_only;
2234       SET_CLASSTYPE_INTERFACE_UNKNOWN_X (t, interface_unknown);
2235       TYPE_REDEFINED (t) = 1;
2236       CLASSTYPE_TEMPLATE_INFO (t) = template_info;
2237       CLASSTYPE_USE_TEMPLATE (t) = use_template;
2238     }
2239   TYPE_SIZE (t) = NULL_TREE;
2240   TYPE_MODE (t) = VOIDmode;
2241   TYPE_FIELDS (t) = NULL_TREE;
2242   TYPE_METHODS (t) = NULL_TREE;
2243   TYPE_VFIELD (t) = NULL_TREE;
2244   TYPE_CONTEXT (t) = NULL_TREE;
2245   TYPE_NONCOPIED_PARTS (t) = NULL_TREE;
2246 }
2247
2248 /* Make the BINFO's vtablehave N entries, including RTTI entries,
2249    vbase and vcall offsets, etc.  Set its type and call the backend
2250    to lay it out.  */
2251
2252 static void
2253 layout_vtable_decl (binfo, n)
2254      tree binfo;
2255      int n;
2256 {
2257   tree itype;
2258   tree atype;
2259   tree vtable;
2260
2261   itype = size_int (n);
2262   atype = build_cplus_array_type (vtable_entry_type, 
2263                                   build_index_type (itype));
2264   layout_type (atype);
2265
2266   /* We may have to grow the vtable.  */
2267   vtable = get_vtbl_decl_for_binfo (binfo);
2268   if (!same_type_p (TREE_TYPE (vtable), atype))
2269     {
2270       TREE_TYPE (vtable) = atype;
2271       DECL_SIZE (vtable) = DECL_SIZE_UNIT (vtable) = NULL_TREE;
2272       layout_decl (vtable, 0);
2273
2274       /* At one time the vtable info was grabbed 2 words at a time.  This
2275          fails on Sparc unless you have 8-byte alignment.  */
2276       DECL_ALIGN (vtable) = MAX (TYPE_ALIGN (double_type_node),
2277                                  DECL_ALIGN (vtable));
2278     }
2279 }
2280
2281 /* True if we should override the given BASE_FNDECL with the given
2282    FNDECL.  */
2283
2284 static int
2285 overrides (fndecl, base_fndecl)
2286      tree fndecl, base_fndecl;
2287 {
2288   /* One destructor overrides another if they are the same kind of
2289      destructor.  */
2290   if (DECL_DESTRUCTOR_P (base_fndecl) && DECL_DESTRUCTOR_P (fndecl)
2291       && special_function_p (base_fndecl) == special_function_p (fndecl))
2292     return 1;
2293   /* But a non-destructor never overrides a destructor, nor vice
2294      versa, nor do different kinds of destructors override
2295      one-another.  For example, a complete object destructor does not
2296      override a deleting destructor.  */
2297   if (DECL_DESTRUCTOR_P (base_fndecl) || DECL_DESTRUCTOR_P (fndecl))
2298     return 0;
2299
2300   if (DECL_NAME (fndecl) == DECL_NAME (base_fndecl))
2301     {
2302       tree types, base_types;
2303       types = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
2304       base_types = TYPE_ARG_TYPES (TREE_TYPE (base_fndecl));
2305       if ((TYPE_QUALS (TREE_TYPE (TREE_VALUE (base_types)))
2306            == TYPE_QUALS (TREE_TYPE (TREE_VALUE (types))))
2307           && compparms (TREE_CHAIN (base_types), TREE_CHAIN (types)))
2308         return 1;
2309     }
2310   return 0;
2311 }
2312
2313 typedef struct find_final_overrider_data_s {
2314   /* The function for which we are trying to find a final overrider.  */
2315   tree fn;
2316   /* The base class in which the function was declared.  */
2317   tree declaring_base;
2318   /* The most derived class in the hierarchy.  */
2319   tree most_derived_type;
2320   /* The final overriding function.  */
2321   tree overriding_fn;
2322   /* The BINFO for the class in which the final overriding function
2323      appears.  */
2324   tree overriding_base;
2325 } find_final_overrider_data;
2326
2327 /* Called from find_final_overrider via dfs_walk.  */
2328
2329 static tree
2330 dfs_find_final_overrider (binfo, data)
2331      tree binfo;
2332      void *data;
2333 {
2334   find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2335
2336   if (same_type_p (BINFO_TYPE (binfo), 
2337                    BINFO_TYPE (ffod->declaring_base))
2338       && tree_int_cst_equal (BINFO_OFFSET (binfo),
2339                              BINFO_OFFSET (ffod->declaring_base)))
2340     {
2341       tree path;
2342       tree method;
2343
2344       /* We've found a path to the declaring base.  Walk down the path
2345          looking for an overrider for FN.  */
2346       for (path = reverse_path (binfo); 
2347            path; 
2348            path = TREE_CHAIN (path))
2349         {
2350           for (method = TYPE_METHODS (BINFO_TYPE (TREE_VALUE (path)));
2351                method;
2352                method = TREE_CHAIN (method))
2353             if (DECL_VIRTUAL_P (method) && overrides (method, ffod->fn))
2354               break;
2355
2356           if (method)
2357             break;
2358         }
2359
2360       /* If we found an overrider, record the overriding function, and
2361          the base from which it came.  */
2362       if (path)
2363         {
2364           if (ffod->overriding_fn && ffod->overriding_fn != method)
2365             {
2366               /* We've found a different overrider along a different
2367                  path.  That can be OK if the new one overrides the
2368                  old one.  Consider:
2369               
2370                    struct S { virtual void f(); };
2371                    struct T : public virtual S { virtual void f(); };
2372                    struct U : public virtual S, public virtual T {};
2373               
2374                  Here `T::f' is the final overrider for `S::f'.  */
2375               if (strictly_overrides (method, ffod->overriding_fn))
2376                 {
2377                   ffod->overriding_fn = method;
2378                   ffod->overriding_base = TREE_VALUE (path);
2379                 }
2380               else if (!strictly_overrides (ffod->overriding_fn, method))
2381                 {
2382                   cp_error ("no unique final overrider for `%D' in `%T'", 
2383                             ffod->most_derived_type,
2384                             ffod->fn);
2385                   cp_error ("candidates are: `%#D'", ffod->overriding_fn);
2386                   cp_error ("                `%#D'", method);
2387                   return error_mark_node;
2388                 }
2389             }
2390           else if (ffod->overriding_base
2391                    && (!tree_int_cst_equal 
2392                        (BINFO_OFFSET (TREE_VALUE (path)),
2393                         BINFO_OFFSET (ffod->overriding_base))))
2394             {
2395               /* We've found two instances of the same base that
2396                  provide overriders.  */
2397               cp_error ("no unique final overrider for `%D' since there two instances of `%T' in `%T'", 
2398                         ffod->fn,
2399                         BINFO_TYPE (ffod->overriding_base),
2400                         ffod->most_derived_type);
2401               return error_mark_node;
2402             }
2403           else
2404             {
2405               ffod->overriding_fn = method;
2406               ffod->overriding_base = TREE_VALUE (path);
2407             }
2408         }
2409     }
2410
2411   return NULL_TREE;
2412 }
2413
2414 /* Returns a TREE_LIST whose TREE_PURPOSE is the final overrider for
2415    FN and whose TREE_VALUE is the binfo for the base where the
2416    overriding occurs.  BINFO (in the hierarchy dominated by T) is the
2417    base object in which FN is declared.  */
2418
2419 static tree
2420 find_final_overrider (t, binfo, fn)
2421      tree t;
2422      tree binfo;
2423      tree fn;
2424 {
2425   find_final_overrider_data ffod;
2426
2427   /* Getting this right is a little tricky.  This is legal:
2428
2429        struct S { virtual void f (); };
2430        struct T { virtual void f (); };
2431        struct U : public S, public T { };
2432
2433      even though calling `f' in `U' is ambiguous.  But, 
2434
2435        struct R { virtual void f(); };
2436        struct S : virtual public R { virtual void f (); };
2437        struct T : virtual public R { virtual void f (); };
2438        struct U : public S, public T { };
2439
2440      is not -- there's no way  to decide whether to put `S::f' or
2441      `T::f' in the vtable for `R'.  
2442      
2443      The solution is to look at all paths to BINFO.  If we find
2444      different overriders along any two, then there is a problem.  */
2445   ffod.fn = fn;
2446   ffod.declaring_base = binfo;
2447   ffod.most_derived_type = t;
2448   ffod.overriding_fn = NULL_TREE;
2449   ffod.overriding_base = NULL_TREE;
2450
2451   if (dfs_walk (TYPE_BINFO (t),
2452                 dfs_find_final_overrider,
2453                 NULL,
2454                 &ffod))
2455     return error_mark_node;
2456
2457   return build_tree_list (ffod.overriding_fn, ffod.overriding_base);
2458 }
2459
2460 /* Called via dfs_walk.  Returns BINFO if BINFO has the same type as
2461    DATA (which is really an _TYPE node).  */
2462
2463 static tree
2464 dfs_find_base (binfo, data)
2465      tree binfo;
2466      void *data;
2467 {
2468   return (same_type_p (BINFO_TYPE (binfo), (tree) data)
2469           ? binfo : NULL_TREE);
2470 }
2471
2472 /* Update a entry in the vtable for BINFO, which is in the hierarchy
2473    dominated by T.  FN has been overridden in BINFO; VIRTUALS points
2474    to the corresponding position in the BINFO_VIRTUALS list.  */
2475
2476 static void
2477 update_vtable_entry_for_fn (t, binfo, fn, virtuals)
2478      tree t;
2479      tree binfo;
2480      tree fn;
2481      tree *virtuals;
2482 {
2483   tree b;
2484   tree overrider;
2485   tree vindex;
2486   tree delta;
2487   HOST_WIDE_INT vindex_val;
2488   HOST_WIDE_INT i;
2489
2490   /* Find the function which originally caused this vtable
2491      entry to be present.  */
2492   vindex = DECL_VINDEX (fn);
2493   b = dfs_walk (binfo, dfs_find_base, NULL, DECL_VIRTUAL_CONTEXT (fn));
2494   fn = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (b)));
2495   i = first_vfun_index (BINFO_TYPE (b));
2496   vindex_val = tree_low_cst (vindex, 0);
2497   while (i < vindex_val)
2498     {
2499       fn = TREE_CHAIN (fn);
2500       ++i;
2501     }
2502   fn = BV_FN (fn);
2503
2504   /* Handle the case of a virtual function defined in BINFO itself.  */
2505   overrider = find_final_overrider (t, b, fn);
2506   if (overrider == error_mark_node)
2507     return;
2508
2509   /* Compute the constant adjustment to the `this' pointer.  The
2510      `this' pointer, when this function is called, will point at the
2511      class whose vtable this is.  */
2512   delta = size_binop (PLUS_EXPR,
2513                       get_derived_offset (binfo,
2514                                           DECL_VIRTUAL_CONTEXT (fn)),
2515                       BINFO_OFFSET (binfo));
2516   if (flag_new_abi)
2517     {
2518       /* Under the new ABI, we only need to adjust as far as the
2519          nearest virtual base.  Then we use the vcall offset in the
2520          virtual bases vtable.  */
2521       for (b = binfo; b; b = BINFO_INHERITANCE_CHAIN (b))
2522         {
2523           if (TREE_VIA_VIRTUAL (b))
2524             break;
2525           if (same_type_p (BINFO_TYPE (b), 
2526                            BINFO_TYPE (TREE_VALUE (overrider))))
2527             break;
2528         }
2529     }
2530   else
2531     b = NULL_TREE;
2532
2533   if (b && TREE_VIA_VIRTUAL (b))
2534     /* The `this' pointer needs to be adjusted to the nearest virtual
2535        base.  */
2536     delta = size_diffop (BINFO_OFFSET (b), delta);
2537   else
2538     /* The `this' pointer needs to be adjusted from pointing to
2539        BINFO to pointing at the base where the final overrider
2540        appears.  */
2541     delta = size_diffop (BINFO_OFFSET (TREE_VALUE (overrider)), delta);
2542
2543   modify_vtable_entry (t, 
2544                        binfo, 
2545                        TREE_PURPOSE (overrider),
2546                        delta,
2547                        virtuals);
2548 }
2549
2550 /* Called from modify_all_vtables via dfs_walk.  */
2551
2552 static tree
2553 dfs_modify_vtables (binfo, data)
2554      tree binfo;
2555      void *data;
2556 {
2557   if (/* There's no need to modify the vtable for a primary base;
2558          we're not going to use that vtable anyhow.  */
2559       !BINFO_PRIMARY_MARKED_P (binfo)
2560       /* Similarly, a base without a vtable needs no modification.  */
2561       && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
2562     {
2563       tree t;
2564       tree virtuals;
2565       tree old_virtuals;
2566
2567       t = (tree) data;
2568
2569       /* If we're supporting RTTI then we always need a new vtable to
2570          point to the RTTI information.  Under the new ABI we may need
2571          a new vtable to contain vcall and vbase offsets.  */
2572       if (flag_rtti || flag_new_abi)
2573         make_new_vtable (t, binfo);
2574       
2575       /* Now, go through each of the virtual functions in the virtual
2576          function table for BINFO.  Find the final overrider, and
2577          update the BINFO_VIRTUALS list appropriately.  */
2578       for (virtuals = BINFO_VIRTUALS (binfo),
2579              old_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo)));
2580            virtuals;
2581            virtuals = TREE_CHAIN (virtuals),
2582              old_virtuals = TREE_CHAIN (old_virtuals))
2583         update_vtable_entry_for_fn (t, 
2584                                     binfo, 
2585                                     BV_FN (old_virtuals),
2586                                     &virtuals);
2587     }
2588
2589   SET_BINFO_MARKED (binfo);
2590
2591   return NULL_TREE;
2592 }
2593
2594 /* Update all of the primary and secondary vtables for T.  Create new
2595    vtables as required, and initialize their RTTI information.  Each
2596    of the functions in OVERRIDDEN_VIRTUALS overrides a virtual
2597    function from a base class; find and modify the appropriate entries
2598    to point to the overriding functions.  Returns a list, in
2599    declaration order, of the functions that are overridden in this
2600    class, but do not appear in the primary base class vtable, and
2601    which should therefore be appended to the end of the vtable for T.  */
2602
2603 static tree
2604 modify_all_vtables (t, vfuns_p, overridden_virtuals)
2605      tree t;
2606      int *vfuns_p;
2607      tree overridden_virtuals;
2608 {
2609   tree binfo;
2610
2611   binfo = TYPE_BINFO (t);
2612
2613   /* Update all of the vtables.  */
2614   dfs_walk (binfo, 
2615             dfs_modify_vtables, 
2616             dfs_unmarked_real_bases_queue_p,
2617             t);
2618   dfs_walk (binfo, dfs_unmark, dfs_marked_real_bases_queue_p, t);
2619
2620   /* If we should include overriding functions for secondary vtables
2621      in our primary vtable, add them now.  */
2622   if (all_overridden_vfuns_in_vtables_p ())
2623     {
2624       tree *fnsp = &overridden_virtuals;
2625
2626       while (*fnsp)
2627         {
2628           tree fn = TREE_VALUE (*fnsp);
2629
2630           if (!BINFO_VIRTUALS (binfo)
2631               || !value_member (fn, BINFO_VIRTUALS (binfo)))
2632             {
2633               /* Set the vtable index.  */
2634               set_vindex (t, fn, vfuns_p);
2635               /* We don't need to convert to a base class when calling
2636                  this function.  */
2637               DECL_VIRTUAL_CONTEXT (fn) = t;
2638
2639               /* We don't need to adjust the `this' pointer when
2640                  calling this function.  */
2641               BV_DELTA (*fnsp) = integer_zero_node;
2642               BV_VCALL_INDEX (*fnsp) = integer_zero_node;
2643
2644               /* This is an overridden function not already in our
2645                  vtable.  Keep it.  */
2646               fnsp = &TREE_CHAIN (*fnsp);
2647             }
2648           else
2649             /* We've already got an entry for this function.  Skip
2650                it.  */
2651             *fnsp = TREE_CHAIN (*fnsp);
2652         }
2653     }
2654   else
2655     overridden_virtuals = NULL_TREE;
2656
2657   return overridden_virtuals;
2658 }
2659
2660 /* Here, we already know that they match in every respect.
2661    All we have to check is where they had their declarations.  */
2662
2663 static int 
2664 strictly_overrides (fndecl1, fndecl2)
2665      tree fndecl1, fndecl2;
2666 {
2667   int distance = get_base_distance (DECL_CONTEXT (fndecl2),
2668                                     DECL_CONTEXT (fndecl1),
2669                                     0, (tree *)0);
2670   if (distance == -2 || distance > 0)
2671     return 1;
2672   return 0;
2673 }
2674
2675 /* Get the base virtual function declarations in T that are either
2676    overridden or hidden by FNDECL as a list.  We set TREE_PURPOSE with
2677    the overrider/hider.  */
2678
2679 static tree
2680 get_basefndecls (fndecl, t)
2681      tree fndecl, t;
2682 {
2683   tree methods = TYPE_METHODS (t);
2684   tree base_fndecls = NULL_TREE;
2685   tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
2686   int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2687
2688   while (methods)
2689     {
2690       if (TREE_CODE (methods) == FUNCTION_DECL
2691           && DECL_VINDEX (methods) != NULL_TREE
2692           && DECL_NAME (fndecl) == DECL_NAME (methods))
2693         base_fndecls = tree_cons (fndecl, methods, base_fndecls);
2694
2695       methods = TREE_CHAIN (methods);
2696     }
2697
2698   if (base_fndecls)
2699     return base_fndecls;
2700
2701   for (i = 0; i < n_baseclasses; i++)
2702     {
2703       tree base_binfo = TREE_VEC_ELT (binfos, i);
2704       tree basetype = BINFO_TYPE (base_binfo);
2705
2706       base_fndecls = chainon (get_basefndecls (fndecl, basetype),
2707                               base_fndecls);
2708     }
2709
2710   return base_fndecls;
2711 }
2712
2713 /* Mark the functions that have been hidden with their overriders.
2714    Since we start out with all functions already marked with a hider,
2715    no need to mark functions that are just hidden.
2716
2717    Subroutine of warn_hidden.  */
2718
2719 static void
2720 mark_overriders (fndecl, base_fndecls)
2721      tree fndecl, base_fndecls;
2722 {
2723   for (; base_fndecls; base_fndecls = TREE_CHAIN (base_fndecls))
2724     {
2725       if (overrides (fndecl, TREE_VALUE (base_fndecls)))
2726         TREE_PURPOSE (base_fndecls) = fndecl;
2727     }
2728 }
2729
2730 /* If this declaration supersedes the declaration of
2731    a method declared virtual in the base class, then
2732    mark this field as being virtual as well.  */
2733
2734 static void
2735 check_for_override (decl, ctype)
2736      tree decl, ctype;
2737 {
2738   tree binfos = BINFO_BASETYPES (TYPE_BINFO (ctype));
2739   int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2740   int virtualp = DECL_VIRTUAL_P (decl);
2741   int found_overriden_fn = 0;
2742
2743   for (i = 0; i < n_baselinks; i++)
2744     {
2745       tree base_binfo = TREE_VEC_ELT (binfos, i);
2746       if (TYPE_POLYMORPHIC_P (BINFO_TYPE (base_binfo)))
2747         {
2748           tree tmp = get_matching_virtual
2749             (base_binfo, decl, DECL_DESTRUCTOR_P (decl));
2750
2751           if (tmp && !found_overriden_fn)
2752             {
2753               /* If this function overrides some virtual in some base
2754                  class, then the function itself is also necessarily
2755                  virtual, even if the user didn't explicitly say so.  */
2756               DECL_VIRTUAL_P (decl) = 1;
2757
2758               /* The TMP we really want is the one from the deepest
2759                  baseclass on this path, taking care not to
2760                  duplicate if we have already found it (via another
2761                  path to its virtual baseclass.  */
2762               if (TREE_CODE (TREE_TYPE (decl)) == FUNCTION_TYPE)
2763                 {
2764                   cp_error_at ("`static %#D' cannot be declared", decl);
2765                   cp_error_at ("  since `virtual %#D' declared in base class",
2766                                tmp);
2767                   break;
2768                 }
2769               virtualp = 1;
2770
2771               /* Set DECL_VINDEX to a value that is neither an
2772                  INTEGER_CST nor the error_mark_node so that
2773                  add_virtual_function will realize this is an
2774                  overridden function.  */
2775               DECL_VINDEX (decl) 
2776                 = tree_cons (tmp, NULL_TREE, DECL_VINDEX (decl));
2777               
2778               /* We now know that DECL overrides something,
2779                  which is all that is important.  But, we must
2780                  continue to iterate through all the base-classes
2781                  in order to allow get_matching_virtual to check for
2782                  various illegal overrides.  */
2783               found_overriden_fn = 1;
2784             }
2785         }
2786     }
2787   if (virtualp)
2788     {
2789       if (DECL_VINDEX (decl) == NULL_TREE)
2790         DECL_VINDEX (decl) = error_mark_node;
2791       IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = 1;
2792     }
2793 }
2794
2795 /* Warn about hidden virtual functions that are not overridden in t.
2796    We know that constructors and destructors don't apply.  */
2797
2798 void
2799 warn_hidden (t)
2800      tree t;
2801 {
2802   tree method_vec = CLASSTYPE_METHOD_VEC (t);
2803   int n_methods = method_vec ? TREE_VEC_LENGTH (method_vec) : 0;
2804   int i;
2805
2806   /* We go through each separately named virtual function.  */
2807   for (i = 2; i < n_methods && TREE_VEC_ELT (method_vec, i); ++i)
2808     {
2809       tree fns = TREE_VEC_ELT (method_vec, i);
2810       tree fndecl = NULL_TREE;
2811
2812       tree base_fndecls = NULL_TREE;
2813       tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
2814       int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2815
2816       /* First see if we have any virtual functions in this batch.  */
2817       for (; fns; fns = OVL_NEXT (fns))
2818         {
2819           fndecl = OVL_CURRENT (fns);
2820           if (DECL_VINDEX (fndecl))
2821             break;
2822         }
2823
2824       if (fns == NULL_TREE)
2825         continue;
2826
2827       /* First we get a list of all possible functions that might be
2828          hidden from each base class.  */
2829       for (i = 0; i < n_baseclasses; i++)
2830         {
2831           tree base_binfo = TREE_VEC_ELT (binfos, i);
2832           tree basetype = BINFO_TYPE (base_binfo);
2833
2834           base_fndecls = chainon (get_basefndecls (fndecl, basetype),
2835                                   base_fndecls);
2836         }
2837
2838       fns = OVL_NEXT (fns);
2839
2840       /* ...then mark up all the base functions with overriders, preferring
2841          overriders to hiders.  */
2842       if (base_fndecls)
2843         for (; fns; fns = OVL_NEXT (fns))
2844           {
2845             fndecl = OVL_CURRENT (fns);
2846             if (DECL_VINDEX (fndecl))
2847               mark_overriders (fndecl, base_fndecls);
2848           }
2849
2850       /* Now give a warning for all base functions without overriders,
2851          as they are hidden.  */
2852       for (; base_fndecls; base_fndecls = TREE_CHAIN (base_fndecls))
2853         {
2854           if (! overrides (TREE_PURPOSE (base_fndecls),
2855                            TREE_VALUE (base_fndecls)))
2856             {
2857               /* Here we know it is a hider, and no overrider exists.  */
2858               cp_warning_at ("`%D' was hidden", TREE_VALUE (base_fndecls));
2859               cp_warning_at ("  by `%D'", TREE_PURPOSE (base_fndecls));
2860             }
2861         }
2862     }
2863 }
2864
2865 /* Check for things that are invalid.  There are probably plenty of other
2866    things we should check for also.  */
2867
2868 static void
2869 finish_struct_anon (t)
2870      tree t;
2871 {
2872   tree field;
2873
2874   for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
2875     {
2876       if (TREE_STATIC (field))
2877         continue;
2878       if (TREE_CODE (field) != FIELD_DECL)
2879         continue;
2880
2881       if (DECL_NAME (field) == NULL_TREE
2882           && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
2883         {
2884           tree elt = TYPE_FIELDS (TREE_TYPE (field));
2885           for (; elt; elt = TREE_CHAIN (elt))
2886             {
2887               if (DECL_ARTIFICIAL (elt))
2888                 continue;
2889
2890               if (DECL_NAME (elt) == constructor_name (t))
2891                 cp_pedwarn_at ("ISO C++ forbids member `%D' with same name as enclosing class",
2892                                elt);
2893
2894               if (TREE_CODE (elt) != FIELD_DECL)
2895                 {
2896                   cp_pedwarn_at ("`%#D' invalid; an anonymous union can only have non-static data members",
2897                                  elt);
2898                   continue;
2899                 }
2900
2901               if (TREE_PRIVATE (elt))
2902                 cp_pedwarn_at ("private member `%#D' in anonymous union",
2903                                elt);
2904               else if (TREE_PROTECTED (elt))
2905                 cp_pedwarn_at ("protected member `%#D' in anonymous union",
2906                                elt);
2907
2908               TREE_PRIVATE (elt) = TREE_PRIVATE (field);
2909               TREE_PROTECTED (elt) = TREE_PROTECTED (field);
2910             }
2911         }
2912     }
2913 }
2914
2915 /* Create default constructors, assignment operators, and so forth for
2916    the type indicated by T, if they are needed.
2917    CANT_HAVE_DEFAULT_CTOR, CANT_HAVE_CONST_CTOR, and
2918    CANT_HAVE_CONST_ASSIGNMENT are nonzero if, for whatever reason, the
2919    class cannot have a default constructor, copy constructor taking a
2920    const reference argument, or an assignment operator taking a const
2921    reference, respectively.  If a virtual destructor is created, its
2922    DECL is returned; otherwise the return value is NULL_TREE.  */
2923
2924 static tree
2925 add_implicitly_declared_members (t, cant_have_default_ctor,
2926                                  cant_have_const_cctor,
2927                                  cant_have_const_assignment)
2928      tree t;
2929      int cant_have_default_ctor;
2930      int cant_have_const_cctor;
2931      int cant_have_const_assignment;
2932 {
2933   tree default_fn;
2934   tree implicit_fns = NULL_TREE;
2935   tree virtual_dtor = NULL_TREE;
2936   tree *f;
2937
2938   /* Destructor.  */
2939   if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) && !TYPE_HAS_DESTRUCTOR (t))
2940     {
2941       default_fn = implicitly_declare_fn (sfk_destructor, t, /*const_p=*/0);
2942       check_for_override (default_fn, t);
2943
2944       /* If we couldn't make it work, then pretend we didn't need it.  */
2945       if (default_fn == void_type_node)
2946         TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = 0;
2947       else
2948         {
2949           TREE_CHAIN (default_fn) = implicit_fns;
2950           implicit_fns = default_fn;
2951
2952           if (DECL_VINDEX (default_fn))
2953             virtual_dtor = default_fn;
2954         }
2955     }
2956   else
2957     /* Any non-implicit destructor is non-trivial.  */
2958     TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) |= TYPE_HAS_DESTRUCTOR (t);
2959
2960   /* Default constructor.  */
2961   if (! TYPE_HAS_CONSTRUCTOR (t) && ! cant_have_default_ctor)
2962     {
2963       default_fn = implicitly_declare_fn (sfk_constructor, t, /*const_p=*/0);
2964       TREE_CHAIN (default_fn) = implicit_fns;
2965       implicit_fns = default_fn;
2966     }
2967
2968   /* Copy constructor.  */
2969   if (! TYPE_HAS_INIT_REF (t) && ! TYPE_FOR_JAVA (t))
2970     {
2971       /* ARM 12.18: You get either X(X&) or X(const X&), but
2972          not both.  --Chip  */
2973       default_fn 
2974         = implicitly_declare_fn (sfk_copy_constructor, t,
2975                                  /*const_p=*/!cant_have_const_cctor);
2976       TREE_CHAIN (default_fn) = implicit_fns;
2977       implicit_fns = default_fn;
2978     }
2979
2980   /* Assignment operator.  */
2981   if (! TYPE_HAS_ASSIGN_REF (t) && ! TYPE_FOR_JAVA (t))
2982     {
2983       default_fn 
2984         = implicitly_declare_fn (sfk_assignment_operator, t,
2985                                  /*const_p=*/!cant_have_const_assignment);
2986       TREE_CHAIN (default_fn) = implicit_fns;
2987       implicit_fns = default_fn;
2988     }
2989
2990   /* Now, hook all of the new functions on to TYPE_METHODS,
2991      and add them to the CLASSTYPE_METHOD_VEC.  */
2992   for (f = &implicit_fns; *f; f = &TREE_CHAIN (*f))
2993     add_method (t, 0, *f);
2994   *f = TYPE_METHODS (t);
2995   TYPE_METHODS (t) = implicit_fns;
2996
2997   return virtual_dtor;
2998 }
2999
3000 /* Subroutine of finish_struct_1.  Recursively count the number of fields
3001    in TYPE, including anonymous union members.  */
3002
3003 static int
3004 count_fields (fields)
3005      tree fields;
3006 {
3007   tree x;
3008   int n_fields = 0;
3009   for (x = fields; x; x = TREE_CHAIN (x))
3010     {
3011       if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
3012         n_fields += count_fields (TYPE_FIELDS (TREE_TYPE (x)));
3013       else
3014         n_fields += 1;
3015     }
3016   return n_fields;
3017 }
3018
3019 /* Subroutine of finish_struct_1.  Recursively add all the fields in the
3020    TREE_LIST FIELDS to the TREE_VEC FIELD_VEC, starting at offset IDX.  */
3021
3022 static int
3023 add_fields_to_vec (fields, field_vec, idx)
3024      tree fields, field_vec;
3025      int idx;
3026 {
3027   tree x;
3028   for (x = fields; x; x = TREE_CHAIN (x))
3029     {
3030       if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
3031         idx = add_fields_to_vec (TYPE_FIELDS (TREE_TYPE (x)), field_vec, idx);
3032       else
3033         TREE_VEC_ELT (field_vec, idx++) = x;
3034     }
3035   return idx;
3036 }
3037
3038 /* FIELD is a bit-field.  We are finishing the processing for its
3039    enclosing type.  Issue any appropriate messages and set appropriate
3040    flags.  */
3041
3042 static void
3043 check_bitfield_decl (field)
3044      tree field;
3045 {
3046   tree type = TREE_TYPE (field);
3047   tree w = NULL_TREE;
3048
3049   /* Detect invalid bit-field type.  */
3050   if (DECL_INITIAL (field)
3051       && ! INTEGRAL_TYPE_P (TREE_TYPE (field)))
3052     {
3053       cp_error_at ("bit-field `%#D' with non-integral type", field);
3054       w = error_mark_node;
3055     }
3056
3057   /* Detect and ignore out of range field width.  */
3058   if (DECL_INITIAL (field))
3059     {
3060       w = DECL_INITIAL (field);
3061
3062       /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs.  */
3063       STRIP_NOPS (w);
3064
3065       /* detect invalid field size.  */
3066       if (TREE_CODE (w) == CONST_DECL)
3067         w = DECL_INITIAL (w);
3068       else
3069         w = decl_constant_value (w);
3070
3071       if (TREE_CODE (w) != INTEGER_CST)
3072         {
3073           cp_error_at ("bit-field `%D' width not an integer constant",
3074                        field);
3075           w = error_mark_node;
3076         }
3077       else if (tree_int_cst_sgn (w) < 0)
3078         {
3079           cp_error_at ("negative width in bit-field `%D'", field);
3080           w = error_mark_node;
3081         }
3082       else if (integer_zerop (w) && DECL_NAME (field) != 0)
3083         {
3084           cp_error_at ("zero width for bit-field `%D'", field);
3085           w = error_mark_node;
3086         }
3087       else if (compare_tree_int (w, TYPE_PRECISION (type)) > 0
3088                && TREE_CODE (type) != ENUMERAL_TYPE
3089                && TREE_CODE (type) != BOOLEAN_TYPE)
3090         cp_warning_at ("width of `%D' exceeds its type", field);
3091       else if (TREE_CODE (type) == ENUMERAL_TYPE
3092                && (0 > compare_tree_int (w,
3093                                          min_precision (TYPE_MIN_VALUE (type),
3094                                                         TREE_UNSIGNED (type)))
3095                    ||  0 > compare_tree_int (w,
3096                                              min_precision
3097                                              (TYPE_MAX_VALUE (type),
3098                                               TREE_UNSIGNED (type)))))
3099         cp_warning_at ("`%D' is too small to hold all values of `%#T'",
3100                        field, type);
3101     }
3102   
3103   /* Remove the bit-field width indicator so that the rest of the
3104      compiler does not treat that value as an initializer.  */
3105   DECL_INITIAL (field) = NULL_TREE;
3106
3107   if (w != error_mark_node)
3108     {
3109       DECL_SIZE (field) = convert (bitsizetype, w);
3110       DECL_BIT_FIELD (field) = 1;
3111
3112       if (integer_zerop (w))
3113         {
3114 #ifdef EMPTY_FIELD_BOUNDARY
3115           DECL_ALIGN (field) = MAX (DECL_ALIGN (field), 
3116                                     EMPTY_FIELD_BOUNDARY);
3117 #endif
3118 #ifdef PCC_BITFIELD_TYPE_MATTERS
3119           if (PCC_BITFIELD_TYPE_MATTERS)
3120             DECL_ALIGN (field) = MAX (DECL_ALIGN (field), 
3121                                       TYPE_ALIGN (type));
3122 #endif
3123         }
3124     }
3125   else
3126     {
3127       /* Non-bit-fields are aligned for their type.  */
3128       DECL_BIT_FIELD (field) = 0;
3129       CLEAR_DECL_C_BIT_FIELD (field);
3130       DECL_ALIGN (field) = MAX (DECL_ALIGN (field), TYPE_ALIGN (type));
3131     }
3132 }
3133
3134 /* FIELD is a non bit-field.  We are finishing the processing for its
3135    enclosing type T.  Issue any appropriate messages and set appropriate
3136    flags.  */
3137
3138 static void
3139 check_field_decl (field, t, cant_have_const_ctor,
3140                   cant_have_default_ctor, no_const_asn_ref,
3141                   any_default_members)
3142      tree field;
3143      tree t;
3144      int *cant_have_const_ctor;
3145      int *cant_have_default_ctor;
3146      int *no_const_asn_ref;
3147      int *any_default_members;
3148 {
3149   tree type = strip_array_types (TREE_TYPE (field));
3150
3151   /* An anonymous union cannot contain any fields which would change
3152      the settings of CANT_HAVE_CONST_CTOR and friends.  */
3153   if (ANON_UNION_TYPE_P (type))
3154     ;
3155   /* And, we don't set TYPE_HAS_CONST_INIT_REF, etc., for anonymous
3156      structs.  So, we recurse through their fields here.  */
3157   else if (ANON_AGGR_TYPE_P (type))
3158     {
3159       tree fields;
3160
3161       for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields))
3162         if (TREE_CODE (fields) == FIELD_DECL && !DECL_C_BIT_FIELD (field))
3163           check_field_decl (fields, t, cant_have_const_ctor,
3164                             cant_have_default_ctor, no_const_asn_ref,
3165                             any_default_members);
3166     }
3167   /* Check members with class type for constructors, destructors,
3168      etc.  */
3169   else if (CLASS_TYPE_P (type))
3170     {
3171       /* Never let anything with uninheritable virtuals
3172          make it through without complaint.  */
3173       abstract_virtuals_error (field, type);
3174                       
3175       if (TREE_CODE (t) == UNION_TYPE)
3176         {
3177           if (TYPE_NEEDS_CONSTRUCTING (type))
3178             cp_error_at ("member `%#D' with constructor not allowed in union",
3179                          field);
3180           if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3181             cp_error_at ("member `%#D' with destructor not allowed in union",
3182                          field);
3183           if (TYPE_HAS_COMPLEX_ASSIGN_REF (type))
3184             cp_error_at ("member `%#D' with copy assignment operator not allowed in union",
3185                          field);
3186         }
3187       else
3188         {
3189           TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type);
3190           TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) 
3191             |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type);
3192           TYPE_HAS_COMPLEX_ASSIGN_REF (t) |= TYPE_HAS_COMPLEX_ASSIGN_REF (type);
3193           TYPE_HAS_COMPLEX_INIT_REF (t) |= TYPE_HAS_COMPLEX_INIT_REF (type);
3194         }
3195
3196       if (!TYPE_HAS_CONST_INIT_REF (type))
3197         *cant_have_const_ctor = 1;
3198
3199       if (!TYPE_HAS_CONST_ASSIGN_REF (type))
3200         *no_const_asn_ref = 1;
3201
3202       if (TYPE_HAS_CONSTRUCTOR (type)
3203           && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
3204         *cant_have_default_ctor = 1;
3205     }
3206   if (DECL_INITIAL (field) != NULL_TREE)
3207     {
3208       /* `build_class_init_list' does not recognize
3209          non-FIELD_DECLs.  */
3210       if (TREE_CODE (t) == UNION_TYPE && any_default_members != 0)
3211         cp_error_at ("multiple fields in union `%T' initialized");
3212       *any_default_members = 1;
3213     }
3214
3215   /* Non-bit-fields are aligned for their type, except packed fields
3216      which require only BITS_PER_UNIT alignment.  */
3217   DECL_ALIGN (field) = MAX (DECL_ALIGN (field), 
3218                             (DECL_PACKED (field) 
3219                              ? BITS_PER_UNIT
3220                              : TYPE_ALIGN (TREE_TYPE (field))));
3221 }
3222
3223 /* Check the data members (both static and non-static), class-scoped
3224    typedefs, etc., appearing in the declaration of T.  Issue
3225    appropriate diagnostics.  Sets ACCESS_DECLS to a list (in
3226    declaration order) of access declarations; each TREE_VALUE in this
3227    list is a USING_DECL.
3228
3229    In addition, set the following flags:
3230
3231      EMPTY_P
3232        The class is empty, i.e., contains no non-static data members.
3233
3234      CANT_HAVE_DEFAULT_CTOR_P
3235        This class cannot have an implicitly generated default
3236        constructor.
3237
3238      CANT_HAVE_CONST_CTOR_P
3239        This class cannot have an implicitly generated copy constructor
3240        taking a const reference.
3241
3242      CANT_HAVE_CONST_ASN_REF
3243        This class cannot have an implicitly generated assignment
3244        operator taking a const reference.
3245
3246    All of these flags should be initialized before calling this
3247    function.
3248
3249    Returns a pointer to the end of the TYPE_FIELDs chain; additional
3250    fields can be added by adding to this chain.  */
3251
3252 static void
3253 check_field_decls (t, access_decls, empty_p, 
3254                    cant_have_default_ctor_p, cant_have_const_ctor_p,
3255                    no_const_asn_ref_p)
3256      tree t;
3257      tree *access_decls;
3258      int *empty_p;
3259      int *cant_have_default_ctor_p;
3260      int *cant_have_const_ctor_p;
3261      int *no_const_asn_ref_p;
3262 {
3263   tree *field;
3264   tree *next;
3265   int has_pointers;
3266   int any_default_members;
3267
3268   /* First, delete any duplicate fields.  */
3269   delete_duplicate_fields (TYPE_FIELDS (t));
3270
3271   /* Assume there are no access declarations.  */
3272   *access_decls = NULL_TREE;
3273   /* Assume this class has no pointer members.  */
3274   has_pointers = 0;
3275   /* Assume none of the members of this class have default
3276      initializations.  */
3277   any_default_members = 0;
3278
3279   for (field = &TYPE_FIELDS (t); *field; field = next)
3280     {
3281       tree x = *field;
3282       tree type = TREE_TYPE (x);
3283
3284       GNU_xref_member (current_class_name, x);
3285
3286       next = &TREE_CHAIN (x);
3287
3288       if (TREE_CODE (x) == FIELD_DECL)
3289         {
3290           DECL_PACKED (x) |= TYPE_PACKED (t);
3291
3292           if (DECL_C_BIT_FIELD (x) && integer_zerop (DECL_INITIAL (x)))
3293             /* We don't treat zero-width bitfields as making a class
3294                non-empty.  */
3295             ;
3296           else
3297             {
3298               /* The class is non-empty.  */
3299               *empty_p = 0;
3300               /* The class is not even nearly empty.  */
3301               CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
3302             }
3303         }
3304
3305       if (TREE_CODE (x) == USING_DECL)
3306         {
3307           /* Prune the access declaration from the list of fields.  */
3308           *field = TREE_CHAIN (x);
3309
3310           /* Save the access declarations for our caller.  */
3311           *access_decls = tree_cons (NULL_TREE, x, *access_decls);
3312
3313           /* Since we've reset *FIELD there's no reason to skip to the
3314              next field.  */
3315           next = field;
3316           continue;
3317         }
3318
3319       if (TREE_CODE (x) == TYPE_DECL
3320           || TREE_CODE (x) == TEMPLATE_DECL)
3321         continue;
3322
3323       /* If we've gotten this far, it's a data member, possibly static,
3324          or an enumerator.  */
3325
3326       DECL_CONTEXT (x) = t;
3327
3328       /* ``A local class cannot have static data members.'' ARM 9.4 */
3329       if (current_function_decl && TREE_STATIC (x))
3330         cp_error_at ("field `%D' in local class cannot be static", x);
3331
3332       /* Perform error checking that did not get done in
3333          grokdeclarator.  */
3334       if (TREE_CODE (type) == FUNCTION_TYPE)
3335         {
3336           cp_error_at ("field `%D' invalidly declared function type",
3337                        x);
3338           type = build_pointer_type (type);
3339           TREE_TYPE (x) = type;
3340         }
3341       else if (TREE_CODE (type) == METHOD_TYPE)
3342         {
3343           cp_error_at ("field `%D' invalidly declared method type", x);
3344           type = build_pointer_type (type);
3345           TREE_TYPE (x) = type;
3346         }
3347       else if (TREE_CODE (type) == OFFSET_TYPE)
3348         {
3349           cp_error_at ("field `%D' invalidly declared offset type", x);
3350           type = build_pointer_type (type);
3351           TREE_TYPE (x) = type;
3352         }
3353
3354       if (type == error_mark_node)
3355         continue;
3356           
3357       /* When this goes into scope, it will be a non-local reference.  */
3358       DECL_NONLOCAL (x) = 1;
3359
3360       if (TREE_CODE (x) == CONST_DECL)
3361         continue;
3362
3363       if (TREE_CODE (x) == VAR_DECL)
3364         {
3365           if (TREE_CODE (t) == UNION_TYPE)
3366             /* Unions cannot have static members.  */
3367             cp_error_at ("field `%D' declared static in union", x);
3368               
3369           continue;
3370         }
3371
3372       /* Now it can only be a FIELD_DECL.  */
3373
3374       if (TREE_PRIVATE (x) || TREE_PROTECTED (x))
3375         CLASSTYPE_NON_AGGREGATE (t) = 1;
3376
3377       /* If this is of reference type, check if it needs an init.
3378          Also do a little ANSI jig if necessary.  */
3379       if (TREE_CODE (type) == REFERENCE_TYPE)
3380         {
3381           CLASSTYPE_NON_POD_P (t) = 1;
3382           if (DECL_INITIAL (x) == NULL_TREE)
3383             CLASSTYPE_REF_FIELDS_NEED_INIT (t) = 1;
3384
3385           /* ARM $12.6.2: [A member initializer list] (or, for an
3386              aggregate, initialization by a brace-enclosed list) is the
3387              only way to initialize nonstatic const and reference
3388              members.  */
3389           *cant_have_default_ctor_p = 1;
3390           TYPE_HAS_COMPLEX_ASSIGN_REF (t) = 1;
3391
3392           if (! TYPE_HAS_CONSTRUCTOR (t) && extra_warnings)
3393             {
3394               if (DECL_NAME (x))
3395                 cp_warning_at ("non-static reference `%#D' in class without a constructor", x);
3396               else
3397                 cp_warning_at ("non-static reference in class without a constructor", x);
3398             }
3399         }
3400
3401       type = strip_array_types (type);
3402       
3403       if (TREE_CODE (type) == POINTER_TYPE)
3404         has_pointers = 1;
3405
3406       if (DECL_MUTABLE_P (x) || TYPE_HAS_MUTABLE_P (type))
3407         CLASSTYPE_HAS_MUTABLE (t) = 1;
3408
3409       if (! pod_type_p (type)
3410           /* For some reason, pointers to members are POD types themselves,
3411              but are not allowed in POD structs.  Silly.  */
3412           || TYPE_PTRMEM_P (type) || TYPE_PTRMEMFUNC_P (type))
3413         CLASSTYPE_NON_POD_P (t) = 1;
3414
3415       /* If any field is const, the structure type is pseudo-const.  */
3416       if (CP_TYPE_CONST_P (type))
3417         {
3418           C_TYPE_FIELDS_READONLY (t) = 1;
3419           if (DECL_INITIAL (x) == NULL_TREE)
3420             CLASSTYPE_READONLY_FIELDS_NEED_INIT (t) = 1;
3421
3422           /* ARM $12.6.2: [A member initializer list] (or, for an
3423              aggregate, initialization by a brace-enclosed list) is the
3424              only way to initialize nonstatic const and reference
3425              members.  */
3426           *cant_have_default_ctor_p = 1;
3427           TYPE_HAS_COMPLEX_ASSIGN_REF (t) = 1;
3428
3429           if (! TYPE_HAS_CONSTRUCTOR (t) && extra_warnings)
3430             {
3431               if (DECL_NAME (x))
3432                 cp_warning_at ("non-static const member `%#D' in class without a constructor", x);
3433               else
3434                 cp_warning_at ("non-static const member in class without a constructor", x);
3435             }
3436         }
3437       /* A field that is pseudo-const makes the structure likewise.  */
3438       else if (IS_AGGR_TYPE (type))
3439         {
3440           C_TYPE_FIELDS_READONLY (t) |= C_TYPE_FIELDS_READONLY (type);
3441           CLASSTYPE_READONLY_FIELDS_NEED_INIT (t) 
3442             |= CLASSTYPE_READONLY_FIELDS_NEED_INIT (type);
3443         }
3444
3445       /* Core issue 80: A nonstatic data member is required to have a
3446          different name from the class iff the class has a
3447          user-defined constructor.  */
3448       if (DECL_NAME (x) == constructor_name (t)
3449           && TYPE_HAS_CONSTRUCTOR (t))
3450         cp_pedwarn_at ("field `%#D' with same name as class", x);
3451
3452       /* We set DECL_C_BIT_FIELD in grokbitfield.
3453          If the type and width are valid, we'll also set DECL_BIT_FIELD.  */
3454       if (DECL_C_BIT_FIELD (x))
3455         check_bitfield_decl (x);
3456       else
3457         check_field_decl (x, t,
3458                           cant_have_const_ctor_p,
3459                           cant_have_default_ctor_p, 
3460                           no_const_asn_ref_p,
3461                           &any_default_members);
3462     }
3463
3464   /* Effective C++ rule 11.  */
3465   if (has_pointers && warn_ecpp && TYPE_HAS_CONSTRUCTOR (t)
3466       && ! (TYPE_HAS_INIT_REF (t) && TYPE_HAS_ASSIGN_REF (t)))
3467     {
3468       cp_warning ("`%#T' has pointer data members", t);
3469       
3470       if (! TYPE_HAS_INIT_REF (t))
3471         {
3472           cp_warning ("  but does not override `%T(const %T&)'", t, t);
3473           if (! TYPE_HAS_ASSIGN_REF (t))
3474             cp_warning ("  or `operator=(const %T&)'", t);
3475         }
3476       else if (! TYPE_HAS_ASSIGN_REF (t))
3477         cp_warning ("  but does not override `operator=(const %T&)'", t);
3478     }
3479
3480
3481   /* Check anonymous struct/anonymous union fields.  */
3482   finish_struct_anon (t);
3483
3484   /* We've built up the list of access declarations in reverse order.
3485      Fix that now.  */
3486   *access_decls = nreverse (*access_decls);
3487 }
3488
3489 /* Return a FIELD_DECL for a pointer-to-virtual-table or
3490    pointer-to-virtual-base.  The NAME, ASSEMBLER_NAME, and TYPE of the
3491    field are as indicated.  The CLASS_TYPE in which this field occurs
3492    is also indicated.  FCONTEXT is the type that is needed for the debug
3493    info output routines.  *EMPTY_P is set to a non-zero value by this
3494    function to indicate that a class containing this field is
3495    non-empty.  */
3496
3497 static tree
3498 build_vtbl_or_vbase_field (name, assembler_name, type, class_type, fcontext,
3499                            empty_p)
3500      tree name;
3501      tree assembler_name;
3502      tree type;
3503      tree class_type;
3504      tree fcontext;
3505      int *empty_p;
3506 {
3507   tree field;
3508
3509   /* This class is non-empty.  */
3510   *empty_p = 0;
3511
3512   /* Build the FIELD_DECL.  */
3513   field = build_lang_decl (FIELD_DECL, name, type);
3514   DECL_ASSEMBLER_NAME (field) = assembler_name;
3515   DECL_VIRTUAL_P (field) = 1;
3516   DECL_ARTIFICIAL (field) = 1;
3517   DECL_FIELD_CONTEXT (field) = class_type;
3518   DECL_FCONTEXT (field) = fcontext;
3519   DECL_ALIGN (field) = TYPE_ALIGN (type);
3520
3521   /* Return it.  */
3522   return field;
3523 }
3524
3525 /* Record the type of BINFO in the slot in DATA (which is really a
3526    `varray_type *') corresponding to the BINFO_OFFSET.  */
3527
3528 static tree
3529 dfs_record_base_offsets (binfo, data)
3530      tree binfo;
3531      void *data;
3532 {
3533   varray_type *v;
3534   unsigned HOST_WIDE_INT offset = tree_low_cst (BINFO_OFFSET (binfo), 1);
3535
3536   v = (varray_type *) data;
3537   while (VARRAY_SIZE (*v) <= offset)
3538     VARRAY_GROW (*v, 2 * VARRAY_SIZE (*v));
3539   VARRAY_TREE (*v, offset) = tree_cons (NULL_TREE,
3540                                         BINFO_TYPE (binfo),
3541                                         VARRAY_TREE (*v, offset));
3542
3543   return NULL_TREE;
3544 }
3545
3546 /* Add the offset of BINFO and its bases to BASE_OFFSETS.  */
3547
3548 static void
3549 record_base_offsets (binfo, base_offsets)
3550      tree binfo;
3551      varray_type *base_offsets;
3552 {
3553   dfs_walk (binfo,
3554             dfs_record_base_offsets,
3555             dfs_skip_vbases,
3556             base_offsets);
3557 }
3558
3559 /* Returns non-NULL if there is already an entry in DATA (which is
3560    really a `varray_type') indicating that an object with the same
3561    type of BINFO is already at the BINFO_OFFSET for BINFO.  */
3562
3563 static tree
3564 dfs_search_base_offsets (binfo, data)
3565      tree binfo;
3566      void *data;
3567 {
3568   if (is_empty_class (BINFO_TYPE (binfo)))
3569     {
3570       varray_type v = (varray_type) data;
3571       /* Find the offset for this BINFO.  */
3572       unsigned HOST_WIDE_INT offset = tree_low_cst (BINFO_OFFSET (binfo), 1);
3573       tree t;
3574
3575       /* If we haven't yet encountered any objects at offsets that
3576          big, then there's no conflict.  */
3577       if (VARRAY_SIZE (v) <= offset)
3578         return NULL_TREE;
3579       /* Otherwise, go through the objects already allocated at this
3580          offset.  */
3581       for (t = VARRAY_TREE (v, offset); t; t = TREE_CHAIN (t))
3582         if (same_type_p (TREE_VALUE (t), BINFO_TYPE (binfo)))
3583           return binfo;
3584     }
3585
3586   return NULL_TREE;
3587 }
3588
3589 /* Returns non-zero if there's a conflict between BINFO and a base
3590    already mentioned in BASE_OFFSETS if BINFO is placed at its current
3591    BINFO_OFFSET.  */
3592
3593 static int
3594 layout_conflict_p (binfo, base_offsets)
3595      tree binfo;
3596      varray_type base_offsets;
3597 {
3598   return dfs_walk (binfo, dfs_search_base_offsets, dfs_skip_vbases,
3599                    base_offsets) != NULL_TREE;
3600 }
3601
3602 /* DECL is a FIELD_DECL corresponding either to a base subobject of a
3603    non-static data member of the type indicated by RLI.  BINFO is the
3604    binfo corresponding to the base subobject, or, if this is a
3605    non-static data-member, a dummy BINFO for the type of the data
3606    member.  BINFO may be NULL if checks to see if the field overlaps
3607    an existing field with the same type are not required.  V maps
3608    offsets to types already located at those offsets.  This function
3609    determines the position of the DECL.  */
3610
3611 static void
3612 layout_nonempty_base_or_field (rli, decl, binfo, v)
3613      record_layout_info rli;
3614      tree decl;
3615      tree binfo;
3616      varray_type v;
3617 {
3618   /* Try to place the field.  It may take more than one try if we have
3619      a hard time placing the field without putting two objects of the
3620      same type at the same address.  */
3621   while (1)
3622     {
3623       tree offset;
3624       struct record_layout_info old_rli = *rli;
3625
3626       /* Place this field.  */
3627       place_field (rli, decl);
3628       
3629       /* Now that we know where it wil be placed, update its
3630          BINFO_OFFSET.  */
3631       offset = byte_position (decl);
3632       if (binfo)
3633         propagate_binfo_offsets (binfo, 
3634                                  convert (ssizetype, offset));
3635  
3636       /* We have to check to see whether or not there is already
3637          something of the same type at the offset we're about to use.
3638          For example:
3639          
3640          struct S {};
3641          struct T : public S { int i; };
3642          struct U : public S, public T {};
3643          
3644          Here, we put S at offset zero in U.  Then, we can't put T at
3645          offset zero -- its S component would be at the same address
3646          as the S we already allocated.  So, we have to skip ahead.
3647          Since all data members, including those whose type is an
3648          empty class, have non-zero size, any overlap can happen only
3649          with a direct or indirect base-class -- it can't happen with
3650          a data member.  */
3651       if (binfo && flag_new_abi && layout_conflict_p (binfo, v))
3652         {
3653           /* Undo the propogate_binfo_offsets call.  */
3654           offset = size_diffop (size_zero_node, offset);
3655           propagate_binfo_offsets (binfo, convert (ssizetype, offset));
3656          
3657           /* Strip off the size allocated to this field.  That puts us
3658              at the first place we could have put the field with
3659              proper alignment.  */
3660           *rli = old_rli;
3661
3662           /* Bump up by the alignment required for the type, without
3663              virtual base classes.  */
3664           rli->bitpos
3665             = size_binop (PLUS_EXPR, rli->bitpos,
3666                           bitsize_int (CLASSTYPE_ALIGN (BINFO_TYPE (binfo))));
3667           normalize_rli (rli);
3668         }
3669       else
3670         /* There was no conflict.  We're done laying out this field.  */
3671         break;
3672     }
3673 }
3674
3675 /* Layout the empty base BINFO.  EOC indicates the byte currently just
3676    past the end of the class, and should be correctly aligned for a
3677    class of the type indicated by BINFO; BINFO_OFFSETS gives the
3678    offsets of the other bases allocated so far.  */
3679
3680 static void
3681 layout_empty_base (binfo, eoc, binfo_offsets)
3682      tree binfo;
3683      tree eoc;
3684      varray_type binfo_offsets;
3685 {
3686   tree alignment;
3687   tree basetype = BINFO_TYPE (binfo);
3688   
3689   /* This routine should only be used for empty classes.  */
3690   my_friendly_assert (is_empty_class (basetype), 20000321);
3691   alignment = ssize_int (CLASSTYPE_ALIGN (basetype));
3692
3693   /* This is an empty base class.  We first try to put it at offset
3694      zero.  */
3695   if (layout_conflict_p (binfo, binfo_offsets))
3696     {
3697       /* That didn't work.  Now, we move forward from the next
3698          available spot in the class.  */
3699       propagate_binfo_offsets (binfo, convert (ssizetype, eoc));
3700       while (1) 
3701         {
3702           if (!layout_conflict_p (binfo, binfo_offsets))
3703             /* We finally found a spot where there's no overlap.  */
3704             break;
3705
3706           /* There's overlap here, too.  Bump along to the next spot.  */
3707           propagate_binfo_offsets (binfo, alignment);
3708         }
3709     }
3710 }
3711
3712 /* Build a FIELD_DECL for the base given by BINFO in the class
3713    indicated by RLI.  If the new object is non-empty, clear *EMPTY_P.
3714    *BASE_ALIGN is a running maximum of the alignments of any base
3715    class.  */
3716
3717 static void
3718 build_base_field (rli, binfo, empty_p, base_align, v)
3719      record_layout_info rli;
3720      tree binfo;
3721      int *empty_p;
3722      unsigned int *base_align;
3723      varray_type *v;
3724 {
3725   tree basetype = BINFO_TYPE (binfo);
3726   tree decl;
3727
3728   if (!COMPLETE_TYPE_P (basetype))
3729     /* This error is now reported in xref_tag, thus giving better
3730        location information.  */
3731     return;
3732   
3733   decl = build_lang_decl (FIELD_DECL, NULL_TREE, basetype);
3734   DECL_ARTIFICIAL (decl) = 1;
3735   DECL_FIELD_CONTEXT (decl) = rli->t;
3736   DECL_SIZE (decl) = CLASSTYPE_SIZE (basetype);
3737   DECL_SIZE_UNIT (decl) = CLASSTYPE_SIZE_UNIT (basetype);
3738   DECL_ALIGN (decl) = CLASSTYPE_ALIGN (basetype);
3739   
3740   if (! flag_new_abi)
3741     {
3742       /* Brain damage for backwards compatibility.  For no good
3743          reason, the old basetype layout made every base have at least
3744          as large as the alignment for the bases up to that point,
3745          gratuitously wasting space.  So we do the same thing here.  */
3746       *base_align = MAX (*base_align, DECL_ALIGN (decl));
3747       DECL_SIZE (decl)
3748         = size_binop (MAX_EXPR, DECL_SIZE (decl), bitsize_int (*base_align));
3749       DECL_SIZE_UNIT (decl)
3750         = size_binop (MAX_EXPR, DECL_SIZE_UNIT (decl),
3751                       size_int (*base_align / BITS_PER_UNIT));
3752     }
3753
3754   if (!integer_zerop (DECL_SIZE (decl)))
3755     {
3756       /* The containing class is non-empty because it has a non-empty
3757          base class.  */
3758       *empty_p = 0;
3759
3760       /* Try to place the field.  It may take more than one try if we
3761          have a hard time placing the field without putting two
3762          objects of the same type at the same address.  */
3763       layout_nonempty_base_or_field (rli, decl, binfo, *v);
3764     }
3765   else
3766     {
3767       unsigned HOST_WIDE_INT eoc;
3768
3769       /* On some platforms (ARM), even empty classes will not be
3770          byte-aligned.  */
3771       eoc = tree_low_cst (rli_size_unit_so_far (rli), 0);
3772       eoc = CEIL (eoc, DECL_ALIGN (decl)) * DECL_ALIGN (decl);
3773       layout_empty_base (binfo, size_int (eoc), *v);
3774     }
3775
3776   /* Check for inaccessible base classes.  If the same base class
3777      appears more than once in the hierarchy, but isn't virtual, then
3778      it's ambiguous.  */
3779   if (get_base_distance (basetype, rli->t, 0, NULL) == -2)
3780     cp_warning ("direct base `%T' inaccessible in `%T' due to ambiguity",
3781                 basetype, rli->t);
3782   
3783   /* Record the offsets of BINFO and its base subobjects.  */
3784   record_base_offsets (binfo, v);
3785 }
3786
3787 /* Layout all of the non-virtual base classes.  Returns a map from
3788    offsets to types present at those offsets.  */
3789
3790 static varray_type
3791 build_base_fields (rli, empty_p)
3792      record_layout_info rli;
3793      int *empty_p;
3794 {
3795   /* Chain to hold all the new FIELD_DECLs which stand in for base class
3796      subobjects.  */
3797   tree rec = rli->t;
3798   int n_baseclasses = CLASSTYPE_N_BASECLASSES (rec);
3799   int i;
3800   varray_type v;
3801   unsigned int base_align = 0;
3802
3803   /* Create the table mapping offsets to empty base classes.  */
3804   VARRAY_TREE_INIT (v, 32, "v");
3805
3806   /* Under the new ABI, the primary base class is always allocated
3807      first.  */
3808   if (flag_new_abi && CLASSTYPE_HAS_PRIMARY_BASE_P (rec))
3809     build_base_field (rli, CLASSTYPE_PRIMARY_BINFO (rec), 
3810                       empty_p, &base_align, &v);
3811
3812   /* Now allocate the rest of the bases.  */
3813   for (i = 0; i < n_baseclasses; ++i)
3814     {
3815       tree base_binfo;
3816
3817       /* Under the new ABI, the primary base was already allocated
3818          above, so we don't need to allocate it again here.  */
3819       if (flag_new_abi && i == CLASSTYPE_VFIELD_PARENT (rec))
3820         continue;
3821
3822       base_binfo = BINFO_BASETYPE (TYPE_BINFO (rec), i);
3823
3824       /* A primary virtual base class is allocated just like any other
3825          base class, but a non-primary virtual base is allocated
3826          later, in layout_virtual_bases.  */
3827       if (TREE_VIA_VIRTUAL (base_binfo) 
3828           && !BINFO_PRIMARY_MARKED_P (base_binfo))
3829         continue;
3830
3831       build_base_field (rli, base_binfo, empty_p, &base_align, &v);
3832     }
3833
3834   return v;
3835 }
3836
3837 /* Go through the TYPE_METHODS of T issuing any appropriate
3838    diagnostics, figuring out which methods override which other
3839    methods, and so forth.  */
3840
3841 static void
3842 check_methods (t)
3843      tree t;
3844 {
3845   tree x;
3846   int seen_one_arg_array_delete_p = 0;
3847
3848   for (x = TYPE_METHODS (t); x; x = TREE_CHAIN (x))
3849     {
3850       GNU_xref_member (current_class_name, x);
3851
3852       /* If this was an evil function, don't keep it in class.  */
3853       if (IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (x)))
3854         continue;
3855
3856       check_for_override (x, t);
3857       if (DECL_PURE_VIRTUAL_P (x) && ! DECL_VINDEX (x))
3858         cp_error_at ("initializer specified for non-virtual method `%D'", x);
3859
3860       /* The name of the field is the original field name
3861          Save this in auxiliary field for later overloading.  */
3862       if (DECL_VINDEX (x))
3863         {
3864           TYPE_POLYMORPHIC_P (t) = 1;
3865           if (DECL_PURE_VIRTUAL_P (x))
3866             CLASSTYPE_PURE_VIRTUALS (t)
3867               = tree_cons (NULL_TREE, x, CLASSTYPE_PURE_VIRTUALS (t));
3868         }
3869
3870       if (DECL_ARRAY_DELETE_OPERATOR_P (x))
3871         {
3872           tree second_parm;
3873
3874           /* When dynamically allocating an array of this type, we
3875              need a "cookie" to record how many elements we allocated,
3876              even if the array elements have no non-trivial
3877              destructor, if the usual array deallocation function
3878              takes a second argument of type size_t.  The standard (in
3879              [class.free]) requires that the second argument be set
3880              correctly.  */
3881           second_parm = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (x)));
3882           /* This is overly conservative, but we must maintain this
3883              behavior for backwards compatibility.  */
3884           if (!flag_new_abi && second_parm != void_list_node)
3885             TYPE_VEC_DELETE_TAKES_SIZE (t) = 1;
3886           /* Under the new ABI, we choose only those function that are
3887              explicitly declared as `operator delete[] (void *,
3888              size_t)'.  */
3889           else if (flag_new_abi 
3890                    && !seen_one_arg_array_delete_p
3891                    && second_parm
3892                    && TREE_CHAIN (second_parm) == void_list_node
3893                    && same_type_p (TREE_VALUE (second_parm), sizetype))
3894             TYPE_VEC_DELETE_TAKES_SIZE (t) = 1;
3895           /* If there's no second parameter, then this is the usual
3896              deallocation function.  */
3897           else if (second_parm == void_list_node)
3898             seen_one_arg_array_delete_p = 1;
3899         }
3900     }
3901 }
3902
3903 /* FN is a constructor or destructor.  Clone the declaration to create
3904    a specialized in-charge or not-in-charge version, as indicated by
3905    NAME.  */
3906
3907 static tree
3908 build_clone (fn, name)
3909      tree fn;
3910      tree name;
3911 {
3912   tree parms;
3913   tree clone;
3914
3915   /* Copy the function.  */
3916   clone = copy_decl (fn);
3917   /* Remember where this function came from.  */
3918   DECL_CLONED_FUNCTION (clone) = fn;
3919   /* Reset the function name.  */
3920   DECL_NAME (clone) = name;
3921   DECL_ASSEMBLER_NAME (clone) = DECL_NAME (clone);
3922   /* There's no pending inline data for this function.  */
3923   DECL_PENDING_INLINE_INFO (clone) = NULL;
3924   DECL_PENDING_INLINE_P (clone) = 0;
3925   /* And it hasn't yet been deferred.  */
3926   DECL_DEFERRED_FN (clone) = 0;
3927
3928   /* The base-class destructor is not virtual.  */
3929   if (name == base_dtor_identifier)
3930     {
3931       DECL_VIRTUAL_P (clone) = 0;
3932       if (TREE_CODE (clone) != TEMPLATE_DECL)
3933         DECL_VINDEX (clone) = NULL_TREE;
3934     }
3935
3936   /* If there was an in-charge parameter, drop it from the function
3937      type.  */
3938   if (DECL_HAS_IN_CHARGE_PARM_P (clone))
3939     {
3940       tree basetype;
3941       tree parmtypes;
3942       tree exceptions;
3943
3944       exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
3945       basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
3946       parmtypes = TYPE_ARG_TYPES (TREE_TYPE (clone));
3947       /* Skip the `this' parameter.  */
3948       parmtypes = TREE_CHAIN (parmtypes);
3949       /* Skip the in-charge parameter.  */
3950       parmtypes = TREE_CHAIN (parmtypes);
3951       TREE_TYPE (clone) 
3952         = build_cplus_method_type (basetype,
3953                                    TREE_TYPE (TREE_TYPE (clone)),
3954                                    parmtypes);
3955       if (exceptions)
3956         TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone),
3957                                                      exceptions);
3958     }
3959
3960   /* Copy the function parameters.  But, DECL_ARGUMENTS aren't
3961      function parameters; instead, those are the template parameters.  */
3962   if (TREE_CODE (clone) != TEMPLATE_DECL)
3963     {
3964       DECL_ARGUMENTS (clone) = copy_list (DECL_ARGUMENTS (clone));
3965       /* Remove the in-charge parameter.  */
3966       if (DECL_HAS_IN_CHARGE_PARM_P (clone))
3967         {
3968           TREE_CHAIN (DECL_ARGUMENTS (clone))
3969             = TREE_CHAIN (TREE_CHAIN (DECL_ARGUMENTS (clone)));
3970           DECL_HAS_IN_CHARGE_PARM_P (clone) = 0;
3971         }
3972       for (parms = DECL_ARGUMENTS (clone); parms; parms = TREE_CHAIN (parms))
3973         {
3974           DECL_CONTEXT (parms) = clone;
3975           copy_lang_decl (parms);
3976         }
3977     }
3978
3979   /* Mangle the function name.  */
3980   set_mangled_name_for_decl (clone);
3981
3982   /* Create the RTL for this function.  */
3983   DECL_RTL (clone) = NULL_RTX;
3984   rest_of_decl_compilation (clone, NULL, /*top_level=*/1, at_eof);
3985   
3986   /* Make it easy to find the CLONE given the FN.  */
3987   TREE_CHAIN (clone) = TREE_CHAIN (fn);
3988   TREE_CHAIN (fn) = clone;
3989
3990   /* If this is a template, handle the DECL_TEMPLATE_RESULT as well.  */
3991   if (TREE_CODE (clone) == TEMPLATE_DECL)
3992     {
3993       tree result;
3994
3995       DECL_TEMPLATE_RESULT (clone) 
3996         = build_clone (DECL_TEMPLATE_RESULT (clone), name);
3997       result = DECL_TEMPLATE_RESULT (clone);
3998       DECL_TEMPLATE_INFO (result) = copy_node (DECL_TEMPLATE_INFO (result));
3999       DECL_TI_TEMPLATE (result) = clone;
4000     }
4001   else if (DECL_DEFERRED_FN (fn))
4002     defer_fn (clone);
4003
4004   return clone;
4005 }
4006
4007 /* Produce declarations for all appropriate clones of FN.  If
4008    UPDATE_METHOD_VEC_P is non-zero, the clones are added to the
4009    CLASTYPE_METHOD_VEC as well.  */
4010
4011 void
4012 clone_function_decl (fn, update_method_vec_p)
4013      tree fn;
4014      int update_method_vec_p;
4015 {
4016   tree clone;
4017
4018   if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn))
4019     {
4020       /* For each constructor, we need two variants: an in-charge version
4021          and a not-in-charge version.  */
4022       clone = build_clone (fn, complete_ctor_identifier);
4023       if (update_method_vec_p)
4024         add_method (DECL_CONTEXT (clone), NULL, clone);
4025       clone = build_clone (fn, base_ctor_identifier);
4026       if (update_method_vec_p)
4027         add_method (DECL_CONTEXT (clone), NULL, clone);
4028     }
4029   else
4030     {
4031       my_friendly_assert (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn), 20000411);
4032
4033       /* For each destructor, we need two variants: an in-charge
4034          version, a not-in-charge version, and an in-charge deleting
4035          version.  We clone the deleting version first because that
4036          means it will go second on the TYPE_METHODS list -- and that
4037          corresponds to the correct layout order in the virtual
4038          function table.  */
4039       clone = build_clone (fn, deleting_dtor_identifier);
4040       if (update_method_vec_p)
4041         add_method (DECL_CONTEXT (clone), NULL, clone);
4042       clone = build_clone (fn, complete_dtor_identifier);
4043       if (update_method_vec_p)
4044         add_method (DECL_CONTEXT (clone), NULL, clone);
4045       clone = build_clone (fn, base_dtor_identifier);
4046       if (update_method_vec_p)
4047         add_method (DECL_CONTEXT (clone), NULL, clone);
4048     }
4049 }
4050
4051 /* For each of the constructors and destructors in T, create an
4052    in-charge and not-in-charge variant.  */
4053
4054 static void
4055 clone_constructors_and_destructors (t)
4056      tree t;
4057 {
4058   tree fns;
4059
4060   /* We only clone constructors and destructors under the new ABI.  */
4061   if (!flag_new_abi)
4062     return;
4063
4064   /* If for some reason we don't have a CLASSTYPE_METHOD_VEC, we bail
4065      out now.  */
4066   if (!CLASSTYPE_METHOD_VEC (t))
4067     return;
4068
4069   for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4070     clone_function_decl (OVL_CURRENT (fns), /*update_method_vec_p=*/1);
4071   for (fns = CLASSTYPE_DESTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4072     clone_function_decl (OVL_CURRENT (fns), /*update_method_vec_p=*/1);
4073 }
4074
4075 /* Remove all zero-width bit-fields from T.  */
4076
4077 static void
4078 remove_zero_width_bit_fields (t)
4079      tree t;
4080 {
4081   tree *fieldsp;
4082
4083   fieldsp = &TYPE_FIELDS (t); 
4084   while (*fieldsp)
4085     {
4086       if (TREE_CODE (*fieldsp) == FIELD_DECL
4087           && DECL_C_BIT_FIELD (*fieldsp) 
4088           && DECL_INITIAL (*fieldsp))
4089         *fieldsp = TREE_CHAIN (*fieldsp);
4090       else
4091         fieldsp = &TREE_CHAIN (*fieldsp);
4092     }
4093 }
4094
4095 /* Check the validity of the bases and members declared in T.  Add any
4096    implicitly-generated functions (like copy-constructors and
4097    assignment operators).  Compute various flag bits (like
4098    CLASSTYPE_NON_POD_T) for T.  This routine works purely at the C++
4099    level: i.e., independently of the ABI in use.  */
4100
4101 static void
4102 check_bases_and_members (t, empty_p)
4103      tree t;
4104      int *empty_p;
4105 {
4106   /* Nonzero if we are not allowed to generate a default constructor
4107      for this case.  */
4108   int cant_have_default_ctor;
4109   /* Nonzero if the implicitly generated copy constructor should take
4110      a non-const reference argument.  */
4111   int cant_have_const_ctor;
4112   /* Nonzero if the the implicitly generated assignment operator
4113      should take a non-const reference argument.  */
4114   int no_const_asn_ref;
4115   tree access_decls;
4116
4117   /* By default, we use const reference arguments and generate default
4118      constructors.  */
4119   cant_have_default_ctor = 0;
4120   cant_have_const_ctor = 0;
4121   no_const_asn_ref = 0;
4122
4123   /* Assume that the class is nearly empty; we'll clear this flag if
4124      it turns out not to be nearly empty.  */
4125   CLASSTYPE_NEARLY_EMPTY_P (t) = 1;
4126
4127   /* Check all the base-classes. */
4128   check_bases (t, &cant_have_default_ctor, &cant_have_const_ctor,
4129                &no_const_asn_ref);
4130
4131   /* Check all the data member declarations.  */
4132   check_field_decls (t, &access_decls, empty_p,
4133                      &cant_have_default_ctor,
4134                      &cant_have_const_ctor,
4135                      &no_const_asn_ref);
4136
4137   /* Check all the method declarations.  */
4138   check_methods (t);
4139
4140   /* A nearly-empty class has to be vptr-containing; a nearly empty
4141      class contains just a vptr.  */
4142   if (!TYPE_CONTAINS_VPTR_P (t))
4143     CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4144
4145   /* Do some bookkeeping that will guide the generation of implicitly
4146      declared member functions.  */
4147   TYPE_HAS_COMPLEX_INIT_REF (t)
4148     |= (TYPE_HAS_INIT_REF (t) 
4149         || TYPE_USES_VIRTUAL_BASECLASSES (t)
4150         || TYPE_POLYMORPHIC_P (t));
4151   TYPE_NEEDS_CONSTRUCTING (t)
4152     |= (TYPE_HAS_CONSTRUCTOR (t) 
4153         || TYPE_USES_VIRTUAL_BASECLASSES (t)
4154         || TYPE_POLYMORPHIC_P (t));
4155   CLASSTYPE_NON_AGGREGATE (t) |= (TYPE_HAS_CONSTRUCTOR (t)
4156                                   || TYPE_POLYMORPHIC_P (t));
4157   CLASSTYPE_NON_POD_P (t)
4158     |= (CLASSTYPE_NON_AGGREGATE (t) || TYPE_HAS_DESTRUCTOR (t) 
4159         || TYPE_HAS_ASSIGN_REF (t));
4160   TYPE_HAS_REAL_ASSIGN_REF (t) |= TYPE_HAS_ASSIGN_REF (t);
4161   TYPE_HAS_COMPLEX_ASSIGN_REF (t)
4162     |= TYPE_HAS_ASSIGN_REF (t) || TYPE_USES_VIRTUAL_BASECLASSES (t);
4163
4164   /* Synthesize any needed methods.  Note that methods will be synthesized
4165      for anonymous unions; grok_x_components undoes that.  */
4166   add_implicitly_declared_members (t, cant_have_default_ctor,
4167                                    cant_have_const_ctor,
4168                                    no_const_asn_ref);
4169
4170   /* Create the in-charge and not-in-charge variants of constructors
4171      and destructors.  */
4172   clone_constructors_and_destructors (t);
4173
4174   /* Process the using-declarations.  */
4175   for (; access_decls; access_decls = TREE_CHAIN (access_decls))
4176     handle_using_decl (TREE_VALUE (access_decls), t);
4177
4178   /* Build and sort the CLASSTYPE_METHOD_VEC.  */
4179   finish_struct_methods (t);
4180 }
4181
4182 /* If T needs a pointer to its virtual function table, set TYPE_VFIELD
4183    accordingly.  If a new vfield was created (because T doesn't have a
4184    primary base class), then the newly created field is returned.  It
4185    is not added to the TYPE_FIELDS list; it is the caller's
4186    responsibility to do that.  */
4187
4188 static tree
4189 create_vtable_ptr (t, empty_p, vfuns_p,
4190                    new_virtuals_p, overridden_virtuals_p)
4191      tree t;
4192      int *empty_p;
4193      int *vfuns_p;
4194      tree *new_virtuals_p;
4195      tree *overridden_virtuals_p;
4196 {
4197   tree fn;
4198
4199   /* Loop over the virtual functions, adding them to our various
4200      vtables.  */
4201   for (fn = TYPE_METHODS (t); fn; fn = TREE_CHAIN (fn))
4202     if (DECL_VINDEX (fn) 
4203         && !(flag_new_abi && DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn)))
4204       add_virtual_function (new_virtuals_p, overridden_virtuals_p,
4205                             vfuns_p, fn, t);
4206
4207   /* If we couldn't find an appropriate base class, create a new field
4208      here.  Even if there weren't any new virtual functions, we might need a
4209      new virtual function table if we're supposed to include vptrs in
4210      all classes that need them.  */
4211   if (!TYPE_VFIELD (t)
4212       && (*vfuns_p 
4213           || (TYPE_CONTAINS_VPTR_P (t) && vptrs_present_everywhere_p ())))
4214     {
4215       /* We build this decl with vtbl_ptr_type_node, which is a
4216          `vtable_entry_type*'.  It might seem more precise to use
4217          `vtable_entry_type (*)[N]' where N is the number of firtual
4218          functions.  However, that would require the vtable pointer in
4219          base classes to have a different type than the vtable pointer
4220          in derived classes.  We could make that happen, but that
4221          still wouldn't solve all the problems.  In particular, the
4222          type-based alias analysis code would decide that assignments
4223          to the base class vtable pointer can't alias assignments to
4224          the derived class vtable pointer, since they have different
4225          types.  Thus, in an derived class destructor, where the base
4226          class constructor was inlined, we could generate bad code for
4227          setting up the vtable pointer.  
4228
4229          Therefore, we use one type for all vtable pointers.  We still
4230          use a type-correct type; it's just doesn't indicate the array
4231          bounds.  That's better than using `void*' or some such; it's
4232          cleaner, and it let's the alias analysis code know that these
4233          stores cannot alias stores to void*!  */
4234       TYPE_VFIELD (t) 
4235         = build_vtbl_or_vbase_field (get_vfield_name (t),
4236                                      get_identifier (VFIELD_BASE),
4237                                      vtbl_ptr_type_node,
4238                                      t,
4239                                      t,
4240                                      empty_p);
4241
4242       if (flag_new_abi && CLASSTYPE_N_BASECLASSES (t))
4243         /* If there were any baseclasses, they can't possibly be at
4244            offset zero any more, because that's where the vtable
4245            pointer is.  So, converting to a base class is going to
4246            take work.  */
4247         TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (t) = 1;
4248
4249       return TYPE_VFIELD (t);
4250     }
4251
4252   return NULL_TREE;
4253 }
4254
4255 /* Fixup the inline function given by INFO now that the class is
4256    complete.  */
4257
4258 static void
4259 fixup_pending_inline (info)
4260      struct pending_inline *info;
4261 {
4262   if (info)
4263     {
4264       tree args;
4265       tree fn = info->fndecl;
4266
4267       args = DECL_ARGUMENTS (fn);
4268       while (args)
4269         {
4270           DECL_CONTEXT (args) = fn;
4271           args = TREE_CHAIN (args);
4272         }
4273     }
4274 }
4275
4276 /* Fixup the inline methods and friends in TYPE now that TYPE is
4277    complete.  */
4278
4279 static void
4280 fixup_inline_methods (type)
4281      tree type;
4282 {
4283   tree method = TYPE_METHODS (type);
4284
4285   if (method && TREE_CODE (method) == TREE_VEC)
4286     {
4287       if (TREE_VEC_ELT (method, 1))
4288         method = TREE_VEC_ELT (method, 1);
4289       else if (TREE_VEC_ELT (method, 0))
4290         method = TREE_VEC_ELT (method, 0);
4291       else
4292         method = TREE_VEC_ELT (method, 2);
4293     }
4294
4295   /* Do inline member functions.  */
4296   for (; method; method = TREE_CHAIN (method))
4297     fixup_pending_inline (DECL_PENDING_INLINE_INFO (method));
4298
4299   /* Do friends.  */
4300   for (method = CLASSTYPE_INLINE_FRIENDS (type); 
4301        method; 
4302        method = TREE_CHAIN (method))
4303     fixup_pending_inline (DECL_PENDING_INLINE_INFO (TREE_VALUE (method)));
4304   CLASSTYPE_INLINE_FRIENDS (type) = NULL_TREE;
4305 }
4306
4307 /* Called from propagate_binfo_offsets via dfs_walk.  */
4308
4309 static tree
4310 dfs_propagate_binfo_offsets (binfo, data)
4311      tree binfo; 
4312      void *data;
4313 {
4314   tree offset = (tree) data;
4315
4316   /* Update the BINFO_OFFSET for this base.  Allow for the case where it
4317      might be negative.  */
4318   BINFO_OFFSET (binfo)
4319     = convert (sizetype, size_binop (PLUS_EXPR,
4320                                      convert (ssizetype, BINFO_OFFSET (binfo)),
4321                                               offset));
4322   SET_BINFO_MARKED (binfo);
4323
4324   return NULL_TREE;
4325 }
4326
4327 /* Add OFFSET to all base types of BINFO which is a base in the
4328    hierarchy dominated by T.
4329
4330    OFFSET, which is a type offset, is number of bytes.
4331
4332    Note that we don't have to worry about having two paths to the
4333    same base type, since this type owns its association list.  */
4334
4335 static void
4336 propagate_binfo_offsets (binfo, offset)
4337      tree binfo;
4338      tree offset;
4339 {
4340   dfs_walk (binfo, 
4341             dfs_propagate_binfo_offsets, 
4342             dfs_skip_nonprimary_vbases_unmarkedp,
4343             offset);
4344   dfs_walk (binfo,
4345             dfs_unmark,
4346             dfs_skip_nonprimary_vbases_markedp,
4347             NULL);
4348 }
4349
4350 /* Called via dfs_walk from layout_virtual bases.  */
4351
4352 static tree
4353 dfs_set_offset_for_shared_vbases (binfo, data)
4354      tree binfo;
4355      void *data;
4356 {
4357   if (TREE_VIA_VIRTUAL (binfo) && BINFO_PRIMARY_MARKED_P (binfo))
4358     {
4359       /* Update the shared copy.  */
4360       tree shared_binfo;
4361
4362       shared_binfo = BINFO_FOR_VBASE (BINFO_TYPE (binfo), (tree) data);
4363       BINFO_OFFSET (shared_binfo) = BINFO_OFFSET (binfo);
4364     }
4365
4366   return NULL_TREE;
4367 }
4368
4369 /* Called via dfs_walk from layout_virtual bases.  */
4370
4371 static tree
4372 dfs_set_offset_for_unshared_vbases (binfo, data)
4373      tree binfo;
4374      void *data;
4375 {
4376   /* If this is a virtual base, make sure it has the same offset as
4377      the shared copy.  If it's a primary base, then we know it's
4378      correct.  */
4379   if (TREE_VIA_VIRTUAL (binfo) && !BINFO_PRIMARY_MARKED_P (binfo))
4380     {
4381       tree t = (tree) data;
4382       tree vbase;
4383       tree offset;
4384       
4385       vbase = BINFO_FOR_VBASE (BINFO_TYPE (binfo), t);
4386       offset = size_diffop (BINFO_OFFSET (vbase), BINFO_OFFSET (binfo));
4387       propagate_binfo_offsets (binfo, offset);
4388     }
4389
4390   return NULL_TREE;
4391 }
4392
4393 /* Set BINFO_OFFSET for all of the virtual bases for T.  Update
4394    TYPE_ALIGN and TYPE_SIZE for T.  BASE_OFFSETS is a varray mapping
4395    offsets to the types at those offsets.  */
4396
4397 static void
4398 layout_virtual_bases (t, base_offsets)
4399      tree t;
4400      varray_type *base_offsets;
4401 {
4402   tree vbases;
4403   unsigned HOST_WIDE_INT dsize;
4404   unsigned HOST_WIDE_INT eoc;
4405
4406   if (CLASSTYPE_N_BASECLASSES (t) == 0)
4407     return;
4408
4409 #ifdef STRUCTURE_SIZE_BOUNDARY
4410   /* Packed structures don't need to have minimum size.  */
4411   if (! TYPE_PACKED (t))
4412     TYPE_ALIGN (t) = MAX (TYPE_ALIGN (t), STRUCTURE_SIZE_BOUNDARY);
4413 #endif
4414
4415   /* DSIZE is the size of the class without the virtual bases.  */
4416   dsize = tree_low_cst (TYPE_SIZE (t), 1);
4417
4418   /* Make every class have alignment of at least one.  */
4419   TYPE_ALIGN (t) = MAX (TYPE_ALIGN (t), BITS_PER_UNIT);
4420
4421   /* Go through the virtual bases, allocating space for each virtual
4422      base that is not already a primary base class.  Under the new
4423      ABI, these are allocated according to a depth-first left-to-right
4424      postorder traversal; in the new ABI, inheritance graph order is
4425      used instead.  */
4426   for (vbases = (flag_new_abi 
4427                  ? TYPE_BINFO (t) 
4428                  : CLASSTYPE_VBASECLASSES (t));
4429        vbases; 
4430        vbases = TREE_CHAIN (vbases))
4431     {
4432       tree vbase;
4433
4434       if (!TREE_VIA_VIRTUAL (vbases))
4435         continue;
4436
4437       if (flag_new_abi)
4438         vbase = BINFO_FOR_VBASE (BINFO_TYPE (vbases), t);
4439       else
4440         vbase = vbases;
4441
4442       if (!BINFO_VBASE_PRIMARY_P (vbase))
4443         {
4444           /* This virtual base is not a primary base of any class in the
4445              hierarchy, so we have to add space for it.  */
4446           tree basetype;
4447           unsigned int desired_align;
4448
4449           basetype = BINFO_TYPE (vbase);
4450
4451           if (flag_new_abi)
4452             desired_align = CLASSTYPE_ALIGN (basetype);
4453           else
4454             /* Under the old ABI, virtual bases were aligned as for the
4455              entire base object (including its virtual bases).  That's
4456              wasteful, in general.  */
4457             desired_align = TYPE_ALIGN (basetype);
4458           TYPE_ALIGN (t) = MAX (TYPE_ALIGN (t), desired_align);
4459
4460           /* Add padding so that we can put the virtual base class at an
4461              appropriately aligned offset.  */
4462           dsize = CEIL (dsize, desired_align) * desired_align;
4463
4464           /* Under the new ABI, we try to squish empty virtual bases in
4465              just like ordinary empty bases.  */
4466           if (flag_new_abi && is_empty_class (basetype))
4467             layout_empty_base (vbase,
4468                                size_int (CEIL (dsize, BITS_PER_UNIT)),
4469                                *base_offsets);
4470           else
4471             {
4472               /* And compute the offset of the virtual base.  */
4473               propagate_binfo_offsets (vbase, 
4474                                        ssize_int (CEIL (dsize, BITS_PER_UNIT)));
4475               /* Every virtual baseclass takes a least a UNIT, so that
4476                  we can take it's address and get something different
4477                  for each base.  */
4478               dsize += MAX (BITS_PER_UNIT,
4479                             tree_low_cst (CLASSTYPE_SIZE (basetype), 0));
4480             }
4481
4482           /* Keep track of the offsets assigned to this virtual base.  */
4483           record_base_offsets (vbase, base_offsets);
4484         }
4485     }
4486
4487   /* Make sure that all of the CLASSTYPE_VBASECLASSES have their
4488      BINFO_OFFSET set correctly.  Those we just allocated certainly
4489      will.  The others are primary baseclasses; we walk the hierarchy
4490      to find the primary copies and update the shared copy.  */
4491   dfs_walk (TYPE_BINFO (t), 
4492             dfs_set_offset_for_shared_vbases, 
4493             dfs_unmarked_real_bases_queue_p,
4494             t);
4495
4496   /* Now, go through the TYPE_BINFO hierarchy again, setting the
4497      BINFO_OFFSETs correctly for all non-primary copies of the virtual
4498      bases and their direct and indirect bases.  The ambiguity checks
4499      in get_base_distance depend on the BINFO_OFFSETs being set
4500      correctly.  */
4501   dfs_walk (TYPE_BINFO (t), dfs_set_offset_for_unshared_vbases, NULL, t);
4502   for (vbases = CLASSTYPE_VBASECLASSES (t);
4503        vbases;
4504        vbases = TREE_CHAIN (vbases))
4505     dfs_walk (vbases, dfs_set_offset_for_unshared_vbases, NULL, t);
4506
4507   /* If we had empty base classes that protruded beyond the end of the
4508      class, we didn't update DSIZE above; we were hoping to overlay
4509      multiple such bases at the same location.  */
4510   eoc = end_of_class (t, /*include_virtuals_p=*/1);
4511   if (eoc * BITS_PER_UNIT > dsize)
4512     dsize = (eoc + 1) * BITS_PER_UNIT;
4513
4514   /* Now, make sure that the total size of the type is a multiple of
4515      its alignment.  */
4516   dsize = CEIL (dsize, TYPE_ALIGN (t)) * TYPE_ALIGN (t);
4517   TYPE_SIZE (t) = bitsize_int (dsize);
4518   TYPE_SIZE_UNIT (t) = convert (sizetype,
4519                                 size_binop (CEIL_DIV_EXPR, TYPE_SIZE (t),
4520                                             bitsize_unit_node));
4521
4522   /* Check for ambiguous virtual bases.  */
4523   if (extra_warnings)
4524     for (vbases = CLASSTYPE_VBASECLASSES (t); 
4525          vbases; 
4526          vbases = TREE_CHAIN (vbases))
4527       {
4528         tree basetype = BINFO_TYPE (vbases);
4529         if (get_base_distance (basetype, t, 0, (tree*)0) == -2)
4530           cp_warning ("virtual base `%T' inaccessible in `%T' due to ambiguity",
4531                       basetype, t);
4532       }
4533 }
4534
4535 /* Returns the offset of the byte just past the end of the base class
4536    with the highest offset in T.  If INCLUDE_VIRTUALS_P is zero, then
4537    only non-virtual bases are included.  */
4538
4539 static unsigned HOST_WIDE_INT
4540 end_of_class (t, include_virtuals_p)
4541      tree t;
4542      int include_virtuals_p;
4543 {
4544   unsigned HOST_WIDE_INT result = 0;
4545   int i;
4546
4547   for (i = 0; i < CLASSTYPE_N_BASECLASSES (t); ++i)
4548     {
4549       tree base_binfo;
4550       tree offset;
4551       unsigned HOST_WIDE_INT end_of_base;
4552
4553       base_binfo = BINFO_BASETYPE (TYPE_BINFO (t), i);
4554
4555       if (!include_virtuals_p
4556           && TREE_VIA_VIRTUAL (base_binfo) 
4557           && !BINFO_PRIMARY_MARKED_P (base_binfo))
4558         continue;
4559
4560       offset = size_binop (PLUS_EXPR, 
4561                            BINFO_OFFSET (base_binfo),
4562                            CLASSTYPE_SIZE_UNIT (BINFO_TYPE (base_binfo)));
4563       end_of_base = tree_low_cst (offset, /*pos=*/1);
4564       if (end_of_base > result)
4565         result = end_of_base;
4566     }
4567
4568   return result;
4569 }
4570
4571 /* Calculate the TYPE_SIZE, TYPE_ALIGN, etc for T.  Calculate
4572    BINFO_OFFSETs for all of the base-classes.  Position the vtable
4573    pointer.  */
4574
4575 static void
4576 layout_class_type (t, empty_p, vfuns_p, 
4577                    new_virtuals_p, overridden_virtuals_p)
4578      tree t;
4579      int *empty_p;
4580      int *vfuns_p;
4581      tree *new_virtuals_p;
4582      tree *overridden_virtuals_p;
4583 {
4584   tree non_static_data_members;
4585   tree field;
4586   tree vptr;
4587   record_layout_info rli;
4588   varray_type v;
4589   unsigned HOST_WIDE_INT eoc;
4590
4591   /* Keep track of the first non-static data member.  */
4592   non_static_data_members = TYPE_FIELDS (t);
4593
4594   /* Start laying out the record.  */
4595   rli = start_record_layout (t);
4596
4597   /* If possible, we reuse the virtual function table pointer from one
4598      of our base classes.  */
4599   determine_primary_base (t, vfuns_p);
4600
4601   /* Create a pointer to our virtual function table.  */
4602   vptr = create_vtable_ptr (t, empty_p, vfuns_p,
4603                             new_virtuals_p, overridden_virtuals_p);
4604
4605   /* Under the new ABI, the vptr is always the first thing in the
4606      class.  */
4607   if (flag_new_abi && vptr)
4608     {
4609       TYPE_FIELDS (t) = chainon (vptr, TYPE_FIELDS (t));
4610       place_field (rli, vptr);
4611     }
4612
4613   /* Add pointers to all of our virtual base-classes.  */
4614   TYPE_FIELDS (t) = chainon (build_vbase_pointer_fields (rli, empty_p),
4615                              TYPE_FIELDS (t));
4616   /* Build FIELD_DECLs for all of the non-virtual base-types.  */
4617   v = build_base_fields (rli, empty_p);
4618
4619   /* CLASSTYPE_INLINE_FRIENDS is really TYPE_NONCOPIED_PARTS.  Thus,
4620      we have to save this before we start modifying
4621      TYPE_NONCOPIED_PARTS.  */
4622   fixup_inline_methods (t);
4623
4624   /* Layout the non-static data members.  */
4625   for (field = non_static_data_members; field; field = TREE_CHAIN (field))
4626     {
4627       tree binfo;
4628       tree type;
4629       tree padding;
4630
4631       /* We still pass things that aren't non-static data members to
4632          the back-end, in case it wants to do something with them.  */
4633       if (TREE_CODE (field) != FIELD_DECL)
4634         {
4635           place_field (rli, field);
4636           continue;
4637         }
4638
4639       type = TREE_TYPE (field);
4640
4641       /* If this field is a bit-field whose width is greater than its
4642          type, then there are some special rules for allocating it
4643          under the new ABI.  Under the old ABI, there were no special
4644          rules, but the back-end can't handle bitfields longer than a
4645          `long long', so we use the same mechanism.  */
4646       if (DECL_C_BIT_FIELD (field)
4647           && ((flag_new_abi 
4648                && INT_CST_LT (TYPE_SIZE (type), DECL_SIZE (field)))
4649               || (!flag_new_abi
4650                   && 0 < compare_tree_int (DECL_SIZE (field),
4651                                            TYPE_PRECISION
4652                                            (long_long_unsigned_type_node)))))
4653         {
4654           integer_type_kind itk;
4655           tree integer_type;
4656
4657           /* We must allocate the bits as if suitably aligned for the
4658              longest integer type that fits in this many bits.  type
4659              of the field.  Then, we are supposed to use the left over
4660              bits as additional padding.  */
4661           for (itk = itk_char; itk != itk_none; ++itk)
4662             if (INT_CST_LT (DECL_SIZE (field), 
4663                             TYPE_SIZE (integer_types[itk])))
4664               break;
4665
4666           /* ITK now indicates a type that is too large for the
4667              field.  We have to back up by one to find the largest
4668              type that fits.  */
4669           integer_type = integer_types[itk - 1];
4670           padding = size_binop (MINUS_EXPR, DECL_SIZE (field), 
4671                                 TYPE_SIZE (integer_type));
4672           DECL_SIZE (field) = TYPE_SIZE (integer_type);
4673           DECL_ALIGN (field) = TYPE_ALIGN (integer_type);
4674         }
4675       else
4676         padding = NULL_TREE;
4677
4678       /* Create a dummy BINFO corresponding to this field.  */
4679       binfo = make_binfo (size_zero_node, type, NULL_TREE, NULL_TREE);
4680       unshare_base_binfos (binfo);
4681       layout_nonempty_base_or_field (rli, field, binfo, v);
4682
4683       /* If we needed additional padding after this field, add it
4684          now.  */
4685       if (padding)
4686         {
4687           tree padding_field;
4688
4689           padding_field = build_decl (FIELD_DECL, 
4690                                       NULL_TREE,
4691                                       char_type_node); 
4692           DECL_BIT_FIELD (padding_field) = 1;
4693           DECL_SIZE (padding_field) = padding;
4694           DECL_ALIGN (padding_field) = 1;
4695           layout_nonempty_base_or_field (rli, padding_field, NULL_TREE, v);
4696         }
4697     }
4698
4699   /* It might be the case that we grew the class to allocate a
4700      zero-sized base class.  That won't be reflected in RLI, yet,
4701      because we are willing to overlay multiple bases at the same
4702      offset.  However, now we need to make sure that RLI is big enough
4703      to reflect the entire class.  */
4704   eoc = end_of_class (t, /*include_virtuals_p=*/0);
4705   if (TREE_CODE (rli_size_unit_so_far (rli)) == INTEGER_CST
4706       && compare_tree_int (rli_size_unit_so_far (rli), eoc) < 0)
4707     {
4708       /* We don't handle zero-sized base classes specially under the
4709          old ABI, so if we get here, we had better be operating under
4710          the new ABI rules.  */
4711       my_friendly_assert (flag_new_abi, 20000321);
4712       rli->offset = size_binop (MAX_EXPR, rli->offset, size_int (eoc + 1));
4713       rli->bitpos = bitsize_zero_node;
4714     }
4715
4716   /* We make all structures have at least one element, so that they
4717      have non-zero size.  In the new ABI, the class may be empty even
4718      if it has basetypes.  Therefore, we add the fake field after all
4719      the other fields; if there are already FIELD_DECLs on the list,
4720      their offsets will not be disturbed.  */
4721   if (*empty_p)
4722     {
4723       tree padding;
4724
4725       padding = build_lang_decl (FIELD_DECL, NULL_TREE, char_type_node);
4726       place_field (rli, padding);
4727       TYPE_NONCOPIED_PARTS (t) 
4728         = tree_cons (NULL_TREE, padding, TYPE_NONCOPIED_PARTS (t));
4729       TREE_STATIC (TYPE_NONCOPIED_PARTS (t)) = 1;
4730     }
4731
4732   /* Under the old ABI, the vptr comes at the very end of the 
4733      class.   */
4734   if (!flag_new_abi && vptr)
4735     {
4736       place_field (rli, vptr);
4737       TYPE_FIELDS (t) = chainon (TYPE_FIELDS (t), vptr);
4738     }
4739   
4740   /* Let the back-end lay out the type. Note that at this point we
4741      have only included non-virtual base-classes; we will lay out the
4742      virtual base classes later.  So, the TYPE_SIZE/TYPE_ALIGN after
4743      this call are not necessarily correct; they are just the size and
4744      alignment when no virtual base clases are used.  */
4745   finish_record_layout (rli);
4746
4747   /* Delete all zero-width bit-fields from the list of fields.  Now
4748      that the type is laid out they are no longer important.  */
4749   remove_zero_width_bit_fields (t);
4750
4751   /* Remember the size and alignment of the class before adding
4752      the virtual bases.  */
4753   if (*empty_p && flag_new_abi)
4754     {
4755       CLASSTYPE_SIZE (t) = bitsize_zero_node;
4756       CLASSTYPE_SIZE_UNIT (t) = size_zero_node;
4757     }
4758   else if (flag_new_abi && TYPE_HAS_COMPLEX_INIT_REF (t)
4759            && TYPE_HAS_COMPLEX_ASSIGN_REF (t))
4760     {
4761       CLASSTYPE_SIZE (t) = TYPE_BINFO_SIZE (t);
4762       CLASSTYPE_SIZE_UNIT (t) = TYPE_BINFO_SIZE_UNIT (t);
4763     }
4764   else
4765     {
4766       CLASSTYPE_SIZE (t) = TYPE_SIZE (t);
4767       CLASSTYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (t);
4768     }
4769
4770   CLASSTYPE_ALIGN (t) = TYPE_ALIGN (t);
4771
4772   /* Set the TYPE_DECL for this type to contain the right
4773      value for DECL_OFFSET, so that we can use it as part
4774      of a COMPONENT_REF for multiple inheritance.  */
4775   layout_decl (TYPE_MAIN_DECL (t), 0);
4776
4777   /* Now fix up any virtual base class types that we left lying
4778      around.  We must get these done before we try to lay out the
4779      virtual function table.  As a side-effect, this will remove the
4780      base subobject fields.  */
4781   layout_virtual_bases (t, &v);
4782
4783   /* Clean up.  */
4784   VARRAY_FREE (v);
4785 }
4786
4787 /* Create a RECORD_TYPE or UNION_TYPE node for a C struct or union declaration
4788    (or C++ class declaration).
4789
4790    For C++, we must handle the building of derived classes.
4791    Also, C++ allows static class members.  The way that this is
4792    handled is to keep the field name where it is (as the DECL_NAME
4793    of the field), and place the overloaded decl in the bit position
4794    of the field.  layout_record and layout_union will know about this.
4795
4796    More C++ hair: inline functions have text in their
4797    DECL_PENDING_INLINE_INFO nodes which must somehow be parsed into
4798    meaningful tree structure.  After the struct has been laid out, set
4799    things up so that this can happen.
4800
4801    And still more: virtual functions.  In the case of single inheritance,
4802    when a new virtual function is seen which redefines a virtual function
4803    from the base class, the new virtual function is placed into
4804    the virtual function table at exactly the same address that
4805    it had in the base class.  When this is extended to multiple
4806    inheritance, the same thing happens, except that multiple virtual
4807    function tables must be maintained.  The first virtual function
4808    table is treated in exactly the same way as in the case of single
4809    inheritance.  Additional virtual function tables have different
4810    DELTAs, which tell how to adjust `this' to point to the right thing.
4811
4812    ATTRIBUTES is the set of decl attributes to be applied, if any.  */
4813
4814 void
4815 finish_struct_1 (t)
4816      tree t;
4817 {
4818   tree x;
4819   int vfuns;
4820   /* The NEW_VIRTUALS is a TREE_LIST.  The TREE_VALUE of each node is
4821      a FUNCTION_DECL.  Each of these functions is a virtual function
4822      declared in T that does not override any virtual function from a
4823      base class.  */
4824   tree new_virtuals = NULL_TREE;
4825   /* The OVERRIDDEN_VIRTUALS list is like the NEW_VIRTUALS list,
4826      except that each declaration here overrides the declaration from
4827      a base class.  */
4828   tree overridden_virtuals = NULL_TREE;
4829   int n_fields = 0;
4830   tree vfield;
4831   int empty = 1;
4832
4833   if (COMPLETE_TYPE_P (t))
4834     {
4835       if (IS_AGGR_TYPE (t))
4836         cp_error ("redefinition of `%#T'", t);
4837       else
4838         my_friendly_abort (172);
4839       popclass ();
4840       return;
4841     }
4842
4843   GNU_xref_decl (current_function_decl, t);
4844
4845   /* If this type was previously laid out as a forward reference,
4846      make sure we lay it out again.  */
4847   TYPE_SIZE (t) = NULL_TREE;
4848   CLASSTYPE_GOT_SEMICOLON (t) = 0;
4849   CLASSTYPE_VFIELD_PARENT (t) = -1;
4850   vfuns = 0;
4851   CLASSTYPE_RTTI (t) = NULL_TREE;
4852
4853   /* Do end-of-class semantic processing: checking the validity of the
4854      bases and members and add implicitly generated methods.  */
4855   check_bases_and_members (t, &empty);
4856
4857   /* Layout the class itself.  */
4858   layout_class_type (t, &empty, &vfuns,
4859                      &new_virtuals, &overridden_virtuals);
4860
4861   /* Set up the DECL_FIELD_BITPOS of the vfield if we need to, as we
4862      might need to know it for setting up the offsets in the vtable
4863      (or in thunks) below.  */
4864   vfield = TYPE_VFIELD (t);
4865   if (vfield != NULL_TREE
4866       && DECL_FIELD_CONTEXT (vfield) != t)
4867     {
4868       tree binfo = get_binfo (DECL_FIELD_CONTEXT (vfield), t, 0);
4869
4870       vfield = copy_decl (vfield);
4871
4872       DECL_FIELD_CONTEXT (vfield) = t;
4873       DECL_FIELD_OFFSET (vfield)
4874         = size_binop (PLUS_EXPR,
4875                       BINFO_OFFSET (binfo),
4876                       DECL_FIELD_OFFSET (vfield));
4877       TYPE_VFIELD (t) = vfield;
4878     }
4879
4880   overridden_virtuals 
4881     = modify_all_vtables (t, &vfuns, nreverse (overridden_virtuals));
4882
4883   /* If necessary, create the primary vtable for this class.  */
4884   if (new_virtuals
4885       || overridden_virtuals
4886       || (TYPE_CONTAINS_VPTR_P (t) && vptrs_present_everywhere_p ()))
4887     {
4888       new_virtuals = nreverse (new_virtuals);
4889       /* We must enter these virtuals into the table.  */
4890       if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
4891         build_primary_vtable (NULL_TREE, t);
4892       else if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t), t))
4893         /* Here we know enough to change the type of our virtual
4894            function table, but we will wait until later this function.  */
4895         build_primary_vtable (CLASSTYPE_PRIMARY_BINFO (t), t);
4896
4897       /* If this type has basetypes with constructors, then those
4898          constructors might clobber the virtual function table.  But
4899          they don't if the derived class shares the exact vtable of the base
4900          class.  */
4901
4902       CLASSTYPE_NEEDS_VIRTUAL_REINIT (t) = 1;
4903     }
4904   /* If we didn't need a new vtable, see if we should copy one from
4905      the base.  */
4906   else if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
4907     {
4908       tree binfo = CLASSTYPE_PRIMARY_BINFO (t);
4909
4910       /* This class contributes nothing new to the virtual function
4911          table.  However, it may have declared functions which
4912          went into the virtual function table "inherited" from the
4913          base class.  If so, we grab a copy of those updated functions,
4914          and pretend they are ours.  */
4915
4916       /* See if we should steal the virtual info from base class.  */
4917       if (TYPE_BINFO_VTABLE (t) == NULL_TREE)
4918         TYPE_BINFO_VTABLE (t) = BINFO_VTABLE (binfo);
4919       if (TYPE_BINFO_VIRTUALS (t) == NULL_TREE)
4920         TYPE_BINFO_VIRTUALS (t) = BINFO_VIRTUALS (binfo);
4921       if (TYPE_BINFO_VTABLE (t) != BINFO_VTABLE (binfo))
4922         CLASSTYPE_NEEDS_VIRTUAL_REINIT (t) = 1;
4923     }
4924
4925   if (TYPE_CONTAINS_VPTR_P (t))
4926     {
4927       if (TYPE_BINFO_VTABLE (t))
4928         my_friendly_assert (DECL_VIRTUAL_P (TYPE_BINFO_VTABLE (t)),
4929                             20000116);
4930       if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
4931         my_friendly_assert (TYPE_BINFO_VIRTUALS (t) == NULL_TREE,
4932                             20000116);
4933
4934       CLASSTYPE_VSIZE (t) = vfuns;
4935       /* Entries for virtual functions defined in the primary base are
4936          followed by entries for new functions unique to this class.  */
4937       TYPE_BINFO_VIRTUALS (t) 
4938         = chainon (TYPE_BINFO_VIRTUALS (t), new_virtuals);
4939       /* Finally, add entries for functions that override virtuals
4940          from non-primary bases.  */
4941       TYPE_BINFO_VIRTUALS (t) 
4942         = chainon (TYPE_BINFO_VIRTUALS (t), overridden_virtuals);
4943     }
4944
4945   /* If we created a new vtbl pointer for this class, add it to the
4946      list.  */
4947   if (TYPE_VFIELD (t) && !CLASSTYPE_HAS_PRIMARY_BASE_P (t))
4948     CLASSTYPE_VFIELDS (t) 
4949       = chainon (CLASSTYPE_VFIELDS (t), build_tree_list (NULL_TREE, t));
4950
4951   finish_struct_bits (t);
4952
4953   /* Complete the rtl for any static member objects of the type we're
4954      working on.  */
4955   for (x = TYPE_FIELDS (t); x; x = TREE_CHAIN (x))
4956     {
4957       if (TREE_CODE (x) == VAR_DECL && TREE_STATIC (x)
4958           && TREE_TYPE (x) == t)
4959         {
4960           DECL_MODE (x) = TYPE_MODE (t);
4961           make_decl_rtl (x, NULL, 0);
4962         }
4963     }
4964
4965   /* Done with FIELDS...now decide whether to sort these for
4966      faster lookups later.
4967
4968      The C front-end only does this when n_fields > 15.  We use
4969      a smaller number because most searches fail (succeeding
4970      ultimately as the search bores through the inheritance
4971      hierarchy), and we want this failure to occur quickly.  */
4972
4973   n_fields = count_fields (TYPE_FIELDS (t));
4974   if (n_fields > 7)
4975     {
4976       tree field_vec = make_tree_vec (n_fields);
4977       add_fields_to_vec (TYPE_FIELDS (t), field_vec, 0);
4978       qsort (&TREE_VEC_ELT (field_vec, 0), n_fields, sizeof (tree),
4979              (int (*)(const void *, const void *))field_decl_cmp);
4980       if (! DECL_LANG_SPECIFIC (TYPE_MAIN_DECL (t)))
4981         retrofit_lang_decl (TYPE_MAIN_DECL (t));
4982       DECL_SORTED_FIELDS (TYPE_MAIN_DECL (t)) = field_vec;
4983     }
4984
4985   if (TYPE_HAS_CONSTRUCTOR (t))
4986     {
4987       tree vfields = CLASSTYPE_VFIELDS (t);
4988
4989       while (vfields)
4990         {
4991           /* Mark the fact that constructor for T
4992              could affect anybody inheriting from T
4993              who wants to initialize vtables for VFIELDS's type.  */
4994           if (VF_DERIVED_VALUE (vfields))
4995             TREE_ADDRESSABLE (vfields) = 1;
4996           vfields = TREE_CHAIN (vfields);
4997         }
4998     }
4999
5000   /* Make the rtl for any new vtables we have created, and unmark
5001      the base types we marked.  */
5002   finish_vtbls (t);
5003
5004   if (TYPE_VFIELD (t))
5005     {
5006       /* In addition to this one, all the other vfields should be listed.  */
5007       /* Before that can be done, we have to have FIELD_DECLs for them, and
5008          a place to find them.  */
5009       TYPE_NONCOPIED_PARTS (t) 
5010         = tree_cons (default_conversion (TYPE_BINFO_VTABLE (t)),
5011                      TYPE_VFIELD (t), TYPE_NONCOPIED_PARTS (t));
5012
5013       if (warn_nonvdtor && TYPE_HAS_DESTRUCTOR (t)
5014           && DECL_VINDEX (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 1)) == NULL_TREE)
5015         cp_warning ("`%#T' has virtual functions but non-virtual destructor",
5016                     t);
5017     }
5018
5019   hack_incomplete_structures (t);
5020
5021   if (warn_overloaded_virtual)
5022     warn_hidden (t);
5023
5024   maybe_suppress_debug_info (t);
5025
5026   /* Finish debugging output for this type.  */
5027   rest_of_type_compilation (t, toplevel_bindings_p ());
5028 }
5029
5030 /* When T was built up, the member declarations were added in reverse
5031    order.  Rearrange them to declaration order.  */
5032
5033 void
5034 unreverse_member_declarations (t)
5035      tree t;
5036 {
5037   tree next;
5038   tree prev;
5039   tree x;
5040
5041   /* The TYPE_FIELDS, TYPE_METHODS, and CLASSTYPE_TAGS are all in
5042      reverse order.  Put them in declaration order now.  */
5043   TYPE_METHODS (t) = nreverse (TYPE_METHODS (t));
5044   CLASSTYPE_TAGS (t) = nreverse (CLASSTYPE_TAGS (t));
5045
5046   /* Actually, for the TYPE_FIELDS, only the non TYPE_DECLs are in
5047      reverse order, so we can't just use nreverse.  */
5048   prev = NULL_TREE;
5049   for (x = TYPE_FIELDS (t); 
5050        x && TREE_CODE (x) != TYPE_DECL; 
5051        x = next)
5052     {
5053       next = TREE_CHAIN (x);
5054       TREE_CHAIN (x) = prev;
5055       prev = x;
5056     }
5057   if (prev)
5058     {
5059       TREE_CHAIN (TYPE_FIELDS (t)) = x;
5060       if (prev)
5061         TYPE_FIELDS (t) = prev;
5062     }
5063 }
5064
5065 tree
5066 finish_struct (t, attributes)
5067      tree t, attributes;
5068 {
5069   /* Now that we've got all the field declarations, reverse everything
5070      as necessary.  */
5071   unreverse_member_declarations (t);
5072
5073   cplus_decl_attributes (t, attributes, NULL_TREE);
5074
5075   if (processing_template_decl)
5076     {
5077       finish_struct_methods (t);
5078       TYPE_SIZE (t) = bitsize_zero_node;
5079     }
5080   else
5081     finish_struct_1 (t);
5082
5083   TYPE_BEING_DEFINED (t) = 0;
5084
5085   if (current_class_type)
5086     popclass ();
5087   else
5088     error ("trying to finish struct, but kicked out due to previous parse errors.");
5089
5090   if (processing_template_decl)
5091     {
5092       tree scope = current_scope ();
5093       if (scope && TREE_CODE (scope) == FUNCTION_DECL)
5094         add_tree (build_min (TAG_DEFN, t));
5095     }
5096
5097   return t;
5098 }
5099 \f
5100 /* Return the dynamic type of INSTANCE, if known.
5101    Used to determine whether the virtual function table is needed
5102    or not.
5103
5104    *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
5105    of our knowledge of its type.  *NONNULL should be initialized
5106    before this function is called.  */
5107
5108 static tree
5109 fixed_type_or_null (instance, nonnull)
5110      tree instance;
5111      int *nonnull;
5112 {
5113   switch (TREE_CODE (instance))
5114     {
5115     case INDIRECT_REF:
5116       /* Check that we are not going through a cast of some sort.  */
5117       if (TREE_TYPE (instance)
5118           == TREE_TYPE (TREE_TYPE (TREE_OPERAND (instance, 0))))
5119         instance = TREE_OPERAND (instance, 0);
5120       /* fall through...  */
5121     case CALL_EXPR:
5122       /* This is a call to a constructor, hence it's never zero.  */
5123       if (TREE_HAS_CONSTRUCTOR (instance))
5124         {
5125           if (nonnull)
5126             *nonnull = 1;
5127           return TREE_TYPE (instance);
5128         }
5129       return NULL_TREE;
5130
5131     case SAVE_EXPR:
5132       /* This is a call to a constructor, hence it's never zero.  */
5133       if (TREE_HAS_CONSTRUCTOR (instance))
5134         {
5135           if (nonnull)
5136             *nonnull = 1;
5137           return TREE_TYPE (instance);
5138         }
5139       return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
5140
5141     case RTL_EXPR:
5142       return NULL_TREE;
5143
5144     case PLUS_EXPR:
5145     case MINUS_EXPR:
5146       if (TREE_CODE (TREE_OPERAND (instance, 1)) == INTEGER_CST)
5147         /* Propagate nonnull.  */
5148         fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
5149       if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR)
5150         return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
5151       return NULL_TREE;
5152
5153     case NOP_EXPR:
5154     case CONVERT_EXPR:
5155       return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
5156
5157     case ADDR_EXPR:
5158       if (nonnull)
5159         *nonnull = 1;
5160       return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
5161
5162     case COMPONENT_REF:
5163       return fixed_type_or_null (TREE_OPERAND (instance, 1), nonnull);
5164
5165     case VAR_DECL:
5166     case FIELD_DECL:
5167       if (TREE_CODE (TREE_TYPE (instance)) == ARRAY_TYPE
5168           && IS_AGGR_TYPE (TREE_TYPE (TREE_TYPE (instance))))
5169         {
5170           if (nonnull)
5171             *nonnull = 1;
5172           return TREE_TYPE (TREE_TYPE (instance));
5173         }
5174       /* fall through...  */
5175     case TARGET_EXPR:
5176     case PARM_DECL:
5177       if (IS_AGGR_TYPE (TREE_TYPE (instance)))
5178         {
5179           if (nonnull)
5180             *nonnull = 1;
5181           return TREE_TYPE (instance);
5182         }
5183       else if (nonnull)
5184         {
5185           if (instance == current_class_ptr
5186               && flag_this_is_variable <= 0)
5187             {
5188               /* Normally, 'this' must be non-null.  */
5189               if (flag_this_is_variable == 0)
5190                 *nonnull = 1;
5191
5192               /* <0 means we're in a constructor and we know our type.  */
5193               if (flag_this_is_variable < 0)
5194                 return TREE_TYPE (TREE_TYPE (instance));
5195             }
5196           else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
5197             /* Reference variables should be references to objects.  */
5198             *nonnull = 1;
5199         }
5200       return NULL_TREE;
5201
5202     default:
5203       return NULL_TREE;
5204     }
5205 }
5206
5207 /* Return non-zero if the dynamic type of INSTANCE is known, and equivalent
5208    to the static type.  We also handle the case where INSTANCE is really
5209    a pointer.
5210
5211    Used to determine whether the virtual function table is needed
5212    or not.
5213
5214    *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
5215    of our knowledge of its type.  *NONNULL should be initialized
5216    before this function is called.  */
5217
5218 int
5219 resolves_to_fixed_type_p (instance, nonnull)
5220      tree instance;
5221      int *nonnull;
5222 {
5223   tree t = TREE_TYPE (instance);
5224   tree fixed = fixed_type_or_null (instance, nonnull);
5225   if (fixed == NULL_TREE)
5226     return 0;
5227   if (POINTER_TYPE_P (t))
5228     t = TREE_TYPE (t);
5229   return same_type_ignoring_top_level_qualifiers_p (t, fixed);
5230 }
5231
5232 \f
5233 void
5234 init_class_processing ()
5235 {
5236   current_class_depth = 0;
5237   current_class_stack_size = 10;
5238   current_class_stack 
5239     = (class_stack_node_t) xmalloc (current_class_stack_size 
5240                                     * sizeof (struct class_stack_node));
5241
5242   access_default_node = build_int_2 (0, 0);
5243   access_public_node = build_int_2 (ak_public, 0);
5244   access_protected_node = build_int_2 (ak_protected, 0);
5245   access_private_node = build_int_2 (ak_private, 0);
5246   access_default_virtual_node = build_int_2 (4, 0);
5247   access_public_virtual_node = build_int_2 (4 | ak_public, 0);
5248   access_protected_virtual_node = build_int_2 (4 | ak_protected, 0);
5249   access_private_virtual_node = build_int_2 (4 | ak_private, 0);
5250 }
5251
5252 /* Set current scope to NAME. CODE tells us if this is a
5253    STRUCT, UNION, or ENUM environment.
5254
5255    NAME may end up being NULL_TREE if this is an anonymous or
5256    late-bound struct (as in "struct { ... } foo;")  */
5257
5258 /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE to
5259    appropriate values, found by looking up the type definition of
5260    NAME (as a CODE).
5261
5262    If MODIFY is 1, we set IDENTIFIER_CLASS_VALUE's of names
5263    which can be seen locally to the class.  They are shadowed by
5264    any subsequent local declaration (including parameter names).
5265
5266    If MODIFY is 2, we set IDENTIFIER_CLASS_VALUE's of names
5267    which have static meaning (i.e., static members, static
5268    member functions, enum declarations, etc).
5269
5270    If MODIFY is 3, we set IDENTIFIER_CLASS_VALUE of names
5271    which can be seen locally to the class (as in 1), but
5272    know that we are doing this for declaration purposes
5273    (i.e. friend foo::bar (int)).
5274
5275    So that we may avoid calls to lookup_name, we cache the _TYPE
5276    nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
5277
5278    For multiple inheritance, we perform a two-pass depth-first search
5279    of the type lattice.  The first pass performs a pre-order search,
5280    marking types after the type has had its fields installed in
5281    the appropriate IDENTIFIER_CLASS_VALUE slot.  The second pass merely
5282    unmarks the marked types.  If a field or member function name
5283    appears in an ambiguous way, the IDENTIFIER_CLASS_VALUE of
5284    that name becomes `error_mark_node'.  */
5285
5286 void
5287 pushclass (type, modify)
5288      tree type;
5289      int modify;
5290 {
5291   type = TYPE_MAIN_VARIANT (type);
5292
5293   /* Make sure there is enough room for the new entry on the stack.  */
5294   if (current_class_depth + 1 >= current_class_stack_size) 
5295     {
5296       current_class_stack_size *= 2;
5297       current_class_stack
5298         = (class_stack_node_t) xrealloc (current_class_stack,
5299                                          current_class_stack_size
5300                                          * sizeof (struct class_stack_node));
5301     }
5302
5303   /* Insert a new entry on the class stack.  */
5304   current_class_stack[current_class_depth].name = current_class_name;
5305   current_class_stack[current_class_depth].type = current_class_type;
5306   current_class_stack[current_class_depth].access = current_access_specifier;
5307   current_class_stack[current_class_depth].names_used = 0;
5308   current_class_depth++;
5309
5310   /* Now set up the new type.  */
5311   current_class_name = TYPE_NAME (type);
5312   if (TREE_CODE (current_class_name) == TYPE_DECL)
5313     current_class_name = DECL_NAME (current_class_name);
5314   current_class_type = type;
5315
5316   /* By default, things in classes are private, while things in
5317      structures or unions are public.  */
5318   current_access_specifier = (CLASSTYPE_DECLARED_CLASS (type) 
5319                               ? access_private_node 
5320                               : access_public_node);
5321
5322   if (previous_class_type != NULL_TREE
5323       && (type != previous_class_type 
5324           || !COMPLETE_TYPE_P (previous_class_type))
5325       && current_class_depth == 1)
5326     {
5327       /* Forcibly remove any old class remnants.  */
5328       invalidate_class_lookup_cache ();
5329     }
5330
5331   /* If we're about to enter a nested class, clear
5332      IDENTIFIER_CLASS_VALUE for the enclosing classes.  */
5333   if (modify && current_class_depth > 1)
5334     clear_identifier_class_values ();
5335
5336   pushlevel_class ();
5337
5338 #if 0
5339   if (CLASSTYPE_TEMPLATE_INFO (type))
5340     overload_template_name (type);
5341 #endif
5342
5343   if (modify)
5344     {
5345       if (type != previous_class_type || current_class_depth > 1)
5346         push_class_decls (type);
5347       else
5348         {
5349           tree item;
5350
5351           /* We are re-entering the same class we just left, so we
5352              don't have to search the whole inheritance matrix to find
5353              all the decls to bind again.  Instead, we install the
5354              cached class_shadowed list, and walk through it binding
5355              names and setting up IDENTIFIER_TYPE_VALUEs.  */
5356           set_class_shadows (previous_class_values);
5357           for (item = previous_class_values; item; item = TREE_CHAIN (item))
5358             {
5359               tree id = TREE_PURPOSE (item);
5360               tree decl = TREE_TYPE (item);
5361
5362               push_class_binding (id, decl);
5363               if (TREE_CODE (decl) == TYPE_DECL)
5364                 set_identifier_type_value (id, TREE_TYPE (decl));
5365             }
5366           unuse_fields (type);
5367         }
5368
5369       storetags (CLASSTYPE_TAGS (type));
5370     }
5371 }
5372
5373 /* When we exit a toplevel class scope, we save the
5374    IDENTIFIER_CLASS_VALUEs so that we can restore them quickly if we
5375    reenter the class.  Here, we've entered some other class, so we
5376    must invalidate our cache.  */
5377
5378 void
5379 invalidate_class_lookup_cache ()
5380 {
5381   tree t;
5382   
5383   /* This code can be seen as a cache miss.  When we've cached a
5384      class' scope's bindings and we can't use them, we need to reset
5385      them.  This is it!  */
5386   for (t = previous_class_values; t; t = TREE_CHAIN (t))
5387     IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (t)) = NULL_TREE;
5388   
5389   previous_class_type = NULL_TREE;
5390 }
5391  
5392 /* Get out of the current class scope. If we were in a class scope
5393    previously, that is the one popped to.  */
5394
5395 void
5396 popclass ()
5397 {
5398   poplevel_class ();
5399   /* Since poplevel_class does the popping of class decls nowadays,
5400      this really only frees the obstack used for these decls.  */
5401   pop_class_decls ();
5402
5403   current_class_depth--;
5404   current_class_name = current_class_stack[current_class_depth].name;
5405   current_class_type = current_class_stack[current_class_depth].type;
5406   current_access_specifier = current_class_stack[current_class_depth].access;
5407   if (current_class_stack[current_class_depth].names_used)
5408     splay_tree_delete (current_class_stack[current_class_depth].names_used);
5409 }
5410
5411 /* Returns 1 if current_class_type is either T or a nested type of T.
5412    We start looking from 1 because entry 0 is from global scope, and has
5413    no type.  */
5414
5415 int
5416 currently_open_class (t)
5417      tree t;
5418 {
5419   int i;
5420   if (t == current_class_type)
5421     return 1;
5422   for (i = 1; i < current_class_depth; ++i)
5423     if (current_class_stack [i].type == t)
5424       return 1;
5425   return 0;
5426 }
5427
5428 /* If either current_class_type or one of its enclosing classes are derived
5429    from T, return the appropriate type.  Used to determine how we found
5430    something via unqualified lookup.  */
5431
5432 tree
5433 currently_open_derived_class (t)
5434      tree t;
5435 {
5436   int i;
5437
5438   if (DERIVED_FROM_P (t, current_class_type))
5439     return current_class_type;
5440
5441   for (i = current_class_depth - 1; i > 0; --i)
5442     if (DERIVED_FROM_P (t, current_class_stack[i].type))
5443       return current_class_stack[i].type;
5444
5445   return NULL_TREE;
5446 }
5447
5448 /* When entering a class scope, all enclosing class scopes' names with
5449    static meaning (static variables, static functions, types and enumerators)
5450    have to be visible.  This recursive function calls pushclass for all
5451    enclosing class contexts until global or a local scope is reached.
5452    TYPE is the enclosed class and MODIFY is equivalent with the pushclass
5453    formal of the same name.  */
5454
5455 void
5456 push_nested_class (type, modify)
5457      tree type;
5458      int modify;
5459 {
5460   tree context;
5461
5462   /* A namespace might be passed in error cases, like A::B:C.  */
5463   if (type == NULL_TREE 
5464       || type == error_mark_node 
5465       || TREE_CODE (type) == NAMESPACE_DECL
5466       || ! IS_AGGR_TYPE (type)
5467       || TREE_CODE (type) == TEMPLATE_TYPE_PARM
5468       || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
5469     return;
5470   
5471   context = DECL_CONTEXT (TYPE_MAIN_DECL (type));
5472
5473   if (context && CLASS_TYPE_P (context))
5474     push_nested_class (context, 2);
5475   pushclass (type, modify);
5476 }
5477
5478 /* Undoes a push_nested_class call.  MODIFY is passed on to popclass.  */
5479
5480 void
5481 pop_nested_class ()
5482 {
5483   tree context = DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type));
5484
5485   popclass ();
5486   if (context && CLASS_TYPE_P (context))
5487     pop_nested_class ();
5488 }
5489
5490 /* Set global variables CURRENT_LANG_NAME to appropriate value
5491    so that behavior of name-mangling machinery is correct.  */
5492
5493 void
5494 push_lang_context (name)
5495      tree name;
5496 {
5497   *current_lang_stack++ = current_lang_name;
5498   if (current_lang_stack - &VARRAY_TREE (current_lang_base, 0)
5499       >= (ptrdiff_t) VARRAY_SIZE (current_lang_base))
5500     {
5501       size_t old_size = VARRAY_SIZE (current_lang_base);
5502
5503       VARRAY_GROW (current_lang_base, old_size + 10);
5504       current_lang_stack = &VARRAY_TREE (current_lang_base, old_size);
5505     }
5506
5507   if (name == lang_name_cplusplus)
5508     {
5509       strict_prototype = strict_prototypes_lang_cplusplus;
5510       current_lang_name = name;
5511     }
5512   else if (name == lang_name_java)
5513     {
5514       strict_prototype = strict_prototypes_lang_cplusplus;
5515       current_lang_name = name;
5516       /* DECL_IGNORED_P is initially set for these types, to avoid clutter.
5517          (See record_builtin_java_type in decl.c.)  However, that causes
5518          incorrect debug entries if these types are actually used.
5519          So we re-enable debug output after extern "Java". */
5520       DECL_IGNORED_P (java_byte_type_node) = 0;
5521       DECL_IGNORED_P (java_short_type_node) = 0;
5522       DECL_IGNORED_P (java_int_type_node) = 0;
5523       DECL_IGNORED_P (java_long_type_node) = 0;
5524       DECL_IGNORED_P (java_float_type_node) = 0;
5525       DECL_IGNORED_P (java_double_type_node) = 0;
5526       DECL_IGNORED_P (java_char_type_node) = 0;
5527       DECL_IGNORED_P (java_boolean_type_node) = 0;
5528     }
5529   else if (name == lang_name_c)
5530     {
5531       strict_prototype = strict_prototypes_lang_c;
5532       current_lang_name = name;
5533     }
5534   else
5535     error ("language string `\"%s\"' not recognized", IDENTIFIER_POINTER (name));
5536 }
5537   
5538 /* Get out of the current language scope.  */
5539
5540 void
5541 pop_lang_context ()
5542 {
5543   /* Clear the current entry so that garbage collector won't hold on
5544      to it.  */
5545   *current_lang_stack = NULL_TREE;
5546   current_lang_name = *--current_lang_stack;
5547   if (current_lang_name == lang_name_cplusplus
5548       || current_lang_name == lang_name_java)
5549     strict_prototype = strict_prototypes_lang_cplusplus;
5550   else if (current_lang_name == lang_name_c)
5551     strict_prototype = strict_prototypes_lang_c;
5552 }
5553 \f
5554 /* Type instantiation routines.  */
5555
5556 /* Given an OVERLOAD and a TARGET_TYPE, return the function that
5557    matches the TARGET_TYPE.  If there is no satisfactory match, return
5558    error_mark_node, and issue an error message if COMPLAIN is
5559    non-zero.  If TEMPLATE_ONLY, the name of the overloaded function
5560    was a template-id, and EXPLICIT_TARGS are the explicitly provided
5561    template arguments.  */
5562
5563 static tree
5564 resolve_address_of_overloaded_function (target_type, 
5565                                         overload,
5566                                         complain, 
5567                                         template_only,
5568                                         explicit_targs)
5569      tree target_type;
5570      tree overload;
5571      int complain;
5572      int template_only;
5573      tree explicit_targs;
5574 {
5575   /* Here's what the standard says:
5576      
5577        [over.over]
5578
5579        If the name is a function template, template argument deduction
5580        is done, and if the argument deduction succeeds, the deduced
5581        arguments are used to generate a single template function, which
5582        is added to the set of overloaded functions considered.
5583
5584        Non-member functions and static member functions match targets of
5585        type "pointer-to-function" or "reference-to-function."  Nonstatic
5586        member functions match targets of type "pointer-to-member
5587        function;" the function type of the pointer to member is used to
5588        select the member function from the set of overloaded member
5589        functions.  If a nonstatic member function is selected, the
5590        reference to the overloaded function name is required to have the
5591        form of a pointer to member as described in 5.3.1.
5592
5593        If more than one function is selected, any template functions in
5594        the set are eliminated if the set also contains a non-template
5595        function, and any given template function is eliminated if the
5596        set contains a second template function that is more specialized
5597        than the first according to the partial ordering rules 14.5.5.2.
5598        After such eliminations, if any, there shall remain exactly one
5599        selected function.  */
5600
5601   int is_ptrmem = 0;
5602   int is_reference = 0;
5603   /* We store the matches in a TREE_LIST rooted here.  The functions
5604      are the TREE_PURPOSE, not the TREE_VALUE, in this list, for easy
5605      interoperability with most_specialized_instantiation.  */
5606   tree matches = NULL_TREE;
5607   tree fn;
5608
5609   /* By the time we get here, we should be seeing only real
5610      pointer-to-member types, not the internal POINTER_TYPE to
5611      METHOD_TYPE representation.  */
5612   my_friendly_assert (!(TREE_CODE (target_type) == POINTER_TYPE
5613                         && (TREE_CODE (TREE_TYPE (target_type)) 
5614                             == METHOD_TYPE)), 0);
5615
5616   if (TREE_CODE (overload) == COMPONENT_REF)
5617     overload = TREE_OPERAND (overload, 1);
5618
5619   /* Check that the TARGET_TYPE is reasonable.  */
5620   if (TYPE_PTRFN_P (target_type))
5621     /* This is OK.  */
5622     ;
5623   else if (TYPE_PTRMEMFUNC_P (target_type))
5624     /* This is OK, too.  */
5625     is_ptrmem = 1;
5626   else if (TREE_CODE (target_type) == FUNCTION_TYPE)
5627     {
5628       /* This is OK, too.  This comes from a conversion to reference
5629          type.  */
5630       target_type = build_reference_type (target_type);
5631       is_reference = 1;
5632     }
5633   else 
5634     {
5635       if (complain)
5636         cp_error("cannot resolve overloaded function `%D' based on conversion to type `%T'", 
5637                  DECL_NAME (OVL_FUNCTION (overload)), target_type);
5638       return error_mark_node;
5639     }
5640   
5641   /* If we can find a non-template function that matches, we can just
5642      use it.  There's no point in generating template instantiations
5643      if we're just going to throw them out anyhow.  But, of course, we
5644      can only do this when we don't *need* a template function.  */
5645   if (!template_only)
5646     {
5647       tree fns;
5648
5649       for (fns = overload; fns; fns = OVL_CHAIN (fns))
5650         {
5651           tree fn = OVL_FUNCTION (fns);
5652           tree fntype;
5653
5654           if (TREE_CODE (fn) == TEMPLATE_DECL)
5655             /* We're not looking for templates just yet.  */
5656             continue;
5657
5658           if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
5659               != is_ptrmem)
5660             /* We're looking for a non-static member, and this isn't
5661                one, or vice versa.  */
5662             continue;
5663         
5664           /* See if there's a match.  */
5665           fntype = TREE_TYPE (fn);
5666           if (is_ptrmem)
5667             fntype = build_ptrmemfunc_type (build_pointer_type (fntype));
5668           else if (!is_reference)
5669             fntype = build_pointer_type (fntype);
5670
5671           if (can_convert_arg (target_type, fntype, fn))
5672             matches = tree_cons (fn, NULL_TREE, matches);
5673         }
5674     }
5675
5676   /* Now, if we've already got a match (or matches), there's no need
5677      to proceed to the template functions.  But, if we don't have a
5678      match we need to look at them, too.  */
5679   if (!matches) 
5680     {
5681       tree target_fn_type;
5682       tree target_arg_types;
5683       tree target_ret_type;
5684       tree fns;
5685
5686       if (is_ptrmem)
5687         target_fn_type
5688           = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (target_type));
5689       else
5690         target_fn_type = TREE_TYPE (target_type);
5691       target_arg_types = TYPE_ARG_TYPES (target_fn_type);
5692       target_ret_type = TREE_TYPE (target_fn_type);
5693           
5694       for (fns = overload; fns; fns = OVL_CHAIN (fns))
5695         {
5696           tree fn = OVL_FUNCTION (fns);
5697           tree instantiation;
5698           tree instantiation_type;
5699           tree targs;
5700
5701           if (TREE_CODE (fn) != TEMPLATE_DECL)
5702             /* We're only looking for templates.  */
5703             continue;
5704
5705           if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
5706               != is_ptrmem)
5707             /* We're not looking for a non-static member, and this is
5708                one, or vice versa.  */
5709             continue;
5710
5711           /* Try to do argument deduction.  */
5712           targs = make_tree_vec (DECL_NTPARMS (fn));
5713           if (fn_type_unification (fn, explicit_targs, targs,
5714                                    target_arg_types, target_ret_type,
5715                                    DEDUCE_EXACT) != 0)
5716             /* Argument deduction failed.  */
5717             continue;
5718
5719           /* Instantiate the template.  */
5720           instantiation = instantiate_template (fn, targs);
5721           if (instantiation == error_mark_node)
5722             /* Instantiation failed.  */
5723             continue;
5724
5725           /* See if there's a match.  */
5726           instantiation_type = TREE_TYPE (instantiation);
5727           if (is_ptrmem)
5728             instantiation_type = 
5729               build_ptrmemfunc_type (build_pointer_type (instantiation_type));
5730           else if (!is_reference)
5731             instantiation_type = build_pointer_type (instantiation_type);
5732           if (can_convert_arg (target_type, instantiation_type, instantiation))
5733             matches = tree_cons (instantiation, fn, matches);
5734         }
5735
5736       /* Now, remove all but the most specialized of the matches.  */
5737       if (matches)
5738         {
5739           tree match = most_specialized_instantiation (matches, 
5740                                                        explicit_targs);
5741
5742           if (match != error_mark_node)
5743             matches = tree_cons (match, NULL_TREE, NULL_TREE);
5744         }
5745     }
5746
5747   /* Now we should have exactly one function in MATCHES.  */
5748   if (matches == NULL_TREE)
5749     {
5750       /* There were *no* matches.  */
5751       if (complain)
5752         {
5753           cp_error ("no matches converting function `%D' to type `%#T'", 
5754                     DECL_NAME (OVL_FUNCTION (overload)),
5755                     target_type);
5756
5757           /* print_candidates expects a chain with the functions in
5758              TREE_VALUE slots, so we cons one up here (we're losing anyway,
5759              so why be clever?).  */
5760           for (; overload; overload = OVL_NEXT (overload))
5761             matches = tree_cons (NULL_TREE, OVL_CURRENT (overload),
5762                                  matches);
5763           
5764           print_candidates (matches);
5765         }
5766       return error_mark_node;
5767     }
5768   else if (TREE_CHAIN (matches))
5769     {
5770       /* There were too many matches.  */
5771
5772       if (complain)
5773         {
5774           tree match;
5775
5776           cp_error ("converting overloaded function `%D' to type `%#T' is ambiguous", 
5777                     DECL_NAME (OVL_FUNCTION (overload)),
5778                     target_type);
5779
5780           /* Since print_candidates expects the functions in the
5781              TREE_VALUE slot, we flip them here.  */
5782           for (match = matches; match; match = TREE_CHAIN (match))
5783             TREE_VALUE (match) = TREE_PURPOSE (match);
5784
5785           print_candidates (matches);
5786         }
5787       
5788       return error_mark_node;
5789     }
5790
5791   /* Good, exactly one match.  Now, convert it to the correct type.  */
5792   fn = TREE_PURPOSE (matches);
5793
5794   mark_used (fn);
5795
5796   if (TYPE_PTRFN_P (target_type) || TYPE_PTRMEMFUNC_P (target_type))
5797     return build_unary_op (ADDR_EXPR, fn, 0);
5798   else
5799     {
5800       /* The target must be a REFERENCE_TYPE.  Above, build_unary_op
5801          will mark the function as addressed, but here we must do it
5802          explicitly.  */
5803       mark_addressable (fn);
5804
5805       return fn;
5806     }
5807 }
5808
5809 /* This function will instantiate the type of the expression given in
5810    RHS to match the type of LHSTYPE.  If errors exist, then return
5811    error_mark_node.  We only complain is COMPLAIN is set.  If we are
5812    not complaining, never modify rhs, as overload resolution wants to
5813    try many possible instantiations, in hopes that at least one will
5814    work.
5815
5816    FLAGS is a bitmask, as we see at the top of the function.
5817
5818    For non-recursive calls, LHSTYPE should be a function, pointer to
5819    function, or a pointer to member function.  */
5820
5821 tree
5822 instantiate_type (lhstype, rhs, flags)
5823      tree lhstype, rhs;
5824      int flags;
5825 {
5826   int complain = (flags & 1);
5827   int strict = (flags & 2) ? COMPARE_NO_ATTRIBUTES : COMPARE_STRICT;
5828   tree r;
5829
5830   if (TREE_CODE (lhstype) == UNKNOWN_TYPE)
5831     {
5832       if (complain)
5833         error ("not enough type information");
5834       return error_mark_node;
5835     }
5836
5837   if (TREE_TYPE (rhs) != NULL_TREE && ! (type_unknown_p (rhs)))
5838     {
5839       if (comptypes (lhstype, TREE_TYPE (rhs), strict))
5840         return rhs;
5841       if (complain)
5842         cp_error ("argument of type `%T' does not match `%T'",
5843                   TREE_TYPE (rhs), lhstype);
5844       return error_mark_node;
5845     }
5846
5847   /* We don't overwrite rhs if it is an overloaded function.
5848      Copying it would destroy the tree link.  */
5849   if (TREE_CODE (rhs) != OVERLOAD)
5850     rhs = copy_node (rhs);
5851
5852   /* This should really only be used when attempting to distinguish
5853      what sort of a pointer to function we have.  For now, any
5854      arithmetic operation which is not supported on pointers
5855      is rejected as an error.  */
5856
5857   switch (TREE_CODE (rhs))
5858     {
5859     case TYPE_EXPR:
5860     case CONVERT_EXPR:
5861     case SAVE_EXPR:
5862     case CONSTRUCTOR:
5863     case BUFFER_REF:
5864       my_friendly_abort (177);
5865       return error_mark_node;
5866
5867     case INDIRECT_REF:
5868     case ARRAY_REF:
5869       {
5870         tree new_rhs;
5871
5872         new_rhs = instantiate_type (build_pointer_type (lhstype),
5873                                     TREE_OPERAND (rhs, 0), flags);
5874         if (new_rhs == error_mark_node)
5875           return error_mark_node;
5876
5877         TREE_TYPE (rhs) = lhstype;
5878         TREE_OPERAND (rhs, 0) = new_rhs;
5879         return rhs;
5880       }
5881
5882     case NOP_EXPR:
5883       rhs = copy_node (TREE_OPERAND (rhs, 0));
5884       TREE_TYPE (rhs) = unknown_type_node;
5885       return instantiate_type (lhstype, rhs, flags);
5886
5887     case COMPONENT_REF:
5888       {
5889         r = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
5890
5891       comp:
5892         if (r != error_mark_node && TYPE_PTRMEMFUNC_P (lhstype)
5893             && complain && !flag_ms_extensions)
5894           {
5895             /* Note: we check this after the recursive call to avoid
5896                complaining about cases where overload resolution fails.  */
5897
5898             tree t = TREE_TYPE (TREE_OPERAND (rhs, 0));
5899             tree fn = PTRMEM_CST_MEMBER (r);
5900
5901             my_friendly_assert (TREE_CODE (r) == PTRMEM_CST, 990811);
5902
5903             cp_pedwarn
5904               ("object-dependent reference to `%E' can only be used in a call",
5905                DECL_NAME (fn));
5906             cp_pedwarn
5907               ("  to form a pointer to member function, say `&%T::%E'",
5908                t, DECL_NAME (fn));
5909           }
5910
5911         return r;
5912       }
5913
5914     case OFFSET_REF:
5915       rhs = TREE_OPERAND (rhs, 1);
5916       if (BASELINK_P (rhs))
5917         return instantiate_type (lhstype, TREE_VALUE (rhs), flags);
5918
5919       /* This can happen if we are forming a pointer-to-member for a
5920          member template.  */
5921       my_friendly_assert (TREE_CODE (rhs) == TEMPLATE_ID_EXPR, 0);
5922
5923       /* Fall through.  */
5924
5925     case TEMPLATE_ID_EXPR:
5926       {
5927         tree fns = TREE_OPERAND (rhs, 0);
5928         tree args = TREE_OPERAND (rhs, 1);
5929
5930         r =
5931           resolve_address_of_overloaded_function (lhstype,
5932                                                   fns,
5933                                                   complain,
5934                                                   /*template_only=*/1,
5935                                                   args);
5936         if (TREE_CODE (fns) == COMPONENT_REF)
5937           {
5938             rhs = fns;
5939             goto comp;
5940           }
5941         return r;
5942       }
5943
5944     case OVERLOAD:
5945       return 
5946         resolve_address_of_overloaded_function (lhstype, 
5947                                                 rhs,
5948                                                 complain,
5949                                                 /*template_only=*/0,
5950                                                 /*explicit_targs=*/NULL_TREE);
5951
5952     case TREE_LIST:
5953       /* Now we should have a baselink. */
5954       my_friendly_assert (BASELINK_P (rhs), 990412);
5955
5956       return instantiate_type (lhstype, TREE_VALUE (rhs), flags);
5957
5958     case CALL_EXPR:
5959       /* This is too hard for now.  */
5960       my_friendly_abort (183);
5961       return error_mark_node;
5962
5963     case PLUS_EXPR:
5964     case MINUS_EXPR:
5965     case COMPOUND_EXPR:
5966       TREE_OPERAND (rhs, 0)
5967         = instantiate_type (lhstype, TREE_OPERAND (rhs, 0), flags);
5968       if (TREE_OPERAND (rhs, 0) == error_mark_node)
5969         return error_mark_node;
5970       TREE_OPERAND (rhs, 1)
5971         = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
5972       if (TREE_OPERAND (rhs, 1) == error_mark_node)
5973         return error_mark_node;
5974
5975       TREE_TYPE (rhs) = lhstype;
5976       return rhs;
5977
5978     case MULT_EXPR:
5979     case TRUNC_DIV_EXPR:
5980     case FLOOR_DIV_EXPR:
5981     case CEIL_DIV_EXPR:
5982     case ROUND_DIV_EXPR:
5983     case RDIV_EXPR:
5984     case TRUNC_MOD_EXPR:
5985     case FLOOR_MOD_EXPR:
5986     case CEIL_MOD_EXPR:
5987     case ROUND_MOD_EXPR:
5988     case FIX_ROUND_EXPR:
5989     case FIX_FLOOR_EXPR:
5990     case FIX_CEIL_EXPR:
5991     case FIX_TRUNC_EXPR:
5992     case FLOAT_EXPR:
5993     case NEGATE_EXPR:
5994     case ABS_EXPR:
5995     case MAX_EXPR:
5996     case MIN_EXPR:
5997     case FFS_EXPR:
5998
5999     case BIT_AND_EXPR:
6000     case BIT_IOR_EXPR:
6001     case BIT_XOR_EXPR:
6002     case LSHIFT_EXPR:
6003     case RSHIFT_EXPR:
6004     case LROTATE_EXPR:
6005     case RROTATE_EXPR:
6006
6007     case PREINCREMENT_EXPR:
6008     case PREDECREMENT_EXPR:
6009     case POSTINCREMENT_EXPR:
6010     case POSTDECREMENT_EXPR:
6011       if (complain)
6012         error ("invalid operation on uninstantiated type");
6013       return error_mark_node;
6014
6015     case TRUTH_AND_EXPR:
6016     case TRUTH_OR_EXPR:
6017     case TRUTH_XOR_EXPR:
6018     case LT_EXPR:
6019     case LE_EXPR:
6020     case GT_EXPR:
6021     case GE_EXPR:
6022     case EQ_EXPR:
6023     case NE_EXPR:
6024     case TRUTH_ANDIF_EXPR:
6025     case TRUTH_ORIF_EXPR:
6026     case TRUTH_NOT_EXPR:
6027       if (complain)
6028         error ("not enough type information");
6029       return error_mark_node;
6030
6031     case COND_EXPR:
6032       if (type_unknown_p (TREE_OPERAND (rhs, 0)))
6033         {
6034           if (complain)
6035             error ("not enough type information");
6036           return error_mark_node;
6037         }
6038       TREE_OPERAND (rhs, 1)
6039         = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
6040       if (TREE_OPERAND (rhs, 1) == error_mark_node)
6041         return error_mark_node;
6042       TREE_OPERAND (rhs, 2)
6043         = instantiate_type (lhstype, TREE_OPERAND (rhs, 2), flags);
6044       if (TREE_OPERAND (rhs, 2) == error_mark_node)
6045         return error_mark_node;
6046
6047       TREE_TYPE (rhs) = lhstype;
6048       return rhs;
6049
6050     case MODIFY_EXPR:
6051       TREE_OPERAND (rhs, 1)
6052         = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
6053       if (TREE_OPERAND (rhs, 1) == error_mark_node)
6054         return error_mark_node;
6055
6056       TREE_TYPE (rhs) = lhstype;
6057       return rhs;
6058       
6059     case ADDR_EXPR:
6060       return instantiate_type (lhstype, TREE_OPERAND (rhs, 0), flags);
6061
6062     case ENTRY_VALUE_EXPR:
6063       my_friendly_abort (184);
6064       return error_mark_node;
6065
6066     case ERROR_MARK:
6067       return error_mark_node;
6068
6069     default:
6070       my_friendly_abort (185);
6071       return error_mark_node;
6072     }
6073 }
6074 \f
6075 /* Return the name of the virtual function pointer field
6076    (as an IDENTIFIER_NODE) for the given TYPE.  Note that
6077    this may have to look back through base types to find the
6078    ultimate field name.  (For single inheritance, these could
6079    all be the same name.  Who knows for multiple inheritance).  */
6080
6081 static tree
6082 get_vfield_name (type)
6083      tree type;
6084 {
6085   tree binfo = TYPE_BINFO (type);
6086   char *buf;
6087
6088   while (BINFO_BASETYPES (binfo)
6089          && TYPE_CONTAINS_VPTR_P (BINFO_TYPE (BINFO_BASETYPE (binfo, 0)))
6090          && ! TREE_VIA_VIRTUAL (BINFO_BASETYPE (binfo, 0)))
6091     binfo = BINFO_BASETYPE (binfo, 0);
6092
6093   type = BINFO_TYPE (binfo);
6094   buf = (char *) alloca (sizeof (VFIELD_NAME_FORMAT)
6095                          + TYPE_NAME_LENGTH (type) + 2);
6096   sprintf (buf, VFIELD_NAME_FORMAT, TYPE_NAME_STRING (type));
6097   return get_identifier (buf);
6098 }
6099
6100 void
6101 print_class_statistics ()
6102 {
6103 #ifdef GATHER_STATISTICS
6104   fprintf (stderr, "convert_harshness = %d\n", n_convert_harshness);
6105   fprintf (stderr, "compute_conversion_costs = %d\n", n_compute_conversion_costs);
6106   fprintf (stderr, "build_method_call = %d (inner = %d)\n",
6107            n_build_method_call, n_inner_fields_searched);
6108   if (n_vtables)
6109     {
6110       fprintf (stderr, "vtables = %d; vtable searches = %d\n",
6111                n_vtables, n_vtable_searches);
6112       fprintf (stderr, "vtable entries = %d; vtable elems = %d\n",
6113                n_vtable_entries, n_vtable_elems);
6114     }
6115 #endif
6116 }
6117
6118 /* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
6119    according to [class]:
6120                                           The class-name is also inserted
6121    into  the scope of the class itself.  For purposes of access checking,
6122    the inserted class name is treated as if it were a public member name.  */
6123
6124 void
6125 build_self_reference ()
6126 {
6127   tree name = constructor_name (current_class_type);
6128   tree value = build_lang_decl (TYPE_DECL, name, current_class_type);
6129   tree saved_cas;
6130
6131   DECL_NONLOCAL (value) = 1;
6132   DECL_CONTEXT (value) = current_class_type;
6133   DECL_ARTIFICIAL (value) = 1;
6134
6135   if (processing_template_decl)
6136     value = push_template_decl (value);
6137
6138   saved_cas = current_access_specifier;
6139   current_access_specifier = access_public_node;
6140   finish_member_declaration (value);
6141   current_access_specifier = saved_cas;
6142 }
6143
6144 /* Returns 1 if TYPE contains only padding bytes.  */
6145
6146 int
6147 is_empty_class (type)
6148      tree type;
6149 {
6150   tree t;
6151
6152   if (type == error_mark_node)
6153     return 0;
6154
6155   if (! IS_AGGR_TYPE (type))
6156     return 0;
6157
6158   if (flag_new_abi)
6159     return integer_zerop (CLASSTYPE_SIZE (type));
6160
6161   if (TYPE_BINFO_BASETYPES (type))
6162     return 0;
6163   t = TYPE_FIELDS (type);
6164   while (t && TREE_CODE (t) != FIELD_DECL)
6165     t = TREE_CHAIN (t);
6166   return (t == NULL_TREE);
6167 }
6168
6169 /* Find the enclosing class of the given NODE.  NODE can be a *_DECL or
6170    a *_TYPE node.  NODE can also be a local class.  */
6171
6172 tree
6173 get_enclosing_class (type)
6174      tree type;
6175 {
6176   tree node = type;
6177
6178   while (node && TREE_CODE (node) != NAMESPACE_DECL)
6179     {
6180       switch (TREE_CODE_CLASS (TREE_CODE (node)))
6181         {
6182         case 'd':
6183           node = DECL_CONTEXT (node);
6184           break;
6185
6186         case 't':
6187           if (node != type)
6188             return node;
6189           node = TYPE_CONTEXT (node);
6190           break;
6191
6192         default:
6193           my_friendly_abort (0);
6194         }
6195     }
6196   return NULL_TREE;
6197 }
6198
6199 /* Return 1 if TYPE or one of its enclosing classes is derived from BASE.  */
6200
6201 int
6202 is_base_of_enclosing_class (base, type)
6203      tree base, type;
6204 {
6205   while (type)
6206     {
6207       if (get_binfo (base, type, 0))
6208         return 1;
6209
6210       type = get_enclosing_class (type);
6211     }
6212   return 0;
6213 }
6214
6215 /* Note that NAME was looked up while the current class was being
6216    defined and that the result of that lookup was DECL.  */
6217
6218 void
6219 maybe_note_name_used_in_class (name, decl)
6220      tree name;
6221      tree decl;
6222 {
6223   splay_tree names_used;
6224
6225   /* If we're not defining a class, there's nothing to do.  */
6226   if (!current_class_type || !TYPE_BEING_DEFINED (current_class_type))
6227     return;
6228   
6229   /* If there's already a binding for this NAME, then we don't have
6230      anything to worry about.  */
6231   if (IDENTIFIER_CLASS_VALUE (name))
6232     return;
6233
6234   if (!current_class_stack[current_class_depth - 1].names_used)
6235     current_class_stack[current_class_depth - 1].names_used
6236       = splay_tree_new (splay_tree_compare_pointers, 0, 0);
6237   names_used = current_class_stack[current_class_depth - 1].names_used;
6238
6239   splay_tree_insert (names_used,
6240                      (splay_tree_key) name, 
6241                      (splay_tree_value) decl);
6242 }
6243
6244 /* Note that NAME was declared (as DECL) in the current class.  Check
6245    to see that the declaration is legal.  */
6246
6247 void
6248 note_name_declared_in_class (name, decl)
6249      tree name;
6250      tree decl;
6251 {
6252   splay_tree names_used;
6253   splay_tree_node n;
6254
6255   /* Look to see if we ever used this name.  */
6256   names_used 
6257     = current_class_stack[current_class_depth - 1].names_used;
6258   if (!names_used)
6259     return;
6260
6261   n = splay_tree_lookup (names_used, (splay_tree_key) name);
6262   if (n)
6263     {
6264       /* [basic.scope.class]
6265          
6266          A name N used in a class S shall refer to the same declaration
6267          in its context and when re-evaluated in the completed scope of
6268          S.  */
6269       cp_error ("declaration of `%#D'", decl);
6270       cp_error_at ("changes meaning of `%s' from `%+#D'", 
6271                    IDENTIFIER_POINTER (DECL_NAME (OVL_CURRENT (decl))),
6272                    (tree) n->value);
6273     }
6274 }
6275
6276 /* Returns the VAR_DECL for the complete vtable associated with
6277    BINFO.  (Under the new ABI, secondary vtables are merged with
6278    primary vtables; this function will return the VAR_DECL for the
6279    primary vtable.)  */
6280
6281 tree
6282 get_vtbl_decl_for_binfo (binfo)
6283      tree binfo;
6284 {
6285   tree decl;
6286
6287   decl = BINFO_VTABLE (binfo);
6288   if (decl && TREE_CODE (decl) == PLUS_EXPR)
6289     {
6290       my_friendly_assert (TREE_CODE (TREE_OPERAND (decl, 0)) == ADDR_EXPR,
6291                           2000403);
6292       decl = TREE_OPERAND (TREE_OPERAND (decl, 0), 0);
6293     }
6294   if (decl)
6295     my_friendly_assert (TREE_CODE (decl) == VAR_DECL, 20000403);
6296   return decl;
6297 }
6298
6299 /* Dump the offsets of all the bases rooted at BINFO (in the hierarchy
6300    dominated by T) to stderr.  INDENT should be zero when called from
6301    the top level; it is incremented recursively.  */
6302
6303 static void
6304 dump_class_hierarchy_r (t, binfo, indent)
6305      tree t;
6306      tree binfo;
6307      int indent;
6308 {
6309   int i;
6310
6311   fprintf (stderr, "%*s0x%lx (%s) ", indent, "",
6312            (unsigned long) binfo,
6313            type_as_string (binfo, TS_PLAIN));
6314   fprintf (stderr, HOST_WIDE_INT_PRINT_DEC,
6315            tree_low_cst (BINFO_OFFSET (binfo), 0));
6316   if (TREE_VIA_VIRTUAL (binfo))
6317     fprintf (stderr, " virtual");
6318   if (BINFO_PRIMARY_MARKED_P (binfo)
6319       || (TREE_VIA_VIRTUAL (binfo) 
6320           && BINFO_VBASE_PRIMARY_P (BINFO_FOR_VBASE (BINFO_TYPE (binfo), 
6321                                                      t))))
6322     fprintf (stderr, " primary");
6323   fprintf (stderr, "\n");
6324
6325   for (i = 0; i < BINFO_N_BASETYPES (binfo); ++i)
6326     dump_class_hierarchy_r (t, BINFO_BASETYPE (binfo, i), indent + 2);
6327 }
6328
6329 /* Dump the BINFO hierarchy for T.  */
6330
6331 void
6332 dump_class_hierarchy (t)
6333      tree t;
6334 {
6335   tree vbase;
6336
6337   dump_class_hierarchy_r (t, TYPE_BINFO (t), 0);
6338   fprintf (stderr, "virtual bases\n");
6339   for (vbase = CLASSTYPE_VBASECLASSES (t); vbase; vbase = TREE_CHAIN (vbase))
6340     dump_class_hierarchy_r (t, vbase, 0);
6341 }
6342
6343 /* Virtual function table initialization.  */
6344
6345 /* Create all the necessary vtables for T and its base classes.  */
6346
6347 static void
6348 finish_vtbls (t)
6349      tree t;
6350 {
6351   if (merge_primary_and_secondary_vtables_p ())
6352     {
6353       tree list;
6354       tree vbase;
6355
6356       /* Under the new ABI, we lay out the primary and secondary
6357          vtables in one contiguous vtable.  The primary vtable is
6358          first, followed by the non-virtual secondary vtables in
6359          inheritance graph order.  */
6360       list = build_tree_list (TYPE_BINFO_VTABLE (t), NULL_TREE);
6361       TREE_TYPE (list) = t;
6362       accumulate_vtbl_inits (TYPE_BINFO (t), list);
6363       /* Then come the virtual bases, also in inheritance graph
6364          order.  */
6365       for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
6366         {
6367           if (!TREE_VIA_VIRTUAL (vbase))
6368             continue;
6369           accumulate_vtbl_inits (BINFO_FOR_VBASE (BINFO_TYPE (vbase), t),
6370                                  list);
6371         }
6372
6373       if (TYPE_BINFO_VTABLE (t))
6374         initialize_vtable (TYPE_BINFO (t), TREE_VALUE (list));
6375     }
6376   else
6377     {
6378       dfs_walk (TYPE_BINFO (t), dfs_finish_vtbls, 
6379                 dfs_unmarked_real_bases_queue_p, t);
6380       dfs_walk (TYPE_BINFO (t), dfs_unmark, 
6381                 dfs_marked_real_bases_queue_p, t);
6382     }
6383 }
6384
6385 /* Called from finish_vtbls via dfs_walk.  */
6386
6387 static tree
6388 dfs_finish_vtbls (binfo, data)
6389      tree binfo;
6390      void *data;
6391 {
6392   tree t = (tree) data;
6393
6394   if (!BINFO_PRIMARY_MARKED_P (binfo)
6395       && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo))
6396       && BINFO_NEW_VTABLE_MARKED (binfo, t))
6397     initialize_vtable (binfo, 
6398                        build_vtbl_initializer (binfo, t, NULL));
6399
6400   CLEAR_BINFO_NEW_VTABLE_MARKED (binfo, t);
6401   SET_BINFO_MARKED (binfo);
6402
6403   return NULL_TREE;
6404 }
6405
6406 /* Initialize the vtable for BINFO with the INITS.  */
6407
6408 static void
6409 initialize_vtable (binfo, inits)
6410      tree binfo;
6411      tree inits;
6412 {
6413   tree context;
6414   tree decl;
6415
6416   layout_vtable_decl (binfo, list_length (inits));
6417   decl = get_vtbl_decl_for_binfo (binfo);
6418   context = DECL_CONTEXT (decl);
6419   DECL_CONTEXT (decl) = 0;
6420   DECL_INITIAL (decl) = build_nt (CONSTRUCTOR, NULL_TREE, inits);
6421   cp_finish_decl (decl, DECL_INITIAL (decl), NULL_TREE, 0);
6422   DECL_CONTEXT (decl) = context;
6423 }
6424
6425 /* Add the vtbl initializers for BINFO (and its non-primary,
6426    non-virtual bases) to the list of INITS.  */
6427
6428 static void
6429 accumulate_vtbl_inits (binfo, inits)
6430      tree binfo;
6431      tree inits;
6432 {
6433   /* Walk the BINFO and its bases.  We walk in preorder so that as we
6434      initialize each vtable we can figure out at what offset the
6435      secondary vtable lies from the primary vtable.  */
6436   dfs_walk_real (binfo,
6437                  dfs_accumulate_vtbl_inits,
6438                  NULL,
6439                  dfs_skip_vbases,
6440                  inits);
6441 }
6442
6443 /* Called from finish_vtbls via dfs_walk when using the new ABI.
6444    Accumulates the vtable initializers for all of the vtables into
6445    TREE_VALUE (DATA).  */
6446
6447 static tree
6448 dfs_accumulate_vtbl_inits (binfo, data)
6449      tree binfo;
6450      void *data;
6451 {
6452   tree l;
6453   tree t;
6454
6455   l = (tree) data;
6456   t = TREE_TYPE (l);
6457
6458   if (!BINFO_PRIMARY_MARKED_P (binfo)
6459       && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo))
6460       && BINFO_NEW_VTABLE_MARKED (binfo, t))
6461     {
6462       tree inits;
6463       tree vtbl;
6464       tree index;
6465       int non_fn_entries;
6466
6467       /* Compute the initializer for this vtable.  */
6468       inits = build_vtbl_initializer (binfo, t, &non_fn_entries);
6469
6470       /* Set BINFO_VTABLE to the address where the VPTR should point.  */
6471       vtbl = TREE_PURPOSE (l);
6472       vtbl = build1 (ADDR_EXPR, 
6473                      build_pointer_type (TREE_TYPE (vtbl)),
6474                      vtbl);
6475       index = size_binop (PLUS_EXPR,
6476                           size_int (non_fn_entries),
6477                           size_int (list_length (TREE_VALUE (l))));
6478       BINFO_VTABLE (binfo)
6479         = build (PLUS_EXPR, TREE_TYPE (vtbl), vtbl,
6480                  size_binop (MULT_EXPR,
6481                              TYPE_SIZE_UNIT (TREE_TYPE (vtbl)),
6482                              index));
6483
6484       /* Add the initializers for this vtable to the initializers for
6485          the other vtables we've already got.  */
6486       TREE_VALUE (l) = chainon (TREE_VALUE (l), inits);
6487     }
6488
6489   CLEAR_BINFO_NEW_VTABLE_MARKED (binfo, t);
6490
6491   return NULL_TREE;
6492 }
6493
6494 /* Construct the initializer for BINFOs virtual function table.  BINFO
6495    is part of the hierarchy dominated by T.  The value returned is a
6496    TREE_LIST suitable for wrapping in a CONSTRUCTOR to use as the
6497    DECL_INITIAL for a vtable.  If NON_FN_ENTRIES_P is not NULL,
6498    *NON_FN_ENTRIES_P is set to the number of non-function entries in
6499    the vtable.  */
6500
6501 static tree
6502 build_vtbl_initializer (binfo, t, non_fn_entries_p)
6503      tree binfo;
6504      tree t;
6505      int *non_fn_entries_p;
6506 {
6507   tree v = BINFO_VIRTUALS (binfo);
6508   tree inits = NULL_TREE;
6509   tree vfun_inits;
6510   tree vbase;
6511   vcall_offset_data vod;
6512
6513   /* Initialize those parts of VOD that matter.  */
6514   vod.derived = t;
6515   vod.inits = NULL_TREE;
6516   vod.primary_p = (binfo == TYPE_BINFO (t));
6517   /* The first vbase or vcall offset is at index -3 in the vtable.  */
6518   vod.index = build_int_2 (-3, -1);
6519
6520   /* Add the vcall and vbase offset entries.  */
6521   build_vcall_and_vbase_vtbl_entries (binfo, &vod);
6522   inits = vod.inits;
6523   /* Clear BINFO_VTABLE_PAATH_MARKED; it's set by
6524      build_vbase_offset_vtbl_entries.  */
6525   for (vbase = CLASSTYPE_VBASECLASSES (t); 
6526        vbase; 
6527        vbase = TREE_CHAIN (vbase))
6528     CLEAR_BINFO_VTABLE_PATH_MARKED (vbase);
6529
6530   /* Add entries to the vtable for RTTI.  */
6531   inits = chainon (inits, build_rtti_vtbl_entries (binfo, t));
6532
6533   if (non_fn_entries_p)
6534     *non_fn_entries_p = list_length (inits);
6535
6536   /* Go through all the ordinary virtual functions, building up
6537      initializers.  */
6538   vfun_inits = NULL_TREE;
6539   while (v)
6540     {
6541       tree delta;
6542       tree vcall_index;
6543       tree fn;
6544       tree pfn;
6545       tree init;
6546
6547       /* Pull the offset for `this', and the function to call, out of
6548          the list.  */
6549       delta = BV_DELTA (v);
6550       vcall_index = BV_VCALL_INDEX (v);
6551       fn = BV_FN (v);
6552       my_friendly_assert (TREE_CODE (delta) == INTEGER_CST, 19990727);
6553       my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL, 19990727);
6554
6555       /* You can't call an abstract virtual function; it's abstract.
6556          So, we replace these functions with __pure_virtual.  */
6557       if (DECL_PURE_VIRTUAL_P (fn))
6558         fn = abort_fndecl;
6559
6560       /* Take the address of the function, considering it to be of an
6561          appropriate generic type.  */
6562       pfn = build1 (ADDR_EXPR, vfunc_ptr_type_node, fn);
6563       /* The address of a function can't change.  */
6564       TREE_CONSTANT (pfn) = 1;
6565       /* Enter it in the vtable.  */
6566       init = build_vtable_entry (delta, vcall_index, pfn);
6567       /* And add it to the chain of initializers.  */
6568       vfun_inits = tree_cons (NULL_TREE, init, vfun_inits);
6569
6570       /* Keep going.  */
6571       v = TREE_CHAIN (v);
6572     }
6573
6574   /* The initializers for virtual functions were built up in reverse
6575      order; straighten them out now.  */
6576   vfun_inits = nreverse (vfun_inits);
6577   
6578   /* The complete initializer is the INITS, followed by the
6579      VFUN_INITS.  */
6580   return chainon (inits, vfun_inits);
6581 }
6582
6583 /* Sets vod->inits to be the initializers for the vbase and vcall
6584    offsets in BINFO, which is in the hierarchy dominated by T.  */
6585
6586 static void
6587 build_vcall_and_vbase_vtbl_entries (binfo, vod)
6588      tree binfo;
6589      vcall_offset_data *vod;
6590 {
6591   tree b;
6592   tree inits;
6593
6594   /* If this is a derived class, we must first create entries
6595      corresponding to the base class.  These entries must go closer to
6596      the vptr, so we save them up and add them to the end of the list
6597      later.  */
6598   inits = vod->inits;
6599   vod->inits = NULL_TREE;
6600   b = BINFO_PRIMARY_BINFO (binfo);
6601   if (b)
6602     build_vcall_and_vbase_vtbl_entries (b, vod);
6603
6604   /* Add the vbase entries for this base.  */
6605   build_vbase_offset_vtbl_entries (binfo, vod);
6606   /* Add the vcall entries for this base.  */
6607   build_vcall_offset_vtbl_entries (binfo, vod);
6608
6609   vod->inits = chainon (vod->inits, inits);
6610 }
6611
6612 /* Returns the initializers for the vbase offset entries in the vtable
6613    for BINFO (which is part of the class hierarchy dominated by T), in
6614    reverse order.  VBASE_OFFSET_INDEX gives the vtable index
6615    where the next vbase offset will go.  */
6616
6617 static void
6618 build_vbase_offset_vtbl_entries (binfo, vod)
6619      tree binfo;
6620      vcall_offset_data *vod;
6621 {
6622   tree vbase;
6623   tree t;
6624
6625   /* Under the old ABI, pointers to virtual bases are stored in each
6626      object.  */
6627   if (!vbase_offsets_in_vtable_p ())
6628     return;
6629
6630   /* If there are no virtual baseclasses, then there is nothing to
6631      do.  */
6632   if (!TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo)))
6633     return;
6634
6635   t = vod->derived;
6636
6637   /* Go through the virtual bases, adding the offsets.  */
6638   for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
6639        vbase;
6640        vbase = TREE_CHAIN (vbase))
6641     {
6642       tree b;
6643       tree delta;
6644       
6645       if (!TREE_VIA_VIRTUAL (vbase))
6646         continue;
6647
6648       /* Find the instance of this virtual base in the complete
6649          object.  */
6650       b = BINFO_FOR_VBASE (BINFO_TYPE (vbase), t);
6651
6652       /* If we've already got an offset for this virtual base, we
6653          don't need another one.  */
6654       if (BINFO_VTABLE_PATH_MARKED (b))
6655         continue;
6656       SET_BINFO_VTABLE_PATH_MARKED (b);
6657
6658       /* Figure out where we can find this vbase offset.  */
6659       delta = size_binop (MULT_EXPR, 
6660                           convert (ssizetype, vod->index),
6661                           convert (ssizetype,
6662                                    TYPE_SIZE_UNIT (vtable_entry_type)));
6663       if (vod->primary_p)
6664         BINFO_VPTR_FIELD (b) = delta;
6665
6666       if (binfo != TYPE_BINFO (t))
6667         {
6668           tree orig_vbase;
6669
6670           /* Find the instance of this virtual base in the type of BINFO.  */
6671           orig_vbase = BINFO_FOR_VBASE (BINFO_TYPE (vbase),
6672                                         BINFO_TYPE (binfo));
6673
6674           /* The vbase offset had better be the same.  */
6675           if (!tree_int_cst_equal (delta,
6676                                    BINFO_VPTR_FIELD (orig_vbase)))
6677             my_friendly_abort (20000403);
6678         }
6679
6680       /* The next vbase will come at a more negative offset.  */
6681       vod->index = fold (build (MINUS_EXPR, integer_type_node,
6682                                 vod->index, integer_one_node));
6683
6684       /* The initializer is the delta from BINFO to this virtual base.
6685          The vbase offsets go in reverse inheritance-graph order, and
6686          we are walking in inheritance graph order so these end up in
6687          the right order.  */
6688       delta = size_diffop (BINFO_OFFSET (b), BINFO_OFFSET (binfo));
6689       vod->inits = tree_cons (NULL_TREE, 
6690                               fold (build1 (NOP_EXPR, 
6691                                             vtable_entry_type,
6692                                             delta)),
6693                               vod->inits);
6694     }
6695 }
6696
6697 /* Called from build_vcall_offset_vtbl_entries via dfs_walk.  */
6698
6699 static tree
6700 dfs_build_vcall_offset_vtbl_entries (binfo, data)
6701      tree binfo;
6702      void *data;
6703 {
6704   vcall_offset_data* vod;
6705   tree derived_virtuals;
6706   tree base_virtuals;
6707   tree binfo_inits;
6708   tree non_primary_binfo;
6709   tree b;
6710   int i;
6711
6712   vod = (vcall_offset_data *) data;
6713   binfo_inits = NULL_TREE;
6714
6715   /* We might be a primary base class.  Go up the inheritance
6716      hierarchy until we find the class of which we are a primary base:
6717      it is the BINFO_VIRTUALS there that we need to consider.  */
6718   non_primary_binfo = binfo;
6719   while (BINFO_PRIMARY_MARKED_P (non_primary_binfo))
6720     non_primary_binfo = BINFO_INHERITANCE_CHAIN (non_primary_binfo);
6721
6722   /* Skip virtuals that we have already handled in a primary base
6723      class.  */
6724   base_virtuals = BINFO_VIRTUALS (binfo);
6725   derived_virtuals = BINFO_VIRTUALS (non_primary_binfo);
6726   b = BINFO_PRIMARY_BINFO (binfo);
6727   if (b)
6728     for (i = 0; i < CLASSTYPE_VSIZE (BINFO_TYPE (b)); ++i)
6729       {
6730         base_virtuals = TREE_CHAIN (base_virtuals);
6731         derived_virtuals = TREE_CHAIN (derived_virtuals);
6732       }
6733
6734   /* Make entries for the rest of the virtuals.  */
6735   while (base_virtuals)
6736     {
6737       /* Figure out what function we're looking at.  */
6738       tree fn = TREE_VALUE (derived_virtuals);
6739       tree base = DECL_CONTEXT (fn);
6740       /* The FN comes from BASE.  So, we must caculate the adjustment
6741          from the virtual base that derived from BINFO to BASE.  */
6742       tree base_binfo = get_binfo (base, vod->derived, /*protect=*/0);
6743
6744       /* Compute the vcall offset.  */
6745       binfo_inits
6746         = tree_cons (NULL_TREE,
6747                      fold (build1 (NOP_EXPR, vtable_entry_type,
6748                                    size_diffop (BINFO_OFFSET (base_binfo),
6749                                                 BINFO_OFFSET (vod->vbase)))),
6750                      binfo_inits);
6751
6752       /* Keep track of the vtable index where this vcall offset can be
6753          found.  */
6754       BV_VCALL_INDEX (derived_virtuals) = vod->index;
6755       /* The next vcall offset will be found at a more negative
6756          offset.  */
6757       vod->index = fold (build (MINUS_EXPR, integer_type_node,
6758                                 vod->index, integer_one_node));
6759
6760       /* Go to the next entries in the list.  */
6761       derived_virtuals = TREE_CHAIN (derived_virtuals);
6762       base_virtuals = TREE_CHAIN (base_virtuals);
6763     }
6764
6765   /* The offests are built up in reverse order, so we straighten them
6766      here.  We simultaneously add them to VOD->INITS; we're walking
6767      the bases in inheritance graph order, and the initializers are
6768      supposed to appear in reverse inheritance order, so that's
6769      correct.  */
6770   while (binfo_inits)
6771     {
6772       tree next;
6773
6774       next = TREE_CHAIN (binfo_inits);
6775       TREE_CHAIN (binfo_inits) = vod->inits;
6776       vod->inits = binfo_inits;
6777       binfo_inits = next;
6778     }
6779
6780   return NULL_TREE;
6781 }
6782
6783 /* Adds the initializers for the vcall offset entries in the vtable
6784    for BINFO (which is part of the class hierarchy dominated by T) to
6785    VOD->INITS.  */
6786
6787 static void
6788 build_vcall_offset_vtbl_entries (binfo, vod)
6789      tree binfo;
6790      vcall_offset_data *vod;
6791 {
6792   tree inits;
6793
6794   /* Under the old ABI, the adjustments to the `this' pointer were made
6795      elsewhere.  */
6796   if (!vcall_offsets_in_vtable_p ())
6797     return;
6798
6799   /* We only need these entries if this base is a virtual base.  */
6800   if (!TREE_VIA_VIRTUAL (binfo))
6801     return;
6802
6803   /* We need a vcall offset for each of the virtual functions in this
6804      vtable.  For example:
6805
6806        class A { virtual void f (); };
6807        class B : virtual public A { };
6808        class C: virtual public A, public B {};
6809       
6810      Now imagine:
6811
6812        B* b = new C;
6813        b->f();
6814
6815      The location of `A' is not at a fixed offset relative to `B'; the
6816      offset depends on the complete object derived from `B'.  So, 
6817      `B' vtable contains an entry for `f' that indicates by what
6818      amount the `this' pointer for `B' needs to be adjusted to arrive
6819      at `A'.  
6820
6821      We need entries for all the functions in our primary vtable and
6822      in our non-virtual bases vtables.  For each base, the entries
6823      appear in the same order as in the base; but the bases themselves
6824      appear in reverse depth-first, left-to-right order.  */
6825   vod->vbase = binfo;
6826   inits = vod->inits;
6827   vod->inits = NULL_TREE;
6828   dfs_walk_real (binfo,
6829                  dfs_build_vcall_offset_vtbl_entries,
6830                  NULL,
6831                  dfs_skip_vbases,
6832                  vod);
6833   vod->inits = chainon (vod->inits, inits);
6834 }
6835
6836 /* Return vtbl initializers for the RTTI entries coresponding to the
6837    BINFO's vtable.  BINFO is a part of the hierarchy dominated by 
6838    T.  */
6839
6840 static tree
6841 build_rtti_vtbl_entries (binfo, t)
6842      tree binfo;
6843      tree t;
6844 {
6845   tree b;
6846   tree basetype;
6847   tree offset;
6848   tree decl;
6849   tree init;
6850   tree inits;
6851
6852   basetype = BINFO_TYPE (binfo);
6853   inits = NULL_TREE;
6854
6855   /* For a COM object there is no RTTI entry.  */
6856   if (CLASSTYPE_COM_INTERFACE (basetype))
6857     return inits;
6858
6859   /* To find the complete object, we will first convert to our most
6860      primary base, and then add the offset in the vtbl to that value.  */
6861   b = binfo;
6862   while (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (b)))
6863     {
6864       tree primary_base;
6865
6866       primary_base = BINFO_PRIMARY_BINFO (b);
6867       if (!BINFO_PRIMARY_MARKED_P (primary_base))
6868         break;
6869       b = primary_base;
6870     }
6871   offset = size_diffop (size_zero_node, BINFO_OFFSET (b));
6872
6873   /* The second entry is, in the case of the new ABI, the address of
6874      the typeinfo object, or, in the case of the old ABI, a function
6875      which returns a typeinfo object.  */
6876   if (new_abi_rtti_p ())
6877     {
6878       if (flag_rtti)
6879         decl = build_unary_op (ADDR_EXPR, get_tinfo_decl (t), 0);
6880       else
6881         decl = integer_zero_node;
6882
6883       /* Convert the declaration to a type that can be stored in the
6884          vtable.  */
6885       init = build1 (NOP_EXPR, vfunc_ptr_type_node, decl);
6886       TREE_CONSTANT (init) = 1;
6887     }
6888   else
6889     {
6890       if (flag_rtti)
6891         decl = get_tinfo_decl (t);
6892       else
6893         decl = abort_fndecl;
6894
6895       /* Convert the declaration to a type that can be stored in the
6896          vtable.  */
6897       init = build1 (ADDR_EXPR, vfunc_ptr_type_node, decl);
6898       TREE_CONSTANT (init) = 1;
6899       init = build_vtable_entry (offset, integer_zero_node, init);
6900     }
6901   inits = tree_cons (NULL_TREE, init, inits);
6902
6903   /* Add the offset-to-top entry.  It comes earlier in the vtable that
6904      the the typeinfo entry.  */
6905   if (flag_vtable_thunks)
6906     {
6907       /* Convert the offset to look like a function pointer, so that
6908          we can put it in the vtable.  */
6909       init = build1 (NOP_EXPR, vfunc_ptr_type_node, offset);
6910       TREE_CONSTANT (init) = 1;
6911       inits = tree_cons (NULL_TREE, init, inits);
6912     }
6913
6914   return inits;
6915 }
6916
6917 /* Build an entry in the virtual function table.  DELTA is the offset
6918    for the `this' pointer.  VCALL_INDEX is the vtable index containing
6919    the vcall offset; zero if none.  ENTRY is the virtual function
6920    table entry itself.  It's TREE_TYPE must be VFUNC_PTR_TYPE_NODE,
6921    but it may not actually be a virtual function table pointer.  (For
6922    example, it might be the address of the RTTI object, under the new
6923    ABI.)  */
6924
6925 static tree
6926 build_vtable_entry (delta, vcall_index, entry)
6927      tree delta;
6928      tree vcall_index;
6929      tree entry;
6930 {
6931   if (flag_vtable_thunks)
6932     {
6933       HOST_WIDE_INT idelta;
6934       HOST_WIDE_INT ivindex;
6935       tree fn;
6936
6937       idelta = tree_low_cst (delta, 0);
6938       ivindex = tree_low_cst (vcall_index, 0);
6939       fn = TREE_OPERAND (entry, 0);
6940       if ((idelta || ivindex) 
6941           && fn != abort_fndecl
6942           && !DECL_TINFO_FN_P (fn))
6943         {
6944           entry = make_thunk (entry, idelta, ivindex);
6945           entry = build1 (ADDR_EXPR, vtable_entry_type, entry);
6946           TREE_READONLY (entry) = 1;
6947           TREE_CONSTANT (entry) = 1;
6948         }
6949 #ifdef GATHER_STATISTICS
6950       n_vtable_entries += 1;
6951 #endif
6952       return entry;
6953     }
6954   else
6955     {
6956       tree elems = tree_cons (NULL_TREE, delta,
6957                               tree_cons (NULL_TREE, integer_zero_node,
6958                                          build_tree_list (NULL_TREE, entry)));
6959       tree entry = build (CONSTRUCTOR, vtable_entry_type, NULL_TREE, elems);
6960
6961       /* We don't use vcall offsets when not using vtable thunks.  */
6962       my_friendly_assert (integer_zerop (vcall_index), 20000125);
6963
6964       /* DELTA used to be constructed by `size_int' and/or size_binop,
6965          which caused overflow problems when it was negative.  That should
6966          be fixed now.  */
6967
6968       if (! int_fits_type_p (delta, delta_type_node))
6969         {
6970           if (flag_huge_objects)
6971             sorry ("object size exceeds built-in limit for virtual function table implementation");
6972           else
6973             sorry ("object size exceeds normal limit for virtual function table implementation, recompile all source and use -fhuge-objects");
6974         }
6975       
6976       TREE_CONSTANT (entry) = 1;
6977       TREE_STATIC (entry) = 1;
6978       TREE_READONLY (entry) = 1;
6979
6980 #ifdef GATHER_STATISTICS
6981       n_vtable_entries += 1;
6982 #endif
6983
6984       return entry;
6985     }
6986 }