OSDN Git Service

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