OSDN Git Service

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