1 /* Functions related to building classes and their related objects. 2 Copyright (C) 1987-2016 Free Software Foundation, Inc. 3 Contributed by Michael Tiemann (tiemann@cygnus.com) 4 5 This file is part of GCC. 6 7 GCC is free software; you can redistribute it and/or modify 8 it under the terms of the GNU General Public License as published by 9 the Free Software Foundation; either version 3, or (at your option) 10 any later version. 11 12 GCC is distributed in the hope that it will be useful, 13 but WITHOUT ANY WARRANTY; without even the implied warranty of 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 GNU General Public License for more details. 16 17 You should have received a copy of the GNU General Public License 18 along with GCC; see the file COPYING3. If not see 19 <http://www.gnu.org/licenses/>. */ 20 21 22 /* High-level class interface. */ 23 24 #include "config.h" 25 #include "system.h" 26 #include "coretypes.h" 27 #include "target.h" 28 #include "cp-tree.h" 29 #include "stringpool.h" 30 #include "cgraph.h" 31 #include "stor-layout.h" 32 #include "attribs.h" 33 #include "flags.h" 34 #include "toplev.h" 35 #include "convert.h" 36 #include "dumpfile.h" 37 #include "gimplify.h" 38 #include "intl.h" 39 40 /* The number of nested classes being processed. If we are not in the 41 scope of any class, this is zero. */ 42 43 int current_class_depth; 44 45 /* In order to deal with nested classes, we keep a stack of classes. 46 The topmost entry is the innermost class, and is the entry at index 47 CURRENT_CLASS_DEPTH */ 48 49 typedef struct class_stack_node { 50 /* The name of the class. */ 51 tree name; 52 53 /* The _TYPE node for the class. */ 54 tree type; 55 56 /* The access specifier pending for new declarations in the scope of 57 this class. */ 58 tree access; 59 60 /* If were defining TYPE, the names used in this class. */ 61 splay_tree names_used; 62 63 /* Nonzero if this class is no longer open, because of a call to 64 push_to_top_level. */ 65 size_t hidden; 66 }* class_stack_node_t; 67 68 struct vtbl_init_data 69 { 70 /* The base for which we're building initializers. */ 71 tree binfo; 72 /* The type of the most-derived type. */ 73 tree derived; 74 /* The binfo for the dynamic type. This will be TYPE_BINFO (derived), 75 unless ctor_vtbl_p is true. */ 76 tree rtti_binfo; 77 /* The negative-index vtable initializers built up so far. These 78 are in order from least negative index to most negative index. */ 79 vec<constructor_elt, va_gc> *inits; 80 /* The binfo for the virtual base for which we're building 81 vcall offset initializers. */ 82 tree vbase; 83 /* The functions in vbase for which we have already provided vcall 84 offsets. */ 85 vec<tree, va_gc> *fns; 86 /* The vtable index of the next vcall or vbase offset. */ 87 tree index; 88 /* Nonzero if we are building the initializer for the primary 89 vtable. */ 90 int primary_vtbl_p; 91 /* Nonzero if we are building the initializer for a construction 92 vtable. */ 93 int ctor_vtbl_p; 94 /* True when adding vcall offset entries to the vtable. False when 95 merely computing the indices. */ 96 bool generate_vcall_entries; 97 }; 98 99 /* The type of a function passed to walk_subobject_offsets. */ 100 typedef int (*subobject_offset_fn) (tree, tree, splay_tree); 101 102 /* The stack itself. This is a dynamically resized array. The 103 number of elements allocated is CURRENT_CLASS_STACK_SIZE. */ 104 static int current_class_stack_size; 105 static class_stack_node_t current_class_stack; 106 107 /* The size of the largest empty class seen in this translation unit. */ 108 static GTY (()) tree sizeof_biggest_empty_class; 109 110 /* An array of all local classes present in this translation unit, in 111 declaration order. */ 112 vec<tree, va_gc> *local_classes; 113 114 static tree get_vfield_name (tree); 115 static void finish_struct_anon (tree); 116 static tree get_vtable_name (tree); 117 static void get_basefndecls (tree, tree, vec<tree> *); 118 static int build_primary_vtable (tree, tree); 119 static int build_secondary_vtable (tree); 120 static void finish_vtbls (tree); 121 static void modify_vtable_entry (tree, tree, tree, tree, tree *); 122 static void finish_struct_bits (tree); 123 static int alter_access (tree, tree, tree); 124 static void handle_using_decl (tree, tree); 125 static tree dfs_modify_vtables (tree, void *); 126 static tree modify_all_vtables (tree, tree); 127 static void determine_primary_bases (tree); 128 static void finish_struct_methods (tree); 129 static void maybe_warn_about_overly_private_class (tree); 130 static int method_name_cmp (const void *, const void *); 131 static int resort_method_name_cmp (const void *, const void *); 132 static void add_implicitly_declared_members (tree, tree*, int, int); 133 static tree fixed_type_or_null (tree, int *, int *); 134 static tree build_simple_base_path (tree expr, tree binfo); 135 static tree build_vtbl_ref_1 (tree, tree); 136 static void build_vtbl_initializer (tree, tree, tree, tree, int *, 137 vec<constructor_elt, va_gc> **); 138 static int count_fields (tree); 139 static int add_fields_to_record_type (tree, struct sorted_fields_type*, int); 140 static void insert_into_classtype_sorted_fields (tree, tree, int); 141 static bool check_bitfield_decl (tree); 142 static void check_field_decl (tree, tree, int *, int *, int *); 143 static void check_field_decls (tree, tree *, int *, int *); 144 static tree *build_base_field (record_layout_info, tree, splay_tree, tree *); 145 static void build_base_fields (record_layout_info, splay_tree, tree *); 146 static void check_methods (tree); 147 static void remove_zero_width_bit_fields (tree); 148 static bool accessible_nvdtor_p (tree); 149 150 /* Used by find_flexarrays and related functions. */ 151 struct flexmems_t; 152 static void diagnose_flexarrays (tree, const flexmems_t *); 153 static void find_flexarrays (tree, flexmems_t *, bool = false, 154 tree = NULL_TREE, tree = NULL_TREE); 155 static void check_flexarrays (tree, flexmems_t * = NULL, bool = false); 156 static void check_bases (tree, int *, int *); 157 static void check_bases_and_members (tree); 158 static tree create_vtable_ptr (tree, tree *); 159 static void include_empty_classes (record_layout_info); 160 static void layout_class_type (tree, tree *); 161 static void propagate_binfo_offsets (tree, tree); 162 static void layout_virtual_bases (record_layout_info, splay_tree); 163 static void build_vbase_offset_vtbl_entries (tree, vtbl_init_data *); 164 static void add_vcall_offset_vtbl_entries_r (tree, vtbl_init_data *); 165 static void add_vcall_offset_vtbl_entries_1 (tree, vtbl_init_data *); 166 static void build_vcall_offset_vtbl_entries (tree, vtbl_init_data *); 167 static void add_vcall_offset (tree, tree, vtbl_init_data *); 168 static void layout_vtable_decl (tree, int); 169 static tree dfs_find_final_overrider_pre (tree, void *); 170 static tree dfs_find_final_overrider_post (tree, void *); 171 static tree find_final_overrider (tree, tree, tree); 172 static int make_new_vtable (tree, tree); 173 static tree get_primary_binfo (tree); 174 static int maybe_indent_hierarchy (FILE *, int, int); 175 static tree dump_class_hierarchy_r (FILE *, int, tree, tree, int); 176 static void dump_class_hierarchy (tree); 177 static void dump_class_hierarchy_1 (FILE *, int, tree); 178 static void dump_array (FILE *, tree); 179 static void dump_vtable (tree, tree, tree); 180 static void dump_vtt (tree, tree); 181 static void dump_thunk (FILE *, int, tree); 182 static tree build_vtable (tree, tree, tree); 183 static void initialize_vtable (tree, vec<constructor_elt, va_gc> *); 184 static void layout_nonempty_base_or_field (record_layout_info, 185 tree, tree, splay_tree); 186 static tree end_of_class (tree, int); 187 static bool layout_empty_base (record_layout_info, tree, tree, splay_tree); 188 static void accumulate_vtbl_inits (tree, tree, tree, tree, tree, 189 vec<constructor_elt, va_gc> **); 190 static void dfs_accumulate_vtbl_inits (tree, tree, tree, tree, tree, 191 vec<constructor_elt, va_gc> **); 192 static void build_rtti_vtbl_entries (tree, vtbl_init_data *); 193 static void build_vcall_and_vbase_vtbl_entries (tree, vtbl_init_data *); 194 static void clone_constructors_and_destructors (tree); 195 static tree build_clone (tree, tree); 196 static void update_vtable_entry_for_fn (tree, tree, tree, tree *, unsigned); 197 static void build_ctor_vtbl_group (tree, tree); 198 static void build_vtt (tree); 199 static tree binfo_ctor_vtable (tree); 200 static void build_vtt_inits (tree, tree, vec<constructor_elt, va_gc> **, 201 tree *); 202 static tree dfs_build_secondary_vptr_vtt_inits (tree, void *); 203 static tree dfs_fixup_binfo_vtbls (tree, void *); 204 static int record_subobject_offset (tree, tree, splay_tree); 205 static int check_subobject_offset (tree, tree, splay_tree); 206 static int walk_subobject_offsets (tree, subobject_offset_fn, 207 tree, splay_tree, tree, int); 208 static void record_subobject_offsets (tree, tree, splay_tree, bool); 209 static int layout_conflict_p (tree, tree, splay_tree, int); 210 static int splay_tree_compare_integer_csts (splay_tree_key k1, 211 splay_tree_key k2); 212 static void warn_about_ambiguous_bases (tree); 213 static bool type_requires_array_cookie (tree); 214 static bool base_derived_from (tree, tree); 215 static int empty_base_at_nonzero_offset_p (tree, tree, splay_tree); 216 static tree end_of_base (tree); 217 static tree get_vcall_index (tree, tree); 218 static bool type_maybe_constexpr_default_constructor (tree); 219 220 /* Variables shared between class.c and call.c. */ 221 222 int n_vtables = 0; 223 int n_vtable_entries = 0; 224 int n_vtable_searches = 0; 225 int n_vtable_elems = 0; 226 int n_convert_harshness = 0; 227 int n_compute_conversion_costs = 0; 228 int n_inner_fields_searched = 0; 229 230 /* Return a COND_EXPR that executes TRUE_STMT if this execution of the 231 'structor is in charge of 'structing virtual bases, or FALSE_STMT 232 otherwise. */ 233 234 tree 235 build_if_in_charge (tree true_stmt, tree false_stmt) 236 { 237 gcc_assert (DECL_HAS_IN_CHARGE_PARM_P (current_function_decl)); 238 tree cmp = build2 (NE_EXPR, boolean_type_node, 239 current_in_charge_parm, integer_zero_node); 240 tree type = unlowered_expr_type (true_stmt); 241 if (VOID_TYPE_P (type)) 242 type = unlowered_expr_type (false_stmt); 243 tree cond = build3 (COND_EXPR, type, 244 cmp, true_stmt, false_stmt); 245 return cond; 246 } 247 248 /* Convert to or from a base subobject. EXPR is an expression of type 249 `A' or `A*', an expression of type `B' or `B*' is returned. To 250 convert A to a base B, CODE is PLUS_EXPR and BINFO is the binfo for 251 the B base instance within A. To convert base A to derived B, CODE 252 is MINUS_EXPR and BINFO is the binfo for the A instance within B. 253 In this latter case, A must not be a morally virtual base of B. 254 NONNULL is true if EXPR is known to be non-NULL (this is only 255 needed when EXPR is of pointer type). CV qualifiers are preserved 256 from EXPR. */ 257 258 tree 259 build_base_path (enum tree_code code, 260 tree expr, 261 tree binfo, 262 int nonnull, 263 tsubst_flags_t complain) 264 { 265 tree v_binfo = NULL_TREE; 266 tree d_binfo = NULL_TREE; 267 tree probe; 268 tree offset; 269 tree target_type; 270 tree null_test = NULL; 271 tree ptr_target_type; 272 int fixed_type_p; 273 int want_pointer = TYPE_PTR_P (TREE_TYPE (expr)); 274 bool has_empty = false; 275 bool virtual_access; 276 bool rvalue = false; 277 278 if (expr == error_mark_node || binfo == error_mark_node || !binfo) 279 return error_mark_node; 280 281 for (probe = binfo; probe; probe = BINFO_INHERITANCE_CHAIN (probe)) 282 { 283 d_binfo = probe; 284 if (is_empty_class (BINFO_TYPE (probe))) 285 has_empty = true; 286 if (!v_binfo && BINFO_VIRTUAL_P (probe)) 287 v_binfo = probe; 288 } 289 290 probe = TYPE_MAIN_VARIANT (TREE_TYPE (expr)); 291 if (want_pointer) 292 probe = TYPE_MAIN_VARIANT (TREE_TYPE (probe)); 293 294 if (code == PLUS_EXPR 295 && !SAME_BINFO_TYPE_P (BINFO_TYPE (d_binfo), probe)) 296 { 297 /* This can happen when adjust_result_of_qualified_name_lookup can't 298 find a unique base binfo in a call to a member function. We 299 couldn't give the diagnostic then since we might have been calling 300 a static member function, so we do it now. In other cases, eg. 301 during error recovery (c++/71979), we may not have a base at all. */ 302 if (complain & tf_error) 303 { 304 tree base = lookup_base (probe, BINFO_TYPE (d_binfo), 305 ba_unique, NULL, complain); 306 gcc_assert (base == error_mark_node || !base); 307 } 308 return error_mark_node; 309 } 310 311 gcc_assert ((code == MINUS_EXPR 312 && SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), probe)) 313 || code == PLUS_EXPR); 314 315 if (binfo == d_binfo) 316 /* Nothing to do. */ 317 return expr; 318 319 if (code == MINUS_EXPR && v_binfo) 320 { 321 if (complain & tf_error) 322 { 323 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (v_binfo))) 324 { 325 if (want_pointer) 326 error ("cannot convert from pointer to base class %qT to " 327 "pointer to derived class %qT because the base is " 328 "virtual", BINFO_TYPE (binfo), BINFO_TYPE (d_binfo)); 329 else 330 error ("cannot convert from base class %qT to derived " 331 "class %qT because the base is virtual", 332 BINFO_TYPE (binfo), BINFO_TYPE (d_binfo)); 333 } 334 else 335 { 336 if (want_pointer) 337 error ("cannot convert from pointer to base class %qT to " 338 "pointer to derived class %qT via virtual base %qT", 339 BINFO_TYPE (binfo), BINFO_TYPE (d_binfo), 340 BINFO_TYPE (v_binfo)); 341 else 342 error ("cannot convert from base class %qT to derived " 343 "class %qT via virtual base %qT", BINFO_TYPE (binfo), 344 BINFO_TYPE (d_binfo), BINFO_TYPE (v_binfo)); 345 } 346 } 347 return error_mark_node; 348 } 349 350 if (!want_pointer) 351 { 352 rvalue = !real_lvalue_p (expr); 353 /* This must happen before the call to save_expr. */ 354 expr = cp_build_addr_expr (expr, complain); 355 } 356 else 357 expr = mark_rvalue_use (expr); 358 359 offset = BINFO_OFFSET (binfo); 360 fixed_type_p = resolves_to_fixed_type_p (expr, &nonnull); 361 target_type = code == PLUS_EXPR ? BINFO_TYPE (binfo) : BINFO_TYPE (d_binfo); 362 /* TARGET_TYPE has been extracted from BINFO, and, is therefore always 363 cv-unqualified. Extract the cv-qualifiers from EXPR so that the 364 expression returned matches the input. */ 365 target_type = cp_build_qualified_type 366 (target_type, cp_type_quals (TREE_TYPE (TREE_TYPE (expr)))); 367 ptr_target_type = build_pointer_type (target_type); 368 369 /* Do we need to look in the vtable for the real offset? */ 370 virtual_access = (v_binfo && fixed_type_p <= 0); 371 372 /* Don't bother with the calculations inside sizeof; they'll ICE if the 373 source type is incomplete and the pointer value doesn't matter. In a 374 template (even in instantiate_non_dependent_expr), we don't have vtables 375 set up properly yet, and the value doesn't matter there either; we're 376 just interested in the result of overload resolution. */ 377 if (cp_unevaluated_operand != 0 378 || in_template_function ()) 379 { 380 expr = build_nop (ptr_target_type, expr); 381 goto indout; 382 } 383 384 /* If we're in an NSDMI, we don't have the full constructor context yet 385 that we need for converting to a virtual base, so just build a stub 386 CONVERT_EXPR and expand it later in bot_replace. */ 387 if (virtual_access && fixed_type_p < 0 388 && current_scope () != current_function_decl) 389 { 390 expr = build1 (CONVERT_EXPR, ptr_target_type, expr); 391 CONVERT_EXPR_VBASE_PATH (expr) = true; 392 goto indout; 393 } 394 395 /* Do we need to check for a null pointer? */ 396 if (want_pointer && !nonnull) 397 { 398 /* If we know the conversion will not actually change the value 399 of EXPR, then we can avoid testing the expression for NULL. 400 We have to avoid generating a COMPONENT_REF for a base class 401 field, because other parts of the compiler know that such 402 expressions are always non-NULL. */ 403 if (!virtual_access && integer_zerop (offset)) 404 return build_nop (ptr_target_type, expr); 405 null_test = error_mark_node; 406 } 407 408 /* Protect against multiple evaluation if necessary. */ 409 if (TREE_SIDE_EFFECTS (expr) && (null_test || virtual_access)) 410 expr = save_expr (expr); 411 412 /* Now that we've saved expr, build the real null test. */ 413 if (null_test) 414 { 415 tree zero = cp_convert (TREE_TYPE (expr), nullptr_node, complain); 416 null_test = build2_loc (input_location, NE_EXPR, boolean_type_node, 417 expr, zero); 418 /* This is a compiler generated comparison, don't emit 419 e.g. -Wnonnull-compare warning for it. */ 420 TREE_NO_WARNING (null_test) = 1; 421 } 422 423 /* If this is a simple base reference, express it as a COMPONENT_REF. */ 424 if (code == PLUS_EXPR && !virtual_access 425 /* We don't build base fields for empty bases, and they aren't very 426 interesting to the optimizers anyway. */ 427 && !has_empty) 428 { 429 expr = cp_build_indirect_ref (expr, RO_NULL, complain); 430 expr = build_simple_base_path (expr, binfo); 431 if (rvalue) 432 expr = move (expr); 433 if (want_pointer) 434 expr = build_address (expr); 435 target_type = TREE_TYPE (expr); 436 goto out; 437 } 438 439 if (virtual_access) 440 { 441 /* Going via virtual base V_BINFO. We need the static offset 442 from V_BINFO to BINFO, and the dynamic offset from D_BINFO to 443 V_BINFO. That offset is an entry in D_BINFO's vtable. */ 444 tree v_offset; 445 446 if (fixed_type_p < 0 && in_base_initializer) 447 { 448 /* In a base member initializer, we cannot rely on the 449 vtable being set up. We have to indirect via the 450 vtt_parm. */ 451 tree t; 452 453 t = TREE_TYPE (TYPE_VFIELD (current_class_type)); 454 t = build_pointer_type (t); 455 v_offset = fold_convert (t, current_vtt_parm); 456 v_offset = cp_build_indirect_ref (v_offset, RO_NULL, complain); 457 } 458 else 459 { 460 tree t = expr; 461 if ((flag_sanitize & SANITIZE_VPTR) && fixed_type_p == 0) 462 { 463 t = cp_ubsan_maybe_instrument_cast_to_vbase (input_location, 464 probe, expr); 465 if (t == NULL_TREE) 466 t = expr; 467 } 468 v_offset = build_vfield_ref (cp_build_indirect_ref (t, RO_NULL, 469 complain), 470 TREE_TYPE (TREE_TYPE (expr))); 471 } 472 473 if (v_offset == error_mark_node) 474 return error_mark_node; 475 476 v_offset = fold_build_pointer_plus (v_offset, BINFO_VPTR_FIELD (v_binfo)); 477 v_offset = build1 (NOP_EXPR, 478 build_pointer_type (ptrdiff_type_node), 479 v_offset); 480 v_offset = cp_build_indirect_ref (v_offset, RO_NULL, complain); 481 TREE_CONSTANT (v_offset) = 1; 482 483 offset = convert_to_integer (ptrdiff_type_node, 484 size_diffop_loc (input_location, offset, 485 BINFO_OFFSET (v_binfo))); 486 487 if (!integer_zerop (offset)) 488 v_offset = build2 (code, ptrdiff_type_node, v_offset, offset); 489 490 if (fixed_type_p < 0) 491 /* Negative fixed_type_p means this is a constructor or destructor; 492 virtual base layout is fixed in in-charge [cd]tors, but not in 493 base [cd]tors. */ 494 offset = build_if_in_charge 495 (convert_to_integer (ptrdiff_type_node, BINFO_OFFSET (binfo)), 496 v_offset); 497 else 498 offset = v_offset; 499 } 500 501 if (want_pointer) 502 target_type = ptr_target_type; 503 504 expr = build1 (NOP_EXPR, ptr_target_type, expr); 505 506 if (!integer_zerop (offset)) 507 { 508 offset = fold_convert (sizetype, offset); 509 if (code == MINUS_EXPR) 510 offset = fold_build1_loc (input_location, NEGATE_EXPR, sizetype, offset); 511 expr = fold_build_pointer_plus (expr, offset); 512 } 513 else 514 null_test = NULL; 515 516 indout: 517 if (!want_pointer) 518 { 519 expr = cp_build_indirect_ref (expr, RO_NULL, complain); 520 if (rvalue) 521 expr = move (expr); 522 } 523 524 out: 525 if (null_test) 526 expr = fold_build3_loc (input_location, COND_EXPR, target_type, null_test, expr, 527 build_zero_cst (target_type)); 528 529 return expr; 530 } 531 532 /* Subroutine of build_base_path; EXPR and BINFO are as in that function. 533 Perform a derived-to-base conversion by recursively building up a 534 sequence of COMPONENT_REFs to the appropriate base fields. */ 535 536 static tree 537 build_simple_base_path (tree expr, tree binfo) 538 { 539 tree type = BINFO_TYPE (binfo); 540 tree d_binfo = BINFO_INHERITANCE_CHAIN (binfo); 541 tree field; 542 543 if (d_binfo == NULL_TREE) 544 { 545 tree temp; 546 547 gcc_assert (TYPE_MAIN_VARIANT (TREE_TYPE (expr)) == type); 548 549 /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x' 550 into `(*(a ? &b : &c)).x', and so on. A COND_EXPR is only 551 an lvalue in the front end; only _DECLs and _REFs are lvalues 552 in the back end. */ 553 temp = unary_complex_lvalue (ADDR_EXPR, expr); 554 if (temp) 555 expr = cp_build_indirect_ref (temp, RO_NULL, tf_warning_or_error); 556 557 return expr; 558 } 559 560 /* Recurse. */ 561 expr = build_simple_base_path (expr, d_binfo); 562 563 for (field = TYPE_FIELDS (BINFO_TYPE (d_binfo)); 564 field; field = DECL_CHAIN (field)) 565 /* Is this the base field created by build_base_field? */ 566 if (TREE_CODE (field) == FIELD_DECL 567 && DECL_FIELD_IS_BASE (field) 568 && TREE_TYPE (field) == type 569 /* If we're looking for a field in the most-derived class, 570 also check the field offset; we can have two base fields 571 of the same type if one is an indirect virtual base and one 572 is a direct non-virtual base. */ 573 && (BINFO_INHERITANCE_CHAIN (d_binfo) 574 || tree_int_cst_equal (byte_position (field), 575 BINFO_OFFSET (binfo)))) 576 { 577 /* We don't use build_class_member_access_expr here, as that 578 has unnecessary checks, and more importantly results in 579 recursive calls to dfs_walk_once. */ 580 int type_quals = cp_type_quals (TREE_TYPE (expr)); 581 582 expr = build3 (COMPONENT_REF, 583 cp_build_qualified_type (type, type_quals), 584 expr, field, NULL_TREE); 585 /* Mark the expression const or volatile, as appropriate. 586 Even though we've dealt with the type above, we still have 587 to mark the expression itself. */ 588 if (type_quals & TYPE_QUAL_CONST) 589 TREE_READONLY (expr) = 1; 590 if (type_quals & TYPE_QUAL_VOLATILE) 591 TREE_THIS_VOLATILE (expr) = 1; 592 593 return expr; 594 } 595 596 /* Didn't find the base field?!? */ 597 gcc_unreachable (); 598 } 599 600 /* Convert OBJECT to the base TYPE. OBJECT is an expression whose 601 type is a class type or a pointer to a class type. In the former 602 case, TYPE is also a class type; in the latter it is another 603 pointer type. If CHECK_ACCESS is true, an error message is emitted 604 if TYPE is inaccessible. If OBJECT has pointer type, the value is 605 assumed to be non-NULL. */ 606 607 tree 608 convert_to_base (tree object, tree type, bool check_access, bool nonnull, 609 tsubst_flags_t complain) 610 { 611 tree binfo; 612 tree object_type; 613 614 if (TYPE_PTR_P (TREE_TYPE (object))) 615 { 616 object_type = TREE_TYPE (TREE_TYPE (object)); 617 type = TREE_TYPE (type); 618 } 619 else 620 object_type = TREE_TYPE (object); 621 622 binfo = lookup_base (object_type, type, check_access ? ba_check : ba_unique, 623 NULL, complain); 624 if (!binfo || binfo == error_mark_node) 625 return error_mark_node; 626 627 return build_base_path (PLUS_EXPR, object, binfo, nonnull, complain); 628 } 629 630 /* EXPR is an expression with unqualified class type. BASE is a base 631 binfo of that class type. Returns EXPR, converted to the BASE 632 type. This function assumes that EXPR is the most derived class; 633 therefore virtual bases can be found at their static offsets. */ 634 635 tree 636 convert_to_base_statically (tree expr, tree base) 637 { 638 tree expr_type; 639 640 expr_type = TREE_TYPE (expr); 641 if (!SAME_BINFO_TYPE_P (BINFO_TYPE (base), expr_type)) 642 { 643 /* If this is a non-empty base, use a COMPONENT_REF. */ 644 if (!is_empty_class (BINFO_TYPE (base))) 645 return build_simple_base_path (expr, base); 646 647 /* We use fold_build2 and fold_convert below to simplify the trees 648 provided to the optimizers. It is not safe to call these functions 649 when processing a template because they do not handle C++-specific 650 trees. */ 651 gcc_assert (!processing_template_decl); 652 expr = cp_build_addr_expr (expr, tf_warning_or_error); 653 if (!integer_zerop (BINFO_OFFSET (base))) 654 expr = fold_build_pointer_plus_loc (input_location, 655 expr, BINFO_OFFSET (base)); 656 expr = fold_convert (build_pointer_type (BINFO_TYPE (base)), expr); 657 expr = build_fold_indirect_ref_loc (input_location, expr); 658 } 659 660 return expr; 661 } 662 663 664 tree 665 build_vfield_ref (tree datum, tree type) 666 { 667 tree vfield, vcontext; 668 669 if (datum == error_mark_node 670 /* Can happen in case of duplicate base types (c++/59082). */ 671 || !TYPE_VFIELD (type)) 672 return error_mark_node; 673 674 /* First, convert to the requested type. */ 675 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (datum), type)) 676 datum = convert_to_base (datum, type, /*check_access=*/false, 677 /*nonnull=*/true, tf_warning_or_error); 678 679 /* Second, the requested type may not be the owner of its own vptr. 680 If not, convert to the base class that owns it. We cannot use 681 convert_to_base here, because VCONTEXT may appear more than once 682 in the inheritance hierarchy of TYPE, and thus direct conversion 683 between the types may be ambiguous. Following the path back up 684 one step at a time via primary bases avoids the problem. */ 685 vfield = TYPE_VFIELD (type); 686 vcontext = DECL_CONTEXT (vfield); 687 while (!same_type_ignoring_top_level_qualifiers_p (vcontext, type)) 688 { 689 datum = build_simple_base_path (datum, CLASSTYPE_PRIMARY_BINFO (type)); 690 type = TREE_TYPE (datum); 691 } 692 693 return build3 (COMPONENT_REF, TREE_TYPE (vfield), datum, vfield, NULL_TREE); 694 } 695 696 /* Given an object INSTANCE, return an expression which yields the 697 vtable element corresponding to INDEX. There are many special 698 cases for INSTANCE which we take care of here, mainly to avoid 699 creating extra tree nodes when we don't have to. */ 700 701 static tree 702 build_vtbl_ref_1 (tree instance, tree idx) 703 { 704 tree aref; 705 tree vtbl = NULL_TREE; 706 707 /* Try to figure out what a reference refers to, and 708 access its virtual function table directly. */ 709 710 int cdtorp = 0; 711 tree fixed_type = fixed_type_or_null (instance, NULL, &cdtorp); 712 713 tree basetype = non_reference (TREE_TYPE (instance)); 714 715 if (fixed_type && !cdtorp) 716 { 717 tree binfo = lookup_base (fixed_type, basetype, 718 ba_unique, NULL, tf_none); 719 if (binfo && binfo != error_mark_node) 720 vtbl = unshare_expr (BINFO_VTABLE (binfo)); 721 } 722 723 if (!vtbl) 724 vtbl = build_vfield_ref (instance, basetype); 725 726 aref = build_array_ref (input_location, vtbl, idx); 727 TREE_CONSTANT (aref) |= TREE_CONSTANT (vtbl) && TREE_CONSTANT (idx); 728 729 return aref; 730 } 731 732 tree 733 build_vtbl_ref (tree instance, tree idx) 734 { 735 tree aref = build_vtbl_ref_1 (instance, idx); 736 737 return aref; 738 } 739 740 /* Given a stable object pointer INSTANCE_PTR, return an expression which 741 yields a function pointer corresponding to vtable element INDEX. */ 742 743 tree 744 build_vfn_ref (tree instance_ptr, tree idx) 745 { 746 tree aref; 747 748 aref = build_vtbl_ref_1 (cp_build_indirect_ref (instance_ptr, RO_NULL, 749 tf_warning_or_error), 750 idx); 751 752 /* When using function descriptors, the address of the 753 vtable entry is treated as a function pointer. */ 754 if (TARGET_VTABLE_USES_DESCRIPTORS) 755 aref = build1 (NOP_EXPR, TREE_TYPE (aref), 756 cp_build_addr_expr (aref, tf_warning_or_error)); 757 758 /* Remember this as a method reference, for later devirtualization. */ 759 aref = build3 (OBJ_TYPE_REF, TREE_TYPE (aref), aref, instance_ptr, idx); 760 761 return aref; 762 } 763 764 /* Return the name of the virtual function table (as an IDENTIFIER_NODE) 765 for the given TYPE. */ 766 767 static tree 768 get_vtable_name (tree type) 769 { 770 return mangle_vtbl_for_type (type); 771 } 772 773 /* DECL is an entity associated with TYPE, like a virtual table or an 774 implicitly generated constructor. Determine whether or not DECL 775 should have external or internal linkage at the object file 776 level. This routine does not deal with COMDAT linkage and other 777 similar complexities; it simply sets TREE_PUBLIC if it possible for 778 entities in other translation units to contain copies of DECL, in 779 the abstract. */ 780 781 void 782 set_linkage_according_to_type (tree /*type*/, tree decl) 783 { 784 TREE_PUBLIC (decl) = 1; 785 determine_visibility (decl); 786 } 787 788 /* Create a VAR_DECL for a primary or secondary vtable for CLASS_TYPE. 789 (For a secondary vtable for B-in-D, CLASS_TYPE should be D, not B.) 790 Use NAME for the name of the vtable, and VTABLE_TYPE for its type. */ 791 792 static tree 793 build_vtable (tree class_type, tree name, tree vtable_type) 794 { 795 tree decl; 796 797 decl = build_lang_decl (VAR_DECL, name, vtable_type); 798 /* vtable names are already mangled; give them their DECL_ASSEMBLER_NAME 799 now to avoid confusion in mangle_decl. */ 800 SET_DECL_ASSEMBLER_NAME (decl, name); 801 DECL_CONTEXT (decl) = class_type; 802 DECL_ARTIFICIAL (decl) = 1; 803 TREE_STATIC (decl) = 1; 804 TREE_READONLY (decl) = 1; 805 DECL_VIRTUAL_P (decl) = 1; 806 DECL_ALIGN (decl) = TARGET_VTABLE_ENTRY_ALIGN; 807 DECL_USER_ALIGN (decl) = true; 808 DECL_VTABLE_OR_VTT_P (decl) = 1; 809 set_linkage_according_to_type (class_type, decl); 810 /* The vtable has not been defined -- yet. */ 811 DECL_EXTERNAL (decl) = 1; 812 DECL_NOT_REALLY_EXTERN (decl) = 1; 813 814 /* Mark the VAR_DECL node representing the vtable itself as a 815 "gratuitous" one, thereby forcing dwarfout.c to ignore it. It 816 is rather important that such things be ignored because any 817 effort to actually generate DWARF for them will run into 818 trouble when/if we encounter code like: 819 820 #pragma interface 821 struct S { virtual void member (); }; 822 823 because the artificial declaration of the vtable itself (as 824 manufactured by the g++ front end) will say that the vtable is 825 a static member of `S' but only *after* the debug output for 826 the definition of `S' has already been output. This causes 827 grief because the DWARF entry for the definition of the vtable 828 will try to refer back to an earlier *declaration* of the 829 vtable as a static member of `S' and there won't be one. We 830 might be able to arrange to have the "vtable static member" 831 attached to the member list for `S' before the debug info for 832 `S' get written (which would solve the problem) but that would 833 require more intrusive changes to the g++ front end. */ 834 DECL_IGNORED_P (decl) = 1; 835 836 return decl; 837 } 838 839 /* Get the VAR_DECL of the vtable for TYPE. TYPE need not be polymorphic, 840 or even complete. If this does not exist, create it. If COMPLETE is 841 nonzero, then complete the definition of it -- that will render it 842 impossible to actually build the vtable, but is useful to get at those 843 which are known to exist in the runtime. */ 844 845 tree 846 get_vtable_decl (tree type, int complete) 847 { 848 tree decl; 849 850 if (CLASSTYPE_VTABLES (type)) 851 return CLASSTYPE_VTABLES (type); 852 853 decl = build_vtable (type, get_vtable_name (type), vtbl_type_node); 854 CLASSTYPE_VTABLES (type) = decl; 855 856 if (complete) 857 { 858 DECL_EXTERNAL (decl) = 1; 859 cp_finish_decl (decl, NULL_TREE, false, NULL_TREE, 0); 860 } 861 862 return decl; 863 } 864 865 /* Build the primary virtual function table for TYPE. If BINFO is 866 non-NULL, build the vtable starting with the initial approximation 867 that it is the same as the one which is the head of the association 868 list. Returns a nonzero value if a new vtable is actually 869 created. */ 870 871 static int 872 build_primary_vtable (tree binfo, tree type) 873 { 874 tree decl; 875 tree virtuals; 876 877 decl = get_vtable_decl (type, /*complete=*/0); 878 879 if (binfo) 880 { 881 if (BINFO_NEW_VTABLE_MARKED (binfo)) 882 /* We have already created a vtable for this base, so there's 883 no need to do it again. */ 884 return 0; 885 886 virtuals = copy_list (BINFO_VIRTUALS (binfo)); 887 TREE_TYPE (decl) = TREE_TYPE (get_vtbl_decl_for_binfo (binfo)); 888 DECL_SIZE (decl) = TYPE_SIZE (TREE_TYPE (decl)); 889 DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (TREE_TYPE (decl)); 890 } 891 else 892 { 893 gcc_assert (TREE_TYPE (decl) == vtbl_type_node); 894 virtuals = NULL_TREE; 895 } 896 897 if (GATHER_STATISTICS) 898 { 899 n_vtables += 1; 900 n_vtable_elems += list_length (virtuals); 901 } 902 903 /* Initialize the association list for this type, based 904 on our first approximation. */ 905 BINFO_VTABLE (TYPE_BINFO (type)) = decl; 906 BINFO_VIRTUALS (TYPE_BINFO (type)) = virtuals; 907 SET_BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (type)); 908 return 1; 909 } 910 911 /* Give BINFO a new virtual function table which is initialized 912 with a skeleton-copy of its original initialization. The only 913 entry that changes is the `delta' entry, so we can really 914 share a lot of structure. 915 916 FOR_TYPE is the most derived type which caused this table to 917 be needed. 918 919 Returns nonzero if we haven't met BINFO before. 920 921 The order in which vtables are built (by calling this function) for 922 an object must remain the same, otherwise a binary incompatibility 923 can result. */ 924 925 static int 926 build_secondary_vtable (tree binfo) 927 { 928 if (BINFO_NEW_VTABLE_MARKED (binfo)) 929 /* We already created a vtable for this base. There's no need to 930 do it again. */ 931 return 0; 932 933 /* Remember that we've created a vtable for this BINFO, so that we 934 don't try to do so again. */ 935 SET_BINFO_NEW_VTABLE_MARKED (binfo); 936 937 /* Make fresh virtual list, so we can smash it later. */ 938 BINFO_VIRTUALS (binfo) = copy_list (BINFO_VIRTUALS (binfo)); 939 940 /* Secondary vtables are laid out as part of the same structure as 941 the primary vtable. */ 942 BINFO_VTABLE (binfo) = NULL_TREE; 943 return 1; 944 } 945 946 /* Create a new vtable for BINFO which is the hierarchy dominated by 947 T. Return nonzero if we actually created a new vtable. */ 948 949 static int 950 make_new_vtable (tree t, tree binfo) 951 { 952 if (binfo == TYPE_BINFO (t)) 953 /* In this case, it is *type*'s vtable we are modifying. We start 954 with the approximation that its vtable is that of the 955 immediate base class. */ 956 return build_primary_vtable (binfo, t); 957 else 958 /* This is our very own copy of `basetype' to play with. Later, 959 we will fill in all the virtual functions that override the 960 virtual functions in these base classes which are not defined 961 by the current type. */ 962 return build_secondary_vtable (binfo); 963 } 964 965 /* Make *VIRTUALS, an entry on the BINFO_VIRTUALS list for BINFO 966 (which is in the hierarchy dominated by T) list FNDECL as its 967 BV_FN. DELTA is the required constant adjustment from the `this' 968 pointer where the vtable entry appears to the `this' required when 969 the function is actually called. */ 970 971 static void 972 modify_vtable_entry (tree t, 973 tree binfo, 974 tree fndecl, 975 tree delta, 976 tree *virtuals) 977 { 978 tree v; 979 980 v = *virtuals; 981 982 if (fndecl != BV_FN (v) 983 || !tree_int_cst_equal (delta, BV_DELTA (v))) 984 { 985 /* We need a new vtable for BINFO. */ 986 if (make_new_vtable (t, binfo)) 987 { 988 /* If we really did make a new vtable, we also made a copy 989 of the BINFO_VIRTUALS list. Now, we have to find the 990 corresponding entry in that list. */ 991 *virtuals = BINFO_VIRTUALS (binfo); 992 while (BV_FN (*virtuals) != BV_FN (v)) 993 *virtuals = TREE_CHAIN (*virtuals); 994 v = *virtuals; 995 } 996 997 BV_DELTA (v) = delta; 998 BV_VCALL_INDEX (v) = NULL_TREE; 999 BV_FN (v) = fndecl; 1000 } 1001 } 1002 1003 1004 /* Add method METHOD to class TYPE. If USING_DECL is non-null, it is 1005 the USING_DECL naming METHOD. Returns true if the method could be 1006 added to the method vec. */ 1007 1008 bool 1009 add_method (tree type, tree method, tree using_decl) 1010 { 1011 unsigned slot; 1012 tree overload; 1013 bool template_conv_p = false; 1014 bool conv_p; 1015 vec<tree, va_gc> *method_vec; 1016 bool complete_p; 1017 bool insert_p = false; 1018 tree current_fns; 1019 tree fns; 1020 1021 if (method == error_mark_node) 1022 return false; 1023 1024 complete_p = COMPLETE_TYPE_P (type); 1025 conv_p = DECL_CONV_FN_P (method); 1026 if (conv_p) 1027 template_conv_p = (TREE_CODE (method) == TEMPLATE_DECL 1028 && DECL_TEMPLATE_CONV_FN_P (method)); 1029 1030 method_vec = CLASSTYPE_METHOD_VEC (type); 1031 if (!method_vec) 1032 { 1033 /* Make a new method vector. We start with 8 entries. We must 1034 allocate at least two (for constructors and destructors), and 1035 we're going to end up with an assignment operator at some 1036 point as well. */ 1037 vec_alloc (method_vec, 8); 1038 /* Create slots for constructors and destructors. */ 1039 method_vec->quick_push (NULL_TREE); 1040 method_vec->quick_push (NULL_TREE); 1041 CLASSTYPE_METHOD_VEC (type) = method_vec; 1042 } 1043 1044 /* Maintain TYPE_HAS_USER_CONSTRUCTOR, etc. */ 1045 grok_special_member_properties (method); 1046 1047 /* Constructors and destructors go in special slots. */ 1048 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (method)) 1049 slot = CLASSTYPE_CONSTRUCTOR_SLOT; 1050 else if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (method)) 1051 { 1052 slot = CLASSTYPE_DESTRUCTOR_SLOT; 1053 1054 if (TYPE_FOR_JAVA (type)) 1055 { 1056 if (!DECL_ARTIFICIAL (method)) 1057 error ("Java class %qT cannot have a destructor", type); 1058 else if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)) 1059 error ("Java class %qT cannot have an implicit non-trivial " 1060 "destructor", 1061 type); 1062 } 1063 } 1064 else 1065 { 1066 tree m; 1067 1068 insert_p = true; 1069 /* See if we already have an entry with this name. */ 1070 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT; 1071 vec_safe_iterate (method_vec, slot, &m); 1072 ++slot) 1073 { 1074 m = OVL_CURRENT (m); 1075 if (template_conv_p) 1076 { 1077 if (TREE_CODE (m) == TEMPLATE_DECL 1078 && DECL_TEMPLATE_CONV_FN_P (m)) 1079 insert_p = false; 1080 break; 1081 } 1082 if (conv_p && !DECL_CONV_FN_P (m)) 1083 break; 1084 if (DECL_NAME (m) == DECL_NAME (method)) 1085 { 1086 insert_p = false; 1087 break; 1088 } 1089 if (complete_p 1090 && !DECL_CONV_FN_P (m) 1091 && DECL_NAME (m) > DECL_NAME (method)) 1092 break; 1093 } 1094 } 1095 current_fns = insert_p ? NULL_TREE : (*method_vec)[slot]; 1096 1097 /* Check to see if we've already got this method. */ 1098 for (fns = current_fns; fns; fns = OVL_NEXT (fns)) 1099 { 1100 tree fn = OVL_CURRENT (fns); 1101 tree fn_type; 1102 tree method_type; 1103 tree parms1; 1104 tree parms2; 1105 1106 if (TREE_CODE (fn) != TREE_CODE (method)) 1107 continue; 1108 1109 /* [over.load] Member function declarations with the 1110 same name and the same parameter types cannot be 1111 overloaded if any of them is a static member 1112 function declaration. 1113 1114 [over.load] Member function declarations with the same name and 1115 the same parameter-type-list as well as member function template 1116 declarations with the same name, the same parameter-type-list, and 1117 the same template parameter lists cannot be overloaded if any of 1118 them, but not all, have a ref-qualifier. 1119 1120 [namespace.udecl] When a using-declaration brings names 1121 from a base class into a derived class scope, member 1122 functions in the derived class override and/or hide member 1123 functions with the same name and parameter types in a base 1124 class (rather than conflicting). */ 1125 fn_type = TREE_TYPE (fn); 1126 method_type = TREE_TYPE (method); 1127 parms1 = TYPE_ARG_TYPES (fn_type); 1128 parms2 = TYPE_ARG_TYPES (method_type); 1129 1130 /* Compare the quals on the 'this' parm. Don't compare 1131 the whole types, as used functions are treated as 1132 coming from the using class in overload resolution. */ 1133 if (! DECL_STATIC_FUNCTION_P (fn) 1134 && ! DECL_STATIC_FUNCTION_P (method) 1135 /* Either both or neither need to be ref-qualified for 1136 differing quals to allow overloading. */ 1137 && (FUNCTION_REF_QUALIFIED (fn_type) 1138 == FUNCTION_REF_QUALIFIED (method_type)) 1139 && (type_memfn_quals (fn_type) != type_memfn_quals (method_type) 1140 || type_memfn_rqual (fn_type) != type_memfn_rqual (method_type))) 1141 continue; 1142 1143 /* For templates, the return type and template parameters 1144 must be identical. */ 1145 if (TREE_CODE (fn) == TEMPLATE_DECL 1146 && (!same_type_p (TREE_TYPE (fn_type), 1147 TREE_TYPE (method_type)) 1148 || !comp_template_parms (DECL_TEMPLATE_PARMS (fn), 1149 DECL_TEMPLATE_PARMS (method)))) 1150 continue; 1151 1152 if (! DECL_STATIC_FUNCTION_P (fn)) 1153 parms1 = TREE_CHAIN (parms1); 1154 if (! DECL_STATIC_FUNCTION_P (method)) 1155 parms2 = TREE_CHAIN (parms2); 1156 1157 if (compparms (parms1, parms2) 1158 && (!DECL_CONV_FN_P (fn) 1159 || same_type_p (TREE_TYPE (fn_type), 1160 TREE_TYPE (method_type))) 1161 && equivalently_constrained (fn, method)) 1162 { 1163 /* For function versions, their parms and types match 1164 but they are not duplicates. Record function versions 1165 as and when they are found. extern "C" functions are 1166 not treated as versions. */ 1167 if (TREE_CODE (fn) == FUNCTION_DECL 1168 && TREE_CODE (method) == FUNCTION_DECL 1169 && !DECL_EXTERN_C_P (fn) 1170 && !DECL_EXTERN_C_P (method) 1171 && targetm.target_option.function_versions (fn, method)) 1172 { 1173 /* Mark functions as versions if necessary. Modify the mangled 1174 decl name if necessary. */ 1175 if (!DECL_FUNCTION_VERSIONED (fn)) 1176 { 1177 DECL_FUNCTION_VERSIONED (fn) = 1; 1178 if (DECL_ASSEMBLER_NAME_SET_P (fn)) 1179 mangle_decl (fn); 1180 } 1181 if (!DECL_FUNCTION_VERSIONED (method)) 1182 { 1183 DECL_FUNCTION_VERSIONED (method) = 1; 1184 if (DECL_ASSEMBLER_NAME_SET_P (method)) 1185 mangle_decl (method); 1186 } 1187 cgraph_node::record_function_versions (fn, method); 1188 continue; 1189 } 1190 if (DECL_INHERITED_CTOR_BASE (method)) 1191 { 1192 if (DECL_INHERITED_CTOR_BASE (fn)) 1193 { 1194 error_at (DECL_SOURCE_LOCATION (method), 1195 "%q#D inherited from %qT", method, 1196 DECL_INHERITED_CTOR_BASE (method)); 1197 error_at (DECL_SOURCE_LOCATION (fn), 1198 "conflicts with version inherited from %qT", 1199 DECL_INHERITED_CTOR_BASE (fn)); 1200 } 1201 /* Otherwise defer to the other function. */ 1202 return false; 1203 } 1204 if (using_decl) 1205 { 1206 if (DECL_CONTEXT (fn) == type) 1207 /* Defer to the local function. */ 1208 return false; 1209 } 1210 else 1211 { 1212 error ("%q+#D cannot be overloaded", method); 1213 error ("with %q+#D", fn); 1214 } 1215 1216 /* We don't call duplicate_decls here to merge the 1217 declarations because that will confuse things if the 1218 methods have inline definitions. In particular, we 1219 will crash while processing the definitions. */ 1220 return false; 1221 } 1222 } 1223 1224 /* A class should never have more than one destructor. */ 1225 if (current_fns && DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (method)) 1226 return false; 1227 1228 /* Add the new binding. */ 1229 if (using_decl) 1230 { 1231 overload = ovl_cons (method, current_fns); 1232 OVL_USED (overload) = true; 1233 } 1234 else 1235 overload = build_overload (method, current_fns); 1236 1237 if (conv_p) 1238 TYPE_HAS_CONVERSION (type) = 1; 1239 else if (slot >= CLASSTYPE_FIRST_CONVERSION_SLOT && !complete_p) 1240 push_class_level_binding (DECL_NAME (method), overload); 1241 1242 if (insert_p) 1243 { 1244 bool reallocated; 1245 1246 /* We only expect to add few methods in the COMPLETE_P case, so 1247 just make room for one more method in that case. */ 1248 if (complete_p) 1249 reallocated = vec_safe_reserve_exact (method_vec, 1); 1250 else 1251 reallocated = vec_safe_reserve (method_vec, 1); 1252 if (reallocated) 1253 CLASSTYPE_METHOD_VEC (type) = method_vec; 1254 if (slot == method_vec->length ()) 1255 method_vec->quick_push (overload); 1256 else 1257 method_vec->quick_insert (slot, overload); 1258 } 1259 else 1260 /* Replace the current slot. */ 1261 (*method_vec)[slot] = overload; 1262 return true; 1263 } 1264 1265 /* Subroutines of finish_struct. */ 1266 1267 /* Change the access of FDECL to ACCESS in T. Return 1 if change was 1268 legit, otherwise return 0. */ 1269 1270 static int 1271 alter_access (tree t, tree fdecl, tree access) 1272 { 1273 tree elem; 1274 1275 if (!DECL_LANG_SPECIFIC (fdecl)) 1276 retrofit_lang_decl (fdecl); 1277 1278 gcc_assert (!DECL_DISCRIMINATOR_P (fdecl)); 1279 1280 elem = purpose_member (t, DECL_ACCESS (fdecl)); 1281 if (elem) 1282 { 1283 if (TREE_VALUE (elem) != access) 1284 { 1285 if (TREE_CODE (TREE_TYPE (fdecl)) == FUNCTION_DECL) 1286 error ("conflicting access specifications for method" 1287 " %q+D, ignored", TREE_TYPE (fdecl)); 1288 else 1289 error ("conflicting access specifications for field %qE, ignored", 1290 DECL_NAME (fdecl)); 1291 } 1292 else 1293 { 1294 /* They're changing the access to the same thing they changed 1295 it to before. That's OK. */ 1296 ; 1297 } 1298 } 1299 else 1300 { 1301 perform_or_defer_access_check (TYPE_BINFO (t), fdecl, fdecl, 1302 tf_warning_or_error); 1303 DECL_ACCESS (fdecl) = tree_cons (t, access, DECL_ACCESS (fdecl)); 1304 return 1; 1305 } 1306 return 0; 1307 } 1308 1309 /* Process the USING_DECL, which is a member of T. */ 1310 1311 static void 1312 handle_using_decl (tree using_decl, tree t) 1313 { 1314 tree decl = USING_DECL_DECLS (using_decl); 1315 tree name = DECL_NAME (using_decl); 1316 tree access 1317 = TREE_PRIVATE (using_decl) ? access_private_node 1318 : TREE_PROTECTED (using_decl) ? access_protected_node 1319 : access_public_node; 1320 tree flist = NULL_TREE; 1321 tree old_value; 1322 1323 gcc_assert (!processing_template_decl && decl); 1324 1325 old_value = lookup_member (t, name, /*protect=*/0, /*want_type=*/false, 1326 tf_warning_or_error); 1327 if (old_value) 1328 { 1329 if (is_overloaded_fn (old_value)) 1330 old_value = OVL_CURRENT (old_value); 1331 1332 if (DECL_P (old_value) && DECL_CONTEXT (old_value) == t) 1333 /* OK */; 1334 else 1335 old_value = NULL_TREE; 1336 } 1337 1338 cp_emit_debug_info_for_using (decl, t); 1339 1340 if (is_overloaded_fn (decl)) 1341 flist = decl; 1342 1343 if (! old_value) 1344 ; 1345 else if (is_overloaded_fn (old_value)) 1346 { 1347 if (flist) 1348 /* It's OK to use functions from a base when there are functions with 1349 the same name already present in the current class. */; 1350 else 1351 { 1352 error ("%q+D invalid in %q#T", using_decl, t); 1353 error (" because of local method %q+#D with same name", 1354 OVL_CURRENT (old_value)); 1355 return; 1356 } 1357 } 1358 else if (!DECL_ARTIFICIAL (old_value)) 1359 { 1360 error ("%q+D invalid in %q#T", using_decl, t); 1361 error (" because of local member %q+#D with same name", old_value); 1362 return; 1363 } 1364 1365 /* Make type T see field decl FDECL with access ACCESS. */ 1366 if (flist) 1367 for (; flist; flist = OVL_NEXT (flist)) 1368 { 1369 add_method (t, OVL_CURRENT (flist), using_decl); 1370 alter_access (t, OVL_CURRENT (flist), access); 1371 } 1372 else 1373 alter_access (t, decl, access); 1374 } 1375 1376 /* Data structure for find_abi_tags_r, below. */ 1377 1378 struct abi_tag_data 1379 { 1380 tree t; // The type that we're checking for missing tags. 1381 tree subob; // The subobject of T that we're getting tags from. 1382 tree tags; // error_mark_node for diagnostics, or a list of missing tags. 1383 }; 1384 1385 /* Subroutine of find_abi_tags_r. Handle a single TAG found on the class TP 1386 in the context of P. TAG can be either an identifier (the DECL_NAME of 1387 a tag NAMESPACE_DECL) or a STRING_CST (a tag attribute). */ 1388 1389 static void 1390 check_tag (tree tag, tree id, tree *tp, abi_tag_data *p) 1391 { 1392 if (!IDENTIFIER_MARKED (id)) 1393 { 1394 if (p->tags != error_mark_node) 1395 { 1396 /* We're collecting tags from template arguments or from 1397 the type of a variable or function return type. */ 1398 p->tags = tree_cons (NULL_TREE, tag, p->tags); 1399 1400 /* Don't inherit this tag multiple times. */ 1401 IDENTIFIER_MARKED (id) = true; 1402 1403 if (TYPE_P (p->t)) 1404 { 1405 /* Tags inherited from type template arguments are only used 1406 to avoid warnings. */ 1407 ABI_TAG_IMPLICIT (p->tags) = true; 1408 return; 1409 } 1410 /* For functions and variables we want to warn, too. */ 1411 } 1412 1413 /* Otherwise we're diagnosing missing tags. */ 1414 if (TREE_CODE (p->t) == FUNCTION_DECL) 1415 { 1416 if (warning (OPT_Wabi_tag, "%qD inherits the %E ABI tag " 1417 "that %qT (used in its return type) has", 1418 p->t, tag, *tp)) 1419 inform (location_of (*tp), "%qT declared here", *tp); 1420 } 1421 else if (VAR_P (p->t)) 1422 { 1423 if (warning (OPT_Wabi_tag, "%qD inherits the %E ABI tag " 1424 "that %qT (used in its type) has", p->t, tag, *tp)) 1425 inform (location_of (*tp), "%qT declared here", *tp); 1426 } 1427 else if (TYPE_P (p->subob)) 1428 { 1429 if (warning (OPT_Wabi_tag, "%qT does not have the %E ABI tag " 1430 "that base %qT has", p->t, tag, p->subob)) 1431 inform (location_of (p->subob), "%qT declared here", 1432 p->subob); 1433 } 1434 else 1435 { 1436 if (warning (OPT_Wabi_tag, "%qT does not have the %E ABI tag " 1437 "that %qT (used in the type of %qD) has", 1438 p->t, tag, *tp, p->subob)) 1439 { 1440 inform (location_of (p->subob), "%qD declared here", 1441 p->subob); 1442 inform (location_of (*tp), "%qT declared here", *tp); 1443 } 1444 } 1445 } 1446 } 1447 1448 /* Find all the ABI tags in the attribute list ATTR and either call 1449 check_tag (if TP is non-null) or set IDENTIFIER_MARKED to val. */ 1450 1451 static void 1452 mark_or_check_attr_tags (tree attr, tree *tp, abi_tag_data *p, bool val) 1453 { 1454 if (!attr) 1455 return; 1456 for (; (attr = lookup_attribute ("abi_tag", attr)); 1457 attr = TREE_CHAIN (attr)) 1458 for (tree list = TREE_VALUE (attr); list; 1459 list = TREE_CHAIN (list)) 1460 { 1461 tree tag = TREE_VALUE (list); 1462 tree id = get_identifier (TREE_STRING_POINTER (tag)); 1463 if (tp) 1464 check_tag (tag, id, tp, p); 1465 else 1466 IDENTIFIER_MARKED (id) = val; 1467 } 1468 } 1469 1470 /* Find all the ABI tags on T and its enclosing scopes and either call 1471 check_tag (if TP is non-null) or set IDENTIFIER_MARKED to val. */ 1472 1473 static void 1474 mark_or_check_tags (tree t, tree *tp, abi_tag_data *p, bool val) 1475 { 1476 while (t != global_namespace) 1477 { 1478 tree attr; 1479 if (TYPE_P (t)) 1480 { 1481 attr = TYPE_ATTRIBUTES (t); 1482 t = CP_TYPE_CONTEXT (t); 1483 } 1484 else 1485 { 1486 attr = DECL_ATTRIBUTES (t); 1487 t = CP_DECL_CONTEXT (t); 1488 } 1489 mark_or_check_attr_tags (attr, tp, p, val); 1490 } 1491 } 1492 1493 /* walk_tree callback for check_abi_tags: if the type at *TP involves any 1494 types with ABI tags, add the corresponding identifiers to the VEC in 1495 *DATA and set IDENTIFIER_MARKED. */ 1496 1497 static tree 1498 find_abi_tags_r (tree *tp, int *walk_subtrees, void *data) 1499 { 1500 if (!OVERLOAD_TYPE_P (*tp)) 1501 return NULL_TREE; 1502 1503 /* walk_tree shouldn't be walking into any subtrees of a RECORD_TYPE 1504 anyway, but let's make sure of it. */ 1505 *walk_subtrees = false; 1506 1507 abi_tag_data *p = static_cast<struct abi_tag_data*>(data); 1508 1509 mark_or_check_tags (*tp, tp, p, false); 1510 1511 return NULL_TREE; 1512 } 1513 1514 /* walk_tree callback for mark_abi_tags: if *TP is a class, set 1515 IDENTIFIER_MARKED on its ABI tags. */ 1516 1517 static tree 1518 mark_abi_tags_r (tree *tp, int *walk_subtrees, void *data) 1519 { 1520 if (!OVERLOAD_TYPE_P (*tp)) 1521 return NULL_TREE; 1522 1523 /* walk_tree shouldn't be walking into any subtrees of a RECORD_TYPE 1524 anyway, but let's make sure of it. */ 1525 *walk_subtrees = false; 1526 1527 bool *valp = static_cast<bool*>(data); 1528 1529 mark_or_check_tags (*tp, NULL, NULL, *valp); 1530 1531 return NULL_TREE; 1532 } 1533 1534 /* Set IDENTIFIER_MARKED on all the ABI tags on T and its enclosing 1535 scopes. */ 1536 1537 static void 1538 mark_abi_tags (tree t, bool val) 1539 { 1540 mark_or_check_tags (t, NULL, NULL, val); 1541 if (DECL_P (t)) 1542 { 1543 if (DECL_LANG_SPECIFIC (t) && DECL_USE_TEMPLATE (t) 1544 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t))) 1545 { 1546 /* Template arguments are part of the signature. */ 1547 tree level = INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (t)); 1548 for (int j = 0; j < TREE_VEC_LENGTH (level); ++j) 1549 { 1550 tree arg = TREE_VEC_ELT (level, j); 1551 cp_walk_tree_without_duplicates (&arg, mark_abi_tags_r, &val); 1552 } 1553 } 1554 if (TREE_CODE (t) == FUNCTION_DECL) 1555 /* A function's parameter types are part of the signature, so 1556 we don't need to inherit any tags that are also in them. */ 1557 for (tree arg = FUNCTION_FIRST_USER_PARMTYPE (t); arg; 1558 arg = TREE_CHAIN (arg)) 1559 cp_walk_tree_without_duplicates (&TREE_VALUE (arg), 1560 mark_abi_tags_r, &val); 1561 } 1562 } 1563 1564 /* Check that T has all the ABI tags that subobject SUBOB has, or 1565 warn if not. If T is a (variable or function) declaration, also 1566 add any missing tags. */ 1567 1568 static void 1569 check_abi_tags (tree t, tree subob) 1570 { 1571 bool inherit = DECL_P (t); 1572 1573 if (!inherit && !warn_abi_tag) 1574 return; 1575 1576 tree decl = TYPE_P (t) ? TYPE_NAME (t) : t; 1577 if (!TREE_PUBLIC (decl)) 1578 /* No need to worry about things local to this TU. */ 1579 return; 1580 1581 mark_abi_tags (t, true); 1582 1583 tree subtype = TYPE_P (subob) ? subob : TREE_TYPE (subob); 1584 struct abi_tag_data data = { t, subob, error_mark_node }; 1585 if (inherit) 1586 data.tags = NULL_TREE; 1587 1588 cp_walk_tree_without_duplicates (&subtype, find_abi_tags_r, &data); 1589 1590 if (inherit && data.tags) 1591 { 1592 tree attr = lookup_attribute ("abi_tag", DECL_ATTRIBUTES (t)); 1593 if (attr) 1594 TREE_VALUE (attr) = chainon (data.tags, TREE_VALUE (attr)); 1595 else 1596 DECL_ATTRIBUTES (t) 1597 = tree_cons (get_identifier ("abi_tag"), data.tags, 1598 DECL_ATTRIBUTES (t)); 1599 } 1600 1601 mark_abi_tags (t, false); 1602 } 1603 1604 /* Check that DECL has all the ABI tags that are used in parts of its type 1605 that are not reflected in its mangled name. */ 1606 1607 void 1608 check_abi_tags (tree decl) 1609 { 1610 tree t; 1611 if (abi_version_at_least (10) 1612 && DECL_LANG_SPECIFIC (decl) 1613 && DECL_USE_TEMPLATE (decl) 1614 && (t = DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (decl)), 1615 t != decl)) 1616 /* Make sure that our template has the appropriate tags, since 1617 write_unqualified_name looks for them there. */ 1618 check_abi_tags (t); 1619 if (VAR_P (decl)) 1620 check_abi_tags (decl, TREE_TYPE (decl)); 1621 else if (TREE_CODE (decl) == FUNCTION_DECL 1622 && !mangle_return_type_p (decl)) 1623 check_abi_tags (decl, TREE_TYPE (TREE_TYPE (decl))); 1624 } 1625 1626 void 1627 inherit_targ_abi_tags (tree t) 1628 { 1629 if (!CLASS_TYPE_P (t) 1630 || CLASSTYPE_TEMPLATE_INFO (t) == NULL_TREE) 1631 return; 1632 1633 mark_abi_tags (t, true); 1634 1635 tree args = CLASSTYPE_TI_ARGS (t); 1636 struct abi_tag_data data = { t, NULL_TREE, NULL_TREE }; 1637 for (int i = 0; i < TMPL_ARGS_DEPTH (args); ++i) 1638 { 1639 tree level = TMPL_ARGS_LEVEL (args, i+1); 1640 for (int j = 0; j < TREE_VEC_LENGTH (level); ++j) 1641 { 1642 tree arg = TREE_VEC_ELT (level, j); 1643 data.subob = arg; 1644 cp_walk_tree_without_duplicates (&arg, find_abi_tags_r, &data); 1645 } 1646 } 1647 1648 // If we found some tags on our template arguments, add them to our 1649 // abi_tag attribute. 1650 if (data.tags) 1651 { 1652 tree attr = lookup_attribute ("abi_tag", TYPE_ATTRIBUTES (t)); 1653 if (attr) 1654 TREE_VALUE (attr) = chainon (data.tags, TREE_VALUE (attr)); 1655 else 1656 TYPE_ATTRIBUTES (t) 1657 = tree_cons (get_identifier ("abi_tag"), data.tags, 1658 TYPE_ATTRIBUTES (t)); 1659 } 1660 1661 mark_abi_tags (t, false); 1662 } 1663 1664 /* Return true, iff class T has a non-virtual destructor that is 1665 accessible from outside the class heirarchy (i.e. is public, or 1666 there's a suitable friend. */ 1667 1668 static bool 1669 accessible_nvdtor_p (tree t) 1670 { 1671 tree dtor = CLASSTYPE_DESTRUCTORS (t); 1672 1673 /* An implicitly declared destructor is always public. And, 1674 if it were virtual, we would have created it by now. */ 1675 if (!dtor) 1676 return true; 1677 1678 if (DECL_VINDEX (dtor)) 1679 return false; /* Virtual */ 1680 1681 if (!TREE_PRIVATE (dtor) && !TREE_PROTECTED (dtor)) 1682 return true; /* Public */ 1683 1684 if (CLASSTYPE_FRIEND_CLASSES (t) 1685 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t))) 1686 return true; /* Has friends */ 1687 1688 return false; 1689 } 1690 1691 /* Run through the base classes of T, updating CANT_HAVE_CONST_CTOR_P, 1692 and NO_CONST_ASN_REF_P. Also set flag bits in T based on 1693 properties of the bases. */ 1694 1695 static void 1696 check_bases (tree t, 1697 int* cant_have_const_ctor_p, 1698 int* no_const_asn_ref_p) 1699 { 1700 int i; 1701 bool seen_non_virtual_nearly_empty_base_p = 0; 1702 int seen_tm_mask = 0; 1703 tree base_binfo; 1704 tree binfo; 1705 tree field = NULL_TREE; 1706 1707 if (!CLASSTYPE_NON_STD_LAYOUT (t)) 1708 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field)) 1709 if (TREE_CODE (field) == FIELD_DECL) 1710 break; 1711 1712 for (binfo = TYPE_BINFO (t), i = 0; 1713 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++) 1714 { 1715 tree basetype = TREE_TYPE (base_binfo); 1716 1717 gcc_assert (COMPLETE_TYPE_P (basetype)); 1718 1719 if (CLASSTYPE_FINAL (basetype)) 1720 error ("cannot derive from %<final%> base %qT in derived type %qT", 1721 basetype, t); 1722 1723 /* If any base class is non-literal, so is the derived class. */ 1724 if (!CLASSTYPE_LITERAL_P (basetype)) 1725 CLASSTYPE_LITERAL_P (t) = false; 1726 1727 /* If the base class doesn't have copy constructors or 1728 assignment operators that take const references, then the 1729 derived class cannot have such a member automatically 1730 generated. */ 1731 if (TYPE_HAS_COPY_CTOR (basetype) 1732 && ! TYPE_HAS_CONST_COPY_CTOR (basetype)) 1733 *cant_have_const_ctor_p = 1; 1734 if (TYPE_HAS_COPY_ASSIGN (basetype) 1735 && !TYPE_HAS_CONST_COPY_ASSIGN (basetype)) 1736 *no_const_asn_ref_p = 1; 1737 1738 if (BINFO_VIRTUAL_P (base_binfo)) 1739 /* A virtual base does not effect nearly emptiness. */ 1740 ; 1741 else if (CLASSTYPE_NEARLY_EMPTY_P (basetype)) 1742 { 1743 if (seen_non_virtual_nearly_empty_base_p) 1744 /* And if there is more than one nearly empty base, then the 1745 derived class is not nearly empty either. */ 1746 CLASSTYPE_NEARLY_EMPTY_P (t) = 0; 1747 else 1748 /* Remember we've seen one. */ 1749 seen_non_virtual_nearly_empty_base_p = 1; 1750 } 1751 else if (!is_empty_class (basetype)) 1752 /* If the base class is not empty or nearly empty, then this 1753 class cannot be nearly empty. */ 1754 CLASSTYPE_NEARLY_EMPTY_P (t) = 0; 1755 1756 /* A lot of properties from the bases also apply to the derived 1757 class. */ 1758 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (basetype); 1759 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) 1760 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype); 1761 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) 1762 |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (basetype) 1763 || !TYPE_HAS_COPY_ASSIGN (basetype)); 1764 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= (TYPE_HAS_COMPLEX_COPY_CTOR (basetype) 1765 || !TYPE_HAS_COPY_CTOR (basetype)); 1766 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) 1767 |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (basetype); 1768 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_HAS_COMPLEX_MOVE_CTOR (basetype); 1769 TYPE_POLYMORPHIC_P (t) |= TYPE_POLYMORPHIC_P (basetype); 1770 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) 1771 |= CLASSTYPE_CONTAINS_EMPTY_CLASS_P (basetype); 1772 TYPE_HAS_COMPLEX_DFLT (t) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype) 1773 || TYPE_HAS_COMPLEX_DFLT (basetype)); 1774 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT 1775 (t, CLASSTYPE_READONLY_FIELDS_NEED_INIT (t) 1776 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (basetype)); 1777 SET_CLASSTYPE_REF_FIELDS_NEED_INIT 1778 (t, CLASSTYPE_REF_FIELDS_NEED_INIT (t) 1779 | CLASSTYPE_REF_FIELDS_NEED_INIT (basetype)); 1780 if (TYPE_HAS_MUTABLE_P (basetype)) 1781 CLASSTYPE_HAS_MUTABLE (t) = 1; 1782 1783 /* A standard-layout class is a class that: 1784 ... 1785 * has no non-standard-layout base classes, */ 1786 CLASSTYPE_NON_STD_LAYOUT (t) |= CLASSTYPE_NON_STD_LAYOUT (basetype); 1787 if (!CLASSTYPE_NON_STD_LAYOUT (t)) 1788 { 1789 tree basefield; 1790 /* ...has no base classes of the same type as the first non-static 1791 data member... */ 1792 if (field && DECL_CONTEXT (field) == t 1793 && (same_type_ignoring_top_level_qualifiers_p 1794 (TREE_TYPE (field), basetype))) 1795 CLASSTYPE_NON_STD_LAYOUT (t) = 1; 1796 else 1797 /* ...either has no non-static data members in the most-derived 1798 class and at most one base class with non-static data 1799 members, or has no base classes with non-static data 1800 members */ 1801 for (basefield = TYPE_FIELDS (basetype); basefield; 1802 basefield = DECL_CHAIN (basefield)) 1803 if (TREE_CODE (basefield) == FIELD_DECL) 1804 { 1805 if (field) 1806 CLASSTYPE_NON_STD_LAYOUT (t) = 1; 1807 else 1808 field = basefield; 1809 break; 1810 } 1811 } 1812 1813 /* Don't bother collecting tm attributes if transactional memory 1814 support is not enabled. */ 1815 if (flag_tm) 1816 { 1817 tree tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (basetype)); 1818 if (tm_attr) 1819 seen_tm_mask |= tm_attr_to_mask (tm_attr); 1820 } 1821 1822 check_abi_tags (t, basetype); 1823 } 1824 1825 /* If one of the base classes had TM attributes, and the current class 1826 doesn't define its own, then the current class inherits one. */ 1827 if (seen_tm_mask && !find_tm_attribute (TYPE_ATTRIBUTES (t))) 1828 { 1829 tree tm_attr = tm_mask_to_attr (seen_tm_mask & -seen_tm_mask); 1830 TYPE_ATTRIBUTES (t) = tree_cons (tm_attr, NULL, TYPE_ATTRIBUTES (t)); 1831 } 1832 } 1833 1834 /* Determine all the primary bases within T. Sets BINFO_PRIMARY_BASE_P for 1835 those that are primaries. Sets BINFO_LOST_PRIMARY_P for those 1836 that have had a nearly-empty virtual primary base stolen by some 1837 other base in the hierarchy. Determines CLASSTYPE_PRIMARY_BASE for 1838 T. */ 1839 1840 static void 1841 determine_primary_bases (tree t) 1842 { 1843 unsigned i; 1844 tree primary = NULL_TREE; 1845 tree type_binfo = TYPE_BINFO (t); 1846 tree base_binfo; 1847 1848 /* Determine the primary bases of our bases. */ 1849 for (base_binfo = TREE_CHAIN (type_binfo); base_binfo; 1850 base_binfo = TREE_CHAIN (base_binfo)) 1851 { 1852 tree primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (base_binfo)); 1853 1854 /* See if we're the non-virtual primary of our inheritance 1855 chain. */ 1856 if (!BINFO_VIRTUAL_P (base_binfo)) 1857 { 1858 tree parent = BINFO_INHERITANCE_CHAIN (base_binfo); 1859 tree parent_primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (parent)); 1860 1861 if (parent_primary 1862 && SAME_BINFO_TYPE_P (BINFO_TYPE (base_binfo), 1863 BINFO_TYPE (parent_primary))) 1864 /* We are the primary binfo. */ 1865 BINFO_PRIMARY_P (base_binfo) = 1; 1866 } 1867 /* Determine if we have a virtual primary base, and mark it so. 1868 */ 1869 if (primary && BINFO_VIRTUAL_P (primary)) 1870 { 1871 tree this_primary = copied_binfo (primary, base_binfo); 1872 1873 if (BINFO_PRIMARY_P (this_primary)) 1874 /* Someone already claimed this base. */ 1875 BINFO_LOST_PRIMARY_P (base_binfo) = 1; 1876 else 1877 { 1878 tree delta; 1879 1880 BINFO_PRIMARY_P (this_primary) = 1; 1881 BINFO_INHERITANCE_CHAIN (this_primary) = base_binfo; 1882 1883 /* A virtual binfo might have been copied from within 1884 another hierarchy. As we're about to use it as a 1885 primary base, make sure the offsets match. */ 1886 delta = size_diffop_loc (input_location, 1887 fold_convert (ssizetype, 1888 BINFO_OFFSET (base_binfo)), 1889 fold_convert (ssizetype, 1890 BINFO_OFFSET (this_primary))); 1891 1892 propagate_binfo_offsets (this_primary, delta); 1893 } 1894 } 1895 } 1896 1897 /* First look for a dynamic direct non-virtual base. */ 1898 for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, base_binfo); i++) 1899 { 1900 tree basetype = BINFO_TYPE (base_binfo); 1901 1902 if (TYPE_CONTAINS_VPTR_P (basetype) && !BINFO_VIRTUAL_P (base_binfo)) 1903 { 1904 primary = base_binfo; 1905 goto found; 1906 } 1907 } 1908 1909 /* A "nearly-empty" virtual base class can be the primary base 1910 class, if no non-virtual polymorphic base can be found. Look for 1911 a nearly-empty virtual dynamic base that is not already a primary 1912 base of something in the hierarchy. If there is no such base, 1913 just pick the first nearly-empty virtual base. */ 1914 1915 for (base_binfo = TREE_CHAIN (type_binfo); base_binfo; 1916 base_binfo = TREE_CHAIN (base_binfo)) 1917 if (BINFO_VIRTUAL_P (base_binfo) 1918 && CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (base_binfo))) 1919 { 1920 if (!BINFO_PRIMARY_P (base_binfo)) 1921 { 1922 /* Found one that is not primary. */ 1923 primary = base_binfo; 1924 goto found; 1925 } 1926 else if (!primary) 1927 /* Remember the first candidate. */ 1928 primary = base_binfo; 1929 } 1930 1931 found: 1932 /* If we've got a primary base, use it. */ 1933 if (primary) 1934 { 1935 tree basetype = BINFO_TYPE (primary); 1936 1937 CLASSTYPE_PRIMARY_BINFO (t) = primary; 1938 if (BINFO_PRIMARY_P (primary)) 1939 /* We are stealing a primary base. */ 1940 BINFO_LOST_PRIMARY_P (BINFO_INHERITANCE_CHAIN (primary)) = 1; 1941 BINFO_PRIMARY_P (primary) = 1; 1942 if (BINFO_VIRTUAL_P (primary)) 1943 { 1944 tree delta; 1945 1946 BINFO_INHERITANCE_CHAIN (primary) = type_binfo; 1947 /* A virtual binfo might have been copied from within 1948 another hierarchy. As we're about to use it as a primary 1949 base, make sure the offsets match. */ 1950 delta = size_diffop_loc (input_location, ssize_int (0), 1951 fold_convert (ssizetype, BINFO_OFFSET (primary))); 1952 1953 propagate_binfo_offsets (primary, delta); 1954 } 1955 1956 primary = TYPE_BINFO (basetype); 1957 1958 TYPE_VFIELD (t) = TYPE_VFIELD (basetype); 1959 BINFO_VTABLE (type_binfo) = BINFO_VTABLE (primary); 1960 BINFO_VIRTUALS (type_binfo) = BINFO_VIRTUALS (primary); 1961 } 1962 } 1963 1964 /* Update the variant types of T. */ 1965 1966 void 1967 fixup_type_variants (tree t) 1968 { 1969 tree variants; 1970 1971 if (!t) 1972 return; 1973 1974 for (variants = TYPE_NEXT_VARIANT (t); 1975 variants; 1976 variants = TYPE_NEXT_VARIANT (variants)) 1977 { 1978 /* These fields are in the _TYPE part of the node, not in 1979 the TYPE_LANG_SPECIFIC component, so they are not shared. */ 1980 TYPE_HAS_USER_CONSTRUCTOR (variants) = TYPE_HAS_USER_CONSTRUCTOR (t); 1981 TYPE_NEEDS_CONSTRUCTING (variants) = TYPE_NEEDS_CONSTRUCTING (t); 1982 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (variants) 1983 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t); 1984 1985 TYPE_POLYMORPHIC_P (variants) = TYPE_POLYMORPHIC_P (t); 1986 1987 TYPE_BINFO (variants) = TYPE_BINFO (t); 1988 1989 /* Copy whatever these are holding today. */ 1990 TYPE_VFIELD (variants) = TYPE_VFIELD (t); 1991 TYPE_FIELDS (variants) = TYPE_FIELDS (t); 1992 } 1993 } 1994 1995 /* KLASS is a class that we're applying may_alias to after the body is 1996 parsed. Fixup any POINTER_TO and REFERENCE_TO types. The 1997 canonical type(s) will be implicitly updated. */ 1998 1999 static void 2000 fixup_may_alias (tree klass) 2001 { 2002 tree t, v; 2003 2004 for (t = TYPE_POINTER_TO (klass); t; t = TYPE_NEXT_PTR_TO (t)) 2005 for (v = TYPE_MAIN_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v)) 2006 TYPE_REF_CAN_ALIAS_ALL (v) = true; 2007 for (t = TYPE_REFERENCE_TO (klass); t; t = TYPE_NEXT_REF_TO (t)) 2008 for (v = TYPE_MAIN_VARIANT (t); v; v = TYPE_NEXT_VARIANT (v)) 2009 TYPE_REF_CAN_ALIAS_ALL (v) = true; 2010 } 2011 2012 /* Early variant fixups: we apply attributes at the beginning of the class 2013 definition, and we need to fix up any variants that have already been 2014 made via elaborated-type-specifier so that check_qualified_type works. */ 2015 2016 void 2017 fixup_attribute_variants (tree t) 2018 { 2019 tree variants; 2020 2021 if (!t) 2022 return; 2023 2024 tree attrs = TYPE_ATTRIBUTES (t); 2025 unsigned align = TYPE_ALIGN (t); 2026 bool user_align = TYPE_USER_ALIGN (t); 2027 bool may_alias = lookup_attribute ("may_alias", attrs); 2028 2029 if (may_alias) 2030 fixup_may_alias (t); 2031 2032 for (variants = TYPE_NEXT_VARIANT (t); 2033 variants; 2034 variants = TYPE_NEXT_VARIANT (variants)) 2035 { 2036 /* These are the two fields that check_qualified_type looks at and 2037 are affected by attributes. */ 2038 TYPE_ATTRIBUTES (variants) = attrs; 2039 unsigned valign = align; 2040 if (TYPE_USER_ALIGN (variants)) 2041 valign = MAX (valign, TYPE_ALIGN (variants)); 2042 else 2043 TYPE_USER_ALIGN (variants) = user_align; 2044 TYPE_ALIGN (variants) = valign; 2045 if (may_alias) 2046 fixup_may_alias (variants); 2047 } 2048 } 2049 2050 /* Set memoizing fields and bits of T (and its variants) for later 2051 use. */ 2052 2053 static void 2054 finish_struct_bits (tree t) 2055 { 2056 /* Fix up variants (if any). */ 2057 fixup_type_variants (t); 2058 2059 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) && TYPE_POLYMORPHIC_P (t)) 2060 /* For a class w/o baseclasses, 'finish_struct' has set 2061 CLASSTYPE_PURE_VIRTUALS correctly (by definition). 2062 Similarly for a class whose base classes do not have vtables. 2063 When neither of these is true, we might have removed abstract 2064 virtuals (by providing a definition), added some (by declaring 2065 new ones), or redeclared ones from a base class. We need to 2066 recalculate what's really an abstract virtual at this point (by 2067 looking in the vtables). */ 2068 get_pure_virtuals (t); 2069 2070 /* If this type has a copy constructor or a destructor, force its 2071 mode to be BLKmode, and force its TREE_ADDRESSABLE bit to be 2072 nonzero. This will cause it to be passed by invisible reference 2073 and prevent it from being returned in a register. */ 2074 if (type_has_nontrivial_copy_init (t) 2075 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)) 2076 { 2077 tree variants; 2078 DECL_MODE (TYPE_MAIN_DECL (t)) = BLKmode; 2079 for (variants = t; variants; variants = TYPE_NEXT_VARIANT (variants)) 2080 { 2081 SET_TYPE_MODE (variants, BLKmode); 2082 TREE_ADDRESSABLE (variants) = 1; 2083 } 2084 } 2085 } 2086 2087 /* Issue warnings about T having private constructors, but no friends, 2088 and so forth. 2089 2090 HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or 2091 static members. HAS_NONPRIVATE_STATIC_FN is nonzero if T has any 2092 non-private static member functions. */ 2093 2094 static void 2095 maybe_warn_about_overly_private_class (tree t) 2096 { 2097 int has_member_fn = 0; 2098 int has_nonprivate_method = 0; 2099 tree fn; 2100 2101 if (!warn_ctor_dtor_privacy 2102 /* If the class has friends, those entities might create and 2103 access instances, so we should not warn. */ 2104 || (CLASSTYPE_FRIEND_CLASSES (t) 2105 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t))) 2106 /* We will have warned when the template was declared; there's 2107 no need to warn on every instantiation. */ 2108 || CLASSTYPE_TEMPLATE_INSTANTIATION (t)) 2109 /* There's no reason to even consider warning about this 2110 class. */ 2111 return; 2112 2113 /* We only issue one warning, if more than one applies, because 2114 otherwise, on code like: 2115 2116 class A { 2117 // Oops - forgot `public:' 2118 A(); 2119 A(const A&); 2120 ~A(); 2121 }; 2122 2123 we warn several times about essentially the same problem. */ 2124 2125 /* Check to see if all (non-constructor, non-destructor) member 2126 functions are private. (Since there are no friends or 2127 non-private statics, we can't ever call any of the private member 2128 functions.) */ 2129 for (fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn)) 2130 /* We're not interested in compiler-generated methods; they don't 2131 provide any way to call private members. */ 2132 if (!DECL_ARTIFICIAL (fn)) 2133 { 2134 if (!TREE_PRIVATE (fn)) 2135 { 2136 if (DECL_STATIC_FUNCTION_P (fn)) 2137 /* A non-private static member function is just like a 2138 friend; it can create and invoke private member 2139 functions, and be accessed without a class 2140 instance. */ 2141 return; 2142 2143 has_nonprivate_method = 1; 2144 /* Keep searching for a static member function. */ 2145 } 2146 else if (!DECL_CONSTRUCTOR_P (fn) && !DECL_DESTRUCTOR_P (fn)) 2147 has_member_fn = 1; 2148 } 2149 2150 if (!has_nonprivate_method && has_member_fn) 2151 { 2152 /* There are no non-private methods, and there's at least one 2153 private member function that isn't a constructor or 2154 destructor. (If all the private members are 2155 constructors/destructors we want to use the code below that 2156 issues error messages specifically referring to 2157 constructors/destructors.) */ 2158 unsigned i; 2159 tree binfo = TYPE_BINFO (t); 2160 2161 for (i = 0; i != BINFO_N_BASE_BINFOS (binfo); i++) 2162 if (BINFO_BASE_ACCESS (binfo, i) != access_private_node) 2163 { 2164 has_nonprivate_method = 1; 2165 break; 2166 } 2167 if (!has_nonprivate_method) 2168 { 2169 warning (OPT_Wctor_dtor_privacy, 2170 "all member functions in class %qT are private", t); 2171 return; 2172 } 2173 } 2174 2175 /* Even if some of the member functions are non-private, the class 2176 won't be useful for much if all the constructors or destructors 2177 are private: such an object can never be created or destroyed. */ 2178 fn = CLASSTYPE_DESTRUCTORS (t); 2179 if (fn && TREE_PRIVATE (fn)) 2180 { 2181 warning (OPT_Wctor_dtor_privacy, 2182 "%q#T only defines a private destructor and has no friends", 2183 t); 2184 return; 2185 } 2186 2187 /* Warn about classes that have private constructors and no friends. */ 2188 if (TYPE_HAS_USER_CONSTRUCTOR (t) 2189 /* Implicitly generated constructors are always public. */ 2190 && (!CLASSTYPE_LAZY_DEFAULT_CTOR (t) 2191 || !CLASSTYPE_LAZY_COPY_CTOR (t))) 2192 { 2193 int nonprivate_ctor = 0; 2194 2195 /* If a non-template class does not define a copy 2196 constructor, one is defined for it, enabling it to avoid 2197 this warning. For a template class, this does not 2198 happen, and so we would normally get a warning on: 2199 2200 template <class T> class C { private: C(); }; 2201 2202 To avoid this asymmetry, we check TYPE_HAS_COPY_CTOR. All 2203 complete non-template or fully instantiated classes have this 2204 flag set. */ 2205 if (!TYPE_HAS_COPY_CTOR (t)) 2206 nonprivate_ctor = 1; 2207 else 2208 for (fn = CLASSTYPE_CONSTRUCTORS (t); fn; fn = OVL_NEXT (fn)) 2209 { 2210 tree ctor = OVL_CURRENT (fn); 2211 /* Ideally, we wouldn't count copy constructors (or, in 2212 fact, any constructor that takes an argument of the 2213 class type as a parameter) because such things cannot 2214 be used to construct an instance of the class unless 2215 you already have one. But, for now at least, we're 2216 more generous. */ 2217 if (! TREE_PRIVATE (ctor)) 2218 { 2219 nonprivate_ctor = 1; 2220 break; 2221 } 2222 } 2223 2224 if (nonprivate_ctor == 0) 2225 { 2226 warning (OPT_Wctor_dtor_privacy, 2227 "%q#T only defines private constructors and has no friends", 2228 t); 2229 return; 2230 } 2231 } 2232 } 2233 2234 static struct { 2235 gt_pointer_operator new_value; 2236 void *cookie; 2237 } resort_data; 2238 2239 /* Comparison function to compare two TYPE_METHOD_VEC entries by name. */ 2240 2241 static int 2242 method_name_cmp (const void* m1_p, const void* m2_p) 2243 { 2244 const tree *const m1 = (const tree *) m1_p; 2245 const tree *const m2 = (const tree *) m2_p; 2246 2247 if (*m1 == NULL_TREE && *m2 == NULL_TREE) 2248 return 0; 2249 if (*m1 == NULL_TREE) 2250 return -1; 2251 if (*m2 == NULL_TREE) 2252 return 1; 2253 if (DECL_NAME (OVL_CURRENT (*m1)) < DECL_NAME (OVL_CURRENT (*m2))) 2254 return -1; 2255 return 1; 2256 } 2257 2258 /* This routine compares two fields like method_name_cmp but using the 2259 pointer operator in resort_field_decl_data. */ 2260 2261 static int 2262 resort_method_name_cmp (const void* m1_p, const void* m2_p) 2263 { 2264 const tree *const m1 = (const tree *) m1_p; 2265 const tree *const m2 = (const tree *) m2_p; 2266 if (*m1 == NULL_TREE && *m2 == NULL_TREE) 2267 return 0; 2268 if (*m1 == NULL_TREE) 2269 return -1; 2270 if (*m2 == NULL_TREE) 2271 return 1; 2272 { 2273 tree d1 = DECL_NAME (OVL_CURRENT (*m1)); 2274 tree d2 = DECL_NAME (OVL_CURRENT (*m2)); 2275 resort_data.new_value (&d1, resort_data.cookie); 2276 resort_data.new_value (&d2, resort_data.cookie); 2277 if (d1 < d2) 2278 return -1; 2279 } 2280 return 1; 2281 } 2282 2283 /* Resort TYPE_METHOD_VEC because pointers have been reordered. */ 2284 2285 void 2286 resort_type_method_vec (void* obj, 2287 void* /*orig_obj*/, 2288 gt_pointer_operator new_value, 2289 void* cookie) 2290 { 2291 vec<tree, va_gc> *method_vec = (vec<tree, va_gc> *) obj; 2292 int len = vec_safe_length (method_vec); 2293 size_t slot; 2294 tree fn; 2295 2296 /* The type conversion ops have to live at the front of the vec, so we 2297 can't sort them. */ 2298 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT; 2299 vec_safe_iterate (method_vec, slot, &fn); 2300 ++slot) 2301 if (!DECL_CONV_FN_P (OVL_CURRENT (fn))) 2302 break; 2303 2304 if (len - slot > 1) 2305 { 2306 resort_data.new_value = new_value; 2307 resort_data.cookie = cookie; 2308 qsort (method_vec->address () + slot, len - slot, sizeof (tree), 2309 resort_method_name_cmp); 2310 } 2311 } 2312 2313 /* Warn about duplicate methods in fn_fields. 2314 2315 Sort methods that are not special (i.e., constructors, destructors, 2316 and type conversion operators) so that we can find them faster in 2317 search. */ 2318 2319 static void 2320 finish_struct_methods (tree t) 2321 { 2322 tree fn_fields; 2323 vec<tree, va_gc> *method_vec; 2324 int slot, len; 2325 2326 method_vec = CLASSTYPE_METHOD_VEC (t); 2327 if (!method_vec) 2328 return; 2329 2330 len = method_vec->length (); 2331 2332 /* Clear DECL_IN_AGGR_P for all functions. */ 2333 for (fn_fields = TYPE_METHODS (t); fn_fields; 2334 fn_fields = DECL_CHAIN (fn_fields)) 2335 DECL_IN_AGGR_P (fn_fields) = 0; 2336 2337 /* Issue warnings about private constructors and such. If there are 2338 no methods, then some public defaults are generated. */ 2339 maybe_warn_about_overly_private_class (t); 2340 2341 /* The type conversion ops have to live at the front of the vec, so we 2342 can't sort them. */ 2343 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT; 2344 method_vec->iterate (slot, &fn_fields); 2345 ++slot) 2346 if (!DECL_CONV_FN_P (OVL_CURRENT (fn_fields))) 2347 break; 2348 if (len - slot > 1) 2349 qsort (method_vec->address () + slot, 2350 len-slot, sizeof (tree), method_name_cmp); 2351 } 2352 2353 /* Make BINFO's vtable have N entries, including RTTI entries, 2354 vbase and vcall offsets, etc. Set its type and call the back end 2355 to lay it out. */ 2356 2357 static void 2358 layout_vtable_decl (tree binfo, int n) 2359 { 2360 tree atype; 2361 tree vtable; 2362 2363 atype = build_array_of_n_type (vtable_entry_type, n); 2364 layout_type (atype); 2365 2366 /* We may have to grow the vtable. */ 2367 vtable = get_vtbl_decl_for_binfo (binfo); 2368 if (!same_type_p (TREE_TYPE (vtable), atype)) 2369 { 2370 TREE_TYPE (vtable) = atype; 2371 DECL_SIZE (vtable) = DECL_SIZE_UNIT (vtable) = NULL_TREE; 2372 layout_decl (vtable, 0); 2373 } 2374 } 2375 2376 /* True iff FNDECL and BASE_FNDECL (both non-static member functions) 2377 have the same signature. */ 2378 2379 int 2380 same_signature_p (const_tree fndecl, const_tree base_fndecl) 2381 { 2382 /* One destructor overrides another if they are the same kind of 2383 destructor. */ 2384 if (DECL_DESTRUCTOR_P (base_fndecl) && DECL_DESTRUCTOR_P (fndecl) 2385 && special_function_p (base_fndecl) == special_function_p (fndecl)) 2386 return 1; 2387 /* But a non-destructor never overrides a destructor, nor vice 2388 versa, nor do different kinds of destructors override 2389 one-another. For example, a complete object destructor does not 2390 override a deleting destructor. */ 2391 if (DECL_DESTRUCTOR_P (base_fndecl) || DECL_DESTRUCTOR_P (fndecl)) 2392 return 0; 2393 2394 if (DECL_NAME (fndecl) == DECL_NAME (base_fndecl) 2395 || (DECL_CONV_FN_P (fndecl) 2396 && DECL_CONV_FN_P (base_fndecl) 2397 && same_type_p (DECL_CONV_FN_TYPE (fndecl), 2398 DECL_CONV_FN_TYPE (base_fndecl)))) 2399 { 2400 tree fntype = TREE_TYPE (fndecl); 2401 tree base_fntype = TREE_TYPE (base_fndecl); 2402 if (type_memfn_quals (fntype) == type_memfn_quals (base_fntype) 2403 && type_memfn_rqual (fntype) == type_memfn_rqual (base_fntype) 2404 && compparms (FUNCTION_FIRST_USER_PARMTYPE (fndecl), 2405 FUNCTION_FIRST_USER_PARMTYPE (base_fndecl))) 2406 return 1; 2407 } 2408 return 0; 2409 } 2410 2411 /* Returns TRUE if DERIVED is a binfo containing the binfo BASE as a 2412 subobject. */ 2413 2414 static bool 2415 base_derived_from (tree derived, tree base) 2416 { 2417 tree probe; 2418 2419 for (probe = base; probe; probe = BINFO_INHERITANCE_CHAIN (probe)) 2420 { 2421 if (probe == derived) 2422 return true; 2423 else if (BINFO_VIRTUAL_P (probe)) 2424 /* If we meet a virtual base, we can't follow the inheritance 2425 any more. See if the complete type of DERIVED contains 2426 such a virtual base. */ 2427 return (binfo_for_vbase (BINFO_TYPE (probe), BINFO_TYPE (derived)) 2428 != NULL_TREE); 2429 } 2430 return false; 2431 } 2432 2433 struct find_final_overrider_data { 2434 /* The function for which we are trying to find a final overrider. */ 2435 tree fn; 2436 /* The base class in which the function was declared. */ 2437 tree declaring_base; 2438 /* The candidate overriders. */ 2439 tree candidates; 2440 /* Path to most derived. */ 2441 vec<tree> path; 2442 }; 2443 2444 /* Add the overrider along the current path to FFOD->CANDIDATES. 2445 Returns true if an overrider was found; false otherwise. */ 2446 2447 static bool 2448 dfs_find_final_overrider_1 (tree binfo, 2449 find_final_overrider_data *ffod, 2450 unsigned depth) 2451 { 2452 tree method; 2453 2454 /* If BINFO is not the most derived type, try a more derived class. 2455 A definition there will overrider a definition here. */ 2456 if (depth) 2457 { 2458 depth--; 2459 if (dfs_find_final_overrider_1 2460 (ffod->path[depth], ffod, depth)) 2461 return true; 2462 } 2463 2464 method = look_for_overrides_here (BINFO_TYPE (binfo), ffod->fn); 2465 if (method) 2466 { 2467 tree *candidate = &ffod->candidates; 2468 2469 /* Remove any candidates overridden by this new function. */ 2470 while (*candidate) 2471 { 2472 /* If *CANDIDATE overrides METHOD, then METHOD 2473 cannot override anything else on the list. */ 2474 if (base_derived_from (TREE_VALUE (*candidate), binfo)) 2475 return true; 2476 /* If METHOD overrides *CANDIDATE, remove *CANDIDATE. */ 2477 if (base_derived_from (binfo, TREE_VALUE (*candidate))) 2478 *candidate = TREE_CHAIN (*candidate); 2479 else 2480 candidate = &TREE_CHAIN (*candidate); 2481 } 2482 2483 /* Add the new function. */ 2484 ffod->candidates = tree_cons (method, binfo, ffod->candidates); 2485 return true; 2486 } 2487 2488 return false; 2489 } 2490 2491 /* Called from find_final_overrider via dfs_walk. */ 2492 2493 static tree 2494 dfs_find_final_overrider_pre (tree binfo, void *data) 2495 { 2496 find_final_overrider_data *ffod = (find_final_overrider_data *) data; 2497 2498 if (binfo == ffod->declaring_base) 2499 dfs_find_final_overrider_1 (binfo, ffod, ffod->path.length ()); 2500 ffod->path.safe_push (binfo); 2501 2502 return NULL_TREE; 2503 } 2504 2505 static tree 2506 dfs_find_final_overrider_post (tree /*binfo*/, void *data) 2507 { 2508 find_final_overrider_data *ffod = (find_final_overrider_data *) data; 2509 ffod->path.pop (); 2510 2511 return NULL_TREE; 2512 } 2513 2514 /* Returns a TREE_LIST whose TREE_PURPOSE is the final overrider for 2515 FN and whose TREE_VALUE is the binfo for the base where the 2516 overriding occurs. BINFO (in the hierarchy dominated by the binfo 2517 DERIVED) is the base object in which FN is declared. */ 2518 2519 static tree 2520 find_final_overrider (tree derived, tree binfo, tree fn) 2521 { 2522 find_final_overrider_data ffod; 2523 2524 /* Getting this right is a little tricky. This is valid: 2525 2526 struct S { virtual void f (); }; 2527 struct T { virtual void f (); }; 2528 struct U : public S, public T { }; 2529 2530 even though calling `f' in `U' is ambiguous. But, 2531 2532 struct R { virtual void f(); }; 2533 struct S : virtual public R { virtual void f (); }; 2534 struct T : virtual public R { virtual void f (); }; 2535 struct U : public S, public T { }; 2536 2537 is not -- there's no way to decide whether to put `S::f' or 2538 `T::f' in the vtable for `R'. 2539 2540 The solution is to look at all paths to BINFO. If we find 2541 different overriders along any two, then there is a problem. */ 2542 if (DECL_THUNK_P (fn)) 2543 fn = THUNK_TARGET (fn); 2544 2545 /* Determine the depth of the hierarchy. */ 2546 ffod.fn = fn; 2547 ffod.declaring_base = binfo; 2548 ffod.candidates = NULL_TREE; 2549 ffod.path.create (30); 2550 2551 dfs_walk_all (derived, dfs_find_final_overrider_pre, 2552 dfs_find_final_overrider_post, &ffod); 2553 2554 ffod.path.release (); 2555 2556 /* If there was no winner, issue an error message. */ 2557 if (!ffod.candidates || TREE_CHAIN (ffod.candidates)) 2558 return error_mark_node; 2559 2560 return ffod.candidates; 2561 } 2562 2563 /* Return the index of the vcall offset for FN when TYPE is used as a 2564 virtual base. */ 2565 2566 static tree 2567 get_vcall_index (tree fn, tree type) 2568 { 2569 vec<tree_pair_s, va_gc> *indices = CLASSTYPE_VCALL_INDICES (type); 2570 tree_pair_p p; 2571 unsigned ix; 2572 2573 FOR_EACH_VEC_SAFE_ELT (indices, ix, p) 2574 if ((DECL_DESTRUCTOR_P (fn) && DECL_DESTRUCTOR_P (p->purpose)) 2575 || same_signature_p (fn, p->purpose)) 2576 return p->value; 2577 2578 /* There should always be an appropriate index. */ 2579 gcc_unreachable (); 2580 } 2581 2582 /* Update an entry in the vtable for BINFO, which is in the hierarchy 2583 dominated by T. FN is the old function; VIRTUALS points to the 2584 corresponding position in the new BINFO_VIRTUALS list. IX is the index 2585 of that entry in the list. */ 2586 2587 static void 2588 update_vtable_entry_for_fn (tree t, tree binfo, tree fn, tree* virtuals, 2589 unsigned ix) 2590 { 2591 tree b; 2592 tree overrider; 2593 tree delta; 2594 tree virtual_base; 2595 tree first_defn; 2596 tree overrider_fn, overrider_target; 2597 tree target_fn = DECL_THUNK_P (fn) ? THUNK_TARGET (fn) : fn; 2598 tree over_return, base_return; 2599 bool lost = false; 2600 2601 /* Find the nearest primary base (possibly binfo itself) which defines 2602 this function; this is the class the caller will convert to when 2603 calling FN through BINFO. */ 2604 for (b = binfo; ; b = get_primary_binfo (b)) 2605 { 2606 gcc_assert (b); 2607 if (look_for_overrides_here (BINFO_TYPE (b), target_fn)) 2608 break; 2609 2610 /* The nearest definition is from a lost primary. */ 2611 if (BINFO_LOST_PRIMARY_P (b)) 2612 lost = true; 2613 } 2614 first_defn = b; 2615 2616 /* Find the final overrider. */ 2617 overrider = find_final_overrider (TYPE_BINFO (t), b, target_fn); 2618 if (overrider == error_mark_node) 2619 { 2620 error ("no unique final overrider for %qD in %qT", target_fn, t); 2621 return; 2622 } 2623 overrider_target = overrider_fn = TREE_PURPOSE (overrider); 2624 2625 /* Check for adjusting covariant return types. */ 2626 over_return = TREE_TYPE (TREE_TYPE (overrider_target)); 2627 base_return = TREE_TYPE (TREE_TYPE (target_fn)); 2628 2629 if (POINTER_TYPE_P (over_return) 2630 && TREE_CODE (over_return) == TREE_CODE (base_return) 2631 && CLASS_TYPE_P (TREE_TYPE (over_return)) 2632 && CLASS_TYPE_P (TREE_TYPE (base_return)) 2633 /* If the overrider is invalid, don't even try. */ 2634 && !DECL_INVALID_OVERRIDER_P (overrider_target)) 2635 { 2636 /* If FN is a covariant thunk, we must figure out the adjustment 2637 to the final base FN was converting to. As OVERRIDER_TARGET might 2638 also be converting to the return type of FN, we have to 2639 combine the two conversions here. */ 2640 tree fixed_offset, virtual_offset; 2641 2642 over_return = TREE_TYPE (over_return); 2643 base_return = TREE_TYPE (base_return); 2644 2645 if (DECL_THUNK_P (fn)) 2646 { 2647 gcc_assert (DECL_RESULT_THUNK_P (fn)); 2648 fixed_offset = ssize_int (THUNK_FIXED_OFFSET (fn)); 2649 virtual_offset = THUNK_VIRTUAL_OFFSET (fn); 2650 } 2651 else 2652 fixed_offset = virtual_offset = NULL_TREE; 2653 2654 if (virtual_offset) 2655 /* Find the equivalent binfo within the return type of the 2656 overriding function. We will want the vbase offset from 2657 there. */ 2658 virtual_offset = binfo_for_vbase (BINFO_TYPE (virtual_offset), 2659 over_return); 2660 else if (!same_type_ignoring_top_level_qualifiers_p 2661 (over_return, base_return)) 2662 { 2663 /* There was no existing virtual thunk (which takes 2664 precedence). So find the binfo of the base function's 2665 return type within the overriding function's return type. 2666 Fortunately we know the covariancy is valid (it 2667 has already been checked), so we can just iterate along 2668 the binfos, which have been chained in inheritance graph 2669 order. Of course it is lame that we have to repeat the 2670 search here anyway -- we should really be caching pieces 2671 of the vtable and avoiding this repeated work. */ 2672 tree thunk_binfo = NULL_TREE; 2673 tree base_binfo = TYPE_BINFO (base_return); 2674 2675 /* Find the base binfo within the overriding function's 2676 return type. We will always find a thunk_binfo, except 2677 when the covariancy is invalid (which we will have 2678 already diagnosed). */ 2679 if (base_binfo) 2680 for (thunk_binfo = TYPE_BINFO (over_return); thunk_binfo; 2681 thunk_binfo = TREE_CHAIN (thunk_binfo)) 2682 if (SAME_BINFO_TYPE_P (BINFO_TYPE (thunk_binfo), 2683 BINFO_TYPE (base_binfo))) 2684 break; 2685 gcc_assert (thunk_binfo || errorcount); 2686 2687 /* See if virtual inheritance is involved. */ 2688 for (virtual_offset = thunk_binfo; 2689 virtual_offset; 2690 virtual_offset = BINFO_INHERITANCE_CHAIN (virtual_offset)) 2691 if (BINFO_VIRTUAL_P (virtual_offset)) 2692 break; 2693 2694 if (virtual_offset 2695 || (thunk_binfo && !BINFO_OFFSET_ZEROP (thunk_binfo))) 2696 { 2697 tree offset = fold_convert (ssizetype, BINFO_OFFSET (thunk_binfo)); 2698 2699 if (virtual_offset) 2700 { 2701 /* We convert via virtual base. Adjust the fixed 2702 offset to be from there. */ 2703 offset = 2704 size_diffop (offset, 2705 fold_convert (ssizetype, 2706 BINFO_OFFSET (virtual_offset))); 2707 } 2708 if (fixed_offset) 2709 /* There was an existing fixed offset, this must be 2710 from the base just converted to, and the base the 2711 FN was thunking to. */ 2712 fixed_offset = size_binop (PLUS_EXPR, fixed_offset, offset); 2713 else 2714 fixed_offset = offset; 2715 } 2716 } 2717 2718 if (fixed_offset || virtual_offset) 2719 /* Replace the overriding function with a covariant thunk. We 2720 will emit the overriding function in its own slot as 2721 well. */ 2722 overrider_fn = make_thunk (overrider_target, /*this_adjusting=*/0, 2723 fixed_offset, virtual_offset); 2724 } 2725 else 2726 gcc_assert (DECL_INVALID_OVERRIDER_P (overrider_target) || 2727 !DECL_THUNK_P (fn)); 2728 2729 /* If we need a covariant thunk, then we may need to adjust first_defn. 2730 The ABI specifies that the thunks emitted with a function are 2731 determined by which bases the function overrides, so we need to be 2732 sure that we're using a thunk for some overridden base; even if we 2733 know that the necessary this adjustment is zero, there may not be an 2734 appropriate zero-this-adjusment thunk for us to use since thunks for 2735 overriding virtual bases always use the vcall offset. 2736 2737 Furthermore, just choosing any base that overrides this function isn't 2738 quite right, as this slot won't be used for calls through a type that 2739 puts a covariant thunk here. Calling the function through such a type 2740 will use a different slot, and that slot is the one that determines 2741 the thunk emitted for that base. 2742 2743 So, keep looking until we find the base that we're really overriding 2744 in this slot: the nearest primary base that doesn't use a covariant 2745 thunk in this slot. */ 2746 if (overrider_target != overrider_fn) 2747 { 2748 if (BINFO_TYPE (b) == DECL_CONTEXT (overrider_target)) 2749 /* We already know that the overrider needs a covariant thunk. */ 2750 b = get_primary_binfo (b); 2751 for (; ; b = get_primary_binfo (b)) 2752 { 2753 tree main_binfo = TYPE_BINFO (BINFO_TYPE (b)); 2754 tree bv = chain_index (ix, BINFO_VIRTUALS (main_binfo)); 2755 if (!DECL_THUNK_P (TREE_VALUE (bv))) 2756 break; 2757 if (BINFO_LOST_PRIMARY_P (b)) 2758 lost = true; 2759 } 2760 first_defn = b; 2761 } 2762 2763 /* Assume that we will produce a thunk that convert all the way to 2764 the final overrider, and not to an intermediate virtual base. */ 2765 virtual_base = NULL_TREE; 2766 2767 /* See if we can convert to an intermediate virtual base first, and then 2768 use the vcall offset located there to finish the conversion. */ 2769 for (; b; b = BINFO_INHERITANCE_CHAIN (b)) 2770 { 2771 /* If we find the final overrider, then we can stop 2772 walking. */ 2773 if (SAME_BINFO_TYPE_P (BINFO_TYPE (b), 2774 BINFO_TYPE (TREE_VALUE (overrider)))) 2775 break; 2776 2777 /* If we find a virtual base, and we haven't yet found the 2778 overrider, then there is a virtual base between the 2779 declaring base (first_defn) and the final overrider. */ 2780 if (BINFO_VIRTUAL_P (b)) 2781 { 2782 virtual_base = b; 2783 break; 2784 } 2785 } 2786 2787 /* Compute the constant adjustment to the `this' pointer. The 2788 `this' pointer, when this function is called, will point at BINFO 2789 (or one of its primary bases, which are at the same offset). */ 2790 if (virtual_base) 2791 /* The `this' pointer needs to be adjusted from the declaration to 2792 the nearest virtual base. */ 2793 delta = size_diffop_loc (input_location, 2794 fold_convert (ssizetype, BINFO_OFFSET (virtual_base)), 2795 fold_convert (ssizetype, BINFO_OFFSET (first_defn))); 2796 else if (lost) 2797 /* If the nearest definition is in a lost primary, we don't need an 2798 entry in our vtable. Except possibly in a constructor vtable, 2799 if we happen to get our primary back. In that case, the offset 2800 will be zero, as it will be a primary base. */ 2801 delta = size_zero_node; 2802 else 2803 /* The `this' pointer needs to be adjusted from pointing to 2804 BINFO to pointing at the base where the final overrider 2805 appears. */ 2806 delta = size_diffop_loc (input_location, 2807 fold_convert (ssizetype, 2808 BINFO_OFFSET (TREE_VALUE (overrider))), 2809 fold_convert (ssizetype, BINFO_OFFSET (binfo))); 2810 2811 modify_vtable_entry (t, binfo, overrider_fn, delta, virtuals); 2812 2813 if (virtual_base) 2814 BV_VCALL_INDEX (*virtuals) 2815 = get_vcall_index (overrider_target, BINFO_TYPE (virtual_base)); 2816 else 2817 BV_VCALL_INDEX (*virtuals) = NULL_TREE; 2818 2819 BV_LOST_PRIMARY (*virtuals) = lost; 2820 } 2821 2822 /* Called from modify_all_vtables via dfs_walk. */ 2823 2824 static tree 2825 dfs_modify_vtables (tree binfo, void* data) 2826 { 2827 tree t = (tree) data; 2828 tree virtuals; 2829 tree old_virtuals; 2830 unsigned ix; 2831 2832 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo))) 2833 /* A base without a vtable needs no modification, and its bases 2834 are uninteresting. */ 2835 return dfs_skip_bases; 2836 2837 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t) 2838 && !CLASSTYPE_HAS_PRIMARY_BASE_P (t)) 2839 /* Don't do the primary vtable, if it's new. */ 2840 return NULL_TREE; 2841 2842 if (BINFO_PRIMARY_P (binfo) && !BINFO_VIRTUAL_P (binfo)) 2843 /* There's no need to modify the vtable for a non-virtual primary 2844 base; we're not going to use that vtable anyhow. We do still 2845 need to do this for virtual primary bases, as they could become 2846 non-primary in a construction vtable. */ 2847 return NULL_TREE; 2848 2849 make_new_vtable (t, binfo); 2850 2851 /* Now, go through each of the virtual functions in the virtual 2852 function table for BINFO. Find the final overrider, and update 2853 the BINFO_VIRTUALS list appropriately. */ 2854 for (ix = 0, virtuals = BINFO_VIRTUALS (binfo), 2855 old_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo))); 2856 virtuals; 2857 ix++, virtuals = TREE_CHAIN (virtuals), 2858 old_virtuals = TREE_CHAIN (old_virtuals)) 2859 update_vtable_entry_for_fn (t, 2860 binfo, 2861 BV_FN (old_virtuals), 2862 &virtuals, ix); 2863 2864 return NULL_TREE; 2865 } 2866 2867 /* Update all of the primary and secondary vtables for T. Create new 2868 vtables as required, and initialize their RTTI information. Each 2869 of the functions in VIRTUALS is declared in T and may override a 2870 virtual function from a base class; find and modify the appropriate 2871 entries to point to the overriding functions. Returns a list, in 2872 declaration order, of the virtual functions that are declared in T, 2873 but do not appear in the primary base class vtable, and which 2874 should therefore be appended to the end of the vtable for T. */ 2875 2876 static tree 2877 modify_all_vtables (tree t, tree virtuals) 2878 { 2879 tree binfo = TYPE_BINFO (t); 2880 tree *fnsp; 2881 2882 /* Mangle the vtable name before entering dfs_walk (c++/51884). */ 2883 if (TYPE_CONTAINS_VPTR_P (t)) 2884 get_vtable_decl (t, false); 2885 2886 /* Update all of the vtables. */ 2887 dfs_walk_once (binfo, dfs_modify_vtables, NULL, t); 2888 2889 /* Add virtual functions not already in our primary vtable. These 2890 will be both those introduced by this class, and those overridden 2891 from secondary bases. It does not include virtuals merely 2892 inherited from secondary bases. */ 2893 for (fnsp = &virtuals; *fnsp; ) 2894 { 2895 tree fn = TREE_VALUE (*fnsp); 2896 2897 if (!value_member (fn, BINFO_VIRTUALS (binfo)) 2898 || DECL_VINDEX (fn) == error_mark_node) 2899 { 2900 /* We don't need to adjust the `this' pointer when 2901 calling this function. */ 2902 BV_DELTA (*fnsp) = integer_zero_node; 2903 BV_VCALL_INDEX (*fnsp) = NULL_TREE; 2904 2905 /* This is a function not already in our vtable. Keep it. */ 2906 fnsp = &TREE_CHAIN (*fnsp); 2907 } 2908 else 2909 /* We've already got an entry for this function. Skip it. */ 2910 *fnsp = TREE_CHAIN (*fnsp); 2911 } 2912 2913 return virtuals; 2914 } 2915 2916 /* Get the base virtual function declarations in T that have the 2917 indicated NAME. */ 2918 2919 static void 2920 get_basefndecls (tree name, tree t, vec<tree> *base_fndecls) 2921 { 2922 tree methods; 2923 int n_baseclasses = BINFO_N_BASE_BINFOS (TYPE_BINFO (t)); 2924 int i; 2925 2926 /* Find virtual functions in T with the indicated NAME. */ 2927 i = lookup_fnfields_1 (t, name); 2928 bool found_decls = false; 2929 if (i != -1) 2930 for (methods = (*CLASSTYPE_METHOD_VEC (t))[i]; 2931 methods; 2932 methods = OVL_NEXT (methods)) 2933 { 2934 tree method = OVL_CURRENT (methods); 2935 2936 if (TREE_CODE (method) == FUNCTION_DECL 2937 && DECL_VINDEX (method)) 2938 { 2939 base_fndecls->safe_push (method); 2940 found_decls = true; 2941 } 2942 } 2943 2944 if (found_decls) 2945 return; 2946 2947 for (i = 0; i < n_baseclasses; i++) 2948 { 2949 tree basetype = BINFO_TYPE (BINFO_BASE_BINFO (TYPE_BINFO (t), i)); 2950 get_basefndecls (name, basetype, base_fndecls); 2951 } 2952 } 2953 2954 /* If this declaration supersedes the declaration of 2955 a method declared virtual in the base class, then 2956 mark this field as being virtual as well. */ 2957 2958 void 2959 check_for_override (tree decl, tree ctype) 2960 { 2961 bool overrides_found = false; 2962 if (TREE_CODE (decl) == TEMPLATE_DECL) 2963 /* In [temp.mem] we have: 2964 2965 A specialization of a member function template does not 2966 override a virtual function from a base class. */ 2967 return; 2968 if ((DECL_DESTRUCTOR_P (decl) 2969 || IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) 2970 || DECL_CONV_FN_P (decl)) 2971 && look_for_overrides (ctype, decl) 2972 && !DECL_STATIC_FUNCTION_P (decl)) 2973 /* Set DECL_VINDEX to a value that is neither an INTEGER_CST nor 2974 the error_mark_node so that we know it is an overriding 2975 function. */ 2976 { 2977 DECL_VINDEX (decl) = decl; 2978 overrides_found = true; 2979 if (warn_override && !DECL_OVERRIDE_P (decl) 2980 && !DECL_DESTRUCTOR_P (decl)) 2981 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wsuggest_override, 2982 "%qD can be marked override", decl); 2983 } 2984 2985 if (DECL_VIRTUAL_P (decl)) 2986 { 2987 if (!DECL_VINDEX (decl)) 2988 DECL_VINDEX (decl) = error_mark_node; 2989 IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = 1; 2990 if (DECL_DESTRUCTOR_P (decl)) 2991 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype) = true; 2992 } 2993 else if (DECL_FINAL_P (decl)) 2994 error ("%q+#D marked %<final%>, but is not virtual", decl); 2995 if (DECL_OVERRIDE_P (decl) && !overrides_found) 2996 error ("%q+#D marked %<override%>, but does not override", decl); 2997 } 2998 2999 /* Warn about hidden virtual functions that are not overridden in t. 3000 We know that constructors and destructors don't apply. */ 3001 3002 static void 3003 warn_hidden (tree t) 3004 { 3005 vec<tree, va_gc> *method_vec = CLASSTYPE_METHOD_VEC (t); 3006 tree fns; 3007 size_t i; 3008 3009 /* We go through each separately named virtual function. */ 3010 for (i = CLASSTYPE_FIRST_CONVERSION_SLOT; 3011 vec_safe_iterate (method_vec, i, &fns); 3012 ++i) 3013 { 3014 tree fn; 3015 tree name; 3016 tree fndecl; 3017 tree base_binfo; 3018 tree binfo; 3019 int j; 3020 3021 /* All functions in this slot in the CLASSTYPE_METHOD_VEC will 3022 have the same name. Figure out what name that is. */ 3023 name = DECL_NAME (OVL_CURRENT (fns)); 3024 /* There are no possibly hidden functions yet. */ 3025 auto_vec<tree, 20> base_fndecls; 3026 /* Iterate through all of the base classes looking for possibly 3027 hidden functions. */ 3028 for (binfo = TYPE_BINFO (t), j = 0; 3029 BINFO_BASE_ITERATE (binfo, j, base_binfo); j++) 3030 { 3031 tree basetype = BINFO_TYPE (base_binfo); 3032 get_basefndecls (name, basetype, &base_fndecls); 3033 } 3034 3035 /* If there are no functions to hide, continue. */ 3036 if (base_fndecls.is_empty ()) 3037 continue; 3038 3039 /* Remove any overridden functions. */ 3040 for (fn = fns; fn; fn = OVL_NEXT (fn)) 3041 { 3042 fndecl = OVL_CURRENT (fn); 3043 if (TREE_CODE (fndecl) == FUNCTION_DECL 3044 && DECL_VINDEX (fndecl)) 3045 { 3046 /* If the method from the base class has the same 3047 signature as the method from the derived class, it 3048 has been overridden. */ 3049 for (size_t k = 0; k < base_fndecls.length (); k++) 3050 if (base_fndecls[k] 3051 && same_signature_p (fndecl, base_fndecls[k])) 3052 base_fndecls[k] = NULL_TREE; 3053 } 3054 } 3055 3056 /* Now give a warning for all base functions without overriders, 3057 as they are hidden. */ 3058 size_t k; 3059 tree base_fndecl; 3060 FOR_EACH_VEC_ELT (base_fndecls, k, base_fndecl) 3061 if (base_fndecl) 3062 { 3063 /* Here we know it is a hider, and no overrider exists. */ 3064 warning_at (location_of (base_fndecl), 3065 OPT_Woverloaded_virtual, 3066 "%qD was hidden", base_fndecl); 3067 warning_at (location_of (fns), 3068 OPT_Woverloaded_virtual, " by %qD", fns); 3069 } 3070 } 3071 } 3072 3073 /* Recursive helper for finish_struct_anon. */ 3074 3075 static void 3076 finish_struct_anon_r (tree field, bool complain) 3077 { 3078 bool is_union = TREE_CODE (TREE_TYPE (field)) == UNION_TYPE; 3079 tree elt = TYPE_FIELDS (TREE_TYPE (field)); 3080 for (; elt; elt = DECL_CHAIN (elt)) 3081 { 3082 /* We're generally only interested in entities the user 3083 declared, but we also find nested classes by noticing 3084 the TYPE_DECL that we create implicitly. You're 3085 allowed to put one anonymous union inside another, 3086 though, so we explicitly tolerate that. We use 3087 TYPE_ANONYMOUS_P rather than ANON_AGGR_TYPE_P so that 3088 we also allow unnamed types used for defining fields. */ 3089 if (DECL_ARTIFICIAL (elt) 3090 && (!DECL_IMPLICIT_TYPEDEF_P (elt) 3091 || TYPE_ANONYMOUS_P (TREE_TYPE (elt)))) 3092 continue; 3093 3094 if (TREE_CODE (elt) != FIELD_DECL) 3095 { 3096 /* We already complained about static data members in 3097 finish_static_data_member_decl. */ 3098 if (complain && !VAR_P (elt)) 3099 { 3100 if (is_union) 3101 permerror (DECL_SOURCE_LOCATION (elt), 3102 "%q#D invalid; an anonymous union can " 3103 "only have non-static data members", elt); 3104 else 3105 permerror (DECL_SOURCE_LOCATION (elt), 3106 "%q#D invalid; an anonymous struct can " 3107 "only have non-static data members", elt); 3108 } 3109 continue; 3110 } 3111 3112 if (complain) 3113 { 3114 if (TREE_PRIVATE (elt)) 3115 { 3116 if (is_union) 3117 permerror (DECL_SOURCE_LOCATION (elt), 3118 "private member %q#D in anonymous union", elt); 3119 else 3120 permerror (DECL_SOURCE_LOCATION (elt), 3121 "private member %q#D in anonymous struct", elt); 3122 } 3123 else if (TREE_PROTECTED (elt)) 3124 { 3125 if (is_union) 3126 permerror (DECL_SOURCE_LOCATION (elt), 3127 "protected member %q#D in anonymous union", elt); 3128 else 3129 permerror (DECL_SOURCE_LOCATION (elt), 3130 "protected member %q#D in anonymous struct", elt); 3131 } 3132 } 3133 3134 TREE_PRIVATE (elt) = TREE_PRIVATE (field); 3135 TREE_PROTECTED (elt) = TREE_PROTECTED (field); 3136 3137 /* Recurse into the anonymous aggregates to handle correctly 3138 access control (c++/24926): 3139 3140 class A { 3141 union { 3142 union { 3143 int i; 3144 }; 3145 }; 3146 }; 3147 3148 int j=A().i; */ 3149 if (DECL_NAME (elt) == NULL_TREE 3150 && ANON_AGGR_TYPE_P (TREE_TYPE (elt))) 3151 finish_struct_anon_r (elt, /*complain=*/false); 3152 } 3153 } 3154 3155 /* Check for things that are invalid. There are probably plenty of other 3156 things we should check for also. */ 3157 3158 static void 3159 finish_struct_anon (tree t) 3160 { 3161 for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field)) 3162 { 3163 if (TREE_STATIC (field)) 3164 continue; 3165 if (TREE_CODE (field) != FIELD_DECL) 3166 continue; 3167 3168 if (DECL_NAME (field) == NULL_TREE 3169 && ANON_AGGR_TYPE_P (TREE_TYPE (field))) 3170 finish_struct_anon_r (field, /*complain=*/true); 3171 } 3172 } 3173 3174 /* Add T to CLASSTYPE_DECL_LIST of current_class_type which 3175 will be used later during class template instantiation. 3176 When FRIEND_P is zero, T can be a static member data (VAR_DECL), 3177 a non-static member data (FIELD_DECL), a member function 3178 (FUNCTION_DECL), a nested type (RECORD_TYPE, ENUM_TYPE), 3179 a typedef (TYPE_DECL) or a member class template (TEMPLATE_DECL) 3180 When FRIEND_P is nonzero, T is either a friend class 3181 (RECORD_TYPE, TEMPLATE_DECL) or a friend function 3182 (FUNCTION_DECL, TEMPLATE_DECL). */ 3183 3184 void 3185 maybe_add_class_template_decl_list (tree type, tree t, int friend_p) 3186 { 3187 /* Save some memory by not creating TREE_LIST if TYPE is not template. */ 3188 if (CLASSTYPE_TEMPLATE_INFO (type)) 3189 CLASSTYPE_DECL_LIST (type) 3190 = tree_cons (friend_p ? NULL_TREE : type, 3191 t, CLASSTYPE_DECL_LIST (type)); 3192 } 3193 3194 /* This function is called from declare_virt_assop_and_dtor via 3195 dfs_walk_all. 3196 3197 DATA is a type that direcly or indirectly inherits the base 3198 represented by BINFO. If BINFO contains a virtual assignment [copy 3199 assignment or move assigment] operator or a virtual constructor, 3200 declare that function in DATA if it hasn't been already declared. */ 3201 3202 static tree 3203 dfs_declare_virt_assop_and_dtor (tree binfo, void *data) 3204 { 3205 tree bv, fn, t = (tree)data; 3206 tree opname = ansi_assopname (NOP_EXPR); 3207 3208 gcc_assert (t && CLASS_TYPE_P (t)); 3209 gcc_assert (binfo && TREE_CODE (binfo) == TREE_BINFO); 3210 3211 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo))) 3212 /* A base without a vtable needs no modification, and its bases 3213 are uninteresting. */ 3214 return dfs_skip_bases; 3215 3216 if (BINFO_PRIMARY_P (binfo)) 3217 /* If this is a primary base, then we have already looked at the 3218 virtual functions of its vtable. */ 3219 return NULL_TREE; 3220 3221 for (bv = BINFO_VIRTUALS (binfo); bv; bv = TREE_CHAIN (bv)) 3222 { 3223 fn = BV_FN (bv); 3224 3225 if (DECL_NAME (fn) == opname) 3226 { 3227 if (CLASSTYPE_LAZY_COPY_ASSIGN (t)) 3228 lazily_declare_fn (sfk_copy_assignment, t); 3229 if (CLASSTYPE_LAZY_MOVE_ASSIGN (t)) 3230 lazily_declare_fn (sfk_move_assignment, t); 3231 } 3232 else if (DECL_DESTRUCTOR_P (fn) 3233 && CLASSTYPE_LAZY_DESTRUCTOR (t)) 3234 lazily_declare_fn (sfk_destructor, t); 3235 } 3236 3237 return NULL_TREE; 3238 } 3239 3240 /* If the class type T has a direct or indirect base that contains a 3241 virtual assignment operator or a virtual destructor, declare that 3242 function in T if it hasn't been already declared. */ 3243 3244 static void 3245 declare_virt_assop_and_dtor (tree t) 3246 { 3247 if (!(TYPE_POLYMORPHIC_P (t) 3248 && (CLASSTYPE_LAZY_COPY_ASSIGN (t) 3249 || CLASSTYPE_LAZY_MOVE_ASSIGN (t) 3250 || CLASSTYPE_LAZY_DESTRUCTOR (t)))) 3251 return; 3252 3253 dfs_walk_all (TYPE_BINFO (t), 3254 dfs_declare_virt_assop_and_dtor, 3255 NULL, t); 3256 } 3257 3258 /* Declare the inheriting constructor for class T inherited from base 3259 constructor CTOR with the parameter array PARMS of size NPARMS. */ 3260 3261 static void 3262 one_inheriting_sig (tree t, tree ctor, tree *parms, int nparms) 3263 { 3264 /* We don't declare an inheriting ctor that would be a default, 3265 copy or move ctor for derived or base. */ 3266 if (nparms == 0) 3267 return; 3268 if (nparms == 1 3269 && TREE_CODE (parms[0]) == REFERENCE_TYPE) 3270 { 3271 tree parm = TYPE_MAIN_VARIANT (TREE_TYPE (parms[0])); 3272 if (parm == t || parm == DECL_CONTEXT (ctor)) 3273 return; 3274 } 3275 3276 tree parmlist = void_list_node; 3277 for (int i = nparms - 1; i >= 0; i--) 3278 parmlist = tree_cons (NULL_TREE, parms[i], parmlist); 3279 tree fn = implicitly_declare_fn (sfk_inheriting_constructor, 3280 t, false, ctor, parmlist); 3281 gcc_assert (TYPE_MAIN_VARIANT (t) == t); 3282 if (add_method (t, fn, NULL_TREE)) 3283 { 3284 DECL_CHAIN (fn) = TYPE_METHODS (t); 3285 TYPE_METHODS (t) = fn; 3286 } 3287 } 3288 3289 /* Declare all the inheriting constructors for class T inherited from base 3290 constructor CTOR. */ 3291 3292 static void 3293 one_inherited_ctor (tree ctor, tree t) 3294 { 3295 tree parms = FUNCTION_FIRST_USER_PARMTYPE (ctor); 3296 3297 tree *new_parms = XALLOCAVEC (tree, list_length (parms)); 3298 int i = 0; 3299 for (; parms && parms != void_list_node; parms = TREE_CHAIN (parms)) 3300 { 3301 if (TREE_PURPOSE (parms)) 3302 one_inheriting_sig (t, ctor, new_parms, i); 3303 new_parms[i++] = TREE_VALUE (parms); 3304 } 3305 one_inheriting_sig (t, ctor, new_parms, i); 3306 if (parms == NULL_TREE) 3307 { 3308 if (warning (OPT_Winherited_variadic_ctor, 3309 "the ellipsis in %qD is not inherited", ctor)) 3310 inform (DECL_SOURCE_LOCATION (ctor), "%qD declared here", ctor); 3311 } 3312 } 3313 3314 /* Create default constructors, assignment operators, and so forth for 3315 the type indicated by T, if they are needed. CANT_HAVE_CONST_CTOR, 3316 and CANT_HAVE_CONST_ASSIGNMENT are nonzero if, for whatever reason, 3317 the class cannot have a default constructor, copy constructor 3318 taking a const reference argument, or an assignment operator taking 3319 a const reference, respectively. */ 3320 3321 static void 3322 add_implicitly_declared_members (tree t, tree* access_decls, 3323 int cant_have_const_cctor, 3324 int cant_have_const_assignment) 3325 { 3326 bool move_ok = false; 3327 3328 if (cxx_dialect >= cxx11 && !CLASSTYPE_DESTRUCTORS (t) 3329 && !TYPE_HAS_COPY_CTOR (t) && !TYPE_HAS_COPY_ASSIGN (t) 3330 && !type_has_move_constructor (t) && !type_has_move_assign (t)) 3331 move_ok = true; 3332 3333 /* Destructor. */ 3334 if (!CLASSTYPE_DESTRUCTORS (t)) 3335 { 3336 /* In general, we create destructors lazily. */ 3337 CLASSTYPE_LAZY_DESTRUCTOR (t) = 1; 3338 3339 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) 3340 && TYPE_FOR_JAVA (t)) 3341 /* But if this is a Java class, any non-trivial destructor is 3342 invalid, even if compiler-generated. Therefore, if the 3343 destructor is non-trivial we create it now. */ 3344 lazily_declare_fn (sfk_destructor, t); 3345 } 3346 3347 /* [class.ctor] 3348 3349 If there is no user-declared constructor for a class, a default 3350 constructor is implicitly declared. */ 3351 if (! TYPE_HAS_USER_CONSTRUCTOR (t)) 3352 { 3353 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 1; 3354 CLASSTYPE_LAZY_DEFAULT_CTOR (t) = 1; 3355 if (cxx_dialect >= cxx11) 3356 TYPE_HAS_CONSTEXPR_CTOR (t) 3357 /* Don't force the declaration to get a hard answer; if the 3358 definition would have made the class non-literal, it will still be 3359 non-literal because of the base or member in question, and that 3360 gives a better diagnostic. */ 3361 = type_maybe_constexpr_default_constructor (t); 3362 } 3363 3364 /* [class.ctor] 3365 3366 If a class definition does not explicitly declare a copy 3367 constructor, one is declared implicitly. */ 3368 if (! TYPE_HAS_COPY_CTOR (t) && ! TYPE_FOR_JAVA (t)) 3369 { 3370 TYPE_HAS_COPY_CTOR (t) = 1; 3371 TYPE_HAS_CONST_COPY_CTOR (t) = !cant_have_const_cctor; 3372 CLASSTYPE_LAZY_COPY_CTOR (t) = 1; 3373 if (move_ok) 3374 CLASSTYPE_LAZY_MOVE_CTOR (t) = 1; 3375 } 3376 3377 /* If there is no assignment operator, one will be created if and 3378 when it is needed. For now, just record whether or not the type 3379 of the parameter to the assignment operator will be a const or 3380 non-const reference. */ 3381 if (!TYPE_HAS_COPY_ASSIGN (t) && !TYPE_FOR_JAVA (t)) 3382 { 3383 TYPE_HAS_COPY_ASSIGN (t) = 1; 3384 TYPE_HAS_CONST_COPY_ASSIGN (t) = !cant_have_const_assignment; 3385 CLASSTYPE_LAZY_COPY_ASSIGN (t) = 1; 3386 if (move_ok && !LAMBDA_TYPE_P (t)) 3387 CLASSTYPE_LAZY_MOVE_ASSIGN (t) = 1; 3388 } 3389 3390 /* We can't be lazy about declaring functions that might override 3391 a virtual function from a base class. */ 3392 declare_virt_assop_and_dtor (t); 3393 3394 while (*access_decls) 3395 { 3396 tree using_decl = TREE_VALUE (*access_decls); 3397 tree decl = USING_DECL_DECLS (using_decl); 3398 if (DECL_NAME (using_decl) == ctor_identifier) 3399 { 3400 /* declare, then remove the decl */ 3401 tree ctor_list = decl; 3402 location_t loc = input_location; 3403 input_location = DECL_SOURCE_LOCATION (using_decl); 3404 if (ctor_list) 3405 for (; ctor_list; ctor_list = OVL_NEXT (ctor_list)) 3406 one_inherited_ctor (OVL_CURRENT (ctor_list), t); 3407 *access_decls = TREE_CHAIN (*access_decls); 3408 input_location = loc; 3409 } 3410 else 3411 access_decls = &TREE_CHAIN (*access_decls); 3412 } 3413 } 3414 3415 /* Subroutine of insert_into_classtype_sorted_fields. Recursively 3416 count the number of fields in TYPE, including anonymous union 3417 members. */ 3418 3419 static int 3420 count_fields (tree fields) 3421 { 3422 tree x; 3423 int n_fields = 0; 3424 for (x = fields; x; x = DECL_CHAIN (x)) 3425 { 3426 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x))) 3427 n_fields += count_fields (TYPE_FIELDS (TREE_TYPE (x))); 3428 else 3429 n_fields += 1; 3430 } 3431 return n_fields; 3432 } 3433 3434 /* Subroutine of insert_into_classtype_sorted_fields. Recursively add 3435 all the fields in the TREE_LIST FIELDS to the SORTED_FIELDS_TYPE 3436 elts, starting at offset IDX. */ 3437 3438 static int 3439 add_fields_to_record_type (tree fields, struct sorted_fields_type *field_vec, int idx) 3440 { 3441 tree x; 3442 for (x = fields; x; x = DECL_CHAIN (x)) 3443 { 3444 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x))) 3445 idx = add_fields_to_record_type (TYPE_FIELDS (TREE_TYPE (x)), field_vec, idx); 3446 else 3447 field_vec->elts[idx++] = x; 3448 } 3449 return idx; 3450 } 3451 3452 /* Add all of the enum values of ENUMTYPE, to the FIELD_VEC elts, 3453 starting at offset IDX. */ 3454 3455 static int 3456 add_enum_fields_to_record_type (tree enumtype, 3457 struct sorted_fields_type *field_vec, 3458 int idx) 3459 { 3460 tree values; 3461 for (values = TYPE_VALUES (enumtype); values; values = TREE_CHAIN (values)) 3462 field_vec->elts[idx++] = TREE_VALUE (values); 3463 return idx; 3464 } 3465 3466 /* FIELD is a bit-field. We are finishing the processing for its 3467 enclosing type. Issue any appropriate messages and set appropriate 3468 flags. Returns false if an error has been diagnosed. */ 3469 3470 static bool 3471 check_bitfield_decl (tree field) 3472 { 3473 tree type = TREE_TYPE (field); 3474 tree w; 3475 3476 /* Extract the declared width of the bitfield, which has been 3477 temporarily stashed in DECL_INITIAL. */ 3478 w = DECL_INITIAL (field); 3479 gcc_assert (w != NULL_TREE); 3480 /* Remove the bit-field width indicator so that the rest of the 3481 compiler does not treat that value as an initializer. */ 3482 DECL_INITIAL (field) = NULL_TREE; 3483 3484 /* Detect invalid bit-field type. */ 3485 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type)) 3486 { 3487 error ("bit-field %q+#D with non-integral type", field); 3488 w = error_mark_node; 3489 } 3490 else 3491 { 3492 location_t loc = input_location; 3493 /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs. */ 3494 STRIP_NOPS (w); 3495 3496 /* detect invalid field size. */ 3497 input_location = DECL_SOURCE_LOCATION (field); 3498 w = cxx_constant_value (w); 3499 input_location = loc; 3500 3501 if (TREE_CODE (w) != INTEGER_CST) 3502 { 3503 error ("bit-field %q+D width not an integer constant", field); 3504 w = error_mark_node; 3505 } 3506 else if (tree_int_cst_sgn (w) < 0) 3507 { 3508 error ("negative width in bit-field %q+D", field); 3509 w = error_mark_node; 3510 } 3511 else if (integer_zerop (w) && DECL_NAME (field) != 0) 3512 { 3513 error ("zero width for bit-field %q+D", field); 3514 w = error_mark_node; 3515 } 3516 else if ((TREE_CODE (type) != ENUMERAL_TYPE 3517 && TREE_CODE (type) != BOOLEAN_TYPE 3518 && compare_tree_int (w, TYPE_PRECISION (type)) > 0) 3519 || ((TREE_CODE (type) == ENUMERAL_TYPE 3520 || TREE_CODE (type) == BOOLEAN_TYPE) 3521 && tree_int_cst_lt (TYPE_SIZE (type), w))) 3522 warning_at (DECL_SOURCE_LOCATION (field), 0, 3523 "width of %qD exceeds its type", field); 3524 else if (TREE_CODE (type) == ENUMERAL_TYPE 3525 && (0 > (compare_tree_int 3526 (w, TYPE_PRECISION (ENUM_UNDERLYING_TYPE (type)))))) 3527 warning_at (DECL_SOURCE_LOCATION (field), 0, 3528 "%qD is too small to hold all values of %q#T", 3529 field, type); 3530 } 3531 3532 if (w != error_mark_node) 3533 { 3534 DECL_SIZE (field) = fold_convert (bitsizetype, w); 3535 DECL_BIT_FIELD (field) = 1; 3536 return true; 3537 } 3538 else 3539 { 3540 /* Non-bit-fields are aligned for their type. */ 3541 DECL_BIT_FIELD (field) = 0; 3542 CLEAR_DECL_C_BIT_FIELD (field); 3543 return false; 3544 } 3545 } 3546 3547 /* FIELD is a non bit-field. We are finishing the processing for its 3548 enclosing type T. Issue any appropriate messages and set appropriate 3549 flags. */ 3550 3551 static void 3552 check_field_decl (tree field, 3553 tree t, 3554 int* cant_have_const_ctor, 3555 int* no_const_asn_ref, 3556 int* any_default_members) 3557 { 3558 tree type = strip_array_types (TREE_TYPE (field)); 3559 3560 /* In C++98 an anonymous union cannot contain any fields which would change 3561 the settings of CANT_HAVE_CONST_CTOR and friends. */ 3562 if (ANON_UNION_TYPE_P (type) && cxx_dialect < cxx11) 3563 ; 3564 /* And, we don't set TYPE_HAS_CONST_COPY_CTOR, etc., for anonymous 3565 structs. So, we recurse through their fields here. */ 3566 else if (ANON_AGGR_TYPE_P (type)) 3567 { 3568 tree fields; 3569 3570 for (fields = TYPE_FIELDS (type); fields; fields = DECL_CHAIN (fields)) 3571 if (TREE_CODE (fields) == FIELD_DECL && !DECL_C_BIT_FIELD (field)) 3572 check_field_decl (fields, t, cant_have_const_ctor, 3573 no_const_asn_ref, any_default_members); 3574 } 3575 /* Check members with class type for constructors, destructors, 3576 etc. */ 3577 else if (CLASS_TYPE_P (type)) 3578 { 3579 /* Never let anything with uninheritable virtuals 3580 make it through without complaint. */ 3581 abstract_virtuals_error (field, type); 3582 3583 if (TREE_CODE (t) == UNION_TYPE && cxx_dialect < cxx11) 3584 { 3585 static bool warned; 3586 int oldcount = errorcount; 3587 if (TYPE_NEEDS_CONSTRUCTING (type)) 3588 error ("member %q+#D with constructor not allowed in union", 3589 field); 3590 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)) 3591 error ("member %q+#D with destructor not allowed in union", field); 3592 if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type)) 3593 error ("member %q+#D with copy assignment operator not allowed in union", 3594 field); 3595 if (!warned && errorcount > oldcount) 3596 { 3597 inform (DECL_SOURCE_LOCATION (field), "unrestricted unions " 3598 "only available with -std=c++11 or -std=gnu++11"); 3599 warned = true; 3600 } 3601 } 3602 else 3603 { 3604 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type); 3605 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) 3606 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type); 3607 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) 3608 |= (TYPE_HAS_COMPLEX_COPY_ASSIGN (type) 3609 || !TYPE_HAS_COPY_ASSIGN (type)); 3610 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= (TYPE_HAS_COMPLEX_COPY_CTOR (type) 3611 || !TYPE_HAS_COPY_CTOR (type)); 3612 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) |= TYPE_HAS_COMPLEX_MOVE_ASSIGN (type); 3613 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_HAS_COMPLEX_MOVE_CTOR (type); 3614 TYPE_HAS_COMPLEX_DFLT (t) |= (!TYPE_HAS_DEFAULT_CONSTRUCTOR (type) 3615 || TYPE_HAS_COMPLEX_DFLT (type)); 3616 } 3617 3618 if (TYPE_HAS_COPY_CTOR (type) 3619 && !TYPE_HAS_CONST_COPY_CTOR (type)) 3620 *cant_have_const_ctor = 1; 3621 3622 if (TYPE_HAS_COPY_ASSIGN (type) 3623 && !TYPE_HAS_CONST_COPY_ASSIGN (type)) 3624 *no_const_asn_ref = 1; 3625 } 3626 3627 check_abi_tags (t, field); 3628 3629 if (DECL_INITIAL (field) != NULL_TREE) 3630 { 3631 /* `build_class_init_list' does not recognize 3632 non-FIELD_DECLs. */ 3633 if (TREE_CODE (t) == UNION_TYPE && *any_default_members != 0) 3634 error ("multiple fields in union %qT initialized", t); 3635 *any_default_members = 1; 3636 } 3637 } 3638 3639 /* Check the data members (both static and non-static), class-scoped 3640 typedefs, etc., appearing in the declaration of T. Issue 3641 appropriate diagnostics. Sets ACCESS_DECLS to a list (in 3642 declaration order) of access declarations; each TREE_VALUE in this 3643 list is a USING_DECL. 3644 3645 In addition, set the following flags: 3646 3647 EMPTY_P 3648 The class is empty, i.e., contains no non-static data members. 3649 3650 CANT_HAVE_CONST_CTOR_P 3651 This class cannot have an implicitly generated copy constructor 3652 taking a const reference. 3653 3654 CANT_HAVE_CONST_ASN_REF 3655 This class cannot have an implicitly generated assignment 3656 operator taking a const reference. 3657 3658 All of these flags should be initialized before calling this 3659 function. 3660 3661 Returns a pointer to the end of the TYPE_FIELDs chain; additional 3662 fields can be added by adding to this chain. */ 3663 3664 static void 3665 check_field_decls (tree t, tree *access_decls, 3666 int *cant_have_const_ctor_p, 3667 int *no_const_asn_ref_p) 3668 { 3669 tree *field; 3670 tree *next; 3671 bool has_pointers; 3672 int any_default_members; 3673 int cant_pack = 0; 3674 int field_access = -1; 3675 3676 /* Assume there are no access declarations. */ 3677 *access_decls = NULL_TREE; 3678 /* Assume this class has no pointer members. */ 3679 has_pointers = false; 3680 /* Assume none of the members of this class have default 3681 initializations. */ 3682 any_default_members = 0; 3683 3684 for (field = &TYPE_FIELDS (t); *field; field = next) 3685 { 3686 tree x = *field; 3687 tree type = TREE_TYPE (x); 3688 int this_field_access; 3689 3690 next = &DECL_CHAIN (x); 3691 3692 if (TREE_CODE (x) == USING_DECL) 3693 { 3694 /* Save the access declarations for our caller. */ 3695 *access_decls = tree_cons (NULL_TREE, x, *access_decls); 3696 continue; 3697 } 3698 3699 if (TREE_CODE (x) == TYPE_DECL 3700 || TREE_CODE (x) == TEMPLATE_DECL) 3701 continue; 3702 3703 /* If we've gotten this far, it's a data member, possibly static, 3704 or an enumerator. */ 3705 if (TREE_CODE (x) != CONST_DECL) 3706 DECL_CONTEXT (x) = t; 3707 3708 /* When this goes into scope, it will be a non-local reference. */ 3709 DECL_NONLOCAL (x) = 1; 3710 3711 if (TREE_CODE (t) == UNION_TYPE 3712 && cxx_dialect < cxx11) 3713 { 3714 /* [class.union] (C++98) 3715 3716 If a union contains a static data member, or a member of 3717 reference type, the program is ill-formed. 3718 3719 In C++11 this limitation doesn't exist anymore. */ 3720 if (VAR_P (x)) 3721 { 3722 error ("in C++98 %q+D may not be static because it is " 3723 "a member of a union", x); 3724 continue; 3725 } 3726 if (TREE_CODE (type) == REFERENCE_TYPE) 3727 { 3728 error ("in C++98 %q+D may not have reference type %qT " 3729 "because it is a member of a union", x, type); 3730 continue; 3731 } 3732 } 3733 3734 /* Perform error checking that did not get done in 3735 grokdeclarator. */ 3736 if (TREE_CODE (type) == FUNCTION_TYPE) 3737 { 3738 error ("field %q+D invalidly declared function type", x); 3739 type = build_pointer_type (type); 3740 TREE_TYPE (x) = type; 3741 } 3742 else if (TREE_CODE (type) == METHOD_TYPE) 3743 { 3744 error ("field %q+D invalidly declared method type", x); 3745 type = build_pointer_type (type); 3746 TREE_TYPE (x) = type; 3747 } 3748 3749 if (type == error_mark_node) 3750 continue; 3751 3752 if (TREE_CODE (x) == CONST_DECL || VAR_P (x)) 3753 continue; 3754 3755 /* Now it can only be a FIELD_DECL. */ 3756 3757 if (TREE_PRIVATE (x) || TREE_PROTECTED (x)) 3758 CLASSTYPE_NON_AGGREGATE (t) = 1; 3759 3760 /* If at least one non-static data member is non-literal, the whole 3761 class becomes non-literal. Per Core/1453, volatile non-static 3762 data members and base classes are also not allowed. 3763 Note: if the type is incomplete we will complain later on. */ 3764 if (COMPLETE_TYPE_P (type) 3765 && (!literal_type_p (type) || CP_TYPE_VOLATILE_P (type))) 3766 CLASSTYPE_LITERAL_P (t) = false; 3767 3768 /* A standard-layout class is a class that: 3769 ... 3770 has the same access control (Clause 11) for all non-static data members, 3771 ... */ 3772 this_field_access = TREE_PROTECTED (x) ? 1 : TREE_PRIVATE (x) ? 2 : 0; 3773 if (field_access == -1) 3774 field_access = this_field_access; 3775 else if (this_field_access != field_access) 3776 CLASSTYPE_NON_STD_LAYOUT (t) = 1; 3777 3778 /* If this is of reference type, check if it needs an init. */ 3779 if (TREE_CODE (type) == REFERENCE_TYPE) 3780 { 3781 CLASSTYPE_NON_LAYOUT_POD_P (t) = 1; 3782 CLASSTYPE_NON_STD_LAYOUT (t) = 1; 3783 if (DECL_INITIAL (x) == NULL_TREE) 3784 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1); 3785 if (cxx_dialect < cxx11) 3786 { 3787 /* ARM $12.6.2: [A member initializer list] (or, for an 3788 aggregate, initialization by a brace-enclosed list) is the 3789 only way to initialize nonstatic const and reference 3790 members. */ 3791 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1; 3792 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = 1; 3793 } 3794 } 3795 3796 type = strip_array_types (type); 3797 3798 if (TYPE_PACKED (t)) 3799 { 3800 if (!layout_pod_type_p (type) && !TYPE_PACKED (type)) 3801 { 3802 warning_at 3803 (DECL_SOURCE_LOCATION (x), 0, 3804 "ignoring packed attribute because of unpacked non-POD field %q#D", 3805 x); 3806 cant_pack = 1; 3807 } 3808 else if (DECL_C_BIT_FIELD (x) 3809 || TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT) 3810 DECL_PACKED (x) = 1; 3811 } 3812 3813 if (DECL_C_BIT_FIELD (x) && integer_zerop (DECL_INITIAL (x))) 3814 /* We don't treat zero-width bitfields as making a class 3815 non-empty. */ 3816 ; 3817 else 3818 { 3819 /* The class is non-empty. */ 3820 CLASSTYPE_EMPTY_P (t) = 0; 3821 /* The class is not even nearly empty. */ 3822 CLASSTYPE_NEARLY_EMPTY_P (t) = 0; 3823 /* If one of the data members contains an empty class, 3824 so does T. */ 3825 if (CLASS_TYPE_P (type) 3826 && CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type)) 3827 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1; 3828 } 3829 3830 /* This is used by -Weffc++ (see below). Warn only for pointers 3831 to members which might hold dynamic memory. So do not warn 3832 for pointers to functions or pointers to members. */ 3833 if (TYPE_PTR_P (type) 3834 && !TYPE_PTRFN_P (type)) 3835 has_pointers = true; 3836 3837 if (CLASS_TYPE_P (type)) 3838 { 3839 if (CLASSTYPE_REF_FIELDS_NEED_INIT (type)) 3840 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1); 3841 if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (type)) 3842 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1); 3843 } 3844 3845 if (DECL_MUTABLE_P (x) || TYPE_HAS_MUTABLE_P (type)) 3846 CLASSTYPE_HAS_MUTABLE (t) = 1; 3847 3848 if (DECL_MUTABLE_P (x)) 3849 { 3850 if (CP_TYPE_CONST_P (type)) 3851 { 3852 error ("member %q+D cannot be declared both %<const%> " 3853 "and %<mutable%>", x); 3854 continue; 3855 } 3856 if (TREE_CODE (type) == REFERENCE_TYPE) 3857 { 3858 error ("member %q+D cannot be declared as a %<mutable%> " 3859 "reference", x); 3860 continue; 3861 } 3862 } 3863 3864 if (! layout_pod_type_p (type)) 3865 /* DR 148 now allows pointers to members (which are POD themselves), 3866 to be allowed in POD structs. */ 3867 CLASSTYPE_NON_LAYOUT_POD_P (t) = 1; 3868 3869 if (!std_layout_type_p (type)) 3870 CLASSTYPE_NON_STD_LAYOUT (t) = 1; 3871 3872 if (! zero_init_p (type)) 3873 CLASSTYPE_NON_ZERO_INIT_P (t) = 1; 3874 3875 /* We set DECL_C_BIT_FIELD in grokbitfield. 3876 If the type and width are valid, we'll also set DECL_BIT_FIELD. */ 3877 if (! DECL_C_BIT_FIELD (x) || ! check_bitfield_decl (x)) 3878 check_field_decl (x, t, 3879 cant_have_const_ctor_p, 3880 no_const_asn_ref_p, 3881 &any_default_members); 3882 3883 /* Now that we've removed bit-field widths from DECL_INITIAL, 3884 anything left in DECL_INITIAL is an NSDMI that makes the class 3885 non-aggregate in C++11. */ 3886 if (DECL_INITIAL (x) && cxx_dialect < cxx14) 3887 CLASSTYPE_NON_AGGREGATE (t) = true; 3888 3889 /* If any field is const, the structure type is pseudo-const. */ 3890 if (CP_TYPE_CONST_P (type)) 3891 { 3892 C_TYPE_FIELDS_READONLY (t) = 1; 3893 if (DECL_INITIAL (x) == NULL_TREE) 3894 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1); 3895 if (cxx_dialect < cxx11) 3896 { 3897 /* ARM $12.6.2: [A member initializer list] (or, for an 3898 aggregate, initialization by a brace-enclosed list) is the 3899 only way to initialize nonstatic const and reference 3900 members. */ 3901 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) = 1; 3902 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) = 1; 3903 } 3904 } 3905 /* A field that is pseudo-const makes the structure likewise. */ 3906 else if (CLASS_TYPE_P (type)) 3907 { 3908 C_TYPE_FIELDS_READONLY (t) |= C_TYPE_FIELDS_READONLY (type); 3909 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 3910 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t) 3911 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (type)); 3912 } 3913 3914 /* Core issue 80: A nonstatic data member is required to have a 3915 different name from the class iff the class has a 3916 user-declared constructor. */ 3917 if (constructor_name_p (DECL_NAME (x), t) 3918 && TYPE_HAS_USER_CONSTRUCTOR (t)) 3919 permerror (DECL_SOURCE_LOCATION (x), 3920 "field %q#D with same name as class", x); 3921 } 3922 3923 /* Effective C++ rule 11: if a class has dynamic memory held by pointers, 3924 it should also define a copy constructor and an assignment operator to 3925 implement the correct copy semantic (deep vs shallow, etc.). As it is 3926 not feasible to check whether the constructors do allocate dynamic memory 3927 and store it within members, we approximate the warning like this: 3928 3929 -- Warn only if there are members which are pointers 3930 -- Warn only if there is a non-trivial constructor (otherwise, 3931 there cannot be memory allocated). 3932 -- Warn only if there is a non-trivial destructor. We assume that the 3933 user at least implemented the cleanup correctly, and a destructor 3934 is needed to free dynamic memory. 3935 3936 This seems enough for practical purposes. */ 3937 if (warn_ecpp 3938 && has_pointers 3939 && TYPE_HAS_USER_CONSTRUCTOR (t) 3940 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) 3941 && !(TYPE_HAS_COPY_CTOR (t) && TYPE_HAS_COPY_ASSIGN (t))) 3942 { 3943 warning (OPT_Weffc__, "%q#T has pointer data members", t); 3944 3945 if (! TYPE_HAS_COPY_CTOR (t)) 3946 { 3947 warning (OPT_Weffc__, 3948 " but does not override %<%T(const %T&)%>", t, t); 3949 if (!TYPE_HAS_COPY_ASSIGN (t)) 3950 warning (OPT_Weffc__, " or %<operator=(const %T&)%>", t); 3951 } 3952 else if (! TYPE_HAS_COPY_ASSIGN (t)) 3953 warning (OPT_Weffc__, 3954 " but does not override %<operator=(const %T&)%>", t); 3955 } 3956 3957 /* Non-static data member initializers make the default constructor 3958 non-trivial. */ 3959 if (any_default_members) 3960 { 3961 TYPE_NEEDS_CONSTRUCTING (t) = true; 3962 TYPE_HAS_COMPLEX_DFLT (t) = true; 3963 } 3964 3965 /* If any of the fields couldn't be packed, unset TYPE_PACKED. */ 3966 if (cant_pack) 3967 TYPE_PACKED (t) = 0; 3968 3969 /* Check anonymous struct/anonymous union fields. */ 3970 finish_struct_anon (t); 3971 3972 /* We've built up the list of access declarations in reverse order. 3973 Fix that now. */ 3974 *access_decls = nreverse (*access_decls); 3975 } 3976 3977 /* If TYPE is an empty class type, records its OFFSET in the table of 3978 OFFSETS. */ 3979 3980 static int 3981 record_subobject_offset (tree type, tree offset, splay_tree offsets) 3982 { 3983 splay_tree_node n; 3984 3985 if (!is_empty_class (type)) 3986 return 0; 3987 3988 /* Record the location of this empty object in OFFSETS. */ 3989 n = splay_tree_lookup (offsets, (splay_tree_key) offset); 3990 if (!n) 3991 n = splay_tree_insert (offsets, 3992 (splay_tree_key) offset, 3993 (splay_tree_value) NULL_TREE); 3994 n->value = ((splay_tree_value) 3995 tree_cons (NULL_TREE, 3996 type, 3997 (tree) n->value)); 3998 3999 return 0; 4000 } 4001 4002 /* Returns nonzero if TYPE is an empty class type and there is 4003 already an entry in OFFSETS for the same TYPE as the same OFFSET. */ 4004 4005 static int 4006 check_subobject_offset (tree type, tree offset, splay_tree offsets) 4007 { 4008 splay_tree_node n; 4009 tree t; 4010 4011 if (!is_empty_class (type)) 4012 return 0; 4013 4014 /* Record the location of this empty object in OFFSETS. */ 4015 n = splay_tree_lookup (offsets, (splay_tree_key) offset); 4016 if (!n) 4017 return 0; 4018 4019 for (t = (tree) n->value; t; t = TREE_CHAIN (t)) 4020 if (same_type_p (TREE_VALUE (t), type)) 4021 return 1; 4022 4023 return 0; 4024 } 4025 4026 /* Walk through all the subobjects of TYPE (located at OFFSET). Call 4027 F for every subobject, passing it the type, offset, and table of 4028 OFFSETS. If VBASES_P is one, then virtual non-primary bases should 4029 be traversed. 4030 4031 If MAX_OFFSET is non-NULL, then subobjects with an offset greater 4032 than MAX_OFFSET will not be walked. 4033 4034 If F returns a nonzero value, the traversal ceases, and that value 4035 is returned. Otherwise, returns zero. */ 4036 4037 static int 4038 walk_subobject_offsets (tree type, 4039 subobject_offset_fn f, 4040 tree offset, 4041 splay_tree offsets, 4042 tree max_offset, 4043 int vbases_p) 4044 { 4045 int r = 0; 4046 tree type_binfo = NULL_TREE; 4047 4048 /* If this OFFSET is bigger than the MAX_OFFSET, then we should 4049 stop. */ 4050 if (max_offset && tree_int_cst_lt (max_offset, offset)) 4051 return 0; 4052 4053 if (type == error_mark_node) 4054 return 0; 4055 4056 if (!TYPE_P (type)) 4057 { 4058 type_binfo = type; 4059 type = BINFO_TYPE (type); 4060 } 4061 4062 if (CLASS_TYPE_P (type)) 4063 { 4064 tree field; 4065 tree binfo; 4066 int i; 4067 4068 /* Avoid recursing into objects that are not interesting. */ 4069 if (!CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type)) 4070 return 0; 4071 4072 /* Record the location of TYPE. */ 4073 r = (*f) (type, offset, offsets); 4074 if (r) 4075 return r; 4076 4077 /* Iterate through the direct base classes of TYPE. */ 4078 if (!type_binfo) 4079 type_binfo = TYPE_BINFO (type); 4080 for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, binfo); i++) 4081 { 4082 tree binfo_offset; 4083 4084 if (BINFO_VIRTUAL_P (binfo)) 4085 continue; 4086 4087 tree orig_binfo; 4088 /* We cannot rely on BINFO_OFFSET being set for the base 4089 class yet, but the offsets for direct non-virtual 4090 bases can be calculated by going back to the TYPE. */ 4091 orig_binfo = BINFO_BASE_BINFO (TYPE_BINFO (type), i); 4092 binfo_offset = size_binop (PLUS_EXPR, 4093 offset, 4094 BINFO_OFFSET (orig_binfo)); 4095 4096 r = walk_subobject_offsets (binfo, 4097 f, 4098 binfo_offset, 4099 offsets, 4100 max_offset, 4101 /*vbases_p=*/0); 4102 if (r) 4103 return r; 4104 } 4105 4106 if (CLASSTYPE_VBASECLASSES (type)) 4107 { 4108 unsigned ix; 4109 vec<tree, va_gc> *vbases; 4110 4111 /* Iterate through the virtual base classes of TYPE. In G++ 4112 3.2, we included virtual bases in the direct base class 4113 loop above, which results in incorrect results; the 4114 correct offsets for virtual bases are only known when 4115 working with the most derived type. */ 4116 if (vbases_p) 4117 for (vbases = CLASSTYPE_VBASECLASSES (type), ix = 0; 4118 vec_safe_iterate (vbases, ix, &binfo); ix++) 4119 { 4120 r = walk_subobject_offsets (binfo, 4121 f, 4122 size_binop (PLUS_EXPR, 4123 offset, 4124 BINFO_OFFSET (binfo)), 4125 offsets, 4126 max_offset, 4127 /*vbases_p=*/0); 4128 if (r) 4129 return r; 4130 } 4131 else 4132 { 4133 /* We still have to walk the primary base, if it is 4134 virtual. (If it is non-virtual, then it was walked 4135 above.) */ 4136 tree vbase = get_primary_binfo (type_binfo); 4137 4138 if (vbase && BINFO_VIRTUAL_P (vbase) 4139 && BINFO_PRIMARY_P (vbase) 4140 && BINFO_INHERITANCE_CHAIN (vbase) == type_binfo) 4141 { 4142 r = (walk_subobject_offsets 4143 (vbase, f, offset, 4144 offsets, max_offset, /*vbases_p=*/0)); 4145 if (r) 4146 return r; 4147 } 4148 } 4149 } 4150 4151 /* Iterate through the fields of TYPE. */ 4152 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field)) 4153 if (TREE_CODE (field) == FIELD_DECL 4154 && TREE_TYPE (field) != error_mark_node 4155 && !DECL_ARTIFICIAL (field)) 4156 { 4157 tree field_offset; 4158 4159 field_offset = byte_position (field); 4160 4161 r = walk_subobject_offsets (TREE_TYPE (field), 4162 f, 4163 size_binop (PLUS_EXPR, 4164 offset, 4165 field_offset), 4166 offsets, 4167 max_offset, 4168 /*vbases_p=*/1); 4169 if (r) 4170 return r; 4171 } 4172 } 4173 else if (TREE_CODE (type) == ARRAY_TYPE) 4174 { 4175 tree element_type = strip_array_types (type); 4176 tree domain = TYPE_DOMAIN (type); 4177 tree index; 4178 4179 /* Avoid recursing into objects that are not interesting. */ 4180 if (!CLASS_TYPE_P (element_type) 4181 || !CLASSTYPE_CONTAINS_EMPTY_CLASS_P (element_type) 4182 || !domain 4183 || integer_minus_onep (TYPE_MAX_VALUE (domain))) 4184 return 0; 4185 4186 /* Step through each of the elements in the array. */ 4187 for (index = size_zero_node; 4188 !tree_int_cst_lt (TYPE_MAX_VALUE (domain), index); 4189 index = size_binop (PLUS_EXPR, index, size_one_node)) 4190 { 4191 r = walk_subobject_offsets (TREE_TYPE (type), 4192 f, 4193 offset, 4194 offsets, 4195 max_offset, 4196 /*vbases_p=*/1); 4197 if (r) 4198 return r; 4199 offset = size_binop (PLUS_EXPR, offset, 4200 TYPE_SIZE_UNIT (TREE_TYPE (type))); 4201 /* If this new OFFSET is bigger than the MAX_OFFSET, then 4202 there's no point in iterating through the remaining 4203 elements of the array. */ 4204 if (max_offset && tree_int_cst_lt (max_offset, offset)) 4205 break; 4206 } 4207 } 4208 4209 return 0; 4210 } 4211 4212 /* Record all of the empty subobjects of TYPE (either a type or a 4213 binfo). If IS_DATA_MEMBER is true, then a non-static data member 4214 is being placed at OFFSET; otherwise, it is a base class that is 4215 being placed at OFFSET. */ 4216 4217 static void 4218 record_subobject_offsets (tree type, 4219 tree offset, 4220 splay_tree offsets, 4221 bool is_data_member) 4222 { 4223 tree max_offset; 4224 /* If recording subobjects for a non-static data member or a 4225 non-empty base class , we do not need to record offsets beyond 4226 the size of the biggest empty class. Additional data members 4227 will go at the end of the class. Additional base classes will go 4228 either at offset zero (if empty, in which case they cannot 4229 overlap with offsets past the size of the biggest empty class) or 4230 at the end of the class. 4231 4232 However, if we are placing an empty base class, then we must record 4233 all offsets, as either the empty class is at offset zero (where 4234 other empty classes might later be placed) or at the end of the 4235 class (where other objects might then be placed, so other empty 4236 subobjects might later overlap). */ 4237 if (is_data_member 4238 || !is_empty_class (BINFO_TYPE (type))) 4239 max_offset = sizeof_biggest_empty_class; 4240 else 4241 max_offset = NULL_TREE; 4242 walk_subobject_offsets (type, record_subobject_offset, offset, 4243 offsets, max_offset, is_data_member); 4244 } 4245 4246 /* Returns nonzero if any of the empty subobjects of TYPE (located at 4247 OFFSET) conflict with entries in OFFSETS. If VBASES_P is nonzero, 4248 virtual bases of TYPE are examined. */ 4249 4250 static int 4251 layout_conflict_p (tree type, 4252 tree offset, 4253 splay_tree offsets, 4254 int vbases_p) 4255 { 4256 splay_tree_node max_node; 4257 4258 /* Get the node in OFFSETS that indicates the maximum offset where 4259 an empty subobject is located. */ 4260 max_node = splay_tree_max (offsets); 4261 /* If there aren't any empty subobjects, then there's no point in 4262 performing this check. */ 4263 if (!max_node) 4264 return 0; 4265 4266 return walk_subobject_offsets (type, check_subobject_offset, offset, 4267 offsets, (tree) (max_node->key), 4268 vbases_p); 4269 } 4270 4271 /* DECL is a FIELD_DECL corresponding either to a base subobject of a 4272 non-static data member of the type indicated by RLI. BINFO is the 4273 binfo corresponding to the base subobject, OFFSETS maps offsets to 4274 types already located at those offsets. This function determines 4275 the position of the DECL. */ 4276 4277 static void 4278 layout_nonempty_base_or_field (record_layout_info rli, 4279 tree decl, 4280 tree binfo, 4281 splay_tree offsets) 4282 { 4283 tree offset = NULL_TREE; 4284 bool field_p; 4285 tree type; 4286 4287 if (binfo) 4288 { 4289 /* For the purposes of determining layout conflicts, we want to 4290 use the class type of BINFO; TREE_TYPE (DECL) will be the 4291 CLASSTYPE_AS_BASE version, which does not contain entries for 4292 zero-sized bases. */ 4293 type = TREE_TYPE (binfo); 4294 field_p = false; 4295 } 4296 else 4297 { 4298 type = TREE_TYPE (decl); 4299 field_p = true; 4300 } 4301 4302 /* Try to place the field. It may take more than one try if we have 4303 a hard time placing the field without putting two objects of the 4304 same type at the same address. */ 4305 while (1) 4306 { 4307 struct record_layout_info_s old_rli = *rli; 4308 4309 /* Place this field. */ 4310 place_field (rli, decl); 4311 offset = byte_position (decl); 4312 4313 /* We have to check to see whether or not there is already 4314 something of the same type at the offset we're about to use. 4315 For example, consider: 4316 4317 struct S {}; 4318 struct T : public S { int i; }; 4319 struct U : public S, public T {}; 4320 4321 Here, we put S at offset zero in U. Then, we can't put T at 4322 offset zero -- its S component would be at the same address 4323 as the S we already allocated. So, we have to skip ahead. 4324 Since all data members, including those whose type is an 4325 empty class, have nonzero size, any overlap can happen only 4326 with a direct or indirect base-class -- it can't happen with 4327 a data member. */ 4328 /* In a union, overlap is permitted; all members are placed at 4329 offset zero. */ 4330 if (TREE_CODE (rli->t) == UNION_TYPE) 4331 break; 4332 if (layout_conflict_p (field_p ? type : binfo, offset, 4333 offsets, field_p)) 4334 { 4335 /* Strip off the size allocated to this field. That puts us 4336 at the first place we could have put the field with 4337 proper alignment. */ 4338 *rli = old_rli; 4339 4340 /* Bump up by the alignment required for the type. */ 4341 rli->bitpos 4342 = size_binop (PLUS_EXPR, rli->bitpos, 4343 bitsize_int (binfo 4344 ? CLASSTYPE_ALIGN (type) 4345 : TYPE_ALIGN (type))); 4346 normalize_rli (rli); 4347 } 4348 else if (TREE_CODE (type) == NULLPTR_TYPE 4349 && warn_abi && abi_version_crosses (9)) 4350 { 4351 /* Before ABI v9, we were giving nullptr_t alignment of 1; if 4352 the offset wasn't aligned like a pointer when we started to 4353 layout this field, that affects its position. */ 4354 tree pos = rli_size_unit_so_far (&old_rli); 4355 if (int_cst_value (pos) % TYPE_ALIGN_UNIT (ptr_type_node) != 0) 4356 { 4357 if (abi_version_at_least (9)) 4358 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wabi, 4359 "alignment of %qD increased in -fabi-version=9 " 4360 "(GCC 5.2)", decl); 4361 else 4362 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wabi, "alignment " 4363 "of %qD will increase in -fabi-version=9", decl); 4364 } 4365 break; 4366 } 4367 else 4368 /* There was no conflict. We're done laying out this field. */ 4369 break; 4370 } 4371 4372 /* Now that we know where it will be placed, update its 4373 BINFO_OFFSET. */ 4374 if (binfo && CLASS_TYPE_P (BINFO_TYPE (binfo))) 4375 /* Indirect virtual bases may have a nonzero BINFO_OFFSET at 4376 this point because their BINFO_OFFSET is copied from another 4377 hierarchy. Therefore, we may not need to add the entire 4378 OFFSET. */ 4379 propagate_binfo_offsets (binfo, 4380 size_diffop_loc (input_location, 4381 fold_convert (ssizetype, offset), 4382 fold_convert (ssizetype, 4383 BINFO_OFFSET (binfo)))); 4384 } 4385 4386 /* Returns true if TYPE is empty and OFFSET is nonzero. */ 4387 4388 static int 4389 empty_base_at_nonzero_offset_p (tree type, 4390 tree offset, 4391 splay_tree /*offsets*/) 4392 { 4393 return is_empty_class (type) && !integer_zerop (offset); 4394 } 4395 4396 /* Layout the empty base BINFO. EOC indicates the byte currently just 4397 past the end of the class, and should be correctly aligned for a 4398 class of the type indicated by BINFO; OFFSETS gives the offsets of 4399 the empty bases allocated so far. T is the most derived 4400 type. Return nonzero iff we added it at the end. */ 4401 4402 static bool 4403 layout_empty_base (record_layout_info rli, tree binfo, 4404 tree eoc, splay_tree offsets) 4405 { 4406 tree alignment; 4407 tree basetype = BINFO_TYPE (binfo); 4408 bool atend = false; 4409 4410 /* This routine should only be used for empty classes. */ 4411 gcc_assert (is_empty_class (basetype)); 4412 alignment = ssize_int (CLASSTYPE_ALIGN_UNIT (basetype)); 4413 4414 if (!integer_zerop (BINFO_OFFSET (binfo))) 4415 propagate_binfo_offsets 4416 (binfo, size_diffop_loc (input_location, 4417 size_zero_node, BINFO_OFFSET (binfo))); 4418 4419 /* This is an empty base class. We first try to put it at offset 4420 zero. */ 4421 if (layout_conflict_p (binfo, 4422 BINFO_OFFSET (binfo), 4423 offsets, 4424 /*vbases_p=*/0)) 4425 { 4426 /* That didn't work. Now, we move forward from the next 4427 available spot in the class. */ 4428 atend = true; 4429 propagate_binfo_offsets (binfo, fold_convert (ssizetype, eoc)); 4430 while (1) 4431 { 4432 if (!layout_conflict_p (binfo, 4433 BINFO_OFFSET (binfo), 4434 offsets, 4435 /*vbases_p=*/0)) 4436 /* We finally found a spot where there's no overlap. */ 4437 break; 4438 4439 /* There's overlap here, too. Bump along to the next spot. */ 4440 propagate_binfo_offsets (binfo, alignment); 4441 } 4442 } 4443 4444 if (CLASSTYPE_USER_ALIGN (basetype)) 4445 { 4446 rli->record_align = MAX (rli->record_align, CLASSTYPE_ALIGN (basetype)); 4447 if (warn_packed) 4448 rli->unpacked_align = MAX (rli->unpacked_align, CLASSTYPE_ALIGN (basetype)); 4449 TYPE_USER_ALIGN (rli->t) = 1; 4450 } 4451 4452 return atend; 4453 } 4454 4455 /* Layout the base given by BINFO in the class indicated by RLI. 4456 *BASE_ALIGN is a running maximum of the alignments of 4457 any base class. OFFSETS gives the location of empty base 4458 subobjects. T is the most derived type. Return nonzero if the new 4459 object cannot be nearly-empty. A new FIELD_DECL is inserted at 4460 *NEXT_FIELD, unless BINFO is for an empty base class. 4461 4462 Returns the location at which the next field should be inserted. */ 4463 4464 static tree * 4465 build_base_field (record_layout_info rli, tree binfo, 4466 splay_tree offsets, tree *next_field) 4467 { 4468 tree t = rli->t; 4469 tree basetype = BINFO_TYPE (binfo); 4470 4471 if (!COMPLETE_TYPE_P (basetype)) 4472 /* This error is now reported in xref_tag, thus giving better 4473 location information. */ 4474 return next_field; 4475 4476 /* Place the base class. */ 4477 if (!is_empty_class (basetype)) 4478 { 4479 tree decl; 4480 4481 /* The containing class is non-empty because it has a non-empty 4482 base class. */ 4483 CLASSTYPE_EMPTY_P (t) = 0; 4484 4485 /* Create the FIELD_DECL. */ 4486 decl = build_decl (input_location, 4487 FIELD_DECL, NULL_TREE, CLASSTYPE_AS_BASE (basetype)); 4488 DECL_ARTIFICIAL (decl) = 1; 4489 DECL_IGNORED_P (decl) = 1; 4490 DECL_FIELD_CONTEXT (decl) = t; 4491 if (CLASSTYPE_AS_BASE (basetype)) 4492 { 4493 DECL_SIZE (decl) = CLASSTYPE_SIZE (basetype); 4494 DECL_SIZE_UNIT (decl) = CLASSTYPE_SIZE_UNIT (basetype); 4495 DECL_ALIGN (decl) = CLASSTYPE_ALIGN (basetype); 4496 DECL_USER_ALIGN (decl) = CLASSTYPE_USER_ALIGN (basetype); 4497 DECL_MODE (decl) = TYPE_MODE (basetype); 4498 DECL_FIELD_IS_BASE (decl) = 1; 4499 4500 /* Try to place the field. It may take more than one try if we 4501 have a hard time placing the field without putting two 4502 objects of the same type at the same address. */ 4503 layout_nonempty_base_or_field (rli, decl, binfo, offsets); 4504 /* Add the new FIELD_DECL to the list of fields for T. */ 4505 DECL_CHAIN (decl) = *next_field; 4506 *next_field = decl; 4507 next_field = &DECL_CHAIN (decl); 4508 } 4509 } 4510 else 4511 { 4512 tree eoc; 4513 bool atend; 4514 4515 /* On some platforms (ARM), even empty classes will not be 4516 byte-aligned. */ 4517 eoc = round_up_loc (input_location, 4518 rli_size_unit_so_far (rli), 4519 CLASSTYPE_ALIGN_UNIT (basetype)); 4520 atend = layout_empty_base (rli, binfo, eoc, offsets); 4521 /* A nearly-empty class "has no proper base class that is empty, 4522 not morally virtual, and at an offset other than zero." */ 4523 if (!BINFO_VIRTUAL_P (binfo) && CLASSTYPE_NEARLY_EMPTY_P (t)) 4524 { 4525 if (atend) 4526 CLASSTYPE_NEARLY_EMPTY_P (t) = 0; 4527 /* The check above (used in G++ 3.2) is insufficient because 4528 an empty class placed at offset zero might itself have an 4529 empty base at a nonzero offset. */ 4530 else if (walk_subobject_offsets (basetype, 4531 empty_base_at_nonzero_offset_p, 4532 size_zero_node, 4533 /*offsets=*/NULL, 4534 /*max_offset=*/NULL_TREE, 4535 /*vbases_p=*/true)) 4536 CLASSTYPE_NEARLY_EMPTY_P (t) = 0; 4537 } 4538 4539 /* We do not create a FIELD_DECL for empty base classes because 4540 it might overlap some other field. We want to be able to 4541 create CONSTRUCTORs for the class by iterating over the 4542 FIELD_DECLs, and the back end does not handle overlapping 4543 FIELD_DECLs. */ 4544 4545 /* An empty virtual base causes a class to be non-empty 4546 -- but in that case we do not need to clear CLASSTYPE_EMPTY_P 4547 here because that was already done when the virtual table 4548 pointer was created. */ 4549 } 4550 4551 /* Record the offsets of BINFO and its base subobjects. */ 4552 record_subobject_offsets (binfo, 4553 BINFO_OFFSET (binfo), 4554 offsets, 4555 /*is_data_member=*/false); 4556 4557 return next_field; 4558 } 4559 4560 /* Layout all of the non-virtual base classes. Record empty 4561 subobjects in OFFSETS. T is the most derived type. Return nonzero 4562 if the type cannot be nearly empty. The fields created 4563 corresponding to the base classes will be inserted at 4564 *NEXT_FIELD. */ 4565 4566 static void 4567 build_base_fields (record_layout_info rli, 4568 splay_tree offsets, tree *next_field) 4569 { 4570 /* Chain to hold all the new FIELD_DECLs which stand in for base class 4571 subobjects. */ 4572 tree t = rli->t; 4573 int n_baseclasses = BINFO_N_BASE_BINFOS (TYPE_BINFO (t)); 4574 int i; 4575 4576 /* The primary base class is always allocated first. */ 4577 if (CLASSTYPE_HAS_PRIMARY_BASE_P (t)) 4578 next_field = build_base_field (rli, CLASSTYPE_PRIMARY_BINFO (t), 4579 offsets, next_field); 4580 4581 /* Now allocate the rest of the bases. */ 4582 for (i = 0; i < n_baseclasses; ++i) 4583 { 4584 tree base_binfo; 4585 4586 base_binfo = BINFO_BASE_BINFO (TYPE_BINFO (t), i); 4587 4588 /* The primary base was already allocated above, so we don't 4589 need to allocate it again here. */ 4590 if (base_binfo == CLASSTYPE_PRIMARY_BINFO (t)) 4591 continue; 4592 4593 /* Virtual bases are added at the end (a primary virtual base 4594 will have already been added). */ 4595 if (BINFO_VIRTUAL_P (base_binfo)) 4596 continue; 4597 4598 next_field = build_base_field (rli, base_binfo, 4599 offsets, next_field); 4600 } 4601 } 4602 4603 /* Go through the TYPE_METHODS of T issuing any appropriate 4604 diagnostics, figuring out which methods override which other 4605 methods, and so forth. */ 4606 4607 static void 4608 check_methods (tree t) 4609 { 4610 tree x; 4611 4612 for (x = TYPE_METHODS (t); x; x = DECL_CHAIN (x)) 4613 { 4614 check_for_override (x, t); 4615 if (DECL_PURE_VIRTUAL_P (x) && (TREE_CODE (x) != FUNCTION_DECL || ! DECL_VINDEX (x))) 4616 error ("initializer specified for non-virtual method %q+D", x); 4617 /* The name of the field is the original field name 4618 Save this in auxiliary field for later overloading. */ 4619 if (TREE_CODE (x) == FUNCTION_DECL && DECL_VINDEX (x)) 4620 { 4621 TYPE_POLYMORPHIC_P (t) = 1; 4622 if (DECL_PURE_VIRTUAL_P (x)) 4623 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (t), x); 4624 } 4625 /* All user-provided destructors are non-trivial. 4626 Constructors and assignment ops are handled in 4627 grok_special_member_properties. */ 4628 if (DECL_DESTRUCTOR_P (x) && user_provided_p (x)) 4629 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = 1; 4630 if (!DECL_VIRTUAL_P (x) 4631 && lookup_attribute ("transaction_safe_dynamic", DECL_ATTRIBUTES (x))) 4632 error_at (DECL_SOURCE_LOCATION (x), 4633 "%<transaction_safe_dynamic%> may only be specified for " 4634 "a virtual function"); 4635 } 4636 } 4637 4638 /* FN is a constructor or destructor. Clone the declaration to create 4639 a specialized in-charge or not-in-charge version, as indicated by 4640 NAME. */ 4641 4642 static tree 4643 build_clone (tree fn, tree name) 4644 { 4645 tree parms; 4646 tree clone; 4647 4648 /* Copy the function. */ 4649 clone = copy_decl (fn); 4650 /* Reset the function name. */ 4651 DECL_NAME (clone) = name; 4652 /* Remember where this function came from. */ 4653 DECL_ABSTRACT_ORIGIN (clone) = fn; 4654 /* Make it easy to find the CLONE given the FN. */ 4655 DECL_CHAIN (clone) = DECL_CHAIN (fn); 4656 DECL_CHAIN (fn) = clone; 4657 4658 /* If this is a template, do the rest on the DECL_TEMPLATE_RESULT. */ 4659 if (TREE_CODE (clone) == TEMPLATE_DECL) 4660 { 4661 tree result = build_clone (DECL_TEMPLATE_RESULT (clone), name); 4662 DECL_TEMPLATE_RESULT (clone) = result; 4663 DECL_TEMPLATE_INFO (result) = copy_node (DECL_TEMPLATE_INFO (result)); 4664 DECL_TI_TEMPLATE (result) = clone; 4665 TREE_TYPE (clone) = TREE_TYPE (result); 4666 return clone; 4667 } 4668 else 4669 { 4670 // Clone constraints. 4671 if (flag_concepts) 4672 if (tree ci = get_constraints (fn)) 4673 set_constraints (clone, copy_node (ci)); 4674 } 4675 4676 4677 SET_DECL_ASSEMBLER_NAME (clone, NULL_TREE); 4678 DECL_CLONED_FUNCTION (clone) = fn; 4679 /* There's no pending inline data for this function. */ 4680 DECL_PENDING_INLINE_INFO (clone) = NULL; 4681 DECL_PENDING_INLINE_P (clone) = 0; 4682 4683 /* The base-class destructor is not virtual. */ 4684 if (name == base_dtor_identifier) 4685 { 4686 DECL_VIRTUAL_P (clone) = 0; 4687 if (TREE_CODE (clone) != TEMPLATE_DECL) 4688 DECL_VINDEX (clone) = NULL_TREE; 4689 } 4690 4691 /* If there was an in-charge parameter, drop it from the function 4692 type. */ 4693 if (DECL_HAS_IN_CHARGE_PARM_P (clone)) 4694 { 4695 tree basetype; 4696 tree parmtypes; 4697 tree exceptions; 4698 4699 exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone)); 4700 basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone)); 4701 parmtypes = TYPE_ARG_TYPES (TREE_TYPE (clone)); 4702 /* Skip the `this' parameter. */ 4703 parmtypes = TREE_CHAIN (parmtypes); 4704 /* Skip the in-charge parameter. */ 4705 parmtypes = TREE_CHAIN (parmtypes); 4706 /* And the VTT parm, in a complete [cd]tor. */ 4707 if (DECL_HAS_VTT_PARM_P (fn) 4708 && ! DECL_NEEDS_VTT_PARM_P (clone)) 4709 parmtypes = TREE_CHAIN (parmtypes); 4710 /* If this is subobject constructor or destructor, add the vtt 4711 parameter. */ 4712 TREE_TYPE (clone) 4713 = build_method_type_directly (basetype, 4714 TREE_TYPE (TREE_TYPE (clone)), 4715 parmtypes); 4716 if (exceptions) 4717 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), 4718 exceptions); 4719 TREE_TYPE (clone) 4720 = cp_build_type_attribute_variant (TREE_TYPE (clone), 4721 TYPE_ATTRIBUTES (TREE_TYPE (fn))); 4722 } 4723 4724 /* Copy the function parameters. */ 4725 DECL_ARGUMENTS (clone) = copy_list (DECL_ARGUMENTS (clone)); 4726 /* Remove the in-charge parameter. */ 4727 if (DECL_HAS_IN_CHARGE_PARM_P (clone)) 4728 { 4729 DECL_CHAIN (DECL_ARGUMENTS (clone)) 4730 = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone))); 4731 DECL_HAS_IN_CHARGE_PARM_P (clone) = 0; 4732 } 4733 /* And the VTT parm, in a complete [cd]tor. */ 4734 if (DECL_HAS_VTT_PARM_P (fn)) 4735 { 4736 if (DECL_NEEDS_VTT_PARM_P (clone)) 4737 DECL_HAS_VTT_PARM_P (clone) = 1; 4738 else 4739 { 4740 DECL_CHAIN (DECL_ARGUMENTS (clone)) 4741 = DECL_CHAIN (DECL_CHAIN (DECL_ARGUMENTS (clone))); 4742 DECL_HAS_VTT_PARM_P (clone) = 0; 4743 } 4744 } 4745 4746 for (parms = DECL_ARGUMENTS (clone); parms; parms = DECL_CHAIN (parms)) 4747 { 4748 DECL_CONTEXT (parms) = clone; 4749 cxx_dup_lang_specific_decl (parms); 4750 } 4751 4752 /* Create the RTL for this function. */ 4753 SET_DECL_RTL (clone, NULL); 4754 rest_of_decl_compilation (clone, /*top_level=*/1, at_eof); 4755 4756 return clone; 4757 } 4758 4759 /* Implementation of DECL_CLONED_FUNCTION and DECL_CLONED_FUNCTION_P, do 4760 not invoke this function directly. 4761 4762 For a non-thunk function, returns the address of the slot for storing 4763 the function it is a clone of. Otherwise returns NULL_TREE. 4764 4765 If JUST_TESTING, looks through TEMPLATE_DECL and returns NULL if 4766 cloned_function is unset. This is to support the separate 4767 DECL_CLONED_FUNCTION and DECL_CLONED_FUNCTION_P modes; using the latter 4768 on a template makes sense, but not the former. */ 4769 4770 tree * 4771 decl_cloned_function_p (const_tree decl, bool just_testing) 4772 { 4773 tree *ptr; 4774 if (just_testing) 4775 decl = STRIP_TEMPLATE (decl); 4776 4777 if (TREE_CODE (decl) != FUNCTION_DECL 4778 || !DECL_LANG_SPECIFIC (decl) 4779 || DECL_LANG_SPECIFIC (decl)->u.fn.thunk_p) 4780 { 4781 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007) 4782 if (!just_testing) 4783 lang_check_failed (__FILE__, __LINE__, __FUNCTION__); 4784 else 4785 #endif 4786 return NULL; 4787 } 4788 4789 ptr = &DECL_LANG_SPECIFIC (decl)->u.fn.u5.cloned_function; 4790 if (just_testing && *ptr == NULL_TREE) 4791 return NULL; 4792 else 4793 return ptr; 4794 } 4795 4796 /* Produce declarations for all appropriate clones of FN. If 4797 UPDATE_METHOD_VEC_P is nonzero, the clones are added to the 4798 CLASTYPE_METHOD_VEC as well. */ 4799 4800 void 4801 clone_function_decl (tree fn, int update_method_vec_p) 4802 { 4803 tree clone; 4804 4805 /* Avoid inappropriate cloning. */ 4806 if (DECL_CHAIN (fn) 4807 && DECL_CLONED_FUNCTION_P (DECL_CHAIN (fn))) 4808 return; 4809 4810 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn)) 4811 { 4812 /* For each constructor, we need two variants: an in-charge version 4813 and a not-in-charge version. */ 4814 clone = build_clone (fn, complete_ctor_identifier); 4815 if (update_method_vec_p) 4816 add_method (DECL_CONTEXT (clone), clone, NULL_TREE); 4817 clone = build_clone (fn, base_ctor_identifier); 4818 if (update_method_vec_p) 4819 add_method (DECL_CONTEXT (clone), clone, NULL_TREE); 4820 } 4821 else 4822 { 4823 gcc_assert (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn)); 4824 4825 /* For each destructor, we need three variants: an in-charge 4826 version, a not-in-charge version, and an in-charge deleting 4827 version. We clone the deleting version first because that 4828 means it will go second on the TYPE_METHODS list -- and that 4829 corresponds to the correct layout order in the virtual 4830 function table. 4831 4832 For a non-virtual destructor, we do not build a deleting 4833 destructor. */ 4834 if (DECL_VIRTUAL_P (fn)) 4835 { 4836 clone = build_clone (fn, deleting_dtor_identifier); 4837 if (update_method_vec_p) 4838 add_method (DECL_CONTEXT (clone), clone, NULL_TREE); 4839 } 4840 clone = build_clone (fn, complete_dtor_identifier); 4841 if (update_method_vec_p) 4842 add_method (DECL_CONTEXT (clone), clone, NULL_TREE); 4843 clone = build_clone (fn, base_dtor_identifier); 4844 if (update_method_vec_p) 4845 add_method (DECL_CONTEXT (clone), clone, NULL_TREE); 4846 } 4847 4848 /* Note that this is an abstract function that is never emitted. */ 4849 DECL_ABSTRACT_P (fn) = true; 4850 } 4851 4852 /* DECL is an in charge constructor, which is being defined. This will 4853 have had an in class declaration, from whence clones were 4854 declared. An out-of-class definition can specify additional default 4855 arguments. As it is the clones that are involved in overload 4856 resolution, we must propagate the information from the DECL to its 4857 clones. */ 4858 4859 void 4860 adjust_clone_args (tree decl) 4861 { 4862 tree clone; 4863 4864 for (clone = DECL_CHAIN (decl); clone && DECL_CLONED_FUNCTION_P (clone); 4865 clone = DECL_CHAIN (clone)) 4866 { 4867 tree orig_clone_parms = TYPE_ARG_TYPES (TREE_TYPE (clone)); 4868 tree orig_decl_parms = TYPE_ARG_TYPES (TREE_TYPE (decl)); 4869 tree decl_parms, clone_parms; 4870 4871 clone_parms = orig_clone_parms; 4872 4873 /* Skip the 'this' parameter. */ 4874 orig_clone_parms = TREE_CHAIN (orig_clone_parms); 4875 orig_decl_parms = TREE_CHAIN (orig_decl_parms); 4876 4877 if (DECL_HAS_IN_CHARGE_PARM_P (decl)) 4878 orig_decl_parms = TREE_CHAIN (orig_decl_parms); 4879 if (DECL_HAS_VTT_PARM_P (decl)) 4880 orig_decl_parms = TREE_CHAIN (orig_decl_parms); 4881 4882 clone_parms = orig_clone_parms; 4883 if (DECL_HAS_VTT_PARM_P (clone)) 4884 clone_parms = TREE_CHAIN (clone_parms); 4885 4886 for (decl_parms = orig_decl_parms; decl_parms; 4887 decl_parms = TREE_CHAIN (decl_parms), 4888 clone_parms = TREE_CHAIN (clone_parms)) 4889 { 4890 gcc_assert (same_type_p (TREE_TYPE (decl_parms), 4891 TREE_TYPE (clone_parms))); 4892 4893 if (TREE_PURPOSE (decl_parms) && !TREE_PURPOSE (clone_parms)) 4894 { 4895 /* A default parameter has been added. Adjust the 4896 clone's parameters. */ 4897 tree exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone)); 4898 tree attrs = TYPE_ATTRIBUTES (TREE_TYPE (clone)); 4899 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone)); 4900 tree type; 4901 4902 clone_parms = orig_decl_parms; 4903 4904 if (DECL_HAS_VTT_PARM_P (clone)) 4905 { 4906 clone_parms = tree_cons (TREE_PURPOSE (orig_clone_parms), 4907 TREE_VALUE (orig_clone_parms), 4908 clone_parms); 4909 TREE_TYPE (clone_parms) = TREE_TYPE (orig_clone_parms); 4910 } 4911 type = build_method_type_directly (basetype, 4912 TREE_TYPE (TREE_TYPE (clone)), 4913 clone_parms); 4914 if (exceptions) 4915 type = build_exception_variant (type, exceptions); 4916 if (attrs) 4917 type = cp_build_type_attribute_variant (type, attrs); 4918 TREE_TYPE (clone) = type; 4919 4920 clone_parms = NULL_TREE; 4921 break; 4922 } 4923 } 4924 gcc_assert (!clone_parms); 4925 } 4926 } 4927 4928 /* For each of the constructors and destructors in T, create an 4929 in-charge and not-in-charge variant. */ 4930 4931 static void 4932 clone_constructors_and_destructors (tree t) 4933 { 4934 tree fns; 4935 4936 /* If for some reason we don't have a CLASSTYPE_METHOD_VEC, we bail 4937 out now. */ 4938 if (!CLASSTYPE_METHOD_VEC (t)) 4939 return; 4940 4941 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns)) 4942 clone_function_decl (OVL_CURRENT (fns), /*update_method_vec_p=*/1); 4943 for (fns = CLASSTYPE_DESTRUCTORS (t); fns; fns = OVL_NEXT (fns)) 4944 clone_function_decl (OVL_CURRENT (fns), /*update_method_vec_p=*/1); 4945 } 4946 4947 /* Deduce noexcept for a destructor DTOR. */ 4948 4949 void 4950 deduce_noexcept_on_destructor (tree dtor) 4951 { 4952 if (!TYPE_RAISES_EXCEPTIONS (TREE_TYPE (dtor))) 4953 { 4954 tree eh_spec = unevaluated_noexcept_spec (); 4955 TREE_TYPE (dtor) = build_exception_variant (TREE_TYPE (dtor), eh_spec); 4956 } 4957 } 4958 4959 /* For each destructor in T, deduce noexcept: 4960 4961 12.4/3: A declaration of a destructor that does not have an 4962 exception-specification is implicitly considered to have the 4963 same exception-specification as an implicit declaration (15.4). */ 4964 4965 static void 4966 deduce_noexcept_on_destructors (tree t) 4967 { 4968 /* If for some reason we don't have a CLASSTYPE_METHOD_VEC, we bail 4969 out now. */ 4970 if (!CLASSTYPE_METHOD_VEC (t)) 4971 return; 4972 4973 for (tree fns = CLASSTYPE_DESTRUCTORS (t); fns; fns = OVL_NEXT (fns)) 4974 deduce_noexcept_on_destructor (OVL_CURRENT (fns)); 4975 } 4976 4977 /* Subroutine of set_one_vmethod_tm_attributes. Search base classes 4978 of TYPE for virtual functions which FNDECL overrides. Return a 4979 mask of the tm attributes found therein. */ 4980 4981 static int 4982 look_for_tm_attr_overrides (tree type, tree fndecl) 4983 { 4984 tree binfo = TYPE_BINFO (type); 4985 tree base_binfo; 4986 int ix, found = 0; 4987 4988 for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ++ix) 4989 { 4990 tree o, basetype = BINFO_TYPE (base_binfo); 4991 4992 if (!TYPE_POLYMORPHIC_P (basetype)) 4993 continue; 4994 4995 o = look_for_overrides_here (basetype, fndecl); 4996 if (o) 4997 { 4998 if (lookup_attribute ("transaction_safe_dynamic", 4999 DECL_ATTRIBUTES (o))) 5000 /* transaction_safe_dynamic is not inherited. */; 5001 else 5002 found |= tm_attr_to_mask (find_tm_attribute 5003 (TYPE_ATTRIBUTES (TREE_TYPE (o)))); 5004 } 5005 else 5006 found |= look_for_tm_attr_overrides (basetype, fndecl); 5007 } 5008 5009 return found; 5010 } 5011 5012 /* Subroutine of set_method_tm_attributes. Handle the checks and 5013 inheritance for one virtual method FNDECL. */ 5014 5015 static void 5016 set_one_vmethod_tm_attributes (tree type, tree fndecl) 5017 { 5018 tree tm_attr; 5019 int found, have; 5020 5021 found = look_for_tm_attr_overrides (type, fndecl); 5022 5023 /* If FNDECL doesn't actually override anything (i.e. T is the 5024 class that first declares FNDECL virtual), then we're done. */ 5025 if (found == 0) 5026 return; 5027 5028 tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl))); 5029 have = tm_attr_to_mask (tm_attr); 5030 5031 /* Intel STM Language Extension 3.0, Section 4.2 table 4: 5032 tm_pure must match exactly, otherwise no weakening of 5033 tm_safe > tm_callable > nothing. */ 5034 /* ??? The tm_pure attribute didn't make the transition to the 5035 multivendor language spec. */ 5036 if (have == TM_ATTR_PURE) 5037 { 5038 if (found != TM_ATTR_PURE) 5039 { 5040 found &= -found; 5041 goto err_override; 5042 } 5043 } 5044 /* If the overridden function is tm_pure, then FNDECL must be. */ 5045 else if (found == TM_ATTR_PURE && tm_attr) 5046 goto err_override; 5047 /* Look for base class combinations that cannot be satisfied. */ 5048 else if (found != TM_ATTR_PURE && (found & TM_ATTR_PURE)) 5049 { 5050 found &= ~TM_ATTR_PURE; 5051 found &= -found; 5052 error_at (DECL_SOURCE_LOCATION (fndecl), 5053 "method overrides both %<transaction_pure%> and %qE methods", 5054 tm_mask_to_attr (found)); 5055 } 5056 /* If FNDECL did not declare an attribute, then inherit the most 5057 restrictive one. */ 5058 else if (tm_attr == NULL) 5059 { 5060 apply_tm_attr (fndecl, tm_mask_to_attr (found & -found)); 5061 } 5062 /* Otherwise validate that we're not weaker than a function 5063 that is being overridden. */ 5064 else 5065 { 5066 found &= -found; 5067 if (found <= TM_ATTR_CALLABLE && have > found) 5068 goto err_override; 5069 } 5070 return; 5071 5072 err_override: 5073 error_at (DECL_SOURCE_LOCATION (fndecl), 5074 "method declared %qE overriding %qE method", 5075 tm_attr, tm_mask_to_attr (found)); 5076 } 5077 5078 /* For each of the methods in T, propagate a class-level tm attribute. */ 5079 5080 static void 5081 set_method_tm_attributes (tree t) 5082 { 5083 tree class_tm_attr, fndecl; 5084 5085 /* Don't bother collecting tm attributes if transactional memory 5086 support is not enabled. */ 5087 if (!flag_tm) 5088 return; 5089 5090 /* Process virtual methods first, as they inherit directly from the 5091 base virtual function and also require validation of new attributes. */ 5092 if (TYPE_CONTAINS_VPTR_P (t)) 5093 { 5094 tree vchain; 5095 for (vchain = BINFO_VIRTUALS (TYPE_BINFO (t)); vchain; 5096 vchain = TREE_CHAIN (vchain)) 5097 { 5098 fndecl = BV_FN (vchain); 5099 if (DECL_THUNK_P (fndecl)) 5100 fndecl = THUNK_TARGET (fndecl); 5101 set_one_vmethod_tm_attributes (t, fndecl); 5102 } 5103 } 5104 5105 /* If the class doesn't have an attribute, nothing more to do. */ 5106 class_tm_attr = find_tm_attribute (TYPE_ATTRIBUTES (t)); 5107 if (class_tm_attr == NULL) 5108 return; 5109 5110 /* Any method that does not yet have a tm attribute inherits 5111 the one from the class. */ 5112 for (fndecl = TYPE_METHODS (t); fndecl; fndecl = TREE_CHAIN (fndecl)) 5113 { 5114 if (!find_tm_attribute (TYPE_ATTRIBUTES (TREE_TYPE (fndecl)))) 5115 apply_tm_attr (fndecl, class_tm_attr); 5116 } 5117 } 5118 5119 /* Returns true iff class T has a user-defined constructor other than 5120 the default constructor. */ 5121 5122 bool 5123 type_has_user_nondefault_constructor (tree t) 5124 { 5125 tree fns; 5126 5127 if (!TYPE_HAS_USER_CONSTRUCTOR (t)) 5128 return false; 5129 5130 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns)) 5131 { 5132 tree fn = OVL_CURRENT (fns); 5133 if (!DECL_ARTIFICIAL (fn) 5134 && (TREE_CODE (fn) == TEMPLATE_DECL 5135 || (skip_artificial_parms_for (fn, DECL_ARGUMENTS (fn)) 5136 != NULL_TREE))) 5137 return true; 5138 } 5139 5140 return false; 5141 } 5142 5143 /* Returns the defaulted constructor if T has one. Otherwise, returns 5144 NULL_TREE. */ 5145 5146 tree 5147 in_class_defaulted_default_constructor (tree t) 5148 { 5149 tree fns, args; 5150 5151 if (!TYPE_HAS_USER_CONSTRUCTOR (t)) 5152 return NULL_TREE; 5153 5154 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns)) 5155 { 5156 tree fn = OVL_CURRENT (fns); 5157 5158 if (DECL_DEFAULTED_IN_CLASS_P (fn)) 5159 { 5160 args = FUNCTION_FIRST_USER_PARMTYPE (fn); 5161 while (args && TREE_PURPOSE (args)) 5162 args = TREE_CHAIN (args); 5163 if (!args || args == void_list_node) 5164 return fn; 5165 } 5166 } 5167 5168 return NULL_TREE; 5169 } 5170 5171 /* Returns true iff FN is a user-provided function, i.e. user-declared 5172 and not defaulted at its first declaration. */ 5173 5174 bool 5175 user_provided_p (tree fn) 5176 { 5177 if (TREE_CODE (fn) == TEMPLATE_DECL) 5178 return true; 5179 else 5180 return (!DECL_ARTIFICIAL (fn) 5181 && !(DECL_INITIALIZED_IN_CLASS_P (fn) 5182 && (DECL_DEFAULTED_FN (fn) || DECL_DELETED_FN (fn)))); 5183 } 5184 5185 /* Returns true iff class T has a user-provided constructor. */ 5186 5187 bool 5188 type_has_user_provided_constructor (tree t) 5189 { 5190 tree fns; 5191 5192 if (!CLASS_TYPE_P (t)) 5193 return false; 5194 5195 if (!TYPE_HAS_USER_CONSTRUCTOR (t)) 5196 return false; 5197 5198 /* This can happen in error cases; avoid crashing. */ 5199 if (!CLASSTYPE_METHOD_VEC (t)) 5200 return false; 5201 5202 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns)) 5203 if (user_provided_p (OVL_CURRENT (fns))) 5204 return true; 5205 5206 return false; 5207 } 5208 5209 /* Returns true iff class T has a user-provided or explicit constructor. */ 5210 5211 bool 5212 type_has_user_provided_or_explicit_constructor (tree t) 5213 { 5214 tree fns; 5215 5216 if (!CLASS_TYPE_P (t)) 5217 return false; 5218 5219 if (!TYPE_HAS_USER_CONSTRUCTOR (t)) 5220 return false; 5221 5222 /* This can happen in error cases; avoid crashing. */ 5223 if (!CLASSTYPE_METHOD_VEC (t)) 5224 return false; 5225 5226 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns)) 5227 { 5228 tree fn = OVL_CURRENT (fns); 5229 if (user_provided_p (fn) || DECL_NONCONVERTING_P (fn)) 5230 return true; 5231 } 5232 5233 return false; 5234 } 5235 5236 /* Returns true iff class T has a non-user-provided (i.e. implicitly 5237 declared or explicitly defaulted in the class body) default 5238 constructor. */ 5239 5240 bool 5241 type_has_non_user_provided_default_constructor (tree t) 5242 { 5243 tree fns; 5244 5245 if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (t)) 5246 return false; 5247 if (CLASSTYPE_LAZY_DEFAULT_CTOR (t)) 5248 return true; 5249 5250 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns)) 5251 { 5252 tree fn = OVL_CURRENT (fns); 5253 if (TREE_CODE (fn) == FUNCTION_DECL 5254 && !user_provided_p (fn) 5255 && sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (fn))) 5256 return true; 5257 } 5258 5259 return false; 5260 } 5261 5262 /* TYPE is being used as a virtual base, and has a non-trivial move 5263 assignment. Return true if this is due to there being a user-provided 5264 move assignment in TYPE or one of its subobjects; if there isn't, then 5265 multiple move assignment can't cause any harm. */ 5266 5267 bool 5268 vbase_has_user_provided_move_assign (tree type) 5269 { 5270 /* Does the type itself have a user-provided move assignment operator? */ 5271 for (tree fns 5272 = lookup_fnfields_slot_nolazy (type, ansi_assopname (NOP_EXPR)); 5273 fns; fns = OVL_NEXT (fns)) 5274 { 5275 tree fn = OVL_CURRENT (fns); 5276 if (move_fn_p (fn) && user_provided_p (fn)) 5277 return true; 5278 } 5279 5280 /* Do any of its bases? */ 5281 tree binfo = TYPE_BINFO (type); 5282 tree base_binfo; 5283 for (int i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i) 5284 if (vbase_has_user_provided_move_assign (BINFO_TYPE (base_binfo))) 5285 return true; 5286 5287 /* Or non-static data members? */ 5288 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field)) 5289 { 5290 if (TREE_CODE (field) == FIELD_DECL 5291 && CLASS_TYPE_P (TREE_TYPE (field)) 5292 && vbase_has_user_provided_move_assign (TREE_TYPE (field))) 5293 return true; 5294 } 5295 5296 /* Seems not. */ 5297 return false; 5298 } 5299 5300 /* If default-initialization leaves part of TYPE uninitialized, returns 5301 a DECL for the field or TYPE itself (DR 253). */ 5302 5303 tree 5304 default_init_uninitialized_part (tree type) 5305 { 5306 tree t, r, binfo; 5307 int i; 5308 5309 type = strip_array_types (type); 5310 if (!CLASS_TYPE_P (type)) 5311 return type; 5312 if (!type_has_non_user_provided_default_constructor (type)) 5313 return NULL_TREE; 5314 for (binfo = TYPE_BINFO (type), i = 0; 5315 BINFO_BASE_ITERATE (binfo, i, t); ++i) 5316 { 5317 r = default_init_uninitialized_part (BINFO_TYPE (t)); 5318 if (r) 5319 return r; 5320 } 5321 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t)) 5322 if (TREE_CODE (t) == FIELD_DECL 5323 && !DECL_ARTIFICIAL (t) 5324 && !DECL_INITIAL (t)) 5325 { 5326 r = default_init_uninitialized_part (TREE_TYPE (t)); 5327 if (r) 5328 return DECL_P (r) ? r : t; 5329 } 5330 5331 return NULL_TREE; 5332 } 5333 5334 /* Returns true iff for class T, a trivial synthesized default constructor 5335 would be constexpr. */ 5336 5337 bool 5338 trivial_default_constructor_is_constexpr (tree t) 5339 { 5340 /* A defaulted trivial default constructor is constexpr 5341 if there is nothing to initialize. */ 5342 gcc_assert (!TYPE_HAS_COMPLEX_DFLT (t)); 5343 return is_really_empty_class (t); 5344 } 5345 5346 /* Returns true iff class T has a constexpr default constructor. */ 5347 5348 bool 5349 type_has_constexpr_default_constructor (tree t) 5350 { 5351 tree fns; 5352 5353 if (!CLASS_TYPE_P (t)) 5354 { 5355 /* The caller should have stripped an enclosing array. */ 5356 gcc_assert (TREE_CODE (t) != ARRAY_TYPE); 5357 return false; 5358 } 5359 if (CLASSTYPE_LAZY_DEFAULT_CTOR (t)) 5360 { 5361 if (!TYPE_HAS_COMPLEX_DFLT (t)) 5362 return trivial_default_constructor_is_constexpr (t); 5363 /* Non-trivial, we need to check subobject constructors. */ 5364 lazily_declare_fn (sfk_constructor, t); 5365 } 5366 fns = locate_ctor (t); 5367 return (fns && DECL_DECLARED_CONSTEXPR_P (fns)); 5368 } 5369 5370 /* Returns true iff class T has a constexpr default constructor or has an 5371 implicitly declared default constructor that we can't tell if it's constexpr 5372 without forcing a lazy declaration (which might cause undesired 5373 instantiations). */ 5374 5375 bool 5376 type_maybe_constexpr_default_constructor (tree t) 5377 { 5378 if (CLASS_TYPE_P (t) && CLASSTYPE_LAZY_DEFAULT_CTOR (t) 5379 && TYPE_HAS_COMPLEX_DFLT (t)) 5380 /* Assume it's constexpr. */ 5381 return true; 5382 return type_has_constexpr_default_constructor (t); 5383 } 5384 5385 /* Returns true iff class TYPE has a virtual destructor. */ 5386 5387 bool 5388 type_has_virtual_destructor (tree type) 5389 { 5390 tree dtor; 5391 5392 if (!CLASS_TYPE_P (type)) 5393 return false; 5394 5395 gcc_assert (COMPLETE_TYPE_P (type)); 5396 dtor = CLASSTYPE_DESTRUCTORS (type); 5397 return (dtor && DECL_VIRTUAL_P (dtor)); 5398 } 5399 5400 /* Returns true iff class T has a move constructor. */ 5401 5402 bool 5403 type_has_move_constructor (tree t) 5404 { 5405 tree fns; 5406 5407 if (CLASSTYPE_LAZY_MOVE_CTOR (t)) 5408 { 5409 gcc_assert (COMPLETE_TYPE_P (t)); 5410 lazily_declare_fn (sfk_move_constructor, t); 5411 } 5412 5413 if (!CLASSTYPE_METHOD_VEC (t)) 5414 return false; 5415 5416 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns)) 5417 if (move_fn_p (OVL_CURRENT (fns))) 5418 return true; 5419 5420 return false; 5421 } 5422 5423 /* Returns true iff class T has a move assignment operator. */ 5424 5425 bool 5426 type_has_move_assign (tree t) 5427 { 5428 tree fns; 5429 5430 if (CLASSTYPE_LAZY_MOVE_ASSIGN (t)) 5431 { 5432 gcc_assert (COMPLETE_TYPE_P (t)); 5433 lazily_declare_fn (sfk_move_assignment, t); 5434 } 5435 5436 for (fns = lookup_fnfields_slot_nolazy (t, ansi_assopname (NOP_EXPR)); 5437 fns; fns = OVL_NEXT (fns)) 5438 if (move_fn_p (OVL_CURRENT (fns))) 5439 return true; 5440 5441 return false; 5442 } 5443 5444 /* Returns true iff class T has a move constructor that was explicitly 5445 declared in the class body. Note that this is different from 5446 "user-provided", which doesn't include functions that are defaulted in 5447 the class. */ 5448 5449 bool 5450 type_has_user_declared_move_constructor (tree t) 5451 { 5452 tree fns; 5453 5454 if (CLASSTYPE_LAZY_MOVE_CTOR (t)) 5455 return false; 5456 5457 if (!CLASSTYPE_METHOD_VEC (t)) 5458 return false; 5459 5460 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns)) 5461 { 5462 tree fn = OVL_CURRENT (fns); 5463 if (move_fn_p (fn) && !DECL_ARTIFICIAL (fn)) 5464 return true; 5465 } 5466 5467 return false; 5468 } 5469 5470 /* Returns true iff class T has a move assignment operator that was 5471 explicitly declared in the class body. */ 5472 5473 bool 5474 type_has_user_declared_move_assign (tree t) 5475 { 5476 tree fns; 5477 5478 if (CLASSTYPE_LAZY_MOVE_ASSIGN (t)) 5479 return false; 5480 5481 for (fns = lookup_fnfields_slot_nolazy (t, ansi_assopname (NOP_EXPR)); 5482 fns; fns = OVL_NEXT (fns)) 5483 { 5484 tree fn = OVL_CURRENT (fns); 5485 if (move_fn_p (fn) && !DECL_ARTIFICIAL (fn)) 5486 return true; 5487 } 5488 5489 return false; 5490 } 5491 5492 /* Nonzero if we need to build up a constructor call when initializing an 5493 object of this class, either because it has a user-declared constructor 5494 or because it doesn't have a default constructor (so we need to give an 5495 error if no initializer is provided). Use TYPE_NEEDS_CONSTRUCTING when 5496 what you care about is whether or not an object can be produced by a 5497 constructor (e.g. so we don't set TREE_READONLY on const variables of 5498 such type); use this function when what you care about is whether or not 5499 to try to call a constructor to create an object. The latter case is 5500 the former plus some cases of constructors that cannot be called. */ 5501 5502 bool 5503 type_build_ctor_call (tree t) 5504 { 5505 tree inner; 5506 if (TYPE_NEEDS_CONSTRUCTING (t)) 5507 return true; 5508 inner = strip_array_types (t); 5509 if (!CLASS_TYPE_P (inner) || ANON_AGGR_TYPE_P (inner)) 5510 return false; 5511 if (!TYPE_HAS_DEFAULT_CONSTRUCTOR (inner)) 5512 return true; 5513 if (cxx_dialect < cxx11) 5514 return false; 5515 /* A user-declared constructor might be private, and a constructor might 5516 be trivial but deleted. */ 5517 for (tree fns = lookup_fnfields_slot (inner, complete_ctor_identifier); 5518 fns; fns = OVL_NEXT (fns)) 5519 { 5520 tree fn = OVL_CURRENT (fns); 5521 if (!DECL_ARTIFICIAL (fn) 5522 || DECL_DELETED_FN (fn)) 5523 return true; 5524 } 5525 return false; 5526 } 5527 5528 /* Like type_build_ctor_call, but for destructors. */ 5529 5530 bool 5531 type_build_dtor_call (tree t) 5532 { 5533 tree inner; 5534 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)) 5535 return true; 5536 inner = strip_array_types (t); 5537 if (!CLASS_TYPE_P (inner) || ANON_AGGR_TYPE_P (inner) 5538 || !COMPLETE_TYPE_P (inner)) 5539 return false; 5540 if (cxx_dialect < cxx11) 5541 return false; 5542 /* A user-declared destructor might be private, and a destructor might 5543 be trivial but deleted. */ 5544 for (tree fns = lookup_fnfields_slot (inner, complete_dtor_identifier); 5545 fns; fns = OVL_NEXT (fns)) 5546 { 5547 tree fn = OVL_CURRENT (fns); 5548 if (!DECL_ARTIFICIAL (fn) 5549 || DECL_DELETED_FN (fn)) 5550 return true; 5551 } 5552 return false; 5553 } 5554 5555 /* Remove all zero-width bit-fields from T. */ 5556 5557 static void 5558 remove_zero_width_bit_fields (tree t) 5559 { 5560 tree *fieldsp; 5561 5562 fieldsp = &TYPE_FIELDS (t); 5563 while (*fieldsp) 5564 { 5565 if (TREE_CODE (*fieldsp) == FIELD_DECL 5566 && DECL_C_BIT_FIELD (*fieldsp) 5567 /* We should not be confused by the fact that grokbitfield 5568 temporarily sets the width of the bit field into 5569 DECL_INITIAL (*fieldsp). 5570 check_bitfield_decl eventually sets DECL_SIZE (*fieldsp) 5571 to that width. */ 5572 && (DECL_SIZE (*fieldsp) == NULL_TREE 5573 || integer_zerop (DECL_SIZE (*fieldsp)))) 5574 *fieldsp = DECL_CHAIN (*fieldsp); 5575 else 5576 fieldsp = &DECL_CHAIN (*fieldsp); 5577 } 5578 } 5579 5580 /* Returns TRUE iff we need a cookie when dynamically allocating an 5581 array whose elements have the indicated class TYPE. */ 5582 5583 static bool 5584 type_requires_array_cookie (tree type) 5585 { 5586 tree fns; 5587 bool has_two_argument_delete_p = false; 5588 5589 gcc_assert (CLASS_TYPE_P (type)); 5590 5591 /* If there's a non-trivial destructor, we need a cookie. In order 5592 to iterate through the array calling the destructor for each 5593 element, we'll have to know how many elements there are. */ 5594 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)) 5595 return true; 5596 5597 /* If the usual deallocation function is a two-argument whose second 5598 argument is of type `size_t', then we have to pass the size of 5599 the array to the deallocation function, so we will need to store 5600 a cookie. */ 5601 fns = lookup_fnfields (TYPE_BINFO (type), 5602 ansi_opname (VEC_DELETE_EXPR), 5603 /*protect=*/0); 5604 /* If there are no `operator []' members, or the lookup is 5605 ambiguous, then we don't need a cookie. */ 5606 if (!fns || fns == error_mark_node) 5607 return false; 5608 /* Loop through all of the functions. */ 5609 for (fns = BASELINK_FUNCTIONS (fns); fns; fns = OVL_NEXT (fns)) 5610 { 5611 tree fn; 5612 tree second_parm; 5613 5614 /* Select the current function. */ 5615 fn = OVL_CURRENT (fns); 5616 /* See if this function is a one-argument delete function. If 5617 it is, then it will be the usual deallocation function. */ 5618 second_parm = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fn))); 5619 if (second_parm == void_list_node) 5620 return false; 5621 /* Do not consider this function if its second argument is an 5622 ellipsis. */ 5623 if (!second_parm) 5624 continue; 5625 /* Otherwise, if we have a two-argument function and the second 5626 argument is `size_t', it will be the usual deallocation 5627 function -- unless there is one-argument function, too. */ 5628 if (TREE_CHAIN (second_parm) == void_list_node 5629 && same_type_p (TREE_VALUE (second_parm), size_type_node)) 5630 has_two_argument_delete_p = true; 5631 } 5632 5633 return has_two_argument_delete_p; 5634 } 5635 5636 /* Finish computing the `literal type' property of class type T. 5637 5638 At this point, we have already processed base classes and 5639 non-static data members. We need to check whether the copy 5640 constructor is trivial, the destructor is trivial, and there 5641 is a trivial default constructor or at least one constexpr 5642 constructor other than the copy constructor. */ 5643 5644 static void 5645 finalize_literal_type_property (tree t) 5646 { 5647 tree fn; 5648 5649 if (cxx_dialect < cxx11 5650 || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)) 5651 CLASSTYPE_LITERAL_P (t) = false; 5652 else if (CLASSTYPE_LITERAL_P (t) && !TYPE_HAS_TRIVIAL_DFLT (t) 5653 && CLASSTYPE_NON_AGGREGATE (t) 5654 && !TYPE_HAS_CONSTEXPR_CTOR (t)) 5655 CLASSTYPE_LITERAL_P (t) = false; 5656 5657 if (!CLASSTYPE_LITERAL_P (t)) 5658 for (fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn)) 5659 if (DECL_DECLARED_CONSTEXPR_P (fn) 5660 && TREE_CODE (fn) != TEMPLATE_DECL 5661 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn) 5662 && !DECL_CONSTRUCTOR_P (fn)) 5663 { 5664 DECL_DECLARED_CONSTEXPR_P (fn) = false; 5665 if (!DECL_GENERATED_P (fn)) 5666 { 5667 error ("enclosing class of constexpr non-static member " 5668 "function %q+#D is not a literal type", fn); 5669 explain_non_literal_class (t); 5670 } 5671 } 5672 } 5673 5674 /* T is a non-literal type used in a context which requires a constant 5675 expression. Explain why it isn't literal. */ 5676 5677 void 5678 explain_non_literal_class (tree t) 5679 { 5680 static hash_set<tree> *diagnosed; 5681 5682 if (!CLASS_TYPE_P (t)) 5683 return; 5684 t = TYPE_MAIN_VARIANT (t); 5685 5686 if (diagnosed == NULL) 5687 diagnosed = new hash_set<tree>; 5688 if (diagnosed->add (t)) 5689 /* Already explained. */ 5690 return; 5691 5692 inform (0, "%q+T is not literal because:", t); 5693 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)) 5694 inform (0, " %q+T has a non-trivial destructor", t); 5695 else if (CLASSTYPE_NON_AGGREGATE (t) 5696 && !TYPE_HAS_TRIVIAL_DFLT (t) 5697 && !TYPE_HAS_CONSTEXPR_CTOR (t)) 5698 { 5699 inform (0, " %q+T is not an aggregate, does not have a trivial " 5700 "default constructor, and has no constexpr constructor that " 5701 "is not a copy or move constructor", t); 5702 if (type_has_non_user_provided_default_constructor (t)) 5703 { 5704 /* Note that we can't simply call locate_ctor because when the 5705 constructor is deleted it just returns NULL_TREE. */ 5706 tree fns; 5707 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns)) 5708 { 5709 tree fn = OVL_CURRENT (fns); 5710 tree parms = TYPE_ARG_TYPES (TREE_TYPE (fn)); 5711 5712 parms = skip_artificial_parms_for (fn, parms); 5713 5714 if (sufficient_parms_p (parms)) 5715 { 5716 if (DECL_DELETED_FN (fn)) 5717 maybe_explain_implicit_delete (fn); 5718 else 5719 explain_invalid_constexpr_fn (fn); 5720 break; 5721 } 5722 } 5723 } 5724 } 5725 else 5726 { 5727 tree binfo, base_binfo, field; int i; 5728 for (binfo = TYPE_BINFO (t), i = 0; 5729 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++) 5730 { 5731 tree basetype = TREE_TYPE (base_binfo); 5732 if (!CLASSTYPE_LITERAL_P (basetype)) 5733 { 5734 inform (0, " base class %qT of %q+T is non-literal", 5735 basetype, t); 5736 explain_non_literal_class (basetype); 5737 return; 5738 } 5739 } 5740 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field)) 5741 { 5742 tree ftype; 5743 if (TREE_CODE (field) != FIELD_DECL) 5744 continue; 5745 ftype = TREE_TYPE (field); 5746 if (!literal_type_p (ftype)) 5747 { 5748 inform (DECL_SOURCE_LOCATION (field), 5749 " non-static data member %qD has non-literal type", 5750 field); 5751 if (CLASS_TYPE_P (ftype)) 5752 explain_non_literal_class (ftype); 5753 } 5754 if (CP_TYPE_VOLATILE_P (ftype)) 5755 inform (DECL_SOURCE_LOCATION (field), 5756 " non-static data member %qD has volatile type", field); 5757 } 5758 } 5759 } 5760 5761 /* Check the validity of the bases and members declared in T. Add any 5762 implicitly-generated functions (like copy-constructors and 5763 assignment operators). Compute various flag bits (like 5764 CLASSTYPE_NON_LAYOUT_POD_T) for T. This routine works purely at the C++ 5765 level: i.e., independently of the ABI in use. */ 5766 5767 static void 5768 check_bases_and_members (tree t) 5769 { 5770 /* Nonzero if the implicitly generated copy constructor should take 5771 a non-const reference argument. */ 5772 int cant_have_const_ctor; 5773 /* Nonzero if the implicitly generated assignment operator 5774 should take a non-const reference argument. */ 5775 int no_const_asn_ref; 5776 tree access_decls; 5777 bool saved_complex_asn_ref; 5778 bool saved_nontrivial_dtor; 5779 tree fn; 5780 5781 /* By default, we use const reference arguments and generate default 5782 constructors. */ 5783 cant_have_const_ctor = 0; 5784 no_const_asn_ref = 0; 5785 5786 /* Check all the base-classes and set FMEM members to point to arrays 5787 of potential interest. */ 5788 check_bases (t, &cant_have_const_ctor, &no_const_asn_ref); 5789 5790 /* Deduce noexcept on destructors. This needs to happen after we've set 5791 triviality flags appropriately for our bases. */ 5792 if (cxx_dialect >= cxx11) 5793 deduce_noexcept_on_destructors (t); 5794 5795 /* Check all the method declarations. */ 5796 check_methods (t); 5797 5798 /* Save the initial values of these flags which only indicate whether 5799 or not the class has user-provided functions. As we analyze the 5800 bases and members we can set these flags for other reasons. */ 5801 saved_complex_asn_ref = TYPE_HAS_COMPLEX_COPY_ASSIGN (t); 5802 saved_nontrivial_dtor = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t); 5803 5804 /* Check all the data member declarations. We cannot call 5805 check_field_decls until we have called check_bases check_methods, 5806 as check_field_decls depends on TYPE_HAS_NONTRIVIAL_DESTRUCTOR 5807 being set appropriately. */ 5808 check_field_decls (t, &access_decls, 5809 &cant_have_const_ctor, 5810 &no_const_asn_ref); 5811 5812 /* A nearly-empty class has to be vptr-containing; a nearly empty 5813 class contains just a vptr. */ 5814 if (!TYPE_CONTAINS_VPTR_P (t)) 5815 CLASSTYPE_NEARLY_EMPTY_P (t) = 0; 5816 5817 /* Do some bookkeeping that will guide the generation of implicitly 5818 declared member functions. */ 5819 TYPE_HAS_COMPLEX_COPY_CTOR (t) |= TYPE_CONTAINS_VPTR_P (t); 5820 TYPE_HAS_COMPLEX_MOVE_CTOR (t) |= TYPE_CONTAINS_VPTR_P (t); 5821 /* We need to call a constructor for this class if it has a 5822 user-provided constructor, or if the default constructor is going 5823 to initialize the vptr. (This is not an if-and-only-if; 5824 TYPE_NEEDS_CONSTRUCTING is set elsewhere if bases or members 5825 themselves need constructing.) */ 5826 TYPE_NEEDS_CONSTRUCTING (t) 5827 |= (type_has_user_provided_constructor (t) || TYPE_CONTAINS_VPTR_P (t)); 5828 /* [dcl.init.aggr] 5829 5830 An aggregate is an array or a class with no user-provided 5831 constructors ... and no virtual functions. 5832 5833 Again, other conditions for being an aggregate are checked 5834 elsewhere. */ 5835 CLASSTYPE_NON_AGGREGATE (t) 5836 |= (type_has_user_provided_or_explicit_constructor (t) 5837 || TYPE_POLYMORPHIC_P (t)); 5838 /* This is the C++98/03 definition of POD; it changed in C++0x, but we 5839 retain the old definition internally for ABI reasons. */ 5840 CLASSTYPE_NON_LAYOUT_POD_P (t) 5841 |= (CLASSTYPE_NON_AGGREGATE (t) 5842 || saved_nontrivial_dtor || saved_complex_asn_ref); 5843 CLASSTYPE_NON_STD_LAYOUT (t) |= TYPE_CONTAINS_VPTR_P (t); 5844 TYPE_HAS_COMPLEX_COPY_ASSIGN (t) |= TYPE_CONTAINS_VPTR_P (t); 5845 TYPE_HAS_COMPLEX_MOVE_ASSIGN (t) |= TYPE_CONTAINS_VPTR_P (t); 5846 TYPE_HAS_COMPLEX_DFLT (t) |= TYPE_CONTAINS_VPTR_P (t); 5847 5848 /* If the only explicitly declared default constructor is user-provided, 5849 set TYPE_HAS_COMPLEX_DFLT. */ 5850 if (!TYPE_HAS_COMPLEX_DFLT (t) 5851 && TYPE_HAS_DEFAULT_CONSTRUCTOR (t) 5852 && !type_has_non_user_provided_default_constructor (t)) 5853 TYPE_HAS_COMPLEX_DFLT (t) = true; 5854 5855 /* Warn if a public base of a polymorphic type has an accessible 5856 non-virtual destructor. It is only now that we know the class is 5857 polymorphic. Although a polymorphic base will have a already 5858 been diagnosed during its definition, we warn on use too. */ 5859 if (TYPE_POLYMORPHIC_P (t) && warn_nonvdtor) 5860 { 5861 tree binfo = TYPE_BINFO (t); 5862 vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo); 5863 tree base_binfo; 5864 unsigned i; 5865 5866 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++) 5867 { 5868 tree basetype = TREE_TYPE (base_binfo); 5869 5870 if ((*accesses)[i] == access_public_node 5871 && (TYPE_POLYMORPHIC_P (basetype) || warn_ecpp) 5872 && accessible_nvdtor_p (basetype)) 5873 warning (OPT_Wnon_virtual_dtor, 5874 "base class %q#T has accessible non-virtual destructor", 5875 basetype); 5876 } 5877 } 5878 5879 /* If the class has no user-declared constructor, but does have 5880 non-static const or reference data members that can never be 5881 initialized, issue a warning. */ 5882 if (warn_uninitialized 5883 /* Classes with user-declared constructors are presumed to 5884 initialize these members. */ 5885 && !TYPE_HAS_USER_CONSTRUCTOR (t) 5886 /* Aggregates can be initialized with brace-enclosed 5887 initializers. */ 5888 && CLASSTYPE_NON_AGGREGATE (t)) 5889 { 5890 tree field; 5891 5892 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field)) 5893 { 5894 tree type; 5895 5896 if (TREE_CODE (field) != FIELD_DECL 5897 || DECL_INITIAL (field) != NULL_TREE) 5898 continue; 5899 5900 type = TREE_TYPE (field); 5901 if (TREE_CODE (type) == REFERENCE_TYPE) 5902 warning_at (DECL_SOURCE_LOCATION (field), 5903 OPT_Wuninitialized, "non-static reference %q#D " 5904 "in class without a constructor", field); 5905 else if (CP_TYPE_CONST_P (type) 5906 && (!CLASS_TYPE_P (type) 5907 || !TYPE_HAS_DEFAULT_CONSTRUCTOR (type))) 5908 warning_at (DECL_SOURCE_LOCATION (field), 5909 OPT_Wuninitialized, "non-static const member %q#D " 5910 "in class without a constructor", field); 5911 } 5912 } 5913 5914 /* Synthesize any needed methods. */ 5915 add_implicitly_declared_members (t, &access_decls, 5916 cant_have_const_ctor, 5917 no_const_asn_ref); 5918 5919 /* Check defaulted declarations here so we have cant_have_const_ctor 5920 and don't need to worry about clones. */ 5921 for (fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn)) 5922 if (!DECL_ARTIFICIAL (fn) && DECL_DEFAULTED_IN_CLASS_P (fn)) 5923 { 5924 int copy = copy_fn_p (fn); 5925 if (copy > 0) 5926 { 5927 bool imp_const_p 5928 = (DECL_CONSTRUCTOR_P (fn) ? !cant_have_const_ctor 5929 : !no_const_asn_ref); 5930 bool fn_const_p = (copy == 2); 5931 5932 if (fn_const_p && !imp_const_p) 5933 /* If the function is defaulted outside the class, we just 5934 give the synthesis error. */ 5935 error ("%q+D declared to take const reference, but implicit " 5936 "declaration would take non-const", fn); 5937 } 5938 defaulted_late_check (fn); 5939 } 5940 5941 if (LAMBDA_TYPE_P (t)) 5942 { 5943 /* "This class type is not an aggregate." */ 5944 CLASSTYPE_NON_AGGREGATE (t) = 1; 5945 } 5946 5947 /* Compute the 'literal type' property before we 5948 do anything with non-static member functions. */ 5949 finalize_literal_type_property (t); 5950 5951 /* Create the in-charge and not-in-charge variants of constructors 5952 and destructors. */ 5953 clone_constructors_and_destructors (t); 5954 5955 /* Process the using-declarations. */ 5956 for (; access_decls; access_decls = TREE_CHAIN (access_decls)) 5957 handle_using_decl (TREE_VALUE (access_decls), t); 5958 5959 /* Build and sort the CLASSTYPE_METHOD_VEC. */ 5960 finish_struct_methods (t); 5961 5962 /* Figure out whether or not we will need a cookie when dynamically 5963 allocating an array of this type. */ 5964 TYPE_LANG_SPECIFIC (t)->u.c.vec_new_uses_cookie 5965 = type_requires_array_cookie (t); 5966 } 5967 5968 /* If T needs a pointer to its virtual function table, set TYPE_VFIELD 5969 accordingly. If a new vfield was created (because T doesn't have a 5970 primary base class), then the newly created field is returned. It 5971 is not added to the TYPE_FIELDS list; it is the caller's 5972 responsibility to do that. Accumulate declared virtual functions 5973 on VIRTUALS_P. */ 5974 5975 static tree 5976 create_vtable_ptr (tree t, tree* virtuals_p) 5977 { 5978 tree fn; 5979 5980 /* Collect the virtual functions declared in T. */ 5981 for (fn = TYPE_METHODS (t); fn; fn = DECL_CHAIN (fn)) 5982 if (TREE_CODE (fn) == FUNCTION_DECL 5983 && DECL_VINDEX (fn) && !DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn) 5984 && TREE_CODE (DECL_VINDEX (fn)) != INTEGER_CST) 5985 { 5986 tree new_virtual = make_node (TREE_LIST); 5987 5988 BV_FN (new_virtual) = fn; 5989 BV_DELTA (new_virtual) = integer_zero_node; 5990 BV_VCALL_INDEX (new_virtual) = NULL_TREE; 5991 5992 TREE_CHAIN (new_virtual) = *virtuals_p; 5993 *virtuals_p = new_virtual; 5994 } 5995 5996 /* If we couldn't find an appropriate base class, create a new field 5997 here. Even if there weren't any new virtual functions, we might need a 5998 new virtual function table if we're supposed to include vptrs in 5999 all classes that need them. */ 6000 if (!TYPE_VFIELD (t) && (*virtuals_p || TYPE_CONTAINS_VPTR_P (t))) 6001 { 6002 /* We build this decl with vtbl_ptr_type_node, which is a 6003 `vtable_entry_type*'. It might seem more precise to use 6004 `vtable_entry_type (*)[N]' where N is the number of virtual 6005 functions. However, that would require the vtable pointer in 6006 base classes to have a different type than the vtable pointer 6007 in derived classes. We could make that happen, but that 6008 still wouldn't solve all the problems. In particular, the 6009 type-based alias analysis code would decide that assignments 6010 to the base class vtable pointer can't alias assignments to 6011 the derived class vtable pointer, since they have different 6012 types. Thus, in a derived class destructor, where the base 6013 class constructor was inlined, we could generate bad code for 6014 setting up the vtable pointer. 6015 6016 Therefore, we use one type for all vtable pointers. We still 6017 use a type-correct type; it's just doesn't indicate the array 6018 bounds. That's better than using `void*' or some such; it's 6019 cleaner, and it let's the alias analysis code know that these 6020 stores cannot alias stores to void*! */ 6021 tree field; 6022 6023 field = build_decl (input_location, 6024 FIELD_DECL, get_vfield_name (t), vtbl_ptr_type_node); 6025 DECL_VIRTUAL_P (field) = 1; 6026 DECL_ARTIFICIAL (field) = 1; 6027 DECL_FIELD_CONTEXT (field) = t; 6028 DECL_FCONTEXT (field) = t; 6029 if (TYPE_PACKED (t)) 6030 DECL_PACKED (field) = 1; 6031 6032 TYPE_VFIELD (t) = field; 6033 6034 /* This class is non-empty. */ 6035 CLASSTYPE_EMPTY_P (t) = 0; 6036 6037 return field; 6038 } 6039 6040 return NULL_TREE; 6041 } 6042 6043 /* Add OFFSET to all base types of BINFO which is a base in the 6044 hierarchy dominated by T. 6045 6046 OFFSET, which is a type offset, is number of bytes. */ 6047 6048 static void 6049 propagate_binfo_offsets (tree binfo, tree offset) 6050 { 6051 int i; 6052 tree primary_binfo; 6053 tree base_binfo; 6054 6055 /* Update BINFO's offset. */ 6056 BINFO_OFFSET (binfo) 6057 = fold_convert (sizetype, 6058 size_binop (PLUS_EXPR, 6059 fold_convert (ssizetype, BINFO_OFFSET (binfo)), 6060 offset)); 6061 6062 /* Find the primary base class. */ 6063 primary_binfo = get_primary_binfo (binfo); 6064 6065 if (primary_binfo && BINFO_INHERITANCE_CHAIN (primary_binfo) == binfo) 6066 propagate_binfo_offsets (primary_binfo, offset); 6067 6068 /* Scan all of the bases, pushing the BINFO_OFFSET adjust 6069 downwards. */ 6070 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i) 6071 { 6072 /* Don't do the primary base twice. */ 6073 if (base_binfo == primary_binfo) 6074 continue; 6075 6076 if (BINFO_VIRTUAL_P (base_binfo)) 6077 continue; 6078 6079 propagate_binfo_offsets (base_binfo, offset); 6080 } 6081 } 6082 6083 /* Set BINFO_OFFSET for all of the virtual bases for RLI->T. Update 6084 TYPE_ALIGN and TYPE_SIZE for T. OFFSETS gives the location of 6085 empty subobjects of T. */ 6086 6087 static void 6088 layout_virtual_bases (record_layout_info rli, splay_tree offsets) 6089 { 6090 tree vbase; 6091 tree t = rli->t; 6092 tree *next_field; 6093 6094 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) == 0) 6095 return; 6096 6097 /* Find the last field. The artificial fields created for virtual 6098 bases will go after the last extant field to date. */ 6099 next_field = &TYPE_FIELDS (t); 6100 while (*next_field) 6101 next_field = &DECL_CHAIN (*next_field); 6102 6103 /* Go through the virtual bases, allocating space for each virtual 6104 base that is not already a primary base class. These are 6105 allocated in inheritance graph order. */ 6106 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase)) 6107 { 6108 if (!BINFO_VIRTUAL_P (vbase)) 6109 continue; 6110 6111 if (!BINFO_PRIMARY_P (vbase)) 6112 { 6113 /* This virtual base is not a primary base of any class in the 6114 hierarchy, so we have to add space for it. */ 6115 next_field = build_base_field (rli, vbase, 6116 offsets, next_field); 6117 } 6118 } 6119 } 6120 6121 /* Returns the offset of the byte just past the end of the base class 6122 BINFO. */ 6123 6124 static tree 6125 end_of_base (tree binfo) 6126 { 6127 tree size; 6128 6129 if (!CLASSTYPE_AS_BASE (BINFO_TYPE (binfo))) 6130 size = TYPE_SIZE_UNIT (char_type_node); 6131 else if (is_empty_class (BINFO_TYPE (binfo))) 6132 /* An empty class has zero CLASSTYPE_SIZE_UNIT, but we need to 6133 allocate some space for it. It cannot have virtual bases, so 6134 TYPE_SIZE_UNIT is fine. */ 6135 size = TYPE_SIZE_UNIT (BINFO_TYPE (binfo)); 6136 else 6137 size = CLASSTYPE_SIZE_UNIT (BINFO_TYPE (binfo)); 6138 6139 return size_binop (PLUS_EXPR, BINFO_OFFSET (binfo), size); 6140 } 6141 6142 /* Returns the offset of the byte just past the end of the base class 6143 with the highest offset in T. If INCLUDE_VIRTUALS_P is zero, then 6144 only non-virtual bases are included. */ 6145 6146 static tree 6147 end_of_class (tree t, int include_virtuals_p) 6148 { 6149 tree result = size_zero_node; 6150 vec<tree, va_gc> *vbases; 6151 tree binfo; 6152 tree base_binfo; 6153 tree offset; 6154 int i; 6155 6156 for (binfo = TYPE_BINFO (t), i = 0; 6157 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i) 6158 { 6159 if (!include_virtuals_p 6160 && BINFO_VIRTUAL_P (base_binfo) 6161 && (!BINFO_PRIMARY_P (base_binfo) 6162 || BINFO_INHERITANCE_CHAIN (base_binfo) != TYPE_BINFO (t))) 6163 continue; 6164 6165 offset = end_of_base (base_binfo); 6166 if (tree_int_cst_lt (result, offset)) 6167 result = offset; 6168 } 6169 6170 if (include_virtuals_p) 6171 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0; 6172 vec_safe_iterate (vbases, i, &base_binfo); i++) 6173 { 6174 offset = end_of_base (base_binfo); 6175 if (tree_int_cst_lt (result, offset)) 6176 result = offset; 6177 } 6178 6179 return result; 6180 } 6181 6182 /* Warn about bases of T that are inaccessible because they are 6183 ambiguous. For example: 6184 6185 struct S {}; 6186 struct T : public S {}; 6187 struct U : public S, public T {}; 6188 6189 Here, `(S*) new U' is not allowed because there are two `S' 6190 subobjects of U. */ 6191 6192 static void 6193 warn_about_ambiguous_bases (tree t) 6194 { 6195 int i; 6196 vec<tree, va_gc> *vbases; 6197 tree basetype; 6198 tree binfo; 6199 tree base_binfo; 6200 6201 /* If there are no repeated bases, nothing can be ambiguous. */ 6202 if (!CLASSTYPE_REPEATED_BASE_P (t)) 6203 return; 6204 6205 /* Check direct bases. */ 6206 for (binfo = TYPE_BINFO (t), i = 0; 6207 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i) 6208 { 6209 basetype = BINFO_TYPE (base_binfo); 6210 6211 if (!uniquely_derived_from_p (basetype, t)) 6212 warning (0, "direct base %qT inaccessible in %qT due to ambiguity", 6213 basetype, t); 6214 } 6215 6216 /* Check for ambiguous virtual bases. */ 6217 if (extra_warnings) 6218 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0; 6219 vec_safe_iterate (vbases, i, &binfo); i++) 6220 { 6221 basetype = BINFO_TYPE (binfo); 6222 6223 if (!uniquely_derived_from_p (basetype, t)) 6224 warning (OPT_Wextra, "virtual base %qT inaccessible in %qT due " 6225 "to ambiguity", basetype, t); 6226 } 6227 } 6228 6229 /* Compare two INTEGER_CSTs K1 and K2. */ 6230 6231 static int 6232 splay_tree_compare_integer_csts (splay_tree_key k1, splay_tree_key k2) 6233 { 6234 return tree_int_cst_compare ((tree) k1, (tree) k2); 6235 } 6236 6237 /* Increase the size indicated in RLI to account for empty classes 6238 that are "off the end" of the class. */ 6239 6240 static void 6241 include_empty_classes (record_layout_info rli) 6242 { 6243 tree eoc; 6244 tree rli_size; 6245 6246 /* It might be the case that we grew the class to allocate a 6247 zero-sized base class. That won't be reflected in RLI, yet, 6248 because we are willing to overlay multiple bases at the same 6249 offset. However, now we need to make sure that RLI is big enough 6250 to reflect the entire class. */ 6251 eoc = end_of_class (rli->t, 6252 CLASSTYPE_AS_BASE (rli->t) != NULL_TREE); 6253 rli_size = rli_size_unit_so_far (rli); 6254 if (TREE_CODE (rli_size) == INTEGER_CST 6255 && tree_int_cst_lt (rli_size, eoc)) 6256 { 6257 /* The size should have been rounded to a whole byte. */ 6258 gcc_assert (tree_int_cst_equal 6259 (rli->bitpos, round_down (rli->bitpos, BITS_PER_UNIT))); 6260 rli->bitpos 6261 = size_binop (PLUS_EXPR, 6262 rli->bitpos, 6263 size_binop (MULT_EXPR, 6264 fold_convert (bitsizetype, 6265 size_binop (MINUS_EXPR, 6266 eoc, rli_size)), 6267 bitsize_int (BITS_PER_UNIT))); 6268 normalize_rli (rli); 6269 } 6270 } 6271 6272 /* Calculate the TYPE_SIZE, TYPE_ALIGN, etc for T. Calculate 6273 BINFO_OFFSETs for all of the base-classes. Position the vtable 6274 pointer. Accumulate declared virtual functions on VIRTUALS_P. */ 6275 6276 static void 6277 layout_class_type (tree t, tree *virtuals_p) 6278 { 6279 tree non_static_data_members; 6280 tree field; 6281 tree vptr; 6282 record_layout_info rli; 6283 /* Maps offsets (represented as INTEGER_CSTs) to a TREE_LIST of 6284 types that appear at that offset. */ 6285 splay_tree empty_base_offsets; 6286 /* True if the last field laid out was a bit-field. */ 6287 bool last_field_was_bitfield = false; 6288 /* The location at which the next field should be inserted. */ 6289 tree *next_field; 6290 /* T, as a base class. */ 6291 tree base_t; 6292 6293 /* Keep track of the first non-static data member. */ 6294 non_static_data_members = TYPE_FIELDS (t); 6295 6296 /* Start laying out the record. */ 6297 rli = start_record_layout (t); 6298 6299 /* Mark all the primary bases in the hierarchy. */ 6300 determine_primary_bases (t); 6301 6302 /* Create a pointer to our virtual function table. */ 6303 vptr = create_vtable_ptr (t, virtuals_p); 6304 6305 /* The vptr is always the first thing in the class. */ 6306 if (vptr) 6307 { 6308 DECL_CHAIN (vptr) = TYPE_FIELDS (t); 6309 TYPE_FIELDS (t) = vptr; 6310 next_field = &DECL_CHAIN (vptr); 6311 place_field (rli, vptr); 6312 } 6313 else 6314 next_field = &TYPE_FIELDS (t); 6315 6316 /* Build FIELD_DECLs for all of the non-virtual base-types. */ 6317 empty_base_offsets = splay_tree_new (splay_tree_compare_integer_csts, 6318 NULL, NULL); 6319 build_base_fields (rli, empty_base_offsets, next_field); 6320 6321 /* Layout the non-static data members. */ 6322 for (field = non_static_data_members; field; field = DECL_CHAIN (field)) 6323 { 6324 tree type; 6325 tree padding; 6326 6327 /* We still pass things that aren't non-static data members to 6328 the back end, in case it wants to do something with them. */ 6329 if (TREE_CODE (field) != FIELD_DECL) 6330 { 6331 place_field (rli, field); 6332 /* If the static data member has incomplete type, keep track 6333 of it so that it can be completed later. (The handling 6334 of pending statics in finish_record_layout is 6335 insufficient; consider: 6336 6337 struct S1; 6338 struct S2 { static S1 s1; }; 6339 6340 At this point, finish_record_layout will be called, but 6341 S1 is still incomplete.) */ 6342 if (VAR_P (field)) 6343 { 6344 maybe_register_incomplete_var (field); 6345 /* The visibility of static data members is determined 6346 at their point of declaration, not their point of 6347 definition. */ 6348 determine_visibility (field); 6349 } 6350 continue; 6351 } 6352 6353 type = TREE_TYPE (field); 6354 if (type == error_mark_node) 6355 continue; 6356 6357 padding = NULL_TREE; 6358 6359 /* If this field is a bit-field whose width is greater than its 6360 type, then there are some special rules for allocating 6361 it. */ 6362 if (DECL_C_BIT_FIELD (field) 6363 && tree_int_cst_lt (TYPE_SIZE (type), DECL_SIZE (field))) 6364 { 6365 unsigned int itk; 6366 tree integer_type; 6367 bool was_unnamed_p = false; 6368 /* We must allocate the bits as if suitably aligned for the 6369 longest integer type that fits in this many bits. type 6370 of the field. Then, we are supposed to use the left over 6371 bits as additional padding. */ 6372 for (itk = itk_char; itk != itk_none; ++itk) 6373 if (integer_types[itk] != NULL_TREE 6374 && (tree_int_cst_lt (size_int (MAX_FIXED_MODE_SIZE), 6375 TYPE_SIZE (integer_types[itk])) 6376 || tree_int_cst_lt (DECL_SIZE (field), 6377 TYPE_SIZE (integer_types[itk])))) 6378 break; 6379 6380 /* ITK now indicates a type that is too large for the 6381 field. We have to back up by one to find the largest 6382 type that fits. */ 6383 do 6384 { 6385 --itk; 6386 integer_type = integer_types[itk]; 6387 } while (itk > 0 && integer_type == NULL_TREE); 6388 6389 /* Figure out how much additional padding is required. */ 6390 if (tree_int_cst_lt (TYPE_SIZE (integer_type), DECL_SIZE (field))) 6391 { 6392 if (TREE_CODE (t) == UNION_TYPE) 6393 /* In a union, the padding field must have the full width 6394 of the bit-field; all fields start at offset zero. */ 6395 padding = DECL_SIZE (field); 6396 else 6397 padding = size_binop (MINUS_EXPR, DECL_SIZE (field), 6398 TYPE_SIZE (integer_type)); 6399 } 6400 6401 /* An unnamed bitfield does not normally affect the 6402 alignment of the containing class on a target where 6403 PCC_BITFIELD_TYPE_MATTERS. But, the C++ ABI does not 6404 make any exceptions for unnamed bitfields when the 6405 bitfields are longer than their types. Therefore, we 6406 temporarily give the field a name. */ 6407 if (PCC_BITFIELD_TYPE_MATTERS && !DECL_NAME (field)) 6408 { 6409 was_unnamed_p = true; 6410 DECL_NAME (field) = make_anon_name (); 6411 } 6412 6413 DECL_SIZE (field) = TYPE_SIZE (integer_type); 6414 DECL_ALIGN (field) = TYPE_ALIGN (integer_type); 6415 DECL_USER_ALIGN (field) = TYPE_USER_ALIGN (integer_type); 6416 layout_nonempty_base_or_field (rli, field, NULL_TREE, 6417 empty_base_offsets); 6418 if (was_unnamed_p) 6419 DECL_NAME (field) = NULL_TREE; 6420 /* Now that layout has been performed, set the size of the 6421 field to the size of its declared type; the rest of the 6422 field is effectively invisible. */ 6423 DECL_SIZE (field) = TYPE_SIZE (type); 6424 /* We must also reset the DECL_MODE of the field. */ 6425 DECL_MODE (field) = TYPE_MODE (type); 6426 } 6427 else 6428 layout_nonempty_base_or_field (rli, field, NULL_TREE, 6429 empty_base_offsets); 6430 6431 /* Remember the location of any empty classes in FIELD. */ 6432 record_subobject_offsets (TREE_TYPE (field), 6433 byte_position(field), 6434 empty_base_offsets, 6435 /*is_data_member=*/true); 6436 6437 /* If a bit-field does not immediately follow another bit-field, 6438 and yet it starts in the middle of a byte, we have failed to 6439 comply with the ABI. */ 6440 if (warn_abi 6441 && DECL_C_BIT_FIELD (field) 6442 /* The TREE_NO_WARNING flag gets set by Objective-C when 6443 laying out an Objective-C class. The ObjC ABI differs 6444 from the C++ ABI, and so we do not want a warning 6445 here. */ 6446 && !TREE_NO_WARNING (field) 6447 && !last_field_was_bitfield 6448 && !integer_zerop (size_binop (TRUNC_MOD_EXPR, 6449 DECL_FIELD_BIT_OFFSET (field), 6450 bitsize_unit_node))) 6451 warning_at (DECL_SOURCE_LOCATION (field), OPT_Wabi, 6452 "offset of %qD is not ABI-compliant and may " 6453 "change in a future version of GCC", field); 6454 6455 /* The middle end uses the type of expressions to determine the 6456 possible range of expression values. In order to optimize 6457 "x.i > 7" to "false" for a 2-bit bitfield "i", the middle end 6458 must be made aware of the width of "i", via its type. 6459 6460 Because C++ does not have integer types of arbitrary width, 6461 we must (for the purposes of the front end) convert from the 6462 type assigned here to the declared type of the bitfield 6463 whenever a bitfield expression is used as an rvalue. 6464 Similarly, when assigning a value to a bitfield, the value 6465 must be converted to the type given the bitfield here. */ 6466 if (DECL_C_BIT_FIELD (field)) 6467 { 6468 unsigned HOST_WIDE_INT width; 6469 tree ftype = TREE_TYPE (field); 6470 width = tree_to_uhwi (DECL_SIZE (field)); 6471 if (width != TYPE_PRECISION (ftype)) 6472 { 6473 TREE_TYPE (field) 6474 = c_build_bitfield_integer_type (width, 6475 TYPE_UNSIGNED (ftype)); 6476 TREE_TYPE (field) 6477 = cp_build_qualified_type (TREE_TYPE (field), 6478 cp_type_quals (ftype)); 6479 } 6480 } 6481 6482 /* If we needed additional padding after this field, add it 6483 now. */ 6484 if (padding) 6485 { 6486 tree padding_field; 6487 6488 padding_field = build_decl (input_location, 6489 FIELD_DECL, 6490 NULL_TREE, 6491 char_type_node); 6492 DECL_BIT_FIELD (padding_field) = 1; 6493 DECL_SIZE (padding_field) = padding; 6494 DECL_CONTEXT (padding_field) = t; 6495 DECL_ARTIFICIAL (padding_field) = 1; 6496 DECL_IGNORED_P (padding_field) = 1; 6497 layout_nonempty_base_or_field (rli, padding_field, 6498 NULL_TREE, 6499 empty_base_offsets); 6500 } 6501 6502 last_field_was_bitfield = DECL_C_BIT_FIELD (field); 6503 } 6504 6505 if (!integer_zerop (rli->bitpos)) 6506 { 6507 /* Make sure that we are on a byte boundary so that the size of 6508 the class without virtual bases will always be a round number 6509 of bytes. */ 6510 rli->bitpos = round_up_loc (input_location, rli->bitpos, BITS_PER_UNIT); 6511 normalize_rli (rli); 6512 } 6513 6514 /* Delete all zero-width bit-fields from the list of fields. Now 6515 that the type is laid out they are no longer important. */ 6516 remove_zero_width_bit_fields (t); 6517 6518 /* Create the version of T used for virtual bases. We do not use 6519 make_class_type for this version; this is an artificial type. For 6520 a POD type, we just reuse T. */ 6521 if (CLASSTYPE_NON_LAYOUT_POD_P (t) || CLASSTYPE_EMPTY_P (t)) 6522 { 6523 base_t = make_node (TREE_CODE (t)); 6524 6525 /* Set the size and alignment for the new type. */ 6526 tree eoc; 6527 6528 /* If the ABI version is not at least two, and the last 6529 field was a bit-field, RLI may not be on a byte 6530 boundary. In particular, rli_size_unit_so_far might 6531 indicate the last complete byte, while rli_size_so_far 6532 indicates the total number of bits used. Therefore, 6533 rli_size_so_far, rather than rli_size_unit_so_far, is 6534 used to compute TYPE_SIZE_UNIT. */ 6535 eoc = end_of_class (t, /*include_virtuals_p=*/0); 6536 TYPE_SIZE_UNIT (base_t) 6537 = size_binop (MAX_EXPR, 6538 fold_convert (sizetype, 6539 size_binop (CEIL_DIV_EXPR, 6540 rli_size_so_far (rli), 6541 bitsize_int (BITS_PER_UNIT))), 6542 eoc); 6543 TYPE_SIZE (base_t) 6544 = size_binop (MAX_EXPR, 6545 rli_size_so_far (rli), 6546 size_binop (MULT_EXPR, 6547 fold_convert (bitsizetype, eoc), 6548 bitsize_int (BITS_PER_UNIT))); 6549 TYPE_ALIGN (base_t) = rli->record_align; 6550 TYPE_USER_ALIGN (base_t) = TYPE_USER_ALIGN (t); 6551 6552 /* Copy the fields from T. */ 6553 next_field = &TYPE_FIELDS (base_t); 6554 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field)) 6555 if (TREE_CODE (field) == FIELD_DECL) 6556 { 6557 *next_field = copy_node (field); 6558 DECL_CONTEXT (*next_field) = base_t; 6559 next_field = &DECL_CHAIN (*next_field); 6560 } 6561 *next_field = NULL_TREE; 6562 6563 /* Record the base version of the type. */ 6564 CLASSTYPE_AS_BASE (t) = base_t; 6565 TYPE_CONTEXT (base_t) = t; 6566 } 6567 else 6568 CLASSTYPE_AS_BASE (t) = t; 6569 6570 /* Every empty class contains an empty class. */ 6571 if (CLASSTYPE_EMPTY_P (t)) 6572 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1; 6573 6574 /* Set the TYPE_DECL for this type to contain the right 6575 value for DECL_OFFSET, so that we can use it as part 6576 of a COMPONENT_REF for multiple inheritance. */ 6577 layout_decl (TYPE_MAIN_DECL (t), 0); 6578 6579 /* Now fix up any virtual base class types that we left lying 6580 around. We must get these done before we try to lay out the 6581 virtual function table. As a side-effect, this will remove the 6582 base subobject fields. */ 6583 layout_virtual_bases (rli, empty_base_offsets); 6584 6585 /* Make sure that empty classes are reflected in RLI at this 6586 point. */ 6587 include_empty_classes(rli); 6588 6589 /* Make sure not to create any structures with zero size. */ 6590 if (integer_zerop (rli_size_unit_so_far (rli)) && CLASSTYPE_EMPTY_P (t)) 6591 place_field (rli, 6592 build_decl (input_location, 6593 FIELD_DECL, NULL_TREE, char_type_node)); 6594 6595 /* If this is a non-POD, declaring it packed makes a difference to how it 6596 can be used as a field; don't let finalize_record_size undo it. */ 6597 if (TYPE_PACKED (t) && !layout_pod_type_p (t)) 6598 rli->packed_maybe_necessary = true; 6599 6600 /* Let the back end lay out the type. */ 6601 finish_record_layout (rli, /*free_p=*/true); 6602 6603 if (TYPE_SIZE_UNIT (t) 6604 && TREE_CODE (TYPE_SIZE_UNIT (t)) == INTEGER_CST 6605 && !TREE_OVERFLOW (TYPE_SIZE_UNIT (t)) 6606 && !valid_constant_size_p (TYPE_SIZE_UNIT (t))) 6607 error ("size of type %qT is too large (%qE bytes)", t, TYPE_SIZE_UNIT (t)); 6608 6609 /* Warn about bases that can't be talked about due to ambiguity. */ 6610 warn_about_ambiguous_bases (t); 6611 6612 /* Now that we're done with layout, give the base fields the real types. */ 6613 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field)) 6614 if (DECL_ARTIFICIAL (field) && IS_FAKE_BASE_TYPE (TREE_TYPE (field))) 6615 TREE_TYPE (field) = TYPE_CONTEXT (TREE_TYPE (field)); 6616 6617 /* Clean up. */ 6618 splay_tree_delete (empty_base_offsets); 6619 6620 if (CLASSTYPE_EMPTY_P (t) 6621 && tree_int_cst_lt (sizeof_biggest_empty_class, 6622 TYPE_SIZE_UNIT (t))) 6623 sizeof_biggest_empty_class = TYPE_SIZE_UNIT (t); 6624 } 6625 6626 /* Determine the "key method" for the class type indicated by TYPE, 6627 and set CLASSTYPE_KEY_METHOD accordingly. */ 6628 6629 void 6630 determine_key_method (tree type) 6631 { 6632 tree method; 6633 6634 if (TYPE_FOR_JAVA (type) 6635 || processing_template_decl 6636 || CLASSTYPE_TEMPLATE_INSTANTIATION (type) 6637 || CLASSTYPE_INTERFACE_KNOWN (type)) 6638 return; 6639 6640 /* The key method is the first non-pure virtual function that is not 6641 inline at the point of class definition. On some targets the 6642 key function may not be inline; those targets should not call 6643 this function until the end of the translation unit. */ 6644 for (method = TYPE_METHODS (type); method != NULL_TREE; 6645 method = DECL_CHAIN (method)) 6646 if (TREE_CODE (method) == FUNCTION_DECL 6647 && DECL_VINDEX (method) != NULL_TREE 6648 && ! DECL_DECLARED_INLINE_P (method) 6649 && ! DECL_PURE_VIRTUAL_P (method)) 6650 { 6651 CLASSTYPE_KEY_METHOD (type) = method; 6652 break; 6653 } 6654 6655 return; 6656 } 6657 6658 6659 /* Allocate and return an instance of struct sorted_fields_type with 6660 N fields. */ 6661 6662 static struct sorted_fields_type * 6663 sorted_fields_type_new (int n) 6664 { 6665 struct sorted_fields_type *sft; 6666 sft = (sorted_fields_type *) ggc_internal_alloc (sizeof (sorted_fields_type) 6667 + n * sizeof (tree)); 6668 sft->len = n; 6669 6670 return sft; 6671 } 6672 6673 /* Helper of find_flexarrays. Return true when FLD refers to a non-static 6674 class data member of non-zero size, otherwise false. */ 6675 6676 static inline bool 6677 field_nonempty_p (const_tree fld) 6678 { 6679 if (TREE_CODE (fld) == ERROR_MARK) 6680 return false; 6681 6682 tree type = TREE_TYPE (fld); 6683 if (TREE_CODE (fld) == FIELD_DECL 6684 && TREE_CODE (type) != ERROR_MARK 6685 && (DECL_NAME (fld) || RECORD_OR_UNION_TYPE_P (type))) 6686 { 6687 return TYPE_SIZE (type) 6688 && (TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST 6689 || !tree_int_cst_equal (size_zero_node, TYPE_SIZE (type))); 6690 } 6691 6692 return false; 6693 } 6694 6695 /* Used by find_flexarrays and related functions. */ 6696 6697 struct flexmems_t 6698 { 6699 /* The first flexible array member or non-zero array member found 6700 in the order of layout. */ 6701 tree array; 6702 /* First non-static non-empty data member in the class or its bases. */ 6703 tree first; 6704 /* The first non-static non-empty data member following either 6705 the flexible array member, if found, or the zero-length array member 6706 otherwise. AFTER[1] refers to the first such data member of a union 6707 of which the struct containing the flexible array member or zero-length 6708 array is a member, or NULL when no such union exists. This element is 6709 only used during searching, not for diagnosing problems. AFTER[0] 6710 refers to the first such data member that is not a member of such 6711 a union. */ 6712 tree after[2]; 6713 6714 /* Refers to a struct (not union) in which the struct of which the flexible 6715 array is member is defined. Used to diagnose strictly (according to C) 6716 invalid uses of the latter structs. */ 6717 tree enclosing; 6718 }; 6719 6720 /* Find either the first flexible array member or the first zero-length 6721 array, in that order of preference, among members of class T (but not 6722 its base classes), and set members of FMEM accordingly. 6723 BASE_P is true if T is a base class of another class. 6724 PUN is set to the outermost union in which the flexible array member 6725 (or zero-length array) is defined if one such union exists, otherwise 6726 to NULL. 6727 Similarly, PSTR is set to a data member of the outermost struct of 6728 which the flexible array is a member if one such struct exists, 6729 otherwise to NULL. */ 6730 6731 static void 6732 find_flexarrays (tree t, flexmems_t *fmem, bool base_p, 6733 tree pun /* = NULL_TREE */, 6734 tree pstr /* = NULL_TREE */) 6735 { 6736 /* Set the "pointer" to the outermost enclosing union if not set 6737 yet and maintain it for the remainder of the recursion. */ 6738 if (!pun && TREE_CODE (t) == UNION_TYPE) 6739 pun = t; 6740 6741 for (tree fld = TYPE_FIELDS (t); fld; fld = DECL_CHAIN (fld)) 6742 { 6743 if (fld == error_mark_node) 6744 return; 6745 6746 /* Is FLD a typedef for an anonymous struct? */ 6747 6748 /* FIXME: Note that typedefs (as well as arrays) need to be fully 6749 handled elsewhere so that errors like the following are detected 6750 as well: 6751 typedef struct { int i, a[], j; } S; // bug c++/72753 6752 S s [2]; // bug c++/68489 6753 */ 6754 if (TREE_CODE (fld) == TYPE_DECL 6755 && DECL_IMPLICIT_TYPEDEF_P (fld) 6756 && CLASS_TYPE_P (TREE_TYPE (fld)) 6757 && anon_aggrname_p (DECL_NAME (fld))) 6758 { 6759 /* Check the nested unnamed type referenced via a typedef 6760 independently of FMEM (since it's not a data member of 6761 the enclosing class). */ 6762 check_flexarrays (TREE_TYPE (fld)); 6763 continue; 6764 } 6765 6766 /* Skip anything that's GCC-generated or not a (non-static) data 6767 member. */ 6768 if (DECL_ARTIFICIAL (fld) || TREE_CODE (fld) != FIELD_DECL) 6769 continue; 6770 6771 /* Type of the member. */ 6772 tree fldtype = TREE_TYPE (fld); 6773 if (fldtype == error_mark_node) 6774 return; 6775 6776 /* Determine the type of the array element or object referenced 6777 by the member so that it can be checked for flexible array 6778 members if it hasn't been yet. */ 6779 tree eltype = fldtype; 6780 while (TREE_CODE (eltype) == ARRAY_TYPE 6781 || TREE_CODE (eltype) == POINTER_TYPE 6782 || TREE_CODE (eltype) == REFERENCE_TYPE) 6783 eltype = TREE_TYPE (eltype); 6784 6785 if (RECORD_OR_UNION_TYPE_P (eltype)) 6786 { 6787 if (fmem->array && !fmem->after[bool (pun)]) 6788 { 6789 /* Once the member after the flexible array has been found 6790 we're done. */ 6791 fmem->after[bool (pun)] = fld; 6792 break; 6793 } 6794 6795 if (eltype == fldtype || TYPE_ANONYMOUS_P (eltype)) 6796 { 6797 /* Descend into the non-static member struct or union and try 6798 to find a flexible array member or zero-length array among 6799 its members. This is only necessary for anonymous types 6800 and types in whose context the current type T has not been 6801 defined (the latter must not be checked again because they 6802 are already in the process of being checked by one of the 6803 recursive calls). */ 6804 6805 tree first = fmem->first; 6806 tree array = fmem->array; 6807 6808 /* If this member isn't anonymous and a prior non-flexible array 6809 member has been seen in one of the enclosing structs, clear 6810 the FIRST member since it doesn't contribute to the flexible 6811 array struct's members. */ 6812 if (first && !array && !ANON_AGGR_TYPE_P (eltype)) 6813 fmem->first = NULL_TREE; 6814 6815 find_flexarrays (eltype, fmem, false, pun, 6816 !pstr && TREE_CODE (t) == RECORD_TYPE ? fld : pstr); 6817 6818 if (fmem->array != array) 6819 continue; 6820 6821 if (first && !array && !ANON_AGGR_TYPE_P (eltype)) 6822 { 6823 /* Restore the FIRST member reset above if no flexible 6824 array member has been found in this member's struct. */ 6825 fmem->first = first; 6826 } 6827 6828 /* If the member struct contains the first flexible array 6829 member, or if this member is a base class, continue to 6830 the next member and avoid setting the FMEM->NEXT pointer 6831 to point to it. */ 6832 if (base_p) 6833 continue; 6834 } 6835 } 6836 6837 if (field_nonempty_p (fld)) 6838 { 6839 /* Remember the first non-static data member. */ 6840 if (!fmem->first) 6841 fmem->first = fld; 6842 6843 /* Remember the first non-static data member after the flexible 6844 array member, if one has been found, or the zero-length array 6845 if it has been found. */ 6846 if (fmem->array && !fmem->after[bool (pun)]) 6847 fmem->after[bool (pun)] = fld; 6848 } 6849 6850 /* Skip non-arrays. */ 6851 if (TREE_CODE (fldtype) != ARRAY_TYPE) 6852 continue; 6853 6854 /* Determine the upper bound of the array if it has one. */ 6855 if (TYPE_DOMAIN (fldtype)) 6856 { 6857 if (fmem->array) 6858 { 6859 /* Make a record of the zero-length array if either one 6860 such field or a flexible array member has been seen to 6861 handle the pathological and unlikely case of multiple 6862 such members. */ 6863 if (!fmem->after[bool (pun)]) 6864 fmem->after[bool (pun)] = fld; 6865 } 6866 else if (integer_all_onesp (TYPE_MAX_VALUE (TYPE_DOMAIN (fldtype)))) 6867 { 6868 /* Remember the first zero-length array unless a flexible array 6869 member has already been seen. */ 6870 fmem->array = fld; 6871 fmem->enclosing = pstr; 6872 } 6873 } 6874 else 6875 { 6876 /* Flexible array members have no upper bound. */ 6877 if (fmem->array) 6878 { 6879 /* Replace the zero-length array if it's been stored and 6880 reset the after pointer. */ 6881 if (TYPE_DOMAIN (TREE_TYPE (fmem->array))) 6882 { 6883 fmem->after[bool (pun)] = NULL_TREE; 6884 fmem->array = fld; 6885 fmem->enclosing = pstr; 6886 } 6887 } 6888 else 6889 { 6890 fmem->array = fld; 6891 fmem->enclosing = pstr; 6892 } 6893 } 6894 } 6895 } 6896 6897 /* Diagnose a strictly (by the C standard) invalid use of a struct with 6898 a flexible array member (or the zero-length array extension). */ 6899 6900 static void 6901 diagnose_invalid_flexarray (const flexmems_t *fmem) 6902 { 6903 if (fmem->array && fmem->enclosing 6904 && pedwarn (location_of (fmem->enclosing), OPT_Wpedantic, 6905 TYPE_DOMAIN (TREE_TYPE (fmem->array)) 6906 ? G_("invalid use of %q#T with a zero-size array " 6907 "in %q#D") 6908 : G_("invalid use of %q#T with a flexible array member " 6909 "in %q#T"), 6910 DECL_CONTEXT (fmem->array), 6911 DECL_CONTEXT (fmem->enclosing))) 6912 inform (DECL_SOURCE_LOCATION (fmem->array), 6913 "array member %q#D declared here", fmem->array); 6914 } 6915 6916 /* Issue diagnostics for invalid flexible array members or zero-length 6917 arrays that are not the last elements of the containing class or its 6918 base classes or that are its sole members. */ 6919 6920 static void 6921 diagnose_flexarrays (tree t, const flexmems_t *fmem) 6922 { 6923 if (!fmem->array) 6924 return; 6925 6926 if (fmem->first && !fmem->after[0]) 6927 { 6928 diagnose_invalid_flexarray (fmem); 6929 return; 6930 } 6931 6932 /* Has a diagnostic been issued? */ 6933 bool diagd = false; 6934 6935 const char *msg = 0; 6936 6937 if (TYPE_DOMAIN (TREE_TYPE (fmem->array))) 6938 { 6939 if (fmem->after[0]) 6940 msg = G_("zero-size array member %qD not at end of %q#T"); 6941 else if (!fmem->first) 6942 msg = G_("zero-size array member %qD in an otherwise empty %q#T"); 6943 6944 if (msg) 6945 { 6946 location_t loc = DECL_SOURCE_LOCATION (fmem->array); 6947 6948 if (pedwarn (loc, OPT_Wpedantic, msg, fmem->array, t)) 6949 { 6950 inform (location_of (t), "in the definition of %q#T", t); 6951 diagd = true; 6952 } 6953 } 6954 } 6955 else 6956 { 6957 if (fmem->after[0]) 6958 msg = G_("flexible array member %qD not at end of %q#T"); 6959 else if (!fmem->first) 6960 msg = G_("flexible array member %qD in an otherwise empty %q#T"); 6961 6962 if (msg) 6963 { 6964 location_t loc = DECL_SOURCE_LOCATION (fmem->array); 6965 diagd = true; 6966 6967 /* For compatibility with GCC 6.2 and 6.1 reject with an error 6968 a flexible array member of a plain struct that's followed 6969 by another member only if they are both members of the same 6970 struct. Otherwise, issue just a pedantic warning. See bug 6971 71921 for details. */ 6972 if (fmem->after[0] 6973 && (!TYPE_BINFO (t) 6974 || 0 == BINFO_N_BASE_BINFOS (TYPE_BINFO (t))) 6975 && DECL_CONTEXT (fmem->array) != DECL_CONTEXT (fmem->after[0]) 6976 && !ANON_AGGR_TYPE_P (DECL_CONTEXT (fmem->array)) 6977 && !ANON_AGGR_TYPE_P (DECL_CONTEXT (fmem->after[0]))) 6978 pedwarn (loc, OPT_Wpedantic, msg, fmem->array, t); 6979 else 6980 error_at (loc, msg, fmem->array, t); 6981 6982 /* In the unlikely event that the member following the flexible 6983 array member is declared in a different class, or the member 6984 overlaps another member of a common union, point to it. 6985 Otherwise it should be obvious. */ 6986 if (fmem->after[0] 6987 && ((DECL_CONTEXT (fmem->after[0]) 6988 != DECL_CONTEXT (fmem->array)))) 6989 { 6990 inform (DECL_SOURCE_LOCATION (fmem->after[0]), 6991 "next member %q#D declared here", 6992 fmem->after[0]); 6993 inform (location_of (t), "in the definition of %q#T", t); 6994 } 6995 } 6996 } 6997 6998 if (!diagd && fmem->array && fmem->enclosing) 6999 diagnose_invalid_flexarray (fmem); 7000 } 7001 7002 7003 /* Recursively check to make sure that any flexible array or zero-length 7004 array members of class T or its bases are valid (i.e., not the sole 7005 non-static data member of T and, if one exists, that it is the last 7006 non-static data member of T and its base classes. FMEM is expected 7007 to be initially null and is used internally by recursive calls to 7008 the function. Issue the appropriate diagnostics for the array member 7009 that fails the checks. */ 7010 7011 static void 7012 check_flexarrays (tree t, flexmems_t *fmem /* = NULL */, 7013 bool base_p /* = false */) 7014 { 7015 /* Initialize the result of a search for flexible array and zero-length 7016 array members. Avoid doing any work if the most interesting FMEM data 7017 have already been populated. */ 7018 flexmems_t flexmems = flexmems_t (); 7019 if (!fmem) 7020 fmem = &flexmems; 7021 else if (fmem->array && fmem->first && fmem->after[0]) 7022 return; 7023 7024 tree fam = fmem->array; 7025 7026 /* Recursively check the primary base class first. */ 7027 if (CLASSTYPE_HAS_PRIMARY_BASE_P (t)) 7028 { 7029 tree basetype = BINFO_TYPE (CLASSTYPE_PRIMARY_BINFO (t)); 7030 check_flexarrays (basetype, fmem, true); 7031 } 7032 7033 /* Recursively check the base classes. */ 7034 int nbases = TYPE_BINFO (t) ? BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) : 0; 7035 for (int i = 0; i < nbases; ++i) 7036 { 7037 tree base_binfo = BINFO_BASE_BINFO (TYPE_BINFO (t), i); 7038 7039 /* The primary base class was already checked above. */ 7040 if (base_binfo == CLASSTYPE_PRIMARY_BINFO (t)) 7041 continue; 7042 7043 /* Virtual base classes are at the end. */ 7044 if (BINFO_VIRTUAL_P (base_binfo)) 7045 continue; 7046 7047 /* Check the base class. */ 7048 check_flexarrays (BINFO_TYPE (base_binfo), fmem, /*base_p=*/true); 7049 } 7050 7051 if (fmem == &flexmems) 7052 { 7053 /* Check virtual base classes only once per derived class. 7054 I.e., this check is not performed recursively for base 7055 classes. */ 7056 int i; 7057 tree base_binfo; 7058 vec<tree, va_gc> *vbases; 7059 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0; 7060 vec_safe_iterate (vbases, i, &base_binfo); i++) 7061 { 7062 /* Check the virtual base class. */ 7063 tree basetype = TREE_TYPE (base_binfo); 7064 7065 check_flexarrays (basetype, fmem, /*base_p=*/true); 7066 } 7067 } 7068 7069 /* Is the type unnamed (and therefore a member of it potentially 7070 an anonymous struct or union)? */ 7071 bool maybe_anon_p = TYPE_ANONYMOUS_P (t); 7072 7073 /* Search the members of the current (possibly derived) class, skipping 7074 unnamed structs and unions since those could be anonymous. */ 7075 if (fmem != &flexmems || !maybe_anon_p) 7076 find_flexarrays (t, fmem, base_p || fam != fmem->array); 7077 7078 if (fmem == &flexmems && !maybe_anon_p) 7079 { 7080 /* Issue diagnostics for invalid flexible and zero-length array 7081 members found in base classes or among the members of the current 7082 class. Ignore anonymous structs and unions whose members are 7083 considered to be members of the enclosing class and thus will 7084 be diagnosed when checking it. */ 7085 diagnose_flexarrays (t, fmem); 7086 } 7087 } 7088 7089 /* Perform processing required when the definition of T (a class type) 7090 is complete. Diagnose invalid definitions of flexible array members 7091 and zero-size arrays. */ 7092 7093 void 7094 finish_struct_1 (tree t) 7095 { 7096 tree x; 7097 /* A TREE_LIST. The TREE_VALUE of each node is a FUNCTION_DECL. */ 7098 tree virtuals = NULL_TREE; 7099 7100 if (COMPLETE_TYPE_P (t)) 7101 { 7102 gcc_assert (MAYBE_CLASS_TYPE_P (t)); 7103 error ("redefinition of %q#T", t); 7104 popclass (); 7105 return; 7106 } 7107 7108 /* If this type was previously laid out as a forward reference, 7109 make sure we lay it out again. */ 7110 TYPE_SIZE (t) = NULL_TREE; 7111 CLASSTYPE_PRIMARY_BINFO (t) = NULL_TREE; 7112 7113 /* Make assumptions about the class; we'll reset the flags if 7114 necessary. */ 7115 CLASSTYPE_EMPTY_P (t) = 1; 7116 CLASSTYPE_NEARLY_EMPTY_P (t) = 1; 7117 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 0; 7118 CLASSTYPE_LITERAL_P (t) = true; 7119 7120 /* Do end-of-class semantic processing: checking the validity of the 7121 bases and members and add implicitly generated methods. */ 7122 check_bases_and_members (t); 7123 7124 /* Find the key method. */ 7125 if (TYPE_CONTAINS_VPTR_P (t)) 7126 { 7127 /* The Itanium C++ ABI permits the key method to be chosen when 7128 the class is defined -- even though the key method so 7129 selected may later turn out to be an inline function. On 7130 some systems (such as ARM Symbian OS) the key method cannot 7131 be determined until the end of the translation unit. On such 7132 systems, we leave CLASSTYPE_KEY_METHOD set to NULL, which 7133 will cause the class to be added to KEYED_CLASSES. Then, in 7134 finish_file we will determine the key method. */ 7135 if (targetm.cxx.key_method_may_be_inline ()) 7136 determine_key_method (t); 7137 7138 /* If a polymorphic class has no key method, we may emit the vtable 7139 in every translation unit where the class definition appears. If 7140 we're devirtualizing, we can look into the vtable even if we 7141 aren't emitting it. */ 7142 if (CLASSTYPE_KEY_METHOD (t) == NULL_TREE) 7143 keyed_classes = tree_cons (NULL_TREE, t, keyed_classes); 7144 } 7145 7146 /* Layout the class itself. */ 7147 layout_class_type (t, &virtuals); 7148 if (CLASSTYPE_AS_BASE (t) != t) 7149 /* We use the base type for trivial assignments, and hence it 7150 needs a mode. */ 7151 compute_record_mode (CLASSTYPE_AS_BASE (t)); 7152 7153 /* With the layout complete, check for flexible array members and 7154 zero-length arrays that might overlap other members in the final 7155 layout. */ 7156 check_flexarrays (t); 7157 7158 virtuals = modify_all_vtables (t, nreverse (virtuals)); 7159 7160 /* If necessary, create the primary vtable for this class. */ 7161 if (virtuals || TYPE_CONTAINS_VPTR_P (t)) 7162 { 7163 /* We must enter these virtuals into the table. */ 7164 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t)) 7165 build_primary_vtable (NULL_TREE, t); 7166 else if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t))) 7167 /* Here we know enough to change the type of our virtual 7168 function table, but we will wait until later this function. */ 7169 build_primary_vtable (CLASSTYPE_PRIMARY_BINFO (t), t); 7170 7171 /* If we're warning about ABI tags, check the types of the new 7172 virtual functions. */ 7173 if (warn_abi_tag) 7174 for (tree v = virtuals; v; v = TREE_CHAIN (v)) 7175 check_abi_tags (t, TREE_VALUE (v)); 7176 } 7177 7178 if (TYPE_CONTAINS_VPTR_P (t)) 7179 { 7180 int vindex; 7181 tree fn; 7182 7183 if (BINFO_VTABLE (TYPE_BINFO (t))) 7184 gcc_assert (DECL_VIRTUAL_P (BINFO_VTABLE (TYPE_BINFO (t)))); 7185 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t)) 7186 gcc_assert (BINFO_VIRTUALS (TYPE_BINFO (t)) == NULL_TREE); 7187 7188 /* Add entries for virtual functions introduced by this class. */ 7189 BINFO_VIRTUALS (TYPE_BINFO (t)) 7190 = chainon (BINFO_VIRTUALS (TYPE_BINFO (t)), virtuals); 7191 7192 /* Set DECL_VINDEX for all functions declared in this class. */ 7193 for (vindex = 0, fn = BINFO_VIRTUALS (TYPE_BINFO (t)); 7194 fn; 7195 fn = TREE_CHAIN (fn), 7196 vindex += (TARGET_VTABLE_USES_DESCRIPTORS 7197 ? TARGET_VTABLE_USES_DESCRIPTORS : 1)) 7198 { 7199 tree fndecl = BV_FN (fn); 7200 7201 if (DECL_THUNK_P (fndecl)) 7202 /* A thunk. We should never be calling this entry directly 7203 from this vtable -- we'd use the entry for the non 7204 thunk base function. */ 7205 DECL_VINDEX (fndecl) = NULL_TREE; 7206 else if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST) 7207 DECL_VINDEX (fndecl) = build_int_cst (NULL_TREE, vindex); 7208 } 7209 } 7210 7211 finish_struct_bits (t); 7212 set_method_tm_attributes (t); 7213 if (flag_openmp || flag_openmp_simd) 7214 finish_omp_declare_simd_methods (t); 7215 7216 /* Complete the rtl for any static member objects of the type we're 7217 working on. */ 7218 for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x)) 7219 if (VAR_P (x) && TREE_STATIC (x) 7220 && TREE_TYPE (x) != error_mark_node 7221 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (x)), t)) 7222 DECL_MODE (x) = TYPE_MODE (t); 7223 7224 /* Done with FIELDS...now decide whether to sort these for 7225 faster lookups later. 7226 7227 We use a small number because most searches fail (succeeding 7228 ultimately as the search bores through the inheritance 7229 hierarchy), and we want this failure to occur quickly. */ 7230 7231 insert_into_classtype_sorted_fields (TYPE_FIELDS (t), t, 8); 7232 7233 /* Complain if one of the field types requires lower visibility. */ 7234 constrain_class_visibility (t); 7235 7236 /* Make the rtl for any new vtables we have created, and unmark 7237 the base types we marked. */ 7238 finish_vtbls (t); 7239 7240 /* Build the VTT for T. */ 7241 build_vtt (t); 7242 7243 /* This warning does not make sense for Java classes, since they 7244 cannot have destructors. */ 7245 if (!TYPE_FOR_JAVA (t) && warn_nonvdtor 7246 && TYPE_POLYMORPHIC_P (t) && accessible_nvdtor_p (t) 7247 && !CLASSTYPE_FINAL (t)) 7248 warning (OPT_Wnon_virtual_dtor, 7249 "%q#T has virtual functions and accessible" 7250 " non-virtual destructor", t); 7251 7252 complete_vars (t); 7253 7254 if (warn_overloaded_virtual) 7255 warn_hidden (t); 7256 7257 /* Class layout, assignment of virtual table slots, etc., is now 7258 complete. Give the back end a chance to tweak the visibility of 7259 the class or perform any other required target modifications. */ 7260 targetm.cxx.adjust_class_at_definition (t); 7261 7262 maybe_suppress_debug_info (t); 7263 7264 if (flag_vtable_verify) 7265 vtv_save_class_info (t); 7266 7267 dump_class_hierarchy (t); 7268 7269 /* Finish debugging output for this type. */ 7270 rest_of_type_compilation (t, ! LOCAL_CLASS_P (t)); 7271 7272 if (TYPE_TRANSPARENT_AGGR (t)) 7273 { 7274 tree field = first_field (t); 7275 if (field == NULL_TREE || error_operand_p (field)) 7276 { 7277 error ("type transparent %q#T does not have any fields", t); 7278 TYPE_TRANSPARENT_AGGR (t) = 0; 7279 } 7280 else if (DECL_ARTIFICIAL (field)) 7281 { 7282 if (DECL_FIELD_IS_BASE (field)) 7283 error ("type transparent class %qT has base classes", t); 7284 else 7285 { 7286 gcc_checking_assert (DECL_VIRTUAL_P (field)); 7287 error ("type transparent class %qT has virtual functions", t); 7288 } 7289 TYPE_TRANSPARENT_AGGR (t) = 0; 7290 } 7291 else if (TYPE_MODE (t) != DECL_MODE (field)) 7292 { 7293 error ("type transparent %q#T cannot be made transparent because " 7294 "the type of the first field has a different ABI from the " 7295 "class overall", t); 7296 TYPE_TRANSPARENT_AGGR (t) = 0; 7297 } 7298 } 7299 } 7300 7301 /* Insert FIELDS into T for the sorted case if the FIELDS count is 7302 equal to THRESHOLD or greater than THRESHOLD. */ 7303 7304 static void 7305 insert_into_classtype_sorted_fields (tree fields, tree t, int threshold) 7306 { 7307 int n_fields = count_fields (fields); 7308 if (n_fields >= threshold) 7309 { 7310 struct sorted_fields_type *field_vec = sorted_fields_type_new (n_fields); 7311 add_fields_to_record_type (fields, field_vec, 0); 7312 qsort (field_vec->elts, n_fields, sizeof (tree), field_decl_cmp); 7313 CLASSTYPE_SORTED_FIELDS (t) = field_vec; 7314 } 7315 } 7316 7317 /* Insert lately defined enum ENUMTYPE into T for the sorted case. */ 7318 7319 void 7320 insert_late_enum_def_into_classtype_sorted_fields (tree enumtype, tree t) 7321 { 7322 struct sorted_fields_type *sorted_fields = CLASSTYPE_SORTED_FIELDS (t); 7323 if (sorted_fields) 7324 { 7325 int i; 7326 int n_fields 7327 = list_length (TYPE_VALUES (enumtype)) + sorted_fields->len; 7328 struct sorted_fields_type *field_vec = sorted_fields_type_new (n_fields); 7329 7330 for (i = 0; i < sorted_fields->len; ++i) 7331 field_vec->elts[i] = sorted_fields->elts[i]; 7332 7333 add_enum_fields_to_record_type (enumtype, field_vec, 7334 sorted_fields->len); 7335 qsort (field_vec->elts, n_fields, sizeof (tree), field_decl_cmp); 7336 CLASSTYPE_SORTED_FIELDS (t) = field_vec; 7337 } 7338 } 7339 7340 /* When T was built up, the member declarations were added in reverse 7341 order. Rearrange them to declaration order. */ 7342 7343 void 7344 unreverse_member_declarations (tree t) 7345 { 7346 tree next; 7347 tree prev; 7348 tree x; 7349 7350 /* The following lists are all in reverse order. Put them in 7351 declaration order now. */ 7352 TYPE_METHODS (t) = nreverse (TYPE_METHODS (t)); 7353 CLASSTYPE_DECL_LIST (t) = nreverse (CLASSTYPE_DECL_LIST (t)); 7354 7355 /* Actually, for the TYPE_FIELDS, only the non TYPE_DECLs are in 7356 reverse order, so we can't just use nreverse. */ 7357 prev = NULL_TREE; 7358 for (x = TYPE_FIELDS (t); 7359 x && TREE_CODE (x) != TYPE_DECL; 7360 x = next) 7361 { 7362 next = DECL_CHAIN (x); 7363 DECL_CHAIN (x) = prev; 7364 prev = x; 7365 } 7366 if (prev) 7367 { 7368 DECL_CHAIN (TYPE_FIELDS (t)) = x; 7369 if (prev) 7370 TYPE_FIELDS (t) = prev; 7371 } 7372 } 7373 7374 tree 7375 finish_struct (tree t, tree attributes) 7376 { 7377 location_t saved_loc = input_location; 7378 7379 /* Now that we've got all the field declarations, reverse everything 7380 as necessary. */ 7381 unreverse_member_declarations (t); 7382 7383 cplus_decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE); 7384 fixup_attribute_variants (t); 7385 7386 /* Nadger the current location so that diagnostics point to the start of 7387 the struct, not the end. */ 7388 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (t)); 7389 7390 if (processing_template_decl) 7391 { 7392 tree x; 7393 7394 finish_struct_methods (t); 7395 TYPE_SIZE (t) = bitsize_zero_node; 7396 TYPE_SIZE_UNIT (t) = size_zero_node; 7397 7398 /* We need to emit an error message if this type was used as a parameter 7399 and it is an abstract type, even if it is a template. We construct 7400 a simple CLASSTYPE_PURE_VIRTUALS list without taking bases into 7401 account and we call complete_vars with this type, which will check 7402 the PARM_DECLS. Note that while the type is being defined, 7403 CLASSTYPE_PURE_VIRTUALS contains the list of the inline friends 7404 (see CLASSTYPE_INLINE_FRIENDS) so we need to clear it. */ 7405 CLASSTYPE_PURE_VIRTUALS (t) = NULL; 7406 for (x = TYPE_METHODS (t); x; x = DECL_CHAIN (x)) 7407 if (DECL_PURE_VIRTUAL_P (x)) 7408 vec_safe_push (CLASSTYPE_PURE_VIRTUALS (t), x); 7409 complete_vars (t); 7410 /* We need to add the target functions to the CLASSTYPE_METHOD_VEC if 7411 an enclosing scope is a template class, so that this function be 7412 found by lookup_fnfields_1 when the using declaration is not 7413 instantiated yet. */ 7414 for (x = TYPE_FIELDS (t); x; x = DECL_CHAIN (x)) 7415 if (TREE_CODE (x) == USING_DECL) 7416 { 7417 tree fn = strip_using_decl (x); 7418 if (is_overloaded_fn (fn)) 7419 for (; fn; fn = OVL_NEXT (fn)) 7420 add_method (t, OVL_CURRENT (fn), x); 7421 } 7422 7423 /* Remember current #pragma pack value. */ 7424 TYPE_PRECISION (t) = maximum_field_alignment; 7425 7426 /* Fix up any variants we've already built. */ 7427 for (x = TYPE_NEXT_VARIANT (t); x; x = TYPE_NEXT_VARIANT (x)) 7428 { 7429 TYPE_SIZE (x) = TYPE_SIZE (t); 7430 TYPE_SIZE_UNIT (x) = TYPE_SIZE_UNIT (t); 7431 TYPE_FIELDS (x) = TYPE_FIELDS (t); 7432 TYPE_METHODS (x) = TYPE_METHODS (t); 7433 } 7434 } 7435 else 7436 finish_struct_1 (t); 7437 7438 if (is_std_init_list (t)) 7439 { 7440 /* People keep complaining that the compiler crashes on an invalid 7441 definition of initializer_list, so I guess we should explicitly 7442 reject it. What the compiler internals care about is that it's a 7443 template and has a pointer field followed by an integer field. */ 7444 bool ok = false; 7445 if (processing_template_decl) 7446 { 7447 tree f = next_initializable_field (TYPE_FIELDS (t)); 7448 if (f && TREE_CODE (TREE_TYPE (f)) == POINTER_TYPE) 7449 { 7450 f = next_initializable_field (DECL_CHAIN (f)); 7451 if (f && same_type_p (TREE_TYPE (f), size_type_node)) 7452 ok = true; 7453 } 7454 } 7455 if (!ok) 7456 fatal_error (input_location, 7457 "definition of std::initializer_list does not match " 7458 "#include <initializer_list>"); 7459 } 7460 7461 input_location = saved_loc; 7462 7463 TYPE_BEING_DEFINED (t) = 0; 7464 7465 if (current_class_type) 7466 popclass (); 7467 else 7468 error ("trying to finish struct, but kicked out due to previous parse errors"); 7469 7470 if (processing_template_decl && at_function_scope_p () 7471 /* Lambdas are defined by the LAMBDA_EXPR. */ 7472 && !LAMBDA_TYPE_P (t)) 7473 add_stmt (build_min (TAG_DEFN, t)); 7474 7475 return t; 7476 } 7477 7478 /* Hash table to avoid endless recursion when handling references. */ 7479 static hash_table<nofree_ptr_hash<tree_node> > *fixed_type_or_null_ref_ht; 7480 7481 /* Return the dynamic type of INSTANCE, if known. 7482 Used to determine whether the virtual function table is needed 7483 or not. 7484 7485 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless 7486 of our knowledge of its type. *NONNULL should be initialized 7487 before this function is called. */ 7488 7489 static tree 7490 fixed_type_or_null (tree instance, int *nonnull, int *cdtorp) 7491 { 7492 #define RECUR(T) fixed_type_or_null((T), nonnull, cdtorp) 7493 7494 switch (TREE_CODE (instance)) 7495 { 7496 case INDIRECT_REF: 7497 if (POINTER_TYPE_P (TREE_TYPE (instance))) 7498 return NULL_TREE; 7499 else 7500 return RECUR (TREE_OPERAND (instance, 0)); 7501 7502 case CALL_EXPR: 7503 /* This is a call to a constructor, hence it's never zero. */ 7504 if (TREE_HAS_CONSTRUCTOR (instance)) 7505 { 7506 if (nonnull) 7507 *nonnull = 1; 7508 return TREE_TYPE (instance); 7509 } 7510 return NULL_TREE; 7511 7512 case SAVE_EXPR: 7513 /* This is a call to a constructor, hence it's never zero. */ 7514 if (TREE_HAS_CONSTRUCTOR (instance)) 7515 { 7516 if (nonnull) 7517 *nonnull = 1; 7518 return TREE_TYPE (instance); 7519 } 7520 return RECUR (TREE_OPERAND (instance, 0)); 7521 7522 case POINTER_PLUS_EXPR: 7523 case PLUS_EXPR: 7524 case MINUS_EXPR: 7525 if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR) 7526 return RECUR (TREE_OPERAND (instance, 0)); 7527 if (TREE_CODE (TREE_OPERAND (instance, 1)) == INTEGER_CST) 7528 /* Propagate nonnull. */ 7529 return RECUR (TREE_OPERAND (instance, 0)); 7530 7531 return NULL_TREE; 7532 7533 CASE_CONVERT: 7534 return RECUR (TREE_OPERAND (instance, 0)); 7535 7536 case ADDR_EXPR: 7537 instance = TREE_OPERAND (instance, 0); 7538 if (nonnull) 7539 { 7540 /* Just because we see an ADDR_EXPR doesn't mean we're dealing 7541 with a real object -- given &p->f, p can still be null. */ 7542 tree t = get_base_address (instance); 7543 /* ??? Probably should check DECL_WEAK here. */ 7544 if (t && DECL_P (t)) 7545 *nonnull = 1; 7546 } 7547 return RECUR (instance); 7548 7549 case COMPONENT_REF: 7550 /* If this component is really a base class reference, then the field 7551 itself isn't definitive. */ 7552 if (DECL_FIELD_IS_BASE (TREE_OPERAND (instance, 1))) 7553 return RECUR (TREE_OPERAND (instance, 0)); 7554 return RECUR (TREE_OPERAND (instance, 1)); 7555 7556 case VAR_DECL: 7557 case FIELD_DECL: 7558 if (TREE_CODE (TREE_TYPE (instance)) == ARRAY_TYPE 7559 && MAYBE_CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (instance)))) 7560 { 7561 if (nonnull) 7562 *nonnull = 1; 7563 return TREE_TYPE (TREE_TYPE (instance)); 7564 } 7565 /* fall through... */ 7566 case TARGET_EXPR: 7567 case PARM_DECL: 7568 case RESULT_DECL: 7569 if (MAYBE_CLASS_TYPE_P (TREE_TYPE (instance))) 7570 { 7571 if (nonnull) 7572 *nonnull = 1; 7573 return TREE_TYPE (instance); 7574 } 7575 else if (instance == current_class_ptr) 7576 { 7577 if (nonnull) 7578 *nonnull = 1; 7579 7580 /* if we're in a ctor or dtor, we know our type. If 7581 current_class_ptr is set but we aren't in a function, we're in 7582 an NSDMI (and therefore a constructor). */ 7583 if (current_scope () != current_function_decl 7584 || (DECL_LANG_SPECIFIC (current_function_decl) 7585 && (DECL_CONSTRUCTOR_P (current_function_decl) 7586 || DECL_DESTRUCTOR_P (current_function_decl)))) 7587 { 7588 if (cdtorp) 7589 *cdtorp = 1; 7590 return TREE_TYPE (TREE_TYPE (instance)); 7591 } 7592 } 7593 else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE) 7594 { 7595 /* We only need one hash table because it is always left empty. */ 7596 if (!fixed_type_or_null_ref_ht) 7597 fixed_type_or_null_ref_ht 7598 = new hash_table<nofree_ptr_hash<tree_node> > (37); 7599 7600 /* Reference variables should be references to objects. */ 7601 if (nonnull) 7602 *nonnull = 1; 7603 7604 /* Enter the INSTANCE in a table to prevent recursion; a 7605 variable's initializer may refer to the variable 7606 itself. */ 7607 if (VAR_P (instance) 7608 && DECL_INITIAL (instance) 7609 && !type_dependent_expression_p_push (DECL_INITIAL (instance)) 7610 && !fixed_type_or_null_ref_ht->find (instance)) 7611 { 7612 tree type; 7613 tree_node **slot; 7614 7615 slot = fixed_type_or_null_ref_ht->find_slot (instance, INSERT); 7616 *slot = instance; 7617 type = RECUR (DECL_INITIAL (instance)); 7618 fixed_type_or_null_ref_ht->remove_elt (instance); 7619 7620 return type; 7621 } 7622 } 7623 return NULL_TREE; 7624 7625 default: 7626 return NULL_TREE; 7627 } 7628 #undef RECUR 7629 } 7630 7631 /* Return nonzero if the dynamic type of INSTANCE is known, and 7632 equivalent to the static type. We also handle the case where 7633 INSTANCE is really a pointer. Return negative if this is a 7634 ctor/dtor. There the dynamic type is known, but this might not be 7635 the most derived base of the original object, and hence virtual 7636 bases may not be laid out according to this type. 7637 7638 Used to determine whether the virtual function table is needed 7639 or not. 7640 7641 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless 7642 of our knowledge of its type. *NONNULL should be initialized 7643 before this function is called. */ 7644 7645 int 7646 resolves_to_fixed_type_p (tree instance, int* nonnull) 7647 { 7648 tree t = TREE_TYPE (instance); 7649 int cdtorp = 0; 7650 tree fixed; 7651 7652 /* processing_template_decl can be false in a template if we're in 7653 instantiate_non_dependent_expr, but we still want to suppress 7654 this check. */ 7655 if (in_template_function ()) 7656 { 7657 /* In a template we only care about the type of the result. */ 7658 if (nonnull) 7659 *nonnull = true; 7660 return true; 7661 } 7662 7663 fixed = fixed_type_or_null (instance, nonnull, &cdtorp); 7664 if (fixed == NULL_TREE) 7665 return 0; 7666 if (POINTER_TYPE_P (t)) 7667 t = TREE_TYPE (t); 7668 if (!same_type_ignoring_top_level_qualifiers_p (t, fixed)) 7669 return 0; 7670 return cdtorp ? -1 : 1; 7671 } 7672 7673 7674 void 7675 init_class_processing (void) 7676 { 7677 current_class_depth = 0; 7678 current_class_stack_size = 10; 7679 current_class_stack 7680 = XNEWVEC (struct class_stack_node, current_class_stack_size); 7681 vec_alloc (local_classes, 8); 7682 sizeof_biggest_empty_class = size_zero_node; 7683 7684 ridpointers[(int) RID_PUBLIC] = access_public_node; 7685 ridpointers[(int) RID_PRIVATE] = access_private_node; 7686 ridpointers[(int) RID_PROTECTED] = access_protected_node; 7687 } 7688 7689 /* Restore the cached PREVIOUS_CLASS_LEVEL. */ 7690 7691 static void 7692 restore_class_cache (void) 7693 { 7694 tree type; 7695 7696 /* We are re-entering the same class we just left, so we don't 7697 have to search the whole inheritance matrix to find all the 7698 decls to bind again. Instead, we install the cached 7699 class_shadowed list and walk through it binding names. */ 7700 push_binding_level (previous_class_level); 7701 class_binding_level = previous_class_level; 7702 /* Restore IDENTIFIER_TYPE_VALUE. */ 7703 for (type = class_binding_level->type_shadowed; 7704 type; 7705 type = TREE_CHAIN (type)) 7706 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (type), TREE_TYPE (type)); 7707 } 7708 7709 /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE as 7710 appropriate for TYPE. 7711 7712 So that we may avoid calls to lookup_name, we cache the _TYPE 7713 nodes of local TYPE_DECLs in the TREE_TYPE field of the name. 7714 7715 For multiple inheritance, we perform a two-pass depth-first search 7716 of the type lattice. */ 7717 7718 void 7719 pushclass (tree type) 7720 { 7721 class_stack_node_t csn; 7722 7723 type = TYPE_MAIN_VARIANT (type); 7724 7725 /* Make sure there is enough room for the new entry on the stack. */ 7726 if (current_class_depth + 1 >= current_class_stack_size) 7727 { 7728 current_class_stack_size *= 2; 7729 current_class_stack 7730 = XRESIZEVEC (struct class_stack_node, current_class_stack, 7731 current_class_stack_size); 7732 } 7733 7734 /* Insert a new entry on the class stack. */ 7735 csn = current_class_stack + current_class_depth; 7736 csn->name = current_class_name; 7737 csn->type = current_class_type; 7738 csn->access = current_access_specifier; 7739 csn->names_used = 0; 7740 csn->hidden = 0; 7741 current_class_depth++; 7742 7743 /* Now set up the new type. */ 7744 current_class_name = TYPE_NAME (type); 7745 if (TREE_CODE (current_class_name) == TYPE_DECL) 7746 current_class_name = DECL_NAME (current_class_name); 7747 current_class_type = type; 7748 7749 /* By default, things in classes are private, while things in 7750 structures or unions are public. */ 7751 current_access_specifier = (CLASSTYPE_DECLARED_CLASS (type) 7752 ? access_private_node 7753 : access_public_node); 7754 7755 if (previous_class_level 7756 && type != previous_class_level->this_entity 7757 && current_class_depth == 1) 7758 { 7759 /* Forcibly remove any old class remnants. */ 7760 invalidate_class_lookup_cache (); 7761 } 7762 7763 if (!previous_class_level 7764 || type != previous_class_level->this_entity 7765 || current_class_depth > 1) 7766 pushlevel_class (); 7767 else 7768 restore_class_cache (); 7769 } 7770 7771 /* When we exit a toplevel class scope, we save its binding level so 7772 that we can restore it quickly. Here, we've entered some other 7773 class, so we must invalidate our cache. */ 7774 7775 void 7776 invalidate_class_lookup_cache (void) 7777 { 7778 previous_class_level = NULL; 7779 } 7780 7781 /* Get out of the current class scope. If we were in a class scope 7782 previously, that is the one popped to. */ 7783 7784 void 7785 popclass (void) 7786 { 7787 poplevel_class (); 7788 7789 current_class_depth--; 7790 current_class_name = current_class_stack[current_class_depth].name; 7791 current_class_type = current_class_stack[current_class_depth].type; 7792 current_access_specifier = current_class_stack[current_class_depth].access; 7793 if (current_class_stack[current_class_depth].names_used) 7794 splay_tree_delete (current_class_stack[current_class_depth].names_used); 7795 } 7796 7797 /* Mark the top of the class stack as hidden. */ 7798 7799 void 7800 push_class_stack (void) 7801 { 7802 if (current_class_depth) 7803 ++current_class_stack[current_class_depth - 1].hidden; 7804 } 7805 7806 /* Mark the top of the class stack as un-hidden. */ 7807 7808 void 7809 pop_class_stack (void) 7810 { 7811 if (current_class_depth) 7812 --current_class_stack[current_class_depth - 1].hidden; 7813 } 7814 7815 /* Returns 1 if the class type currently being defined is either T or 7816 a nested type of T. Returns the type from the current_class_stack, 7817 which might be equivalent to but not equal to T in case of 7818 constrained partial specializations. */ 7819 7820 tree 7821 currently_open_class (tree t) 7822 { 7823 int i; 7824 7825 if (!CLASS_TYPE_P (t)) 7826 return NULL_TREE; 7827 7828 t = TYPE_MAIN_VARIANT (t); 7829 7830 /* We start looking from 1 because entry 0 is from global scope, 7831 and has no type. */ 7832 for (i = current_class_depth; i > 0; --i) 7833 { 7834 tree c; 7835 if (i == current_class_depth) 7836 c = current_class_type; 7837 else 7838 { 7839 if (current_class_stack[i].hidden) 7840 break; 7841 c = current_class_stack[i].type; 7842 } 7843 if (!c) 7844 continue; 7845 if (same_type_p (c, t)) 7846 return c; 7847 } 7848 return NULL_TREE; 7849 } 7850 7851 /* If either current_class_type or one of its enclosing classes are derived 7852 from T, return the appropriate type. Used to determine how we found 7853 something via unqualified lookup. */ 7854 7855 tree 7856 currently_open_derived_class (tree t) 7857 { 7858 int i; 7859 7860 /* The bases of a dependent type are unknown. */ 7861 if (dependent_type_p (t)) 7862 return NULL_TREE; 7863 7864 if (!current_class_type) 7865 return NULL_TREE; 7866 7867 if (DERIVED_FROM_P (t, current_class_type)) 7868 return current_class_type; 7869 7870 for (i = current_class_depth - 1; i > 0; --i) 7871 { 7872 if (current_class_stack[i].hidden) 7873 break; 7874 if (DERIVED_FROM_P (t, current_class_stack[i].type)) 7875 return current_class_stack[i].type; 7876 } 7877 7878 return NULL_TREE; 7879 } 7880 7881 /* Return the outermost enclosing class type that is still open, or 7882 NULL_TREE. */ 7883 7884 tree 7885 outermost_open_class (void) 7886 { 7887 if (!current_class_type) 7888 return NULL_TREE; 7889 tree r = NULL_TREE; 7890 if (TYPE_BEING_DEFINED (current_class_type)) 7891 r = current_class_type; 7892 for (int i = current_class_depth - 1; i > 0; --i) 7893 { 7894 if (current_class_stack[i].hidden) 7895 break; 7896 tree t = current_class_stack[i].type; 7897 if (!TYPE_BEING_DEFINED (t)) 7898 break; 7899 r = t; 7900 } 7901 return r; 7902 } 7903 7904 /* Returns the innermost class type which is not a lambda closure type. */ 7905 7906 tree 7907 current_nonlambda_class_type (void) 7908 { 7909 int i; 7910 7911 /* We start looking from 1 because entry 0 is from global scope, 7912 and has no type. */ 7913 for (i = current_class_depth; i > 0; --i) 7914 { 7915 tree c; 7916 if (i == current_class_depth) 7917 c = current_class_type; 7918 else 7919 { 7920 if (current_class_stack[i].hidden) 7921 break; 7922 c = current_class_stack[i].type; 7923 } 7924 if (!c) 7925 continue; 7926 if (!LAMBDA_TYPE_P (c)) 7927 return c; 7928 } 7929 return NULL_TREE; 7930 } 7931 7932 /* When entering a class scope, all enclosing class scopes' names with 7933 static meaning (static variables, static functions, types and 7934 enumerators) have to be visible. This recursive function calls 7935 pushclass for all enclosing class contexts until global or a local 7936 scope is reached. TYPE is the enclosed class. */ 7937 7938 void 7939 push_nested_class (tree type) 7940 { 7941 /* A namespace might be passed in error cases, like A::B:C. */ 7942 if (type == NULL_TREE 7943 || !CLASS_TYPE_P (type)) 7944 return; 7945 7946 push_nested_class (DECL_CONTEXT (TYPE_MAIN_DECL (type))); 7947 7948 pushclass (type); 7949 } 7950 7951 /* Undoes a push_nested_class call. */ 7952 7953 void 7954 pop_nested_class (void) 7955 { 7956 tree context = DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type)); 7957 7958 popclass (); 7959 if (context && CLASS_TYPE_P (context)) 7960 pop_nested_class (); 7961 } 7962 7963 /* Returns the number of extern "LANG" blocks we are nested within. */ 7964 7965 int 7966 current_lang_depth (void) 7967 { 7968 return vec_safe_length (current_lang_base); 7969 } 7970 7971 /* Set global variables CURRENT_LANG_NAME to appropriate value 7972 so that behavior of name-mangling machinery is correct. */ 7973 7974 void 7975 push_lang_context (tree name) 7976 { 7977 vec_safe_push (current_lang_base, current_lang_name); 7978 7979 if (name == lang_name_cplusplus) 7980 { 7981 current_lang_name = name; 7982 } 7983 else if (name == lang_name_java) 7984 { 7985 current_lang_name = name; 7986 /* DECL_IGNORED_P is initially set for these types, to avoid clutter. 7987 (See record_builtin_java_type in decl.c.) However, that causes 7988 incorrect debug entries if these types are actually used. 7989 So we re-enable debug output after extern "Java". */ 7990 DECL_IGNORED_P (TYPE_NAME (java_byte_type_node)) = 0; 7991 DECL_IGNORED_P (TYPE_NAME (java_short_type_node)) = 0; 7992 DECL_IGNORED_P (TYPE_NAME (java_int_type_node)) = 0; 7993 DECL_IGNORED_P (TYPE_NAME (java_long_type_node)) = 0; 7994 DECL_IGNORED_P (TYPE_NAME (java_float_type_node)) = 0; 7995 DECL_IGNORED_P (TYPE_NAME (java_double_type_node)) = 0; 7996 DECL_IGNORED_P (TYPE_NAME (java_char_type_node)) = 0; 7997 DECL_IGNORED_P (TYPE_NAME (java_boolean_type_node)) = 0; 7998 } 7999 else if (name == lang_name_c) 8000 { 8001 current_lang_name = name; 8002 } 8003 else 8004 error ("language string %<\"%E\"%> not recognized", name); 8005 } 8006 8007 /* Get out of the current language scope. */ 8008 8009 void 8010 pop_lang_context (void) 8011 { 8012 current_lang_name = current_lang_base->pop (); 8013 } 8014 8015 /* Type instantiation routines. */ 8016 8017 /* Given an OVERLOAD and a TARGET_TYPE, return the function that 8018 matches the TARGET_TYPE. If there is no satisfactory match, return 8019 error_mark_node, and issue an error & warning messages under 8020 control of FLAGS. Permit pointers to member function if FLAGS 8021 permits. If TEMPLATE_ONLY, the name of the overloaded function was 8022 a template-id, and EXPLICIT_TARGS are the explicitly provided 8023 template arguments. 8024 8025 If OVERLOAD is for one or more member functions, then ACCESS_PATH 8026 is the base path used to reference those member functions. If 8027 the address is resolved to a member function, access checks will be 8028 performed and errors issued if appropriate. */ 8029 8030 static tree 8031 resolve_address_of_overloaded_function (tree target_type, 8032 tree overload, 8033 tsubst_flags_t complain, 8034 bool template_only, 8035 tree explicit_targs, 8036 tree access_path) 8037 { 8038 /* Here's what the standard says: 8039 8040 [over.over] 8041 8042 If the name is a function template, template argument deduction 8043 is done, and if the argument deduction succeeds, the deduced 8044 arguments are used to generate a single template function, which 8045 is added to the set of overloaded functions considered. 8046 8047 Non-member functions and static member functions match targets of 8048 type "pointer-to-function" or "reference-to-function." Nonstatic 8049 member functions match targets of type "pointer-to-member 8050 function;" the function type of the pointer to member is used to 8051 select the member function from the set of overloaded member 8052 functions. If a nonstatic member function is selected, the 8053 reference to the overloaded function name is required to have the 8054 form of a pointer to member as described in 5.3.1. 8055 8056 If more than one function is selected, any template functions in 8057 the set are eliminated if the set also contains a non-template 8058 function, and any given template function is eliminated if the 8059 set contains a second template function that is more specialized 8060 than the first according to the partial ordering rules 14.5.5.2. 8061 After such eliminations, if any, there shall remain exactly one 8062 selected function. */ 8063 8064 int is_ptrmem = 0; 8065 /* We store the matches in a TREE_LIST rooted here. The functions 8066 are the TREE_PURPOSE, not the TREE_VALUE, in this list, for easy 8067 interoperability with most_specialized_instantiation. */ 8068 tree matches = NULL_TREE; 8069 tree fn; 8070 tree target_fn_type; 8071 8072 /* By the time we get here, we should be seeing only real 8073 pointer-to-member types, not the internal POINTER_TYPE to 8074 METHOD_TYPE representation. */ 8075 gcc_assert (!TYPE_PTR_P (target_type) 8076 || TREE_CODE (TREE_TYPE (target_type)) != METHOD_TYPE); 8077 8078 gcc_assert (is_overloaded_fn (overload)); 8079 8080 /* Check that the TARGET_TYPE is reasonable. */ 8081 if (TYPE_PTRFN_P (target_type) 8082 || TYPE_REFFN_P (target_type)) 8083 /* This is OK. */; 8084 else if (TYPE_PTRMEMFUNC_P (target_type)) 8085 /* This is OK, too. */ 8086 is_ptrmem = 1; 8087 else if (TREE_CODE (target_type) == FUNCTION_TYPE) 8088 /* This is OK, too. This comes from a conversion to reference 8089 type. */ 8090 target_type = build_reference_type (target_type); 8091 else 8092 { 8093 if (complain & tf_error) 8094 error ("cannot resolve overloaded function %qD based on" 8095 " conversion to type %qT", 8096 DECL_NAME (OVL_FUNCTION (overload)), target_type); 8097 return error_mark_node; 8098 } 8099 8100 /* Non-member functions and static member functions match targets of type 8101 "pointer-to-function" or "reference-to-function." Nonstatic member 8102 functions match targets of type "pointer-to-member-function;" the 8103 function type of the pointer to member is used to select the member 8104 function from the set of overloaded member functions. 8105 8106 So figure out the FUNCTION_TYPE that we want to match against. */ 8107 target_fn_type = static_fn_type (target_type); 8108 8109 /* If we can find a non-template function that matches, we can just 8110 use it. There's no point in generating template instantiations 8111 if we're just going to throw them out anyhow. But, of course, we 8112 can only do this when we don't *need* a template function. */ 8113 if (!template_only) 8114 { 8115 tree fns; 8116 8117 for (fns = overload; fns; fns = OVL_NEXT (fns)) 8118 { 8119 tree fn = OVL_CURRENT (fns); 8120 8121 if (TREE_CODE (fn) == TEMPLATE_DECL) 8122 /* We're not looking for templates just yet. */ 8123 continue; 8124 8125 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE) 8126 != is_ptrmem) 8127 /* We're looking for a non-static member, and this isn't 8128 one, or vice versa. */ 8129 continue; 8130 8131 /* Ignore functions which haven't been explicitly 8132 declared. */ 8133 if (DECL_ANTICIPATED (fn)) 8134 continue; 8135 8136 /* See if there's a match. */ 8137 tree fntype = static_fn_type (fn); 8138 if (same_type_p (target_fn_type, fntype) 8139 || can_convert_tx_safety (target_fn_type, fntype)) 8140 matches = tree_cons (fn, NULL_TREE, matches); 8141 } 8142 } 8143 8144 /* Now, if we've already got a match (or matches), there's no need 8145 to proceed to the template functions. But, if we don't have a 8146 match we need to look at them, too. */ 8147 if (!matches) 8148 { 8149 tree target_arg_types; 8150 tree target_ret_type; 8151 tree fns; 8152 tree *args; 8153 unsigned int nargs, ia; 8154 tree arg; 8155 8156 target_arg_types = TYPE_ARG_TYPES (target_fn_type); 8157 target_ret_type = TREE_TYPE (target_fn_type); 8158 8159 nargs = list_length (target_arg_types); 8160 args = XALLOCAVEC (tree, nargs); 8161 for (arg = target_arg_types, ia = 0; 8162 arg != NULL_TREE && arg != void_list_node; 8163 arg = TREE_CHAIN (arg), ++ia) 8164 args[ia] = TREE_VALUE (arg); 8165 nargs = ia; 8166 8167 for (fns = overload; fns; fns = OVL_NEXT (fns)) 8168 { 8169 tree fn = OVL_CURRENT (fns); 8170 tree instantiation; 8171 tree targs; 8172 8173 if (TREE_CODE (fn) != TEMPLATE_DECL) 8174 /* We're only looking for templates. */ 8175 continue; 8176 8177 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE) 8178 != is_ptrmem) 8179 /* We're not looking for a non-static member, and this is 8180 one, or vice versa. */ 8181 continue; 8182 8183 tree ret = target_ret_type; 8184 8185 /* If the template has a deduced return type, don't expose it to 8186 template argument deduction. */ 8187 if (undeduced_auto_decl (fn)) 8188 ret = NULL_TREE; 8189 8190 /* Try to do argument deduction. */ 8191 targs = make_tree_vec (DECL_NTPARMS (fn)); 8192 instantiation = fn_type_unification (fn, explicit_targs, targs, args, 8193 nargs, ret, 8194 DEDUCE_EXACT, LOOKUP_NORMAL, 8195 false, false); 8196 if (instantiation == error_mark_node) 8197 /* Instantiation failed. */ 8198 continue; 8199 8200 /* Constraints must be satisfied. This is done before 8201 return type deduction since that instantiates the 8202 function. */ 8203 if (flag_concepts && !constraints_satisfied_p (instantiation)) 8204 continue; 8205 8206 /* And now force instantiation to do return type deduction. */ 8207 if (undeduced_auto_decl (instantiation)) 8208 { 8209 ++function_depth; 8210 instantiate_decl (instantiation, /*defer*/false, /*class*/false); 8211 --function_depth; 8212 8213 require_deduced_type (instantiation); 8214 } 8215 8216 /* See if there's a match. */ 8217 tree fntype = static_fn_type (instantiation); 8218 if (same_type_p (target_fn_type, fntype) 8219 || can_convert_tx_safety (target_fn_type, fntype)) 8220 matches = tree_cons (instantiation, fn, matches); 8221 } 8222 8223 /* Now, remove all but the most specialized of the matches. */ 8224 if (matches) 8225 { 8226 tree match = most_specialized_instantiation (matches); 8227 8228 if (match != error_mark_node) 8229 matches = tree_cons (TREE_PURPOSE (match), 8230 NULL_TREE, 8231 NULL_TREE); 8232 } 8233 } 8234 8235 /* Now we should have exactly one function in MATCHES. */ 8236 if (matches == NULL_TREE) 8237 { 8238 /* There were *no* matches. */ 8239 if (complain & tf_error) 8240 { 8241 error ("no matches converting function %qD to type %q#T", 8242 DECL_NAME (OVL_CURRENT (overload)), 8243 target_type); 8244 8245 print_candidates (overload); 8246 } 8247 return error_mark_node; 8248 } 8249 else if (TREE_CHAIN (matches)) 8250 { 8251 /* There were too many matches. First check if they're all 8252 the same function. */ 8253 tree match = NULL_TREE; 8254 8255 fn = TREE_PURPOSE (matches); 8256 8257 /* For multi-versioned functions, more than one match is just fine and 8258 decls_match will return false as they are different. */ 8259 for (match = TREE_CHAIN (matches); match; match = TREE_CHAIN (match)) 8260 if (!decls_match (fn, TREE_PURPOSE (match)) 8261 && !targetm.target_option.function_versions 8262 (fn, TREE_PURPOSE (match))) 8263 break; 8264 8265 if (match) 8266 { 8267 if (complain & tf_error) 8268 { 8269 error ("converting overloaded function %qD to type %q#T is ambiguous", 8270 DECL_NAME (OVL_FUNCTION (overload)), 8271 target_type); 8272 8273 /* Since print_candidates expects the functions in the 8274 TREE_VALUE slot, we flip them here. */ 8275 for (match = matches; match; match = TREE_CHAIN (match)) 8276 TREE_VALUE (match) = TREE_PURPOSE (match); 8277 8278 print_candidates (matches); 8279 } 8280 8281 return error_mark_node; 8282 } 8283 } 8284 8285 /* Good, exactly one match. Now, convert it to the correct type. */ 8286 fn = TREE_PURPOSE (matches); 8287 8288 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn) 8289 && !(complain & tf_ptrmem_ok) && !flag_ms_extensions) 8290 { 8291 static int explained; 8292 8293 if (!(complain & tf_error)) 8294 return error_mark_node; 8295 8296 permerror (input_location, "assuming pointer to member %qD", fn); 8297 if (!explained) 8298 { 8299 inform (input_location, "(a pointer to member can only be formed with %<&%E%>)", fn); 8300 explained = 1; 8301 } 8302 } 8303 8304 /* If a pointer to a function that is multi-versioned is requested, the 8305 pointer to the dispatcher function is returned instead. This works 8306 well because indirectly calling the function will dispatch the right 8307 function version at run-time. */ 8308 if (DECL_FUNCTION_VERSIONED (fn)) 8309 { 8310 fn = get_function_version_dispatcher (fn); 8311 if (fn == NULL) 8312 return error_mark_node; 8313 /* Mark all the versions corresponding to the dispatcher as used. */ 8314 if (!(complain & tf_conv)) 8315 mark_versions_used (fn); 8316 } 8317 8318 /* If we're doing overload resolution purely for the purpose of 8319 determining conversion sequences, we should not consider the 8320 function used. If this conversion sequence is selected, the 8321 function will be marked as used at this point. */ 8322 if (!(complain & tf_conv)) 8323 { 8324 /* Make =delete work with SFINAE. */ 8325 if (DECL_DELETED_FN (fn) && !(complain & tf_error)) 8326 return error_mark_node; 8327 if (!mark_used (fn, complain) && !(complain & tf_error)) 8328 return error_mark_node; 8329 } 8330 8331 /* We could not check access to member functions when this 8332 expression was originally created since we did not know at that 8333 time to which function the expression referred. */ 8334 if (DECL_FUNCTION_MEMBER_P (fn)) 8335 { 8336 gcc_assert (access_path); 8337 perform_or_defer_access_check (access_path, fn, fn, complain); 8338 } 8339 8340 if (TYPE_PTRFN_P (target_type) || TYPE_PTRMEMFUNC_P (target_type)) 8341 return cp_build_addr_expr (fn, complain); 8342 else 8343 { 8344 /* The target must be a REFERENCE_TYPE. Above, cp_build_unary_op 8345 will mark the function as addressed, but here we must do it 8346 explicitly. */ 8347 cxx_mark_addressable (fn); 8348 8349 return fn; 8350 } 8351 } 8352 8353 /* This function will instantiate the type of the expression given in 8354 RHS to match the type of LHSTYPE. If errors exist, then return 8355 error_mark_node. COMPLAIN is a bit mask. If TF_ERROR is set, then 8356 we complain on errors. If we are not complaining, never modify rhs, 8357 as overload resolution wants to try many possible instantiations, in 8358 the hope that at least one will work. 8359 8360 For non-recursive calls, LHSTYPE should be a function, pointer to 8361 function, or a pointer to member function. */ 8362 8363 tree 8364 instantiate_type (tree lhstype, tree rhs, tsubst_flags_t complain) 8365 { 8366 tsubst_flags_t complain_in = complain; 8367 tree access_path = NULL_TREE; 8368 8369 complain &= ~tf_ptrmem_ok; 8370 8371 if (lhstype == unknown_type_node) 8372 { 8373 if (complain & tf_error) 8374 error ("not enough type information"); 8375 return error_mark_node; 8376 } 8377 8378 if (TREE_TYPE (rhs) != NULL_TREE && ! (type_unknown_p (rhs))) 8379 { 8380 tree fntype = non_reference (lhstype); 8381 if (same_type_p (fntype, TREE_TYPE (rhs))) 8382 return rhs; 8383 if (flag_ms_extensions 8384 && TYPE_PTRMEMFUNC_P (fntype) 8385 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (rhs))) 8386 /* Microsoft allows `A::f' to be resolved to a 8387 pointer-to-member. */ 8388 ; 8389 else 8390 { 8391 if (complain & tf_error) 8392 error ("cannot convert %qE from type %qT to type %qT", 8393 rhs, TREE_TYPE (rhs), fntype); 8394 return error_mark_node; 8395 } 8396 } 8397 8398 if (BASELINK_P (rhs)) 8399 { 8400 access_path = BASELINK_ACCESS_BINFO (rhs); 8401 rhs = BASELINK_FUNCTIONS (rhs); 8402 } 8403 8404 /* If we are in a template, and have a NON_DEPENDENT_EXPR, we cannot 8405 deduce any type information. */ 8406 if (TREE_CODE (rhs) == NON_DEPENDENT_EXPR) 8407 { 8408 if (complain & tf_error) 8409 error ("not enough type information"); 8410 return error_mark_node; 8411 } 8412 8413 /* There only a few kinds of expressions that may have a type 8414 dependent on overload resolution. */ 8415 gcc_assert (TREE_CODE (rhs) == ADDR_EXPR 8416 || TREE_CODE (rhs) == COMPONENT_REF 8417 || is_overloaded_fn (rhs) 8418 || (flag_ms_extensions && TREE_CODE (rhs) == FUNCTION_DECL)); 8419 8420 /* This should really only be used when attempting to distinguish 8421 what sort of a pointer to function we have. For now, any 8422 arithmetic operation which is not supported on pointers 8423 is rejected as an error. */ 8424 8425 switch (TREE_CODE (rhs)) 8426 { 8427 case COMPONENT_REF: 8428 { 8429 tree member = TREE_OPERAND (rhs, 1); 8430 8431 member = instantiate_type (lhstype, member, complain); 8432 if (member != error_mark_node 8433 && TREE_SIDE_EFFECTS (TREE_OPERAND (rhs, 0))) 8434 /* Do not lose object's side effects. */ 8435 return build2 (COMPOUND_EXPR, TREE_TYPE (member), 8436 TREE_OPERAND (rhs, 0), member); 8437 return member; 8438 } 8439 8440 case OFFSET_REF: 8441 rhs = TREE_OPERAND (rhs, 1); 8442 if (BASELINK_P (rhs)) 8443 return instantiate_type (lhstype, rhs, complain_in); 8444 8445 /* This can happen if we are forming a pointer-to-member for a 8446 member template. */ 8447 gcc_assert (TREE_CODE (rhs) == TEMPLATE_ID_EXPR); 8448 8449 /* Fall through. */ 8450 8451 case TEMPLATE_ID_EXPR: 8452 { 8453 tree fns = TREE_OPERAND (rhs, 0); 8454 tree args = TREE_OPERAND (rhs, 1); 8455 8456 return 8457 resolve_address_of_overloaded_function (lhstype, fns, complain_in, 8458 /*template_only=*/true, 8459 args, access_path); 8460 } 8461 8462 case OVERLOAD: 8463 case FUNCTION_DECL: 8464 return 8465 resolve_address_of_overloaded_function (lhstype, rhs, complain_in, 8466 /*template_only=*/false, 8467 /*explicit_targs=*/NULL_TREE, 8468 access_path); 8469 8470 case ADDR_EXPR: 8471 { 8472 if (PTRMEM_OK_P (rhs)) 8473 complain |= tf_ptrmem_ok; 8474 8475 return instantiate_type (lhstype, TREE_OPERAND (rhs, 0), complain); 8476 } 8477 8478 case ERROR_MARK: 8479 return error_mark_node; 8480 8481 default: 8482 gcc_unreachable (); 8483 } 8484 return error_mark_node; 8485 } 8486 8487 /* Return the name of the virtual function pointer field 8488 (as an IDENTIFIER_NODE) for the given TYPE. Note that 8489 this may have to look back through base types to find the 8490 ultimate field name. (For single inheritance, these could 8491 all be the same name. Who knows for multiple inheritance). */ 8492 8493 static tree 8494 get_vfield_name (tree type) 8495 { 8496 tree binfo, base_binfo; 8497 char *buf; 8498 8499 for (binfo = TYPE_BINFO (type); 8500 BINFO_N_BASE_BINFOS (binfo); 8501 binfo = base_binfo) 8502 { 8503 base_binfo = BINFO_BASE_BINFO (binfo, 0); 8504 8505 if (BINFO_VIRTUAL_P (base_binfo) 8506 || !TYPE_CONTAINS_VPTR_P (BINFO_TYPE (base_binfo))) 8507 break; 8508 } 8509 8510 type = BINFO_TYPE (binfo); 8511 buf = (char *) alloca (sizeof (VFIELD_NAME_FORMAT) 8512 + TYPE_NAME_LENGTH (type) + 2); 8513 sprintf (buf, VFIELD_NAME_FORMAT, 8514 IDENTIFIER_POINTER (constructor_name (type))); 8515 return get_identifier (buf); 8516 } 8517 8518 void 8519 print_class_statistics (void) 8520 { 8521 if (! GATHER_STATISTICS) 8522 return; 8523 8524 fprintf (stderr, "convert_harshness = %d\n", n_convert_harshness); 8525 fprintf (stderr, "compute_conversion_costs = %d\n", n_compute_conversion_costs); 8526 if (n_vtables) 8527 { 8528 fprintf (stderr, "vtables = %d; vtable searches = %d\n", 8529 n_vtables, n_vtable_searches); 8530 fprintf (stderr, "vtable entries = %d; vtable elems = %d\n", 8531 n_vtable_entries, n_vtable_elems); 8532 } 8533 } 8534 8535 /* Build a dummy reference to ourselves so Derived::Base (and A::A) works, 8536 according to [class]: 8537 The class-name is also inserted 8538 into the scope of the class itself. For purposes of access checking, 8539 the inserted class name is treated as if it were a public member name. */ 8540 8541 void 8542 build_self_reference (void) 8543 { 8544 tree name = constructor_name (current_class_type); 8545 tree value = build_lang_decl (TYPE_DECL, name, current_class_type); 8546 tree saved_cas; 8547 8548 DECL_NONLOCAL (value) = 1; 8549 DECL_CONTEXT (value) = current_class_type; 8550 DECL_ARTIFICIAL (value) = 1; 8551 SET_DECL_SELF_REFERENCE_P (value); 8552 set_underlying_type (value); 8553 8554 if (processing_template_decl) 8555 value = push_template_decl (value); 8556 8557 saved_cas = current_access_specifier; 8558 current_access_specifier = access_public_node; 8559 finish_member_declaration (value); 8560 current_access_specifier = saved_cas; 8561 } 8562 8563 /* Returns 1 if TYPE contains only padding bytes. */ 8564 8565 int 8566 is_empty_class (tree type) 8567 { 8568 if (type == error_mark_node) 8569 return 0; 8570 8571 if (! CLASS_TYPE_P (type)) 8572 return 0; 8573 8574 return CLASSTYPE_EMPTY_P (type); 8575 } 8576 8577 /* Returns true if TYPE contains no actual data, just various 8578 possible combinations of empty classes and possibly a vptr. */ 8579 8580 bool 8581 is_really_empty_class (tree type) 8582 { 8583 if (CLASS_TYPE_P (type)) 8584 { 8585 tree field; 8586 tree binfo; 8587 tree base_binfo; 8588 int i; 8589 8590 /* CLASSTYPE_EMPTY_P isn't set properly until the class is actually laid 8591 out, but we'd like to be able to check this before then. */ 8592 if (COMPLETE_TYPE_P (type) && is_empty_class (type)) 8593 return true; 8594 8595 for (binfo = TYPE_BINFO (type), i = 0; 8596 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i) 8597 if (!is_really_empty_class (BINFO_TYPE (base_binfo))) 8598 return false; 8599 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field)) 8600 if (TREE_CODE (field) == FIELD_DECL 8601 && !DECL_ARTIFICIAL (field) 8602 /* An unnamed bit-field is not a data member. */ 8603 && (DECL_NAME (field) || !DECL_C_BIT_FIELD (field)) 8604 && !is_really_empty_class (TREE_TYPE (field))) 8605 return false; 8606 return true; 8607 } 8608 else if (TREE_CODE (type) == ARRAY_TYPE) 8609 return (integer_zerop (array_type_nelts_top (type)) 8610 || is_really_empty_class (TREE_TYPE (type))); 8611 return false; 8612 } 8613 8614 /* Note that NAME was looked up while the current class was being 8615 defined and that the result of that lookup was DECL. */ 8616 8617 void 8618 maybe_note_name_used_in_class (tree name, tree decl) 8619 { 8620 splay_tree names_used; 8621 8622 /* If we're not defining a class, there's nothing to do. */ 8623 if (!(innermost_scope_kind() == sk_class 8624 && TYPE_BEING_DEFINED (current_class_type) 8625 && !LAMBDA_TYPE_P (current_class_type))) 8626 return; 8627 8628 /* If there's already a binding for this NAME, then we don't have 8629 anything to worry about. */ 8630 if (lookup_member (current_class_type, name, 8631 /*protect=*/0, /*want_type=*/false, tf_warning_or_error)) 8632 return; 8633 8634 if (!current_class_stack[current_class_depth - 1].names_used) 8635 current_class_stack[current_class_depth - 1].names_used 8636 = splay_tree_new (splay_tree_compare_pointers, 0, 0); 8637 names_used = current_class_stack[current_class_depth - 1].names_used; 8638 8639 splay_tree_insert (names_used, 8640 (splay_tree_key) name, 8641 (splay_tree_value) decl); 8642 } 8643 8644 /* Note that NAME was declared (as DECL) in the current class. Check 8645 to see that the declaration is valid. */ 8646 8647 void 8648 note_name_declared_in_class (tree name, tree decl) 8649 { 8650 splay_tree names_used; 8651 splay_tree_node n; 8652 8653 /* Look to see if we ever used this name. */ 8654 names_used 8655 = current_class_stack[current_class_depth - 1].names_used; 8656 if (!names_used) 8657 return; 8658 /* The C language allows members to be declared with a type of the same 8659 name, and the C++ standard says this diagnostic is not required. So 8660 allow it in extern "C" blocks unless predantic is specified. 8661 Allow it in all cases if -ms-extensions is specified. */ 8662 if ((!pedantic && current_lang_name == lang_name_c) 8663 || flag_ms_extensions) 8664 return; 8665 n = splay_tree_lookup (names_used, (splay_tree_key) name); 8666 if (n) 8667 { 8668 /* [basic.scope.class] 8669 8670 A name N used in a class S shall refer to the same declaration 8671 in its context and when re-evaluated in the completed scope of 8672 S. */ 8673 permerror (input_location, "declaration of %q#D", decl); 8674 permerror (location_of ((tree) n->value), 8675 "changes meaning of %qD from %q#D", 8676 DECL_NAME (OVL_CURRENT (decl)), (tree) n->value); 8677 } 8678 } 8679 8680 /* Returns the VAR_DECL for the complete vtable associated with BINFO. 8681 Secondary vtables are merged with primary vtables; this function 8682 will return the VAR_DECL for the primary vtable. */ 8683 8684 tree 8685 get_vtbl_decl_for_binfo (tree binfo) 8686 { 8687 tree decl; 8688 8689 decl = BINFO_VTABLE (binfo); 8690 if (decl && TREE_CODE (decl) == POINTER_PLUS_EXPR) 8691 { 8692 gcc_assert (TREE_CODE (TREE_OPERAND (decl, 0)) == ADDR_EXPR); 8693 decl = TREE_OPERAND (TREE_OPERAND (decl, 0), 0); 8694 } 8695 if (decl) 8696 gcc_assert (VAR_P (decl)); 8697 return decl; 8698 } 8699 8700 8701 /* Returns the binfo for the primary base of BINFO. If the resulting 8702 BINFO is a virtual base, and it is inherited elsewhere in the 8703 hierarchy, then the returned binfo might not be the primary base of 8704 BINFO in the complete object. Check BINFO_PRIMARY_P or 8705 BINFO_LOST_PRIMARY_P to be sure. */ 8706 8707 static tree 8708 get_primary_binfo (tree binfo) 8709 { 8710 tree primary_base; 8711 8712 primary_base = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (binfo)); 8713 if (!primary_base) 8714 return NULL_TREE; 8715 8716 return copied_binfo (primary_base, binfo); 8717 } 8718 8719 /* As above, but iterate until we reach the binfo that actually provides the 8720 vptr for BINFO. */ 8721 8722 static tree 8723 most_primary_binfo (tree binfo) 8724 { 8725 tree b = binfo; 8726 while (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (b)) 8727 && !BINFO_LOST_PRIMARY_P (b)) 8728 { 8729 tree primary_base = get_primary_binfo (b); 8730 gcc_assert (BINFO_PRIMARY_P (primary_base) 8731 && BINFO_INHERITANCE_CHAIN (primary_base) == b); 8732 b = primary_base; 8733 } 8734 return b; 8735 } 8736 8737 /* Returns true if BINFO gets its vptr from a virtual base of the most derived 8738 type. Note that the virtual inheritance might be above or below BINFO in 8739 the hierarchy. */ 8740 8741 bool 8742 vptr_via_virtual_p (tree binfo) 8743 { 8744 if (TYPE_P (binfo)) 8745 binfo = TYPE_BINFO (binfo); 8746 tree primary = most_primary_binfo (binfo); 8747 /* Don't limit binfo_via_virtual, we want to return true when BINFO itself is 8748 a morally virtual base. */ 8749 tree virt = binfo_via_virtual (primary, NULL_TREE); 8750 return virt != NULL_TREE; 8751 } 8752 8753 /* If INDENTED_P is zero, indent to INDENT. Return nonzero. */ 8754 8755 static int 8756 maybe_indent_hierarchy (FILE * stream, int indent, int indented_p) 8757 { 8758 if (!indented_p) 8759 fprintf (stream, "%*s", indent, ""); 8760 return 1; 8761 } 8762 8763 /* Dump the offsets of all the bases rooted at BINFO to STREAM. 8764 INDENT should be zero when called from the top level; it is 8765 incremented recursively. IGO indicates the next expected BINFO in 8766 inheritance graph ordering. */ 8767 8768 static tree 8769 dump_class_hierarchy_r (FILE *stream, 8770 int flags, 8771 tree binfo, 8772 tree igo, 8773 int indent) 8774 { 8775 int indented = 0; 8776 tree base_binfo; 8777 int i; 8778 8779 indented = maybe_indent_hierarchy (stream, indent, 0); 8780 fprintf (stream, "%s (0x" HOST_WIDE_INT_PRINT_HEX ") ", 8781 type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER), 8782 (HOST_WIDE_INT) (uintptr_t) binfo); 8783 if (binfo != igo) 8784 { 8785 fprintf (stream, "alternative-path\n"); 8786 return igo; 8787 } 8788 igo = TREE_CHAIN (binfo); 8789 8790 fprintf (stream, HOST_WIDE_INT_PRINT_DEC, 8791 tree_to_shwi (BINFO_OFFSET (binfo))); 8792 if (is_empty_class (BINFO_TYPE (binfo))) 8793 fprintf (stream, " empty"); 8794 else if (CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (binfo))) 8795 fprintf (stream, " nearly-empty"); 8796 if (BINFO_VIRTUAL_P (binfo)) 8797 fprintf (stream, " virtual"); 8798 fprintf (stream, "\n"); 8799 8800 indented = 0; 8801 if (BINFO_PRIMARY_P (binfo)) 8802 { 8803 indented = maybe_indent_hierarchy (stream, indent + 3, indented); 8804 fprintf (stream, " primary-for %s (0x" HOST_WIDE_INT_PRINT_HEX ")", 8805 type_as_string (BINFO_TYPE (BINFO_INHERITANCE_CHAIN (binfo)), 8806 TFF_PLAIN_IDENTIFIER), 8807 (HOST_WIDE_INT) (uintptr_t) BINFO_INHERITANCE_CHAIN (binfo)); 8808 } 8809 if (BINFO_LOST_PRIMARY_P (binfo)) 8810 { 8811 indented = maybe_indent_hierarchy (stream, indent + 3, indented); 8812 fprintf (stream, " lost-primary"); 8813 } 8814 if (indented) 8815 fprintf (stream, "\n"); 8816 8817 if (!(flags & TDF_SLIM)) 8818 { 8819 int indented = 0; 8820 8821 if (BINFO_SUBVTT_INDEX (binfo)) 8822 { 8823 indented = maybe_indent_hierarchy (stream, indent + 3, indented); 8824 fprintf (stream, " subvttidx=%s", 8825 expr_as_string (BINFO_SUBVTT_INDEX (binfo), 8826 TFF_PLAIN_IDENTIFIER)); 8827 } 8828 if (BINFO_VPTR_INDEX (binfo)) 8829 { 8830 indented = maybe_indent_hierarchy (stream, indent + 3, indented); 8831 fprintf (stream, " vptridx=%s", 8832 expr_as_string (BINFO_VPTR_INDEX (binfo), 8833 TFF_PLAIN_IDENTIFIER)); 8834 } 8835 if (BINFO_VPTR_FIELD (binfo)) 8836 { 8837 indented = maybe_indent_hierarchy (stream, indent + 3, indented); 8838 fprintf (stream, " vbaseoffset=%s", 8839 expr_as_string (BINFO_VPTR_FIELD (binfo), 8840 TFF_PLAIN_IDENTIFIER)); 8841 } 8842 if (BINFO_VTABLE (binfo)) 8843 { 8844 indented = maybe_indent_hierarchy (stream, indent + 3, indented); 8845 fprintf (stream, " vptr=%s", 8846 expr_as_string (BINFO_VTABLE (binfo), 8847 TFF_PLAIN_IDENTIFIER)); 8848 } 8849 8850 if (indented) 8851 fprintf (stream, "\n"); 8852 } 8853 8854 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++) 8855 igo = dump_class_hierarchy_r (stream, flags, base_binfo, igo, indent + 2); 8856 8857 return igo; 8858 } 8859 8860 /* Dump the BINFO hierarchy for T. */ 8861 8862 static void 8863 dump_class_hierarchy_1 (FILE *stream, int flags, tree t) 8864 { 8865 fprintf (stream, "Class %s\n", type_as_string (t, TFF_PLAIN_IDENTIFIER)); 8866 fprintf (stream, " size=%lu align=%lu\n", 8867 (unsigned long)(tree_to_shwi (TYPE_SIZE (t)) / BITS_PER_UNIT), 8868 (unsigned long)(TYPE_ALIGN (t) / BITS_PER_UNIT)); 8869 fprintf (stream, " base size=%lu base align=%lu\n", 8870 (unsigned long)(tree_to_shwi (TYPE_SIZE (CLASSTYPE_AS_BASE (t))) 8871 / BITS_PER_UNIT), 8872 (unsigned long)(TYPE_ALIGN (CLASSTYPE_AS_BASE (t)) 8873 / BITS_PER_UNIT)); 8874 dump_class_hierarchy_r (stream, flags, TYPE_BINFO (t), TYPE_BINFO (t), 0); 8875 fprintf (stream, "\n"); 8876 } 8877 8878 /* Debug interface to hierarchy dumping. */ 8879 8880 void 8881 debug_class (tree t) 8882 { 8883 dump_class_hierarchy_1 (stderr, TDF_SLIM, t); 8884 } 8885 8886 static void 8887 dump_class_hierarchy (tree t) 8888 { 8889 int flags; 8890 FILE *stream = get_dump_info (TDI_class, &flags); 8891 8892 if (stream) 8893 { 8894 dump_class_hierarchy_1 (stream, flags, t); 8895 } 8896 } 8897 8898 static void 8899 dump_array (FILE * stream, tree decl) 8900 { 8901 tree value; 8902 unsigned HOST_WIDE_INT ix; 8903 HOST_WIDE_INT elt; 8904 tree size = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (decl))); 8905 8906 elt = (tree_to_shwi (TYPE_SIZE (TREE_TYPE (TREE_TYPE (decl)))) 8907 / BITS_PER_UNIT); 8908 fprintf (stream, "%s:", decl_as_string (decl, TFF_PLAIN_IDENTIFIER)); 8909 fprintf (stream, " %s entries", 8910 expr_as_string (size_binop (PLUS_EXPR, size, size_one_node), 8911 TFF_PLAIN_IDENTIFIER)); 8912 fprintf (stream, "\n"); 8913 8914 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)), 8915 ix, value) 8916 fprintf (stream, "%-4ld %s\n", (long)(ix * elt), 8917 expr_as_string (value, TFF_PLAIN_IDENTIFIER)); 8918 } 8919 8920 static void 8921 dump_vtable (tree t, tree binfo, tree vtable) 8922 { 8923 int flags; 8924 FILE *stream = get_dump_info (TDI_class, &flags); 8925 8926 if (!stream) 8927 return; 8928 8929 if (!(flags & TDF_SLIM)) 8930 { 8931 int ctor_vtbl_p = TYPE_BINFO (t) != binfo; 8932 8933 fprintf (stream, "%s for %s", 8934 ctor_vtbl_p ? "Construction vtable" : "Vtable", 8935 type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER)); 8936 if (ctor_vtbl_p) 8937 { 8938 if (!BINFO_VIRTUAL_P (binfo)) 8939 fprintf (stream, " (0x" HOST_WIDE_INT_PRINT_HEX " instance)", 8940 (HOST_WIDE_INT) (uintptr_t) binfo); 8941 fprintf (stream, " in %s", type_as_string (t, TFF_PLAIN_IDENTIFIER)); 8942 } 8943 fprintf (stream, "\n"); 8944 dump_array (stream, vtable); 8945 fprintf (stream, "\n"); 8946 } 8947 } 8948 8949 static void 8950 dump_vtt (tree t, tree vtt) 8951 { 8952 int flags; 8953 FILE *stream = get_dump_info (TDI_class, &flags); 8954 8955 if (!stream) 8956 return; 8957 8958 if (!(flags & TDF_SLIM)) 8959 { 8960 fprintf (stream, "VTT for %s\n", 8961 type_as_string (t, TFF_PLAIN_IDENTIFIER)); 8962 dump_array (stream, vtt); 8963 fprintf (stream, "\n"); 8964 } 8965 } 8966 8967 /* Dump a function or thunk and its thunkees. */ 8968 8969 static void 8970 dump_thunk (FILE *stream, int indent, tree thunk) 8971 { 8972 static const char spaces[] = " "; 8973 tree name = DECL_NAME (thunk); 8974 tree thunks; 8975 8976 fprintf (stream, "%.*s%p %s %s", indent, spaces, 8977 (void *)thunk, 8978 !DECL_THUNK_P (thunk) ? "function" 8979 : DECL_THIS_THUNK_P (thunk) ? "this-thunk" : "covariant-thunk", 8980 name ? IDENTIFIER_POINTER (name) : "<unset>"); 8981 if (DECL_THUNK_P (thunk)) 8982 { 8983 HOST_WIDE_INT fixed_adjust = THUNK_FIXED_OFFSET (thunk); 8984 tree virtual_adjust = THUNK_VIRTUAL_OFFSET (thunk); 8985 8986 fprintf (stream, " fixed=" HOST_WIDE_INT_PRINT_DEC, fixed_adjust); 8987 if (!virtual_adjust) 8988 /*NOP*/; 8989 else if (DECL_THIS_THUNK_P (thunk)) 8990 fprintf (stream, " vcall=" HOST_WIDE_INT_PRINT_DEC, 8991 tree_to_shwi (virtual_adjust)); 8992 else 8993 fprintf (stream, " vbase=" HOST_WIDE_INT_PRINT_DEC "(%s)", 8994 tree_to_shwi (BINFO_VPTR_FIELD (virtual_adjust)), 8995 type_as_string (BINFO_TYPE (virtual_adjust), TFF_SCOPE)); 8996 if (THUNK_ALIAS (thunk)) 8997 fprintf (stream, " alias to %p", (void *)THUNK_ALIAS (thunk)); 8998 } 8999 fprintf (stream, "\n"); 9000 for (thunks = DECL_THUNKS (thunk); thunks; thunks = TREE_CHAIN (thunks)) 9001 dump_thunk (stream, indent + 2, thunks); 9002 } 9003 9004 /* Dump the thunks for FN. */ 9005 9006 void 9007 debug_thunks (tree fn) 9008 { 9009 dump_thunk (stderr, 0, fn); 9010 } 9011 9012 /* Virtual function table initialization. */ 9013 9014 /* Create all the necessary vtables for T and its base classes. */ 9015 9016 static void 9017 finish_vtbls (tree t) 9018 { 9019 tree vbase; 9020 vec<constructor_elt, va_gc> *v = NULL; 9021 tree vtable = BINFO_VTABLE (TYPE_BINFO (t)); 9022 9023 /* We lay out the primary and secondary vtables in one contiguous 9024 vtable. The primary vtable is first, followed by the non-virtual 9025 secondary vtables in inheritance graph order. */ 9026 accumulate_vtbl_inits (TYPE_BINFO (t), TYPE_BINFO (t), TYPE_BINFO (t), 9027 vtable, t, &v); 9028 9029 /* Then come the virtual bases, also in inheritance graph order. */ 9030 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase)) 9031 { 9032 if (!BINFO_VIRTUAL_P (vbase)) 9033 continue; 9034 accumulate_vtbl_inits (vbase, vbase, TYPE_BINFO (t), vtable, t, &v); 9035 } 9036 9037 if (BINFO_VTABLE (TYPE_BINFO (t))) 9038 initialize_vtable (TYPE_BINFO (t), v); 9039 } 9040 9041 /* Initialize the vtable for BINFO with the INITS. */ 9042 9043 static void 9044 initialize_vtable (tree binfo, vec<constructor_elt, va_gc> *inits) 9045 { 9046 tree decl; 9047 9048 layout_vtable_decl (binfo, vec_safe_length (inits)); 9049 decl = get_vtbl_decl_for_binfo (binfo); 9050 initialize_artificial_var (decl, inits); 9051 dump_vtable (BINFO_TYPE (binfo), binfo, decl); 9052 } 9053 9054 /* Build the VTT (virtual table table) for T. 9055 A class requires a VTT if it has virtual bases. 9056 9057 This holds 9058 1 - primary virtual pointer for complete object T 9059 2 - secondary VTTs for each direct non-virtual base of T which requires a 9060 VTT 9061 3 - secondary virtual pointers for each direct or indirect base of T which 9062 has virtual bases or is reachable via a virtual path from T. 9063 4 - secondary VTTs for each direct or indirect virtual base of T. 9064 9065 Secondary VTTs look like complete object VTTs without part 4. */ 9066 9067 static void 9068 build_vtt (tree t) 9069 { 9070 tree type; 9071 tree vtt; 9072 tree index; 9073 vec<constructor_elt, va_gc> *inits; 9074 9075 /* Build up the initializers for the VTT. */ 9076 inits = NULL; 9077 index = size_zero_node; 9078 build_vtt_inits (TYPE_BINFO (t), t, &inits, &index); 9079 9080 /* If we didn't need a VTT, we're done. */ 9081 if (!inits) 9082 return; 9083 9084 /* Figure out the type of the VTT. */ 9085 type = build_array_of_n_type (const_ptr_type_node, 9086 inits->length ()); 9087 9088 /* Now, build the VTT object itself. */ 9089 vtt = build_vtable (t, mangle_vtt_for_type (t), type); 9090 initialize_artificial_var (vtt, inits); 9091 /* Add the VTT to the vtables list. */ 9092 DECL_CHAIN (vtt) = DECL_CHAIN (CLASSTYPE_VTABLES (t)); 9093 DECL_CHAIN (CLASSTYPE_VTABLES (t)) = vtt; 9094 9095 dump_vtt (t, vtt); 9096 } 9097 9098 /* When building a secondary VTT, BINFO_VTABLE is set to a TREE_LIST with 9099 PURPOSE the RTTI_BINFO, VALUE the real vtable pointer for this binfo, 9100 and CHAIN the vtable pointer for this binfo after construction is 9101 complete. VALUE can also be another BINFO, in which case we recurse. */ 9102 9103 static tree 9104 binfo_ctor_vtable (tree binfo) 9105 { 9106 tree vt; 9107 9108 while (1) 9109 { 9110 vt = BINFO_VTABLE (binfo); 9111 if (TREE_CODE (vt) == TREE_LIST) 9112 vt = TREE_VALUE (vt); 9113 if (TREE_CODE (vt) == TREE_BINFO) 9114 binfo = vt; 9115 else 9116 break; 9117 } 9118 9119 return vt; 9120 } 9121 9122 /* Data for secondary VTT initialization. */ 9123 struct secondary_vptr_vtt_init_data 9124 { 9125 /* Is this the primary VTT? */ 9126 bool top_level_p; 9127 9128 /* Current index into the VTT. */ 9129 tree index; 9130 9131 /* Vector of initializers built up. */ 9132 vec<constructor_elt, va_gc> *inits; 9133 9134 /* The type being constructed by this secondary VTT. */ 9135 tree type_being_constructed; 9136 }; 9137 9138 /* Recursively build the VTT-initializer for BINFO (which is in the 9139 hierarchy dominated by T). INITS points to the end of the initializer 9140 list to date. INDEX is the VTT index where the next element will be 9141 replaced. Iff BINFO is the binfo for T, this is the top level VTT (i.e. 9142 not a subvtt for some base of T). When that is so, we emit the sub-VTTs 9143 for virtual bases of T. When it is not so, we build the constructor 9144 vtables for the BINFO-in-T variant. */ 9145 9146 static void 9147 build_vtt_inits (tree binfo, tree t, vec<constructor_elt, va_gc> **inits, 9148 tree *index) 9149 { 9150 int i; 9151 tree b; 9152 tree init; 9153 secondary_vptr_vtt_init_data data; 9154 int top_level_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t); 9155 9156 /* We only need VTTs for subobjects with virtual bases. */ 9157 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))) 9158 return; 9159 9160 /* We need to use a construction vtable if this is not the primary 9161 VTT. */ 9162 if (!top_level_p) 9163 { 9164 build_ctor_vtbl_group (binfo, t); 9165 9166 /* Record the offset in the VTT where this sub-VTT can be found. */ 9167 BINFO_SUBVTT_INDEX (binfo) = *index; 9168 } 9169 9170 /* Add the address of the primary vtable for the complete object. */ 9171 init = binfo_ctor_vtable (binfo); 9172 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init); 9173 if (top_level_p) 9174 { 9175 gcc_assert (!BINFO_VPTR_INDEX (binfo)); 9176 BINFO_VPTR_INDEX (binfo) = *index; 9177 } 9178 *index = size_binop (PLUS_EXPR, *index, TYPE_SIZE_UNIT (ptr_type_node)); 9179 9180 /* Recursively add the secondary VTTs for non-virtual bases. */ 9181 for (i = 0; BINFO_BASE_ITERATE (binfo, i, b); ++i) 9182 if (!BINFO_VIRTUAL_P (b)) 9183 build_vtt_inits (b, t, inits, index); 9184 9185 /* Add secondary virtual pointers for all subobjects of BINFO with 9186 either virtual bases or reachable along a virtual path, except 9187 subobjects that are non-virtual primary bases. */ 9188 data.top_level_p = top_level_p; 9189 data.index = *index; 9190 data.inits = *inits; 9191 data.type_being_constructed = BINFO_TYPE (binfo); 9192 9193 dfs_walk_once (binfo, dfs_build_secondary_vptr_vtt_inits, NULL, &data); 9194 9195 *index = data.index; 9196 9197 /* data.inits might have grown as we added secondary virtual pointers. 9198 Make sure our caller knows about the new vector. */ 9199 *inits = data.inits; 9200 9201 if (top_level_p) 9202 /* Add the secondary VTTs for virtual bases in inheritance graph 9203 order. */ 9204 for (b = TYPE_BINFO (BINFO_TYPE (binfo)); b; b = TREE_CHAIN (b)) 9205 { 9206 if (!BINFO_VIRTUAL_P (b)) 9207 continue; 9208 9209 build_vtt_inits (b, t, inits, index); 9210 } 9211 else 9212 /* Remove the ctor vtables we created. */ 9213 dfs_walk_all (binfo, dfs_fixup_binfo_vtbls, NULL, binfo); 9214 } 9215 9216 /* Called from build_vtt_inits via dfs_walk. BINFO is the binfo for the base 9217 in most derived. DATA is a SECONDARY_VPTR_VTT_INIT_DATA structure. */ 9218 9219 static tree 9220 dfs_build_secondary_vptr_vtt_inits (tree binfo, void *data_) 9221 { 9222 secondary_vptr_vtt_init_data *data = (secondary_vptr_vtt_init_data *)data_; 9223 9224 /* We don't care about bases that don't have vtables. */ 9225 if (!TYPE_VFIELD (BINFO_TYPE (binfo))) 9226 return dfs_skip_bases; 9227 9228 /* We're only interested in proper subobjects of the type being 9229 constructed. */ 9230 if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), data->type_being_constructed)) 9231 return NULL_TREE; 9232 9233 /* We're only interested in bases with virtual bases or reachable 9234 via a virtual path from the type being constructed. */ 9235 if (!(CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)) 9236 || binfo_via_virtual (binfo, data->type_being_constructed))) 9237 return dfs_skip_bases; 9238 9239 /* We're not interested in non-virtual primary bases. */ 9240 if (!BINFO_VIRTUAL_P (binfo) && BINFO_PRIMARY_P (binfo)) 9241 return NULL_TREE; 9242 9243 /* Record the index where this secondary vptr can be found. */ 9244 if (data->top_level_p) 9245 { 9246 gcc_assert (!BINFO_VPTR_INDEX (binfo)); 9247 BINFO_VPTR_INDEX (binfo) = data->index; 9248 9249 if (BINFO_VIRTUAL_P (binfo)) 9250 { 9251 /* It's a primary virtual base, and this is not a 9252 construction vtable. Find the base this is primary of in 9253 the inheritance graph, and use that base's vtable 9254 now. */ 9255 while (BINFO_PRIMARY_P (binfo)) 9256 binfo = BINFO_INHERITANCE_CHAIN (binfo); 9257 } 9258 } 9259 9260 /* Add the initializer for the secondary vptr itself. */ 9261 CONSTRUCTOR_APPEND_ELT (data->inits, NULL_TREE, binfo_ctor_vtable (binfo)); 9262 9263 /* Advance the vtt index. */ 9264 data->index = size_binop (PLUS_EXPR, data->index, 9265 TYPE_SIZE_UNIT (ptr_type_node)); 9266 9267 return NULL_TREE; 9268 } 9269 9270 /* Called from build_vtt_inits via dfs_walk. After building 9271 constructor vtables and generating the sub-vtt from them, we need 9272 to restore the BINFO_VTABLES that were scribbled on. DATA is the 9273 binfo of the base whose sub vtt was generated. */ 9274 9275 static tree 9276 dfs_fixup_binfo_vtbls (tree binfo, void* data) 9277 { 9278 tree vtable = BINFO_VTABLE (binfo); 9279 9280 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo))) 9281 /* If this class has no vtable, none of its bases do. */ 9282 return dfs_skip_bases; 9283 9284 if (!vtable) 9285 /* This might be a primary base, so have no vtable in this 9286 hierarchy. */ 9287 return NULL_TREE; 9288 9289 /* If we scribbled the construction vtable vptr into BINFO, clear it 9290 out now. */ 9291 if (TREE_CODE (vtable) == TREE_LIST 9292 && (TREE_PURPOSE (vtable) == (tree) data)) 9293 BINFO_VTABLE (binfo) = TREE_CHAIN (vtable); 9294 9295 return NULL_TREE; 9296 } 9297 9298 /* Build the construction vtable group for BINFO which is in the 9299 hierarchy dominated by T. */ 9300 9301 static void 9302 build_ctor_vtbl_group (tree binfo, tree t) 9303 { 9304 tree type; 9305 tree vtbl; 9306 tree id; 9307 tree vbase; 9308 vec<constructor_elt, va_gc> *v; 9309 9310 /* See if we've already created this construction vtable group. */ 9311 id = mangle_ctor_vtbl_for_type (t, binfo); 9312 if (IDENTIFIER_GLOBAL_VALUE (id)) 9313 return; 9314 9315 gcc_assert (!SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t)); 9316 /* Build a version of VTBL (with the wrong type) for use in 9317 constructing the addresses of secondary vtables in the 9318 construction vtable group. */ 9319 vtbl = build_vtable (t, id, ptr_type_node); 9320 DECL_CONSTRUCTION_VTABLE_P (vtbl) = 1; 9321 /* Don't export construction vtables from shared libraries. Even on 9322 targets that don't support hidden visibility, this tells 9323 can_refer_decl_in_current_unit_p not to assume that it's safe to 9324 access from a different compilation unit (bz 54314). */ 9325 DECL_VISIBILITY (vtbl) = VISIBILITY_HIDDEN; 9326 DECL_VISIBILITY_SPECIFIED (vtbl) = true; 9327 9328 v = NULL; 9329 accumulate_vtbl_inits (binfo, TYPE_BINFO (TREE_TYPE (binfo)), 9330 binfo, vtbl, t, &v); 9331 9332 /* Add the vtables for each of our virtual bases using the vbase in T 9333 binfo. */ 9334 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo)); 9335 vbase; 9336 vbase = TREE_CHAIN (vbase)) 9337 { 9338 tree b; 9339 9340 if (!BINFO_VIRTUAL_P (vbase)) 9341 continue; 9342 b = copied_binfo (vbase, binfo); 9343 9344 accumulate_vtbl_inits (b, vbase, binfo, vtbl, t, &v); 9345 } 9346 9347 /* Figure out the type of the construction vtable. */ 9348 type = build_array_of_n_type (vtable_entry_type, v->length ()); 9349 layout_type (type); 9350 TREE_TYPE (vtbl) = type; 9351 DECL_SIZE (vtbl) = DECL_SIZE_UNIT (vtbl) = NULL_TREE; 9352 layout_decl (vtbl, 0); 9353 9354 /* Initialize the construction vtable. */ 9355 CLASSTYPE_VTABLES (t) = chainon (CLASSTYPE_VTABLES (t), vtbl); 9356 initialize_artificial_var (vtbl, v); 9357 dump_vtable (t, binfo, vtbl); 9358 } 9359 9360 /* Add the vtbl initializers for BINFO (and its bases other than 9361 non-virtual primaries) to the list of INITS. BINFO is in the 9362 hierarchy dominated by T. RTTI_BINFO is the binfo within T of 9363 the constructor the vtbl inits should be accumulated for. (If this 9364 is the complete object vtbl then RTTI_BINFO will be TYPE_BINFO (T).) 9365 ORIG_BINFO is the binfo for this object within BINFO_TYPE (RTTI_BINFO). 9366 BINFO is the active base equivalent of ORIG_BINFO in the inheritance 9367 graph of T. Both BINFO and ORIG_BINFO will have the same BINFO_TYPE, 9368 but are not necessarily the same in terms of layout. */ 9369 9370 static void 9371 accumulate_vtbl_inits (tree binfo, 9372 tree orig_binfo, 9373 tree rtti_binfo, 9374 tree vtbl, 9375 tree t, 9376 vec<constructor_elt, va_gc> **inits) 9377 { 9378 int i; 9379 tree base_binfo; 9380 int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t); 9381 9382 gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (orig_binfo))); 9383 9384 /* If it doesn't have a vptr, we don't do anything. */ 9385 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo))) 9386 return; 9387 9388 /* If we're building a construction vtable, we're not interested in 9389 subobjects that don't require construction vtables. */ 9390 if (ctor_vtbl_p 9391 && !CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)) 9392 && !binfo_via_virtual (orig_binfo, BINFO_TYPE (rtti_binfo))) 9393 return; 9394 9395 /* Build the initializers for the BINFO-in-T vtable. */ 9396 dfs_accumulate_vtbl_inits (binfo, orig_binfo, rtti_binfo, vtbl, t, inits); 9397 9398 /* Walk the BINFO and its bases. We walk in preorder so that as we 9399 initialize each vtable we can figure out at what offset the 9400 secondary vtable lies from the primary vtable. We can't use 9401 dfs_walk here because we need to iterate through bases of BINFO 9402 and RTTI_BINFO simultaneously. */ 9403 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i) 9404 { 9405 /* Skip virtual bases. */ 9406 if (BINFO_VIRTUAL_P (base_binfo)) 9407 continue; 9408 accumulate_vtbl_inits (base_binfo, 9409 BINFO_BASE_BINFO (orig_binfo, i), 9410 rtti_binfo, vtbl, t, 9411 inits); 9412 } 9413 } 9414 9415 /* Called from accumulate_vtbl_inits. Adds the initializers for the 9416 BINFO vtable to L. */ 9417 9418 static void 9419 dfs_accumulate_vtbl_inits (tree binfo, 9420 tree orig_binfo, 9421 tree rtti_binfo, 9422 tree orig_vtbl, 9423 tree t, 9424 vec<constructor_elt, va_gc> **l) 9425 { 9426 tree vtbl = NULL_TREE; 9427 int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t); 9428 int n_inits; 9429 9430 if (ctor_vtbl_p 9431 && BINFO_VIRTUAL_P (orig_binfo) && BINFO_PRIMARY_P (orig_binfo)) 9432 { 9433 /* In the hierarchy of BINFO_TYPE (RTTI_BINFO), this is a 9434 primary virtual base. If it is not the same primary in 9435 the hierarchy of T, we'll need to generate a ctor vtable 9436 for it, to place at its location in T. If it is the same 9437 primary, we still need a VTT entry for the vtable, but it 9438 should point to the ctor vtable for the base it is a 9439 primary for within the sub-hierarchy of RTTI_BINFO. 9440 9441 There are three possible cases: 9442 9443 1) We are in the same place. 9444 2) We are a primary base within a lost primary virtual base of 9445 RTTI_BINFO. 9446 3) We are primary to something not a base of RTTI_BINFO. */ 9447 9448 tree b; 9449 tree last = NULL_TREE; 9450 9451 /* First, look through the bases we are primary to for RTTI_BINFO 9452 or a virtual base. */ 9453 b = binfo; 9454 while (BINFO_PRIMARY_P (b)) 9455 { 9456 b = BINFO_INHERITANCE_CHAIN (b); 9457 last = b; 9458 if (BINFO_VIRTUAL_P (b) || b == rtti_binfo) 9459 goto found; 9460 } 9461 /* If we run out of primary links, keep looking down our 9462 inheritance chain; we might be an indirect primary. */ 9463 for (b = last; b; b = BINFO_INHERITANCE_CHAIN (b)) 9464 if (BINFO_VIRTUAL_P (b) || b == rtti_binfo) 9465 break; 9466 found: 9467 9468 /* If we found RTTI_BINFO, this is case 1. If we found a virtual 9469 base B and it is a base of RTTI_BINFO, this is case 2. In 9470 either case, we share our vtable with LAST, i.e. the 9471 derived-most base within B of which we are a primary. */ 9472 if (b == rtti_binfo 9473 || (b && binfo_for_vbase (BINFO_TYPE (b), BINFO_TYPE (rtti_binfo)))) 9474 /* Just set our BINFO_VTABLE to point to LAST, as we may not have 9475 set LAST's BINFO_VTABLE yet. We'll extract the actual vptr in 9476 binfo_ctor_vtable after everything's been set up. */ 9477 vtbl = last; 9478 9479 /* Otherwise, this is case 3 and we get our own. */ 9480 } 9481 else if (!BINFO_NEW_VTABLE_MARKED (orig_binfo)) 9482 return; 9483 9484 n_inits = vec_safe_length (*l); 9485 9486 if (!vtbl) 9487 { 9488 tree index; 9489 int non_fn_entries; 9490 9491 /* Add the initializer for this vtable. */ 9492 build_vtbl_initializer (binfo, orig_binfo, t, rtti_binfo, 9493 &non_fn_entries, l); 9494 9495 /* Figure out the position to which the VPTR should point. */ 9496 vtbl = build1 (ADDR_EXPR, vtbl_ptr_type_node, orig_vtbl); 9497 index = size_binop (MULT_EXPR, 9498 TYPE_SIZE_UNIT (vtable_entry_type), 9499 size_int (non_fn_entries + n_inits)); 9500 vtbl = fold_build_pointer_plus (vtbl, index); 9501 } 9502 9503 if (ctor_vtbl_p) 9504 /* For a construction vtable, we can't overwrite BINFO_VTABLE. 9505 So, we make a TREE_LIST. Later, dfs_fixup_binfo_vtbls will 9506 straighten this out. */ 9507 BINFO_VTABLE (binfo) = tree_cons (rtti_binfo, vtbl, BINFO_VTABLE (binfo)); 9508 else if (BINFO_PRIMARY_P (binfo) && BINFO_VIRTUAL_P (binfo)) 9509 /* Throw away any unneeded intializers. */ 9510 (*l)->truncate (n_inits); 9511 else 9512 /* For an ordinary vtable, set BINFO_VTABLE. */ 9513 BINFO_VTABLE (binfo) = vtbl; 9514 } 9515 9516 static GTY(()) tree abort_fndecl_addr; 9517 9518 /* Construct the initializer for BINFO's virtual function table. BINFO 9519 is part of the hierarchy dominated by T. If we're building a 9520 construction vtable, the ORIG_BINFO is the binfo we should use to 9521 find the actual function pointers to put in the vtable - but they 9522 can be overridden on the path to most-derived in the graph that 9523 ORIG_BINFO belongs. Otherwise, 9524 ORIG_BINFO should be the same as BINFO. The RTTI_BINFO is the 9525 BINFO that should be indicated by the RTTI information in the 9526 vtable; it will be a base class of T, rather than T itself, if we 9527 are building a construction vtable. 9528 9529 The value returned is a TREE_LIST suitable for wrapping in a 9530 CONSTRUCTOR to use as the DECL_INITIAL for a vtable. If 9531 NON_FN_ENTRIES_P is not NULL, *NON_FN_ENTRIES_P is set to the 9532 number of non-function entries in the vtable. 9533 9534 It might seem that this function should never be called with a 9535 BINFO for which BINFO_PRIMARY_P holds, the vtable for such a 9536 base is always subsumed by a derived class vtable. However, when 9537 we are building construction vtables, we do build vtables for 9538 primary bases; we need these while the primary base is being 9539 constructed. */ 9540 9541 static void 9542 build_vtbl_initializer (tree binfo, 9543 tree orig_binfo, 9544 tree t, 9545 tree rtti_binfo, 9546 int* non_fn_entries_p, 9547 vec<constructor_elt, va_gc> **inits) 9548 { 9549 tree v; 9550 vtbl_init_data vid; 9551 unsigned ix, jx; 9552 tree vbinfo; 9553 vec<tree, va_gc> *vbases; 9554 constructor_elt *e; 9555 9556 /* Initialize VID. */ 9557 memset (&vid, 0, sizeof (vid)); 9558 vid.binfo = binfo; 9559 vid.derived = t; 9560 vid.rtti_binfo = rtti_binfo; 9561 vid.primary_vtbl_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t); 9562 vid.ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t); 9563 vid.generate_vcall_entries = true; 9564 /* The first vbase or vcall offset is at index -3 in the vtable. */ 9565 vid.index = ssize_int(-3 * TARGET_VTABLE_DATA_ENTRY_DISTANCE); 9566 9567 /* Add entries to the vtable for RTTI. */ 9568 build_rtti_vtbl_entries (binfo, &vid); 9569 9570 /* Create an array for keeping track of the functions we've 9571 processed. When we see multiple functions with the same 9572 signature, we share the vcall offsets. */ 9573 vec_alloc (vid.fns, 32); 9574 /* Add the vcall and vbase offset entries. */ 9575 build_vcall_and_vbase_vtbl_entries (binfo, &vid); 9576 9577 /* Clear BINFO_VTABLE_PATH_MARKED; it's set by 9578 build_vbase_offset_vtbl_entries. */ 9579 for (vbases = CLASSTYPE_VBASECLASSES (t), ix = 0; 9580 vec_safe_iterate (vbases, ix, &vbinfo); ix++) 9581 BINFO_VTABLE_PATH_MARKED (vbinfo) = 0; 9582 9583 /* If the target requires padding between data entries, add that now. */ 9584 if (TARGET_VTABLE_DATA_ENTRY_DISTANCE > 1) 9585 { 9586 int n_entries = vec_safe_length (vid.inits); 9587 9588 vec_safe_grow (vid.inits, TARGET_VTABLE_DATA_ENTRY_DISTANCE * n_entries); 9589 9590 /* Move data entries into their new positions and add padding 9591 after the new positions. Iterate backwards so we don't 9592 overwrite entries that we would need to process later. */ 9593 for (ix = n_entries - 1; 9594 vid.inits->iterate (ix, &e); 9595 ix--) 9596 { 9597 int j; 9598 int new_position = (TARGET_VTABLE_DATA_ENTRY_DISTANCE * ix 9599 + (TARGET_VTABLE_DATA_ENTRY_DISTANCE - 1)); 9600 9601 (*vid.inits)[new_position] = *e; 9602 9603 for (j = 1; j < TARGET_VTABLE_DATA_ENTRY_DISTANCE; ++j) 9604 { 9605 constructor_elt *f = &(*vid.inits)[new_position - j]; 9606 f->index = NULL_TREE; 9607 f->value = build1 (NOP_EXPR, vtable_entry_type, 9608 null_pointer_node); 9609 } 9610 } 9611 } 9612 9613 if (non_fn_entries_p) 9614 *non_fn_entries_p = vec_safe_length (vid.inits); 9615 9616 /* The initializers for virtual functions were built up in reverse 9617 order. Straighten them out and add them to the running list in one 9618 step. */ 9619 jx = vec_safe_length (*inits); 9620 vec_safe_grow (*inits, jx + vid.inits->length ()); 9621 9622 for (ix = vid.inits->length () - 1; 9623 vid.inits->iterate (ix, &e); 9624 ix--, jx++) 9625 (**inits)[jx] = *e; 9626 9627 /* Go through all the ordinary virtual functions, building up 9628 initializers. */ 9629 for (v = BINFO_VIRTUALS (orig_binfo); v; v = TREE_CHAIN (v)) 9630 { 9631 tree delta; 9632 tree vcall_index; 9633 tree fn, fn_original; 9634 tree init = NULL_TREE; 9635 9636 fn = BV_FN (v); 9637 fn_original = fn; 9638 if (DECL_THUNK_P (fn)) 9639 { 9640 if (!DECL_NAME (fn)) 9641 finish_thunk (fn); 9642 if (THUNK_ALIAS (fn)) 9643 { 9644 fn = THUNK_ALIAS (fn); 9645 BV_FN (v) = fn; 9646 } 9647 fn_original = THUNK_TARGET (fn); 9648 } 9649 9650 /* If the only definition of this function signature along our 9651 primary base chain is from a lost primary, this vtable slot will 9652 never be used, so just zero it out. This is important to avoid 9653 requiring extra thunks which cannot be generated with the function. 9654 9655 We first check this in update_vtable_entry_for_fn, so we handle 9656 restored primary bases properly; we also need to do it here so we 9657 zero out unused slots in ctor vtables, rather than filling them 9658 with erroneous values (though harmless, apart from relocation 9659 costs). */ 9660 if (BV_LOST_PRIMARY (v)) 9661 init = size_zero_node; 9662 9663 if (! init) 9664 { 9665 /* Pull the offset for `this', and the function to call, out of 9666 the list. */ 9667 delta = BV_DELTA (v); 9668 vcall_index = BV_VCALL_INDEX (v); 9669 9670 gcc_assert (TREE_CODE (delta) == INTEGER_CST); 9671 gcc_assert (TREE_CODE (fn) == FUNCTION_DECL); 9672 9673 /* You can't call an abstract virtual function; it's abstract. 9674 So, we replace these functions with __pure_virtual. */ 9675 if (DECL_PURE_VIRTUAL_P (fn_original)) 9676 { 9677 fn = abort_fndecl; 9678 if (!TARGET_VTABLE_USES_DESCRIPTORS) 9679 { 9680 if (abort_fndecl_addr == NULL) 9681 abort_fndecl_addr 9682 = fold_convert (vfunc_ptr_type_node, 9683 build_fold_addr_expr (fn)); 9684 init = abort_fndecl_addr; 9685 } 9686 } 9687 /* Likewise for deleted virtuals. */ 9688 else if (DECL_DELETED_FN (fn_original)) 9689 { 9690 fn = get_identifier ("__cxa_deleted_virtual"); 9691 if (!get_global_value_if_present (fn, &fn)) 9692 fn = push_library_fn (fn, (build_function_type_list 9693 (void_type_node, NULL_TREE)), 9694 NULL_TREE, ECF_NORETURN); 9695 if (!TARGET_VTABLE_USES_DESCRIPTORS) 9696 init = fold_convert (vfunc_ptr_type_node, 9697 build_fold_addr_expr (fn)); 9698 } 9699 else 9700 { 9701 if (!integer_zerop (delta) || vcall_index) 9702 { 9703 fn = make_thunk (fn, /*this_adjusting=*/1, delta, vcall_index); 9704 if (!DECL_NAME (fn)) 9705 finish_thunk (fn); 9706 } 9707 /* Take the address of the function, considering it to be of an 9708 appropriate generic type. */ 9709 if (!TARGET_VTABLE_USES_DESCRIPTORS) 9710 init = fold_convert (vfunc_ptr_type_node, 9711 build_fold_addr_expr (fn)); 9712 /* Don't refer to a virtual destructor from a constructor 9713 vtable or a vtable for an abstract class, since destroying 9714 an object under construction is undefined behavior and we 9715 don't want it to be considered a candidate for speculative 9716 devirtualization. But do create the thunk for ABI 9717 compliance. */ 9718 if (DECL_DESTRUCTOR_P (fn_original) 9719 && (CLASSTYPE_PURE_VIRTUALS (DECL_CONTEXT (fn_original)) 9720 || orig_binfo != binfo)) 9721 init = size_zero_node; 9722 } 9723 } 9724 9725 /* And add it to the chain of initializers. */ 9726 if (TARGET_VTABLE_USES_DESCRIPTORS) 9727 { 9728 int i; 9729 if (init == size_zero_node) 9730 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i) 9731 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init); 9732 else 9733 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i) 9734 { 9735 tree fdesc = build2 (FDESC_EXPR, vfunc_ptr_type_node, 9736 fn, build_int_cst (NULL_TREE, i)); 9737 TREE_CONSTANT (fdesc) = 1; 9738 9739 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, fdesc); 9740 } 9741 } 9742 else 9743 CONSTRUCTOR_APPEND_ELT (*inits, NULL_TREE, init); 9744 } 9745 } 9746 9747 /* Adds to vid->inits the initializers for the vbase and vcall 9748 offsets in BINFO, which is in the hierarchy dominated by T. */ 9749 9750 static void 9751 build_vcall_and_vbase_vtbl_entries (tree binfo, vtbl_init_data* vid) 9752 { 9753 tree b; 9754 9755 /* If this is a derived class, we must first create entries 9756 corresponding to the primary base class. */ 9757 b = get_primary_binfo (binfo); 9758 if (b) 9759 build_vcall_and_vbase_vtbl_entries (b, vid); 9760 9761 /* Add the vbase entries for this base. */ 9762 build_vbase_offset_vtbl_entries (binfo, vid); 9763 /* Add the vcall entries for this base. */ 9764 build_vcall_offset_vtbl_entries (binfo, vid); 9765 } 9766 9767 /* Returns the initializers for the vbase offset entries in the vtable 9768 for BINFO (which is part of the class hierarchy dominated by T), in 9769 reverse order. VBASE_OFFSET_INDEX gives the vtable index 9770 where the next vbase offset will go. */ 9771 9772 static void 9773 build_vbase_offset_vtbl_entries (tree binfo, vtbl_init_data* vid) 9774 { 9775 tree vbase; 9776 tree t; 9777 tree non_primary_binfo; 9778 9779 /* If there are no virtual baseclasses, then there is nothing to 9780 do. */ 9781 if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))) 9782 return; 9783 9784 t = vid->derived; 9785 9786 /* We might be a primary base class. Go up the inheritance hierarchy 9787 until we find the most derived class of which we are a primary base: 9788 it is the offset of that which we need to use. */ 9789 non_primary_binfo = binfo; 9790 while (BINFO_INHERITANCE_CHAIN (non_primary_binfo)) 9791 { 9792 tree b; 9793 9794 /* If we have reached a virtual base, then it must be a primary 9795 base (possibly multi-level) of vid->binfo, or we wouldn't 9796 have called build_vcall_and_vbase_vtbl_entries for it. But it 9797 might be a lost primary, so just skip down to vid->binfo. */ 9798 if (BINFO_VIRTUAL_P (non_primary_binfo)) 9799 { 9800 non_primary_binfo = vid->binfo; 9801 break; 9802 } 9803 9804 b = BINFO_INHERITANCE_CHAIN (non_primary_binfo); 9805 if (get_primary_binfo (b) != non_primary_binfo) 9806 break; 9807 non_primary_binfo = b; 9808 } 9809 9810 /* Go through the virtual bases, adding the offsets. */ 9811 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo)); 9812 vbase; 9813 vbase = TREE_CHAIN (vbase)) 9814 { 9815 tree b; 9816 tree delta; 9817 9818 if (!BINFO_VIRTUAL_P (vbase)) 9819 continue; 9820 9821 /* Find the instance of this virtual base in the complete 9822 object. */ 9823 b = copied_binfo (vbase, binfo); 9824 9825 /* If we've already got an offset for this virtual base, we 9826 don't need another one. */ 9827 if (BINFO_VTABLE_PATH_MARKED (b)) 9828 continue; 9829 BINFO_VTABLE_PATH_MARKED (b) = 1; 9830 9831 /* Figure out where we can find this vbase offset. */ 9832 delta = size_binop (MULT_EXPR, 9833 vid->index, 9834 fold_convert (ssizetype, 9835 TYPE_SIZE_UNIT (vtable_entry_type))); 9836 if (vid->primary_vtbl_p) 9837 BINFO_VPTR_FIELD (b) = delta; 9838 9839 if (binfo != TYPE_BINFO (t)) 9840 /* The vbase offset had better be the same. */ 9841 gcc_assert (tree_int_cst_equal (delta, BINFO_VPTR_FIELD (vbase))); 9842 9843 /* The next vbase will come at a more negative offset. */ 9844 vid->index = size_binop (MINUS_EXPR, vid->index, 9845 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE)); 9846 9847 /* The initializer is the delta from BINFO to this virtual base. 9848 The vbase offsets go in reverse inheritance-graph order, and 9849 we are walking in inheritance graph order so these end up in 9850 the right order. */ 9851 delta = size_diffop_loc (input_location, 9852 BINFO_OFFSET (b), BINFO_OFFSET (non_primary_binfo)); 9853 9854 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, 9855 fold_build1_loc (input_location, NOP_EXPR, 9856 vtable_entry_type, delta)); 9857 } 9858 } 9859 9860 /* Adds the initializers for the vcall offset entries in the vtable 9861 for BINFO (which is part of the class hierarchy dominated by VID->DERIVED) 9862 to VID->INITS. */ 9863 9864 static void 9865 build_vcall_offset_vtbl_entries (tree binfo, vtbl_init_data* vid) 9866 { 9867 /* We only need these entries if this base is a virtual base. We 9868 compute the indices -- but do not add to the vtable -- when 9869 building the main vtable for a class. */ 9870 if (binfo == TYPE_BINFO (vid->derived) 9871 || (BINFO_VIRTUAL_P (binfo) 9872 /* If BINFO is RTTI_BINFO, then (since BINFO does not 9873 correspond to VID->DERIVED), we are building a primary 9874 construction virtual table. Since this is a primary 9875 virtual table, we do not need the vcall offsets for 9876 BINFO. */ 9877 && binfo != vid->rtti_binfo)) 9878 { 9879 /* We need a vcall offset for each of the virtual functions in this 9880 vtable. For example: 9881 9882 class A { virtual void f (); }; 9883 class B1 : virtual public A { virtual void f (); }; 9884 class B2 : virtual public A { virtual void f (); }; 9885 class C: public B1, public B2 { virtual void f (); }; 9886 9887 A C object has a primary base of B1, which has a primary base of A. A 9888 C also has a secondary base of B2, which no longer has a primary base 9889 of A. So the B2-in-C construction vtable needs a secondary vtable for 9890 A, which will adjust the A* to a B2* to call f. We have no way of 9891 knowing what (or even whether) this offset will be when we define B2, 9892 so we store this "vcall offset" in the A sub-vtable and look it up in 9893 a "virtual thunk" for B2::f. 9894 9895 We need entries for all the functions in our primary vtable and 9896 in our non-virtual bases' secondary vtables. */ 9897 vid->vbase = binfo; 9898 /* If we are just computing the vcall indices -- but do not need 9899 the actual entries -- not that. */ 9900 if (!BINFO_VIRTUAL_P (binfo)) 9901 vid->generate_vcall_entries = false; 9902 /* Now, walk through the non-virtual bases, adding vcall offsets. */ 9903 add_vcall_offset_vtbl_entries_r (binfo, vid); 9904 } 9905 } 9906 9907 /* Build vcall offsets, starting with those for BINFO. */ 9908 9909 static void 9910 add_vcall_offset_vtbl_entries_r (tree binfo, vtbl_init_data* vid) 9911 { 9912 int i; 9913 tree primary_binfo; 9914 tree base_binfo; 9915 9916 /* Don't walk into virtual bases -- except, of course, for the 9917 virtual base for which we are building vcall offsets. Any 9918 primary virtual base will have already had its offsets generated 9919 through the recursion in build_vcall_and_vbase_vtbl_entries. */ 9920 if (BINFO_VIRTUAL_P (binfo) && vid->vbase != binfo) 9921 return; 9922 9923 /* If BINFO has a primary base, process it first. */ 9924 primary_binfo = get_primary_binfo (binfo); 9925 if (primary_binfo) 9926 add_vcall_offset_vtbl_entries_r (primary_binfo, vid); 9927 9928 /* Add BINFO itself to the list. */ 9929 add_vcall_offset_vtbl_entries_1 (binfo, vid); 9930 9931 /* Scan the non-primary bases of BINFO. */ 9932 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i) 9933 if (base_binfo != primary_binfo) 9934 add_vcall_offset_vtbl_entries_r (base_binfo, vid); 9935 } 9936 9937 /* Called from build_vcall_offset_vtbl_entries_r. */ 9938 9939 static void 9940 add_vcall_offset_vtbl_entries_1 (tree binfo, vtbl_init_data* vid) 9941 { 9942 /* Make entries for the rest of the virtuals. */ 9943 tree orig_fn; 9944 9945 /* The ABI requires that the methods be processed in declaration 9946 order. */ 9947 for (orig_fn = TYPE_METHODS (BINFO_TYPE (binfo)); 9948 orig_fn; 9949 orig_fn = DECL_CHAIN (orig_fn)) 9950 if (TREE_CODE (orig_fn) == FUNCTION_DECL && DECL_VINDEX (orig_fn)) 9951 add_vcall_offset (orig_fn, binfo, vid); 9952 } 9953 9954 /* Add a vcall offset entry for ORIG_FN to the vtable. */ 9955 9956 static void 9957 add_vcall_offset (tree orig_fn, tree binfo, vtbl_init_data *vid) 9958 { 9959 size_t i; 9960 tree vcall_offset; 9961 tree derived_entry; 9962 9963 /* If there is already an entry for a function with the same 9964 signature as FN, then we do not need a second vcall offset. 9965 Check the list of functions already present in the derived 9966 class vtable. */ 9967 FOR_EACH_VEC_SAFE_ELT (vid->fns, i, derived_entry) 9968 { 9969 if (same_signature_p (derived_entry, orig_fn) 9970 /* We only use one vcall offset for virtual destructors, 9971 even though there are two virtual table entries. */ 9972 || (DECL_DESTRUCTOR_P (derived_entry) 9973 && DECL_DESTRUCTOR_P (orig_fn))) 9974 return; 9975 } 9976 9977 /* If we are building these vcall offsets as part of building 9978 the vtable for the most derived class, remember the vcall 9979 offset. */ 9980 if (vid->binfo == TYPE_BINFO (vid->derived)) 9981 { 9982 tree_pair_s elt = {orig_fn, vid->index}; 9983 vec_safe_push (CLASSTYPE_VCALL_INDICES (vid->derived), elt); 9984 } 9985 9986 /* The next vcall offset will be found at a more negative 9987 offset. */ 9988 vid->index = size_binop (MINUS_EXPR, vid->index, 9989 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE)); 9990 9991 /* Keep track of this function. */ 9992 vec_safe_push (vid->fns, orig_fn); 9993 9994 if (vid->generate_vcall_entries) 9995 { 9996 tree base; 9997 tree fn; 9998 9999 /* Find the overriding function. */ 10000 fn = find_final_overrider (vid->rtti_binfo, binfo, orig_fn); 10001 if (fn == error_mark_node) 10002 vcall_offset = build_zero_cst (vtable_entry_type); 10003 else 10004 { 10005 base = TREE_VALUE (fn); 10006 10007 /* The vbase we're working on is a primary base of 10008 vid->binfo. But it might be a lost primary, so its 10009 BINFO_OFFSET might be wrong, so we just use the 10010 BINFO_OFFSET from vid->binfo. */ 10011 vcall_offset = size_diffop_loc (input_location, 10012 BINFO_OFFSET (base), 10013 BINFO_OFFSET (vid->binfo)); 10014 vcall_offset = fold_build1_loc (input_location, 10015 NOP_EXPR, vtable_entry_type, 10016 vcall_offset); 10017 } 10018 /* Add the initializer to the vtable. */ 10019 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, vcall_offset); 10020 } 10021 } 10022 10023 /* Return vtbl initializers for the RTTI entries corresponding to the 10024 BINFO's vtable. The RTTI entries should indicate the object given 10025 by VID->rtti_binfo. */ 10026 10027 static void 10028 build_rtti_vtbl_entries (tree binfo, vtbl_init_data* vid) 10029 { 10030 tree b; 10031 tree t; 10032 tree offset; 10033 tree decl; 10034 tree init; 10035 10036 t = BINFO_TYPE (vid->rtti_binfo); 10037 10038 /* To find the complete object, we will first convert to our most 10039 primary base, and then add the offset in the vtbl to that value. */ 10040 b = most_primary_binfo (binfo); 10041 offset = size_diffop_loc (input_location, 10042 BINFO_OFFSET (vid->rtti_binfo), BINFO_OFFSET (b)); 10043 10044 /* The second entry is the address of the typeinfo object. */ 10045 if (flag_rtti) 10046 decl = build_address (get_tinfo_decl (t)); 10047 else 10048 decl = integer_zero_node; 10049 10050 /* Convert the declaration to a type that can be stored in the 10051 vtable. */ 10052 init = build_nop (vfunc_ptr_type_node, decl); 10053 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, init); 10054 10055 /* Add the offset-to-top entry. It comes earlier in the vtable than 10056 the typeinfo entry. Convert the offset to look like a 10057 function pointer, so that we can put it in the vtable. */ 10058 init = build_nop (vfunc_ptr_type_node, offset); 10059 CONSTRUCTOR_APPEND_ELT (vid->inits, NULL_TREE, init); 10060 } 10061 10062 /* TRUE iff TYPE is uniquely derived from PARENT. Ignores 10063 accessibility. */ 10064 10065 bool 10066 uniquely_derived_from_p (tree parent, tree type) 10067 { 10068 tree base = lookup_base (type, parent, ba_unique, NULL, tf_none); 10069 return base && base != error_mark_node; 10070 } 10071 10072 /* TRUE iff TYPE is publicly & uniquely derived from PARENT. */ 10073 10074 bool 10075 publicly_uniquely_derived_p (tree parent, tree type) 10076 { 10077 tree base = lookup_base (type, parent, ba_ignore_scope | ba_check, 10078 NULL, tf_none); 10079 return base && base != error_mark_node; 10080 } 10081 10082 /* CTX1 and CTX2 are declaration contexts. Return the innermost common 10083 class between them, if any. */ 10084 10085 tree 10086 common_enclosing_class (tree ctx1, tree ctx2) 10087 { 10088 if (!TYPE_P (ctx1) || !TYPE_P (ctx2)) 10089 return NULL_TREE; 10090 gcc_assert (ctx1 == TYPE_MAIN_VARIANT (ctx1) 10091 && ctx2 == TYPE_MAIN_VARIANT (ctx2)); 10092 if (ctx1 == ctx2) 10093 return ctx1; 10094 for (tree t = ctx1; TYPE_P (t); t = TYPE_CONTEXT (t)) 10095 TYPE_MARKED_P (t) = true; 10096 tree found = NULL_TREE; 10097 for (tree t = ctx2; TYPE_P (t); t = TYPE_CONTEXT (t)) 10098 if (TYPE_MARKED_P (t)) 10099 { 10100 found = t; 10101 break; 10102 } 10103 for (tree t = ctx1; TYPE_P (t); t = TYPE_CONTEXT (t)) 10104 TYPE_MARKED_P (t) = false; 10105 return found; 10106 } 10107 10108 #include "gt-cp-class.h" 10109