118334Speter/* Functions related to building classes and their related objects. 290075Sobrien Copyright (C) 1987, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 3169689Skan 1999, 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc. 418334Speter Contributed by Michael Tiemann (tiemann@cygnus.com) 518334Speter 6132718SkanThis file is part of GCC. 718334Speter 8132718SkanGCC is free software; you can redistribute it and/or modify 918334Speterit under the terms of the GNU General Public License as published by 1018334Speterthe Free Software Foundation; either version 2, or (at your option) 1118334Speterany later version. 1218334Speter 13132718SkanGCC is distributed in the hope that it will be useful, 1418334Speterbut WITHOUT ANY WARRANTY; without even the implied warranty of 1518334SpeterMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1618334SpeterGNU General Public License for more details. 1718334Speter 1818334SpeterYou should have received a copy of the GNU General Public License 19132718Skanalong with GCC; see the file COPYING. If not, write to 20169689Skanthe Free Software Foundation, 51 Franklin Street, Fifth Floor, 21169689SkanBoston, MA 02110-1301, USA. */ 2218334Speter 2318334Speter 2450397Sobrien/* High-level class interface. */ 2518334Speter 2618334Speter#include "config.h" 2750397Sobrien#include "system.h" 28132718Skan#include "coretypes.h" 29132718Skan#include "tm.h" 3018334Speter#include "tree.h" 3118334Speter#include "cp-tree.h" 3218334Speter#include "flags.h" 3318334Speter#include "rtl.h" 3418334Speter#include "output.h" 3550397Sobrien#include "toplev.h" 3696263Sobrien#include "target.h" 37132718Skan#include "convert.h" 38169689Skan#include "cgraph.h" 39169689Skan#include "tree-dump.h" 4018334Speter 4152284Sobrien/* The number of nested classes being processed. If we are not in the 4252284Sobrien scope of any class, this is zero. */ 4352284Sobrien 4418334Speterint current_class_depth; 4518334Speter 4652284Sobrien/* In order to deal with nested classes, we keep a stack of classes. 4752284Sobrien The topmost entry is the innermost class, and is the entry at index 4852284Sobrien CURRENT_CLASS_DEPTH */ 4918334Speter 5052284Sobrientypedef struct class_stack_node { 5152284Sobrien /* The name of the class. */ 5252284Sobrien tree name; 5318334Speter 5452284Sobrien /* The _TYPE node for the class. */ 5518334Speter tree type; 5618334Speter 5752284Sobrien /* The access specifier pending for new declarations in the scope of 5852284Sobrien this class. */ 5952284Sobrien tree access; 6018334Speter 6152284Sobrien /* If were defining TYPE, the names used in this class. */ 6252284Sobrien splay_tree names_used; 63169689Skan 64169689Skan /* Nonzero if this class is no longer open, because of a call to 65169689Skan push_to_top_level. */ 66169689Skan size_t hidden; 6752284Sobrien}* class_stack_node_t; 6852284Sobrien 6990075Sobrientypedef struct vtbl_init_data_s 7090075Sobrien{ 7190075Sobrien /* The base for which we're building initializers. */ 7290075Sobrien tree binfo; 7390075Sobrien /* The type of the most-derived type. */ 7490075Sobrien tree derived; 7590075Sobrien /* The binfo for the dynamic type. This will be TYPE_BINFO (derived), 7690075Sobrien unless ctor_vtbl_p is true. */ 7790075Sobrien tree rtti_binfo; 7890075Sobrien /* The negative-index vtable initializers built up so far. These 7990075Sobrien are in order from least negative index to most negative index. */ 8090075Sobrien tree inits; 8190075Sobrien /* The last (i.e., most negative) entry in INITS. */ 8290075Sobrien tree* last_init; 8390075Sobrien /* The binfo for the virtual base for which we're building 8490075Sobrien vcall offset initializers. */ 8590075Sobrien tree vbase; 8690075Sobrien /* The functions in vbase for which we have already provided vcall 8790075Sobrien offsets. */ 88169689Skan VEC(tree,gc) *fns; 8990075Sobrien /* The vtable index of the next vcall or vbase offset. */ 9090075Sobrien tree index; 9190075Sobrien /* Nonzero if we are building the initializer for the primary 9290075Sobrien vtable. */ 9390075Sobrien int primary_vtbl_p; 9490075Sobrien /* Nonzero if we are building the initializer for a construction 9590075Sobrien vtable. */ 9690075Sobrien int ctor_vtbl_p; 97117395Skan /* True when adding vcall offset entries to the vtable. False when 98117395Skan merely computing the indices. */ 99117395Skan bool generate_vcall_entries; 10090075Sobrien} vtbl_init_data; 10190075Sobrien 10290075Sobrien/* The type of a function passed to walk_subobject_offsets. */ 103132718Skantypedef int (*subobject_offset_fn) (tree, tree, splay_tree); 10490075Sobrien 105132718Skan/* The stack itself. This is a dynamically resized array. The 10652284Sobrien number of elements allocated is CURRENT_CLASS_STACK_SIZE. */ 10752284Sobrienstatic int current_class_stack_size; 10852284Sobrienstatic class_stack_node_t current_class_stack; 10952284Sobrien 110169689Skan/* The size of the largest empty class seen in this translation unit. */ 111169689Skanstatic GTY (()) tree sizeof_biggest_empty_class; 112169689Skan 11390075Sobrien/* An array of all local classes present in this translation unit, in 11490075Sobrien declaration order. */ 115169689SkanVEC(tree,gc) *local_classes; 11618334Speter 117132718Skanstatic tree get_vfield_name (tree); 118132718Skanstatic void finish_struct_anon (tree); 119132718Skanstatic tree get_vtable_name (tree); 120132718Skanstatic tree get_basefndecls (tree, tree); 121132718Skanstatic int build_primary_vtable (tree, tree); 122132718Skanstatic int build_secondary_vtable (tree); 123132718Skanstatic void finish_vtbls (tree); 124132718Skanstatic void modify_vtable_entry (tree, tree, tree, tree, tree *); 125132718Skanstatic void finish_struct_bits (tree); 126132718Skanstatic int alter_access (tree, tree, tree); 127132718Skanstatic void handle_using_decl (tree, tree); 128132718Skanstatic tree dfs_modify_vtables (tree, void *); 129132718Skanstatic tree modify_all_vtables (tree, tree); 130169689Skanstatic void determine_primary_bases (tree); 131132718Skanstatic void finish_struct_methods (tree); 132132718Skanstatic void maybe_warn_about_overly_private_class (tree); 133132718Skanstatic int method_name_cmp (const void *, const void *); 134132718Skanstatic int resort_method_name_cmp (const void *, const void *); 135169689Skanstatic void add_implicitly_declared_members (tree, int, int); 136132718Skanstatic tree fixed_type_or_null (tree, int *, int *); 137169689Skanstatic tree build_simple_base_path (tree expr, tree binfo); 138132718Skanstatic tree build_vtbl_ref_1 (tree, tree); 139132718Skanstatic tree build_vtbl_initializer (tree, tree, tree, tree, int *); 140132718Skanstatic int count_fields (tree); 141132718Skanstatic int add_fields_to_record_type (tree, struct sorted_fields_type*, int); 142132718Skanstatic void check_bitfield_decl (tree); 143169689Skanstatic void check_field_decl (tree, tree, int *, int *, int *); 144169689Skanstatic void check_field_decls (tree, tree *, int *, int *); 145117395Skanstatic tree *build_base_field (record_layout_info, tree, splay_tree, tree *); 146117395Skanstatic void build_base_fields (record_layout_info, splay_tree, tree *); 147132718Skanstatic void check_methods (tree); 148132718Skanstatic void remove_zero_width_bit_fields (tree); 149169689Skanstatic void check_bases (tree, int *, int *); 150117395Skanstatic void check_bases_and_members (tree); 151117395Skanstatic tree create_vtable_ptr (tree, tree *); 152117395Skanstatic void include_empty_classes (record_layout_info); 153117395Skanstatic void layout_class_type (tree, tree *); 154132718Skanstatic void fixup_pending_inline (tree); 155132718Skanstatic void fixup_inline_methods (tree); 156132718Skanstatic void propagate_binfo_offsets (tree, tree); 157117395Skanstatic void layout_virtual_bases (record_layout_info, splay_tree); 158132718Skanstatic void build_vbase_offset_vtbl_entries (tree, vtbl_init_data *); 159132718Skanstatic void add_vcall_offset_vtbl_entries_r (tree, vtbl_init_data *); 160132718Skanstatic void add_vcall_offset_vtbl_entries_1 (tree, vtbl_init_data *); 161132718Skanstatic void build_vcall_offset_vtbl_entries (tree, vtbl_init_data *); 162117395Skanstatic void add_vcall_offset (tree, tree, vtbl_init_data *); 163132718Skanstatic void layout_vtable_decl (tree, int); 164169689Skanstatic tree dfs_find_final_overrider_pre (tree, void *); 165132718Skanstatic tree dfs_find_final_overrider_post (tree, void *); 166132718Skanstatic tree find_final_overrider (tree, tree, tree); 167132718Skanstatic int make_new_vtable (tree, tree); 168169689Skanstatic tree get_primary_binfo (tree); 169132718Skanstatic int maybe_indent_hierarchy (FILE *, int, int); 170132718Skanstatic tree dump_class_hierarchy_r (FILE *, int, tree, tree, int); 171132718Skanstatic void dump_class_hierarchy (tree); 172132718Skanstatic void dump_class_hierarchy_1 (FILE *, int, tree); 173132718Skanstatic void dump_array (FILE *, tree); 174132718Skanstatic void dump_vtable (tree, tree, tree); 175132718Skanstatic void dump_vtt (tree, tree); 176132718Skanstatic void dump_thunk (FILE *, int, tree); 177132718Skanstatic tree build_vtable (tree, tree, tree); 178132718Skanstatic void initialize_vtable (tree, tree); 179132718Skanstatic void layout_nonempty_base_or_field (record_layout_info, 180169689Skan tree, tree, splay_tree); 181132718Skanstatic tree end_of_class (tree, int); 182132718Skanstatic bool layout_empty_base (tree, tree, splay_tree); 183132718Skanstatic void accumulate_vtbl_inits (tree, tree, tree, tree, tree); 184132718Skanstatic tree dfs_accumulate_vtbl_inits (tree, tree, tree, tree, 185132718Skan tree); 186132718Skanstatic void build_rtti_vtbl_entries (tree, vtbl_init_data *); 187169689Skanstatic void build_vcall_and_vbase_vtbl_entries (tree, vtbl_init_data *); 188132718Skanstatic void clone_constructors_and_destructors (tree); 189132718Skanstatic tree build_clone (tree, tree); 190132718Skanstatic void update_vtable_entry_for_fn (tree, tree, tree, tree *, unsigned); 191132718Skanstatic void build_ctor_vtbl_group (tree, tree); 192132718Skanstatic void build_vtt (tree); 193132718Skanstatic tree binfo_ctor_vtable (tree); 194132718Skanstatic tree *build_vtt_inits (tree, tree, tree *, tree *); 195132718Skanstatic tree dfs_build_secondary_vptr_vtt_inits (tree, void *); 196132718Skanstatic tree dfs_fixup_binfo_vtbls (tree, void *); 197132718Skanstatic int record_subobject_offset (tree, tree, splay_tree); 198132718Skanstatic int check_subobject_offset (tree, tree, splay_tree); 199132718Skanstatic int walk_subobject_offsets (tree, subobject_offset_fn, 200169689Skan tree, splay_tree, tree, int); 201169689Skanstatic void record_subobject_offsets (tree, tree, splay_tree, bool); 202132718Skanstatic int layout_conflict_p (tree, tree, splay_tree, int); 203132718Skanstatic int splay_tree_compare_integer_csts (splay_tree_key k1, 204169689Skan splay_tree_key k2); 205132718Skanstatic void warn_about_ambiguous_bases (tree); 206132718Skanstatic bool type_requires_array_cookie (tree); 207117395Skanstatic bool contains_empty_class_p (tree); 208117395Skanstatic bool base_derived_from (tree, tree); 209117395Skanstatic int empty_base_at_nonzero_offset_p (tree, tree, splay_tree); 210117395Skanstatic tree end_of_base (tree); 211117395Skanstatic tree get_vcall_index (tree, tree); 21250397Sobrien 21318334Speter/* Variables shared between class.c and call.c. */ 21418334Speter 21550397Sobrien#ifdef GATHER_STATISTICS 21618334Speterint n_vtables = 0; 21718334Speterint n_vtable_entries = 0; 21818334Speterint n_vtable_searches = 0; 21918334Speterint n_vtable_elems = 0; 22018334Speterint n_convert_harshness = 0; 22118334Speterint n_compute_conversion_costs = 0; 22218334Speterint n_inner_fields_searched = 0; 22350397Sobrien#endif 22418334Speter 22590075Sobrien/* Convert to or from a base subobject. EXPR is an expression of type 22690075Sobrien `A' or `A*', an expression of type `B' or `B*' is returned. To 22790075Sobrien convert A to a base B, CODE is PLUS_EXPR and BINFO is the binfo for 22890075Sobrien the B base instance within A. To convert base A to derived B, CODE 22990075Sobrien is MINUS_EXPR and BINFO is the binfo for the A instance within B. 23090075Sobrien In this latter case, A must not be a morally virtual base of B. 23190075Sobrien NONNULL is true if EXPR is known to be non-NULL (this is only 23290075Sobrien needed when EXPR is of pointer type). CV qualifiers are preserved 23390075Sobrien from EXPR. */ 23450397Sobrien 23518334Spetertree 236132718Skanbuild_base_path (enum tree_code code, 237169689Skan tree expr, 238169689Skan tree binfo, 239169689Skan int nonnull) 24018334Speter{ 24190075Sobrien tree v_binfo = NULL_TREE; 24290075Sobrien tree d_binfo = NULL_TREE; 24390075Sobrien tree probe; 24490075Sobrien tree offset; 24590075Sobrien tree target_type; 24690075Sobrien tree null_test = NULL; 24790075Sobrien tree ptr_target_type; 24850397Sobrien int fixed_type_p; 24990075Sobrien int want_pointer = TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE; 250169689Skan bool has_empty = false; 251169689Skan bool virtual_access; 25218334Speter 25390075Sobrien if (expr == error_mark_node || binfo == error_mark_node || !binfo) 25490075Sobrien return error_mark_node; 25550397Sobrien 25690075Sobrien for (probe = binfo; probe; probe = BINFO_INHERITANCE_CHAIN (probe)) 25718334Speter { 25890075Sobrien d_binfo = probe; 259169689Skan if (is_empty_class (BINFO_TYPE (probe))) 260169689Skan has_empty = true; 261169689Skan if (!v_binfo && BINFO_VIRTUAL_P (probe)) 26290075Sobrien v_binfo = probe; 26318334Speter } 26418334Speter 26590075Sobrien probe = TYPE_MAIN_VARIANT (TREE_TYPE (expr)); 26690075Sobrien if (want_pointer) 26790075Sobrien probe = TYPE_MAIN_VARIANT (TREE_TYPE (probe)); 268169689Skan 269169689Skan gcc_assert ((code == MINUS_EXPR 270169689Skan && SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), probe)) 271169689Skan || (code == PLUS_EXPR 272169689Skan && SAME_BINFO_TYPE_P (BINFO_TYPE (d_binfo), probe))); 273169689Skan 274169689Skan if (binfo == d_binfo) 275169689Skan /* Nothing to do. */ 276169689Skan return expr; 277169689Skan 27890075Sobrien if (code == MINUS_EXPR && v_binfo) 27918334Speter { 280169689Skan error ("cannot convert from base %qT to derived type %qT via virtual base %qT", 28190075Sobrien BINFO_TYPE (binfo), BINFO_TYPE (d_binfo), BINFO_TYPE (v_binfo)); 28290075Sobrien return error_mark_node; 28318334Speter } 28418334Speter 285117395Skan if (!want_pointer) 286117395Skan /* This must happen before the call to save_expr. */ 287117395Skan expr = build_unary_op (ADDR_EXPR, expr, 0); 288117395Skan 289169689Skan offset = BINFO_OFFSET (binfo); 29090075Sobrien fixed_type_p = resolves_to_fixed_type_p (expr, &nonnull); 291169689Skan target_type = code == PLUS_EXPR ? BINFO_TYPE (binfo) : BINFO_TYPE (d_binfo); 29296263Sobrien 293169689Skan /* Do we need to look in the vtable for the real offset? */ 294169689Skan virtual_access = (v_binfo && fixed_type_p <= 0); 295169689Skan 296169689Skan /* Do we need to check for a null pointer? */ 297117395Skan if (want_pointer && !nonnull) 29818334Speter { 299169689Skan /* If we know the conversion will not actually change the value 300169689Skan of EXPR, then we can avoid testing the expression for NULL. 301169689Skan We have to avoid generating a COMPONENT_REF for a base class 302169689Skan field, because other parts of the compiler know that such 303169689Skan expressions are always non-NULL. */ 304169689Skan if (!virtual_access && integer_zerop (offset)) 305169689Skan { 306169689Skan tree class_type; 307169689Skan /* TARGET_TYPE has been extracted from BINFO, and, is 308169689Skan therefore always cv-unqualified. Extract the 309169689Skan cv-qualifiers from EXPR so that the expression returned 310169689Skan matches the input. */ 311169689Skan class_type = TREE_TYPE (TREE_TYPE (expr)); 312169689Skan target_type 313169689Skan = cp_build_qualified_type (target_type, 314169689Skan cp_type_quals (class_type)); 315169689Skan return build_nop (build_pointer_type (target_type), expr); 316169689Skan } 317169689Skan null_test = error_mark_node; 318169689Skan } 319169689Skan 320169689Skan /* Protect against multiple evaluation if necessary. */ 321169689Skan if (TREE_SIDE_EFFECTS (expr) && (null_test || virtual_access)) 322169689Skan expr = save_expr (expr); 323169689Skan 324169689Skan /* Now that we've saved expr, build the real null test. */ 325169689Skan if (null_test) 326169689Skan { 327169689Skan tree zero = cp_convert (TREE_TYPE (expr), integer_zero_node); 328169689Skan null_test = fold_build2 (NE_EXPR, boolean_type_node, 329169689Skan expr, zero); 330169689Skan } 331169689Skan 332169689Skan /* If this is a simple base reference, express it as a COMPONENT_REF. */ 333169689Skan if (code == PLUS_EXPR && !virtual_access 334169689Skan /* We don't build base fields for empty bases, and they aren't very 335169689Skan interesting to the optimizers anyway. */ 336169689Skan && !has_empty) 337169689Skan { 338169689Skan expr = build_indirect_ref (expr, NULL); 339169689Skan expr = build_simple_base_path (expr, binfo); 340169689Skan if (want_pointer) 341169689Skan expr = build_address (expr); 342169689Skan target_type = TREE_TYPE (expr); 343169689Skan goto out; 344169689Skan } 345169689Skan 346169689Skan if (virtual_access) 347169689Skan { 34890075Sobrien /* Going via virtual base V_BINFO. We need the static offset 349169689Skan from V_BINFO to BINFO, and the dynamic offset from D_BINFO to 350169689Skan V_BINFO. That offset is an entry in D_BINFO's vtable. */ 351117395Skan tree v_offset; 352117395Skan 353117395Skan if (fixed_type_p < 0 && in_base_initializer) 354117395Skan { 355169689Skan /* In a base member initializer, we cannot rely on the 356169689Skan vtable being set up. We have to indirect via the 357169689Skan vtt_parm. */ 358169689Skan tree t; 359169689Skan 360169689Skan t = TREE_TYPE (TYPE_VFIELD (current_class_type)); 361169689Skan t = build_pointer_type (t); 362169689Skan v_offset = convert (t, current_vtt_parm); 363169689Skan v_offset = build_indirect_ref (v_offset, NULL); 364117395Skan } 365117395Skan else 366117395Skan v_offset = build_vfield_ref (build_indirect_ref (expr, NULL), 367117395Skan TREE_TYPE (TREE_TYPE (expr))); 368169689Skan 369169689Skan v_offset = build2 (PLUS_EXPR, TREE_TYPE (v_offset), 370169689Skan v_offset, BINFO_VPTR_FIELD (v_binfo)); 371169689Skan v_offset = build1 (NOP_EXPR, 37290075Sobrien build_pointer_type (ptrdiff_type_node), 37390075Sobrien v_offset); 37490075Sobrien v_offset = build_indirect_ref (v_offset, NULL); 375169689Skan TREE_CONSTANT (v_offset) = 1; 376169689Skan TREE_INVARIANT (v_offset) = 1; 377117395Skan 378132718Skan offset = convert_to_integer (ptrdiff_type_node, 379169689Skan size_diffop (offset, 380132718Skan BINFO_OFFSET (v_binfo))); 38190075Sobrien 38290075Sobrien if (!integer_zerop (offset)) 383169689Skan v_offset = build2 (code, ptrdiff_type_node, v_offset, offset); 38496263Sobrien 38596263Sobrien if (fixed_type_p < 0) 38696263Sobrien /* Negative fixed_type_p means this is a constructor or destructor; 38796263Sobrien virtual base layout is fixed in in-charge [cd]tors, but not in 38896263Sobrien base [cd]tors. */ 389169689Skan offset = build3 (COND_EXPR, ptrdiff_type_node, 390169689Skan build2 (EQ_EXPR, boolean_type_node, 391169689Skan current_in_charge_parm, integer_zero_node), 392169689Skan v_offset, 393169689Skan convert_to_integer (ptrdiff_type_node, 394169689Skan BINFO_OFFSET (binfo))); 39518334Speter else 39690075Sobrien offset = v_offset; 39718334Speter } 39818334Speter 39990075Sobrien target_type = cp_build_qualified_type 40090075Sobrien (target_type, cp_type_quals (TREE_TYPE (TREE_TYPE (expr)))); 40190075Sobrien ptr_target_type = build_pointer_type (target_type); 40290075Sobrien if (want_pointer) 40390075Sobrien target_type = ptr_target_type; 404169689Skan 40590075Sobrien expr = build1 (NOP_EXPR, ptr_target_type, expr); 40618334Speter 40790075Sobrien if (!integer_zerop (offset)) 408169689Skan expr = build2 (code, ptr_target_type, expr, offset); 40990075Sobrien else 41090075Sobrien null_test = NULL; 411169689Skan 41290075Sobrien if (!want_pointer) 41390075Sobrien expr = build_indirect_ref (expr, NULL); 41418334Speter 415169689Skan out: 41690075Sobrien if (null_test) 417169689Skan expr = fold_build3 (COND_EXPR, target_type, null_test, expr, 418169689Skan fold_build1 (NOP_EXPR, target_type, 419169689Skan integer_zero_node)); 42096263Sobrien 42190075Sobrien return expr; 42218334Speter} 42318334Speter 424169689Skan/* Subroutine of build_base_path; EXPR and BINFO are as in that function. 425169689Skan Perform a derived-to-base conversion by recursively building up a 426169689Skan sequence of COMPONENT_REFs to the appropriate base fields. */ 427117395Skan 428169689Skanstatic tree 429169689Skanbuild_simple_base_path (tree expr, tree binfo) 430169689Skan{ 431169689Skan tree type = BINFO_TYPE (binfo); 432169689Skan tree d_binfo = BINFO_INHERITANCE_CHAIN (binfo); 433169689Skan tree field; 434169689Skan 435169689Skan if (d_binfo == NULL_TREE) 436169689Skan { 437169689Skan tree temp; 438169689Skan 439169689Skan gcc_assert (TYPE_MAIN_VARIANT (TREE_TYPE (expr)) == type); 440169689Skan 441169689Skan /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x' 442169689Skan into `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only 443169689Skan an lvalue in the frontend; only _DECLs and _REFs are lvalues 444169689Skan in the backend. */ 445169689Skan temp = unary_complex_lvalue (ADDR_EXPR, expr); 446169689Skan if (temp) 447169689Skan expr = build_indirect_ref (temp, NULL); 448169689Skan 449169689Skan return expr; 450169689Skan } 451169689Skan 452169689Skan /* Recurse. */ 453169689Skan expr = build_simple_base_path (expr, d_binfo); 454169689Skan 455169689Skan for (field = TYPE_FIELDS (BINFO_TYPE (d_binfo)); 456169689Skan field; field = TREE_CHAIN (field)) 457169689Skan /* Is this the base field created by build_base_field? */ 458169689Skan if (TREE_CODE (field) == FIELD_DECL 459169689Skan && DECL_FIELD_IS_BASE (field) 460169689Skan && TREE_TYPE (field) == type) 461169689Skan { 462169689Skan /* We don't use build_class_member_access_expr here, as that 463169689Skan has unnecessary checks, and more importantly results in 464169689Skan recursive calls to dfs_walk_once. */ 465169689Skan int type_quals = cp_type_quals (TREE_TYPE (expr)); 466169689Skan 467169689Skan expr = build3 (COMPONENT_REF, 468169689Skan cp_build_qualified_type (type, type_quals), 469169689Skan expr, field, NULL_TREE); 470169689Skan expr = fold_if_not_in_template (expr); 471169689Skan 472169689Skan /* Mark the expression const or volatile, as appropriate. 473169689Skan Even though we've dealt with the type above, we still have 474169689Skan to mark the expression itself. */ 475169689Skan if (type_quals & TYPE_QUAL_CONST) 476169689Skan TREE_READONLY (expr) = 1; 477169689Skan if (type_quals & TYPE_QUAL_VOLATILE) 478169689Skan TREE_THIS_VOLATILE (expr) = 1; 479169689Skan 480169689Skan return expr; 481169689Skan } 482169689Skan 483169689Skan /* Didn't find the base field?!? */ 484169689Skan gcc_unreachable (); 485169689Skan} 486169689Skan 487169689Skan/* Convert OBJECT to the base TYPE. OBJECT is an expression whose 488169689Skan type is a class type or a pointer to a class type. In the former 489169689Skan case, TYPE is also a class type; in the latter it is another 490169689Skan pointer type. If CHECK_ACCESS is true, an error message is emitted 491169689Skan if TYPE is inaccessible. If OBJECT has pointer type, the value is 492169689Skan assumed to be non-NULL. */ 493169689Skan 494117395Skantree 495169689Skanconvert_to_base (tree object, tree type, bool check_access, bool nonnull) 496117395Skan{ 497117395Skan tree binfo; 498169689Skan tree object_type; 499117395Skan 500169689Skan if (TYPE_PTR_P (TREE_TYPE (object))) 501169689Skan { 502169689Skan object_type = TREE_TYPE (TREE_TYPE (object)); 503169689Skan type = TREE_TYPE (type); 504169689Skan } 505169689Skan else 506169689Skan object_type = TREE_TYPE (object); 507169689Skan 508169689Skan binfo = lookup_base (object_type, type, 509169689Skan check_access ? ba_check : ba_unique, 510117395Skan NULL); 511117395Skan if (!binfo || binfo == error_mark_node) 512117395Skan return error_mark_node; 513117395Skan 514169689Skan return build_base_path (PLUS_EXPR, object, binfo, nonnull); 515117395Skan} 516117395Skan 517169689Skan/* EXPR is an expression with unqualified class type. BASE is a base 518169689Skan binfo of that class type. Returns EXPR, converted to the BASE 519119256Skan type. This function assumes that EXPR is the most derived class; 520119256Skan therefore virtual bases can be found at their static offsets. */ 521119256Skan 522119256Skantree 523119256Skanconvert_to_base_statically (tree expr, tree base) 524119256Skan{ 525119256Skan tree expr_type; 526119256Skan 527119256Skan expr_type = TREE_TYPE (expr); 528169689Skan if (!SAME_BINFO_TYPE_P (BINFO_TYPE (base), expr_type)) 529119256Skan { 530119256Skan tree pointer_type; 531119256Skan 532119256Skan pointer_type = build_pointer_type (expr_type); 533119256Skan expr = build_unary_op (ADDR_EXPR, expr, /*noconvert=*/1); 534119256Skan if (!integer_zerop (BINFO_OFFSET (base))) 535169689Skan expr = build2 (PLUS_EXPR, pointer_type, expr, 536169689Skan build_nop (pointer_type, BINFO_OFFSET (base))); 537119256Skan expr = build_nop (build_pointer_type (BINFO_TYPE (base)), expr); 538119256Skan expr = build1 (INDIRECT_REF, BINFO_TYPE (base), expr); 539119256Skan } 540119256Skan 541119256Skan return expr; 542119256Skan} 543119256Skan 54490075Sobrien 545169689Skantree 546169689Skanbuild_vfield_ref (tree datum, tree type) 547169689Skan{ 548169689Skan tree vfield, vcontext; 549169689Skan 550169689Skan if (datum == error_mark_node) 551169689Skan return error_mark_node; 552169689Skan 553169689Skan /* First, convert to the requested type. */ 554169689Skan if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (datum), type)) 555169689Skan datum = convert_to_base (datum, type, /*check_access=*/false, 556169689Skan /*nonnull=*/true); 557169689Skan 558169689Skan /* Second, the requested type may not be the owner of its own vptr. 559169689Skan If not, convert to the base class that owns it. We cannot use 560169689Skan convert_to_base here, because VCONTEXT may appear more than once 561169689Skan in the inheritance hierarchy of TYPE, and thus direct conversion 562169689Skan between the types may be ambiguous. Following the path back up 563169689Skan one step at a time via primary bases avoids the problem. */ 564169689Skan vfield = TYPE_VFIELD (type); 565169689Skan vcontext = DECL_CONTEXT (vfield); 566169689Skan while (!same_type_ignoring_top_level_qualifiers_p (vcontext, type)) 567169689Skan { 568169689Skan datum = build_simple_base_path (datum, CLASSTYPE_PRIMARY_BINFO (type)); 569169689Skan type = TREE_TYPE (datum); 570169689Skan } 571169689Skan 572169689Skan return build3 (COMPONENT_REF, TREE_TYPE (vfield), datum, vfield, NULL_TREE); 573169689Skan} 574169689Skan 57518334Speter/* Given an object INSTANCE, return an expression which yields the 57690075Sobrien vtable element corresponding to INDEX. There are many special 57790075Sobrien cases for INSTANCE which we take care of here, mainly to avoid 57890075Sobrien creating extra tree nodes when we don't have to. */ 57950397Sobrien 58090075Sobrienstatic tree 581132718Skanbuild_vtbl_ref_1 (tree instance, tree idx) 58218334Speter{ 583117395Skan tree aref; 584117395Skan tree vtbl = NULL_TREE; 585117395Skan 586117395Skan /* Try to figure out what a reference refers to, and 587117395Skan access its virtual function table directly. */ 588117395Skan 589117395Skan int cdtorp = 0; 590117395Skan tree fixed_type = fixed_type_or_null (instance, NULL, &cdtorp); 591117395Skan 592132718Skan tree basetype = non_reference (TREE_TYPE (instance)); 59318334Speter 594117395Skan if (fixed_type && !cdtorp) 59518334Speter { 596117395Skan tree binfo = lookup_base (fixed_type, basetype, 597169689Skan ba_unique | ba_quiet, NULL); 598117395Skan if (binfo) 599169689Skan vtbl = unshare_expr (BINFO_VTABLE (binfo)); 600117395Skan } 60118334Speter 602117395Skan if (!vtbl) 603132718Skan vtbl = build_vfield_ref (instance, basetype); 604169689Skan 60518334Speter assemble_external (vtbl); 60652284Sobrien 60718334Speter aref = build_array_ref (vtbl, idx); 608169689Skan TREE_CONSTANT (aref) |= TREE_CONSTANT (vtbl) && TREE_CONSTANT (idx); 609169689Skan TREE_INVARIANT (aref) = TREE_CONSTANT (aref); 61018334Speter 61150397Sobrien return aref; 61250397Sobrien} 61318334Speter 61490075Sobrientree 615132718Skanbuild_vtbl_ref (tree instance, tree idx) 61690075Sobrien{ 61790075Sobrien tree aref = build_vtbl_ref_1 (instance, idx); 61850397Sobrien 61990075Sobrien return aref; 62090075Sobrien} 62190075Sobrien 622169689Skan/* Given a stable object pointer INSTANCE_PTR, return an expression which 623169689Skan yields a function pointer corresponding to vtable element INDEX. */ 62490075Sobrien 62550397Sobrientree 626169689Skanbuild_vfn_ref (tree instance_ptr, tree idx) 62750397Sobrien{ 628169689Skan tree aref; 62950397Sobrien 630169689Skan aref = build_vtbl_ref_1 (build_indirect_ref (instance_ptr, 0), idx); 631169689Skan 63290075Sobrien /* When using function descriptors, the address of the 63390075Sobrien vtable entry is treated as a function pointer. */ 63490075Sobrien if (TARGET_VTABLE_USES_DESCRIPTORS) 63590075Sobrien aref = build1 (NOP_EXPR, TREE_TYPE (aref), 63690075Sobrien build_unary_op (ADDR_EXPR, aref, /*noconvert=*/1)); 63750397Sobrien 638169689Skan /* Remember this as a method reference, for later devirtualization. */ 639169689Skan aref = build3 (OBJ_TYPE_REF, TREE_TYPE (aref), aref, instance_ptr, idx); 640169689Skan 64190075Sobrien return aref; 64218334Speter} 64318334Speter 64418334Speter/* Return the name of the virtual function table (as an IDENTIFIER_NODE) 64518334Speter for the given TYPE. */ 64650397Sobrien 64718334Speterstatic tree 648132718Skanget_vtable_name (tree type) 64918334Speter{ 65090075Sobrien return mangle_vtbl_for_type (type); 65118334Speter} 65218334Speter 653169689Skan/* DECL is an entity associated with TYPE, like a virtual table or an 654169689Skan implicitly generated constructor. Determine whether or not DECL 655169689Skan should have external or internal linkage at the object file 656169689Skan level. This routine does not deal with COMDAT linkage and other 657169689Skan similar complexities; it simply sets TREE_PUBLIC if it possible for 658169689Skan entities in other translation units to contain copies of DECL, in 659169689Skan the abstract. */ 66050397Sobrien 661169689Skanvoid 662169689Skanset_linkage_according_to_type (tree type, tree decl) 66350397Sobrien{ 664169689Skan /* If TYPE involves a local class in a function with internal 665169689Skan linkage, then DECL should have internal linkage too. Other local 666169689Skan classes have no linkage -- but if their containing functions 667169689Skan have external linkage, it makes sense for DECL to have external 668169689Skan linkage too. That will allow template definitions to be merged, 669169689Skan for example. */ 670169689Skan if (no_linkage_check (type, /*relaxed_p=*/true)) 671169689Skan { 672169689Skan TREE_PUBLIC (decl) = 0; 673169689Skan DECL_INTERFACE_KNOWN (decl) = 1; 674169689Skan } 675169689Skan else 676169689Skan TREE_PUBLIC (decl) = 1; 67750397Sobrien} 67850397Sobrien 67990075Sobrien/* Create a VAR_DECL for a primary or secondary vtable for CLASS_TYPE. 68090075Sobrien (For a secondary vtable for B-in-D, CLASS_TYPE should be D, not B.) 68190075Sobrien Use NAME for the name of the vtable, and VTABLE_TYPE for its type. */ 68250397Sobrien 68350397Sobrienstatic tree 684132718Skanbuild_vtable (tree class_type, tree name, tree vtable_type) 68550397Sobrien{ 68690075Sobrien tree decl; 68790075Sobrien 68890075Sobrien decl = build_lang_decl (VAR_DECL, name, vtable_type); 68990075Sobrien /* vtable names are already mangled; give them their DECL_ASSEMBLER_NAME 69090075Sobrien now to avoid confusion in mangle_decl. */ 69190075Sobrien SET_DECL_ASSEMBLER_NAME (decl, name); 69290075Sobrien DECL_CONTEXT (decl) = class_type; 69390075Sobrien DECL_ARTIFICIAL (decl) = 1; 69490075Sobrien TREE_STATIC (decl) = 1; 69590075Sobrien TREE_READONLY (decl) = 1; 69690075Sobrien DECL_VIRTUAL_P (decl) = 1; 697117395Skan DECL_ALIGN (decl) = TARGET_VTABLE_ENTRY_ALIGN; 698132718Skan DECL_VTABLE_OR_VTT_P (decl) = 1; 699132718Skan /* At one time the vtable info was grabbed 2 words at a time. This 700132718Skan fails on sparc unless you have 8-byte alignment. (tiemann) */ 701132718Skan DECL_ALIGN (decl) = MAX (TYPE_ALIGN (double_type_node), 702132718Skan DECL_ALIGN (decl)); 703169689Skan set_linkage_according_to_type (class_type, decl); 704169689Skan /* The vtable has not been defined -- yet. */ 705169689Skan DECL_EXTERNAL (decl) = 1; 706169689Skan DECL_NOT_REALLY_EXTERN (decl) = 1; 707132718Skan 708169689Skan /* Mark the VAR_DECL node representing the vtable itself as a 709169689Skan "gratuitous" one, thereby forcing dwarfout.c to ignore it. It 710169689Skan is rather important that such things be ignored because any 711169689Skan effort to actually generate DWARF for them will run into 712169689Skan trouble when/if we encounter code like: 71390075Sobrien 714169689Skan #pragma interface 715169689Skan struct S { virtual void member (); }; 716169689Skan 717169689Skan because the artificial declaration of the vtable itself (as 718169689Skan manufactured by the g++ front end) will say that the vtable is 719169689Skan a static member of `S' but only *after* the debug output for 720169689Skan the definition of `S' has already been output. This causes 721169689Skan grief because the DWARF entry for the definition of the vtable 722169689Skan will try to refer back to an earlier *declaration* of the 723169689Skan vtable as a static member of `S' and there won't be one. We 724169689Skan might be able to arrange to have the "vtable static member" 725169689Skan attached to the member list for `S' before the debug info for 726169689Skan `S' get written (which would solve the problem) but that would 727169689Skan require more intrusive changes to the g++ front end. */ 728169689Skan DECL_IGNORED_P (decl) = 1; 729169689Skan 73090075Sobrien return decl; 73150397Sobrien} 73250397Sobrien 73390075Sobrien/* Get the VAR_DECL of the vtable for TYPE. TYPE need not be polymorphic, 73490075Sobrien or even complete. If this does not exist, create it. If COMPLETE is 735117395Skan nonzero, then complete the definition of it -- that will render it 73690075Sobrien impossible to actually build the vtable, but is useful to get at those 73790075Sobrien which are known to exist in the runtime. */ 73850397Sobrien 739169689Skantree 740132718Skanget_vtable_decl (tree type, int complete) 74150397Sobrien{ 742117395Skan tree decl; 743117395Skan 744117395Skan if (CLASSTYPE_VTABLES (type)) 745117395Skan return CLASSTYPE_VTABLES (type); 746169689Skan 747132718Skan decl = build_vtable (type, get_vtable_name (type), vtbl_type_node); 748117395Skan CLASSTYPE_VTABLES (type) = decl; 749117395Skan 75090075Sobrien if (complete) 75190075Sobrien { 75290075Sobrien DECL_EXTERNAL (decl) = 1; 753169689Skan finish_decl (decl, NULL_TREE, NULL_TREE); 75490075Sobrien } 75552284Sobrien 75690075Sobrien return decl; 75790075Sobrien} 75850397Sobrien 75990075Sobrien/* Build the primary virtual function table for TYPE. If BINFO is 76090075Sobrien non-NULL, build the vtable starting with the initial approximation 76190075Sobrien that it is the same as the one which is the head of the association 762117395Skan list. Returns a nonzero value if a new vtable is actually 76390075Sobrien created. */ 76450397Sobrien 76590075Sobrienstatic int 766132718Skanbuild_primary_vtable (tree binfo, tree type) 76718334Speter{ 76890075Sobrien tree decl; 76990075Sobrien tree virtuals; 77018334Speter 77190075Sobrien decl = get_vtable_decl (type, /*complete=*/0); 772169689Skan 77318334Speter if (binfo) 77418334Speter { 775132718Skan if (BINFO_NEW_VTABLE_MARKED (binfo)) 77690075Sobrien /* We have already created a vtable for this base, so there's 77790075Sobrien no need to do it again. */ 77890075Sobrien return 0; 779169689Skan 780169689Skan virtuals = copy_list (BINFO_VIRTUALS (binfo)); 78190075Sobrien TREE_TYPE (decl) = TREE_TYPE (get_vtbl_decl_for_binfo (binfo)); 78290075Sobrien DECL_SIZE (decl) = TYPE_SIZE (TREE_TYPE (decl)); 78390075Sobrien DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (TREE_TYPE (decl)); 78418334Speter } 78518334Speter else 78618334Speter { 787169689Skan gcc_assert (TREE_TYPE (decl) == vtbl_type_node); 78818334Speter virtuals = NULL_TREE; 78918334Speter } 79018334Speter 79118334Speter#ifdef GATHER_STATISTICS 79218334Speter n_vtables += 1; 79318334Speter n_vtable_elems += list_length (virtuals); 79418334Speter#endif 79518334Speter 79618334Speter /* Initialize the association list for this type, based 79718334Speter on our first approximation. */ 798169689Skan BINFO_VTABLE (TYPE_BINFO (type)) = decl; 799169689Skan BINFO_VIRTUALS (TYPE_BINFO (type)) = virtuals; 800132718Skan SET_BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (type)); 80190075Sobrien return 1; 80218334Speter} 80318334Speter 80490075Sobrien/* Give BINFO a new virtual function table which is initialized 80550397Sobrien with a skeleton-copy of its original initialization. The only 80650397Sobrien entry that changes is the `delta' entry, so we can really 80750397Sobrien share a lot of structure. 80818334Speter 80990075Sobrien FOR_TYPE is the most derived type which caused this table to 81050397Sobrien be needed. 81118334Speter 812117395Skan Returns nonzero if we haven't met BINFO before. 81318334Speter 81450397Sobrien The order in which vtables are built (by calling this function) for 81550397Sobrien an object must remain the same, otherwise a binary incompatibility 81650397Sobrien can result. */ 81718334Speter 81890075Sobrienstatic int 819132718Skanbuild_secondary_vtable (tree binfo) 82018334Speter{ 821132718Skan if (BINFO_NEW_VTABLE_MARKED (binfo)) 82290075Sobrien /* We already created a vtable for this base. There's no need to 82390075Sobrien do it again. */ 82490075Sobrien return 0; 82518334Speter 82690075Sobrien /* Remember that we've created a vtable for this BINFO, so that we 82790075Sobrien don't try to do so again. */ 828132718Skan SET_BINFO_NEW_VTABLE_MARKED (binfo); 829169689Skan 83018334Speter /* Make fresh virtual list, so we can smash it later. */ 831169689Skan BINFO_VIRTUALS (binfo) = copy_list (BINFO_VIRTUALS (binfo)); 83218334Speter 83390075Sobrien /* Secondary vtables are laid out as part of the same structure as 83490075Sobrien the primary vtable. */ 83590075Sobrien BINFO_VTABLE (binfo) = NULL_TREE; 83690075Sobrien return 1; 83790075Sobrien} 83850397Sobrien 83990075Sobrien/* Create a new vtable for BINFO which is the hierarchy dominated by 840117395Skan T. Return nonzero if we actually created a new vtable. */ 84150397Sobrien 84290075Sobrienstatic int 843132718Skanmake_new_vtable (tree t, tree binfo) 84490075Sobrien{ 84590075Sobrien if (binfo == TYPE_BINFO (t)) 84690075Sobrien /* In this case, it is *type*'s vtable we are modifying. We start 84790075Sobrien with the approximation that its vtable is that of the 84890075Sobrien immediate base class. */ 849169689Skan return build_primary_vtable (binfo, t); 85018334Speter else 85190075Sobrien /* This is our very own copy of `basetype' to play with. Later, 85290075Sobrien we will fill in all the virtual functions that override the 85390075Sobrien virtual functions in these base classes which are not defined 85490075Sobrien by the current type. */ 855132718Skan return build_secondary_vtable (binfo); 85618334Speter} 85718334Speter 85890075Sobrien/* Make *VIRTUALS, an entry on the BINFO_VIRTUALS list for BINFO 85990075Sobrien (which is in the hierarchy dominated by T) list FNDECL as its 86090075Sobrien BV_FN. DELTA is the required constant adjustment from the `this' 86190075Sobrien pointer where the vtable entry appears to the `this' required when 86290075Sobrien the function is actually called. */ 86360967Sobrien 86490075Sobrienstatic void 865132718Skanmodify_vtable_entry (tree t, 866169689Skan tree binfo, 867169689Skan tree fndecl, 868169689Skan tree delta, 869169689Skan tree *virtuals) 87060967Sobrien{ 87190075Sobrien tree v; 87260967Sobrien 87390075Sobrien v = *virtuals; 87460967Sobrien 87590075Sobrien if (fndecl != BV_FN (v) 87690075Sobrien || !tree_int_cst_equal (delta, BV_DELTA (v))) 87790075Sobrien { 87890075Sobrien /* We need a new vtable for BINFO. */ 87990075Sobrien if (make_new_vtable (t, binfo)) 88090075Sobrien { 88190075Sobrien /* If we really did make a new vtable, we also made a copy 88290075Sobrien of the BINFO_VIRTUALS list. Now, we have to find the 88390075Sobrien corresponding entry in that list. */ 88490075Sobrien *virtuals = BINFO_VIRTUALS (binfo); 88590075Sobrien while (BV_FN (*virtuals) != BV_FN (v)) 88690075Sobrien *virtuals = TREE_CHAIN (*virtuals); 88790075Sobrien v = *virtuals; 88890075Sobrien } 88960967Sobrien 89090075Sobrien BV_DELTA (v) = delta; 89190075Sobrien BV_VCALL_INDEX (v) = NULL_TREE; 89290075Sobrien BV_FN (v) = fndecl; 89318334Speter } 89418334Speter} 89518334Speter 89618334Speter 897169689Skan/* Add method METHOD to class TYPE. If USING_DECL is non-null, it is 898169689Skan the USING_DECL naming METHOD. Returns true if the method could be 899169689Skan added to the method vec. */ 90018334Speter 901169689Skanbool 902169689Skanadd_method (tree type, tree method, tree using_decl) 90352284Sobrien{ 904169689Skan unsigned slot; 905169689Skan tree overload; 906169689Skan bool template_conv_p = false; 907169689Skan bool conv_p; 908169689Skan VEC(tree,gc) *method_vec; 909169689Skan bool complete_p; 910169689Skan bool insert_p = false; 911169689Skan tree current_fns; 91252284Sobrien 913132718Skan if (method == error_mark_node) 914169689Skan return false; 915132718Skan 916169689Skan complete_p = COMPLETE_TYPE_P (type); 917169689Skan conv_p = DECL_CONV_FN_P (method); 918169689Skan if (conv_p) 919169689Skan template_conv_p = (TREE_CODE (method) == TEMPLATE_DECL 920169689Skan && DECL_TEMPLATE_CONV_FN_P (method)); 92152284Sobrien 92290075Sobrien method_vec = CLASSTYPE_METHOD_VEC (type); 923169689Skan if (!method_vec) 924169689Skan { 925169689Skan /* Make a new method vector. We start with 8 entries. We must 926169689Skan allocate at least two (for constructors and destructors), and 927169689Skan we're going to end up with an assignment operator at some 928169689Skan point as well. */ 929169689Skan method_vec = VEC_alloc (tree, gc, 8); 930169689Skan /* Create slots for constructors and destructors. */ 931169689Skan VEC_quick_push (tree, method_vec, NULL_TREE); 932169689Skan VEC_quick_push (tree, method_vec, NULL_TREE); 933169689Skan CLASSTYPE_METHOD_VEC (type) = method_vec; 934169689Skan } 93552284Sobrien 936169689Skan /* Maintain TYPE_HAS_CONSTRUCTOR, etc. */ 937169689Skan grok_special_member_properties (method); 938169689Skan 93990075Sobrien /* Constructors and destructors go in special slots. */ 94090075Sobrien if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (method)) 94190075Sobrien slot = CLASSTYPE_CONSTRUCTOR_SLOT; 94290075Sobrien else if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (method)) 943132718Skan { 944132718Skan slot = CLASSTYPE_DESTRUCTOR_SLOT; 945169689Skan 946132718Skan if (TYPE_FOR_JAVA (type)) 947169689Skan { 948169689Skan if (!DECL_ARTIFICIAL (method)) 949169689Skan error ("Java class %qT cannot have a destructor", type); 950169689Skan else if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)) 951169689Skan error ("Java class %qT cannot have an implicit non-trivial " 952169689Skan "destructor", 953169689Skan type); 954169689Skan } 955132718Skan } 95690075Sobrien else 95718334Speter { 958169689Skan tree m; 959169689Skan 960169689Skan insert_p = true; 96190075Sobrien /* See if we already have an entry with this name. */ 962169689Skan for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT; 963169689Skan VEC_iterate (tree, method_vec, slot, m); 964169689Skan ++slot) 965117395Skan { 966117395Skan m = OVL_CURRENT (m); 967117395Skan if (template_conv_p) 968117395Skan { 969169689Skan if (TREE_CODE (m) == TEMPLATE_DECL 970169689Skan && DECL_TEMPLATE_CONV_FN_P (m)) 971169689Skan insert_p = false; 972117395Skan break; 973117395Skan } 974169689Skan if (conv_p && !DECL_CONV_FN_P (m)) 975169689Skan break; 976117395Skan if (DECL_NAME (m) == DECL_NAME (method)) 97790075Sobrien { 978169689Skan insert_p = false; 979169689Skan break; 98018334Speter } 981169689Skan if (complete_p 982169689Skan && !DECL_CONV_FN_P (m) 983169689Skan && DECL_NAME (m) > DECL_NAME (method)) 984169689Skan break; 98552284Sobrien } 98690075Sobrien } 987169689Skan current_fns = insert_p ? NULL_TREE : VEC_index (tree, method_vec, slot); 988169689Skan 989132718Skan if (processing_template_decl) 99090075Sobrien /* TYPE is a template class. Don't issue any errors now; wait 99190075Sobrien until instantiation time to complain. */ 99290075Sobrien ; 99390075Sobrien else 99490075Sobrien { 99590075Sobrien tree fns; 99690075Sobrien 99790075Sobrien /* Check to see if we've already got this method. */ 998169689Skan for (fns = current_fns; fns; fns = OVL_NEXT (fns)) 99952284Sobrien { 100090075Sobrien tree fn = OVL_CURRENT (fns); 1001161651Skan tree fn_type; 1002161651Skan tree method_type; 1003102780Skan tree parms1; 1004102780Skan tree parms2; 1005102780Skan 100690075Sobrien if (TREE_CODE (fn) != TREE_CODE (method)) 100790075Sobrien continue; 100818334Speter 1009102780Skan /* [over.load] Member function declarations with the 1010102780Skan same name and the same parameter types cannot be 1011102780Skan overloaded if any of them is a static member 1012102780Skan function declaration. 1013102780Skan 1014102780Skan [namespace.udecl] When a using-declaration brings names 1015102780Skan from a base class into a derived class scope, member 1016102780Skan functions in the derived class override and/or hide member 1017102780Skan functions with the same name and parameter types in a base 1018102780Skan class (rather than conflicting). */ 1019161651Skan fn_type = TREE_TYPE (fn); 1020161651Skan method_type = TREE_TYPE (method); 1021161651Skan parms1 = TYPE_ARG_TYPES (fn_type); 1022161651Skan parms2 = TYPE_ARG_TYPES (method_type); 1023102780Skan 1024102780Skan /* Compare the quals on the 'this' parm. Don't compare 1025102780Skan the whole types, as used functions are treated as 1026102780Skan coming from the using class in overload resolution. */ 1027102780Skan if (! DECL_STATIC_FUNCTION_P (fn) 1028102780Skan && ! DECL_STATIC_FUNCTION_P (method) 1029102780Skan && (TYPE_QUALS (TREE_TYPE (TREE_VALUE (parms1))) 1030102780Skan != TYPE_QUALS (TREE_TYPE (TREE_VALUE (parms2))))) 1031161651Skan continue; 1032169689Skan 1033169689Skan /* For templates, the return type and template parameters 1034169689Skan must be identical. */ 1035107590Sobrien if (TREE_CODE (fn) == TEMPLATE_DECL 1036161651Skan && (!same_type_p (TREE_TYPE (fn_type), 1037161651Skan TREE_TYPE (method_type)) 1038161651Skan || !comp_template_parms (DECL_TEMPLATE_PARMS (fn), 1039161651Skan DECL_TEMPLATE_PARMS (method)))) 1040161651Skan continue; 1041169689Skan 1042102780Skan if (! DECL_STATIC_FUNCTION_P (fn)) 1043102780Skan parms1 = TREE_CHAIN (parms1); 1044102780Skan if (! DECL_STATIC_FUNCTION_P (method)) 1045102780Skan parms2 = TREE_CHAIN (parms2); 1046102780Skan 1047169689Skan if (compparms (parms1, parms2) 1048169689Skan && (!DECL_CONV_FN_P (fn) 1049161651Skan || same_type_p (TREE_TYPE (fn_type), 1050161651Skan TREE_TYPE (method_type)))) 105118334Speter { 1052169689Skan if (using_decl) 1053169689Skan { 1054169689Skan if (DECL_CONTEXT (fn) == type) 1055169689Skan /* Defer to the local function. */ 1056169689Skan return false; 1057169689Skan if (DECL_CONTEXT (fn) == DECL_CONTEXT (method)) 1058169689Skan error ("repeated using declaration %q+D", using_decl); 1059169689Skan else 1060169689Skan error ("using declaration %q+D conflicts with a previous using declaration", 1061169689Skan using_decl); 1062169689Skan } 1063102780Skan else 106452284Sobrien { 1065169689Skan error ("%q+#D cannot be overloaded", method); 1066169689Skan error ("with %q+#D", fn); 1067169689Skan } 106890075Sobrien 1069169689Skan /* We don't call duplicate_decls here to merge the 1070169689Skan declarations because that will confuse things if the 1071169689Skan methods have inline definitions. In particular, we 1072169689Skan will crash while processing the definitions. */ 1073169689Skan return false; 107490075Sobrien } 107518334Speter } 107690075Sobrien } 107752284Sobrien 1078169689Skan /* A class should never have more than one destructor. */ 1079169689Skan if (current_fns && DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (method)) 1080169689Skan return false; 108152284Sobrien 1082169689Skan /* Add the new binding. */ 1083169689Skan overload = build_overload (method, current_fns); 1084169689Skan 1085169689Skan if (conv_p) 1086169689Skan TYPE_HAS_CONVERSION (type) = 1; 1087169689Skan else if (slot >= CLASSTYPE_FIRST_CONVERSION_SLOT && !complete_p) 1088169689Skan push_class_level_binding (DECL_NAME (method), overload); 1089169689Skan 1090169689Skan if (insert_p) 1091169689Skan { 1092169689Skan bool reallocated; 1093169689Skan 1094169689Skan /* We only expect to add few methods in the COMPLETE_P case, so 1095169689Skan just make room for one more method in that case. */ 1096169689Skan if (complete_p) 1097169689Skan reallocated = VEC_reserve_exact (tree, gc, method_vec, 1); 1098169689Skan else 1099169689Skan reallocated = VEC_reserve (tree, gc, method_vec, 1); 1100169689Skan if (reallocated) 1101169689Skan CLASSTYPE_METHOD_VEC (type) = method_vec; 1102169689Skan if (slot == VEC_length (tree, method_vec)) 1103169689Skan VEC_quick_push (tree, method_vec, overload); 1104169689Skan else 1105169689Skan VEC_quick_insert (tree, method_vec, slot, overload); 1106169689Skan } 1107169689Skan else 1108169689Skan /* Replace the current slot. */ 1109169689Skan VEC_replace (tree, method_vec, slot, overload); 1110169689Skan return true; 111118334Speter} 111218334Speter 111318334Speter/* Subroutines of finish_struct. */ 111418334Speter 111590075Sobrien/* Change the access of FDECL to ACCESS in T. Return 1 if change was 111690075Sobrien legit, otherwise return 0. */ 111750397Sobrien 111818334Speterstatic int 1119132718Skanalter_access (tree t, tree fdecl, tree access) 112018334Speter{ 112190075Sobrien tree elem; 112290075Sobrien 112390075Sobrien if (!DECL_LANG_SPECIFIC (fdecl)) 112490075Sobrien retrofit_lang_decl (fdecl); 112590075Sobrien 1126169689Skan gcc_assert (!DECL_DISCRIMINATOR_P (fdecl)); 112790075Sobrien 112890075Sobrien elem = purpose_member (t, DECL_ACCESS (fdecl)); 112950397Sobrien if (elem) 113018334Speter { 113150397Sobrien if (TREE_VALUE (elem) != access) 113218334Speter { 113350397Sobrien if (TREE_CODE (TREE_TYPE (fdecl)) == FUNCTION_DECL) 1134169689Skan error ("conflicting access specifications for method" 1135169689Skan " %q+D, ignored", TREE_TYPE (fdecl)); 113650397Sobrien else 1137169689Skan error ("conflicting access specifications for field %qE, ignored", 1138169689Skan DECL_NAME (fdecl)); 113918334Speter } 114018334Speter else 114150397Sobrien { 114250397Sobrien /* They're changing the access to the same thing they changed 114350397Sobrien it to before. That's OK. */ 114450397Sobrien ; 114550397Sobrien } 114618334Speter } 114750397Sobrien else 114818334Speter { 1149169689Skan perform_or_defer_access_check (TYPE_BINFO (t), fdecl, fdecl); 115050397Sobrien DECL_ACCESS (fdecl) = tree_cons (t, access, DECL_ACCESS (fdecl)); 115118334Speter return 1; 115218334Speter } 115318334Speter return 0; 115418334Speter} 115518334Speter 115690075Sobrien/* Process the USING_DECL, which is a member of T. */ 115718334Speter 115852284Sobrienstatic void 1159132718Skanhandle_using_decl (tree using_decl, tree t) 116018334Speter{ 1161169689Skan tree decl = USING_DECL_DECLS (using_decl); 116250397Sobrien tree name = DECL_NAME (using_decl); 116350397Sobrien tree access 116450397Sobrien = TREE_PRIVATE (using_decl) ? access_private_node 116550397Sobrien : TREE_PROTECTED (using_decl) ? access_protected_node 116650397Sobrien : access_public_node; 116750397Sobrien tree flist = NULL_TREE; 116890075Sobrien tree old_value; 116950397Sobrien 1170169689Skan gcc_assert (!processing_template_decl && decl); 1171117395Skan 1172169689Skan old_value = lookup_member (t, name, /*protect=*/0, /*want_type=*/false); 117390075Sobrien if (old_value) 117450397Sobrien { 117590075Sobrien if (is_overloaded_fn (old_value)) 117690075Sobrien old_value = OVL_CURRENT (old_value); 117790075Sobrien 117890075Sobrien if (DECL_P (old_value) && DECL_CONTEXT (old_value) == t) 117990075Sobrien /* OK */; 118090075Sobrien else 118190075Sobrien old_value = NULL_TREE; 118250397Sobrien } 118350397Sobrien 1184169689Skan cp_emit_debug_info_for_using (decl, USING_DECL_SCOPE (using_decl)); 118590075Sobrien 1186169689Skan if (is_overloaded_fn (decl)) 1187169689Skan flist = decl; 1188169689Skan 118990075Sobrien if (! old_value) 119090075Sobrien ; 119190075Sobrien else if (is_overloaded_fn (old_value)) 119250397Sobrien { 119390075Sobrien if (flist) 119490075Sobrien /* It's OK to use functions from a base when there are functions with 119590075Sobrien the same name already present in the current class. */; 119690075Sobrien else 119750397Sobrien { 1198169689Skan error ("%q+D invalid in %q#T", using_decl, t); 1199169689Skan error (" because of local method %q+#D with same name", 1200169689Skan OVL_CURRENT (old_value)); 120190075Sobrien return; 120250397Sobrien } 120350397Sobrien } 120490075Sobrien else if (!DECL_ARTIFICIAL (old_value)) 120590075Sobrien { 1206169689Skan error ("%q+D invalid in %q#T", using_decl, t); 1207169689Skan error (" because of local member %q+#D with same name", old_value); 120890075Sobrien return; 120990075Sobrien } 1210169689Skan 1211132718Skan /* Make type T see field decl FDECL with access ACCESS. */ 121290075Sobrien if (flist) 121390075Sobrien for (; flist; flist = OVL_NEXT (flist)) 121490075Sobrien { 1215169689Skan add_method (t, OVL_CURRENT (flist), using_decl); 121690075Sobrien alter_access (t, OVL_CURRENT (flist), access); 121790075Sobrien } 121850397Sobrien else 1219169689Skan alter_access (t, decl, access); 122018334Speter} 122118334Speter 1222169689Skan/* Run through the base classes of T, updating CANT_HAVE_CONST_CTOR_P, 1223169689Skan and NO_CONST_ASN_REF_P. Also set flag bits in T based on 1224169689Skan properties of the bases. */ 122590075Sobrien 122690075Sobrienstatic void 1227132718Skancheck_bases (tree t, 1228169689Skan int* cant_have_const_ctor_p, 1229169689Skan int* no_const_asn_ref_p) 123018334Speter{ 123190075Sobrien int i; 123290075Sobrien int seen_non_virtual_nearly_empty_base_p; 1233169689Skan tree base_binfo; 1234169689Skan tree binfo; 123518334Speter 123690075Sobrien seen_non_virtual_nearly_empty_base_p = 0; 123718334Speter 1238169689Skan for (binfo = TYPE_BINFO (t), i = 0; 1239169689Skan BINFO_BASE_ITERATE (binfo, i, base_binfo); i++) 124018334Speter { 1241169689Skan tree basetype = TREE_TYPE (base_binfo); 124218334Speter 1243169689Skan gcc_assert (COMPLETE_TYPE_P (basetype)); 124450397Sobrien 124590075Sobrien /* Effective C++ rule 14. We only need to check TYPE_POLYMORPHIC_P 124690075Sobrien here because the case of virtual functions but non-virtual 124790075Sobrien dtor is handled in finish_struct_1. */ 1248169689Skan if (!TYPE_POLYMORPHIC_P (basetype)) 1249169689Skan warning (OPT_Weffc__, 1250169689Skan "base class %q#T has a non-virtual destructor", basetype); 125190075Sobrien 125290075Sobrien /* If the base class doesn't have copy constructors or 125390075Sobrien assignment operators that take const references, then the 125490075Sobrien derived class cannot have such a member automatically 125590075Sobrien generated. */ 125650397Sobrien if (! TYPE_HAS_CONST_INIT_REF (basetype)) 125790075Sobrien *cant_have_const_ctor_p = 1; 125890075Sobrien if (TYPE_HAS_ASSIGN_REF (basetype) 125990075Sobrien && !TYPE_HAS_CONST_ASSIGN_REF (basetype)) 126090075Sobrien *no_const_asn_ref_p = 1; 126118334Speter 1262169689Skan if (BINFO_VIRTUAL_P (base_binfo)) 1263117395Skan /* A virtual base does not effect nearly emptiness. */ 126490075Sobrien ; 126590075Sobrien else if (CLASSTYPE_NEARLY_EMPTY_P (basetype)) 126690075Sobrien { 126790075Sobrien if (seen_non_virtual_nearly_empty_base_p) 126890075Sobrien /* And if there is more than one nearly empty base, then the 126990075Sobrien derived class is not nearly empty either. */ 127090075Sobrien CLASSTYPE_NEARLY_EMPTY_P (t) = 0; 127190075Sobrien else 1272117395Skan /* Remember we've seen one. */ 127390075Sobrien seen_non_virtual_nearly_empty_base_p = 1; 127490075Sobrien } 127590075Sobrien else if (!is_empty_class (basetype)) 127690075Sobrien /* If the base class is not empty or nearly empty, then this 127790075Sobrien class cannot be nearly empty. */ 127890075Sobrien CLASSTYPE_NEARLY_EMPTY_P (t) = 0; 127918334Speter 128090075Sobrien /* A lot of properties from the bases also apply to the derived 128190075Sobrien class. */ 128218334Speter TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (basetype); 1283169689Skan TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) 128490075Sobrien |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype); 1285169689Skan TYPE_HAS_COMPLEX_ASSIGN_REF (t) 128690075Sobrien |= TYPE_HAS_COMPLEX_ASSIGN_REF (basetype); 128718334Speter TYPE_HAS_COMPLEX_INIT_REF (t) |= TYPE_HAS_COMPLEX_INIT_REF (basetype); 128890075Sobrien TYPE_POLYMORPHIC_P (t) |= TYPE_POLYMORPHIC_P (basetype); 1289169689Skan CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) 1290107590Sobrien |= CLASSTYPE_CONTAINS_EMPTY_CLASS_P (basetype); 129190075Sobrien } 129290075Sobrien} 129318334Speter 1294169689Skan/* Determine all the primary bases within T. Sets BINFO_PRIMARY_BASE_P for 1295169689Skan those that are primaries. Sets BINFO_LOST_PRIMARY_P for those 1296169689Skan that have had a nearly-empty virtual primary base stolen by some 1297169689Skan other base in the hierarchy. Determines CLASSTYPE_PRIMARY_BASE for 1298169689Skan T. */ 129990075Sobrien 130090075Sobrienstatic void 1301169689Skandetermine_primary_bases (tree t) 130290075Sobrien{ 1303169689Skan unsigned i; 1304169689Skan tree primary = NULL_TREE; 1305169689Skan tree type_binfo = TYPE_BINFO (t); 1306169689Skan tree base_binfo; 1307169689Skan 1308169689Skan /* Determine the primary bases of our bases. */ 1309169689Skan for (base_binfo = TREE_CHAIN (type_binfo); base_binfo; 1310169689Skan base_binfo = TREE_CHAIN (base_binfo)) 131190075Sobrien { 1312169689Skan tree primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (base_binfo)); 131390075Sobrien 1314169689Skan /* See if we're the non-virtual primary of our inheritance 1315169689Skan chain. */ 1316169689Skan if (!BINFO_VIRTUAL_P (base_binfo)) 1317132718Skan { 1318169689Skan tree parent = BINFO_INHERITANCE_CHAIN (base_binfo); 1319169689Skan tree parent_primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (parent)); 1320169689Skan 1321169689Skan if (parent_primary 1322169689Skan && SAME_BINFO_TYPE_P (BINFO_TYPE (base_binfo), 1323169689Skan BINFO_TYPE (parent_primary))) 1324169689Skan /* We are the primary binfo. */ 1325169689Skan BINFO_PRIMARY_P (base_binfo) = 1; 1326132718Skan } 1327169689Skan /* Determine if we have a virtual primary base, and mark it so. 1328169689Skan */ 1329169689Skan if (primary && BINFO_VIRTUAL_P (primary)) 133018334Speter { 1331169689Skan tree this_primary = copied_binfo (primary, base_binfo); 133218334Speter 1333169689Skan if (BINFO_PRIMARY_P (this_primary)) 1334169689Skan /* Someone already claimed this base. */ 1335169689Skan BINFO_LOST_PRIMARY_P (base_binfo) = 1; 133618334Speter else 133718334Speter { 1338169689Skan tree delta; 133990075Sobrien 1340169689Skan BINFO_PRIMARY_P (this_primary) = 1; 1341169689Skan BINFO_INHERITANCE_CHAIN (this_primary) = base_binfo; 1342169689Skan 1343169689Skan /* A virtual binfo might have been copied from within 1344169689Skan another hierarchy. As we're about to use it as a 1345169689Skan primary base, make sure the offsets match. */ 1346169689Skan delta = size_diffop (convert (ssizetype, 1347169689Skan BINFO_OFFSET (base_binfo)), 1348169689Skan convert (ssizetype, 1349169689Skan BINFO_OFFSET (this_primary))); 1350169689Skan 1351169689Skan propagate_binfo_offsets (this_primary, delta); 135290075Sobrien } 135390075Sobrien } 135490075Sobrien } 135518334Speter 1356169689Skan /* First look for a dynamic direct non-virtual base. */ 1357169689Skan for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, base_binfo); i++) 135890075Sobrien { 1359169689Skan tree basetype = BINFO_TYPE (base_binfo); 136090075Sobrien 1361169689Skan if (TYPE_CONTAINS_VPTR_P (basetype) && !BINFO_VIRTUAL_P (base_binfo)) 136290075Sobrien { 1363169689Skan primary = base_binfo; 1364169689Skan goto found; 136518334Speter } 136618334Speter } 136718334Speter 136890075Sobrien /* A "nearly-empty" virtual base class can be the primary base 1369169689Skan class, if no non-virtual polymorphic base can be found. Look for 1370169689Skan a nearly-empty virtual dynamic base that is not already a primary 1371169689Skan base of something in the hierarchy. If there is no such base, 1372169689Skan just pick the first nearly-empty virtual base. */ 1373169689Skan 1374169689Skan for (base_binfo = TREE_CHAIN (type_binfo); base_binfo; 1375169689Skan base_binfo = TREE_CHAIN (base_binfo)) 1376169689Skan if (BINFO_VIRTUAL_P (base_binfo) 1377169689Skan && CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (base_binfo))) 1378169689Skan { 1379169689Skan if (!BINFO_PRIMARY_P (base_binfo)) 1380169689Skan { 1381169689Skan /* Found one that is not primary. */ 1382169689Skan primary = base_binfo; 1383169689Skan goto found; 1384169689Skan } 1385169689Skan else if (!primary) 1386169689Skan /* Remember the first candidate. */ 1387169689Skan primary = base_binfo; 1388169689Skan } 1389169689Skan 1390169689Skan found: 1391169689Skan /* If we've got a primary base, use it. */ 1392169689Skan if (primary) 139390075Sobrien { 1394169689Skan tree basetype = BINFO_TYPE (primary); 139518334Speter 1396169689Skan CLASSTYPE_PRIMARY_BINFO (t) = primary; 1397169689Skan if (BINFO_PRIMARY_P (primary)) 1398169689Skan /* We are stealing a primary base. */ 1399169689Skan BINFO_LOST_PRIMARY_P (BINFO_INHERITANCE_CHAIN (primary)) = 1; 1400169689Skan BINFO_PRIMARY_P (primary) = 1; 1401169689Skan if (BINFO_VIRTUAL_P (primary)) 140290075Sobrien { 1403169689Skan tree delta; 140450397Sobrien 1405169689Skan BINFO_INHERITANCE_CHAIN (primary) = type_binfo; 1406169689Skan /* A virtual binfo might have been copied from within 1407169689Skan another hierarchy. As we're about to use it as a primary 1408169689Skan base, make sure the offsets match. */ 1409169689Skan delta = size_diffop (ssize_int (0), 1410169689Skan convert (ssizetype, BINFO_OFFSET (primary))); 141150397Sobrien 1412169689Skan propagate_binfo_offsets (primary, delta); 141390075Sobrien } 141490075Sobrien 1415169689Skan primary = TYPE_BINFO (basetype); 1416169689Skan 1417169689Skan TYPE_VFIELD (t) = TYPE_VFIELD (basetype); 1418169689Skan BINFO_VTABLE (type_binfo) = BINFO_VTABLE (primary); 1419169689Skan BINFO_VIRTUALS (type_binfo) = BINFO_VIRTUALS (primary); 142090075Sobrien } 142118334Speter} 142218334Speter 142390075Sobrien/* Set memoizing fields and bits of T (and its variants) for later 142490075Sobrien use. */ 142550397Sobrien 142618334Speterstatic void 1427132718Skanfinish_struct_bits (tree t) 142818334Speter{ 1429169689Skan tree variants; 143018334Speter 143118334Speter /* Fix up variants (if any). */ 1432169689Skan for (variants = TYPE_NEXT_VARIANT (t); 1433169689Skan variants; 1434169689Skan variants = TYPE_NEXT_VARIANT (variants)) 143518334Speter { 143618334Speter /* These fields are in the _TYPE part of the node, not in 143718334Speter the TYPE_LANG_SPECIFIC component, so they are not shared. */ 143818334Speter TYPE_HAS_CONSTRUCTOR (variants) = TYPE_HAS_CONSTRUCTOR (t); 143918334Speter TYPE_NEEDS_CONSTRUCTING (variants) = TYPE_NEEDS_CONSTRUCTING (t); 1440169689Skan TYPE_HAS_NONTRIVIAL_DESTRUCTOR (variants) 144190075Sobrien = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t); 144218334Speter 144390075Sobrien TYPE_POLYMORPHIC_P (variants) = TYPE_POLYMORPHIC_P (t); 1444169689Skan 1445169689Skan TYPE_BINFO (variants) = TYPE_BINFO (t); 1446169689Skan 144718334Speter /* Copy whatever these are holding today. */ 1448169689Skan TYPE_VFIELD (variants) = TYPE_VFIELD (t); 1449169689Skan TYPE_METHODS (variants) = TYPE_METHODS (t); 145050397Sobrien TYPE_FIELDS (variants) = TYPE_FIELDS (t); 145118334Speter } 145218334Speter 1453169689Skan if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) && TYPE_POLYMORPHIC_P (t)) 1454169689Skan /* For a class w/o baseclasses, 'finish_struct' has set 1455169689Skan CLASSTYPE_PURE_VIRTUALS correctly (by definition). 1456169689Skan Similarly for a class whose base classes do not have vtables. 1457169689Skan When neither of these is true, we might have removed abstract 1458169689Skan virtuals (by providing a definition), added some (by declaring 1459169689Skan new ones), or redeclared ones from a base class. We need to 1460169689Skan recalculate what's really an abstract virtual at this point (by 1461169689Skan looking in the vtables). */ 1462169689Skan get_pure_virtuals (t); 146318334Speter 1464169689Skan /* If this type has a copy constructor or a destructor, force its 1465169689Skan mode to be BLKmode, and force its TREE_ADDRESSABLE bit to be 1466169689Skan nonzero. This will cause it to be passed by invisible reference 1467169689Skan and prevent it from being returned in a register. */ 146896263Sobrien if (! TYPE_HAS_TRIVIAL_INIT_REF (t) || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)) 146918334Speter { 147018334Speter tree variants; 147150397Sobrien DECL_MODE (TYPE_MAIN_DECL (t)) = BLKmode; 147218334Speter for (variants = t; variants; variants = TYPE_NEXT_VARIANT (variants)) 147318334Speter { 147418334Speter TYPE_MODE (variants) = BLKmode; 147518334Speter TREE_ADDRESSABLE (variants) = 1; 147618334Speter } 147718334Speter } 147818334Speter} 147918334Speter 148052284Sobrien/* Issue warnings about T having private constructors, but no friends, 1481169689Skan and so forth. 148250397Sobrien 148352284Sobrien HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or 148452284Sobrien static members. HAS_NONPRIVATE_STATIC_FN is nonzero if T has any 148552284Sobrien non-private static member functions. */ 148652284Sobrien 148718334Speterstatic void 1488132718Skanmaybe_warn_about_overly_private_class (tree t) 148918334Speter{ 149052284Sobrien int has_member_fn = 0; 149152284Sobrien int has_nonprivate_method = 0; 149252284Sobrien tree fn; 149350397Sobrien 149452284Sobrien if (!warn_ctor_dtor_privacy 149552284Sobrien /* If the class has friends, those entities might create and 149652284Sobrien access instances, so we should not warn. */ 149752284Sobrien || (CLASSTYPE_FRIEND_CLASSES (t) 149852284Sobrien || DECL_FRIENDLIST (TYPE_MAIN_DECL (t))) 149952284Sobrien /* We will have warned when the template was declared; there's 150052284Sobrien no need to warn on every instantiation. */ 150152284Sobrien || CLASSTYPE_TEMPLATE_INSTANTIATION (t)) 1502169689Skan /* There's no reason to even consider warning about this 150352284Sobrien class. */ 150452284Sobrien return; 1505169689Skan 150652284Sobrien /* We only issue one warning, if more than one applies, because 150752284Sobrien otherwise, on code like: 150850397Sobrien 150952284Sobrien class A { 151052284Sobrien // Oops - forgot `public:' 151152284Sobrien A(); 151252284Sobrien A(const A&); 151352284Sobrien ~A(); 151452284Sobrien }; 151518334Speter 151652284Sobrien we warn several times about essentially the same problem. */ 151752284Sobrien 151852284Sobrien /* Check to see if all (non-constructor, non-destructor) member 151952284Sobrien functions are private. (Since there are no friends or 152052284Sobrien non-private statics, we can't ever call any of the private member 152152284Sobrien functions.) */ 152252284Sobrien for (fn = TYPE_METHODS (t); fn; fn = TREE_CHAIN (fn)) 152352284Sobrien /* We're not interested in compiler-generated methods; they don't 152452284Sobrien provide any way to call private members. */ 1525169689Skan if (!DECL_ARTIFICIAL (fn)) 152652284Sobrien { 152752284Sobrien if (!TREE_PRIVATE (fn)) 152852284Sobrien { 1529169689Skan if (DECL_STATIC_FUNCTION_P (fn)) 153052284Sobrien /* A non-private static member function is just like a 153152284Sobrien friend; it can create and invoke private member 153252284Sobrien functions, and be accessed without a class 153352284Sobrien instance. */ 153452284Sobrien return; 1535169689Skan 153652284Sobrien has_nonprivate_method = 1; 1537117395Skan /* Keep searching for a static member function. */ 153852284Sobrien } 153952284Sobrien else if (!DECL_CONSTRUCTOR_P (fn) && !DECL_DESTRUCTOR_P (fn)) 154052284Sobrien has_member_fn = 1; 1541169689Skan } 154252284Sobrien 1543169689Skan if (!has_nonprivate_method && has_member_fn) 154418334Speter { 154552284Sobrien /* There are no non-private methods, and there's at least one 154652284Sobrien private member function that isn't a constructor or 154752284Sobrien destructor. (If all the private members are 154852284Sobrien constructors/destructors we want to use the code below that 154952284Sobrien issues error messages specifically referring to 155052284Sobrien constructors/destructors.) */ 1551169689Skan unsigned i; 1552132718Skan tree binfo = TYPE_BINFO (t); 1553169689Skan 1554169689Skan for (i = 0; i != BINFO_N_BASE_BINFOS (binfo); i++) 1555169689Skan if (BINFO_BASE_ACCESS (binfo, i) != access_private_node) 155652284Sobrien { 155752284Sobrien has_nonprivate_method = 1; 155852284Sobrien break; 155952284Sobrien } 1560169689Skan if (!has_nonprivate_method) 156152284Sobrien { 1562169689Skan warning (OPT_Wctor_dtor_privacy, 1563169689Skan "all member functions in class %qT are private", t); 156452284Sobrien return; 156552284Sobrien } 156618334Speter } 156752284Sobrien 156852284Sobrien /* Even if some of the member functions are non-private, the class 156952284Sobrien won't be useful for much if all the constructors or destructors 157052284Sobrien are private: such an object can never be created or destroyed. */ 1571169689Skan fn = CLASSTYPE_DESTRUCTORS (t); 1572169689Skan if (fn && TREE_PRIVATE (fn)) 157352284Sobrien { 1574169689Skan warning (OPT_Wctor_dtor_privacy, 1575169689Skan "%q#T only defines a private destructor and has no friends", 1576132718Skan t); 1577132718Skan return; 157852284Sobrien } 157952284Sobrien 1580169689Skan if (TYPE_HAS_CONSTRUCTOR (t) 1581169689Skan /* Implicitly generated constructors are always public. */ 1582169689Skan && (!CLASSTYPE_LAZY_DEFAULT_CTOR (t) 1583169689Skan || !CLASSTYPE_LAZY_COPY_CTOR (t))) 158452284Sobrien { 158552284Sobrien int nonprivate_ctor = 0; 1586169689Skan 158752284Sobrien /* If a non-template class does not define a copy 158852284Sobrien constructor, one is defined for it, enabling it to avoid 158952284Sobrien this warning. For a template class, this does not 159052284Sobrien happen, and so we would normally get a warning on: 159152284Sobrien 1592169689Skan template <class T> class C { private: C(); }; 1593169689Skan 159452284Sobrien To avoid this asymmetry, we check TYPE_HAS_INIT_REF. All 159552284Sobrien complete non-template or fully instantiated classes have this 159652284Sobrien flag set. */ 159752284Sobrien if (!TYPE_HAS_INIT_REF (t)) 159852284Sobrien nonprivate_ctor = 1; 1599169689Skan else 1600169689Skan for (fn = CLASSTYPE_CONSTRUCTORS (t); fn; fn = OVL_NEXT (fn)) 160152284Sobrien { 160252284Sobrien tree ctor = OVL_CURRENT (fn); 160352284Sobrien /* Ideally, we wouldn't count copy constructors (or, in 160452284Sobrien fact, any constructor that takes an argument of the 160552284Sobrien class type as a parameter) because such things cannot 160652284Sobrien be used to construct an instance of the class unless 160752284Sobrien you already have one. But, for now at least, we're 160852284Sobrien more generous. */ 160952284Sobrien if (! TREE_PRIVATE (ctor)) 161052284Sobrien { 161152284Sobrien nonprivate_ctor = 1; 161252284Sobrien break; 161352284Sobrien } 161452284Sobrien } 161552284Sobrien 161652284Sobrien if (nonprivate_ctor == 0) 161752284Sobrien { 1618169689Skan warning (OPT_Wctor_dtor_privacy, 1619169689Skan "%q#T only defines private constructors and has no friends", 1620169689Skan t); 162152284Sobrien return; 162252284Sobrien } 162352284Sobrien } 162418334Speter} 162518334Speter 1626132718Skanstatic struct { 1627132718Skan gt_pointer_operator new_value; 1628132718Skan void *cookie; 1629132718Skan} resort_data; 163052284Sobrien 1631132718Skan/* Comparison function to compare two TYPE_METHOD_VEC entries by name. */ 1632132718Skan 163390075Sobrienstatic int 1634132718Skanmethod_name_cmp (const void* m1_p, const void* m2_p) 163590075Sobrien{ 1636169689Skan const tree *const m1 = (const tree *) m1_p; 1637169689Skan const tree *const m2 = (const tree *) m2_p; 1638169689Skan 1639132718Skan if (*m1 == NULL_TREE && *m2 == NULL_TREE) 1640132718Skan return 0; 1641132718Skan if (*m1 == NULL_TREE) 164290075Sobrien return -1; 1643132718Skan if (*m2 == NULL_TREE) 164490075Sobrien return 1; 1645132718Skan if (DECL_NAME (OVL_CURRENT (*m1)) < DECL_NAME (OVL_CURRENT (*m2))) 164690075Sobrien return -1; 164790075Sobrien return 1; 164890075Sobrien} 164990075Sobrien 1650132718Skan/* This routine compares two fields like method_name_cmp but using the 1651132718Skan pointer operator in resort_field_decl_data. */ 165290075Sobrien 165390075Sobrienstatic int 1654132718Skanresort_method_name_cmp (const void* m1_p, const void* m2_p) 165590075Sobrien{ 1656169689Skan const tree *const m1 = (const tree *) m1_p; 1657169689Skan const tree *const m2 = (const tree *) m2_p; 165890075Sobrien if (*m1 == NULL_TREE && *m2 == NULL_TREE) 165990075Sobrien return 0; 166090075Sobrien if (*m1 == NULL_TREE) 166190075Sobrien return -1; 166290075Sobrien if (*m2 == NULL_TREE) 166390075Sobrien return 1; 1664132718Skan { 1665132718Skan tree d1 = DECL_NAME (OVL_CURRENT (*m1)); 1666132718Skan tree d2 = DECL_NAME (OVL_CURRENT (*m2)); 1667132718Skan resort_data.new_value (&d1, resort_data.cookie); 1668132718Skan resort_data.new_value (&d2, resort_data.cookie); 1669132718Skan if (d1 < d2) 1670132718Skan return -1; 1671132718Skan } 167290075Sobrien return 1; 167390075Sobrien} 167490075Sobrien 1675132718Skan/* Resort TYPE_METHOD_VEC because pointers have been reordered. */ 1676132718Skan 1677169689Skanvoid 1678132718Skanresort_type_method_vec (void* obj, 1679169689Skan void* orig_obj ATTRIBUTE_UNUSED , 1680169689Skan gt_pointer_operator new_value, 1681169689Skan void* cookie) 1682132718Skan{ 1683169689Skan VEC(tree,gc) *method_vec = (VEC(tree,gc) *) obj; 1684169689Skan int len = VEC_length (tree, method_vec); 1685169689Skan size_t slot; 1686169689Skan tree fn; 1687132718Skan 1688132718Skan /* The type conversion ops have to live at the front of the vec, so we 1689132718Skan can't sort them. */ 1690169689Skan for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT; 1691169689Skan VEC_iterate (tree, method_vec, slot, fn); 1692169689Skan ++slot) 1693169689Skan if (!DECL_CONV_FN_P (OVL_CURRENT (fn))) 1694169689Skan break; 1695169689Skan 1696132718Skan if (len - slot > 1) 1697132718Skan { 1698132718Skan resort_data.new_value = new_value; 1699132718Skan resort_data.cookie = cookie; 1700169689Skan qsort (VEC_address (tree, method_vec) + slot, len - slot, sizeof (tree), 1701132718Skan resort_method_name_cmp); 1702132718Skan } 1703132718Skan} 1704132718Skan 1705169689Skan/* Warn about duplicate methods in fn_fields. 170618334Speter 170790075Sobrien Sort methods that are not special (i.e., constructors, destructors, 170890075Sobrien and type conversion operators) so that we can find them faster in 170990075Sobrien search. */ 171018334Speter 171152284Sobrienstatic void 1712132718Skanfinish_struct_methods (tree t) 171318334Speter{ 171452284Sobrien tree fn_fields; 1715169689Skan VEC(tree,gc) *method_vec; 171690075Sobrien int slot, len; 171718334Speter 171890075Sobrien method_vec = CLASSTYPE_METHOD_VEC (t); 1719169689Skan if (!method_vec) 1720169689Skan return; 172190075Sobrien 1722169689Skan len = VEC_length (tree, method_vec); 1723169689Skan 1724169689Skan /* Clear DECL_IN_AGGR_P for all functions. */ 1725169689Skan for (fn_fields = TYPE_METHODS (t); fn_fields; 172652284Sobrien fn_fields = TREE_CHAIN (fn_fields)) 172790075Sobrien DECL_IN_AGGR_P (fn_fields) = 0; 172818334Speter 172952284Sobrien /* Issue warnings about private constructors and such. If there are 173052284Sobrien no methods, then some public defaults are generated. */ 173190075Sobrien maybe_warn_about_overly_private_class (t); 173290075Sobrien 173390075Sobrien /* The type conversion ops have to live at the front of the vec, so we 173490075Sobrien can't sort them. */ 1735169689Skan for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT; 1736169689Skan VEC_iterate (tree, method_vec, slot, fn_fields); 1737169689Skan ++slot) 1738169689Skan if (!DECL_CONV_FN_P (OVL_CURRENT (fn_fields))) 1739169689Skan break; 174090075Sobrien if (len - slot > 1) 1741169689Skan qsort (VEC_address (tree, method_vec) + slot, 1742169689Skan len-slot, sizeof (tree), method_name_cmp); 174318334Speter} 174418334Speter 174590075Sobrien/* Make BINFO's vtable have N entries, including RTTI entries, 174690075Sobrien vbase and vcall offsets, etc. Set its type and call the backend 174790075Sobrien to lay it out. */ 174850397Sobrien 174918334Speterstatic void 1750132718Skanlayout_vtable_decl (tree binfo, int n) 175118334Speter{ 175290075Sobrien tree atype; 175390075Sobrien tree vtable; 175418334Speter 1755169689Skan atype = build_cplus_array_type (vtable_entry_type, 175690075Sobrien build_index_type (size_int (n - 1))); 175790075Sobrien layout_type (atype); 175890075Sobrien 175990075Sobrien /* We may have to grow the vtable. */ 176090075Sobrien vtable = get_vtbl_decl_for_binfo (binfo); 176190075Sobrien if (!same_type_p (TREE_TYPE (vtable), atype)) 176218334Speter { 176390075Sobrien TREE_TYPE (vtable) = atype; 176490075Sobrien DECL_SIZE (vtable) = DECL_SIZE_UNIT (vtable) = NULL_TREE; 176590075Sobrien layout_decl (vtable, 0); 176618334Speter } 176718334Speter} 176818334Speter 176990075Sobrien/* True iff FNDECL and BASE_FNDECL (both non-static member functions) 177090075Sobrien have the same signature. */ 177150397Sobrien 177290075Sobrienint 1773132718Skansame_signature_p (tree fndecl, tree base_fndecl) 177418334Speter{ 177590075Sobrien /* One destructor overrides another if they are the same kind of 177690075Sobrien destructor. */ 177790075Sobrien if (DECL_DESTRUCTOR_P (base_fndecl) && DECL_DESTRUCTOR_P (fndecl) 177890075Sobrien && special_function_p (base_fndecl) == special_function_p (fndecl)) 177918334Speter return 1; 178090075Sobrien /* But a non-destructor never overrides a destructor, nor vice 178190075Sobrien versa, nor do different kinds of destructors override 178290075Sobrien one-another. For example, a complete object destructor does not 178390075Sobrien override a deleting destructor. */ 178490075Sobrien if (DECL_DESTRUCTOR_P (base_fndecl) || DECL_DESTRUCTOR_P (fndecl)) 178518334Speter return 0; 178690075Sobrien 1787132718Skan if (DECL_NAME (fndecl) == DECL_NAME (base_fndecl) 1788132718Skan || (DECL_CONV_FN_P (fndecl) 1789132718Skan && DECL_CONV_FN_P (base_fndecl) 1790132718Skan && same_type_p (DECL_CONV_FN_TYPE (fndecl), 1791132718Skan DECL_CONV_FN_TYPE (base_fndecl)))) 179218334Speter { 179350397Sobrien tree types, base_types; 179418334Speter types = TYPE_ARG_TYPES (TREE_TYPE (fndecl)); 179518334Speter base_types = TYPE_ARG_TYPES (TREE_TYPE (base_fndecl)); 179652284Sobrien if ((TYPE_QUALS (TREE_TYPE (TREE_VALUE (base_types))) 179752284Sobrien == TYPE_QUALS (TREE_TYPE (TREE_VALUE (types)))) 179852284Sobrien && compparms (TREE_CHAIN (base_types), TREE_CHAIN (types))) 179918334Speter return 1; 180018334Speter } 180118334Speter return 0; 180218334Speter} 180318334Speter 1804117395Skan/* Returns TRUE if DERIVED is a binfo containing the binfo BASE as a 1805117395Skan subobject. */ 1806169689Skan 1807117395Skanstatic bool 1808117395Skanbase_derived_from (tree derived, tree base) 1809117395Skan{ 1810132718Skan tree probe; 1811132718Skan 1812132718Skan for (probe = base; probe; probe = BINFO_INHERITANCE_CHAIN (probe)) 1813132718Skan { 1814132718Skan if (probe == derived) 1815132718Skan return true; 1816169689Skan else if (BINFO_VIRTUAL_P (probe)) 1817132718Skan /* If we meet a virtual base, we can't follow the inheritance 1818132718Skan any more. See if the complete type of DERIVED contains 1819132718Skan such a virtual base. */ 1820169689Skan return (binfo_for_vbase (BINFO_TYPE (probe), BINFO_TYPE (derived)) 1821169689Skan != NULL_TREE); 1822132718Skan } 1823132718Skan return false; 1824117395Skan} 1825117395Skan 182690075Sobrientypedef struct find_final_overrider_data_s { 182790075Sobrien /* The function for which we are trying to find a final overrider. */ 182890075Sobrien tree fn; 182990075Sobrien /* The base class in which the function was declared. */ 183090075Sobrien tree declaring_base; 1831117395Skan /* The candidate overriders. */ 183290075Sobrien tree candidates; 1833169689Skan /* Path to most derived. */ 1834169689Skan VEC(tree,heap) *path; 183590075Sobrien} find_final_overrider_data; 183690075Sobrien 1837146895Skan/* Add the overrider along the current path to FFOD->CANDIDATES. 1838146895Skan Returns true if an overrider was found; false otherwise. */ 1839146895Skan 1840146895Skanstatic bool 1841169689Skandfs_find_final_overrider_1 (tree binfo, 1842169689Skan find_final_overrider_data *ffod, 1843169689Skan unsigned depth) 1844146895Skan{ 1845146895Skan tree method; 1846169689Skan 1847146895Skan /* If BINFO is not the most derived type, try a more derived class. 1848146895Skan A definition there will overrider a definition here. */ 1849169689Skan if (depth) 1850146895Skan { 1851169689Skan depth--; 1852169689Skan if (dfs_find_final_overrider_1 1853169689Skan (VEC_index (tree, ffod->path, depth), ffod, depth)) 1854146895Skan return true; 1855146895Skan } 1856146895Skan 1857146895Skan method = look_for_overrides_here (BINFO_TYPE (binfo), ffod->fn); 1858146895Skan if (method) 1859146895Skan { 1860146895Skan tree *candidate = &ffod->candidates; 1861169689Skan 1862146895Skan /* Remove any candidates overridden by this new function. */ 1863146895Skan while (*candidate) 1864146895Skan { 1865146895Skan /* If *CANDIDATE overrides METHOD, then METHOD 1866146895Skan cannot override anything else on the list. */ 1867146895Skan if (base_derived_from (TREE_VALUE (*candidate), binfo)) 1868146895Skan return true; 1869146895Skan /* If METHOD overrides *CANDIDATE, remove *CANDIDATE. */ 1870146895Skan if (base_derived_from (binfo, TREE_VALUE (*candidate))) 1871146895Skan *candidate = TREE_CHAIN (*candidate); 1872146895Skan else 1873146895Skan candidate = &TREE_CHAIN (*candidate); 1874146895Skan } 1875169689Skan 1876146895Skan /* Add the new function. */ 1877146895Skan ffod->candidates = tree_cons (method, binfo, ffod->candidates); 1878146895Skan return true; 1879146895Skan } 1880146895Skan 1881146895Skan return false; 1882146895Skan} 1883146895Skan 188490075Sobrien/* Called from find_final_overrider via dfs_walk. */ 188590075Sobrien 188618334Speterstatic tree 1887169689Skandfs_find_final_overrider_pre (tree binfo, void *data) 188818334Speter{ 188990075Sobrien find_final_overrider_data *ffod = (find_final_overrider_data *) data; 189018334Speter 1891132718Skan if (binfo == ffod->declaring_base) 1892169689Skan dfs_find_final_overrider_1 (binfo, ffod, VEC_length (tree, ffod->path)); 1893169689Skan VEC_safe_push (tree, heap, ffod->path, binfo); 189418334Speter 1895132718Skan return NULL_TREE; 1896132718Skan} 189718334Speter 1898132718Skanstatic tree 1899169689Skandfs_find_final_overrider_post (tree binfo ATTRIBUTE_UNUSED, void *data) 1900132718Skan{ 1901132718Skan find_final_overrider_data *ffod = (find_final_overrider_data *) data; 1902169689Skan VEC_pop (tree, ffod->path); 190318334Speter 190490075Sobrien return NULL_TREE; 190518334Speter} 190618334Speter 190790075Sobrien/* Returns a TREE_LIST whose TREE_PURPOSE is the final overrider for 190890075Sobrien FN and whose TREE_VALUE is the binfo for the base where the 1909117395Skan overriding occurs. BINFO (in the hierarchy dominated by the binfo 1910117395Skan DERIVED) is the base object in which FN is declared. */ 191150397Sobrien 191290075Sobrienstatic tree 1913132718Skanfind_final_overrider (tree derived, tree binfo, tree fn) 191418334Speter{ 191590075Sobrien find_final_overrider_data ffod; 191618334Speter 1917117395Skan /* Getting this right is a little tricky. This is valid: 191852284Sobrien 191990075Sobrien struct S { virtual void f (); }; 192090075Sobrien struct T { virtual void f (); }; 192190075Sobrien struct U : public S, public T { }; 192218334Speter 1923169689Skan even though calling `f' in `U' is ambiguous. But, 192418334Speter 192590075Sobrien struct R { virtual void f(); }; 192690075Sobrien struct S : virtual public R { virtual void f (); }; 192790075Sobrien struct T : virtual public R { virtual void f (); }; 192890075Sobrien struct U : public S, public T { }; 192918334Speter 193090075Sobrien is not -- there's no way to decide whether to put `S::f' or 1931169689Skan `T::f' in the vtable for `R'. 1932169689Skan 193390075Sobrien The solution is to look at all paths to BINFO. If we find 193490075Sobrien different overriders along any two, then there is a problem. */ 1935132718Skan if (DECL_THUNK_P (fn)) 1936132718Skan fn = THUNK_TARGET (fn); 1937146895Skan 1938146895Skan /* Determine the depth of the hierarchy. */ 193990075Sobrien ffod.fn = fn; 194090075Sobrien ffod.declaring_base = binfo; 194190075Sobrien ffod.candidates = NULL_TREE; 1942169689Skan ffod.path = VEC_alloc (tree, heap, 30); 194390075Sobrien 1944169689Skan dfs_walk_all (derived, dfs_find_final_overrider_pre, 1945169689Skan dfs_find_final_overrider_post, &ffod); 194690075Sobrien 1947169689Skan VEC_free (tree, heap, ffod.path); 1948146895Skan 194990075Sobrien /* If there was no winner, issue an error message. */ 1950117395Skan if (!ffod.candidates || TREE_CHAIN (ffod.candidates)) 1951161651Skan return error_mark_node; 195218334Speter 1953117395Skan return ffod.candidates; 195490075Sobrien} 195518334Speter 1956117395Skan/* Return the index of the vcall offset for FN when TYPE is used as a 1957117395Skan virtual base. */ 195818334Speter 195990075Sobrienstatic tree 1960117395Skanget_vcall_index (tree fn, tree type) 196190075Sobrien{ 1962169689Skan VEC(tree_pair_s,gc) *indices = CLASSTYPE_VCALL_INDICES (type); 1963169689Skan tree_pair_p p; 1964169689Skan unsigned ix; 196518334Speter 1966169689Skan for (ix = 0; VEC_iterate (tree_pair_s, indices, ix, p); ix++) 1967169689Skan if ((DECL_DESTRUCTOR_P (fn) && DECL_DESTRUCTOR_P (p->purpose)) 1968169689Skan || same_signature_p (fn, p->purpose)) 1969169689Skan return p->value; 1970117395Skan 1971117395Skan /* There should always be an appropriate index. */ 1972169689Skan gcc_unreachable (); 197318334Speter} 197418334Speter 197590075Sobrien/* Update an entry in the vtable for BINFO, which is in the hierarchy 1976132718Skan dominated by T. FN has been overridden in BINFO; VIRTUALS points to the 197790075Sobrien corresponding position in the BINFO_VIRTUALS list. */ 197850397Sobrien 197918334Speterstatic void 1980132718Skanupdate_vtable_entry_for_fn (tree t, tree binfo, tree fn, tree* virtuals, 1981132718Skan unsigned ix) 198218334Speter{ 198390075Sobrien tree b; 198490075Sobrien tree overrider; 198590075Sobrien tree delta; 198690075Sobrien tree virtual_base; 198790075Sobrien tree first_defn; 1988132718Skan tree overrider_fn, overrider_target; 1989132718Skan tree target_fn = DECL_THUNK_P (fn) ? THUNK_TARGET (fn) : fn; 1990132718Skan tree over_return, base_return; 199190075Sobrien bool lost = false; 199218334Speter 199390075Sobrien /* Find the nearest primary base (possibly binfo itself) which defines 199490075Sobrien this function; this is the class the caller will convert to when 199590075Sobrien calling FN through BINFO. */ 199690075Sobrien for (b = binfo; ; b = get_primary_binfo (b)) 199718334Speter { 1998169689Skan gcc_assert (b); 1999132718Skan if (look_for_overrides_here (BINFO_TYPE (b), target_fn)) 200090075Sobrien break; 200118334Speter 200290075Sobrien /* The nearest definition is from a lost primary. */ 200390075Sobrien if (BINFO_LOST_PRIMARY_P (b)) 200490075Sobrien lost = true; 200518334Speter } 200690075Sobrien first_defn = b; 200718334Speter 200890075Sobrien /* Find the final overrider. */ 2009132718Skan overrider = find_final_overrider (TYPE_BINFO (t), b, target_fn); 201090075Sobrien if (overrider == error_mark_node) 2011161651Skan { 2012169689Skan error ("no unique final overrider for %qD in %qT", target_fn, t); 2013161651Skan return; 2014161651Skan } 2015132718Skan overrider_target = overrider_fn = TREE_PURPOSE (overrider); 2016169689Skan 2017132718Skan /* Check for adjusting covariant return types. */ 2018132718Skan over_return = TREE_TYPE (TREE_TYPE (overrider_target)); 2019132718Skan base_return = TREE_TYPE (TREE_TYPE (target_fn)); 2020169689Skan 2021132718Skan if (POINTER_TYPE_P (over_return) 2022132718Skan && TREE_CODE (over_return) == TREE_CODE (base_return) 2023132718Skan && CLASS_TYPE_P (TREE_TYPE (over_return)) 2024169689Skan && CLASS_TYPE_P (TREE_TYPE (base_return)) 2025169689Skan /* If the overrider is invalid, don't even try. */ 2026169689Skan && !DECL_INVALID_OVERRIDER_P (overrider_target)) 2027132718Skan { 2028132718Skan /* If FN is a covariant thunk, we must figure out the adjustment 2029169689Skan to the final base FN was converting to. As OVERRIDER_TARGET might 2030169689Skan also be converting to the return type of FN, we have to 2031169689Skan combine the two conversions here. */ 2032132718Skan tree fixed_offset, virtual_offset; 2033146895Skan 2034146895Skan over_return = TREE_TYPE (over_return); 2035146895Skan base_return = TREE_TYPE (base_return); 2036169689Skan 2037132718Skan if (DECL_THUNK_P (fn)) 2038132718Skan { 2039169689Skan gcc_assert (DECL_RESULT_THUNK_P (fn)); 2040132718Skan fixed_offset = ssize_int (THUNK_FIXED_OFFSET (fn)); 2041132718Skan virtual_offset = THUNK_VIRTUAL_OFFSET (fn); 2042132718Skan } 2043132718Skan else 2044132718Skan fixed_offset = virtual_offset = NULL_TREE; 204550397Sobrien 2046132718Skan if (virtual_offset) 2047132718Skan /* Find the equivalent binfo within the return type of the 2048132718Skan overriding function. We will want the vbase offset from 2049132718Skan there. */ 2050169689Skan virtual_offset = binfo_for_vbase (BINFO_TYPE (virtual_offset), 2051169689Skan over_return); 2052146895Skan else if (!same_type_ignoring_top_level_qualifiers_p 2053146895Skan (over_return, base_return)) 2054132718Skan { 2055169689Skan /* There was no existing virtual thunk (which takes 2056146895Skan precedence). So find the binfo of the base function's 2057146895Skan return type within the overriding function's return type. 2058146895Skan We cannot call lookup base here, because we're inside a 2059146895Skan dfs_walk, and will therefore clobber the BINFO_MARKED 2060146895Skan flags. Fortunately we know the covariancy is valid (it 2061146895Skan has already been checked), so we can just iterate along 2062146895Skan the binfos, which have been chained in inheritance graph 2063146895Skan order. Of course it is lame that we have to repeat the 2064146895Skan search here anyway -- we should really be caching pieces 2065146895Skan of the vtable and avoiding this repeated work. */ 2066146895Skan tree thunk_binfo, base_binfo; 2067146895Skan 2068146895Skan /* Find the base binfo within the overriding function's 2069146895Skan return type. We will always find a thunk_binfo, except 2070146895Skan when the covariancy is invalid (which we will have 2071146895Skan already diagnosed). */ 2072146895Skan for (base_binfo = TYPE_BINFO (base_return), 2073146895Skan thunk_binfo = TYPE_BINFO (over_return); 2074146895Skan thunk_binfo; 2075146895Skan thunk_binfo = TREE_CHAIN (thunk_binfo)) 2076169689Skan if (SAME_BINFO_TYPE_P (BINFO_TYPE (thunk_binfo), 2077169689Skan BINFO_TYPE (base_binfo))) 2078146895Skan break; 2079169689Skan 2080146895Skan /* See if virtual inheritance is involved. */ 2081146895Skan for (virtual_offset = thunk_binfo; 2082146895Skan virtual_offset; 2083146895Skan virtual_offset = BINFO_INHERITANCE_CHAIN (virtual_offset)) 2084169689Skan if (BINFO_VIRTUAL_P (virtual_offset)) 2085146895Skan break; 2086169689Skan 2087146895Skan if (virtual_offset 2088146895Skan || (thunk_binfo && !BINFO_OFFSET_ZEROP (thunk_binfo))) 2089132718Skan { 2090132718Skan tree offset = convert (ssizetype, BINFO_OFFSET (thunk_binfo)); 2091132718Skan 2092146895Skan if (virtual_offset) 2093132718Skan { 2094146895Skan /* We convert via virtual base. Adjust the fixed 2095146895Skan offset to be from there. */ 2096132718Skan offset = size_diffop 2097132718Skan (offset, convert 2098132718Skan (ssizetype, BINFO_OFFSET (virtual_offset))); 2099132718Skan } 2100132718Skan if (fixed_offset) 2101132718Skan /* There was an existing fixed offset, this must be 2102132718Skan from the base just converted to, and the base the 2103132718Skan FN was thunking to. */ 2104132718Skan fixed_offset = size_binop (PLUS_EXPR, fixed_offset, offset); 2105132718Skan else 2106132718Skan fixed_offset = offset; 2107132718Skan } 2108132718Skan } 2109169689Skan 2110132718Skan if (fixed_offset || virtual_offset) 2111132718Skan /* Replace the overriding function with a covariant thunk. We 2112132718Skan will emit the overriding function in its own slot as 2113132718Skan well. */ 2114132718Skan overrider_fn = make_thunk (overrider_target, /*this_adjusting=*/0, 2115132718Skan fixed_offset, virtual_offset); 2116132718Skan } 2117132718Skan else 2118169689Skan gcc_assert (!DECL_THUNK_P (fn)); 2119169689Skan 212090075Sobrien /* Assume that we will produce a thunk that convert all the way to 212190075Sobrien the final overrider, and not to an intermediate virtual base. */ 212290075Sobrien virtual_base = NULL_TREE; 212318334Speter 212490075Sobrien /* See if we can convert to an intermediate virtual base first, and then 212590075Sobrien use the vcall offset located there to finish the conversion. */ 212690075Sobrien for (; b; b = BINFO_INHERITANCE_CHAIN (b)) 212718334Speter { 212890075Sobrien /* If we find the final overrider, then we can stop 212990075Sobrien walking. */ 2130169689Skan if (SAME_BINFO_TYPE_P (BINFO_TYPE (b), 2131169689Skan BINFO_TYPE (TREE_VALUE (overrider)))) 213290075Sobrien break; 213318334Speter 213490075Sobrien /* If we find a virtual base, and we haven't yet found the 213590075Sobrien overrider, then there is a virtual base between the 213690075Sobrien declaring base (first_defn) and the final overrider. */ 2137169689Skan if (BINFO_VIRTUAL_P (b)) 2138132718Skan { 2139132718Skan virtual_base = b; 2140132718Skan break; 2141132718Skan } 214290075Sobrien } 214318334Speter 2144132718Skan if (overrider_fn != overrider_target && !virtual_base) 2145132718Skan { 2146132718Skan /* The ABI specifies that a covariant thunk includes a mangling 2147169689Skan for a this pointer adjustment. This-adjusting thunks that 2148169689Skan override a function from a virtual base have a vcall 2149169689Skan adjustment. When the virtual base in question is a primary 2150169689Skan virtual base, we know the adjustments are zero, (and in the 2151169689Skan non-covariant case, we would not use the thunk). 2152169689Skan Unfortunately we didn't notice this could happen, when 2153169689Skan designing the ABI and so never mandated that such a covariant 2154169689Skan thunk should be emitted. Because we must use the ABI mandated 2155169689Skan name, we must continue searching from the binfo where we 2156169689Skan found the most recent definition of the function, towards the 2157169689Skan primary binfo which first introduced the function into the 2158169689Skan vtable. If that enters a virtual base, we must use a vcall 2159169689Skan this-adjusting thunk. Bleah! */ 2160132718Skan tree probe = first_defn; 2161132718Skan 2162132718Skan while ((probe = get_primary_binfo (probe)) 2163132718Skan && (unsigned) list_length (BINFO_VIRTUALS (probe)) > ix) 2164169689Skan if (BINFO_VIRTUAL_P (probe)) 2165132718Skan virtual_base = probe; 2166169689Skan 2167132718Skan if (virtual_base) 2168132718Skan /* Even if we find a virtual base, the correct delta is 2169132718Skan between the overrider and the binfo we're building a vtable 2170132718Skan for. */ 2171132718Skan goto virtual_covariant; 2172132718Skan } 2173169689Skan 217490075Sobrien /* Compute the constant adjustment to the `this' pointer. The 217590075Sobrien `this' pointer, when this function is called, will point at BINFO 217690075Sobrien (or one of its primary bases, which are at the same offset). */ 217790075Sobrien if (virtual_base) 217890075Sobrien /* The `this' pointer needs to be adjusted from the declaration to 217990075Sobrien the nearest virtual base. */ 2180132718Skan delta = size_diffop (convert (ssizetype, BINFO_OFFSET (virtual_base)), 2181132718Skan convert (ssizetype, BINFO_OFFSET (first_defn))); 218290075Sobrien else if (lost) 218390075Sobrien /* If the nearest definition is in a lost primary, we don't need an 218490075Sobrien entry in our vtable. Except possibly in a constructor vtable, 218590075Sobrien if we happen to get our primary back. In that case, the offset 218690075Sobrien will be zero, as it will be a primary base. */ 218790075Sobrien delta = size_zero_node; 218890075Sobrien else 2189117395Skan /* The `this' pointer needs to be adjusted from pointing to 2190117395Skan BINFO to pointing at the base where the final overrider 2191117395Skan appears. */ 2192132718Skan virtual_covariant: 2193132718Skan delta = size_diffop (convert (ssizetype, 2194132718Skan BINFO_OFFSET (TREE_VALUE (overrider))), 2195132718Skan convert (ssizetype, BINFO_OFFSET (binfo))); 219618334Speter 2197132718Skan modify_vtable_entry (t, binfo, overrider_fn, delta, virtuals); 219850397Sobrien 219990075Sobrien if (virtual_base) 2200169689Skan BV_VCALL_INDEX (*virtuals) 2201132718Skan = get_vcall_index (overrider_target, BINFO_TYPE (virtual_base)); 2202169689Skan else 2203169689Skan BV_VCALL_INDEX (*virtuals) = NULL_TREE; 220418334Speter} 220518334Speter 220690075Sobrien/* Called from modify_all_vtables via dfs_walk. */ 220750397Sobrien 220890075Sobrienstatic tree 2209132718Skandfs_modify_vtables (tree binfo, void* data) 221018334Speter{ 2211169689Skan tree t = (tree) data; 2212169689Skan tree virtuals; 2213169689Skan tree old_virtuals; 2214169689Skan unsigned ix; 221518334Speter 2216169689Skan if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo))) 2217169689Skan /* A base without a vtable needs no modification, and its bases 2218169689Skan are uninteresting. */ 2219169689Skan return dfs_skip_bases; 222018334Speter 2221169689Skan if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t) 2222169689Skan && !CLASSTYPE_HAS_PRIMARY_BASE_P (t)) 2223169689Skan /* Don't do the primary vtable, if it's new. */ 2224169689Skan return NULL_TREE; 2225169689Skan 2226169689Skan if (BINFO_PRIMARY_P (binfo) && !BINFO_VIRTUAL_P (binfo)) 2227169689Skan /* There's no need to modify the vtable for a non-virtual primary 2228169689Skan base; we're not going to use that vtable anyhow. We do still 2229169689Skan need to do this for virtual primary bases, as they could become 2230169689Skan non-primary in a construction vtable. */ 2231169689Skan return NULL_TREE; 2232169689Skan 2233169689Skan make_new_vtable (t, binfo); 2234169689Skan 2235169689Skan /* Now, go through each of the virtual functions in the virtual 2236169689Skan function table for BINFO. Find the final overrider, and update 2237169689Skan the BINFO_VIRTUALS list appropriately. */ 2238169689Skan for (ix = 0, virtuals = BINFO_VIRTUALS (binfo), 2239169689Skan old_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo))); 2240169689Skan virtuals; 2241169689Skan ix++, virtuals = TREE_CHAIN (virtuals), 2242169689Skan old_virtuals = TREE_CHAIN (old_virtuals)) 2243169689Skan update_vtable_entry_for_fn (t, 2244169689Skan binfo, 2245169689Skan BV_FN (old_virtuals), 2246169689Skan &virtuals, ix); 2247169689Skan 224890075Sobrien return NULL_TREE; 224918334Speter} 225018334Speter 225190075Sobrien/* Update all of the primary and secondary vtables for T. Create new 225290075Sobrien vtables as required, and initialize their RTTI information. Each 2253102780Skan of the functions in VIRTUALS is declared in T and may override a 2254102780Skan virtual function from a base class; find and modify the appropriate 2255102780Skan entries to point to the overriding functions. Returns a list, in 2256102780Skan declaration order, of the virtual functions that are declared in T, 2257102780Skan but do not appear in the primary base class vtable, and which 2258102780Skan should therefore be appended to the end of the vtable for T. */ 225918334Speter 226090075Sobrienstatic tree 2261132718Skanmodify_all_vtables (tree t, tree virtuals) 226218334Speter{ 226390075Sobrien tree binfo = TYPE_BINFO (t); 226490075Sobrien tree *fnsp; 226518334Speter 226690075Sobrien /* Update all of the vtables. */ 2267169689Skan dfs_walk_once (binfo, dfs_modify_vtables, NULL, t); 226818334Speter 2269102780Skan /* Add virtual functions not already in our primary vtable. These 2270102780Skan will be both those introduced by this class, and those overridden 2271102780Skan from secondary bases. It does not include virtuals merely 2272102780Skan inherited from secondary bases. */ 2273102780Skan for (fnsp = &virtuals; *fnsp; ) 227490075Sobrien { 227590075Sobrien tree fn = TREE_VALUE (*fnsp); 227618334Speter 2277102780Skan if (!value_member (fn, BINFO_VIRTUALS (binfo)) 2278102780Skan || DECL_VINDEX (fn) == error_mark_node) 227918334Speter { 228090075Sobrien /* We don't need to adjust the `this' pointer when 228190075Sobrien calling this function. */ 228290075Sobrien BV_DELTA (*fnsp) = integer_zero_node; 228390075Sobrien BV_VCALL_INDEX (*fnsp) = NULL_TREE; 228418334Speter 2285102780Skan /* This is a function not already in our vtable. Keep it. */ 228690075Sobrien fnsp = &TREE_CHAIN (*fnsp); 228718334Speter } 228890075Sobrien else 228990075Sobrien /* We've already got an entry for this function. Skip it. */ 229090075Sobrien *fnsp = TREE_CHAIN (*fnsp); 229118334Speter } 2292117395Skan 2293102780Skan return virtuals; 229418334Speter} 229518334Speter 229690075Sobrien/* Get the base virtual function declarations in T that have the 229790075Sobrien indicated NAME. */ 229850397Sobrien 229950397Sobrienstatic tree 2300132718Skanget_basefndecls (tree name, tree t) 230150397Sobrien{ 230290075Sobrien tree methods; 230350397Sobrien tree base_fndecls = NULL_TREE; 2304169689Skan int n_baseclasses = BINFO_N_BASE_BINFOS (TYPE_BINFO (t)); 230590075Sobrien int i; 230650397Sobrien 2307117395Skan /* Find virtual functions in T with the indicated NAME. */ 2308117395Skan i = lookup_fnfields_1 (t, name); 2309117395Skan if (i != -1) 2310169689Skan for (methods = VEC_index (tree, CLASSTYPE_METHOD_VEC (t), i); 2311117395Skan methods; 2312117395Skan methods = OVL_NEXT (methods)) 2313117395Skan { 2314117395Skan tree method = OVL_CURRENT (methods); 231550397Sobrien 2316117395Skan if (TREE_CODE (method) == FUNCTION_DECL 2317117395Skan && DECL_VINDEX (method)) 2318117395Skan base_fndecls = tree_cons (NULL_TREE, method, base_fndecls); 2319117395Skan } 2320117395Skan 232150397Sobrien if (base_fndecls) 232250397Sobrien return base_fndecls; 232350397Sobrien 232450397Sobrien for (i = 0; i < n_baseclasses; i++) 232550397Sobrien { 2326169689Skan tree basetype = BINFO_TYPE (BINFO_BASE_BINFO (TYPE_BINFO (t), i)); 232790075Sobrien base_fndecls = chainon (get_basefndecls (name, basetype), 232850397Sobrien base_fndecls); 232950397Sobrien } 233050397Sobrien 233150397Sobrien return base_fndecls; 233250397Sobrien} 233350397Sobrien 233450397Sobrien/* If this declaration supersedes the declaration of 233550397Sobrien a method declared virtual in the base class, then 233650397Sobrien mark this field as being virtual as well. */ 233750397Sobrien 2338169689Skanvoid 2339132718Skancheck_for_override (tree decl, tree ctype) 234050397Sobrien{ 234190075Sobrien if (TREE_CODE (decl) == TEMPLATE_DECL) 234290075Sobrien /* In [temp.mem] we have: 234350397Sobrien 2344169689Skan A specialization of a member function template does not 2345169689Skan override a virtual function from a base class. */ 234690075Sobrien return; 234790075Sobrien if ((DECL_DESTRUCTOR_P (decl) 2348132718Skan || IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) 2349132718Skan || DECL_CONV_FN_P (decl)) 235090075Sobrien && look_for_overrides (ctype, decl) 235190075Sobrien && !DECL_STATIC_FUNCTION_P (decl)) 2352102780Skan /* Set DECL_VINDEX to a value that is neither an INTEGER_CST nor 2353102780Skan the error_mark_node so that we know it is an overriding 2354102780Skan function. */ 2355102780Skan DECL_VINDEX (decl) = decl; 2356102780Skan 235790075Sobrien if (DECL_VIRTUAL_P (decl)) 235850397Sobrien { 2359102780Skan if (!DECL_VINDEX (decl)) 236050397Sobrien DECL_VINDEX (decl) = error_mark_node; 236150397Sobrien IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = 1; 2362169689Skan if (DECL_DLLIMPORT_P (decl)) 2363169689Skan { 2364169689Skan /* When we handled the dllimport attribute we may not have known 2365169689Skan that this function is virtual We can't use dllimport 2366169689Skan semantics for a virtual method because we need to initialize 2367169689Skan the vtable entry with a constant address. */ 2368169689Skan DECL_DLLIMPORT_P (decl) = 0; 2369169689Skan DECL_ATTRIBUTES (decl) 2370169689Skan = remove_attribute ("dllimport", DECL_ATTRIBUTES (decl)); 2371169689Skan } 237250397Sobrien } 237350397Sobrien} 237450397Sobrien 237550397Sobrien/* Warn about hidden virtual functions that are not overridden in t. 237650397Sobrien We know that constructors and destructors don't apply. */ 237750397Sobrien 2378169689Skanstatic void 2379132718Skanwarn_hidden (tree t) 238050397Sobrien{ 2381169689Skan VEC(tree,gc) *method_vec = CLASSTYPE_METHOD_VEC (t); 2382169689Skan tree fns; 2383169689Skan size_t i; 238450397Sobrien 238550397Sobrien /* We go through each separately named virtual function. */ 2386169689Skan for (i = CLASSTYPE_FIRST_CONVERSION_SLOT; 2387169689Skan VEC_iterate (tree, method_vec, i, fns); 2388169689Skan ++i) 238950397Sobrien { 2390169689Skan tree fn; 239190075Sobrien tree name; 239250397Sobrien tree fndecl; 239390075Sobrien tree base_fndecls; 2394169689Skan tree base_binfo; 2395169689Skan tree binfo; 239690075Sobrien int j; 239750397Sobrien 239890075Sobrien /* All functions in this slot in the CLASSTYPE_METHOD_VEC will 239990075Sobrien have the same name. Figure out what name that is. */ 2400169689Skan name = DECL_NAME (OVL_CURRENT (fns)); 240190075Sobrien /* There are no possibly hidden functions yet. */ 240290075Sobrien base_fndecls = NULL_TREE; 240390075Sobrien /* Iterate through all of the base classes looking for possibly 240490075Sobrien hidden functions. */ 2405169689Skan for (binfo = TYPE_BINFO (t), j = 0; 2406169689Skan BINFO_BASE_ITERATE (binfo, j, base_binfo); j++) 240752284Sobrien { 2408169689Skan tree basetype = BINFO_TYPE (base_binfo); 240990075Sobrien base_fndecls = chainon (get_basefndecls (name, basetype), 241090075Sobrien base_fndecls); 241152284Sobrien } 241252284Sobrien 2413117395Skan /* If there are no functions to hide, continue. */ 241490075Sobrien if (!base_fndecls) 241550397Sobrien continue; 241650397Sobrien 2417117395Skan /* Remove any overridden functions. */ 2418169689Skan for (fn = fns; fn; fn = OVL_NEXT (fn)) 241950397Sobrien { 2420169689Skan fndecl = OVL_CURRENT (fn); 242190075Sobrien if (DECL_VINDEX (fndecl)) 242290075Sobrien { 242390075Sobrien tree *prev = &base_fndecls; 2424169689Skan 2425169689Skan while (*prev) 242690075Sobrien /* If the method from the base class has the same 242790075Sobrien signature as the method from the derived class, it 242890075Sobrien has been overridden. */ 242990075Sobrien if (same_signature_p (fndecl, TREE_VALUE (*prev))) 243090075Sobrien *prev = TREE_CHAIN (*prev); 243190075Sobrien else 243290075Sobrien prev = &TREE_CHAIN (*prev); 243390075Sobrien } 243450397Sobrien } 243550397Sobrien 243650397Sobrien /* Now give a warning for all base functions without overriders, 243750397Sobrien as they are hidden. */ 2438169689Skan while (base_fndecls) 243950397Sobrien { 244090075Sobrien /* Here we know it is a hider, and no overrider exists. */ 2441169689Skan warning (0, "%q+D was hidden", TREE_VALUE (base_fndecls)); 2442169689Skan warning (0, " by %q+D", fns); 244390075Sobrien base_fndecls = TREE_CHAIN (base_fndecls); 244450397Sobrien } 244550397Sobrien } 244650397Sobrien} 244750397Sobrien 244850397Sobrien/* Check for things that are invalid. There are probably plenty of other 244950397Sobrien things we should check for also. */ 245050397Sobrien 245150397Sobrienstatic void 2452132718Skanfinish_struct_anon (tree t) 245350397Sobrien{ 245450397Sobrien tree field; 245590075Sobrien 245650397Sobrien for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field)) 245750397Sobrien { 245850397Sobrien if (TREE_STATIC (field)) 245950397Sobrien continue; 246050397Sobrien if (TREE_CODE (field) != FIELD_DECL) 246150397Sobrien continue; 246250397Sobrien 246350397Sobrien if (DECL_NAME (field) == NULL_TREE 246490075Sobrien && ANON_AGGR_TYPE_P (TREE_TYPE (field))) 246550397Sobrien { 246690075Sobrien tree elt = TYPE_FIELDS (TREE_TYPE (field)); 246790075Sobrien for (; elt; elt = TREE_CHAIN (elt)) 246850397Sobrien { 246990075Sobrien /* We're generally only interested in entities the user 247090075Sobrien declared, but we also find nested classes by noticing 247190075Sobrien the TYPE_DECL that we create implicitly. You're 247290075Sobrien allowed to put one anonymous union inside another, 247390075Sobrien though, so we explicitly tolerate that. We use 247490075Sobrien TYPE_ANONYMOUS_P rather than ANON_AGGR_TYPE_P so that 247590075Sobrien we also allow unnamed types used for defining fields. */ 2476169689Skan if (DECL_ARTIFICIAL (elt) 247790075Sobrien && (!DECL_IMPLICIT_TYPEDEF_P (elt) 247890075Sobrien || TYPE_ANONYMOUS_P (TREE_TYPE (elt)))) 247950397Sobrien continue; 248050397Sobrien 248190075Sobrien if (TREE_CODE (elt) != FIELD_DECL) 248252284Sobrien { 2483169689Skan pedwarn ("%q+#D invalid; an anonymous union can " 2484169689Skan "only have non-static data members", elt); 248552284Sobrien continue; 248652284Sobrien } 248752284Sobrien 248890075Sobrien if (TREE_PRIVATE (elt)) 2489169689Skan pedwarn ("private member %q+#D in anonymous union", elt); 249090075Sobrien else if (TREE_PROTECTED (elt)) 2491169689Skan pedwarn ("protected member %q+#D in anonymous union", elt); 249250397Sobrien 249390075Sobrien TREE_PRIVATE (elt) = TREE_PRIVATE (field); 249490075Sobrien TREE_PROTECTED (elt) = TREE_PROTECTED (field); 249550397Sobrien } 249650397Sobrien } 249750397Sobrien } 249850397Sobrien} 249950397Sobrien 2500117395Skan/* Add T to CLASSTYPE_DECL_LIST of current_class_type which 2501117395Skan will be used later during class template instantiation. 2502117395Skan When FRIEND_P is zero, T can be a static member data (VAR_DECL), 2503117395Skan a non-static member data (FIELD_DECL), a member function 2504169689Skan (FUNCTION_DECL), a nested type (RECORD_TYPE, ENUM_TYPE), 2505117395Skan a typedef (TYPE_DECL) or a member class template (TEMPLATE_DECL) 2506117395Skan When FRIEND_P is nonzero, T is either a friend class 2507117395Skan (RECORD_TYPE, TEMPLATE_DECL) or a friend function 2508117395Skan (FUNCTION_DECL, TEMPLATE_DECL). */ 2509117395Skan 2510117395Skanvoid 2511132718Skanmaybe_add_class_template_decl_list (tree type, tree t, int friend_p) 2512117395Skan{ 2513117395Skan /* Save some memory by not creating TREE_LIST if TYPE is not template. */ 2514117395Skan if (CLASSTYPE_TEMPLATE_INFO (type)) 2515117395Skan CLASSTYPE_DECL_LIST (type) 2516117395Skan = tree_cons (friend_p ? NULL_TREE : type, 2517117395Skan t, CLASSTYPE_DECL_LIST (type)); 2518117395Skan} 2519117395Skan 252052284Sobrien/* Create default constructors, assignment operators, and so forth for 2521169689Skan the type indicated by T, if they are needed. CANT_HAVE_CONST_CTOR, 2522169689Skan and CANT_HAVE_CONST_ASSIGNMENT are nonzero if, for whatever reason, 2523169689Skan the class cannot have a default constructor, copy constructor 2524169689Skan taking a const reference argument, or an assignment operator taking 2525169689Skan a const reference, respectively. */ 252652284Sobrien 2527117395Skanstatic void 2528169689Skanadd_implicitly_declared_members (tree t, 2529132718Skan int cant_have_const_cctor, 2530132718Skan int cant_have_const_assignment) 253152284Sobrien{ 253252284Sobrien /* Destructor. */ 2533169689Skan if (!CLASSTYPE_DESTRUCTORS (t)) 253452284Sobrien { 2535169689Skan /* In general, we create destructors lazily. */ 2536169689Skan CLASSTYPE_LAZY_DESTRUCTOR (t) = 1; 2537169689Skan /* However, if the implicit destructor is non-trivial 2538169689Skan destructor, we sometimes have to create it at this point. */ 2539169689Skan if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)) 254052284Sobrien { 2541169689Skan bool lazy_p = true; 254252284Sobrien 2543169689Skan if (TYPE_FOR_JAVA (t)) 2544169689Skan /* If this a Java class, any non-trivial destructor is 2545169689Skan invalid, even if compiler-generated. Therefore, if the 2546169689Skan destructor is non-trivial we create it now. */ 2547169689Skan lazy_p = false; 2548169689Skan else 2549169689Skan { 2550169689Skan tree binfo; 2551169689Skan tree base_binfo; 2552169689Skan int ix; 2553169689Skan 2554169689Skan /* If the implicit destructor will be virtual, then we must 2555169689Skan generate it now because (unfortunately) we do not 2556169689Skan generate virtual tables lazily. */ 2557169689Skan binfo = TYPE_BINFO (t); 2558169689Skan for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++) 2559169689Skan { 2560169689Skan tree base_type; 2561169689Skan tree dtor; 2562169689Skan 2563169689Skan base_type = BINFO_TYPE (base_binfo); 2564169689Skan dtor = CLASSTYPE_DESTRUCTORS (base_type); 2565169689Skan if (dtor && DECL_VIRTUAL_P (dtor)) 2566169689Skan { 2567169689Skan lazy_p = false; 2568169689Skan break; 2569169689Skan } 2570169689Skan } 2571169689Skan } 2572169689Skan 2573169689Skan /* If we can't get away with being lazy, generate the destructor 2574169689Skan now. */ 2575169689Skan if (!lazy_p) 2576169689Skan lazily_declare_fn (sfk_destructor, t); 257752284Sobrien } 257852284Sobrien } 257952284Sobrien 258052284Sobrien /* Default constructor. */ 2581169689Skan if (! TYPE_HAS_CONSTRUCTOR (t)) 258252284Sobrien { 2583169689Skan TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 1; 2584169689Skan CLASSTYPE_LAZY_DEFAULT_CTOR (t) = 1; 258552284Sobrien } 258652284Sobrien 258752284Sobrien /* Copy constructor. */ 258890075Sobrien if (! TYPE_HAS_INIT_REF (t) && ! TYPE_FOR_JAVA (t)) 258952284Sobrien { 2590169689Skan TYPE_HAS_INIT_REF (t) = 1; 2591169689Skan TYPE_HAS_CONST_INIT_REF (t) = !cant_have_const_cctor; 2592169689Skan CLASSTYPE_LAZY_COPY_CTOR (t) = 1; 2593169689Skan TYPE_HAS_CONSTRUCTOR (t) = 1; 259452284Sobrien } 259552284Sobrien 2596169689Skan /* If there is no assignment operator, one will be created if and 2597169689Skan when it is needed. For now, just record whether or not the type 2598169689Skan of the parameter to the assignment operator will be a const or 2599169689Skan non-const reference. */ 2600169689Skan if (!TYPE_HAS_ASSIGN_REF (t) && !TYPE_FOR_JAVA (t)) 260152284Sobrien { 2602169689Skan TYPE_HAS_ASSIGN_REF (t) = 1; 2603169689Skan TYPE_HAS_CONST_ASSIGN_REF (t) = !cant_have_const_assignment; 2604169689Skan CLASSTYPE_LAZY_ASSIGNMENT_OP (t) = 1; 260552284Sobrien } 260652284Sobrien} 260752284Sobrien 260890075Sobrien/* Subroutine of finish_struct_1. Recursively count the number of fields 260990075Sobrien in TYPE, including anonymous union members. */ 261018334Speter 261190075Sobrienstatic int 2612132718Skancount_fields (tree fields) 261390075Sobrien{ 261490075Sobrien tree x; 261590075Sobrien int n_fields = 0; 261690075Sobrien for (x = fields; x; x = TREE_CHAIN (x)) 261790075Sobrien { 261890075Sobrien if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x))) 261990075Sobrien n_fields += count_fields (TYPE_FIELDS (TREE_TYPE (x))); 262090075Sobrien else 262190075Sobrien n_fields += 1; 262290075Sobrien } 262390075Sobrien return n_fields; 262490075Sobrien} 262518334Speter 262690075Sobrien/* Subroutine of finish_struct_1. Recursively add all the fields in the 2627132718Skan TREE_LIST FIELDS to the SORTED_FIELDS_TYPE elts, starting at offset IDX. */ 262818334Speter 262990075Sobrienstatic int 2630132718Skanadd_fields_to_record_type (tree fields, struct sorted_fields_type *field_vec, int idx) 263118334Speter{ 263290075Sobrien tree x; 263390075Sobrien for (x = fields; x; x = TREE_CHAIN (x)) 263418334Speter { 263590075Sobrien if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x))) 2636132718Skan idx = add_fields_to_record_type (TYPE_FIELDS (TREE_TYPE (x)), field_vec, idx); 263718334Speter else 2638132718Skan field_vec->elts[idx++] = x; 263918334Speter } 264090075Sobrien return idx; 264190075Sobrien} 264218334Speter 264390075Sobrien/* FIELD is a bit-field. We are finishing the processing for its 264490075Sobrien enclosing type. Issue any appropriate messages and set appropriate 264590075Sobrien flags. */ 264618334Speter 264790075Sobrienstatic void 2648132718Skancheck_bitfield_decl (tree field) 264990075Sobrien{ 265090075Sobrien tree type = TREE_TYPE (field); 2651169689Skan tree w; 265218334Speter 2653169689Skan /* Extract the declared width of the bitfield, which has been 2654169689Skan temporarily stashed in DECL_INITIAL. */ 2655169689Skan w = DECL_INITIAL (field); 2656169689Skan gcc_assert (w != NULL_TREE); 2657169689Skan /* Remove the bit-field width indicator so that the rest of the 2658169689Skan compiler does not treat that value as an initializer. */ 2659169689Skan DECL_INITIAL (field) = NULL_TREE; 2660169689Skan 266190075Sobrien /* Detect invalid bit-field type. */ 2662169689Skan if (!INTEGRAL_TYPE_P (type)) 266318334Speter { 2664169689Skan error ("bit-field %q+#D with non-integral type", field); 2665169689Skan TREE_TYPE (field) = error_mark_node; 266690075Sobrien w = error_mark_node; 266718334Speter } 2668169689Skan else 266990075Sobrien { 267090075Sobrien /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs. */ 267190075Sobrien STRIP_NOPS (w); 267218334Speter 267390075Sobrien /* detect invalid field size. */ 2674169689Skan w = integral_constant_value (w); 267518334Speter 267690075Sobrien if (TREE_CODE (w) != INTEGER_CST) 267790075Sobrien { 2678169689Skan error ("bit-field %q+D width not an integer constant", field); 267990075Sobrien w = error_mark_node; 268090075Sobrien } 268190075Sobrien else if (tree_int_cst_sgn (w) < 0) 268290075Sobrien { 2683169689Skan error ("negative width in bit-field %q+D", field); 268490075Sobrien w = error_mark_node; 268590075Sobrien } 268690075Sobrien else if (integer_zerop (w) && DECL_NAME (field) != 0) 268790075Sobrien { 2688169689Skan error ("zero width for bit-field %q+D", field); 268990075Sobrien w = error_mark_node; 269090075Sobrien } 269190075Sobrien else if (compare_tree_int (w, TYPE_PRECISION (type)) > 0 269290075Sobrien && TREE_CODE (type) != ENUMERAL_TYPE 269390075Sobrien && TREE_CODE (type) != BOOLEAN_TYPE) 2694169689Skan warning (0, "width of %q+D exceeds its type", field); 269590075Sobrien else if (TREE_CODE (type) == ENUMERAL_TYPE 269690075Sobrien && (0 > compare_tree_int (w, 269790075Sobrien min_precision (TYPE_MIN_VALUE (type), 2698169689Skan TYPE_UNSIGNED (type))) 269990075Sobrien || 0 > compare_tree_int (w, 270090075Sobrien min_precision 270190075Sobrien (TYPE_MAX_VALUE (type), 2702169689Skan TYPE_UNSIGNED (type))))) 2703169689Skan warning (0, "%q+D is too small to hold all values of %q#T", field, type); 270490075Sobrien } 270590075Sobrien 270690075Sobrien if (w != error_mark_node) 270718334Speter { 270890075Sobrien DECL_SIZE (field) = convert (bitsizetype, w); 270990075Sobrien DECL_BIT_FIELD (field) = 1; 271018334Speter } 271118334Speter else 271218334Speter { 271390075Sobrien /* Non-bit-fields are aligned for their type. */ 271490075Sobrien DECL_BIT_FIELD (field) = 0; 271590075Sobrien CLEAR_DECL_C_BIT_FIELD (field); 271618334Speter } 271790075Sobrien} 271818334Speter 271990075Sobrien/* FIELD is a non bit-field. We are finishing the processing for its 272090075Sobrien enclosing type T. Issue any appropriate messages and set appropriate 272190075Sobrien flags. */ 272290075Sobrien 272390075Sobrienstatic void 2724132718Skancheck_field_decl (tree field, 2725169689Skan tree t, 2726169689Skan int* cant_have_const_ctor, 2727169689Skan int* no_const_asn_ref, 2728132718Skan int* any_default_members) 272990075Sobrien{ 273090075Sobrien tree type = strip_array_types (TREE_TYPE (field)); 273190075Sobrien 273290075Sobrien /* An anonymous union cannot contain any fields which would change 273390075Sobrien the settings of CANT_HAVE_CONST_CTOR and friends. */ 273490075Sobrien if (ANON_UNION_TYPE_P (type)) 273590075Sobrien ; 273690075Sobrien /* And, we don't set TYPE_HAS_CONST_INIT_REF, etc., for anonymous 273790075Sobrien structs. So, we recurse through their fields here. */ 273890075Sobrien else if (ANON_AGGR_TYPE_P (type)) 273918334Speter { 274090075Sobrien tree fields; 274190075Sobrien 274290075Sobrien for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields)) 274390075Sobrien if (TREE_CODE (fields) == FIELD_DECL && !DECL_C_BIT_FIELD (field)) 274490075Sobrien check_field_decl (fields, t, cant_have_const_ctor, 2745169689Skan no_const_asn_ref, any_default_members); 274618334Speter } 274790075Sobrien /* Check members with class type for constructors, destructors, 274890075Sobrien etc. */ 274990075Sobrien else if (CLASS_TYPE_P (type)) 275090075Sobrien { 275190075Sobrien /* Never let anything with uninheritable virtuals 275290075Sobrien make it through without complaint. */ 275390075Sobrien abstract_virtuals_error (field, type); 2754169689Skan 275590075Sobrien if (TREE_CODE (t) == UNION_TYPE) 275690075Sobrien { 275790075Sobrien if (TYPE_NEEDS_CONSTRUCTING (type)) 2758169689Skan error ("member %q+#D with constructor not allowed in union", 2759169689Skan field); 276090075Sobrien if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)) 2761169689Skan error ("member %q+#D with destructor not allowed in union", field); 276290075Sobrien if (TYPE_HAS_COMPLEX_ASSIGN_REF (type)) 2763169689Skan error ("member %q+#D with copy assignment operator not allowed in union", 2764169689Skan field); 276590075Sobrien } 276690075Sobrien else 276790075Sobrien { 276890075Sobrien TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type); 2769169689Skan TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) 277090075Sobrien |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type); 277190075Sobrien TYPE_HAS_COMPLEX_ASSIGN_REF (t) |= TYPE_HAS_COMPLEX_ASSIGN_REF (type); 277290075Sobrien TYPE_HAS_COMPLEX_INIT_REF (t) |= TYPE_HAS_COMPLEX_INIT_REF (type); 277390075Sobrien } 277418334Speter 277590075Sobrien if (!TYPE_HAS_CONST_INIT_REF (type)) 277690075Sobrien *cant_have_const_ctor = 1; 277718334Speter 277890075Sobrien if (!TYPE_HAS_CONST_ASSIGN_REF (type)) 277990075Sobrien *no_const_asn_ref = 1; 278090075Sobrien } 278190075Sobrien if (DECL_INITIAL (field) != NULL_TREE) 278218334Speter { 278390075Sobrien /* `build_class_init_list' does not recognize 278490075Sobrien non-FIELD_DECLs. */ 278590075Sobrien if (TREE_CODE (t) == UNION_TYPE && any_default_members != 0) 2786169689Skan error ("multiple fields in union %qT initialized", t); 278790075Sobrien *any_default_members = 1; 278890075Sobrien } 278990075Sobrien} 279018334Speter 279190075Sobrien/* Check the data members (both static and non-static), class-scoped 279290075Sobrien typedefs, etc., appearing in the declaration of T. Issue 279390075Sobrien appropriate diagnostics. Sets ACCESS_DECLS to a list (in 279490075Sobrien declaration order) of access declarations; each TREE_VALUE in this 279590075Sobrien list is a USING_DECL. 279618334Speter 279790075Sobrien In addition, set the following flags: 279850397Sobrien 279990075Sobrien EMPTY_P 280090075Sobrien The class is empty, i.e., contains no non-static data members. 280118334Speter 280290075Sobrien CANT_HAVE_CONST_CTOR_P 280390075Sobrien This class cannot have an implicitly generated copy constructor 280490075Sobrien taking a const reference. 280590075Sobrien 280690075Sobrien CANT_HAVE_CONST_ASN_REF 280790075Sobrien This class cannot have an implicitly generated assignment 280890075Sobrien operator taking a const reference. 280990075Sobrien 281090075Sobrien All of these flags should be initialized before calling this 281190075Sobrien function. 281290075Sobrien 281390075Sobrien Returns a pointer to the end of the TYPE_FIELDs chain; additional 281490075Sobrien fields can be added by adding to this chain. */ 281590075Sobrien 281690075Sobrienstatic void 2817117395Skancheck_field_decls (tree t, tree *access_decls, 2818117395Skan int *cant_have_const_ctor_p, 2819117395Skan int *no_const_asn_ref_p) 282090075Sobrien{ 282190075Sobrien tree *field; 282290075Sobrien tree *next; 2823169689Skan bool has_pointers; 282490075Sobrien int any_default_members; 2825169689Skan int cant_pack = 0; 282690075Sobrien 282790075Sobrien /* Assume there are no access declarations. */ 282890075Sobrien *access_decls = NULL_TREE; 282990075Sobrien /* Assume this class has no pointer members. */ 2830169689Skan has_pointers = false; 283190075Sobrien /* Assume none of the members of this class have default 283290075Sobrien initializations. */ 283390075Sobrien any_default_members = 0; 283490075Sobrien 283590075Sobrien for (field = &TYPE_FIELDS (t); *field; field = next) 283618334Speter { 283790075Sobrien tree x = *field; 283890075Sobrien tree type = TREE_TYPE (x); 283990075Sobrien 284090075Sobrien next = &TREE_CHAIN (x); 284190075Sobrien 284250397Sobrien if (TREE_CODE (x) == USING_DECL) 284350397Sobrien { 284490075Sobrien /* Prune the access declaration from the list of fields. */ 284590075Sobrien *field = TREE_CHAIN (x); 284690075Sobrien 284790075Sobrien /* Save the access declarations for our caller. */ 284890075Sobrien *access_decls = tree_cons (NULL_TREE, x, *access_decls); 284990075Sobrien 285090075Sobrien /* Since we've reset *FIELD there's no reason to skip to the 285190075Sobrien next field. */ 285290075Sobrien next = field; 285318334Speter continue; 285418334Speter } 285518334Speter 285650397Sobrien if (TREE_CODE (x) == TYPE_DECL 285750397Sobrien || TREE_CODE (x) == TEMPLATE_DECL) 285818334Speter continue; 285918334Speter 286018334Speter /* If we've gotten this far, it's a data member, possibly static, 286150397Sobrien or an enumerator. */ 286290075Sobrien DECL_CONTEXT (x) = t; 286318334Speter 2864132718Skan /* When this goes into scope, it will be a non-local reference. */ 2865132718Skan DECL_NONLOCAL (x) = 1; 2866132718Skan 2867132718Skan if (TREE_CODE (t) == UNION_TYPE) 2868132718Skan { 2869132718Skan /* [class.union] 2870132718Skan 2871132718Skan If a union contains a static data member, or a member of 2872169689Skan reference type, the program is ill-formed. */ 2873132718Skan if (TREE_CODE (x) == VAR_DECL) 2874132718Skan { 2875169689Skan error ("%q+D may not be static because it is a member of a union", x); 2876132718Skan continue; 2877132718Skan } 2878132718Skan if (TREE_CODE (type) == REFERENCE_TYPE) 2879132718Skan { 2880169689Skan error ("%q+D may not have reference type %qT because" 2881169689Skan " it is a member of a union", 2882169689Skan x, type); 2883132718Skan continue; 2884132718Skan } 2885132718Skan } 2886132718Skan 288718334Speter /* Perform error checking that did not get done in 288818334Speter grokdeclarator. */ 288990075Sobrien if (TREE_CODE (type) == FUNCTION_TYPE) 289018334Speter { 2891169689Skan error ("field %q+D invalidly declared function type", x); 289290075Sobrien type = build_pointer_type (type); 289390075Sobrien TREE_TYPE (x) = type; 289418334Speter } 289590075Sobrien else if (TREE_CODE (type) == METHOD_TYPE) 289618334Speter { 2897169689Skan error ("field %q+D invalidly declared method type", x); 289890075Sobrien type = build_pointer_type (type); 289990075Sobrien TREE_TYPE (x) = type; 290018334Speter } 290118334Speter 290290075Sobrien if (type == error_mark_node) 290318334Speter continue; 2904169689Skan 2905132718Skan if (TREE_CODE (x) == CONST_DECL || TREE_CODE (x) == VAR_DECL) 290618334Speter continue; 290718334Speter 290818334Speter /* Now it can only be a FIELD_DECL. */ 290918334Speter 291018334Speter if (TREE_PRIVATE (x) || TREE_PROTECTED (x)) 291190075Sobrien CLASSTYPE_NON_AGGREGATE (t) = 1; 291218334Speter 291318334Speter /* If this is of reference type, check if it needs an init. 291418334Speter Also do a little ANSI jig if necessary. */ 291590075Sobrien if (TREE_CODE (type) == REFERENCE_TYPE) 2916169689Skan { 291790075Sobrien CLASSTYPE_NON_POD_P (t) = 1; 291818334Speter if (DECL_INITIAL (x) == NULL_TREE) 2919107590Sobrien SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1); 292018334Speter 292118334Speter /* ARM $12.6.2: [A member initializer list] (or, for an 292218334Speter aggregate, initialization by a brace-enclosed list) is the 292318334Speter only way to initialize nonstatic const and reference 292418334Speter members. */ 292550397Sobrien TYPE_HAS_COMPLEX_ASSIGN_REF (t) = 1; 292618334Speter 2927117395Skan if (! TYPE_HAS_CONSTRUCTOR (t) && CLASSTYPE_NON_AGGREGATE (t) 2928117395Skan && extra_warnings) 2929169689Skan warning (OPT_Wextra, "non-static reference %q+#D in class without a constructor", x); 293018334Speter } 293118334Speter 293290075Sobrien type = strip_array_types (type); 293350397Sobrien 2934169689Skan if (TYPE_PACKED (t)) 2935169689Skan { 2936169689Skan if (!pod_type_p (type) && !TYPE_PACKED (type)) 2937169689Skan { 2938169689Skan warning 2939169689Skan (0, 2940169689Skan "ignoring packed attribute because of unpacked non-POD field %q+#D", 2941169689Skan x); 2942169689Skan cant_pack = 1; 2943169689Skan } 2944169689Skan else if (TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT) 2945169689Skan DECL_PACKED (x) = 1; 2946169689Skan } 2947169689Skan 2948169689Skan if (DECL_C_BIT_FIELD (x) && integer_zerop (DECL_INITIAL (x))) 2949169689Skan /* We don't treat zero-width bitfields as making a class 2950169689Skan non-empty. */ 2951169689Skan ; 2952169689Skan else 2953169689Skan { 2954169689Skan /* The class is non-empty. */ 2955169689Skan CLASSTYPE_EMPTY_P (t) = 0; 2956169689Skan /* The class is not even nearly empty. */ 2957169689Skan CLASSTYPE_NEARLY_EMPTY_P (t) = 0; 2958169689Skan /* If one of the data members contains an empty class, 2959169689Skan so does T. */ 2960169689Skan if (CLASS_TYPE_P (type) 2961169689Skan && CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type)) 2962169689Skan CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1; 2963169689Skan } 2964169689Skan 2965169689Skan /* This is used by -Weffc++ (see below). Warn only for pointers 2966169689Skan to members which might hold dynamic memory. So do not warn 2967169689Skan for pointers to functions or pointers to members. */ 2968169689Skan if (TYPE_PTR_P (type) 2969169689Skan && !TYPE_PTRFN_P (type) 2970169689Skan && !TYPE_PTR_TO_MEMBER_P (type)) 2971169689Skan has_pointers = true; 2972169689Skan 2973132718Skan if (CLASS_TYPE_P (type)) 2974132718Skan { 2975132718Skan if (CLASSTYPE_REF_FIELDS_NEED_INIT (type)) 2976132718Skan SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1); 2977132718Skan if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (type)) 2978132718Skan SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1); 2979132718Skan } 2980132718Skan 298190075Sobrien if (DECL_MUTABLE_P (x) || TYPE_HAS_MUTABLE_P (type)) 298290075Sobrien CLASSTYPE_HAS_MUTABLE (t) = 1; 298352284Sobrien 298490075Sobrien if (! pod_type_p (type)) 2985169689Skan /* DR 148 now allows pointers to members (which are POD themselves), 2986169689Skan to be allowed in POD structs. */ 298790075Sobrien CLASSTYPE_NON_POD_P (t) = 1; 298890075Sobrien 2989102780Skan if (! zero_init_p (type)) 2990102780Skan CLASSTYPE_NON_ZERO_INIT_P (t) = 1; 2991102780Skan 299218334Speter /* If any field is const, the structure type is pseudo-const. */ 299390075Sobrien if (CP_TYPE_CONST_P (type)) 299418334Speter { 299518334Speter C_TYPE_FIELDS_READONLY (t) = 1; 299618334Speter if (DECL_INITIAL (x) == NULL_TREE) 2997107590Sobrien SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1); 299818334Speter 299918334Speter /* ARM $12.6.2: [A member initializer list] (or, for an 300018334Speter aggregate, initialization by a brace-enclosed list) is the 300118334Speter only way to initialize nonstatic const and reference 300218334Speter members. */ 300350397Sobrien TYPE_HAS_COMPLEX_ASSIGN_REF (t) = 1; 300418334Speter 3005117395Skan if (! TYPE_HAS_CONSTRUCTOR (t) && CLASSTYPE_NON_AGGREGATE (t) 3006117395Skan && extra_warnings) 3007169689Skan warning (OPT_Wextra, "non-static const member %q+#D in class without a constructor", x); 300818334Speter } 300990075Sobrien /* A field that is pseudo-const makes the structure likewise. */ 3010132718Skan else if (CLASS_TYPE_P (type)) 301118334Speter { 301290075Sobrien C_TYPE_FIELDS_READONLY (t) |= C_TYPE_FIELDS_READONLY (type); 3013107590Sobrien SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 3014107590Sobrien CLASSTYPE_READONLY_FIELDS_NEED_INIT (t) 3015107590Sobrien | CLASSTYPE_READONLY_FIELDS_NEED_INIT (type)); 301618334Speter } 301718334Speter 301890075Sobrien /* Core issue 80: A nonstatic data member is required to have a 301990075Sobrien different name from the class iff the class has a 302090075Sobrien user-defined constructor. */ 3021132718Skan if (constructor_name_p (DECL_NAME (x), t) && TYPE_HAS_CONSTRUCTOR (t)) 3022169689Skan pedwarn ("field %q+#D with same name as class", x); 302390075Sobrien 302452284Sobrien /* We set DECL_C_BIT_FIELD in grokbitfield. 302552284Sobrien If the type and width are valid, we'll also set DECL_BIT_FIELD. */ 302652284Sobrien if (DECL_C_BIT_FIELD (x)) 302790075Sobrien check_bitfield_decl (x); 302890075Sobrien else 302990075Sobrien check_field_decl (x, t, 303090075Sobrien cant_have_const_ctor_p, 303190075Sobrien no_const_asn_ref_p, 303290075Sobrien &any_default_members); 303390075Sobrien } 303490075Sobrien 3035169689Skan /* Effective C++ rule 11: if a class has dynamic memory held by pointers, 3036169689Skan it should also define a copy constructor and an assignment operator to 3037169689Skan implement the correct copy semantic (deep vs shallow, etc.). As it is 3038169689Skan not feasible to check whether the constructors do allocate dynamic memory 3039169689Skan and store it within members, we approximate the warning like this: 3040169689Skan 3041169689Skan -- Warn only if there are members which are pointers 3042169689Skan -- Warn only if there is a non-trivial constructor (otherwise, 3043169689Skan there cannot be memory allocated). 3044169689Skan -- Warn only if there is a non-trivial destructor. We assume that the 3045169689Skan user at least implemented the cleanup correctly, and a destructor 3046169689Skan is needed to free dynamic memory. 3047169689Skan 3048169689Skan This seems enough for practical purposes. */ 3049169689Skan if (warn_ecpp 3050169689Skan && has_pointers 3051169689Skan && TYPE_HAS_CONSTRUCTOR (t) 3052169689Skan && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) 3053169689Skan && !(TYPE_HAS_INIT_REF (t) && TYPE_HAS_ASSIGN_REF (t))) 305490075Sobrien { 3055169689Skan warning (OPT_Weffc__, "%q#T has pointer data members", t); 3056169689Skan 305790075Sobrien if (! TYPE_HAS_INIT_REF (t)) 305818334Speter { 3059169689Skan warning (OPT_Weffc__, 3060169689Skan " but does not override %<%T(const %T&)%>", t, t); 3061169689Skan if (!TYPE_HAS_ASSIGN_REF (t)) 3062169689Skan warning (OPT_Weffc__, " or %<operator=(const %T&)%>", t); 306390075Sobrien } 306490075Sobrien else if (! TYPE_HAS_ASSIGN_REF (t)) 3065169689Skan warning (OPT_Weffc__, 3066169689Skan " but does not override %<operator=(const %T&)%>", t); 306790075Sobrien } 306818334Speter 3069169689Skan /* If any of the fields couldn't be packed, unset TYPE_PACKED. */ 3070169689Skan if (cant_pack) 3071169689Skan TYPE_PACKED (t) = 0; 307218334Speter 307390075Sobrien /* Check anonymous struct/anonymous union fields. */ 307490075Sobrien finish_struct_anon (t); 307550397Sobrien 307690075Sobrien /* We've built up the list of access declarations in reverse order. 307790075Sobrien Fix that now. */ 307890075Sobrien *access_decls = nreverse (*access_decls); 307990075Sobrien} 308050397Sobrien 308190075Sobrien/* If TYPE is an empty class type, records its OFFSET in the table of 308290075Sobrien OFFSETS. */ 308318334Speter 308490075Sobrienstatic int 3085132718Skanrecord_subobject_offset (tree type, tree offset, splay_tree offsets) 308690075Sobrien{ 308790075Sobrien splay_tree_node n; 308852284Sobrien 308990075Sobrien if (!is_empty_class (type)) 309090075Sobrien return 0; 309190075Sobrien 309290075Sobrien /* Record the location of this empty object in OFFSETS. */ 309390075Sobrien n = splay_tree_lookup (offsets, (splay_tree_key) offset); 309490075Sobrien if (!n) 3095169689Skan n = splay_tree_insert (offsets, 309690075Sobrien (splay_tree_key) offset, 309790075Sobrien (splay_tree_value) NULL_TREE); 3098169689Skan n->value = ((splay_tree_value) 309990075Sobrien tree_cons (NULL_TREE, 310090075Sobrien type, 310190075Sobrien (tree) n->value)); 310290075Sobrien 310390075Sobrien return 0; 310490075Sobrien} 310590075Sobrien 3106117395Skan/* Returns nonzero if TYPE is an empty class type and there is 310790075Sobrien already an entry in OFFSETS for the same TYPE as the same OFFSET. */ 310890075Sobrien 310990075Sobrienstatic int 3110132718Skancheck_subobject_offset (tree type, tree offset, splay_tree offsets) 311190075Sobrien{ 311290075Sobrien splay_tree_node n; 311390075Sobrien tree t; 311490075Sobrien 311590075Sobrien if (!is_empty_class (type)) 311690075Sobrien return 0; 311790075Sobrien 311890075Sobrien /* Record the location of this empty object in OFFSETS. */ 311990075Sobrien n = splay_tree_lookup (offsets, (splay_tree_key) offset); 312090075Sobrien if (!n) 312190075Sobrien return 0; 312290075Sobrien 312390075Sobrien for (t = (tree) n->value; t; t = TREE_CHAIN (t)) 312490075Sobrien if (same_type_p (TREE_VALUE (t), type)) 312590075Sobrien return 1; 312690075Sobrien 312790075Sobrien return 0; 312890075Sobrien} 312990075Sobrien 313090075Sobrien/* Walk through all the subobjects of TYPE (located at OFFSET). Call 313190075Sobrien F for every subobject, passing it the type, offset, and table of 3132117395Skan OFFSETS. If VBASES_P is one, then virtual non-primary bases should 3133117395Skan be traversed. 313490075Sobrien 313590075Sobrien If MAX_OFFSET is non-NULL, then subobjects with an offset greater 313690075Sobrien than MAX_OFFSET will not be walked. 313790075Sobrien 3138117395Skan If F returns a nonzero value, the traversal ceases, and that value 313990075Sobrien is returned. Otherwise, returns zero. */ 314090075Sobrien 314190075Sobrienstatic int 3142169689Skanwalk_subobject_offsets (tree type, 3143169689Skan subobject_offset_fn f, 3144169689Skan tree offset, 3145169689Skan splay_tree offsets, 3146169689Skan tree max_offset, 3147169689Skan int vbases_p) 314890075Sobrien{ 314990075Sobrien int r = 0; 3150117395Skan tree type_binfo = NULL_TREE; 315190075Sobrien 315290075Sobrien /* If this OFFSET is bigger than the MAX_OFFSET, then we should 315390075Sobrien stop. */ 315490075Sobrien if (max_offset && INT_CST_LT (max_offset, offset)) 315590075Sobrien return 0; 315690075Sobrien 3157169689Skan if (type == error_mark_node) 3158169689Skan return 0; 3159169689Skan 3160169689Skan if (!TYPE_P (type)) 3161117395Skan { 3162117395Skan if (abi_version_at_least (2)) 3163117395Skan type_binfo = type; 3164117395Skan type = BINFO_TYPE (type); 3165117395Skan } 3166117395Skan 316790075Sobrien if (CLASS_TYPE_P (type)) 316890075Sobrien { 316990075Sobrien tree field; 3170117395Skan tree binfo; 317190075Sobrien int i; 317290075Sobrien 3173107590Sobrien /* Avoid recursing into objects that are not interesting. */ 3174107590Sobrien if (!CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type)) 3175107590Sobrien return 0; 3176107590Sobrien 317790075Sobrien /* Record the location of TYPE. */ 317890075Sobrien r = (*f) (type, offset, offsets); 317990075Sobrien if (r) 318090075Sobrien return r; 318190075Sobrien 318290075Sobrien /* Iterate through the direct base classes of TYPE. */ 3183117395Skan if (!type_binfo) 3184117395Skan type_binfo = TYPE_BINFO (type); 3185169689Skan for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, binfo); i++) 318690075Sobrien { 3187117395Skan tree binfo_offset; 318890075Sobrien 3189169689Skan if (abi_version_at_least (2) 3190169689Skan && BINFO_VIRTUAL_P (binfo)) 3191117395Skan continue; 3192117395Skan 3193169689Skan if (!vbases_p 3194169689Skan && BINFO_VIRTUAL_P (binfo) 319590075Sobrien && !BINFO_PRIMARY_P (binfo)) 319690075Sobrien continue; 319790075Sobrien 3198117395Skan if (!abi_version_at_least (2)) 3199117395Skan binfo_offset = size_binop (PLUS_EXPR, 3200117395Skan offset, 3201117395Skan BINFO_OFFSET (binfo)); 3202117395Skan else 3203117395Skan { 3204117395Skan tree orig_binfo; 3205117395Skan /* We cannot rely on BINFO_OFFSET being set for the base 3206117395Skan class yet, but the offsets for direct non-virtual 3207117395Skan bases can be calculated by going back to the TYPE. */ 3208169689Skan orig_binfo = BINFO_BASE_BINFO (TYPE_BINFO (type), i); 3209169689Skan binfo_offset = size_binop (PLUS_EXPR, 3210117395Skan offset, 3211117395Skan BINFO_OFFSET (orig_binfo)); 3212117395Skan } 3213117395Skan 3214117395Skan r = walk_subobject_offsets (binfo, 321590075Sobrien f, 3216117395Skan binfo_offset, 321790075Sobrien offsets, 321890075Sobrien max_offset, 3219169689Skan (abi_version_at_least (2) 3220117395Skan ? /*vbases_p=*/0 : vbases_p)); 322190075Sobrien if (r) 322290075Sobrien return r; 322318334Speter } 322490075Sobrien 3225169689Skan if (abi_version_at_least (2) && CLASSTYPE_VBASECLASSES (type)) 3226117395Skan { 3227169689Skan unsigned ix; 3228169689Skan VEC(tree,gc) *vbases; 3229117395Skan 3230117395Skan /* Iterate through the virtual base classes of TYPE. In G++ 3231117395Skan 3.2, we included virtual bases in the direct base class 3232117395Skan loop above, which results in incorrect results; the 3233117395Skan correct offsets for virtual bases are only known when 3234117395Skan working with the most derived type. */ 3235117395Skan if (vbases_p) 3236169689Skan for (vbases = CLASSTYPE_VBASECLASSES (type), ix = 0; 3237169689Skan VEC_iterate (tree, vbases, ix, binfo); ix++) 3238117395Skan { 3239117395Skan r = walk_subobject_offsets (binfo, 3240117395Skan f, 3241117395Skan size_binop (PLUS_EXPR, 3242117395Skan offset, 3243117395Skan BINFO_OFFSET (binfo)), 3244117395Skan offsets, 3245117395Skan max_offset, 3246117395Skan /*vbases_p=*/0); 3247117395Skan if (r) 3248117395Skan return r; 3249117395Skan } 3250117395Skan else 3251117395Skan { 3252117395Skan /* We still have to walk the primary base, if it is 3253117395Skan virtual. (If it is non-virtual, then it was walked 3254117395Skan above.) */ 3255169689Skan tree vbase = get_primary_binfo (type_binfo); 3256169689Skan 3257169689Skan if (vbase && BINFO_VIRTUAL_P (vbase) 3258169689Skan && BINFO_PRIMARY_P (vbase) 3259169689Skan && BINFO_INHERITANCE_CHAIN (vbase) == type_binfo) 3260117395Skan { 3261169689Skan r = (walk_subobject_offsets 3262132718Skan (vbase, f, offset, 3263132718Skan offsets, max_offset, /*vbases_p=*/0)); 3264132718Skan if (r) 3265132718Skan return r; 3266117395Skan } 3267117395Skan } 3268117395Skan } 3269117395Skan 327090075Sobrien /* Iterate through the fields of TYPE. */ 327190075Sobrien for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field)) 3272117395Skan if (TREE_CODE (field) == FIELD_DECL && !DECL_ARTIFICIAL (field)) 327390075Sobrien { 3274117395Skan tree field_offset; 3275117395Skan 3276117395Skan if (abi_version_at_least (2)) 3277117395Skan field_offset = byte_position (field); 3278117395Skan else 3279117395Skan /* In G++ 3.2, DECL_FIELD_OFFSET was used. */ 3280117395Skan field_offset = DECL_FIELD_OFFSET (field); 3281117395Skan 328290075Sobrien r = walk_subobject_offsets (TREE_TYPE (field), 328390075Sobrien f, 328490075Sobrien size_binop (PLUS_EXPR, 328590075Sobrien offset, 3286117395Skan field_offset), 328790075Sobrien offsets, 328890075Sobrien max_offset, 328990075Sobrien /*vbases_p=*/1); 329090075Sobrien if (r) 329190075Sobrien return r; 329290075Sobrien } 329390075Sobrien } 329490075Sobrien else if (TREE_CODE (type) == ARRAY_TYPE) 329590075Sobrien { 3296107590Sobrien tree element_type = strip_array_types (type); 329790075Sobrien tree domain = TYPE_DOMAIN (type); 329890075Sobrien tree index; 329990075Sobrien 3300107590Sobrien /* Avoid recursing into objects that are not interesting. */ 3301107590Sobrien if (!CLASS_TYPE_P (element_type) 3302107590Sobrien || !CLASSTYPE_CONTAINS_EMPTY_CLASS_P (element_type)) 3303107590Sobrien return 0; 3304107590Sobrien 330590075Sobrien /* Step through each of the elements in the array. */ 3306117395Skan for (index = size_zero_node; 3307117395Skan /* G++ 3.2 had an off-by-one error here. */ 3308169689Skan (abi_version_at_least (2) 3309117395Skan ? !INT_CST_LT (TYPE_MAX_VALUE (domain), index) 3310117395Skan : INT_CST_LT (index, TYPE_MAX_VALUE (domain))); 331190075Sobrien index = size_binop (PLUS_EXPR, index, size_one_node)) 331290075Sobrien { 331390075Sobrien r = walk_subobject_offsets (TREE_TYPE (type), 331490075Sobrien f, 331590075Sobrien offset, 331690075Sobrien offsets, 331790075Sobrien max_offset, 331890075Sobrien /*vbases_p=*/1); 331990075Sobrien if (r) 332090075Sobrien return r; 3321169689Skan offset = size_binop (PLUS_EXPR, offset, 332290075Sobrien TYPE_SIZE_UNIT (TREE_TYPE (type))); 332390075Sobrien /* If this new OFFSET is bigger than the MAX_OFFSET, then 332490075Sobrien there's no point in iterating through the remaining 332590075Sobrien elements of the array. */ 332690075Sobrien if (max_offset && INT_CST_LT (max_offset, offset)) 332790075Sobrien break; 332890075Sobrien } 332990075Sobrien } 333090075Sobrien 333190075Sobrien return 0; 333290075Sobrien} 333390075Sobrien 3334169689Skan/* Record all of the empty subobjects of TYPE (either a type or a 3335169689Skan binfo). If IS_DATA_MEMBER is true, then a non-static data member 3336169689Skan is being placed at OFFSET; otherwise, it is a base class that is 3337169689Skan being placed at OFFSET. */ 333890075Sobrien 333990075Sobrienstatic void 3340169689Skanrecord_subobject_offsets (tree type, 3341169689Skan tree offset, 3342169689Skan splay_tree offsets, 3343169689Skan bool is_data_member) 334490075Sobrien{ 3345169689Skan tree max_offset; 3346169689Skan /* If recording subobjects for a non-static data member or a 3347169689Skan non-empty base class , we do not need to record offsets beyond 3348169689Skan the size of the biggest empty class. Additional data members 3349169689Skan will go at the end of the class. Additional base classes will go 3350169689Skan either at offset zero (if empty, in which case they cannot 3351169689Skan overlap with offsets past the size of the biggest empty class) or 3352169689Skan at the end of the class. 3353169689Skan 3354169689Skan However, if we are placing an empty base class, then we must record 3355169689Skan all offsets, as either the empty class is at offset zero (where 3356169689Skan other empty classes might later be placed) or at the end of the 3357169689Skan class (where other objects might then be placed, so other empty 3358169689Skan subobjects might later overlap). */ 3359169689Skan if (is_data_member 3360169689Skan || !is_empty_class (BINFO_TYPE (type))) 3361169689Skan max_offset = sizeof_biggest_empty_class; 3362169689Skan else 3363169689Skan max_offset = NULL_TREE; 336490075Sobrien walk_subobject_offsets (type, record_subobject_offset, offset, 3365169689Skan offsets, max_offset, is_data_member); 336690075Sobrien} 336790075Sobrien 3368117395Skan/* Returns nonzero if any of the empty subobjects of TYPE (located at 3369117395Skan OFFSET) conflict with entries in OFFSETS. If VBASES_P is nonzero, 337090075Sobrien virtual bases of TYPE are examined. */ 337190075Sobrien 337290075Sobrienstatic int 3373132718Skanlayout_conflict_p (tree type, 3374169689Skan tree offset, 3375169689Skan splay_tree offsets, 3376169689Skan int vbases_p) 337790075Sobrien{ 337890075Sobrien splay_tree_node max_node; 337990075Sobrien 338090075Sobrien /* Get the node in OFFSETS that indicates the maximum offset where 338190075Sobrien an empty subobject is located. */ 338290075Sobrien max_node = splay_tree_max (offsets); 338390075Sobrien /* If there aren't any empty subobjects, then there's no point in 338490075Sobrien performing this check. */ 338590075Sobrien if (!max_node) 338690075Sobrien return 0; 338790075Sobrien 338890075Sobrien return walk_subobject_offsets (type, check_subobject_offset, offset, 338990075Sobrien offsets, (tree) (max_node->key), 339090075Sobrien vbases_p); 339190075Sobrien} 339290075Sobrien 339390075Sobrien/* DECL is a FIELD_DECL corresponding either to a base subobject of a 339490075Sobrien non-static data member of the type indicated by RLI. BINFO is the 339590075Sobrien binfo corresponding to the base subobject, OFFSETS maps offsets to 3396117395Skan types already located at those offsets. This function determines 3397117395Skan the position of the DECL. */ 339890075Sobrien 339990075Sobrienstatic void 3400169689Skanlayout_nonempty_base_or_field (record_layout_info rli, 3401169689Skan tree decl, 3402169689Skan tree binfo, 3403117395Skan splay_tree offsets) 340490075Sobrien{ 340590075Sobrien tree offset = NULL_TREE; 3406117395Skan bool field_p; 3407117395Skan tree type; 3408169689Skan 3409117395Skan if (binfo) 3410117395Skan { 3411117395Skan /* For the purposes of determining layout conflicts, we want to 3412117395Skan use the class type of BINFO; TREE_TYPE (DECL) will be the 3413117395Skan CLASSTYPE_AS_BASE version, which does not contain entries for 3414117395Skan zero-sized bases. */ 3415117395Skan type = TREE_TYPE (binfo); 3416117395Skan field_p = false; 3417117395Skan } 3418117395Skan else 3419117395Skan { 3420117395Skan type = TREE_TYPE (decl); 3421117395Skan field_p = true; 3422117395Skan } 342390075Sobrien 342490075Sobrien /* Try to place the field. It may take more than one try if we have 342590075Sobrien a hard time placing the field without putting two objects of the 342690075Sobrien same type at the same address. */ 342790075Sobrien while (1) 342890075Sobrien { 342990075Sobrien struct record_layout_info_s old_rli = *rli; 343090075Sobrien 343190075Sobrien /* Place this field. */ 343290075Sobrien place_field (rli, decl); 343390075Sobrien offset = byte_position (decl); 3434132718Skan 343590075Sobrien /* We have to check to see whether or not there is already 343690075Sobrien something of the same type at the offset we're about to use. 3437132718Skan For example, consider: 3438169689Skan 3439132718Skan struct S {}; 3440132718Skan struct T : public S { int i; }; 3441132718Skan struct U : public S, public T {}; 3442169689Skan 344390075Sobrien Here, we put S at offset zero in U. Then, we can't put T at 344490075Sobrien offset zero -- its S component would be at the same address 344590075Sobrien as the S we already allocated. So, we have to skip ahead. 344690075Sobrien Since all data members, including those whose type is an 3447117395Skan empty class, have nonzero size, any overlap can happen only 344890075Sobrien with a direct or indirect base-class -- it can't happen with 344990075Sobrien a data member. */ 3450132718Skan /* In a union, overlap is permitted; all members are placed at 3451132718Skan offset zero. */ 3452132718Skan if (TREE_CODE (rli->t) == UNION_TYPE) 3453132718Skan break; 3454117395Skan /* G++ 3.2 did not check for overlaps when placing a non-empty 3455117395Skan virtual base. */ 3456169689Skan if (!abi_version_at_least (2) && binfo && BINFO_VIRTUAL_P (binfo)) 3457117395Skan break; 3458169689Skan if (layout_conflict_p (field_p ? type : binfo, offset, 3459117395Skan offsets, field_p)) 346090075Sobrien { 346190075Sobrien /* Strip off the size allocated to this field. That puts us 346290075Sobrien at the first place we could have put the field with 346390075Sobrien proper alignment. */ 346490075Sobrien *rli = old_rli; 346590075Sobrien 346690075Sobrien /* Bump up by the alignment required for the type. */ 346790075Sobrien rli->bitpos 3468169689Skan = size_binop (PLUS_EXPR, rli->bitpos, 3469169689Skan bitsize_int (binfo 347090075Sobrien ? CLASSTYPE_ALIGN (type) 347190075Sobrien : TYPE_ALIGN (type))); 347290075Sobrien normalize_rli (rli); 347390075Sobrien } 347418334Speter else 347590075Sobrien /* There was no conflict. We're done laying out this field. */ 347690075Sobrien break; 347790075Sobrien } 347890075Sobrien 347990075Sobrien /* Now that we know where it will be placed, update its 348090075Sobrien BINFO_OFFSET. */ 348190075Sobrien if (binfo && CLASS_TYPE_P (BINFO_TYPE (binfo))) 3482117395Skan /* Indirect virtual bases may have a nonzero BINFO_OFFSET at 3483117395Skan this point because their BINFO_OFFSET is copied from another 3484117395Skan hierarchy. Therefore, we may not need to add the entire 3485117395Skan OFFSET. */ 3486169689Skan propagate_binfo_offsets (binfo, 3487117395Skan size_diffop (convert (ssizetype, offset), 3488169689Skan convert (ssizetype, 3489132718Skan BINFO_OFFSET (binfo)))); 349090075Sobrien} 349190075Sobrien 3492117395Skan/* Returns true if TYPE is empty and OFFSET is nonzero. */ 3493117395Skan 3494117395Skanstatic int 3495117395Skanempty_base_at_nonzero_offset_p (tree type, 3496117395Skan tree offset, 3497117395Skan splay_tree offsets ATTRIBUTE_UNUSED) 3498117395Skan{ 3499117395Skan return is_empty_class (type) && !integer_zerop (offset); 3500117395Skan} 3501117395Skan 350290075Sobrien/* Layout the empty base BINFO. EOC indicates the byte currently just 350390075Sobrien past the end of the class, and should be correctly aligned for a 350490075Sobrien class of the type indicated by BINFO; OFFSETS gives the offsets of 350590075Sobrien the empty bases allocated so far. T is the most derived 3506117395Skan type. Return nonzero iff we added it at the end. */ 350790075Sobrien 350890075Sobrienstatic bool 3509132718Skanlayout_empty_base (tree binfo, tree eoc, splay_tree offsets) 351090075Sobrien{ 351190075Sobrien tree alignment; 351290075Sobrien tree basetype = BINFO_TYPE (binfo); 351390075Sobrien bool atend = false; 3514117395Skan 351590075Sobrien /* This routine should only be used for empty classes. */ 3516169689Skan gcc_assert (is_empty_class (basetype)); 351790075Sobrien alignment = ssize_int (CLASSTYPE_ALIGN_UNIT (basetype)); 3518132718Skan 3519132718Skan if (!integer_zerop (BINFO_OFFSET (binfo))) 3520132718Skan { 3521132718Skan if (abi_version_at_least (2)) 3522132718Skan propagate_binfo_offsets 3523132718Skan (binfo, size_diffop (size_zero_node, BINFO_OFFSET (binfo))); 3524169689Skan else 3525169689Skan warning (OPT_Wabi, 3526169689Skan "offset of empty base %qT may not be ABI-compliant and may" 3527132718Skan "change in a future version of GCC", 3528132718Skan BINFO_TYPE (binfo)); 3529132718Skan } 3530169689Skan 353190075Sobrien /* This is an empty base class. We first try to put it at offset 353290075Sobrien zero. */ 3533117395Skan if (layout_conflict_p (binfo, 353490075Sobrien BINFO_OFFSET (binfo), 3535169689Skan offsets, 353690075Sobrien /*vbases_p=*/0)) 353790075Sobrien { 353890075Sobrien /* That didn't work. Now, we move forward from the next 353990075Sobrien available spot in the class. */ 354090075Sobrien atend = true; 3541132718Skan propagate_binfo_offsets (binfo, convert (ssizetype, eoc)); 3542169689Skan while (1) 354318334Speter { 3544117395Skan if (!layout_conflict_p (binfo, 3545169689Skan BINFO_OFFSET (binfo), 354690075Sobrien offsets, 354790075Sobrien /*vbases_p=*/0)) 354890075Sobrien /* We finally found a spot where there's no overlap. */ 354990075Sobrien break; 355018334Speter 355190075Sobrien /* There's overlap here, too. Bump along to the next spot. */ 3552132718Skan propagate_binfo_offsets (binfo, alignment); 355390075Sobrien } 355490075Sobrien } 355590075Sobrien return atend; 355690075Sobrien} 355718334Speter 3558169689Skan/* Layout the base given by BINFO in the class indicated by RLI. 3559117395Skan *BASE_ALIGN is a running maximum of the alignments of 3560117395Skan any base class. OFFSETS gives the location of empty base 3561117395Skan subobjects. T is the most derived type. Return nonzero if the new 3562117395Skan object cannot be nearly-empty. A new FIELD_DECL is inserted at 3563169689Skan *NEXT_FIELD, unless BINFO is for an empty base class. 356418334Speter 3565117395Skan Returns the location at which the next field should be inserted. */ 3566117395Skan 3567117395Skanstatic tree * 3568117395Skanbuild_base_field (record_layout_info rli, tree binfo, 3569117395Skan splay_tree offsets, tree *next_field) 357090075Sobrien{ 3571117395Skan tree t = rli->t; 357290075Sobrien tree basetype = BINFO_TYPE (binfo); 357318334Speter 357490075Sobrien if (!COMPLETE_TYPE_P (basetype)) 357590075Sobrien /* This error is now reported in xref_tag, thus giving better 357690075Sobrien location information. */ 3577117395Skan return next_field; 3578169689Skan 3579117395Skan /* Place the base class. */ 3580117395Skan if (!is_empty_class (basetype)) 358190075Sobrien { 3582117395Skan tree decl; 3583117395Skan 358490075Sobrien /* The containing class is non-empty because it has a non-empty 358590075Sobrien base class. */ 3586117395Skan CLASSTYPE_EMPTY_P (t) = 0; 3587169689Skan 3588117395Skan /* Create the FIELD_DECL. */ 3589117395Skan decl = build_decl (FIELD_DECL, NULL_TREE, CLASSTYPE_AS_BASE (basetype)); 3590117395Skan DECL_ARTIFICIAL (decl) = 1; 3591169689Skan DECL_IGNORED_P (decl) = 1; 3592117395Skan DECL_FIELD_CONTEXT (decl) = t; 3593117395Skan DECL_SIZE (decl) = CLASSTYPE_SIZE (basetype); 3594117395Skan DECL_SIZE_UNIT (decl) = CLASSTYPE_SIZE_UNIT (basetype); 3595117395Skan DECL_ALIGN (decl) = CLASSTYPE_ALIGN (basetype); 3596117395Skan DECL_USER_ALIGN (decl) = CLASSTYPE_USER_ALIGN (basetype); 3597169689Skan DECL_MODE (decl) = TYPE_MODE (basetype); 3598169689Skan DECL_FIELD_IS_BASE (decl) = 1; 359918334Speter 360090075Sobrien /* Try to place the field. It may take more than one try if we 360190075Sobrien have a hard time placing the field without putting two 360290075Sobrien objects of the same type at the same address. */ 3603117395Skan layout_nonempty_base_or_field (rli, decl, binfo, offsets); 3604117395Skan /* Add the new FIELD_DECL to the list of fields for T. */ 3605117395Skan TREE_CHAIN (decl) = *next_field; 3606117395Skan *next_field = decl; 3607117395Skan next_field = &TREE_CHAIN (decl); 360890075Sobrien } 360990075Sobrien else 361090075Sobrien { 3611117395Skan tree eoc; 3612117395Skan bool atend; 361390075Sobrien 361490075Sobrien /* On some platforms (ARM), even empty classes will not be 361590075Sobrien byte-aligned. */ 3616117395Skan eoc = round_up (rli_size_unit_so_far (rli), 3617117395Skan CLASSTYPE_ALIGN_UNIT (basetype)); 3618132718Skan atend = layout_empty_base (binfo, eoc, offsets); 3619117395Skan /* A nearly-empty class "has no proper base class that is empty, 3620117395Skan not morally virtual, and at an offset other than zero." */ 3621169689Skan if (!BINFO_VIRTUAL_P (binfo) && CLASSTYPE_NEARLY_EMPTY_P (t)) 3622117395Skan { 3623117395Skan if (atend) 3624117395Skan CLASSTYPE_NEARLY_EMPTY_P (t) = 0; 3625169689Skan /* The check above (used in G++ 3.2) is insufficient because 3626117395Skan an empty class placed at offset zero might itself have an 3627117395Skan empty base at a nonzero offset. */ 3628169689Skan else if (walk_subobject_offsets (basetype, 3629117395Skan empty_base_at_nonzero_offset_p, 3630117395Skan size_zero_node, 3631117395Skan /*offsets=*/NULL, 3632117395Skan /*max_offset=*/NULL_TREE, 3633117395Skan /*vbases_p=*/true)) 3634117395Skan { 3635117395Skan if (abi_version_at_least (2)) 3636117395Skan CLASSTYPE_NEARLY_EMPTY_P (t) = 0; 3637169689Skan else 3638169689Skan warning (OPT_Wabi, 3639169689Skan "class %qT will be considered nearly empty in a " 3640117395Skan "future version of GCC", t); 3641117395Skan } 3642117395Skan } 3643169689Skan 3644117395Skan /* We do not create a FIELD_DECL for empty base classes because 3645117395Skan it might overlap some other field. We want to be able to 3646117395Skan create CONSTRUCTORs for the class by iterating over the 3647117395Skan FIELD_DECLs, and the back end does not handle overlapping 3648117395Skan FIELD_DECLs. */ 3649117395Skan 3650117395Skan /* An empty virtual base causes a class to be non-empty 3651117395Skan -- but in that case we do not need to clear CLASSTYPE_EMPTY_P 3652117395Skan here because that was already done when the virtual table 3653117395Skan pointer was created. */ 365490075Sobrien } 365590075Sobrien 365690075Sobrien /* Record the offsets of BINFO and its base subobjects. */ 3657117395Skan record_subobject_offsets (binfo, 365890075Sobrien BINFO_OFFSET (binfo), 3659169689Skan offsets, 3660169689Skan /*is_data_member=*/false); 3661117395Skan 3662117395Skan return next_field; 366390075Sobrien} 366490075Sobrien 366590075Sobrien/* Layout all of the non-virtual base classes. Record empty 3666117395Skan subobjects in OFFSETS. T is the most derived type. Return nonzero 3667117395Skan if the type cannot be nearly empty. The fields created 3668117395Skan corresponding to the base classes will be inserted at 3669117395Skan *NEXT_FIELD. */ 367090075Sobrien 3671117395Skanstatic void 3672117395Skanbuild_base_fields (record_layout_info rli, 3673117395Skan splay_tree offsets, tree *next_field) 367490075Sobrien{ 367590075Sobrien /* Chain to hold all the new FIELD_DECLs which stand in for base class 367690075Sobrien subobjects. */ 3677117395Skan tree t = rli->t; 3678169689Skan int n_baseclasses = BINFO_N_BASE_BINFOS (TYPE_BINFO (t)); 367990075Sobrien int i; 368090075Sobrien 368190075Sobrien /* The primary base class is always allocated first. */ 3682117395Skan if (CLASSTYPE_HAS_PRIMARY_BASE_P (t)) 3683117395Skan next_field = build_base_field (rli, CLASSTYPE_PRIMARY_BINFO (t), 3684117395Skan offsets, next_field); 368590075Sobrien 368690075Sobrien /* Now allocate the rest of the bases. */ 368790075Sobrien for (i = 0; i < n_baseclasses; ++i) 368890075Sobrien { 368990075Sobrien tree base_binfo; 369090075Sobrien 3691169689Skan base_binfo = BINFO_BASE_BINFO (TYPE_BINFO (t), i); 369290075Sobrien 369390075Sobrien /* The primary base was already allocated above, so we don't 369490075Sobrien need to allocate it again here. */ 3695117395Skan if (base_binfo == CLASSTYPE_PRIMARY_BINFO (t)) 369690075Sobrien continue; 369790075Sobrien 3698132718Skan /* Virtual bases are added at the end (a primary virtual base 3699132718Skan will have already been added). */ 3700169689Skan if (BINFO_VIRTUAL_P (base_binfo)) 370190075Sobrien continue; 370290075Sobrien 3703117395Skan next_field = build_base_field (rli, base_binfo, 3704117395Skan offsets, next_field); 370590075Sobrien } 370690075Sobrien} 370790075Sobrien 370890075Sobrien/* Go through the TYPE_METHODS of T issuing any appropriate 370990075Sobrien diagnostics, figuring out which methods override which other 371090075Sobrien methods, and so forth. */ 371190075Sobrien 371290075Sobrienstatic void 3713132718Skancheck_methods (tree t) 371490075Sobrien{ 371590075Sobrien tree x; 371690075Sobrien 371790075Sobrien for (x = TYPE_METHODS (t); x; x = TREE_CHAIN (x)) 371890075Sobrien { 371990075Sobrien check_for_override (x, t); 372090075Sobrien if (DECL_PURE_VIRTUAL_P (x) && ! DECL_VINDEX (x)) 3721169689Skan error ("initializer specified for non-virtual method %q+D", x); 372290075Sobrien /* The name of the field is the original field name 372390075Sobrien Save this in auxiliary field for later overloading. */ 372490075Sobrien if (DECL_VINDEX (x)) 372590075Sobrien { 372690075Sobrien TYPE_POLYMORPHIC_P (t) = 1; 372790075Sobrien if (DECL_PURE_VIRTUAL_P (x)) 3728169689Skan VEC_safe_push (tree, gc, CLASSTYPE_PURE_VIRTUALS (t), x); 372990075Sobrien } 3730169689Skan /* All user-declared destructors are non-trivial. */ 3731169689Skan if (DECL_DESTRUCTOR_P (x)) 3732169689Skan TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = 1; 373390075Sobrien } 373490075Sobrien} 373590075Sobrien 373690075Sobrien/* FN is a constructor or destructor. Clone the declaration to create 373790075Sobrien a specialized in-charge or not-in-charge version, as indicated by 373890075Sobrien NAME. */ 373990075Sobrien 374090075Sobrienstatic tree 3741132718Skanbuild_clone (tree fn, tree name) 374290075Sobrien{ 374390075Sobrien tree parms; 374490075Sobrien tree clone; 374590075Sobrien 374690075Sobrien /* Copy the function. */ 374790075Sobrien clone = copy_decl (fn); 374890075Sobrien /* Remember where this function came from. */ 374990075Sobrien DECL_CLONED_FUNCTION (clone) = fn; 375090075Sobrien DECL_ABSTRACT_ORIGIN (clone) = fn; 375190075Sobrien /* Reset the function name. */ 375290075Sobrien DECL_NAME (clone) = name; 375390075Sobrien SET_DECL_ASSEMBLER_NAME (clone, NULL_TREE); 375490075Sobrien /* There's no pending inline data for this function. */ 375590075Sobrien DECL_PENDING_INLINE_INFO (clone) = NULL; 375690075Sobrien DECL_PENDING_INLINE_P (clone) = 0; 375790075Sobrien /* And it hasn't yet been deferred. */ 375890075Sobrien DECL_DEFERRED_FN (clone) = 0; 375990075Sobrien 376090075Sobrien /* The base-class destructor is not virtual. */ 376190075Sobrien if (name == base_dtor_identifier) 376290075Sobrien { 376390075Sobrien DECL_VIRTUAL_P (clone) = 0; 376490075Sobrien if (TREE_CODE (clone) != TEMPLATE_DECL) 376590075Sobrien DECL_VINDEX (clone) = NULL_TREE; 376690075Sobrien } 376790075Sobrien 376890075Sobrien /* If there was an in-charge parameter, drop it from the function 376990075Sobrien type. */ 377090075Sobrien if (DECL_HAS_IN_CHARGE_PARM_P (clone)) 377190075Sobrien { 377290075Sobrien tree basetype; 377390075Sobrien tree parmtypes; 377490075Sobrien tree exceptions; 377590075Sobrien 377690075Sobrien exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone)); 377790075Sobrien basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone)); 377890075Sobrien parmtypes = TYPE_ARG_TYPES (TREE_TYPE (clone)); 377990075Sobrien /* Skip the `this' parameter. */ 378090075Sobrien parmtypes = TREE_CHAIN (parmtypes); 378190075Sobrien /* Skip the in-charge parameter. */ 378290075Sobrien parmtypes = TREE_CHAIN (parmtypes); 378390075Sobrien /* And the VTT parm, in a complete [cd]tor. */ 378490075Sobrien if (DECL_HAS_VTT_PARM_P (fn) 378590075Sobrien && ! DECL_NEEDS_VTT_PARM_P (clone)) 378690075Sobrien parmtypes = TREE_CHAIN (parmtypes); 378790075Sobrien /* If this is subobject constructor or destructor, add the vtt 378890075Sobrien parameter. */ 3789169689Skan TREE_TYPE (clone) 3790132718Skan = build_method_type_directly (basetype, 3791132718Skan TREE_TYPE (TREE_TYPE (clone)), 3792132718Skan parmtypes); 379390075Sobrien if (exceptions) 379490075Sobrien TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), 379590075Sobrien exceptions); 3796169689Skan TREE_TYPE (clone) 3797132718Skan = cp_build_type_attribute_variant (TREE_TYPE (clone), 3798132718Skan TYPE_ATTRIBUTES (TREE_TYPE (fn))); 379990075Sobrien } 380090075Sobrien 380190075Sobrien /* Copy the function parameters. But, DECL_ARGUMENTS on a TEMPLATE_DECL 380290075Sobrien aren't function parameters; those are the template parameters. */ 380390075Sobrien if (TREE_CODE (clone) != TEMPLATE_DECL) 380490075Sobrien { 380590075Sobrien DECL_ARGUMENTS (clone) = copy_list (DECL_ARGUMENTS (clone)); 380690075Sobrien /* Remove the in-charge parameter. */ 380790075Sobrien if (DECL_HAS_IN_CHARGE_PARM_P (clone)) 380890075Sobrien { 380990075Sobrien TREE_CHAIN (DECL_ARGUMENTS (clone)) 381090075Sobrien = TREE_CHAIN (TREE_CHAIN (DECL_ARGUMENTS (clone))); 381190075Sobrien DECL_HAS_IN_CHARGE_PARM_P (clone) = 0; 381290075Sobrien } 381390075Sobrien /* And the VTT parm, in a complete [cd]tor. */ 381490075Sobrien if (DECL_HAS_VTT_PARM_P (fn)) 381590075Sobrien { 381690075Sobrien if (DECL_NEEDS_VTT_PARM_P (clone)) 381790075Sobrien DECL_HAS_VTT_PARM_P (clone) = 1; 381890075Sobrien else 381918334Speter { 382090075Sobrien TREE_CHAIN (DECL_ARGUMENTS (clone)) 382190075Sobrien = TREE_CHAIN (TREE_CHAIN (DECL_ARGUMENTS (clone))); 382290075Sobrien DECL_HAS_VTT_PARM_P (clone) = 0; 382318334Speter } 382418334Speter } 382590075Sobrien 382690075Sobrien for (parms = DECL_ARGUMENTS (clone); parms; parms = TREE_CHAIN (parms)) 382790075Sobrien { 382890075Sobrien DECL_CONTEXT (parms) = clone; 3829117395Skan cxx_dup_lang_specific_decl (parms); 383090075Sobrien } 383118334Speter } 383218334Speter 383390075Sobrien /* Create the RTL for this function. */ 383490075Sobrien SET_DECL_RTL (clone, NULL_RTX); 3835169689Skan rest_of_decl_compilation (clone, /*top_level=*/1, at_eof); 3836169689Skan 383790075Sobrien /* Make it easy to find the CLONE given the FN. */ 383890075Sobrien TREE_CHAIN (clone) = TREE_CHAIN (fn); 383990075Sobrien TREE_CHAIN (fn) = clone; 384018334Speter 384190075Sobrien /* If this is a template, handle the DECL_TEMPLATE_RESULT as well. */ 384290075Sobrien if (TREE_CODE (clone) == TEMPLATE_DECL) 384350397Sobrien { 384490075Sobrien tree result; 384590075Sobrien 3846169689Skan DECL_TEMPLATE_RESULT (clone) 384790075Sobrien = build_clone (DECL_TEMPLATE_RESULT (clone), name); 384890075Sobrien result = DECL_TEMPLATE_RESULT (clone); 384990075Sobrien DECL_TEMPLATE_INFO (result) = copy_node (DECL_TEMPLATE_INFO (result)); 385090075Sobrien DECL_TI_TEMPLATE (result) = clone; 385190075Sobrien } 3852169689Skan else if (pch_file) 3853169689Skan note_decl_for_pch (clone); 385490075Sobrien 385590075Sobrien return clone; 385690075Sobrien} 385790075Sobrien 385890075Sobrien/* Produce declarations for all appropriate clones of FN. If 3859117395Skan UPDATE_METHOD_VEC_P is nonzero, the clones are added to the 386090075Sobrien CLASTYPE_METHOD_VEC as well. */ 386190075Sobrien 386290075Sobrienvoid 3863132718Skanclone_function_decl (tree fn, int update_method_vec_p) 386490075Sobrien{ 386590075Sobrien tree clone; 386690075Sobrien 386790075Sobrien /* Avoid inappropriate cloning. */ 386890075Sobrien if (TREE_CHAIN (fn) 386990075Sobrien && DECL_CLONED_FUNCTION (TREE_CHAIN (fn))) 387090075Sobrien return; 387190075Sobrien 387290075Sobrien if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn)) 387390075Sobrien { 387490075Sobrien /* For each constructor, we need two variants: an in-charge version 387590075Sobrien and a not-in-charge version. */ 387690075Sobrien clone = build_clone (fn, complete_ctor_identifier); 387790075Sobrien if (update_method_vec_p) 3878169689Skan add_method (DECL_CONTEXT (clone), clone, NULL_TREE); 387990075Sobrien clone = build_clone (fn, base_ctor_identifier); 388090075Sobrien if (update_method_vec_p) 3881169689Skan add_method (DECL_CONTEXT (clone), clone, NULL_TREE); 388290075Sobrien } 388390075Sobrien else 388490075Sobrien { 3885169689Skan gcc_assert (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn)); 388690075Sobrien 388790075Sobrien /* For each destructor, we need three variants: an in-charge 388890075Sobrien version, a not-in-charge version, and an in-charge deleting 388990075Sobrien version. We clone the deleting version first because that 389090075Sobrien means it will go second on the TYPE_METHODS list -- and that 389190075Sobrien corresponds to the correct layout order in the virtual 3892169689Skan function table. 389390075Sobrien 3894169689Skan For a non-virtual destructor, we do not build a deleting 389590075Sobrien destructor. */ 389690075Sobrien if (DECL_VIRTUAL_P (fn)) 389790075Sobrien { 389890075Sobrien clone = build_clone (fn, deleting_dtor_identifier); 389990075Sobrien if (update_method_vec_p) 3900169689Skan add_method (DECL_CONTEXT (clone), clone, NULL_TREE); 390190075Sobrien } 390290075Sobrien clone = build_clone (fn, complete_dtor_identifier); 390390075Sobrien if (update_method_vec_p) 3904169689Skan add_method (DECL_CONTEXT (clone), clone, NULL_TREE); 390590075Sobrien clone = build_clone (fn, base_dtor_identifier); 390690075Sobrien if (update_method_vec_p) 3907169689Skan add_method (DECL_CONTEXT (clone), clone, NULL_TREE); 390890075Sobrien } 390990075Sobrien 391090075Sobrien /* Note that this is an abstract function that is never emitted. */ 391190075Sobrien DECL_ABSTRACT (fn) = 1; 391290075Sobrien} 391390075Sobrien 391490075Sobrien/* DECL is an in charge constructor, which is being defined. This will 391590075Sobrien have had an in class declaration, from whence clones were 391690075Sobrien declared. An out-of-class definition can specify additional default 391790075Sobrien arguments. As it is the clones that are involved in overload 391890075Sobrien resolution, we must propagate the information from the DECL to its 3919117395Skan clones. */ 392090075Sobrien 392190075Sobrienvoid 3922132718Skanadjust_clone_args (tree decl) 392390075Sobrien{ 392490075Sobrien tree clone; 3925169689Skan 392690075Sobrien for (clone = TREE_CHAIN (decl); clone && DECL_CLONED_FUNCTION (clone); 392790075Sobrien clone = TREE_CHAIN (clone)) 392890075Sobrien { 392990075Sobrien tree orig_clone_parms = TYPE_ARG_TYPES (TREE_TYPE (clone)); 393090075Sobrien tree orig_decl_parms = TYPE_ARG_TYPES (TREE_TYPE (decl)); 393190075Sobrien tree decl_parms, clone_parms; 393290075Sobrien 393390075Sobrien clone_parms = orig_clone_parms; 3934169689Skan 3935117395Skan /* Skip the 'this' parameter. */ 393690075Sobrien orig_clone_parms = TREE_CHAIN (orig_clone_parms); 393790075Sobrien orig_decl_parms = TREE_CHAIN (orig_decl_parms); 393890075Sobrien 393990075Sobrien if (DECL_HAS_IN_CHARGE_PARM_P (decl)) 394090075Sobrien orig_decl_parms = TREE_CHAIN (orig_decl_parms); 394190075Sobrien if (DECL_HAS_VTT_PARM_P (decl)) 394290075Sobrien orig_decl_parms = TREE_CHAIN (orig_decl_parms); 3943169689Skan 394490075Sobrien clone_parms = orig_clone_parms; 394590075Sobrien if (DECL_HAS_VTT_PARM_P (clone)) 394690075Sobrien clone_parms = TREE_CHAIN (clone_parms); 3947169689Skan 394890075Sobrien for (decl_parms = orig_decl_parms; decl_parms; 394990075Sobrien decl_parms = TREE_CHAIN (decl_parms), 395090075Sobrien clone_parms = TREE_CHAIN (clone_parms)) 395150397Sobrien { 3952169689Skan gcc_assert (same_type_p (TREE_TYPE (decl_parms), 3953169689Skan TREE_TYPE (clone_parms))); 3954169689Skan 395590075Sobrien if (TREE_PURPOSE (decl_parms) && !TREE_PURPOSE (clone_parms)) 395690075Sobrien { 395790075Sobrien /* A default parameter has been added. Adjust the 3958117395Skan clone's parameters. */ 395990075Sobrien tree exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone)); 396090075Sobrien tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone)); 396190075Sobrien tree type; 396290075Sobrien 396390075Sobrien clone_parms = orig_decl_parms; 396490075Sobrien 396590075Sobrien if (DECL_HAS_VTT_PARM_P (clone)) 396690075Sobrien { 396790075Sobrien clone_parms = tree_cons (TREE_PURPOSE (orig_clone_parms), 396890075Sobrien TREE_VALUE (orig_clone_parms), 396990075Sobrien clone_parms); 397090075Sobrien TREE_TYPE (clone_parms) = TREE_TYPE (orig_clone_parms); 397190075Sobrien } 3972132718Skan type = build_method_type_directly (basetype, 3973132718Skan TREE_TYPE (TREE_TYPE (clone)), 3974132718Skan clone_parms); 397590075Sobrien if (exceptions) 397690075Sobrien type = build_exception_variant (type, exceptions); 397790075Sobrien TREE_TYPE (clone) = type; 3978169689Skan 397990075Sobrien clone_parms = NULL_TREE; 398090075Sobrien break; 398190075Sobrien } 398250397Sobrien } 3983169689Skan gcc_assert (!clone_parms); 398450397Sobrien } 398590075Sobrien} 398690075Sobrien 398790075Sobrien/* For each of the constructors and destructors in T, create an 398890075Sobrien in-charge and not-in-charge variant. */ 398990075Sobrien 399090075Sobrienstatic void 3991132718Skanclone_constructors_and_destructors (tree t) 399290075Sobrien{ 399390075Sobrien tree fns; 399490075Sobrien 399590075Sobrien /* If for some reason we don't have a CLASSTYPE_METHOD_VEC, we bail 399690075Sobrien out now. */ 399790075Sobrien if (!CLASSTYPE_METHOD_VEC (t)) 399890075Sobrien return; 399990075Sobrien 400090075Sobrien for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns)) 400190075Sobrien clone_function_decl (OVL_CURRENT (fns), /*update_method_vec_p=*/1); 400290075Sobrien for (fns = CLASSTYPE_DESTRUCTORS (t); fns; fns = OVL_NEXT (fns)) 400390075Sobrien clone_function_decl (OVL_CURRENT (fns), /*update_method_vec_p=*/1); 400490075Sobrien} 400590075Sobrien 400690075Sobrien/* Remove all zero-width bit-fields from T. */ 400790075Sobrien 400890075Sobrienstatic void 4009132718Skanremove_zero_width_bit_fields (tree t) 401090075Sobrien{ 401190075Sobrien tree *fieldsp; 401290075Sobrien 4013169689Skan fieldsp = &TYPE_FIELDS (t); 401490075Sobrien while (*fieldsp) 401590075Sobrien { 401690075Sobrien if (TREE_CODE (*fieldsp) == FIELD_DECL 4017169689Skan && DECL_C_BIT_FIELD (*fieldsp) 401890075Sobrien && DECL_INITIAL (*fieldsp)) 401990075Sobrien *fieldsp = TREE_CHAIN (*fieldsp); 402090075Sobrien else 402190075Sobrien fieldsp = &TREE_CHAIN (*fieldsp); 402290075Sobrien } 402390075Sobrien} 402490075Sobrien 402590075Sobrien/* Returns TRUE iff we need a cookie when dynamically allocating an 402690075Sobrien array whose elements have the indicated class TYPE. */ 402790075Sobrien 402890075Sobrienstatic bool 4029132718Skantype_requires_array_cookie (tree type) 403090075Sobrien{ 403190075Sobrien tree fns; 403290075Sobrien bool has_two_argument_delete_p = false; 403390075Sobrien 4034169689Skan gcc_assert (CLASS_TYPE_P (type)); 403590075Sobrien 403690075Sobrien /* If there's a non-trivial destructor, we need a cookie. In order 403790075Sobrien to iterate through the array calling the destructor for each 403890075Sobrien element, we'll have to know how many elements there are. */ 403990075Sobrien if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)) 404090075Sobrien return true; 404190075Sobrien 404290075Sobrien /* If the usual deallocation function is a two-argument whose second 404390075Sobrien argument is of type `size_t', then we have to pass the size of 404490075Sobrien the array to the deallocation function, so we will need to store 404590075Sobrien a cookie. */ 4046169689Skan fns = lookup_fnfields (TYPE_BINFO (type), 404790075Sobrien ansi_opname (VEC_DELETE_EXPR), 404890075Sobrien /*protect=*/0); 404990075Sobrien /* If there are no `operator []' members, or the lookup is 405090075Sobrien ambiguous, then we don't need a cookie. */ 405190075Sobrien if (!fns || fns == error_mark_node) 405290075Sobrien return false; 405390075Sobrien /* Loop through all of the functions. */ 4054117395Skan for (fns = BASELINK_FUNCTIONS (fns); fns; fns = OVL_NEXT (fns)) 405590075Sobrien { 405690075Sobrien tree fn; 405790075Sobrien tree second_parm; 405890075Sobrien 405990075Sobrien /* Select the current function. */ 406090075Sobrien fn = OVL_CURRENT (fns); 406190075Sobrien /* See if this function is a one-argument delete function. If 406290075Sobrien it is, then it will be the usual deallocation function. */ 406390075Sobrien second_parm = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fn))); 406490075Sobrien if (second_parm == void_list_node) 406590075Sobrien return false; 406690075Sobrien /* Otherwise, if we have a two-argument function and the second 406790075Sobrien argument is `size_t', it will be the usual deallocation 406890075Sobrien function -- unless there is one-argument function, too. */ 406990075Sobrien if (TREE_CHAIN (second_parm) == void_list_node 407090075Sobrien && same_type_p (TREE_VALUE (second_parm), sizetype)) 407190075Sobrien has_two_argument_delete_p = true; 407290075Sobrien } 407390075Sobrien 407490075Sobrien return has_two_argument_delete_p; 407590075Sobrien} 407690075Sobrien 407790075Sobrien/* Check the validity of the bases and members declared in T. Add any 407890075Sobrien implicitly-generated functions (like copy-constructors and 407990075Sobrien assignment operators). Compute various flag bits (like 408090075Sobrien CLASSTYPE_NON_POD_T) for T. This routine works purely at the C++ 408190075Sobrien level: i.e., independently of the ABI in use. */ 408290075Sobrien 408390075Sobrienstatic void 4084117395Skancheck_bases_and_members (tree t) 408590075Sobrien{ 408690075Sobrien /* Nonzero if the implicitly generated copy constructor should take 408790075Sobrien a non-const reference argument. */ 408890075Sobrien int cant_have_const_ctor; 4089169689Skan /* Nonzero if the implicitly generated assignment operator 409090075Sobrien should take a non-const reference argument. */ 409190075Sobrien int no_const_asn_ref; 409290075Sobrien tree access_decls; 409390075Sobrien 409490075Sobrien /* By default, we use const reference arguments and generate default 409590075Sobrien constructors. */ 409690075Sobrien cant_have_const_ctor = 0; 409790075Sobrien no_const_asn_ref = 0; 409890075Sobrien 4099117395Skan /* Check all the base-classes. */ 4100169689Skan check_bases (t, &cant_have_const_ctor, 410190075Sobrien &no_const_asn_ref); 410290075Sobrien 4103169689Skan /* Check all the method declarations. */ 4104169689Skan check_methods (t); 4105169689Skan 4106169689Skan /* Check all the data member declarations. We cannot call 4107169689Skan check_field_decls until we have called check_bases check_methods, 4108169689Skan as check_field_decls depends on TYPE_HAS_NONTRIVIAL_DESTRUCTOR 4109169689Skan being set appropriately. */ 4110117395Skan check_field_decls (t, &access_decls, 411190075Sobrien &cant_have_const_ctor, 411290075Sobrien &no_const_asn_ref); 411390075Sobrien 411490075Sobrien /* A nearly-empty class has to be vptr-containing; a nearly empty 411590075Sobrien class contains just a vptr. */ 411690075Sobrien if (!TYPE_CONTAINS_VPTR_P (t)) 411790075Sobrien CLASSTYPE_NEARLY_EMPTY_P (t) = 0; 411890075Sobrien 411952284Sobrien /* Do some bookkeeping that will guide the generation of implicitly 412052284Sobrien declared member functions. */ 412118334Speter TYPE_HAS_COMPLEX_INIT_REF (t) 4122169689Skan |= (TYPE_HAS_INIT_REF (t) || TYPE_CONTAINS_VPTR_P (t)); 412318334Speter TYPE_NEEDS_CONSTRUCTING (t) 4124169689Skan |= (TYPE_HAS_CONSTRUCTOR (t) || TYPE_CONTAINS_VPTR_P (t)); 4125169689Skan CLASSTYPE_NON_AGGREGATE (t) 4126169689Skan |= (TYPE_HAS_CONSTRUCTOR (t) || TYPE_POLYMORPHIC_P (t)); 412790075Sobrien CLASSTYPE_NON_POD_P (t) 4128169689Skan |= (CLASSTYPE_NON_AGGREGATE (t) 4129169689Skan || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) 413090075Sobrien || TYPE_HAS_ASSIGN_REF (t)); 413118334Speter TYPE_HAS_COMPLEX_ASSIGN_REF (t) 413290075Sobrien |= TYPE_HAS_ASSIGN_REF (t) || TYPE_CONTAINS_VPTR_P (t); 413318334Speter 4134169689Skan /* Synthesize any needed methods. */ 4135169689Skan add_implicitly_declared_members (t, 413690075Sobrien cant_have_const_ctor, 413790075Sobrien no_const_asn_ref); 413818334Speter 413990075Sobrien /* Create the in-charge and not-in-charge variants of constructors 414090075Sobrien and destructors. */ 414190075Sobrien clone_constructors_and_destructors (t); 414218334Speter 414390075Sobrien /* Process the using-declarations. */ 414490075Sobrien for (; access_decls; access_decls = TREE_CHAIN (access_decls)) 414590075Sobrien handle_using_decl (TREE_VALUE (access_decls), t); 414618334Speter 414790075Sobrien /* Build and sort the CLASSTYPE_METHOD_VEC. */ 414890075Sobrien finish_struct_methods (t); 414918334Speter 415090075Sobrien /* Figure out whether or not we will need a cookie when dynamically 415190075Sobrien allocating an array of this type. */ 4152117395Skan TYPE_LANG_SPECIFIC (t)->u.c.vec_new_uses_cookie 415390075Sobrien = type_requires_array_cookie (t); 415490075Sobrien} 415590075Sobrien 415690075Sobrien/* If T needs a pointer to its virtual function table, set TYPE_VFIELD 415790075Sobrien accordingly. If a new vfield was created (because T doesn't have a 415890075Sobrien primary base class), then the newly created field is returned. It 415990075Sobrien is not added to the TYPE_FIELDS list; it is the caller's 4160102780Skan responsibility to do that. Accumulate declared virtual functions 4161102780Skan on VIRTUALS_P. */ 416290075Sobrien 416390075Sobrienstatic tree 4164132718Skancreate_vtable_ptr (tree t, tree* virtuals_p) 416590075Sobrien{ 416690075Sobrien tree fn; 416790075Sobrien 4168102780Skan /* Collect the virtual functions declared in T. */ 416990075Sobrien for (fn = TYPE_METHODS (t); fn; fn = TREE_CHAIN (fn)) 4170102780Skan if (DECL_VINDEX (fn) && !DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn) 4171102780Skan && TREE_CODE (DECL_VINDEX (fn)) != INTEGER_CST) 4172102780Skan { 4173102780Skan tree new_virtual = make_node (TREE_LIST); 4174169689Skan 4175102780Skan BV_FN (new_virtual) = fn; 4176102780Skan BV_DELTA (new_virtual) = integer_zero_node; 4177169689Skan BV_VCALL_INDEX (new_virtual) = NULL_TREE; 417890075Sobrien 4179102780Skan TREE_CHAIN (new_virtual) = *virtuals_p; 4180102780Skan *virtuals_p = new_virtual; 4181102780Skan } 4182169689Skan 418390075Sobrien /* If we couldn't find an appropriate base class, create a new field 418490075Sobrien here. Even if there weren't any new virtual functions, we might need a 418590075Sobrien new virtual function table if we're supposed to include vptrs in 418690075Sobrien all classes that need them. */ 4187102780Skan if (!TYPE_VFIELD (t) && (*virtuals_p || TYPE_CONTAINS_VPTR_P (t))) 418818334Speter { 418952284Sobrien /* We build this decl with vtbl_ptr_type_node, which is a 419052284Sobrien `vtable_entry_type*'. It might seem more precise to use 4191169689Skan `vtable_entry_type (*)[N]' where N is the number of virtual 419252284Sobrien functions. However, that would require the vtable pointer in 419352284Sobrien base classes to have a different type than the vtable pointer 419452284Sobrien in derived classes. We could make that happen, but that 419552284Sobrien still wouldn't solve all the problems. In particular, the 419652284Sobrien type-based alias analysis code would decide that assignments 419752284Sobrien to the base class vtable pointer can't alias assignments to 419852284Sobrien the derived class vtable pointer, since they have different 4199132718Skan types. Thus, in a derived class destructor, where the base 420052284Sobrien class constructor was inlined, we could generate bad code for 4201169689Skan setting up the vtable pointer. 420252284Sobrien 4203169689Skan Therefore, we use one type for all vtable pointers. We still 420452284Sobrien use a type-correct type; it's just doesn't indicate the array 420552284Sobrien bounds. That's better than using `void*' or some such; it's 420652284Sobrien cleaner, and it let's the alias analysis code know that these 420752284Sobrien stores cannot alias stores to void*! */ 420890075Sobrien tree field; 420990075Sobrien 421090075Sobrien field = build_decl (FIELD_DECL, get_vfield_name (t), vtbl_ptr_type_node); 421190075Sobrien DECL_VIRTUAL_P (field) = 1; 421290075Sobrien DECL_ARTIFICIAL (field) = 1; 421390075Sobrien DECL_FIELD_CONTEXT (field) = t; 421490075Sobrien DECL_FCONTEXT (field) = t; 4215169689Skan 421690075Sobrien TYPE_VFIELD (t) = field; 4217169689Skan 421890075Sobrien /* This class is non-empty. */ 4219117395Skan CLASSTYPE_EMPTY_P (t) = 0; 422090075Sobrien 422190075Sobrien return field; 422290075Sobrien } 422390075Sobrien 422490075Sobrien return NULL_TREE; 422590075Sobrien} 422690075Sobrien 422790075Sobrien/* Fixup the inline function given by INFO now that the class is 422890075Sobrien complete. */ 422990075Sobrien 423090075Sobrienstatic void 4231132718Skanfixup_pending_inline (tree fn) 423290075Sobrien{ 423390075Sobrien if (DECL_PENDING_INLINE_INFO (fn)) 423490075Sobrien { 423590075Sobrien tree args = DECL_ARGUMENTS (fn); 423690075Sobrien while (args) 423718334Speter { 423890075Sobrien DECL_CONTEXT (args) = fn; 423990075Sobrien args = TREE_CHAIN (args); 424018334Speter } 424190075Sobrien } 424290075Sobrien} 424390075Sobrien 424490075Sobrien/* Fixup the inline methods and friends in TYPE now that TYPE is 424590075Sobrien complete. */ 424690075Sobrien 424790075Sobrienstatic void 4248132718Skanfixup_inline_methods (tree type) 424990075Sobrien{ 425090075Sobrien tree method = TYPE_METHODS (type); 4251169689Skan VEC(tree,gc) *friends; 4252169689Skan unsigned ix; 425390075Sobrien 425490075Sobrien if (method && TREE_CODE (method) == TREE_VEC) 425590075Sobrien { 425690075Sobrien if (TREE_VEC_ELT (method, 1)) 425790075Sobrien method = TREE_VEC_ELT (method, 1); 425890075Sobrien else if (TREE_VEC_ELT (method, 0)) 425990075Sobrien method = TREE_VEC_ELT (method, 0); 426018334Speter else 426190075Sobrien method = TREE_VEC_ELT (method, 2); 426218334Speter } 426318334Speter 426490075Sobrien /* Do inline member functions. */ 426590075Sobrien for (; method; method = TREE_CHAIN (method)) 426690075Sobrien fixup_pending_inline (method); 426718334Speter 426890075Sobrien /* Do friends. */ 4269169689Skan for (friends = CLASSTYPE_INLINE_FRIENDS (type), ix = 0; 4270169689Skan VEC_iterate (tree, friends, ix, method); ix++) 4271169689Skan fixup_pending_inline (method); 4272169689Skan CLASSTYPE_INLINE_FRIENDS (type) = NULL; 427390075Sobrien} 427418334Speter 427590075Sobrien/* Add OFFSET to all base types of BINFO which is a base in the 427690075Sobrien hierarchy dominated by T. 427718334Speter 427890075Sobrien OFFSET, which is a type offset, is number of bytes. */ 427918334Speter 428090075Sobrienstatic void 4281132718Skanpropagate_binfo_offsets (tree binfo, tree offset) 428290075Sobrien{ 428390075Sobrien int i; 428490075Sobrien tree primary_binfo; 4285169689Skan tree base_binfo; 428618334Speter 428790075Sobrien /* Update BINFO's offset. */ 428890075Sobrien BINFO_OFFSET (binfo) 4289169689Skan = convert (sizetype, 429090075Sobrien size_binop (PLUS_EXPR, 429190075Sobrien convert (ssizetype, BINFO_OFFSET (binfo)), 429290075Sobrien offset)); 429390075Sobrien 429490075Sobrien /* Find the primary base class. */ 429590075Sobrien primary_binfo = get_primary_binfo (binfo); 429690075Sobrien 4297169689Skan if (primary_binfo && BINFO_INHERITANCE_CHAIN (primary_binfo) == binfo) 4298169689Skan propagate_binfo_offsets (primary_binfo, offset); 4299169689Skan 430090075Sobrien /* Scan all of the bases, pushing the BINFO_OFFSET adjust 430190075Sobrien downwards. */ 4302169689Skan for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i) 430318334Speter { 4304169689Skan /* Don't do the primary base twice. */ 4305169689Skan if (base_binfo == primary_binfo) 4306169689Skan continue; 430718334Speter 4308169689Skan if (BINFO_VIRTUAL_P (base_binfo)) 430990075Sobrien continue; 431090075Sobrien 4311132718Skan propagate_binfo_offsets (base_binfo, offset); 431218334Speter } 431390075Sobrien} 431452284Sobrien 4315117395Skan/* Set BINFO_OFFSET for all of the virtual bases for RLI->T. Update 431690075Sobrien TYPE_ALIGN and TYPE_SIZE for T. OFFSETS gives the location of 431790075Sobrien empty subobjects of T. */ 431818334Speter 431990075Sobrienstatic void 4320117395Skanlayout_virtual_bases (record_layout_info rli, splay_tree offsets) 432190075Sobrien{ 4322132718Skan tree vbase; 4323117395Skan tree t = rli->t; 4324102780Skan bool first_vbase = true; 4325117395Skan tree *next_field; 432618334Speter 4327169689Skan if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) == 0) 432890075Sobrien return; 432918334Speter 4330117395Skan if (!abi_version_at_least(2)) 4331117395Skan { 4332117395Skan /* In G++ 3.2, we incorrectly rounded the size before laying out 4333117395Skan the virtual bases. */ 4334117395Skan finish_record_layout (rli, /*free_p=*/false); 433590075Sobrien#ifdef STRUCTURE_SIZE_BOUNDARY 4336117395Skan /* Packed structures don't need to have minimum size. */ 4337117395Skan if (! TYPE_PACKED (t)) 4338132718Skan TYPE_ALIGN (t) = MAX (TYPE_ALIGN (t), (unsigned) STRUCTURE_SIZE_BOUNDARY); 433990075Sobrien#endif 4340117395Skan rli->offset = TYPE_SIZE_UNIT (t); 4341117395Skan rli->bitpos = bitsize_zero_node; 4342117395Skan rli->record_align = TYPE_ALIGN (t); 4343117395Skan } 434418334Speter 4345117395Skan /* Find the last field. The artificial fields created for virtual 4346117395Skan bases will go after the last extant field to date. */ 4347117395Skan next_field = &TYPE_FIELDS (t); 4348117395Skan while (*next_field) 4349117395Skan next_field = &TREE_CHAIN (*next_field); 435018334Speter 435190075Sobrien /* Go through the virtual bases, allocating space for each virtual 435290075Sobrien base that is not already a primary base class. These are 435390075Sobrien allocated in inheritance graph order. */ 4354132718Skan for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase)) 435590075Sobrien { 4356169689Skan if (!BINFO_VIRTUAL_P (vbase)) 435790075Sobrien continue; 4358102780Skan 435990075Sobrien if (!BINFO_PRIMARY_P (vbase)) 436090075Sobrien { 4361117395Skan tree basetype = TREE_TYPE (vbase); 4362117395Skan 436390075Sobrien /* This virtual base is not a primary base of any class in the 436490075Sobrien hierarchy, so we have to add space for it. */ 4365117395Skan next_field = build_base_field (rli, vbase, 4366117395Skan offsets, next_field); 436752284Sobrien 4368102780Skan /* If the first virtual base might have been placed at a 4369102780Skan lower address, had we started from CLASSTYPE_SIZE, rather 4370102780Skan than TYPE_SIZE, issue a warning. There can be both false 4371102780Skan positives and false negatives from this warning in rare 4372102780Skan cases; to deal with all the possibilities would probably 4373102780Skan require performing both layout algorithms and comparing 4374102780Skan the results which is not particularly tractable. */ 4375102780Skan if (warn_abi 4376102780Skan && first_vbase 4377169689Skan && (tree_int_cst_lt 4378117395Skan (size_binop (CEIL_DIV_EXPR, 4379117395Skan round_up (CLASSTYPE_SIZE (t), 4380117395Skan CLASSTYPE_ALIGN (basetype)), 4381117395Skan bitsize_unit_node), 4382117395Skan BINFO_OFFSET (vbase)))) 4383169689Skan warning (OPT_Wabi, 4384169689Skan "offset of virtual base %qT is not ABI-compliant and " 4385169689Skan "may change in a future version of GCC", 4386102780Skan basetype); 4387102780Skan 4388102780Skan first_vbase = false; 438918334Speter } 439018334Speter } 4391117395Skan} 439290075Sobrien 4393117395Skan/* Returns the offset of the byte just past the end of the base class 4394117395Skan BINFO. */ 439590075Sobrien 4396117395Skanstatic tree 4397117395Skanend_of_base (tree binfo) 4398117395Skan{ 4399117395Skan tree size; 440090075Sobrien 4401117395Skan if (is_empty_class (BINFO_TYPE (binfo))) 4402117395Skan /* An empty class has zero CLASSTYPE_SIZE_UNIT, but we need to 4403117395Skan allocate some space for it. It cannot have virtual bases, so 4404117395Skan TYPE_SIZE_UNIT is fine. */ 4405117395Skan size = TYPE_SIZE_UNIT (BINFO_TYPE (binfo)); 4406117395Skan else 4407117395Skan size = CLASSTYPE_SIZE_UNIT (BINFO_TYPE (binfo)); 4408117395Skan 4409117395Skan return size_binop (PLUS_EXPR, BINFO_OFFSET (binfo), size); 441090075Sobrien} 441190075Sobrien 441290075Sobrien/* Returns the offset of the byte just past the end of the base class 441390075Sobrien with the highest offset in T. If INCLUDE_VIRTUALS_P is zero, then 441490075Sobrien only non-virtual bases are included. */ 441590075Sobrien 4416117395Skanstatic tree 4417132718Skanend_of_class (tree t, int include_virtuals_p) 441890075Sobrien{ 4419117395Skan tree result = size_zero_node; 4420169689Skan VEC(tree,gc) *vbases; 4421117395Skan tree binfo; 4422169689Skan tree base_binfo; 4423117395Skan tree offset; 442490075Sobrien int i; 442590075Sobrien 4426169689Skan for (binfo = TYPE_BINFO (t), i = 0; 4427169689Skan BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i) 442818334Speter { 442990075Sobrien if (!include_virtuals_p 4430169689Skan && BINFO_VIRTUAL_P (base_binfo) 4431169689Skan && (!BINFO_PRIMARY_P (base_binfo) 4432169689Skan || BINFO_INHERITANCE_CHAIN (base_binfo) != TYPE_BINFO (t))) 443390075Sobrien continue; 443490075Sobrien 4435169689Skan offset = end_of_base (base_binfo); 4436117395Skan if (INT_CST_LT_UNSIGNED (result, offset)) 4437117395Skan result = offset; 443818334Speter } 443918334Speter 4440117395Skan /* G++ 3.2 did not check indirect virtual bases. */ 4441117395Skan if (abi_version_at_least (2) && include_virtuals_p) 4442169689Skan for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0; 4443169689Skan VEC_iterate (tree, vbases, i, base_binfo); i++) 4444117395Skan { 4445169689Skan offset = end_of_base (base_binfo); 4446117395Skan if (INT_CST_LT_UNSIGNED (result, offset)) 4447117395Skan result = offset; 4448117395Skan } 4449117395Skan 445090075Sobrien return result; 445190075Sobrien} 445218334Speter 4453117395Skan/* Warn about bases of T that are inaccessible because they are 445490075Sobrien ambiguous. For example: 445518334Speter 445690075Sobrien struct S {}; 445790075Sobrien struct T : public S {}; 445890075Sobrien struct U : public S, public T {}; 445990075Sobrien 446090075Sobrien Here, `(S*) new U' is not allowed because there are two `S' 446190075Sobrien subobjects of U. */ 446290075Sobrien 446390075Sobrienstatic void 4464132718Skanwarn_about_ambiguous_bases (tree t) 446590075Sobrien{ 446690075Sobrien int i; 4467169689Skan VEC(tree,gc) *vbases; 4468117395Skan tree basetype; 4469169689Skan tree binfo; 4470169689Skan tree base_binfo; 447190075Sobrien 4472169689Skan /* If there are no repeated bases, nothing can be ambiguous. */ 4473169689Skan if (!CLASSTYPE_REPEATED_BASE_P (t)) 4474169689Skan return; 4475169689Skan 4476117395Skan /* Check direct bases. */ 4477169689Skan for (binfo = TYPE_BINFO (t), i = 0; 4478169689Skan BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i) 447918334Speter { 4480169689Skan basetype = BINFO_TYPE (base_binfo); 448190075Sobrien 4482169689Skan if (!lookup_base (t, basetype, ba_unique | ba_quiet, NULL)) 4483169689Skan warning (0, "direct base %qT inaccessible in %qT due to ambiguity", 4484117395Skan basetype, t); 448518334Speter } 4486117395Skan 4487117395Skan /* Check for ambiguous virtual bases. */ 4488117395Skan if (extra_warnings) 4489169689Skan for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0; 4490169689Skan VEC_iterate (tree, vbases, i, binfo); i++) 4491117395Skan { 4492169689Skan basetype = BINFO_TYPE (binfo); 4493169689Skan 4494169689Skan if (!lookup_base (t, basetype, ba_unique | ba_quiet, NULL)) 4495169689Skan warning (OPT_Wextra, "virtual base %qT inaccessible in %qT due to ambiguity", 4496117395Skan basetype, t); 4497117395Skan } 449890075Sobrien} 449990075Sobrien 450090075Sobrien/* Compare two INTEGER_CSTs K1 and K2. */ 450190075Sobrien 450290075Sobrienstatic int 4503132718Skansplay_tree_compare_integer_csts (splay_tree_key k1, splay_tree_key k2) 450490075Sobrien{ 450590075Sobrien return tree_int_cst_compare ((tree) k1, (tree) k2); 450690075Sobrien} 450790075Sobrien 4508117395Skan/* Increase the size indicated in RLI to account for empty classes 4509117395Skan that are "off the end" of the class. */ 4510117395Skan 4511117395Skanstatic void 4512117395Skaninclude_empty_classes (record_layout_info rli) 4513117395Skan{ 4514117395Skan tree eoc; 4515117395Skan tree rli_size; 4516117395Skan 4517117395Skan /* It might be the case that we grew the class to allocate a 4518117395Skan zero-sized base class. That won't be reflected in RLI, yet, 4519117395Skan because we are willing to overlay multiple bases at the same 4520117395Skan offset. However, now we need to make sure that RLI is big enough 4521117395Skan to reflect the entire class. */ 4522169689Skan eoc = end_of_class (rli->t, 4523117395Skan CLASSTYPE_AS_BASE (rli->t) != NULL_TREE); 4524117395Skan rli_size = rli_size_unit_so_far (rli); 4525117395Skan if (TREE_CODE (rli_size) == INTEGER_CST 4526117395Skan && INT_CST_LT_UNSIGNED (rli_size, eoc)) 4527117395Skan { 4528132718Skan if (!abi_version_at_least (2)) 4529132718Skan /* In version 1 of the ABI, the size of a class that ends with 4530132718Skan a bitfield was not rounded up to a whole multiple of a 4531132718Skan byte. Because rli_size_unit_so_far returns only the number 4532132718Skan of fully allocated bytes, any extra bits were not included 4533132718Skan in the size. */ 4534132718Skan rli->bitpos = round_down (rli->bitpos, BITS_PER_UNIT); 4535132718Skan else 4536132718Skan /* The size should have been rounded to a whole byte. */ 4537169689Skan gcc_assert (tree_int_cst_equal 4538169689Skan (rli->bitpos, round_down (rli->bitpos, BITS_PER_UNIT))); 4539169689Skan rli->bitpos 4540169689Skan = size_binop (PLUS_EXPR, 4541117395Skan rli->bitpos, 4542117395Skan size_binop (MULT_EXPR, 4543117395Skan convert (bitsizetype, 4544117395Skan size_binop (MINUS_EXPR, 4545117395Skan eoc, rli_size)), 4546117395Skan bitsize_int (BITS_PER_UNIT))); 4547117395Skan normalize_rli (rli); 4548117395Skan } 4549117395Skan} 4550117395Skan 455190075Sobrien/* Calculate the TYPE_SIZE, TYPE_ALIGN, etc for T. Calculate 455290075Sobrien BINFO_OFFSETs for all of the base-classes. Position the vtable 4553117395Skan pointer. Accumulate declared virtual functions on VIRTUALS_P. */ 455490075Sobrien 455590075Sobrienstatic void 4556117395Skanlayout_class_type (tree t, tree *virtuals_p) 455790075Sobrien{ 455890075Sobrien tree non_static_data_members; 455990075Sobrien tree field; 456090075Sobrien tree vptr; 456190075Sobrien record_layout_info rli; 456290075Sobrien /* Maps offsets (represented as INTEGER_CSTs) to a TREE_LIST of 456390075Sobrien types that appear at that offset. */ 456490075Sobrien splay_tree empty_base_offsets; 4565102780Skan /* True if the last field layed out was a bit-field. */ 4566102780Skan bool last_field_was_bitfield = false; 4567117395Skan /* The location at which the next field should be inserted. */ 4568117395Skan tree *next_field; 4569117395Skan /* T, as a base class. */ 4570117395Skan tree base_t; 457190075Sobrien 457290075Sobrien /* Keep track of the first non-static data member. */ 457390075Sobrien non_static_data_members = TYPE_FIELDS (t); 457490075Sobrien 457590075Sobrien /* Start laying out the record. */ 457690075Sobrien rli = start_record_layout (t); 457790075Sobrien 4578169689Skan /* Mark all the primary bases in the hierarchy. */ 4579169689Skan determine_primary_bases (t); 458090075Sobrien 458190075Sobrien /* Create a pointer to our virtual function table. */ 4582117395Skan vptr = create_vtable_ptr (t, virtuals_p); 458390075Sobrien 458490075Sobrien /* The vptr is always the first thing in the class. */ 458590075Sobrien if (vptr) 458690075Sobrien { 4587117395Skan TREE_CHAIN (vptr) = TYPE_FIELDS (t); 4588117395Skan TYPE_FIELDS (t) = vptr; 4589117395Skan next_field = &TREE_CHAIN (vptr); 459090075Sobrien place_field (rli, vptr); 459190075Sobrien } 4592117395Skan else 4593117395Skan next_field = &TYPE_FIELDS (t); 459490075Sobrien 459590075Sobrien /* Build FIELD_DECLs for all of the non-virtual base-types. */ 4596169689Skan empty_base_offsets = splay_tree_new (splay_tree_compare_integer_csts, 459790075Sobrien NULL, NULL); 4598117395Skan build_base_fields (rli, empty_base_offsets, next_field); 4599169689Skan 460090075Sobrien /* Layout the non-static data members. */ 460190075Sobrien for (field = non_static_data_members; field; field = TREE_CHAIN (field)) 460250397Sobrien { 460390075Sobrien tree type; 460490075Sobrien tree padding; 460590075Sobrien 460690075Sobrien /* We still pass things that aren't non-static data members to 460790075Sobrien the back-end, in case it wants to do something with them. */ 460890075Sobrien if (TREE_CODE (field) != FIELD_DECL) 460950397Sobrien { 461090075Sobrien place_field (rli, field); 461196263Sobrien /* If the static data member has incomplete type, keep track 4612169689Skan of it so that it can be completed later. (The handling 461396263Sobrien of pending statics in finish_record_layout is 461496263Sobrien insufficient; consider: 461596263Sobrien 461696263Sobrien struct S1; 461796263Sobrien struct S2 { static S1 s1; }; 4618169689Skan 4619169689Skan At this point, finish_record_layout will be called, but 462096263Sobrien S1 is still incomplete.) */ 462196263Sobrien if (TREE_CODE (field) == VAR_DECL) 4622169689Skan { 4623169689Skan maybe_register_incomplete_var (field); 4624169689Skan /* The visibility of static data members is determined 4625169689Skan at their point of declaration, not their point of 4626169689Skan definition. */ 4627169689Skan determine_visibility (field); 4628169689Skan } 462990075Sobrien continue; 463050397Sobrien } 463118334Speter 463290075Sobrien type = TREE_TYPE (field); 4633169689Skan if (type == error_mark_node) 4634169689Skan continue; 4635169689Skan 4636132718Skan padding = NULL_TREE; 463790075Sobrien 463890075Sobrien /* If this field is a bit-field whose width is greater than its 463990075Sobrien type, then there are some special rules for allocating 464090075Sobrien it. */ 464190075Sobrien if (DECL_C_BIT_FIELD (field) 464290075Sobrien && INT_CST_LT (TYPE_SIZE (type), DECL_SIZE (field))) 464318334Speter { 464490075Sobrien integer_type_kind itk; 464590075Sobrien tree integer_type; 4646132718Skan bool was_unnamed_p = false; 464790075Sobrien /* We must allocate the bits as if suitably aligned for the 464890075Sobrien longest integer type that fits in this many bits. type 464990075Sobrien of the field. Then, we are supposed to use the left over 465090075Sobrien bits as additional padding. */ 465190075Sobrien for (itk = itk_char; itk != itk_none; ++itk) 4652169689Skan if (INT_CST_LT (DECL_SIZE (field), 465390075Sobrien TYPE_SIZE (integer_types[itk]))) 465490075Sobrien break; 465550397Sobrien 465690075Sobrien /* ITK now indicates a type that is too large for the 465790075Sobrien field. We have to back up by one to find the largest 465890075Sobrien type that fits. */ 465990075Sobrien integer_type = integer_types[itk - 1]; 4660117395Skan 4661132718Skan /* Figure out how much additional padding is required. GCC 4662132718Skan 3.2 always created a padding field, even if it had zero 4663132718Skan width. */ 4664132718Skan if (!abi_version_at_least (2) 4665132718Skan || INT_CST_LT (TYPE_SIZE (integer_type), DECL_SIZE (field))) 4666117395Skan { 4667132718Skan if (abi_version_at_least (2) && TREE_CODE (t) == UNION_TYPE) 4668132718Skan /* In a union, the padding field must have the full width 4669132718Skan of the bit-field; all fields start at offset zero. */ 4670132718Skan padding = DECL_SIZE (field); 4671132718Skan else 4672132718Skan { 4673169689Skan if (TREE_CODE (t) == UNION_TYPE) 4674169689Skan warning (OPT_Wabi, "size assigned to %qT may not be " 4675132718Skan "ABI-compliant and may change in a future " 4676169689Skan "version of GCC", 4677132718Skan t); 4678132718Skan padding = size_binop (MINUS_EXPR, DECL_SIZE (field), 4679132718Skan TYPE_SIZE (integer_type)); 4680132718Skan } 4681117395Skan } 4682132718Skan#ifdef PCC_BITFIELD_TYPE_MATTERS 4683132718Skan /* An unnamed bitfield does not normally affect the 4684132718Skan alignment of the containing class on a target where 4685132718Skan PCC_BITFIELD_TYPE_MATTERS. But, the C++ ABI does not 4686132718Skan make any exceptions for unnamed bitfields when the 4687132718Skan bitfields are longer than their types. Therefore, we 4688132718Skan temporarily give the field a name. */ 4689132718Skan if (PCC_BITFIELD_TYPE_MATTERS && !DECL_NAME (field)) 4690132718Skan { 4691132718Skan was_unnamed_p = true; 4692132718Skan DECL_NAME (field) = make_anon_name (); 4693132718Skan } 4694132718Skan#endif 469590075Sobrien DECL_SIZE (field) = TYPE_SIZE (integer_type); 469690075Sobrien DECL_ALIGN (field) = TYPE_ALIGN (integer_type); 469790075Sobrien DECL_USER_ALIGN (field) = TYPE_USER_ALIGN (integer_type); 4698117395Skan layout_nonempty_base_or_field (rli, field, NULL_TREE, 4699117395Skan empty_base_offsets); 4700132718Skan if (was_unnamed_p) 4701132718Skan DECL_NAME (field) = NULL_TREE; 4702117395Skan /* Now that layout has been performed, set the size of the 4703117395Skan field to the size of its declared type; the rest of the 4704117395Skan field is effectively invisible. */ 4705117395Skan DECL_SIZE (field) = TYPE_SIZE (type); 4706122180Skan /* We must also reset the DECL_MODE of the field. */ 4707122180Skan if (abi_version_at_least (2)) 4708122180Skan DECL_MODE (field) = TYPE_MODE (type); 4709122180Skan else if (warn_abi 4710122180Skan && DECL_MODE (field) != TYPE_MODE (type)) 4711122180Skan /* Versions of G++ before G++ 3.4 did not reset the 4712122180Skan DECL_MODE. */ 4713169689Skan warning (OPT_Wabi, 4714169689Skan "the offset of %qD may not be ABI-compliant and may " 4715122180Skan "change in a future version of GCC", field); 471618334Speter } 471718334Speter else 4718132718Skan layout_nonempty_base_or_field (rli, field, NULL_TREE, 4719132718Skan empty_base_offsets); 472090075Sobrien 4721117395Skan /* Remember the location of any empty classes in FIELD. */ 4722117395Skan if (abi_version_at_least (2)) 4723169689Skan record_subobject_offsets (TREE_TYPE (field), 4724117395Skan byte_position(field), 4725117395Skan empty_base_offsets, 4726169689Skan /*is_data_member=*/true); 472790075Sobrien 4728102780Skan /* If a bit-field does not immediately follow another bit-field, 4729102780Skan and yet it starts in the middle of a byte, we have failed to 4730102780Skan comply with the ABI. */ 4731102780Skan if (warn_abi 4732169689Skan && DECL_C_BIT_FIELD (field) 4733169689Skan /* The TREE_NO_WARNING flag gets set by Objective-C when 4734169689Skan laying out an Objective-C class. The ObjC ABI differs 4735169689Skan from the C++ ABI, and so we do not want a warning 4736169689Skan here. */ 4737169689Skan && !TREE_NO_WARNING (field) 4738102780Skan && !last_field_was_bitfield 4739102780Skan && !integer_zerop (size_binop (TRUNC_MOD_EXPR, 4740102780Skan DECL_FIELD_BIT_OFFSET (field), 4741102780Skan bitsize_unit_node))) 4742169689Skan warning (OPT_Wabi, "offset of %q+D is not ABI-compliant and may " 4743169689Skan "change in a future version of GCC", field); 4744102780Skan 4745117395Skan /* G++ used to use DECL_FIELD_OFFSET as if it were the byte 4746117395Skan offset of the field. */ 4747169689Skan if (warn_abi 4748117395Skan && !tree_int_cst_equal (DECL_FIELD_OFFSET (field), 4749117395Skan byte_position (field)) 4750117395Skan && contains_empty_class_p (TREE_TYPE (field))) 4751169689Skan warning (OPT_Wabi, "%q+D contains empty classes which may cause base " 4752169689Skan "classes to be placed at different locations in a " 4753169689Skan "future version of GCC", field); 4754117395Skan 4755169689Skan /* The middle end uses the type of expressions to determine the 4756169689Skan possible range of expression values. In order to optimize 4757169689Skan "x.i > 7" to "false" for a 2-bit bitfield "i", the middle end 4758169689Skan must be made aware of the width of "i", via its type. 4759169689Skan 4760169689Skan Because C++ does not have integer types of arbitrary width, 4761169689Skan we must (for the purposes of the front end) convert from the 4762169689Skan type assigned here to the declared type of the bitfield 4763169689Skan whenever a bitfield expression is used as an rvalue. 4764169689Skan Similarly, when assigning a value to a bitfield, the value 4765169689Skan must be converted to the type given the bitfield here. */ 4766169689Skan if (DECL_C_BIT_FIELD (field)) 4767169689Skan { 4768169689Skan tree ftype; 4769169689Skan unsigned HOST_WIDE_INT width; 4770169689Skan ftype = TREE_TYPE (field); 4771169689Skan width = tree_low_cst (DECL_SIZE (field), /*unsignedp=*/1); 4772169689Skan if (width != TYPE_PRECISION (ftype)) 4773169689Skan TREE_TYPE (field) 4774169689Skan = c_build_bitfield_integer_type (width, 4775169689Skan TYPE_UNSIGNED (ftype)); 4776169689Skan } 4777169689Skan 477890075Sobrien /* If we needed additional padding after this field, add it 477990075Sobrien now. */ 478090075Sobrien if (padding) 478118334Speter { 478290075Sobrien tree padding_field; 478318334Speter 4784169689Skan padding_field = build_decl (FIELD_DECL, 478590075Sobrien NULL_TREE, 4786169689Skan char_type_node); 478790075Sobrien DECL_BIT_FIELD (padding_field) = 1; 478890075Sobrien DECL_SIZE (padding_field) = padding; 4789132718Skan DECL_CONTEXT (padding_field) = t; 4790132718Skan DECL_ARTIFICIAL (padding_field) = 1; 4791169689Skan DECL_IGNORED_P (padding_field) = 1; 479290075Sobrien layout_nonempty_base_or_field (rli, padding_field, 4793169689Skan NULL_TREE, 4794117395Skan empty_base_offsets); 479518334Speter } 4796102780Skan 4797102780Skan last_field_was_bitfield = DECL_C_BIT_FIELD (field); 479890075Sobrien } 479918334Speter 4800117395Skan if (abi_version_at_least (2) && !integer_zerop (rli->bitpos)) 480190075Sobrien { 4802117395Skan /* Make sure that we are on a byte boundary so that the size of 4803117395Skan the class without virtual bases will always be a round number 4804117395Skan of bytes. */ 4805117395Skan rli->bitpos = round_up (rli->bitpos, BITS_PER_UNIT); 4806117395Skan normalize_rli (rli); 480790075Sobrien } 480890075Sobrien 4809117395Skan /* G++ 3.2 does not allow virtual bases to be overlaid with tail 4810117395Skan padding. */ 4811117395Skan if (!abi_version_at_least (2)) 4812117395Skan include_empty_classes(rli); 481390075Sobrien 481490075Sobrien /* Delete all zero-width bit-fields from the list of fields. Now 481590075Sobrien that the type is laid out they are no longer important. */ 481690075Sobrien remove_zero_width_bit_fields (t); 481790075Sobrien 4818117395Skan /* Create the version of T used for virtual bases. We do not use 4819117395Skan make_aggr_type for this version; this is an artificial type. For 4820117395Skan a POD type, we just reuse T. */ 4821117395Skan if (CLASSTYPE_NON_POD_P (t) || CLASSTYPE_EMPTY_P (t)) 482290075Sobrien { 4823117395Skan base_t = make_node (TREE_CODE (t)); 4824169689Skan 4825117395Skan /* Set the size and alignment for the new type. In G++ 3.2, all 4826117395Skan empty classes were considered to have size zero when used as 4827117395Skan base classes. */ 4828117395Skan if (!abi_version_at_least (2) && CLASSTYPE_EMPTY_P (t)) 4829117395Skan { 4830117395Skan TYPE_SIZE (base_t) = bitsize_zero_node; 4831117395Skan TYPE_SIZE_UNIT (base_t) = size_zero_node; 4832117395Skan if (warn_abi && !integer_zerop (rli_size_unit_so_far (rli))) 4833169689Skan warning (OPT_Wabi, 4834169689Skan "layout of classes derived from empty class %qT " 4835117395Skan "may change in a future version of GCC", 4836117395Skan t); 4837117395Skan } 4838117395Skan else 4839117395Skan { 4840117395Skan tree eoc; 4841117395Skan 4842117395Skan /* If the ABI version is not at least two, and the last 4843117395Skan field was a bit-field, RLI may not be on a byte 4844117395Skan boundary. In particular, rli_size_unit_so_far might 4845117395Skan indicate the last complete byte, while rli_size_so_far 4846117395Skan indicates the total number of bits used. Therefore, 4847117395Skan rli_size_so_far, rather than rli_size_unit_so_far, is 4848117395Skan used to compute TYPE_SIZE_UNIT. */ 4849117395Skan eoc = end_of_class (t, /*include_virtuals_p=*/0); 4850169689Skan TYPE_SIZE_UNIT (base_t) 4851117395Skan = size_binop (MAX_EXPR, 4852117395Skan convert (sizetype, 4853117395Skan size_binop (CEIL_DIV_EXPR, 4854117395Skan rli_size_so_far (rli), 4855117395Skan bitsize_int (BITS_PER_UNIT))), 4856117395Skan eoc); 4857169689Skan TYPE_SIZE (base_t) 4858117395Skan = size_binop (MAX_EXPR, 4859117395Skan rli_size_so_far (rli), 4860117395Skan size_binop (MULT_EXPR, 4861117395Skan convert (bitsizetype, eoc), 4862117395Skan bitsize_int (BITS_PER_UNIT))); 4863117395Skan } 4864117395Skan TYPE_ALIGN (base_t) = rli->record_align; 4865117395Skan TYPE_USER_ALIGN (base_t) = TYPE_USER_ALIGN (t); 4866117395Skan 4867117395Skan /* Copy the fields from T. */ 4868117395Skan next_field = &TYPE_FIELDS (base_t); 4869117395Skan for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field)) 4870117395Skan if (TREE_CODE (field) == FIELD_DECL) 4871117395Skan { 4872117395Skan *next_field = build_decl (FIELD_DECL, 4873169689Skan DECL_NAME (field), 4874117395Skan TREE_TYPE (field)); 4875117395Skan DECL_CONTEXT (*next_field) = base_t; 4876117395Skan DECL_FIELD_OFFSET (*next_field) = DECL_FIELD_OFFSET (field); 4877117395Skan DECL_FIELD_BIT_OFFSET (*next_field) 4878117395Skan = DECL_FIELD_BIT_OFFSET (field); 4879132718Skan DECL_SIZE (*next_field) = DECL_SIZE (field); 4880132718Skan DECL_MODE (*next_field) = DECL_MODE (field); 4881117395Skan next_field = &TREE_CHAIN (*next_field); 4882117395Skan } 4883117395Skan 4884117395Skan /* Record the base version of the type. */ 4885117395Skan CLASSTYPE_AS_BASE (t) = base_t; 4886117395Skan TYPE_CONTEXT (base_t) = t; 488790075Sobrien } 488890075Sobrien else 4889117395Skan CLASSTYPE_AS_BASE (t) = t; 489090075Sobrien 4891107590Sobrien /* Every empty class contains an empty class. */ 4892117395Skan if (CLASSTYPE_EMPTY_P (t)) 4893107590Sobrien CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1; 4894107590Sobrien 489590075Sobrien /* Set the TYPE_DECL for this type to contain the right 489690075Sobrien value for DECL_OFFSET, so that we can use it as part 489790075Sobrien of a COMPONENT_REF for multiple inheritance. */ 489890075Sobrien layout_decl (TYPE_MAIN_DECL (t), 0); 489990075Sobrien 490090075Sobrien /* Now fix up any virtual base class types that we left lying 490190075Sobrien around. We must get these done before we try to lay out the 490290075Sobrien virtual function table. As a side-effect, this will remove the 490390075Sobrien base subobject fields. */ 4904117395Skan layout_virtual_bases (rli, empty_base_offsets); 490590075Sobrien 4906169689Skan /* Make sure that empty classes are reflected in RLI at this 4907117395Skan point. */ 4908117395Skan include_empty_classes(rli); 490990075Sobrien 4910117395Skan /* Make sure not to create any structures with zero size. */ 4911117395Skan if (integer_zerop (rli_size_unit_so_far (rli)) && CLASSTYPE_EMPTY_P (t)) 4912169689Skan place_field (rli, 4913117395Skan build_decl (FIELD_DECL, NULL_TREE, char_type_node)); 4914117395Skan 4915117395Skan /* Let the back-end lay out the type. */ 4916117395Skan finish_record_layout (rli, /*free_p=*/true); 4917117395Skan 4918117395Skan /* Warn about bases that can't be talked about due to ambiguity. */ 4919117395Skan warn_about_ambiguous_bases (t); 4920117395Skan 4921169689Skan /* Now that we're done with layout, give the base fields the real types. */ 4922169689Skan for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field)) 4923169689Skan if (DECL_ARTIFICIAL (field) && IS_FAKE_BASE_TYPE (TREE_TYPE (field))) 4924169689Skan TREE_TYPE (field) = TYPE_CONTEXT (TREE_TYPE (field)); 4925169689Skan 492690075Sobrien /* Clean up. */ 492790075Sobrien splay_tree_delete (empty_base_offsets); 4928169689Skan 4929169689Skan if (CLASSTYPE_EMPTY_P (t) 4930169689Skan && tree_int_cst_lt (sizeof_biggest_empty_class, 4931169689Skan TYPE_SIZE_UNIT (t))) 4932169689Skan sizeof_biggest_empty_class = TYPE_SIZE_UNIT (t); 493390075Sobrien} 493490075Sobrien 4935169689Skan/* Determine the "key method" for the class type indicated by TYPE, 4936169689Skan and set CLASSTYPE_KEY_METHOD accordingly. */ 493790075Sobrien 4938169689Skanvoid 4939169689Skandetermine_key_method (tree type) 4940117395Skan{ 4941117395Skan tree method; 494290075Sobrien 4943117395Skan if (TYPE_FOR_JAVA (type) 4944117395Skan || processing_template_decl 4945117395Skan || CLASSTYPE_TEMPLATE_INSTANTIATION (type) 4946117395Skan || CLASSTYPE_INTERFACE_KNOWN (type)) 4947169689Skan return; 494890075Sobrien 4949169689Skan /* The key method is the first non-pure virtual function that is not 4950169689Skan inline at the point of class definition. On some targets the 4951169689Skan key function may not be inline; those targets should not call 4952169689Skan this function until the end of the translation unit. */ 4953117395Skan for (method = TYPE_METHODS (type); method != NULL_TREE; 4954117395Skan method = TREE_CHAIN (method)) 4955117395Skan if (DECL_VINDEX (method) != NULL_TREE 4956117395Skan && ! DECL_DECLARED_INLINE_P (method) 4957117395Skan && ! DECL_PURE_VIRTUAL_P (method)) 4958169689Skan { 4959169689Skan CLASSTYPE_KEY_METHOD (type) = method; 4960169689Skan break; 4961169689Skan } 496290075Sobrien 4963169689Skan return; 4964117395Skan} 496590075Sobrien 4966117395Skan/* Perform processing required when the definition of T (a class type) 4967117395Skan is complete. */ 4968117395Skan 496990075Sobrienvoid 4970132718Skanfinish_struct_1 (tree t) 497190075Sobrien{ 497290075Sobrien tree x; 4973117395Skan /* A TREE_LIST. The TREE_VALUE of each node is a FUNCTION_DECL. */ 4974102780Skan tree virtuals = NULL_TREE; 497590075Sobrien int n_fields = 0; 497690075Sobrien 497790075Sobrien if (COMPLETE_TYPE_P (t)) 497890075Sobrien { 4979169689Skan gcc_assert (IS_AGGR_TYPE (t)); 4980169689Skan error ("redefinition of %q#T", t); 498190075Sobrien popclass (); 498290075Sobrien return; 498390075Sobrien } 498490075Sobrien 498590075Sobrien /* If this type was previously laid out as a forward reference, 498690075Sobrien make sure we lay it out again. */ 498790075Sobrien TYPE_SIZE (t) = NULL_TREE; 498890075Sobrien CLASSTYPE_PRIMARY_BINFO (t) = NULL_TREE; 498990075Sobrien 499090075Sobrien fixup_inline_methods (t); 4991169689Skan 4992117395Skan /* Make assumptions about the class; we'll reset the flags if 4993117395Skan necessary. */ 4994117395Skan CLASSTYPE_EMPTY_P (t) = 1; 4995117395Skan CLASSTYPE_NEARLY_EMPTY_P (t) = 1; 4996117395Skan CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 0; 4997117395Skan 499890075Sobrien /* Do end-of-class semantic processing: checking the validity of the 499990075Sobrien bases and members and add implicitly generated methods. */ 5000117395Skan check_bases_and_members (t); 500190075Sobrien 5002132718Skan /* Find the key method. */ 5003132718Skan if (TYPE_CONTAINS_VPTR_P (t)) 5004117395Skan { 5005169689Skan /* The Itanium C++ ABI permits the key method to be chosen when 5006169689Skan the class is defined -- even though the key method so 5007169689Skan selected may later turn out to be an inline function. On 5008169689Skan some systems (such as ARM Symbian OS) the key method cannot 5009169689Skan be determined until the end of the translation unit. On such 5010169689Skan systems, we leave CLASSTYPE_KEY_METHOD set to NULL, which 5011169689Skan will cause the class to be added to KEYED_CLASSES. Then, in 5012169689Skan finish_file we will determine the key method. */ 5013169689Skan if (targetm.cxx.key_method_may_be_inline ()) 5014169689Skan determine_key_method (t); 5015117395Skan 5016117395Skan /* If a polymorphic class has no key method, we may emit the vtable 5017132718Skan in every translation unit where the class definition appears. */ 5018117395Skan if (CLASSTYPE_KEY_METHOD (t) == NULL_TREE) 5019117395Skan keyed_classes = tree_cons (NULL_TREE, t, keyed_classes); 5020117395Skan } 5021117395Skan 502290075Sobrien /* Layout the class itself. */ 5023117395Skan layout_class_type (t, &virtuals); 5024132718Skan if (CLASSTYPE_AS_BASE (t) != t) 5025132718Skan /* We use the base type for trivial assignments, and hence it 5026132718Skan needs a mode. */ 5027132718Skan compute_record_mode (CLASSTYPE_AS_BASE (t)); 502890075Sobrien 5029117395Skan virtuals = modify_all_vtables (t, nreverse (virtuals)); 503090075Sobrien 503190075Sobrien /* If necessary, create the primary vtable for this class. */ 5032102780Skan if (virtuals || TYPE_CONTAINS_VPTR_P (t)) 503390075Sobrien { 503490075Sobrien /* We must enter these virtuals into the table. */ 503590075Sobrien if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t)) 503690075Sobrien build_primary_vtable (NULL_TREE, t); 5037132718Skan else if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t))) 503890075Sobrien /* Here we know enough to change the type of our virtual 503990075Sobrien function table, but we will wait until later this function. */ 504090075Sobrien build_primary_vtable (CLASSTYPE_PRIMARY_BINFO (t), t); 504118334Speter } 504218334Speter 504390075Sobrien if (TYPE_CONTAINS_VPTR_P (t)) 504418334Speter { 5045117395Skan int vindex; 5046117395Skan tree fn; 5047117395Skan 5048169689Skan if (BINFO_VTABLE (TYPE_BINFO (t))) 5049169689Skan gcc_assert (DECL_VIRTUAL_P (BINFO_VTABLE (TYPE_BINFO (t)))); 505090075Sobrien if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t)) 5051169689Skan gcc_assert (BINFO_VIRTUALS (TYPE_BINFO (t)) == NULL_TREE); 505290075Sobrien 5053102780Skan /* Add entries for virtual functions introduced by this class. */ 5054169689Skan BINFO_VIRTUALS (TYPE_BINFO (t)) 5055169689Skan = chainon (BINFO_VIRTUALS (TYPE_BINFO (t)), virtuals); 5056117395Skan 5057117395Skan /* Set DECL_VINDEX for all functions declared in this class. */ 5058169689Skan for (vindex = 0, fn = BINFO_VIRTUALS (TYPE_BINFO (t)); 5059169689Skan fn; 5060169689Skan fn = TREE_CHAIN (fn), 5061117395Skan vindex += (TARGET_VTABLE_USES_DESCRIPTORS 5062117395Skan ? TARGET_VTABLE_USES_DESCRIPTORS : 1)) 5063132718Skan { 5064132718Skan tree fndecl = BV_FN (fn); 5065132718Skan 5066132718Skan if (DECL_THUNK_P (fndecl)) 5067132718Skan /* A thunk. We should never be calling this entry directly 5068132718Skan from this vtable -- we'd use the entry for the non 5069132718Skan thunk base function. */ 5070132718Skan DECL_VINDEX (fndecl) = NULL_TREE; 5071132718Skan else if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST) 5072169689Skan DECL_VINDEX (fndecl) = build_int_cst (NULL_TREE, vindex); 5073132718Skan } 507418334Speter } 507518334Speter 507690075Sobrien finish_struct_bits (t); 507718334Speter 507890075Sobrien /* Complete the rtl for any static member objects of the type we're 507990075Sobrien working on. */ 508090075Sobrien for (x = TYPE_FIELDS (t); x; x = TREE_CHAIN (x)) 508190075Sobrien if (TREE_CODE (x) == VAR_DECL && TREE_STATIC (x) 5082169689Skan && TREE_TYPE (x) != error_mark_node 508396263Sobrien && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (x)), t)) 508490075Sobrien DECL_MODE (x) = TYPE_MODE (t); 508518334Speter 508690075Sobrien /* Done with FIELDS...now decide whether to sort these for 508790075Sobrien faster lookups later. 508818334Speter 5089117395Skan We use a small number because most searches fail (succeeding 509090075Sobrien ultimately as the search bores through the inheritance 509190075Sobrien hierarchy), and we want this failure to occur quickly. */ 509218334Speter 509390075Sobrien n_fields = count_fields (TYPE_FIELDS (t)); 509490075Sobrien if (n_fields > 7) 509518334Speter { 5096169689Skan struct sorted_fields_type *field_vec = GGC_NEWVAR 5097169689Skan (struct sorted_fields_type, 5098169689Skan sizeof (struct sorted_fields_type) + n_fields * sizeof (tree)); 5099132718Skan field_vec->len = n_fields; 5100132718Skan add_fields_to_record_type (TYPE_FIELDS (t), field_vec, 0); 5101132718Skan qsort (field_vec->elts, n_fields, sizeof (tree), 5102132718Skan field_decl_cmp); 510390075Sobrien if (! DECL_LANG_SPECIFIC (TYPE_MAIN_DECL (t))) 510490075Sobrien retrofit_lang_decl (TYPE_MAIN_DECL (t)); 510590075Sobrien DECL_SORTED_FIELDS (TYPE_MAIN_DECL (t)) = field_vec; 510618334Speter } 510718334Speter 5108169689Skan /* Complain if one of the field types requires lower visibility. */ 5109169689Skan constrain_class_visibility (t); 511018334Speter 511118334Speter /* Make the rtl for any new vtables we have created, and unmark 511218334Speter the base types we marked. */ 511390075Sobrien finish_vtbls (t); 5114169689Skan 511590075Sobrien /* Build the VTT for T. */ 511690075Sobrien build_vtt (t); 511718334Speter 5118169689Skan /* This warning does not make sense for Java classes, since they 5119169689Skan cannot have destructors. */ 5120169689Skan if (!TYPE_FOR_JAVA (t) && warn_nonvdtor && TYPE_POLYMORPHIC_P (t)) 5121169689Skan { 5122169689Skan tree dtor; 512318334Speter 5124169689Skan dtor = CLASSTYPE_DESTRUCTORS (t); 5125169689Skan /* Warn only if the dtor is non-private or the class has 5126169689Skan friends. */ 5127169689Skan if (/* An implicitly declared destructor is always public. And, 5128169689Skan if it were virtual, we would have created it by now. */ 5129169689Skan !dtor 5130169689Skan || (!DECL_VINDEX (dtor) 5131169689Skan && (!TREE_PRIVATE (dtor) 5132169689Skan || CLASSTYPE_FRIEND_CLASSES (t) 5133169689Skan || DECL_FRIENDLIST (TYPE_MAIN_DECL (t))))) 5134169689Skan warning (0, "%q#T has virtual functions but non-virtual destructor", 5135169689Skan t); 5136169689Skan } 5137169689Skan 513896263Sobrien complete_vars (t); 513918334Speter 514050397Sobrien if (warn_overloaded_virtual) 514150397Sobrien warn_hidden (t); 514218334Speter 5143169689Skan /* Class layout, assignment of virtual table slots, etc., is now 5144169689Skan complete. Give the back end a chance to tweak the visibility of 5145169689Skan the class or perform any other required target modifications. */ 5146169689Skan targetm.cxx.adjust_class_at_definition (t); 5147169689Skan 514890075Sobrien maybe_suppress_debug_info (t); 514918334Speter 515090075Sobrien dump_class_hierarchy (t); 5151169689Skan 515218334Speter /* Finish debugging output for this type. */ 515390075Sobrien rest_of_type_compilation (t, ! LOCAL_CLASS_P (t)); 515418334Speter} 515518334Speter 515652284Sobrien/* When T was built up, the member declarations were added in reverse 515752284Sobrien order. Rearrange them to declaration order. */ 515852284Sobrien 515952284Sobrienvoid 5160132718Skanunreverse_member_declarations (tree t) 516152284Sobrien{ 516252284Sobrien tree next; 516352284Sobrien tree prev; 516452284Sobrien tree x; 516552284Sobrien 5166117395Skan /* The following lists are all in reverse order. Put them in 5167117395Skan declaration order now. */ 516852284Sobrien TYPE_METHODS (t) = nreverse (TYPE_METHODS (t)); 5169117395Skan CLASSTYPE_DECL_LIST (t) = nreverse (CLASSTYPE_DECL_LIST (t)); 517052284Sobrien 517152284Sobrien /* Actually, for the TYPE_FIELDS, only the non TYPE_DECLs are in 517252284Sobrien reverse order, so we can't just use nreverse. */ 517352284Sobrien prev = NULL_TREE; 5174169689Skan for (x = TYPE_FIELDS (t); 5175169689Skan x && TREE_CODE (x) != TYPE_DECL; 517652284Sobrien x = next) 517752284Sobrien { 517852284Sobrien next = TREE_CHAIN (x); 517952284Sobrien TREE_CHAIN (x) = prev; 518052284Sobrien prev = x; 518152284Sobrien } 518252284Sobrien if (prev) 518352284Sobrien { 518452284Sobrien TREE_CHAIN (TYPE_FIELDS (t)) = x; 518552284Sobrien if (prev) 518652284Sobrien TYPE_FIELDS (t) = prev; 518752284Sobrien } 518852284Sobrien} 518952284Sobrien 519018334Spetertree 5191132718Skanfinish_struct (tree t, tree attributes) 519218334Speter{ 5193132718Skan location_t saved_loc = input_location; 519418334Speter 519552284Sobrien /* Now that we've got all the field declarations, reverse everything 519652284Sobrien as necessary. */ 519752284Sobrien unreverse_member_declarations (t); 519850397Sobrien 519990075Sobrien cplus_decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE); 520050397Sobrien 520190075Sobrien /* Nadger the current location so that diagnostics point to the start of 520290075Sobrien the struct, not the end. */ 5203132718Skan input_location = DECL_SOURCE_LOCATION (TYPE_NAME (t)); 520490075Sobrien 520550397Sobrien if (processing_template_decl) 520618334Speter { 5207169689Skan tree x; 5208169689Skan 520952284Sobrien finish_struct_methods (t); 521090075Sobrien TYPE_SIZE (t) = bitsize_zero_node; 5211146895Skan TYPE_SIZE_UNIT (t) = size_zero_node; 5212169689Skan 5213169689Skan /* We need to emit an error message if this type was used as a parameter 5214169689Skan and it is an abstract type, even if it is a template. We construct 5215169689Skan a simple CLASSTYPE_PURE_VIRTUALS list without taking bases into 5216169689Skan account and we call complete_vars with this type, which will check 5217169689Skan the PARM_DECLS. Note that while the type is being defined, 5218169689Skan CLASSTYPE_PURE_VIRTUALS contains the list of the inline friends 5219169689Skan (see CLASSTYPE_INLINE_FRIENDS) so we need to clear it. */ 5220169689Skan CLASSTYPE_PURE_VIRTUALS (t) = NULL; 5221169689Skan for (x = TYPE_METHODS (t); x; x = TREE_CHAIN (x)) 5222169689Skan if (DECL_PURE_VIRTUAL_P (x)) 5223169689Skan VEC_safe_push (tree, gc, CLASSTYPE_PURE_VIRTUALS (t), x); 5224169689Skan complete_vars (t); 522590075Sobrien } 522650397Sobrien else 522790075Sobrien finish_struct_1 (t); 522850397Sobrien 5229132718Skan input_location = saved_loc; 523090075Sobrien 523150397Sobrien TYPE_BEING_DEFINED (t) = 0; 523250397Sobrien 523350397Sobrien if (current_class_type) 523452284Sobrien popclass (); 523518334Speter else 523690075Sobrien error ("trying to finish struct, but kicked out due to previous parse errors"); 523750397Sobrien 5238117395Skan if (processing_template_decl && at_function_scope_p ()) 5239117395Skan add_stmt (build_min (TAG_DEFN, t)); 524090075Sobrien 524150397Sobrien return t; 524218334Speter} 524318334Speter 524452284Sobrien/* Return the dynamic type of INSTANCE, if known. 524518334Speter Used to determine whether the virtual function table is needed 524618334Speter or not. 524718334Speter 524818334Speter *NONNULL is set iff INSTANCE can be known to be nonnull, regardless 524990075Sobrien of our knowledge of its type. *NONNULL should be initialized 525090075Sobrien before this function is called. */ 525150397Sobrien 525252284Sobrienstatic tree 5253132718Skanfixed_type_or_null (tree instance, int* nonnull, int* cdtorp) 525418334Speter{ 525518334Speter switch (TREE_CODE (instance)) 525618334Speter { 525718334Speter case INDIRECT_REF: 525890075Sobrien if (POINTER_TYPE_P (TREE_TYPE (instance))) 525990075Sobrien return NULL_TREE; 526090075Sobrien else 526190075Sobrien return fixed_type_or_null (TREE_OPERAND (instance, 0), 526290075Sobrien nonnull, cdtorp); 526390075Sobrien 526418334Speter case CALL_EXPR: 526518334Speter /* This is a call to a constructor, hence it's never zero. */ 526618334Speter if (TREE_HAS_CONSTRUCTOR (instance)) 526718334Speter { 526818334Speter if (nonnull) 526918334Speter *nonnull = 1; 527052284Sobrien return TREE_TYPE (instance); 527118334Speter } 527252284Sobrien return NULL_TREE; 527318334Speter 527418334Speter case SAVE_EXPR: 527518334Speter /* This is a call to a constructor, hence it's never zero. */ 527618334Speter if (TREE_HAS_CONSTRUCTOR (instance)) 527718334Speter { 527818334Speter if (nonnull) 527918334Speter *nonnull = 1; 528052284Sobrien return TREE_TYPE (instance); 528118334Speter } 528290075Sobrien return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp); 528318334Speter 528418334Speter case PLUS_EXPR: 528518334Speter case MINUS_EXPR: 528690075Sobrien if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR) 528790075Sobrien return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp); 528818334Speter if (TREE_CODE (TREE_OPERAND (instance, 1)) == INTEGER_CST) 528918334Speter /* Propagate nonnull. */ 5290117395Skan return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp); 529152284Sobrien return NULL_TREE; 529218334Speter 529318334Speter case NOP_EXPR: 529418334Speter case CONVERT_EXPR: 529590075Sobrien return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp); 529618334Speter 529718334Speter case ADDR_EXPR: 5298169689Skan instance = TREE_OPERAND (instance, 0); 529918334Speter if (nonnull) 5300169689Skan { 5301169689Skan /* Just because we see an ADDR_EXPR doesn't mean we're dealing 5302169689Skan with a real object -- given &p->f, p can still be null. */ 5303169689Skan tree t = get_base_address (instance); 5304169689Skan /* ??? Probably should check DECL_WEAK here. */ 5305169689Skan if (t && DECL_P (t)) 5306169689Skan *nonnull = 1; 5307169689Skan } 5308169689Skan return fixed_type_or_null (instance, nonnull, cdtorp); 530918334Speter 531018334Speter case COMPONENT_REF: 5311169689Skan /* If this component is really a base class reference, then the field 5312169689Skan itself isn't definitive. */ 5313169689Skan if (DECL_FIELD_IS_BASE (TREE_OPERAND (instance, 1))) 5314169689Skan return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp); 531590075Sobrien return fixed_type_or_null (TREE_OPERAND (instance, 1), nonnull, cdtorp); 531618334Speter 531718334Speter case VAR_DECL: 531818334Speter case FIELD_DECL: 531918334Speter if (TREE_CODE (TREE_TYPE (instance)) == ARRAY_TYPE 532018334Speter && IS_AGGR_TYPE (TREE_TYPE (TREE_TYPE (instance)))) 532118334Speter { 532218334Speter if (nonnull) 532318334Speter *nonnull = 1; 532452284Sobrien return TREE_TYPE (TREE_TYPE (instance)); 532518334Speter } 532650397Sobrien /* fall through... */ 532718334Speter case TARGET_EXPR: 532818334Speter case PARM_DECL: 5329117395Skan case RESULT_DECL: 533018334Speter if (IS_AGGR_TYPE (TREE_TYPE (instance))) 533118334Speter { 533218334Speter if (nonnull) 533318334Speter *nonnull = 1; 533452284Sobrien return TREE_TYPE (instance); 533518334Speter } 533690075Sobrien else if (instance == current_class_ptr) 5337169689Skan { 5338169689Skan if (nonnull) 5339169689Skan *nonnull = 1; 5340169689Skan 5341169689Skan /* if we're in a ctor or dtor, we know our type. */ 5342169689Skan if (DECL_LANG_SPECIFIC (current_function_decl) 5343169689Skan && (DECL_CONSTRUCTOR_P (current_function_decl) 5344169689Skan || DECL_DESTRUCTOR_P (current_function_decl))) 5345169689Skan { 5346169689Skan if (cdtorp) 5347169689Skan *cdtorp = 1; 5348169689Skan return TREE_TYPE (TREE_TYPE (instance)); 5349169689Skan } 5350169689Skan } 535190075Sobrien else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE) 5352169689Skan { 5353171825Skan /* We only need one hash table because it is always left empty. */ 5354171825Skan static htab_t ht; 5355171825Skan if (!ht) 5356171825Skan ht = htab_create (37, 5357171825Skan htab_hash_pointer, 5358171825Skan htab_eq_pointer, 5359171825Skan /*htab_del=*/NULL); 5360171825Skan 5361169689Skan /* Reference variables should be references to objects. */ 5362169689Skan if (nonnull) 536318334Speter *nonnull = 1; 5364169689Skan 5365171825Skan /* Enter the INSTANCE in a table to prevent recursion; a 5366117395Skan variable's initializer may refer to the variable 5367117395Skan itself. */ 5368169689Skan if (TREE_CODE (instance) == VAR_DECL 5369117395Skan && DECL_INITIAL (instance) 5370171825Skan && !htab_find (ht, instance)) 5371117395Skan { 5372117395Skan tree type; 5373171825Skan void **slot; 5374171825Skan 5375171825Skan slot = htab_find_slot (ht, instance, INSERT); 5376171825Skan *slot = instance; 5377117395Skan type = fixed_type_or_null (DECL_INITIAL (instance), 5378117395Skan nonnull, cdtorp); 5379171825Skan htab_remove_elt (ht, instance); 5380171825Skan 5381117395Skan return type; 5382117395Skan } 538318334Speter } 538452284Sobrien return NULL_TREE; 538518334Speter 538618334Speter default: 538752284Sobrien return NULL_TREE; 538818334Speter } 538918334Speter} 539052284Sobrien 5391117395Skan/* Return nonzero if the dynamic type of INSTANCE is known, and 539290075Sobrien equivalent to the static type. We also handle the case where 539390075Sobrien INSTANCE is really a pointer. Return negative if this is a 539490075Sobrien ctor/dtor. There the dynamic type is known, but this might not be 539590075Sobrien the most derived base of the original object, and hence virtual 539690075Sobrien bases may not be layed out according to this type. 539752284Sobrien 539852284Sobrien Used to determine whether the virtual function table is needed 539952284Sobrien or not. 540052284Sobrien 540152284Sobrien *NONNULL is set iff INSTANCE can be known to be nonnull, regardless 540290075Sobrien of our knowledge of its type. *NONNULL should be initialized 540390075Sobrien before this function is called. */ 540452284Sobrien 540552284Sobrienint 5406132718Skanresolves_to_fixed_type_p (tree instance, int* nonnull) 540752284Sobrien{ 540852284Sobrien tree t = TREE_TYPE (instance); 540990075Sobrien int cdtorp = 0; 5410169689Skan 541190075Sobrien tree fixed = fixed_type_or_null (instance, nonnull, &cdtorp); 541252284Sobrien if (fixed == NULL_TREE) 541352284Sobrien return 0; 541452284Sobrien if (POINTER_TYPE_P (t)) 541552284Sobrien t = TREE_TYPE (t); 541690075Sobrien if (!same_type_ignoring_top_level_qualifiers_p (t, fixed)) 541790075Sobrien return 0; 541890075Sobrien return cdtorp ? -1 : 1; 541952284Sobrien} 542052284Sobrien 542118334Speter 542218334Spetervoid 5423132718Skaninit_class_processing (void) 542418334Speter{ 542518334Speter current_class_depth = 0; 542652284Sobrien current_class_stack_size = 10; 5427169689Skan current_class_stack 5428169689Skan = XNEWVEC (struct class_stack_node, current_class_stack_size); 5429169689Skan local_classes = VEC_alloc (tree, gc, 8); 5430169689Skan sizeof_biggest_empty_class = size_zero_node; 543118334Speter 543290075Sobrien ridpointers[(int) RID_PUBLIC] = access_public_node; 543390075Sobrien ridpointers[(int) RID_PRIVATE] = access_private_node; 543490075Sobrien ridpointers[(int) RID_PROTECTED] = access_protected_node; 543518334Speter} 543618334Speter 5437169689Skan/* Restore the cached PREVIOUS_CLASS_LEVEL. */ 5438169689Skan 5439169689Skanstatic void 5440169689Skanrestore_class_cache (void) 5441169689Skan{ 5442169689Skan tree type; 5443169689Skan 5444169689Skan /* We are re-entering the same class we just left, so we don't 5445169689Skan have to search the whole inheritance matrix to find all the 5446169689Skan decls to bind again. Instead, we install the cached 5447169689Skan class_shadowed list and walk through it binding names. */ 5448169689Skan push_binding_level (previous_class_level); 5449169689Skan class_binding_level = previous_class_level; 5450169689Skan /* Restore IDENTIFIER_TYPE_VALUE. */ 5451169689Skan for (type = class_binding_level->type_shadowed; 5452169689Skan type; 5453169689Skan type = TREE_CHAIN (type)) 5454169689Skan SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (type), TREE_TYPE (type)); 5455169689Skan} 5456169689Skan 5457132718Skan/* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE as 5458132718Skan appropriate for TYPE. 545918334Speter 546018334Speter So that we may avoid calls to lookup_name, we cache the _TYPE 546118334Speter nodes of local TYPE_DECLs in the TREE_TYPE field of the name. 546218334Speter 546318334Speter For multiple inheritance, we perform a two-pass depth-first search 5464169689Skan of the type lattice. */ 546518334Speter 546618334Spetervoid 5467132718Skanpushclass (tree type) 546818334Speter{ 5469169689Skan class_stack_node_t csn; 5470169689Skan 547150397Sobrien type = TYPE_MAIN_VARIANT (type); 547218334Speter 547352284Sobrien /* Make sure there is enough room for the new entry on the stack. */ 5474169689Skan if (current_class_depth + 1 >= current_class_stack_size) 547518334Speter { 547652284Sobrien current_class_stack_size *= 2; 547752284Sobrien current_class_stack 5478169689Skan = XRESIZEVEC (struct class_stack_node, current_class_stack, 5479169689Skan current_class_stack_size); 548018334Speter } 548118334Speter 548252284Sobrien /* Insert a new entry on the class stack. */ 5483169689Skan csn = current_class_stack + current_class_depth; 5484169689Skan csn->name = current_class_name; 5485169689Skan csn->type = current_class_type; 5486169689Skan csn->access = current_access_specifier; 5487169689Skan csn->names_used = 0; 5488169689Skan csn->hidden = 0; 548952284Sobrien current_class_depth++; 549052284Sobrien 549152284Sobrien /* Now set up the new type. */ 549218334Speter current_class_name = TYPE_NAME (type); 549318334Speter if (TREE_CODE (current_class_name) == TYPE_DECL) 549418334Speter current_class_name = DECL_NAME (current_class_name); 549518334Speter current_class_type = type; 549618334Speter 549752284Sobrien /* By default, things in classes are private, while things in 549852284Sobrien structures or unions are public. */ 5499169689Skan current_access_specifier = (CLASSTYPE_DECLARED_CLASS (type) 5500169689Skan ? access_private_node 550152284Sobrien : access_public_node); 550252284Sobrien 5503169689Skan if (previous_class_level 5504169689Skan && type != previous_class_level->this_entity 550518334Speter && current_class_depth == 1) 550618334Speter { 550718334Speter /* Forcibly remove any old class remnants. */ 550852284Sobrien invalidate_class_lookup_cache (); 550918334Speter } 551018334Speter 5511169689Skan if (!previous_class_level 5512169689Skan || type != previous_class_level->this_entity 5513169689Skan || current_class_depth > 1) 5514169689Skan pushlevel_class (); 5515132718Skan else 5516169689Skan restore_class_cache (); 551752284Sobrien} 551850397Sobrien 5519169689Skan/* When we exit a toplevel class scope, we save its binding level so 5520169689Skan that we can restore it quickly. Here, we've entered some other 5521169689Skan class, so we must invalidate our cache. */ 552218334Speter 552352284Sobrienvoid 5524132718Skaninvalidate_class_lookup_cache (void) 552552284Sobrien{ 5526169689Skan previous_class_level = NULL; 5527169689Skan} 552890075Sobrien 552918334Speter/* Get out of the current class scope. If we were in a class scope 553052284Sobrien previously, that is the one popped to. */ 553150397Sobrien 553218334Spetervoid 5533132718Skanpopclass (void) 553418334Speter{ 553590075Sobrien poplevel_class (); 553618334Speter 553718334Speter current_class_depth--; 553852284Sobrien current_class_name = current_class_stack[current_class_depth].name; 553952284Sobrien current_class_type = current_class_stack[current_class_depth].type; 554052284Sobrien current_access_specifier = current_class_stack[current_class_depth].access; 554152284Sobrien if (current_class_stack[current_class_depth].names_used) 554252284Sobrien splay_tree_delete (current_class_stack[current_class_depth].names_used); 554318334Speter} 554418334Speter 5545169689Skan/* Mark the top of the class stack as hidden. */ 554650397Sobrien 5547169689Skanvoid 5548169689Skanpush_class_stack (void) 5549169689Skan{ 5550169689Skan if (current_class_depth) 5551169689Skan ++current_class_stack[current_class_depth - 1].hidden; 5552169689Skan} 5553169689Skan 5554169689Skan/* Mark the top of the class stack as un-hidden. */ 5555169689Skan 5556169689Skanvoid 5557169689Skanpop_class_stack (void) 5558169689Skan{ 5559169689Skan if (current_class_depth) 5560169689Skan --current_class_stack[current_class_depth - 1].hidden; 5561169689Skan} 5562169689Skan 5563169689Skan/* Returns 1 if the class type currently being defined is either T or 5564169689Skan a nested type of T. */ 5565169689Skan 5566169689Skanbool 5567132718Skancurrently_open_class (tree t) 556850397Sobrien{ 556950397Sobrien int i; 5570169689Skan 5571169689Skan /* We start looking from 1 because entry 0 is from global scope, 5572169689Skan and has no type. */ 5573169689Skan for (i = current_class_depth; i > 0; --i) 5574169689Skan { 5575169689Skan tree c; 5576169689Skan if (i == current_class_depth) 5577169689Skan c = current_class_type; 5578169689Skan else 5579169689Skan { 5580169689Skan if (current_class_stack[i].hidden) 5581169689Skan break; 5582169689Skan c = current_class_stack[i].type; 5583169689Skan } 5584169689Skan if (!c) 5585169689Skan continue; 5586169689Skan if (same_type_p (c, t)) 5587169689Skan return true; 5588169689Skan } 5589169689Skan return false; 559050397Sobrien} 559150397Sobrien 559290075Sobrien/* If either current_class_type or one of its enclosing classes are derived 559390075Sobrien from T, return the appropriate type. Used to determine how we found 559490075Sobrien something via unqualified lookup. */ 559590075Sobrien 559690075Sobrientree 5597132718Skancurrently_open_derived_class (tree t) 559890075Sobrien{ 559990075Sobrien int i; 560090075Sobrien 5601132718Skan /* The bases of a dependent type are unknown. */ 5602132718Skan if (dependent_type_p (t)) 5603132718Skan return NULL_TREE; 5604132718Skan 5605132718Skan if (!current_class_type) 5606132718Skan return NULL_TREE; 5607132718Skan 560890075Sobrien if (DERIVED_FROM_P (t, current_class_type)) 560990075Sobrien return current_class_type; 561090075Sobrien 561190075Sobrien for (i = current_class_depth - 1; i > 0; --i) 5612169689Skan { 5613169689Skan if (current_class_stack[i].hidden) 5614169689Skan break; 5615169689Skan if (DERIVED_FROM_P (t, current_class_stack[i].type)) 5616169689Skan return current_class_stack[i].type; 5617169689Skan } 561890075Sobrien 561990075Sobrien return NULL_TREE; 562090075Sobrien} 562190075Sobrien 562218334Speter/* When entering a class scope, all enclosing class scopes' names with 5623132718Skan static meaning (static variables, static functions, types and 5624132718Skan enumerators) have to be visible. This recursive function calls 5625132718Skan pushclass for all enclosing class contexts until global or a local 5626132718Skan scope is reached. TYPE is the enclosed class. */ 562718334Speter 562818334Spetervoid 5629132718Skanpush_nested_class (tree type) 563018334Speter{ 563118334Speter tree context; 563218334Speter 563352284Sobrien /* A namespace might be passed in error cases, like A::B:C. */ 5634169689Skan if (type == NULL_TREE 5635169689Skan || type == error_mark_node 563652284Sobrien || TREE_CODE (type) == NAMESPACE_DECL 563790075Sobrien || ! IS_AGGR_TYPE (type) 563850397Sobrien || TREE_CODE (type) == TEMPLATE_TYPE_PARM 563990075Sobrien || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM) 564018334Speter return; 5641169689Skan 564250397Sobrien context = DECL_CONTEXT (TYPE_MAIN_DECL (type)); 564318334Speter 564452284Sobrien if (context && CLASS_TYPE_P (context)) 5645132718Skan push_nested_class (context); 5646132718Skan pushclass (type); 564718334Speter} 564818334Speter 5649132718Skan/* Undoes a push_nested_class call. */ 565018334Speter 565118334Spetervoid 5652132718Skanpop_nested_class (void) 565318334Speter{ 565450397Sobrien tree context = DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type)); 565518334Speter 565652284Sobrien popclass (); 565752284Sobrien if (context && CLASS_TYPE_P (context)) 565852284Sobrien pop_nested_class (); 565918334Speter} 566018334Speter 566190075Sobrien/* Returns the number of extern "LANG" blocks we are nested within. */ 566290075Sobrien 566390075Sobrienint 5664132718Skancurrent_lang_depth (void) 566590075Sobrien{ 5666169689Skan return VEC_length (tree, current_lang_base); 566790075Sobrien} 566890075Sobrien 566918334Speter/* Set global variables CURRENT_LANG_NAME to appropriate value 567018334Speter so that behavior of name-mangling machinery is correct. */ 567118334Speter 567218334Spetervoid 5673132718Skanpush_lang_context (tree name) 567418334Speter{ 5675169689Skan VEC_safe_push (tree, gc, current_lang_base, current_lang_name); 567618334Speter 567752284Sobrien if (name == lang_name_cplusplus) 567818334Speter { 567918334Speter current_lang_name = name; 568018334Speter } 568152284Sobrien else if (name == lang_name_java) 568252284Sobrien { 568352284Sobrien current_lang_name = name; 568452284Sobrien /* DECL_IGNORED_P is initially set for these types, to avoid clutter. 568552284Sobrien (See record_builtin_java_type in decl.c.) However, that causes 568652284Sobrien incorrect debug entries if these types are actually used. 5687117395Skan So we re-enable debug output after extern "Java". */ 568890075Sobrien DECL_IGNORED_P (TYPE_NAME (java_byte_type_node)) = 0; 568990075Sobrien DECL_IGNORED_P (TYPE_NAME (java_short_type_node)) = 0; 569090075Sobrien DECL_IGNORED_P (TYPE_NAME (java_int_type_node)) = 0; 569190075Sobrien DECL_IGNORED_P (TYPE_NAME (java_long_type_node)) = 0; 569290075Sobrien DECL_IGNORED_P (TYPE_NAME (java_float_type_node)) = 0; 569390075Sobrien DECL_IGNORED_P (TYPE_NAME (java_double_type_node)) = 0; 569490075Sobrien DECL_IGNORED_P (TYPE_NAME (java_char_type_node)) = 0; 569590075Sobrien DECL_IGNORED_P (TYPE_NAME (java_boolean_type_node)) = 0; 569652284Sobrien } 569718334Speter else if (name == lang_name_c) 569818334Speter { 569918334Speter current_lang_name = name; 570018334Speter } 570118334Speter else 5702169689Skan error ("language string %<\"%E\"%> not recognized", name); 570318334Speter} 5704169689Skan 570518334Speter/* Get out of the current language scope. */ 570650397Sobrien 570718334Spetervoid 5708132718Skanpop_lang_context (void) 570918334Speter{ 5710169689Skan current_lang_name = VEC_pop (tree, current_lang_base); 571118334Speter} 571250397Sobrien 571350397Sobrien/* Type instantiation routines. */ 571418334Speter 571552284Sobrien/* Given an OVERLOAD and a TARGET_TYPE, return the function that 571652284Sobrien matches the TARGET_TYPE. If there is no satisfactory match, return 5717169689Skan error_mark_node, and issue an error & warning messages under 5718169689Skan control of FLAGS. Permit pointers to member function if FLAGS 5719169689Skan permits. If TEMPLATE_ONLY, the name of the overloaded function was 5720169689Skan a template-id, and EXPLICIT_TARGS are the explicitly provided 5721169689Skan template arguments. If OVERLOAD is for one or more member 5722169689Skan functions, then ACCESS_PATH is the base path used to reference 5723169689Skan those member functions. */ 572452284Sobrien 572550397Sobrienstatic tree 5726169689Skanresolve_address_of_overloaded_function (tree target_type, 5727132718Skan tree overload, 5728132718Skan tsubst_flags_t flags, 5729132718Skan bool template_only, 5730169689Skan tree explicit_targs, 5731169689Skan tree access_path) 573218334Speter{ 573352284Sobrien /* Here's what the standard says: 5734169689Skan 573552284Sobrien [over.over] 573650397Sobrien 573752284Sobrien If the name is a function template, template argument deduction 573852284Sobrien is done, and if the argument deduction succeeds, the deduced 573952284Sobrien arguments are used to generate a single template function, which 574052284Sobrien is added to the set of overloaded functions considered. 574152284Sobrien 574252284Sobrien Non-member functions and static member functions match targets of 574352284Sobrien type "pointer-to-function" or "reference-to-function." Nonstatic 574452284Sobrien member functions match targets of type "pointer-to-member 574552284Sobrien function;" the function type of the pointer to member is used to 574652284Sobrien select the member function from the set of overloaded member 574752284Sobrien functions. If a nonstatic member function is selected, the 574852284Sobrien reference to the overloaded function name is required to have the 574952284Sobrien form of a pointer to member as described in 5.3.1. 575052284Sobrien 575152284Sobrien If more than one function is selected, any template functions in 575252284Sobrien the set are eliminated if the set also contains a non-template 575352284Sobrien function, and any given template function is eliminated if the 575452284Sobrien set contains a second template function that is more specialized 575552284Sobrien than the first according to the partial ordering rules 14.5.5.2. 575652284Sobrien After such eliminations, if any, there shall remain exactly one 575752284Sobrien selected function. */ 575852284Sobrien 575952284Sobrien int is_ptrmem = 0; 576052284Sobrien int is_reference = 0; 576152284Sobrien /* We store the matches in a TREE_LIST rooted here. The functions 576252284Sobrien are the TREE_PURPOSE, not the TREE_VALUE, in this list, for easy 576352284Sobrien interoperability with most_specialized_instantiation. */ 576452284Sobrien tree matches = NULL_TREE; 576552284Sobrien tree fn; 576652284Sobrien 576752284Sobrien /* By the time we get here, we should be seeing only real 576852284Sobrien pointer-to-member types, not the internal POINTER_TYPE to 576952284Sobrien METHOD_TYPE representation. */ 5770169689Skan gcc_assert (TREE_CODE (target_type) != POINTER_TYPE 5771169689Skan || TREE_CODE (TREE_TYPE (target_type)) != METHOD_TYPE); 577252284Sobrien 5773169689Skan gcc_assert (is_overloaded_fn (overload)); 5774169689Skan 577552284Sobrien /* Check that the TARGET_TYPE is reasonable. */ 577652284Sobrien if (TYPE_PTRFN_P (target_type)) 577790075Sobrien /* This is OK. */; 577852284Sobrien else if (TYPE_PTRMEMFUNC_P (target_type)) 577952284Sobrien /* This is OK, too. */ 578052284Sobrien is_ptrmem = 1; 578152284Sobrien else if (TREE_CODE (target_type) == FUNCTION_TYPE) 578250397Sobrien { 578352284Sobrien /* This is OK, too. This comes from a conversion to reference 578452284Sobrien type. */ 578552284Sobrien target_type = build_reference_type (target_type); 578652284Sobrien is_reference = 1; 578752284Sobrien } 5788169689Skan else 578952284Sobrien { 5790122180Skan if (flags & tf_error) 5791169689Skan error ("cannot resolve overloaded function %qD based on" 5792169689Skan " conversion to type %qT", 5793169689Skan DECL_NAME (OVL_FUNCTION (overload)), target_type); 579452284Sobrien return error_mark_node; 579552284Sobrien } 5796169689Skan 579752284Sobrien /* If we can find a non-template function that matches, we can just 579852284Sobrien use it. There's no point in generating template instantiations 579952284Sobrien if we're just going to throw them out anyhow. But, of course, we 580052284Sobrien can only do this when we don't *need* a template function. */ 580152284Sobrien if (!template_only) 580252284Sobrien { 580352284Sobrien tree fns; 580452284Sobrien 5805132718Skan for (fns = overload; fns; fns = OVL_NEXT (fns)) 580652284Sobrien { 5807132718Skan tree fn = OVL_CURRENT (fns); 580852284Sobrien tree fntype; 580952284Sobrien 581052284Sobrien if (TREE_CODE (fn) == TEMPLATE_DECL) 581152284Sobrien /* We're not looking for templates just yet. */ 581252284Sobrien continue; 581352284Sobrien 581452284Sobrien if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE) 581552284Sobrien != is_ptrmem) 581652284Sobrien /* We're looking for a non-static member, and this isn't 581752284Sobrien one, or vice versa. */ 581852284Sobrien continue; 5819122180Skan 5820169689Skan /* Ignore functions which haven't been explicitly 5821169689Skan declared. */ 5822122180Skan if (DECL_ANTICIPATED (fn)) 5823122180Skan continue; 5824122180Skan 582552284Sobrien /* See if there's a match. */ 582652284Sobrien fntype = TREE_TYPE (fn); 582752284Sobrien if (is_ptrmem) 582852284Sobrien fntype = build_ptrmemfunc_type (build_pointer_type (fntype)); 582952284Sobrien else if (!is_reference) 583052284Sobrien fntype = build_pointer_type (fntype); 583152284Sobrien 5832169689Skan if (can_convert_arg (target_type, fntype, fn, LOOKUP_NORMAL)) 583390075Sobrien matches = tree_cons (fn, NULL_TREE, matches); 583452284Sobrien } 583552284Sobrien } 583652284Sobrien 583752284Sobrien /* Now, if we've already got a match (or matches), there's no need 583852284Sobrien to proceed to the template functions. But, if we don't have a 583952284Sobrien match we need to look at them, too. */ 5840169689Skan if (!matches) 584152284Sobrien { 584252284Sobrien tree target_fn_type; 584352284Sobrien tree target_arg_types; 584490075Sobrien tree target_ret_type; 584552284Sobrien tree fns; 584652284Sobrien 584752284Sobrien if (is_ptrmem) 584852284Sobrien target_fn_type 584952284Sobrien = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (target_type)); 585050397Sobrien else 585152284Sobrien target_fn_type = TREE_TYPE (target_type); 585252284Sobrien target_arg_types = TYPE_ARG_TYPES (target_fn_type); 585390075Sobrien target_ret_type = TREE_TYPE (target_fn_type); 585490075Sobrien 585590075Sobrien /* Never do unification on the 'this' parameter. */ 585690075Sobrien if (TREE_CODE (target_fn_type) == METHOD_TYPE) 585790075Sobrien target_arg_types = TREE_CHAIN (target_arg_types); 5858169689Skan 5859132718Skan for (fns = overload; fns; fns = OVL_NEXT (fns)) 586050397Sobrien { 5861132718Skan tree fn = OVL_CURRENT (fns); 586252284Sobrien tree instantiation; 586352284Sobrien tree instantiation_type; 586452284Sobrien tree targs; 586552284Sobrien 586652284Sobrien if (TREE_CODE (fn) != TEMPLATE_DECL) 586752284Sobrien /* We're only looking for templates. */ 586852284Sobrien continue; 586952284Sobrien 587052284Sobrien if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE) 587152284Sobrien != is_ptrmem) 587252284Sobrien /* We're not looking for a non-static member, and this is 587352284Sobrien one, or vice versa. */ 587452284Sobrien continue; 587552284Sobrien 587652284Sobrien /* Try to do argument deduction. */ 587790075Sobrien targs = make_tree_vec (DECL_NTPARMS (fn)); 587852284Sobrien if (fn_type_unification (fn, explicit_targs, targs, 587990075Sobrien target_arg_types, target_ret_type, 5880169689Skan DEDUCE_EXACT, LOOKUP_NORMAL)) 588152284Sobrien /* Argument deduction failed. */ 588252284Sobrien continue; 588352284Sobrien 588452284Sobrien /* Instantiate the template. */ 5885132718Skan instantiation = instantiate_template (fn, targs, flags); 588652284Sobrien if (instantiation == error_mark_node) 588752284Sobrien /* Instantiation failed. */ 588852284Sobrien continue; 588952284Sobrien 589052284Sobrien /* See if there's a match. */ 589152284Sobrien instantiation_type = TREE_TYPE (instantiation); 589252284Sobrien if (is_ptrmem) 5893169689Skan instantiation_type = 589452284Sobrien build_ptrmemfunc_type (build_pointer_type (instantiation_type)); 589552284Sobrien else if (!is_reference) 589652284Sobrien instantiation_type = build_pointer_type (instantiation_type); 5897169689Skan if (can_convert_arg (target_type, instantiation_type, instantiation, 5898169689Skan LOOKUP_NORMAL)) 589990075Sobrien matches = tree_cons (instantiation, fn, matches); 590050397Sobrien } 590152284Sobrien 590252284Sobrien /* Now, remove all but the most specialized of the matches. */ 590352284Sobrien if (matches) 590452284Sobrien { 590590075Sobrien tree match = most_specialized_instantiation (matches); 590652284Sobrien 590752284Sobrien if (match != error_mark_node) 5908169689Skan matches = tree_cons (TREE_PURPOSE (match), 5909169689Skan NULL_TREE, 5910169689Skan NULL_TREE); 591152284Sobrien } 591250397Sobrien } 591352284Sobrien 591452284Sobrien /* Now we should have exactly one function in MATCHES. */ 591552284Sobrien if (matches == NULL_TREE) 591652284Sobrien { 591752284Sobrien /* There were *no* matches. */ 5918122180Skan if (flags & tf_error) 591952284Sobrien { 5920169689Skan error ("no matches converting function %qD to type %q#T", 5921169689Skan DECL_NAME (OVL_FUNCTION (overload)), 5922169689Skan target_type); 592352284Sobrien 592452284Sobrien /* print_candidates expects a chain with the functions in 5925169689Skan TREE_VALUE slots, so we cons one up here (we're losing anyway, 5926169689Skan so why be clever?). */ 5927169689Skan for (; overload; overload = OVL_NEXT (overload)) 5928169689Skan matches = tree_cons (NULL_TREE, OVL_CURRENT (overload), 592990075Sobrien matches); 5930169689Skan 593152284Sobrien print_candidates (matches); 593252284Sobrien } 593352284Sobrien return error_mark_node; 593452284Sobrien } 593552284Sobrien else if (TREE_CHAIN (matches)) 593652284Sobrien { 593752284Sobrien /* There were too many matches. */ 593852284Sobrien 5939122180Skan if (flags & tf_error) 594052284Sobrien { 594152284Sobrien tree match; 594252284Sobrien 5943169689Skan error ("converting overloaded function %qD to type %q#T is ambiguous", 594452284Sobrien DECL_NAME (OVL_FUNCTION (overload)), 594552284Sobrien target_type); 594652284Sobrien 594752284Sobrien /* Since print_candidates expects the functions in the 594852284Sobrien TREE_VALUE slot, we flip them here. */ 594952284Sobrien for (match = matches; match; match = TREE_CHAIN (match)) 595052284Sobrien TREE_VALUE (match) = TREE_PURPOSE (match); 595152284Sobrien 595252284Sobrien print_candidates (matches); 595352284Sobrien } 5954169689Skan 595552284Sobrien return error_mark_node; 595652284Sobrien } 595752284Sobrien 595852284Sobrien /* Good, exactly one match. Now, convert it to the correct type. */ 595952284Sobrien fn = TREE_PURPOSE (matches); 596052284Sobrien 596190075Sobrien if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn) 5962132718Skan && !(flags & tf_ptrmem_ok) && !flag_ms_extensions) 596390075Sobrien { 596490075Sobrien static int explained; 5965169689Skan 5966122180Skan if (!(flags & tf_error)) 5967169689Skan return error_mark_node; 596890075Sobrien 5969169689Skan pedwarn ("assuming pointer to member %qD", fn); 597090075Sobrien if (!explained) 5971169689Skan { 5972169689Skan pedwarn ("(a pointer to member can only be formed with %<&%E%>)", fn); 5973169689Skan explained = 1; 5974169689Skan } 597590075Sobrien } 597652284Sobrien 5977122180Skan /* If we're doing overload resolution purely for the purpose of 5978122180Skan determining conversion sequences, we should not consider the 5979122180Skan function used. If this conversion sequence is selected, the 5980122180Skan function will be marked as used at this point. */ 5981122180Skan if (!(flags & tf_conv)) 5982169689Skan { 5983169689Skan mark_used (fn); 5984169689Skan /* We could not check access when this expression was originally 5985169689Skan created since we did not know at that time to which function 5986169689Skan the expression referred. */ 5987169689Skan if (DECL_FUNCTION_MEMBER_P (fn)) 5988169689Skan { 5989169689Skan gcc_assert (access_path); 5990169689Skan perform_or_defer_access_check (access_path, fn, fn); 5991169689Skan } 5992169689Skan } 5993122180Skan 599452284Sobrien if (TYPE_PTRFN_P (target_type) || TYPE_PTRMEMFUNC_P (target_type)) 599552284Sobrien return build_unary_op (ADDR_EXPR, fn, 0); 599652284Sobrien else 599752284Sobrien { 599852284Sobrien /* The target must be a REFERENCE_TYPE. Above, build_unary_op 599952284Sobrien will mark the function as addressed, but here we must do it 600052284Sobrien explicitly. */ 6001117395Skan cxx_mark_addressable (fn); 600252284Sobrien 600352284Sobrien return fn; 600452284Sobrien } 600518334Speter} 600618334Speter 600750397Sobrien/* This function will instantiate the type of the expression given in 600850397Sobrien RHS to match the type of LHSTYPE. If errors exist, then return 6009132718Skan error_mark_node. FLAGS is a bit mask. If TF_ERROR is set, then 601090075Sobrien we complain on errors. If we are not complaining, never modify rhs, 601190075Sobrien as overload resolution wants to try many possible instantiations, in 601290075Sobrien the hope that at least one will work. 6013169689Skan 601452284Sobrien For non-recursive calls, LHSTYPE should be a function, pointer to 601552284Sobrien function, or a pointer to member function. */ 601652284Sobrien 601718334Spetertree 6018132718Skaninstantiate_type (tree lhstype, tree rhs, tsubst_flags_t flags) 601918334Speter{ 6020122180Skan tsubst_flags_t flags_in = flags; 6021169689Skan tree access_path = NULL_TREE; 6022169689Skan 602396263Sobrien flags &= ~tf_ptrmem_ok; 6024169689Skan 602518334Speter if (TREE_CODE (lhstype) == UNKNOWN_TYPE) 602618334Speter { 6027132718Skan if (flags & tf_error) 602818334Speter error ("not enough type information"); 602918334Speter return error_mark_node; 603018334Speter } 603118334Speter 603218334Speter if (TREE_TYPE (rhs) != NULL_TREE && ! (type_unknown_p (rhs))) 603350397Sobrien { 6034132718Skan if (same_type_p (lhstype, TREE_TYPE (rhs))) 603550397Sobrien return rhs; 6036169689Skan if (flag_ms_extensions 6037132718Skan && TYPE_PTRMEMFUNC_P (lhstype) 6038132718Skan && !TYPE_PTRMEMFUNC_P (TREE_TYPE (rhs))) 6039132718Skan /* Microsoft allows `A::f' to be resolved to a 6040132718Skan pointer-to-member. */ 6041132718Skan ; 6042132718Skan else 6043132718Skan { 6044132718Skan if (flags & tf_error) 6045169689Skan error ("argument of type %qT does not match %qT", 6046132718Skan TREE_TYPE (rhs), lhstype); 6047132718Skan return error_mark_node; 6048132718Skan } 604950397Sobrien } 605018334Speter 6051117395Skan if (TREE_CODE (rhs) == BASELINK) 6052169689Skan { 6053169689Skan access_path = BASELINK_ACCESS_BINFO (rhs); 6054169689Skan rhs = BASELINK_FUNCTIONS (rhs); 6055169689Skan } 6056117395Skan 6057169689Skan /* If we are in a template, and have a NON_DEPENDENT_EXPR, we cannot 6058169689Skan deduce any type information. */ 6059169689Skan if (TREE_CODE (rhs) == NON_DEPENDENT_EXPR) 6060169689Skan { 6061169689Skan if (flags & tf_error) 6062169689Skan error ("not enough type information"); 6063169689Skan return error_mark_node; 6064169689Skan } 6065169689Skan 6066169689Skan /* There only a few kinds of expressions that may have a type 6067169689Skan dependent on overload resolution. */ 6068169689Skan gcc_assert (TREE_CODE (rhs) == ADDR_EXPR 6069169689Skan || TREE_CODE (rhs) == COMPONENT_REF 6070169689Skan || TREE_CODE (rhs) == COMPOUND_EXPR 6071169689Skan || really_overloaded_fn (rhs)); 6072169689Skan 607350397Sobrien /* We don't overwrite rhs if it is an overloaded function. 607450397Sobrien Copying it would destroy the tree link. */ 607550397Sobrien if (TREE_CODE (rhs) != OVERLOAD) 607650397Sobrien rhs = copy_node (rhs); 607750397Sobrien 607818334Speter /* This should really only be used when attempting to distinguish 607918334Speter what sort of a pointer to function we have. For now, any 608018334Speter arithmetic operation which is not supported on pointers 608118334Speter is rejected as an error. */ 608218334Speter 608318334Speter switch (TREE_CODE (rhs)) 608418334Speter { 608518334Speter case COMPONENT_REF: 6086132718Skan { 6087169689Skan tree member = TREE_OPERAND (rhs, 1); 608818334Speter 6089169689Skan member = instantiate_type (lhstype, member, flags); 6090169689Skan if (member != error_mark_node 6091132718Skan && TREE_SIDE_EFFECTS (TREE_OPERAND (rhs, 0))) 6092132718Skan /* Do not lose object's side effects. */ 6093169689Skan return build2 (COMPOUND_EXPR, TREE_TYPE (member), 6094169689Skan TREE_OPERAND (rhs, 0), member); 6095169689Skan return member; 6096132718Skan } 6097132718Skan 609850397Sobrien case OFFSET_REF: 609952284Sobrien rhs = TREE_OPERAND (rhs, 1); 610052284Sobrien if (BASELINK_P (rhs)) 6101169689Skan return instantiate_type (lhstype, rhs, flags_in); 610252284Sobrien 610350397Sobrien /* This can happen if we are forming a pointer-to-member for a 610450397Sobrien member template. */ 6105169689Skan gcc_assert (TREE_CODE (rhs) == TEMPLATE_ID_EXPR); 610652284Sobrien 610750397Sobrien /* Fall through. */ 610850397Sobrien 610950397Sobrien case TEMPLATE_ID_EXPR: 611090075Sobrien { 611190075Sobrien tree fns = TREE_OPERAND (rhs, 0); 611290075Sobrien tree args = TREE_OPERAND (rhs, 1); 611318334Speter 611490075Sobrien return 6115132718Skan resolve_address_of_overloaded_function (lhstype, fns, flags_in, 6116132718Skan /*template_only=*/true, 6117169689Skan args, access_path); 611890075Sobrien } 611990075Sobrien 612050397Sobrien case OVERLOAD: 6121132718Skan case FUNCTION_DECL: 6122169689Skan return 6123132718Skan resolve_address_of_overloaded_function (lhstype, rhs, flags_in, 6124132718Skan /*template_only=*/false, 6125169689Skan /*explicit_targs=*/NULL_TREE, 6126169689Skan access_path); 612718334Speter 612818334Speter case COMPOUND_EXPR: 612918334Speter TREE_OPERAND (rhs, 0) 613052284Sobrien = instantiate_type (lhstype, TREE_OPERAND (rhs, 0), flags); 613118334Speter if (TREE_OPERAND (rhs, 0) == error_mark_node) 613218334Speter return error_mark_node; 613318334Speter TREE_OPERAND (rhs, 1) 613452284Sobrien = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags); 613518334Speter if (TREE_OPERAND (rhs, 1) == error_mark_node) 613618334Speter return error_mark_node; 613718334Speter 613818334Speter TREE_TYPE (rhs) = lhstype; 613918334Speter return rhs; 614018334Speter 614118334Speter case ADDR_EXPR: 614290075Sobrien { 614390075Sobrien if (PTRMEM_OK_P (rhs)) 6144169689Skan flags |= tf_ptrmem_ok; 6145169689Skan 614652284Sobrien return instantiate_type (lhstype, TREE_OPERAND (rhs, 0), flags); 614790075Sobrien } 614818334Speter 614918334Speter case ERROR_MARK: 615018334Speter return error_mark_node; 615118334Speter 615218334Speter default: 6153169689Skan gcc_unreachable (); 615418334Speter } 6155169689Skan return error_mark_node; 615618334Speter} 615718334Speter 615818334Speter/* Return the name of the virtual function pointer field 615918334Speter (as an IDENTIFIER_NODE) for the given TYPE. Note that 616018334Speter this may have to look back through base types to find the 616118334Speter ultimate field name. (For single inheritance, these could 616218334Speter all be the same name. Who knows for multiple inheritance). */ 616350397Sobrien 616418334Speterstatic tree 6165132718Skanget_vfield_name (tree type) 616618334Speter{ 6167169689Skan tree binfo, base_binfo; 616818334Speter char *buf; 616918334Speter 6170169689Skan for (binfo = TYPE_BINFO (type); 6171169689Skan BINFO_N_BASE_BINFOS (binfo); 6172169689Skan binfo = base_binfo) 6173169689Skan { 6174169689Skan base_binfo = BINFO_BASE_BINFO (binfo, 0); 617518334Speter 6176169689Skan if (BINFO_VIRTUAL_P (base_binfo) 6177169689Skan || !TYPE_CONTAINS_VPTR_P (BINFO_TYPE (base_binfo))) 6178169689Skan break; 6179169689Skan } 6180169689Skan 618118334Speter type = BINFO_TYPE (binfo); 6182169689Skan buf = (char *) alloca (sizeof (VFIELD_NAME_FORMAT) 6183169689Skan + TYPE_NAME_LENGTH (type) + 2); 618496263Sobrien sprintf (buf, VFIELD_NAME_FORMAT, 618596263Sobrien IDENTIFIER_POINTER (constructor_name (type))); 618618334Speter return get_identifier (buf); 618718334Speter} 618818334Speter 618918334Spetervoid 6190132718Skanprint_class_statistics (void) 619118334Speter{ 619218334Speter#ifdef GATHER_STATISTICS 619318334Speter fprintf (stderr, "convert_harshness = %d\n", n_convert_harshness); 619418334Speter fprintf (stderr, "compute_conversion_costs = %d\n", n_compute_conversion_costs); 619518334Speter if (n_vtables) 619618334Speter { 619718334Speter fprintf (stderr, "vtables = %d; vtable searches = %d\n", 619818334Speter n_vtables, n_vtable_searches); 619918334Speter fprintf (stderr, "vtable entries = %d; vtable elems = %d\n", 620018334Speter n_vtable_entries, n_vtable_elems); 620118334Speter } 620218334Speter#endif 620318334Speter} 620418334Speter 620550397Sobrien/* Build a dummy reference to ourselves so Derived::Base (and A::A) works, 620650397Sobrien according to [class]: 6207169689Skan The class-name is also inserted 620850397Sobrien into the scope of the class itself. For purposes of access checking, 620950397Sobrien the inserted class name is treated as if it were a public member name. */ 621050397Sobrien 621152284Sobrienvoid 6212132718Skanbuild_self_reference (void) 621350397Sobrien{ 621450397Sobrien tree name = constructor_name (current_class_type); 621550397Sobrien tree value = build_lang_decl (TYPE_DECL, name, current_class_type); 621652284Sobrien tree saved_cas; 621752284Sobrien 621850397Sobrien DECL_NONLOCAL (value) = 1; 621950397Sobrien DECL_CONTEXT (value) = current_class_type; 622050397Sobrien DECL_ARTIFICIAL (value) = 1; 6221119256Skan SET_DECL_SELF_REFERENCE_P (value); 622250397Sobrien 622390075Sobrien if (processing_template_decl) 622490075Sobrien value = push_template_decl (value); 622590075Sobrien 622652284Sobrien saved_cas = current_access_specifier; 622752284Sobrien current_access_specifier = access_public_node; 622852284Sobrien finish_member_declaration (value); 622952284Sobrien current_access_specifier = saved_cas; 623050397Sobrien} 623150397Sobrien 623250397Sobrien/* Returns 1 if TYPE contains only padding bytes. */ 623350397Sobrien 623450397Sobrienint 6235132718Skanis_empty_class (tree type) 623650397Sobrien{ 623750397Sobrien if (type == error_mark_node) 623850397Sobrien return 0; 623950397Sobrien 624050397Sobrien if (! IS_AGGR_TYPE (type)) 624150397Sobrien return 0; 624250397Sobrien 6243117395Skan /* In G++ 3.2, whether or not a class was empty was determined by 6244117395Skan looking at its size. */ 6245117395Skan if (abi_version_at_least (2)) 6246117395Skan return CLASSTYPE_EMPTY_P (type); 6247117395Skan else 6248117395Skan return integer_zerop (CLASSTYPE_SIZE (type)); 624950397Sobrien} 625052284Sobrien 6251117395Skan/* Returns true if TYPE contains an empty class. */ 6252117395Skan 6253117395Skanstatic bool 6254117395Skancontains_empty_class_p (tree type) 6255117395Skan{ 6256117395Skan if (is_empty_class (type)) 6257117395Skan return true; 6258117395Skan if (CLASS_TYPE_P (type)) 6259117395Skan { 6260117395Skan tree field; 6261169689Skan tree binfo; 6262169689Skan tree base_binfo; 6263117395Skan int i; 6264117395Skan 6265169689Skan for (binfo = TYPE_BINFO (type), i = 0; 6266169689Skan BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i) 6267169689Skan if (contains_empty_class_p (BINFO_TYPE (base_binfo))) 6268117395Skan return true; 6269117395Skan for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field)) 6270117395Skan if (TREE_CODE (field) == FIELD_DECL 6271117395Skan && !DECL_ARTIFICIAL (field) 6272117395Skan && is_empty_class (TREE_TYPE (field))) 6273117395Skan return true; 6274117395Skan } 6275117395Skan else if (TREE_CODE (type) == ARRAY_TYPE) 6276117395Skan return contains_empty_class_p (TREE_TYPE (type)); 6277117395Skan return false; 6278117395Skan} 6279117395Skan 628052284Sobrien/* Note that NAME was looked up while the current class was being 628152284Sobrien defined and that the result of that lookup was DECL. */ 628252284Sobrien 628352284Sobrienvoid 6284132718Skanmaybe_note_name_used_in_class (tree name, tree decl) 628552284Sobrien{ 628652284Sobrien splay_tree names_used; 628752284Sobrien 628852284Sobrien /* If we're not defining a class, there's nothing to do. */ 6289169689Skan if (!(innermost_scope_kind() == sk_class 6290169689Skan && TYPE_BEING_DEFINED (current_class_type))) 629152284Sobrien return; 6292169689Skan 629352284Sobrien /* If there's already a binding for this NAME, then we don't have 629452284Sobrien anything to worry about. */ 6295169689Skan if (lookup_member (current_class_type, name, 6296169689Skan /*protect=*/0, /*want_type=*/false)) 629752284Sobrien return; 629852284Sobrien 629952284Sobrien if (!current_class_stack[current_class_depth - 1].names_used) 630052284Sobrien current_class_stack[current_class_depth - 1].names_used 630152284Sobrien = splay_tree_new (splay_tree_compare_pointers, 0, 0); 630252284Sobrien names_used = current_class_stack[current_class_depth - 1].names_used; 630352284Sobrien 630452284Sobrien splay_tree_insert (names_used, 6305169689Skan (splay_tree_key) name, 630652284Sobrien (splay_tree_value) decl); 630752284Sobrien} 630852284Sobrien 630952284Sobrien/* Note that NAME was declared (as DECL) in the current class. Check 6310117395Skan to see that the declaration is valid. */ 631152284Sobrien 631252284Sobrienvoid 6313132718Skannote_name_declared_in_class (tree name, tree decl) 631452284Sobrien{ 631552284Sobrien splay_tree names_used; 631652284Sobrien splay_tree_node n; 631752284Sobrien 631852284Sobrien /* Look to see if we ever used this name. */ 6319169689Skan names_used 632052284Sobrien = current_class_stack[current_class_depth - 1].names_used; 632152284Sobrien if (!names_used) 632252284Sobrien return; 632352284Sobrien 632452284Sobrien n = splay_tree_lookup (names_used, (splay_tree_key) name); 632552284Sobrien if (n) 632652284Sobrien { 632752284Sobrien /* [basic.scope.class] 6328169689Skan 632952284Sobrien A name N used in a class S shall refer to the same declaration 633052284Sobrien in its context and when re-evaluated in the completed scope of 633152284Sobrien S. */ 6332169689Skan error ("declaration of %q#D", decl); 6333169689Skan error ("changes meaning of %qD from %q+#D", 6334169689Skan DECL_NAME (OVL_CURRENT (decl)), (tree) n->value); 633552284Sobrien } 633652284Sobrien} 633790075Sobrien 633890075Sobrien/* Returns the VAR_DECL for the complete vtable associated with BINFO. 633990075Sobrien Secondary vtables are merged with primary vtables; this function 634090075Sobrien will return the VAR_DECL for the primary vtable. */ 634190075Sobrien 634290075Sobrientree 6343132718Skanget_vtbl_decl_for_binfo (tree binfo) 634490075Sobrien{ 634590075Sobrien tree decl; 634690075Sobrien 634790075Sobrien decl = BINFO_VTABLE (binfo); 634890075Sobrien if (decl && TREE_CODE (decl) == PLUS_EXPR) 634990075Sobrien { 6350169689Skan gcc_assert (TREE_CODE (TREE_OPERAND (decl, 0)) == ADDR_EXPR); 635190075Sobrien decl = TREE_OPERAND (TREE_OPERAND (decl, 0), 0); 635290075Sobrien } 635390075Sobrien if (decl) 6354169689Skan gcc_assert (TREE_CODE (decl) == VAR_DECL); 635590075Sobrien return decl; 635690075Sobrien} 635790075Sobrien 635890075Sobrien 6359132718Skan/* Returns the binfo for the primary base of BINFO. If the resulting 6360132718Skan BINFO is a virtual base, and it is inherited elsewhere in the 6361132718Skan hierarchy, then the returned binfo might not be the primary base of 6362132718Skan BINFO in the complete object. Check BINFO_PRIMARY_P or 6363132718Skan BINFO_LOST_PRIMARY_P to be sure. */ 636490075Sobrien 6365169689Skanstatic tree 6366132718Skanget_primary_binfo (tree binfo) 636790075Sobrien{ 636890075Sobrien tree primary_base; 6369169689Skan 637090075Sobrien primary_base = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (binfo)); 637190075Sobrien if (!primary_base) 637290075Sobrien return NULL_TREE; 637390075Sobrien 6374169689Skan return copied_binfo (primary_base, binfo); 637590075Sobrien} 637690075Sobrien 6377117395Skan/* If INDENTED_P is zero, indent to INDENT. Return nonzero. */ 637890075Sobrien 637990075Sobrienstatic int 6380132718Skanmaybe_indent_hierarchy (FILE * stream, int indent, int indented_p) 638190075Sobrien{ 638290075Sobrien if (!indented_p) 638390075Sobrien fprintf (stream, "%*s", indent, ""); 638490075Sobrien return 1; 638590075Sobrien} 638690075Sobrien 6387132718Skan/* Dump the offsets of all the bases rooted at BINFO to STREAM. 6388132718Skan INDENT should be zero when called from the top level; it is 6389132718Skan incremented recursively. IGO indicates the next expected BINFO in 6390132718Skan inheritance graph ordering. */ 639190075Sobrien 6392132718Skanstatic tree 6393132718Skandump_class_hierarchy_r (FILE *stream, 6394169689Skan int flags, 6395169689Skan tree binfo, 6396169689Skan tree igo, 6397169689Skan int indent) 639890075Sobrien{ 639990075Sobrien int indented = 0; 6400169689Skan tree base_binfo; 6401169689Skan int i; 6402169689Skan 640390075Sobrien indented = maybe_indent_hierarchy (stream, indent, 0); 640490075Sobrien fprintf (stream, "%s (0x%lx) ", 6405169689Skan type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER), 640690075Sobrien (unsigned long) binfo); 6407132718Skan if (binfo != igo) 6408132718Skan { 6409132718Skan fprintf (stream, "alternative-path\n"); 6410132718Skan return igo; 6411132718Skan } 6412132718Skan igo = TREE_CHAIN (binfo); 6413169689Skan 641490075Sobrien fprintf (stream, HOST_WIDE_INT_PRINT_DEC, 641590075Sobrien tree_low_cst (BINFO_OFFSET (binfo), 0)); 641690075Sobrien if (is_empty_class (BINFO_TYPE (binfo))) 641790075Sobrien fprintf (stream, " empty"); 641890075Sobrien else if (CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (binfo))) 641990075Sobrien fprintf (stream, " nearly-empty"); 6420169689Skan if (BINFO_VIRTUAL_P (binfo)) 6421132718Skan fprintf (stream, " virtual"); 642290075Sobrien fprintf (stream, "\n"); 642390075Sobrien 642490075Sobrien indented = 0; 6425169689Skan if (BINFO_PRIMARY_P (binfo)) 642690075Sobrien { 642790075Sobrien indented = maybe_indent_hierarchy (stream, indent + 3, indented); 642890075Sobrien fprintf (stream, " primary-for %s (0x%lx)", 6429169689Skan type_as_string (BINFO_TYPE (BINFO_INHERITANCE_CHAIN (binfo)), 643090075Sobrien TFF_PLAIN_IDENTIFIER), 6431169689Skan (unsigned long)BINFO_INHERITANCE_CHAIN (binfo)); 643290075Sobrien } 643390075Sobrien if (BINFO_LOST_PRIMARY_P (binfo)) 643490075Sobrien { 643590075Sobrien indented = maybe_indent_hierarchy (stream, indent + 3, indented); 643690075Sobrien fprintf (stream, " lost-primary"); 643790075Sobrien } 643890075Sobrien if (indented) 643990075Sobrien fprintf (stream, "\n"); 644090075Sobrien 644190075Sobrien if (!(flags & TDF_SLIM)) 644290075Sobrien { 644390075Sobrien int indented = 0; 6444169689Skan 644590075Sobrien if (BINFO_SUBVTT_INDEX (binfo)) 644690075Sobrien { 644790075Sobrien indented = maybe_indent_hierarchy (stream, indent + 3, indented); 644890075Sobrien fprintf (stream, " subvttidx=%s", 644990075Sobrien expr_as_string (BINFO_SUBVTT_INDEX (binfo), 645090075Sobrien TFF_PLAIN_IDENTIFIER)); 645190075Sobrien } 645290075Sobrien if (BINFO_VPTR_INDEX (binfo)) 645390075Sobrien { 645490075Sobrien indented = maybe_indent_hierarchy (stream, indent + 3, indented); 645590075Sobrien fprintf (stream, " vptridx=%s", 645690075Sobrien expr_as_string (BINFO_VPTR_INDEX (binfo), 645790075Sobrien TFF_PLAIN_IDENTIFIER)); 645890075Sobrien } 645990075Sobrien if (BINFO_VPTR_FIELD (binfo)) 646090075Sobrien { 646190075Sobrien indented = maybe_indent_hierarchy (stream, indent + 3, indented); 646290075Sobrien fprintf (stream, " vbaseoffset=%s", 646390075Sobrien expr_as_string (BINFO_VPTR_FIELD (binfo), 646490075Sobrien TFF_PLAIN_IDENTIFIER)); 646590075Sobrien } 646690075Sobrien if (BINFO_VTABLE (binfo)) 646790075Sobrien { 646890075Sobrien indented = maybe_indent_hierarchy (stream, indent + 3, indented); 646990075Sobrien fprintf (stream, " vptr=%s", 647090075Sobrien expr_as_string (BINFO_VTABLE (binfo), 647190075Sobrien TFF_PLAIN_IDENTIFIER)); 647290075Sobrien } 6473169689Skan 647490075Sobrien if (indented) 647590075Sobrien fprintf (stream, "\n"); 647690075Sobrien } 647790075Sobrien 6478169689Skan for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++) 6479169689Skan igo = dump_class_hierarchy_r (stream, flags, base_binfo, igo, indent + 2); 6480132718Skan 6481132718Skan return igo; 648290075Sobrien} 648390075Sobrien 648490075Sobrien/* Dump the BINFO hierarchy for T. */ 648590075Sobrien 648690075Sobrienstatic void 6487132718Skandump_class_hierarchy_1 (FILE *stream, int flags, tree t) 648890075Sobrien{ 648990075Sobrien fprintf (stream, "Class %s\n", type_as_string (t, TFF_PLAIN_IDENTIFIER)); 649090075Sobrien fprintf (stream, " size=%lu align=%lu\n", 649190075Sobrien (unsigned long)(tree_low_cst (TYPE_SIZE (t), 0) / BITS_PER_UNIT), 649290075Sobrien (unsigned long)(TYPE_ALIGN (t) / BITS_PER_UNIT)); 6493132718Skan fprintf (stream, " base size=%lu base align=%lu\n", 6494132718Skan (unsigned long)(tree_low_cst (TYPE_SIZE (CLASSTYPE_AS_BASE (t)), 0) 6495132718Skan / BITS_PER_UNIT), 6496132718Skan (unsigned long)(TYPE_ALIGN (CLASSTYPE_AS_BASE (t)) 6497132718Skan / BITS_PER_UNIT)); 6498132718Skan dump_class_hierarchy_r (stream, flags, TYPE_BINFO (t), TYPE_BINFO (t), 0); 649990075Sobrien fprintf (stream, "\n"); 650090075Sobrien} 650190075Sobrien 6502132718Skan/* Debug interface to hierarchy dumping. */ 6503132718Skan 6504169689Skanvoid 6505132718Skandebug_class (tree t) 6506132718Skan{ 6507132718Skan dump_class_hierarchy_1 (stderr, TDF_SLIM, t); 6508132718Skan} 6509132718Skan 651090075Sobrienstatic void 6511132718Skandump_class_hierarchy (tree t) 651290075Sobrien{ 6513132718Skan int flags; 6514132718Skan FILE *stream = dump_begin (TDI_class, &flags); 6515132718Skan 6516132718Skan if (stream) 6517132718Skan { 6518132718Skan dump_class_hierarchy_1 (stream, flags, t); 6519132718Skan dump_end (TDI_class, stream); 6520132718Skan } 6521132718Skan} 6522132718Skan 6523132718Skanstatic void 6524132718Skandump_array (FILE * stream, tree decl) 6525132718Skan{ 6526169689Skan tree value; 6527169689Skan unsigned HOST_WIDE_INT ix; 652890075Sobrien HOST_WIDE_INT elt; 652990075Sobrien tree size = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (decl))); 653090075Sobrien 653190075Sobrien elt = (tree_low_cst (TYPE_SIZE (TREE_TYPE (TREE_TYPE (decl))), 0) 653290075Sobrien / BITS_PER_UNIT); 653390075Sobrien fprintf (stream, "%s:", decl_as_string (decl, TFF_PLAIN_IDENTIFIER)); 653490075Sobrien fprintf (stream, " %s entries", 653590075Sobrien expr_as_string (size_binop (PLUS_EXPR, size, size_one_node), 653690075Sobrien TFF_PLAIN_IDENTIFIER)); 653790075Sobrien fprintf (stream, "\n"); 653890075Sobrien 6539169689Skan FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)), 6540169689Skan ix, value) 654190075Sobrien fprintf (stream, "%-4ld %s\n", (long)(ix * elt), 6542169689Skan expr_as_string (value, TFF_PLAIN_IDENTIFIER)); 654390075Sobrien} 654490075Sobrien 654590075Sobrienstatic void 6546132718Skandump_vtable (tree t, tree binfo, tree vtable) 654790075Sobrien{ 654890075Sobrien int flags; 654990075Sobrien FILE *stream = dump_begin (TDI_class, &flags); 655090075Sobrien 655190075Sobrien if (!stream) 655290075Sobrien return; 655390075Sobrien 655490075Sobrien if (!(flags & TDF_SLIM)) 655590075Sobrien { 655690075Sobrien int ctor_vtbl_p = TYPE_BINFO (t) != binfo; 6557169689Skan 655890075Sobrien fprintf (stream, "%s for %s", 655990075Sobrien ctor_vtbl_p ? "Construction vtable" : "Vtable", 6560169689Skan type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER)); 656190075Sobrien if (ctor_vtbl_p) 656290075Sobrien { 6563169689Skan if (!BINFO_VIRTUAL_P (binfo)) 656490075Sobrien fprintf (stream, " (0x%lx instance)", (unsigned long)binfo); 656590075Sobrien fprintf (stream, " in %s", type_as_string (t, TFF_PLAIN_IDENTIFIER)); 656690075Sobrien } 656790075Sobrien fprintf (stream, "\n"); 656890075Sobrien dump_array (stream, vtable); 656990075Sobrien fprintf (stream, "\n"); 657090075Sobrien } 6571169689Skan 657290075Sobrien dump_end (TDI_class, stream); 657390075Sobrien} 657490075Sobrien 657590075Sobrienstatic void 6576132718Skandump_vtt (tree t, tree vtt) 657790075Sobrien{ 657890075Sobrien int flags; 657990075Sobrien FILE *stream = dump_begin (TDI_class, &flags); 658090075Sobrien 658190075Sobrien if (!stream) 658290075Sobrien return; 658390075Sobrien 658490075Sobrien if (!(flags & TDF_SLIM)) 658590075Sobrien { 658690075Sobrien fprintf (stream, "VTT for %s\n", 658790075Sobrien type_as_string (t, TFF_PLAIN_IDENTIFIER)); 658890075Sobrien dump_array (stream, vtt); 658990075Sobrien fprintf (stream, "\n"); 659090075Sobrien } 6591169689Skan 659290075Sobrien dump_end (TDI_class, stream); 659390075Sobrien} 659490075Sobrien 6595132718Skan/* Dump a function or thunk and its thunkees. */ 6596132718Skan 6597132718Skanstatic void 6598132718Skandump_thunk (FILE *stream, int indent, tree thunk) 6599132718Skan{ 6600132718Skan static const char spaces[] = " "; 6601132718Skan tree name = DECL_NAME (thunk); 6602132718Skan tree thunks; 6603169689Skan 6604132718Skan fprintf (stream, "%.*s%p %s %s", indent, spaces, 6605132718Skan (void *)thunk, 6606132718Skan !DECL_THUNK_P (thunk) ? "function" 6607132718Skan : DECL_THIS_THUNK_P (thunk) ? "this-thunk" : "covariant-thunk", 6608132718Skan name ? IDENTIFIER_POINTER (name) : "<unset>"); 6609132718Skan if (DECL_THUNK_P (thunk)) 6610132718Skan { 6611132718Skan HOST_WIDE_INT fixed_adjust = THUNK_FIXED_OFFSET (thunk); 6612132718Skan tree virtual_adjust = THUNK_VIRTUAL_OFFSET (thunk); 6613132718Skan 6614132718Skan fprintf (stream, " fixed=" HOST_WIDE_INT_PRINT_DEC, fixed_adjust); 6615132718Skan if (!virtual_adjust) 6616132718Skan /*NOP*/; 6617132718Skan else if (DECL_THIS_THUNK_P (thunk)) 6618132718Skan fprintf (stream, " vcall=" HOST_WIDE_INT_PRINT_DEC, 6619132718Skan tree_low_cst (virtual_adjust, 0)); 6620132718Skan else 6621132718Skan fprintf (stream, " vbase=" HOST_WIDE_INT_PRINT_DEC "(%s)", 6622132718Skan tree_low_cst (BINFO_VPTR_FIELD (virtual_adjust), 0), 6623132718Skan type_as_string (BINFO_TYPE (virtual_adjust), TFF_SCOPE)); 6624132718Skan if (THUNK_ALIAS (thunk)) 6625132718Skan fprintf (stream, " alias to %p", (void *)THUNK_ALIAS (thunk)); 6626132718Skan } 6627132718Skan fprintf (stream, "\n"); 6628132718Skan for (thunks = DECL_THUNKS (thunk); thunks; thunks = TREE_CHAIN (thunks)) 6629132718Skan dump_thunk (stream, indent + 2, thunks); 6630132718Skan} 6631132718Skan 6632132718Skan/* Dump the thunks for FN. */ 6633132718Skan 6634169689Skanvoid 6635132718Skandebug_thunks (tree fn) 6636132718Skan{ 6637132718Skan dump_thunk (stderr, 0, fn); 6638132718Skan} 6639132718Skan 664090075Sobrien/* Virtual function table initialization. */ 664190075Sobrien 664290075Sobrien/* Create all the necessary vtables for T and its base classes. */ 664390075Sobrien 664490075Sobrienstatic void 6645132718Skanfinish_vtbls (tree t) 664690075Sobrien{ 664790075Sobrien tree list; 664890075Sobrien tree vbase; 664990075Sobrien 665090075Sobrien /* We lay out the primary and secondary vtables in one contiguous 665190075Sobrien vtable. The primary vtable is first, followed by the non-virtual 665290075Sobrien secondary vtables in inheritance graph order. */ 6653169689Skan list = build_tree_list (BINFO_VTABLE (TYPE_BINFO (t)), NULL_TREE); 665490075Sobrien accumulate_vtbl_inits (TYPE_BINFO (t), TYPE_BINFO (t), 665590075Sobrien TYPE_BINFO (t), t, list); 6656169689Skan 665790075Sobrien /* Then come the virtual bases, also in inheritance graph order. */ 665890075Sobrien for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase)) 665990075Sobrien { 6660169689Skan if (!BINFO_VIRTUAL_P (vbase)) 666190075Sobrien continue; 6662132718Skan accumulate_vtbl_inits (vbase, vbase, TYPE_BINFO (t), t, list); 666390075Sobrien } 666490075Sobrien 6665169689Skan if (BINFO_VTABLE (TYPE_BINFO (t))) 666690075Sobrien initialize_vtable (TYPE_BINFO (t), TREE_VALUE (list)); 666790075Sobrien} 666890075Sobrien 666990075Sobrien/* Initialize the vtable for BINFO with the INITS. */ 667090075Sobrien 667190075Sobrienstatic void 6672132718Skaninitialize_vtable (tree binfo, tree inits) 667390075Sobrien{ 667490075Sobrien tree decl; 667590075Sobrien 667690075Sobrien layout_vtable_decl (binfo, list_length (inits)); 667790075Sobrien decl = get_vtbl_decl_for_binfo (binfo); 6678169689Skan initialize_artificial_var (decl, inits); 667990075Sobrien dump_vtable (BINFO_TYPE (binfo), binfo, decl); 668090075Sobrien} 668190075Sobrien 668290075Sobrien/* Build the VTT (virtual table table) for T. 668390075Sobrien A class requires a VTT if it has virtual bases. 6684169689Skan 668590075Sobrien This holds 668690075Sobrien 1 - primary virtual pointer for complete object T 668790075Sobrien 2 - secondary VTTs for each direct non-virtual base of T which requires a 668890075Sobrien VTT 668990075Sobrien 3 - secondary virtual pointers for each direct or indirect base of T which 669090075Sobrien has virtual bases or is reachable via a virtual path from T. 669190075Sobrien 4 - secondary VTTs for each direct or indirect virtual base of T. 6692169689Skan 669390075Sobrien Secondary VTTs look like complete object VTTs without part 4. */ 669490075Sobrien 669590075Sobrienstatic void 6696132718Skanbuild_vtt (tree t) 669790075Sobrien{ 669890075Sobrien tree inits; 669990075Sobrien tree type; 670090075Sobrien tree vtt; 670190075Sobrien tree index; 670290075Sobrien 670390075Sobrien /* Build up the initializers for the VTT. */ 670490075Sobrien inits = NULL_TREE; 670590075Sobrien index = size_zero_node; 670690075Sobrien build_vtt_inits (TYPE_BINFO (t), t, &inits, &index); 670790075Sobrien 670890075Sobrien /* If we didn't need a VTT, we're done. */ 670990075Sobrien if (!inits) 671090075Sobrien return; 671190075Sobrien 671290075Sobrien /* Figure out the type of the VTT. */ 671390075Sobrien type = build_index_type (size_int (list_length (inits) - 1)); 671490075Sobrien type = build_cplus_array_type (const_ptr_type_node, type); 6715169689Skan 671690075Sobrien /* Now, build the VTT object itself. */ 6717169689Skan vtt = build_vtable (t, mangle_vtt_for_type (t), type); 6718169689Skan initialize_artificial_var (vtt, inits); 6719117395Skan /* Add the VTT to the vtables list. */ 6720117395Skan TREE_CHAIN (vtt) = TREE_CHAIN (CLASSTYPE_VTABLES (t)); 6721117395Skan TREE_CHAIN (CLASSTYPE_VTABLES (t)) = vtt; 672290075Sobrien 672390075Sobrien dump_vtt (t, vtt); 672490075Sobrien} 672590075Sobrien 672690075Sobrien/* When building a secondary VTT, BINFO_VTABLE is set to a TREE_LIST with 672790075Sobrien PURPOSE the RTTI_BINFO, VALUE the real vtable pointer for this binfo, 672890075Sobrien and CHAIN the vtable pointer for this binfo after construction is 6729117395Skan complete. VALUE can also be another BINFO, in which case we recurse. */ 673090075Sobrien 673190075Sobrienstatic tree 6732132718Skanbinfo_ctor_vtable (tree binfo) 673390075Sobrien{ 673490075Sobrien tree vt; 673590075Sobrien 673690075Sobrien while (1) 673790075Sobrien { 673890075Sobrien vt = BINFO_VTABLE (binfo); 673990075Sobrien if (TREE_CODE (vt) == TREE_LIST) 674090075Sobrien vt = TREE_VALUE (vt); 6741169689Skan if (TREE_CODE (vt) == TREE_BINFO) 674290075Sobrien binfo = vt; 674390075Sobrien else 674490075Sobrien break; 674590075Sobrien } 674690075Sobrien 674790075Sobrien return vt; 674890075Sobrien} 674990075Sobrien 6750169689Skan/* Data for secondary VTT initialization. */ 6751169689Skantypedef struct secondary_vptr_vtt_init_data_s 6752169689Skan{ 6753169689Skan /* Is this the primary VTT? */ 6754169689Skan bool top_level_p; 6755169689Skan 6756169689Skan /* Current index into the VTT. */ 6757169689Skan tree index; 6758169689Skan 6759169689Skan /* TREE_LIST of initializers built up. */ 6760169689Skan tree inits; 6761169689Skan 6762169689Skan /* The type being constructed by this secondary VTT. */ 6763169689Skan tree type_being_constructed; 6764169689Skan} secondary_vptr_vtt_init_data; 6765169689Skan 676690075Sobrien/* Recursively build the VTT-initializer for BINFO (which is in the 676790075Sobrien hierarchy dominated by T). INITS points to the end of the initializer 676890075Sobrien list to date. INDEX is the VTT index where the next element will be 676990075Sobrien replaced. Iff BINFO is the binfo for T, this is the top level VTT (i.e. 677090075Sobrien not a subvtt for some base of T). When that is so, we emit the sub-VTTs 677190075Sobrien for virtual bases of T. When it is not so, we build the constructor 677290075Sobrien vtables for the BINFO-in-T variant. */ 677390075Sobrien 677490075Sobrienstatic tree * 6775169689Skanbuild_vtt_inits (tree binfo, tree t, tree *inits, tree *index) 677690075Sobrien{ 677790075Sobrien int i; 677890075Sobrien tree b; 677990075Sobrien tree init; 678090075Sobrien tree secondary_vptrs; 6781169689Skan secondary_vptr_vtt_init_data data; 6782169689Skan int top_level_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t); 678390075Sobrien 678490075Sobrien /* We only need VTTs for subobjects with virtual bases. */ 6785169689Skan if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))) 678690075Sobrien return inits; 678790075Sobrien 678890075Sobrien /* We need to use a construction vtable if this is not the primary 678990075Sobrien VTT. */ 679090075Sobrien if (!top_level_p) 679190075Sobrien { 679290075Sobrien build_ctor_vtbl_group (binfo, t); 679390075Sobrien 679490075Sobrien /* Record the offset in the VTT where this sub-VTT can be found. */ 679590075Sobrien BINFO_SUBVTT_INDEX (binfo) = *index; 679690075Sobrien } 679790075Sobrien 679890075Sobrien /* Add the address of the primary vtable for the complete object. */ 679990075Sobrien init = binfo_ctor_vtable (binfo); 680090075Sobrien *inits = build_tree_list (NULL_TREE, init); 680190075Sobrien inits = &TREE_CHAIN (*inits); 680290075Sobrien if (top_level_p) 680390075Sobrien { 6804169689Skan gcc_assert (!BINFO_VPTR_INDEX (binfo)); 680590075Sobrien BINFO_VPTR_INDEX (binfo) = *index; 680690075Sobrien } 680790075Sobrien *index = size_binop (PLUS_EXPR, *index, TYPE_SIZE_UNIT (ptr_type_node)); 6808169689Skan 680990075Sobrien /* Recursively add the secondary VTTs for non-virtual bases. */ 6810169689Skan for (i = 0; BINFO_BASE_ITERATE (binfo, i, b); ++i) 6811169689Skan if (!BINFO_VIRTUAL_P (b)) 6812169689Skan inits = build_vtt_inits (b, t, inits, index); 6813169689Skan 681490075Sobrien /* Add secondary virtual pointers for all subobjects of BINFO with 681590075Sobrien either virtual bases or reachable along a virtual path, except 681690075Sobrien subobjects that are non-virtual primary bases. */ 6817169689Skan data.top_level_p = top_level_p; 6818169689Skan data.index = *index; 6819169689Skan data.inits = NULL; 6820169689Skan data.type_being_constructed = BINFO_TYPE (binfo); 682190075Sobrien 6822169689Skan dfs_walk_once (binfo, dfs_build_secondary_vptr_vtt_inits, NULL, &data); 682390075Sobrien 6824169689Skan *index = data.index; 6825169689Skan 682690075Sobrien /* The secondary vptrs come back in reverse order. After we reverse 682790075Sobrien them, and add the INITS, the last init will be the first element 682890075Sobrien of the chain. */ 6829169689Skan secondary_vptrs = data.inits; 683090075Sobrien if (secondary_vptrs) 683190075Sobrien { 683290075Sobrien *inits = nreverse (secondary_vptrs); 683390075Sobrien inits = &TREE_CHAIN (secondary_vptrs); 6834169689Skan gcc_assert (*inits == NULL_TREE); 683590075Sobrien } 683690075Sobrien 683790075Sobrien if (top_level_p) 6838169689Skan /* Add the secondary VTTs for virtual bases in inheritance graph 6839169689Skan order. */ 684090075Sobrien for (b = TYPE_BINFO (BINFO_TYPE (binfo)); b; b = TREE_CHAIN (b)) 684190075Sobrien { 6842169689Skan if (!BINFO_VIRTUAL_P (b)) 684390075Sobrien continue; 6844169689Skan 6845132718Skan inits = build_vtt_inits (b, t, inits, index); 684690075Sobrien } 6847169689Skan else 6848169689Skan /* Remove the ctor vtables we created. */ 6849169689Skan dfs_walk_all (binfo, dfs_fixup_binfo_vtbls, NULL, binfo); 685090075Sobrien 685190075Sobrien return inits; 685290075Sobrien} 685390075Sobrien 6854169689Skan/* Called from build_vtt_inits via dfs_walk. BINFO is the binfo for the base 6855169689Skan in most derived. DATA is a SECONDARY_VPTR_VTT_INIT_DATA structure. */ 685690075Sobrien 685790075Sobrienstatic tree 6858169689Skandfs_build_secondary_vptr_vtt_inits (tree binfo, void *data_) 685990075Sobrien{ 6860169689Skan secondary_vptr_vtt_init_data *data = (secondary_vptr_vtt_init_data *)data_; 686190075Sobrien 686290075Sobrien /* We don't care about bases that don't have vtables. */ 686390075Sobrien if (!TYPE_VFIELD (BINFO_TYPE (binfo))) 6864169689Skan return dfs_skip_bases; 686590075Sobrien 6866169689Skan /* We're only interested in proper subobjects of the type being 6867169689Skan constructed. */ 6868169689Skan if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), data->type_being_constructed)) 686990075Sobrien return NULL_TREE; 687090075Sobrien 6871169689Skan /* We're only interested in bases with virtual bases or reachable 6872169689Skan via a virtual path from the type being constructed. */ 6873169689Skan if (!(CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)) 6874169689Skan || binfo_via_virtual (binfo, data->type_being_constructed))) 6875169689Skan return dfs_skip_bases; 6876169689Skan 687790075Sobrien /* We're not interested in non-virtual primary bases. */ 6878169689Skan if (!BINFO_VIRTUAL_P (binfo) && BINFO_PRIMARY_P (binfo)) 687990075Sobrien return NULL_TREE; 688090075Sobrien 688190075Sobrien /* Record the index where this secondary vptr can be found. */ 6882169689Skan if (data->top_level_p) 688390075Sobrien { 6884169689Skan gcc_assert (!BINFO_VPTR_INDEX (binfo)); 6885169689Skan BINFO_VPTR_INDEX (binfo) = data->index; 6886169689Skan 6887169689Skan if (BINFO_VIRTUAL_P (binfo)) 6888169689Skan { 6889169689Skan /* It's a primary virtual base, and this is not a 6890169689Skan construction vtable. Find the base this is primary of in 6891169689Skan the inheritance graph, and use that base's vtable 6892169689Skan now. */ 6893169689Skan while (BINFO_PRIMARY_P (binfo)) 6894169689Skan binfo = BINFO_INHERITANCE_CHAIN (binfo); 6895169689Skan } 689690075Sobrien } 689790075Sobrien 689890075Sobrien /* Add the initializer for the secondary vptr itself. */ 6899169689Skan data->inits = tree_cons (NULL_TREE, binfo_ctor_vtable (binfo), data->inits); 690090075Sobrien 6901169689Skan /* Advance the vtt index. */ 6902169689Skan data->index = size_binop (PLUS_EXPR, data->index, 6903169689Skan TYPE_SIZE_UNIT (ptr_type_node)); 6904169689Skan 690590075Sobrien return NULL_TREE; 690690075Sobrien} 690790075Sobrien 6908169689Skan/* Called from build_vtt_inits via dfs_walk. After building 6909169689Skan constructor vtables and generating the sub-vtt from them, we need 6910169689Skan to restore the BINFO_VTABLES that were scribbled on. DATA is the 6911169689Skan binfo of the base whose sub vtt was generated. */ 691290075Sobrien 691390075Sobrienstatic tree 6914169689Skandfs_fixup_binfo_vtbls (tree binfo, void* data) 691590075Sobrien{ 6916169689Skan tree vtable = BINFO_VTABLE (binfo); 691790075Sobrien 6918169689Skan if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo))) 6919169689Skan /* If this class has no vtable, none of its bases do. */ 6920169689Skan return dfs_skip_bases; 692190075Sobrien 6922169689Skan if (!vtable) 6923169689Skan /* This might be a primary base, so have no vtable in this 6924169689Skan hierarchy. */ 692590075Sobrien return NULL_TREE; 692690075Sobrien 692790075Sobrien /* If we scribbled the construction vtable vptr into BINFO, clear it 692890075Sobrien out now. */ 6929169689Skan if (TREE_CODE (vtable) == TREE_LIST 6930169689Skan && (TREE_PURPOSE (vtable) == (tree) data)) 6931169689Skan BINFO_VTABLE (binfo) = TREE_CHAIN (vtable); 693290075Sobrien 693390075Sobrien return NULL_TREE; 693490075Sobrien} 693590075Sobrien 693690075Sobrien/* Build the construction vtable group for BINFO which is in the 693790075Sobrien hierarchy dominated by T. */ 693890075Sobrien 693990075Sobrienstatic void 6940132718Skanbuild_ctor_vtbl_group (tree binfo, tree t) 694190075Sobrien{ 694290075Sobrien tree list; 694390075Sobrien tree type; 694490075Sobrien tree vtbl; 694590075Sobrien tree inits; 694690075Sobrien tree id; 694790075Sobrien tree vbase; 694890075Sobrien 694990075Sobrien /* See if we've already created this construction vtable group. */ 695090075Sobrien id = mangle_ctor_vtbl_for_type (t, binfo); 695190075Sobrien if (IDENTIFIER_GLOBAL_VALUE (id)) 695290075Sobrien return; 695390075Sobrien 6954169689Skan gcc_assert (!SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t)); 695590075Sobrien /* Build a version of VTBL (with the wrong type) for use in 695690075Sobrien constructing the addresses of secondary vtables in the 695790075Sobrien construction vtable group. */ 695890075Sobrien vtbl = build_vtable (t, id, ptr_type_node); 6959169689Skan DECL_CONSTRUCTION_VTABLE_P (vtbl) = 1; 696090075Sobrien list = build_tree_list (vtbl, NULL_TREE); 696190075Sobrien accumulate_vtbl_inits (binfo, TYPE_BINFO (TREE_TYPE (binfo)), 696290075Sobrien binfo, t, list); 696390075Sobrien 696490075Sobrien /* Add the vtables for each of our virtual bases using the vbase in T 696590075Sobrien binfo. */ 6966169689Skan for (vbase = TYPE_BINFO (BINFO_TYPE (binfo)); 6967169689Skan vbase; 696890075Sobrien vbase = TREE_CHAIN (vbase)) 696990075Sobrien { 697090075Sobrien tree b; 697190075Sobrien 6972169689Skan if (!BINFO_VIRTUAL_P (vbase)) 697390075Sobrien continue; 6974132718Skan b = copied_binfo (vbase, binfo); 6975169689Skan 6976132718Skan accumulate_vtbl_inits (b, vbase, binfo, t, list); 697790075Sobrien } 697890075Sobrien inits = TREE_VALUE (list); 697990075Sobrien 698090075Sobrien /* Figure out the type of the construction vtable. */ 698190075Sobrien type = build_index_type (size_int (list_length (inits) - 1)); 698290075Sobrien type = build_cplus_array_type (vtable_entry_type, type); 698390075Sobrien TREE_TYPE (vtbl) = type; 698490075Sobrien 698590075Sobrien /* Initialize the construction vtable. */ 6986117395Skan CLASSTYPE_VTABLES (t) = chainon (CLASSTYPE_VTABLES (t), vtbl); 6987169689Skan initialize_artificial_var (vtbl, inits); 698890075Sobrien dump_vtable (t, binfo, vtbl); 698990075Sobrien} 699090075Sobrien 699190075Sobrien/* Add the vtbl initializers for BINFO (and its bases other than 699290075Sobrien non-virtual primaries) to the list of INITS. BINFO is in the 699390075Sobrien hierarchy dominated by T. RTTI_BINFO is the binfo within T of 699490075Sobrien the constructor the vtbl inits should be accumulated for. (If this 699590075Sobrien is the complete object vtbl then RTTI_BINFO will be TYPE_BINFO (T).) 699690075Sobrien ORIG_BINFO is the binfo for this object within BINFO_TYPE (RTTI_BINFO). 699790075Sobrien BINFO is the active base equivalent of ORIG_BINFO in the inheritance 699890075Sobrien graph of T. Both BINFO and ORIG_BINFO will have the same BINFO_TYPE, 699990075Sobrien but are not necessarily the same in terms of layout. */ 700090075Sobrien 700190075Sobrienstatic void 7002132718Skanaccumulate_vtbl_inits (tree binfo, 7003169689Skan tree orig_binfo, 7004169689Skan tree rtti_binfo, 7005169689Skan tree t, 7006169689Skan tree inits) 700790075Sobrien{ 700890075Sobrien int i; 7009169689Skan tree base_binfo; 7010169689Skan int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t); 701190075Sobrien 7012169689Skan gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (orig_binfo))); 701390075Sobrien 7014117395Skan /* If it doesn't have a vptr, we don't do anything. */ 701590075Sobrien if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo))) 701690075Sobrien return; 7017169689Skan 701890075Sobrien /* If we're building a construction vtable, we're not interested in 701990075Sobrien subobjects that don't require construction vtables. */ 7020169689Skan if (ctor_vtbl_p 7021169689Skan && !CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)) 702290075Sobrien && !binfo_via_virtual (orig_binfo, BINFO_TYPE (rtti_binfo))) 702390075Sobrien return; 702490075Sobrien 702590075Sobrien /* Build the initializers for the BINFO-in-T vtable. */ 7026169689Skan TREE_VALUE (inits) 702790075Sobrien = chainon (TREE_VALUE (inits), 702890075Sobrien dfs_accumulate_vtbl_inits (binfo, orig_binfo, 702990075Sobrien rtti_binfo, t, inits)); 7030169689Skan 703190075Sobrien /* Walk the BINFO and its bases. We walk in preorder so that as we 703290075Sobrien initialize each vtable we can figure out at what offset the 703390075Sobrien secondary vtable lies from the primary vtable. We can't use 703490075Sobrien dfs_walk here because we need to iterate through bases of BINFO 703590075Sobrien and RTTI_BINFO simultaneously. */ 7036169689Skan for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i) 703790075Sobrien { 703890075Sobrien /* Skip virtual bases. */ 7039169689Skan if (BINFO_VIRTUAL_P (base_binfo)) 704090075Sobrien continue; 704190075Sobrien accumulate_vtbl_inits (base_binfo, 7042169689Skan BINFO_BASE_BINFO (orig_binfo, i), 704390075Sobrien rtti_binfo, t, 704490075Sobrien inits); 704590075Sobrien } 704690075Sobrien} 704790075Sobrien 704890075Sobrien/* Called from accumulate_vtbl_inits. Returns the initializers for 704990075Sobrien the BINFO vtable. */ 705090075Sobrien 705190075Sobrienstatic tree 7052132718Skandfs_accumulate_vtbl_inits (tree binfo, 7053169689Skan tree orig_binfo, 7054169689Skan tree rtti_binfo, 7055169689Skan tree t, 7056169689Skan tree l) 705790075Sobrien{ 705890075Sobrien tree inits = NULL_TREE; 705990075Sobrien tree vtbl = NULL_TREE; 7060169689Skan int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t); 706190075Sobrien 706290075Sobrien if (ctor_vtbl_p 7063169689Skan && BINFO_VIRTUAL_P (orig_binfo) && BINFO_PRIMARY_P (orig_binfo)) 706490075Sobrien { 706590075Sobrien /* In the hierarchy of BINFO_TYPE (RTTI_BINFO), this is a 706690075Sobrien primary virtual base. If it is not the same primary in 706790075Sobrien the hierarchy of T, we'll need to generate a ctor vtable 706890075Sobrien for it, to place at its location in T. If it is the same 706990075Sobrien primary, we still need a VTT entry for the vtable, but it 707090075Sobrien should point to the ctor vtable for the base it is a 707190075Sobrien primary for within the sub-hierarchy of RTTI_BINFO. 7072169689Skan 707390075Sobrien There are three possible cases: 7074169689Skan 707590075Sobrien 1) We are in the same place. 707690075Sobrien 2) We are a primary base within a lost primary virtual base of 707790075Sobrien RTTI_BINFO. 707890075Sobrien 3) We are primary to something not a base of RTTI_BINFO. */ 7079169689Skan 7080169689Skan tree b; 708190075Sobrien tree last = NULL_TREE; 708290075Sobrien 708390075Sobrien /* First, look through the bases we are primary to for RTTI_BINFO 708490075Sobrien or a virtual base. */ 7085169689Skan b = binfo; 7086169689Skan while (BINFO_PRIMARY_P (b)) 708790075Sobrien { 7088169689Skan b = BINFO_INHERITANCE_CHAIN (b); 708990075Sobrien last = b; 7090169689Skan if (BINFO_VIRTUAL_P (b) || b == rtti_binfo) 7091169689Skan goto found; 709290075Sobrien } 709390075Sobrien /* If we run out of primary links, keep looking down our 709490075Sobrien inheritance chain; we might be an indirect primary. */ 7095169689Skan for (b = last; b; b = BINFO_INHERITANCE_CHAIN (b)) 7096169689Skan if (BINFO_VIRTUAL_P (b) || b == rtti_binfo) 7097169689Skan break; 7098169689Skan found: 709990075Sobrien 710090075Sobrien /* If we found RTTI_BINFO, this is case 1. If we found a virtual 710190075Sobrien base B and it is a base of RTTI_BINFO, this is case 2. In 710290075Sobrien either case, we share our vtable with LAST, i.e. the 710390075Sobrien derived-most base within B of which we are a primary. */ 710490075Sobrien if (b == rtti_binfo 7105169689Skan || (b && binfo_for_vbase (BINFO_TYPE (b), BINFO_TYPE (rtti_binfo)))) 710690075Sobrien /* Just set our BINFO_VTABLE to point to LAST, as we may not have 710790075Sobrien set LAST's BINFO_VTABLE yet. We'll extract the actual vptr in 710890075Sobrien binfo_ctor_vtable after everything's been set up. */ 710990075Sobrien vtbl = last; 711090075Sobrien 711190075Sobrien /* Otherwise, this is case 3 and we get our own. */ 711290075Sobrien } 7113132718Skan else if (!BINFO_NEW_VTABLE_MARKED (orig_binfo)) 711490075Sobrien return inits; 711590075Sobrien 711690075Sobrien if (!vtbl) 711790075Sobrien { 711890075Sobrien tree index; 711990075Sobrien int non_fn_entries; 712090075Sobrien 712190075Sobrien /* Compute the initializer for this vtable. */ 712290075Sobrien inits = build_vtbl_initializer (binfo, orig_binfo, t, rtti_binfo, 712390075Sobrien &non_fn_entries); 712490075Sobrien 712590075Sobrien /* Figure out the position to which the VPTR should point. */ 712690075Sobrien vtbl = TREE_PURPOSE (l); 7127169689Skan vtbl = build_address (vtbl); 7128169689Skan /* ??? We should call fold_convert to convert the address to 7129169689Skan vtbl_ptr_type_node, which is the type of elements in the 7130169689Skan vtable. However, the resulting NOP_EXPRs confuse other parts 7131169689Skan of the C++ front end. */ 7132169689Skan gcc_assert (TREE_CODE (vtbl) == ADDR_EXPR); 7133169689Skan TREE_TYPE (vtbl) = vtbl_ptr_type_node; 713490075Sobrien index = size_binop (PLUS_EXPR, 713590075Sobrien size_int (non_fn_entries), 713690075Sobrien size_int (list_length (TREE_VALUE (l)))); 713790075Sobrien index = size_binop (MULT_EXPR, 713890075Sobrien TYPE_SIZE_UNIT (vtable_entry_type), 713990075Sobrien index); 7140169689Skan vtbl = build2 (PLUS_EXPR, TREE_TYPE (vtbl), vtbl, index); 714190075Sobrien } 714290075Sobrien 714390075Sobrien if (ctor_vtbl_p) 714490075Sobrien /* For a construction vtable, we can't overwrite BINFO_VTABLE. 714590075Sobrien So, we make a TREE_LIST. Later, dfs_fixup_binfo_vtbls will 714690075Sobrien straighten this out. */ 714790075Sobrien BINFO_VTABLE (binfo) = tree_cons (rtti_binfo, vtbl, BINFO_VTABLE (binfo)); 7148169689Skan else if (BINFO_PRIMARY_P (binfo) && BINFO_VIRTUAL_P (binfo)) 714990075Sobrien inits = NULL_TREE; 715090075Sobrien else 715190075Sobrien /* For an ordinary vtable, set BINFO_VTABLE. */ 715290075Sobrien BINFO_VTABLE (binfo) = vtbl; 715390075Sobrien 715490075Sobrien return inits; 715590075Sobrien} 715690075Sobrien 7157169689Skanstatic GTY(()) tree abort_fndecl_addr; 7158169689Skan 715990075Sobrien/* Construct the initializer for BINFO's virtual function table. BINFO 716090075Sobrien is part of the hierarchy dominated by T. If we're building a 716190075Sobrien construction vtable, the ORIG_BINFO is the binfo we should use to 716290075Sobrien find the actual function pointers to put in the vtable - but they 716390075Sobrien can be overridden on the path to most-derived in the graph that 716490075Sobrien ORIG_BINFO belongs. Otherwise, 716590075Sobrien ORIG_BINFO should be the same as BINFO. The RTTI_BINFO is the 716690075Sobrien BINFO that should be indicated by the RTTI information in the 716790075Sobrien vtable; it will be a base class of T, rather than T itself, if we 716890075Sobrien are building a construction vtable. 716990075Sobrien 717090075Sobrien The value returned is a TREE_LIST suitable for wrapping in a 717190075Sobrien CONSTRUCTOR to use as the DECL_INITIAL for a vtable. If 717290075Sobrien NON_FN_ENTRIES_P is not NULL, *NON_FN_ENTRIES_P is set to the 7173169689Skan number of non-function entries in the vtable. 717490075Sobrien 717590075Sobrien It might seem that this function should never be called with a 717690075Sobrien BINFO for which BINFO_PRIMARY_P holds, the vtable for such a 717790075Sobrien base is always subsumed by a derived class vtable. However, when 717890075Sobrien we are building construction vtables, we do build vtables for 717990075Sobrien primary bases; we need these while the primary base is being 718090075Sobrien constructed. */ 718190075Sobrien 718290075Sobrienstatic tree 7183132718Skanbuild_vtbl_initializer (tree binfo, 7184169689Skan tree orig_binfo, 7185169689Skan tree t, 7186169689Skan tree rtti_binfo, 7187169689Skan int* non_fn_entries_p) 718890075Sobrien{ 718990075Sobrien tree v, b; 719090075Sobrien tree vfun_inits; 719190075Sobrien vtbl_init_data vid; 7192169689Skan unsigned ix; 7193169689Skan tree vbinfo; 7194169689Skan VEC(tree,gc) *vbases; 719590075Sobrien 719690075Sobrien /* Initialize VID. */ 719790075Sobrien memset (&vid, 0, sizeof (vid)); 719890075Sobrien vid.binfo = binfo; 719990075Sobrien vid.derived = t; 720090075Sobrien vid.rtti_binfo = rtti_binfo; 720190075Sobrien vid.last_init = &vid.inits; 7202169689Skan vid.primary_vtbl_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t); 7203169689Skan vid.ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t); 7204117395Skan vid.generate_vcall_entries = true; 720590075Sobrien /* The first vbase or vcall offset is at index -3 in the vtable. */ 7206169689Skan vid.index = ssize_int(-3 * TARGET_VTABLE_DATA_ENTRY_DISTANCE); 720790075Sobrien 720890075Sobrien /* Add entries to the vtable for RTTI. */ 720990075Sobrien build_rtti_vtbl_entries (binfo, &vid); 721090075Sobrien 721190075Sobrien /* Create an array for keeping track of the functions we've 721290075Sobrien processed. When we see multiple functions with the same 721390075Sobrien signature, we share the vcall offsets. */ 7214169689Skan vid.fns = VEC_alloc (tree, gc, 32); 721590075Sobrien /* Add the vcall and vbase offset entries. */ 721690075Sobrien build_vcall_and_vbase_vtbl_entries (binfo, &vid); 7217169689Skan 721890075Sobrien /* Clear BINFO_VTABLE_PATH_MARKED; it's set by 721990075Sobrien build_vbase_offset_vtbl_entries. */ 7220169689Skan for (vbases = CLASSTYPE_VBASECLASSES (t), ix = 0; 7221169689Skan VEC_iterate (tree, vbases, ix, vbinfo); ix++) 7222169689Skan BINFO_VTABLE_PATH_MARKED (vbinfo) = 0; 722390075Sobrien 7224117395Skan /* If the target requires padding between data entries, add that now. */ 7225117395Skan if (TARGET_VTABLE_DATA_ENTRY_DISTANCE > 1) 7226117395Skan { 7227117395Skan tree cur, *prev; 7228117395Skan 7229117395Skan for (prev = &vid.inits; (cur = *prev); prev = &TREE_CHAIN (cur)) 7230117395Skan { 7231117395Skan tree add = cur; 7232117395Skan int i; 7233117395Skan 7234117395Skan for (i = 1; i < TARGET_VTABLE_DATA_ENTRY_DISTANCE; ++i) 7235117395Skan add = tree_cons (NULL_TREE, 7236117395Skan build1 (NOP_EXPR, vtable_entry_type, 7237117395Skan null_pointer_node), 7238117395Skan add); 7239117395Skan *prev = add; 7240117395Skan } 7241117395Skan } 7242117395Skan 724390075Sobrien if (non_fn_entries_p) 724490075Sobrien *non_fn_entries_p = list_length (vid.inits); 724590075Sobrien 724690075Sobrien /* Go through all the ordinary virtual functions, building up 724790075Sobrien initializers. */ 724890075Sobrien vfun_inits = NULL_TREE; 724990075Sobrien for (v = BINFO_VIRTUALS (orig_binfo); v; v = TREE_CHAIN (v)) 725090075Sobrien { 725190075Sobrien tree delta; 725290075Sobrien tree vcall_index; 7253132718Skan tree fn, fn_original; 725490075Sobrien tree init = NULL_TREE; 7255169689Skan 725690075Sobrien fn = BV_FN (v); 7257132718Skan fn_original = fn; 7258132718Skan if (DECL_THUNK_P (fn)) 7259132718Skan { 7260132718Skan if (!DECL_NAME (fn)) 7261132718Skan finish_thunk (fn); 7262132718Skan if (THUNK_ALIAS (fn)) 7263132718Skan { 7264132718Skan fn = THUNK_ALIAS (fn); 7265132718Skan BV_FN (v) = fn; 7266132718Skan } 7267132718Skan fn_original = THUNK_TARGET (fn); 7268132718Skan } 7269169689Skan 727090075Sobrien /* If the only definition of this function signature along our 727190075Sobrien primary base chain is from a lost primary, this vtable slot will 727290075Sobrien never be used, so just zero it out. This is important to avoid 727390075Sobrien requiring extra thunks which cannot be generated with the function. 727490075Sobrien 727590075Sobrien We first check this in update_vtable_entry_for_fn, so we handle 727690075Sobrien restored primary bases properly; we also need to do it here so we 727790075Sobrien zero out unused slots in ctor vtables, rather than filling themff 727890075Sobrien with erroneous values (though harmless, apart from relocation 727990075Sobrien costs). */ 728090075Sobrien for (b = binfo; ; b = get_primary_binfo (b)) 728190075Sobrien { 728290075Sobrien /* We found a defn before a lost primary; go ahead as normal. */ 7283132718Skan if (look_for_overrides_here (BINFO_TYPE (b), fn_original)) 728490075Sobrien break; 728590075Sobrien 728690075Sobrien /* The nearest definition is from a lost primary; clear the 728790075Sobrien slot. */ 728890075Sobrien if (BINFO_LOST_PRIMARY_P (b)) 728990075Sobrien { 729090075Sobrien init = size_zero_node; 729190075Sobrien break; 729290075Sobrien } 729390075Sobrien } 729490075Sobrien 729590075Sobrien if (! init) 729690075Sobrien { 729790075Sobrien /* Pull the offset for `this', and the function to call, out of 729890075Sobrien the list. */ 729990075Sobrien delta = BV_DELTA (v); 7300117395Skan vcall_index = BV_VCALL_INDEX (v); 730190075Sobrien 7302169689Skan gcc_assert (TREE_CODE (delta) == INTEGER_CST); 7303169689Skan gcc_assert (TREE_CODE (fn) == FUNCTION_DECL); 730490075Sobrien 730590075Sobrien /* You can't call an abstract virtual function; it's abstract. 730690075Sobrien So, we replace these functions with __pure_virtual. */ 7307132718Skan if (DECL_PURE_VIRTUAL_P (fn_original)) 7308132718Skan { 7309169689Skan fn = abort_fndecl; 7310169689Skan if (abort_fndecl_addr == NULL) 7311169689Skan abort_fndecl_addr = build1 (ADDR_EXPR, vfunc_ptr_type_node, fn); 7312169689Skan init = abort_fndecl_addr; 7313132718Skan } 7314169689Skan else 7315169689Skan { 7316169689Skan if (!integer_zerop (delta) || vcall_index) 7317169689Skan { 7318169689Skan fn = make_thunk (fn, /*this_adjusting=*/1, delta, vcall_index); 7319169689Skan if (!DECL_NAME (fn)) 7320169689Skan finish_thunk (fn); 7321169689Skan } 7322169689Skan /* Take the address of the function, considering it to be of an 7323169689Skan appropriate generic type. */ 7324169689Skan init = build1 (ADDR_EXPR, vfunc_ptr_type_node, fn); 7325169689Skan } 732690075Sobrien } 732790075Sobrien 732890075Sobrien /* And add it to the chain of initializers. */ 732990075Sobrien if (TARGET_VTABLE_USES_DESCRIPTORS) 733090075Sobrien { 733190075Sobrien int i; 733290075Sobrien if (init == size_zero_node) 733390075Sobrien for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i) 733490075Sobrien vfun_inits = tree_cons (NULL_TREE, init, vfun_inits); 733590075Sobrien else 733690075Sobrien for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i) 733790075Sobrien { 7338169689Skan tree fdesc = build2 (FDESC_EXPR, vfunc_ptr_type_node, 7339169689Skan TREE_OPERAND (init, 0), 7340169689Skan build_int_cst (NULL_TREE, i)); 734190075Sobrien TREE_CONSTANT (fdesc) = 1; 7342169689Skan TREE_INVARIANT (fdesc) = 1; 734390075Sobrien 734490075Sobrien vfun_inits = tree_cons (NULL_TREE, fdesc, vfun_inits); 734590075Sobrien } 734690075Sobrien } 734790075Sobrien else 7348169689Skan vfun_inits = tree_cons (NULL_TREE, init, vfun_inits); 734990075Sobrien } 735090075Sobrien 735190075Sobrien /* The initializers for virtual functions were built up in reverse 735290075Sobrien order; straighten them out now. */ 735390075Sobrien vfun_inits = nreverse (vfun_inits); 7354169689Skan 735590075Sobrien /* The negative offset initializers are also in reverse order. */ 735690075Sobrien vid.inits = nreverse (vid.inits); 735790075Sobrien 735890075Sobrien /* Chain the two together. */ 735990075Sobrien return chainon (vid.inits, vfun_inits); 736090075Sobrien} 736190075Sobrien 736290075Sobrien/* Adds to vid->inits the initializers for the vbase and vcall 736390075Sobrien offsets in BINFO, which is in the hierarchy dominated by T. */ 736490075Sobrien 736590075Sobrienstatic void 7366132718Skanbuild_vcall_and_vbase_vtbl_entries (tree binfo, vtbl_init_data* vid) 736790075Sobrien{ 736890075Sobrien tree b; 736990075Sobrien 737090075Sobrien /* If this is a derived class, we must first create entries 737190075Sobrien corresponding to the primary base class. */ 737290075Sobrien b = get_primary_binfo (binfo); 737390075Sobrien if (b) 737490075Sobrien build_vcall_and_vbase_vtbl_entries (b, vid); 737590075Sobrien 737690075Sobrien /* Add the vbase entries for this base. */ 737790075Sobrien build_vbase_offset_vtbl_entries (binfo, vid); 737890075Sobrien /* Add the vcall entries for this base. */ 737990075Sobrien build_vcall_offset_vtbl_entries (binfo, vid); 738090075Sobrien} 738190075Sobrien 738290075Sobrien/* Returns the initializers for the vbase offset entries in the vtable 738390075Sobrien for BINFO (which is part of the class hierarchy dominated by T), in 738490075Sobrien reverse order. VBASE_OFFSET_INDEX gives the vtable index 738590075Sobrien where the next vbase offset will go. */ 738690075Sobrien 738790075Sobrienstatic void 7388132718Skanbuild_vbase_offset_vtbl_entries (tree binfo, vtbl_init_data* vid) 738990075Sobrien{ 739090075Sobrien tree vbase; 739190075Sobrien tree t; 739290075Sobrien tree non_primary_binfo; 739390075Sobrien 739490075Sobrien /* If there are no virtual baseclasses, then there is nothing to 739590075Sobrien do. */ 7396169689Skan if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))) 739790075Sobrien return; 739890075Sobrien 739990075Sobrien t = vid->derived; 7400169689Skan 740190075Sobrien /* We might be a primary base class. Go up the inheritance hierarchy 740290075Sobrien until we find the most derived class of which we are a primary base: 740390075Sobrien it is the offset of that which we need to use. */ 740490075Sobrien non_primary_binfo = binfo; 740590075Sobrien while (BINFO_INHERITANCE_CHAIN (non_primary_binfo)) 740690075Sobrien { 740790075Sobrien tree b; 740890075Sobrien 740990075Sobrien /* If we have reached a virtual base, then it must be a primary 741090075Sobrien base (possibly multi-level) of vid->binfo, or we wouldn't 741190075Sobrien have called build_vcall_and_vbase_vtbl_entries for it. But it 741290075Sobrien might be a lost primary, so just skip down to vid->binfo. */ 7413169689Skan if (BINFO_VIRTUAL_P (non_primary_binfo)) 741490075Sobrien { 741590075Sobrien non_primary_binfo = vid->binfo; 741690075Sobrien break; 741790075Sobrien } 741890075Sobrien 741990075Sobrien b = BINFO_INHERITANCE_CHAIN (non_primary_binfo); 742090075Sobrien if (get_primary_binfo (b) != non_primary_binfo) 742190075Sobrien break; 742290075Sobrien non_primary_binfo = b; 742390075Sobrien } 742490075Sobrien 742590075Sobrien /* Go through the virtual bases, adding the offsets. */ 742690075Sobrien for (vbase = TYPE_BINFO (BINFO_TYPE (binfo)); 742790075Sobrien vbase; 742890075Sobrien vbase = TREE_CHAIN (vbase)) 742990075Sobrien { 743090075Sobrien tree b; 743190075Sobrien tree delta; 7432169689Skan 7433169689Skan if (!BINFO_VIRTUAL_P (vbase)) 743490075Sobrien continue; 743590075Sobrien 743690075Sobrien /* Find the instance of this virtual base in the complete 743790075Sobrien object. */ 7438132718Skan b = copied_binfo (vbase, binfo); 743990075Sobrien 744090075Sobrien /* If we've already got an offset for this virtual base, we 744190075Sobrien don't need another one. */ 744290075Sobrien if (BINFO_VTABLE_PATH_MARKED (b)) 744390075Sobrien continue; 7444132718Skan BINFO_VTABLE_PATH_MARKED (b) = 1; 744590075Sobrien 744690075Sobrien /* Figure out where we can find this vbase offset. */ 7447169689Skan delta = size_binop (MULT_EXPR, 744890075Sobrien vid->index, 744990075Sobrien convert (ssizetype, 745090075Sobrien TYPE_SIZE_UNIT (vtable_entry_type))); 745190075Sobrien if (vid->primary_vtbl_p) 745290075Sobrien BINFO_VPTR_FIELD (b) = delta; 745390075Sobrien 745490075Sobrien if (binfo != TYPE_BINFO (t)) 7455169689Skan /* The vbase offset had better be the same. */ 7456169689Skan gcc_assert (tree_int_cst_equal (delta, BINFO_VPTR_FIELD (vbase))); 745790075Sobrien 745890075Sobrien /* The next vbase will come at a more negative offset. */ 7459117395Skan vid->index = size_binop (MINUS_EXPR, vid->index, 7460117395Skan ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE)); 746190075Sobrien 746290075Sobrien /* The initializer is the delta from BINFO to this virtual base. 746390075Sobrien The vbase offsets go in reverse inheritance-graph order, and 746490075Sobrien we are walking in inheritance graph order so these end up in 746590075Sobrien the right order. */ 746690075Sobrien delta = size_diffop (BINFO_OFFSET (b), BINFO_OFFSET (non_primary_binfo)); 7467169689Skan 7468169689Skan *vid->last_init 746990075Sobrien = build_tree_list (NULL_TREE, 7470169689Skan fold_build1 (NOP_EXPR, 7471169689Skan vtable_entry_type, 7472169689Skan delta)); 747390075Sobrien vid->last_init = &TREE_CHAIN (*vid->last_init); 747490075Sobrien } 747590075Sobrien} 747690075Sobrien 747790075Sobrien/* Adds the initializers for the vcall offset entries in the vtable 747890075Sobrien for BINFO (which is part of the class hierarchy dominated by VID->DERIVED) 747990075Sobrien to VID->INITS. */ 748090075Sobrien 748190075Sobrienstatic void 7482132718Skanbuild_vcall_offset_vtbl_entries (tree binfo, vtbl_init_data* vid) 748390075Sobrien{ 7484117395Skan /* We only need these entries if this base is a virtual base. We 7485117395Skan compute the indices -- but do not add to the vtable -- when 7486117395Skan building the main vtable for a class. */ 7487169689Skan if (BINFO_VIRTUAL_P (binfo) || binfo == TYPE_BINFO (vid->derived)) 7488117395Skan { 7489117395Skan /* We need a vcall offset for each of the virtual functions in this 7490117395Skan vtable. For example: 749190075Sobrien 7492117395Skan class A { virtual void f (); }; 7493117395Skan class B1 : virtual public A { virtual void f (); }; 7494117395Skan class B2 : virtual public A { virtual void f (); }; 7495117395Skan class C: public B1, public B2 { virtual void f (); }; 749690075Sobrien 7497117395Skan A C object has a primary base of B1, which has a primary base of A. A 7498117395Skan C also has a secondary base of B2, which no longer has a primary base 7499117395Skan of A. So the B2-in-C construction vtable needs a secondary vtable for 7500117395Skan A, which will adjust the A* to a B2* to call f. We have no way of 7501117395Skan knowing what (or even whether) this offset will be when we define B2, 7502117395Skan so we store this "vcall offset" in the A sub-vtable and look it up in 7503117395Skan a "virtual thunk" for B2::f. 750490075Sobrien 7505117395Skan We need entries for all the functions in our primary vtable and 7506117395Skan in our non-virtual bases' secondary vtables. */ 7507117395Skan vid->vbase = binfo; 7508117395Skan /* If we are just computing the vcall indices -- but do not need 7509117395Skan the actual entries -- not that. */ 7510169689Skan if (!BINFO_VIRTUAL_P (binfo)) 7511117395Skan vid->generate_vcall_entries = false; 7512117395Skan /* Now, walk through the non-virtual bases, adding vcall offsets. */ 7513117395Skan add_vcall_offset_vtbl_entries_r (binfo, vid); 7514117395Skan } 751590075Sobrien} 751690075Sobrien 751790075Sobrien/* Build vcall offsets, starting with those for BINFO. */ 751890075Sobrien 751990075Sobrienstatic void 7520132718Skanadd_vcall_offset_vtbl_entries_r (tree binfo, vtbl_init_data* vid) 752190075Sobrien{ 752290075Sobrien int i; 752390075Sobrien tree primary_binfo; 7524169689Skan tree base_binfo; 752590075Sobrien 752690075Sobrien /* Don't walk into virtual bases -- except, of course, for the 752790075Sobrien virtual base for which we are building vcall offsets. Any 752890075Sobrien primary virtual base will have already had its offsets generated 752990075Sobrien through the recursion in build_vcall_and_vbase_vtbl_entries. */ 7530169689Skan if (BINFO_VIRTUAL_P (binfo) && vid->vbase != binfo) 753190075Sobrien return; 7532169689Skan 753390075Sobrien /* If BINFO has a primary base, process it first. */ 753490075Sobrien primary_binfo = get_primary_binfo (binfo); 753590075Sobrien if (primary_binfo) 753690075Sobrien add_vcall_offset_vtbl_entries_r (primary_binfo, vid); 753790075Sobrien 753890075Sobrien /* Add BINFO itself to the list. */ 753990075Sobrien add_vcall_offset_vtbl_entries_1 (binfo, vid); 754090075Sobrien 754190075Sobrien /* Scan the non-primary bases of BINFO. */ 7542169689Skan for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i) 7543169689Skan if (base_binfo != primary_binfo) 7544169689Skan add_vcall_offset_vtbl_entries_r (base_binfo, vid); 754590075Sobrien} 754690075Sobrien 754790075Sobrien/* Called from build_vcall_offset_vtbl_entries_r. */ 754890075Sobrien 754990075Sobrienstatic void 7550132718Skanadd_vcall_offset_vtbl_entries_1 (tree binfo, vtbl_init_data* vid) 755190075Sobrien{ 7552117395Skan /* Make entries for the rest of the virtuals. */ 7553117395Skan if (abi_version_at_least (2)) 7554117395Skan { 7555117395Skan tree orig_fn; 755690075Sobrien 7557117395Skan /* The ABI requires that the methods be processed in declaration 7558117395Skan order. G++ 3.2 used the order in the vtable. */ 7559117395Skan for (orig_fn = TYPE_METHODS (BINFO_TYPE (binfo)); 7560117395Skan orig_fn; 7561117395Skan orig_fn = TREE_CHAIN (orig_fn)) 7562117395Skan if (DECL_VINDEX (orig_fn)) 7563117395Skan add_vcall_offset (orig_fn, binfo, vid); 7564117395Skan } 7565117395Skan else 756690075Sobrien { 7567117395Skan tree derived_virtuals; 7568117395Skan tree base_virtuals; 7569117395Skan tree orig_virtuals; 7570117395Skan /* If BINFO is a primary base, the most derived class which has 7571117395Skan BINFO as a primary base; otherwise, just BINFO. */ 7572117395Skan tree non_primary_binfo; 757390075Sobrien 7574117395Skan /* We might be a primary base class. Go up the inheritance hierarchy 7575117395Skan until we find the most derived class of which we are a primary base: 7576117395Skan it is the BINFO_VIRTUALS there that we need to consider. */ 7577117395Skan non_primary_binfo = binfo; 7578117395Skan while (BINFO_INHERITANCE_CHAIN (non_primary_binfo)) 757990075Sobrien { 7580117395Skan tree b; 7581117395Skan 7582117395Skan /* If we have reached a virtual base, then it must be vid->vbase, 7583117395Skan because we ignore other virtual bases in 7584117395Skan add_vcall_offset_vtbl_entries_r. In turn, it must be a primary 7585117395Skan base (possibly multi-level) of vid->binfo, or we wouldn't 7586117395Skan have called build_vcall_and_vbase_vtbl_entries for it. But it 7587117395Skan might be a lost primary, so just skip down to vid->binfo. */ 7588169689Skan if (BINFO_VIRTUAL_P (non_primary_binfo)) 7589117395Skan { 7590169689Skan gcc_assert (non_primary_binfo == vid->vbase); 7591117395Skan non_primary_binfo = vid->binfo; 7592117395Skan break; 7593117395Skan } 7594117395Skan 7595117395Skan b = BINFO_INHERITANCE_CHAIN (non_primary_binfo); 7596117395Skan if (get_primary_binfo (b) != non_primary_binfo) 7597117395Skan break; 7598117395Skan non_primary_binfo = b; 759990075Sobrien } 760090075Sobrien 7601117395Skan if (vid->ctor_vtbl_p) 7602117395Skan /* For a ctor vtable we need the equivalent binfo within the hierarchy 7603117395Skan where rtti_binfo is the most derived type. */ 7604132718Skan non_primary_binfo 7605132718Skan = original_binfo (non_primary_binfo, vid->rtti_binfo); 7606169689Skan 7607117395Skan for (base_virtuals = BINFO_VIRTUALS (binfo), 7608117395Skan derived_virtuals = BINFO_VIRTUALS (non_primary_binfo), 7609117395Skan orig_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo))); 7610117395Skan base_virtuals; 7611117395Skan base_virtuals = TREE_CHAIN (base_virtuals), 7612117395Skan derived_virtuals = TREE_CHAIN (derived_virtuals), 7613117395Skan orig_virtuals = TREE_CHAIN (orig_virtuals)) 7614117395Skan { 7615117395Skan tree orig_fn; 7616117395Skan 7617117395Skan /* Find the declaration that originally caused this function to 7618117395Skan be present in BINFO_TYPE (binfo). */ 7619117395Skan orig_fn = BV_FN (orig_virtuals); 7620117395Skan 7621117395Skan /* When processing BINFO, we only want to generate vcall slots for 7622117395Skan function slots introduced in BINFO. So don't try to generate 7623117395Skan one if the function isn't even defined in BINFO. */ 7624169689Skan if (!SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), DECL_CONTEXT (orig_fn))) 7625117395Skan continue; 7626117395Skan 7627117395Skan add_vcall_offset (orig_fn, binfo, vid); 7628117395Skan } 762990075Sobrien } 7630117395Skan} 763190075Sobrien 7632117395Skan/* Add a vcall offset entry for ORIG_FN to the vtable. */ 763390075Sobrien 7634117395Skanstatic void 7635117395Skanadd_vcall_offset (tree orig_fn, tree binfo, vtbl_init_data *vid) 7636117395Skan{ 7637117395Skan size_t i; 7638117395Skan tree vcall_offset; 7639169689Skan tree derived_entry; 7640117395Skan 7641117395Skan /* If there is already an entry for a function with the same 7642117395Skan signature as FN, then we do not need a second vcall offset. 7643117395Skan Check the list of functions already present in the derived 7644117395Skan class vtable. */ 7645169689Skan for (i = 0; VEC_iterate (tree, vid->fns, i, derived_entry); ++i) 764690075Sobrien { 7647117395Skan if (same_signature_p (derived_entry, orig_fn) 7648117395Skan /* We only use one vcall offset for virtual destructors, 7649117395Skan even though there are two virtual table entries. */ 7650117395Skan || (DECL_DESTRUCTOR_P (derived_entry) 7651117395Skan && DECL_DESTRUCTOR_P (orig_fn))) 7652117395Skan return; 7653117395Skan } 765490075Sobrien 7655117395Skan /* If we are building these vcall offsets as part of building 7656117395Skan the vtable for the most derived class, remember the vcall 7657117395Skan offset. */ 7658117395Skan if (vid->binfo == TYPE_BINFO (vid->derived)) 7659169689Skan { 7660169689Skan tree_pair_p elt = VEC_safe_push (tree_pair_s, gc, 7661169689Skan CLASSTYPE_VCALL_INDICES (vid->derived), 7662169689Skan NULL); 7663169689Skan elt->purpose = orig_fn; 7664169689Skan elt->value = vid->index; 7665169689Skan } 766690075Sobrien 7667117395Skan /* The next vcall offset will be found at a more negative 7668117395Skan offset. */ 7669117395Skan vid->index = size_binop (MINUS_EXPR, vid->index, 7670117395Skan ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE)); 7671117395Skan 7672117395Skan /* Keep track of this function. */ 7673169689Skan VEC_safe_push (tree, gc, vid->fns, orig_fn); 7674117395Skan 7675117395Skan if (vid->generate_vcall_entries) 7676117395Skan { 7677117395Skan tree base; 7678117395Skan tree fn; 7679117395Skan 768090075Sobrien /* Find the overriding function. */ 7681117395Skan fn = find_final_overrider (vid->rtti_binfo, binfo, orig_fn); 7682117395Skan if (fn == error_mark_node) 7683117395Skan vcall_offset = build1 (NOP_EXPR, vtable_entry_type, 7684117395Skan integer_zero_node); 7685117395Skan else 768690075Sobrien { 7687117395Skan base = TREE_VALUE (fn); 768890075Sobrien 7689117395Skan /* The vbase we're working on is a primary base of 7690117395Skan vid->binfo. But it might be a lost primary, so its 7691117395Skan BINFO_OFFSET might be wrong, so we just use the 7692117395Skan BINFO_OFFSET from vid->binfo. */ 7693117395Skan vcall_offset = size_diffop (BINFO_OFFSET (base), 7694117395Skan BINFO_OFFSET (vid->binfo)); 7695169689Skan vcall_offset = fold_build1 (NOP_EXPR, vtable_entry_type, 7696169689Skan vcall_offset); 769790075Sobrien } 7698132718Skan /* Add the initializer to the vtable. */ 769990075Sobrien *vid->last_init = build_tree_list (NULL_TREE, vcall_offset); 770090075Sobrien vid->last_init = &TREE_CHAIN (*vid->last_init); 770190075Sobrien } 770290075Sobrien} 770390075Sobrien 7704132718Skan/* Return vtbl initializers for the RTTI entries corresponding to the 770590075Sobrien BINFO's vtable. The RTTI entries should indicate the object given 770690075Sobrien by VID->rtti_binfo. */ 770790075Sobrien 770890075Sobrienstatic void 7709132718Skanbuild_rtti_vtbl_entries (tree binfo, vtbl_init_data* vid) 771090075Sobrien{ 771190075Sobrien tree b; 771290075Sobrien tree t; 771390075Sobrien tree basetype; 771490075Sobrien tree offset; 771590075Sobrien tree decl; 771690075Sobrien tree init; 771790075Sobrien 771890075Sobrien basetype = BINFO_TYPE (binfo); 771990075Sobrien t = BINFO_TYPE (vid->rtti_binfo); 772090075Sobrien 772190075Sobrien /* To find the complete object, we will first convert to our most 772290075Sobrien primary base, and then add the offset in the vtbl to that value. */ 772390075Sobrien b = binfo; 772490075Sobrien while (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (b)) 7725169689Skan && !BINFO_LOST_PRIMARY_P (b)) 772690075Sobrien { 772790075Sobrien tree primary_base; 772890075Sobrien 772990075Sobrien primary_base = get_primary_binfo (b); 7730169689Skan gcc_assert (BINFO_PRIMARY_P (primary_base) 7731169689Skan && BINFO_INHERITANCE_CHAIN (primary_base) == b); 773290075Sobrien b = primary_base; 773390075Sobrien } 773490075Sobrien offset = size_diffop (BINFO_OFFSET (vid->rtti_binfo), BINFO_OFFSET (b)); 773590075Sobrien 773690075Sobrien /* The second entry is the address of the typeinfo object. */ 773790075Sobrien if (flag_rtti) 7738117395Skan decl = build_address (get_tinfo_decl (t)); 773990075Sobrien else 774090075Sobrien decl = integer_zero_node; 7741169689Skan 774290075Sobrien /* Convert the declaration to a type that can be stored in the 774390075Sobrien vtable. */ 7744117395Skan init = build_nop (vfunc_ptr_type_node, decl); 774590075Sobrien *vid->last_init = build_tree_list (NULL_TREE, init); 774690075Sobrien vid->last_init = &TREE_CHAIN (*vid->last_init); 774790075Sobrien 7748169689Skan /* Add the offset-to-top entry. It comes earlier in the vtable than 7749169689Skan the typeinfo entry. Convert the offset to look like a 775090075Sobrien function pointer, so that we can put it in the vtable. */ 7751117395Skan init = build_nop (vfunc_ptr_type_node, offset); 775290075Sobrien *vid->last_init = build_tree_list (NULL_TREE, init); 775390075Sobrien vid->last_init = &TREE_CHAIN (*vid->last_init); 775490075Sobrien} 7755169689Skan 7756169689Skan/* Fold a OBJ_TYPE_REF expression to the address of a function. 7757169689Skan KNOWN_TYPE carries the true type of OBJ_TYPE_REF_OBJECT(REF). */ 7758169689Skan 7759169689Skantree 7760169689Skancp_fold_obj_type_ref (tree ref, tree known_type) 7761169689Skan{ 7762169689Skan HOST_WIDE_INT index = tree_low_cst (OBJ_TYPE_REF_TOKEN (ref), 1); 7763169689Skan HOST_WIDE_INT i = 0; 7764169689Skan tree v = BINFO_VIRTUALS (TYPE_BINFO (known_type)); 7765169689Skan tree fndecl; 7766169689Skan 7767169689Skan while (i != index) 7768169689Skan { 7769169689Skan i += (TARGET_VTABLE_USES_DESCRIPTORS 7770169689Skan ? TARGET_VTABLE_USES_DESCRIPTORS : 1); 7771169689Skan v = TREE_CHAIN (v); 7772169689Skan } 7773169689Skan 7774169689Skan fndecl = BV_FN (v); 7775169689Skan 7776169689Skan#ifdef ENABLE_CHECKING 7777169689Skan gcc_assert (tree_int_cst_equal (OBJ_TYPE_REF_TOKEN (ref), 7778169689Skan DECL_VINDEX (fndecl))); 7779169689Skan#endif 7780169689Skan 7781169689Skan cgraph_node (fndecl)->local.vtable_method = true; 7782169689Skan 7783169689Skan return build_address (fndecl); 7784169689Skan} 7785169689Skan 7786169689Skan#include "gt-cp-class.h" 7787