OSDN Git Service

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