OSDN Git Service

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