OSDN Git Service

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