1 /* Functions related to building classes and their related objects. 2 Copyright (C) 1987, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 3 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009 4 Free Software Foundation, Inc. 5 Contributed by Michael Tiemann (tiemann@cygnus.com) 6 7 This file is part of GCC. 8 9 GCC is free software; you can redistribute it and/or modify 10 it under the terms of the GNU General Public License as published by 11 the Free Software Foundation; either version 3, or (at your option) 12 any later version. 13 14 GCC is distributed in the hope that it will be useful, 15 but WITHOUT ANY WARRANTY; without even the implied warranty of 16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 GNU General Public License for more details. 18 19 You should have received a copy of the GNU General Public License 20 along with GCC; see the file COPYING3. If not see 21 <http://www.gnu.org/licenses/>. */ 22 23 24 /* High-level class interface. */ 25 26 #include "config.h" 27 #include "system.h" 28 #include "coretypes.h" 29 #include "tm.h" 30 #include "tree.h" 31 #include "cp-tree.h" 32 #include "flags.h" 33 #include "rtl.h" 34 #include "output.h" 35 #include "toplev.h" 36 #include "target.h" 37 #include "convert.h" 38 #include "cgraph.h" 39 #include "tree-dump.h" 40 41 /* The number of nested classes being processed. If we are not in the 42 scope of any class, this is zero. */ 43 44 int current_class_depth; 45 46 /* In order to deal with nested classes, we keep a stack of classes. 47 The topmost entry is the innermost class, and is the entry at index 48 CURRENT_CLASS_DEPTH */ 49 50 typedef struct class_stack_node { 51 /* The name of the class. */ 52 tree name; 53 54 /* The _TYPE node for the class. */ 55 tree type; 56 57 /* The access specifier pending for new declarations in the scope of 58 this class. */ 59 tree access; 60 61 /* If were defining TYPE, the names used in this class. */ 62 splay_tree names_used; 63 64 /* Nonzero if this class is no longer open, because of a call to 65 push_to_top_level. */ 66 size_t hidden; 67 }* class_stack_node_t; 68 69 typedef struct vtbl_init_data_s 70 { 71 /* The base for which we're building initializers. */ 72 tree binfo; 73 /* The type of the most-derived type. */ 74 tree derived; 75 /* The binfo for the dynamic type. This will be TYPE_BINFO (derived), 76 unless ctor_vtbl_p is true. */ 77 tree rtti_binfo; 78 /* The negative-index vtable initializers built up so far. These 79 are in order from least negative index to most negative index. */ 80 tree inits; 81 /* The last (i.e., most negative) entry in INITS. */ 82 tree* last_init; 83 /* The binfo for the virtual base for which we're building 84 vcall offset initializers. */ 85 tree vbase; 86 /* The functions in vbase for which we have already provided vcall 87 offsets. */ 88 VEC(tree,gc) *fns; 89 /* The vtable index of the next vcall or vbase offset. */ 90 tree index; 91 /* Nonzero if we are building the initializer for the primary 92 vtable. */ 93 int primary_vtbl_p; 94 /* Nonzero if we are building the initializer for a construction 95 vtable. */ 96 int ctor_vtbl_p; 97 /* True when adding vcall offset entries to the vtable. False when 98 merely computing the indices. */ 99 bool generate_vcall_entries; 100 } vtbl_init_data; 101 102 /* The type of a function passed to walk_subobject_offsets. */ 103 typedef int (*subobject_offset_fn) (tree, tree, splay_tree); 104 105 /* The stack itself. This is a dynamically resized array. The 106 number of elements allocated is CURRENT_CLASS_STACK_SIZE. */ 107 static int current_class_stack_size; 108 static class_stack_node_t current_class_stack; 109 110 /* The size of the largest empty class seen in this translation unit. */ 111 static GTY (()) tree sizeof_biggest_empty_class; 112 113 /* An array of all local classes present in this translation unit, in 114 declaration order. */ 115 VEC(tree,gc) *local_classes; 116 117 static tree get_vfield_name (tree); 118 static void finish_struct_anon (tree); 119 static tree get_vtable_name (tree); 120 static tree get_basefndecls (tree, tree); 121 static int build_primary_vtable (tree, tree); 122 static int build_secondary_vtable (tree); 123 static void finish_vtbls (tree); 124 static void modify_vtable_entry (tree, tree, tree, tree, tree *); 125 static void finish_struct_bits (tree); 126 static int alter_access (tree, tree, tree); 127 static void handle_using_decl (tree, tree); 128 static tree dfs_modify_vtables (tree, void *); 129 static tree modify_all_vtables (tree, tree); 130 static void determine_primary_bases (tree); 131 static void finish_struct_methods (tree); 132 static void maybe_warn_about_overly_private_class (tree); 133 static int method_name_cmp (const void *, const void *); 134 static int resort_method_name_cmp (const void *, const void *); 135 static void add_implicitly_declared_members (tree, int, int); 136 static tree fixed_type_or_null (tree, int *, int *); 137 static tree build_simple_base_path (tree expr, tree binfo); 138 static tree build_vtbl_ref_1 (tree, tree); 139 static tree build_vtbl_initializer (tree, tree, tree, tree, int *); 140 static int count_fields (tree); 141 static int add_fields_to_record_type (tree, struct sorted_fields_type*, int); 142 static bool check_bitfield_decl (tree); 143 static void check_field_decl (tree, tree, int *, int *, int *); 144 static void check_field_decls (tree, tree *, int *, int *); 145 static tree *build_base_field (record_layout_info, tree, splay_tree, tree *); 146 static void build_base_fields (record_layout_info, splay_tree, tree *); 147 static void check_methods (tree); 148 static void remove_zero_width_bit_fields (tree); 149 static void check_bases (tree, int *, int *); 150 static void check_bases_and_members (tree); 151 static tree create_vtable_ptr (tree, tree *); 152 static void include_empty_classes (record_layout_info); 153 static void layout_class_type (tree, tree *); 154 static void propagate_binfo_offsets (tree, tree); 155 static void layout_virtual_bases (record_layout_info, splay_tree); 156 static void build_vbase_offset_vtbl_entries (tree, vtbl_init_data *); 157 static void add_vcall_offset_vtbl_entries_r (tree, vtbl_init_data *); 158 static void add_vcall_offset_vtbl_entries_1 (tree, vtbl_init_data *); 159 static void build_vcall_offset_vtbl_entries (tree, vtbl_init_data *); 160 static void add_vcall_offset (tree, tree, vtbl_init_data *); 161 static void layout_vtable_decl (tree, int); 162 static tree dfs_find_final_overrider_pre (tree, void *); 163 static tree dfs_find_final_overrider_post (tree, void *); 164 static tree find_final_overrider (tree, tree, tree); 165 static int make_new_vtable (tree, tree); 166 static tree get_primary_binfo (tree); 167 static int maybe_indent_hierarchy (FILE *, int, int); 168 static tree dump_class_hierarchy_r (FILE *, int, tree, tree, int); 169 static void dump_class_hierarchy (tree); 170 static void dump_class_hierarchy_1 (FILE *, int, tree); 171 static void dump_array (FILE *, tree); 172 static void dump_vtable (tree, tree, tree); 173 static void dump_vtt (tree, tree); 174 static void dump_thunk (FILE *, int, tree); 175 static tree build_vtable (tree, tree, tree); 176 static void initialize_vtable (tree, tree); 177 static void layout_nonempty_base_or_field (record_layout_info, 178 tree, tree, splay_tree); 179 static tree end_of_class (tree, int); 180 static bool layout_empty_base (record_layout_info, tree, tree, splay_tree); 181 static void accumulate_vtbl_inits (tree, tree, tree, tree, tree); 182 static tree dfs_accumulate_vtbl_inits (tree, tree, tree, tree, 183 tree); 184 static void build_rtti_vtbl_entries (tree, vtbl_init_data *); 185 static void build_vcall_and_vbase_vtbl_entries (tree, vtbl_init_data *); 186 static void clone_constructors_and_destructors (tree); 187 static tree build_clone (tree, tree); 188 static void update_vtable_entry_for_fn (tree, tree, tree, tree *, unsigned); 189 static void build_ctor_vtbl_group (tree, tree); 190 static void build_vtt (tree); 191 static tree binfo_ctor_vtable (tree); 192 static tree *build_vtt_inits (tree, tree, tree *, tree *); 193 static tree dfs_build_secondary_vptr_vtt_inits (tree, void *); 194 static tree dfs_fixup_binfo_vtbls (tree, void *); 195 static int record_subobject_offset (tree, tree, splay_tree); 196 static int check_subobject_offset (tree, tree, splay_tree); 197 static int walk_subobject_offsets (tree, subobject_offset_fn, 198 tree, splay_tree, tree, int); 199 static void record_subobject_offsets (tree, tree, splay_tree, bool); 200 static int layout_conflict_p (tree, tree, splay_tree, int); 201 static int splay_tree_compare_integer_csts (splay_tree_key k1, 202 splay_tree_key k2); 203 static void warn_about_ambiguous_bases (tree); 204 static bool type_requires_array_cookie (tree); 205 static bool contains_empty_class_p (tree); 206 static bool base_derived_from (tree, tree); 207 static int empty_base_at_nonzero_offset_p (tree, tree, splay_tree); 208 static tree end_of_base (tree); 209 static tree get_vcall_index (tree, tree); 210 211 /* Variables shared between class.c and call.c. */ 212 213 #ifdef GATHER_STATISTICS 214 int n_vtables = 0; 215 int n_vtable_entries = 0; 216 int n_vtable_searches = 0; 217 int n_vtable_elems = 0; 218 int n_convert_harshness = 0; 219 int n_compute_conversion_costs = 0; 220 int n_inner_fields_searched = 0; 221 #endif 222 223 /* Convert to or from a base subobject. EXPR is an expression of type 224 `A' or `A*', an expression of type `B' or `B*' is returned. To 225 convert A to a base B, CODE is PLUS_EXPR and BINFO is the binfo for 226 the B base instance within A. To convert base A to derived B, CODE 227 is MINUS_EXPR and BINFO is the binfo for the A instance within B. 228 In this latter case, A must not be a morally virtual base of B. 229 NONNULL is true if EXPR is known to be non-NULL (this is only 230 needed when EXPR is of pointer type). CV qualifiers are preserved 231 from EXPR. */ 232 233 tree 234 build_base_path (enum tree_code code, 235 tree expr, 236 tree binfo, 237 int nonnull) 238 { 239 tree v_binfo = NULL_TREE; 240 tree d_binfo = NULL_TREE; 241 tree probe; 242 tree offset; 243 tree target_type; 244 tree null_test = NULL; 245 tree ptr_target_type; 246 int fixed_type_p; 247 int want_pointer = TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE; 248 bool has_empty = false; 249 bool virtual_access; 250 251 if (expr == error_mark_node || binfo == error_mark_node || !binfo) 252 return error_mark_node; 253 254 for (probe = binfo; probe; probe = BINFO_INHERITANCE_CHAIN (probe)) 255 { 256 d_binfo = probe; 257 if (is_empty_class (BINFO_TYPE (probe))) 258 has_empty = true; 259 if (!v_binfo && BINFO_VIRTUAL_P (probe)) 260 v_binfo = probe; 261 } 262 263 probe = TYPE_MAIN_VARIANT (TREE_TYPE (expr)); 264 if (want_pointer) 265 probe = TYPE_MAIN_VARIANT (TREE_TYPE (probe)); 266 267 gcc_assert ((code == MINUS_EXPR 268 && SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), probe)) 269 || (code == PLUS_EXPR 270 && SAME_BINFO_TYPE_P (BINFO_TYPE (d_binfo), probe))); 271 272 if (binfo == d_binfo) 273 /* Nothing to do. */ 274 return expr; 275 276 if (code == MINUS_EXPR && v_binfo) 277 { 278 error ("cannot convert from base %qT to derived type %qT via virtual base %qT", 279 BINFO_TYPE (binfo), BINFO_TYPE (d_binfo), BINFO_TYPE (v_binfo)); 280 return error_mark_node; 281 } 282 283 if (!want_pointer) 284 /* This must happen before the call to save_expr. */ 285 expr = cp_build_unary_op (ADDR_EXPR, expr, 0, tf_warning_or_error); 286 287 offset = BINFO_OFFSET (binfo); 288 fixed_type_p = resolves_to_fixed_type_p (expr, &nonnull); 289 target_type = code == PLUS_EXPR ? BINFO_TYPE (binfo) : BINFO_TYPE (d_binfo); 290 291 /* Do we need to look in the vtable for the real offset? */ 292 virtual_access = (v_binfo && fixed_type_p <= 0); 293 294 /* Don't bother with the calculations inside sizeof; they'll ICE if the 295 source type is incomplete and the pointer value doesn't matter. */ 296 if (cp_unevaluated_operand != 0) 297 { 298 expr = build_nop (build_pointer_type (target_type), expr); 299 if (!want_pointer) 300 expr = build_indirect_ref (EXPR_LOCATION (expr), expr, RO_NULL); 301 return expr; 302 } 303 304 /* Do we need to check for a null pointer? */ 305 if (want_pointer && !nonnull) 306 { 307 /* If we know the conversion will not actually change the value 308 of EXPR, then we can avoid testing the expression for NULL. 309 We have to avoid generating a COMPONENT_REF for a base class 310 field, because other parts of the compiler know that such 311 expressions are always non-NULL. */ 312 if (!virtual_access && integer_zerop (offset)) 313 { 314 tree class_type; 315 /* TARGET_TYPE has been extracted from BINFO, and, is 316 therefore always cv-unqualified. Extract the 317 cv-qualifiers from EXPR so that the expression returned 318 matches the input. */ 319 class_type = TREE_TYPE (TREE_TYPE (expr)); 320 target_type 321 = cp_build_qualified_type (target_type, 322 cp_type_quals (class_type)); 323 return build_nop (build_pointer_type (target_type), expr); 324 } 325 null_test = error_mark_node; 326 } 327 328 /* Protect against multiple evaluation if necessary. */ 329 if (TREE_SIDE_EFFECTS (expr) && (null_test || virtual_access)) 330 expr = save_expr (expr); 331 332 /* Now that we've saved expr, build the real null test. */ 333 if (null_test) 334 { 335 tree zero = cp_convert (TREE_TYPE (expr), integer_zero_node); 336 null_test = fold_build2_loc (input_location, NE_EXPR, boolean_type_node, 337 expr, zero); 338 } 339 340 /* If this is a simple base reference, express it as a COMPONENT_REF. */ 341 if (code == PLUS_EXPR && !virtual_access 342 /* We don't build base fields for empty bases, and they aren't very 343 interesting to the optimizers anyway. */ 344 && !has_empty) 345 { 346 expr = cp_build_indirect_ref (expr, RO_NULL, tf_warning_or_error); 347 expr = build_simple_base_path (expr, binfo); 348 if (want_pointer) 349 expr = build_address (expr); 350 target_type = TREE_TYPE (expr); 351 goto out; 352 } 353 354 if (virtual_access) 355 { 356 /* Going via virtual base V_BINFO. We need the static offset 357 from V_BINFO to BINFO, and the dynamic offset from D_BINFO to 358 V_BINFO. That offset is an entry in D_BINFO's vtable. */ 359 tree v_offset; 360 361 if (fixed_type_p < 0 && in_base_initializer) 362 { 363 /* In a base member initializer, we cannot rely on the 364 vtable being set up. We have to indirect via the 365 vtt_parm. */ 366 tree t; 367 368 t = TREE_TYPE (TYPE_VFIELD (current_class_type)); 369 t = build_pointer_type (t); 370 v_offset = convert (t, current_vtt_parm); 371 v_offset = cp_build_indirect_ref (v_offset, RO_NULL, 372 tf_warning_or_error); 373 } 374 else 375 v_offset = build_vfield_ref (cp_build_indirect_ref (expr, RO_NULL, 376 tf_warning_or_error), 377 TREE_TYPE (TREE_TYPE (expr))); 378 379 v_offset = build2 (POINTER_PLUS_EXPR, TREE_TYPE (v_offset), 380 v_offset, fold_convert (sizetype, BINFO_VPTR_FIELD (v_binfo))); 381 v_offset = build1 (NOP_EXPR, 382 build_pointer_type (ptrdiff_type_node), 383 v_offset); 384 v_offset = cp_build_indirect_ref (v_offset, RO_NULL, tf_warning_or_error); 385 TREE_CONSTANT (v_offset) = 1; 386 387 offset = convert_to_integer (ptrdiff_type_node, 388 size_diffop_loc (input_location, offset, 389 BINFO_OFFSET (v_binfo))); 390 391 if (!integer_zerop (offset)) 392 v_offset = build2 (code, ptrdiff_type_node, v_offset, offset); 393 394 if (fixed_type_p < 0) 395 /* Negative fixed_type_p means this is a constructor or destructor; 396 virtual base layout is fixed in in-charge [cd]tors, but not in 397 base [cd]tors. */ 398 offset = build3 (COND_EXPR, ptrdiff_type_node, 399 build2 (EQ_EXPR, boolean_type_node, 400 current_in_charge_parm, integer_zero_node), 401 v_offset, 402 convert_to_integer (ptrdiff_type_node, 403 BINFO_OFFSET (binfo))); 404 else 405 offset = v_offset; 406 } 407 408 target_type = cp_build_qualified_type 409 (target_type, cp_type_quals (TREE_TYPE (TREE_TYPE (expr)))); 410 ptr_target_type = build_pointer_type (target_type); 411 if (want_pointer) 412 target_type = ptr_target_type; 413 414 expr = build1 (NOP_EXPR, ptr_target_type, expr); 415 416 if (!integer_zerop (offset)) 417 { 418 offset = fold_convert (sizetype, offset); 419 if (code == MINUS_EXPR) 420 offset = fold_build1_loc (input_location, NEGATE_EXPR, sizetype, offset); 421 expr = build2 (POINTER_PLUS_EXPR, ptr_target_type, expr, offset); 422 } 423 else 424 null_test = NULL; 425 426 if (!want_pointer) 427 expr = cp_build_indirect_ref (expr, RO_NULL, tf_warning_or_error); 428 429 out: 430 if (null_test) 431 expr = fold_build3_loc (input_location, COND_EXPR, target_type, null_test, expr, 432 fold_build1_loc (input_location, NOP_EXPR, target_type, 433 integer_zero_node)); 434 435 return expr; 436 } 437 438 /* Subroutine of build_base_path; EXPR and BINFO are as in that function. 439 Perform a derived-to-base conversion by recursively building up a 440 sequence of COMPONENT_REFs to the appropriate base fields. */ 441 442 static tree 443 build_simple_base_path (tree expr, tree binfo) 444 { 445 tree type = BINFO_TYPE (binfo); 446 tree d_binfo = BINFO_INHERITANCE_CHAIN (binfo); 447 tree field; 448 449 if (d_binfo == NULL_TREE) 450 { 451 tree temp; 452 453 gcc_assert (TYPE_MAIN_VARIANT (TREE_TYPE (expr)) == type); 454 455 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x' 456 into `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only 457 an lvalue in the front end; only _DECLs and _REFs are lvalues 458 in the back end. */ 459 temp = unary_complex_lvalue (ADDR_EXPR, expr); 460 if (temp) 461 expr = cp_build_indirect_ref (temp, RO_NULL, tf_warning_or_error); 462 463 return expr; 464 } 465 466 /* Recurse. */ 467 expr = build_simple_base_path (expr, d_binfo); 468 469 for (field = TYPE_FIELDS (BINFO_TYPE (d_binfo)); 470 field; field = TREE_CHAIN (field)) 471 /* Is this the base field created by build_base_field? */ 472 if (TREE_CODE (field) == FIELD_DECL 473 && DECL_FIELD_IS_BASE (field) 474 && TREE_TYPE (field) == type) 475 { 476 /* We don't use build_class_member_access_expr here, as that 477 has unnecessary checks, and more importantly results in 478 recursive calls to dfs_walk_once. */ 479 int type_quals = cp_type_quals (TREE_TYPE (expr)); 480 481 expr = build3 (COMPONENT_REF, 482 cp_build_qualified_type (type, type_quals), 483 expr, field, NULL_TREE); 484 expr = fold_if_not_in_template (expr); 485 486 /* Mark the expression const or volatile, as appropriate. 487 Even though we've dealt with the type above, we still have 488 to mark the expression itself. */ 489 if (type_quals & TYPE_QUAL_CONST) 490 TREE_READONLY (expr) = 1; 491 if (type_quals & TYPE_QUAL_VOLATILE) 492 TREE_THIS_VOLATILE (expr) = 1; 493 494 return expr; 495 } 496 497 /* Didn't find the base field?!? */ 498 gcc_unreachable (); 499 } 500 501 /* Convert OBJECT to the base TYPE. OBJECT is an expression whose 502 type is a class type or a pointer to a class type. In the former 503 case, TYPE is also a class type; in the latter it is another 504 pointer type. If CHECK_ACCESS is true, an error message is emitted 505 if TYPE is inaccessible. If OBJECT has pointer type, the value is 506 assumed to be non-NULL. */ 507 508 tree 509 convert_to_base (tree object, tree type, bool check_access, bool nonnull) 510 { 511 tree binfo; 512 tree object_type; 513 514 if (TYPE_PTR_P (TREE_TYPE (object))) 515 { 516 object_type = TREE_TYPE (TREE_TYPE (object)); 517 type = TREE_TYPE (type); 518 } 519 else 520 object_type = TREE_TYPE (object); 521 522 binfo = lookup_base (object_type, type, 523 check_access ? ba_check : ba_unique, 524 NULL); 525 if (!binfo || binfo == error_mark_node) 526 return error_mark_node; 527 528 return build_base_path (PLUS_EXPR, object, binfo, nonnull); 529 } 530 531 /* EXPR is an expression with unqualified class type. BASE is a base 532 binfo of that class type. Returns EXPR, converted to the BASE 533 type. This function assumes that EXPR is the most derived class; 534 therefore virtual bases can be found at their static offsets. */ 535 536 tree 537 convert_to_base_statically (tree expr, tree base) 538 { 539 tree expr_type; 540 541 expr_type = TREE_TYPE (expr); 542 if (!SAME_BINFO_TYPE_P (BINFO_TYPE (base), expr_type)) 543 { 544 tree pointer_type; 545 546 pointer_type = build_pointer_type (expr_type); 547 548 /* We use fold_build2 and fold_convert below to simplify the trees 549 provided to the optimizers. It is not safe to call these functions 550 when processing a template because they do not handle C++-specific 551 trees. */ 552 gcc_assert (!processing_template_decl); 553 expr = cp_build_unary_op (ADDR_EXPR, expr, /*noconvert=*/1, 554 tf_warning_or_error); 555 if (!integer_zerop (BINFO_OFFSET (base))) 556 expr = fold_build2_loc (input_location, 557 POINTER_PLUS_EXPR, pointer_type, expr, 558 fold_convert (sizetype, BINFO_OFFSET (base))); 559 expr = fold_convert (build_pointer_type (BINFO_TYPE (base)), expr); 560 expr = build_fold_indirect_ref_loc (input_location, expr); 561 } 562 563 return expr; 564 } 565 566 567 tree 568 build_vfield_ref (tree datum, tree type) 569 { 570 tree vfield, vcontext; 571 572 if (datum == error_mark_node) 573 return error_mark_node; 574 575 /* First, convert to the requested type. */ 576 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (datum), type)) 577 datum = convert_to_base (datum, type, /*check_access=*/false, 578 /*nonnull=*/true); 579 580 /* Second, the requested type may not be the owner of its own vptr. 581 If not, convert to the base class that owns it. We cannot use 582 convert_to_base here, because VCONTEXT may appear more than once 583 in the inheritance hierarchy of TYPE, and thus direct conversion 584 between the types may be ambiguous. Following the path back up 585 one step at a time via primary bases avoids the problem. */ 586 vfield = TYPE_VFIELD (type); 587 vcontext = DECL_CONTEXT (vfield); 588 while (!same_type_ignoring_top_level_qualifiers_p (vcontext, type)) 589 { 590 datum = build_simple_base_path (datum, CLASSTYPE_PRIMARY_BINFO (type)); 591 type = TREE_TYPE (datum); 592 } 593 594 return build3 (COMPONENT_REF, TREE_TYPE (vfield), datum, vfield, NULL_TREE); 595 } 596 597 /* Given an object INSTANCE, return an expression which yields the 598 vtable element corresponding to INDEX. There are many special 599 cases for INSTANCE which we take care of here, mainly to avoid 600 creating extra tree nodes when we don't have to. */ 601 602 static tree 603 build_vtbl_ref_1 (tree instance, tree idx) 604 { 605 tree aref; 606 tree vtbl = NULL_TREE; 607 608 /* Try to figure out what a reference refers to, and 609 access its virtual function table directly. */ 610 611 int cdtorp = 0; 612 tree fixed_type = fixed_type_or_null (instance, NULL, &cdtorp); 613 614 tree basetype = non_reference (TREE_TYPE (instance)); 615 616 if (fixed_type && !cdtorp) 617 { 618 tree binfo = lookup_base (fixed_type, basetype, 619 ba_unique | ba_quiet, NULL); 620 if (binfo) 621 vtbl = unshare_expr (BINFO_VTABLE (binfo)); 622 } 623 624 if (!vtbl) 625 vtbl = build_vfield_ref (instance, basetype); 626 627 aref = build_array_ref (input_location, vtbl, idx); 628 TREE_CONSTANT (aref) |= TREE_CONSTANT (vtbl) && TREE_CONSTANT (idx); 629 630 return aref; 631 } 632 633 tree 634 build_vtbl_ref (tree instance, tree idx) 635 { 636 tree aref = build_vtbl_ref_1 (instance, idx); 637 638 return aref; 639 } 640 641 /* Given a stable object pointer INSTANCE_PTR, return an expression which 642 yields a function pointer corresponding to vtable element INDEX. */ 643 644 tree 645 build_vfn_ref (tree instance_ptr, tree idx) 646 { 647 tree aref; 648 649 aref = build_vtbl_ref_1 (cp_build_indirect_ref (instance_ptr, RO_NULL, 650 tf_warning_or_error), 651 idx); 652 653 /* When using function descriptors, the address of the 654 vtable entry is treated as a function pointer. */ 655 if (TARGET_VTABLE_USES_DESCRIPTORS) 656 aref = build1 (NOP_EXPR, TREE_TYPE (aref), 657 cp_build_unary_op (ADDR_EXPR, aref, /*noconvert=*/1, 658 tf_warning_or_error)); 659 660 /* Remember this as a method reference, for later devirtualization. */ 661 aref = build3 (OBJ_TYPE_REF, TREE_TYPE (aref), aref, instance_ptr, idx); 662 663 return aref; 664 } 665 666 /* Return the name of the virtual function table (as an IDENTIFIER_NODE) 667 for the given TYPE. */ 668 669 static tree 670 get_vtable_name (tree type) 671 { 672 return mangle_vtbl_for_type (type); 673 } 674 675 /* DECL is an entity associated with TYPE, like a virtual table or an 676 implicitly generated constructor. Determine whether or not DECL 677 should have external or internal linkage at the object file 678 level. This routine does not deal with COMDAT linkage and other 679 similar complexities; it simply sets TREE_PUBLIC if it possible for 680 entities in other translation units to contain copies of DECL, in 681 the abstract. */ 682 683 void 684 set_linkage_according_to_type (tree type ATTRIBUTE_UNUSED, tree decl) 685 { 686 TREE_PUBLIC (decl) = 1; 687 determine_visibility (decl); 688 } 689 690 /* Create a VAR_DECL for a primary or secondary vtable for CLASS_TYPE. 691 (For a secondary vtable for B-in-D, CLASS_TYPE should be D, not B.) 692 Use NAME for the name of the vtable, and VTABLE_TYPE for its type. */ 693 694 static tree 695 build_vtable (tree class_type, tree name, tree vtable_type) 696 { 697 tree decl; 698 699 decl = build_lang_decl (VAR_DECL, name, vtable_type); 700 /* vtable names are already mangled; give them their DECL_ASSEMBLER_NAME 701 now to avoid confusion in mangle_decl. */ 702 SET_DECL_ASSEMBLER_NAME (decl, name); 703 DECL_CONTEXT (decl) = class_type; 704 DECL_ARTIFICIAL (decl) = 1; 705 TREE_STATIC (decl) = 1; 706 TREE_READONLY (decl) = 1; 707 DECL_VIRTUAL_P (decl) = 1; 708 DECL_ALIGN (decl) = TARGET_VTABLE_ENTRY_ALIGN; 709 DECL_VTABLE_OR_VTT_P (decl) = 1; 710 /* At one time the vtable info was grabbed 2 words at a time. This 711 fails on sparc unless you have 8-byte alignment. (tiemann) */ 712 DECL_ALIGN (decl) = MAX (TYPE_ALIGN (double_type_node), 713 DECL_ALIGN (decl)); 714 set_linkage_according_to_type (class_type, decl); 715 /* The vtable has not been defined -- yet. */ 716 DECL_EXTERNAL (decl) = 1; 717 DECL_NOT_REALLY_EXTERN (decl) = 1; 718 719 /* Mark the VAR_DECL node representing the vtable itself as a 720 "gratuitous" one, thereby forcing dwarfout.c to ignore it. It 721 is rather important that such things be ignored because any 722 effort to actually generate DWARF for them will run into 723 trouble when/if we encounter code like: 724 725 #pragma interface 726 struct S { virtual void member (); }; 727 728 because the artificial declaration of the vtable itself (as 729 manufactured by the g++ front end) will say that the vtable is 730 a static member of `S' but only *after* the debug output for 731 the definition of `S' has already been output. This causes 732 grief because the DWARF entry for the definition of the vtable 733 will try to refer back to an earlier *declaration* of the 734 vtable as a static member of `S' and there won't be one. We 735 might be able to arrange to have the "vtable static member" 736 attached to the member list for `S' before the debug info for 737 `S' get written (which would solve the problem) but that would 738 require more intrusive changes to the g++ front end. */ 739 DECL_IGNORED_P (decl) = 1; 740 741 return decl; 742 } 743 744 /* Get the VAR_DECL of the vtable for TYPE. TYPE need not be polymorphic, 745 or even complete. If this does not exist, create it. If COMPLETE is 746 nonzero, then complete the definition of it -- that will render it 747 impossible to actually build the vtable, but is useful to get at those 748 which are known to exist in the runtime. */ 749 750 tree 751 get_vtable_decl (tree type, int complete) 752 { 753 tree decl; 754 755 if (CLASSTYPE_VTABLES (type)) 756 return CLASSTYPE_VTABLES (type); 757 758 decl = build_vtable (type, get_vtable_name (type), vtbl_type_node); 759 CLASSTYPE_VTABLES (type) = decl; 760 761 if (complete) 762 { 763 DECL_EXTERNAL (decl) = 1; 764 cp_finish_decl (decl, NULL_TREE, false, NULL_TREE, 0); 765 } 766 767 return decl; 768 } 769 770 /* Build the primary virtual function table for TYPE. If BINFO is 771 non-NULL, build the vtable starting with the initial approximation 772 that it is the same as the one which is the head of the association 773 list. Returns a nonzero value if a new vtable is actually 774 created. */ 775 776 static int 777 build_primary_vtable (tree binfo, tree type) 778 { 779 tree decl; 780 tree virtuals; 781 782 decl = get_vtable_decl (type, /*complete=*/0); 783 784 if (binfo) 785 { 786 if (BINFO_NEW_VTABLE_MARKED (binfo)) 787 /* We have already created a vtable for this base, so there's 788 no need to do it again. */ 789 return 0; 790 791 virtuals = copy_list (BINFO_VIRTUALS (binfo)); 792 TREE_TYPE (decl) = TREE_TYPE (get_vtbl_decl_for_binfo (binfo)); 793 DECL_SIZE (decl) = TYPE_SIZE (TREE_TYPE (decl)); 794 DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (TREE_TYPE (decl)); 795 } 796 else 797 { 798 gcc_assert (TREE_TYPE (decl) == vtbl_type_node); 799 virtuals = NULL_TREE; 800 } 801 802 #ifdef GATHER_STATISTICS 803 n_vtables += 1; 804 n_vtable_elems += list_length (virtuals); 805 #endif 806 807 /* Initialize the association list for this type, based 808 on our first approximation. */ 809 BINFO_VTABLE (TYPE_BINFO (type)) = decl; 810 BINFO_VIRTUALS (TYPE_BINFO (type)) = virtuals; 811 SET_BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (type)); 812 return 1; 813 } 814 815 /* Give BINFO a new virtual function table which is initialized 816 with a skeleton-copy of its original initialization. The only 817 entry that changes is the `delta' entry, so we can really 818 share a lot of structure. 819 820 FOR_TYPE is the most derived type which caused this table to 821 be needed. 822 823 Returns nonzero if we haven't met BINFO before. 824 825 The order in which vtables are built (by calling this function) for 826 an object must remain the same, otherwise a binary incompatibility 827 can result. */ 828 829 static int 830 build_secondary_vtable (tree binfo) 831 { 832 if (BINFO_NEW_VTABLE_MARKED (binfo)) 833 /* We already created a vtable for this base. There's no need to 834 do it again. */ 835 return 0; 836 837 /* Remember that we've created a vtable for this BINFO, so that we 838 don't try to do so again. */ 839 SET_BINFO_NEW_VTABLE_MARKED (binfo); 840 841 /* Make fresh virtual list, so we can smash it later. */ 842 BINFO_VIRTUALS (binfo) = copy_list (BINFO_VIRTUALS (binfo)); 843 844 /* Secondary vtables are laid out as part of the same structure as 845 the primary vtable. */ 846 BINFO_VTABLE (binfo) = NULL_TREE; 847 return 1; 848 } 849 850 /* Create a new vtable for BINFO which is the hierarchy dominated by 851 T. Return nonzero if we actually created a new vtable. */ 852 853 static int 854 make_new_vtable (tree t, tree binfo) 855 { 856 if (binfo == TYPE_BINFO (t)) 857 /* In this case, it is *type*'s vtable we are modifying. We start 858 with the approximation that its vtable is that of the 859 immediate base class. */ 860 return build_primary_vtable (binfo, t); 861 else 862 /* This is our very own copy of `basetype' to play with. Later, 863 we will fill in all the virtual functions that override the 864 virtual functions in these base classes which are not defined 865 by the current type. */ 866 return build_secondary_vtable (binfo); 867 } 868 869 /* Make *VIRTUALS, an entry on the BINFO_VIRTUALS list for BINFO 870 (which is in the hierarchy dominated by T) list FNDECL as its 871 BV_FN. DELTA is the required constant adjustment from the `this' 872 pointer where the vtable entry appears to the `this' required when 873 the function is actually called. */ 874 875 static void 876 modify_vtable_entry (tree t, 877 tree binfo, 878 tree fndecl, 879 tree delta, 880 tree *virtuals) 881 { 882 tree v; 883 884 v = *virtuals; 885 886 if (fndecl != BV_FN (v) 887 || !tree_int_cst_equal (delta, BV_DELTA (v))) 888 { 889 /* We need a new vtable for BINFO. */ 890 if (make_new_vtable (t, binfo)) 891 { 892 /* If we really did make a new vtable, we also made a copy 893 of the BINFO_VIRTUALS list. Now, we have to find the 894 corresponding entry in that list. */ 895 *virtuals = BINFO_VIRTUALS (binfo); 896 while (BV_FN (*virtuals) != BV_FN (v)) 897 *virtuals = TREE_CHAIN (*virtuals); 898 v = *virtuals; 899 } 900 901 BV_DELTA (v) = delta; 902 BV_VCALL_INDEX (v) = NULL_TREE; 903 BV_FN (v) = fndecl; 904 } 905 } 906 907 908 /* Add method METHOD to class TYPE. If USING_DECL is non-null, it is 909 the USING_DECL naming METHOD. Returns true if the method could be 910 added to the method vec. */ 911 912 bool 913 add_method (tree type, tree method, tree using_decl) 914 { 915 unsigned slot; 916 tree overload; 917 bool template_conv_p = false; 918 bool conv_p; 919 VEC(tree,gc) *method_vec; 920 bool complete_p; 921 bool insert_p = false; 922 tree current_fns; 923 tree fns; 924 925 if (method == error_mark_node) 926 return false; 927 928 complete_p = COMPLETE_TYPE_P (type); 929 conv_p = DECL_CONV_FN_P (method); 930 if (conv_p) 931 template_conv_p = (TREE_CODE (method) == TEMPLATE_DECL 932 && DECL_TEMPLATE_CONV_FN_P (method)); 933 934 method_vec = CLASSTYPE_METHOD_VEC (type); 935 if (!method_vec) 936 { 937 /* Make a new method vector. We start with 8 entries. We must 938 allocate at least two (for constructors and destructors), and 939 we're going to end up with an assignment operator at some 940 point as well. */ 941 method_vec = VEC_alloc (tree, gc, 8); 942 /* Create slots for constructors and destructors. */ 943 VEC_quick_push (tree, method_vec, NULL_TREE); 944 VEC_quick_push (tree, method_vec, NULL_TREE); 945 CLASSTYPE_METHOD_VEC (type) = method_vec; 946 } 947 948 /* Maintain TYPE_HAS_USER_CONSTRUCTOR, etc. */ 949 grok_special_member_properties (method); 950 951 /* Constructors and destructors go in special slots. */ 952 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (method)) 953 slot = CLASSTYPE_CONSTRUCTOR_SLOT; 954 else if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (method)) 955 { 956 slot = CLASSTYPE_DESTRUCTOR_SLOT; 957 958 if (TYPE_FOR_JAVA (type)) 959 { 960 if (!DECL_ARTIFICIAL (method)) 961 error ("Java class %qT cannot have a destructor", type); 962 else if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)) 963 error ("Java class %qT cannot have an implicit non-trivial " 964 "destructor", 965 type); 966 } 967 } 968 else 969 { 970 tree m; 971 972 insert_p = true; 973 /* See if we already have an entry with this name. */ 974 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT; 975 VEC_iterate (tree, method_vec, slot, m); 976 ++slot) 977 { 978 m = OVL_CURRENT (m); 979 if (template_conv_p) 980 { 981 if (TREE_CODE (m) == TEMPLATE_DECL 982 && DECL_TEMPLATE_CONV_FN_P (m)) 983 insert_p = false; 984 break; 985 } 986 if (conv_p && !DECL_CONV_FN_P (m)) 987 break; 988 if (DECL_NAME (m) == DECL_NAME (method)) 989 { 990 insert_p = false; 991 break; 992 } 993 if (complete_p 994 && !DECL_CONV_FN_P (m) 995 && DECL_NAME (m) > DECL_NAME (method)) 996 break; 997 } 998 } 999 current_fns = insert_p ? NULL_TREE : VEC_index (tree, method_vec, slot); 1000 1001 /* Check to see if we've already got this method. */ 1002 for (fns = current_fns; fns; fns = OVL_NEXT (fns)) 1003 { 1004 tree fn = OVL_CURRENT (fns); 1005 tree fn_type; 1006 tree method_type; 1007 tree parms1; 1008 tree parms2; 1009 1010 if (TREE_CODE (fn) != TREE_CODE (method)) 1011 continue; 1012 1013 /* [over.load] Member function declarations with the 1014 same name and the same parameter types cannot be 1015 overloaded if any of them is a static member 1016 function declaration. 1017 1018 [namespace.udecl] When a using-declaration brings names 1019 from a base class into a derived class scope, member 1020 functions in the derived class override and/or hide member 1021 functions with the same name and parameter types in a base 1022 class (rather than conflicting). */ 1023 fn_type = TREE_TYPE (fn); 1024 method_type = TREE_TYPE (method); 1025 parms1 = TYPE_ARG_TYPES (fn_type); 1026 parms2 = TYPE_ARG_TYPES (method_type); 1027 1028 /* Compare the quals on the 'this' parm. Don't compare 1029 the whole types, as used functions are treated as 1030 coming from the using class in overload resolution. */ 1031 if (! DECL_STATIC_FUNCTION_P (fn) 1032 && ! DECL_STATIC_FUNCTION_P (method) 1033 && TREE_TYPE (TREE_VALUE (parms1)) != error_mark_node 1034 && TREE_TYPE (TREE_VALUE (parms2)) != error_mark_node 1035 && (TYPE_QUALS (TREE_TYPE (TREE_VALUE (parms1))) 1036 != TYPE_QUALS (TREE_TYPE (TREE_VALUE (parms2))))) 1037 continue; 1038 1039 /* For templates, the return type and template parameters 1040 must be identical. */ 1041 if (TREE_CODE (fn) == TEMPLATE_DECL 1042 && (!same_type_p (TREE_TYPE (fn_type), 1043 TREE_TYPE (method_type)) 1044 || !comp_template_parms (DECL_TEMPLATE_PARMS (fn), 1045 DECL_TEMPLATE_PARMS (method)))) 1046 continue; 1047 1048 if (! DECL_STATIC_FUNCTION_P (fn)) 1049 parms1 = TREE_CHAIN (parms1); 1050 if (! DECL_STATIC_FUNCTION_P (method)) 1051 parms2 = TREE_CHAIN (parms2); 1052 1053 if (compparms (parms1, parms2) 1054 && (!DECL_CONV_FN_P (fn) 1055 || same_type_p (TREE_TYPE (fn_type), 1056 TREE_TYPE (method_type)))) 1057 { 1058 if (using_decl) 1059 { 1060 if (DECL_CONTEXT (fn) == type) 1061 /* Defer to the local function. */ 1062 return false; 1063 if (DECL_CONTEXT (fn) == DECL_CONTEXT (method)) 1064 error ("repeated using declaration %q+D", using_decl); 1065 else 1066 error ("using declaration %q+D conflicts with a previous using declaration", 1067 using_decl); 1068 } 1069 else 1070 { 1071 error ("%q+#D cannot be overloaded", method); 1072 error ("with %q+#D", fn); 1073 } 1074 1075 /* We don't call duplicate_decls here to merge the 1076 declarations because that will confuse things if the 1077 methods have inline definitions. In particular, we 1078 will crash while processing the definitions. */ 1079 return false; 1080 } 1081 } 1082 1083 /* A class should never have more than one destructor. */ 1084 if (current_fns && DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (method)) 1085 return false; 1086 1087 /* Add the new binding. */ 1088 overload = build_overload (method, current_fns); 1089 1090 if (conv_p) 1091 TYPE_HAS_CONVERSION (type) = 1; 1092 else if (slot >= CLASSTYPE_FIRST_CONVERSION_SLOT && !complete_p) 1093 push_class_level_binding (DECL_NAME (method), overload); 1094 1095 if (insert_p) 1096 { 1097 bool reallocated; 1098 1099 /* We only expect to add few methods in the COMPLETE_P case, so 1100 just make room for one more method in that case. */ 1101 if (complete_p) 1102 reallocated = VEC_reserve_exact (tree, gc, method_vec, 1); 1103 else 1104 reallocated = VEC_reserve (tree, gc, method_vec, 1); 1105 if (reallocated) 1106 CLASSTYPE_METHOD_VEC (type) = method_vec; 1107 if (slot == VEC_length (tree, method_vec)) 1108 VEC_quick_push (tree, method_vec, overload); 1109 else 1110 VEC_quick_insert (tree, method_vec, slot, overload); 1111 } 1112 else 1113 /* Replace the current slot. */ 1114 VEC_replace (tree, method_vec, slot, overload); 1115 return true; 1116 } 1117 1118 /* Subroutines of finish_struct. */ 1119 1120 /* Change the access of FDECL to ACCESS in T. Return 1 if change was 1121 legit, otherwise return 0. */ 1122 1123 static int 1124 alter_access (tree t, tree fdecl, tree access) 1125 { 1126 tree elem; 1127 1128 if (!DECL_LANG_SPECIFIC (fdecl)) 1129 retrofit_lang_decl (fdecl); 1130 1131 gcc_assert (!DECL_DISCRIMINATOR_P (fdecl)); 1132 1133 elem = purpose_member (t, DECL_ACCESS (fdecl)); 1134 if (elem) 1135 { 1136 if (TREE_VALUE (elem) != access) 1137 { 1138 if (TREE_CODE (TREE_TYPE (fdecl)) == FUNCTION_DECL) 1139 error ("conflicting access specifications for method" 1140 " %q+D, ignored", TREE_TYPE (fdecl)); 1141 else 1142 error ("conflicting access specifications for field %qE, ignored", 1143 DECL_NAME (fdecl)); 1144 } 1145 else 1146 { 1147 /* They're changing the access to the same thing they changed 1148 it to before. That's OK. */ 1149 ; 1150 } 1151 } 1152 else 1153 { 1154 perform_or_defer_access_check (TYPE_BINFO (t), fdecl, fdecl); 1155 DECL_ACCESS (fdecl) = tree_cons (t, access, DECL_ACCESS (fdecl)); 1156 return 1; 1157 } 1158 return 0; 1159 } 1160 1161 /* Process the USING_DECL, which is a member of T. */ 1162 1163 static void 1164 handle_using_decl (tree using_decl, tree t) 1165 { 1166 tree decl = USING_DECL_DECLS (using_decl); 1167 tree name = DECL_NAME (using_decl); 1168 tree access 1169 = TREE_PRIVATE (using_decl) ? access_private_node 1170 : TREE_PROTECTED (using_decl) ? access_protected_node 1171 : access_public_node; 1172 tree flist = NULL_TREE; 1173 tree old_value; 1174 1175 gcc_assert (!processing_template_decl && decl); 1176 1177 old_value = lookup_member (t, name, /*protect=*/0, /*want_type=*/false); 1178 if (old_value) 1179 { 1180 if (is_overloaded_fn (old_value)) 1181 old_value = OVL_CURRENT (old_value); 1182 1183 if (DECL_P (old_value) && DECL_CONTEXT (old_value) == t) 1184 /* OK */; 1185 else 1186 old_value = NULL_TREE; 1187 } 1188 1189 cp_emit_debug_info_for_using (decl, USING_DECL_SCOPE (using_decl)); 1190 1191 if (is_overloaded_fn (decl)) 1192 flist = decl; 1193 1194 if (! old_value) 1195 ; 1196 else if (is_overloaded_fn (old_value)) 1197 { 1198 if (flist) 1199 /* It's OK to use functions from a base when there are functions with 1200 the same name already present in the current class. */; 1201 else 1202 { 1203 error ("%q+D invalid in %q#T", using_decl, t); 1204 error (" because of local method %q+#D with same name", 1205 OVL_CURRENT (old_value)); 1206 return; 1207 } 1208 } 1209 else if (!DECL_ARTIFICIAL (old_value)) 1210 { 1211 error ("%q+D invalid in %q#T", using_decl, t); 1212 error (" because of local member %q+#D with same name", old_value); 1213 return; 1214 } 1215 1216 /* Make type T see field decl FDECL with access ACCESS. */ 1217 if (flist) 1218 for (; flist; flist = OVL_NEXT (flist)) 1219 { 1220 add_method (t, OVL_CURRENT (flist), using_decl); 1221 alter_access (t, OVL_CURRENT (flist), access); 1222 } 1223 else 1224 alter_access (t, decl, access); 1225 } 1226 1227 /* Run through the base classes of T, updating CANT_HAVE_CONST_CTOR_P, 1228 and NO_CONST_ASN_REF_P. Also set flag bits in T based on 1229 properties of the bases. */ 1230 1231 static void 1232 check_bases (tree t, 1233 int* cant_have_const_ctor_p, 1234 int* no_const_asn_ref_p) 1235 { 1236 int i; 1237 int seen_non_virtual_nearly_empty_base_p; 1238 tree base_binfo; 1239 tree binfo; 1240 tree field = NULL_TREE; 1241 1242 seen_non_virtual_nearly_empty_base_p = 0; 1243 1244 if (!CLASSTYPE_NON_STD_LAYOUT (t)) 1245 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field)) 1246 if (TREE_CODE (field) == FIELD_DECL) 1247 break; 1248 1249 for (binfo = TYPE_BINFO (t), i = 0; 1250 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++) 1251 { 1252 tree basetype = TREE_TYPE (base_binfo); 1253 1254 gcc_assert (COMPLETE_TYPE_P (basetype)); 1255 1256 /* Effective C++ rule 14. We only need to check TYPE_POLYMORPHIC_P 1257 here because the case of virtual functions but non-virtual 1258 dtor is handled in finish_struct_1. */ 1259 if (!TYPE_POLYMORPHIC_P (basetype)) 1260 warning (OPT_Weffc__, 1261 "base class %q#T has a non-virtual destructor", basetype); 1262 1263 /* If the base class doesn't have copy constructors or 1264 assignment operators that take const references, then the 1265 derived class cannot have such a member automatically 1266 generated. */ 1267 if (! TYPE_HAS_CONST_INIT_REF (basetype)) 1268 *cant_have_const_ctor_p = 1; 1269 if (TYPE_HAS_ASSIGN_REF (basetype) 1270 && !TYPE_HAS_CONST_ASSIGN_REF (basetype)) 1271 *no_const_asn_ref_p = 1; 1272 1273 if (BINFO_VIRTUAL_P (base_binfo)) 1274 /* A virtual base does not effect nearly emptiness. */ 1275 ; 1276 else if (CLASSTYPE_NEARLY_EMPTY_P (basetype)) 1277 { 1278 if (seen_non_virtual_nearly_empty_base_p) 1279 /* And if there is more than one nearly empty base, then the 1280 derived class is not nearly empty either. */ 1281 CLASSTYPE_NEARLY_EMPTY_P (t) = 0; 1282 else 1283 /* Remember we've seen one. */ 1284 seen_non_virtual_nearly_empty_base_p = 1; 1285 } 1286 else if (!is_empty_class (basetype)) 1287 /* If the base class is not empty or nearly empty, then this 1288 class cannot be nearly empty. */ 1289 CLASSTYPE_NEARLY_EMPTY_P (t) = 0; 1290 1291 /* A lot of properties from the bases also apply to the derived 1292 class. */ 1293 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (basetype); 1294 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) 1295 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype); 1296 TYPE_HAS_COMPLEX_ASSIGN_REF (t) 1297 |= TYPE_HAS_COMPLEX_ASSIGN_REF (basetype); 1298 TYPE_HAS_COMPLEX_INIT_REF (t) |= TYPE_HAS_COMPLEX_INIT_REF (basetype); 1299 TYPE_POLYMORPHIC_P (t) |= TYPE_POLYMORPHIC_P (basetype); 1300 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) 1301 |= CLASSTYPE_CONTAINS_EMPTY_CLASS_P (basetype); 1302 TYPE_HAS_COMPLEX_DFLT (t) |= TYPE_HAS_COMPLEX_DFLT (basetype); 1303 1304 /* A standard-layout class is a class that: 1305 ... 1306 * has no non-standard-layout base classes, */ 1307 CLASSTYPE_NON_STD_LAYOUT (t) |= CLASSTYPE_NON_STD_LAYOUT (basetype); 1308 if (!CLASSTYPE_NON_STD_LAYOUT (t)) 1309 { 1310 tree basefield; 1311 /* ...has no base classes of the same type as the first non-static 1312 data member... */ 1313 if (field && DECL_CONTEXT (field) == t 1314 && (same_type_ignoring_top_level_qualifiers_p 1315 (TREE_TYPE (field), basetype))) 1316 CLASSTYPE_NON_STD_LAYOUT (t) = 1; 1317 else 1318 /* ...either has no non-static data members in the most-derived 1319 class and at most one base class with non-static data 1320 members, or has no base classes with non-static data 1321 members */ 1322 for (basefield = TYPE_FIELDS (basetype); basefield; 1323 basefield = TREE_CHAIN (basefield)) 1324 if (TREE_CODE (basefield) == FIELD_DECL) 1325 { 1326 if (field) 1327 CLASSTYPE_NON_STD_LAYOUT (t) = 1; 1328 else 1329 field = basefield; 1330 break; 1331 } 1332 } 1333 } 1334 } 1335 1336 /* Determine all the primary bases within T. Sets BINFO_PRIMARY_BASE_P for 1337 those that are primaries. Sets BINFO_LOST_PRIMARY_P for those 1338 that have had a nearly-empty virtual primary base stolen by some 1339 other base in the hierarchy. Determines CLASSTYPE_PRIMARY_BASE for 1340 T. */ 1341 1342 static void 1343 determine_primary_bases (tree t) 1344 { 1345 unsigned i; 1346 tree primary = NULL_TREE; 1347 tree type_binfo = TYPE_BINFO (t); 1348 tree base_binfo; 1349 1350 /* Determine the primary bases of our bases. */ 1351 for (base_binfo = TREE_CHAIN (type_binfo); base_binfo; 1352 base_binfo = TREE_CHAIN (base_binfo)) 1353 { 1354 tree primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (base_binfo)); 1355 1356 /* See if we're the non-virtual primary of our inheritance 1357 chain. */ 1358 if (!BINFO_VIRTUAL_P (base_binfo)) 1359 { 1360 tree parent = BINFO_INHERITANCE_CHAIN (base_binfo); 1361 tree parent_primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (parent)); 1362 1363 if (parent_primary 1364 && SAME_BINFO_TYPE_P (BINFO_TYPE (base_binfo), 1365 BINFO_TYPE (parent_primary))) 1366 /* We are the primary binfo. */ 1367 BINFO_PRIMARY_P (base_binfo) = 1; 1368 } 1369 /* Determine if we have a virtual primary base, and mark it so. 1370 */ 1371 if (primary && BINFO_VIRTUAL_P (primary)) 1372 { 1373 tree this_primary = copied_binfo (primary, base_binfo); 1374 1375 if (BINFO_PRIMARY_P (this_primary)) 1376 /* Someone already claimed this base. */ 1377 BINFO_LOST_PRIMARY_P (base_binfo) = 1; 1378 else 1379 { 1380 tree delta; 1381 1382 BINFO_PRIMARY_P (this_primary) = 1; 1383 BINFO_INHERITANCE_CHAIN (this_primary) = base_binfo; 1384 1385 /* A virtual binfo might have been copied from within 1386 another hierarchy. As we're about to use it as a 1387 primary base, make sure the offsets match. */ 1388 delta = size_diffop_loc (input_location, 1389 convert (ssizetype, 1390 BINFO_OFFSET (base_binfo)), 1391 convert (ssizetype, 1392 BINFO_OFFSET (this_primary))); 1393 1394 propagate_binfo_offsets (this_primary, delta); 1395 } 1396 } 1397 } 1398 1399 /* First look for a dynamic direct non-virtual base. */ 1400 for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, base_binfo); i++) 1401 { 1402 tree basetype = BINFO_TYPE (base_binfo); 1403 1404 if (TYPE_CONTAINS_VPTR_P (basetype) && !BINFO_VIRTUAL_P (base_binfo)) 1405 { 1406 primary = base_binfo; 1407 goto found; 1408 } 1409 } 1410 1411 /* A "nearly-empty" virtual base class can be the primary base 1412 class, if no non-virtual polymorphic base can be found. Look for 1413 a nearly-empty virtual dynamic base that is not already a primary 1414 base of something in the hierarchy. If there is no such base, 1415 just pick the first nearly-empty virtual base. */ 1416 1417 for (base_binfo = TREE_CHAIN (type_binfo); base_binfo; 1418 base_binfo = TREE_CHAIN (base_binfo)) 1419 if (BINFO_VIRTUAL_P (base_binfo) 1420 && CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (base_binfo))) 1421 { 1422 if (!BINFO_PRIMARY_P (base_binfo)) 1423 { 1424 /* Found one that is not primary. */ 1425 primary = base_binfo; 1426 goto found; 1427 } 1428 else if (!primary) 1429 /* Remember the first candidate. */ 1430 primary = base_binfo; 1431 } 1432 1433 found: 1434 /* If we've got a primary base, use it. */ 1435 if (primary) 1436 { 1437 tree basetype = BINFO_TYPE (primary); 1438 1439 CLASSTYPE_PRIMARY_BINFO (t) = primary; 1440 if (BINFO_PRIMARY_P (primary)) 1441 /* We are stealing a primary base. */ 1442 BINFO_LOST_PRIMARY_P (BINFO_INHERITANCE_CHAIN (primary)) = 1; 1443 BINFO_PRIMARY_P (primary) = 1; 1444 if (BINFO_VIRTUAL_P (primary)) 1445 { 1446 tree delta; 1447 1448 BINFO_INHERITANCE_CHAIN (primary) = type_binfo; 1449 /* A virtual binfo might have been copied from within 1450 another hierarchy. As we're about to use it as a primary 1451 base, make sure the offsets match. */ 1452 delta = size_diffop_loc (input_location, ssize_int (0), 1453 convert (ssizetype, BINFO_OFFSET (primary))); 1454 1455 propagate_binfo_offsets (primary, delta); 1456 } 1457 1458 primary = TYPE_BINFO (basetype); 1459 1460 TYPE_VFIELD (t) = TYPE_VFIELD (basetype); 1461 BINFO_VTABLE (type_binfo) = BINFO_VTABLE (primary); 1462 BINFO_VIRTUALS (type_binfo) = BINFO_VIRTUALS (primary); 1463 } 1464 } 1465 1466 /* Update the variant types of T. */ 1467 1468 void 1469 fixup_type_variants (tree t) 1470 { 1471 tree variants; 1472 1473 if (!t) 1474 return; 1475 1476 for (variants = TYPE_NEXT_VARIANT (t); 1477 variants; 1478 variants = TYPE_NEXT_VARIANT (variants)) 1479 { 1480 /* These fields are in the _TYPE part of the node, not in 1481 the TYPE_LANG_SPECIFIC component, so they are not shared. */ 1482 TYPE_HAS_USER_CONSTRUCTOR (variants) = TYPE_HAS_USER_CONSTRUCTOR (t); 1483 TYPE_NEEDS_CONSTRUCTING (variants) = TYPE_NEEDS_CONSTRUCTING (t); 1484 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (variants) 1485 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t); 1486 1487 TYPE_POLYMORPHIC_P (variants) = TYPE_POLYMORPHIC_P (t); 1488 1489 TYPE_BINFO (variants) = TYPE_BINFO (t); 1490 1491 /* Copy whatever these are holding today. */ 1492 TYPE_VFIELD (variants) = TYPE_VFIELD (t); 1493 TYPE_METHODS (variants) = TYPE_METHODS (t); 1494 TYPE_FIELDS (variants) = TYPE_FIELDS (t); 1495 1496 /* All variants of a class have the same attributes. */ 1497 TYPE_ATTRIBUTES (variants) = TYPE_ATTRIBUTES (t); 1498 } 1499 } 1500 1501 1502 /* Set memoizing fields and bits of T (and its variants) for later 1503 use. */ 1504 1505 static void 1506 finish_struct_bits (tree t) 1507 { 1508 /* Fix up variants (if any). */ 1509 fixup_type_variants (t); 1510 1511 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) && TYPE_POLYMORPHIC_P (t)) 1512 /* For a class w/o baseclasses, 'finish_struct' has set 1513 CLASSTYPE_PURE_VIRTUALS correctly (by definition). 1514 Similarly for a class whose base classes do not have vtables. 1515 When neither of these is true, we might have removed abstract 1516 virtuals (by providing a definition), added some (by declaring 1517 new ones), or redeclared ones from a base class. We need to 1518 recalculate what's really an abstract virtual at this point (by 1519 looking in the vtables). */ 1520 get_pure_virtuals (t); 1521 1522 /* If this type has a copy constructor or a destructor, force its 1523 mode to be BLKmode, and force its TREE_ADDRESSABLE bit to be 1524 nonzero. This will cause it to be passed by invisible reference 1525 and prevent it from being returned in a register. */ 1526 if (! TYPE_HAS_TRIVIAL_INIT_REF (t) || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)) 1527 { 1528 tree variants; 1529 DECL_MODE (TYPE_MAIN_DECL (t)) = BLKmode; 1530 for (variants = t; variants; variants = TYPE_NEXT_VARIANT (variants)) 1531 { 1532 SET_TYPE_MODE (variants, BLKmode); 1533 TREE_ADDRESSABLE (variants) = 1; 1534 } 1535 } 1536 } 1537 1538 /* Issue warnings about T having private constructors, but no friends, 1539 and so forth. 1540 1541 HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or 1542 static members. HAS_NONPRIVATE_STATIC_FN is nonzero if T has any 1543 non-private static member functions. */ 1544 1545 static void 1546 maybe_warn_about_overly_private_class (tree t) 1547 { 1548 int has_member_fn = 0; 1549 int has_nonprivate_method = 0; 1550 tree fn; 1551 1552 if (!warn_ctor_dtor_privacy 1553 /* If the class has friends, those entities might create and 1554 access instances, so we should not warn. */ 1555 || (CLASSTYPE_FRIEND_CLASSES (t) 1556 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t))) 1557 /* We will have warned when the template was declared; there's 1558 no need to warn on every instantiation. */ 1559 || CLASSTYPE_TEMPLATE_INSTANTIATION (t)) 1560 /* There's no reason to even consider warning about this 1561 class. */ 1562 return; 1563 1564 /* We only issue one warning, if more than one applies, because 1565 otherwise, on code like: 1566 1567 class A { 1568 // Oops - forgot `public:' 1569 A(); 1570 A(const A&); 1571 ~A(); 1572 }; 1573 1574 we warn several times about essentially the same problem. */ 1575 1576 /* Check to see if all (non-constructor, non-destructor) member 1577 functions are private. (Since there are no friends or 1578 non-private statics, we can't ever call any of the private member 1579 functions.) */ 1580 for (fn = TYPE_METHODS (t); fn; fn = TREE_CHAIN (fn)) 1581 /* We're not interested in compiler-generated methods; they don't 1582 provide any way to call private members. */ 1583 if (!DECL_ARTIFICIAL (fn)) 1584 { 1585 if (!TREE_PRIVATE (fn)) 1586 { 1587 if (DECL_STATIC_FUNCTION_P (fn)) 1588 /* A non-private static member function is just like a 1589 friend; it can create and invoke private member 1590 functions, and be accessed without a class 1591 instance. */ 1592 return; 1593 1594 has_nonprivate_method = 1; 1595 /* Keep searching for a static member function. */ 1596 } 1597 else if (!DECL_CONSTRUCTOR_P (fn) && !DECL_DESTRUCTOR_P (fn)) 1598 has_member_fn = 1; 1599 } 1600 1601 if (!has_nonprivate_method && has_member_fn) 1602 { 1603 /* There are no non-private methods, and there's at least one 1604 private member function that isn't a constructor or 1605 destructor. (If all the private members are 1606 constructors/destructors we want to use the code below that 1607 issues error messages specifically referring to 1608 constructors/destructors.) */ 1609 unsigned i; 1610 tree binfo = TYPE_BINFO (t); 1611 1612 for (i = 0; i != BINFO_N_BASE_BINFOS (binfo); i++) 1613 if (BINFO_BASE_ACCESS (binfo, i) != access_private_node) 1614 { 1615 has_nonprivate_method = 1; 1616 break; 1617 } 1618 if (!has_nonprivate_method) 1619 { 1620 warning (OPT_Wctor_dtor_privacy, 1621 "all member functions in class %qT are private", t); 1622 return; 1623 } 1624 } 1625 1626 /* Even if some of the member functions are non-private, the class 1627 won't be useful for much if all the constructors or destructors 1628 are private: such an object can never be created or destroyed. */ 1629 fn = CLASSTYPE_DESTRUCTORS (t); 1630 if (fn && TREE_PRIVATE (fn)) 1631 { 1632 warning (OPT_Wctor_dtor_privacy, 1633 "%q#T only defines a private destructor and has no friends", 1634 t); 1635 return; 1636 } 1637 1638 /* Warn about classes that have private constructors and no friends. */ 1639 if (TYPE_HAS_USER_CONSTRUCTOR (t) 1640 /* Implicitly generated constructors are always public. */ 1641 && (!CLASSTYPE_LAZY_DEFAULT_CTOR (t) 1642 || !CLASSTYPE_LAZY_COPY_CTOR (t))) 1643 { 1644 int nonprivate_ctor = 0; 1645 1646 /* If a non-template class does not define a copy 1647 constructor, one is defined for it, enabling it to avoid 1648 this warning. For a template class, this does not 1649 happen, and so we would normally get a warning on: 1650 1651 template <class T> class C { private: C(); }; 1652 1653 To avoid this asymmetry, we check TYPE_HAS_INIT_REF. All 1654 complete non-template or fully instantiated classes have this 1655 flag set. */ 1656 if (!TYPE_HAS_INIT_REF (t)) 1657 nonprivate_ctor = 1; 1658 else 1659 for (fn = CLASSTYPE_CONSTRUCTORS (t); fn; fn = OVL_NEXT (fn)) 1660 { 1661 tree ctor = OVL_CURRENT (fn); 1662 /* Ideally, we wouldn't count copy constructors (or, in 1663 fact, any constructor that takes an argument of the 1664 class type as a parameter) because such things cannot 1665 be used to construct an instance of the class unless 1666 you already have one. But, for now at least, we're 1667 more generous. */ 1668 if (! TREE_PRIVATE (ctor)) 1669 { 1670 nonprivate_ctor = 1; 1671 break; 1672 } 1673 } 1674 1675 if (nonprivate_ctor == 0) 1676 { 1677 warning (OPT_Wctor_dtor_privacy, 1678 "%q#T only defines private constructors and has no friends", 1679 t); 1680 return; 1681 } 1682 } 1683 } 1684 1685 static struct { 1686 gt_pointer_operator new_value; 1687 void *cookie; 1688 } resort_data; 1689 1690 /* Comparison function to compare two TYPE_METHOD_VEC entries by name. */ 1691 1692 static int 1693 method_name_cmp (const void* m1_p, const void* m2_p) 1694 { 1695 const tree *const m1 = (const tree *) m1_p; 1696 const tree *const m2 = (const tree *) m2_p; 1697 1698 if (*m1 == NULL_TREE && *m2 == NULL_TREE) 1699 return 0; 1700 if (*m1 == NULL_TREE) 1701 return -1; 1702 if (*m2 == NULL_TREE) 1703 return 1; 1704 if (DECL_NAME (OVL_CURRENT (*m1)) < DECL_NAME (OVL_CURRENT (*m2))) 1705 return -1; 1706 return 1; 1707 } 1708 1709 /* This routine compares two fields like method_name_cmp but using the 1710 pointer operator in resort_field_decl_data. */ 1711 1712 static int 1713 resort_method_name_cmp (const void* m1_p, const void* m2_p) 1714 { 1715 const tree *const m1 = (const tree *) m1_p; 1716 const tree *const m2 = (const tree *) m2_p; 1717 if (*m1 == NULL_TREE && *m2 == NULL_TREE) 1718 return 0; 1719 if (*m1 == NULL_TREE) 1720 return -1; 1721 if (*m2 == NULL_TREE) 1722 return 1; 1723 { 1724 tree d1 = DECL_NAME (OVL_CURRENT (*m1)); 1725 tree d2 = DECL_NAME (OVL_CURRENT (*m2)); 1726 resort_data.new_value (&d1, resort_data.cookie); 1727 resort_data.new_value (&d2, resort_data.cookie); 1728 if (d1 < d2) 1729 return -1; 1730 } 1731 return 1; 1732 } 1733 1734 /* Resort TYPE_METHOD_VEC because pointers have been reordered. */ 1735 1736 void 1737 resort_type_method_vec (void* obj, 1738 void* orig_obj ATTRIBUTE_UNUSED , 1739 gt_pointer_operator new_value, 1740 void* cookie) 1741 { 1742 VEC(tree,gc) *method_vec = (VEC(tree,gc) *) obj; 1743 int len = VEC_length (tree, method_vec); 1744 size_t slot; 1745 tree fn; 1746 1747 /* The type conversion ops have to live at the front of the vec, so we 1748 can't sort them. */ 1749 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT; 1750 VEC_iterate (tree, method_vec, slot, fn); 1751 ++slot) 1752 if (!DECL_CONV_FN_P (OVL_CURRENT (fn))) 1753 break; 1754 1755 if (len - slot > 1) 1756 { 1757 resort_data.new_value = new_value; 1758 resort_data.cookie = cookie; 1759 qsort (VEC_address (tree, method_vec) + slot, len - slot, sizeof (tree), 1760 resort_method_name_cmp); 1761 } 1762 } 1763 1764 /* Warn about duplicate methods in fn_fields. 1765 1766 Sort methods that are not special (i.e., constructors, destructors, 1767 and type conversion operators) so that we can find them faster in 1768 search. */ 1769 1770 static void 1771 finish_struct_methods (tree t) 1772 { 1773 tree fn_fields; 1774 VEC(tree,gc) *method_vec; 1775 int slot, len; 1776 1777 method_vec = CLASSTYPE_METHOD_VEC (t); 1778 if (!method_vec) 1779 return; 1780 1781 len = VEC_length (tree, method_vec); 1782 1783 /* Clear DECL_IN_AGGR_P for all functions. */ 1784 for (fn_fields = TYPE_METHODS (t); fn_fields; 1785 fn_fields = TREE_CHAIN (fn_fields)) 1786 DECL_IN_AGGR_P (fn_fields) = 0; 1787 1788 /* Issue warnings about private constructors and such. If there are 1789 no methods, then some public defaults are generated. */ 1790 maybe_warn_about_overly_private_class (t); 1791 1792 /* The type conversion ops have to live at the front of the vec, so we 1793 can't sort them. */ 1794 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT; 1795 VEC_iterate (tree, method_vec, slot, fn_fields); 1796 ++slot) 1797 if (!DECL_CONV_FN_P (OVL_CURRENT (fn_fields))) 1798 break; 1799 if (len - slot > 1) 1800 qsort (VEC_address (tree, method_vec) + slot, 1801 len-slot, sizeof (tree), method_name_cmp); 1802 } 1803 1804 /* Make BINFO's vtable have N entries, including RTTI entries, 1805 vbase and vcall offsets, etc. Set its type and call the back end 1806 to lay it out. */ 1807 1808 static void 1809 layout_vtable_decl (tree binfo, int n) 1810 { 1811 tree atype; 1812 tree vtable; 1813 1814 atype = build_cplus_array_type (vtable_entry_type, 1815 build_index_type (size_int (n - 1))); 1816 layout_type (atype); 1817 1818 /* We may have to grow the vtable. */ 1819 vtable = get_vtbl_decl_for_binfo (binfo); 1820 if (!same_type_p (TREE_TYPE (vtable), atype)) 1821 { 1822 TREE_TYPE (vtable) = atype; 1823 DECL_SIZE (vtable) = DECL_SIZE_UNIT (vtable) = NULL_TREE; 1824 layout_decl (vtable, 0); 1825 } 1826 } 1827 1828 /* True iff FNDECL and BASE_FNDECL (both non-static member functions) 1829 have the same signature. */ 1830 1831 int 1832 same_signature_p (const_tree fndecl, const_tree base_fndecl) 1833 { 1834 /* One destructor overrides another if they are the same kind of 1835 destructor. */ 1836 if (DECL_DESTRUCTOR_P (base_fndecl) && DECL_DESTRUCTOR_P (fndecl) 1837 && special_function_p (base_fndecl) == special_function_p (fndecl)) 1838 return 1; 1839 /* But a non-destructor never overrides a destructor, nor vice 1840 versa, nor do different kinds of destructors override 1841 one-another. For example, a complete object destructor does not 1842 override a deleting destructor. */ 1843 if (DECL_DESTRUCTOR_P (base_fndecl) || DECL_DESTRUCTOR_P (fndecl)) 1844 return 0; 1845 1846 if (DECL_NAME (fndecl) == DECL_NAME (base_fndecl) 1847 || (DECL_CONV_FN_P (fndecl) 1848 && DECL_CONV_FN_P (base_fndecl) 1849 && same_type_p (DECL_CONV_FN_TYPE (fndecl), 1850 DECL_CONV_FN_TYPE (base_fndecl)))) 1851 { 1852 tree types, base_types; 1853 types = TYPE_ARG_TYPES (TREE_TYPE (fndecl)); 1854 base_types = TYPE_ARG_TYPES (TREE_TYPE (base_fndecl)); 1855 if ((TYPE_QUALS (TREE_TYPE (TREE_VALUE (base_types))) 1856 == TYPE_QUALS (TREE_TYPE (TREE_VALUE (types)))) 1857 && compparms (TREE_CHAIN (base_types), TREE_CHAIN (types))) 1858 return 1; 1859 } 1860 return 0; 1861 } 1862 1863 /* Returns TRUE if DERIVED is a binfo containing the binfo BASE as a 1864 subobject. */ 1865 1866 static bool 1867 base_derived_from (tree derived, tree base) 1868 { 1869 tree probe; 1870 1871 for (probe = base; probe; probe = BINFO_INHERITANCE_CHAIN (probe)) 1872 { 1873 if (probe == derived) 1874 return true; 1875 else if (BINFO_VIRTUAL_P (probe)) 1876 /* If we meet a virtual base, we can't follow the inheritance 1877 any more. See if the complete type of DERIVED contains 1878 such a virtual base. */ 1879 return (binfo_for_vbase (BINFO_TYPE (probe), BINFO_TYPE (derived)) 1880 != NULL_TREE); 1881 } 1882 return false; 1883 } 1884 1885 typedef struct find_final_overrider_data_s { 1886 /* The function for which we are trying to find a final overrider. */ 1887 tree fn; 1888 /* The base class in which the function was declared. */ 1889 tree declaring_base; 1890 /* The candidate overriders. */ 1891 tree candidates; 1892 /* Path to most derived. */ 1893 VEC(tree,heap) *path; 1894 } find_final_overrider_data; 1895 1896 /* Add the overrider along the current path to FFOD->CANDIDATES. 1897 Returns true if an overrider was found; false otherwise. */ 1898 1899 static bool 1900 dfs_find_final_overrider_1 (tree binfo, 1901 find_final_overrider_data *ffod, 1902 unsigned depth) 1903 { 1904 tree method; 1905 1906 /* If BINFO is not the most derived type, try a more derived class. 1907 A definition there will overrider a definition here. */ 1908 if (depth) 1909 { 1910 depth--; 1911 if (dfs_find_final_overrider_1 1912 (VEC_index (tree, ffod->path, depth), ffod, depth)) 1913 return true; 1914 } 1915 1916 method = look_for_overrides_here (BINFO_TYPE (binfo), ffod->fn); 1917 if (method) 1918 { 1919 tree *candidate = &ffod->candidates; 1920 1921 /* Remove any candidates overridden by this new function. */ 1922 while (*candidate) 1923 { 1924 /* If *CANDIDATE overrides METHOD, then METHOD 1925 cannot override anything else on the list. */ 1926 if (base_derived_from (TREE_VALUE (*candidate), binfo)) 1927 return true; 1928 /* If METHOD overrides *CANDIDATE, remove *CANDIDATE. */ 1929 if (base_derived_from (binfo, TREE_VALUE (*candidate))) 1930 *candidate = TREE_CHAIN (*candidate); 1931 else 1932 candidate = &TREE_CHAIN (*candidate); 1933 } 1934 1935 /* Add the new function. */ 1936 ffod->candidates = tree_cons (method, binfo, ffod->candidates); 1937 return true; 1938 } 1939 1940 return false; 1941 } 1942 1943 /* Called from find_final_overrider via dfs_walk. */ 1944 1945 static tree 1946 dfs_find_final_overrider_pre (tree binfo, void *data) 1947 { 1948 find_final_overrider_data *ffod = (find_final_overrider_data *) data; 1949 1950 if (binfo == ffod->declaring_base) 1951 dfs_find_final_overrider_1 (binfo, ffod, VEC_length (tree, ffod->path)); 1952 VEC_safe_push (tree, heap, ffod->path, binfo); 1953 1954 return NULL_TREE; 1955 } 1956 1957 static tree 1958 dfs_find_final_overrider_post (tree binfo ATTRIBUTE_UNUSED, void *data) 1959 { 1960 find_final_overrider_data *ffod = (find_final_overrider_data *) data; 1961 VEC_pop (tree, ffod->path); 1962 1963 return NULL_TREE; 1964 } 1965 1966 /* Returns a TREE_LIST whose TREE_PURPOSE is the final overrider for 1967 FN and whose TREE_VALUE is the binfo for the base where the 1968 overriding occurs. BINFO (in the hierarchy dominated by the binfo 1969 DERIVED) is the base object in which FN is declared. */ 1970 1971 static tree 1972 find_final_overrider (tree derived, tree binfo, tree fn) 1973 { 1974 find_final_overrider_data ffod; 1975 1976 /* Getting this right is a little tricky. This is valid: 1977 1978 struct S { virtual void f (); }; 1979 struct T { virtual void f (); }; 1980 struct U : public S, public T { }; 1981 1982 even though calling `f' in `U' is ambiguous. But, 1983 1984 struct R { virtual void f(); }; 1985 struct S : virtual public R { virtual void f (); }; 1986 struct T : virtual public R { virtual void f (); }; 1987 struct U : public S, public T { }; 1988 1989 is not -- there's no way to decide whether to put `S::f' or 1990 `T::f' in the vtable for `R'. 1991 1992 The solution is to look at all paths to BINFO. If we find 1993 different overriders along any two, then there is a problem. */ 1994 if (DECL_THUNK_P (fn)) 1995 fn = THUNK_TARGET (fn); 1996 1997 /* Determine the depth of the hierarchy. */ 1998 ffod.fn = fn; 1999 ffod.declaring_base = binfo; 2000 ffod.candidates = NULL_TREE; 2001 ffod.path = VEC_alloc (tree, heap, 30); 2002 2003 dfs_walk_all (derived, dfs_find_final_overrider_pre, 2004 dfs_find_final_overrider_post, &ffod); 2005 2006 VEC_free (tree, heap, ffod.path); 2007 2008 /* If there was no winner, issue an error message. */ 2009 if (!ffod.candidates || TREE_CHAIN (ffod.candidates)) 2010 return error_mark_node; 2011 2012 return ffod.candidates; 2013 } 2014 2015 /* Return the index of the vcall offset for FN when TYPE is used as a 2016 virtual base. */ 2017 2018 static tree 2019 get_vcall_index (tree fn, tree type) 2020 { 2021 VEC(tree_pair_s,gc) *indices = CLASSTYPE_VCALL_INDICES (type); 2022 tree_pair_p p; 2023 unsigned ix; 2024 2025 for (ix = 0; VEC_iterate (tree_pair_s, indices, ix, p); ix++) 2026 if ((DECL_DESTRUCTOR_P (fn) && DECL_DESTRUCTOR_P (p->purpose)) 2027 || same_signature_p (fn, p->purpose)) 2028 return p->value; 2029 2030 /* There should always be an appropriate index. */ 2031 gcc_unreachable (); 2032 } 2033 2034 /* Update an entry in the vtable for BINFO, which is in the hierarchy 2035 dominated by T. FN has been overridden in BINFO; VIRTUALS points to the 2036 corresponding position in the BINFO_VIRTUALS list. */ 2037 2038 static void 2039 update_vtable_entry_for_fn (tree t, tree binfo, tree fn, tree* virtuals, 2040 unsigned ix) 2041 { 2042 tree b; 2043 tree overrider; 2044 tree delta; 2045 tree virtual_base; 2046 tree first_defn; 2047 tree overrider_fn, overrider_target; 2048 tree target_fn = DECL_THUNK_P (fn) ? THUNK_TARGET (fn) : fn; 2049 tree over_return, base_return; 2050 bool lost = false; 2051 2052 /* Find the nearest primary base (possibly binfo itself) which defines 2053 this function; this is the class the caller will convert to when 2054 calling FN through BINFO. */ 2055 for (b = binfo; ; b = get_primary_binfo (b)) 2056 { 2057 gcc_assert (b); 2058 if (look_for_overrides_here (BINFO_TYPE (b), target_fn)) 2059 break; 2060 2061 /* The nearest definition is from a lost primary. */ 2062 if (BINFO_LOST_PRIMARY_P (b)) 2063 lost = true; 2064 } 2065 first_defn = b; 2066 2067 /* Find the final overrider. */ 2068 overrider = find_final_overrider (TYPE_BINFO (t), b, target_fn); 2069 if (overrider == error_mark_node) 2070 { 2071 error ("no unique final overrider for %qD in %qT", target_fn, t); 2072 return; 2073 } 2074 overrider_target = overrider_fn = TREE_PURPOSE (overrider); 2075 2076 /* Check for adjusting covariant return types. */ 2077 over_return = TREE_TYPE (TREE_TYPE (overrider_target)); 2078 base_return = TREE_TYPE (TREE_TYPE (target_fn)); 2079 2080 if (POINTER_TYPE_P (over_return) 2081 && TREE_CODE (over_return) == TREE_CODE (base_return) 2082 && CLASS_TYPE_P (TREE_TYPE (over_return)) 2083 && CLASS_TYPE_P (TREE_TYPE (base_return)) 2084 /* If the overrider is invalid, don't even try. */ 2085 && !DECL_INVALID_OVERRIDER_P (overrider_target)) 2086 { 2087 /* If FN is a covariant thunk, we must figure out the adjustment 2088 to the final base FN was converting to. As OVERRIDER_TARGET might 2089 also be converting to the return type of FN, we have to 2090 combine the two conversions here. */ 2091 tree fixed_offset, virtual_offset; 2092 2093 over_return = TREE_TYPE (over_return); 2094 base_return = TREE_TYPE (base_return); 2095 2096 if (DECL_THUNK_P (fn)) 2097 { 2098 gcc_assert (DECL_RESULT_THUNK_P (fn)); 2099 fixed_offset = ssize_int (THUNK_FIXED_OFFSET (fn)); 2100 virtual_offset = THUNK_VIRTUAL_OFFSET (fn); 2101 } 2102 else 2103 fixed_offset = virtual_offset = NULL_TREE; 2104 2105 if (virtual_offset) 2106 /* Find the equivalent binfo within the return type of the 2107 overriding function. We will want the vbase offset from 2108 there. */ 2109 virtual_offset = binfo_for_vbase (BINFO_TYPE (virtual_offset), 2110 over_return); 2111 else if (!same_type_ignoring_top_level_qualifiers_p 2112 (over_return, base_return)) 2113 { 2114 /* There was no existing virtual thunk (which takes 2115 precedence). So find the binfo of the base function's 2116 return type within the overriding function's return type. 2117 We cannot call lookup base here, because we're inside a 2118 dfs_walk, and will therefore clobber the BINFO_MARKED 2119 flags. Fortunately we know the covariancy is valid (it 2120 has already been checked), so we can just iterate along 2121 the binfos, which have been chained in inheritance graph 2122 order. Of course it is lame that we have to repeat the 2123 search here anyway -- we should really be caching pieces 2124 of the vtable and avoiding this repeated work. */ 2125 tree thunk_binfo, base_binfo; 2126 2127 /* Find the base binfo within the overriding function's 2128 return type. We will always find a thunk_binfo, except 2129 when the covariancy is invalid (which we will have 2130 already diagnosed). */ 2131 for (base_binfo = TYPE_BINFO (base_return), 2132 thunk_binfo = TYPE_BINFO (over_return); 2133 thunk_binfo; 2134 thunk_binfo = TREE_CHAIN (thunk_binfo)) 2135 if (SAME_BINFO_TYPE_P (BINFO_TYPE (thunk_binfo), 2136 BINFO_TYPE (base_binfo))) 2137 break; 2138 2139 /* See if virtual inheritance is involved. */ 2140 for (virtual_offset = thunk_binfo; 2141 virtual_offset; 2142 virtual_offset = BINFO_INHERITANCE_CHAIN (virtual_offset)) 2143 if (BINFO_VIRTUAL_P (virtual_offset)) 2144 break; 2145 2146 if (virtual_offset 2147 || (thunk_binfo && !BINFO_OFFSET_ZEROP (thunk_binfo))) 2148 { 2149 tree offset = convert (ssizetype, BINFO_OFFSET (thunk_binfo)); 2150 2151 if (virtual_offset) 2152 { 2153 /* We convert via virtual base. Adjust the fixed 2154 offset to be from there. */ 2155 offset = 2156 size_diffop (offset, 2157 convert (ssizetype, 2158 BINFO_OFFSET (virtual_offset))); 2159 } 2160 if (fixed_offset) 2161 /* There was an existing fixed offset, this must be 2162 from the base just converted to, and the base the 2163 FN was thunking to. */ 2164 fixed_offset = size_binop (PLUS_EXPR, fixed_offset, offset); 2165 else 2166 fixed_offset = offset; 2167 } 2168 } 2169 2170 if (fixed_offset || virtual_offset) 2171 /* Replace the overriding function with a covariant thunk. We 2172 will emit the overriding function in its own slot as 2173 well. */ 2174 overrider_fn = make_thunk (overrider_target, /*this_adjusting=*/0, 2175 fixed_offset, virtual_offset); 2176 } 2177 else 2178 gcc_assert (DECL_INVALID_OVERRIDER_P (overrider_target) || 2179 !DECL_THUNK_P (fn)); 2180 2181 /* Assume that we will produce a thunk that convert all the way to 2182 the final overrider, and not to an intermediate virtual base. */ 2183 virtual_base = NULL_TREE; 2184 2185 /* See if we can convert to an intermediate virtual base first, and then 2186 use the vcall offset located there to finish the conversion. */ 2187 for (; b; b = BINFO_INHERITANCE_CHAIN (b)) 2188 { 2189 /* If we find the final overrider, then we can stop 2190 walking. */ 2191 if (SAME_BINFO_TYPE_P (BINFO_TYPE (b), 2192 BINFO_TYPE (TREE_VALUE (overrider)))) 2193 break; 2194 2195 /* If we find a virtual base, and we haven't yet found the 2196 overrider, then there is a virtual base between the 2197 declaring base (first_defn) and the final overrider. */ 2198 if (BINFO_VIRTUAL_P (b)) 2199 { 2200 virtual_base = b; 2201 break; 2202 } 2203 } 2204 2205 if (overrider_fn != overrider_target && !virtual_base) 2206 { 2207 /* The ABI specifies that a covariant thunk includes a mangling 2208 for a this pointer adjustment. This-adjusting thunks that 2209 override a function from a virtual base have a vcall 2210 adjustment. When the virtual base in question is a primary 2211 virtual base, we know the adjustments are zero, (and in the 2212 non-covariant case, we would not use the thunk). 2213 Unfortunately we didn't notice this could happen, when 2214 designing the ABI and so never mandated that such a covariant 2215 thunk should be emitted. Because we must use the ABI mandated 2216 name, we must continue searching from the binfo where we 2217 found the most recent definition of the function, towards the 2218 primary binfo which first introduced the function into the 2219 vtable. If that enters a virtual base, we must use a vcall 2220 this-adjusting thunk. Bleah! */ 2221 tree probe = first_defn; 2222 2223 while ((probe = get_primary_binfo (probe)) 2224 && (unsigned) list_length (BINFO_VIRTUALS (probe)) > ix) 2225 if (BINFO_VIRTUAL_P (probe)) 2226 virtual_base = probe; 2227 2228 if (virtual_base) 2229 /* Even if we find a virtual base, the correct delta is 2230 between the overrider and the binfo we're building a vtable 2231 for. */ 2232 goto virtual_covariant; 2233 } 2234 2235 /* Compute the constant adjustment to the `this' pointer. The 2236 `this' pointer, when this function is called, will point at BINFO 2237 (or one of its primary bases, which are at the same offset). */ 2238 if (virtual_base) 2239 /* The `this' pointer needs to be adjusted from the declaration to 2240 the nearest virtual base. */ 2241 delta = size_diffop_loc (input_location, 2242 convert (ssizetype, BINFO_OFFSET (virtual_base)), 2243 convert (ssizetype, BINFO_OFFSET (first_defn))); 2244 else if (lost) 2245 /* If the nearest definition is in a lost primary, we don't need an 2246 entry in our vtable. Except possibly in a constructor vtable, 2247 if we happen to get our primary back. In that case, the offset 2248 will be zero, as it will be a primary base. */ 2249 delta = size_zero_node; 2250 else 2251 /* The `this' pointer needs to be adjusted from pointing to 2252 BINFO to pointing at the base where the final overrider 2253 appears. */ 2254 virtual_covariant: 2255 delta = size_diffop_loc (input_location, 2256 convert (ssizetype, 2257 BINFO_OFFSET (TREE_VALUE (overrider))), 2258 convert (ssizetype, BINFO_OFFSET (binfo))); 2259 2260 modify_vtable_entry (t, binfo, overrider_fn, delta, virtuals); 2261 2262 if (virtual_base) 2263 BV_VCALL_INDEX (*virtuals) 2264 = get_vcall_index (overrider_target, BINFO_TYPE (virtual_base)); 2265 else 2266 BV_VCALL_INDEX (*virtuals) = NULL_TREE; 2267 } 2268 2269 /* Called from modify_all_vtables via dfs_walk. */ 2270 2271 static tree 2272 dfs_modify_vtables (tree binfo, void* data) 2273 { 2274 tree t = (tree) data; 2275 tree virtuals; 2276 tree old_virtuals; 2277 unsigned ix; 2278 2279 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo))) 2280 /* A base without a vtable needs no modification, and its bases 2281 are uninteresting. */ 2282 return dfs_skip_bases; 2283 2284 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t) 2285 && !CLASSTYPE_HAS_PRIMARY_BASE_P (t)) 2286 /* Don't do the primary vtable, if it's new. */ 2287 return NULL_TREE; 2288 2289 if (BINFO_PRIMARY_P (binfo) && !BINFO_VIRTUAL_P (binfo)) 2290 /* There's no need to modify the vtable for a non-virtual primary 2291 base; we're not going to use that vtable anyhow. We do still 2292 need to do this for virtual primary bases, as they could become 2293 non-primary in a construction vtable. */ 2294 return NULL_TREE; 2295 2296 make_new_vtable (t, binfo); 2297 2298 /* Now, go through each of the virtual functions in the virtual 2299 function table for BINFO. Find the final overrider, and update 2300 the BINFO_VIRTUALS list appropriately. */ 2301 for (ix = 0, virtuals = BINFO_VIRTUALS (binfo), 2302 old_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo))); 2303 virtuals; 2304 ix++, virtuals = TREE_CHAIN (virtuals), 2305 old_virtuals = TREE_CHAIN (old_virtuals)) 2306 update_vtable_entry_for_fn (t, 2307 binfo, 2308 BV_FN (old_virtuals), 2309 &virtuals, ix); 2310 2311 return NULL_TREE; 2312 } 2313 2314 /* Update all of the primary and secondary vtables for T. Create new 2315 vtables as required, and initialize their RTTI information. Each 2316 of the functions in VIRTUALS is declared in T and may override a 2317 virtual function from a base class; find and modify the appropriate 2318 entries to point to the overriding functions. Returns a list, in 2319 declaration order, of the virtual functions that are declared in T, 2320 but do not appear in the primary base class vtable, and which 2321 should therefore be appended to the end of the vtable for T. */ 2322 2323 static tree 2324 modify_all_vtables (tree t, tree virtuals) 2325 { 2326 tree binfo = TYPE_BINFO (t); 2327 tree *fnsp; 2328 2329 /* Update all of the vtables. */ 2330 dfs_walk_once (binfo, dfs_modify_vtables, NULL, t); 2331 2332 /* Add virtual functions not already in our primary vtable. These 2333 will be both those introduced by this class, and those overridden 2334 from secondary bases. It does not include virtuals merely 2335 inherited from secondary bases. */ 2336 for (fnsp = &virtuals; *fnsp; ) 2337 { 2338 tree fn = TREE_VALUE (*fnsp); 2339 2340 if (!value_member (fn, BINFO_VIRTUALS (binfo)) 2341 || DECL_VINDEX (fn) == error_mark_node) 2342 { 2343 /* We don't need to adjust the `this' pointer when 2344 calling this function. */ 2345 BV_DELTA (*fnsp) = integer_zero_node; 2346 BV_VCALL_INDEX (*fnsp) = NULL_TREE; 2347 2348 /* This is a function not already in our vtable. Keep it. */ 2349 fnsp = &TREE_CHAIN (*fnsp); 2350 } 2351 else 2352 /* We've already got an entry for this function. Skip it. */ 2353 *fnsp = TREE_CHAIN (*fnsp); 2354 } 2355 2356 return virtuals; 2357 } 2358 2359 /* Get the base virtual function declarations in T that have the 2360 indicated NAME. */ 2361 2362 static tree 2363 get_basefndecls (tree name, tree t) 2364 { 2365 tree methods; 2366 tree base_fndecls = NULL_TREE; 2367 int n_baseclasses = BINFO_N_BASE_BINFOS (TYPE_BINFO (t)); 2368 int i; 2369 2370 /* Find virtual functions in T with the indicated NAME. */ 2371 i = lookup_fnfields_1 (t, name); 2372 if (i != -1) 2373 for (methods = VEC_index (tree, CLASSTYPE_METHOD_VEC (t), i); 2374 methods; 2375 methods = OVL_NEXT (methods)) 2376 { 2377 tree method = OVL_CURRENT (methods); 2378 2379 if (TREE_CODE (method) == FUNCTION_DECL 2380 && DECL_VINDEX (method)) 2381 base_fndecls = tree_cons (NULL_TREE, method, base_fndecls); 2382 } 2383 2384 if (base_fndecls) 2385 return base_fndecls; 2386 2387 for (i = 0; i < n_baseclasses; i++) 2388 { 2389 tree basetype = BINFO_TYPE (BINFO_BASE_BINFO (TYPE_BINFO (t), i)); 2390 base_fndecls = chainon (get_basefndecls (name, basetype), 2391 base_fndecls); 2392 } 2393 2394 return base_fndecls; 2395 } 2396 2397 /* If this declaration supersedes the declaration of 2398 a method declared virtual in the base class, then 2399 mark this field as being virtual as well. */ 2400 2401 void 2402 check_for_override (tree decl, tree ctype) 2403 { 2404 if (TREE_CODE (decl) == TEMPLATE_DECL) 2405 /* In [temp.mem] we have: 2406 2407 A specialization of a member function template does not 2408 override a virtual function from a base class. */ 2409 return; 2410 if ((DECL_DESTRUCTOR_P (decl) 2411 || IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) 2412 || DECL_CONV_FN_P (decl)) 2413 && look_for_overrides (ctype, decl) 2414 && !DECL_STATIC_FUNCTION_P (decl)) 2415 /* Set DECL_VINDEX to a value that is neither an INTEGER_CST nor 2416 the error_mark_node so that we know it is an overriding 2417 function. */ 2418 DECL_VINDEX (decl) = decl; 2419 2420 if (DECL_VIRTUAL_P (decl)) 2421 { 2422 if (!DECL_VINDEX (decl)) 2423 DECL_VINDEX (decl) = error_mark_node; 2424 IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = 1; 2425 } 2426 } 2427 2428 /* Warn about hidden virtual functions that are not overridden in t. 2429 We know that constructors and destructors don't apply. */ 2430 2431 static void 2432 warn_hidden (tree t) 2433 { 2434 VEC(tree,gc) *method_vec = CLASSTYPE_METHOD_VEC (t); 2435 tree fns; 2436 size_t i; 2437 2438 /* We go through each separately named virtual function. */ 2439 for (i = CLASSTYPE_FIRST_CONVERSION_SLOT; 2440 VEC_iterate (tree, method_vec, i, fns); 2441 ++i) 2442 { 2443 tree fn; 2444 tree name; 2445 tree fndecl; 2446 tree base_fndecls; 2447 tree base_binfo; 2448 tree binfo; 2449 int j; 2450 2451 /* All functions in this slot in the CLASSTYPE_METHOD_VEC will 2452 have the same name. Figure out what name that is. */ 2453 name = DECL_NAME (OVL_CURRENT (fns)); 2454 /* There are no possibly hidden functions yet. */ 2455 base_fndecls = NULL_TREE; 2456 /* Iterate through all of the base classes looking for possibly 2457 hidden functions. */ 2458 for (binfo = TYPE_BINFO (t), j = 0; 2459 BINFO_BASE_ITERATE (binfo, j, base_binfo); j++) 2460 { 2461 tree basetype = BINFO_TYPE (base_binfo); 2462 base_fndecls = chainon (get_basefndecls (name, basetype), 2463 base_fndecls); 2464 } 2465 2466 /* If there are no functions to hide, continue. */ 2467 if (!base_fndecls) 2468 continue; 2469 2470 /* Remove any overridden functions. */ 2471 for (fn = fns; fn; fn = OVL_NEXT (fn)) 2472 { 2473 fndecl = OVL_CURRENT (fn); 2474 if (DECL_VINDEX (fndecl)) 2475 { 2476 tree *prev = &base_fndecls; 2477 2478 while (*prev) 2479 /* If the method from the base class has the same 2480 signature as the method from the derived class, it 2481 has been overridden. */ 2482 if (same_signature_p (fndecl, TREE_VALUE (*prev))) 2483 *prev = TREE_CHAIN (*prev); 2484 else 2485 prev = &TREE_CHAIN (*prev); 2486 } 2487 } 2488 2489 /* Now give a warning for all base functions without overriders, 2490 as they are hidden. */ 2491 while (base_fndecls) 2492 { 2493 /* Here we know it is a hider, and no overrider exists. */ 2494 warning (OPT_Woverloaded_virtual, "%q+D was hidden", TREE_VALUE (base_fndecls)); 2495 warning (OPT_Woverloaded_virtual, " by %q+D", fns); 2496 base_fndecls = TREE_CHAIN (base_fndecls); 2497 } 2498 } 2499 } 2500 2501 /* Check for things that are invalid. There are probably plenty of other 2502 things we should check for also. */ 2503 2504 static void 2505 finish_struct_anon (tree t) 2506 { 2507 tree field; 2508 2509 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field)) 2510 { 2511 if (TREE_STATIC (field)) 2512 continue; 2513 if (TREE_CODE (field) != FIELD_DECL) 2514 continue; 2515 2516 if (DECL_NAME (field) == NULL_TREE 2517 && ANON_AGGR_TYPE_P (TREE_TYPE (field))) 2518 { 2519 bool is_union = TREE_CODE (TREE_TYPE (field)) == UNION_TYPE; 2520 tree elt = TYPE_FIELDS (TREE_TYPE (field)); 2521 for (; elt; elt = TREE_CHAIN (elt)) 2522 { 2523 /* We're generally only interested in entities the user 2524 declared, but we also find nested classes by noticing 2525 the TYPE_DECL that we create implicitly. You're 2526 allowed to put one anonymous union inside another, 2527 though, so we explicitly tolerate that. We use 2528 TYPE_ANONYMOUS_P rather than ANON_AGGR_TYPE_P so that 2529 we also allow unnamed types used for defining fields. */ 2530 if (DECL_ARTIFICIAL (elt) 2531 && (!DECL_IMPLICIT_TYPEDEF_P (elt) 2532 || TYPE_ANONYMOUS_P (TREE_TYPE (elt)))) 2533 continue; 2534 2535 if (TREE_CODE (elt) != FIELD_DECL) 2536 { 2537 if (is_union) 2538 permerror (input_location, "%q+#D invalid; an anonymous union can " 2539 "only have non-static data members", elt); 2540 else 2541 permerror (input_location, "%q+#D invalid; an anonymous struct can " 2542 "only have non-static data members", elt); 2543 continue; 2544 } 2545 2546 if (TREE_PRIVATE (elt)) 2547 { 2548 if (is_union) 2549 permerror (input_location, "private member %q+#D in anonymous union", elt); 2550 else 2551 permerror (input_location, "private member %q+#D in anonymous struct", elt); 2552 } 2553 else if (TREE_PROTECTED (elt)) 2554 { 2555 if (is_union) 2556 permerror (input_location, "protected member %q+#D in anonymous union", elt); 2557 else 2558 permerror (input_location, "protected member %q+#D in anonymous struct", elt); 2559 } 2560 2561 TREE_PRIVATE (elt) = TREE_PRIVATE (field); 2562 TREE_PROTECTED (elt) = TREE_PROTECTED (field); 2563 } 2564 } 2565 } 2566 } 2567 2568 /* Add T to CLASSTYPE_DECL_LIST of current_class_type which 2569 will be used later during class template instantiation. 2570 When FRIEND_P is zero, T can be a static member data (VAR_DECL), 2571 a non-static member data (FIELD_DECL), a member function 2572 (FUNCTION_DECL), a nested type (RECORD_TYPE, ENUM_TYPE), 2573 a typedef (TYPE_DECL) or a member class template (TEMPLATE_DECL) 2574 When FRIEND_P is nonzero, T is either a friend class 2575 (RECORD_TYPE, TEMPLATE_DECL) or a friend function 2576 (FUNCTION_DECL, TEMPLATE_DECL). */ 2577 2578 void 2579 maybe_add_class_template_decl_list (tree type, tree t, int friend_p) 2580 { 2581 /* Save some memory by not creating TREE_LIST if TYPE is not template. */ 2582 if (CLASSTYPE_TEMPLATE_INFO (type)) 2583 CLASSTYPE_DECL_LIST (type) 2584 = tree_cons (friend_p ? NULL_TREE : type, 2585 t, CLASSTYPE_DECL_LIST (type)); 2586 } 2587 2588 /* Create default constructors, assignment operators, and so forth for 2589 the type indicated by T, if they are needed. CANT_HAVE_CONST_CTOR, 2590 and CANT_HAVE_CONST_ASSIGNMENT are nonzero if, for whatever reason, 2591 the class cannot have a default constructor, copy constructor 2592 taking a const reference argument, or an assignment operator taking 2593 a const reference, respectively. */ 2594 2595 static void 2596 add_implicitly_declared_members (tree t, 2597 int cant_have_const_cctor, 2598 int cant_have_const_assignment) 2599 { 2600 /* Destructor. */ 2601 if (!CLASSTYPE_DESTRUCTORS (t)) 2602 { 2603 /* In general, we create destructors lazily. */ 2604 CLASSTYPE_LAZY_DESTRUCTOR (t) = 1; 2605 /* However, if the implicit destructor is non-trivial 2606 destructor, we sometimes have to create it at this point. */ 2607 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)) 2608 { 2609 bool lazy_p = true; 2610 2611 if (TYPE_FOR_JAVA (t)) 2612 /* If this a Java class, any non-trivial destructor is 2613 invalid, even if compiler-generated. Therefore, if the 2614 destructor is non-trivial we create it now. */ 2615 lazy_p = false; 2616 else 2617 { 2618 tree binfo; 2619 tree base_binfo; 2620 int ix; 2621 2622 /* If the implicit destructor will be virtual, then we must 2623 generate it now because (unfortunately) we do not 2624 generate virtual tables lazily. */ 2625 binfo = TYPE_BINFO (t); 2626 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++) 2627 { 2628 tree base_type; 2629 tree dtor; 2630 2631 base_type = BINFO_TYPE (base_binfo); 2632 dtor = CLASSTYPE_DESTRUCTORS (base_type); 2633 if (dtor && DECL_VIRTUAL_P (dtor)) 2634 { 2635 lazy_p = false; 2636 break; 2637 } 2638 } 2639 } 2640 2641 /* If we can't get away with being lazy, generate the destructor 2642 now. */ 2643 if (!lazy_p) 2644 lazily_declare_fn (sfk_destructor, t); 2645 } 2646 } 2647 2648 /* [class.ctor] 2649 2650 If there is no user-declared constructor for a class, a default 2651 constructor is implicitly declared. */ 2652 if (! TYPE_HAS_USER_CONSTRUCTOR (t)) 2653 { 2654 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 1; 2655 CLASSTYPE_LAZY_DEFAULT_CTOR (t) = 1; 2656 } 2657 2658 /* [class.ctor] 2659 2660 If a class definition does not explicitly declare a copy 2661 constructor, one is declared implicitly. */ 2662 if (! TYPE_HAS_INIT_REF (t) && ! TYPE_FOR_JAVA (t)) 2663 { 2664 TYPE_HAS_INIT_REF (t) = 1; 2665 TYPE_HAS_CONST_INIT_REF (t) = !cant_have_const_cctor; 2666 CLASSTYPE_LAZY_COPY_CTOR (t) = 1; 2667 } 2668 2669 /* Currently only lambdas get a lazy move ctor, but N2987 adds them for 2670 other classes. */ 2671 if (LAMBDA_TYPE_P (t)) 2672 CLASSTYPE_LAZY_MOVE_CTOR (t) = 1; 2673 2674 /* If there is no assignment operator, one will be created if and 2675 when it is needed. For now, just record whether or not the type 2676 of the parameter to the assignment operator will be a const or 2677 non-const reference. */ 2678 if (!TYPE_HAS_ASSIGN_REF (t) && !TYPE_FOR_JAVA (t)) 2679 { 2680 TYPE_HAS_ASSIGN_REF (t) = 1; 2681 TYPE_HAS_CONST_ASSIGN_REF (t) = !cant_have_const_assignment; 2682 CLASSTYPE_LAZY_ASSIGNMENT_OP (t) = 1; 2683 } 2684 } 2685 2686 /* Subroutine of finish_struct_1. Recursively count the number of fields 2687 in TYPE, including anonymous union members. */ 2688 2689 static int 2690 count_fields (tree fields) 2691 { 2692 tree x; 2693 int n_fields = 0; 2694 for (x = fields; x; x = TREE_CHAIN (x)) 2695 { 2696 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x))) 2697 n_fields += count_fields (TYPE_FIELDS (TREE_TYPE (x))); 2698 else 2699 n_fields += 1; 2700 } 2701 return n_fields; 2702 } 2703 2704 /* Subroutine of finish_struct_1. Recursively add all the fields in the 2705 TREE_LIST FIELDS to the SORTED_FIELDS_TYPE elts, starting at offset IDX. */ 2706 2707 static int 2708 add_fields_to_record_type (tree fields, struct sorted_fields_type *field_vec, int idx) 2709 { 2710 tree x; 2711 for (x = fields; x; x = TREE_CHAIN (x)) 2712 { 2713 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x))) 2714 idx = add_fields_to_record_type (TYPE_FIELDS (TREE_TYPE (x)), field_vec, idx); 2715 else 2716 field_vec->elts[idx++] = x; 2717 } 2718 return idx; 2719 } 2720 2721 /* FIELD is a bit-field. We are finishing the processing for its 2722 enclosing type. Issue any appropriate messages and set appropriate 2723 flags. Returns false if an error has been diagnosed. */ 2724 2725 static bool 2726 check_bitfield_decl (tree field) 2727 { 2728 tree type = TREE_TYPE (field); 2729 tree w; 2730 2731 /* Extract the declared width of the bitfield, which has been 2732 temporarily stashed in DECL_INITIAL. */ 2733 w = DECL_INITIAL (field); 2734 gcc_assert (w != NULL_TREE); 2735 /* Remove the bit-field width indicator so that the rest of the 2736 compiler does not treat that value as an initializer. */ 2737 DECL_INITIAL (field) = NULL_TREE; 2738 2739 /* Detect invalid bit-field type. */ 2740 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type)) 2741 { 2742 error ("bit-field %q+#D with non-integral type", field); 2743 w = error_mark_node; 2744 } 2745 else 2746 { 2747 /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs. */ 2748 STRIP_NOPS (w); 2749 2750 /* detect invalid field size. */ 2751 w = integral_constant_value (w); 2752 2753 if (TREE_CODE (w) != INTEGER_CST) 2754 { 2755 error ("bit-field %q+D width not an integer constant", field); 2756 w = error_mark_node; 2757 } 2758 else if (tree_int_cst_sgn (w) < 0) 2759 { 2760 error ("negative width in bit-field %q+D", field); 2761 w = error_mark_node; 2762 } 2763 else if (integer_zerop (w) && DECL_NAME (field) != 0) 2764 { 2765 error ("zero width for bit-field %q+D", field); 2766 w = error_mark_node; 2767 } 2768 else if (compare_tree_int (w, TYPE_PRECISION (type)) > 0 2769 && TREE_CODE (type) != ENUMERAL_TYPE 2770 && TREE_CODE (type) != BOOLEAN_TYPE) 2771 warning (0, "width of %q+D exceeds its type", field); 2772 else if (TREE_CODE (type) == ENUMERAL_TYPE 2773 && (0 > compare_tree_int (w, 2774 tree_int_cst_min_precision 2775 (TYPE_MIN_VALUE (type), 2776 TYPE_UNSIGNED (type))) 2777 || 0 > compare_tree_int (w, 2778 tree_int_cst_min_precision 2779 (TYPE_MAX_VALUE (type), 2780 TYPE_UNSIGNED (type))))) 2781 warning (0, "%q+D is too small to hold all values of %q#T", field, type); 2782 } 2783 2784 if (w != error_mark_node) 2785 { 2786 DECL_SIZE (field) = convert (bitsizetype, w); 2787 DECL_BIT_FIELD (field) = 1; 2788 return true; 2789 } 2790 else 2791 { 2792 /* Non-bit-fields are aligned for their type. */ 2793 DECL_BIT_FIELD (field) = 0; 2794 CLEAR_DECL_C_BIT_FIELD (field); 2795 return false; 2796 } 2797 } 2798 2799 /* FIELD is a non bit-field. We are finishing the processing for its 2800 enclosing type T. Issue any appropriate messages and set appropriate 2801 flags. */ 2802 2803 static void 2804 check_field_decl (tree field, 2805 tree t, 2806 int* cant_have_const_ctor, 2807 int* no_const_asn_ref, 2808 int* any_default_members) 2809 { 2810 tree type = strip_array_types (TREE_TYPE (field)); 2811 2812 /* An anonymous union cannot contain any fields which would change 2813 the settings of CANT_HAVE_CONST_CTOR and friends. */ 2814 if (ANON_UNION_TYPE_P (type)) 2815 ; 2816 /* And, we don't set TYPE_HAS_CONST_INIT_REF, etc., for anonymous 2817 structs. So, we recurse through their fields here. */ 2818 else if (ANON_AGGR_TYPE_P (type)) 2819 { 2820 tree fields; 2821 2822 for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields)) 2823 if (TREE_CODE (fields) == FIELD_DECL && !DECL_C_BIT_FIELD (field)) 2824 check_field_decl (fields, t, cant_have_const_ctor, 2825 no_const_asn_ref, any_default_members); 2826 } 2827 /* Check members with class type for constructors, destructors, 2828 etc. */ 2829 else if (CLASS_TYPE_P (type)) 2830 { 2831 /* Never let anything with uninheritable virtuals 2832 make it through without complaint. */ 2833 abstract_virtuals_error (field, type); 2834 2835 if (TREE_CODE (t) == UNION_TYPE) 2836 { 2837 if (TYPE_NEEDS_CONSTRUCTING (type)) 2838 error ("member %q+#D with constructor not allowed in union", 2839 field); 2840 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)) 2841 error ("member %q+#D with destructor not allowed in union", field); 2842 if (TYPE_HAS_COMPLEX_ASSIGN_REF (type)) 2843 error ("member %q+#D with copy assignment operator not allowed in union", 2844 field); 2845 } 2846 else 2847 { 2848 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type); 2849 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) 2850 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type); 2851 TYPE_HAS_COMPLEX_ASSIGN_REF (t) |= TYPE_HAS_COMPLEX_ASSIGN_REF (type); 2852 TYPE_HAS_COMPLEX_INIT_REF (t) |= TYPE_HAS_COMPLEX_INIT_REF (type); 2853 TYPE_HAS_COMPLEX_DFLT (t) |= TYPE_HAS_COMPLEX_DFLT (type); 2854 } 2855 2856 if (!TYPE_HAS_CONST_INIT_REF (type)) 2857 *cant_have_const_ctor = 1; 2858 2859 if (!TYPE_HAS_CONST_ASSIGN_REF (type)) 2860 *no_const_asn_ref = 1; 2861 } 2862 if (DECL_INITIAL (field) != NULL_TREE) 2863 { 2864 /* `build_class_init_list' does not recognize 2865 non-FIELD_DECLs. */ 2866 if (TREE_CODE (t) == UNION_TYPE && any_default_members != 0) 2867 error ("multiple fields in union %qT initialized", t); 2868 *any_default_members = 1; 2869 } 2870 } 2871 2872 /* Check the data members (both static and non-static), class-scoped 2873 typedefs, etc., appearing in the declaration of T. Issue 2874 appropriate diagnostics. Sets ACCESS_DECLS to a list (in 2875 declaration order) of access declarations; each TREE_VALUE in this 2876 list is a USING_DECL. 2877 2878 In addition, set the following flags: 2879 2880 EMPTY_P 2881 The class is empty, i.e., contains no non-static data members. 2882 2883 CANT_HAVE_CONST_CTOR_P 2884 This class cannot have an implicitly generated copy constructor 2885 taking a const reference. 2886 2887 CANT_HAVE_CONST_ASN_REF 2888 This class cannot have an implicitly generated assignment 2889 operator taking a const reference. 2890 2891 All of these flags should be initialized before calling this 2892 function. 2893 2894 Returns a pointer to the end of the TYPE_FIELDs chain; additional 2895 fields can be added by adding to this chain. */ 2896 2897 static void 2898 check_field_decls (tree t, tree *access_decls, 2899 int *cant_have_const_ctor_p, 2900 int *no_const_asn_ref_p) 2901 { 2902 tree *field; 2903 tree *next; 2904 bool has_pointers; 2905 int any_default_members; 2906 int cant_pack = 0; 2907 int field_access = -1; 2908 2909 /* Assume there are no access declarations. */ 2910 *access_decls = NULL_TREE; 2911 /* Assume this class has no pointer members. */ 2912 has_pointers = false; 2913 /* Assume none of the members of this class have default 2914 initializations. */ 2915 any_default_members = 0; 2916 2917 for (field = &TYPE_FIELDS (t); *field; field = next) 2918 { 2919 tree x = *field; 2920 tree type = TREE_TYPE (x); 2921 int this_field_access; 2922 2923 next = &TREE_CHAIN (x); 2924 2925 if (TREE_CODE (x) == USING_DECL) 2926 { 2927 /* Prune the access declaration from the list of fields. */ 2928 *field = TREE_CHAIN (x); 2929 2930 /* Save the access declarations for our caller. */ 2931 *access_decls = tree_cons (NULL_TREE, x, *access_decls); 2932 2933 /* Since we've reset *FIELD there's no reason to skip to the 2934 next field. */ 2935 next = field; 2936 continue; 2937 } 2938 2939 if (TREE_CODE (x) == TYPE_DECL 2940 || TREE_CODE (x) == TEMPLATE_DECL) 2941 continue; 2942 2943 /* If we've gotten this far, it's a data member, possibly static, 2944 or an enumerator. */ 2945 DECL_CONTEXT (x) = t; 2946 2947 /* When this goes into scope, it will be a non-local reference. */ 2948 DECL_NONLOCAL (x) = 1; 2949 2950 if (TREE_CODE (t) == UNION_TYPE) 2951 { 2952 /* [class.union] 2953 2954 If a union contains a static data member, or a member of 2955 reference type, the program is ill-formed. */ 2956 if (TREE_CODE (x) == VAR_DECL) 2957 { 2958 error ("%q+D may not be static because it is a member of a union", x); 2959 continue; 2960 } 2961 if (TREE_CODE (type) == REFERENCE_TYPE) 2962 { 2963 error ("%q+D may not have reference type %qT because" 2964 " it is a member of a union", 2965 x, type); 2966 continue; 2967 } 2968 } 2969 2970 /* Perform error checking that did not get done in 2971 grokdeclarator. */ 2972 if (TREE_CODE (type) == FUNCTION_TYPE) 2973 { 2974 error ("field %q+D invalidly declared function type", x); 2975 type = build_pointer_type (type); 2976 TREE_TYPE (x) = type; 2977 } 2978 else if (TREE_CODE (type) == METHOD_TYPE) 2979 { 2980 error ("field %q+D invalidly declared method type", x); 2981 type = build_pointer_type (type); 2982 TREE_TYPE (x) = type; 2983 } 2984 2985 if (type == error_mark_node) 2986 continue; 2987 2988 if (TREE_CODE (x) == CONST_DECL || TREE_CODE (x) == VAR_DECL) 2989 continue; 2990 2991 /* Now it can only be a FIELD_DECL. */ 2992 2993 if (TREE_PRIVATE (x) || TREE_PROTECTED (x)) 2994 CLASSTYPE_NON_AGGREGATE (t) = 1; 2995 2996 /* A standard-layout class is a class that: 2997 ... 2998 has the same access control (Clause 11) for all non-static data members, 2999 ... */ 3000 this_field_access = TREE_PROTECTED (x) ? 1 : TREE_PRIVATE (x) ? 2 : 0; 3001 if (field_access == -1) 3002 field_access = this_field_access; 3003 else if (this_field_access != field_access) 3004 CLASSTYPE_NON_STD_LAYOUT (t) = 1; 3005 3006 /* If this is of reference type, check if it needs an init. */ 3007 if (TREE_CODE (type) == REFERENCE_TYPE) 3008 { 3009 CLASSTYPE_NON_LAYOUT_POD_P (t) = 1; 3010 CLASSTYPE_NON_STD_LAYOUT (t) = 1; 3011 if (DECL_INITIAL (x) == NULL_TREE) 3012 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1); 3013 3014 /* ARM $12.6.2: [A member initializer list] (or, for an 3015 aggregate, initialization by a brace-enclosed list) is the 3016 only way to initialize nonstatic const and reference 3017 members. */ 3018 TYPE_HAS_COMPLEX_ASSIGN_REF (t) = 1; 3019 } 3020 3021 type = strip_array_types (type); 3022 3023 if (TYPE_PACKED (t)) 3024 { 3025 if (!layout_pod_type_p (type) && !TYPE_PACKED (type)) 3026 { 3027 warning 3028 (0, 3029 "ignoring packed attribute because of unpacked non-POD field %q+#D", 3030 x); 3031 cant_pack = 1; 3032 } 3033 else if (DECL_C_BIT_FIELD (x) 3034 || TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT) 3035 DECL_PACKED (x) = 1; 3036 } 3037 3038 if (DECL_C_BIT_FIELD (x) && integer_zerop (DECL_INITIAL (x))) 3039 /* We don't treat zero-width bitfields as making a class 3040 non-empty. */ 3041 ; 3042 else 3043 { 3044 /* The class is non-empty. */ 3045 CLASSTYPE_EMPTY_P (t) = 0; 3046 /* The class is not even nearly empty. */ 3047 CLASSTYPE_NEARLY_EMPTY_P (t) = 0; 3048 /* If one of the data members contains an empty class, 3049 so does T. */ 3050 if (CLASS_TYPE_P (type) 3051 && CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type)) 3052 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1; 3053 } 3054 3055 /* This is used by -Weffc++ (see below). Warn only for pointers 3056 to members which might hold dynamic memory. So do not warn 3057 for pointers to functions or pointers to members. */ 3058 if (TYPE_PTR_P (type) 3059 && !TYPE_PTRFN_P (type) 3060 && !TYPE_PTR_TO_MEMBER_P (type)) 3061 has_pointers = true; 3062 3063 if (CLASS_TYPE_P (type)) 3064 { 3065 if (CLASSTYPE_REF_FIELDS_NEED_INIT (type)) 3066 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1); 3067 if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (type)) 3068 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1); 3069 } 3070 3071 if (DECL_MUTABLE_P (x) || TYPE_HAS_MUTABLE_P (type)) 3072 CLASSTYPE_HAS_MUTABLE (t) = 1; 3073 3074 if (! layout_pod_type_p (type)) 3075 /* DR 148 now allows pointers to members (which are POD themselves), 3076 to be allowed in POD structs. */ 3077 CLASSTYPE_NON_LAYOUT_POD_P (t) = 1; 3078 3079 if (!std_layout_type_p (type)) 3080 CLASSTYPE_NON_STD_LAYOUT (t) = 1; 3081 3082 if (! zero_init_p (type)) 3083 CLASSTYPE_NON_ZERO_INIT_P (t) = 1; 3084 3085 /* If any field is const, the structure type is pseudo-const. */ 3086 if (CP_TYPE_CONST_P (type)) 3087 { 3088 C_TYPE_FIELDS_READONLY (t) = 1; 3089 if (DECL_INITIAL (x) == NULL_TREE) 3090 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1); 3091 3092 /* ARM $12.6.2: [A member initializer list] (or, for an 3093 aggregate, initialization by a brace-enclosed list) is the 3094 only way to initialize nonstatic const and reference 3095 members. */ 3096 TYPE_HAS_COMPLEX_ASSIGN_REF (t) = 1; 3097 } 3098 /* A field that is pseudo-const makes the structure likewise. */ 3099 else if (CLASS_TYPE_P (type)) 3100 { 3101 C_TYPE_FIELDS_READONLY (t) |= C_TYPE_FIELDS_READONLY (type); 3102 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 3103 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t) 3104 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (type)); 3105 } 3106 3107 /* Core issue 80: A nonstatic data member is required to have a 3108 different name from the class iff the class has a 3109 user-declared constructor. */ 3110 if (constructor_name_p (DECL_NAME (x), t) 3111 && TYPE_HAS_USER_CONSTRUCTOR (t)) 3112 permerror (input_location, "field %q+#D with same name as class", x); 3113 3114 /* We set DECL_C_BIT_FIELD in grokbitfield. 3115 If the type and width are valid, we'll also set DECL_BIT_FIELD. */ 3116 if (! DECL_C_BIT_FIELD (x) || ! check_bitfield_decl (x)) 3117 check_field_decl (x, t, 3118 cant_have_const_ctor_p, 3119 no_const_asn_ref_p, 3120 &any_default_members); 3121 } 3122 3123 /* Effective C++ rule 11: if a class has dynamic memory held by pointers, 3124 it should also define a copy constructor and an assignment operator to 3125 implement the correct copy semantic (deep vs shallow, etc.). As it is 3126 not feasible to check whether the constructors do allocate dynamic memory 3127 and store it within members, we approximate the warning like this: 3128 3129 -- Warn only if there are members which are pointers 3130 -- Warn only if there is a non-trivial constructor (otherwise, 3131 there cannot be memory allocated). 3132 -- Warn only if there is a non-trivial destructor. We assume that the 3133 user at least implemented the cleanup correctly, and a destructor 3134 is needed to free dynamic memory. 3135 3136 This seems enough for practical purposes. */ 3137 if (warn_ecpp 3138 && has_pointers 3139 && TYPE_HAS_USER_CONSTRUCTOR (t) 3140 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) 3141 && !(TYPE_HAS_INIT_REF (t) && TYPE_HAS_ASSIGN_REF (t))) 3142 { 3143 warning (OPT_Weffc__, "%q#T has pointer data members", t); 3144 3145 if (! TYPE_HAS_INIT_REF (t)) 3146 { 3147 warning (OPT_Weffc__, 3148 " but does not override %<%T(const %T&)%>", t, t); 3149 if (!TYPE_HAS_ASSIGN_REF (t)) 3150 warning (OPT_Weffc__, " or %<operator=(const %T&)%>", t); 3151 } 3152 else if (! TYPE_HAS_ASSIGN_REF (t)) 3153 warning (OPT_Weffc__, 3154 " but does not override %<operator=(const %T&)%>", t); 3155 } 3156 3157 /* If any of the fields couldn't be packed, unset TYPE_PACKED. */ 3158 if (cant_pack) 3159 TYPE_PACKED (t) = 0; 3160 3161 /* Check anonymous struct/anonymous union fields. */ 3162 finish_struct_anon (t); 3163 3164 /* We've built up the list of access declarations in reverse order. 3165 Fix that now. */ 3166 *access_decls = nreverse (*access_decls); 3167 } 3168 3169 /* If TYPE is an empty class type, records its OFFSET in the table of 3170 OFFSETS. */ 3171 3172 static int 3173 record_subobject_offset (tree type, tree offset, splay_tree offsets) 3174 { 3175 splay_tree_node n; 3176 3177 if (!is_empty_class (type)) 3178 return 0; 3179 3180 /* Record the location of this empty object in OFFSETS. */ 3181 n = splay_tree_lookup (offsets, (splay_tree_key) offset); 3182 if (!n) 3183 n = splay_tree_insert (offsets, 3184 (splay_tree_key) offset, 3185 (splay_tree_value) NULL_TREE); 3186 n->value = ((splay_tree_value) 3187 tree_cons (NULL_TREE, 3188 type, 3189 (tree) n->value)); 3190 3191 return 0; 3192 } 3193 3194 /* Returns nonzero if TYPE is an empty class type and there is 3195 already an entry in OFFSETS for the same TYPE as the same OFFSET. */ 3196 3197 static int 3198 check_subobject_offset (tree type, tree offset, splay_tree offsets) 3199 { 3200 splay_tree_node n; 3201 tree t; 3202 3203 if (!is_empty_class (type)) 3204 return 0; 3205 3206 /* Record the location of this empty object in OFFSETS. */ 3207 n = splay_tree_lookup (offsets, (splay_tree_key) offset); 3208 if (!n) 3209 return 0; 3210 3211 for (t = (tree) n->value; t; t = TREE_CHAIN (t)) 3212 if (same_type_p (TREE_VALUE (t), type)) 3213 return 1; 3214 3215 return 0; 3216 } 3217 3218 /* Walk through all the subobjects of TYPE (located at OFFSET). Call 3219 F for every subobject, passing it the type, offset, and table of 3220 OFFSETS. If VBASES_P is one, then virtual non-primary bases should 3221 be traversed. 3222 3223 If MAX_OFFSET is non-NULL, then subobjects with an offset greater 3224 than MAX_OFFSET will not be walked. 3225 3226 If F returns a nonzero value, the traversal ceases, and that value 3227 is returned. Otherwise, returns zero. */ 3228 3229 static int 3230 walk_subobject_offsets (tree type, 3231 subobject_offset_fn f, 3232 tree offset, 3233 splay_tree offsets, 3234 tree max_offset, 3235 int vbases_p) 3236 { 3237 int r = 0; 3238 tree type_binfo = NULL_TREE; 3239 3240 /* If this OFFSET is bigger than the MAX_OFFSET, then we should 3241 stop. */ 3242 if (max_offset && INT_CST_LT (max_offset, offset)) 3243 return 0; 3244 3245 if (type == error_mark_node) 3246 return 0; 3247 3248 if (!TYPE_P (type)) 3249 { 3250 if (abi_version_at_least (2)) 3251 type_binfo = type; 3252 type = BINFO_TYPE (type); 3253 } 3254 3255 if (CLASS_TYPE_P (type)) 3256 { 3257 tree field; 3258 tree binfo; 3259 int i; 3260 3261 /* Avoid recursing into objects that are not interesting. */ 3262 if (!CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type)) 3263 return 0; 3264 3265 /* Record the location of TYPE. */ 3266 r = (*f) (type, offset, offsets); 3267 if (r) 3268 return r; 3269 3270 /* Iterate through the direct base classes of TYPE. */ 3271 if (!type_binfo) 3272 type_binfo = TYPE_BINFO (type); 3273 for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, binfo); i++) 3274 { 3275 tree binfo_offset; 3276 3277 if (abi_version_at_least (2) 3278 && BINFO_VIRTUAL_P (binfo)) 3279 continue; 3280 3281 if (!vbases_p 3282 && BINFO_VIRTUAL_P (binfo) 3283 && !BINFO_PRIMARY_P (binfo)) 3284 continue; 3285 3286 if (!abi_version_at_least (2)) 3287 binfo_offset = size_binop (PLUS_EXPR, 3288 offset, 3289 BINFO_OFFSET (binfo)); 3290 else 3291 { 3292 tree orig_binfo; 3293 /* We cannot rely on BINFO_OFFSET being set for the base 3294 class yet, but the offsets for direct non-virtual 3295 bases can be calculated by going back to the TYPE. */ 3296 orig_binfo = BINFO_BASE_BINFO (TYPE_BINFO (type), i); 3297 binfo_offset = size_binop (PLUS_EXPR, 3298 offset, 3299 BINFO_OFFSET (orig_binfo)); 3300 } 3301 3302 r = walk_subobject_offsets (binfo, 3303 f, 3304 binfo_offset, 3305 offsets, 3306 max_offset, 3307 (abi_version_at_least (2) 3308 ? /*vbases_p=*/0 : vbases_p)); 3309 if (r) 3310 return r; 3311 } 3312 3313 if (abi_version_at_least (2) && CLASSTYPE_VBASECLASSES (type)) 3314 { 3315 unsigned ix; 3316 VEC(tree,gc) *vbases; 3317 3318 /* Iterate through the virtual base classes of TYPE. In G++ 3319 3.2, we included virtual bases in the direct base class 3320 loop above, which results in incorrect results; the 3321 correct offsets for virtual bases are only known when 3322 working with the most derived type. */ 3323 if (vbases_p) 3324 for (vbases = CLASSTYPE_VBASECLASSES (type), ix = 0; 3325 VEC_iterate (tree, vbases, ix, binfo); ix++) 3326 { 3327 r = walk_subobject_offsets (binfo, 3328 f, 3329 size_binop (PLUS_EXPR, 3330 offset, 3331 BINFO_OFFSET (binfo)), 3332 offsets, 3333 max_offset, 3334 /*vbases_p=*/0); 3335 if (r) 3336 return r; 3337 } 3338 else 3339 { 3340 /* We still have to walk the primary base, if it is 3341 virtual. (If it is non-virtual, then it was walked 3342 above.) */ 3343 tree vbase = get_primary_binfo (type_binfo); 3344 3345 if (vbase && BINFO_VIRTUAL_P (vbase) 3346 && BINFO_PRIMARY_P (vbase) 3347 && BINFO_INHERITANCE_CHAIN (vbase) == type_binfo) 3348 { 3349 r = (walk_subobject_offsets 3350 (vbase, f, offset, 3351 offsets, max_offset, /*vbases_p=*/0)); 3352 if (r) 3353 return r; 3354 } 3355 } 3356 } 3357 3358 /* Iterate through the fields of TYPE. */ 3359 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field)) 3360 if (TREE_CODE (field) == FIELD_DECL && !DECL_ARTIFICIAL (field)) 3361 { 3362 tree field_offset; 3363 3364 if (abi_version_at_least (2)) 3365 field_offset = byte_position (field); 3366 else 3367 /* In G++ 3.2, DECL_FIELD_OFFSET was used. */ 3368 field_offset = DECL_FIELD_OFFSET (field); 3369 3370 r = walk_subobject_offsets (TREE_TYPE (field), 3371 f, 3372 size_binop (PLUS_EXPR, 3373 offset, 3374 field_offset), 3375 offsets, 3376 max_offset, 3377 /*vbases_p=*/1); 3378 if (r) 3379 return r; 3380 } 3381 } 3382 else if (TREE_CODE (type) == ARRAY_TYPE) 3383 { 3384 tree element_type = strip_array_types (type); 3385 tree domain = TYPE_DOMAIN (type); 3386 tree index; 3387 3388 /* Avoid recursing into objects that are not interesting. */ 3389 if (!CLASS_TYPE_P (element_type) 3390 || !CLASSTYPE_CONTAINS_EMPTY_CLASS_P (element_type)) 3391 return 0; 3392 3393 /* Step through each of the elements in the array. */ 3394 for (index = size_zero_node; 3395 /* G++ 3.2 had an off-by-one error here. */ 3396 (abi_version_at_least (2) 3397 ? !INT_CST_LT (TYPE_MAX_VALUE (domain), index) 3398 : INT_CST_LT (index, TYPE_MAX_VALUE (domain))); 3399 index = size_binop (PLUS_EXPR, index, size_one_node)) 3400 { 3401 r = walk_subobject_offsets (TREE_TYPE (type), 3402 f, 3403 offset, 3404 offsets, 3405 max_offset, 3406 /*vbases_p=*/1); 3407 if (r) 3408 return r; 3409 offset = size_binop (PLUS_EXPR, offset, 3410 TYPE_SIZE_UNIT (TREE_TYPE (type))); 3411 /* If this new OFFSET is bigger than the MAX_OFFSET, then 3412 there's no point in iterating through the remaining 3413 elements of the array. */ 3414 if (max_offset && INT_CST_LT (max_offset, offset)) 3415 break; 3416 } 3417 } 3418 3419 return 0; 3420 } 3421 3422 /* Record all of the empty subobjects of TYPE (either a type or a 3423 binfo). If IS_DATA_MEMBER is true, then a non-static data member 3424 is being placed at OFFSET; otherwise, it is a base class that is 3425 being placed at OFFSET. */ 3426 3427 static void 3428 record_subobject_offsets (tree type, 3429 tree offset, 3430 splay_tree offsets, 3431 bool is_data_member) 3432 { 3433 tree max_offset; 3434 /* If recording subobjects for a non-static data member or a 3435 non-empty base class , we do not need to record offsets beyond 3436 the size of the biggest empty class. Additional data members 3437 will go at the end of the class. Additional base classes will go 3438 either at offset zero (if empty, in which case they cannot 3439 overlap with offsets past the size of the biggest empty class) or 3440 at the end of the class. 3441 3442 However, if we are placing an empty base class, then we must record 3443 all offsets, as either the empty class is at offset zero (where 3444 other empty classes might later be placed) or at the end of the 3445 class (where other objects might then be placed, so other empty 3446 subobjects might later overlap). */ 3447 if (is_data_member 3448 || !is_empty_class (BINFO_TYPE (type))) 3449 max_offset = sizeof_biggest_empty_class; 3450 else 3451 max_offset = NULL_TREE; 3452 walk_subobject_offsets (type, record_subobject_offset, offset, 3453 offsets, max_offset, is_data_member); 3454 } 3455 3456 /* Returns nonzero if any of the empty subobjects of TYPE (located at 3457 OFFSET) conflict with entries in OFFSETS. If VBASES_P is nonzero, 3458 virtual bases of TYPE are examined. */ 3459 3460 static int 3461 layout_conflict_p (tree type, 3462 tree offset, 3463 splay_tree offsets, 3464 int vbases_p) 3465 { 3466 splay_tree_node max_node; 3467 3468 /* Get the node in OFFSETS that indicates the maximum offset where 3469 an empty subobject is located. */ 3470 max_node = splay_tree_max (offsets); 3471 /* If there aren't any empty subobjects, then there's no point in 3472 performing this check. */ 3473 if (!max_node) 3474 return 0; 3475 3476 return walk_subobject_offsets (type, check_subobject_offset, offset, 3477 offsets, (tree) (max_node->key), 3478 vbases_p); 3479 } 3480 3481 /* DECL is a FIELD_DECL corresponding either to a base subobject of a 3482 non-static data member of the type indicated by RLI. BINFO is the 3483 binfo corresponding to the base subobject, OFFSETS maps offsets to 3484 types already located at those offsets. This function determines 3485 the position of the DECL. */ 3486 3487 static void 3488 layout_nonempty_base_or_field (record_layout_info rli, 3489 tree decl, 3490 tree binfo, 3491 splay_tree offsets) 3492 { 3493 tree offset = NULL_TREE; 3494 bool field_p; 3495 tree type; 3496 3497 if (binfo) 3498 { 3499 /* For the purposes of determining layout conflicts, we want to 3500 use the class type of BINFO; TREE_TYPE (DECL) will be the 3501 CLASSTYPE_AS_BASE version, which does not contain entries for 3502 zero-sized bases. */ 3503 type = TREE_TYPE (binfo); 3504 field_p = false; 3505 } 3506 else 3507 { 3508 type = TREE_TYPE (decl); 3509 field_p = true; 3510 } 3511 3512 /* Try to place the field. It may take more than one try if we have 3513 a hard time placing the field without putting two objects of the 3514 same type at the same address. */ 3515 while (1) 3516 { 3517 struct record_layout_info_s old_rli = *rli; 3518 3519 /* Place this field. */ 3520 place_field (rli, decl); 3521 offset = byte_position (decl); 3522 3523 /* We have to check to see whether or not there is already 3524 something of the same type at the offset we're about to use. 3525 For example, consider: 3526 3527 struct S {}; 3528 struct T : public S { int i; }; 3529 struct U : public S, public T {}; 3530 3531 Here, we put S at offset zero in U. Then, we can't put T at 3532 offset zero -- its S component would be at the same address 3533 as the S we already allocated. So, we have to skip ahead. 3534 Since all data members, including those whose type is an 3535 empty class, have nonzero size, any overlap can happen only 3536 with a direct or indirect base-class -- it can't happen with 3537 a data member. */ 3538 /* In a union, overlap is permitted; all members are placed at 3539 offset zero. */ 3540 if (TREE_CODE (rli->t) == UNION_TYPE) 3541 break; 3542 /* G++ 3.2 did not check for overlaps when placing a non-empty 3543 virtual base. */ 3544 if (!abi_version_at_least (2) && binfo && BINFO_VIRTUAL_P (binfo)) 3545 break; 3546 if (layout_conflict_p (field_p ? type : binfo, offset, 3547 offsets, field_p)) 3548 { 3549 /* Strip off the size allocated to this field. That puts us 3550 at the first place we could have put the field with 3551 proper alignment. */ 3552 *rli = old_rli; 3553 3554 /* Bump up by the alignment required for the type. */ 3555 rli->bitpos 3556 = size_binop (PLUS_EXPR, rli->bitpos, 3557 bitsize_int (binfo 3558 ? CLASSTYPE_ALIGN (type) 3559 : TYPE_ALIGN (type))); 3560 normalize_rli (rli); 3561 } 3562 else 3563 /* There was no conflict. We're done laying out this field. */ 3564 break; 3565 } 3566 3567 /* Now that we know where it will be placed, update its 3568 BINFO_OFFSET. */ 3569 if (binfo && CLASS_TYPE_P (BINFO_TYPE (binfo))) 3570 /* Indirect virtual bases may have a nonzero BINFO_OFFSET at 3571 this point because their BINFO_OFFSET is copied from another 3572 hierarchy. Therefore, we may not need to add the entire 3573 OFFSET. */ 3574 propagate_binfo_offsets (binfo, 3575 size_diffop_loc (input_location, 3576 convert (ssizetype, offset), 3577 convert (ssizetype, 3578 BINFO_OFFSET (binfo)))); 3579 } 3580 3581 /* Returns true if TYPE is empty and OFFSET is nonzero. */ 3582 3583 static int 3584 empty_base_at_nonzero_offset_p (tree type, 3585 tree offset, 3586 splay_tree offsets ATTRIBUTE_UNUSED) 3587 { 3588 return is_empty_class (type) && !integer_zerop (offset); 3589 } 3590 3591 /* Layout the empty base BINFO. EOC indicates the byte currently just 3592 past the end of the class, and should be correctly aligned for a 3593 class of the type indicated by BINFO; OFFSETS gives the offsets of 3594 the empty bases allocated so far. T is the most derived 3595 type. Return nonzero iff we added it at the end. */ 3596 3597 static bool 3598 layout_empty_base (record_layout_info rli, tree binfo, 3599 tree eoc, splay_tree offsets) 3600 { 3601 tree alignment; 3602 tree basetype = BINFO_TYPE (binfo); 3603 bool atend = false; 3604 3605 /* This routine should only be used for empty classes. */ 3606 gcc_assert (is_empty_class (basetype)); 3607 alignment = ssize_int (CLASSTYPE_ALIGN_UNIT (basetype)); 3608 3609 if (!integer_zerop (BINFO_OFFSET (binfo))) 3610 { 3611 if (abi_version_at_least (2)) 3612 propagate_binfo_offsets 3613 (binfo, size_diffop_loc (input_location, 3614 size_zero_node, BINFO_OFFSET (binfo))); 3615 else 3616 warning (OPT_Wabi, 3617 "offset of empty base %qT may not be ABI-compliant and may" 3618 "change in a future version of GCC", 3619 BINFO_TYPE (binfo)); 3620 } 3621 3622 /* This is an empty base class. We first try to put it at offset 3623 zero. */ 3624 if (layout_conflict_p (binfo, 3625 BINFO_OFFSET (binfo), 3626 offsets, 3627 /*vbases_p=*/0)) 3628 { 3629 /* That didn't work. Now, we move forward from the next 3630 available spot in the class. */ 3631 atend = true; 3632 propagate_binfo_offsets (binfo, convert (ssizetype, eoc)); 3633 while (1) 3634 { 3635 if (!layout_conflict_p (binfo, 3636 BINFO_OFFSET (binfo), 3637 offsets, 3638 /*vbases_p=*/0)) 3639 /* We finally found a spot where there's no overlap. */ 3640 break; 3641 3642 /* There's overlap here, too. Bump along to the next spot. */ 3643 propagate_binfo_offsets (binfo, alignment); 3644 } 3645 } 3646 3647 if (CLASSTYPE_USER_ALIGN (basetype)) 3648 { 3649 rli->record_align = MAX (rli->record_align, CLASSTYPE_ALIGN (basetype)); 3650 if (warn_packed) 3651 rli->unpacked_align = MAX (rli->unpacked_align, CLASSTYPE_ALIGN (basetype)); 3652 TYPE_USER_ALIGN (rli->t) = 1; 3653 } 3654 3655 return atend; 3656 } 3657 3658 /* Layout the base given by BINFO in the class indicated by RLI. 3659 *BASE_ALIGN is a running maximum of the alignments of 3660 any base class. OFFSETS gives the location of empty base 3661 subobjects. T is the most derived type. Return nonzero if the new 3662 object cannot be nearly-empty. A new FIELD_DECL is inserted at 3663 *NEXT_FIELD, unless BINFO is for an empty base class. 3664 3665 Returns the location at which the next field should be inserted. */ 3666 3667 static tree * 3668 build_base_field (record_layout_info rli, tree binfo, 3669 splay_tree offsets, tree *next_field) 3670 { 3671 tree t = rli->t; 3672 tree basetype = BINFO_TYPE (binfo); 3673 3674 if (!COMPLETE_TYPE_P (basetype)) 3675 /* This error is now reported in xref_tag, thus giving better 3676 location information. */ 3677 return next_field; 3678 3679 /* Place the base class. */ 3680 if (!is_empty_class (basetype)) 3681 { 3682 tree decl; 3683 3684 /* The containing class is non-empty because it has a non-empty 3685 base class. */ 3686 CLASSTYPE_EMPTY_P (t) = 0; 3687 3688 /* Create the FIELD_DECL. */ 3689 decl = build_decl (input_location, 3690 FIELD_DECL, NULL_TREE, CLASSTYPE_AS_BASE (basetype)); 3691 DECL_ARTIFICIAL (decl) = 1; 3692 DECL_IGNORED_P (decl) = 1; 3693 DECL_FIELD_CONTEXT (decl) = t; 3694 if (CLASSTYPE_AS_BASE (basetype)) 3695 { 3696 DECL_SIZE (decl) = CLASSTYPE_SIZE (basetype); 3697 DECL_SIZE_UNIT (decl) = CLASSTYPE_SIZE_UNIT (basetype); 3698 DECL_ALIGN (decl) = CLASSTYPE_ALIGN (basetype); 3699 DECL_USER_ALIGN (decl) = CLASSTYPE_USER_ALIGN (basetype); 3700 DECL_MODE (decl) = TYPE_MODE (basetype); 3701 DECL_FIELD_IS_BASE (decl) = 1; 3702 3703 /* Try to place the field. It may take more than one try if we 3704 have a hard time placing the field without putting two 3705 objects of the same type at the same address. */ 3706 layout_nonempty_base_or_field (rli, decl, binfo, offsets); 3707 /* Add the new FIELD_DECL to the list of fields for T. */ 3708 TREE_CHAIN (decl) = *next_field; 3709 *next_field = decl; 3710 next_field = &TREE_CHAIN (decl); 3711 } 3712 } 3713 else 3714 { 3715 tree eoc; 3716 bool atend; 3717 3718 /* On some platforms (ARM), even empty classes will not be 3719 byte-aligned. */ 3720 eoc = round_up_loc (input_location, 3721 rli_size_unit_so_far (rli), 3722 CLASSTYPE_ALIGN_UNIT (basetype)); 3723 atend = layout_empty_base (rli, binfo, eoc, offsets); 3724 /* A nearly-empty class "has no proper base class that is empty, 3725 not morally virtual, and at an offset other than zero." */ 3726 if (!BINFO_VIRTUAL_P (binfo) && CLASSTYPE_NEARLY_EMPTY_P (t)) 3727 { 3728 if (atend) 3729 CLASSTYPE_NEARLY_EMPTY_P (t) = 0; 3730 /* The check above (used in G++ 3.2) is insufficient because 3731 an empty class placed at offset zero might itself have an 3732 empty base at a nonzero offset. */ 3733 else if (walk_subobject_offsets (basetype, 3734 empty_base_at_nonzero_offset_p, 3735 size_zero_node, 3736 /*offsets=*/NULL, 3737 /*max_offset=*/NULL_TREE, 3738 /*vbases_p=*/true)) 3739 { 3740 if (abi_version_at_least (2)) 3741 CLASSTYPE_NEARLY_EMPTY_P (t) = 0; 3742 else 3743 warning (OPT_Wabi, 3744 "class %qT will be considered nearly empty in a " 3745 "future version of GCC", t); 3746 } 3747 } 3748 3749 /* We do not create a FIELD_DECL for empty base classes because 3750 it might overlap some other field. We want to be able to 3751 create CONSTRUCTORs for the class by iterating over the 3752 FIELD_DECLs, and the back end does not handle overlapping 3753 FIELD_DECLs. */ 3754 3755 /* An empty virtual base causes a class to be non-empty 3756 -- but in that case we do not need to clear CLASSTYPE_EMPTY_P 3757 here because that was already done when the virtual table 3758 pointer was created. */ 3759 } 3760 3761 /* Record the offsets of BINFO and its base subobjects. */ 3762 record_subobject_offsets (binfo, 3763 BINFO_OFFSET (binfo), 3764 offsets, 3765 /*is_data_member=*/false); 3766 3767 return next_field; 3768 } 3769 3770 /* Layout all of the non-virtual base classes. Record empty 3771 subobjects in OFFSETS. T is the most derived type. Return nonzero 3772 if the type cannot be nearly empty. The fields created 3773 corresponding to the base classes will be inserted at 3774 *NEXT_FIELD. */ 3775 3776 static void 3777 build_base_fields (record_layout_info rli, 3778 splay_tree offsets, tree *next_field) 3779 { 3780 /* Chain to hold all the new FIELD_DECLs which stand in for base class 3781 subobjects. */ 3782 tree t = rli->t; 3783 int n_baseclasses = BINFO_N_BASE_BINFOS (TYPE_BINFO (t)); 3784 int i; 3785 3786 /* The primary base class is always allocated first. */ 3787 if (CLASSTYPE_HAS_PRIMARY_BASE_P (t)) 3788 next_field = build_base_field (rli, CLASSTYPE_PRIMARY_BINFO (t), 3789 offsets, next_field); 3790 3791 /* Now allocate the rest of the bases. */ 3792 for (i = 0; i < n_baseclasses; ++i) 3793 { 3794 tree base_binfo; 3795 3796 base_binfo = BINFO_BASE_BINFO (TYPE_BINFO (t), i); 3797 3798 /* The primary base was already allocated above, so we don't 3799 need to allocate it again here. */ 3800 if (base_binfo == CLASSTYPE_PRIMARY_BINFO (t)) 3801 continue; 3802 3803 /* Virtual bases are added at the end (a primary virtual base 3804 will have already been added). */ 3805 if (BINFO_VIRTUAL_P (base_binfo)) 3806 continue; 3807 3808 next_field = build_base_field (rli, base_binfo, 3809 offsets, next_field); 3810 } 3811 } 3812 3813 /* Go through the TYPE_METHODS of T issuing any appropriate 3814 diagnostics, figuring out which methods override which other 3815 methods, and so forth. */ 3816 3817 static void 3818 check_methods (tree t) 3819 { 3820 tree x; 3821 3822 for (x = TYPE_METHODS (t); x; x = TREE_CHAIN (x)) 3823 { 3824 check_for_override (x, t); 3825 if (DECL_PURE_VIRTUAL_P (x) && ! DECL_VINDEX (x)) 3826 error ("initializer specified for non-virtual method %q+D", x); 3827 /* The name of the field is the original field name 3828 Save this in auxiliary field for later overloading. */ 3829 if (DECL_VINDEX (x)) 3830 { 3831 TYPE_POLYMORPHIC_P (t) = 1; 3832 if (DECL_PURE_VIRTUAL_P (x)) 3833 VEC_safe_push (tree, gc, CLASSTYPE_PURE_VIRTUALS (t), x); 3834 } 3835 /* All user-provided destructors are non-trivial. */ 3836 if (DECL_DESTRUCTOR_P (x) && user_provided_p (x)) 3837 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = 1; 3838 } 3839 } 3840 3841 /* FN is a constructor or destructor. Clone the declaration to create 3842 a specialized in-charge or not-in-charge version, as indicated by 3843 NAME. */ 3844 3845 static tree 3846 build_clone (tree fn, tree name) 3847 { 3848 tree parms; 3849 tree clone; 3850 3851 /* Copy the function. */ 3852 clone = copy_decl (fn); 3853 /* Reset the function name. */ 3854 DECL_NAME (clone) = name; 3855 SET_DECL_ASSEMBLER_NAME (clone, NULL_TREE); 3856 /* Remember where this function came from. */ 3857 DECL_ABSTRACT_ORIGIN (clone) = fn; 3858 /* Make it easy to find the CLONE given the FN. */ 3859 TREE_CHAIN (clone) = TREE_CHAIN (fn); 3860 TREE_CHAIN (fn) = clone; 3861 3862 /* If this is a template, do the rest on the DECL_TEMPLATE_RESULT. */ 3863 if (TREE_CODE (clone) == TEMPLATE_DECL) 3864 { 3865 tree result = build_clone (DECL_TEMPLATE_RESULT (clone), name); 3866 DECL_TEMPLATE_RESULT (clone) = result; 3867 DECL_TEMPLATE_INFO (result) = copy_node (DECL_TEMPLATE_INFO (result)); 3868 DECL_TI_TEMPLATE (result) = clone; 3869 TREE_TYPE (clone) = TREE_TYPE (result); 3870 return clone; 3871 } 3872 3873 DECL_CLONED_FUNCTION (clone) = fn; 3874 /* There's no pending inline data for this function. */ 3875 DECL_PENDING_INLINE_INFO (clone) = NULL; 3876 DECL_PENDING_INLINE_P (clone) = 0; 3877 3878 /* The base-class destructor is not virtual. */ 3879 if (name == base_dtor_identifier) 3880 { 3881 DECL_VIRTUAL_P (clone) = 0; 3882 if (TREE_CODE (clone) != TEMPLATE_DECL) 3883 DECL_VINDEX (clone) = NULL_TREE; 3884 } 3885 3886 /* If there was an in-charge parameter, drop it from the function 3887 type. */ 3888 if (DECL_HAS_IN_CHARGE_PARM_P (clone)) 3889 { 3890 tree basetype; 3891 tree parmtypes; 3892 tree exceptions; 3893 3894 exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone)); 3895 basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone)); 3896 parmtypes = TYPE_ARG_TYPES (TREE_TYPE (clone)); 3897 /* Skip the `this' parameter. */ 3898 parmtypes = TREE_CHAIN (parmtypes); 3899 /* Skip the in-charge parameter. */ 3900 parmtypes = TREE_CHAIN (parmtypes); 3901 /* And the VTT parm, in a complete [cd]tor. */ 3902 if (DECL_HAS_VTT_PARM_P (fn) 3903 && ! DECL_NEEDS_VTT_PARM_P (clone)) 3904 parmtypes = TREE_CHAIN (parmtypes); 3905 /* If this is subobject constructor or destructor, add the vtt 3906 parameter. */ 3907 TREE_TYPE (clone) 3908 = build_method_type_directly (basetype, 3909 TREE_TYPE (TREE_TYPE (clone)), 3910 parmtypes); 3911 if (exceptions) 3912 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), 3913 exceptions); 3914 TREE_TYPE (clone) 3915 = cp_build_type_attribute_variant (TREE_TYPE (clone), 3916 TYPE_ATTRIBUTES (TREE_TYPE (fn))); 3917 } 3918 3919 /* Copy the function parameters. */ 3920 DECL_ARGUMENTS (clone) = copy_list (DECL_ARGUMENTS (clone)); 3921 /* Remove the in-charge parameter. */ 3922 if (DECL_HAS_IN_CHARGE_PARM_P (clone)) 3923 { 3924 TREE_CHAIN (DECL_ARGUMENTS (clone)) 3925 = TREE_CHAIN (TREE_CHAIN (DECL_ARGUMENTS (clone))); 3926 DECL_HAS_IN_CHARGE_PARM_P (clone) = 0; 3927 } 3928 /* And the VTT parm, in a complete [cd]tor. */ 3929 if (DECL_HAS_VTT_PARM_P (fn)) 3930 { 3931 if (DECL_NEEDS_VTT_PARM_P (clone)) 3932 DECL_HAS_VTT_PARM_P (clone) = 1; 3933 else 3934 { 3935 TREE_CHAIN (DECL_ARGUMENTS (clone)) 3936 = TREE_CHAIN (TREE_CHAIN (DECL_ARGUMENTS (clone))); 3937 DECL_HAS_VTT_PARM_P (clone) = 0; 3938 } 3939 } 3940 3941 for (parms = DECL_ARGUMENTS (clone); parms; parms = TREE_CHAIN (parms)) 3942 { 3943 DECL_CONTEXT (parms) = clone; 3944 cxx_dup_lang_specific_decl (parms); 3945 } 3946 3947 /* Create the RTL for this function. */ 3948 SET_DECL_RTL (clone, NULL_RTX); 3949 rest_of_decl_compilation (clone, /*top_level=*/1, at_eof); 3950 3951 if (pch_file) 3952 note_decl_for_pch (clone); 3953 3954 return clone; 3955 } 3956 3957 /* Implementation of DECL_CLONED_FUNCTION and DECL_CLONED_FUNCTION_P, do 3958 not invoke this function directly. 3959 3960 For a non-thunk function, returns the address of the slot for storing 3961 the function it is a clone of. Otherwise returns NULL_TREE. 3962 3963 If JUST_TESTING, looks through TEMPLATE_DECL and returns NULL if 3964 cloned_function is unset. This is to support the separate 3965 DECL_CLONED_FUNCTION and DECL_CLONED_FUNCTION_P modes; using the latter 3966 on a template makes sense, but not the former. */ 3967 3968 tree * 3969 decl_cloned_function_p (const_tree decl, bool just_testing) 3970 { 3971 tree *ptr; 3972 if (just_testing) 3973 decl = STRIP_TEMPLATE (decl); 3974 3975 if (TREE_CODE (decl) != FUNCTION_DECL 3976 || !DECL_LANG_SPECIFIC (decl) 3977 || DECL_LANG_SPECIFIC (decl)->u.fn.thunk_p) 3978 { 3979 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007) 3980 if (!just_testing) 3981 lang_check_failed (__FILE__, __LINE__, __FUNCTION__); 3982 else 3983 #endif 3984 return NULL; 3985 } 3986 3987 ptr = &DECL_LANG_SPECIFIC (decl)->u.fn.u5.cloned_function; 3988 if (just_testing && *ptr == NULL_TREE) 3989 return NULL; 3990 else 3991 return ptr; 3992 } 3993 3994 /* Produce declarations for all appropriate clones of FN. If 3995 UPDATE_METHOD_VEC_P is nonzero, the clones are added to the 3996 CLASTYPE_METHOD_VEC as well. */ 3997 3998 void 3999 clone_function_decl (tree fn, int update_method_vec_p) 4000 { 4001 tree clone; 4002 4003 /* Avoid inappropriate cloning. */ 4004 if (TREE_CHAIN (fn) 4005 && DECL_CLONED_FUNCTION_P (TREE_CHAIN (fn))) 4006 return; 4007 4008 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn)) 4009 { 4010 /* For each constructor, we need two variants: an in-charge version 4011 and a not-in-charge version. */ 4012 clone = build_clone (fn, complete_ctor_identifier); 4013 if (update_method_vec_p) 4014 add_method (DECL_CONTEXT (clone), clone, NULL_TREE); 4015 clone = build_clone (fn, base_ctor_identifier); 4016 if (update_method_vec_p) 4017 add_method (DECL_CONTEXT (clone), clone, NULL_TREE); 4018 } 4019 else 4020 { 4021 gcc_assert (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn)); 4022 4023 /* For each destructor, we need three variants: an in-charge 4024 version, a not-in-charge version, and an in-charge deleting 4025 version. We clone the deleting version first because that 4026 means it will go second on the TYPE_METHODS list -- and that 4027 corresponds to the correct layout order in the virtual 4028 function table. 4029 4030 For a non-virtual destructor, we do not build a deleting 4031 destructor. */ 4032 if (DECL_VIRTUAL_P (fn)) 4033 { 4034 clone = build_clone (fn, deleting_dtor_identifier); 4035 if (update_method_vec_p) 4036 add_method (DECL_CONTEXT (clone), clone, NULL_TREE); 4037 } 4038 clone = build_clone (fn, complete_dtor_identifier); 4039 if (update_method_vec_p) 4040 add_method (DECL_CONTEXT (clone), clone, NULL_TREE); 4041 clone = build_clone (fn, base_dtor_identifier); 4042 if (update_method_vec_p) 4043 add_method (DECL_CONTEXT (clone), clone, NULL_TREE); 4044 } 4045 4046 /* Note that this is an abstract function that is never emitted. */ 4047 DECL_ABSTRACT (fn) = 1; 4048 } 4049 4050 /* DECL is an in charge constructor, which is being defined. This will 4051 have had an in class declaration, from whence clones were 4052 declared. An out-of-class definition can specify additional default 4053 arguments. As it is the clones that are involved in overload 4054 resolution, we must propagate the information from the DECL to its 4055 clones. */ 4056 4057 void 4058 adjust_clone_args (tree decl) 4059 { 4060 tree clone; 4061 4062 for (clone = TREE_CHAIN (decl); clone && DECL_CLONED_FUNCTION_P (clone); 4063 clone = TREE_CHAIN (clone)) 4064 { 4065 tree orig_clone_parms = TYPE_ARG_TYPES (TREE_TYPE (clone)); 4066 tree orig_decl_parms = TYPE_ARG_TYPES (TREE_TYPE (decl)); 4067 tree decl_parms, clone_parms; 4068 4069 clone_parms = orig_clone_parms; 4070 4071 /* Skip the 'this' parameter. */ 4072 orig_clone_parms = TREE_CHAIN (orig_clone_parms); 4073 orig_decl_parms = TREE_CHAIN (orig_decl_parms); 4074 4075 if (DECL_HAS_IN_CHARGE_PARM_P (decl)) 4076 orig_decl_parms = TREE_CHAIN (orig_decl_parms); 4077 if (DECL_HAS_VTT_PARM_P (decl)) 4078 orig_decl_parms = TREE_CHAIN (orig_decl_parms); 4079 4080 clone_parms = orig_clone_parms; 4081 if (DECL_HAS_VTT_PARM_P (clone)) 4082 clone_parms = TREE_CHAIN (clone_parms); 4083 4084 for (decl_parms = orig_decl_parms; decl_parms; 4085 decl_parms = TREE_CHAIN (decl_parms), 4086 clone_parms = TREE_CHAIN (clone_parms)) 4087 { 4088 gcc_assert (same_type_p (TREE_TYPE (decl_parms), 4089 TREE_TYPE (clone_parms))); 4090 4091 if (TREE_PURPOSE (decl_parms) && !TREE_PURPOSE (clone_parms)) 4092 { 4093 /* A default parameter has been added. Adjust the 4094 clone's parameters. */ 4095 tree exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone)); 4096 tree attrs = TYPE_ATTRIBUTES (TREE_TYPE (clone)); 4097 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone)); 4098 tree type; 4099 4100 clone_parms = orig_decl_parms; 4101 4102 if (DECL_HAS_VTT_PARM_P (clone)) 4103 { 4104 clone_parms = tree_cons (TREE_PURPOSE (orig_clone_parms), 4105 TREE_VALUE (orig_clone_parms), 4106 clone_parms); 4107 TREE_TYPE (clone_parms) = TREE_TYPE (orig_clone_parms); 4108 } 4109 type = build_method_type_directly (basetype, 4110 TREE_TYPE (TREE_TYPE (clone)), 4111 clone_parms); 4112 if (exceptions) 4113 type = build_exception_variant (type, exceptions); 4114 if (attrs) 4115 type = cp_build_type_attribute_variant (type, attrs); 4116 TREE_TYPE (clone) = type; 4117 4118 clone_parms = NULL_TREE; 4119 break; 4120 } 4121 } 4122 gcc_assert (!clone_parms); 4123 } 4124 } 4125 4126 /* For each of the constructors and destructors in T, create an 4127 in-charge and not-in-charge variant. */ 4128 4129 static void 4130 clone_constructors_and_destructors (tree t) 4131 { 4132 tree fns; 4133 4134 /* If for some reason we don't have a CLASSTYPE_METHOD_VEC, we bail 4135 out now. */ 4136 if (!CLASSTYPE_METHOD_VEC (t)) 4137 return; 4138 4139 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns)) 4140 clone_function_decl (OVL_CURRENT (fns), /*update_method_vec_p=*/1); 4141 for (fns = CLASSTYPE_DESTRUCTORS (t); fns; fns = OVL_NEXT (fns)) 4142 clone_function_decl (OVL_CURRENT (fns), /*update_method_vec_p=*/1); 4143 } 4144 4145 /* Returns true iff class T has a user-defined constructor other than 4146 the default constructor. */ 4147 4148 bool 4149 type_has_user_nondefault_constructor (tree t) 4150 { 4151 tree fns; 4152 4153 if (!TYPE_HAS_USER_CONSTRUCTOR (t)) 4154 return false; 4155 4156 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns)) 4157 { 4158 tree fn = OVL_CURRENT (fns); 4159 if (!DECL_ARTIFICIAL (fn) 4160 && (TREE_CODE (fn) == TEMPLATE_DECL 4161 || (skip_artificial_parms_for (fn, DECL_ARGUMENTS (fn)) 4162 != NULL_TREE))) 4163 return true; 4164 } 4165 4166 return false; 4167 } 4168 4169 /* Returns true iff FN is a user-provided function, i.e. user-declared 4170 and not defaulted at its first declaration; or explicit, private, 4171 protected, or non-const. */ 4172 4173 bool 4174 user_provided_p (tree fn) 4175 { 4176 if (TREE_CODE (fn) == TEMPLATE_DECL) 4177 return true; 4178 else 4179 return (!DECL_ARTIFICIAL (fn) 4180 && !DECL_DEFAULTED_IN_CLASS_P (fn)); 4181 } 4182 4183 /* Returns true iff class T has a user-provided constructor. */ 4184 4185 bool 4186 type_has_user_provided_constructor (tree t) 4187 { 4188 tree fns; 4189 4190 if (!CLASS_TYPE_P (t)) 4191 return false; 4192 4193 if (!TYPE_HAS_USER_CONSTRUCTOR (t)) 4194 return false; 4195 4196 /* This can happen in error cases; avoid crashing. */ 4197 if (!CLASSTYPE_METHOD_VEC (t)) 4198 return false; 4199 4200 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns)) 4201 if (user_provided_p (OVL_CURRENT (fns))) 4202 return true; 4203 4204 return false; 4205 } 4206 4207 /* Returns true iff class T has a user-provided default constructor. */ 4208 4209 bool 4210 type_has_user_provided_default_constructor (tree t) 4211 { 4212 tree fns, args; 4213 4214 if (!TYPE_HAS_USER_CONSTRUCTOR (t)) 4215 return false; 4216 4217 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns)) 4218 { 4219 tree fn = OVL_CURRENT (fns); 4220 if (TREE_CODE (fn) == FUNCTION_DECL 4221 && user_provided_p (fn)) 4222 { 4223 args = FUNCTION_FIRST_USER_PARMTYPE (fn); 4224 while (args && TREE_PURPOSE (args)) 4225 args = TREE_CHAIN (args); 4226 if (!args || args == void_list_node) 4227 return true; 4228 } 4229 } 4230 4231 return false; 4232 } 4233 4234 /* Remove all zero-width bit-fields from T. */ 4235 4236 static void 4237 remove_zero_width_bit_fields (tree t) 4238 { 4239 tree *fieldsp; 4240 4241 fieldsp = &TYPE_FIELDS (t); 4242 while (*fieldsp) 4243 { 4244 if (TREE_CODE (*fieldsp) == FIELD_DECL 4245 && DECL_C_BIT_FIELD (*fieldsp) 4246 /* We should not be confused by the fact that grokbitfield 4247 temporarily sets the width of the bit field into 4248 DECL_INITIAL (*fieldsp). 4249 check_bitfield_decl eventually sets DECL_SIZE (*fieldsp) 4250 to that width. */ 4251 && integer_zerop (DECL_SIZE (*fieldsp))) 4252 *fieldsp = TREE_CHAIN (*fieldsp); 4253 else 4254 fieldsp = &TREE_CHAIN (*fieldsp); 4255 } 4256 } 4257 4258 /* Returns TRUE iff we need a cookie when dynamically allocating an 4259 array whose elements have the indicated class TYPE. */ 4260 4261 static bool 4262 type_requires_array_cookie (tree type) 4263 { 4264 tree fns; 4265 bool has_two_argument_delete_p = false; 4266 4267 gcc_assert (CLASS_TYPE_P (type)); 4268 4269 /* If there's a non-trivial destructor, we need a cookie. In order 4270 to iterate through the array calling the destructor for each 4271 element, we'll have to know how many elements there are. */ 4272 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)) 4273 return true; 4274 4275 /* If the usual deallocation function is a two-argument whose second 4276 argument is of type `size_t', then we have to pass the size of 4277 the array to the deallocation function, so we will need to store 4278 a cookie. */ 4279 fns = lookup_fnfields (TYPE_BINFO (type), 4280 ansi_opname (VEC_DELETE_EXPR), 4281 /*protect=*/0); 4282 /* If there are no `operator []' members, or the lookup is 4283 ambiguous, then we don't need a cookie. */ 4284 if (!fns || fns == error_mark_node) 4285 return false; 4286 /* Loop through all of the functions. */ 4287 for (fns = BASELINK_FUNCTIONS (fns); fns; fns = OVL_NEXT (fns)) 4288 { 4289 tree fn; 4290 tree second_parm; 4291 4292 /* Select the current function. */ 4293 fn = OVL_CURRENT (fns); 4294 /* See if this function is a one-argument delete function. If 4295 it is, then it will be the usual deallocation function. */ 4296 second_parm = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fn))); 4297 if (second_parm == void_list_node) 4298 return false; 4299 /* Do not consider this function if its second argument is an 4300 ellipsis. */ 4301 if (!second_parm) 4302 continue; 4303 /* Otherwise, if we have a two-argument function and the second 4304 argument is `size_t', it will be the usual deallocation 4305 function -- unless there is one-argument function, too. */ 4306 if (TREE_CHAIN (second_parm) == void_list_node 4307 && same_type_p (TREE_VALUE (second_parm), size_type_node)) 4308 has_two_argument_delete_p = true; 4309 } 4310 4311 return has_two_argument_delete_p; 4312 } 4313 4314 /* Check the validity of the bases and members declared in T. Add any 4315 implicitly-generated functions (like copy-constructors and 4316 assignment operators). Compute various flag bits (like 4317 CLASSTYPE_NON_LAYOUT_POD_T) for T. This routine works purely at the C++ 4318 level: i.e., independently of the ABI in use. */ 4319 4320 static void 4321 check_bases_and_members (tree t) 4322 { 4323 /* Nonzero if the implicitly generated copy constructor should take 4324 a non-const reference argument. */ 4325 int cant_have_const_ctor; 4326 /* Nonzero if the implicitly generated assignment operator 4327 should take a non-const reference argument. */ 4328 int no_const_asn_ref; 4329 tree access_decls; 4330 bool saved_complex_asn_ref; 4331 bool saved_nontrivial_dtor; 4332 tree fn; 4333 4334 /* By default, we use const reference arguments and generate default 4335 constructors. */ 4336 cant_have_const_ctor = 0; 4337 no_const_asn_ref = 0; 4338 4339 /* Check all the base-classes. */ 4340 check_bases (t, &cant_have_const_ctor, 4341 &no_const_asn_ref); 4342 4343 /* Check all the method declarations. */ 4344 check_methods (t); 4345 4346 /* Save the initial values of these flags which only indicate whether 4347 or not the class has user-provided functions. As we analyze the 4348 bases and members we can set these flags for other reasons. */ 4349 saved_complex_asn_ref = TYPE_HAS_COMPLEX_ASSIGN_REF (t); 4350 saved_nontrivial_dtor = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t); 4351 4352 /* Check all the data member declarations. We cannot call 4353 check_field_decls until we have called check_bases check_methods, 4354 as check_field_decls depends on TYPE_HAS_NONTRIVIAL_DESTRUCTOR 4355 being set appropriately. */ 4356 check_field_decls (t, &access_decls, 4357 &cant_have_const_ctor, 4358 &no_const_asn_ref); 4359 4360 /* A nearly-empty class has to be vptr-containing; a nearly empty 4361 class contains just a vptr. */ 4362 if (!TYPE_CONTAINS_VPTR_P (t)) 4363 CLASSTYPE_NEARLY_EMPTY_P (t) = 0; 4364 4365 /* Do some bookkeeping that will guide the generation of implicitly 4366 declared member functions. */ 4367 TYPE_HAS_COMPLEX_INIT_REF (t) |= TYPE_CONTAINS_VPTR_P (t); 4368 /* We need to call a constructor for this class if it has a 4369 user-provided constructor, or if the default constructor is going 4370 to initialize the vptr. (This is not an if-and-only-if; 4371 TYPE_NEEDS_CONSTRUCTING is set elsewhere if bases or members 4372 themselves need constructing.) */ 4373 TYPE_NEEDS_CONSTRUCTING (t) 4374 |= (type_has_user_provided_constructor (t) || TYPE_CONTAINS_VPTR_P (t)); 4375 /* [dcl.init.aggr] 4376 4377 An aggregate is an array or a class with no user-provided 4378 constructors ... and no virtual functions. 4379 4380 Again, other conditions for being an aggregate are checked 4381 elsewhere. */ 4382 CLASSTYPE_NON_AGGREGATE (t) 4383 |= (type_has_user_provided_constructor (t) || TYPE_POLYMORPHIC_P (t)); 4384 /* This is the C++98/03 definition of POD; it changed in C++0x, but we 4385 retain the old definition internally for ABI reasons. */ 4386 CLASSTYPE_NON_LAYOUT_POD_P (t) 4387 |= (CLASSTYPE_NON_AGGREGATE (t) 4388 || saved_nontrivial_dtor || saved_complex_asn_ref); 4389 CLASSTYPE_NON_STD_LAYOUT (t) |= TYPE_CONTAINS_VPTR_P (t); 4390 TYPE_HAS_COMPLEX_ASSIGN_REF (t) |= TYPE_CONTAINS_VPTR_P (t); 4391 TYPE_HAS_COMPLEX_DFLT (t) |= TYPE_CONTAINS_VPTR_P (t); 4392 4393 /* If the class has no user-declared constructor, but does have 4394 non-static const or reference data members that can never be 4395 initialized, issue a warning. */ 4396 if (warn_uninitialized 4397 /* Classes with user-declared constructors are presumed to 4398 initialize these members. */ 4399 && !TYPE_HAS_USER_CONSTRUCTOR (t) 4400 /* Aggregates can be initialized with brace-enclosed 4401 initializers. */ 4402 && CLASSTYPE_NON_AGGREGATE (t)) 4403 { 4404 tree field; 4405 4406 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field)) 4407 { 4408 tree type; 4409 4410 if (TREE_CODE (field) != FIELD_DECL) 4411 continue; 4412 4413 type = TREE_TYPE (field); 4414 if (TREE_CODE (type) == REFERENCE_TYPE) 4415 warning (OPT_Wuninitialized, "non-static reference %q+#D " 4416 "in class without a constructor", field); 4417 else if (CP_TYPE_CONST_P (type) 4418 && (!CLASS_TYPE_P (type) 4419 || !TYPE_HAS_DEFAULT_CONSTRUCTOR (type))) 4420 warning (OPT_Wuninitialized, "non-static const member %q+#D " 4421 "in class without a constructor", field); 4422 } 4423 } 4424 4425 /* Synthesize any needed methods. */ 4426 add_implicitly_declared_members (t, 4427 cant_have_const_ctor, 4428 no_const_asn_ref); 4429 4430 /* Check defaulted declarations here so we have cant_have_const_ctor 4431 and don't need to worry about clones. */ 4432 for (fn = TYPE_METHODS (t); fn; fn = TREE_CHAIN (fn)) 4433 if (DECL_DEFAULTED_IN_CLASS_P (fn)) 4434 { 4435 int copy = copy_fn_p (fn); 4436 if (copy > 0) 4437 { 4438 bool imp_const_p 4439 = (DECL_CONSTRUCTOR_P (fn) ? !cant_have_const_ctor 4440 : !no_const_asn_ref); 4441 bool fn_const_p = (copy == 2); 4442 4443 if (fn_const_p && !imp_const_p) 4444 /* If the function is defaulted outside the class, we just 4445 give the synthesis error. */ 4446 error ("%q+D declared to take const reference, but implicit " 4447 "declaration would take non-const", fn); 4448 else if (imp_const_p && !fn_const_p) 4449 error ("%q+D declared to take non-const reference cannot be " 4450 "defaulted in the class body", fn); 4451 } 4452 defaulted_late_check (fn); 4453 } 4454 4455 if (LAMBDA_TYPE_P (t)) 4456 { 4457 /* "The closure type associated with a lambda-expression has a deleted 4458 default constructor and a deleted copy assignment operator." */ 4459 TYPE_NEEDS_CONSTRUCTING (t) = 1; 4460 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 0; 4461 CLASSTYPE_LAZY_DEFAULT_CTOR (t) = 0; 4462 TYPE_HAS_ASSIGN_REF (t) = 0; 4463 CLASSTYPE_LAZY_ASSIGNMENT_OP (t) = 0; 4464 4465 /* "This class type is not an aggregate." */ 4466 CLASSTYPE_NON_AGGREGATE (t) = 1; 4467 } 4468 4469 /* Create the in-charge and not-in-charge variants of constructors 4470 and destructors. */ 4471 clone_constructors_and_destructors (t); 4472 4473 /* Process the using-declarations. */ 4474 for (; access_decls; access_decls = TREE_CHAIN (access_decls)) 4475 handle_using_decl (TREE_VALUE (access_decls), t); 4476 4477 /* Build and sort the CLASSTYPE_METHOD_VEC. */ 4478 finish_struct_methods (t); 4479 4480 /* Figure out whether or not we will need a cookie when dynamically 4481 allocating an array of this type. */ 4482 TYPE_LANG_SPECIFIC (t)->u.c.vec_new_uses_cookie 4483 = type_requires_array_cookie (t); 4484 } 4485 4486 /* If T needs a pointer to its virtual function table, set TYPE_VFIELD 4487 accordingly. If a new vfield was created (because T doesn't have a 4488 primary base class), then the newly created field is returned. It 4489 is not added to the TYPE_FIELDS list; it is the caller's 4490 responsibility to do that. Accumulate declared virtual functions 4491 on VIRTUALS_P. */ 4492 4493 static tree 4494 create_vtable_ptr (tree t, tree* virtuals_p) 4495 { 4496 tree fn; 4497 4498 /* Collect the virtual functions declared in T. */ 4499 for (fn = TYPE_METHODS (t); fn; fn = TREE_CHAIN (fn)) 4500 if (DECL_VINDEX (fn) && !DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn) 4501 && TREE_CODE (DECL_VINDEX (fn)) != INTEGER_CST) 4502 { 4503 tree new_virtual = make_node (TREE_LIST); 4504 4505 BV_FN (new_virtual) = fn; 4506 BV_DELTA (new_virtual) = integer_zero_node; 4507 BV_VCALL_INDEX (new_virtual) = NULL_TREE; 4508 4509 TREE_CHAIN (new_virtual) = *virtuals_p; 4510 *virtuals_p = new_virtual; 4511 } 4512 4513 /* If we couldn't find an appropriate base class, create a new field 4514 here. Even if there weren't any new virtual functions, we might need a 4515 new virtual function table if we're supposed to include vptrs in 4516 all classes that need them. */ 4517 if (!TYPE_VFIELD (t) && (*virtuals_p || TYPE_CONTAINS_VPTR_P (t))) 4518 { 4519 /* We build this decl with vtbl_ptr_type_node, which is a 4520 `vtable_entry_type*'. It might seem more precise to use 4521 `vtable_entry_type (*)[N]' where N is the number of virtual 4522 functions. However, that would require the vtable pointer in 4523 base classes to have a different type than the vtable pointer 4524 in derived classes. We could make that happen, but that 4525 still wouldn't solve all the problems. In particular, the 4526 type-based alias analysis code would decide that assignments 4527 to the base class vtable pointer can't alias assignments to 4528 the derived class vtable pointer, since they have different 4529 types. Thus, in a derived class destructor, where the base 4530 class constructor was inlined, we could generate bad code for 4531 setting up the vtable pointer. 4532 4533 Therefore, we use one type for all vtable pointers. We still 4534 use a type-correct type; it's just doesn't indicate the array 4535 bounds. That's better than using `void*' or some such; it's 4536 cleaner, and it let's the alias analysis code know that these 4537 stores cannot alias stores to void*! */ 4538 tree field; 4539 4540 field = build_decl (input_location, 4541 FIELD_DECL, get_vfield_name (t), vtbl_ptr_type_node); 4542 DECL_VIRTUAL_P (field) = 1; 4543 DECL_ARTIFICIAL (field) = 1; 4544 DECL_FIELD_CONTEXT (field) = t; 4545 DECL_FCONTEXT (field) = t; 4546 if (TYPE_PACKED (t)) 4547 DECL_PACKED (field) = 1; 4548 4549 TYPE_VFIELD (t) = field; 4550 4551 /* This class is non-empty. */ 4552 CLASSTYPE_EMPTY_P (t) = 0; 4553 4554 return field; 4555 } 4556 4557 return NULL_TREE; 4558 } 4559 4560 /* Add OFFSET to all base types of BINFO which is a base in the 4561 hierarchy dominated by T. 4562 4563 OFFSET, which is a type offset, is number of bytes. */ 4564 4565 static void 4566 propagate_binfo_offsets (tree binfo, tree offset) 4567 { 4568 int i; 4569 tree primary_binfo; 4570 tree base_binfo; 4571 4572 /* Update BINFO's offset. */ 4573 BINFO_OFFSET (binfo) 4574 = convert (sizetype, 4575 size_binop (PLUS_EXPR, 4576 convert (ssizetype, BINFO_OFFSET (binfo)), 4577 offset)); 4578 4579 /* Find the primary base class. */ 4580 primary_binfo = get_primary_binfo (binfo); 4581 4582 if (primary_binfo && BINFO_INHERITANCE_CHAIN (primary_binfo) == binfo) 4583 propagate_binfo_offsets (primary_binfo, offset); 4584 4585 /* Scan all of the bases, pushing the BINFO_OFFSET adjust 4586 downwards. */ 4587 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i) 4588 { 4589 /* Don't do the primary base twice. */ 4590 if (base_binfo == primary_binfo) 4591 continue; 4592 4593 if (BINFO_VIRTUAL_P (base_binfo)) 4594 continue; 4595 4596 propagate_binfo_offsets (base_binfo, offset); 4597 } 4598 } 4599 4600 /* Set BINFO_OFFSET for all of the virtual bases for RLI->T. Update 4601 TYPE_ALIGN and TYPE_SIZE for T. OFFSETS gives the location of 4602 empty subobjects of T. */ 4603 4604 static void 4605 layout_virtual_bases (record_layout_info rli, splay_tree offsets) 4606 { 4607 tree vbase; 4608 tree t = rli->t; 4609 bool first_vbase = true; 4610 tree *next_field; 4611 4612 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) == 0) 4613 return; 4614 4615 if (!abi_version_at_least(2)) 4616 { 4617 /* In G++ 3.2, we incorrectly rounded the size before laying out 4618 the virtual bases. */ 4619 finish_record_layout (rli, /*free_p=*/false); 4620 #ifdef STRUCTURE_SIZE_BOUNDARY 4621 /* Packed structures don't need to have minimum size. */ 4622 if (! TYPE_PACKED (t)) 4623 TYPE_ALIGN (t) = MAX (TYPE_ALIGN (t), (unsigned) STRUCTURE_SIZE_BOUNDARY); 4624 #endif 4625 rli->offset = TYPE_SIZE_UNIT (t); 4626 rli->bitpos = bitsize_zero_node; 4627 rli->record_align = TYPE_ALIGN (t); 4628 } 4629 4630 /* Find the last field. The artificial fields created for virtual 4631 bases will go after the last extant field to date. */ 4632 next_field = &TYPE_FIELDS (t); 4633 while (*next_field) 4634 next_field = &TREE_CHAIN (*next_field); 4635 4636 /* Go through the virtual bases, allocating space for each virtual 4637 base that is not already a primary base class. These are 4638 allocated in inheritance graph order. */ 4639 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase)) 4640 { 4641 if (!BINFO_VIRTUAL_P (vbase)) 4642 continue; 4643 4644 if (!BINFO_PRIMARY_P (vbase)) 4645 { 4646 tree basetype = TREE_TYPE (vbase); 4647 4648 /* This virtual base is not a primary base of any class in the 4649 hierarchy, so we have to add space for it. */ 4650 next_field = build_base_field (rli, vbase, 4651 offsets, next_field); 4652 4653 /* If the first virtual base might have been placed at a 4654 lower address, had we started from CLASSTYPE_SIZE, rather 4655 than TYPE_SIZE, issue a warning. There can be both false 4656 positives and false negatives from this warning in rare 4657 cases; to deal with all the possibilities would probably 4658 require performing both layout algorithms and comparing 4659 the results which is not particularly tractable. */ 4660 if (warn_abi 4661 && first_vbase 4662 && (tree_int_cst_lt 4663 (size_binop (CEIL_DIV_EXPR, 4664 round_up_loc (input_location, 4665 CLASSTYPE_SIZE (t), 4666 CLASSTYPE_ALIGN (basetype)), 4667 bitsize_unit_node), 4668 BINFO_OFFSET (vbase)))) 4669 warning (OPT_Wabi, 4670 "offset of virtual base %qT is not ABI-compliant and " 4671 "may change in a future version of GCC", 4672 basetype); 4673 4674 first_vbase = false; 4675 } 4676 } 4677 } 4678 4679 /* Returns the offset of the byte just past the end of the base class 4680 BINFO. */ 4681 4682 static tree 4683 end_of_base (tree binfo) 4684 { 4685 tree size; 4686 4687 if (!CLASSTYPE_AS_BASE (BINFO_TYPE (binfo))) 4688 size = TYPE_SIZE_UNIT (char_type_node); 4689 else if (is_empty_class (BINFO_TYPE (binfo))) 4690 /* An empty class has zero CLASSTYPE_SIZE_UNIT, but we need to 4691 allocate some space for it. It cannot have virtual bases, so 4692 TYPE_SIZE_UNIT is fine. */ 4693 size = TYPE_SIZE_UNIT (BINFO_TYPE (binfo)); 4694 else 4695 size = CLASSTYPE_SIZE_UNIT (BINFO_TYPE (binfo)); 4696 4697 return size_binop (PLUS_EXPR, BINFO_OFFSET (binfo), size); 4698 } 4699 4700 /* Returns the offset of the byte just past the end of the base class 4701 with the highest offset in T. If INCLUDE_VIRTUALS_P is zero, then 4702 only non-virtual bases are included. */ 4703 4704 static tree 4705 end_of_class (tree t, int include_virtuals_p) 4706 { 4707 tree result = size_zero_node; 4708 VEC(tree,gc) *vbases; 4709 tree binfo; 4710 tree base_binfo; 4711 tree offset; 4712 int i; 4713 4714 for (binfo = TYPE_BINFO (t), i = 0; 4715 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i) 4716 { 4717 if (!include_virtuals_p 4718 && BINFO_VIRTUAL_P (base_binfo) 4719 && (!BINFO_PRIMARY_P (base_binfo) 4720 || BINFO_INHERITANCE_CHAIN (base_binfo) != TYPE_BINFO (t))) 4721 continue; 4722 4723 offset = end_of_base (base_binfo); 4724 if (INT_CST_LT_UNSIGNED (result, offset)) 4725 result = offset; 4726 } 4727 4728 /* G++ 3.2 did not check indirect virtual bases. */ 4729 if (abi_version_at_least (2) && include_virtuals_p) 4730 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0; 4731 VEC_iterate (tree, vbases, i, base_binfo); i++) 4732 { 4733 offset = end_of_base (base_binfo); 4734 if (INT_CST_LT_UNSIGNED (result, offset)) 4735 result = offset; 4736 } 4737 4738 return result; 4739 } 4740 4741 /* Warn about bases of T that are inaccessible because they are 4742 ambiguous. For example: 4743 4744 struct S {}; 4745 struct T : public S {}; 4746 struct U : public S, public T {}; 4747 4748 Here, `(S*) new U' is not allowed because there are two `S' 4749 subobjects of U. */ 4750 4751 static void 4752 warn_about_ambiguous_bases (tree t) 4753 { 4754 int i; 4755 VEC(tree,gc) *vbases; 4756 tree basetype; 4757 tree binfo; 4758 tree base_binfo; 4759 4760 /* If there are no repeated bases, nothing can be ambiguous. */ 4761 if (!CLASSTYPE_REPEATED_BASE_P (t)) 4762 return; 4763 4764 /* Check direct bases. */ 4765 for (binfo = TYPE_BINFO (t), i = 0; 4766 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i) 4767 { 4768 basetype = BINFO_TYPE (base_binfo); 4769 4770 if (!lookup_base (t, basetype, ba_unique | ba_quiet, NULL)) 4771 warning (0, "direct base %qT inaccessible in %qT due to ambiguity", 4772 basetype, t); 4773 } 4774 4775 /* Check for ambiguous virtual bases. */ 4776 if (extra_warnings) 4777 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0; 4778 VEC_iterate (tree, vbases, i, binfo); i++) 4779 { 4780 basetype = BINFO_TYPE (binfo); 4781 4782 if (!lookup_base (t, basetype, ba_unique | ba_quiet, NULL)) 4783 warning (OPT_Wextra, "virtual base %qT inaccessible in %qT due to ambiguity", 4784 basetype, t); 4785 } 4786 } 4787 4788 /* Compare two INTEGER_CSTs K1 and K2. */ 4789 4790 static int 4791 splay_tree_compare_integer_csts (splay_tree_key k1, splay_tree_key k2) 4792 { 4793 return tree_int_cst_compare ((tree) k1, (tree) k2); 4794 } 4795 4796 /* Increase the size indicated in RLI to account for empty classes 4797 that are "off the end" of the class. */ 4798 4799 static void 4800 include_empty_classes (record_layout_info rli) 4801 { 4802 tree eoc; 4803 tree rli_size; 4804 4805 /* It might be the case that we grew the class to allocate a 4806 zero-sized base class. That won't be reflected in RLI, yet, 4807 because we are willing to overlay multiple bases at the same 4808 offset. However, now we need to make sure that RLI is big enough 4809 to reflect the entire class. */ 4810 eoc = end_of_class (rli->t, 4811 CLASSTYPE_AS_BASE (rli->t) != NULL_TREE); 4812 rli_size = rli_size_unit_so_far (rli); 4813 if (TREE_CODE (rli_size) == INTEGER_CST 4814 && INT_CST_LT_UNSIGNED (rli_size, eoc)) 4815 { 4816 if (!abi_version_at_least (2)) 4817 /* In version 1 of the ABI, the size of a class that ends with 4818 a bitfield was not rounded up to a whole multiple of a 4819 byte. Because rli_size_unit_so_far returns only the number 4820 of fully allocated bytes, any extra bits were not included 4821 in the size. */ 4822 rli->bitpos = round_down (rli->bitpos, BITS_PER_UNIT); 4823 else 4824 /* The size should have been rounded to a whole byte. */ 4825 gcc_assert (tree_int_cst_equal 4826 (rli->bitpos, round_down (rli->bitpos, BITS_PER_UNIT))); 4827 rli->bitpos 4828 = size_binop (PLUS_EXPR, 4829 rli->bitpos, 4830 size_binop (MULT_EXPR, 4831 convert (bitsizetype, 4832 size_binop (MINUS_EXPR, 4833 eoc, rli_size)), 4834 bitsize_int (BITS_PER_UNIT))); 4835 normalize_rli (rli); 4836 } 4837 } 4838 4839 /* Calculate the TYPE_SIZE, TYPE_ALIGN, etc for T. Calculate 4840 BINFO_OFFSETs for all of the base-classes. Position the vtable 4841 pointer. Accumulate declared virtual functions on VIRTUALS_P. */ 4842 4843 static void 4844 layout_class_type (tree t, tree *virtuals_p) 4845 { 4846 tree non_static_data_members; 4847 tree field; 4848 tree vptr; 4849 record_layout_info rli; 4850 /* Maps offsets (represented as INTEGER_CSTs) to a TREE_LIST of 4851 types that appear at that offset. */ 4852 splay_tree empty_base_offsets; 4853 /* True if the last field layed out was a bit-field. */ 4854 bool last_field_was_bitfield = false; 4855 /* The location at which the next field should be inserted. */ 4856 tree *next_field; 4857 /* T, as a base class. */ 4858 tree base_t; 4859 4860 /* Keep track of the first non-static data member. */ 4861 non_static_data_members = TYPE_FIELDS (t); 4862 4863 /* Start laying out the record. */ 4864 rli = start_record_layout (t); 4865 4866 /* Mark all the primary bases in the hierarchy. */ 4867 determine_primary_bases (t); 4868 4869 /* Create a pointer to our virtual function table. */ 4870 vptr = create_vtable_ptr (t, virtuals_p); 4871 4872 /* The vptr is always the first thing in the class. */ 4873 if (vptr) 4874 { 4875 TREE_CHAIN (vptr) = TYPE_FIELDS (t); 4876 TYPE_FIELDS (t) = vptr; 4877 next_field = &TREE_CHAIN (vptr); 4878 place_field (rli, vptr); 4879 } 4880 else 4881 next_field = &TYPE_FIELDS (t); 4882 4883 /* Build FIELD_DECLs for all of the non-virtual base-types. */ 4884 empty_base_offsets = splay_tree_new (splay_tree_compare_integer_csts, 4885 NULL, NULL); 4886 build_base_fields (rli, empty_base_offsets, next_field); 4887 4888 /* Layout the non-static data members. */ 4889 for (field = non_static_data_members; field; field = TREE_CHAIN (field)) 4890 { 4891 tree type; 4892 tree padding; 4893 4894 /* We still pass things that aren't non-static data members to 4895 the back end, in case it wants to do something with them. */ 4896 if (TREE_CODE (field) != FIELD_DECL) 4897 { 4898 place_field (rli, field); 4899 /* If the static data member has incomplete type, keep track 4900 of it so that it can be completed later. (The handling 4901 of pending statics in finish_record_layout is 4902 insufficient; consider: 4903 4904 struct S1; 4905 struct S2 { static S1 s1; }; 4906 4907 At this point, finish_record_layout will be called, but 4908 S1 is still incomplete.) */ 4909 if (TREE_CODE (field) == VAR_DECL) 4910 { 4911 maybe_register_incomplete_var (field); 4912 /* The visibility of static data members is determined 4913 at their point of declaration, not their point of 4914 definition. */ 4915 determine_visibility (field); 4916 } 4917 continue; 4918 } 4919 4920 type = TREE_TYPE (field); 4921 if (type == error_mark_node) 4922 continue; 4923 4924 padding = NULL_TREE; 4925 4926 /* If this field is a bit-field whose width is greater than its 4927 type, then there are some special rules for allocating 4928 it. */ 4929 if (DECL_C_BIT_FIELD (field) 4930 && INT_CST_LT (TYPE_SIZE (type), DECL_SIZE (field))) 4931 { 4932 unsigned int itk; 4933 tree integer_type; 4934 bool was_unnamed_p = false; 4935 /* We must allocate the bits as if suitably aligned for the 4936 longest integer type that fits in this many bits. type 4937 of the field. Then, we are supposed to use the left over 4938 bits as additional padding. */ 4939 for (itk = itk_char; itk != itk_none; ++itk) 4940 if (INT_CST_LT (DECL_SIZE (field), 4941 TYPE_SIZE (integer_types[itk]))) 4942 break; 4943 4944 /* ITK now indicates a type that is too large for the 4945 field. We have to back up by one to find the largest 4946 type that fits. */ 4947 integer_type = integer_types[itk - 1]; 4948 4949 /* Figure out how much additional padding is required. GCC 4950 3.2 always created a padding field, even if it had zero 4951 width. */ 4952 if (!abi_version_at_least (2) 4953 || INT_CST_LT (TYPE_SIZE (integer_type), DECL_SIZE (field))) 4954 { 4955 if (abi_version_at_least (2) && TREE_CODE (t) == UNION_TYPE) 4956 /* In a union, the padding field must have the full width 4957 of the bit-field; all fields start at offset zero. */ 4958 padding = DECL_SIZE (field); 4959 else 4960 { 4961 if (TREE_CODE (t) == UNION_TYPE) 4962 warning (OPT_Wabi, "size assigned to %qT may not be " 4963 "ABI-compliant and may change in a future " 4964 "version of GCC", 4965 t); 4966 padding = size_binop (MINUS_EXPR, DECL_SIZE (field), 4967 TYPE_SIZE (integer_type)); 4968 } 4969 } 4970 #ifdef PCC_BITFIELD_TYPE_MATTERS 4971 /* An unnamed bitfield does not normally affect the 4972 alignment of the containing class on a target where 4973 PCC_BITFIELD_TYPE_MATTERS. But, the C++ ABI does not 4974 make any exceptions for unnamed bitfields when the 4975 bitfields are longer than their types. Therefore, we 4976 temporarily give the field a name. */ 4977 if (PCC_BITFIELD_TYPE_MATTERS && !DECL_NAME (field)) 4978 { 4979 was_unnamed_p = true; 4980 DECL_NAME (field) = make_anon_name (); 4981 } 4982 #endif 4983 DECL_SIZE (field) = TYPE_SIZE (integer_type); 4984 DECL_ALIGN (field) = TYPE_ALIGN (integer_type); 4985 DECL_USER_ALIGN (field) = TYPE_USER_ALIGN (integer_type); 4986 layout_nonempty_base_or_field (rli, field, NULL_TREE, 4987 empty_base_offsets); 4988 if (was_unnamed_p) 4989 DECL_NAME (field) = NULL_TREE; 4990 /* Now that layout has been performed, set the size of the 4991 field to the size of its declared type; the rest of the 4992 field is effectively invisible. */ 4993 DECL_SIZE (field) = TYPE_SIZE (type); 4994 /* We must also reset the DECL_MODE of the field. */ 4995 if (abi_version_at_least (2)) 4996 DECL_MODE (field) = TYPE_MODE (type); 4997 else if (warn_abi 4998 && DECL_MODE (field) != TYPE_MODE (type)) 4999 /* Versions of G++ before G++ 3.4 did not reset the 5000 DECL_MODE. */ 5001 warning (OPT_Wabi, 5002 "the offset of %qD may not be ABI-compliant and may " 5003 "change in a future version of GCC", field); 5004 } 5005 else 5006 layout_nonempty_base_or_field (rli, field, NULL_TREE, 5007 empty_base_offsets); 5008 5009 /* Remember the location of any empty classes in FIELD. */ 5010 if (abi_version_at_least (2)) 5011 record_subobject_offsets (TREE_TYPE (field), 5012 byte_position(field), 5013 empty_base_offsets, 5014 /*is_data_member=*/true); 5015 5016 /* If a bit-field does not immediately follow another bit-field, 5017 and yet it starts in the middle of a byte, we have failed to 5018 comply with the ABI. */ 5019 if (warn_abi 5020 && DECL_C_BIT_FIELD (field) 5021 /* The TREE_NO_WARNING flag gets set by Objective-C when 5022 laying out an Objective-C class. The ObjC ABI differs 5023 from the C++ ABI, and so we do not want a warning 5024 here. */ 5025 && !TREE_NO_WARNING (field) 5026 && !last_field_was_bitfield 5027 && !integer_zerop (size_binop (TRUNC_MOD_EXPR, 5028 DECL_FIELD_BIT_OFFSET (field), 5029 bitsize_unit_node))) 5030 warning (OPT_Wabi, "offset of %q+D is not ABI-compliant and may " 5031 "change in a future version of GCC", field); 5032 5033 /* G++ used to use DECL_FIELD_OFFSET as if it were the byte 5034 offset of the field. */ 5035 if (warn_abi 5036 && !abi_version_at_least (2) 5037 && !tree_int_cst_equal (DECL_FIELD_OFFSET (field), 5038 byte_position (field)) 5039 && contains_empty_class_p (TREE_TYPE (field))) 5040 warning (OPT_Wabi, "%q+D contains empty classes which may cause base " 5041 "classes to be placed at different locations in a " 5042 "future version of GCC", field); 5043 5044 /* The middle end uses the type of expressions to determine the 5045 possible range of expression values. In order to optimize 5046 "x.i > 7" to "false" for a 2-bit bitfield "i", the middle end 5047 must be made aware of the width of "i", via its type. 5048 5049 Because C++ does not have integer types of arbitrary width, 5050 we must (for the purposes of the front end) convert from the 5051 type assigned here to the declared type of the bitfield 5052 whenever a bitfield expression is used as an rvalue. 5053 Similarly, when assigning a value to a bitfield, the value 5054 must be converted to the type given the bitfield here. */ 5055 if (DECL_C_BIT_FIELD (field)) 5056 { 5057 unsigned HOST_WIDE_INT width; 5058 tree ftype = TREE_TYPE (field); 5059 width = tree_low_cst (DECL_SIZE (field), /*unsignedp=*/1); 5060 if (width != TYPE_PRECISION (ftype)) 5061 { 5062 TREE_TYPE (field) 5063 = c_build_bitfield_integer_type (width, 5064 TYPE_UNSIGNED (ftype)); 5065 TREE_TYPE (field) 5066 = cp_build_qualified_type (TREE_TYPE (field), 5067 TYPE_QUALS (ftype)); 5068 } 5069 } 5070 5071 /* If we needed additional padding after this field, add it 5072 now. */ 5073 if (padding) 5074 { 5075 tree padding_field; 5076 5077 padding_field = build_decl (input_location, 5078 FIELD_DECL, 5079 NULL_TREE, 5080 char_type_node); 5081 DECL_BIT_FIELD (padding_field) = 1; 5082 DECL_SIZE (padding_field) = padding; 5083 DECL_CONTEXT (padding_field) = t; 5084 DECL_ARTIFICIAL (padding_field) = 1; 5085 DECL_IGNORED_P (padding_field) = 1; 5086 layout_nonempty_base_or_field (rli, padding_field, 5087 NULL_TREE, 5088 empty_base_offsets); 5089 } 5090 5091 last_field_was_bitfield = DECL_C_BIT_FIELD (field); 5092 } 5093 5094 if (abi_version_at_least (2) && !integer_zerop (rli->bitpos)) 5095 { 5096 /* Make sure that we are on a byte boundary so that the size of 5097 the class without virtual bases will always be a round number 5098 of bytes. */ 5099 rli->bitpos = round_up_loc (input_location, rli->bitpos, BITS_PER_UNIT); 5100 normalize_rli (rli); 5101 } 5102 5103 /* G++ 3.2 does not allow virtual bases to be overlaid with tail 5104 padding. */ 5105 if (!abi_version_at_least (2)) 5106 include_empty_classes(rli); 5107 5108 /* Delete all zero-width bit-fields from the list of fields. Now 5109 that the type is laid out they are no longer important. */ 5110 remove_zero_width_bit_fields (t); 5111 5112 /* Create the version of T used for virtual bases. We do not use 5113 make_class_type for this version; this is an artificial type. For 5114 a POD type, we just reuse T. */ 5115 if (CLASSTYPE_NON_LAYOUT_POD_P (t) || CLASSTYPE_EMPTY_P (t)) 5116 { 5117 base_t = make_node (TREE_CODE (t)); 5118 5119 /* Set the size and alignment for the new type. In G++ 3.2, all 5120 empty classes were considered to have size zero when used as 5121 base classes. */ 5122 if (!abi_version_at_least (2) && CLASSTYPE_EMPTY_P (t)) 5123 { 5124 TYPE_SIZE (base_t) = bitsize_zero_node; 5125 TYPE_SIZE_UNIT (base_t) = size_zero_node; 5126 if (warn_abi && !integer_zerop (rli_size_unit_so_far (rli))) 5127 warning (OPT_Wabi, 5128 "layout of classes derived from empty class %qT " 5129 "may change in a future version of GCC", 5130 t); 5131 } 5132 else 5133 { 5134 tree eoc; 5135 5136 /* If the ABI version is not at least two, and the last 5137 field was a bit-field, RLI may not be on a byte 5138 boundary. In particular, rli_size_unit_so_far might 5139 indicate the last complete byte, while rli_size_so_far 5140 indicates the total number of bits used. Therefore, 5141 rli_size_so_far, rather than rli_size_unit_so_far, is 5142 used to compute TYPE_SIZE_UNIT. */ 5143 eoc = end_of_class (t, /*include_virtuals_p=*/0); 5144 TYPE_SIZE_UNIT (base_t) 5145 = size_binop (MAX_EXPR, 5146 convert (sizetype, 5147 size_binop (CEIL_DIV_EXPR, 5148 rli_size_so_far (rli), 5149 bitsize_int (BITS_PER_UNIT))), 5150 eoc); 5151 TYPE_SIZE (base_t) 5152 = size_binop (MAX_EXPR, 5153 rli_size_so_far (rli), 5154 size_binop (MULT_EXPR, 5155 convert (bitsizetype, eoc), 5156 bitsize_int (BITS_PER_UNIT))); 5157 } 5158 TYPE_ALIGN (base_t) = rli->record_align; 5159 TYPE_USER_ALIGN (base_t) = TYPE_USER_ALIGN (t); 5160 5161 /* Copy the fields from T. */ 5162 next_field = &TYPE_FIELDS (base_t); 5163 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field)) 5164 if (TREE_CODE (field) == FIELD_DECL) 5165 { 5166 *next_field = build_decl (input_location, 5167 FIELD_DECL, 5168 DECL_NAME (field), 5169 TREE_TYPE (field)); 5170 DECL_CONTEXT (*next_field) = base_t; 5171 DECL_FIELD_OFFSET (*next_field) = DECL_FIELD_OFFSET (field); 5172 DECL_FIELD_BIT_OFFSET (*next_field) 5173 = DECL_FIELD_BIT_OFFSET (field); 5174 DECL_SIZE (*next_field) = DECL_SIZE (field); 5175 DECL_MODE (*next_field) = DECL_MODE (field); 5176 next_field = &TREE_CHAIN (*next_field); 5177 } 5178 5179 /* Record the base version of the type. */ 5180 CLASSTYPE_AS_BASE (t) = base_t; 5181 TYPE_CONTEXT (base_t) = t; 5182 } 5183 else 5184 CLASSTYPE_AS_BASE (t) = t; 5185 5186 /* Every empty class contains an empty class. */ 5187 if (CLASSTYPE_EMPTY_P (t)) 5188 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1; 5189 5190 /* Set the TYPE_DECL for this type to contain the right 5191 value for DECL_OFFSET, so that we can use it as part 5192 of a COMPONENT_REF for multiple inheritance. */ 5193 layout_decl (TYPE_MAIN_DECL (t), 0); 5194 5195 /* Now fix up any virtual base class types that we left lying 5196 around. We must get these done before we try to lay out the 5197 virtual function table. As a side-effect, this will remove the 5198 base subobject fields. */ 5199 layout_virtual_bases (rli, empty_base_offsets); 5200 5201 /* Make sure that empty classes are reflected in RLI at this 5202 point. */ 5203 include_empty_classes(rli); 5204 5205 /* Make sure not to create any structures with zero size. */ 5206 if (integer_zerop (rli_size_unit_so_far (rli)) && CLASSTYPE_EMPTY_P (t)) 5207 place_field (rli, 5208 build_decl (input_location, 5209 FIELD_DECL, NULL_TREE, char_type_node)); 5210 5211 /* If this is a non-POD, declaring it packed makes a difference to how it 5212 can be used as a field; don't let finalize_record_size undo it. */ 5213 if (TYPE_PACKED (t) && !layout_pod_type_p (t)) 5214 rli->packed_maybe_necessary = true; 5215 5216 /* Let the back end lay out the type. */ 5217 finish_record_layout (rli, /*free_p=*/true); 5218 5219 /* Warn about bases that can't be talked about due to ambiguity. */ 5220 warn_about_ambiguous_bases (t); 5221 5222 /* Now that we're done with layout, give the base fields the real types. */ 5223 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field)) 5224 if (DECL_ARTIFICIAL (field) && IS_FAKE_BASE_TYPE (TREE_TYPE (field))) 5225 TREE_TYPE (field) = TYPE_CONTEXT (TREE_TYPE (field)); 5226 5227 /* Clean up. */ 5228 splay_tree_delete (empty_base_offsets); 5229 5230 if (CLASSTYPE_EMPTY_P (t) 5231 && tree_int_cst_lt (sizeof_biggest_empty_class, 5232 TYPE_SIZE_UNIT (t))) 5233 sizeof_biggest_empty_class = TYPE_SIZE_UNIT (t); 5234 } 5235 5236 /* Determine the "key method" for the class type indicated by TYPE, 5237 and set CLASSTYPE_KEY_METHOD accordingly. */ 5238 5239 void 5240 determine_key_method (tree type) 5241 { 5242 tree method; 5243 5244 if (TYPE_FOR_JAVA (type) 5245 || processing_template_decl 5246 || CLASSTYPE_TEMPLATE_INSTANTIATION (type) 5247 || CLASSTYPE_INTERFACE_KNOWN (type)) 5248 return; 5249 5250 /* The key method is the first non-pure virtual function that is not 5251 inline at the point of class definition. On some targets the 5252 key function may not be inline; those targets should not call 5253 this function until the end of the translation unit. */ 5254 for (method = TYPE_METHODS (type); method != NULL_TREE; 5255 method = TREE_CHAIN (method)) 5256 if (DECL_VINDEX (method) != NULL_TREE 5257 && ! DECL_DECLARED_INLINE_P (method) 5258 && ! DECL_PURE_VIRTUAL_P (method)) 5259 { 5260 CLASSTYPE_KEY_METHOD (type) = method; 5261 break; 5262 } 5263 5264 return; 5265 } 5266 5267 /* Perform processing required when the definition of T (a class type) 5268 is complete. */ 5269 5270 void 5271 finish_struct_1 (tree t) 5272 { 5273 tree x; 5274 /* A TREE_LIST. The TREE_VALUE of each node is a FUNCTION_DECL. */ 5275 tree virtuals = NULL_TREE; 5276 int n_fields = 0; 5277 5278 if (COMPLETE_TYPE_P (t)) 5279 { 5280 gcc_assert (MAYBE_CLASS_TYPE_P (t)); 5281 error ("redefinition of %q#T", t); 5282 popclass (); 5283 return; 5284 } 5285 5286 /* If this type was previously laid out as a forward reference, 5287 make sure we lay it out again. */ 5288 TYPE_SIZE (t) = NULL_TREE; 5289 CLASSTYPE_PRIMARY_BINFO (t) = NULL_TREE; 5290 5291 /* Make assumptions about the class; we'll reset the flags if 5292 necessary. */ 5293 CLASSTYPE_EMPTY_P (t) = 1; 5294 CLASSTYPE_NEARLY_EMPTY_P (t) = 1; 5295 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 0; 5296 5297 /* Do end-of-class semantic processing: checking the validity of the 5298 bases and members and add implicitly generated methods. */ 5299 check_bases_and_members (t); 5300 5301 /* Find the key method. */ 5302 if (TYPE_CONTAINS_VPTR_P (t)) 5303 { 5304 /* The Itanium C++ ABI permits the key method to be chosen when 5305 the class is defined -- even though the key method so 5306 selected may later turn out to be an inline function. On 5307 some systems (such as ARM Symbian OS) the key method cannot 5308 be determined until the end of the translation unit. On such 5309 systems, we leave CLASSTYPE_KEY_METHOD set to NULL, which 5310 will cause the class to be added to KEYED_CLASSES. Then, in 5311 finish_file we will determine the key method. */ 5312 if (targetm.cxx.key_method_may_be_inline ()) 5313 determine_key_method (t); 5314 5315 /* If a polymorphic class has no key method, we may emit the vtable 5316 in every translation unit where the class definition appears. */ 5317 if (CLASSTYPE_KEY_METHOD (t) == NULL_TREE) 5318 keyed_classes = tree_cons (NULL_TREE, t, keyed_classes); 5319 } 5320 5321 /* Layout the class itself. */ 5322 layout_class_type (t, &virtuals); 5323 if (CLASSTYPE_AS_BASE (t) != t) 5324 /* We use the base type for trivial assignments, and hence it 5325 needs a mode. */ 5326 compute_record_mode (CLASSTYPE_AS_BASE (t)); 5327 5328 virtuals = modify_all_vtables (t, nreverse (virtuals)); 5329 5330 /* If necessary, create the primary vtable for this class. */ 5331 if (virtuals || TYPE_CONTAINS_VPTR_P (t)) 5332 { 5333 /* We must enter these virtuals into the table. */ 5334 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t)) 5335 build_primary_vtable (NULL_TREE, t); 5336 else if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t))) 5337 /* Here we know enough to change the type of our virtual 5338 function table, but we will wait until later this function. */ 5339 build_primary_vtable (CLASSTYPE_PRIMARY_BINFO (t), t); 5340 } 5341 5342 if (TYPE_CONTAINS_VPTR_P (t)) 5343 { 5344 int vindex; 5345 tree fn; 5346 5347 if (BINFO_VTABLE (TYPE_BINFO (t))) 5348 gcc_assert (DECL_VIRTUAL_P (BINFO_VTABLE (TYPE_BINFO (t)))); 5349 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t)) 5350 gcc_assert (BINFO_VIRTUALS (TYPE_BINFO (t)) == NULL_TREE); 5351 5352 /* Add entries for virtual functions introduced by this class. */ 5353 BINFO_VIRTUALS (TYPE_BINFO (t)) 5354 = chainon (BINFO_VIRTUALS (TYPE_BINFO (t)), virtuals); 5355 5356 /* Set DECL_VINDEX for all functions declared in this class. */ 5357 for (vindex = 0, fn = BINFO_VIRTUALS (TYPE_BINFO (t)); 5358 fn; 5359 fn = TREE_CHAIN (fn), 5360 vindex += (TARGET_VTABLE_USES_DESCRIPTORS 5361 ? TARGET_VTABLE_USES_DESCRIPTORS : 1)) 5362 { 5363 tree fndecl = BV_FN (fn); 5364 5365 if (DECL_THUNK_P (fndecl)) 5366 /* A thunk. We should never be calling this entry directly 5367 from this vtable -- we'd use the entry for the non 5368 thunk base function. */ 5369 DECL_VINDEX (fndecl) = NULL_TREE; 5370 else if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST) 5371 DECL_VINDEX (fndecl) = build_int_cst (NULL_TREE, vindex); 5372 } 5373 } 5374 5375 finish_struct_bits (t); 5376 5377 /* Complete the rtl for any static member objects of the type we're 5378 working on. */ 5379 for (x = TYPE_FIELDS (t); x; x = TREE_CHAIN (x)) 5380 if (TREE_CODE (x) == VAR_DECL && TREE_STATIC (x) 5381 && TREE_TYPE (x) != error_mark_node 5382 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (x)), t)) 5383 DECL_MODE (x) = TYPE_MODE (t); 5384 5385 /* Done with FIELDS...now decide whether to sort these for 5386 faster lookups later. 5387 5388 We use a small number because most searches fail (succeeding 5389 ultimately as the search bores through the inheritance 5390 hierarchy), and we want this failure to occur quickly. */ 5391 5392 n_fields = count_fields (TYPE_FIELDS (t)); 5393 if (n_fields > 7) 5394 { 5395 struct sorted_fields_type *field_vec = GGC_NEWVAR 5396 (struct sorted_fields_type, 5397 sizeof (struct sorted_fields_type) + n_fields * sizeof (tree)); 5398 field_vec->len = n_fields; 5399 add_fields_to_record_type (TYPE_FIELDS (t), field_vec, 0); 5400 qsort (field_vec->elts, n_fields, sizeof (tree), 5401 field_decl_cmp); 5402 CLASSTYPE_SORTED_FIELDS (t) = field_vec; 5403 } 5404 5405 /* Complain if one of the field types requires lower visibility. */ 5406 constrain_class_visibility (t); 5407 5408 /* Make the rtl for any new vtables we have created, and unmark 5409 the base types we marked. */ 5410 finish_vtbls (t); 5411 5412 /* Build the VTT for T. */ 5413 build_vtt (t); 5414 5415 /* This warning does not make sense for Java classes, since they 5416 cannot have destructors. */ 5417 if (!TYPE_FOR_JAVA (t) && warn_nonvdtor && TYPE_POLYMORPHIC_P (t)) 5418 { 5419 tree dtor; 5420 5421 dtor = CLASSTYPE_DESTRUCTORS (t); 5422 if (/* An implicitly declared destructor is always public. And, 5423 if it were virtual, we would have created it by now. */ 5424 !dtor 5425 || (!DECL_VINDEX (dtor) 5426 && (/* public non-virtual */ 5427 (!TREE_PRIVATE (dtor) && !TREE_PROTECTED (dtor)) 5428 || (/* non-public non-virtual with friends */ 5429 (TREE_PRIVATE (dtor) || TREE_PROTECTED (dtor)) 5430 && (CLASSTYPE_FRIEND_CLASSES (t) 5431 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t))))))) 5432 warning (OPT_Wnon_virtual_dtor, 5433 "%q#T has virtual functions and accessible" 5434 " non-virtual destructor", t); 5435 } 5436 5437 complete_vars (t); 5438 5439 if (warn_overloaded_virtual) 5440 warn_hidden (t); 5441 5442 /* Class layout, assignment of virtual table slots, etc., is now 5443 complete. Give the back end a chance to tweak the visibility of 5444 the class or perform any other required target modifications. */ 5445 targetm.cxx.adjust_class_at_definition (t); 5446 5447 maybe_suppress_debug_info (t); 5448 5449 dump_class_hierarchy (t); 5450 5451 /* Finish debugging output for this type. */ 5452 rest_of_type_compilation (t, ! LOCAL_CLASS_P (t)); 5453 } 5454 5455 /* When T was built up, the member declarations were added in reverse 5456 order. Rearrange them to declaration order. */ 5457 5458 void 5459 unreverse_member_declarations (tree t) 5460 { 5461 tree next; 5462 tree prev; 5463 tree x; 5464 5465 /* The following lists are all in reverse order. Put them in 5466 declaration order now. */ 5467 TYPE_METHODS (t) = nreverse (TYPE_METHODS (t)); 5468 CLASSTYPE_DECL_LIST (t) = nreverse (CLASSTYPE_DECL_LIST (t)); 5469 5470 /* Actually, for the TYPE_FIELDS, only the non TYPE_DECLs are in 5471 reverse order, so we can't just use nreverse. */ 5472 prev = NULL_TREE; 5473 for (x = TYPE_FIELDS (t); 5474 x && TREE_CODE (x) != TYPE_DECL; 5475 x = next) 5476 { 5477 next = TREE_CHAIN (x); 5478 TREE_CHAIN (x) = prev; 5479 prev = x; 5480 } 5481 if (prev) 5482 { 5483 TREE_CHAIN (TYPE_FIELDS (t)) = x; 5484 if (prev) 5485 TYPE_FIELDS (t) = prev; 5486 } 5487 } 5488 5489 tree 5490 finish_struct (tree t, tree attributes) 5491 { 5492 location_t saved_loc = input_location; 5493 5494 /* Now that we've got all the field declarations, reverse everything 5495 as necessary. */ 5496 unreverse_member_declarations (t); 5497 5498 cplus_decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE); 5499 5500 /* Nadger the current location so that diagnostics point to the start of 5501 the struct, not the end. */ 5502 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (t)); 5503 5504 if (processing_template_decl) 5505 { 5506 tree x; 5507 5508 finish_struct_methods (t); 5509 TYPE_SIZE (t) = bitsize_zero_node; 5510 TYPE_SIZE_UNIT (t) = size_zero_node; 5511 5512 /* We need to emit an error message if this type was used as a parameter 5513 and it is an abstract type, even if it is a template. We construct 5514 a simple CLASSTYPE_PURE_VIRTUALS list without taking bases into 5515 account and we call complete_vars with this type, which will check 5516 the PARM_DECLS. Note that while the type is being defined, 5517 CLASSTYPE_PURE_VIRTUALS contains the list of the inline friends 5518 (see CLASSTYPE_INLINE_FRIENDS) so we need to clear it. */ 5519 CLASSTYPE_PURE_VIRTUALS (t) = NULL; 5520 for (x = TYPE_METHODS (t); x; x = TREE_CHAIN (x)) 5521 if (DECL_PURE_VIRTUAL_P (x)) 5522 VEC_safe_push (tree, gc, CLASSTYPE_PURE_VIRTUALS (t), x); 5523 complete_vars (t); 5524 5525 /* Remember current #pragma pack value. */ 5526 TYPE_PRECISION (t) = maximum_field_alignment; 5527 } 5528 else 5529 finish_struct_1 (t); 5530 5531 input_location = saved_loc; 5532 5533 TYPE_BEING_DEFINED (t) = 0; 5534 5535 if (current_class_type) 5536 popclass (); 5537 else 5538 error ("trying to finish struct, but kicked out due to previous parse errors"); 5539 5540 if (processing_template_decl && at_function_scope_p ()) 5541 add_stmt (build_min (TAG_DEFN, t)); 5542 5543 return t; 5544 } 5545 5546 /* Return the dynamic type of INSTANCE, if known. 5547 Used to determine whether the virtual function table is needed 5548 or not. 5549 5550 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless 5551 of our knowledge of its type. *NONNULL should be initialized 5552 before this function is called. */ 5553 5554 static tree 5555 fixed_type_or_null (tree instance, int *nonnull, int *cdtorp) 5556 { 5557 #define RECUR(T) fixed_type_or_null((T), nonnull, cdtorp) 5558 5559 switch (TREE_CODE (instance)) 5560 { 5561 case INDIRECT_REF: 5562 if (POINTER_TYPE_P (TREE_TYPE (instance))) 5563 return NULL_TREE; 5564 else 5565 return RECUR (TREE_OPERAND (instance, 0)); 5566 5567 case CALL_EXPR: 5568 /* This is a call to a constructor, hence it's never zero. */ 5569 if (TREE_HAS_CONSTRUCTOR (instance)) 5570 { 5571 if (nonnull) 5572 *nonnull = 1; 5573 return TREE_TYPE (instance); 5574 } 5575 return NULL_TREE; 5576 5577 case SAVE_EXPR: 5578 /* This is a call to a constructor, hence it's never zero. */ 5579 if (TREE_HAS_CONSTRUCTOR (instance)) 5580 { 5581 if (nonnull) 5582 *nonnull = 1; 5583 return TREE_TYPE (instance); 5584 } 5585 return RECUR (TREE_OPERAND (instance, 0)); 5586 5587 case POINTER_PLUS_EXPR: 5588 case PLUS_EXPR: 5589 case MINUS_EXPR: 5590 if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR) 5591 return RECUR (TREE_OPERAND (instance, 0)); 5592 if (TREE_CODE (TREE_OPERAND (instance, 1)) == INTEGER_CST) 5593 /* Propagate nonnull. */ 5594 return RECUR (TREE_OPERAND (instance, 0)); 5595 5596 return NULL_TREE; 5597 5598 CASE_CONVERT: 5599 return RECUR (TREE_OPERAND (instance, 0)); 5600 5601 case ADDR_EXPR: 5602 instance = TREE_OPERAND (instance, 0); 5603 if (nonnull) 5604 { 5605 /* Just because we see an ADDR_EXPR doesn't mean we're dealing 5606 with a real object -- given &p->f, p can still be null. */ 5607 tree t = get_base_address (instance); 5608 /* ??? Probably should check DECL_WEAK here. */ 5609 if (t && DECL_P (t)) 5610 *nonnull = 1; 5611 } 5612 return RECUR (instance); 5613 5614 case COMPONENT_REF: 5615 /* If this component is really a base class reference, then the field 5616 itself isn't definitive. */ 5617 if (DECL_FIELD_IS_BASE (TREE_OPERAND (instance, 1))) 5618 return RECUR (TREE_OPERAND (instance, 0)); 5619 return RECUR (TREE_OPERAND (instance, 1)); 5620 5621 case VAR_DECL: 5622 case FIELD_DECL: 5623 if (TREE_CODE (TREE_TYPE (instance)) == ARRAY_TYPE 5624 && MAYBE_CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (instance)))) 5625 { 5626 if (nonnull) 5627 *nonnull = 1; 5628 return TREE_TYPE (TREE_TYPE (instance)); 5629 } 5630 /* fall through... */ 5631 case TARGET_EXPR: 5632 case PARM_DECL: 5633 case RESULT_DECL: 5634 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (instance))) 5635 { 5636 if (nonnull) 5637 *nonnull = 1; 5638 return TREE_TYPE (instance); 5639 } 5640 else if (instance == current_class_ptr) 5641 { 5642 if (nonnull) 5643 *nonnull = 1; 5644 5645 /* if we're in a ctor or dtor, we know our type. */ 5646 if (DECL_LANG_SPECIFIC (current_function_decl) 5647 && (DECL_CONSTRUCTOR_P (current_function_decl) 5648 || DECL_DESTRUCTOR_P (current_function_decl))) 5649 { 5650 if (cdtorp) 5651 *cdtorp = 1; 5652 return TREE_TYPE (TREE_TYPE (instance)); 5653 } 5654 } 5655 else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE) 5656 { 5657 /* We only need one hash table because it is always left empty. */ 5658 static htab_t ht; 5659 if (!ht) 5660 ht = htab_create (37, 5661 htab_hash_pointer, 5662 htab_eq_pointer, 5663 /*htab_del=*/NULL); 5664 5665 /* Reference variables should be references to objects. */ 5666 if (nonnull) 5667 *nonnull = 1; 5668 5669 /* Enter the INSTANCE in a table to prevent recursion; a 5670 variable's initializer may refer to the variable 5671 itself. */ 5672 if (TREE_CODE (instance) == VAR_DECL 5673 && DECL_INITIAL (instance) 5674 && !htab_find (ht, instance)) 5675 { 5676 tree type; 5677 void **slot; 5678 5679 slot = htab_find_slot (ht, instance, INSERT); 5680 *slot = instance; 5681 type = RECUR (DECL_INITIAL (instance)); 5682 htab_remove_elt (ht, instance); 5683 5684 return type; 5685 } 5686 } 5687 return NULL_TREE; 5688 5689 default: 5690 return NULL_TREE; 5691 } 5692 #undef RECUR 5693 } 5694 5695 /* Return nonzero if the dynamic type of INSTANCE is known, and 5696 equivalent to the static type. We also handle the case where 5697 INSTANCE is really a pointer. Return negative if this is a 5698 ctor/dtor. There the dynamic type is known, but this might not be 5699 the most derived base of the original object, and hence virtual 5700 bases may not be layed out according to this type. 5701 5702 Used to determine whether the virtual function table is needed 5703 or not. 5704 5705 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless 5706 of our knowledge of its type. *NONNULL should be initialized 5707 before this function is called. */ 5708 5709 int 5710 resolves_to_fixed_type_p (tree instance, int* nonnull) 5711 { 5712 tree t = TREE_TYPE (instance); 5713 int cdtorp = 0; 5714 tree fixed = fixed_type_or_null (instance, nonnull, &cdtorp); 5715 if (fixed == NULL_TREE) 5716 return 0; 5717 if (POINTER_TYPE_P (t)) 5718 t = TREE_TYPE (t); 5719 if (!same_type_ignoring_top_level_qualifiers_p (t, fixed)) 5720 return 0; 5721 return cdtorp ? -1 : 1; 5722 } 5723 5724 5725 void 5726 init_class_processing (void) 5727 { 5728 current_class_depth = 0; 5729 current_class_stack_size = 10; 5730 current_class_stack 5731 = XNEWVEC (struct class_stack_node, current_class_stack_size); 5732 local_classes = VEC_alloc (tree, gc, 8); 5733 sizeof_biggest_empty_class = size_zero_node; 5734 5735 ridpointers[(int) RID_PUBLIC] = access_public_node; 5736 ridpointers[(int) RID_PRIVATE] = access_private_node; 5737 ridpointers[(int) RID_PROTECTED] = access_protected_node; 5738 } 5739 5740 /* Restore the cached PREVIOUS_CLASS_LEVEL. */ 5741 5742 static void 5743 restore_class_cache (void) 5744 { 5745 tree type; 5746 5747 /* We are re-entering the same class we just left, so we don't 5748 have to search the whole inheritance matrix to find all the 5749 decls to bind again. Instead, we install the cached 5750 class_shadowed list and walk through it binding names. */ 5751 push_binding_level (previous_class_level); 5752 class_binding_level = previous_class_level; 5753 /* Restore IDENTIFIER_TYPE_VALUE. */ 5754 for (type = class_binding_level->type_shadowed; 5755 type; 5756 type = TREE_CHAIN (type)) 5757 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (type), TREE_TYPE (type)); 5758 } 5759 5760 /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE as 5761 appropriate for TYPE. 5762 5763 So that we may avoid calls to lookup_name, we cache the _TYPE 5764 nodes of local TYPE_DECLs in the TREE_TYPE field of the name. 5765 5766 For multiple inheritance, we perform a two-pass depth-first search 5767 of the type lattice. */ 5768 5769 void 5770 pushclass (tree type) 5771 { 5772 class_stack_node_t csn; 5773 5774 type = TYPE_MAIN_VARIANT (type); 5775 5776 /* Make sure there is enough room for the new entry on the stack. */ 5777 if (current_class_depth + 1 >= current_class_stack_size) 5778 { 5779 current_class_stack_size *= 2; 5780 current_class_stack 5781 = XRESIZEVEC (struct class_stack_node, current_class_stack, 5782 current_class_stack_size); 5783 } 5784 5785 /* Insert a new entry on the class stack. */ 5786 csn = current_class_stack + current_class_depth; 5787 csn->name = current_class_name; 5788 csn->type = current_class_type; 5789 csn->access = current_access_specifier; 5790 csn->names_used = 0; 5791 csn->hidden = 0; 5792 current_class_depth++; 5793 5794 /* Now set up the new type. */ 5795 current_class_name = TYPE_NAME (type); 5796 if (TREE_CODE (current_class_name) == TYPE_DECL) 5797 current_class_name = DECL_NAME (current_class_name); 5798 current_class_type = type; 5799 5800 /* By default, things in classes are private, while things in 5801 structures or unions are public. */ 5802 current_access_specifier = (CLASSTYPE_DECLARED_CLASS (type) 5803 ? access_private_node 5804 : access_public_node); 5805 5806 if (previous_class_level 5807 && type != previous_class_level->this_entity 5808 && current_class_depth == 1) 5809 { 5810 /* Forcibly remove any old class remnants. */ 5811 invalidate_class_lookup_cache (); 5812 } 5813 5814 if (!previous_class_level 5815 || type != previous_class_level->this_entity 5816 || current_class_depth > 1) 5817 pushlevel_class (); 5818 else 5819 restore_class_cache (); 5820 } 5821 5822 /* When we exit a toplevel class scope, we save its binding level so 5823 that we can restore it quickly. Here, we've entered some other 5824 class, so we must invalidate our cache. */ 5825 5826 void 5827 invalidate_class_lookup_cache (void) 5828 { 5829 previous_class_level = NULL; 5830 } 5831 5832 /* Get out of the current class scope. If we were in a class scope 5833 previously, that is the one popped to. */ 5834 5835 void 5836 popclass (void) 5837 { 5838 poplevel_class (); 5839 5840 current_class_depth--; 5841 current_class_name = current_class_stack[current_class_depth].name; 5842 current_class_type = current_class_stack[current_class_depth].type; 5843 current_access_specifier = current_class_stack[current_class_depth].access; 5844 if (current_class_stack[current_class_depth].names_used) 5845 splay_tree_delete (current_class_stack[current_class_depth].names_used); 5846 } 5847 5848 /* Mark the top of the class stack as hidden. */ 5849 5850 void 5851 push_class_stack (void) 5852 { 5853 if (current_class_depth) 5854 ++current_class_stack[current_class_depth - 1].hidden; 5855 } 5856 5857 /* Mark the top of the class stack as un-hidden. */ 5858 5859 void 5860 pop_class_stack (void) 5861 { 5862 if (current_class_depth) 5863 --current_class_stack[current_class_depth - 1].hidden; 5864 } 5865 5866 /* Returns 1 if the class type currently being defined is either T or 5867 a nested type of T. */ 5868 5869 bool 5870 currently_open_class (tree t) 5871 { 5872 int i; 5873 5874 if (!CLASS_TYPE_P (t)) 5875 return false; 5876 5877 t = TYPE_MAIN_VARIANT (t); 5878 5879 /* We start looking from 1 because entry 0 is from global scope, 5880 and has no type. */ 5881 for (i = current_class_depth; i > 0; --i) 5882 { 5883 tree c; 5884 if (i == current_class_depth) 5885 c = current_class_type; 5886 else 5887 { 5888 if (current_class_stack[i].hidden) 5889 break; 5890 c = current_class_stack[i].type; 5891 } 5892 if (!c) 5893 continue; 5894 if (same_type_p (c, t)) 5895 return true; 5896 } 5897 return false; 5898 } 5899 5900 /* If either current_class_type or one of its enclosing classes are derived 5901 from T, return the appropriate type. Used to determine how we found 5902 something via unqualified lookup. */ 5903 5904 tree 5905 currently_open_derived_class (tree t) 5906 { 5907 int i; 5908 5909 /* The bases of a dependent type are unknown. */ 5910 if (dependent_type_p (t)) 5911 return NULL_TREE; 5912 5913 if (!current_class_type) 5914 return NULL_TREE; 5915 5916 if (DERIVED_FROM_P (t, current_class_type)) 5917 return current_class_type; 5918 5919 for (i = current_class_depth - 1; i > 0; --i) 5920 { 5921 if (current_class_stack[i].hidden) 5922 break; 5923 if (DERIVED_FROM_P (t, current_class_stack[i].type)) 5924 return current_class_stack[i].type; 5925 } 5926 5927 return NULL_TREE; 5928 } 5929 5930 /* When entering a class scope, all enclosing class scopes' names with 5931 static meaning (static variables, static functions, types and 5932 enumerators) have to be visible. This recursive function calls 5933 pushclass for all enclosing class contexts until global or a local 5934 scope is reached. TYPE is the enclosed class. */ 5935 5936 void 5937 push_nested_class (tree type) 5938 { 5939 /* A namespace might be passed in error cases, like A::B:C. */ 5940 if (type == NULL_TREE 5941 || !CLASS_TYPE_P (type)) 5942 return; 5943 5944 push_nested_class (DECL_CONTEXT (TYPE_MAIN_DECL (type))); 5945 5946 pushclass (type); 5947 } 5948 5949 /* Undoes a push_nested_class call. */ 5950 5951 void 5952 pop_nested_class (void) 5953 { 5954 tree context = DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type)); 5955 5956 popclass (); 5957 if (context && CLASS_TYPE_P (context)) 5958 pop_nested_class (); 5959 } 5960 5961 /* Returns the number of extern "LANG" blocks we are nested within. */ 5962 5963 int 5964 current_lang_depth (void) 5965 { 5966 return VEC_length (tree, current_lang_base); 5967 } 5968 5969 /* Set global variables CURRENT_LANG_NAME to appropriate value 5970 so that behavior of name-mangling machinery is correct. */ 5971 5972 void 5973 push_lang_context (tree name) 5974 { 5975 VEC_safe_push (tree, gc, current_lang_base, current_lang_name); 5976 5977 if (name == lang_name_cplusplus) 5978 { 5979 current_lang_name = name; 5980 } 5981 else if (name == lang_name_java) 5982 { 5983 current_lang_name = name; 5984 /* DECL_IGNORED_P is initially set for these types, to avoid clutter. 5985 (See record_builtin_java_type in decl.c.) However, that causes 5986 incorrect debug entries if these types are actually used. 5987 So we re-enable debug output after extern "Java". */ 5988 DECL_IGNORED_P (TYPE_NAME (java_byte_type_node)) = 0; 5989 DECL_IGNORED_P (TYPE_NAME (java_short_type_node)) = 0; 5990 DECL_IGNORED_P (TYPE_NAME (java_int_type_node)) = 0; 5991 DECL_IGNORED_P (TYPE_NAME (java_long_type_node)) = 0; 5992 DECL_IGNORED_P (TYPE_NAME (java_float_type_node)) = 0; 5993 DECL_IGNORED_P (TYPE_NAME (java_double_type_node)) = 0; 5994 DECL_IGNORED_P (TYPE_NAME (java_char_type_node)) = 0; 5995 DECL_IGNORED_P (TYPE_NAME (java_boolean_type_node)) = 0; 5996 } 5997 else if (name == lang_name_c) 5998 { 5999 current_lang_name = name; 6000 } 6001 else 6002 error ("language string %<\"%E\"%> not recognized", name); 6003 } 6004 6005 /* Get out of the current language scope. */ 6006 6007 void 6008 pop_lang_context (void) 6009 { 6010 current_lang_name = VEC_pop (tree, current_lang_base); 6011 } 6012 6013 /* Type instantiation routines. */ 6014 6015 /* Given an OVERLOAD and a TARGET_TYPE, return the function that 6016 matches the TARGET_TYPE. If there is no satisfactory match, return 6017 error_mark_node, and issue an error & warning messages under 6018 control of FLAGS. Permit pointers to member function if FLAGS 6019 permits. If TEMPLATE_ONLY, the name of the overloaded function was 6020 a template-id, and EXPLICIT_TARGS are the explicitly provided 6021 template arguments. 6022 6023 If OVERLOAD is for one or more member functions, then ACCESS_PATH 6024 is the base path used to reference those member functions. If 6025 TF_NO_ACCESS_CONTROL is not set in FLAGS, and the address is 6026 resolved to a member function, access checks will be performed and 6027 errors issued if appropriate. */ 6028 6029 static tree 6030 resolve_address_of_overloaded_function (tree target_type, 6031 tree overload, 6032 tsubst_flags_t flags, 6033 bool template_only, 6034 tree explicit_targs, 6035 tree access_path) 6036 { 6037 /* Here's what the standard says: 6038 6039 [over.over] 6040 6041 If the name is a function template, template argument deduction 6042 is done, and if the argument deduction succeeds, the deduced 6043 arguments are used to generate a single template function, which 6044 is added to the set of overloaded functions considered. 6045 6046 Non-member functions and static member functions match targets of 6047 type "pointer-to-function" or "reference-to-function." Nonstatic 6048 member functions match targets of type "pointer-to-member 6049 function;" the function type of the pointer to member is used to 6050 select the member function from the set of overloaded member 6051 functions. If a nonstatic member function is selected, the 6052 reference to the overloaded function name is required to have the 6053 form of a pointer to member as described in 5.3.1. 6054 6055 If more than one function is selected, any template functions in 6056 the set are eliminated if the set also contains a non-template 6057 function, and any given template function is eliminated if the 6058 set contains a second template function that is more specialized 6059 than the first according to the partial ordering rules 14.5.5.2. 6060 After such eliminations, if any, there shall remain exactly one 6061 selected function. */ 6062 6063 int is_ptrmem = 0; 6064 /* We store the matches in a TREE_LIST rooted here. The functions 6065 are the TREE_PURPOSE, not the TREE_VALUE, in this list, for easy 6066 interoperability with most_specialized_instantiation. */ 6067 tree matches = NULL_TREE; 6068 tree fn; 6069 tree target_fn_type; 6070 6071 /* By the time we get here, we should be seeing only real 6072 pointer-to-member types, not the internal POINTER_TYPE to 6073 METHOD_TYPE representation. */ 6074 gcc_assert (TREE_CODE (target_type) != POINTER_TYPE 6075 || TREE_CODE (TREE_TYPE (target_type)) != METHOD_TYPE); 6076 6077 gcc_assert (is_overloaded_fn (overload)); 6078 6079 /* Check that the TARGET_TYPE is reasonable. */ 6080 if (TYPE_PTRFN_P (target_type)) 6081 /* This is OK. */; 6082 else if (TYPE_PTRMEMFUNC_P (target_type)) 6083 /* This is OK, too. */ 6084 is_ptrmem = 1; 6085 else if (TREE_CODE (target_type) == FUNCTION_TYPE) 6086 /* This is OK, too. This comes from a conversion to reference 6087 type. */ 6088 target_type = build_reference_type (target_type); 6089 else 6090 { 6091 if (flags & tf_error) 6092 error ("cannot resolve overloaded function %qD based on" 6093 " conversion to type %qT", 6094 DECL_NAME (OVL_FUNCTION (overload)), target_type); 6095 return error_mark_node; 6096 } 6097 6098 /* Non-member functions and static member functions match targets of type 6099 "pointer-to-function" or "reference-to-function." Nonstatic member 6100 functions match targets of type "pointer-to-member-function;" the 6101 function type of the pointer to member is used to select the member 6102 function from the set of overloaded member functions. 6103 6104 So figure out the FUNCTION_TYPE that we want to match against. */ 6105 target_fn_type = static_fn_type (target_type); 6106 6107 /* If we can find a non-template function that matches, we can just 6108 use it. There's no point in generating template instantiations 6109 if we're just going to throw them out anyhow. But, of course, we 6110 can only do this when we don't *need* a template function. */ 6111 if (!template_only) 6112 { 6113 tree fns; 6114 6115 for (fns = overload; fns; fns = OVL_NEXT (fns)) 6116 { 6117 tree fn = OVL_CURRENT (fns); 6118 6119 if (TREE_CODE (fn) == TEMPLATE_DECL) 6120 /* We're not looking for templates just yet. */ 6121 continue; 6122 6123 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE) 6124 != is_ptrmem) 6125 /* We're looking for a non-static member, and this isn't 6126 one, or vice versa. */ 6127 continue; 6128 6129 /* Ignore functions which haven't been explicitly 6130 declared. */ 6131 if (DECL_ANTICIPATED (fn)) 6132 continue; 6133 6134 /* See if there's a match. */ 6135 if (same_type_p (target_fn_type, static_fn_type (fn))) 6136 matches = tree_cons (fn, NULL_TREE, matches); 6137 } 6138 } 6139 6140 /* Now, if we've already got a match (or matches), there's no need 6141 to proceed to the template functions. But, if we don't have a 6142 match we need to look at them, too. */ 6143 if (!matches) 6144 { 6145 tree target_arg_types; 6146 tree target_ret_type; 6147 tree fns; 6148 tree *args; 6149 unsigned int nargs, ia; 6150 tree arg; 6151 6152 target_arg_types = TYPE_ARG_TYPES (target_fn_type); 6153 target_ret_type = TREE_TYPE (target_fn_type); 6154 6155 nargs = list_length (target_arg_types); 6156 args = XALLOCAVEC (tree, nargs); 6157 for (arg = target_arg_types, ia = 0; 6158 arg != NULL_TREE && arg != void_list_node; 6159 arg = TREE_CHAIN (arg), ++ia) 6160 args[ia] = TREE_VALUE (arg); 6161 nargs = ia; 6162 6163 for (fns = overload; fns; fns = OVL_NEXT (fns)) 6164 { 6165 tree fn = OVL_CURRENT (fns); 6166 tree instantiation; 6167 tree targs; 6168 6169 if (TREE_CODE (fn) != TEMPLATE_DECL) 6170 /* We're only looking for templates. */ 6171 continue; 6172 6173 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE) 6174 != is_ptrmem) 6175 /* We're not looking for a non-static member, and this is 6176 one, or vice versa. */ 6177 continue; 6178 6179 /* Try to do argument deduction. */ 6180 targs = make_tree_vec (DECL_NTPARMS (fn)); 6181 if (fn_type_unification (fn, explicit_targs, targs, args, nargs, 6182 target_ret_type, DEDUCE_EXACT, 6183 LOOKUP_NORMAL)) 6184 /* Argument deduction failed. */ 6185 continue; 6186 6187 /* Instantiate the template. */ 6188 instantiation = instantiate_template (fn, targs, flags); 6189 if (instantiation == error_mark_node) 6190 /* Instantiation failed. */ 6191 continue; 6192 6193 /* See if there's a match. */ 6194 if (same_type_p (target_fn_type, static_fn_type (instantiation))) 6195 matches = tree_cons (instantiation, fn, matches); 6196 } 6197 6198 /* Now, remove all but the most specialized of the matches. */ 6199 if (matches) 6200 { 6201 tree match = most_specialized_instantiation (matches); 6202 6203 if (match != error_mark_node) 6204 matches = tree_cons (TREE_PURPOSE (match), 6205 NULL_TREE, 6206 NULL_TREE); 6207 } 6208 } 6209 6210 /* Now we should have exactly one function in MATCHES. */ 6211 if (matches == NULL_TREE) 6212 { 6213 /* There were *no* matches. */ 6214 if (flags & tf_error) 6215 { 6216 error ("no matches converting function %qD to type %q#T", 6217 DECL_NAME (OVL_CURRENT (overload)), 6218 target_type); 6219 6220 /* print_candidates expects a chain with the functions in 6221 TREE_VALUE slots, so we cons one up here (we're losing anyway, 6222 so why be clever?). */ 6223 for (; overload; overload = OVL_NEXT (overload)) 6224 matches = tree_cons (NULL_TREE, OVL_CURRENT (overload), 6225 matches); 6226 6227 print_candidates (matches); 6228 } 6229 return error_mark_node; 6230 } 6231 else if (TREE_CHAIN (matches)) 6232 { 6233 /* There were too many matches. First check if they're all 6234 the same function. */ 6235 tree match; 6236 6237 fn = TREE_PURPOSE (matches); 6238 for (match = TREE_CHAIN (matches); match; match = TREE_CHAIN (match)) 6239 if (!decls_match (fn, TREE_PURPOSE (match))) 6240 break; 6241 6242 if (match) 6243 { 6244 if (flags & tf_error) 6245 { 6246 error ("converting overloaded function %qD to type %q#T is ambiguous", 6247 DECL_NAME (OVL_FUNCTION (overload)), 6248 target_type); 6249 6250 /* Since print_candidates expects the functions in the 6251 TREE_VALUE slot, we flip them here. */ 6252 for (match = matches; match; match = TREE_CHAIN (match)) 6253 TREE_VALUE (match) = TREE_PURPOSE (match); 6254 6255 print_candidates (matches); 6256 } 6257 6258 return error_mark_node; 6259 } 6260 } 6261 6262 /* Good, exactly one match. Now, convert it to the correct type. */ 6263 fn = TREE_PURPOSE (matches); 6264 6265 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn) 6266 && !(flags & tf_ptrmem_ok) && !flag_ms_extensions) 6267 { 6268 static int explained; 6269 6270 if (!(flags & tf_error)) 6271 return error_mark_node; 6272 6273 permerror (input_location, "assuming pointer to member %qD", fn); 6274 if (!explained) 6275 { 6276 inform (input_location, "(a pointer to member can only be formed with %<&%E%>)", fn); 6277 explained = 1; 6278 } 6279 } 6280 6281 /* If we're doing overload resolution purely for the purpose of 6282 determining conversion sequences, we should not consider the 6283 function used. If this conversion sequence is selected, the 6284 function will be marked as used at this point. */ 6285 if (!(flags & tf_conv)) 6286 { 6287 /* Make =delete work with SFINAE. */ 6288 if (DECL_DELETED_FN (fn) && !(flags & tf_error)) 6289 return error_mark_node; 6290 6291 mark_used (fn); 6292 } 6293 6294 /* We could not check access to member functions when this 6295 expression was originally created since we did not know at that 6296 time to which function the expression referred. */ 6297 if (!(flags & tf_no_access_control) 6298 && DECL_FUNCTION_MEMBER_P (fn)) 6299 { 6300 gcc_assert (access_path); 6301 perform_or_defer_access_check (access_path, fn, fn); 6302 } 6303 6304 if (TYPE_PTRFN_P (target_type) || TYPE_PTRMEMFUNC_P (target_type)) 6305 return cp_build_unary_op (ADDR_EXPR, fn, 0, flags); 6306 else 6307 { 6308 /* The target must be a REFERENCE_TYPE. Above, cp_build_unary_op 6309 will mark the function as addressed, but here we must do it 6310 explicitly. */ 6311 cxx_mark_addressable (fn); 6312 6313 return fn; 6314 } 6315 } 6316 6317 /* This function will instantiate the type of the expression given in 6318 RHS to match the type of LHSTYPE. If errors exist, then return 6319 error_mark_node. FLAGS is a bit mask. If TF_ERROR is set, then 6320 we complain on errors. If we are not complaining, never modify rhs, 6321 as overload resolution wants to try many possible instantiations, in 6322 the hope that at least one will work. 6323 6324 For non-recursive calls, LHSTYPE should be a function, pointer to 6325 function, or a pointer to member function. */ 6326 6327 tree 6328 instantiate_type (tree lhstype, tree rhs, tsubst_flags_t flags) 6329 { 6330 tsubst_flags_t flags_in = flags; 6331 tree access_path = NULL_TREE; 6332 6333 flags &= ~tf_ptrmem_ok; 6334 6335 if (TREE_CODE (lhstype) == UNKNOWN_TYPE) 6336 { 6337 if (flags & tf_error) 6338 error ("not enough type information"); 6339 return error_mark_node; 6340 } 6341 6342 if (TREE_TYPE (rhs) != NULL_TREE && ! (type_unknown_p (rhs))) 6343 { 6344 if (same_type_p (lhstype, TREE_TYPE (rhs))) 6345 return rhs; 6346 if (flag_ms_extensions 6347 && TYPE_PTRMEMFUNC_P (lhstype) 6348 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (rhs))) 6349 /* Microsoft allows `A::f' to be resolved to a 6350 pointer-to-member. */ 6351 ; 6352 else 6353 { 6354 if (flags & tf_error) 6355 error ("argument of type %qT does not match %qT", 6356 TREE_TYPE (rhs), lhstype); 6357 return error_mark_node; 6358 } 6359 } 6360 6361 if (TREE_CODE (rhs) == BASELINK) 6362 { 6363 access_path = BASELINK_ACCESS_BINFO (rhs); 6364 rhs = BASELINK_FUNCTIONS (rhs); 6365 } 6366 6367 /* If we are in a template, and have a NON_DEPENDENT_EXPR, we cannot 6368 deduce any type information. */ 6369 if (TREE_CODE (rhs) == NON_DEPENDENT_EXPR) 6370 { 6371 if (flags & tf_error) 6372 error ("not enough type information"); 6373 return error_mark_node; 6374 } 6375 6376 /* There only a few kinds of expressions that may have a type 6377 dependent on overload resolution. */ 6378 gcc_assert (TREE_CODE (rhs) == ADDR_EXPR 6379 || TREE_CODE (rhs) == COMPONENT_REF 6380 || really_overloaded_fn (rhs) 6381 || (flag_ms_extensions && TREE_CODE (rhs) == FUNCTION_DECL)); 6382 6383 /* This should really only be used when attempting to distinguish 6384 what sort of a pointer to function we have. For now, any 6385 arithmetic operation which is not supported on pointers 6386 is rejected as an error. */ 6387 6388 switch (TREE_CODE (rhs)) 6389 { 6390 case COMPONENT_REF: 6391 { 6392 tree member = TREE_OPERAND (rhs, 1); 6393 6394 member = instantiate_type (lhstype, member, flags); 6395 if (member != error_mark_node 6396 && TREE_SIDE_EFFECTS (TREE_OPERAND (rhs, 0))) 6397 /* Do not lose object's side effects. */ 6398 return build2 (COMPOUND_EXPR, TREE_TYPE (member), 6399 TREE_OPERAND (rhs, 0), member); 6400 return member; 6401 } 6402 6403 case OFFSET_REF: 6404 rhs = TREE_OPERAND (rhs, 1); 6405 if (BASELINK_P (rhs)) 6406 return instantiate_type (lhstype, rhs, flags_in); 6407 6408 /* This can happen if we are forming a pointer-to-member for a 6409 member template. */ 6410 gcc_assert (TREE_CODE (rhs) == TEMPLATE_ID_EXPR); 6411 6412 /* Fall through. */ 6413 6414 case TEMPLATE_ID_EXPR: 6415 { 6416 tree fns = TREE_OPERAND (rhs, 0); 6417 tree args = TREE_OPERAND (rhs, 1); 6418 6419 return 6420 resolve_address_of_overloaded_function (lhstype, fns, flags_in, 6421 /*template_only=*/true, 6422 args, access_path); 6423 } 6424 6425 case OVERLOAD: 6426 case FUNCTION_DECL: 6427 return 6428 resolve_address_of_overloaded_function (lhstype, rhs, flags_in, 6429 /*template_only=*/false, 6430 /*explicit_targs=*/NULL_TREE, 6431 access_path); 6432 6433 case ADDR_EXPR: 6434 { 6435 if (PTRMEM_OK_P (rhs)) 6436 flags |= tf_ptrmem_ok; 6437 6438 return instantiate_type (lhstype, TREE_OPERAND (rhs, 0), flags); 6439 } 6440 6441 case ERROR_MARK: 6442 return error_mark_node; 6443 6444 default: 6445 gcc_unreachable (); 6446 } 6447 return error_mark_node; 6448 } 6449 6450 /* Return the name of the virtual function pointer field 6451 (as an IDENTIFIER_NODE) for the given TYPE. Note that 6452 this may have to look back through base types to find the 6453 ultimate field name. (For single inheritance, these could 6454 all be the same name. Who knows for multiple inheritance). */ 6455 6456 static tree 6457 get_vfield_name (tree type) 6458 { 6459 tree binfo, base_binfo; 6460 char *buf; 6461 6462 for (binfo = TYPE_BINFO (type); 6463 BINFO_N_BASE_BINFOS (binfo); 6464 binfo = base_binfo) 6465 { 6466 base_binfo = BINFO_BASE_BINFO (binfo, 0); 6467 6468 if (BINFO_VIRTUAL_P (base_binfo) 6469 || !TYPE_CONTAINS_VPTR_P (BINFO_TYPE (base_binfo))) 6470 break; 6471 } 6472 6473 type = BINFO_TYPE (binfo); 6474 buf = (char *) alloca (sizeof (VFIELD_NAME_FORMAT) 6475 + TYPE_NAME_LENGTH (type) + 2); 6476 sprintf (buf, VFIELD_NAME_FORMAT, 6477 IDENTIFIER_POINTER (constructor_name (type))); 6478 return get_identifier (buf); 6479 } 6480 6481 void 6482 print_class_statistics (void) 6483 { 6484 #ifdef GATHER_STATISTICS 6485 fprintf (stderr, "convert_harshness = %d\n", n_convert_harshness); 6486 fprintf (stderr, "compute_conversion_costs = %d\n", n_compute_conversion_costs); 6487 if (n_vtables) 6488 { 6489 fprintf (stderr, "vtables = %d; vtable searches = %d\n", 6490 n_vtables, n_vtable_searches); 6491 fprintf (stderr, "vtable entries = %d; vtable elems = %d\n", 6492 n_vtable_entries, n_vtable_elems); 6493 } 6494 #endif 6495 } 6496 6497 /* Build a dummy reference to ourselves so Derived::Base (and A::A) works, 6498 according to [class]: 6499 The class-name is also inserted 6500 into the scope of the class itself. For purposes of access checking, 6501 the inserted class name is treated as if it were a public member name. */ 6502 6503 void 6504 build_self_reference (void) 6505 { 6506 tree name = constructor_name (current_class_type); 6507 tree value = build_lang_decl (TYPE_DECL, name, current_class_type); 6508 tree saved_cas; 6509 6510 DECL_NONLOCAL (value) = 1; 6511 DECL_CONTEXT (value) = current_class_type; 6512 DECL_ARTIFICIAL (value) = 1; 6513 SET_DECL_SELF_REFERENCE_P (value); 6514 cp_set_underlying_type (value); 6515 6516 if (processing_template_decl) 6517 value = push_template_decl (value); 6518 6519 saved_cas = current_access_specifier; 6520 current_access_specifier = access_public_node; 6521 finish_member_declaration (value); 6522 current_access_specifier = saved_cas; 6523 } 6524 6525 /* Returns 1 if TYPE contains only padding bytes. */ 6526 6527 int 6528 is_empty_class (tree type) 6529 { 6530 if (type == error_mark_node) 6531 return 0; 6532 6533 if (! CLASS_TYPE_P (type)) 6534 return 0; 6535 6536 /* In G++ 3.2, whether or not a class was empty was determined by 6537 looking at its size. */ 6538 if (abi_version_at_least (2)) 6539 return CLASSTYPE_EMPTY_P (type); 6540 else 6541 return integer_zerop (CLASSTYPE_SIZE (type)); 6542 } 6543 6544 /* Returns true if TYPE contains an empty class. */ 6545 6546 static bool 6547 contains_empty_class_p (tree type) 6548 { 6549 if (is_empty_class (type)) 6550 return true; 6551 if (CLASS_TYPE_P (type)) 6552 { 6553 tree field; 6554 tree binfo; 6555 tree base_binfo; 6556 int i; 6557 6558 for (binfo = TYPE_BINFO (type), i = 0; 6559 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i) 6560 if (contains_empty_class_p (BINFO_TYPE (base_binfo))) 6561 return true; 6562 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field)) 6563 if (TREE_CODE (field) == FIELD_DECL 6564 && !DECL_ARTIFICIAL (field) 6565 && is_empty_class (TREE_TYPE (field))) 6566 return true; 6567 } 6568 else if (TREE_CODE (type) == ARRAY_TYPE) 6569 return contains_empty_class_p (TREE_TYPE (type)); 6570 return false; 6571 } 6572 6573 /* Returns true if TYPE contains no actual data, just various 6574 possible combinations of empty classes. */ 6575 6576 bool 6577 is_really_empty_class (tree type) 6578 { 6579 if (is_empty_class (type)) 6580 return true; 6581 if (CLASS_TYPE_P (type)) 6582 { 6583 tree field; 6584 tree binfo; 6585 tree base_binfo; 6586 int i; 6587 6588 for (binfo = TYPE_BINFO (type), i = 0; 6589 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i) 6590 if (!is_really_empty_class (BINFO_TYPE (base_binfo))) 6591 return false; 6592 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field)) 6593 if (TREE_CODE (field) == FIELD_DECL 6594 && !DECL_ARTIFICIAL (field) 6595 && !is_really_empty_class (TREE_TYPE (field))) 6596 return false; 6597 return true; 6598 } 6599 else if (TREE_CODE (type) == ARRAY_TYPE) 6600 return is_really_empty_class (TREE_TYPE (type)); 6601 return false; 6602 } 6603 6604 /* Note that NAME was looked up while the current class was being 6605 defined and that the result of that lookup was DECL. */ 6606 6607 void 6608 maybe_note_name_used_in_class (tree name, tree decl) 6609 { 6610 splay_tree names_used; 6611 6612 /* If we're not defining a class, there's nothing to do. */ 6613 if (!(innermost_scope_kind() == sk_class 6614 && TYPE_BEING_DEFINED (current_class_type) 6615 && !LAMBDA_TYPE_P (current_class_type))) 6616 return; 6617 6618 /* If there's already a binding for this NAME, then we don't have 6619 anything to worry about. */ 6620 if (lookup_member (current_class_type, name, 6621 /*protect=*/0, /*want_type=*/false)) 6622 return; 6623 6624 if (!current_class_stack[current_class_depth - 1].names_used) 6625 current_class_stack[current_class_depth - 1].names_used 6626 = splay_tree_new (splay_tree_compare_pointers, 0, 0); 6627 names_used = current_class_stack[current_class_depth - 1].names_used; 6628 6629 splay_tree_insert (names_used, 6630 (splay_tree_key) name, 6631 (splay_tree_value) decl); 6632 } 6633 6634 /* Note that NAME was declared (as DECL) in the current class. Check 6635 to see that the declaration is valid. */ 6636 6637 void 6638 note_name_declared_in_class (tree name, tree decl) 6639 { 6640 splay_tree names_used; 6641 splay_tree_node n; 6642 6643 /* Look to see if we ever used this name. */ 6644 names_used 6645 = current_class_stack[current_class_depth - 1].names_used; 6646 if (!names_used) 6647 return; 6648 6649 n = splay_tree_lookup (names_used, (splay_tree_key) name); 6650 if (n) 6651 { 6652 /* [basic.scope.class] 6653 6654 A name N used in a class S shall refer to the same declaration 6655 in its context and when re-evaluated in the completed scope of 6656 S. */ 6657 permerror (input_location, "declaration of %q#D", decl); 6658 permerror (input_location, "changes meaning of %qD from %q+#D", 6659 DECL_NAME (OVL_CURRENT (decl)), (tree) n->value); 6660 } 6661 } 6662 6663 /* Returns the VAR_DECL for the complete vtable associated with BINFO. 6664 Secondary vtables are merged with primary vtables; this function 6665 will return the VAR_DECL for the primary vtable. */ 6666 6667 tree 6668 get_vtbl_decl_for_binfo (tree binfo) 6669 { 6670 tree decl; 6671 6672 decl = BINFO_VTABLE (binfo); 6673 if (decl && TREE_CODE (decl) == POINTER_PLUS_EXPR) 6674 { 6675 gcc_assert (TREE_CODE (TREE_OPERAND (decl, 0)) == ADDR_EXPR); 6676 decl = TREE_OPERAND (TREE_OPERAND (decl, 0), 0); 6677 } 6678 if (decl) 6679 gcc_assert (TREE_CODE (decl) == VAR_DECL); 6680 return decl; 6681 } 6682 6683 6684 /* Returns the binfo for the primary base of BINFO. If the resulting 6685 BINFO is a virtual base, and it is inherited elsewhere in the 6686 hierarchy, then the returned binfo might not be the primary base of 6687 BINFO in the complete object. Check BINFO_PRIMARY_P or 6688 BINFO_LOST_PRIMARY_P to be sure. */ 6689 6690 static tree 6691 get_primary_binfo (tree binfo) 6692 { 6693 tree primary_base; 6694 6695 primary_base = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (binfo)); 6696 if (!primary_base) 6697 return NULL_TREE; 6698 6699 return copied_binfo (primary_base, binfo); 6700 } 6701 6702 /* If INDENTED_P is zero, indent to INDENT. Return nonzero. */ 6703 6704 static int 6705 maybe_indent_hierarchy (FILE * stream, int indent, int indented_p) 6706 { 6707 if (!indented_p) 6708 fprintf (stream, "%*s", indent, ""); 6709 return 1; 6710 } 6711 6712 /* Dump the offsets of all the bases rooted at BINFO to STREAM. 6713 INDENT should be zero when called from the top level; it is 6714 incremented recursively. IGO indicates the next expected BINFO in 6715 inheritance graph ordering. */ 6716 6717 static tree 6718 dump_class_hierarchy_r (FILE *stream, 6719 int flags, 6720 tree binfo, 6721 tree igo, 6722 int indent) 6723 { 6724 int indented = 0; 6725 tree base_binfo; 6726 int i; 6727 6728 indented = maybe_indent_hierarchy (stream, indent, 0); 6729 fprintf (stream, "%s (0x%lx) ", 6730 type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER), 6731 (unsigned long) binfo); 6732 if (binfo != igo) 6733 { 6734 fprintf (stream, "alternative-path\n"); 6735 return igo; 6736 } 6737 igo = TREE_CHAIN (binfo); 6738 6739 fprintf (stream, HOST_WIDE_INT_PRINT_DEC, 6740 tree_low_cst (BINFO_OFFSET (binfo), 0)); 6741 if (is_empty_class (BINFO_TYPE (binfo))) 6742 fprintf (stream, " empty"); 6743 else if (CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (binfo))) 6744 fprintf (stream, " nearly-empty"); 6745 if (BINFO_VIRTUAL_P (binfo)) 6746 fprintf (stream, " virtual"); 6747 fprintf (stream, "\n"); 6748 6749 indented = 0; 6750 if (BINFO_PRIMARY_P (binfo)) 6751 { 6752 indented = maybe_indent_hierarchy (stream, indent + 3, indented); 6753 fprintf (stream, " primary-for %s (0x%lx)", 6754 type_as_string (BINFO_TYPE (BINFO_INHERITANCE_CHAIN (binfo)), 6755 TFF_PLAIN_IDENTIFIER), 6756 (unsigned long)BINFO_INHERITANCE_CHAIN (binfo)); 6757 } 6758 if (BINFO_LOST_PRIMARY_P (binfo)) 6759 { 6760 indented = maybe_indent_hierarchy (stream, indent + 3, indented); 6761 fprintf (stream, " lost-primary"); 6762 } 6763 if (indented) 6764 fprintf (stream, "\n"); 6765 6766 if (!(flags & TDF_SLIM)) 6767 { 6768 int indented = 0; 6769 6770 if (BINFO_SUBVTT_INDEX (binfo)) 6771 { 6772 indented = maybe_indent_hierarchy (stream, indent + 3, indented); 6773 fprintf (stream, " subvttidx=%s", 6774 expr_as_string (BINFO_SUBVTT_INDEX (binfo), 6775 TFF_PLAIN_IDENTIFIER)); 6776 } 6777 if (BINFO_VPTR_INDEX (binfo)) 6778 { 6779 indented = maybe_indent_hierarchy (stream, indent + 3, indented); 6780 fprintf (stream, " vptridx=%s", 6781 expr_as_string (BINFO_VPTR_INDEX (binfo), 6782 TFF_PLAIN_IDENTIFIER)); 6783 } 6784 if (BINFO_VPTR_FIELD (binfo)) 6785 { 6786 indented = maybe_indent_hierarchy (stream, indent + 3, indented); 6787 fprintf (stream, " vbaseoffset=%s", 6788 expr_as_string (BINFO_VPTR_FIELD (binfo), 6789 TFF_PLAIN_IDENTIFIER)); 6790 } 6791 if (BINFO_VTABLE (binfo)) 6792 { 6793 indented = maybe_indent_hierarchy (stream, indent + 3, indented); 6794 fprintf (stream, " vptr=%s", 6795 expr_as_string (BINFO_VTABLE (binfo), 6796 TFF_PLAIN_IDENTIFIER)); 6797 } 6798 6799 if (indented) 6800 fprintf (stream, "\n"); 6801 } 6802 6803 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++) 6804 igo = dump_class_hierarchy_r (stream, flags, base_binfo, igo, indent + 2); 6805 6806 return igo; 6807 } 6808 6809 /* Dump the BINFO hierarchy for T. */ 6810 6811 static void 6812 dump_class_hierarchy_1 (FILE *stream, int flags, tree t) 6813 { 6814 fprintf (stream, "Class %s\n", type_as_string (t, TFF_PLAIN_IDENTIFIER)); 6815 fprintf (stream, " size=%lu align=%lu\n", 6816 (unsigned long)(tree_low_cst (TYPE_SIZE (t), 0) / BITS_PER_UNIT), 6817 (unsigned long)(TYPE_ALIGN (t) / BITS_PER_UNIT)); 6818 fprintf (stream, " base size=%lu base align=%lu\n", 6819 (unsigned long)(tree_low_cst (TYPE_SIZE (CLASSTYPE_AS_BASE (t)), 0) 6820 / BITS_PER_UNIT), 6821 (unsigned long)(TYPE_ALIGN (CLASSTYPE_AS_BASE (t)) 6822 / BITS_PER_UNIT)); 6823 dump_class_hierarchy_r (stream, flags, TYPE_BINFO (t), TYPE_BINFO (t), 0); 6824 fprintf (stream, "\n"); 6825 } 6826 6827 /* Debug interface to hierarchy dumping. */ 6828 6829 void 6830 debug_class (tree t) 6831 { 6832 dump_class_hierarchy_1 (stderr, TDF_SLIM, t); 6833 } 6834 6835 static void 6836 dump_class_hierarchy (tree t) 6837 { 6838 int flags; 6839 FILE *stream = dump_begin (TDI_class, &flags); 6840 6841 if (stream) 6842 { 6843 dump_class_hierarchy_1 (stream, flags, t); 6844 dump_end (TDI_class, stream); 6845 } 6846 } 6847 6848 static void 6849 dump_array (FILE * stream, tree decl) 6850 { 6851 tree value; 6852 unsigned HOST_WIDE_INT ix; 6853 HOST_WIDE_INT elt; 6854 tree size = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (decl))); 6855 6856 elt = (tree_low_cst (TYPE_SIZE (TREE_TYPE (TREE_TYPE (decl))), 0) 6857 / BITS_PER_UNIT); 6858 fprintf (stream, "%s:", decl_as_string (decl, TFF_PLAIN_IDENTIFIER)); 6859 fprintf (stream, " %s entries", 6860 expr_as_string (size_binop (PLUS_EXPR, size, size_one_node), 6861 TFF_PLAIN_IDENTIFIER)); 6862 fprintf (stream, "\n"); 6863 6864 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)), 6865 ix, value) 6866 fprintf (stream, "%-4ld %s\n", (long)(ix * elt), 6867 expr_as_string (value, TFF_PLAIN_IDENTIFIER)); 6868 } 6869 6870 static void 6871 dump_vtable (tree t, tree binfo, tree vtable) 6872 { 6873 int flags; 6874 FILE *stream = dump_begin (TDI_class, &flags); 6875 6876 if (!stream) 6877 return; 6878 6879 if (!(flags & TDF_SLIM)) 6880 { 6881 int ctor_vtbl_p = TYPE_BINFO (t) != binfo; 6882 6883 fprintf (stream, "%s for %s", 6884 ctor_vtbl_p ? "Construction vtable" : "Vtable", 6885 type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER)); 6886 if (ctor_vtbl_p) 6887 { 6888 if (!BINFO_VIRTUAL_P (binfo)) 6889 fprintf (stream, " (0x%lx instance)", (unsigned long)binfo); 6890 fprintf (stream, " in %s", type_as_string (t, TFF_PLAIN_IDENTIFIER)); 6891 } 6892 fprintf (stream, "\n"); 6893 dump_array (stream, vtable); 6894 fprintf (stream, "\n"); 6895 } 6896 6897 dump_end (TDI_class, stream); 6898 } 6899 6900 static void 6901 dump_vtt (tree t, tree vtt) 6902 { 6903 int flags; 6904 FILE *stream = dump_begin (TDI_class, &flags); 6905 6906 if (!stream) 6907 return; 6908 6909 if (!(flags & TDF_SLIM)) 6910 { 6911 fprintf (stream, "VTT for %s\n", 6912 type_as_string (t, TFF_PLAIN_IDENTIFIER)); 6913 dump_array (stream, vtt); 6914 fprintf (stream, "\n"); 6915 } 6916 6917 dump_end (TDI_class, stream); 6918 } 6919 6920 /* Dump a function or thunk and its thunkees. */ 6921 6922 static void 6923 dump_thunk (FILE *stream, int indent, tree thunk) 6924 { 6925 static const char spaces[] = " "; 6926 tree name = DECL_NAME (thunk); 6927 tree thunks; 6928 6929 fprintf (stream, "%.*s%p %s %s", indent, spaces, 6930 (void *)thunk, 6931 !DECL_THUNK_P (thunk) ? "function" 6932 : DECL_THIS_THUNK_P (thunk) ? "this-thunk" : "covariant-thunk", 6933 name ? IDENTIFIER_POINTER (name) : "<unset>"); 6934 if (DECL_THUNK_P (thunk)) 6935 { 6936 HOST_WIDE_INT fixed_adjust = THUNK_FIXED_OFFSET (thunk); 6937 tree virtual_adjust = THUNK_VIRTUAL_OFFSET (thunk); 6938 6939 fprintf (stream, " fixed=" HOST_WIDE_INT_PRINT_DEC, fixed_adjust); 6940 if (!virtual_adjust) 6941 /*NOP*/; 6942 else if (DECL_THIS_THUNK_P (thunk)) 6943 fprintf (stream, " vcall=" HOST_WIDE_INT_PRINT_DEC, 6944 tree_low_cst (virtual_adjust, 0)); 6945 else 6946 fprintf (stream, " vbase=" HOST_WIDE_INT_PRINT_DEC "(%s)", 6947 tree_low_cst (BINFO_VPTR_FIELD (virtual_adjust), 0), 6948 type_as_string (BINFO_TYPE (virtual_adjust), TFF_SCOPE)); 6949 if (THUNK_ALIAS (thunk)) 6950 fprintf (stream, " alias to %p", (void *)THUNK_ALIAS (thunk)); 6951 } 6952 fprintf (stream, "\n"); 6953 for (thunks = DECL_THUNKS (thunk); thunks; thunks = TREE_CHAIN (thunks)) 6954 dump_thunk (stream, indent + 2, thunks); 6955 } 6956 6957 /* Dump the thunks for FN. */ 6958 6959 void 6960 debug_thunks (tree fn) 6961 { 6962 dump_thunk (stderr, 0, fn); 6963 } 6964 6965 /* Virtual function table initialization. */ 6966 6967 /* Create all the necessary vtables for T and its base classes. */ 6968 6969 static void 6970 finish_vtbls (tree t) 6971 { 6972 tree list; 6973 tree vbase; 6974 6975 /* We lay out the primary and secondary vtables in one contiguous 6976 vtable. The primary vtable is first, followed by the non-virtual 6977 secondary vtables in inheritance graph order. */ 6978 list = build_tree_list (BINFO_VTABLE (TYPE_BINFO (t)), NULL_TREE); 6979 accumulate_vtbl_inits (TYPE_BINFO (t), TYPE_BINFO (t), 6980 TYPE_BINFO (t), t, list); 6981 6982 /* Then come the virtual bases, also in inheritance graph order. */ 6983 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase)) 6984 { 6985 if (!BINFO_VIRTUAL_P (vbase)) 6986 continue; 6987 accumulate_vtbl_inits (vbase, vbase, TYPE_BINFO (t), t, list); 6988 } 6989 6990 if (BINFO_VTABLE (TYPE_BINFO (t))) 6991 initialize_vtable (TYPE_BINFO (t), TREE_VALUE (list)); 6992 } 6993 6994 /* Initialize the vtable for BINFO with the INITS. */ 6995 6996 static void 6997 initialize_vtable (tree binfo, tree inits) 6998 { 6999 tree decl; 7000 7001 layout_vtable_decl (binfo, list_length (inits)); 7002 decl = get_vtbl_decl_for_binfo (binfo); 7003 initialize_artificial_var (decl, inits); 7004 dump_vtable (BINFO_TYPE (binfo), binfo, decl); 7005 } 7006 7007 /* Build the VTT (virtual table table) for T. 7008 A class requires a VTT if it has virtual bases. 7009 7010 This holds 7011 1 - primary virtual pointer for complete object T 7012 2 - secondary VTTs for each direct non-virtual base of T which requires a 7013 VTT 7014 3 - secondary virtual pointers for each direct or indirect base of T which 7015 has virtual bases or is reachable via a virtual path from T. 7016 4 - secondary VTTs for each direct or indirect virtual base of T. 7017 7018 Secondary VTTs look like complete object VTTs without part 4. */ 7019 7020 static void 7021 build_vtt (tree t) 7022 { 7023 tree inits; 7024 tree type; 7025 tree vtt; 7026 tree index; 7027 7028 /* Build up the initializers for the VTT. */ 7029 inits = NULL_TREE; 7030 index = size_zero_node; 7031 build_vtt_inits (TYPE_BINFO (t), t, &inits, &index); 7032 7033 /* If we didn't need a VTT, we're done. */ 7034 if (!inits) 7035 return; 7036 7037 /* Figure out the type of the VTT. */ 7038 type = build_index_type (size_int (list_length (inits) - 1)); 7039 type = build_cplus_array_type (const_ptr_type_node, type); 7040 7041 /* Now, build the VTT object itself. */ 7042 vtt = build_vtable (t, mangle_vtt_for_type (t), type); 7043 initialize_artificial_var (vtt, inits); 7044 /* Add the VTT to the vtables list. */ 7045 TREE_CHAIN (vtt) = TREE_CHAIN (CLASSTYPE_VTABLES (t)); 7046 TREE_CHAIN (CLASSTYPE_VTABLES (t)) = vtt; 7047 7048 dump_vtt (t, vtt); 7049 } 7050 7051 /* When building a secondary VTT, BINFO_VTABLE is set to a TREE_LIST with 7052 PURPOSE the RTTI_BINFO, VALUE the real vtable pointer for this binfo, 7053 and CHAIN the vtable pointer for this binfo after construction is 7054 complete. VALUE can also be another BINFO, in which case we recurse. */ 7055 7056 static tree 7057 binfo_ctor_vtable (tree binfo) 7058 { 7059 tree vt; 7060 7061 while (1) 7062 { 7063 vt = BINFO_VTABLE (binfo); 7064 if (TREE_CODE (vt) == TREE_LIST) 7065 vt = TREE_VALUE (vt); 7066 if (TREE_CODE (vt) == TREE_BINFO) 7067 binfo = vt; 7068 else 7069 break; 7070 } 7071 7072 return vt; 7073 } 7074 7075 /* Data for secondary VTT initialization. */ 7076 typedef struct secondary_vptr_vtt_init_data_s 7077 { 7078 /* Is this the primary VTT? */ 7079 bool top_level_p; 7080 7081 /* Current index into the VTT. */ 7082 tree index; 7083 7084 /* TREE_LIST of initializers built up. */ 7085 tree inits; 7086 7087 /* The type being constructed by this secondary VTT. */ 7088 tree type_being_constructed; 7089 } secondary_vptr_vtt_init_data; 7090 7091 /* Recursively build the VTT-initializer for BINFO (which is in the 7092 hierarchy dominated by T). INITS points to the end of the initializer 7093 list to date. INDEX is the VTT index where the next element will be 7094 replaced. Iff BINFO is the binfo for T, this is the top level VTT (i.e. 7095 not a subvtt for some base of T). When that is so, we emit the sub-VTTs 7096 for virtual bases of T. When it is not so, we build the constructor 7097 vtables for the BINFO-in-T variant. */ 7098 7099 static tree * 7100 build_vtt_inits (tree binfo, tree t, tree *inits, tree *index) 7101 { 7102 int i; 7103 tree b; 7104 tree init; 7105 tree secondary_vptrs; 7106 secondary_vptr_vtt_init_data data; 7107 int top_level_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t); 7108 7109 /* We only need VTTs for subobjects with virtual bases. */ 7110 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))) 7111 return inits; 7112 7113 /* We need to use a construction vtable if this is not the primary 7114 VTT. */ 7115 if (!top_level_p) 7116 { 7117 build_ctor_vtbl_group (binfo, t); 7118 7119 /* Record the offset in the VTT where this sub-VTT can be found. */ 7120 BINFO_SUBVTT_INDEX (binfo) = *index; 7121 } 7122 7123 /* Add the address of the primary vtable for the complete object. */ 7124 init = binfo_ctor_vtable (binfo); 7125 *inits = build_tree_list (NULL_TREE, init); 7126 inits = &TREE_CHAIN (*inits); 7127 if (top_level_p) 7128 { 7129 gcc_assert (!BINFO_VPTR_INDEX (binfo)); 7130 BINFO_VPTR_INDEX (binfo) = *index; 7131 } 7132 *index = size_binop (PLUS_EXPR, *index, TYPE_SIZE_UNIT (ptr_type_node)); 7133 7134 /* Recursively add the secondary VTTs for non-virtual bases. */ 7135 for (i = 0; BINFO_BASE_ITERATE (binfo, i, b); ++i) 7136 if (!BINFO_VIRTUAL_P (b)) 7137 inits = build_vtt_inits (b, t, inits, index); 7138 7139 /* Add secondary virtual pointers for all subobjects of BINFO with 7140 either virtual bases or reachable along a virtual path, except 7141 subobjects that are non-virtual primary bases. */ 7142 data.top_level_p = top_level_p; 7143 data.index = *index; 7144 data.inits = NULL; 7145 data.type_being_constructed = BINFO_TYPE (binfo); 7146 7147 dfs_walk_once (binfo, dfs_build_secondary_vptr_vtt_inits, NULL, &data); 7148 7149 *index = data.index; 7150 7151 /* The secondary vptrs come back in reverse order. After we reverse 7152 them, and add the INITS, the last init will be the first element 7153 of the chain. */ 7154 secondary_vptrs = data.inits; 7155 if (secondary_vptrs) 7156 { 7157 *inits = nreverse (secondary_vptrs); 7158 inits = &TREE_CHAIN (secondary_vptrs); 7159 gcc_assert (*inits == NULL_TREE); 7160 } 7161 7162 if (top_level_p) 7163 /* Add the secondary VTTs for virtual bases in inheritance graph 7164 order. */ 7165 for (b = TYPE_BINFO (BINFO_TYPE (binfo)); b; b = TREE_CHAIN (b)) 7166 { 7167 if (!BINFO_VIRTUAL_P (b)) 7168 continue; 7169 7170 inits = build_vtt_inits (b, t, inits, index); 7171 } 7172 else 7173 /* Remove the ctor vtables we created. */ 7174 dfs_walk_all (binfo, dfs_fixup_binfo_vtbls, NULL, binfo); 7175 7176 return inits; 7177 } 7178 7179 /* Called from build_vtt_inits via dfs_walk. BINFO is the binfo for the base 7180 in most derived. DATA is a SECONDARY_VPTR_VTT_INIT_DATA structure. */ 7181 7182 static tree 7183 dfs_build_secondary_vptr_vtt_inits (tree binfo, void *data_) 7184 { 7185 secondary_vptr_vtt_init_data *data = (secondary_vptr_vtt_init_data *)data_; 7186 7187 /* We don't care about bases that don't have vtables. */ 7188 if (!TYPE_VFIELD (BINFO_TYPE (binfo))) 7189 return dfs_skip_bases; 7190 7191 /* We're only interested in proper subobjects of the type being 7192 constructed. */ 7193 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), data->type_being_constructed)) 7194 return NULL_TREE; 7195 7196 /* We're only interested in bases with virtual bases or reachable 7197 via a virtual path from the type being constructed. */ 7198 if (!(CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)) 7199 || binfo_via_virtual (binfo, data->type_being_constructed))) 7200 return dfs_skip_bases; 7201 7202 /* We're not interested in non-virtual primary bases. */ 7203 if (!BINFO_VIRTUAL_P (binfo) && BINFO_PRIMARY_P (binfo)) 7204 return NULL_TREE; 7205 7206 /* Record the index where this secondary vptr can be found. */ 7207 if (data->top_level_p) 7208 { 7209 gcc_assert (!BINFO_VPTR_INDEX (binfo)); 7210 BINFO_VPTR_INDEX (binfo) = data->index; 7211 7212 if (BINFO_VIRTUAL_P (binfo)) 7213 { 7214 /* It's a primary virtual base, and this is not a 7215 construction vtable. Find the base this is primary of in 7216 the inheritance graph, and use that base's vtable 7217 now. */ 7218 while (BINFO_PRIMARY_P (binfo)) 7219 binfo = BINFO_INHERITANCE_CHAIN (binfo); 7220 } 7221 } 7222 7223 /* Add the initializer for the secondary vptr itself. */ 7224 data->inits = tree_cons (NULL_TREE, binfo_ctor_vtable (binfo), data->inits); 7225 7226 /* Advance the vtt index. */ 7227 data->index = size_binop (PLUS_EXPR, data->index, 7228 TYPE_SIZE_UNIT (ptr_type_node)); 7229 7230 return NULL_TREE; 7231 } 7232 7233 /* Called from build_vtt_inits via dfs_walk. After building 7234 constructor vtables and generating the sub-vtt from them, we need 7235 to restore the BINFO_VTABLES that were scribbled on. DATA is the 7236 binfo of the base whose sub vtt was generated. */ 7237 7238 static tree 7239 dfs_fixup_binfo_vtbls (tree binfo, void* data) 7240 { 7241 tree vtable = BINFO_VTABLE (binfo); 7242 7243 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo))) 7244 /* If this class has no vtable, none of its bases do. */ 7245 return dfs_skip_bases; 7246 7247 if (!vtable) 7248 /* This might be a primary base, so have no vtable in this 7249 hierarchy. */ 7250 return NULL_TREE; 7251 7252 /* If we scribbled the construction vtable vptr into BINFO, clear it 7253 out now. */ 7254 if (TREE_CODE (vtable) == TREE_LIST 7255 && (TREE_PURPOSE (vtable) == (tree) data)) 7256 BINFO_VTABLE (binfo) = TREE_CHAIN (vtable); 7257 7258 return NULL_TREE; 7259 } 7260 7261 /* Build the construction vtable group for BINFO which is in the 7262 hierarchy dominated by T. */ 7263 7264 static void 7265 build_ctor_vtbl_group (tree binfo, tree t) 7266 { 7267 tree list; 7268 tree type; 7269 tree vtbl; 7270 tree inits; 7271 tree id; 7272 tree vbase; 7273 7274 /* See if we've already created this construction vtable group. */ 7275 id = mangle_ctor_vtbl_for_type (t, binfo); 7276 if (IDENTIFIER_GLOBAL_VALUE (id)) 7277 return; 7278 7279 gcc_assert (!SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t)); 7280 /* Build a version of VTBL (with the wrong type) for use in 7281 constructing the addresses of secondary vtables in the 7282 construction vtable group. */ 7283 vtbl = build_vtable (t, id, ptr_type_node); 7284 DECL_CONSTRUCTION_VTABLE_P (vtbl) = 1; 7285 list = build_tree_list (vtbl, NULL_TREE); 7286 accumulate_vtbl_inits (binfo, TYPE_BINFO (TREE_TYPE (binfo)), 7287 binfo, t, list); 7288 7289 /* Add the vtables for each of our virtual bases using the vbase in T 7290 binfo. */ 7291 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo)); 7292 vbase; 7293 vbase = TREE_CHAIN (vbase)) 7294 { 7295 tree b; 7296 7297 if (!BINFO_VIRTUAL_P (vbase)) 7298 continue; 7299 b = copied_binfo (vbase, binfo); 7300 7301 accumulate_vtbl_inits (b, vbase, binfo, t, list); 7302 } 7303 inits = TREE_VALUE (list); 7304 7305 /* Figure out the type of the construction vtable. */ 7306 type = build_index_type (size_int (list_length (inits) - 1)); 7307 type = build_cplus_array_type (vtable_entry_type, type); 7308 layout_type (type); 7309 TREE_TYPE (vtbl) = type; 7310 DECL_SIZE (vtbl) = DECL_SIZE_UNIT (vtbl) = NULL_TREE; 7311 layout_decl (vtbl, 0); 7312 7313 /* Initialize the construction vtable. */ 7314 CLASSTYPE_VTABLES (t) = chainon (CLASSTYPE_VTABLES (t), vtbl); 7315 initialize_artificial_var (vtbl, inits); 7316 dump_vtable (t, binfo, vtbl); 7317 } 7318 7319 /* Add the vtbl initializers for BINFO (and its bases other than 7320 non-virtual primaries) to the list of INITS. BINFO is in the 7321 hierarchy dominated by T. RTTI_BINFO is the binfo within T of 7322 the constructor the vtbl inits should be accumulated for. (If this 7323 is the complete object vtbl then RTTI_BINFO will be TYPE_BINFO (T).) 7324 ORIG_BINFO is the binfo for this object within BINFO_TYPE (RTTI_BINFO). 7325 BINFO is the active base equivalent of ORIG_BINFO in the inheritance 7326 graph of T. Both BINFO and ORIG_BINFO will have the same BINFO_TYPE, 7327 but are not necessarily the same in terms of layout. */ 7328 7329 static void 7330 accumulate_vtbl_inits (tree binfo, 7331 tree orig_binfo, 7332 tree rtti_binfo, 7333 tree t, 7334 tree inits) 7335 { 7336 int i; 7337 tree base_binfo; 7338 int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t); 7339 7340 gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (orig_binfo))); 7341 7342 /* If it doesn't have a vptr, we don't do anything. */ 7343 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo))) 7344 return; 7345 7346 /* If we're building a construction vtable, we're not interested in 7347 subobjects that don't require construction vtables. */ 7348 if (ctor_vtbl_p 7349 && !CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)) 7350 && !binfo_via_virtual (orig_binfo, BINFO_TYPE (rtti_binfo))) 7351 return; 7352 7353 /* Build the initializers for the BINFO-in-T vtable. */ 7354 TREE_VALUE (inits) 7355 = chainon (TREE_VALUE (inits), 7356 dfs_accumulate_vtbl_inits (binfo, orig_binfo, 7357 rtti_binfo, t, inits)); 7358 7359 /* Walk the BINFO and its bases. We walk in preorder so that as we 7360 initialize each vtable we can figure out at what offset the 7361 secondary vtable lies from the primary vtable. We can't use 7362 dfs_walk here because we need to iterate through bases of BINFO 7363 and RTTI_BINFO simultaneously. */ 7364 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i) 7365 { 7366 /* Skip virtual bases. */ 7367 if (BINFO_VIRTUAL_P (base_binfo)) 7368 continue; 7369 accumulate_vtbl_inits (base_binfo, 7370 BINFO_BASE_BINFO (orig_binfo, i), 7371 rtti_binfo, t, 7372 inits); 7373 } 7374 } 7375 7376 /* Called from accumulate_vtbl_inits. Returns the initializers for 7377 the BINFO vtable. */ 7378 7379 static tree 7380 dfs_accumulate_vtbl_inits (tree binfo, 7381 tree orig_binfo, 7382 tree rtti_binfo, 7383 tree t, 7384 tree l) 7385 { 7386 tree inits = NULL_TREE; 7387 tree vtbl = NULL_TREE; 7388 int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t); 7389 7390 if (ctor_vtbl_p 7391 && BINFO_VIRTUAL_P (orig_binfo) && BINFO_PRIMARY_P (orig_binfo)) 7392 { 7393 /* In the hierarchy of BINFO_TYPE (RTTI_BINFO), this is a 7394 primary virtual base. If it is not the same primary in 7395 the hierarchy of T, we'll need to generate a ctor vtable 7396 for it, to place at its location in T. If it is the same 7397 primary, we still need a VTT entry for the vtable, but it 7398 should point to the ctor vtable for the base it is a 7399 primary for within the sub-hierarchy of RTTI_BINFO. 7400 7401 There are three possible cases: 7402 7403 1) We are in the same place. 7404 2) We are a primary base within a lost primary virtual base of 7405 RTTI_BINFO. 7406 3) We are primary to something not a base of RTTI_BINFO. */ 7407 7408 tree b; 7409 tree last = NULL_TREE; 7410 7411 /* First, look through the bases we are primary to for RTTI_BINFO 7412 or a virtual base. */ 7413 b = binfo; 7414 while (BINFO_PRIMARY_P (b)) 7415 { 7416 b = BINFO_INHERITANCE_CHAIN (b); 7417 last = b; 7418 if (BINFO_VIRTUAL_P (b) || b == rtti_binfo) 7419 goto found; 7420 } 7421 /* If we run out of primary links, keep looking down our 7422 inheritance chain; we might be an indirect primary. */ 7423 for (b = last; b; b = BINFO_INHERITANCE_CHAIN (b)) 7424 if (BINFO_VIRTUAL_P (b) || b == rtti_binfo) 7425 break; 7426 found: 7427 7428 /* If we found RTTI_BINFO, this is case 1. If we found a virtual 7429 base B and it is a base of RTTI_BINFO, this is case 2. In 7430 either case, we share our vtable with LAST, i.e. the 7431 derived-most base within B of which we are a primary. */ 7432 if (b == rtti_binfo 7433 || (b && binfo_for_vbase (BINFO_TYPE (b), BINFO_TYPE (rtti_binfo)))) 7434 /* Just set our BINFO_VTABLE to point to LAST, as we may not have 7435 set LAST's BINFO_VTABLE yet. We'll extract the actual vptr in 7436 binfo_ctor_vtable after everything's been set up. */ 7437 vtbl = last; 7438 7439 /* Otherwise, this is case 3 and we get our own. */ 7440 } 7441 else if (!BINFO_NEW_VTABLE_MARKED (orig_binfo)) 7442 return inits; 7443 7444 if (!vtbl) 7445 { 7446 tree index; 7447 int non_fn_entries; 7448 7449 /* Compute the initializer for this vtable. */ 7450 inits = build_vtbl_initializer (binfo, orig_binfo, t, rtti_binfo, 7451 &non_fn_entries); 7452 7453 /* Figure out the position to which the VPTR should point. */ 7454 vtbl = TREE_PURPOSE (l); 7455 vtbl = build1 (ADDR_EXPR, vtbl_ptr_type_node, vtbl); 7456 index = size_binop (PLUS_EXPR, 7457 size_int (non_fn_entries), 7458 size_int (list_length (TREE_VALUE (l)))); 7459 index = size_binop (MULT_EXPR, 7460 TYPE_SIZE_UNIT (vtable_entry_type), 7461 index); 7462 vtbl = build2 (POINTER_PLUS_EXPR, TREE_TYPE (vtbl), vtbl, index); 7463 } 7464 7465 if (ctor_vtbl_p) 7466 /* For a construction vtable, we can't overwrite BINFO_VTABLE. 7467 So, we make a TREE_LIST. Later, dfs_fixup_binfo_vtbls will 7468 straighten this out. */ 7469 BINFO_VTABLE (binfo) = tree_cons (rtti_binfo, vtbl, BINFO_VTABLE (binfo)); 7470 else if (BINFO_PRIMARY_P (binfo) && BINFO_VIRTUAL_P (binfo)) 7471 inits = NULL_TREE; 7472 else 7473 /* For an ordinary vtable, set BINFO_VTABLE. */ 7474 BINFO_VTABLE (binfo) = vtbl; 7475 7476 return inits; 7477 } 7478 7479 static GTY(()) tree abort_fndecl_addr; 7480 7481 /* Construct the initializer for BINFO's virtual function table. BINFO 7482 is part of the hierarchy dominated by T. If we're building a 7483 construction vtable, the ORIG_BINFO is the binfo we should use to 7484 find the actual function pointers to put in the vtable - but they 7485 can be overridden on the path to most-derived in the graph that 7486 ORIG_BINFO belongs. Otherwise, 7487 ORIG_BINFO should be the same as BINFO. The RTTI_BINFO is the 7488 BINFO that should be indicated by the RTTI information in the 7489 vtable; it will be a base class of T, rather than T itself, if we 7490 are building a construction vtable. 7491 7492 The value returned is a TREE_LIST suitable for wrapping in a 7493 CONSTRUCTOR to use as the DECL_INITIAL for a vtable. If 7494 NON_FN_ENTRIES_P is not NULL, *NON_FN_ENTRIES_P is set to the 7495 number of non-function entries in the vtable. 7496 7497 It might seem that this function should never be called with a 7498 BINFO for which BINFO_PRIMARY_P holds, the vtable for such a 7499 base is always subsumed by a derived class vtable. However, when 7500 we are building construction vtables, we do build vtables for 7501 primary bases; we need these while the primary base is being 7502 constructed. */ 7503 7504 static tree 7505 build_vtbl_initializer (tree binfo, 7506 tree orig_binfo, 7507 tree t, 7508 tree rtti_binfo, 7509 int* non_fn_entries_p) 7510 { 7511 tree v, b; 7512 tree vfun_inits; 7513 vtbl_init_data vid; 7514 unsigned ix; 7515 tree vbinfo; 7516 VEC(tree,gc) *vbases; 7517 7518 /* Initialize VID. */ 7519 memset (&vid, 0, sizeof (vid)); 7520 vid.binfo = binfo; 7521 vid.derived = t; 7522 vid.rtti_binfo = rtti_binfo; 7523 vid.last_init = &vid.inits; 7524 vid.primary_vtbl_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t); 7525 vid.ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t); 7526 vid.generate_vcall_entries = true; 7527 /* The first vbase or vcall offset is at index -3 in the vtable. */ 7528 vid.index = ssize_int(-3 * TARGET_VTABLE_DATA_ENTRY_DISTANCE); 7529 7530 /* Add entries to the vtable for RTTI. */ 7531 build_rtti_vtbl_entries (binfo, &vid); 7532 7533 /* Create an array for keeping track of the functions we've 7534 processed. When we see multiple functions with the same 7535 signature, we share the vcall offsets. */ 7536 vid.fns = VEC_alloc (tree, gc, 32); 7537 /* Add the vcall and vbase offset entries. */ 7538 build_vcall_and_vbase_vtbl_entries (binfo, &vid); 7539 7540 /* Clear BINFO_VTABLE_PATH_MARKED; it's set by 7541 build_vbase_offset_vtbl_entries. */ 7542 for (vbases = CLASSTYPE_VBASECLASSES (t), ix = 0; 7543 VEC_iterate (tree, vbases, ix, vbinfo); ix++) 7544 BINFO_VTABLE_PATH_MARKED (vbinfo) = 0; 7545 7546 /* If the target requires padding between data entries, add that now. */ 7547 if (TARGET_VTABLE_DATA_ENTRY_DISTANCE > 1) 7548 { 7549 tree cur, *prev; 7550 7551 for (prev = &vid.inits; (cur = *prev); prev = &TREE_CHAIN (cur)) 7552 { 7553 tree add = cur; 7554 int i; 7555 7556 for (i = 1; i < TARGET_VTABLE_DATA_ENTRY_DISTANCE; ++i) 7557 add = tree_cons (NULL_TREE, 7558 build1 (NOP_EXPR, vtable_entry_type, 7559 null_pointer_node), 7560 add); 7561 *prev = add; 7562 } 7563 } 7564 7565 if (non_fn_entries_p) 7566 *non_fn_entries_p = list_length (vid.inits); 7567 7568 /* Go through all the ordinary virtual functions, building up 7569 initializers. */ 7570 vfun_inits = NULL_TREE; 7571 for (v = BINFO_VIRTUALS (orig_binfo); v; v = TREE_CHAIN (v)) 7572 { 7573 tree delta; 7574 tree vcall_index; 7575 tree fn, fn_original; 7576 tree init = NULL_TREE; 7577 7578 fn = BV_FN (v); 7579 fn_original = fn; 7580 if (DECL_THUNK_P (fn)) 7581 { 7582 if (!DECL_NAME (fn)) 7583 finish_thunk (fn); 7584 if (THUNK_ALIAS (fn)) 7585 { 7586 fn = THUNK_ALIAS (fn); 7587 BV_FN (v) = fn; 7588 } 7589 fn_original = THUNK_TARGET (fn); 7590 } 7591 7592 /* If the only definition of this function signature along our 7593 primary base chain is from a lost primary, this vtable slot will 7594 never be used, so just zero it out. This is important to avoid 7595 requiring extra thunks which cannot be generated with the function. 7596 7597 We first check this in update_vtable_entry_for_fn, so we handle 7598 restored primary bases properly; we also need to do it here so we 7599 zero out unused slots in ctor vtables, rather than filling them 7600 with erroneous values (though harmless, apart from relocation 7601 costs). */ 7602 for (b = binfo; ; b = get_primary_binfo (b)) 7603 { 7604 /* We found a defn before a lost primary; go ahead as normal. */ 7605 if (look_for_overrides_here (BINFO_TYPE (b), fn_original)) 7606 break; 7607 7608 /* The nearest definition is from a lost primary; clear the 7609 slot. */ 7610 if (BINFO_LOST_PRIMARY_P (b)) 7611 { 7612 init = size_zero_node; 7613 break; 7614 } 7615 } 7616 7617 if (! init) 7618 { 7619 /* Pull the offset for `this', and the function to call, out of 7620 the list. */ 7621 delta = BV_DELTA (v); 7622 vcall_index = BV_VCALL_INDEX (v); 7623 7624 gcc_assert (TREE_CODE (delta) == INTEGER_CST); 7625 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL); 7626 7627 /* You can't call an abstract virtual function; it's abstract. 7628 So, we replace these functions with __pure_virtual. */ 7629 if (DECL_PURE_VIRTUAL_P (fn_original)) 7630 { 7631 fn = abort_fndecl; 7632 if (abort_fndecl_addr == NULL) 7633 abort_fndecl_addr = build1 (ADDR_EXPR, vfunc_ptr_type_node, fn); 7634 init = abort_fndecl_addr; 7635 } 7636 else 7637 { 7638 if (!integer_zerop (delta) || vcall_index) 7639 { 7640 fn = make_thunk (fn, /*this_adjusting=*/1, delta, vcall_index); 7641 if (!DECL_NAME (fn)) 7642 finish_thunk (fn); 7643 } 7644 /* Take the address of the function, considering it to be of an 7645 appropriate generic type. */ 7646 init = build1 (ADDR_EXPR, vfunc_ptr_type_node, fn); 7647 } 7648 } 7649 7650 /* And add it to the chain of initializers. */ 7651 if (TARGET_VTABLE_USES_DESCRIPTORS) 7652 { 7653 int i; 7654 if (init == size_zero_node) 7655 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i) 7656 vfun_inits = tree_cons (NULL_TREE, init, vfun_inits); 7657 else 7658 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i) 7659 { 7660 tree fdesc = build2 (FDESC_EXPR, vfunc_ptr_type_node, 7661 TREE_OPERAND (init, 0), 7662 build_int_cst (NULL_TREE, i)); 7663 TREE_CONSTANT (fdesc) = 1; 7664 7665 vfun_inits = tree_cons (NULL_TREE, fdesc, vfun_inits); 7666 } 7667 } 7668 else 7669 vfun_inits = tree_cons (NULL_TREE, init, vfun_inits); 7670 } 7671 7672 /* The initializers for virtual functions were built up in reverse 7673 order; straighten them out now. */ 7674 vfun_inits = nreverse (vfun_inits); 7675 7676 /* The negative offset initializers are also in reverse order. */ 7677 vid.inits = nreverse (vid.inits); 7678 7679 /* Chain the two together. */ 7680 return chainon (vid.inits, vfun_inits); 7681 } 7682 7683 /* Adds to vid->inits the initializers for the vbase and vcall 7684 offsets in BINFO, which is in the hierarchy dominated by T. */ 7685 7686 static void 7687 build_vcall_and_vbase_vtbl_entries (tree binfo, vtbl_init_data* vid) 7688 { 7689 tree b; 7690 7691 /* If this is a derived class, we must first create entries 7692 corresponding to the primary base class. */ 7693 b = get_primary_binfo (binfo); 7694 if (b) 7695 build_vcall_and_vbase_vtbl_entries (b, vid); 7696 7697 /* Add the vbase entries for this base. */ 7698 build_vbase_offset_vtbl_entries (binfo, vid); 7699 /* Add the vcall entries for this base. */ 7700 build_vcall_offset_vtbl_entries (binfo, vid); 7701 } 7702 7703 /* Returns the initializers for the vbase offset entries in the vtable 7704 for BINFO (which is part of the class hierarchy dominated by T), in 7705 reverse order. VBASE_OFFSET_INDEX gives the vtable index 7706 where the next vbase offset will go. */ 7707 7708 static void 7709 build_vbase_offset_vtbl_entries (tree binfo, vtbl_init_data* vid) 7710 { 7711 tree vbase; 7712 tree t; 7713 tree non_primary_binfo; 7714 7715 /* If there are no virtual baseclasses, then there is nothing to 7716 do. */ 7717 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))) 7718 return; 7719 7720 t = vid->derived; 7721 7722 /* We might be a primary base class. Go up the inheritance hierarchy 7723 until we find the most derived class of which we are a primary base: 7724 it is the offset of that which we need to use. */ 7725 non_primary_binfo = binfo; 7726 while (BINFO_INHERITANCE_CHAIN (non_primary_binfo)) 7727 { 7728 tree b; 7729 7730 /* If we have reached a virtual base, then it must be a primary 7731 base (possibly multi-level) of vid->binfo, or we wouldn't 7732 have called build_vcall_and_vbase_vtbl_entries for it. But it 7733 might be a lost primary, so just skip down to vid->binfo. */ 7734 if (BINFO_VIRTUAL_P (non_primary_binfo)) 7735 { 7736 non_primary_binfo = vid->binfo; 7737 break; 7738 } 7739 7740 b = BINFO_INHERITANCE_CHAIN (non_primary_binfo); 7741 if (get_primary_binfo (b) != non_primary_binfo) 7742 break; 7743 non_primary_binfo = b; 7744 } 7745 7746 /* Go through the virtual bases, adding the offsets. */ 7747 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo)); 7748 vbase; 7749 vbase = TREE_CHAIN (vbase)) 7750 { 7751 tree b; 7752 tree delta; 7753 7754 if (!BINFO_VIRTUAL_P (vbase)) 7755 continue; 7756 7757 /* Find the instance of this virtual base in the complete 7758 object. */ 7759 b = copied_binfo (vbase, binfo); 7760 7761 /* If we've already got an offset for this virtual base, we 7762 don't need another one. */ 7763 if (BINFO_VTABLE_PATH_MARKED (b)) 7764 continue; 7765 BINFO_VTABLE_PATH_MARKED (b) = 1; 7766 7767 /* Figure out where we can find this vbase offset. */ 7768 delta = size_binop (MULT_EXPR, 7769 vid->index, 7770 convert (ssizetype, 7771 TYPE_SIZE_UNIT (vtable_entry_type))); 7772 if (vid->primary_vtbl_p) 7773 BINFO_VPTR_FIELD (b) = delta; 7774 7775 if (binfo != TYPE_BINFO (t)) 7776 /* The vbase offset had better be the same. */ 7777 gcc_assert (tree_int_cst_equal (delta, BINFO_VPTR_FIELD (vbase))); 7778 7779 /* The next vbase will come at a more negative offset. */ 7780 vid->index = size_binop (MINUS_EXPR, vid->index, 7781 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE)); 7782 7783 /* The initializer is the delta from BINFO to this virtual base. 7784 The vbase offsets go in reverse inheritance-graph order, and 7785 we are walking in inheritance graph order so these end up in 7786 the right order. */ 7787 delta = size_diffop_loc (input_location, 7788 BINFO_OFFSET (b), BINFO_OFFSET (non_primary_binfo)); 7789 7790 *vid->last_init 7791 = build_tree_list (NULL_TREE, 7792 fold_build1_loc (input_location, NOP_EXPR, 7793 vtable_entry_type, 7794 delta)); 7795 vid->last_init = &TREE_CHAIN (*vid->last_init); 7796 } 7797 } 7798 7799 /* Adds the initializers for the vcall offset entries in the vtable 7800 for BINFO (which is part of the class hierarchy dominated by VID->DERIVED) 7801 to VID->INITS. */ 7802 7803 static void 7804 build_vcall_offset_vtbl_entries (tree binfo, vtbl_init_data* vid) 7805 { 7806 /* We only need these entries if this base is a virtual base. We 7807 compute the indices -- but do not add to the vtable -- when 7808 building the main vtable for a class. */ 7809 if (binfo == TYPE_BINFO (vid->derived) 7810 || (BINFO_VIRTUAL_P (binfo) 7811 /* If BINFO is RTTI_BINFO, then (since BINFO does not 7812 correspond to VID->DERIVED), we are building a primary 7813 construction virtual table. Since this is a primary 7814 virtual table, we do not need the vcall offsets for 7815 BINFO. */ 7816 && binfo != vid->rtti_binfo)) 7817 { 7818 /* We need a vcall offset for each of the virtual functions in this 7819 vtable. For example: 7820 7821 class A { virtual void f (); }; 7822 class B1 : virtual public A { virtual void f (); }; 7823 class B2 : virtual public A { virtual void f (); }; 7824 class C: public B1, public B2 { virtual void f (); }; 7825 7826 A C object has a primary base of B1, which has a primary base of A. A 7827 C also has a secondary base of B2, which no longer has a primary base 7828 of A. So the B2-in-C construction vtable needs a secondary vtable for 7829 A, which will adjust the A* to a B2* to call f. We have no way of 7830 knowing what (or even whether) this offset will be when we define B2, 7831 so we store this "vcall offset" in the A sub-vtable and look it up in 7832 a "virtual thunk" for B2::f. 7833 7834 We need entries for all the functions in our primary vtable and 7835 in our non-virtual bases' secondary vtables. */ 7836 vid->vbase = binfo; 7837 /* If we are just computing the vcall indices -- but do not need 7838 the actual entries -- not that. */ 7839 if (!BINFO_VIRTUAL_P (binfo)) 7840 vid->generate_vcall_entries = false; 7841 /* Now, walk through the non-virtual bases, adding vcall offsets. */ 7842 add_vcall_offset_vtbl_entries_r (binfo, vid); 7843 } 7844 } 7845 7846 /* Build vcall offsets, starting with those for BINFO. */ 7847 7848 static void 7849 add_vcall_offset_vtbl_entries_r (tree binfo, vtbl_init_data* vid) 7850 { 7851 int i; 7852 tree primary_binfo; 7853 tree base_binfo; 7854 7855 /* Don't walk into virtual bases -- except, of course, for the 7856 virtual base for which we are building vcall offsets. Any 7857 primary virtual base will have already had its offsets generated 7858 through the recursion in build_vcall_and_vbase_vtbl_entries. */ 7859 if (BINFO_VIRTUAL_P (binfo) && vid->vbase != binfo) 7860 return; 7861 7862 /* If BINFO has a primary base, process it first. */ 7863 primary_binfo = get_primary_binfo (binfo); 7864 if (primary_binfo) 7865 add_vcall_offset_vtbl_entries_r (primary_binfo, vid); 7866 7867 /* Add BINFO itself to the list. */ 7868 add_vcall_offset_vtbl_entries_1 (binfo, vid); 7869 7870 /* Scan the non-primary bases of BINFO. */ 7871 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i) 7872 if (base_binfo != primary_binfo) 7873 add_vcall_offset_vtbl_entries_r (base_binfo, vid); 7874 } 7875 7876 /* Called from build_vcall_offset_vtbl_entries_r. */ 7877 7878 static void 7879 add_vcall_offset_vtbl_entries_1 (tree binfo, vtbl_init_data* vid) 7880 { 7881 /* Make entries for the rest of the virtuals. */ 7882 if (abi_version_at_least (2)) 7883 { 7884 tree orig_fn; 7885 7886 /* The ABI requires that the methods be processed in declaration 7887 order. G++ 3.2 used the order in the vtable. */ 7888 for (orig_fn = TYPE_METHODS (BINFO_TYPE (binfo)); 7889 orig_fn; 7890 orig_fn = TREE_CHAIN (orig_fn)) 7891 if (DECL_VINDEX (orig_fn)) 7892 add_vcall_offset (orig_fn, binfo, vid); 7893 } 7894 else 7895 { 7896 tree derived_virtuals; 7897 tree base_virtuals; 7898 tree orig_virtuals; 7899 /* If BINFO is a primary base, the most derived class which has 7900 BINFO as a primary base; otherwise, just BINFO. */ 7901 tree non_primary_binfo; 7902 7903 /* We might be a primary base class. Go up the inheritance hierarchy 7904 until we find the most derived class of which we are a primary base: 7905 it is the BINFO_VIRTUALS there that we need to consider. */ 7906 non_primary_binfo = binfo; 7907 while (BINFO_INHERITANCE_CHAIN (non_primary_binfo)) 7908 { 7909 tree b; 7910 7911 /* If we have reached a virtual base, then it must be vid->vbase, 7912 because we ignore other virtual bases in 7913 add_vcall_offset_vtbl_entries_r. In turn, it must be a primary 7914 base (possibly multi-level) of vid->binfo, or we wouldn't 7915 have called build_vcall_and_vbase_vtbl_entries for it. But it 7916 might be a lost primary, so just skip down to vid->binfo. */ 7917 if (BINFO_VIRTUAL_P (non_primary_binfo)) 7918 { 7919 gcc_assert (non_primary_binfo == vid->vbase); 7920 non_primary_binfo = vid->binfo; 7921 break; 7922 } 7923 7924 b = BINFO_INHERITANCE_CHAIN (non_primary_binfo); 7925 if (get_primary_binfo (b) != non_primary_binfo) 7926 break; 7927 non_primary_binfo = b; 7928 } 7929 7930 if (vid->ctor_vtbl_p) 7931 /* For a ctor vtable we need the equivalent binfo within the hierarchy 7932 where rtti_binfo is the most derived type. */ 7933 non_primary_binfo 7934 = original_binfo (non_primary_binfo, vid->rtti_binfo); 7935 7936 for (base_virtuals = BINFO_VIRTUALS (binfo), 7937 derived_virtuals = BINFO_VIRTUALS (non_primary_binfo), 7938 orig_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo))); 7939 base_virtuals; 7940 base_virtuals = TREE_CHAIN (base_virtuals), 7941 derived_virtuals = TREE_CHAIN (derived_virtuals), 7942 orig_virtuals = TREE_CHAIN (orig_virtuals)) 7943 { 7944 tree orig_fn; 7945 7946 /* Find the declaration that originally caused this function to 7947 be present in BINFO_TYPE (binfo). */ 7948 orig_fn = BV_FN (orig_virtuals); 7949 7950 /* When processing BINFO, we only want to generate vcall slots for 7951 function slots introduced in BINFO. So don't try to generate 7952 one if the function isn't even defined in BINFO. */ 7953 if (!SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), DECL_CONTEXT (orig_fn))) 7954 continue; 7955 7956 add_vcall_offset (orig_fn, binfo, vid); 7957 } 7958 } 7959 } 7960 7961 /* Add a vcall offset entry for ORIG_FN to the vtable. */ 7962 7963 static void 7964 add_vcall_offset (tree orig_fn, tree binfo, vtbl_init_data *vid) 7965 { 7966 size_t i; 7967 tree vcall_offset; 7968 tree derived_entry; 7969 7970 /* If there is already an entry for a function with the same 7971 signature as FN, then we do not need a second vcall offset. 7972 Check the list of functions already present in the derived 7973 class vtable. */ 7974 for (i = 0; VEC_iterate (tree, vid->fns, i, derived_entry); ++i) 7975 { 7976 if (same_signature_p (derived_entry, orig_fn) 7977 /* We only use one vcall offset for virtual destructors, 7978 even though there are two virtual table entries. */ 7979 || (DECL_DESTRUCTOR_P (derived_entry) 7980 && DECL_DESTRUCTOR_P (orig_fn))) 7981 return; 7982 } 7983 7984 /* If we are building these vcall offsets as part of building 7985 the vtable for the most derived class, remember the vcall 7986 offset. */ 7987 if (vid->binfo == TYPE_BINFO (vid->derived)) 7988 { 7989 tree_pair_p elt = VEC_safe_push (tree_pair_s, gc, 7990 CLASSTYPE_VCALL_INDICES (vid->derived), 7991 NULL); 7992 elt->purpose = orig_fn; 7993 elt->value = vid->index; 7994 } 7995 7996 /* The next vcall offset will be found at a more negative 7997 offset. */ 7998 vid->index = size_binop (MINUS_EXPR, vid->index, 7999 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE)); 8000 8001 /* Keep track of this function. */ 8002 VEC_safe_push (tree, gc, vid->fns, orig_fn); 8003 8004 if (vid->generate_vcall_entries) 8005 { 8006 tree base; 8007 tree fn; 8008 8009 /* Find the overriding function. */ 8010 fn = find_final_overrider (vid->rtti_binfo, binfo, orig_fn); 8011 if (fn == error_mark_node) 8012 vcall_offset = build1 (NOP_EXPR, vtable_entry_type, 8013 integer_zero_node); 8014 else 8015 { 8016 base = TREE_VALUE (fn); 8017 8018 /* The vbase we're working on is a primary base of 8019 vid->binfo. But it might be a lost primary, so its 8020 BINFO_OFFSET might be wrong, so we just use the 8021 BINFO_OFFSET from vid->binfo. */ 8022 vcall_offset = size_diffop_loc (input_location, 8023 BINFO_OFFSET (base), 8024 BINFO_OFFSET (vid->binfo)); 8025 vcall_offset = fold_build1_loc (input_location, 8026 NOP_EXPR, vtable_entry_type, 8027 vcall_offset); 8028 } 8029 /* Add the initializer to the vtable. */ 8030 *vid->last_init = build_tree_list (NULL_TREE, vcall_offset); 8031 vid->last_init = &TREE_CHAIN (*vid->last_init); 8032 } 8033 } 8034 8035 /* Return vtbl initializers for the RTTI entries corresponding to the 8036 BINFO's vtable. The RTTI entries should indicate the object given 8037 by VID->rtti_binfo. */ 8038 8039 static void 8040 build_rtti_vtbl_entries (tree binfo, vtbl_init_data* vid) 8041 { 8042 tree b; 8043 tree t; 8044 tree offset; 8045 tree decl; 8046 tree init; 8047 8048 t = BINFO_TYPE (vid->rtti_binfo); 8049 8050 /* To find the complete object, we will first convert to our most 8051 primary base, and then add the offset in the vtbl to that value. */ 8052 b = binfo; 8053 while (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (b)) 8054 && !BINFO_LOST_PRIMARY_P (b)) 8055 { 8056 tree primary_base; 8057 8058 primary_base = get_primary_binfo (b); 8059 gcc_assert (BINFO_PRIMARY_P (primary_base) 8060 && BINFO_INHERITANCE_CHAIN (primary_base) == b); 8061 b = primary_base; 8062 } 8063 offset = size_diffop_loc (input_location, 8064 BINFO_OFFSET (vid->rtti_binfo), BINFO_OFFSET (b)); 8065 8066 /* The second entry is the address of the typeinfo object. */ 8067 if (flag_rtti) 8068 decl = build_address (get_tinfo_decl (t)); 8069 else 8070 decl = integer_zero_node; 8071 8072 /* Convert the declaration to a type that can be stored in the 8073 vtable. */ 8074 init = build_nop (vfunc_ptr_type_node, decl); 8075 *vid->last_init = build_tree_list (NULL_TREE, init); 8076 vid->last_init = &TREE_CHAIN (*vid->last_init); 8077 8078 /* Add the offset-to-top entry. It comes earlier in the vtable than 8079 the typeinfo entry. Convert the offset to look like a 8080 function pointer, so that we can put it in the vtable. */ 8081 init = build_nop (vfunc_ptr_type_node, offset); 8082 *vid->last_init = build_tree_list (NULL_TREE, init); 8083 vid->last_init = &TREE_CHAIN (*vid->last_init); 8084 } 8085 8086 /* Fold a OBJ_TYPE_REF expression to the address of a function. 8087 KNOWN_TYPE carries the true type of OBJ_TYPE_REF_OBJECT(REF). */ 8088 8089 tree 8090 cp_fold_obj_type_ref (tree ref, tree known_type) 8091 { 8092 HOST_WIDE_INT index = tree_low_cst (OBJ_TYPE_REF_TOKEN (ref), 1); 8093 HOST_WIDE_INT i = 0; 8094 tree v = BINFO_VIRTUALS (TYPE_BINFO (known_type)); 8095 tree fndecl; 8096 8097 while (i != index) 8098 { 8099 i += (TARGET_VTABLE_USES_DESCRIPTORS 8100 ? TARGET_VTABLE_USES_DESCRIPTORS : 1); 8101 v = TREE_CHAIN (v); 8102 } 8103 8104 fndecl = BV_FN (v); 8105 8106 #ifdef ENABLE_CHECKING 8107 gcc_assert (tree_int_cst_equal (OBJ_TYPE_REF_TOKEN (ref), 8108 DECL_VINDEX (fndecl))); 8109 #endif 8110 8111 cgraph_node (fndecl)->local.vtable_method = true; 8112 8113 return build_address (fndecl); 8114 } 8115 8116 #include "gt-cp-class.h" 8117