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); 1285261188Spfg /* APPLE LOCAL begin omit calls to empty destructors 5559195 */ 1286261188Spfg if (CLASSTYPE_HAS_NONTRIVIAL_DESTRUCTOR_BODY (basetype) 1287261188Spfg || CLASSTYPE_DESTRUCTOR_NONTRIVIAL_BECAUSE_OF_BASE (basetype)) 1288261188Spfg CLASSTYPE_DESTRUCTOR_NONTRIVIAL_BECAUSE_OF_BASE (t) = 1; 1289261188Spfg /* APPLE LOCAL end omit calls to empty destructors 5559195 */ 1290261188Spfg 1291169689Skan TYPE_HAS_COMPLEX_ASSIGN_REF (t) 129290075Sobrien |= TYPE_HAS_COMPLEX_ASSIGN_REF (basetype); 129318334Speter TYPE_HAS_COMPLEX_INIT_REF (t) |= TYPE_HAS_COMPLEX_INIT_REF (basetype); 129490075Sobrien TYPE_POLYMORPHIC_P (t) |= TYPE_POLYMORPHIC_P (basetype); 1295169689Skan CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) 1296107590Sobrien |= CLASSTYPE_CONTAINS_EMPTY_CLASS_P (basetype); 129790075Sobrien } 129890075Sobrien} 129918334Speter 1300169689Skan/* Determine all the primary bases within T. Sets BINFO_PRIMARY_BASE_P for 1301169689Skan those that are primaries. Sets BINFO_LOST_PRIMARY_P for those 1302169689Skan that have had a nearly-empty virtual primary base stolen by some 1303169689Skan other base in the hierarchy. Determines CLASSTYPE_PRIMARY_BASE for 1304169689Skan T. */ 130590075Sobrien 130690075Sobrienstatic void 1307169689Skandetermine_primary_bases (tree t) 130890075Sobrien{ 1309169689Skan unsigned i; 1310169689Skan tree primary = NULL_TREE; 1311169689Skan tree type_binfo = TYPE_BINFO (t); 1312169689Skan tree base_binfo; 1313169689Skan 1314169689Skan /* Determine the primary bases of our bases. */ 1315169689Skan for (base_binfo = TREE_CHAIN (type_binfo); base_binfo; 1316169689Skan base_binfo = TREE_CHAIN (base_binfo)) 131790075Sobrien { 1318169689Skan tree primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (base_binfo)); 131990075Sobrien 1320169689Skan /* See if we're the non-virtual primary of our inheritance 1321169689Skan chain. */ 1322169689Skan if (!BINFO_VIRTUAL_P (base_binfo)) 1323132718Skan { 1324169689Skan tree parent = BINFO_INHERITANCE_CHAIN (base_binfo); 1325169689Skan tree parent_primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (parent)); 1326169689Skan 1327169689Skan if (parent_primary 1328169689Skan && SAME_BINFO_TYPE_P (BINFO_TYPE (base_binfo), 1329169689Skan BINFO_TYPE (parent_primary))) 1330169689Skan /* We are the primary binfo. */ 1331169689Skan BINFO_PRIMARY_P (base_binfo) = 1; 1332132718Skan } 1333169689Skan /* Determine if we have a virtual primary base, and mark it so. 1334169689Skan */ 1335169689Skan if (primary && BINFO_VIRTUAL_P (primary)) 133618334Speter { 1337169689Skan tree this_primary = copied_binfo (primary, base_binfo); 133818334Speter 1339169689Skan if (BINFO_PRIMARY_P (this_primary)) 1340169689Skan /* Someone already claimed this base. */ 1341169689Skan BINFO_LOST_PRIMARY_P (base_binfo) = 1; 134218334Speter else 134318334Speter { 1344169689Skan tree delta; 134590075Sobrien 1346169689Skan BINFO_PRIMARY_P (this_primary) = 1; 1347169689Skan BINFO_INHERITANCE_CHAIN (this_primary) = base_binfo; 1348169689Skan 1349169689Skan /* A virtual binfo might have been copied from within 1350169689Skan another hierarchy. As we're about to use it as a 1351169689Skan primary base, make sure the offsets match. */ 1352169689Skan delta = size_diffop (convert (ssizetype, 1353169689Skan BINFO_OFFSET (base_binfo)), 1354169689Skan convert (ssizetype, 1355169689Skan BINFO_OFFSET (this_primary))); 1356169689Skan 1357169689Skan propagate_binfo_offsets (this_primary, delta); 135890075Sobrien } 135990075Sobrien } 136090075Sobrien } 136118334Speter 1362169689Skan /* First look for a dynamic direct non-virtual base. */ 1363169689Skan for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, base_binfo); i++) 136490075Sobrien { 1365169689Skan tree basetype = BINFO_TYPE (base_binfo); 136690075Sobrien 1367169689Skan if (TYPE_CONTAINS_VPTR_P (basetype) && !BINFO_VIRTUAL_P (base_binfo)) 136890075Sobrien { 1369169689Skan primary = base_binfo; 1370169689Skan goto found; 137118334Speter } 137218334Speter } 137318334Speter 137490075Sobrien /* A "nearly-empty" virtual base class can be the primary base 1375169689Skan class, if no non-virtual polymorphic base can be found. Look for 1376169689Skan a nearly-empty virtual dynamic base that is not already a primary 1377169689Skan base of something in the hierarchy. If there is no such base, 1378169689Skan just pick the first nearly-empty virtual base. */ 1379169689Skan 1380169689Skan for (base_binfo = TREE_CHAIN (type_binfo); base_binfo; 1381169689Skan base_binfo = TREE_CHAIN (base_binfo)) 1382169689Skan if (BINFO_VIRTUAL_P (base_binfo) 1383169689Skan && CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (base_binfo))) 1384169689Skan { 1385169689Skan if (!BINFO_PRIMARY_P (base_binfo)) 1386169689Skan { 1387169689Skan /* Found one that is not primary. */ 1388169689Skan primary = base_binfo; 1389169689Skan goto found; 1390169689Skan } 1391169689Skan else if (!primary) 1392169689Skan /* Remember the first candidate. */ 1393169689Skan primary = base_binfo; 1394169689Skan } 1395169689Skan 1396169689Skan found: 1397169689Skan /* If we've got a primary base, use it. */ 1398169689Skan if (primary) 139990075Sobrien { 1400169689Skan tree basetype = BINFO_TYPE (primary); 140118334Speter 1402169689Skan CLASSTYPE_PRIMARY_BINFO (t) = primary; 1403169689Skan if (BINFO_PRIMARY_P (primary)) 1404169689Skan /* We are stealing a primary base. */ 1405169689Skan BINFO_LOST_PRIMARY_P (BINFO_INHERITANCE_CHAIN (primary)) = 1; 1406169689Skan BINFO_PRIMARY_P (primary) = 1; 1407169689Skan if (BINFO_VIRTUAL_P (primary)) 140890075Sobrien { 1409169689Skan tree delta; 141050397Sobrien 1411169689Skan BINFO_INHERITANCE_CHAIN (primary) = type_binfo; 1412169689Skan /* A virtual binfo might have been copied from within 1413169689Skan another hierarchy. As we're about to use it as a primary 1414169689Skan base, make sure the offsets match. */ 1415169689Skan delta = size_diffop (ssize_int (0), 1416169689Skan convert (ssizetype, BINFO_OFFSET (primary))); 141750397Sobrien 1418169689Skan propagate_binfo_offsets (primary, delta); 141990075Sobrien } 142090075Sobrien 1421169689Skan primary = TYPE_BINFO (basetype); 1422169689Skan 1423169689Skan TYPE_VFIELD (t) = TYPE_VFIELD (basetype); 1424169689Skan BINFO_VTABLE (type_binfo) = BINFO_VTABLE (primary); 1425169689Skan BINFO_VIRTUALS (type_binfo) = BINFO_VIRTUALS (primary); 142690075Sobrien } 142718334Speter} 142818334Speter 142990075Sobrien/* Set memoizing fields and bits of T (and its variants) for later 143090075Sobrien use. */ 143150397Sobrien 143218334Speterstatic void 1433132718Skanfinish_struct_bits (tree t) 143418334Speter{ 1435169689Skan tree variants; 143618334Speter 143718334Speter /* Fix up variants (if any). */ 1438169689Skan for (variants = TYPE_NEXT_VARIANT (t); 1439169689Skan variants; 1440169689Skan variants = TYPE_NEXT_VARIANT (variants)) 144118334Speter { 144218334Speter /* These fields are in the _TYPE part of the node, not in 144318334Speter the TYPE_LANG_SPECIFIC component, so they are not shared. */ 144418334Speter TYPE_HAS_CONSTRUCTOR (variants) = TYPE_HAS_CONSTRUCTOR (t); 144518334Speter TYPE_NEEDS_CONSTRUCTING (variants) = TYPE_NEEDS_CONSTRUCTING (t); 1446169689Skan TYPE_HAS_NONTRIVIAL_DESTRUCTOR (variants) 144790075Sobrien = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t); 144818334Speter 1449261188Spfg /* APPLE LOCAL begin omit calls to empty destructors 5559195 */ 1450261188Spfg CLASSTYPE_HAS_NONTRIVIAL_DESTRUCTOR_BODY (variants) = 1451261188Spfg CLASSTYPE_HAS_NONTRIVIAL_DESTRUCTOR_BODY (t); 1452261188Spfg CLASSTYPE_DESTRUCTOR_NONTRIVIAL_BECAUSE_OF_BASE (variants) = 1453261188Spfg CLASSTYPE_DESTRUCTOR_NONTRIVIAL_BECAUSE_OF_BASE (t); 1454261188Spfg /* APPLE LOCAL end omit calls to empty destructors 5559195 */ 1455261188Spfg 145690075Sobrien TYPE_POLYMORPHIC_P (variants) = TYPE_POLYMORPHIC_P (t); 1457169689Skan 1458169689Skan TYPE_BINFO (variants) = TYPE_BINFO (t); 1459169689Skan 146018334Speter /* Copy whatever these are holding today. */ 1461169689Skan TYPE_VFIELD (variants) = TYPE_VFIELD (t); 1462169689Skan TYPE_METHODS (variants) = TYPE_METHODS (t); 146350397Sobrien TYPE_FIELDS (variants) = TYPE_FIELDS (t); 146418334Speter } 146518334Speter 1466169689Skan if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) && TYPE_POLYMORPHIC_P (t)) 1467169689Skan /* For a class w/o baseclasses, 'finish_struct' has set 1468169689Skan CLASSTYPE_PURE_VIRTUALS correctly (by definition). 1469169689Skan Similarly for a class whose base classes do not have vtables. 1470169689Skan When neither of these is true, we might have removed abstract 1471169689Skan virtuals (by providing a definition), added some (by declaring 1472169689Skan new ones), or redeclared ones from a base class. We need to 1473169689Skan recalculate what's really an abstract virtual at this point (by 1474169689Skan looking in the vtables). */ 1475169689Skan get_pure_virtuals (t); 147618334Speter 1477169689Skan /* If this type has a copy constructor or a destructor, force its 1478169689Skan mode to be BLKmode, and force its TREE_ADDRESSABLE bit to be 1479169689Skan nonzero. This will cause it to be passed by invisible reference 1480169689Skan and prevent it from being returned in a register. */ 148196263Sobrien if (! TYPE_HAS_TRIVIAL_INIT_REF (t) || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)) 148218334Speter { 148318334Speter tree variants; 148450397Sobrien DECL_MODE (TYPE_MAIN_DECL (t)) = BLKmode; 148518334Speter for (variants = t; variants; variants = TYPE_NEXT_VARIANT (variants)) 148618334Speter { 148718334Speter TYPE_MODE (variants) = BLKmode; 148818334Speter TREE_ADDRESSABLE (variants) = 1; 148918334Speter } 149018334Speter } 149118334Speter} 149218334Speter 149352284Sobrien/* Issue warnings about T having private constructors, but no friends, 1494169689Skan and so forth. 149550397Sobrien 149652284Sobrien HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or 149752284Sobrien static members. HAS_NONPRIVATE_STATIC_FN is nonzero if T has any 149852284Sobrien non-private static member functions. */ 149952284Sobrien 150018334Speterstatic void 1501132718Skanmaybe_warn_about_overly_private_class (tree t) 150218334Speter{ 150352284Sobrien int has_member_fn = 0; 150452284Sobrien int has_nonprivate_method = 0; 150552284Sobrien tree fn; 150650397Sobrien 150752284Sobrien if (!warn_ctor_dtor_privacy 150852284Sobrien /* If the class has friends, those entities might create and 150952284Sobrien access instances, so we should not warn. */ 151052284Sobrien || (CLASSTYPE_FRIEND_CLASSES (t) 151152284Sobrien || DECL_FRIENDLIST (TYPE_MAIN_DECL (t))) 151252284Sobrien /* We will have warned when the template was declared; there's 151352284Sobrien no need to warn on every instantiation. */ 151452284Sobrien || CLASSTYPE_TEMPLATE_INSTANTIATION (t)) 1515169689Skan /* There's no reason to even consider warning about this 151652284Sobrien class. */ 151752284Sobrien return; 1518169689Skan 151952284Sobrien /* We only issue one warning, if more than one applies, because 152052284Sobrien otherwise, on code like: 152150397Sobrien 152252284Sobrien class A { 152352284Sobrien // Oops - forgot `public:' 152452284Sobrien A(); 152552284Sobrien A(const A&); 152652284Sobrien ~A(); 152752284Sobrien }; 152818334Speter 152952284Sobrien we warn several times about essentially the same problem. */ 153052284Sobrien 153152284Sobrien /* Check to see if all (non-constructor, non-destructor) member 153252284Sobrien functions are private. (Since there are no friends or 153352284Sobrien non-private statics, we can't ever call any of the private member 153452284Sobrien functions.) */ 153552284Sobrien for (fn = TYPE_METHODS (t); fn; fn = TREE_CHAIN (fn)) 153652284Sobrien /* We're not interested in compiler-generated methods; they don't 153752284Sobrien provide any way to call private members. */ 1538169689Skan if (!DECL_ARTIFICIAL (fn)) 153952284Sobrien { 154052284Sobrien if (!TREE_PRIVATE (fn)) 154152284Sobrien { 1542169689Skan if (DECL_STATIC_FUNCTION_P (fn)) 154352284Sobrien /* A non-private static member function is just like a 154452284Sobrien friend; it can create and invoke private member 154552284Sobrien functions, and be accessed without a class 154652284Sobrien instance. */ 154752284Sobrien return; 1548169689Skan 154952284Sobrien has_nonprivate_method = 1; 1550117395Skan /* Keep searching for a static member function. */ 155152284Sobrien } 155252284Sobrien else if (!DECL_CONSTRUCTOR_P (fn) && !DECL_DESTRUCTOR_P (fn)) 155352284Sobrien has_member_fn = 1; 1554169689Skan } 155552284Sobrien 1556169689Skan if (!has_nonprivate_method && has_member_fn) 155718334Speter { 155852284Sobrien /* There are no non-private methods, and there's at least one 155952284Sobrien private member function that isn't a constructor or 156052284Sobrien destructor. (If all the private members are 156152284Sobrien constructors/destructors we want to use the code below that 156252284Sobrien issues error messages specifically referring to 156352284Sobrien constructors/destructors.) */ 1564169689Skan unsigned i; 1565132718Skan tree binfo = TYPE_BINFO (t); 1566169689Skan 1567169689Skan for (i = 0; i != BINFO_N_BASE_BINFOS (binfo); i++) 1568169689Skan if (BINFO_BASE_ACCESS (binfo, i) != access_private_node) 156952284Sobrien { 157052284Sobrien has_nonprivate_method = 1; 157152284Sobrien break; 157252284Sobrien } 1573169689Skan if (!has_nonprivate_method) 157452284Sobrien { 1575169689Skan warning (OPT_Wctor_dtor_privacy, 1576169689Skan "all member functions in class %qT are private", t); 157752284Sobrien return; 157852284Sobrien } 157918334Speter } 158052284Sobrien 158152284Sobrien /* Even if some of the member functions are non-private, the class 158252284Sobrien won't be useful for much if all the constructors or destructors 158352284Sobrien are private: such an object can never be created or destroyed. */ 1584169689Skan fn = CLASSTYPE_DESTRUCTORS (t); 1585169689Skan if (fn && TREE_PRIVATE (fn)) 158652284Sobrien { 1587169689Skan warning (OPT_Wctor_dtor_privacy, 1588169689Skan "%q#T only defines a private destructor and has no friends", 1589132718Skan t); 1590132718Skan return; 159152284Sobrien } 159252284Sobrien 1593169689Skan if (TYPE_HAS_CONSTRUCTOR (t) 1594169689Skan /* Implicitly generated constructors are always public. */ 1595169689Skan && (!CLASSTYPE_LAZY_DEFAULT_CTOR (t) 1596169689Skan || !CLASSTYPE_LAZY_COPY_CTOR (t))) 159752284Sobrien { 159852284Sobrien int nonprivate_ctor = 0; 1599169689Skan 160052284Sobrien /* If a non-template class does not define a copy 160152284Sobrien constructor, one is defined for it, enabling it to avoid 160252284Sobrien this warning. For a template class, this does not 160352284Sobrien happen, and so we would normally get a warning on: 160452284Sobrien 1605169689Skan template <class T> class C { private: C(); }; 1606169689Skan 160752284Sobrien To avoid this asymmetry, we check TYPE_HAS_INIT_REF. All 160852284Sobrien complete non-template or fully instantiated classes have this 160952284Sobrien flag set. */ 161052284Sobrien if (!TYPE_HAS_INIT_REF (t)) 161152284Sobrien nonprivate_ctor = 1; 1612169689Skan else 1613169689Skan for (fn = CLASSTYPE_CONSTRUCTORS (t); fn; fn = OVL_NEXT (fn)) 161452284Sobrien { 161552284Sobrien tree ctor = OVL_CURRENT (fn); 161652284Sobrien /* Ideally, we wouldn't count copy constructors (or, in 161752284Sobrien fact, any constructor that takes an argument of the 161852284Sobrien class type as a parameter) because such things cannot 161952284Sobrien be used to construct an instance of the class unless 162052284Sobrien you already have one. But, for now at least, we're 162152284Sobrien more generous. */ 162252284Sobrien if (! TREE_PRIVATE (ctor)) 162352284Sobrien { 162452284Sobrien nonprivate_ctor = 1; 162552284Sobrien break; 162652284Sobrien } 162752284Sobrien } 162852284Sobrien 162952284Sobrien if (nonprivate_ctor == 0) 163052284Sobrien { 1631169689Skan warning (OPT_Wctor_dtor_privacy, 1632169689Skan "%q#T only defines private constructors and has no friends", 1633169689Skan t); 163452284Sobrien return; 163552284Sobrien } 163652284Sobrien } 163718334Speter} 163818334Speter 1639132718Skanstatic struct { 1640132718Skan gt_pointer_operator new_value; 1641132718Skan void *cookie; 1642132718Skan} resort_data; 164352284Sobrien 1644132718Skan/* Comparison function to compare two TYPE_METHOD_VEC entries by name. */ 1645132718Skan 164690075Sobrienstatic int 1647132718Skanmethod_name_cmp (const void* m1_p, const void* m2_p) 164890075Sobrien{ 1649169689Skan const tree *const m1 = (const tree *) m1_p; 1650169689Skan const tree *const m2 = (const tree *) m2_p; 1651169689Skan 1652132718Skan if (*m1 == NULL_TREE && *m2 == NULL_TREE) 1653132718Skan return 0; 1654132718Skan if (*m1 == NULL_TREE) 165590075Sobrien return -1; 1656132718Skan if (*m2 == NULL_TREE) 165790075Sobrien return 1; 1658132718Skan if (DECL_NAME (OVL_CURRENT (*m1)) < DECL_NAME (OVL_CURRENT (*m2))) 165990075Sobrien return -1; 166090075Sobrien return 1; 166190075Sobrien} 166290075Sobrien 1663132718Skan/* This routine compares two fields like method_name_cmp but using the 1664132718Skan pointer operator in resort_field_decl_data. */ 166590075Sobrien 166690075Sobrienstatic int 1667132718Skanresort_method_name_cmp (const void* m1_p, const void* m2_p) 166890075Sobrien{ 1669169689Skan const tree *const m1 = (const tree *) m1_p; 1670169689Skan const tree *const m2 = (const tree *) m2_p; 167190075Sobrien if (*m1 == NULL_TREE && *m2 == NULL_TREE) 167290075Sobrien return 0; 167390075Sobrien if (*m1 == NULL_TREE) 167490075Sobrien return -1; 167590075Sobrien if (*m2 == NULL_TREE) 167690075Sobrien return 1; 1677132718Skan { 1678132718Skan tree d1 = DECL_NAME (OVL_CURRENT (*m1)); 1679132718Skan tree d2 = DECL_NAME (OVL_CURRENT (*m2)); 1680132718Skan resort_data.new_value (&d1, resort_data.cookie); 1681132718Skan resort_data.new_value (&d2, resort_data.cookie); 1682132718Skan if (d1 < d2) 1683132718Skan return -1; 1684132718Skan } 168590075Sobrien return 1; 168690075Sobrien} 168790075Sobrien 1688132718Skan/* Resort TYPE_METHOD_VEC because pointers have been reordered. */ 1689132718Skan 1690169689Skanvoid 1691132718Skanresort_type_method_vec (void* obj, 1692169689Skan void* orig_obj ATTRIBUTE_UNUSED , 1693169689Skan gt_pointer_operator new_value, 1694169689Skan void* cookie) 1695132718Skan{ 1696169689Skan VEC(tree,gc) *method_vec = (VEC(tree,gc) *) obj; 1697169689Skan int len = VEC_length (tree, method_vec); 1698169689Skan size_t slot; 1699169689Skan tree fn; 1700132718Skan 1701132718Skan /* The type conversion ops have to live at the front of the vec, so we 1702132718Skan can't sort them. */ 1703169689Skan for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT; 1704169689Skan VEC_iterate (tree, method_vec, slot, fn); 1705169689Skan ++slot) 1706169689Skan if (!DECL_CONV_FN_P (OVL_CURRENT (fn))) 1707169689Skan break; 1708169689Skan 1709132718Skan if (len - slot > 1) 1710132718Skan { 1711132718Skan resort_data.new_value = new_value; 1712132718Skan resort_data.cookie = cookie; 1713169689Skan qsort (VEC_address (tree, method_vec) + slot, len - slot, sizeof (tree), 1714132718Skan resort_method_name_cmp); 1715132718Skan } 1716132718Skan} 1717132718Skan 1718169689Skan/* Warn about duplicate methods in fn_fields. 171918334Speter 172090075Sobrien Sort methods that are not special (i.e., constructors, destructors, 172190075Sobrien and type conversion operators) so that we can find them faster in 172290075Sobrien search. */ 172318334Speter 172452284Sobrienstatic void 1725132718Skanfinish_struct_methods (tree t) 172618334Speter{ 172752284Sobrien tree fn_fields; 1728169689Skan VEC(tree,gc) *method_vec; 172990075Sobrien int slot, len; 173018334Speter 173190075Sobrien method_vec = CLASSTYPE_METHOD_VEC (t); 1732169689Skan if (!method_vec) 1733169689Skan return; 173490075Sobrien 1735169689Skan len = VEC_length (tree, method_vec); 1736169689Skan 1737169689Skan /* Clear DECL_IN_AGGR_P for all functions. */ 1738169689Skan for (fn_fields = TYPE_METHODS (t); fn_fields; 173952284Sobrien fn_fields = TREE_CHAIN (fn_fields)) 174090075Sobrien DECL_IN_AGGR_P (fn_fields) = 0; 174118334Speter 174252284Sobrien /* Issue warnings about private constructors and such. If there are 174352284Sobrien no methods, then some public defaults are generated. */ 174490075Sobrien maybe_warn_about_overly_private_class (t); 174590075Sobrien 174690075Sobrien /* The type conversion ops have to live at the front of the vec, so we 174790075Sobrien can't sort them. */ 1748169689Skan for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT; 1749169689Skan VEC_iterate (tree, method_vec, slot, fn_fields); 1750169689Skan ++slot) 1751169689Skan if (!DECL_CONV_FN_P (OVL_CURRENT (fn_fields))) 1752169689Skan break; 175390075Sobrien if (len - slot > 1) 1754169689Skan qsort (VEC_address (tree, method_vec) + slot, 1755169689Skan len-slot, sizeof (tree), method_name_cmp); 175618334Speter} 175718334Speter 175890075Sobrien/* Make BINFO's vtable have N entries, including RTTI entries, 175990075Sobrien vbase and vcall offsets, etc. Set its type and call the backend 176090075Sobrien to lay it out. */ 176150397Sobrien 176218334Speterstatic void 1763132718Skanlayout_vtable_decl (tree binfo, int n) 176418334Speter{ 176590075Sobrien tree atype; 176690075Sobrien tree vtable; 176718334Speter 1768169689Skan atype = build_cplus_array_type (vtable_entry_type, 176990075Sobrien build_index_type (size_int (n - 1))); 177090075Sobrien layout_type (atype); 177190075Sobrien 177290075Sobrien /* We may have to grow the vtable. */ 177390075Sobrien vtable = get_vtbl_decl_for_binfo (binfo); 177490075Sobrien if (!same_type_p (TREE_TYPE (vtable), atype)) 177518334Speter { 177690075Sobrien TREE_TYPE (vtable) = atype; 177790075Sobrien DECL_SIZE (vtable) = DECL_SIZE_UNIT (vtable) = NULL_TREE; 177890075Sobrien layout_decl (vtable, 0); 177918334Speter } 178018334Speter} 178118334Speter 178290075Sobrien/* True iff FNDECL and BASE_FNDECL (both non-static member functions) 178390075Sobrien have the same signature. */ 178450397Sobrien 178590075Sobrienint 1786132718Skansame_signature_p (tree fndecl, tree base_fndecl) 178718334Speter{ 178890075Sobrien /* One destructor overrides another if they are the same kind of 178990075Sobrien destructor. */ 179090075Sobrien if (DECL_DESTRUCTOR_P (base_fndecl) && DECL_DESTRUCTOR_P (fndecl) 179190075Sobrien && special_function_p (base_fndecl) == special_function_p (fndecl)) 179218334Speter return 1; 179390075Sobrien /* But a non-destructor never overrides a destructor, nor vice 179490075Sobrien versa, nor do different kinds of destructors override 179590075Sobrien one-another. For example, a complete object destructor does not 179690075Sobrien override a deleting destructor. */ 179790075Sobrien if (DECL_DESTRUCTOR_P (base_fndecl) || DECL_DESTRUCTOR_P (fndecl)) 179818334Speter return 0; 179990075Sobrien 1800132718Skan if (DECL_NAME (fndecl) == DECL_NAME (base_fndecl) 1801132718Skan || (DECL_CONV_FN_P (fndecl) 1802132718Skan && DECL_CONV_FN_P (base_fndecl) 1803132718Skan && same_type_p (DECL_CONV_FN_TYPE (fndecl), 1804132718Skan DECL_CONV_FN_TYPE (base_fndecl)))) 180518334Speter { 180650397Sobrien tree types, base_types; 180718334Speter types = TYPE_ARG_TYPES (TREE_TYPE (fndecl)); 180818334Speter base_types = TYPE_ARG_TYPES (TREE_TYPE (base_fndecl)); 180952284Sobrien if ((TYPE_QUALS (TREE_TYPE (TREE_VALUE (base_types))) 181052284Sobrien == TYPE_QUALS (TREE_TYPE (TREE_VALUE (types)))) 181152284Sobrien && compparms (TREE_CHAIN (base_types), TREE_CHAIN (types))) 181218334Speter return 1; 181318334Speter } 181418334Speter return 0; 181518334Speter} 181618334Speter 1817117395Skan/* Returns TRUE if DERIVED is a binfo containing the binfo BASE as a 1818117395Skan subobject. */ 1819169689Skan 1820117395Skanstatic bool 1821117395Skanbase_derived_from (tree derived, tree base) 1822117395Skan{ 1823132718Skan tree probe; 1824132718Skan 1825132718Skan for (probe = base; probe; probe = BINFO_INHERITANCE_CHAIN (probe)) 1826132718Skan { 1827132718Skan if (probe == derived) 1828132718Skan return true; 1829169689Skan else if (BINFO_VIRTUAL_P (probe)) 1830132718Skan /* If we meet a virtual base, we can't follow the inheritance 1831132718Skan any more. See if the complete type of DERIVED contains 1832132718Skan such a virtual base. */ 1833169689Skan return (binfo_for_vbase (BINFO_TYPE (probe), BINFO_TYPE (derived)) 1834169689Skan != NULL_TREE); 1835132718Skan } 1836132718Skan return false; 1837117395Skan} 1838117395Skan 183990075Sobrientypedef struct find_final_overrider_data_s { 184090075Sobrien /* The function for which we are trying to find a final overrider. */ 184190075Sobrien tree fn; 184290075Sobrien /* The base class in which the function was declared. */ 184390075Sobrien tree declaring_base; 1844117395Skan /* The candidate overriders. */ 184590075Sobrien tree candidates; 1846169689Skan /* Path to most derived. */ 1847169689Skan VEC(tree,heap) *path; 184890075Sobrien} find_final_overrider_data; 184990075Sobrien 1850146895Skan/* Add the overrider along the current path to FFOD->CANDIDATES. 1851146895Skan Returns true if an overrider was found; false otherwise. */ 1852146895Skan 1853146895Skanstatic bool 1854169689Skandfs_find_final_overrider_1 (tree binfo, 1855169689Skan find_final_overrider_data *ffod, 1856169689Skan unsigned depth) 1857146895Skan{ 1858146895Skan tree method; 1859169689Skan 1860146895Skan /* If BINFO is not the most derived type, try a more derived class. 1861146895Skan A definition there will overrider a definition here. */ 1862169689Skan if (depth) 1863146895Skan { 1864169689Skan depth--; 1865169689Skan if (dfs_find_final_overrider_1 1866169689Skan (VEC_index (tree, ffod->path, depth), ffod, depth)) 1867146895Skan return true; 1868146895Skan } 1869146895Skan 1870146895Skan method = look_for_overrides_here (BINFO_TYPE (binfo), ffod->fn); 1871146895Skan if (method) 1872146895Skan { 1873146895Skan tree *candidate = &ffod->candidates; 1874169689Skan 1875146895Skan /* Remove any candidates overridden by this new function. */ 1876146895Skan while (*candidate) 1877146895Skan { 1878146895Skan /* If *CANDIDATE overrides METHOD, then METHOD 1879146895Skan cannot override anything else on the list. */ 1880146895Skan if (base_derived_from (TREE_VALUE (*candidate), binfo)) 1881146895Skan return true; 1882146895Skan /* If METHOD overrides *CANDIDATE, remove *CANDIDATE. */ 1883146895Skan if (base_derived_from (binfo, TREE_VALUE (*candidate))) 1884146895Skan *candidate = TREE_CHAIN (*candidate); 1885146895Skan else 1886146895Skan candidate = &TREE_CHAIN (*candidate); 1887146895Skan } 1888169689Skan 1889146895Skan /* Add the new function. */ 1890146895Skan ffod->candidates = tree_cons (method, binfo, ffod->candidates); 1891146895Skan return true; 1892146895Skan } 1893146895Skan 1894146895Skan return false; 1895146895Skan} 1896146895Skan 189790075Sobrien/* Called from find_final_overrider via dfs_walk. */ 189890075Sobrien 189918334Speterstatic tree 1900169689Skandfs_find_final_overrider_pre (tree binfo, void *data) 190118334Speter{ 190290075Sobrien find_final_overrider_data *ffod = (find_final_overrider_data *) data; 190318334Speter 1904132718Skan if (binfo == ffod->declaring_base) 1905169689Skan dfs_find_final_overrider_1 (binfo, ffod, VEC_length (tree, ffod->path)); 1906169689Skan VEC_safe_push (tree, heap, ffod->path, binfo); 190718334Speter 1908132718Skan return NULL_TREE; 1909132718Skan} 191018334Speter 1911132718Skanstatic tree 1912169689Skandfs_find_final_overrider_post (tree binfo ATTRIBUTE_UNUSED, void *data) 1913132718Skan{ 1914132718Skan find_final_overrider_data *ffod = (find_final_overrider_data *) data; 1915169689Skan VEC_pop (tree, ffod->path); 191618334Speter 191790075Sobrien return NULL_TREE; 191818334Speter} 191918334Speter 192090075Sobrien/* Returns a TREE_LIST whose TREE_PURPOSE is the final overrider for 192190075Sobrien FN and whose TREE_VALUE is the binfo for the base where the 1922117395Skan overriding occurs. BINFO (in the hierarchy dominated by the binfo 1923117395Skan DERIVED) is the base object in which FN is declared. */ 192450397Sobrien 192590075Sobrienstatic tree 1926132718Skanfind_final_overrider (tree derived, tree binfo, tree fn) 192718334Speter{ 192890075Sobrien find_final_overrider_data ffod; 192918334Speter 1930117395Skan /* Getting this right is a little tricky. This is valid: 193152284Sobrien 193290075Sobrien struct S { virtual void f (); }; 193390075Sobrien struct T { virtual void f (); }; 193490075Sobrien struct U : public S, public T { }; 193518334Speter 1936169689Skan even though calling `f' in `U' is ambiguous. But, 193718334Speter 193890075Sobrien struct R { virtual void f(); }; 193990075Sobrien struct S : virtual public R { virtual void f (); }; 194090075Sobrien struct T : virtual public R { virtual void f (); }; 194190075Sobrien struct U : public S, public T { }; 194218334Speter 194390075Sobrien is not -- there's no way to decide whether to put `S::f' or 1944169689Skan `T::f' in the vtable for `R'. 1945169689Skan 194690075Sobrien The solution is to look at all paths to BINFO. If we find 194790075Sobrien different overriders along any two, then there is a problem. */ 1948132718Skan if (DECL_THUNK_P (fn)) 1949132718Skan fn = THUNK_TARGET (fn); 1950146895Skan 1951146895Skan /* Determine the depth of the hierarchy. */ 195290075Sobrien ffod.fn = fn; 195390075Sobrien ffod.declaring_base = binfo; 195490075Sobrien ffod.candidates = NULL_TREE; 1955169689Skan ffod.path = VEC_alloc (tree, heap, 30); 195690075Sobrien 1957169689Skan dfs_walk_all (derived, dfs_find_final_overrider_pre, 1958169689Skan dfs_find_final_overrider_post, &ffod); 195990075Sobrien 1960169689Skan VEC_free (tree, heap, ffod.path); 1961146895Skan 196290075Sobrien /* If there was no winner, issue an error message. */ 1963117395Skan if (!ffod.candidates || TREE_CHAIN (ffod.candidates)) 1964161651Skan return error_mark_node; 196518334Speter 1966117395Skan return ffod.candidates; 196790075Sobrien} 196818334Speter 1969117395Skan/* Return the index of the vcall offset for FN when TYPE is used as a 1970117395Skan virtual base. */ 197118334Speter 197290075Sobrienstatic tree 1973117395Skanget_vcall_index (tree fn, tree type) 197490075Sobrien{ 1975169689Skan VEC(tree_pair_s,gc) *indices = CLASSTYPE_VCALL_INDICES (type); 1976169689Skan tree_pair_p p; 1977169689Skan unsigned ix; 197818334Speter 1979169689Skan for (ix = 0; VEC_iterate (tree_pair_s, indices, ix, p); ix++) 1980169689Skan if ((DECL_DESTRUCTOR_P (fn) && DECL_DESTRUCTOR_P (p->purpose)) 1981169689Skan || same_signature_p (fn, p->purpose)) 1982169689Skan return p->value; 1983117395Skan 1984117395Skan /* There should always be an appropriate index. */ 1985169689Skan gcc_unreachable (); 198618334Speter} 198718334Speter 198890075Sobrien/* Update an entry in the vtable for BINFO, which is in the hierarchy 1989132718Skan dominated by T. FN has been overridden in BINFO; VIRTUALS points to the 199090075Sobrien corresponding position in the BINFO_VIRTUALS list. */ 199150397Sobrien 199218334Speterstatic void 1993132718Skanupdate_vtable_entry_for_fn (tree t, tree binfo, tree fn, tree* virtuals, 1994132718Skan unsigned ix) 199518334Speter{ 199690075Sobrien tree b; 199790075Sobrien tree overrider; 199890075Sobrien tree delta; 199990075Sobrien tree virtual_base; 200090075Sobrien tree first_defn; 2001132718Skan tree overrider_fn, overrider_target; 2002132718Skan tree target_fn = DECL_THUNK_P (fn) ? THUNK_TARGET (fn) : fn; 2003132718Skan tree over_return, base_return; 200490075Sobrien bool lost = false; 200518334Speter 200690075Sobrien /* Find the nearest primary base (possibly binfo itself) which defines 200790075Sobrien this function; this is the class the caller will convert to when 200890075Sobrien calling FN through BINFO. */ 200990075Sobrien for (b = binfo; ; b = get_primary_binfo (b)) 201018334Speter { 2011169689Skan gcc_assert (b); 2012132718Skan if (look_for_overrides_here (BINFO_TYPE (b), target_fn)) 201390075Sobrien break; 201418334Speter 201590075Sobrien /* The nearest definition is from a lost primary. */ 201690075Sobrien if (BINFO_LOST_PRIMARY_P (b)) 201790075Sobrien lost = true; 201818334Speter } 201990075Sobrien first_defn = b; 202018334Speter 202190075Sobrien /* Find the final overrider. */ 2022132718Skan overrider = find_final_overrider (TYPE_BINFO (t), b, target_fn); 202390075Sobrien if (overrider == error_mark_node) 2024161651Skan { 2025169689Skan error ("no unique final overrider for %qD in %qT", target_fn, t); 2026161651Skan return; 2027161651Skan } 2028132718Skan overrider_target = overrider_fn = TREE_PURPOSE (overrider); 2029169689Skan 2030132718Skan /* Check for adjusting covariant return types. */ 2031132718Skan over_return = TREE_TYPE (TREE_TYPE (overrider_target)); 2032132718Skan base_return = TREE_TYPE (TREE_TYPE (target_fn)); 2033169689Skan 2034132718Skan if (POINTER_TYPE_P (over_return) 2035132718Skan && TREE_CODE (over_return) == TREE_CODE (base_return) 2036132718Skan && CLASS_TYPE_P (TREE_TYPE (over_return)) 2037169689Skan && CLASS_TYPE_P (TREE_TYPE (base_return)) 2038169689Skan /* If the overrider is invalid, don't even try. */ 2039169689Skan && !DECL_INVALID_OVERRIDER_P (overrider_target)) 2040132718Skan { 2041132718Skan /* If FN is a covariant thunk, we must figure out the adjustment 2042169689Skan to the final base FN was converting to. As OVERRIDER_TARGET might 2043169689Skan also be converting to the return type of FN, we have to 2044169689Skan combine the two conversions here. */ 2045132718Skan tree fixed_offset, virtual_offset; 2046146895Skan 2047146895Skan over_return = TREE_TYPE (over_return); 2048146895Skan base_return = TREE_TYPE (base_return); 2049169689Skan 2050132718Skan if (DECL_THUNK_P (fn)) 2051132718Skan { 2052169689Skan gcc_assert (DECL_RESULT_THUNK_P (fn)); 2053132718Skan fixed_offset = ssize_int (THUNK_FIXED_OFFSET (fn)); 2054132718Skan virtual_offset = THUNK_VIRTUAL_OFFSET (fn); 2055132718Skan } 2056132718Skan else 2057132718Skan fixed_offset = virtual_offset = NULL_TREE; 205850397Sobrien 2059132718Skan if (virtual_offset) 2060132718Skan /* Find the equivalent binfo within the return type of the 2061132718Skan overriding function. We will want the vbase offset from 2062132718Skan there. */ 2063169689Skan virtual_offset = binfo_for_vbase (BINFO_TYPE (virtual_offset), 2064169689Skan over_return); 2065146895Skan else if (!same_type_ignoring_top_level_qualifiers_p 2066146895Skan (over_return, base_return)) 2067132718Skan { 2068169689Skan /* There was no existing virtual thunk (which takes 2069146895Skan precedence). So find the binfo of the base function's 2070146895Skan return type within the overriding function's return type. 2071146895Skan We cannot call lookup base here, because we're inside a 2072146895Skan dfs_walk, and will therefore clobber the BINFO_MARKED 2073146895Skan flags. Fortunately we know the covariancy is valid (it 2074146895Skan has already been checked), so we can just iterate along 2075146895Skan the binfos, which have been chained in inheritance graph 2076146895Skan order. Of course it is lame that we have to repeat the 2077146895Skan search here anyway -- we should really be caching pieces 2078146895Skan of the vtable and avoiding this repeated work. */ 2079146895Skan tree thunk_binfo, base_binfo; 2080146895Skan 2081146895Skan /* Find the base binfo within the overriding function's 2082146895Skan return type. We will always find a thunk_binfo, except 2083146895Skan when the covariancy is invalid (which we will have 2084146895Skan already diagnosed). */ 2085146895Skan for (base_binfo = TYPE_BINFO (base_return), 2086146895Skan thunk_binfo = TYPE_BINFO (over_return); 2087146895Skan thunk_binfo; 2088146895Skan thunk_binfo = TREE_CHAIN (thunk_binfo)) 2089169689Skan if (SAME_BINFO_TYPE_P (BINFO_TYPE (thunk_binfo), 2090169689Skan BINFO_TYPE (base_binfo))) 2091146895Skan break; 2092169689Skan 2093146895Skan /* See if virtual inheritance is involved. */ 2094146895Skan for (virtual_offset = thunk_binfo; 2095146895Skan virtual_offset; 2096146895Skan virtual_offset = BINFO_INHERITANCE_CHAIN (virtual_offset)) 2097169689Skan if (BINFO_VIRTUAL_P (virtual_offset)) 2098146895Skan break; 2099169689Skan 2100146895Skan if (virtual_offset 2101146895Skan || (thunk_binfo && !BINFO_OFFSET_ZEROP (thunk_binfo))) 2102132718Skan { 2103132718Skan tree offset = convert (ssizetype, BINFO_OFFSET (thunk_binfo)); 2104132718Skan 2105146895Skan if (virtual_offset) 2106132718Skan { 2107146895Skan /* We convert via virtual base. Adjust the fixed 2108146895Skan offset to be from there. */ 2109132718Skan offset = size_diffop 2110132718Skan (offset, convert 2111132718Skan (ssizetype, BINFO_OFFSET (virtual_offset))); 2112132718Skan } 2113132718Skan if (fixed_offset) 2114132718Skan /* There was an existing fixed offset, this must be 2115132718Skan from the base just converted to, and the base the 2116132718Skan FN was thunking to. */ 2117132718Skan fixed_offset = size_binop (PLUS_EXPR, fixed_offset, offset); 2118132718Skan else 2119132718Skan fixed_offset = offset; 2120132718Skan } 2121132718Skan } 2122169689Skan 2123132718Skan if (fixed_offset || virtual_offset) 2124132718Skan /* Replace the overriding function with a covariant thunk. We 2125132718Skan will emit the overriding function in its own slot as 2126132718Skan well. */ 2127132718Skan overrider_fn = make_thunk (overrider_target, /*this_adjusting=*/0, 2128132718Skan fixed_offset, virtual_offset); 2129132718Skan } 2130132718Skan else 2131169689Skan gcc_assert (!DECL_THUNK_P (fn)); 2132169689Skan 213390075Sobrien /* Assume that we will produce a thunk that convert all the way to 213490075Sobrien the final overrider, and not to an intermediate virtual base. */ 213590075Sobrien virtual_base = NULL_TREE; 213618334Speter 213790075Sobrien /* See if we can convert to an intermediate virtual base first, and then 213890075Sobrien use the vcall offset located there to finish the conversion. */ 213990075Sobrien for (; b; b = BINFO_INHERITANCE_CHAIN (b)) 214018334Speter { 214190075Sobrien /* If we find the final overrider, then we can stop 214290075Sobrien walking. */ 2143169689Skan if (SAME_BINFO_TYPE_P (BINFO_TYPE (b), 2144169689Skan BINFO_TYPE (TREE_VALUE (overrider)))) 214590075Sobrien break; 214618334Speter 214790075Sobrien /* If we find a virtual base, and we haven't yet found the 214890075Sobrien overrider, then there is a virtual base between the 214990075Sobrien declaring base (first_defn) and the final overrider. */ 2150169689Skan if (BINFO_VIRTUAL_P (b)) 2151132718Skan { 2152132718Skan virtual_base = b; 2153132718Skan break; 2154132718Skan } 215590075Sobrien } 215618334Speter 2157132718Skan if (overrider_fn != overrider_target && !virtual_base) 2158132718Skan { 2159132718Skan /* The ABI specifies that a covariant thunk includes a mangling 2160169689Skan for a this pointer adjustment. This-adjusting thunks that 2161169689Skan override a function from a virtual base have a vcall 2162169689Skan adjustment. When the virtual base in question is a primary 2163169689Skan virtual base, we know the adjustments are zero, (and in the 2164169689Skan non-covariant case, we would not use the thunk). 2165169689Skan Unfortunately we didn't notice this could happen, when 2166169689Skan designing the ABI and so never mandated that such a covariant 2167169689Skan thunk should be emitted. Because we must use the ABI mandated 2168169689Skan name, we must continue searching from the binfo where we 2169169689Skan found the most recent definition of the function, towards the 2170169689Skan primary binfo which first introduced the function into the 2171169689Skan vtable. If that enters a virtual base, we must use a vcall 2172169689Skan this-adjusting thunk. Bleah! */ 2173132718Skan tree probe = first_defn; 2174132718Skan 2175132718Skan while ((probe = get_primary_binfo (probe)) 2176132718Skan && (unsigned) list_length (BINFO_VIRTUALS (probe)) > ix) 2177169689Skan if (BINFO_VIRTUAL_P (probe)) 2178132718Skan virtual_base = probe; 2179169689Skan 2180132718Skan if (virtual_base) 2181132718Skan /* Even if we find a virtual base, the correct delta is 2182132718Skan between the overrider and the binfo we're building a vtable 2183132718Skan for. */ 2184132718Skan goto virtual_covariant; 2185132718Skan } 2186169689Skan 218790075Sobrien /* Compute the constant adjustment to the `this' pointer. The 218890075Sobrien `this' pointer, when this function is called, will point at BINFO 218990075Sobrien (or one of its primary bases, which are at the same offset). */ 219090075Sobrien if (virtual_base) 219190075Sobrien /* The `this' pointer needs to be adjusted from the declaration to 219290075Sobrien the nearest virtual base. */ 2193132718Skan delta = size_diffop (convert (ssizetype, BINFO_OFFSET (virtual_base)), 2194132718Skan convert (ssizetype, BINFO_OFFSET (first_defn))); 219590075Sobrien else if (lost) 219690075Sobrien /* If the nearest definition is in a lost primary, we don't need an 219790075Sobrien entry in our vtable. Except possibly in a constructor vtable, 219890075Sobrien if we happen to get our primary back. In that case, the offset 219990075Sobrien will be zero, as it will be a primary base. */ 220090075Sobrien delta = size_zero_node; 220190075Sobrien else 2202117395Skan /* The `this' pointer needs to be adjusted from pointing to 2203117395Skan BINFO to pointing at the base where the final overrider 2204117395Skan appears. */ 2205132718Skan virtual_covariant: 2206132718Skan delta = size_diffop (convert (ssizetype, 2207132718Skan BINFO_OFFSET (TREE_VALUE (overrider))), 2208132718Skan convert (ssizetype, BINFO_OFFSET (binfo))); 220918334Speter 2210132718Skan modify_vtable_entry (t, binfo, overrider_fn, delta, virtuals); 221150397Sobrien 221290075Sobrien if (virtual_base) 2213169689Skan BV_VCALL_INDEX (*virtuals) 2214132718Skan = get_vcall_index (overrider_target, BINFO_TYPE (virtual_base)); 2215169689Skan else 2216169689Skan BV_VCALL_INDEX (*virtuals) = NULL_TREE; 221718334Speter} 221818334Speter 221990075Sobrien/* Called from modify_all_vtables via dfs_walk. */ 222050397Sobrien 222190075Sobrienstatic tree 2222132718Skandfs_modify_vtables (tree binfo, void* data) 222318334Speter{ 2224169689Skan tree t = (tree) data; 2225169689Skan tree virtuals; 2226169689Skan tree old_virtuals; 2227169689Skan unsigned ix; 222818334Speter 2229169689Skan if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo))) 2230169689Skan /* A base without a vtable needs no modification, and its bases 2231169689Skan are uninteresting. */ 2232169689Skan return dfs_skip_bases; 223318334Speter 2234169689Skan if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t) 2235169689Skan && !CLASSTYPE_HAS_PRIMARY_BASE_P (t)) 2236169689Skan /* Don't do the primary vtable, if it's new. */ 2237169689Skan return NULL_TREE; 2238169689Skan 2239169689Skan if (BINFO_PRIMARY_P (binfo) && !BINFO_VIRTUAL_P (binfo)) 2240169689Skan /* There's no need to modify the vtable for a non-virtual primary 2241169689Skan base; we're not going to use that vtable anyhow. We do still 2242169689Skan need to do this for virtual primary bases, as they could become 2243169689Skan non-primary in a construction vtable. */ 2244169689Skan return NULL_TREE; 2245169689Skan 2246169689Skan make_new_vtable (t, binfo); 2247169689Skan 2248169689Skan /* Now, go through each of the virtual functions in the virtual 2249169689Skan function table for BINFO. Find the final overrider, and update 2250169689Skan the BINFO_VIRTUALS list appropriately. */ 2251169689Skan for (ix = 0, virtuals = BINFO_VIRTUALS (binfo), 2252169689Skan old_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo))); 2253169689Skan virtuals; 2254169689Skan ix++, virtuals = TREE_CHAIN (virtuals), 2255169689Skan old_virtuals = TREE_CHAIN (old_virtuals)) 2256169689Skan update_vtable_entry_for_fn (t, 2257169689Skan binfo, 2258169689Skan BV_FN (old_virtuals), 2259169689Skan &virtuals, ix); 2260169689Skan 226190075Sobrien return NULL_TREE; 226218334Speter} 226318334Speter 226490075Sobrien/* Update all of the primary and secondary vtables for T. Create new 226590075Sobrien vtables as required, and initialize their RTTI information. Each 2266102780Skan of the functions in VIRTUALS is declared in T and may override a 2267102780Skan virtual function from a base class; find and modify the appropriate 2268102780Skan entries to point to the overriding functions. Returns a list, in 2269102780Skan declaration order, of the virtual functions that are declared in T, 2270102780Skan but do not appear in the primary base class vtable, and which 2271102780Skan should therefore be appended to the end of the vtable for T. */ 227218334Speter 227390075Sobrienstatic tree 2274132718Skanmodify_all_vtables (tree t, tree virtuals) 227518334Speter{ 227690075Sobrien tree binfo = TYPE_BINFO (t); 227790075Sobrien tree *fnsp; 227818334Speter 227990075Sobrien /* Update all of the vtables. */ 2280169689Skan dfs_walk_once (binfo, dfs_modify_vtables, NULL, t); 228118334Speter 2282102780Skan /* Add virtual functions not already in our primary vtable. These 2283102780Skan will be both those introduced by this class, and those overridden 2284102780Skan from secondary bases. It does not include virtuals merely 2285102780Skan inherited from secondary bases. */ 2286102780Skan for (fnsp = &virtuals; *fnsp; ) 228790075Sobrien { 228890075Sobrien tree fn = TREE_VALUE (*fnsp); 228918334Speter 2290102780Skan if (!value_member (fn, BINFO_VIRTUALS (binfo)) 2291102780Skan || DECL_VINDEX (fn) == error_mark_node) 229218334Speter { 229390075Sobrien /* We don't need to adjust the `this' pointer when 229490075Sobrien calling this function. */ 229590075Sobrien BV_DELTA (*fnsp) = integer_zero_node; 229690075Sobrien BV_VCALL_INDEX (*fnsp) = NULL_TREE; 229718334Speter 2298102780Skan /* This is a function not already in our vtable. Keep it. */ 229990075Sobrien fnsp = &TREE_CHAIN (*fnsp); 230018334Speter } 230190075Sobrien else 230290075Sobrien /* We've already got an entry for this function. Skip it. */ 230390075Sobrien *fnsp = TREE_CHAIN (*fnsp); 230418334Speter } 2305117395Skan 2306102780Skan return virtuals; 230718334Speter} 230818334Speter 230990075Sobrien/* Get the base virtual function declarations in T that have the 231090075Sobrien indicated NAME. */ 231150397Sobrien 231250397Sobrienstatic tree 2313132718Skanget_basefndecls (tree name, tree t) 231450397Sobrien{ 231590075Sobrien tree methods; 231650397Sobrien tree base_fndecls = NULL_TREE; 2317169689Skan int n_baseclasses = BINFO_N_BASE_BINFOS (TYPE_BINFO (t)); 231890075Sobrien int i; 231950397Sobrien 2320117395Skan /* Find virtual functions in T with the indicated NAME. */ 2321117395Skan i = lookup_fnfields_1 (t, name); 2322117395Skan if (i != -1) 2323169689Skan for (methods = VEC_index (tree, CLASSTYPE_METHOD_VEC (t), i); 2324117395Skan methods; 2325117395Skan methods = OVL_NEXT (methods)) 2326117395Skan { 2327117395Skan tree method = OVL_CURRENT (methods); 232850397Sobrien 2329117395Skan if (TREE_CODE (method) == FUNCTION_DECL 2330117395Skan && DECL_VINDEX (method)) 2331117395Skan base_fndecls = tree_cons (NULL_TREE, method, base_fndecls); 2332117395Skan } 2333117395Skan 233450397Sobrien if (base_fndecls) 233550397Sobrien return base_fndecls; 233650397Sobrien 233750397Sobrien for (i = 0; i < n_baseclasses; i++) 233850397Sobrien { 2339169689Skan tree basetype = BINFO_TYPE (BINFO_BASE_BINFO (TYPE_BINFO (t), i)); 234090075Sobrien base_fndecls = chainon (get_basefndecls (name, basetype), 234150397Sobrien base_fndecls); 234250397Sobrien } 234350397Sobrien 234450397Sobrien return base_fndecls; 234550397Sobrien} 234650397Sobrien 234750397Sobrien/* If this declaration supersedes the declaration of 234850397Sobrien a method declared virtual in the base class, then 234950397Sobrien mark this field as being virtual as well. */ 235050397Sobrien 2351169689Skanvoid 2352132718Skancheck_for_override (tree decl, tree ctype) 235350397Sobrien{ 235490075Sobrien if (TREE_CODE (decl) == TEMPLATE_DECL) 235590075Sobrien /* In [temp.mem] we have: 235650397Sobrien 2357169689Skan A specialization of a member function template does not 2358169689Skan override a virtual function from a base class. */ 235990075Sobrien return; 236090075Sobrien if ((DECL_DESTRUCTOR_P (decl) 2361132718Skan || IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) 2362132718Skan || DECL_CONV_FN_P (decl)) 236390075Sobrien && look_for_overrides (ctype, decl) 236490075Sobrien && !DECL_STATIC_FUNCTION_P (decl)) 2365102780Skan /* Set DECL_VINDEX to a value that is neither an INTEGER_CST nor 2366102780Skan the error_mark_node so that we know it is an overriding 2367102780Skan function. */ 2368102780Skan DECL_VINDEX (decl) = decl; 2369102780Skan 237090075Sobrien if (DECL_VIRTUAL_P (decl)) 237150397Sobrien { 2372102780Skan if (!DECL_VINDEX (decl)) 237350397Sobrien DECL_VINDEX (decl) = error_mark_node; 237450397Sobrien IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = 1; 2375169689Skan if (DECL_DLLIMPORT_P (decl)) 2376169689Skan { 2377169689Skan /* When we handled the dllimport attribute we may not have known 2378169689Skan that this function is virtual We can't use dllimport 2379169689Skan semantics for a virtual method because we need to initialize 2380169689Skan the vtable entry with a constant address. */ 2381169689Skan DECL_DLLIMPORT_P (decl) = 0; 2382169689Skan DECL_ATTRIBUTES (decl) 2383169689Skan = remove_attribute ("dllimport", DECL_ATTRIBUTES (decl)); 2384169689Skan } 238550397Sobrien } 238650397Sobrien} 238750397Sobrien 238850397Sobrien/* Warn about hidden virtual functions that are not overridden in t. 238950397Sobrien We know that constructors and destructors don't apply. */ 239050397Sobrien 2391169689Skanstatic void 2392132718Skanwarn_hidden (tree t) 239350397Sobrien{ 2394169689Skan VEC(tree,gc) *method_vec = CLASSTYPE_METHOD_VEC (t); 2395169689Skan tree fns; 2396169689Skan size_t i; 239750397Sobrien 239850397Sobrien /* We go through each separately named virtual function. */ 2399169689Skan for (i = CLASSTYPE_FIRST_CONVERSION_SLOT; 2400169689Skan VEC_iterate (tree, method_vec, i, fns); 2401169689Skan ++i) 240250397Sobrien { 2403169689Skan tree fn; 240490075Sobrien tree name; 240550397Sobrien tree fndecl; 240690075Sobrien tree base_fndecls; 2407169689Skan tree base_binfo; 2408169689Skan tree binfo; 240990075Sobrien int j; 241050397Sobrien 241190075Sobrien /* All functions in this slot in the CLASSTYPE_METHOD_VEC will 241290075Sobrien have the same name. Figure out what name that is. */ 2413169689Skan name = DECL_NAME (OVL_CURRENT (fns)); 241490075Sobrien /* There are no possibly hidden functions yet. */ 241590075Sobrien base_fndecls = NULL_TREE; 241690075Sobrien /* Iterate through all of the base classes looking for possibly 241790075Sobrien hidden functions. */ 2418169689Skan for (binfo = TYPE_BINFO (t), j = 0; 2419169689Skan BINFO_BASE_ITERATE (binfo, j, base_binfo); j++) 242052284Sobrien { 2421169689Skan tree basetype = BINFO_TYPE (base_binfo); 242290075Sobrien base_fndecls = chainon (get_basefndecls (name, basetype), 242390075Sobrien base_fndecls); 242452284Sobrien } 242552284Sobrien 2426117395Skan /* If there are no functions to hide, continue. */ 242790075Sobrien if (!base_fndecls) 242850397Sobrien continue; 242950397Sobrien 2430117395Skan /* Remove any overridden functions. */ 2431169689Skan for (fn = fns; fn; fn = OVL_NEXT (fn)) 243250397Sobrien { 2433169689Skan fndecl = OVL_CURRENT (fn); 243490075Sobrien if (DECL_VINDEX (fndecl)) 243590075Sobrien { 243690075Sobrien tree *prev = &base_fndecls; 2437169689Skan 2438169689Skan while (*prev) 243990075Sobrien /* If the method from the base class has the same 244090075Sobrien signature as the method from the derived class, it 244190075Sobrien has been overridden. */ 244290075Sobrien if (same_signature_p (fndecl, TREE_VALUE (*prev))) 244390075Sobrien *prev = TREE_CHAIN (*prev); 244490075Sobrien else 244590075Sobrien prev = &TREE_CHAIN (*prev); 244690075Sobrien } 244750397Sobrien } 244850397Sobrien 244950397Sobrien /* Now give a warning for all base functions without overriders, 245050397Sobrien as they are hidden. */ 2451169689Skan while (base_fndecls) 245250397Sobrien { 245390075Sobrien /* Here we know it is a hider, and no overrider exists. */ 2454169689Skan warning (0, "%q+D was hidden", TREE_VALUE (base_fndecls)); 2455169689Skan warning (0, " by %q+D", fns); 245690075Sobrien base_fndecls = TREE_CHAIN (base_fndecls); 245750397Sobrien } 245850397Sobrien } 245950397Sobrien} 246050397Sobrien 246150397Sobrien/* Check for things that are invalid. There are probably plenty of other 246250397Sobrien things we should check for also. */ 246350397Sobrien 246450397Sobrienstatic void 2465132718Skanfinish_struct_anon (tree t) 246650397Sobrien{ 246750397Sobrien tree field; 246890075Sobrien 246950397Sobrien for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field)) 247050397Sobrien { 247150397Sobrien if (TREE_STATIC (field)) 247250397Sobrien continue; 247350397Sobrien if (TREE_CODE (field) != FIELD_DECL) 247450397Sobrien continue; 247550397Sobrien 247650397Sobrien if (DECL_NAME (field) == NULL_TREE 247790075Sobrien && ANON_AGGR_TYPE_P (TREE_TYPE (field))) 247850397Sobrien { 247990075Sobrien tree elt = TYPE_FIELDS (TREE_TYPE (field)); 248090075Sobrien for (; elt; elt = TREE_CHAIN (elt)) 248150397Sobrien { 248290075Sobrien /* We're generally only interested in entities the user 248390075Sobrien declared, but we also find nested classes by noticing 248490075Sobrien the TYPE_DECL that we create implicitly. You're 248590075Sobrien allowed to put one anonymous union inside another, 248690075Sobrien though, so we explicitly tolerate that. We use 248790075Sobrien TYPE_ANONYMOUS_P rather than ANON_AGGR_TYPE_P so that 248890075Sobrien we also allow unnamed types used for defining fields. */ 2489169689Skan if (DECL_ARTIFICIAL (elt) 249090075Sobrien && (!DECL_IMPLICIT_TYPEDEF_P (elt) 249190075Sobrien || TYPE_ANONYMOUS_P (TREE_TYPE (elt)))) 249250397Sobrien continue; 249350397Sobrien 249490075Sobrien if (TREE_CODE (elt) != FIELD_DECL) 249552284Sobrien { 2496169689Skan pedwarn ("%q+#D invalid; an anonymous union can " 2497169689Skan "only have non-static data members", elt); 249852284Sobrien continue; 249952284Sobrien } 250052284Sobrien 250190075Sobrien if (TREE_PRIVATE (elt)) 2502169689Skan pedwarn ("private member %q+#D in anonymous union", elt); 250390075Sobrien else if (TREE_PROTECTED (elt)) 2504169689Skan pedwarn ("protected member %q+#D in anonymous union", elt); 250550397Sobrien 250690075Sobrien TREE_PRIVATE (elt) = TREE_PRIVATE (field); 250790075Sobrien TREE_PROTECTED (elt) = TREE_PROTECTED (field); 250850397Sobrien } 250950397Sobrien } 251050397Sobrien } 251150397Sobrien} 251250397Sobrien 2513117395Skan/* Add T to CLASSTYPE_DECL_LIST of current_class_type which 2514117395Skan will be used later during class template instantiation. 2515117395Skan When FRIEND_P is zero, T can be a static member data (VAR_DECL), 2516117395Skan a non-static member data (FIELD_DECL), a member function 2517169689Skan (FUNCTION_DECL), a nested type (RECORD_TYPE, ENUM_TYPE), 2518117395Skan a typedef (TYPE_DECL) or a member class template (TEMPLATE_DECL) 2519117395Skan When FRIEND_P is nonzero, T is either a friend class 2520117395Skan (RECORD_TYPE, TEMPLATE_DECL) or a friend function 2521117395Skan (FUNCTION_DECL, TEMPLATE_DECL). */ 2522117395Skan 2523117395Skanvoid 2524132718Skanmaybe_add_class_template_decl_list (tree type, tree t, int friend_p) 2525117395Skan{ 2526117395Skan /* Save some memory by not creating TREE_LIST if TYPE is not template. */ 2527117395Skan if (CLASSTYPE_TEMPLATE_INFO (type)) 2528117395Skan CLASSTYPE_DECL_LIST (type) 2529117395Skan = tree_cons (friend_p ? NULL_TREE : type, 2530117395Skan t, CLASSTYPE_DECL_LIST (type)); 2531117395Skan} 2532117395Skan 253352284Sobrien/* Create default constructors, assignment operators, and so forth for 2534169689Skan the type indicated by T, if they are needed. CANT_HAVE_CONST_CTOR, 2535169689Skan and CANT_HAVE_CONST_ASSIGNMENT are nonzero if, for whatever reason, 2536169689Skan the class cannot have a default constructor, copy constructor 2537169689Skan taking a const reference argument, or an assignment operator taking 2538169689Skan a const reference, respectively. */ 253952284Sobrien 2540117395Skanstatic void 2541169689Skanadd_implicitly_declared_members (tree t, 2542132718Skan int cant_have_const_cctor, 2543132718Skan int cant_have_const_assignment) 254452284Sobrien{ 254552284Sobrien /* Destructor. */ 2546169689Skan if (!CLASSTYPE_DESTRUCTORS (t)) 254752284Sobrien { 2548169689Skan /* In general, we create destructors lazily. */ 2549169689Skan CLASSTYPE_LAZY_DESTRUCTOR (t) = 1; 2550169689Skan /* However, if the implicit destructor is non-trivial 2551169689Skan destructor, we sometimes have to create it at this point. */ 2552169689Skan if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)) 255352284Sobrien { 2554169689Skan bool lazy_p = true; 255552284Sobrien 2556261188Spfg /* APPLE LOCAL begin omit calls to empty destructors 5559195 */ 2557261188Spfg /* Since this is an empty destructor, it can only be nontrivial 2558261188Spfg because one of its base classes has a destructor that must be 2559261188Spfg called. */ 2560261188Spfg CLASSTYPE_DESTRUCTOR_NONTRIVIAL_BECAUSE_OF_BASE (t) = 1; 2561261188Spfg /* APPLE LOCAL end omit calls to empty destructors 5559195 */ 2562261188Spfg 2563169689Skan if (TYPE_FOR_JAVA (t)) 2564169689Skan /* If this a Java class, any non-trivial destructor is 2565169689Skan invalid, even if compiler-generated. Therefore, if the 2566169689Skan destructor is non-trivial we create it now. */ 2567169689Skan lazy_p = false; 2568169689Skan else 2569169689Skan { 2570169689Skan tree binfo; 2571169689Skan tree base_binfo; 2572169689Skan int ix; 2573169689Skan 2574169689Skan /* If the implicit destructor will be virtual, then we must 2575169689Skan generate it now because (unfortunately) we do not 2576169689Skan generate virtual tables lazily. */ 2577169689Skan binfo = TYPE_BINFO (t); 2578169689Skan for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++) 2579169689Skan { 2580169689Skan tree base_type; 2581169689Skan tree dtor; 2582169689Skan 2583169689Skan base_type = BINFO_TYPE (base_binfo); 2584169689Skan dtor = CLASSTYPE_DESTRUCTORS (base_type); 2585169689Skan if (dtor && DECL_VIRTUAL_P (dtor)) 2586169689Skan { 2587169689Skan lazy_p = false; 2588169689Skan break; 2589169689Skan } 2590169689Skan } 2591169689Skan } 2592169689Skan 2593169689Skan /* If we can't get away with being lazy, generate the destructor 2594169689Skan now. */ 2595169689Skan if (!lazy_p) 2596169689Skan lazily_declare_fn (sfk_destructor, t); 259752284Sobrien } 259852284Sobrien } 259952284Sobrien 260052284Sobrien /* Default constructor. */ 2601169689Skan if (! TYPE_HAS_CONSTRUCTOR (t)) 260252284Sobrien { 2603169689Skan TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 1; 2604169689Skan CLASSTYPE_LAZY_DEFAULT_CTOR (t) = 1; 260552284Sobrien } 260652284Sobrien 260752284Sobrien /* Copy constructor. */ 260890075Sobrien if (! TYPE_HAS_INIT_REF (t) && ! TYPE_FOR_JAVA (t)) 260952284Sobrien { 2610169689Skan TYPE_HAS_INIT_REF (t) = 1; 2611169689Skan TYPE_HAS_CONST_INIT_REF (t) = !cant_have_const_cctor; 2612169689Skan CLASSTYPE_LAZY_COPY_CTOR (t) = 1; 2613169689Skan TYPE_HAS_CONSTRUCTOR (t) = 1; 261452284Sobrien } 261552284Sobrien 2616169689Skan /* If there is no assignment operator, one will be created if and 2617169689Skan when it is needed. For now, just record whether or not the type 2618169689Skan of the parameter to the assignment operator will be a const or 2619169689Skan non-const reference. */ 2620169689Skan if (!TYPE_HAS_ASSIGN_REF (t) && !TYPE_FOR_JAVA (t)) 262152284Sobrien { 2622169689Skan TYPE_HAS_ASSIGN_REF (t) = 1; 2623169689Skan TYPE_HAS_CONST_ASSIGN_REF (t) = !cant_have_const_assignment; 2624169689Skan CLASSTYPE_LAZY_ASSIGNMENT_OP (t) = 1; 262552284Sobrien } 262652284Sobrien} 262752284Sobrien 262890075Sobrien/* Subroutine of finish_struct_1. Recursively count the number of fields 262990075Sobrien in TYPE, including anonymous union members. */ 263018334Speter 263190075Sobrienstatic int 2632132718Skancount_fields (tree fields) 263390075Sobrien{ 263490075Sobrien tree x; 263590075Sobrien int n_fields = 0; 263690075Sobrien for (x = fields; x; x = TREE_CHAIN (x)) 263790075Sobrien { 263890075Sobrien if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x))) 263990075Sobrien n_fields += count_fields (TYPE_FIELDS (TREE_TYPE (x))); 264090075Sobrien else 264190075Sobrien n_fields += 1; 264290075Sobrien } 264390075Sobrien return n_fields; 264490075Sobrien} 264518334Speter 264690075Sobrien/* Subroutine of finish_struct_1. Recursively add all the fields in the 2647132718Skan TREE_LIST FIELDS to the SORTED_FIELDS_TYPE elts, starting at offset IDX. */ 264818334Speter 264990075Sobrienstatic int 2650132718Skanadd_fields_to_record_type (tree fields, struct sorted_fields_type *field_vec, int idx) 265118334Speter{ 265290075Sobrien tree x; 265390075Sobrien for (x = fields; x; x = TREE_CHAIN (x)) 265418334Speter { 265590075Sobrien if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x))) 2656132718Skan idx = add_fields_to_record_type (TYPE_FIELDS (TREE_TYPE (x)), field_vec, idx); 265718334Speter else 2658132718Skan field_vec->elts[idx++] = x; 265918334Speter } 266090075Sobrien return idx; 266190075Sobrien} 266218334Speter 266390075Sobrien/* FIELD is a bit-field. We are finishing the processing for its 266490075Sobrien enclosing type. Issue any appropriate messages and set appropriate 266590075Sobrien flags. */ 266618334Speter 266790075Sobrienstatic void 2668132718Skancheck_bitfield_decl (tree field) 266990075Sobrien{ 267090075Sobrien tree type = TREE_TYPE (field); 2671169689Skan tree w; 267218334Speter 2673169689Skan /* Extract the declared width of the bitfield, which has been 2674169689Skan temporarily stashed in DECL_INITIAL. */ 2675169689Skan w = DECL_INITIAL (field); 2676169689Skan gcc_assert (w != NULL_TREE); 2677169689Skan /* Remove the bit-field width indicator so that the rest of the 2678169689Skan compiler does not treat that value as an initializer. */ 2679169689Skan DECL_INITIAL (field) = NULL_TREE; 2680169689Skan 268190075Sobrien /* Detect invalid bit-field type. */ 2682169689Skan if (!INTEGRAL_TYPE_P (type)) 268318334Speter { 2684169689Skan error ("bit-field %q+#D with non-integral type", field); 2685169689Skan TREE_TYPE (field) = error_mark_node; 268690075Sobrien w = error_mark_node; 268718334Speter } 2688169689Skan else 268990075Sobrien { 269090075Sobrien /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs. */ 269190075Sobrien STRIP_NOPS (w); 269218334Speter 269390075Sobrien /* detect invalid field size. */ 2694169689Skan w = integral_constant_value (w); 269518334Speter 269690075Sobrien if (TREE_CODE (w) != INTEGER_CST) 269790075Sobrien { 2698169689Skan error ("bit-field %q+D width not an integer constant", field); 269990075Sobrien w = error_mark_node; 270090075Sobrien } 270190075Sobrien else if (tree_int_cst_sgn (w) < 0) 270290075Sobrien { 2703169689Skan error ("negative width in bit-field %q+D", field); 270490075Sobrien w = error_mark_node; 270590075Sobrien } 270690075Sobrien else if (integer_zerop (w) && DECL_NAME (field) != 0) 270790075Sobrien { 2708169689Skan error ("zero width for bit-field %q+D", field); 270990075Sobrien w = error_mark_node; 271090075Sobrien } 271190075Sobrien else if (compare_tree_int (w, TYPE_PRECISION (type)) > 0 271290075Sobrien && TREE_CODE (type) != ENUMERAL_TYPE 271390075Sobrien && TREE_CODE (type) != BOOLEAN_TYPE) 2714169689Skan warning (0, "width of %q+D exceeds its type", field); 271590075Sobrien else if (TREE_CODE (type) == ENUMERAL_TYPE 271690075Sobrien && (0 > compare_tree_int (w, 271790075Sobrien min_precision (TYPE_MIN_VALUE (type), 2718169689Skan TYPE_UNSIGNED (type))) 271990075Sobrien || 0 > compare_tree_int (w, 272090075Sobrien min_precision 272190075Sobrien (TYPE_MAX_VALUE (type), 2722169689Skan TYPE_UNSIGNED (type))))) 2723169689Skan warning (0, "%q+D is too small to hold all values of %q#T", field, type); 272490075Sobrien } 272590075Sobrien 272690075Sobrien if (w != error_mark_node) 272718334Speter { 272890075Sobrien DECL_SIZE (field) = convert (bitsizetype, w); 272990075Sobrien DECL_BIT_FIELD (field) = 1; 273018334Speter } 273118334Speter else 273218334Speter { 273390075Sobrien /* Non-bit-fields are aligned for their type. */ 273490075Sobrien DECL_BIT_FIELD (field) = 0; 273590075Sobrien CLEAR_DECL_C_BIT_FIELD (field); 273618334Speter } 273790075Sobrien} 273818334Speter 273990075Sobrien/* FIELD is a non bit-field. We are finishing the processing for its 274090075Sobrien enclosing type T. Issue any appropriate messages and set appropriate 274190075Sobrien flags. */ 274290075Sobrien 274390075Sobrienstatic void 2744132718Skancheck_field_decl (tree field, 2745169689Skan tree t, 2746169689Skan int* cant_have_const_ctor, 2747169689Skan int* no_const_asn_ref, 2748132718Skan int* any_default_members) 274990075Sobrien{ 275090075Sobrien tree type = strip_array_types (TREE_TYPE (field)); 275190075Sobrien 275290075Sobrien /* An anonymous union cannot contain any fields which would change 275390075Sobrien the settings of CANT_HAVE_CONST_CTOR and friends. */ 275490075Sobrien if (ANON_UNION_TYPE_P (type)) 275590075Sobrien ; 275690075Sobrien /* And, we don't set TYPE_HAS_CONST_INIT_REF, etc., for anonymous 275790075Sobrien structs. So, we recurse through their fields here. */ 275890075Sobrien else if (ANON_AGGR_TYPE_P (type)) 275918334Speter { 276090075Sobrien tree fields; 276190075Sobrien 276290075Sobrien for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields)) 276390075Sobrien if (TREE_CODE (fields) == FIELD_DECL && !DECL_C_BIT_FIELD (field)) 276490075Sobrien check_field_decl (fields, t, cant_have_const_ctor, 2765169689Skan no_const_asn_ref, any_default_members); 276618334Speter } 276790075Sobrien /* Check members with class type for constructors, destructors, 276890075Sobrien etc. */ 276990075Sobrien else if (CLASS_TYPE_P (type)) 277090075Sobrien { 277190075Sobrien /* Never let anything with uninheritable virtuals 277290075Sobrien make it through without complaint. */ 277390075Sobrien abstract_virtuals_error (field, type); 2774169689Skan 277590075Sobrien if (TREE_CODE (t) == UNION_TYPE) 277690075Sobrien { 277790075Sobrien if (TYPE_NEEDS_CONSTRUCTING (type)) 2778169689Skan error ("member %q+#D with constructor not allowed in union", 2779169689Skan field); 278090075Sobrien if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)) 2781169689Skan error ("member %q+#D with destructor not allowed in union", field); 278290075Sobrien if (TYPE_HAS_COMPLEX_ASSIGN_REF (type)) 2783169689Skan error ("member %q+#D with copy assignment operator not allowed in union", 2784169689Skan field); 278590075Sobrien } 278690075Sobrien else 278790075Sobrien { 278890075Sobrien TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type); 2789169689Skan TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) 279090075Sobrien |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type); 279190075Sobrien TYPE_HAS_COMPLEX_ASSIGN_REF (t) |= TYPE_HAS_COMPLEX_ASSIGN_REF (type); 279290075Sobrien TYPE_HAS_COMPLEX_INIT_REF (t) |= TYPE_HAS_COMPLEX_INIT_REF (type); 279390075Sobrien } 279418334Speter 279590075Sobrien if (!TYPE_HAS_CONST_INIT_REF (type)) 279690075Sobrien *cant_have_const_ctor = 1; 279718334Speter 279890075Sobrien if (!TYPE_HAS_CONST_ASSIGN_REF (type)) 279990075Sobrien *no_const_asn_ref = 1; 280090075Sobrien } 280190075Sobrien if (DECL_INITIAL (field) != NULL_TREE) 280218334Speter { 280390075Sobrien /* `build_class_init_list' does not recognize 280490075Sobrien non-FIELD_DECLs. */ 280590075Sobrien if (TREE_CODE (t) == UNION_TYPE && any_default_members != 0) 2806169689Skan error ("multiple fields in union %qT initialized", t); 280790075Sobrien *any_default_members = 1; 280890075Sobrien } 280990075Sobrien} 281018334Speter 281190075Sobrien/* Check the data members (both static and non-static), class-scoped 281290075Sobrien typedefs, etc., appearing in the declaration of T. Issue 281390075Sobrien appropriate diagnostics. Sets ACCESS_DECLS to a list (in 281490075Sobrien declaration order) of access declarations; each TREE_VALUE in this 281590075Sobrien list is a USING_DECL. 281618334Speter 281790075Sobrien In addition, set the following flags: 281850397Sobrien 281990075Sobrien EMPTY_P 282090075Sobrien The class is empty, i.e., contains no non-static data members. 282118334Speter 282290075Sobrien CANT_HAVE_CONST_CTOR_P 282390075Sobrien This class cannot have an implicitly generated copy constructor 282490075Sobrien taking a const reference. 282590075Sobrien 282690075Sobrien CANT_HAVE_CONST_ASN_REF 282790075Sobrien This class cannot have an implicitly generated assignment 282890075Sobrien operator taking a const reference. 282990075Sobrien 283090075Sobrien All of these flags should be initialized before calling this 283190075Sobrien function. 283290075Sobrien 283390075Sobrien Returns a pointer to the end of the TYPE_FIELDs chain; additional 283490075Sobrien fields can be added by adding to this chain. */ 283590075Sobrien 283690075Sobrienstatic void 2837117395Skancheck_field_decls (tree t, tree *access_decls, 2838117395Skan int *cant_have_const_ctor_p, 2839117395Skan int *no_const_asn_ref_p) 284090075Sobrien{ 284190075Sobrien tree *field; 284290075Sobrien tree *next; 2843169689Skan bool has_pointers; 284490075Sobrien int any_default_members; 2845169689Skan int cant_pack = 0; 284690075Sobrien 284790075Sobrien /* Assume there are no access declarations. */ 284890075Sobrien *access_decls = NULL_TREE; 284990075Sobrien /* Assume this class has no pointer members. */ 2850169689Skan has_pointers = false; 285190075Sobrien /* Assume none of the members of this class have default 285290075Sobrien initializations. */ 285390075Sobrien any_default_members = 0; 285490075Sobrien 285590075Sobrien for (field = &TYPE_FIELDS (t); *field; field = next) 285618334Speter { 285790075Sobrien tree x = *field; 285890075Sobrien tree type = TREE_TYPE (x); 285990075Sobrien 286090075Sobrien next = &TREE_CHAIN (x); 286190075Sobrien 286250397Sobrien if (TREE_CODE (x) == USING_DECL) 286350397Sobrien { 286490075Sobrien /* Prune the access declaration from the list of fields. */ 286590075Sobrien *field = TREE_CHAIN (x); 286690075Sobrien 286790075Sobrien /* Save the access declarations for our caller. */ 286890075Sobrien *access_decls = tree_cons (NULL_TREE, x, *access_decls); 286990075Sobrien 287090075Sobrien /* Since we've reset *FIELD there's no reason to skip to the 287190075Sobrien next field. */ 287290075Sobrien next = field; 287318334Speter continue; 287418334Speter } 287518334Speter 287650397Sobrien if (TREE_CODE (x) == TYPE_DECL 287750397Sobrien || TREE_CODE (x) == TEMPLATE_DECL) 287818334Speter continue; 287918334Speter 288018334Speter /* If we've gotten this far, it's a data member, possibly static, 288150397Sobrien or an enumerator. */ 288290075Sobrien DECL_CONTEXT (x) = t; 288318334Speter 2884132718Skan /* When this goes into scope, it will be a non-local reference. */ 2885132718Skan DECL_NONLOCAL (x) = 1; 2886132718Skan 2887132718Skan if (TREE_CODE (t) == UNION_TYPE) 2888132718Skan { 2889132718Skan /* [class.union] 2890132718Skan 2891132718Skan If a union contains a static data member, or a member of 2892169689Skan reference type, the program is ill-formed. */ 2893132718Skan if (TREE_CODE (x) == VAR_DECL) 2894132718Skan { 2895169689Skan error ("%q+D may not be static because it is a member of a union", x); 2896132718Skan continue; 2897132718Skan } 2898132718Skan if (TREE_CODE (type) == REFERENCE_TYPE) 2899132718Skan { 2900169689Skan error ("%q+D may not have reference type %qT because" 2901169689Skan " it is a member of a union", 2902169689Skan x, type); 2903132718Skan continue; 2904132718Skan } 2905132718Skan } 2906132718Skan 290718334Speter /* Perform error checking that did not get done in 290818334Speter grokdeclarator. */ 290990075Sobrien if (TREE_CODE (type) == FUNCTION_TYPE) 291018334Speter { 2911169689Skan error ("field %q+D invalidly declared function type", x); 291290075Sobrien type = build_pointer_type (type); 291390075Sobrien TREE_TYPE (x) = type; 291418334Speter } 291590075Sobrien else if (TREE_CODE (type) == METHOD_TYPE) 291618334Speter { 2917169689Skan error ("field %q+D invalidly declared method type", x); 291890075Sobrien type = build_pointer_type (type); 291990075Sobrien TREE_TYPE (x) = type; 292018334Speter } 292118334Speter 292290075Sobrien if (type == error_mark_node) 292318334Speter continue; 2924169689Skan 2925132718Skan if (TREE_CODE (x) == CONST_DECL || TREE_CODE (x) == VAR_DECL) 292618334Speter continue; 292718334Speter 292818334Speter /* Now it can only be a FIELD_DECL. */ 292918334Speter 293018334Speter if (TREE_PRIVATE (x) || TREE_PROTECTED (x)) 293190075Sobrien CLASSTYPE_NON_AGGREGATE (t) = 1; 293218334Speter 293318334Speter /* If this is of reference type, check if it needs an init. 293418334Speter Also do a little ANSI jig if necessary. */ 293590075Sobrien if (TREE_CODE (type) == REFERENCE_TYPE) 2936169689Skan { 293790075Sobrien CLASSTYPE_NON_POD_P (t) = 1; 293818334Speter if (DECL_INITIAL (x) == NULL_TREE) 2939107590Sobrien SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1); 294018334Speter 294118334Speter /* ARM $12.6.2: [A member initializer list] (or, for an 294218334Speter aggregate, initialization by a brace-enclosed list) is the 294318334Speter only way to initialize nonstatic const and reference 294418334Speter members. */ 294550397Sobrien TYPE_HAS_COMPLEX_ASSIGN_REF (t) = 1; 294618334Speter 2947117395Skan if (! TYPE_HAS_CONSTRUCTOR (t) && CLASSTYPE_NON_AGGREGATE (t) 2948117395Skan && extra_warnings) 2949169689Skan warning (OPT_Wextra, "non-static reference %q+#D in class without a constructor", x); 295018334Speter } 295118334Speter 295290075Sobrien type = strip_array_types (type); 295350397Sobrien 2954169689Skan if (TYPE_PACKED (t)) 2955169689Skan { 2956169689Skan if (!pod_type_p (type) && !TYPE_PACKED (type)) 2957169689Skan { 2958169689Skan warning 2959169689Skan (0, 2960169689Skan "ignoring packed attribute because of unpacked non-POD field %q+#D", 2961169689Skan x); 2962169689Skan cant_pack = 1; 2963169689Skan } 2964169689Skan else if (TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT) 2965169689Skan DECL_PACKED (x) = 1; 2966169689Skan } 2967169689Skan 2968169689Skan if (DECL_C_BIT_FIELD (x) && integer_zerop (DECL_INITIAL (x))) 2969169689Skan /* We don't treat zero-width bitfields as making a class 2970169689Skan non-empty. */ 2971169689Skan ; 2972169689Skan else 2973169689Skan { 2974169689Skan /* The class is non-empty. */ 2975169689Skan CLASSTYPE_EMPTY_P (t) = 0; 2976169689Skan /* The class is not even nearly empty. */ 2977169689Skan CLASSTYPE_NEARLY_EMPTY_P (t) = 0; 2978169689Skan /* If one of the data members contains an empty class, 2979169689Skan so does T. */ 2980169689Skan if (CLASS_TYPE_P (type) 2981169689Skan && CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type)) 2982169689Skan CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1; 2983169689Skan } 2984169689Skan 2985169689Skan /* This is used by -Weffc++ (see below). Warn only for pointers 2986169689Skan to members which might hold dynamic memory. So do not warn 2987169689Skan for pointers to functions or pointers to members. */ 2988169689Skan if (TYPE_PTR_P (type) 2989169689Skan && !TYPE_PTRFN_P (type) 2990169689Skan && !TYPE_PTR_TO_MEMBER_P (type)) 2991169689Skan has_pointers = true; 2992169689Skan 2993132718Skan if (CLASS_TYPE_P (type)) 2994132718Skan { 2995132718Skan if (CLASSTYPE_REF_FIELDS_NEED_INIT (type)) 2996132718Skan SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1); 2997132718Skan if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (type)) 2998132718Skan SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1); 2999132718Skan } 3000132718Skan 300190075Sobrien if (DECL_MUTABLE_P (x) || TYPE_HAS_MUTABLE_P (type)) 300290075Sobrien CLASSTYPE_HAS_MUTABLE (t) = 1; 300352284Sobrien 300490075Sobrien if (! pod_type_p (type)) 3005169689Skan /* DR 148 now allows pointers to members (which are POD themselves), 3006169689Skan to be allowed in POD structs. */ 300790075Sobrien CLASSTYPE_NON_POD_P (t) = 1; 300890075Sobrien 3009102780Skan if (! zero_init_p (type)) 3010102780Skan CLASSTYPE_NON_ZERO_INIT_P (t) = 1; 3011102780Skan 301218334Speter /* If any field is const, the structure type is pseudo-const. */ 301390075Sobrien if (CP_TYPE_CONST_P (type)) 301418334Speter { 301518334Speter C_TYPE_FIELDS_READONLY (t) = 1; 301618334Speter if (DECL_INITIAL (x) == NULL_TREE) 3017107590Sobrien SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1); 301818334Speter 301918334Speter /* ARM $12.6.2: [A member initializer list] (or, for an 302018334Speter aggregate, initialization by a brace-enclosed list) is the 302118334Speter only way to initialize nonstatic const and reference 302218334Speter members. */ 302350397Sobrien TYPE_HAS_COMPLEX_ASSIGN_REF (t) = 1; 302418334Speter 3025117395Skan if (! TYPE_HAS_CONSTRUCTOR (t) && CLASSTYPE_NON_AGGREGATE (t) 3026117395Skan && extra_warnings) 3027169689Skan warning (OPT_Wextra, "non-static const member %q+#D in class without a constructor", x); 302818334Speter } 302990075Sobrien /* A field that is pseudo-const makes the structure likewise. */ 3030132718Skan else if (CLASS_TYPE_P (type)) 303118334Speter { 303290075Sobrien C_TYPE_FIELDS_READONLY (t) |= C_TYPE_FIELDS_READONLY (type); 3033107590Sobrien SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 3034107590Sobrien CLASSTYPE_READONLY_FIELDS_NEED_INIT (t) 3035107590Sobrien | CLASSTYPE_READONLY_FIELDS_NEED_INIT (type)); 303618334Speter } 303718334Speter 303890075Sobrien /* Core issue 80: A nonstatic data member is required to have a 303990075Sobrien different name from the class iff the class has a 304090075Sobrien user-defined constructor. */ 3041132718Skan if (constructor_name_p (DECL_NAME (x), t) && TYPE_HAS_CONSTRUCTOR (t)) 3042169689Skan pedwarn ("field %q+#D with same name as class", x); 304390075Sobrien 304452284Sobrien /* We set DECL_C_BIT_FIELD in grokbitfield. 304552284Sobrien If the type and width are valid, we'll also set DECL_BIT_FIELD. */ 304652284Sobrien if (DECL_C_BIT_FIELD (x)) 304790075Sobrien check_bitfield_decl (x); 304890075Sobrien else 304990075Sobrien check_field_decl (x, t, 305090075Sobrien cant_have_const_ctor_p, 305190075Sobrien no_const_asn_ref_p, 305290075Sobrien &any_default_members); 305390075Sobrien } 305490075Sobrien 3055169689Skan /* Effective C++ rule 11: if a class has dynamic memory held by pointers, 3056169689Skan it should also define a copy constructor and an assignment operator to 3057169689Skan implement the correct copy semantic (deep vs shallow, etc.). As it is 3058169689Skan not feasible to check whether the constructors do allocate dynamic memory 3059169689Skan and store it within members, we approximate the warning like this: 3060169689Skan 3061169689Skan -- Warn only if there are members which are pointers 3062169689Skan -- Warn only if there is a non-trivial constructor (otherwise, 3063169689Skan there cannot be memory allocated). 3064169689Skan -- Warn only if there is a non-trivial destructor. We assume that the 3065169689Skan user at least implemented the cleanup correctly, and a destructor 3066169689Skan is needed to free dynamic memory. 3067169689Skan 3068169689Skan This seems enough for practical purposes. */ 3069169689Skan if (warn_ecpp 3070169689Skan && has_pointers 3071169689Skan && TYPE_HAS_CONSTRUCTOR (t) 3072169689Skan && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) 3073169689Skan && !(TYPE_HAS_INIT_REF (t) && TYPE_HAS_ASSIGN_REF (t))) 307490075Sobrien { 3075169689Skan warning (OPT_Weffc__, "%q#T has pointer data members", t); 3076169689Skan 307790075Sobrien if (! TYPE_HAS_INIT_REF (t)) 307818334Speter { 3079169689Skan warning (OPT_Weffc__, 3080169689Skan " but does not override %<%T(const %T&)%>", t, t); 3081169689Skan if (!TYPE_HAS_ASSIGN_REF (t)) 3082169689Skan warning (OPT_Weffc__, " or %<operator=(const %T&)%>", t); 308390075Sobrien } 308490075Sobrien else if (! TYPE_HAS_ASSIGN_REF (t)) 3085169689Skan warning (OPT_Weffc__, 3086169689Skan " but does not override %<operator=(const %T&)%>", t); 308790075Sobrien } 308818334Speter 3089169689Skan /* If any of the fields couldn't be packed, unset TYPE_PACKED. */ 3090169689Skan if (cant_pack) 3091169689Skan TYPE_PACKED (t) = 0; 309218334Speter 309390075Sobrien /* Check anonymous struct/anonymous union fields. */ 309490075Sobrien finish_struct_anon (t); 309550397Sobrien 309690075Sobrien /* We've built up the list of access declarations in reverse order. 309790075Sobrien Fix that now. */ 309890075Sobrien *access_decls = nreverse (*access_decls); 309990075Sobrien} 310050397Sobrien 310190075Sobrien/* If TYPE is an empty class type, records its OFFSET in the table of 310290075Sobrien OFFSETS. */ 310318334Speter 310490075Sobrienstatic int 3105132718Skanrecord_subobject_offset (tree type, tree offset, splay_tree offsets) 310690075Sobrien{ 310790075Sobrien splay_tree_node n; 310852284Sobrien 310990075Sobrien if (!is_empty_class (type)) 311090075Sobrien return 0; 311190075Sobrien 311290075Sobrien /* Record the location of this empty object in OFFSETS. */ 311390075Sobrien n = splay_tree_lookup (offsets, (splay_tree_key) offset); 311490075Sobrien if (!n) 3115169689Skan n = splay_tree_insert (offsets, 311690075Sobrien (splay_tree_key) offset, 311790075Sobrien (splay_tree_value) NULL_TREE); 3118169689Skan n->value = ((splay_tree_value) 311990075Sobrien tree_cons (NULL_TREE, 312090075Sobrien type, 312190075Sobrien (tree) n->value)); 312290075Sobrien 312390075Sobrien return 0; 312490075Sobrien} 312590075Sobrien 3126117395Skan/* Returns nonzero if TYPE is an empty class type and there is 312790075Sobrien already an entry in OFFSETS for the same TYPE as the same OFFSET. */ 312890075Sobrien 312990075Sobrienstatic int 3130132718Skancheck_subobject_offset (tree type, tree offset, splay_tree offsets) 313190075Sobrien{ 313290075Sobrien splay_tree_node n; 313390075Sobrien tree t; 313490075Sobrien 313590075Sobrien if (!is_empty_class (type)) 313690075Sobrien return 0; 313790075Sobrien 313890075Sobrien /* Record the location of this empty object in OFFSETS. */ 313990075Sobrien n = splay_tree_lookup (offsets, (splay_tree_key) offset); 314090075Sobrien if (!n) 314190075Sobrien return 0; 314290075Sobrien 314390075Sobrien for (t = (tree) n->value; t; t = TREE_CHAIN (t)) 314490075Sobrien if (same_type_p (TREE_VALUE (t), type)) 314590075Sobrien return 1; 314690075Sobrien 314790075Sobrien return 0; 314890075Sobrien} 314990075Sobrien 315090075Sobrien/* Walk through all the subobjects of TYPE (located at OFFSET). Call 315190075Sobrien F for every subobject, passing it the type, offset, and table of 3152117395Skan OFFSETS. If VBASES_P is one, then virtual non-primary bases should 3153117395Skan be traversed. 315490075Sobrien 315590075Sobrien If MAX_OFFSET is non-NULL, then subobjects with an offset greater 315690075Sobrien than MAX_OFFSET will not be walked. 315790075Sobrien 3158117395Skan If F returns a nonzero value, the traversal ceases, and that value 315990075Sobrien is returned. Otherwise, returns zero. */ 316090075Sobrien 316190075Sobrienstatic int 3162169689Skanwalk_subobject_offsets (tree type, 3163169689Skan subobject_offset_fn f, 3164169689Skan tree offset, 3165169689Skan splay_tree offsets, 3166169689Skan tree max_offset, 3167169689Skan int vbases_p) 316890075Sobrien{ 316990075Sobrien int r = 0; 3170117395Skan tree type_binfo = NULL_TREE; 317190075Sobrien 317290075Sobrien /* If this OFFSET is bigger than the MAX_OFFSET, then we should 317390075Sobrien stop. */ 317490075Sobrien if (max_offset && INT_CST_LT (max_offset, offset)) 317590075Sobrien return 0; 317690075Sobrien 3177169689Skan if (type == error_mark_node) 3178169689Skan return 0; 3179169689Skan 3180169689Skan if (!TYPE_P (type)) 3181117395Skan { 3182117395Skan if (abi_version_at_least (2)) 3183117395Skan type_binfo = type; 3184117395Skan type = BINFO_TYPE (type); 3185117395Skan } 3186117395Skan 318790075Sobrien if (CLASS_TYPE_P (type)) 318890075Sobrien { 318990075Sobrien tree field; 3190117395Skan tree binfo; 319190075Sobrien int i; 319290075Sobrien 3193107590Sobrien /* Avoid recursing into objects that are not interesting. */ 3194107590Sobrien if (!CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type)) 3195107590Sobrien return 0; 3196107590Sobrien 319790075Sobrien /* Record the location of TYPE. */ 319890075Sobrien r = (*f) (type, offset, offsets); 319990075Sobrien if (r) 320090075Sobrien return r; 320190075Sobrien 320290075Sobrien /* Iterate through the direct base classes of TYPE. */ 3203117395Skan if (!type_binfo) 3204117395Skan type_binfo = TYPE_BINFO (type); 3205169689Skan for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, binfo); i++) 320690075Sobrien { 3207117395Skan tree binfo_offset; 320890075Sobrien 3209169689Skan if (abi_version_at_least (2) 3210169689Skan && BINFO_VIRTUAL_P (binfo)) 3211117395Skan continue; 3212117395Skan 3213169689Skan if (!vbases_p 3214169689Skan && BINFO_VIRTUAL_P (binfo) 321590075Sobrien && !BINFO_PRIMARY_P (binfo)) 321690075Sobrien continue; 321790075Sobrien 3218117395Skan if (!abi_version_at_least (2)) 3219117395Skan binfo_offset = size_binop (PLUS_EXPR, 3220117395Skan offset, 3221117395Skan BINFO_OFFSET (binfo)); 3222117395Skan else 3223117395Skan { 3224117395Skan tree orig_binfo; 3225117395Skan /* We cannot rely on BINFO_OFFSET being set for the base 3226117395Skan class yet, but the offsets for direct non-virtual 3227117395Skan bases can be calculated by going back to the TYPE. */ 3228169689Skan orig_binfo = BINFO_BASE_BINFO (TYPE_BINFO (type), i); 3229169689Skan binfo_offset = size_binop (PLUS_EXPR, 3230117395Skan offset, 3231117395Skan BINFO_OFFSET (orig_binfo)); 3232117395Skan } 3233117395Skan 3234117395Skan r = walk_subobject_offsets (binfo, 323590075Sobrien f, 3236117395Skan binfo_offset, 323790075Sobrien offsets, 323890075Sobrien max_offset, 3239169689Skan (abi_version_at_least (2) 3240117395Skan ? /*vbases_p=*/0 : vbases_p)); 324190075Sobrien if (r) 324290075Sobrien return r; 324318334Speter } 324490075Sobrien 3245169689Skan if (abi_version_at_least (2) && CLASSTYPE_VBASECLASSES (type)) 3246117395Skan { 3247169689Skan unsigned ix; 3248169689Skan VEC(tree,gc) *vbases; 3249117395Skan 3250117395Skan /* Iterate through the virtual base classes of TYPE. In G++ 3251117395Skan 3.2, we included virtual bases in the direct base class 3252117395Skan loop above, which results in incorrect results; the 3253117395Skan correct offsets for virtual bases are only known when 3254117395Skan working with the most derived type. */ 3255117395Skan if (vbases_p) 3256169689Skan for (vbases = CLASSTYPE_VBASECLASSES (type), ix = 0; 3257169689Skan VEC_iterate (tree, vbases, ix, binfo); ix++) 3258117395Skan { 3259117395Skan r = walk_subobject_offsets (binfo, 3260117395Skan f, 3261117395Skan size_binop (PLUS_EXPR, 3262117395Skan offset, 3263117395Skan BINFO_OFFSET (binfo)), 3264117395Skan offsets, 3265117395Skan max_offset, 3266117395Skan /*vbases_p=*/0); 3267117395Skan if (r) 3268117395Skan return r; 3269117395Skan } 3270117395Skan else 3271117395Skan { 3272117395Skan /* We still have to walk the primary base, if it is 3273117395Skan virtual. (If it is non-virtual, then it was walked 3274117395Skan above.) */ 3275169689Skan tree vbase = get_primary_binfo (type_binfo); 3276169689Skan 3277169689Skan if (vbase && BINFO_VIRTUAL_P (vbase) 3278169689Skan && BINFO_PRIMARY_P (vbase) 3279169689Skan && BINFO_INHERITANCE_CHAIN (vbase) == type_binfo) 3280117395Skan { 3281169689Skan r = (walk_subobject_offsets 3282132718Skan (vbase, f, offset, 3283132718Skan offsets, max_offset, /*vbases_p=*/0)); 3284132718Skan if (r) 3285132718Skan return r; 3286117395Skan } 3287117395Skan } 3288117395Skan } 3289117395Skan 329090075Sobrien /* Iterate through the fields of TYPE. */ 329190075Sobrien for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field)) 3292117395Skan if (TREE_CODE (field) == FIELD_DECL && !DECL_ARTIFICIAL (field)) 329390075Sobrien { 3294117395Skan tree field_offset; 3295117395Skan 3296117395Skan if (abi_version_at_least (2)) 3297117395Skan field_offset = byte_position (field); 3298117395Skan else 3299117395Skan /* In G++ 3.2, DECL_FIELD_OFFSET was used. */ 3300117395Skan field_offset = DECL_FIELD_OFFSET (field); 3301117395Skan 330290075Sobrien r = walk_subobject_offsets (TREE_TYPE (field), 330390075Sobrien f, 330490075Sobrien size_binop (PLUS_EXPR, 330590075Sobrien offset, 3306117395Skan field_offset), 330790075Sobrien offsets, 330890075Sobrien max_offset, 330990075Sobrien /*vbases_p=*/1); 331090075Sobrien if (r) 331190075Sobrien return r; 331290075Sobrien } 331390075Sobrien } 331490075Sobrien else if (TREE_CODE (type) == ARRAY_TYPE) 331590075Sobrien { 3316107590Sobrien tree element_type = strip_array_types (type); 331790075Sobrien tree domain = TYPE_DOMAIN (type); 331890075Sobrien tree index; 331990075Sobrien 3320107590Sobrien /* Avoid recursing into objects that are not interesting. */ 3321107590Sobrien if (!CLASS_TYPE_P (element_type) 3322107590Sobrien || !CLASSTYPE_CONTAINS_EMPTY_CLASS_P (element_type)) 3323107590Sobrien return 0; 3324107590Sobrien 332590075Sobrien /* Step through each of the elements in the array. */ 3326117395Skan for (index = size_zero_node; 3327117395Skan /* G++ 3.2 had an off-by-one error here. */ 3328169689Skan (abi_version_at_least (2) 3329117395Skan ? !INT_CST_LT (TYPE_MAX_VALUE (domain), index) 3330117395Skan : INT_CST_LT (index, TYPE_MAX_VALUE (domain))); 333190075Sobrien index = size_binop (PLUS_EXPR, index, size_one_node)) 333290075Sobrien { 333390075Sobrien r = walk_subobject_offsets (TREE_TYPE (type), 333490075Sobrien f, 333590075Sobrien offset, 333690075Sobrien offsets, 333790075Sobrien max_offset, 333890075Sobrien /*vbases_p=*/1); 333990075Sobrien if (r) 334090075Sobrien return r; 3341169689Skan offset = size_binop (PLUS_EXPR, offset, 334290075Sobrien TYPE_SIZE_UNIT (TREE_TYPE (type))); 334390075Sobrien /* If this new OFFSET is bigger than the MAX_OFFSET, then 334490075Sobrien there's no point in iterating through the remaining 334590075Sobrien elements of the array. */ 334690075Sobrien if (max_offset && INT_CST_LT (max_offset, offset)) 334790075Sobrien break; 334890075Sobrien } 334990075Sobrien } 335090075Sobrien 335190075Sobrien return 0; 335290075Sobrien} 335390075Sobrien 3354169689Skan/* Record all of the empty subobjects of TYPE (either a type or a 3355169689Skan binfo). If IS_DATA_MEMBER is true, then a non-static data member 3356169689Skan is being placed at OFFSET; otherwise, it is a base class that is 3357169689Skan being placed at OFFSET. */ 335890075Sobrien 335990075Sobrienstatic void 3360169689Skanrecord_subobject_offsets (tree type, 3361169689Skan tree offset, 3362169689Skan splay_tree offsets, 3363169689Skan bool is_data_member) 336490075Sobrien{ 3365169689Skan tree max_offset; 3366169689Skan /* If recording subobjects for a non-static data member or a 3367169689Skan non-empty base class , we do not need to record offsets beyond 3368169689Skan the size of the biggest empty class. Additional data members 3369169689Skan will go at the end of the class. Additional base classes will go 3370169689Skan either at offset zero (if empty, in which case they cannot 3371169689Skan overlap with offsets past the size of the biggest empty class) or 3372169689Skan at the end of the class. 3373169689Skan 3374169689Skan However, if we are placing an empty base class, then we must record 3375169689Skan all offsets, as either the empty class is at offset zero (where 3376169689Skan other empty classes might later be placed) or at the end of the 3377169689Skan class (where other objects might then be placed, so other empty 3378169689Skan subobjects might later overlap). */ 3379169689Skan if (is_data_member 3380169689Skan || !is_empty_class (BINFO_TYPE (type))) 3381169689Skan max_offset = sizeof_biggest_empty_class; 3382169689Skan else 3383169689Skan max_offset = NULL_TREE; 338490075Sobrien walk_subobject_offsets (type, record_subobject_offset, offset, 3385169689Skan offsets, max_offset, is_data_member); 338690075Sobrien} 338790075Sobrien 3388117395Skan/* Returns nonzero if any of the empty subobjects of TYPE (located at 3389117395Skan OFFSET) conflict with entries in OFFSETS. If VBASES_P is nonzero, 339090075Sobrien virtual bases of TYPE are examined. */ 339190075Sobrien 339290075Sobrienstatic int 3393132718Skanlayout_conflict_p (tree type, 3394169689Skan tree offset, 3395169689Skan splay_tree offsets, 3396169689Skan int vbases_p) 339790075Sobrien{ 339890075Sobrien splay_tree_node max_node; 339990075Sobrien 340090075Sobrien /* Get the node in OFFSETS that indicates the maximum offset where 340190075Sobrien an empty subobject is located. */ 340290075Sobrien max_node = splay_tree_max (offsets); 340390075Sobrien /* If there aren't any empty subobjects, then there's no point in 340490075Sobrien performing this check. */ 340590075Sobrien if (!max_node) 340690075Sobrien return 0; 340790075Sobrien 340890075Sobrien return walk_subobject_offsets (type, check_subobject_offset, offset, 340990075Sobrien offsets, (tree) (max_node->key), 341090075Sobrien vbases_p); 341190075Sobrien} 341290075Sobrien 341390075Sobrien/* DECL is a FIELD_DECL corresponding either to a base subobject of a 341490075Sobrien non-static data member of the type indicated by RLI. BINFO is the 341590075Sobrien binfo corresponding to the base subobject, OFFSETS maps offsets to 3416117395Skan types already located at those offsets. This function determines 3417117395Skan the position of the DECL. */ 341890075Sobrien 341990075Sobrienstatic void 3420169689Skanlayout_nonempty_base_or_field (record_layout_info rli, 3421169689Skan tree decl, 3422169689Skan tree binfo, 3423117395Skan splay_tree offsets) 342490075Sobrien{ 342590075Sobrien tree offset = NULL_TREE; 3426117395Skan bool field_p; 3427117395Skan tree type; 3428169689Skan 3429117395Skan if (binfo) 3430117395Skan { 3431117395Skan /* For the purposes of determining layout conflicts, we want to 3432117395Skan use the class type of BINFO; TREE_TYPE (DECL) will be the 3433117395Skan CLASSTYPE_AS_BASE version, which does not contain entries for 3434117395Skan zero-sized bases. */ 3435117395Skan type = TREE_TYPE (binfo); 3436117395Skan field_p = false; 3437117395Skan } 3438117395Skan else 3439117395Skan { 3440117395Skan type = TREE_TYPE (decl); 3441117395Skan field_p = true; 3442117395Skan } 344390075Sobrien 344490075Sobrien /* Try to place the field. It may take more than one try if we have 344590075Sobrien a hard time placing the field without putting two objects of the 344690075Sobrien same type at the same address. */ 344790075Sobrien while (1) 344890075Sobrien { 344990075Sobrien struct record_layout_info_s old_rli = *rli; 345090075Sobrien 345190075Sobrien /* Place this field. */ 345290075Sobrien place_field (rli, decl); 345390075Sobrien offset = byte_position (decl); 3454132718Skan 345590075Sobrien /* We have to check to see whether or not there is already 345690075Sobrien something of the same type at the offset we're about to use. 3457132718Skan For example, consider: 3458169689Skan 3459132718Skan struct S {}; 3460132718Skan struct T : public S { int i; }; 3461132718Skan struct U : public S, public T {}; 3462169689Skan 346390075Sobrien Here, we put S at offset zero in U. Then, we can't put T at 346490075Sobrien offset zero -- its S component would be at the same address 346590075Sobrien as the S we already allocated. So, we have to skip ahead. 346690075Sobrien Since all data members, including those whose type is an 3467117395Skan empty class, have nonzero size, any overlap can happen only 346890075Sobrien with a direct or indirect base-class -- it can't happen with 346990075Sobrien a data member. */ 3470132718Skan /* In a union, overlap is permitted; all members are placed at 3471132718Skan offset zero. */ 3472132718Skan if (TREE_CODE (rli->t) == UNION_TYPE) 3473132718Skan break; 3474117395Skan /* G++ 3.2 did not check for overlaps when placing a non-empty 3475117395Skan virtual base. */ 3476169689Skan if (!abi_version_at_least (2) && binfo && BINFO_VIRTUAL_P (binfo)) 3477117395Skan break; 3478169689Skan if (layout_conflict_p (field_p ? type : binfo, offset, 3479117395Skan offsets, field_p)) 348090075Sobrien { 348190075Sobrien /* Strip off the size allocated to this field. That puts us 348290075Sobrien at the first place we could have put the field with 348390075Sobrien proper alignment. */ 348490075Sobrien *rli = old_rli; 348590075Sobrien 348690075Sobrien /* Bump up by the alignment required for the type. */ 348790075Sobrien rli->bitpos 3488169689Skan = size_binop (PLUS_EXPR, rli->bitpos, 3489169689Skan bitsize_int (binfo 349090075Sobrien ? CLASSTYPE_ALIGN (type) 349190075Sobrien : TYPE_ALIGN (type))); 349290075Sobrien normalize_rli (rli); 349390075Sobrien } 349418334Speter else 349590075Sobrien /* There was no conflict. We're done laying out this field. */ 349690075Sobrien break; 349790075Sobrien } 349890075Sobrien 349990075Sobrien /* Now that we know where it will be placed, update its 350090075Sobrien BINFO_OFFSET. */ 350190075Sobrien if (binfo && CLASS_TYPE_P (BINFO_TYPE (binfo))) 3502117395Skan /* Indirect virtual bases may have a nonzero BINFO_OFFSET at 3503117395Skan this point because their BINFO_OFFSET is copied from another 3504117395Skan hierarchy. Therefore, we may not need to add the entire 3505117395Skan OFFSET. */ 3506169689Skan propagate_binfo_offsets (binfo, 3507117395Skan size_diffop (convert (ssizetype, offset), 3508169689Skan convert (ssizetype, 3509132718Skan BINFO_OFFSET (binfo)))); 351090075Sobrien} 351190075Sobrien 3512117395Skan/* Returns true if TYPE is empty and OFFSET is nonzero. */ 3513117395Skan 3514117395Skanstatic int 3515117395Skanempty_base_at_nonzero_offset_p (tree type, 3516117395Skan tree offset, 3517117395Skan splay_tree offsets ATTRIBUTE_UNUSED) 3518117395Skan{ 3519117395Skan return is_empty_class (type) && !integer_zerop (offset); 3520117395Skan} 3521117395Skan 352290075Sobrien/* Layout the empty base BINFO. EOC indicates the byte currently just 352390075Sobrien past the end of the class, and should be correctly aligned for a 352490075Sobrien class of the type indicated by BINFO; OFFSETS gives the offsets of 352590075Sobrien the empty bases allocated so far. T is the most derived 3526117395Skan type. Return nonzero iff we added it at the end. */ 352790075Sobrien 352890075Sobrienstatic bool 3529132718Skanlayout_empty_base (tree binfo, tree eoc, splay_tree offsets) 353090075Sobrien{ 353190075Sobrien tree alignment; 353290075Sobrien tree basetype = BINFO_TYPE (binfo); 353390075Sobrien bool atend = false; 3534117395Skan 353590075Sobrien /* This routine should only be used for empty classes. */ 3536169689Skan gcc_assert (is_empty_class (basetype)); 353790075Sobrien alignment = ssize_int (CLASSTYPE_ALIGN_UNIT (basetype)); 3538132718Skan 3539132718Skan if (!integer_zerop (BINFO_OFFSET (binfo))) 3540132718Skan { 3541132718Skan if (abi_version_at_least (2)) 3542132718Skan propagate_binfo_offsets 3543132718Skan (binfo, size_diffop (size_zero_node, BINFO_OFFSET (binfo))); 3544169689Skan else 3545169689Skan warning (OPT_Wabi, 3546169689Skan "offset of empty base %qT may not be ABI-compliant and may" 3547132718Skan "change in a future version of GCC", 3548132718Skan BINFO_TYPE (binfo)); 3549132718Skan } 3550169689Skan 355190075Sobrien /* This is an empty base class. We first try to put it at offset 355290075Sobrien zero. */ 3553117395Skan if (layout_conflict_p (binfo, 355490075Sobrien BINFO_OFFSET (binfo), 3555169689Skan offsets, 355690075Sobrien /*vbases_p=*/0)) 355790075Sobrien { 355890075Sobrien /* That didn't work. Now, we move forward from the next 355990075Sobrien available spot in the class. */ 356090075Sobrien atend = true; 3561132718Skan propagate_binfo_offsets (binfo, convert (ssizetype, eoc)); 3562169689Skan while (1) 356318334Speter { 3564117395Skan if (!layout_conflict_p (binfo, 3565169689Skan BINFO_OFFSET (binfo), 356690075Sobrien offsets, 356790075Sobrien /*vbases_p=*/0)) 356890075Sobrien /* We finally found a spot where there's no overlap. */ 356990075Sobrien break; 357018334Speter 357190075Sobrien /* There's overlap here, too. Bump along to the next spot. */ 3572132718Skan propagate_binfo_offsets (binfo, alignment); 357390075Sobrien } 357490075Sobrien } 357590075Sobrien return atend; 357690075Sobrien} 357718334Speter 3578169689Skan/* Layout the base given by BINFO in the class indicated by RLI. 3579117395Skan *BASE_ALIGN is a running maximum of the alignments of 3580117395Skan any base class. OFFSETS gives the location of empty base 3581117395Skan subobjects. T is the most derived type. Return nonzero if the new 3582117395Skan object cannot be nearly-empty. A new FIELD_DECL is inserted at 3583169689Skan *NEXT_FIELD, unless BINFO is for an empty base class. 358418334Speter 3585117395Skan Returns the location at which the next field should be inserted. */ 3586117395Skan 3587117395Skanstatic tree * 3588117395Skanbuild_base_field (record_layout_info rli, tree binfo, 3589117395Skan splay_tree offsets, tree *next_field) 359090075Sobrien{ 3591117395Skan tree t = rli->t; 359290075Sobrien tree basetype = BINFO_TYPE (binfo); 359318334Speter 359490075Sobrien if (!COMPLETE_TYPE_P (basetype)) 359590075Sobrien /* This error is now reported in xref_tag, thus giving better 359690075Sobrien location information. */ 3597117395Skan return next_field; 3598169689Skan 3599117395Skan /* Place the base class. */ 3600117395Skan if (!is_empty_class (basetype)) 360190075Sobrien { 3602117395Skan tree decl; 3603117395Skan 360490075Sobrien /* The containing class is non-empty because it has a non-empty 360590075Sobrien base class. */ 3606117395Skan CLASSTYPE_EMPTY_P (t) = 0; 3607169689Skan 3608117395Skan /* Create the FIELD_DECL. */ 3609117395Skan decl = build_decl (FIELD_DECL, NULL_TREE, CLASSTYPE_AS_BASE (basetype)); 3610117395Skan DECL_ARTIFICIAL (decl) = 1; 3611169689Skan DECL_IGNORED_P (decl) = 1; 3612117395Skan DECL_FIELD_CONTEXT (decl) = t; 3613117395Skan DECL_SIZE (decl) = CLASSTYPE_SIZE (basetype); 3614117395Skan DECL_SIZE_UNIT (decl) = CLASSTYPE_SIZE_UNIT (basetype); 3615117395Skan DECL_ALIGN (decl) = CLASSTYPE_ALIGN (basetype); 3616117395Skan DECL_USER_ALIGN (decl) = CLASSTYPE_USER_ALIGN (basetype); 3617169689Skan DECL_MODE (decl) = TYPE_MODE (basetype); 3618169689Skan DECL_FIELD_IS_BASE (decl) = 1; 361918334Speter 362090075Sobrien /* Try to place the field. It may take more than one try if we 362190075Sobrien have a hard time placing the field without putting two 362290075Sobrien objects of the same type at the same address. */ 3623117395Skan layout_nonempty_base_or_field (rli, decl, binfo, offsets); 3624117395Skan /* Add the new FIELD_DECL to the list of fields for T. */ 3625117395Skan TREE_CHAIN (decl) = *next_field; 3626117395Skan *next_field = decl; 3627117395Skan next_field = &TREE_CHAIN (decl); 362890075Sobrien } 362990075Sobrien else 363090075Sobrien { 3631117395Skan tree eoc; 3632117395Skan bool atend; 363390075Sobrien 363490075Sobrien /* On some platforms (ARM), even empty classes will not be 363590075Sobrien byte-aligned. */ 3636117395Skan eoc = round_up (rli_size_unit_so_far (rli), 3637117395Skan CLASSTYPE_ALIGN_UNIT (basetype)); 3638132718Skan atend = layout_empty_base (binfo, eoc, offsets); 3639117395Skan /* A nearly-empty class "has no proper base class that is empty, 3640117395Skan not morally virtual, and at an offset other than zero." */ 3641169689Skan if (!BINFO_VIRTUAL_P (binfo) && CLASSTYPE_NEARLY_EMPTY_P (t)) 3642117395Skan { 3643117395Skan if (atend) 3644117395Skan CLASSTYPE_NEARLY_EMPTY_P (t) = 0; 3645169689Skan /* The check above (used in G++ 3.2) is insufficient because 3646117395Skan an empty class placed at offset zero might itself have an 3647117395Skan empty base at a nonzero offset. */ 3648169689Skan else if (walk_subobject_offsets (basetype, 3649117395Skan empty_base_at_nonzero_offset_p, 3650117395Skan size_zero_node, 3651117395Skan /*offsets=*/NULL, 3652117395Skan /*max_offset=*/NULL_TREE, 3653117395Skan /*vbases_p=*/true)) 3654117395Skan { 3655117395Skan if (abi_version_at_least (2)) 3656117395Skan CLASSTYPE_NEARLY_EMPTY_P (t) = 0; 3657169689Skan else 3658169689Skan warning (OPT_Wabi, 3659169689Skan "class %qT will be considered nearly empty in a " 3660117395Skan "future version of GCC", t); 3661117395Skan } 3662117395Skan } 3663169689Skan 3664117395Skan /* We do not create a FIELD_DECL for empty base classes because 3665117395Skan it might overlap some other field. We want to be able to 3666117395Skan create CONSTRUCTORs for the class by iterating over the 3667117395Skan FIELD_DECLs, and the back end does not handle overlapping 3668117395Skan FIELD_DECLs. */ 3669117395Skan 3670117395Skan /* An empty virtual base causes a class to be non-empty 3671117395Skan -- but in that case we do not need to clear CLASSTYPE_EMPTY_P 3672117395Skan here because that was already done when the virtual table 3673117395Skan pointer was created. */ 367490075Sobrien } 367590075Sobrien 367690075Sobrien /* Record the offsets of BINFO and its base subobjects. */ 3677117395Skan record_subobject_offsets (binfo, 367890075Sobrien BINFO_OFFSET (binfo), 3679169689Skan offsets, 3680169689Skan /*is_data_member=*/false); 3681117395Skan 3682117395Skan return next_field; 368390075Sobrien} 368490075Sobrien 368590075Sobrien/* Layout all of the non-virtual base classes. Record empty 3686117395Skan subobjects in OFFSETS. T is the most derived type. Return nonzero 3687117395Skan if the type cannot be nearly empty. The fields created 3688117395Skan corresponding to the base classes will be inserted at 3689117395Skan *NEXT_FIELD. */ 369090075Sobrien 3691117395Skanstatic void 3692117395Skanbuild_base_fields (record_layout_info rli, 3693117395Skan splay_tree offsets, tree *next_field) 369490075Sobrien{ 369590075Sobrien /* Chain to hold all the new FIELD_DECLs which stand in for base class 369690075Sobrien subobjects. */ 3697117395Skan tree t = rli->t; 3698169689Skan int n_baseclasses = BINFO_N_BASE_BINFOS (TYPE_BINFO (t)); 369990075Sobrien int i; 370090075Sobrien 370190075Sobrien /* The primary base class is always allocated first. */ 3702117395Skan if (CLASSTYPE_HAS_PRIMARY_BASE_P (t)) 3703117395Skan next_field = build_base_field (rli, CLASSTYPE_PRIMARY_BINFO (t), 3704117395Skan offsets, next_field); 370590075Sobrien 370690075Sobrien /* Now allocate the rest of the bases. */ 370790075Sobrien for (i = 0; i < n_baseclasses; ++i) 370890075Sobrien { 370990075Sobrien tree base_binfo; 371090075Sobrien 3711169689Skan base_binfo = BINFO_BASE_BINFO (TYPE_BINFO (t), i); 371290075Sobrien 371390075Sobrien /* The primary base was already allocated above, so we don't 371490075Sobrien need to allocate it again here. */ 3715117395Skan if (base_binfo == CLASSTYPE_PRIMARY_BINFO (t)) 371690075Sobrien continue; 371790075Sobrien 3718132718Skan /* Virtual bases are added at the end (a primary virtual base 3719132718Skan will have already been added). */ 3720169689Skan if (BINFO_VIRTUAL_P (base_binfo)) 372190075Sobrien continue; 372290075Sobrien 3723117395Skan next_field = build_base_field (rli, base_binfo, 3724117395Skan offsets, next_field); 372590075Sobrien } 372690075Sobrien} 372790075Sobrien 372890075Sobrien/* Go through the TYPE_METHODS of T issuing any appropriate 372990075Sobrien diagnostics, figuring out which methods override which other 373090075Sobrien methods, and so forth. */ 373190075Sobrien 373290075Sobrienstatic void 3733132718Skancheck_methods (tree t) 373490075Sobrien{ 373590075Sobrien tree x; 373690075Sobrien 373790075Sobrien for (x = TYPE_METHODS (t); x; x = TREE_CHAIN (x)) 373890075Sobrien { 373990075Sobrien check_for_override (x, t); 374090075Sobrien if (DECL_PURE_VIRTUAL_P (x) && ! DECL_VINDEX (x)) 3741169689Skan error ("initializer specified for non-virtual method %q+D", x); 374290075Sobrien /* The name of the field is the original field name 374390075Sobrien Save this in auxiliary field for later overloading. */ 374490075Sobrien if (DECL_VINDEX (x)) 374590075Sobrien { 374690075Sobrien TYPE_POLYMORPHIC_P (t) = 1; 374790075Sobrien if (DECL_PURE_VIRTUAL_P (x)) 3748169689Skan VEC_safe_push (tree, gc, CLASSTYPE_PURE_VIRTUALS (t), x); 374990075Sobrien } 3750169689Skan /* All user-declared destructors are non-trivial. */ 3751169689Skan if (DECL_DESTRUCTOR_P (x)) 3752261188Spfg /* APPLE LOCAL begin omit calls to empty destructors 5559195 */ 3753261188Spfg { 3754261188Spfg TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = 1; 3755261188Spfg 3756261188Spfg /* Conservatively assume that destructor body is nontrivial. Will 3757261188Spfg be unmarked during parsing of function body if it happens to be 3758261188Spfg trivial. */ 3759261188Spfg CLASSTYPE_HAS_NONTRIVIAL_DESTRUCTOR_BODY (t) = 1; 3760261188Spfg } 3761261188Spfg /* APPLE LOCAL end omit calls to empty destructors 5559195 */ 376290075Sobrien } 376390075Sobrien} 376490075Sobrien 376590075Sobrien/* FN is a constructor or destructor. Clone the declaration to create 376690075Sobrien a specialized in-charge or not-in-charge version, as indicated by 376790075Sobrien NAME. */ 376890075Sobrien 376990075Sobrienstatic tree 3770132718Skanbuild_clone (tree fn, tree name) 377190075Sobrien{ 377290075Sobrien tree parms; 377390075Sobrien tree clone; 377490075Sobrien 377590075Sobrien /* Copy the function. */ 377690075Sobrien clone = copy_decl (fn); 377790075Sobrien /* Remember where this function came from. */ 377890075Sobrien DECL_CLONED_FUNCTION (clone) = fn; 377990075Sobrien DECL_ABSTRACT_ORIGIN (clone) = fn; 378090075Sobrien /* Reset the function name. */ 378190075Sobrien DECL_NAME (clone) = name; 378290075Sobrien SET_DECL_ASSEMBLER_NAME (clone, NULL_TREE); 378390075Sobrien /* There's no pending inline data for this function. */ 378490075Sobrien DECL_PENDING_INLINE_INFO (clone) = NULL; 378590075Sobrien DECL_PENDING_INLINE_P (clone) = 0; 378690075Sobrien /* And it hasn't yet been deferred. */ 378790075Sobrien DECL_DEFERRED_FN (clone) = 0; 378890075Sobrien 378990075Sobrien /* The base-class destructor is not virtual. */ 379090075Sobrien if (name == base_dtor_identifier) 379190075Sobrien { 379290075Sobrien DECL_VIRTUAL_P (clone) = 0; 379390075Sobrien if (TREE_CODE (clone) != TEMPLATE_DECL) 379490075Sobrien DECL_VINDEX (clone) = NULL_TREE; 379590075Sobrien } 379690075Sobrien 379790075Sobrien /* If there was an in-charge parameter, drop it from the function 379890075Sobrien type. */ 379990075Sobrien if (DECL_HAS_IN_CHARGE_PARM_P (clone)) 380090075Sobrien { 380190075Sobrien tree basetype; 380290075Sobrien tree parmtypes; 380390075Sobrien tree exceptions; 380490075Sobrien 380590075Sobrien exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone)); 380690075Sobrien basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone)); 380790075Sobrien parmtypes = TYPE_ARG_TYPES (TREE_TYPE (clone)); 380890075Sobrien /* Skip the `this' parameter. */ 380990075Sobrien parmtypes = TREE_CHAIN (parmtypes); 381090075Sobrien /* Skip the in-charge parameter. */ 381190075Sobrien parmtypes = TREE_CHAIN (parmtypes); 381290075Sobrien /* And the VTT parm, in a complete [cd]tor. */ 381390075Sobrien if (DECL_HAS_VTT_PARM_P (fn) 381490075Sobrien && ! DECL_NEEDS_VTT_PARM_P (clone)) 381590075Sobrien parmtypes = TREE_CHAIN (parmtypes); 381690075Sobrien /* If this is subobject constructor or destructor, add the vtt 381790075Sobrien parameter. */ 3818169689Skan TREE_TYPE (clone) 3819132718Skan = build_method_type_directly (basetype, 3820132718Skan TREE_TYPE (TREE_TYPE (clone)), 3821132718Skan parmtypes); 382290075Sobrien if (exceptions) 382390075Sobrien TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), 382490075Sobrien exceptions); 3825169689Skan TREE_TYPE (clone) 3826132718Skan = cp_build_type_attribute_variant (TREE_TYPE (clone), 3827132718Skan TYPE_ATTRIBUTES (TREE_TYPE (fn))); 382890075Sobrien } 382990075Sobrien 383090075Sobrien /* Copy the function parameters. But, DECL_ARGUMENTS on a TEMPLATE_DECL 383190075Sobrien aren't function parameters; those are the template parameters. */ 383290075Sobrien if (TREE_CODE (clone) != TEMPLATE_DECL) 383390075Sobrien { 383490075Sobrien DECL_ARGUMENTS (clone) = copy_list (DECL_ARGUMENTS (clone)); 383590075Sobrien /* Remove the in-charge parameter. */ 383690075Sobrien if (DECL_HAS_IN_CHARGE_PARM_P (clone)) 383790075Sobrien { 383890075Sobrien TREE_CHAIN (DECL_ARGUMENTS (clone)) 383990075Sobrien = TREE_CHAIN (TREE_CHAIN (DECL_ARGUMENTS (clone))); 384090075Sobrien DECL_HAS_IN_CHARGE_PARM_P (clone) = 0; 384190075Sobrien } 384290075Sobrien /* And the VTT parm, in a complete [cd]tor. */ 384390075Sobrien if (DECL_HAS_VTT_PARM_P (fn)) 384490075Sobrien { 384590075Sobrien if (DECL_NEEDS_VTT_PARM_P (clone)) 384690075Sobrien DECL_HAS_VTT_PARM_P (clone) = 1; 384790075Sobrien else 384818334Speter { 384990075Sobrien TREE_CHAIN (DECL_ARGUMENTS (clone)) 385090075Sobrien = TREE_CHAIN (TREE_CHAIN (DECL_ARGUMENTS (clone))); 385190075Sobrien DECL_HAS_VTT_PARM_P (clone) = 0; 385218334Speter } 385318334Speter } 385490075Sobrien 385590075Sobrien for (parms = DECL_ARGUMENTS (clone); parms; parms = TREE_CHAIN (parms)) 385690075Sobrien { 385790075Sobrien DECL_CONTEXT (parms) = clone; 3858117395Skan cxx_dup_lang_specific_decl (parms); 385990075Sobrien } 386018334Speter } 386118334Speter 386290075Sobrien /* Create the RTL for this function. */ 386390075Sobrien SET_DECL_RTL (clone, NULL_RTX); 3864169689Skan rest_of_decl_compilation (clone, /*top_level=*/1, at_eof); 3865169689Skan 386690075Sobrien /* Make it easy to find the CLONE given the FN. */ 386790075Sobrien TREE_CHAIN (clone) = TREE_CHAIN (fn); 386890075Sobrien TREE_CHAIN (fn) = clone; 386918334Speter 387090075Sobrien /* If this is a template, handle the DECL_TEMPLATE_RESULT as well. */ 387190075Sobrien if (TREE_CODE (clone) == TEMPLATE_DECL) 387250397Sobrien { 387390075Sobrien tree result; 387490075Sobrien 3875169689Skan DECL_TEMPLATE_RESULT (clone) 387690075Sobrien = build_clone (DECL_TEMPLATE_RESULT (clone), name); 387790075Sobrien result = DECL_TEMPLATE_RESULT (clone); 387890075Sobrien DECL_TEMPLATE_INFO (result) = copy_node (DECL_TEMPLATE_INFO (result)); 387990075Sobrien DECL_TI_TEMPLATE (result) = clone; 388090075Sobrien } 3881169689Skan else if (pch_file) 3882169689Skan note_decl_for_pch (clone); 388390075Sobrien 388490075Sobrien return clone; 388590075Sobrien} 388690075Sobrien 388790075Sobrien/* Produce declarations for all appropriate clones of FN. If 3888117395Skan UPDATE_METHOD_VEC_P is nonzero, the clones are added to the 388990075Sobrien CLASTYPE_METHOD_VEC as well. */ 389090075Sobrien 389190075Sobrienvoid 3892132718Skanclone_function_decl (tree fn, int update_method_vec_p) 389390075Sobrien{ 389490075Sobrien tree clone; 389590075Sobrien 389690075Sobrien /* Avoid inappropriate cloning. */ 389790075Sobrien if (TREE_CHAIN (fn) 389890075Sobrien && DECL_CLONED_FUNCTION (TREE_CHAIN (fn))) 389990075Sobrien return; 390090075Sobrien 390190075Sobrien if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn)) 390290075Sobrien { 390390075Sobrien /* For each constructor, we need two variants: an in-charge version 390490075Sobrien and a not-in-charge version. */ 390590075Sobrien clone = build_clone (fn, complete_ctor_identifier); 390690075Sobrien if (update_method_vec_p) 3907169689Skan add_method (DECL_CONTEXT (clone), clone, NULL_TREE); 390890075Sobrien clone = build_clone (fn, base_ctor_identifier); 390990075Sobrien if (update_method_vec_p) 3910169689Skan add_method (DECL_CONTEXT (clone), clone, NULL_TREE); 391190075Sobrien } 391290075Sobrien else 391390075Sobrien { 3914169689Skan gcc_assert (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn)); 391590075Sobrien 391690075Sobrien /* For each destructor, we need three variants: an in-charge 391790075Sobrien version, a not-in-charge version, and an in-charge deleting 391890075Sobrien version. We clone the deleting version first because that 391990075Sobrien means it will go second on the TYPE_METHODS list -- and that 392090075Sobrien corresponds to the correct layout order in the virtual 3921169689Skan function table. 392290075Sobrien 3923169689Skan For a non-virtual destructor, we do not build a deleting 392490075Sobrien destructor. */ 392590075Sobrien if (DECL_VIRTUAL_P (fn)) 392690075Sobrien { 392790075Sobrien clone = build_clone (fn, deleting_dtor_identifier); 392890075Sobrien if (update_method_vec_p) 3929169689Skan add_method (DECL_CONTEXT (clone), clone, NULL_TREE); 393090075Sobrien } 393190075Sobrien clone = build_clone (fn, complete_dtor_identifier); 393290075Sobrien if (update_method_vec_p) 3933169689Skan add_method (DECL_CONTEXT (clone), clone, NULL_TREE); 393490075Sobrien clone = build_clone (fn, base_dtor_identifier); 393590075Sobrien if (update_method_vec_p) 3936169689Skan add_method (DECL_CONTEXT (clone), clone, NULL_TREE); 393790075Sobrien } 393890075Sobrien 393990075Sobrien /* Note that this is an abstract function that is never emitted. */ 394090075Sobrien DECL_ABSTRACT (fn) = 1; 394190075Sobrien} 394290075Sobrien 394390075Sobrien/* DECL is an in charge constructor, which is being defined. This will 394490075Sobrien have had an in class declaration, from whence clones were 394590075Sobrien declared. An out-of-class definition can specify additional default 394690075Sobrien arguments. As it is the clones that are involved in overload 394790075Sobrien resolution, we must propagate the information from the DECL to its 3948117395Skan clones. */ 394990075Sobrien 395090075Sobrienvoid 3951132718Skanadjust_clone_args (tree decl) 395290075Sobrien{ 395390075Sobrien tree clone; 3954169689Skan 395590075Sobrien for (clone = TREE_CHAIN (decl); clone && DECL_CLONED_FUNCTION (clone); 395690075Sobrien clone = TREE_CHAIN (clone)) 395790075Sobrien { 395890075Sobrien tree orig_clone_parms = TYPE_ARG_TYPES (TREE_TYPE (clone)); 395990075Sobrien tree orig_decl_parms = TYPE_ARG_TYPES (TREE_TYPE (decl)); 396090075Sobrien tree decl_parms, clone_parms; 396190075Sobrien 396290075Sobrien clone_parms = orig_clone_parms; 3963169689Skan 3964117395Skan /* Skip the 'this' parameter. */ 396590075Sobrien orig_clone_parms = TREE_CHAIN (orig_clone_parms); 396690075Sobrien orig_decl_parms = TREE_CHAIN (orig_decl_parms); 396790075Sobrien 396890075Sobrien if (DECL_HAS_IN_CHARGE_PARM_P (decl)) 396990075Sobrien orig_decl_parms = TREE_CHAIN (orig_decl_parms); 397090075Sobrien if (DECL_HAS_VTT_PARM_P (decl)) 397190075Sobrien orig_decl_parms = TREE_CHAIN (orig_decl_parms); 3972169689Skan 397390075Sobrien clone_parms = orig_clone_parms; 397490075Sobrien if (DECL_HAS_VTT_PARM_P (clone)) 397590075Sobrien clone_parms = TREE_CHAIN (clone_parms); 3976169689Skan 397790075Sobrien for (decl_parms = orig_decl_parms; decl_parms; 397890075Sobrien decl_parms = TREE_CHAIN (decl_parms), 397990075Sobrien clone_parms = TREE_CHAIN (clone_parms)) 398050397Sobrien { 3981169689Skan gcc_assert (same_type_p (TREE_TYPE (decl_parms), 3982169689Skan TREE_TYPE (clone_parms))); 3983169689Skan 398490075Sobrien if (TREE_PURPOSE (decl_parms) && !TREE_PURPOSE (clone_parms)) 398590075Sobrien { 398690075Sobrien /* A default parameter has been added. Adjust the 3987117395Skan clone's parameters. */ 398890075Sobrien tree exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone)); 398990075Sobrien tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone)); 399090075Sobrien tree type; 399190075Sobrien 399290075Sobrien clone_parms = orig_decl_parms; 399390075Sobrien 399490075Sobrien if (DECL_HAS_VTT_PARM_P (clone)) 399590075Sobrien { 399690075Sobrien clone_parms = tree_cons (TREE_PURPOSE (orig_clone_parms), 399790075Sobrien TREE_VALUE (orig_clone_parms), 399890075Sobrien clone_parms); 399990075Sobrien TREE_TYPE (clone_parms) = TREE_TYPE (orig_clone_parms); 400090075Sobrien } 4001132718Skan type = build_method_type_directly (basetype, 4002132718Skan TREE_TYPE (TREE_TYPE (clone)), 4003132718Skan clone_parms); 400490075Sobrien if (exceptions) 400590075Sobrien type = build_exception_variant (type, exceptions); 400690075Sobrien TREE_TYPE (clone) = type; 4007169689Skan 400890075Sobrien clone_parms = NULL_TREE; 400990075Sobrien break; 401090075Sobrien } 401150397Sobrien } 4012169689Skan gcc_assert (!clone_parms); 401350397Sobrien } 401490075Sobrien} 401590075Sobrien 401690075Sobrien/* For each of the constructors and destructors in T, create an 401790075Sobrien in-charge and not-in-charge variant. */ 401890075Sobrien 401990075Sobrienstatic void 4020132718Skanclone_constructors_and_destructors (tree t) 402190075Sobrien{ 402290075Sobrien tree fns; 402390075Sobrien 402490075Sobrien /* If for some reason we don't have a CLASSTYPE_METHOD_VEC, we bail 402590075Sobrien out now. */ 402690075Sobrien if (!CLASSTYPE_METHOD_VEC (t)) 402790075Sobrien return; 402890075Sobrien 402990075Sobrien for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns)) 403090075Sobrien clone_function_decl (OVL_CURRENT (fns), /*update_method_vec_p=*/1); 403190075Sobrien for (fns = CLASSTYPE_DESTRUCTORS (t); fns; fns = OVL_NEXT (fns)) 403290075Sobrien clone_function_decl (OVL_CURRENT (fns), /*update_method_vec_p=*/1); 403390075Sobrien} 403490075Sobrien 403590075Sobrien/* Remove all zero-width bit-fields from T. */ 403690075Sobrien 403790075Sobrienstatic void 4038132718Skanremove_zero_width_bit_fields (tree t) 403990075Sobrien{ 404090075Sobrien tree *fieldsp; 404190075Sobrien 4042169689Skan fieldsp = &TYPE_FIELDS (t); 404390075Sobrien while (*fieldsp) 404490075Sobrien { 404590075Sobrien if (TREE_CODE (*fieldsp) == FIELD_DECL 4046169689Skan && DECL_C_BIT_FIELD (*fieldsp) 404790075Sobrien && DECL_INITIAL (*fieldsp)) 404890075Sobrien *fieldsp = TREE_CHAIN (*fieldsp); 404990075Sobrien else 405090075Sobrien fieldsp = &TREE_CHAIN (*fieldsp); 405190075Sobrien } 405290075Sobrien} 405390075Sobrien 405490075Sobrien/* Returns TRUE iff we need a cookie when dynamically allocating an 405590075Sobrien array whose elements have the indicated class TYPE. */ 405690075Sobrien 405790075Sobrienstatic bool 4058132718Skantype_requires_array_cookie (tree type) 405990075Sobrien{ 406090075Sobrien tree fns; 406190075Sobrien bool has_two_argument_delete_p = false; 406290075Sobrien 4063169689Skan gcc_assert (CLASS_TYPE_P (type)); 406490075Sobrien 406590075Sobrien /* If there's a non-trivial destructor, we need a cookie. In order 406690075Sobrien to iterate through the array calling the destructor for each 406790075Sobrien element, we'll have to know how many elements there are. */ 406890075Sobrien if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)) 406990075Sobrien return true; 407090075Sobrien 407190075Sobrien /* If the usual deallocation function is a two-argument whose second 407290075Sobrien argument is of type `size_t', then we have to pass the size of 407390075Sobrien the array to the deallocation function, so we will need to store 407490075Sobrien a cookie. */ 4075169689Skan fns = lookup_fnfields (TYPE_BINFO (type), 407690075Sobrien ansi_opname (VEC_DELETE_EXPR), 407790075Sobrien /*protect=*/0); 407890075Sobrien /* If there are no `operator []' members, or the lookup is 407990075Sobrien ambiguous, then we don't need a cookie. */ 408090075Sobrien if (!fns || fns == error_mark_node) 408190075Sobrien return false; 408290075Sobrien /* Loop through all of the functions. */ 4083117395Skan for (fns = BASELINK_FUNCTIONS (fns); fns; fns = OVL_NEXT (fns)) 408490075Sobrien { 408590075Sobrien tree fn; 408690075Sobrien tree second_parm; 408790075Sobrien 408890075Sobrien /* Select the current function. */ 408990075Sobrien fn = OVL_CURRENT (fns); 409090075Sobrien /* See if this function is a one-argument delete function. If 409190075Sobrien it is, then it will be the usual deallocation function. */ 409290075Sobrien second_parm = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fn))); 409390075Sobrien if (second_parm == void_list_node) 409490075Sobrien return false; 409590075Sobrien /* Otherwise, if we have a two-argument function and the second 409690075Sobrien argument is `size_t', it will be the usual deallocation 409790075Sobrien function -- unless there is one-argument function, too. */ 409890075Sobrien if (TREE_CHAIN (second_parm) == void_list_node 409990075Sobrien && same_type_p (TREE_VALUE (second_parm), sizetype)) 410090075Sobrien has_two_argument_delete_p = true; 410190075Sobrien } 410290075Sobrien 410390075Sobrien return has_two_argument_delete_p; 410490075Sobrien} 410590075Sobrien 410690075Sobrien/* Check the validity of the bases and members declared in T. Add any 410790075Sobrien implicitly-generated functions (like copy-constructors and 410890075Sobrien assignment operators). Compute various flag bits (like 410990075Sobrien CLASSTYPE_NON_POD_T) for T. This routine works purely at the C++ 411090075Sobrien level: i.e., independently of the ABI in use. */ 411190075Sobrien 411290075Sobrienstatic void 4113117395Skancheck_bases_and_members (tree t) 411490075Sobrien{ 411590075Sobrien /* Nonzero if the implicitly generated copy constructor should take 411690075Sobrien a non-const reference argument. */ 411790075Sobrien int cant_have_const_ctor; 4118169689Skan /* Nonzero if the implicitly generated assignment operator 411990075Sobrien should take a non-const reference argument. */ 412090075Sobrien int no_const_asn_ref; 412190075Sobrien tree access_decls; 412290075Sobrien 412390075Sobrien /* By default, we use const reference arguments and generate default 412490075Sobrien constructors. */ 412590075Sobrien cant_have_const_ctor = 0; 412690075Sobrien no_const_asn_ref = 0; 412790075Sobrien 4128117395Skan /* Check all the base-classes. */ 4129169689Skan check_bases (t, &cant_have_const_ctor, 413090075Sobrien &no_const_asn_ref); 413190075Sobrien 4132169689Skan /* Check all the method declarations. */ 4133169689Skan check_methods (t); 4134169689Skan 4135169689Skan /* Check all the data member declarations. We cannot call 4136169689Skan check_field_decls until we have called check_bases check_methods, 4137169689Skan as check_field_decls depends on TYPE_HAS_NONTRIVIAL_DESTRUCTOR 4138169689Skan being set appropriately. */ 4139117395Skan check_field_decls (t, &access_decls, 414090075Sobrien &cant_have_const_ctor, 414190075Sobrien &no_const_asn_ref); 414290075Sobrien 414390075Sobrien /* A nearly-empty class has to be vptr-containing; a nearly empty 414490075Sobrien class contains just a vptr. */ 414590075Sobrien if (!TYPE_CONTAINS_VPTR_P (t)) 414690075Sobrien CLASSTYPE_NEARLY_EMPTY_P (t) = 0; 414790075Sobrien 414852284Sobrien /* Do some bookkeeping that will guide the generation of implicitly 414952284Sobrien declared member functions. */ 415018334Speter TYPE_HAS_COMPLEX_INIT_REF (t) 4151169689Skan |= (TYPE_HAS_INIT_REF (t) || TYPE_CONTAINS_VPTR_P (t)); 415218334Speter TYPE_NEEDS_CONSTRUCTING (t) 4153169689Skan |= (TYPE_HAS_CONSTRUCTOR (t) || TYPE_CONTAINS_VPTR_P (t)); 4154169689Skan CLASSTYPE_NON_AGGREGATE (t) 4155169689Skan |= (TYPE_HAS_CONSTRUCTOR (t) || TYPE_POLYMORPHIC_P (t)); 415690075Sobrien CLASSTYPE_NON_POD_P (t) 4157169689Skan |= (CLASSTYPE_NON_AGGREGATE (t) 4158169689Skan || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) 415990075Sobrien || TYPE_HAS_ASSIGN_REF (t)); 416018334Speter TYPE_HAS_COMPLEX_ASSIGN_REF (t) 416190075Sobrien |= TYPE_HAS_ASSIGN_REF (t) || TYPE_CONTAINS_VPTR_P (t); 416218334Speter 4163169689Skan /* Synthesize any needed methods. */ 4164169689Skan add_implicitly_declared_members (t, 416590075Sobrien cant_have_const_ctor, 416690075Sobrien no_const_asn_ref); 416718334Speter 416890075Sobrien /* Create the in-charge and not-in-charge variants of constructors 416990075Sobrien and destructors. */ 417090075Sobrien clone_constructors_and_destructors (t); 417118334Speter 417290075Sobrien /* Process the using-declarations. */ 417390075Sobrien for (; access_decls; access_decls = TREE_CHAIN (access_decls)) 417490075Sobrien handle_using_decl (TREE_VALUE (access_decls), t); 417518334Speter 417690075Sobrien /* Build and sort the CLASSTYPE_METHOD_VEC. */ 417790075Sobrien finish_struct_methods (t); 417818334Speter 417990075Sobrien /* Figure out whether or not we will need a cookie when dynamically 418090075Sobrien allocating an array of this type. */ 4181117395Skan TYPE_LANG_SPECIFIC (t)->u.c.vec_new_uses_cookie 418290075Sobrien = type_requires_array_cookie (t); 418390075Sobrien} 418490075Sobrien 418590075Sobrien/* If T needs a pointer to its virtual function table, set TYPE_VFIELD 418690075Sobrien accordingly. If a new vfield was created (because T doesn't have a 418790075Sobrien primary base class), then the newly created field is returned. It 418890075Sobrien is not added to the TYPE_FIELDS list; it is the caller's 4189102780Skan responsibility to do that. Accumulate declared virtual functions 4190102780Skan on VIRTUALS_P. */ 419190075Sobrien 419290075Sobrienstatic tree 4193132718Skancreate_vtable_ptr (tree t, tree* virtuals_p) 419490075Sobrien{ 419590075Sobrien tree fn; 419690075Sobrien 4197102780Skan /* Collect the virtual functions declared in T. */ 419890075Sobrien for (fn = TYPE_METHODS (t); fn; fn = TREE_CHAIN (fn)) 4199102780Skan if (DECL_VINDEX (fn) && !DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn) 4200102780Skan && TREE_CODE (DECL_VINDEX (fn)) != INTEGER_CST) 4201102780Skan { 4202102780Skan tree new_virtual = make_node (TREE_LIST); 4203169689Skan 4204102780Skan BV_FN (new_virtual) = fn; 4205102780Skan BV_DELTA (new_virtual) = integer_zero_node; 4206169689Skan BV_VCALL_INDEX (new_virtual) = NULL_TREE; 420790075Sobrien 4208102780Skan TREE_CHAIN (new_virtual) = *virtuals_p; 4209102780Skan *virtuals_p = new_virtual; 4210102780Skan } 4211169689Skan 421290075Sobrien /* If we couldn't find an appropriate base class, create a new field 421390075Sobrien here. Even if there weren't any new virtual functions, we might need a 421490075Sobrien new virtual function table if we're supposed to include vptrs in 421590075Sobrien all classes that need them. */ 4216102780Skan if (!TYPE_VFIELD (t) && (*virtuals_p || TYPE_CONTAINS_VPTR_P (t))) 421718334Speter { 421852284Sobrien /* We build this decl with vtbl_ptr_type_node, which is a 421952284Sobrien `vtable_entry_type*'. It might seem more precise to use 4220169689Skan `vtable_entry_type (*)[N]' where N is the number of virtual 422152284Sobrien functions. However, that would require the vtable pointer in 422252284Sobrien base classes to have a different type than the vtable pointer 422352284Sobrien in derived classes. We could make that happen, but that 422452284Sobrien still wouldn't solve all the problems. In particular, the 422552284Sobrien type-based alias analysis code would decide that assignments 422652284Sobrien to the base class vtable pointer can't alias assignments to 422752284Sobrien the derived class vtable pointer, since they have different 4228132718Skan types. Thus, in a derived class destructor, where the base 422952284Sobrien class constructor was inlined, we could generate bad code for 4230169689Skan setting up the vtable pointer. 423152284Sobrien 4232169689Skan Therefore, we use one type for all vtable pointers. We still 423352284Sobrien use a type-correct type; it's just doesn't indicate the array 423452284Sobrien bounds. That's better than using `void*' or some such; it's 423552284Sobrien cleaner, and it let's the alias analysis code know that these 423652284Sobrien stores cannot alias stores to void*! */ 423790075Sobrien tree field; 423890075Sobrien 423990075Sobrien field = build_decl (FIELD_DECL, get_vfield_name (t), vtbl_ptr_type_node); 424090075Sobrien DECL_VIRTUAL_P (field) = 1; 424190075Sobrien DECL_ARTIFICIAL (field) = 1; 424290075Sobrien DECL_FIELD_CONTEXT (field) = t; 424390075Sobrien DECL_FCONTEXT (field) = t; 4244169689Skan 424590075Sobrien TYPE_VFIELD (t) = field; 4246169689Skan 424790075Sobrien /* This class is non-empty. */ 4248117395Skan CLASSTYPE_EMPTY_P (t) = 0; 424990075Sobrien 425090075Sobrien return field; 425190075Sobrien } 425290075Sobrien 425390075Sobrien return NULL_TREE; 425490075Sobrien} 425590075Sobrien 425690075Sobrien/* Fixup the inline function given by INFO now that the class is 425790075Sobrien complete. */ 425890075Sobrien 425990075Sobrienstatic void 4260132718Skanfixup_pending_inline (tree fn) 426190075Sobrien{ 426290075Sobrien if (DECL_PENDING_INLINE_INFO (fn)) 426390075Sobrien { 426490075Sobrien tree args = DECL_ARGUMENTS (fn); 426590075Sobrien while (args) 426618334Speter { 426790075Sobrien DECL_CONTEXT (args) = fn; 426890075Sobrien args = TREE_CHAIN (args); 426918334Speter } 427090075Sobrien } 427190075Sobrien} 427290075Sobrien 427390075Sobrien/* Fixup the inline methods and friends in TYPE now that TYPE is 427490075Sobrien complete. */ 427590075Sobrien 427690075Sobrienstatic void 4277132718Skanfixup_inline_methods (tree type) 427890075Sobrien{ 427990075Sobrien tree method = TYPE_METHODS (type); 4280169689Skan VEC(tree,gc) *friends; 4281169689Skan unsigned ix; 428290075Sobrien 428390075Sobrien if (method && TREE_CODE (method) == TREE_VEC) 428490075Sobrien { 428590075Sobrien if (TREE_VEC_ELT (method, 1)) 428690075Sobrien method = TREE_VEC_ELT (method, 1); 428790075Sobrien else if (TREE_VEC_ELT (method, 0)) 428890075Sobrien method = TREE_VEC_ELT (method, 0); 428918334Speter else 429090075Sobrien method = TREE_VEC_ELT (method, 2); 429118334Speter } 429218334Speter 429390075Sobrien /* Do inline member functions. */ 429490075Sobrien for (; method; method = TREE_CHAIN (method)) 429590075Sobrien fixup_pending_inline (method); 429618334Speter 429790075Sobrien /* Do friends. */ 4298169689Skan for (friends = CLASSTYPE_INLINE_FRIENDS (type), ix = 0; 4299169689Skan VEC_iterate (tree, friends, ix, method); ix++) 4300169689Skan fixup_pending_inline (method); 4301169689Skan CLASSTYPE_INLINE_FRIENDS (type) = NULL; 430290075Sobrien} 430318334Speter 430490075Sobrien/* Add OFFSET to all base types of BINFO which is a base in the 430590075Sobrien hierarchy dominated by T. 430618334Speter 430790075Sobrien OFFSET, which is a type offset, is number of bytes. */ 430818334Speter 430990075Sobrienstatic void 4310132718Skanpropagate_binfo_offsets (tree binfo, tree offset) 431190075Sobrien{ 431290075Sobrien int i; 431390075Sobrien tree primary_binfo; 4314169689Skan tree base_binfo; 431518334Speter 431690075Sobrien /* Update BINFO's offset. */ 431790075Sobrien BINFO_OFFSET (binfo) 4318169689Skan = convert (sizetype, 431990075Sobrien size_binop (PLUS_EXPR, 432090075Sobrien convert (ssizetype, BINFO_OFFSET (binfo)), 432190075Sobrien offset)); 432290075Sobrien 432390075Sobrien /* Find the primary base class. */ 432490075Sobrien primary_binfo = get_primary_binfo (binfo); 432590075Sobrien 4326169689Skan if (primary_binfo && BINFO_INHERITANCE_CHAIN (primary_binfo) == binfo) 4327169689Skan propagate_binfo_offsets (primary_binfo, offset); 4328169689Skan 432990075Sobrien /* Scan all of the bases, pushing the BINFO_OFFSET adjust 433090075Sobrien downwards. */ 4331169689Skan for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i) 433218334Speter { 4333169689Skan /* Don't do the primary base twice. */ 4334169689Skan if (base_binfo == primary_binfo) 4335169689Skan continue; 433618334Speter 4337169689Skan if (BINFO_VIRTUAL_P (base_binfo)) 433890075Sobrien continue; 433990075Sobrien 4340132718Skan propagate_binfo_offsets (base_binfo, offset); 434118334Speter } 434290075Sobrien} 434352284Sobrien 4344117395Skan/* Set BINFO_OFFSET for all of the virtual bases for RLI->T. Update 434590075Sobrien TYPE_ALIGN and TYPE_SIZE for T. OFFSETS gives the location of 434690075Sobrien empty subobjects of T. */ 434718334Speter 434890075Sobrienstatic void 4349117395Skanlayout_virtual_bases (record_layout_info rli, splay_tree offsets) 435090075Sobrien{ 4351132718Skan tree vbase; 4352117395Skan tree t = rli->t; 4353102780Skan bool first_vbase = true; 4354117395Skan tree *next_field; 435518334Speter 4356169689Skan if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) == 0) 435790075Sobrien return; 435818334Speter 4359117395Skan if (!abi_version_at_least(2)) 4360117395Skan { 4361117395Skan /* In G++ 3.2, we incorrectly rounded the size before laying out 4362117395Skan the virtual bases. */ 4363117395Skan finish_record_layout (rli, /*free_p=*/false); 436490075Sobrien#ifdef STRUCTURE_SIZE_BOUNDARY 4365117395Skan /* Packed structures don't need to have minimum size. */ 4366117395Skan if (! TYPE_PACKED (t)) 4367132718Skan TYPE_ALIGN (t) = MAX (TYPE_ALIGN (t), (unsigned) STRUCTURE_SIZE_BOUNDARY); 436890075Sobrien#endif 4369117395Skan rli->offset = TYPE_SIZE_UNIT (t); 4370117395Skan rli->bitpos = bitsize_zero_node; 4371117395Skan rli->record_align = TYPE_ALIGN (t); 4372117395Skan } 437318334Speter 4374117395Skan /* Find the last field. The artificial fields created for virtual 4375117395Skan bases will go after the last extant field to date. */ 4376117395Skan next_field = &TYPE_FIELDS (t); 4377117395Skan while (*next_field) 4378117395Skan next_field = &TREE_CHAIN (*next_field); 437918334Speter 438090075Sobrien /* Go through the virtual bases, allocating space for each virtual 438190075Sobrien base that is not already a primary base class. These are 438290075Sobrien allocated in inheritance graph order. */ 4383132718Skan for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase)) 438490075Sobrien { 4385169689Skan if (!BINFO_VIRTUAL_P (vbase)) 438690075Sobrien continue; 4387102780Skan 438890075Sobrien if (!BINFO_PRIMARY_P (vbase)) 438990075Sobrien { 4390117395Skan tree basetype = TREE_TYPE (vbase); 4391117395Skan 439290075Sobrien /* This virtual base is not a primary base of any class in the 439390075Sobrien hierarchy, so we have to add space for it. */ 4394117395Skan next_field = build_base_field (rli, vbase, 4395117395Skan offsets, next_field); 439652284Sobrien 4397102780Skan /* If the first virtual base might have been placed at a 4398102780Skan lower address, had we started from CLASSTYPE_SIZE, rather 4399102780Skan than TYPE_SIZE, issue a warning. There can be both false 4400102780Skan positives and false negatives from this warning in rare 4401102780Skan cases; to deal with all the possibilities would probably 4402102780Skan require performing both layout algorithms and comparing 4403102780Skan the results which is not particularly tractable. */ 4404102780Skan if (warn_abi 4405102780Skan && first_vbase 4406169689Skan && (tree_int_cst_lt 4407117395Skan (size_binop (CEIL_DIV_EXPR, 4408117395Skan round_up (CLASSTYPE_SIZE (t), 4409117395Skan CLASSTYPE_ALIGN (basetype)), 4410117395Skan bitsize_unit_node), 4411117395Skan BINFO_OFFSET (vbase)))) 4412169689Skan warning (OPT_Wabi, 4413169689Skan "offset of virtual base %qT is not ABI-compliant and " 4414169689Skan "may change in a future version of GCC", 4415102780Skan basetype); 4416102780Skan 4417102780Skan first_vbase = false; 441818334Speter } 441918334Speter } 4420117395Skan} 442190075Sobrien 4422117395Skan/* Returns the offset of the byte just past the end of the base class 4423117395Skan BINFO. */ 442490075Sobrien 4425117395Skanstatic tree 4426117395Skanend_of_base (tree binfo) 4427117395Skan{ 4428117395Skan tree size; 442990075Sobrien 4430117395Skan if (is_empty_class (BINFO_TYPE (binfo))) 4431117395Skan /* An empty class has zero CLASSTYPE_SIZE_UNIT, but we need to 4432117395Skan allocate some space for it. It cannot have virtual bases, so 4433117395Skan TYPE_SIZE_UNIT is fine. */ 4434117395Skan size = TYPE_SIZE_UNIT (BINFO_TYPE (binfo)); 4435117395Skan else 4436117395Skan size = CLASSTYPE_SIZE_UNIT (BINFO_TYPE (binfo)); 4437117395Skan 4438117395Skan return size_binop (PLUS_EXPR, BINFO_OFFSET (binfo), size); 443990075Sobrien} 444090075Sobrien 444190075Sobrien/* Returns the offset of the byte just past the end of the base class 444290075Sobrien with the highest offset in T. If INCLUDE_VIRTUALS_P is zero, then 444390075Sobrien only non-virtual bases are included. */ 444490075Sobrien 4445117395Skanstatic tree 4446132718Skanend_of_class (tree t, int include_virtuals_p) 444790075Sobrien{ 4448117395Skan tree result = size_zero_node; 4449169689Skan VEC(tree,gc) *vbases; 4450117395Skan tree binfo; 4451169689Skan tree base_binfo; 4452117395Skan tree offset; 445390075Sobrien int i; 445490075Sobrien 4455169689Skan for (binfo = TYPE_BINFO (t), i = 0; 4456169689Skan BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i) 445718334Speter { 445890075Sobrien if (!include_virtuals_p 4459169689Skan && BINFO_VIRTUAL_P (base_binfo) 4460169689Skan && (!BINFO_PRIMARY_P (base_binfo) 4461169689Skan || BINFO_INHERITANCE_CHAIN (base_binfo) != TYPE_BINFO (t))) 446290075Sobrien continue; 446390075Sobrien 4464169689Skan offset = end_of_base (base_binfo); 4465117395Skan if (INT_CST_LT_UNSIGNED (result, offset)) 4466117395Skan result = offset; 446718334Speter } 446818334Speter 4469117395Skan /* G++ 3.2 did not check indirect virtual bases. */ 4470117395Skan if (abi_version_at_least (2) && include_virtuals_p) 4471169689Skan for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0; 4472169689Skan VEC_iterate (tree, vbases, i, base_binfo); i++) 4473117395Skan { 4474169689Skan offset = end_of_base (base_binfo); 4475117395Skan if (INT_CST_LT_UNSIGNED (result, offset)) 4476117395Skan result = offset; 4477117395Skan } 4478117395Skan 447990075Sobrien return result; 448090075Sobrien} 448118334Speter 4482117395Skan/* Warn about bases of T that are inaccessible because they are 448390075Sobrien ambiguous. For example: 448418334Speter 448590075Sobrien struct S {}; 448690075Sobrien struct T : public S {}; 448790075Sobrien struct U : public S, public T {}; 448890075Sobrien 448990075Sobrien Here, `(S*) new U' is not allowed because there are two `S' 449090075Sobrien subobjects of U. */ 449190075Sobrien 449290075Sobrienstatic void 4493132718Skanwarn_about_ambiguous_bases (tree t) 449490075Sobrien{ 449590075Sobrien int i; 4496169689Skan VEC(tree,gc) *vbases; 4497117395Skan tree basetype; 4498169689Skan tree binfo; 4499169689Skan tree base_binfo; 450090075Sobrien 4501169689Skan /* If there are no repeated bases, nothing can be ambiguous. */ 4502169689Skan if (!CLASSTYPE_REPEATED_BASE_P (t)) 4503169689Skan return; 4504169689Skan 4505117395Skan /* Check direct bases. */ 4506169689Skan for (binfo = TYPE_BINFO (t), i = 0; 4507169689Skan BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i) 450818334Speter { 4509169689Skan basetype = BINFO_TYPE (base_binfo); 451090075Sobrien 4511169689Skan if (!lookup_base (t, basetype, ba_unique | ba_quiet, NULL)) 4512169689Skan warning (0, "direct base %qT inaccessible in %qT due to ambiguity", 4513117395Skan basetype, t); 451418334Speter } 4515117395Skan 4516117395Skan /* Check for ambiguous virtual bases. */ 4517117395Skan if (extra_warnings) 4518169689Skan for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0; 4519169689Skan VEC_iterate (tree, vbases, i, binfo); i++) 4520117395Skan { 4521169689Skan basetype = BINFO_TYPE (binfo); 4522169689Skan 4523169689Skan if (!lookup_base (t, basetype, ba_unique | ba_quiet, NULL)) 4524169689Skan warning (OPT_Wextra, "virtual base %qT inaccessible in %qT due to ambiguity", 4525117395Skan basetype, t); 4526117395Skan } 452790075Sobrien} 452890075Sobrien 452990075Sobrien/* Compare two INTEGER_CSTs K1 and K2. */ 453090075Sobrien 453190075Sobrienstatic int 4532132718Skansplay_tree_compare_integer_csts (splay_tree_key k1, splay_tree_key k2) 453390075Sobrien{ 453490075Sobrien return tree_int_cst_compare ((tree) k1, (tree) k2); 453590075Sobrien} 453690075Sobrien 4537117395Skan/* Increase the size indicated in RLI to account for empty classes 4538117395Skan that are "off the end" of the class. */ 4539117395Skan 4540117395Skanstatic void 4541117395Skaninclude_empty_classes (record_layout_info rli) 4542117395Skan{ 4543117395Skan tree eoc; 4544117395Skan tree rli_size; 4545117395Skan 4546117395Skan /* It might be the case that we grew the class to allocate a 4547117395Skan zero-sized base class. That won't be reflected in RLI, yet, 4548117395Skan because we are willing to overlay multiple bases at the same 4549117395Skan offset. However, now we need to make sure that RLI is big enough 4550117395Skan to reflect the entire class. */ 4551169689Skan eoc = end_of_class (rli->t, 4552117395Skan CLASSTYPE_AS_BASE (rli->t) != NULL_TREE); 4553117395Skan rli_size = rli_size_unit_so_far (rli); 4554117395Skan if (TREE_CODE (rli_size) == INTEGER_CST 4555117395Skan && INT_CST_LT_UNSIGNED (rli_size, eoc)) 4556117395Skan { 4557132718Skan if (!abi_version_at_least (2)) 4558132718Skan /* In version 1 of the ABI, the size of a class that ends with 4559132718Skan a bitfield was not rounded up to a whole multiple of a 4560132718Skan byte. Because rli_size_unit_so_far returns only the number 4561132718Skan of fully allocated bytes, any extra bits were not included 4562132718Skan in the size. */ 4563132718Skan rli->bitpos = round_down (rli->bitpos, BITS_PER_UNIT); 4564132718Skan else 4565132718Skan /* The size should have been rounded to a whole byte. */ 4566169689Skan gcc_assert (tree_int_cst_equal 4567169689Skan (rli->bitpos, round_down (rli->bitpos, BITS_PER_UNIT))); 4568169689Skan rli->bitpos 4569169689Skan = size_binop (PLUS_EXPR, 4570117395Skan rli->bitpos, 4571117395Skan size_binop (MULT_EXPR, 4572117395Skan convert (bitsizetype, 4573117395Skan size_binop (MINUS_EXPR, 4574117395Skan eoc, rli_size)), 4575117395Skan bitsize_int (BITS_PER_UNIT))); 4576117395Skan normalize_rli (rli); 4577117395Skan } 4578117395Skan} 4579117395Skan 458090075Sobrien/* Calculate the TYPE_SIZE, TYPE_ALIGN, etc for T. Calculate 458190075Sobrien BINFO_OFFSETs for all of the base-classes. Position the vtable 4582117395Skan pointer. Accumulate declared virtual functions on VIRTUALS_P. */ 458390075Sobrien 458490075Sobrienstatic void 4585117395Skanlayout_class_type (tree t, tree *virtuals_p) 458690075Sobrien{ 458790075Sobrien tree non_static_data_members; 458890075Sobrien tree field; 458990075Sobrien tree vptr; 459090075Sobrien record_layout_info rli; 459190075Sobrien /* Maps offsets (represented as INTEGER_CSTs) to a TREE_LIST of 459290075Sobrien types that appear at that offset. */ 459390075Sobrien splay_tree empty_base_offsets; 4594102780Skan /* True if the last field layed out was a bit-field. */ 4595102780Skan bool last_field_was_bitfield = false; 4596117395Skan /* The location at which the next field should be inserted. */ 4597117395Skan tree *next_field; 4598117395Skan /* T, as a base class. */ 4599117395Skan tree base_t; 460090075Sobrien 460190075Sobrien /* Keep track of the first non-static data member. */ 460290075Sobrien non_static_data_members = TYPE_FIELDS (t); 460390075Sobrien 460490075Sobrien /* Start laying out the record. */ 460590075Sobrien rli = start_record_layout (t); 460690075Sobrien 4607169689Skan /* Mark all the primary bases in the hierarchy. */ 4608169689Skan determine_primary_bases (t); 460990075Sobrien 461090075Sobrien /* Create a pointer to our virtual function table. */ 4611117395Skan vptr = create_vtable_ptr (t, virtuals_p); 461290075Sobrien 461390075Sobrien /* The vptr is always the first thing in the class. */ 461490075Sobrien if (vptr) 461590075Sobrien { 4616117395Skan TREE_CHAIN (vptr) = TYPE_FIELDS (t); 4617117395Skan TYPE_FIELDS (t) = vptr; 4618117395Skan next_field = &TREE_CHAIN (vptr); 461990075Sobrien place_field (rli, vptr); 462090075Sobrien } 4621117395Skan else 4622117395Skan next_field = &TYPE_FIELDS (t); 462390075Sobrien 462490075Sobrien /* Build FIELD_DECLs for all of the non-virtual base-types. */ 4625169689Skan empty_base_offsets = splay_tree_new (splay_tree_compare_integer_csts, 462690075Sobrien NULL, NULL); 4627117395Skan build_base_fields (rli, empty_base_offsets, next_field); 4628169689Skan 462990075Sobrien /* Layout the non-static data members. */ 463090075Sobrien for (field = non_static_data_members; field; field = TREE_CHAIN (field)) 463150397Sobrien { 463290075Sobrien tree type; 463390075Sobrien tree padding; 463490075Sobrien 463590075Sobrien /* We still pass things that aren't non-static data members to 463690075Sobrien the back-end, in case it wants to do something with them. */ 463790075Sobrien if (TREE_CODE (field) != FIELD_DECL) 463850397Sobrien { 463990075Sobrien place_field (rli, field); 464096263Sobrien /* If the static data member has incomplete type, keep track 4641169689Skan of it so that it can be completed later. (The handling 464296263Sobrien of pending statics in finish_record_layout is 464396263Sobrien insufficient; consider: 464496263Sobrien 464596263Sobrien struct S1; 464696263Sobrien struct S2 { static S1 s1; }; 4647169689Skan 4648169689Skan At this point, finish_record_layout will be called, but 464996263Sobrien S1 is still incomplete.) */ 465096263Sobrien if (TREE_CODE (field) == VAR_DECL) 4651169689Skan { 4652169689Skan maybe_register_incomplete_var (field); 4653169689Skan /* The visibility of static data members is determined 4654169689Skan at their point of declaration, not their point of 4655169689Skan definition. */ 4656169689Skan determine_visibility (field); 4657169689Skan } 465890075Sobrien continue; 465950397Sobrien } 466018334Speter 466190075Sobrien type = TREE_TYPE (field); 4662169689Skan if (type == error_mark_node) 4663169689Skan continue; 4664169689Skan 4665132718Skan padding = NULL_TREE; 466690075Sobrien 466790075Sobrien /* If this field is a bit-field whose width is greater than its 466890075Sobrien type, then there are some special rules for allocating 466990075Sobrien it. */ 467090075Sobrien if (DECL_C_BIT_FIELD (field) 467190075Sobrien && INT_CST_LT (TYPE_SIZE (type), DECL_SIZE (field))) 467218334Speter { 467390075Sobrien integer_type_kind itk; 467490075Sobrien tree integer_type; 4675132718Skan bool was_unnamed_p = false; 467690075Sobrien /* We must allocate the bits as if suitably aligned for the 467790075Sobrien longest integer type that fits in this many bits. type 467890075Sobrien of the field. Then, we are supposed to use the left over 467990075Sobrien bits as additional padding. */ 468090075Sobrien for (itk = itk_char; itk != itk_none; ++itk) 4681169689Skan if (INT_CST_LT (DECL_SIZE (field), 468290075Sobrien TYPE_SIZE (integer_types[itk]))) 468390075Sobrien break; 468450397Sobrien 468590075Sobrien /* ITK now indicates a type that is too large for the 468690075Sobrien field. We have to back up by one to find the largest 468790075Sobrien type that fits. */ 468890075Sobrien integer_type = integer_types[itk - 1]; 4689117395Skan 4690132718Skan /* Figure out how much additional padding is required. GCC 4691132718Skan 3.2 always created a padding field, even if it had zero 4692132718Skan width. */ 4693132718Skan if (!abi_version_at_least (2) 4694132718Skan || INT_CST_LT (TYPE_SIZE (integer_type), DECL_SIZE (field))) 4695117395Skan { 4696132718Skan if (abi_version_at_least (2) && TREE_CODE (t) == UNION_TYPE) 4697132718Skan /* In a union, the padding field must have the full width 4698132718Skan of the bit-field; all fields start at offset zero. */ 4699132718Skan padding = DECL_SIZE (field); 4700132718Skan else 4701132718Skan { 4702169689Skan if (TREE_CODE (t) == UNION_TYPE) 4703169689Skan warning (OPT_Wabi, "size assigned to %qT may not be " 4704132718Skan "ABI-compliant and may change in a future " 4705169689Skan "version of GCC", 4706132718Skan t); 4707132718Skan padding = size_binop (MINUS_EXPR, DECL_SIZE (field), 4708132718Skan TYPE_SIZE (integer_type)); 4709132718Skan } 4710117395Skan } 4711132718Skan#ifdef PCC_BITFIELD_TYPE_MATTERS 4712132718Skan /* An unnamed bitfield does not normally affect the 4713132718Skan alignment of the containing class on a target where 4714132718Skan PCC_BITFIELD_TYPE_MATTERS. But, the C++ ABI does not 4715132718Skan make any exceptions for unnamed bitfields when the 4716132718Skan bitfields are longer than their types. Therefore, we 4717132718Skan temporarily give the field a name. */ 4718132718Skan if (PCC_BITFIELD_TYPE_MATTERS && !DECL_NAME (field)) 4719132718Skan { 4720132718Skan was_unnamed_p = true; 4721132718Skan DECL_NAME (field) = make_anon_name (); 4722132718Skan } 4723132718Skan#endif 472490075Sobrien DECL_SIZE (field) = TYPE_SIZE (integer_type); 472590075Sobrien DECL_ALIGN (field) = TYPE_ALIGN (integer_type); 472690075Sobrien DECL_USER_ALIGN (field) = TYPE_USER_ALIGN (integer_type); 4727117395Skan layout_nonempty_base_or_field (rli, field, NULL_TREE, 4728117395Skan empty_base_offsets); 4729132718Skan if (was_unnamed_p) 4730132718Skan DECL_NAME (field) = NULL_TREE; 4731117395Skan /* Now that layout has been performed, set the size of the 4732117395Skan field to the size of its declared type; the rest of the 4733117395Skan field is effectively invisible. */ 4734117395Skan DECL_SIZE (field) = TYPE_SIZE (type); 4735122180Skan /* We must also reset the DECL_MODE of the field. */ 4736122180Skan if (abi_version_at_least (2)) 4737122180Skan DECL_MODE (field) = TYPE_MODE (type); 4738122180Skan else if (warn_abi 4739122180Skan && DECL_MODE (field) != TYPE_MODE (type)) 4740122180Skan /* Versions of G++ before G++ 3.4 did not reset the 4741122180Skan DECL_MODE. */ 4742169689Skan warning (OPT_Wabi, 4743169689Skan "the offset of %qD may not be ABI-compliant and may " 4744122180Skan "change in a future version of GCC", field); 474518334Speter } 474618334Speter else 4747132718Skan layout_nonempty_base_or_field (rli, field, NULL_TREE, 4748132718Skan empty_base_offsets); 474990075Sobrien 4750117395Skan /* Remember the location of any empty classes in FIELD. */ 4751117395Skan if (abi_version_at_least (2)) 4752169689Skan record_subobject_offsets (TREE_TYPE (field), 4753117395Skan byte_position(field), 4754117395Skan empty_base_offsets, 4755169689Skan /*is_data_member=*/true); 475690075Sobrien 4757102780Skan /* If a bit-field does not immediately follow another bit-field, 4758102780Skan and yet it starts in the middle of a byte, we have failed to 4759102780Skan comply with the ABI. */ 4760102780Skan if (warn_abi 4761169689Skan && DECL_C_BIT_FIELD (field) 4762169689Skan /* The TREE_NO_WARNING flag gets set by Objective-C when 4763169689Skan laying out an Objective-C class. The ObjC ABI differs 4764169689Skan from the C++ ABI, and so we do not want a warning 4765169689Skan here. */ 4766169689Skan && !TREE_NO_WARNING (field) 4767102780Skan && !last_field_was_bitfield 4768102780Skan && !integer_zerop (size_binop (TRUNC_MOD_EXPR, 4769102780Skan DECL_FIELD_BIT_OFFSET (field), 4770102780Skan bitsize_unit_node))) 4771169689Skan warning (OPT_Wabi, "offset of %q+D is not ABI-compliant and may " 4772169689Skan "change in a future version of GCC", field); 4773102780Skan 4774117395Skan /* G++ used to use DECL_FIELD_OFFSET as if it were the byte 4775117395Skan offset of the field. */ 4776169689Skan if (warn_abi 4777117395Skan && !tree_int_cst_equal (DECL_FIELD_OFFSET (field), 4778117395Skan byte_position (field)) 4779117395Skan && contains_empty_class_p (TREE_TYPE (field))) 4780169689Skan warning (OPT_Wabi, "%q+D contains empty classes which may cause base " 4781169689Skan "classes to be placed at different locations in a " 4782169689Skan "future version of GCC", field); 4783117395Skan 4784169689Skan /* The middle end uses the type of expressions to determine the 4785169689Skan possible range of expression values. In order to optimize 4786169689Skan "x.i > 7" to "false" for a 2-bit bitfield "i", the middle end 4787169689Skan must be made aware of the width of "i", via its type. 4788169689Skan 4789169689Skan Because C++ does not have integer types of arbitrary width, 4790169689Skan we must (for the purposes of the front end) convert from the 4791169689Skan type assigned here to the declared type of the bitfield 4792169689Skan whenever a bitfield expression is used as an rvalue. 4793169689Skan Similarly, when assigning a value to a bitfield, the value 4794169689Skan must be converted to the type given the bitfield here. */ 4795169689Skan if (DECL_C_BIT_FIELD (field)) 4796169689Skan { 4797169689Skan tree ftype; 4798169689Skan unsigned HOST_WIDE_INT width; 4799169689Skan ftype = TREE_TYPE (field); 4800169689Skan width = tree_low_cst (DECL_SIZE (field), /*unsignedp=*/1); 4801169689Skan if (width != TYPE_PRECISION (ftype)) 4802169689Skan TREE_TYPE (field) 4803169689Skan = c_build_bitfield_integer_type (width, 4804169689Skan TYPE_UNSIGNED (ftype)); 4805169689Skan } 4806169689Skan 480790075Sobrien /* If we needed additional padding after this field, add it 480890075Sobrien now. */ 480990075Sobrien if (padding) 481018334Speter { 481190075Sobrien tree padding_field; 481218334Speter 4813169689Skan padding_field = build_decl (FIELD_DECL, 481490075Sobrien NULL_TREE, 4815169689Skan char_type_node); 481690075Sobrien DECL_BIT_FIELD (padding_field) = 1; 481790075Sobrien DECL_SIZE (padding_field) = padding; 4818132718Skan DECL_CONTEXT (padding_field) = t; 4819132718Skan DECL_ARTIFICIAL (padding_field) = 1; 4820169689Skan DECL_IGNORED_P (padding_field) = 1; 482190075Sobrien layout_nonempty_base_or_field (rli, padding_field, 4822169689Skan NULL_TREE, 4823117395Skan empty_base_offsets); 482418334Speter } 4825102780Skan 4826102780Skan last_field_was_bitfield = DECL_C_BIT_FIELD (field); 482790075Sobrien } 482818334Speter 4829117395Skan if (abi_version_at_least (2) && !integer_zerop (rli->bitpos)) 483090075Sobrien { 4831117395Skan /* Make sure that we are on a byte boundary so that the size of 4832117395Skan the class without virtual bases will always be a round number 4833117395Skan of bytes. */ 4834117395Skan rli->bitpos = round_up (rli->bitpos, BITS_PER_UNIT); 4835117395Skan normalize_rli (rli); 483690075Sobrien } 483790075Sobrien 4838117395Skan /* G++ 3.2 does not allow virtual bases to be overlaid with tail 4839117395Skan padding. */ 4840117395Skan if (!abi_version_at_least (2)) 4841117395Skan include_empty_classes(rli); 484290075Sobrien 484390075Sobrien /* Delete all zero-width bit-fields from the list of fields. Now 484490075Sobrien that the type is laid out they are no longer important. */ 484590075Sobrien remove_zero_width_bit_fields (t); 484690075Sobrien 4847117395Skan /* Create the version of T used for virtual bases. We do not use 4848117395Skan make_aggr_type for this version; this is an artificial type. For 4849117395Skan a POD type, we just reuse T. */ 4850117395Skan if (CLASSTYPE_NON_POD_P (t) || CLASSTYPE_EMPTY_P (t)) 485190075Sobrien { 4852117395Skan base_t = make_node (TREE_CODE (t)); 4853169689Skan 4854117395Skan /* Set the size and alignment for the new type. In G++ 3.2, all 4855117395Skan empty classes were considered to have size zero when used as 4856117395Skan base classes. */ 4857117395Skan if (!abi_version_at_least (2) && CLASSTYPE_EMPTY_P (t)) 4858117395Skan { 4859117395Skan TYPE_SIZE (base_t) = bitsize_zero_node; 4860117395Skan TYPE_SIZE_UNIT (base_t) = size_zero_node; 4861117395Skan if (warn_abi && !integer_zerop (rli_size_unit_so_far (rli))) 4862169689Skan warning (OPT_Wabi, 4863169689Skan "layout of classes derived from empty class %qT " 4864117395Skan "may change in a future version of GCC", 4865117395Skan t); 4866117395Skan } 4867117395Skan else 4868117395Skan { 4869117395Skan tree eoc; 4870117395Skan 4871117395Skan /* If the ABI version is not at least two, and the last 4872117395Skan field was a bit-field, RLI may not be on a byte 4873117395Skan boundary. In particular, rli_size_unit_so_far might 4874117395Skan indicate the last complete byte, while rli_size_so_far 4875117395Skan indicates the total number of bits used. Therefore, 4876117395Skan rli_size_so_far, rather than rli_size_unit_so_far, is 4877117395Skan used to compute TYPE_SIZE_UNIT. */ 4878117395Skan eoc = end_of_class (t, /*include_virtuals_p=*/0); 4879169689Skan TYPE_SIZE_UNIT (base_t) 4880117395Skan = size_binop (MAX_EXPR, 4881117395Skan convert (sizetype, 4882117395Skan size_binop (CEIL_DIV_EXPR, 4883117395Skan rli_size_so_far (rli), 4884117395Skan bitsize_int (BITS_PER_UNIT))), 4885117395Skan eoc); 4886169689Skan TYPE_SIZE (base_t) 4887117395Skan = size_binop (MAX_EXPR, 4888117395Skan rli_size_so_far (rli), 4889117395Skan size_binop (MULT_EXPR, 4890117395Skan convert (bitsizetype, eoc), 4891117395Skan bitsize_int (BITS_PER_UNIT))); 4892117395Skan } 4893117395Skan TYPE_ALIGN (base_t) = rli->record_align; 4894117395Skan TYPE_USER_ALIGN (base_t) = TYPE_USER_ALIGN (t); 4895117395Skan 4896117395Skan /* Copy the fields from T. */ 4897117395Skan next_field = &TYPE_FIELDS (base_t); 4898117395Skan for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field)) 4899117395Skan if (TREE_CODE (field) == FIELD_DECL) 4900117395Skan { 4901117395Skan *next_field = build_decl (FIELD_DECL, 4902169689Skan DECL_NAME (field), 4903117395Skan TREE_TYPE (field)); 4904117395Skan DECL_CONTEXT (*next_field) = base_t; 4905117395Skan DECL_FIELD_OFFSET (*next_field) = DECL_FIELD_OFFSET (field); 4906117395Skan DECL_FIELD_BIT_OFFSET (*next_field) 4907117395Skan = DECL_FIELD_BIT_OFFSET (field); 4908132718Skan DECL_SIZE (*next_field) = DECL_SIZE (field); 4909132718Skan DECL_MODE (*next_field) = DECL_MODE (field); 4910117395Skan next_field = &TREE_CHAIN (*next_field); 4911117395Skan } 4912117395Skan 4913117395Skan /* Record the base version of the type. */ 4914117395Skan CLASSTYPE_AS_BASE (t) = base_t; 4915117395Skan TYPE_CONTEXT (base_t) = t; 491690075Sobrien } 491790075Sobrien else 4918117395Skan CLASSTYPE_AS_BASE (t) = t; 491990075Sobrien 4920107590Sobrien /* Every empty class contains an empty class. */ 4921117395Skan if (CLASSTYPE_EMPTY_P (t)) 4922107590Sobrien CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1; 4923107590Sobrien 492490075Sobrien /* Set the TYPE_DECL for this type to contain the right 492590075Sobrien value for DECL_OFFSET, so that we can use it as part 492690075Sobrien of a COMPONENT_REF for multiple inheritance. */ 492790075Sobrien layout_decl (TYPE_MAIN_DECL (t), 0); 492890075Sobrien 492990075Sobrien /* Now fix up any virtual base class types that we left lying 493090075Sobrien around. We must get these done before we try to lay out the 493190075Sobrien virtual function table. As a side-effect, this will remove the 493290075Sobrien base subobject fields. */ 4933117395Skan layout_virtual_bases (rli, empty_base_offsets); 493490075Sobrien 4935169689Skan /* Make sure that empty classes are reflected in RLI at this 4936117395Skan point. */ 4937117395Skan include_empty_classes(rli); 493890075Sobrien 4939117395Skan /* Make sure not to create any structures with zero size. */ 4940117395Skan if (integer_zerop (rli_size_unit_so_far (rli)) && CLASSTYPE_EMPTY_P (t)) 4941169689Skan place_field (rli, 4942117395Skan build_decl (FIELD_DECL, NULL_TREE, char_type_node)); 4943117395Skan 4944117395Skan /* Let the back-end lay out the type. */ 4945117395Skan finish_record_layout (rli, /*free_p=*/true); 4946117395Skan 4947117395Skan /* Warn about bases that can't be talked about due to ambiguity. */ 4948117395Skan warn_about_ambiguous_bases (t); 4949117395Skan 4950169689Skan /* Now that we're done with layout, give the base fields the real types. */ 4951169689Skan for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field)) 4952169689Skan if (DECL_ARTIFICIAL (field) && IS_FAKE_BASE_TYPE (TREE_TYPE (field))) 4953169689Skan TREE_TYPE (field) = TYPE_CONTEXT (TREE_TYPE (field)); 4954169689Skan 495590075Sobrien /* Clean up. */ 495690075Sobrien splay_tree_delete (empty_base_offsets); 4957169689Skan 4958169689Skan if (CLASSTYPE_EMPTY_P (t) 4959169689Skan && tree_int_cst_lt (sizeof_biggest_empty_class, 4960169689Skan TYPE_SIZE_UNIT (t))) 4961169689Skan sizeof_biggest_empty_class = TYPE_SIZE_UNIT (t); 496290075Sobrien} 496390075Sobrien 4964169689Skan/* Determine the "key method" for the class type indicated by TYPE, 4965169689Skan and set CLASSTYPE_KEY_METHOD accordingly. */ 496690075Sobrien 4967169689Skanvoid 4968169689Skandetermine_key_method (tree type) 4969117395Skan{ 4970117395Skan tree method; 497190075Sobrien 4972117395Skan if (TYPE_FOR_JAVA (type) 4973117395Skan || processing_template_decl 4974117395Skan || CLASSTYPE_TEMPLATE_INSTANTIATION (type) 4975117395Skan || CLASSTYPE_INTERFACE_KNOWN (type)) 4976169689Skan return; 497790075Sobrien 4978169689Skan /* The key method is the first non-pure virtual function that is not 4979169689Skan inline at the point of class definition. On some targets the 4980169689Skan key function may not be inline; those targets should not call 4981169689Skan this function until the end of the translation unit. */ 4982117395Skan for (method = TYPE_METHODS (type); method != NULL_TREE; 4983117395Skan method = TREE_CHAIN (method)) 4984117395Skan if (DECL_VINDEX (method) != NULL_TREE 4985117395Skan && ! DECL_DECLARED_INLINE_P (method) 4986117395Skan && ! DECL_PURE_VIRTUAL_P (method)) 4987169689Skan { 4988169689Skan CLASSTYPE_KEY_METHOD (type) = method; 4989169689Skan break; 4990169689Skan } 499190075Sobrien 4992169689Skan return; 4993117395Skan} 499490075Sobrien 4995117395Skan/* Perform processing required when the definition of T (a class type) 4996117395Skan is complete. */ 4997117395Skan 499890075Sobrienvoid 4999132718Skanfinish_struct_1 (tree t) 500090075Sobrien{ 500190075Sobrien tree x; 5002117395Skan /* A TREE_LIST. The TREE_VALUE of each node is a FUNCTION_DECL. */ 5003102780Skan tree virtuals = NULL_TREE; 500490075Sobrien int n_fields = 0; 500590075Sobrien 500690075Sobrien if (COMPLETE_TYPE_P (t)) 500790075Sobrien { 5008169689Skan gcc_assert (IS_AGGR_TYPE (t)); 5009169689Skan error ("redefinition of %q#T", t); 501090075Sobrien popclass (); 501190075Sobrien return; 501290075Sobrien } 501390075Sobrien 501490075Sobrien /* If this type was previously laid out as a forward reference, 501590075Sobrien make sure we lay it out again. */ 501690075Sobrien TYPE_SIZE (t) = NULL_TREE; 501790075Sobrien CLASSTYPE_PRIMARY_BINFO (t) = NULL_TREE; 501890075Sobrien 501990075Sobrien fixup_inline_methods (t); 5020169689Skan 5021117395Skan /* Make assumptions about the class; we'll reset the flags if 5022117395Skan necessary. */ 5023117395Skan CLASSTYPE_EMPTY_P (t) = 1; 5024117395Skan CLASSTYPE_NEARLY_EMPTY_P (t) = 1; 5025117395Skan CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 0; 5026117395Skan 502790075Sobrien /* Do end-of-class semantic processing: checking the validity of the 502890075Sobrien bases and members and add implicitly generated methods. */ 5029117395Skan check_bases_and_members (t); 503090075Sobrien 5031132718Skan /* Find the key method. */ 5032132718Skan if (TYPE_CONTAINS_VPTR_P (t)) 5033117395Skan { 5034169689Skan /* The Itanium C++ ABI permits the key method to be chosen when 5035169689Skan the class is defined -- even though the key method so 5036169689Skan selected may later turn out to be an inline function. On 5037169689Skan some systems (such as ARM Symbian OS) the key method cannot 5038169689Skan be determined until the end of the translation unit. On such 5039169689Skan systems, we leave CLASSTYPE_KEY_METHOD set to NULL, which 5040169689Skan will cause the class to be added to KEYED_CLASSES. Then, in 5041169689Skan finish_file we will determine the key method. */ 5042169689Skan if (targetm.cxx.key_method_may_be_inline ()) 5043169689Skan determine_key_method (t); 5044117395Skan 5045117395Skan /* If a polymorphic class has no key method, we may emit the vtable 5046132718Skan in every translation unit where the class definition appears. */ 5047117395Skan if (CLASSTYPE_KEY_METHOD (t) == NULL_TREE) 5048117395Skan keyed_classes = tree_cons (NULL_TREE, t, keyed_classes); 5049117395Skan } 5050117395Skan 505190075Sobrien /* Layout the class itself. */ 5052117395Skan layout_class_type (t, &virtuals); 5053132718Skan if (CLASSTYPE_AS_BASE (t) != t) 5054132718Skan /* We use the base type for trivial assignments, and hence it 5055132718Skan needs a mode. */ 5056132718Skan compute_record_mode (CLASSTYPE_AS_BASE (t)); 505790075Sobrien 5058117395Skan virtuals = modify_all_vtables (t, nreverse (virtuals)); 505990075Sobrien 506090075Sobrien /* If necessary, create the primary vtable for this class. */ 5061102780Skan if (virtuals || TYPE_CONTAINS_VPTR_P (t)) 506290075Sobrien { 506390075Sobrien /* We must enter these virtuals into the table. */ 506490075Sobrien if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t)) 506590075Sobrien build_primary_vtable (NULL_TREE, t); 5066132718Skan else if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t))) 506790075Sobrien /* Here we know enough to change the type of our virtual 506890075Sobrien function table, but we will wait until later this function. */ 506990075Sobrien build_primary_vtable (CLASSTYPE_PRIMARY_BINFO (t), t); 507018334Speter } 507118334Speter 507290075Sobrien if (TYPE_CONTAINS_VPTR_P (t)) 507318334Speter { 5074117395Skan int vindex; 5075117395Skan tree fn; 5076117395Skan 5077169689Skan if (BINFO_VTABLE (TYPE_BINFO (t))) 5078169689Skan gcc_assert (DECL_VIRTUAL_P (BINFO_VTABLE (TYPE_BINFO (t)))); 507990075Sobrien if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t)) 5080169689Skan gcc_assert (BINFO_VIRTUALS (TYPE_BINFO (t)) == NULL_TREE); 508190075Sobrien 5082102780Skan /* Add entries for virtual functions introduced by this class. */ 5083169689Skan BINFO_VIRTUALS (TYPE_BINFO (t)) 5084169689Skan = chainon (BINFO_VIRTUALS (TYPE_BINFO (t)), virtuals); 5085117395Skan 5086117395Skan /* Set DECL_VINDEX for all functions declared in this class. */ 5087169689Skan for (vindex = 0, fn = BINFO_VIRTUALS (TYPE_BINFO (t)); 5088169689Skan fn; 5089169689Skan fn = TREE_CHAIN (fn), 5090117395Skan vindex += (TARGET_VTABLE_USES_DESCRIPTORS 5091117395Skan ? TARGET_VTABLE_USES_DESCRIPTORS : 1)) 5092132718Skan { 5093132718Skan tree fndecl = BV_FN (fn); 5094132718Skan 5095132718Skan if (DECL_THUNK_P (fndecl)) 5096132718Skan /* A thunk. We should never be calling this entry directly 5097132718Skan from this vtable -- we'd use the entry for the non 5098132718Skan thunk base function. */ 5099132718Skan DECL_VINDEX (fndecl) = NULL_TREE; 5100132718Skan else if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST) 5101169689Skan DECL_VINDEX (fndecl) = build_int_cst (NULL_TREE, vindex); 5102132718Skan } 510318334Speter } 510418334Speter 510590075Sobrien finish_struct_bits (t); 510618334Speter 510790075Sobrien /* Complete the rtl for any static member objects of the type we're 510890075Sobrien working on. */ 510990075Sobrien for (x = TYPE_FIELDS (t); x; x = TREE_CHAIN (x)) 511090075Sobrien if (TREE_CODE (x) == VAR_DECL && TREE_STATIC (x) 5111169689Skan && TREE_TYPE (x) != error_mark_node 511296263Sobrien && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (x)), t)) 511390075Sobrien DECL_MODE (x) = TYPE_MODE (t); 511418334Speter 511590075Sobrien /* Done with FIELDS...now decide whether to sort these for 511690075Sobrien faster lookups later. 511718334Speter 5118117395Skan We use a small number because most searches fail (succeeding 511990075Sobrien ultimately as the search bores through the inheritance 512090075Sobrien hierarchy), and we want this failure to occur quickly. */ 512118334Speter 512290075Sobrien n_fields = count_fields (TYPE_FIELDS (t)); 512390075Sobrien if (n_fields > 7) 512418334Speter { 5125169689Skan struct sorted_fields_type *field_vec = GGC_NEWVAR 5126169689Skan (struct sorted_fields_type, 5127169689Skan sizeof (struct sorted_fields_type) + n_fields * sizeof (tree)); 5128132718Skan field_vec->len = n_fields; 5129132718Skan add_fields_to_record_type (TYPE_FIELDS (t), field_vec, 0); 5130132718Skan qsort (field_vec->elts, n_fields, sizeof (tree), 5131132718Skan field_decl_cmp); 513290075Sobrien if (! DECL_LANG_SPECIFIC (TYPE_MAIN_DECL (t))) 513390075Sobrien retrofit_lang_decl (TYPE_MAIN_DECL (t)); 513490075Sobrien DECL_SORTED_FIELDS (TYPE_MAIN_DECL (t)) = field_vec; 513518334Speter } 513618334Speter 5137169689Skan /* Complain if one of the field types requires lower visibility. */ 5138169689Skan constrain_class_visibility (t); 513918334Speter 514018334Speter /* Make the rtl for any new vtables we have created, and unmark 514118334Speter the base types we marked. */ 514290075Sobrien finish_vtbls (t); 5143169689Skan 514490075Sobrien /* Build the VTT for T. */ 514590075Sobrien build_vtt (t); 514618334Speter 5147169689Skan /* This warning does not make sense for Java classes, since they 5148169689Skan cannot have destructors. */ 5149169689Skan if (!TYPE_FOR_JAVA (t) && warn_nonvdtor && TYPE_POLYMORPHIC_P (t)) 5150169689Skan { 5151169689Skan tree dtor; 515218334Speter 5153169689Skan dtor = CLASSTYPE_DESTRUCTORS (t); 5154169689Skan /* Warn only if the dtor is non-private or the class has 5155169689Skan friends. */ 5156169689Skan if (/* An implicitly declared destructor is always public. And, 5157169689Skan if it were virtual, we would have created it by now. */ 5158169689Skan !dtor 5159169689Skan || (!DECL_VINDEX (dtor) 5160169689Skan && (!TREE_PRIVATE (dtor) 5161169689Skan || CLASSTYPE_FRIEND_CLASSES (t) 5162169689Skan || DECL_FRIENDLIST (TYPE_MAIN_DECL (t))))) 5163169689Skan warning (0, "%q#T has virtual functions but non-virtual destructor", 5164169689Skan t); 5165169689Skan } 5166169689Skan 516796263Sobrien complete_vars (t); 516818334Speter 516950397Sobrien if (warn_overloaded_virtual) 517050397Sobrien warn_hidden (t); 517118334Speter 5172169689Skan /* Class layout, assignment of virtual table slots, etc., is now 5173169689Skan complete. Give the back end a chance to tweak the visibility of 5174169689Skan the class or perform any other required target modifications. */ 5175169689Skan targetm.cxx.adjust_class_at_definition (t); 5176169689Skan 517790075Sobrien maybe_suppress_debug_info (t); 517818334Speter 517990075Sobrien dump_class_hierarchy (t); 5180169689Skan 518118334Speter /* Finish debugging output for this type. */ 518290075Sobrien rest_of_type_compilation (t, ! LOCAL_CLASS_P (t)); 518318334Speter} 518418334Speter 518552284Sobrien/* When T was built up, the member declarations were added in reverse 518652284Sobrien order. Rearrange them to declaration order. */ 518752284Sobrien 518852284Sobrienvoid 5189132718Skanunreverse_member_declarations (tree t) 519052284Sobrien{ 519152284Sobrien tree next; 519252284Sobrien tree prev; 519352284Sobrien tree x; 519452284Sobrien 5195117395Skan /* The following lists are all in reverse order. Put them in 5196117395Skan declaration order now. */ 519752284Sobrien TYPE_METHODS (t) = nreverse (TYPE_METHODS (t)); 5198117395Skan CLASSTYPE_DECL_LIST (t) = nreverse (CLASSTYPE_DECL_LIST (t)); 519952284Sobrien 520052284Sobrien /* Actually, for the TYPE_FIELDS, only the non TYPE_DECLs are in 520152284Sobrien reverse order, so we can't just use nreverse. */ 520252284Sobrien prev = NULL_TREE; 5203169689Skan for (x = TYPE_FIELDS (t); 5204169689Skan x && TREE_CODE (x) != TYPE_DECL; 520552284Sobrien x = next) 520652284Sobrien { 520752284Sobrien next = TREE_CHAIN (x); 520852284Sobrien TREE_CHAIN (x) = prev; 520952284Sobrien prev = x; 521052284Sobrien } 521152284Sobrien if (prev) 521252284Sobrien { 521352284Sobrien TREE_CHAIN (TYPE_FIELDS (t)) = x; 521452284Sobrien if (prev) 521552284Sobrien TYPE_FIELDS (t) = prev; 521652284Sobrien } 521752284Sobrien} 521852284Sobrien 521918334Spetertree 5220132718Skanfinish_struct (tree t, tree attributes) 522118334Speter{ 5222132718Skan location_t saved_loc = input_location; 522318334Speter 522452284Sobrien /* Now that we've got all the field declarations, reverse everything 522552284Sobrien as necessary. */ 522652284Sobrien unreverse_member_declarations (t); 522750397Sobrien 522890075Sobrien cplus_decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE); 522950397Sobrien 523090075Sobrien /* Nadger the current location so that diagnostics point to the start of 523190075Sobrien the struct, not the end. */ 5232132718Skan input_location = DECL_SOURCE_LOCATION (TYPE_NAME (t)); 523390075Sobrien 523450397Sobrien if (processing_template_decl) 523518334Speter { 5236169689Skan tree x; 5237169689Skan 523852284Sobrien finish_struct_methods (t); 523990075Sobrien TYPE_SIZE (t) = bitsize_zero_node; 5240146895Skan TYPE_SIZE_UNIT (t) = size_zero_node; 5241169689Skan 5242169689Skan /* We need to emit an error message if this type was used as a parameter 5243169689Skan and it is an abstract type, even if it is a template. We construct 5244169689Skan a simple CLASSTYPE_PURE_VIRTUALS list without taking bases into 5245169689Skan account and we call complete_vars with this type, which will check 5246169689Skan the PARM_DECLS. Note that while the type is being defined, 5247169689Skan CLASSTYPE_PURE_VIRTUALS contains the list of the inline friends 5248169689Skan (see CLASSTYPE_INLINE_FRIENDS) so we need to clear it. */ 5249169689Skan CLASSTYPE_PURE_VIRTUALS (t) = NULL; 5250169689Skan for (x = TYPE_METHODS (t); x; x = TREE_CHAIN (x)) 5251169689Skan if (DECL_PURE_VIRTUAL_P (x)) 5252169689Skan VEC_safe_push (tree, gc, CLASSTYPE_PURE_VIRTUALS (t), x); 5253169689Skan complete_vars (t); 525490075Sobrien } 525550397Sobrien else 525690075Sobrien finish_struct_1 (t); 525750397Sobrien 5258132718Skan input_location = saved_loc; 525990075Sobrien 526050397Sobrien TYPE_BEING_DEFINED (t) = 0; 526150397Sobrien 526250397Sobrien if (current_class_type) 526352284Sobrien popclass (); 526418334Speter else 526590075Sobrien error ("trying to finish struct, but kicked out due to previous parse errors"); 526650397Sobrien 5267117395Skan if (processing_template_decl && at_function_scope_p ()) 5268117395Skan add_stmt (build_min (TAG_DEFN, t)); 526990075Sobrien 527050397Sobrien return t; 527118334Speter} 527218334Speter 527352284Sobrien/* Return the dynamic type of INSTANCE, if known. 527418334Speter Used to determine whether the virtual function table is needed 527518334Speter or not. 527618334Speter 527718334Speter *NONNULL is set iff INSTANCE can be known to be nonnull, regardless 527890075Sobrien of our knowledge of its type. *NONNULL should be initialized 527990075Sobrien before this function is called. */ 528050397Sobrien 528152284Sobrienstatic tree 5282132718Skanfixed_type_or_null (tree instance, int* nonnull, int* cdtorp) 528318334Speter{ 528418334Speter switch (TREE_CODE (instance)) 528518334Speter { 528618334Speter case INDIRECT_REF: 528790075Sobrien if (POINTER_TYPE_P (TREE_TYPE (instance))) 528890075Sobrien return NULL_TREE; 528990075Sobrien else 529090075Sobrien return fixed_type_or_null (TREE_OPERAND (instance, 0), 529190075Sobrien nonnull, cdtorp); 529290075Sobrien 529318334Speter case CALL_EXPR: 529418334Speter /* This is a call to a constructor, hence it's never zero. */ 529518334Speter if (TREE_HAS_CONSTRUCTOR (instance)) 529618334Speter { 529718334Speter if (nonnull) 529818334Speter *nonnull = 1; 529952284Sobrien return TREE_TYPE (instance); 530018334Speter } 530152284Sobrien return NULL_TREE; 530218334Speter 530318334Speter case SAVE_EXPR: 530418334Speter /* This is a call to a constructor, hence it's never zero. */ 530518334Speter if (TREE_HAS_CONSTRUCTOR (instance)) 530618334Speter { 530718334Speter if (nonnull) 530818334Speter *nonnull = 1; 530952284Sobrien return TREE_TYPE (instance); 531018334Speter } 531190075Sobrien return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp); 531218334Speter 531318334Speter case PLUS_EXPR: 531418334Speter case MINUS_EXPR: 531590075Sobrien if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR) 531690075Sobrien return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp); 531718334Speter if (TREE_CODE (TREE_OPERAND (instance, 1)) == INTEGER_CST) 531818334Speter /* Propagate nonnull. */ 5319117395Skan return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp); 532052284Sobrien return NULL_TREE; 532118334Speter 532218334Speter case NOP_EXPR: 532318334Speter case CONVERT_EXPR: 532490075Sobrien return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp); 532518334Speter 532618334Speter case ADDR_EXPR: 5327169689Skan instance = TREE_OPERAND (instance, 0); 532818334Speter if (nonnull) 5329169689Skan { 5330169689Skan /* Just because we see an ADDR_EXPR doesn't mean we're dealing 5331169689Skan with a real object -- given &p->f, p can still be null. */ 5332169689Skan tree t = get_base_address (instance); 5333169689Skan /* ??? Probably should check DECL_WEAK here. */ 5334169689Skan if (t && DECL_P (t)) 5335169689Skan *nonnull = 1; 5336169689Skan } 5337169689Skan return fixed_type_or_null (instance, nonnull, cdtorp); 533818334Speter 533918334Speter case COMPONENT_REF: 5340169689Skan /* If this component is really a base class reference, then the field 5341169689Skan itself isn't definitive. */ 5342169689Skan if (DECL_FIELD_IS_BASE (TREE_OPERAND (instance, 1))) 5343169689Skan return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp); 534490075Sobrien return fixed_type_or_null (TREE_OPERAND (instance, 1), nonnull, cdtorp); 534518334Speter 534618334Speter case VAR_DECL: 534718334Speter case FIELD_DECL: 534818334Speter if (TREE_CODE (TREE_TYPE (instance)) == ARRAY_TYPE 534918334Speter && IS_AGGR_TYPE (TREE_TYPE (TREE_TYPE (instance)))) 535018334Speter { 535118334Speter if (nonnull) 535218334Speter *nonnull = 1; 535352284Sobrien return TREE_TYPE (TREE_TYPE (instance)); 535418334Speter } 535550397Sobrien /* fall through... */ 535618334Speter case TARGET_EXPR: 535718334Speter case PARM_DECL: 5358117395Skan case RESULT_DECL: 535918334Speter if (IS_AGGR_TYPE (TREE_TYPE (instance))) 536018334Speter { 536118334Speter if (nonnull) 536218334Speter *nonnull = 1; 536352284Sobrien return TREE_TYPE (instance); 536418334Speter } 536590075Sobrien else if (instance == current_class_ptr) 5366169689Skan { 5367169689Skan if (nonnull) 5368169689Skan *nonnull = 1; 5369169689Skan 5370169689Skan /* if we're in a ctor or dtor, we know our type. */ 5371169689Skan if (DECL_LANG_SPECIFIC (current_function_decl) 5372169689Skan && (DECL_CONSTRUCTOR_P (current_function_decl) 5373169689Skan || DECL_DESTRUCTOR_P (current_function_decl))) 5374169689Skan { 5375169689Skan if (cdtorp) 5376169689Skan *cdtorp = 1; 5377169689Skan return TREE_TYPE (TREE_TYPE (instance)); 5378169689Skan } 5379169689Skan } 538090075Sobrien else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE) 5381169689Skan { 5382171825Skan /* We only need one hash table because it is always left empty. */ 5383171825Skan static htab_t ht; 5384171825Skan if (!ht) 5385171825Skan ht = htab_create (37, 5386171825Skan htab_hash_pointer, 5387171825Skan htab_eq_pointer, 5388171825Skan /*htab_del=*/NULL); 5389171825Skan 5390169689Skan /* Reference variables should be references to objects. */ 5391169689Skan if (nonnull) 539218334Speter *nonnull = 1; 5393169689Skan 5394171825Skan /* Enter the INSTANCE in a table to prevent recursion; a 5395117395Skan variable's initializer may refer to the variable 5396117395Skan itself. */ 5397169689Skan if (TREE_CODE (instance) == VAR_DECL 5398117395Skan && DECL_INITIAL (instance) 5399171825Skan && !htab_find (ht, instance)) 5400117395Skan { 5401117395Skan tree type; 5402171825Skan void **slot; 5403171825Skan 5404171825Skan slot = htab_find_slot (ht, instance, INSERT); 5405171825Skan *slot = instance; 5406117395Skan type = fixed_type_or_null (DECL_INITIAL (instance), 5407117395Skan nonnull, cdtorp); 5408171825Skan htab_remove_elt (ht, instance); 5409171825Skan 5410117395Skan return type; 5411117395Skan } 541218334Speter } 541352284Sobrien return NULL_TREE; 541418334Speter 541518334Speter default: 541652284Sobrien return NULL_TREE; 541718334Speter } 541818334Speter} 541952284Sobrien 5420117395Skan/* Return nonzero if the dynamic type of INSTANCE is known, and 542190075Sobrien equivalent to the static type. We also handle the case where 542290075Sobrien INSTANCE is really a pointer. Return negative if this is a 542390075Sobrien ctor/dtor. There the dynamic type is known, but this might not be 542490075Sobrien the most derived base of the original object, and hence virtual 542590075Sobrien bases may not be layed out according to this type. 542652284Sobrien 542752284Sobrien Used to determine whether the virtual function table is needed 542852284Sobrien or not. 542952284Sobrien 543052284Sobrien *NONNULL is set iff INSTANCE can be known to be nonnull, regardless 543190075Sobrien of our knowledge of its type. *NONNULL should be initialized 543290075Sobrien before this function is called. */ 543352284Sobrien 543452284Sobrienint 5435132718Skanresolves_to_fixed_type_p (tree instance, int* nonnull) 543652284Sobrien{ 543752284Sobrien tree t = TREE_TYPE (instance); 543890075Sobrien int cdtorp = 0; 5439169689Skan 544090075Sobrien tree fixed = fixed_type_or_null (instance, nonnull, &cdtorp); 544152284Sobrien if (fixed == NULL_TREE) 544252284Sobrien return 0; 544352284Sobrien if (POINTER_TYPE_P (t)) 544452284Sobrien t = TREE_TYPE (t); 544590075Sobrien if (!same_type_ignoring_top_level_qualifiers_p (t, fixed)) 544690075Sobrien return 0; 544790075Sobrien return cdtorp ? -1 : 1; 544852284Sobrien} 544952284Sobrien 545018334Speter 545118334Spetervoid 5452132718Skaninit_class_processing (void) 545318334Speter{ 545418334Speter current_class_depth = 0; 545552284Sobrien current_class_stack_size = 10; 5456169689Skan current_class_stack 5457169689Skan = XNEWVEC (struct class_stack_node, current_class_stack_size); 5458169689Skan local_classes = VEC_alloc (tree, gc, 8); 5459169689Skan sizeof_biggest_empty_class = size_zero_node; 546018334Speter 546190075Sobrien ridpointers[(int) RID_PUBLIC] = access_public_node; 546290075Sobrien ridpointers[(int) RID_PRIVATE] = access_private_node; 546390075Sobrien ridpointers[(int) RID_PROTECTED] = access_protected_node; 546418334Speter} 546518334Speter 5466169689Skan/* Restore the cached PREVIOUS_CLASS_LEVEL. */ 5467169689Skan 5468169689Skanstatic void 5469169689Skanrestore_class_cache (void) 5470169689Skan{ 5471169689Skan tree type; 5472169689Skan 5473169689Skan /* We are re-entering the same class we just left, so we don't 5474169689Skan have to search the whole inheritance matrix to find all the 5475169689Skan decls to bind again. Instead, we install the cached 5476169689Skan class_shadowed list and walk through it binding names. */ 5477169689Skan push_binding_level (previous_class_level); 5478169689Skan class_binding_level = previous_class_level; 5479169689Skan /* Restore IDENTIFIER_TYPE_VALUE. */ 5480169689Skan for (type = class_binding_level->type_shadowed; 5481169689Skan type; 5482169689Skan type = TREE_CHAIN (type)) 5483169689Skan SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (type), TREE_TYPE (type)); 5484169689Skan} 5485169689Skan 5486132718Skan/* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE as 5487132718Skan appropriate for TYPE. 548818334Speter 548918334Speter So that we may avoid calls to lookup_name, we cache the _TYPE 549018334Speter nodes of local TYPE_DECLs in the TREE_TYPE field of the name. 549118334Speter 549218334Speter For multiple inheritance, we perform a two-pass depth-first search 5493169689Skan of the type lattice. */ 549418334Speter 549518334Spetervoid 5496132718Skanpushclass (tree type) 549718334Speter{ 5498169689Skan class_stack_node_t csn; 5499169689Skan 550050397Sobrien type = TYPE_MAIN_VARIANT (type); 550118334Speter 550252284Sobrien /* Make sure there is enough room for the new entry on the stack. */ 5503169689Skan if (current_class_depth + 1 >= current_class_stack_size) 550418334Speter { 550552284Sobrien current_class_stack_size *= 2; 550652284Sobrien current_class_stack 5507169689Skan = XRESIZEVEC (struct class_stack_node, current_class_stack, 5508169689Skan current_class_stack_size); 550918334Speter } 551018334Speter 551152284Sobrien /* Insert a new entry on the class stack. */ 5512169689Skan csn = current_class_stack + current_class_depth; 5513169689Skan csn->name = current_class_name; 5514169689Skan csn->type = current_class_type; 5515169689Skan csn->access = current_access_specifier; 5516169689Skan csn->names_used = 0; 5517169689Skan csn->hidden = 0; 551852284Sobrien current_class_depth++; 551952284Sobrien 552052284Sobrien /* Now set up the new type. */ 552118334Speter current_class_name = TYPE_NAME (type); 552218334Speter if (TREE_CODE (current_class_name) == TYPE_DECL) 552318334Speter current_class_name = DECL_NAME (current_class_name); 552418334Speter current_class_type = type; 552518334Speter 552652284Sobrien /* By default, things in classes are private, while things in 552752284Sobrien structures or unions are public. */ 5528169689Skan current_access_specifier = (CLASSTYPE_DECLARED_CLASS (type) 5529169689Skan ? access_private_node 553052284Sobrien : access_public_node); 553152284Sobrien 5532169689Skan if (previous_class_level 5533169689Skan && type != previous_class_level->this_entity 553418334Speter && current_class_depth == 1) 553518334Speter { 553618334Speter /* Forcibly remove any old class remnants. */ 553752284Sobrien invalidate_class_lookup_cache (); 553818334Speter } 553918334Speter 5540169689Skan if (!previous_class_level 5541169689Skan || type != previous_class_level->this_entity 5542169689Skan || current_class_depth > 1) 5543169689Skan pushlevel_class (); 5544132718Skan else 5545169689Skan restore_class_cache (); 554652284Sobrien} 554750397Sobrien 5548169689Skan/* When we exit a toplevel class scope, we save its binding level so 5549169689Skan that we can restore it quickly. Here, we've entered some other 5550169689Skan class, so we must invalidate our cache. */ 555118334Speter 555252284Sobrienvoid 5553132718Skaninvalidate_class_lookup_cache (void) 555452284Sobrien{ 5555169689Skan previous_class_level = NULL; 5556169689Skan} 555790075Sobrien 555818334Speter/* Get out of the current class scope. If we were in a class scope 555952284Sobrien previously, that is the one popped to. */ 556050397Sobrien 556118334Spetervoid 5562132718Skanpopclass (void) 556318334Speter{ 556490075Sobrien poplevel_class (); 556518334Speter 556618334Speter current_class_depth--; 556752284Sobrien current_class_name = current_class_stack[current_class_depth].name; 556852284Sobrien current_class_type = current_class_stack[current_class_depth].type; 556952284Sobrien current_access_specifier = current_class_stack[current_class_depth].access; 557052284Sobrien if (current_class_stack[current_class_depth].names_used) 557152284Sobrien splay_tree_delete (current_class_stack[current_class_depth].names_used); 557218334Speter} 557318334Speter 5574169689Skan/* Mark the top of the class stack as hidden. */ 557550397Sobrien 5576169689Skanvoid 5577169689Skanpush_class_stack (void) 5578169689Skan{ 5579169689Skan if (current_class_depth) 5580169689Skan ++current_class_stack[current_class_depth - 1].hidden; 5581169689Skan} 5582169689Skan 5583169689Skan/* Mark the top of the class stack as un-hidden. */ 5584169689Skan 5585169689Skanvoid 5586169689Skanpop_class_stack (void) 5587169689Skan{ 5588169689Skan if (current_class_depth) 5589169689Skan --current_class_stack[current_class_depth - 1].hidden; 5590169689Skan} 5591169689Skan 5592169689Skan/* Returns 1 if the class type currently being defined is either T or 5593169689Skan a nested type of T. */ 5594169689Skan 5595169689Skanbool 5596132718Skancurrently_open_class (tree t) 559750397Sobrien{ 559850397Sobrien int i; 5599169689Skan 5600169689Skan /* We start looking from 1 because entry 0 is from global scope, 5601169689Skan and has no type. */ 5602169689Skan for (i = current_class_depth; i > 0; --i) 5603169689Skan { 5604169689Skan tree c; 5605169689Skan if (i == current_class_depth) 5606169689Skan c = current_class_type; 5607169689Skan else 5608169689Skan { 5609169689Skan if (current_class_stack[i].hidden) 5610169689Skan break; 5611169689Skan c = current_class_stack[i].type; 5612169689Skan } 5613169689Skan if (!c) 5614169689Skan continue; 5615169689Skan if (same_type_p (c, t)) 5616169689Skan return true; 5617169689Skan } 5618169689Skan return false; 561950397Sobrien} 562050397Sobrien 562190075Sobrien/* If either current_class_type or one of its enclosing classes are derived 562290075Sobrien from T, return the appropriate type. Used to determine how we found 562390075Sobrien something via unqualified lookup. */ 562490075Sobrien 562590075Sobrientree 5626132718Skancurrently_open_derived_class (tree t) 562790075Sobrien{ 562890075Sobrien int i; 562990075Sobrien 5630132718Skan /* The bases of a dependent type are unknown. */ 5631132718Skan if (dependent_type_p (t)) 5632132718Skan return NULL_TREE; 5633132718Skan 5634132718Skan if (!current_class_type) 5635132718Skan return NULL_TREE; 5636132718Skan 563790075Sobrien if (DERIVED_FROM_P (t, current_class_type)) 563890075Sobrien return current_class_type; 563990075Sobrien 564090075Sobrien for (i = current_class_depth - 1; i > 0; --i) 5641169689Skan { 5642169689Skan if (current_class_stack[i].hidden) 5643169689Skan break; 5644169689Skan if (DERIVED_FROM_P (t, current_class_stack[i].type)) 5645169689Skan return current_class_stack[i].type; 5646169689Skan } 564790075Sobrien 564890075Sobrien return NULL_TREE; 564990075Sobrien} 565090075Sobrien 565118334Speter/* When entering a class scope, all enclosing class scopes' names with 5652132718Skan static meaning (static variables, static functions, types and 5653132718Skan enumerators) have to be visible. This recursive function calls 5654132718Skan pushclass for all enclosing class contexts until global or a local 5655132718Skan scope is reached. TYPE is the enclosed class. */ 565618334Speter 565718334Spetervoid 5658132718Skanpush_nested_class (tree type) 565918334Speter{ 566018334Speter tree context; 566118334Speter 566252284Sobrien /* A namespace might be passed in error cases, like A::B:C. */ 5663169689Skan if (type == NULL_TREE 5664169689Skan || type == error_mark_node 566552284Sobrien || TREE_CODE (type) == NAMESPACE_DECL 566690075Sobrien || ! IS_AGGR_TYPE (type) 566750397Sobrien || TREE_CODE (type) == TEMPLATE_TYPE_PARM 566890075Sobrien || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM) 566918334Speter return; 5670169689Skan 567150397Sobrien context = DECL_CONTEXT (TYPE_MAIN_DECL (type)); 567218334Speter 567352284Sobrien if (context && CLASS_TYPE_P (context)) 5674132718Skan push_nested_class (context); 5675132718Skan pushclass (type); 567618334Speter} 567718334Speter 5678132718Skan/* Undoes a push_nested_class call. */ 567918334Speter 568018334Spetervoid 5681132718Skanpop_nested_class (void) 568218334Speter{ 568350397Sobrien tree context = DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type)); 568418334Speter 568552284Sobrien popclass (); 568652284Sobrien if (context && CLASS_TYPE_P (context)) 568752284Sobrien pop_nested_class (); 568818334Speter} 568918334Speter 569090075Sobrien/* Returns the number of extern "LANG" blocks we are nested within. */ 569190075Sobrien 569290075Sobrienint 5693132718Skancurrent_lang_depth (void) 569490075Sobrien{ 5695169689Skan return VEC_length (tree, current_lang_base); 569690075Sobrien} 569790075Sobrien 569818334Speter/* Set global variables CURRENT_LANG_NAME to appropriate value 569918334Speter so that behavior of name-mangling machinery is correct. */ 570018334Speter 570118334Spetervoid 5702132718Skanpush_lang_context (tree name) 570318334Speter{ 5704169689Skan VEC_safe_push (tree, gc, current_lang_base, current_lang_name); 570518334Speter 570652284Sobrien if (name == lang_name_cplusplus) 570718334Speter { 570818334Speter current_lang_name = name; 570918334Speter } 571052284Sobrien else if (name == lang_name_java) 571152284Sobrien { 571252284Sobrien current_lang_name = name; 571352284Sobrien /* DECL_IGNORED_P is initially set for these types, to avoid clutter. 571452284Sobrien (See record_builtin_java_type in decl.c.) However, that causes 571552284Sobrien incorrect debug entries if these types are actually used. 5716117395Skan So we re-enable debug output after extern "Java". */ 571790075Sobrien DECL_IGNORED_P (TYPE_NAME (java_byte_type_node)) = 0; 571890075Sobrien DECL_IGNORED_P (TYPE_NAME (java_short_type_node)) = 0; 571990075Sobrien DECL_IGNORED_P (TYPE_NAME (java_int_type_node)) = 0; 572090075Sobrien DECL_IGNORED_P (TYPE_NAME (java_long_type_node)) = 0; 572190075Sobrien DECL_IGNORED_P (TYPE_NAME (java_float_type_node)) = 0; 572290075Sobrien DECL_IGNORED_P (TYPE_NAME (java_double_type_node)) = 0; 572390075Sobrien DECL_IGNORED_P (TYPE_NAME (java_char_type_node)) = 0; 572490075Sobrien DECL_IGNORED_P (TYPE_NAME (java_boolean_type_node)) = 0; 572552284Sobrien } 572618334Speter else if (name == lang_name_c) 572718334Speter { 572818334Speter current_lang_name = name; 572918334Speter } 573018334Speter else 5731169689Skan error ("language string %<\"%E\"%> not recognized", name); 573218334Speter} 5733169689Skan 573418334Speter/* Get out of the current language scope. */ 573550397Sobrien 573618334Spetervoid 5737132718Skanpop_lang_context (void) 573818334Speter{ 5739169689Skan current_lang_name = VEC_pop (tree, current_lang_base); 574018334Speter} 574150397Sobrien 574250397Sobrien/* Type instantiation routines. */ 574318334Speter 574452284Sobrien/* Given an OVERLOAD and a TARGET_TYPE, return the function that 574552284Sobrien matches the TARGET_TYPE. If there is no satisfactory match, return 5746169689Skan error_mark_node, and issue an error & warning messages under 5747169689Skan control of FLAGS. Permit pointers to member function if FLAGS 5748169689Skan permits. If TEMPLATE_ONLY, the name of the overloaded function was 5749169689Skan a template-id, and EXPLICIT_TARGS are the explicitly provided 5750169689Skan template arguments. If OVERLOAD is for one or more member 5751169689Skan functions, then ACCESS_PATH is the base path used to reference 5752169689Skan those member functions. */ 575352284Sobrien 575450397Sobrienstatic tree 5755169689Skanresolve_address_of_overloaded_function (tree target_type, 5756132718Skan tree overload, 5757132718Skan tsubst_flags_t flags, 5758132718Skan bool template_only, 5759169689Skan tree explicit_targs, 5760169689Skan tree access_path) 576118334Speter{ 576252284Sobrien /* Here's what the standard says: 5763169689Skan 576452284Sobrien [over.over] 576550397Sobrien 576652284Sobrien If the name is a function template, template argument deduction 576752284Sobrien is done, and if the argument deduction succeeds, the deduced 576852284Sobrien arguments are used to generate a single template function, which 576952284Sobrien is added to the set of overloaded functions considered. 577052284Sobrien 577152284Sobrien Non-member functions and static member functions match targets of 577252284Sobrien type "pointer-to-function" or "reference-to-function." Nonstatic 577352284Sobrien member functions match targets of type "pointer-to-member 577452284Sobrien function;" the function type of the pointer to member is used to 577552284Sobrien select the member function from the set of overloaded member 577652284Sobrien functions. If a nonstatic member function is selected, the 577752284Sobrien reference to the overloaded function name is required to have the 577852284Sobrien form of a pointer to member as described in 5.3.1. 577952284Sobrien 578052284Sobrien If more than one function is selected, any template functions in 578152284Sobrien the set are eliminated if the set also contains a non-template 578252284Sobrien function, and any given template function is eliminated if the 578352284Sobrien set contains a second template function that is more specialized 578452284Sobrien than the first according to the partial ordering rules 14.5.5.2. 578552284Sobrien After such eliminations, if any, there shall remain exactly one 578652284Sobrien selected function. */ 578752284Sobrien 578852284Sobrien int is_ptrmem = 0; 578952284Sobrien int is_reference = 0; 579052284Sobrien /* We store the matches in a TREE_LIST rooted here. The functions 579152284Sobrien are the TREE_PURPOSE, not the TREE_VALUE, in this list, for easy 579252284Sobrien interoperability with most_specialized_instantiation. */ 579352284Sobrien tree matches = NULL_TREE; 579452284Sobrien tree fn; 579552284Sobrien 579652284Sobrien /* By the time we get here, we should be seeing only real 579752284Sobrien pointer-to-member types, not the internal POINTER_TYPE to 579852284Sobrien METHOD_TYPE representation. */ 5799169689Skan gcc_assert (TREE_CODE (target_type) != POINTER_TYPE 5800169689Skan || TREE_CODE (TREE_TYPE (target_type)) != METHOD_TYPE); 580152284Sobrien 5802169689Skan gcc_assert (is_overloaded_fn (overload)); 5803169689Skan 580452284Sobrien /* Check that the TARGET_TYPE is reasonable. */ 580552284Sobrien if (TYPE_PTRFN_P (target_type)) 580690075Sobrien /* This is OK. */; 580752284Sobrien else if (TYPE_PTRMEMFUNC_P (target_type)) 580852284Sobrien /* This is OK, too. */ 580952284Sobrien is_ptrmem = 1; 581052284Sobrien else if (TREE_CODE (target_type) == FUNCTION_TYPE) 581150397Sobrien { 581252284Sobrien /* This is OK, too. This comes from a conversion to reference 581352284Sobrien type. */ 581452284Sobrien target_type = build_reference_type (target_type); 581552284Sobrien is_reference = 1; 581652284Sobrien } 5817169689Skan else 581852284Sobrien { 5819122180Skan if (flags & tf_error) 5820169689Skan error ("cannot resolve overloaded function %qD based on" 5821169689Skan " conversion to type %qT", 5822169689Skan DECL_NAME (OVL_FUNCTION (overload)), target_type); 582352284Sobrien return error_mark_node; 582452284Sobrien } 5825169689Skan 582652284Sobrien /* If we can find a non-template function that matches, we can just 582752284Sobrien use it. There's no point in generating template instantiations 582852284Sobrien if we're just going to throw them out anyhow. But, of course, we 582952284Sobrien can only do this when we don't *need* a template function. */ 583052284Sobrien if (!template_only) 583152284Sobrien { 583252284Sobrien tree fns; 583352284Sobrien 5834132718Skan for (fns = overload; fns; fns = OVL_NEXT (fns)) 583552284Sobrien { 5836132718Skan tree fn = OVL_CURRENT (fns); 583752284Sobrien tree fntype; 583852284Sobrien 583952284Sobrien if (TREE_CODE (fn) == TEMPLATE_DECL) 584052284Sobrien /* We're not looking for templates just yet. */ 584152284Sobrien continue; 584252284Sobrien 584352284Sobrien if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE) 584452284Sobrien != is_ptrmem) 584552284Sobrien /* We're looking for a non-static member, and this isn't 584652284Sobrien one, or vice versa. */ 584752284Sobrien continue; 5848122180Skan 5849169689Skan /* Ignore functions which haven't been explicitly 5850169689Skan declared. */ 5851122180Skan if (DECL_ANTICIPATED (fn)) 5852122180Skan continue; 5853122180Skan 585452284Sobrien /* See if there's a match. */ 585552284Sobrien fntype = TREE_TYPE (fn); 585652284Sobrien if (is_ptrmem) 585752284Sobrien fntype = build_ptrmemfunc_type (build_pointer_type (fntype)); 585852284Sobrien else if (!is_reference) 585952284Sobrien fntype = build_pointer_type (fntype); 586052284Sobrien 5861169689Skan if (can_convert_arg (target_type, fntype, fn, LOOKUP_NORMAL)) 586290075Sobrien matches = tree_cons (fn, NULL_TREE, matches); 586352284Sobrien } 586452284Sobrien } 586552284Sobrien 586652284Sobrien /* Now, if we've already got a match (or matches), there's no need 586752284Sobrien to proceed to the template functions. But, if we don't have a 586852284Sobrien match we need to look at them, too. */ 5869169689Skan if (!matches) 587052284Sobrien { 587152284Sobrien tree target_fn_type; 587252284Sobrien tree target_arg_types; 587390075Sobrien tree target_ret_type; 587452284Sobrien tree fns; 587552284Sobrien 587652284Sobrien if (is_ptrmem) 587752284Sobrien target_fn_type 587852284Sobrien = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (target_type)); 587950397Sobrien else 588052284Sobrien target_fn_type = TREE_TYPE (target_type); 588152284Sobrien target_arg_types = TYPE_ARG_TYPES (target_fn_type); 588290075Sobrien target_ret_type = TREE_TYPE (target_fn_type); 588390075Sobrien 588490075Sobrien /* Never do unification on the 'this' parameter. */ 588590075Sobrien if (TREE_CODE (target_fn_type) == METHOD_TYPE) 588690075Sobrien target_arg_types = TREE_CHAIN (target_arg_types); 5887169689Skan 5888132718Skan for (fns = overload; fns; fns = OVL_NEXT (fns)) 588950397Sobrien { 5890132718Skan tree fn = OVL_CURRENT (fns); 589152284Sobrien tree instantiation; 589252284Sobrien tree instantiation_type; 589352284Sobrien tree targs; 589452284Sobrien 589552284Sobrien if (TREE_CODE (fn) != TEMPLATE_DECL) 589652284Sobrien /* We're only looking for templates. */ 589752284Sobrien continue; 589852284Sobrien 589952284Sobrien if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE) 590052284Sobrien != is_ptrmem) 590152284Sobrien /* We're not looking for a non-static member, and this is 590252284Sobrien one, or vice versa. */ 590352284Sobrien continue; 590452284Sobrien 590552284Sobrien /* Try to do argument deduction. */ 590690075Sobrien targs = make_tree_vec (DECL_NTPARMS (fn)); 590752284Sobrien if (fn_type_unification (fn, explicit_targs, targs, 590890075Sobrien target_arg_types, target_ret_type, 5909169689Skan DEDUCE_EXACT, LOOKUP_NORMAL)) 591052284Sobrien /* Argument deduction failed. */ 591152284Sobrien continue; 591252284Sobrien 591352284Sobrien /* Instantiate the template. */ 5914132718Skan instantiation = instantiate_template (fn, targs, flags); 591552284Sobrien if (instantiation == error_mark_node) 591652284Sobrien /* Instantiation failed. */ 591752284Sobrien continue; 591852284Sobrien 591952284Sobrien /* See if there's a match. */ 592052284Sobrien instantiation_type = TREE_TYPE (instantiation); 592152284Sobrien if (is_ptrmem) 5922169689Skan instantiation_type = 592352284Sobrien build_ptrmemfunc_type (build_pointer_type (instantiation_type)); 592452284Sobrien else if (!is_reference) 592552284Sobrien instantiation_type = build_pointer_type (instantiation_type); 5926169689Skan if (can_convert_arg (target_type, instantiation_type, instantiation, 5927169689Skan LOOKUP_NORMAL)) 592890075Sobrien matches = tree_cons (instantiation, fn, matches); 592950397Sobrien } 593052284Sobrien 593152284Sobrien /* Now, remove all but the most specialized of the matches. */ 593252284Sobrien if (matches) 593352284Sobrien { 593490075Sobrien tree match = most_specialized_instantiation (matches); 593552284Sobrien 593652284Sobrien if (match != error_mark_node) 5937169689Skan matches = tree_cons (TREE_PURPOSE (match), 5938169689Skan NULL_TREE, 5939169689Skan NULL_TREE); 594052284Sobrien } 594150397Sobrien } 594252284Sobrien 594352284Sobrien /* Now we should have exactly one function in MATCHES. */ 594452284Sobrien if (matches == NULL_TREE) 594552284Sobrien { 594652284Sobrien /* There were *no* matches. */ 5947122180Skan if (flags & tf_error) 594852284Sobrien { 5949169689Skan error ("no matches converting function %qD to type %q#T", 5950169689Skan DECL_NAME (OVL_FUNCTION (overload)), 5951169689Skan target_type); 595252284Sobrien 595352284Sobrien /* print_candidates expects a chain with the functions in 5954169689Skan TREE_VALUE slots, so we cons one up here (we're losing anyway, 5955169689Skan so why be clever?). */ 5956169689Skan for (; overload; overload = OVL_NEXT (overload)) 5957169689Skan matches = tree_cons (NULL_TREE, OVL_CURRENT (overload), 595890075Sobrien matches); 5959169689Skan 596052284Sobrien print_candidates (matches); 596152284Sobrien } 596252284Sobrien return error_mark_node; 596352284Sobrien } 596452284Sobrien else if (TREE_CHAIN (matches)) 596552284Sobrien { 596652284Sobrien /* There were too many matches. */ 596752284Sobrien 5968122180Skan if (flags & tf_error) 596952284Sobrien { 597052284Sobrien tree match; 597152284Sobrien 5972169689Skan error ("converting overloaded function %qD to type %q#T is ambiguous", 597352284Sobrien DECL_NAME (OVL_FUNCTION (overload)), 597452284Sobrien target_type); 597552284Sobrien 597652284Sobrien /* Since print_candidates expects the functions in the 597752284Sobrien TREE_VALUE slot, we flip them here. */ 597852284Sobrien for (match = matches; match; match = TREE_CHAIN (match)) 597952284Sobrien TREE_VALUE (match) = TREE_PURPOSE (match); 598052284Sobrien 598152284Sobrien print_candidates (matches); 598252284Sobrien } 5983169689Skan 598452284Sobrien return error_mark_node; 598552284Sobrien } 598652284Sobrien 598752284Sobrien /* Good, exactly one match. Now, convert it to the correct type. */ 598852284Sobrien fn = TREE_PURPOSE (matches); 598952284Sobrien 599090075Sobrien if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn) 5991132718Skan && !(flags & tf_ptrmem_ok) && !flag_ms_extensions) 599290075Sobrien { 599390075Sobrien static int explained; 5994169689Skan 5995122180Skan if (!(flags & tf_error)) 5996169689Skan return error_mark_node; 599790075Sobrien 5998169689Skan pedwarn ("assuming pointer to member %qD", fn); 599990075Sobrien if (!explained) 6000169689Skan { 6001169689Skan pedwarn ("(a pointer to member can only be formed with %<&%E%>)", fn); 6002169689Skan explained = 1; 6003169689Skan } 600490075Sobrien } 600552284Sobrien 6006122180Skan /* If we're doing overload resolution purely for the purpose of 6007122180Skan determining conversion sequences, we should not consider the 6008122180Skan function used. If this conversion sequence is selected, the 6009122180Skan function will be marked as used at this point. */ 6010122180Skan if (!(flags & tf_conv)) 6011169689Skan { 6012169689Skan mark_used (fn); 6013169689Skan /* We could not check access when this expression was originally 6014169689Skan created since we did not know at that time to which function 6015169689Skan the expression referred. */ 6016169689Skan if (DECL_FUNCTION_MEMBER_P (fn)) 6017169689Skan { 6018169689Skan gcc_assert (access_path); 6019169689Skan perform_or_defer_access_check (access_path, fn, fn); 6020169689Skan } 6021169689Skan } 6022122180Skan 602352284Sobrien if (TYPE_PTRFN_P (target_type) || TYPE_PTRMEMFUNC_P (target_type)) 602452284Sobrien return build_unary_op (ADDR_EXPR, fn, 0); 602552284Sobrien else 602652284Sobrien { 602752284Sobrien /* The target must be a REFERENCE_TYPE. Above, build_unary_op 602852284Sobrien will mark the function as addressed, but here we must do it 602952284Sobrien explicitly. */ 6030117395Skan cxx_mark_addressable (fn); 603152284Sobrien 603252284Sobrien return fn; 603352284Sobrien } 603418334Speter} 603518334Speter 603650397Sobrien/* This function will instantiate the type of the expression given in 603750397Sobrien RHS to match the type of LHSTYPE. If errors exist, then return 6038132718Skan error_mark_node. FLAGS is a bit mask. If TF_ERROR is set, then 603990075Sobrien we complain on errors. If we are not complaining, never modify rhs, 604090075Sobrien as overload resolution wants to try many possible instantiations, in 604190075Sobrien the hope that at least one will work. 6042169689Skan 604352284Sobrien For non-recursive calls, LHSTYPE should be a function, pointer to 604452284Sobrien function, or a pointer to member function. */ 604552284Sobrien 604618334Spetertree 6047132718Skaninstantiate_type (tree lhstype, tree rhs, tsubst_flags_t flags) 604818334Speter{ 6049122180Skan tsubst_flags_t flags_in = flags; 6050169689Skan tree access_path = NULL_TREE; 6051169689Skan 605296263Sobrien flags &= ~tf_ptrmem_ok; 6053169689Skan 605418334Speter if (TREE_CODE (lhstype) == UNKNOWN_TYPE) 605518334Speter { 6056132718Skan if (flags & tf_error) 605718334Speter error ("not enough type information"); 605818334Speter return error_mark_node; 605918334Speter } 606018334Speter 606118334Speter if (TREE_TYPE (rhs) != NULL_TREE && ! (type_unknown_p (rhs))) 606250397Sobrien { 6063132718Skan if (same_type_p (lhstype, TREE_TYPE (rhs))) 606450397Sobrien return rhs; 6065169689Skan if (flag_ms_extensions 6066132718Skan && TYPE_PTRMEMFUNC_P (lhstype) 6067132718Skan && !TYPE_PTRMEMFUNC_P (TREE_TYPE (rhs))) 6068132718Skan /* Microsoft allows `A::f' to be resolved to a 6069132718Skan pointer-to-member. */ 6070132718Skan ; 6071132718Skan else 6072132718Skan { 6073132718Skan if (flags & tf_error) 6074169689Skan error ("argument of type %qT does not match %qT", 6075132718Skan TREE_TYPE (rhs), lhstype); 6076132718Skan return error_mark_node; 6077132718Skan } 607850397Sobrien } 607918334Speter 6080117395Skan if (TREE_CODE (rhs) == BASELINK) 6081169689Skan { 6082169689Skan access_path = BASELINK_ACCESS_BINFO (rhs); 6083169689Skan rhs = BASELINK_FUNCTIONS (rhs); 6084169689Skan } 6085117395Skan 6086169689Skan /* If we are in a template, and have a NON_DEPENDENT_EXPR, we cannot 6087169689Skan deduce any type information. */ 6088169689Skan if (TREE_CODE (rhs) == NON_DEPENDENT_EXPR) 6089169689Skan { 6090169689Skan if (flags & tf_error) 6091169689Skan error ("not enough type information"); 6092169689Skan return error_mark_node; 6093169689Skan } 6094169689Skan 6095169689Skan /* There only a few kinds of expressions that may have a type 6096169689Skan dependent on overload resolution. */ 6097169689Skan gcc_assert (TREE_CODE (rhs) == ADDR_EXPR 6098169689Skan || TREE_CODE (rhs) == COMPONENT_REF 6099169689Skan || TREE_CODE (rhs) == COMPOUND_EXPR 6100169689Skan || really_overloaded_fn (rhs)); 6101169689Skan 610250397Sobrien /* We don't overwrite rhs if it is an overloaded function. 610350397Sobrien Copying it would destroy the tree link. */ 610450397Sobrien if (TREE_CODE (rhs) != OVERLOAD) 610550397Sobrien rhs = copy_node (rhs); 610650397Sobrien 610718334Speter /* This should really only be used when attempting to distinguish 610818334Speter what sort of a pointer to function we have. For now, any 610918334Speter arithmetic operation which is not supported on pointers 611018334Speter is rejected as an error. */ 611118334Speter 611218334Speter switch (TREE_CODE (rhs)) 611318334Speter { 611418334Speter case COMPONENT_REF: 6115132718Skan { 6116169689Skan tree member = TREE_OPERAND (rhs, 1); 611718334Speter 6118169689Skan member = instantiate_type (lhstype, member, flags); 6119169689Skan if (member != error_mark_node 6120132718Skan && TREE_SIDE_EFFECTS (TREE_OPERAND (rhs, 0))) 6121132718Skan /* Do not lose object's side effects. */ 6122169689Skan return build2 (COMPOUND_EXPR, TREE_TYPE (member), 6123169689Skan TREE_OPERAND (rhs, 0), member); 6124169689Skan return member; 6125132718Skan } 6126132718Skan 612750397Sobrien case OFFSET_REF: 612852284Sobrien rhs = TREE_OPERAND (rhs, 1); 612952284Sobrien if (BASELINK_P (rhs)) 6130169689Skan return instantiate_type (lhstype, rhs, flags_in); 613152284Sobrien 613250397Sobrien /* This can happen if we are forming a pointer-to-member for a 613350397Sobrien member template. */ 6134169689Skan gcc_assert (TREE_CODE (rhs) == TEMPLATE_ID_EXPR); 613552284Sobrien 613650397Sobrien /* Fall through. */ 613750397Sobrien 613850397Sobrien case TEMPLATE_ID_EXPR: 613990075Sobrien { 614090075Sobrien tree fns = TREE_OPERAND (rhs, 0); 614190075Sobrien tree args = TREE_OPERAND (rhs, 1); 614218334Speter 614390075Sobrien return 6144132718Skan resolve_address_of_overloaded_function (lhstype, fns, flags_in, 6145132718Skan /*template_only=*/true, 6146169689Skan args, access_path); 614790075Sobrien } 614890075Sobrien 614950397Sobrien case OVERLOAD: 6150132718Skan case FUNCTION_DECL: 6151169689Skan return 6152132718Skan resolve_address_of_overloaded_function (lhstype, rhs, flags_in, 6153132718Skan /*template_only=*/false, 6154169689Skan /*explicit_targs=*/NULL_TREE, 6155169689Skan access_path); 615618334Speter 615718334Speter case COMPOUND_EXPR: 615818334Speter TREE_OPERAND (rhs, 0) 615952284Sobrien = instantiate_type (lhstype, TREE_OPERAND (rhs, 0), flags); 616018334Speter if (TREE_OPERAND (rhs, 0) == error_mark_node) 616118334Speter return error_mark_node; 616218334Speter TREE_OPERAND (rhs, 1) 616352284Sobrien = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags); 616418334Speter if (TREE_OPERAND (rhs, 1) == error_mark_node) 616518334Speter return error_mark_node; 616618334Speter 616718334Speter TREE_TYPE (rhs) = lhstype; 616818334Speter return rhs; 616918334Speter 617018334Speter case ADDR_EXPR: 617190075Sobrien { 617290075Sobrien if (PTRMEM_OK_P (rhs)) 6173169689Skan flags |= tf_ptrmem_ok; 6174169689Skan 617552284Sobrien return instantiate_type (lhstype, TREE_OPERAND (rhs, 0), flags); 617690075Sobrien } 617718334Speter 617818334Speter case ERROR_MARK: 617918334Speter return error_mark_node; 618018334Speter 618118334Speter default: 6182169689Skan gcc_unreachable (); 618318334Speter } 6184169689Skan return error_mark_node; 618518334Speter} 618618334Speter 618718334Speter/* Return the name of the virtual function pointer field 618818334Speter (as an IDENTIFIER_NODE) for the given TYPE. Note that 618918334Speter this may have to look back through base types to find the 619018334Speter ultimate field name. (For single inheritance, these could 619118334Speter all be the same name. Who knows for multiple inheritance). */ 619250397Sobrien 619318334Speterstatic tree 6194132718Skanget_vfield_name (tree type) 619518334Speter{ 6196169689Skan tree binfo, base_binfo; 619718334Speter char *buf; 619818334Speter 6199169689Skan for (binfo = TYPE_BINFO (type); 6200169689Skan BINFO_N_BASE_BINFOS (binfo); 6201169689Skan binfo = base_binfo) 6202169689Skan { 6203169689Skan base_binfo = BINFO_BASE_BINFO (binfo, 0); 620418334Speter 6205169689Skan if (BINFO_VIRTUAL_P (base_binfo) 6206169689Skan || !TYPE_CONTAINS_VPTR_P (BINFO_TYPE (base_binfo))) 6207169689Skan break; 6208169689Skan } 6209169689Skan 621018334Speter type = BINFO_TYPE (binfo); 6211169689Skan buf = (char *) alloca (sizeof (VFIELD_NAME_FORMAT) 6212169689Skan + TYPE_NAME_LENGTH (type) + 2); 621396263Sobrien sprintf (buf, VFIELD_NAME_FORMAT, 621496263Sobrien IDENTIFIER_POINTER (constructor_name (type))); 621518334Speter return get_identifier (buf); 621618334Speter} 621718334Speter 621818334Spetervoid 6219132718Skanprint_class_statistics (void) 622018334Speter{ 622118334Speter#ifdef GATHER_STATISTICS 622218334Speter fprintf (stderr, "convert_harshness = %d\n", n_convert_harshness); 622318334Speter fprintf (stderr, "compute_conversion_costs = %d\n", n_compute_conversion_costs); 622418334Speter if (n_vtables) 622518334Speter { 622618334Speter fprintf (stderr, "vtables = %d; vtable searches = %d\n", 622718334Speter n_vtables, n_vtable_searches); 622818334Speter fprintf (stderr, "vtable entries = %d; vtable elems = %d\n", 622918334Speter n_vtable_entries, n_vtable_elems); 623018334Speter } 623118334Speter#endif 623218334Speter} 623318334Speter 623450397Sobrien/* Build a dummy reference to ourselves so Derived::Base (and A::A) works, 623550397Sobrien according to [class]: 6236169689Skan The class-name is also inserted 623750397Sobrien into the scope of the class itself. For purposes of access checking, 623850397Sobrien the inserted class name is treated as if it were a public member name. */ 623950397Sobrien 624052284Sobrienvoid 6241132718Skanbuild_self_reference (void) 624250397Sobrien{ 624350397Sobrien tree name = constructor_name (current_class_type); 624450397Sobrien tree value = build_lang_decl (TYPE_DECL, name, current_class_type); 624552284Sobrien tree saved_cas; 624652284Sobrien 624750397Sobrien DECL_NONLOCAL (value) = 1; 624850397Sobrien DECL_CONTEXT (value) = current_class_type; 624950397Sobrien DECL_ARTIFICIAL (value) = 1; 6250119256Skan SET_DECL_SELF_REFERENCE_P (value); 625150397Sobrien 625290075Sobrien if (processing_template_decl) 625390075Sobrien value = push_template_decl (value); 625490075Sobrien 625552284Sobrien saved_cas = current_access_specifier; 625652284Sobrien current_access_specifier = access_public_node; 625752284Sobrien finish_member_declaration (value); 625852284Sobrien current_access_specifier = saved_cas; 625950397Sobrien} 626050397Sobrien 626150397Sobrien/* Returns 1 if TYPE contains only padding bytes. */ 626250397Sobrien 626350397Sobrienint 6264132718Skanis_empty_class (tree type) 626550397Sobrien{ 626650397Sobrien if (type == error_mark_node) 626750397Sobrien return 0; 626850397Sobrien 626950397Sobrien if (! IS_AGGR_TYPE (type)) 627050397Sobrien return 0; 627150397Sobrien 6272117395Skan /* In G++ 3.2, whether or not a class was empty was determined by 6273117395Skan looking at its size. */ 6274117395Skan if (abi_version_at_least (2)) 6275117395Skan return CLASSTYPE_EMPTY_P (type); 6276117395Skan else 6277117395Skan return integer_zerop (CLASSTYPE_SIZE (type)); 627850397Sobrien} 627952284Sobrien 6280117395Skan/* Returns true if TYPE contains an empty class. */ 6281117395Skan 6282117395Skanstatic bool 6283117395Skancontains_empty_class_p (tree type) 6284117395Skan{ 6285117395Skan if (is_empty_class (type)) 6286117395Skan return true; 6287117395Skan if (CLASS_TYPE_P (type)) 6288117395Skan { 6289117395Skan tree field; 6290169689Skan tree binfo; 6291169689Skan tree base_binfo; 6292117395Skan int i; 6293117395Skan 6294169689Skan for (binfo = TYPE_BINFO (type), i = 0; 6295169689Skan BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i) 6296169689Skan if (contains_empty_class_p (BINFO_TYPE (base_binfo))) 6297117395Skan return true; 6298117395Skan for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field)) 6299117395Skan if (TREE_CODE (field) == FIELD_DECL 6300117395Skan && !DECL_ARTIFICIAL (field) 6301117395Skan && is_empty_class (TREE_TYPE (field))) 6302117395Skan return true; 6303117395Skan } 6304117395Skan else if (TREE_CODE (type) == ARRAY_TYPE) 6305117395Skan return contains_empty_class_p (TREE_TYPE (type)); 6306117395Skan return false; 6307117395Skan} 6308117395Skan 630952284Sobrien/* Note that NAME was looked up while the current class was being 631052284Sobrien defined and that the result of that lookup was DECL. */ 631152284Sobrien 631252284Sobrienvoid 6313132718Skanmaybe_note_name_used_in_class (tree name, tree decl) 631452284Sobrien{ 631552284Sobrien splay_tree names_used; 631652284Sobrien 631752284Sobrien /* If we're not defining a class, there's nothing to do. */ 6318169689Skan if (!(innermost_scope_kind() == sk_class 6319169689Skan && TYPE_BEING_DEFINED (current_class_type))) 632052284Sobrien return; 6321169689Skan 632252284Sobrien /* If there's already a binding for this NAME, then we don't have 632352284Sobrien anything to worry about. */ 6324169689Skan if (lookup_member (current_class_type, name, 6325169689Skan /*protect=*/0, /*want_type=*/false)) 632652284Sobrien return; 632752284Sobrien 632852284Sobrien if (!current_class_stack[current_class_depth - 1].names_used) 632952284Sobrien current_class_stack[current_class_depth - 1].names_used 633052284Sobrien = splay_tree_new (splay_tree_compare_pointers, 0, 0); 633152284Sobrien names_used = current_class_stack[current_class_depth - 1].names_used; 633252284Sobrien 633352284Sobrien splay_tree_insert (names_used, 6334169689Skan (splay_tree_key) name, 633552284Sobrien (splay_tree_value) decl); 633652284Sobrien} 633752284Sobrien 633852284Sobrien/* Note that NAME was declared (as DECL) in the current class. Check 6339117395Skan to see that the declaration is valid. */ 634052284Sobrien 634152284Sobrienvoid 6342132718Skannote_name_declared_in_class (tree name, tree decl) 634352284Sobrien{ 634452284Sobrien splay_tree names_used; 634552284Sobrien splay_tree_node n; 634652284Sobrien 634752284Sobrien /* Look to see if we ever used this name. */ 6348169689Skan names_used 634952284Sobrien = current_class_stack[current_class_depth - 1].names_used; 635052284Sobrien if (!names_used) 635152284Sobrien return; 635252284Sobrien 635352284Sobrien n = splay_tree_lookup (names_used, (splay_tree_key) name); 635452284Sobrien if (n) 635552284Sobrien { 635652284Sobrien /* [basic.scope.class] 6357169689Skan 635852284Sobrien A name N used in a class S shall refer to the same declaration 635952284Sobrien in its context and when re-evaluated in the completed scope of 636052284Sobrien S. */ 6361169689Skan error ("declaration of %q#D", decl); 6362169689Skan error ("changes meaning of %qD from %q+#D", 6363169689Skan DECL_NAME (OVL_CURRENT (decl)), (tree) n->value); 636452284Sobrien } 636552284Sobrien} 636690075Sobrien 636790075Sobrien/* Returns the VAR_DECL for the complete vtable associated with BINFO. 636890075Sobrien Secondary vtables are merged with primary vtables; this function 636990075Sobrien will return the VAR_DECL for the primary vtable. */ 637090075Sobrien 637190075Sobrientree 6372132718Skanget_vtbl_decl_for_binfo (tree binfo) 637390075Sobrien{ 637490075Sobrien tree decl; 637590075Sobrien 637690075Sobrien decl = BINFO_VTABLE (binfo); 637790075Sobrien if (decl && TREE_CODE (decl) == PLUS_EXPR) 637890075Sobrien { 6379169689Skan gcc_assert (TREE_CODE (TREE_OPERAND (decl, 0)) == ADDR_EXPR); 638090075Sobrien decl = TREE_OPERAND (TREE_OPERAND (decl, 0), 0); 638190075Sobrien } 638290075Sobrien if (decl) 6383169689Skan gcc_assert (TREE_CODE (decl) == VAR_DECL); 638490075Sobrien return decl; 638590075Sobrien} 638690075Sobrien 638790075Sobrien 6388132718Skan/* Returns the binfo for the primary base of BINFO. If the resulting 6389132718Skan BINFO is a virtual base, and it is inherited elsewhere in the 6390132718Skan hierarchy, then the returned binfo might not be the primary base of 6391132718Skan BINFO in the complete object. Check BINFO_PRIMARY_P or 6392132718Skan BINFO_LOST_PRIMARY_P to be sure. */ 639390075Sobrien 6394169689Skanstatic tree 6395132718Skanget_primary_binfo (tree binfo) 639690075Sobrien{ 639790075Sobrien tree primary_base; 6398169689Skan 639990075Sobrien primary_base = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (binfo)); 640090075Sobrien if (!primary_base) 640190075Sobrien return NULL_TREE; 640290075Sobrien 6403169689Skan return copied_binfo (primary_base, binfo); 640490075Sobrien} 640590075Sobrien 6406117395Skan/* If INDENTED_P is zero, indent to INDENT. Return nonzero. */ 640790075Sobrien 640890075Sobrienstatic int 6409132718Skanmaybe_indent_hierarchy (FILE * stream, int indent, int indented_p) 641090075Sobrien{ 641190075Sobrien if (!indented_p) 641290075Sobrien fprintf (stream, "%*s", indent, ""); 641390075Sobrien return 1; 641490075Sobrien} 641590075Sobrien 6416132718Skan/* Dump the offsets of all the bases rooted at BINFO to STREAM. 6417132718Skan INDENT should be zero when called from the top level; it is 6418132718Skan incremented recursively. IGO indicates the next expected BINFO in 6419132718Skan inheritance graph ordering. */ 642090075Sobrien 6421132718Skanstatic tree 6422132718Skandump_class_hierarchy_r (FILE *stream, 6423169689Skan int flags, 6424169689Skan tree binfo, 6425169689Skan tree igo, 6426169689Skan int indent) 642790075Sobrien{ 642890075Sobrien int indented = 0; 6429169689Skan tree base_binfo; 6430169689Skan int i; 6431169689Skan 643290075Sobrien indented = maybe_indent_hierarchy (stream, indent, 0); 643390075Sobrien fprintf (stream, "%s (0x%lx) ", 6434169689Skan type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER), 643590075Sobrien (unsigned long) binfo); 6436132718Skan if (binfo != igo) 6437132718Skan { 6438132718Skan fprintf (stream, "alternative-path\n"); 6439132718Skan return igo; 6440132718Skan } 6441132718Skan igo = TREE_CHAIN (binfo); 6442169689Skan 644390075Sobrien fprintf (stream, HOST_WIDE_INT_PRINT_DEC, 644490075Sobrien tree_low_cst (BINFO_OFFSET (binfo), 0)); 644590075Sobrien if (is_empty_class (BINFO_TYPE (binfo))) 644690075Sobrien fprintf (stream, " empty"); 644790075Sobrien else if (CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (binfo))) 644890075Sobrien fprintf (stream, " nearly-empty"); 6449169689Skan if (BINFO_VIRTUAL_P (binfo)) 6450132718Skan fprintf (stream, " virtual"); 645190075Sobrien fprintf (stream, "\n"); 645290075Sobrien 645390075Sobrien indented = 0; 6454169689Skan if (BINFO_PRIMARY_P (binfo)) 645590075Sobrien { 645690075Sobrien indented = maybe_indent_hierarchy (stream, indent + 3, indented); 645790075Sobrien fprintf (stream, " primary-for %s (0x%lx)", 6458169689Skan type_as_string (BINFO_TYPE (BINFO_INHERITANCE_CHAIN (binfo)), 645990075Sobrien TFF_PLAIN_IDENTIFIER), 6460169689Skan (unsigned long)BINFO_INHERITANCE_CHAIN (binfo)); 646190075Sobrien } 646290075Sobrien if (BINFO_LOST_PRIMARY_P (binfo)) 646390075Sobrien { 646490075Sobrien indented = maybe_indent_hierarchy (stream, indent + 3, indented); 646590075Sobrien fprintf (stream, " lost-primary"); 646690075Sobrien } 646790075Sobrien if (indented) 646890075Sobrien fprintf (stream, "\n"); 646990075Sobrien 647090075Sobrien if (!(flags & TDF_SLIM)) 647190075Sobrien { 647290075Sobrien int indented = 0; 6473169689Skan 647490075Sobrien if (BINFO_SUBVTT_INDEX (binfo)) 647590075Sobrien { 647690075Sobrien indented = maybe_indent_hierarchy (stream, indent + 3, indented); 647790075Sobrien fprintf (stream, " subvttidx=%s", 647890075Sobrien expr_as_string (BINFO_SUBVTT_INDEX (binfo), 647990075Sobrien TFF_PLAIN_IDENTIFIER)); 648090075Sobrien } 648190075Sobrien if (BINFO_VPTR_INDEX (binfo)) 648290075Sobrien { 648390075Sobrien indented = maybe_indent_hierarchy (stream, indent + 3, indented); 648490075Sobrien fprintf (stream, " vptridx=%s", 648590075Sobrien expr_as_string (BINFO_VPTR_INDEX (binfo), 648690075Sobrien TFF_PLAIN_IDENTIFIER)); 648790075Sobrien } 648890075Sobrien if (BINFO_VPTR_FIELD (binfo)) 648990075Sobrien { 649090075Sobrien indented = maybe_indent_hierarchy (stream, indent + 3, indented); 649190075Sobrien fprintf (stream, " vbaseoffset=%s", 649290075Sobrien expr_as_string (BINFO_VPTR_FIELD (binfo), 649390075Sobrien TFF_PLAIN_IDENTIFIER)); 649490075Sobrien } 649590075Sobrien if (BINFO_VTABLE (binfo)) 649690075Sobrien { 649790075Sobrien indented = maybe_indent_hierarchy (stream, indent + 3, indented); 649890075Sobrien fprintf (stream, " vptr=%s", 649990075Sobrien expr_as_string (BINFO_VTABLE (binfo), 650090075Sobrien TFF_PLAIN_IDENTIFIER)); 650190075Sobrien } 6502169689Skan 650390075Sobrien if (indented) 650490075Sobrien fprintf (stream, "\n"); 650590075Sobrien } 650690075Sobrien 6507169689Skan for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++) 6508169689Skan igo = dump_class_hierarchy_r (stream, flags, base_binfo, igo, indent + 2); 6509132718Skan 6510132718Skan return igo; 651190075Sobrien} 651290075Sobrien 651390075Sobrien/* Dump the BINFO hierarchy for T. */ 651490075Sobrien 651590075Sobrienstatic void 6516132718Skandump_class_hierarchy_1 (FILE *stream, int flags, tree t) 651790075Sobrien{ 651890075Sobrien fprintf (stream, "Class %s\n", type_as_string (t, TFF_PLAIN_IDENTIFIER)); 651990075Sobrien fprintf (stream, " size=%lu align=%lu\n", 652090075Sobrien (unsigned long)(tree_low_cst (TYPE_SIZE (t), 0) / BITS_PER_UNIT), 652190075Sobrien (unsigned long)(TYPE_ALIGN (t) / BITS_PER_UNIT)); 6522132718Skan fprintf (stream, " base size=%lu base align=%lu\n", 6523132718Skan (unsigned long)(tree_low_cst (TYPE_SIZE (CLASSTYPE_AS_BASE (t)), 0) 6524132718Skan / BITS_PER_UNIT), 6525132718Skan (unsigned long)(TYPE_ALIGN (CLASSTYPE_AS_BASE (t)) 6526132718Skan / BITS_PER_UNIT)); 6527132718Skan dump_class_hierarchy_r (stream, flags, TYPE_BINFO (t), TYPE_BINFO (t), 0); 652890075Sobrien fprintf (stream, "\n"); 652990075Sobrien} 653090075Sobrien 6531132718Skan/* Debug interface to hierarchy dumping. */ 6532132718Skan 6533169689Skanvoid 6534132718Skandebug_class (tree t) 6535132718Skan{ 6536132718Skan dump_class_hierarchy_1 (stderr, TDF_SLIM, t); 6537132718Skan} 6538132718Skan 653990075Sobrienstatic void 6540132718Skandump_class_hierarchy (tree t) 654190075Sobrien{ 6542132718Skan int flags; 6543132718Skan FILE *stream = dump_begin (TDI_class, &flags); 6544132718Skan 6545132718Skan if (stream) 6546132718Skan { 6547132718Skan dump_class_hierarchy_1 (stream, flags, t); 6548132718Skan dump_end (TDI_class, stream); 6549132718Skan } 6550132718Skan} 6551132718Skan 6552132718Skanstatic void 6553132718Skandump_array (FILE * stream, tree decl) 6554132718Skan{ 6555169689Skan tree value; 6556169689Skan unsigned HOST_WIDE_INT ix; 655790075Sobrien HOST_WIDE_INT elt; 655890075Sobrien tree size = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (decl))); 655990075Sobrien 656090075Sobrien elt = (tree_low_cst (TYPE_SIZE (TREE_TYPE (TREE_TYPE (decl))), 0) 656190075Sobrien / BITS_PER_UNIT); 656290075Sobrien fprintf (stream, "%s:", decl_as_string (decl, TFF_PLAIN_IDENTIFIER)); 656390075Sobrien fprintf (stream, " %s entries", 656490075Sobrien expr_as_string (size_binop (PLUS_EXPR, size, size_one_node), 656590075Sobrien TFF_PLAIN_IDENTIFIER)); 656690075Sobrien fprintf (stream, "\n"); 656790075Sobrien 6568169689Skan FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)), 6569169689Skan ix, value) 657090075Sobrien fprintf (stream, "%-4ld %s\n", (long)(ix * elt), 6571169689Skan expr_as_string (value, TFF_PLAIN_IDENTIFIER)); 657290075Sobrien} 657390075Sobrien 657490075Sobrienstatic void 6575132718Skandump_vtable (tree t, tree binfo, tree vtable) 657690075Sobrien{ 657790075Sobrien int flags; 657890075Sobrien FILE *stream = dump_begin (TDI_class, &flags); 657990075Sobrien 658090075Sobrien if (!stream) 658190075Sobrien return; 658290075Sobrien 658390075Sobrien if (!(flags & TDF_SLIM)) 658490075Sobrien { 658590075Sobrien int ctor_vtbl_p = TYPE_BINFO (t) != binfo; 6586169689Skan 658790075Sobrien fprintf (stream, "%s for %s", 658890075Sobrien ctor_vtbl_p ? "Construction vtable" : "Vtable", 6589169689Skan type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER)); 659090075Sobrien if (ctor_vtbl_p) 659190075Sobrien { 6592169689Skan if (!BINFO_VIRTUAL_P (binfo)) 659390075Sobrien fprintf (stream, " (0x%lx instance)", (unsigned long)binfo); 659490075Sobrien fprintf (stream, " in %s", type_as_string (t, TFF_PLAIN_IDENTIFIER)); 659590075Sobrien } 659690075Sobrien fprintf (stream, "\n"); 659790075Sobrien dump_array (stream, vtable); 659890075Sobrien fprintf (stream, "\n"); 659990075Sobrien } 6600169689Skan 660190075Sobrien dump_end (TDI_class, stream); 660290075Sobrien} 660390075Sobrien 660490075Sobrienstatic void 6605132718Skandump_vtt (tree t, tree vtt) 660690075Sobrien{ 660790075Sobrien int flags; 660890075Sobrien FILE *stream = dump_begin (TDI_class, &flags); 660990075Sobrien 661090075Sobrien if (!stream) 661190075Sobrien return; 661290075Sobrien 661390075Sobrien if (!(flags & TDF_SLIM)) 661490075Sobrien { 661590075Sobrien fprintf (stream, "VTT for %s\n", 661690075Sobrien type_as_string (t, TFF_PLAIN_IDENTIFIER)); 661790075Sobrien dump_array (stream, vtt); 661890075Sobrien fprintf (stream, "\n"); 661990075Sobrien } 6620169689Skan 662190075Sobrien dump_end (TDI_class, stream); 662290075Sobrien} 662390075Sobrien 6624132718Skan/* Dump a function or thunk and its thunkees. */ 6625132718Skan 6626132718Skanstatic void 6627132718Skandump_thunk (FILE *stream, int indent, tree thunk) 6628132718Skan{ 6629132718Skan static const char spaces[] = " "; 6630132718Skan tree name = DECL_NAME (thunk); 6631132718Skan tree thunks; 6632169689Skan 6633132718Skan fprintf (stream, "%.*s%p %s %s", indent, spaces, 6634132718Skan (void *)thunk, 6635132718Skan !DECL_THUNK_P (thunk) ? "function" 6636132718Skan : DECL_THIS_THUNK_P (thunk) ? "this-thunk" : "covariant-thunk", 6637132718Skan name ? IDENTIFIER_POINTER (name) : "<unset>"); 6638132718Skan if (DECL_THUNK_P (thunk)) 6639132718Skan { 6640132718Skan HOST_WIDE_INT fixed_adjust = THUNK_FIXED_OFFSET (thunk); 6641132718Skan tree virtual_adjust = THUNK_VIRTUAL_OFFSET (thunk); 6642132718Skan 6643132718Skan fprintf (stream, " fixed=" HOST_WIDE_INT_PRINT_DEC, fixed_adjust); 6644132718Skan if (!virtual_adjust) 6645132718Skan /*NOP*/; 6646132718Skan else if (DECL_THIS_THUNK_P (thunk)) 6647132718Skan fprintf (stream, " vcall=" HOST_WIDE_INT_PRINT_DEC, 6648132718Skan tree_low_cst (virtual_adjust, 0)); 6649132718Skan else 6650132718Skan fprintf (stream, " vbase=" HOST_WIDE_INT_PRINT_DEC "(%s)", 6651132718Skan tree_low_cst (BINFO_VPTR_FIELD (virtual_adjust), 0), 6652132718Skan type_as_string (BINFO_TYPE (virtual_adjust), TFF_SCOPE)); 6653132718Skan if (THUNK_ALIAS (thunk)) 6654132718Skan fprintf (stream, " alias to %p", (void *)THUNK_ALIAS (thunk)); 6655132718Skan } 6656132718Skan fprintf (stream, "\n"); 6657132718Skan for (thunks = DECL_THUNKS (thunk); thunks; thunks = TREE_CHAIN (thunks)) 6658132718Skan dump_thunk (stream, indent + 2, thunks); 6659132718Skan} 6660132718Skan 6661132718Skan/* Dump the thunks for FN. */ 6662132718Skan 6663169689Skanvoid 6664132718Skandebug_thunks (tree fn) 6665132718Skan{ 6666132718Skan dump_thunk (stderr, 0, fn); 6667132718Skan} 6668132718Skan 666990075Sobrien/* Virtual function table initialization. */ 667090075Sobrien 667190075Sobrien/* Create all the necessary vtables for T and its base classes. */ 667290075Sobrien 667390075Sobrienstatic void 6674132718Skanfinish_vtbls (tree t) 667590075Sobrien{ 667690075Sobrien tree list; 667790075Sobrien tree vbase; 667890075Sobrien 667990075Sobrien /* We lay out the primary and secondary vtables in one contiguous 668090075Sobrien vtable. The primary vtable is first, followed by the non-virtual 668190075Sobrien secondary vtables in inheritance graph order. */ 6682169689Skan list = build_tree_list (BINFO_VTABLE (TYPE_BINFO (t)), NULL_TREE); 668390075Sobrien accumulate_vtbl_inits (TYPE_BINFO (t), TYPE_BINFO (t), 668490075Sobrien TYPE_BINFO (t), t, list); 6685169689Skan 668690075Sobrien /* Then come the virtual bases, also in inheritance graph order. */ 668790075Sobrien for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase)) 668890075Sobrien { 6689169689Skan if (!BINFO_VIRTUAL_P (vbase)) 669090075Sobrien continue; 6691132718Skan accumulate_vtbl_inits (vbase, vbase, TYPE_BINFO (t), t, list); 669290075Sobrien } 669390075Sobrien 6694169689Skan if (BINFO_VTABLE (TYPE_BINFO (t))) 669590075Sobrien initialize_vtable (TYPE_BINFO (t), TREE_VALUE (list)); 669690075Sobrien} 669790075Sobrien 669890075Sobrien/* Initialize the vtable for BINFO with the INITS. */ 669990075Sobrien 670090075Sobrienstatic void 6701132718Skaninitialize_vtable (tree binfo, tree inits) 670290075Sobrien{ 670390075Sobrien tree decl; 670490075Sobrien 670590075Sobrien layout_vtable_decl (binfo, list_length (inits)); 670690075Sobrien decl = get_vtbl_decl_for_binfo (binfo); 6707169689Skan initialize_artificial_var (decl, inits); 670890075Sobrien dump_vtable (BINFO_TYPE (binfo), binfo, decl); 670990075Sobrien} 671090075Sobrien 671190075Sobrien/* Build the VTT (virtual table table) for T. 671290075Sobrien A class requires a VTT if it has virtual bases. 6713169689Skan 671490075Sobrien This holds 671590075Sobrien 1 - primary virtual pointer for complete object T 671690075Sobrien 2 - secondary VTTs for each direct non-virtual base of T which requires a 671790075Sobrien VTT 671890075Sobrien 3 - secondary virtual pointers for each direct or indirect base of T which 671990075Sobrien has virtual bases or is reachable via a virtual path from T. 672090075Sobrien 4 - secondary VTTs for each direct or indirect virtual base of T. 6721169689Skan 672290075Sobrien Secondary VTTs look like complete object VTTs without part 4. */ 672390075Sobrien 672490075Sobrienstatic void 6725132718Skanbuild_vtt (tree t) 672690075Sobrien{ 672790075Sobrien tree inits; 672890075Sobrien tree type; 672990075Sobrien tree vtt; 673090075Sobrien tree index; 673190075Sobrien 673290075Sobrien /* Build up the initializers for the VTT. */ 673390075Sobrien inits = NULL_TREE; 673490075Sobrien index = size_zero_node; 673590075Sobrien build_vtt_inits (TYPE_BINFO (t), t, &inits, &index); 673690075Sobrien 673790075Sobrien /* If we didn't need a VTT, we're done. */ 673890075Sobrien if (!inits) 673990075Sobrien return; 674090075Sobrien 674190075Sobrien /* Figure out the type of the VTT. */ 674290075Sobrien type = build_index_type (size_int (list_length (inits) - 1)); 674390075Sobrien type = build_cplus_array_type (const_ptr_type_node, type); 6744169689Skan 674590075Sobrien /* Now, build the VTT object itself. */ 6746169689Skan vtt = build_vtable (t, mangle_vtt_for_type (t), type); 6747169689Skan initialize_artificial_var (vtt, inits); 6748117395Skan /* Add the VTT to the vtables list. */ 6749117395Skan TREE_CHAIN (vtt) = TREE_CHAIN (CLASSTYPE_VTABLES (t)); 6750117395Skan TREE_CHAIN (CLASSTYPE_VTABLES (t)) = vtt; 675190075Sobrien 675290075Sobrien dump_vtt (t, vtt); 675390075Sobrien} 675490075Sobrien 675590075Sobrien/* When building a secondary VTT, BINFO_VTABLE is set to a TREE_LIST with 675690075Sobrien PURPOSE the RTTI_BINFO, VALUE the real vtable pointer for this binfo, 675790075Sobrien and CHAIN the vtable pointer for this binfo after construction is 6758117395Skan complete. VALUE can also be another BINFO, in which case we recurse. */ 675990075Sobrien 676090075Sobrienstatic tree 6761132718Skanbinfo_ctor_vtable (tree binfo) 676290075Sobrien{ 676390075Sobrien tree vt; 676490075Sobrien 676590075Sobrien while (1) 676690075Sobrien { 676790075Sobrien vt = BINFO_VTABLE (binfo); 676890075Sobrien if (TREE_CODE (vt) == TREE_LIST) 676990075Sobrien vt = TREE_VALUE (vt); 6770169689Skan if (TREE_CODE (vt) == TREE_BINFO) 677190075Sobrien binfo = vt; 677290075Sobrien else 677390075Sobrien break; 677490075Sobrien } 677590075Sobrien 677690075Sobrien return vt; 677790075Sobrien} 677890075Sobrien 6779169689Skan/* Data for secondary VTT initialization. */ 6780169689Skantypedef struct secondary_vptr_vtt_init_data_s 6781169689Skan{ 6782169689Skan /* Is this the primary VTT? */ 6783169689Skan bool top_level_p; 6784169689Skan 6785169689Skan /* Current index into the VTT. */ 6786169689Skan tree index; 6787169689Skan 6788169689Skan /* TREE_LIST of initializers built up. */ 6789169689Skan tree inits; 6790169689Skan 6791169689Skan /* The type being constructed by this secondary VTT. */ 6792169689Skan tree type_being_constructed; 6793169689Skan} secondary_vptr_vtt_init_data; 6794169689Skan 679590075Sobrien/* Recursively build the VTT-initializer for BINFO (which is in the 679690075Sobrien hierarchy dominated by T). INITS points to the end of the initializer 679790075Sobrien list to date. INDEX is the VTT index where the next element will be 679890075Sobrien replaced. Iff BINFO is the binfo for T, this is the top level VTT (i.e. 679990075Sobrien not a subvtt for some base of T). When that is so, we emit the sub-VTTs 680090075Sobrien for virtual bases of T. When it is not so, we build the constructor 680190075Sobrien vtables for the BINFO-in-T variant. */ 680290075Sobrien 680390075Sobrienstatic tree * 6804169689Skanbuild_vtt_inits (tree binfo, tree t, tree *inits, tree *index) 680590075Sobrien{ 680690075Sobrien int i; 680790075Sobrien tree b; 680890075Sobrien tree init; 680990075Sobrien tree secondary_vptrs; 6810169689Skan secondary_vptr_vtt_init_data data; 6811169689Skan int top_level_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t); 681290075Sobrien 681390075Sobrien /* We only need VTTs for subobjects with virtual bases. */ 6814169689Skan if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))) 681590075Sobrien return inits; 681690075Sobrien 681790075Sobrien /* We need to use a construction vtable if this is not the primary 681890075Sobrien VTT. */ 681990075Sobrien if (!top_level_p) 682090075Sobrien { 682190075Sobrien build_ctor_vtbl_group (binfo, t); 682290075Sobrien 682390075Sobrien /* Record the offset in the VTT where this sub-VTT can be found. */ 682490075Sobrien BINFO_SUBVTT_INDEX (binfo) = *index; 682590075Sobrien } 682690075Sobrien 682790075Sobrien /* Add the address of the primary vtable for the complete object. */ 682890075Sobrien init = binfo_ctor_vtable (binfo); 682990075Sobrien *inits = build_tree_list (NULL_TREE, init); 683090075Sobrien inits = &TREE_CHAIN (*inits); 683190075Sobrien if (top_level_p) 683290075Sobrien { 6833169689Skan gcc_assert (!BINFO_VPTR_INDEX (binfo)); 683490075Sobrien BINFO_VPTR_INDEX (binfo) = *index; 683590075Sobrien } 683690075Sobrien *index = size_binop (PLUS_EXPR, *index, TYPE_SIZE_UNIT (ptr_type_node)); 6837169689Skan 683890075Sobrien /* Recursively add the secondary VTTs for non-virtual bases. */ 6839169689Skan for (i = 0; BINFO_BASE_ITERATE (binfo, i, b); ++i) 6840169689Skan if (!BINFO_VIRTUAL_P (b)) 6841169689Skan inits = build_vtt_inits (b, t, inits, index); 6842169689Skan 684390075Sobrien /* Add secondary virtual pointers for all subobjects of BINFO with 684490075Sobrien either virtual bases or reachable along a virtual path, except 684590075Sobrien subobjects that are non-virtual primary bases. */ 6846169689Skan data.top_level_p = top_level_p; 6847169689Skan data.index = *index; 6848169689Skan data.inits = NULL; 6849169689Skan data.type_being_constructed = BINFO_TYPE (binfo); 685090075Sobrien 6851169689Skan dfs_walk_once (binfo, dfs_build_secondary_vptr_vtt_inits, NULL, &data); 685290075Sobrien 6853169689Skan *index = data.index; 6854169689Skan 685590075Sobrien /* The secondary vptrs come back in reverse order. After we reverse 685690075Sobrien them, and add the INITS, the last init will be the first element 685790075Sobrien of the chain. */ 6858169689Skan secondary_vptrs = data.inits; 685990075Sobrien if (secondary_vptrs) 686090075Sobrien { 686190075Sobrien *inits = nreverse (secondary_vptrs); 686290075Sobrien inits = &TREE_CHAIN (secondary_vptrs); 6863169689Skan gcc_assert (*inits == NULL_TREE); 686490075Sobrien } 686590075Sobrien 686690075Sobrien if (top_level_p) 6867169689Skan /* Add the secondary VTTs for virtual bases in inheritance graph 6868169689Skan order. */ 686990075Sobrien for (b = TYPE_BINFO (BINFO_TYPE (binfo)); b; b = TREE_CHAIN (b)) 687090075Sobrien { 6871169689Skan if (!BINFO_VIRTUAL_P (b)) 687290075Sobrien continue; 6873169689Skan 6874132718Skan inits = build_vtt_inits (b, t, inits, index); 687590075Sobrien } 6876169689Skan else 6877169689Skan /* Remove the ctor vtables we created. */ 6878169689Skan dfs_walk_all (binfo, dfs_fixup_binfo_vtbls, NULL, binfo); 687990075Sobrien 688090075Sobrien return inits; 688190075Sobrien} 688290075Sobrien 6883169689Skan/* Called from build_vtt_inits via dfs_walk. BINFO is the binfo for the base 6884169689Skan in most derived. DATA is a SECONDARY_VPTR_VTT_INIT_DATA structure. */ 688590075Sobrien 688690075Sobrienstatic tree 6887169689Skandfs_build_secondary_vptr_vtt_inits (tree binfo, void *data_) 688890075Sobrien{ 6889169689Skan secondary_vptr_vtt_init_data *data = (secondary_vptr_vtt_init_data *)data_; 689090075Sobrien 689190075Sobrien /* We don't care about bases that don't have vtables. */ 689290075Sobrien if (!TYPE_VFIELD (BINFO_TYPE (binfo))) 6893169689Skan return dfs_skip_bases; 689490075Sobrien 6895169689Skan /* We're only interested in proper subobjects of the type being 6896169689Skan constructed. */ 6897169689Skan if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), data->type_being_constructed)) 689890075Sobrien return NULL_TREE; 689990075Sobrien 6900169689Skan /* We're only interested in bases with virtual bases or reachable 6901169689Skan via a virtual path from the type being constructed. */ 6902169689Skan if (!(CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)) 6903169689Skan || binfo_via_virtual (binfo, data->type_being_constructed))) 6904169689Skan return dfs_skip_bases; 6905169689Skan 690690075Sobrien /* We're not interested in non-virtual primary bases. */ 6907169689Skan if (!BINFO_VIRTUAL_P (binfo) && BINFO_PRIMARY_P (binfo)) 690890075Sobrien return NULL_TREE; 690990075Sobrien 691090075Sobrien /* Record the index where this secondary vptr can be found. */ 6911169689Skan if (data->top_level_p) 691290075Sobrien { 6913169689Skan gcc_assert (!BINFO_VPTR_INDEX (binfo)); 6914169689Skan BINFO_VPTR_INDEX (binfo) = data->index; 6915169689Skan 6916169689Skan if (BINFO_VIRTUAL_P (binfo)) 6917169689Skan { 6918169689Skan /* It's a primary virtual base, and this is not a 6919169689Skan construction vtable. Find the base this is primary of in 6920169689Skan the inheritance graph, and use that base's vtable 6921169689Skan now. */ 6922169689Skan while (BINFO_PRIMARY_P (binfo)) 6923169689Skan binfo = BINFO_INHERITANCE_CHAIN (binfo); 6924169689Skan } 692590075Sobrien } 692690075Sobrien 692790075Sobrien /* Add the initializer for the secondary vptr itself. */ 6928169689Skan data->inits = tree_cons (NULL_TREE, binfo_ctor_vtable (binfo), data->inits); 692990075Sobrien 6930169689Skan /* Advance the vtt index. */ 6931169689Skan data->index = size_binop (PLUS_EXPR, data->index, 6932169689Skan TYPE_SIZE_UNIT (ptr_type_node)); 6933169689Skan 693490075Sobrien return NULL_TREE; 693590075Sobrien} 693690075Sobrien 6937169689Skan/* Called from build_vtt_inits via dfs_walk. After building 6938169689Skan constructor vtables and generating the sub-vtt from them, we need 6939169689Skan to restore the BINFO_VTABLES that were scribbled on. DATA is the 6940169689Skan binfo of the base whose sub vtt was generated. */ 694190075Sobrien 694290075Sobrienstatic tree 6943169689Skandfs_fixup_binfo_vtbls (tree binfo, void* data) 694490075Sobrien{ 6945169689Skan tree vtable = BINFO_VTABLE (binfo); 694690075Sobrien 6947169689Skan if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo))) 6948169689Skan /* If this class has no vtable, none of its bases do. */ 6949169689Skan return dfs_skip_bases; 695090075Sobrien 6951169689Skan if (!vtable) 6952169689Skan /* This might be a primary base, so have no vtable in this 6953169689Skan hierarchy. */ 695490075Sobrien return NULL_TREE; 695590075Sobrien 695690075Sobrien /* If we scribbled the construction vtable vptr into BINFO, clear it 695790075Sobrien out now. */ 6958169689Skan if (TREE_CODE (vtable) == TREE_LIST 6959169689Skan && (TREE_PURPOSE (vtable) == (tree) data)) 6960169689Skan BINFO_VTABLE (binfo) = TREE_CHAIN (vtable); 696190075Sobrien 696290075Sobrien return NULL_TREE; 696390075Sobrien} 696490075Sobrien 696590075Sobrien/* Build the construction vtable group for BINFO which is in the 696690075Sobrien hierarchy dominated by T. */ 696790075Sobrien 696890075Sobrienstatic void 6969132718Skanbuild_ctor_vtbl_group (tree binfo, tree t) 697090075Sobrien{ 697190075Sobrien tree list; 697290075Sobrien tree type; 697390075Sobrien tree vtbl; 697490075Sobrien tree inits; 697590075Sobrien tree id; 697690075Sobrien tree vbase; 697790075Sobrien 697890075Sobrien /* See if we've already created this construction vtable group. */ 697990075Sobrien id = mangle_ctor_vtbl_for_type (t, binfo); 698090075Sobrien if (IDENTIFIER_GLOBAL_VALUE (id)) 698190075Sobrien return; 698290075Sobrien 6983169689Skan gcc_assert (!SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t)); 698490075Sobrien /* Build a version of VTBL (with the wrong type) for use in 698590075Sobrien constructing the addresses of secondary vtables in the 698690075Sobrien construction vtable group. */ 698790075Sobrien vtbl = build_vtable (t, id, ptr_type_node); 6988169689Skan DECL_CONSTRUCTION_VTABLE_P (vtbl) = 1; 698990075Sobrien list = build_tree_list (vtbl, NULL_TREE); 699090075Sobrien accumulate_vtbl_inits (binfo, TYPE_BINFO (TREE_TYPE (binfo)), 699190075Sobrien binfo, t, list); 699290075Sobrien 699390075Sobrien /* Add the vtables for each of our virtual bases using the vbase in T 699490075Sobrien binfo. */ 6995169689Skan for (vbase = TYPE_BINFO (BINFO_TYPE (binfo)); 6996169689Skan vbase; 699790075Sobrien vbase = TREE_CHAIN (vbase)) 699890075Sobrien { 699990075Sobrien tree b; 700090075Sobrien 7001169689Skan if (!BINFO_VIRTUAL_P (vbase)) 700290075Sobrien continue; 7003132718Skan b = copied_binfo (vbase, binfo); 7004169689Skan 7005132718Skan accumulate_vtbl_inits (b, vbase, binfo, t, list); 700690075Sobrien } 700790075Sobrien inits = TREE_VALUE (list); 700890075Sobrien 700990075Sobrien /* Figure out the type of the construction vtable. */ 701090075Sobrien type = build_index_type (size_int (list_length (inits) - 1)); 701190075Sobrien type = build_cplus_array_type (vtable_entry_type, type); 701290075Sobrien TREE_TYPE (vtbl) = type; 701390075Sobrien 701490075Sobrien /* Initialize the construction vtable. */ 7015117395Skan CLASSTYPE_VTABLES (t) = chainon (CLASSTYPE_VTABLES (t), vtbl); 7016169689Skan initialize_artificial_var (vtbl, inits); 701790075Sobrien dump_vtable (t, binfo, vtbl); 701890075Sobrien} 701990075Sobrien 702090075Sobrien/* Add the vtbl initializers for BINFO (and its bases other than 702190075Sobrien non-virtual primaries) to the list of INITS. BINFO is in the 702290075Sobrien hierarchy dominated by T. RTTI_BINFO is the binfo within T of 702390075Sobrien the constructor the vtbl inits should be accumulated for. (If this 702490075Sobrien is the complete object vtbl then RTTI_BINFO will be TYPE_BINFO (T).) 702590075Sobrien ORIG_BINFO is the binfo for this object within BINFO_TYPE (RTTI_BINFO). 702690075Sobrien BINFO is the active base equivalent of ORIG_BINFO in the inheritance 702790075Sobrien graph of T. Both BINFO and ORIG_BINFO will have the same BINFO_TYPE, 702890075Sobrien but are not necessarily the same in terms of layout. */ 702990075Sobrien 703090075Sobrienstatic void 7031132718Skanaccumulate_vtbl_inits (tree binfo, 7032169689Skan tree orig_binfo, 7033169689Skan tree rtti_binfo, 7034169689Skan tree t, 7035169689Skan tree inits) 703690075Sobrien{ 703790075Sobrien int i; 7038169689Skan tree base_binfo; 7039169689Skan int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t); 704090075Sobrien 7041169689Skan gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (orig_binfo))); 704290075Sobrien 7043117395Skan /* If it doesn't have a vptr, we don't do anything. */ 704490075Sobrien if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo))) 704590075Sobrien return; 7046169689Skan 704790075Sobrien /* If we're building a construction vtable, we're not interested in 704890075Sobrien subobjects that don't require construction vtables. */ 7049169689Skan if (ctor_vtbl_p 7050169689Skan && !CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)) 705190075Sobrien && !binfo_via_virtual (orig_binfo, BINFO_TYPE (rtti_binfo))) 705290075Sobrien return; 705390075Sobrien 705490075Sobrien /* Build the initializers for the BINFO-in-T vtable. */ 7055169689Skan TREE_VALUE (inits) 705690075Sobrien = chainon (TREE_VALUE (inits), 705790075Sobrien dfs_accumulate_vtbl_inits (binfo, orig_binfo, 705890075Sobrien rtti_binfo, t, inits)); 7059169689Skan 706090075Sobrien /* Walk the BINFO and its bases. We walk in preorder so that as we 706190075Sobrien initialize each vtable we can figure out at what offset the 706290075Sobrien secondary vtable lies from the primary vtable. We can't use 706390075Sobrien dfs_walk here because we need to iterate through bases of BINFO 706490075Sobrien and RTTI_BINFO simultaneously. */ 7065169689Skan for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i) 706690075Sobrien { 706790075Sobrien /* Skip virtual bases. */ 7068169689Skan if (BINFO_VIRTUAL_P (base_binfo)) 706990075Sobrien continue; 707090075Sobrien accumulate_vtbl_inits (base_binfo, 7071169689Skan BINFO_BASE_BINFO (orig_binfo, i), 707290075Sobrien rtti_binfo, t, 707390075Sobrien inits); 707490075Sobrien } 707590075Sobrien} 707690075Sobrien 707790075Sobrien/* Called from accumulate_vtbl_inits. Returns the initializers for 707890075Sobrien the BINFO vtable. */ 707990075Sobrien 708090075Sobrienstatic tree 7081132718Skandfs_accumulate_vtbl_inits (tree binfo, 7082169689Skan tree orig_binfo, 7083169689Skan tree rtti_binfo, 7084169689Skan tree t, 7085169689Skan tree l) 708690075Sobrien{ 708790075Sobrien tree inits = NULL_TREE; 708890075Sobrien tree vtbl = NULL_TREE; 7089169689Skan int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t); 709090075Sobrien 709190075Sobrien if (ctor_vtbl_p 7092169689Skan && BINFO_VIRTUAL_P (orig_binfo) && BINFO_PRIMARY_P (orig_binfo)) 709390075Sobrien { 709490075Sobrien /* In the hierarchy of BINFO_TYPE (RTTI_BINFO), this is a 709590075Sobrien primary virtual base. If it is not the same primary in 709690075Sobrien the hierarchy of T, we'll need to generate a ctor vtable 709790075Sobrien for it, to place at its location in T. If it is the same 709890075Sobrien primary, we still need a VTT entry for the vtable, but it 709990075Sobrien should point to the ctor vtable for the base it is a 710090075Sobrien primary for within the sub-hierarchy of RTTI_BINFO. 7101169689Skan 710290075Sobrien There are three possible cases: 7103169689Skan 710490075Sobrien 1) We are in the same place. 710590075Sobrien 2) We are a primary base within a lost primary virtual base of 710690075Sobrien RTTI_BINFO. 710790075Sobrien 3) We are primary to something not a base of RTTI_BINFO. */ 7108169689Skan 7109169689Skan tree b; 711090075Sobrien tree last = NULL_TREE; 711190075Sobrien 711290075Sobrien /* First, look through the bases we are primary to for RTTI_BINFO 711390075Sobrien or a virtual base. */ 7114169689Skan b = binfo; 7115169689Skan while (BINFO_PRIMARY_P (b)) 711690075Sobrien { 7117169689Skan b = BINFO_INHERITANCE_CHAIN (b); 711890075Sobrien last = b; 7119169689Skan if (BINFO_VIRTUAL_P (b) || b == rtti_binfo) 7120169689Skan goto found; 712190075Sobrien } 712290075Sobrien /* If we run out of primary links, keep looking down our 712390075Sobrien inheritance chain; we might be an indirect primary. */ 7124169689Skan for (b = last; b; b = BINFO_INHERITANCE_CHAIN (b)) 7125169689Skan if (BINFO_VIRTUAL_P (b) || b == rtti_binfo) 7126169689Skan break; 7127169689Skan found: 712890075Sobrien 712990075Sobrien /* If we found RTTI_BINFO, this is case 1. If we found a virtual 713090075Sobrien base B and it is a base of RTTI_BINFO, this is case 2. In 713190075Sobrien either case, we share our vtable with LAST, i.e. the 713290075Sobrien derived-most base within B of which we are a primary. */ 713390075Sobrien if (b == rtti_binfo 7134169689Skan || (b && binfo_for_vbase (BINFO_TYPE (b), BINFO_TYPE (rtti_binfo)))) 713590075Sobrien /* Just set our BINFO_VTABLE to point to LAST, as we may not have 713690075Sobrien set LAST's BINFO_VTABLE yet. We'll extract the actual vptr in 713790075Sobrien binfo_ctor_vtable after everything's been set up. */ 713890075Sobrien vtbl = last; 713990075Sobrien 714090075Sobrien /* Otherwise, this is case 3 and we get our own. */ 714190075Sobrien } 7142132718Skan else if (!BINFO_NEW_VTABLE_MARKED (orig_binfo)) 714390075Sobrien return inits; 714490075Sobrien 714590075Sobrien if (!vtbl) 714690075Sobrien { 714790075Sobrien tree index; 714890075Sobrien int non_fn_entries; 714990075Sobrien 715090075Sobrien /* Compute the initializer for this vtable. */ 715190075Sobrien inits = build_vtbl_initializer (binfo, orig_binfo, t, rtti_binfo, 715290075Sobrien &non_fn_entries); 715390075Sobrien 715490075Sobrien /* Figure out the position to which the VPTR should point. */ 715590075Sobrien vtbl = TREE_PURPOSE (l); 7156169689Skan vtbl = build_address (vtbl); 7157169689Skan /* ??? We should call fold_convert to convert the address to 7158169689Skan vtbl_ptr_type_node, which is the type of elements in the 7159169689Skan vtable. However, the resulting NOP_EXPRs confuse other parts 7160169689Skan of the C++ front end. */ 7161169689Skan gcc_assert (TREE_CODE (vtbl) == ADDR_EXPR); 7162169689Skan TREE_TYPE (vtbl) = vtbl_ptr_type_node; 716390075Sobrien index = size_binop (PLUS_EXPR, 716490075Sobrien size_int (non_fn_entries), 716590075Sobrien size_int (list_length (TREE_VALUE (l)))); 716690075Sobrien index = size_binop (MULT_EXPR, 716790075Sobrien TYPE_SIZE_UNIT (vtable_entry_type), 716890075Sobrien index); 7169169689Skan vtbl = build2 (PLUS_EXPR, TREE_TYPE (vtbl), vtbl, index); 717090075Sobrien } 717190075Sobrien 717290075Sobrien if (ctor_vtbl_p) 717390075Sobrien /* For a construction vtable, we can't overwrite BINFO_VTABLE. 717490075Sobrien So, we make a TREE_LIST. Later, dfs_fixup_binfo_vtbls will 717590075Sobrien straighten this out. */ 717690075Sobrien BINFO_VTABLE (binfo) = tree_cons (rtti_binfo, vtbl, BINFO_VTABLE (binfo)); 7177169689Skan else if (BINFO_PRIMARY_P (binfo) && BINFO_VIRTUAL_P (binfo)) 717890075Sobrien inits = NULL_TREE; 717990075Sobrien else 718090075Sobrien /* For an ordinary vtable, set BINFO_VTABLE. */ 718190075Sobrien BINFO_VTABLE (binfo) = vtbl; 718290075Sobrien 718390075Sobrien return inits; 718490075Sobrien} 718590075Sobrien 7186169689Skanstatic GTY(()) tree abort_fndecl_addr; 7187169689Skan 718890075Sobrien/* Construct the initializer for BINFO's virtual function table. BINFO 718990075Sobrien is part of the hierarchy dominated by T. If we're building a 719090075Sobrien construction vtable, the ORIG_BINFO is the binfo we should use to 719190075Sobrien find the actual function pointers to put in the vtable - but they 719290075Sobrien can be overridden on the path to most-derived in the graph that 719390075Sobrien ORIG_BINFO belongs. Otherwise, 719490075Sobrien ORIG_BINFO should be the same as BINFO. The RTTI_BINFO is the 719590075Sobrien BINFO that should be indicated by the RTTI information in the 719690075Sobrien vtable; it will be a base class of T, rather than T itself, if we 719790075Sobrien are building a construction vtable. 719890075Sobrien 719990075Sobrien The value returned is a TREE_LIST suitable for wrapping in a 720090075Sobrien CONSTRUCTOR to use as the DECL_INITIAL for a vtable. If 720190075Sobrien NON_FN_ENTRIES_P is not NULL, *NON_FN_ENTRIES_P is set to the 7202169689Skan number of non-function entries in the vtable. 720390075Sobrien 720490075Sobrien It might seem that this function should never be called with a 720590075Sobrien BINFO for which BINFO_PRIMARY_P holds, the vtable for such a 720690075Sobrien base is always subsumed by a derived class vtable. However, when 720790075Sobrien we are building construction vtables, we do build vtables for 720890075Sobrien primary bases; we need these while the primary base is being 720990075Sobrien constructed. */ 721090075Sobrien 721190075Sobrienstatic tree 7212132718Skanbuild_vtbl_initializer (tree binfo, 7213169689Skan tree orig_binfo, 7214169689Skan tree t, 7215169689Skan tree rtti_binfo, 7216169689Skan int* non_fn_entries_p) 721790075Sobrien{ 721890075Sobrien tree v, b; 721990075Sobrien tree vfun_inits; 722090075Sobrien vtbl_init_data vid; 7221169689Skan unsigned ix; 7222169689Skan tree vbinfo; 7223169689Skan VEC(tree,gc) *vbases; 722490075Sobrien 722590075Sobrien /* Initialize VID. */ 722690075Sobrien memset (&vid, 0, sizeof (vid)); 722790075Sobrien vid.binfo = binfo; 722890075Sobrien vid.derived = t; 722990075Sobrien vid.rtti_binfo = rtti_binfo; 723090075Sobrien vid.last_init = &vid.inits; 7231169689Skan vid.primary_vtbl_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t); 7232169689Skan vid.ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t); 7233117395Skan vid.generate_vcall_entries = true; 723490075Sobrien /* The first vbase or vcall offset is at index -3 in the vtable. */ 7235169689Skan vid.index = ssize_int(-3 * TARGET_VTABLE_DATA_ENTRY_DISTANCE); 723690075Sobrien 723790075Sobrien /* Add entries to the vtable for RTTI. */ 723890075Sobrien build_rtti_vtbl_entries (binfo, &vid); 723990075Sobrien 724090075Sobrien /* Create an array for keeping track of the functions we've 724190075Sobrien processed. When we see multiple functions with the same 724290075Sobrien signature, we share the vcall offsets. */ 7243169689Skan vid.fns = VEC_alloc (tree, gc, 32); 724490075Sobrien /* Add the vcall and vbase offset entries. */ 724590075Sobrien build_vcall_and_vbase_vtbl_entries (binfo, &vid); 7246169689Skan 724790075Sobrien /* Clear BINFO_VTABLE_PATH_MARKED; it's set by 724890075Sobrien build_vbase_offset_vtbl_entries. */ 7249169689Skan for (vbases = CLASSTYPE_VBASECLASSES (t), ix = 0; 7250169689Skan VEC_iterate (tree, vbases, ix, vbinfo); ix++) 7251169689Skan BINFO_VTABLE_PATH_MARKED (vbinfo) = 0; 725290075Sobrien 7253117395Skan /* If the target requires padding between data entries, add that now. */ 7254117395Skan if (TARGET_VTABLE_DATA_ENTRY_DISTANCE > 1) 7255117395Skan { 7256117395Skan tree cur, *prev; 7257117395Skan 7258117395Skan for (prev = &vid.inits; (cur = *prev); prev = &TREE_CHAIN (cur)) 7259117395Skan { 7260117395Skan tree add = cur; 7261117395Skan int i; 7262117395Skan 7263117395Skan for (i = 1; i < TARGET_VTABLE_DATA_ENTRY_DISTANCE; ++i) 7264117395Skan add = tree_cons (NULL_TREE, 7265117395Skan build1 (NOP_EXPR, vtable_entry_type, 7266117395Skan null_pointer_node), 7267117395Skan add); 7268117395Skan *prev = add; 7269117395Skan } 7270117395Skan } 7271117395Skan 727290075Sobrien if (non_fn_entries_p) 727390075Sobrien *non_fn_entries_p = list_length (vid.inits); 727490075Sobrien 727590075Sobrien /* Go through all the ordinary virtual functions, building up 727690075Sobrien initializers. */ 727790075Sobrien vfun_inits = NULL_TREE; 727890075Sobrien for (v = BINFO_VIRTUALS (orig_binfo); v; v = TREE_CHAIN (v)) 727990075Sobrien { 728090075Sobrien tree delta; 728190075Sobrien tree vcall_index; 7282132718Skan tree fn, fn_original; 728390075Sobrien tree init = NULL_TREE; 7284169689Skan 728590075Sobrien fn = BV_FN (v); 7286132718Skan fn_original = fn; 7287132718Skan if (DECL_THUNK_P (fn)) 7288132718Skan { 7289132718Skan if (!DECL_NAME (fn)) 7290132718Skan finish_thunk (fn); 7291132718Skan if (THUNK_ALIAS (fn)) 7292132718Skan { 7293132718Skan fn = THUNK_ALIAS (fn); 7294132718Skan BV_FN (v) = fn; 7295132718Skan } 7296132718Skan fn_original = THUNK_TARGET (fn); 7297132718Skan } 7298169689Skan 729990075Sobrien /* If the only definition of this function signature along our 730090075Sobrien primary base chain is from a lost primary, this vtable slot will 730190075Sobrien never be used, so just zero it out. This is important to avoid 730290075Sobrien requiring extra thunks which cannot be generated with the function. 730390075Sobrien 730490075Sobrien We first check this in update_vtable_entry_for_fn, so we handle 730590075Sobrien restored primary bases properly; we also need to do it here so we 730690075Sobrien zero out unused slots in ctor vtables, rather than filling themff 730790075Sobrien with erroneous values (though harmless, apart from relocation 730890075Sobrien costs). */ 730990075Sobrien for (b = binfo; ; b = get_primary_binfo (b)) 731090075Sobrien { 731190075Sobrien /* We found a defn before a lost primary; go ahead as normal. */ 7312132718Skan if (look_for_overrides_here (BINFO_TYPE (b), fn_original)) 731390075Sobrien break; 731490075Sobrien 731590075Sobrien /* The nearest definition is from a lost primary; clear the 731690075Sobrien slot. */ 731790075Sobrien if (BINFO_LOST_PRIMARY_P (b)) 731890075Sobrien { 731990075Sobrien init = size_zero_node; 732090075Sobrien break; 732190075Sobrien } 732290075Sobrien } 732390075Sobrien 732490075Sobrien if (! init) 732590075Sobrien { 732690075Sobrien /* Pull the offset for `this', and the function to call, out of 732790075Sobrien the list. */ 732890075Sobrien delta = BV_DELTA (v); 7329117395Skan vcall_index = BV_VCALL_INDEX (v); 733090075Sobrien 7331169689Skan gcc_assert (TREE_CODE (delta) == INTEGER_CST); 7332169689Skan gcc_assert (TREE_CODE (fn) == FUNCTION_DECL); 733390075Sobrien 733490075Sobrien /* You can't call an abstract virtual function; it's abstract. 733590075Sobrien So, we replace these functions with __pure_virtual. */ 7336132718Skan if (DECL_PURE_VIRTUAL_P (fn_original)) 7337132718Skan { 7338169689Skan fn = abort_fndecl; 7339169689Skan if (abort_fndecl_addr == NULL) 7340169689Skan abort_fndecl_addr = build1 (ADDR_EXPR, vfunc_ptr_type_node, fn); 7341169689Skan init = abort_fndecl_addr; 7342132718Skan } 7343169689Skan else 7344169689Skan { 7345169689Skan if (!integer_zerop (delta) || vcall_index) 7346169689Skan { 7347169689Skan fn = make_thunk (fn, /*this_adjusting=*/1, delta, vcall_index); 7348169689Skan if (!DECL_NAME (fn)) 7349169689Skan finish_thunk (fn); 7350169689Skan } 7351169689Skan /* Take the address of the function, considering it to be of an 7352169689Skan appropriate generic type. */ 7353169689Skan init = build1 (ADDR_EXPR, vfunc_ptr_type_node, fn); 7354169689Skan } 735590075Sobrien } 735690075Sobrien 735790075Sobrien /* And add it to the chain of initializers. */ 735890075Sobrien if (TARGET_VTABLE_USES_DESCRIPTORS) 735990075Sobrien { 736090075Sobrien int i; 736190075Sobrien if (init == size_zero_node) 736290075Sobrien for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i) 736390075Sobrien vfun_inits = tree_cons (NULL_TREE, init, vfun_inits); 736490075Sobrien else 736590075Sobrien for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i) 736690075Sobrien { 7367169689Skan tree fdesc = build2 (FDESC_EXPR, vfunc_ptr_type_node, 7368169689Skan TREE_OPERAND (init, 0), 7369169689Skan build_int_cst (NULL_TREE, i)); 737090075Sobrien TREE_CONSTANT (fdesc) = 1; 7371169689Skan TREE_INVARIANT (fdesc) = 1; 737290075Sobrien 737390075Sobrien vfun_inits = tree_cons (NULL_TREE, fdesc, vfun_inits); 737490075Sobrien } 737590075Sobrien } 737690075Sobrien else 7377169689Skan vfun_inits = tree_cons (NULL_TREE, init, vfun_inits); 737890075Sobrien } 737990075Sobrien 738090075Sobrien /* The initializers for virtual functions were built up in reverse 738190075Sobrien order; straighten them out now. */ 738290075Sobrien vfun_inits = nreverse (vfun_inits); 7383169689Skan 738490075Sobrien /* The negative offset initializers are also in reverse order. */ 738590075Sobrien vid.inits = nreverse (vid.inits); 738690075Sobrien 738790075Sobrien /* Chain the two together. */ 738890075Sobrien return chainon (vid.inits, vfun_inits); 738990075Sobrien} 739090075Sobrien 739190075Sobrien/* Adds to vid->inits the initializers for the vbase and vcall 739290075Sobrien offsets in BINFO, which is in the hierarchy dominated by T. */ 739390075Sobrien 739490075Sobrienstatic void 7395132718Skanbuild_vcall_and_vbase_vtbl_entries (tree binfo, vtbl_init_data* vid) 739690075Sobrien{ 739790075Sobrien tree b; 739890075Sobrien 739990075Sobrien /* If this is a derived class, we must first create entries 740090075Sobrien corresponding to the primary base class. */ 740190075Sobrien b = get_primary_binfo (binfo); 740290075Sobrien if (b) 740390075Sobrien build_vcall_and_vbase_vtbl_entries (b, vid); 740490075Sobrien 740590075Sobrien /* Add the vbase entries for this base. */ 740690075Sobrien build_vbase_offset_vtbl_entries (binfo, vid); 740790075Sobrien /* Add the vcall entries for this base. */ 740890075Sobrien build_vcall_offset_vtbl_entries (binfo, vid); 740990075Sobrien} 741090075Sobrien 741190075Sobrien/* Returns the initializers for the vbase offset entries in the vtable 741290075Sobrien for BINFO (which is part of the class hierarchy dominated by T), in 741390075Sobrien reverse order. VBASE_OFFSET_INDEX gives the vtable index 741490075Sobrien where the next vbase offset will go. */ 741590075Sobrien 741690075Sobrienstatic void 7417132718Skanbuild_vbase_offset_vtbl_entries (tree binfo, vtbl_init_data* vid) 741890075Sobrien{ 741990075Sobrien tree vbase; 742090075Sobrien tree t; 742190075Sobrien tree non_primary_binfo; 742290075Sobrien 742390075Sobrien /* If there are no virtual baseclasses, then there is nothing to 742490075Sobrien do. */ 7425169689Skan if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))) 742690075Sobrien return; 742790075Sobrien 742890075Sobrien t = vid->derived; 7429169689Skan 743090075Sobrien /* We might be a primary base class. Go up the inheritance hierarchy 743190075Sobrien until we find the most derived class of which we are a primary base: 743290075Sobrien it is the offset of that which we need to use. */ 743390075Sobrien non_primary_binfo = binfo; 743490075Sobrien while (BINFO_INHERITANCE_CHAIN (non_primary_binfo)) 743590075Sobrien { 743690075Sobrien tree b; 743790075Sobrien 743890075Sobrien /* If we have reached a virtual base, then it must be a primary 743990075Sobrien base (possibly multi-level) of vid->binfo, or we wouldn't 744090075Sobrien have called build_vcall_and_vbase_vtbl_entries for it. But it 744190075Sobrien might be a lost primary, so just skip down to vid->binfo. */ 7442169689Skan if (BINFO_VIRTUAL_P (non_primary_binfo)) 744390075Sobrien { 744490075Sobrien non_primary_binfo = vid->binfo; 744590075Sobrien break; 744690075Sobrien } 744790075Sobrien 744890075Sobrien b = BINFO_INHERITANCE_CHAIN (non_primary_binfo); 744990075Sobrien if (get_primary_binfo (b) != non_primary_binfo) 745090075Sobrien break; 745190075Sobrien non_primary_binfo = b; 745290075Sobrien } 745390075Sobrien 745490075Sobrien /* Go through the virtual bases, adding the offsets. */ 745590075Sobrien for (vbase = TYPE_BINFO (BINFO_TYPE (binfo)); 745690075Sobrien vbase; 745790075Sobrien vbase = TREE_CHAIN (vbase)) 745890075Sobrien { 745990075Sobrien tree b; 746090075Sobrien tree delta; 7461169689Skan 7462169689Skan if (!BINFO_VIRTUAL_P (vbase)) 746390075Sobrien continue; 746490075Sobrien 746590075Sobrien /* Find the instance of this virtual base in the complete 746690075Sobrien object. */ 7467132718Skan b = copied_binfo (vbase, binfo); 746890075Sobrien 746990075Sobrien /* If we've already got an offset for this virtual base, we 747090075Sobrien don't need another one. */ 747190075Sobrien if (BINFO_VTABLE_PATH_MARKED (b)) 747290075Sobrien continue; 7473132718Skan BINFO_VTABLE_PATH_MARKED (b) = 1; 747490075Sobrien 747590075Sobrien /* Figure out where we can find this vbase offset. */ 7476169689Skan delta = size_binop (MULT_EXPR, 747790075Sobrien vid->index, 747890075Sobrien convert (ssizetype, 747990075Sobrien TYPE_SIZE_UNIT (vtable_entry_type))); 748090075Sobrien if (vid->primary_vtbl_p) 748190075Sobrien BINFO_VPTR_FIELD (b) = delta; 748290075Sobrien 748390075Sobrien if (binfo != TYPE_BINFO (t)) 7484169689Skan /* The vbase offset had better be the same. */ 7485169689Skan gcc_assert (tree_int_cst_equal (delta, BINFO_VPTR_FIELD (vbase))); 748690075Sobrien 748790075Sobrien /* The next vbase will come at a more negative offset. */ 7488117395Skan vid->index = size_binop (MINUS_EXPR, vid->index, 7489117395Skan ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE)); 749090075Sobrien 749190075Sobrien /* The initializer is the delta from BINFO to this virtual base. 749290075Sobrien The vbase offsets go in reverse inheritance-graph order, and 749390075Sobrien we are walking in inheritance graph order so these end up in 749490075Sobrien the right order. */ 749590075Sobrien delta = size_diffop (BINFO_OFFSET (b), BINFO_OFFSET (non_primary_binfo)); 7496169689Skan 7497169689Skan *vid->last_init 749890075Sobrien = build_tree_list (NULL_TREE, 7499169689Skan fold_build1 (NOP_EXPR, 7500169689Skan vtable_entry_type, 7501169689Skan delta)); 750290075Sobrien vid->last_init = &TREE_CHAIN (*vid->last_init); 750390075Sobrien } 750490075Sobrien} 750590075Sobrien 750690075Sobrien/* Adds the initializers for the vcall offset entries in the vtable 750790075Sobrien for BINFO (which is part of the class hierarchy dominated by VID->DERIVED) 750890075Sobrien to VID->INITS. */ 750990075Sobrien 751090075Sobrienstatic void 7511132718Skanbuild_vcall_offset_vtbl_entries (tree binfo, vtbl_init_data* vid) 751290075Sobrien{ 7513117395Skan /* We only need these entries if this base is a virtual base. We 7514117395Skan compute the indices -- but do not add to the vtable -- when 7515117395Skan building the main vtable for a class. */ 7516169689Skan if (BINFO_VIRTUAL_P (binfo) || binfo == TYPE_BINFO (vid->derived)) 7517117395Skan { 7518117395Skan /* We need a vcall offset for each of the virtual functions in this 7519117395Skan vtable. For example: 752090075Sobrien 7521117395Skan class A { virtual void f (); }; 7522117395Skan class B1 : virtual public A { virtual void f (); }; 7523117395Skan class B2 : virtual public A { virtual void f (); }; 7524117395Skan class C: public B1, public B2 { virtual void f (); }; 752590075Sobrien 7526117395Skan A C object has a primary base of B1, which has a primary base of A. A 7527117395Skan C also has a secondary base of B2, which no longer has a primary base 7528117395Skan of A. So the B2-in-C construction vtable needs a secondary vtable for 7529117395Skan A, which will adjust the A* to a B2* to call f. We have no way of 7530117395Skan knowing what (or even whether) this offset will be when we define B2, 7531117395Skan so we store this "vcall offset" in the A sub-vtable and look it up in 7532117395Skan a "virtual thunk" for B2::f. 753390075Sobrien 7534117395Skan We need entries for all the functions in our primary vtable and 7535117395Skan in our non-virtual bases' secondary vtables. */ 7536117395Skan vid->vbase = binfo; 7537117395Skan /* If we are just computing the vcall indices -- but do not need 7538117395Skan the actual entries -- not that. */ 7539169689Skan if (!BINFO_VIRTUAL_P (binfo)) 7540117395Skan vid->generate_vcall_entries = false; 7541117395Skan /* Now, walk through the non-virtual bases, adding vcall offsets. */ 7542117395Skan add_vcall_offset_vtbl_entries_r (binfo, vid); 7543117395Skan } 754490075Sobrien} 754590075Sobrien 754690075Sobrien/* Build vcall offsets, starting with those for BINFO. */ 754790075Sobrien 754890075Sobrienstatic void 7549132718Skanadd_vcall_offset_vtbl_entries_r (tree binfo, vtbl_init_data* vid) 755090075Sobrien{ 755190075Sobrien int i; 755290075Sobrien tree primary_binfo; 7553169689Skan tree base_binfo; 755490075Sobrien 755590075Sobrien /* Don't walk into virtual bases -- except, of course, for the 755690075Sobrien virtual base for which we are building vcall offsets. Any 755790075Sobrien primary virtual base will have already had its offsets generated 755890075Sobrien through the recursion in build_vcall_and_vbase_vtbl_entries. */ 7559169689Skan if (BINFO_VIRTUAL_P (binfo) && vid->vbase != binfo) 756090075Sobrien return; 7561169689Skan 756290075Sobrien /* If BINFO has a primary base, process it first. */ 756390075Sobrien primary_binfo = get_primary_binfo (binfo); 756490075Sobrien if (primary_binfo) 756590075Sobrien add_vcall_offset_vtbl_entries_r (primary_binfo, vid); 756690075Sobrien 756790075Sobrien /* Add BINFO itself to the list. */ 756890075Sobrien add_vcall_offset_vtbl_entries_1 (binfo, vid); 756990075Sobrien 757090075Sobrien /* Scan the non-primary bases of BINFO. */ 7571169689Skan for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i) 7572169689Skan if (base_binfo != primary_binfo) 7573169689Skan add_vcall_offset_vtbl_entries_r (base_binfo, vid); 757490075Sobrien} 757590075Sobrien 757690075Sobrien/* Called from build_vcall_offset_vtbl_entries_r. */ 757790075Sobrien 757890075Sobrienstatic void 7579132718Skanadd_vcall_offset_vtbl_entries_1 (tree binfo, vtbl_init_data* vid) 758090075Sobrien{ 7581117395Skan /* Make entries for the rest of the virtuals. */ 7582117395Skan if (abi_version_at_least (2)) 7583117395Skan { 7584117395Skan tree orig_fn; 758590075Sobrien 7586117395Skan /* The ABI requires that the methods be processed in declaration 7587117395Skan order. G++ 3.2 used the order in the vtable. */ 7588117395Skan for (orig_fn = TYPE_METHODS (BINFO_TYPE (binfo)); 7589117395Skan orig_fn; 7590117395Skan orig_fn = TREE_CHAIN (orig_fn)) 7591117395Skan if (DECL_VINDEX (orig_fn)) 7592117395Skan add_vcall_offset (orig_fn, binfo, vid); 7593117395Skan } 7594117395Skan else 759590075Sobrien { 7596117395Skan tree derived_virtuals; 7597117395Skan tree base_virtuals; 7598117395Skan tree orig_virtuals; 7599117395Skan /* If BINFO is a primary base, the most derived class which has 7600117395Skan BINFO as a primary base; otherwise, just BINFO. */ 7601117395Skan tree non_primary_binfo; 760290075Sobrien 7603117395Skan /* We might be a primary base class. Go up the inheritance hierarchy 7604117395Skan until we find the most derived class of which we are a primary base: 7605117395Skan it is the BINFO_VIRTUALS there that we need to consider. */ 7606117395Skan non_primary_binfo = binfo; 7607117395Skan while (BINFO_INHERITANCE_CHAIN (non_primary_binfo)) 760890075Sobrien { 7609117395Skan tree b; 7610117395Skan 7611117395Skan /* If we have reached a virtual base, then it must be vid->vbase, 7612117395Skan because we ignore other virtual bases in 7613117395Skan add_vcall_offset_vtbl_entries_r. In turn, it must be a primary 7614117395Skan base (possibly multi-level) of vid->binfo, or we wouldn't 7615117395Skan have called build_vcall_and_vbase_vtbl_entries for it. But it 7616117395Skan might be a lost primary, so just skip down to vid->binfo. */ 7617169689Skan if (BINFO_VIRTUAL_P (non_primary_binfo)) 7618117395Skan { 7619169689Skan gcc_assert (non_primary_binfo == vid->vbase); 7620117395Skan non_primary_binfo = vid->binfo; 7621117395Skan break; 7622117395Skan } 7623117395Skan 7624117395Skan b = BINFO_INHERITANCE_CHAIN (non_primary_binfo); 7625117395Skan if (get_primary_binfo (b) != non_primary_binfo) 7626117395Skan break; 7627117395Skan non_primary_binfo = b; 762890075Sobrien } 762990075Sobrien 7630117395Skan if (vid->ctor_vtbl_p) 7631117395Skan /* For a ctor vtable we need the equivalent binfo within the hierarchy 7632117395Skan where rtti_binfo is the most derived type. */ 7633132718Skan non_primary_binfo 7634132718Skan = original_binfo (non_primary_binfo, vid->rtti_binfo); 7635169689Skan 7636117395Skan for (base_virtuals = BINFO_VIRTUALS (binfo), 7637117395Skan derived_virtuals = BINFO_VIRTUALS (non_primary_binfo), 7638117395Skan orig_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo))); 7639117395Skan base_virtuals; 7640117395Skan base_virtuals = TREE_CHAIN (base_virtuals), 7641117395Skan derived_virtuals = TREE_CHAIN (derived_virtuals), 7642117395Skan orig_virtuals = TREE_CHAIN (orig_virtuals)) 7643117395Skan { 7644117395Skan tree orig_fn; 7645117395Skan 7646117395Skan /* Find the declaration that originally caused this function to 7647117395Skan be present in BINFO_TYPE (binfo). */ 7648117395Skan orig_fn = BV_FN (orig_virtuals); 7649117395Skan 7650117395Skan /* When processing BINFO, we only want to generate vcall slots for 7651117395Skan function slots introduced in BINFO. So don't try to generate 7652117395Skan one if the function isn't even defined in BINFO. */ 7653169689Skan if (!SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), DECL_CONTEXT (orig_fn))) 7654117395Skan continue; 7655117395Skan 7656117395Skan add_vcall_offset (orig_fn, binfo, vid); 7657117395Skan } 765890075Sobrien } 7659117395Skan} 766090075Sobrien 7661117395Skan/* Add a vcall offset entry for ORIG_FN to the vtable. */ 766290075Sobrien 7663117395Skanstatic void 7664117395Skanadd_vcall_offset (tree orig_fn, tree binfo, vtbl_init_data *vid) 7665117395Skan{ 7666117395Skan size_t i; 7667117395Skan tree vcall_offset; 7668169689Skan tree derived_entry; 7669117395Skan 7670117395Skan /* If there is already an entry for a function with the same 7671117395Skan signature as FN, then we do not need a second vcall offset. 7672117395Skan Check the list of functions already present in the derived 7673117395Skan class vtable. */ 7674169689Skan for (i = 0; VEC_iterate (tree, vid->fns, i, derived_entry); ++i) 767590075Sobrien { 7676117395Skan if (same_signature_p (derived_entry, orig_fn) 7677117395Skan /* We only use one vcall offset for virtual destructors, 7678117395Skan even though there are two virtual table entries. */ 7679117395Skan || (DECL_DESTRUCTOR_P (derived_entry) 7680117395Skan && DECL_DESTRUCTOR_P (orig_fn))) 7681117395Skan return; 7682117395Skan } 768390075Sobrien 7684117395Skan /* If we are building these vcall offsets as part of building 7685117395Skan the vtable for the most derived class, remember the vcall 7686117395Skan offset. */ 7687117395Skan if (vid->binfo == TYPE_BINFO (vid->derived)) 7688169689Skan { 7689169689Skan tree_pair_p elt = VEC_safe_push (tree_pair_s, gc, 7690169689Skan CLASSTYPE_VCALL_INDICES (vid->derived), 7691169689Skan NULL); 7692169689Skan elt->purpose = orig_fn; 7693169689Skan elt->value = vid->index; 7694169689Skan } 769590075Sobrien 7696117395Skan /* The next vcall offset will be found at a more negative 7697117395Skan offset. */ 7698117395Skan vid->index = size_binop (MINUS_EXPR, vid->index, 7699117395Skan ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE)); 7700117395Skan 7701117395Skan /* Keep track of this function. */ 7702169689Skan VEC_safe_push (tree, gc, vid->fns, orig_fn); 7703117395Skan 7704117395Skan if (vid->generate_vcall_entries) 7705117395Skan { 7706117395Skan tree base; 7707117395Skan tree fn; 7708117395Skan 770990075Sobrien /* Find the overriding function. */ 7710117395Skan fn = find_final_overrider (vid->rtti_binfo, binfo, orig_fn); 7711117395Skan if (fn == error_mark_node) 7712117395Skan vcall_offset = build1 (NOP_EXPR, vtable_entry_type, 7713117395Skan integer_zero_node); 7714117395Skan else 771590075Sobrien { 7716117395Skan base = TREE_VALUE (fn); 771790075Sobrien 7718117395Skan /* The vbase we're working on is a primary base of 7719117395Skan vid->binfo. But it might be a lost primary, so its 7720117395Skan BINFO_OFFSET might be wrong, so we just use the 7721117395Skan BINFO_OFFSET from vid->binfo. */ 7722117395Skan vcall_offset = size_diffop (BINFO_OFFSET (base), 7723117395Skan BINFO_OFFSET (vid->binfo)); 7724169689Skan vcall_offset = fold_build1 (NOP_EXPR, vtable_entry_type, 7725169689Skan vcall_offset); 772690075Sobrien } 7727132718Skan /* Add the initializer to the vtable. */ 772890075Sobrien *vid->last_init = build_tree_list (NULL_TREE, vcall_offset); 772990075Sobrien vid->last_init = &TREE_CHAIN (*vid->last_init); 773090075Sobrien } 773190075Sobrien} 773290075Sobrien 7733132718Skan/* Return vtbl initializers for the RTTI entries corresponding to the 773490075Sobrien BINFO's vtable. The RTTI entries should indicate the object given 773590075Sobrien by VID->rtti_binfo. */ 773690075Sobrien 773790075Sobrienstatic void 7738132718Skanbuild_rtti_vtbl_entries (tree binfo, vtbl_init_data* vid) 773990075Sobrien{ 774090075Sobrien tree b; 774190075Sobrien tree t; 774290075Sobrien tree basetype; 774390075Sobrien tree offset; 774490075Sobrien tree decl; 774590075Sobrien tree init; 774690075Sobrien 774790075Sobrien basetype = BINFO_TYPE (binfo); 774890075Sobrien t = BINFO_TYPE (vid->rtti_binfo); 774990075Sobrien 775090075Sobrien /* To find the complete object, we will first convert to our most 775190075Sobrien primary base, and then add the offset in the vtbl to that value. */ 775290075Sobrien b = binfo; 775390075Sobrien while (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (b)) 7754169689Skan && !BINFO_LOST_PRIMARY_P (b)) 775590075Sobrien { 775690075Sobrien tree primary_base; 775790075Sobrien 775890075Sobrien primary_base = get_primary_binfo (b); 7759169689Skan gcc_assert (BINFO_PRIMARY_P (primary_base) 7760169689Skan && BINFO_INHERITANCE_CHAIN (primary_base) == b); 776190075Sobrien b = primary_base; 776290075Sobrien } 776390075Sobrien offset = size_diffop (BINFO_OFFSET (vid->rtti_binfo), BINFO_OFFSET (b)); 776490075Sobrien 776590075Sobrien /* The second entry is the address of the typeinfo object. */ 776690075Sobrien if (flag_rtti) 7767117395Skan decl = build_address (get_tinfo_decl (t)); 776890075Sobrien else 776990075Sobrien decl = integer_zero_node; 7770169689Skan 777190075Sobrien /* Convert the declaration to a type that can be stored in the 777290075Sobrien vtable. */ 7773117395Skan init = build_nop (vfunc_ptr_type_node, decl); 777490075Sobrien *vid->last_init = build_tree_list (NULL_TREE, init); 777590075Sobrien vid->last_init = &TREE_CHAIN (*vid->last_init); 777690075Sobrien 7777169689Skan /* Add the offset-to-top entry. It comes earlier in the vtable than 7778169689Skan the typeinfo entry. Convert the offset to look like a 777990075Sobrien function pointer, so that we can put it in the vtable. */ 7780117395Skan init = build_nop (vfunc_ptr_type_node, offset); 778190075Sobrien *vid->last_init = build_tree_list (NULL_TREE, init); 778290075Sobrien vid->last_init = &TREE_CHAIN (*vid->last_init); 778390075Sobrien} 7784169689Skan 7785169689Skan/* Fold a OBJ_TYPE_REF expression to the address of a function. 7786169689Skan KNOWN_TYPE carries the true type of OBJ_TYPE_REF_OBJECT(REF). */ 7787169689Skan 7788169689Skantree 7789169689Skancp_fold_obj_type_ref (tree ref, tree known_type) 7790169689Skan{ 7791169689Skan HOST_WIDE_INT index = tree_low_cst (OBJ_TYPE_REF_TOKEN (ref), 1); 7792169689Skan HOST_WIDE_INT i = 0; 7793169689Skan tree v = BINFO_VIRTUALS (TYPE_BINFO (known_type)); 7794169689Skan tree fndecl; 7795169689Skan 7796169689Skan while (i != index) 7797169689Skan { 7798169689Skan i += (TARGET_VTABLE_USES_DESCRIPTORS 7799169689Skan ? TARGET_VTABLE_USES_DESCRIPTORS : 1); 7800169689Skan v = TREE_CHAIN (v); 7801169689Skan } 7802169689Skan 7803169689Skan fndecl = BV_FN (v); 7804169689Skan 7805169689Skan#ifdef ENABLE_CHECKING 7806169689Skan gcc_assert (tree_int_cst_equal (OBJ_TYPE_REF_TOKEN (ref), 7807169689Skan DECL_VINDEX (fndecl))); 7808169689Skan#endif 7809169689Skan 7810169689Skan cgraph_node (fndecl)->local.vtable_method = true; 7811169689Skan 7812169689Skan return build_address (fndecl); 7813169689Skan} 7814169689Skan 7815169689Skan#include "gt-cp-class.h" 7816