OSDN Git Service

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