OSDN Git Service

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