OSDN Git Service

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