1 /* Process declarations and variables for C++ compiler. 2 Copyright (C) 1988-2024 Free Software Foundation, Inc. 3 Hacked by Michael Tiemann (tiemann (at) 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 /* Process declarations and symbol lookup for C++ front end. 23 Also constructs types; the standard scalar types at initialization, 24 and structure, union, array and enum types when they are declared. */ 25 26 /* ??? not all decl nodes are given the most useful possible 27 line numbers. For example, the CONST_DECLs for enum values. */ 28 29 #include "config.h" 30 #include "system.h" 31 #include "coretypes.h" 32 #include "memmodel.h" 33 #include "target.h" 34 #include "cp-tree.h" 35 #include "c-family/c-common.h" 36 #include "timevar.h" 37 #include "stringpool.h" 38 #include "cgraph.h" 39 #include "varasm.h" 40 #include "attribs.h" 41 #include "stor-layout.h" 42 #include "calls.h" 43 #include "decl.h" 44 #include "toplev.h" 45 #include "c-family/c-objc.h" 46 #include "c-family/c-pragma.h" 47 #include "dumpfile.h" 48 #include "intl.h" 49 #include "c-family/c-ada-spec.h" 50 #include "asan.h" 51 #include "optabs-query.h" 52 #include "omp-general.h" 53 54 /* Id for dumping the raw trees. */ 55 int raw_dump_id; 56 57 extern cpp_reader *parse_in; 58 59 static tree start_objects (bool, unsigned, bool); 60 static tree finish_objects (bool, unsigned, tree, bool = true); 61 static tree start_partial_init_fini_fn (bool, unsigned, unsigned); 62 static void finish_partial_init_fini_fn (tree); 63 static void emit_partial_init_fini_fn (bool, unsigned, tree, 64 unsigned, location_t); 65 static void one_static_initialization_or_destruction (bool, tree, tree); 66 static void generate_ctor_or_dtor_function (bool, unsigned, tree, location_t); 67 static tree prune_vars_needing_no_initialization (tree *); 68 static void write_out_vars (tree); 69 static void import_export_class (tree); 70 static tree get_guard_bits (tree); 71 static void determine_visibility_from_class (tree, tree); 72 static bool determine_hidden_inline (tree); 73 74 /* A list of static class variables. This is needed, because a 75 static class variable can be declared inside the class without 76 an initializer, and then initialized, statically, outside the class. */ 77 static GTY(()) vec<tree, va_gc> *pending_statics; 78 79 /* A list of functions which were declared inline, but which we 80 may need to emit outline anyway. */ 81 static GTY(()) vec<tree, va_gc> *deferred_fns; 82 83 /* A list of decls that use types with no linkage, which we need to make 84 sure are defined. */ 85 static GTY(()) vec<tree, va_gc> *no_linkage_decls; 86 87 /* A vector of alternating decls and identifiers, where the latter 88 is to be an alias for the former if the former is defined. */ 89 static GTY(()) vec<tree, va_gc> *mangling_aliases; 90 91 /* hash traits for declarations. Hashes single decls via 92 DECL_ASSEMBLER_NAME_RAW. */ 93 94 struct mangled_decl_hash : ggc_remove <tree> 95 { 96 typedef tree value_type; /* A DECL. */ 97 typedef tree compare_type; /* An identifier. */ 98 99 static hashval_t hash (const value_type decl) 100 { 101 return IDENTIFIER_HASH_VALUE (DECL_ASSEMBLER_NAME_RAW (decl)); 102 } 103 static bool equal (const value_type existing, compare_type candidate) 104 { 105 tree name = DECL_ASSEMBLER_NAME_RAW (existing); 106 return candidate == name; 107 } 108 109 static const bool empty_zero_p = true; 110 static inline void mark_empty (value_type &p) {p = NULL_TREE;} 111 static inline bool is_empty (value_type p) {return !p;} 112 113 static bool is_deleted (value_type e) 114 { 115 return e == reinterpret_cast <value_type> (1); 116 } 117 static void mark_deleted (value_type &e) 118 { 119 e = reinterpret_cast <value_type> (1); 120 } 121 }; 122 123 /* A hash table of decls keyed by mangled name. Used to figure out if 124 we need compatibility aliases. */ 125 static GTY(()) hash_table<mangled_decl_hash> *mangled_decls; 126 127 // Hash table mapping priority to lists of variables or functions. 128 struct priority_map_traits 129 { 130 typedef unsigned key_type; 131 typedef tree value_type; 132 static const bool maybe_mx = true; 133 static hashval_t hash (key_type v) 134 { 135 return hashval_t (v); 136 } 137 static bool equal_keys (key_type k1, key_type k2) 138 { 139 return k1 == k2; 140 } 141 template <typename T> static void remove (T &) 142 { 143 } 144 // Zero is not a priority 145 static const bool empty_zero_p = true; 146 template <typename T> static bool is_empty (const T &entry) 147 { 148 return entry.m_key == 0; 149 } 150 template <typename T> static void mark_empty (T &entry) 151 { 152 entry.m_key = 0; 153 } 154 // Entries are not deleteable 155 template <typename T> static bool is_deleted (const T &) 156 { 157 return false; 158 } 159 template <typename T> static void mark_deleted (T &) 160 { 161 gcc_unreachable (); 162 } 163 }; 164 165 typedef hash_map<unsigned/*Priority*/, tree/*List*/, 166 priority_map_traits> priority_map_t; 167 168 /* A pair of such hash tables, indexed by initp -- one for fini and 169 one for init. The fini table is only ever used when !cxa_atexit. */ 170 static GTY(()) priority_map_t *static_init_fini_fns[2]; 171 172 /* Nonzero if we're done parsing and into end-of-file activities. 173 2 if all templates have been instantiated. 174 3 if we're done with front-end processing. */ 175 176 int at_eof; 177 178 /* True if note_mangling_alias should enqueue mangling aliases for 179 later generation, rather than emitting them right away. */ 180 181 bool defer_mangling_aliases = true; 182 183 185 /* Return a member function type (a METHOD_TYPE), given FNTYPE (a 186 FUNCTION_TYPE), CTYPE (class type), and QUALS (the cv-qualifiers 187 that apply to the function). */ 188 189 tree 190 build_memfn_type (tree fntype, tree ctype, cp_cv_quals quals, 191 cp_ref_qualifier rqual) 192 { 193 if (fntype == error_mark_node || ctype == error_mark_node) 194 return error_mark_node; 195 196 gcc_assert (FUNC_OR_METHOD_TYPE_P (fntype)); 197 198 cp_cv_quals type_quals = quals & ~TYPE_QUAL_RESTRICT; 199 ctype = cp_build_qualified_type (ctype, type_quals); 200 201 tree newtype 202 = build_method_type_directly (ctype, TREE_TYPE (fntype), 203 (TREE_CODE (fntype) == METHOD_TYPE 204 ? TREE_CHAIN (TYPE_ARG_TYPES (fntype)) 205 : TYPE_ARG_TYPES (fntype))); 206 if (tree attrs = TYPE_ATTRIBUTES (fntype)) 207 newtype = cp_build_type_attribute_variant (newtype, attrs); 208 newtype = build_cp_fntype_variant (newtype, rqual, 209 TYPE_RAISES_EXCEPTIONS (fntype), 210 TYPE_HAS_LATE_RETURN_TYPE (fntype)); 211 212 return newtype; 213 } 214 215 /* Return a variant of FNTYPE, a FUNCTION_TYPE or METHOD_TYPE, with its 216 return type changed to NEW_RET. */ 217 218 tree 219 change_return_type (tree new_ret, tree fntype) 220 { 221 if (new_ret == error_mark_node) 222 return fntype; 223 224 if (same_type_p (new_ret, TREE_TYPE (fntype))) 225 return fntype; 226 227 tree newtype; 228 tree args = TYPE_ARG_TYPES (fntype); 229 230 if (TREE_CODE (fntype) == FUNCTION_TYPE) 231 { 232 newtype = build_function_type (new_ret, args); 233 newtype = apply_memfn_quals (newtype, 234 type_memfn_quals (fntype)); 235 } 236 else 237 newtype = build_method_type_directly 238 (class_of_this_parm (fntype), new_ret, TREE_CHAIN (args)); 239 240 if (tree attrs = TYPE_ATTRIBUTES (fntype)) 241 newtype = cp_build_type_attribute_variant (newtype, attrs); 242 newtype = cxx_copy_lang_qualifiers (newtype, fntype); 243 244 return newtype; 245 } 246 247 /* Build a PARM_DECL of FN with NAME and TYPE, and set DECL_ARG_TYPE 248 appropriately. */ 249 250 tree 251 cp_build_parm_decl (tree fn, tree name, tree type) 252 { 253 tree parm = build_decl (input_location, 254 PARM_DECL, name, type); 255 DECL_CONTEXT (parm) = fn; 256 257 /* DECL_ARG_TYPE is only used by the back end and the back end never 258 sees templates. */ 259 if (!processing_template_decl) 260 DECL_ARG_TYPE (parm) = type_passed_as (type); 261 262 return parm; 263 } 264 265 /* Returns a PARM_DECL of FN for a parameter of the indicated TYPE, with the 266 indicated NAME. */ 267 268 tree 269 build_artificial_parm (tree fn, tree name, tree type) 270 { 271 tree parm = cp_build_parm_decl (fn, name, type); 272 DECL_ARTIFICIAL (parm) = 1; 273 /* All our artificial parms are implicitly `const'; they cannot be 274 assigned to. */ 275 TREE_READONLY (parm) = 1; 276 return parm; 277 } 278 279 /* Constructors for types with virtual baseclasses need an "in-charge" flag 280 saying whether this constructor is responsible for initialization of 281 virtual baseclasses or not. All destructors also need this "in-charge" 282 flag, which additionally determines whether or not the destructor should 283 free the memory for the object. 284 285 This function adds the "in-charge" flag to member function FN if 286 appropriate. It is called from grokclassfn and tsubst. 287 FN must be either a constructor or destructor. 288 289 The in-charge flag follows the 'this' parameter, and is followed by the 290 VTT parm (if any), then the user-written parms. */ 291 292 void 293 maybe_retrofit_in_chrg (tree fn) 294 { 295 tree basetype, arg_types, parms, parm, fntype; 296 297 /* If we've already add the in-charge parameter don't do it again. */ 298 if (DECL_HAS_IN_CHARGE_PARM_P (fn)) 299 return; 300 301 /* When processing templates we can't know, in general, whether or 302 not we're going to have virtual baseclasses. */ 303 if (processing_template_decl) 304 return; 305 306 /* We don't need an in-charge parameter for constructors that don't 307 have virtual bases. */ 308 if (DECL_CONSTRUCTOR_P (fn) 309 && !CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn))) 310 return; 311 312 arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn)); 313 basetype = TREE_TYPE (TREE_VALUE (arg_types)); 314 arg_types = TREE_CHAIN (arg_types); 315 316 parms = DECL_CHAIN (DECL_ARGUMENTS (fn)); 317 318 /* If this is a subobject constructor or destructor, our caller will 319 pass us a pointer to our VTT. */ 320 if (CLASSTYPE_VBASECLASSES (DECL_CONTEXT (fn))) 321 { 322 parm = build_artificial_parm (fn, vtt_parm_identifier, vtt_parm_type); 323 324 /* First add it to DECL_ARGUMENTS between 'this' and the real args... */ 325 DECL_CHAIN (parm) = parms; 326 parms = parm; 327 328 /* ...and then to TYPE_ARG_TYPES. */ 329 arg_types = hash_tree_chain (vtt_parm_type, arg_types); 330 331 DECL_HAS_VTT_PARM_P (fn) = 1; 332 } 333 334 /* Then add the in-charge parm (before the VTT parm). */ 335 parm = build_artificial_parm (fn, in_charge_identifier, integer_type_node); 336 DECL_CHAIN (parm) = parms; 337 parms = parm; 338 arg_types = hash_tree_chain (integer_type_node, arg_types); 339 340 /* Insert our new parameter(s) into the list. */ 341 DECL_CHAIN (DECL_ARGUMENTS (fn)) = parms; 342 343 /* And rebuild the function type. */ 344 fntype = build_method_type_directly (basetype, TREE_TYPE (TREE_TYPE (fn)), 345 arg_types); 346 if (TYPE_ATTRIBUTES (TREE_TYPE (fn))) 347 fntype = (cp_build_type_attribute_variant 348 (fntype, TYPE_ATTRIBUTES (TREE_TYPE (fn)))); 349 fntype = cxx_copy_lang_qualifiers (fntype, TREE_TYPE (fn)); 350 TREE_TYPE (fn) = fntype; 351 352 /* Now we've got the in-charge parameter. */ 353 DECL_HAS_IN_CHARGE_PARM_P (fn) = 1; 354 } 355 356 /* Classes overload their constituent function names automatically. 357 When a function name is declared in a record structure, 358 its name is changed to it overloaded name. Since names for 359 constructors and destructors can conflict, we place a leading 360 '$' for destructors. 361 362 CNAME is the name of the class we are grokking for. 363 364 FUNCTION is a FUNCTION_DECL. It was created by `grokdeclarator'. 365 366 FLAGS contains bits saying what's special about today's 367 arguments. DTOR_FLAG == DESTRUCTOR. 368 369 If FUNCTION is a destructor, then we must add the `auto-delete' field 370 as a second parameter. There is some hair associated with the fact 371 that we must "declare" this variable in the manner consistent with the 372 way the rest of the arguments were declared. 373 374 QUALS are the qualifiers for the this pointer. */ 375 376 void 377 grokclassfn (tree ctype, tree function, enum overload_flags flags) 378 { 379 tree fn_name = DECL_NAME (function); 380 381 /* Even within an `extern "C"' block, members get C++ linkage. See 382 [dcl.link] for details. */ 383 SET_DECL_LANGUAGE (function, lang_cplusplus); 384 385 if (fn_name == NULL_TREE) 386 { 387 error ("name missing for member function"); 388 fn_name = get_identifier ("<anonymous>"); 389 DECL_NAME (function) = fn_name; 390 } 391 392 DECL_CONTEXT (function) = ctype; 393 394 if (flags == DTOR_FLAG) 395 DECL_CXX_DESTRUCTOR_P (function) = 1; 396 397 if (flags == DTOR_FLAG || DECL_CONSTRUCTOR_P (function)) 398 maybe_retrofit_in_chrg (function); 399 } 400 401 /* Create an ARRAY_REF, checking for the user doing things backwards 402 along the way. 403 If INDEX_EXP is non-NULL, then that is the index expression, 404 otherwise INDEX_EXP_LIST is the list of index expressions. */ 405 406 tree 407 grok_array_decl (location_t loc, tree array_expr, tree index_exp, 408 vec<tree, va_gc> **index_exp_list, tsubst_flags_t complain) 409 { 410 tree type; 411 tree expr; 412 tree orig_array_expr = array_expr; 413 tree orig_index_exp = index_exp; 414 vec<tree, va_gc> *orig_index_exp_list 415 = index_exp_list ? *index_exp_list : NULL; 416 tree overload = NULL_TREE; 417 418 if (error_operand_p (array_expr) || error_operand_p (index_exp)) 419 return error_mark_node; 420 421 if (processing_template_decl) 422 { 423 if (type_dependent_expression_p (array_expr) 424 || (index_exp ? type_dependent_expression_p (index_exp) 425 : any_type_dependent_arguments_p (*index_exp_list))) 426 { 427 if (index_exp == NULL) 428 index_exp = build_min_nt_call_vec (ovl_op_identifier (ARRAY_REF), 429 *index_exp_list); 430 return build_min_nt_loc (loc, ARRAY_REF, array_expr, index_exp, 431 NULL_TREE, NULL_TREE); 432 } 433 if (!index_exp) 434 orig_index_exp_list = make_tree_vector_copy (*index_exp_list); 435 } 436 437 type = TREE_TYPE (array_expr); 438 gcc_assert (type); 439 type = non_reference (type); 440 441 /* If they have an `operator[]', use that. */ 442 if (MAYBE_CLASS_TYPE_P (type) 443 || (index_exp && MAYBE_CLASS_TYPE_P (TREE_TYPE (index_exp))) 444 || (index_exp == NULL_TREE 445 && !(*index_exp_list)->is_empty () 446 && MAYBE_CLASS_TYPE_P (TREE_TYPE ((*index_exp_list)->last ())))) 447 { 448 if (index_exp) 449 expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, array_expr, 450 index_exp, NULL_TREE, NULL_TREE, 451 &overload, complain); 452 else if ((*index_exp_list)->is_empty ()) 453 expr = build_op_subscript (loc, array_expr, index_exp_list, &overload, 454 complain); 455 else 456 { 457 expr = build_op_subscript (loc, array_expr, index_exp_list, 458 &overload, complain & tf_decltype); 459 if (expr == error_mark_node 460 /* Don't do the backward compatibility fallback in a SFINAE 461 context. */ 462 && (complain & tf_error)) 463 { 464 tree idx = build_x_compound_expr_from_vec (*index_exp_list, NULL, 465 tf_none); 466 if (idx != error_mark_node) 467 expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, array_expr, 468 idx, NULL_TREE, NULL_TREE, &overload, 469 complain & tf_decltype); 470 if (expr == error_mark_node) 471 { 472 overload = NULL_TREE; 473 expr = build_op_subscript (loc, array_expr, index_exp_list, 474 &overload, complain); 475 } 476 else 477 { 478 /* If it would be valid albeit deprecated expression in 479 C++20, just pedwarn on it and treat it as if wrapped 480 in (). */ 481 pedwarn (loc, OPT_Wcomma_subscript, 482 "top-level comma expression in array subscript " 483 "changed meaning in C++23"); 484 if (processing_template_decl) 485 { 486 orig_index_exp 487 = build_x_compound_expr_from_vec (orig_index_exp_list, 488 NULL, complain); 489 if (orig_index_exp == error_mark_node) 490 expr = error_mark_node; 491 release_tree_vector (orig_index_exp_list); 492 } 493 } 494 } 495 } 496 } 497 else 498 { 499 tree p1, p2, i1, i2; 500 bool swapped = false; 501 502 /* Otherwise, create an ARRAY_REF for a pointer or array type. 503 It is a little-known fact that, if `a' is an array and `i' is 504 an int, you can write `i[a]', which means the same thing as 505 `a[i]'. */ 506 if (TREE_CODE (type) == ARRAY_TYPE || VECTOR_TYPE_P (type)) 507 p1 = array_expr; 508 else 509 p1 = build_expr_type_conversion (WANT_POINTER, array_expr, false); 510 511 if (index_exp == NULL_TREE) 512 { 513 if (!(complain & tf_error)) 514 /* Don't do the backward compatibility fallback in a SFINAE 515 context. */ 516 return error_mark_node; 517 518 if ((*index_exp_list)->is_empty ()) 519 { 520 error_at (loc, "built-in subscript operator without expression " 521 "list"); 522 return error_mark_node; 523 } 524 tree idx = build_x_compound_expr_from_vec (*index_exp_list, NULL, 525 tf_none); 526 if (idx != error_mark_node) 527 /* If it would be valid albeit deprecated expression in C++20, 528 just pedwarn on it and treat it as if wrapped in (). */ 529 pedwarn (loc, OPT_Wcomma_subscript, 530 "top-level comma expression in array subscript " 531 "changed meaning in C++23"); 532 else 533 { 534 error_at (loc, "built-in subscript operator with more than one " 535 "expression in expression list"); 536 return error_mark_node; 537 } 538 index_exp = idx; 539 if (processing_template_decl) 540 { 541 orig_index_exp 542 = build_x_compound_expr_from_vec (orig_index_exp_list, 543 NULL, complain); 544 release_tree_vector (orig_index_exp_list); 545 if (orig_index_exp == error_mark_node) 546 return error_mark_node; 547 } 548 } 549 550 if (TREE_CODE (TREE_TYPE (index_exp)) == ARRAY_TYPE) 551 p2 = index_exp; 552 else 553 p2 = build_expr_type_conversion (WANT_POINTER, index_exp, false); 554 555 i1 = build_expr_type_conversion (WANT_INT | WANT_ENUM, array_expr, 556 false); 557 i2 = build_expr_type_conversion (WANT_INT | WANT_ENUM, index_exp, 558 false); 559 560 if ((p1 && i2) && (i1 && p2)) 561 error ("ambiguous conversion for array subscript"); 562 563 if (p1 && i2) 564 array_expr = p1, index_exp = i2; 565 else if (i1 && p2) 566 swapped = true, array_expr = p2, index_exp = i1; 567 else 568 { 569 if (complain & tf_error) 570 error_at (loc, "invalid types %<%T[%T]%> for array subscript", 571 type, TREE_TYPE (index_exp)); 572 return error_mark_node; 573 } 574 575 if (array_expr == error_mark_node || index_exp == error_mark_node) 576 error ("ambiguous conversion for array subscript"); 577 578 if (TYPE_PTR_P (TREE_TYPE (array_expr))) 579 array_expr = mark_rvalue_use (array_expr); 580 else 581 array_expr = mark_lvalue_use_nonread (array_expr); 582 index_exp = mark_rvalue_use (index_exp); 583 if (swapped 584 && flag_strong_eval_order == 2 585 && (TREE_SIDE_EFFECTS (array_expr) || TREE_SIDE_EFFECTS (index_exp))) 586 expr = build_array_ref (input_location, index_exp, array_expr); 587 else 588 expr = build_array_ref (input_location, array_expr, index_exp); 589 } 590 if (processing_template_decl && expr != error_mark_node) 591 { 592 if (overload != NULL_TREE) 593 { 594 if (orig_index_exp == NULL_TREE) 595 { 596 expr = build_min_non_dep_op_overload (expr, overload, 597 orig_array_expr, 598 orig_index_exp_list); 599 release_tree_vector (orig_index_exp_list); 600 return expr; 601 } 602 return build_min_non_dep_op_overload (ARRAY_REF, expr, overload, 603 orig_array_expr, 604 orig_index_exp); 605 } 606 607 if (orig_index_exp == NULL_TREE) 608 { 609 orig_index_exp 610 = build_min_nt_call_vec (ovl_op_identifier (ARRAY_REF), 611 orig_index_exp_list); 612 release_tree_vector (orig_index_exp_list); 613 } 614 615 return build_min_non_dep (ARRAY_REF, expr, orig_array_expr, 616 orig_index_exp, NULL_TREE, NULL_TREE); 617 } 618 return expr; 619 } 620 621 /* Build an OMP_ARRAY_SECTION expression, handling usage in template 622 definitions, etc. */ 623 624 tree 625 grok_omp_array_section (location_t loc, tree array_expr, tree index, 626 tree length) 627 { 628 tree orig_array_expr = array_expr; 629 tree orig_index = index; 630 tree orig_length = length; 631 632 if (error_operand_p (array_expr) 633 || error_operand_p (index) 634 || error_operand_p (length)) 635 return error_mark_node; 636 637 if (processing_template_decl 638 && (type_dependent_expression_p (array_expr) 639 || type_dependent_expression_p (index) 640 || type_dependent_expression_p (length))) 641 return build_min_nt_loc (loc, OMP_ARRAY_SECTION, array_expr, index, length); 642 643 index = fold_non_dependent_expr (index); 644 length = fold_non_dependent_expr (length); 645 646 /* NOTE: We can pass through invalidly-typed index/length fields 647 here (e.g. if the user tries to use a floating-point index/length). 648 This is diagnosed later in semantics.cc:handle_omp_array_sections_1. */ 649 650 tree expr = build_omp_array_section (loc, array_expr, index, length); 651 652 if (processing_template_decl) 653 expr = build_min_non_dep (OMP_ARRAY_SECTION, expr, orig_array_expr, 654 orig_index, orig_length); 655 return expr; 656 } 657 658 /* Given the cast expression EXP, checking out its validity. Either return 659 an error_mark_node if there was an unavoidable error, return a cast to 660 void for trying to delete a pointer w/ the value 0, or return the 661 call to delete. If DOING_VEC is true, we handle things differently 662 for doing an array delete. 663 Implements ARM $5.3.4. This is called from the parser. */ 664 665 tree 666 delete_sanity (location_t loc, tree exp, tree size, bool doing_vec, 667 int use_global_delete, tsubst_flags_t complain) 668 { 669 tree t, type; 670 671 if (exp == error_mark_node) 672 return exp; 673 674 if (processing_template_decl) 675 { 676 t = build_min (DELETE_EXPR, void_type_node, exp, size); 677 DELETE_EXPR_USE_GLOBAL (t) = use_global_delete; 678 DELETE_EXPR_USE_VEC (t) = doing_vec; 679 TREE_SIDE_EFFECTS (t) = 1; 680 SET_EXPR_LOCATION (t, loc); 681 return t; 682 } 683 684 location_t exp_loc = cp_expr_loc_or_loc (exp, loc); 685 686 /* An array can't have been allocated by new, so complain. */ 687 if (TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE 688 && (complain & tf_warning)) 689 warning_at (exp_loc, 0, "deleting array %q#E", exp); 690 691 t = build_expr_type_conversion (WANT_POINTER, exp, true); 692 693 if (t == NULL_TREE || t == error_mark_node) 694 { 695 if (complain & tf_error) 696 error_at (exp_loc, 697 "type %q#T argument given to %<delete%>, expected pointer", 698 TREE_TYPE (exp)); 699 return error_mark_node; 700 } 701 702 type = TREE_TYPE (t); 703 704 /* As of Valley Forge, you can delete a pointer to const. */ 705 706 /* You can't delete functions. */ 707 if (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE) 708 { 709 if (complain & tf_error) 710 error_at (exp_loc, 711 "cannot delete a function. Only pointer-to-objects are " 712 "valid arguments to %<delete%>"); 713 return error_mark_node; 714 } 715 716 /* Deleting ptr to void is undefined behavior [expr.delete/3]. */ 717 if (VOID_TYPE_P (TREE_TYPE (type))) 718 { 719 if (complain & tf_warning) 720 warning_at (exp_loc, OPT_Wdelete_incomplete, 721 "deleting %qT is undefined", type); 722 doing_vec = 0; 723 } 724 725 /* Deleting a pointer with the value zero is valid and has no effect. */ 726 if (integer_zerop (t)) 727 return build1_loc (loc, NOP_EXPR, void_type_node, t); 728 729 if (doing_vec) 730 return build_vec_delete (loc, t, /*maxindex=*/NULL_TREE, 731 sfk_deleting_destructor, 732 use_global_delete, complain); 733 else 734 return build_delete (loc, type, t, sfk_deleting_destructor, 735 LOOKUP_NORMAL, use_global_delete, 736 complain); 737 } 738 739 /* Report an error if the indicated template declaration is not the 740 sort of thing that should be a member template. */ 741 742 void 743 check_member_template (tree tmpl) 744 { 745 tree decl; 746 747 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL); 748 decl = DECL_TEMPLATE_RESULT (tmpl); 749 750 if (TREE_CODE (decl) == FUNCTION_DECL 751 || DECL_ALIAS_TEMPLATE_P (tmpl) 752 || (TREE_CODE (decl) == TYPE_DECL 753 && MAYBE_CLASS_TYPE_P (TREE_TYPE (decl)))) 754 { 755 /* The parser rejects template declarations in local classes 756 (with the exception of generic lambdas). */ 757 gcc_assert (!current_function_decl || LAMBDA_FUNCTION_P (decl)); 758 /* The parser rejects any use of virtual in a function template. */ 759 gcc_assert (!(TREE_CODE (decl) == FUNCTION_DECL 760 && DECL_VIRTUAL_P (decl))); 761 762 /* The debug-information generating code doesn't know what to do 763 with member templates. */ 764 DECL_IGNORED_P (tmpl) = 1; 765 } 766 else if (variable_template_p (tmpl)) 767 /* OK */; 768 else 769 error ("template declaration of %q#D", decl); 770 } 771 772 /* Sanity check: report error if this function FUNCTION is not 773 really a member of the class (CTYPE) it is supposed to belong to. 774 TEMPLATE_PARMS is used to specify the template parameters of a member 775 template passed as FUNCTION_DECL. If the member template is passed as a 776 TEMPLATE_DECL, it can be NULL since the parameters can be extracted 777 from the declaration. If the function is not a function template, it 778 must be NULL. 779 It returns the original declaration for the function, NULL_TREE if 780 no declaration was found, error_mark_node if an error was emitted. */ 781 782 tree 783 check_classfn (tree ctype, tree function, tree template_parms) 784 { 785 if (DECL_USE_TEMPLATE (function) 786 && !(TREE_CODE (function) == TEMPLATE_DECL 787 && DECL_TEMPLATE_SPECIALIZATION (function)) 788 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (function))) 789 /* Since this is a specialization of a member template, 790 we're not going to find the declaration in the class. 791 For example, in: 792 793 struct S { template <typename T> void f(T); }; 794 template <> void S::f(int); 795 796 we're not going to find `S::f(int)', but there's no 797 reason we should, either. We let our callers know we didn't 798 find the method, but we don't complain. */ 799 return NULL_TREE; 800 801 /* Basic sanity check: for a template function, the template parameters 802 either were not passed, or they are the same of DECL_TEMPLATE_PARMS. */ 803 if (TREE_CODE (function) == TEMPLATE_DECL) 804 { 805 if (template_parms 806 && !comp_template_parms (template_parms, 807 DECL_TEMPLATE_PARMS (function))) 808 { 809 error ("template parameter lists provided don%'t match the " 810 "template parameters of %qD", function); 811 return error_mark_node; 812 } 813 template_parms = DECL_TEMPLATE_PARMS (function); 814 } 815 816 /* OK, is this a definition of a member template? */ 817 bool is_template = (template_parms != NULL_TREE); 818 819 /* [temp.mem] 820 821 A destructor shall not be a member template. */ 822 if (DECL_DESTRUCTOR_P (function) && is_template) 823 { 824 error ("destructor %qD declared as member template", function); 825 return error_mark_node; 826 } 827 828 /* We must enter the scope here, because conversion operators are 829 named by target type, and type equivalence relies on typenames 830 resolving within the scope of CTYPE. */ 831 tree pushed_scope = push_scope (ctype); 832 tree matched = NULL_TREE; 833 tree fns = get_class_binding (ctype, DECL_NAME (function)); 834 bool saw_template = false; 835 836 for (ovl_iterator iter (fns); !matched && iter; ++iter) 837 { 838 tree fndecl = *iter; 839 840 if (TREE_CODE (fndecl) == TEMPLATE_DECL) 841 saw_template = true; 842 843 /* A member template definition only matches a member template 844 declaration. */ 845 if (is_template != (TREE_CODE (fndecl) == TEMPLATE_DECL)) 846 continue; 847 848 if (!DECL_DECLARES_FUNCTION_P (fndecl)) 849 continue; 850 851 tree p1 = TYPE_ARG_TYPES (TREE_TYPE (function)); 852 tree p2 = TYPE_ARG_TYPES (TREE_TYPE (fndecl)); 853 854 /* We cannot simply call decls_match because this doesn't work 855 for static member functions that are pretending to be 856 methods, and because the name may have been changed by 857 asm("new_name"). */ 858 859 /* Get rid of the this parameter on functions that become 860 static. */ 861 if (DECL_STATIC_FUNCTION_P (fndecl) 862 && TREE_CODE (TREE_TYPE (function)) == METHOD_TYPE) 863 p1 = TREE_CHAIN (p1); 864 865 /* ref-qualifier or absence of same must match. */ 866 if (type_memfn_rqual (TREE_TYPE (function)) 867 != type_memfn_rqual (TREE_TYPE (fndecl))) 868 continue; 869 870 // Include constraints in the match. 871 tree c1 = get_constraints (function); 872 tree c2 = get_constraints (fndecl); 873 874 /* While finding a match, same types and params are not enough 875 if the function is versioned. Also check for different target 876 specific attributes. */ 877 if (same_type_p (TREE_TYPE (TREE_TYPE (function)), 878 TREE_TYPE (TREE_TYPE (fndecl))) 879 && compparms (p1, p2) 880 && !targetm.target_option.function_versions (function, fndecl) 881 && (!is_template 882 || comp_template_parms (template_parms, 883 DECL_TEMPLATE_PARMS (fndecl))) 884 && equivalent_constraints (c1, c2) 885 && (DECL_TEMPLATE_SPECIALIZATION (function) 886 == DECL_TEMPLATE_SPECIALIZATION (fndecl)) 887 && (!DECL_TEMPLATE_SPECIALIZATION (function) 888 || (DECL_TI_TEMPLATE (function) == DECL_TI_TEMPLATE (fndecl)))) 889 matched = fndecl; 890 } 891 892 if (!matched && !is_template && saw_template 893 && !processing_template_decl && DECL_UNIQUE_FRIEND_P (function)) 894 { 895 /* "[if no non-template match is found,] each remaining function template 896 is replaced with the specialization chosen by deduction from the 897 friend declaration or discarded if deduction fails." 898 899 So tell check_explicit_specialization to look for a match. */ 900 SET_DECL_IMPLICIT_INSTANTIATION (function); 901 DECL_TEMPLATE_INFO (function) = build_template_info (fns, NULL_TREE); 902 matched = function; 903 } 904 905 if (!matched) 906 { 907 if (!COMPLETE_TYPE_P (ctype)) 908 cxx_incomplete_type_error (DECL_SOURCE_LOCATION (function), 909 function, ctype); 910 else 911 { 912 if (DECL_CONV_FN_P (function)) 913 fns = get_class_binding (ctype, conv_op_identifier); 914 915 error_at (DECL_SOURCE_LOCATION (function), 916 "no declaration matches %q#D", function); 917 if (fns) 918 print_candidates (fns); 919 else if (DECL_CONV_FN_P (function)) 920 inform (DECL_SOURCE_LOCATION (function), 921 "no conversion operators declared"); 922 else 923 inform (DECL_SOURCE_LOCATION (function), 924 "no functions named %qD", function); 925 inform (DECL_SOURCE_LOCATION (TYPE_NAME (ctype)), 926 "%#qT defined here", ctype); 927 } 928 matched = error_mark_node; 929 } 930 931 if (pushed_scope) 932 pop_scope (pushed_scope); 933 934 return matched; 935 } 936 937 /* DECL is a function with vague linkage. Remember it so that at the 938 end of the translation unit we can decide whether or not to emit 939 it. */ 940 941 void 942 note_vague_linkage_fn (tree decl) 943 { 944 if (processing_template_decl) 945 return; 946 947 DECL_DEFER_OUTPUT (decl) = 1; 948 vec_safe_push (deferred_fns, decl); 949 } 950 951 /* As above, but for variables. */ 952 953 void 954 note_vague_linkage_variable (tree decl) 955 { 956 vec_safe_push (pending_statics, decl); 957 } 958 959 /* We have just processed the DECL, which is a static data member. 960 The other parameters are as for cp_finish_decl. */ 961 962 void 963 finish_static_data_member_decl (tree decl, 964 tree init, bool init_const_expr_p, 965 tree asmspec_tree, 966 int flags) 967 { 968 if (DECL_TEMPLATE_INSTANTIATED (decl)) 969 /* We already needed to instantiate this, so the processing in this 970 function is unnecessary/wrong. */ 971 return; 972 973 DECL_CONTEXT (decl) = current_class_type; 974 975 /* We cannot call pushdecl here, because that would fill in the 976 TREE_CHAIN of our decl. Instead, we modify cp_finish_decl to do 977 the right thing, namely, to put this decl out straight away. */ 978 979 if (! processing_template_decl) 980 vec_safe_push (pending_statics, decl); 981 982 if (LOCAL_CLASS_P (current_class_type) 983 /* We already complained about the template definition. */ 984 && !DECL_TEMPLATE_INSTANTIATION (decl)) 985 permerror (DECL_SOURCE_LOCATION (decl), 986 "local class %q#T shall not have static data member %q#D", 987 current_class_type, decl); 988 else 989 for (tree t = current_class_type; TYPE_P (t); 990 t = CP_TYPE_CONTEXT (t)) 991 if (TYPE_UNNAMED_P (t)) 992 { 993 auto_diagnostic_group d; 994 if (permerror (DECL_SOURCE_LOCATION (decl), 995 "static data member %qD in unnamed class", decl)) 996 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)), 997 "unnamed class defined here"); 998 break; 999 } 1000 1001 if (DECL_INLINE_VAR_P (decl) && !DECL_TEMPLATE_INSTANTIATION (decl)) 1002 /* An inline variable is immediately defined, so don't set DECL_IN_AGGR_P. 1003 Except that if decl is a template instantiation, it isn't defined until 1004 instantiate_decl. */; 1005 else 1006 DECL_IN_AGGR_P (decl) = 1; 1007 1008 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE 1009 && TYPE_DOMAIN (TREE_TYPE (decl)) == NULL_TREE) 1010 SET_VAR_HAD_UNKNOWN_BOUND (decl); 1011 1012 if (init) 1013 { 1014 /* Similarly to start_decl_1, we want to complete the type in order 1015 to do the right thing in cp_apply_type_quals_to_decl, possibly 1016 clear TYPE_QUAL_CONST (c++/65579). */ 1017 tree type = TREE_TYPE (decl) = complete_type (TREE_TYPE (decl)); 1018 cp_apply_type_quals_to_decl (cp_type_quals (type), decl); 1019 } 1020 1021 cp_finish_decl (decl, init, init_const_expr_p, asmspec_tree, flags); 1022 } 1023 1024 /* DECLARATOR and DECLSPECS correspond to a class member. The other 1025 parameters are as for cp_finish_decl. Return the DECL for the 1026 class member declared. */ 1027 1028 tree 1029 grokfield (const cp_declarator *declarator, 1030 cp_decl_specifier_seq *declspecs, 1031 tree init, bool init_const_expr_p, 1032 tree asmspec_tree, 1033 tree attrlist) 1034 { 1035 tree value; 1036 const char *asmspec = 0; 1037 int flags; 1038 1039 if (init 1040 && TREE_CODE (init) == TREE_LIST 1041 && TREE_VALUE (init) == error_mark_node 1042 && TREE_CHAIN (init) == NULL_TREE) 1043 init = NULL_TREE; 1044 1045 int initialized; 1046 if (init == ridpointers[(int)RID_DELETE]) 1047 initialized = SD_DELETED; 1048 else if (init == ridpointers[(int)RID_DEFAULT]) 1049 initialized = SD_DEFAULTED; 1050 else if (init) 1051 initialized = SD_INITIALIZED; 1052 else 1053 initialized = SD_UNINITIALIZED; 1054 1055 value = grokdeclarator (declarator, declspecs, FIELD, initialized, &attrlist); 1056 if (! value || value == error_mark_node) 1057 /* friend or constructor went bad. */ 1058 return error_mark_node; 1059 if (TREE_TYPE (value) == error_mark_node) 1060 return value; 1061 1062 if (TREE_CODE (value) == TYPE_DECL && init) 1063 { 1064 error_at (cp_expr_loc_or_loc (init, DECL_SOURCE_LOCATION (value)), 1065 "typedef %qD is initialized (use %qs instead)", 1066 value, "decltype"); 1067 init = NULL_TREE; 1068 } 1069 1070 /* Pass friendly classes back. */ 1071 if (value == void_type_node) 1072 return value; 1073 1074 if (DECL_NAME (value) 1075 && TREE_CODE (DECL_NAME (value)) == TEMPLATE_ID_EXPR) 1076 { 1077 error_at (declarator->id_loc, 1078 "explicit template argument list not allowed"); 1079 return error_mark_node; 1080 } 1081 1082 /* Stash away type declarations. */ 1083 if (TREE_CODE (value) == TYPE_DECL) 1084 { 1085 DECL_NONLOCAL (value) = 1; 1086 DECL_CONTEXT (value) = current_class_type; 1087 1088 if (attrlist) 1089 { 1090 int attrflags = 0; 1091 1092 /* If this is a typedef that names the class for linkage purposes 1093 (7.1.3p8), apply any attributes directly to the type. */ 1094 if (OVERLOAD_TYPE_P (TREE_TYPE (value)) 1095 && value == TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value)))) 1096 attrflags = ATTR_FLAG_TYPE_IN_PLACE; 1097 1098 cplus_decl_attributes (&value, attrlist, attrflags); 1099 } 1100 1101 if (decl_spec_seq_has_spec_p (declspecs, ds_typedef) 1102 && TREE_TYPE (value) != error_mark_node 1103 && TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (value))) != value) 1104 set_underlying_type (value); 1105 1106 /* It's important that push_template_decl below follows 1107 set_underlying_type above so that the created template 1108 carries the properly set type of VALUE. */ 1109 if (processing_template_decl) 1110 value = push_template_decl (value); 1111 1112 record_locally_defined_typedef (value); 1113 return value; 1114 } 1115 1116 int friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend); 1117 1118 if (!friendp && DECL_IN_AGGR_P (value)) 1119 { 1120 error ("%qD is already defined in %qT", value, DECL_CONTEXT (value)); 1121 return void_type_node; 1122 } 1123 1124 if (asmspec_tree && asmspec_tree != error_mark_node) 1125 asmspec = TREE_STRING_POINTER (asmspec_tree); 1126 1127 if (init) 1128 { 1129 if (TREE_CODE (value) == FUNCTION_DECL) 1130 { 1131 if (init == ridpointers[(int)RID_DELETE]) 1132 { 1133 DECL_DELETED_FN (value) = 1; 1134 DECL_DECLARED_INLINE_P (value) = 1; 1135 } 1136 else if (init == ridpointers[(int)RID_DEFAULT]) 1137 { 1138 if (defaultable_fn_check (value)) 1139 { 1140 DECL_DEFAULTED_FN (value) = 1; 1141 DECL_INITIALIZED_IN_CLASS_P (value) = 1; 1142 DECL_DECLARED_INLINE_P (value) = 1; 1143 /* grokfndecl set this to error_mark_node, but we want to 1144 leave it unset until synthesize_method. */ 1145 DECL_INITIAL (value) = NULL_TREE; 1146 } 1147 } 1148 else if (TREE_CODE (init) == DEFERRED_PARSE) 1149 error ("invalid initializer for member function %qD", value); 1150 else if (TREE_CODE (TREE_TYPE (value)) == METHOD_TYPE) 1151 { 1152 if (integer_zerop (init)) 1153 DECL_PURE_VIRTUAL_P (value) = 1; 1154 else if (error_operand_p (init)) 1155 ; /* An error has already been reported. */ 1156 else 1157 error ("invalid initializer for member function %qD", 1158 value); 1159 } 1160 else 1161 { 1162 gcc_assert (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE); 1163 location_t iloc 1164 = cp_expr_loc_or_loc (init, DECL_SOURCE_LOCATION (value)); 1165 if (friendp) 1166 error_at (iloc, "initializer specified for friend " 1167 "function %qD", value); 1168 else 1169 error_at (iloc, "initializer specified for static " 1170 "member function %qD", value); 1171 } 1172 } 1173 else if (TREE_CODE (value) == FIELD_DECL) 1174 /* C++11 NSDMI, keep going. */; 1175 else if (!VAR_P (value)) 1176 gcc_unreachable (); 1177 } 1178 1179 /* Pass friend decls back. */ 1180 if ((TREE_CODE (value) == FUNCTION_DECL 1181 || TREE_CODE (value) == TEMPLATE_DECL) 1182 && DECL_CONTEXT (value) != current_class_type) 1183 { 1184 if (attrlist) 1185 cplus_decl_attributes (&value, attrlist, 0); 1186 return value; 1187 } 1188 1189 /* Need to set this before push_template_decl. */ 1190 if (VAR_P (value)) 1191 DECL_CONTEXT (value) = current_class_type; 1192 1193 if (processing_template_decl && VAR_OR_FUNCTION_DECL_P (value)) 1194 { 1195 value = push_template_decl (value); 1196 if (error_operand_p (value)) 1197 return error_mark_node; 1198 } 1199 1200 if (attrlist) 1201 cplus_decl_attributes (&value, attrlist, 0); 1202 1203 if (init && DIRECT_LIST_INIT_P (init)) 1204 flags = LOOKUP_NORMAL; 1205 else 1206 flags = LOOKUP_IMPLICIT; 1207 1208 switch (TREE_CODE (value)) 1209 { 1210 case VAR_DECL: 1211 finish_static_data_member_decl (value, init, init_const_expr_p, 1212 asmspec_tree, flags); 1213 return value; 1214 1215 case FIELD_DECL: 1216 if (asmspec) 1217 error ("%<asm%> specifiers are not permitted on non-static data members"); 1218 if (DECL_INITIAL (value) == error_mark_node) 1219 init = error_mark_node; 1220 cp_finish_decl (value, init, /*init_const_expr_p=*/false, 1221 NULL_TREE, flags); 1222 DECL_IN_AGGR_P (value) = 1; 1223 return value; 1224 1225 case FUNCTION_DECL: 1226 if (asmspec) 1227 set_user_assembler_name (value, asmspec); 1228 1229 cp_finish_decl (value, 1230 /*init=*/NULL_TREE, 1231 /*init_const_expr_p=*/false, 1232 asmspec_tree, flags); 1233 1234 /* Pass friends back this way. */ 1235 if (DECL_UNIQUE_FRIEND_P (value)) 1236 return void_type_node; 1237 1238 DECL_IN_AGGR_P (value) = 1; 1239 return value; 1240 1241 default: 1242 gcc_unreachable (); 1243 } 1244 return NULL_TREE; 1245 } 1246 1247 /* Like `grokfield', but for bitfields. 1248 WIDTH is the width of the bitfield, a constant expression. 1249 The other parameters are as for grokfield. */ 1250 1251 tree 1252 grokbitfield (const cp_declarator *declarator, 1253 cp_decl_specifier_seq *declspecs, tree width, tree init, 1254 tree attrlist) 1255 { 1256 tree value = grokdeclarator (declarator, declspecs, BITFIELD, 1257 init != NULL_TREE, &attrlist); 1258 1259 if (value == error_mark_node) 1260 return NULL_TREE; /* friends went bad. */ 1261 1262 tree type = TREE_TYPE (value); 1263 if (type == error_mark_node) 1264 return value; 1265 1266 /* Pass friendly classes back. */ 1267 if (VOID_TYPE_P (value)) 1268 return void_type_node; 1269 1270 if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type) 1271 && (INDIRECT_TYPE_P (type) || !dependent_type_p (type))) 1272 { 1273 error_at (DECL_SOURCE_LOCATION (value), 1274 "bit-field %qD with non-integral type %qT", 1275 value, type); 1276 return error_mark_node; 1277 } 1278 1279 if (TREE_CODE (value) == TYPE_DECL) 1280 { 1281 error_at (DECL_SOURCE_LOCATION (value), 1282 "cannot declare %qD to be a bit-field type", value); 1283 return NULL_TREE; 1284 } 1285 1286 /* Usually, finish_struct_1 catches bitfields with invalid types. 1287 But, in the case of bitfields with function type, we confuse 1288 ourselves into thinking they are member functions, so we must 1289 check here. */ 1290 if (TREE_CODE (value) == FUNCTION_DECL) 1291 { 1292 error_at (DECL_SOURCE_LOCATION (value), 1293 "cannot declare bit-field %qD with function type", value); 1294 return NULL_TREE; 1295 } 1296 1297 if (TYPE_WARN_IF_NOT_ALIGN (type)) 1298 { 1299 error_at (DECL_SOURCE_LOCATION (value), "cannot declare bit-field " 1300 "%qD with %<warn_if_not_aligned%> type", value); 1301 return NULL_TREE; 1302 } 1303 1304 if (DECL_IN_AGGR_P (value)) 1305 { 1306 error ("%qD is already defined in the class %qT", value, 1307 DECL_CONTEXT (value)); 1308 return void_type_node; 1309 } 1310 1311 if (TREE_STATIC (value)) 1312 { 1313 error_at (DECL_SOURCE_LOCATION (value), 1314 "static member %qD cannot be a bit-field", value); 1315 return NULL_TREE; 1316 } 1317 1318 int flags = LOOKUP_IMPLICIT; 1319 if (init && DIRECT_LIST_INIT_P (init)) 1320 flags = LOOKUP_NORMAL; 1321 cp_finish_decl (value, init, false, NULL_TREE, flags); 1322 1323 if (width != error_mark_node) 1324 { 1325 /* The width must be an integer type. */ 1326 if (!type_dependent_expression_p (width) 1327 && !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (width))) 1328 error ("width of bit-field %qD has non-integral type %qT", value, 1329 TREE_TYPE (width)); 1330 else if (!check_for_bare_parameter_packs (width)) 1331 { 1332 /* Temporarily stash the width in DECL_BIT_FIELD_REPRESENTATIVE. 1333 check_bitfield_decl picks it from there later and sets DECL_SIZE 1334 accordingly. */ 1335 DECL_BIT_FIELD_REPRESENTATIVE (value) = width; 1336 SET_DECL_C_BIT_FIELD (value); 1337 } 1338 } 1339 1340 DECL_IN_AGGR_P (value) = 1; 1341 1342 if (attrlist) 1343 cplus_decl_attributes (&value, attrlist, /*flags=*/0); 1344 1345 return value; 1346 } 1347 1348 1349 /* Returns true iff ATTR is an attribute which needs to be applied at 1351 instantiation time rather than template definition time. */ 1352 1353 static bool 1354 is_late_template_attribute (tree attr, tree decl) 1355 { 1356 tree name = get_attribute_name (attr); 1357 tree args = TREE_VALUE (attr); 1358 const struct attribute_spec *spec = lookup_attribute_spec (name); 1359 tree arg; 1360 1361 if (!spec) 1362 /* Unknown attribute. */ 1363 return false; 1364 1365 /* Attribute weak handling wants to write out assembly right away. */ 1366 if (is_attribute_p ("weak", name)) 1367 return true; 1368 1369 /* Attributes used and unused are applied directly to typedefs for the 1370 benefit of maybe_warn_unused_local_typedefs. */ 1371 if (TREE_CODE (decl) == TYPE_DECL 1372 && (is_attribute_p ("unused", name) 1373 || is_attribute_p ("used", name))) 1374 return false; 1375 1376 /* Attribute tls_model wants to modify the symtab. */ 1377 if (is_attribute_p ("tls_model", name)) 1378 return true; 1379 1380 /* #pragma omp declare simd attribute needs to be always deferred. */ 1381 if (flag_openmp 1382 && is_attribute_p ("omp declare simd", name)) 1383 return true; 1384 1385 if (args == error_mark_node) 1386 return false; 1387 1388 /* An attribute pack is clearly dependent. */ 1389 if (args && PACK_EXPANSION_P (args)) 1390 return true; 1391 1392 /* If any of the arguments are dependent expressions, we can't evaluate 1393 the attribute until instantiation time. */ 1394 for (arg = args; arg; arg = TREE_CHAIN (arg)) 1395 { 1396 tree t = TREE_VALUE (arg); 1397 1398 /* If the first attribute argument is an identifier, only consider 1399 second and following arguments. Attributes like mode, format, 1400 cleanup and several target specific attributes aren't late 1401 just because they have an IDENTIFIER_NODE as first argument. */ 1402 if (arg == args && attribute_takes_identifier_p (name) 1403 && identifier_p (t)) 1404 continue; 1405 1406 if (value_dependent_expression_p (t)) 1407 return true; 1408 } 1409 1410 if (TREE_CODE (decl) == TYPE_DECL 1411 || TYPE_P (decl) 1412 || spec->type_required) 1413 { 1414 tree type = TYPE_P (decl) ? decl : TREE_TYPE (decl); 1415 1416 if (!type) 1417 return true; 1418 1419 /* We can't apply any attributes to a completely unknown type until 1420 instantiation time. */ 1421 enum tree_code code = TREE_CODE (type); 1422 if (code == TEMPLATE_TYPE_PARM 1423 || code == BOUND_TEMPLATE_TEMPLATE_PARM 1424 || code == TYPENAME_TYPE) 1425 return true; 1426 /* Also defer most attributes on dependent types. This is not 1427 necessary in all cases, but is the better default. */ 1428 else if (dependent_type_p (type) 1429 /* But some attributes specifically apply to templates. */ 1430 && !is_attribute_p ("abi_tag", name) 1431 && !is_attribute_p ("deprecated", name) 1432 && !is_attribute_p ("unavailable", name) 1433 && !is_attribute_p ("visibility", name)) 1434 return true; 1435 else 1436 return false; 1437 } 1438 else 1439 return false; 1440 } 1441 1442 /* ATTR_P is a list of attributes. Remove any attributes which need to be 1443 applied at instantiation time and return them. If IS_DEPENDENT is true, 1444 the declaration itself is dependent, so all attributes should be applied 1445 at instantiation time. */ 1446 1447 tree 1448 splice_template_attributes (tree *attr_p, tree decl) 1449 { 1450 tree *p = attr_p; 1451 tree late_attrs = NULL_TREE; 1452 tree *q = &late_attrs; 1453 1454 if (!p || *p == error_mark_node) 1455 return NULL_TREE; 1456 1457 for (; *p; ) 1458 { 1459 if (is_late_template_attribute (*p, decl)) 1460 { 1461 ATTR_IS_DEPENDENT (*p) = 1; 1462 *q = *p; 1463 *p = TREE_CHAIN (*p); 1464 q = &TREE_CHAIN (*q); 1465 *q = NULL_TREE; 1466 } 1467 else 1468 p = &TREE_CHAIN (*p); 1469 } 1470 1471 return late_attrs; 1472 } 1473 1474 /* Attach any LATE_ATTRS to DECL_P, after the non-dependent attributes have 1475 been applied by a previous call to decl_attributes. */ 1476 1477 static void 1478 save_template_attributes (tree late_attrs, tree *decl_p, int flags) 1479 { 1480 tree *q; 1481 1482 if (!late_attrs) 1483 return; 1484 1485 if (DECL_P (*decl_p)) 1486 q = &DECL_ATTRIBUTES (*decl_p); 1487 else 1488 q = &TYPE_ATTRIBUTES (*decl_p); 1489 1490 tree old_attrs = *q; 1491 1492 /* Place the late attributes at the beginning of the attribute 1493 list. */ 1494 late_attrs = chainon (late_attrs, *q); 1495 if (*q != late_attrs 1496 && !DECL_P (*decl_p) 1497 && !(flags & ATTR_FLAG_TYPE_IN_PLACE)) 1498 { 1499 if (!dependent_type_p (*decl_p)) 1500 *decl_p = cp_build_type_attribute_variant (*decl_p, late_attrs); 1501 else 1502 { 1503 *decl_p = build_variant_type_copy (*decl_p); 1504 TYPE_ATTRIBUTES (*decl_p) = late_attrs; 1505 } 1506 } 1507 else 1508 *q = late_attrs; 1509 1510 if (!DECL_P (*decl_p) && *decl_p == TYPE_MAIN_VARIANT (*decl_p)) 1511 { 1512 /* We've added new attributes directly to the main variant, so 1513 now we need to update all of the other variants to include 1514 these new attributes. */ 1515 tree variant; 1516 for (variant = TYPE_NEXT_VARIANT (*decl_p); variant; 1517 variant = TYPE_NEXT_VARIANT (variant)) 1518 { 1519 gcc_assert (TYPE_ATTRIBUTES (variant) == old_attrs); 1520 TYPE_ATTRIBUTES (variant) = TYPE_ATTRIBUTES (*decl_p); 1521 } 1522 } 1523 } 1524 1525 /* True if ATTRS contains any dependent attributes that affect type 1526 identity. */ 1527 1528 bool 1529 any_dependent_type_attributes_p (tree attrs) 1530 { 1531 for (tree a = attrs; a; a = TREE_CHAIN (a)) 1532 if (ATTR_IS_DEPENDENT (a)) 1533 { 1534 const attribute_spec *as = lookup_attribute_spec (TREE_PURPOSE (a)); 1535 if (as && as->affects_type_identity) 1536 return true; 1537 } 1538 return false; 1539 } 1540 1541 /* Return true iff ATTRS are acceptable attributes to be applied in-place 1542 to a typedef which gives a previously unnamed class or enum a name for 1543 linkage purposes. */ 1544 1545 bool 1546 attributes_naming_typedef_ok (tree attrs) 1547 { 1548 for (; attrs; attrs = TREE_CHAIN (attrs)) 1549 { 1550 tree name = get_attribute_name (attrs); 1551 if (is_attribute_p ("vector_size", name)) 1552 return false; 1553 } 1554 return true; 1555 } 1556 1557 /* Like reconstruct_complex_type, but handle also template trees. */ 1558 1559 tree 1560 cp_reconstruct_complex_type (tree type, tree bottom) 1561 { 1562 tree inner, outer; 1563 1564 if (TYPE_PTR_P (type)) 1565 { 1566 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom); 1567 outer = build_pointer_type_for_mode (inner, TYPE_MODE (type), 1568 TYPE_REF_CAN_ALIAS_ALL (type)); 1569 } 1570 else if (TYPE_REF_P (type)) 1571 { 1572 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom); 1573 outer = build_reference_type_for_mode (inner, TYPE_MODE (type), 1574 TYPE_REF_CAN_ALIAS_ALL (type)); 1575 } 1576 else if (TREE_CODE (type) == ARRAY_TYPE) 1577 { 1578 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom); 1579 outer = build_cplus_array_type (inner, TYPE_DOMAIN (type)); 1580 /* Don't call cp_build_qualified_type on ARRAY_TYPEs, the 1581 element type qualification will be handled by the recursive 1582 cp_reconstruct_complex_type call and cp_build_qualified_type 1583 for ARRAY_TYPEs changes the element type. */ 1584 return outer; 1585 } 1586 else if (TREE_CODE (type) == FUNCTION_TYPE) 1587 { 1588 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom); 1589 outer = build_function_type (inner, TYPE_ARG_TYPES (type)); 1590 outer = apply_memfn_quals (outer, type_memfn_quals (type)); 1591 } 1592 else if (TREE_CODE (type) == METHOD_TYPE) 1593 { 1594 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom); 1595 /* The build_method_type_directly() routine prepends 'this' to argument list, 1596 so we must compensate by getting rid of it. */ 1597 outer 1598 = build_method_type_directly 1599 (class_of_this_parm (type), inner, 1600 TREE_CHAIN (TYPE_ARG_TYPES (type))); 1601 } 1602 else if (TREE_CODE (type) == OFFSET_TYPE) 1603 { 1604 inner = cp_reconstruct_complex_type (TREE_TYPE (type), bottom); 1605 outer = build_offset_type (TYPE_OFFSET_BASETYPE (type), inner); 1606 } 1607 else 1608 return bottom; 1609 1610 if (TYPE_ATTRIBUTES (type)) 1611 outer = cp_build_type_attribute_variant (outer, TYPE_ATTRIBUTES (type)); 1612 outer = cp_build_qualified_type (outer, cp_type_quals (type)); 1613 outer = cxx_copy_lang_qualifiers (outer, type); 1614 1615 return outer; 1616 } 1617 1618 /* Replaces any constexpr expression that may be into the attributes 1619 arguments with their reduced value. */ 1620 1621 void 1622 cp_check_const_attributes (tree attributes) 1623 { 1624 if (attributes == error_mark_node) 1625 return; 1626 1627 tree attr; 1628 for (attr = attributes; attr; attr = TREE_CHAIN (attr)) 1629 { 1630 if (cxx_contract_attribute_p (attr)) 1631 continue; 1632 1633 tree arg; 1634 /* As we implement alignas using gnu::aligned attribute and 1635 alignas argument is a constant expression, force manifestly 1636 constant evaluation of aligned attribute argument. */ 1637 bool manifestly_const_eval 1638 = is_attribute_p ("aligned", get_attribute_name (attr)); 1639 for (arg = TREE_VALUE (attr); arg && TREE_CODE (arg) == TREE_LIST; 1640 arg = TREE_CHAIN (arg)) 1641 { 1642 tree expr = TREE_VALUE (arg); 1643 if (EXPR_P (expr)) 1644 TREE_VALUE (arg) 1645 = fold_non_dependent_expr (expr, tf_warning_or_error, 1646 manifestly_const_eval); 1647 } 1648 } 1649 } 1650 1651 /* Copies hot or cold attributes to a function FN if present on the 1652 encapsulating class, struct, or union TYPE. */ 1653 1654 void 1655 maybe_propagate_warmth_attributes (tree fn, tree type) 1656 { 1657 if (fn == NULL_TREE || type == NULL_TREE 1658 || !(TREE_CODE (type) == RECORD_TYPE 1659 || TREE_CODE (type) == UNION_TYPE)) 1660 return; 1661 1662 tree has_cold_attr = lookup_attribute ("cold", TYPE_ATTRIBUTES (type)); 1663 tree has_hot_attr = lookup_attribute ("hot", TYPE_ATTRIBUTES (type)); 1664 1665 if (has_cold_attr || has_hot_attr) 1666 { 1667 /* Transparently ignore the new warmth attribute if it 1668 conflicts with a present function attribute. Otherwise 1669 decl_attribute would still honour the present attribute, 1670 but producing an undesired warning in the process. */ 1671 1672 if (has_cold_attr) 1673 { 1674 if (lookup_attribute ("hot", DECL_ATTRIBUTES (fn)) == NULL) 1675 { 1676 tree cold_cons 1677 = tree_cons (get_identifier ("cold"), NULL, NULL); 1678 1679 decl_attributes (&fn, cold_cons, 0); 1680 } 1681 } 1682 else if (has_hot_attr) 1683 { 1684 if (lookup_attribute ("cold", DECL_ATTRIBUTES (fn)) == NULL) 1685 { 1686 tree hot_cons 1687 = tree_cons (get_identifier ("hot"), NULL, NULL); 1688 1689 decl_attributes (&fn, hot_cons, 0); 1690 } 1691 } 1692 } 1693 } 1694 1695 /* Return the last pushed declaration for the symbol DECL or NULL 1696 when no such declaration exists. */ 1697 1698 static tree 1699 find_last_decl (tree decl) 1700 { 1701 tree last_decl = NULL_TREE; 1702 1703 if (tree name = DECL_P (decl) ? DECL_NAME (decl) : NULL_TREE) 1704 { 1705 /* Template specializations are matched elsewhere. */ 1706 if (DECL_LANG_SPECIFIC (decl) 1707 && DECL_USE_TEMPLATE (decl)) 1708 return NULL_TREE; 1709 1710 /* Look up the declaration in its scope. */ 1711 tree pushed_scope = NULL_TREE; 1712 if (tree ctype = DECL_CONTEXT (decl)) 1713 pushed_scope = push_scope (ctype); 1714 1715 last_decl = lookup_name (name); 1716 1717 if (pushed_scope) 1718 pop_scope (pushed_scope); 1719 1720 /* The declaration may be a member conversion operator 1721 or a bunch of overfloads (handle the latter below). */ 1722 if (last_decl && BASELINK_P (last_decl)) 1723 last_decl = BASELINK_FUNCTIONS (last_decl); 1724 } 1725 1726 if (!last_decl) 1727 return NULL_TREE; 1728 1729 if (DECL_P (last_decl) || TREE_CODE (last_decl) == OVERLOAD) 1730 { 1731 /* A set of overloads of the same function. */ 1732 for (lkp_iterator iter (last_decl); iter; ++iter) 1733 { 1734 if (TREE_CODE (*iter) == OVERLOAD) 1735 continue; 1736 1737 tree d = *iter; 1738 1739 /* We can't compare versions in the middle of processing the 1740 attribute that has the version. */ 1741 if (TREE_CODE (d) == FUNCTION_DECL 1742 && DECL_FUNCTION_VERSIONED (d)) 1743 return NULL_TREE; 1744 1745 if (decls_match (decl, d, /*record_decls=*/false)) 1746 return d; 1747 } 1748 return NULL_TREE; 1749 } 1750 1751 return NULL_TREE; 1752 } 1753 1754 /* Like decl_attributes, but handle C++ complexity. */ 1755 1756 void 1757 cplus_decl_attributes (tree *decl, tree attributes, int flags) 1758 { 1759 if (*decl == NULL_TREE || *decl == void_type_node 1760 || *decl == error_mark_node || attributes == error_mark_node) 1761 return; 1762 1763 /* Add implicit "omp declare target" attribute if requested. */ 1764 if (vec_safe_length (scope_chain->omp_declare_target_attribute) 1765 && ((VAR_P (*decl) 1766 && (TREE_STATIC (*decl) || DECL_EXTERNAL (*decl))) 1767 || TREE_CODE (*decl) == FUNCTION_DECL)) 1768 { 1769 if (VAR_P (*decl) 1770 && DECL_CLASS_SCOPE_P (*decl)) 1771 error ("%q+D static data member inside of declare target directive", 1772 *decl); 1773 else 1774 { 1775 if (VAR_P (*decl) 1776 && (processing_template_decl 1777 || !omp_mappable_type (TREE_TYPE (*decl)))) 1778 attributes 1779 = tree_cons (get_identifier ("omp declare target implicit"), 1780 NULL_TREE, attributes); 1781 else 1782 { 1783 attributes = tree_cons (get_identifier ("omp declare target"), 1784 NULL_TREE, attributes); 1785 attributes 1786 = tree_cons (get_identifier ("omp declare target block"), 1787 NULL_TREE, attributes); 1788 } 1789 if (TREE_CODE (*decl) == FUNCTION_DECL) 1790 { 1791 cp_omp_declare_target_attr &last 1792 = scope_chain->omp_declare_target_attribute->last (); 1793 int device_type = MAX (last.device_type, 0); 1794 if ((device_type & OMP_CLAUSE_DEVICE_TYPE_HOST) != 0 1795 && !lookup_attribute ("omp declare target host", 1796 attributes)) 1797 attributes 1798 = tree_cons (get_identifier ("omp declare target host"), 1799 NULL_TREE, attributes); 1800 if ((device_type & OMP_CLAUSE_DEVICE_TYPE_NOHOST) != 0 1801 && !lookup_attribute ("omp declare target nohost", 1802 attributes)) 1803 attributes 1804 = tree_cons (get_identifier ("omp declare target nohost"), 1805 NULL_TREE, attributes); 1806 if (last.indirect 1807 && !lookup_attribute ("omp declare target indirect", 1808 attributes)) 1809 attributes 1810 = tree_cons (get_identifier ("omp declare target indirect"), 1811 NULL_TREE, attributes); 1812 } 1813 } 1814 } 1815 1816 tree late_attrs = NULL_TREE; 1817 if (processing_template_decl) 1818 { 1819 if (check_for_bare_parameter_packs (attributes)) 1820 return; 1821 late_attrs = splice_template_attributes (&attributes, *decl); 1822 } 1823 1824 cp_check_const_attributes (attributes); 1825 1826 if (flag_openmp || flag_openmp_simd) 1827 { 1828 bool diagnosed = false; 1829 for (tree *pa = &attributes; *pa; ) 1830 { 1831 if (get_attribute_namespace (*pa) == omp_identifier) 1832 { 1833 tree name = get_attribute_name (*pa); 1834 if (is_attribute_p ("directive", name) 1835 || is_attribute_p ("sequence", name) 1836 || is_attribute_p ("decl", name)) 1837 { 1838 const char *p = NULL; 1839 if (TREE_VALUE (*pa) == NULL_TREE) 1840 p = IDENTIFIER_POINTER (name); 1841 for (tree a = TREE_VALUE (*pa); a; a = TREE_CHAIN (a)) 1842 { 1843 tree d = TREE_VALUE (a); 1844 gcc_assert (TREE_CODE (d) == DEFERRED_PARSE); 1845 if (TREE_PUBLIC (d) 1846 && (VAR_P (*decl) 1847 || TREE_CODE (*decl) == FUNCTION_DECL) 1848 && cp_maybe_parse_omp_decl (*decl, d)) 1849 continue; 1850 p = TREE_PUBLIC (d) ? "decl" : "directive"; 1851 } 1852 if (p && !diagnosed) 1853 { 1854 error ("%<omp::%s%> not allowed to be specified in " 1855 "this context", p); 1856 diagnosed = true; 1857 } 1858 if (p) 1859 { 1860 *pa = TREE_CHAIN (*pa); 1861 continue; 1862 } 1863 } 1864 } 1865 pa = &TREE_CHAIN (*pa); 1866 } 1867 } 1868 1869 if (TREE_CODE (*decl) == TEMPLATE_DECL) 1870 decl = &DECL_TEMPLATE_RESULT (*decl); 1871 1872 if (TREE_TYPE (*decl) && TYPE_PTRMEMFUNC_P (TREE_TYPE (*decl))) 1873 { 1874 attributes 1875 = decl_attributes (decl, attributes, flags | ATTR_FLAG_FUNCTION_NEXT); 1876 decl_attributes (&TYPE_PTRMEMFUNC_FN_TYPE_RAW (TREE_TYPE (*decl)), 1877 attributes, flags); 1878 } 1879 else 1880 { 1881 tree last_decl = find_last_decl (*decl); 1882 decl_attributes (decl, attributes, flags, last_decl); 1883 } 1884 1885 if (late_attrs) 1886 save_template_attributes (late_attrs, decl, flags); 1887 1888 /* Propagate deprecation out to the template. */ 1889 if (TREE_DEPRECATED (*decl)) 1890 if (tree ti = get_template_info (*decl)) 1891 { 1892 tree tmpl = TI_TEMPLATE (ti); 1893 tree pattern = (TYPE_P (*decl) ? TREE_TYPE (tmpl) 1894 : DECL_TEMPLATE_RESULT (tmpl)); 1895 if (*decl == pattern) 1896 TREE_DEPRECATED (tmpl) = true; 1897 } 1898 1899 /* Likewise, propagate unavailability out to the template. */ 1900 if (TREE_UNAVAILABLE (*decl)) 1901 if (tree ti = get_template_info (*decl)) 1902 { 1903 tree tmpl = TI_TEMPLATE (ti); 1904 tree pattern = (TYPE_P (*decl) ? TREE_TYPE (tmpl) 1905 : DECL_TEMPLATE_RESULT (tmpl)); 1906 if (*decl == pattern) 1907 TREE_UNAVAILABLE (tmpl) = true; 1908 } 1909 } 1910 1911 /* Walks through the namespace- or function-scope anonymous union 1913 OBJECT, with the indicated TYPE, building appropriate VAR_DECLs. 1914 Returns one of the fields for use in the mangled name. */ 1915 1916 static tree 1917 build_anon_union_vars (tree type, tree object) 1918 { 1919 tree main_decl = NULL_TREE; 1920 tree field; 1921 1922 /* Rather than write the code to handle the non-union case, 1923 just give an error. */ 1924 if (TREE_CODE (type) != UNION_TYPE) 1925 { 1926 error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)), 1927 "anonymous struct not inside named type"); 1928 return error_mark_node; 1929 } 1930 1931 for (field = TYPE_FIELDS (type); 1932 field != NULL_TREE; 1933 field = DECL_CHAIN (field)) 1934 { 1935 tree decl; 1936 tree ref; 1937 1938 if (DECL_ARTIFICIAL (field)) 1939 continue; 1940 if (TREE_CODE (field) != FIELD_DECL) 1941 { 1942 permerror (DECL_SOURCE_LOCATION (field), 1943 "%q#D invalid; an anonymous union can only " 1944 "have non-static data members", field); 1945 continue; 1946 } 1947 1948 if (TREE_PRIVATE (field)) 1949 permerror (DECL_SOURCE_LOCATION (field), 1950 "private member %q#D in anonymous union", field); 1951 else if (TREE_PROTECTED (field)) 1952 permerror (DECL_SOURCE_LOCATION (field), 1953 "protected member %q#D in anonymous union", field); 1954 1955 if (processing_template_decl) 1956 ref = build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF, object, 1957 field, NULL_TREE); 1958 else 1959 ref = build_class_member_access_expr (object, field, NULL_TREE, 1960 false, tf_warning_or_error); 1961 1962 if (DECL_NAME (field)) 1963 { 1964 tree base; 1965 1966 decl = build_decl (input_location, 1967 VAR_DECL, DECL_NAME (field), TREE_TYPE (field)); 1968 DECL_ANON_UNION_VAR_P (decl) = 1; 1969 DECL_ARTIFICIAL (decl) = 1; 1970 1971 base = get_base_address (object); 1972 TREE_PUBLIC (decl) = TREE_PUBLIC (base); 1973 TREE_STATIC (decl) = TREE_STATIC (base); 1974 DECL_EXTERNAL (decl) = DECL_EXTERNAL (base); 1975 1976 SET_DECL_VALUE_EXPR (decl, ref); 1977 DECL_HAS_VALUE_EXPR_P (decl) = 1; 1978 1979 decl = pushdecl (decl); 1980 } 1981 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field))) 1982 decl = build_anon_union_vars (TREE_TYPE (field), ref); 1983 else 1984 decl = 0; 1985 1986 if (main_decl == NULL_TREE) 1987 main_decl = decl; 1988 } 1989 1990 return main_decl; 1991 } 1992 1993 /* Finish off the processing of a UNION_TYPE structure. If the union is an 1994 anonymous union, then all members must be laid out together. PUBLIC_P 1995 is nonzero if this union is not declared static. */ 1996 1997 void 1998 finish_anon_union (tree anon_union_decl) 1999 { 2000 tree type; 2001 tree main_decl; 2002 bool public_p; 2003 2004 if (anon_union_decl == error_mark_node) 2005 return; 2006 2007 type = TREE_TYPE (anon_union_decl); 2008 public_p = TREE_PUBLIC (anon_union_decl); 2009 2010 /* The VAR_DECL's context is the same as the TYPE's context. */ 2011 DECL_CONTEXT (anon_union_decl) = DECL_CONTEXT (TYPE_NAME (type)); 2012 2013 if (TYPE_FIELDS (type) == NULL_TREE) 2014 return; 2015 2016 if (public_p) 2017 { 2018 error ("namespace-scope anonymous aggregates must be static"); 2019 return; 2020 } 2021 2022 main_decl = build_anon_union_vars (type, anon_union_decl); 2023 if (main_decl == error_mark_node) 2024 return; 2025 if (main_decl == NULL_TREE) 2026 { 2027 pedwarn (input_location, 0, "anonymous union with no members"); 2028 return; 2029 } 2030 2031 if (!processing_template_decl) 2032 { 2033 /* Use main_decl to set the mangled name. */ 2034 DECL_NAME (anon_union_decl) = DECL_NAME (main_decl); 2035 maybe_commonize_var (anon_union_decl); 2036 if (TREE_STATIC (anon_union_decl) || DECL_EXTERNAL (anon_union_decl)) 2037 { 2038 if (DECL_DISCRIMINATOR_P (anon_union_decl)) 2039 determine_local_discriminator (anon_union_decl); 2040 mangle_decl (anon_union_decl); 2041 } 2042 DECL_NAME (anon_union_decl) = NULL_TREE; 2043 } 2044 2045 pushdecl (anon_union_decl); 2046 cp_finish_decl (anon_union_decl, NULL_TREE, false, NULL_TREE, 0); 2047 } 2048 2049 /* Auxiliary functions to make type signatures for 2051 `operator new' and `operator delete' correspond to 2052 what compiler will be expecting. */ 2053 2054 tree 2055 coerce_new_type (tree type, location_t loc) 2056 { 2057 int e = 0; 2058 tree args = TYPE_ARG_TYPES (type); 2059 2060 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE); 2061 2062 if (!same_type_p (TREE_TYPE (type), ptr_type_node)) 2063 { 2064 e = 1; 2065 error_at (loc, "%<operator new%> must return type %qT", 2066 ptr_type_node); 2067 } 2068 2069 if (args && args != void_list_node) 2070 { 2071 if (TREE_PURPOSE (args)) 2072 { 2073 /* [basic.stc.dynamic.allocation] 2074 2075 The first parameter shall not have an associated default 2076 argument. */ 2077 error_at (loc, "the first parameter of %<operator new%> cannot " 2078 "have a default argument"); 2079 /* Throw away the default argument. */ 2080 TREE_PURPOSE (args) = NULL_TREE; 2081 } 2082 2083 if (!same_type_p (TREE_VALUE (args), size_type_node)) 2084 { 2085 e = 2; 2086 args = TREE_CHAIN (args); 2087 } 2088 } 2089 else 2090 e = 2; 2091 2092 if (e == 2) 2093 permerror (loc, "%<operator new%> takes type %<size_t%> (%qT) " 2094 "as first parameter", size_type_node); 2095 2096 switch (e) 2097 { 2098 case 2: 2099 args = tree_cons (NULL_TREE, size_type_node, args); 2100 /* Fall through. */ 2101 case 1: 2102 type = (cxx_copy_lang_qualifiers 2103 (build_function_type (ptr_type_node, args), 2104 type)); 2105 /* Fall through. */ 2106 default:; 2107 } 2108 return type; 2109 } 2110 2111 void 2112 coerce_delete_type (tree decl, location_t loc) 2113 { 2114 int e = 0; 2115 tree type = TREE_TYPE (decl); 2116 tree args = TYPE_ARG_TYPES (type); 2117 2118 gcc_assert (TREE_CODE (type) == FUNCTION_TYPE); 2119 2120 if (!same_type_p (TREE_TYPE (type), void_type_node)) 2121 { 2122 e = 1; 2123 error_at (loc, "%<operator delete%> must return type %qT", 2124 void_type_node); 2125 } 2126 2127 tree ptrtype = ptr_type_node; 2128 if (destroying_delete_p (decl)) 2129 { 2130 if (DECL_CLASS_SCOPE_P (decl)) 2131 /* If the function is a destroying operator delete declared in class 2132 type C, the type of its first parameter shall be C*. */ 2133 ptrtype = build_pointer_type (DECL_CONTEXT (decl)); 2134 else 2135 /* A destroying operator delete shall be a class member function named 2136 operator delete. */ 2137 error_at (loc, 2138 "destroying %<operator delete%> must be a member function"); 2139 const ovl_op_info_t *op = IDENTIFIER_OVL_OP_INFO (DECL_NAME (decl)); 2140 if (op->flags & OVL_OP_FLAG_VEC) 2141 error_at (loc, "%<operator delete[]%> cannot be a destroying delete"); 2142 if (!usual_deallocation_fn_p (decl)) 2143 error_at (loc, "destroying %<operator delete%> must be a usual " 2144 "deallocation function"); 2145 } 2146 2147 if (!args || args == void_list_node 2148 || !same_type_p (TREE_VALUE (args), ptrtype)) 2149 { 2150 e = 2; 2151 if (args && args != void_list_node) 2152 args = TREE_CHAIN (args); 2153 error_at (loc, "%<operator delete%> takes type %qT as first parameter", 2154 ptrtype); 2155 } 2156 switch (e) 2157 { 2158 case 2: 2159 args = tree_cons (NULL_TREE, ptrtype, args); 2160 /* Fall through. */ 2161 case 1: 2162 type = (cxx_copy_lang_qualifiers 2163 (build_function_type (void_type_node, args), 2164 type)); 2165 /* Fall through. */ 2166 default:; 2167 } 2168 2169 TREE_TYPE (decl) = type; 2170 } 2171 2172 /* DECL is a VAR_DECL for a vtable: walk through the entries in the vtable 2174 and mark them as needed. */ 2175 2176 static void 2177 mark_vtable_entries (tree decl, vec<tree> &consteval_vtables) 2178 { 2179 /* It's OK for the vtable to refer to deprecated virtual functions. */ 2180 auto du = make_temp_override (deprecated_state, 2181 UNAVAILABLE_DEPRECATED_SUPPRESS); 2182 2183 bool consteval_seen = false; 2184 2185 for (auto &e: CONSTRUCTOR_ELTS (DECL_INITIAL (decl))) 2186 { 2187 tree fnaddr = e.value; 2188 2189 STRIP_NOPS (fnaddr); 2190 2191 if (TREE_CODE (fnaddr) != ADDR_EXPR 2192 && TREE_CODE (fnaddr) != FDESC_EXPR) 2193 /* This entry is an offset: a virtual base class offset, a 2194 virtual call offset, an RTTI offset, etc. */ 2195 continue; 2196 2197 tree fn = TREE_OPERAND (fnaddr, 0); 2198 if (TREE_CODE (fn) == FUNCTION_DECL && DECL_IMMEDIATE_FUNCTION_P (fn)) 2199 { 2200 if (!consteval_seen) 2201 { 2202 consteval_seen = true; 2203 consteval_vtables.safe_push (decl); 2204 } 2205 continue; 2206 } 2207 TREE_ADDRESSABLE (fn) = 1; 2208 /* When we don't have vcall offsets, we output thunks whenever 2209 we output the vtables that contain them. With vcall offsets, 2210 we know all the thunks we'll need when we emit a virtual 2211 function, so we emit the thunks there instead. */ 2212 if (DECL_THUNK_P (fn)) 2213 use_thunk (fn, /*emit_p=*/0); 2214 /* Set the location, as marking the function could cause 2215 instantiation. We do not need to preserve the incoming 2216 location, as we're called from c_parse_final_cleanups, which 2217 takes care of that. */ 2218 input_location = DECL_SOURCE_LOCATION (fn); 2219 mark_used (fn); 2220 } 2221 } 2222 2223 /* Replace any consteval functions in vtables with null pointers. */ 2224 2225 static void 2226 clear_consteval_vfns (vec<tree> &consteval_vtables) 2227 { 2228 for (tree vtable : consteval_vtables) 2229 for (constructor_elt &elt : CONSTRUCTOR_ELTS (DECL_INITIAL (vtable))) 2230 { 2231 tree fn = cp_get_fndecl_from_callee (elt.value, /*fold*/false); 2232 if (fn && DECL_IMMEDIATE_FUNCTION_P (fn)) 2233 elt.value = build_zero_cst (vtable_entry_type); 2234 } 2235 } 2236 2237 /* Adjust the TLS model on variable DECL if need be, typically after 2238 the linkage of DECL has been modified. */ 2239 2240 static void 2241 adjust_var_decl_tls_model (tree decl) 2242 { 2243 if (CP_DECL_THREAD_LOCAL_P (decl) 2244 && !lookup_attribute ("tls_model", DECL_ATTRIBUTES (decl))) 2245 set_decl_tls_model (decl, decl_default_tls_model (decl)); 2246 } 2247 2248 /* Set DECL up to have the closest approximation of "initialized common" 2249 linkage available. */ 2250 2251 void 2252 comdat_linkage (tree decl) 2253 { 2254 if (flag_weak) 2255 { 2256 make_decl_one_only (decl, cxx_comdat_group (decl)); 2257 if (HAVE_COMDAT_GROUP && flag_contracts && DECL_CONTRACTS (decl)) 2258 { 2259 symtab_node *n = symtab_node::get (decl); 2260 if (tree pre = DECL_PRE_FN (decl)) 2261 cgraph_node::get_create (pre)->add_to_same_comdat_group (n); 2262 if (tree post = DECL_POST_FN (decl)) 2263 cgraph_node::get_create (post)->add_to_same_comdat_group (n); 2264 } 2265 } 2266 else if (TREE_CODE (decl) == FUNCTION_DECL 2267 || (VAR_P (decl) && DECL_ARTIFICIAL (decl))) 2268 /* We can just emit function and compiler-generated variables 2269 statically; having multiple copies is (for the most part) only 2270 a waste of space. 2271 2272 There are two correctness issues, however: the address of a 2273 template instantiation with external linkage should be the 2274 same, independent of what translation unit asks for the 2275 address, and this will not hold when we emit multiple copies of 2276 the function. However, there's little else we can do. 2277 2278 Also, by default, the typeinfo implementation assumes that 2279 there will be only one copy of the string used as the name for 2280 each type. Therefore, if weak symbols are unavailable, the 2281 run-time library should perform a more conservative check; it 2282 should perform a string comparison, rather than an address 2283 comparison. */ 2284 TREE_PUBLIC (decl) = 0; 2285 else 2286 { 2287 /* Static data member template instantiations, however, cannot 2288 have multiple copies. */ 2289 if (DECL_INITIAL (decl) == 0 2290 || DECL_INITIAL (decl) == error_mark_node) 2291 DECL_COMMON (decl) = 1; 2292 else if (EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl))) 2293 { 2294 DECL_COMMON (decl) = 1; 2295 DECL_INITIAL (decl) = error_mark_node; 2296 } 2297 else if (!DECL_EXPLICIT_INSTANTIATION (decl)) 2298 { 2299 /* We can't do anything useful; leave vars for explicit 2300 instantiation. */ 2301 DECL_EXTERNAL (decl) = 1; 2302 DECL_NOT_REALLY_EXTERN (decl) = 0; 2303 } 2304 } 2305 2306 if (TREE_PUBLIC (decl)) 2307 DECL_COMDAT (decl) = 1; 2308 2309 if (VAR_P (decl)) 2310 adjust_var_decl_tls_model (decl); 2311 } 2312 2313 /* For win32 we also want to put explicit instantiations in 2314 linkonce sections, so that they will be merged with implicit 2315 instantiations; otherwise we get duplicate symbol errors. 2316 For Darwin we do not want explicit instantiations to be 2317 linkonce. */ 2318 2319 void 2320 maybe_make_one_only (tree decl) 2321 { 2322 /* We used to say that this was not necessary on targets that support weak 2323 symbols, because the implicit instantiations will defer to the explicit 2324 one. However, that's not actually the case in SVR4; a strong definition 2325 after a weak one is an error. Also, not making explicit 2326 instantiations one_only means that we can end up with two copies of 2327 some template instantiations. */ 2328 if (! flag_weak) 2329 return; 2330 2331 /* We can't set DECL_COMDAT on functions, or cp_finish_file will think 2332 we can get away with not emitting them if they aren't used. We need 2333 to for variables so that cp_finish_decl will update their linkage, 2334 because their DECL_INITIAL may not have been set properly yet. */ 2335 2336 if (!TARGET_WEAK_NOT_IN_ARCHIVE_TOC 2337 || (! DECL_EXPLICIT_INSTANTIATION (decl) 2338 && ! DECL_TEMPLATE_SPECIALIZATION (decl))) 2339 { 2340 make_decl_one_only (decl, cxx_comdat_group (decl)); 2341 2342 if (VAR_P (decl)) 2343 { 2344 varpool_node *node = varpool_node::get_create (decl); 2345 DECL_COMDAT (decl) = 1; 2346 /* Mark it needed so we don't forget to emit it. */ 2347 node->forced_by_abi = true; 2348 TREE_USED (decl) = 1; 2349 2350 adjust_var_decl_tls_model (decl); 2351 } 2352 } 2353 } 2354 2355 /* Returns true iff DECL, a FUNCTION_DECL or VAR_DECL, has vague linkage. 2356 This predicate will give the right answer during parsing of the 2357 function, which other tests may not. */ 2358 2359 bool 2360 vague_linkage_p (tree decl) 2361 { 2362 if (!TREE_PUBLIC (decl)) 2363 { 2364 /* maybe_thunk_body clears TREE_PUBLIC and DECL_ABSTRACT_P on the 2365 maybe-in-charge 'tor variants; in that case we need to check one of 2366 the "clones" for the real linkage. But only in that case; before 2367 maybe_clone_body we haven't yet copied the linkage to the clones. */ 2368 if (DECL_MAYBE_IN_CHARGE_CDTOR_P (decl) 2369 && !DECL_ABSTRACT_P (decl) 2370 && DECL_CHAIN (decl) 2371 && DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl))) 2372 return vague_linkage_p (DECL_CHAIN (decl)); 2373 2374 gcc_checking_assert (!DECL_COMDAT (decl)); 2375 return false; 2376 } 2377 /* Unfortunately, import_export_decl has not always been called 2378 before the function is processed, so we cannot simply check 2379 DECL_COMDAT. */ 2380 if (DECL_COMDAT (decl) 2381 || (TREE_CODE (decl) == FUNCTION_DECL 2382 && DECL_DECLARED_INLINE_P (decl)) 2383 || (DECL_LANG_SPECIFIC (decl) 2384 && DECL_TEMPLATE_INSTANTIATION (decl)) 2385 || (VAR_P (decl) && DECL_INLINE_VAR_P (decl))) 2386 return true; 2387 else if (DECL_FUNCTION_SCOPE_P (decl)) 2388 /* A local static in an inline effectively has vague linkage. */ 2389 return (TREE_STATIC (decl) 2390 && vague_linkage_p (DECL_CONTEXT (decl))); 2391 else 2392 return false; 2393 } 2394 2395 /* Determine whether or not we want to specifically import or export CTYPE, 2396 using various heuristics. */ 2397 2398 static void 2399 import_export_class (tree ctype) 2400 { 2401 /* -1 for imported, 1 for exported. */ 2402 int import_export = 0; 2403 2404 /* It only makes sense to call this function at EOF. The reason is 2405 that this function looks at whether or not the first non-inline 2406 non-abstract virtual member function has been defined in this 2407 translation unit. But, we can't possibly know that until we've 2408 seen the entire translation unit. */ 2409 gcc_assert (at_eof); 2410 2411 if (CLASSTYPE_INTERFACE_KNOWN (ctype)) 2412 return; 2413 2414 /* If MULTIPLE_SYMBOL_SPACES is set and we saw a #pragma interface, 2415 we will have CLASSTYPE_INTERFACE_ONLY set but not 2416 CLASSTYPE_INTERFACE_KNOWN. In that case, we don't want to use this 2417 heuristic because someone will supply a #pragma implementation 2418 elsewhere, and deducing it here would produce a conflict. */ 2419 if (CLASSTYPE_INTERFACE_ONLY (ctype)) 2420 return; 2421 2422 if (lookup_attribute ("dllimport", TYPE_ATTRIBUTES (ctype))) 2423 import_export = -1; 2424 else if (lookup_attribute ("dllexport", TYPE_ATTRIBUTES (ctype))) 2425 import_export = 1; 2426 else if (CLASSTYPE_IMPLICIT_INSTANTIATION (ctype) 2427 && !flag_implicit_templates) 2428 /* For a template class, without -fimplicit-templates, check the 2429 repository. If the virtual table is assigned to this 2430 translation unit, then export the class; otherwise, import 2431 it. */ 2432 import_export = -1; 2433 else if (TYPE_POLYMORPHIC_P (ctype)) 2434 { 2435 /* The ABI specifies that the virtual table and associated 2436 information are emitted with the key method, if any. */ 2437 tree method = CLASSTYPE_KEY_METHOD (ctype); 2438 /* If weak symbol support is not available, then we must be 2439 careful not to emit the vtable when the key function is 2440 inline. An inline function can be defined in multiple 2441 translation units. If we were to emit the vtable in each 2442 translation unit containing a definition, we would get 2443 multiple definition errors at link-time. */ 2444 if (method && (flag_weak || ! DECL_DECLARED_INLINE_P (method))) 2445 import_export = (DECL_REALLY_EXTERN (method) ? -1 : 1); 2446 } 2447 2448 /* When MULTIPLE_SYMBOL_SPACES is set, we cannot count on seeing 2449 a definition anywhere else. */ 2450 if (MULTIPLE_SYMBOL_SPACES && import_export == -1) 2451 import_export = 0; 2452 2453 /* Allow back ends the chance to overrule the decision. */ 2454 if (targetm.cxx.import_export_class) 2455 import_export = targetm.cxx.import_export_class (ctype, import_export); 2456 2457 if (import_export) 2458 { 2459 SET_CLASSTYPE_INTERFACE_KNOWN (ctype); 2460 CLASSTYPE_INTERFACE_ONLY (ctype) = (import_export < 0); 2461 } 2462 } 2463 2464 /* Return true if VAR has already been provided to the back end; in that 2465 case VAR should not be modified further by the front end. */ 2466 static bool 2467 var_finalized_p (tree var) 2468 { 2469 return varpool_node::get_create (var)->definition; 2470 } 2471 2472 /* DECL is a VAR_DECL or FUNCTION_DECL which, for whatever reason, 2473 must be emitted in this translation unit. Mark it as such. */ 2474 2475 void 2476 mark_needed (tree decl) 2477 { 2478 TREE_USED (decl) = 1; 2479 if (TREE_CODE (decl) == FUNCTION_DECL) 2480 { 2481 /* Extern inline functions don't become needed when referenced. 2482 If we know a method will be emitted in other TU and no new 2483 functions can be marked reachable, just use the external 2484 definition. */ 2485 struct cgraph_node *node = cgraph_node::get_create (decl); 2486 node->forced_by_abi = true; 2487 2488 /* #pragma interface can call mark_needed for 2489 maybe-in-charge 'tors; mark the clones as well. */ 2490 tree clone; 2491 FOR_EACH_CLONE (clone, decl) 2492 mark_needed (clone); 2493 } 2494 else if (VAR_P (decl)) 2495 { 2496 varpool_node *node = varpool_node::get_create (decl); 2497 /* C++ frontend use mark_decl_references to force COMDAT variables 2498 to be output that might appear dead otherwise. */ 2499 node->forced_by_abi = true; 2500 } 2501 } 2502 2503 /* DECL is either a FUNCTION_DECL or a VAR_DECL. This function 2504 returns true if a definition of this entity should be provided in 2505 this object file. Callers use this function to determine whether 2506 or not to let the back end know that a definition of DECL is 2507 available in this translation unit. */ 2508 2509 bool 2510 decl_needed_p (tree decl) 2511 { 2512 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl)); 2513 /* This function should only be called at the end of the translation 2514 unit. We cannot be sure of whether or not something will be 2515 COMDAT until that point. */ 2516 gcc_assert (at_eof); 2517 2518 /* All entities with external linkage that are not COMDAT/EXTERN should be 2519 emitted; they may be referred to from other object files. */ 2520 if (TREE_PUBLIC (decl) && !DECL_COMDAT (decl) && !DECL_REALLY_EXTERN (decl)) 2521 return true; 2522 2523 /* Functions marked "dllexport" must be emitted so that they are 2524 visible to other DLLs. */ 2525 if (flag_keep_inline_dllexport 2526 && lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl))) 2527 return true; 2528 2529 /* When not optimizing, do not bother to produce definitions for extern 2530 symbols. */ 2531 if (DECL_REALLY_EXTERN (decl) 2532 && ((TREE_CODE (decl) != FUNCTION_DECL 2533 && !optimize) 2534 || (TREE_CODE (decl) == FUNCTION_DECL 2535 && !opt_for_fn (decl, optimize))) 2536 && !lookup_attribute ("always_inline", decl)) 2537 return false; 2538 2539 /* If this entity was used, let the back end see it; it will decide 2540 whether or not to emit it into the object file. */ 2541 if (TREE_USED (decl)) 2542 return true; 2543 2544 /* Virtual functions might be needed for devirtualization. */ 2545 if (flag_devirtualize 2546 && TREE_CODE (decl) == FUNCTION_DECL 2547 && DECL_VIRTUAL_P (decl)) 2548 return true; 2549 2550 /* Otherwise, DECL does not need to be emitted -- yet. A subsequent 2551 reference to DECL might cause it to be emitted later. */ 2552 return false; 2553 } 2554 2555 /* If necessary, write out the vtables for the dynamic class CTYPE. 2556 Returns true if any vtables were emitted. */ 2557 2558 static bool 2559 maybe_emit_vtables (tree ctype, vec<tree> &consteval_vtables) 2560 { 2561 tree vtbl; 2562 tree primary_vtbl; 2563 int needed = 0; 2564 varpool_node *current = NULL, *last = NULL; 2565 2566 /* If the vtables for this class have already been emitted there is 2567 nothing more to do. */ 2568 primary_vtbl = CLASSTYPE_VTABLES (ctype); 2569 if (var_finalized_p (primary_vtbl)) 2570 return false; 2571 /* Ignore dummy vtables made by get_vtable_decl. */ 2572 if (TREE_TYPE (primary_vtbl) == void_type_node) 2573 return false; 2574 2575 /* On some targets, we cannot determine the key method until the end 2576 of the translation unit -- which is when this function is 2577 called. */ 2578 if (!targetm.cxx.key_method_may_be_inline ()) 2579 determine_key_method (ctype); 2580 2581 /* See if any of the vtables are needed. */ 2582 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl)) 2583 { 2584 import_export_decl (vtbl); 2585 if (DECL_NOT_REALLY_EXTERN (vtbl) && decl_needed_p (vtbl)) 2586 needed = 1; 2587 } 2588 if (!needed) 2589 { 2590 /* If the references to this class' vtables are optimized away, 2591 still emit the appropriate debugging information. See 2592 dfs_debug_mark. */ 2593 if (DECL_COMDAT (primary_vtbl) 2594 && CLASSTYPE_DEBUG_REQUESTED (ctype)) 2595 note_debug_info_needed (ctype); 2596 return false; 2597 } 2598 2599 /* The ABI requires that we emit all of the vtables if we emit any 2600 of them. */ 2601 for (vtbl = CLASSTYPE_VTABLES (ctype); vtbl; vtbl = DECL_CHAIN (vtbl)) 2602 { 2603 /* Mark entities references from the virtual table as used. */ 2604 mark_vtable_entries (vtbl, consteval_vtables); 2605 2606 if (TREE_TYPE (DECL_INITIAL (vtbl)) == 0) 2607 { 2608 vec<tree, va_gc> *cleanups = NULL; 2609 tree expr = store_init_value (vtbl, DECL_INITIAL (vtbl), &cleanups, 2610 LOOKUP_NORMAL); 2611 2612 /* It had better be all done at compile-time. */ 2613 gcc_assert (!expr && !cleanups); 2614 } 2615 2616 /* Write it out. */ 2617 DECL_EXTERNAL (vtbl) = 0; 2618 rest_of_decl_compilation (vtbl, 1, 1); 2619 2620 /* Because we're only doing syntax-checking, we'll never end up 2621 actually marking the variable as written. */ 2622 if (flag_syntax_only) 2623 TREE_ASM_WRITTEN (vtbl) = 1; 2624 else if (DECL_ONE_ONLY (vtbl)) 2625 { 2626 current = varpool_node::get_create (vtbl); 2627 if (last) 2628 current->add_to_same_comdat_group (last); 2629 last = current; 2630 } 2631 } 2632 2633 /* For abstract classes, the destructor has been removed from the 2634 vtable (in class.cc's build_vtbl_initializer). For a compiler- 2635 generated destructor, it hence might not have been generated in 2636 this translation unit - and with '#pragma interface' it might 2637 never get generated. */ 2638 if (CLASSTYPE_PURE_VIRTUALS (ctype) 2639 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (ctype) 2640 && !CLASSTYPE_LAZY_DESTRUCTOR (ctype) 2641 && DECL_DEFAULTED_IN_CLASS_P (CLASSTYPE_DESTRUCTOR (ctype))) 2642 note_vague_linkage_fn (CLASSTYPE_DESTRUCTOR (ctype)); 2643 2644 /* Since we're writing out the vtable here, also write the debug 2645 info. */ 2646 note_debug_info_needed (ctype); 2647 2648 return true; 2649 } 2650 2651 /* A special return value from type_visibility meaning internal 2652 linkage. */ 2653 2654 enum { VISIBILITY_ANON = VISIBILITY_INTERNAL+1 }; 2655 2656 static int expr_visibility (tree); 2657 static int type_visibility (tree); 2658 2659 /* walk_tree helper function for type_visibility. */ 2660 2661 static tree 2662 min_vis_r (tree *tp, int *walk_subtrees, void *data) 2663 { 2664 int *vis_p = (int *)data; 2665 int this_vis = VISIBILITY_DEFAULT; 2666 if (! TYPE_P (*tp)) 2667 *walk_subtrees = 0; 2668 else if (OVERLOAD_TYPE_P (*tp) 2669 && !TREE_PUBLIC (TYPE_MAIN_DECL (*tp))) 2670 { 2671 this_vis = VISIBILITY_ANON; 2672 *walk_subtrees = 0; 2673 } 2674 else if (CLASS_TYPE_P (*tp)) 2675 { 2676 this_vis = CLASSTYPE_VISIBILITY (*tp); 2677 *walk_subtrees = 0; 2678 } 2679 else if (TREE_CODE (*tp) == ARRAY_TYPE 2680 && uses_template_parms (TYPE_DOMAIN (*tp))) 2681 this_vis = expr_visibility (TYPE_MAX_VALUE (TYPE_DOMAIN (*tp))); 2682 2683 if (this_vis > *vis_p) 2684 *vis_p = this_vis; 2685 2686 /* Tell cp_walk_subtrees to look through typedefs. */ 2687 if (*walk_subtrees == 1) 2688 *walk_subtrees = 2; 2689 2690 return NULL; 2691 } 2692 2693 /* walk_tree helper function for expr_visibility. */ 2694 2695 static tree 2696 min_vis_expr_r (tree *tp, int */*walk_subtrees*/, void *data) 2697 { 2698 int *vis_p = (int *)data; 2699 int tpvis = VISIBILITY_DEFAULT; 2700 2701 tree t = *tp; 2702 if (TREE_CODE (t) == PTRMEM_CST) 2703 t = PTRMEM_CST_MEMBER (t); 2704 switch (TREE_CODE (t)) 2705 { 2706 case CAST_EXPR: 2707 case IMPLICIT_CONV_EXPR: 2708 case STATIC_CAST_EXPR: 2709 case REINTERPRET_CAST_EXPR: 2710 case CONST_CAST_EXPR: 2711 case DYNAMIC_CAST_EXPR: 2712 case NEW_EXPR: 2713 case CONSTRUCTOR: 2714 case LAMBDA_EXPR: 2715 tpvis = type_visibility (TREE_TYPE (t)); 2716 break; 2717 2718 case ADDR_EXPR: 2719 t = TREE_OPERAND (t, 0); 2720 if (VAR_P (t)) 2721 /* If a variable has its address taken, the lvalue-rvalue conversion is 2722 not applied, so skip that case. */ 2723 goto addressable; 2724 break; 2725 2726 case TEMPLATE_DECL: 2727 if (DECL_ALIAS_TEMPLATE_P (t) || standard_concept_p (t)) 2728 /* FIXME: We don't maintain TREE_PUBLIC / DECL_VISIBILITY for 2729 alias templates so we can't trust it here (PR107906). Ditto 2730 for concepts. */ 2731 break; 2732 t = DECL_TEMPLATE_RESULT (t); 2733 /* Fall through. */ 2734 case VAR_DECL: 2735 case FUNCTION_DECL: 2736 if (decl_constant_var_p (t)) 2737 /* The ODR allows definitions in different TUs to refer to distinct 2738 constant variables with internal or no linkage, so such a reference 2739 shouldn't affect visibility if the lvalue-rvalue conversion is 2740 applied (PR110323). We still want to restrict visibility according 2741 to the type of the declaration however. */ 2742 { 2743 tpvis = type_visibility (TREE_TYPE (t)); 2744 break; 2745 } 2746 addressable: 2747 if (! TREE_PUBLIC (t)) 2748 tpvis = VISIBILITY_ANON; 2749 else 2750 tpvis = DECL_VISIBILITY (t); 2751 break; 2752 2753 case FIELD_DECL: 2754 tpvis = type_visibility (DECL_CONTEXT (t)); 2755 break; 2756 2757 default: 2758 break; 2759 } 2760 2761 if (tpvis > *vis_p) 2762 *vis_p = tpvis; 2763 2764 return NULL_TREE; 2765 } 2766 2767 /* Returns the visibility of TYPE, which is the minimum visibility of its 2768 component types. */ 2769 2770 static int 2771 type_visibility (tree type) 2772 { 2773 int vis = VISIBILITY_DEFAULT; 2774 cp_walk_tree_without_duplicates (&type, min_vis_r, &vis); 2775 return vis; 2776 } 2777 2778 /* Returns the visibility of an expression EXPR that appears in the signature 2779 of a function template, which is the minimum visibility of names that appear 2780 in its mangling. */ 2781 2782 static int 2783 expr_visibility (tree expr) 2784 { 2785 int vis = VISIBILITY_DEFAULT; 2786 cp_walk_tree_without_duplicates (&expr, min_vis_expr_r, &vis); 2787 return vis; 2788 } 2789 2790 /* Limit the visibility of DECL to VISIBILITY, if not explicitly 2791 specified (or if VISIBILITY is static). If TMPL is true, this 2792 constraint is for a template argument, and takes precedence 2793 over explicitly-specified visibility on the template. */ 2794 2795 static void 2796 constrain_visibility (tree decl, int visibility, bool tmpl) 2797 { 2798 if (visibility == VISIBILITY_ANON) 2799 { 2800 /* extern "C" declarations aren't affected by the anonymous 2801 namespace. */ 2802 if (!DECL_EXTERN_C_P (decl)) 2803 { 2804 TREE_PUBLIC (decl) = 0; 2805 DECL_WEAK (decl) = 0; 2806 DECL_COMMON (decl) = 0; 2807 DECL_COMDAT (decl) = false; 2808 if (VAR_OR_FUNCTION_DECL_P (decl)) 2809 { 2810 struct symtab_node *snode = symtab_node::get (decl); 2811 2812 if (snode) 2813 snode->set_comdat_group (NULL); 2814 } 2815 DECL_INTERFACE_KNOWN (decl) = 1; 2816 if (DECL_LANG_SPECIFIC (decl)) 2817 DECL_NOT_REALLY_EXTERN (decl) = 1; 2818 } 2819 } 2820 else if (visibility > DECL_VISIBILITY (decl) 2821 && (tmpl || !DECL_VISIBILITY_SPECIFIED (decl))) 2822 { 2823 DECL_VISIBILITY (decl) = (enum symbol_visibility) visibility; 2824 /* This visibility was not specified. */ 2825 DECL_VISIBILITY_SPECIFIED (decl) = false; 2826 } 2827 } 2828 2829 /* Constrain the visibility of DECL based on the visibility of its template 2830 arguments. */ 2831 2832 static void 2833 constrain_visibility_for_template (tree decl, tree targs) 2834 { 2835 /* If this is a template instantiation, check the innermost 2836 template args for visibility constraints. The outer template 2837 args are covered by the class check. */ 2838 tree args = INNERMOST_TEMPLATE_ARGS (targs); 2839 int i; 2840 for (i = TREE_VEC_LENGTH (args); i > 0; --i) 2841 { 2842 int vis = 0; 2843 2844 tree arg = TREE_VEC_ELT (args, i-1); 2845 if (TYPE_P (arg)) 2846 vis = type_visibility (arg); 2847 else 2848 vis = expr_visibility (arg); 2849 if (vis) 2850 constrain_visibility (decl, vis, true); 2851 } 2852 } 2853 2854 /* Like c_determine_visibility, but with additional C++-specific 2855 behavior. 2856 2857 Function-scope entities can rely on the function's visibility because 2858 it is set in start_preparsed_function. 2859 2860 Class-scope entities cannot rely on the class's visibility until the end 2861 of the enclosing class definition. 2862 2863 Note that because namespaces have multiple independent definitions, 2864 namespace visibility is handled elsewhere using the #pragma visibility 2865 machinery rather than by decorating the namespace declaration. 2866 2867 The goal is for constraints from the type to give a diagnostic, and 2868 other constraints to be applied silently. */ 2869 2870 void 2871 determine_visibility (tree decl) 2872 { 2873 /* Remember that all decls get VISIBILITY_DEFAULT when built. */ 2874 2875 /* Only relevant for names with external linkage. */ 2876 if (!TREE_PUBLIC (decl)) 2877 return; 2878 2879 /* Cloned constructors and destructors get the same visibility as 2880 the underlying function. That should be set up in 2881 maybe_clone_body. */ 2882 gcc_assert (!DECL_CLONED_FUNCTION_P (decl)); 2883 2884 bool orig_visibility_specified = DECL_VISIBILITY_SPECIFIED (decl); 2885 enum symbol_visibility orig_visibility = DECL_VISIBILITY (decl); 2886 2887 /* The decl may be a template instantiation, which could influence 2888 visibilty. */ 2889 tree template_decl = NULL_TREE; 2890 if (TREE_CODE (decl) == TYPE_DECL) 2891 { 2892 if (CLASS_TYPE_P (TREE_TYPE (decl))) 2893 { 2894 if (CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl))) 2895 template_decl = decl; 2896 } 2897 else if (TYPE_TEMPLATE_INFO (TREE_TYPE (decl))) 2898 template_decl = decl; 2899 } 2900 else if (DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl)) 2901 template_decl = decl; 2902 2903 if (TREE_CODE (decl) == TYPE_DECL 2904 && LAMBDA_TYPE_P (TREE_TYPE (decl)) 2905 && CLASSTYPE_LAMBDA_EXPR (TREE_TYPE (decl)) != error_mark_node) 2906 if (tree extra = LAMBDA_TYPE_EXTRA_SCOPE (TREE_TYPE (decl))) 2907 { 2908 /* The lambda's visibility is limited by that of its extra 2909 scope. */ 2910 int vis = 0; 2911 if (TYPE_P (extra)) 2912 vis = type_visibility (extra); 2913 else 2914 vis = expr_visibility (extra); 2915 constrain_visibility (decl, vis, false); 2916 } 2917 2918 /* If DECL is a member of a class, visibility specifiers on the 2919 class can influence the visibility of the DECL. */ 2920 tree class_type = NULL_TREE; 2921 if (DECL_CLASS_SCOPE_P (decl)) 2922 class_type = DECL_CONTEXT (decl); 2923 else 2924 { 2925 /* Not a class member. */ 2926 2927 /* Virtual tables have DECL_CONTEXT set to their associated class, 2928 so they are automatically handled above. */ 2929 gcc_assert (!VAR_P (decl) 2930 || !DECL_VTABLE_OR_VTT_P (decl)); 2931 2932 if (DECL_FUNCTION_SCOPE_P (decl) && ! DECL_VISIBILITY_SPECIFIED (decl)) 2933 { 2934 /* Local statics and classes get the visibility of their 2935 containing function by default, except that 2936 -fvisibility-inlines-hidden doesn't affect them. */ 2937 tree fn = DECL_CONTEXT (decl); 2938 if (DECL_VISIBILITY_SPECIFIED (fn)) 2939 { 2940 DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn); 2941 DECL_VISIBILITY_SPECIFIED (decl) = 2942 DECL_VISIBILITY_SPECIFIED (fn); 2943 } 2944 else 2945 { 2946 if (DECL_CLASS_SCOPE_P (fn)) 2947 determine_visibility_from_class (decl, DECL_CONTEXT (fn)); 2948 else if (determine_hidden_inline (fn)) 2949 { 2950 DECL_VISIBILITY (decl) = default_visibility; 2951 DECL_VISIBILITY_SPECIFIED (decl) = 2952 visibility_options.inpragma; 2953 } 2954 else 2955 { 2956 DECL_VISIBILITY (decl) = DECL_VISIBILITY (fn); 2957 DECL_VISIBILITY_SPECIFIED (decl) = 2958 DECL_VISIBILITY_SPECIFIED (fn); 2959 } 2960 } 2961 2962 /* Local classes in templates have CLASSTYPE_USE_TEMPLATE set, 2963 but have no TEMPLATE_INFO, so don't try to check it. */ 2964 template_decl = NULL_TREE; 2965 } 2966 else if (VAR_P (decl) && DECL_TINFO_P (decl) 2967 && flag_visibility_ms_compat) 2968 { 2969 /* Under -fvisibility-ms-compat, types are visible by default, 2970 even though their contents aren't. */ 2971 tree underlying_type = TREE_TYPE (DECL_NAME (decl)); 2972 int underlying_vis = type_visibility (underlying_type); 2973 if (underlying_vis == VISIBILITY_ANON 2974 || (CLASS_TYPE_P (underlying_type) 2975 && CLASSTYPE_VISIBILITY_SPECIFIED (underlying_type))) 2976 constrain_visibility (decl, underlying_vis, false); 2977 else 2978 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT; 2979 } 2980 else if (VAR_P (decl) && DECL_TINFO_P (decl)) 2981 { 2982 /* tinfo visibility is based on the type it's for. */ 2983 constrain_visibility 2984 (decl, type_visibility (TREE_TYPE (DECL_NAME (decl))), false); 2985 2986 /* Give the target a chance to override the visibility associated 2987 with DECL. */ 2988 if (TREE_PUBLIC (decl) 2989 && !DECL_REALLY_EXTERN (decl) 2990 && CLASS_TYPE_P (TREE_TYPE (DECL_NAME (decl))) 2991 && !CLASSTYPE_VISIBILITY_SPECIFIED (TREE_TYPE (DECL_NAME (decl)))) 2992 targetm.cxx.determine_class_data_visibility (decl); 2993 } 2994 else if (template_decl) 2995 /* Template instantiations and specializations get visibility based 2996 on their template unless they override it with an attribute. */; 2997 else if (! DECL_VISIBILITY_SPECIFIED (decl)) 2998 { 2999 if (determine_hidden_inline (decl)) 3000 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN; 3001 else 3002 { 3003 /* Set default visibility to whatever the user supplied with 3004 #pragma GCC visibility or a namespace visibility attribute. */ 3005 DECL_VISIBILITY (decl) = default_visibility; 3006 DECL_VISIBILITY_SPECIFIED (decl) = visibility_options.inpragma; 3007 } 3008 } 3009 } 3010 3011 if (template_decl) 3012 { 3013 /* If the specialization doesn't specify visibility, use the 3014 visibility from the template. */ 3015 tree tinfo = get_template_info (template_decl); 3016 tree args = TI_ARGS (tinfo); 3017 tree attribs = (TREE_CODE (decl) == TYPE_DECL 3018 ? TYPE_ATTRIBUTES (TREE_TYPE (decl)) 3019 : DECL_ATTRIBUTES (decl)); 3020 tree attr = lookup_attribute ("visibility", attribs); 3021 3022 if (args != error_mark_node) 3023 { 3024 tree pattern = DECL_TEMPLATE_RESULT (TI_TEMPLATE (tinfo)); 3025 3026 if (!DECL_VISIBILITY_SPECIFIED (decl)) 3027 { 3028 if (!attr 3029 && determine_hidden_inline (decl)) 3030 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN; 3031 else 3032 { 3033 DECL_VISIBILITY (decl) = DECL_VISIBILITY (pattern); 3034 DECL_VISIBILITY_SPECIFIED (decl) 3035 = DECL_VISIBILITY_SPECIFIED (pattern); 3036 } 3037 } 3038 3039 if (args 3040 /* Template argument visibility outweighs #pragma or namespace 3041 visibility, but not an explicit attribute. */ 3042 && !attr) 3043 { 3044 int depth = TMPL_ARGS_DEPTH (args); 3045 if (DECL_VISIBILITY_SPECIFIED (decl)) 3046 { 3047 /* A class template member with explicit visibility 3048 overrides the class visibility, so we need to apply 3049 all the levels of template args directly. */ 3050 int i; 3051 for (i = 1; i <= depth; ++i) 3052 { 3053 tree lev = TMPL_ARGS_LEVEL (args, i); 3054 constrain_visibility_for_template (decl, lev); 3055 } 3056 } 3057 else if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))) 3058 /* Limit visibility based on its template arguments. */ 3059 constrain_visibility_for_template (decl, args); 3060 } 3061 } 3062 } 3063 3064 if (class_type) 3065 determine_visibility_from_class (decl, class_type); 3066 3067 if (decl_internal_context_p (decl)) 3068 /* Names in an anonymous namespace get internal linkage. */ 3069 constrain_visibility (decl, VISIBILITY_ANON, false); 3070 else if (TREE_CODE (decl) != TYPE_DECL) 3071 { 3072 /* Propagate anonymity from type to decl. */ 3073 int tvis = type_visibility (TREE_TYPE (decl)); 3074 if (tvis == VISIBILITY_ANON 3075 || ! DECL_VISIBILITY_SPECIFIED (decl)) 3076 constrain_visibility (decl, tvis, false); 3077 } 3078 else if (no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/true)) 3079 /* DR 757: A type without linkage shall not be used as the type of a 3080 variable or function with linkage, unless 3081 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or 3082 o the variable or function is not used (3.2 [basic.def.odr]) or is 3083 defined in the same translation unit. 3084 3085 Since non-extern "C" decls need to be defined in the same 3086 translation unit, we can make the type internal. */ 3087 constrain_visibility (decl, VISIBILITY_ANON, false); 3088 3089 /* If visibility changed and DECL already has DECL_RTL, ensure 3090 symbol flags are updated. */ 3091 if ((DECL_VISIBILITY (decl) != orig_visibility 3092 || DECL_VISIBILITY_SPECIFIED (decl) != orig_visibility_specified) 3093 && ((VAR_P (decl) && TREE_STATIC (decl)) 3094 || TREE_CODE (decl) == FUNCTION_DECL) 3095 && DECL_RTL_SET_P (decl)) 3096 make_decl_rtl (decl); 3097 } 3098 3099 /* By default, static data members and function members receive 3100 the visibility of their containing class. */ 3101 3102 static void 3103 determine_visibility_from_class (tree decl, tree class_type) 3104 { 3105 if (DECL_VISIBILITY_SPECIFIED (decl)) 3106 return; 3107 3108 if (determine_hidden_inline (decl)) 3109 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN; 3110 else 3111 { 3112 /* Default to the class visibility. */ 3113 DECL_VISIBILITY (decl) = CLASSTYPE_VISIBILITY (class_type); 3114 DECL_VISIBILITY_SPECIFIED (decl) 3115 = CLASSTYPE_VISIBILITY_SPECIFIED (class_type); 3116 } 3117 3118 /* Give the target a chance to override the visibility associated 3119 with DECL. */ 3120 if (VAR_P (decl) 3121 && TREE_PUBLIC (decl) 3122 && (DECL_TINFO_P (decl) || DECL_VTABLE_OR_VTT_P (decl)) 3123 && !DECL_REALLY_EXTERN (decl) 3124 && !CLASSTYPE_VISIBILITY_SPECIFIED (class_type)) 3125 targetm.cxx.determine_class_data_visibility (decl); 3126 } 3127 3128 /* Returns true iff DECL is an inline that should get hidden visibility 3129 because of -fvisibility-inlines-hidden. */ 3130 3131 static bool 3132 determine_hidden_inline (tree decl) 3133 { 3134 return (visibility_options.inlines_hidden 3135 /* Don't do this for inline templates; specializations might not be 3136 inline, and we don't want them to inherit the hidden 3137 visibility. We'll set it here for all inline instantiations. */ 3138 && !processing_template_decl 3139 && TREE_CODE (decl) == FUNCTION_DECL 3140 && DECL_DECLARED_INLINE_P (decl) 3141 && (! DECL_LANG_SPECIFIC (decl) 3142 || ! DECL_EXPLICIT_INSTANTIATION (decl))); 3143 } 3144 3145 /* Constrain the visibility of a class TYPE based on the visibility of its 3146 field types. Warn if any fields require lesser visibility. */ 3147 3148 void 3149 constrain_class_visibility (tree type) 3150 { 3151 tree binfo; 3152 tree t; 3153 int i; 3154 3155 int vis = type_visibility (type); 3156 3157 if (vis == VISIBILITY_ANON 3158 || DECL_IN_SYSTEM_HEADER (TYPE_MAIN_DECL (type))) 3159 return; 3160 3161 /* Don't warn about visibility if the class has explicit visibility. */ 3162 if (CLASSTYPE_VISIBILITY_SPECIFIED (type)) 3163 vis = VISIBILITY_INTERNAL; 3164 3165 for (t = TYPE_FIELDS (type); t; t = DECL_CHAIN (t)) 3166 if (TREE_CODE (t) == FIELD_DECL && TREE_TYPE (t) != error_mark_node 3167 && !DECL_ARTIFICIAL (t)) 3168 { 3169 tree ftype = strip_pointer_or_array_types (TREE_TYPE (t)); 3170 int subvis = type_visibility (ftype); 3171 3172 if (subvis == VISIBILITY_ANON) 3173 { 3174 if (!in_main_input_context()) 3175 { 3176 tree nlt = no_linkage_check (ftype, /*relaxed_p=*/false); 3177 if (nlt) 3178 { 3179 if (same_type_p (TREE_TYPE (t), nlt)) 3180 warning (OPT_Wsubobject_linkage, "\ 3181 %qT has a field %q#D whose type has no linkage", 3182 type, t); 3183 else 3184 warning (OPT_Wsubobject_linkage, "\ 3185 %qT has a field %qD whose type depends on the type %qT which has no linkage", 3186 type, t, nlt); 3187 } 3188 else if (cxx_dialect > cxx98 3189 && !decl_anon_ns_mem_p (ftype)) 3190 warning (OPT_Wsubobject_linkage, "\ 3191 %qT has a field %q#D whose type has internal linkage", 3192 type, t); 3193 else // In C++98 this can only happen with unnamed namespaces. 3194 warning (OPT_Wsubobject_linkage, "\ 3195 %qT has a field %q#D whose type uses the anonymous namespace", 3196 type, t); 3197 } 3198 } 3199 else if (MAYBE_CLASS_TYPE_P (ftype) 3200 && vis < VISIBILITY_HIDDEN 3201 && subvis >= VISIBILITY_HIDDEN) 3202 warning (OPT_Wattributes, "\ 3203 %qT declared with greater visibility than the type of its field %qD", 3204 type, t); 3205 } 3206 3207 binfo = TYPE_BINFO (type); 3208 for (i = 0; BINFO_BASE_ITERATE (binfo, i, t); ++i) 3209 { 3210 tree btype = BINFO_TYPE (t); 3211 int subvis = type_visibility (btype); 3212 3213 if (subvis == VISIBILITY_ANON) 3214 { 3215 if (!in_main_input_context()) 3216 { 3217 tree nlt = no_linkage_check (btype, /*relaxed_p=*/false); 3218 if (nlt) 3219 { 3220 if (same_type_p (btype, nlt)) 3221 warning (OPT_Wsubobject_linkage, "\ 3222 %qT has a base %qT which has no linkage", 3223 type, btype); 3224 else 3225 warning (OPT_Wsubobject_linkage, "\ 3226 %qT has a base %qT which depends on the type %qT which has no linkage", 3227 type, btype, nlt); 3228 } 3229 else if (cxx_dialect > cxx98 3230 && !decl_anon_ns_mem_p (btype)) 3231 warning (OPT_Wsubobject_linkage, "\ 3232 %qT has a base %qT which has internal linkage", 3233 type, btype); 3234 else // In C++98 this can only happen with unnamed namespaces. 3235 warning (OPT_Wsubobject_linkage, "\ 3236 %qT has a base %qT which uses the anonymous namespace", 3237 type, btype); 3238 } 3239 } 3240 else if (vis < VISIBILITY_HIDDEN 3241 && subvis >= VISIBILITY_HIDDEN) 3242 warning (OPT_Wattributes, "\ 3243 %qT declared with greater visibility than its base %qT", 3244 type, TREE_TYPE (t)); 3245 } 3246 } 3247 3248 /* Functions for adjusting the visibility of a tagged type and its nested 3249 types and declarations when it gets a name for linkage purposes from a 3250 typedef. */ 3251 // FIXME: It is now a DR for such a class type to contain anything 3252 // other than C. So at minium most of this can probably be deleted. 3253 3254 /* First reset the visibility of all the types. */ 3255 3256 static void 3257 reset_type_linkage_1 (tree type) 3258 { 3259 set_linkage_according_to_type (type, TYPE_MAIN_DECL (type)); 3260 if (CLASS_TYPE_P (type)) 3261 for (tree member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member)) 3262 if (DECL_IMPLICIT_TYPEDEF_P (member)) 3263 reset_type_linkage_1 (TREE_TYPE (member)); 3264 } 3265 3266 /* Then reset the visibility of any static data members or member 3267 functions that use those types. */ 3268 3269 static void 3270 reset_decl_linkage (tree decl) 3271 { 3272 if (TREE_PUBLIC (decl)) 3273 return; 3274 if (DECL_CLONED_FUNCTION_P (decl)) 3275 return; 3276 TREE_PUBLIC (decl) = true; 3277 DECL_INTERFACE_KNOWN (decl) = false; 3278 determine_visibility (decl); 3279 tentative_decl_linkage (decl); 3280 } 3281 3282 void 3283 reset_type_linkage (tree type) 3284 { 3285 reset_type_linkage_1 (type); 3286 if (CLASS_TYPE_P (type)) 3287 { 3288 if (tree vt = CLASSTYPE_VTABLES (type)) 3289 { 3290 tree name = mangle_vtbl_for_type (type); 3291 DECL_NAME (vt) = name; 3292 SET_DECL_ASSEMBLER_NAME (vt, name); 3293 reset_decl_linkage (vt); 3294 } 3295 if (!ANON_AGGR_TYPE_P (type)) 3296 if (tree ti = CLASSTYPE_TYPEINFO_VAR (type)) 3297 { 3298 tree name = mangle_typeinfo_for_type (type); 3299 DECL_NAME (ti) = name; 3300 SET_DECL_ASSEMBLER_NAME (ti, name); 3301 TREE_TYPE (name) = type; 3302 reset_decl_linkage (ti); 3303 } 3304 for (tree m = TYPE_FIELDS (type); m; m = DECL_CHAIN (m)) 3305 { 3306 tree mem = STRIP_TEMPLATE (m); 3307 if (TREE_CODE (mem) == VAR_DECL || TREE_CODE (mem) == FUNCTION_DECL) 3308 reset_decl_linkage (mem); 3309 else if (DECL_IMPLICIT_TYPEDEF_P (mem)) 3310 reset_type_linkage (TREE_TYPE (mem)); 3311 } 3312 } 3313 } 3314 3315 /* Set up our initial idea of what the linkage of DECL should be. */ 3316 3317 void 3318 tentative_decl_linkage (tree decl) 3319 { 3320 if (DECL_INTERFACE_KNOWN (decl)) 3321 /* We've already made a decision as to how this function will 3322 be handled. */; 3323 else if (vague_linkage_p (decl)) 3324 { 3325 if (TREE_CODE (decl) == FUNCTION_DECL 3326 && decl_defined_p (decl)) 3327 { 3328 DECL_EXTERNAL (decl) = 1; 3329 DECL_NOT_REALLY_EXTERN (decl) = 1; 3330 note_vague_linkage_fn (decl); 3331 /* A non-template inline function with external linkage will 3332 always be COMDAT. As we must eventually determine the 3333 linkage of all functions, and as that causes writes to 3334 the data mapped in from the PCH file, it's advantageous 3335 to mark the functions at this point. */ 3336 if (DECL_DECLARED_INLINE_P (decl)) 3337 { 3338 if (!DECL_IMPLICIT_INSTANTIATION (decl) 3339 || DECL_DEFAULTED_FN (decl)) 3340 { 3341 /* This function must have external linkage, as 3342 otherwise DECL_INTERFACE_KNOWN would have been 3343 set. */ 3344 gcc_assert (TREE_PUBLIC (decl)); 3345 comdat_linkage (decl); 3346 DECL_INTERFACE_KNOWN (decl) = 1; 3347 } 3348 else if (DECL_MAYBE_IN_CHARGE_CDTOR_P (decl)) 3349 /* For implicit instantiations of cdtors try to make 3350 it comdat, so that maybe_clone_body can use aliases. 3351 See PR113208. */ 3352 maybe_make_one_only (decl); 3353 } 3354 } 3355 else if (VAR_P (decl)) 3356 maybe_commonize_var (decl); 3357 } 3358 } 3359 3360 /* DECL is a FUNCTION_DECL or VAR_DECL. If the object file linkage 3361 for DECL has not already been determined, do so now by setting 3362 DECL_EXTERNAL, DECL_COMDAT and other related flags. Until this 3363 function is called entities with vague linkage whose definitions 3364 are available must have TREE_PUBLIC set. 3365 3366 If this function decides to place DECL in COMDAT, it will set 3367 appropriate flags -- but will not clear DECL_EXTERNAL. It is up to 3368 the caller to decide whether or not to clear DECL_EXTERNAL. Some 3369 callers defer that decision until it is clear that DECL is actually 3370 required. */ 3371 3372 void 3373 import_export_decl (tree decl) 3374 { 3375 bool comdat_p; 3376 bool import_p; 3377 tree class_type = NULL_TREE; 3378 3379 if (DECL_INTERFACE_KNOWN (decl)) 3380 return; 3381 3382 /* We cannot determine what linkage to give to an entity with vague 3383 linkage until the end of the file. For example, a virtual table 3384 for a class will be defined if and only if the key method is 3385 defined in this translation unit. */ 3386 gcc_assert (at_eof); 3387 /* Object file linkage for explicit instantiations is handled in 3388 mark_decl_instantiated. For static variables in functions with 3389 vague linkage, maybe_commonize_var is used. 3390 3391 Therefore, the only declarations that should be provided to this 3392 function are those with external linkage that are: 3393 3394 * implicit instantiations of function templates 3395 3396 * inline functions 3397 3398 * inline variables 3399 3400 * implicit instantiations of static data members of class 3401 templates 3402 3403 * virtual tables 3404 3405 * typeinfo objects 3406 3407 Furthermore, all entities that reach this point must have a 3408 definition available in this translation unit. 3409 3410 The following assertions check these conditions. */ 3411 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl)); 3412 /* Any code that creates entities with TREE_PUBLIC cleared should 3413 also set DECL_INTERFACE_KNOWN. */ 3414 gcc_assert (TREE_PUBLIC (decl)); 3415 if (TREE_CODE (decl) == FUNCTION_DECL) 3416 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl) 3417 || DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl) 3418 || DECL_DECLARED_INLINE_P (decl)); 3419 else 3420 gcc_assert (DECL_IMPLICIT_INSTANTIATION (decl) 3421 || DECL_INLINE_VAR_P (decl) 3422 || DECL_VTABLE_OR_VTT_P (decl) 3423 || DECL_TINFO_P (decl)); 3424 /* Check that a definition of DECL is available in this translation 3425 unit. */ 3426 gcc_assert (!DECL_REALLY_EXTERN (decl)); 3427 3428 /* Assume that DECL will not have COMDAT linkage. */ 3429 comdat_p = false; 3430 /* Assume that DECL will not be imported into this translation 3431 unit. */ 3432 import_p = false; 3433 3434 /* FIXME: Since https://github.com/itanium-cxx-abi/cxx-abi/pull/171, 3435 the ABI specifies that classes attached to named modules should 3436 have their vtables uniquely emitted in the object for the module 3437 unit in which it is defined. And similarly for RTTI structures. */ 3438 if (VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl)) 3439 { 3440 class_type = DECL_CONTEXT (decl); 3441 import_export_class (class_type); 3442 if (CLASSTYPE_INTERFACE_KNOWN (class_type) 3443 && CLASSTYPE_INTERFACE_ONLY (class_type)) 3444 import_p = true; 3445 else if ((!flag_weak || TARGET_WEAK_NOT_IN_ARCHIVE_TOC) 3446 && !CLASSTYPE_USE_TEMPLATE (class_type) 3447 && CLASSTYPE_KEY_METHOD (class_type) 3448 && !DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type))) 3449 /* The ABI requires that all virtual tables be emitted with 3450 COMDAT linkage. However, on systems where COMDAT symbols 3451 don't show up in the table of contents for a static 3452 archive, or on systems without weak symbols (where we 3453 approximate COMDAT linkage by using internal linkage), the 3454 linker will report errors about undefined symbols because 3455 it will not see the virtual table definition. Therefore, 3456 in the case that we know that the virtual table will be 3457 emitted in only one translation unit, we make the virtual 3458 table an ordinary definition with external linkage. */ 3459 DECL_EXTERNAL (decl) = 0; 3460 else if (CLASSTYPE_INTERFACE_KNOWN (class_type)) 3461 { 3462 /* CLASS_TYPE is being exported from this translation unit, 3463 so DECL should be defined here. */ 3464 if (!flag_weak && CLASSTYPE_EXPLICIT_INSTANTIATION (class_type)) 3465 /* If a class is declared in a header with the "extern 3466 template" extension, then it will not be instantiated, 3467 even in translation units that would normally require 3468 it. Often such classes are explicitly instantiated in 3469 one translation unit. Therefore, the explicit 3470 instantiation must be made visible to other translation 3471 units. */ 3472 DECL_EXTERNAL (decl) = 0; 3473 else 3474 { 3475 /* The generic C++ ABI says that class data is always 3476 COMDAT, even if there is a key function. Some 3477 variants (e.g., the ARM EABI) says that class data 3478 only has COMDAT linkage if the class data might be 3479 emitted in more than one translation unit. When the 3480 key method can be inline and is inline, we still have 3481 to arrange for comdat even though 3482 class_data_always_comdat is false. */ 3483 if (!CLASSTYPE_KEY_METHOD (class_type) 3484 || DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (class_type)) 3485 || targetm.cxx.class_data_always_comdat ()) 3486 { 3487 /* The ABI requires COMDAT linkage. Normally, we 3488 only emit COMDAT things when they are needed; 3489 make sure that we realize that this entity is 3490 indeed needed. */ 3491 comdat_p = true; 3492 mark_needed (decl); 3493 } 3494 } 3495 } 3496 else if (!flag_implicit_templates 3497 && CLASSTYPE_IMPLICIT_INSTANTIATION (class_type)) 3498 import_p = true; 3499 else 3500 comdat_p = true; 3501 } 3502 else if (VAR_P (decl) && DECL_TINFO_P (decl)) 3503 { 3504 tree type = TREE_TYPE (DECL_NAME (decl)); 3505 if (CLASS_TYPE_P (type)) 3506 { 3507 class_type = type; 3508 import_export_class (type); 3509 if (CLASSTYPE_INTERFACE_KNOWN (type) 3510 && TYPE_POLYMORPHIC_P (type) 3511 && CLASSTYPE_INTERFACE_ONLY (type) 3512 /* If -fno-rtti was specified, then we cannot be sure 3513 that RTTI information will be emitted with the 3514 virtual table of the class, so we must emit it 3515 wherever it is used. */ 3516 && flag_rtti) 3517 import_p = true; 3518 else 3519 { 3520 if (CLASSTYPE_INTERFACE_KNOWN (type) 3521 && !CLASSTYPE_INTERFACE_ONLY (type)) 3522 { 3523 comdat_p = (targetm.cxx.class_data_always_comdat () 3524 || (CLASSTYPE_KEY_METHOD (type) 3525 && DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (type)))); 3526 mark_needed (decl); 3527 if (!flag_weak) 3528 { 3529 comdat_p = false; 3530 DECL_EXTERNAL (decl) = 0; 3531 } 3532 } 3533 else 3534 comdat_p = true; 3535 } 3536 } 3537 else 3538 comdat_p = true; 3539 } 3540 else if (DECL_TEMPLOID_INSTANTIATION (decl)) 3541 { 3542 /* DECL is an implicit instantiation of a function or static 3543 data member. */ 3544 if (flag_implicit_templates 3545 || (flag_implicit_inline_templates 3546 && TREE_CODE (decl) == FUNCTION_DECL 3547 && DECL_DECLARED_INLINE_P (decl))) 3548 comdat_p = true; 3549 else 3550 /* If we are not implicitly generating templates, then mark 3551 this entity as undefined in this translation unit. */ 3552 import_p = true; 3553 } 3554 else if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FUNCTION_MEMBER_P (decl)) 3555 { 3556 if (!DECL_DECLARED_INLINE_P (decl)) 3557 { 3558 tree ctype = DECL_CONTEXT (decl); 3559 import_export_class (ctype); 3560 if (CLASSTYPE_INTERFACE_KNOWN (ctype)) 3561 { 3562 DECL_NOT_REALLY_EXTERN (decl) 3563 = ! (CLASSTYPE_INTERFACE_ONLY (ctype) 3564 || (DECL_DECLARED_INLINE_P (decl) 3565 && ! flag_implement_inlines 3566 && !DECL_VINDEX (decl))); 3567 3568 if (!DECL_NOT_REALLY_EXTERN (decl)) 3569 DECL_EXTERNAL (decl) = 1; 3570 3571 /* Always make artificials weak. */ 3572 if (DECL_ARTIFICIAL (decl) && flag_weak) 3573 comdat_p = true; 3574 else 3575 maybe_make_one_only (decl); 3576 } 3577 } 3578 else 3579 comdat_p = true; 3580 } 3581 else 3582 comdat_p = true; 3583 3584 if (import_p) 3585 { 3586 /* If we are importing DECL into this translation unit, mark is 3587 an undefined here. */ 3588 DECL_EXTERNAL (decl) = 1; 3589 DECL_NOT_REALLY_EXTERN (decl) = 0; 3590 } 3591 else if (comdat_p) 3592 { 3593 /* If we decided to put DECL in COMDAT, mark it accordingly at 3594 this point. */ 3595 comdat_linkage (decl); 3596 } 3597 3598 DECL_INTERFACE_KNOWN (decl) = 1; 3599 } 3600 3601 /* Return an expression that performs the destruction of DECL, which 3602 must be a VAR_DECL whose type has a non-trivial destructor, or is 3603 an array whose (innermost) elements have a non-trivial destructor. */ 3604 3605 tree 3606 build_cleanup (tree decl) 3607 { 3608 tree clean = cxx_maybe_build_cleanup (decl, tf_warning_or_error); 3609 gcc_assert (clean != NULL_TREE); 3610 return clean; 3611 } 3612 3613 /* GUARD is a helper variable for DECL; make them have the same linkage and 3614 visibility. */ 3615 3616 void 3617 copy_linkage (tree guard, tree decl) 3618 { 3619 TREE_PUBLIC (guard) = TREE_PUBLIC (decl); 3620 TREE_STATIC (guard) = TREE_STATIC (decl); 3621 DECL_COMMON (guard) = DECL_COMMON (decl); 3622 DECL_COMDAT (guard) = DECL_COMDAT (decl); 3623 if (TREE_STATIC (guard)) 3624 { 3625 CP_DECL_THREAD_LOCAL_P (guard) = CP_DECL_THREAD_LOCAL_P (decl); 3626 set_decl_tls_model (guard, DECL_TLS_MODEL (decl)); 3627 if (DECL_ONE_ONLY (decl)) 3628 make_decl_one_only (guard, cxx_comdat_group (guard)); 3629 if (TREE_PUBLIC (decl)) 3630 DECL_WEAK (guard) = DECL_WEAK (decl); 3631 /* Also check vague_linkage_p, as DECL_WEAK and DECL_ONE_ONLY might not 3632 be set until import_export_decl at EOF. */ 3633 if (vague_linkage_p (decl)) 3634 comdat_linkage (guard); 3635 DECL_VISIBILITY (guard) = DECL_VISIBILITY (decl); 3636 DECL_VISIBILITY_SPECIFIED (guard) = DECL_VISIBILITY_SPECIFIED (decl); 3637 } 3638 } 3639 3640 /* Returns the initialization guard variable for the variable DECL, 3641 which has static storage duration. */ 3642 3643 tree 3644 get_guard (tree decl) 3645 { 3646 tree sname = mangle_guard_variable (decl); 3647 tree guard = get_global_binding (sname); 3648 if (! guard) 3649 { 3650 tree guard_type; 3651 3652 /* We use a type that is big enough to contain a mutex as well 3653 as an integer counter. */ 3654 guard_type = targetm.cxx.guard_type (); 3655 guard = build_decl (DECL_SOURCE_LOCATION (decl), 3656 VAR_DECL, sname, guard_type); 3657 3658 /* The guard should have the same linkage as what it guards. */ 3659 copy_linkage (guard, decl); 3660 3661 DECL_ARTIFICIAL (guard) = 1; 3662 DECL_IGNORED_P (guard) = 1; 3663 TREE_USED (guard) = 1; 3664 pushdecl_top_level_and_finish (guard, NULL_TREE); 3665 } 3666 return guard; 3667 } 3668 3669 /* Returns true if accessing the GUARD atomic is expensive, 3670 i.e. involves a call to __sync_synchronize or similar. 3671 In this case let __cxa_guard_acquire handle the atomics. */ 3672 3673 static bool 3674 is_atomic_expensive_p (machine_mode mode) 3675 { 3676 if (!flag_inline_atomics) 3677 return true; 3678 3679 if (!can_compare_and_swap_p (mode, false) || !can_atomic_load_p (mode)) 3680 return true; 3681 3682 return false; 3683 } 3684 3685 /* Return an atomic load of src with the appropriate memory model. */ 3686 3687 static tree 3688 build_atomic_load_type (tree src, HOST_WIDE_INT model, tree type) 3689 { 3690 tree ptr_type = build_pointer_type (type); 3691 tree mem_model = build_int_cst (integer_type_node, model); 3692 tree t, addr, val; 3693 unsigned int size; 3694 int fncode; 3695 3696 size = tree_to_uhwi (TYPE_SIZE_UNIT (type)); 3697 3698 fncode = BUILT_IN_ATOMIC_LOAD_N + exact_log2 (size) + 1; 3699 t = builtin_decl_implicit ((enum built_in_function) fncode); 3700 3701 addr = build1 (ADDR_EXPR, ptr_type, src); 3702 val = build_call_expr (t, 2, addr, mem_model); 3703 return val; 3704 } 3705 3706 /* Return those bits of the GUARD variable that should be set when the 3707 guarded entity is actually initialized. */ 3708 3709 static tree 3710 get_guard_bits (tree guard) 3711 { 3712 if (!targetm.cxx.guard_mask_bit ()) 3713 { 3714 /* We only set the first byte of the guard, in order to leave room 3715 for a mutex in the high-order bits. */ 3716 guard = build1 (ADDR_EXPR, 3717 build_pointer_type (TREE_TYPE (guard)), 3718 guard); 3719 guard = build1 (NOP_EXPR, 3720 build_pointer_type (char_type_node), 3721 guard); 3722 guard = build1 (INDIRECT_REF, char_type_node, guard); 3723 } 3724 3725 return guard; 3726 } 3727 3728 /* Return an expression which determines whether or not the GUARD 3729 variable has already been initialized. */ 3730 3731 tree 3732 get_guard_cond (tree guard, bool thread_safe) 3733 { 3734 tree guard_value; 3735 3736 if (!thread_safe) 3737 guard = get_guard_bits (guard); 3738 else 3739 { 3740 tree type = targetm.cxx.guard_mask_bit () 3741 ? TREE_TYPE (guard) : char_type_node; 3742 3743 if (is_atomic_expensive_p (TYPE_MODE (type))) 3744 guard = integer_zero_node; 3745 else 3746 guard = build_atomic_load_type (guard, MEMMODEL_ACQUIRE, type); 3747 } 3748 3749 /* Mask off all but the low bit. */ 3750 if (targetm.cxx.guard_mask_bit ()) 3751 { 3752 guard_value = integer_one_node; 3753 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard))) 3754 guard_value = fold_convert (TREE_TYPE (guard), guard_value); 3755 guard = cp_build_binary_op (input_location, 3756 BIT_AND_EXPR, guard, guard_value, 3757 tf_warning_or_error); 3758 } 3759 3760 guard_value = integer_zero_node; 3761 if (!same_type_p (TREE_TYPE (guard_value), TREE_TYPE (guard))) 3762 guard_value = fold_convert (TREE_TYPE (guard), guard_value); 3763 return cp_build_binary_op (input_location, 3764 EQ_EXPR, guard, guard_value, 3765 tf_warning_or_error); 3766 } 3767 3768 /* Return an expression which sets the GUARD variable, indicating that 3769 the variable being guarded has been initialized. */ 3770 3771 tree 3772 set_guard (tree guard) 3773 { 3774 tree guard_init; 3775 3776 /* Set the GUARD to one. */ 3777 guard = get_guard_bits (guard); 3778 guard_init = integer_one_node; 3779 if (!same_type_p (TREE_TYPE (guard_init), TREE_TYPE (guard))) 3780 guard_init = fold_convert (TREE_TYPE (guard), guard_init); 3781 return cp_build_modify_expr (input_location, guard, NOP_EXPR, guard_init, 3782 tf_warning_or_error); 3783 } 3784 3785 /* Returns true iff we can tell that VAR does not have a dynamic 3786 initializer. */ 3787 3788 static bool 3789 var_defined_without_dynamic_init (tree var) 3790 { 3791 /* constinit vars are guaranteed to not have dynamic initializer, 3792 but still registering the destructor counts as dynamic initialization. */ 3793 if (DECL_DECLARED_CONSTINIT_P (var) 3794 && COMPLETE_TYPE_P (TREE_TYPE (var)) 3795 && !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (var))) 3796 return true; 3797 /* If it's defined in another TU, we can't tell. */ 3798 if (DECL_EXTERNAL (var)) 3799 return false; 3800 /* If it has a non-trivial destructor, registering the destructor 3801 counts as dynamic initialization. */ 3802 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (var))) 3803 return false; 3804 /* If it's in this TU, its initializer has been processed, unless 3805 it's a case of self-initialization, then DECL_INITIALIZED_P is 3806 false while the initializer is handled by finish_id_expression. */ 3807 if (!DECL_INITIALIZED_P (var)) 3808 return false; 3809 /* If it has no initializer or a constant one, it's not dynamic. */ 3810 return (!DECL_NONTRIVIALLY_INITIALIZED_P (var) 3811 || DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (var)); 3812 } 3813 3814 /* Returns true iff VAR is a variable that needs uses to be 3815 wrapped for possible dynamic initialization. */ 3816 3817 bool 3818 var_needs_tls_wrapper (tree var) 3819 { 3820 return (!error_operand_p (var) 3821 && CP_DECL_THREAD_LOCAL_P (var) 3822 && !DECL_GNU_TLS_P (var) 3823 && !DECL_FUNCTION_SCOPE_P (var) 3824 && !var_defined_without_dynamic_init (var)); 3825 } 3826 3827 /* Get the FUNCTION_DECL for the shared TLS init function for this 3828 translation unit. */ 3829 3830 static tree 3831 get_local_tls_init_fn (location_t loc) 3832 { 3833 tree sname = get_identifier ("__tls_init"); 3834 tree fn = get_global_binding (sname); 3835 if (!fn) 3836 { 3837 fn = build_lang_decl_loc (loc, FUNCTION_DECL, sname, 3838 build_function_type (void_type_node, 3839 void_list_node)); 3840 SET_DECL_LANGUAGE (fn, lang_c); 3841 TREE_PUBLIC (fn) = false; 3842 DECL_ARTIFICIAL (fn) = true; 3843 mark_used (fn); 3844 set_global_binding (fn); 3845 } 3846 return fn; 3847 } 3848 3849 /* Get a FUNCTION_DECL for the init function for the thread_local 3850 variable VAR. The init function will be an alias to the function 3851 that initializes all the non-local TLS variables in the translation 3852 unit. The init function is only used by the wrapper function. */ 3853 3854 static tree 3855 get_tls_init_fn (tree var) 3856 { 3857 /* Only C++11 TLS vars need this init fn. */ 3858 if (!var_needs_tls_wrapper (var)) 3859 return NULL_TREE; 3860 3861 /* If -fno-extern-tls-init, assume that we don't need to call 3862 a tls init function for a variable defined in another TU. */ 3863 if (!flag_extern_tls_init && DECL_EXTERNAL (var)) 3864 return NULL_TREE; 3865 3866 /* If the variable is internal, or if we can't generate aliases, 3867 call the local init function directly. */ 3868 if (!TREE_PUBLIC (var) || !TARGET_SUPPORTS_ALIASES) 3869 return get_local_tls_init_fn (DECL_SOURCE_LOCATION (var)); 3870 3871 tree sname = mangle_tls_init_fn (var); 3872 tree fn = get_global_binding (sname); 3873 if (!fn) 3874 { 3875 fn = build_lang_decl (FUNCTION_DECL, sname, 3876 build_function_type (void_type_node, 3877 void_list_node)); 3878 SET_DECL_LANGUAGE (fn, lang_c); 3879 TREE_PUBLIC (fn) = TREE_PUBLIC (var); 3880 DECL_ARTIFICIAL (fn) = true; 3881 DECL_COMDAT (fn) = DECL_COMDAT (var); 3882 DECL_EXTERNAL (fn) = DECL_EXTERNAL (var); 3883 if (DECL_ONE_ONLY (var)) 3884 make_decl_one_only (fn, cxx_comdat_group (fn)); 3885 if (TREE_PUBLIC (var)) 3886 { 3887 tree obtype = strip_array_types (non_reference (TREE_TYPE (var))); 3888 /* If the variable is defined somewhere else and might have static 3889 initialization, make the init function a weak reference. */ 3890 if ((!TYPE_NEEDS_CONSTRUCTING (obtype) 3891 || TYPE_HAS_CONSTEXPR_CTOR (obtype) 3892 || TYPE_HAS_TRIVIAL_DFLT (obtype)) 3893 && TYPE_HAS_TRIVIAL_DESTRUCTOR (obtype) 3894 && DECL_EXTERNAL (var)) 3895 declare_weak (fn); 3896 else 3897 DECL_WEAK (fn) = DECL_WEAK (var); 3898 } 3899 DECL_VISIBILITY (fn) = DECL_VISIBILITY (var); 3900 DECL_VISIBILITY_SPECIFIED (fn) = DECL_VISIBILITY_SPECIFIED (var); 3901 DECL_DLLIMPORT_P (fn) = DECL_DLLIMPORT_P (var); 3902 DECL_IGNORED_P (fn) = 1; 3903 mark_used (fn); 3904 3905 DECL_BEFRIENDING_CLASSES (fn) = var; 3906 3907 set_global_binding (fn); 3908 } 3909 return fn; 3910 } 3911 3912 /* Get a FUNCTION_DECL for the init wrapper function for the thread_local 3913 variable VAR. The wrapper function calls the init function (if any) for 3914 VAR and then returns a reference to VAR. The wrapper function is used 3915 in place of VAR everywhere VAR is mentioned. */ 3916 3917 static tree 3918 get_tls_wrapper_fn (tree var) 3919 { 3920 /* Only C++11 TLS vars need this wrapper fn. */ 3921 if (!var_needs_tls_wrapper (var)) 3922 return NULL_TREE; 3923 3924 tree sname = mangle_tls_wrapper_fn (var); 3925 tree fn = get_global_binding (sname); 3926 if (!fn) 3927 { 3928 /* A named rvalue reference is an lvalue, so the wrapper should 3929 always return an lvalue reference. */ 3930 tree type = non_reference (TREE_TYPE (var)); 3931 type = build_reference_type (type); 3932 tree fntype = build_function_type (type, void_list_node); 3933 3934 fn = build_lang_decl_loc (DECL_SOURCE_LOCATION (var), 3935 FUNCTION_DECL, sname, fntype); 3936 SET_DECL_LANGUAGE (fn, lang_c); 3937 TREE_PUBLIC (fn) = TREE_PUBLIC (var); 3938 DECL_ARTIFICIAL (fn) = true; 3939 DECL_IGNORED_P (fn) = 1; 3940 DECL_CONTEXT (fn) = DECL_CONTEXT (var); 3941 /* The wrapper is inline and emitted everywhere var is used. */ 3942 DECL_DECLARED_INLINE_P (fn) = true; 3943 if (TREE_PUBLIC (var)) 3944 { 3945 comdat_linkage (fn); 3946 #ifdef HAVE_GAS_HIDDEN 3947 /* Make the wrapper bind locally; there's no reason to share 3948 the wrapper between multiple shared objects. */ 3949 DECL_VISIBILITY (fn) = VISIBILITY_INTERNAL; 3950 DECL_VISIBILITY_SPECIFIED (fn) = true; 3951 #endif 3952 } 3953 if (!TREE_PUBLIC (fn)) 3954 DECL_INTERFACE_KNOWN (fn) = true; 3955 mark_used (fn); 3956 note_vague_linkage_fn (fn); 3957 3958 #if 0 3959 /* We want CSE to commonize calls to the wrapper, but marking it as 3960 pure is unsafe since it has side-effects. I guess we need a new 3961 ECF flag even weaker than ECF_PURE. FIXME! */ 3962 DECL_PURE_P (fn) = true; 3963 #endif 3964 3965 DECL_BEFRIENDING_CLASSES (fn) = var; 3966 3967 set_global_binding (fn); 3968 } 3969 return fn; 3970 } 3971 3972 /* If EXPR is a thread_local variable that should be wrapped by init 3973 wrapper function, return a call to that function, otherwise return 3974 NULL. */ 3975 3976 tree 3977 maybe_get_tls_wrapper_call (tree expr) 3978 { 3979 if (VAR_P (expr) 3980 && !processing_template_decl 3981 && !cp_unevaluated_operand 3982 && CP_DECL_THREAD_LOCAL_P (expr)) 3983 if (tree wrap = get_tls_wrapper_fn (expr)) 3984 return build_cxx_call (wrap, 0, NULL, tf_warning_or_error); 3985 return NULL; 3986 } 3987 3988 /* At EOF, generate the definition for the TLS wrapper function FN: 3989 3990 T& var_wrapper() { 3991 if (init_fn) init_fn(); 3992 return var; 3993 } */ 3994 3995 static void 3996 generate_tls_wrapper (tree fn) 3997 { 3998 tree var = DECL_BEFRIENDING_CLASSES (fn); 3999 4000 start_preparsed_function (fn, NULL_TREE, SF_DEFAULT | SF_PRE_PARSED); 4001 tree body = begin_function_body (); 4002 /* Only call the init fn if there might be one. */ 4003 if (tree init_fn = get_tls_init_fn (var)) 4004 { 4005 tree if_stmt = NULL_TREE; 4006 /* If init_fn is a weakref, make sure it exists before calling. */ 4007 if (lookup_attribute ("weak", DECL_ATTRIBUTES (init_fn))) 4008 { 4009 if_stmt = begin_if_stmt (); 4010 tree addr = cp_build_addr_expr (init_fn, tf_warning_or_error); 4011 tree cond = cp_build_binary_op (DECL_SOURCE_LOCATION (var), 4012 NE_EXPR, addr, nullptr_node, 4013 tf_warning_or_error); 4014 finish_if_stmt_cond (cond, if_stmt); 4015 } 4016 finish_expr_stmt (build_cxx_call 4017 (init_fn, 0, NULL, tf_warning_or_error)); 4018 if (if_stmt) 4019 { 4020 finish_then_clause (if_stmt); 4021 finish_if_stmt (if_stmt); 4022 } 4023 } 4024 else 4025 /* If there's no initialization, the wrapper is a constant function. */ 4026 TREE_READONLY (fn) = true; 4027 finish_return_stmt (convert_from_reference (var)); 4028 finish_function_body (body); 4029 expand_or_defer_fn (finish_function (/*inline_p=*/false)); 4030 } 4031 4032 /* Start a global constructor or destructor function. */ 4033 4034 static tree 4035 start_objects (bool initp, unsigned priority, bool has_body) 4036 { 4037 bool default_init = initp && priority == DEFAULT_INIT_PRIORITY; 4038 bool is_module_init = default_init && module_global_init_needed (); 4039 tree name = NULL_TREE; 4040 4041 if (is_module_init) 4042 name = mangle_module_global_init (0); 4043 else 4044 { 4045 char type[14]; 4046 4047 /* We use `I' to indicate initialization and `D' to indicate 4048 destruction. */ 4049 unsigned len = sprintf (type, "sub_%c", initp ? 'I' : 'D'); 4050 if (priority != DEFAULT_INIT_PRIORITY) 4051 { 4052 char joiner = '_'; 4053 #ifdef JOINER 4054 joiner = JOINER; 4055 #endif 4056 type[len++] = joiner; 4057 sprintf (type + len, "%.5u", priority); 4058 } 4059 name = get_file_function_name (type); 4060 } 4061 4062 tree fntype = build_function_type (void_type_node, void_list_node); 4063 tree fndecl = build_lang_decl (FUNCTION_DECL, name, fntype); 4064 DECL_CONTEXT (fndecl) = FROB_CONTEXT (global_namespace); 4065 if (is_module_init) 4066 { 4067 SET_DECL_ASSEMBLER_NAME (fndecl, name); 4068 TREE_PUBLIC (fndecl) = true; 4069 determine_visibility (fndecl); 4070 } 4071 else 4072 TREE_PUBLIC (fndecl) = 0; 4073 start_preparsed_function (fndecl, /*attrs=*/NULL_TREE, SF_PRE_PARSED); 4074 4075 /* Mark as artificial because it's not explicitly in the user's 4076 source code. */ 4077 DECL_ARTIFICIAL (current_function_decl) = 1; 4078 4079 /* Mark this declaration as used to avoid spurious warnings. */ 4080 TREE_USED (current_function_decl) = 1; 4081 4082 /* Mark this function as a global constructor or destructor. */ 4083 if (initp) 4084 DECL_GLOBAL_CTOR_P (current_function_decl) = 1; 4085 else 4086 DECL_GLOBAL_DTOR_P (current_function_decl) = 1; 4087 4088 tree body = begin_compound_stmt (BCS_FN_BODY); 4089 4090 if (is_module_init && has_body) 4091 { 4092 // If the function is going to be empty, don't emit idempotency. 4093 // 'static bool __in_chrg = false; 4094 // if (__inchrg) return; 4095 // __inchrg = true 4096 tree var = build_lang_decl (VAR_DECL, in_charge_identifier, 4097 boolean_type_node); 4098 DECL_CONTEXT (var) = fndecl; 4099 DECL_ARTIFICIAL (var) = true; 4100 TREE_STATIC (var) = true; 4101 pushdecl (var); 4102 cp_finish_decl (var, NULL_TREE, false, NULL_TREE, 0); 4103 4104 tree if_stmt = begin_if_stmt (); 4105 finish_if_stmt_cond (var, if_stmt); 4106 finish_return_stmt (NULL_TREE); 4107 finish_then_clause (if_stmt); 4108 finish_if_stmt (if_stmt); 4109 4110 tree assign = build2 (MODIFY_EXPR, boolean_type_node, 4111 var, boolean_true_node); 4112 TREE_SIDE_EFFECTS (assign) = true; 4113 finish_expr_stmt (assign); 4114 } 4115 4116 return body; 4117 } 4118 4119 /* Finish a global constructor or destructor. Add it to the global 4120 ctors or dtors, if STARTP is true. */ 4121 4122 static tree 4123 finish_objects (bool initp, unsigned priority, tree body, bool startp) 4124 { 4125 /* Finish up. */ 4126 finish_compound_stmt (body); 4127 tree fn = finish_function (/*inline_p=*/false); 4128 4129 if (!startp) 4130 ; // Neither ctor nor dtor I be. 4131 else if (initp) 4132 { 4133 DECL_STATIC_CONSTRUCTOR (fn) = 1; 4134 decl_init_priority_insert (fn, priority); 4135 } 4136 else 4137 { 4138 DECL_STATIC_DESTRUCTOR (fn) = 1; 4139 decl_fini_priority_insert (fn, priority); 4140 } 4141 4142 return fn; 4143 } 4144 4145 /* The name of the function we create to handle initializations and 4146 destructions for objects with static storage duration. */ 4147 #define SSDF_IDENTIFIER "__static_initialization_and_destruction" 4148 4149 /* Begins the generation of the function that will handle all 4150 initialization or destruction of objects with static storage 4151 duration at PRIORITY. 4152 4153 It is assumed that this function will only be called once. */ 4154 4155 static tree 4156 start_partial_init_fini_fn (bool initp, unsigned priority, unsigned count) 4157 { 4158 char id[sizeof (SSDF_IDENTIFIER) + 1 /* '\0' */ + 32]; 4159 4160 /* Create the identifier for this function. It will be of the form 4161 SSDF_IDENTIFIER_<number>. */ 4162 sprintf (id, "%s_%u", SSDF_IDENTIFIER, count); 4163 4164 tree type = build_function_type (void_type_node, void_list_node); 4165 4166 /* Create the FUNCTION_DECL itself. */ 4167 tree fn = build_lang_decl (FUNCTION_DECL, get_identifier (id), type); 4168 TREE_PUBLIC (fn) = 0; 4169 DECL_ARTIFICIAL (fn) = 1; 4170 4171 /* Put this function in the list of functions to be called from the 4172 static constructors and destructors. */ 4173 if (!static_init_fini_fns[initp]) 4174 static_init_fini_fns[initp] = priority_map_t::create_ggc (); 4175 auto &slot = static_init_fini_fns[initp]->get_or_insert (priority); 4176 slot = tree_cons (fn, NULL_TREE, slot); 4177 4178 /* Put the function in the global scope. */ 4179 pushdecl (fn); 4180 4181 /* Start the function itself. This is equivalent to declaring the 4182 function as: 4183 4184 static void __ssdf (int __initialize_p, init __priority_p); 4185 4186 It is static because we only need to call this function from the 4187 various constructor and destructor functions for this module. */ 4188 start_preparsed_function (fn, /*attrs=*/NULL_TREE, SF_PRE_PARSED); 4189 4190 /* Set up the scope of the outermost block in the function. */ 4191 return begin_compound_stmt (BCS_FN_BODY); 4192 } 4193 4194 /* Finish the generation of the function which performs initialization 4195 or destruction of objects with static storage duration. */ 4196 4197 static void 4198 finish_partial_init_fini_fn (tree body) 4199 { 4200 /* Close out the function. */ 4201 finish_compound_stmt (body); 4202 expand_or_defer_fn (finish_function (/*inline_p=*/false)); 4203 } 4204 4205 /* The effective initialization priority of a DECL. */ 4206 4207 #define DECL_EFFECTIVE_INIT_PRIORITY(decl) \ 4208 ((!DECL_HAS_INIT_PRIORITY_P (decl) || DECL_INIT_PRIORITY (decl) == 0) \ 4209 ? DEFAULT_INIT_PRIORITY : DECL_INIT_PRIORITY (decl)) 4210 4211 /* Whether a DECL needs a guard to protect it against multiple 4212 initialization. */ 4213 4214 #define NEEDS_GUARD_P(decl) (TREE_PUBLIC (decl) && (DECL_COMMON (decl) \ 4215 || DECL_ONE_ONLY (decl) \ 4216 || DECL_WEAK (decl))) 4217 4218 /* Walks the initializer list of a global variable and looks for 4219 temporary variables (DECL_NAME() == NULL and DECL_ARTIFICIAL != 0) 4220 and that have their DECL_CONTEXT() == NULL. For each such 4221 temporary variable, set their DECL_CONTEXT() to CTX -- the 4222 initializing function. This is necessary because otherwise some 4223 optimizers (enabled by -O2 -fprofile-arcs) might crash when trying 4224 to refer to a temporary variable that does not have its 4225 DECL_CONTEXT() properly set. */ 4226 4227 static tree 4228 fix_temporary_vars_context_r (tree *node, 4229 int * /*unused*/, 4230 void *ctx) 4231 { 4232 if (TREE_CODE (*node) == BIND_EXPR) 4233 for (tree var = BIND_EXPR_VARS (*node); var; var = DECL_CHAIN (var)) 4234 if (VAR_P (var) && !DECL_NAME (var) 4235 && DECL_ARTIFICIAL (var) && !DECL_CONTEXT (var)) 4236 DECL_CONTEXT (var) = tree (ctx); 4237 4238 return NULL_TREE; 4239 } 4240 4241 /* Set up to handle the initialization or destruction of DECL. If 4242 INITP is nonzero, we are initializing the variable. Otherwise, we 4243 are destroying it. */ 4244 4245 static void 4246 one_static_initialization_or_destruction (bool initp, tree decl, tree init) 4247 { 4248 /* If we are supposed to destruct and there's a trivial destructor, 4249 nothing has to be done. */ 4250 gcc_checking_assert (init || !TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl))); 4251 4252 /* Trick the compiler into thinking we are at the file and line 4253 where DECL was declared so that error-messages make sense, and so 4254 that the debugger will show somewhat sensible file and line 4255 information. */ 4256 input_location = DECL_SOURCE_LOCATION (decl); 4257 4258 /* Make sure temporary variables in the initialiser all have 4259 their DECL_CONTEXT() set to a value different from NULL_TREE. 4260 This can happen when global variables initializers are built. 4261 In that case, the DECL_CONTEXT() of the global variables _AND_ of all 4262 the temporary variables that might have been generated in the 4263 accompanying initializers is NULL_TREE, meaning the variables have been 4264 declared in the global namespace. 4265 What we want to do here is to fix that and make sure the DECL_CONTEXT() 4266 of the temporaries are set to the current function decl. */ 4267 cp_walk_tree_without_duplicates (&init, 4268 fix_temporary_vars_context_r, 4269 current_function_decl); 4270 4271 /* Because of: 4272 4273 [class.access.spec] 4274 4275 Access control for implicit calls to the constructors, 4276 the conversion functions, or the destructor called to 4277 create and destroy a static data member is performed as 4278 if these calls appeared in the scope of the member's 4279 class. 4280 4281 we pretend we are in a static member function of the class of 4282 which the DECL is a member. */ 4283 if (member_p (decl)) 4284 { 4285 DECL_CONTEXT (current_function_decl) = DECL_CONTEXT (decl); 4286 DECL_STATIC_FUNCTION_P (current_function_decl) = 1; 4287 } 4288 4289 /* Assume we don't need a guard. */ 4290 tree guard_if_stmt = NULL_TREE; 4291 4292 /* We need a guard if this is an object with external linkage that 4293 might be initialized in more than one place. (For example, a 4294 static data member of a template, when the data member requires 4295 construction.) */ 4296 if (NEEDS_GUARD_P (decl)) 4297 { 4298 tree guard = get_guard (decl); 4299 tree guard_cond; 4300 4301 if (flag_use_cxa_atexit) 4302 { 4303 /* When using __cxa_atexit, we just check the GUARD as we 4304 would for a local static. We never try to destroy 4305 anything from a static destructor. */ 4306 gcc_assert (initp); 4307 guard_cond = get_guard_cond (guard, false); 4308 } 4309 else 4310 { 4311 /* If we don't have __cxa_atexit, then we will be running 4312 destructors from .fini sections, or their equivalents. 4313 So, we need to know how many times we've tried to 4314 initialize this object. We do initializations only if 4315 the GUARD was or becomes zero (initp vs !initp 4316 respectively). */ 4317 guard_cond = cp_build_unary_op (initp ? POSTINCREMENT_EXPR 4318 : PREDECREMENT_EXPR, 4319 guard, 4320 /*noconvert=*/true, 4321 tf_warning_or_error); 4322 guard_cond = cp_build_binary_op (input_location, EQ_EXPR, guard_cond, 4323 integer_zero_node, 4324 tf_warning_or_error); 4325 } 4326 4327 guard_if_stmt = begin_if_stmt (); 4328 finish_if_stmt_cond (guard_cond, guard_if_stmt); 4329 4330 if (flag_use_cxa_atexit) 4331 /* Set the GUARD now. */ 4332 finish_expr_stmt (set_guard (guard)); 4333 } 4334 4335 /* Perform the initialization or destruction. */ 4336 if (initp) 4337 { 4338 if (init) 4339 { 4340 finish_expr_stmt (init); 4341 if (sanitize_flags_p (SANITIZE_ADDRESS, decl)) 4342 if (varpool_node *vnode = varpool_node::get (decl)) 4343 vnode->dynamically_initialized = 1; 4344 } 4345 4346 /* If we're using __cxa_atexit, register a function that calls the 4347 destructor for the object. */ 4348 if (flag_use_cxa_atexit) 4349 finish_expr_stmt (register_dtor_fn (decl)); 4350 } 4351 else 4352 finish_expr_stmt (build_cleanup (decl)); 4353 4354 /* Finish the guard if-stmt, if necessary. */ 4355 if (guard_if_stmt) 4356 { 4357 finish_then_clause (guard_if_stmt); 4358 finish_if_stmt (guard_if_stmt); 4359 } 4360 4361 /* Now that we're done with DECL we don't need to pretend to be a 4362 member of its class any longer. */ 4363 DECL_CONTEXT (current_function_decl) = NULL_TREE; 4364 DECL_STATIC_FUNCTION_P (current_function_decl) = 0; 4365 } 4366 4367 /* Generate code to do the initialization or destruction of the decls in VARS, 4368 a TREE_LIST of VAR_DECL with static storage duration. 4369 Whether initialization or destruction is performed is specified by INITP. */ 4370 4371 static void 4372 emit_partial_init_fini_fn (bool initp, unsigned priority, tree vars, 4373 unsigned counter, location_t locus) 4374 { 4375 input_location = locus; 4376 tree body = start_partial_init_fini_fn (initp, priority, counter); 4377 4378 for (tree node = vars; node; node = TREE_CHAIN (node)) 4379 /* Do one initialization or destruction. */ 4380 one_static_initialization_or_destruction (initp, TREE_VALUE (node), 4381 TREE_PURPOSE (node)); 4382 4383 /* Finish up the static storage duration function for this 4384 round. */ 4385 input_location = locus; 4386 finish_partial_init_fini_fn (body); 4387 } 4388 4389 /* VARS is a list of variables with static storage duration which may 4390 need initialization and/or finalization. Remove those variables 4391 that don't really need to be initialized or finalized, and return 4392 the resulting list. The order in which the variables appear in 4393 VARS is in reverse order of the order in which they should actually 4394 be initialized. That order is preserved. */ 4395 4396 static tree 4397 prune_vars_needing_no_initialization (tree *vars) 4398 { 4399 tree *var = vars; 4400 tree result = NULL_TREE; 4401 4402 while (*var) 4403 { 4404 tree t = *var; 4405 tree decl = TREE_VALUE (t); 4406 tree init = TREE_PURPOSE (t); 4407 4408 /* Deal gracefully with error. */ 4409 if (error_operand_p (decl)) 4410 { 4411 var = &TREE_CHAIN (t); 4412 continue; 4413 } 4414 4415 /* The only things that can be initialized are variables. */ 4416 gcc_assert (VAR_P (decl)); 4417 4418 /* If this object is not defined, we don't need to do anything 4419 here. */ 4420 if (DECL_EXTERNAL (decl)) 4421 { 4422 var = &TREE_CHAIN (t); 4423 continue; 4424 } 4425 4426 /* Also, if the initializer already contains errors, we can bail 4427 out now. */ 4428 if (init && TREE_CODE (init) == TREE_LIST 4429 && value_member (error_mark_node, init)) 4430 { 4431 var = &TREE_CHAIN (t); 4432 continue; 4433 } 4434 4435 /* This variable is going to need initialization and/or 4436 finalization, so we add it to the list. */ 4437 *var = TREE_CHAIN (t); 4438 TREE_CHAIN (t) = result; 4439 result = t; 4440 } 4441 4442 return result; 4443 } 4444 4445 /* Split VAR_LIST by init priority and add into PARTS hash table. 4446 This reverses the variable ordering. */ 4447 4448 void 4449 partition_vars_for_init_fini (tree var_list, priority_map_t *(&parts)[2]) 4450 { 4451 for (auto node = var_list; node; node = TREE_CHAIN (node)) 4452 { 4453 tree decl = TREE_VALUE (node); 4454 tree init = TREE_PURPOSE (node); 4455 bool has_cleanup = !TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)); 4456 unsigned priority = DECL_EFFECTIVE_INIT_PRIORITY (decl); 4457 4458 if (init || (flag_use_cxa_atexit && has_cleanup)) 4459 { 4460 // Add to initialization list. 4461 if (!parts[true]) 4462 parts[true] = priority_map_t::create_ggc (); 4463 auto &slot = parts[true]->get_or_insert (priority); 4464 slot = tree_cons (init, decl, slot); 4465 } 4466 4467 if (!flag_use_cxa_atexit && has_cleanup) 4468 { 4469 // Add to finalization list. 4470 if (!parts[false]) 4471 parts[false] = priority_map_t::create_ggc (); 4472 auto &slot = parts[false]->get_or_insert (priority); 4473 slot = tree_cons (NULL_TREE, decl, slot); 4474 } 4475 } 4476 } 4477 4478 /* Make sure we have told the back end about all the variables in 4479 VARS. */ 4480 4481 static void 4482 write_out_vars (tree vars) 4483 { 4484 tree v; 4485 4486 for (v = vars; v; v = TREE_CHAIN (v)) 4487 { 4488 tree var = TREE_VALUE (v); 4489 if (!var_finalized_p (var)) 4490 { 4491 import_export_decl (var); 4492 rest_of_decl_compilation (var, 1, 1); 4493 } 4494 } 4495 } 4496 4497 /* Generate a static constructor or destructor that calls the given 4498 init/fini fns at the indicated priority. */ 4499 4500 static void 4501 generate_ctor_or_dtor_function (bool initp, unsigned priority, 4502 tree fns, location_t locus) 4503 { 4504 input_location = locus; 4505 tree body = start_objects (initp, priority, bool (fns)); 4506 4507 if (fns) 4508 { 4509 /* To make sure dynamic construction doesn't access globals from 4510 other compilation units where they might not be yet 4511 constructed, for -fsanitize=address insert 4512 __asan_before_dynamic_init call that prevents access to 4513 either all global variables that need construction in other 4514 compilation units, or at least those that haven't been 4515 initialized yet. Variables that need dynamic construction in 4516 the current compilation unit are kept accessible. */ 4517 if (initp && (flag_sanitize & SANITIZE_ADDRESS)) 4518 finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/false)); 4519 4520 /* Call the static init/fini functions. */ 4521 for (tree node = fns; node; node = TREE_CHAIN (node)) 4522 { 4523 tree fn = TREE_PURPOSE (node); 4524 4525 // We should never find a pure or constant cdtor. 4526 gcc_checking_assert (!(flags_from_decl_or_type (fn) 4527 & (ECF_CONST | ECF_PURE))); 4528 4529 tree call = cp_build_function_call_nary (fn, tf_warning_or_error, 4530 NULL_TREE); 4531 finish_expr_stmt (call); 4532 } 4533 4534 /* Revert what __asan_before_dynamic_init did by calling 4535 __asan_after_dynamic_init. */ 4536 if (initp && (flag_sanitize & SANITIZE_ADDRESS)) 4537 finish_expr_stmt (asan_dynamic_init_call (/*after_p=*/true)); 4538 } 4539 4540 /* Close out the function, and arrange for it to be called at init 4541 or fini time, if non-empty. (Even non-nop module initializer 4542 functions need this, as we cannot guarantee the module is 4543 imported somewhere in the program.) */ 4544 expand_or_defer_fn (finish_objects (initp, priority, body, fns != NULL_TREE)); 4545 } 4546 4547 /* Return C++ property of T, based on given operation OP. */ 4548 4549 static int 4550 cpp_check (tree t, cpp_operation op) 4551 { 4552 switch (op) 4553 { 4554 case HAS_DEPENDENT_TEMPLATE_ARGS: 4555 { 4556 tree ti = CLASSTYPE_TEMPLATE_INFO (t); 4557 if (!ti) 4558 return 0; 4559 ++processing_template_decl; 4560 const bool dep = any_dependent_template_arguments_p (TI_ARGS (ti)); 4561 --processing_template_decl; 4562 return dep; 4563 } 4564 case IS_ABSTRACT: 4565 return DECL_PURE_VIRTUAL_P (t); 4566 case IS_ASSIGNMENT_OPERATOR: 4567 return DECL_ASSIGNMENT_OPERATOR_P (t); 4568 case IS_CONSTRUCTOR: 4569 return DECL_CONSTRUCTOR_P (t); 4570 case IS_DESTRUCTOR: 4571 return DECL_DESTRUCTOR_P (t); 4572 case IS_COPY_CONSTRUCTOR: 4573 return DECL_COPY_CONSTRUCTOR_P (t); 4574 case IS_MOVE_CONSTRUCTOR: 4575 return DECL_MOVE_CONSTRUCTOR_P (t); 4576 case IS_TEMPLATE: 4577 return TREE_CODE (t) == TEMPLATE_DECL; 4578 case IS_TRIVIAL: 4579 return trivial_type_p (t); 4580 default: 4581 return 0; 4582 } 4583 } 4584 4585 /* Collect source file references recursively, starting from NAMESPC. */ 4586 4587 static void 4588 collect_source_refs (tree namespc) 4589 { 4590 /* Iterate over names in this name space. */ 4591 for (tree t = NAMESPACE_LEVEL (namespc)->names; t; t = TREE_CHAIN (t)) 4592 if (DECL_IS_UNDECLARED_BUILTIN (t)) 4593 ; 4594 else if (TREE_CODE (t) == NAMESPACE_DECL && !DECL_NAMESPACE_ALIAS (t)) 4595 collect_source_refs (t); 4596 else 4597 collect_source_ref (DECL_SOURCE_FILE (t)); 4598 } 4599 4600 /* Collect decls relevant to SOURCE_FILE from all namespaces recursively, 4601 starting from NAMESPC. */ 4602 4603 static void 4604 collect_ada_namespace (tree namespc, const char *source_file) 4605 { 4606 tree decl = NAMESPACE_LEVEL (namespc)->names; 4607 4608 /* Collect decls from this namespace. This will skip 4609 NAMESPACE_DECLs (both aliases and regular, it cannot tell). */ 4610 collect_ada_nodes (decl, source_file); 4611 4612 /* Now scan for namespace children, and dump them. */ 4613 for (; decl; decl = TREE_CHAIN (decl)) 4614 if (TREE_CODE (decl) == NAMESPACE_DECL && !DECL_NAMESPACE_ALIAS (decl)) 4615 collect_ada_namespace (decl, source_file); 4616 } 4617 4618 /* Returns true iff there is a definition available for variable or 4619 function DECL. */ 4620 4621 bool 4622 decl_defined_p (tree decl) 4623 { 4624 if (TREE_CODE (decl) == FUNCTION_DECL) 4625 return (DECL_INITIAL (decl) != NULL_TREE 4626 /* A pending instantiation of a friend temploid is defined. */ 4627 || (DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl) 4628 && DECL_INITIAL (DECL_TEMPLATE_RESULT 4629 (DECL_TI_TEMPLATE (decl))))); 4630 else 4631 { 4632 gcc_assert (VAR_P (decl)); 4633 return !DECL_EXTERNAL (decl); 4634 } 4635 } 4636 4637 /* Nonzero for a VAR_DECL whose value can be used in a constant expression. 4638 4639 [expr.const] 4640 4641 An integral constant-expression can only involve ... const 4642 variables of integral or enumeration types initialized with 4643 constant expressions ... 4644 4645 C++0x also allows constexpr variables and temporaries initialized 4646 with constant expressions. We handle the former here, but the latter 4647 are just folded away in cxx_eval_constant_expression. 4648 4649 The standard does not require that the expression be non-volatile. 4650 G++ implements the proposed correction in DR 457. */ 4651 4652 bool 4653 decl_constant_var_p (tree decl) 4654 { 4655 if (!decl_maybe_constant_var_p (decl)) 4656 return false; 4657 4658 /* We don't know if a template static data member is initialized with 4659 a constant expression until we instantiate its initializer. Even 4660 in the case of a constexpr variable, we can't treat it as a 4661 constant until its initializer is complete in case it's used in 4662 its own initializer. */ 4663 maybe_instantiate_decl (decl); 4664 return DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl); 4665 } 4666 4667 /* Returns true if DECL could be a symbolic constant variable, depending on 4668 its initializer. */ 4669 4670 bool 4671 decl_maybe_constant_var_p (tree decl) 4672 { 4673 tree type = TREE_TYPE (decl); 4674 if (!VAR_P (decl)) 4675 return false; 4676 if (DECL_DECLARED_CONSTEXPR_P (decl) 4677 && (!TREE_THIS_VOLATILE (decl) || NULLPTR_TYPE_P (type))) 4678 return true; 4679 if (DECL_HAS_VALUE_EXPR_P (decl)) 4680 /* A proxy isn't constant. */ 4681 return false; 4682 if (TYPE_REF_P (type)) 4683 /* References can be constant. */; 4684 else if (CP_TYPE_CONST_NON_VOLATILE_P (type) 4685 && INTEGRAL_OR_ENUMERATION_TYPE_P (type)) 4686 /* And const integers. */; 4687 else 4688 return false; 4689 4690 if (DECL_INITIAL (decl) 4691 && !DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl)) 4692 /* We know the initializer, and it isn't constant. */ 4693 return false; 4694 else 4695 return true; 4696 } 4697 4698 /* Complain that DECL uses a type with no linkage. In C++98 mode this is 4699 called from grokfndecl and grokvardecl; in all modes it is called from 4700 cp_write_global_declarations. */ 4701 4702 void 4703 no_linkage_error (tree decl) 4704 { 4705 if (cxx_dialect >= cxx11 4706 && (decl_defined_p (decl) 4707 /* Treat templates which limit_bad_template_recursion decided 4708 not to instantiate as if they were defined. */ 4709 || (errorcount + sorrycount > 0 4710 && DECL_LANG_SPECIFIC (decl) 4711 && DECL_TEMPLATE_INFO (decl) 4712 && warning_suppressed_p (decl /* What warning? */)))) 4713 /* In C++11 it's ok if the decl is defined. */ 4714 return; 4715 4716 if (DECL_LANG_SPECIFIC (decl) && DECL_MODULE_IMPORT_P (decl)) 4717 /* An imported decl is ok. */ 4718 return; 4719 4720 tree t = no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false); 4721 if (t == NULL_TREE) 4722 /* The type that got us on no_linkage_decls must have gotten a name for 4723 linkage purposes. */; 4724 else if (CLASS_TYPE_P (t) && TYPE_BEING_DEFINED (t)) 4725 // FIXME: This is now invalid, as a DR to c++98 4726 /* The type might end up having a typedef name for linkage purposes. */ 4727 vec_safe_push (no_linkage_decls, decl); 4728 else if (TYPE_UNNAMED_P (t)) 4729 { 4730 bool d = false; 4731 auto_diagnostic_group grp; 4732 if (cxx_dialect >= cxx11) 4733 { 4734 /* If t is declared in a module CMI, then decl could actually 4735 be defined in a different TU, so don't warn since C++20. */ 4736 tree relaxed = no_linkage_check (t, /*relaxed_p=*/true); 4737 if (relaxed != NULL_TREE) 4738 d = permerror (DECL_SOURCE_LOCATION (decl), 4739 "%q#D, declared using an unnamed type, " 4740 "is used but never defined", decl); 4741 else if (cxx_dialect < cxx20) 4742 d = pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wc__20_extensions, 4743 "%q#D, declared using an unnamed type, " 4744 "is used but not defined", decl); 4745 } 4746 else if (DECL_EXTERN_C_P (decl)) 4747 /* Allow this; it's pretty common in C. */; 4748 else if (VAR_P (decl)) 4749 /* DRs 132, 319 and 389 seem to indicate types with 4750 no linkage can only be used to declare extern "C" 4751 entities. Since it's not always an error in the 4752 ISO C++ 90 Standard, we only issue a warning. */ 4753 d = warning_at (DECL_SOURCE_LOCATION (decl), 0, "unnamed type " 4754 "with no linkage used to declare variable %q#D with " 4755 "linkage", decl); 4756 else 4757 d = permerror (DECL_SOURCE_LOCATION (decl), "unnamed type with no " 4758 "linkage used to declare function %q#D with linkage", 4759 decl); 4760 if (d && is_typedef_decl (TYPE_NAME (t))) 4761 inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)), "%q#D does not refer " 4762 "to the unqualified type, so it is not used for linkage", 4763 TYPE_NAME (t)); 4764 /* Suppress warning from check_global_declaration if needed. */ 4765 if (d) 4766 suppress_warning (decl, OPT_Wunused); 4767 } 4768 else if (cxx_dialect >= cxx11) 4769 { 4770 if (VAR_P (decl) || !DECL_PURE_VIRTUAL_P (decl)) 4771 { 4772 /* Similarly for local types in a function with vague linkage or 4773 defined in a module CMI, then decl could actually be defined 4774 in a different TU, so don't warn since C++20. */ 4775 bool d = false; 4776 tree relaxed = no_linkage_check (t, /*relaxed_p=*/true); 4777 if (relaxed != NULL_TREE) 4778 d = permerror (DECL_SOURCE_LOCATION (decl), 4779 "%q#D, declared using local type " 4780 "%qT, is used but never defined", decl, t); 4781 else if (cxx_dialect < cxx20) 4782 d = pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wc__20_extensions, 4783 "%q#D, declared using local type " 4784 "%qT, is used but not defined here", decl, t); 4785 /* Suppress warning from check_global_declaration if needed. */ 4786 if (d) 4787 suppress_warning (decl, OPT_Wunused); 4788 } 4789 } 4790 else if (VAR_P (decl)) 4791 warning_at (DECL_SOURCE_LOCATION (decl), 0, "type %qT with no linkage " 4792 "used to declare variable %q#D with linkage", t, decl); 4793 else 4794 permerror (DECL_SOURCE_LOCATION (decl), "type %qT with no linkage used " 4795 "to declare function %q#D with linkage", t, decl); 4796 } 4797 4798 /* Collect declarations from all namespaces relevant to SOURCE_FILE. */ 4799 4800 static void 4801 collect_all_refs (const char *source_file) 4802 { 4803 collect_ada_namespace (global_namespace, source_file); 4804 } 4805 4806 /* Clear DECL_EXTERNAL for NODE. */ 4807 4808 static bool 4809 clear_decl_external (struct cgraph_node *node, void * /*data*/) 4810 { 4811 DECL_EXTERNAL (node->decl) = 0; 4812 return false; 4813 } 4814 4815 /* Build up the function to run dynamic initializers for thread_local 4816 variables in this translation unit and alias the init functions for the 4817 individual variables to it. */ 4818 4819 static void 4820 handle_tls_init (void) 4821 { 4822 tree vars = prune_vars_needing_no_initialization (&tls_aggregates); 4823 if (vars == NULL_TREE) 4824 return; 4825 4826 location_t loc = DECL_SOURCE_LOCATION (TREE_VALUE (vars)); 4827 4828 write_out_vars (vars); 4829 4830 tree guard = build_decl (loc, VAR_DECL, get_identifier ("__tls_guard"), 4831 boolean_type_node); 4832 TREE_PUBLIC (guard) = false; 4833 TREE_STATIC (guard) = true; 4834 DECL_ARTIFICIAL (guard) = true; 4835 DECL_IGNORED_P (guard) = true; 4836 TREE_USED (guard) = true; 4837 CP_DECL_THREAD_LOCAL_P (guard) = true; 4838 set_decl_tls_model (guard, decl_default_tls_model (guard)); 4839 pushdecl_top_level_and_finish (guard, NULL_TREE); 4840 4841 tree fn = get_local_tls_init_fn (loc); 4842 start_preparsed_function (fn, NULL_TREE, SF_PRE_PARSED); 4843 tree body = begin_function_body (); 4844 tree if_stmt = begin_if_stmt (); 4845 tree cond = cp_build_unary_op (TRUTH_NOT_EXPR, guard, false, 4846 tf_warning_or_error); 4847 finish_if_stmt_cond (cond, if_stmt); 4848 finish_expr_stmt (cp_build_modify_expr (loc, guard, NOP_EXPR, 4849 boolean_true_node, 4850 tf_warning_or_error)); 4851 for (; vars; vars = TREE_CHAIN (vars)) 4852 { 4853 tree var = TREE_VALUE (vars); 4854 tree init = TREE_PURPOSE (vars); 4855 one_static_initialization_or_destruction (/*initp=*/true, var, init); 4856 4857 /* Output init aliases even with -fno-extern-tls-init. */ 4858 if (TARGET_SUPPORTS_ALIASES && TREE_PUBLIC (var)) 4859 { 4860 tree single_init_fn = get_tls_init_fn (var); 4861 if (single_init_fn == NULL_TREE) 4862 continue; 4863 cgraph_node *alias 4864 = cgraph_node::get_create (fn)->create_same_body_alias 4865 (single_init_fn, fn); 4866 gcc_assert (alias != NULL); 4867 } 4868 } 4869 4870 finish_then_clause (if_stmt); 4871 finish_if_stmt (if_stmt); 4872 finish_function_body (body); 4873 expand_or_defer_fn (finish_function (/*inline_p=*/false)); 4874 } 4875 4876 /* We're at the end of compilation, so generate any mangling aliases that 4877 we've been saving up, if DECL is going to be output and ID2 isn't 4878 already taken by another declaration. */ 4879 4880 static void 4881 generate_mangling_alias (tree decl, tree id2) 4882 { 4883 struct cgraph_node *n = NULL; 4884 4885 if (TREE_CODE (decl) == FUNCTION_DECL) 4886 { 4887 n = cgraph_node::get (decl); 4888 if (!n) 4889 /* Don't create an alias to an unreferenced function. */ 4890 return; 4891 } 4892 4893 tree *slot 4894 = mangled_decls->find_slot_with_hash (id2, IDENTIFIER_HASH_VALUE (id2), 4895 INSERT); 4896 4897 /* If there's a declaration already using this mangled name, 4898 don't create a compatibility alias that conflicts. */ 4899 if (*slot) 4900 return; 4901 4902 tree alias = make_alias_for (decl, id2); 4903 *slot = alias; 4904 4905 DECL_IGNORED_P (alias) = 1; 4906 TREE_PUBLIC (alias) = TREE_PUBLIC (decl); 4907 DECL_VISIBILITY (alias) = DECL_VISIBILITY (decl); 4908 if (vague_linkage_p (decl)) 4909 DECL_WEAK (alias) = 1; 4910 4911 if (n) 4912 n->create_same_body_alias (alias, decl); 4913 else 4914 varpool_node::create_extra_name_alias (alias, decl); 4915 } 4916 4917 /* Note that we might want to emit an alias with the symbol ID2 for DECL at 4918 the end of translation, for compatibility across bugs in the mangling 4919 implementation. */ 4920 4921 void 4922 note_mangling_alias (tree decl, tree id2) 4923 { 4924 if (TARGET_SUPPORTS_ALIASES) 4925 { 4926 if (!defer_mangling_aliases) 4927 generate_mangling_alias (decl, id2); 4928 else 4929 { 4930 vec_safe_push (mangling_aliases, decl); 4931 vec_safe_push (mangling_aliases, id2); 4932 } 4933 } 4934 } 4935 4936 /* Emit all mangling aliases that were deferred up to this point. */ 4937 4938 void 4939 generate_mangling_aliases () 4940 { 4941 while (!vec_safe_is_empty (mangling_aliases)) 4942 { 4943 tree id2 = mangling_aliases->pop(); 4944 tree decl = mangling_aliases->pop(); 4945 generate_mangling_alias (decl, id2); 4946 } 4947 defer_mangling_aliases = false; 4948 } 4949 4950 /* Record a mangling of DECL, whose DECL_ASSEMBLER_NAME has just been 4951 set. NEED_WARNING is true if we must warn about collisions. We do 4952 this to spot changes in mangling that may require compatibility 4953 aliases. */ 4954 4955 void 4956 record_mangling (tree decl, bool need_warning) 4957 { 4958 if (!mangled_decls) 4959 mangled_decls = hash_table<mangled_decl_hash>::create_ggc (499); 4960 4961 gcc_checking_assert (DECL_ASSEMBLER_NAME_SET_P (decl)); 4962 tree id = DECL_ASSEMBLER_NAME_RAW (decl); 4963 tree *slot 4964 = mangled_decls->find_slot_with_hash (id, IDENTIFIER_HASH_VALUE (id), 4965 INSERT); 4966 4967 /* If this is already an alias, cancel the alias, because the real 4968 decl takes precedence. */ 4969 if (*slot && DECL_ARTIFICIAL (*slot) && DECL_IGNORED_P (*slot)) 4970 { 4971 if (symtab_node *n = symtab_node::get (*slot)) 4972 { 4973 if (n->cpp_implicit_alias) 4974 /* Actually removing the node isn't safe if other code is already 4975 holding a pointer to it, so just neutralize it. */ 4976 n->reset (); 4977 } 4978 else 4979 /* analyze_functions might have already removed the alias from the 4980 symbol table if it's internal. */ 4981 gcc_checking_assert (!TREE_PUBLIC (*slot)); 4982 4983 *slot = NULL_TREE; 4984 } 4985 4986 if (!*slot) 4987 *slot = decl; 4988 else if (need_warning) 4989 { 4990 error_at (DECL_SOURCE_LOCATION (decl), 4991 "mangling of %q#D as %qE conflicts with a previous mangle", 4992 decl, id); 4993 inform (DECL_SOURCE_LOCATION (*slot), 4994 "previous mangling %q#D", *slot); 4995 inform (DECL_SOURCE_LOCATION (decl), 4996 "a later %<-fabi-version=%> (or =0)" 4997 " avoids this error with a change in mangling"); 4998 *slot = decl; 4999 } 5000 } 5001 5002 /* The mangled name of DECL is being forcibly changed to NAME. Remove 5003 any existing knowledge of DECL's mangled name meaning DECL. */ 5004 5005 void 5006 overwrite_mangling (tree decl, tree name) 5007 { 5008 if (tree id = DECL_ASSEMBLER_NAME_RAW (decl)) 5009 if ((TREE_CODE (decl) == VAR_DECL 5010 || TREE_CODE (decl) == FUNCTION_DECL) 5011 && mangled_decls) 5012 if (tree *slot 5013 = mangled_decls->find_slot_with_hash (id, IDENTIFIER_HASH_VALUE (id), 5014 NO_INSERT)) 5015 if (*slot == decl) 5016 { 5017 mangled_decls->clear_slot (slot); 5018 5019 /* If this is an alias, remove it from the symbol table. */ 5020 if (DECL_ARTIFICIAL (decl) && DECL_IGNORED_P (decl)) 5021 if (symtab_node *n = symtab_node::get (decl)) 5022 if (n->cpp_implicit_alias) 5023 n->remove (); 5024 } 5025 5026 DECL_ASSEMBLER_NAME_RAW (decl) = name; 5027 } 5028 5029 /* The entire file is now complete. If requested, dump everything 5030 to a file. */ 5031 5032 static void 5033 dump_tu (void) 5034 { 5035 dump_flags_t flags; 5036 if (FILE *stream = dump_begin (raw_dump_id, &flags)) 5037 { 5038 dump_node (global_namespace, flags & ~TDF_SLIM, stream); 5039 dump_end (raw_dump_id, stream); 5040 } 5041 } 5042 5043 static location_t locus_at_end_of_parsing; 5044 5045 /* Check the deallocation functions for CODE to see if we want to warn that 5046 only one was defined. */ 5047 5048 static void 5049 maybe_warn_sized_delete (enum tree_code code) 5050 { 5051 tree sized = NULL_TREE; 5052 tree unsized = NULL_TREE; 5053 5054 for (ovl_iterator iter (get_global_binding (ovl_op_identifier (false, code))); 5055 iter; ++iter) 5056 { 5057 tree fn = *iter; 5058 /* We're only interested in usual deallocation functions. */ 5059 if (!usual_deallocation_fn_p (fn)) 5060 continue; 5061 if (FUNCTION_ARG_CHAIN (fn) == void_list_node) 5062 unsized = fn; 5063 else 5064 sized = fn; 5065 } 5066 if (DECL_INITIAL (unsized) && !DECL_INITIAL (sized)) 5067 warning_at (DECL_SOURCE_LOCATION (unsized), OPT_Wsized_deallocation, 5068 "the program should also define %qD", sized); 5069 else if (!DECL_INITIAL (unsized) && DECL_INITIAL (sized)) 5070 warning_at (DECL_SOURCE_LOCATION (sized), OPT_Wsized_deallocation, 5071 "the program should also define %qD", unsized); 5072 } 5073 5074 /* Check the global deallocation functions to see if we want to warn about 5075 defining unsized without sized (or vice versa). */ 5076 5077 static void 5078 maybe_warn_sized_delete () 5079 { 5080 if (!flag_sized_deallocation || !warn_sized_deallocation) 5081 return; 5082 maybe_warn_sized_delete (DELETE_EXPR); 5083 maybe_warn_sized_delete (VEC_DELETE_EXPR); 5084 } 5085 5086 /* Earlier we left PTRMEM_CST in variable initializers alone so that we could 5087 look them up when evaluating non-type template parameters. Now we need to 5088 lower them to something the back end can understand. */ 5089 5090 static void 5091 lower_var_init () 5092 { 5093 varpool_node *node; 5094 FOR_EACH_VARIABLE (node) 5095 { 5096 tree d = node->decl; 5097 if (tree init = DECL_INITIAL (d)) 5098 DECL_INITIAL (d) = cplus_expand_constant (init); 5099 } 5100 } 5101 5102 /* This routine is called at the end of compilation. 5103 Its job is to create all the code needed to initialize and 5104 destroy the global aggregates. We do the destruction 5105 first, since that way we only need to reverse the decls once. */ 5106 5107 void 5108 c_parse_final_cleanups (void) 5109 { 5110 size_t i; 5111 tree decl; 5112 5113 locus_at_end_of_parsing = input_location; 5114 /* We're done parsing. */ 5115 at_eof = 1; 5116 5117 /* Bad parse errors. Just forget about it. */ 5118 if (! global_bindings_p () || current_class_type 5119 || !vec_safe_is_empty (decl_namespace_list)) 5120 return; 5121 5122 /* This is the point to write out a PCH if we're doing that. 5123 In that case we do not want to do anything else. */ 5124 if (pch_file) 5125 { 5126 /* Mangle all symbols at PCH creation time. */ 5127 symtab_node *node; 5128 FOR_EACH_SYMBOL (node) 5129 if (! is_a <varpool_node *> (node) 5130 || ! DECL_HARD_REGISTER (node->decl)) 5131 DECL_ASSEMBLER_NAME (node->decl); 5132 c_common_write_pch (); 5133 dump_tu (); 5134 /* Ensure even the callers don't try to finalize the CU. */ 5135 flag_syntax_only = 1; 5136 return; 5137 } 5138 5139 timevar_stop (TV_PHASE_PARSING); 5140 timevar_start (TV_PHASE_DEFERRED); 5141 5142 symtab->process_same_body_aliases (); 5143 5144 /* Handle -fdump-ada-spec[-slim] */ 5145 if (flag_dump_ada_spec || flag_dump_ada_spec_slim) 5146 { 5147 collect_source_ref (main_input_filename); 5148 if (!flag_dump_ada_spec_slim) 5149 collect_source_refs (global_namespace); 5150 5151 dump_ada_specs (collect_all_refs, cpp_check); 5152 } 5153 5154 /* FIXME - huh? was input_line -= 1;*/ 5155 5156 /* We now have to write out all the stuff we put off writing out. 5157 These include: 5158 5159 o Template specializations that we have not yet instantiated, 5160 but which are needed. 5161 o Initialization and destruction for non-local objects with 5162 static storage duration. (Local objects with static storage 5163 duration are initialized when their scope is first entered, 5164 and are cleaned up via atexit.) 5165 o Virtual function tables. 5166 5167 All of these may cause others to be needed. For example, 5168 instantiating one function may cause another to be needed, and 5169 generating the initializer for an object may cause templates to be 5170 instantiated, etc., etc. */ 5171 5172 emit_support_tinfos (); 5173 5174 /* Track vtables we want to emit that refer to consteval functions. */ 5175 auto_vec<tree> consteval_vtables; 5176 5177 int retries = 0; 5178 unsigned ssdf_count = 0; 5179 for (bool reconsider = true; reconsider; retries++) 5180 { 5181 reconsider = false; 5182 5183 /* If there are templates that we've put off instantiating, do 5184 them now. */ 5185 instantiate_pending_templates (retries); 5186 ggc_collect (); 5187 5188 if (header_module_p ()) 5189 /* A header modules initializations are handled in its 5190 importer. */ 5191 continue; 5192 5193 /* Write out virtual tables as required. Writing out the 5194 virtual table for a template class may cause the 5195 instantiation of members of that class. If we write out 5196 vtables then we remove the class from our list so we don't 5197 have to look at it again. */ 5198 tree t; 5199 for (i = keyed_classes->length (); 5200 keyed_classes->iterate (--i, &t);) 5201 if (maybe_emit_vtables (t, consteval_vtables)) 5202 { 5203 reconsider = true; 5204 keyed_classes->unordered_remove (i); 5205 } 5206 /* The input_location may have been changed during marking of 5207 vtable entries. */ 5208 input_location = locus_at_end_of_parsing; 5209 5210 /* Write out needed type info variables. We have to be careful 5211 looping through unemitted decls, because emit_tinfo_decl may 5212 cause other variables to be needed. New elements will be 5213 appended, and we remove from the vector those that actually 5214 get emitted. */ 5215 for (i = unemitted_tinfo_decls->length (); 5216 unemitted_tinfo_decls->iterate (--i, &t);) 5217 if (DECL_INITIAL (t) || emit_tinfo_decl (t)) 5218 { 5219 reconsider = true; 5220 unemitted_tinfo_decls->unordered_remove (i); 5221 } 5222 5223 /* The list of objects with static storage duration is built up 5224 in reverse order. We clear STATIC_AGGREGATES so that any new 5225 aggregates added during the initialization of these will be 5226 initialized in the correct order when we next come around the 5227 loop. */ 5228 if (tree vars = prune_vars_needing_no_initialization (&static_aggregates)) 5229 { 5230 if (flag_openmp) 5231 /* Add initializer information from VARS into 5232 DYNAMIC_INITIALIZERS. */ 5233 for (t = vars; t; t = TREE_CHAIN (t)) 5234 hash_map_safe_put<hm_ggc> (dynamic_initializers, 5235 TREE_VALUE (t), TREE_PURPOSE (t)); 5236 5237 /* Make sure the back end knows about all the variables. */ 5238 write_out_vars (vars); 5239 5240 function_depth++; // Disable GC 5241 priority_map_t *parts[2] = {nullptr, nullptr}; 5242 partition_vars_for_init_fini (vars, parts); 5243 5244 for (unsigned initp = 2; initp--;) 5245 if (parts[initp]) 5246 for (auto iter : *parts[initp]) 5247 { 5248 auto list = iter.second; 5249 if (initp) 5250 // Partitioning kept the vars in reverse order. 5251 // We only want that for dtors. 5252 list = nreverse (list); 5253 emit_partial_init_fini_fn (initp, iter.first, list, 5254 ssdf_count++, 5255 locus_at_end_of_parsing); 5256 } 5257 function_depth--; // Re-enable GC 5258 5259 /* All those initializations and finalizations might cause 5260 us to need more inline functions, more template 5261 instantiations, etc. */ 5262 reconsider = true; 5263 } 5264 5265 /* Now do the same for thread_local variables. */ 5266 handle_tls_init (); 5267 5268 /* Go through the set of inline functions whose bodies have not 5269 been emitted yet. If out-of-line copies of these functions 5270 are required, emit them. */ 5271 FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl) 5272 { 5273 /* Does it need synthesizing? */ 5274 if (DECL_DEFAULTED_FN (decl) && ! DECL_INITIAL (decl) 5275 && (! DECL_REALLY_EXTERN (decl) || possibly_inlined_p (decl))) 5276 { 5277 /* Even though we're already at the top-level, we push 5278 there again. That way, when we pop back a few lines 5279 hence, all of our state is restored. Otherwise, 5280 finish_function doesn't clean things up, and we end 5281 up with CURRENT_FUNCTION_DECL set. */ 5282 push_to_top_level (); 5283 /* The decl's location will mark where it was first 5284 needed. Save that so synthesize method can indicate 5285 where it was needed from, in case of error */ 5286 input_location = DECL_SOURCE_LOCATION (decl); 5287 synthesize_method (decl); 5288 pop_from_top_level (); 5289 reconsider = true; 5290 } 5291 5292 if (!DECL_INITIAL (decl) && decl_tls_wrapper_p (decl)) 5293 generate_tls_wrapper (decl); 5294 5295 if (!DECL_SAVED_TREE (decl)) 5296 continue; 5297 5298 cgraph_node *node = cgraph_node::get_create (decl); 5299 5300 /* We lie to the back end, pretending that some functions 5301 are not defined when they really are. This keeps these 5302 functions from being put out unnecessarily. But, we must 5303 stop lying when the functions are referenced, or if they 5304 are not comdat since they need to be put out now. If 5305 DECL_INTERFACE_KNOWN, then we have already set 5306 DECL_EXTERNAL appropriately, so there's no need to check 5307 again, and we do not want to clear DECL_EXTERNAL if a 5308 previous call to import_export_decl set it. 5309 5310 This is done in a separate for cycle, because if some 5311 deferred function is contained in another deferred 5312 function later in deferred_fns varray, 5313 rest_of_compilation would skip this function and we 5314 really cannot expand the same function twice. */ 5315 import_export_decl (decl); 5316 if (DECL_NOT_REALLY_EXTERN (decl) 5317 && DECL_INITIAL (decl) 5318 && decl_needed_p (decl)) 5319 { 5320 if (node->cpp_implicit_alias) 5321 node = node->get_alias_target (); 5322 5323 node->call_for_symbol_thunks_and_aliases (clear_decl_external, 5324 NULL, true); 5325 /* If we mark !DECL_EXTERNAL one of the symbols in some comdat 5326 group, we need to mark all symbols in the same comdat group 5327 that way. */ 5328 if (node->same_comdat_group) 5329 for (cgraph_node *next 5330 = dyn_cast<cgraph_node *> (node->same_comdat_group); 5331 next != node; 5332 next = dyn_cast<cgraph_node *> (next->same_comdat_group)) 5333 next->call_for_symbol_thunks_and_aliases (clear_decl_external, 5334 NULL, true); 5335 } 5336 5337 /* If we're going to need to write this function out, and 5338 there's already a body for it, create RTL for it now. 5339 (There might be no body if this is a method we haven't 5340 gotten around to synthesizing yet.) */ 5341 if (!DECL_EXTERNAL (decl) 5342 && decl_needed_p (decl) 5343 && !TREE_ASM_WRITTEN (decl) 5344 && !DECL_IMMEDIATE_FUNCTION_P (decl) 5345 && !node->definition) 5346 { 5347 /* We will output the function; no longer consider it in this 5348 loop. */ 5349 DECL_DEFER_OUTPUT (decl) = 0; 5350 /* Generate RTL for this function now that we know we 5351 need it. */ 5352 expand_or_defer_fn (decl); 5353 reconsider = true; 5354 } 5355 } 5356 5357 if (wrapup_namespace_globals ()) 5358 reconsider = true; 5359 5360 /* Static data members are just like namespace-scope globals. */ 5361 FOR_EACH_VEC_SAFE_ELT (pending_statics, i, decl) 5362 { 5363 if (var_finalized_p (decl) || DECL_REALLY_EXTERN (decl) 5364 /* Don't write it out if we haven't seen a definition. */ 5365 || DECL_IN_AGGR_P (decl)) 5366 continue; 5367 import_export_decl (decl); 5368 /* If this static data member is needed, provide it to the 5369 back end. */ 5370 if (DECL_NOT_REALLY_EXTERN (decl) && decl_needed_p (decl)) 5371 DECL_EXTERNAL (decl) = 0; 5372 } 5373 5374 if (vec_safe_length (pending_statics) != 0 5375 && wrapup_global_declarations (pending_statics->address (), 5376 pending_statics->length ())) 5377 reconsider = true; 5378 } 5379 5380 /* All templates have been instantiated. */ 5381 at_eof = 2; 5382 5383 void *module_cookie = finish_module_processing (parse_in); 5384 5385 lower_var_init (); 5386 5387 generate_mangling_aliases (); 5388 5389 /* All used inline functions must have a definition at this point. */ 5390 FOR_EACH_VEC_SAFE_ELT (deferred_fns, i, decl) 5391 { 5392 if (/* Check online inline functions that were actually used. */ 5393 DECL_ODR_USED (decl) && DECL_DECLARED_INLINE_P (decl) 5394 /* If the definition actually was available here, then the 5395 fact that the function was not defined merely represents 5396 that for some reason (use of a template repository, 5397 #pragma interface, etc.) we decided not to emit the 5398 definition here. */ 5399 && !DECL_INITIAL (decl) 5400 /* A defaulted fn or TLS wrapper in a header module can be 5401 synthesized on demand later. (In non-header modules we 5402 should have synthesized it above.) */ 5403 && !(header_module_p () 5404 && (DECL_DEFAULTED_FN (decl) || decl_tls_wrapper_p (decl))) 5405 /* Don't complain if the template was defined. */ 5406 && !(DECL_TEMPLATE_INSTANTIATION (decl) 5407 && DECL_INITIAL (DECL_TEMPLATE_RESULT 5408 (template_for_substitution (decl)))) 5409 && warning_at (DECL_SOURCE_LOCATION (decl), 0, 5410 "inline function %qD used but never defined", decl)) 5411 /* Avoid a duplicate warning from check_global_declaration. */ 5412 suppress_warning (decl, OPT_Wunused); 5413 } 5414 5415 /* So must decls that use a type with no linkage. */ 5416 FOR_EACH_VEC_SAFE_ELT (no_linkage_decls, i, decl) 5417 no_linkage_error (decl); 5418 5419 maybe_warn_sized_delete (); 5420 5421 // Place the init fns in the right order. We need to do this now, 5422 // so that any module init will go at the start. 5423 if (static_init_fini_fns[true]) 5424 for (auto iter : *static_init_fini_fns[true]) 5425 iter.second = nreverse (iter.second); 5426 5427 /* Now we've instantiated all templates. Now we can escalate the functions 5428 we squirreled away earlier. */ 5429 process_and_check_pending_immediate_escalating_fns (); 5430 5431 /* Then, do the Objective-C stuff. This is where all the 5432 Objective-C module stuff gets generated (symtab, 5433 class/protocol/selector lists etc). This must be done after C++ 5434 templates, destructors etc. so that selectors used in C++ 5435 templates are properly allocated. */ 5436 if (c_dialect_objc ()) 5437 objc_write_global_declarations (); 5438 5439 bool has_module_inits = module_determine_import_inits (); 5440 bool has_objc_init = c_dialect_objc () && objc_static_init_needed_p (); 5441 if (has_module_inits || has_objc_init) 5442 { 5443 input_location = locus_at_end_of_parsing; 5444 tree body = start_partial_init_fini_fn (true, DEFAULT_INIT_PRIORITY, 5445 ssdf_count++); 5446 /* For Objective-C++, we may need to initialize metadata found 5447 in this module. This must be done _before_ any other static 5448 initializations. */ 5449 if (has_objc_init) 5450 objc_generate_static_init_call (NULL_TREE); 5451 if (has_module_inits) 5452 module_add_import_initializers (); 5453 input_location = locus_at_end_of_parsing; 5454 finish_partial_init_fini_fn (body); 5455 } 5456 5457 if (module_global_init_needed ()) 5458 { 5459 // Make sure there's a default priority entry. 5460 if (!static_init_fini_fns[true]) 5461 static_init_fini_fns[true] = priority_map_t::create_ggc (); 5462 if (static_init_fini_fns[true]->get_or_insert (DEFAULT_INIT_PRIORITY)) 5463 has_module_inits = true; 5464 } 5465 5466 /* Generate initialization and destruction functions for all 5467 priorities for which they are required. They have C-language 5468 linkage. */ 5469 push_lang_context (lang_name_c); 5470 for (unsigned initp = 2; initp--;) 5471 if (static_init_fini_fns[initp]) 5472 { 5473 for (auto iter : *static_init_fini_fns[initp]) 5474 generate_ctor_or_dtor_function (initp, iter.first, iter.second, 5475 locus_at_end_of_parsing); 5476 static_init_fini_fns[initp] = nullptr; 5477 } 5478 pop_lang_context (); 5479 5480 fini_modules (parse_in, module_cookie, has_module_inits); 5481 5482 /* Generate any missing aliases. */ 5483 maybe_apply_pending_pragma_weaks (); 5484 5485 if (flag_vtable_verify) 5486 { 5487 vtv_recover_class_info (); 5488 vtv_compute_class_hierarchy_transitive_closure (); 5489 vtv_build_vtable_verify_fndecl (); 5490 } 5491 5492 perform_deferred_noexcept_checks (); 5493 5494 fini_constexpr (); 5495 cp_tree_c_finish_parsing (); 5496 clear_consteval_vfns (consteval_vtables); 5497 5498 /* The entire file is now complete. If requested, dump everything 5499 to a file. */ 5500 dump_tu (); 5501 5502 if (flag_detailed_statistics) 5503 { 5504 dump_tree_statistics (); 5505 dump_time_statistics (); 5506 } 5507 5508 timevar_stop (TV_PHASE_DEFERRED); 5509 timevar_start (TV_PHASE_PARSING); 5510 5511 /* Indicate that we're done with front end processing. */ 5512 at_eof = 3; 5513 } 5514 5515 /* Perform any post compilation-proper cleanups for the C++ front-end. 5516 This should really go away. No front-end should need to do 5517 anything past the compilation process. */ 5518 5519 void 5520 cxx_post_compilation_parsing_cleanups (void) 5521 { 5522 timevar_start (TV_PHASE_LATE_PARSING_CLEANUPS); 5523 5524 if (flag_vtable_verify) 5525 { 5526 /* Generate the special constructor initialization function that 5527 calls __VLTRegisterPairs, and give it a very high 5528 initialization priority. This must be done after 5529 finalize_compilation_unit so that we have accurate 5530 information about which vtable will actually be emitted. */ 5531 vtv_generate_init_routine (); 5532 } 5533 5534 input_location = locus_at_end_of_parsing; 5535 5536 if (flag_checking) 5537 validate_conversion_obstack (); 5538 5539 timevar_stop (TV_PHASE_LATE_PARSING_CLEANUPS); 5540 } 5541 5542 /* FN is an OFFSET_REF, DOTSTAR_EXPR or MEMBER_REF indicating the 5543 function to call in parse-tree form; it has not yet been 5544 semantically analyzed. ARGS are the arguments to the function. 5545 They have already been semantically analyzed. This may change 5546 ARGS. */ 5547 5548 tree 5549 build_offset_ref_call_from_tree (tree fn, vec<tree, va_gc> **args, 5550 tsubst_flags_t complain) 5551 { 5552 tree orig_fn; 5553 vec<tree, va_gc> *orig_args = NULL; 5554 tree expr; 5555 tree object; 5556 5557 orig_fn = fn; 5558 object = TREE_OPERAND (fn, 0); 5559 5560 if (processing_template_decl) 5561 { 5562 gcc_assert (TREE_CODE (fn) == DOTSTAR_EXPR 5563 || TREE_CODE (fn) == MEMBER_REF); 5564 if (type_dependent_expression_p (fn) 5565 || any_type_dependent_arguments_p (*args)) 5566 return build_min_nt_call_vec (fn, *args); 5567 5568 orig_args = make_tree_vector_copy (*args); 5569 5570 /* Transform the arguments and add the implicit "this" 5571 parameter. */ 5572 if (TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE) 5573 { 5574 if (TREE_CODE (fn) == DOTSTAR_EXPR) 5575 object = cp_build_addr_expr (object, complain); 5576 vec_safe_insert (*args, 0, object); 5577 } 5578 } 5579 5580 /* A qualified name corresponding to a bound pointer-to-member is 5581 represented as an OFFSET_REF: 5582 5583 struct B { void g(); }; 5584 void (B::*p)(); 5585 void B::g() { (this->*p)(); } */ 5586 if (TREE_CODE (fn) == OFFSET_REF) 5587 { 5588 tree object_addr = cp_build_addr_expr (object, complain); 5589 fn = TREE_OPERAND (fn, 1); 5590 fn = get_member_function_from_ptrfunc (&object_addr, fn, 5591 complain); 5592 vec_safe_insert (*args, 0, object_addr); 5593 } 5594 5595 if (CLASS_TYPE_P (TREE_TYPE (fn))) 5596 expr = build_op_call (fn, args, complain); 5597 else 5598 expr = cp_build_function_call_vec (fn, args, complain); 5599 if (processing_template_decl && expr != error_mark_node) 5600 expr = build_min_non_dep_call_vec (expr, orig_fn, orig_args); 5601 5602 if (orig_args != NULL) 5603 release_tree_vector (orig_args); 5604 5605 return expr; 5606 } 5607 5608 5609 void 5610 check_default_args (tree x) 5611 { 5612 tree arg = TYPE_ARG_TYPES (TREE_TYPE (x)); 5613 bool saw_def = false; 5614 bool noted_first_def = false; 5615 int idx_of_first_default_arg = 0; 5616 location_t loc_of_first_default_arg = UNKNOWN_LOCATION; 5617 int i = 0 - (TREE_CODE (TREE_TYPE (x)) == METHOD_TYPE); 5618 tree fndecl = STRIP_TEMPLATE (x); 5619 auto_diagnostic_group d; 5620 for (; arg && arg != void_list_node; arg = TREE_CHAIN (arg), ++i) 5621 { 5622 if (TREE_PURPOSE (arg)) 5623 { 5624 if (!saw_def) 5625 { 5626 saw_def = true; 5627 idx_of_first_default_arg = i; 5628 location_t loc = get_fndecl_argument_location (fndecl, i); 5629 if (loc != DECL_SOURCE_LOCATION (x)) 5630 loc_of_first_default_arg = loc; 5631 } 5632 } 5633 else if (saw_def && !PACK_EXPANSION_P (TREE_VALUE (arg))) 5634 { 5635 error_at (get_fndecl_argument_location (fndecl, i), 5636 "default argument missing for parameter %P of %q#D", i, x); 5637 if (loc_of_first_default_arg != UNKNOWN_LOCATION 5638 && !noted_first_def) 5639 { 5640 inform (loc_of_first_default_arg, 5641 "...following parameter %P which has a default argument", 5642 idx_of_first_default_arg); 5643 noted_first_def = true; 5644 } 5645 TREE_PURPOSE (arg) = error_mark_node; 5646 } 5647 } 5648 } 5649 5650 /* Return true if function DECL can be inlined. This is used to force 5651 instantiation of methods that might be interesting for inlining. */ 5652 bool 5653 possibly_inlined_p (tree decl) 5654 { 5655 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL); 5656 if (DECL_UNINLINABLE (decl)) 5657 return false; 5658 if (!optimize) 5659 return DECL_DECLARED_INLINE_P (decl); 5660 /* When optimizing, we might inline everything when flatten 5661 attribute or heuristics inlining for size or autoinlining 5662 is used. */ 5663 return true; 5664 } 5665 5666 /* If DECL is a function or variable template specialization, instantiate 5667 its definition now. */ 5668 5669 void 5670 maybe_instantiate_decl (tree decl) 5671 { 5672 if (VAR_OR_FUNCTION_DECL_P (decl) 5673 && DECL_LANG_SPECIFIC (decl) 5674 && DECL_TEMPLATE_INFO (decl) 5675 && !DECL_DECLARED_CONCEPT_P (decl) 5676 && !uses_template_parms (DECL_TI_ARGS (decl))) 5677 { 5678 /* Instantiating a function will result in garbage collection. We 5679 must treat this situation as if we were within the body of a 5680 function so as to avoid collecting live data only referenced from 5681 the stack (such as overload resolution candidates). */ 5682 ++function_depth; 5683 instantiate_decl (decl, /*defer_ok=*/false, 5684 /*expl_inst_class_mem_p=*/false); 5685 --function_depth; 5686 } 5687 } 5688 5689 /* Error if the DECL is unavailable (unless this is currently suppressed). 5690 Maybe warn if DECL is deprecated, subject to COMPLAIN. Returns true if 5691 an error or warning was emitted. */ 5692 5693 bool 5694 cp_handle_deprecated_or_unavailable (tree decl, tsubst_flags_t complain) 5695 { 5696 if (!decl) 5697 return false; 5698 5699 if ((complain & tf_error) 5700 && deprecated_state != UNAVAILABLE_DEPRECATED_SUPPRESS) 5701 { 5702 if (TREE_UNAVAILABLE (decl)) 5703 { 5704 error_unavailable_use (decl, NULL_TREE); 5705 return true; 5706 } 5707 else 5708 { 5709 /* Perhaps this is an unavailable typedef. */ 5710 if (TYPE_P (decl) 5711 && TYPE_NAME (decl) 5712 && TREE_UNAVAILABLE (TYPE_NAME (decl))) 5713 { 5714 decl = TYPE_NAME (decl); 5715 /* Don't error within members of a unavailable type. */ 5716 if (TYPE_P (decl) 5717 && currently_open_class (decl)) 5718 return false; 5719 5720 error_unavailable_use (decl, NULL_TREE); 5721 return true; 5722 } 5723 } 5724 /* Carry on to consider deprecatedness. */ 5725 } 5726 5727 if (!(complain & tf_warning) 5728 || deprecated_state == DEPRECATED_SUPPRESS 5729 || deprecated_state == UNAVAILABLE_DEPRECATED_SUPPRESS) 5730 return false; 5731 5732 if (!TREE_DEPRECATED (decl)) 5733 { 5734 /* Perhaps this is a deprecated typedef. */ 5735 if (TYPE_P (decl) && TYPE_NAME (decl)) 5736 decl = TYPE_NAME (decl); 5737 5738 if (!TREE_DEPRECATED (decl)) 5739 return false; 5740 } 5741 5742 /* Don't warn within members of a deprecated type. */ 5743 if (TYPE_P (decl) 5744 && currently_open_class (decl)) 5745 return false; 5746 5747 bool warned = false; 5748 if (cxx_dialect >= cxx11 5749 && DECL_P (decl) 5750 && DECL_ARTIFICIAL (decl) 5751 && DECL_IOBJ_MEMBER_FUNCTION_P (decl) 5752 && copy_fn_p (decl)) 5753 { 5754 /* Don't warn if the flag was disabled around the class definition 5755 (c++/94492). */ 5756 if (warning_enabled_at (DECL_SOURCE_LOCATION (decl), 5757 OPT_Wdeprecated_copy)) 5758 { 5759 auto_diagnostic_group d; 5760 tree ctx = DECL_CONTEXT (decl); 5761 tree other = classtype_has_depr_implicit_copy (ctx); 5762 int opt = (DECL_DESTRUCTOR_P (other) 5763 ? OPT_Wdeprecated_copy_dtor 5764 : OPT_Wdeprecated_copy); 5765 warned = warning (opt, "implicitly-declared %qD is deprecated", 5766 decl); 5767 if (warned) 5768 inform (DECL_SOURCE_LOCATION (other), 5769 "because %qT has user-provided %qD", 5770 ctx, other); 5771 } 5772 } 5773 else 5774 warned = warn_deprecated_use (decl, NULL_TREE); 5775 5776 return warned; 5777 } 5778 5779 /* Like above, but takes into account outer scopes. */ 5780 5781 void 5782 cp_warn_deprecated_use_scopes (tree scope) 5783 { 5784 while (scope 5785 && scope != error_mark_node 5786 && scope != global_namespace) 5787 { 5788 if ((TREE_CODE (scope) == NAMESPACE_DECL || OVERLOAD_TYPE_P (scope)) 5789 && cp_handle_deprecated_or_unavailable (scope)) 5790 return; 5791 if (TYPE_P (scope)) 5792 scope = CP_TYPE_CONTEXT (scope); 5793 else 5794 scope = CP_DECL_CONTEXT (scope); 5795 } 5796 } 5797 5798 /* True if DECL or its enclosing scope have unbound template parameters. */ 5799 5800 bool 5801 decl_dependent_p (tree decl) 5802 { 5803 if (DECL_FUNCTION_SCOPE_P (decl) 5804 || TREE_CODE (decl) == CONST_DECL 5805 || TREE_CODE (decl) == USING_DECL 5806 || TREE_CODE (decl) == FIELD_DECL) 5807 decl = CP_DECL_CONTEXT (decl); 5808 if (tree tinfo = get_template_info (decl)) 5809 if (any_dependent_template_arguments_p (TI_ARGS (tinfo))) 5810 return true; 5811 if (LAMBDA_FUNCTION_P (decl) 5812 && dependent_type_p (DECL_CONTEXT (decl))) 5813 return true; 5814 return false; 5815 } 5816 5817 /* [basic.def.odr] A function is named [and therefore odr-used] by an 5818 expression or conversion if it is the selected member of an overload set in 5819 an overload resolution performed as part of forming that expression or 5820 conversion, unless it is a pure virtual function and either the expression 5821 is not an id-expression naming the function with an explicitly qualified 5822 name or the expression forms a pointer to member. 5823 5824 Mostly, we call mark_used in places that actually do something with a 5825 function, like build_over_call. But in a few places we end up with a 5826 non-overloaded FUNCTION_DECL that we aren't going to do any more with, like 5827 convert_to_void. resolve_nondeduced_context is called in those places, 5828 but it's also called in too many other places. */ 5829 5830 bool 5831 mark_single_function (tree expr, tsubst_flags_t complain) 5832 { 5833 expr = maybe_undo_parenthesized_ref (expr); 5834 expr = tree_strip_any_location_wrapper (expr); 5835 5836 if (is_overloaded_fn (expr) == 1 5837 && !mark_used (expr, complain) 5838 && !(complain & tf_error)) 5839 return false; 5840 return true; 5841 } 5842 5843 /* Mark DECL (either a _DECL or a BASELINK) as "used" in the program. 5844 If DECL is a specialization or implicitly declared class member, 5845 generate the actual definition. Return false if something goes 5846 wrong, true otherwise. */ 5847 5848 bool 5849 mark_used (tree decl, tsubst_flags_t complain /* = tf_warning_or_error */) 5850 { 5851 /* If we're just testing conversions or resolving overloads, we 5852 don't want any permanent effects like forcing functions to be 5853 output or instantiating templates. */ 5854 if ((complain & tf_conv)) 5855 return true; 5856 5857 /* If DECL is a BASELINK for a single function, then treat it just 5858 like the DECL for the function. Otherwise, if the BASELINK is 5859 for an overloaded function, we don't know which function was 5860 actually used until after overload resolution. */ 5861 if (BASELINK_P (decl)) 5862 { 5863 tree fns = BASELINK_FUNCTIONS (decl); 5864 if (really_overloaded_fn (fns)) 5865 return true; 5866 fns = OVL_FIRST (fns); 5867 if (!mark_used (fns, complain)) 5868 return false; 5869 /* We might have deduced its return type. */ 5870 TREE_TYPE (decl) = TREE_TYPE (fns); 5871 return true; 5872 } 5873 5874 if (!DECL_P (decl)) 5875 return true; 5876 5877 /* Set TREE_USED for the benefit of -Wunused. */ 5878 TREE_USED (decl) = true; 5879 5880 /* And for structured bindings also the underlying decl. */ 5881 if (DECL_DECOMPOSITION_P (decl) && DECL_DECOMP_BASE (decl)) 5882 TREE_USED (DECL_DECOMP_BASE (decl)) = true; 5883 5884 if (TREE_CODE (decl) == TEMPLATE_DECL) 5885 return true; 5886 5887 if (DECL_CLONED_FUNCTION_P (decl)) 5888 TREE_USED (DECL_CLONED_FUNCTION (decl)) = 1; 5889 5890 /* Mark enumeration types as used. */ 5891 if (TREE_CODE (decl) == CONST_DECL) 5892 used_types_insert (DECL_CONTEXT (decl)); 5893 5894 if (TREE_CODE (decl) == FUNCTION_DECL) 5895 { 5896 if (DECL_MAYBE_DELETED (decl)) 5897 { 5898 ++function_depth; 5899 maybe_synthesize_method (decl); 5900 --function_depth; 5901 } 5902 5903 if (DECL_DELETED_FN (decl)) 5904 { 5905 if (DECL_ARTIFICIAL (decl) 5906 && DECL_CONV_FN_P (decl) 5907 && LAMBDA_TYPE_P (DECL_CONTEXT (decl))) 5908 /* We mark a lambda conversion op as deleted if we can't 5909 generate it properly; see maybe_add_lambda_conv_op. */ 5910 sorry ("converting lambda that uses %<...%> to function pointer"); 5911 else if (complain & tf_error) 5912 { 5913 error ("use of deleted function %qD", decl); 5914 if (!maybe_explain_implicit_delete (decl)) 5915 inform (DECL_SOURCE_LOCATION (decl), "declared here"); 5916 } 5917 return false; 5918 } 5919 5920 if (!maybe_instantiate_noexcept (decl, complain)) 5921 return false; 5922 } 5923 5924 if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_LOCAL_DECL_P (decl)) 5925 { 5926 if (!DECL_LANG_SPECIFIC (decl)) 5927 /* An unresolved dependent local extern. */ 5928 return true; 5929 5930 DECL_ODR_USED (decl) = 1; 5931 auto alias = DECL_LOCAL_DECL_ALIAS (decl); 5932 if (!alias || alias == error_mark_node) 5933 return true; 5934 5935 /* Process the underlying decl. */ 5936 decl = alias; 5937 TREE_USED (decl) = true; 5938 } 5939 5940 cp_handle_deprecated_or_unavailable (decl, complain); 5941 5942 /* We can only check DECL_ODR_USED on variables or functions with 5943 DECL_LANG_SPECIFIC set, and these are also the only decls that we 5944 might need special handling for. */ 5945 if (!VAR_OR_FUNCTION_DECL_P (decl) 5946 || DECL_LANG_SPECIFIC (decl) == NULL 5947 || DECL_THUNK_P (decl)) 5948 { 5949 if (!decl_dependent_p (decl) 5950 && !require_deduced_type (decl, complain)) 5951 return false; 5952 return true; 5953 } 5954 5955 /* We only want to do this processing once. We don't need to keep trying 5956 to instantiate inline templates, because unit-at-a-time will make sure 5957 we get them compiled before functions that want to inline them. */ 5958 if (DECL_ODR_USED (decl)) 5959 return true; 5960 5961 if (flag_concepts && TREE_CODE (decl) == FUNCTION_DECL 5962 && !constraints_satisfied_p (decl)) 5963 { 5964 if (complain & tf_error) 5965 { 5966 auto_diagnostic_group d; 5967 error ("use of function %qD with unsatisfied constraints", 5968 decl); 5969 location_t loc = DECL_SOURCE_LOCATION (decl); 5970 inform (loc, "declared here"); 5971 diagnose_constraints (loc, decl, NULL_TREE); 5972 } 5973 return false; 5974 } 5975 5976 /* If DECL has a deduced return type, we need to instantiate it now to 5977 find out its type. For OpenMP user defined reductions, we need them 5978 instantiated for reduction clauses which inline them by hand directly. */ 5979 if (undeduced_auto_decl (decl) 5980 || (VAR_P (decl) 5981 && VAR_HAD_UNKNOWN_BOUND (decl)) 5982 || (TREE_CODE (decl) == FUNCTION_DECL 5983 && DECL_OMP_DECLARE_REDUCTION_P (decl))) 5984 maybe_instantiate_decl (decl); 5985 5986 if (processing_template_decl || in_template_context) 5987 return true; 5988 5989 /* Check this too in case we're within instantiate_non_dependent_expr. */ 5990 if (DECL_TEMPLATE_INFO (decl) 5991 && uses_template_parms (DECL_TI_ARGS (decl))) 5992 return true; 5993 5994 if (!require_deduced_type (decl, complain)) 5995 return false; 5996 5997 if (builtin_pack_fn_p (decl)) 5998 { 5999 error ("use of built-in parameter pack %qD outside of a template", 6000 DECL_NAME (decl)); 6001 return false; 6002 } 6003 6004 /* If we don't need a value, then we don't need to synthesize DECL. */ 6005 if (cp_unevaluated_operand || in_discarded_stmt) 6006 return true; 6007 6008 DECL_ODR_USED (decl) = 1; 6009 if (DECL_CLONED_FUNCTION_P (decl)) 6010 DECL_ODR_USED (DECL_CLONED_FUNCTION (decl)) = 1; 6011 6012 /* DR 757: A type without linkage shall not be used as the type of a 6013 variable or function with linkage, unless 6014 o the variable or function has extern "C" linkage (7.5 [dcl.link]), or 6015 o the variable or function is not used (3.2 [basic.def.odr]) or is 6016 defined in the same translation unit. */ 6017 if (cxx_dialect > cxx98 6018 && decl_linkage (decl) != lk_none 6019 && !DECL_EXTERN_C_P (decl) 6020 && !DECL_ARTIFICIAL (decl) 6021 && !decl_defined_p (decl) 6022 && no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false)) 6023 vec_safe_push (no_linkage_decls, decl); 6024 6025 if (TREE_CODE (decl) == FUNCTION_DECL 6026 && DECL_DECLARED_INLINE_P (decl) 6027 && !DECL_INITIAL (decl) 6028 && !DECL_ARTIFICIAL (decl) 6029 && !DECL_PURE_VIRTUAL_P (decl)) 6030 /* Remember it, so we can check it was defined. */ 6031 note_vague_linkage_fn (decl); 6032 6033 /* Is it a synthesized method that needs to be synthesized? */ 6034 if (TREE_CODE (decl) == FUNCTION_DECL 6035 && DECL_DEFAULTED_FN (decl) 6036 /* A function defaulted outside the class is synthesized either by 6037 cp_finish_decl or instantiate_decl. */ 6038 && !DECL_DEFAULTED_OUTSIDE_CLASS_P (decl) 6039 && ! DECL_INITIAL (decl)) 6040 { 6041 /* Remember the current location for a function we will end up 6042 synthesizing. Then we can inform the user where it was 6043 required in the case of error. */ 6044 if (decl_remember_implicit_trigger_p (decl)) 6045 DECL_SOURCE_LOCATION (decl) = input_location; 6046 6047 /* Synthesizing an implicitly defined member function will result in 6048 garbage collection. We must treat this situation as if we were 6049 within the body of a function so as to avoid collecting live data 6050 on the stack (such as overload resolution candidates). 6051 6052 We could just let c_parse_final_cleanups handle synthesizing 6053 this function by adding it to deferred_fns, but doing 6054 it at the use site produces better error messages. */ 6055 ++function_depth; 6056 synthesize_method (decl); 6057 --function_depth; 6058 /* If this is a synthesized method we don't need to 6059 do the instantiation test below. */ 6060 } 6061 else if (VAR_OR_FUNCTION_DECL_P (decl) 6062 && DECL_TEMPLATE_INFO (decl) 6063 && !DECL_DECLARED_CONCEPT_P (decl) 6064 && (!DECL_EXPLICIT_INSTANTIATION (decl) 6065 || always_instantiate_p (decl))) 6066 /* If this is a function or variable that is an instance of some 6067 template, we now know that we will need to actually do the 6068 instantiation. We check that DECL is not an explicit 6069 instantiation because that is not checked in instantiate_decl. 6070 6071 We put off instantiating functions in order to improve compile 6072 times. Maintaining a stack of active functions is expensive, 6073 and the inliner knows to instantiate any functions it might 6074 need. Therefore, we always try to defer instantiation. */ 6075 { 6076 ++function_depth; 6077 instantiate_decl (decl, /*defer_ok=*/true, 6078 /*expl_inst_class_mem_p=*/false); 6079 --function_depth; 6080 } 6081 6082 return true; 6083 } 6084 6085 tree 6086 vtv_start_verification_constructor_init_function (void) 6087 { 6088 return start_objects (/*initp=*/true, MAX_RESERVED_INIT_PRIORITY - 1, true); 6089 } 6090 6091 tree 6092 vtv_finish_verification_constructor_init_function (tree body) 6093 { 6094 return finish_objects (/*initp=*/true, MAX_RESERVED_INIT_PRIORITY - 1, body); 6095 } 6096 6097 #include "gt-cp-decl2.h" 6098