OSDN Git Service

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