OSDN Git Service

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