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