blob: e4deb01f0c2819c2859819741919caeb7c8d3bbf [file] [log] [blame]
/* Functions related to building classes and their related objects.
Copyright (C) 1987, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
1999, 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
Contributed by Michael Tiemann (tiemann@cygnus.com)
This file is part of GCC.
GCC is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GCC is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING. If not, write to
the Free Software Foundation, 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA. */
/* High-level class interface. */
#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "tm.h"
#include "tree.h"
#include "cp-tree.h"
#include "flags.h"
#include "rtl.h"
#include "output.h"
#include "toplev.h"
#include "lex.h"
#include "target.h"
#include "convert.h"
/* The number of nested classes being processed. If we are not in the
scope of any class, this is zero. */
int current_class_depth;
/* In order to deal with nested classes, we keep a stack of classes.
The topmost entry is the innermost class, and is the entry at index
CURRENT_CLASS_DEPTH */
typedef struct class_stack_node {
/* The name of the class. */
tree name;
/* The _TYPE node for the class. */
tree type;
/* The access specifier pending for new declarations in the scope of
this class. */
tree access;
/* If were defining TYPE, the names used in this class. */
splay_tree names_used;
}* class_stack_node_t;
typedef struct vtbl_init_data_s
{
/* The base for which we're building initializers. */
tree binfo;
/* The type of the most-derived type. */
tree derived;
/* The binfo for the dynamic type. This will be TYPE_BINFO (derived),
unless ctor_vtbl_p is true. */
tree rtti_binfo;
/* The negative-index vtable initializers built up so far. These
are in order from least negative index to most negative index. */
tree inits;
/* The last (i.e., most negative) entry in INITS. */
tree* last_init;
/* The binfo for the virtual base for which we're building
vcall offset initializers. */
tree vbase;
/* The functions in vbase for which we have already provided vcall
offsets. */
varray_type fns;
/* The vtable index of the next vcall or vbase offset. */
tree index;
/* Nonzero if we are building the initializer for the primary
vtable. */
int primary_vtbl_p;
/* Nonzero if we are building the initializer for a construction
vtable. */
int ctor_vtbl_p;
/* True when adding vcall offset entries to the vtable. False when
merely computing the indices. */
bool generate_vcall_entries;
} vtbl_init_data;
/* The type of a function passed to walk_subobject_offsets. */
typedef int (*subobject_offset_fn) (tree, tree, splay_tree);
/* The stack itself. This is a dynamically resized array. The
number of elements allocated is CURRENT_CLASS_STACK_SIZE. */
static int current_class_stack_size;
static class_stack_node_t current_class_stack;
/* An array of all local classes present in this translation unit, in
declaration order. */
varray_type local_classes;
static tree get_vfield_name (tree);
static void finish_struct_anon (tree);
static tree get_vtable_name (tree);
static tree get_basefndecls (tree, tree);
static int build_primary_vtable (tree, tree);
static int build_secondary_vtable (tree);
static void finish_vtbls (tree);
static void modify_vtable_entry (tree, tree, tree, tree, tree *);
static void finish_struct_bits (tree);
static int alter_access (tree, tree, tree);
static void handle_using_decl (tree, tree);
static void check_for_override (tree, tree);
static tree dfs_modify_vtables (tree, void *);
static tree modify_all_vtables (tree, tree);
static void determine_primary_base (tree);
static void finish_struct_methods (tree);
static void maybe_warn_about_overly_private_class (tree);
static int method_name_cmp (const void *, const void *);
static int resort_method_name_cmp (const void *, const void *);
static void add_implicitly_declared_members (tree, int, int, int);
static tree fixed_type_or_null (tree, int *, int *);
static tree resolve_address_of_overloaded_function (tree, tree, tsubst_flags_t,
bool, tree);
static tree build_vtbl_ref_1 (tree, tree);
static tree build_vtbl_initializer (tree, tree, tree, tree, int *);
static int count_fields (tree);
static int add_fields_to_record_type (tree, struct sorted_fields_type*, int);
static void check_bitfield_decl (tree);
static void check_field_decl (tree, tree, int *, int *, int *, int *);
static void check_field_decls (tree, tree *, int *, int *, int *);
static tree *build_base_field (record_layout_info, tree, splay_tree, tree *);
static void build_base_fields (record_layout_info, splay_tree, tree *);
static void check_methods (tree);
static void remove_zero_width_bit_fields (tree);
static void check_bases (tree, int *, int *, int *);
static void check_bases_and_members (tree);
static tree create_vtable_ptr (tree, tree *);
static void include_empty_classes (record_layout_info);
static void layout_class_type (tree, tree *);
static void fixup_pending_inline (tree);
static void fixup_inline_methods (tree);
static void set_primary_base (tree, tree);
static void propagate_binfo_offsets (tree, tree);
static void layout_virtual_bases (record_layout_info, splay_tree);
static void build_vbase_offset_vtbl_entries (tree, vtbl_init_data *);
static void add_vcall_offset_vtbl_entries_r (tree, vtbl_init_data *);
static void add_vcall_offset_vtbl_entries_1 (tree, vtbl_init_data *);
static void build_vcall_offset_vtbl_entries (tree, vtbl_init_data *);
static void add_vcall_offset (tree, tree, vtbl_init_data *);
static void layout_vtable_decl (tree, int);
static tree dfs_find_final_overrider (tree, void *);
static tree dfs_find_final_overrider_post (tree, void *);
static tree dfs_find_final_overrider_q (tree, int, void *);
static tree find_final_overrider (tree, tree, tree);
static int make_new_vtable (tree, tree);
static int maybe_indent_hierarchy (FILE *, int, int);
static tree dump_class_hierarchy_r (FILE *, int, tree, tree, int);
static void dump_class_hierarchy (tree);
static void dump_class_hierarchy_1 (FILE *, int, tree);
static void dump_array (FILE *, tree);
static void dump_vtable (tree, tree, tree);
static void dump_vtt (tree, tree);
static void dump_thunk (FILE *, int, tree);
static tree build_vtable (tree, tree, tree);
static void initialize_vtable (tree, tree);
static void initialize_array (tree, tree);
static void layout_nonempty_base_or_field (record_layout_info,
tree, tree, splay_tree);
static tree end_of_class (tree, int);
static bool layout_empty_base (tree, tree, splay_tree);
static void accumulate_vtbl_inits (tree, tree, tree, tree, tree);
static tree dfs_accumulate_vtbl_inits (tree, tree, tree, tree,
tree);
static void build_rtti_vtbl_entries (tree, vtbl_init_data *);
static void build_vcall_and_vbase_vtbl_entries (tree,
vtbl_init_data *);
static void mark_primary_bases (tree);
static void clone_constructors_and_destructors (tree);
static tree build_clone (tree, tree);
static void update_vtable_entry_for_fn (tree, tree, tree, tree *, unsigned);
static tree copy_virtuals (tree);
static void build_ctor_vtbl_group (tree, tree);
static void build_vtt (tree);
static tree binfo_ctor_vtable (tree);
static tree *build_vtt_inits (tree, tree, tree *, tree *);
static tree dfs_build_secondary_vptr_vtt_inits (tree, void *);
static tree dfs_ctor_vtable_bases_queue_p (tree, int, void *data);
static tree dfs_fixup_binfo_vtbls (tree, void *);
static int record_subobject_offset (tree, tree, splay_tree);
static int check_subobject_offset (tree, tree, splay_tree);
static int walk_subobject_offsets (tree, subobject_offset_fn,
tree, splay_tree, tree, int);
static void record_subobject_offsets (tree, tree, splay_tree, int);
static int layout_conflict_p (tree, tree, splay_tree, int);
static int splay_tree_compare_integer_csts (splay_tree_key k1,
splay_tree_key k2);
static void warn_about_ambiguous_bases (tree);
static bool type_requires_array_cookie (tree);
static bool contains_empty_class_p (tree);
static bool base_derived_from (tree, tree);
static int empty_base_at_nonzero_offset_p (tree, tree, splay_tree);
static tree end_of_base (tree);
static tree get_vcall_index (tree, tree);
/* Macros for dfs walking during vtt construction. See
dfs_ctor_vtable_bases_queue_p, dfs_build_secondary_vptr_vtt_inits
and dfs_fixup_binfo_vtbls. */
#define VTT_TOP_LEVEL_P(NODE) TREE_UNSIGNED (NODE)
#define VTT_MARKED_BINFO_P(NODE) TREE_USED (NODE)
/* Variables shared between class.c and call.c. */
#ifdef GATHER_STATISTICS
int n_vtables = 0;
int n_vtable_entries = 0;
int n_vtable_searches = 0;
int n_vtable_elems = 0;
int n_convert_harshness = 0;
int n_compute_conversion_costs = 0;
int n_inner_fields_searched = 0;
#endif
/* Convert to or from a base subobject. EXPR is an expression of type
`A' or `A*', an expression of type `B' or `B*' is returned. To
convert A to a base B, CODE is PLUS_EXPR and BINFO is the binfo for
the B base instance within A. To convert base A to derived B, CODE
is MINUS_EXPR and BINFO is the binfo for the A instance within B.
In this latter case, A must not be a morally virtual base of B.
NONNULL is true if EXPR is known to be non-NULL (this is only
needed when EXPR is of pointer type). CV qualifiers are preserved
from EXPR. */
tree
build_base_path (enum tree_code code,
tree expr,
tree binfo,
int nonnull)
{
tree v_binfo = NULL_TREE;
tree d_binfo = NULL_TREE;
tree probe;
tree offset;
tree target_type;
tree null_test = NULL;
tree ptr_target_type;
int fixed_type_p;
int want_pointer = TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE;
if (expr == error_mark_node || binfo == error_mark_node || !binfo)
return error_mark_node;
for (probe = binfo; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
{
d_binfo = probe;
if (!v_binfo && TREE_VIA_VIRTUAL (probe))
v_binfo = probe;
}
probe = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
if (want_pointer)
probe = TYPE_MAIN_VARIANT (TREE_TYPE (probe));
my_friendly_assert (code == MINUS_EXPR
? same_type_p (BINFO_TYPE (binfo), probe)
: code == PLUS_EXPR
? same_type_p (BINFO_TYPE (d_binfo), probe)
: false, 20010723);
if (code == MINUS_EXPR && v_binfo)
{
error ("cannot convert from base `%T' to derived type `%T' via virtual base `%T'",
BINFO_TYPE (binfo), BINFO_TYPE (d_binfo), BINFO_TYPE (v_binfo));
return error_mark_node;
}
if (!want_pointer)
/* This must happen before the call to save_expr. */
expr = build_unary_op (ADDR_EXPR, expr, 0);
fixed_type_p = resolves_to_fixed_type_p (expr, &nonnull);
if (fixed_type_p <= 0 && TREE_SIDE_EFFECTS (expr))
expr = save_expr (expr);
if (want_pointer && !nonnull)
null_test = build (EQ_EXPR, boolean_type_node, expr, integer_zero_node);
offset = BINFO_OFFSET (binfo);
if (v_binfo && fixed_type_p <= 0)
{
/* Going via virtual base V_BINFO. We need the static offset
from V_BINFO to BINFO, and the dynamic offset from D_BINFO to
V_BINFO. That offset is an entry in D_BINFO's vtable. */
tree v_offset;
if (fixed_type_p < 0 && in_base_initializer)
{
/* In a base member initializer, we cannot rely on
the vtable being set up. We have to use the vtt_parm. */
tree derived = BINFO_INHERITANCE_CHAIN (v_binfo);
v_offset = build (PLUS_EXPR, TREE_TYPE (current_vtt_parm),
current_vtt_parm, BINFO_VPTR_INDEX (derived));
v_offset = build1 (INDIRECT_REF,
TREE_TYPE (TYPE_VFIELD (BINFO_TYPE (derived))),
v_offset);
}
else
v_offset = build_vfield_ref (build_indirect_ref (expr, NULL),
TREE_TYPE (TREE_TYPE (expr)));
v_offset = build (PLUS_EXPR, TREE_TYPE (v_offset),
v_offset, BINFO_VPTR_FIELD (v_binfo));
v_offset = build1 (NOP_EXPR,
build_pointer_type (ptrdiff_type_node),
v_offset);
v_offset = build_indirect_ref (v_offset, NULL);
offset = convert_to_integer (ptrdiff_type_node,
size_diffop (offset,
BINFO_OFFSET (v_binfo)));
if (!integer_zerop (offset))
v_offset = build (code, ptrdiff_type_node, v_offset, offset);
if (fixed_type_p < 0)
/* Negative fixed_type_p means this is a constructor or destructor;
virtual base layout is fixed in in-charge [cd]tors, but not in
base [cd]tors. */
offset = build (COND_EXPR, ptrdiff_type_node,
build (EQ_EXPR, boolean_type_node,
current_in_charge_parm, integer_zero_node),
v_offset,
BINFO_OFFSET (binfo));
else
offset = v_offset;
}
target_type = code == PLUS_EXPR ? BINFO_TYPE (binfo) : BINFO_TYPE (d_binfo);
target_type = cp_build_qualified_type
(target_type, cp_type_quals (TREE_TYPE (TREE_TYPE (expr))));
ptr_target_type = build_pointer_type (target_type);
if (want_pointer)
target_type = ptr_target_type;
expr = build1 (NOP_EXPR, ptr_target_type, expr);
if (!integer_zerop (offset))
expr = build (code, ptr_target_type, expr, offset);
else
null_test = NULL;
if (!want_pointer)
expr = build_indirect_ref (expr, NULL);
if (null_test)
expr = build (COND_EXPR, target_type, null_test,
build1 (NOP_EXPR, target_type, integer_zero_node),
expr);
return expr;
}
/* Convert OBJECT to the base TYPE. If CHECK_ACCESS is true, an error
message is emitted if TYPE is inaccessible. OBJECT is assumed to
be non-NULL. */
tree
convert_to_base (tree object, tree type, bool check_access)
{
tree binfo;
binfo = lookup_base (TREE_TYPE (object), type,
check_access ? ba_check : ba_ignore,
NULL);
if (!binfo || binfo == error_mark_node)
return error_mark_node;
return build_base_path (PLUS_EXPR, object, binfo, /*nonnull=*/1);
}
/* EXPR is an expression with class type. BASE is a base class (a
BINFO) of that class type. Returns EXPR, converted to the BASE
type. This function assumes that EXPR is the most derived class;
therefore virtual bases can be found at their static offsets. */
tree
convert_to_base_statically (tree expr, tree base)
{
tree expr_type;
expr_type = TREE_TYPE (expr);
if (!same_type_p (expr_type, BINFO_TYPE (base)))
{
tree pointer_type;
pointer_type = build_pointer_type (expr_type);
expr = build_unary_op (ADDR_EXPR, expr, /*noconvert=*/1);
if (!integer_zerop (BINFO_OFFSET (base)))
expr = build (PLUS_EXPR, pointer_type, expr,
build_nop (pointer_type, BINFO_OFFSET (base)));
expr = build_nop (build_pointer_type (BINFO_TYPE (base)), expr);
expr = build1 (INDIRECT_REF, BINFO_TYPE (base), expr);
}
return expr;
}
/* Given an object INSTANCE, return an expression which yields the
vtable element corresponding to INDEX. There are many special
cases for INSTANCE which we take care of here, mainly to avoid
creating extra tree nodes when we don't have to. */
static tree
build_vtbl_ref_1 (tree instance, tree idx)
{
tree aref;
tree vtbl = NULL_TREE;
/* Try to figure out what a reference refers to, and
access its virtual function table directly. */
int cdtorp = 0;
tree fixed_type = fixed_type_or_null (instance, NULL, &cdtorp);
tree basetype = non_reference (TREE_TYPE (instance));
if (fixed_type && !cdtorp)
{
tree binfo = lookup_base (fixed_type, basetype,
ba_ignore|ba_quiet, NULL);
if (binfo)
vtbl = BINFO_VTABLE (binfo);
}
if (!vtbl)
vtbl = build_vfield_ref (instance, basetype);
assemble_external (vtbl);
aref = build_array_ref (vtbl, idx);
return aref;
}
tree
build_vtbl_ref (tree instance, tree idx)
{
tree aref = build_vtbl_ref_1 (instance, idx);
return aref;
}
/* Given an object INSTANCE, return an expression which yields a
function pointer corresponding to vtable element INDEX. */
tree
build_vfn_ref (tree instance, tree idx)
{
tree aref = build_vtbl_ref_1 (instance, idx);
/* When using function descriptors, the address of the
vtable entry is treated as a function pointer. */
if (TARGET_VTABLE_USES_DESCRIPTORS)
aref = build1 (NOP_EXPR, TREE_TYPE (aref),
build_unary_op (ADDR_EXPR, aref, /*noconvert=*/1));
return aref;
}
/* Return the name of the virtual function table (as an IDENTIFIER_NODE)
for the given TYPE. */
static tree
get_vtable_name (tree type)
{
return mangle_vtbl_for_type (type);
}
/* Return an IDENTIFIER_NODE for the name of the virtual table table
for TYPE. */
tree
get_vtt_name (tree type)
{
return mangle_vtt_for_type (type);
}
/* Create a VAR_DECL for a primary or secondary vtable for CLASS_TYPE.
(For a secondary vtable for B-in-D, CLASS_TYPE should be D, not B.)
Use NAME for the name of the vtable, and VTABLE_TYPE for its type. */
static tree
build_vtable (tree class_type, tree name, tree vtable_type)
{
tree decl;
decl = build_lang_decl (VAR_DECL, name, vtable_type);
/* vtable names are already mangled; give them their DECL_ASSEMBLER_NAME
now to avoid confusion in mangle_decl. */
SET_DECL_ASSEMBLER_NAME (decl, name);
DECL_CONTEXT (decl) = class_type;
DECL_ARTIFICIAL (decl) = 1;
TREE_STATIC (decl) = 1;
TREE_READONLY (decl) = 1;
DECL_VIRTUAL_P (decl) = 1;
DECL_ALIGN (decl) = TARGET_VTABLE_ENTRY_ALIGN;
DECL_VTABLE_OR_VTT_P (decl) = 1;
/* At one time the vtable info was grabbed 2 words at a time. This
fails on sparc unless you have 8-byte alignment. (tiemann) */
DECL_ALIGN (decl) = MAX (TYPE_ALIGN (double_type_node),
DECL_ALIGN (decl));
import_export_vtable (decl, class_type, 0);
return decl;
}
/* Get the VAR_DECL of the vtable for TYPE. TYPE need not be polymorphic,
or even complete. If this does not exist, create it. If COMPLETE is
nonzero, then complete the definition of it -- that will render it
impossible to actually build the vtable, but is useful to get at those
which are known to exist in the runtime. */
tree
get_vtable_decl (tree type, int complete)
{
tree decl;
if (CLASSTYPE_VTABLES (type))
return CLASSTYPE_VTABLES (type);
decl = build_vtable (type, get_vtable_name (type), vtbl_type_node);
CLASSTYPE_VTABLES (type) = decl;
if (complete)
{
DECL_EXTERNAL (decl) = 1;
cp_finish_decl (decl, NULL_TREE, NULL_TREE, 0);
}
return decl;
}
/* Returns a copy of the BINFO_VIRTUALS list in BINFO. The
BV_VCALL_INDEX for each entry is cleared. */
static tree
copy_virtuals (tree binfo)
{
tree copies;
tree t;
copies = copy_list (BINFO_VIRTUALS (binfo));
for (t = copies; t; t = TREE_CHAIN (t))
BV_VCALL_INDEX (t) = NULL_TREE;
return copies;
}
/* Build the primary virtual function table for TYPE. If BINFO is
non-NULL, build the vtable starting with the initial approximation
that it is the same as the one which is the head of the association
list. Returns a nonzero value if a new vtable is actually
created. */
static int
build_primary_vtable (tree binfo, tree type)
{
tree decl;
tree virtuals;
decl = get_vtable_decl (type, /*complete=*/0);
if (binfo)
{
if (BINFO_NEW_VTABLE_MARKED (binfo))
/* We have already created a vtable for this base, so there's
no need to do it again. */
return 0;
virtuals = copy_virtuals (binfo);
TREE_TYPE (decl) = TREE_TYPE (get_vtbl_decl_for_binfo (binfo));
DECL_SIZE (decl) = TYPE_SIZE (TREE_TYPE (decl));
DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
}
else
{
my_friendly_assert (TREE_TYPE (decl) == vtbl_type_node, 20000118);
virtuals = NULL_TREE;
}
#ifdef GATHER_STATISTICS
n_vtables += 1;
n_vtable_elems += list_length (virtuals);
#endif
/* Initialize the association list for this type, based
on our first approximation. */
TYPE_BINFO_VTABLE (type) = decl;
TYPE_BINFO_VIRTUALS (type) = virtuals;
SET_BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (type));
return 1;
}
/* Give BINFO a new virtual function table which is initialized
with a skeleton-copy of its original initialization. The only
entry that changes is the `delta' entry, so we can really
share a lot of structure.
FOR_TYPE is the most derived type which caused this table to
be needed.
Returns nonzero if we haven't met BINFO before.
The order in which vtables are built (by calling this function) for
an object must remain the same, otherwise a binary incompatibility
can result. */
static int
build_secondary_vtable (tree binfo)
{
if (BINFO_NEW_VTABLE_MARKED (binfo))
/* We already created a vtable for this base. There's no need to
do it again. */
return 0;
/* Remember that we've created a vtable for this BINFO, so that we
don't try to do so again. */
SET_BINFO_NEW_VTABLE_MARKED (binfo);
/* Make fresh virtual list, so we can smash it later. */
BINFO_VIRTUALS (binfo) = copy_virtuals (binfo);
/* Secondary vtables are laid out as part of the same structure as
the primary vtable. */
BINFO_VTABLE (binfo) = NULL_TREE;
return 1;
}
/* Create a new vtable for BINFO which is the hierarchy dominated by
T. Return nonzero if we actually created a new vtable. */
static int
make_new_vtable (tree t, tree binfo)
{
if (binfo == TYPE_BINFO (t))
/* In this case, it is *type*'s vtable we are modifying. We start
with the approximation that its vtable is that of the
immediate base class. */
/* ??? This actually passes TYPE_BINFO (t), not the primary base binfo,
since we've updated DECL_CONTEXT (TYPE_VFIELD (t)) by now. */
return build_primary_vtable (TYPE_BINFO (DECL_CONTEXT (TYPE_VFIELD (t))),
t);
else
/* This is our very own copy of `basetype' to play with. Later,
we will fill in all the virtual functions that override the
virtual functions in these base classes which are not defined
by the current type. */
return build_secondary_vtable (binfo);
}
/* Make *VIRTUALS, an entry on the BINFO_VIRTUALS list for BINFO
(which is in the hierarchy dominated by T) list FNDECL as its
BV_FN. DELTA is the required constant adjustment from the `this'
pointer where the vtable entry appears to the `this' required when
the function is actually called. */
static void
modify_vtable_entry (tree t,
tree binfo,
tree fndecl,
tree delta,
tree *virtuals)
{
tree v;
v = *virtuals;
if (fndecl != BV_FN (v)
|| !tree_int_cst_equal (delta, BV_DELTA (v)))
{
/* We need a new vtable for BINFO. */
if (make_new_vtable (t, binfo))
{
/* If we really did make a new vtable, we also made a copy
of the BINFO_VIRTUALS list. Now, we have to find the
corresponding entry in that list. */
*virtuals = BINFO_VIRTUALS (binfo);
while (BV_FN (*virtuals) != BV_FN (v))
*virtuals = TREE_CHAIN (*virtuals);
v = *virtuals;
}
BV_DELTA (v) = delta;
BV_VCALL_INDEX (v) = NULL_TREE;
BV_FN (v) = fndecl;
}
}
/* Add method METHOD to class TYPE. If ERROR_P is true, we are adding
the method after the class has already been defined because a
declaration for it was seen. (Even though that is erroneous, we
add the method for improved error recovery.) */
void
add_method (tree type, tree method, int error_p)
{
int using;
int len;
int slot;
tree method_vec;
int template_conv_p;
if (method == error_mark_node)
return;
using = (DECL_CONTEXT (method) != type);
template_conv_p = (TREE_CODE (method) == TEMPLATE_DECL
&& DECL_TEMPLATE_CONV_FN_P (method));
if (!CLASSTYPE_METHOD_VEC (type))
/* Make a new method vector. We start with 8 entries. We must
allocate at least two (for constructors and destructors), and
we're going to end up with an assignment operator at some point
as well.
We could use a TREE_LIST for now, and convert it to a TREE_VEC
in finish_struct, but we would probably waste more memory
making the links in the list than we would by over-allocating
the size of the vector here. Furthermore, we would complicate
all the code that expects this to be a vector. */
CLASSTYPE_METHOD_VEC (type) = make_tree_vec (8);
method_vec = CLASSTYPE_METHOD_VEC (type);
len = TREE_VEC_LENGTH (method_vec);
/* Constructors and destructors go in special slots. */
if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (method))
slot = CLASSTYPE_CONSTRUCTOR_SLOT;
else if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (method))
{
slot = CLASSTYPE_DESTRUCTOR_SLOT;
TYPE_HAS_DESTRUCTOR (type) = 1;
if (TYPE_FOR_JAVA (type))
error (DECL_ARTIFICIAL (method)
? "Java class '%T' cannot have an implicit non-trivial destructor"
: "Java class '%T' cannot have a destructor",
DECL_CONTEXT (method));
}
else
{
int have_template_convs_p = 0;
/* See if we already have an entry with this name. */
for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT; slot < len; ++slot)
{
tree m = TREE_VEC_ELT (method_vec, slot);
if (!m)
break;
m = OVL_CURRENT (m);
if (template_conv_p)
{
have_template_convs_p = (TREE_CODE (m) == TEMPLATE_DECL
&& DECL_TEMPLATE_CONV_FN_P (m));
/* If we need to move things up, see if there's
space. */
if (!have_template_convs_p)
{
slot = len - 1;
if (TREE_VEC_ELT (method_vec, slot))
slot++;
}
break;
}
if (DECL_NAME (m) == DECL_NAME (method))
break;
}
if (slot == len)
{
/* We need a bigger method vector. */
int new_len;
tree new_vec;
/* In the non-error case, we are processing a class
definition. Double the size of the vector to give room
for new methods. */
if (!error_p)
new_len = 2 * len;
/* In the error case, the vector is already complete. We
don't expect many errors, and the rest of the front-end
will get confused if there are empty slots in the vector. */
else
new_len = len + 1;
new_vec = make_tree_vec (new_len);
memcpy (&TREE_VEC_ELT (new_vec, 0), &TREE_VEC_ELT (method_vec, 0),
len * sizeof (tree));
len = new_len;
method_vec = CLASSTYPE_METHOD_VEC (type) = new_vec;
}
if (DECL_CONV_FN_P (method) && !TREE_VEC_ELT (method_vec, slot))
{
/* Type conversion operators have to come before ordinary
methods; add_conversions depends on this to speed up
looking for conversion operators. So, if necessary, we
slide some of the vector elements up. In theory, this
makes this algorithm O(N^2) but we don't expect many
conversion operators. */
if (template_conv_p)
slot = CLASSTYPE_FIRST_CONVERSION_SLOT;
else
for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT; slot < len; ++slot)
{
tree fn = TREE_VEC_ELT (method_vec, slot);
if (!fn)
/* There are no more entries in the vector, so we
can insert the new conversion operator here. */
break;
if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
/* We can insert the new function right at the
SLOTth position. */
break;
}
if (template_conv_p && have_template_convs_p)
/*OK*/;
else if (!TREE_VEC_ELT (method_vec, slot))
/* There is nothing in the Ith slot, so we can avoid
moving anything. */
;
else
{
/* We know the last slot in the vector is empty
because we know that at this point there's room
for a new function. */
memmove (&TREE_VEC_ELT (method_vec, slot + 1),
&TREE_VEC_ELT (method_vec, slot),
(len - slot - 1) * sizeof (tree));
TREE_VEC_ELT (method_vec, slot) = NULL_TREE;
}
}
}
if (processing_template_decl)
/* TYPE is a template class. Don't issue any errors now; wait
until instantiation time to complain. */
;
else
{
tree fns;
/* Check to see if we've already got this method. */
for (fns = TREE_VEC_ELT (method_vec, slot);
fns;
fns = OVL_NEXT (fns))
{
tree fn = OVL_CURRENT (fns);
tree parms1;
tree parms2;
bool same = 1;
if (TREE_CODE (fn) != TREE_CODE (method))
continue;
/* [over.load] Member function declarations with the
same name and the same parameter types cannot be
overloaded if any of them is a static member
function declaration.
[namespace.udecl] When a using-declaration brings names
from a base class into a derived class scope, member
functions in the derived class override and/or hide member
functions with the same name and parameter types in a base
class (rather than conflicting). */
parms1 = TYPE_ARG_TYPES (TREE_TYPE (fn));
parms2 = TYPE_ARG_TYPES (TREE_TYPE (method));
/* Compare the quals on the 'this' parm. Don't compare
the whole types, as used functions are treated as
coming from the using class in overload resolution. */
if (! DECL_STATIC_FUNCTION_P (fn)
&& ! DECL_STATIC_FUNCTION_P (method)
&& (TYPE_QUALS (TREE_TYPE (TREE_VALUE (parms1)))
!= TYPE_QUALS (TREE_TYPE (TREE_VALUE (parms2)))))
same = 0;
/* For templates, the template parms must be identical. */
if (TREE_CODE (fn) == TEMPLATE_DECL
&& !comp_template_parms (DECL_TEMPLATE_PARMS (fn),
DECL_TEMPLATE_PARMS (method)))
same = 0;
if (! DECL_STATIC_FUNCTION_P (fn))
parms1 = TREE_CHAIN (parms1);
if (! DECL_STATIC_FUNCTION_P (method))
parms2 = TREE_CHAIN (parms2);
if (same && compparms (parms1, parms2)
&& (!DECL_CONV_FN_P (fn)
|| same_type_p (TREE_TYPE (TREE_TYPE (fn)),
TREE_TYPE (TREE_TYPE (method)))))
{
if (using && DECL_CONTEXT (fn) == type)
/* Defer to the local function. */
return;
else
{
cp_error_at ("`%#D' and `%#D' cannot be overloaded",
method, fn);
/* We don't call duplicate_decls here to merge
the declarations because that will confuse
things if the methods have inline
definitions. In particular, we will crash
while processing the definitions. */
return;
}
}
}
}
/* Actually insert the new method. */
TREE_VEC_ELT (method_vec, slot)
= build_overload (method, TREE_VEC_ELT (method_vec, slot));
/* Add the new binding. */
if (!DECL_CONSTRUCTOR_P (method)
&& !DECL_DESTRUCTOR_P (method))
push_class_level_binding (DECL_NAME (method),
TREE_VEC_ELT (method_vec, slot));
}
/* Subroutines of finish_struct. */
/* Change the access of FDECL to ACCESS in T. Return 1 if change was
legit, otherwise return 0. */
static int
alter_access (tree t, tree fdecl, tree access)
{
tree elem;
if (!DECL_LANG_SPECIFIC (fdecl))
retrofit_lang_decl (fdecl);
my_friendly_assert (!DECL_DISCRIMINATOR_P (fdecl), 20030624);
elem = purpose_member (t, DECL_ACCESS (fdecl));
if (elem)
{
if (TREE_VALUE (elem) != access)
{
if (TREE_CODE (TREE_TYPE (fdecl)) == FUNCTION_DECL)
cp_error_at ("conflicting access specifications for method `%D', ignored", TREE_TYPE (fdecl));
else
error ("conflicting access specifications for field `%s', ignored",
IDENTIFIER_POINTER (DECL_NAME (fdecl)));
}
else
{
/* They're changing the access to the same thing they changed
it to before. That's OK. */
;
}
}
else
{
perform_or_defer_access_check (TYPE_BINFO (t), fdecl);
DECL_ACCESS (fdecl) = tree_cons (t, access, DECL_ACCESS (fdecl));
return 1;
}
return 0;
}
/* Process the USING_DECL, which is a member of T. */
static void
handle_using_decl (tree using_decl, tree t)
{
tree ctype = DECL_INITIAL (using_decl);
tree name = DECL_NAME (using_decl);
tree access
= TREE_PRIVATE (using_decl) ? access_private_node
: TREE_PROTECTED (using_decl) ? access_protected_node
: access_public_node;
tree fdecl, binfo;
tree flist = NULL_TREE;
tree old_value;
if (ctype == error_mark_node)
return;
binfo = lookup_base (t, ctype, ba_any, NULL);
if (! binfo)
{
location_t saved_loc = input_location;
input_location = DECL_SOURCE_LOCATION (using_decl);
error_not_base_type (ctype, t);
input_location = saved_loc;
return;
}
if (constructor_name_p (name, ctype))
{
cp_error_at ("`%D' names constructor", using_decl);
return;
}
if (constructor_name_p (name, t))
{
cp_error_at ("`%D' invalid in `%T'", using_decl, t);
return;
}
fdecl = lookup_member (binfo, name, 0, false);
if (!fdecl)
{
cp_error_at ("no members matching `%D' in `%#T'", using_decl, ctype);
return;
}
if (BASELINK_P (fdecl))
/* Ignore base type this came from. */
fdecl = BASELINK_FUNCTIONS (fdecl);
old_value = IDENTIFIER_CLASS_VALUE (name);
if (old_value)
{
if (is_overloaded_fn (old_value))
old_value = OVL_CURRENT (old_value);
if (DECL_P (old_value) && DECL_CONTEXT (old_value) == t)
/* OK */;
else
old_value = NULL_TREE;
}
if (is_overloaded_fn (fdecl))
flist = fdecl;
if (! old_value)
;
else if (is_overloaded_fn (old_value))
{
if (flist)
/* It's OK to use functions from a base when there are functions with
the same name already present in the current class. */;
else
{
cp_error_at ("`%D' invalid in `%#T'", using_decl, t);
cp_error_at (" because of local method `%#D' with same name",
OVL_CURRENT (old_value));
return;
}
}
else if (!DECL_ARTIFICIAL (old_value))
{
cp_error_at ("`%D' invalid in `%#T'", using_decl, t);
cp_error_at (" because of local member `%#D' with same name", old_value);
return;
}
/* Make type T see field decl FDECL with access ACCESS. */
if (flist)
for (; flist; flist = OVL_NEXT (flist))
{
add_method (t, OVL_CURRENT (flist), /*error_p=*/0);
alter_access (t, OVL_CURRENT (flist), access);
}
else
alter_access (t, fdecl, access);
}
/* Run through the base clases of T, updating
CANT_HAVE_DEFAULT_CTOR_P, CANT_HAVE_CONST_CTOR_P, and
NO_CONST_ASN_REF_P. Also set flag bits in T based on properties of
the bases. */
static void
check_bases (tree t,
int* cant_have_default_ctor_p,
int* cant_have_const_ctor_p,
int* no_const_asn_ref_p)
{
int n_baseclasses;
int i;
int seen_non_virtual_nearly_empty_base_p;
tree binfos;
binfos = TYPE_BINFO_BASETYPES (t);
n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
seen_non_virtual_nearly_empty_base_p = 0;
/* An aggregate cannot have baseclasses. */
CLASSTYPE_NON_AGGREGATE (t) |= (n_baseclasses != 0);
for (i = 0; i < n_baseclasses; ++i)
{
tree base_binfo;
tree basetype;
/* Figure out what base we're looking at. */
base_binfo = TREE_VEC_ELT (binfos, i);
basetype = TREE_TYPE (base_binfo);
/* If the type of basetype is incomplete, then we already
complained about that fact (and we should have fixed it up as
well). */
if (!COMPLETE_TYPE_P (basetype))
{
int j;
/* The base type is of incomplete type. It is
probably best to pretend that it does not
exist. */
if (i == n_baseclasses-1)
TREE_VEC_ELT (binfos, i) = NULL_TREE;
TREE_VEC_LENGTH (binfos) -= 1;
n_baseclasses -= 1;
for (j = i; j+1 < n_baseclasses; j++)
TREE_VEC_ELT (binfos, j) = TREE_VEC_ELT (binfos, j+1);
continue;
}
/* Effective C++ rule 14. We only need to check TYPE_POLYMORPHIC_P
here because the case of virtual functions but non-virtual
dtor is handled in finish_struct_1. */
if (warn_ecpp && ! TYPE_POLYMORPHIC_P (basetype)
&& TYPE_HAS_DESTRUCTOR (basetype))
warning ("base class `%#T' has a non-virtual destructor",
basetype);
/* If the base class doesn't have copy constructors or
assignment operators that take const references, then the
derived class cannot have such a member automatically
generated. */
if (! TYPE_HAS_CONST_INIT_REF (basetype))
*cant_have_const_ctor_p = 1;
if (TYPE_HAS_ASSIGN_REF (basetype)
&& !TYPE_HAS_CONST_ASSIGN_REF (basetype))
*no_const_asn_ref_p = 1;
/* Similarly, if the base class doesn't have a default
constructor, then the derived class won't have an
automatically generated default constructor. */
if (TYPE_HAS_CONSTRUCTOR (basetype)
&& ! TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype))
{
*cant_have_default_ctor_p = 1;
if (! TYPE_HAS_CONSTRUCTOR (t))
pedwarn ("base `%T' with only non-default constructor in class without a constructor",
basetype);
}
if (TREE_VIA_VIRTUAL (base_binfo))
/* A virtual base does not effect nearly emptiness. */
;
else if (CLASSTYPE_NEARLY_EMPTY_P (basetype))
{
if (seen_non_virtual_nearly_empty_base_p)
/* And if there is more than one nearly empty base, then the
derived class is not nearly empty either. */
CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
else
/* Remember we've seen one. */
seen_non_virtual_nearly_empty_base_p = 1;
}
else if (!is_empty_class (basetype))
/* If the base class is not empty or nearly empty, then this
class cannot be nearly empty. */
CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
/* A lot of properties from the bases also apply to the derived
class. */
TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (basetype);
TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
|= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype);
TYPE_HAS_COMPLEX_ASSIGN_REF (t)
|= TYPE_HAS_COMPLEX_ASSIGN_REF (basetype);
TYPE_HAS_COMPLEX_INIT_REF (t) |= TYPE_HAS_COMPLEX_INIT_REF (basetype);
TYPE_POLYMORPHIC_P (t) |= TYPE_POLYMORPHIC_P (basetype);
CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t)
|= CLASSTYPE_CONTAINS_EMPTY_CLASS_P (basetype);
}
}
/* Set BINFO_PRIMARY_BASE_OF for all binfos in the hierarchy
dominated by TYPE that are primary bases. */
static void
mark_primary_bases (tree type)
{
tree binfo;
/* Walk the bases in inheritance graph order. */
for (binfo = TYPE_BINFO (type); binfo; binfo = TREE_CHAIN (binfo))
{
tree base_binfo = get_primary_binfo (binfo);
if (!base_binfo)
/* Not a dynamic base. */;
else if (BINFO_PRIMARY_P (base_binfo))
BINFO_LOST_PRIMARY_P (binfo) = 1;
else
{
BINFO_PRIMARY_BASE_OF (base_binfo) = binfo;
/* A virtual binfo might have been copied from within
another hierarchy. As we're about to use it as a primary
base, make sure the offsets match. */
if (TREE_VIA_VIRTUAL (base_binfo))
{
tree delta = size_diffop (convert (ssizetype,
BINFO_OFFSET (binfo)),
convert (ssizetype,
BINFO_OFFSET (base_binfo)));
propagate_binfo_offsets (base_binfo, delta);
}
}
}
}
/* Make the BINFO the primary base of T. */
static void
set_primary_base (tree t, tree binfo)
{
tree basetype;
CLASSTYPE_PRIMARY_BINFO (t) = binfo;
basetype = BINFO_TYPE (binfo);
TYPE_BINFO_VTABLE (t) = TYPE_BINFO_VTABLE (basetype);
TYPE_BINFO_VIRTUALS (t) = TYPE_BINFO_VIRTUALS (basetype);
TYPE_VFIELD (t) = TYPE_VFIELD (basetype);
}
/* Determine the primary class for T. */
static void
determine_primary_base (tree t)
{
int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
tree vbases;
tree type_binfo;
/* If there are no baseclasses, there is certainly no primary base. */
if (n_baseclasses == 0)
return;
type_binfo = TYPE_BINFO (t);
for (i = 0; i < n_baseclasses; i++)
{
tree base_binfo = BINFO_BASETYPE (type_binfo, i);
tree basetype = BINFO_TYPE (base_binfo);
if (TYPE_CONTAINS_VPTR_P (basetype))
{
/* We prefer a non-virtual base, although a virtual one will
do. */
if (TREE_VIA_VIRTUAL (base_binfo))
continue;
if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
{
set_primary_base (t, base_binfo);
CLASSTYPE_VFIELDS (t) = copy_list (CLASSTYPE_VFIELDS (basetype));
}
else
{
tree vfields;
/* Only add unique vfields, and flatten them out as we go. */
for (vfields = CLASSTYPE_VFIELDS (basetype);
vfields;
vfields = TREE_CHAIN (vfields))
if (VF_BINFO_VALUE (vfields) == NULL_TREE
|| ! TREE_VIA_VIRTUAL (VF_BINFO_VALUE (vfields)))
CLASSTYPE_VFIELDS (t)
= tree_cons (base_binfo,
VF_BASETYPE_VALUE (vfields),
CLASSTYPE_VFIELDS (t));
}
}
}
if (!TYPE_VFIELD (t))
CLASSTYPE_PRIMARY_BINFO (t) = NULL_TREE;
/* Find the indirect primary bases - those virtual bases which are primary
bases of something else in this hierarchy. */
for (vbases = CLASSTYPE_VBASECLASSES (t);
vbases;
vbases = TREE_CHAIN (vbases))
{
tree vbase_binfo = TREE_VALUE (vbases);
/* See if this virtual base is an indirect primary base. To be so,
it must be a primary base within the hierarchy of one of our
direct bases. */
for (i = 0; i < n_baseclasses; ++i)
{
tree basetype = TYPE_BINFO_BASETYPE (t, i);
tree v;
for (v = CLASSTYPE_VBASECLASSES (basetype);
v;
v = TREE_CHAIN (v))
{
tree base_vbase = TREE_VALUE (v);
if (BINFO_PRIMARY_P (base_vbase)
&& same_type_p (BINFO_TYPE (base_vbase),
BINFO_TYPE (vbase_binfo)))
{
BINFO_INDIRECT_PRIMARY_P (vbase_binfo) = 1;
break;
}
}
/* If we've discovered that this virtual base is an indirect
primary base, then we can move on to the next virtual
base. */
if (BINFO_INDIRECT_PRIMARY_P (vbase_binfo))
break;
}
}
/* A "nearly-empty" virtual base class can be the primary base
class, if no non-virtual polymorphic base can be found. */
if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
{
/* If not NULL, this is the best primary base candidate we have
found so far. */
tree candidate = NULL_TREE;
tree base_binfo;
/* Loop over the baseclasses. */
for (base_binfo = TYPE_BINFO (t);
base_binfo;
base_binfo = TREE_CHAIN (base_binfo))
{
tree basetype = BINFO_TYPE (base_binfo);
if (TREE_VIA_VIRTUAL (base_binfo)
&& CLASSTYPE_NEARLY_EMPTY_P (basetype))
{
/* If this is not an indirect primary base, then it's
definitely our primary base. */
if (!BINFO_INDIRECT_PRIMARY_P (base_binfo))
{
candidate = base_binfo;
break;
}
/* If this is an indirect primary base, it still could be
our primary base -- unless we later find there's another
nearly-empty virtual base that isn't an indirect
primary base. */
if (!candidate)
candidate = base_binfo;
}
}
/* If we've got a primary base, use it. */
if (candidate)
{
set_primary_base (t, candidate);
CLASSTYPE_VFIELDS (t)
= copy_list (CLASSTYPE_VFIELDS (BINFO_TYPE (candidate)));
}
}
/* Mark the primary base classes at this point. */
mark_primary_bases (t);
}
/* Set memoizing fields and bits of T (and its variants) for later
use. */
static void
finish_struct_bits (tree t)
{
int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
/* Fix up variants (if any). */
tree variants = TYPE_NEXT_VARIANT (t);
while (variants)
{
/* These fields are in the _TYPE part of the node, not in
the TYPE_LANG_SPECIFIC component, so they are not shared. */
TYPE_HAS_CONSTRUCTOR (variants) = TYPE_HAS_CONSTRUCTOR (t);
TYPE_HAS_DESTRUCTOR (variants) = TYPE_HAS_DESTRUCTOR (t);
TYPE_NEEDS_CONSTRUCTING (variants) = TYPE_NEEDS_CONSTRUCTING (t);
TYPE_HAS_NONTRIVIAL_DESTRUCTOR (variants)
= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (variants)
= TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (t);
TYPE_POLYMORPHIC_P (variants) = TYPE_POLYMORPHIC_P (t);
TYPE_USES_VIRTUAL_BASECLASSES (variants) = TYPE_USES_VIRTUAL_BASECLASSES (t);
/* Copy whatever these are holding today. */
TYPE_MIN_VALUE (variants) = TYPE_MIN_VALUE (t);
TYPE_MAX_VALUE (variants) = TYPE_MAX_VALUE (t);
TYPE_FIELDS (variants) = TYPE_FIELDS (t);
TYPE_SIZE (variants) = TYPE_SIZE (t);
TYPE_SIZE_UNIT (variants) = TYPE_SIZE_UNIT (t);
variants = TYPE_NEXT_VARIANT (variants);
}
if (n_baseclasses && TYPE_POLYMORPHIC_P (t))
/* For a class w/o baseclasses, `finish_struct' has set
CLASS_TYPE_ABSTRACT_VIRTUALS correctly (by
definition). Similarly for a class whose base classes do not
have vtables. When neither of these is true, we might have
removed abstract virtuals (by providing a definition), added
some (by declaring new ones), or redeclared ones from a base
class. We need to recalculate what's really an abstract virtual
at this point (by looking in the vtables). */
get_pure_virtuals (t);
if (n_baseclasses)
{
/* Notice whether this class has type conversion functions defined. */
tree binfo = TYPE_BINFO (t);
tree binfos = BINFO_BASETYPES (binfo);
tree basetype;
for (i = n_baseclasses-1; i >= 0; i--)
{
basetype = BINFO_TYPE (TREE_VEC_ELT (binfos, i));
TYPE_HAS_CONVERSION (t) |= TYPE_HAS_CONVERSION (basetype);
}
}
/* If this type has a copy constructor or a destructor, force its mode to
be BLKmode, and force its TREE_ADDRESSABLE bit to be nonzero. This
will cause it to be passed by invisible reference and prevent it from
being returned in a register. */
if (! TYPE_HAS_TRIVIAL_INIT_REF (t) || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
{
tree variants;
DECL_MODE (TYPE_MAIN_DECL (t)) = BLKmode;
for (variants = t; variants; variants = TYPE_NEXT_VARIANT (variants))
{
TYPE_MODE (variants) = BLKmode;
TREE_ADDRESSABLE (variants) = 1;
}
}
}
/* Issue warnings about T having private constructors, but no friends,
and so forth.
HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or
static members. HAS_NONPRIVATE_STATIC_FN is nonzero if T has any
non-private static member functions. */
static void
maybe_warn_about_overly_private_class (tree t)
{
int has_member_fn = 0;
int has_nonprivate_method = 0;
tree fn;
if (!warn_ctor_dtor_privacy
/* If the class has friends, those entities might create and
access instances, so we should not warn. */
|| (CLASSTYPE_FRIEND_CLASSES (t)
|| DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
/* We will have warned when the template was declared; there's
no need to warn on every instantiation. */
|| CLASSTYPE_TEMPLATE_INSTANTIATION (t))
/* There's no reason to even consider warning about this
class. */
return;
/* We only issue one warning, if more than one applies, because
otherwise, on code like:
class A {
// Oops - forgot `public:'
A();
A(const A&);
~A();
};
we warn several times about essentially the same problem. */
/* Check to see if all (non-constructor, non-destructor) member
functions are private. (Since there are no friends or
non-private statics, we can't ever call any of the private member
functions.) */
for (fn = TYPE_METHODS (t); fn; fn = TREE_CHAIN (fn))
/* We're not interested in compiler-generated methods; they don't
provide any way to call private members. */
if (!DECL_ARTIFICIAL (fn))
{
if (!TREE_PRIVATE (fn))
{
if (DECL_STATIC_FUNCTION_P (fn))
/* A non-private static member function is just like a
friend; it can create and invoke private member
functions, and be accessed without a class
instance. */
return;
has_nonprivate_method = 1;
/* Keep searching for a static member function. */
}
else if (!DECL_CONSTRUCTOR_P (fn) && !DECL_DESTRUCTOR_P (fn))
has_member_fn = 1;
}
if (!has_nonprivate_method && has_member_fn)
{
/* There are no non-private methods, and there's at least one
private member function that isn't a constructor or
destructor. (If all the private members are
constructors/destructors we want to use the code below that
issues error messages specifically referring to
constructors/destructors.) */
int i;
tree binfo = TYPE_BINFO (t);
for (i = 0; i < BINFO_N_BASETYPES (binfo); i++)
if (BINFO_BASEACCESS (binfo, i) != access_private_node)
{
has_nonprivate_method = 1;
break;
}
if (!has_nonprivate_method)
{
warning ("all member functions in class `%T' are private", t);
return;
}
}
/* Even if some of the member functions are non-private, the class
won't be useful for much if all the constructors or destructors
are private: such an object can never be created or destroyed. */
if (TYPE_HAS_DESTRUCTOR (t)
&& TREE_PRIVATE (CLASSTYPE_DESTRUCTORS (t)))
{
warning ("`%#T' only defines a private destructor and has no friends",
t);
return;
}
if (TYPE_HAS_CONSTRUCTOR (t))
{
int nonprivate_ctor = 0;
/* If a non-template class does not define a copy
constructor, one is defined for it, enabling it to avoid
this warning. For a template class, this does not
happen, and so we would normally get a warning on:
template <class T> class C { private: C(); };
To avoid this asymmetry, we check TYPE_HAS_INIT_REF. All
complete non-template or fully instantiated classes have this
flag set. */
if (!TYPE_HAS_INIT_REF (t))
nonprivate_ctor = 1;
else
for (fn = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 0);
fn;
fn = OVL_NEXT (fn))
{
tree ctor = OVL_CURRENT (fn);
/* Ideally, we wouldn't count copy constructors (or, in
fact, any constructor that takes an argument of the
class type as a parameter) because such things cannot
be used to construct an instance of the class unless
you already have one. But, for now at least, we're
more generous. */
if (! TREE_PRIVATE (ctor))
{
nonprivate_ctor = 1;
break;
}
}
if (nonprivate_ctor == 0)
{
warning ("`%#T' only defines private constructors and has no friends",
t);
return;
}
}
}
static struct {
gt_pointer_operator new_value;
void *cookie;
} resort_data;
/* Comparison function to compare two TYPE_METHOD_VEC entries by name. */
static int
method_name_cmp (const void* m1_p, const void* m2_p)
{
const tree *const m1 = m1_p;
const tree *const m2 = m2_p;
if (*m1 == NULL_TREE && *m2 == NULL_TREE)
return 0;
if (*m1 == NULL_TREE)
return -1;
if (*m2 == NULL_TREE)
return 1;
if (DECL_NAME (OVL_CURRENT (*m1)) < DECL_NAME (OVL_CURRENT (*m2)))
return -1;
return 1;
}
/* This routine compares two fields like method_name_cmp but using the
pointer operator in resort_field_decl_data. */
static int
resort_method_name_cmp (const void* m1_p, const void* m2_p)
{
const tree *const m1 = m1_p;
const tree *const m2 = m2_p;
if (*m1 == NULL_TREE && *m2 == NULL_TREE)
return 0;
if (*m1 == NULL_TREE)
return -1;
if (*m2 == NULL_TREE)
return 1;
{
tree d1 = DECL_NAME (OVL_CURRENT (*m1));
tree d2 = DECL_NAME (OVL_CURRENT (*m2));
resort_data.new_value (&d1, resort_data.cookie);
resort_data.new_value (&d2, resort_data.cookie);
if (d1 < d2)
return -1;
}
return 1;
}
/* Resort TYPE_METHOD_VEC because pointers have been reordered. */
void
resort_type_method_vec (void* obj,
void* orig_obj ATTRIBUTE_UNUSED ,
gt_pointer_operator new_value,
void* cookie)
{
tree method_vec = obj;
int len = TREE_VEC_LENGTH (method_vec);
int slot;
/* The type conversion ops have to live at the front of the vec, so we
can't sort them. */
for (slot = 2; slot < len; ++slot)
{
tree fn = TREE_VEC_ELT (method_vec, slot);
if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
break;
}
if (len - slot > 1)
{
resort_data.new_value = new_value;
resort_data.cookie = cookie;
qsort (&TREE_VEC_ELT (method_vec, slot), len - slot, sizeof (tree),
resort_method_name_cmp);
}
}
/* Warn about duplicate methods in fn_fields. Also compact method
lists so that lookup can be made faster.
Data Structure: List of method lists. The outer list is a
TREE_LIST, whose TREE_PURPOSE field is the field name and the
TREE_VALUE is the DECL_CHAIN of the FUNCTION_DECLs. TREE_CHAIN
links the entire list of methods for TYPE_METHODS. Friends are
chained in the same way as member functions (? TREE_CHAIN or
DECL_CHAIN), but they live in the TREE_TYPE field of the outer
list. That allows them to be quickly deleted, and requires no
extra storage.
Sort methods that are not special (i.e., constructors, destructors,
and type conversion operators) so that we can find them faster in
search. */
static void
finish_struct_methods (tree t)
{
tree fn_fields;
tree method_vec;
int slot, len;
if (!TYPE_METHODS (t))
{
/* Clear these for safety; perhaps some parsing error could set
these incorrectly. */
TYPE_HAS_CONSTRUCTOR (t) = 0;
TYPE_HAS_DESTRUCTOR (t) = 0;
CLASSTYPE_METHOD_VEC (t) = NULL_TREE;
return;
}
method_vec = CLASSTYPE_METHOD_VEC (t);
my_friendly_assert (method_vec != NULL_TREE, 19991215);
len = TREE_VEC_LENGTH (method_vec);
/* First fill in entry 0 with the constructors, entry 1 with destructors,
and the next few with type conversion operators (if any). */
for (fn_fields = TYPE_METHODS (t); fn_fields;
fn_fields = TREE_CHAIN (fn_fields))
/* Clear out this flag. */
DECL_IN_AGGR_P (fn_fields) = 0;
if (TYPE_HAS_DESTRUCTOR (t) && !CLASSTYPE_DESTRUCTORS (t))
/* We thought there was a destructor, but there wasn't. Some
parse errors cause this anomalous situation. */
TYPE_HAS_DESTRUCTOR (t) = 0;
/* Issue warnings about private constructors and such. If there are
no methods, then some public defaults are generated. */
maybe_warn_about_overly_private_class (t);
/* Now sort the methods. */
while (len > 2 && TREE_VEC_ELT (method_vec, len-1) == NULL_TREE)
len--;
TREE_VEC_LENGTH (method_vec) = len;
/* The type conversion ops have to live at the front of the vec, so we
can't sort them. */
for (slot = 2; slot < len; ++slot)
{
tree fn = TREE_VEC_ELT (method_vec, slot);
if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
break;
}
if (len - slot > 1)
qsort (&TREE_VEC_ELT (method_vec, slot), len-slot, sizeof (tree),
method_name_cmp);
}
/* Make BINFO's vtable have N entries, including RTTI entries,
vbase and vcall offsets, etc. Set its type and call the backend
to lay it out. */
static void
layout_vtable_decl (tree binfo, int n)
{
tree atype;
tree vtable;
atype = build_cplus_array_type (vtable_entry_type,
build_index_type (size_int (n - 1)));
layout_type (atype);
/* We may have to grow the vtable. */
vtable = get_vtbl_decl_for_binfo (binfo);
if (!same_type_p (TREE_TYPE (vtable), atype))
{
TREE_TYPE (vtable) = atype;
DECL_SIZE (vtable) = DECL_SIZE_UNIT (vtable) = NULL_TREE;
layout_decl (vtable, 0);
}
}
/* True iff FNDECL and BASE_FNDECL (both non-static member functions)
have the same signature. */
int
same_signature_p (tree fndecl, tree base_fndecl)
{
/* One destructor overrides another if they are the same kind of
destructor. */
if (DECL_DESTRUCTOR_P (base_fndecl) && DECL_DESTRUCTOR_P (fndecl)
&& special_function_p (base_fndecl) == special_function_p (fndecl))
return 1;
/* But a non-destructor never overrides a destructor, nor vice
versa, nor do different kinds of destructors override
one-another. For example, a complete object destructor does not
override a deleting destructor. */
if (DECL_DESTRUCTOR_P (base_fndecl) || DECL_DESTRUCTOR_P (fndecl))
return 0;
if (DECL_NAME (fndecl) == DECL_NAME (base_fndecl)
|| (DECL_CONV_FN_P (fndecl)
&& DECL_CONV_FN_P (base_fndecl)
&& same_type_p (DECL_CONV_FN_TYPE (fndecl),
DECL_CONV_FN_TYPE (base_fndecl))))
{
tree types, base_types;
types = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
base_types = TYPE_ARG_TYPES (TREE_TYPE (base_fndecl));
if ((TYPE_QUALS (TREE_TYPE (TREE_VALUE (base_types)))
== TYPE_QUALS (TREE_TYPE (TREE_VALUE (types))))
&& compparms (TREE_CHAIN (base_types), TREE_CHAIN (types)))
return 1;
}
return 0;
}
/* Returns TRUE if DERIVED is a binfo containing the binfo BASE as a
subobject. */
static bool
base_derived_from (tree derived, tree base)
{
tree probe;
for (probe = base; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
{
if (probe == derived)
return true;
else if (TREE_VIA_VIRTUAL (probe))
/* If we meet a virtual base, we can't follow the inheritance
any more. See if the complete type of DERIVED contains
such a virtual base. */
return purpose_member (BINFO_TYPE (probe),
CLASSTYPE_VBASECLASSES (BINFO_TYPE (derived)))
!= NULL_TREE;
}
return false;
}
typedef struct count_depth_data {
/* The depth of the current subobject, with "1" as the depth of the
most derived object in the hierarchy. */
size_t depth;
/* The maximum depth found so far. */
size_t max_depth;
} count_depth_data;
/* Called from find_final_overrider via dfs_walk. */
static tree
dfs_depth_post (tree binfo ATTRIBUTE_UNUSED, void *data)
{
count_depth_data *cd = (count_depth_data *) data;
if (cd->depth > cd->max_depth)
cd->max_depth = cd->depth;
cd->depth--;
return NULL_TREE;
}
/* Called from find_final_overrider via dfs_walk. */
static tree
dfs_depth_q (tree derived, int i, void *data)
{
count_depth_data *cd = (count_depth_data *) data;
cd->depth++;
return BINFO_BASETYPE (derived, i);
}
typedef struct find_final_overrider_data_s {
/* The function for which we are trying to find a final overrider. */
tree fn;
/* The base class in which the function was declared. */
tree declaring_base;
/* The most derived class in the hierarchy. */
tree most_derived_type;
/* The candidate overriders. */
tree candidates;
/* Each entry in this array is the next-most-derived class for a
virtual base class along the current path. */
tree *vpath_list;
/* A pointer one past the top of the VPATH_LIST. */
tree *vpath;
} find_final_overrider_data;
/* Add the overrider along the current path to FFOD->CANDIDATES.
Returns true if an overrider was found; false otherwise. */
static bool
dfs_find_final_overrider_1 (tree binfo,
tree *vpath,
find_final_overrider_data *ffod)
{
tree method;
/* If BINFO is not the most derived type, try a more derived class.
A definition there will overrider a definition here. */
if (!same_type_p (BINFO_TYPE (binfo), ffod->most_derived_type))
{
tree derived;
if (TREE_VIA_VIRTUAL (binfo))
derived = *--vpath;
else
derived = BINFO_INHERITANCE_CHAIN (binfo);
if (dfs_find_final_overrider_1 (derived, vpath, ffod))
return true;
}
method = look_for_overrides_here (BINFO_TYPE (binfo), ffod->fn);
if (method)
{
tree *candidate = &ffod->candidates;
/* Remove any candidates overridden by this new function. */
while (*candidate)
{
/* If *CANDIDATE overrides METHOD, then METHOD
cannot override anything else on the list. */
if (base_derived_from (TREE_VALUE (*candidate), binfo))
return true;
/* If METHOD overrides *CANDIDATE, remove *CANDIDATE. */
if (base_derived_from (binfo, TREE_VALUE (*candidate)))
*candidate = TREE_CHAIN (*candidate);
else
candidate = &TREE_CHAIN (*candidate);
}
/* Add the new function. */
ffod->candidates = tree_cons (method, binfo, ffod->candidates);
return true;
}
return false;
}
/* Called from find_final_overrider via dfs_walk. */
static tree
dfs_find_final_overrider (tree binfo, void* data)
{
find_final_overrider_data *ffod = (find_final_overrider_data *) data;
if (binfo == ffod->declaring_base)
dfs_find_final_overrider_1 (binfo, ffod->vpath, ffod);
return NULL_TREE;
}
static tree
dfs_find_final_overrider_q (tree derived, int ix, void *data)
{
tree binfo = BINFO_BASETYPE (derived, ix);
find_final_overrider_data *ffod = (find_final_overrider_data *) data;
if (TREE_VIA_VIRTUAL (binfo))
*ffod->vpath++ = derived;
return binfo;
}
static tree
dfs_find_final_overrider_post (tree binfo, void *data)
{
find_final_overrider_data *ffod = (find_final_overrider_data *) data;
if (TREE_VIA_VIRTUAL (binfo))
ffod->vpath--;
return NULL_TREE;
}
/* Returns a TREE_LIST whose TREE_PURPOSE is the final overrider for
FN and whose TREE_VALUE is the binfo for the base where the
overriding occurs. BINFO (in the hierarchy dominated by the binfo
DERIVED) is the base object in which FN is declared. */
static tree
find_final_overrider (tree derived, tree binfo, tree fn)
{
find_final_overrider_data ffod;
count_depth_data cd;
/* Getting this right is a little tricky. This is valid:
struct S { virtual void f (); };
struct T { virtual void f (); };
struct U : public S, public T { };
even though calling `f' in `U' is ambiguous. But,
struct R { virtual void f(); };
struct S : virtual public R { virtual void f (); };
struct T : virtual public R { virtual void f (); };
struct U : public S, public T { };
is not -- there's no way to decide whether to put `S::f' or
`T::f' in the vtable for `R'.
The solution is to look at all paths to BINFO. If we find
different overriders along any two, then there is a problem. */
if (DECL_THUNK_P (fn))
fn = THUNK_TARGET (fn);
/* Determine the depth of the hierarchy. */
cd.depth = 0;
cd.max_depth = 0;
dfs_walk (derived, dfs_depth_post, dfs_depth_q, &cd);
ffod.fn = fn;
ffod.declaring_base = binfo;
ffod.most_derived_type = BINFO_TYPE (derived);
ffod.candidates = NULL_TREE;
ffod.vpath_list = (tree *) xcalloc (cd.max_depth, sizeof (tree));
ffod.vpath = ffod.vpath_list;
dfs_walk_real (derived,
dfs_find_final_overrider,
dfs_find_final_overrider_post,
dfs_find_final_overrider_q,
&ffod);
free (ffod.vpath_list);
/* If there was no winner, issue an error message. */
if (!ffod.candidates || TREE_CHAIN (ffod.candidates))
{
error ("no unique final overrider for `%D' in `%T'", fn,
BINFO_TYPE (derived));
return error_mark_node;
}
return ffod.candidates;
}
/* Return the index of the vcall offset for FN when TYPE is used as a
virtual base. */
static tree
get_vcall_index (tree fn, tree type)
{
tree v;
for (v = CLASSTYPE_VCALL_INDICES (type); v; v = TREE_CHAIN (v))
if ((DECL_DESTRUCTOR_P (fn) && DECL_DESTRUCTOR_P (TREE_PURPOSE (v)))
|| same_signature_p (fn, TREE_PURPOSE (v)))
break;
/* There should always be an appropriate index. */
my_friendly_assert (v, 20021103);
return TREE_VALUE (v);
}
/* Update an entry in the vtable for BINFO, which is in the hierarchy
dominated by T. FN has been overridden in BINFO; VIRTUALS points to the
corresponding position in the BINFO_VIRTUALS list. */
static void
update_vtable_entry_for_fn (tree t, tree binfo, tree fn, tree* virtuals,
unsigned ix)
{
tree b;
tree overrider;
tree delta;
tree virtual_base;
tree first_defn;
tree overrider_fn, overrider_target;
tree target_fn = DECL_THUNK_P (fn) ? THUNK_TARGET (fn) : fn;
tree over_return, base_return;
bool lost = false;
/* Find the nearest primary base (possibly binfo itself) which defines
this function; this is the class the caller will convert to when
calling FN through BINFO. */
for (b = binfo; ; b = get_primary_binfo (b))
{
my_friendly_assert (b, 20021227);
if (look_for_overrides_here (BINFO_TYPE (b), target_fn))
break;
/* The nearest definition is from a lost primary. */
if (BINFO_LOST_PRIMARY_P (b))
lost = true;
}
first_defn = b;
/* Find the final overrider. */
overrider = find_final_overrider (TYPE_BINFO (t), b, target_fn);
if (overrider == error_mark_node)
return;
overrider_target = overrider_fn = TREE_PURPOSE (overrider);
/* Check for adjusting covariant return types. */
over_return = TREE_TYPE (TREE_TYPE (overrider_target));
base_return = TREE_TYPE (TREE_TYPE (target_fn));
if (POINTER_TYPE_P (over_return)
&& TREE_CODE (over_return) == TREE_CODE (base_return)
&& CLASS_TYPE_P (TREE_TYPE (over_return))
&& CLASS_TYPE_P (TREE_TYPE (base_return)))
{
/* If FN is a covariant thunk, we must figure out the adjustment
to the final base FN was converting to. As OVERRIDER_TARGET might
also be converting to the return type of FN, we have to
combine the two conversions here. */
tree fixed_offset, virtual_offset;
if (DECL_THUNK_P (fn))
{
my_friendly_assert (DECL_RESULT_THUNK_P (fn), 20031211);
fixed_offset = ssize_int (THUNK_FIXED_OFFSET (fn));
virtual_offset = THUNK_VIRTUAL_OFFSET (fn);
}
else
fixed_offset = virtual_offset = NULL_TREE;
if (virtual_offset)
/* Find the equivalent binfo within the return type of the
overriding function. We will want the vbase offset from
there. */
virtual_offset =
TREE_VALUE (purpose_member
(BINFO_TYPE (virtual_offset),
CLASSTYPE_VBASECLASSES (TREE_TYPE (over_return))));
else if (!same_type_p (TREE_TYPE (over_return),
TREE_TYPE (base_return)))
{
/* There was no existing virtual thunk (which takes
precedence). */
tree thunk_binfo;
base_kind kind;
thunk_binfo = lookup_base (TREE_TYPE (over_return),
TREE_TYPE (base_return),
ba_check | ba_quiet, &kind);
if (thunk_binfo && (kind == bk_via_virtual
|| !BINFO_OFFSET_ZEROP (thunk_binfo)))
{
tree offset = convert (ssizetype, BINFO_OFFSET (thunk_binfo));
if (kind == bk_via_virtual)
{
/* We convert via virtual base. Find the virtual
base and adjust the fixed offset to be from there. */
while (!TREE_VIA_VIRTUAL (thunk_binfo))
thunk_binfo = BINFO_INHERITANCE_CHAIN (thunk_binfo);
virtual_offset = thunk_binfo;
offset = size_diffop
(offset, convert
(ssizetype, BINFO_OFFSET (virtual_offset)));
}
if (fixed_offset)
/* There was an existing fixed offset, this must be
from the base just converted to, and the base the
FN was thunking to. */
fixed_offset = size_binop (PLUS_EXPR, fixed_offset, offset);
else
fixed_offset = offset;
}
}
if (fixed_offset || virtual_offset)
/* Replace the overriding function with a covariant thunk. We
will emit the overriding function in its own slot as
well. */
overrider_fn = make_thunk (overrider_target, /*this_adjusting=*/0,
fixed_offset, virtual_offset);
}
else
my_friendly_assert (!DECL_THUNK_P (fn), 20021231);
/* Assume that we will produce a thunk that convert all the way to
the final overrider, and not to an intermediate virtual base. */
virtual_base = NULL_TREE;
/* See if we can convert to an intermediate virtual base first, and then
use the vcall offset located there to finish the conversion. */
for (; b; b = BINFO_INHERITANCE_CHAIN (b))
{
/* If we find the final overrider, then we can stop
walking. */
if (same_type_p (BINFO_TYPE (b),
BINFO_TYPE (TREE_VALUE (overrider))))
break;
/* If we find a virtual base, and we haven't yet found the
overrider, then there is a virtual base between the
declaring base (first_defn) and the final overrider. */
if (TREE_VIA_VIRTUAL (b))
{
virtual_base = b;
break;
}
}
if (overrider_fn != overrider_target && !virtual_base)
{
/* The ABI specifies that a covariant thunk includes a mangling
for a this pointer adjustment. This-adjusting thunks that
override a function from a virtual base have a vcall
adjustment. When the virtual base in question is a primary
virtual base, we know the adjustments are zero, (and in the
non-covariant case, we would not use the thunk).
Unfortunately we didn't notice this could happen, when
designing the ABI and so never mandated that such a covariant
thunk should be emitted. Because we must use the ABI mandated
name, we must continue searching from the binfo where we
found the most recent definition of the function, towards the
primary binfo which first introduced the function into the
vtable. If that enters a virtual base, we must use a vcall
this-adjusting thunk. Bleah! */
tree probe = first_defn;
while ((probe = get_primary_binfo (probe))
&& (unsigned) list_length (BINFO_VIRTUALS (probe)) > ix)
if (TREE_VIA_VIRTUAL (probe))
virtual_base = probe;
if (virtual_base)
/* Even if we find a virtual base, the correct delta is
between the overrider and the binfo we're building a vtable
for. */
goto virtual_covariant;
}
/* Compute the constant adjustment to the `this' pointer. The
`this' pointer, when this function is called, will point at BINFO
(or one of its primary bases, which are at the same offset). */
if (virtual_base)
/* The `this' pointer needs to be adjusted from the declaration to
the nearest virtual base. */
delta = size_diffop (convert (ssizetype, BINFO_OFFSET (virtual_base)),
convert (ssizetype, BINFO_OFFSET (first_defn)));
else if (lost)
/* If the nearest definition is in a lost primary, we don't need an
entry in our vtable. Except possibly in a constructor vtable,
if we happen to get our primary back. In that case, the offset
will be zero, as it will be a primary base. */
delta = size_zero_node;
else
/* The `this' pointer needs to be adjusted from pointing to
BINFO to pointing at the base where the final overrider
appears. */
virtual_covariant:
delta = size_diffop (convert (ssizetype,
BINFO_OFFSET (TREE_VALUE (overrider))),
convert (ssizetype, BINFO_OFFSET (binfo)));
modify_vtable_entry (t, binfo, overrider_fn, delta, virtuals);
if (virtual_base)
BV_VCALL_INDEX (*virtuals)
= get_vcall_index (overrider_target, BINFO_TYPE (virtual_base));
}
/* Called from modify_all_vtables via dfs_walk. */
static tree
dfs_modify_vtables (tree binfo, void* data)
{
if (/* There's no need to modify the vtable for a non-virtual
primary base; we're not going to use that vtable anyhow.
We do still need to do this for virtual primary bases, as they
could become non-primary in a construction vtable. */
(!BINFO_PRIMARY_P (binfo) || TREE_VIA_VIRTUAL (binfo))
/* Similarly, a base without a vtable needs no modification. */
&& CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
{
tree t = (tree) data;
tree virtuals;
tree old_virtuals;
unsigned ix;
make_new_vtable (t, binfo);
/* Now, go through each of the virtual functions in the virtual
function table for BINFO. Find the final overrider, and
update the BINFO_VIRTUALS list appropriately. */
for (ix = 0, virtuals = BINFO_VIRTUALS (binfo),
old_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo)));
virtuals;
ix++, virtuals = TREE_CHAIN (virtuals),
old_virtuals = TREE_CHAIN (old_virtuals))
update_vtable_entry_for_fn (t,
binfo,
BV_FN (old_virtuals),
&virtuals, ix);
}
BINFO_MARKED (binfo) = 1;
return NULL_TREE;
}
/* Update all of the primary and secondary vtables for T. Create new
vtables as required, and initialize their RTTI information. Each
of the functions in VIRTUALS is declared in T and may override a
virtual function from a base class; find and modify the appropriate
entries to point to the overriding functions. Returns a list, in
declaration order, of the virtual functions that are declared in T,
but do not appear in the primary base class vtable, and which
should therefore be appended to the end of the vtable for T. */
static tree
modify_all_vtables (tree t, tree virtuals)
{
tree binfo = TYPE_BINFO (t);
tree *fnsp;
/* Update all of the vtables. */
dfs_walk (binfo, dfs_modify_vtables, unmarkedp, t);
dfs_walk (binfo, dfs_unmark, markedp, t);
/* Add virtual functions not already in our primary vtable. These
will be both those introduced by this class, and those overridden
from secondary bases. It does not include virtuals merely
inherited from secondary bases. */
for (fnsp = &virtuals; *fnsp; )
{
tree fn = TREE_VALUE (*fnsp);
if (!value_member (fn, BINFO_VIRTUALS (binfo))
|| DECL_VINDEX (fn) == error_mark_node)
{
/* We don't need to adjust the `this' pointer when
calling this function. */
BV_DELTA (*fnsp) = integer_zero_node;
BV_VCALL_INDEX (*fnsp) = NULL_TREE;
/* This is a function not already in our vtable. Keep it. */
fnsp = &TREE_CHAIN (*fnsp);
}
else
/* We've already got an entry for this function. Skip it. */
*fnsp = TREE_CHAIN (*fnsp);
}
return virtuals;
}
/* Get the base virtual function declarations in T that have the
indicated NAME. */
static tree
get_basefndecls (tree name, tree t)
{
tree methods;
tree base_fndecls = NULL_TREE;
int n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
int i;
/* Find virtual functions in T with the indicated NAME. */
i = lookup_fnfields_1 (t, name);
if (i != -1)
for (methods = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), i);
methods;
methods = OVL_NEXT (methods))
{
tree method = OVL_CURRENT (methods);
if (TREE_CODE (method) == FUNCTION_DECL
&& DECL_VINDEX (method))
base_fndecls = tree_cons (NULL_TREE, method, base_fndecls);
}
if (base_fndecls)
return base_fndecls;
for (i = 0; i < n_baseclasses; i++)
{
tree basetype = TYPE_BINFO_BASETYPE (t, i);
base_fndecls = chainon (get_basefndecls (name, basetype),
base_fndecls);
}
return base_fndecls;
}
/* If this declaration supersedes the declaration of
a method declared virtual in the base class, then
mark this field as being virtual as well. */
static void
check_for_override (tree decl, tree ctype)
{
if (TREE_CODE (decl) == TEMPLATE_DECL)
/* In [temp.mem] we have:
A specialization of a member function template does not
override a virtual function from a base class. */
return;
if ((DECL_DESTRUCTOR_P (decl)
|| IDENTIFIER_VIRTUAL_P (DECL_NAME (decl))
|| DECL_CONV_FN_P (decl))
&& look_for_overrides (ctype, decl)
&& !DECL_STATIC_FUNCTION_P (decl))
/* Set DECL_VINDEX to a value that is neither an INTEGER_CST nor
the error_mark_node so that we know it is an overriding
function. */
DECL_VINDEX (decl) = decl;
if (DECL_VIRTUAL_P (decl))
{
if (!DECL_VINDEX (decl))
DECL_VINDEX (decl) = error_mark_node;
IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = 1;
}
}
/* Warn about hidden virtual functions that are not overridden in t.
We know that constructors and destructors don't apply. */
void
warn_hidden (tree t)
{
tree method_vec = CLASSTYPE_METHOD_VEC (t);
int n_methods = method_vec ? TREE_VEC_LENGTH (method_vec) : 0;
int i;
/* We go through each separately named virtual function. */
for (i = 2; i < n_methods && TREE_VEC_ELT (method_vec, i); ++i)
{
tree fns;
tree name;
tree fndecl;
tree base_fndecls;
int j;
/* All functions in this slot in the CLASSTYPE_METHOD_VEC will
have the same name. Figure out what name that is. */
name = DECL_NAME (OVL_CURRENT (TREE_VEC_ELT (method_vec, i)));
/* There are no possibly hidden functions yet. */
base_fndecls = NULL_TREE;
/* Iterate through all of the base classes looking for possibly
hidden functions. */
for (j = 0; j < CLASSTYPE_N_BASECLASSES (t); j++)
{
tree basetype = TYPE_BINFO_BASETYPE (t, j);
base_fndecls = chainon (get_basefndecls (name, basetype),
base_fndecls);
}
/* If there are no functions to hide, continue. */
if (!base_fndecls)
continue;
/* Remove any overridden functions. */
for (fns = TREE_VEC_ELT (method_vec, i); fns; fns = OVL_NEXT (fns))
{
fndecl = OVL_CURRENT (fns);
if (DECL_VINDEX (fndecl))
{
tree *prev = &base_fndecls;
while (*prev)
/* If the method from the base class has the same
signature as the method from the derived class, it
has been overridden. */
if (same_signature_p (fndecl, TREE_VALUE (*prev)))
*prev = TREE_CHAIN (*prev);
else
prev = &TREE_CHAIN (*prev);
}
}
/* Now give a warning for all base functions without overriders,
as they are hidden. */
while (base_fndecls)
{
/* Here we know it is a hider, and no overrider exists. */
cp_warning_at ("`%D' was hidden", TREE_VALUE (base_fndecls));
cp_warning_at (" by `%D'",
OVL_CURRENT (TREE_VEC_ELT (method_vec, i)));
base_fndecls = TREE_CHAIN (base_fndecls);
}
}
}
/* Check for things that are invalid. There are probably plenty of other
things we should check for also. */
static void
finish_struct_anon (tree t)
{
tree field;
for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
{
if (TREE_STATIC (field))
continue;
if (TREE_CODE (field) != FIELD_DECL)
continue;
if (DECL_NAME (field) == NULL_TREE
&& ANON_AGGR_TYPE_P (TREE_TYPE (field)))
{
tree elt = TYPE_FIELDS (TREE_TYPE (field));
for (; elt; elt = TREE_CHAIN (elt))
{
/* We're generally only interested in entities the user
declared, but we also find nested classes by noticing
the TYPE_DECL that we create implicitly. You're
allowed to put one anonymous union inside another,
though, so we explicitly tolerate that. We use
TYPE_ANONYMOUS_P rather than ANON_AGGR_TYPE_P so that
we also allow unnamed types used for defining fields. */
if (DECL_ARTIFICIAL (elt)
&& (!DECL_IMPLICIT_TYPEDEF_P (elt)
|| TYPE_ANONYMOUS_P (TREE_TYPE (elt))))
continue;
if (TREE_CODE (elt) != FIELD_DECL)
{
cp_pedwarn_at ("`%#D' invalid; an anonymous union can only have non-static data members",
elt);
continue;
}
if (TREE_PRIVATE (elt))
cp_pedwarn_at ("private member `%#D' in anonymous union",
elt);
else if (TREE_PROTECTED (elt))
cp_pedwarn_at ("protected member `%#D' in anonymous union",
elt);
TREE_PRIVATE (elt) = TREE_PRIVATE (field);
TREE_PROTECTED (elt) = TREE_PROTECTED (field);
}
}
}
}
/* Add T to CLASSTYPE_DECL_LIST of current_class_type which
will be used later during class template instantiation.
When FRIEND_P is zero, T can be a static member data (VAR_DECL),
a non-static member data (FIELD_DECL), a member function
(FUNCTION_DECL), a nested type (RECORD_TYPE, ENUM_TYPE),
a typedef (TYPE_DECL) or a member class template (TEMPLATE_DECL)
When FRIEND_P is nonzero, T is either a friend class
(RECORD_TYPE, TEMPLATE_DECL) or a friend function
(FUNCTION_DECL, TEMPLATE_DECL). */
void
maybe_add_class_template_decl_list (tree type, tree t, int friend_p)
{
/* Save some memory by not creating TREE_LIST if TYPE is not template. */
if (CLASSTYPE_TEMPLATE_INFO (type))
CLASSTYPE_DECL_LIST (type)
= tree_cons (friend_p ? NULL_TREE : type,
t, CLASSTYPE_DECL_LIST (type));
}
/* Create default constructors, assignment operators, and so forth for
the type indicated by T, if they are needed.
CANT_HAVE_DEFAULT_CTOR, CANT_HAVE_CONST_CTOR, and
CANT_HAVE_CONST_ASSIGNMENT are nonzero if, for whatever reason, the
class cannot have a default constructor, copy constructor taking a
const reference argument, or an assignment operator taking a const
reference, respectively. If a virtual destructor is created, its
DECL is returned; otherwise the return value is NULL_TREE. */
static void
add_implicitly_declared_members (tree t,
int cant_have_default_ctor,
int cant_have_const_cctor,
int cant_have_const_assignment)
{
tree default_fn;
tree implicit_fns = NULL_TREE;
tree virtual_dtor = NULL_TREE;
tree *f;
++adding_implicit_members;
/* Destructor. */
if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) && !TYPE_HAS_DESTRUCTOR (t))
{
default_fn = implicitly_declare_fn (sfk_destructor, t, /*const_p=*/0);
check_for_override (default_fn, t);
/* If we couldn't make it work, then pretend we didn't need it. */
if (default_fn == void_type_node)
TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = 0;
else
{
TREE_CHAIN (default_fn) = implicit_fns;
implicit_fns = default_fn;
if (DECL_VINDEX (default_fn))
virtual_dtor = default_fn;
}
}
else
/* Any non-implicit destructor is non-trivial. */
TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) |= TYPE_HAS_DESTRUCTOR (t);
/* Default constructor. */
if (! TYPE_HAS_CONSTRUCTOR (t) && ! cant_have_default_ctor)
{
default_fn = implicitly_declare_fn (sfk_constructor, t, /*const_p=*/0);
TREE_CHAIN (default_fn) = implicit_fns;
implicit_fns = default_fn;
}
/* Copy constructor. */
if (! TYPE_HAS_INIT_REF (t) && ! TYPE_FOR_JAVA (t))
{
/* ARM 12.18: You get either X(X&) or X(const X&), but
not both. --Chip */
default_fn
= implicitly_declare_fn (sfk_copy_constructor, t,
/*const_p=*/!cant_have_const_cctor);
TREE_CHAIN (default_fn) = implicit_fns;
implicit_fns = default_fn;
}
/* Assignment operator. */
if (! TYPE_HAS_ASSIGN_REF (t) && ! TYPE_FOR_JAVA (t))
{
default_fn
= implicitly_declare_fn (sfk_assignment_operator, t,
/*const_p=*/!cant_have_const_assignment);
TREE_CHAIN (default_fn) = implicit_fns;
implicit_fns = default_fn;
}
/* Now, hook all of the new functions on to TYPE_METHODS,
and add them to the CLASSTYPE_METHOD_VEC. */
for (f = &implicit_fns; *f; f = &TREE_CHAIN (*f))
{
add_method (t, *f, /*error_p=*/0);
maybe_add_class_template_decl_list (current_class_type, *f, /*friend_p=*/0);
}
if (abi_version_at_least (2))
/* G++ 3.2 put the implicit destructor at the *beginning* of the
list, which cause the destructor to be emitted in an incorrect
location in the vtable. */
TYPE_METHODS (t) = chainon (TYPE_METHODS (t), implicit_fns);
else
{
if (warn_abi && virtual_dtor)
warning ("vtable layout for class `%T' may not be ABI-compliant "
"and may change in a future version of GCC due to implicit "
"virtual destructor",
t);
*f = TYPE_METHODS (t);
TYPE_METHODS (t) = implicit_fns;
}
--adding_implicit_members;
}
/* Subroutine of finish_struct_1. Recursively count the number of fields
in TYPE, including anonymous union members. */
static int
count_fields (tree fields)
{
tree x;
int n_fields = 0;
for (x = fields; x; x = TREE_CHAIN (x))
{
if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
n_fields += count_fields (TYPE_FIELDS (TREE_TYPE (x)));
else
n_fields += 1;
}
return n_fields;
}
/* Subroutine of finish_struct_1. Recursively add all the fields in the
TREE_LIST FIELDS to the SORTED_FIELDS_TYPE elts, starting at offset IDX. */
static int
add_fields_to_record_type (tree fields, struct sorted_fields_type *field_vec, int idx)
{
tree x;
for (x = fields; x; x = TREE_CHAIN (x))
{
if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
idx = add_fields_to_record_type (TYPE_FIELDS (TREE_TYPE (x)), field_vec, idx);
else
field_vec->elts[idx++] = x;
}
return idx;
}
/* FIELD is a bit-field. We are finishing the processing for its
enclosing type. Issue any appropriate messages and set appropriate
flags. */
static void
check_bitfield_decl (tree field)
{
tree type = TREE_TYPE (field);
tree w = NULL_TREE;
/* Detect invalid bit-field type. */
if (DECL_INITIAL (field)
&& ! INTEGRAL_TYPE_P (TREE_TYPE (field)))
{
cp_error_at ("bit-field `%#D' with non-integral type", field);
w = error_mark_node;
}
/* Detect and ignore out of range field width. */
if (DECL_INITIAL (field))
{
w = DECL_INITIAL (field);
/* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs. */
STRIP_NOPS (w);
/* detect invalid field size. */
if (TREE_CODE (w) == CONST_DECL)
w = DECL_INITIAL (w);
else
w = decl_constant_value (w);
if (TREE_CODE (w) != INTEGER_CST)
{
cp_error_at ("bit-field `%D' width not an integer constant",
field);
w = error_mark_node;
}
else if (tree_int_cst_sgn (w) < 0)
{
cp_error_at ("negative width in bit-field `%D'", field);
w = error_mark_node;
}
else if (integer_zerop (w) && DECL_NAME (field) != 0)
{
cp_error_at ("zero width for bit-field `%D'", field);
w = error_mark_node;
}
else if (compare_tree_int (w, TYPE_PRECISION (type)) > 0
&& TREE_CODE (type) != ENUMERAL_TYPE
&& TREE_CODE (type) != BOOLEAN_TYPE)
cp_warning_at ("width of `%D' exceeds its type", field);
else if (TREE_CODE (type) == ENUMERAL_TYPE
&& (0 > compare_tree_int (w,
min_precision (TYPE_MIN_VALUE (type),
TREE_UNSIGNED (type)))
|| 0 > compare_tree_int (w,
min_precision
(TYPE_MAX_VALUE (type),
TREE_UNSIGNED (type)))))
cp_warning_at ("`%D' is too small to hold all values of `%#T'",
field, type);
}
/* Remove the bit-field width indicator so that the rest of the
compiler does not treat that value as an initializer. */
DECL_INITIAL (field) = NULL_TREE;
if (w != error_mark_node)
{
DECL_SIZE (field) = convert (bitsizetype, w);
DECL_BIT_FIELD (field) = 1;
}
else
{
/* Non-bit-fields are aligned for their type. */
DECL_BIT_FIELD (field) = 0;
CLEAR_DECL_C_BIT_FIELD (field);
}
}
/* FIELD is a non bit-field. We are finishing the processing for its
enclosing type T. Issue any appropriate messages and set appropriate
flags. */
static void
check_field_decl (tree field,
tree t,
int* cant_have_const_ctor,
int* cant_have_default_ctor,
int* no_const_asn_ref,
int* any_default_members)
{
tree type = strip_array_types (TREE_TYPE (field));
/* An anonymous union cannot contain any fields which would change
the settings of CANT_HAVE_CONST_CTOR and friends. */
if (ANON_UNION_TYPE_P (type))
;
/* And, we don't set TYPE_HAS_CONST_INIT_REF, etc., for anonymous
structs. So, we recurse through their fields here. */
else if (ANON_AGGR_TYPE_P (type))
{
tree fields;
for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields))
if (TREE_CODE (fields) == FIELD_DECL && !DECL_C_BIT_FIELD (field))
check_field_decl (fields, t, cant_have_const_ctor,
cant_have_default_ctor, no_const_asn_ref,
any_default_members);
}
/* Check members with class type for constructors, destructors,
etc. */
else if (CLASS_TYPE_P (type))
{
/* Never let anything with uninheritable virtuals
make it through without complaint. */
abstract_virtuals_error (field, type);
if (TREE_CODE (t) == UNION_TYPE)
{
if (TYPE_NEEDS_CONSTRUCTING (type))
cp_error_at ("member `%#D' with constructor not allowed in union",
field);
if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
cp_error_at ("member `%#D' with destructor not allowed in union",
field);
if (TYPE_HAS_COMPLEX_ASSIGN_REF (type))
cp_error_at ("member `%#D' with copy assignment operator not allowed in union",
field);
}
else
{
TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type);
TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
|= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type);
TYPE_HAS_COMPLEX_ASSIGN_REF (t) |= TYPE_HAS_COMPLEX_ASSIGN_REF (type);
TYPE_HAS_COMPLEX_INIT_REF (t) |= TYPE_HAS_COMPLEX_INIT_REF (type);
}
if (!TYPE_HAS_CONST_INIT_REF (type))
*cant_have_const_ctor = 1;
if (!TYPE_HAS_CONST_ASSIGN_REF (type))
*no_const_asn_ref = 1;
if (TYPE_HAS_CONSTRUCTOR (type)
&& ! TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
*cant_have_default_ctor = 1;
}
if (DECL_INITIAL (field) != NULL_TREE)
{
/* `build_class_init_list' does not recognize
non-FIELD_DECLs. */
if (TREE_CODE (t) == UNION_TYPE && any_default_members != 0)
error ("multiple fields in union `%T' initialized", t);
*any_default_members = 1;
}
}
/* Check the data members (both static and non-static), class-scoped
typedefs, etc., appearing in the declaration of T. Issue
appropriate diagnostics. Sets ACCESS_DECLS to a list (in
declaration order) of access declarations; each TREE_VALUE in this
list is a USING_DECL.
In addition, set the following flags:
EMPTY_P
The class is empty, i.e., contains no non-static data members.
CANT_HAVE_DEFAULT_CTOR_P
This class cannot have an implicitly generated default
constructor.
CANT_HAVE_CONST_CTOR_P
This class cannot have an implicitly generated copy constructor
taking a const reference.
CANT_HAVE_CONST_ASN_REF
This class cannot have an implicitly generated assignment
operator taking a const reference.
All of these flags should be initialized before calling this
function.
Returns a pointer to the end of the TYPE_FIELDs chain; additional
fields can be added by adding to this chain. */
static void
check_field_decls (tree t, tree *access_decls,
int *cant_have_default_ctor_p,
int *cant_have_const_ctor_p,
int *no_const_asn_ref_p)
{
tree *field;
tree *next;
int has_pointers;
int any_default_members;
/* Assume there are no access declarations. */
*access_decls = NULL_TREE;
/* Assume this class has no pointer members. */
has_pointers = 0;
/* Assume none of the members of this class have default
initializations. */
any_default_members = 0;
for (field = &TYPE_FIELDS (t); *field; field = next)
{
tree x = *field;
tree type = TREE_TYPE (x);
next = &TREE_CHAIN (x);
if (TREE_CODE (x) == FIELD_DECL)
{
if (TYPE_PACKED (t))
{
if (!pod_type_p (TREE_TYPE (x)) && !TYPE_PACKED (TREE_TYPE (x)))
cp_warning_at
("ignoring packed attribute on unpacked non-POD field `%#D'",
x);
else
DECL_PACKED (x) = 1;
}
if (DECL_C_BIT_FIELD (x) && integer_zerop (DECL_INITIAL (x)))
/* We don't treat zero-width bitfields as making a class
non-empty. */
;
else
{
tree element_type;
/* The class is non-empty. */
CLASSTYPE_EMPTY_P (t) = 0;
/* The class is not even nearly empty. */
CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
/* If one of the data members contains an empty class,
so does T. */
element_type = strip_array_types (type);
if (CLASS_TYPE_P (element_type)
&& CLASSTYPE_CONTAINS_EMPTY_CLASS_P (element_type))
CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
}
}
if (TREE_CODE (x) == USING_DECL)
{
/* Prune the access declaration from the list of fields. */
*field = TREE_CHAIN (x);
/* Save the access declarations for our caller. */
*access_decls = tree_cons (NULL_TREE, x, *access_decls);
/* Since we've reset *FIELD there's no reason to skip to the
next field. */
next = field;
continue;
}
if (TREE_CODE (x) == TYPE_DECL
|| TREE_CODE (x) == TEMPLATE_DECL)
continue;
/* If we've gotten this far, it's a data member, possibly static,
or an enumerator. */
DECL_CONTEXT (x) = t;
/* When this goes into scope, it will be a non-local reference. */
DECL_NONLOCAL (x) = 1;
if (TREE_CODE (t) == UNION_TYPE)
{
/* [class.union]
If a union contains a static data member, or a member of
reference type, the program is ill-formed. */
if (TREE_CODE (x) == VAR_DECL)
{
cp_error_at ("`%D' may not be static because it is a member of a union", x);
continue;
}
if (TREE_CODE (type) == REFERENCE_TYPE)
{
cp_error_at ("`%D' may not have reference type `%T' because it is a member of a union",
x, type);
continue;
}
}
/* ``A local class cannot have static data members.'' ARM 9.4 */
if (current_function_decl && TREE_STATIC (x))
cp_error_at ("field `%D' in local class cannot be static", x);
/* Perform error checking that did not get done in
grokdeclarator. */
if (TREE_CODE (type) == FUNCTION_TYPE)
{
cp_error_at ("field `%D' invalidly declared function type",
x);
type = build_pointer_type (type);
TREE_TYPE (x) = type;
}
else if (TREE_CODE (type) == METHOD_TYPE)
{
cp_error_at ("field `%D' invalidly declared method type", x);
type = build_pointer_type (type);
TREE_TYPE (x) = type;
}
if (type == error_mark_node)
continue;
if (TREE_CODE (x) == CONST_DECL || TREE_CODE (x) == VAR_DECL)
continue;
/* Now it can only be a FIELD_DECL. */
if (TREE_PRIVATE (x) || TREE_PROTECTED (x))
CLASSTYPE_NON_AGGREGATE (t) = 1;
/* If this is of reference type, check if it needs an init.
Also do a little ANSI jig if necessary. */
if (TREE_CODE (type) == REFERENCE_TYPE)
{
CLASSTYPE_NON_POD_P (t) = 1;
if (DECL_INITIAL (x) == NULL_TREE)
SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
/* ARM $12.6.2: [A member initializer list] (or, for an
aggregate, initialization by a brace-enclosed list) is the
only way to initialize nonstatic const and reference
members. */
*cant_have_default_ctor_p = 1;
TYPE_HAS_COMPLEX_ASSIGN_REF (t) = 1;
if (! TYPE_HAS_CONSTRUCTOR (t) && CLASSTYPE_NON_AGGREGATE (t)
&& extra_warnings)
cp_warning_at ("non-static reference `%#D' in class without a constructor", x);
}
type = strip_array_types (type);
if (TYPE_PTR_P (type))
has_pointers = 1;
if (CLASS_TYPE_P (type))
{
if (CLASSTYPE_REF_FIELDS_NEED_INIT (type))
SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (type))
SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
}
if (DECL_MUTABLE_P (x) || TYPE_HAS_MUTABLE_P (type))
CLASSTYPE_HAS_MUTABLE (t) = 1;
if (! pod_type_p (type))
/* DR 148 now allows pointers to members (which are POD themselves),
to be allowed in POD structs. */
CLASSTYPE_NON_POD_P (t) = 1;
if (! zero_init_p (type))
CLASSTYPE_NON_ZERO_INIT_P (t) = 1;
/* If any field is const, the structure type is pseudo-const. */
if (CP_TYPE_CONST_P (type))
{
C_TYPE_FIELDS_READONLY (t) = 1;
if (DECL_INITIAL (x) == NULL_TREE)
SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
/* ARM $12.6.2: [A member initializer list] (or, for an
aggregate, initialization by a brace-enclosed list) is the
only way to initialize nonstatic const and reference
members. */
*cant_have_default_ctor_p = 1;
TYPE_HAS_COMPLEX_ASSIGN_REF (t) = 1;
if (! TYPE_HAS_CONSTRUCTOR (t) && CLASSTYPE_NON_AGGREGATE (t)
&& extra_warnings)
cp_warning_at ("non-static const member `%#D' in class without a constructor", x);
}
/* A field that is pseudo-const makes the structure likewise. */
else if (CLASS_TYPE_P (type))
{
C_TYPE_FIELDS_READONLY (t) |= C_TYPE_FIELDS_READONLY (type);
SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t,
CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
| CLASSTYPE_READONLY_FIELDS_NEED_INIT (type));
}
/* Core issue 80: A nonstatic data member is required to have a
different name from the class iff the class has a
user-defined constructor. */
if (constructor_name_p (DECL_NAME (x), t) && TYPE_HAS_CONSTRUCTOR (t))
cp_pedwarn_at ("field `%#D' with same name as class", x);
/* We set DECL_C_BIT_FIELD in grokbitfield.
If the type and width are valid, we'll also set DECL_BIT_FIELD. */
if (DECL_C_BIT_FIELD (x))
check_bitfield_decl (x);
else
check_field_decl (x, t,
cant_have_const_ctor_p,
cant_have_default_ctor_p,
no_const_asn_ref_p,
&any_default_members);
}
/* Effective C++ rule 11. */
if (has_pointers && warn_ecpp && TYPE_HAS_CONSTRUCTOR (t)
&& ! (TYPE_HAS_INIT_REF (t) && TYPE_HAS_ASSIGN_REF (t)))
{
warning ("`%#T' has pointer data members", t);
if (! TYPE_HAS_INIT_REF (t))
{
warning (" but does not override `%T(const %T&)'", t, t);
if (! TYPE_HAS_ASSIGN_REF (t))
warning (" or `operator=(const %T&)'", t);
}
else if (! TYPE_HAS_ASSIGN_REF (t))
warning (" but does not override `operator=(const %T&)'", t);
}
/* Check anonymous struct/anonymous union fields. */
finish_struct_anon (t);
/* We've built up the list of access declarations in reverse order.
Fix that now. */
*access_decls = nreverse (*access_decls);
}
/* If TYPE is an empty class type, records its OFFSET in the table of
OFFSETS. */
static int
record_subobject_offset (tree type, tree offset, splay_tree offsets)
{
splay_tree_node n;
if (!is_empty_class (type))
return 0;
/* Record the location of this empty object in OFFSETS. */
n = splay_tree_lookup (offsets, (splay_tree_key) offset);
if (!n)
n = splay_tree_insert (offsets,
(splay_tree_key) offset,
(splay_tree_value) NULL_TREE);
n->value = ((splay_tree_value)
tree_cons (NULL_TREE,
type,
(tree) n->value));
return 0;
}
/* Returns nonzero if TYPE is an empty class type and there is
already an entry in OFFSETS for the same TYPE as the same OFFSET. */
static int
check_subobject_offset (tree type, tree offset, splay_tree offsets)
{
splay_tree_node n;
tree t;
if (!is_empty_class (type))
return 0;
/* Record the location of this empty object in OFFSETS. */
n = splay_tree_lookup (offsets, (splay_tree_key) offset);
if (!n)
return 0;
for (t = (tree) n->value; t; t = TREE_CHAIN (t))
if (same_type_p (TREE_VALUE (t), type))
return 1;
return 0;
}
/* Walk through all the subobjects of TYPE (located at OFFSET). Call
F for every subobject, passing it the type, offset, and table of
OFFSETS. If VBASES_P is one, then virtual non-primary bases should
be traversed.
If MAX_OFFSET is non-NULL, then subobjects with an offset greater
than MAX_OFFSET will not be walked.
If F returns a nonzero value, the traversal ceases, and that value
is returned. Otherwise, returns zero. */
static int
walk_subobject_offsets (tree type,
subobject_offset_fn f,
tree offset,
splay_tree offsets,
tree max_offset,
int vbases_p)
{
int r = 0;
tree type_binfo = NULL_TREE;
/* If this OFFSET is bigger than the MAX_OFFSET, then we should
stop. */
if (max_offset && INT_CST_LT (max_offset, offset))
return 0;
if (!TYPE_P (type))
{
if (abi_version_at_least (2))
type_binfo = type;
type = BINFO_TYPE (type);
}
if (CLASS_TYPE_P (type))
{
tree field;
tree binfo;
int i;
/* Avoid recursing into objects that are not interesting. */
if (!CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
return 0;
/* Record the location of TYPE. */
r = (*f) (type, offset, offsets);
if (r)
return r;
/* Iterate through the direct base classes of TYPE. */
if (!type_binfo)
type_binfo = TYPE_BINFO (type);
for (i = 0; i < BINFO_N_BASETYPES (type_binfo); ++i)
{
tree binfo_offset;
binfo = BINFO_BASETYPE (type_binfo, i);
if (abi_version_at_least (2)
&& TREE_VIA_VIRTUAL (binfo))
continue;
if (!vbases_p
&& TREE_VIA_VIRTUAL (binfo)
&& !BINFO_PRIMARY_P (binfo))
continue;
if (!abi_version_at_least (2))
binfo_offset = size_binop (PLUS_EXPR,
offset,
BINFO_OFFSET (binfo));
else
{
tree orig_binfo;
/* We cannot rely on BINFO_OFFSET being set for the base
class yet, but the offsets for direct non-virtual
bases can be calculated by going back to the TYPE. */
orig_binfo = BINFO_BASETYPE (TYPE_BINFO (type), i);
binfo_offset = size_binop (PLUS_EXPR,
offset,
BINFO_OFFSET (orig_binfo));
}
r = walk_subobject_offsets (binfo,
f,
binfo_offset,
offsets,
max_offset,