• Main Page
  • Modules
  • Data Types
  • Files

osprey/kg++fe/gnu/cp/class.c

Go to the documentation of this file.
00001 /*
00002  * Copyright 2003, 2004, 2005, 2006 PathScale, Inc.  All Rights Reserved.
00003  */
00004 
00005 /* Functions related to building classes and their related objects.
00006    Copyright (C) 1987, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
00007    1999, 2000, 2001, 2002  Free Software Foundation, Inc.
00008    Contributed by Michael Tiemann (tiemann@cygnus.com)
00009 
00010 This file is part of GNU CC.
00011 
00012 GNU CC is free software; you can redistribute it and/or modify
00013 it under the terms of the GNU General Public License as published by
00014 the Free Software Foundation; either version 2, or (at your option)
00015 any later version.
00016 
00017 GNU CC is distributed in the hope that it will be useful,
00018 but WITHOUT ANY WARRANTY; without even the implied warranty of
00019 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020 GNU General Public License for more details.
00021 
00022 You should have received a copy of the GNU General Public License
00023 along with GNU CC; see the file COPYING.  If not, write to
00024 the Free Software Foundation, 59 Temple Place - Suite 330,
00025 Boston, MA 02111-1307, USA.  */
00026 
00027 
00028 /* High-level class interface.  */
00029 
00030 #include "config.h"
00031 #include "system.h"
00032 #include "tree.h"
00033 #include "cp-tree.h"
00034 #include "flags.h"
00035 #include "rtl.h"
00036 #include "output.h"
00037 #include "toplev.h"
00038 #include "ggc.h"
00039 #include "lex.h"
00040 #include "target.h"
00041 #ifdef SGI_MONGOOSE
00042 #include "defaults.h"   // get TARGET_VTABLE_USES_DESCRIPTORS
00043 #endif
00044 
00045 /* The number of nested classes being processed.  If we are not in the
00046    scope of any class, this is zero.  */
00047 
00048 int current_class_depth;
00049 
00050 /* In order to deal with nested classes, we keep a stack of classes.
00051    The topmost entry is the innermost class, and is the entry at index
00052    CURRENT_CLASS_DEPTH  */
00053 
00054 typedef struct class_stack_node {
00055   /* The name of the class.  */
00056   tree name;
00057 
00058   /* The _TYPE node for the class.  */
00059   tree type;
00060 
00061   /* The access specifier pending for new declarations in the scope of
00062      this class.  */
00063   tree access;
00064 
00065   /* If were defining TYPE, the names used in this class.  */
00066   splay_tree names_used;
00067 }* class_stack_node_t;
00068 
00069 typedef struct vtbl_init_data_s
00070 {
00071   /* The base for which we're building initializers.  */
00072   tree binfo;
00073   /* The type of the most-derived type.  */
00074   tree derived;
00075   /* The binfo for the dynamic type. This will be TYPE_BINFO (derived),
00076      unless ctor_vtbl_p is true.  */
00077   tree rtti_binfo;
00078   /* The negative-index vtable initializers built up so far.  These
00079      are in order from least negative index to most negative index.  */
00080   tree inits;
00081   /* The last (i.e., most negative) entry in INITS.  */
00082   tree* last_init;
00083   /* The binfo for the virtual base for which we're building
00084      vcall offset initializers.  */
00085   tree vbase;
00086   /* The functions in vbase for which we have already provided vcall
00087      offsets.  */
00088   varray_type fns;
00089   /* The vtable index of the next vcall or vbase offset.  */
00090   tree index;
00091   /* Nonzero if we are building the initializer for the primary
00092      vtable.  */
00093   int primary_vtbl_p;
00094   /* Nonzero if we are building the initializer for a construction
00095      vtable.  */
00096   int ctor_vtbl_p;
00097   /* True when adding vcall offset entries to the vtable.  False when
00098      merely computing the indices.  */
00099   bool generate_vcall_entries;
00100 } vtbl_init_data;
00101 
00102 /* The type of a function passed to walk_subobject_offsets.  */
00103 typedef int (*subobject_offset_fn) PARAMS ((tree, tree, splay_tree));
00104 
00105 /* The stack itself.  This is an dynamically resized array.  The
00106    number of elements allocated is CURRENT_CLASS_STACK_SIZE.  */
00107 static int current_class_stack_size;
00108 static class_stack_node_t current_class_stack;
00109 
00110 /* An array of all local classes present in this translation unit, in
00111    declaration order.  */
00112 varray_type local_classes;
00113 
00114 static tree get_vfield_name PARAMS ((tree));
00115 static void finish_struct_anon PARAMS ((tree));
00116 static tree get_vtable_name PARAMS ((tree));
00117 static tree get_basefndecls PARAMS ((tree, tree));
00118 static int build_primary_vtable PARAMS ((tree, tree));
00119 static int build_secondary_vtable PARAMS ((tree, tree));
00120 static void finish_vtbls PARAMS ((tree));
00121 static void modify_vtable_entry PARAMS ((tree, tree, tree, tree, tree *));
00122 static tree delete_duplicate_fields_1 PARAMS ((tree, tree));
00123 static void delete_duplicate_fields PARAMS ((tree));
00124 static void finish_struct_bits PARAMS ((tree));
00125 static int alter_access PARAMS ((tree, tree, tree));
00126 static void handle_using_decl PARAMS ((tree, tree));
00127 static void check_for_override PARAMS ((tree, tree));
00128 static tree dfs_modify_vtables PARAMS ((tree, void *));
00129 static tree modify_all_vtables PARAMS ((tree, tree));
00130 static void determine_primary_base PARAMS ((tree));
00131 static void finish_struct_methods PARAMS ((tree));
00132 static void maybe_warn_about_overly_private_class PARAMS ((tree));
00133 static int field_decl_cmp PARAMS ((const tree *, const tree *));
00134 static int method_name_cmp PARAMS ((const tree *, const tree *));
00135 static void add_implicitly_declared_members PARAMS ((tree, int, int, int));
00136 static tree fixed_type_or_null PARAMS ((tree, int *, int *));
00137 static tree resolve_address_of_overloaded_function PARAMS ((tree, tree, int,
00138                 int, int, tree));
00139 static tree build_vtable_entry_ref PARAMS ((tree, tree, tree));
00140 static tree build_vtbl_ref_1 PARAMS ((tree, tree));
00141 static tree build_vtbl_initializer PARAMS ((tree, tree, tree, tree, int *));
00142 static int count_fields PARAMS ((tree));
00143 static int add_fields_to_vec PARAMS ((tree, tree, int));
00144 static void check_bitfield_decl PARAMS ((tree));
00145 static void check_field_decl (tree, tree, int *, int *, int *, int *);
00146 static void check_field_decls (tree, tree *, int *, int *, int *);
00147 static tree *build_base_field (record_layout_info, tree, splay_tree, tree *);
00148 static void build_base_fields (record_layout_info, splay_tree, tree *);
00149 static void check_methods PARAMS ((tree));
00150 static void remove_zero_width_bit_fields PARAMS ((tree));
00151 static void check_bases PARAMS ((tree, int *, int *, int *));
00152 static void check_bases_and_members (tree);
00153 static tree create_vtable_ptr (tree, tree *);
00154 static void include_empty_classes (record_layout_info);
00155 static void layout_class_type (tree, tree *);
00156 static void fixup_pending_inline PARAMS ((tree));
00157 static void fixup_inline_methods PARAMS ((tree));
00158 static void set_primary_base PARAMS ((tree, tree));
00159 static void propagate_binfo_offsets PARAMS ((tree, tree, tree));
00160 static void layout_virtual_bases (record_layout_info, splay_tree);
00161 static tree dfs_set_offset_for_unshared_vbases PARAMS ((tree, void *));
00162 static void build_vbase_offset_vtbl_entries PARAMS ((tree, vtbl_init_data *));
00163 static void add_vcall_offset_vtbl_entries_r PARAMS ((tree, vtbl_init_data *));
00164 static void add_vcall_offset_vtbl_entries_1 PARAMS ((tree, vtbl_init_data *));
00165 static void build_vcall_offset_vtbl_entries PARAMS ((tree, vtbl_init_data *));
00166 static void add_vcall_offset (tree, tree, vtbl_init_data *);
00167 static void layout_vtable_decl PARAMS ((tree, int));
00168 static tree dfs_find_final_overrider PARAMS ((tree, void *));
00169 static tree find_final_overrider PARAMS ((tree, tree, tree));
00170 static int make_new_vtable PARAMS ((tree, tree));
00171 static int maybe_indent_hierarchy PARAMS ((FILE *, int, int));
00172 static void dump_class_hierarchy_r PARAMS ((FILE *, int, tree, tree, int));
00173 static void dump_class_hierarchy PARAMS ((tree));
00174 static void dump_array PARAMS ((FILE *, tree));
00175 static void dump_vtable PARAMS ((tree, tree, tree));
00176 static void dump_vtt PARAMS ((tree, tree));
00177 static tree build_vtable PARAMS ((tree, tree, tree));
00178 static void initialize_vtable PARAMS ((tree, tree));
00179 static void initialize_array PARAMS ((tree, tree));
00180 static void layout_nonempty_base_or_field PARAMS ((record_layout_info,
00181                tree, tree, splay_tree));
00182 static tree end_of_class PARAMS ((tree, int));
00183 static bool layout_empty_base PARAMS ((tree, tree, splay_tree, tree));
00184 static void accumulate_vtbl_inits PARAMS ((tree, tree, tree, tree, tree));
00185 static tree dfs_accumulate_vtbl_inits PARAMS ((tree, tree, tree, tree,
00186                  tree));
00187 static void build_rtti_vtbl_entries PARAMS ((tree, vtbl_init_data *));
00188 static void build_vcall_and_vbase_vtbl_entries PARAMS ((tree, 
00189               vtbl_init_data *));
00190 static void force_canonical_binfo_r PARAMS ((tree, tree, tree, tree));
00191 static void force_canonical_binfo PARAMS ((tree, tree, tree, tree));
00192 static tree dfs_unshared_virtual_bases PARAMS ((tree, void *));
00193 static void mark_primary_bases PARAMS ((tree));
00194 static tree mark_primary_virtual_base PARAMS ((tree, tree));
00195 static void clone_constructors_and_destructors PARAMS ((tree));
00196 static tree build_clone PARAMS ((tree, tree));
00197 static void update_vtable_entry_for_fn PARAMS ((tree, tree, tree, tree *));
00198 static tree copy_virtuals PARAMS ((tree));
00199 static void build_ctor_vtbl_group PARAMS ((tree, tree));
00200 static void build_vtt PARAMS ((tree));
00201 static tree binfo_ctor_vtable PARAMS ((tree));
00202 static tree *build_vtt_inits PARAMS ((tree, tree, tree *, tree *));
00203 static tree dfs_build_secondary_vptr_vtt_inits PARAMS ((tree, void *));
00204 static tree dfs_ctor_vtable_bases_queue_p PARAMS ((tree, void *data));
00205 static tree dfs_fixup_binfo_vtbls PARAMS ((tree, void *));
00206 static tree get_original_base PARAMS ((tree, tree));
00207 static tree dfs_get_primary_binfo PARAMS ((tree, void*));
00208 static int record_subobject_offset PARAMS ((tree, tree, splay_tree));
00209 static int check_subobject_offset PARAMS ((tree, tree, splay_tree));
00210 static int walk_subobject_offsets PARAMS ((tree, subobject_offset_fn,
00211              tree, splay_tree, tree, int));
00212 static void record_subobject_offsets PARAMS ((tree, tree, splay_tree, int));
00213 static int layout_conflict_p PARAMS ((tree, tree, splay_tree, int));
00214 static int splay_tree_compare_integer_csts PARAMS ((splay_tree_key k1,
00215                 splay_tree_key k2));
00216 static void warn_about_ambiguous_bases PARAMS ((tree));
00217 static bool type_requires_array_cookie PARAMS ((tree));
00218 static bool contains_empty_class_p (tree);
00219 static tree dfs_base_derived_from (tree, void *);
00220 static bool base_derived_from (tree, tree);
00221 static int empty_base_at_nonzero_offset_p (tree, tree, splay_tree);
00222 static tree end_of_base (tree);
00223 static tree get_vcall_index (tree, tree);
00224 
00225 /* Macros for dfs walking during vtt construction. See
00226    dfs_ctor_vtable_bases_queue_p, dfs_build_secondary_vptr_vtt_inits
00227    and dfs_fixup_binfo_vtbls.  */
00228 #define VTT_TOP_LEVEL_P(NODE) TREE_UNSIGNED (NODE)
00229 #define VTT_MARKED_BINFO_P(NODE) TREE_USED (NODE)
00230 
00231 /* Variables shared between class.c and call.c.  */
00232 
00233 #ifdef GATHER_STATISTICS
00234 int n_vtables = 0;
00235 int n_vtable_entries = 0;
00236 int n_vtable_searches = 0;
00237 int n_vtable_elems = 0;
00238 int n_convert_harshness = 0;
00239 int n_compute_conversion_costs = 0;
00240 int n_build_method_call = 0;
00241 int n_inner_fields_searched = 0;
00242 #endif
00243 
00244 /* Convert to or from a base subobject.  EXPR is an expression of type
00245    `A' or `A*', an expression of type `B' or `B*' is returned.  To
00246    convert A to a base B, CODE is PLUS_EXPR and BINFO is the binfo for
00247    the B base instance within A.  To convert base A to derived B, CODE
00248    is MINUS_EXPR and BINFO is the binfo for the A instance within B.
00249    In this latter case, A must not be a morally virtual base of B.
00250    NONNULL is true if EXPR is known to be non-NULL (this is only
00251    needed when EXPR is of pointer type).  CV qualifiers are preserved
00252    from EXPR.  */
00253 
00254 tree
00255 build_base_path (code, expr, binfo, nonnull)
00256      enum tree_code code;
00257      tree expr;
00258      tree binfo;
00259      int nonnull;
00260 {
00261   tree v_binfo = NULL_TREE;
00262   tree d_binfo = NULL_TREE;
00263   tree probe;
00264   tree offset;
00265   tree target_type;
00266   tree null_test = NULL;
00267   tree ptr_target_type;
00268   int fixed_type_p;
00269   int want_pointer = TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE;
00270 
00271   if (expr == error_mark_node || binfo == error_mark_node || !binfo)
00272     return error_mark_node;
00273 
00274   for (probe = binfo; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
00275     {
00276       d_binfo = probe;
00277       if (!v_binfo && TREE_VIA_VIRTUAL (probe))
00278   v_binfo = probe;
00279     }
00280 
00281   probe = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
00282   if (want_pointer)
00283     probe = TYPE_MAIN_VARIANT (TREE_TYPE (probe));
00284   
00285   my_friendly_assert (code == MINUS_EXPR
00286           ? same_type_p (BINFO_TYPE (binfo), probe)
00287           : code == PLUS_EXPR
00288           ? same_type_p (BINFO_TYPE (d_binfo), probe)
00289           : false, 20010723);
00290   
00291   if (code == MINUS_EXPR && v_binfo)
00292     {
00293       error ("cannot convert from base `%T' to derived type `%T' via virtual base `%T'",
00294        BINFO_TYPE (binfo), BINFO_TYPE (d_binfo), BINFO_TYPE (v_binfo));
00295       return error_mark_node;
00296     }
00297 
00298   if (!want_pointer)
00299     /* This must happen before the call to save_expr.  */
00300     expr = build_unary_op (ADDR_EXPR, expr, 0);
00301 
00302   fixed_type_p = resolves_to_fixed_type_p (expr, &nonnull);
00303   if (fixed_type_p <= 0 && TREE_SIDE_EFFECTS (expr))
00304     expr = save_expr (expr);
00305 
00306   if (want_pointer && !nonnull)
00307     null_test = build (EQ_EXPR, boolean_type_node, expr, integer_zero_node);
00308   
00309   offset = BINFO_OFFSET (binfo);
00310   
00311   if (v_binfo && fixed_type_p <= 0)
00312     {
00313       /* Going via virtual base V_BINFO.  We need the static offset
00314          from V_BINFO to BINFO, and the dynamic offset from D_BINFO to
00315          V_BINFO.  That offset is an entry in D_BINFO's vtable.  */
00316       tree v_offset;
00317 
00318       if (fixed_type_p < 0 && in_base_initializer)
00319   {
00320     /* In a base member initializer, we cannot rely on
00321        the vtable being set up. We have to use the vtt_parm.  */
00322     tree derived = v_binfo;
00323 
00324     while (BINFO_INHERITANCE_CHAIN (derived))
00325       derived = BINFO_INHERITANCE_CHAIN (derived);
00326     
00327     v_offset = build (PLUS_EXPR, TREE_TYPE (current_vtt_parm),
00328           current_vtt_parm, BINFO_VPTR_INDEX (derived));
00329     
00330     v_offset = build1 (INDIRECT_REF,
00331            TREE_TYPE (TYPE_VFIELD (BINFO_TYPE (derived))),
00332            v_offset);
00333   }
00334       else
00335   v_offset = build_vfield_ref (build_indirect_ref (expr, NULL),
00336              TREE_TYPE (TREE_TYPE (expr)));
00337       
00338       v_binfo = binfo_for_vbase (BINFO_TYPE (v_binfo), BINFO_TYPE (d_binfo));
00339       
00340       v_offset = build (PLUS_EXPR, TREE_TYPE (v_offset),
00341       v_offset,  BINFO_VPTR_FIELD (v_binfo));
00342       v_offset = build1 (NOP_EXPR, 
00343        build_pointer_type (ptrdiff_type_node),
00344        v_offset);
00345       v_offset = build_indirect_ref (v_offset, NULL);
00346       TREE_CONSTANT (v_offset) = 1;
00347 
00348       offset = cp_convert (ptrdiff_type_node,
00349          size_diffop (offset, BINFO_OFFSET (v_binfo)));
00350 
00351       if (!integer_zerop (offset))
00352   v_offset = build (code, ptrdiff_type_node, v_offset, offset);
00353 
00354       if (fixed_type_p < 0)
00355   /* Negative fixed_type_p means this is a constructor or destructor;
00356      virtual base layout is fixed in in-charge [cd]tors, but not in
00357      base [cd]tors.  */
00358   offset = build (COND_EXPR, ptrdiff_type_node,
00359       build (EQ_EXPR, boolean_type_node,
00360              current_in_charge_parm, integer_zero_node),
00361       v_offset,
00362       BINFO_OFFSET (binfo));
00363       else
00364   offset = v_offset;
00365     }
00366 
00367   target_type = code == PLUS_EXPR ? BINFO_TYPE (binfo) : BINFO_TYPE (d_binfo);
00368   
00369   target_type = cp_build_qualified_type
00370     (target_type, cp_type_quals (TREE_TYPE (TREE_TYPE (expr))));
00371   ptr_target_type = build_pointer_type (target_type);
00372   if (want_pointer)
00373     target_type = ptr_target_type;
00374   
00375   expr = build1 (NOP_EXPR, ptr_target_type, expr);
00376 
00377   if (!integer_zerop (offset))
00378     expr = build (code, ptr_target_type, expr, offset);
00379   else
00380     null_test = NULL;
00381   
00382   if (!want_pointer)
00383     expr = build_indirect_ref (expr, NULL);
00384 
00385   if (null_test)
00386     expr = build (COND_EXPR, target_type, null_test,
00387       build1 (NOP_EXPR, target_type, integer_zero_node),
00388       expr);
00389 
00390   return expr;
00391 }
00392 
00393 /* Convert OBJECT to the base TYPE.  If CHECK_ACCESS is true, an error
00394    message is emitted if TYPE is inaccessible.  OBJECT is assumed to
00395    be non-NULL.  */
00396 
00397 tree
00398 convert_to_base (tree object, tree type, bool check_access)
00399 {
00400   tree binfo;
00401 
00402   binfo = lookup_base (TREE_TYPE (object), type, 
00403            check_access ? ba_check : ba_ignore, 
00404            NULL);
00405   if (!binfo || binfo == error_mark_node)
00406     return error_mark_node;
00407 
00408   return build_base_path (PLUS_EXPR, object, binfo, /*nonnull=*/1);
00409 }
00410 
00411 /* EXPR is an expression with class type.  BASE is a base class (a
00412    BINFO) of that class type.  Returns EXPR, converted to the BASE
00413    type.  This function assumes that EXPR is the most derived class;
00414    therefore virtual bases can be found at their static offsets.  */
00415 
00416 tree
00417 convert_to_base_statically (tree expr, tree base)
00418 {
00419   tree expr_type;
00420 
00421   expr_type = TREE_TYPE (expr);
00422   if (!same_type_p (expr_type, BINFO_TYPE (base)))
00423     {
00424       tree pointer_type;
00425 
00426       pointer_type = build_pointer_type (expr_type);
00427       expr = build_unary_op (ADDR_EXPR, expr, /*noconvert=*/1);
00428       if (!integer_zerop (BINFO_OFFSET (base)))
00429     expr = build (PLUS_EXPR, pointer_type, expr, 
00430       build_nop (pointer_type, BINFO_OFFSET (base)));
00431       expr = build_nop (build_pointer_type (BINFO_TYPE (base)), expr);
00432       expr = build1 (INDIRECT_REF, BINFO_TYPE (base), expr);
00433     }
00434 
00435   return expr;
00436 }
00437 
00438 
00439 /* Virtual function things.  */
00440 
00441 static tree
00442 build_vtable_entry_ref (array_ref, instance, idx)
00443      tree array_ref, instance, idx;
00444 {
00445   tree i, i2, vtable, first_fn, basetype;
00446 
00447   basetype = TREE_TYPE (instance);
00448   if (TREE_CODE (basetype) == REFERENCE_TYPE)
00449     basetype = TREE_TYPE (basetype);
00450 
00451   vtable = get_vtbl_decl_for_binfo (TYPE_BINFO (basetype));
00452   first_fn = TYPE_BINFO_VTABLE (basetype);
00453 
00454   i = fold (build_array_ref (first_fn, idx));
00455   i = fold (build_c_cast (ptrdiff_type_node,
00456         build_unary_op (ADDR_EXPR, i, 0)));
00457   i2 = fold (build_array_ref (vtable, build_int_2 (0,0)));
00458   i2 = fold (build_c_cast (ptrdiff_type_node,
00459          build_unary_op (ADDR_EXPR, i2, 0)));
00460   i = fold (cp_build_binary_op (MINUS_EXPR, i, i2));
00461 
00462   if (TREE_CODE (i) != INTEGER_CST)
00463     abort ();
00464 
00465   return build (VTABLE_REF, TREE_TYPE (array_ref), array_ref, vtable, i);
00466 }
00467 
00468 /* Given an object INSTANCE, return an expression which yields the
00469    vtable element corresponding to INDEX.  There are many special
00470    cases for INSTANCE which we take care of here, mainly to avoid
00471    creating extra tree nodes when we don't have to.  */
00472 
00473 static tree
00474 build_vtbl_ref_1 (instance, idx)
00475      tree instance, idx;
00476 {
00477   tree aref;
00478   tree vtbl = NULL_TREE;
00479 
00480   /* Try to figure out what a reference refers to, and
00481      access its virtual function table directly.  */
00482 
00483   int cdtorp = 0;
00484   tree fixed_type = fixed_type_or_null (instance, NULL, &cdtorp);
00485 
00486   tree basetype = TREE_TYPE (instance);
00487   if (TREE_CODE (basetype) == REFERENCE_TYPE)
00488     basetype = TREE_TYPE (basetype);
00489 
00490   if (fixed_type && !cdtorp)
00491     {
00492       tree binfo = lookup_base (fixed_type, basetype,
00493         ba_ignore|ba_quiet, NULL);
00494       if (binfo)
00495   vtbl = BINFO_VTABLE (binfo);
00496     }
00497 
00498   if (!vtbl)
00499     {
00500       vtbl = build_vfield_ref (instance, basetype);
00501     }
00502 
00503   assemble_external (vtbl);
00504 
00505   aref = build_array_ref (vtbl, idx);
00506   TREE_CONSTANT (aref) = 1;
00507 
00508   return aref;
00509 }
00510 
00511 tree
00512 build_vtbl_ref (instance, idx)
00513      tree instance, idx;
00514 {
00515   tree aref = build_vtbl_ref_1 (instance, idx);
00516 
00517   if (flag_vtable_gc)
00518     aref = build_vtable_entry_ref (aref, instance, idx);
00519 
00520   return aref;
00521 }
00522 
00523 /* Given an object INSTANCE, return an expression which yields a
00524    function pointer corresponding to vtable element INDEX.  */
00525 
00526 tree
00527 build_vfn_ref (instance, idx)
00528      tree instance, idx;
00529 {
00530   tree aref = build_vtbl_ref_1 (instance, idx);
00531 
00532   /* When using function descriptors, the address of the
00533      vtable entry is treated as a function pointer.  */
00534   if (TARGET_VTABLE_USES_DESCRIPTORS)
00535     aref = build1 (NOP_EXPR, TREE_TYPE (aref),
00536        build_unary_op (ADDR_EXPR, aref, /*noconvert=*/1));
00537 
00538   if (flag_vtable_gc)
00539     aref = build_vtable_entry_ref (aref, instance, idx);
00540 
00541   return aref;
00542 }
00543 
00544 /* Return the name of the virtual function table (as an IDENTIFIER_NODE)
00545    for the given TYPE.  */
00546 
00547 static tree
00548 get_vtable_name (type)
00549      tree type;
00550 {
00551   return mangle_vtbl_for_type (type);
00552 }
00553 
00554 /* Return an IDENTIFIER_NODE for the name of the virtual table table
00555    for TYPE.  */
00556 
00557 tree
00558 get_vtt_name (type)
00559      tree type;
00560 {
00561   return mangle_vtt_for_type (type);
00562 }
00563 
00564 /* Create a VAR_DECL for a primary or secondary vtable for CLASS_TYPE.
00565    (For a secondary vtable for B-in-D, CLASS_TYPE should be D, not B.)
00566    Use NAME for the name of the vtable, and VTABLE_TYPE for its type.  */
00567 
00568 static tree
00569 build_vtable (class_type, name, vtable_type)
00570      tree class_type;
00571      tree name;
00572      tree vtable_type;
00573 {
00574   tree decl;
00575 
00576   decl = build_lang_decl (VAR_DECL, name, vtable_type);
00577   /* vtable names are already mangled; give them their DECL_ASSEMBLER_NAME
00578      now to avoid confusion in mangle_decl.  */
00579   SET_DECL_ASSEMBLER_NAME (decl, name);
00580   DECL_CONTEXT (decl) = class_type;
00581   DECL_ARTIFICIAL (decl) = 1;
00582   TREE_STATIC (decl) = 1;
00583   TREE_READONLY (decl) = 1;
00584   DECL_VIRTUAL_P (decl) = 1;
00585   DECL_ALIGN (decl) = TARGET_VTABLE_ENTRY_ALIGN;
00586 
00587   import_export_vtable (decl, class_type, 0);
00588 
00589   return decl;
00590 }
00591 
00592 /* Get the VAR_DECL of the vtable for TYPE. TYPE need not be polymorphic,
00593    or even complete.  If this does not exist, create it.  If COMPLETE is
00594    nonzero, then complete the definition of it -- that will render it
00595    impossible to actually build the vtable, but is useful to get at those
00596    which are known to exist in the runtime.  */
00597 
00598 tree 
00599 get_vtable_decl (type, complete)
00600      tree type;
00601      int complete;
00602 {
00603   tree decl;
00604 
00605   if (CLASSTYPE_VTABLES (type))
00606     return CLASSTYPE_VTABLES (type);
00607   
00608   decl = build_vtable (type, get_vtable_name (type), void_type_node);
00609   CLASSTYPE_VTABLES (type) = decl;
00610 
00611   /* At one time the vtable info was grabbed 2 words at a time.  This
00612      fails on sparc unless you have 8-byte alignment.  (tiemann) */
00613   DECL_ALIGN (decl) = MAX (TYPE_ALIGN (double_type_node),
00614          DECL_ALIGN (decl));
00615 
00616   if (complete)
00617     {
00618       DECL_EXTERNAL (decl) = 1;
00619       cp_finish_decl (decl, NULL_TREE, NULL_TREE, 0);
00620     }
00621 
00622   return decl;
00623 }
00624 
00625 /* Returns a copy of the BINFO_VIRTUALS list in BINFO.  The
00626    BV_VCALL_INDEX for each entry is cleared.  */
00627 
00628 static tree
00629 copy_virtuals (binfo)
00630      tree binfo;
00631 {
00632   tree copies;
00633   tree t;
00634 
00635   copies = copy_list (BINFO_VIRTUALS (binfo));
00636   for (t = copies; t; t = TREE_CHAIN (t))
00637     BV_VCALL_INDEX (t) = NULL_TREE;
00638 
00639   return copies;
00640 }
00641 
00642 /* Build the primary virtual function table for TYPE.  If BINFO is
00643    non-NULL, build the vtable starting with the initial approximation
00644    that it is the same as the one which is the head of the association
00645    list.  Returns a nonzero value if a new vtable is actually
00646    created.  */
00647 
00648 static int
00649 build_primary_vtable (binfo, type)
00650      tree binfo, type;
00651 {
00652   tree decl;
00653   tree virtuals;
00654 
00655   decl = get_vtable_decl (type, /*complete=*/0);
00656   
00657   if (binfo)
00658     {
00659       if (BINFO_NEW_VTABLE_MARKED (binfo, type))
00660   /* We have already created a vtable for this base, so there's
00661      no need to do it again.  */
00662   return 0;
00663       
00664       virtuals = copy_virtuals (binfo);
00665       TREE_TYPE (decl) = TREE_TYPE (get_vtbl_decl_for_binfo (binfo));
00666       DECL_SIZE (decl) = TYPE_SIZE (TREE_TYPE (decl));
00667       DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
00668     }
00669   else
00670     {
00671       my_friendly_assert (TREE_CODE (TREE_TYPE (decl)) == VOID_TYPE,
00672                           20000118);
00673       virtuals = NULL_TREE;
00674     }
00675 
00676 #ifdef GATHER_STATISTICS
00677   n_vtables += 1;
00678   n_vtable_elems += list_length (virtuals);
00679 #endif
00680 
00681   /* Initialize the association list for this type, based
00682      on our first approximation.  */
00683   TYPE_BINFO_VTABLE (type) = decl;
00684   TYPE_BINFO_VIRTUALS (type) = virtuals;
00685   SET_BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (type), type);
00686   return 1;
00687 }
00688 
00689 /* Give BINFO a new virtual function table which is initialized
00690    with a skeleton-copy of its original initialization.  The only
00691    entry that changes is the `delta' entry, so we can really
00692    share a lot of structure.
00693 
00694    FOR_TYPE is the most derived type which caused this table to
00695    be needed.
00696 
00697    Returns nonzero if we haven't met BINFO before.
00698 
00699    The order in which vtables are built (by calling this function) for
00700    an object must remain the same, otherwise a binary incompatibility
00701    can result.  */
00702 
00703 static int
00704 build_secondary_vtable (binfo, for_type)
00705      tree binfo, for_type;
00706 {
00707   my_friendly_assert (binfo == CANONICAL_BINFO (binfo, for_type), 20010605);
00708 
00709   if (BINFO_NEW_VTABLE_MARKED (binfo, for_type))
00710     /* We already created a vtable for this base.  There's no need to
00711        do it again.  */
00712     return 0;
00713 
00714   /* Remember that we've created a vtable for this BINFO, so that we
00715      don't try to do so again.  */
00716   SET_BINFO_NEW_VTABLE_MARKED (binfo, for_type);
00717   
00718   /* Make fresh virtual list, so we can smash it later.  */
00719   BINFO_VIRTUALS (binfo) = copy_virtuals (binfo);
00720 
00721   /* Secondary vtables are laid out as part of the same structure as
00722      the primary vtable.  */
00723   BINFO_VTABLE (binfo) = NULL_TREE;
00724   return 1;
00725 }
00726 
00727 /* Create a new vtable for BINFO which is the hierarchy dominated by
00728    T. Return nonzero if we actually created a new vtable.  */
00729 
00730 static int
00731 make_new_vtable (t, binfo)
00732      tree t;
00733      tree binfo;
00734 {
00735   if (binfo == TYPE_BINFO (t))
00736     /* In this case, it is *type*'s vtable we are modifying.  We start
00737        with the approximation that its vtable is that of the
00738        immediate base class.  */
00739     /* ??? This actually passes TYPE_BINFO (t), not the primary base binfo,
00740        since we've updated DECL_CONTEXT (TYPE_VFIELD (t)) by now.  */
00741     return build_primary_vtable (TYPE_BINFO (DECL_CONTEXT (TYPE_VFIELD (t))),
00742          t);
00743   else
00744     /* This is our very own copy of `basetype' to play with.  Later,
00745        we will fill in all the virtual functions that override the
00746        virtual functions in these base classes which are not defined
00747        by the current type.  */
00748     return build_secondary_vtable (binfo, t);
00749 }
00750 
00751 /* Make *VIRTUALS, an entry on the BINFO_VIRTUALS list for BINFO
00752    (which is in the hierarchy dominated by T) list FNDECL as its
00753    BV_FN.  DELTA is the required constant adjustment from the `this'
00754    pointer where the vtable entry appears to the `this' required when
00755    the function is actually called.  */
00756 
00757 static void
00758 modify_vtable_entry (t, binfo, fndecl, delta, virtuals)
00759      tree t;
00760      tree binfo;
00761      tree fndecl;
00762      tree delta;
00763      tree *virtuals;
00764 {
00765   tree v;
00766 
00767   v = *virtuals;
00768 
00769   if (fndecl != BV_FN (v)
00770       || !tree_int_cst_equal (delta, BV_DELTA (v)))
00771     {
00772       tree base_fndecl;
00773 
00774       /* We need a new vtable for BINFO.  */
00775       if (make_new_vtable (t, binfo))
00776   {
00777     /* If we really did make a new vtable, we also made a copy
00778        of the BINFO_VIRTUALS list.  Now, we have to find the
00779        corresponding entry in that list.  */
00780     *virtuals = BINFO_VIRTUALS (binfo);
00781     while (BV_FN (*virtuals) != BV_FN (v))
00782       *virtuals = TREE_CHAIN (*virtuals);
00783     v = *virtuals;
00784   }
00785 
00786       base_fndecl = BV_FN (v);
00787       BV_DELTA (v) = delta;
00788       BV_VCALL_INDEX (v) = NULL_TREE;
00789       BV_FN (v) = fndecl;
00790     }
00791 }
00792 
00793 
00794 /* Add method METHOD to class TYPE.  If ERROR_P is true, we are adding
00795    the method after the class has already been defined because a
00796    declaration for it was seen.  (Even though that is erroneous, we
00797    add the method for improved error recovery.)  */
00798 
00799 void
00800 add_method (type, method, error_p)
00801      tree type;
00802      tree method;
00803      int error_p;
00804 {
00805   int using = (DECL_CONTEXT (method) != type);
00806   int len;
00807   int slot;
00808   tree method_vec;
00809   int template_conv_p = (TREE_CODE (method) == TEMPLATE_DECL
00810        && DECL_TEMPLATE_CONV_FN_P (method));
00811 
00812   if (!CLASSTYPE_METHOD_VEC (type))
00813     /* Make a new method vector.  We start with 8 entries.  We must
00814        allocate at least two (for constructors and destructors), and
00815        we're going to end up with an assignment operator at some point
00816        as well.
00817        
00818        We could use a TREE_LIST for now, and convert it to a TREE_VEC
00819        in finish_struct, but we would probably waste more memory
00820        making the links in the list than we would by over-allocating
00821        the size of the vector here.  Furthermore, we would complicate
00822        all the code that expects this to be a vector.  */
00823     CLASSTYPE_METHOD_VEC (type) = make_tree_vec (8);
00824 
00825   method_vec = CLASSTYPE_METHOD_VEC (type);
00826   len = TREE_VEC_LENGTH (method_vec);
00827 
00828   /* Constructors and destructors go in special slots.  */
00829   if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (method))
00830     slot = CLASSTYPE_CONSTRUCTOR_SLOT;
00831   else if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (method))
00832     slot = CLASSTYPE_DESTRUCTOR_SLOT;
00833   else
00834     {
00835       int have_template_convs_p = 0;
00836       
00837       /* See if we already have an entry with this name.  */
00838       for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT; slot < len; ++slot)
00839   {
00840     tree m = TREE_VEC_ELT (method_vec, slot);
00841 
00842     if (!m)
00843       break;
00844     m = OVL_CURRENT (m);
00845     
00846     if (template_conv_p)
00847       {
00848         have_template_convs_p = (TREE_CODE (m) == TEMPLATE_DECL
00849                && DECL_TEMPLATE_CONV_FN_P (m));
00850         
00851         /* If we need to move things up, see if there's
00852      space.  */
00853         if (!have_template_convs_p)
00854     {
00855       slot = len - 1;
00856       if (TREE_VEC_ELT (method_vec, slot))
00857         slot++;
00858     }
00859         break;
00860       }
00861     if (DECL_NAME (m) == DECL_NAME (method))
00862       break;
00863   }
00864       
00865       if (slot == len)
00866   {
00867     /* We need a bigger method vector.  */
00868     int new_len;
00869     tree new_vec;
00870 
00871     /* In the non-error case, we are processing a class
00872        definition.  Double the size of the vector to give room
00873        for new methods.  */
00874     if (!error_p)
00875       new_len = 2 * len;
00876     /* In the error case, the vector is already complete.  We
00877        don't expect many errors, and the rest of the front-end
00878        will get confused if there are empty slots in the vector.  */
00879     else
00880       new_len = len + 1;
00881 
00882     new_vec = make_tree_vec (new_len);
00883     memcpy (&TREE_VEC_ELT (new_vec, 0), &TREE_VEC_ELT (method_vec, 0),
00884       len * sizeof (tree));
00885     len = new_len;
00886     method_vec = CLASSTYPE_METHOD_VEC (type) = new_vec;
00887   }
00888 
00889       if (DECL_CONV_FN_P (method) && !TREE_VEC_ELT (method_vec, slot))
00890   {
00891     /* Type conversion operators have to come before ordinary
00892        methods; add_conversions depends on this to speed up
00893        looking for conversion operators.  So, if necessary, we
00894        slide some of the vector elements up.  In theory, this
00895        makes this algorithm O(N^2) but we don't expect many
00896        conversion operators.  */
00897     if (template_conv_p)
00898       slot = CLASSTYPE_FIRST_CONVERSION_SLOT;
00899     else
00900       for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT; slot < len; ++slot)
00901         {
00902     tree fn = TREE_VEC_ELT (method_vec, slot);
00903   
00904     if (!fn)
00905       /* There are no more entries in the vector, so we
00906          can insert the new conversion operator here.  */
00907       break;
00908         
00909     if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
00910       /* We can insert the new function right at the
00911          SLOTth position.  */
00912       break;
00913         }
00914 
00915     if (template_conv_p && have_template_convs_p)
00916       /*OK*/;
00917     else if (!TREE_VEC_ELT (method_vec, slot))
00918       /* There is nothing in the Ith slot, so we can avoid
00919          moving anything.  */
00920     ; 
00921     else
00922       {
00923         /* We know the last slot in the vector is empty
00924      because we know that at this point there's room
00925      for a new function.  */
00926         memmove (&TREE_VEC_ELT (method_vec, slot + 1),
00927            &TREE_VEC_ELT (method_vec, slot),
00928            (len - slot - 1) * sizeof (tree));
00929         TREE_VEC_ELT (method_vec, slot) = NULL_TREE;
00930       }
00931   }
00932     }
00933       
00934   if (template_class_depth (type))
00935     /* TYPE is a template class.  Don't issue any errors now; wait
00936        until instantiation time to complain.  */
00937     ;
00938   else
00939     {
00940       tree fns;
00941 
00942       /* Check to see if we've already got this method.  */
00943       for (fns = TREE_VEC_ELT (method_vec, slot);
00944      fns;
00945      fns = OVL_NEXT (fns))
00946   {
00947     tree fn = OVL_CURRENT (fns);
00948     tree parms1;
00949     tree parms2;
00950     bool same = 1;
00951 
00952     if (TREE_CODE (fn) != TREE_CODE (method))
00953       continue;
00954 
00955     /* [over.load] Member function declarations with the
00956        same name and the same parameter types cannot be
00957        overloaded if any of them is a static member
00958        function declaration.
00959 
00960        [namespace.udecl] When a using-declaration brings names
00961        from a base class into a derived class scope, member
00962        functions in the derived class override and/or hide member
00963        functions with the same name and parameter types in a base
00964        class (rather than conflicting).  */
00965     parms1 = TYPE_ARG_TYPES (TREE_TYPE (fn));
00966     parms2 = TYPE_ARG_TYPES (TREE_TYPE (method));
00967 
00968     /* Compare the quals on the 'this' parm.  Don't compare
00969        the whole types, as used functions are treated as
00970        coming from the using class in overload resolution.  */
00971     if (! DECL_STATIC_FUNCTION_P (fn)
00972         && ! DECL_STATIC_FUNCTION_P (method)
00973         && (TYPE_QUALS (TREE_TYPE (TREE_VALUE (parms1)))
00974       != TYPE_QUALS (TREE_TYPE (TREE_VALUE (parms2)))))
00975       same = 0;
00976     
00977     /* For templates, the template parms must be identical.  */
00978     if (TREE_CODE (fn) == TEMPLATE_DECL
00979         && !comp_template_parms (DECL_TEMPLATE_PARMS (fn),
00980                DECL_TEMPLATE_PARMS (method)))
00981       same = 0;
00982     
00983     if (! DECL_STATIC_FUNCTION_P (fn))
00984       parms1 = TREE_CHAIN (parms1);
00985     if (! DECL_STATIC_FUNCTION_P (method))
00986       parms2 = TREE_CHAIN (parms2);
00987 
00988     if (same && compparms (parms1, parms2) 
00989         && (!DECL_CONV_FN_P (fn) 
00990       || same_type_p (TREE_TYPE (TREE_TYPE (fn)),
00991           TREE_TYPE (TREE_TYPE (method)))))
00992       {
00993         if (using && DECL_CONTEXT (fn) == type)
00994     /* Defer to the local function.  */
00995     return;
00996         else
00997     {
00998       cp_error_at ("`%#D' and `%#D' cannot be overloaded",
00999              method, fn, method);
01000 
01001       /* We don't call duplicate_decls here to merge
01002          the declarations because that will confuse
01003          things if the methods have inline
01004          definitions.  In particular, we will crash
01005          while processing the definitions.  */
01006       return;
01007     }
01008       }
01009   }
01010     }
01011 
01012   /* Actually insert the new method.  */
01013   TREE_VEC_ELT (method_vec, slot) 
01014     = build_overload (method, TREE_VEC_ELT (method_vec, slot));
01015 
01016   /* Add the new binding.  */ 
01017   if (!DECL_CONSTRUCTOR_P (method)
01018       && !DECL_DESTRUCTOR_P (method))
01019     push_class_level_binding (DECL_NAME (method),
01020             TREE_VEC_ELT (method_vec, slot));
01021 }
01022 
01023 /* Subroutines of finish_struct.  */
01024 
01025 /* Look through the list of fields for this struct, deleting
01026    duplicates as we go.  This must be recursive to handle
01027    anonymous unions.
01028 
01029    FIELD is the field which may not appear anywhere in FIELDS.
01030    FIELD_PTR, if non-null, is the starting point at which
01031    chained deletions may take place.
01032    The value returned is the first acceptable entry found
01033    in FIELDS.
01034 
01035    Note that anonymous fields which are not of UNION_TYPE are
01036    not duplicates, they are just anonymous fields.  This happens
01037    when we have unnamed bitfields, for example.  */
01038 
01039 static tree
01040 delete_duplicate_fields_1 (field, fields)
01041      tree field, fields;
01042 {
01043   tree x;
01044   tree prev = 0;
01045   if (DECL_NAME (field) == 0)
01046     {
01047       if (! ANON_AGGR_TYPE_P (TREE_TYPE (field)))
01048   return fields;
01049 
01050       for (x = TYPE_FIELDS (TREE_TYPE (field)); x; x = TREE_CHAIN (x))
01051   fields = delete_duplicate_fields_1 (x, fields);
01052       return fields;
01053     }
01054   else
01055     {
01056       for (x = fields; x; prev = x, x = TREE_CHAIN (x))
01057   {
01058     if (DECL_NAME (x) == 0)
01059       {
01060         if (! ANON_AGGR_TYPE_P (TREE_TYPE (x)))
01061     continue;
01062         TYPE_FIELDS (TREE_TYPE (x))
01063     = delete_duplicate_fields_1 (field, TYPE_FIELDS (TREE_TYPE (x)));
01064         if (TYPE_FIELDS (TREE_TYPE (x)) == 0)
01065     {
01066       if (prev == 0)
01067         fields = TREE_CHAIN (fields);
01068       else
01069         TREE_CHAIN (prev) = TREE_CHAIN (x);
01070     }
01071       }
01072     else if (TREE_CODE (field) == USING_DECL)
01073       /* A using declaration is allowed to appear more than
01074          once.  We'll prune these from the field list later, and
01075          handle_using_decl will complain about invalid multiple
01076          uses.  */
01077       ;
01078     else if (DECL_NAME (field) == DECL_NAME (x))
01079       {
01080         if (TREE_CODE (field) == CONST_DECL
01081       && TREE_CODE (x) == CONST_DECL)
01082     cp_error_at ("duplicate enum value `%D'", x);
01083         else if (TREE_CODE (field) == CONST_DECL
01084            || TREE_CODE (x) == CONST_DECL)
01085     cp_error_at ("duplicate field `%D' (as enum and non-enum)",
01086            x);
01087         else if (DECL_DECLARES_TYPE_P (field)
01088            && DECL_DECLARES_TYPE_P (x))
01089     {
01090       if (same_type_p (TREE_TYPE (field), TREE_TYPE (x)))
01091         continue;
01092       cp_error_at ("duplicate nested type `%D'", x);
01093     }
01094         else if (DECL_DECLARES_TYPE_P (field)
01095            || DECL_DECLARES_TYPE_P (x))
01096     {
01097       /* Hide tag decls.  */
01098       if ((TREE_CODE (field) == TYPE_DECL
01099            && DECL_ARTIFICIAL (field))
01100           || (TREE_CODE (x) == TYPE_DECL
01101         && DECL_ARTIFICIAL (x)))
01102         continue;
01103       cp_error_at ("duplicate field `%D' (as type and non-type)",
01104              x);
01105     }
01106         else
01107     cp_error_at ("duplicate member `%D'", x);
01108         if (prev == 0)
01109     fields = TREE_CHAIN (fields);
01110         else
01111     TREE_CHAIN (prev) = TREE_CHAIN (x);
01112       }
01113   }
01114     }
01115   return fields;
01116 }
01117 
01118 static void
01119 delete_duplicate_fields (fields)
01120      tree fields;
01121 {
01122   tree x;
01123   for (x = fields; x && TREE_CHAIN (x); x = TREE_CHAIN (x))
01124     TREE_CHAIN (x) = delete_duplicate_fields_1 (x, TREE_CHAIN (x));
01125 }
01126 
01127 /* Change the access of FDECL to ACCESS in T.  Return 1 if change was
01128    legit, otherwise return 0.  */
01129 
01130 static int
01131 alter_access (t, fdecl, access)
01132      tree t;
01133      tree fdecl;
01134      tree access;
01135 {
01136   tree elem;
01137 
01138   if (!DECL_LANG_SPECIFIC (fdecl))
01139     retrofit_lang_decl (fdecl);
01140 
01141   if (DECL_DISCRIMINATOR_P (fdecl))
01142     abort ();
01143 
01144   elem = purpose_member (t, DECL_ACCESS (fdecl));
01145   if (elem)
01146     {
01147       if (TREE_VALUE (elem) != access)
01148   {
01149     if (TREE_CODE (TREE_TYPE (fdecl)) == FUNCTION_DECL)
01150       cp_error_at ("conflicting access specifications for method `%D', ignored", TREE_TYPE (fdecl));
01151     else
01152       error ("conflicting access specifications for field `%s', ignored",
01153        IDENTIFIER_POINTER (DECL_NAME (fdecl)));
01154   }
01155       else
01156   {
01157     /* They're changing the access to the same thing they changed
01158        it to before.  That's OK.  */
01159     ;
01160   }
01161     }
01162   else
01163     {
01164       enforce_access (t, fdecl);
01165       DECL_ACCESS (fdecl) = tree_cons (t, access, DECL_ACCESS (fdecl));
01166       return 1;
01167     }
01168   return 0;
01169 }
01170 
01171 /* Process the USING_DECL, which is a member of T.  */
01172 
01173 static void
01174 handle_using_decl (using_decl, t)
01175      tree using_decl;
01176      tree t;
01177 {
01178   tree ctype = DECL_INITIAL (using_decl);
01179   tree name = DECL_NAME (using_decl);
01180   tree access
01181     = TREE_PRIVATE (using_decl) ? access_private_node
01182     : TREE_PROTECTED (using_decl) ? access_protected_node
01183     : access_public_node;
01184   tree fdecl, binfo;
01185   tree flist = NULL_TREE;
01186   tree old_value;
01187 
01188   if (ctype == error_mark_node)
01189     return;
01190 
01191   binfo = lookup_base (t, ctype, ba_any, NULL);
01192   if (! binfo)
01193     {
01194       error_not_base_type (t, ctype);
01195       return;
01196     }
01197   
01198   if (constructor_name_p (name, ctype))
01199     {
01200       cp_error_at ("`%D' names constructor", using_decl);
01201       return;
01202     }
01203   if (constructor_name_p (name, t))
01204     {
01205       cp_error_at ("`%D' invalid in `%T'", using_decl, t);
01206       return;
01207     }
01208 
01209   fdecl = lookup_member (binfo, name, 0, 0);
01210   
01211   if (!fdecl)
01212     {
01213       cp_error_at ("no members matching `%D' in `%#T'", using_decl, ctype);
01214       return;
01215     }
01216 
01217   if (BASELINK_P (fdecl))
01218     /* Ignore base type this came from.  */
01219     fdecl = BASELINK_FUNCTIONS (fdecl);
01220 
01221   old_value = IDENTIFIER_CLASS_VALUE (name);
01222   if (old_value)
01223     {
01224       if (is_overloaded_fn (old_value))
01225   old_value = OVL_CURRENT (old_value);
01226 
01227       if (DECL_P (old_value) && DECL_CONTEXT (old_value) == t)
01228   /* OK */;
01229       else
01230   old_value = NULL_TREE;
01231     }
01232 
01233   if (is_overloaded_fn (fdecl))
01234     flist = fdecl;
01235 
01236   if (! old_value)
01237     ;
01238   else if (is_overloaded_fn (old_value))
01239     {
01240       if (flist)
01241   /* It's OK to use functions from a base when there are functions with
01242      the same name already present in the current class.  */;
01243       else
01244   {
01245     cp_error_at ("`%D' invalid in `%#T'", using_decl, t);
01246     cp_error_at ("  because of local method `%#D' with same name",
01247            OVL_CURRENT (old_value));
01248     return;
01249   }
01250     }
01251   else if (!DECL_ARTIFICIAL (old_value))
01252     {
01253       cp_error_at ("`%D' invalid in `%#T'", using_decl, t);
01254       cp_error_at ("  because of local member `%#D' with same name", old_value);
01255       return;
01256     }
01257   
01258   /* Make type T see field decl FDECL with access ACCESS.*/
01259   if (flist)
01260     for (; flist; flist = OVL_NEXT (flist))
01261       {
01262   add_method (t, OVL_CURRENT (flist), /*error_p=*/0);
01263   alter_access (t, OVL_CURRENT (flist), access);
01264       }
01265   else
01266     alter_access (t, fdecl, access);
01267 }
01268 
01269 /* Run through the base clases of T, updating
01270    CANT_HAVE_DEFAULT_CTOR_P, CANT_HAVE_CONST_CTOR_P, and
01271    NO_CONST_ASN_REF_P.  Also set flag bits in T based on properties of
01272    the bases.  */
01273 
01274 static void
01275 check_bases (t, cant_have_default_ctor_p, cant_have_const_ctor_p,
01276        no_const_asn_ref_p)
01277      tree t;
01278      int *cant_have_default_ctor_p;
01279      int *cant_have_const_ctor_p;
01280      int *no_const_asn_ref_p;
01281 {
01282   int n_baseclasses;
01283   int i;
01284   int seen_non_virtual_nearly_empty_base_p;
01285   tree binfos;
01286 
01287   binfos = TYPE_BINFO_BASETYPES (t);
01288   n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
01289   seen_non_virtual_nearly_empty_base_p = 0;
01290 
01291   /* An aggregate cannot have baseclasses.  */
01292   CLASSTYPE_NON_AGGREGATE (t) |= (n_baseclasses != 0);
01293 
01294   for (i = 0; i < n_baseclasses; ++i) 
01295     {
01296       tree base_binfo;
01297       tree basetype;
01298 
01299       /* Figure out what base we're looking at.  */
01300       base_binfo = TREE_VEC_ELT (binfos, i);
01301       basetype = TREE_TYPE (base_binfo);
01302 
01303       /* If the type of basetype is incomplete, then we already
01304    complained about that fact (and we should have fixed it up as
01305    well).  */
01306       if (!COMPLETE_TYPE_P (basetype))
01307   {
01308     int j;
01309     /* The base type is of incomplete type.  It is
01310        probably best to pretend that it does not
01311        exist.  */
01312     if (i == n_baseclasses-1)
01313       TREE_VEC_ELT (binfos, i) = NULL_TREE;
01314     TREE_VEC_LENGTH (binfos) -= 1;
01315     n_baseclasses -= 1;
01316     for (j = i; j+1 < n_baseclasses; j++)
01317       TREE_VEC_ELT (binfos, j) = TREE_VEC_ELT (binfos, j+1);
01318     continue;
01319   }
01320 
01321       /* Effective C++ rule 14.  We only need to check TYPE_POLYMORPHIC_P
01322    here because the case of virtual functions but non-virtual
01323    dtor is handled in finish_struct_1.  */
01324       if (warn_ecpp && ! TYPE_POLYMORPHIC_P (basetype)
01325     && TYPE_HAS_DESTRUCTOR (basetype))
01326   warning ("base class `%#T' has a non-virtual destructor",
01327         basetype);
01328 
01329       /* If the base class doesn't have copy constructors or
01330    assignment operators that take const references, then the
01331    derived class cannot have such a member automatically
01332    generated.  */
01333       if (! TYPE_HAS_CONST_INIT_REF (basetype))
01334   *cant_have_const_ctor_p = 1;
01335       if (TYPE_HAS_ASSIGN_REF (basetype)
01336     && !TYPE_HAS_CONST_ASSIGN_REF (basetype))
01337   *no_const_asn_ref_p = 1;
01338       /* Similarly, if the base class doesn't have a default
01339    constructor, then the derived class won't have an
01340    automatically generated default constructor.  */
01341       if (TYPE_HAS_CONSTRUCTOR (basetype)
01342     && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype))
01343   {
01344     *cant_have_default_ctor_p = 1;
01345     if (! TYPE_HAS_CONSTRUCTOR (t))
01346             pedwarn ("base `%T' with only non-default constructor in class without a constructor",
01347                         basetype);
01348   }
01349 
01350       if (TREE_VIA_VIRTUAL (base_binfo))
01351   /* A virtual base does not effect nearly emptiness.  */
01352   ;
01353       else if (CLASSTYPE_NEARLY_EMPTY_P (basetype))
01354   {
01355     if (seen_non_virtual_nearly_empty_base_p)
01356       /* And if there is more than one nearly empty base, then the
01357          derived class is not nearly empty either.  */
01358       CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
01359     else
01360       /* Remember we've seen one.  */
01361       seen_non_virtual_nearly_empty_base_p = 1;
01362   }
01363       else if (!is_empty_class (basetype))
01364   /* If the base class is not empty or nearly empty, then this
01365      class cannot be nearly empty.  */
01366   CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
01367 
01368       /* A lot of properties from the bases also apply to the derived
01369    class.  */
01370       TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (basetype);
01371       TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) 
01372   |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype);
01373       TYPE_HAS_COMPLEX_ASSIGN_REF (t) 
01374   |= TYPE_HAS_COMPLEX_ASSIGN_REF (basetype);
01375       TYPE_HAS_COMPLEX_INIT_REF (t) |= TYPE_HAS_COMPLEX_INIT_REF (basetype);
01376       TYPE_OVERLOADS_CALL_EXPR (t) |= TYPE_OVERLOADS_CALL_EXPR (basetype);
01377       TYPE_OVERLOADS_ARRAY_REF (t) |= TYPE_OVERLOADS_ARRAY_REF (basetype);
01378       TYPE_OVERLOADS_ARROW (t) |= TYPE_OVERLOADS_ARROW (basetype);
01379       TYPE_POLYMORPHIC_P (t) |= TYPE_POLYMORPHIC_P (basetype);
01380       CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) 
01381   |= CLASSTYPE_CONTAINS_EMPTY_CLASS_P (basetype);
01382     }
01383 }
01384 
01385 /* Binfo FROM is within a virtual hierarchy which is being reseated to
01386    TO. Move primary information from FROM to TO, and recursively traverse
01387    into FROM's bases. The hierarchy is dominated by TYPE.  MAPPINGS is an
01388    assoc list of binfos that have already been reseated.  */
01389 
01390 static void
01391 force_canonical_binfo_r (to, from, type, mappings)
01392      tree to;
01393      tree from;
01394      tree type;
01395      tree mappings;
01396 {
01397   int i, n_baseclasses = BINFO_N_BASETYPES (from);
01398 
01399   my_friendly_assert (to != from, 20010905);
01400   BINFO_INDIRECT_PRIMARY_P (to)
01401           = BINFO_INDIRECT_PRIMARY_P (from);
01402   BINFO_INDIRECT_PRIMARY_P (from) = 0;
01403   BINFO_UNSHARED_MARKED (to) = BINFO_UNSHARED_MARKED (from);
01404   BINFO_UNSHARED_MARKED (from) = 0;
01405   BINFO_LOST_PRIMARY_P (to) = BINFO_LOST_PRIMARY_P (from);
01406   BINFO_LOST_PRIMARY_P (from) = 0;
01407   if (BINFO_PRIMARY_P (from))
01408     {
01409       tree primary = BINFO_PRIMARY_BASE_OF (from);
01410       tree assoc;
01411       
01412       /* We might have just moved the primary base too, see if it's on our
01413          mappings.  */
01414       assoc = purpose_member (primary, mappings);
01415       if (assoc)
01416         primary = TREE_VALUE (assoc);
01417       BINFO_PRIMARY_BASE_OF (to) = primary;
01418       BINFO_PRIMARY_BASE_OF (from) = NULL_TREE;
01419     }
01420   my_friendly_assert (same_type_p (BINFO_TYPE (to), BINFO_TYPE (from)),
01421           20010104);
01422   mappings = tree_cons (from, to, mappings);
01423 
01424   if (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (from))
01425       && TREE_VIA_VIRTUAL (CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (from))))
01426     {
01427       tree from_primary = get_primary_binfo (from);
01428       
01429       if (BINFO_PRIMARY_BASE_OF (from_primary) == from)
01430   force_canonical_binfo (get_primary_binfo (to), from_primary,
01431              type, mappings);
01432     }
01433   
01434   for (i = 0; i != n_baseclasses; i++)
01435     {
01436       tree from_binfo = BINFO_BASETYPE (from, i);
01437       tree to_binfo = BINFO_BASETYPE (to, i);
01438 
01439       if (TREE_VIA_VIRTUAL (from_binfo))
01440         {
01441     if (BINFO_PRIMARY_P (from_binfo) &&
01442         purpose_member (BINFO_PRIMARY_BASE_OF (from_binfo), mappings))
01443       /* This base is a primary of some binfo we have already
01444          reseated. We must reseat this one too.  */
01445             force_canonical_binfo (to_binfo, from_binfo, type, mappings);
01446         }
01447       else
01448         force_canonical_binfo_r (to_binfo, from_binfo, type, mappings);
01449     }
01450 }
01451 
01452 /* FROM is the canonical binfo for a virtual base. It is being reseated to
01453    make TO the canonical binfo, within the hierarchy dominated by TYPE.
01454    MAPPINGS is an assoc list of binfos that have already been reseated.
01455    Adjust any non-virtual bases within FROM, and also move any virtual bases
01456    which are canonical.  This complication arises because selecting primary
01457    bases walks in inheritance graph order, but we don't share binfos for
01458    virtual bases, hence we can fill in the primaries for a virtual base,
01459    and then discover that a later base requires the virtual as its
01460    primary.  */
01461 
01462 static void
01463 force_canonical_binfo (to, from, type, mappings)
01464      tree to;
01465      tree from;
01466      tree type;
01467      tree mappings;
01468 {
01469   tree assoc = purpose_member (BINFO_TYPE (to),
01470                    CLASSTYPE_VBASECLASSES (type));
01471   if (TREE_VALUE (assoc) != to)
01472     {
01473       TREE_VALUE (assoc) = to;
01474       force_canonical_binfo_r (to, from, type, mappings);
01475     }
01476 }
01477 
01478 /* Make BASE_BINFO the a primary virtual base within the hierarchy
01479    dominated by TYPE. Returns BASE_BINFO, if it is not already one, NULL
01480    otherwise (because something else has already made it primary).  */
01481 
01482 static tree
01483 mark_primary_virtual_base (base_binfo, type)
01484      tree base_binfo;
01485      tree type;
01486 {
01487   tree shared_binfo = binfo_for_vbase (BINFO_TYPE (base_binfo), type);
01488 
01489   if (BINFO_PRIMARY_P (shared_binfo))
01490     {
01491       /* It's already allocated in the hierarchy. BINFO won't have a
01492          primary base in this hierarchy, even though the complete object
01493          BINFO is for, would do.  */
01494       return NULL_TREE;
01495     }
01496      
01497   /* We need to make sure that the assoc list
01498      CLASSTYPE_VBASECLASSES of TYPE, indicates this particular
01499      primary BINFO for the virtual base, as this is the one
01500      that'll really exist.  */
01501   if (base_binfo != shared_binfo)
01502     force_canonical_binfo (base_binfo, shared_binfo, type, NULL);
01503 
01504   return base_binfo;
01505 }
01506 
01507 /* If BINFO is an unmarked virtual binfo for a class with a primary virtual
01508    base, then BINFO has no primary base in this graph.  Called from
01509    mark_primary_bases.  DATA is the most derived type.  */
01510 
01511 static tree dfs_unshared_virtual_bases (binfo, data)
01512      tree binfo;
01513      void *data;
01514 {
01515   tree t = (tree) data;
01516   
01517   if (!BINFO_UNSHARED_MARKED (binfo)
01518       && CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (binfo)))
01519     {
01520       /* This morally virtual base has a primary base when it
01521          is a complete object. We need to locate the shared instance
01522          of this binfo in the type dominated by T. We duplicate the
01523          primary base information from there to here.  */
01524       tree vbase;
01525       tree unshared_base;
01526       
01527       for (vbase = binfo; !TREE_VIA_VIRTUAL (vbase);
01528      vbase = BINFO_INHERITANCE_CHAIN (vbase))
01529   continue;
01530       unshared_base = get_original_base (binfo,
01531            binfo_for_vbase (BINFO_TYPE (vbase),
01532                 t));
01533       my_friendly_assert (unshared_base != binfo, 20010612);
01534       BINFO_LOST_PRIMARY_P (binfo) = BINFO_LOST_PRIMARY_P (unshared_base);
01535       if (!BINFO_LOST_PRIMARY_P (binfo))
01536   BINFO_PRIMARY_BASE_OF (get_primary_binfo (binfo)) = binfo;
01537     }
01538   
01539   if (binfo != TYPE_BINFO (t))
01540     /* The vtable fields will have been copied when duplicating the
01541        base binfos. That information is bogus, make sure we don't try
01542        and use it.  */
01543     BINFO_VTABLE (binfo) = NULL_TREE;
01544 
01545   /* If this is a virtual primary base, make sure its offset matches
01546      that which it is primary for.  */
01547   if (BINFO_PRIMARY_P (binfo) && TREE_VIA_VIRTUAL (binfo) &&
01548       binfo_for_vbase (BINFO_TYPE (binfo), t) == binfo)
01549     {
01550       tree delta = size_diffop (BINFO_OFFSET (BINFO_PRIMARY_BASE_OF (binfo)),
01551         BINFO_OFFSET (binfo));
01552       if (!integer_zerop (delta))
01553   propagate_binfo_offsets (binfo, delta, t);
01554     }
01555   
01556   BINFO_UNSHARED_MARKED (binfo) = 0;
01557   return NULL;
01558 }
01559 
01560 /* Set BINFO_PRIMARY_BASE_OF for all binfos in the hierarchy
01561    dominated by TYPE that are primary bases.  */
01562 
01563 static void
01564 mark_primary_bases (type)
01565      tree type;
01566 {
01567   tree binfo;
01568   
01569   /* Walk the bases in inheritance graph order.  */
01570   for (binfo = TYPE_BINFO (type); binfo; binfo = TREE_CHAIN (binfo))
01571     {
01572       tree base_binfo;
01573       
01574       if (!CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (binfo)))
01575         /* Not a dynamic base.  */
01576         continue;
01577 
01578       base_binfo = get_primary_binfo (binfo);
01579 
01580       if (TREE_VIA_VIRTUAL (base_binfo))
01581         base_binfo = mark_primary_virtual_base (base_binfo, type);
01582 
01583       if (base_binfo)
01584         BINFO_PRIMARY_BASE_OF (base_binfo) = binfo;
01585       else
01586   BINFO_LOST_PRIMARY_P (binfo) = 1;
01587       
01588       BINFO_UNSHARED_MARKED (binfo) = 1;
01589     }
01590   /* There could remain unshared morally virtual bases which were not
01591      visited in the inheritance graph walk. These bases will have lost
01592      their virtual primary base (should they have one). We must now
01593      find them. Also we must fix up the BINFO_OFFSETs of primary
01594      virtual bases. We could not do that as we went along, as they
01595      were originally copied from the bases we inherited from by
01596      unshare_base_binfos. That may have decided differently about
01597      where a virtual primary base went.  */
01598   dfs_walk (TYPE_BINFO (type), dfs_unshared_virtual_bases, NULL, type);
01599 }
01600 
01601 /* Make the BINFO the primary base of T.  */
01602 
01603 static void
01604 set_primary_base (t, binfo)
01605      tree t;
01606      tree binfo;
01607 {
01608   tree basetype;
01609 
01610   CLASSTYPE_PRIMARY_BINFO (t) = binfo;
01611   basetype = BINFO_TYPE (binfo);
01612   TYPE_BINFO_VTABLE (t) = TYPE_BINFO_VTABLE (basetype);
01613   TYPE_BINFO_VIRTUALS (t) = TYPE_BINFO_VIRTUALS (basetype);
01614   TYPE_VFIELD (t) = TYPE_VFIELD (basetype);
01615 }
01616 
01617 /* Determine the primary class for T.  */
01618 
01619 static void
01620 determine_primary_base (t)
01621      tree t;
01622 {
01623   int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
01624   tree vbases;
01625   tree type_binfo;
01626 
01627   /* If there are no baseclasses, there is certainly no primary base.  */
01628   if (n_baseclasses == 0)
01629     return;
01630 
01631   type_binfo = TYPE_BINFO (t);
01632 
01633   for (i = 0; i < n_baseclasses; i++)
01634     {
01635       tree base_binfo = BINFO_BASETYPE (type_binfo, i);
01636       tree basetype = BINFO_TYPE (base_binfo);
01637 
01638       if (TYPE_CONTAINS_VPTR_P (basetype))
01639   {
01640     /* We prefer a non-virtual base, although a virtual one will
01641        do.  */
01642     if (TREE_VIA_VIRTUAL (base_binfo))
01643       continue;
01644 
01645     if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
01646       {
01647         set_primary_base (t, base_binfo);
01648         CLASSTYPE_VFIELDS (t) = copy_list (CLASSTYPE_VFIELDS (basetype));
01649       }
01650     else
01651       {
01652         tree vfields;
01653 
01654         /* Only add unique vfields, and flatten them out as we go.  */
01655         for (vfields = CLASSTYPE_VFIELDS (basetype);
01656        vfields;
01657        vfields = TREE_CHAIN (vfields))
01658     if (VF_BINFO_VALUE (vfields) == NULL_TREE
01659         || ! TREE_VIA_VIRTUAL (VF_BINFO_VALUE (vfields)))
01660       CLASSTYPE_VFIELDS (t) 
01661         = tree_cons (base_binfo, 
01662          VF_BASETYPE_VALUE (vfields),
01663          CLASSTYPE_VFIELDS (t));
01664       }
01665   }
01666     }
01667 
01668   if (!TYPE_VFIELD (t))
01669     CLASSTYPE_PRIMARY_BINFO (t) = NULL_TREE;
01670 
01671   /* Find the indirect primary bases - those virtual bases which are primary
01672      bases of something else in this hierarchy.  */
01673   for (vbases = CLASSTYPE_VBASECLASSES (t);
01674        vbases;
01675        vbases = TREE_CHAIN (vbases)) 
01676     {
01677       tree vbase_binfo = TREE_VALUE (vbases);
01678 
01679       /* See if this virtual base is an indirect primary base.  To be so,
01680          it must be a primary base within the hierarchy of one of our
01681          direct bases.  */
01682       for (i = 0; i < n_baseclasses; ++i) 
01683   {
01684     tree basetype = TYPE_BINFO_BASETYPE (t, i);
01685     tree v;
01686 
01687     for (v = CLASSTYPE_VBASECLASSES (basetype); 
01688          v; 
01689          v = TREE_CHAIN (v))
01690       {
01691         tree base_vbase = TREE_VALUE (v);
01692         
01693         if (BINFO_PRIMARY_P (base_vbase)
01694       && same_type_p (BINFO_TYPE (base_vbase),
01695                             BINFO_TYPE (vbase_binfo)))
01696     {
01697       BINFO_INDIRECT_PRIMARY_P (vbase_binfo) = 1;
01698       break;
01699     }
01700       }
01701 
01702     /* If we've discovered that this virtual base is an indirect
01703        primary base, then we can move on to the next virtual
01704        base.  */
01705     if (BINFO_INDIRECT_PRIMARY_P (vbase_binfo))
01706       break;
01707   }
01708     }
01709 
01710   /* A "nearly-empty" virtual base class can be the primary base
01711      class, if no non-virtual polymorphic base can be found.  */
01712   if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
01713     {
01714       /* If not NULL, this is the best primary base candidate we have
01715          found so far.  */
01716       tree candidate = NULL_TREE;
01717       tree base_binfo;
01718 
01719       /* Loop over the baseclasses.  */
01720       for (base_binfo = TYPE_BINFO (t);
01721      base_binfo;
01722      base_binfo = TREE_CHAIN (base_binfo))
01723   {
01724     tree basetype = BINFO_TYPE (base_binfo);
01725 
01726     if (TREE_VIA_VIRTUAL (base_binfo) 
01727         && CLASSTYPE_NEARLY_EMPTY_P (basetype))
01728       {
01729         /* If this is not an indirect primary base, then it's
01730      definitely our primary base.  */
01731         if (!BINFO_INDIRECT_PRIMARY_P (base_binfo))
01732     {
01733       candidate = base_binfo;
01734       break;
01735     }
01736 
01737         /* If this is an indirect primary base, it still could be
01738            our primary base -- unless we later find there's another
01739            nearly-empty virtual base that isn't an indirect
01740            primary base.  */
01741         if (!candidate)
01742     candidate = base_binfo;
01743       }
01744   }
01745 
01746       /* If we've got a primary base, use it.  */
01747       if (candidate)
01748   {
01749     set_primary_base (t, candidate);
01750     CLASSTYPE_VFIELDS (t) 
01751       = copy_list (CLASSTYPE_VFIELDS (BINFO_TYPE (candidate)));
01752   } 
01753     }
01754 
01755   /* Mark the primary base classes at this point.  */
01756   mark_primary_bases (t);
01757 }
01758 
01759 /* Set memoizing fields and bits of T (and its variants) for later
01760    use.  */
01761 
01762 static void
01763 finish_struct_bits (t)
01764      tree t;
01765 {
01766   int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
01767 
01768   /* Fix up variants (if any).  */
01769   tree variants = TYPE_NEXT_VARIANT (t);
01770   while (variants)
01771     {
01772       /* These fields are in the _TYPE part of the node, not in
01773    the TYPE_LANG_SPECIFIC component, so they are not shared.  */
01774       TYPE_HAS_CONSTRUCTOR (variants) = TYPE_HAS_CONSTRUCTOR (t);
01775       TYPE_HAS_DESTRUCTOR (variants) = TYPE_HAS_DESTRUCTOR (t);
01776       TYPE_NEEDS_CONSTRUCTING (variants) = TYPE_NEEDS_CONSTRUCTING (t);
01777       TYPE_HAS_NONTRIVIAL_DESTRUCTOR (variants) 
01778   = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
01779 
01780       TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (variants) 
01781   = TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (t);
01782       TYPE_POLYMORPHIC_P (variants) = TYPE_POLYMORPHIC_P (t);
01783       TYPE_USES_VIRTUAL_BASECLASSES (variants) = TYPE_USES_VIRTUAL_BASECLASSES (t);
01784       /* Copy whatever these are holding today.  */
01785       TYPE_MIN_VALUE (variants) = TYPE_MIN_VALUE (t);
01786       TYPE_MAX_VALUE (variants) = TYPE_MAX_VALUE (t);
01787       TYPE_FIELDS (variants) = TYPE_FIELDS (t);
01788       TYPE_SIZE (variants) = TYPE_SIZE (t);
01789       TYPE_SIZE_UNIT (variants) = TYPE_SIZE_UNIT (t);
01790       variants = TYPE_NEXT_VARIANT (variants);
01791     }
01792 
01793   if (n_baseclasses && TYPE_POLYMORPHIC_P (t))
01794     /* For a class w/o baseclasses, `finish_struct' has set
01795        CLASS_TYPE_ABSTRACT_VIRTUALS correctly (by
01796        definition). Similarly for a class whose base classes do not
01797        have vtables. When neither of these is true, we might have
01798        removed abstract virtuals (by providing a definition), added
01799        some (by declaring new ones), or redeclared ones from a base
01800        class. We need to recalculate what's really an abstract virtual
01801        at this point (by looking in the vtables).  */
01802       get_pure_virtuals (t);
01803 
01804   if (n_baseclasses)
01805     {
01806       /* Notice whether this class has type conversion functions defined.  */
01807       tree binfo = TYPE_BINFO (t);
01808       tree binfos = BINFO_BASETYPES (binfo);
01809       tree basetype;
01810 
01811       for (i = n_baseclasses-1; i >= 0; i--)
01812   {
01813     basetype = BINFO_TYPE (TREE_VEC_ELT (binfos, i));
01814 
01815     TYPE_HAS_CONVERSION (t) |= TYPE_HAS_CONVERSION (basetype);
01816   }
01817     }
01818 
01819   /* If this type has a copy constructor or a destructor, force its mode to
01820      be BLKmode, and force its TREE_ADDRESSABLE bit to be nonzero.  This
01821      will cause it to be passed by invisible reference and prevent it from
01822      being returned in a register.  */
01823   if (! TYPE_HAS_TRIVIAL_INIT_REF (t) || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
01824     {
01825       tree variants;
01826       DECL_MODE (TYPE_MAIN_DECL (t)) = BLKmode;
01827       for (variants = t; variants; variants = TYPE_NEXT_VARIANT (variants))
01828   {
01829     TYPE_MODE (variants) = BLKmode;
01830     TREE_ADDRESSABLE (variants) = 1;
01831   }
01832     }
01833 }
01834 
01835 /* Issue warnings about T having private constructors, but no friends,
01836    and so forth.  
01837 
01838    HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or
01839    static members.  HAS_NONPRIVATE_STATIC_FN is nonzero if T has any
01840    non-private static member functions.  */
01841 
01842 static void
01843 maybe_warn_about_overly_private_class (t)
01844      tree t;
01845 {
01846   int has_member_fn = 0;
01847   int has_nonprivate_method = 0;
01848   tree fn;
01849 
01850   if (!warn_ctor_dtor_privacy
01851       /* If the class has friends, those entities might create and
01852    access instances, so we should not warn.  */
01853       || (CLASSTYPE_FRIEND_CLASSES (t)
01854     || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
01855       /* We will have warned when the template was declared; there's
01856    no need to warn on every instantiation.  */
01857       || CLASSTYPE_TEMPLATE_INSTANTIATION (t))
01858     /* There's no reason to even consider warning about this 
01859        class.  */
01860     return;
01861     
01862   /* We only issue one warning, if more than one applies, because
01863      otherwise, on code like:
01864 
01865      class A {
01866        // Oops - forgot `public:'
01867        A();
01868        A(const A&);
01869        ~A();
01870      };
01871 
01872      we warn several times about essentially the same problem.  */
01873 
01874   /* Check to see if all (non-constructor, non-destructor) member
01875      functions are private.  (Since there are no friends or
01876      non-private statics, we can't ever call any of the private member
01877      functions.)  */
01878   for (fn = TYPE_METHODS (t); fn; fn = TREE_CHAIN (fn))
01879     /* We're not interested in compiler-generated methods; they don't
01880        provide any way to call private members.  */
01881     if (!DECL_ARTIFICIAL (fn)) 
01882       {
01883   if (!TREE_PRIVATE (fn))
01884     {
01885       if (DECL_STATIC_FUNCTION_P (fn)) 
01886         /* A non-private static member function is just like a
01887      friend; it can create and invoke private member
01888      functions, and be accessed without a class
01889      instance.  */
01890         return;
01891     
01892       has_nonprivate_method = 1;
01893       /* Keep searching for a static member function.  */
01894     }
01895   else if (!DECL_CONSTRUCTOR_P (fn) && !DECL_DESTRUCTOR_P (fn))
01896     has_member_fn = 1;
01897       } 
01898 
01899   if (!has_nonprivate_method && has_member_fn) 
01900     {
01901       /* There are no non-private methods, and there's at least one
01902    private member function that isn't a constructor or
01903    destructor.  (If all the private members are
01904    constructors/destructors we want to use the code below that
01905    issues error messages specifically referring to
01906    constructors/destructors.)  */
01907       int i;
01908       tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
01909       for (i = 0; i < CLASSTYPE_N_BASECLASSES (t); i++)
01910   if (TREE_VIA_PUBLIC (TREE_VEC_ELT (binfos, i))
01911       || TREE_VIA_PROTECTED (TREE_VEC_ELT (binfos, i)))
01912     {
01913       has_nonprivate_method = 1;
01914       break;
01915     }
01916       if (!has_nonprivate_method) 
01917   {
01918     warning ("all member functions in class `%T' are private", t);
01919     return;
01920   }
01921     }
01922 
01923   /* Even if some of the member functions are non-private, the class
01924      won't be useful for much if all the constructors or destructors
01925      are private: such an object can never be created or destroyed.  */
01926   if (TYPE_HAS_DESTRUCTOR (t))
01927     {
01928       tree dtor = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 1);
01929 
01930       if (TREE_PRIVATE (dtor))
01931   {
01932     warning ("`%#T' only defines a private destructor and has no friends",
01933           t);
01934     return;
01935   }
01936     }
01937 
01938   if (TYPE_HAS_CONSTRUCTOR (t))
01939     {
01940       int nonprivate_ctor = 0;
01941     
01942       /* If a non-template class does not define a copy
01943    constructor, one is defined for it, enabling it to avoid
01944    this warning.  For a template class, this does not
01945    happen, and so we would normally get a warning on:
01946 
01947      template <class T> class C { private: C(); };  
01948     
01949    To avoid this asymmetry, we check TYPE_HAS_INIT_REF.  All
01950    complete non-template or fully instantiated classes have this
01951    flag set.  */
01952       if (!TYPE_HAS_INIT_REF (t))
01953   nonprivate_ctor = 1;
01954       else 
01955   for (fn = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 0);
01956        fn;
01957        fn = OVL_NEXT (fn)) 
01958     {
01959       tree ctor = OVL_CURRENT (fn);
01960       /* Ideally, we wouldn't count copy constructors (or, in
01961          fact, any constructor that takes an argument of the
01962          class type as a parameter) because such things cannot
01963          be used to construct an instance of the class unless
01964          you already have one.  But, for now at least, we're
01965          more generous.  */
01966       if (! TREE_PRIVATE (ctor))
01967         {
01968     nonprivate_ctor = 1;
01969     break;
01970         }
01971     }
01972 
01973       if (nonprivate_ctor == 0)
01974   {
01975     warning ("`%#T' only defines private constructors and has no friends",
01976           t);
01977     return;
01978   }
01979     }
01980 }
01981 
01982 /* Function to help qsort sort FIELD_DECLs by name order.  */
01983 
01984 static int
01985 field_decl_cmp (x, y)
01986      const tree *x, *y;
01987 {
01988   if (DECL_NAME (*x) == DECL_NAME (*y))
01989     /* A nontype is "greater" than a type.  */
01990     return DECL_DECLARES_TYPE_P (*y) - DECL_DECLARES_TYPE_P (*x);
01991   if (DECL_NAME (*x) == NULL_TREE)
01992     return -1;
01993   if (DECL_NAME (*y) == NULL_TREE)
01994     return 1;
01995   if (DECL_NAME (*x) < DECL_NAME (*y))
01996     return -1;
01997   return 1;
01998 }
01999 
02000 /* Comparison function to compare two TYPE_METHOD_VEC entries by name.  */
02001 
02002 static int
02003 method_name_cmp (m1, m2)
02004      const tree *m1, *m2;
02005 {
02006   if (*m1 == NULL_TREE && *m2 == NULL_TREE)
02007     return 0;
02008   if (*m1 == NULL_TREE)
02009     return -1;
02010   if (*m2 == NULL_TREE)
02011     return 1;
02012   if (DECL_NAME (OVL_CURRENT (*m1)) < DECL_NAME (OVL_CURRENT (*m2)))
02013     return -1;
02014   return 1;
02015 }
02016 
02017 /* Warn about duplicate methods in fn_fields.  Also compact method
02018    lists so that lookup can be made faster.
02019 
02020    Data Structure: List of method lists.  The outer list is a
02021    TREE_LIST, whose TREE_PURPOSE field is the field name and the
02022    TREE_VALUE is the DECL_CHAIN of the FUNCTION_DECLs.  TREE_CHAIN
02023    links the entire list of methods for TYPE_METHODS.  Friends are
02024    chained in the same way as member functions (? TREE_CHAIN or
02025    DECL_CHAIN), but they live in the TREE_TYPE field of the outer
02026    list.  That allows them to be quickly deleted, and requires no
02027    extra storage.
02028 
02029    Sort methods that are not special (i.e., constructors, destructors,
02030    and type conversion operators) so that we can find them faster in
02031    search.  */
02032 
02033 static void
02034 finish_struct_methods (t)
02035      tree t;
02036 {
02037   tree fn_fields;
02038   tree method_vec;
02039   int slot, len;
02040 
02041   if (!TYPE_METHODS (t))
02042     {
02043       /* Clear these for safety; perhaps some parsing error could set
02044    these incorrectly.  */
02045       TYPE_HAS_CONSTRUCTOR (t) = 0;
02046       TYPE_HAS_DESTRUCTOR (t) = 0;
02047       CLASSTYPE_METHOD_VEC (t) = NULL_TREE;
02048       return;
02049     }
02050 
02051   method_vec = CLASSTYPE_METHOD_VEC (t);
02052   my_friendly_assert (method_vec != NULL_TREE, 19991215);
02053   len = TREE_VEC_LENGTH (method_vec);
02054 
02055   /* First fill in entry 0 with the constructors, entry 1 with destructors,
02056      and the next few with type conversion operators (if any).  */
02057   for (fn_fields = TYPE_METHODS (t); fn_fields; 
02058        fn_fields = TREE_CHAIN (fn_fields))
02059     /* Clear out this flag.  */
02060     DECL_IN_AGGR_P (fn_fields) = 0;
02061 
02062   if (TYPE_HAS_DESTRUCTOR (t) && !CLASSTYPE_DESTRUCTORS (t))
02063     /* We thought there was a destructor, but there wasn't.  Some
02064        parse errors cause this anomalous situation.  */
02065     TYPE_HAS_DESTRUCTOR (t) = 0;
02066     
02067   /* Issue warnings about private constructors and such.  If there are
02068      no methods, then some public defaults are generated.  */
02069   maybe_warn_about_overly_private_class (t);
02070 
02071   /* Now sort the methods.  */
02072   while (len > 2 && TREE_VEC_ELT (method_vec, len-1) == NULL_TREE)
02073     len--;
02074   TREE_VEC_LENGTH (method_vec) = len;
02075 
02076   /* The type conversion ops have to live at the front of the vec, so we
02077      can't sort them.  */
02078   for (slot = 2; slot < len; ++slot)
02079     {
02080       tree fn = TREE_VEC_ELT (method_vec, slot);
02081   
02082       if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
02083   break;
02084     }
02085   if (len - slot > 1)
02086     qsort (&TREE_VEC_ELT (method_vec, slot), len-slot, sizeof (tree),
02087      (int (*)(const void *, const void *))method_name_cmp);
02088 }
02089 
02090 /* Emit error when a duplicate definition of a type is seen.  Patch up.  */
02091 
02092 void
02093 duplicate_tag_error (t)
02094      tree t;
02095 {
02096   error ("redefinition of `%#T'", t);
02097   cp_error_at ("previous definition of `%#T'", t);
02098 
02099   /* Pretend we haven't defined this type.  */
02100 
02101   /* All of the component_decl's were TREE_CHAINed together in the parser.
02102      finish_struct_methods walks these chains and assembles all methods with
02103      the same base name into DECL_CHAINs. Now we don't need the parser chains
02104      anymore, so we unravel them.  */
02105 
02106   /* This used to be in finish_struct, but it turns out that the
02107      TREE_CHAIN is used by dbxout_type_methods and perhaps some other
02108      things...  */
02109   if (CLASSTYPE_METHOD_VEC (t)) 
02110     {
02111       tree method_vec = CLASSTYPE_METHOD_VEC (t);
02112       int i, len  = TREE_VEC_LENGTH (method_vec);
02113       for (i = 0; i < len; i++)
02114   {
02115     tree unchain = TREE_VEC_ELT (method_vec, i);
02116     while (unchain != NULL_TREE) 
02117       {
02118         TREE_CHAIN (OVL_CURRENT (unchain)) = NULL_TREE;
02119         unchain = OVL_NEXT (unchain);
02120       }
02121   }
02122     }
02123 
02124   if (TYPE_LANG_SPECIFIC (t))
02125     {
02126       tree binfo = TYPE_BINFO (t);
02127       int interface_only = CLASSTYPE_INTERFACE_ONLY (t);
02128       int interface_unknown = CLASSTYPE_INTERFACE_UNKNOWN (t);
02129       tree template_info = CLASSTYPE_TEMPLATE_INFO (t);
02130       int use_template = CLASSTYPE_USE_TEMPLATE (t);
02131 
02132       memset ((char *) TYPE_LANG_SPECIFIC (t), 0, sizeof (struct lang_type));
02133       BINFO_BASETYPES(binfo) = NULL_TREE;
02134 
02135       TYPE_LANG_SPECIFIC (t)->u.h.is_lang_type_class = 1;
02136       TYPE_BINFO (t) = binfo;
02137       CLASSTYPE_INTERFACE_ONLY (t) = interface_only;
02138       SET_CLASSTYPE_INTERFACE_UNKNOWN_X (t, interface_unknown);
02139       TYPE_REDEFINED (t) = 1;
02140       CLASSTYPE_TEMPLATE_INFO (t) = template_info;
02141       CLASSTYPE_USE_TEMPLATE (t) = use_template;
02142       CLASSTYPE_DECL_LIST (t) = NULL_TREE;
02143     }
02144   TYPE_SIZE (t) = NULL_TREE;
02145   TYPE_MODE (t) = VOIDmode;
02146   TYPE_FIELDS (t) = NULL_TREE;
02147   TYPE_METHODS (t) = NULL_TREE;
02148   TYPE_VFIELD (t) = NULL_TREE;
02149   TYPE_CONTEXT (t) = NULL_TREE;
02150   
02151   /* Clear TYPE_LANG_FLAGS -- those in TYPE_LANG_SPECIFIC are cleared above.  */
02152   TYPE_LANG_FLAG_0 (t) = 0;
02153   TYPE_LANG_FLAG_1 (t) = 0;
02154   TYPE_LANG_FLAG_2 (t) = 0;
02155   TYPE_LANG_FLAG_3 (t) = 0;
02156   TYPE_LANG_FLAG_4 (t) = 0;
02157   TYPE_LANG_FLAG_5 (t) = 0;
02158   TYPE_LANG_FLAG_6 (t) = 0;
02159   /* But not this one.  */
02160   SET_IS_AGGR_TYPE (t, 1);
02161 }
02162 
02163 /* Make BINFO's vtable have N entries, including RTTI entries,
02164    vbase and vcall offsets, etc.  Set its type and call the backend
02165    to lay it out.  */
02166 
02167 static void
02168 layout_vtable_decl (binfo, n)
02169      tree binfo;
02170      int n;
02171 {
02172   tree atype;
02173   tree vtable;
02174 
02175   atype = build_cplus_array_type (vtable_entry_type, 
02176           build_index_type (size_int (n - 1)));
02177   layout_type (atype);
02178 
02179   /* We may have to grow the vtable.  */
02180   vtable = get_vtbl_decl_for_binfo (binfo);
02181   if (!same_type_p (TREE_TYPE (vtable), atype))
02182     {
02183       TREE_TYPE (vtable) = atype;
02184       DECL_SIZE (vtable) = DECL_SIZE_UNIT (vtable) = NULL_TREE;
02185       layout_decl (vtable, 0);
02186 
02187       /* At one time the vtable info was grabbed 2 words at a time.  This
02188    fails on SPARC unless you have 8-byte alignment.  */
02189       DECL_ALIGN (vtable) = MAX (TYPE_ALIGN (double_type_node),
02190          DECL_ALIGN (vtable));
02191     }
02192 }
02193 
02194 /* True iff FNDECL and BASE_FNDECL (both non-static member functions)
02195    have the same signature.  */
02196 
02197 int
02198 same_signature_p (fndecl, base_fndecl)
02199      tree fndecl, base_fndecl;
02200 {
02201   /* One destructor overrides another if they are the same kind of
02202      destructor.  */
02203   if (DECL_DESTRUCTOR_P (base_fndecl) && DECL_DESTRUCTOR_P (fndecl)
02204       && special_function_p (base_fndecl) == special_function_p (fndecl))
02205     return 1;
02206   /* But a non-destructor never overrides a destructor, nor vice
02207      versa, nor do different kinds of destructors override
02208      one-another.  For example, a complete object destructor does not
02209      override a deleting destructor.  */
02210   if (DECL_DESTRUCTOR_P (base_fndecl) || DECL_DESTRUCTOR_P (fndecl))
02211     return 0;
02212 
02213   if (DECL_NAME (fndecl) == DECL_NAME (base_fndecl))
02214     {
02215       tree types, base_types;
02216       types = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
02217       base_types = TYPE_ARG_TYPES (TREE_TYPE (base_fndecl));
02218       if ((TYPE_QUALS (TREE_TYPE (TREE_VALUE (base_types)))
02219      == TYPE_QUALS (TREE_TYPE (TREE_VALUE (types))))
02220     && compparms (TREE_CHAIN (base_types), TREE_CHAIN (types)))
02221   return 1;
02222     }
02223   return 0;
02224 }
02225 
02226 /* Called from base_derived_from via dfs_walk.  */
02227 
02228 static tree
02229 dfs_base_derived_from (tree binfo, void *data)
02230 {
02231   tree base = (tree) data;
02232 
02233   if (same_type_p (TREE_TYPE (base), TREE_TYPE (binfo))
02234       && tree_int_cst_equal (BINFO_OFFSET (base), BINFO_OFFSET (binfo)))
02235     return error_mark_node;
02236 
02237   return NULL_TREE;
02238 }
02239 
02240 /* Returns TRUE if DERIVED is a binfo containing the binfo BASE as a
02241    subobject.  */
02242  
02243 static bool
02244 base_derived_from (tree derived, tree base)
02245 {
02246   return dfs_walk (derived, dfs_base_derived_from, NULL, base) != NULL_TREE;
02247 }
02248 
02249 typedef struct find_final_overrider_data_s {
02250   /* The function for which we are trying to find a final overrider.  */
02251   tree fn;
02252   /* The base class in which the function was declared.  */
02253   tree declaring_base;
02254   /* The most derived class in the hierarchy.  */
02255   tree most_derived_type;
02256   /* The candidate overriders.  */
02257   tree candidates;
02258 } find_final_overrider_data;
02259 
02260 /* Called from find_final_overrider via dfs_walk.  */
02261 
02262 static tree
02263 dfs_find_final_overrider (binfo, data)
02264      tree binfo;
02265      void *data;
02266 {
02267   find_final_overrider_data *ffod = (find_final_overrider_data *) data;
02268 
02269   if (same_type_p (BINFO_TYPE (binfo), 
02270        BINFO_TYPE (ffod->declaring_base))
02271       && tree_int_cst_equal (BINFO_OFFSET (binfo),
02272            BINFO_OFFSET (ffod->declaring_base)))
02273     {
02274       tree path;
02275       tree method;
02276 
02277       /* We haven't found an overrider yet.  */
02278       method = NULL_TREE;
02279       /* We've found a path to the declaring base.  Walk down the path
02280    looking for an overrider for FN.  */
02281       path = reverse_path (binfo);
02282       while (!same_type_p (BINFO_TYPE (TREE_VALUE (path)),
02283          ffod->most_derived_type))
02284   path = TREE_CHAIN (path);
02285       while (path)
02286   {
02287     method = look_for_overrides_here (BINFO_TYPE (TREE_VALUE (path)),
02288               ffod->fn);
02289     if (method)
02290       {
02291         path = TREE_VALUE (path);
02292         break;
02293       }
02294 
02295     path = TREE_CHAIN (path);
02296   }
02297 
02298       /* If we found an overrider, record the overriding function, and
02299    the base from which it came.  */
02300       if (path)
02301   {
02302     tree *candidate;
02303 
02304     /* Remove any candidates overridden by this new function.  */
02305     candidate = &ffod->candidates;
02306     while (*candidate)
02307       {
02308         /* If *CANDIDATE overrides METHOD, then METHOD
02309      cannot override anything else on the list.  */
02310         if (base_derived_from (TREE_VALUE (*candidate), path))
02311       return NULL_TREE;
02312         /* If METHOD overrides *CANDIDATE, remove *CANDIDATE.  */
02313         if (base_derived_from (path, TREE_VALUE (*candidate)))
02314     *candidate = TREE_CHAIN (*candidate);
02315         else
02316     candidate = &TREE_CHAIN (*candidate);
02317       }
02318 
02319     /* Add the new function.  */
02320     ffod->candidates = tree_cons (method, path, ffod->candidates);
02321   }
02322     }
02323 
02324   return NULL_TREE;
02325 }
02326 
02327 /* Returns a TREE_LIST whose TREE_PURPOSE is the final overrider for
02328    FN and whose TREE_VALUE is the binfo for the base where the
02329    overriding occurs.  BINFO (in the hierarchy dominated by the binfo
02330    DERIVED) is the base object in which FN is declared.  */
02331 
02332 static tree
02333 find_final_overrider (derived, binfo, fn)
02334      tree derived;
02335      tree binfo;
02336      tree fn;
02337 {
02338   find_final_overrider_data ffod;
02339 
02340   /* Getting this right is a little tricky.  This is valid:
02341 
02342        struct S { virtual void f (); };
02343        struct T { virtual void f (); };
02344        struct U : public S, public T { };
02345 
02346      even though calling `f' in `U' is ambiguous.  But, 
02347 
02348        struct R { virtual void f(); };
02349        struct S : virtual public R { virtual void f (); };
02350        struct T : virtual public R { virtual void f (); };
02351        struct U : public S, public T { };
02352 
02353      is not -- there's no way to decide whether to put `S::f' or
02354      `T::f' in the vtable for `R'.  
02355      
02356      The solution is to look at all paths to BINFO.  If we find
02357      different overriders along any two, then there is a problem.  */
02358   ffod.fn = fn;
02359   ffod.declaring_base = binfo;
02360   ffod.most_derived_type = BINFO_TYPE (derived);
02361   ffod.candidates = NULL_TREE;
02362 
02363   dfs_walk (derived,
02364       dfs_find_final_overrider,
02365       NULL,
02366       &ffod);
02367 
02368   /* If there was no winner, issue an error message.  */
02369   if (!ffod.candidates || TREE_CHAIN (ffod.candidates))
02370     {
02371       error ("no unique final overrider for `%D' in `%T'", fn, 
02372        BINFO_TYPE (derived));
02373       return error_mark_node;
02374     }
02375 
02376   return ffod.candidates;
02377 }
02378 
02379 /* Return the index of the vcall offset for FN when TYPE is used as a
02380    virtual base.  */
02381 
02382 static tree
02383 get_vcall_index (tree fn, tree type)
02384 {
02385   tree v;
02386 
02387   for (v = CLASSTYPE_VCALL_INDICES (type); v; v = TREE_CHAIN (v))
02388     if ((DECL_DESTRUCTOR_P (fn) && DECL_DESTRUCTOR_P (TREE_PURPOSE (v)))
02389   || same_signature_p (fn, TREE_PURPOSE (v)))
02390       break;
02391 
02392   /* There should always be an appropriate index.  */
02393   my_friendly_assert (v, 20021103);
02394 
02395   return TREE_VALUE (v);
02396 }
02397 
02398 /* Update an entry in the vtable for BINFO, which is in the hierarchy
02399    dominated by T.  FN has been overriden in BINFO; VIRTUALS points to the
02400    corresponding position in the BINFO_VIRTUALS list.  */
02401 
02402 static void
02403 update_vtable_entry_for_fn (t, binfo, fn, virtuals)
02404      tree t;
02405      tree binfo;
02406      tree fn;
02407      tree *virtuals;
02408 {
02409   tree b;
02410   tree overrider;
02411   tree delta;
02412   tree virtual_base;
02413   tree first_defn;
02414   bool lost = false;
02415 
02416   /* Find the nearest primary base (possibly binfo itself) which defines
02417      this function; this is the class the caller will convert to when
02418      calling FN through BINFO.  */
02419   for (b = binfo; ; b = get_primary_binfo (b))
02420     {
02421       if (look_for_overrides_here (BINFO_TYPE (b), fn))
02422   break;
02423 
02424       /* The nearest definition is from a lost primary.  */
02425       if (BINFO_LOST_PRIMARY_P (b))
02426   lost = true;
02427     }
02428   first_defn = b;
02429 
02430   /* Find the final overrider.  */
02431   overrider = find_final_overrider (TYPE_BINFO (t), b, fn);
02432   if (overrider == error_mark_node)
02433     return;
02434 
02435   /* Check for unsupported covariant returns again now that we've
02436      calculated the base offsets.  */
02437   check_final_overrider (TREE_PURPOSE (overrider), fn);
02438 
02439   /* Assume that we will produce a thunk that convert all the way to
02440      the final overrider, and not to an intermediate virtual base.  */
02441   virtual_base = NULL_TREE;
02442 
02443   /* See if we can convert to an intermediate virtual base first, and then
02444      use the vcall offset located there to finish the conversion.  */
02445   for (; b; b = BINFO_INHERITANCE_CHAIN (b))
02446     {
02447       /* If we find the final overrider, then we can stop
02448    walking.  */
02449       if (same_type_p (BINFO_TYPE (b), 
02450            BINFO_TYPE (TREE_VALUE (overrider))))
02451   break;
02452 
02453       /* If we find a virtual base, and we haven't yet found the
02454    overrider, then there is a virtual base between the
02455    declaring base (first_defn) and the final overrider.  */
02456       if (!virtual_base && TREE_VIA_VIRTUAL (b))
02457   virtual_base = b;
02458     }
02459 
02460   /* Compute the constant adjustment to the `this' pointer.  The
02461      `this' pointer, when this function is called, will point at BINFO
02462      (or one of its primary bases, which are at the same offset).  */
02463   if (virtual_base)
02464     /* The `this' pointer needs to be adjusted from the declaration to
02465        the nearest virtual base.  */
02466     delta = size_diffop (BINFO_OFFSET (virtual_base),
02467        BINFO_OFFSET (first_defn));
02468   else if (lost)
02469     /* If the nearest definition is in a lost primary, we don't need an
02470        entry in our vtable.  Except possibly in a constructor vtable,
02471        if we happen to get our primary back.  In that case, the offset
02472        will be zero, as it will be a primary base.  */
02473     delta = size_zero_node;
02474   else
02475     /* The `this' pointer needs to be adjusted from pointing to
02476        BINFO to pointing at the base where the final overrider
02477        appears.  */
02478     delta = size_diffop (BINFO_OFFSET (TREE_VALUE (overrider)),
02479        BINFO_OFFSET (binfo));
02480 
02481   modify_vtable_entry (t, 
02482            binfo, 
02483            TREE_PURPOSE (overrider),
02484            delta,
02485            virtuals);
02486 
02487   if (virtual_base)
02488     BV_VCALL_INDEX (*virtuals) 
02489       = get_vcall_index (TREE_PURPOSE (overrider),
02490        BINFO_TYPE (virtual_base));
02491 }
02492 
02493 /* Called from modify_all_vtables via dfs_walk.  */
02494 
02495 static tree
02496 dfs_modify_vtables (binfo, data)
02497      tree binfo;
02498      void *data;
02499 {
02500   if (/* There's no need to modify the vtable for a non-virtual
02501          primary base; we're not going to use that vtable anyhow.
02502    We do still need to do this for virtual primary bases, as they
02503    could become non-primary in a construction vtable.  */
02504       (!BINFO_PRIMARY_P (binfo) || TREE_VIA_VIRTUAL (binfo))
02505       /* Similarly, a base without a vtable needs no modification.  */
02506       && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
02507     {
02508       tree t;
02509       tree virtuals;
02510       tree old_virtuals;
02511 
02512       t = (tree) data;
02513 
02514       make_new_vtable (t, binfo);
02515       
02516       /* Now, go through each of the virtual functions in the virtual
02517    function table for BINFO.  Find the final overrider, and
02518    update the BINFO_VIRTUALS list appropriately.  */
02519       for (virtuals = BINFO_VIRTUALS (binfo),
02520        old_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo)));
02521      virtuals;
02522      virtuals = TREE_CHAIN (virtuals),
02523        old_virtuals = TREE_CHAIN (old_virtuals))
02524   update_vtable_entry_for_fn (t, 
02525             binfo, 
02526             BV_FN (old_virtuals),
02527             &virtuals);
02528     }
02529 
02530   SET_BINFO_MARKED (binfo);
02531 
02532   return NULL_TREE;
02533 }
02534 
02535 /* Update all of the primary and secondary vtables for T.  Create new
02536    vtables as required, and initialize their RTTI information.  Each
02537    of the functions in VIRTUALS is declared in T and may override a
02538    virtual function from a base class; find and modify the appropriate
02539    entries to point to the overriding functions.  Returns a list, in
02540    declaration order, of the virtual functions that are declared in T,
02541    but do not appear in the primary base class vtable, and which
02542    should therefore be appended to the end of the vtable for T.  */
02543 
02544 static tree
02545 modify_all_vtables (t, virtuals)
02546      tree t;
02547      tree virtuals;
02548 {
02549   tree binfo = TYPE_BINFO (t);
02550   tree *fnsp;
02551 
02552   /* Update all of the vtables.  */
02553   dfs_walk (binfo, 
02554       dfs_modify_vtables, 
02555       dfs_unmarked_real_bases_queue_p,
02556       t);
02557   dfs_walk (binfo, dfs_unmark, dfs_marked_real_bases_queue_p, t);
02558 
02559   /* Add virtual functions not already in our primary vtable. These
02560      will be both those introduced by this class, and those overridden
02561      from secondary bases.  It does not include virtuals merely
02562      inherited from secondary bases.  */
02563   for (fnsp = &virtuals; *fnsp; )
02564     {
02565       tree fn = TREE_VALUE (*fnsp);
02566 
02567       if (!value_member (fn, BINFO_VIRTUALS (binfo))
02568     || DECL_VINDEX (fn) == error_mark_node)
02569   {
02570     /* We don't need to adjust the `this' pointer when
02571        calling this function.  */
02572     BV_DELTA (*fnsp) = integer_zero_node;
02573     BV_VCALL_INDEX (*fnsp) = NULL_TREE;
02574 
02575     /* This is a function not already in our vtable.  Keep it.  */
02576     fnsp = &TREE_CHAIN (*fnsp);
02577   }
02578       else
02579   /* We've already got an entry for this function.  Skip it.  */
02580   *fnsp = TREE_CHAIN (*fnsp);
02581     }
02582 
02583   return virtuals;
02584 }
02585 
02586 /* Get the base virtual function declarations in T that have the
02587    indicated NAME.  */
02588 
02589 static tree
02590 get_basefndecls (name, t)
02591      tree name, t;
02592 {
02593   tree methods;
02594   tree base_fndecls = NULL_TREE;
02595   int n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
02596   int i;
02597 
02598   /* Find virtual functions in T with the indicated NAME.  */
02599   i = lookup_fnfields_1 (t, name);
02600   if (i != -1)
02601     for (methods = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), i);
02602    methods;
02603    methods = OVL_NEXT (methods))
02604       {
02605   tree method = OVL_CURRENT (methods);
02606 
02607   if (TREE_CODE (method) == FUNCTION_DECL
02608       && DECL_VINDEX (method))
02609     base_fndecls = tree_cons (NULL_TREE, method, base_fndecls);
02610       }
02611 
02612   if (base_fndecls)
02613     return base_fndecls;
02614 
02615   for (i = 0; i < n_baseclasses; i++)
02616     {
02617       tree basetype = TYPE_BINFO_BASETYPE (t, i);
02618       base_fndecls = chainon (get_basefndecls (name, basetype),
02619             base_fndecls);
02620     }
02621 
02622   return base_fndecls;
02623 }
02624 
02625 /* If this declaration supersedes the declaration of
02626    a method declared virtual in the base class, then
02627    mark this field as being virtual as well.  */
02628 
02629 static void
02630 check_for_override (decl, ctype)
02631      tree decl, ctype;
02632 {
02633   if (TREE_CODE (decl) == TEMPLATE_DECL)
02634     /* In [temp.mem] we have:
02635 
02636          A specialization of a member function template does not
02637          override a virtual function from a base class.  */
02638     return;
02639   if ((DECL_DESTRUCTOR_P (decl)
02640        || IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)))
02641       && look_for_overrides (ctype, decl)
02642       && !DECL_STATIC_FUNCTION_P (decl))
02643     /* Set DECL_VINDEX to a value that is neither an INTEGER_CST nor
02644        the error_mark_node so that we know it is an overriding
02645        function.  */
02646     DECL_VINDEX (decl) = decl;
02647 
02648   if (DECL_VIRTUAL_P (decl))
02649     {
02650       if (!DECL_VINDEX (decl))
02651   DECL_VINDEX (decl) = error_mark_node;
02652       IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = 1;
02653     }
02654 }
02655 
02656 /* Warn about hidden virtual functions that are not overridden in t.
02657    We know that constructors and destructors don't apply.  */
02658 
02659 void
02660 warn_hidden (t)
02661      tree t;
02662 {
02663   tree method_vec = CLASSTYPE_METHOD_VEC (t);
02664   int n_methods = method_vec ? TREE_VEC_LENGTH (method_vec) : 0;
02665   int i;
02666 
02667   /* We go through each separately named virtual function.  */
02668   for (i = 2; i < n_methods && TREE_VEC_ELT (method_vec, i); ++i)
02669     {
02670       tree fns;
02671       tree name;
02672       tree fndecl;
02673       tree base_fndecls;
02674       int j;
02675 
02676       /* All functions in this slot in the CLASSTYPE_METHOD_VEC will
02677    have the same name.  Figure out what name that is.  */
02678       name = DECL_NAME (OVL_CURRENT (TREE_VEC_ELT (method_vec, i)));
02679       /* There are no possibly hidden functions yet.  */
02680       base_fndecls = NULL_TREE;
02681       /* Iterate through all of the base classes looking for possibly
02682    hidden functions.  */
02683       for (j = 0; j < CLASSTYPE_N_BASECLASSES (t); j++)
02684   {
02685     tree basetype = TYPE_BINFO_BASETYPE (t, j);
02686     base_fndecls = chainon (get_basefndecls (name, basetype),
02687           base_fndecls);
02688   }
02689 
02690       /* If there are no functions to hide, continue.  */
02691       if (!base_fndecls)
02692   continue;
02693 
02694       /* Remove any overridden functions.  */
02695       for (fns = TREE_VEC_ELT (method_vec, i); fns; fns = OVL_NEXT (fns))
02696   {
02697     fndecl = OVL_CURRENT (fns);
02698     if (DECL_VINDEX (fndecl))
02699       {
02700         tree *prev = &base_fndecls;
02701         
02702         while (*prev) 
02703     /* If the method from the base class has the same
02704        signature as the method from the derived class, it
02705        has been overridden.  */
02706     if (same_signature_p (fndecl, TREE_VALUE (*prev)))
02707       *prev = TREE_CHAIN (*prev);
02708     else
02709       prev = &TREE_CHAIN (*prev);
02710       }
02711   }
02712 
02713       /* Now give a warning for all base functions without overriders,
02714    as they are hidden.  */
02715       while (base_fndecls) 
02716   {
02717     /* Here we know it is a hider, and no overrider exists.  */
02718     cp_warning_at ("`%D' was hidden", TREE_VALUE (base_fndecls));
02719     cp_warning_at ("  by `%D'", 
02720        OVL_CURRENT (TREE_VEC_ELT (method_vec, i)));
02721     base_fndecls = TREE_CHAIN (base_fndecls);
02722   }
02723     }
02724 }
02725 
02726 /* Check for things that are invalid.  There are probably plenty of other
02727    things we should check for also.  */
02728 
02729 static void
02730 finish_struct_anon (t)
02731      tree t;
02732 {
02733   tree field;
02734 
02735   for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
02736     {
02737       if (TREE_STATIC (field))
02738   continue;
02739       if (TREE_CODE (field) != FIELD_DECL)
02740   continue;
02741 
02742       if (DECL_NAME (field) == NULL_TREE
02743     && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
02744   {
02745     tree elt = TYPE_FIELDS (TREE_TYPE (field));
02746     for (; elt; elt = TREE_CHAIN (elt))
02747       {
02748         /* We're generally only interested in entities the user
02749      declared, but we also find nested classes by noticing
02750      the TYPE_DECL that we create implicitly.  You're
02751      allowed to put one anonymous union inside another,
02752      though, so we explicitly tolerate that.  We use
02753      TYPE_ANONYMOUS_P rather than ANON_AGGR_TYPE_P so that
02754      we also allow unnamed types used for defining fields.  */
02755         if (DECL_ARTIFICIAL (elt) 
02756       && (!DECL_IMPLICIT_TYPEDEF_P (elt)
02757           || TYPE_ANONYMOUS_P (TREE_TYPE (elt))))
02758     continue;
02759 
02760         if (DECL_NAME (elt) == constructor_name (t))
02761     cp_pedwarn_at ("ISO C++ forbids member `%D' with same name as enclosing class",
02762              elt);
02763 
02764         if (TREE_CODE (elt) != FIELD_DECL)
02765     {
02766       cp_pedwarn_at ("`%#D' invalid; an anonymous union can only have non-static data members",
02767          elt);
02768       continue;
02769     }
02770 
02771         if (TREE_PRIVATE (elt))
02772     cp_pedwarn_at ("private member `%#D' in anonymous union",
02773              elt);
02774         else if (TREE_PROTECTED (elt))
02775     cp_pedwarn_at ("protected member `%#D' in anonymous union",
02776              elt);
02777 
02778         TREE_PRIVATE (elt) = TREE_PRIVATE (field);
02779         TREE_PROTECTED (elt) = TREE_PROTECTED (field);
02780       }
02781   }
02782     }
02783 }
02784 
02785 /* Add T to CLASSTYPE_DECL_LIST of current_class_type which
02786    will be used later during class template instantiation.
02787    When FRIEND_P is zero, T can be a static member data (VAR_DECL),
02788    a non-static member data (FIELD_DECL), a member function
02789    (FUNCTION_DECL), a nested type (RECORD_TYPE, ENUM_TYPE), 
02790    a typedef (TYPE_DECL) or a member class template (TEMPLATE_DECL)
02791    When FRIEND_P is nonzero, T is either a friend class
02792    (RECORD_TYPE, TEMPLATE_DECL) or a friend function
02793    (FUNCTION_DECL, TEMPLATE_DECL).  */
02794 
02795 void
02796 maybe_add_class_template_decl_list (type, t, friend_p)
02797      tree type;
02798      tree t;
02799      int friend_p;
02800 {
02801   /* Save some memory by not creating TREE_LIST if TYPE is not template.  */
02802   if (CLASSTYPE_TEMPLATE_INFO (type))
02803     CLASSTYPE_DECL_LIST (type)
02804       = tree_cons (friend_p ? NULL_TREE : type,
02805        t, CLASSTYPE_DECL_LIST (type));
02806 }
02807 
02808 /* Create default constructors, assignment operators, and so forth for
02809    the type indicated by T, if they are needed.
02810    CANT_HAVE_DEFAULT_CTOR, CANT_HAVE_CONST_CTOR, and
02811    CANT_HAVE_CONST_ASSIGNMENT are nonzero if, for whatever reason, the
02812    class cannot have a default constructor, copy constructor taking a
02813    const reference argument, or an assignment operator taking a const
02814    reference, respectively.  If a virtual destructor is created, its
02815    DECL is returned; otherwise the return value is NULL_TREE.  */
02816 
02817 static void
02818 add_implicitly_declared_members (t, cant_have_default_ctor,
02819          cant_have_const_cctor,
02820          cant_have_const_assignment)
02821      tree t;
02822      int cant_have_default_ctor;
02823      int cant_have_const_cctor;
02824      int cant_have_const_assignment;
02825 {
02826   tree default_fn;
02827   tree implicit_fns = NULL_TREE;
02828   tree virtual_dtor = NULL_TREE;
02829   tree *f;
02830 
02831   ++adding_implicit_members;
02832 
02833   /* Destructor.  */
02834   if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) && !TYPE_HAS_DESTRUCTOR (t))
02835     {
02836       default_fn = implicitly_declare_fn (sfk_destructor, t, /*const_p=*/0);
02837       check_for_override (default_fn, t);
02838 
02839       /* If we couldn't make it work, then pretend we didn't need it.  */
02840       if (default_fn == void_type_node)
02841   TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = 0;
02842       else
02843   {
02844     TREE_CHAIN (default_fn) = implicit_fns;
02845     implicit_fns = default_fn;
02846 
02847     if (DECL_VINDEX (default_fn))
02848       virtual_dtor = default_fn;
02849   }
02850     }
02851   else
02852     /* Any non-implicit destructor is non-trivial.  */
02853     TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) |= TYPE_HAS_DESTRUCTOR (t);
02854 
02855   /* Default constructor.  */
02856   if (! TYPE_HAS_CONSTRUCTOR (t) && ! cant_have_default_ctor)
02857     {
02858       default_fn = implicitly_declare_fn (sfk_constructor, t, /*const_p=*/0);
02859       TREE_CHAIN (default_fn) = implicit_fns;
02860       implicit_fns = default_fn;
02861     }
02862 
02863   /* Copy constructor.  */
02864   if (! TYPE_HAS_INIT_REF (t) && ! TYPE_FOR_JAVA (t))
02865     {
02866       /* ARM 12.18: You get either X(X&) or X(const X&), but
02867    not both.  --Chip  */
02868       default_fn 
02869   = implicitly_declare_fn (sfk_copy_constructor, t,
02870          /*const_p=*/!cant_have_const_cctor);
02871       TREE_CHAIN (default_fn) = implicit_fns;
02872       implicit_fns = default_fn;
02873 #ifdef KEY
02874       TYPE_HAS_IMPLICIT_COPY_CONSTRUCTOR (t) = 1;
02875 #endif
02876     }
02877 
02878   /* Assignment operator.  */
02879   if (! TYPE_HAS_ASSIGN_REF (t) && ! TYPE_FOR_JAVA (t))
02880     {
02881       default_fn 
02882   = implicitly_declare_fn (sfk_assignment_operator, t,
02883          /*const_p=*/!cant_have_const_assignment);
02884       TREE_CHAIN (default_fn) = implicit_fns;
02885       implicit_fns = default_fn;
02886     }
02887 
02888   /* Now, hook all of the new functions on to TYPE_METHODS,
02889      and add them to the CLASSTYPE_METHOD_VEC.  */
02890   for (f = &implicit_fns; *f; f = &TREE_CHAIN (*f))
02891     {
02892       add_method (t, *f, /*error_p=*/0);
02893       maybe_add_class_template_decl_list (current_class_type, *f, /*friend_p=*/0);
02894     }
02895   if (abi_version_at_least (2))
02896     /* G++ 3.2 put the implicit destructor at the *beginning* of the
02897        list, which cause the destructor to be emitted in an incorrect
02898        location in the vtable.  */
02899     TYPE_METHODS (t) = chainon (TYPE_METHODS (t), implicit_fns);
02900   else
02901     {
02902       if (warn_abi && virtual_dtor)
02903   warning ("vtable layout for class `%T' may not be ABI-compliant "
02904      "and may change in a future version of GCC due to implicit "
02905      "virtual destructor",
02906      t);
02907       *f = TYPE_METHODS (t);
02908       TYPE_METHODS (t) = implicit_fns;
02909     }
02910 
02911   --adding_implicit_members;
02912 }
02913 
02914 /* Subroutine of finish_struct_1.  Recursively count the number of fields
02915    in TYPE, including anonymous union members.  */
02916 
02917 static int
02918 count_fields (fields)
02919      tree fields;
02920 {
02921   tree x;
02922   int n_fields = 0;
02923   for (x = fields; x; x = TREE_CHAIN (x))
02924     {
02925       if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
02926   n_fields += count_fields (TYPE_FIELDS (TREE_TYPE (x)));
02927       else
02928   n_fields += 1;
02929     }
02930   return n_fields;
02931 }
02932 
02933 /* Subroutine of finish_struct_1.  Recursively add all the fields in the
02934    TREE_LIST FIELDS to the TREE_VEC FIELD_VEC, starting at offset IDX.  */
02935 
02936 static int
02937 add_fields_to_vec (fields, field_vec, idx)
02938      tree fields, field_vec;
02939      int idx;
02940 {
02941   tree x;
02942   for (x = fields; x; x = TREE_CHAIN (x))
02943     {
02944       if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
02945   idx = add_fields_to_vec (TYPE_FIELDS (TREE_TYPE (x)), field_vec, idx);
02946       else
02947   TREE_VEC_ELT (field_vec, idx++) = x;
02948     }
02949   return idx;
02950 }
02951 
02952 /* FIELD is a bit-field.  We are finishing the processing for its
02953    enclosing type.  Issue any appropriate messages and set appropriate
02954    flags.  */
02955 
02956 static void
02957 check_bitfield_decl (field)
02958      tree field;
02959 {
02960   tree type = TREE_TYPE (field);
02961   tree w = NULL_TREE;
02962 
02963   /* Detect invalid bit-field type.  */
02964   if (DECL_INITIAL (field)
02965       && ! INTEGRAL_TYPE_P (TREE_TYPE (field)))
02966     {
02967       cp_error_at ("bit-field `%#D' with non-integral type", field);
02968       w = error_mark_node;
02969     }
02970 
02971   /* Detect and ignore out of range field width.  */
02972   if (DECL_INITIAL (field))
02973     {
02974       w = DECL_INITIAL (field);
02975 
02976       /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs.  */
02977       STRIP_NOPS (w);
02978 
02979       /* detect invalid field size.  */
02980       if (TREE_CODE (w) == CONST_DECL)
02981   w = DECL_INITIAL (w);
02982       else
02983   w = decl_constant_value (w);
02984 
02985       if (TREE_CODE (w) != INTEGER_CST)
02986   {
02987     cp_error_at ("bit-field `%D' width not an integer constant",
02988            field);
02989     w = error_mark_node;
02990   }
02991       else if (tree_int_cst_sgn (w) < 0)
02992   {
02993     cp_error_at ("negative width in bit-field `%D'", field);
02994     w = error_mark_node;
02995   }
02996       else if (integer_zerop (w) && DECL_NAME (field) != 0)
02997   {
02998     cp_error_at ("zero width for bit-field `%D'", field);
02999     w = error_mark_node;
03000   }
03001       else if (compare_tree_int (w, TYPE_PRECISION (type)) > 0
03002          && TREE_CODE (type) != ENUMERAL_TYPE
03003          && TREE_CODE (type) != BOOLEAN_TYPE)
03004   cp_warning_at ("width of `%D' exceeds its type", field);
03005       else if (TREE_CODE (type) == ENUMERAL_TYPE
03006          && (0 > compare_tree_int (w,
03007            min_precision (TYPE_MIN_VALUE (type),
03008               TREE_UNSIGNED (type)))
03009        ||  0 > compare_tree_int (w,
03010                min_precision
03011                (TYPE_MAX_VALUE (type),
03012                 TREE_UNSIGNED (type)))))
03013   cp_warning_at ("`%D' is too small to hold all values of `%#T'",
03014            field, type);
03015     }
03016   
03017   /* Remove the bit-field width indicator so that the rest of the
03018      compiler does not treat that value as an initializer.  */
03019   DECL_INITIAL (field) = NULL_TREE;
03020 
03021   if (w != error_mark_node)
03022     {
03023       DECL_SIZE (field) = convert (bitsizetype, w);
03024       DECL_BIT_FIELD (field) = 1;
03025 
03026       if (integer_zerop (w)
03027     && ! (* targetm.ms_bitfield_layout_p) (DECL_FIELD_CONTEXT (field)))
03028   {
03029 #ifdef EMPTY_FIELD_BOUNDARY
03030     DECL_ALIGN (field) = MAX (DECL_ALIGN (field), 
03031             EMPTY_FIELD_BOUNDARY);
03032 #endif
03033 #ifdef PCC_BITFIELD_TYPE_MATTERS
03034     if (PCC_BITFIELD_TYPE_MATTERS)
03035       {
03036         DECL_ALIGN (field) = MAX (DECL_ALIGN (field), 
03037           TYPE_ALIGN (type));
03038         DECL_USER_ALIGN (field) |= TYPE_USER_ALIGN (type);
03039       }
03040 #endif
03041   }
03042     }
03043   else
03044     {
03045       /* Non-bit-fields are aligned for their type.  */
03046       DECL_BIT_FIELD (field) = 0;
03047       CLEAR_DECL_C_BIT_FIELD (field);
03048       DECL_ALIGN (field) = MAX (DECL_ALIGN (field), TYPE_ALIGN (type));
03049       DECL_USER_ALIGN (field) |= TYPE_USER_ALIGN (type);
03050     }
03051 }
03052 
03053 /* FIELD is a non bit-field.  We are finishing the processing for its
03054    enclosing type T.  Issue any appropriate messages and set appropriate
03055    flags.  */
03056 
03057 static void
03058 check_field_decl (field, t, cant_have_const_ctor,
03059       cant_have_default_ctor, no_const_asn_ref,
03060       any_default_members)
03061      tree field;
03062      tree t;
03063      int *cant_have_const_ctor;
03064      int *cant_have_default_ctor;
03065      int *no_const_asn_ref;
03066      int *any_default_members;
03067 {
03068   tree type = strip_array_types (TREE_TYPE (field));
03069 
03070   /* An anonymous union cannot contain any fields which would change
03071      the settings of CANT_HAVE_CONST_CTOR and friends.  */
03072   if (ANON_UNION_TYPE_P (type))
03073     ;
03074   /* And, we don't set TYPE_HAS_CONST_INIT_REF, etc., for anonymous
03075      structs.  So, we recurse through their fields here.  */
03076   else if (ANON_AGGR_TYPE_P (type))
03077     {
03078       tree fields;
03079 
03080       for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields))
03081   if (TREE_CODE (fields) == FIELD_DECL && !DECL_C_BIT_FIELD (field))
03082     check_field_decl (fields, t, cant_have_const_ctor,
03083           cant_have_default_ctor, no_const_asn_ref,
03084           any_default_members);
03085     }
03086   /* Check members with class type for constructors, destructors,
03087      etc.  */
03088   else if (CLASS_TYPE_P (type))
03089     {
03090       /* Never let anything with uninheritable virtuals
03091    make it through without complaint.  */
03092       abstract_virtuals_error (field, type);
03093           
03094       if (TREE_CODE (t) == UNION_TYPE)
03095   {
03096     if (TYPE_NEEDS_CONSTRUCTING (type))
03097       cp_error_at ("member `%#D' with constructor not allowed in union",
03098        field);
03099     if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
03100       cp_error_at ("member `%#D' with destructor not allowed in union",
03101        field);
03102     if (TYPE_HAS_COMPLEX_ASSIGN_REF (type))
03103       cp_error_at ("member `%#D' with copy assignment operator not allowed in union",
03104        field);
03105   }
03106       else
03107   {
03108     TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type);
03109     TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) 
03110       |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type);
03111     TYPE_HAS_COMPLEX_ASSIGN_REF (t) |= TYPE_HAS_COMPLEX_ASSIGN_REF (type);
03112     TYPE_HAS_COMPLEX_INIT_REF (t) |= TYPE_HAS_COMPLEX_INIT_REF (type);
03113   }
03114 
03115       if (!TYPE_HAS_CONST_INIT_REF (type))
03116   *cant_have_const_ctor = 1;
03117 
03118       if (!TYPE_HAS_CONST_ASSIGN_REF (type))
03119   *no_const_asn_ref = 1;
03120 
03121       if (TYPE_HAS_CONSTRUCTOR (type)
03122     && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
03123   *cant_have_default_ctor = 1;
03124     }
03125   if (DECL_INITIAL (field) != NULL_TREE)
03126     {
03127       /* `build_class_init_list' does not recognize
03128    non-FIELD_DECLs.  */
03129       if (TREE_CODE (t) == UNION_TYPE && any_default_members != 0)
03130   cp_error_at ("multiple fields in union `%T' initialized");
03131       *any_default_members = 1;
03132     }
03133 }
03134 
03135 /* Check the data members (both static and non-static), class-scoped
03136    typedefs, etc., appearing in the declaration of T.  Issue
03137    appropriate diagnostics.  Sets ACCESS_DECLS to a list (in
03138    declaration order) of access declarations; each TREE_VALUE in this
03139    list is a USING_DECL.
03140 
03141    In addition, set the following flags:
03142 
03143      EMPTY_P
03144        The class is empty, i.e., contains no non-static data members.
03145 
03146      CANT_HAVE_DEFAULT_CTOR_P
03147        This class cannot have an implicitly generated default
03148        constructor.
03149 
03150      CANT_HAVE_CONST_CTOR_P
03151        This class cannot have an implicitly generated copy constructor
03152        taking a const reference.
03153 
03154      CANT_HAVE_CONST_ASN_REF
03155        This class cannot have an implicitly generated assignment
03156        operator taking a const reference.
03157 
03158    All of these flags should be initialized before calling this
03159    function.
03160 
03161    Returns a pointer to the end of the TYPE_FIELDs chain; additional
03162    fields can be added by adding to this chain.  */
03163 
03164 static void
03165 check_field_decls (tree t, tree *access_decls,
03166        int *cant_have_default_ctor_p, 
03167        int *cant_have_const_ctor_p,
03168        int *no_const_asn_ref_p)
03169 {
03170   tree *field;
03171   tree *next;
03172   int has_pointers;
03173   int any_default_members;
03174 
03175   /* First, delete any duplicate fields.  */
03176   delete_duplicate_fields (TYPE_FIELDS (t));
03177 
03178   /* Assume there are no access declarations.  */
03179   *access_decls = NULL_TREE;
03180   /* Assume this class has no pointer members.  */
03181   has_pointers = 0;
03182   /* Assume none of the members of this class have default
03183      initializations.  */
03184   any_default_members = 0;
03185 
03186   for (field = &TYPE_FIELDS (t); *field; field = next)
03187     {
03188       tree x = *field;
03189       tree type = TREE_TYPE (x);
03190 
03191       next = &TREE_CHAIN (x);
03192 
03193       if (TREE_CODE (x) == FIELD_DECL)
03194   {
03195     DECL_PACKED (x) |= TYPE_PACKED (t);
03196 
03197     if (DECL_C_BIT_FIELD (x) && integer_zerop (DECL_INITIAL (x)))
03198       /* We don't treat zero-width bitfields as making a class
03199          non-empty.  */
03200       ;
03201     else
03202       {
03203         tree element_type;
03204 
03205         /* The class is non-empty.  */
03206         CLASSTYPE_EMPTY_P (t) = 0;
03207         /* The class is not even nearly empty.  */
03208         CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
03209         /* If one of the data members contains an empty class,
03210      so does T.  */
03211         element_type = strip_array_types (type);
03212         if (CLASS_TYPE_P (element_type) 
03213       && CLASSTYPE_CONTAINS_EMPTY_CLASS_P (element_type))
03214     CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
03215       }
03216   }
03217 
03218       if (TREE_CODE (x) == USING_DECL)
03219   {
03220     /* Prune the access declaration from the list of fields.  */
03221     *field = TREE_CHAIN (x);
03222 
03223     /* Save the access declarations for our caller.  */
03224     *access_decls = tree_cons (NULL_TREE, x, *access_decls);
03225 
03226     /* Since we've reset *FIELD there's no reason to skip to the
03227        next field.  */
03228     next = field;
03229     continue;
03230   }
03231 
03232       if (TREE_CODE (x) == TYPE_DECL
03233     || TREE_CODE (x) == TEMPLATE_DECL)
03234   continue;
03235 
03236       /* If we've gotten this far, it's a data member, possibly static,
03237    or an enumerator.  */
03238 
03239       DECL_CONTEXT (x) = t;
03240 
03241       /* ``A local class cannot have static data members.'' ARM 9.4 */
03242       if (current_function_decl && TREE_STATIC (x))
03243   cp_error_at ("field `%D' in local class cannot be static", x);
03244 
03245       /* Perform error checking that did not get done in
03246    grokdeclarator.  */
03247       if (TREE_CODE (type) == FUNCTION_TYPE)
03248   {
03249     cp_error_at ("field `%D' invalidly declared function type",
03250            x);
03251     type = build_pointer_type (type);
03252     TREE_TYPE (x) = type;
03253   }
03254       else if (TREE_CODE (type) == METHOD_TYPE)
03255   {
03256     cp_error_at ("field `%D' invalidly declared method type", x);
03257     type = build_pointer_type (type);
03258     TREE_TYPE (x) = type;
03259   }
03260       else if (TREE_CODE (type) == OFFSET_TYPE)
03261   {
03262     cp_error_at ("field `%D' invalidly declared offset type", x);
03263     type = build_pointer_type (type);
03264     TREE_TYPE (x) = type;
03265   }
03266 
03267       if (type == error_mark_node)
03268   continue;
03269     
03270       /* When this goes into scope, it will be a non-local reference.  */
03271       DECL_NONLOCAL (x) = 1;
03272 
03273       if (TREE_CODE (x) == CONST_DECL)
03274   continue;
03275 
03276       if (TREE_CODE (x) == VAR_DECL)
03277   {
03278     if (TREE_CODE (t) == UNION_TYPE)
03279       /* Unions cannot have static members.  */
03280       cp_error_at ("field `%D' declared static in union", x);
03281         
03282     continue;
03283   }
03284 
03285       /* Now it can only be a FIELD_DECL.  */
03286 
03287       if (TREE_PRIVATE (x) || TREE_PROTECTED (x))
03288   CLASSTYPE_NON_AGGREGATE (t) = 1;
03289 
03290       /* If this is of reference type, check if it needs an init.
03291    Also do a little ANSI jig if necessary.  */
03292       if (TREE_CODE (type) == REFERENCE_TYPE)
03293   {
03294     CLASSTYPE_NON_POD_P (t) = 1;
03295     if (DECL_INITIAL (x) == NULL_TREE)
03296       SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
03297 
03298     /* ARM $12.6.2: [A member initializer list] (or, for an
03299        aggregate, initialization by a brace-enclosed list) is the
03300        only way to initialize nonstatic const and reference
03301        members.  */
03302     *cant_have_default_ctor_p = 1;
03303     TYPE_HAS_COMPLEX_ASSIGN_REF (t) = 1;
03304 
03305     if (! TYPE_HAS_CONSTRUCTOR (t) && CLASSTYPE_NON_AGGREGATE (t)
03306         && extra_warnings)
03307             cp_warning_at ("non-static reference `%#D' in class without a constructor", x);
03308   }
03309 
03310       type = strip_array_types (type);
03311       
03312       if (TREE_CODE (type) == POINTER_TYPE)
03313   has_pointers = 1;
03314 
03315       if (DECL_MUTABLE_P (x) || TYPE_HAS_MUTABLE_P (type))
03316   CLASSTYPE_HAS_MUTABLE (t) = 1;
03317 
03318       if (! pod_type_p (type))
03319         /* DR 148 now allows pointers to members (which are POD themselves),
03320            to be allowed in POD structs.  */
03321   CLASSTYPE_NON_POD_P (t) = 1;
03322 
03323       if (! zero_init_p (type))
03324   CLASSTYPE_NON_ZERO_INIT_P (t) = 1;
03325 
03326       /* If any field is const, the structure type is pseudo-const.  */
03327       if (CP_TYPE_CONST_P (type))
03328   {
03329     C_TYPE_FIELDS_READONLY (t) = 1;
03330     if (DECL_INITIAL (x) == NULL_TREE)
03331       SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
03332 
03333     /* ARM $12.6.2: [A member initializer list] (or, for an
03334        aggregate, initialization by a brace-enclosed list) is the
03335        only way to initialize nonstatic const and reference
03336        members.  */
03337     *cant_have_default_ctor_p = 1;
03338     TYPE_HAS_COMPLEX_ASSIGN_REF (t) = 1;
03339 
03340     if (! TYPE_HAS_CONSTRUCTOR (t) && CLASSTYPE_NON_AGGREGATE (t)
03341         && extra_warnings)
03342             cp_warning_at ("non-static const member `%#D' in class without a constructor", x);
03343   }
03344       /* A field that is pseudo-const makes the structure likewise.  */
03345       else if (IS_AGGR_TYPE (type))
03346   {
03347     C_TYPE_FIELDS_READONLY (t) |= C_TYPE_FIELDS_READONLY (type);
03348     SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t,
03349       CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
03350       | CLASSTYPE_READONLY_FIELDS_NEED_INIT (type));
03351   }
03352 
03353       /* Core issue 80: A nonstatic data member is required to have a
03354    different name from the class iff the class has a
03355    user-defined constructor.  */
03356       if (DECL_NAME (x) == constructor_name (t)
03357     && TYPE_HAS_CONSTRUCTOR (t))
03358   cp_pedwarn_at ("field `%#D' with same name as class", x);
03359 
03360       /* We set DECL_C_BIT_FIELD in grokbitfield.
03361    If the type and width are valid, we'll also set DECL_BIT_FIELD.  */
03362       if (DECL_C_BIT_FIELD (x))
03363   check_bitfield_decl (x);
03364       else
03365   check_field_decl (x, t,
03366         cant_have_const_ctor_p,
03367         cant_have_default_ctor_p, 
03368         no_const_asn_ref_p,
03369         &any_default_members);
03370     }
03371 
03372   /* Effective C++ rule 11.  */
03373   if (has_pointers && warn_ecpp && TYPE_HAS_CONSTRUCTOR (t)
03374       && ! (TYPE_HAS_INIT_REF (t) && TYPE_HAS_ASSIGN_REF (t)))
03375     {
03376       warning ("`%#T' has pointer data members", t);
03377       
03378       if (! TYPE_HAS_INIT_REF (t))
03379   {
03380     warning ("  but does not override `%T(const %T&)'", t, t);
03381     if (! TYPE_HAS_ASSIGN_REF (t))
03382       warning ("  or `operator=(const %T&)'", t);
03383   }
03384       else if (! TYPE_HAS_ASSIGN_REF (t))
03385   warning ("  but does not override `operator=(const %T&)'", t);
03386     }
03387 
03388 
03389   /* Check anonymous struct/anonymous union fields.  */
03390   finish_struct_anon (t);
03391 
03392   /* We've built up the list of access declarations in reverse order.
03393      Fix that now.  */
03394   *access_decls = nreverse (*access_decls);
03395 }
03396 
03397 /* If TYPE is an empty class type, records its OFFSET in the table of
03398    OFFSETS.  */
03399 
03400 static int
03401 record_subobject_offset (type, offset, offsets)
03402      tree type;
03403      tree offset;
03404      splay_tree offsets;
03405 {
03406   splay_tree_node n;
03407 
03408   if (!is_empty_class (type))
03409     return 0;
03410 
03411   /* Record the location of this empty object in OFFSETS.  */
03412   n = splay_tree_lookup (offsets, (splay_tree_key) offset);
03413   if (!n)
03414     n = splay_tree_insert (offsets, 
03415          (splay_tree_key) offset,
03416          (splay_tree_value) NULL_TREE);
03417   n->value = ((splay_tree_value) 
03418         tree_cons (NULL_TREE,
03419        type,
03420        (tree) n->value));
03421 
03422   return 0;
03423 }
03424 
03425 /* Returns nonzero if TYPE is an empty class type and there is
03426    already an entry in OFFSETS for the same TYPE as the same OFFSET.  */
03427 
03428 static int
03429 check_subobject_offset (type, offset, offsets)
03430      tree type;
03431      tree offset;
03432      splay_tree offsets;
03433 {
03434   splay_tree_node n;
03435   tree t;
03436 
03437   if (!is_empty_class (type))
03438     return 0;
03439 
03440   /* Record the location of this empty object in OFFSETS.  */
03441   n = splay_tree_lookup (offsets, (splay_tree_key) offset);
03442   if (!n)
03443     return 0;
03444 
03445   for (t = (tree) n->value; t; t = TREE_CHAIN (t))
03446     if (same_type_p (TREE_VALUE (t), type))
03447       return 1;
03448 
03449   return 0;
03450 }
03451 
03452 /* Walk through all the subobjects of TYPE (located at OFFSET).  Call
03453    F for every subobject, passing it the type, offset, and table of
03454    OFFSETS.  If VBASES_P is one, then virtual non-primary bases should
03455    be traversed.
03456 
03457    If MAX_OFFSET is non-NULL, then subobjects with an offset greater
03458    than MAX_OFFSET will not be walked.
03459 
03460    If F returns a nonzero value, the traversal ceases, and that value
03461    is returned.  Otherwise, returns zero.  */
03462 
03463 static int
03464 walk_subobject_offsets (type, f, offset, offsets, max_offset, vbases_p)
03465      tree type;
03466      subobject_offset_fn f;
03467      tree offset;
03468      splay_tree offsets;
03469      tree max_offset;
03470      int vbases_p;
03471 {
03472   int r = 0;
03473   tree type_binfo = NULL_TREE;
03474 
03475   /* If this OFFSET is bigger than the MAX_OFFSET, then we should
03476      stop.  */
03477   if (max_offset && INT_CST_LT (max_offset, offset))
03478     return 0;
03479 
03480   if (!TYPE_P (type)) 
03481     {
03482       if (abi_version_at_least (2))
03483   type_binfo = type;
03484       type = BINFO_TYPE (type);
03485     }
03486 
03487   if (CLASS_TYPE_P (type))
03488     {
03489       tree field;
03490       tree binfo;
03491       int i;
03492 
03493       /* Avoid recursing into objects that are not interesting.  */
03494       if (!CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
03495   return 0;
03496 
03497       /* Record the location of TYPE.  */
03498       r = (*f) (type, offset, offsets);
03499       if (r)
03500   return r;
03501 
03502       /* Iterate through the direct base classes of TYPE.  */
03503       if (!type_binfo)
03504   type_binfo = TYPE_BINFO (type);
03505       for (i = 0; i < BINFO_N_BASETYPES (type_binfo); ++i)
03506   {
03507     tree binfo_offset;
03508 
03509     binfo = BINFO_BASETYPE (type_binfo, i);
03510 
03511     if (abi_version_at_least (2) 
03512         && TREE_VIA_VIRTUAL (binfo))
03513       continue;
03514 
03515     if (!vbases_p 
03516         && TREE_VIA_VIRTUAL (binfo) 
03517         && !BINFO_PRIMARY_P (binfo))
03518       continue;
03519 
03520     if (!abi_version_at_least (2))
03521       binfo_offset = size_binop (PLUS_EXPR,
03522                offset,
03523                BINFO_OFFSET (binfo));
03524     else
03525       {
03526         tree orig_binfo;
03527         /* We cannot rely on BINFO_OFFSET being set for the base
03528      class yet, but the offsets for direct non-virtual
03529      bases can be calculated by going back to the TYPE.  */
03530         orig_binfo = BINFO_BASETYPE (TYPE_BINFO (type), i);
03531         binfo_offset = size_binop (PLUS_EXPR,       
03532            offset,
03533            BINFO_OFFSET (orig_binfo));
03534       }
03535 
03536     r = walk_subobject_offsets (binfo,
03537               f,
03538               binfo_offset,
03539               offsets,
03540               max_offset,
03541               (abi_version_at_least (2) 
03542                ? /*vbases_p=*/0 : vbases_p));
03543     if (r)
03544       return r;
03545   }
03546 
03547       if (abi_version_at_least (2))
03548   {
03549     tree vbase;
03550 
03551     /* Iterate through the virtual base classes of TYPE.  In G++
03552        3.2, we included virtual bases in the direct base class
03553        loop above, which results in incorrect results; the
03554        correct offsets for virtual bases are only known when
03555        working with the most derived type.  */
03556     if (vbases_p)
03557       for (vbase = CLASSTYPE_VBASECLASSES (type);
03558      vbase;
03559      vbase = TREE_CHAIN (vbase))
03560         {
03561     binfo = TREE_VALUE (vbase);
03562     r = walk_subobject_offsets (binfo,
03563               f,
03564               size_binop (PLUS_EXPR,
03565               offset,
03566               BINFO_OFFSET (binfo)),
03567               offsets,
03568               max_offset,
03569               /*vbases_p=*/0);
03570     if (r)
03571       return r;
03572         }
03573     else
03574       {
03575         /* We still have to walk the primary base, if it is
03576      virtual.  (If it is non-virtual, then it was walked
03577      above.)  */
03578         vbase = get_primary_binfo (type_binfo);
03579         if (vbase && TREE_VIA_VIRTUAL (vbase))
03580     {
03581       tree derived = type_binfo;
03582       while (BINFO_INHERITANCE_CHAIN (derived))
03583         derived = BINFO_INHERITANCE_CHAIN (derived);
03584       derived = TREE_TYPE (derived);
03585       vbase = binfo_for_vbase (TREE_TYPE (vbase), derived);
03586 
03587       if (BINFO_PRIMARY_BASE_OF (vbase) == type_binfo)
03588         {
03589           r = (walk_subobject_offsets 
03590          (vbase, f, offset,
03591           offsets, max_offset, /*vbases_p=*/0));
03592           if (r)
03593       return r;
03594         }
03595     }
03596       }
03597   }
03598 
03599       /* Iterate through the fields of TYPE.  */
03600       for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
03601   if (TREE_CODE (field) == FIELD_DECL && !DECL_ARTIFICIAL (field))
03602     {
03603       tree field_offset;
03604 
03605       if (abi_version_at_least (2))
03606         field_offset = byte_position (field);
03607       else
03608         /* In G++ 3.2, DECL_FIELD_OFFSET was used.  */
03609         field_offset = DECL_FIELD_OFFSET (field);
03610 
03611       r = walk_subobject_offsets (TREE_TYPE (field),
03612           f,
03613           size_binop (PLUS_EXPR,
03614                 offset,
03615                 field_offset),
03616           offsets,
03617           max_offset,
03618           /*vbases_p=*/1);
03619       if (r)
03620         return r;
03621     }
03622     }
03623   else if (TREE_CODE (type) == ARRAY_TYPE)
03624     {
03625       tree element_type = strip_array_types (type);
03626       tree domain = TYPE_DOMAIN (type);
03627       tree index;
03628 
03629       /* Avoid recursing into objects that are not interesting.  */
03630       if (!CLASS_TYPE_P (element_type)
03631     || !CLASSTYPE_CONTAINS_EMPTY_CLASS_P (element_type))
03632   return 0;
03633 
03634       /* Step through each of the elements in the array.  */
03635       for (index = size_zero_node;
03636      /* G++ 3.2 had an off-by-one error here.  */
03637      (abi_version_at_least (2) 
03638       ? !INT_CST_LT (TYPE_MAX_VALUE (domain), index)
03639       : INT_CST_LT (index, TYPE_MAX_VALUE (domain)));
03640      index = size_binop (PLUS_EXPR, index, size_one_node))
03641   {
03642     r = walk_subobject_offsets (TREE_TYPE (type),
03643               f,
03644               offset,
03645               offsets,
03646               max_offset,
03647               /*vbases_p=*/1);
03648     if (r)
03649       return r;
03650     offset = size_binop (PLUS_EXPR, offset, 
03651              TYPE_SIZE_UNIT (TREE_TYPE (type)));
03652     /* If this new OFFSET is bigger than the MAX_OFFSET, then
03653        there's no point in iterating through the remaining
03654        elements of the array.  */
03655     if (max_offset && INT_CST_LT (max_offset, offset))
03656       break;
03657   }
03658     }
03659 
03660   return 0;
03661 }
03662 
03663 /* Record all of the empty subobjects of TYPE (located at OFFSET) in
03664    OFFSETS.  If VBASES_P is nonzero, virtual bases of TYPE are
03665    examined.  */
03666 
03667 static void
03668 record_subobject_offsets (type, offset, offsets, vbases_p)
03669      tree type;
03670      tree offset;
03671      splay_tree offsets;
03672      int vbases_p;
03673 {
03674   walk_subobject_offsets (type, record_subobject_offset, offset,
03675         offsets, /*max_offset=*/NULL_TREE, vbases_p);
03676 }
03677 
03678 /* Returns nonzero if any of the empty subobjects of TYPE (located at
03679    OFFSET) conflict with entries in OFFSETS.  If VBASES_P is nonzero,
03680    virtual bases of TYPE are examined.  */
03681 
03682 static int
03683 layout_conflict_p (type, offset, offsets, vbases_p)
03684      tree type;
03685      tree offset;
03686      splay_tree offsets;
03687      int vbases_p;
03688 {
03689   splay_tree_node max_node;
03690 
03691   /* Get the node in OFFSETS that indicates the maximum offset where
03692      an empty subobject is located.  */
03693   max_node = splay_tree_max (offsets);
03694   /* If there aren't any empty subobjects, then there's no point in
03695      performing this check.  */
03696   if (!max_node)
03697     return 0;
03698 
03699   return walk_subobject_offsets (type, check_subobject_offset, offset,
03700          offsets, (tree) (max_node->key),
03701          vbases_p);
03702 }
03703 
03704 /* DECL is a FIELD_DECL corresponding either to a base subobject of a
03705    non-static data member of the type indicated by RLI.  BINFO is the
03706    binfo corresponding to the base subobject, OFFSETS maps offsets to
03707    types already located at those offsets.  This function determines
03708    the position of the DECL.  */
03709 
03710 static void
03711 layout_nonempty_base_or_field (record_layout_info rli, 
03712              tree decl, 
03713              tree binfo, 
03714              splay_tree offsets)
03715 {
03716   tree t = rli->t;
03717   tree offset = NULL_TREE;
03718   bool field_p;
03719   tree type;
03720   
03721   if (binfo)
03722     {
03723       /* For the purposes of determining layout conflicts, we want to
03724    use the class type of BINFO; TREE_TYPE (DECL) will be the
03725    CLASSTYPE_AS_BASE version, which does not contain entries for
03726    zero-sized bases.  */
03727       type = TREE_TYPE (binfo);
03728       field_p = false;
03729     }
03730   else
03731     {
03732       type = TREE_TYPE (decl);
03733       field_p = true;
03734     }
03735 
03736   /* Try to place the field.  It may take more than one try if we have
03737      a hard time placing the field without putting two objects of the
03738      same type at the same address.  */
03739   while (1)
03740     {
03741       struct record_layout_info_s old_rli = *rli;
03742 
03743       /* Place this field.  */
03744       place_field (rli, decl);
03745       offset = byte_position (decl);
03746  
03747       /* We have to check to see whether or not there is already
03748    something of the same type at the offset we're about to use.
03749    For example:
03750    
03751    struct S {};
03752    struct T : public S { int i; };
03753    struct U : public S, public T {};
03754    
03755    Here, we put S at offset zero in U.  Then, we can't put T at
03756    offset zero -- its S component would be at the same address
03757    as the S we already allocated.  So, we have to skip ahead.
03758    Since all data members, including those whose type is an
03759    empty class, have nonzero size, any overlap can happen only
03760    with a direct or indirect base-class -- it can't happen with
03761    a data member.  */
03762       /* G++ 3.2 did not check for overlaps when placing a non-empty
03763    virtual base.  */
03764       if (!abi_version_at_least (2) && binfo && TREE_VIA_VIRTUAL (binfo))
03765   break;
03766       if (layout_conflict_p (field_p ? type : binfo, offset, 
03767            offsets, field_p))
03768   {
03769     /* Strip off the size allocated to this field.  That puts us
03770        at the first place we could have put the field with
03771        proper alignment.  */
03772     *rli = old_rli;
03773 
03774     /* Bump up by the alignment required for the type.  */
03775     rli->bitpos
03776       = size_binop (PLUS_EXPR, rli->bitpos, 
03777         bitsize_int (binfo 
03778                ? CLASSTYPE_ALIGN (type)
03779                : TYPE_ALIGN (type)));
03780     normalize_rli (rli);
03781   }
03782       else
03783   /* There was no conflict.  We're done laying out this field.  */
03784   break;
03785     }
03786 
03787   /* Now that we know where it will be placed, update its
03788      BINFO_OFFSET.  */
03789   if (binfo && CLASS_TYPE_P (BINFO_TYPE (binfo)))
03790     /* Indirect virtual bases may have a nonzero BINFO_OFFSET at
03791        this point because their BINFO_OFFSET is copied from another
03792        hierarchy.  Therefore, we may not need to add the entire
03793        OFFSET.  */
03794     propagate_binfo_offsets (binfo, 
03795            size_diffop (convert (ssizetype, offset),
03796             convert (ssizetype, 
03797                BINFO_OFFSET (binfo))),
03798            t);
03799 }
03800 
03801 /* Returns true if TYPE is empty and OFFSET is nonzero.  */
03802 
03803 static int
03804 empty_base_at_nonzero_offset_p (tree type,
03805         tree offset,
03806         splay_tree offsets ATTRIBUTE_UNUSED)
03807 {
03808   return is_empty_class (type) && !integer_zerop (offset);
03809 }
03810 
03811 /* Layout the empty base BINFO.  EOC indicates the byte currently just
03812    past the end of the class, and should be correctly aligned for a
03813    class of the type indicated by BINFO; OFFSETS gives the offsets of
03814    the empty bases allocated so far. T is the most derived
03815    type.  Return nonzero iff we added it at the end.  */
03816 
03817 static bool
03818 layout_empty_base (binfo, eoc, offsets, t)
03819      tree binfo;
03820      tree eoc;
03821      splay_tree offsets;
03822      tree t;
03823 {
03824   tree alignment;
03825   tree basetype = BINFO_TYPE (binfo);
03826   bool atend = false;
03827 
03828   /* This routine should only be used for empty classes.  */
03829   my_friendly_assert (is_empty_class (basetype), 20000321);
03830   alignment = ssize_int (CLASSTYPE_ALIGN_UNIT (basetype));
03831   
03832   if (abi_version_at_least (2))
03833     BINFO_OFFSET (binfo) = size_zero_node;
03834   if (warn_abi && !integer_zerop (BINFO_OFFSET (binfo)))
03835     warning ("offset of empty base `%T' may not be ABI-compliant and may"
03836        "change in a future version of GCC",
03837        BINFO_TYPE (binfo));
03838 
03839   /* This is an empty base class.  We first try to put it at offset
03840      zero.  */
03841   if (layout_conflict_p (binfo,
03842        BINFO_OFFSET (binfo),
03843        offsets, 
03844        /*vbases_p=*/0))
03845     {
03846       /* That didn't work.  Now, we move forward from the next
03847    available spot in the class.  */
03848       atend = true;
03849       propagate_binfo_offsets (binfo, convert (ssizetype, eoc), t);
03850       while (1) 
03851   {
03852     if (!layout_conflict_p (binfo,
03853           BINFO_OFFSET (binfo), 
03854           offsets,
03855           /*vbases_p=*/0))
03856       /* We finally found a spot where there's no overlap.  */
03857       break;
03858 
03859     /* There's overlap here, too.  Bump along to the next spot.  */
03860     propagate_binfo_offsets (binfo, alignment, t);
03861   }
03862     }
03863   return atend;
03864 }
03865 
03866 /* Layout the the base given by BINFO in the class indicated by RLI.
03867    *BASE_ALIGN is a running maximum of the alignments of
03868    any base class.  OFFSETS gives the location of empty base
03869    subobjects.  T is the most derived type.  Return nonzero if the new
03870    object cannot be nearly-empty.  A new FIELD_DECL is inserted at
03871    *NEXT_FIELD, unless BINFO is for an empty base class.  
03872 
03873    Returns the location at which the next field should be inserted.  */
03874 
03875 static tree *
03876 build_base_field (record_layout_info rli, tree binfo,
03877       splay_tree offsets, tree *next_field)
03878 {
03879   tree t = rli->t;
03880   tree basetype = BINFO_TYPE (binfo);
03881 
03882   if (!COMPLETE_TYPE_P (basetype))
03883     /* This error is now reported in xref_tag, thus giving better
03884        location information.  */
03885     return next_field;
03886   
03887   /* Place the base class.  */
03888   if (!is_empty_class (basetype))
03889     {
03890       tree decl;
03891 
03892       /* The containing class is non-empty because it has a non-empty
03893    base class.  */
03894       CLASSTYPE_EMPTY_P (t) = 0;
03895       
03896       /* Create the FIELD_DECL.  */
03897       decl = build_decl (FIELD_DECL, NULL_TREE, CLASSTYPE_AS_BASE (basetype));
03898       DECL_ARTIFICIAL (decl) = 1;
03899       DECL_FIELD_CONTEXT (decl) = t;
03900       DECL_SIZE (decl) = CLASSTYPE_SIZE (basetype);
03901       DECL_SIZE_UNIT (decl) = CLASSTYPE_SIZE_UNIT (basetype);
03902       DECL_ALIGN (decl) = CLASSTYPE_ALIGN (basetype);
03903       DECL_USER_ALIGN (decl) = CLASSTYPE_USER_ALIGN (basetype);
03904       DECL_IGNORED_P (decl) = 1;
03905 
03906       /* Try to place the field.  It may take more than one try if we
03907    have a hard time placing the field without putting two
03908    objects of the same type at the same address.  */
03909       layout_nonempty_base_or_field (rli, decl, binfo, offsets);
03910       /* Add the new FIELD_DECL to the list of fields for T.  */
03911       TREE_CHAIN (decl) = *next_field;
03912       *next_field = decl;
03913       next_field = &TREE_CHAIN (decl);
03914     }
03915   else
03916     {
03917       tree eoc;
03918       bool atend;
03919 
03920       /* On some platforms (ARM), even empty classes will not be
03921    byte-aligned.  */
03922       eoc = round_up (rli_size_unit_so_far (rli),
03923           CLASSTYPE_ALIGN_UNIT (basetype));
03924       atend = layout_empty_base (binfo, eoc, offsets, t);
03925       /* A nearly-empty class "has no proper base class that is empty,
03926    not morally virtual, and at an offset other than zero."  */
03927       if (!TREE_VIA_VIRTUAL (binfo) && CLASSTYPE_NEARLY_EMPTY_P (t))
03928   {
03929     if (atend)
03930       CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
03931     /* The check above (used in G++ 3.2) is insufficient  because
03932        an empty class placed at offset zero might itself have an
03933        empty base at a nonzero offset.  */
03934     else if (walk_subobject_offsets (basetype, 
03935              empty_base_at_nonzero_offset_p,
03936              size_zero_node,
03937              /*offsets=*/NULL,
03938              /*max_offset=*/NULL_TREE,
03939              /*vbases_p=*/true))
03940       {
03941         if (abi_version_at_least (2))
03942     CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
03943         else if (warn_abi)
03944     warning ("class `%T' will be considered nearly empty in a "
03945        "future version of GCC", t);
03946       }
03947   }
03948   
03949       /* We do not create a FIELD_DECL for empty base classes because
03950    it might overlap some other field.  We want to be able to
03951    create CONSTRUCTORs for the class by iterating over the
03952    FIELD_DECLs, and the back end does not handle overlapping
03953    FIELD_DECLs.  */
03954 
03955       /* An empty virtual base causes a class to be non-empty
03956    -- but in that case we do not need to clear CLASSTYPE_EMPTY_P
03957    here because that was already done when the virtual table
03958    pointer was created.  */
03959     }
03960 
03961   /* Record the offsets of BINFO and its base subobjects.  */
03962   record_subobject_offsets (binfo,
03963           BINFO_OFFSET (binfo),
03964           offsets, 
03965           /*vbases_p=*/0);
03966 
03967   return next_field;
03968 }
03969 
03970 /* Layout all of the non-virtual base classes.  Record empty
03971    subobjects in OFFSETS.  T is the most derived type.  Return nonzero
03972    if the type cannot be nearly empty.  The fields created
03973    corresponding to the base classes will be inserted at
03974    *NEXT_FIELD.  */
03975 
03976 static void
03977 build_base_fields (record_layout_info rli,
03978        splay_tree offsets, tree *next_field)
03979 {
03980   /* Chain to hold all the new FIELD_DECLs which stand in for base class
03981      subobjects.  */
03982   tree t = rli->t;
03983   int n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
03984   int i;
03985 
03986   /* The primary base class is always allocated first.  */
03987   if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
03988     next_field = build_base_field (rli, CLASSTYPE_PRIMARY_BINFO (t),
03989            offsets, next_field);
03990 
03991   /* Now allocate the rest of the bases.  */
03992   for (i = 0; i < n_baseclasses; ++i)
03993     {
03994       tree base_binfo;
03995 
03996       base_binfo = BINFO_BASETYPE (TYPE_BINFO (t), i);
03997 
03998       /* The primary base was already allocated above, so we don't
03999    need to allocate it again here.  */
04000       if (base_binfo == CLASSTYPE_PRIMARY_BINFO (t))
04001   continue;
04002 
04003       /* A primary virtual base class is allocated just like any other
04004    base class, but a non-primary virtual base is allocated
04005    later, in layout_virtual_bases.  */
04006       if (TREE_VIA_VIRTUAL (base_binfo) 
04007     && !BINFO_PRIMARY_P (base_binfo))
04008   continue;
04009 
04010       next_field = build_base_field (rli, base_binfo,
04011              offsets, next_field);
04012     }
04013 }
04014 
04015 /* Go through the TYPE_METHODS of T issuing any appropriate
04016    diagnostics, figuring out which methods override which other
04017    methods, and so forth.  */
04018 
04019 static void
04020 check_methods (t)
04021      tree t;
04022 {
04023   tree x;
04024 
04025   for (x = TYPE_METHODS (t); x; x = TREE_CHAIN (x))
04026     {
04027       /* If this was an evil function, don't keep it in class.  */
04028       if (DECL_ASSEMBLER_NAME_SET_P (x) 
04029     && IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (x)))
04030   continue;
04031 
04032       check_for_override (x, t);
04033       if (DECL_PURE_VIRTUAL_P (x) && ! DECL_VINDEX (x))
04034   cp_error_at ("initializer specified for non-virtual method `%D'", x);
04035 
04036       /* The name of the field is the original field name
04037    Save this in auxiliary field for later overloading.  */
04038       if (DECL_VINDEX (x))
04039   {
04040     TYPE_POLYMORPHIC_P (t) = 1;
04041     if (DECL_PURE_VIRTUAL_P (x))
04042       CLASSTYPE_PURE_VIRTUALS (t)
04043         = tree_cons (NULL_TREE, x, CLASSTYPE_PURE_VIRTUALS (t));
04044   }
04045     }
04046 }
04047 
04048 /* FN is a constructor or destructor.  Clone the declaration to create
04049    a specialized in-charge or not-in-charge version, as indicated by
04050    NAME.  */
04051 
04052 static tree
04053 build_clone (fn, name)
04054      tree fn;
04055      tree name;
04056 {
04057   tree parms;
04058   tree clone;
04059 
04060   /* Copy the function.  */
04061   clone = copy_decl (fn);
04062   /* Remember where this function came from.  */
04063   DECL_CLONED_FUNCTION (clone) = fn;
04064   DECL_ABSTRACT_ORIGIN (clone) = fn;
04065   /* Reset the function name.  */
04066   DECL_NAME (clone) = name;
04067   SET_DECL_ASSEMBLER_NAME (clone, NULL_TREE);
04068   /* There's no pending inline data for this function.  */
04069   DECL_PENDING_INLINE_INFO (clone) = NULL;
04070   DECL_PENDING_INLINE_P (clone) = 0;
04071   /* And it hasn't yet been deferred.  */
04072   DECL_DEFERRED_FN (clone) = 0;
04073 
04074   /* The base-class destructor is not virtual.  */
04075   if (name == base_dtor_identifier)
04076     {
04077       DECL_VIRTUAL_P (clone) = 0;
04078       if (TREE_CODE (clone) != TEMPLATE_DECL)
04079   DECL_VINDEX (clone) = NULL_TREE;
04080     }
04081 
04082   /* If there was an in-charge parameter, drop it from the function
04083      type.  */
04084   if (DECL_HAS_IN_CHARGE_PARM_P (clone))
04085     {
04086       tree basetype;
04087       tree parmtypes;
04088       tree exceptions;
04089 
04090       exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
04091       basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
04092       parmtypes = TYPE_ARG_TYPES (TREE_TYPE (clone));
04093       /* Skip the `this' parameter.  */
04094       parmtypes = TREE_CHAIN (parmtypes);
04095       /* Skip the in-charge parameter.  */
04096       parmtypes = TREE_CHAIN (parmtypes);
04097       /* And the VTT parm, in a complete [cd]tor.  */
04098       if (DECL_HAS_VTT_PARM_P (fn)
04099     && ! DECL_NEEDS_VTT_PARM_P (clone))
04100   parmtypes = TREE_CHAIN (parmtypes);
04101        /* If this is subobject constructor or destructor, add the vtt
04102    parameter.  */
04103       TREE_TYPE (clone) 
04104   = build_cplus_method_type (basetype,
04105            TREE_TYPE (TREE_TYPE (clone)),
04106            parmtypes);
04107       if (exceptions)
04108   TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone),
04109                  exceptions);
04110     }
04111 
04112   /* Copy the function parameters.  But, DECL_ARGUMENTS on a TEMPLATE_DECL
04113      aren't function parameters; those are the template parameters.  */
04114   if (TREE_CODE (clone) != TEMPLATE_DECL)
04115     {
04116       DECL_ARGUMENTS (clone) = copy_list (DECL_ARGUMENTS (clone));
04117       /* Remove the in-charge parameter.  */
04118       if (DECL_HAS_IN_CHARGE_PARM_P (clone))
04119   {
04120     TREE_CHAIN (DECL_ARGUMENTS (clone))
04121       = TREE_CHAIN (TREE_CHAIN (DECL_ARGUMENTS (clone)));
04122     DECL_HAS_IN_CHARGE_PARM_P (clone) = 0;
04123   }
04124       /* And the VTT parm, in a complete [cd]tor.  */
04125       if (DECL_HAS_VTT_PARM_P (fn))
04126   {
04127     if (DECL_NEEDS_VTT_PARM_P (clone))
04128       DECL_HAS_VTT_PARM_P (clone) = 1;
04129     else
04130       {
04131         TREE_CHAIN (DECL_ARGUMENTS (clone))
04132     = TREE_CHAIN (TREE_CHAIN (DECL_ARGUMENTS (clone)));
04133         DECL_HAS_VTT_PARM_P (clone) = 0;
04134       }
04135   }
04136 
04137       for (parms = DECL_ARGUMENTS (clone); parms; parms = TREE_CHAIN (parms))
04138   {
04139     DECL_CONTEXT (parms) = clone;
04140     cxx_dup_lang_specific_decl (parms);
04141   }
04142     }
04143 
04144   /* Create the RTL for this function.  */
04145   SET_DECL_RTL (clone, NULL_RTX);
04146   rest_of_decl_compilation (clone, NULL, /*top_level=*/1, at_eof);
04147   
04148   /* Make it easy to find the CLONE given the FN.  */
04149   TREE_CHAIN (clone) = TREE_CHAIN (fn);
04150   TREE_CHAIN (fn) = clone;
04151 
04152   /* If this is a template, handle the DECL_TEMPLATE_RESULT as well.  */
04153   if (TREE_CODE (clone) == TEMPLATE_DECL)
04154     {
04155       tree result;
04156 
04157       DECL_TEMPLATE_RESULT (clone) 
04158   = build_clone (DECL_TEMPLATE_RESULT (clone), name);
04159       result = DECL_TEMPLATE_RESULT (clone);
04160       DECL_TEMPLATE_INFO (result) = copy_node (DECL_TEMPLATE_INFO (result));
04161       DECL_TI_TEMPLATE (result) = clone;
04162     }
04163   else if (DECL_DEFERRED_FN (fn))
04164     defer_fn (clone);
04165 
04166   return clone;
04167 }
04168 
04169 /* Produce declarations for all appropriate clones of FN.  If
04170    UPDATE_METHOD_VEC_P is nonzero, the clones are added to the
04171    CLASTYPE_METHOD_VEC as well.  */
04172 
04173 void
04174 clone_function_decl (fn, update_method_vec_p)
04175      tree fn;
04176      int update_method_vec_p;
04177 {
04178   tree clone;
04179 
04180   /* Avoid inappropriate cloning.  */
04181   if (TREE_CHAIN (fn)
04182       && DECL_CLONED_FUNCTION (TREE_CHAIN (fn)))
04183     return;
04184 
04185   if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn))
04186     {
04187       /* For each constructor, we need two variants: an in-charge version
04188    and a not-in-charge version.  */
04189       clone = build_clone (fn, complete_ctor_identifier);
04190       if (update_method_vec_p)
04191   add_method (DECL_CONTEXT (clone), clone, /*error_p=*/0);
04192 #ifdef KEY
04193       /* Identify the copy constructor for use by the WHIRL translator to copy
04194    objects. */
04195       {
04196   tree type = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
04197   if (!TYPE_HAS_IMPLICIT_COPY_CONSTRUCTOR (type) &&
04198       DECL_COPY_CONSTRUCTOR_P (clone)) {
04199     if (!DECL_COMPLETE_CONSTRUCTOR_P (clone))
04200       abort ();
04201     CLASSTYPE_COPY_CONSTRUCTOR (type) = clone;
04202   }
04203       }
04204 #endif
04205 
04206       clone = build_clone (fn, base_ctor_identifier);
04207       if (update_method_vec_p)
04208   add_method (DECL_CONTEXT (clone), clone, /*error_p=*/0);
04209     }
04210   else
04211     {
04212       my_friendly_assert (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn), 20000411);
04213 
04214       /* For each destructor, we need three variants: an in-charge
04215    version, a not-in-charge version, and an in-charge deleting
04216    version.  We clone the deleting version first because that
04217    means it will go second on the TYPE_METHODS list -- and that
04218    corresponds to the correct layout order in the virtual
04219    function table.  
04220 
04221          For a non-virtual destructor, we do not build a deleting
04222    destructor.  */
04223       if (DECL_VIRTUAL_P (fn))
04224   {
04225     clone = build_clone (fn, deleting_dtor_identifier);
04226     if (update_method_vec_p)
04227       add_method (DECL_CONTEXT (clone), clone, /*error_p=*/0);
04228   }
04229       clone = build_clone (fn, complete_dtor_identifier);
04230       if (update_method_vec_p)
04231   add_method (DECL_CONTEXT (clone), clone, /*error_p=*/0);
04232       clone = build_clone (fn, base_dtor_identifier);
04233       if (update_method_vec_p)
04234   add_method (DECL_CONTEXT (clone), clone, /*error_p=*/0);
04235     }
04236 
04237   /* Note that this is an abstract function that is never emitted.  */
04238   DECL_ABSTRACT (fn) = 1;
04239 }
04240 
04241 /* DECL is an in charge constructor, which is being defined. This will
04242    have had an in class declaration, from whence clones were
04243    declared. An out-of-class definition can specify additional default
04244    arguments. As it is the clones that are involved in overload
04245    resolution, we must propagate the information from the DECL to its
04246    clones.  */
04247 
04248 void
04249 adjust_clone_args (decl)
04250      tree decl;
04251 {
04252   tree clone;
04253   
04254   for (clone = TREE_CHAIN (decl); clone && DECL_CLONED_FUNCTION (clone);
04255        clone = TREE_CHAIN (clone))
04256     {
04257       tree orig_clone_parms = TYPE_ARG_TYPES (TREE_TYPE (clone));
04258       tree orig_decl_parms = TYPE_ARG_TYPES (TREE_TYPE (decl));
04259       tree decl_parms, clone_parms;
04260 
04261       clone_parms = orig_clone_parms;
04262       
04263       /* Skip the 'this' parameter.  */
04264       orig_clone_parms = TREE_CHAIN (orig_clone_parms);
04265       orig_decl_parms = TREE_CHAIN (orig_decl_parms);
04266 
04267       if (DECL_HAS_IN_CHARGE_PARM_P (decl))
04268   orig_decl_parms = TREE_CHAIN (orig_decl_parms);
04269       if (DECL_HAS_VTT_PARM_P (decl))
04270   orig_decl_parms = TREE_CHAIN (orig_decl_parms);
04271       
04272       clone_parms = orig_clone_parms;
04273       if (DECL_HAS_VTT_PARM_P (clone))
04274   clone_parms = TREE_CHAIN (clone_parms);
04275       
04276       for (decl_parms = orig_decl_parms; decl_parms;
04277      decl_parms = TREE_CHAIN (decl_parms),
04278        clone_parms = TREE_CHAIN (clone_parms))
04279   {
04280     my_friendly_assert (same_type_p (TREE_TYPE (decl_parms),
04281              TREE_TYPE (clone_parms)), 20010424);
04282     
04283     if (TREE_PURPOSE (decl_parms) && !TREE_PURPOSE (clone_parms))
04284       {
04285         /* A default parameter has been added. Adjust the
04286      clone's parameters.  */
04287         tree exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
04288         tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
04289         tree type;
04290 
04291         clone_parms = orig_decl_parms;
04292 
04293         if (DECL_HAS_VTT_PARM_P (clone))
04294     {
04295       clone_parms = tree_cons (TREE_PURPOSE (orig_clone_parms),
04296              TREE_VALUE (orig_clone_parms),
04297              clone_parms);
04298       TREE_TYPE (clone_parms) = TREE_TYPE (orig_clone_parms);
04299     }
04300         type = build_cplus_method_type (basetype,
04301                 TREE_TYPE (TREE_TYPE (clone)),
04302                 clone_parms);
04303         if (exceptions)
04304     type = build_exception_variant (type, exceptions);
04305         TREE_TYPE (clone) = type;
04306         
04307         clone_parms = NULL_TREE;
04308         break;
04309       }
04310   }
04311       my_friendly_assert (!clone_parms, 20010424);
04312     }
04313 }
04314 
04315 /* For each of the constructors and destructors in T, create an
04316    in-charge and not-in-charge variant.  */
04317 
04318 static void
04319 clone_constructors_and_destructors (t)
04320      tree t;
04321 {
04322   tree fns;
04323 
04324   /* If for some reason we don't have a CLASSTYPE_METHOD_VEC, we bail
04325      out now.  */
04326   if (!CLASSTYPE_METHOD_VEC (t))
04327     return;
04328 
04329   for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
04330     clone_function_decl (OVL_CURRENT (fns), /*update_method_vec_p=*/1);
04331   for (fns = CLASSTYPE_DESTRUCTORS (t); fns; fns = OVL_NEXT (fns))
04332     clone_function_decl (OVL_CURRENT (fns), /*update_method_vec_p=*/1);
04333 }
04334 
04335 /* Remove all zero-width bit-fields from T.  */
04336 
04337 static void
04338 remove_zero_width_bit_fields (t)
04339      tree t;
04340 {
04341   tree *fieldsp;
04342 
04343   fieldsp = &TYPE_FIELDS (t); 
04344   while (*fieldsp)
04345     {
04346       if (TREE_CODE (*fieldsp) == FIELD_DECL
04347     && DECL_C_BIT_FIELD (*fieldsp) 
04348     && DECL_INITIAL (*fieldsp))
04349   *fieldsp = TREE_CHAIN (*fieldsp);
04350       else
04351   fieldsp = &TREE_CHAIN (*fieldsp);
04352     }
04353 }
04354 
04355 /* Returns TRUE iff we need a cookie when dynamically allocating an
04356    array whose elements have the indicated class TYPE.  */
04357 
04358 static bool
04359 type_requires_array_cookie (type)
04360      tree type;
04361 {
04362   tree fns;
04363   bool has_two_argument_delete_p = false;
04364 
04365   my_friendly_assert (CLASS_TYPE_P (type), 20010712);
04366 
04367   /* If there's a non-trivial destructor, we need a cookie.  In order
04368      to iterate through the array calling the destructor for each
04369      element, we'll have to know how many elements there are.  */
04370   if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
04371     return true;
04372 
04373   /* If the usual deallocation function is a two-argument whose second
04374      argument is of type `size_t', then we have to pass the size of
04375      the array to the deallocation function, so we will need to store
04376      a cookie.  */
04377   fns = lookup_fnfields (TYPE_BINFO (type), 
04378        ansi_opname (VEC_DELETE_EXPR),
04379        /*protect=*/0);
04380   /* If there are no `operator []' members, or the lookup is
04381      ambiguous, then we don't need a cookie.  */
04382   if (!fns || fns == error_mark_node)
04383     return false;
04384   /* Loop through all of the functions.  */
04385   for (fns = BASELINK_FUNCTIONS (fns); fns; fns = OVL_NEXT (fns))
04386     {
04387       tree fn;
04388       tree second_parm;
04389 
04390       /* Select the current function.  */
04391       fn = OVL_CURRENT (fns);
04392       /* See if this function is a one-argument delete function.  If
04393    it is, then it will be the usual deallocation function.  */
04394       second_parm = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fn)));
04395       if (second_parm == void_list_node)
04396   return false;
04397       /* Otherwise, if we have a two-argument function and the second
04398    argument is `size_t', it will be the usual deallocation
04399    function -- unless there is one-argument function, too.  */
04400       if (TREE_CHAIN (second_parm) == void_list_node
04401     && same_type_p (TREE_VALUE (second_parm), sizetype))
04402   has_two_argument_delete_p = true;
04403     }
04404 
04405   return has_two_argument_delete_p;
04406 }
04407 
04408 /* Check the validity of the bases and members declared in T.  Add any
04409    implicitly-generated functions (like copy-constructors and
04410    assignment operators).  Compute various flag bits (like
04411    CLASSTYPE_NON_POD_T) for T.  This routine works purely at the C++
04412    level: i.e., independently of the ABI in use.  */
04413 
04414 static void
04415 check_bases_and_members (tree t)
04416 {
04417   /* Nonzero if we are not allowed to generate a default constructor
04418      for this case.  */
04419   int cant_have_default_ctor;
04420   /* Nonzero if the implicitly generated copy constructor should take
04421      a non-const reference argument.  */
04422   int cant_have_const_ctor;
04423   /* Nonzero if the the implicitly generated assignment operator
04424      should take a non-const reference argument.  */
04425   int no_const_asn_ref;
04426   tree access_decls;
04427 
04428   /* By default, we use const reference arguments and generate default
04429      constructors.  */
04430   cant_have_default_ctor = 0;
04431   cant_have_const_ctor = 0;
04432   no_const_asn_ref = 0;
04433 
04434   /* Check all the base-classes.  */
04435   check_bases (t, &cant_have_default_ctor, &cant_have_const_ctor,
04436          &no_const_asn_ref);
04437 
04438   /* Check all the data member declarations.  */
04439   check_field_decls (t, &access_decls,
04440          &cant_have_default_ctor,
04441          &cant_have_const_ctor,
04442          &no_const_asn_ref);
04443 
04444   /* Check all the method declarations.  */
04445   check_methods (t);
04446 
04447   /* A nearly-empty class has to be vptr-containing; a nearly empty
04448      class contains just a vptr.  */
04449   if (!TYPE_CONTAINS_VPTR_P (t))
04450     CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
04451 
04452   /* Do some bookkeeping that will guide the generation of implicitly
04453      declared member functions.  */
04454   TYPE_HAS_COMPLEX_INIT_REF (t)
04455     |= (TYPE_HAS_INIT_REF (t) 
04456   || TYPE_USES_VIRTUAL_BASECLASSES (t)
04457   || TYPE_POLYMORPHIC_P (t));
04458   TYPE_NEEDS_CONSTRUCTING (t)
04459     |= (TYPE_HAS_CONSTRUCTOR (t) 
04460   || TYPE_USES_VIRTUAL_BASECLASSES (t)
04461   || TYPE_POLYMORPHIC_P (t));
04462   CLASSTYPE_NON_AGGREGATE (t) |= (TYPE_HAS_CONSTRUCTOR (t)
04463           || TYPE_POLYMORPHIC_P (t));
04464   CLASSTYPE_NON_POD_P (t)
04465     |= (CLASSTYPE_NON_AGGREGATE (t) || TYPE_HAS_DESTRUCTOR (t) 
04466   || TYPE_HAS_ASSIGN_REF (t));
04467   TYPE_HAS_REAL_ASSIGN_REF (t) |= TYPE_HAS_ASSIGN_REF (t);
04468   TYPE_HAS_COMPLEX_ASSIGN_REF (t)
04469     |= TYPE_HAS_ASSIGN_REF (t) || TYPE_CONTAINS_VPTR_P (t);
04470 
04471   /* Synthesize any needed methods.  Note that methods will be synthesized
04472      for anonymous unions; grok_x_components undoes that.  */
04473   add_implicitly_declared_members (t, cant_have_default_ctor,
04474            cant_have_const_ctor,
04475            no_const_asn_ref);
04476 
04477   /* Create the in-charge and not-in-charge variants of constructors
04478      and destructors.  */
04479   clone_constructors_and_destructors (t);
04480 
04481   /* Process the using-declarations.  */
04482   for (; access_decls; access_decls = TREE_CHAIN (access_decls))
04483     handle_using_decl (TREE_VALUE (access_decls), t);
04484 
04485   /* Build and sort the CLASSTYPE_METHOD_VEC.  */
04486   finish_struct_methods (t);
04487 
04488   /* Figure out whether or not we will need a cookie when dynamically
04489      allocating an array of this type.  */
04490   TYPE_LANG_SPECIFIC (t)->u.c.vec_new_uses_cookie
04491     = type_requires_array_cookie (t);
04492 }
04493 
04494 /* If T needs a pointer to its virtual function table, set TYPE_VFIELD
04495    accordingly.  If a new vfield was created (because T doesn't have a
04496    primary base class), then the newly created field is returned.  It
04497    is not added to the TYPE_FIELDS list; it is the caller's
04498    responsibility to do that.  Accumulate declared virtual functions
04499    on VIRTUALS_P.  */
04500 
04501 static tree
04502 create_vtable_ptr (t, virtuals_p)
04503      tree t;
04504      tree *virtuals_p;
04505 {
04506   tree fn;
04507 
04508   /* Collect the virtual functions declared in T.  */
04509   for (fn = TYPE_METHODS (t); fn; fn = TREE_CHAIN (fn))
04510     if (DECL_VINDEX (fn) && !DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn)
04511   && TREE_CODE (DECL_VINDEX (fn)) != INTEGER_CST)
04512       {
04513   tree new_virtual = make_node (TREE_LIST);
04514   
04515   BV_FN (new_virtual) = fn;
04516   BV_DELTA (new_virtual) = integer_zero_node;
04517 
04518   TREE_CHAIN (new_virtual) = *virtuals_p;
04519   *virtuals_p = new_virtual;
04520       }
04521   
04522   /* If we couldn't find an appropriate base class, create a new field
04523      here.  Even if there weren't any new virtual functions, we might need a
04524      new virtual function table if we're supposed to include vptrs in
04525      all classes that need them.  */
04526   if (!TYPE_VFIELD (t) && (*virtuals_p || TYPE_CONTAINS_VPTR_P (t)))
04527     {
04528       /* We build this decl with vtbl_ptr_type_node, which is a
04529    `vtable_entry_type*'.  It might seem more precise to use
04530    `vtable_entry_type (*)[N]' where N is the number of firtual
04531    functions.  However, that would require the vtable pointer in
04532    base classes to have a different type than the vtable pointer
04533    in derived classes.  We could make that happen, but that
04534    still wouldn't solve all the problems.  In particular, the
04535    type-based alias analysis code would decide that assignments
04536    to the base class vtable pointer can't alias assignments to
04537    the derived class vtable pointer, since they have different
04538    types.  Thus, in an derived class destructor, where the base
04539    class constructor was inlined, we could generate bad code for
04540    setting up the vtable pointer.  
04541 
04542          Therefore, we use one type for all vtable pointers.  We still
04543    use a type-correct type; it's just doesn't indicate the array
04544    bounds.  That's better than using `void*' or some such; it's
04545    cleaner, and it let's the alias analysis code know that these
04546    stores cannot alias stores to void*!  */
04547       tree field;
04548 
04549       field = build_decl (FIELD_DECL, get_vfield_name (t), vtbl_ptr_type_node);
04550       SET_DECL_ASSEMBLER_NAME (field, get_identifier (VFIELD_BASE));
04551       DECL_VIRTUAL_P (field) = 1;
04552       DECL_ARTIFICIAL (field) = 1;
04553       DECL_FIELD_CONTEXT (field) = t;
04554       DECL_FCONTEXT (field) = t;
04555       DECL_ALIGN (field) = TYPE_ALIGN (vtbl_ptr_type_node);
04556       DECL_USER_ALIGN (field) = TYPE_USER_ALIGN (vtbl_ptr_type_node);
04557       
04558       TYPE_VFIELD (t) = field;
04559       
04560       /* This class is non-empty.  */
04561       CLASSTYPE_EMPTY_P (t) = 0;
04562 
04563       if (CLASSTYPE_N_BASECLASSES (t))
04564   /* If there were any baseclasses, they can't possibly be at
04565      offset zero any more, because that's where the vtable
04566      pointer is.  So, converting to a base class is going to
04567      take work.  */
04568   TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (t) = 1;
04569 
04570       return field;
04571     }
04572 
04573   return NULL_TREE;
04574 }
04575 
04576 /* Fixup the inline function given by INFO now that the class is
04577    complete.  */
04578 
04579 static void
04580 fixup_pending_inline (fn)
04581      tree fn;
04582 {
04583   if (DECL_PENDING_INLINE_INFO (fn))
04584     {
04585       tree args = DECL_ARGUMENTS (fn);
04586       while (args)
04587   {
04588     DECL_CONTEXT (args) = fn;
04589     args = TREE_CHAIN (args);
04590   }
04591     }
04592 }
04593 
04594 /* Fixup the inline methods and friends in TYPE now that TYPE is
04595    complete.  */
04596 
04597 static void
04598 fixup_inline_methods (type)
04599      tree type;
04600 {
04601   tree method = TYPE_METHODS (type);
04602 
04603   if (method && TREE_CODE (method) == TREE_VEC)
04604     {
04605       if (TREE_VEC_ELT (method, 1))
04606   method = TREE_VEC_ELT (method, 1);
04607       else if (TREE_VEC_ELT (method, 0))
04608   method = TREE_VEC_ELT (method, 0);
04609       else
04610   method = TREE_VEC_ELT (method, 2);
04611     }
04612 
04613   /* Do inline member functions.  */
04614   for (; method; method = TREE_CHAIN (method))
04615     fixup_pending_inline (method);
04616 
04617   /* Do friends.  */
04618   for (method = CLASSTYPE_INLINE_FRIENDS (type); 
04619        method; 
04620        method = TREE_CHAIN (method))
04621     fixup_pending_inline (TREE_VALUE (method));
04622   CLASSTYPE_INLINE_FRIENDS (type) = NULL_TREE;
04623 }
04624 
04625 /* Add OFFSET to all base types of BINFO which is a base in the
04626    hierarchy dominated by T.
04627 
04628    OFFSET, which is a type offset, is number of bytes.  */
04629 
04630 static void
04631 propagate_binfo_offsets (binfo, offset, t)
04632      tree binfo;
04633      tree offset;
04634      tree t;
04635 {
04636   int i;
04637   tree primary_binfo;
04638 
04639   /* Update BINFO's offset.  */
04640   BINFO_OFFSET (binfo)
04641     = convert (sizetype, 
04642          size_binop (PLUS_EXPR,
04643          convert (ssizetype, BINFO_OFFSET (binfo)),
04644          offset));
04645 
04646   /* Find the primary base class.  */
04647   primary_binfo = get_primary_binfo (binfo);
04648 
04649   /* Scan all of the bases, pushing the BINFO_OFFSET adjust
04650      downwards.  */
04651   for (i = -1; i < BINFO_N_BASETYPES (binfo); ++i)
04652     {
04653       tree base_binfo;
04654 
04655       /* On the first time through the loop, do the primary base.
04656    Because the primary base need not be an immediate base, we
04657    must handle the primary base specially.  */
04658       if (i == -1) 
04659   {
04660     if (!primary_binfo) 
04661       continue;
04662 
04663     base_binfo = primary_binfo;
04664   }
04665       else
04666   {
04667     base_binfo = BINFO_BASETYPE (binfo, i);
04668     /* Don't do the primary base twice.  */
04669     if (base_binfo == primary_binfo)
04670       continue;
04671   }
04672 
04673       /* Skip virtual bases that aren't our canonical primary base.  */
04674       if (TREE_VIA_VIRTUAL (base_binfo)
04675     && (BINFO_PRIMARY_BASE_OF (base_binfo) != binfo
04676         || base_binfo != binfo_for_vbase (BINFO_TYPE (base_binfo), t)))
04677   continue;
04678 
04679       propagate_binfo_offsets (base_binfo, offset, t);
04680     }
04681 }
04682 
04683 /* Called via dfs_walk from layout_virtual bases.  */
04684 
04685 static tree
04686 dfs_set_offset_for_unshared_vbases (binfo, data)
04687      tree binfo;
04688      void *data;
04689 {
04690   /* If this is a virtual base, make sure it has the same offset as
04691      the shared copy.  If it's a primary base, then we know it's
04692      correct.  */
04693   if (TREE_VIA_VIRTUAL (binfo))
04694     {
04695       tree t = (tree) data;
04696       tree vbase;
04697       tree offset;
04698       
04699       vbase = binfo_for_vbase (BINFO_TYPE (binfo), t);
04700       if (vbase != binfo)
04701   {
04702     offset = size_diffop (BINFO_OFFSET (vbase), BINFO_OFFSET (binfo));
04703     propagate_binfo_offsets (binfo, offset, t);
04704   }
04705     }
04706 
04707   return NULL_TREE;
04708 }
04709 
04710 /* Set BINFO_OFFSET for all of the virtual bases for RLI->T.  Update
04711    TYPE_ALIGN and TYPE_SIZE for T.  OFFSETS gives the location of
04712    empty subobjects of T.  */
04713 
04714 static void
04715 layout_virtual_bases (record_layout_info rli, splay_tree offsets)
04716 {
04717   tree vbases;
04718   tree t = rli->t;
04719   bool first_vbase = true;
04720   tree *next_field;
04721 
04722   if (CLASSTYPE_N_BASECLASSES (t) == 0)
04723     return;
04724 
04725   if (!abi_version_at_least(2))
04726     {
04727       /* In G++ 3.2, we incorrectly rounded the size before laying out
04728    the virtual bases.  */
04729       finish_record_layout (rli, /*free_p=*/false);
04730 #ifdef STRUCTURE_SIZE_BOUNDARY
04731       /* Packed structures don't need to have minimum size.  */
04732       if (! TYPE_PACKED (t))
04733   TYPE_ALIGN (t) = MAX (TYPE_ALIGN (t), STRUCTURE_SIZE_BOUNDARY);
04734 #endif
04735       rli->offset = TYPE_SIZE_UNIT (t);
04736       rli->bitpos = bitsize_zero_node;
04737       rli->record_align = TYPE_ALIGN (t);
04738     }
04739 
04740   /* Find the last field.  The artificial fields created for virtual
04741      bases will go after the last extant field to date.  */
04742   next_field = &TYPE_FIELDS (t);
04743   while (*next_field)
04744     next_field = &TREE_CHAIN (*next_field);
04745 
04746   /* Go through the virtual bases, allocating space for each virtual
04747      base that is not already a primary base class.  These are
04748      allocated in inheritance graph order.  */
04749   for (vbases = TYPE_BINFO (t);
04750        vbases; 
04751        vbases = TREE_CHAIN (vbases))
04752     {
04753       tree vbase;
04754 
04755       if (!TREE_VIA_VIRTUAL (vbases))
04756   continue;
04757 
04758       vbase = binfo_for_vbase (BINFO_TYPE (vbases), t);
04759 
04760       if (!BINFO_PRIMARY_P (vbase))
04761   {
04762     tree basetype = TREE_TYPE (vbase);
04763 
04764     /* This virtual base is not a primary base of any class in the
04765        hierarchy, so we have to add space for it.  */
04766     next_field = build_base_field (rli, vbase,
04767            offsets, next_field);
04768 
04769     /* If the first virtual base might have been placed at a
04770        lower address, had we started from CLASSTYPE_SIZE, rather
04771        than TYPE_SIZE, issue a warning.  There can be both false
04772        positives and false negatives from this warning in rare
04773        cases; to deal with all the possibilities would probably
04774        require performing both layout algorithms and comparing
04775        the results which is not particularly tractable.  */
04776     if (warn_abi
04777         && first_vbase
04778         && (tree_int_cst_lt 
04779       (size_binop (CEIL_DIV_EXPR,
04780              round_up (CLASSTYPE_SIZE (t),
04781            CLASSTYPE_ALIGN (basetype)),
04782              bitsize_unit_node),
04783        BINFO_OFFSET (vbase))))
04784       warning ("offset of virtual base `%T' is not ABI-compliant and may change in a future version of GCC",
04785          basetype);
04786 
04787     first_vbase = false;
04788   }
04789     }
04790 
04791   /* Now, go through the TYPE_BINFO hierarchy, setting the
04792      BINFO_OFFSETs correctly for all non-primary copies of the virtual
04793      bases and their direct and indirect bases.  The ambiguity checks
04794      in lookup_base depend on the BINFO_OFFSETs being set
04795      correctly.  */
04796   dfs_walk (TYPE_BINFO (t), dfs_set_offset_for_unshared_vbases, NULL, t);
04797 }
04798 
04799 /* Returns the offset of the byte just past the end of the base class
04800    BINFO.  */
04801 
04802 static tree
04803 end_of_base (tree binfo)
04804 {
04805   tree size;
04806 
04807   if (is_empty_class (BINFO_TYPE (binfo)))
04808     /* An empty class has zero CLASSTYPE_SIZE_UNIT, but we need to
04809        allocate some space for it. It cannot have virtual bases, so
04810        TYPE_SIZE_UNIT is fine.  */
04811     size = TYPE_SIZE_UNIT (BINFO_TYPE (binfo));
04812   else
04813     size = CLASSTYPE_SIZE_UNIT (BINFO_TYPE (binfo));
04814 
04815   return size_binop (PLUS_EXPR, BINFO_OFFSET (binfo), size);
04816 }
04817 
04818 /* Returns the offset of the byte just past the end of the base class
04819    with the highest offset in T.  If INCLUDE_VIRTUALS_P is zero, then
04820    only non-virtual bases are included.  */
04821 
04822 static tree
04823 end_of_class (t, include_virtuals_p)
04824      tree t;
04825      int include_virtuals_p;
04826 {
04827   tree result = size_zero_node;
04828   tree binfo;
04829   tree offset;
04830   int i;
04831 
04832   for (i = 0; i < CLASSTYPE_N_BASECLASSES (t); ++i)
04833     {
04834       binfo = BINFO_BASETYPE (TYPE_BINFO (t), i);
04835 
04836       if (!include_virtuals_p
04837     && TREE_VIA_VIRTUAL (binfo) 
04838     && BINFO_PRIMARY_BASE_OF (binfo) != TYPE_BINFO (t))
04839   continue;
04840 
04841       offset = end_of_base (binfo);
04842       if (INT_CST_LT_UNSIGNED (result, offset))
04843   result = offset;
04844     }
04845 
04846   /* G++ 3.2 did not check indirect virtual bases.  */
04847   if (abi_version_at_least (2) && include_virtuals_p)
04848     for (binfo = CLASSTYPE_VBASECLASSES (t); 
04849    binfo; 
04850    binfo = TREE_CHAIN (binfo))
04851       {
04852   offset = end_of_base (TREE_VALUE (binfo));
04853   if (INT_CST_LT_UNSIGNED (result, offset))
04854     result = offset;
04855       }
04856 
04857   return result;
04858 }
04859 
04860 /* Warn about bases of T that are inaccessible because they are
04861    ambiguous.  For example:
04862 
04863      struct S {};
04864      struct T : public S {};
04865      struct U : public S, public T {};
04866 
04867    Here, `(S*) new U' is not allowed because there are two `S'
04868    subobjects of U.  */
04869 
04870 static void
04871 warn_about_ambiguous_bases (t)
04872      tree t;
04873 {
04874   int i;
04875   tree vbases;
04876   tree basetype;
04877 
04878   /* Check direct bases.  */
04879   for (i = 0; i < CLASSTYPE_N_BASECLASSES (t); ++i)
04880     {
04881       basetype = TYPE_BINFO_BASETYPE (t, i);
04882 
04883       if (!lookup_base (t, basetype, ba_ignore | ba_quiet, NULL))
04884   warning ("direct base `%T' inaccessible in `%T' due to ambiguity",
04885      basetype, t);
04886     }
04887 
04888   /* Check for ambiguous virtual bases.  */
04889   if (extra_warnings)
04890     for (vbases = CLASSTYPE_VBASECLASSES (t); 
04891    vbases; 
04892    vbases = TREE_CHAIN (vbases))
04893       {
04894   basetype = BINFO_TYPE (TREE_VALUE (vbases));
04895   
04896   if (!lookup_base (t, basetype, ba_ignore | ba_quiet, NULL))
04897     warning ("virtual base `%T' inaccessible in `%T' due to ambiguity",
04898        basetype, t);
04899       }
04900 }
04901 
04902 /* Compare two INTEGER_CSTs K1 and K2.  */
04903 
04904 static int
04905 splay_tree_compare_integer_csts (k1, k2)
04906      splay_tree_key k1;
04907      splay_tree_key k2;
04908 {
04909   return tree_int_cst_compare ((tree) k1, (tree) k2);
04910 }
04911 
04912 /* Increase the size indicated in RLI to account for empty classes
04913    that are "off the end" of the class.  */
04914 
04915 static void
04916 include_empty_classes (record_layout_info rli)
04917 {
04918   tree eoc;
04919   tree rli_size;
04920 
04921   /* It might be the case that we grew the class to allocate a
04922      zero-sized base class.  That won't be reflected in RLI, yet,
04923      because we are willing to overlay multiple bases at the same
04924      offset.  However, now we need to make sure that RLI is big enough
04925      to reflect the entire class.  */
04926   eoc = end_of_class (rli->t, 
04927           CLASSTYPE_AS_BASE (rli->t) != NULL_TREE);
04928   rli_size = rli_size_unit_so_far (rli);
04929   if (TREE_CODE (rli_size) == INTEGER_CST
04930       && INT_CST_LT_UNSIGNED (rli_size, eoc))
04931     {
04932       rli->bitpos = round_up (rli->bitpos, BITS_PER_UNIT);
04933       rli->bitpos 
04934   = size_binop (PLUS_EXPR, 
04935           rli->bitpos,
04936           size_binop (MULT_EXPR,
04937           convert (bitsizetype,
04938              size_binop (MINUS_EXPR,
04939                    eoc, rli_size)),
04940           bitsize_int (BITS_PER_UNIT)));
04941       normalize_rli (rli);
04942     }
04943 }
04944 
04945 /* Calculate the TYPE_SIZE, TYPE_ALIGN, etc for T.  Calculate
04946    BINFO_OFFSETs for all of the base-classes.  Position the vtable
04947    pointer.  Accumulate declared virtual functions on VIRTUALS_P.  */
04948 
04949 static void
04950 layout_class_type (tree t, tree *virtuals_p)
04951 {
04952   tree non_static_data_members;
04953   tree field;
04954   tree vptr;
04955   record_layout_info rli;
04956   /* Maps offsets (represented as INTEGER_CSTs) to a TREE_LIST of
04957      types that appear at that offset.  */
04958   splay_tree empty_base_offsets;
04959   /* True if the last field layed out was a bit-field.  */
04960   bool last_field_was_bitfield = false;
04961   /* The location at which the next field should be inserted.  */
04962   tree *next_field;
04963   /* T, as a base class.  */
04964   tree base_t;
04965 
04966   /* Keep track of the first non-static data member.  */
04967   non_static_data_members = TYPE_FIELDS (t);
04968 
04969   /* Start laying out the record.  */
04970   rli = start_record_layout (t);
04971 
04972   /* If possible, we reuse the virtual function table pointer from one
04973      of our base classes.  */
04974   determine_primary_base (t);
04975 
04976   /* Create a pointer to our virtual function table.  */
04977   vptr = create_vtable_ptr (t, virtuals_p);
04978 
04979   /* The vptr is always the first thing in the class.  */
04980   if (vptr)
04981     {
04982       TREE_CHAIN (vptr) = TYPE_FIELDS (t);
04983       TYPE_FIELDS (t) = vptr;
04984       next_field = &TREE_CHAIN (vptr);
04985       place_field (rli, vptr);
04986     }
04987   else
04988     next_field = &TYPE_FIELDS (t);
04989 
04990   /* Build FIELD_DECLs for all of the non-virtual base-types.  */
04991   empty_base_offsets = splay_tree_new (splay_tree_compare_integer_csts, 
04992                NULL, NULL);
04993   build_base_fields (rli, empty_base_offsets, next_field);
04994   
04995   /* Layout the non-static data members.  */
04996   for (field = non_static_data_members; field; field = TREE_CHAIN (field))
04997     {
04998       tree type;
04999       tree padding;
05000 
05001       /* We still pass things that aren't non-static data members to
05002    the back-end, in case it wants to do something with them.  */
05003       if (TREE_CODE (field) != FIELD_DECL)
05004   {
05005     place_field (rli, field);
05006     /* If the static data member has incomplete type, keep track
05007        of it so that it can be completed later.  (The handling 
05008        of pending statics in finish_record_layout is
05009        insufficient; consider:
05010 
05011          struct S1;
05012          struct S2 { static S1 s1; };
05013          
05014              At this point, finish_record_layout will be called, but
05015        S1 is still incomplete.)  */
05016     if (TREE_CODE (field) == VAR_DECL)
05017       maybe_register_incomplete_var (field);
05018     continue;
05019   }
05020 
05021       type = TREE_TYPE (field);
05022 
05023       /* If this field is a bit-field whose width is greater than its
05024    type, then there are some special rules for allocating
05025    it.  */
05026       if (DECL_C_BIT_FIELD (field)
05027     && INT_CST_LT (TYPE_SIZE (type), DECL_SIZE (field)))
05028   {
05029     integer_type_kind itk;
05030     tree integer_type;
05031 
05032     /* We must allocate the bits as if suitably aligned for the
05033        longest integer type that fits in this many bits.  type
05034        of the field.  Then, we are supposed to use the left over
05035        bits as additional padding.  */
05036     for (itk = itk_char; itk != itk_none; ++itk)
05037       if (INT_CST_LT (DECL_SIZE (field), 
05038           TYPE_SIZE (integer_types[itk])))
05039         break;
05040 
05041     /* ITK now indicates a type that is too large for the
05042        field.  We have to back up by one to find the largest
05043        type that fits.  */
05044     integer_type = integer_types[itk - 1];
05045 
05046     if (abi_version_at_least (2) && TREE_CODE (t) == UNION_TYPE)
05047       /* In a union, the padding field must have the full width
05048          of the bit-field; all fields start at offset zero.  */
05049       padding = DECL_SIZE (field);
05050     else
05051       {
05052         if (warn_abi && TREE_CODE (t) == UNION_TYPE)
05053     warning ("size assigned to `%T' may not be "
05054        "ABI-compliant and may change in a future "
05055        "version of GCC", 
05056        t);
05057         padding = size_binop (MINUS_EXPR, DECL_SIZE (field),
05058             TYPE_SIZE (integer_type));
05059       }
05060     DECL_SIZE (field) = TYPE_SIZE (integer_type);
05061     DECL_ALIGN (field) = TYPE_ALIGN (integer_type);
05062     DECL_USER_ALIGN (field) = TYPE_USER_ALIGN (integer_type);
05063     layout_nonempty_base_or_field (rli, field, NULL_TREE,
05064            empty_base_offsets);
05065     /* Now that layout has been performed, set the size of the
05066        field to the size of its declared type; the rest of the
05067        field is effectively invisible.  */
05068     DECL_SIZE (field) = TYPE_SIZE (type);
05069     /* We must also reset the DECL_MODE of the field.  */
05070     if (abi_version_at_least (2))
05071       DECL_MODE (field) = TYPE_MODE (type);
05072     else if (warn_abi
05073        && DECL_MODE (field) != TYPE_MODE (type))
05074       /* Versions of G++ before G++ 3.4 did not reset the
05075          DECL_MODE.  */
05076       warning ("the offset of `%D' may not be ABI-compliant and may "
05077          "change in a future version of GCC", field);
05078   }
05079       else
05080   {
05081     padding = NULL_TREE;
05082     layout_nonempty_base_or_field (rli, field, NULL_TREE,
05083            empty_base_offsets);
05084   }
05085 
05086       /* Remember the location of any empty classes in FIELD.  */
05087       if (abi_version_at_least (2))
05088   record_subobject_offsets (TREE_TYPE (field), 
05089           byte_position(field),
05090           empty_base_offsets,
05091           /*vbases_p=*/1);
05092 
05093       /* If a bit-field does not immediately follow another bit-field,
05094    and yet it starts in the middle of a byte, we have failed to
05095    comply with the ABI.  */
05096       if (warn_abi
05097     && DECL_C_BIT_FIELD (field) 
05098     && !last_field_was_bitfield
05099     && !integer_zerop (size_binop (TRUNC_MOD_EXPR,
05100            DECL_FIELD_BIT_OFFSET (field),
05101            bitsize_unit_node)))
05102   cp_warning_at ("offset of `%D' is not ABI-compliant and may change in a future version of GCC", 
05103            field);
05104 
05105       /* G++ used to use DECL_FIELD_OFFSET as if it were the byte
05106    offset of the field.  */
05107       if (warn_abi 
05108     && !tree_int_cst_equal (DECL_FIELD_OFFSET (field),
05109           byte_position (field))
05110     && contains_empty_class_p (TREE_TYPE (field)))
05111   cp_warning_at ("`%D' contains empty classes which may cause base "
05112            "classes to be placed at different locations in a "
05113            "future version of GCC",
05114            field);
05115 
05116       /* If we needed additional padding after this field, add it
05117    now.  */
05118       if (padding)
05119   {
05120     tree padding_field;
05121 
05122     padding_field = build_decl (FIELD_DECL, 
05123               NULL_TREE,
05124               char_type_node); 
05125     DECL_BIT_FIELD (padding_field) = 1;
05126     DECL_SIZE (padding_field) = padding;
05127     DECL_ALIGN (padding_field) = 1;
05128     DECL_USER_ALIGN (padding_field) = 0;
05129     layout_nonempty_base_or_field (rli, padding_field,
05130            NULL_TREE, 
05131            empty_base_offsets);
05132   }
05133 
05134       last_field_was_bitfield = DECL_C_BIT_FIELD (field);
05135     }
05136 
05137   if (abi_version_at_least (2) && !integer_zerop (rli->bitpos))
05138     {
05139       /* Make sure that we are on a byte boundary so that the size of
05140    the class without virtual bases will always be a round number
05141    of bytes.  */
05142       rli->bitpos = round_up (rli->bitpos, BITS_PER_UNIT);
05143       normalize_rli (rli);
05144     }
05145 
05146   /* G++ 3.2 does not allow virtual bases to be overlaid with tail
05147      padding.  */
05148   if (!abi_version_at_least (2))
05149     include_empty_classes(rli);
05150 
05151   /* Delete all zero-width bit-fields from the list of fields.  Now
05152      that the type is laid out they are no longer important.  */
05153   remove_zero_width_bit_fields (t);
05154 
05155   /* Create the version of T used for virtual bases.  We do not use
05156      make_aggr_type for this version; this is an artificial type.  For
05157      a POD type, we just reuse T.  */
05158   if (CLASSTYPE_NON_POD_P (t) || CLASSTYPE_EMPTY_P (t))
05159     {
05160       base_t = make_node (TREE_CODE (t));
05161       
05162       /* Set the size and alignment for the new type.  In G++ 3.2, all
05163    empty classes were considered to have size zero when used as
05164    base classes.  */
05165       if (!abi_version_at_least (2) && CLASSTYPE_EMPTY_P (t))
05166   {
05167     TYPE_SIZE (base_t) = bitsize_zero_node;
05168     TYPE_SIZE_UNIT (base_t) = size_zero_node;
05169     if (warn_abi && !integer_zerop (rli_size_unit_so_far (rli)))
05170       warning ("layout of classes derived from empty class `%T' "
05171          "may change in a future version of GCC",
05172          t);
05173   }
05174       else
05175   {
05176     tree eoc;
05177 
05178     /* If the ABI version is not at least two, and the last
05179        field was a bit-field, RLI may not be on a byte
05180        boundary.  In particular, rli_size_unit_so_far might
05181        indicate the last complete byte, while rli_size_so_far
05182        indicates the total number of bits used.  Therefore,
05183        rli_size_so_far, rather than rli_size_unit_so_far, is
05184        used to compute TYPE_SIZE_UNIT.  */
05185     eoc = end_of_class (t, /*include_virtuals_p=*/0);
05186     TYPE_SIZE_UNIT (base_t) 
05187       = size_binop (MAX_EXPR,
05188         convert (sizetype,
05189            size_binop (CEIL_DIV_EXPR,
05190                  rli_size_so_far (rli),
05191                  bitsize_int (BITS_PER_UNIT))),
05192         eoc);
05193     TYPE_SIZE (base_t) 
05194       = size_binop (MAX_EXPR,
05195         rli_size_so_far (rli),
05196         size_binop (MULT_EXPR,
05197               convert (bitsizetype, eoc),
05198               bitsize_int (BITS_PER_UNIT)));
05199   }
05200       TYPE_ALIGN (base_t) = rli->record_align;
05201       TYPE_USER_ALIGN (base_t) = TYPE_USER_ALIGN (t);
05202 
05203       /* Copy the fields from T.  */
05204       next_field = &TYPE_FIELDS (base_t);
05205       for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
05206   if (TREE_CODE (field) == FIELD_DECL)
05207     {
05208       *next_field = build_decl (FIELD_DECL,
05209               DECL_NAME (field), 
05210               TREE_TYPE (field));
05211       DECL_CONTEXT (*next_field) = base_t;
05212       DECL_FIELD_OFFSET (*next_field) = DECL_FIELD_OFFSET (field);
05213       DECL_FIELD_BIT_OFFSET (*next_field)
05214         = DECL_FIELD_BIT_OFFSET (field);
05215       DECL_BIT_FIELD (*next_field) = DECL_BIT_FIELD (field);
05216 #ifdef KEY
05217       DECL_SIZE (*next_field) = DECL_SIZE (field);
05218             DECL_SIZE_UNIT (*next_field) = DECL_SIZE_UNIT (field);
05219 #endif // KEY
05220       next_field = &TREE_CHAIN (*next_field);
05221     }
05222 
05223       /* Record the base version of the type.  */
05224       CLASSTYPE_AS_BASE (t) = base_t;
05225       TYPE_CONTEXT (base_t) = t;
05226     }
05227   else
05228     CLASSTYPE_AS_BASE (t) = t;
05229 
05230   /* Every empty class contains an empty class.  */
05231   if (CLASSTYPE_EMPTY_P (t))
05232     CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
05233 
05234   /* Set the TYPE_DECL for this type to contain the right
05235      value for DECL_OFFSET, so that we can use it as part
05236      of a COMPONENT_REF for multiple inheritance.  */
05237   layout_decl (TYPE_MAIN_DECL (t), 0);
05238 
05239   /* Now fix up any virtual base class types that we left lying
05240      around.  We must get these done before we try to lay out the
05241      virtual function table.  As a side-effect, this will remove the
05242      base subobject fields.  */
05243   layout_virtual_bases (rli, empty_base_offsets);
05244 
05245   /* Make sure that empty classes are reflected in RLI at this 
05246      point.  */
05247   include_empty_classes(rli);
05248 
05249   /* Make sure not to create any structures with zero size.  */
05250   if (integer_zerop (rli_size_unit_so_far (rli)) && CLASSTYPE_EMPTY_P (t))
05251     place_field (rli, 
05252      build_decl (FIELD_DECL, NULL_TREE, char_type_node));
05253 
05254   /* Let the back-end lay out the type.  */
05255   finish_record_layout (rli, /*free_p=*/true);
05256 
05257   /* Warn about bases that can't be talked about due to ambiguity.  */
05258   warn_about_ambiguous_bases (t);
05259 
05260   /* Clean up.  */
05261   splay_tree_delete (empty_base_offsets);
05262 }
05263 
05264 /* Returns the virtual function with which the vtable for TYPE is
05265    emitted, or NULL_TREE if that heuristic is not applicable to TYPE.  */
05266 
05267 static tree
05268 key_method (tree type)
05269 {
05270   tree method;
05271 
05272   if (TYPE_FOR_JAVA (type)
05273       || processing_template_decl
05274       || CLASSTYPE_TEMPLATE_INSTANTIATION (type)
05275       || CLASSTYPE_INTERFACE_KNOWN (type))
05276     return NULL_TREE;
05277 
05278   for (method = TYPE_METHODS (type); method != NULL_TREE;
05279        method = TREE_CHAIN (method))
05280     if (DECL_VINDEX (method) != NULL_TREE
05281   && ! DECL_DECLARED_INLINE_P (method)
05282   && ! DECL_PURE_VIRTUAL_P (method))
05283       return method;
05284 
05285   return NULL_TREE;
05286 }
05287 
05288 /* Perform processing required when the definition of T (a class type)
05289    is complete.  */
05290 
05291 void
05292 finish_struct_1 (t)
05293      tree t;
05294 {
05295   tree x;
05296   /* A TREE_LIST.  The TREE_VALUE of each node is a FUNCTION_DECL.  */
05297   tree virtuals = NULL_TREE;
05298   int n_fields = 0;
05299   tree vfield;
05300 
05301   if (COMPLETE_TYPE_P (t))
05302     {
05303       if (IS_AGGR_TYPE (t))
05304   error ("redefinition of `%#T'", t);
05305       else
05306   abort ();
05307       popclass ();
05308       return;
05309     }
05310 
05311   /* If this type was previously laid out as a forward reference,
05312      make sure we lay it out again.  */
05313   TYPE_SIZE (t) = NULL_TREE;
05314   CLASSTYPE_GOT_SEMICOLON (t) = 0;
05315   CLASSTYPE_PRIMARY_BINFO (t) = NULL_TREE;
05316 
05317   fixup_inline_methods (t);
05318   
05319   /* Make assumptions about the class; we'll reset the flags if
05320      necessary.  */
05321   CLASSTYPE_EMPTY_P (t) = 1;
05322   CLASSTYPE_NEARLY_EMPTY_P (t) = 1;
05323   CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 0;
05324 
05325   /* Do end-of-class semantic processing: checking the validity of the
05326      bases and members and add implicitly generated methods.  */
05327   check_bases_and_members (t);
05328 
05329   /* Find the key method */
05330     if (TYPE_CONTAINS_VPTR_P (t))
05331     {
05332       CLASSTYPE_KEY_METHOD (t) = key_method (t);
05333 
05334       /* If a polymorphic class has no key method, we may emit the vtable
05335    in every translation unit where the class definition appears. */
05336       if (CLASSTYPE_KEY_METHOD (t) == NULL_TREE)
05337   keyed_classes = tree_cons (NULL_TREE, t, keyed_classes);
05338     }
05339 
05340   /* Layout the class itself.  */
05341   layout_class_type (t, &virtuals);
05342 
05343   /* Make sure that we get our own copy of the vfield FIELD_DECL.  */
05344   vfield = TYPE_VFIELD (t);
05345   if (vfield && CLASSTYPE_HAS_PRIMARY_BASE_P (t))
05346     {
05347       tree primary = CLASSTYPE_PRIMARY_BINFO (t);
05348 
05349       my_friendly_assert (same_type_p (DECL_FIELD_CONTEXT (vfield),
05350                BINFO_TYPE (primary)),
05351         20010726);
05352       /* The vtable better be at the start.  */
05353       my_friendly_assert (integer_zerop (DECL_FIELD_OFFSET (vfield)),
05354         20010726);
05355       my_friendly_assert (integer_zerop (BINFO_OFFSET (primary)),
05356         20010726);
05357       
05358       vfield = copy_decl (vfield);
05359       DECL_FIELD_CONTEXT (vfield) = t;
05360       TYPE_VFIELD (t) = vfield;
05361     }
05362   else
05363     my_friendly_assert (!vfield || DECL_FIELD_CONTEXT (vfield) == t, 20010726);
05364 
05365   virtuals = modify_all_vtables (t, nreverse (virtuals));
05366 
05367   /* If we created a new vtbl pointer for this class, add it to the
05368      list.  */
05369   if (TYPE_VFIELD (t) && !CLASSTYPE_HAS_PRIMARY_BASE_P (t))
05370     CLASSTYPE_VFIELDS (t) 
05371       = chainon (CLASSTYPE_VFIELDS (t), build_tree_list (NULL_TREE, t));
05372 
05373   /* If necessary, create the primary vtable for this class.  */
05374   if (virtuals || TYPE_CONTAINS_VPTR_P (t))
05375     {
05376       /* We must enter these virtuals into the table.  */
05377       if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
05378   build_primary_vtable (NULL_TREE, t);
05379       else if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t), t))
05380   /* Here we know enough to change the type of our virtual
05381      function table, but we will wait until later this function.  */
05382   build_primary_vtable (CLASSTYPE_PRIMARY_BINFO (t), t);
05383     }
05384 
05385   if (TYPE_CONTAINS_VPTR_P (t))
05386     {
05387       int vindex;
05388       tree fn;
05389 
05390       if (TYPE_BINFO_VTABLE (t))
05391   my_friendly_assert (DECL_VIRTUAL_P (TYPE_BINFO_VTABLE (t)),
05392           20000116);
05393       if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
05394   my_friendly_assert (TYPE_BINFO_VIRTUALS (t) == NULL_TREE,
05395           20000116);
05396 
05397       /* Add entries for virtual functions introduced by this class.  */
05398       TYPE_BINFO_VIRTUALS (t) = chainon (TYPE_BINFO_VIRTUALS (t), virtuals);
05399 
05400       /* Set DECL_VINDEX for all functions declared in this class.  */
05401       for (vindex = 0, fn = BINFO_VIRTUALS (TYPE_BINFO (t)); 
05402      fn; 
05403      fn = TREE_CHAIN (fn), 
05404        vindex += (TARGET_VTABLE_USES_DESCRIPTORS
05405       ? TARGET_VTABLE_USES_DESCRIPTORS : 1))
05406   if (TREE_CODE (DECL_VINDEX (BV_FN (fn))) != INTEGER_CST)
05407     DECL_VINDEX (BV_FN (fn)) = build_shared_int_cst (vindex);
05408     }
05409 
05410   finish_struct_bits (t);
05411 
05412   /* Complete the rtl for any static member objects of the type we're
05413      working on.  */
05414   for (x = TYPE_FIELDS (t); x; x = TREE_CHAIN (x))
05415     if (TREE_CODE (x) == VAR_DECL && TREE_STATIC (x)
05416   && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (x)), t))
05417       DECL_MODE (x) = TYPE_MODE (t);
05418 
05419   /* Done with FIELDS...now decide whether to sort these for
05420      faster lookups later.
05421 
05422      We use a small number because most searches fail (succeeding
05423      ultimately as the search bores through the inheritance
05424      hierarchy), and we want this failure to occur quickly.  */
05425 
05426   n_fields = count_fields (TYPE_FIELDS (t));
05427   if (n_fields > 7)
05428     {
05429       tree field_vec = make_tree_vec (n_fields);
05430       add_fields_to_vec (TYPE_FIELDS (t), field_vec, 0);
05431       qsort (&TREE_VEC_ELT (field_vec, 0), n_fields, sizeof (tree),
05432        (int (*)(const void *, const void *))field_decl_cmp);
05433       if (! DECL_LANG_SPECIFIC (TYPE_MAIN_DECL (t)))
05434   retrofit_lang_decl (TYPE_MAIN_DECL (t));
05435       DECL_SORTED_FIELDS (TYPE_MAIN_DECL (t)) = field_vec;
05436     }
05437 
05438   if (TYPE_HAS_CONSTRUCTOR (t))
05439     {
05440       tree vfields = CLASSTYPE_VFIELDS (t);
05441 
05442       for (vfields = CLASSTYPE_VFIELDS (t);
05443      vfields; vfields = TREE_CHAIN (vfields))
05444   /* Mark the fact that constructor for T could affect anybody
05445      inheriting from T who wants to initialize vtables for
05446      VFIELDS's type.  */
05447   if (VF_BINFO_VALUE (vfields))
05448     TREE_ADDRESSABLE (vfields) = 1;
05449     }
05450 
05451   /* Make the rtl for any new vtables we have created, and unmark
05452      the base types we marked.  */
05453   finish_vtbls (t);
05454   
05455   /* Build the VTT for T.  */
05456   build_vtt (t);
05457 
05458   if (warn_nonvdtor && TYPE_POLYMORPHIC_P (t) && TYPE_HAS_DESTRUCTOR (t)
05459       && DECL_VINDEX (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 1)) == NULL_TREE)
05460     warning ("`%#T' has virtual functions but non-virtual destructor", t);
05461 
05462   complete_vars (t);
05463 
05464   if (warn_overloaded_virtual)
05465     warn_hidden (t);
05466 
05467   maybe_suppress_debug_info (t);
05468 
05469   dump_class_hierarchy (t);
05470   
05471   /* Finish debugging output for this type.  */
05472   rest_of_type_compilation (t, ! LOCAL_CLASS_P (t));
05473 }
05474 
05475 /* When T was built up, the member declarations were added in reverse
05476    order.  Rearrange them to declaration order.  */
05477 
05478 void
05479 unreverse_member_declarations (t)
05480      tree t;
05481 {
05482   tree next;
05483   tree prev;
05484   tree x;
05485 
05486   /* The following lists are all in reverse order.  Put them in
05487      declaration order now.  */
05488   TYPE_METHODS (t) = nreverse (TYPE_METHODS (t));
05489   CLASSTYPE_DECL_LIST (t) = nreverse (CLASSTYPE_DECL_LIST (t));
05490 
05491   /* Actually, for the TYPE_FIELDS, only the non TYPE_DECLs are in
05492      reverse order, so we can't just use nreverse.  */
05493   prev = NULL_TREE;
05494   for (x = TYPE_FIELDS (t); 
05495        x && TREE_CODE (x) != TYPE_DECL; 
05496        x = next)
05497     {
05498       next = TREE_CHAIN (x);
05499       TREE_CHAIN (x) = prev;
05500       prev = x;
05501     }
05502   if (prev)
05503     {
05504       TREE_CHAIN (TYPE_FIELDS (t)) = x;
05505       if (prev)
05506   TYPE_FIELDS (t) = prev;
05507     }
05508 }
05509 
05510 tree
05511 finish_struct (t, attributes)
05512      tree t, attributes;
05513 {
05514   const char *saved_filename = input_filename;
05515   int saved_lineno = lineno;
05516 
05517   /* Now that we've got all the field declarations, reverse everything
05518      as necessary.  */
05519   unreverse_member_declarations (t);
05520 
05521   cplus_decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
05522 
05523   /* Nadger the current location so that diagnostics point to the start of
05524      the struct, not the end.  */
05525   input_filename = DECL_SOURCE_FILE (TYPE_NAME (t));
05526   lineno = DECL_SOURCE_LINE (TYPE_NAME (t));
05527 
05528   if (processing_template_decl)
05529     {
05530       finish_struct_methods (t);
05531       TYPE_SIZE (t) = bitsize_zero_node;
05532     }
05533   else
05534     finish_struct_1 (t);
05535 
05536   input_filename = saved_filename;
05537   lineno = saved_lineno;
05538 
05539   TYPE_BEING_DEFINED (t) = 0;
05540 
05541   if (current_class_type)
05542     popclass ();
05543   else
05544     error ("trying to finish struct, but kicked out due to previous parse errors");
05545 
05546   if (processing_template_decl && at_function_scope_p ())
05547     add_stmt (build_min (TAG_DEFN, t));
05548 
05549   return t;
05550 }
05551 
05552 /* Return the dynamic type of INSTANCE, if known.
05553    Used to determine whether the virtual function table is needed
05554    or not.
05555 
05556    *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
05557    of our knowledge of its type.  *NONNULL should be initialized
05558    before this function is called.  */
05559 
05560 static tree
05561 fixed_type_or_null (instance, nonnull, cdtorp)
05562      tree instance;
05563      int *nonnull;
05564      int *cdtorp;
05565 {
05566   switch (TREE_CODE (instance))
05567     {
05568     case INDIRECT_REF:
05569       if (POINTER_TYPE_P (TREE_TYPE (instance)))
05570   return NULL_TREE;
05571       else
05572   return fixed_type_or_null (TREE_OPERAND (instance, 0),
05573            nonnull, cdtorp);
05574 
05575     case CALL_EXPR:
05576       /* This is a call to a constructor, hence it's never zero.  */
05577       if (TREE_HAS_CONSTRUCTOR (instance))
05578   {
05579     if (nonnull)
05580       *nonnull = 1;
05581     return TREE_TYPE (instance);
05582   }
05583       return NULL_TREE;
05584 
05585     case SAVE_EXPR:
05586       /* This is a call to a constructor, hence it's never zero.  */
05587       if (TREE_HAS_CONSTRUCTOR (instance))
05588   {
05589     if (nonnull)
05590       *nonnull = 1;
05591     return TREE_TYPE (instance);
05592   }
05593       return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp);
05594 
05595     case RTL_EXPR:
05596       return NULL_TREE;
05597 
05598     case PLUS_EXPR:
05599     case MINUS_EXPR:
05600       if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR)
05601   return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp);
05602       if (TREE_CODE (TREE_OPERAND (instance, 1)) == INTEGER_CST)
05603   /* Propagate nonnull.  */
05604   return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp);
05605       return NULL_TREE;
05606 
05607     case NOP_EXPR:
05608     case CONVERT_EXPR:
05609       return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp);
05610 
05611     case ADDR_EXPR:
05612       if (nonnull)
05613   *nonnull = 1;
05614       return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp);
05615 
05616     case COMPONENT_REF:
05617       return fixed_type_or_null (TREE_OPERAND (instance, 1), nonnull, cdtorp);
05618 
05619     case VAR_DECL:
05620     case FIELD_DECL:
05621       if (TREE_CODE (TREE_TYPE (instance)) == ARRAY_TYPE
05622     && IS_AGGR_TYPE (TREE_TYPE (TREE_TYPE (instance))))
05623   {
05624     if (nonnull)
05625       *nonnull = 1;
05626     return TREE_TYPE (TREE_TYPE (instance));
05627   }
05628       /* fall through...  */
05629     case TARGET_EXPR:
05630     case PARM_DECL:
05631     case RESULT_DECL:
05632       if (IS_AGGR_TYPE (TREE_TYPE (instance)))
05633   {
05634     if (nonnull)
05635       *nonnull = 1;
05636     return TREE_TYPE (instance);
05637   }
05638       else if (instance == current_class_ptr)
05639         {
05640           if (nonnull)
05641             *nonnull = 1;
05642         
05643           /* if we're in a ctor or dtor, we know our type.  */
05644           if (DECL_LANG_SPECIFIC (current_function_decl)
05645               && (DECL_CONSTRUCTOR_P (current_function_decl)
05646                   || DECL_DESTRUCTOR_P (current_function_decl)))
05647             {
05648               if (cdtorp)
05649                 *cdtorp = 1;
05650               return TREE_TYPE (TREE_TYPE (instance));
05651             }
05652         }
05653       else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
05654         {
05655           /* Reference variables should be references to objects.  */
05656           if (nonnull)
05657       *nonnull = 1;
05658     
05659     /* DECL_VAR_MARKED_P is used to prevent recursion; a
05660        variable's initializer may refer to the variable
05661        itself.  */
05662     if (TREE_CODE (instance) == VAR_DECL 
05663         && DECL_INITIAL (instance)
05664         && !DECL_VAR_MARKED_P (instance))
05665       {
05666         tree type;
05667         DECL_VAR_MARKED_P (instance) = 1;
05668         type = fixed_type_or_null (DECL_INITIAL (instance),
05669            nonnull, cdtorp);
05670         DECL_VAR_MARKED_P (instance) = 0;
05671         return type;
05672       }
05673   }
05674       return NULL_TREE;
05675 
05676     default:
05677       return NULL_TREE;
05678     }
05679 }
05680 
05681 /* Return nonzero if the dynamic type of INSTANCE is known, and
05682    equivalent to the static type.  We also handle the case where
05683    INSTANCE is really a pointer. Return negative if this is a
05684    ctor/dtor. There the dynamic type is known, but this might not be
05685    the most derived base of the original object, and hence virtual
05686    bases may not be layed out according to this type.
05687 
05688    Used to determine whether the virtual function table is needed
05689    or not.
05690 
05691    *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
05692    of our knowledge of its type.  *NONNULL should be initialized
05693    before this function is called.  */
05694 
05695 int
05696 resolves_to_fixed_type_p (instance, nonnull)
05697      tree instance;
05698      int *nonnull;
05699 {
05700   tree t = TREE_TYPE (instance);
05701   int cdtorp = 0;
05702   
05703   tree fixed = fixed_type_or_null (instance, nonnull, &cdtorp);
05704   if (fixed == NULL_TREE)
05705     return 0;
05706   if (POINTER_TYPE_P (t))
05707     t = TREE_TYPE (t);
05708   if (!same_type_ignoring_top_level_qualifiers_p (t, fixed))
05709     return 0;
05710   return cdtorp ? -1 : 1;
05711 }
05712 
05713 
05714 void
05715 init_class_processing ()
05716 {
05717   current_class_depth = 0;
05718   current_class_stack_size = 10;
05719   current_class_stack 
05720     = (class_stack_node_t) xmalloc (current_class_stack_size 
05721             * sizeof (struct class_stack_node));
05722   VARRAY_TREE_INIT (local_classes, 8, "local_classes");
05723 
05724   access_default_node = build_int_2 (0, 0);
05725   access_public_node = build_int_2 (ak_public, 0);
05726   access_protected_node = build_int_2 (ak_protected, 0);
05727   access_private_node = build_int_2 (ak_private, 0);
05728   access_default_virtual_node = build_int_2 (4, 0);
05729   access_public_virtual_node = build_int_2 (4 | ak_public, 0);
05730   access_protected_virtual_node = build_int_2 (4 | ak_protected, 0);
05731   access_private_virtual_node = build_int_2 (4 | ak_private, 0);
05732 
05733   ridpointers[(int) RID_PUBLIC] = access_public_node;
05734   ridpointers[(int) RID_PRIVATE] = access_private_node;
05735   ridpointers[(int) RID_PROTECTED] = access_protected_node;
05736 }
05737 
05738 /* Set current scope to NAME. CODE tells us if this is a
05739    STRUCT, UNION, or ENUM environment.
05740 
05741    NAME may end up being NULL_TREE if this is an anonymous or
05742    late-bound struct (as in "struct { ... } foo;")  */
05743 
05744 /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE to
05745    appropriate values, found by looking up the type definition of
05746    NAME (as a CODE).
05747 
05748    If MODIFY is 1, we set IDENTIFIER_CLASS_VALUE's of names
05749    which can be seen locally to the class.  They are shadowed by
05750    any subsequent local declaration (including parameter names).
05751 
05752    If MODIFY is 2, we set IDENTIFIER_CLASS_VALUE's of names
05753    which have static meaning (i.e., static members, static
05754    member functions, enum declarations, etc).
05755 
05756    If MODIFY is 3, we set IDENTIFIER_CLASS_VALUE of names
05757    which can be seen locally to the class (as in 1), but
05758    know that we are doing this for declaration purposes
05759    (i.e. friend foo::bar (int)).
05760 
05761    So that we may avoid calls to lookup_name, we cache the _TYPE
05762    nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
05763 
05764    For multiple inheritance, we perform a two-pass depth-first search
05765    of the type lattice.  The first pass performs a pre-order search,
05766    marking types after the type has had its fields installed in
05767    the appropriate IDENTIFIER_CLASS_VALUE slot.  The second pass merely
05768    unmarks the marked types.  If a field or member function name
05769    appears in an ambiguous way, the IDENTIFIER_CLASS_VALUE of
05770    that name becomes `error_mark_node'.  */
05771 
05772 void
05773 pushclass (type, modify)
05774      tree type;
05775      int modify;
05776 {
05777   type = TYPE_MAIN_VARIANT (type);
05778 
05779   /* Make sure there is enough room for the new entry on the stack.  */
05780   if (current_class_depth + 1 >= current_class_stack_size) 
05781     {
05782       current_class_stack_size *= 2;
05783       current_class_stack
05784   = (class_stack_node_t) xrealloc (current_class_stack,
05785            current_class_stack_size
05786            * sizeof (struct class_stack_node));
05787     }
05788 
05789   /* Insert a new entry on the class stack.  */
05790   current_class_stack[current_class_depth].name = current_class_name;
05791   current_class_stack[current_class_depth].type = current_class_type;
05792   current_class_stack[current_class_depth].access = current_access_specifier;
05793   current_class_stack[current_class_depth].names_used = 0;
05794   current_class_depth++;
05795 
05796   /* Now set up the new type.  */
05797   current_class_name = TYPE_NAME (type);
05798   if (TREE_CODE (current_class_name) == TYPE_DECL)
05799     current_class_name = DECL_NAME (current_class_name);
05800   current_class_type = type;
05801 
05802   /* By default, things in classes are private, while things in
05803      structures or unions are public.  */
05804   current_access_specifier = (CLASSTYPE_DECLARED_CLASS (type) 
05805             ? access_private_node 
05806             : access_public_node);
05807 
05808   if (previous_class_type != NULL_TREE
05809       && (type != previous_class_type 
05810     || !COMPLETE_TYPE_P (previous_class_type))
05811       && current_class_depth == 1)
05812     {
05813       /* Forcibly remove any old class remnants.  */
05814       invalidate_class_lookup_cache ();
05815     }
05816 
05817   /* If we're about to enter a nested class, clear
05818      IDENTIFIER_CLASS_VALUE for the enclosing classes.  */
05819   if (modify && current_class_depth > 1)
05820     clear_identifier_class_values ();
05821 
05822   pushlevel_class ();
05823 
05824   if (modify)
05825     {
05826       if (type != previous_class_type || current_class_depth > 1)
05827   push_class_decls (type);
05828       else
05829   {
05830     tree item;
05831 
05832     /* We are re-entering the same class we just left, so we
05833        don't have to search the whole inheritance matrix to find
05834        all the decls to bind again.  Instead, we install the
05835        cached class_shadowed list, and walk through it binding
05836        names and setting up IDENTIFIER_TYPE_VALUEs.  */
05837     set_class_shadows (previous_class_values);
05838     for (item = previous_class_values; item; item = TREE_CHAIN (item))
05839       {
05840         tree id = TREE_PURPOSE (item);
05841         tree decl = TREE_TYPE (item);
05842 
05843         push_class_binding (id, decl);
05844         if (TREE_CODE (decl) == TYPE_DECL)
05845     set_identifier_type_value (id, TREE_TYPE (decl));
05846       }
05847     unuse_fields (type);
05848   }
05849 
05850       cxx_remember_type_decls (CLASSTYPE_NESTED_UDTS (type));
05851     }
05852 }
05853 
05854 /* When we exit a toplevel class scope, we save the
05855    IDENTIFIER_CLASS_VALUEs so that we can restore them quickly if we
05856    reenter the class.  Here, we've entered some other class, so we
05857    must invalidate our cache.  */
05858 
05859 void
05860 invalidate_class_lookup_cache ()
05861 {
05862   tree t;
05863   
05864   /* The IDENTIFIER_CLASS_VALUEs are no longer valid.  */
05865   for (t = previous_class_values; t; t = TREE_CHAIN (t))
05866     IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (t)) = NULL_TREE;
05867 
05868   previous_class_values = NULL_TREE;
05869   previous_class_type = NULL_TREE;
05870 }
05871  
05872 /* Get out of the current class scope. If we were in a class scope
05873    previously, that is the one popped to.  */
05874 
05875 void
05876 popclass ()
05877 {
05878   poplevel_class ();
05879   pop_class_decls ();
05880 
05881   current_class_depth--;
05882   current_class_name = current_class_stack[current_class_depth].name;
05883   current_class_type = current_class_stack[current_class_depth].type;
05884   current_access_specifier = current_class_stack[current_class_depth].access;
05885   if (current_class_stack[current_class_depth].names_used)
05886     splay_tree_delete (current_class_stack[current_class_depth].names_used);
05887 }
05888 
05889 /* Returns 1 if current_class_type is either T or a nested type of T.
05890    We start looking from 1 because entry 0 is from global scope, and has
05891    no type.  */
05892 
05893 int
05894 currently_open_class (t)
05895      tree t;
05896 {
05897   int i;
05898   if (t == current_class_type)
05899     return 1;
05900   for (i = 1; i < current_class_depth; ++i)
05901     if (current_class_stack [i].type == t)
05902       return 1;
05903   return 0;
05904 }
05905 
05906 /* If either current_class_type or one of its enclosing classes are derived
05907    from T, return the appropriate type.  Used to determine how we found
05908    something via unqualified lookup.  */
05909 
05910 tree
05911 currently_open_derived_class (t)
05912      tree t;
05913 {
05914   int i;
05915 
05916   if (DERIVED_FROM_P (t, current_class_type))
05917     return current_class_type;
05918 
05919   for (i = current_class_depth - 1; i > 0; --i)
05920     if (DERIVED_FROM_P (t, current_class_stack[i].type))
05921       return current_class_stack[i].type;
05922 
05923   return NULL_TREE;
05924 }
05925 
05926 /* When entering a class scope, all enclosing class scopes' names with
05927    static meaning (static variables, static functions, types and enumerators)
05928    have to be visible.  This recursive function calls pushclass for all
05929    enclosing class contexts until global or a local scope is reached.
05930    TYPE is the enclosed class and MODIFY is equivalent with the pushclass
05931    formal of the same name.  */
05932 
05933 void
05934 push_nested_class (type, modify)
05935      tree type;
05936      int modify;
05937 {
05938   tree context;
05939 
05940   /* A namespace might be passed in error cases, like A::B:C.  */
05941   if (type == NULL_TREE 
05942       || type == error_mark_node 
05943       || TREE_CODE (type) == NAMESPACE_DECL
05944       || ! IS_AGGR_TYPE (type)
05945       || TREE_CODE (type) == TEMPLATE_TYPE_PARM
05946       || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
05947     return;
05948   
05949   context = DECL_CONTEXT (TYPE_MAIN_DECL (type));
05950 
05951   if (context && CLASS_TYPE_P (context))
05952     push_nested_class (context, 2);
05953   pushclass (type, modify);
05954 }
05955 
05956 /* Undoes a push_nested_class call.  MODIFY is passed on to popclass.  */
05957 
05958 void
05959 pop_nested_class ()
05960 {
05961   tree context = DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type));
05962 
05963   popclass ();
05964   if (context && CLASS_TYPE_P (context))
05965     pop_nested_class ();
05966 }
05967 
05968 /* Returns the number of extern "LANG" blocks we are nested within.  */
05969 
05970 int
05971 current_lang_depth ()
05972 {
05973   return VARRAY_ACTIVE_SIZE (current_lang_base);
05974 }
05975 
05976 /* Set global variables CURRENT_LANG_NAME to appropriate value
05977    so that behavior of name-mangling machinery is correct.  */
05978 
05979 void
05980 push_lang_context (name)
05981      tree name;
05982 {
05983   VARRAY_PUSH_TREE (current_lang_base, current_lang_name);
05984 
05985   if (name == lang_name_cplusplus)
05986     {
05987       current_lang_name = name;
05988     }
05989   else if (name == lang_name_java)
05990     {
05991       current_lang_name = name;
05992       /* DECL_IGNORED_P is initially set for these types, to avoid clutter.
05993    (See record_builtin_java_type in decl.c.)  However, that causes
05994    incorrect debug entries if these types are actually used.
05995    So we re-enable debug output after extern "Java".  */
05996       DECL_IGNORED_P (TYPE_NAME (java_byte_type_node)) = 0;
05997       DECL_IGNORED_P (TYPE_NAME (java_short_type_node)) = 0;
05998       DECL_IGNORED_P (TYPE_NAME (java_int_type_node)) = 0;
05999       DECL_IGNORED_P (TYPE_NAME (java_long_type_node)) = 0;
06000       DECL_IGNORED_P (TYPE_NAME (java_float_type_node)) = 0;
06001       DECL_IGNORED_P (TYPE_NAME (java_double_type_node)) = 0;
06002       DECL_IGNORED_P (TYPE_NAME (java_char_type_node)) = 0;
06003       DECL_IGNORED_P (TYPE_NAME (java_boolean_type_node)) = 0;
06004     }
06005   else if (name == lang_name_c)
06006     {
06007       current_lang_name = name;
06008     }
06009   else
06010     error ("language string `\"%s\"' not recognized", IDENTIFIER_POINTER (name));
06011 }
06012   
06013 /* Get out of the current language scope.  */
06014 
06015 void
06016 pop_lang_context ()
06017 {
06018   current_lang_name = VARRAY_TOP_TREE (current_lang_base);
06019   VARRAY_POP (current_lang_base);
06020 }
06021 
06022 /* Type instantiation routines.  */
06023 
06024 /* Given an OVERLOAD and a TARGET_TYPE, return the function that
06025    matches the TARGET_TYPE.  If there is no satisfactory match, return
06026    error_mark_node, and issue an error message if COMPLAIN is
06027    nonzero.  Permit pointers to member function if PTRMEM is nonzero.
06028    If TEMPLATE_ONLY, the name of the overloaded function
06029    was a template-id, and EXPLICIT_TARGS are the explicitly provided
06030    template arguments.  */
06031 
06032 static tree
06033 resolve_address_of_overloaded_function (target_type, 
06034           overload,
06035           complain,
06036                                   ptrmem,
06037           template_only,
06038           explicit_targs)
06039      tree target_type;
06040      tree overload;
06041      int complain;
06042      int ptrmem;
06043      int template_only;
06044      tree explicit_targs;
06045 {
06046   /* Here's what the standard says:
06047      
06048        [over.over]
06049 
06050        If the name is a function template, template argument deduction
06051        is done, and if the argument deduction succeeds, the deduced
06052        arguments are used to generate a single template function, which
06053        is added to the set of overloaded functions considered.
06054 
06055        Non-member functions and static member functions match targets of
06056        type "pointer-to-function" or "reference-to-function."  Nonstatic
06057        member functions match targets of type "pointer-to-member
06058        function;" the function type of the pointer to member is used to
06059        select the member function from the set of overloaded member
06060        functions.  If a nonstatic member function is selected, the
06061        reference to the overloaded function name is required to have the
06062        form of a pointer to member as described in 5.3.1.
06063 
06064        If more than one function is selected, any template functions in
06065        the set are eliminated if the set also contains a non-template
06066        function, and any given template function is eliminated if the
06067        set contains a second template function that is more specialized
06068        than the first according to the partial ordering rules 14.5.5.2.
06069        After such eliminations, if any, there shall remain exactly one
06070        selected function.  */
06071 
06072   int is_ptrmem = 0;
06073   int is_reference = 0;
06074   /* We store the matches in a TREE_LIST rooted here.  The functions
06075      are the TREE_PURPOSE, not the TREE_VALUE, in this list, for easy
06076      interoperability with most_specialized_instantiation.  */
06077   tree matches = NULL_TREE;
06078   tree fn;
06079 
06080   /* By the time we get here, we should be seeing only real
06081      pointer-to-member types, not the internal POINTER_TYPE to
06082      METHOD_TYPE representation.  */
06083   my_friendly_assert (!(TREE_CODE (target_type) == POINTER_TYPE
06084       && (TREE_CODE (TREE_TYPE (target_type)) 
06085           == METHOD_TYPE)), 0);
06086 
06087   if (TREE_CODE (overload) == COMPONENT_REF)
06088     overload = TREE_OPERAND (overload, 1);
06089 
06090   /* Check that the TARGET_TYPE is reasonable.  */
06091   if (TYPE_PTRFN_P (target_type))
06092     /* This is OK.  */;
06093   else if (TYPE_PTRMEMFUNC_P (target_type))
06094     /* This is OK, too.  */
06095     is_ptrmem = 1;
06096   else if (TREE_CODE (target_type) == FUNCTION_TYPE)
06097     {
06098       /* This is OK, too.  This comes from a conversion to reference
06099    type.  */
06100       target_type = build_reference_type (target_type);
06101       is_reference = 1;
06102     }
06103   else 
06104     {
06105       if (complain)
06106   error ("\
06107 cannot resolve overloaded function `%D' based on conversion to type `%T'", 
06108       DECL_NAME (OVL_FUNCTION (overload)), target_type);
06109       return error_mark_node;
06110     }
06111   
06112   /* If we can find a non-template function that matches, we can just
06113      use it.  There's no point in generating template instantiations
06114      if we're just going to throw them out anyhow.  But, of course, we
06115      can only do this when we don't *need* a template function.  */
06116   if (!template_only)
06117     {
06118       tree fns;
06119 
06120       for (fns = overload; fns; fns = OVL_CHAIN (fns))
06121   {
06122     tree fn = OVL_FUNCTION (fns);
06123     tree fntype;
06124 
06125     if (TREE_CODE (fn) == TEMPLATE_DECL)
06126       /* We're not looking for templates just yet.  */
06127       continue;
06128 
06129     if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
06130         != is_ptrmem)
06131       /* We're looking for a non-static member, and this isn't
06132          one, or vice versa.  */
06133       continue;
06134   
06135     /* See if there's a match.  */
06136     fntype = TREE_TYPE (fn);
06137     if (is_ptrmem)
06138       fntype = build_ptrmemfunc_type (build_pointer_type (fntype));
06139     else if (!is_reference)
06140       fntype = build_pointer_type (fntype);
06141 
06142     if (can_convert_arg (target_type, fntype, fn))
06143       matches = tree_cons (fn, NULL_TREE, matches);
06144   }
06145     }
06146 
06147   /* Now, if we've already got a match (or matches), there's no need
06148      to proceed to the template functions.  But, if we don't have a
06149      match we need to look at them, too.  */
06150   if (!matches) 
06151     {
06152       tree target_fn_type;
06153       tree target_arg_types;
06154       tree target_ret_type;
06155       tree fns;
06156 
06157       if (is_ptrmem)
06158   target_fn_type
06159     = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (target_type));
06160       else
06161   target_fn_type = TREE_TYPE (target_type);
06162       target_arg_types = TYPE_ARG_TYPES (target_fn_type);
06163       target_ret_type = TREE_TYPE (target_fn_type);
06164 
06165       /* Never do unification on the 'this' parameter.  */
06166       if (TREE_CODE (target_fn_type) == METHOD_TYPE)
06167   target_arg_types = TREE_CHAIN (target_arg_types);
06168     
06169       for (fns = overload; fns; fns = OVL_CHAIN (fns))
06170   {
06171     tree fn = OVL_FUNCTION (fns);
06172     tree instantiation;
06173     tree instantiation_type;
06174     tree targs;
06175 
06176     if (TREE_CODE (fn) != TEMPLATE_DECL)
06177       /* We're only looking for templates.  */
06178       continue;
06179 
06180     if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
06181         != is_ptrmem)
06182       /* We're not looking for a non-static member, and this is
06183          one, or vice versa.  */
06184       continue;
06185 
06186     /* Try to do argument deduction.  */
06187     targs = make_tree_vec (DECL_NTPARMS (fn));
06188     if (fn_type_unification (fn, explicit_targs, targs,
06189            target_arg_types, target_ret_type,
06190            DEDUCE_EXACT, -1) != 0)
06191       /* Argument deduction failed.  */
06192       continue;
06193 
06194     /* Instantiate the template.  */
06195     instantiation = instantiate_template (fn, targs);
06196     if (instantiation == error_mark_node)
06197       /* Instantiation failed.  */
06198       continue;
06199 
06200     /* See if there's a match.  */
06201     instantiation_type = TREE_TYPE (instantiation);
06202     if (is_ptrmem)
06203       instantiation_type = 
06204         build_ptrmemfunc_type (build_pointer_type (instantiation_type));
06205     else if (!is_reference)
06206       instantiation_type = build_pointer_type (instantiation_type);
06207     if (can_convert_arg (target_type, instantiation_type, instantiation))
06208       matches = tree_cons (instantiation, fn, matches);
06209   }
06210 
06211       /* Now, remove all but the most specialized of the matches.  */
06212       if (matches)
06213   {
06214     tree match = most_specialized_instantiation (matches);
06215 
06216     if (match != error_mark_node)
06217       matches = tree_cons (match, NULL_TREE, NULL_TREE);
06218   }
06219     }
06220 
06221   /* Now we should have exactly one function in MATCHES.  */
06222   if (matches == NULL_TREE)
06223     {
06224       /* There were *no* matches.  */
06225       if (complain)
06226   {
06227     error ("no matches converting function `%D' to type `%#T'", 
06228         DECL_NAME (OVL_FUNCTION (overload)),
06229         target_type);
06230 
06231     /* print_candidates expects a chain with the functions in
06232              TREE_VALUE slots, so we cons one up here (we're losing anyway,
06233              so why be clever?).  */
06234           for (; overload; overload = OVL_NEXT (overload))
06235             matches = tree_cons (NULL_TREE, OVL_CURRENT (overload),
06236          matches);
06237           
06238     print_candidates (matches);
06239   }
06240       return error_mark_node;
06241     }
06242   else if (TREE_CHAIN (matches))
06243     {
06244       /* There were too many matches.  */
06245 
06246       if (complain)
06247   {
06248     tree match;
06249 
06250     error ("converting overloaded function `%D' to type `%#T' is ambiguous", 
06251         DECL_NAME (OVL_FUNCTION (overload)),
06252         target_type);
06253 
06254     /* Since print_candidates expects the functions in the
06255        TREE_VALUE slot, we flip them here.  */
06256     for (match = matches; match; match = TREE_CHAIN (match))
06257       TREE_VALUE (match) = TREE_PURPOSE (match);
06258 
06259     print_candidates (matches);
06260   }
06261       
06262       return error_mark_node;
06263     }
06264 
06265   /* Good, exactly one match.  Now, convert it to the correct type.  */
06266   fn = TREE_PURPOSE (matches);
06267 
06268   if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
06269       && !ptrmem && !flag_ms_extensions)
06270     {
06271       static int explained;
06272       
06273       if (!complain)
06274         return error_mark_node;
06275 
06276       pedwarn ("assuming pointer to member `%D'", fn);
06277       if (!explained)
06278         {
06279           pedwarn ("(a pointer to member can only be formed with `&%E')", fn);
06280           explained = 1;
06281         }
06282     }
06283   mark_used (fn);
06284 
06285   if (TYPE_PTRFN_P (target_type) || TYPE_PTRMEMFUNC_P (target_type))
06286     return build_unary_op (ADDR_EXPR, fn, 0);
06287   else
06288     {
06289       /* The target must be a REFERENCE_TYPE.  Above, build_unary_op
06290    will mark the function as addressed, but here we must do it
06291    explicitly.  */
06292       cxx_mark_addressable (fn);
06293 
06294       return fn;
06295     }
06296 }
06297 
06298 /* This function will instantiate the type of the expression given in
06299    RHS to match the type of LHSTYPE.  If errors exist, then return
06300    error_mark_node. FLAGS is a bit mask.  If ITF_COMPLAIN is set, then
06301    we complain on errors.  If we are not complaining, never modify rhs,
06302    as overload resolution wants to try many possible instantiations, in
06303    the hope that at least one will work.
06304    
06305    For non-recursive calls, LHSTYPE should be a function, pointer to
06306    function, or a pointer to member function.  */
06307 
06308 tree
06309 instantiate_type (lhstype, rhs, flags)
06310      tree lhstype, rhs;
06311      tsubst_flags_t flags;
06312 {
06313   int complain = (flags & tf_error);
06314   int strict = (flags & tf_no_attributes)
06315                ? COMPARE_NO_ATTRIBUTES : COMPARE_STRICT;
06316   int allow_ptrmem = flags & tf_ptrmem_ok;
06317   
06318   flags &= ~tf_ptrmem_ok;
06319   
06320   if (TREE_CODE (lhstype) == UNKNOWN_TYPE)
06321     {
06322       if (complain)
06323   error ("not enough type information");
06324       return error_mark_node;
06325     }
06326 
06327   if (TREE_TYPE (rhs) != NULL_TREE && ! (type_unknown_p (rhs)))
06328     {
06329       if (comptypes (lhstype, TREE_TYPE (rhs), strict))
06330   return rhs;
06331       if (complain)
06332   error ("argument of type `%T' does not match `%T'",
06333       TREE_TYPE (rhs), lhstype);
06334       return error_mark_node;
06335     }
06336 
06337   if (TREE_CODE (rhs) == BASELINK)
06338     rhs = BASELINK_FUNCTIONS (rhs);
06339 
06340   /* We don't overwrite rhs if it is an overloaded function.
06341      Copying it would destroy the tree link.  */
06342   if (TREE_CODE (rhs) != OVERLOAD)
06343     rhs = copy_node (rhs);
06344 
06345   /* This should really only be used when attempting to distinguish
06346      what sort of a pointer to function we have.  For now, any
06347      arithmetic operation which is not supported on pointers
06348      is rejected as an error.  */
06349 
06350   switch (TREE_CODE (rhs))
06351     {
06352     case TYPE_EXPR:
06353     case CONVERT_EXPR:
06354     case SAVE_EXPR:
06355     case CONSTRUCTOR:
06356     case BUFFER_REF:
06357       abort ();
06358       return error_mark_node;
06359 
06360     case INDIRECT_REF:
06361     case ARRAY_REF:
06362       {
06363   tree new_rhs;
06364 
06365   new_rhs = instantiate_type (build_pointer_type (lhstype),
06366             TREE_OPERAND (rhs, 0), flags);
06367   if (new_rhs == error_mark_node)
06368     return error_mark_node;
06369 
06370   TREE_TYPE (rhs) = lhstype;
06371   TREE_OPERAND (rhs, 0) = new_rhs;
06372   return rhs;
06373       }
06374 
06375     case NOP_EXPR:
06376       rhs = copy_node (TREE_OPERAND (rhs, 0));
06377       TREE_TYPE (rhs) = unknown_type_node;
06378       return instantiate_type (lhstype, rhs, flags);
06379 
06380     case COMPONENT_REF:
06381       return instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
06382 
06383     case OFFSET_REF:
06384       rhs = TREE_OPERAND (rhs, 1);
06385       if (BASELINK_P (rhs))
06386   return instantiate_type (lhstype, BASELINK_FUNCTIONS (rhs),
06387                            flags | allow_ptrmem);
06388 
06389       /* This can happen if we are forming a pointer-to-member for a
06390    member template.  */
06391       my_friendly_assert (TREE_CODE (rhs) == TEMPLATE_ID_EXPR, 0);
06392 
06393       /* Fall through.  */
06394 
06395     case TEMPLATE_ID_EXPR:
06396       {
06397   tree fns = TREE_OPERAND (rhs, 0);
06398   tree args = TREE_OPERAND (rhs, 1);
06399 
06400   return
06401     resolve_address_of_overloaded_function (lhstype,
06402               fns,
06403               complain,
06404                                             allow_ptrmem,
06405               /*template_only=*/1,
06406               args);
06407       }
06408 
06409     case OVERLOAD:
06410       return 
06411   resolve_address_of_overloaded_function (lhstype, 
06412             rhs,
06413             complain,
06414                                           allow_ptrmem,
06415             /*template_only=*/0,
06416             /*explicit_targs=*/NULL_TREE);
06417 
06418     case TREE_LIST:
06419       /* Now we should have a baselink.  */
06420       my_friendly_assert (BASELINK_P (rhs), 990412);
06421 
06422       return instantiate_type (lhstype, BASELINK_FUNCTIONS (rhs), flags);
06423 
06424     case CALL_EXPR:
06425       /* This is too hard for now.  */
06426       abort ();
06427       return error_mark_node;
06428 
06429     case PLUS_EXPR:
06430     case MINUS_EXPR:
06431     case COMPOUND_EXPR:
06432       TREE_OPERAND (rhs, 0)
06433   = instantiate_type (lhstype, TREE_OPERAND (rhs, 0), flags);
06434       if (TREE_OPERAND (rhs, 0) == error_mark_node)
06435   return error_mark_node;
06436       TREE_OPERAND (rhs, 1)
06437   = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
06438       if (TREE_OPERAND (rhs, 1) == error_mark_node)
06439   return error_mark_node;
06440 
06441       TREE_TYPE (rhs) = lhstype;
06442       return rhs;
06443 
06444     case MULT_EXPR:
06445     case TRUNC_DIV_EXPR:
06446     case FLOOR_DIV_EXPR:
06447     case CEIL_DIV_EXPR:
06448     case ROUND_DIV_EXPR:
06449     case RDIV_EXPR:
06450     case TRUNC_MOD_EXPR:
06451     case FLOOR_MOD_EXPR:
06452     case CEIL_MOD_EXPR:
06453     case ROUND_MOD_EXPR:
06454     case FIX_ROUND_EXPR:
06455     case FIX_FLOOR_EXPR:
06456     case FIX_CEIL_EXPR:
06457     case FIX_TRUNC_EXPR:
06458     case FLOAT_EXPR:
06459     case NEGATE_EXPR:
06460     case ABS_EXPR:
06461     case MAX_EXPR:
06462     case MIN_EXPR:
06463     case FFS_EXPR:
06464 
06465     case BIT_AND_EXPR:
06466     case BIT_IOR_EXPR:
06467     case BIT_XOR_EXPR:
06468     case LSHIFT_EXPR:
06469     case RSHIFT_EXPR:
06470     case LROTATE_EXPR:
06471     case RROTATE_EXPR:
06472 
06473     case PREINCREMENT_EXPR:
06474     case PREDECREMENT_EXPR:
06475     case POSTINCREMENT_EXPR:
06476     case POSTDECREMENT_EXPR:
06477       if (complain)
06478   error ("invalid operation on uninstantiated type");
06479       return error_mark_node;
06480 
06481     case TRUTH_AND_EXPR:
06482     case TRUTH_OR_EXPR:
06483     case TRUTH_XOR_EXPR:
06484     case LT_EXPR:
06485     case LE_EXPR:
06486     case GT_EXPR:
06487     case GE_EXPR:
06488     case EQ_EXPR:
06489     case NE_EXPR:
06490     case TRUTH_ANDIF_EXPR:
06491     case TRUTH_ORIF_EXPR:
06492     case TRUTH_NOT_EXPR:
06493       if (complain)
06494   error ("not enough type information");
06495       return error_mark_node;
06496 
06497     case COND_EXPR:
06498       if (type_unknown_p (TREE_OPERAND (rhs, 0)))
06499   {
06500     if (complain)
06501       error ("not enough type information");
06502     return error_mark_node;
06503   }
06504       TREE_OPERAND (rhs, 1)
06505   = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
06506       if (TREE_OPERAND (rhs, 1) == error_mark_node)
06507   return error_mark_node;
06508       TREE_OPERAND (rhs, 2)
06509   = instantiate_type (lhstype, TREE_OPERAND (rhs, 2), flags);
06510       if (TREE_OPERAND (rhs, 2) == error_mark_node)
06511   return error_mark_node;
06512 
06513       TREE_TYPE (rhs) = lhstype;
06514       return rhs;
06515 
06516     case MODIFY_EXPR:
06517       TREE_OPERAND (rhs, 1)
06518   = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
06519       if (TREE_OPERAND (rhs, 1) == error_mark_node)
06520   return error_mark_node;
06521 
06522       TREE_TYPE (rhs) = lhstype;
06523       return rhs;
06524       
06525     case ADDR_EXPR:
06526     {
06527       if (PTRMEM_OK_P (rhs))
06528         flags |= tf_ptrmem_ok;
06529       
06530       return instantiate_type (lhstype, TREE_OPERAND (rhs, 0), flags);
06531     }
06532     case ENTRY_VALUE_EXPR:
06533       abort ();
06534       return error_mark_node;
06535 
06536     case ERROR_MARK:
06537       return error_mark_node;
06538 
06539     default:
06540       abort ();
06541       return error_mark_node;
06542     }
06543 }
06544 
06545 /* Return the name of the virtual function pointer field
06546    (as an IDENTIFIER_NODE) for the given TYPE.  Note that
06547    this may have to look back through base types to find the
06548    ultimate field name.  (For single inheritance, these could
06549    all be the same name.  Who knows for multiple inheritance).  */
06550 
06551 static tree
06552 get_vfield_name (type)
06553      tree type;
06554 {
06555   tree binfo = TYPE_BINFO (type);
06556   char *buf;
06557 
06558   while (BINFO_BASETYPES (binfo)
06559    && TYPE_CONTAINS_VPTR_P (BINFO_TYPE (BINFO_BASETYPE (binfo, 0)))
06560    && ! TREE_VIA_VIRTUAL (BINFO_BASETYPE (binfo, 0)))
06561     binfo = BINFO_BASETYPE (binfo, 0);
06562 
06563   type = BINFO_TYPE (binfo);
06564   buf = (char *) alloca (sizeof (VFIELD_NAME_FORMAT)
06565        + TYPE_NAME_LENGTH (type) + 2);
06566   sprintf (buf, VFIELD_NAME_FORMAT,
06567      IDENTIFIER_POINTER (constructor_name (type)));
06568   return get_identifier (buf);
06569 }
06570 
06571 void
06572 print_class_statistics ()
06573 {
06574 #ifdef GATHER_STATISTICS
06575   fprintf (stderr, "convert_harshness = %d\n", n_convert_harshness);
06576   fprintf (stderr, "compute_conversion_costs = %d\n", n_compute_conversion_costs);
06577   fprintf (stderr, "build_method_call = %d (inner = %d)\n",
06578      n_build_method_call, n_inner_fields_searched);
06579   if (n_vtables)
06580     {
06581       fprintf (stderr, "vtables = %d; vtable searches = %d\n",
06582          n_vtables, n_vtable_searches);
06583       fprintf (stderr, "vtable entries = %d; vtable elems = %d\n",
06584          n_vtable_entries, n_vtable_elems);
06585     }
06586 #endif
06587 }
06588 
06589 /* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
06590    according to [class]:
06591                                           The class-name is also inserted
06592    into  the scope of the class itself.  For purposes of access checking,
06593    the inserted class name is treated as if it were a public member name.  */
06594 
06595 void
06596 build_self_reference ()
06597 {
06598   tree name = constructor_name (current_class_type);
06599   tree value = build_lang_decl (TYPE_DECL, name, current_class_type);
06600   tree saved_cas;
06601 
06602   DECL_NONLOCAL (value) = 1;
06603   DECL_CONTEXT (value) = current_class_type;
06604   DECL_ARTIFICIAL (value) = 1;
06605   SET_DECL_SELF_REFERENCE_P (value);
06606 
06607   if (processing_template_decl)
06608     value = push_template_decl (value);
06609 
06610   saved_cas = current_access_specifier;
06611   current_access_specifier = access_public_node;
06612   finish_member_declaration (value);
06613   current_access_specifier = saved_cas;
06614 }
06615 
06616 /* Returns 1 if TYPE contains only padding bytes.  */
06617 
06618 int
06619 is_empty_class (type)
06620      tree type;
06621 {
06622   if (type == error_mark_node)
06623     return 0;
06624 
06625   if (! IS_AGGR_TYPE (type))
06626     return 0;
06627 
06628   /* In G++ 3.2, whether or not a class was empty was determined by
06629      looking at its size.  */
06630   if (abi_version_at_least (2))
06631     return CLASSTYPE_EMPTY_P (type);
06632   else
06633     return integer_zerop (CLASSTYPE_SIZE (type));
06634 }
06635 
06636 /* Returns true if TYPE contains an empty class.  */
06637 
06638 static bool
06639 contains_empty_class_p (tree type)
06640 {
06641   if (is_empty_class (type))
06642     return true;
06643   if (CLASS_TYPE_P (type))
06644     {
06645       tree field;
06646       int i;
06647 
06648       for (i = 0; i < CLASSTYPE_N_BASECLASSES (type); ++i)
06649   if (contains_empty_class_p (TYPE_BINFO_BASETYPE (type, i)))
06650     return true;
06651       for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
06652   if (TREE_CODE (field) == FIELD_DECL
06653       && !DECL_ARTIFICIAL (field)
06654       && is_empty_class (TREE_TYPE (field)))
06655     return true;
06656     }
06657   else if (TREE_CODE (type) == ARRAY_TYPE)
06658     return contains_empty_class_p (TREE_TYPE (type));
06659   return false;
06660 }
06661 
06662 /* Find the enclosing class of the given NODE.  NODE can be a *_DECL or
06663    a *_TYPE node.  NODE can also be a local class.  */
06664 
06665 tree
06666 get_enclosing_class (type)
06667      tree type;
06668 {
06669   tree node = type;
06670 
06671   while (node && TREE_CODE (node) != NAMESPACE_DECL)
06672     {
06673       switch (TREE_CODE_CLASS (TREE_CODE (node)))
06674   {
06675   case 'd':
06676     node = DECL_CONTEXT (node);
06677     break;
06678 
06679   case 't':
06680     if (node != type)
06681       return node;
06682     node = TYPE_CONTEXT (node);
06683     break;
06684 
06685   default:
06686     abort ();
06687   }
06688     }
06689   return NULL_TREE;
06690 }
06691 
06692 /* Return 1 if TYPE or one of its enclosing classes is derived from BASE.  */
06693 
06694 int
06695 is_base_of_enclosing_class (base, type)
06696      tree base, type;
06697 {
06698   while (type)
06699     {
06700       if (lookup_base (type, base, ba_any, NULL))
06701   return 1;
06702 
06703       type = get_enclosing_class (type);
06704     }
06705   return 0;
06706 }
06707 
06708 /* Note that NAME was looked up while the current class was being
06709    defined and that the result of that lookup was DECL.  */
06710 
06711 void
06712 maybe_note_name_used_in_class (name, decl)
06713      tree name;
06714      tree decl;
06715 {
06716   splay_tree names_used;
06717 
06718   /* If we're not defining a class, there's nothing to do.  */
06719   if (!innermost_scope_is_class_p ())
06720     return;
06721   
06722   /* If there's already a binding for this NAME, then we don't have
06723      anything to worry about.  */
06724   if (IDENTIFIER_CLASS_VALUE (name))
06725     return;
06726 
06727   if (!current_class_stack[current_class_depth - 1].names_used)
06728     current_class_stack[current_class_depth - 1].names_used
06729       = splay_tree_new (splay_tree_compare_pointers, 0, 0);
06730   names_used = current_class_stack[current_class_depth - 1].names_used;
06731 
06732   splay_tree_insert (names_used,
06733          (splay_tree_key) name, 
06734          (splay_tree_value) decl);
06735 }
06736 
06737 /* Note that NAME was declared (as DECL) in the current class.  Check
06738    to see that the declaration is valid.  */
06739 
06740 void
06741 note_name_declared_in_class (name, decl)
06742      tree name;
06743      tree decl;
06744 {
06745   splay_tree names_used;
06746   splay_tree_node n;
06747 
06748   /* Look to see if we ever used this name.  */
06749   names_used 
06750     = current_class_stack[current_class_depth - 1].names_used;
06751   if (!names_used)
06752     return;
06753 
06754   n = splay_tree_lookup (names_used, (splay_tree_key) name);
06755   if (n)
06756     {
06757       /* [basic.scope.class]
06758    
06759    A name N used in a class S shall refer to the same declaration
06760    in its context and when re-evaluated in the completed scope of
06761    S.  */
06762       error ("declaration of `%#D'", decl);
06763       cp_error_at ("changes meaning of `%D' from `%+#D'", 
06764        DECL_NAME (OVL_CURRENT (decl)),
06765        (tree) n->value);
06766     }
06767 }
06768 
06769 /* Returns the VAR_DECL for the complete vtable associated with BINFO.
06770    Secondary vtables are merged with primary vtables; this function
06771    will return the VAR_DECL for the primary vtable.  */
06772 
06773 tree
06774 get_vtbl_decl_for_binfo (binfo)
06775      tree binfo;
06776 {
06777   tree decl;
06778 
06779   decl = BINFO_VTABLE (binfo);
06780   if (decl && TREE_CODE (decl) == PLUS_EXPR)
06781     {
06782       my_friendly_assert (TREE_CODE (TREE_OPERAND (decl, 0)) == ADDR_EXPR,
06783         2000403);
06784       decl = TREE_OPERAND (TREE_OPERAND (decl, 0), 0);
06785     }
06786   if (decl)
06787     my_friendly_assert (TREE_CODE (decl) == VAR_DECL, 20000403);
06788   return decl;
06789 }
06790 
06791 /* Called from get_primary_binfo via dfs_walk.  DATA is a TREE_LIST
06792    who's TREE_PURPOSE is the TYPE of the required primary base and
06793    who's TREE_VALUE is a list of candidate binfos that we fill in.  */
06794 
06795 static tree
06796 dfs_get_primary_binfo (binfo, data)
06797      tree binfo;
06798      void *data;
06799 {
06800   tree cons = (tree) data;
06801   tree primary_base = TREE_PURPOSE (cons);
06802 
06803   if (TREE_VIA_VIRTUAL (binfo) 
06804       && same_type_p (BINFO_TYPE (binfo), primary_base))
06805     /* This is the right type of binfo, but it might be an unshared
06806        instance, and the shared instance is later in the dfs walk.  We
06807        must keep looking.  */
06808     TREE_VALUE (cons) = tree_cons (NULL, binfo, TREE_VALUE (cons));
06809   
06810   return NULL_TREE;
06811 }
06812 
06813 /* Returns the unshared binfo for the primary base of BINFO.  Note
06814    that in a complex hierarchy the resulting BINFO may not actually
06815    *be* primary.  In particular if the resulting BINFO is a virtual
06816    base, and it occurs elsewhere in the hierarchy, then this
06817    occurrence may not actually be a primary base in the complete
06818    object.  Check BINFO_PRIMARY_P to be sure.  */
06819 
06820 tree
06821 get_primary_binfo (binfo)
06822      tree binfo;
06823 {
06824   tree primary_base;
06825   tree result = NULL_TREE;
06826   tree virtuals;
06827   
06828   primary_base = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (binfo));
06829   if (!primary_base)
06830     return NULL_TREE;
06831 
06832   /* A non-virtual primary base is always a direct base, and easy to
06833      find.  */
06834   if (!TREE_VIA_VIRTUAL (primary_base))
06835     {
06836       int i;
06837 
06838       /* Scan the direct basetypes until we find a base with the same
06839    type as the primary base.  */
06840       for (i = 0; i < BINFO_N_BASETYPES (binfo); ++i)
06841   {
06842     tree base_binfo = BINFO_BASETYPE (binfo, i);
06843     
06844     if (same_type_p (BINFO_TYPE (base_binfo),
06845          BINFO_TYPE (primary_base)))
06846       return base_binfo;
06847   }
06848 
06849       /* We should always find the primary base.  */
06850       abort ();
06851     }
06852 
06853   /* For a primary virtual base, we have to scan the entire hierarchy
06854      rooted at BINFO; the virtual base could be an indirect virtual
06855      base.  There could be more than one instance of the primary base
06856      in the hierarchy, and if one is the canonical binfo we want that
06857      one.  If it exists, it should be the first one we find, but as a
06858      consistency check we find them all and make sure.  */
06859   virtuals = build_tree_list (BINFO_TYPE (primary_base), NULL_TREE);
06860   dfs_walk (binfo, dfs_get_primary_binfo, NULL, virtuals);
06861   virtuals = TREE_VALUE (virtuals);
06862   
06863   /* We must have found at least one instance.  */
06864   my_friendly_assert (virtuals, 20010612);
06865 
06866   if (TREE_CHAIN (virtuals))
06867     {
06868       /* We found more than one instance of the base. We must make
06869          sure that, if one is the canonical one, it is the first one
06870          we found. As the chain is in reverse dfs order, that means
06871          the last on the list.  */
06872       tree complete_binfo;
06873       tree canonical;
06874       
06875       for (complete_binfo = binfo;
06876      BINFO_INHERITANCE_CHAIN (complete_binfo);
06877      complete_binfo = BINFO_INHERITANCE_CHAIN (complete_binfo))
06878   continue;
06879       canonical = binfo_for_vbase (BINFO_TYPE (primary_base),
06880            BINFO_TYPE (complete_binfo));
06881       
06882       for (; virtuals; virtuals = TREE_CHAIN (virtuals))
06883   {
06884     result = TREE_VALUE (virtuals);
06885 
06886     if (canonical == result)
06887       {
06888         /* This is the unshared instance. Make sure it was the
06889      first one found.  */
06890         my_friendly_assert (!TREE_CHAIN (virtuals), 20010612);
06891         break;
06892       }
06893   }
06894     }
06895   else
06896     result = TREE_VALUE (virtuals);
06897   return result;
06898 }
06899 
06900 /* If INDENTED_P is zero, indent to INDENT. Return nonzero.  */
06901 
06902 static int
06903 maybe_indent_hierarchy (stream, indent, indented_p)
06904      FILE *stream;
06905      int indent;
06906      int indented_p;
06907 {
06908   if (!indented_p)
06909     fprintf (stream, "%*s", indent, "");
06910   return 1;
06911 }
06912 
06913 /* Dump the offsets of all the bases rooted at BINFO (in the hierarchy
06914    dominated by T) to stderr.  INDENT should be zero when called from
06915    the top level; it is incremented recursively.  */
06916 
06917 static void
06918 dump_class_hierarchy_r (stream, flags, t, binfo, indent)
06919      FILE *stream;
06920      int flags;
06921      tree t;
06922      tree binfo;
06923      int indent;
06924 {
06925   int i;
06926   int indented = 0;
06927   
06928   indented = maybe_indent_hierarchy (stream, indent, 0);
06929   fprintf (stream, "%s (0x%lx) ",
06930      type_as_string (binfo, TFF_PLAIN_IDENTIFIER),
06931      (unsigned long) binfo);
06932   fprintf (stream, HOST_WIDE_INT_PRINT_DEC,
06933      tree_low_cst (BINFO_OFFSET (binfo), 0));
06934   if (is_empty_class (BINFO_TYPE (binfo)))
06935     fprintf (stream, " empty");
06936   else if (CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (binfo)))
06937     fprintf (stream, " nearly-empty");
06938   if (TREE_VIA_VIRTUAL (binfo))
06939     {
06940       tree canonical = binfo_for_vbase (BINFO_TYPE (binfo), t);
06941 
06942       fprintf (stream, " virtual");
06943       if (canonical == binfo)
06944         fprintf (stream, " canonical");
06945       else
06946         fprintf (stream, " non-canonical");
06947     }
06948   fprintf (stream, "\n");
06949 
06950   indented = 0;
06951   if (BINFO_PRIMARY_BASE_OF (binfo))
06952     {
06953       indented = maybe_indent_hierarchy (stream, indent + 3, indented);
06954       fprintf (stream, " primary-for %s (0x%lx)",
06955          type_as_string (BINFO_PRIMARY_BASE_OF (binfo),
06956              TFF_PLAIN_IDENTIFIER),
06957          (unsigned long)BINFO_PRIMARY_BASE_OF (binfo));
06958     }
06959   if (BINFO_LOST_PRIMARY_P (binfo))
06960     {
06961       indented = maybe_indent_hierarchy (stream, indent + 3, indented);
06962       fprintf (stream, " lost-primary");
06963     }
06964   if (indented)
06965     fprintf (stream, "\n");
06966 
06967   if (!(flags & TDF_SLIM))
06968     {
06969       int indented = 0;
06970       
06971       if (BINFO_SUBVTT_INDEX (binfo))
06972   {
06973     indented = maybe_indent_hierarchy (stream, indent + 3, indented);
06974     fprintf (stream, " subvttidx=%s",
06975        expr_as_string (BINFO_SUBVTT_INDEX (binfo),
06976            TFF_PLAIN_IDENTIFIER));
06977   }
06978       if (BINFO_VPTR_INDEX (binfo))
06979   {
06980     indented = maybe_indent_hierarchy (stream, indent + 3, indented);
06981     fprintf (stream, " vptridx=%s",
06982        expr_as_string (BINFO_VPTR_INDEX (binfo),
06983            TFF_PLAIN_IDENTIFIER));
06984   }
06985       if (BINFO_VPTR_FIELD (binfo))
06986   {
06987     indented = maybe_indent_hierarchy (stream, indent + 3, indented);
06988     fprintf (stream, " vbaseoffset=%s",
06989        expr_as_string (BINFO_VPTR_FIELD (binfo),
06990            TFF_PLAIN_IDENTIFIER));
06991   }
06992       if (BINFO_VTABLE (binfo))
06993   {
06994     indented = maybe_indent_hierarchy (stream, indent + 3, indented);
06995     fprintf (stream, " vptr=%s",
06996        expr_as_string (BINFO_VTABLE (binfo),
06997            TFF_PLAIN_IDENTIFIER));
06998   }
06999       
07000       if (indented)
07001   fprintf (stream, "\n");
07002     }
07003   
07004 
07005   for (i = 0; i < BINFO_N_BASETYPES (binfo); ++i)
07006     dump_class_hierarchy_r (stream, flags,
07007           t, BINFO_BASETYPE (binfo, i),
07008           indent + 2);
07009 }
07010 
07011 /* Dump the BINFO hierarchy for T.  */
07012 
07013 static void
07014 dump_class_hierarchy (t)
07015      tree t;
07016 {
07017   int flags;
07018   FILE *stream = dump_begin (TDI_class, &flags);
07019 
07020   if (!stream)
07021     return;
07022   
07023   fprintf (stream, "Class %s\n", type_as_string (t, TFF_PLAIN_IDENTIFIER));
07024   fprintf (stream, "   size=%lu align=%lu\n",
07025      (unsigned long)(tree_low_cst (TYPE_SIZE (t), 0) / BITS_PER_UNIT),
07026      (unsigned long)(TYPE_ALIGN (t) / BITS_PER_UNIT));
07027   dump_class_hierarchy_r (stream, flags, t, TYPE_BINFO (t), 0);
07028   fprintf (stream, "\n");
07029   dump_end (TDI_class, stream);
07030 }
07031 
07032 static void
07033 dump_array (stream, decl)
07034      FILE *stream;
07035      tree decl;
07036 {
07037   tree inits;
07038   int ix;
07039   HOST_WIDE_INT elt;
07040   tree size = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (decl)));
07041 
07042   elt = (tree_low_cst (TYPE_SIZE (TREE_TYPE (TREE_TYPE (decl))), 0)
07043    / BITS_PER_UNIT);
07044   fprintf (stream, "%s:", decl_as_string (decl, TFF_PLAIN_IDENTIFIER));
07045   fprintf (stream, " %s entries",
07046      expr_as_string (size_binop (PLUS_EXPR, size, size_one_node),
07047          TFF_PLAIN_IDENTIFIER));
07048   fprintf (stream, "\n");
07049 
07050   for (ix = 0, inits = TREE_OPERAND (DECL_INITIAL (decl), 1);
07051        inits; ix++, inits = TREE_CHAIN (inits))
07052     fprintf (stream, "%-4ld  %s\n", (long)(ix * elt),
07053        expr_as_string (TREE_VALUE (inits), TFF_PLAIN_IDENTIFIER));
07054 }
07055 
07056 static void
07057 dump_vtable (t, binfo, vtable)
07058      tree t;
07059      tree binfo;
07060      tree vtable;
07061 {
07062   int flags;
07063   FILE *stream = dump_begin (TDI_class, &flags);
07064 
07065   if (!stream)
07066     return;
07067 
07068   if (!(flags & TDF_SLIM))
07069     {
07070       int ctor_vtbl_p = TYPE_BINFO (t) != binfo;
07071       
07072       fprintf (stream, "%s for %s",
07073          ctor_vtbl_p ? "Construction vtable" : "Vtable",
07074          type_as_string (binfo, TFF_PLAIN_IDENTIFIER));
07075       if (ctor_vtbl_p)
07076   {
07077     if (!TREE_VIA_VIRTUAL (binfo))
07078       fprintf (stream, " (0x%lx instance)", (unsigned long)binfo);
07079     fprintf (stream, " in %s", type_as_string (t, TFF_PLAIN_IDENTIFIER));
07080   }
07081       fprintf (stream, "\n");
07082       dump_array (stream, vtable);
07083       fprintf (stream, "\n");
07084     }
07085   
07086   dump_end (TDI_class, stream);
07087 }
07088 
07089 static void
07090 dump_vtt (t, vtt)
07091      tree t;
07092      tree vtt;
07093 {
07094   int flags;
07095   FILE *stream = dump_begin (TDI_class, &flags);
07096 
07097   if (!stream)
07098     return;
07099 
07100   if (!(flags & TDF_SLIM))
07101     {
07102       fprintf (stream, "VTT for %s\n",
07103          type_as_string (t, TFF_PLAIN_IDENTIFIER));
07104       dump_array (stream, vtt);
07105       fprintf (stream, "\n");
07106     }
07107   
07108   dump_end (TDI_class, stream);
07109 }
07110 
07111 /* Virtual function table initialization.  */
07112 
07113 /* Create all the necessary vtables for T and its base classes.  */
07114 
07115 static void
07116 finish_vtbls (t)
07117      tree t;
07118 {
07119   tree list;
07120   tree vbase;
07121   int i;
07122 
07123   /* We lay out the primary and secondary vtables in one contiguous
07124      vtable.  The primary vtable is first, followed by the non-virtual
07125      secondary vtables in inheritance graph order.  */
07126   list = build_tree_list (TYPE_BINFO_VTABLE (t), NULL_TREE);
07127   accumulate_vtbl_inits (TYPE_BINFO (t), TYPE_BINFO (t),
07128        TYPE_BINFO (t), t, list);
07129   
07130   /* Then come the virtual bases, also in inheritance graph order.  */
07131   for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
07132     {
07133       tree real_base;
07134     
07135       if (!TREE_VIA_VIRTUAL (vbase))
07136   continue;
07137           
07138       /* Although we walk in inheritance order, that might not get the
07139          canonical base.  */
07140       real_base = binfo_for_vbase (BINFO_TYPE (vbase), t);
07141           
07142       accumulate_vtbl_inits (real_base, real_base,
07143            TYPE_BINFO (t), t, list);
07144     }
07145 
07146   /* Fill in BINFO_VPTR_FIELD in the immediate binfos for our virtual
07147      base classes, for the benefit of the debugging backends.  */
07148   for (i = 0; i < BINFO_N_BASETYPES (TYPE_BINFO (t)); ++i)
07149     {
07150       tree base = BINFO_BASETYPE (TYPE_BINFO (t), i);
07151       if (TREE_VIA_VIRTUAL (base))
07152   {
07153     vbase = binfo_for_vbase (BINFO_TYPE (base), t);
07154     BINFO_VPTR_FIELD (base) = BINFO_VPTR_FIELD (vbase);
07155   }
07156     }
07157 
07158   if (TYPE_BINFO_VTABLE (t))
07159     initialize_vtable (TYPE_BINFO (t), TREE_VALUE (list));
07160 }
07161 
07162 /* Initialize the vtable for BINFO with the INITS.  */
07163 
07164 static void
07165 initialize_vtable (binfo, inits)
07166      tree binfo;
07167      tree inits;
07168 {
07169   tree decl;
07170 
07171   layout_vtable_decl (binfo, list_length (inits));
07172   decl = get_vtbl_decl_for_binfo (binfo);
07173   initialize_array (decl, inits);
07174   dump_vtable (BINFO_TYPE (binfo), binfo, decl);
07175 }
07176 
07177 /* Initialize DECL (a declaration for a namespace-scope array) with
07178    the INITS.  */
07179 
07180 static void
07181 initialize_array (decl, inits)
07182   tree decl;
07183   tree inits;
07184 {
07185   tree context;
07186 
07187   context = DECL_CONTEXT (decl);
07188   DECL_CONTEXT (decl) = NULL_TREE;
07189   DECL_INITIAL (decl) = build_nt (CONSTRUCTOR, NULL_TREE, inits);
07190   TREE_HAS_CONSTRUCTOR (DECL_INITIAL (decl)) = 1;
07191   cp_finish_decl (decl, DECL_INITIAL (decl), NULL_TREE, 0);
07192   DECL_CONTEXT (decl) = context;
07193 }
07194 
07195 /* Build the VTT (virtual table table) for T.
07196    A class requires a VTT if it has virtual bases.
07197    
07198    This holds
07199    1 - primary virtual pointer for complete object T
07200    2 - secondary VTTs for each direct non-virtual base of T which requires a
07201        VTT
07202    3 - secondary virtual pointers for each direct or indirect base of T which
07203        has virtual bases or is reachable via a virtual path from T.
07204    4 - secondary VTTs for each direct or indirect virtual base of T.
07205    
07206    Secondary VTTs look like complete object VTTs without part 4.  */
07207 
07208 static void
07209 build_vtt (t)
07210      tree t;
07211 {
07212   tree inits;
07213   tree type;
07214   tree vtt;
07215   tree index;
07216 
07217   /* Build up the initializers for the VTT.  */
07218   inits = NULL_TREE;
07219   index = size_zero_node;
07220   build_vtt_inits (TYPE_BINFO (t), t, &inits, &index);
07221 
07222   /* If we didn't need a VTT, we're done.  */
07223   if (!inits)
07224     return;
07225 
07226   /* Figure out the type of the VTT.  */
07227   type = build_index_type (size_int (list_length (inits) - 1));
07228   type = build_cplus_array_type (const_ptr_type_node, type);
07229          
07230   /* Now, build the VTT object itself.  */
07231   vtt = build_vtable (t, get_vtt_name (t), type);
07232   initialize_array (vtt, inits);
07233   /* Add the VTT to the vtables list.  */
07234   TREE_CHAIN (vtt) = TREE_CHAIN (CLASSTYPE_VTABLES (t));
07235   TREE_CHAIN (CLASSTYPE_VTABLES (t)) = vtt;
07236 
07237   dump_vtt (t, vtt);
07238 }
07239 
07240 /* The type corresponding to BASE_BINFO is a base of the type of BINFO, but
07241    from within some hierarchy which is inherited from the type of BINFO.
07242    Return BASE_BINFO's equivalent binfo from the hierarchy dominated by
07243    BINFO.  */
07244 
07245 static tree
07246 get_original_base (base_binfo, binfo)
07247      tree base_binfo;
07248      tree binfo;
07249 {
07250   tree derived;
07251   int ix;
07252   
07253   if (same_type_p (BINFO_TYPE (base_binfo), BINFO_TYPE (binfo)))
07254     return binfo;
07255   if (TREE_VIA_VIRTUAL (base_binfo))
07256     return binfo_for_vbase (BINFO_TYPE (base_binfo), BINFO_TYPE (binfo));
07257   derived = get_original_base (BINFO_INHERITANCE_CHAIN (base_binfo), binfo);
07258   
07259   for (ix = 0; ix != BINFO_N_BASETYPES (derived); ix++)
07260     if (same_type_p (BINFO_TYPE (base_binfo),
07261                      BINFO_TYPE (BINFO_BASETYPE (derived, ix))))
07262       return BINFO_BASETYPE (derived, ix);
07263   abort ();
07264   return NULL;
07265 }
07266 
07267 /* When building a secondary VTT, BINFO_VTABLE is set to a TREE_LIST with
07268    PURPOSE the RTTI_BINFO, VALUE the real vtable pointer for this binfo,
07269    and CHAIN the vtable pointer for this binfo after construction is
07270    complete.  VALUE can also be another BINFO, in which case we recurse.  */
07271 
07272 static tree
07273 binfo_ctor_vtable (binfo)
07274      tree binfo;
07275 {
07276   tree vt;
07277 
07278   while (1)
07279     {
07280       vt = BINFO_VTABLE (binfo);
07281       if (TREE_CODE (vt) == TREE_LIST)
07282   vt = TREE_VALUE (vt);
07283       if (TREE_CODE (vt) == TREE_VEC)
07284   binfo = vt;
07285       else
07286   break;
07287     }
07288 
07289   return vt;
07290 }
07291 
07292 /* Recursively build the VTT-initializer for BINFO (which is in the
07293    hierarchy dominated by T).  INITS points to the end of the initializer
07294    list to date.  INDEX is the VTT index where the next element will be
07295    replaced.  Iff BINFO is the binfo for T, this is the top level VTT (i.e.
07296    not a subvtt for some base of T).  When that is so, we emit the sub-VTTs
07297    for virtual bases of T. When it is not so, we build the constructor
07298    vtables for the BINFO-in-T variant.  */
07299 
07300 static tree *
07301 build_vtt_inits (binfo, t, inits, index)
07302      tree binfo;
07303      tree t;
07304      tree *inits;
07305      tree *index;
07306 {
07307   int i;
07308   tree b;
07309   tree init;
07310   tree secondary_vptrs;
07311   int top_level_p = same_type_p (TREE_TYPE (binfo), t);
07312 
07313   /* We only need VTTs for subobjects with virtual bases.  */
07314   if (!TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo)))
07315     return inits;
07316 
07317   /* We need to use a construction vtable if this is not the primary
07318      VTT.  */
07319   if (!top_level_p)
07320     {
07321       build_ctor_vtbl_group (binfo, t);
07322 
07323       /* Record the offset in the VTT where this sub-VTT can be found.  */
07324       BINFO_SUBVTT_INDEX (binfo) = *index;
07325     }
07326 
07327   /* Add the address of the primary vtable for the complete object.  */
07328   init = binfo_ctor_vtable (binfo);
07329   *inits = build_tree_list (NULL_TREE, init);
07330   inits = &TREE_CHAIN (*inits);
07331   if (top_level_p)
07332     {
07333       my_friendly_assert (!BINFO_VPTR_INDEX (binfo), 20010129);
07334       BINFO_VPTR_INDEX (binfo) = *index;
07335     }
07336   *index = size_binop (PLUS_EXPR, *index, TYPE_SIZE_UNIT (ptr_type_node));
07337            
07338   /* Recursively add the secondary VTTs for non-virtual bases.  */
07339   for (i = 0; i < BINFO_N_BASETYPES (binfo); ++i)
07340     {
07341       b = BINFO_BASETYPE (binfo, i);
07342       if (!TREE_VIA_VIRTUAL (b))
07343   inits = build_vtt_inits (BINFO_BASETYPE (binfo, i), t, 
07344          inits, index);
07345     }
07346       
07347   /* Add secondary virtual pointers for all subobjects of BINFO with
07348      either virtual bases or reachable along a virtual path, except
07349      subobjects that are non-virtual primary bases.  */
07350   secondary_vptrs = tree_cons (t, NULL_TREE, BINFO_TYPE (binfo));
07351   TREE_TYPE (secondary_vptrs) = *index;
07352   VTT_TOP_LEVEL_P (secondary_vptrs) = top_level_p;
07353   VTT_MARKED_BINFO_P (secondary_vptrs) = 0;
07354   
07355   dfs_walk_real (binfo,
07356      dfs_build_secondary_vptr_vtt_inits,
07357      NULL,
07358            dfs_ctor_vtable_bases_queue_p,
07359      secondary_vptrs);
07360   VTT_MARKED_BINFO_P (secondary_vptrs) = 1;
07361   dfs_walk (binfo, dfs_unmark, dfs_ctor_vtable_bases_queue_p,
07362             secondary_vptrs);
07363 
07364   *index = TREE_TYPE (secondary_vptrs);
07365 
07366   /* The secondary vptrs come back in reverse order.  After we reverse
07367      them, and add the INITS, the last init will be the first element
07368      of the chain.  */
07369   secondary_vptrs = TREE_VALUE (secondary_vptrs);
07370   if (secondary_vptrs)
07371     {
07372       *inits = nreverse (secondary_vptrs);
07373       inits = &TREE_CHAIN (secondary_vptrs);
07374       my_friendly_assert (*inits == NULL_TREE, 20000517);
07375     }
07376 
07377   /* Add the secondary VTTs for virtual bases.  */
07378   if (top_level_p)
07379     for (b = TYPE_BINFO (BINFO_TYPE (binfo)); b; b = TREE_CHAIN (b))
07380       {
07381   tree vbase;
07382   
07383   if (!TREE_VIA_VIRTUAL (b))
07384     continue;
07385   
07386   vbase = binfo_for_vbase (BINFO_TYPE (b), t);
07387   inits = build_vtt_inits (vbase, t, inits, index);
07388       }
07389 
07390   if (!top_level_p)
07391     {
07392       tree data = tree_cons (t, binfo, NULL_TREE);
07393       VTT_TOP_LEVEL_P (data) = 0;
07394       VTT_MARKED_BINFO_P (data) = 0;
07395       
07396       dfs_walk (binfo, dfs_fixup_binfo_vtbls,
07397           dfs_ctor_vtable_bases_queue_p,
07398           data);
07399     }
07400 
07401   return inits;
07402 }
07403 
07404 /* Called from build_vtt_inits via dfs_walk.  BINFO is the binfo
07405    for the base in most derived. DATA is a TREE_LIST who's
07406    TREE_CHAIN is the type of the base being
07407    constructed whilst this secondary vptr is live.  The TREE_UNSIGNED
07408    flag of DATA indicates that this is a constructor vtable.  The
07409    TREE_TOP_LEVEL flag indicates that this is the primary VTT.  */
07410 
07411 static tree
07412 dfs_build_secondary_vptr_vtt_inits (binfo, data)
07413      tree binfo;
07414      void *data;
07415 {
07416   tree l; 
07417   tree t;
07418   tree init;
07419   tree index;
07420   int top_level_p;
07421 
07422   l = (tree) data;
07423   t = TREE_CHAIN (l);
07424   top_level_p = VTT_TOP_LEVEL_P (l);
07425   
07426   SET_BINFO_MARKED (binfo);
07427 
07428   /* We don't care about bases that don't have vtables.  */
07429   if (!TYPE_VFIELD (BINFO_TYPE (binfo)))
07430     return NULL_TREE;
07431 
07432   /* We're only interested in proper subobjects of T.  */
07433   if (same_type_p (BINFO_TYPE (binfo), t))
07434     return NULL_TREE;
07435 
07436   /* We're not interested in non-virtual primary bases.  */
07437   if (!TREE_VIA_VIRTUAL (binfo) && BINFO_PRIMARY_P (binfo))
07438     return NULL_TREE;
07439 
07440   /* If BINFO has virtual bases or is reachable via a virtual path
07441      from T, it'll have a secondary vptr.  */
07442   if (!TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo))
07443       && !binfo_via_virtual (binfo, t))
07444     return NULL_TREE;
07445 
07446   /* Record the index where this secondary vptr can be found.  */
07447   index = TREE_TYPE (l);
07448   if (top_level_p)
07449     {
07450       my_friendly_assert (!BINFO_VPTR_INDEX (binfo), 20010129);
07451       BINFO_VPTR_INDEX (binfo) = index;
07452     }
07453   TREE_TYPE (l) = size_binop (PLUS_EXPR, index, 
07454             TYPE_SIZE_UNIT (ptr_type_node));
07455 
07456   /* Add the initializer for the secondary vptr itself.  */
07457   if (top_level_p && TREE_VIA_VIRTUAL (binfo))
07458     {
07459       /* It's a primary virtual base, and this is not the construction
07460          vtable. Find the base this is primary of in the inheritance graph,
07461          and use that base's vtable now.  */
07462       while (BINFO_PRIMARY_BASE_OF (binfo))
07463         binfo = BINFO_PRIMARY_BASE_OF (binfo);
07464     }
07465   init = binfo_ctor_vtable (binfo);
07466   TREE_VALUE (l) = tree_cons (NULL_TREE, init, TREE_VALUE (l));
07467 
07468   return NULL_TREE;
07469 }
07470 
07471 /* dfs_walk_real predicate for building vtables. DATA is a TREE_LIST,
07472    VTT_MARKED_BINFO_P indicates whether marked or unmarked bases
07473    should be walked.  TREE_PURPOSE is the TREE_TYPE that dominates the
07474    hierarchy.  */
07475 
07476 static tree
07477 dfs_ctor_vtable_bases_queue_p (binfo, data)
07478      tree binfo;
07479      void *data;
07480 {
07481   if (TREE_VIA_VIRTUAL (binfo))
07482      /* Get the shared version.  */
07483     binfo = binfo_for_vbase (BINFO_TYPE (binfo), TREE_PURPOSE ((tree) data));
07484 
07485   if (!BINFO_MARKED (binfo) == VTT_MARKED_BINFO_P ((tree) data))
07486     return NULL_TREE;
07487   return binfo;
07488 }
07489 
07490 /* Called from build_vtt_inits via dfs_walk. After building constructor
07491    vtables and generating the sub-vtt from them, we need to restore the
07492    BINFO_VTABLES that were scribbled on.  DATA is a TREE_LIST whose
07493    TREE_VALUE is the TREE_TYPE of the base whose sub vtt was generated.  */
07494 
07495 static tree
07496 dfs_fixup_binfo_vtbls (binfo, data)
07497      tree binfo;
07498      void *data;
07499 {
07500   CLEAR_BINFO_MARKED (binfo);
07501 
07502   /* We don't care about bases that don't have vtables.  */
07503   if (!TYPE_VFIELD (BINFO_TYPE (binfo)))
07504     return NULL_TREE;
07505 
07506   /* If we scribbled the construction vtable vptr into BINFO, clear it
07507      out now.  */
07508   if (BINFO_VTABLE (binfo)
07509       && TREE_CODE (BINFO_VTABLE (binfo)) == TREE_LIST
07510       && (TREE_PURPOSE (BINFO_VTABLE (binfo)) 
07511     == TREE_VALUE ((tree) data)))
07512     BINFO_VTABLE (binfo) = TREE_CHAIN (BINFO_VTABLE (binfo));
07513 
07514   return NULL_TREE;
07515 }
07516 
07517 /* Build the construction vtable group for BINFO which is in the
07518    hierarchy dominated by T.  */
07519 
07520 static void
07521 build_ctor_vtbl_group (binfo, t)
07522      tree binfo;
07523      tree t;
07524 {
07525   tree list;
07526   tree type;
07527   tree vtbl;
07528   tree inits;
07529   tree id;
07530   tree vbase;
07531 
07532   /* See if we've already created this construction vtable group.  */
07533   id = mangle_ctor_vtbl_for_type (t, binfo);
07534   if (IDENTIFIER_GLOBAL_VALUE (id))
07535     return;
07536 
07537   my_friendly_assert (!same_type_p (BINFO_TYPE (binfo), t), 20010124);
07538   /* Build a version of VTBL (with the wrong type) for use in
07539      constructing the addresses of secondary vtables in the
07540      construction vtable group.  */
07541   vtbl = build_vtable (t, id, ptr_type_node);
07542   list = build_tree_list (vtbl, NULL_TREE);
07543   accumulate_vtbl_inits (binfo, TYPE_BINFO (TREE_TYPE (binfo)),
07544        binfo, t, list);
07545 
07546   /* Add the vtables for each of our virtual bases using the vbase in T
07547      binfo.  */
07548   for (vbase = TYPE_BINFO (BINFO_TYPE (binfo)); 
07549        vbase; 
07550        vbase = TREE_CHAIN (vbase))
07551     {
07552       tree b;
07553       tree orig_base;
07554 
07555       if (!TREE_VIA_VIRTUAL (vbase))
07556   continue;
07557       b = binfo_for_vbase (BINFO_TYPE (vbase), t);
07558       orig_base = binfo_for_vbase (BINFO_TYPE (vbase), BINFO_TYPE (binfo));
07559       
07560       accumulate_vtbl_inits (b, orig_base, binfo, t, list);
07561     }
07562   inits = TREE_VALUE (list);
07563 
07564   /* Figure out the type of the construction vtable.  */
07565   type = build_index_type (size_int (list_length (inits) - 1));
07566   type = build_cplus_array_type (vtable_entry_type, type);
07567   TREE_TYPE (vtbl) = type;
07568 
07569   /* Initialize the construction vtable.  */
07570   CLASSTYPE_VTABLES (t) = chainon (CLASSTYPE_VTABLES (t), vtbl);
07571   initialize_array (vtbl, inits);
07572   dump_vtable (t, binfo, vtbl);
07573 }
07574 
07575 /* Add the vtbl initializers for BINFO (and its bases other than
07576    non-virtual primaries) to the list of INITS.  BINFO is in the
07577    hierarchy dominated by T.  RTTI_BINFO is the binfo within T of
07578    the constructor the vtbl inits should be accumulated for. (If this
07579    is the complete object vtbl then RTTI_BINFO will be TYPE_BINFO (T).)
07580    ORIG_BINFO is the binfo for this object within BINFO_TYPE (RTTI_BINFO).
07581    BINFO is the active base equivalent of ORIG_BINFO in the inheritance
07582    graph of T. Both BINFO and ORIG_BINFO will have the same BINFO_TYPE,
07583    but are not necessarily the same in terms of layout.  */
07584 
07585 static void
07586 accumulate_vtbl_inits (binfo, orig_binfo, rtti_binfo, t, inits)
07587      tree binfo;
07588      tree orig_binfo;
07589      tree rtti_binfo;
07590      tree t;
07591      tree inits;
07592 {
07593   int i;
07594   int ctor_vtbl_p = !same_type_p (BINFO_TYPE (rtti_binfo), t);
07595 
07596   my_friendly_assert (same_type_p (BINFO_TYPE (binfo),
07597            BINFO_TYPE (orig_binfo)),
07598           20000517);
07599 
07600   /* If it doesn't have a vptr, we don't do anything.  */
07601   if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
07602     return;
07603   
07604   /* If we're building a construction vtable, we're not interested in
07605      subobjects that don't require construction vtables.  */
07606   if (ctor_vtbl_p 
07607       && !TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo))
07608       && !binfo_via_virtual (orig_binfo, BINFO_TYPE (rtti_binfo)))
07609     return;
07610 
07611   /* Build the initializers for the BINFO-in-T vtable.  */
07612   TREE_VALUE (inits) 
07613     = chainon (TREE_VALUE (inits),
07614          dfs_accumulate_vtbl_inits (binfo, orig_binfo,
07615             rtti_binfo, t, inits));
07616           
07617   /* Walk the BINFO and its bases.  We walk in preorder so that as we
07618      initialize each vtable we can figure out at what offset the
07619      secondary vtable lies from the primary vtable.  We can't use
07620      dfs_walk here because we need to iterate through bases of BINFO
07621      and RTTI_BINFO simultaneously.  */
07622   for (i = 0; i < BINFO_N_BASETYPES (binfo); ++i)
07623     {
07624       tree base_binfo = BINFO_BASETYPE (binfo, i);
07625       
07626       /* Skip virtual bases.  */
07627       if (TREE_VIA_VIRTUAL (base_binfo))
07628   continue;
07629       accumulate_vtbl_inits (base_binfo,
07630            BINFO_BASETYPE (orig_binfo, i),
07631            rtti_binfo, t,
07632            inits);
07633     }
07634 }
07635 
07636 /* Called from accumulate_vtbl_inits.  Returns the initializers for
07637    the BINFO vtable.  */
07638 
07639 static tree
07640 dfs_accumulate_vtbl_inits (binfo, orig_binfo, rtti_binfo, t, l)
07641      tree binfo;
07642      tree orig_binfo;
07643      tree rtti_binfo;
07644      tree t;
07645      tree l;
07646 {
07647   tree inits = NULL_TREE;
07648   tree vtbl = NULL_TREE;
07649   int ctor_vtbl_p = !same_type_p (BINFO_TYPE (rtti_binfo), t);
07650 
07651   if (ctor_vtbl_p
07652       && TREE_VIA_VIRTUAL (orig_binfo) && BINFO_PRIMARY_P (orig_binfo))
07653     {
07654       /* In the hierarchy of BINFO_TYPE (RTTI_BINFO), this is a
07655    primary virtual base.  If it is not the same primary in
07656    the hierarchy of T, we'll need to generate a ctor vtable
07657    for it, to place at its location in T.  If it is the same
07658    primary, we still need a VTT entry for the vtable, but it
07659    should point to the ctor vtable for the base it is a
07660    primary for within the sub-hierarchy of RTTI_BINFO.
07661         
07662    There are three possible cases:
07663         
07664    1) We are in the same place.
07665    2) We are a primary base within a lost primary virtual base of
07666    RTTI_BINFO.
07667    3) We are primary to something not a base of RTTI_BINFO.  */
07668     
07669       tree b = BINFO_PRIMARY_BASE_OF (binfo);
07670       tree last = NULL_TREE;
07671 
07672       /* First, look through the bases we are primary to for RTTI_BINFO
07673    or a virtual base.  */
07674       for (; b; b = BINFO_PRIMARY_BASE_OF (b))
07675   {
07676     last = b;
07677     if (TREE_VIA_VIRTUAL (b) || b == rtti_binfo)
07678       break;
07679   }
07680       /* If we run out of primary links, keep looking down our
07681    inheritance chain; we might be an indirect primary.  */
07682       if (b == NULL_TREE)
07683   for (b = last; b; b = BINFO_INHERITANCE_CHAIN (b))
07684     if (TREE_VIA_VIRTUAL (b) || b == rtti_binfo)
07685       break;
07686 
07687       /* If we found RTTI_BINFO, this is case 1.  If we found a virtual
07688    base B and it is a base of RTTI_BINFO, this is case 2.  In
07689    either case, we share our vtable with LAST, i.e. the
07690    derived-most base within B of which we are a primary.  */
07691       if (b == rtti_binfo
07692     || (b && binfo_for_vbase (BINFO_TYPE (b),
07693             BINFO_TYPE (rtti_binfo))))
07694   /* Just set our BINFO_VTABLE to point to LAST, as we may not have
07695      set LAST's BINFO_VTABLE yet.  We'll extract the actual vptr in
07696      binfo_ctor_vtable after everything's been set up.  */
07697   vtbl = last;
07698 
07699       /* Otherwise, this is case 3 and we get our own.  */
07700     }
07701   else if (!BINFO_NEW_VTABLE_MARKED (orig_binfo, BINFO_TYPE (rtti_binfo)))
07702     return inits;
07703 
07704   if (!vtbl)
07705     {
07706       tree index;
07707       int non_fn_entries;
07708 
07709       /* Compute the initializer for this vtable.  */
07710       inits = build_vtbl_initializer (binfo, orig_binfo, t, rtti_binfo,
07711               &non_fn_entries);
07712 
07713       /* Figure out the position to which the VPTR should point.  */
07714       vtbl = TREE_PURPOSE (l);
07715       vtbl = build1 (ADDR_EXPR, 
07716          vtbl_ptr_type_node,
07717          vtbl);
07718       TREE_CONSTANT (vtbl) = 1;
07719       index = size_binop (PLUS_EXPR,
07720         size_int (non_fn_entries),
07721         size_int (list_length (TREE_VALUE (l))));
07722       index = size_binop (MULT_EXPR,
07723         TYPE_SIZE_UNIT (vtable_entry_type),
07724         index);
07725       vtbl = build (PLUS_EXPR, TREE_TYPE (vtbl), vtbl, index);
07726       TREE_CONSTANT (vtbl) = 1;
07727     }
07728 
07729   if (ctor_vtbl_p)
07730     /* For a construction vtable, we can't overwrite BINFO_VTABLE.
07731        So, we make a TREE_LIST.  Later, dfs_fixup_binfo_vtbls will
07732        straighten this out.  */
07733     BINFO_VTABLE (binfo) = tree_cons (rtti_binfo, vtbl, BINFO_VTABLE (binfo));
07734   else if (BINFO_PRIMARY_P (binfo) && TREE_VIA_VIRTUAL (binfo))
07735     inits = NULL_TREE;
07736   else
07737      /* For an ordinary vtable, set BINFO_VTABLE.  */
07738     BINFO_VTABLE (binfo) = vtbl;
07739 
07740   return inits;
07741 }
07742 
07743 /* Construct the initializer for BINFO's virtual function table.  BINFO
07744    is part of the hierarchy dominated by T.  If we're building a
07745    construction vtable, the ORIG_BINFO is the binfo we should use to
07746    find the actual function pointers to put in the vtable - but they
07747    can be overridden on the path to most-derived in the graph that
07748    ORIG_BINFO belongs.  Otherwise,
07749    ORIG_BINFO should be the same as BINFO.  The RTTI_BINFO is the
07750    BINFO that should be indicated by the RTTI information in the
07751    vtable; it will be a base class of T, rather than T itself, if we
07752    are building a construction vtable.
07753 
07754    The value returned is a TREE_LIST suitable for wrapping in a
07755    CONSTRUCTOR to use as the DECL_INITIAL for a vtable.  If
07756    NON_FN_ENTRIES_P is not NULL, *NON_FN_ENTRIES_P is set to the
07757    number of non-function entries in the vtable.  
07758 
07759    It might seem that this function should never be called with a
07760    BINFO for which BINFO_PRIMARY_P holds, the vtable for such a
07761    base is always subsumed by a derived class vtable.  However, when
07762    we are building construction vtables, we do build vtables for
07763    primary bases; we need these while the primary base is being
07764    constructed.  */
07765 
07766 static tree
07767 build_vtbl_initializer (binfo, orig_binfo, t, rtti_binfo, non_fn_entries_p)
07768      tree binfo;
07769      tree orig_binfo;
07770      tree t;
07771      tree rtti_binfo;
07772      int *non_fn_entries_p;
07773 {
07774   tree v, b;
07775   tree vfun_inits;
07776   tree vbase;
07777   vtbl_init_data vid;
07778 
07779   /* Initialize VID.  */
07780   memset (&vid, 0, sizeof (vid));
07781   vid.binfo = binfo;
07782   vid.derived = t;
07783   vid.rtti_binfo = rtti_binfo;
07784   vid.last_init = &vid.inits;
07785   vid.primary_vtbl_p = (binfo == TYPE_BINFO (t));
07786   vid.ctor_vtbl_p = !same_type_p (BINFO_TYPE (rtti_binfo), t);
07787   vid.generate_vcall_entries = true;
07788   /* The first vbase or vcall offset is at index -3 in the vtable.  */
07789   vid.index = ssize_int (-3 * TARGET_VTABLE_DATA_ENTRY_DISTANCE);
07790 
07791   /* Add entries to the vtable for RTTI.  */
07792   build_rtti_vtbl_entries (binfo, &vid);
07793 
07794   /* Create an array for keeping track of the functions we've
07795      processed.  When we see multiple functions with the same
07796      signature, we share the vcall offsets.  */
07797   VARRAY_TREE_INIT (vid.fns, 32, "fns");
07798   /* Add the vcall and vbase offset entries.  */
07799   build_vcall_and_vbase_vtbl_entries (binfo, &vid);
07800   /* Clear BINFO_VTABLE_PATH_MARKED; it's set by
07801      build_vbase_offset_vtbl_entries.  */
07802   for (vbase = CLASSTYPE_VBASECLASSES (t); 
07803        vbase; 
07804        vbase = TREE_CHAIN (vbase))
07805     CLEAR_BINFO_VTABLE_PATH_MARKED (TREE_VALUE (vbase));
07806 
07807   /* If the target requires padding between data entries, add that now.  */
07808   if (TARGET_VTABLE_DATA_ENTRY_DISTANCE > 1)
07809     {
07810       tree cur, *prev;
07811 
07812       for (prev = &vid.inits; (cur = *prev); prev = &TREE_CHAIN (cur))
07813   {
07814     tree add = cur;
07815     int i;
07816 
07817     for (i = 1; i < TARGET_VTABLE_DATA_ENTRY_DISTANCE; ++i)
07818       add = tree_cons (NULL_TREE,
07819            build1 (NOP_EXPR, vtable_entry_type,
07820              null_pointer_node),
07821            add);
07822     *prev = add;
07823   }
07824     }
07825 
07826   if (non_fn_entries_p)
07827     *non_fn_entries_p = list_length (vid.inits);
07828 
07829   /* Go through all the ordinary virtual functions, building up
07830      initializers.  */
07831   vfun_inits = NULL_TREE;
07832   for (v = BINFO_VIRTUALS (orig_binfo); v; v = TREE_CHAIN (v))
07833     {
07834       tree delta;
07835       tree vcall_index;
07836       tree fn;
07837       tree init = NULL_TREE;
07838       
07839       fn = BV_FN (v);
07840 
07841       /* If the only definition of this function signature along our
07842    primary base chain is from a lost primary, this vtable slot will
07843    never be used, so just zero it out.  This is important to avoid
07844    requiring extra thunks which cannot be generated with the function.
07845 
07846    We first check this in update_vtable_entry_for_fn, so we handle
07847    restored primary bases properly; we also need to do it here so we
07848    zero out unused slots in ctor vtables, rather than filling themff
07849    with erroneous values (though harmless, apart from relocation
07850    costs).  */
07851       for (b = binfo; ; b = get_primary_binfo (b))
07852   {
07853     /* We found a defn before a lost primary; go ahead as normal.  */
07854     if (look_for_overrides_here (BINFO_TYPE (b), fn))
07855       break;
07856 
07857     /* The nearest definition is from a lost primary; clear the
07858        slot.  */
07859     if (BINFO_LOST_PRIMARY_P (b))
07860       {
07861         init = size_zero_node;
07862         break;
07863       }
07864   }
07865 
07866       if (! init)
07867   {
07868     /* Pull the offset for `this', and the function to call, out of
07869        the list.  */
07870     delta = BV_DELTA (v);
07871     vcall_index = BV_VCALL_INDEX (v);
07872 
07873     my_friendly_assert (TREE_CODE (delta) == INTEGER_CST, 19990727);
07874     my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL, 19990727);
07875 
07876     /* You can't call an abstract virtual function; it's abstract.
07877        So, we replace these functions with __pure_virtual.  */
07878     if (DECL_PURE_VIRTUAL_P (fn))
07879       fn = abort_fndecl;
07880     else if (!integer_zerop (delta) || vcall_index)
07881       fn = make_thunk (fn, delta, vcall_index);
07882     /* Take the address of the function, considering it to be of an
07883        appropriate generic type.  */
07884     init = build1 (ADDR_EXPR, vfunc_ptr_type_node, fn);
07885     /* The address of a function can't change.  */
07886     TREE_CONSTANT (init) = 1;
07887   }
07888 
07889       /* And add it to the chain of initializers.  */
07890       if (TARGET_VTABLE_USES_DESCRIPTORS)
07891   {
07892     int i;
07893     if (init == size_zero_node)
07894       for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
07895         vfun_inits = tree_cons (NULL_TREE, init, vfun_inits);
07896     else
07897       for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
07898         {
07899     tree fdesc = build (FDESC_EXPR, vfunc_ptr_type_node,
07900             TREE_OPERAND (init, 0),
07901             build_int_2 (i, 0));
07902     TREE_CONSTANT (fdesc) = 1;
07903 
07904     vfun_inits = tree_cons (NULL_TREE, fdesc, vfun_inits);
07905         }
07906   }
07907       else
07908         vfun_inits = tree_cons (NULL_TREE, init, vfun_inits);
07909     }
07910 
07911   /* The initializers for virtual functions were built up in reverse
07912      order; straighten them out now.  */
07913   vfun_inits = nreverse (vfun_inits);
07914   
07915   /* The negative offset initializers are also in reverse order.  */
07916   vid.inits = nreverse (vid.inits);
07917 
07918   /* Chain the two together.  */
07919   return chainon (vid.inits, vfun_inits);
07920 }
07921 
07922 /* Adds to vid->inits the initializers for the vbase and vcall
07923    offsets in BINFO, which is in the hierarchy dominated by T.  */
07924 
07925 static void
07926 build_vcall_and_vbase_vtbl_entries (binfo, vid)
07927      tree binfo;
07928      vtbl_init_data *vid;
07929 {
07930   tree b;
07931 
07932   /* If this is a derived class, we must first create entries
07933      corresponding to the primary base class.  */
07934   b = get_primary_binfo (binfo);
07935   if (b)
07936     build_vcall_and_vbase_vtbl_entries (b, vid);
07937 
07938   /* Add the vbase entries for this base.  */
07939   build_vbase_offset_vtbl_entries (binfo, vid);
07940   /* Add the vcall entries for this base.  */
07941   build_vcall_offset_vtbl_entries (binfo, vid);
07942 }
07943 
07944 /* Returns the initializers for the vbase offset entries in the vtable
07945    for BINFO (which is part of the class hierarchy dominated by T), in
07946    reverse order.  VBASE_OFFSET_INDEX gives the vtable index
07947    where the next vbase offset will go.  */
07948 
07949 static void
07950 build_vbase_offset_vtbl_entries (binfo, vid)
07951      tree binfo;
07952      vtbl_init_data *vid;
07953 {
07954   tree vbase;
07955   tree t;
07956   tree non_primary_binfo;
07957 
07958   /* If there are no virtual baseclasses, then there is nothing to
07959      do.  */
07960   if (!TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo)))
07961     return;
07962 
07963   t = vid->derived;
07964   
07965   /* We might be a primary base class.  Go up the inheritance hierarchy
07966      until we find the most derived class of which we are a primary base:
07967      it is the offset of that which we need to use.  */
07968   non_primary_binfo = binfo;
07969   while (BINFO_INHERITANCE_CHAIN (non_primary_binfo))
07970     {
07971       tree b;
07972 
07973       /* If we have reached a virtual base, then it must be a primary
07974    base (possibly multi-level) of vid->binfo, or we wouldn't
07975    have called build_vcall_and_vbase_vtbl_entries for it.  But it
07976    might be a lost primary, so just skip down to vid->binfo.  */
07977       if (TREE_VIA_VIRTUAL (non_primary_binfo))
07978   {
07979     non_primary_binfo = vid->binfo;
07980     break;
07981   }
07982 
07983       b = BINFO_INHERITANCE_CHAIN (non_primary_binfo);
07984       if (get_primary_binfo (b) != non_primary_binfo)
07985   break;
07986       non_primary_binfo = b;
07987     }
07988 
07989   /* Go through the virtual bases, adding the offsets.  */
07990   for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
07991        vbase;
07992        vbase = TREE_CHAIN (vbase))
07993     {
07994       tree b;
07995       tree delta;
07996       
07997       if (!TREE_VIA_VIRTUAL (vbase))
07998   continue;
07999 
08000       /* Find the instance of this virtual base in the complete
08001    object.  */
08002       b = binfo_for_vbase (BINFO_TYPE (vbase), t);
08003 
08004       /* If we've already got an offset for this virtual base, we
08005    don't need another one.  */
08006       if (BINFO_VTABLE_PATH_MARKED (b))
08007   continue;
08008       SET_BINFO_VTABLE_PATH_MARKED (b);
08009 
08010       /* Figure out where we can find this vbase offset.  */
08011       delta = size_binop (MULT_EXPR, 
08012         vid->index,
08013         convert (ssizetype,
08014            TYPE_SIZE_UNIT (vtable_entry_type)));
08015       if (vid->primary_vtbl_p)
08016   BINFO_VPTR_FIELD (b) = delta;
08017 
08018       if (binfo != TYPE_BINFO (t))
08019   {
08020     tree orig_vbase;
08021 
08022     /* Find the instance of this virtual base in the type of BINFO.  */
08023     orig_vbase = binfo_for_vbase (BINFO_TYPE (vbase),
08024           BINFO_TYPE (binfo));
08025 
08026     /* The vbase offset had better be the same.  */
08027     if (!tree_int_cst_equal (delta,
08028            BINFO_VPTR_FIELD (orig_vbase)))
08029       abort ();
08030   }
08031 
08032       /* The next vbase will come at a more negative offset.  */
08033       vid->index = size_binop (MINUS_EXPR, vid->index,
08034              ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
08035 
08036       /* The initializer is the delta from BINFO to this virtual base.
08037    The vbase offsets go in reverse inheritance-graph order, and
08038    we are walking in inheritance graph order so these end up in
08039    the right order.  */
08040       delta = size_diffop (BINFO_OFFSET (b), BINFO_OFFSET (non_primary_binfo));
08041       
08042       *vid->last_init 
08043   = build_tree_list (NULL_TREE,
08044          fold (build1 (NOP_EXPR, 
08045            vtable_entry_type,
08046            delta)));
08047       vid->last_init = &TREE_CHAIN (*vid->last_init);
08048     }
08049 }
08050 
08051 /* Adds the initializers for the vcall offset entries in the vtable
08052    for BINFO (which is part of the class hierarchy dominated by VID->DERIVED)
08053    to VID->INITS.  */
08054 
08055 static void
08056 build_vcall_offset_vtbl_entries (binfo, vid)
08057      tree binfo;
08058      vtbl_init_data *vid;
08059 {
08060   /* We only need these entries if this base is a virtual base.  We
08061      compute the indices -- but do not add to the vtable -- when
08062      building the main vtable for a class.  */
08063   if (TREE_VIA_VIRTUAL (binfo) || binfo == TYPE_BINFO (vid->derived))
08064     {
08065       /* We need a vcall offset for each of the virtual functions in this
08066    vtable.  For example:
08067 
08068      class A { virtual void f (); };
08069      class B1 : virtual public A { virtual void f (); };
08070      class B2 : virtual public A { virtual void f (); };
08071      class C: public B1, public B2 { virtual void f (); };
08072 
08073    A C object has a primary base of B1, which has a primary base of A.  A
08074    C also has a secondary base of B2, which no longer has a primary base
08075    of A.  So the B2-in-C construction vtable needs a secondary vtable for
08076    A, which will adjust the A* to a B2* to call f.  We have no way of
08077    knowing what (or even whether) this offset will be when we define B2,
08078    so we store this "vcall offset" in the A sub-vtable and look it up in
08079    a "virtual thunk" for B2::f.
08080 
08081    We need entries for all the functions in our primary vtable and
08082    in our non-virtual bases' secondary vtables.  */
08083       vid->vbase = binfo;
08084       /* If we are just computing the vcall indices -- but do not need
08085    the actual entries -- not that.  */
08086       if (!TREE_VIA_VIRTUAL (binfo))
08087   vid->generate_vcall_entries = false;
08088       /* Now, walk through the non-virtual bases, adding vcall offsets.  */
08089       add_vcall_offset_vtbl_entries_r (binfo, vid);
08090     }
08091 }
08092 
08093 /* Build vcall offsets, starting with those for BINFO.  */
08094 
08095 static void
08096 add_vcall_offset_vtbl_entries_r (binfo, vid)
08097      tree binfo;
08098      vtbl_init_data *vid;
08099 {
08100   int i;
08101   tree primary_binfo;
08102 
08103   /* Don't walk into virtual bases -- except, of course, for the
08104      virtual base for which we are building vcall offsets.  Any
08105      primary virtual base will have already had its offsets generated
08106      through the recursion in build_vcall_and_vbase_vtbl_entries.  */
08107   if (TREE_VIA_VIRTUAL (binfo) && vid->vbase != binfo)
08108     return;
08109   
08110   /* If BINFO has a primary base, process it first.  */
08111   primary_binfo = get_primary_binfo (binfo);
08112   if (primary_binfo)
08113     add_vcall_offset_vtbl_entries_r (primary_binfo, vid);
08114 
08115   /* Add BINFO itself to the list.  */
08116   add_vcall_offset_vtbl_entries_1 (binfo, vid);
08117 
08118   /* Scan the non-primary bases of BINFO.  */
08119   for (i = 0; i < BINFO_N_BASETYPES (binfo); ++i) 
08120     {
08121       tree base_binfo;
08122       
08123       base_binfo = BINFO_BASETYPE (binfo, i);
08124       if (base_binfo != primary_binfo)
08125   add_vcall_offset_vtbl_entries_r (base_binfo, vid);
08126     }
08127 }
08128 
08129 /* Called from build_vcall_offset_vtbl_entries_r.  */
08130 
08131 static void
08132 add_vcall_offset_vtbl_entries_1 (binfo, vid)
08133      tree binfo;
08134      vtbl_init_data* vid;
08135 {
08136   /* Make entries for the rest of the virtuals.  */
08137   if (abi_version_at_least (2))
08138     {
08139       tree orig_fn;
08140 
08141       /* The ABI requires that the methods be processed in declaration
08142    order.  G++ 3.2 used the order in the vtable.  */
08143       for (orig_fn = TYPE_METHODS (BINFO_TYPE (binfo));
08144      orig_fn;
08145      orig_fn = TREE_CHAIN (orig_fn))
08146   if (DECL_VINDEX (orig_fn))
08147     add_vcall_offset (orig_fn, binfo, vid);
08148     }
08149   else
08150     {
08151       tree derived_virtuals;
08152       tree base_virtuals;
08153       tree orig_virtuals;
08154       /* If BINFO is a primary base, the most derived class which has
08155    BINFO as a primary base; otherwise, just BINFO.  */
08156       tree non_primary_binfo;
08157 
08158       /* We might be a primary base class.  Go up the inheritance hierarchy
08159    until we find the most derived class of which we are a primary base:
08160    it is the BINFO_VIRTUALS there that we need to consider.  */
08161       non_primary_binfo = binfo;
08162       while (BINFO_INHERITANCE_CHAIN (non_primary_binfo))
08163   {
08164     tree b;
08165 
08166     /* If we have reached a virtual base, then it must be vid->vbase,
08167        because we ignore other virtual bases in
08168        add_vcall_offset_vtbl_entries_r.  In turn, it must be a primary
08169        base (possibly multi-level) of vid->binfo, or we wouldn't
08170        have called build_vcall_and_vbase_vtbl_entries for it.  But it
08171        might be a lost primary, so just skip down to vid->binfo.  */
08172     if (TREE_VIA_VIRTUAL (non_primary_binfo))
08173       {
08174         if (non_primary_binfo != vid->vbase)
08175     abort ();