1 /* Process declarations and variables for -*- C++ -*- compiler. 2 Copyright (C) 1988-2024 Free Software Foundation, Inc. 3 Contributed 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 #define INCLUDE_MEMORY 31 #include "system.h" 32 #include "coretypes.h" 33 #include "target.h" 34 #include "c-family/c-target.h" 35 #include "cp-tree.h" 36 #include "timevar.h" 37 #include "stringpool.h" 38 #include "cgraph.h" 39 #include "stor-layout.h" 40 #include "varasm.h" 41 #include "attribs.h" 42 #include "flags.h" 43 #include "tree-iterator.h" 44 #include "decl.h" 45 #include "intl.h" 46 #include "toplev.h" 47 #include "c-family/c-objc.h" 48 #include "c-family/c-pragma.h" 49 #include "c-family/c-ubsan.h" 50 #include "cp/cp-name-hint.h" 51 #include "debug.h" 52 #include "plugin.h" 53 #include "builtins.h" 54 #include "gimplify.h" 55 #include "asan.h" 56 #include "gcc-rich-location.h" 57 #include "langhooks.h" 58 #include "context.h" /* For 'g'. */ 59 #include "omp-general.h" 60 #include "omp-offload.h" /* For offload_vars. */ 61 #include "opts.h" 62 #include "langhooks-def.h" /* For lhd_simulate_record_decl */ 63 64 /* Possible cases of bad specifiers type used by bad_specifiers. */ 65 enum bad_spec_place { 66 BSP_VAR, /* variable */ 67 BSP_PARM, /* parameter */ 68 BSP_TYPE, /* type */ 69 BSP_FIELD /* field */ 70 }; 71 72 static const char *redeclaration_error_message (tree, tree); 73 74 static bool decl_jump_unsafe (tree); 75 static void require_complete_types_for_parms (tree); 76 static tree grok_reference_init (tree, tree, tree, int); 77 static tree grokvardecl (tree, tree, tree, const cp_decl_specifier_seq *, 78 int, int, int, bool, int, tree, location_t); 79 static void check_static_variable_definition (tree, tree); 80 static void record_unknown_type (tree, const char *); 81 static int member_function_or_else (tree, tree, enum overload_flags); 82 static tree local_variable_p_walkfn (tree *, int *, void *); 83 static const char *tag_name (enum tag_types); 84 static tree lookup_and_check_tag (enum tag_types, tree, TAG_how, bool); 85 static void maybe_deduce_size_from_array_init (tree, tree); 86 static void layout_var_decl (tree); 87 static tree check_initializer (tree, tree, int, vec<tree, va_gc> **); 88 static void make_rtl_for_nonlocal_decl (tree, tree, const char *); 89 static void copy_type_enum (tree , tree); 90 static void check_function_type (tree, tree); 91 static void begin_destructor_body (void); 92 static void record_key_method_defined (tree); 93 static tree create_array_type_for_decl (tree, tree, tree, location_t); 94 static tree get_atexit_node (void); 95 static tree get_dso_handle_node (void); 96 static tree start_cleanup_fn (bool); 97 static void end_cleanup_fn (void); 98 static tree cp_make_fname_decl (location_t, tree, int); 99 static void initialize_predefined_identifiers (void); 100 static tree check_special_function_return_type 101 (special_function_kind, tree, tree, int, const location_t*); 102 static tree push_cp_library_fn (enum tree_code, tree, int); 103 static tree build_cp_library_fn (tree, enum tree_code, tree, int); 104 static void store_parm_decls (tree); 105 static void initialize_local_var (tree, tree); 106 static void expand_static_init (tree, tree); 107 static location_t smallest_type_location (const cp_decl_specifier_seq*); 108 static bool identify_goto (tree, location_t, const location_t *, 109 diagnostic_t, bool); 110 111 /* The following symbols are subsumed in the cp_global_trees array, and 112 listed here individually for documentation purposes. 113 114 C++ extensions 115 tree wchar_decl_node; 116 117 tree vtable_entry_type; 118 tree delta_type_node; 119 tree __t_desc_type_node; 120 121 tree class_type_node; 122 tree unknown_type_node; 123 124 Array type `vtable_entry_type[]' 125 126 tree vtbl_type_node; 127 tree vtbl_ptr_type_node; 128 129 Namespaces, 130 131 tree std_node; 132 tree abi_node; 133 134 A FUNCTION_DECL which can call `abort'. Not necessarily the 135 one that the user will declare, but sufficient to be called 136 by routines that want to abort the program. 137 138 tree abort_fndecl; 139 140 Used by RTTI 141 tree type_info_type_node, tinfo_decl_id, tinfo_decl_type; 142 tree tinfo_var_id; */ 143 144 tree cp_global_trees[CPTI_MAX]; 145 146 /* A list of objects which have constructors or destructors 147 which reside in namespace scope. The decl is stored in 148 the TREE_VALUE slot and the initializer is stored 149 in the TREE_PURPOSE slot. */ 150 tree static_aggregates; 151 152 /* Like static_aggregates, but for thread_local variables. */ 153 tree tls_aggregates; 154 155 /* A hash-map mapping from variable decls to the dynamic initializer for 156 the decl. This is currently only used by OpenMP. */ 157 decl_tree_map *dynamic_initializers; 158 159 /* -- end of C++ */ 160 161 /* A node for the integer constant 2. */ 162 163 tree integer_two_node; 164 165 /* vector of static decls. */ 166 vec<tree, va_gc> *static_decls; 167 168 /* vector of keyed classes. */ 169 vec<tree, va_gc> *keyed_classes; 170 171 /* Used only for jumps to as-yet undefined labels, since jumps to 172 defined labels can have their validity checked immediately. */ 173 174 struct GTY((chain_next ("%h.next"))) named_label_use_entry { 175 struct named_label_use_entry *next; 176 /* The binding level to which this entry is *currently* attached. 177 This is initially the binding level in which the goto appeared, 178 but is modified as scopes are closed. */ 179 cp_binding_level *binding_level; 180 /* The head of the names list that was current when the goto appeared, 181 or the inner scope popped. These are the decls that will *not* be 182 skipped when jumping to the label. */ 183 tree names_in_scope; 184 /* If the use is a possible destination of a computed goto, a vec of decls 185 that aren't destroyed, filled in by poplevel_named_label_1. */ 186 vec<tree,va_gc> *computed_goto; 187 /* The location of the goto, for error reporting. */ 188 location_t o_goto_locus; 189 /* True if an OpenMP structured block scope has been closed since 190 the goto appeared. This means that the branch from the label will 191 illegally exit an OpenMP scope. */ 192 bool in_omp_scope; 193 }; 194 195 /* A list of all LABEL_DECLs in the function that have names. Here so 196 we can clear out their names' definitions at the end of the 197 function, and so we can check the validity of jumps to these labels. */ 198 199 struct GTY((for_user)) named_label_entry { 200 201 tree name; /* Name of decl. */ 202 203 tree label_decl; /* LABEL_DECL, unless deleted local label. */ 204 205 named_label_entry *outer; /* Outer shadowed chain. */ 206 207 /* The binding level to which the label is *currently* attached. 208 This is initially set to the binding level in which the label 209 is defined, but is modified as scopes are closed. */ 210 cp_binding_level *binding_level; 211 212 /* The head of the names list that was current when the label was 213 defined, or the inner scope popped. These are the decls that will 214 be skipped when jumping to the label. */ 215 tree names_in_scope; 216 217 /* A vector of all decls from all binding levels that would be 218 crossed by a backward branch to the label. */ 219 vec<tree, va_gc> *bad_decls; 220 221 /* A list of uses of the label, before the label is defined. */ 222 named_label_use_entry *uses; 223 224 /* True if we've seen &&label. Appalently we can't use TREE_ADDRESSABLE for 225 this, it has a more specific meaning for LABEL_DECL. */ 226 bool addressed; 227 228 /* The following bits are set after the label is defined, and are 229 updated as scopes are popped. They indicate that a jump to the 230 label will illegally enter a scope of the given flavor. */ 231 bool in_try_scope; 232 bool in_catch_scope; 233 bool in_omp_scope; 234 bool in_transaction_scope; 235 bool in_constexpr_if; 236 bool in_consteval_if; 237 bool in_stmt_expr; 238 }; 239 240 #define named_labels cp_function_chain->x_named_labels 241 242 /* The number of function bodies which we are currently processing. 244 (Zero if we are at namespace scope, one inside the body of a 245 function, two inside the body of a function in a local class, etc.) */ 246 int function_depth; 247 248 /* Whether the exception-specifier is part of a function type (i.e. C++17). */ 249 bool flag_noexcept_type; 250 251 /* States indicating how grokdeclarator() should handle declspecs marked 252 with __attribute__((deprecated)). An object declared as 253 __attribute__((deprecated)) suppresses warnings of uses of other 254 deprecated items. */ 255 enum deprecated_states deprecated_state = DEPRECATED_NORMAL; 256 257 258 /* A list of VAR_DECLs whose type was incomplete at the time the 260 variable was declared. */ 261 262 struct GTY(()) incomplete_var { 263 tree decl; 264 tree incomplete_type; 265 }; 266 267 268 static GTY(()) vec<incomplete_var, va_gc> *incomplete_vars; 269 270 /* Returns the kind of template specialization we are currently 272 processing, given that it's declaration contained N_CLASS_SCOPES 273 explicit scope qualifications. */ 274 275 tmpl_spec_kind 276 current_tmpl_spec_kind (int n_class_scopes) 277 { 278 int n_template_parm_scopes = 0; 279 int seen_specialization_p = 0; 280 int innermost_specialization_p = 0; 281 cp_binding_level *b; 282 283 /* Scan through the template parameter scopes. */ 284 for (b = current_binding_level; 285 b->kind == sk_template_parms; 286 b = b->level_chain) 287 { 288 /* If we see a specialization scope inside a parameter scope, 289 then something is wrong. That corresponds to a declaration 290 like: 291 292 template <class T> template <> ... 293 294 which is always invalid since [temp.expl.spec] forbids the 295 specialization of a class member template if the enclosing 296 class templates are not explicitly specialized as well. */ 297 if (b->explicit_spec_p) 298 { 299 if (n_template_parm_scopes == 0) 300 innermost_specialization_p = 1; 301 else 302 seen_specialization_p = 1; 303 } 304 else if (seen_specialization_p == 1) 305 return tsk_invalid_member_spec; 306 307 ++n_template_parm_scopes; 308 } 309 310 /* Handle explicit instantiations. */ 311 if (processing_explicit_instantiation) 312 { 313 if (n_template_parm_scopes != 0) 314 /* We've seen a template parameter list during an explicit 315 instantiation. For example: 316 317 template <class T> template void f(int); 318 319 This is erroneous. */ 320 return tsk_invalid_expl_inst; 321 else 322 return tsk_expl_inst; 323 } 324 325 if (n_template_parm_scopes < n_class_scopes) 326 /* We've not seen enough template headers to match all the 327 specialized classes present. For example: 328 329 template <class T> void R<T>::S<T>::f(int); 330 331 This is invalid; there needs to be one set of template 332 parameters for each class. */ 333 return tsk_insufficient_parms; 334 else if (n_template_parm_scopes == n_class_scopes) 335 /* We're processing a non-template declaration (even though it may 336 be a member of a template class.) For example: 337 338 template <class T> void S<T>::f(int); 339 340 The `class T' matches the `S<T>', leaving no template headers 341 corresponding to the `f'. */ 342 return tsk_none; 343 else if (n_template_parm_scopes > n_class_scopes + 1) 344 /* We've got too many template headers. For example: 345 346 template <> template <class T> void f (T); 347 348 There need to be more enclosing classes. */ 349 return tsk_excessive_parms; 350 else 351 /* This must be a template. It's of the form: 352 353 template <class T> template <class U> void S<T>::f(U); 354 355 This is a specialization if the innermost level was a 356 specialization; otherwise it's just a definition of the 357 template. */ 358 return innermost_specialization_p ? tsk_expl_spec : tsk_template; 359 } 360 361 /* Exit the current scope. */ 362 363 void 364 finish_scope (void) 365 { 366 poplevel (0, 0, 0); 367 } 368 369 /* When a label goes out of scope, check to see if that label was used 370 in a valid manner, and issue any appropriate warnings or errors. */ 371 372 static void 373 check_label_used (tree label) 374 { 375 if (!processing_template_decl) 376 { 377 if (DECL_INITIAL (label) == NULL_TREE) 378 { 379 location_t location; 380 381 error ("label %q+D used but not defined", label); 382 location = input_location; 383 /* FIXME want (LOCATION_FILE (input_location), (line)0) */ 384 /* Avoid crashing later. */ 385 define_label (location, DECL_NAME (label)); 386 } 387 else 388 warn_for_unused_label (label); 389 } 390 } 391 392 /* Helper function to sort named label entries in a vector by DECL_UID. */ 393 394 static int 395 sort_labels (const void *a, const void *b) 396 { 397 tree label1 = *(tree const *) a; 398 tree label2 = *(tree const *) b; 399 400 /* DECL_UIDs can never be equal. */ 401 return DECL_UID (label1) > DECL_UID (label2) ? -1 : +1; 402 } 403 404 /* At the end of a function, all labels declared within the function 405 go out of scope. BLOCK is the top-level block for the 406 function. */ 407 408 static void 409 pop_labels (tree block) 410 { 411 if (!named_labels) 412 return; 413 414 /* We need to add the labels to the block chain, so debug 415 information is emitted. But, we want the order to be stable so 416 need to sort them first. Otherwise the debug output could be 417 randomly ordered. I guess it's mostly stable, unless the hash 418 table implementation changes. */ 419 auto_vec<tree, 32> labels (named_labels->elements ()); 420 hash_table<named_label_hash>::iterator end (named_labels->end ()); 421 for (hash_table<named_label_hash>::iterator iter 422 (named_labels->begin ()); iter != end; ++iter) 423 { 424 named_label_entry *ent = *iter; 425 426 gcc_checking_assert (!ent->outer); 427 if (ent->label_decl) 428 labels.quick_push (ent->label_decl); 429 ggc_free (ent); 430 } 431 named_labels = NULL; 432 labels.qsort (sort_labels); 433 434 while (labels.length ()) 435 { 436 tree label = labels.pop (); 437 438 DECL_CHAIN (label) = BLOCK_VARS (block); 439 BLOCK_VARS (block) = label; 440 441 check_label_used (label); 442 } 443 } 444 445 /* At the end of a block with local labels, restore the outer definition. */ 446 447 static void 448 pop_local_label (tree id, tree label) 449 { 450 check_label_used (label); 451 named_label_entry **slot = named_labels->find_slot_with_hash 452 (id, IDENTIFIER_HASH_VALUE (id), NO_INSERT); 453 named_label_entry *ent = *slot; 454 455 if (ent->outer) 456 ent = ent->outer; 457 else 458 { 459 ent = ggc_cleared_alloc<named_label_entry> (); 460 ent->name = id; 461 } 462 *slot = ent; 463 } 464 465 /* The following two routines are used to interface to Objective-C++. 466 The binding level is purposely treated as an opaque type. */ 467 468 void * 469 objc_get_current_scope (void) 470 { 471 return current_binding_level; 472 } 473 474 /* The following routine is used by the NeXT-style SJLJ exceptions; 475 variables get marked 'volatile' so as to not be clobbered by 476 _setjmp()/_longjmp() calls. All variables in the current scope, 477 as well as parent scopes up to (but not including) ENCLOSING_BLK 478 shall be thusly marked. */ 479 480 void 481 objc_mark_locals_volatile (void *enclosing_blk) 482 { 483 cp_binding_level *scope; 484 485 for (scope = current_binding_level; 486 scope && scope != enclosing_blk; 487 scope = scope->level_chain) 488 { 489 tree decl; 490 491 for (decl = scope->names; decl; decl = TREE_CHAIN (decl)) 492 objc_volatilize_decl (decl); 493 494 /* Do not climb up past the current function. */ 495 if (scope->kind == sk_function_parms) 496 break; 497 } 498 } 499 500 /* True if B is the level for the condition of a constexpr if. */ 501 502 static bool 503 level_for_constexpr_if (cp_binding_level *b) 504 { 505 return (b->kind == sk_cond && b->this_entity 506 && TREE_CODE (b->this_entity) == IF_STMT 507 && IF_STMT_CONSTEXPR_P (b->this_entity)); 508 } 509 510 /* True if B is the level for the condition of a consteval if. */ 511 512 static bool 513 level_for_consteval_if (cp_binding_level *b) 514 { 515 return (b->kind == sk_cond && b->this_entity 516 && TREE_CODE (b->this_entity) == IF_STMT 517 && IF_STMT_CONSTEVAL_P (b->this_entity)); 518 } 519 520 /* Update data for defined and undefined labels when leaving a scope. */ 521 522 int 523 poplevel_named_label_1 (named_label_entry **slot, cp_binding_level *bl) 524 { 525 named_label_entry *ent = *slot; 526 cp_binding_level *obl = bl->level_chain; 527 528 if (ent->binding_level == bl) 529 { 530 tree decl; 531 532 /* ENT->NAMES_IN_SCOPE may contain a mixture of DECLs and 533 TREE_LISTs representing OVERLOADs, so be careful. */ 534 for (decl = ent->names_in_scope; decl; decl = (DECL_P (decl) 535 ? DECL_CHAIN (decl) 536 : TREE_CHAIN (decl))) 537 if (decl_jump_unsafe (decl)) 538 vec_safe_push (ent->bad_decls, decl); 539 540 ent->binding_level = obl; 541 ent->names_in_scope = obl->names; 542 switch (bl->kind) 543 { 544 case sk_try: 545 ent->in_try_scope = true; 546 break; 547 case sk_catch: 548 ent->in_catch_scope = true; 549 break; 550 case sk_omp: 551 ent->in_omp_scope = true; 552 break; 553 case sk_transaction: 554 ent->in_transaction_scope = true; 555 break; 556 case sk_stmt_expr: 557 ent->in_stmt_expr = true; 558 break; 559 case sk_block: 560 if (level_for_constexpr_if (bl->level_chain)) 561 ent->in_constexpr_if = true; 562 else if (level_for_consteval_if (bl->level_chain)) 563 ent->in_consteval_if = true; 564 break; 565 default: 566 break; 567 } 568 } 569 else if (ent->uses) 570 { 571 struct named_label_use_entry *use; 572 573 for (use = ent->uses; use ; use = use->next) 574 if (use->binding_level == bl) 575 { 576 if (auto &cg = use->computed_goto) 577 { 578 if (bl->kind == sk_catch) 579 vec_safe_push (cg, get_identifier ("catch")); 580 for (tree d = use->names_in_scope; d; d = DECL_CHAIN (d)) 581 if (TREE_CODE (d) == VAR_DECL && !TREE_STATIC (d) 582 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (d))) 583 vec_safe_push (cg, d); 584 } 585 586 use->binding_level = obl; 587 use->names_in_scope = obl->names; 588 if (bl->kind == sk_omp) 589 use->in_omp_scope = true; 590 } 591 } 592 593 return 1; 594 } 595 596 /* Saved errorcount to avoid -Wunused-but-set-{parameter,variable} warnings 597 when errors were reported, except for -Werror-unused-but-set-*. */ 598 static int unused_but_set_errorcount; 599 600 /* Exit a binding level. 601 Pop the level off, and restore the state of the identifier-decl mappings 602 that were in effect when this level was entered. 603 604 If KEEP == 1, this level had explicit declarations, so 605 and create a "block" (a BLOCK node) for the level 606 to record its declarations and subblocks for symbol table output. 607 608 If FUNCTIONBODY is nonzero, this level is the body of a function, 609 so create a block as if KEEP were set and also clear out all 610 label names. 611 612 If REVERSE is nonzero, reverse the order of decls before putting 613 them into the BLOCK. */ 614 615 tree 616 poplevel (int keep, int reverse, int functionbody) 617 { 618 tree link; 619 /* The chain of decls was accumulated in reverse order. 620 Put it into forward order, just for cleanliness. */ 621 tree decls; 622 tree subblocks; 623 tree block; 624 tree decl; 625 scope_kind kind; 626 627 auto_cond_timevar tv (TV_NAME_LOOKUP); 628 restart: 629 630 block = NULL_TREE; 631 632 gcc_assert (current_binding_level->kind != sk_class 633 && current_binding_level->kind != sk_namespace); 634 635 if (current_binding_level->kind == sk_cleanup) 636 functionbody = 0; 637 subblocks = functionbody >= 0 ? current_binding_level->blocks : 0; 638 639 gcc_assert (!vec_safe_length (current_binding_level->class_shadowed)); 640 641 /* We used to use KEEP == 2 to indicate that the new block should go 642 at the beginning of the list of blocks at this binding level, 643 rather than the end. This hack is no longer used. */ 644 gcc_assert (keep == 0 || keep == 1); 645 646 if (current_binding_level->keep) 647 keep = 1; 648 649 /* Any uses of undefined labels, and any defined labels, now operate 650 under constraints of next binding contour. */ 651 if (cfun && !functionbody && named_labels) 652 named_labels->traverse<cp_binding_level *, poplevel_named_label_1> 653 (current_binding_level); 654 655 /* Get the decls in the order they were written. 656 Usually current_binding_level->names is in reverse order. 657 But parameter decls were previously put in forward order. */ 658 659 decls = current_binding_level->names; 660 if (reverse) 661 { 662 decls = nreverse (decls); 663 current_binding_level->names = decls; 664 } 665 666 /* If there were any declarations or structure tags in that level, 667 or if this level is a function body, 668 create a BLOCK to record them for the life of this function. */ 669 block = NULL_TREE; 670 /* Avoid function body block if possible. */ 671 if (functionbody && subblocks && BLOCK_CHAIN (subblocks) == NULL_TREE) 672 keep = 0; 673 else if (keep == 1 || functionbody) 674 block = make_node (BLOCK); 675 if (block != NULL_TREE) 676 { 677 BLOCK_VARS (block) = decls; 678 BLOCK_SUBBLOCKS (block) = subblocks; 679 } 680 681 /* In each subblock, record that this is its superior. */ 682 if (keep >= 0) 683 for (link = subblocks; link; link = BLOCK_CHAIN (link)) 684 BLOCK_SUPERCONTEXT (link) = block; 685 686 /* Before we remove the declarations first check for unused variables. */ 687 if ((warn_unused_variable || warn_unused_but_set_variable) 688 && current_binding_level->kind != sk_template_parms 689 && !processing_template_decl) 690 for (tree d = get_local_decls (); d; d = TREE_CHAIN (d)) 691 { 692 /* There are cases where D itself is a TREE_LIST. See in 693 push_local_binding where the list of decls returned by 694 getdecls is built. */ 695 decl = TREE_CODE (d) == TREE_LIST ? TREE_VALUE (d) : d; 696 697 tree type = TREE_TYPE (decl); 698 if (VAR_P (decl) 699 && (! TREE_USED (decl) || !DECL_READ_P (decl)) 700 && ! DECL_IN_SYSTEM_HEADER (decl) 701 /* For structured bindings, consider only real variables, not 702 subobjects. */ 703 && (DECL_DECOMPOSITION_P (decl) ? !DECL_DECOMP_BASE (decl) 704 : (DECL_NAME (decl) && !DECL_ARTIFICIAL (decl))) 705 /* Don't warn about name-independent declarations. */ 706 && !name_independent_decl_p (decl) 707 && type != error_mark_node 708 && (!CLASS_TYPE_P (type) 709 || !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type) 710 || lookup_attribute ("warn_unused", 711 TYPE_ATTRIBUTES (TREE_TYPE (decl))))) 712 { 713 if (! TREE_USED (decl)) 714 { 715 if (!DECL_NAME (decl) && DECL_DECOMPOSITION_P (decl)) 716 warning_at (DECL_SOURCE_LOCATION (decl), 717 OPT_Wunused_variable, 718 "unused structured binding declaration"); 719 else 720 warning_at (DECL_SOURCE_LOCATION (decl), 721 OPT_Wunused_variable, "unused variable %qD", decl); 722 suppress_warning (decl, OPT_Wunused_variable); 723 } 724 else if (DECL_CONTEXT (decl) == current_function_decl 725 // For -Wunused-but-set-variable leave references alone. 726 && !TYPE_REF_P (TREE_TYPE (decl)) 727 && errorcount == unused_but_set_errorcount) 728 { 729 if (!DECL_NAME (decl) && DECL_DECOMPOSITION_P (decl)) 730 warning_at (DECL_SOURCE_LOCATION (decl), 731 OPT_Wunused_but_set_variable, "structured " 732 "binding declaration set but not used"); 733 else 734 warning_at (DECL_SOURCE_LOCATION (decl), 735 OPT_Wunused_but_set_variable, 736 "variable %qD set but not used", decl); 737 unused_but_set_errorcount = errorcount; 738 } 739 } 740 } 741 742 /* Remove declarations for all the DECLs in this level. */ 743 for (link = decls; link; link = TREE_CHAIN (link)) 744 { 745 tree name; 746 if (TREE_CODE (link) == TREE_LIST) 747 { 748 decl = TREE_VALUE (link); 749 name = TREE_PURPOSE (link); 750 gcc_checking_assert (name); 751 } 752 else 753 { 754 decl = link; 755 name = DECL_NAME (decl); 756 } 757 758 /* Remove the binding. */ 759 if (TREE_CODE (decl) == LABEL_DECL) 760 pop_local_label (name, decl); 761 else 762 pop_local_binding (name, decl); 763 } 764 765 /* Restore the IDENTIFIER_TYPE_VALUEs. */ 766 for (link = current_binding_level->type_shadowed; 767 link; link = TREE_CHAIN (link)) 768 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (link), TREE_VALUE (link)); 769 770 /* There may be OVERLOADs (wrapped in TREE_LISTs) on the BLOCK_VARs 771 list if a `using' declaration put them there. The debugging 772 back ends won't understand OVERLOAD, so we remove them here. 773 Because the BLOCK_VARS are (temporarily) shared with 774 CURRENT_BINDING_LEVEL->NAMES we must do this fixup after we have 775 popped all the bindings. Also remove undeduced 'auto' decls, 776 which LTO doesn't understand, and can't have been used by anything. */ 777 if (block) 778 { 779 tree* d; 780 781 for (d = &BLOCK_VARS (block); *d; ) 782 { 783 if (TREE_CODE (*d) == TREE_LIST 784 || (!processing_template_decl 785 && undeduced_auto_decl (*d))) 786 *d = TREE_CHAIN (*d); 787 else 788 d = &DECL_CHAIN (*d); 789 } 790 } 791 792 /* If the level being exited is the top level of a function, 793 check over all the labels. */ 794 if (functionbody) 795 { 796 if (block) 797 { 798 /* Since this is the top level block of a function, the vars are 799 the function's parameters. Don't leave them in the BLOCK 800 because they are found in the FUNCTION_DECL instead. */ 801 BLOCK_VARS (block) = 0; 802 pop_labels (block); 803 } 804 else 805 pop_labels (subblocks); 806 } 807 808 kind = current_binding_level->kind; 809 if (kind == sk_cleanup) 810 { 811 tree stmt; 812 813 /* If this is a temporary binding created for a cleanup, then we'll 814 have pushed a statement list level. Pop that, create a new 815 BIND_EXPR for the block, and insert it into the stream. */ 816 stmt = pop_stmt_list (current_binding_level->statement_list); 817 stmt = c_build_bind_expr (input_location, block, stmt); 818 add_stmt (stmt); 819 } 820 821 leave_scope (); 822 if (functionbody) 823 { 824 /* The current function is being defined, so its DECL_INITIAL 825 should be error_mark_node. */ 826 gcc_assert (DECL_INITIAL (current_function_decl) == error_mark_node); 827 DECL_INITIAL (current_function_decl) = block ? block : subblocks; 828 if (subblocks) 829 { 830 if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl)) 831 { 832 if (BLOCK_SUBBLOCKS (subblocks)) 833 BLOCK_OUTER_CURLY_BRACE_P (BLOCK_SUBBLOCKS (subblocks)) = 1; 834 } 835 else 836 BLOCK_OUTER_CURLY_BRACE_P (subblocks) = 1; 837 } 838 } 839 else if (block) 840 current_binding_level->blocks 841 = block_chainon (current_binding_level->blocks, block); 842 843 /* If we did not make a block for the level just exited, 844 any blocks made for inner levels 845 (since they cannot be recorded as subblocks in that level) 846 must be carried forward so they will later become subblocks 847 of something else. */ 848 else if (subblocks) 849 current_binding_level->blocks 850 = block_chainon (current_binding_level->blocks, subblocks); 851 852 /* Each and every BLOCK node created here in `poplevel' is important 853 (e.g. for proper debugging information) so if we created one 854 earlier, mark it as "used". */ 855 if (block) 856 TREE_USED (block) = 1; 857 858 /* All temporary bindings created for cleanups are popped silently. */ 859 if (kind == sk_cleanup) 860 goto restart; 861 862 return block; 863 } 864 865 /* Call wrapup_globals_declarations for the globals in NAMESPACE. */ 866 /* Diagnose odr-used extern inline variables without definitions 867 in the current TU. */ 868 869 int 870 wrapup_namespace_globals () 871 { 872 if (vec<tree, va_gc> *statics = static_decls) 873 { 874 for (tree decl : *statics) 875 { 876 if (warn_unused_function 877 && TREE_CODE (decl) == FUNCTION_DECL 878 && DECL_INITIAL (decl) == 0 879 && DECL_EXTERNAL (decl) 880 && !TREE_PUBLIC (decl) 881 && !DECL_ARTIFICIAL (decl) 882 && !DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl) 883 && !warning_suppressed_p (decl, OPT_Wunused_function)) 884 warning_at (DECL_SOURCE_LOCATION (decl), 885 OPT_Wunused_function, 886 "%qF declared %<static%> but never defined", decl); 887 888 if (VAR_P (decl) 889 && DECL_EXTERNAL (decl) 890 && DECL_INLINE_VAR_P (decl) 891 && DECL_ODR_USED (decl)) 892 error_at (DECL_SOURCE_LOCATION (decl), 893 "odr-used inline variable %qD is not defined", decl); 894 } 895 896 /* Clear out the list, so we don't rescan next time. */ 897 static_decls = NULL; 898 899 /* Write out any globals that need to be output. */ 900 return wrapup_global_declarations (statics->address (), 901 statics->length ()); 902 } 903 return 0; 904 } 905 906 /* In C++, you don't have to write `struct S' to refer to `S'; you 908 can just use `S'. We accomplish this by creating a TYPE_DECL as 909 if the user had written `typedef struct S S'. Create and return 910 the TYPE_DECL for TYPE. */ 911 912 tree 913 create_implicit_typedef (tree name, tree type) 914 { 915 tree decl; 916 917 decl = build_decl (input_location, TYPE_DECL, name, type); 918 DECL_ARTIFICIAL (decl) = 1; 919 /* There are other implicit type declarations, like the one *within* 920 a class that allows you to write `S::S'. We must distinguish 921 amongst these. */ 922 SET_DECL_IMPLICIT_TYPEDEF_P (decl); 923 TYPE_NAME (type) = decl; 924 TYPE_STUB_DECL (type) = decl; 925 926 return decl; 927 } 928 929 /* Function-scope local entities that need discriminators. Each entry 930 is a {decl,name} pair. VAR_DECLs for anon unions get their name 931 smashed, so we cannot rely on DECL_NAME. */ 932 933 static GTY((deletable)) vec<tree, va_gc> *local_entities; 934 935 /* Determine the mangling discriminator of local DECL. There are 936 generally very few of these in any particular function. */ 937 938 void 939 determine_local_discriminator (tree decl, tree name) 940 { 941 auto_cond_timevar tv (TV_NAME_LOOKUP); 942 retrofit_lang_decl (decl); 943 tree ctx = DECL_CONTEXT (decl); 944 size_t nelts = vec_safe_length (local_entities); 945 if (name == NULL_TREE) 946 name = (TREE_CODE (decl) == TYPE_DECL 947 && TYPE_UNNAMED_P (TREE_TYPE (decl)) 948 ? NULL_TREE : DECL_NAME (decl)); 949 for (size_t i = 0; i < nelts; i += 2) 950 { 951 tree *pair = &(*local_entities)[i]; 952 tree d = pair[0]; 953 tree n = pair[1]; 954 gcc_checking_assert (d != decl); 955 if (name == n 956 && TREE_CODE (decl) == TREE_CODE (d) 957 && ctx == DECL_CONTEXT (d)) 958 { 959 tree disc = integer_one_node; 960 if (DECL_DISCRIMINATOR (d)) 961 disc = build_int_cst (TREE_TYPE (disc), 962 TREE_INT_CST_LOW (DECL_DISCRIMINATOR (d)) + 1); 963 DECL_DISCRIMINATOR (decl) = disc; 964 /* Replace the saved decl. */ 965 pair[0] = decl; 966 decl = NULL_TREE; 967 break; 968 } 969 } 970 971 if (decl) 972 { 973 vec_safe_reserve (local_entities, 2); 974 local_entities->quick_push (decl); 975 local_entities->quick_push (name); 976 } 977 } 978 979 980 /* True if DECL is a constrained hidden friend as per [temp.friend]/9: 982 983 A non-template friend declaration with a requires-clause shall be a 984 definition. A friend function template with a constraint that depends on a 985 template parameter from an enclosing template shall be a definition. Such a 986 constrained friend function or function template declaration does not 987 declare the same function or function template as a declaration in any other 988 scope. 989 990 The ABI calls this a "member-like constrained friend" and mangles it like a 991 member function to avoid collisions. */ 992 993 bool 994 member_like_constrained_friend_p (tree decl) 995 { 996 return (TREE_CODE (decl) == FUNCTION_DECL 997 && DECL_UNIQUE_FRIEND_P (decl) 998 && DECL_FRIEND_CONTEXT (decl) 999 && get_constraints (decl) 1000 && (!DECL_TEMPLATE_INFO (decl) 1001 || !PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)) 1002 || (uses_outer_template_parms_in_constraints 1003 (most_general_template (decl))))); 1004 } 1005 1006 /* Returns true if functions FN1 and FN2 have equivalent trailing 1007 requires clauses. */ 1008 1009 static bool 1010 function_requirements_equivalent_p (tree newfn, tree oldfn) 1011 { 1012 /* In the concepts TS, the combined constraints are compared. */ 1013 if (cxx_dialect < cxx20) 1014 { 1015 tree ci1 = get_constraints (oldfn); 1016 tree ci2 = get_constraints (newfn); 1017 tree req1 = ci1 ? CI_ASSOCIATED_CONSTRAINTS (ci1) : NULL_TREE; 1018 tree req2 = ci2 ? CI_ASSOCIATED_CONSTRAINTS (ci2) : NULL_TREE; 1019 return cp_tree_equal (req1, req2); 1020 } 1021 1022 /* [temp.friend]/9 "Such a constrained friend function does not declare the 1023 same function as a declaration in any other scope." So no need to 1024 actually compare the requirements. */ 1025 if (member_like_constrained_friend_p (newfn) 1026 || member_like_constrained_friend_p (oldfn)) 1027 return false; 1028 1029 /* Compare only trailing requirements. */ 1030 tree reqs1 = get_trailing_function_requirements (newfn); 1031 tree reqs2 = get_trailing_function_requirements (oldfn); 1032 if ((reqs1 != NULL_TREE) != (reqs2 != NULL_TREE)) 1033 return false; 1034 1035 /* Substitution is needed when friends are involved. */ 1036 reqs1 = maybe_substitute_reqs_for (reqs1, newfn); 1037 reqs2 = maybe_substitute_reqs_for (reqs2, oldfn); 1038 1039 return cp_tree_equal (reqs1, reqs2); 1040 } 1041 1042 /* Two functions of the same name correspond [basic.scope.scope] if 1043 1044 + both declare functions with the same non-object-parameter-type-list, 1045 equivalent ([temp.over.link]) trailing requires-clauses (if any, except as 1046 specified in [temp.friend]), and, if both are non-static members, they have 1047 corresponding object parameters, or 1048 1049 + both declare function templates with equivalent 1050 non-object-parameter-type-lists, return types (if any), template-heads, and 1051 trailing requires-clauses (if any), and, if both are non-static members, 1052 they have corresponding object parameters. 1053 1054 This is a subset of decls_match: it identifies declarations that cannot be 1055 overloaded with one another. This function does not consider DECL_NAME. */ 1056 1057 bool 1058 fns_correspond (tree newdecl, tree olddecl) 1059 { 1060 if (TREE_CODE (newdecl) != TREE_CODE (olddecl)) 1061 return false; 1062 1063 if (TREE_CODE (newdecl) == TEMPLATE_DECL) 1064 { 1065 if (!template_heads_equivalent_p (newdecl, olddecl)) 1066 return 0; 1067 newdecl = DECL_TEMPLATE_RESULT (newdecl); 1068 olddecl = DECL_TEMPLATE_RESULT (olddecl); 1069 } 1070 1071 tree f1 = TREE_TYPE (newdecl); 1072 tree f2 = TREE_TYPE (olddecl); 1073 1074 int rq1 = type_memfn_rqual (f1); 1075 int rq2 = type_memfn_rqual (f2); 1076 1077 /* If only one is a non-static member function, ignore ref-quals. */ 1078 if (TREE_CODE (f1) != TREE_CODE (f2)) 1079 rq1 = rq2; 1080 /* Two non-static member functions have corresponding object parameters if: 1081 + exactly one is an implicit object member function with no ref-qualifier 1082 and the types of their object parameters ([dcl.fct]), after removing 1083 top-level references, are the same, or 1084 + their object parameters have the same type. */ 1085 /* ??? We treat member functions of different classes as corresponding even 1086 though that means the object parameters have different types. */ 1087 else if ((rq1 == REF_QUAL_NONE) != (rq2 == REF_QUAL_NONE)) 1088 rq1 = rq2; 1089 1090 bool types_match = rq1 == rq2; 1091 1092 if (types_match) 1093 { 1094 tree p1 = FUNCTION_FIRST_USER_PARMTYPE (newdecl); 1095 tree p2 = FUNCTION_FIRST_USER_PARMTYPE (olddecl); 1096 types_match = compparms (p1, p2); 1097 } 1098 1099 /* Two function declarations match if either has a requires-clause 1100 then both have a requires-clause and their constraints-expressions 1101 are equivalent. */ 1102 if (types_match && flag_concepts) 1103 types_match = function_requirements_equivalent_p (newdecl, olddecl); 1104 1105 return types_match; 1106 } 1107 1108 /* Subroutine of duplicate_decls: return truthvalue of whether 1109 or not types of these decls match. 1110 1111 For C++, we must compare the parameter list so that `int' can match 1112 `int&' in a parameter position, but `int&' is not confused with 1113 `const int&'. */ 1114 1115 int 1116 decls_match (tree newdecl, tree olddecl, bool record_versions /* = true */) 1117 { 1118 int types_match; 1119 1120 if (newdecl == olddecl) 1121 return 1; 1122 1123 if (TREE_CODE (newdecl) != TREE_CODE (olddecl)) 1124 /* If the two DECLs are not even the same kind of thing, we're not 1125 interested in their types. */ 1126 return 0; 1127 1128 gcc_assert (DECL_P (newdecl)); 1129 1130 if (TREE_CODE (newdecl) == FUNCTION_DECL) 1131 { 1132 /* Specializations of different templates are different functions 1133 even if they have the same type. */ 1134 tree t1 = (DECL_USE_TEMPLATE (newdecl) 1135 ? DECL_TI_TEMPLATE (newdecl) 1136 : NULL_TREE); 1137 tree t2 = (DECL_USE_TEMPLATE (olddecl) 1138 ? DECL_TI_TEMPLATE (olddecl) 1139 : NULL_TREE); 1140 if (t1 != t2) 1141 return 0; 1142 1143 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl) 1144 && ! (DECL_EXTERN_C_P (newdecl) 1145 && DECL_EXTERN_C_P (olddecl))) 1146 return 0; 1147 1148 /* A new declaration doesn't match a built-in one unless it 1149 is also extern "C". */ 1150 if (DECL_IS_UNDECLARED_BUILTIN (olddecl) 1151 && DECL_EXTERN_C_P (olddecl) && !DECL_EXTERN_C_P (newdecl)) 1152 return 0; 1153 1154 tree f1 = TREE_TYPE (newdecl); 1155 tree f2 = TREE_TYPE (olddecl); 1156 if (TREE_CODE (f1) != TREE_CODE (f2)) 1157 return 0; 1158 1159 /* A declaration with deduced return type should use its pre-deduction 1160 type for declaration matching. */ 1161 tree r2 = fndecl_declared_return_type (olddecl); 1162 tree r1 = fndecl_declared_return_type (newdecl); 1163 1164 tree p1 = TYPE_ARG_TYPES (f1); 1165 tree p2 = TYPE_ARG_TYPES (f2); 1166 1167 if (same_type_p (r1, r2)) 1168 { 1169 if (!prototype_p (f2) && DECL_EXTERN_C_P (olddecl) 1170 && fndecl_built_in_p (olddecl)) 1171 { 1172 types_match = self_promoting_args_p (p1); 1173 if (p1 == void_list_node) 1174 TREE_TYPE (newdecl) = TREE_TYPE (olddecl); 1175 } 1176 else 1177 types_match = 1178 compparms (p1, p2) 1179 && type_memfn_rqual (f1) == type_memfn_rqual (f2) 1180 && (TYPE_ATTRIBUTES (TREE_TYPE (newdecl)) == NULL_TREE 1181 || comp_type_attributes (TREE_TYPE (newdecl), 1182 TREE_TYPE (olddecl)) != 0); 1183 } 1184 else 1185 types_match = 0; 1186 1187 /* Two function declarations match if either has a requires-clause 1188 then both have a requires-clause and their constraints-expressions 1189 are equivalent. */ 1190 if (types_match && flag_concepts) 1191 types_match = function_requirements_equivalent_p (newdecl, olddecl); 1192 1193 /* The decls dont match if they correspond to two different versions 1194 of the same function. Disallow extern "C" functions to be 1195 versions for now. */ 1196 if (types_match 1197 && !DECL_EXTERN_C_P (newdecl) 1198 && !DECL_EXTERN_C_P (olddecl) 1199 && targetm.target_option.function_versions (newdecl, olddecl)) 1200 { 1201 if (record_versions) 1202 maybe_version_functions (newdecl, olddecl, 1203 (!DECL_FUNCTION_VERSIONED (newdecl) 1204 || !DECL_FUNCTION_VERSIONED (olddecl))); 1205 return 0; 1206 } 1207 } 1208 else if (TREE_CODE (newdecl) == TEMPLATE_DECL) 1209 { 1210 if (!template_heads_equivalent_p (newdecl, olddecl)) 1211 return 0; 1212 1213 tree oldres = DECL_TEMPLATE_RESULT (olddecl); 1214 tree newres = DECL_TEMPLATE_RESULT (newdecl); 1215 1216 if (TREE_CODE (newres) != TREE_CODE (oldres)) 1217 return 0; 1218 1219 /* Two template types match if they are the same. Otherwise, compare 1220 the underlying declarations. */ 1221 if (TREE_CODE (newres) == TYPE_DECL) 1222 types_match = same_type_p (TREE_TYPE (newres), TREE_TYPE (oldres)); 1223 else 1224 types_match = decls_match (newres, oldres); 1225 } 1226 else 1227 { 1228 /* Need to check scope for variable declaration (VAR_DECL). 1229 For typedef (TYPE_DECL), scope is ignored. */ 1230 if (VAR_P (newdecl) 1231 && CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl) 1232 /* [dcl.link] 1233 Two declarations for an object with C language linkage 1234 with the same name (ignoring the namespace that qualify 1235 it) that appear in different namespace scopes refer to 1236 the same object. */ 1237 && !(DECL_EXTERN_C_P (olddecl) && DECL_EXTERN_C_P (newdecl))) 1238 return 0; 1239 1240 if (TREE_TYPE (newdecl) == error_mark_node) 1241 types_match = TREE_TYPE (olddecl) == error_mark_node; 1242 else if (TREE_TYPE (olddecl) == NULL_TREE) 1243 types_match = TREE_TYPE (newdecl) == NULL_TREE; 1244 else if (TREE_TYPE (newdecl) == NULL_TREE) 1245 types_match = 0; 1246 else 1247 types_match = comptypes (TREE_TYPE (newdecl), 1248 TREE_TYPE (olddecl), 1249 COMPARE_REDECLARATION); 1250 } 1251 1252 return types_match; 1253 } 1254 1255 /* Mark DECL as versioned if it isn't already. */ 1256 1257 static void 1258 maybe_mark_function_versioned (tree decl) 1259 { 1260 if (!DECL_FUNCTION_VERSIONED (decl)) 1261 { 1262 DECL_FUNCTION_VERSIONED (decl) = 1; 1263 /* If DECL_ASSEMBLER_NAME has already been set, re-mangle 1264 to include the version marker. */ 1265 if (DECL_ASSEMBLER_NAME_SET_P (decl)) 1266 mangle_decl (decl); 1267 } 1268 } 1269 1270 /* NEWDECL and OLDDECL have identical signatures. If they are 1271 different versions adjust them and return true. 1272 If RECORD is set to true, record function versions. */ 1273 1274 bool 1275 maybe_version_functions (tree newdecl, tree olddecl, bool record) 1276 { 1277 if (!targetm.target_option.function_versions (newdecl, olddecl)) 1278 return false; 1279 1280 maybe_mark_function_versioned (olddecl); 1281 if (DECL_LOCAL_DECL_P (olddecl)) 1282 { 1283 olddecl = DECL_LOCAL_DECL_ALIAS (olddecl); 1284 maybe_mark_function_versioned (olddecl); 1285 } 1286 1287 maybe_mark_function_versioned (newdecl); 1288 if (DECL_LOCAL_DECL_P (newdecl)) 1289 { 1290 /* Unfortunately, we can get here before pushdecl naturally calls 1291 push_local_extern_decl_alias, so we need to call it directly. */ 1292 if (!DECL_LOCAL_DECL_ALIAS (newdecl)) 1293 push_local_extern_decl_alias (newdecl); 1294 newdecl = DECL_LOCAL_DECL_ALIAS (newdecl); 1295 maybe_mark_function_versioned (newdecl); 1296 } 1297 1298 if (record) 1299 cgraph_node::record_function_versions (olddecl, newdecl); 1300 1301 return true; 1302 } 1303 1304 /* If NEWDECL is `static' and an `extern' was seen previously, 1305 warn about it. OLDDECL is the previous declaration. 1306 1307 Note that this does not apply to the C++ case of declaring 1308 a variable `extern const' and then later `const'. 1309 1310 Don't complain about built-in functions, since they are beyond 1311 the user's control. */ 1312 1313 void 1314 warn_extern_redeclared_static (tree newdecl, tree olddecl) 1315 { 1316 if (TREE_CODE (newdecl) == TYPE_DECL 1317 || TREE_CODE (newdecl) == TEMPLATE_DECL 1318 || TREE_CODE (newdecl) == CONST_DECL 1319 || TREE_CODE (newdecl) == NAMESPACE_DECL) 1320 return; 1321 1322 /* Don't get confused by static member functions; that's a different 1323 use of `static'. */ 1324 if (TREE_CODE (newdecl) == FUNCTION_DECL 1325 && DECL_STATIC_FUNCTION_P (newdecl)) 1326 return; 1327 1328 /* If the old declaration was `static', or the new one isn't, then 1329 everything is OK. */ 1330 if (DECL_THIS_STATIC (olddecl) || !DECL_THIS_STATIC (newdecl)) 1331 return; 1332 1333 /* It's OK to declare a builtin function as `static'. */ 1334 if (TREE_CODE (olddecl) == FUNCTION_DECL 1335 && DECL_ARTIFICIAL (olddecl)) 1336 return; 1337 1338 auto_diagnostic_group d; 1339 if (permerror (DECL_SOURCE_LOCATION (newdecl), 1340 "%qD was declared %<extern%> and later %<static%>", newdecl)) 1341 inform (DECL_SOURCE_LOCATION (olddecl), 1342 "previous declaration of %qD", olddecl); 1343 } 1344 1345 /* NEW_DECL is a redeclaration of OLD_DECL; both are functions or 1346 function templates. If their exception specifications do not 1347 match, issue a diagnostic. */ 1348 1349 static void 1350 check_redeclaration_exception_specification (tree new_decl, 1351 tree old_decl) 1352 { 1353 tree new_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (new_decl)); 1354 tree old_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old_decl)); 1355 1356 /* Two default specs are equivalent, don't force evaluation. */ 1357 if (UNEVALUATED_NOEXCEPT_SPEC_P (new_exceptions) 1358 && UNEVALUATED_NOEXCEPT_SPEC_P (old_exceptions)) 1359 return; 1360 1361 if (!type_dependent_expression_p (old_decl)) 1362 { 1363 maybe_instantiate_noexcept (new_decl); 1364 maybe_instantiate_noexcept (old_decl); 1365 } 1366 new_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (new_decl)); 1367 old_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old_decl)); 1368 1369 /* [except.spec] 1370 1371 If any declaration of a function has an exception-specification, 1372 all declarations, including the definition and an explicit 1373 specialization, of that function shall have an 1374 exception-specification with the same set of type-ids. */ 1375 if (!DECL_IS_UNDECLARED_BUILTIN (old_decl) 1376 && !DECL_IS_UNDECLARED_BUILTIN (new_decl) 1377 && !comp_except_specs (new_exceptions, old_exceptions, ce_normal)) 1378 { 1379 const char *const msg 1380 = G_("declaration of %qF has a different exception specifier"); 1381 bool complained = true; 1382 location_t new_loc = DECL_SOURCE_LOCATION (new_decl); 1383 auto_diagnostic_group d; 1384 if (DECL_IN_SYSTEM_HEADER (old_decl)) 1385 complained = pedwarn (new_loc, OPT_Wsystem_headers, msg, new_decl); 1386 else if (!flag_exceptions) 1387 /* We used to silently permit mismatched eh specs with 1388 -fno-exceptions, so make them a pedwarn now. */ 1389 complained = pedwarn (new_loc, OPT_Wpedantic, msg, new_decl); 1390 else 1391 error_at (new_loc, msg, new_decl); 1392 if (complained) 1393 inform (DECL_SOURCE_LOCATION (old_decl), 1394 "from previous declaration %qF", old_decl); 1395 } 1396 } 1397 1398 /* Return true if OLD_DECL and NEW_DECL agree on constexprness. 1399 Otherwise issue diagnostics. */ 1400 1401 static bool 1402 validate_constexpr_redeclaration (tree old_decl, tree new_decl) 1403 { 1404 old_decl = STRIP_TEMPLATE (old_decl); 1405 new_decl = STRIP_TEMPLATE (new_decl); 1406 if (!VAR_OR_FUNCTION_DECL_P (old_decl) 1407 || !VAR_OR_FUNCTION_DECL_P (new_decl)) 1408 return true; 1409 if (DECL_DECLARED_CONSTEXPR_P (old_decl) 1410 == DECL_DECLARED_CONSTEXPR_P (new_decl)) 1411 { 1412 if (TREE_CODE (old_decl) != FUNCTION_DECL) 1413 return true; 1414 if (DECL_IMMEDIATE_FUNCTION_P (old_decl) 1415 == DECL_IMMEDIATE_FUNCTION_P (new_decl)) 1416 return true; 1417 } 1418 if (TREE_CODE (old_decl) == FUNCTION_DECL) 1419 { 1420 /* With -fimplicit-constexpr, ignore changes in the constexpr 1421 keyword. */ 1422 if (flag_implicit_constexpr 1423 && (DECL_IMMEDIATE_FUNCTION_P (new_decl) 1424 == DECL_IMMEDIATE_FUNCTION_P (old_decl))) 1425 return true; 1426 if (fndecl_built_in_p (old_decl)) 1427 { 1428 /* Hide a built-in declaration. */ 1429 DECL_DECLARED_CONSTEXPR_P (old_decl) 1430 = DECL_DECLARED_CONSTEXPR_P (new_decl); 1431 if (DECL_IMMEDIATE_FUNCTION_P (new_decl)) 1432 SET_DECL_IMMEDIATE_FUNCTION_P (old_decl); 1433 return true; 1434 } 1435 /* 7.1.5 [dcl.constexpr] 1436 Note: An explicit specialization can differ from the template 1437 declaration with respect to the constexpr specifier. */ 1438 if (! DECL_TEMPLATE_SPECIALIZATION (old_decl) 1439 && DECL_TEMPLATE_SPECIALIZATION (new_decl)) 1440 return true; 1441 1442 const char *kind = "constexpr"; 1443 if (DECL_IMMEDIATE_FUNCTION_P (old_decl) 1444 || DECL_IMMEDIATE_FUNCTION_P (new_decl)) 1445 kind = "consteval"; 1446 error_at (DECL_SOURCE_LOCATION (new_decl), 1447 "redeclaration %qD differs in %qs " 1448 "from previous declaration", new_decl, 1449 kind); 1450 inform (DECL_SOURCE_LOCATION (old_decl), 1451 "previous declaration %qD", old_decl); 1452 return false; 1453 } 1454 return true; 1455 } 1456 1457 // If OLDDECL and NEWDECL are concept declarations with the same type 1458 // (i.e., and template parameters), but different requirements, 1459 // emit diagnostics and return true. Otherwise, return false. 1460 static inline bool 1461 check_concept_refinement (tree olddecl, tree newdecl) 1462 { 1463 if (!DECL_DECLARED_CONCEPT_P (olddecl) || !DECL_DECLARED_CONCEPT_P (newdecl)) 1464 return false; 1465 1466 tree d1 = DECL_TEMPLATE_RESULT (olddecl); 1467 tree d2 = DECL_TEMPLATE_RESULT (newdecl); 1468 if (TREE_CODE (d1) != TREE_CODE (d2)) 1469 return false; 1470 1471 tree t1 = TREE_TYPE (d1); 1472 tree t2 = TREE_TYPE (d2); 1473 if (TREE_CODE (d1) == FUNCTION_DECL) 1474 { 1475 if (compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2)) 1476 && comp_template_parms (DECL_TEMPLATE_PARMS (olddecl), 1477 DECL_TEMPLATE_PARMS (newdecl)) 1478 && !equivalently_constrained (olddecl, newdecl)) 1479 { 1480 error ("cannot specialize concept %q#D", olddecl); 1481 return true; 1482 } 1483 } 1484 return false; 1485 } 1486 1487 /* DECL is a redeclaration of a function or function template. If 1488 it does have default arguments issue a diagnostic. Note: this 1489 function is used to enforce the requirements in C++11 8.3.6 about 1490 no default arguments in redeclarations. */ 1491 1492 static void 1493 check_redeclaration_no_default_args (tree decl) 1494 { 1495 gcc_assert (DECL_DECLARES_FUNCTION_P (decl)); 1496 1497 for (tree t = FUNCTION_FIRST_USER_PARMTYPE (decl); 1498 t && t != void_list_node; t = TREE_CHAIN (t)) 1499 if (TREE_PURPOSE (t)) 1500 { 1501 permerror (DECL_SOURCE_LOCATION (decl), 1502 "redeclaration of %q#D may not have default " 1503 "arguments", decl); 1504 return; 1505 } 1506 } 1507 1508 /* NEWDECL is a redeclaration of a function or function template OLDDECL, 1509 in any case represented as FUNCTION_DECLs (the DECL_TEMPLATE_RESULTs of 1510 the TEMPLATE_DECLs in case of function templates). This function is used 1511 to enforce the final part of C++17 11.3.6/4, about a single declaration: 1512 "If a friend declaration specifies a default argument expression, that 1513 declaration shall be a definition and shall be the only declaration of 1514 the function or function template in the translation unit." */ 1515 1516 static void 1517 check_no_redeclaration_friend_default_args (tree olddecl, tree newdecl) 1518 { 1519 if (!DECL_UNIQUE_FRIEND_P (olddecl) && !DECL_UNIQUE_FRIEND_P (newdecl)) 1520 return; 1521 1522 for (tree t1 = FUNCTION_FIRST_USER_PARMTYPE (olddecl), 1523 t2 = FUNCTION_FIRST_USER_PARMTYPE (newdecl); 1524 t1 && t1 != void_list_node; 1525 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2)) 1526 if ((DECL_UNIQUE_FRIEND_P (olddecl) && TREE_PURPOSE (t1)) 1527 || (DECL_UNIQUE_FRIEND_P (newdecl) && TREE_PURPOSE (t2))) 1528 { 1529 auto_diagnostic_group d; 1530 if (permerror (DECL_SOURCE_LOCATION (newdecl), 1531 "friend declaration of %q#D specifies default " 1532 "arguments and isn%'t the only declaration", newdecl)) 1533 inform (DECL_SOURCE_LOCATION (olddecl), 1534 "previous declaration of %q#D", olddecl); 1535 return; 1536 } 1537 } 1538 1539 /* Merge tree bits that correspond to attributes noreturn, nothrow, 1540 const, malloc, and pure from NEWDECL with those of OLDDECL. */ 1541 1542 static void 1543 merge_attribute_bits (tree newdecl, tree olddecl) 1544 { 1545 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl); 1546 TREE_THIS_VOLATILE (olddecl) |= TREE_THIS_VOLATILE (newdecl); 1547 TREE_NOTHROW (newdecl) |= TREE_NOTHROW (olddecl); 1548 TREE_NOTHROW (olddecl) |= TREE_NOTHROW (newdecl); 1549 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl); 1550 TREE_READONLY (olddecl) |= TREE_READONLY (newdecl); 1551 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl); 1552 DECL_IS_MALLOC (olddecl) |= DECL_IS_MALLOC (newdecl); 1553 DECL_PURE_P (newdecl) |= DECL_PURE_P (olddecl); 1554 DECL_PURE_P (olddecl) |= DECL_PURE_P (newdecl); 1555 DECL_UNINLINABLE (newdecl) |= DECL_UNINLINABLE (olddecl); 1556 DECL_UNINLINABLE (olddecl) |= DECL_UNINLINABLE (newdecl); 1557 TREE_DEPRECATED (newdecl) |= TREE_DEPRECATED (olddecl); 1558 TREE_DEPRECATED (olddecl) |= TREE_DEPRECATED (newdecl); 1559 TREE_UNAVAILABLE (newdecl) |= TREE_UNAVAILABLE (olddecl); 1560 TREE_UNAVAILABLE (olddecl) |= TREE_UNAVAILABLE (newdecl); 1561 } 1562 1563 #define GNU_INLINE_P(fn) (DECL_DECLARED_INLINE_P (fn) \ 1564 && lookup_attribute ("gnu_inline", \ 1565 DECL_ATTRIBUTES (fn))) 1566 1567 /* A subroutine of duplicate_decls. Emits a diagnostic when newdecl 1568 ambiguates olddecl. Returns true if an error occurs. */ 1569 1570 static bool 1571 duplicate_function_template_decls (tree newdecl, tree olddecl) 1572 { 1573 tree newres = DECL_TEMPLATE_RESULT (newdecl); 1574 tree oldres = DECL_TEMPLATE_RESULT (olddecl); 1575 /* Function template declarations can be differentiated by parameter 1576 and return type. */ 1577 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (oldres)), 1578 TYPE_ARG_TYPES (TREE_TYPE (newres))) 1579 && same_type_p (TREE_TYPE (TREE_TYPE (newdecl)), 1580 TREE_TYPE (TREE_TYPE (olddecl)))) 1581 { 1582 /* ... and also by their template-heads and requires-clauses. */ 1583 if (template_heads_equivalent_p (newdecl, olddecl) 1584 && function_requirements_equivalent_p (newres, oldres)) 1585 { 1586 error ("ambiguating new declaration %q+#D", newdecl); 1587 inform (DECL_SOURCE_LOCATION (olddecl), 1588 "old declaration %q#D", olddecl); 1589 return true; 1590 } 1591 1592 /* FIXME: The types are the same but the are differences 1593 in either the template heads or function requirements. 1594 We should be able to diagnose a set of common errors 1595 stemming from these declarations. For example: 1596 1597 template<typename T> requires C void f(...); 1598 template<typename T> void f(...) requires C; 1599 1600 These are functionally equivalent but not equivalent. */ 1601 } 1602 1603 return false; 1604 } 1605 1606 /* OLD_PARMS is the innermost set of template parameters for some template 1607 declaration, and NEW_PARMS is the corresponding set of template parameters 1608 for a redeclaration of that template. Merge the default arguments within 1609 these two sets of parameters. CLASS_P is true iff the template in 1610 question is a class template. */ 1611 1612 bool 1613 merge_default_template_args (tree new_parms, tree old_parms, bool class_p) 1614 { 1615 gcc_checking_assert (TREE_VEC_LENGTH (new_parms) 1616 == TREE_VEC_LENGTH (old_parms)); 1617 for (int i = 0; i < TREE_VEC_LENGTH (new_parms); i++) 1618 { 1619 tree new_parm = TREE_VALUE (TREE_VEC_ELT (new_parms, i)); 1620 tree old_parm = TREE_VALUE (TREE_VEC_ELT (old_parms, i)); 1621 tree& new_default = TREE_PURPOSE (TREE_VEC_ELT (new_parms, i)); 1622 tree& old_default = TREE_PURPOSE (TREE_VEC_ELT (old_parms, i)); 1623 if (error_operand_p (new_parm) || error_operand_p (old_parm)) 1624 return false; 1625 if (new_default != NULL_TREE && old_default != NULL_TREE) 1626 { 1627 auto_diagnostic_group d; 1628 error ("redefinition of default argument for %q+#D", new_parm); 1629 inform (DECL_SOURCE_LOCATION (old_parm), 1630 "original definition appeared here"); 1631 return false; 1632 } 1633 else if (new_default != NULL_TREE) 1634 /* Update the previous template parameters (which are the ones 1635 that will really count) with the new default value. */ 1636 old_default = new_default; 1637 else if (class_p && old_default != NULL_TREE) 1638 /* Update the new parameters, too; they'll be used as the 1639 parameters for any members. */ 1640 new_default = old_default; 1641 } 1642 return true; 1643 } 1644 1645 /* If NEWDECL is a redeclaration of OLDDECL, merge the declarations. 1646 If the redeclaration is invalid, a diagnostic is issued, and the 1647 error_mark_node is returned. Otherwise, OLDDECL is returned. 1648 1649 If NEWDECL is not a redeclaration of OLDDECL, NULL_TREE is 1650 returned. 1651 1652 HIDING is true if the new decl is being hidden. WAS_HIDDEN is true 1653 if the old decl was hidden. 1654 1655 Hidden decls can be anticipated builtins, injected friends, or 1656 (coming soon) injected from a local-extern decl. */ 1657 1658 tree 1659 duplicate_decls (tree newdecl, tree olddecl, bool hiding, bool was_hidden) 1660 { 1661 unsigned olddecl_uid = DECL_UID (olddecl); 1662 int types_match = 0; 1663 int new_defines_function = 0; 1664 tree new_template_info; 1665 location_t olddecl_loc = DECL_SOURCE_LOCATION (olddecl); 1666 location_t newdecl_loc = DECL_SOURCE_LOCATION (newdecl); 1667 1668 if (newdecl == olddecl) 1669 return olddecl; 1670 1671 types_match = decls_match (newdecl, olddecl); 1672 1673 /* If either the type of the new decl or the type of the old decl is an 1674 error_mark_node, then that implies that we have already issued an 1675 error (earlier) for some bogus type specification, and in that case, 1676 it is rather pointless to harass the user with yet more error message 1677 about the same declaration, so just pretend the types match here. */ 1678 if (TREE_TYPE (newdecl) == error_mark_node 1679 || TREE_TYPE (olddecl) == error_mark_node) 1680 return error_mark_node; 1681 1682 /* Check for redeclaration and other discrepancies. */ 1683 if (TREE_CODE (olddecl) == FUNCTION_DECL 1684 && DECL_IS_UNDECLARED_BUILTIN (olddecl)) 1685 { 1686 if (TREE_CODE (newdecl) != FUNCTION_DECL) 1687 { 1688 /* Avoid warnings redeclaring built-ins which have not been 1689 explicitly declared. */ 1690 if (was_hidden) 1691 { 1692 if (TREE_PUBLIC (newdecl) 1693 && CP_DECL_CONTEXT (newdecl) == global_namespace) 1694 warning_at (newdecl_loc, 1695 OPT_Wbuiltin_declaration_mismatch, 1696 "built-in function %qD declared as non-function", 1697 newdecl); 1698 return NULL_TREE; 1699 } 1700 1701 /* If you declare a built-in or predefined function name as static, 1702 the old definition is overridden, but optionally warn this was a 1703 bad choice of name. */ 1704 if (! TREE_PUBLIC (newdecl)) 1705 { 1706 warning_at (newdecl_loc, 1707 OPT_Wshadow, 1708 fndecl_built_in_p (olddecl) 1709 ? G_("shadowing built-in function %q#D") 1710 : G_("shadowing library function %q#D"), olddecl); 1711 /* Discard the old built-in function. */ 1712 return NULL_TREE; 1713 } 1714 /* If the built-in is not ansi, then programs can override 1715 it even globally without an error. */ 1716 else if (! fndecl_built_in_p (olddecl)) 1717 warning_at (newdecl_loc, 0, 1718 "library function %q#D redeclared as non-function %q#D", 1719 olddecl, newdecl); 1720 else 1721 error_at (newdecl_loc, 1722 "declaration of %q#D conflicts with built-in " 1723 "declaration %q#D", newdecl, olddecl); 1724 return NULL_TREE; 1725 } 1726 else if (!types_match) 1727 { 1728 /* Avoid warnings redeclaring built-ins which have not been 1729 explicitly declared. */ 1730 if (was_hidden) 1731 { 1732 tree t1, t2; 1733 1734 /* A new declaration doesn't match a built-in one unless it 1735 is also extern "C". */ 1736 gcc_assert (DECL_IS_UNDECLARED_BUILTIN (olddecl)); 1737 gcc_assert (DECL_EXTERN_C_P (olddecl)); 1738 if (!DECL_EXTERN_C_P (newdecl)) 1739 return NULL_TREE; 1740 1741 for (t1 = TYPE_ARG_TYPES (TREE_TYPE (newdecl)), 1742 t2 = TYPE_ARG_TYPES (TREE_TYPE (olddecl)); 1743 t1 || t2; 1744 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2)) 1745 { 1746 if (!t1 || !t2) 1747 break; 1748 /* FILE, tm types are not known at the time 1749 we create the builtins. */ 1750 for (unsigned i = 0; 1751 i < sizeof (builtin_structptr_types) 1752 / sizeof (builtin_structptr_type); 1753 ++i) 1754 if (TREE_VALUE (t2) == builtin_structptr_types[i].node) 1755 { 1756 tree t = TREE_VALUE (t1); 1757 1758 if (TYPE_PTR_P (t) 1759 && TYPE_IDENTIFIER (TREE_TYPE (t)) 1760 == get_identifier (builtin_structptr_types[i].str) 1761 && compparms (TREE_CHAIN (t1), TREE_CHAIN (t2))) 1762 { 1763 tree oldargs = TYPE_ARG_TYPES (TREE_TYPE (olddecl)); 1764 1765 TYPE_ARG_TYPES (TREE_TYPE (olddecl)) 1766 = TYPE_ARG_TYPES (TREE_TYPE (newdecl)); 1767 types_match = decls_match (newdecl, olddecl); 1768 if (types_match) 1769 return duplicate_decls (newdecl, olddecl, 1770 hiding, was_hidden); 1771 TYPE_ARG_TYPES (TREE_TYPE (olddecl)) = oldargs; 1772 } 1773 goto next_arg; 1774 } 1775 1776 if (! same_type_p (TREE_VALUE (t1), TREE_VALUE (t2))) 1777 break; 1778 next_arg:; 1779 } 1780 1781 warning_at (newdecl_loc, 1782 OPT_Wbuiltin_declaration_mismatch, 1783 "declaration of %q#D conflicts with built-in " 1784 "declaration %q#D", newdecl, olddecl); 1785 } 1786 else if ((DECL_EXTERN_C_P (newdecl) 1787 && DECL_EXTERN_C_P (olddecl)) 1788 || compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)), 1789 TYPE_ARG_TYPES (TREE_TYPE (olddecl)))) 1790 { 1791 /* Don't really override olddecl for __* prefixed builtins 1792 except for __[^b]*_chk, the compiler might be using those 1793 explicitly. */ 1794 if (fndecl_built_in_p (olddecl)) 1795 { 1796 tree id = DECL_NAME (olddecl); 1797 const char *name = IDENTIFIER_POINTER (id); 1798 size_t len; 1799 1800 if (name[0] == '_' 1801 && name[1] == '_' 1802 && (startswith (name + 2, "builtin_") 1803 || (len = strlen (name)) <= strlen ("___chk") 1804 || memcmp (name + len - strlen ("_chk"), 1805 "_chk", strlen ("_chk") + 1) != 0)) 1806 { 1807 if (DECL_INITIAL (newdecl)) 1808 { 1809 error_at (newdecl_loc, 1810 "definition of %q#D ambiguates built-in " 1811 "declaration %q#D", newdecl, olddecl); 1812 return error_mark_node; 1813 } 1814 auto_diagnostic_group d; 1815 if (permerror (newdecl_loc, 1816 "new declaration %q#D ambiguates built-in" 1817 " declaration %q#D", newdecl, olddecl) 1818 && flag_permissive) 1819 inform (newdecl_loc, 1820 "ignoring the %q#D declaration", newdecl); 1821 return flag_permissive ? olddecl : error_mark_node; 1822 } 1823 } 1824 1825 /* A near match; override the builtin. */ 1826 1827 if (TREE_PUBLIC (newdecl)) 1828 warning_at (newdecl_loc, 1829 OPT_Wbuiltin_declaration_mismatch, 1830 "new declaration %q#D ambiguates built-in " 1831 "declaration %q#D", newdecl, olddecl); 1832 else 1833 warning (OPT_Wshadow, 1834 fndecl_built_in_p (olddecl) 1835 ? G_("shadowing built-in function %q#D") 1836 : G_("shadowing library function %q#D"), olddecl); 1837 } 1838 else 1839 /* Discard the old built-in function. */ 1840 return NULL_TREE; 1841 1842 /* Replace the old RTL to avoid problems with inlining. */ 1843 COPY_DECL_RTL (newdecl, olddecl); 1844 } 1845 else 1846 { 1847 /* Even if the types match, prefer the new declarations type 1848 for built-ins which have not been explicitly declared, 1849 for exception lists, etc... */ 1850 tree type = TREE_TYPE (newdecl); 1851 tree attribs = (*targetm.merge_type_attributes) 1852 (TREE_TYPE (olddecl), type); 1853 1854 type = cp_build_type_attribute_variant (type, attribs); 1855 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = type; 1856 } 1857 1858 /* If a function is explicitly declared "throw ()", propagate that to 1859 the corresponding builtin. */ 1860 if (DECL_BUILT_IN_CLASS (olddecl) == BUILT_IN_NORMAL 1861 && was_hidden 1862 && TREE_NOTHROW (newdecl) 1863 && !TREE_NOTHROW (olddecl)) 1864 { 1865 enum built_in_function fncode = DECL_FUNCTION_CODE (olddecl); 1866 tree tmpdecl = builtin_decl_explicit (fncode); 1867 if (tmpdecl && tmpdecl != olddecl && types_match) 1868 TREE_NOTHROW (tmpdecl) = 1; 1869 } 1870 1871 /* Whether or not the builtin can throw exceptions has no 1872 bearing on this declarator. */ 1873 TREE_NOTHROW (olddecl) = 0; 1874 1875 if (DECL_THIS_STATIC (newdecl) && !DECL_THIS_STATIC (olddecl)) 1876 { 1877 /* If a builtin function is redeclared as `static', merge 1878 the declarations, but make the original one static. */ 1879 DECL_THIS_STATIC (olddecl) = 1; 1880 TREE_PUBLIC (olddecl) = 0; 1881 1882 /* Make the old declaration consistent with the new one so 1883 that all remnants of the builtin-ness of this function 1884 will be banished. */ 1885 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl)); 1886 COPY_DECL_RTL (newdecl, olddecl); 1887 } 1888 } 1889 else if (TREE_CODE (olddecl) != TREE_CODE (newdecl)) 1890 { 1891 /* C++ Standard, 3.3, clause 4: 1892 "[Note: a namespace name or a class template name must be unique 1893 in its declarative region (7.3.2, clause 14). ]" */ 1894 if (TREE_CODE (olddecl) == NAMESPACE_DECL 1895 || TREE_CODE (newdecl) == NAMESPACE_DECL) 1896 /* Namespace conflicts with not namespace. */; 1897 else if (DECL_TYPE_TEMPLATE_P (olddecl) 1898 || DECL_TYPE_TEMPLATE_P (newdecl)) 1899 /* Class template conflicts. */; 1900 else if ((TREE_CODE (olddecl) == TEMPLATE_DECL 1901 && DECL_TEMPLATE_RESULT (olddecl) 1902 && TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == VAR_DECL) 1903 || (TREE_CODE (newdecl) == TEMPLATE_DECL 1904 && DECL_TEMPLATE_RESULT (newdecl) 1905 && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == VAR_DECL)) 1906 /* Variable template conflicts. */; 1907 else if (concept_definition_p (olddecl) 1908 || concept_definition_p (newdecl)) 1909 /* Concept conflicts. */; 1910 else if ((TREE_CODE (newdecl) == FUNCTION_DECL 1911 && DECL_FUNCTION_TEMPLATE_P (olddecl)) 1912 || (TREE_CODE (olddecl) == FUNCTION_DECL 1913 && DECL_FUNCTION_TEMPLATE_P (newdecl))) 1914 { 1915 /* One is a function and the other is a template 1916 function. */ 1917 if (!UDLIT_OPER_P (DECL_NAME (newdecl))) 1918 return NULL_TREE; 1919 1920 /* There can only be one! */ 1921 if (TREE_CODE (newdecl) == TEMPLATE_DECL 1922 && check_raw_literal_operator (olddecl)) 1923 error_at (newdecl_loc, 1924 "literal operator %q#D conflicts with" 1925 " raw literal operator", newdecl); 1926 else if (check_raw_literal_operator (newdecl)) 1927 error_at (newdecl_loc, 1928 "raw literal operator %q#D conflicts with" 1929 " literal operator template", newdecl); 1930 else 1931 return NULL_TREE; 1932 1933 inform (olddecl_loc, "previous declaration %q#D", olddecl); 1934 return error_mark_node; 1935 } 1936 else if ((VAR_P (olddecl) && DECL_DECOMPOSITION_P (olddecl)) 1937 || (VAR_P (newdecl) && DECL_DECOMPOSITION_P (newdecl))) 1938 /* A structured binding must be unique in its declarative region. */; 1939 else if (DECL_IMPLICIT_TYPEDEF_P (olddecl) 1940 || DECL_IMPLICIT_TYPEDEF_P (newdecl)) 1941 /* One is an implicit typedef, that's ok. */ 1942 return NULL_TREE; 1943 1944 error ("%q#D redeclared as different kind of entity", newdecl); 1945 inform (olddecl_loc, "previous declaration %q#D", olddecl); 1946 1947 return error_mark_node; 1948 } 1949 else if (!types_match) 1950 { 1951 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)) 1952 /* These are certainly not duplicate declarations; they're 1953 from different scopes. */ 1954 return NULL_TREE; 1955 1956 if (TREE_CODE (newdecl) == TEMPLATE_DECL) 1957 { 1958 tree oldres = DECL_TEMPLATE_RESULT (olddecl); 1959 tree newres = DECL_TEMPLATE_RESULT (newdecl); 1960 1961 /* The name of a class template may not be declared to refer to 1962 any other template, class, function, object, namespace, value, 1963 or type in the same scope. */ 1964 if (TREE_CODE (oldres) == TYPE_DECL 1965 || TREE_CODE (newres) == TYPE_DECL) 1966 { 1967 error_at (newdecl_loc, 1968 "conflicting declaration of template %q#D", newdecl); 1969 inform (olddecl_loc, 1970 "previous declaration %q#D", olddecl); 1971 return error_mark_node; 1972 } 1973 1974 else if (TREE_CODE (oldres) == FUNCTION_DECL 1975 && TREE_CODE (newres) == FUNCTION_DECL) 1976 { 1977 if (duplicate_function_template_decls (newdecl, olddecl)) 1978 return error_mark_node; 1979 return NULL_TREE; 1980 } 1981 else if (check_concept_refinement (olddecl, newdecl)) 1982 return error_mark_node; 1983 return NULL_TREE; 1984 } 1985 if (TREE_CODE (newdecl) == FUNCTION_DECL) 1986 { 1987 if (DECL_EXTERN_C_P (newdecl) && DECL_EXTERN_C_P (olddecl)) 1988 { 1989 error_at (newdecl_loc, 1990 "conflicting declaration of C function %q#D", 1991 newdecl); 1992 inform (olddecl_loc, 1993 "previous declaration %q#D", olddecl); 1994 return error_mark_node; 1995 } 1996 /* For function versions, params and types match, but they 1997 are not ambiguous. */ 1998 else if ((!DECL_FUNCTION_VERSIONED (newdecl) 1999 && !DECL_FUNCTION_VERSIONED (olddecl)) 2000 /* Let constrained hidden friends coexist for now, we'll 2001 check satisfaction later. */ 2002 && !member_like_constrained_friend_p (newdecl) 2003 && !member_like_constrained_friend_p (olddecl) 2004 // The functions have the same parameter types. 2005 && compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)), 2006 TYPE_ARG_TYPES (TREE_TYPE (olddecl))) 2007 // And the same constraints. 2008 && equivalently_constrained (newdecl, olddecl)) 2009 { 2010 error_at (newdecl_loc, 2011 "ambiguating new declaration of %q#D", newdecl); 2012 inform (olddecl_loc, 2013 "old declaration %q#D", olddecl); 2014 return error_mark_node; 2015 } 2016 else 2017 return NULL_TREE; 2018 } 2019 else 2020 { 2021 error_at (newdecl_loc, "conflicting declaration %q#D", newdecl); 2022 inform (olddecl_loc, 2023 "previous declaration as %q#D", olddecl); 2024 return error_mark_node; 2025 } 2026 } 2027 else if (TREE_CODE (newdecl) == FUNCTION_DECL 2028 && DECL_OMP_DECLARE_REDUCTION_P (newdecl)) 2029 { 2030 /* OMP UDRs are never duplicates. */ 2031 gcc_assert (DECL_OMP_DECLARE_REDUCTION_P (olddecl)); 2032 error_at (newdecl_loc, 2033 "redeclaration of %<pragma omp declare reduction%>"); 2034 inform (olddecl_loc, 2035 "previous %<pragma omp declare reduction%> declaration"); 2036 return error_mark_node; 2037 } 2038 else if (TREE_CODE (newdecl) == FUNCTION_DECL 2039 && ((DECL_TEMPLATE_SPECIALIZATION (olddecl) 2040 && (!DECL_TEMPLATE_INFO (newdecl) 2041 || (DECL_TI_TEMPLATE (newdecl) 2042 != DECL_TI_TEMPLATE (olddecl)))) 2043 || (DECL_TEMPLATE_SPECIALIZATION (newdecl) 2044 && (!DECL_TEMPLATE_INFO (olddecl) 2045 || (DECL_TI_TEMPLATE (olddecl) 2046 != DECL_TI_TEMPLATE (newdecl)))))) 2047 /* It's OK to have a template specialization and a non-template 2048 with the same type, or to have specializations of two 2049 different templates with the same type. Note that if one is a 2050 specialization, and the other is an instantiation of the same 2051 template, that we do not exit at this point. That situation 2052 can occur if we instantiate a template class, and then 2053 specialize one of its methods. This situation is valid, but 2054 the declarations must be merged in the usual way. */ 2055 return NULL_TREE; 2056 else if (TREE_CODE (newdecl) == FUNCTION_DECL 2057 && ((DECL_TEMPLATE_INSTANTIATION (olddecl) 2058 && !DECL_USE_TEMPLATE (newdecl)) 2059 || (DECL_TEMPLATE_INSTANTIATION (newdecl) 2060 && !DECL_USE_TEMPLATE (olddecl)))) 2061 /* One of the declarations is a template instantiation, and the 2062 other is not a template at all. That's OK. */ 2063 return NULL_TREE; 2064 else if (TREE_CODE (newdecl) == NAMESPACE_DECL) 2065 { 2066 /* In [namespace.alias] we have: 2067 2068 In a declarative region, a namespace-alias-definition can be 2069 used to redefine a namespace-alias declared in that declarative 2070 region to refer only to the namespace to which it already 2071 refers. 2072 2073 Therefore, if we encounter a second alias directive for the same 2074 alias, we can just ignore the second directive. */ 2075 if (DECL_NAMESPACE_ALIAS (newdecl) 2076 && (DECL_NAMESPACE_ALIAS (newdecl) 2077 == DECL_NAMESPACE_ALIAS (olddecl))) 2078 return olddecl; 2079 2080 /* Leave it to update_binding to merge or report error. */ 2081 return NULL_TREE; 2082 } 2083 else 2084 { 2085 const char *errmsg = redeclaration_error_message (newdecl, olddecl); 2086 if (errmsg) 2087 { 2088 auto_diagnostic_group d; 2089 error_at (newdecl_loc, errmsg, newdecl); 2090 if (DECL_NAME (olddecl) != NULL_TREE) 2091 inform (olddecl_loc, 2092 (DECL_INITIAL (olddecl) && namespace_bindings_p ()) 2093 ? G_("%q#D previously defined here") 2094 : G_("%q#D previously declared here"), olddecl); 2095 if (cxx_dialect >= cxx26 2096 && DECL_NAME (newdecl) 2097 && id_equal (DECL_NAME (newdecl), "_") 2098 && !name_independent_decl_p (newdecl)) 2099 { 2100 if (TREE_CODE (newdecl) == PARM_DECL) 2101 inform (newdecl_loc, 2102 "parameter declaration is not name-independent"); 2103 else if (DECL_DECOMPOSITION_P (newdecl)) 2104 { 2105 if (at_namespace_scope_p ()) 2106 inform (newdecl_loc, 2107 "structured binding at namespace scope is not " 2108 "name-independent"); 2109 else if (TREE_STATIC (newdecl)) 2110 inform (newdecl_loc, 2111 "static structured binding is not " 2112 "name-independent"); 2113 else if (DECL_EXTERNAL (newdecl)) 2114 inform (newdecl_loc, 2115 "extern structured binding is not " 2116 "name-independent"); 2117 } 2118 else if (at_class_scope_p () 2119 && VAR_P (newdecl) 2120 && TREE_STATIC (newdecl)) 2121 inform (newdecl_loc, 2122 "static data member is not name-independent"); 2123 else if (VAR_P (newdecl) && at_namespace_scope_p ()) 2124 inform (newdecl_loc, 2125 "variable at namespace scope is not name-independent"); 2126 else if (VAR_P (newdecl) && TREE_STATIC (newdecl)) 2127 inform (newdecl_loc, 2128 "static variable is not name-independent"); 2129 else if (VAR_P (newdecl) && DECL_EXTERNAL (newdecl)) 2130 inform (newdecl_loc, 2131 "extern variable is not name-independent"); 2132 } 2133 return error_mark_node; 2134 } 2135 else if (TREE_CODE (olddecl) == FUNCTION_DECL 2136 && DECL_INITIAL (olddecl) != NULL_TREE 2137 && !prototype_p (TREE_TYPE (olddecl)) 2138 && prototype_p (TREE_TYPE (newdecl))) 2139 { 2140 /* Prototype decl follows defn w/o prototype. */ 2141 auto_diagnostic_group d; 2142 if (warning_at (newdecl_loc, 0, 2143 "prototype specified for %q#D", newdecl)) 2144 inform (olddecl_loc, 2145 "previous non-prototype definition here"); 2146 } 2147 else if (VAR_OR_FUNCTION_DECL_P (olddecl) 2148 && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl)) 2149 { 2150 /* [dcl.link] 2151 If two declarations of the same function or object 2152 specify different linkage-specifications ..., the program 2153 is ill-formed.... Except for functions with C++ linkage, 2154 a function declaration without a linkage specification 2155 shall not precede the first linkage specification for 2156 that function. A function can be declared without a 2157 linkage specification after an explicit linkage 2158 specification has been seen; the linkage explicitly 2159 specified in the earlier declaration is not affected by 2160 such a function declaration. 2161 2162 DR 563 raises the question why the restrictions on 2163 functions should not also apply to objects. Older 2164 versions of G++ silently ignore the linkage-specification 2165 for this example: 2166 2167 namespace N { 2168 extern int i; 2169 extern "C" int i; 2170 } 2171 2172 which is clearly wrong. Therefore, we now treat objects 2173 like functions. */ 2174 if (current_lang_depth () == 0) 2175 { 2176 /* There is no explicit linkage-specification, so we use 2177 the linkage from the previous declaration. */ 2178 retrofit_lang_decl (newdecl); 2179 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl)); 2180 } 2181 else 2182 { 2183 auto_diagnostic_group d; 2184 error_at (newdecl_loc, 2185 "conflicting declaration of %q#D with %qL linkage", 2186 newdecl, DECL_LANGUAGE (newdecl)); 2187 inform (olddecl_loc, 2188 "previous declaration with %qL linkage", 2189 DECL_LANGUAGE (olddecl)); 2190 } 2191 } 2192 2193 if (DECL_LANG_SPECIFIC (olddecl) && DECL_USE_TEMPLATE (olddecl)) 2194 ; 2195 else if (TREE_CODE (olddecl) == FUNCTION_DECL) 2196 { 2197 /* Note: free functions, as TEMPLATE_DECLs, are handled below. */ 2198 if (DECL_FUNCTION_MEMBER_P (olddecl) 2199 && (/* grokfndecl passes member function templates too 2200 as FUNCTION_DECLs. */ 2201 DECL_TEMPLATE_INFO (olddecl) 2202 /* C++11 8.3.6/6. 2203 Default arguments for a member function of a class 2204 template shall be specified on the initial declaration 2205 of the member function within the class template. */ 2206 || CLASSTYPE_TEMPLATE_INFO (CP_DECL_CONTEXT (olddecl)))) 2207 { 2208 check_redeclaration_no_default_args (newdecl); 2209 2210 if (DECL_TEMPLATE_INFO (olddecl) 2211 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (olddecl))) 2212 { 2213 tree new_parms = DECL_TEMPLATE_INFO (newdecl) 2214 ? DECL_INNERMOST_TEMPLATE_PARMS (DECL_TI_TEMPLATE (newdecl)) 2215 : INNERMOST_TEMPLATE_PARMS (current_template_parms); 2216 tree old_parms 2217 = DECL_INNERMOST_TEMPLATE_PARMS (DECL_TI_TEMPLATE (olddecl)); 2218 merge_default_template_args (new_parms, old_parms, 2219 /*class_p=*/false); 2220 } 2221 } 2222 else 2223 { 2224 tree t1 = FUNCTION_FIRST_USER_PARMTYPE (olddecl); 2225 tree t2 = FUNCTION_FIRST_USER_PARMTYPE (newdecl); 2226 int i = 1; 2227 2228 for (; t1 && t1 != void_list_node; 2229 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2), i++) 2230 if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2)) 2231 { 2232 if (simple_cst_equal (TREE_PURPOSE (t1), 2233 TREE_PURPOSE (t2)) == 1) 2234 { 2235 auto_diagnostic_group d; 2236 if (permerror (newdecl_loc, 2237 "default argument given for parameter " 2238 "%d of %q#D", i, newdecl)) 2239 inform (olddecl_loc, 2240 "previous specification in %q#D here", 2241 olddecl); 2242 } 2243 else 2244 { 2245 auto_diagnostic_group d; 2246 error_at (newdecl_loc, 2247 "default argument given for parameter %d " 2248 "of %q#D", i, newdecl); 2249 inform (olddecl_loc, 2250 "previous specification in %q#D here", 2251 olddecl); 2252 } 2253 } 2254 2255 /* C++17 11.3.6/4: "If a friend declaration specifies a default 2256 argument expression, that declaration... shall be the only 2257 declaration of the function or function template in the 2258 translation unit." */ 2259 check_no_redeclaration_friend_default_args (olddecl, newdecl); 2260 } 2261 } 2262 } 2263 2264 /* Do not merge an implicit typedef with an explicit one. In: 2265 2266 class A; 2267 ... 2268 typedef class A A __attribute__ ((foo)); 2269 2270 the attribute should apply only to the typedef. */ 2271 if (TREE_CODE (olddecl) == TYPE_DECL 2272 && (DECL_IMPLICIT_TYPEDEF_P (olddecl) 2273 || DECL_IMPLICIT_TYPEDEF_P (newdecl))) 2274 return NULL_TREE; 2275 2276 if (DECL_TEMPLATE_PARM_P (olddecl) != DECL_TEMPLATE_PARM_P (newdecl)) 2277 return NULL_TREE; 2278 2279 if (!validate_constexpr_redeclaration (olddecl, newdecl)) 2280 return error_mark_node; 2281 2282 if (modules_p () 2283 && TREE_CODE (CP_DECL_CONTEXT (olddecl)) == NAMESPACE_DECL 2284 && TREE_CODE (olddecl) != NAMESPACE_DECL) 2285 { 2286 if (!module_may_redeclare (olddecl, newdecl)) 2287 return error_mark_node; 2288 2289 if (!hiding) 2290 { 2291 /* The old declaration should match the exportingness of the new 2292 declaration. But hidden friend declarations just keep the 2293 exportingness of the old declaration; see CWG2588. */ 2294 tree not_tmpl = STRIP_TEMPLATE (olddecl); 2295 if (DECL_LANG_SPECIFIC (not_tmpl) 2296 && DECL_MODULE_ATTACH_P (not_tmpl) 2297 /* Typedefs are not entities and so are OK to be redeclared 2298 as exported: see [module.interface]/p6. */ 2299 && TREE_CODE (olddecl) != TYPE_DECL) 2300 { 2301 if (DECL_MODULE_EXPORT_P (newdecl) 2302 && !DECL_MODULE_EXPORT_P (not_tmpl)) 2303 { 2304 auto_diagnostic_group d; 2305 error ("conflicting exporting for declaration %qD", newdecl); 2306 inform (olddecl_loc, 2307 "previously declared here without exporting"); 2308 } 2309 } 2310 else if (DECL_MODULE_EXPORT_P (newdecl)) 2311 DECL_MODULE_EXPORT_P (not_tmpl) = true; 2312 } 2313 } 2314 2315 /* We have committed to returning OLDDECL at this point. */ 2316 2317 /* If new decl is `static' and an `extern' was seen previously, 2318 warn about it. */ 2319 warn_extern_redeclared_static (newdecl, olddecl); 2320 2321 /* True to merge attributes between the declarations, false to 2322 set OLDDECL's attributes to those of NEWDECL (for template 2323 explicit specializations that specify their own attributes 2324 independent of those specified for the primary template). */ 2325 const bool merge_attr = (TREE_CODE (newdecl) != FUNCTION_DECL 2326 || !DECL_TEMPLATE_SPECIALIZATION (newdecl) 2327 || DECL_TEMPLATE_SPECIALIZATION (olddecl)); 2328 2329 if (TREE_CODE (newdecl) == FUNCTION_DECL) 2330 { 2331 if (merge_attr) 2332 { 2333 if (diagnose_mismatched_attributes (olddecl, newdecl)) 2334 inform (olddecl_loc, DECL_INITIAL (olddecl) 2335 ? G_("previous definition of %qD here") 2336 : G_("previous declaration of %qD here"), olddecl); 2337 2338 /* [dcl.attr.noreturn]: The first declaration of a function shall 2339 specify the noreturn attribute if any declaration of that function 2340 specifies the noreturn attribute. */ 2341 tree a; 2342 if (TREE_THIS_VOLATILE (newdecl) 2343 && !TREE_THIS_VOLATILE (olddecl) 2344 /* This applies to [[noreturn]] only, not its GNU variants. */ 2345 && (a = lookup_attribute ("noreturn", DECL_ATTRIBUTES (newdecl))) 2346 && cxx11_attribute_p (a) 2347 && get_attribute_namespace (a) == NULL_TREE) 2348 { 2349 error_at (newdecl_loc, "function %qD declared %<[[noreturn]]%> " 2350 "but its first declaration was not", newdecl); 2351 inform (olddecl_loc, "previous declaration of %qD", olddecl); 2352 } 2353 } 2354 2355 /* Now that functions must hold information normally held 2356 by field decls, there is extra work to do so that 2357 declaration information does not get destroyed during 2358 definition. */ 2359 if (DECL_VINDEX (olddecl)) 2360 DECL_VINDEX (newdecl) = DECL_VINDEX (olddecl); 2361 if (DECL_CONTEXT (olddecl)) 2362 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl); 2363 DECL_STATIC_CONSTRUCTOR (newdecl) |= DECL_STATIC_CONSTRUCTOR (olddecl); 2364 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl); 2365 DECL_PURE_VIRTUAL_P (newdecl) |= DECL_PURE_VIRTUAL_P (olddecl); 2366 DECL_VIRTUAL_P (newdecl) |= DECL_VIRTUAL_P (olddecl); 2367 DECL_INVALID_OVERRIDER_P (newdecl) |= DECL_INVALID_OVERRIDER_P (olddecl); 2368 DECL_FINAL_P (newdecl) |= DECL_FINAL_P (olddecl); 2369 DECL_OVERRIDE_P (newdecl) |= DECL_OVERRIDE_P (olddecl); 2370 DECL_THIS_STATIC (newdecl) |= DECL_THIS_STATIC (olddecl); 2371 DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (newdecl) 2372 |= DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (olddecl); 2373 if (DECL_OVERLOADED_OPERATOR_P (olddecl)) 2374 DECL_OVERLOADED_OPERATOR_CODE_RAW (newdecl) 2375 = DECL_OVERLOADED_OPERATOR_CODE_RAW (olddecl); 2376 new_defines_function = DECL_INITIAL (newdecl) != NULL_TREE; 2377 2378 duplicate_contracts (newdecl, olddecl); 2379 2380 /* Optionally warn about more than one declaration for the same 2381 name, but don't warn about a function declaration followed by a 2382 definition. */ 2383 if (warn_redundant_decls && ! DECL_ARTIFICIAL (olddecl) 2384 && !(new_defines_function && DECL_INITIAL (olddecl) == NULL_TREE) 2385 /* Don't warn about extern decl followed by definition. */ 2386 && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl)) 2387 /* Don't warn if at least one is/was hidden. */ 2388 && !(hiding || was_hidden) 2389 /* Don't warn about declaration followed by specialization. */ 2390 && (! DECL_TEMPLATE_SPECIALIZATION (newdecl) 2391 || DECL_TEMPLATE_SPECIALIZATION (olddecl))) 2392 { 2393 auto_diagnostic_group d; 2394 if (warning_at (newdecl_loc, 2395 OPT_Wredundant_decls, 2396 "redundant redeclaration of %qD in same scope", 2397 newdecl)) 2398 inform (olddecl_loc, 2399 "previous declaration of %qD", olddecl); 2400 } 2401 2402 /* [dcl.fct.def.delete] A deleted definition of a function shall be the 2403 first declaration of the function or, for an explicit specialization 2404 of a function template, the first declaration of that 2405 specialization. */ 2406 if (!(DECL_TEMPLATE_INSTANTIATION (olddecl) 2407 && DECL_TEMPLATE_SPECIALIZATION (newdecl))) 2408 { 2409 if (DECL_DELETED_FN (newdecl)) 2410 { 2411 auto_diagnostic_group d; 2412 if (pedwarn (newdecl_loc, 0, "deleted definition of %qD " 2413 "is not first declaration", newdecl)) 2414 inform (olddecl_loc, 2415 "previous declaration of %qD", olddecl); 2416 } 2417 DECL_DELETED_FN (newdecl) |= DECL_DELETED_FN (olddecl); 2418 } 2419 } 2420 2421 /* Deal with C++: must preserve virtual function table size. */ 2422 if (TREE_CODE (olddecl) == TYPE_DECL) 2423 { 2424 tree newtype = TREE_TYPE (newdecl); 2425 tree oldtype = TREE_TYPE (olddecl); 2426 2427 if (newtype != error_mark_node && oldtype != error_mark_node 2428 && TYPE_LANG_SPECIFIC (newtype) && TYPE_LANG_SPECIFIC (oldtype)) 2429 CLASSTYPE_FRIEND_CLASSES (newtype) 2430 = CLASSTYPE_FRIEND_CLASSES (oldtype); 2431 2432 DECL_ORIGINAL_TYPE (newdecl) = DECL_ORIGINAL_TYPE (olddecl); 2433 } 2434 2435 /* Copy all the DECL_... slots specified in the new decl except for 2436 any that we copy here from the old type. */ 2437 if (merge_attr) 2438 DECL_ATTRIBUTES (newdecl) 2439 = (*targetm.merge_decl_attributes) (olddecl, newdecl); 2440 else 2441 DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl); 2442 2443 if (TREE_CODE (newdecl) == TEMPLATE_DECL) 2444 { 2445 tree old_result = DECL_TEMPLATE_RESULT (olddecl); 2446 tree new_result = DECL_TEMPLATE_RESULT (newdecl); 2447 TREE_TYPE (olddecl) = TREE_TYPE (old_result); 2448 2449 /* The new decl should not already have gathered any 2450 specializations. */ 2451 gcc_assert (!DECL_TEMPLATE_SPECIALIZATIONS (newdecl)); 2452 2453 /* Make sure the contracts are equivalent. */ 2454 duplicate_contracts (newdecl, olddecl); 2455 2456 /* Remove contracts from old_result so they aren't appended to 2457 old_result by the merge function. */ 2458 remove_contract_attributes (old_result); 2459 2460 DECL_ATTRIBUTES (old_result) 2461 = (*targetm.merge_decl_attributes) (old_result, new_result); 2462 2463 if (DECL_FUNCTION_TEMPLATE_P (newdecl)) 2464 { 2465 if (DECL_SOURCE_LOCATION (newdecl) 2466 != DECL_SOURCE_LOCATION (olddecl)) 2467 { 2468 /* Per C++11 8.3.6/4, default arguments cannot be added in 2469 later declarations of a function template. */ 2470 check_redeclaration_no_default_args (newdecl); 2471 /* C++17 11.3.6/4: "If a friend declaration specifies a default 2472 argument expression, that declaration... shall be the only 2473 declaration of the function or function template in the 2474 translation unit." */ 2475 check_no_redeclaration_friend_default_args 2476 (old_result, new_result); 2477 2478 tree new_parms = DECL_INNERMOST_TEMPLATE_PARMS (newdecl); 2479 tree old_parms = DECL_INNERMOST_TEMPLATE_PARMS (olddecl); 2480 merge_default_template_args (new_parms, old_parms, 2481 /*class_p=*/false); 2482 } 2483 if (!DECL_UNIQUE_FRIEND_P (new_result)) 2484 DECL_UNIQUE_FRIEND_P (old_result) = false; 2485 2486 check_default_args (newdecl); 2487 2488 if (GNU_INLINE_P (old_result) != GNU_INLINE_P (new_result) 2489 && DECL_INITIAL (new_result)) 2490 { 2491 if (DECL_INITIAL (old_result)) 2492 DECL_UNINLINABLE (old_result) = 1; 2493 else 2494 DECL_UNINLINABLE (old_result) = DECL_UNINLINABLE (new_result); 2495 DECL_EXTERNAL (old_result) = DECL_EXTERNAL (new_result); 2496 DECL_NOT_REALLY_EXTERN (old_result) 2497 = DECL_NOT_REALLY_EXTERN (new_result); 2498 DECL_INTERFACE_KNOWN (old_result) 2499 = DECL_INTERFACE_KNOWN (new_result); 2500 DECL_DECLARED_INLINE_P (old_result) 2501 = DECL_DECLARED_INLINE_P (new_result); 2502 DECL_DISREGARD_INLINE_LIMITS (old_result) 2503 |= DECL_DISREGARD_INLINE_LIMITS (new_result); 2504 } 2505 else 2506 { 2507 DECL_DECLARED_INLINE_P (old_result) 2508 |= DECL_DECLARED_INLINE_P (new_result); 2509 DECL_DISREGARD_INLINE_LIMITS (old_result) 2510 |= DECL_DISREGARD_INLINE_LIMITS (new_result); 2511 check_redeclaration_exception_specification (newdecl, olddecl); 2512 2513 merge_attribute_bits (new_result, old_result); 2514 } 2515 } 2516 2517 /* If the new declaration is a definition, update the file and 2518 line information on the declaration, and also make 2519 the old declaration the same definition. */ 2520 if (DECL_INITIAL (new_result) != NULL_TREE) 2521 { 2522 DECL_SOURCE_LOCATION (olddecl) 2523 = DECL_SOURCE_LOCATION (old_result) 2524 = DECL_SOURCE_LOCATION (newdecl); 2525 DECL_INITIAL (old_result) = DECL_INITIAL (new_result); 2526 if (DECL_FUNCTION_TEMPLATE_P (newdecl)) 2527 { 2528 DECL_ARGUMENTS (old_result) = DECL_ARGUMENTS (new_result); 2529 for (tree p = DECL_ARGUMENTS (old_result); p; p = DECL_CHAIN (p)) 2530 DECL_CONTEXT (p) = old_result; 2531 2532 if (tree fc = DECL_FRIEND_CONTEXT (new_result)) 2533 SET_DECL_FRIEND_CONTEXT (old_result, fc); 2534 } 2535 } 2536 2537 return olddecl; 2538 } 2539 2540 if (types_match) 2541 { 2542 if (TREE_CODE (newdecl) == FUNCTION_DECL) 2543 check_redeclaration_exception_specification (newdecl, olddecl); 2544 2545 /* Automatically handles default parameters. */ 2546 tree oldtype = TREE_TYPE (olddecl); 2547 tree newtype; 2548 2549 /* For typedefs use the old type, as the new type's DECL_NAME points 2550 at newdecl, which will be ggc_freed. */ 2551 if (TREE_CODE (newdecl) == TYPE_DECL) 2552 { 2553 /* But NEWTYPE might have an attribute, honor that. */ 2554 tree tem = TREE_TYPE (newdecl); 2555 newtype = oldtype; 2556 2557 if (TYPE_USER_ALIGN (tem)) 2558 { 2559 if (TYPE_ALIGN (tem) > TYPE_ALIGN (newtype)) 2560 SET_TYPE_ALIGN (newtype, TYPE_ALIGN (tem)); 2561 TYPE_USER_ALIGN (newtype) = true; 2562 } 2563 2564 /* And remove the new type from the variants list. */ 2565 if (TYPE_NAME (TREE_TYPE (newdecl)) == newdecl) 2566 { 2567 tree remove = TREE_TYPE (newdecl); 2568 if (TYPE_MAIN_VARIANT (remove) == remove) 2569 { 2570 gcc_assert (TYPE_NEXT_VARIANT (remove) == NULL_TREE); 2571 /* If remove is the main variant, no need to remove that 2572 from the list. One of the DECL_ORIGINAL_TYPE 2573 variants, e.g. created for aligned attribute, might still 2574 refer to the newdecl TYPE_DECL though, so remove that one 2575 in that case. */ 2576 if (tree orig = DECL_ORIGINAL_TYPE (newdecl)) 2577 if (orig != remove) 2578 for (tree t = TYPE_MAIN_VARIANT (orig); t; 2579 t = TYPE_MAIN_VARIANT (t)) 2580 if (TYPE_NAME (TYPE_NEXT_VARIANT (t)) == newdecl) 2581 { 2582 TYPE_NEXT_VARIANT (t) 2583 = TYPE_NEXT_VARIANT (TYPE_NEXT_VARIANT (t)); 2584 break; 2585 } 2586 } 2587 else 2588 for (tree t = TYPE_MAIN_VARIANT (remove); ; 2589 t = TYPE_NEXT_VARIANT (t)) 2590 if (TYPE_NEXT_VARIANT (t) == remove) 2591 { 2592 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (remove); 2593 break; 2594 } 2595 } 2596 } 2597 else if (merge_attr) 2598 newtype = merge_types (TREE_TYPE (newdecl), TREE_TYPE (olddecl)); 2599 else 2600 newtype = TREE_TYPE (newdecl); 2601 2602 if (VAR_P (newdecl)) 2603 { 2604 DECL_THIS_EXTERN (newdecl) |= DECL_THIS_EXTERN (olddecl); 2605 /* For already initialized vars, TREE_READONLY could have been 2606 cleared in cp_finish_decl, because the var needs runtime 2607 initialization or destruction. Make sure not to set 2608 TREE_READONLY on it again. */ 2609 if (DECL_INITIALIZED_P (olddecl) 2610 && !DECL_EXTERNAL (olddecl) 2611 && !TREE_READONLY (olddecl)) 2612 TREE_READONLY (newdecl) = 0; 2613 DECL_INITIALIZED_P (newdecl) |= DECL_INITIALIZED_P (olddecl); 2614 DECL_NONTRIVIALLY_INITIALIZED_P (newdecl) 2615 |= DECL_NONTRIVIALLY_INITIALIZED_P (olddecl); 2616 if (DECL_DEPENDENT_INIT_P (olddecl)) 2617 SET_DECL_DEPENDENT_INIT_P (newdecl, true); 2618 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (newdecl) 2619 |= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (olddecl); 2620 DECL_DECLARED_CONSTEXPR_P (newdecl) 2621 |= DECL_DECLARED_CONSTEXPR_P (olddecl); 2622 DECL_DECLARED_CONSTINIT_P (newdecl) 2623 |= DECL_DECLARED_CONSTINIT_P (olddecl); 2624 2625 /* Merge the threadprivate attribute from OLDDECL into NEWDECL. */ 2626 if (DECL_LANG_SPECIFIC (olddecl) 2627 && CP_DECL_THREADPRIVATE_P (olddecl)) 2628 { 2629 /* Allocate a LANG_SPECIFIC structure for NEWDECL, if needed. */ 2630 retrofit_lang_decl (newdecl); 2631 CP_DECL_THREADPRIVATE_P (newdecl) = 1; 2632 } 2633 } 2634 2635 /* An explicit specialization of a function template or of a member 2636 function of a class template can be declared transaction_safe 2637 independently of whether the corresponding template entity is declared 2638 transaction_safe. */ 2639 if (flag_tm && TREE_CODE (newdecl) == FUNCTION_DECL 2640 && DECL_TEMPLATE_INSTANTIATION (olddecl) 2641 && DECL_TEMPLATE_SPECIALIZATION (newdecl) 2642 && tx_safe_fn_type_p (newtype) 2643 && !tx_safe_fn_type_p (TREE_TYPE (newdecl))) 2644 newtype = tx_unsafe_fn_variant (newtype); 2645 2646 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = newtype; 2647 2648 if (TREE_CODE (newdecl) == FUNCTION_DECL) 2649 check_default_args (newdecl); 2650 2651 /* Lay the type out, unless already done. */ 2652 if (! same_type_p (newtype, oldtype) 2653 && TREE_TYPE (newdecl) != error_mark_node 2654 && !(processing_template_decl && uses_template_parms (newdecl))) 2655 layout_type (TREE_TYPE (newdecl)); 2656 2657 if ((VAR_P (newdecl) 2658 || TREE_CODE (newdecl) == PARM_DECL 2659 || TREE_CODE (newdecl) == RESULT_DECL 2660 || TREE_CODE (newdecl) == FIELD_DECL 2661 || TREE_CODE (newdecl) == TYPE_DECL) 2662 && !(processing_template_decl && uses_template_parms (newdecl))) 2663 layout_decl (newdecl, 0); 2664 2665 /* Merge deprecatedness. */ 2666 if (TREE_DEPRECATED (newdecl)) 2667 TREE_DEPRECATED (olddecl) = 1; 2668 2669 /* Merge unavailability. */ 2670 if (TREE_UNAVAILABLE (newdecl)) 2671 TREE_UNAVAILABLE (olddecl) = 1; 2672 2673 /* Preserve function specific target and optimization options */ 2674 if (TREE_CODE (newdecl) == FUNCTION_DECL) 2675 { 2676 if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl) 2677 && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl)) 2678 DECL_FUNCTION_SPECIFIC_TARGET (newdecl) 2679 = DECL_FUNCTION_SPECIFIC_TARGET (olddecl); 2680 2681 if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl) 2682 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl)) 2683 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl) 2684 = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl); 2685 2686 if (!DECL_UNIQUE_FRIEND_P (olddecl)) 2687 DECL_UNIQUE_FRIEND_P (newdecl) = false; 2688 } 2689 else 2690 { 2691 /* Merge the const type qualifier. */ 2692 if (TREE_READONLY (newdecl)) 2693 TREE_READONLY (olddecl) = 1; 2694 /* Merge the volatile type qualifier. */ 2695 if (TREE_THIS_VOLATILE (newdecl)) 2696 TREE_THIS_VOLATILE (olddecl) = 1; 2697 } 2698 2699 /* Merge the initialization information. */ 2700 if (DECL_INITIAL (newdecl) == NULL_TREE 2701 && DECL_INITIAL (olddecl) != NULL_TREE) 2702 { 2703 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl); 2704 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl); 2705 if (TREE_CODE (newdecl) == FUNCTION_DECL) 2706 { 2707 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl); 2708 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl); 2709 } 2710 } 2711 2712 if (TREE_CODE (newdecl) == FUNCTION_DECL) 2713 { 2714 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl) 2715 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl); 2716 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl); 2717 if (DECL_IS_OPERATOR_NEW_P (olddecl)) 2718 DECL_SET_IS_OPERATOR_NEW (newdecl, true); 2719 DECL_LOOPING_CONST_OR_PURE_P (newdecl) 2720 |= DECL_LOOPING_CONST_OR_PURE_P (olddecl); 2721 DECL_IS_REPLACEABLE_OPERATOR (newdecl) 2722 |= DECL_IS_REPLACEABLE_OPERATOR (olddecl); 2723 2724 if (merge_attr) 2725 merge_attribute_bits (newdecl, olddecl); 2726 else 2727 { 2728 /* Merge the noreturn bit. */ 2729 TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl); 2730 TREE_READONLY (olddecl) = TREE_READONLY (newdecl); 2731 TREE_NOTHROW (olddecl) = TREE_NOTHROW (newdecl); 2732 DECL_IS_MALLOC (olddecl) = DECL_IS_MALLOC (newdecl); 2733 DECL_PURE_P (olddecl) = DECL_PURE_P (newdecl); 2734 } 2735 /* Keep the old RTL. */ 2736 COPY_DECL_RTL (olddecl, newdecl); 2737 } 2738 else if (VAR_P (newdecl) 2739 && (DECL_SIZE (olddecl) || !DECL_SIZE (newdecl))) 2740 { 2741 /* Keep the old RTL. We cannot keep the old RTL if the old 2742 declaration was for an incomplete object and the new 2743 declaration is not since many attributes of the RTL will 2744 change. */ 2745 COPY_DECL_RTL (olddecl, newdecl); 2746 } 2747 } 2748 /* If cannot merge, then use the new type and qualifiers, 2749 and don't preserve the old rtl. */ 2750 else 2751 { 2752 /* Clean out any memory we had of the old declaration. */ 2753 tree oldstatic = value_member (olddecl, static_aggregates); 2754 if (oldstatic) 2755 TREE_VALUE (oldstatic) = error_mark_node; 2756 2757 TREE_TYPE (olddecl) = TREE_TYPE (newdecl); 2758 TREE_READONLY (olddecl) = TREE_READONLY (newdecl); 2759 TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl); 2760 TREE_NOTHROW (olddecl) = TREE_NOTHROW (newdecl); 2761 TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl); 2762 } 2763 2764 /* Merge the storage class information. */ 2765 merge_weak (newdecl, olddecl); 2766 2767 DECL_DEFER_OUTPUT (newdecl) |= DECL_DEFER_OUTPUT (olddecl); 2768 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl); 2769 TREE_STATIC (olddecl) = TREE_STATIC (newdecl) |= TREE_STATIC (olddecl); 2770 if (! DECL_EXTERNAL (olddecl)) 2771 DECL_EXTERNAL (newdecl) = 0; 2772 if (! DECL_COMDAT (olddecl)) 2773 DECL_COMDAT (newdecl) = 0; 2774 2775 if (VAR_OR_FUNCTION_DECL_P (newdecl) && DECL_LOCAL_DECL_P (newdecl)) 2776 { 2777 if (!DECL_LOCAL_DECL_P (olddecl)) 2778 /* This can happen if olddecl was brought in from the 2779 enclosing namespace via a using-decl. The new decl is 2780 then not a block-scope extern at all. */ 2781 DECL_LOCAL_DECL_P (newdecl) = false; 2782 else 2783 { 2784 retrofit_lang_decl (newdecl); 2785 tree alias = DECL_LOCAL_DECL_ALIAS (newdecl) 2786 = DECL_LOCAL_DECL_ALIAS (olddecl); 2787 DECL_ATTRIBUTES (alias) 2788 = (*targetm.merge_decl_attributes) (alias, newdecl); 2789 if (TREE_CODE (newdecl) == FUNCTION_DECL) 2790 merge_attribute_bits (newdecl, alias); 2791 } 2792 } 2793 2794 new_template_info = NULL_TREE; 2795 if (DECL_LANG_SPECIFIC (newdecl) && DECL_LANG_SPECIFIC (olddecl)) 2796 { 2797 bool new_redefines_gnu_inline = false; 2798 2799 if (new_defines_function 2800 && ((DECL_INTERFACE_KNOWN (olddecl) 2801 && TREE_CODE (olddecl) == FUNCTION_DECL) 2802 || (TREE_CODE (olddecl) == TEMPLATE_DECL 2803 && (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) 2804 == FUNCTION_DECL)))) 2805 new_redefines_gnu_inline = GNU_INLINE_P (STRIP_TEMPLATE (olddecl)); 2806 2807 if (!new_redefines_gnu_inline) 2808 { 2809 DECL_INTERFACE_KNOWN (newdecl) |= DECL_INTERFACE_KNOWN (olddecl); 2810 DECL_NOT_REALLY_EXTERN (newdecl) |= DECL_NOT_REALLY_EXTERN (olddecl); 2811 DECL_COMDAT (newdecl) |= DECL_COMDAT (olddecl); 2812 } 2813 2814 if (TREE_CODE (newdecl) != TYPE_DECL) 2815 { 2816 DECL_TEMPLATE_INSTANTIATED (newdecl) 2817 |= DECL_TEMPLATE_INSTANTIATED (olddecl); 2818 DECL_ODR_USED (newdecl) |= DECL_ODR_USED (olddecl); 2819 2820 /* If the OLDDECL is an instantiation and/or specialization, 2821 then the NEWDECL must be too. But, it may not yet be marked 2822 as such if the caller has created NEWDECL, but has not yet 2823 figured out that it is a redeclaration. */ 2824 if (!DECL_USE_TEMPLATE (newdecl)) 2825 DECL_USE_TEMPLATE (newdecl) = DECL_USE_TEMPLATE (olddecl); 2826 2827 if (!DECL_TEMPLATE_SPECIALIZATION (newdecl)) 2828 DECL_INITIALIZED_IN_CLASS_P (newdecl) 2829 |= DECL_INITIALIZED_IN_CLASS_P (olddecl); 2830 } 2831 2832 /* Don't really know how much of the language-specific 2833 values we should copy from old to new. */ 2834 DECL_IN_AGGR_P (newdecl) = DECL_IN_AGGR_P (olddecl); 2835 2836 if (LANG_DECL_HAS_MIN (newdecl)) 2837 { 2838 DECL_ACCESS (newdecl) = DECL_ACCESS (olddecl); 2839 if (DECL_TEMPLATE_INFO (newdecl)) 2840 { 2841 new_template_info = DECL_TEMPLATE_INFO (newdecl); 2842 if (DECL_TEMPLATE_INSTANTIATION (olddecl) 2843 && DECL_TEMPLATE_SPECIALIZATION (newdecl)) 2844 /* Remember the presence of explicit specialization args. */ 2845 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (olddecl)) 2846 = TINFO_USED_TEMPLATE_ID (new_template_info); 2847 } 2848 2849 /* We don't want to copy template info from a non-templated friend 2850 (PR105761), but these shouldn't have DECL_TEMPLATE_INFO now. */ 2851 gcc_checking_assert (!DECL_TEMPLATE_INFO (olddecl) 2852 || !non_templated_friend_p (olddecl)); 2853 DECL_TEMPLATE_INFO (newdecl) = DECL_TEMPLATE_INFO (olddecl); 2854 } 2855 2856 if (DECL_DECLARES_FUNCTION_P (newdecl)) 2857 { 2858 /* Only functions have these fields. */ 2859 DECL_NONCONVERTING_P (newdecl) = DECL_NONCONVERTING_P (olddecl); 2860 DECL_BEFRIENDING_CLASSES (newdecl) 2861 = chainon (DECL_BEFRIENDING_CLASSES (newdecl), 2862 DECL_BEFRIENDING_CLASSES (olddecl)); 2863 /* DECL_THUNKS is only valid for virtual functions, 2864 otherwise it is a DECL_FRIEND_CONTEXT. */ 2865 if (DECL_VIRTUAL_P (newdecl)) 2866 SET_DECL_THUNKS (newdecl, DECL_THUNKS (olddecl)); 2867 else if (tree fc = DECL_FRIEND_CONTEXT (newdecl)) 2868 SET_DECL_FRIEND_CONTEXT (olddecl, fc); 2869 } 2870 else if (VAR_P (newdecl)) 2871 { 2872 /* Only variables have this field. */ 2873 if (VAR_HAD_UNKNOWN_BOUND (olddecl)) 2874 SET_VAR_HAD_UNKNOWN_BOUND (newdecl); 2875 } 2876 } 2877 2878 if (TREE_CODE (newdecl) == FUNCTION_DECL) 2879 { 2880 tree parm; 2881 2882 /* Merge parameter attributes. */ 2883 tree oldarg, newarg; 2884 for (oldarg = DECL_ARGUMENTS(olddecl), newarg = DECL_ARGUMENTS(newdecl); 2885 oldarg && newarg; 2886 oldarg = DECL_CHAIN(oldarg), newarg = DECL_CHAIN(newarg)) 2887 { 2888 DECL_ATTRIBUTES (newarg) 2889 = (*targetm.merge_decl_attributes) (oldarg, newarg); 2890 DECL_ATTRIBUTES (oldarg) = DECL_ATTRIBUTES (newarg); 2891 } 2892 2893 if (DECL_TEMPLATE_INSTANTIATION (olddecl) 2894 && !DECL_TEMPLATE_INSTANTIATION (newdecl)) 2895 { 2896 /* If newdecl is not a specialization, then it is not a 2897 template-related function at all. And that means that we 2898 should have exited above, returning 0. */ 2899 gcc_assert (DECL_TEMPLATE_SPECIALIZATION (newdecl)); 2900 2901 if (DECL_ODR_USED (olddecl)) 2902 /* From [temp.expl.spec]: 2903 2904 If a template, a member template or the member of a class 2905 template is explicitly specialized then that 2906 specialization shall be declared before the first use of 2907 that specialization that would cause an implicit 2908 instantiation to take place, in every translation unit in 2909 which such a use occurs. */ 2910 error ("explicit specialization of %qD after first use", 2911 olddecl); 2912 2913 SET_DECL_TEMPLATE_SPECIALIZATION (olddecl); 2914 DECL_COMDAT (newdecl) = (TREE_PUBLIC (newdecl) 2915 && DECL_DECLARED_INLINE_P (newdecl)); 2916 2917 /* Don't propagate visibility from the template to the 2918 specialization here. We'll do that in determine_visibility if 2919 appropriate. */ 2920 DECL_VISIBILITY_SPECIFIED (olddecl) = 0; 2921 2922 /* [temp.expl.spec/14] We don't inline explicit specialization 2923 just because the primary template says so. */ 2924 gcc_assert (!merge_attr); 2925 2926 DECL_DECLARED_INLINE_P (olddecl) 2927 = DECL_DECLARED_INLINE_P (newdecl); 2928 2929 DECL_DISREGARD_INLINE_LIMITS (olddecl) 2930 = DECL_DISREGARD_INLINE_LIMITS (newdecl); 2931 2932 DECL_UNINLINABLE (olddecl) = DECL_UNINLINABLE (newdecl); 2933 } 2934 else if (new_defines_function && DECL_INITIAL (olddecl)) 2935 { 2936 /* Never inline re-defined extern inline functions. 2937 FIXME: this could be better handled by keeping both 2938 function as separate declarations. */ 2939 DECL_UNINLINABLE (newdecl) = 1; 2940 } 2941 else 2942 { 2943 if (DECL_PENDING_INLINE_P (olddecl)) 2944 { 2945 DECL_PENDING_INLINE_P (newdecl) = 1; 2946 DECL_PENDING_INLINE_INFO (newdecl) 2947 = DECL_PENDING_INLINE_INFO (olddecl); 2948 } 2949 else if (DECL_PENDING_INLINE_P (newdecl)) 2950 ; 2951 else if (DECL_SAVED_AUTO_RETURN_TYPE (newdecl) == NULL) 2952 DECL_SAVED_AUTO_RETURN_TYPE (newdecl) 2953 = DECL_SAVED_AUTO_RETURN_TYPE (olddecl); 2954 2955 DECL_DECLARED_INLINE_P (newdecl) |= DECL_DECLARED_INLINE_P (olddecl); 2956 2957 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl) 2958 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl)); 2959 2960 DECL_DISREGARD_INLINE_LIMITS (newdecl) 2961 = DECL_DISREGARD_INLINE_LIMITS (olddecl) 2962 = (DECL_DISREGARD_INLINE_LIMITS (newdecl) 2963 || DECL_DISREGARD_INLINE_LIMITS (olddecl)); 2964 } 2965 2966 /* Preserve abstractness on cloned [cd]tors. */ 2967 DECL_ABSTRACT_P (newdecl) = DECL_ABSTRACT_P (olddecl); 2968 2969 /* Update newdecl's parms to point at olddecl. */ 2970 for (parm = DECL_ARGUMENTS (newdecl); parm; 2971 parm = DECL_CHAIN (parm)) 2972 DECL_CONTEXT (parm) = olddecl; 2973 2974 if (! types_match) 2975 { 2976 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl)); 2977 COPY_DECL_ASSEMBLER_NAME (newdecl, olddecl); 2978 COPY_DECL_RTL (newdecl, olddecl); 2979 } 2980 if (! types_match || new_defines_function) 2981 { 2982 /* These are the final DECL_ARGUMENTS that will be used within the 2983 body; update any references to old DECL_ARGUMENTS in the 2984 contracts, if present. */ 2985 if (tree contracts = DECL_CONTRACTS (newdecl)) 2986 remap_contracts (olddecl, newdecl, contracts, true); 2987 2988 /* These need to be copied so that the names are available. 2989 Note that if the types do match, we'll preserve inline 2990 info and other bits, but if not, we won't. */ 2991 DECL_ARGUMENTS (olddecl) = DECL_ARGUMENTS (newdecl); 2992 DECL_RESULT (olddecl) = DECL_RESULT (newdecl); 2993 2994 /* In some cases, duplicate_contracts will remove contracts from 2995 OLDDECL, to avoid duplications. Sometimes, the contracts end up 2996 shared. If we removed them, re-add them. */ 2997 if (!DECL_CONTRACTS (olddecl)) 2998 copy_contract_attributes (olddecl, newdecl); 2999 } 3000 /* If redeclaring a builtin function, it stays built in 3001 if newdecl is a gnu_inline definition, or if newdecl is just 3002 a declaration. */ 3003 if (fndecl_built_in_p (olddecl) 3004 && (new_defines_function ? GNU_INLINE_P (newdecl) : types_match)) 3005 { 3006 copy_decl_built_in_function (newdecl, olddecl); 3007 /* If we're keeping the built-in definition, keep the rtl, 3008 regardless of declaration matches. */ 3009 COPY_DECL_RTL (olddecl, newdecl); 3010 if (DECL_BUILT_IN_CLASS (newdecl) == BUILT_IN_NORMAL) 3011 { 3012 enum built_in_function fncode = DECL_FUNCTION_CODE (newdecl); 3013 if (builtin_decl_explicit_p (fncode)) 3014 { 3015 /* A compatible prototype of these builtin functions 3016 is seen, assume the runtime implements it with 3017 the expected semantics. */ 3018 switch (fncode) 3019 { 3020 case BUILT_IN_STPCPY: 3021 set_builtin_decl_implicit_p (fncode, true); 3022 break; 3023 default: 3024 set_builtin_decl_declared_p (fncode, true); 3025 break; 3026 } 3027 } 3028 3029 copy_attributes_to_builtin (newdecl); 3030 } 3031 } 3032 if (new_defines_function) 3033 /* If defining a function declared with other language 3034 linkage, use the previously declared language linkage. */ 3035 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl)); 3036 else if (types_match) 3037 { 3038 DECL_RESULT (newdecl) = DECL_RESULT (olddecl); 3039 /* Don't clear out the arguments if we're just redeclaring a 3040 function. */ 3041 if (DECL_ARGUMENTS (olddecl)) 3042 { 3043 /* If we removed contracts from previous definition, re-attach 3044 them. Otherwise, rewrite the contracts so they match the 3045 parameters of the new declaration. */ 3046 if (DECL_INITIAL (olddecl) 3047 && DECL_CONTRACTS (newdecl) 3048 && !DECL_CONTRACTS (olddecl)) 3049 copy_contract_attributes (olddecl, newdecl); 3050 else 3051 { 3052 /* Temporarily undo the re-contexting of parameters so we can 3053 actually remap parameters. The inliner won't replace 3054 parameters if we don't do this. */ 3055 tree args = DECL_ARGUMENTS (newdecl); 3056 for (tree p = args; p; p = DECL_CHAIN (p)) 3057 DECL_CONTEXT (p) = newdecl; 3058 3059 /* Save new argument names for use in contracts parsing, 3060 unless we've already started parsing the body of olddecl 3061 (particular issues arise when newdecl is from a prior 3062 friend decl with no argument names, see 3063 modules/contracts-tpl-friend-1). */ 3064 if (tree contracts = DECL_CONTRACTS (olddecl)) 3065 remap_contracts (newdecl, olddecl, contracts, true); 3066 3067 /* And reverse this operation again. */ 3068 for (tree p = args; p; p = DECL_CHAIN (p)) 3069 DECL_CONTEXT (p) = olddecl; 3070 } 3071 3072 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl); 3073 } 3074 } 3075 } 3076 else if (TREE_CODE (newdecl) == NAMESPACE_DECL) 3077 NAMESPACE_LEVEL (newdecl) = NAMESPACE_LEVEL (olddecl); 3078 3079 /* Now preserve various other info from the definition. */ 3080 TREE_ADDRESSABLE (newdecl) = TREE_ADDRESSABLE (olddecl); 3081 TREE_ASM_WRITTEN (newdecl) = TREE_ASM_WRITTEN (olddecl); 3082 DECL_COMMON (newdecl) = DECL_COMMON (olddecl); 3083 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl); 3084 3085 /* Warn about conflicting visibility specifications. */ 3086 if (DECL_VISIBILITY_SPECIFIED (olddecl) 3087 && DECL_VISIBILITY_SPECIFIED (newdecl) 3088 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl)) 3089 { 3090 auto_diagnostic_group d; 3091 if (warning_at (newdecl_loc, OPT_Wattributes, 3092 "%qD: visibility attribute ignored because it " 3093 "conflicts with previous declaration", newdecl)) 3094 inform (olddecl_loc, 3095 "previous declaration of %qD", olddecl); 3096 } 3097 /* Choose the declaration which specified visibility. */ 3098 if (DECL_VISIBILITY_SPECIFIED (olddecl)) 3099 { 3100 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl); 3101 DECL_VISIBILITY_SPECIFIED (newdecl) = 1; 3102 } 3103 /* Init priority used to be merged from newdecl to olddecl by the memcpy, 3104 so keep this behavior. */ 3105 if (VAR_P (newdecl) && DECL_HAS_INIT_PRIORITY_P (newdecl)) 3106 { 3107 SET_DECL_INIT_PRIORITY (olddecl, DECL_INIT_PRIORITY (newdecl)); 3108 DECL_HAS_INIT_PRIORITY_P (olddecl) = 1; 3109 } 3110 /* Likewise for DECL_ALIGN, DECL_USER_ALIGN and DECL_PACKED. */ 3111 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl)) 3112 { 3113 SET_DECL_ALIGN (newdecl, DECL_ALIGN (olddecl)); 3114 DECL_USER_ALIGN (newdecl) |= DECL_USER_ALIGN (olddecl); 3115 } 3116 else if (DECL_ALIGN (olddecl) == DECL_ALIGN (newdecl) 3117 && DECL_USER_ALIGN (olddecl) != DECL_USER_ALIGN (newdecl)) 3118 DECL_USER_ALIGN (newdecl) = 1; 3119 3120 DECL_USER_ALIGN (olddecl) = DECL_USER_ALIGN (newdecl); 3121 if (DECL_WARN_IF_NOT_ALIGN (olddecl) 3122 > DECL_WARN_IF_NOT_ALIGN (newdecl)) 3123 SET_DECL_WARN_IF_NOT_ALIGN (newdecl, 3124 DECL_WARN_IF_NOT_ALIGN (olddecl)); 3125 if (TREE_CODE (newdecl) == FIELD_DECL) 3126 DECL_PACKED (olddecl) = DECL_PACKED (newdecl); 3127 3128 /* Merge module entity mapping information. */ 3129 if (DECL_LANG_SPECIFIC (olddecl) 3130 && (DECL_MODULE_ENTITY_P (olddecl) 3131 || DECL_MODULE_KEYED_DECLS_P (olddecl))) 3132 { 3133 retrofit_lang_decl (newdecl); 3134 DECL_MODULE_ENTITY_P (newdecl) = DECL_MODULE_ENTITY_P (olddecl); 3135 DECL_MODULE_KEYED_DECLS_P (newdecl) = DECL_MODULE_KEYED_DECLS_P (olddecl); 3136 } 3137 3138 /* The DECL_LANG_SPECIFIC information in OLDDECL will be replaced 3139 with that from NEWDECL below. */ 3140 if (DECL_LANG_SPECIFIC (olddecl)) 3141 { 3142 gcc_checking_assert (DECL_LANG_SPECIFIC (olddecl) 3143 != DECL_LANG_SPECIFIC (newdecl)); 3144 ggc_free (DECL_LANG_SPECIFIC (olddecl)); 3145 } 3146 3147 /* Merge the USED information. */ 3148 if (TREE_USED (olddecl)) 3149 TREE_USED (newdecl) = 1; 3150 else if (TREE_USED (newdecl)) 3151 TREE_USED (olddecl) = 1; 3152 3153 if (VAR_P (newdecl)) 3154 { 3155 if (DECL_READ_P (olddecl)) 3156 DECL_READ_P (newdecl) = 1; 3157 else if (DECL_READ_P (newdecl)) 3158 DECL_READ_P (olddecl) = 1; 3159 } 3160 3161 if (DECL_PRESERVE_P (olddecl)) 3162 DECL_PRESERVE_P (newdecl) = 1; 3163 else if (DECL_PRESERVE_P (newdecl)) 3164 DECL_PRESERVE_P (olddecl) = 1; 3165 3166 /* Merge the DECL_FUNCTION_VERSIONED information. newdecl will be copied 3167 to olddecl and deleted. */ 3168 if (TREE_CODE (newdecl) == FUNCTION_DECL 3169 && DECL_FUNCTION_VERSIONED (olddecl)) 3170 { 3171 /* Set the flag for newdecl so that it gets copied to olddecl. */ 3172 DECL_FUNCTION_VERSIONED (newdecl) = 1; 3173 /* newdecl will be purged after copying to olddecl and is no longer 3174 a version. */ 3175 cgraph_node::delete_function_version_by_decl (newdecl); 3176 } 3177 3178 if (TREE_CODE (newdecl) == FUNCTION_DECL) 3179 { 3180 int function_size; 3181 struct symtab_node *snode = symtab_node::get (olddecl); 3182 3183 function_size = sizeof (struct tree_decl_common); 3184 3185 memcpy ((char *) olddecl + sizeof (struct tree_common), 3186 (char *) newdecl + sizeof (struct tree_common), 3187 function_size - sizeof (struct tree_common)); 3188 3189 memcpy ((char *) olddecl + sizeof (struct tree_decl_common), 3190 (char *) newdecl + sizeof (struct tree_decl_common), 3191 sizeof (struct tree_function_decl) - sizeof (struct tree_decl_common)); 3192 3193 /* Preserve symtab node mapping. */ 3194 olddecl->decl_with_vis.symtab_node = snode; 3195 3196 if (new_template_info) 3197 /* If newdecl is a template instantiation, it is possible that 3198 the following sequence of events has occurred: 3199 3200 o A friend function was declared in a class template. The 3201 class template was instantiated. 3202 3203 o The instantiation of the friend declaration was 3204 recorded on the instantiation list, and is newdecl. 3205 3206 o Later, however, instantiate_class_template called pushdecl 3207 on the newdecl to perform name injection. But, pushdecl in 3208 turn called duplicate_decls when it discovered that another 3209 declaration of a global function with the same name already 3210 existed. 3211 3212 o Here, in duplicate_decls, we decided to clobber newdecl. 3213 3214 If we're going to do that, we'd better make sure that 3215 olddecl, and not newdecl, is on the list of 3216 instantiations so that if we try to do the instantiation 3217 again we won't get the clobbered declaration. */ 3218 reregister_specialization (newdecl, 3219 new_template_info, 3220 olddecl); 3221 } 3222 else 3223 { 3224 size_t size = tree_code_size (TREE_CODE (newdecl)); 3225 3226 memcpy ((char *) olddecl + sizeof (struct tree_common), 3227 (char *) newdecl + sizeof (struct tree_common), 3228 sizeof (struct tree_decl_common) - sizeof (struct tree_common)); 3229 3230 switch (TREE_CODE (newdecl)) 3231 { 3232 case LABEL_DECL: 3233 case VAR_DECL: 3234 case RESULT_DECL: 3235 case PARM_DECL: 3236 case FIELD_DECL: 3237 case TYPE_DECL: 3238 case CONST_DECL: 3239 { 3240 struct symtab_node *snode = NULL; 3241 3242 if (VAR_P (newdecl) 3243 && (TREE_STATIC (olddecl) || TREE_PUBLIC (olddecl) 3244 || DECL_EXTERNAL (olddecl))) 3245 snode = symtab_node::get (olddecl); 3246 memcpy ((char *) olddecl + sizeof (struct tree_decl_common), 3247 (char *) newdecl + sizeof (struct tree_decl_common), 3248 size - sizeof (struct tree_decl_common) 3249 + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *)); 3250 if (VAR_P (newdecl)) 3251 olddecl->decl_with_vis.symtab_node = snode; 3252 } 3253 break; 3254 default: 3255 memcpy ((char *) olddecl + sizeof (struct tree_decl_common), 3256 (char *) newdecl + sizeof (struct tree_decl_common), 3257 sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common) 3258 + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *)); 3259 break; 3260 } 3261 } 3262 3263 if (VAR_OR_FUNCTION_DECL_P (newdecl)) 3264 { 3265 if (DECL_EXTERNAL (olddecl) 3266 || TREE_PUBLIC (olddecl) 3267 || TREE_STATIC (olddecl)) 3268 { 3269 /* Merge the section attribute. 3270 We want to issue an error if the sections conflict but that must be 3271 done later in decl_attributes since we are called before attributes 3272 are assigned. */ 3273 if (DECL_SECTION_NAME (newdecl) != NULL) 3274 set_decl_section_name (olddecl, newdecl); 3275 3276 if (DECL_ONE_ONLY (newdecl)) 3277 { 3278 struct symtab_node *oldsym, *newsym; 3279 if (TREE_CODE (olddecl) == FUNCTION_DECL) 3280 oldsym = cgraph_node::get_create (olddecl); 3281 else 3282 oldsym = varpool_node::get_create (olddecl); 3283 newsym = symtab_node::get (newdecl); 3284 oldsym->set_comdat_group (newsym->get_comdat_group ()); 3285 } 3286 } 3287 3288 if (VAR_P (newdecl) 3289 && CP_DECL_THREAD_LOCAL_P (newdecl)) 3290 { 3291 CP_DECL_THREAD_LOCAL_P (olddecl) = true; 3292 if (!processing_template_decl) 3293 set_decl_tls_model (olddecl, DECL_TLS_MODEL (newdecl)); 3294 } 3295 } 3296 3297 DECL_UID (olddecl) = olddecl_uid; 3298 3299 /* NEWDECL contains the merged attribute lists. 3300 Update OLDDECL to be the same. */ 3301 DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl); 3302 3303 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl 3304 so that encode_section_info has a chance to look at the new decl 3305 flags and attributes. */ 3306 if (DECL_RTL_SET_P (olddecl) 3307 && (TREE_CODE (olddecl) == FUNCTION_DECL 3308 || (VAR_P (olddecl) 3309 && TREE_STATIC (olddecl)))) 3310 make_decl_rtl (olddecl); 3311 3312 /* The NEWDECL will no longer be needed. Because every out-of-class 3313 declaration of a member results in a call to duplicate_decls, 3314 freeing these nodes represents in a significant savings. 3315 3316 Before releasing the node, be sore to remove function from symbol 3317 table that might have been inserted there to record comdat group. 3318 Be sure to however do not free DECL_STRUCT_FUNCTION because this 3319 structure is shared in between newdecl and oldecl. */ 3320 if (TREE_CODE (newdecl) == FUNCTION_DECL) 3321 DECL_STRUCT_FUNCTION (newdecl) = NULL; 3322 if (VAR_OR_FUNCTION_DECL_P (newdecl)) 3323 { 3324 struct symtab_node *snode = symtab_node::get (newdecl); 3325 if (snode) 3326 snode->remove (); 3327 } 3328 3329 if (TREE_CODE (olddecl) == FUNCTION_DECL) 3330 { 3331 tree clone; 3332 FOR_EACH_CLONE (clone, olddecl) 3333 { 3334 DECL_ATTRIBUTES (clone) = DECL_ATTRIBUTES (olddecl); 3335 DECL_PRESERVE_P (clone) |= DECL_PRESERVE_P (olddecl); 3336 } 3337 } 3338 3339 /* Remove the associated constraints for newdecl, if any, before 3340 reclaiming memory. */ 3341 if (flag_concepts) 3342 remove_constraints (newdecl); 3343 3344 /* And similarly for any module tracking data. */ 3345 if (modules_p ()) 3346 remove_defining_module (newdecl); 3347 3348 ggc_free (newdecl); 3349 3350 return olddecl; 3351 } 3352 3353 /* Return zero if the declaration NEWDECL is valid 3355 when the declaration OLDDECL (assumed to be for the same name) 3356 has already been seen. 3357 Otherwise return an error message format string with a %s 3358 where the identifier should go. */ 3359 3360 static const char * 3361 redeclaration_error_message (tree newdecl, tree olddecl) 3362 { 3363 if (TREE_CODE (newdecl) == TYPE_DECL) 3364 { 3365 /* Because C++ can put things into name space for free, 3366 constructs like "typedef struct foo { ... } foo" 3367 would look like an erroneous redeclaration. */ 3368 if (same_type_p (TREE_TYPE (newdecl), TREE_TYPE (olddecl))) 3369 return NULL; 3370 else 3371 return G_("redefinition of %q#D"); 3372 } 3373 else if (TREE_CODE (newdecl) == FUNCTION_DECL) 3374 { 3375 /* If this is a pure function, its olddecl will actually be 3376 the original initialization to `0' (which we force to call 3377 abort()). Don't complain about redefinition in this case. */ 3378 if (DECL_LANG_SPECIFIC (olddecl) && DECL_PURE_VIRTUAL_P (olddecl) 3379 && DECL_INITIAL (olddecl) == NULL_TREE) 3380 return NULL; 3381 3382 /* If both functions come from different namespaces, this is not 3383 a redeclaration - this is a conflict with a used function. */ 3384 if (DECL_NAMESPACE_SCOPE_P (olddecl) 3385 && DECL_CONTEXT (olddecl) != DECL_CONTEXT (newdecl) 3386 && ! decls_match (olddecl, newdecl)) 3387 return G_("%qD conflicts with used function"); 3388 3389 /* We'll complain about linkage mismatches in 3390 warn_extern_redeclared_static. */ 3391 3392 /* Defining the same name twice is no good. */ 3393 if (decl_defined_p (olddecl) 3394 && decl_defined_p (newdecl)) 3395 { 3396 if (DECL_NAME (olddecl) == NULL_TREE) 3397 return G_("%q#D not declared in class"); 3398 else if (!GNU_INLINE_P (olddecl) 3399 || GNU_INLINE_P (newdecl)) 3400 return G_("redefinition of %q#D"); 3401 } 3402 3403 if (DECL_DECLARED_INLINE_P (olddecl) && DECL_DECLARED_INLINE_P (newdecl)) 3404 { 3405 bool olda = GNU_INLINE_P (olddecl); 3406 bool newa = GNU_INLINE_P (newdecl); 3407 3408 if (olda != newa) 3409 { 3410 if (newa) 3411 return G_("%q+D redeclared inline with " 3412 "%<gnu_inline%> attribute"); 3413 else 3414 return G_("%q+D redeclared inline without " 3415 "%<gnu_inline%> attribute"); 3416 } 3417 } 3418 3419 /* [class.compare.default]: A definition of a comparison operator as 3420 defaulted that appears in a class shall be the first declaration of 3421 that function. */ 3422 special_function_kind sfk = special_function_p (olddecl); 3423 if (sfk == sfk_comparison && DECL_DEFAULTED_FN (newdecl)) 3424 return G_("comparison operator %q+D defaulted after " 3425 "its first declaration"); 3426 3427 check_abi_tag_redeclaration 3428 (olddecl, lookup_attribute ("abi_tag", DECL_ATTRIBUTES (olddecl)), 3429 lookup_attribute ("abi_tag", DECL_ATTRIBUTES (newdecl))); 3430 3431 return NULL; 3432 } 3433 else if (TREE_CODE (newdecl) == TEMPLATE_DECL) 3434 { 3435 tree nt, ot; 3436 3437 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == CONCEPT_DECL) 3438 return G_("redefinition of %q#D"); 3439 3440 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) != FUNCTION_DECL) 3441 return redeclaration_error_message (DECL_TEMPLATE_RESULT (newdecl), 3442 DECL_TEMPLATE_RESULT (olddecl)); 3443 3444 if (DECL_TEMPLATE_RESULT (newdecl) == DECL_TEMPLATE_RESULT (olddecl)) 3445 return NULL; 3446 3447 nt = DECL_TEMPLATE_RESULT (newdecl); 3448 if (DECL_TEMPLATE_INFO (nt)) 3449 nt = DECL_TEMPLATE_RESULT (template_for_substitution (nt)); 3450 ot = DECL_TEMPLATE_RESULT (olddecl); 3451 if (DECL_TEMPLATE_INFO (ot)) 3452 ot = DECL_TEMPLATE_RESULT (template_for_substitution (ot)); 3453 if (DECL_INITIAL (nt) && DECL_INITIAL (ot) 3454 && (!GNU_INLINE_P (ot) || GNU_INLINE_P (nt))) 3455 return G_("redefinition of %q#D"); 3456 3457 if (DECL_DECLARED_INLINE_P (ot) && DECL_DECLARED_INLINE_P (nt)) 3458 { 3459 bool olda = GNU_INLINE_P (ot); 3460 bool newa = GNU_INLINE_P (nt); 3461 3462 if (olda != newa) 3463 { 3464 if (newa) 3465 return G_("%q+D redeclared inline with " 3466 "%<gnu_inline%> attribute"); 3467 else 3468 return G_("%q+D redeclared inline without " 3469 "%<gnu_inline%> attribute"); 3470 } 3471 } 3472 3473 /* Core issue #226 (C++11): 3474 3475 If a friend function template declaration specifies a 3476 default template-argument, that declaration shall be a 3477 definition and shall be the only declaration of the 3478 function template in the translation unit. */ 3479 if ((cxx_dialect != cxx98) 3480 && TREE_CODE (ot) == FUNCTION_DECL && DECL_UNIQUE_FRIEND_P (ot) 3481 && !check_default_tmpl_args (nt, DECL_TEMPLATE_PARMS (newdecl), 3482 /*is_primary=*/true, 3483 /*is_partial=*/false, 3484 /*is_friend_decl=*/2)) 3485 return G_("redeclaration of friend %q#D " 3486 "may not have default template arguments"); 3487 3488 return NULL; 3489 } 3490 else if (VAR_P (newdecl) 3491 && (CP_DECL_THREAD_LOCAL_P (newdecl) 3492 != CP_DECL_THREAD_LOCAL_P (olddecl)) 3493 && (! DECL_LANG_SPECIFIC (olddecl) 3494 || ! CP_DECL_THREADPRIVATE_P (olddecl) 3495 || CP_DECL_THREAD_LOCAL_P (newdecl))) 3496 { 3497 /* Only variables can be thread-local, and all declarations must 3498 agree on this property. */ 3499 if (CP_DECL_THREAD_LOCAL_P (newdecl)) 3500 return G_("thread-local declaration of %q#D follows " 3501 "non-thread-local declaration"); 3502 else 3503 return G_("non-thread-local declaration of %q#D follows " 3504 "thread-local declaration"); 3505 } 3506 else if (toplevel_bindings_p () || DECL_NAMESPACE_SCOPE_P (newdecl)) 3507 { 3508 /* The objects have been declared at namespace scope. If either 3509 is a member of an anonymous union, then this is an invalid 3510 redeclaration. For example: 3511 3512 int i; 3513 union { int i; }; 3514 3515 is invalid. */ 3516 if ((VAR_P (newdecl) && DECL_ANON_UNION_VAR_P (newdecl)) 3517 || (VAR_P (olddecl) && DECL_ANON_UNION_VAR_P (olddecl))) 3518 return G_("redeclaration of %q#D"); 3519 /* If at least one declaration is a reference, there is no 3520 conflict. For example: 3521 3522 int i = 3; 3523 extern int i; 3524 3525 is valid. */ 3526 if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl)) 3527 return NULL; 3528 3529 /* Static data member declared outside a class definition 3530 if the variable is defined within the class with constexpr 3531 specifier is declaration rather than definition (and 3532 deprecated). */ 3533 if (cxx_dialect >= cxx17 3534 && VAR_P (olddecl) 3535 && DECL_CLASS_SCOPE_P (olddecl) 3536 && DECL_DECLARED_CONSTEXPR_P (olddecl) 3537 && !DECL_INITIAL (newdecl)) 3538 { 3539 DECL_EXTERNAL (newdecl) = 1; 3540 /* For now, only warn with explicit -Wdeprecated. */ 3541 if (OPTION_SET_P (warn_deprecated)) 3542 { 3543 auto_diagnostic_group d; 3544 if (warning_at (DECL_SOURCE_LOCATION (newdecl), OPT_Wdeprecated, 3545 "redundant redeclaration of %<constexpr%> " 3546 "static data member %qD", newdecl)) 3547 inform (DECL_SOURCE_LOCATION (olddecl), 3548 "previous declaration of %qD", olddecl); 3549 } 3550 return NULL; 3551 } 3552 3553 /* Reject two definitions. */ 3554 return G_("redefinition of %q#D"); 3555 } 3556 else 3557 { 3558 /* Objects declared with block scope: */ 3559 /* Reject two definitions, and reject a definition 3560 together with an external reference. */ 3561 if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl))) 3562 return G_("redeclaration of %q#D"); 3563 return NULL; 3564 } 3565 } 3566 3567 3569 /* Hash and equality functions for the named_label table. */ 3570 3571 hashval_t 3572 named_label_hash::hash (const value_type entry) 3573 { 3574 return IDENTIFIER_HASH_VALUE (entry->name); 3575 } 3576 3577 bool 3578 named_label_hash::equal (const value_type entry, compare_type name) 3579 { 3580 return name == entry->name; 3581 } 3582 3583 /* Look for a label named ID in the current function. If one cannot 3584 be found, create one. Return the named_label_entry, or NULL on 3585 failure. */ 3586 3587 static named_label_entry * 3588 lookup_label_1 (tree id, bool making_local_p) 3589 { 3590 auto_cond_timevar tv (TV_NAME_LOOKUP); 3591 3592 /* You can't use labels at global scope. */ 3593 if (current_function_decl == NULL_TREE) 3594 { 3595 error ("label %qE referenced outside of any function", id); 3596 return NULL; 3597 } 3598 3599 if (!named_labels) 3600 named_labels = hash_table<named_label_hash>::create_ggc (13); 3601 3602 hashval_t hash = IDENTIFIER_HASH_VALUE (id); 3603 named_label_entry **slot 3604 = named_labels->find_slot_with_hash (id, hash, INSERT); 3605 named_label_entry *old = *slot; 3606 3607 if (old && old->label_decl) 3608 { 3609 if (!making_local_p) 3610 return old; 3611 3612 if (old->binding_level == current_binding_level) 3613 { 3614 error ("local label %qE conflicts with existing label", id); 3615 inform (DECL_SOURCE_LOCATION (old->label_decl), "previous label"); 3616 return NULL; 3617 } 3618 } 3619 3620 /* We are making a new decl, create or reuse the named_label_entry */ 3621 named_label_entry *ent = NULL; 3622 if (old && !old->label_decl) 3623 ent = old; 3624 else 3625 { 3626 ent = ggc_cleared_alloc<named_label_entry> (); 3627 ent->name = id; 3628 ent->outer = old; 3629 *slot = ent; 3630 } 3631 3632 /* Now create the LABEL_DECL. */ 3633 tree decl = build_decl (input_location, LABEL_DECL, id, void_type_node); 3634 3635 DECL_CONTEXT (decl) = current_function_decl; 3636 SET_DECL_MODE (decl, VOIDmode); 3637 if (making_local_p) 3638 { 3639 C_DECLARED_LABEL_FLAG (decl) = true; 3640 DECL_CHAIN (decl) = current_binding_level->names; 3641 current_binding_level->names = decl; 3642 } 3643 3644 ent->label_decl = decl; 3645 3646 return ent; 3647 } 3648 3649 /* Wrapper for lookup_label_1. */ 3650 3651 tree 3652 lookup_label (tree id) 3653 { 3654 named_label_entry *ent = lookup_label_1 (id, false); 3655 return ent ? ent->label_decl : NULL_TREE; 3656 } 3657 3658 /* Remember that we've seen &&ID. */ 3659 3660 void 3661 mark_label_addressed (tree id) 3662 { 3663 named_label_entry *ent = lookup_label_1 (id, false); 3664 ent->addressed = true; 3665 } 3666 3667 tree 3668 declare_local_label (tree id) 3669 { 3670 named_label_entry *ent = lookup_label_1 (id, true); 3671 return ent ? ent->label_decl : NULL_TREE; 3672 } 3673 3674 /* Returns true if it is ill-formed to jump past the declaration of DECL. */ 3675 3676 static bool 3677 decl_jump_unsafe (tree decl) 3678 { 3679 /* [stmt.dcl]/3: A program that jumps from a point where a local variable 3680 with automatic storage duration is not in scope to a point where it is 3681 in scope is ill-formed unless the variable has scalar type, class type 3682 with a trivial default constructor and a trivial destructor, a 3683 cv-qualified version of one of these types, or an array of one of the 3684 preceding types and is declared without an initializer (8.5). */ 3685 tree type = TREE_TYPE (decl); 3686 3687 return (type != error_mark_node 3688 && VAR_P (decl) 3689 && !TREE_STATIC (decl) 3690 && (DECL_NONTRIVIALLY_INITIALIZED_P (decl) 3691 || variably_modified_type_p (type, NULL_TREE))); 3692 } 3693 3694 /* A subroutine of check_previous_goto_1 and check_goto to identify a branch 3695 to the user. */ 3696 3697 static bool 3698 identify_goto (tree decl, location_t loc, const location_t *locus, 3699 diagnostic_t diag_kind, bool computed) 3700 { 3701 if (computed) 3702 diag_kind = DK_WARNING; 3703 bool complained 3704 = emit_diagnostic (diag_kind, loc, 0, 3705 decl ? G_("jump to label %qD") 3706 : G_("jump to case label"), decl); 3707 if (complained && locus) 3708 { 3709 if (computed) 3710 inform (*locus, " as a possible target of computed goto"); 3711 else 3712 inform (*locus, " from here"); 3713 } 3714 return complained; 3715 } 3716 3717 /* Check that a single previously seen jump to a newly defined label 3718 is OK. DECL is the LABEL_DECL or 0; LEVEL is the binding_level for 3719 the jump context; NAMES are the names in scope in LEVEL at the jump 3720 context; LOCUS is the source position of the jump or 0. COMPUTED 3721 is a vec of decls if the jump is a computed goto. Returns 3722 true if all is well. */ 3723 3724 static bool 3725 check_previous_goto_1 (tree decl, cp_binding_level* level, tree names, 3726 bool exited_omp, const location_t *locus, 3727 vec<tree,va_gc> *computed) 3728 { 3729 cp_binding_level *b; 3730 bool complained = false; 3731 int identified = 0; 3732 bool saw_eh = false, saw_omp = false, saw_tm = false, saw_cxif = false; 3733 bool saw_ceif = false, saw_se = false; 3734 3735 if (exited_omp) 3736 { 3737 complained = identify_goto (decl, input_location, locus, DK_ERROR, 3738 computed); 3739 if (complained) 3740 inform (input_location, " exits OpenMP structured block"); 3741 saw_omp = true; 3742 identified = 2; 3743 } 3744 3745 for (b = current_binding_level; b ; b = b->level_chain) 3746 { 3747 tree new_decls, old_decls = (b == level ? names : NULL_TREE); 3748 3749 for (new_decls = b->names; new_decls != old_decls; 3750 new_decls = (DECL_P (new_decls) ? DECL_CHAIN (new_decls) 3751 : TREE_CHAIN (new_decls))) 3752 { 3753 bool problem = decl_jump_unsafe (new_decls); 3754 if (! problem) 3755 continue; 3756 3757 if (!identified) 3758 { 3759 complained = identify_goto (decl, input_location, locus, DK_ERROR, 3760 computed); 3761 identified = 2; 3762 } 3763 if (complained) 3764 inform (DECL_SOURCE_LOCATION (new_decls), 3765 " crosses initialization of %q#D", new_decls); 3766 } 3767 3768 if (b == level) 3769 break; 3770 3771 const char *inf = NULL; 3772 location_t loc = input_location; 3773 switch (b->kind) 3774 { 3775 case sk_try: 3776 if (!saw_eh) 3777 inf = G_(" enters %<try%> block"); 3778 saw_eh = true; 3779 break; 3780 3781 case sk_catch: 3782 if (!saw_eh) 3783 inf = G_(" enters %<catch%> block"); 3784 saw_eh = true; 3785 break; 3786 3787 case sk_omp: 3788 if (!saw_omp) 3789 inf = G_(" enters OpenMP structured block"); 3790 saw_omp = true; 3791 break; 3792 3793 case sk_transaction: 3794 if (!saw_tm) 3795 inf = G_(" enters synchronized or atomic statement"); 3796 saw_tm = true; 3797 break; 3798 3799 case sk_stmt_expr: 3800 if (!saw_se) 3801 inf = G_(" enters statement expression"); 3802 saw_se = true; 3803 break; 3804 3805 case sk_block: 3806 if (!saw_cxif && level_for_constexpr_if (b->level_chain)) 3807 { 3808 inf = G_(" enters %<constexpr if%> statement"); 3809 loc = EXPR_LOCATION (b->level_chain->this_entity); 3810 saw_cxif = true; 3811 } 3812 else if (!saw_ceif && level_for_consteval_if (b->level_chain)) 3813 { 3814 inf = G_(" enters %<consteval if%> statement"); 3815 loc = EXPR_LOCATION (b->level_chain->this_entity); 3816 saw_ceif = true; 3817 } 3818 break; 3819 3820 default: 3821 break; 3822 } 3823 3824 if (inf) 3825 { 3826 if (identified < 2) 3827 complained = identify_goto (decl, input_location, locus, DK_ERROR, 3828 computed); 3829 identified = 2; 3830 if (complained) 3831 inform (loc, inf); 3832 } 3833 } 3834 3835 if (!vec_safe_is_empty (computed)) 3836 { 3837 if (!identified) 3838 complained = identify_goto (decl, input_location, locus, DK_ERROR, 3839 computed); 3840 identified = 2; 3841 if (complained) 3842 for (tree d : computed) 3843 { 3844 if (DECL_P (d)) 3845 inform (DECL_SOURCE_LOCATION (d), " does not destroy %qD", d); 3846 else if (d == get_identifier ("catch")) 3847 inform (*locus, " does not clean up handled exception"); 3848 } 3849 } 3850 3851 return !identified; 3852 } 3853 3854 static void 3855 check_previous_goto (tree decl, struct named_label_use_entry *use) 3856 { 3857 check_previous_goto_1 (decl, use->binding_level, 3858 use->names_in_scope, use->in_omp_scope, 3859 &use->o_goto_locus, use->computed_goto); 3860 } 3861 3862 static bool 3863 check_switch_goto (cp_binding_level* level) 3864 { 3865 return check_previous_goto_1 (NULL_TREE, level, level->names, 3866 false, NULL, nullptr); 3867 } 3868 3869 /* Check that a new jump to a label ENT is OK. COMPUTED is true 3870 if this is a possible target of a computed goto. */ 3871 3872 void 3873 check_goto_1 (named_label_entry *ent, bool computed) 3874 { 3875 tree decl = ent->label_decl; 3876 3877 /* If the label hasn't been defined yet, defer checking. */ 3878 if (! DECL_INITIAL (decl)) 3879 { 3880 /* Don't bother creating another use if the last goto had the 3881 same data, and will therefore create the same set of errors. */ 3882 if (ent->uses 3883 && ent->uses->names_in_scope == current_binding_level->names) 3884 return; 3885 3886 named_label_use_entry *new_use 3887 = ggc_alloc<named_label_use_entry> (); 3888 new_use->binding_level = current_binding_level; 3889 new_use->names_in_scope = current_binding_level->names; 3890 new_use->o_goto_locus = input_location; 3891 new_use->in_omp_scope = false; 3892 new_use->computed_goto = computed ? make_tree_vector () : nullptr; 3893 3894 new_use->next = ent->uses; 3895 ent->uses = new_use; 3896 return; 3897 } 3898 3899 bool saw_catch = false, complained = false; 3900 int identified = 0; 3901 tree bad; 3902 unsigned ix; 3903 3904 if (ent->in_try_scope || ent->in_catch_scope || ent->in_transaction_scope 3905 || ent->in_constexpr_if || ent->in_consteval_if 3906 || ent->in_omp_scope || ent->in_stmt_expr 3907 || !vec_safe_is_empty (ent->bad_decls)) 3908 { 3909 diagnostic_t diag_kind = DK_PERMERROR; 3910 if (ent->in_try_scope || ent->in_catch_scope || ent->in_constexpr_if 3911 || ent->in_consteval_if || ent->in_transaction_scope 3912 || ent->in_omp_scope || ent->in_stmt_expr) 3913 diag_kind = DK_ERROR; 3914 complained = identify_goto (decl, DECL_SOURCE_LOCATION (decl), 3915 &input_location, diag_kind, computed); 3916 identified = 1 + (diag_kind == DK_ERROR); 3917 } 3918 3919 FOR_EACH_VEC_SAFE_ELT (ent->bad_decls, ix, bad) 3920 { 3921 bool problem = decl_jump_unsafe (bad); 3922 3923 if (problem && DECL_ARTIFICIAL (bad)) 3924 { 3925 /* Can't skip init of __exception_info. */ 3926 if (identified == 1) 3927 { 3928 complained = identify_goto (decl, DECL_SOURCE_LOCATION (decl), 3929 &input_location, DK_ERROR, computed); 3930 identified = 2; 3931 } 3932 if (complained) 3933 inform (DECL_SOURCE_LOCATION (bad), " enters %<catch%> block"); 3934 saw_catch = true; 3935 } 3936 else if (complained) 3937 inform (DECL_SOURCE_LOCATION (bad), 3938 " skips initialization of %q#D", bad); 3939 } 3940 3941 if (complained) 3942 { 3943 if (ent->in_try_scope) 3944 inform (input_location, " enters %<try%> block"); 3945 else if (ent->in_catch_scope && !saw_catch) 3946 inform (input_location, " enters %<catch%> block"); 3947 else if (ent->in_transaction_scope) 3948 inform (input_location, " enters synchronized or atomic statement"); 3949 else if (ent->in_constexpr_if) 3950 inform (input_location, " enters %<constexpr if%> statement"); 3951 else if (ent->in_consteval_if) 3952 inform (input_location, " enters %<consteval if%> statement"); 3953 else if (ent->in_stmt_expr) 3954 inform (input_location, " enters statement expression"); 3955 } 3956 3957 if (ent->in_omp_scope) 3958 { 3959 if (complained) 3960 inform (input_location, " enters OpenMP structured block"); 3961 } 3962 else if (flag_openmp) 3963 for (cp_binding_level *b = current_binding_level; b ; b = b->level_chain) 3964 { 3965 if (b == ent->binding_level) 3966 break; 3967 if (b->kind == sk_omp) 3968 { 3969 if (identified < 2) 3970 { 3971 complained = identify_goto (decl, 3972 DECL_SOURCE_LOCATION (decl), 3973 &input_location, DK_ERROR, 3974 computed); 3975 identified = 2; 3976 } 3977 if (complained) 3978 inform (input_location, " exits OpenMP structured block"); 3979 break; 3980 } 3981 } 3982 3983 /* Warn if a computed goto might involve a local variable going out of scope 3984 without being cleaned up. */ 3985 if (computed) 3986 { 3987 auto level = ent->binding_level; 3988 auto names = ent->names_in_scope; 3989 for (auto b = current_binding_level; ; b = b->level_chain) 3990 { 3991 if (b->kind == sk_catch) 3992 { 3993 if (!identified) 3994 { 3995 complained 3996 = identify_goto (decl, DECL_SOURCE_LOCATION (decl), 3997 &input_location, DK_ERROR, computed); 3998 identified = 2; 3999 } 4000 if (complained) 4001 inform (input_location, 4002 " does not clean up handled exception"); 4003 } 4004 tree end = b == level ? names : NULL_TREE; 4005 for (tree d = b->names; d != end; d = DECL_CHAIN (d)) 4006 { 4007 if (TREE_CODE (d) == VAR_DECL && !TREE_STATIC (d) 4008 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (d))) 4009 { 4010 if (!identified) 4011 { 4012 complained 4013 = identify_goto (decl, DECL_SOURCE_LOCATION (decl), 4014 &input_location, DK_ERROR, computed); 4015 identified = 2; 4016 } 4017 if (complained) 4018 inform (DECL_SOURCE_LOCATION (d), 4019 " does not destroy %qD", d); 4020 } 4021 } 4022 if (b == level) 4023 break; 4024 } 4025 } 4026 } 4027 4028 /* Check that a new jump to a label DECL is OK. Called by 4029 finish_goto_stmt. */ 4030 4031 void 4032 check_goto (tree decl) 4033 { 4034 if (!named_labels) 4035 return; 4036 if (TREE_CODE (decl) != LABEL_DECL) 4037 { 4038 /* We don't know where a computed goto is jumping, 4039 so check all addressable labels. */ 4040 for (auto iter = named_labels->begin (); 4041 iter != named_labels->end (); 4042 ++iter) 4043 { 4044 auto ent = *iter; 4045 if (ent->addressed) 4046 check_goto_1 (ent, true); 4047 } 4048 } 4049 else 4050 { 4051 hashval_t hash = IDENTIFIER_HASH_VALUE (DECL_NAME (decl)); 4052 named_label_entry **slot 4053 = named_labels->find_slot_with_hash (DECL_NAME (decl), hash, NO_INSERT); 4054 named_label_entry *ent = *slot; 4055 check_goto_1 (ent, false); 4056 } 4057 } 4058 4059 /* Check that a return is ok wrt OpenMP structured blocks. 4060 Called by finish_return_stmt. Returns true if all is well. */ 4061 4062 bool 4063 check_omp_return (void) 4064 { 4065 for (cp_binding_level *b = current_binding_level; b ; b = b->level_chain) 4066 if (b->kind == sk_omp) 4067 { 4068 error ("invalid exit from OpenMP structured block"); 4069 return false; 4070 } 4071 else if (b->kind == sk_function_parms) 4072 break; 4073 return true; 4074 } 4075 4076 /* Define a label, specifying the location in the source file. 4077 Return the LABEL_DECL node for the label. */ 4078 4079 tree 4080 define_label (location_t location, tree name) 4081 { 4082 auto_cond_timevar tv (TV_NAME_LOOKUP); 4083 4084 /* After labels, make any new cleanups in the function go into their 4085 own new (temporary) binding contour. */ 4086 for (cp_binding_level *p = current_binding_level; 4087 p->kind != sk_function_parms; 4088 p = p->level_chain) 4089 p->more_cleanups_ok = 0; 4090 4091 named_label_entry *ent = lookup_label_1 (name, false); 4092 tree decl = ent->label_decl; 4093 4094 if (DECL_INITIAL (decl) != NULL_TREE) 4095 { 4096 error ("duplicate label %qD", decl); 4097 return error_mark_node; 4098 } 4099 else 4100 { 4101 /* Mark label as having been defined. */ 4102 DECL_INITIAL (decl) = error_mark_node; 4103 /* Say where in the source. */ 4104 DECL_SOURCE_LOCATION (decl) = location; 4105 4106 ent->binding_level = current_binding_level; 4107 ent->names_in_scope = current_binding_level->names; 4108 4109 for (named_label_use_entry *use = ent->uses; use; use = use->next) 4110 check_previous_goto (decl, use); 4111 ent->uses = NULL; 4112 } 4113 4114 return decl; 4115 } 4116 4117 struct cp_switch 4118 { 4119 cp_binding_level *level; 4120 struct cp_switch *next; 4121 /* The SWITCH_STMT being built. */ 4122 tree switch_stmt; 4123 /* A splay-tree mapping the low element of a case range to the high 4124 element, or NULL_TREE if there is no high element. Used to 4125 determine whether or not a new case label duplicates an old case 4126 label. We need a tree, rather than simply a hash table, because 4127 of the GNU case range extension. */ 4128 splay_tree cases; 4129 /* Remember whether a default: case label has been seen. */ 4130 bool has_default_p; 4131 /* Remember whether a BREAK_STMT has been seen in this SWITCH_STMT. */ 4132 bool break_stmt_seen_p; 4133 /* Set if inside of {FOR,DO,WHILE}_BODY nested inside of a switch, 4134 where BREAK_STMT doesn't belong to the SWITCH_STMT. */ 4135 bool in_loop_body_p; 4136 }; 4137 4138 /* A stack of the currently active switch statements. The innermost 4139 switch statement is on the top of the stack. There is no need to 4140 mark the stack for garbage collection because it is only active 4141 during the processing of the body of a function, and we never 4142 collect at that point. */ 4143 4144 static struct cp_switch *switch_stack; 4145 4146 /* Called right after a switch-statement condition is parsed. 4147 SWITCH_STMT is the switch statement being parsed. */ 4148 4149 void 4150 push_switch (tree switch_stmt) 4151 { 4152 struct cp_switch *p = XNEW (struct cp_switch); 4153 p->level = current_binding_level; 4154 p->next = switch_stack; 4155 p->switch_stmt = switch_stmt; 4156 p->cases = splay_tree_new (case_compare, NULL, NULL); 4157 p->has_default_p = false; 4158 p->break_stmt_seen_p = false; 4159 p->in_loop_body_p = false; 4160 switch_stack = p; 4161 } 4162 4163 void 4164 pop_switch (void) 4165 { 4166 struct cp_switch *cs = switch_stack; 4167 4168 /* Emit warnings as needed. */ 4169 location_t switch_location = cp_expr_loc_or_input_loc (cs->switch_stmt); 4170 tree cond = SWITCH_STMT_COND (cs->switch_stmt); 4171 const bool bool_cond_p 4172 = (SWITCH_STMT_TYPE (cs->switch_stmt) 4173 && TREE_CODE (SWITCH_STMT_TYPE (cs->switch_stmt)) == BOOLEAN_TYPE); 4174 if (!processing_template_decl) 4175 c_do_switch_warnings (cs->cases, switch_location, 4176 SWITCH_STMT_TYPE (cs->switch_stmt), cond, 4177 bool_cond_p); 4178 4179 /* For the benefit of block_may_fallthru remember if the switch body 4180 case labels cover all possible values and if there are break; stmts. */ 4181 if (cs->has_default_p 4182 || (!processing_template_decl 4183 && c_switch_covers_all_cases_p (cs->cases, 4184 SWITCH_STMT_TYPE (cs->switch_stmt)))) 4185 SWITCH_STMT_ALL_CASES_P (cs->switch_stmt) = 1; 4186 if (!cs->break_stmt_seen_p) 4187 SWITCH_STMT_NO_BREAK_P (cs->switch_stmt) = 1; 4188 /* Now that we're done with the switch warnings, set the switch type 4189 to the type of the condition if the index type was of scoped enum type. 4190 (Such types don't participate in the integer promotions.) We do this 4191 because of bit-fields whose declared type is a scoped enum type: 4192 gimplification will use the lowered index type, but convert the 4193 case values to SWITCH_STMT_TYPE, which would have been the declared type 4194 and verify_gimple_switch doesn't accept that. */ 4195 if (is_bitfield_expr_with_lowered_type (cond)) 4196 SWITCH_STMT_TYPE (cs->switch_stmt) = TREE_TYPE (cond); 4197 gcc_assert (!cs->in_loop_body_p); 4198 splay_tree_delete (cs->cases); 4199 switch_stack = switch_stack->next; 4200 free (cs); 4201 } 4202 4203 /* Note that a BREAK_STMT is about to be added. If it is inside of 4204 a SWITCH_STMT and not inside of a loop body inside of it, note 4205 in switch_stack we've seen a BREAK_STMT. */ 4206 4207 void 4208 note_break_stmt (void) 4209 { 4210 if (switch_stack && !switch_stack->in_loop_body_p) 4211 switch_stack->break_stmt_seen_p = true; 4212 } 4213 4214 /* Note the start of processing of an iteration statement's body. 4215 The note_break_stmt function will do nothing while processing it. 4216 Return a flag that should be passed to note_iteration_stmt_body_end. */ 4217 4218 bool 4219 note_iteration_stmt_body_start (void) 4220 { 4221 if (!switch_stack) 4222 return false; 4223 bool ret = switch_stack->in_loop_body_p; 4224 switch_stack->in_loop_body_p = true; 4225 return ret; 4226 } 4227 4228 /* Note the end of processing of an iteration statement's body. */ 4229 4230 void 4231 note_iteration_stmt_body_end (bool prev) 4232 { 4233 if (switch_stack) 4234 switch_stack->in_loop_body_p = prev; 4235 } 4236 4237 /* Convert a case constant VALUE in a switch to the type TYPE of the switch 4238 condition. Note that if TYPE and VALUE are already integral we don't 4239 really do the conversion because the language-independent 4240 warning/optimization code will work better that way. */ 4241 4242 static tree 4243 case_conversion (tree type, tree value) 4244 { 4245 if (value == NULL_TREE) 4246 return value; 4247 4248 value = mark_rvalue_use (value); 4249 4250 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type)) 4251 type = type_promotes_to (type); 4252 4253 tree ovalue = value; 4254 /* The constant-expression VALUE shall be a converted constant expression 4255 of the adjusted type of the switch condition, which doesn't allow 4256 narrowing conversions. */ 4257 value = build_converted_constant_expr (type, value, tf_warning_or_error); 4258 4259 if (cxx_dialect >= cxx11 4260 && (SCOPED_ENUM_P (type) 4261 || !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (ovalue)))) 4262 /* Use the converted value. */; 4263 else 4264 /* The already integral case. */ 4265 value = ovalue; 4266 4267 return cxx_constant_value (value); 4268 } 4269 4270 /* Note that we've seen a definition of a case label, and complain if this 4271 is a bad place for one. */ 4272 4273 tree 4274 finish_case_label (location_t loc, tree low_value, tree high_value) 4275 { 4276 tree cond, r; 4277 cp_binding_level *p; 4278 tree type; 4279 4280 if (low_value == NULL_TREE && high_value == NULL_TREE) 4281 switch_stack->has_default_p = true; 4282 4283 if (processing_template_decl) 4284 { 4285 tree label; 4286 4287 /* For templates, just add the case label; we'll do semantic 4288 analysis at instantiation-time. */ 4289 label = build_decl (loc, LABEL_DECL, NULL_TREE, void_type_node); 4290 return add_stmt (build_case_label (low_value, high_value, label)); 4291 } 4292 4293 /* Find the condition on which this switch statement depends. */ 4294 cond = SWITCH_STMT_COND (switch_stack->switch_stmt); 4295 if (cond && TREE_CODE (cond) == TREE_LIST) 4296 cond = TREE_VALUE (cond); 4297 4298 if (!check_switch_goto (switch_stack->level)) 4299 return error_mark_node; 4300 4301 type = SWITCH_STMT_TYPE (switch_stack->switch_stmt); 4302 if (type == error_mark_node) 4303 return error_mark_node; 4304 4305 low_value = case_conversion (type, low_value); 4306 high_value = case_conversion (type, high_value); 4307 4308 r = c_add_case_label (loc, switch_stack->cases, cond, low_value, high_value); 4309 4310 /* After labels, make any new cleanups in the function go into their 4311 own new (temporary) binding contour. */ 4312 for (p = current_binding_level; 4313 p->kind != sk_function_parms; 4314 p = p->level_chain) 4315 p->more_cleanups_ok = 0; 4316 4317 return r; 4318 } 4319 4320 struct typename_info { 4322 tree scope; 4323 tree name; 4324 tree template_id; 4325 bool enum_p; 4326 bool class_p; 4327 }; 4328 4329 struct typename_hasher : ggc_ptr_hash<tree_node> 4330 { 4331 typedef typename_info *compare_type; 4332 4333 /* Hash a TYPENAME_TYPE. */ 4334 4335 static hashval_t 4336 hash (tree context, tree fullname) 4337 { 4338 hashval_t hash = 0; 4339 hash = iterative_hash_object (context, hash); 4340 hash = iterative_hash_object (fullname, hash); 4341 return hash; 4342 } 4343 4344 static hashval_t 4345 hash (const typename_info *ti) 4346 { 4347 return typename_hasher::hash (ti->scope, ti->template_id); 4348 } 4349 4350 static hashval_t 4351 hash (tree t) 4352 { 4353 return typename_hasher::hash (TYPE_CONTEXT (t), TYPENAME_TYPE_FULLNAME (t)); 4354 } 4355 4356 /* Compare two TYPENAME_TYPEs. */ 4357 4358 static bool 4359 equal (tree t1, const typename_info *t2) 4360 { 4361 return (TYPE_IDENTIFIER (t1) == t2->name 4362 && TYPE_CONTEXT (t1) == t2->scope 4363 && TYPENAME_TYPE_FULLNAME (t1) == t2->template_id 4364 && TYPENAME_IS_ENUM_P (t1) == t2->enum_p 4365 && TYPENAME_IS_CLASS_P (t1) == t2->class_p); 4366 } 4367 }; 4368 4369 /* Build a TYPENAME_TYPE. If the type is `typename T::t', CONTEXT is 4370 the type of `T', NAME is the IDENTIFIER_NODE for `t'. 4371 4372 Returns the new TYPENAME_TYPE. */ 4373 4374 static GTY (()) hash_table<typename_hasher> *typename_htab; 4375 4376 tree 4377 build_typename_type (tree context, tree name, tree fullname, 4378 enum tag_types tag_type) 4379 { 4380 typename_info ti; 4381 4382 if (typename_htab == NULL) 4383 typename_htab = hash_table<typename_hasher>::create_ggc (61); 4384 4385 ti.scope = FROB_CONTEXT (context); 4386 ti.name = name; 4387 ti.template_id = fullname; 4388 ti.enum_p = tag_type == enum_type; 4389 ti.class_p = (tag_type == class_type 4390 || tag_type == record_type 4391 || tag_type == union_type); 4392 hashval_t hash = typename_hasher::hash (&ti); 4393 4394 /* See if we already have this type. */ 4395 tree *e = typename_htab->find_slot_with_hash (&ti, hash, INSERT); 4396 tree t = *e; 4397 if (*e) 4398 t = *e; 4399 else 4400 { 4401 /* Build the TYPENAME_TYPE. */ 4402 t = cxx_make_type (TYPENAME_TYPE); 4403 TYPE_CONTEXT (t) = ti.scope; 4404 TYPENAME_TYPE_FULLNAME (t) = ti.template_id; 4405 TYPENAME_IS_ENUM_P (t) = ti.enum_p; 4406 TYPENAME_IS_CLASS_P (t) = ti.class_p; 4407 4408 /* Build the corresponding TYPE_DECL. */ 4409 tree d = build_decl (input_location, TYPE_DECL, name, t); 4410 TYPE_NAME (t) = d; 4411 TYPE_STUB_DECL (t) = d; 4412 DECL_CONTEXT (d) = ti.scope; 4413 DECL_ARTIFICIAL (d) = 1; 4414 4415 /* Store it in the hash table. */ 4416 *e = t; 4417 4418 /* TYPENAME_TYPEs must always be compared structurally, because 4419 they may or may not resolve down to another type depending on 4420 the currently open classes. */ 4421 SET_TYPE_STRUCTURAL_EQUALITY (t); 4422 } 4423 4424 return t; 4425 } 4426 4427 /* Resolve `typename CONTEXT::NAME'. TAG_TYPE indicates the tag 4428 provided to name the type. Returns an appropriate type, unless an 4429 error occurs, in which case error_mark_node is returned. If we 4430 locate a non-artificial TYPE_DECL and TF_KEEP_TYPE_DECL is set, we 4431 return that, rather than the _TYPE it corresponds to, in other 4432 cases we look through the type decl. If TF_ERROR is set, complain 4433 about errors, otherwise be quiet. */ 4434 4435 tree 4436 make_typename_type (tree context, tree name, enum tag_types tag_type, 4437 tsubst_flags_t complain) 4438 { 4439 tree fullname; 4440 tree t; 4441 bool want_template; 4442 4443 if (name == error_mark_node 4444 || context == NULL_TREE 4445 || context == error_mark_node) 4446 return error_mark_node; 4447 4448 if (TYPE_P (name)) 4449 { 4450 if (!(TYPE_LANG_SPECIFIC (name) 4451 && (CLASSTYPE_IS_TEMPLATE (name) 4452 || CLASSTYPE_USE_TEMPLATE (name)))) 4453 name = TYPE_IDENTIFIER (name); 4454 else 4455 /* Create a TEMPLATE_ID_EXPR for the type. */ 4456 name = build_nt (TEMPLATE_ID_EXPR, 4457 CLASSTYPE_TI_TEMPLATE (name), 4458 CLASSTYPE_TI_ARGS (name)); 4459 } 4460 else if (TREE_CODE (name) == TYPE_DECL) 4461 name = DECL_NAME (name); 4462 4463 fullname = name; 4464 4465 if (TREE_CODE (name) == TEMPLATE_ID_EXPR) 4466 { 4467 name = TREE_OPERAND (name, 0); 4468 if (DECL_TYPE_TEMPLATE_P (name)) 4469 name = TREE_OPERAND (fullname, 0) = DECL_NAME (name); 4470 if (TREE_CODE (name) != IDENTIFIER_NODE) 4471 { 4472 if (complain & tf_error) 4473 error ("%qD is not a type", name); 4474 return error_mark_node; 4475 } 4476 } 4477 if (TREE_CODE (name) == TEMPLATE_DECL) 4478 { 4479 if (complain & tf_error) 4480 error ("%qD used without template arguments", name); 4481 return error_mark_node; 4482 } 4483 else if (is_overloaded_fn (name)) 4484 { 4485 if (complain & tf_error) 4486 error ("%qD is a function, not a type", name); 4487 return error_mark_node; 4488 } 4489 gcc_assert (identifier_p (name)); 4490 gcc_assert (TYPE_P (context)); 4491 4492 if (TREE_CODE (context) == TYPE_PACK_EXPANSION) 4493 /* This can happen for C++17 variadic using (c++/88986). */; 4494 else if (!MAYBE_CLASS_TYPE_P (context)) 4495 { 4496 if (complain & tf_error) 4497 error ("%q#T is not a class", context); 4498 return error_mark_node; 4499 } 4500 4501 /* When the CONTEXT is a dependent type, NAME could refer to a 4502 dependent base class of CONTEXT. But look inside it anyway 4503 if CONTEXT is a currently open scope, in case it refers to a 4504 member of the current instantiation or a non-dependent base; 4505 lookup will stop when we hit a dependent base. */ 4506 if (!dependent_scope_p (context)) 4507 { 4508 /* We generally don't ignore non-types during TYPENAME_TYPE lookup 4509 (as per [temp.res.general]/3), unless 4510 - the tag corresponds to a class-key or 'enum' so 4511 [basic.lookup.elab] applies, or 4512 - the tag corresponds to scope_type or tf_qualifying_scope is 4513 set so [basic.lookup.qual]/1 applies. 4514 TODO: If we'd set/track the scope_type tag thoroughly on all 4515 TYPENAME_TYPEs that are followed by :: then we wouldn't need the 4516 tf_qualifying_scope flag. */ 4517 bool want_type = (tag_type != none_type && tag_type != typename_type) 4518 || (complain & tf_qualifying_scope); 4519 t = lookup_member (context, name, /*protect=*/2, want_type, complain); 4520 } 4521 else 4522 t = NULL_TREE; 4523 4524 if ((!t || TREE_CODE (t) == TREE_LIST) && dependent_type_p (context)) 4525 return build_typename_type (context, name, fullname, tag_type); 4526 4527 want_template = TREE_CODE (fullname) == TEMPLATE_ID_EXPR; 4528 4529 if (!t) 4530 { 4531 if (complain & tf_error) 4532 { 4533 if (!COMPLETE_TYPE_P (context)) 4534 cxx_incomplete_type_error (NULL_TREE, context); 4535 else 4536 error (want_template ? G_("no class template named %q#T in %q#T") 4537 : G_("no type named %q#T in %q#T"), name, context); 4538 } 4539 return error_mark_node; 4540 } 4541 4542 /* Pull out the template from an injected-class-name (or multiple). */ 4543 if (want_template) 4544 t = maybe_get_template_decl_from_type_decl (t); 4545 4546 if (TREE_CODE (t) == TREE_LIST) 4547 { 4548 if (complain & tf_error) 4549 { 4550 error ("lookup of %qT in %qT is ambiguous", name, context); 4551 print_candidates (t); 4552 } 4553 return error_mark_node; 4554 } 4555 4556 if (want_template && !DECL_TYPE_TEMPLATE_P (t)) 4557 { 4558 if (complain & tf_error) 4559 error ("%<typename %T::%D%> names %q#T, which is not a class template", 4560 context, name, t); 4561 return error_mark_node; 4562 } 4563 if (!want_template && TREE_CODE (t) != TYPE_DECL) 4564 { 4565 if ((complain & tf_tst_ok) && cxx_dialect >= cxx17 4566 && DECL_TYPE_TEMPLATE_P (t)) 4567 /* The caller permits this typename-specifier to name a template 4568 (because it appears in a CTAD-enabled context). */; 4569 else 4570 { 4571 if (complain & tf_error) 4572 error ("%<typename %T::%D%> names %q#D, which is not a type", 4573 context, name, t); 4574 return error_mark_node; 4575 } 4576 } 4577 4578 if (!check_accessibility_of_qualified_id (t, /*object_type=*/NULL_TREE, 4579 context, complain)) 4580 return error_mark_node; 4581 4582 if (!want_template && DECL_TYPE_TEMPLATE_P (t)) 4583 return make_template_placeholder (t); 4584 4585 if (want_template) 4586 { 4587 t = lookup_template_class (t, TREE_OPERAND (fullname, 1), 4588 NULL_TREE, context, 4589 /*entering_scope=*/0, 4590 complain | tf_user); 4591 if (t == error_mark_node) 4592 return error_mark_node; 4593 t = TYPE_NAME (t); 4594 } 4595 4596 if (DECL_ARTIFICIAL (t) || !(complain & tf_keep_type_decl)) 4597 t = TREE_TYPE (t); 4598 4599 maybe_record_typedef_use (t); 4600 4601 return t; 4602 } 4603 4604 /* Resolve `CONTEXT::template NAME'. Returns a TEMPLATE_DECL if the name 4605 can be resolved or an UNBOUND_CLASS_TEMPLATE, unless an error occurs, 4606 in which case error_mark_node is returned. 4607 4608 If PARM_LIST is non-NULL, also make sure that the template parameter 4609 list of TEMPLATE_DECL matches. 4610 4611 If COMPLAIN zero, don't complain about any errors that occur. */ 4612 4613 tree 4614 make_unbound_class_template (tree context, tree name, tree parm_list, 4615 tsubst_flags_t complain) 4616 { 4617 if (TYPE_P (name)) 4618 name = TYPE_IDENTIFIER (name); 4619 else if (DECL_P (name)) 4620 name = DECL_NAME (name); 4621 gcc_assert (identifier_p (name)); 4622 4623 if (!dependent_type_p (context) 4624 || currently_open_class (context)) 4625 { 4626 tree tmpl = NULL_TREE; 4627 4628 if (MAYBE_CLASS_TYPE_P (context)) 4629 tmpl = lookup_field (context, name, 0, false); 4630 4631 if (tmpl && TREE_CODE (tmpl) == TYPE_DECL) 4632 tmpl = maybe_get_template_decl_from_type_decl (tmpl); 4633 4634 if (!tmpl || !DECL_TYPE_TEMPLATE_P (tmpl)) 4635 { 4636 if (complain & tf_error) 4637 error ("no class template named %q#T in %q#T", name, context); 4638 return error_mark_node; 4639 } 4640 4641 if (parm_list 4642 && !comp_template_parms (DECL_TEMPLATE_PARMS (tmpl), parm_list)) 4643 { 4644 if (complain & tf_error) 4645 { 4646 error ("template parameters do not match template %qD", tmpl); 4647 inform (DECL_SOURCE_LOCATION (tmpl), 4648 "%qD declared here", tmpl); 4649 } 4650 return error_mark_node; 4651 } 4652 4653 if (!perform_or_defer_access_check (TYPE_BINFO (context), tmpl, tmpl, 4654 complain)) 4655 return error_mark_node; 4656 4657 return tmpl; 4658 } 4659 4660 return make_unbound_class_template_raw (context, name, parm_list); 4661 } 4662 4663 /* Build an UNBOUND_CLASS_TEMPLATE. */ 4664 4665 tree 4666 make_unbound_class_template_raw (tree context, tree name, tree parm_list) 4667 { 4668 /* Build the UNBOUND_CLASS_TEMPLATE. */ 4669 tree t = cxx_make_type (UNBOUND_CLASS_TEMPLATE); 4670 TYPE_CONTEXT (t) = FROB_CONTEXT (context); 4671 TREE_TYPE (t) = NULL_TREE; 4672 SET_TYPE_STRUCTURAL_EQUALITY (t); 4673 4674 /* Build the corresponding TEMPLATE_DECL. */ 4675 tree d = build_decl (input_location, TEMPLATE_DECL, name, t); 4676 TYPE_NAME (t) = d; 4677 TYPE_STUB_DECL (t) = d; 4678 DECL_CONTEXT (d) = TYPE_CONTEXT (t); 4679 DECL_ARTIFICIAL (d) = 1; 4680 DECL_TEMPLATE_PARMS (d) = parm_list; 4681 4682 return t; 4683 } 4684 4685 4686 4688 /* Push the declarations of builtin types into the global namespace. 4689 RID_INDEX is the index of the builtin type in the array 4690 RID_POINTERS. NAME is the name used when looking up the builtin 4691 type. TYPE is the _TYPE node for the builtin type. 4692 4693 The calls to set_global_binding below should be 4694 eliminated. Built-in types should not be looked up name; their 4695 names are keywords that the parser can recognize. However, there 4696 is code in c-common.cc that uses identifier_global_value to look up 4697 built-in types by name. */ 4698 4699 void 4700 record_builtin_type (enum rid rid_index, 4701 const char* name, 4702 tree type) 4703 { 4704 tree decl = NULL_TREE; 4705 4706 if (name) 4707 { 4708 tree tname = get_identifier (name); 4709 tree tdecl = build_decl (BUILTINS_LOCATION, TYPE_DECL, tname, type); 4710 DECL_ARTIFICIAL (tdecl) = 1; 4711 set_global_binding (tdecl); 4712 decl = tdecl; 4713 } 4714 4715 if ((int) rid_index < (int) RID_MAX) 4716 if (tree rname = ridpointers[(int) rid_index]) 4717 if (!decl || DECL_NAME (decl) != rname) 4718 { 4719 tree rdecl = build_decl (BUILTINS_LOCATION, TYPE_DECL, rname, type); 4720 DECL_ARTIFICIAL (rdecl) = 1; 4721 set_global_binding (rdecl); 4722 if (!decl) 4723 decl = rdecl; 4724 } 4725 4726 if (decl) 4727 { 4728 if (!TYPE_NAME (type)) 4729 TYPE_NAME (type) = decl; 4730 debug_hooks->type_decl (decl, 0); 4731 } 4732 } 4733 4734 /* Push a type into the namespace so that the back ends ignore it. */ 4735 4736 static void 4737 record_unknown_type (tree type, const char* name) 4738 { 4739 tree decl = pushdecl (build_decl (UNKNOWN_LOCATION, 4740 TYPE_DECL, get_identifier (name), type)); 4741 /* Make sure the "unknown type" typedecl gets ignored for debug info. */ 4742 DECL_IGNORED_P (decl) = 1; 4743 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1; 4744 TYPE_SIZE (type) = TYPE_SIZE (void_type_node); 4745 SET_TYPE_ALIGN (type, 1); 4746 TYPE_USER_ALIGN (type) = 0; 4747 SET_TYPE_MODE (type, TYPE_MODE (void_type_node)); 4748 } 4749 4750 /* Create all the predefined identifiers. */ 4751 4752 static void 4753 initialize_predefined_identifiers (void) 4754 { 4755 struct predefined_identifier 4756 { 4757 const char *name; /* Name. */ 4758 tree *node; /* Node to store it in. */ 4759 cp_identifier_kind kind; /* Kind of identifier. */ 4760 }; 4761 4762 /* A table of identifiers to create at startup. */ 4763 static const predefined_identifier predefined_identifiers[] = { 4764 {"C++", &lang_name_cplusplus, cik_normal}, 4765 {"C", &lang_name_c, cik_normal}, 4766 /* Some of these names have a trailing space so that it is 4767 impossible for them to conflict with names written by users. */ 4768 {"__ct ", &ctor_identifier, cik_ctor}, 4769 {"__ct_base ", &base_ctor_identifier, cik_ctor}, 4770 {"__ct_comp ", &complete_ctor_identifier, cik_ctor}, 4771 {"__dt ", &dtor_identifier, cik_dtor}, 4772 {"__dt_base ", &base_dtor_identifier, cik_dtor}, 4773 {"__dt_comp ", &complete_dtor_identifier, cik_dtor}, 4774 {"__dt_del ", &deleting_dtor_identifier, cik_dtor}, 4775 {"__conv_op ", &conv_op_identifier, cik_conv_op}, 4776 {"__in_chrg", &in_charge_identifier, cik_normal}, 4777 {"__as_base ", &as_base_identifier, cik_normal}, 4778 {"this", &this_identifier, cik_normal}, 4779 {"__delta", &delta_identifier, cik_normal}, 4780 {"__pfn", &pfn_identifier, cik_normal}, 4781 {"_vptr", &vptr_identifier, cik_normal}, 4782 {"__vtt_parm", &vtt_parm_identifier, cik_normal}, 4783 {"::", &global_identifier, cik_normal}, 4784 /* The demangler expects anonymous namespaces to be called 4785 something starting with '_GLOBAL__N_'. It no longer needs 4786 to be unique to the TU. */ 4787 {"_GLOBAL__N_1", &anon_identifier, cik_normal}, 4788 {"auto", &auto_identifier, cik_normal}, 4789 {"decltype(auto)", &decltype_auto_identifier, cik_normal}, 4790 {"initializer_list", &init_list_identifier, cik_normal}, 4791 {"__for_range ", &for_range__identifier, cik_normal}, 4792 {"__for_begin ", &for_begin__identifier, cik_normal}, 4793 {"__for_end ", &for_end__identifier, cik_normal}, 4794 {"__for_range", &for_range_identifier, cik_normal}, 4795 {"__for_begin", &for_begin_identifier, cik_normal}, 4796 {"__for_end", &for_end_identifier, cik_normal}, 4797 {"abi_tag", &abi_tag_identifier, cik_normal}, 4798 {"aligned", &aligned_identifier, cik_normal}, 4799 {"begin", &begin_identifier, cik_normal}, 4800 {"end", &end_identifier, cik_normal}, 4801 {"get", &get__identifier, cik_normal}, 4802 {"gnu", &gnu_identifier, cik_normal}, 4803 {"tuple_element", &tuple_element_identifier, cik_normal}, 4804 {"tuple_size", &tuple_size_identifier, cik_normal}, 4805 {"type", &type_identifier, cik_normal}, 4806 {"value", &value_identifier, cik_normal}, 4807 {"_FUN", &fun_identifier, cik_normal}, 4808 {"__closure", &closure_identifier, cik_normal}, 4809 {"heap uninit", &heap_uninit_identifier, cik_normal}, 4810 {"heap ", &heap_identifier, cik_normal}, 4811 {"heap deleted", &heap_deleted_identifier, cik_normal}, 4812 {"heap [] uninit", &heap_vec_uninit_identifier, cik_normal}, 4813 {"heap []", &heap_vec_identifier, cik_normal}, 4814 {"omp", &omp_identifier, cik_normal}, 4815 {NULL, NULL, cik_normal} 4816 }; 4817 4818 for (const predefined_identifier *pid = predefined_identifiers; 4819 pid->name; ++pid) 4820 { 4821 *pid->node = get_identifier (pid->name); 4822 /* Some of these identifiers already have a special kind. */ 4823 if (pid->kind != cik_normal) 4824 set_identifier_kind (*pid->node, pid->kind); 4825 } 4826 } 4827 4828 /* Create the predefined scalar types of C, 4829 and some nodes representing standard constants (0, 1, (void *)0). 4830 Initialize the global binding level. 4831 Make definitions for built-in primitive functions. */ 4832 4833 void 4834 cxx_init_decl_processing (void) 4835 { 4836 tree void_ftype; 4837 tree void_ftype_ptr; 4838 4839 /* Create all the identifiers we need. */ 4840 initialize_predefined_identifiers (); 4841 4842 /* Create the global variables. */ 4843 push_to_top_level (); 4844 4845 current_function_decl = NULL_TREE; 4846 current_binding_level = NULL; 4847 /* Enter the global namespace. */ 4848 gcc_assert (global_namespace == NULL_TREE); 4849 global_namespace = build_lang_decl (NAMESPACE_DECL, global_identifier, 4850 void_type_node); 4851 TREE_PUBLIC (global_namespace) = true; 4852 DECL_MODULE_EXPORT_P (global_namespace) = true; 4853 DECL_CONTEXT (global_namespace) 4854 = build_translation_unit_decl (get_identifier (main_input_filename)); 4855 /* Remember whether we want the empty class passing ABI change warning 4856 in this TU. */ 4857 TRANSLATION_UNIT_WARN_EMPTY_P (DECL_CONTEXT (global_namespace)) 4858 = warn_abi && abi_version_crosses (12); 4859 debug_hooks->register_main_translation_unit 4860 (DECL_CONTEXT (global_namespace)); 4861 begin_scope (sk_namespace, global_namespace); 4862 current_namespace = global_namespace; 4863 4864 if (flag_visibility_ms_compat) 4865 default_visibility = VISIBILITY_HIDDEN; 4866 4867 /* Initially, C. */ 4868 current_lang_name = lang_name_c; 4869 4870 /* Create the `std' namespace. */ 4871 push_namespace (get_identifier ("std")); 4872 std_node = current_namespace; 4873 pop_namespace (); 4874 4875 flag_noexcept_type = (cxx_dialect >= cxx17); 4876 4877 c_common_nodes_and_builtins (); 4878 4879 tree bool_ftype = build_function_type_list (boolean_type_node, NULL_TREE); 4880 tree decl 4881 = add_builtin_function ("__builtin_is_constant_evaluated", 4882 bool_ftype, CP_BUILT_IN_IS_CONSTANT_EVALUATED, 4883 BUILT_IN_FRONTEND, NULL, NULL_TREE); 4884 set_call_expr_flags (decl, ECF_CONST | ECF_NOTHROW | ECF_LEAF); 4885 4886 /* The concrete return type of __builtin_source_location is 4887 const std::source_location::__impl*, but we can't form the type 4888 at this point. So we initially declare it with an auto return 4889 type which we then "deduce" from require_deduced_type upon first use. */ 4890 tree auto_ftype = build_function_type_list (make_auto (), NULL_TREE); 4891 decl = add_builtin_function ("__builtin_source_location", 4892 auto_ftype, CP_BUILT_IN_SOURCE_LOCATION, 4893 BUILT_IN_FRONTEND, NULL, NULL_TREE); 4894 set_call_expr_flags (decl, ECF_CONST | ECF_NOTHROW | ECF_LEAF); 4895 4896 tree bool_vaftype = build_varargs_function_type_list (boolean_type_node, 4897 NULL_TREE); 4898 decl 4899 = add_builtin_function ("__builtin_is_corresponding_member", 4900 bool_vaftype, 4901 CP_BUILT_IN_IS_CORRESPONDING_MEMBER, 4902 BUILT_IN_FRONTEND, NULL, NULL_TREE); 4903 set_call_expr_flags (decl, ECF_CONST | ECF_NOTHROW | ECF_LEAF); 4904 4905 decl 4906 = add_builtin_function ("__builtin_is_pointer_interconvertible_with_class", 4907 bool_vaftype, 4908 CP_BUILT_IN_IS_POINTER_INTERCONVERTIBLE_WITH_CLASS, 4909 BUILT_IN_FRONTEND, NULL, NULL_TREE); 4910 set_call_expr_flags (decl, ECF_CONST | ECF_NOTHROW | ECF_LEAF); 4911 4912 integer_two_node = build_int_cst (NULL_TREE, 2); 4913 4914 /* Guess at the initial static decls size. */ 4915 vec_alloc (static_decls, 500); 4916 4917 /* ... and keyed classes. */ 4918 vec_alloc (keyed_classes, 100); 4919 4920 record_builtin_type (RID_BOOL, "bool", boolean_type_node); 4921 truthvalue_type_node = boolean_type_node; 4922 truthvalue_false_node = boolean_false_node; 4923 truthvalue_true_node = boolean_true_node; 4924 4925 empty_except_spec = build_tree_list (NULL_TREE, NULL_TREE); 4926 noexcept_true_spec = build_tree_list (boolean_true_node, NULL_TREE); 4927 noexcept_false_spec = build_tree_list (boolean_false_node, NULL_TREE); 4928 noexcept_deferred_spec = build_tree_list (make_node (DEFERRED_NOEXCEPT), 4929 NULL_TREE); 4930 4931 #if 0 4932 record_builtin_type (RID_MAX, NULL, string_type_node); 4933 #endif 4934 4935 delta_type_node = ptrdiff_type_node; 4936 vtable_index_type = ptrdiff_type_node; 4937 4938 vtt_parm_type = build_pointer_type (const_ptr_type_node); 4939 void_ftype = build_function_type_list (void_type_node, NULL_TREE); 4940 void_ftype_ptr = build_function_type_list (void_type_node, 4941 ptr_type_node, NULL_TREE); 4942 void_ftype_ptr 4943 = build_exception_variant (void_ftype_ptr, empty_except_spec); 4944 4945 /* Create the conversion operator marker. This operator's DECL_NAME 4946 is in the identifier table, so we can use identifier equality to 4947 find it. */ 4948 conv_op_marker = build_lang_decl (FUNCTION_DECL, conv_op_identifier, 4949 void_ftype); 4950 4951 /* C++ extensions */ 4952 4953 unknown_type_node = make_node (LANG_TYPE); 4954 record_unknown_type (unknown_type_node, "unknown type"); 4955 4956 /* Indirecting an UNKNOWN_TYPE node yields an UNKNOWN_TYPE node. */ 4957 TREE_TYPE (unknown_type_node) = unknown_type_node; 4958 4959 /* Looking up TYPE_POINTER_TO and TYPE_REFERENCE_TO yield the same 4960 result. */ 4961 TYPE_POINTER_TO (unknown_type_node) = unknown_type_node; 4962 TYPE_REFERENCE_TO (unknown_type_node) = unknown_type_node; 4963 4964 init_list_type_node = make_node (LANG_TYPE); 4965 record_unknown_type (init_list_type_node, "init list"); 4966 4967 /* Used when parsing to distinguish parameter-lists () and (void). */ 4968 explicit_void_list_node = build_tree_list (NULL_TREE, void_type_node); 4969 4970 { 4971 /* Make sure we get a unique function type, so we can give 4972 its pointer type a name. (This wins for gdb.) */ 4973 tree vfunc_type = make_node (FUNCTION_TYPE); 4974 TREE_TYPE (vfunc_type) = integer_type_node; 4975 TYPE_ARG_TYPES (vfunc_type) = NULL_TREE; 4976 layout_type (vfunc_type); 4977 4978 vtable_entry_type = build_pointer_type (vfunc_type); 4979 } 4980 record_builtin_type (RID_MAX, "__vtbl_ptr_type", vtable_entry_type); 4981 4982 vtbl_type_node 4983 = build_cplus_array_type (vtable_entry_type, NULL_TREE); 4984 layout_type (vtbl_type_node); 4985 vtbl_type_node = cp_build_qualified_type (vtbl_type_node, TYPE_QUAL_CONST); 4986 record_builtin_type (RID_MAX, NULL, vtbl_type_node); 4987 vtbl_ptr_type_node = build_pointer_type (vtable_entry_type); 4988 layout_type (vtbl_ptr_type_node); 4989 record_builtin_type (RID_MAX, NULL, vtbl_ptr_type_node); 4990 4991 push_namespace (get_identifier ("__cxxabiv1")); 4992 abi_node = current_namespace; 4993 pop_namespace (); 4994 4995 any_targ_node = make_node (LANG_TYPE); 4996 record_unknown_type (any_targ_node, "any type"); 4997 4998 /* Now, C++. */ 4999 current_lang_name = lang_name_cplusplus; 5000 5001 if (aligned_new_threshold > 1 5002 && !pow2p_hwi (aligned_new_threshold)) 5003 { 5004 error ("%<-faligned-new=%d%> is not a power of two", 5005 aligned_new_threshold); 5006 aligned_new_threshold = 1; 5007 } 5008 if (aligned_new_threshold == -1) 5009 aligned_new_threshold = (cxx_dialect >= cxx17) ? 1 : 0; 5010 if (aligned_new_threshold == 1) 5011 aligned_new_threshold = malloc_alignment () / BITS_PER_UNIT; 5012 5013 { 5014 tree newattrs, extvisattr; 5015 tree newtype, deltype; 5016 tree ptr_ftype_sizetype; 5017 tree new_eh_spec; 5018 5019 ptr_ftype_sizetype 5020 = build_function_type_list (ptr_type_node, size_type_node, NULL_TREE); 5021 if (cxx_dialect == cxx98) 5022 { 5023 tree bad_alloc_id; 5024 tree bad_alloc_type_node; 5025 tree bad_alloc_decl; 5026 5027 push_nested_namespace (std_node); 5028 bad_alloc_id = get_identifier ("bad_alloc"); 5029 bad_alloc_type_node = make_class_type (RECORD_TYPE); 5030 TYPE_CONTEXT (bad_alloc_type_node) = current_namespace; 5031 bad_alloc_decl 5032 = create_implicit_typedef (bad_alloc_id, bad_alloc_type_node); 5033 DECL_CONTEXT (bad_alloc_decl) = current_namespace; 5034 pop_nested_namespace (std_node); 5035 5036 new_eh_spec 5037 = add_exception_specifier (NULL_TREE, bad_alloc_type_node, -1); 5038 } 5039 else 5040 new_eh_spec = noexcept_false_spec; 5041 5042 /* Ensure attribs.cc is initialized. */ 5043 init_attributes (); 5044 5045 extvisattr = build_tree_list (get_identifier ("externally_visible"), 5046 NULL_TREE); 5047 newattrs = tree_cons (get_identifier ("alloc_size"), 5048 build_tree_list (NULL_TREE, integer_one_node), 5049 extvisattr); 5050 newtype = cp_build_type_attribute_variant (ptr_ftype_sizetype, newattrs); 5051 newtype = build_exception_variant (newtype, new_eh_spec); 5052 deltype = cp_build_type_attribute_variant (void_ftype_ptr, extvisattr); 5053 deltype = build_exception_variant (deltype, empty_except_spec); 5054 tree opnew = push_cp_library_fn (NEW_EXPR, newtype, 0); 5055 DECL_IS_MALLOC (opnew) = 1; 5056 DECL_SET_IS_OPERATOR_NEW (opnew, true); 5057 DECL_IS_REPLACEABLE_OPERATOR (opnew) = 1; 5058 opnew = push_cp_library_fn (VEC_NEW_EXPR, newtype, 0); 5059 DECL_IS_MALLOC (opnew) = 1; 5060 DECL_SET_IS_OPERATOR_NEW (opnew, true); 5061 DECL_IS_REPLACEABLE_OPERATOR (opnew) = 1; 5062 tree opdel = push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW); 5063 DECL_SET_IS_OPERATOR_DELETE (opdel, true); 5064 DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1; 5065 opdel = push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW); 5066 DECL_SET_IS_OPERATOR_DELETE (opdel, true); 5067 DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1; 5068 if (flag_sized_deallocation) 5069 { 5070 /* Also push the sized deallocation variants: 5071 void operator delete(void*, std::size_t) throw(); 5072 void operator delete[](void*, std::size_t) throw(); */ 5073 tree void_ftype_ptr_size 5074 = build_function_type_list (void_type_node, ptr_type_node, 5075 size_type_node, NULL_TREE); 5076 deltype = cp_build_type_attribute_variant (void_ftype_ptr_size, 5077 extvisattr); 5078 deltype = build_exception_variant (deltype, empty_except_spec); 5079 opdel = push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW); 5080 DECL_SET_IS_OPERATOR_DELETE (opdel, true); 5081 DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1; 5082 opdel = push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW); 5083 DECL_SET_IS_OPERATOR_DELETE (opdel, true); 5084 DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1; 5085 } 5086 5087 if (aligned_new_threshold) 5088 { 5089 push_nested_namespace (std_node); 5090 tree align_id = get_identifier ("align_val_t"); 5091 align_type_node = start_enum (align_id, NULL_TREE, size_type_node, 5092 NULL_TREE, /*scoped*/true, NULL); 5093 pop_nested_namespace (std_node); 5094 5095 /* operator new (size_t, align_val_t); */ 5096 newtype = build_function_type_list (ptr_type_node, size_type_node, 5097 align_type_node, NULL_TREE); 5098 newtype = cp_build_type_attribute_variant (newtype, newattrs); 5099 newtype = build_exception_variant (newtype, new_eh_spec); 5100 opnew = push_cp_library_fn (NEW_EXPR, newtype, 0); 5101 DECL_IS_MALLOC (opnew) = 1; 5102 DECL_SET_IS_OPERATOR_NEW (opnew, true); 5103 DECL_IS_REPLACEABLE_OPERATOR (opnew) = 1; 5104 opnew = push_cp_library_fn (VEC_NEW_EXPR, newtype, 0); 5105 DECL_IS_MALLOC (opnew) = 1; 5106 DECL_SET_IS_OPERATOR_NEW (opnew, true); 5107 DECL_IS_REPLACEABLE_OPERATOR (opnew) = 1; 5108 5109 /* operator delete (void *, align_val_t); */ 5110 deltype = build_function_type_list (void_type_node, ptr_type_node, 5111 align_type_node, NULL_TREE); 5112 deltype = cp_build_type_attribute_variant (deltype, extvisattr); 5113 deltype = build_exception_variant (deltype, empty_except_spec); 5114 opdel = push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW); 5115 DECL_SET_IS_OPERATOR_DELETE (opdel, true); 5116 DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1; 5117 opdel = push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW); 5118 DECL_SET_IS_OPERATOR_DELETE (opdel, true); 5119 DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1; 5120 5121 if (flag_sized_deallocation) 5122 { 5123 /* operator delete (void *, size_t, align_val_t); */ 5124 deltype = build_function_type_list (void_type_node, ptr_type_node, 5125 size_type_node, align_type_node, 5126 NULL_TREE); 5127 deltype = cp_build_type_attribute_variant (deltype, extvisattr); 5128 deltype = build_exception_variant (deltype, empty_except_spec); 5129 opdel = push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW); 5130 DECL_SET_IS_OPERATOR_DELETE (opdel, true); 5131 DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1; 5132 opdel = push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW); 5133 DECL_SET_IS_OPERATOR_DELETE (opdel, true); 5134 DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1; 5135 } 5136 } 5137 5138 /* C++-specific nullptr initialization. */ 5139 if (abi_version_at_least (9)) 5140 SET_TYPE_ALIGN (nullptr_type_node, GET_MODE_ALIGNMENT (ptr_mode)); 5141 record_builtin_type (RID_MAX, "decltype(nullptr)", nullptr_type_node); 5142 } 5143 5144 if (! supports_one_only ()) 5145 flag_weak = 0; 5146 5147 abort_fndecl 5148 = build_library_fn_ptr ("__cxa_pure_virtual", void_ftype, 5149 ECF_NORETURN | ECF_NOTHROW | ECF_COLD); 5150 if (flag_weak) 5151 /* If no definition is available, resolve references to NULL. */ 5152 declare_weak (abort_fndecl); 5153 5154 /* Perform other language dependent initializations. */ 5155 init_class_processing (); 5156 init_rtti_processing (); 5157 init_template_processing (); 5158 5159 if (flag_exceptions) 5160 init_exception_processing (); 5161 5162 if (modules_p ()) 5163 init_modules (parse_in); 5164 5165 make_fname_decl = cp_make_fname_decl; 5166 start_fname_decls (); 5167 5168 /* Show we use EH for cleanups. */ 5169 if (flag_exceptions) 5170 using_eh_for_cleanups (); 5171 5172 /* Check that the hardware interference sizes are at least 5173 alignof(max_align_t), as required by the standard. */ 5174 const int max_align = max_align_t_align () / BITS_PER_UNIT; 5175 if (OPTION_SET_P (param_destruct_interfere_size)) 5176 { 5177 if (param_destruct_interfere_size < max_align) 5178 error ("%<--param destructive-interference-size=%d%> is less than " 5179 "%d", param_destruct_interfere_size, max_align); 5180 else if (param_destruct_interfere_size < param_l1_cache_line_size) 5181 warning (OPT_Winterference_size, 5182 "%<--param destructive-interference-size=%d%> " 5183 "is less than %<--param l1-cache-line-size=%d%>", 5184 param_destruct_interfere_size, param_l1_cache_line_size); 5185 } 5186 else if (param_destruct_interfere_size) 5187 /* Assume the internal value is OK. */; 5188 else if (param_l1_cache_line_size >= max_align) 5189 param_destruct_interfere_size = param_l1_cache_line_size; 5190 /* else leave it unset. */ 5191 5192 if (OPTION_SET_P (param_construct_interfere_size)) 5193 { 5194 if (param_construct_interfere_size < max_align) 5195 error ("%<--param constructive-interference-size=%d%> is less than " 5196 "%d", param_construct_interfere_size, max_align); 5197 else if (param_construct_interfere_size > param_l1_cache_line_size 5198 && param_l1_cache_line_size >= max_align) 5199 warning (OPT_Winterference_size, 5200 "%<--param constructive-interference-size=%d%> " 5201 "is greater than %<--param l1-cache-line-size=%d%>", 5202 param_construct_interfere_size, param_l1_cache_line_size); 5203 } 5204 else if (param_construct_interfere_size) 5205 /* Assume the internal value is OK. */; 5206 else if (param_l1_cache_line_size >= max_align) 5207 param_construct_interfere_size = param_l1_cache_line_size; 5208 } 5209 5210 /* Enter an abi node in global-module context. returns a cookie to 5211 give to pop_abi_namespace. */ 5212 5213 unsigned 5214 push_abi_namespace (tree node) 5215 { 5216 push_nested_namespace (node); 5217 push_visibility ("default", 2); 5218 unsigned flags = module_kind; 5219 module_kind = 0; 5220 return flags; 5221 } 5222 5223 /* Pop an abi namespace, FLAGS is the cookie push_abi_namespace gave 5224 you. */ 5225 5226 void 5227 pop_abi_namespace (unsigned flags, tree node) 5228 { 5229 module_kind = flags; 5230 pop_visibility (2); 5231 pop_nested_namespace (node); 5232 } 5233 5234 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give 5235 the decl, LOC is the location to give the decl, NAME is the 5236 initialization string and TYPE_DEP indicates whether NAME depended 5237 on the type of the function. We make use of that to detect 5238 __PRETTY_FUNCTION__ inside a template fn. This is being done lazily 5239 at the point of first use, so we mustn't push the decl now. */ 5240 5241 static tree 5242 cp_make_fname_decl (location_t loc, tree id, int type_dep) 5243 { 5244 tree domain = NULL_TREE; 5245 tree init = NULL_TREE; 5246 5247 if (!(type_dep && current_function_decl && in_template_context)) 5248 { 5249 const char *name = NULL; 5250 bool release_name = false; 5251 5252 if (current_function_decl == NULL_TREE) 5253 name = "top level"; 5254 else if (type_dep == 0) 5255 { 5256 /* __FUNCTION__ */ 5257 name = fname_as_string (type_dep); 5258 release_name = true; 5259 } 5260 else 5261 { 5262 /* __PRETTY_FUNCTION__ */ 5263 gcc_checking_assert (type_dep == 1); 5264 name = cxx_printable_name (current_function_decl, 2); 5265 } 5266 5267 size_t length = strlen (name); 5268 domain = build_index_type (size_int (length)); 5269 init = build_string (length + 1, name); 5270 if (release_name) 5271 free (const_cast<char *> (name)); 5272 } 5273 5274 tree type = cp_build_qualified_type (char_type_node, TYPE_QUAL_CONST); 5275 type = build_cplus_array_type (type, domain); 5276 5277 if (init) 5278 TREE_TYPE (init) = type; 5279 else 5280 init = error_mark_node; 5281 5282 tree decl = build_decl (loc, VAR_DECL, id, type); 5283 5284 TREE_READONLY (decl) = 1; 5285 DECL_ARTIFICIAL (decl) = 1; 5286 DECL_DECLARED_CONSTEXPR_P (decl) = 1; 5287 TREE_STATIC (decl) = 1; 5288 5289 TREE_USED (decl) = 1; 5290 5291 SET_DECL_VALUE_EXPR (decl, init); 5292 DECL_HAS_VALUE_EXPR_P (decl) = 1; 5293 /* For decl_constant_var_p. */ 5294 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1; 5295 5296 if (current_function_decl) 5297 { 5298 DECL_CONTEXT (decl) = current_function_decl; 5299 decl = pushdecl_outermost_localscope (decl); 5300 if (decl != error_mark_node) 5301 add_decl_expr (decl); 5302 } 5303 else 5304 { 5305 DECL_THIS_STATIC (decl) = true; 5306 decl = pushdecl_top_level_and_finish (decl, NULL_TREE); 5307 } 5308 5309 return decl; 5310 } 5311 5312 /* Install DECL as a builtin function at current global scope. Return 5313 the new decl (if we found an existing version). Also installs it 5314 into ::std, if it's not '_*'. */ 5315 5316 tree 5317 cxx_builtin_function (tree decl) 5318 { 5319 retrofit_lang_decl (decl); 5320 5321 DECL_ARTIFICIAL (decl) = 1; 5322 SET_DECL_LANGUAGE (decl, lang_c); 5323 /* Runtime library routines are, by definition, available in an 5324 external shared object. */ 5325 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT; 5326 DECL_VISIBILITY_SPECIFIED (decl) = 1; 5327 5328 tree id = DECL_NAME (decl); 5329 const char *name = IDENTIFIER_POINTER (id); 5330 bool hiding = false; 5331 if (name[0] != '_' || name[1] != '_') 5332 /* In the user's namespace, it must be declared before use. */ 5333 hiding = true; 5334 else if (IDENTIFIER_LENGTH (id) > strlen ("___chk") 5335 && !startswith (name + 2, "builtin_") 5336 && 0 == memcmp (name + IDENTIFIER_LENGTH (id) - strlen ("_chk"), 5337 "_chk", strlen ("_chk") + 1)) 5338 /* Treat __*_chk fortification functions as anticipated as well, 5339 unless they are __builtin_*_chk. */ 5340 hiding = true; 5341 5342 /* All builtins that don't begin with an '_' should additionally 5343 go in the 'std' namespace. */ 5344 if (name[0] != '_') 5345 { 5346 tree std_decl = copy_decl (decl); 5347 5348 push_nested_namespace (std_node); 5349 DECL_CONTEXT (std_decl) = FROB_CONTEXT (std_node); 5350 pushdecl (std_decl, hiding); 5351 pop_nested_namespace (std_node); 5352 } 5353 5354 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace); 5355 decl = pushdecl (decl, hiding); 5356 5357 return decl; 5358 } 5359 5360 /* Like cxx_builtin_function, but guarantee the function is added to the global 5361 scope. This is to allow function specific options to add new machine 5362 dependent builtins when the target ISA changes via attribute((target(...))) 5363 which saves space on program startup if the program does not use non-generic 5364 ISAs. */ 5365 5366 tree 5367 cxx_builtin_function_ext_scope (tree decl) 5368 { 5369 push_nested_namespace (global_namespace); 5370 decl = cxx_builtin_function (decl); 5371 pop_nested_namespace (global_namespace); 5372 5373 return decl; 5374 } 5375 5376 /* Implement LANG_HOOKS_SIMULATE_BUILTIN_FUNCTION_DECL. */ 5377 5378 tree 5379 cxx_simulate_builtin_function_decl (tree decl) 5380 { 5381 retrofit_lang_decl (decl); 5382 5383 DECL_ARTIFICIAL (decl) = 1; 5384 SET_DECL_LANGUAGE (decl, lang_cplusplus); 5385 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace); 5386 return pushdecl (decl); 5387 } 5388 5389 /* Generate a FUNCTION_DECL with the typical flags for a runtime library 5390 function. Not called directly. */ 5391 5392 static tree 5393 build_library_fn (tree name, enum tree_code operator_code, tree type, 5394 int ecf_flags) 5395 { 5396 tree fn = build_lang_decl (FUNCTION_DECL, name, type); 5397 DECL_EXTERNAL (fn) = 1; 5398 TREE_PUBLIC (fn) = 1; 5399 DECL_ARTIFICIAL (fn) = 1; 5400 DECL_OVERLOADED_OPERATOR_CODE_RAW (fn) 5401 = OVL_OP_INFO (false, operator_code)->ovl_op_code; 5402 SET_DECL_LANGUAGE (fn, lang_c); 5403 /* Runtime library routines are, by definition, available in an 5404 external shared object. */ 5405 DECL_VISIBILITY (fn) = VISIBILITY_DEFAULT; 5406 DECL_VISIBILITY_SPECIFIED (fn) = 1; 5407 set_call_expr_flags (fn, ecf_flags); 5408 return fn; 5409 } 5410 5411 /* Returns the _DECL for a library function with C++ linkage. */ 5412 5413 static tree 5414 build_cp_library_fn (tree name, enum tree_code operator_code, tree type, 5415 int ecf_flags) 5416 { 5417 tree fn = build_library_fn (name, operator_code, type, ecf_flags); 5418 DECL_CONTEXT (fn) = FROB_CONTEXT (current_namespace); 5419 SET_DECL_LANGUAGE (fn, lang_cplusplus); 5420 return fn; 5421 } 5422 5423 /* Like build_library_fn, but takes a C string instead of an 5424 IDENTIFIER_NODE. */ 5425 5426 tree 5427 build_library_fn_ptr (const char* name, tree type, int ecf_flags) 5428 { 5429 return build_library_fn (get_identifier (name), ERROR_MARK, type, ecf_flags); 5430 } 5431 5432 /* Like build_cp_library_fn, but takes a C string instead of an 5433 IDENTIFIER_NODE. */ 5434 5435 tree 5436 build_cp_library_fn_ptr (const char* name, tree type, int ecf_flags) 5437 { 5438 return build_cp_library_fn (get_identifier (name), ERROR_MARK, type, 5439 ecf_flags); 5440 } 5441 5442 /* Like build_library_fn, but also pushes the function so that we will 5443 be able to find it via get_global_binding. Also, the function 5444 may throw exceptions listed in RAISES. */ 5445 5446 tree 5447 push_library_fn (tree name, tree type, tree raises, int ecf_flags) 5448 { 5449 if (raises) 5450 type = build_exception_variant (type, raises); 5451 5452 tree fn = build_library_fn (name, ERROR_MARK, type, ecf_flags); 5453 return pushdecl_top_level (fn); 5454 } 5455 5456 /* Like build_cp_library_fn, but also pushes the function so that it 5457 will be found by normal lookup. */ 5458 5459 static tree 5460 push_cp_library_fn (enum tree_code operator_code, tree type, 5461 int ecf_flags) 5462 { 5463 tree fn = build_cp_library_fn (ovl_op_identifier (false, operator_code), 5464 operator_code, type, ecf_flags); 5465 pushdecl (fn); 5466 if (flag_tm) 5467 apply_tm_attr (fn, get_identifier ("transaction_safe")); 5468 return fn; 5469 } 5470 5471 /* Like push_library_fn, but also note that this function throws 5472 and does not return. Used for __throw_foo and the like. */ 5473 5474 tree 5475 push_throw_library_fn (tree name, tree type) 5476 { 5477 tree fn = push_library_fn (name, type, NULL_TREE, 5478 ECF_NORETURN | ECF_XTHROW | ECF_COLD); 5479 return fn; 5480 } 5481 5482 /* When we call finish_struct for an anonymous union, we create 5484 default copy constructors and such. But, an anonymous union 5485 shouldn't have such things; this function undoes the damage to the 5486 anonymous union type T. 5487 5488 (The reason that we create the synthesized methods is that we don't 5489 distinguish `union { int i; }' from `typedef union { int i; } U'. 5490 The first is an anonymous union; the second is just an ordinary 5491 union type.) */ 5492 5493 void 5494 fixup_anonymous_aggr (tree t) 5495 { 5496 /* Wipe out memory of synthesized methods. */ 5497 TYPE_HAS_USER_CONSTRUCTOR (t) = 0; 5498 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 0; 5499 TYPE_HAS_COPY_CTOR (t) = 0; 5500 TYPE_HAS_CONST_COPY_CTOR (t) = 0; 5501 TYPE_HAS_COPY_ASSIGN (t) = 0; 5502 TYPE_HAS_CONST_COPY_ASSIGN (t) = 0; 5503 5504 /* Splice the implicitly generated functions out of TYPE_FIELDS and diagnose 5505 invalid members. */ 5506 for (tree probe, *prev_p = &TYPE_FIELDS (t); (probe = *prev_p);) 5507 { 5508 if (TREE_CODE (probe) == FUNCTION_DECL && DECL_ARTIFICIAL (probe)) 5509 *prev_p = DECL_CHAIN (probe); 5510 else 5511 prev_p = &DECL_CHAIN (probe); 5512 5513 if (DECL_ARTIFICIAL (probe) 5514 && (!DECL_IMPLICIT_TYPEDEF_P (probe) 5515 || TYPE_ANON_P (TREE_TYPE (probe)))) 5516 continue; 5517 5518 if (TREE_CODE (probe) != FIELD_DECL 5519 || (TREE_PRIVATE (probe) || TREE_PROTECTED (probe))) 5520 { 5521 /* We already complained about static data members in 5522 finish_static_data_member_decl. */ 5523 if (!VAR_P (probe)) 5524 { 5525 auto_diagnostic_group d; 5526 if (permerror (DECL_SOURCE_LOCATION (probe), 5527 TREE_CODE (t) == UNION_TYPE 5528 ? "%q#D invalid; an anonymous union may " 5529 "only have public non-static data members" 5530 : "%q#D invalid; an anonymous struct may " 5531 "only have public non-static data members", probe)) 5532 { 5533 static bool hint; 5534 if (flag_permissive && !hint) 5535 { 5536 hint = true; 5537 inform (DECL_SOURCE_LOCATION (probe), 5538 "this flexibility is deprecated and will be " 5539 "removed"); 5540 } 5541 } 5542 } 5543 } 5544 } 5545 5546 /* Splice all functions out of CLASSTYPE_MEMBER_VEC. */ 5547 vec<tree,va_gc>* vec = CLASSTYPE_MEMBER_VEC (t); 5548 unsigned store = 0; 5549 for (tree elt : vec) 5550 if (!is_overloaded_fn (elt)) 5551 (*vec)[store++] = elt; 5552 vec_safe_truncate (vec, store); 5553 5554 /* Wipe RTTI info. */ 5555 CLASSTYPE_TYPEINFO_VAR (t) = NULL_TREE; 5556 5557 /* Anonymous aggregates cannot have fields with ctors, dtors or complex 5558 assignment operators (because they cannot have these methods themselves). 5559 For anonymous unions this is already checked because they are not allowed 5560 in any union, otherwise we have to check it. */ 5561 if (TREE_CODE (t) != UNION_TYPE) 5562 { 5563 tree field, type; 5564 5565 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t))) 5566 { 5567 error_at (location_of (t), "anonymous struct with base classes"); 5568 /* Avoid ICE after error on anon-struct9.C. */ 5569 TYPE_NEEDS_CONSTRUCTING (t) = false; 5570 } 5571 5572 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field)) 5573 if (TREE_CODE (field) == FIELD_DECL) 5574 { 5575 type = TREE_TYPE (field); 5576 if (CLASS_TYPE_P (type)) 5577 { 5578 if (TYPE_NEEDS_CONSTRUCTING (type)) 5579 error ("member %q+#D with constructor not allowed " 5580 "in anonymous aggregate", field); 5581 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)) 5582 error ("member %q+#D with destructor not allowed " 5583 "in anonymous aggregate", field); 5584 if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type)) 5585 error ("member %q+#D with copy assignment operator " 5586 "not allowed in anonymous aggregate", field); 5587 } 5588 } 5589 } 5590 } 5591 5592 /* Warn for an attribute located at LOCATION that appertains to the 5593 class type CLASS_TYPE that has not been properly placed after its 5594 class-key, in it class-specifier. */ 5595 5596 void 5597 warn_misplaced_attr_for_class_type (location_t location, 5598 tree class_type) 5599 { 5600 gcc_assert (OVERLOAD_TYPE_P (class_type)); 5601 5602 auto_diagnostic_group d; 5603 if (warning_at (location, OPT_Wattributes, 5604 "attribute ignored in declaration " 5605 "of %q#T", class_type)) 5606 inform (location, 5607 "attribute for %q#T must follow the %qs keyword", 5608 class_type, class_key_or_enum_as_string (class_type)); 5609 } 5610 5611 /* Returns the cv-qualifiers that apply to the type specified 5612 by the DECLSPECS. */ 5613 5614 static int 5615 get_type_quals (const cp_decl_specifier_seq *declspecs) 5616 { 5617 int type_quals = TYPE_UNQUALIFIED; 5618 5619 if (decl_spec_seq_has_spec_p (declspecs, ds_const)) 5620 type_quals |= TYPE_QUAL_CONST; 5621 if (decl_spec_seq_has_spec_p (declspecs, ds_volatile)) 5622 type_quals |= TYPE_QUAL_VOLATILE; 5623 if (decl_spec_seq_has_spec_p (declspecs, ds_restrict)) 5624 type_quals |= TYPE_QUAL_RESTRICT; 5625 5626 return type_quals; 5627 } 5628 5629 /* Make sure that a declaration with no declarator is well-formed, i.e. 5630 just declares a tagged type or anonymous union. 5631 5632 Returns the type declared; or NULL_TREE if none. */ 5633 5634 tree 5635 check_tag_decl (cp_decl_specifier_seq *declspecs, 5636 bool explicit_type_instantiation_p) 5637 { 5638 int saw_friend = decl_spec_seq_has_spec_p (declspecs, ds_friend); 5639 int saw_typedef = decl_spec_seq_has_spec_p (declspecs, ds_typedef); 5640 /* If a class, struct, or enum type is declared by the DECLSPECS 5641 (i.e, if a class-specifier, enum-specifier, or non-typename 5642 elaborated-type-specifier appears in the DECLSPECS), 5643 DECLARED_TYPE is set to the corresponding type. */ 5644 tree declared_type = NULL_TREE; 5645 bool error_p = false; 5646 5647 if (declspecs->multiple_types_p) 5648 error_at (smallest_type_location (declspecs), 5649 "multiple types in one declaration"); 5650 else if (declspecs->redefined_builtin_type) 5651 { 5652 location_t loc = declspecs->locations[ds_redefined_builtin_type_spec]; 5653 if (!in_system_header_at (loc)) 5654 permerror (loc, "redeclaration of C++ built-in type %qT", 5655 declspecs->redefined_builtin_type); 5656 return NULL_TREE; 5657 } 5658 5659 if (declspecs->type 5660 && TYPE_P (declspecs->type) 5661 && ((TREE_CODE (declspecs->type) != TYPENAME_TYPE 5662 && MAYBE_CLASS_TYPE_P (declspecs->type)) 5663 || TREE_CODE (declspecs->type) == ENUMERAL_TYPE)) 5664 declared_type = declspecs->type; 5665 else if (declspecs->type == error_mark_node) 5666 error_p = true; 5667 5668 if (type_uses_auto (declared_type)) 5669 { 5670 error_at (declspecs->locations[ds_type_spec], 5671 "%<auto%> can only be specified for variables " 5672 "or function declarations"); 5673 return error_mark_node; 5674 } 5675 5676 if (declared_type && !OVERLOAD_TYPE_P (declared_type)) 5677 declared_type = NULL_TREE; 5678 5679 if (!declared_type && !saw_friend && !error_p) 5680 permerror (input_location, "declaration does not declare anything"); 5681 /* Check for an anonymous union. */ 5682 else if (declared_type && RECORD_OR_UNION_CODE_P (TREE_CODE (declared_type)) 5683 && TYPE_UNNAMED_P (declared_type)) 5684 { 5685 /* 7/3 In a simple-declaration, the optional init-declarator-list 5686 can be omitted only when declaring a class (clause 9) or 5687 enumeration (7.2), that is, when the decl-specifier-seq contains 5688 either a class-specifier, an elaborated-type-specifier with 5689 a class-key (9.1), or an enum-specifier. In these cases and 5690 whenever a class-specifier or enum-specifier is present in the 5691 decl-specifier-seq, the identifiers in these specifiers are among 5692 the names being declared by the declaration (as class-name, 5693 enum-names, or enumerators, depending on the syntax). In such 5694 cases, and except for the declaration of an unnamed bit-field (9.6), 5695 the decl-specifier-seq shall introduce one or more names into the 5696 program, or shall redeclare a name introduced by a previous 5697 declaration. [Example: 5698 enum { }; // ill-formed 5699 typedef class { }; // ill-formed 5700 --end example] */ 5701 if (saw_typedef) 5702 { 5703 error_at (declspecs->locations[ds_typedef], 5704 "missing type-name in typedef-declaration"); 5705 return NULL_TREE; 5706 } 5707 /* Anonymous unions are objects, so they can have specifiers. */; 5708 SET_ANON_AGGR_TYPE_P (declared_type); 5709 5710 if (TREE_CODE (declared_type) != UNION_TYPE) 5711 pedwarn (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (declared_type)), 5712 OPT_Wpedantic, "ISO C++ prohibits anonymous structs"); 5713 } 5714 5715 else 5716 { 5717 if (decl_spec_seq_has_spec_p (declspecs, ds_inline)) 5718 error_at (declspecs->locations[ds_inline], 5719 "%<inline%> can only be specified for functions"); 5720 else if (decl_spec_seq_has_spec_p (declspecs, ds_virtual)) 5721 error_at (declspecs->locations[ds_virtual], 5722 "%<virtual%> can only be specified for functions"); 5723 else if (saw_friend 5724 && (!current_class_type 5725 || current_scope () != current_class_type)) 5726 error_at (declspecs->locations[ds_friend], 5727 "%<friend%> can only be specified inside a class"); 5728 else if (decl_spec_seq_has_spec_p (declspecs, ds_explicit)) 5729 error_at (declspecs->locations[ds_explicit], 5730 "%<explicit%> can only be specified for constructors"); 5731 else if (declspecs->storage_class) 5732 error_at (declspecs->locations[ds_storage_class], 5733 "a storage class can only be specified for objects " 5734 "and functions"); 5735 else if (decl_spec_seq_has_spec_p (declspecs, ds_const)) 5736 error_at (declspecs->locations[ds_const], 5737 "%<const%> can only be specified for objects and " 5738 "functions"); 5739 else if (decl_spec_seq_has_spec_p (declspecs, ds_volatile)) 5740 error_at (declspecs->locations[ds_volatile], 5741 "%<volatile%> can only be specified for objects and " 5742 "functions"); 5743 else if (decl_spec_seq_has_spec_p (declspecs, ds_restrict)) 5744 error_at (declspecs->locations[ds_restrict], 5745 "%<__restrict%> can only be specified for objects and " 5746 "functions"); 5747 else if (decl_spec_seq_has_spec_p (declspecs, ds_thread)) 5748 error_at (declspecs->locations[ds_thread], 5749 "%<__thread%> can only be specified for objects " 5750 "and functions"); 5751 else if (saw_typedef) 5752 warning_at (declspecs->locations[ds_typedef], 0, 5753 "%<typedef%> was ignored in this declaration"); 5754 else if (decl_spec_seq_has_spec_p (declspecs, ds_constexpr)) 5755 error_at (declspecs->locations[ds_constexpr], 5756 "%qs cannot be used for type declarations", "constexpr"); 5757 else if (decl_spec_seq_has_spec_p (declspecs, ds_constinit)) 5758 error_at (declspecs->locations[ds_constinit], 5759 "%qs cannot be used for type declarations", "constinit"); 5760 else if (decl_spec_seq_has_spec_p (declspecs, ds_consteval)) 5761 error_at (declspecs->locations[ds_consteval], 5762 "%qs cannot be used for type declarations", "consteval"); 5763 } 5764 5765 if (declspecs->attributes && warn_attributes && declared_type) 5766 { 5767 location_t loc; 5768 if (!CLASS_TYPE_P (declared_type) 5769 || !CLASSTYPE_TEMPLATE_INSTANTIATION (declared_type)) 5770 /* For a non-template class, use the name location. */ 5771 loc = location_of (declared_type); 5772 else 5773 /* For a template class (an explicit instantiation), use the 5774 current location. */ 5775 loc = input_location; 5776 5777 if (explicit_type_instantiation_p) 5778 /* [dcl.attr.grammar]/4: 5779 5780 No attribute-specifier-seq shall appertain to an explicit 5781 instantiation. */ 5782 { 5783 if (warning_at (loc, OPT_Wattributes, 5784 "attribute ignored in explicit instantiation %q#T", 5785 declared_type)) 5786 inform (loc, 5787 "no attribute can be applied to " 5788 "an explicit instantiation"); 5789 } 5790 else 5791 warn_misplaced_attr_for_class_type (loc, declared_type); 5792 } 5793 5794 /* Diagnose invalid application of contracts, if any. */ 5795 if (find_contract (declspecs->attributes)) 5796 diagnose_misapplied_contracts (declspecs->attributes); 5797 else 5798 diagnose_misapplied_contracts (declspecs->std_attributes); 5799 5800 return declared_type; 5801 } 5802 5803 /* Called when a declaration is seen that contains no names to declare. 5804 If its type is a reference to a structure, union or enum inherited 5805 from a containing scope, shadow that tag name for the current scope 5806 with a forward reference. 5807 If its type defines a new named structure or union 5808 or defines an enum, it is valid but we need not do anything here. 5809 Otherwise, it is an error. 5810 5811 C++: may have to grok the declspecs to learn about static, 5812 complain for anonymous unions. 5813 5814 Returns the TYPE declared -- or NULL_TREE if none. */ 5815 5816 tree 5817 shadow_tag (cp_decl_specifier_seq *declspecs) 5818 { 5819 tree t = check_tag_decl (declspecs, 5820 /*explicit_type_instantiation_p=*/false); 5821 5822 if (!t) 5823 return NULL_TREE; 5824 5825 t = maybe_process_partial_specialization (t); 5826 if (t == error_mark_node) 5827 return NULL_TREE; 5828 5829 /* This is where the variables in an anonymous union are 5830 declared. An anonymous union declaration looks like: 5831 union { ... } ; 5832 because there is no declarator after the union, the parser 5833 sends that declaration here. */ 5834 if (ANON_AGGR_TYPE_P (t)) 5835 { 5836 fixup_anonymous_aggr (t); 5837 5838 if (TYPE_FIELDS (t)) 5839 { 5840 tree decl = grokdeclarator (/*declarator=*/NULL, 5841 declspecs, NORMAL, 0, NULL); 5842 finish_anon_union (decl); 5843 } 5844 } 5845 5846 return t; 5847 } 5848 5849 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */ 5851 5852 tree 5853 groktypename (cp_decl_specifier_seq *type_specifiers, 5854 const cp_declarator *declarator, 5855 bool is_template_arg) 5856 { 5857 tree attrs; 5858 tree type; 5859 enum decl_context context 5860 = is_template_arg ? TEMPLATE_TYPE_ARG : TYPENAME; 5861 attrs = type_specifiers->attributes; 5862 type_specifiers->attributes = NULL_TREE; 5863 type = grokdeclarator (declarator, type_specifiers, context, 0, &attrs); 5864 if (attrs && type != error_mark_node) 5865 { 5866 if (CLASS_TYPE_P (type)) 5867 warning (OPT_Wattributes, "ignoring attributes applied to class type %qT " 5868 "outside of definition", type); 5869 else if (MAYBE_CLASS_TYPE_P (type)) 5870 /* A template type parameter or other dependent type. */ 5871 warning (OPT_Wattributes, "ignoring attributes applied to dependent " 5872 "type %qT without an associated declaration", type); 5873 else 5874 cplus_decl_attributes (&type, attrs, 0); 5875 } 5876 return type; 5877 } 5878 5879 /* Process a DECLARATOR for a function-scope or namespace-scope 5880 variable or function declaration. 5881 (Function definitions go through start_function; class member 5882 declarations appearing in the body of the class go through 5883 grokfield.) The DECL corresponding to the DECLARATOR is returned. 5884 If an error occurs, the error_mark_node is returned instead. 5885 5886 DECLSPECS are the decl-specifiers for the declaration. INITIALIZED is 5887 SD_INITIALIZED if an explicit initializer is present, or SD_DEFAULTED 5888 for an explicitly defaulted function, or SD_DELETED for an explicitly 5889 deleted function, but 0 (SD_UNINITIALIZED) if this is a variable 5890 implicitly initialized via a default constructor. It can also be 5891 SD_DECOMPOSITION which behaves much like SD_INITIALIZED, but we also 5892 mark the new decl as DECL_DECOMPOSITION_P. 5893 5894 ATTRIBUTES and PREFIX_ATTRIBUTES are GNU attributes associated with this 5895 declaration. 5896 5897 The scope represented by the context of the returned DECL is pushed 5898 (if it is not the global namespace) and is assigned to 5899 *PUSHED_SCOPE_P. The caller is then responsible for calling 5900 pop_scope on *PUSHED_SCOPE_P if it is set. */ 5901 5902 tree 5903 start_decl (const cp_declarator *declarator, 5904 cp_decl_specifier_seq *declspecs, 5905 int initialized, 5906 tree attributes, 5907 tree prefix_attributes, 5908 tree *pushed_scope_p) 5909 { 5910 tree decl; 5911 tree context; 5912 bool was_public; 5913 int flags; 5914 bool alias; 5915 tree initial; 5916 5917 *pushed_scope_p = NULL_TREE; 5918 5919 if (prefix_attributes != error_mark_node) 5920 attributes = attr_chainon (attributes, prefix_attributes); 5921 5922 decl = grokdeclarator (declarator, declspecs, NORMAL, initialized, 5923 &attributes); 5924 5925 if (decl == NULL_TREE || VOID_TYPE_P (decl) 5926 || decl == error_mark_node 5927 || prefix_attributes == error_mark_node) 5928 return error_mark_node; 5929 5930 context = CP_DECL_CONTEXT (decl); 5931 if (context != global_namespace) 5932 *pushed_scope_p = push_scope (context); 5933 5934 if (initialized && TREE_CODE (decl) == TYPE_DECL) 5935 { 5936 error_at (DECL_SOURCE_LOCATION (decl), 5937 "typedef %qD is initialized (use %qs instead)", 5938 decl, "decltype"); 5939 return error_mark_node; 5940 } 5941 5942 /* Save the DECL_INITIAL value in case it gets clobbered to assist 5943 with attribute validation. */ 5944 initial = DECL_INITIAL (decl); 5945 5946 if (initialized) 5947 { 5948 if (! toplevel_bindings_p () 5949 && DECL_EXTERNAL (decl)) 5950 warning (0, "declaration of %q#D has %<extern%> and is initialized", 5951 decl); 5952 DECL_EXTERNAL (decl) = 0; 5953 if (toplevel_bindings_p ()) 5954 TREE_STATIC (decl) = 1; 5955 /* Tell 'cplus_decl_attributes' this is an initialized decl, 5956 even though we might not yet have the initializer expression. */ 5957 if (!DECL_INITIAL (decl)) 5958 DECL_INITIAL (decl) = error_mark_node; 5959 } 5960 alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl)) != 0; 5961 5962 if (alias && TREE_CODE (decl) == FUNCTION_DECL) 5963 record_key_method_defined (decl); 5964 5965 /* If this is a typedef that names the class for linkage purposes 5966 (7.1.3p8), apply any attributes directly to the type. */ 5967 if (TREE_CODE (decl) == TYPE_DECL 5968 && OVERLOAD_TYPE_P (TREE_TYPE (decl)) 5969 && decl == TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (decl)))) 5970 flags = ATTR_FLAG_TYPE_IN_PLACE; 5971 else 5972 flags = 0; 5973 5974 /* Set attributes here so if duplicate decl, will have proper attributes. */ 5975 cplus_decl_attributes (&decl, attributes, flags); 5976 5977 /* Restore the original DECL_INITIAL that we may have clobbered earlier to 5978 assist with attribute validation. */ 5979 DECL_INITIAL (decl) = initial; 5980 5981 /* Dllimported symbols cannot be defined. Static data members (which 5982 can be initialized in-class and dllimported) go through grokfield, 5983 not here, so we don't need to exclude those decls when checking for 5984 a definition. */ 5985 if (initialized && DECL_DLLIMPORT_P (decl)) 5986 { 5987 error_at (DECL_SOURCE_LOCATION (decl), 5988 "definition of %q#D is marked %<dllimport%>", decl); 5989 DECL_DLLIMPORT_P (decl) = 0; 5990 } 5991 5992 /* If #pragma weak was used, mark the decl weak now. */ 5993 if (!processing_template_decl && !DECL_DECOMPOSITION_P (decl)) 5994 maybe_apply_pragma_weak (decl); 5995 5996 if (TREE_CODE (decl) == FUNCTION_DECL 5997 && DECL_DECLARED_INLINE_P (decl) 5998 && DECL_UNINLINABLE (decl) 5999 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl))) 6000 warning_at (DECL_SOURCE_LOCATION (decl), 0, 6001 "inline function %qD given attribute %qs", decl, "noinline"); 6002 6003 if (TYPE_P (context) && COMPLETE_TYPE_P (complete_type (context))) 6004 { 6005 bool this_tmpl = (current_template_depth 6006 > template_class_depth (context)); 6007 if (VAR_P (decl)) 6008 { 6009 tree field = lookup_field (context, DECL_NAME (decl), 0, false); 6010 if (field == NULL_TREE 6011 || !(VAR_P (field) || variable_template_p (field))) 6012 error ("%q+#D is not a static data member of %q#T", decl, context); 6013 else if (variable_template_p (field) 6014 && (DECL_LANG_SPECIFIC (decl) 6015 && DECL_TEMPLATE_SPECIALIZATION (decl))) 6016 /* OK, specialization was already checked. */; 6017 else if (variable_template_p (field) && !this_tmpl) 6018 { 6019 error_at (DECL_SOURCE_LOCATION (decl), 6020 "non-member-template declaration of %qD", decl); 6021 inform (DECL_SOURCE_LOCATION (field), "does not match " 6022 "member template declaration here"); 6023 return error_mark_node; 6024 } 6025 else 6026 { 6027 if (variable_template_p (field)) 6028 field = DECL_TEMPLATE_RESULT (field); 6029 6030 if (DECL_CONTEXT (field) != context) 6031 { 6032 if (!same_type_p (DECL_CONTEXT (field), context)) 6033 permerror (input_location, "ISO C++ does not permit %<%T::%D%> " 6034 "to be defined as %<%T::%D%>", 6035 DECL_CONTEXT (field), DECL_NAME (decl), 6036 context, DECL_NAME (decl)); 6037 DECL_CONTEXT (decl) = DECL_CONTEXT (field); 6038 } 6039 /* Static data member are tricky; an in-class initialization 6040 still doesn't provide a definition, so the in-class 6041 declaration will have DECL_EXTERNAL set, but will have an 6042 initialization. Thus, duplicate_decls won't warn 6043 about this situation, and so we check here. */ 6044 if (initialized && DECL_INITIALIZED_IN_CLASS_P (field)) 6045 error ("duplicate initialization of %qD", decl); 6046 field = duplicate_decls (decl, field); 6047 if (field == error_mark_node) 6048 return error_mark_node; 6049 else if (field) 6050 decl = field; 6051 } 6052 } 6053 else 6054 { 6055 tree field = check_classfn (context, decl, 6056 this_tmpl 6057 ? current_template_parms 6058 : NULL_TREE); 6059 if (field && field != error_mark_node 6060 && duplicate_decls (decl, field)) 6061 decl = field; 6062 } 6063 6064 /* cp_finish_decl sets DECL_EXTERNAL if DECL_IN_AGGR_P is set. */ 6065 DECL_IN_AGGR_P (decl) = 0; 6066 /* Do not mark DECL as an explicit specialization if it was not 6067 already marked as an instantiation; a declaration should 6068 never be marked as a specialization unless we know what 6069 template is being specialized. */ 6070 if (DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl)) 6071 { 6072 SET_DECL_TEMPLATE_SPECIALIZATION (decl); 6073 if (TREE_CODE (decl) == FUNCTION_DECL) 6074 DECL_COMDAT (decl) = (TREE_PUBLIC (decl) 6075 && DECL_DECLARED_INLINE_P (decl)); 6076 else 6077 DECL_COMDAT (decl) = false; 6078 6079 /* [temp.expl.spec] An explicit specialization of a static data 6080 member of a template is a definition if the declaration 6081 includes an initializer; otherwise, it is a declaration. 6082 6083 We check for processing_specialization so this only applies 6084 to the new specialization syntax. */ 6085 if (!initialized && processing_specialization) 6086 DECL_EXTERNAL (decl) = 1; 6087 } 6088 6089 if (DECL_EXTERNAL (decl) && ! DECL_TEMPLATE_SPECIALIZATION (decl) 6090 /* Aliases are definitions. */ 6091 && !alias) 6092 { 6093 if (DECL_VIRTUAL_P (decl) || !flag_contracts) 6094 permerror (declarator->id_loc, 6095 "declaration of %q#D outside of class is not definition", 6096 decl); 6097 else if (flag_contract_strict_declarations) 6098 warning_at (declarator->id_loc, OPT_fcontract_strict_declarations_, 6099 "declaration of %q#D outside of class is not definition", 6100 decl); 6101 } 6102 } 6103 6104 /* Create a DECL_LANG_SPECIFIC so that DECL_DECOMPOSITION_P works. */ 6105 if (initialized == SD_DECOMPOSITION) 6106 fit_decomposition_lang_decl (decl, NULL_TREE); 6107 6108 was_public = TREE_PUBLIC (decl); 6109 6110 if ((DECL_EXTERNAL (decl) || TREE_CODE (decl) == FUNCTION_DECL) 6111 && current_function_decl) 6112 { 6113 /* A function-scope decl of some namespace-scope decl. */ 6114 DECL_LOCAL_DECL_P (decl) = true; 6115 if (named_module_attach_p ()) 6116 error_at (declarator->id_loc, 6117 "block-scope extern declaration %q#D must not be" 6118 " attached to a named module", decl); 6119 } 6120 6121 /* Enter this declaration into the symbol table. Don't push the plain 6122 VAR_DECL for a variable template. */ 6123 if (!template_parm_scope_p () 6124 || !VAR_P (decl)) 6125 decl = maybe_push_decl (decl); 6126 6127 if (processing_template_decl) 6128 decl = push_template_decl (decl); 6129 6130 if (decl == error_mark_node) 6131 return error_mark_node; 6132 6133 if (VAR_P (decl) 6134 && DECL_NAMESPACE_SCOPE_P (decl) && !TREE_PUBLIC (decl) && !was_public 6135 && !DECL_THIS_STATIC (decl) && !DECL_ARTIFICIAL (decl) 6136 /* But not templated variables. */ 6137 && !(DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))) 6138 { 6139 /* This is a const variable with implicit 'static'. Set 6140 DECL_THIS_STATIC so we can tell it from variables that are 6141 !TREE_PUBLIC because of the anonymous namespace. */ 6142 gcc_assert (CP_TYPE_CONST_P (TREE_TYPE (decl)) || errorcount); 6143 DECL_THIS_STATIC (decl) = 1; 6144 } 6145 6146 if (current_function_decl && VAR_P (decl) 6147 && DECL_DECLARED_CONSTEXPR_P (current_function_decl) 6148 && cxx_dialect < cxx23) 6149 { 6150 bool ok = false; 6151 if (CP_DECL_THREAD_LOCAL_P (decl) && !DECL_REALLY_EXTERN (decl)) 6152 error_at (DECL_SOURCE_LOCATION (decl), 6153 "%qD defined %<thread_local%> in %qs function only " 6154 "available with %<-std=c++2b%> or %<-std=gnu++2b%>", decl, 6155 DECL_IMMEDIATE_FUNCTION_P (current_function_decl) 6156 ? "consteval" : "constexpr"); 6157 else if (TREE_STATIC (decl)) 6158 error_at (DECL_SOURCE_LOCATION (decl), 6159 "%qD defined %<static%> in %qs function only available " 6160 "with %<-std=c++2b%> or %<-std=gnu++2b%>", decl, 6161 DECL_IMMEDIATE_FUNCTION_P (current_function_decl) 6162 ? "consteval" : "constexpr"); 6163 else 6164 ok = true; 6165 if (!ok) 6166 cp_function_chain->invalid_constexpr = true; 6167 } 6168 6169 if (!processing_template_decl && VAR_P (decl)) 6170 start_decl_1 (decl, initialized); 6171 6172 return decl; 6173 } 6174 6175 /* Process the declaration of a variable DECL. INITIALIZED is true 6176 iff DECL is explicitly initialized. (INITIALIZED is false if the 6177 variable is initialized via an implicitly-called constructor.) 6178 This function must be called for ordinary variables (including, for 6179 example, implicit instantiations of templates), but must not be 6180 called for template declarations. */ 6181 6182 void 6183 start_decl_1 (tree decl, bool initialized) 6184 { 6185 gcc_checking_assert (!processing_template_decl); 6186 6187 if (error_operand_p (decl)) 6188 return; 6189 6190 gcc_checking_assert (VAR_P (decl)); 6191 6192 tree type = TREE_TYPE (decl); 6193 bool complete_p = COMPLETE_TYPE_P (type); 6194 bool aggregate_definition_p 6195 = MAYBE_CLASS_TYPE_P (type) && !DECL_EXTERNAL (decl); 6196 6197 /* If an explicit initializer is present, or if this is a definition 6198 of an aggregate, then we need a complete type at this point. 6199 (Scalars are always complete types, so there is nothing to 6200 check.) This code just sets COMPLETE_P; errors (if necessary) 6201 are issued below. */ 6202 if ((initialized || aggregate_definition_p) 6203 && !complete_p 6204 && COMPLETE_TYPE_P (complete_type (type))) 6205 { 6206 complete_p = true; 6207 /* We will not yet have set TREE_READONLY on DECL if the type 6208 was "const", but incomplete, before this point. But, now, we 6209 have a complete type, so we can try again. */ 6210 cp_apply_type_quals_to_decl (cp_type_quals (type), decl); 6211 } 6212 6213 if (initialized) 6214 /* Is it valid for this decl to have an initializer at all? */ 6215 { 6216 /* Don't allow initializations for incomplete types except for 6217 arrays which might be completed by the initialization. */ 6218 if (complete_p) 6219 ; /* A complete type is ok. */ 6220 else if (type_uses_auto (type)) 6221 ; /* An auto type is ok. */ 6222 else if (TREE_CODE (type) != ARRAY_TYPE) 6223 { 6224 auto_diagnostic_group d; 6225 error ("variable %q#D has initializer but incomplete type", decl); 6226 maybe_suggest_missing_header (input_location, 6227 TYPE_IDENTIFIER (type), 6228 CP_TYPE_CONTEXT (type)); 6229 type = TREE_TYPE (decl) = error_mark_node; 6230 } 6231 else if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (type)))) 6232 { 6233 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)) 6234 error ("elements of array %q#D have incomplete type", decl); 6235 /* else we already gave an error in start_decl. */ 6236 } 6237 } 6238 else if (aggregate_definition_p && !complete_p) 6239 { 6240 if (type_uses_auto (type)) 6241 gcc_assert (CLASS_PLACEHOLDER_TEMPLATE (type)); 6242 else 6243 { 6244 auto_diagnostic_group d; 6245 error ("aggregate %q#D has incomplete type and cannot be defined", 6246 decl); 6247 maybe_suggest_missing_header (input_location, 6248 TYPE_IDENTIFIER (type), 6249 CP_TYPE_CONTEXT (type)); 6250 /* Change the type so that assemble_variable will give 6251 DECL an rtl we can live with: (mem (const_int 0)). */ 6252 type = TREE_TYPE (decl) = error_mark_node; 6253 } 6254 } 6255 6256 /* Create a new scope to hold this declaration if necessary. 6257 Whether or not a new scope is necessary cannot be determined 6258 until after the type has been completed; if the type is a 6259 specialization of a class template it is not until after 6260 instantiation has occurred that TYPE_HAS_NONTRIVIAL_DESTRUCTOR 6261 will be set correctly. */ 6262 maybe_push_cleanup_level (type); 6263 } 6264 6265 /* Given a parenthesized list of values INIT, create a CONSTRUCTOR to handle 6266 C++20 P0960. TYPE is the type of the object we're initializing. */ 6267 6268 tree 6269 do_aggregate_paren_init (tree init, tree type) 6270 { 6271 tree val = TREE_VALUE (init); 6272 6273 if (TREE_CHAIN (init) == NULL_TREE) 6274 { 6275 /* If the list has a single element and it's a string literal, 6276 then it's the initializer for the array as a whole. */ 6277 if (TREE_CODE (type) == ARRAY_TYPE 6278 && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type))) 6279 && TREE_CODE (tree_strip_any_location_wrapper (val)) 6280 == STRING_CST) 6281 return val; 6282 /* Handle non-standard extensions like compound literals. This also 6283 prevents triggering aggregate parenthesized-initialization in 6284 compiler-generated code for =default. */ 6285 else if (same_type_ignoring_top_level_qualifiers_p (type, 6286 TREE_TYPE (val))) 6287 return val; 6288 } 6289 6290 init = build_constructor_from_list (init_list_type_node, init); 6291 CONSTRUCTOR_IS_DIRECT_INIT (init) = true; 6292 CONSTRUCTOR_IS_PAREN_INIT (init) = true; 6293 return init; 6294 } 6295 6296 /* Handle initialization of references. DECL, TYPE, and INIT have the 6297 same meaning as in cp_finish_decl. *CLEANUP must be NULL on entry, 6298 but will be set to a new CLEANUP_STMT if a temporary is created 6299 that must be destroyed subsequently. 6300 6301 Returns an initializer expression to use to initialize DECL, or 6302 NULL if the initialization can be performed statically. 6303 6304 Quotes on semantics can be found in ARM 8.4.3. */ 6305 6306 static tree 6307 grok_reference_init (tree decl, tree type, tree init, int flags) 6308 { 6309 if (init == NULL_TREE) 6310 { 6311 if ((DECL_LANG_SPECIFIC (decl) == 0 6312 || DECL_IN_AGGR_P (decl) == 0) 6313 && ! DECL_THIS_EXTERN (decl)) 6314 error_at (DECL_SOURCE_LOCATION (decl), 6315 "%qD declared as reference but not initialized", decl); 6316 return NULL_TREE; 6317 } 6318 6319 tree ttype = TREE_TYPE (type); 6320 if (TREE_CODE (init) == TREE_LIST) 6321 { 6322 /* This handles (C++20 only) code like 6323 6324 const A& r(1, 2, 3); 6325 6326 where we treat the parenthesized list as a CONSTRUCTOR. */ 6327 if (TREE_TYPE (init) == NULL_TREE 6328 && CP_AGGREGATE_TYPE_P (ttype) 6329 && !DECL_DECOMPOSITION_P (decl) 6330 && (cxx_dialect >= cxx20)) 6331 { 6332 /* We don't know yet if we should treat const A& r(1) as 6333 const A& r{1}. */ 6334 if (list_length (init) == 1) 6335 { 6336 flags |= LOOKUP_AGGREGATE_PAREN_INIT; 6337 init = build_x_compound_expr_from_list (init, ELK_INIT, 6338 tf_warning_or_error); 6339 } 6340 /* If the list had more than one element, the code is ill-formed 6341 pre-C++20, so we can build a constructor right away. */ 6342 else 6343 init = do_aggregate_paren_init (init, ttype); 6344 } 6345 else 6346 init = build_x_compound_expr_from_list (init, ELK_INIT, 6347 tf_warning_or_error); 6348 } 6349 6350 if (TREE_CODE (ttype) != ARRAY_TYPE 6351 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE) 6352 /* Note: default conversion is only called in very special cases. */ 6353 init = decay_conversion (init, tf_warning_or_error); 6354 6355 /* check_initializer handles this for non-reference variables, but for 6356 references we need to do it here or the initializer will get the 6357 incomplete array type and confuse later calls to 6358 cp_complete_array_type. */ 6359 if (TREE_CODE (ttype) == ARRAY_TYPE 6360 && TYPE_DOMAIN (ttype) == NULL_TREE 6361 && (BRACE_ENCLOSED_INITIALIZER_P (init) 6362 || TREE_CODE (init) == STRING_CST)) 6363 { 6364 cp_complete_array_type (&ttype, init, false); 6365 if (ttype != TREE_TYPE (type)) 6366 type = cp_build_reference_type (ttype, TYPE_REF_IS_RVALUE (type)); 6367 } 6368 6369 /* Convert INIT to the reference type TYPE. This may involve the 6370 creation of a temporary, whose lifetime must be the same as that 6371 of the reference. If so, a DECL_EXPR for the temporary will be 6372 added just after the DECL_EXPR for DECL. That's why we don't set 6373 DECL_INITIAL for local references (instead assigning to them 6374 explicitly); we need to allow the temporary to be initialized 6375 first. */ 6376 return initialize_reference (type, init, flags, 6377 tf_warning_or_error); 6378 } 6379 6380 /* Designated initializers in arrays are not supported in GNU C++. 6381 The parser cannot detect this error since it does not know whether 6382 a given brace-enclosed initializer is for a class type or for an 6383 array. This function checks that CE does not use a designated 6384 initializer. If it does, an error is issued. Returns true if CE 6385 is valid, i.e., does not have a designated initializer. */ 6386 6387 bool 6388 check_array_designated_initializer (constructor_elt *ce, 6389 unsigned HOST_WIDE_INT index) 6390 { 6391 /* Designated initializers for array elements are not supported. */ 6392 if (ce->index) 6393 { 6394 /* The parser only allows identifiers as designated 6395 initializers. */ 6396 if (ce->index == error_mark_node) 6397 { 6398 error ("name used in a GNU-style designated " 6399 "initializer for an array"); 6400 return false; 6401 } 6402 else if (identifier_p (ce->index)) 6403 { 6404 error ("name %qD used in a GNU-style designated " 6405 "initializer for an array", ce->index); 6406 return false; 6407 } 6408 6409 tree ce_index = build_expr_type_conversion (WANT_INT | WANT_ENUM, 6410 ce->index, true); 6411 if (ce_index 6412 && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (ce_index)) 6413 && (TREE_CODE (ce_index = fold_non_dependent_expr (ce_index)) 6414 == INTEGER_CST)) 6415 { 6416 /* A C99 designator is OK if it matches the current index. */ 6417 if (wi::to_wide (ce_index) == index) 6418 { 6419 ce->index = ce_index; 6420 return true; 6421 } 6422 else 6423 sorry ("non-trivial designated initializers not supported"); 6424 } 6425 else 6426 error_at (cp_expr_loc_or_input_loc (ce->index), 6427 "C99 designator %qE is not an integral constant-expression", 6428 ce->index); 6429 6430 return false; 6431 } 6432 6433 return true; 6434 } 6435 6436 /* When parsing `int a[] = {1, 2};' we don't know the size of the 6437 array until we finish parsing the initializer. If that's the 6438 situation we're in, update DECL accordingly. */ 6439 6440 static void 6441 maybe_deduce_size_from_array_init (tree decl, tree init) 6442 { 6443 tree type = TREE_TYPE (decl); 6444 6445 if (TREE_CODE (type) == ARRAY_TYPE 6446 && TYPE_DOMAIN (type) == NULL_TREE 6447 && TREE_CODE (decl) != TYPE_DECL) 6448 { 6449 /* do_default is really a C-ism to deal with tentative definitions. 6450 But let's leave it here to ease the eventual merge. */ 6451 int do_default = !DECL_EXTERNAL (decl); 6452 tree initializer = init ? init : DECL_INITIAL (decl); 6453 int failure = 0; 6454 6455 /* Check that there are no designated initializers in INIT, as 6456 those are not supported in GNU C++, and as the middle-end 6457 will crash if presented with a non-numeric designated 6458 initializer. */ 6459 if (initializer && BRACE_ENCLOSED_INITIALIZER_P (initializer)) 6460 { 6461 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initializer); 6462 constructor_elt *ce; 6463 HOST_WIDE_INT i; 6464 FOR_EACH_VEC_SAFE_ELT (v, i, ce) 6465 { 6466 if (instantiation_dependent_expression_p (ce->index)) 6467 return; 6468 if (!check_array_designated_initializer (ce, i)) 6469 failure = 1; 6470 /* If an un-designated initializer is type-dependent, we can't 6471 check brace elision yet. */ 6472 if (ce->index == NULL_TREE 6473 && type_dependent_expression_p (ce->value)) 6474 return; 6475 } 6476 } 6477 6478 if (failure) 6479 TREE_TYPE (decl) = error_mark_node; 6480 else 6481 { 6482 failure = cp_complete_array_type (&TREE_TYPE (decl), initializer, 6483 do_default); 6484 if (failure == 1) 6485 { 6486 error_at (cp_expr_loc_or_loc (initializer, 6487 DECL_SOURCE_LOCATION (decl)), 6488 "initializer fails to determine size of %qD", decl); 6489 } 6490 else if (failure == 2) 6491 { 6492 if (do_default) 6493 { 6494 error_at (DECL_SOURCE_LOCATION (decl), 6495 "array size missing in %qD", decl); 6496 } 6497 /* If a `static' var's size isn't known, make it extern as 6498 well as static, so it does not get allocated. If it's not 6499 `static', then don't mark it extern; finish_incomplete_decl 6500 will give it a default size and it will get allocated. */ 6501 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl)) 6502 DECL_EXTERNAL (decl) = 1; 6503 } 6504 else if (failure == 3) 6505 { 6506 error_at (DECL_SOURCE_LOCATION (decl), 6507 "zero-size array %qD", decl); 6508 } 6509 } 6510 6511 cp_apply_type_quals_to_decl (cp_type_quals (TREE_TYPE (decl)), decl); 6512 6513 relayout_decl (decl); 6514 } 6515 } 6516 6517 /* Set DECL_SIZE, DECL_ALIGN, etc. for DECL (a VAR_DECL), and issue 6518 any appropriate error messages regarding the layout. */ 6519 6520 static void 6521 layout_var_decl (tree decl) 6522 { 6523 tree type; 6524 6525 type = TREE_TYPE (decl); 6526 if (type == error_mark_node) 6527 return; 6528 6529 /* If we haven't already laid out this declaration, do so now. 6530 Note that we must not call complete type for an external object 6531 because it's type might involve templates that we are not 6532 supposed to instantiate yet. (And it's perfectly valid to say 6533 `extern X x' for some incomplete type `X'.) */ 6534 if (!DECL_EXTERNAL (decl)) 6535 complete_type (type); 6536 if (!DECL_SIZE (decl) 6537 && TREE_TYPE (decl) != error_mark_node 6538 && complete_or_array_type_p (type)) 6539 layout_decl (decl, 0); 6540 6541 if (!DECL_EXTERNAL (decl) && DECL_SIZE (decl) == NULL_TREE) 6542 { 6543 /* An automatic variable with an incomplete type: that is an error. 6544 Don't talk about array types here, since we took care of that 6545 message in grokdeclarator. */ 6546 error_at (DECL_SOURCE_LOCATION (decl), 6547 "storage size of %qD isn%'t known", decl); 6548 TREE_TYPE (decl) = error_mark_node; 6549 } 6550 #if 0 6551 /* Keep this code around in case we later want to control debug info 6552 based on whether a type is "used". (jason 1999-11-11) */ 6553 6554 else if (!DECL_EXTERNAL (decl) && MAYBE_CLASS_TYPE_P (ttype)) 6555 /* Let debugger know it should output info for this type. */ 6556 note_debug_info_needed (ttype); 6557 6558 if (TREE_STATIC (decl) && DECL_CLASS_SCOPE_P (decl)) 6559 note_debug_info_needed (DECL_CONTEXT (decl)); 6560 #endif 6561 6562 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl)) 6563 && DECL_SIZE (decl) != NULL_TREE 6564 && ! TREE_CONSTANT (DECL_SIZE (decl))) 6565 { 6566 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST 6567 && !DECL_LOCAL_DECL_P (decl)) 6568 constant_expression_warning (DECL_SIZE (decl)); 6569 else 6570 { 6571 error_at (DECL_SOURCE_LOCATION (decl), 6572 "storage size of %qD isn%'t constant", decl); 6573 TREE_TYPE (decl) = error_mark_node; 6574 type = error_mark_node; 6575 } 6576 } 6577 6578 /* If the final element initializes a flexible array field, add the size of 6579 that initializer to DECL's size. */ 6580 if (type != error_mark_node 6581 && DECL_INITIAL (decl) 6582 && TREE_CODE (DECL_INITIAL (decl)) == CONSTRUCTOR 6583 && !vec_safe_is_empty (CONSTRUCTOR_ELTS (DECL_INITIAL (decl))) 6584 && DECL_SIZE (decl) != NULL_TREE 6585 && TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST 6586 && COMPLETE_TYPE_P (type) 6587 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST 6588 && tree_int_cst_equal (DECL_SIZE (decl), TYPE_SIZE (type))) 6589 { 6590 constructor_elt &elt = CONSTRUCTOR_ELTS (DECL_INITIAL (decl))->last (); 6591 if (elt.index) 6592 { 6593 tree itype = TREE_TYPE (elt.index); 6594 tree vtype = TREE_TYPE (elt.value); 6595 if (TREE_CODE (itype) == ARRAY_TYPE 6596 && TYPE_DOMAIN (itype) == NULL 6597 && TREE_CODE (vtype) == ARRAY_TYPE 6598 && COMPLETE_TYPE_P (vtype)) 6599 { 6600 DECL_SIZE (decl) 6601 = size_binop (PLUS_EXPR, DECL_SIZE (decl), TYPE_SIZE (vtype)); 6602 DECL_SIZE_UNIT (decl) 6603 = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl), 6604 TYPE_SIZE_UNIT (vtype)); 6605 } 6606 } 6607 } 6608 } 6609 6610 /* If a local static variable is declared in an inline function, or if 6611 we have a weak definition, we must endeavor to create only one 6612 instance of the variable at link-time. */ 6613 6614 void 6615 maybe_commonize_var (tree decl) 6616 { 6617 /* Don't mess with __FUNCTION__ and similar. But do handle structured 6618 bindings. */ 6619 if (DECL_ARTIFICIAL (decl) && !DECL_DECOMPOSITION_P (decl)) 6620 return; 6621 6622 /* Static data in a function with comdat linkage also has comdat 6623 linkage. */ 6624 if ((TREE_STATIC (decl) 6625 && DECL_FUNCTION_SCOPE_P (decl) 6626 && vague_linkage_p (DECL_CONTEXT (decl))) 6627 || (TREE_PUBLIC (decl) && DECL_INLINE_VAR_P (decl))) 6628 { 6629 if (flag_weak) 6630 { 6631 /* With weak symbols, we simply make the variable COMDAT; 6632 that will cause copies in multiple translations units to 6633 be merged. */ 6634 comdat_linkage (decl); 6635 } 6636 else 6637 { 6638 if (DECL_INITIAL (decl) == NULL_TREE 6639 || DECL_INITIAL (decl) == error_mark_node) 6640 { 6641 /* Without weak symbols, we can use COMMON to merge 6642 uninitialized variables. */ 6643 TREE_PUBLIC (decl) = 1; 6644 DECL_COMMON (decl) = 1; 6645 } 6646 else 6647 { 6648 /* While for initialized variables, we must use internal 6649 linkage -- which means that multiple copies will not 6650 be merged. */ 6651 TREE_PUBLIC (decl) = 0; 6652 DECL_COMMON (decl) = 0; 6653 DECL_INTERFACE_KNOWN (decl) = 1; 6654 const char *msg; 6655 if (DECL_INLINE_VAR_P (decl)) 6656 msg = G_("sorry: semantics of inline variable " 6657 "%q#D are wrong (you%'ll wind up with " 6658 "multiple copies)"); 6659 else 6660 msg = G_("sorry: semantics of inline function " 6661 "static data %q#D are wrong (you%'ll wind " 6662 "up with multiple copies)"); 6663 if (warning_at (DECL_SOURCE_LOCATION (decl), 0, 6664 msg, decl)) 6665 inform (DECL_SOURCE_LOCATION (decl), 6666 "you can work around this by removing the initializer"); 6667 } 6668 } 6669 } 6670 } 6671 6672 /* Issue an error message if DECL is an uninitialized const variable. 6673 CONSTEXPR_CONTEXT_P is true when the function is called in a constexpr 6674 context from potential_constant_expression. Returns true if all is well, 6675 false otherwise. */ 6676 6677 bool 6678 check_for_uninitialized_const_var (tree decl, bool constexpr_context_p, 6679 tsubst_flags_t complain) 6680 { 6681 tree type = strip_array_types (TREE_TYPE (decl)); 6682 6683 /* ``Unless explicitly declared extern, a const object does not have 6684 external linkage and must be initialized. ($8.4; $12.1)'' ARM 6685 7.1.6 */ 6686 if (VAR_P (decl) 6687 && !TYPE_REF_P (type) 6688 && (CP_TYPE_CONST_P (type) 6689 /* C++20 permits trivial default initialization in constexpr 6690 context (P1331R2). */ 6691 || (cxx_dialect < cxx20 6692 && (constexpr_context_p 6693 || var_in_constexpr_fn (decl)))) 6694 && !DECL_NONTRIVIALLY_INITIALIZED_P (decl)) 6695 { 6696 tree field = default_init_uninitialized_part (type); 6697 if (!field) 6698 return true; 6699 6700 bool show_notes = true; 6701 6702 if (!constexpr_context_p || cxx_dialect >= cxx20) 6703 { 6704 if (CP_TYPE_CONST_P (type)) 6705 { 6706 if (complain & tf_error) 6707 show_notes = permerror (DECL_SOURCE_LOCATION (decl), 6708 "uninitialized %<const %D%>", decl); 6709 } 6710 else 6711 { 6712 if (!is_instantiation_of_constexpr (current_function_decl) 6713 && (complain & tf_error)) 6714 error_at (DECL_SOURCE_LOCATION (decl), 6715 "uninitialized variable %qD in %<constexpr%> " 6716 "function", decl); 6717 else 6718 show_notes = false; 6719 cp_function_chain->invalid_constexpr = true; 6720 } 6721 } 6722 else if (complain & tf_error) 6723 error_at (DECL_SOURCE_LOCATION (decl), 6724 "uninitialized variable %qD in %<constexpr%> context", 6725 decl); 6726 6727 if (show_notes && CLASS_TYPE_P (type) && (complain & tf_error)) 6728 { 6729 tree defaulted_ctor; 6730 6731 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)), 6732 "%q#T has no user-provided default constructor", type); 6733 defaulted_ctor = in_class_defaulted_default_constructor (type); 6734 if (defaulted_ctor) 6735 inform (DECL_SOURCE_LOCATION (defaulted_ctor), 6736 "constructor is not user-provided because it is " 6737 "explicitly defaulted in the class body"); 6738 inform (DECL_SOURCE_LOCATION (field), 6739 "and the implicitly-defined constructor does not " 6740 "initialize %q#D", field); 6741 } 6742 6743 return false; 6744 } 6745 6746 return true; 6747 } 6748 6749 /* Structure holding the current initializer being processed by reshape_init. 6751 CUR is a pointer to the current element being processed, END is a pointer 6752 after the last element present in the initializer. */ 6753 struct reshape_iter 6754 { 6755 constructor_elt *cur; 6756 constructor_elt *end; 6757 }; 6758 6759 static tree reshape_init_r (tree, reshape_iter *, tree, tsubst_flags_t); 6760 6761 /* FIELD is an element of TYPE_FIELDS or NULL. In the former case, the value 6762 returned is the next FIELD_DECL (possibly FIELD itself) that can be 6763 initialized as if for an aggregate class. If there are no more such fields, 6764 the return value will be NULL. */ 6765 6766 tree 6767 next_aggregate_field (tree field) 6768 { 6769 while (field 6770 && (TREE_CODE (field) != FIELD_DECL 6771 || DECL_UNNAMED_BIT_FIELD (field) 6772 || (DECL_ARTIFICIAL (field) 6773 /* In C++17, aggregates can have bases. */ 6774 && !(cxx_dialect >= cxx17 && DECL_FIELD_IS_BASE (field))))) 6775 field = DECL_CHAIN (field); 6776 6777 return field; 6778 } 6779 6780 /* FIELD is an element of TYPE_FIELDS or NULL. In the former case, the value 6781 returned is the next FIELD_DECL (possibly FIELD itself) that corresponds 6782 to a subobject. If there are no more such fields, the return value will be 6783 NULL. */ 6784 6785 tree 6786 next_subobject_field (tree field) 6787 { 6788 while (field 6789 && (TREE_CODE (field) != FIELD_DECL 6790 || DECL_UNNAMED_BIT_FIELD (field) 6791 || (DECL_ARTIFICIAL (field) 6792 && !DECL_FIELD_IS_BASE (field) 6793 && !DECL_VIRTUAL_P (field)))) 6794 field = DECL_CHAIN (field); 6795 6796 return field; 6797 } 6798 6799 /* Return true for [dcl.init.list] direct-list-initialization from 6800 single element of enumeration with a fixed underlying type. */ 6801 6802 bool 6803 is_direct_enum_init (tree type, tree init) 6804 { 6805 if (cxx_dialect >= cxx17 6806 && TREE_CODE (type) == ENUMERAL_TYPE 6807 && ENUM_FIXED_UNDERLYING_TYPE_P (type) 6808 && TREE_CODE (init) == CONSTRUCTOR 6809 && CONSTRUCTOR_IS_DIRECT_INIT (init) 6810 && CONSTRUCTOR_NELTS (init) == 1 6811 /* DR 2374: The single element needs to be implicitly 6812 convertible to the underlying type of the enum. */ 6813 && !type_dependent_expression_p (CONSTRUCTOR_ELT (init, 0)->value) 6814 && can_convert_arg (ENUM_UNDERLYING_TYPE (type), 6815 TREE_TYPE (CONSTRUCTOR_ELT (init, 0)->value), 6816 CONSTRUCTOR_ELT (init, 0)->value, 6817 LOOKUP_IMPLICIT, tf_none)) 6818 return true; 6819 return false; 6820 } 6821 6822 /* Subroutine of reshape_init_array and reshape_init_vector, which does 6823 the actual work. ELT_TYPE is the element type of the array. MAX_INDEX is an 6824 INTEGER_CST representing the size of the array minus one (the maximum index), 6825 or NULL_TREE if the array was declared without specifying the size. D is 6826 the iterator within the constructor. */ 6827 6828 static tree 6829 reshape_init_array_1 (tree elt_type, tree max_index, reshape_iter *d, 6830 tree first_initializer_p, tsubst_flags_t complain) 6831 { 6832 tree new_init; 6833 bool sized_array_p = (max_index && TREE_CONSTANT (max_index)); 6834 unsigned HOST_WIDE_INT max_index_cst = 0; 6835 unsigned HOST_WIDE_INT index; 6836 6837 /* The initializer for an array is always a CONSTRUCTOR. If this is the 6838 outermost CONSTRUCTOR and the element type is non-aggregate, we don't need 6839 to build a new one. But don't reuse if not complaining; if this is 6840 tentative, we might also reshape to another type (95319). */ 6841 bool reuse = (first_initializer_p 6842 && (complain & tf_error) 6843 && !CP_AGGREGATE_TYPE_P (elt_type) 6844 && !TREE_SIDE_EFFECTS (first_initializer_p)); 6845 if (reuse) 6846 new_init = first_initializer_p; 6847 else 6848 new_init = build_constructor (init_list_type_node, NULL); 6849 6850 if (sized_array_p) 6851 { 6852 /* Minus 1 is used for zero sized arrays. */ 6853 if (integer_all_onesp (max_index)) 6854 return new_init; 6855 6856 if (tree_fits_uhwi_p (max_index)) 6857 max_index_cst = tree_to_uhwi (max_index); 6858 /* sizetype is sign extended, not zero extended. */ 6859 else 6860 max_index_cst = tree_to_uhwi (fold_convert (size_type_node, max_index)); 6861 } 6862 6863 /* Loop until there are no more initializers. */ 6864 for (index = 0; 6865 d->cur != d->end && (!sized_array_p || index <= max_index_cst); 6866 ++index) 6867 { 6868 tree elt_init; 6869 constructor_elt *old_cur = d->cur; 6870 6871 if (d->cur->index) 6872 CONSTRUCTOR_IS_DESIGNATED_INIT (new_init) = true; 6873 check_array_designated_initializer (d->cur, index); 6874 elt_init = reshape_init_r (elt_type, d, 6875 /*first_initializer_p=*/NULL_TREE, 6876 complain); 6877 if (elt_init == error_mark_node) 6878 return error_mark_node; 6879 tree idx = size_int (index); 6880 if (reuse) 6881 { 6882 old_cur->index = idx; 6883 old_cur->value = elt_init; 6884 } 6885 else 6886 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init), 6887 idx, elt_init); 6888 if (!TREE_CONSTANT (elt_init)) 6889 TREE_CONSTANT (new_init) = false; 6890 6891 /* This can happen with an invalid initializer (c++/54501). */ 6892 if (d->cur == old_cur && !sized_array_p) 6893 break; 6894 } 6895 6896 return new_init; 6897 } 6898 6899 /* Subroutine of reshape_init_r, processes the initializers for arrays. 6900 Parameters are the same of reshape_init_r. */ 6901 6902 static tree 6903 reshape_init_array (tree type, reshape_iter *d, tree first_initializer_p, 6904 tsubst_flags_t complain) 6905 { 6906 tree max_index = NULL_TREE; 6907 6908 gcc_assert (TREE_CODE (type) == ARRAY_TYPE); 6909 6910 if (TYPE_DOMAIN (type)) 6911 max_index = array_type_nelts (type); 6912 6913 return reshape_init_array_1 (TREE_TYPE (type), max_index, d, 6914 first_initializer_p, complain); 6915 } 6916 6917 /* Subroutine of reshape_init_r, processes the initializers for vectors. 6918 Parameters are the same of reshape_init_r. */ 6919 6920 static tree 6921 reshape_init_vector (tree type, reshape_iter *d, tsubst_flags_t complain) 6922 { 6923 tree max_index = NULL_TREE; 6924 6925 gcc_assert (VECTOR_TYPE_P (type)); 6926 6927 if (COMPOUND_LITERAL_P (d->cur->value)) 6928 { 6929 tree value = d->cur->value; 6930 if (!same_type_p (TREE_TYPE (value), type)) 6931 { 6932 if (complain & tf_error) 6933 error ("invalid type %qT as initializer for a vector of type %qT", 6934 TREE_TYPE (d->cur->value), type); 6935 value = error_mark_node; 6936 } 6937 ++d->cur; 6938 return value; 6939 } 6940 6941 /* For a vector, we initialize it as an array of the appropriate size. */ 6942 if (VECTOR_TYPE_P (type)) 6943 max_index = size_int (TYPE_VECTOR_SUBPARTS (type) - 1); 6944 6945 return reshape_init_array_1 (TREE_TYPE (type), max_index, d, 6946 NULL_TREE, complain); 6947 } 6948 6949 /* Subroutine of reshape_init*: We're initializing an element with TYPE from 6950 INIT, in isolation from any designator or other initializers. */ 6951 6952 static tree 6953 reshape_single_init (tree type, tree init, tsubst_flags_t complain) 6954 { 6955 /* We could also implement this by wrapping init in a new CONSTRUCTOR and 6956 calling reshape_init, but this way can just live on the stack. */ 6957 constructor_elt elt = { /*index=*/NULL_TREE, init }; 6958 reshape_iter iter = { &elt, &elt + 1 }; 6959 return reshape_init_r (type, &iter, 6960 /*first_initializer_p=*/NULL_TREE, 6961 complain); 6962 } 6963 6964 /* Subroutine of reshape_init_r, processes the initializers for classes 6965 or union. Parameters are the same of reshape_init_r. */ 6966 6967 static tree 6968 reshape_init_class (tree type, reshape_iter *d, bool first_initializer_p, 6969 tsubst_flags_t complain) 6970 { 6971 tree field; 6972 tree new_init; 6973 6974 gcc_assert (CLASS_TYPE_P (type)); 6975 6976 /* The initializer for a class is always a CONSTRUCTOR. */ 6977 new_init = build_constructor (init_list_type_node, NULL); 6978 6979 int binfo_idx = -1; 6980 tree binfo = TYPE_BINFO (type); 6981 tree base_binfo = NULL_TREE; 6982 if (cxx_dialect >= cxx17 && uses_template_parms (type)) 6983 { 6984 /* We get here from maybe_aggr_guide for C++20 class template argument 6985 deduction. In this case we need to look through the binfo because a 6986 template doesn't have base fields. */ 6987 binfo_idx = 0; 6988 BINFO_BASE_ITERATE (binfo, binfo_idx, base_binfo); 6989 } 6990 if (base_binfo) 6991 field = base_binfo; 6992 else 6993 field = next_aggregate_field (TYPE_FIELDS (type)); 6994 6995 if (!field) 6996 { 6997 /* [dcl.init.aggr] 6998 6999 An initializer for an aggregate member that is an 7000 empty class shall have the form of an empty 7001 initializer-list {}. */ 7002 if (!first_initializer_p) 7003 { 7004 if (complain & tf_error) 7005 error ("initializer for %qT must be brace-enclosed", type); 7006 return error_mark_node; 7007 } 7008 return new_init; 7009 } 7010 7011 /* For C++20 CTAD, handle pack expansions in the base list. */ 7012 tree last_was_pack_expansion = NULL_TREE; 7013 7014 /* Loop through the initializable fields, gathering initializers. */ 7015 while (d->cur != d->end) 7016 { 7017 tree field_init; 7018 constructor_elt *old_cur = d->cur; 7019 bool direct_desig = false; 7020 7021 /* Handle C++20 designated initializers. */ 7022 if (d->cur->index) 7023 { 7024 if (d->cur->index == error_mark_node) 7025 return error_mark_node; 7026 7027 if (TREE_CODE (d->cur->index) == FIELD_DECL) 7028 { 7029 /* We already reshaped this; we should have returned early from 7030 reshape_init. */ 7031 gcc_checking_assert (false); 7032 if (field != d->cur->index) 7033 { 7034 if (tree id = DECL_NAME (d->cur->index)) 7035 gcc_checking_assert (d->cur->index 7036 == get_class_binding (type, id)); 7037 field = d->cur->index; 7038 } 7039 } 7040 else if (TREE_CODE (d->cur->index) == IDENTIFIER_NODE) 7041 { 7042 CONSTRUCTOR_IS_DESIGNATED_INIT (new_init) = true; 7043 field = get_class_binding (type, d->cur->index); 7044 direct_desig = true; 7045 } 7046 else 7047 { 7048 if (complain & tf_error) 7049 error ("%<[%E] =%> used in a GNU-style designated initializer" 7050 " for class %qT", d->cur->index, type); 7051 return error_mark_node; 7052 } 7053 7054 if (!field && ANON_AGGR_TYPE_P (type)) 7055 /* Apparently the designator isn't for a member of this anonymous 7056 struct, so head back to the enclosing class. */ 7057 break; 7058 7059 if (!field || TREE_CODE (field) != FIELD_DECL) 7060 { 7061 if (complain & tf_error) 7062 { 7063 if (field && TREE_CODE (field) == TREE_LIST) 7064 { 7065 error ("request for member %qD is ambiguous", 7066 d->cur->index); 7067 print_candidates (field); 7068 } 7069 else 7070 error ("%qT has no non-static data member named %qD", type, 7071 d->cur->index); 7072 } 7073 return error_mark_node; 7074 } 7075 7076 /* If the element is an anonymous union object and the initializer 7077 list is a designated-initializer-list, the anonymous union object 7078 is initialized by the designated-initializer-list { D }, where D 7079 is the designated-initializer-clause naming a member of the 7080 anonymous union object. */ 7081 tree ictx = DECL_CONTEXT (field); 7082 if (!same_type_ignoring_top_level_qualifiers_p (ictx, type)) 7083 { 7084 /* Find the anon aggr that is a direct member of TYPE. */ 7085 while (ANON_AGGR_TYPE_P (ictx)) 7086 { 7087 tree cctx = TYPE_CONTEXT (ictx); 7088 if (same_type_ignoring_top_level_qualifiers_p (cctx, type)) 7089 goto found; 7090 ictx = cctx; 7091 } 7092 7093 /* Not found, e.g. FIELD is a member of a base class. */ 7094 if (complain & tf_error) 7095 error ("%qD is not a direct member of %qT", field, type); 7096 return error_mark_node; 7097 7098 found: 7099 /* Now find the TYPE member with that anon aggr type. */ 7100 tree aafield = TYPE_FIELDS (type); 7101 for (; aafield; aafield = TREE_CHAIN (aafield)) 7102 if (TREE_TYPE (aafield) == ictx) 7103 break; 7104 gcc_assert (aafield); 7105 field = aafield; 7106 direct_desig = false; 7107 } 7108 } 7109 7110 /* If we processed all the member of the class, we are done. */ 7111 if (!field) 7112 break; 7113 7114 last_was_pack_expansion = (PACK_EXPANSION_P (TREE_TYPE (field)) 7115 ? field : NULL_TREE); 7116 if (last_was_pack_expansion) 7117 /* Each non-trailing aggregate element that is a pack expansion is 7118 assumed to correspond to no elements of the initializer list. */ 7119 goto continue_; 7120 7121 if (direct_desig) 7122 { 7123 /* The designated field F is initialized from this one element. 7124 7125 Note that we don't want to do this if we found the designator 7126 inside an anon aggr above; we use the normal code to implement: 7127 7128 "If the element is an anonymous union member and the initializer 7129 list is a brace-enclosed designated- initializer-list, the element 7130 is initialized by the designated-initializer-list { D }, where D 7131 is the designated- initializer-clause naming a member of the 7132 anonymous union member." */ 7133 field_init = reshape_single_init (TREE_TYPE (field), 7134 d->cur->value, complain); 7135 d->cur++; 7136 } 7137 else 7138 field_init = reshape_init_r (TREE_TYPE (field), d, 7139 /*first_initializer_p=*/NULL_TREE, 7140 complain); 7141 7142 if (field_init == error_mark_node) 7143 return error_mark_node; 7144 7145 if (d->cur == old_cur && d->cur->index) 7146 { 7147 /* This can happen with an invalid initializer for a flexible 7148 array member (c++/54441). */ 7149 if (complain & tf_error) 7150 error ("invalid initializer for %q#D", field); 7151 return error_mark_node; 7152 } 7153 7154 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init), field, field_init); 7155 7156 /* [dcl.init.aggr] 7157 7158 When a union is initialized with a brace-enclosed 7159 initializer, the braces shall only contain an 7160 initializer for the first member of the union. */ 7161 if (TREE_CODE (type) == UNION_TYPE) 7162 break; 7163 7164 continue_: 7165 if (base_binfo) 7166 { 7167 if (BINFO_BASE_ITERATE (binfo, ++binfo_idx, base_binfo)) 7168 field = base_binfo; 7169 else 7170 field = next_aggregate_field (TYPE_FIELDS (type)); 7171 } 7172 else 7173 field = next_aggregate_field (DECL_CHAIN (field)); 7174 } 7175 7176 /* A trailing aggregate element that is a pack expansion is assumed to 7177 correspond to all remaining elements of the initializer list (if any). */ 7178 if (last_was_pack_expansion) 7179 { 7180 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init), 7181 last_was_pack_expansion, d->cur->value); 7182 while (d->cur != d->end) 7183 d->cur++; 7184 } 7185 7186 return new_init; 7187 } 7188 7189 /* Subroutine of reshape_init_r. We're in a context where C99 initializer 7190 designators are not valid; either complain or return true to indicate 7191 that reshape_init_r should return error_mark_node. */ 7192 7193 static bool 7194 has_designator_problem (reshape_iter *d, tsubst_flags_t complain) 7195 { 7196 if (d->cur->index) 7197 { 7198 if (complain & tf_error) 7199 error_at (cp_expr_loc_or_input_loc (d->cur->index), 7200 "C99 designator %qE outside aggregate initializer", 7201 d->cur->index); 7202 else 7203 return true; 7204 } 7205 return false; 7206 } 7207 7208 /* Subroutine of reshape_init, which processes a single initializer (part of 7209 a CONSTRUCTOR). TYPE is the type of the variable being initialized, D is the 7210 iterator within the CONSTRUCTOR which points to the initializer to process. 7211 If this is the first initializer of the outermost CONSTRUCTOR node, 7212 FIRST_INITIALIZER_P is that CONSTRUCTOR; otherwise, it is NULL_TREE. */ 7213 7214 static tree 7215 reshape_init_r (tree type, reshape_iter *d, tree first_initializer_p, 7216 tsubst_flags_t complain) 7217 { 7218 tree init = d->cur->value; 7219 7220 if (error_operand_p (init)) 7221 return error_mark_node; 7222 7223 if (first_initializer_p && !CP_AGGREGATE_TYPE_P (type) 7224 && has_designator_problem (d, complain)) 7225 return error_mark_node; 7226 7227 tree stripped_init = tree_strip_any_location_wrapper (init); 7228 7229 if (TREE_CODE (type) == COMPLEX_TYPE) 7230 { 7231 /* A complex type can be initialized from one or two initializers, 7232 but braces are not elided. */ 7233 d->cur++; 7234 if (BRACE_ENCLOSED_INITIALIZER_P (stripped_init)) 7235 { 7236 if (CONSTRUCTOR_NELTS (stripped_init) > 2) 7237 { 7238 if (complain & tf_error) 7239 error ("too many initializers for %qT", type); 7240 else 7241 return error_mark_node; 7242 } 7243 } 7244 else if (first_initializer_p && d->cur != d->end) 7245 { 7246 vec<constructor_elt, va_gc> *v = 0; 7247 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, init); 7248 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, d->cur->value); 7249 if (has_designator_problem (d, complain)) 7250 return error_mark_node; 7251 d->cur++; 7252 init = build_constructor (init_list_type_node, v); 7253 } 7254 return init; 7255 } 7256 7257 /* A non-aggregate type is always initialized with a single 7258 initializer. */ 7259 if (!CP_AGGREGATE_TYPE_P (type) 7260 /* As is an array with dependent bound, which we can see 7261 during C++20 aggregate CTAD. */ 7262 || (cxx_dialect >= cxx20 7263 && TREE_CODE (type) == ARRAY_TYPE 7264 && uses_template_parms (TYPE_DOMAIN (type)))) 7265 { 7266 /* It is invalid to initialize a non-aggregate type with a 7267 brace-enclosed initializer before C++0x. 7268 We need to check for BRACE_ENCLOSED_INITIALIZER_P here because 7269 of g++.old-deja/g++.mike/p7626.C: a pointer-to-member constant is 7270 a CONSTRUCTOR (with a record type). */ 7271 if (TREE_CODE (stripped_init) == CONSTRUCTOR 7272 /* Don't complain about a capture-init. */ 7273 && !CONSTRUCTOR_IS_DIRECT_INIT (stripped_init) 7274 && BRACE_ENCLOSED_INITIALIZER_P (stripped_init)) /* p7626.C */ 7275 { 7276 if (SCALAR_TYPE_P (type)) 7277 { 7278 if (cxx_dialect < cxx11) 7279 { 7280 if (complain & tf_error) 7281 error ("braces around scalar initializer for type %qT", 7282 type); 7283 init = error_mark_node; 7284 } 7285 else if (first_initializer_p 7286 || (CONSTRUCTOR_NELTS (stripped_init) > 0 7287 && (BRACE_ENCLOSED_INITIALIZER_P 7288 (CONSTRUCTOR_ELT (stripped_init,0)->value)))) 7289 { 7290 if (complain & tf_error) 7291 error ("too many braces around scalar initializer " 7292 "for type %qT", type); 7293 init = error_mark_node; 7294 } 7295 } 7296 else 7297 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS); 7298 } 7299 7300 d->cur++; 7301 return init; 7302 } 7303 7304 /* "If T is a class type and the initializer list has a single element of 7305 type cv U, where U is T or a class derived from T, the object is 7306 initialized from that element." Even if T is an aggregate. */ 7307 if (cxx_dialect >= cxx11 && (CLASS_TYPE_P (type) || VECTOR_TYPE_P (type)) 7308 && first_initializer_p 7309 /* But not if it's a designated init. */ 7310 && !d->cur->index 7311 && d->end - d->cur == 1 7312 && reference_related_p (type, TREE_TYPE (init))) 7313 { 7314 d->cur++; 7315 return init; 7316 } 7317 7318 /* [dcl.init.aggr] 7319 7320 All implicit type conversions (clause _conv_) are considered when 7321 initializing the aggregate member with an initializer from an 7322 initializer-list. If the initializer can initialize a member, 7323 the member is initialized. Otherwise, if the member is itself a 7324 non-empty subaggregate, brace elision is assumed and the 7325 initializer is considered for the initialization of the first 7326 member of the subaggregate. */ 7327 if ((TREE_CODE (init) != CONSTRUCTOR || COMPOUND_LITERAL_P (init)) 7328 /* But don't try this for the first initializer, since that would be 7329 looking through the outermost braces; A a2 = { a1 }; is not a 7330 valid aggregate initialization. */ 7331 && !first_initializer_p 7332 && (same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (init)) 7333 || can_convert_arg (type, TREE_TYPE (init), init, LOOKUP_NORMAL, 7334 complain))) 7335 { 7336 d->cur++; 7337 return init; 7338 } 7339 7340 /* [dcl.init.string] 7341 7342 A char array (whether plain char, signed char, or unsigned char) 7343 can be initialized by a string-literal (optionally enclosed in 7344 braces); a wchar_t array can be initialized by a wide 7345 string-literal (optionally enclosed in braces). */ 7346 if (TREE_CODE (type) == ARRAY_TYPE 7347 && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type)))) 7348 { 7349 tree str_init = init; 7350 tree stripped_str_init = stripped_init; 7351 reshape_iter stripd = {}; 7352 7353 /* Strip one level of braces if and only if they enclose a single 7354 element (as allowed by [dcl.init.string]). */ 7355 if (!first_initializer_p 7356 && TREE_CODE (stripped_str_init) == CONSTRUCTOR 7357 && CONSTRUCTOR_NELTS (stripped_str_init) == 1) 7358 { 7359 stripd.cur = CONSTRUCTOR_ELT (stripped_str_init, 0); 7360 str_init = stripd.cur->value; 7361 stripped_str_init = tree_strip_any_location_wrapper (str_init); 7362 } 7363 7364 /* If it's a string literal, then it's the initializer for the array 7365 as a whole. Otherwise, continue with normal initialization for 7366 array types (one value per array element). */ 7367 if (TREE_CODE (stripped_str_init) == STRING_CST) 7368 { 7369 if ((first_initializer_p && has_designator_problem (d, complain)) 7370 || (stripd.cur && has_designator_problem (&stripd, complain))) 7371 return error_mark_node; 7372 d->cur++; 7373 return str_init; 7374 } 7375 } 7376 7377 /* The following cases are about aggregates. If we are not within a full 7378 initializer already, and there is not a CONSTRUCTOR, it means that there 7379 is a missing set of braces (that is, we are processing the case for 7380 which reshape_init exists). */ 7381 bool braces_elided_p = false; 7382 if (!first_initializer_p) 7383 { 7384 if (TREE_CODE (stripped_init) == CONSTRUCTOR) 7385 { 7386 tree init_type = TREE_TYPE (init); 7387 if (init_type && TYPE_PTRMEMFUNC_P (init_type)) 7388 /* There is no need to call reshape_init for pointer-to-member 7389 function initializers, as they are always constructed correctly 7390 by the front end. Here we have e.g. {.__pfn=0B, .__delta=0}, 7391 which is missing outermost braces. We should warn below, and 7392 one of the routines below will wrap it in additional { }. */; 7393 /* For a nested compound literal, proceed to specialized routines, 7394 to handle initialization of arrays and similar. */ 7395 else if (COMPOUND_LITERAL_P (stripped_init)) 7396 gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (stripped_init)); 7397 /* If we have an unresolved designator, we need to find the member it 7398 designates within TYPE, so proceed to the routines below. For 7399 FIELD_DECL or INTEGER_CST designators, we're already initializing 7400 the designated element. */ 7401 else if (d->cur->index 7402 && TREE_CODE (d->cur->index) == IDENTIFIER_NODE) 7403 /* Brace elision with designators is only permitted for anonymous 7404 aggregates. */ 7405 gcc_checking_assert (ANON_AGGR_TYPE_P (type)); 7406 /* A CONSTRUCTOR of the target's type is a previously 7407 digested initializer. */ 7408 else if (same_type_ignoring_top_level_qualifiers_p (type, init_type)) 7409 { 7410 ++d->cur; 7411 return init; 7412 } 7413 else 7414 { 7415 /* Something that hasn't been reshaped yet. */ 7416 ++d->cur; 7417 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (stripped_init)); 7418 return reshape_init (type, init, complain); 7419 } 7420 } 7421 7422 if (complain & tf_warning) 7423 warning (OPT_Wmissing_braces, 7424 "missing braces around initializer for %qT", 7425 type); 7426 braces_elided_p = true; 7427 } 7428 7429 /* Dispatch to specialized routines. */ 7430 tree new_init; 7431 if (CLASS_TYPE_P (type)) 7432 new_init = reshape_init_class (type, d, first_initializer_p, complain); 7433 else if (TREE_CODE (type) == ARRAY_TYPE) 7434 new_init = reshape_init_array (type, d, first_initializer_p, complain); 7435 else if (VECTOR_TYPE_P (type)) 7436 new_init = reshape_init_vector (type, d, complain); 7437 else 7438 gcc_unreachable(); 7439 7440 if (braces_elided_p 7441 && TREE_CODE (new_init) == CONSTRUCTOR) 7442 CONSTRUCTOR_BRACES_ELIDED_P (new_init) = true; 7443 7444 return new_init; 7445 } 7446 7447 /* Undo the brace-elision allowed by [dcl.init.aggr] in a 7448 brace-enclosed aggregate initializer. 7449 7450 INIT is the CONSTRUCTOR containing the list of initializers describing 7451 a brace-enclosed initializer for an entity of the indicated aggregate TYPE. 7452 It may not presently match the shape of the TYPE; for example: 7453 7454 struct S { int a; int b; }; 7455 struct S a[] = { 1, 2, 3, 4 }; 7456 7457 Here INIT will hold a vector of four elements, rather than a 7458 vector of two elements, each itself a vector of two elements. This 7459 routine transforms INIT from the former form into the latter. The 7460 revised CONSTRUCTOR node is returned. */ 7461 7462 tree 7463 reshape_init (tree type, tree init, tsubst_flags_t complain) 7464 { 7465 vec<constructor_elt, va_gc> *v; 7466 reshape_iter d; 7467 tree new_init; 7468 7469 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init)); 7470 7471 v = CONSTRUCTOR_ELTS (init); 7472 7473 /* An empty constructor does not need reshaping, and it is always a valid 7474 initializer. */ 7475 if (vec_safe_is_empty (v)) 7476 return init; 7477 7478 if ((*v)[0].index && TREE_CODE ((*v)[0].index) == FIELD_DECL) 7479 /* Already reshaped. */ 7480 return init; 7481 7482 /* Brace elision is not performed for a CONSTRUCTOR representing 7483 parenthesized aggregate initialization. */ 7484 if (CONSTRUCTOR_IS_PAREN_INIT (init)) 7485 { 7486 tree elt = (*v)[0].value; 7487 /* If we're initializing a char array from a string-literal that is 7488 enclosed in braces, unwrap it here. */ 7489 if (TREE_CODE (type) == ARRAY_TYPE 7490 && vec_safe_length (v) == 1 7491 && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type))) 7492 && TREE_CODE (tree_strip_any_location_wrapper (elt)) == STRING_CST) 7493 return elt; 7494 return init; 7495 } 7496 7497 /* Handle [dcl.init.list] direct-list-initialization from 7498 single element of enumeration with a fixed underlying type. */ 7499 if (is_direct_enum_init (type, init)) 7500 { 7501 tree elt = CONSTRUCTOR_ELT (init, 0)->value; 7502 type = cv_unqualified (type); 7503 if (check_narrowing (ENUM_UNDERLYING_TYPE (type), elt, complain)) 7504 { 7505 warning_sentinel w (warn_useless_cast); 7506 warning_sentinel w2 (warn_ignored_qualifiers); 7507 return cp_build_c_cast (input_location, type, elt, 7508 tf_warning_or_error); 7509 } 7510 else 7511 return error_mark_node; 7512 } 7513 7514 /* Recurse on this CONSTRUCTOR. */ 7515 d.cur = &(*v)[0]; 7516 d.end = d.cur + v->length (); 7517 7518 new_init = reshape_init_r (type, &d, init, complain); 7519 if (new_init == error_mark_node) 7520 return error_mark_node; 7521 7522 /* Make sure all the element of the constructor were used. Otherwise, 7523 issue an error about exceeding initializers. */ 7524 if (d.cur != d.end) 7525 { 7526 if (complain & tf_error) 7527 error ("too many initializers for %qT", type); 7528 return error_mark_node; 7529 } 7530 7531 if (CONSTRUCTOR_IS_DIRECT_INIT (init) 7532 && BRACE_ENCLOSED_INITIALIZER_P (new_init)) 7533 CONSTRUCTOR_IS_DIRECT_INIT (new_init) = true; 7534 if (CONSTRUCTOR_IS_DESIGNATED_INIT (init) 7535 && BRACE_ENCLOSED_INITIALIZER_P (new_init)) 7536 gcc_checking_assert (CONSTRUCTOR_IS_DESIGNATED_INIT (new_init) 7537 || seen_error ()); 7538 7539 return new_init; 7540 } 7541 7542 /* Verify array initializer. Returns true if errors have been reported. */ 7543 7544 bool 7545 check_array_initializer (tree decl, tree type, tree init) 7546 { 7547 tree element_type = TREE_TYPE (type); 7548 7549 /* Structured binding when initialized with an array type needs 7550 to have complete type. */ 7551 if (decl 7552 && DECL_DECOMPOSITION_P (decl) 7553 && !DECL_DECOMP_BASE (decl) 7554 && !COMPLETE_TYPE_P (type)) 7555 { 7556 error_at (DECL_SOURCE_LOCATION (decl), 7557 "structured binding has incomplete type %qT", type); 7558 TREE_TYPE (decl) = error_mark_node; 7559 return true; 7560 } 7561 7562 /* The array type itself need not be complete, because the 7563 initializer may tell us how many elements are in the array. 7564 But, the elements of the array must be complete. */ 7565 if (!COMPLETE_TYPE_P (complete_type (element_type))) 7566 { 7567 if (decl) 7568 error_at (DECL_SOURCE_LOCATION (decl), 7569 "elements of array %q#D have incomplete type", decl); 7570 else 7571 error ("elements of array %q#T have incomplete type", type); 7572 return true; 7573 } 7574 7575 location_t loc = (decl ? location_of (decl) : input_location); 7576 if (!verify_type_context (loc, TCTX_ARRAY_ELEMENT, element_type)) 7577 return true; 7578 7579 /* A compound literal can't have variable size. */ 7580 if (init && !decl 7581 && ((COMPLETE_TYPE_P (type) && !TREE_CONSTANT (TYPE_SIZE (type))) 7582 || !TREE_CONSTANT (TYPE_SIZE (element_type)))) 7583 { 7584 error ("variable-sized compound literal"); 7585 return true; 7586 } 7587 return false; 7588 } 7589 7590 /* Subroutine of check_initializer; args are passed down from that function. 7591 Set stmts_are_full_exprs_p to 1 across a call to build_aggr_init. */ 7592 7593 static tree 7594 build_aggr_init_full_exprs (tree decl, tree init, int flags) 7595 { 7596 gcc_assert (stmts_are_full_exprs_p ()); 7597 if (init) 7598 maybe_warn_pessimizing_move (init, TREE_TYPE (decl), /*return_p*/false); 7599 return build_aggr_init (decl, init, flags, tf_warning_or_error); 7600 } 7601 7602 /* Verify INIT (the initializer for DECL), and record the 7603 initialization in DECL_INITIAL, if appropriate. CLEANUP is as for 7604 grok_reference_init. 7605 7606 If the return value is non-NULL, it is an expression that must be 7607 evaluated dynamically to initialize DECL. */ 7608 7609 static tree 7610 check_initializer (tree decl, tree init, int flags, vec<tree, va_gc> **cleanups) 7611 { 7612 tree type; 7613 tree init_code = NULL; 7614 tree core_type; 7615 7616 /* Things that are going to be initialized need to have complete 7617 type. */ 7618 TREE_TYPE (decl) = type = complete_type (TREE_TYPE (decl)); 7619 7620 if (DECL_HAS_VALUE_EXPR_P (decl)) 7621 { 7622 /* A variable with DECL_HAS_VALUE_EXPR_P set is just a placeholder, 7623 it doesn't have storage to be initialized. */ 7624 gcc_assert (init == NULL_TREE); 7625 return NULL_TREE; 7626 } 7627 7628 if (type == error_mark_node) 7629 /* We will have already complained. */ 7630 return NULL_TREE; 7631 7632 if (TREE_CODE (type) == ARRAY_TYPE) 7633 { 7634 if (check_array_initializer (decl, type, init)) 7635 return NULL_TREE; 7636 } 7637 else if (!COMPLETE_TYPE_P (type)) 7638 { 7639 error_at (DECL_SOURCE_LOCATION (decl), 7640 "%q#D has incomplete type", decl); 7641 TREE_TYPE (decl) = error_mark_node; 7642 return NULL_TREE; 7643 } 7644 else 7645 /* There is no way to make a variable-sized class type in GNU C++. */ 7646 gcc_assert (TREE_CONSTANT (TYPE_SIZE (type))); 7647 7648 if (init && BRACE_ENCLOSED_INITIALIZER_P (init)) 7649 { 7650 int init_len = CONSTRUCTOR_NELTS (init); 7651 if (SCALAR_TYPE_P (type)) 7652 { 7653 if (init_len == 0) 7654 { 7655 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS); 7656 init = build_zero_init (type, NULL_TREE, false); 7657 } 7658 else if (init_len != 1 && TREE_CODE (type) != COMPLEX_TYPE) 7659 { 7660 error_at (cp_expr_loc_or_loc (init, DECL_SOURCE_LOCATION (decl)), 7661 "scalar object %qD requires one element in " 7662 "initializer", decl); 7663 TREE_TYPE (decl) = error_mark_node; 7664 return NULL_TREE; 7665 } 7666 } 7667 } 7668 7669 if (TREE_CODE (decl) == CONST_DECL) 7670 { 7671 gcc_assert (!TYPE_REF_P (type)); 7672 7673 DECL_INITIAL (decl) = init; 7674 7675 gcc_assert (init != NULL_TREE); 7676 init = NULL_TREE; 7677 } 7678 else if (!init && DECL_REALLY_EXTERN (decl)) 7679 ; 7680 else if (init || type_build_ctor_call (type) 7681 || TYPE_REF_P (type)) 7682 { 7683 if (TYPE_REF_P (type)) 7684 { 7685 init = grok_reference_init (decl, type, init, flags); 7686 flags |= LOOKUP_ALREADY_DIGESTED; 7687 } 7688 else if (!init) 7689 check_for_uninitialized_const_var (decl, /*constexpr_context_p=*/false, 7690 tf_warning_or_error); 7691 /* Do not reshape constructors of vectors (they don't need to be 7692 reshaped. */ 7693 else if (BRACE_ENCLOSED_INITIALIZER_P (init)) 7694 { 7695 if (is_std_init_list (type)) 7696 { 7697 init = perform_implicit_conversion (type, init, 7698 tf_warning_or_error); 7699 flags |= LOOKUP_ALREADY_DIGESTED; 7700 } 7701 else if (TYPE_NON_AGGREGATE_CLASS (type)) 7702 { 7703 /* Don't reshape if the class has constructors. */ 7704 if (cxx_dialect == cxx98) 7705 error_at (cp_expr_loc_or_loc (init, DECL_SOURCE_LOCATION (decl)), 7706 "in C++98 %qD must be initialized by " 7707 "constructor, not by %<{...}%>", 7708 decl); 7709 } 7710 else if (VECTOR_TYPE_P (type) && TYPE_VECTOR_OPAQUE (type)) 7711 { 7712 error ("opaque vector types cannot be initialized"); 7713 init = error_mark_node; 7714 } 7715 else 7716 { 7717 init = reshape_init (type, init, tf_warning_or_error); 7718 flags |= LOOKUP_NO_NARROWING; 7719 } 7720 } 7721 /* [dcl.init] "Otherwise, if the destination type is an array, the object 7722 is initialized as follows..." So handle things like 7723 7724 int a[](1, 2, 3); 7725 7726 which is permitted in C++20 by P0960. */ 7727 else if (TREE_CODE (init) == TREE_LIST 7728 && TREE_TYPE (init) == NULL_TREE 7729 && TREE_CODE (type) == ARRAY_TYPE 7730 && !DECL_DECOMPOSITION_P (decl) 7731 && (cxx_dialect >= cxx20)) 7732 init = do_aggregate_paren_init (init, type); 7733 else if (TREE_CODE (init) == TREE_LIST 7734 && TREE_TYPE (init) != unknown_type_node 7735 && !MAYBE_CLASS_TYPE_P (type)) 7736 { 7737 gcc_assert (TREE_CODE (decl) != RESULT_DECL); 7738 7739 /* We get here with code like `int a (2);' */ 7740 init = build_x_compound_expr_from_list (init, ELK_INIT, 7741 tf_warning_or_error); 7742 } 7743 7744 /* If DECL has an array type without a specific bound, deduce the 7745 array size from the initializer. */ 7746 maybe_deduce_size_from_array_init (decl, init); 7747 type = TREE_TYPE (decl); 7748 if (type == error_mark_node) 7749 return NULL_TREE; 7750 7751 if (((type_build_ctor_call (type) || CLASS_TYPE_P (type)) 7752 && !(flags & LOOKUP_ALREADY_DIGESTED) 7753 && !(init && BRACE_ENCLOSED_INITIALIZER_P (init) 7754 && CP_AGGREGATE_TYPE_P (type) 7755 && (CLASS_TYPE_P (type) 7756 /* The call to build_aggr_init below could end up 7757 calling build_vec_init, which may break when we 7758 are processing a template. */ 7759 || processing_template_decl 7760 || !TYPE_NEEDS_CONSTRUCTING (type) 7761 || type_has_extended_temps (type)))) 7762 || (DECL_DECOMPOSITION_P (decl) && TREE_CODE (type) == ARRAY_TYPE)) 7763 { 7764 init_code = build_aggr_init_full_exprs (decl, init, flags); 7765 7766 /* A constructor call is a non-trivial initializer even if 7767 it isn't explicitly written. */ 7768 if (TREE_SIDE_EFFECTS (init_code)) 7769 DECL_NONTRIVIALLY_INITIALIZED_P (decl) = true; 7770 7771 /* If this is a constexpr initializer, expand_default_init will 7772 have returned an INIT_EXPR rather than a CALL_EXPR. In that 7773 case, pull the initializer back out and pass it down into 7774 store_init_value. */ 7775 while (true) 7776 { 7777 if (TREE_CODE (init_code) == EXPR_STMT 7778 || TREE_CODE (init_code) == STMT_EXPR 7779 || TREE_CODE (init_code) == CONVERT_EXPR) 7780 init_code = TREE_OPERAND (init_code, 0); 7781 else if (TREE_CODE (init_code) == BIND_EXPR) 7782 init_code = BIND_EXPR_BODY (init_code); 7783 else 7784 break; 7785 } 7786 if (TREE_CODE (init_code) == INIT_EXPR) 7787 { 7788 /* In C++20, the call to build_aggr_init could have created 7789 an INIT_EXPR with a CONSTRUCTOR as the RHS to handle 7790 A(1, 2). */ 7791 tree rhs = TREE_OPERAND (init_code, 1); 7792 if (processing_template_decl && TREE_CODE (rhs) == TARGET_EXPR) 7793 /* Avoid leaking TARGET_EXPR into template trees. */ 7794 rhs = build_implicit_conv_flags (type, init, flags); 7795 init = rhs; 7796 7797 init_code = NULL_TREE; 7798 /* Don't call digest_init; it's unnecessary and will complain 7799 about aggregate initialization of non-aggregate classes. */ 7800 flags |= LOOKUP_ALREADY_DIGESTED; 7801 } 7802 else if (DECL_DECLARED_CONSTEXPR_P (decl) 7803 || DECL_DECLARED_CONSTINIT_P (decl)) 7804 { 7805 /* Declared constexpr or constinit, but no suitable initializer; 7806 massage init appropriately so we can pass it into 7807 store_init_value for the error. */ 7808 tree new_init = NULL_TREE; 7809 if (!processing_template_decl 7810 && TREE_CODE (init_code) == CALL_EXPR) 7811 new_init = build_cplus_new (type, init_code, tf_none); 7812 else if (CLASS_TYPE_P (type) 7813 && (!init || TREE_CODE (init) == TREE_LIST)) 7814 new_init = build_functional_cast (input_location, type, 7815 init, tf_none); 7816 if (new_init) 7817 { 7818 init = new_init; 7819 if (TREE_CODE (init) == TARGET_EXPR 7820 && !(flags & LOOKUP_ONLYCONVERTING)) 7821 TARGET_EXPR_DIRECT_INIT_P (init) = true; 7822 } 7823 init_code = NULL_TREE; 7824 } 7825 else 7826 init = NULL_TREE; 7827 } 7828 7829 if (init && TREE_CODE (init) != TREE_VEC) 7830 { 7831 init_code = store_init_value (decl, init, cleanups, flags); 7832 7833 if (DECL_INITIAL (decl) 7834 && TREE_CODE (DECL_INITIAL (decl)) == CONSTRUCTOR 7835 && !vec_safe_is_empty (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)))) 7836 { 7837 tree elt = CONSTRUCTOR_ELTS (DECL_INITIAL (decl))->last ().value; 7838 if (TREE_CODE (TREE_TYPE (elt)) == ARRAY_TYPE 7839 && TYPE_SIZE (TREE_TYPE (elt)) == NULL_TREE) 7840 cp_complete_array_type (&TREE_TYPE (elt), elt, false); 7841 } 7842 7843 if (pedantic && TREE_CODE (type) == ARRAY_TYPE 7844 && DECL_INITIAL (decl) 7845 && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST 7846 && PAREN_STRING_LITERAL_P (DECL_INITIAL (decl))) 7847 warning_at (cp_expr_loc_or_loc (DECL_INITIAL (decl), 7848 DECL_SOURCE_LOCATION (decl)), 7849 0, "array %qD initialized by parenthesized " 7850 "string literal %qE", 7851 decl, DECL_INITIAL (decl)); 7852 init = NULL_TREE; 7853 } 7854 } 7855 else 7856 { 7857 if (CLASS_TYPE_P (core_type = strip_array_types (type)) 7858 && (CLASSTYPE_READONLY_FIELDS_NEED_INIT (core_type) 7859 || CLASSTYPE_REF_FIELDS_NEED_INIT (core_type))) 7860 diagnose_uninitialized_cst_or_ref_member (core_type, /*using_new=*/false, 7861 /*complain=*/true); 7862 7863 check_for_uninitialized_const_var (decl, /*constexpr_context_p=*/false, 7864 tf_warning_or_error); 7865 } 7866 7867 if (init && init != error_mark_node) 7868 init_code = cp_build_init_expr (decl, init); 7869 7870 if (init_code && !TREE_SIDE_EFFECTS (init_code) 7871 && init_code != error_mark_node) 7872 init_code = NULL_TREE; 7873 7874 if (init_code) 7875 { 7876 /* We might have set these in cp_finish_decl. */ 7877 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = false; 7878 TREE_CONSTANT (decl) = false; 7879 } 7880 7881 if (init_code 7882 && DECL_IN_AGGR_P (decl) 7883 && DECL_INITIALIZED_IN_CLASS_P (decl)) 7884 { 7885 static int explained = 0; 7886 7887 if (cxx_dialect < cxx11) 7888 error ("initializer invalid for static member with constructor"); 7889 else if (cxx_dialect < cxx17) 7890 error ("non-constant in-class initialization invalid for static " 7891 "member %qD", decl); 7892 else 7893 error ("non-constant in-class initialization invalid for non-inline " 7894 "static member %qD", decl); 7895 if (!explained) 7896 { 7897 inform (input_location, 7898 "(an out of class initialization is required)"); 7899 explained = 1; 7900 } 7901 return NULL_TREE; 7902 } 7903 7904 return init_code; 7905 } 7906 7907 /* If DECL is not a local variable, give it RTL. */ 7908 7909 static void 7910 make_rtl_for_nonlocal_decl (tree decl, tree init, const char* asmspec) 7911 { 7912 int toplev = toplevel_bindings_p (); 7913 int defer_p; 7914 7915 /* Set the DECL_ASSEMBLER_NAME for the object. */ 7916 if (asmspec) 7917 { 7918 /* The `register' keyword, when used together with an 7919 asm-specification, indicates that the variable should be 7920 placed in a particular register. */ 7921 if (VAR_P (decl) && DECL_REGISTER (decl)) 7922 { 7923 set_user_assembler_name (decl, asmspec); 7924 DECL_HARD_REGISTER (decl) = 1; 7925 } 7926 else 7927 { 7928 if (TREE_CODE (decl) == FUNCTION_DECL 7929 && fndecl_built_in_p (decl, BUILT_IN_NORMAL)) 7930 set_builtin_user_assembler_name (decl, asmspec); 7931 set_user_assembler_name (decl, asmspec); 7932 if (DECL_LOCAL_DECL_P (decl)) 7933 if (auto ns_decl = DECL_LOCAL_DECL_ALIAS (decl)) 7934 /* We have to propagate the name to the ns-alias. 7935 This is horrible, as we're affecting a 7936 possibly-shared decl. Again, a one-true-decl 7937 model breaks down. */ 7938 if (ns_decl != error_mark_node) 7939 set_user_assembler_name (ns_decl, asmspec); 7940 } 7941 } 7942 7943 /* Handle non-variables up front. */ 7944 if (!VAR_P (decl)) 7945 { 7946 rest_of_decl_compilation (decl, toplev, at_eof); 7947 return; 7948 } 7949 7950 /* If we see a class member here, it should be a static data 7951 member. */ 7952 if (DECL_LANG_SPECIFIC (decl) && DECL_IN_AGGR_P (decl)) 7953 { 7954 gcc_assert (TREE_STATIC (decl)); 7955 /* An in-class declaration of a static data member should be 7956 external; it is only a declaration, and not a definition. */ 7957 if (init == NULL_TREE) 7958 gcc_assert (DECL_EXTERNAL (decl) 7959 || !TREE_PUBLIC (decl)); 7960 } 7961 7962 /* We don't create any RTL for local variables. */ 7963 if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl)) 7964 return; 7965 7966 /* We defer emission of local statics until the corresponding 7967 DECL_EXPR is expanded. But with constexpr its function might never 7968 be expanded, so go ahead and tell cgraph about the variable now. */ 7969 defer_p = ((DECL_FUNCTION_SCOPE_P (decl) 7970 && !var_in_maybe_constexpr_fn (decl)) 7971 || DECL_VIRTUAL_P (decl)); 7972 7973 /* Defer template instantiations. */ 7974 if (DECL_LANG_SPECIFIC (decl) 7975 && DECL_IMPLICIT_INSTANTIATION (decl)) 7976 defer_p = 1; 7977 7978 /* If we're not deferring, go ahead and assemble the variable. */ 7979 if (!defer_p) 7980 rest_of_decl_compilation (decl, toplev, at_eof); 7981 } 7982 7983 /* walk_tree helper for wrap_temporary_cleanups, below. */ 7984 7985 static tree 7986 wrap_cleanups_r (tree *stmt_p, int *walk_subtrees, void *data) 7987 { 7988 /* Stop at types or full-expression boundaries. */ 7989 if (TYPE_P (*stmt_p) 7990 || TREE_CODE (*stmt_p) == CLEANUP_POINT_EXPR) 7991 { 7992 *walk_subtrees = 0; 7993 return NULL_TREE; 7994 } 7995 7996 if (TREE_CODE (*stmt_p) == TARGET_EXPR) 7997 { 7998 tree guard = (tree)data; 7999 tree tcleanup = TARGET_EXPR_CLEANUP (*stmt_p); 8000 8001 if (tcleanup && !CLEANUP_EH_ONLY (*stmt_p) 8002 && !expr_noexcept_p (tcleanup, tf_none)) 8003 { 8004 tcleanup = build2 (TRY_CATCH_EXPR, void_type_node, tcleanup, guard); 8005 /* Tell honor_protect_cleanup_actions to handle this as a separate 8006 cleanup. */ 8007 TRY_CATCH_IS_CLEANUP (tcleanup) = 1; 8008 TARGET_EXPR_CLEANUP (*stmt_p) = tcleanup; 8009 } 8010 } 8011 8012 return NULL_TREE; 8013 } 8014 8015 /* We're initializing a local variable which has a cleanup GUARD. If there 8016 are any temporaries used in the initializer INIT of this variable, we 8017 need to wrap their cleanups with TRY_CATCH_EXPR (, GUARD) so that the 8018 variable will be cleaned up properly if one of them throws. 8019 8020 Unfortunately, there's no way to express this properly in terms of 8021 nesting, as the regions for the temporaries overlap the region for the 8022 variable itself; if there are two temporaries, the variable needs to be 8023 the first thing destroyed if either of them throws. However, we only 8024 want to run the variable's cleanup if it actually got constructed. So 8025 we need to guard the temporary cleanups with the variable's cleanup if 8026 they are run on the normal path, but not if they are run on the 8027 exceptional path. We implement this by telling 8028 honor_protect_cleanup_actions to strip the variable cleanup from the 8029 exceptional path. 8030 8031 Another approach could be to make the variable cleanup region enclose 8032 initialization, but depend on a flag to indicate that the variable is 8033 initialized; that's effectively what we do for arrays. But the current 8034 approach works fine for non-arrays, and has no code overhead in the usual 8035 case where the temporary destructors are noexcept. */ 8036 8037 static void 8038 wrap_temporary_cleanups (tree init, tree guard) 8039 { 8040 if (TREE_CODE (guard) == BIND_EXPR) 8041 { 8042 /* An array cleanup region already encloses any temporary cleanups, 8043 don't wrap it around them again. */ 8044 gcc_checking_assert (BIND_EXPR_VEC_DTOR (guard)); 8045 return; 8046 } 8047 cp_walk_tree_without_duplicates (&init, wrap_cleanups_r, (void *)guard); 8048 } 8049 8050 /* Generate code to initialize DECL (a local variable). */ 8051 8052 static void 8053 initialize_local_var (tree decl, tree init) 8054 { 8055 tree type = TREE_TYPE (decl); 8056 tree cleanup; 8057 int already_used; 8058 8059 gcc_assert (VAR_P (decl) 8060 || TREE_CODE (decl) == RESULT_DECL); 8061 gcc_assert (!TREE_STATIC (decl)); 8062 8063 if (DECL_SIZE (decl) == NULL_TREE) 8064 { 8065 /* If we used it already as memory, it must stay in memory. */ 8066 DECL_INITIAL (decl) = NULL_TREE; 8067 TREE_ADDRESSABLE (decl) = TREE_USED (decl); 8068 return; 8069 } 8070 8071 if (type == error_mark_node) 8072 return; 8073 8074 /* Compute and store the initial value. */ 8075 already_used = TREE_USED (decl) || TREE_USED (type); 8076 if (TREE_USED (type)) 8077 DECL_READ_P (decl) = 1; 8078 8079 /* Generate a cleanup, if necessary. */ 8080 cleanup = cxx_maybe_build_cleanup (decl, tf_warning_or_error); 8081 8082 /* Perform the initialization. */ 8083 if (init) 8084 { 8085 tree rinit = (TREE_CODE (init) == INIT_EXPR 8086 ? TREE_OPERAND (init, 1) : NULL_TREE); 8087 if (rinit && !TREE_SIDE_EFFECTS (rinit) 8088 && TREE_OPERAND (init, 0) == decl) 8089 { 8090 /* Stick simple initializers in DECL_INITIAL so that 8091 -Wno-init-self works (c++/34772). */ 8092 DECL_INITIAL (decl) = rinit; 8093 8094 if (warn_init_self && TYPE_REF_P (type)) 8095 { 8096 STRIP_NOPS (rinit); 8097 if (rinit == decl) 8098 warning_at (DECL_SOURCE_LOCATION (decl), 8099 OPT_Winit_self, 8100 "reference %qD is initialized with itself", decl); 8101 } 8102 } 8103 else 8104 { 8105 int saved_stmts_are_full_exprs_p; 8106 8107 /* If we're only initializing a single object, guard the 8108 destructors of any temporaries used in its initializer with 8109 its destructor. */ 8110 if (cleanup) 8111 wrap_temporary_cleanups (init, cleanup); 8112 8113 gcc_assert (building_stmt_list_p ()); 8114 saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p (); 8115 current_stmt_tree ()->stmts_are_full_exprs_p = 1; 8116 finish_expr_stmt (init); 8117 current_stmt_tree ()->stmts_are_full_exprs_p = 8118 saved_stmts_are_full_exprs_p; 8119 } 8120 } 8121 8122 /* Set this to 0 so we can tell whether an aggregate which was 8123 initialized was ever used. Don't do this if it has a 8124 destructor, so we don't complain about the 'resource 8125 allocation is initialization' idiom. Now set 8126 attribute((unused)) on types so decls of that type will be 8127 marked used. (see TREE_USED, above.) */ 8128 if (TYPE_NEEDS_CONSTRUCTING (type) 8129 && ! already_used 8130 && TYPE_HAS_TRIVIAL_DESTRUCTOR (type) 8131 && DECL_NAME (decl)) 8132 TREE_USED (decl) = 0; 8133 else if (already_used) 8134 TREE_USED (decl) = 1; 8135 8136 if (cleanup) 8137 finish_decl_cleanup (decl, cleanup); 8138 } 8139 8140 /* DECL is a VAR_DECL for a compiler-generated variable with static 8141 storage duration (like a virtual table) whose initializer is a 8142 compile-time constant. Initialize the variable and provide it to the 8143 back end. */ 8144 8145 void 8146 initialize_artificial_var (tree decl, vec<constructor_elt, va_gc> *v) 8147 { 8148 tree init; 8149 gcc_assert (DECL_ARTIFICIAL (decl)); 8150 init = build_constructor (TREE_TYPE (decl), v); 8151 gcc_assert (TREE_CODE (init) == CONSTRUCTOR); 8152 DECL_INITIAL (decl) = init; 8153 DECL_INITIALIZED_P (decl) = 1; 8154 /* Mark the decl as constexpr so that we can access its content 8155 at compile time. */ 8156 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true; 8157 DECL_DECLARED_CONSTEXPR_P (decl) = true; 8158 determine_visibility (decl); 8159 layout_var_decl (decl); 8160 maybe_commonize_var (decl); 8161 make_rtl_for_nonlocal_decl (decl, init, /*asmspec=*/NULL); 8162 } 8163 8164 /* INIT is the initializer for a variable, as represented by the 8165 parser. Returns true iff INIT is value-dependent. */ 8166 8167 static bool 8168 value_dependent_init_p (tree init) 8169 { 8170 if (TREE_CODE (init) == TREE_LIST) 8171 /* A parenthesized initializer, e.g.: int i (3, 2); ? */ 8172 return any_value_dependent_elements_p (init); 8173 else if (TREE_CODE (init) == CONSTRUCTOR) 8174 /* A brace-enclosed initializer, e.g.: int i = { 3 }; ? */ 8175 { 8176 if (dependent_type_p (TREE_TYPE (init))) 8177 return true; 8178 8179 vec<constructor_elt, va_gc> *elts; 8180 size_t nelts; 8181 size_t i; 8182 8183 elts = CONSTRUCTOR_ELTS (init); 8184 nelts = vec_safe_length (elts); 8185 for (i = 0; i < nelts; ++i) 8186 if (value_dependent_init_p ((*elts)[i].value)) 8187 return true; 8188 } 8189 else 8190 /* It must be a simple expression, e.g., int i = 3; */ 8191 return value_dependent_expression_p (init); 8192 8193 return false; 8194 } 8195 8196 // Returns true if a DECL is VAR_DECL with the concept specifier. 8197 static inline bool 8198 is_concept_var (tree decl) 8199 { 8200 return (VAR_P (decl) 8201 // Not all variables have DECL_LANG_SPECIFIC. 8202 && DECL_LANG_SPECIFIC (decl) 8203 && DECL_DECLARED_CONCEPT_P (decl)); 8204 } 8205 8206 /* A helper function to be called via walk_tree. If any label exists 8207 under *TP, it is (going to be) forced. Set has_forced_label_in_static. */ 8208 8209 static tree 8210 notice_forced_label_r (tree *tp, int *walk_subtrees, void *) 8211 { 8212 if (TYPE_P (*tp)) 8213 *walk_subtrees = 0; 8214 if (TREE_CODE (*tp) == LABEL_DECL) 8215 cfun->has_forced_label_in_static = 1; 8216 return NULL_TREE; 8217 } 8218 8219 /* Return true if DECL has either a trivial destructor, or for C++20 8220 is constexpr and has a constexpr destructor. */ 8221 8222 static bool 8223 decl_maybe_constant_destruction (tree decl, tree type) 8224 { 8225 return (TYPE_HAS_TRIVIAL_DESTRUCTOR (type) 8226 || (cxx_dialect >= cxx20 8227 && VAR_P (decl) 8228 && DECL_DECLARED_CONSTEXPR_P (decl) 8229 && type_has_constexpr_destructor (strip_array_types (type)))); 8230 } 8231 8232 static tree declare_simd_adjust_this (tree *, int *, void *); 8233 8234 /* Helper function of omp_declare_variant_finalize. Finalize one 8235 "omp declare variant base" attribute. Return true if it should be 8236 removed. */ 8237 8238 static bool 8239 omp_declare_variant_finalize_one (tree decl, tree attr) 8240 { 8241 if (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE) 8242 { 8243 walk_tree (&TREE_VALUE (TREE_VALUE (attr)), declare_simd_adjust_this, 8244 DECL_ARGUMENTS (decl), NULL); 8245 walk_tree (&TREE_PURPOSE (TREE_VALUE (attr)), declare_simd_adjust_this, 8246 DECL_ARGUMENTS (decl), NULL); 8247 } 8248 8249 tree ctx = TREE_VALUE (TREE_VALUE (attr)); 8250 tree simd = omp_get_context_selector (ctx, OMP_TRAIT_SET_CONSTRUCT, 8251 OMP_TRAIT_CONSTRUCT_SIMD); 8252 if (simd) 8253 { 8254 TREE_VALUE (simd) 8255 = c_omp_declare_simd_clauses_to_numbers (DECL_ARGUMENTS (decl), 8256 OMP_TS_PROPERTIES (simd)); 8257 /* FIXME, adjusting simd args unimplemented. */ 8258 return true; 8259 } 8260 8261 tree chain = TREE_CHAIN (TREE_VALUE (attr)); 8262 location_t varid_loc 8263 = cp_expr_loc_or_input_loc (TREE_PURPOSE (TREE_CHAIN (chain))); 8264 location_t match_loc = cp_expr_loc_or_input_loc (TREE_PURPOSE (chain)); 8265 cp_id_kind idk = (cp_id_kind) tree_to_uhwi (TREE_VALUE (chain)); 8266 tree variant = TREE_PURPOSE (TREE_VALUE (attr)); 8267 8268 location_t save_loc = input_location; 8269 input_location = varid_loc; 8270 8271 releasing_vec args; 8272 tree parm = DECL_ARGUMENTS (decl); 8273 if (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE) 8274 parm = DECL_CHAIN (parm); 8275 for (; parm; parm = DECL_CHAIN (parm)) 8276 if (type_dependent_expression_p (parm)) 8277 vec_safe_push (args, build_constructor (TREE_TYPE (parm), NULL)); 8278 else if (MAYBE_CLASS_TYPE_P (TREE_TYPE (parm))) 8279 vec_safe_push (args, build_local_temp (TREE_TYPE (parm))); 8280 else 8281 vec_safe_push (args, build_zero_cst (TREE_TYPE (parm))); 8282 8283 bool koenig_p = false; 8284 if (idk == CP_ID_KIND_UNQUALIFIED || idk == CP_ID_KIND_TEMPLATE_ID) 8285 { 8286 if (identifier_p (variant) 8287 /* In C++20, we may need to perform ADL for a template 8288 name. */ 8289 || (TREE_CODE (variant) == TEMPLATE_ID_EXPR 8290 && identifier_p (TREE_OPERAND (variant, 0)))) 8291 { 8292 if (!args->is_empty ()) 8293 { 8294 koenig_p = true; 8295 if (!any_type_dependent_arguments_p (args)) 8296 variant = perform_koenig_lookup (variant, args, 8297 tf_warning_or_error); 8298 } 8299 else 8300 variant = unqualified_fn_lookup_error (variant); 8301 } 8302 else if (!args->is_empty () && is_overloaded_fn (variant)) 8303 { 8304 tree fn = get_first_fn (variant); 8305 fn = STRIP_TEMPLATE (fn); 8306 if (!((TREE_CODE (fn) == USING_DECL && DECL_DEPENDENT_P (fn)) 8307 || DECL_FUNCTION_MEMBER_P (fn) 8308 || DECL_LOCAL_DECL_P (fn))) 8309 { 8310 koenig_p = true; 8311 if (!any_type_dependent_arguments_p (args)) 8312 variant = perform_koenig_lookup (variant, args, 8313 tf_warning_or_error); 8314 } 8315 } 8316 } 8317 8318 if (idk == CP_ID_KIND_QUALIFIED) 8319 variant = finish_call_expr (variant, &args, /*disallow_virtual=*/true, 8320 koenig_p, tf_warning_or_error); 8321 else 8322 variant = finish_call_expr (variant, &args, /*disallow_virtual=*/false, 8323 koenig_p, tf_warning_or_error); 8324 if (variant == error_mark_node && !processing_template_decl) 8325 return true; 8326 8327 variant = cp_get_callee_fndecl_nofold (variant); 8328 input_location = save_loc; 8329 8330 if (variant) 8331 { 8332 const char *varname = IDENTIFIER_POINTER (DECL_NAME (variant)); 8333 if (!comptypes (TREE_TYPE (decl), TREE_TYPE (variant), 0)) 8334 { 8335 error_at (varid_loc, "variant %qD and base %qD have incompatible " 8336 "types", variant, decl); 8337 return true; 8338 } 8339 if (fndecl_built_in_p (variant) 8340 && (startswith (varname, "__builtin_") 8341 || startswith (varname, "__sync_") 8342 || startswith (varname, "__atomic_"))) 8343 { 8344 error_at (varid_loc, "variant %qD is a built-in", variant); 8345 return true; 8346 } 8347 else 8348 { 8349 tree construct 8350 = omp_get_context_selector_list (ctx, OMP_TRAIT_SET_CONSTRUCT); 8351 omp_mark_declare_variant (match_loc, variant, construct); 8352 if (!omp_context_selector_matches (ctx)) 8353 return true; 8354 TREE_PURPOSE (TREE_VALUE (attr)) = variant; 8355 } 8356 } 8357 else if (!processing_template_decl) 8358 { 8359 error_at (varid_loc, "could not find variant declaration"); 8360 return true; 8361 } 8362 8363 return false; 8364 } 8365 8366 /* Helper function, finish up "omp declare variant base" attribute 8367 now that there is a DECL. ATTR is the first "omp declare variant base" 8368 attribute. */ 8369 8370 void 8371 omp_declare_variant_finalize (tree decl, tree attr) 8372 { 8373 size_t attr_len = strlen ("omp declare variant base"); 8374 tree *list = &DECL_ATTRIBUTES (decl); 8375 bool remove_all = false; 8376 location_t match_loc = DECL_SOURCE_LOCATION (decl); 8377 if (TREE_CHAIN (TREE_VALUE (attr)) 8378 && TREE_PURPOSE (TREE_CHAIN (TREE_VALUE (attr))) 8379 && EXPR_HAS_LOCATION (TREE_PURPOSE (TREE_CHAIN (TREE_VALUE (attr))))) 8380 match_loc = EXPR_LOCATION (TREE_PURPOSE (TREE_CHAIN (TREE_VALUE (attr)))); 8381 if (DECL_CONSTRUCTOR_P (decl)) 8382 { 8383 error_at (match_loc, "%<declare variant%> on constructor %qD", decl); 8384 remove_all = true; 8385 } 8386 else if (DECL_DESTRUCTOR_P (decl)) 8387 { 8388 error_at (match_loc, "%<declare variant%> on destructor %qD", decl); 8389 remove_all = true; 8390 } 8391 else if (DECL_DEFAULTED_FN (decl)) 8392 { 8393 error_at (match_loc, "%<declare variant%> on defaulted %qD", decl); 8394 remove_all = true; 8395 } 8396 else if (DECL_DELETED_FN (decl)) 8397 { 8398 error_at (match_loc, "%<declare variant%> on deleted %qD", decl); 8399 remove_all = true; 8400 } 8401 else if (DECL_VIRTUAL_P (decl)) 8402 { 8403 error_at (match_loc, "%<declare variant%> on virtual %qD", decl); 8404 remove_all = true; 8405 } 8406 /* This loop is like private_lookup_attribute, except that it works 8407 with tree * rather than tree, as we might want to remove the 8408 attributes that are diagnosed as errorneous. */ 8409 while (*list) 8410 { 8411 tree attr = get_attribute_name (*list); 8412 size_t ident_len = IDENTIFIER_LENGTH (attr); 8413 if (cmp_attribs ("omp declare variant base", attr_len, 8414 IDENTIFIER_POINTER (attr), ident_len)) 8415 { 8416 if (remove_all || omp_declare_variant_finalize_one (decl, *list)) 8417 { 8418 *list = TREE_CHAIN (*list); 8419 continue; 8420 } 8421 } 8422 list = &TREE_CHAIN (*list); 8423 } 8424 } 8425 8426 static void cp_maybe_mangle_decomp (tree, cp_decomp *); 8427 8428 /* Finish processing of a declaration; 8429 install its line number and initial value. 8430 If the length of an array type is not known before, 8431 it must be determined now, from the initial value, or it is an error. 8432 8433 INIT is the initializer (if any) for DECL. If INIT_CONST_EXPR_P is 8434 true, then INIT is an integral constant expression. 8435 8436 FLAGS is LOOKUP_ONLYCONVERTING if the = init syntax was used, else 0 8437 if the (init) syntax was used. 8438 8439 DECOMP is first identifier's DECL and identifier count in a structured 8440 bindings, nullptr if not a structured binding. */ 8441 8442 void 8443 cp_finish_decl (tree decl, tree init, bool init_const_expr_p, 8444 tree asmspec_tree, int flags, cp_decomp *decomp) 8445 { 8446 vec<tree, va_gc> *cleanups = NULL; 8447 const char *asmspec = NULL; 8448 int was_readonly = 0; 8449 bool var_definition_p = false; 8450 tree auto_node; 8451 8452 if (decl == error_mark_node) 8453 return; 8454 else if (! decl) 8455 { 8456 if (init) 8457 error ("assignment (not initialization) in declaration"); 8458 return; 8459 } 8460 8461 gcc_assert (TREE_CODE (decl) != RESULT_DECL); 8462 /* Parameters are handled by store_parm_decls, not cp_finish_decl. */ 8463 gcc_assert (TREE_CODE (decl) != PARM_DECL); 8464 8465 tree type = TREE_TYPE (decl); 8466 if (type == error_mark_node) 8467 return; 8468 8469 if (VAR_P (decl) && is_copy_initialization (init)) 8470 flags |= LOOKUP_ONLYCONVERTING; 8471 8472 /* Warn about register storage specifiers except when in GNU global 8473 or local register variable extension. */ 8474 if (VAR_P (decl) && DECL_REGISTER (decl) && asmspec_tree == NULL_TREE) 8475 { 8476 if (cxx_dialect >= cxx17) 8477 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wregister, 8478 "ISO C++17 does not allow %<register%> storage " 8479 "class specifier"); 8480 else 8481 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wregister, 8482 "%<register%> storage class specifier used"); 8483 } 8484 8485 /* If a name was specified, get the string. */ 8486 if (at_namespace_scope_p ()) 8487 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree); 8488 if (asmspec_tree && asmspec_tree != error_mark_node) 8489 asmspec = TREE_STRING_POINTER (asmspec_tree); 8490 8491 bool in_class_decl 8492 = (current_class_type 8493 && CP_DECL_CONTEXT (decl) == current_class_type 8494 && TYPE_BEING_DEFINED (current_class_type) 8495 && !CLASSTYPE_TEMPLATE_INSTANTIATION (current_class_type)); 8496 8497 if (in_class_decl 8498 && (DECL_INITIAL (decl) || init)) 8499 DECL_INITIALIZED_IN_CLASS_P (decl) = 1; 8500 8501 if (VAR_P (decl) 8502 && (auto_node = type_uses_auto (type))) 8503 { 8504 tree d_init; 8505 if (init == NULL_TREE) 8506 { 8507 if (DECL_LANG_SPECIFIC (decl) 8508 && DECL_TEMPLATE_INSTANTIATION (decl) 8509 && !DECL_TEMPLATE_INSTANTIATED (decl)) 8510 { 8511 /* init is null because we're deferring instantiating the 8512 initializer until we need it. Well, we need it now. */ 8513 instantiate_decl (decl, /*defer_ok*/true, /*expl*/false); 8514 return; 8515 } 8516 8517 if (CLASS_PLACEHOLDER_TEMPLATE (auto_node)) 8518 /* Class deduction with no initializer is OK. */; 8519 else 8520 { 8521 /* Ordinary auto deduction without an initializer, a situation 8522 which grokdeclarator already detects and rejects for the most 8523 part. But we can still get here if we're instantiating a 8524 variable template before we've fully parsed (and attached) its 8525 initializer, e.g. template<class> auto x = x<int>; */ 8526 error_at (DECL_SOURCE_LOCATION (decl), 8527 "declaration of %q#D has no initializer", decl); 8528 TREE_TYPE (decl) = error_mark_node; 8529 return; 8530 } 8531 } 8532 d_init = init; 8533 if (d_init) 8534 { 8535 if (TREE_CODE (d_init) == TREE_LIST 8536 && !CLASS_PLACEHOLDER_TEMPLATE (auto_node)) 8537 d_init = build_x_compound_expr_from_list (d_init, ELK_INIT, 8538 tf_warning_or_error); 8539 d_init = resolve_nondeduced_context (d_init, tf_warning_or_error); 8540 /* Force auto deduction now. Use tf_none to avoid redundant warnings 8541 on deprecated-14.C. */ 8542 mark_single_function (d_init, tf_none); 8543 } 8544 enum auto_deduction_context adc = adc_variable_type; 8545 if (DECL_DECOMPOSITION_P (decl)) 8546 adc = adc_decomp_type; 8547 tree outer_targs = NULL_TREE; 8548 if (PLACEHOLDER_TYPE_CONSTRAINTS_INFO (auto_node) 8549 && DECL_LANG_SPECIFIC (decl) 8550 && DECL_TEMPLATE_INFO (decl) 8551 && !DECL_FUNCTION_SCOPE_P (decl)) 8552 /* The outer template arguments might be needed for satisfaction. 8553 (For function scope variables, do_auto_deduction will obtain the 8554 outer template arguments from current_function_decl.) */ 8555 outer_targs = DECL_TI_ARGS (decl); 8556 type = TREE_TYPE (decl) = do_auto_deduction (type, d_init, auto_node, 8557 tf_warning_or_error, adc, 8558 outer_targs, flags); 8559 if (type == error_mark_node) 8560 return; 8561 if (TREE_CODE (type) == FUNCTION_TYPE) 8562 { 8563 error ("initializer for %<decltype(auto) %D%> has function type; " 8564 "did you forget the %<()%>?", decl); 8565 TREE_TYPE (decl) = error_mark_node; 8566 return; 8567 } 8568 /* As in start_decl_1, complete so TREE_READONLY is set properly. */ 8569 if (!processing_template_decl 8570 && !type_uses_auto (type) 8571 && !COMPLETE_TYPE_P (complete_type (type))) 8572 { 8573 error_at (location_of (decl), 8574 "deduced type %qT for %qD is incomplete", type, decl); 8575 cxx_incomplete_type_inform (type); 8576 TREE_TYPE (decl) = error_mark_node; 8577 return; 8578 } 8579 cp_apply_type_quals_to_decl (cp_type_quals (type), decl); 8580 8581 /* Update the type of the corresponding TEMPLATE_DECL to match. */ 8582 if (DECL_LANG_SPECIFIC (decl) 8583 && DECL_TEMPLATE_INFO (decl) 8584 && DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (decl)) == decl) 8585 TREE_TYPE (DECL_TI_TEMPLATE (decl)) = type; 8586 } 8587 8588 if (ensure_literal_type_for_constexpr_object (decl) == error_mark_node) 8589 { 8590 DECL_DECLARED_CONSTEXPR_P (decl) = 0; 8591 if (VAR_P (decl) && DECL_CLASS_SCOPE_P (decl)) 8592 { 8593 init = NULL_TREE; 8594 DECL_EXTERNAL (decl) = 1; 8595 } 8596 } 8597 8598 if (VAR_P (decl) 8599 && DECL_CLASS_SCOPE_P (decl) 8600 && verify_type_context (DECL_SOURCE_LOCATION (decl), 8601 TCTX_STATIC_STORAGE, type) 8602 && DECL_INITIALIZED_IN_CLASS_P (decl)) 8603 check_static_variable_definition (decl, type); 8604 8605 if (!processing_template_decl && VAR_P (decl) && is_global_var (decl)) 8606 { 8607 type_context_kind context = (DECL_THREAD_LOCAL_P (decl) 8608 ? TCTX_THREAD_STORAGE 8609 : TCTX_STATIC_STORAGE); 8610 verify_type_context (input_location, context, TREE_TYPE (decl)); 8611 } 8612 8613 if (init && TREE_CODE (decl) == FUNCTION_DECL) 8614 { 8615 tree clone; 8616 if (init == ridpointers[(int)RID_DELETE]) 8617 { 8618 /* FIXME check this is 1st decl. */ 8619 DECL_DELETED_FN (decl) = 1; 8620 DECL_DECLARED_INLINE_P (decl) = 1; 8621 DECL_INITIAL (decl) = error_mark_node; 8622 FOR_EACH_CLONE (clone, decl) 8623 { 8624 DECL_DELETED_FN (clone) = 1; 8625 DECL_DECLARED_INLINE_P (clone) = 1; 8626 DECL_INITIAL (clone) = error_mark_node; 8627 } 8628 init = NULL_TREE; 8629 } 8630 else if (init == ridpointers[(int)RID_DEFAULT]) 8631 { 8632 if (defaultable_fn_check (decl)) 8633 DECL_DEFAULTED_FN (decl) = 1; 8634 else 8635 DECL_INITIAL (decl) = NULL_TREE; 8636 } 8637 } 8638 8639 if (init && VAR_P (decl)) 8640 { 8641 DECL_NONTRIVIALLY_INITIALIZED_P (decl) = 1; 8642 /* If DECL is a reference, then we want to know whether init is a 8643 reference constant; init_const_expr_p as passed tells us whether 8644 it's an rvalue constant. */ 8645 if (TYPE_REF_P (type)) 8646 init_const_expr_p = potential_constant_expression (init); 8647 if (init_const_expr_p) 8648 { 8649 /* Set these flags now for templates. We'll update the flags in 8650 store_init_value for instantiations. */ 8651 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1; 8652 if (decl_maybe_constant_var_p (decl) 8653 /* FIXME setting TREE_CONSTANT on refs breaks the back end. */ 8654 && !TYPE_REF_P (type)) 8655 TREE_CONSTANT (decl) = true; 8656 } 8657 /* This is handled mostly by gimplify.cc, but we have to deal with 8658 not warning about int x = x; as it is a GCC extension to turn off 8659 this warning but only if warn_init_self is zero. */ 8660 if (!DECL_EXTERNAL (decl) 8661 && !TREE_STATIC (decl) 8662 && decl == tree_strip_any_location_wrapper (init) 8663 && !warning_enabled_at (DECL_SOURCE_LOCATION (decl), OPT_Winit_self)) 8664 suppress_warning (decl, OPT_Winit_self); 8665 } 8666 else if (VAR_P (decl) 8667 && COMPLETE_TYPE_P (type) 8668 && !TYPE_REF_P (type) 8669 && !dependent_type_p (type) 8670 && is_really_empty_class (type, /*ignore_vptr*/false)) 8671 /* We have no initializer but there's nothing to initialize anyway. 8672 Treat DECL as constant due to c++/109876. */ 8673 TREE_CONSTANT (decl) = true; 8674 8675 if (flag_openmp 8676 && TREE_CODE (decl) == FUNCTION_DECL 8677 /* #pragma omp declare variant on methods handled in finish_struct 8678 instead. */ 8679 && (!DECL_OBJECT_MEMBER_FUNCTION_P (decl) 8680 || COMPLETE_TYPE_P (DECL_CONTEXT (decl)))) 8681 if (tree attr = lookup_attribute ("omp declare variant base", 8682 DECL_ATTRIBUTES (decl))) 8683 omp_declare_variant_finalize (decl, attr); 8684 8685 if (processing_template_decl) 8686 { 8687 bool type_dependent_p; 8688 8689 /* Add this declaration to the statement-tree. */ 8690 if (at_function_scope_p ()) 8691 add_decl_expr (decl); 8692 8693 type_dependent_p = dependent_type_p (type); 8694 8695 if (check_for_bare_parameter_packs (init)) 8696 { 8697 init = NULL_TREE; 8698 DECL_INITIAL (decl) = NULL_TREE; 8699 } 8700 8701 /* Generally, initializers in templates are expanded when the 8702 template is instantiated. But, if DECL is a variable constant 8703 then it can be used in future constant expressions, so its value 8704 must be available. */ 8705 8706 bool dep_init = false; 8707 8708 if (!VAR_P (decl) || type_dependent_p) 8709 /* We can't do anything if the decl has dependent type. */; 8710 else if (!init && is_concept_var (decl)) 8711 { 8712 error ("variable concept has no initializer"); 8713 init = boolean_true_node; 8714 } 8715 else if (init 8716 && (init_const_expr_p || DECL_DECLARED_CONSTEXPR_P (decl)) 8717 && !TYPE_REF_P (type) 8718 && decl_maybe_constant_var_p (decl) 8719 && !(dep_init = value_dependent_init_p (init))) 8720 { 8721 /* This variable seems to be a non-dependent constant, so process 8722 its initializer. If check_initializer returns non-null the 8723 initialization wasn't constant after all. */ 8724 tree init_code; 8725 cleanups = make_tree_vector (); 8726 init_code = check_initializer (decl, init, flags, &cleanups); 8727 if (init_code == NULL_TREE) 8728 init = NULL_TREE; 8729 release_tree_vector (cleanups); 8730 } 8731 else 8732 { 8733 gcc_assert (!DECL_PRETTY_FUNCTION_P (decl)); 8734 /* Try to deduce array size. */ 8735 maybe_deduce_size_from_array_init (decl, init); 8736 /* And complain about multiple initializers. */ 8737 if (init && TREE_CODE (init) == TREE_LIST && TREE_CHAIN (init) 8738 && !MAYBE_CLASS_TYPE_P (type)) 8739 init = build_x_compound_expr_from_list (init, ELK_INIT, 8740 tf_warning_or_error); 8741 } 8742 8743 if (init) 8744 DECL_INITIAL (decl) = init; 8745 8746 if (dep_init) 8747 { 8748 retrofit_lang_decl (decl); 8749 SET_DECL_DEPENDENT_INIT_P (decl, true); 8750 } 8751 8752 if (VAR_P (decl) && DECL_REGISTER (decl) && asmspec) 8753 { 8754 set_user_assembler_name (decl, asmspec); 8755 DECL_HARD_REGISTER (decl) = 1; 8756 } 8757 return; 8758 } 8759 8760 /* Just store non-static data member initializers for later. */ 8761 if (init && TREE_CODE (decl) == FIELD_DECL) 8762 DECL_INITIAL (decl) = init; 8763 8764 /* Take care of TYPE_DECLs up front. */ 8765 if (TREE_CODE (decl) == TYPE_DECL) 8766 { 8767 if (type != error_mark_node 8768 && MAYBE_CLASS_TYPE_P (type) && DECL_NAME (decl)) 8769 { 8770 if (TREE_TYPE (DECL_NAME (decl)) && TREE_TYPE (decl) != type) 8771 warning (0, "shadowing previous type declaration of %q#D", decl); 8772 set_identifier_type_value (DECL_NAME (decl), decl); 8773 } 8774 8775 /* If we have installed this as the canonical typedef for this 8776 type, and that type has not been defined yet, delay emitting 8777 the debug information for it, as we will emit it later. */ 8778 if (TYPE_MAIN_DECL (TREE_TYPE (decl)) == decl 8779 && !COMPLETE_TYPE_P (TREE_TYPE (decl))) 8780 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1; 8781 8782 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl), 8783 at_eof); 8784 return; 8785 } 8786 8787 /* A reference will be modified here, as it is initialized. */ 8788 if (! DECL_EXTERNAL (decl) 8789 && TREE_READONLY (decl) 8790 && TYPE_REF_P (type)) 8791 { 8792 was_readonly = 1; 8793 TREE_READONLY (decl) = 0; 8794 } 8795 8796 /* This needs to happen before extend_ref_init_temps. */ 8797 if (VAR_OR_FUNCTION_DECL_P (decl)) 8798 { 8799 if (VAR_P (decl)) 8800 maybe_commonize_var (decl); 8801 determine_visibility (decl); 8802 } 8803 8804 if (VAR_P (decl)) 8805 { 8806 duration_kind dk = decl_storage_duration (decl); 8807 /* [dcl.constinit]/1 "The constinit specifier shall be applied 8808 only to a declaration of a variable with static or thread storage 8809 duration." */ 8810 if (DECL_DECLARED_CONSTINIT_P (decl) 8811 && !(dk == dk_thread || dk == dk_static)) 8812 { 8813 error_at (DECL_SOURCE_LOCATION (decl), 8814 "%<constinit%> can only be applied to a variable with " 8815 "static or thread storage duration"); 8816 return; 8817 } 8818 8819 if (decomp) 8820 cp_maybe_mangle_decomp (decl, decomp); 8821 8822 /* If this is a local variable that will need a mangled name, 8823 register it now. We must do this before processing the 8824 initializer for the variable, since the initialization might 8825 require a guard variable, and since the mangled name of the 8826 guard variable will depend on the mangled name of this 8827 variable. */ 8828 if (DECL_FUNCTION_SCOPE_P (decl) 8829 && TREE_STATIC (decl) 8830 && !DECL_ARTIFICIAL (decl)) 8831 { 8832 /* The variable holding an anonymous union will have had its 8833 discriminator set in finish_anon_union, after which it's 8834 NAME will have been cleared. */ 8835 if (DECL_NAME (decl)) 8836 determine_local_discriminator (decl); 8837 /* Normally has_forced_label_in_static is set during GIMPLE 8838 lowering, but [cd]tors are never actually compiled directly. 8839 We need to set this early so we can deal with the label 8840 address extension. */ 8841 if ((DECL_CONSTRUCTOR_P (current_function_decl) 8842 || DECL_DESTRUCTOR_P (current_function_decl)) 8843 && init) 8844 { 8845 walk_tree (&init, notice_forced_label_r, NULL, NULL); 8846 add_local_decl (cfun, decl); 8847 } 8848 /* And make sure it's in the symbol table for 8849 c_parse_final_cleanups to find. */ 8850 varpool_node::get_create (decl); 8851 } 8852 8853 /* Convert the initializer to the type of DECL, if we have not 8854 already initialized DECL. */ 8855 if (!DECL_INITIALIZED_P (decl) 8856 /* If !DECL_EXTERNAL then DECL is being defined. In the 8857 case of a static data member initialized inside the 8858 class-specifier, there can be an initializer even if DECL 8859 is *not* defined. */ 8860 && (!DECL_EXTERNAL (decl) || init)) 8861 { 8862 cleanups = make_tree_vector (); 8863 init = check_initializer (decl, init, flags, &cleanups); 8864 8865 /* Handle: 8866 8867 [dcl.init] 8868 8869 The memory occupied by any object of static storage 8870 duration is zero-initialized at program startup before 8871 any other initialization takes place. 8872 8873 We cannot create an appropriate initializer until after 8874 the type of DECL is finalized. If DECL_INITIAL is set, 8875 then the DECL is statically initialized, and any 8876 necessary zero-initialization has already been performed. */ 8877 if (TREE_STATIC (decl) && !DECL_INITIAL (decl)) 8878 DECL_INITIAL (decl) = build_zero_init (TREE_TYPE (decl), 8879 /*nelts=*/NULL_TREE, 8880 /*static_storage_p=*/true); 8881 /* Remember that the initialization for this variable has 8882 taken place. */ 8883 DECL_INITIALIZED_P (decl) = 1; 8884 /* This declaration is the definition of this variable, 8885 unless we are initializing a static data member within 8886 the class specifier. */ 8887 if (!DECL_EXTERNAL (decl)) 8888 var_definition_p = true; 8889 } 8890 /* If the variable has an array type, lay out the type, even if 8891 there is no initializer. It is valid to index through the 8892 array, and we must get TYPE_ALIGN set correctly on the array 8893 type. */ 8894 else if (TREE_CODE (type) == ARRAY_TYPE) 8895 layout_type (type); 8896 8897 if (TREE_STATIC (decl) 8898 && !at_function_scope_p () 8899 && current_function_decl == NULL) 8900 /* So decl is a global variable or a static member of a 8901 non local class. Record the types it uses 8902 so that we can decide later to emit debug info for them. */ 8903 record_types_used_by_current_var_decl (decl); 8904 } 8905 8906 /* Add this declaration to the statement-tree. This needs to happen 8907 after the call to check_initializer so that the DECL_EXPR for a 8908 reference temp is added before the DECL_EXPR for the reference itself. */ 8909 if (DECL_FUNCTION_SCOPE_P (decl)) 8910 { 8911 /* If we're building a variable sized type, and we might be 8912 reachable other than via the top of the current binding 8913 level, then create a new BIND_EXPR so that we deallocate 8914 the object at the right time. */ 8915 if (VAR_P (decl) 8916 && DECL_SIZE (decl) 8917 && !TREE_CONSTANT (DECL_SIZE (decl)) 8918 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list)) 8919 { 8920 tree bind; 8921 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL); 8922 TREE_SIDE_EFFECTS (bind) = 1; 8923 add_stmt (bind); 8924 BIND_EXPR_BODY (bind) = push_stmt_list (); 8925 } 8926 add_decl_expr (decl); 8927 } 8928 8929 /* Let the middle end know about variables and functions -- but not 8930 static data members in uninstantiated class templates. */ 8931 if (VAR_OR_FUNCTION_DECL_P (decl)) 8932 { 8933 if (VAR_P (decl)) 8934 { 8935 layout_var_decl (decl); 8936 if (!flag_weak) 8937 /* Check again now that we have an initializer. */ 8938 maybe_commonize_var (decl); 8939 /* A class-scope constexpr variable with an out-of-class declaration. 8940 C++17 makes them implicitly inline, but still force it out. */ 8941 if (DECL_INLINE_VAR_P (decl) 8942 && !DECL_VAR_DECLARED_INLINE_P (decl) 8943 && !DECL_TEMPLATE_INSTANTIATION (decl) 8944 && !in_class_decl) 8945 mark_needed (decl); 8946 } 8947 8948 if (var_definition_p 8949 /* With -fmerge-all-constants, gimplify_init_constructor 8950 might add TREE_STATIC to aggregate variables. */ 8951 && (TREE_STATIC (decl) 8952 || (flag_merge_constants >= 2 8953 && AGGREGATE_TYPE_P (type)))) 8954 { 8955 /* If a TREE_READONLY variable needs initialization 8956 at runtime, it is no longer readonly and we need to 8957 avoid MEM_READONLY_P being set on RTL created for it. */ 8958 if (init) 8959 { 8960 if (TREE_READONLY (decl)) 8961 TREE_READONLY (decl) = 0; 8962 was_readonly = 0; 8963 } 8964 else if (was_readonly) 8965 TREE_READONLY (decl) = 1; 8966 8967 /* Likewise if it needs destruction. */ 8968 if (!decl_maybe_constant_destruction (decl, type)) 8969 TREE_READONLY (decl) = 0; 8970 } 8971 else if (VAR_P (decl) 8972 && CP_DECL_THREAD_LOCAL_P (decl) 8973 && (!DECL_EXTERNAL (decl) || flag_extern_tls_init) 8974 && (was_readonly || TREE_READONLY (decl)) 8975 && var_needs_tls_wrapper (decl)) 8976 { 8977 /* TLS variables need dynamic initialization by the TLS wrapper 8978 function, we don't want to hoist accesses to it before the 8979 wrapper. */ 8980 was_readonly = 0; 8981 TREE_READONLY (decl) = 0; 8982 } 8983 8984 make_rtl_for_nonlocal_decl (decl, init, asmspec); 8985 8986 /* Check for abstractness of the type. */ 8987 if (var_definition_p) 8988 abstract_virtuals_error (decl, type); 8989 8990 if (TREE_TYPE (decl) == error_mark_node) 8991 /* No initialization required. */ 8992 ; 8993 else if (TREE_CODE (decl) == FUNCTION_DECL) 8994 { 8995 if (init) 8996 { 8997 if (init == ridpointers[(int)RID_DEFAULT]) 8998 { 8999 /* An out-of-class default definition is defined at 9000 the point where it is explicitly defaulted. */ 9001 if (DECL_DELETED_FN (decl)) 9002 maybe_explain_implicit_delete (decl); 9003 else if (DECL_INITIAL (decl) == error_mark_node) 9004 synthesize_method (decl); 9005 } 9006 else 9007 error_at (cp_expr_loc_or_loc (init, 9008 DECL_SOURCE_LOCATION (decl)), 9009 "function %q#D is initialized like a variable", 9010 decl); 9011 } 9012 /* else no initialization required. */ 9013 } 9014 else if (DECL_EXTERNAL (decl) 9015 && ! (DECL_LANG_SPECIFIC (decl) 9016 && DECL_NOT_REALLY_EXTERN (decl))) 9017 { 9018 /* check_initializer will have done any constant initialization. */ 9019 } 9020 /* A variable definition. */ 9021 else if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl)) 9022 /* Initialize the local variable. */ 9023 initialize_local_var (decl, init); 9024 9025 /* If a variable is defined, and then a subsequent 9026 definition with external linkage is encountered, we will 9027 get here twice for the same variable. We want to avoid 9028 calling expand_static_init more than once. For variables 9029 that are not static data members, we can call 9030 expand_static_init only when we actually process the 9031 initializer. It is not legal to redeclare a static data 9032 member, so this issue does not arise in that case. */ 9033 else if (var_definition_p && TREE_STATIC (decl)) 9034 expand_static_init (decl, init); 9035 } 9036 9037 /* If a CLEANUP_STMT was created to destroy a temporary bound to a 9038 reference, insert it in the statement-tree now. */ 9039 if (cleanups) 9040 { 9041 for (tree t : *cleanups) 9042 { 9043 push_cleanup (NULL_TREE, t, false); 9044 /* As in initialize_local_var. */ 9045 wrap_temporary_cleanups (init, t); 9046 } 9047 release_tree_vector (cleanups); 9048 } 9049 9050 if (was_readonly) 9051 TREE_READONLY (decl) = 1; 9052 9053 if (flag_openmp 9054 && VAR_P (decl) 9055 && lookup_attribute ("omp declare target implicit", 9056 DECL_ATTRIBUTES (decl))) 9057 { 9058 DECL_ATTRIBUTES (decl) 9059 = remove_attribute ("omp declare target implicit", 9060 DECL_ATTRIBUTES (decl)); 9061 complete_type (TREE_TYPE (decl)); 9062 if (!omp_mappable_type (TREE_TYPE (decl))) 9063 { 9064 error ("%q+D in declare target directive does not have mappable" 9065 " type", decl); 9066 if (TREE_TYPE (decl) != error_mark_node 9067 && !COMPLETE_TYPE_P (TREE_TYPE (decl))) 9068 cxx_incomplete_type_inform (TREE_TYPE (decl)); 9069 } 9070 else if (!lookup_attribute ("omp declare target", 9071 DECL_ATTRIBUTES (decl)) 9072 && !lookup_attribute ("omp declare target link", 9073 DECL_ATTRIBUTES (decl))) 9074 { 9075 DECL_ATTRIBUTES (decl) 9076 = tree_cons (get_identifier ("omp declare target"), 9077 NULL_TREE, DECL_ATTRIBUTES (decl)); 9078 symtab_node *node = symtab_node::get (decl); 9079 if (node != NULL) 9080 { 9081 node->offloadable = 1; 9082 if (ENABLE_OFFLOADING) 9083 { 9084 g->have_offload = true; 9085 if (is_a <varpool_node *> (node)) 9086 vec_safe_push (offload_vars, decl); 9087 } 9088 } 9089 } 9090 } 9091 9092 /* This is the last point we can lower alignment so give the target the 9093 chance to do so. */ 9094 if (VAR_P (decl) 9095 && !is_global_var (decl) 9096 && !DECL_HARD_REGISTER (decl)) 9097 targetm.lower_local_decl_alignment (decl); 9098 9099 invoke_plugin_callbacks (PLUGIN_FINISH_DECL, decl); 9100 } 9101 9102 /* For class TYPE return itself or some its bases that contain 9103 any direct non-static data members. Return error_mark_node if an 9104 error has been diagnosed. */ 9105 9106 static tree 9107 find_decomp_class_base (location_t loc, tree type, tree ret) 9108 { 9109 bool member_seen = false; 9110 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field)) 9111 if (TREE_CODE (field) != FIELD_DECL 9112 || DECL_ARTIFICIAL (field) 9113 || DECL_UNNAMED_BIT_FIELD (field)) 9114 continue; 9115 else if (ret) 9116 return type; 9117 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field))) 9118 { 9119 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE) 9120 error_at (loc, "cannot decompose class type %qT because it has an " 9121 "anonymous struct member", type); 9122 else 9123 error_at (loc, "cannot decompose class type %qT because it has an " 9124 "anonymous union member", type); 9125 inform (DECL_SOURCE_LOCATION (field), "declared here"); 9126 return error_mark_node; 9127 } 9128 else if (!accessible_p (type, field, true)) 9129 { 9130 error_at (loc, "cannot decompose inaccessible member %qD of %qT", 9131 field, type); 9132 inform (DECL_SOURCE_LOCATION (field), 9133 TREE_PRIVATE (field) 9134 ? G_("declared private here") 9135 : G_("declared protected here")); 9136 return error_mark_node; 9137 } 9138 else 9139 member_seen = true; 9140 9141 tree base_binfo, binfo; 9142 tree orig_ret = ret; 9143 int i; 9144 if (member_seen) 9145 ret = type; 9146 for (binfo = TYPE_BINFO (type), i = 0; 9147 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++) 9148 { 9149 tree t = find_decomp_class_base (loc, TREE_TYPE (base_binfo), ret); 9150 if (t == error_mark_node) 9151 return error_mark_node; 9152 if (t != NULL_TREE && t != ret) 9153 { 9154 if (ret == type) 9155 { 9156 error_at (loc, "cannot decompose class type %qT: both it and " 9157 "its base class %qT have non-static data members", 9158 type, t); 9159 return error_mark_node; 9160 } 9161 else if (orig_ret != NULL_TREE) 9162 return t; 9163 else if (ret != NULL_TREE) 9164 { 9165 error_at (loc, "cannot decompose class type %qT: its base " 9166 "classes %qT and %qT have non-static data " 9167 "members", type, ret, t); 9168 return error_mark_node; 9169 } 9170 else 9171 ret = t; 9172 } 9173 } 9174 return ret; 9175 } 9176 9177 /* Return std::tuple_size<TYPE>::value. */ 9178 9179 static tree 9180 get_tuple_size (tree type) 9181 { 9182 tree args = make_tree_vec (1); 9183 TREE_VEC_ELT (args, 0) = type; 9184 tree inst = lookup_template_class (tuple_size_identifier, args, 9185 /*in_decl*/NULL_TREE, 9186 /*context*/std_node, 9187 /*entering_scope*/false, tf_none); 9188 inst = complete_type (inst); 9189 if (inst == error_mark_node 9190 || !COMPLETE_TYPE_P (inst) 9191 || !CLASS_TYPE_P (type)) 9192 return NULL_TREE; 9193 tree val = lookup_qualified_name (inst, value_identifier, 9194 LOOK_want::NORMAL, /*complain*/false); 9195 if (val == error_mark_node) 9196 return NULL_TREE; 9197 if (VAR_P (val) || TREE_CODE (val) == CONST_DECL) 9198 val = maybe_constant_value (val); 9199 if (TREE_CODE (val) == INTEGER_CST) 9200 return val; 9201 else 9202 return error_mark_node; 9203 } 9204 9205 /* Return std::tuple_element<I,TYPE>::type. */ 9206 9207 static tree 9208 get_tuple_element_type (tree type, unsigned i) 9209 { 9210 tree args = make_tree_vec (2); 9211 TREE_VEC_ELT (args, 0) = build_int_cst (integer_type_node, i); 9212 TREE_VEC_ELT (args, 1) = type; 9213 tree inst = lookup_template_class (tuple_element_identifier, args, 9214 /*in_decl*/NULL_TREE, 9215 /*context*/std_node, 9216 /*entering_scope*/false, 9217 tf_warning_or_error); 9218 return make_typename_type (inst, type_identifier, 9219 none_type, tf_warning_or_error); 9220 } 9221 9222 /* Return e.get<i>() or get<i>(e). */ 9223 9224 static tree 9225 get_tuple_decomp_init (tree decl, unsigned i) 9226 { 9227 tree targs = make_tree_vec (1); 9228 TREE_VEC_ELT (targs, 0) = build_int_cst (integer_type_node, i); 9229 9230 tree etype = TREE_TYPE (decl); 9231 tree e = convert_from_reference (decl); 9232 9233 /* [The id-expression] e is an lvalue if the type of the entity e is an 9234 lvalue reference and an xvalue otherwise. */ 9235 if (!TYPE_REF_P (etype) 9236 || TYPE_REF_IS_RVALUE (etype)) 9237 e = move (e); 9238 9239 tree fns = lookup_qualified_name (TREE_TYPE (e), get__identifier, 9240 LOOK_want::NORMAL, /*complain*/false); 9241 bool use_member_get = false; 9242 9243 /* To use a member get, member lookup must find at least one 9244 declaration that is a function template 9245 whose first template parameter is a non-type parameter. */ 9246 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (fns)); iter; ++iter) 9247 { 9248 tree fn = *iter; 9249 if (TREE_CODE (fn) == TEMPLATE_DECL) 9250 { 9251 tree tparms = DECL_TEMPLATE_PARMS (fn); 9252 tree parm = TREE_VEC_ELT (INNERMOST_TEMPLATE_PARMS (tparms), 0); 9253 if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL) 9254 { 9255 use_member_get = true; 9256 break; 9257 } 9258 } 9259 } 9260 9261 if (use_member_get) 9262 { 9263 fns = lookup_template_function (fns, targs); 9264 return build_new_method_call (e, fns, /*args*/NULL, 9265 /*path*/NULL_TREE, LOOKUP_NORMAL, 9266 /*fn_p*/NULL, tf_warning_or_error); 9267 } 9268 else 9269 { 9270 releasing_vec args (make_tree_vector_single (e)); 9271 fns = lookup_template_function (get__identifier, targs); 9272 fns = perform_koenig_lookup (fns, args, tf_warning_or_error); 9273 return finish_call_expr (fns, &args, /*novirt*/false, 9274 /*koenig*/true, tf_warning_or_error); 9275 } 9276 } 9277 9278 /* It's impossible to recover the decltype of a tuple decomposition variable 9279 based on the actual type of the variable, so store it in a hash table. */ 9280 9281 static GTY((cache)) decl_tree_cache_map *decomp_type_table; 9282 9283 tree 9284 lookup_decomp_type (tree v) 9285 { 9286 if (decomp_type_table) 9287 if (tree *slot = decomp_type_table->get (v)) 9288 return *slot; 9289 return NULL_TREE; 9290 } 9291 9292 /* Mangle a decomposition declaration if needed. Arguments like 9293 in cp_finish_decomp. */ 9294 9295 static void 9296 cp_maybe_mangle_decomp (tree decl, cp_decomp *decomp) 9297 { 9298 if (!processing_template_decl 9299 && !error_operand_p (decl) 9300 && TREE_STATIC (decl)) 9301 { 9302 auto_vec<tree, 16> v; 9303 v.safe_grow (decomp->count, true); 9304 tree d = decomp->decl; 9305 for (unsigned int i = 0; i < decomp->count; i++, d = DECL_CHAIN (d)) 9306 v[decomp->count - i - 1] = d; 9307 if (DECL_FUNCTION_SCOPE_P (decl)) 9308 { 9309 size_t sz = 3; 9310 for (unsigned int i = 0; i < decomp->count; ++i) 9311 sz += IDENTIFIER_LENGTH (DECL_NAME (v[i])) + 1; 9312 char *name = XALLOCAVEC (char, sz); 9313 name[0] = 'D'; 9314 name[1] = 'C'; 9315 char *p = name + 2; 9316 for (unsigned int i = 0; i < decomp->count; ++i) 9317 { 9318 size_t len = IDENTIFIER_LENGTH (DECL_NAME (v[i])); 9319 *p++ = ' '; 9320 memcpy (p, IDENTIFIER_POINTER (DECL_NAME (v[i])), len); 9321 p += len; 9322 } 9323 *p = '\0'; 9324 determine_local_discriminator (decl, get_identifier (name)); 9325 } 9326 SET_DECL_ASSEMBLER_NAME (decl, mangle_decomp (decl, v)); 9327 maybe_apply_pragma_weak (decl); 9328 } 9329 } 9330 9331 /* Finish a decomposition declaration. DECL is the underlying declaration 9332 "e", FIRST is the head of a chain of decls for the individual identifiers 9333 chained through DECL_CHAIN in reverse order and COUNT is the number of 9334 those decls. */ 9335 9336 void 9337 cp_finish_decomp (tree decl, cp_decomp *decomp) 9338 { 9339 tree first = decomp->decl; 9340 unsigned count = decomp->count; 9341 if (error_operand_p (decl)) 9342 { 9343 error_out: 9344 while (count--) 9345 { 9346 TREE_TYPE (first) = error_mark_node; 9347 if (DECL_HAS_VALUE_EXPR_P (first)) 9348 { 9349 SET_DECL_VALUE_EXPR (first, NULL_TREE); 9350 DECL_HAS_VALUE_EXPR_P (first) = 0; 9351 } 9352 first = DECL_CHAIN (first); 9353 } 9354 if (DECL_P (decl) && DECL_NAMESPACE_SCOPE_P (decl)) 9355 SET_DECL_ASSEMBLER_NAME (decl, get_identifier ("<decomp>")); 9356 return; 9357 } 9358 9359 location_t loc = DECL_SOURCE_LOCATION (decl); 9360 if (type_dependent_expression_p (decl) 9361 /* This happens for range for when not in templates. 9362 Still add the DECL_VALUE_EXPRs for later processing. */ 9363 || (!processing_template_decl 9364 && type_uses_auto (TREE_TYPE (decl)))) 9365 { 9366 for (unsigned int i = 0; i < count; i++) 9367 { 9368 if (!DECL_HAS_VALUE_EXPR_P (first)) 9369 { 9370 tree v = build_nt (ARRAY_REF, decl, 9371 size_int (count - i - 1), 9372 NULL_TREE, NULL_TREE); 9373 SET_DECL_VALUE_EXPR (first, v); 9374 DECL_HAS_VALUE_EXPR_P (first) = 1; 9375 } 9376 if (processing_template_decl) 9377 fit_decomposition_lang_decl (first, decl); 9378 first = DECL_CHAIN (first); 9379 } 9380 return; 9381 } 9382 9383 auto_vec<tree, 16> v; 9384 v.safe_grow (count, true); 9385 tree d = first; 9386 for (unsigned int i = 0; i < count; i++, d = DECL_CHAIN (d)) 9387 { 9388 v[count - i - 1] = d; 9389 fit_decomposition_lang_decl (d, decl); 9390 } 9391 9392 tree type = TREE_TYPE (decl); 9393 tree dexp = decl; 9394 9395 if (TYPE_REF_P (type)) 9396 { 9397 dexp = convert_from_reference (dexp); 9398 type = complete_type (TREE_TYPE (type)); 9399 if (type == error_mark_node) 9400 goto error_out; 9401 if (!COMPLETE_TYPE_P (type)) 9402 { 9403 error_at (loc, "structured binding refers to incomplete type %qT", 9404 type); 9405 goto error_out; 9406 } 9407 } 9408 9409 tree eltype = NULL_TREE; 9410 unsigned HOST_WIDE_INT eltscnt = 0; 9411 if (TREE_CODE (type) == ARRAY_TYPE) 9412 { 9413 tree nelts; 9414 nelts = array_type_nelts_top (type); 9415 if (nelts == error_mark_node) 9416 goto error_out; 9417 if (!tree_fits_uhwi_p (nelts)) 9418 { 9419 error_at (loc, "cannot decompose variable length array %qT", type); 9420 goto error_out; 9421 } 9422 eltscnt = tree_to_uhwi (nelts); 9423 if (count != eltscnt) 9424 { 9425 cnt_mismatch: 9426 if (count > eltscnt) 9427 error_n (loc, count, 9428 "%u name provided for structured binding", 9429 "%u names provided for structured binding", count); 9430 else 9431 error_n (loc, count, 9432 "only %u name provided for structured binding", 9433 "only %u names provided for structured binding", count); 9434 inform_n (loc, eltscnt, 9435 "while %qT decomposes into %wu element", 9436 "while %qT decomposes into %wu elements", 9437 type, eltscnt); 9438 goto error_out; 9439 } 9440 eltype = TREE_TYPE (type); 9441 for (unsigned int i = 0; i < count; i++) 9442 { 9443 TREE_TYPE (v[i]) = eltype; 9444 layout_decl (v[i], 0); 9445 if (processing_template_decl) 9446 continue; 9447 tree t = unshare_expr (dexp); 9448 t = build4 (ARRAY_REF, eltype, t, size_int (i), NULL_TREE, NULL_TREE); 9449 SET_DECL_VALUE_EXPR (v[i], t); 9450 DECL_HAS_VALUE_EXPR_P (v[i]) = 1; 9451 } 9452 } 9453 /* 2 GNU extensions. */ 9454 else if (TREE_CODE (type) == COMPLEX_TYPE) 9455 { 9456 eltscnt = 2; 9457 if (count != eltscnt) 9458 goto cnt_mismatch; 9459 eltype = cp_build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type)); 9460 for (unsigned int i = 0; i < count; i++) 9461 { 9462 TREE_TYPE (v[i]) = eltype; 9463 layout_decl (v[i], 0); 9464 if (processing_template_decl) 9465 continue; 9466 tree t = unshare_expr (dexp); 9467 t = build1 (i ? IMAGPART_EXPR : REALPART_EXPR, eltype, t); 9468 SET_DECL_VALUE_EXPR (v[i], t); 9469 DECL_HAS_VALUE_EXPR_P (v[i]) = 1; 9470 } 9471 } 9472 else if (TREE_CODE (type) == VECTOR_TYPE) 9473 { 9474 if (!TYPE_VECTOR_SUBPARTS (type).is_constant (&eltscnt)) 9475 { 9476 error_at (loc, "cannot decompose variable length vector %qT", type); 9477 goto error_out; 9478 } 9479 if (count != eltscnt) 9480 goto cnt_mismatch; 9481 eltype = cp_build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type)); 9482 for (unsigned int i = 0; i < count; i++) 9483 { 9484 TREE_TYPE (v[i]) = eltype; 9485 layout_decl (v[i], 0); 9486 if (processing_template_decl) 9487 continue; 9488 tree t = unshare_expr (dexp); 9489 convert_vector_to_array_for_subscript (DECL_SOURCE_LOCATION (v[i]), 9490 &t, size_int (i)); 9491 t = build4 (ARRAY_REF, eltype, t, size_int (i), NULL_TREE, NULL_TREE); 9492 SET_DECL_VALUE_EXPR (v[i], t); 9493 DECL_HAS_VALUE_EXPR_P (v[i]) = 1; 9494 } 9495 } 9496 else if (tree tsize = get_tuple_size (type)) 9497 { 9498 if (tsize == error_mark_node) 9499 { 9500 error_at (loc, "%<std::tuple_size<%T>::value%> is not an integral " 9501 "constant expression", type); 9502 goto error_out; 9503 } 9504 if (!tree_fits_uhwi_p (tsize)) 9505 { 9506 error_n (loc, count, 9507 "%u name provided for structured binding", 9508 "%u names provided for structured binding", count); 9509 inform (loc, "while %qT decomposes into %E elements", 9510 type, tsize); 9511 goto error_out; 9512 } 9513 eltscnt = tree_to_uhwi (tsize); 9514 if (count != eltscnt) 9515 goto cnt_mismatch; 9516 int save_read = DECL_READ_P (decl); 9517 for (unsigned i = 0; i < count; ++i) 9518 { 9519 location_t sloc = input_location; 9520 location_t dloc = DECL_SOURCE_LOCATION (v[i]); 9521 9522 input_location = dloc; 9523 tree init = get_tuple_decomp_init (decl, i); 9524 tree eltype = (init == error_mark_node ? error_mark_node 9525 : get_tuple_element_type (type, i)); 9526 input_location = sloc; 9527 9528 if (VOID_TYPE_P (eltype)) 9529 { 9530 error ("%<std::tuple_element<%u, %T>::type%> is %<void%>", 9531 i, type); 9532 eltype = error_mark_node; 9533 } 9534 if (init == error_mark_node || eltype == error_mark_node) 9535 { 9536 inform (dloc, "in initialization of structured binding " 9537 "variable %qD", v[i]); 9538 goto error_out; 9539 } 9540 /* Save the decltype away before reference collapse. */ 9541 hash_map_safe_put<hm_ggc> (decomp_type_table, v[i], eltype); 9542 eltype = cp_build_reference_type (eltype, !lvalue_p (init)); 9543 TREE_TYPE (v[i]) = eltype; 9544 layout_decl (v[i], 0); 9545 if (DECL_HAS_VALUE_EXPR_P (v[i])) 9546 { 9547 /* In this case the names are variables, not just proxies. */ 9548 SET_DECL_VALUE_EXPR (v[i], NULL_TREE); 9549 DECL_HAS_VALUE_EXPR_P (v[i]) = 0; 9550 } 9551 if (!processing_template_decl) 9552 { 9553 copy_linkage (v[i], decl); 9554 cp_finish_decl (v[i], init, /*constexpr*/false, 9555 /*asm*/NULL_TREE, LOOKUP_NORMAL); 9556 } 9557 } 9558 /* Ignore reads from the underlying decl performed during initialization 9559 of the individual variables. If those will be read, we'll mark 9560 the underlying decl as read at that point. */ 9561 DECL_READ_P (decl) = save_read; 9562 } 9563 else if (TREE_CODE (type) == UNION_TYPE) 9564 { 9565 error_at (loc, "cannot decompose union type %qT", type); 9566 goto error_out; 9567 } 9568 else if (!CLASS_TYPE_P (type)) 9569 { 9570 error_at (loc, "cannot decompose non-array non-class type %qT", type); 9571 goto error_out; 9572 } 9573 else if (LAMBDA_TYPE_P (type)) 9574 { 9575 error_at (loc, "cannot decompose lambda closure type %qT", type); 9576 goto error_out; 9577 } 9578 else if (processing_template_decl && complete_type (type) == error_mark_node) 9579 goto error_out; 9580 else if (processing_template_decl && !COMPLETE_TYPE_P (type)) 9581 pedwarn (loc, 0, "structured binding refers to incomplete class type %qT", 9582 type); 9583 else 9584 { 9585 tree btype = find_decomp_class_base (loc, type, NULL_TREE); 9586 if (btype == error_mark_node) 9587 goto error_out; 9588 else if (btype == NULL_TREE) 9589 { 9590 error_at (loc, "cannot decompose class type %qT without non-static " 9591 "data members", type); 9592 goto error_out; 9593 } 9594 for (tree field = TYPE_FIELDS (btype); field; field = TREE_CHAIN (field)) 9595 if (TREE_CODE (field) != FIELD_DECL 9596 || DECL_ARTIFICIAL (field) 9597 || DECL_UNNAMED_BIT_FIELD (field)) 9598 continue; 9599 else 9600 eltscnt++; 9601 if (count != eltscnt) 9602 goto cnt_mismatch; 9603 tree t = dexp; 9604 if (type != btype) 9605 { 9606 t = convert_to_base (t, btype, /*check_access*/true, 9607 /*nonnull*/false, tf_warning_or_error); 9608 type = btype; 9609 } 9610 unsigned int i = 0; 9611 for (tree field = TYPE_FIELDS (btype); field; field = TREE_CHAIN (field)) 9612 if (TREE_CODE (field) != FIELD_DECL 9613 || DECL_ARTIFICIAL (field) 9614 || DECL_UNNAMED_BIT_FIELD (field)) 9615 continue; 9616 else 9617 { 9618 tree tt = finish_non_static_data_member (field, unshare_expr (t), 9619 NULL_TREE); 9620 if (REFERENCE_REF_P (tt)) 9621 tt = TREE_OPERAND (tt, 0); 9622 TREE_TYPE (v[i]) = TREE_TYPE (tt); 9623 layout_decl (v[i], 0); 9624 if (!processing_template_decl) 9625 { 9626 SET_DECL_VALUE_EXPR (v[i], tt); 9627 DECL_HAS_VALUE_EXPR_P (v[i]) = 1; 9628 } 9629 i++; 9630 } 9631 } 9632 if (processing_template_decl) 9633 { 9634 for (unsigned int i = 0; i < count; i++) 9635 if (!DECL_HAS_VALUE_EXPR_P (v[i])) 9636 { 9637 tree a = build_nt (ARRAY_REF, decl, size_int (i), 9638 NULL_TREE, NULL_TREE); 9639 SET_DECL_VALUE_EXPR (v[i], a); 9640 DECL_HAS_VALUE_EXPR_P (v[i]) = 1; 9641 } 9642 } 9643 } 9644 9645 /* Returns a declaration for a VAR_DECL as if: 9646 9647 extern "C" TYPE NAME; 9648 9649 had been seen. Used to create compiler-generated global 9650 variables. */ 9651 9652 static tree 9653 declare_global_var (tree name, tree type) 9654 { 9655 auto cookie = push_abi_namespace (global_namespace); 9656 tree decl = build_decl (input_location, VAR_DECL, name, type); 9657 TREE_PUBLIC (decl) = 1; 9658 DECL_EXTERNAL (decl) = 1; 9659 DECL_ARTIFICIAL (decl) = 1; 9660 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace); 9661 /* If the user has explicitly declared this variable (perhaps 9662 because the code we are compiling is part of a low-level runtime 9663 library), then it is possible that our declaration will be merged 9664 with theirs by pushdecl. */ 9665 decl = pushdecl (decl); 9666 cp_finish_decl (decl, NULL_TREE, false, NULL_TREE, 0); 9667 pop_abi_namespace (cookie, global_namespace); 9668 9669 return decl; 9670 } 9671 9672 /* Returns the type for the argument to "atexit" corresponding to the function 9673 to be called when the program exits. */ 9674 9675 static tree 9676 get_atexit_fn_ptr_type () 9677 { 9678 if (!atexit_fn_ptr_type_node) 9679 { 9680 tree fn_type = build_function_type_list (void_type_node, NULL_TREE); 9681 atexit_fn_ptr_type_node = build_pointer_type (fn_type); 9682 } 9683 9684 return atexit_fn_ptr_type_node; 9685 } 9686 9687 /* Returns the type for the argument to "__cxa_atexit", "__cxa_thread_atexit" 9688 or "__cxa_throw" corresponding to the destructor to be called when the 9689 program exits. */ 9690 9691 tree 9692 get_cxa_atexit_fn_ptr_type () 9693 { 9694 if (!cleanup_type) 9695 { 9696 tree fntype = build_function_type_list (void_type_node, 9697 ptr_type_node, NULL_TREE); 9698 fntype = targetm.cxx.adjust_cdtor_callabi_fntype (fntype); 9699 cleanup_type = build_pointer_type (fntype); 9700 } 9701 9702 return cleanup_type; 9703 } 9704 9705 /* Returns a pointer to the `atexit' function. Note that if 9706 FLAG_USE_CXA_ATEXIT is nonzero, then this will actually be the new 9707 `__cxa_atexit' function specified in the IA64 C++ ABI. */ 9708 9709 static tree 9710 get_atexit_node (void) 9711 { 9712 tree atexit_fndecl; 9713 tree fn_type; 9714 tree fn_ptr_type; 9715 const char *name; 9716 bool use_aeabi_atexit; 9717 tree ctx = global_namespace; 9718 9719 if (atexit_node) 9720 return atexit_node; 9721 9722 if (flag_use_cxa_atexit && !targetm.cxx.use_atexit_for_cxa_atexit ()) 9723 { 9724 /* The declaration for `__cxa_atexit' is: 9725 9726 int __cxa_atexit (void (*)(void *), void *, void *) 9727 9728 We build up the argument types and then the function type 9729 itself. */ 9730 tree argtype0, argtype1, argtype2; 9731 9732 use_aeabi_atexit = targetm.cxx.use_aeabi_atexit (); 9733 /* First, build the pointer-to-function type for the first 9734 argument. */ 9735 fn_ptr_type = get_cxa_atexit_fn_ptr_type (); 9736 /* Then, build the rest of the argument types. */ 9737 argtype2 = ptr_type_node; 9738 if (use_aeabi_atexit) 9739 { 9740 argtype1 = fn_ptr_type; 9741 argtype0 = ptr_type_node; 9742 } 9743 else 9744 { 9745 argtype1 = ptr_type_node; 9746 argtype0 = fn_ptr_type; 9747 } 9748 /* And the final __cxa_atexit type. */ 9749 fn_type = build_function_type_list (integer_type_node, 9750 argtype0, argtype1, argtype2, 9751 NULL_TREE); 9752 /* ... which needs noexcept. */ 9753 fn_type = build_exception_variant (fn_type, noexcept_true_spec); 9754 if (use_aeabi_atexit) 9755 { 9756 name = "__aeabi_atexit"; 9757 push_to_top_level (); 9758 int n = push_namespace (get_identifier ("__aeabiv1"), false); 9759 ctx = current_namespace; 9760 while (n--) 9761 pop_namespace (); 9762 pop_from_top_level (); 9763 } 9764 else 9765 { 9766 name = "__cxa_atexit"; 9767 ctx = abi_node; 9768 } 9769 } 9770 else 9771 { 9772 /* The declaration for `atexit' is: 9773 9774 int atexit (void (*)()); 9775 9776 We build up the argument types and then the function type 9777 itself. */ 9778 fn_ptr_type = get_atexit_fn_ptr_type (); 9779 /* Build the final atexit type. */ 9780 fn_type = build_function_type_list (integer_type_node, 9781 fn_ptr_type, NULL_TREE); 9782 /* ... which needs noexcept. */ 9783 fn_type = build_exception_variant (fn_type, noexcept_true_spec); 9784 name = "atexit"; 9785 } 9786 9787 /* Now, build the function declaration. */ 9788 push_lang_context (lang_name_c); 9789 auto cookie = push_abi_namespace (ctx); 9790 atexit_fndecl = build_library_fn_ptr (name, fn_type, ECF_LEAF | ECF_NOTHROW); 9791 DECL_CONTEXT (atexit_fndecl) = FROB_CONTEXT (current_namespace); 9792 /* Install as hidden builtin so we're (a) more relaxed about 9793 exception spec matching and (b) will not give a confusing location 9794 in diagnostic and (c) won't magically appear in user-visible name 9795 lookups. */ 9796 DECL_SOURCE_LOCATION (atexit_fndecl) = BUILTINS_LOCATION; 9797 atexit_fndecl = pushdecl (atexit_fndecl, /*hiding=*/true); 9798 pop_abi_namespace (cookie, ctx); 9799 mark_used (atexit_fndecl); 9800 pop_lang_context (); 9801 atexit_node = decay_conversion (atexit_fndecl, tf_warning_or_error); 9802 9803 return atexit_node; 9804 } 9805 9806 /* Like get_atexit_node, but for thread-local cleanups. */ 9807 9808 static tree 9809 get_thread_atexit_node (void) 9810 { 9811 if (thread_atexit_node) 9812 return thread_atexit_node; 9813 9814 /* The declaration for `__cxa_thread_atexit' is: 9815 9816 int __cxa_thread_atexit (void (*)(void *), void *, void *) */ 9817 tree fn_type = build_function_type_list (integer_type_node, 9818 get_cxa_atexit_fn_ptr_type (), 9819 ptr_type_node, ptr_type_node, 9820 NULL_TREE); 9821 9822 /* Now, build the function declaration, as with __cxa_atexit. */ 9823 unsigned flags = push_abi_namespace (); 9824 tree atexit_fndecl = build_library_fn_ptr ("__cxa_thread_atexit", fn_type, 9825 ECF_LEAF | ECF_NOTHROW); 9826 DECL_CONTEXT (atexit_fndecl) = FROB_CONTEXT (current_namespace); 9827 DECL_SOURCE_LOCATION (atexit_fndecl) = BUILTINS_LOCATION; 9828 atexit_fndecl = pushdecl (atexit_fndecl, /*hiding=*/true); 9829 pop_abi_namespace (flags); 9830 mark_used (atexit_fndecl); 9831 thread_atexit_node = decay_conversion (atexit_fndecl, tf_warning_or_error); 9832 9833 return thread_atexit_node; 9834 } 9835 9836 /* Returns the __dso_handle VAR_DECL. */ 9837 9838 static tree 9839 get_dso_handle_node (void) 9840 { 9841 if (dso_handle_node) 9842 return dso_handle_node; 9843 9844 /* Declare the variable. */ 9845 dso_handle_node = declare_global_var (get_identifier ("__dso_handle"), 9846 ptr_type_node); 9847 9848 #ifdef HAVE_GAS_HIDDEN 9849 if (dso_handle_node != error_mark_node) 9850 { 9851 DECL_VISIBILITY (dso_handle_node) = VISIBILITY_HIDDEN; 9852 DECL_VISIBILITY_SPECIFIED (dso_handle_node) = 1; 9853 } 9854 #endif 9855 9856 return dso_handle_node; 9857 } 9858 9859 /* Begin a new function with internal linkage whose job will be simply 9860 to destroy some particular variable. OB_PARM is true if object pointer 9861 is passed to the cleanup function, otherwise no argument is passed. */ 9862 9863 static GTY(()) int start_cleanup_cnt; 9864 9865 static tree 9866 start_cleanup_fn (bool ob_parm) 9867 { 9868 char name[32]; 9869 9870 push_to_top_level (); 9871 9872 /* No need to mangle this. */ 9873 push_lang_context (lang_name_c); 9874 9875 /* Build the name of the function. */ 9876 sprintf (name, "__tcf_%d", start_cleanup_cnt++); 9877 tree fntype = TREE_TYPE (ob_parm ? get_cxa_atexit_fn_ptr_type () 9878 : get_atexit_fn_ptr_type ()); 9879 /* Build the function declaration. */ 9880 tree fndecl = build_lang_decl (FUNCTION_DECL, get_identifier (name), fntype); 9881 DECL_CONTEXT (fndecl) = FROB_CONTEXT (current_namespace); 9882 /* It's a function with internal linkage, generated by the 9883 compiler. */ 9884 TREE_PUBLIC (fndecl) = 0; 9885 DECL_ARTIFICIAL (fndecl) = 1; 9886 /* Make the function `inline' so that it is only emitted if it is 9887 actually needed. It is unlikely that it will be inlined, since 9888 it is only called via a function pointer, but we avoid unnecessary 9889 emissions this way. */ 9890 DECL_DECLARED_INLINE_P (fndecl) = 1; 9891 DECL_INTERFACE_KNOWN (fndecl) = 1; 9892 if (ob_parm) 9893 { 9894 /* Build the parameter. */ 9895 tree parmdecl = cp_build_parm_decl (fndecl, NULL_TREE, ptr_type_node); 9896 TREE_USED (parmdecl) = 1; 9897 DECL_READ_P (parmdecl) = 1; 9898 DECL_ARGUMENTS (fndecl) = parmdecl; 9899 } 9900 9901 fndecl = pushdecl (fndecl, /*hidden=*/true); 9902 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED); 9903 9904 pop_lang_context (); 9905 9906 return current_function_decl; 9907 } 9908 9909 /* Finish the cleanup function begun by start_cleanup_fn. */ 9910 9911 static void 9912 end_cleanup_fn (void) 9913 { 9914 expand_or_defer_fn (finish_function (/*inline_p=*/false)); 9915 9916 pop_from_top_level (); 9917 } 9918 9919 /* Generate code to handle the destruction of DECL, an object with 9920 static storage duration. */ 9921 9922 tree 9923 register_dtor_fn (tree decl) 9924 { 9925 tree cleanup; 9926 tree addr; 9927 tree compound_stmt; 9928 tree fcall; 9929 tree type; 9930 bool ob_parm, dso_parm, use_dtor; 9931 tree arg0, arg1, arg2; 9932 tree atex_node; 9933 9934 type = TREE_TYPE (decl); 9935 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type)) 9936 return void_node; 9937 9938 if (decl_maybe_constant_destruction (decl, type) 9939 && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl)) 9940 { 9941 cxx_maybe_build_cleanup (decl, tf_warning_or_error); 9942 return void_node; 9943 } 9944 9945 /* If we're using "__cxa_atexit" (or "__cxa_thread_atexit" or 9946 "__aeabi_atexit"), and DECL is a class object, we can just pass the 9947 destructor to "__cxa_atexit"; we don't have to build a temporary 9948 function to do the cleanup. */ 9949 dso_parm = (flag_use_cxa_atexit 9950 && !targetm.cxx.use_atexit_for_cxa_atexit ()); 9951 ob_parm = (CP_DECL_THREAD_LOCAL_P (decl) || dso_parm); 9952 use_dtor = ob_parm && CLASS_TYPE_P (type); 9953 if (use_dtor) 9954 { 9955 cleanup = get_class_binding (type, complete_dtor_identifier); 9956 9957 /* Make sure it is accessible. */ 9958 perform_or_defer_access_check (TYPE_BINFO (type), cleanup, cleanup, 9959 tf_warning_or_error); 9960 } 9961 else 9962 { 9963 /* Call build_cleanup before we enter the anonymous function so 9964 that any access checks will be done relative to the current 9965 scope, rather than the scope of the anonymous function. */ 9966 build_cleanup (decl); 9967 9968 /* Now start the function. */ 9969 cleanup = start_cleanup_fn (ob_parm); 9970 9971 /* Now, recompute the cleanup. It may contain SAVE_EXPRs that refer 9972 to the original function, rather than the anonymous one. That 9973 will make the back end think that nested functions are in use, 9974 which causes confusion. */ 9975 push_deferring_access_checks (dk_no_check); 9976 fcall = build_cleanup (decl); 9977 pop_deferring_access_checks (); 9978 9979 /* Create the body of the anonymous function. */ 9980 compound_stmt = begin_compound_stmt (BCS_FN_BODY); 9981 finish_expr_stmt (fcall); 9982 finish_compound_stmt (compound_stmt); 9983 end_cleanup_fn (); 9984 } 9985 9986 /* Call atexit with the cleanup function. */ 9987 mark_used (cleanup); 9988 cleanup = build_address (cleanup); 9989 9990 if (CP_DECL_THREAD_LOCAL_P (decl)) 9991 atex_node = get_thread_atexit_node (); 9992 else 9993 atex_node = get_atexit_node (); 9994 9995 if (use_dtor) 9996 { 9997 /* We must convert CLEANUP to the type that "__cxa_atexit" 9998 expects. */ 9999 cleanup = build_nop (get_cxa_atexit_fn_ptr_type (), cleanup); 10000 /* "__cxa_atexit" will pass the address of DECL to the 10001 cleanup function. */ 10002 mark_used (decl); 10003 addr = build_address (decl); 10004 /* The declared type of the parameter to "__cxa_atexit" is 10005 "void *". For plain "T*", we could just let the 10006 machinery in cp_build_function_call convert it -- but if the 10007 type is "cv-qualified T *", then we need to convert it 10008 before passing it in, to avoid spurious errors. */ 10009 addr = build_nop (ptr_type_node, addr); 10010 } 10011 else 10012 /* Since the cleanup functions we build ignore the address 10013 they're given, there's no reason to pass the actual address 10014 in, and, in general, it's cheaper to pass NULL than any 10015 other value. */ 10016 addr = null_pointer_node; 10017 10018 if (dso_parm) 10019 arg2 = cp_build_addr_expr (get_dso_handle_node (), 10020 tf_warning_or_error); 10021 else if (ob_parm) 10022 /* Just pass NULL to the dso handle parm if we don't actually 10023 have a DSO handle on this target. */ 10024 arg2 = null_pointer_node; 10025 else 10026 arg2 = NULL_TREE; 10027 10028 if (ob_parm) 10029 { 10030 if (!CP_DECL_THREAD_LOCAL_P (decl) 10031 && targetm.cxx.use_aeabi_atexit ()) 10032 { 10033 arg1 = cleanup; 10034 arg0 = addr; 10035 } 10036 else 10037 { 10038 arg1 = addr; 10039 arg0 = cleanup; 10040 } 10041 } 10042 else 10043 { 10044 arg0 = cleanup; 10045 arg1 = NULL_TREE; 10046 } 10047 return cp_build_function_call_nary (atex_node, tf_warning_or_error, 10048 arg0, arg1, arg2, NULL_TREE); 10049 } 10050 10051 /* DECL is a VAR_DECL with static storage duration. INIT, if present, 10052 is its initializer. Generate code to handle the construction 10053 and destruction of DECL. */ 10054 10055 static void 10056 expand_static_init (tree decl, tree init) 10057 { 10058 gcc_assert (VAR_P (decl)); 10059 gcc_assert (TREE_STATIC (decl)); 10060 10061 /* Some variables require no dynamic initialization. */ 10062 if (decl_maybe_constant_destruction (decl, TREE_TYPE (decl))) 10063 { 10064 /* Make sure the destructor is callable. */ 10065 cxx_maybe_build_cleanup (decl, tf_warning_or_error); 10066 if (!init) 10067 return; 10068 } 10069 10070 if (CP_DECL_THREAD_LOCAL_P (decl) && DECL_GNU_TLS_P (decl) 10071 && !DECL_FUNCTION_SCOPE_P (decl)) 10072 { 10073 location_t dloc = DECL_SOURCE_LOCATION (decl); 10074 if (init) 10075 error_at (dloc, "non-local variable %qD declared %<__thread%> " 10076 "needs dynamic initialization", decl); 10077 else 10078 error_at (dloc, "non-local variable %qD declared %<__thread%> " 10079 "has a non-trivial destructor", decl); 10080 static bool informed; 10081 if (!informed) 10082 { 10083 inform (dloc, "C++11 %<thread_local%> allows dynamic " 10084 "initialization and destruction"); 10085 informed = true; 10086 } 10087 return; 10088 } 10089 10090 if (DECL_FUNCTION_SCOPE_P (decl)) 10091 { 10092 /* Emit code to perform this initialization but once. */ 10093 tree if_stmt = NULL_TREE, inner_if_stmt = NULL_TREE; 10094 tree then_clause = NULL_TREE, inner_then_clause = NULL_TREE; 10095 tree guard, guard_addr; 10096 tree flag, begin; 10097 /* We don't need thread-safety code for thread-local vars. */ 10098 bool thread_guard = (flag_threadsafe_statics 10099 && !CP_DECL_THREAD_LOCAL_P (decl)); 10100 10101 /* Emit code to perform this initialization but once. This code 10102 looks like: 10103 10104 static <type> guard; 10105 if (!__atomic_load (guard.first_byte)) { 10106 if (__cxa_guard_acquire (&guard)) { 10107 bool flag = false; 10108 try { 10109 // Do initialization. 10110 flag = true; __cxa_guard_release (&guard); 10111 // Register variable for destruction at end of program. 10112 } catch { 10113 if (!flag) __cxa_guard_abort (&guard); 10114 } 10115 } 10116 } 10117 10118 Note that the `flag' variable is only set to 1 *after* the 10119 initialization is complete. This ensures that an exception, 10120 thrown during the construction, will cause the variable to 10121 reinitialized when we pass through this code again, as per: 10122 10123 [stmt.dcl] 10124 10125 If the initialization exits by throwing an exception, the 10126 initialization is not complete, so it will be tried again 10127 the next time control enters the declaration. 10128 10129 This process should be thread-safe, too; multiple threads 10130 should not be able to initialize the variable more than 10131 once. */ 10132 10133 /* Create the guard variable. */ 10134 guard = get_guard (decl); 10135 10136 /* Begin the conditional initialization. */ 10137 if_stmt = begin_if_stmt (); 10138 10139 finish_if_stmt_cond (get_guard_cond (guard, thread_guard), if_stmt); 10140 then_clause = begin_compound_stmt (BCS_NO_SCOPE); 10141 10142 if (thread_guard) 10143 { 10144 tree vfntype = NULL_TREE; 10145 tree acquire_name, release_name, abort_name; 10146 tree acquire_fn, release_fn, abort_fn; 10147 guard_addr = build_address (guard); 10148 10149 acquire_name = get_identifier ("__cxa_guard_acquire"); 10150 release_name = get_identifier ("__cxa_guard_release"); 10151 abort_name = get_identifier ("__cxa_guard_abort"); 10152 acquire_fn = get_global_binding (acquire_name); 10153 release_fn = get_global_binding (release_name); 10154 abort_fn = get_global_binding (abort_name); 10155 if (!acquire_fn) 10156 acquire_fn = push_library_fn 10157 (acquire_name, build_function_type_list (integer_type_node, 10158 TREE_TYPE (guard_addr), 10159 NULL_TREE), 10160 NULL_TREE, ECF_NOTHROW); 10161 if (!release_fn || !abort_fn) 10162 vfntype = build_function_type_list (void_type_node, 10163 TREE_TYPE (guard_addr), 10164 NULL_TREE); 10165 if (!release_fn) 10166 release_fn = push_library_fn (release_name, vfntype, NULL_TREE, 10167 ECF_NOTHROW); 10168 if (!abort_fn) 10169 abort_fn = push_library_fn (abort_name, vfntype, NULL_TREE, 10170 ECF_NOTHROW | ECF_LEAF); 10171 10172 inner_if_stmt = begin_if_stmt (); 10173 finish_if_stmt_cond (build_call_n (acquire_fn, 1, guard_addr), 10174 inner_if_stmt); 10175 10176 inner_then_clause = begin_compound_stmt (BCS_NO_SCOPE); 10177 begin = get_target_expr (boolean_false_node); 10178 flag = TARGET_EXPR_SLOT (begin); 10179 10180 TARGET_EXPR_CLEANUP (begin) 10181 = build3 (COND_EXPR, void_type_node, flag, 10182 void_node, 10183 build_call_n (abort_fn, 1, guard_addr)); 10184 CLEANUP_EH_ONLY (begin) = 1; 10185 10186 /* Do the initialization itself. */ 10187 init = add_stmt_to_compound (begin, init); 10188 init = add_stmt_to_compound (init, 10189 build2 (MODIFY_EXPR, void_type_node, 10190 flag, boolean_true_node)); 10191 10192 /* Use atexit to register a function for destroying this static 10193 variable. Do this before calling __cxa_guard_release. */ 10194 init = add_stmt_to_compound (init, register_dtor_fn (decl)); 10195 10196 init = add_stmt_to_compound (init, build_call_n (release_fn, 1, 10197 guard_addr)); 10198 } 10199 else 10200 { 10201 init = add_stmt_to_compound (init, set_guard (guard)); 10202 10203 /* Use atexit to register a function for destroying this static 10204 variable. */ 10205 init = add_stmt_to_compound (init, register_dtor_fn (decl)); 10206 } 10207 10208 finish_expr_stmt (init); 10209 10210 if (thread_guard) 10211 { 10212 finish_compound_stmt (inner_then_clause); 10213 finish_then_clause (inner_if_stmt); 10214 finish_if_stmt (inner_if_stmt); 10215 } 10216 10217 finish_compound_stmt (then_clause); 10218 finish_then_clause (if_stmt); 10219 finish_if_stmt (if_stmt); 10220 } 10221 else if (CP_DECL_THREAD_LOCAL_P (decl)) 10222 tls_aggregates = tree_cons (init, decl, tls_aggregates); 10223 else 10224 static_aggregates = tree_cons (init, decl, static_aggregates); 10225 } 10226 10227 10228 /* Make TYPE a complete type based on INITIAL_VALUE. 10230 Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered, 10231 2 if there was no information (in which case assume 0 if DO_DEFAULT), 10232 3 if the initializer list is empty (in pedantic mode). */ 10233 10234 int 10235 cp_complete_array_type (tree *ptype, tree initial_value, bool do_default) 10236 { 10237 int failure; 10238 tree type, elt_type; 10239 10240 /* Don't get confused by a CONSTRUCTOR for some other type. */ 10241 if (initial_value && TREE_CODE (initial_value) == CONSTRUCTOR 10242 && !BRACE_ENCLOSED_INITIALIZER_P (initial_value) 10243 && TREE_CODE (TREE_TYPE (initial_value)) != ARRAY_TYPE) 10244 return 1; 10245 10246 if (initial_value) 10247 { 10248 /* An array of character type can be initialized from a 10249 brace-enclosed string constant so call reshape_init to 10250 remove the optional braces from a braced string literal. */ 10251 if (char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (*ptype))) 10252 && BRACE_ENCLOSED_INITIALIZER_P (initial_value)) 10253 initial_value = reshape_init (*ptype, initial_value, 10254 tf_warning_or_error); 10255 10256 /* If any of the elements are parameter packs, we can't actually 10257 complete this type now because the array size is dependent. */ 10258 if (TREE_CODE (initial_value) == CONSTRUCTOR) 10259 for (auto &e: CONSTRUCTOR_ELTS (initial_value)) 10260 if (PACK_EXPANSION_P (e.value)) 10261 return 0; 10262 } 10263 10264 failure = complete_array_type (ptype, initial_value, do_default); 10265 10266 /* We can create the array before the element type is complete, which 10267 means that we didn't have these two bits set in the original type 10268 either. In completing the type, we are expected to propagate these 10269 bits. See also complete_type which does the same thing for arrays 10270 of fixed size. */ 10271 type = *ptype; 10272 if (type != error_mark_node && TYPE_DOMAIN (type)) 10273 { 10274 elt_type = TREE_TYPE (type); 10275 TYPE_NEEDS_CONSTRUCTING (type) = TYPE_NEEDS_CONSTRUCTING (elt_type); 10276 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type) 10277 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (elt_type); 10278 } 10279 10280 return failure; 10281 } 10282 10283 /* As above, but either give an error or reject zero-size arrays, depending 10284 on COMPLAIN. */ 10285 10286 int 10287 cp_complete_array_type_or_error (tree *ptype, tree initial_value, 10288 bool do_default, tsubst_flags_t complain) 10289 { 10290 int failure; 10291 bool sfinae = !(complain & tf_error); 10292 /* In SFINAE context we can't be lenient about zero-size arrays. */ 10293 if (sfinae) 10294 ++pedantic; 10295 failure = cp_complete_array_type (ptype, initial_value, do_default); 10296 if (sfinae) 10297 --pedantic; 10298 if (failure) 10299 { 10300 if (sfinae) 10301 /* Not an error. */; 10302 else if (failure == 1) 10303 error ("initializer fails to determine size of %qT", *ptype); 10304 else if (failure == 2) 10305 { 10306 if (do_default) 10307 error ("array size missing in %qT", *ptype); 10308 } 10309 else if (failure == 3) 10310 error ("zero-size array %qT", *ptype); 10311 *ptype = error_mark_node; 10312 } 10313 return failure; 10314 } 10315 10316 /* Return zero if something is declared to be a member of type 10318 CTYPE when in the context of CUR_TYPE. STRING is the error 10319 message to print in that case. Otherwise, quietly return 1. */ 10320 10321 static int 10322 member_function_or_else (tree ctype, tree cur_type, enum overload_flags flags) 10323 { 10324 if (ctype && ctype != cur_type) 10325 { 10326 if (flags == DTOR_FLAG) 10327 error ("destructor for alien class %qT cannot be a member", ctype); 10328 else 10329 error ("constructor for alien class %qT cannot be a member", ctype); 10330 return 0; 10331 } 10332 return 1; 10333 } 10334 10335 /* Subroutine of `grokdeclarator'. */ 10337 10338 /* Generate errors possibly applicable for a given set of specifiers. 10339 This is for ARM $7.1.2. */ 10340 10341 static void 10342 bad_specifiers (tree object, 10343 enum bad_spec_place type, 10344 int virtualp, 10345 int quals, 10346 int inlinep, 10347 int friendp, 10348 int raises, 10349 const location_t* locations) 10350 { 10351 switch (type) 10352 { 10353 case BSP_VAR: 10354 if (virtualp) 10355 error_at (locations[ds_virtual], 10356 "%qD declared as a %<virtual%> variable", object); 10357 if (quals) 10358 error ("%<const%> and %<volatile%> function specifiers on " 10359 "%qD invalid in variable declaration", object); 10360 break; 10361 case BSP_PARM: 10362 if (virtualp) 10363 error_at (locations[ds_virtual], 10364 "%qD declared as a %<virtual%> parameter", object); 10365 if (inlinep) 10366 error_at (locations[ds_inline], 10367 "%qD declared as an %<inline%> parameter", object); 10368 if (quals) 10369 error ("%<const%> and %<volatile%> function specifiers on " 10370 "%qD invalid in parameter declaration", object); 10371 break; 10372 case BSP_TYPE: 10373 if (virtualp) 10374 error_at (locations[ds_virtual], 10375 "%qD declared as a %<virtual%> type", object); 10376 if (inlinep) 10377 error_at (locations[ds_inline], 10378 "%qD declared as an %<inline%> type", object); 10379 if (quals) 10380 error ("%<const%> and %<volatile%> function specifiers on " 10381 "%qD invalid in type declaration", object); 10382 break; 10383 case BSP_FIELD: 10384 if (virtualp) 10385 error_at (locations[ds_virtual], 10386 "%qD declared as a %<virtual%> field", object); 10387 if (inlinep) 10388 error_at (locations[ds_inline], 10389 "%qD declared as an %<inline%> field", object); 10390 if (quals) 10391 error ("%<const%> and %<volatile%> function specifiers on " 10392 "%qD invalid in field declaration", object); 10393 break; 10394 default: 10395 gcc_unreachable(); 10396 } 10397 if (friendp) 10398 error ("%q+D declared as a friend", object); 10399 if (raises 10400 && !flag_noexcept_type 10401 && (TREE_CODE (object) == TYPE_DECL 10402 || (!TYPE_PTRFN_P (TREE_TYPE (object)) 10403 && !TYPE_REFFN_P (TREE_TYPE (object)) 10404 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (object))))) 10405 error ("%q+D declared with an exception specification", object); 10406 } 10407 10408 /* DECL is a member function or static data member and is presently 10409 being defined. Check that the definition is taking place in a 10410 valid namespace. */ 10411 10412 static void 10413 check_class_member_definition_namespace (tree decl) 10414 { 10415 /* These checks only apply to member functions and static data 10416 members. */ 10417 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl)); 10418 /* We check for problems with specializations in pt.cc in 10419 check_specialization_namespace, where we can issue better 10420 diagnostics. */ 10421 if (processing_specialization) 10422 return; 10423 /* We check this in check_explicit_instantiation_namespace. */ 10424 if (processing_explicit_instantiation) 10425 return; 10426 /* [class.mfct] 10427 10428 A member function definition that appears outside of the 10429 class definition shall appear in a namespace scope enclosing 10430 the class definition. 10431 10432 [class.static.data] 10433 10434 The definition for a static data member shall appear in a 10435 namespace scope enclosing the member's class definition. */ 10436 if (!is_ancestor (current_namespace, DECL_CONTEXT (decl))) 10437 permerror (input_location, "definition of %qD is not in namespace enclosing %qT", 10438 decl, DECL_CONTEXT (decl)); 10439 } 10440 10441 /* Build a PARM_DECL for the "this" parameter of FN. TYPE is the 10442 METHOD_TYPE for a non-static member function; QUALS are the 10443 cv-qualifiers that apply to the function. */ 10444 10445 tree 10446 build_this_parm (tree fn, tree type, cp_cv_quals quals) 10447 { 10448 tree this_type; 10449 tree qual_type; 10450 tree parm; 10451 cp_cv_quals this_quals; 10452 10453 if (CLASS_TYPE_P (type)) 10454 { 10455 this_type 10456 = cp_build_qualified_type (type, quals & ~TYPE_QUAL_RESTRICT); 10457 this_type = build_pointer_type (this_type); 10458 } 10459 else 10460 this_type = type_of_this_parm (type); 10461 /* The `this' parameter is implicitly `const'; it cannot be 10462 assigned to. */ 10463 this_quals = (quals & TYPE_QUAL_RESTRICT) | TYPE_QUAL_CONST; 10464 qual_type = cp_build_qualified_type (this_type, this_quals); 10465 parm = build_artificial_parm (fn, this_identifier, qual_type); 10466 cp_apply_type_quals_to_decl (this_quals, parm); 10467 return parm; 10468 } 10469 10470 /* DECL is a static member function. Complain if it was declared 10471 with function-cv-quals. */ 10472 10473 static void 10474 check_static_quals (tree decl, cp_cv_quals quals) 10475 { 10476 if (quals != TYPE_UNQUALIFIED) 10477 error ("static member function %q#D declared with type qualifiers", 10478 decl); 10479 } 10480 10481 // Check that FN takes no arguments and returns bool. 10482 static void 10483 check_concept_fn (tree fn) 10484 { 10485 // A constraint is nullary. 10486 if (DECL_ARGUMENTS (fn)) 10487 error_at (DECL_SOURCE_LOCATION (fn), 10488 "concept %q#D declared with function parameters", fn); 10489 10490 // The declared return type of the concept shall be bool, and 10491 // it shall not be deduced from it definition. 10492 tree type = TREE_TYPE (TREE_TYPE (fn)); 10493 if (is_auto (type)) 10494 error_at (DECL_SOURCE_LOCATION (fn), 10495 "concept %q#D declared with a deduced return type", fn); 10496 else if (type != boolean_type_node) 10497 error_at (DECL_SOURCE_LOCATION (fn), 10498 "concept %q#D with non-%<bool%> return type %qT", fn, type); 10499 } 10500 10501 /* Helper function. Replace the temporary this parameter injected 10502 during cp_finish_omp_declare_simd with the real this parameter. */ 10503 10504 static tree 10505 declare_simd_adjust_this (tree *tp, int *walk_subtrees, void *data) 10506 { 10507 tree this_parm = (tree) data; 10508 if (TREE_CODE (*tp) == PARM_DECL 10509 && DECL_NAME (*tp) == this_identifier 10510 && *tp != this_parm) 10511 *tp = this_parm; 10512 else if (TYPE_P (*tp)) 10513 *walk_subtrees = 0; 10514 return NULL_TREE; 10515 } 10516 10517 /* CTYPE is class type, or null if non-class. 10518 TYPE is type this FUNCTION_DECL should have, either FUNCTION_TYPE 10519 or METHOD_TYPE. 10520 DECLARATOR is the function's name. 10521 PARMS is a chain of PARM_DECLs for the function. 10522 VIRTUALP is truthvalue of whether the function is virtual or not. 10523 FLAGS are to be passed through to `grokclassfn'. 10524 QUALS are qualifiers indicating whether the function is `const' 10525 or `volatile'. 10526 RAISES is a list of exceptions that this function can raise. 10527 CHECK is 1 if we must find this method in CTYPE, 0 if we should 10528 not look, and -1 if we should not call `grokclassfn' at all. 10529 10530 SFK is the kind of special function (if any) for the new function. 10531 10532 Returns `NULL_TREE' if something goes wrong, after issuing 10533 applicable error messages. */ 10534 10535 static tree 10536 grokfndecl (tree ctype, 10537 tree type, 10538 tree declarator, 10539 tree parms, 10540 tree orig_declarator, 10541 const cp_decl_specifier_seq *declspecs, 10542 tree decl_reqs, 10543 int virtualp, 10544 enum overload_flags flags, 10545 cp_cv_quals quals, 10546 cp_ref_qualifier rqual, 10547 tree raises, 10548 int check, 10549 int friendp, 10550 int publicp, 10551 int inlinep, 10552 bool deletedp, 10553 bool xobj_func_p, 10554 special_function_kind sfk, 10555 bool funcdef_flag, 10556 bool late_return_type_p, 10557 int template_count, 10558 tree in_namespace, 10559 tree* attrlist, 10560 location_t location) 10561 { 10562 tree decl; 10563 tree t; 10564 10565 if (location == UNKNOWN_LOCATION) 10566 location = input_location; 10567 10568 /* Was the concept specifier present? */ 10569 bool concept_p = inlinep & 4; 10570 10571 /* Concept declarations must have a corresponding definition. */ 10572 if (concept_p && !funcdef_flag) 10573 { 10574 error_at (location, "concept %qD has no definition", declarator); 10575 return NULL_TREE; 10576 } 10577 10578 type = build_cp_fntype_variant (type, rqual, raises, late_return_type_p); 10579 10580 decl = build_lang_decl_loc (location, FUNCTION_DECL, declarator, type); 10581 10582 /* Set the constraints on the declaration. */ 10583 if (flag_concepts) 10584 { 10585 tree tmpl_reqs = NULL_TREE; 10586 tree ctx = friendp ? current_class_type : ctype; 10587 bool block_local = TREE_CODE (current_scope ()) == FUNCTION_DECL; 10588 bool memtmpl = (!block_local 10589 && (current_template_depth 10590 > template_class_depth (ctx))); 10591 if (memtmpl) 10592 { 10593 if (!current_template_parms) 10594 /* If there are no template parameters, something must have 10595 gone wrong. */ 10596 gcc_assert (seen_error ()); 10597 else 10598 tmpl_reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms); 10599 } 10600 tree ci = build_constraints (tmpl_reqs, decl_reqs); 10601 if (concept_p && ci) 10602 { 10603 error_at (location, "a function concept cannot be constrained"); 10604 ci = NULL_TREE; 10605 } 10606 /* C++20 CA378: Remove non-templated constrained functions. */ 10607 /* [temp.friend]/9 A non-template friend declaration with a 10608 requires-clause shall be a definition. A friend function template with 10609 a constraint that depends on a template parameter from an enclosing 10610 template shall be a definition. */ 10611 if (ci 10612 && (block_local 10613 || (!flag_concepts_ts 10614 && (!processing_template_decl 10615 || (friendp && !memtmpl && !funcdef_flag))))) 10616 { 10617 if (!friendp || !processing_template_decl) 10618 error_at (location, "constraints on a non-templated function"); 10619 else 10620 error_at (location, "constrained non-template friend declaration" 10621 " must be a definition"); 10622 ci = NULL_TREE; 10623 } 10624 set_constraints (decl, ci); 10625 if (ci && friendp && memtmpl && !funcdef_flag 10626 && uses_outer_template_parms_in_constraints (decl, ctx)) 10627 error_at (location, "friend function template with constraints that " 10628 "depend on outer template parameters must be a definition"); 10629 } 10630 10631 if (TREE_CODE (type) == METHOD_TYPE) 10632 { 10633 tree parm = build_this_parm (decl, type, quals); 10634 DECL_CHAIN (parm) = parms; 10635 parms = parm; 10636 10637 /* Allocate space to hold the vptr bit if needed. */ 10638 SET_DECL_ALIGN (decl, MINIMUM_METHOD_BOUNDARY); 10639 } 10640 10641 DECL_ARGUMENTS (decl) = parms; 10642 for (t = parms; t; t = DECL_CHAIN (t)) 10643 DECL_CONTEXT (t) = decl; 10644 10645 /* Propagate volatile out from type to decl. */ 10646 if (TYPE_VOLATILE (type)) 10647 TREE_THIS_VOLATILE (decl) = 1; 10648 10649 /* Setup decl according to sfk. */ 10650 switch (sfk) 10651 { 10652 case sfk_constructor: 10653 case sfk_copy_constructor: 10654 case sfk_move_constructor: 10655 DECL_CXX_CONSTRUCTOR_P (decl) = 1; 10656 DECL_NAME (decl) = ctor_identifier; 10657 break; 10658 case sfk_destructor: 10659 DECL_CXX_DESTRUCTOR_P (decl) = 1; 10660 DECL_NAME (decl) = dtor_identifier; 10661 break; 10662 case sfk_deduction_guide: 10663 /* Give deduction guides a definition even though they don't really 10664 have one: the restriction that you can't repeat a deduction guide 10665 makes them more like a definition anyway. */ 10666 DECL_INITIAL (decl) = void_node; 10667 break; 10668 default: 10669 break; 10670 } 10671 10672 if (friendp && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR) 10673 { 10674 if (funcdef_flag) 10675 error_at (location, 10676 "defining explicit specialization %qD in friend declaration", 10677 orig_declarator); 10678 else 10679 { 10680 tree fns = TREE_OPERAND (orig_declarator, 0); 10681 tree args = TREE_OPERAND (orig_declarator, 1); 10682 10683 if (PROCESSING_REAL_TEMPLATE_DECL_P ()) 10684 { 10685 /* Something like `template <class T> friend void f<T>()'. */ 10686 error_at (location, 10687 "invalid use of template-id %qD in declaration " 10688 "of primary template", 10689 orig_declarator); 10690 return NULL_TREE; 10691 } 10692 10693 /* A friend declaration of the form friend void f<>(). Record 10694 the information in the TEMPLATE_ID_EXPR. */ 10695 SET_DECL_IMPLICIT_INSTANTIATION (decl); 10696 10697 gcc_assert (identifier_p (fns) || OVL_P (fns)); 10698 DECL_TEMPLATE_INFO (decl) = build_template_info (fns, args); 10699 10700 for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); t; t = TREE_CHAIN (t)) 10701 if (TREE_PURPOSE (t) 10702 && TREE_CODE (TREE_PURPOSE (t)) == DEFERRED_PARSE) 10703 { 10704 error_at (defparse_location (TREE_PURPOSE (t)), 10705 "default arguments are not allowed in declaration " 10706 "of friend template specialization %qD", 10707 decl); 10708 return NULL_TREE; 10709 } 10710 10711 if (inlinep & 1) 10712 { 10713 error_at (declspecs->locations[ds_inline], 10714 "%<inline%> is not allowed in declaration of friend " 10715 "template specialization %qD", 10716 decl); 10717 return NULL_TREE; 10718 } 10719 } 10720 } 10721 10722 /* C++17 11.3.6/4: "If a friend declaration specifies a default argument 10723 expression, that declaration shall be a definition..." */ 10724 if (friendp && !funcdef_flag) 10725 { 10726 for (tree t = FUNCTION_FIRST_USER_PARMTYPE (decl); 10727 t && t != void_list_node; t = TREE_CHAIN (t)) 10728 if (TREE_PURPOSE (t)) 10729 { 10730 permerror (DECL_SOURCE_LOCATION (decl), 10731 "friend declaration of %qD specifies default " 10732 "arguments and isn%'t a definition", decl); 10733 break; 10734 } 10735 } 10736 10737 /* If this decl has namespace scope, set that up. */ 10738 if (in_namespace) 10739 set_decl_namespace (decl, in_namespace, friendp); 10740 else if (ctype) 10741 DECL_CONTEXT (decl) = ctype; 10742 else 10743 DECL_CONTEXT (decl) = FROB_CONTEXT (current_decl_namespace ()); 10744 10745 /* `main' and builtins have implicit 'C' linkage. */ 10746 if (ctype == NULL_TREE 10747 && DECL_FILE_SCOPE_P (decl) 10748 && current_lang_name == lang_name_cplusplus 10749 && (MAIN_NAME_P (declarator) 10750 || (IDENTIFIER_LENGTH (declarator) > 10 10751 && IDENTIFIER_POINTER (declarator)[0] == '_' 10752 && IDENTIFIER_POINTER (declarator)[1] == '_' 10753 && startswith (IDENTIFIER_POINTER (declarator) + 2, 10754 "builtin_")) 10755 || (targetcm.cxx_implicit_extern_c 10756 && (targetcm.cxx_implicit_extern_c 10757 (IDENTIFIER_POINTER (declarator)))))) 10758 SET_DECL_LANGUAGE (decl, lang_c); 10759 10760 DECL_STATIC_FUNCTION_P (decl) 10761 = !xobj_func_p && ctype && TREE_CODE (type) == FUNCTION_TYPE; 10762 DECL_FUNCTION_XOBJ_FLAG (decl) = xobj_func_p; 10763 10764 if (deletedp) 10765 DECL_DELETED_FN (decl) = 1; 10766 10767 if (ctype && funcdef_flag) 10768 check_class_member_definition_namespace (decl); 10769 10770 if (ctype == NULL_TREE && DECL_MAIN_P (decl)) 10771 { 10772 if (PROCESSING_REAL_TEMPLATE_DECL_P()) 10773 error_at (location, "cannot declare %<::main%> to be a template"); 10774 if (inlinep & 1) 10775 error_at (declspecs->locations[ds_inline], 10776 "cannot declare %<::main%> to be inline"); 10777 if (inlinep & 2) 10778 error_at (declspecs->locations[ds_constexpr], 10779 "cannot declare %<::main%> to be %qs", "constexpr"); 10780 if (inlinep & 8) 10781 error_at (declspecs->locations[ds_consteval], 10782 "cannot declare %<::main%> to be %qs", "consteval"); 10783 if (!publicp) 10784 error_at (location, "cannot declare %<::main%> to be static"); 10785 inlinep = 0; 10786 publicp = 1; 10787 } 10788 10789 /* Members of anonymous types and local classes have no linkage; make 10790 them internal. If a typedef is made later, this will be changed. */ 10791 if (ctype && !TREE_PUBLIC (TYPE_MAIN_DECL (ctype))) 10792 publicp = 0; 10793 else if (ctype && decl_function_context (TYPE_MAIN_DECL (ctype))) 10794 /* But members of local classes in a module CMI should have their 10795 definitions exported, in case they are (directly or indirectly) 10796 used by an importer. We don't just use module_has_cmi_p here 10797 because for entities in the GMF we don't yet know whether this 10798 module will have a CMI, so we'll conservatively assume it might. */ 10799 publicp = module_maybe_has_cmi_p (); 10800 10801 if (publicp && cxx_dialect == cxx98) 10802 { 10803 /* [basic.link]: A name with no linkage (notably, the name of a class 10804 or enumeration declared in a local scope) shall not be used to 10805 declare an entity with linkage. 10806 10807 DR 757 relaxes this restriction for C++0x. */ 10808 no_linkage_error (decl); 10809 } 10810 10811 TREE_PUBLIC (decl) = publicp; 10812 if (! publicp) 10813 { 10814 DECL_INTERFACE_KNOWN (decl) = 1; 10815 DECL_NOT_REALLY_EXTERN (decl) = 1; 10816 } 10817 10818 /* If the declaration was declared inline, mark it as such. */ 10819 if (inlinep) 10820 { 10821 DECL_DECLARED_INLINE_P (decl) = 1; 10822 if (publicp) 10823 DECL_COMDAT (decl) = 1; 10824 } 10825 if (inlinep & 2) 10826 DECL_DECLARED_CONSTEXPR_P (decl) = true; 10827 else if (inlinep & 8) 10828 { 10829 DECL_DECLARED_CONSTEXPR_P (decl) = true; 10830 SET_DECL_IMMEDIATE_FUNCTION_P (decl); 10831 } 10832 10833 // If the concept declaration specifier was found, check 10834 // that the declaration satisfies the necessary requirements. 10835 if (concept_p) 10836 { 10837 DECL_DECLARED_CONCEPT_P (decl) = true; 10838 check_concept_fn (decl); 10839 } 10840 10841 DECL_EXTERNAL (decl) = 1; 10842 if (TREE_CODE (type) == FUNCTION_TYPE) 10843 { 10844 if (quals || rqual) 10845 TREE_TYPE (decl) = apply_memfn_quals (TREE_TYPE (decl), 10846 TYPE_UNQUALIFIED, 10847 REF_QUAL_NONE); 10848 auto_diagnostic_group d; 10849 if (quals) 10850 error (!ctype 10851 ? G_("non-member function %qD cannot have cv-qualifier") 10852 : !xobj_func_p 10853 ? G_("static member function %qD cannot have cv-qualifier") 10854 : G_("explicit object member function " 10855 "%qD cannot have cv-qualifier"), 10856 decl); 10857 if (rqual) 10858 error (!ctype 10859 ? G_("non-member function %qD cannot have ref-qualifier") 10860 : !xobj_func_p 10861 ? G_("static member function %qD cannot have ref-qualifier") 10862 : G_("explicit object member function " 10863 "%qD cannot have ref-qualifier"), 10864 decl); 10865 10866 if (xobj_func_p && (quals || rqual)) 10867 inform (DECL_SOURCE_LOCATION (DECL_ARGUMENTS (decl)), 10868 "explicit object parameter declared here"); 10869 quals = TYPE_UNQUALIFIED; 10870 rqual = REF_QUAL_NONE; 10871 10872 } 10873 10874 if (deduction_guide_p (decl)) 10875 { 10876 tree type = TREE_TYPE (DECL_NAME (decl)); 10877 if (in_namespace == NULL_TREE 10878 && CP_DECL_CONTEXT (decl) != CP_TYPE_CONTEXT (type)) 10879 { 10880 error_at (location, "deduction guide %qD must be declared in the " 10881 "same scope as %qT", decl, type); 10882 inform (location_of (type), " declared here"); 10883 return NULL_TREE; 10884 } 10885 if (DECL_CLASS_SCOPE_P (decl) 10886 && current_access_specifier != declared_access (TYPE_NAME (type))) 10887 { 10888 error_at (location, "deduction guide %qD must have the same access " 10889 "as %qT", decl, type); 10890 inform (location_of (type), " declared here"); 10891 } 10892 if (funcdef_flag) 10893 error_at (location, 10894 "deduction guide %qD must not have a function body", decl); 10895 } 10896 else if (IDENTIFIER_ANY_OP_P (DECL_NAME (decl)) 10897 && !grok_op_properties (decl, /*complain=*/true)) 10898 return NULL_TREE; 10899 else if (UDLIT_OPER_P (DECL_NAME (decl))) 10900 { 10901 bool long_long_unsigned_p; 10902 bool long_double_p; 10903 const char *suffix = NULL; 10904 /* [over.literal]/6: Literal operators shall not have C linkage. */ 10905 if (DECL_LANGUAGE (decl) == lang_c) 10906 { 10907 error_at (location, "literal operator with C linkage"); 10908 maybe_show_extern_c_location (); 10909 return NULL_TREE; 10910 } 10911 10912 if (DECL_NAMESPACE_SCOPE_P (decl)) 10913 { 10914 if (!check_literal_operator_args (decl, &long_long_unsigned_p, 10915 &long_double_p)) 10916 { 10917 error_at (location, "%qD has invalid argument list", decl); 10918 return NULL_TREE; 10919 } 10920 10921 suffix = UDLIT_OP_SUFFIX (DECL_NAME (decl)); 10922 if (long_long_unsigned_p) 10923 { 10924 if (cpp_interpret_int_suffix (parse_in, suffix, strlen (suffix))) 10925 warning_at (location, 0, "integer suffix %qs" 10926 " shadowed by implementation", suffix); 10927 } 10928 else if (long_double_p) 10929 { 10930 if (cpp_interpret_float_suffix (parse_in, suffix, strlen (suffix))) 10931 warning_at (location, 0, "floating-point suffix %qs" 10932 " shadowed by implementation", suffix); 10933 } 10934 /* 17.6.3.3.5 */ 10935 if (suffix[0] != '_' 10936 && !current_function_decl && !(friendp && !funcdef_flag)) 10937 warning_at (location, OPT_Wliteral_suffix, 10938 "literal operator suffixes not preceded by %<_%>" 10939 " are reserved for future standardization"); 10940 } 10941 else 10942 { 10943 error_at (location, "%qD must be a non-member function", decl); 10944 return NULL_TREE; 10945 } 10946 } 10947 10948 if (funcdef_flag) 10949 /* Make the init_value nonzero so pushdecl knows this is not 10950 tentative. error_mark_node is replaced later with the BLOCK. */ 10951 DECL_INITIAL (decl) = error_mark_node; 10952 10953 if (TYPE_NOTHROW_P (type) || nothrow_libfn_p (decl)) 10954 TREE_NOTHROW (decl) = 1; 10955 10956 if (flag_openmp || flag_openmp_simd) 10957 { 10958 /* Adjust "omp declare simd" attributes. */ 10959 tree ods = lookup_attribute ("omp declare simd", *attrlist); 10960 if (ods) 10961 { 10962 tree attr; 10963 for (attr = ods; attr; 10964 attr = lookup_attribute ("omp declare simd", TREE_CHAIN (attr))) 10965 { 10966 if (TREE_CODE (type) == METHOD_TYPE) 10967 walk_tree (&TREE_VALUE (attr), declare_simd_adjust_this, 10968 DECL_ARGUMENTS (decl), NULL); 10969 if (TREE_VALUE (attr) != NULL_TREE) 10970 { 10971 tree cl = TREE_VALUE (TREE_VALUE (attr)); 10972 cl = c_omp_declare_simd_clauses_to_numbers 10973 (DECL_ARGUMENTS (decl), cl); 10974 if (cl) 10975 TREE_VALUE (TREE_VALUE (attr)) = cl; 10976 else 10977 TREE_VALUE (attr) = NULL_TREE; 10978 } 10979 } 10980 } 10981 } 10982 10983 /* Caller will do the rest of this. */ 10984 if (check < 0) 10985 return decl; 10986 10987 if (ctype != NULL_TREE) 10988 grokclassfn (ctype, decl, flags); 10989 10990 /* 12.4/3 */ 10991 if (cxx_dialect >= cxx11 10992 && DECL_DESTRUCTOR_P (decl) 10993 && !TYPE_BEING_DEFINED (DECL_CONTEXT (decl)) 10994 && !processing_template_decl) 10995 deduce_noexcept_on_destructor (decl); 10996 10997 set_originating_module (decl); 10998 10999 decl = check_explicit_specialization (orig_declarator, decl, 11000 template_count, 11001 2 * funcdef_flag + 11002 4 * (friendp != 0) + 11003 8 * concept_p, 11004 *attrlist); 11005 if (decl == error_mark_node) 11006 return NULL_TREE; 11007 11008 if (DECL_STATIC_FUNCTION_P (decl)) 11009 check_static_quals (decl, quals); 11010 11011 if (attrlist) 11012 { 11013 cplus_decl_attributes (&decl, *attrlist, 0); 11014 *attrlist = NULL_TREE; 11015 } 11016 11017 if (DECL_HAS_CONTRACTS_P (decl)) 11018 rebuild_postconditions (decl); 11019 11020 /* Check main's type after attributes have been applied. */ 11021 if (ctype == NULL_TREE && DECL_MAIN_P (decl)) 11022 { 11023 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)), 11024 integer_type_node)) 11025 { 11026 tree oldtypeargs = TYPE_ARG_TYPES (TREE_TYPE (decl)); 11027 tree newtype; 11028 error_at (declspecs->locations[ds_type_spec], 11029 "%<::main%> must return %<int%>"); 11030 newtype = build_function_type (integer_type_node, oldtypeargs); 11031 TREE_TYPE (decl) = newtype; 11032 } 11033 if (warn_main) 11034 check_main_parameter_types (decl); 11035 } 11036 11037 if (ctype != NULL_TREE && check) 11038 { 11039 tree old_decl = check_classfn (ctype, decl, 11040 (current_template_depth 11041 > template_class_depth (ctype)) 11042 ? current_template_parms 11043 : NULL_TREE); 11044 11045 if (old_decl == error_mark_node) 11046 return NULL_TREE; 11047 11048 if (old_decl) 11049 { 11050 tree ok; 11051 tree pushed_scope; 11052 11053 if (TREE_CODE (old_decl) == TEMPLATE_DECL) 11054 /* Because grokfndecl is always supposed to return a 11055 FUNCTION_DECL, we pull out the DECL_TEMPLATE_RESULT 11056 here. We depend on our callers to figure out that its 11057 really a template that's being returned. */ 11058 old_decl = DECL_TEMPLATE_RESULT (old_decl); 11059 11060 if (DECL_STATIC_FUNCTION_P (old_decl) 11061 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE) 11062 { 11063 /* Remove the `this' parm added by grokclassfn. */ 11064 revert_static_member_fn (decl); 11065 check_static_quals (decl, quals); 11066 } 11067 if (DECL_ARTIFICIAL (old_decl)) 11068 { 11069 error ("definition of implicitly-declared %qD", old_decl); 11070 return NULL_TREE; 11071 } 11072 else if (DECL_DEFAULTED_FN (old_decl)) 11073 { 11074 error ("definition of explicitly-defaulted %q+D", decl); 11075 inform (DECL_SOURCE_LOCATION (old_decl), 11076 "%q#D explicitly defaulted here", old_decl); 11077 return NULL_TREE; 11078 } 11079 11080 /* Since we've smashed OLD_DECL to its 11081 DECL_TEMPLATE_RESULT, we must do the same to DECL. */ 11082 if (TREE_CODE (decl) == TEMPLATE_DECL) 11083 decl = DECL_TEMPLATE_RESULT (decl); 11084 11085 /* Attempt to merge the declarations. This can fail, in 11086 the case of some invalid specialization declarations. */ 11087 pushed_scope = push_scope (ctype); 11088 ok = duplicate_decls (decl, old_decl); 11089 if (pushed_scope) 11090 pop_scope (pushed_scope); 11091 if (!ok) 11092 { 11093 error ("no %q#D member function declared in class %qT", 11094 decl, ctype); 11095 return NULL_TREE; 11096 } 11097 if (ok == error_mark_node) 11098 return NULL_TREE; 11099 return old_decl; 11100 } 11101 } 11102 11103 if (DECL_CONSTRUCTOR_P (decl) && !grok_ctor_properties (ctype, decl)) 11104 return NULL_TREE; 11105 11106 if (ctype == NULL_TREE || check) 11107 return decl; 11108 11109 if (virtualp) 11110 DECL_VIRTUAL_P (decl) = 1; 11111 11112 return decl; 11113 } 11114 11115 /* decl is a FUNCTION_DECL. 11116 specifiers are the parsed virt-specifiers. 11117 11118 Set flags to reflect the virt-specifiers. 11119 11120 Returns decl. */ 11121 11122 static tree 11123 set_virt_specifiers (tree decl, cp_virt_specifiers specifiers) 11124 { 11125 if (decl == NULL_TREE) 11126 return decl; 11127 if (specifiers & VIRT_SPEC_OVERRIDE) 11128 DECL_OVERRIDE_P (decl) = 1; 11129 if (specifiers & VIRT_SPEC_FINAL) 11130 DECL_FINAL_P (decl) = 1; 11131 return decl; 11132 } 11133 11134 /* DECL is a VAR_DECL for a static data member. Set flags to reflect 11135 the linkage that DECL will receive in the object file. */ 11136 11137 static void 11138 set_linkage_for_static_data_member (tree decl) 11139 { 11140 /* A static data member always has static storage duration and 11141 external linkage. Note that static data members are forbidden in 11142 local classes -- the only situation in which a class has 11143 non-external linkage. */ 11144 TREE_PUBLIC (decl) = 1; 11145 TREE_STATIC (decl) = 1; 11146 /* For non-template classes, static data members are always put 11147 out in exactly those files where they are defined, just as 11148 with ordinary namespace-scope variables. */ 11149 if (!processing_template_decl) 11150 DECL_INTERFACE_KNOWN (decl) = 1; 11151 } 11152 11153 /* Create a VAR_DECL named NAME with the indicated TYPE. 11154 11155 If SCOPE is non-NULL, it is the class type or namespace containing 11156 the variable. If SCOPE is NULL, the variable should is created in 11157 the innermost enclosing scope. */ 11158 11159 static tree 11160 grokvardecl (tree type, 11161 tree name, 11162 tree orig_declarator, 11163 const cp_decl_specifier_seq *declspecs, 11164 int initialized, 11165 int type_quals, 11166 int inlinep, 11167 bool conceptp, 11168 int template_count, 11169 tree scope, 11170 location_t location) 11171 { 11172 tree decl; 11173 tree explicit_scope; 11174 11175 gcc_assert (!name || identifier_p (name)); 11176 11177 bool constp = (type_quals & TYPE_QUAL_CONST) != 0; 11178 bool volatilep = (type_quals & TYPE_QUAL_VOLATILE) != 0; 11179 11180 /* Compute the scope in which to place the variable, but remember 11181 whether or not that scope was explicitly specified by the user. */ 11182 explicit_scope = scope; 11183 if (!scope) 11184 { 11185 /* An explicit "extern" specifier indicates a namespace-scope 11186 variable. */ 11187 if (declspecs->storage_class == sc_extern) 11188 scope = current_decl_namespace (); 11189 else if (!at_function_scope_p ()) 11190 scope = current_scope (); 11191 } 11192 11193 if (scope 11194 && (/* If the variable is a namespace-scope variable declared in a 11195 template, we need DECL_LANG_SPECIFIC. */ 11196 (TREE_CODE (scope) == NAMESPACE_DECL && processing_template_decl) 11197 /* Similarly for namespace-scope variables with language linkage 11198 other than C++. */ 11199 || (TREE_CODE (scope) == NAMESPACE_DECL 11200 && current_lang_name != lang_name_cplusplus) 11201 /* Similarly for static data members. */ 11202 || TYPE_P (scope) 11203 /* Similarly for explicit specializations. */ 11204 || (orig_declarator 11205 && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR))) 11206 decl = build_lang_decl_loc (location, VAR_DECL, name, type); 11207 else 11208 decl = build_decl (location, VAR_DECL, name, type); 11209 11210 if (explicit_scope && TREE_CODE (explicit_scope) == NAMESPACE_DECL) 11211 set_decl_namespace (decl, explicit_scope, 0); 11212 else 11213 DECL_CONTEXT (decl) = FROB_CONTEXT (scope); 11214 11215 if (declspecs->storage_class == sc_extern) 11216 { 11217 DECL_THIS_EXTERN (decl) = 1; 11218 DECL_EXTERNAL (decl) = !initialized; 11219 } 11220 11221 if (DECL_CLASS_SCOPE_P (decl)) 11222 { 11223 set_linkage_for_static_data_member (decl); 11224 /* This function is only called with out-of-class definitions. */ 11225 DECL_EXTERNAL (decl) = 0; 11226 check_class_member_definition_namespace (decl); 11227 } 11228 /* At top level, either `static' or no s.c. makes a definition 11229 (perhaps tentative), and absence of `static' makes it public. */ 11230 else if (toplevel_bindings_p ()) 11231 { 11232 TREE_PUBLIC (decl) = (declspecs->storage_class != sc_static 11233 && (DECL_THIS_EXTERN (decl) 11234 || ! constp 11235 || volatilep 11236 || inlinep 11237 || module_attach_p ())); 11238 TREE_STATIC (decl) = ! DECL_EXTERNAL (decl); 11239 } 11240 /* Not at top level, only `static' makes a static definition. */ 11241 else 11242 { 11243 TREE_STATIC (decl) = declspecs->storage_class == sc_static; 11244 TREE_PUBLIC (decl) = DECL_EXTERNAL (decl); 11245 } 11246 11247 set_originating_module (decl); 11248 11249 if (decl_spec_seq_has_spec_p (declspecs, ds_thread)) 11250 { 11251 if (DECL_EXTERNAL (decl) || TREE_STATIC (decl)) 11252 { 11253 CP_DECL_THREAD_LOCAL_P (decl) = true; 11254 if (!processing_template_decl) 11255 set_decl_tls_model (decl, decl_default_tls_model (decl)); 11256 } 11257 if (declspecs->gnu_thread_keyword_p) 11258 SET_DECL_GNU_TLS_P (decl); 11259 } 11260 11261 /* If the type of the decl has no linkage, make sure that we'll 11262 notice that in mark_used. */ 11263 if (cxx_dialect > cxx98 11264 && decl_linkage (decl) != lk_none 11265 && DECL_LANG_SPECIFIC (decl) == NULL 11266 && !DECL_EXTERN_C_P (decl) 11267 && no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false)) 11268 retrofit_lang_decl (decl); 11269 11270 if (TREE_PUBLIC (decl)) 11271 { 11272 /* [basic.link]: A name with no linkage (notably, the name of a class 11273 or enumeration declared in a local scope) shall not be used to 11274 declare an entity with linkage. 11275 11276 DR 757 relaxes this restriction for C++0x. */ 11277 if (cxx_dialect < cxx11) 11278 no_linkage_error (decl); 11279 } 11280 else 11281 DECL_INTERFACE_KNOWN (decl) = 1; 11282 11283 if (DECL_NAME (decl) 11284 && MAIN_NAME_P (DECL_NAME (decl)) 11285 && scope == global_namespace) 11286 error_at (DECL_SOURCE_LOCATION (decl), 11287 "cannot declare %<::main%> to be a global variable"); 11288 11289 /* Check that the variable can be safely declared as a concept. 11290 Note that this also forbids explicit specializations. */ 11291 if (conceptp) 11292 { 11293 if (!processing_template_decl) 11294 { 11295 error_at (declspecs->locations[ds_concept], 11296 "a non-template variable cannot be %<concept%>"); 11297 return NULL_TREE; 11298 } 11299 else if (!at_namespace_scope_p ()) 11300 { 11301 error_at (declspecs->locations[ds_concept], 11302 "concept must be defined at namespace scope"); 11303 return NULL_TREE; 11304 } 11305 else 11306 DECL_DECLARED_CONCEPT_P (decl) = true; 11307 if (!same_type_ignoring_top_level_qualifiers_p (type, boolean_type_node)) 11308 error_at (declspecs->locations[ds_type_spec], 11309 "concept must have type %<bool%>"); 11310 if (TEMPLATE_PARMS_CONSTRAINTS (current_template_parms)) 11311 { 11312 error_at (location, "a variable concept cannot be constrained"); 11313 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = NULL_TREE; 11314 } 11315 } 11316 else if (flag_concepts 11317 && current_template_depth > template_class_depth (scope)) 11318 { 11319 tree ci = current_template_constraints (); 11320 set_constraints (decl, ci); 11321 } 11322 11323 // Handle explicit specializations and instantiations of variable templates. 11324 if (orig_declarator) 11325 decl = check_explicit_specialization (orig_declarator, decl, 11326 template_count, conceptp * 8); 11327 11328 return decl != error_mark_node ? decl : NULL_TREE; 11329 } 11330 11331 /* Create and return a canonical pointer to member function type, for 11332 TYPE, which is a POINTER_TYPE to a METHOD_TYPE. */ 11333 11334 tree 11335 build_ptrmemfunc_type (tree type) 11336 { 11337 tree field, fields; 11338 tree t; 11339 11340 if (type == error_mark_node) 11341 return type; 11342 11343 /* Make sure that we always have the unqualified pointer-to-member 11344 type first. */ 11345 if (cp_cv_quals quals = cp_type_quals (type)) 11346 { 11347 tree unqual = build_ptrmemfunc_type (TYPE_MAIN_VARIANT (type)); 11348 return cp_build_qualified_type (unqual, quals); 11349 } 11350 11351 /* If a canonical type already exists for this type, use it. We use 11352 this method instead of type_hash_canon, because it only does a 11353 simple equality check on the list of field members. */ 11354 11355 t = TYPE_PTRMEMFUNC_TYPE (type); 11356 if (t) 11357 return t; 11358 11359 t = make_node (RECORD_TYPE); 11360 11361 /* Let the front end know this is a pointer to member function. */ 11362 TYPE_PTRMEMFUNC_FLAG (t) = 1; 11363 11364 field = build_decl (input_location, FIELD_DECL, pfn_identifier, type); 11365 DECL_NONADDRESSABLE_P (field) = 1; 11366 fields = field; 11367 11368 field = build_decl (input_location, FIELD_DECL, delta_identifier, 11369 delta_type_node); 11370 DECL_NONADDRESSABLE_P (field) = 1; 11371 DECL_CHAIN (field) = fields; 11372 fields = field; 11373 11374 finish_builtin_struct (t, "__ptrmemfunc_type", fields, ptr_type_node); 11375 11376 /* Zap out the name so that the back end will give us the debugging 11377 information for this anonymous RECORD_TYPE. */ 11378 TYPE_NAME (t) = NULL_TREE; 11379 11380 /* Cache this pointer-to-member type so that we can find it again 11381 later. */ 11382 TYPE_PTRMEMFUNC_TYPE (type) = t; 11383 11384 if (TYPE_STRUCTURAL_EQUALITY_P (type)) 11385 SET_TYPE_STRUCTURAL_EQUALITY (t); 11386 else if (TYPE_CANONICAL (type) != type) 11387 TYPE_CANONICAL (t) = build_ptrmemfunc_type (TYPE_CANONICAL (type)); 11388 11389 return t; 11390 } 11391 11392 /* Create and return a pointer to data member type. */ 11393 11394 tree 11395 build_ptrmem_type (tree class_type, tree member_type) 11396 { 11397 if (TREE_CODE (member_type) == METHOD_TYPE) 11398 { 11399 cp_cv_quals quals = type_memfn_quals (member_type); 11400 cp_ref_qualifier rqual = type_memfn_rqual (member_type); 11401 member_type = build_memfn_type (member_type, class_type, quals, rqual); 11402 return build_ptrmemfunc_type (build_pointer_type (member_type)); 11403 } 11404 else 11405 { 11406 gcc_assert (TREE_CODE (member_type) != FUNCTION_TYPE); 11407 return build_offset_type (class_type, member_type); 11408 } 11409 } 11410 11411 /* DECL is a VAR_DECL defined in-class, whose TYPE is also given. 11412 Check to see that the definition is valid. Issue appropriate error 11413 messages. */ 11414 11415 static void 11416 check_static_variable_definition (tree decl, tree type) 11417 { 11418 /* Avoid redundant diagnostics on out-of-class definitions. */ 11419 if (!current_class_type || !TYPE_BEING_DEFINED (current_class_type)) 11420 ; 11421 /* Can't check yet if we don't know the type. */ 11422 else if (dependent_type_p (type)) 11423 ; 11424 /* If DECL is declared constexpr, we'll do the appropriate checks 11425 in check_initializer. Similarly for inline static data members. */ 11426 else if (DECL_P (decl) 11427 && (DECL_DECLARED_CONSTEXPR_P (decl) 11428 || DECL_VAR_DECLARED_INLINE_P (decl))) 11429 ; 11430 else if (cxx_dialect >= cxx11 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)) 11431 { 11432 if (!COMPLETE_TYPE_P (type)) 11433 error_at (DECL_SOURCE_LOCATION (decl), 11434 "in-class initialization of static data member %q#D of " 11435 "incomplete type", decl); 11436 else if (literal_type_p (type)) 11437 permerror (DECL_SOURCE_LOCATION (decl), 11438 "%<constexpr%> needed for in-class initialization of " 11439 "static data member %q#D of non-integral type", decl); 11440 else 11441 error_at (DECL_SOURCE_LOCATION (decl), 11442 "in-class initialization of static data member %q#D of " 11443 "non-literal type", decl); 11444 } 11445 /* Motion 10 at San Diego: If a static const integral data member is 11446 initialized with an integral constant expression, the initializer 11447 may appear either in the declaration (within the class), or in 11448 the definition, but not both. If it appears in the class, the 11449 member is a member constant. The file-scope definition is always 11450 required. */ 11451 else if (!ARITHMETIC_TYPE_P (type) && TREE_CODE (type) != ENUMERAL_TYPE) 11452 error_at (DECL_SOURCE_LOCATION (decl), 11453 "invalid in-class initialization of static data member " 11454 "of non-integral type %qT", 11455 type); 11456 else if (!CP_TYPE_CONST_P (type)) 11457 error_at (DECL_SOURCE_LOCATION (decl), 11458 "ISO C++ forbids in-class initialization of non-const " 11459 "static member %qD", 11460 decl); 11461 else if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type)) 11462 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic, 11463 "ISO C++ forbids initialization of member constant " 11464 "%qD of non-integral type %qT", decl, type); 11465 } 11466 11467 /* *expr_p is part of the TYPE_SIZE of a variably-sized array. If any 11468 SAVE_EXPRs in *expr_p wrap expressions with side-effects, break those 11469 expressions out into temporary variables so that walk_tree doesn't 11470 step into them (c++/15764). */ 11471 11472 static tree 11473 stabilize_save_expr_r (tree *expr_p, int *walk_subtrees, void *data) 11474 { 11475 hash_set<tree> *pset = (hash_set<tree> *)data; 11476 tree expr = *expr_p; 11477 if (TREE_CODE (expr) == SAVE_EXPR) 11478 { 11479 tree op = TREE_OPERAND (expr, 0); 11480 cp_walk_tree (&op, stabilize_save_expr_r, data, pset); 11481 if (TREE_SIDE_EFFECTS (op)) 11482 TREE_OPERAND (expr, 0) = get_temp_regvar (TREE_TYPE (op), op); 11483 *walk_subtrees = 0; 11484 } 11485 else if (!EXPR_P (expr) || !TREE_SIDE_EFFECTS (expr)) 11486 *walk_subtrees = 0; 11487 return NULL; 11488 } 11489 11490 /* Entry point for the above. */ 11491 11492 static void 11493 stabilize_vla_size (tree size) 11494 { 11495 hash_set<tree> pset; 11496 /* Break out any function calls into temporary variables. */ 11497 cp_walk_tree (&size, stabilize_save_expr_r, &pset, &pset); 11498 } 11499 11500 /* Reduce a SIZEOF_EXPR to its value. */ 11501 11502 tree 11503 fold_sizeof_expr (tree t) 11504 { 11505 tree r; 11506 if (SIZEOF_EXPR_TYPE_P (t)) 11507 r = cxx_sizeof_or_alignof_type (EXPR_LOCATION (t), 11508 TREE_TYPE (TREE_OPERAND (t, 0)), 11509 SIZEOF_EXPR, false, false); 11510 else if (TYPE_P (TREE_OPERAND (t, 0))) 11511 r = cxx_sizeof_or_alignof_type (EXPR_LOCATION (t), 11512 TREE_OPERAND (t, 0), SIZEOF_EXPR, 11513 false, false); 11514 else 11515 r = cxx_sizeof_or_alignof_expr (EXPR_LOCATION (t), 11516 TREE_OPERAND (t, 0), SIZEOF_EXPR, 11517 false, false); 11518 if (r == error_mark_node) 11519 r = size_one_node; 11520 r = cp_fold_convert (size_type_node, r); 11521 return r; 11522 } 11523 11524 /* Given the SIZE (i.e., number of elements) in an array, compute 11525 an appropriate index type for the array. If non-NULL, NAME is 11526 the name of the entity being declared. */ 11527 11528 static tree 11529 compute_array_index_type_loc (location_t name_loc, tree name, tree size, 11530 tsubst_flags_t complain) 11531 { 11532 if (error_operand_p (size)) 11533 return error_mark_node; 11534 11535 /* The type of the index being computed. */ 11536 tree itype; 11537 11538 /* The original numeric size as seen in the source code before 11539 conversion to size_t. */ 11540 tree origsize = size; 11541 11542 location_t loc = cp_expr_loc_or_loc (size, name ? name_loc : input_location); 11543 11544 if (!type_dependent_expression_p (size)) 11545 { 11546 origsize = size = mark_rvalue_use (size); 11547 11548 if (cxx_dialect < cxx11 && TREE_CODE (size) == NOP_EXPR 11549 && TREE_SIDE_EFFECTS (size)) 11550 /* In C++98, we mark a non-constant array bound with a magic 11551 NOP_EXPR with TREE_SIDE_EFFECTS; don't fold in that case. */; 11552 else 11553 { 11554 size = build_converted_constant_expr (size_type_node, size, complain); 11555 /* Pedantically a constant expression is required here and so 11556 __builtin_is_constant_evaluated () should fold to true if it 11557 is successfully folded into a constant. */ 11558 size = fold_non_dependent_expr (size, complain, 11559 /*manifestly_const_eval=*/true); 11560 11561 if (!TREE_CONSTANT (size)) 11562 size = origsize; 11563 } 11564 11565 if (error_operand_p (size)) 11566 return error_mark_node; 11567 11568 /* The array bound must be an integer type. */ 11569 tree type = TREE_TYPE (size); 11570 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type)) 11571 { 11572 if (!(complain & tf_error)) 11573 return error_mark_node; 11574 if (name) 11575 error_at (loc, "size of array %qD has non-integral type %qT", 11576 name, type); 11577 else 11578 error_at (loc, "size of array has non-integral type %qT", type); 11579 size = integer_one_node; 11580 } 11581 } 11582 11583 /* A type is dependent if it is...an array type constructed from any 11584 dependent type or whose size is specified by a constant expression 11585 that is value-dependent. */ 11586 /* We can only call value_dependent_expression_p on integral constant 11587 expressions. */ 11588 if (processing_template_decl 11589 && potential_constant_expression (size) 11590 && value_dependent_expression_p (size)) 11591 { 11592 /* Just build the index type and mark that it requires 11593 structural equality checks. */ 11594 in_template: 11595 itype = build_index_type (build_min (MINUS_EXPR, sizetype, 11596 size, size_one_node)); 11597 TYPE_DEPENDENT_P (itype) = 1; 11598 TYPE_DEPENDENT_P_VALID (itype) = 1; 11599 SET_TYPE_STRUCTURAL_EQUALITY (itype); 11600 return itype; 11601 } 11602 11603 if (TREE_CODE (size) != INTEGER_CST) 11604 { 11605 tree folded = cp_fully_fold (size); 11606 if (TREE_CODE (folded) == INTEGER_CST) 11607 { 11608 if (name) 11609 pedwarn (loc, OPT_Wpedantic, "size of array %qD is not an " 11610 "integral constant-expression", name); 11611 else 11612 pedwarn (loc, OPT_Wpedantic, 11613 "size of array is not an integral constant-expression"); 11614 } 11615 if (TREE_CONSTANT (size) && !TREE_CONSTANT (folded)) 11616 /* We might have lost the TREE_CONSTANT flag e.g. when we are 11617 folding a conversion from a pointer to integral type. In that 11618 case issue an error below and don't treat this as a VLA. */; 11619 else 11620 /* Use the folded result for VLAs, too; it will have resolved 11621 SIZEOF_EXPR. */ 11622 size = folded; 11623 } 11624 11625 /* Normally, the array-bound will be a constant. */ 11626 if (TREE_CODE (size) == INTEGER_CST) 11627 { 11628 /* The size to use in diagnostics that reflects the constant 11629 size used in the source, rather than SIZE massaged above. */ 11630 tree diagsize = size; 11631 11632 /* If the original size before conversion to size_t was signed 11633 and negative, convert it to ssizetype to restore the sign. */ 11634 if (!TYPE_UNSIGNED (TREE_TYPE (origsize)) 11635 && TREE_CODE (size) == INTEGER_CST 11636 && tree_int_cst_sign_bit (size)) 11637 { 11638 diagsize = fold_convert (ssizetype, size); 11639 11640 /* Clear the overflow bit that may have been set as a result 11641 of the conversion from the sizetype of the new size to 11642 ssizetype. */ 11643 TREE_OVERFLOW (diagsize) = false; 11644 } 11645 11646 /* Verify that the array has a positive number of elements 11647 and issue the appropriate diagnostic if it doesn't. */ 11648 if (!valid_array_size_p (loc, diagsize, name, (complain & tf_error))) 11649 { 11650 if (!(complain & tf_error)) 11651 return error_mark_node; 11652 size = integer_one_node; 11653 } 11654 /* As an extension we allow zero-sized arrays. */ 11655 else if (integer_zerop (size)) 11656 { 11657 if (!(complain & tf_error)) 11658 /* We must fail if performing argument deduction (as 11659 indicated by the state of complain), so that 11660 another substitution can be found. */ 11661 return error_mark_node; 11662 else if (name) 11663 pedwarn (loc, OPT_Wpedantic, 11664 "ISO C++ forbids zero-size array %qD", name); 11665 else 11666 pedwarn (loc, OPT_Wpedantic, 11667 "ISO C++ forbids zero-size array"); 11668 } 11669 } 11670 else if (TREE_CONSTANT (size) 11671 /* We don't allow VLAs at non-function scopes, or during 11672 tentative template substitution. */ 11673 || !at_function_scope_p () 11674 || !(complain & tf_error)) 11675 { 11676 if (!(complain & tf_error)) 11677 return error_mark_node; 11678 /* `(int) &fn' is not a valid array bound. */ 11679 if (name) 11680 error_at (loc, 11681 "size of array %qD is not an integral constant-expression", 11682 name); 11683 else 11684 error_at (loc, "size of array is not an integral constant-expression"); 11685 size = integer_one_node; 11686 } 11687 else if (pedantic && warn_vla != 0) 11688 { 11689 if (name) 11690 pedwarn (name_loc, OPT_Wvla, 11691 "ISO C++ forbids variable length array %qD", name); 11692 else 11693 pedwarn (input_location, OPT_Wvla, 11694 "ISO C++ forbids variable length array"); 11695 } 11696 else if (warn_vla > 0) 11697 { 11698 if (name) 11699 warning_at (name_loc, OPT_Wvla, 11700 "variable length array %qD is used", name); 11701 else 11702 warning (OPT_Wvla, 11703 "variable length array is used"); 11704 } 11705 11706 if (processing_template_decl && !TREE_CONSTANT (size)) 11707 goto in_template; 11708 else 11709 { 11710 if (!TREE_CONSTANT (size)) 11711 { 11712 /* A variable sized array. Arrange for the SAVE_EXPR on the inside 11713 of the MINUS_EXPR, which allows the -1 to get folded with the +1 11714 that happens when building TYPE_SIZE. */ 11715 size = variable_size (size); 11716 stabilize_vla_size (size); 11717 } 11718 11719 /* Compute the index of the largest element in the array. It is 11720 one less than the number of elements in the array. We save 11721 and restore PROCESSING_TEMPLATE_DECL so that computations in 11722 cp_build_binary_op will be appropriately folded. */ 11723 { 11724 processing_template_decl_sentinel s; 11725 itype = cp_build_binary_op (input_location, 11726 MINUS_EXPR, 11727 cp_convert (ssizetype, size, complain), 11728 cp_convert (ssizetype, integer_one_node, 11729 complain), 11730 complain); 11731 itype = maybe_constant_value (itype, NULL_TREE, mce_true); 11732 } 11733 11734 if (!TREE_CONSTANT (itype)) 11735 { 11736 if (sanitize_flags_p (SANITIZE_VLA) 11737 && current_function_decl != NULL_TREE) 11738 { 11739 /* We have to add 1 -- in the ubsan routine we generate 11740 LE_EXPR rather than LT_EXPR. */ 11741 tree t = fold_build2 (PLUS_EXPR, TREE_TYPE (itype), itype, 11742 build_one_cst (TREE_TYPE (itype))); 11743 t = ubsan_instrument_vla (input_location, t); 11744 finish_expr_stmt (t); 11745 } 11746 } 11747 /* Make sure that there was no overflow when creating to a signed 11748 index type. (For example, on a 32-bit machine, an array with 11749 size 2^32 - 1 is too big.) */ 11750 else if (TREE_CODE (itype) == INTEGER_CST 11751 && TREE_OVERFLOW (itype)) 11752 { 11753 if (!(complain & tf_error)) 11754 return error_mark_node; 11755 error ("overflow in array dimension"); 11756 TREE_OVERFLOW (itype) = 0; 11757 } 11758 } 11759 11760 /* Create and return the appropriate index type. */ 11761 itype = build_index_type (itype); 11762 11763 /* If the index type were dependent, we would have returned early, so 11764 remember that it isn't. */ 11765 TYPE_DEPENDENT_P (itype) = 0; 11766 TYPE_DEPENDENT_P_VALID (itype) = 1; 11767 return itype; 11768 } 11769 11770 tree 11771 compute_array_index_type (tree name, tree size, tsubst_flags_t complain) 11772 { 11773 return compute_array_index_type_loc (input_location, name, size, complain); 11774 } 11775 11776 /* Returns the scope (if any) in which the entity declared by 11777 DECLARATOR will be located. If the entity was declared with an 11778 unqualified name, NULL_TREE is returned. */ 11779 11780 tree 11781 get_scope_of_declarator (const cp_declarator *declarator) 11782 { 11783 while (declarator && declarator->kind != cdk_id) 11784 declarator = declarator->declarator; 11785 11786 /* If the declarator-id is a SCOPE_REF, the scope in which the 11787 declaration occurs is the first operand. */ 11788 if (declarator 11789 && declarator->u.id.qualifying_scope) 11790 return declarator->u.id.qualifying_scope; 11791 11792 /* Otherwise, the declarator is not a qualified name; the entity will 11793 be declared in the current scope. */ 11794 return NULL_TREE; 11795 } 11796 11797 /* Returns an ARRAY_TYPE for an array with SIZE elements of the 11798 indicated TYPE. If non-NULL, NAME is the NAME of the declaration 11799 with this type. */ 11800 11801 static tree 11802 create_array_type_for_decl (tree name, tree type, tree size, location_t loc) 11803 { 11804 tree itype = NULL_TREE; 11805 11806 /* If things have already gone awry, bail now. */ 11807 if (type == error_mark_node || size == error_mark_node) 11808 return error_mark_node; 11809 11810 /* [dcl.type.class.deduct] prohibits forming an array of placeholder 11811 for a deduced class type. */ 11812 if (template_placeholder_p (type)) 11813 { 11814 if (name) 11815 error_at (loc, "%qD declared as array of template placeholder " 11816 "type %qT", name, type); 11817 else 11818 error ("creating array of template placeholder type %qT", type); 11819 return error_mark_node; 11820 } 11821 11822 /* If there are some types which cannot be array elements, 11823 issue an error-message and return. */ 11824 switch (TREE_CODE (type)) 11825 { 11826 case VOID_TYPE: 11827 if (name) 11828 error_at (loc, "declaration of %qD as array of void", name); 11829 else 11830 error ("creating array of void"); 11831 return error_mark_node; 11832 11833 case FUNCTION_TYPE: 11834 if (name) 11835 error_at (loc, "declaration of %qD as array of functions", name); 11836 else 11837 error ("creating array of functions"); 11838 return error_mark_node; 11839 11840 case REFERENCE_TYPE: 11841 if (name) 11842 error_at (loc, "declaration of %qD as array of references", name); 11843 else 11844 error ("creating array of references"); 11845 return error_mark_node; 11846 11847 case METHOD_TYPE: 11848 if (name) 11849 error_at (loc, "declaration of %qD as array of function members", 11850 name); 11851 else 11852 error ("creating array of function members"); 11853 return error_mark_node; 11854 11855 default: 11856 break; 11857 } 11858 11859 if (!verify_type_context (name ? loc : input_location, 11860 TCTX_ARRAY_ELEMENT, type)) 11861 return error_mark_node; 11862 11863 /* [dcl.array] 11864 11865 The constant expressions that specify the bounds of the arrays 11866 can be omitted only for the first member of the sequence. */ 11867 if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)) 11868 { 11869 if (name) 11870 error_at (loc, "declaration of %qD as multidimensional array must " 11871 "have bounds for all dimensions except the first", 11872 name); 11873 else 11874 error ("multidimensional array must have bounds for all " 11875 "dimensions except the first"); 11876 11877 return error_mark_node; 11878 } 11879 11880 /* Figure out the index type for the array. */ 11881 if (size) 11882 { 11883 itype = compute_array_index_type_loc (loc, name, size, 11884 tf_warning_or_error); 11885 if (type_uses_auto (type) 11886 && variably_modified_type_p (itype, /*fn=*/NULL_TREE)) 11887 { 11888 sorry_at (loc, "variable-length array of %<auto%>"); 11889 return error_mark_node; 11890 } 11891 } 11892 11893 return build_cplus_array_type (type, itype); 11894 } 11895 11896 /* Returns the smallest location that is not UNKNOWN_LOCATION. */ 11897 11898 static location_t 11899 min_location (location_t loca, location_t locb) 11900 { 11901 if (loca == UNKNOWN_LOCATION 11902 || (locb != UNKNOWN_LOCATION 11903 && linemap_location_before_p (line_table, locb, loca))) 11904 return locb; 11905 return loca; 11906 } 11907 11908 /* Returns the smallest location != UNKNOWN_LOCATION among the 11909 three stored in LOCATIONS[ds_const], LOCATIONS[ds_volatile], 11910 and LOCATIONS[ds_restrict]. */ 11911 11912 static location_t 11913 smallest_type_quals_location (int type_quals, const location_t* locations) 11914 { 11915 location_t loc = UNKNOWN_LOCATION; 11916 11917 if (type_quals & TYPE_QUAL_CONST) 11918 loc = locations[ds_const]; 11919 11920 if (type_quals & TYPE_QUAL_VOLATILE) 11921 loc = min_location (loc, locations[ds_volatile]); 11922 11923 if (type_quals & TYPE_QUAL_RESTRICT) 11924 loc = min_location (loc, locations[ds_restrict]); 11925 11926 return loc; 11927 } 11928 11929 /* Returns the smallest among the latter and locations[ds_type_spec]. */ 11930 11931 static location_t 11932 smallest_type_location (int type_quals, const location_t* locations) 11933 { 11934 location_t loc = smallest_type_quals_location (type_quals, locations); 11935 return min_location (loc, locations[ds_type_spec]); 11936 } 11937 11938 static location_t 11939 smallest_type_location (const cp_decl_specifier_seq *declspecs) 11940 { 11941 int type_quals = get_type_quals (declspecs); 11942 return smallest_type_location (type_quals, declspecs->locations); 11943 } 11944 11945 /* Check that it's OK to declare a function with the indicated TYPE 11946 and TYPE_QUALS. SFK indicates the kind of special function (if any) 11947 that this function is. OPTYPE is the type given in a conversion 11948 operator declaration, or the class type for a constructor/destructor. 11949 Returns the actual return type of the function; that may be different 11950 than TYPE if an error occurs, or for certain special functions. */ 11951 11952 static tree 11953 check_special_function_return_type (special_function_kind sfk, 11954 tree type, 11955 tree optype, 11956 int type_quals, 11957 const location_t* locations) 11958 { 11959 switch (sfk) 11960 { 11961 case sfk_constructor: 11962 if (type) 11963 error_at (smallest_type_location (type_quals, locations), 11964 "return type specification for constructor invalid"); 11965 else if (type_quals != TYPE_UNQUALIFIED) 11966 error_at (smallest_type_quals_location (type_quals, locations), 11967 "qualifiers are not allowed on constructor declaration"); 11968 11969 if (targetm.cxx.cdtor_returns_this ()) 11970 type = build_pointer_type (optype); 11971 else 11972 type = void_type_node; 11973 break; 11974 11975 case sfk_destructor: 11976 if (type) 11977 error_at (smallest_type_location (type_quals, locations), 11978 "return type specification for destructor invalid"); 11979 else if (type_quals != TYPE_UNQUALIFIED) 11980 error_at (smallest_type_quals_location (type_quals, locations), 11981 "qualifiers are not allowed on destructor declaration"); 11982 11983 /* We can't use the proper return type here because we run into 11984 problems with ambiguous bases and covariant returns. */ 11985 if (targetm.cxx.cdtor_returns_this ()) 11986 type = build_pointer_type (void_type_node); 11987 else 11988 type = void_type_node; 11989 break; 11990 11991 case sfk_conversion: 11992 if (type) 11993 error_at (smallest_type_location (type_quals, locations), 11994 "return type specified for %<operator %T%>", optype); 11995 else if (type_quals != TYPE_UNQUALIFIED) 11996 error_at (smallest_type_quals_location (type_quals, locations), 11997 "qualifiers are not allowed on declaration of " 11998 "%<operator %T%>", optype); 11999 12000 type = optype; 12001 break; 12002 12003 case sfk_deduction_guide: 12004 if (type) 12005 error_at (smallest_type_location (type_quals, locations), 12006 "return type specified for deduction guide"); 12007 else if (type_quals != TYPE_UNQUALIFIED) 12008 error_at (smallest_type_quals_location (type_quals, locations), 12009 "qualifiers are not allowed on declaration of " 12010 "deduction guide"); 12011 if (TREE_CODE (optype) == TEMPLATE_TEMPLATE_PARM) 12012 { 12013 error ("template template parameter %qT in declaration of " 12014 "deduction guide", optype); 12015 type = error_mark_node; 12016 } 12017 else 12018 type = make_template_placeholder (CLASSTYPE_TI_TEMPLATE (optype)); 12019 for (int i = 0; i < ds_last; ++i) 12020 if (i != ds_explicit && locations[i]) 12021 error_at (locations[i], 12022 "%<decl-specifier%> in declaration of deduction guide"); 12023 break; 12024 12025 default: 12026 gcc_unreachable (); 12027 } 12028 12029 return type; 12030 } 12031 12032 /* A variable or data member (whose unqualified name is IDENTIFIER) 12033 has been declared with the indicated TYPE. If the TYPE is not 12034 acceptable, issue an error message and return a type to use for 12035 error-recovery purposes. */ 12036 12037 tree 12038 check_var_type (tree identifier, tree type, location_t loc) 12039 { 12040 if (VOID_TYPE_P (type)) 12041 { 12042 if (!identifier) 12043 error_at (loc, "unnamed variable or field declared void"); 12044 else if (identifier_p (identifier)) 12045 { 12046 gcc_assert (!IDENTIFIER_ANY_OP_P (identifier)); 12047 error_at (loc, "variable or field %qE declared void", 12048 identifier); 12049 } 12050 else 12051 error_at (loc, "variable or field declared void"); 12052 type = error_mark_node; 12053 } 12054 12055 return type; 12056 } 12057 12058 /* Handle declaring DECL as an inline variable. */ 12059 12060 static void 12061 mark_inline_variable (tree decl, location_t loc) 12062 { 12063 bool inlinep = true; 12064 if (! toplevel_bindings_p ()) 12065 { 12066 error_at (loc, "%<inline%> specifier invalid for variable " 12067 "%qD declared at block scope", decl); 12068 inlinep = false; 12069 } 12070 else if (cxx_dialect < cxx17) 12071 pedwarn (loc, OPT_Wc__17_extensions, "inline variables are only available " 12072 "with %<-std=c++17%> or %<-std=gnu++17%>"); 12073 if (inlinep) 12074 { 12075 retrofit_lang_decl (decl); 12076 SET_DECL_VAR_DECLARED_INLINE_P (decl); 12077 } 12078 } 12079 12080 12081 /* Assign a typedef-given name to a class or enumeration type declared 12082 as anonymous at first. This was split out of grokdeclarator 12083 because it is also used in libcc1. */ 12084 12085 void 12086 name_unnamed_type (tree type, tree decl) 12087 { 12088 gcc_assert (TYPE_UNNAMED_P (type)); 12089 12090 /* Replace the anonymous decl with the real decl. Be careful not to 12091 rename other typedefs (such as the self-reference) of type. */ 12092 tree orig = TYPE_NAME (type); 12093 for (tree t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t)) 12094 if (TYPE_NAME (t) == orig) 12095 TYPE_NAME (t) = decl; 12096 12097 /* If this is a typedef within a template class, the nested 12098 type is a (non-primary) template. The name for the 12099 template needs updating as well. */ 12100 if (TYPE_LANG_SPECIFIC (type) && CLASSTYPE_TEMPLATE_INFO (type)) 12101 DECL_NAME (CLASSTYPE_TI_TEMPLATE (type)) = DECL_NAME (decl); 12102 12103 /* Adjust linkage now that we aren't unnamed anymore. */ 12104 reset_type_linkage (type); 12105 12106 /* FIXME remangle member functions; member functions of a 12107 type with external linkage have external linkage. */ 12108 12109 /* Check that our job is done, and that it would fail if we 12110 attempted to do it again. */ 12111 gcc_assert (!TYPE_UNNAMED_P (type)); 12112 } 12113 12114 /* Check that decltype(auto) was well-formed: only plain decltype(auto) 12115 is allowed. TYPE might contain a decltype(auto). Returns true if 12116 there was a problem, false otherwise. */ 12117 12118 static bool 12119 check_decltype_auto (location_t loc, tree type) 12120 { 12121 if (tree a = type_uses_auto (type)) 12122 { 12123 if (AUTO_IS_DECLTYPE (a)) 12124 { 12125 if (a != type) 12126 { 12127 error_at (loc, "%qT as type rather than plain " 12128 "%<decltype(auto)%>", type); 12129 return true; 12130 } 12131 else if (TYPE_QUALS (type) != TYPE_UNQUALIFIED) 12132 { 12133 error_at (loc, "%<decltype(auto)%> cannot be cv-qualified"); 12134 return true; 12135 } 12136 } 12137 } 12138 return false; 12139 } 12140 12141 /* Given declspecs and a declarator (abstract or otherwise), determine 12142 the name and type of the object declared and construct a DECL node 12143 for it. 12144 12145 DECLSPECS points to the representation of declaration-specifier 12146 sequence that precedes declarator. 12147 12148 DECL_CONTEXT says which syntactic context this declaration is in: 12149 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL. 12150 FUNCDEF for a function definition. Like NORMAL but a few different 12151 error messages in each case. Return value may be zero meaning 12152 this definition is too screwy to try to parse. 12153 MEMFUNCDEF for a function definition. Like FUNCDEF but prepares to 12154 handle member functions (which have FIELD context). 12155 Return value may be zero meaning this definition is too screwy to 12156 try to parse. 12157 PARM for a parameter declaration (either within a function prototype 12158 or before a function body). Make a PARM_DECL, or return void_type_node. 12159 TPARM for a template parameter declaration. 12160 CATCHPARM for a parameter declaration before a catch clause. 12161 TYPENAME if for a typename (in a cast or sizeof). 12162 Don't make a DECL node; just return the ..._TYPE node. 12163 FIELD for a struct or union field; make a FIELD_DECL. 12164 BITFIELD for a field with specified width. 12165 12166 INITIALIZED is as for start_decl. 12167 12168 ATTRLIST is a pointer to the list of attributes, which may be NULL 12169 if there are none; *ATTRLIST may be modified if attributes from inside 12170 the declarator should be applied to the declaration. 12171 12172 When this function is called, scoping variables (such as 12173 CURRENT_CLASS_TYPE) should reflect the scope in which the 12174 declaration occurs, not the scope in which the new declaration will 12175 be placed. For example, on: 12176 12177 void S::f() { ... } 12178 12179 when grokdeclarator is called for `S::f', the CURRENT_CLASS_TYPE 12180 should not be `S'. 12181 12182 Returns a DECL (if a declarator is present), a TYPE (if there is no 12183 declarator, in cases like "struct S;"), or the ERROR_MARK_NODE if an 12184 error occurs. */ 12185 12186 tree 12187 grokdeclarator (const cp_declarator *declarator, 12188 cp_decl_specifier_seq *declspecs, 12189 enum decl_context decl_context, 12190 int initialized, 12191 tree* attrlist) 12192 { 12193 tree type = NULL_TREE; 12194 int longlong = 0; 12195 int explicit_intN = 0; 12196 int int_n_alt = 0; 12197 int virtualp, explicitp, friendp, inlinep, staticp; 12198 int explicit_int = 0; 12199 int explicit_char = 0; 12200 int defaulted_int = 0; 12201 12202 tree typedef_decl = NULL_TREE; 12203 const char *name = NULL; 12204 tree typedef_type = NULL_TREE; 12205 /* True if this declarator is a function definition. */ 12206 bool funcdef_flag = false; 12207 cp_declarator_kind innermost_code = cdk_error; 12208 int bitfield = 0; 12209 #if 0 12210 /* See the code below that used this. */ 12211 tree decl_attr = NULL_TREE; 12212 #endif 12213 12214 /* Keep track of what sort of function is being processed 12215 so that we can warn about default return values, or explicit 12216 return values which do not match prescribed defaults. */ 12217 special_function_kind sfk = sfk_none; 12218 12219 tree dname = NULL_TREE; 12220 tree ctor_return_type = NULL_TREE; 12221 enum overload_flags flags = NO_SPECIAL; 12222 /* cv-qualifiers that apply to the declarator, for a declaration of 12223 a member function. */ 12224 cp_cv_quals memfn_quals = TYPE_UNQUALIFIED; 12225 /* virt-specifiers that apply to the declarator, for a declaration of 12226 a member function. */ 12227 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED; 12228 /* ref-qualifier that applies to the declarator, for a declaration of 12229 a member function. */ 12230 cp_ref_qualifier rqual = REF_QUAL_NONE; 12231 /* cv-qualifiers that apply to the type specified by the DECLSPECS. */ 12232 int type_quals = get_type_quals (declspecs); 12233 tree raises = NULL_TREE; 12234 int template_count = 0; 12235 tree returned_attrs = NULL_TREE; 12236 tree parms = NULL_TREE; 12237 const cp_declarator *id_declarator; 12238 /* The unqualified name of the declarator; either an 12239 IDENTIFIER_NODE, BIT_NOT_EXPR, or TEMPLATE_ID_EXPR. */ 12240 tree unqualified_id; 12241 /* The class type, if any, in which this entity is located, 12242 or NULL_TREE if none. Note that this value may be different from 12243 the current class type; for example if an attempt is made to declare 12244 "A::f" inside "B", this value will be "A". */ 12245 tree ctype = current_class_type; 12246 /* The NAMESPACE_DECL for the namespace in which this entity is 12247 located. If an unqualified name is used to declare the entity, 12248 this value will be NULL_TREE, even if the entity is located at 12249 namespace scope. */ 12250 tree in_namespace = NULL_TREE; 12251 cp_storage_class storage_class; 12252 bool unsigned_p, signed_p, short_p, long_p, thread_p; 12253 bool type_was_error_mark_node = false; 12254 bool parameter_pack_p = declarator ? declarator->parameter_pack_p : false; 12255 bool template_type_arg = false; 12256 bool template_parm_flag = false; 12257 bool typedef_p = decl_spec_seq_has_spec_p (declspecs, ds_typedef); 12258 bool constexpr_p = decl_spec_seq_has_spec_p (declspecs, ds_constexpr); 12259 bool constinit_p = decl_spec_seq_has_spec_p (declspecs, ds_constinit); 12260 bool consteval_p = decl_spec_seq_has_spec_p (declspecs, ds_consteval); 12261 bool late_return_type_p = false; 12262 bool array_parameter_p = false; 12263 tree reqs = NULL_TREE; 12264 12265 signed_p = decl_spec_seq_has_spec_p (declspecs, ds_signed); 12266 unsigned_p = decl_spec_seq_has_spec_p (declspecs, ds_unsigned); 12267 short_p = decl_spec_seq_has_spec_p (declspecs, ds_short); 12268 long_p = decl_spec_seq_has_spec_p (declspecs, ds_long); 12269 longlong = decl_spec_seq_has_spec_p (declspecs, ds_long_long); 12270 explicit_intN = declspecs->explicit_intN_p; 12271 int_n_alt = declspecs->int_n_alt; 12272 thread_p = decl_spec_seq_has_spec_p (declspecs, ds_thread); 12273 12274 // Was concept_p specified? Note that ds_concept 12275 // implies ds_constexpr! 12276 bool concept_p = decl_spec_seq_has_spec_p (declspecs, ds_concept); 12277 if (concept_p) 12278 constexpr_p = true; 12279 12280 if (decl_context == FUNCDEF) 12281 funcdef_flag = true, decl_context = NORMAL; 12282 else if (decl_context == MEMFUNCDEF) 12283 funcdef_flag = true, decl_context = FIELD; 12284 else if (decl_context == BITFIELD) 12285 bitfield = 1, decl_context = FIELD; 12286 else if (decl_context == TEMPLATE_TYPE_ARG) 12287 template_type_arg = true, decl_context = TYPENAME; 12288 else if (decl_context == TPARM) 12289 template_parm_flag = true, decl_context = PARM; 12290 12291 if (initialized == SD_DEFAULTED || initialized == SD_DELETED) 12292 funcdef_flag = true; 12293 12294 location_t typespec_loc = loc_or_input_loc (smallest_type_location 12295 (type_quals, 12296 declspecs->locations)); 12297 location_t id_loc; 12298 location_t init_loc; 12299 if (declarator) 12300 { 12301 id_loc = loc_or_input_loc (declarator->id_loc); 12302 init_loc = loc_or_input_loc (declarator->init_loc); 12303 } 12304 else 12305 init_loc = id_loc = input_location; 12306 12307 /* Look inside a declarator for the name being declared 12308 and get it as a string, for an error message. */ 12309 for (id_declarator = declarator; 12310 id_declarator; 12311 id_declarator = id_declarator->declarator) 12312 { 12313 if (id_declarator->kind != cdk_id) 12314 innermost_code = id_declarator->kind; 12315 12316 switch (id_declarator->kind) 12317 { 12318 case cdk_function: 12319 if (id_declarator->declarator 12320 && id_declarator->declarator->kind == cdk_id) 12321 { 12322 sfk = id_declarator->declarator->u.id.sfk; 12323 if (sfk == sfk_destructor) 12324 flags = DTOR_FLAG; 12325 } 12326 break; 12327 12328 case cdk_id: 12329 { 12330 tree qualifying_scope = id_declarator->u.id.qualifying_scope; 12331 tree decl = id_declarator->u.id.unqualified_name; 12332 if (!decl) 12333 break; 12334 if (qualifying_scope) 12335 { 12336 if (check_for_bare_parameter_packs (qualifying_scope, 12337 id_declarator->id_loc)) 12338 return error_mark_node; 12339 if (at_function_scope_p ()) 12340 { 12341 /* [dcl.meaning] 12342 12343 A declarator-id shall not be qualified except 12344 for ... 12345 12346 None of the cases are permitted in block 12347 scope. */ 12348 if (qualifying_scope == global_namespace) 12349 error ("invalid use of qualified-name %<::%D%>", 12350 decl); 12351 else if (TYPE_P (qualifying_scope)) 12352 error ("invalid use of qualified-name %<%T::%D%>", 12353 qualifying_scope, decl); 12354 else 12355 error ("invalid use of qualified-name %<%D::%D%>", 12356 qualifying_scope, decl); 12357 return error_mark_node; 12358 } 12359 else if (TYPE_P (qualifying_scope)) 12360 { 12361 ctype = qualifying_scope; 12362 if (!MAYBE_CLASS_TYPE_P (ctype)) 12363 { 12364 error_at (id_declarator->id_loc, 12365 "%q#T is not a class or namespace", ctype); 12366 ctype = NULL_TREE; 12367 } 12368 else if (innermost_code != cdk_function 12369 && current_class_type 12370 && !uniquely_derived_from_p (ctype, 12371 current_class_type)) 12372 { 12373 error_at (id_declarator->id_loc, 12374 "invalid use of qualified-name %<%T::%D%>", 12375 qualifying_scope, decl); 12376 return error_mark_node; 12377 } 12378 } 12379 else if (TREE_CODE (qualifying_scope) == NAMESPACE_DECL) 12380 in_namespace = qualifying_scope; 12381 } 12382 switch (TREE_CODE (decl)) 12383 { 12384 case BIT_NOT_EXPR: 12385 { 12386 if (innermost_code != cdk_function) 12387 { 12388 error_at (EXPR_LOCATION (decl), 12389 "declaration of %qE as non-function", decl); 12390 return error_mark_node; 12391 } 12392 else if (!qualifying_scope 12393 && !(current_class_type && at_class_scope_p ())) 12394 { 12395 error_at (EXPR_LOCATION (decl), 12396 "declaration of %qE as non-member", decl); 12397 return error_mark_node; 12398 } 12399 12400 tree type = TREE_OPERAND (decl, 0); 12401 if (TYPE_P (type)) 12402 type = constructor_name (type); 12403 name = identifier_to_locale (IDENTIFIER_POINTER (type)); 12404 dname = decl; 12405 } 12406 break; 12407 12408 case TEMPLATE_ID_EXPR: 12409 { 12410 tree fns = TREE_OPERAND (decl, 0); 12411 12412 dname = fns; 12413 if (!identifier_p (dname)) 12414 dname = OVL_NAME (dname); 12415 } 12416 /* Fall through. */ 12417 12418 case IDENTIFIER_NODE: 12419 if (identifier_p (decl)) 12420 dname = decl; 12421 12422 if (IDENTIFIER_KEYWORD_P (dname)) 12423 { 12424 error ("declarator-id missing; using reserved word %qD", 12425 dname); 12426 name = identifier_to_locale (IDENTIFIER_POINTER (dname)); 12427 } 12428 else if (!IDENTIFIER_CONV_OP_P (dname)) 12429 name = identifier_to_locale (IDENTIFIER_POINTER (dname)); 12430 else 12431 { 12432 gcc_assert (flags == NO_SPECIAL); 12433 flags = TYPENAME_FLAG; 12434 sfk = sfk_conversion; 12435 tree glob = get_global_binding (dname); 12436 if (glob && TREE_CODE (glob) == TYPE_DECL) 12437 name = identifier_to_locale (IDENTIFIER_POINTER (dname)); 12438 else 12439 name = "<invalid operator>"; 12440 } 12441 break; 12442 12443 default: 12444 gcc_unreachable (); 12445 } 12446 break; 12447 } 12448 12449 case cdk_array: 12450 case cdk_pointer: 12451 case cdk_reference: 12452 case cdk_ptrmem: 12453 break; 12454 12455 case cdk_decomp: 12456 name = "structured binding"; 12457 break; 12458 12459 case cdk_error: 12460 return error_mark_node; 12461 12462 default: 12463 gcc_unreachable (); 12464 } 12465 if (id_declarator->kind == cdk_id) 12466 break; 12467 } 12468 12469 /* [dcl.fct.edf] 12470 12471 The declarator in a function-definition shall have the form 12472 D1 ( parameter-declaration-clause) ... */ 12473 if (funcdef_flag && innermost_code != cdk_function) 12474 { 12475 error_at (id_loc, "function definition does not declare parameters"); 12476 return error_mark_node; 12477 } 12478 12479 if (flags == TYPENAME_FLAG 12480 && innermost_code != cdk_function 12481 && ! (ctype && !declspecs->any_specifiers_p)) 12482 { 12483 error_at (id_loc, "declaration of %qD as non-function", dname); 12484 return error_mark_node; 12485 } 12486 12487 if (dname && identifier_p (dname)) 12488 { 12489 if (UDLIT_OPER_P (dname) 12490 && innermost_code != cdk_function) 12491 { 12492 error_at (id_loc, "declaration of %qD as non-function", dname); 12493 return error_mark_node; 12494 } 12495 12496 if (IDENTIFIER_ANY_OP_P (dname)) 12497 { 12498 if (typedef_p) 12499 { 12500 error_at (id_loc, "declaration of %qD as %<typedef%>", dname); 12501 return error_mark_node; 12502 } 12503 else if (decl_context == PARM || decl_context == CATCHPARM) 12504 { 12505 error_at (id_loc, "declaration of %qD as parameter", dname); 12506 return error_mark_node; 12507 } 12508 } 12509 } 12510 12511 /* Anything declared one level down from the top level 12512 must be one of the parameters of a function 12513 (because the body is at least two levels down). */ 12514 12515 /* This heuristic cannot be applied to C++ nodes! Fixed, however, 12516 by not allowing C++ class definitions to specify their parameters 12517 with xdecls (must be spec.d in the parmlist). 12518 12519 Since we now wait to push a class scope until we are sure that 12520 we are in a legitimate method context, we must set oldcname 12521 explicitly (since current_class_name is not yet alive). 12522 12523 We also want to avoid calling this a PARM if it is in a namespace. */ 12524 12525 if (decl_context == NORMAL && !toplevel_bindings_p ()) 12526 { 12527 cp_binding_level *b = current_binding_level; 12528 current_binding_level = b->level_chain; 12529 if (current_binding_level != 0 && toplevel_bindings_p ()) 12530 decl_context = PARM; 12531 current_binding_level = b; 12532 } 12533 12534 if (name == NULL) 12535 name = decl_context == PARM ? "parameter" : "type name"; 12536 12537 if (consteval_p && constexpr_p) 12538 { 12539 error_at (declspecs->locations[ds_consteval], 12540 "both %qs and %qs specified", "constexpr", "consteval"); 12541 return error_mark_node; 12542 } 12543 12544 if (concept_p && typedef_p) 12545 { 12546 error_at (declspecs->locations[ds_concept], 12547 "%qs cannot appear in a typedef declaration", "concept"); 12548 return error_mark_node; 12549 } 12550 12551 if (constexpr_p && typedef_p) 12552 { 12553 error_at (declspecs->locations[ds_constexpr], 12554 "%qs cannot appear in a typedef declaration", "constexpr"); 12555 return error_mark_node; 12556 } 12557 12558 if (consteval_p && typedef_p) 12559 { 12560 error_at (declspecs->locations[ds_consteval], 12561 "%qs cannot appear in a typedef declaration", "consteval"); 12562 return error_mark_node; 12563 } 12564 12565 if (constinit_p && typedef_p) 12566 { 12567 error_at (declspecs->locations[ds_constinit], 12568 "%qs cannot appear in a typedef declaration", "constinit"); 12569 return error_mark_node; 12570 } 12571 12572 /* [dcl.spec]/2 "At most one of the constexpr, consteval, and constinit 12573 keywords shall appear in a decl-specifier-seq." */ 12574 if (constinit_p && constexpr_p) 12575 { 12576 gcc_rich_location richloc (declspecs->locations[ds_constinit]); 12577 richloc.add_range (declspecs->locations[ds_constexpr]); 12578 error_at (&richloc, 12579 "can use at most one of the %<constinit%> and %<constexpr%> " 12580 "specifiers"); 12581 return error_mark_node; 12582 } 12583 12584 /* If there were multiple types specified in the decl-specifier-seq, 12585 issue an error message. */ 12586 if (declspecs->multiple_types_p) 12587 { 12588 error_at (typespec_loc, 12589 "two or more data types in declaration of %qs", name); 12590 return error_mark_node; 12591 } 12592 12593 if (declspecs->conflicting_specifiers_p) 12594 return error_mark_node; 12595 12596 /* Extract the basic type from the decl-specifier-seq. */ 12597 type = declspecs->type; 12598 if (type == error_mark_node) 12599 { 12600 type = NULL_TREE; 12601 type_was_error_mark_node = true; 12602 } 12603 12604 /* Ignore erroneous attributes. */ 12605 if (attrlist && *attrlist == error_mark_node) 12606 *attrlist = NULL_TREE; 12607 12608 /* An object declared as __attribute__((unavailable)) suppresses 12609 any reports of being declared with unavailable or deprecated 12610 items. An object declared as __attribute__((deprecated)) 12611 suppresses warnings of uses of other deprecated items. */ 12612 auto ds = make_temp_override (deprecated_state); 12613 if (attrlist && lookup_attribute ("unavailable", *attrlist)) 12614 deprecated_state = UNAVAILABLE_DEPRECATED_SUPPRESS; 12615 else if (attrlist && lookup_attribute ("deprecated", *attrlist)) 12616 deprecated_state = DEPRECATED_SUPPRESS; 12617 12618 cp_handle_deprecated_or_unavailable (type); 12619 if (type && TREE_CODE (type) == TYPE_DECL) 12620 { 12621 cp_warn_deprecated_use_scopes (CP_DECL_CONTEXT (type)); 12622 typedef_decl = type; 12623 type = TREE_TYPE (typedef_decl); 12624 if (DECL_ARTIFICIAL (typedef_decl)) 12625 cp_handle_deprecated_or_unavailable (type); 12626 } 12627 /* No type at all: default to `int', and set DEFAULTED_INT 12628 because it was not a user-defined typedef. */ 12629 if (type == NULL_TREE) 12630 { 12631 if (signed_p || unsigned_p || long_p || short_p) 12632 { 12633 /* These imply 'int'. */ 12634 type = integer_type_node; 12635 defaulted_int = 1; 12636 } 12637 /* If we just have "complex", it is equivalent to "complex double". */ 12638 else if (!longlong && !explicit_intN 12639 && decl_spec_seq_has_spec_p (declspecs, ds_complex)) 12640 { 12641 type = double_type_node; 12642 pedwarn (declspecs->locations[ds_complex], OPT_Wpedantic, 12643 "ISO C++ does not support plain %<complex%> meaning " 12644 "%<double complex%>"); 12645 } 12646 } 12647 /* Gather flags. */ 12648 explicit_int = declspecs->explicit_int_p; 12649 explicit_char = declspecs->explicit_char_p; 12650 12651 #if 0 12652 /* See the code below that used this. */ 12653 if (typedef_decl) 12654 decl_attr = DECL_ATTRIBUTES (typedef_decl); 12655 #endif 12656 typedef_type = type; 12657 12658 if (sfk == sfk_conversion || sfk == sfk_deduction_guide) 12659 ctor_return_type = TREE_TYPE (dname); 12660 else 12661 ctor_return_type = ctype; 12662 12663 if (sfk != sfk_none) 12664 { 12665 type = check_special_function_return_type (sfk, type, 12666 ctor_return_type, 12667 type_quals, 12668 declspecs->locations); 12669 type_quals = TYPE_UNQUALIFIED; 12670 } 12671 else if (type == NULL_TREE) 12672 { 12673 int is_main; 12674 12675 explicit_int = -1; 12676 12677 /* We handle `main' specially here, because 'main () { }' is so 12678 common. With no options, it is allowed. With -Wreturn-type, 12679 it is a warning. It is only an error with -pedantic-errors. */ 12680 is_main = (funcdef_flag 12681 && dname && identifier_p (dname) 12682 && MAIN_NAME_P (dname) 12683 && ctype == NULL_TREE 12684 && in_namespace == NULL_TREE 12685 && current_namespace == global_namespace); 12686 12687 if (type_was_error_mark_node) 12688 /* We've already issued an error, don't complain more. */; 12689 else if (in_system_header_at (id_loc) || flag_ms_extensions) 12690 /* Allow it, sigh. */; 12691 else if (! is_main) 12692 permerror (id_loc, "ISO C++ forbids declaration of %qs with no type", 12693 name); 12694 else if (pedantic) 12695 pedwarn (id_loc, OPT_Wpedantic, 12696 "ISO C++ forbids declaration of %qs with no type", name); 12697 else 12698 warning_at (id_loc, OPT_Wreturn_type, 12699 "ISO C++ forbids declaration of %qs with no type", name); 12700 12701 if (type_was_error_mark_node && template_parm_flag) 12702 /* FIXME we should be able to propagate the error_mark_node as is 12703 for other contexts too. */ 12704 type = error_mark_node; 12705 else 12706 type = integer_type_node; 12707 } 12708 12709 ctype = NULL_TREE; 12710 12711 if (explicit_intN) 12712 { 12713 if (! int_n_enabled_p[declspecs->int_n_idx]) 12714 { 12715 error_at (declspecs->locations[ds_type_spec], 12716 "%<__int%d%> is not supported by this target", 12717 int_n_data[declspecs->int_n_idx].bitsize); 12718 explicit_intN = false; 12719 } 12720 /* Don't pedwarn if the alternate "__intN__" form has been used instead 12721 of "__intN". */ 12722 else if (!int_n_alt && pedantic) 12723 pedwarn (declspecs->locations[ds_type_spec], OPT_Wpedantic, 12724 "ISO C++ does not support %<__int%d%> for %qs", 12725 int_n_data[declspecs->int_n_idx].bitsize, name); 12726 } 12727 12728 /* Now process the modifiers that were specified 12729 and check for invalid combinations. */ 12730 12731 /* Long double is a special combination. */ 12732 if (long_p && !longlong && TYPE_MAIN_VARIANT (type) == double_type_node) 12733 { 12734 long_p = false; 12735 type = cp_build_qualified_type (long_double_type_node, 12736 cp_type_quals (type)); 12737 } 12738 12739 /* Check all other uses of type modifiers. */ 12740 12741 if (unsigned_p || signed_p || long_p || short_p) 12742 { 12743 location_t loc; 12744 const char *key; 12745 if (unsigned_p) 12746 { 12747 key = "unsigned"; 12748 loc = declspecs->locations[ds_unsigned]; 12749 } 12750 else if (signed_p) 12751 { 12752 key = "signed"; 12753 loc = declspecs->locations[ds_signed]; 12754 } 12755 else if (longlong) 12756 { 12757 key = "long long"; 12758 loc = declspecs->locations[ds_long_long]; 12759 } 12760 else if (long_p) 12761 { 12762 key = "long"; 12763 loc = declspecs->locations[ds_long]; 12764 } 12765 else /* if (short_p) */ 12766 { 12767 key = "short"; 12768 loc = declspecs->locations[ds_short]; 12769 } 12770 12771 int ok = 0; 12772 12773 if (signed_p && unsigned_p) 12774 { 12775 gcc_rich_location richloc (declspecs->locations[ds_signed]); 12776 richloc.add_range (declspecs->locations[ds_unsigned]); 12777 error_at (&richloc, 12778 "%<signed%> and %<unsigned%> specified together"); 12779 } 12780 else if (long_p && short_p) 12781 { 12782 gcc_rich_location richloc (declspecs->locations[ds_long]); 12783 richloc.add_range (declspecs->locations[ds_short]); 12784 error_at (&richloc, "%<long%> and %<short%> specified together"); 12785 } 12786 else if (TREE_CODE (type) != INTEGER_TYPE 12787 || type == char8_type_node 12788 || type == char16_type_node 12789 || type == char32_type_node 12790 || ((long_p || short_p) 12791 && (explicit_char || explicit_intN))) 12792 error_at (loc, "%qs specified with %qT", key, type); 12793 else if (!explicit_int && !defaulted_int 12794 && !explicit_char && !explicit_intN) 12795 { 12796 if (typedef_decl) 12797 { 12798 pedwarn (loc, OPT_Wpedantic, 12799 "%qs specified with typedef-name %qD", 12800 key, typedef_decl); 12801 ok = !flag_pedantic_errors; 12802 /* PR108099: __int128_t comes from c_common_nodes_and_builtins, 12803 and is not built as a typedef. */ 12804 if (is_typedef_decl (typedef_decl)) 12805 type = DECL_ORIGINAL_TYPE (typedef_decl); 12806 } 12807 else if (declspecs->decltype_p) 12808 error_at (loc, "%qs specified with %<decltype%>", key); 12809 else 12810 error_at (loc, "%qs specified with %<typeof%>", key); 12811 } 12812 else 12813 ok = 1; 12814 12815 /* Discard the type modifiers if they are invalid. */ 12816 if (! ok) 12817 { 12818 unsigned_p = false; 12819 signed_p = false; 12820 long_p = false; 12821 short_p = false; 12822 longlong = 0; 12823 } 12824 } 12825 12826 /* Decide whether an integer type is signed or not. 12827 Optionally treat bitfields as signed by default. */ 12828 if (unsigned_p 12829 /* [class.bit] 12830 12831 It is implementation-defined whether a plain (neither 12832 explicitly signed or unsigned) char, short, int, or long 12833 bit-field is signed or unsigned. 12834 12835 Naturally, we extend this to long long as well. Note that 12836 this does not include wchar_t. */ 12837 || (bitfield && !flag_signed_bitfields 12838 && !signed_p 12839 /* A typedef for plain `int' without `signed' can be 12840 controlled just like plain `int', but a typedef for 12841 `signed int' cannot be so controlled. */ 12842 && !(typedef_decl 12843 && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)) 12844 && TREE_CODE (type) == INTEGER_TYPE 12845 && !same_type_p (TYPE_MAIN_VARIANT (type), wchar_type_node))) 12846 { 12847 if (explicit_intN) 12848 type = int_n_trees[declspecs->int_n_idx].unsigned_type; 12849 else if (longlong) 12850 type = long_long_unsigned_type_node; 12851 else if (long_p) 12852 type = long_unsigned_type_node; 12853 else if (short_p) 12854 type = short_unsigned_type_node; 12855 else if (type == char_type_node) 12856 type = unsigned_char_type_node; 12857 else if (typedef_decl) 12858 type = c_common_unsigned_type (type); 12859 else 12860 type = unsigned_type_node; 12861 } 12862 else if (signed_p && type == char_type_node) 12863 type = signed_char_type_node; 12864 else if (explicit_intN) 12865 type = int_n_trees[declspecs->int_n_idx].signed_type; 12866 else if (longlong) 12867 type = long_long_integer_type_node; 12868 else if (long_p) 12869 type = long_integer_type_node; 12870 else if (short_p) 12871 type = short_integer_type_node; 12872 else if (signed_p && typedef_decl) 12873 type = c_common_signed_type (type); 12874 12875 if (decl_spec_seq_has_spec_p (declspecs, ds_complex)) 12876 { 12877 if (TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE) 12878 error_at (declspecs->locations[ds_complex], 12879 "complex invalid for %qs", name); 12880 /* If a modifier is specified, the resulting complex is the complex 12881 form of TYPE. E.g, "complex short" is "complex short int". */ 12882 else if (type == integer_type_node) 12883 type = complex_integer_type_node; 12884 else if (type == float_type_node) 12885 type = complex_float_type_node; 12886 else if (type == double_type_node) 12887 type = complex_double_type_node; 12888 else if (type == long_double_type_node) 12889 type = complex_long_double_type_node; 12890 else 12891 type = build_complex_type (type); 12892 } 12893 12894 /* If we're using the injected-class-name to form a compound type or a 12895 declaration, replace it with the underlying class so we don't get 12896 redundant typedefs in the debug output. But if we are returning the 12897 type unchanged, leave it alone so that it's available to 12898 maybe_get_template_decl_from_type_decl. */ 12899 if (CLASS_TYPE_P (type) 12900 && DECL_SELF_REFERENCE_P (TYPE_NAME (type)) 12901 && type == TREE_TYPE (TYPE_NAME (type)) 12902 && (declarator || type_quals)) 12903 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type)); 12904 12905 type_quals |= cp_type_quals (type); 12906 type = cp_build_qualified_type 12907 (type, type_quals, ((((typedef_decl && !DECL_ARTIFICIAL (typedef_decl)) 12908 || declspecs->decltype_p) 12909 ? tf_ignore_bad_quals : 0) | tf_warning_or_error)); 12910 /* We might have ignored or rejected some of the qualifiers. */ 12911 type_quals = cp_type_quals (type); 12912 12913 if (cxx_dialect >= cxx17 && type && is_auto (type) 12914 && innermost_code != cdk_function 12915 /* Placeholder in parm gets a better error below. */ 12916 && !(decl_context == PARM || decl_context == CATCHPARM) 12917 && id_declarator && declarator != id_declarator) 12918 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (type)) 12919 { 12920 auto_diagnostic_group g; 12921 gcc_rich_location richloc (typespec_loc); 12922 richloc.add_fixit_insert_after ("<>"); 12923 error_at (&richloc, "missing template argument list after %qE; " 12924 "for deduction, template placeholder must be followed " 12925 "by a simple declarator-id", tmpl); 12926 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here", tmpl); 12927 type = error_mark_node; 12928 } 12929 12930 staticp = 0; 12931 inlinep = decl_spec_seq_has_spec_p (declspecs, ds_inline); 12932 virtualp = decl_spec_seq_has_spec_p (declspecs, ds_virtual); 12933 explicitp = decl_spec_seq_has_spec_p (declspecs, ds_explicit); 12934 12935 storage_class = declspecs->storage_class; 12936 if (storage_class == sc_static) 12937 staticp = 1 + (decl_context == FIELD); 12938 else if (decl_context == FIELD && sfk == sfk_deduction_guide) 12939 /* Treat class-scope deduction guides as static member functions 12940 so that they get a FUNCTION_TYPE instead of a METHOD_TYPE. */ 12941 staticp = 2; 12942 12943 if (virtualp) 12944 { 12945 if (staticp == 2) 12946 { 12947 gcc_rich_location richloc (declspecs->locations[ds_virtual]); 12948 richloc.add_range (declspecs->locations[ds_storage_class]); 12949 error_at (&richloc, "member %qD cannot be declared both %<virtual%> " 12950 "and %<static%>", dname); 12951 storage_class = sc_none; 12952 staticp = 0; 12953 } 12954 if (constexpr_p && pedantic && cxx_dialect < cxx20) 12955 { 12956 gcc_rich_location richloc (declspecs->locations[ds_virtual]); 12957 richloc.add_range (declspecs->locations[ds_constexpr]); 12958 pedwarn (&richloc, OPT_Wc__20_extensions, "member %qD can be " 12959 "declared both %<virtual%> and %<constexpr%> only in " 12960 "%<-std=c++20%> or %<-std=gnu++20%>", dname); 12961 } 12962 } 12963 friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend); 12964 12965 /* Issue errors about use of storage classes for parameters. */ 12966 if (decl_context == PARM) 12967 { 12968 if (typedef_p) 12969 { 12970 error_at (declspecs->locations[ds_typedef], 12971 "typedef declaration invalid in parameter declaration"); 12972 return error_mark_node; 12973 } 12974 else if (template_parm_flag && storage_class != sc_none) 12975 { 12976 error_at (min_location (declspecs->locations[ds_thread], 12977 declspecs->locations[ds_storage_class]), 12978 "storage class specified for template parameter %qs", 12979 name); 12980 return error_mark_node; 12981 } 12982 else if (storage_class == sc_static 12983 || storage_class == sc_extern 12984 || thread_p) 12985 { 12986 error_at (min_location (declspecs->locations[ds_thread], 12987 declspecs->locations[ds_storage_class]), 12988 "storage class specified for parameter %qs", name); 12989 return error_mark_node; 12990 } 12991 12992 /* Function parameters cannot be concept. */ 12993 if (concept_p) 12994 { 12995 error_at (declspecs->locations[ds_concept], 12996 "a parameter cannot be declared %qs", "concept"); 12997 concept_p = 0; 12998 constexpr_p = 0; 12999 } 13000 /* Function parameters cannot be constexpr. If we saw one, moan 13001 and pretend it wasn't there. */ 13002 else if (constexpr_p) 13003 { 13004 error_at (declspecs->locations[ds_constexpr], 13005 "a parameter cannot be declared %qs", "constexpr"); 13006 constexpr_p = 0; 13007 } 13008 if (constinit_p) 13009 { 13010 error_at (declspecs->locations[ds_constinit], 13011 "a parameter cannot be declared %qs", "constinit"); 13012 constinit_p = 0; 13013 } 13014 if (consteval_p) 13015 { 13016 error_at (declspecs->locations[ds_consteval], 13017 "a parameter cannot be declared %qs", "consteval"); 13018 consteval_p = 0; 13019 } 13020 } 13021 13022 /* Give error if `virtual' is used outside of class declaration. */ 13023 if (virtualp 13024 && (current_class_name == NULL_TREE || decl_context != FIELD)) 13025 { 13026 error_at (declspecs->locations[ds_virtual], 13027 "%<virtual%> outside class declaration"); 13028 virtualp = 0; 13029 } 13030 13031 if (innermost_code == cdk_decomp) 13032 { 13033 location_t loc = (declarator->kind == cdk_reference 13034 ? declarator->declarator->id_loc : declarator->id_loc); 13035 if (inlinep) 13036 error_at (declspecs->locations[ds_inline], 13037 "structured binding declaration cannot be %qs", "inline"); 13038 if (typedef_p) 13039 error_at (declspecs->locations[ds_typedef], 13040 "structured binding declaration cannot be %qs", "typedef"); 13041 if (constexpr_p && !concept_p) 13042 error_at (declspecs->locations[ds_constexpr], "structured " 13043 "binding declaration cannot be %qs", "constexpr"); 13044 if (consteval_p) 13045 error_at (declspecs->locations[ds_consteval], "structured " 13046 "binding declaration cannot be %qs", "consteval"); 13047 if (thread_p && cxx_dialect < cxx20) 13048 pedwarn (declspecs->locations[ds_thread], OPT_Wc__20_extensions, 13049 "structured binding declaration can be %qs only in " 13050 "%<-std=c++20%> or %<-std=gnu++20%>", 13051 declspecs->gnu_thread_keyword_p 13052 ? "__thread" : "thread_local"); 13053 if (concept_p) 13054 error_at (declspecs->locations[ds_concept], 13055 "structured binding declaration cannot be %qs", "concept"); 13056 /* [dcl.struct.bind] "A cv that includes volatile is deprecated." */ 13057 if (type_quals & TYPE_QUAL_VOLATILE) 13058 warning_at (declspecs->locations[ds_volatile], OPT_Wvolatile, 13059 "%<volatile%>-qualified structured binding is deprecated"); 13060 switch (storage_class) 13061 { 13062 case sc_none: 13063 break; 13064 case sc_register: 13065 error_at (loc, "structured binding declaration cannot be %qs", 13066 "register"); 13067 break; 13068 case sc_static: 13069 if (cxx_dialect < cxx20) 13070 pedwarn (loc, OPT_Wc__20_extensions, 13071 "structured binding declaration can be %qs only in " 13072 "%<-std=c++20%> or %<-std=gnu++20%>", "static"); 13073 break; 13074 case sc_extern: 13075 error_at (loc, "structured binding declaration cannot be %qs", 13076 "extern"); 13077 break; 13078 case sc_mutable: 13079 error_at (loc, "structured binding declaration cannot be %qs", 13080 "mutable"); 13081 break; 13082 case sc_auto: 13083 error_at (loc, "structured binding declaration cannot be " 13084 "C++98 %<auto%>"); 13085 break; 13086 default: 13087 gcc_unreachable (); 13088 } 13089 if (TREE_CODE (type) != TEMPLATE_TYPE_PARM 13090 || TYPE_IDENTIFIER (type) != auto_identifier) 13091 { 13092 if (type != error_mark_node) 13093 { 13094 auto_diagnostic_group d; 13095 error_at (loc, "structured binding declaration cannot have " 13096 "type %qT", type); 13097 inform (loc, 13098 "type must be cv-qualified %<auto%> or reference to " 13099 "cv-qualified %<auto%>"); 13100 } 13101 type = build_qualified_type (make_auto (), type_quals); 13102 declspecs->type = type; 13103 } 13104 else if (PLACEHOLDER_TYPE_CONSTRAINTS_INFO (type)) 13105 pedwarn (loc, OPT_Wpedantic, 13106 "structured binding declaration cannot have constrained " 13107 "%<auto%> type %qT", type); 13108 inlinep = 0; 13109 typedef_p = 0; 13110 constexpr_p = 0; 13111 consteval_p = 0; 13112 concept_p = 0; 13113 if (storage_class != sc_static) 13114 { 13115 storage_class = sc_none; 13116 declspecs->storage_class = sc_none; 13117 } 13118 } 13119 13120 /* Static anonymous unions are dealt with here. */ 13121 if (staticp && decl_context == TYPENAME 13122 && declspecs->type 13123 && ANON_AGGR_TYPE_P (declspecs->type)) 13124 decl_context = FIELD; 13125 13126 /* Warn about storage classes that are invalid for certain 13127 kinds of declarations (parameters, typenames, etc.). */ 13128 if (thread_p 13129 && ((storage_class 13130 && storage_class != sc_extern 13131 && storage_class != sc_static) 13132 || typedef_p)) 13133 { 13134 location_t loc 13135 = min_location (declspecs->locations[ds_thread], 13136 declspecs->locations[ds_storage_class]); 13137 error_at (loc, "multiple storage classes in declaration of %qs", name); 13138 thread_p = false; 13139 } 13140 if (decl_context != NORMAL 13141 && ((storage_class != sc_none 13142 && storage_class != sc_mutable) 13143 || thread_p)) 13144 { 13145 if ((decl_context == PARM || decl_context == CATCHPARM) 13146 && (storage_class == sc_register 13147 || storage_class == sc_auto)) 13148 ; 13149 else if (typedef_p) 13150 ; 13151 else if (decl_context == FIELD 13152 /* C++ allows static class elements. */ 13153 && storage_class == sc_static) 13154 /* C++ also allows inlines and signed and unsigned elements, 13155 but in those cases we don't come in here. */ 13156 ; 13157 else 13158 { 13159 location_t loc 13160 = min_location (declspecs->locations[ds_thread], 13161 declspecs->locations[ds_storage_class]); 13162 if (decl_context == FIELD) 13163 error_at (loc, "storage class specified for %qs", name); 13164 else if (decl_context == PARM || decl_context == CATCHPARM) 13165 error_at (loc, "storage class specified for parameter %qs", name); 13166 else 13167 error_at (loc, "storage class specified for typename"); 13168 if (storage_class == sc_register 13169 || storage_class == sc_auto 13170 || storage_class == sc_extern 13171 || thread_p) 13172 storage_class = sc_none; 13173 } 13174 } 13175 else if (storage_class == sc_extern && funcdef_flag 13176 && ! toplevel_bindings_p ()) 13177 error ("nested function %qs declared %<extern%>", name); 13178 else if (toplevel_bindings_p ()) 13179 { 13180 if (storage_class == sc_auto) 13181 error_at (declspecs->locations[ds_storage_class], 13182 "top-level declaration of %qs specifies %<auto%>", name); 13183 } 13184 else if (thread_p 13185 && storage_class != sc_extern 13186 && storage_class != sc_static) 13187 { 13188 if (declspecs->gnu_thread_keyword_p) 13189 pedwarn (declspecs->locations[ds_thread], 13190 0, "function-scope %qs implicitly auto and " 13191 "declared %<__thread%>", name); 13192 13193 /* When thread_local is applied to a variable of block scope the 13194 storage-class-specifier static is implied if it does not appear 13195 explicitly. */ 13196 storage_class = declspecs->storage_class = sc_static; 13197 staticp = 1; 13198 } 13199 13200 if (storage_class && friendp) 13201 { 13202 error_at (min_location (declspecs->locations[ds_thread], 13203 declspecs->locations[ds_storage_class]), 13204 "storage class specifiers invalid in friend function " 13205 "declarations"); 13206 storage_class = sc_none; 13207 staticp = 0; 13208 } 13209 13210 if (!id_declarator) 13211 unqualified_id = NULL_TREE; 13212 else 13213 { 13214 unqualified_id = id_declarator->u.id.unqualified_name; 13215 switch (TREE_CODE (unqualified_id)) 13216 { 13217 case BIT_NOT_EXPR: 13218 unqualified_id = TREE_OPERAND (unqualified_id, 0); 13219 if (TYPE_P (unqualified_id)) 13220 unqualified_id = constructor_name (unqualified_id); 13221 break; 13222 13223 case IDENTIFIER_NODE: 13224 case TEMPLATE_ID_EXPR: 13225 break; 13226 13227 default: 13228 gcc_unreachable (); 13229 } 13230 } 13231 13232 if (declspecs->std_attributes 13233 && !diagnose_misapplied_contracts (declspecs->std_attributes)) 13234 { 13235 location_t attr_loc = declspecs->locations[ds_std_attribute]; 13236 if (any_nonignored_attribute_p (declspecs->std_attributes) 13237 && warning_at (attr_loc, OPT_Wattributes, "attribute ignored")) 13238 inform (attr_loc, "an attribute that appertains to a type-specifier " 13239 "is ignored"); 13240 } 13241 13242 if (attrlist) 13243 diagnose_misapplied_contracts (*attrlist); 13244 13245 /* Skip over build_memfn_type when a FUNCTION_DECL is an xobj memfn. */ 13246 bool is_xobj_member_function = false; 13247 /* Determine the type of the entity declared by recurring on the 13248 declarator. */ 13249 for (; declarator; declarator = declarator->declarator) 13250 { 13251 const cp_declarator *inner_declarator; 13252 tree attrs; 13253 13254 if (type == error_mark_node) 13255 return error_mark_node; 13256 13257 attrs = declarator->attributes; 13258 if (attrs) 13259 { 13260 int attr_flags; 13261 13262 attr_flags = 0; 13263 if (declarator->kind == cdk_id) 13264 attr_flags |= (int) ATTR_FLAG_DECL_NEXT; 13265 if (declarator->kind == cdk_function) 13266 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT; 13267 if (declarator->kind == cdk_array) 13268 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT; 13269 tree late_attrs = NULL_TREE; 13270 if (decl_context != PARM && decl_context != TYPENAME) 13271 /* Assume that any attributes that get applied late to 13272 templates will DTRT when applied to the declaration 13273 as a whole. */ 13274 late_attrs = splice_template_attributes (&attrs, type); 13275 returned_attrs = decl_attributes (&type, 13276 attr_chainon (returned_attrs, 13277 attrs), 13278 attr_flags); 13279 returned_attrs = attr_chainon (late_attrs, returned_attrs); 13280 } 13281 13282 inner_declarator = declarator->declarator; 13283 13284 /* Check that contracts aren't misapplied. */ 13285 if (tree contract_attr = find_contract (declarator->std_attributes)) 13286 if (declarator->kind != cdk_function 13287 || innermost_code != cdk_function) 13288 diagnose_misapplied_contracts (contract_attr); 13289 13290 /* We don't want to warn in parameter context because we don't 13291 yet know if the parse will succeed, and this might turn out 13292 to be a constructor call. */ 13293 if (decl_context != PARM 13294 && decl_context != TYPENAME 13295 && !typedef_p 13296 && declarator->parenthesized != UNKNOWN_LOCATION 13297 /* If the type is class-like and the inner name used a 13298 global namespace qualifier, we need the parens. 13299 Unfortunately all we can tell is whether a qualified name 13300 was used or not. */ 13301 && !(inner_declarator 13302 && inner_declarator->kind == cdk_id 13303 && inner_declarator->u.id.qualifying_scope 13304 && (MAYBE_CLASS_TYPE_P (type) 13305 || TREE_CODE (type) == ENUMERAL_TYPE))) 13306 { 13307 if (warning_at (declarator->parenthesized, OPT_Wparentheses, 13308 "unnecessary parentheses in declaration of %qs", 13309 name)) 13310 { 13311 gcc_rich_location iloc (declarator->parenthesized); 13312 iloc.add_fixit_remove (get_start (declarator->parenthesized)); 13313 iloc.add_fixit_remove (get_finish (declarator->parenthesized)); 13314 inform (&iloc, "remove parentheses"); 13315 } 13316 } 13317 if (declarator->kind == cdk_id || declarator->kind == cdk_decomp) 13318 break; 13319 13320 switch (declarator->kind) 13321 { 13322 case cdk_array: 13323 type = create_array_type_for_decl (dname, type, 13324 declarator->u.array.bounds, 13325 declarator->id_loc); 13326 if (!valid_array_size_p (dname 13327 ? declarator->id_loc : input_location, 13328 type, dname)) 13329 type = error_mark_node; 13330 13331 if (declarator->std_attributes) 13332 /* [dcl.array]/1: 13333 13334 The optional attribute-specifier-seq appertains to the 13335 array. */ 13336 returned_attrs = attr_chainon (returned_attrs, 13337 declarator->std_attributes); 13338 break; 13339 13340 case cdk_function: 13341 { 13342 tree arg_types; 13343 int funcdecl_p; 13344 13345 /* Declaring a function type. */ 13346 13347 /* Pick up type qualifiers which should be applied to `this'. */ 13348 memfn_quals = declarator->u.function.qualifiers; 13349 /* Pick up virt-specifiers. */ 13350 virt_specifiers = declarator->u.function.virt_specifiers; 13351 /* And ref-qualifier, too */ 13352 rqual = declarator->u.function.ref_qualifier; 13353 /* And tx-qualifier. */ 13354 tree tx_qual = declarator->u.function.tx_qualifier; 13355 /* Pick up the exception specifications. */ 13356 raises = declarator->u.function.exception_specification; 13357 /* If the exception-specification is ill-formed, let's pretend 13358 there wasn't one. */ 13359 if (raises == error_mark_node) 13360 raises = NULL_TREE; 13361 13362 auto find_xobj_parm = [](tree parm_list) 13363 { 13364 /* There is no need to iterate over the list, 13365 only the first parm can be a valid xobj parm. */ 13366 if (!parm_list || TREE_PURPOSE (parm_list) != this_identifier) 13367 return NULL_TREE; 13368 /* If we make it here, we are looking at an xobj parm. 13369 13370 Non-null 'purpose' usually means the parm has a default 13371 argument, we don't want to violate this assumption. */ 13372 TREE_PURPOSE (parm_list) = NULL_TREE; 13373 return TREE_VALUE (parm_list); 13374 }; 13375 13376 tree xobj_parm 13377 = find_xobj_parm (declarator->u.function.parameters); 13378 is_xobj_member_function = xobj_parm; 13379 13380 if (xobj_parm && cxx_dialect < cxx23) 13381 pedwarn (DECL_SOURCE_LOCATION (xobj_parm), OPT_Wc__23_extensions, 13382 "explicit object member function only available " 13383 "with %<-std=c++23%> or %<-std=gnu++23%>"); 13384 13385 if (xobj_parm && decl_context == TYPENAME) 13386 { 13387 /* We inform in every case, just differently depending on what 13388 case it is. */ 13389 auto_diagnostic_group d; 13390 bool ptr_type = true; 13391 /* If declarator->kind is cdk_function and we are at the end of 13392 the declarator chain, we are looking at a function type. */ 13393 if (!declarator->declarator) 13394 { 13395 error_at (DECL_SOURCE_LOCATION (xobj_parm), 13396 "a function type cannot " 13397 "have an explicit object parameter"); 13398 ptr_type = false; 13399 } 13400 else if (declarator->declarator->kind == cdk_pointer) 13401 error_at (DECL_SOURCE_LOCATION (xobj_parm), 13402 "a pointer to function type cannot " 13403 "have an explicit object parameter"); 13404 else if (declarator->declarator->kind == cdk_ptrmem) 13405 error_at (DECL_SOURCE_LOCATION (xobj_parm), 13406 "a pointer to member function type " 13407 "cannot have an explicit object parameter"); 13408 else 13409 gcc_unreachable (); 13410 13411 /* The locations being used here are probably not correct. */ 13412 if (ptr_type) 13413 inform (DECL_SOURCE_LOCATION (xobj_parm), 13414 "the type of a pointer to explicit object member " 13415 "function is a regular pointer to function type"); 13416 else 13417 inform (DECL_SOURCE_LOCATION (xobj_parm), 13418 "the type of an explicit object " 13419 "member function is a regular function type"); 13420 /* Ideally we should synthesize the correct syntax 13421 for the user, perhaps this could be added later. */ 13422 } 13423 /* Since a valid xobj parm has its purpose cleared in find_xobj_parm 13424 the first parm node will never erroneously be detected here. */ 13425 { 13426 auto_diagnostic_group d; 13427 bool bad_xobj_parm_encountered = false; 13428 for (tree parm = declarator->u.function.parameters; 13429 parm && parm != void_list_node; 13430 parm = TREE_CHAIN (parm)) 13431 { 13432 if (TREE_PURPOSE (parm) != this_identifier) 13433 continue; 13434 bad_xobj_parm_encountered = true; 13435 TREE_PURPOSE (parm) = NULL_TREE; 13436 gcc_rich_location bad_xobj_parm 13437 (DECL_SOURCE_LOCATION (TREE_VALUE (parm))); 13438 error_at (&bad_xobj_parm, 13439 "only the first parameter of a member function " 13440 "can be declared as an explicit object parameter"); 13441 } 13442 if (bad_xobj_parm_encountered && xobj_parm) 13443 inform (DECL_SOURCE_LOCATION (xobj_parm), 13444 "valid explicit object parameter declared here"); 13445 } 13446 13447 if (reqs) 13448 error_at (location_of (reqs), "requires-clause on return type"); 13449 reqs = declarator->u.function.requires_clause; 13450 13451 /* Say it's a definition only for the CALL_EXPR 13452 closest to the identifier. */ 13453 funcdecl_p = inner_declarator && inner_declarator->kind == cdk_id; 13454 13455 /* Handle a late-specified return type. */ 13456 tree late_return_type = declarator->u.function.late_return_type; 13457 if (tree auto_node = type_uses_auto (type)) 13458 { 13459 if (!late_return_type) 13460 { 13461 if (!funcdecl_p) 13462 /* auto (*fp)() = f; is OK. */; 13463 else if (current_class_type 13464 && LAMBDA_TYPE_P (current_class_type)) 13465 /* OK for C++11 lambdas. */; 13466 else if (cxx_dialect < cxx14) 13467 { 13468 error_at (typespec_loc, "%qs function uses " 13469 "%<auto%> type specifier without " 13470 "trailing return type", name); 13471 inform (typespec_loc, 13472 "deduced return type only available " 13473 "with %<-std=c++14%> or %<-std=gnu++14%>"); 13474 } 13475 else if (virtualp) 13476 { 13477 error_at (typespec_loc, "virtual function " 13478 "cannot have deduced return type"); 13479 virtualp = false; 13480 } 13481 } 13482 else if (!is_auto (type) && sfk != sfk_conversion) 13483 { 13484 error_at (typespec_loc, "%qs function with trailing " 13485 "return type has %qT as its type rather " 13486 "than plain %<auto%>", name, type); 13487 return error_mark_node; 13488 } 13489 else if (is_auto (type) && AUTO_IS_DECLTYPE (type)) 13490 { 13491 if (funcdecl_p) 13492 error_at (typespec_loc, 13493 "%qs function with trailing return type " 13494 "has %<decltype(auto)%> as its type " 13495 "rather than plain %<auto%>", name); 13496 else 13497 error_at (typespec_loc, 13498 "invalid use of %<decltype(auto)%>"); 13499 return error_mark_node; 13500 } 13501 tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node); 13502 if (!tmpl) 13503 if (tree late_auto = type_uses_auto (late_return_type)) 13504 tmpl = CLASS_PLACEHOLDER_TEMPLATE (late_auto); 13505 if (tmpl) 13506 { 13507 if (!funcdecl_p || !dguide_name_p (unqualified_id)) 13508 { 13509 auto_diagnostic_group g; 13510 error_at (typespec_loc, "deduced class " 13511 "type %qD in function return type", 13512 DECL_NAME (tmpl)); 13513 inform (DECL_SOURCE_LOCATION (tmpl), 13514 "%qD declared here", tmpl); 13515 return error_mark_node; 13516 } 13517 else if (!late_return_type) 13518 { 13519 error_at (declarator->id_loc, "deduction guide " 13520 "for %qT must have trailing return " 13521 "type", TREE_TYPE (tmpl)); 13522 inform (DECL_SOURCE_LOCATION (tmpl), 13523 "%qD declared here", tmpl); 13524 return error_mark_node; 13525 } 13526 else if (CLASS_TYPE_P (late_return_type) 13527 && CLASSTYPE_TEMPLATE_INFO (late_return_type) 13528 && (CLASSTYPE_TI_TEMPLATE (late_return_type) 13529 == tmpl)) 13530 /* OK */; 13531 else 13532 error ("trailing return type %qT of deduction guide " 13533 "is not a specialization of %qT", 13534 late_return_type, TREE_TYPE (tmpl)); 13535 } 13536 } 13537 else if (late_return_type 13538 && sfk != sfk_conversion) 13539 { 13540 if (late_return_type == error_mark_node) 13541 return error_mark_node; 13542 if (cxx_dialect < cxx11) 13543 /* Not using maybe_warn_cpp0x because this should 13544 always be an error. */ 13545 error_at (typespec_loc, 13546 "trailing return type only available " 13547 "with %<-std=c++11%> or %<-std=gnu++11%>"); 13548 else 13549 error_at (typespec_loc, "%qs function with trailing " 13550 "return type not declared with %<auto%> " 13551 "type specifier", name); 13552 return error_mark_node; 13553 } 13554 if (late_return_type && sfk == sfk_conversion) 13555 { 13556 error ("a conversion function cannot have a trailing return type"); 13557 return error_mark_node; 13558 } 13559 type = splice_late_return_type (type, late_return_type); 13560 if (type == error_mark_node) 13561 return error_mark_node; 13562 13563 if (late_return_type) 13564 { 13565 late_return_type_p = true; 13566 type_quals = cp_type_quals (type); 13567 } 13568 13569 if (type_quals != TYPE_UNQUALIFIED) 13570 { 13571 /* It's wrong, for instance, to issue a -Wignored-qualifiers 13572 warning for 13573 static_assert(!is_same_v<void(*)(), const void(*)()>); 13574 because there the qualifier matters. */ 13575 if (funcdecl_p && (SCALAR_TYPE_P (type) || VOID_TYPE_P (type))) 13576 warning_at (typespec_loc, OPT_Wignored_qualifiers, "type " 13577 "qualifiers ignored on function return type"); 13578 /* [dcl.fct] "A volatile-qualified return type is 13579 deprecated." */ 13580 if (type_quals & TYPE_QUAL_VOLATILE) 13581 warning_at (typespec_loc, OPT_Wvolatile, 13582 "%<volatile%>-qualified return type is " 13583 "deprecated"); 13584 13585 /* We now know that the TYPE_QUALS don't apply to the 13586 decl, but to its return type. */ 13587 type_quals = TYPE_UNQUALIFIED; 13588 } 13589 13590 /* Error about some types functions can't return. */ 13591 13592 if (TREE_CODE (type) == FUNCTION_TYPE) 13593 { 13594 error_at (typespec_loc, "%qs declared as function returning " 13595 "a function", name); 13596 return error_mark_node; 13597 } 13598 if (TREE_CODE (type) == ARRAY_TYPE) 13599 { 13600 error_at (typespec_loc, "%qs declared as function returning " 13601 "an array", name); 13602 return error_mark_node; 13603 } 13604 if (constinit_p && funcdecl_p) 13605 { 13606 error_at (declspecs->locations[ds_constinit], 13607 "%<constinit%> on function return type is not " 13608 "allowed"); 13609 return error_mark_node; 13610 } 13611 13612 if (check_decltype_auto (typespec_loc, type)) 13613 return error_mark_node; 13614 13615 if (ctype == NULL_TREE 13616 && decl_context == FIELD 13617 && funcdecl_p 13618 && friendp == 0) 13619 ctype = current_class_type; 13620 13621 if (ctype && (sfk == sfk_constructor 13622 || sfk == sfk_destructor)) 13623 { 13624 /* We are within a class's scope. If our declarator name 13625 is the same as the class name, and we are defining 13626 a function, then it is a constructor/destructor, and 13627 therefore returns a void type. */ 13628 13629 /* ISO C++ 12.4/2. A destructor may not be declared 13630 const or volatile. A destructor may not be static. 13631 A destructor may not be declared with ref-qualifier. 13632 13633 ISO C++ 12.1. A constructor may not be declared 13634 const or volatile. A constructor may not be 13635 virtual. A constructor may not be static. 13636 A constructor may not be declared with ref-qualifier. */ 13637 if (staticp == 2) 13638 error_at (declspecs->locations[ds_storage_class], 13639 (flags == DTOR_FLAG) 13640 ? G_("destructor cannot be static member " 13641 "function") 13642 : G_("constructor cannot be static member " 13643 "function")); 13644 if (memfn_quals) 13645 { 13646 error ((flags == DTOR_FLAG) 13647 ? G_("destructors may not be cv-qualified") 13648 : G_("constructors may not be cv-qualified")); 13649 memfn_quals = TYPE_UNQUALIFIED; 13650 } 13651 13652 if (rqual) 13653 { 13654 maybe_warn_cpp0x (CPP0X_REF_QUALIFIER); 13655 error ((flags == DTOR_FLAG) 13656 ? G_("destructors may not be ref-qualified") 13657 : G_("constructors may not be ref-qualified")); 13658 rqual = REF_QUAL_NONE; 13659 } 13660 13661 if (decl_context == FIELD 13662 && !member_function_or_else (ctype, 13663 current_class_type, 13664 flags)) 13665 return error_mark_node; 13666 13667 if (flags != DTOR_FLAG) 13668 { 13669 /* It's a constructor. */ 13670 if (explicitp == 1) 13671 explicitp = 2; 13672 if (virtualp) 13673 { 13674 permerror (declspecs->locations[ds_virtual], 13675 "constructors cannot be declared %<virtual%>"); 13676 virtualp = 0; 13677 } 13678 if (decl_context == FIELD 13679 && sfk != sfk_constructor) 13680 return error_mark_node; 13681 } 13682 if (decl_context == FIELD) 13683 staticp = 0; 13684 } 13685 else if (friendp) 13686 { 13687 if (virtualp) 13688 { 13689 /* Cannot be both friend and virtual. */ 13690 gcc_rich_location richloc (declspecs->locations[ds_virtual]); 13691 richloc.add_range (declspecs->locations[ds_friend]); 13692 error_at (&richloc, "virtual functions cannot be friends"); 13693 friendp = 0; 13694 } 13695 if (decl_context == NORMAL) 13696 error_at (declarator->id_loc, 13697 "friend declaration not in class definition"); 13698 if (current_function_decl && funcdef_flag) 13699 { 13700 error_at (declarator->id_loc, 13701 "cannot define friend function %qs in a local " 13702 "class definition", name); 13703 friendp = 0; 13704 } 13705 /* [class.friend]/6: A function can be defined in a friend 13706 declaration if the function name is unqualified. */ 13707 if (funcdef_flag && in_namespace) 13708 { 13709 if (in_namespace == global_namespace) 13710 error_at (declarator->id_loc, 13711 "friend function definition %qs cannot have " 13712 "a name qualified with %<::%>", name); 13713 else 13714 error_at (declarator->id_loc, 13715 "friend function definition %qs cannot have " 13716 "a name qualified with %<%D::%>", name, 13717 in_namespace); 13718 } 13719 } 13720 else if (ctype && sfk == sfk_conversion) 13721 { 13722 if (explicitp == 1) 13723 { 13724 maybe_warn_cpp0x (CPP0X_EXPLICIT_CONVERSION); 13725 explicitp = 2; 13726 } 13727 } 13728 else if (sfk == sfk_deduction_guide) 13729 { 13730 if (explicitp == 1) 13731 explicitp = 2; 13732 } 13733 13734 if (xobj_parm) 13735 { 13736 if (!ctype 13737 && decl_context == NORMAL 13738 && (in_namespace 13739 || !declarator->declarator->u.id.qualifying_scope)) 13740 error_at (DECL_SOURCE_LOCATION (xobj_parm), 13741 "a non-member function cannot have " 13742 "an explicit object parameter"); 13743 else 13744 { 13745 if (virtualp) 13746 { 13747 auto_diagnostic_group d; 13748 error_at (declspecs->locations[ds_virtual], 13749 "an explicit object member function cannot " 13750 "be %<virtual%>"); 13751 inform (DECL_SOURCE_LOCATION (xobj_parm), 13752 "explicit object parameter declared here"); 13753 virtualp = false; 13754 } 13755 if (staticp >= 2) 13756 { 13757 auto_diagnostic_group d; 13758 error_at (declspecs->locations[ds_storage_class], 13759 "an explicit object member function cannot " 13760 "be %<static%>"); 13761 inform (DECL_SOURCE_LOCATION (xobj_parm), 13762 "explicit object parameter declared here"); 13763 } 13764 if (unqualified_id 13765 && identifier_p (unqualified_id) 13766 && IDENTIFIER_NEWDEL_OP_P (unqualified_id)) 13767 error_at (DECL_SOURCE_LOCATION (xobj_parm), 13768 "%qD cannot be an explicit object member " 13769 "function", unqualified_id); 13770 } 13771 } 13772 tree pushed_scope = NULL_TREE; 13773 if (funcdecl_p 13774 && decl_context != FIELD 13775 && inner_declarator->u.id.qualifying_scope 13776 && CLASS_TYPE_P (inner_declarator->u.id.qualifying_scope)) 13777 pushed_scope 13778 = push_scope (inner_declarator->u.id.qualifying_scope); 13779 13780 arg_types = grokparms (declarator->u.function.parameters, &parms); 13781 13782 if (pushed_scope) 13783 pop_scope (pushed_scope); 13784 13785 if (inner_declarator 13786 && inner_declarator->kind == cdk_id 13787 && inner_declarator->u.id.sfk == sfk_destructor 13788 && arg_types != void_list_node) 13789 { 13790 error_at (declarator->id_loc, 13791 "destructors may not have parameters"); 13792 arg_types = void_list_node; 13793 parms = NULL_TREE; 13794 is_xobj_member_function = false; 13795 } 13796 13797 type = build_function_type (type, arg_types); 13798 13799 tree attrs = declarator->std_attributes; 13800 if (tx_qual) 13801 { 13802 tree att = build_tree_list (tx_qual, NULL_TREE); 13803 /* transaction_safe applies to the type, but 13804 transaction_safe_dynamic applies to the function. */ 13805 if (is_attribute_p ("transaction_safe", tx_qual)) 13806 attrs = attr_chainon (attrs, att); 13807 else 13808 returned_attrs = attr_chainon (returned_attrs, att); 13809 } 13810 13811 /* Actually apply the contract attributes to the declaration. */ 13812 for (tree *p = &attrs; *p;) 13813 { 13814 tree l = *p; 13815 if (cxx_contract_attribute_p (l)) 13816 { 13817 *p = TREE_CHAIN (l); 13818 /* Intentionally reverse order of contracts so they're 13819 reversed back into their lexical order. */ 13820 TREE_CHAIN (l) = NULL_TREE; 13821 returned_attrs = chainon (l, returned_attrs); 13822 } 13823 else 13824 p = &TREE_CHAIN (l); 13825 } 13826 13827 if (attrs) 13828 /* [dcl.fct]/2: 13829 13830 The optional attribute-specifier-seq appertains to 13831 the function type. */ 13832 cplus_decl_attributes (&type, attrs, 0); 13833 13834 if (raises) 13835 type = build_exception_variant (type, raises); 13836 } 13837 break; 13838 13839 case cdk_pointer: 13840 case cdk_reference: 13841 case cdk_ptrmem: 13842 /* Filter out pointers-to-references and references-to-references. 13843 We can get these if a TYPE_DECL is used. */ 13844 13845 if (TYPE_REF_P (type)) 13846 { 13847 if (declarator->kind != cdk_reference) 13848 { 13849 error ("cannot declare pointer to %q#T", type); 13850 type = TREE_TYPE (type); 13851 } 13852 13853 /* In C++0x, we allow reference to reference declarations 13854 that occur indirectly through typedefs [7.1.3/8 dcl.typedef] 13855 and template type arguments [14.3.1/4 temp.arg.type]. The 13856 check for direct reference to reference declarations, which 13857 are still forbidden, occurs below. Reasoning behind the change 13858 can be found in DR106, DR540, and the rvalue reference 13859 proposals. */ 13860 else if (cxx_dialect == cxx98) 13861 { 13862 error ("cannot declare reference to %q#T", type); 13863 type = TREE_TYPE (type); 13864 } 13865 } 13866 else if (VOID_TYPE_P (type)) 13867 { 13868 if (declarator->kind == cdk_reference) 13869 error ("cannot declare reference to %q#T", type); 13870 else if (declarator->kind == cdk_ptrmem) 13871 error ("cannot declare pointer to %q#T member", type); 13872 } 13873 13874 /* We now know that the TYPE_QUALS don't apply to the decl, 13875 but to the target of the pointer. */ 13876 type_quals = TYPE_UNQUALIFIED; 13877 13878 /* This code used to handle METHOD_TYPE, but I don't think it's 13879 possible to get it here anymore. */ 13880 gcc_assert (TREE_CODE (type) != METHOD_TYPE); 13881 if (declarator->kind == cdk_ptrmem 13882 && TREE_CODE (type) == FUNCTION_TYPE) 13883 { 13884 memfn_quals |= type_memfn_quals (type); 13885 type = build_memfn_type (type, 13886 declarator->u.pointer.class_type, 13887 memfn_quals, 13888 rqual); 13889 if (type == error_mark_node) 13890 return error_mark_node; 13891 13892 rqual = REF_QUAL_NONE; 13893 memfn_quals = TYPE_UNQUALIFIED; 13894 } 13895 13896 if (TREE_CODE (type) == FUNCTION_TYPE 13897 && (type_memfn_quals (type) != TYPE_UNQUALIFIED 13898 || type_memfn_rqual (type) != REF_QUAL_NONE)) 13899 error (declarator->kind == cdk_reference 13900 ? G_("cannot declare reference to qualified function type %qT") 13901 : G_("cannot declare pointer to qualified function type %qT"), 13902 type); 13903 13904 /* When the pointed-to type involves components of variable size, 13905 care must be taken to ensure that the size evaluation code is 13906 emitted early enough to dominate all the possible later uses 13907 and late enough for the variables on which it depends to have 13908 been assigned. 13909 13910 This is expected to happen automatically when the pointed-to 13911 type has a name/declaration of it's own, but special attention 13912 is required if the type is anonymous. 13913 13914 We handle the NORMAL and FIELD contexts here by inserting a 13915 dummy statement that just evaluates the size at a safe point 13916 and ensures it is not deferred until e.g. within a deeper 13917 conditional context (c++/43555). 13918 13919 We expect nothing to be needed here for PARM or TYPENAME. 13920 Evaluating the size at this point for TYPENAME would 13921 actually be incorrect, as we might be in the middle of an 13922 expression with side effects on the pointed-to type size 13923 "arguments" prior to the pointer declaration point and the 13924 size evaluation could end up prior to the side effects. */ 13925 13926 if (!TYPE_NAME (type) 13927 && (decl_context == NORMAL || decl_context == FIELD) 13928 && at_function_scope_p () 13929 && variably_modified_type_p (type, NULL_TREE)) 13930 { 13931 TYPE_NAME (type) = build_decl (UNKNOWN_LOCATION, TYPE_DECL, 13932 NULL_TREE, type); 13933 add_decl_expr (TYPE_NAME (type)); 13934 } 13935 13936 if (declarator->kind == cdk_reference) 13937 { 13938 /* In C++0x, the type we are creating a reference to might be 13939 a typedef which is itself a reference type. In that case, 13940 we follow the reference collapsing rules in 13941 [7.1.3/8 dcl.typedef] to create the final reference type: 13942 13943 "If a typedef TD names a type that is a reference to a type 13944 T, an attempt to create the type 'lvalue reference to cv TD' 13945 creates the type 'lvalue reference to T,' while an attempt 13946 to create the type "rvalue reference to cv TD' creates the 13947 type TD." 13948 */ 13949 if (VOID_TYPE_P (type)) 13950 /* We already gave an error. */; 13951 else if (TYPE_REF_P (type)) 13952 { 13953 if (declarator->u.reference.rvalue_ref) 13954 /* Leave type alone. */; 13955 else 13956 type = cp_build_reference_type (TREE_TYPE (type), false); 13957 } 13958 else 13959 type = cp_build_reference_type 13960 (type, declarator->u.reference.rvalue_ref); 13961 13962 /* In C++0x, we need this check for direct reference to 13963 reference declarations, which are forbidden by 13964 [8.3.2/5 dcl.ref]. Reference to reference declarations 13965 are only allowed indirectly through typedefs and template 13966 type arguments. Example: 13967 13968 void foo(int & &); // invalid ref-to-ref decl 13969 13970 typedef int & int_ref; 13971 void foo(int_ref &); // valid ref-to-ref decl 13972 */ 13973 if (inner_declarator && inner_declarator->kind == cdk_reference) 13974 error ("cannot declare reference to %q#T, which is not " 13975 "a typedef or a template type argument", type); 13976 } 13977 else if (TREE_CODE (type) == METHOD_TYPE) 13978 type = build_ptrmemfunc_type (build_pointer_type (type)); 13979 else if (declarator->kind == cdk_ptrmem) 13980 { 13981 gcc_assert (TREE_CODE (declarator->u.pointer.class_type) 13982 != NAMESPACE_DECL); 13983 if (declarator->u.pointer.class_type == error_mark_node) 13984 /* We will already have complained. */ 13985 type = error_mark_node; 13986 else 13987 type = build_ptrmem_type (declarator->u.pointer.class_type, 13988 type); 13989 } 13990 else 13991 type = build_pointer_type (type); 13992 13993 /* Process a list of type modifier keywords (such as 13994 const or volatile) that were given inside the `*' or `&'. */ 13995 13996 if (declarator->u.pointer.qualifiers) 13997 { 13998 type 13999 = cp_build_qualified_type (type, 14000 declarator->u.pointer.qualifiers); 14001 type_quals = cp_type_quals (type); 14002 } 14003 14004 /* Apply C++11 attributes to the pointer, and not to the 14005 type pointed to. This is unlike what is done for GNU 14006 attributes above. It is to comply with [dcl.ptr]/1: 14007 14008 [the optional attribute-specifier-seq (7.6.1) appertains 14009 to the pointer and not to the object pointed to]. */ 14010 if (declarator->std_attributes) 14011 decl_attributes (&type, declarator->std_attributes, 14012 0); 14013 14014 ctype = NULL_TREE; 14015 break; 14016 14017 case cdk_error: 14018 break; 14019 14020 default: 14021 gcc_unreachable (); 14022 } 14023 } 14024 14025 id_loc = declarator ? declarator->id_loc : input_location; 14026 14027 if (innermost_code != cdk_function 14028 /* Don't check this if it can be the artifical decltype(auto) 14029 we created when building a constraint in a compound-requirement: 14030 that the type-constraint is plain is going to be checked in 14031 cp_parser_compound_requirement. */ 14032 && decl_context != TYPENAME 14033 && check_decltype_auto (id_loc, type)) 14034 return error_mark_node; 14035 14036 /* A `constexpr' specifier used in an object declaration declares 14037 the object as `const'. */ 14038 if (constexpr_p && innermost_code != cdk_function) 14039 { 14040 /* DR1688 says that a `constexpr' specifier in combination with 14041 `volatile' is valid. */ 14042 14043 if (!TYPE_REF_P (type)) 14044 { 14045 type_quals |= TYPE_QUAL_CONST; 14046 type = cp_build_qualified_type (type, type_quals); 14047 } 14048 } 14049 14050 if (unqualified_id && TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR 14051 && !FUNC_OR_METHOD_TYPE_P (type) 14052 && !variable_template_p (TREE_OPERAND (unqualified_id, 0))) 14053 { 14054 error ("template-id %qD used as a declarator", 14055 unqualified_id); 14056 unqualified_id = dname; 14057 } 14058 14059 /* If TYPE is a FUNCTION_TYPE, but the function name was explicitly 14060 qualified with a class-name, turn it into a METHOD_TYPE, unless 14061 we know that the function is static. We take advantage of this 14062 opportunity to do other processing that pertains to entities 14063 explicitly declared to be class members. Note that if DECLARATOR 14064 is non-NULL, we know it is a cdk_id declarator; otherwise, we 14065 would not have exited the loop above. */ 14066 if (declarator 14067 && declarator->kind == cdk_id 14068 && declarator->u.id.qualifying_scope 14069 && MAYBE_CLASS_TYPE_P (declarator->u.id.qualifying_scope)) 14070 { 14071 ctype = declarator->u.id.qualifying_scope; 14072 ctype = TYPE_MAIN_VARIANT (ctype); 14073 template_count = num_template_headers_for_class (ctype); 14074 14075 if (ctype == current_class_type) 14076 { 14077 if (friendp) 14078 { 14079 permerror (declspecs->locations[ds_friend], 14080 "member functions are implicitly " 14081 "friends of their class"); 14082 friendp = 0; 14083 } 14084 else 14085 permerror (id_loc, "extra qualification %<%T::%> on member %qs", 14086 ctype, name); 14087 } 14088 else if (/* If the qualifying type is already complete, then we 14089 can skip the following checks. */ 14090 !COMPLETE_TYPE_P (ctype) 14091 && (/* If the function is being defined, then 14092 qualifying type must certainly be complete. */ 14093 funcdef_flag 14094 /* A friend declaration of "T::f" is OK, even if 14095 "T" is a template parameter. But, if this 14096 function is not a friend, the qualifying type 14097 must be a class. */ 14098 || (!friendp && !CLASS_TYPE_P (ctype)) 14099 /* For a declaration, the type need not be 14100 complete, if either it is dependent (since there 14101 is no meaningful definition of complete in that 14102 case) or the qualifying class is currently being 14103 defined. */ 14104 || !(dependent_type_p (ctype) 14105 || currently_open_class (ctype))) 14106 /* Check that the qualifying type is complete. */ 14107 && !complete_type_or_else (ctype, NULL_TREE)) 14108 return error_mark_node; 14109 else if (TREE_CODE (type) == FUNCTION_TYPE) 14110 { 14111 if (current_class_type 14112 && (!friendp || funcdef_flag || initialized)) 14113 { 14114 error_at (id_loc, funcdef_flag || initialized 14115 ? G_("cannot define member function %<%T::%s%> " 14116 "within %qT") 14117 : G_("cannot declare member function %<%T::%s%> " 14118 "within %qT"), 14119 ctype, name, current_class_type); 14120 return error_mark_node; 14121 } 14122 } 14123 else if (typedef_p && current_class_type) 14124 { 14125 error_at (id_loc, "cannot declare member %<%T::%s%> within %qT", 14126 ctype, name, current_class_type); 14127 return error_mark_node; 14128 } 14129 } 14130 14131 if (ctype == NULL_TREE && decl_context == FIELD && friendp == 0) 14132 ctype = current_class_type; 14133 14134 /* Now TYPE has the actual type. */ 14135 14136 if (returned_attrs) 14137 { 14138 if (attrlist) 14139 *attrlist = attr_chainon (returned_attrs, *attrlist); 14140 else 14141 attrlist = &returned_attrs; 14142 } 14143 14144 if (declarator 14145 && declarator->kind == cdk_id 14146 && declarator->std_attributes 14147 && attrlist != NULL) 14148 { 14149 /* [dcl.meaning]/1: The optional attribute-specifier-seq following 14150 a declarator-id appertains to the entity that is declared. */ 14151 if (declarator->std_attributes != error_mark_node) 14152 *attrlist = attr_chainon (declarator->std_attributes, *attrlist); 14153 else 14154 /* We should have already diagnosed the issue (c++/78344). */ 14155 gcc_assert (seen_error ()); 14156 } 14157 14158 /* Handle parameter packs. */ 14159 if (parameter_pack_p) 14160 { 14161 if (decl_context == PARM) 14162 /* Turn the type into a pack expansion.*/ 14163 type = make_pack_expansion (type); 14164 else 14165 error ("non-parameter %qs cannot be a parameter pack", name); 14166 } 14167 14168 if ((decl_context == FIELD || decl_context == PARM) 14169 && !processing_template_decl 14170 && variably_modified_type_p (type, NULL_TREE)) 14171 { 14172 if (decl_context == FIELD) 14173 error_at (id_loc, 14174 "data member may not have variably modified type %qT", type); 14175 else 14176 error_at (id_loc, 14177 "parameter may not have variably modified type %qT", type); 14178 type = error_mark_node; 14179 } 14180 14181 if (explicitp == 1 || (explicitp && friendp)) 14182 { 14183 /* [dcl.fct.spec] (C++11) The explicit specifier shall be used only 14184 in the declaration of a constructor or conversion function within 14185 a class definition. */ 14186 if (!current_class_type) 14187 error_at (declspecs->locations[ds_explicit], 14188 "%<explicit%> outside class declaration"); 14189 else if (friendp) 14190 error_at (declspecs->locations[ds_explicit], 14191 "%<explicit%> in friend declaration"); 14192 else 14193 error_at (declspecs->locations[ds_explicit], 14194 "only declarations of constructors and conversion operators " 14195 "can be %<explicit%>"); 14196 explicitp = 0; 14197 } 14198 14199 if (storage_class == sc_mutable) 14200 { 14201 location_t sloc = declspecs->locations[ds_storage_class]; 14202 if (decl_context != FIELD || friendp) 14203 { 14204 error_at (sloc, "non-member %qs cannot be declared %<mutable%>", 14205 name); 14206 storage_class = sc_none; 14207 } 14208 else if (decl_context == TYPENAME || typedef_p) 14209 { 14210 error_at (sloc, 14211 "non-object member %qs cannot be declared %<mutable%>", 14212 name); 14213 storage_class = sc_none; 14214 } 14215 else if (FUNC_OR_METHOD_TYPE_P (type)) 14216 { 14217 error_at (sloc, "function %qs cannot be declared %<mutable%>", 14218 name); 14219 storage_class = sc_none; 14220 } 14221 else if (staticp) 14222 { 14223 error_at (sloc, "%<static%> %qs cannot be declared %<mutable%>", 14224 name); 14225 storage_class = sc_none; 14226 } 14227 else if (type_quals & TYPE_QUAL_CONST) 14228 { 14229 error_at (sloc, "%<const%> %qs cannot be declared %<mutable%>", 14230 name); 14231 storage_class = sc_none; 14232 } 14233 else if (TYPE_REF_P (type)) 14234 { 14235 permerror (sloc, "reference %qs cannot be declared %<mutable%>", 14236 name); 14237 storage_class = sc_none; 14238 } 14239 } 14240 14241 /* If this is declaring a typedef name, return a TYPE_DECL. */ 14242 if (typedef_p && decl_context != TYPENAME) 14243 { 14244 bool alias_p = decl_spec_seq_has_spec_p (declspecs, ds_alias); 14245 tree decl; 14246 14247 if (funcdef_flag) 14248 { 14249 if (decl_context == NORMAL) 14250 error_at (id_loc, 14251 "typedef may not be a function definition"); 14252 else 14253 error_at (id_loc, 14254 "typedef may not be a member function definition"); 14255 return error_mark_node; 14256 } 14257 14258 /* This declaration: 14259 14260 typedef void f(int) const; 14261 14262 declares a function type which is not a member of any 14263 particular class, but which is cv-qualified; for 14264 example "f S::*" declares a pointer to a const-qualified 14265 member function of S. We record the cv-qualification in the 14266 function type. */ 14267 if ((rqual || memfn_quals) && TREE_CODE (type) == FUNCTION_TYPE) 14268 { 14269 type = apply_memfn_quals (type, memfn_quals, rqual); 14270 14271 /* We have now dealt with these qualifiers. */ 14272 memfn_quals = TYPE_UNQUALIFIED; 14273 rqual = REF_QUAL_NONE; 14274 } 14275 14276 if (type_uses_auto (type)) 14277 { 14278 if (alias_p) 14279 error_at (declspecs->locations[ds_type_spec], 14280 "%<auto%> not allowed in alias declaration"); 14281 else 14282 error_at (declspecs->locations[ds_type_spec], 14283 "typedef declared %<auto%>"); 14284 type = error_mark_node; 14285 } 14286 14287 if (reqs) 14288 error_at (location_of (reqs), "requires-clause on typedef"); 14289 14290 if (id_declarator && declarator->u.id.qualifying_scope) 14291 { 14292 error_at (id_loc, "typedef name may not be a nested-name-specifier"); 14293 type = error_mark_node; 14294 } 14295 14296 if (decl_context == FIELD) 14297 decl = build_lang_decl_loc (id_loc, TYPE_DECL, unqualified_id, type); 14298 else 14299 decl = build_decl (id_loc, TYPE_DECL, unqualified_id, type); 14300 14301 if (decl_context != FIELD) 14302 { 14303 if (!current_function_decl) 14304 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace); 14305 else if (DECL_MAYBE_IN_CHARGE_CDTOR_P (current_function_decl)) 14306 /* The TYPE_DECL is "abstract" because there will be 14307 clones of this constructor/destructor, and there will 14308 be copies of this TYPE_DECL generated in those 14309 clones. The decloning optimization (for space) may 14310 revert this subsequently if it determines that 14311 the clones should share a common implementation. */ 14312 DECL_ABSTRACT_P (decl) = true; 14313 14314 set_originating_module (decl); 14315 } 14316 else if (current_class_type 14317 && constructor_name_p (unqualified_id, current_class_type)) 14318 permerror (id_loc, "ISO C++ forbids nested type %qD with same name " 14319 "as enclosing class", 14320 unqualified_id); 14321 14322 /* If the user declares "typedef struct {...} foo" then the 14323 struct will have an anonymous name. Fill that name in now. 14324 Nothing can refer to it, so nothing needs know about the name 14325 change. */ 14326 if (type != error_mark_node 14327 && unqualified_id 14328 && TYPE_NAME (type) 14329 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL 14330 && TYPE_UNNAMED_P (type) 14331 && declspecs->type_definition_p 14332 && attributes_naming_typedef_ok (*attrlist) 14333 && cp_type_quals (type) == TYPE_UNQUALIFIED) 14334 name_unnamed_type (type, decl); 14335 14336 if (signed_p 14337 || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl))) 14338 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1; 14339 14340 bad_specifiers (decl, BSP_TYPE, virtualp, 14341 memfn_quals != TYPE_UNQUALIFIED, 14342 inlinep, friendp, raises != NULL_TREE, 14343 declspecs->locations); 14344 14345 if (alias_p) 14346 /* Acknowledge that this was written: 14347 `using analias = atype;'. */ 14348 TYPE_DECL_ALIAS_P (decl) = 1; 14349 14350 return decl; 14351 } 14352 14353 /* Detect the case of an array type of unspecified size 14354 which came, as such, direct from a typedef name. 14355 We must copy the type, so that the array's domain can be 14356 individually set by the object's initializer. */ 14357 14358 if (type && typedef_type 14359 && TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type) 14360 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type)) 14361 type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE); 14362 14363 /* Detect where we're using a typedef of function type to declare a 14364 function. PARMS will not be set, so we must create it now. */ 14365 14366 if (type == typedef_type && TREE_CODE (type) == FUNCTION_TYPE) 14367 { 14368 tree decls = NULL_TREE; 14369 tree args; 14370 14371 for (args = TYPE_ARG_TYPES (type); 14372 args && args != void_list_node; 14373 args = TREE_CHAIN (args)) 14374 { 14375 tree decl = cp_build_parm_decl (NULL_TREE, NULL_TREE, 14376 TREE_VALUE (args)); 14377 14378 DECL_CHAIN (decl) = decls; 14379 decls = decl; 14380 } 14381 14382 parms = nreverse (decls); 14383 14384 if (decl_context != TYPENAME) 14385 { 14386 /* The qualifiers on the function type become the qualifiers on 14387 the non-static member function. */ 14388 memfn_quals |= type_memfn_quals (type); 14389 rqual = type_memfn_rqual (type); 14390 type_quals = TYPE_UNQUALIFIED; 14391 raises = TYPE_RAISES_EXCEPTIONS (type); 14392 } 14393 } 14394 14395 /* If this is a type name (such as, in a cast or sizeof), 14396 compute the type and return it now. */ 14397 14398 if (decl_context == TYPENAME) 14399 { 14400 /* Note that here we don't care about type_quals. */ 14401 14402 /* Special case: "friend class foo" looks like a TYPENAME context. */ 14403 if (friendp) 14404 { 14405 if (inlinep) 14406 { 14407 error ("%<inline%> specified for friend class declaration"); 14408 inlinep = 0; 14409 } 14410 14411 if (!current_aggr) 14412 { 14413 /* Don't allow friend declaration without a class-key. */ 14414 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM) 14415 permerror (input_location, "template parameters cannot be friends"); 14416 else if (TREE_CODE (type) == TYPENAME_TYPE) 14417 permerror (input_location, "friend declaration requires class-key, " 14418 "i.e. %<friend class %T::%D%>", 14419 TYPE_CONTEXT (type), TYPENAME_TYPE_FULLNAME (type)); 14420 else 14421 permerror (input_location, "friend declaration requires class-key, " 14422 "i.e. %<friend %#T%>", 14423 type); 14424 } 14425 14426 /* Only try to do this stuff if we didn't already give up. */ 14427 if (type != integer_type_node) 14428 { 14429 /* A friendly class? */ 14430 if (current_class_type) 14431 make_friend_class (current_class_type, TYPE_MAIN_VARIANT (type), 14432 /*complain=*/true); 14433 else 14434 error ("trying to make class %qT a friend of global scope", 14435 type); 14436 14437 type = void_type_node; 14438 } 14439 } 14440 else if (memfn_quals || rqual) 14441 { 14442 if (ctype == NULL_TREE 14443 && TREE_CODE (type) == METHOD_TYPE) 14444 ctype = TYPE_METHOD_BASETYPE (type); 14445 14446 if (ctype) 14447 type = build_memfn_type (type, ctype, memfn_quals, rqual); 14448 /* Core issue #547: need to allow this in template type args. 14449 Allow it in general in C++11 for alias-declarations. */ 14450 else if ((template_type_arg || cxx_dialect >= cxx11) 14451 && TREE_CODE (type) == FUNCTION_TYPE) 14452 type = apply_memfn_quals (type, memfn_quals, rqual); 14453 else 14454 error ("invalid qualifiers on non-member function type"); 14455 } 14456 14457 if (reqs) 14458 error_at (location_of (reqs), "requires-clause on type-id"); 14459 14460 return type; 14461 } 14462 else if (unqualified_id == NULL_TREE && decl_context != PARM 14463 && decl_context != CATCHPARM 14464 && TREE_CODE (type) != UNION_TYPE 14465 && ! bitfield 14466 && innermost_code != cdk_decomp) 14467 { 14468 error ("abstract declarator %qT used as declaration", type); 14469 return error_mark_node; 14470 } 14471 14472 if (!FUNC_OR_METHOD_TYPE_P (type)) 14473 { 14474 /* Only functions may be declared using an operator-function-id. */ 14475 if (dname && IDENTIFIER_ANY_OP_P (dname)) 14476 { 14477 error_at (id_loc, "declaration of %qD as non-function", dname); 14478 return error_mark_node; 14479 } 14480 14481 if (reqs) 14482 error_at (location_of (reqs), 14483 "requires-clause on declaration of non-function type %qT", 14484 type); 14485 } 14486 14487 /* We don't check parameter types here because we can emit a better 14488 error message later. */ 14489 if (decl_context != PARM) 14490 { 14491 type = check_var_type (unqualified_id, type, id_loc); 14492 if (type == error_mark_node) 14493 return error_mark_node; 14494 } 14495 14496 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL 14497 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */ 14498 14499 if (decl_context == PARM || decl_context == CATCHPARM) 14500 { 14501 if (ctype || in_namespace) 14502 error ("cannot use %<::%> in parameter declaration"); 14503 14504 tree auto_node = type_uses_auto (type); 14505 if (auto_node && !(cxx_dialect >= cxx17 && template_parm_flag)) 14506 { 14507 bool err_p = true; 14508 if (cxx_dialect >= cxx14) 14509 { 14510 if (decl_context == PARM && AUTO_IS_DECLTYPE (auto_node)) 14511 error_at (typespec_loc, 14512 "cannot declare a parameter with %<decltype(auto)%>"); 14513 else if (tree c = CLASS_PLACEHOLDER_TEMPLATE (auto_node)) 14514 { 14515 auto_diagnostic_group g; 14516 gcc_rich_location richloc (typespec_loc); 14517 richloc.add_fixit_insert_after ("<>"); 14518 error_at (&richloc, 14519 "missing template argument list after %qE; template " 14520 "placeholder not permitted in parameter", c); 14521 if (decl_context == PARM && cxx_dialect >= cxx20) 14522 inform (typespec_loc, "or use %<auto%> for an " 14523 "abbreviated function template"); 14524 inform (DECL_SOURCE_LOCATION (c), "%qD declared here", c); 14525 } 14526 else if (decl_context == CATCHPARM || template_parm_flag) 14527 error_at (typespec_loc, 14528 "%<auto%> parameter not permitted in this context"); 14529 else 14530 /* Do not issue an error while tentatively parsing a function 14531 parameter: for T t(auto(a), 42);, when we just saw the 1st 14532 parameter, we don't know yet that this construct won't be 14533 a function declaration. Defer the checking to 14534 cp_parser_parameter_declaration_clause. */ 14535 err_p = false; 14536 } 14537 else 14538 error_at (typespec_loc, "parameter declared %<auto%>"); 14539 if (err_p) 14540 type = error_mark_node; 14541 } 14542 14543 /* A parameter declared as an array of T is really a pointer to T. 14544 One declared as a function is really a pointer to a function. 14545 One declared as a member is really a pointer to member. */ 14546 14547 if (TREE_CODE (type) == ARRAY_TYPE) 14548 { 14549 /* Transfer const-ness of array into that of type pointed to. */ 14550 type = build_pointer_type (TREE_TYPE (type)); 14551 type_quals = TYPE_UNQUALIFIED; 14552 array_parameter_p = true; 14553 } 14554 else if (TREE_CODE (type) == FUNCTION_TYPE) 14555 type = build_pointer_type (type); 14556 } 14557 14558 if (ctype && TREE_CODE (type) == FUNCTION_TYPE && staticp < 2 14559 /* Don't convert xobj member functions to METHOD_TYPE. */ 14560 && !is_xobj_member_function 14561 && !(unqualified_id 14562 && identifier_p (unqualified_id) 14563 && IDENTIFIER_NEWDEL_OP_P (unqualified_id))) 14564 { 14565 cp_cv_quals real_quals = memfn_quals; 14566 if (cxx_dialect < cxx14 && constexpr_p 14567 && sfk != sfk_constructor && sfk != sfk_destructor) 14568 real_quals |= TYPE_QUAL_CONST; 14569 type = build_memfn_type (type, ctype, real_quals, rqual); 14570 } 14571 14572 { 14573 tree decl = NULL_TREE; 14574 14575 if (decl_context == PARM) 14576 { 14577 decl = cp_build_parm_decl (NULL_TREE, unqualified_id, type); 14578 DECL_ARRAY_PARAMETER_P (decl) = array_parameter_p; 14579 14580 bad_specifiers (decl, BSP_PARM, virtualp, 14581 memfn_quals != TYPE_UNQUALIFIED, 14582 inlinep, friendp, raises != NULL_TREE, 14583 declspecs->locations); 14584 } 14585 else if (decl_context == FIELD) 14586 { 14587 if (!staticp && !friendp && !FUNC_OR_METHOD_TYPE_P (type)) 14588 if (tree auto_node = type_uses_auto (type)) 14589 { 14590 location_t tloc = declspecs->locations[ds_type_spec]; 14591 if (CLASS_PLACEHOLDER_TEMPLATE (auto_node)) 14592 error_at (tloc, "invalid use of template-name %qE without an " 14593 "argument list", 14594 CLASS_PLACEHOLDER_TEMPLATE (auto_node)); 14595 else 14596 error_at (tloc, "non-static data member declared with " 14597 "placeholder %qT", auto_node); 14598 type = error_mark_node; 14599 } 14600 14601 /* The C99 flexible array extension. */ 14602 if (!staticp && TREE_CODE (type) == ARRAY_TYPE 14603 && TYPE_DOMAIN (type) == NULL_TREE) 14604 { 14605 if (ctype 14606 && (TREE_CODE (ctype) == UNION_TYPE 14607 || TREE_CODE (ctype) == QUAL_UNION_TYPE)) 14608 { 14609 error_at (id_loc, "flexible array member in union"); 14610 type = error_mark_node; 14611 } 14612 else 14613 { 14614 /* Array is a flexible member. */ 14615 if (name) 14616 pedwarn (id_loc, OPT_Wpedantic, 14617 "ISO C++ forbids flexible array member %qs", name); 14618 else 14619 pedwarn (input_location, OPT_Wpedantic, 14620 "ISO C++ forbids flexible array members"); 14621 14622 /* Flexible array member has a null domain. */ 14623 type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE); 14624 } 14625 } 14626 14627 if (type == error_mark_node) 14628 { 14629 /* Happens when declaring arrays of sizes which 14630 are error_mark_node, for example. */ 14631 decl = NULL_TREE; 14632 } 14633 else if (in_namespace && !friendp) 14634 { 14635 /* Something like struct S { int N::j; }; */ 14636 error_at (id_loc, "invalid use of %<::%>"); 14637 return error_mark_node; 14638 } 14639 else if (FUNC_OR_METHOD_TYPE_P (type) && unqualified_id) 14640 { 14641 int publicp = 0; 14642 tree function_context; 14643 14644 if (friendp == 0) 14645 { 14646 /* This should never happen in pure C++ (the check 14647 could be an assert). It could happen in 14648 Objective-C++ if someone writes invalid code that 14649 uses a function declaration for an instance 14650 variable or property (instance variables and 14651 properties are parsed as FIELD_DECLs, but they are 14652 part of an Objective-C class, not a C++ class). 14653 That code is invalid and is caught by this 14654 check. */ 14655 if (!ctype) 14656 { 14657 error ("declaration of function %qD in invalid context", 14658 unqualified_id); 14659 return error_mark_node; 14660 } 14661 14662 /* ``A union may [ ... ] not [ have ] virtual functions.'' 14663 ARM 9.5 */ 14664 if (virtualp && TREE_CODE (ctype) == UNION_TYPE) 14665 { 14666 error_at (declspecs->locations[ds_virtual], 14667 "function %qD declared %<virtual%> inside a union", 14668 unqualified_id); 14669 return error_mark_node; 14670 } 14671 14672 if (virtualp 14673 && identifier_p (unqualified_id) 14674 && IDENTIFIER_NEWDEL_OP_P (unqualified_id)) 14675 { 14676 error_at (declspecs->locations[ds_virtual], 14677 "%qD cannot be declared %<virtual%>, since it " 14678 "is always static", unqualified_id); 14679 virtualp = 0; 14680 } 14681 } 14682 14683 /* Check that the name used for a destructor makes sense. */ 14684 if (sfk == sfk_destructor) 14685 { 14686 tree uqname = id_declarator->u.id.unqualified_name; 14687 14688 if (!ctype) 14689 { 14690 gcc_assert (friendp); 14691 error_at (id_loc, "expected qualified name in friend " 14692 "declaration for destructor %qD", uqname); 14693 return error_mark_node; 14694 } 14695 14696 if (!check_dtor_name (ctype, TREE_OPERAND (uqname, 0))) 14697 { 14698 error_at (id_loc, "declaration of %qD as member of %qT", 14699 uqname, ctype); 14700 return error_mark_node; 14701 } 14702 if (concept_p) 14703 { 14704 error_at (declspecs->locations[ds_concept], 14705 "a destructor cannot be %qs", "concept"); 14706 return error_mark_node; 14707 } 14708 if (constexpr_p && cxx_dialect < cxx20) 14709 { 14710 error_at (declspecs->locations[ds_constexpr], 14711 "%<constexpr%> destructors only available" 14712 " with %<-std=c++20%> or %<-std=gnu++20%>"); 14713 return error_mark_node; 14714 } 14715 if (consteval_p) 14716 { 14717 error_at (declspecs->locations[ds_consteval], 14718 "a destructor cannot be %qs", "consteval"); 14719 return error_mark_node; 14720 } 14721 } 14722 else if (sfk == sfk_constructor && friendp && !ctype) 14723 { 14724 error ("expected qualified name in friend declaration " 14725 "for constructor %qD", 14726 id_declarator->u.id.unqualified_name); 14727 return error_mark_node; 14728 } 14729 if (sfk == sfk_constructor) 14730 if (concept_p) 14731 { 14732 error_at (declspecs->locations[ds_concept], 14733 "a constructor cannot be %<concept%>"); 14734 return error_mark_node; 14735 } 14736 if (concept_p) 14737 { 14738 error_at (declspecs->locations[ds_concept], 14739 "a concept cannot be a member function"); 14740 concept_p = false; 14741 } 14742 else if (consteval_p 14743 && identifier_p (unqualified_id) 14744 && IDENTIFIER_NEWDEL_OP_P (unqualified_id)) 14745 { 14746 error_at (declspecs->locations[ds_consteval], 14747 "%qD cannot be %qs", unqualified_id, "consteval"); 14748 consteval_p = false; 14749 } 14750 14751 if (TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR) 14752 { 14753 tree tmpl = TREE_OPERAND (unqualified_id, 0); 14754 if (variable_template_p (tmpl)) 14755 { 14756 error_at (id_loc, "specialization of variable template " 14757 "%qD declared as function", tmpl); 14758 inform (DECL_SOURCE_LOCATION (tmpl), 14759 "variable template declared here"); 14760 return error_mark_node; 14761 } 14762 } 14763 14764 /* Tell grokfndecl if it needs to set TREE_PUBLIC on the node. */ 14765 function_context 14766 = (ctype != NULL_TREE 14767 ? decl_function_context (TYPE_MAIN_DECL (ctype)) : NULL_TREE); 14768 publicp = ((! friendp || ! staticp) 14769 && function_context == NULL_TREE); 14770 14771 decl = grokfndecl (ctype, type, 14772 TREE_CODE (unqualified_id) != TEMPLATE_ID_EXPR 14773 ? unqualified_id : dname, 14774 parms, 14775 unqualified_id, 14776 declspecs, 14777 reqs, 14778 virtualp, flags, memfn_quals, rqual, raises, 14779 friendp ? -1 : 0, friendp, publicp, 14780 inlinep | (2 * constexpr_p) | (4 * concept_p) 14781 | (8 * consteval_p), 14782 initialized == SD_DELETED, 14783 is_xobj_member_function, sfk, 14784 funcdef_flag, late_return_type_p, 14785 template_count, in_namespace, 14786 attrlist, id_loc); 14787 decl = set_virt_specifiers (decl, virt_specifiers); 14788 if (decl == NULL_TREE) 14789 return error_mark_node; 14790 #if 0 14791 /* This clobbers the attrs stored in `decl' from `attrlist'. */ 14792 /* The decl and setting of decl_attr is also turned off. */ 14793 decl = build_decl_attribute_variant (decl, decl_attr); 14794 #endif 14795 14796 /* [class.conv.ctor] 14797 14798 A constructor declared without the function-specifier 14799 explicit that can be called with a single parameter 14800 specifies a conversion from the type of its first 14801 parameter to the type of its class. Such a constructor 14802 is called a converting constructor. */ 14803 if (explicitp == 2) 14804 DECL_NONCONVERTING_P (decl) = 1; 14805 14806 if (declspecs->explicit_specifier) 14807 store_explicit_specifier (decl, declspecs->explicit_specifier); 14808 } 14809 else if (!staticp 14810 && ((current_class_type 14811 && same_type_p (TYPE_MAIN_VARIANT (type), 14812 current_class_type)) 14813 || (!dependent_type_p (type) 14814 && !COMPLETE_TYPE_P (complete_type (type)) 14815 && (!complete_or_array_type_p (type) 14816 || initialized == SD_UNINITIALIZED)))) 14817 { 14818 if (TREE_CODE (type) != ARRAY_TYPE 14819 || !COMPLETE_TYPE_P (TREE_TYPE (type))) 14820 { 14821 if (unqualified_id) 14822 { 14823 error_at (id_loc, "field %qD has incomplete type %qT", 14824 unqualified_id, type); 14825 cxx_incomplete_type_inform (strip_array_types (type)); 14826 } 14827 else 14828 error ("name %qT has incomplete type", type); 14829 14830 type = error_mark_node; 14831 decl = NULL_TREE; 14832 } 14833 } 14834 else if (!verify_type_context (input_location, 14835 staticp 14836 ? TCTX_STATIC_STORAGE 14837 : TCTX_FIELD, type)) 14838 { 14839 type = error_mark_node; 14840 decl = NULL_TREE; 14841 } 14842 else 14843 { 14844 if (friendp) 14845 { 14846 if (unqualified_id) 14847 error_at (id_loc, 14848 "%qE is neither function nor member function; " 14849 "cannot be declared friend", unqualified_id); 14850 else 14851 error ("unnamed field is neither function nor member " 14852 "function; cannot be declared friend"); 14853 return error_mark_node; 14854 } 14855 decl = NULL_TREE; 14856 } 14857 14858 if (friendp) 14859 { 14860 /* Packages tend to use GNU attributes on friends, so we only 14861 warn for standard attributes. */ 14862 if (attrlist 14863 && !funcdef_flag 14864 && cxx11_attribute_p (*attrlist) 14865 && !all_attributes_are_contracts_p (*attrlist)) 14866 { 14867 *attrlist = NULL_TREE; 14868 if (warning_at (id_loc, OPT_Wattributes, "attribute ignored")) 14869 inform (id_loc, "an attribute that appertains to a friend " 14870 "declaration that is not a definition is ignored"); 14871 } 14872 /* Friends are treated specially. */ 14873 if (ctype == current_class_type) 14874 ; /* We already issued a permerror. */ 14875 else if (decl && DECL_NAME (decl)) 14876 { 14877 set_originating_module (decl, true); 14878 14879 if (initialized) 14880 /* Kludge: We need funcdef_flag to be true in do_friend for 14881 in-class defaulted functions, but that breaks grokfndecl. 14882 So set it here. */ 14883 funcdef_flag = true; 14884 14885 cplus_decl_attributes (&decl, *attrlist, 0); 14886 *attrlist = NULL_TREE; 14887 14888 tree scope = ctype ? ctype : in_namespace; 14889 decl = do_friend (scope, unqualified_id, decl, 14890 flags, funcdef_flag); 14891 return decl; 14892 } 14893 else 14894 return error_mark_node; 14895 } 14896 14897 /* Structure field. It may not be a function, except for C++. */ 14898 14899 if (decl == NULL_TREE) 14900 { 14901 if (staticp) 14902 { 14903 /* C++ allows static class members. All other work 14904 for this is done by grokfield. */ 14905 decl = build_lang_decl_loc (id_loc, VAR_DECL, 14906 dname, type); 14907 if (unqualified_id 14908 && TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR) 14909 { 14910 decl = check_explicit_specialization (unqualified_id, decl, 14911 template_count, 14912 concept_p * 8); 14913 if (decl == error_mark_node) 14914 return error_mark_node; 14915 } 14916 set_linkage_for_static_data_member (decl); 14917 if (concept_p) 14918 error_at (declspecs->locations[ds_concept], 14919 "static data member %qE declared %qs", 14920 unqualified_id, "concept"); 14921 else if (constexpr_p && !initialized) 14922 { 14923 error_at (DECL_SOURCE_LOCATION (decl), 14924 "%<constexpr%> static data member %qD must " 14925 "have an initializer", decl); 14926 constexpr_p = false; 14927 } 14928 if (consteval_p) 14929 error_at (declspecs->locations[ds_consteval], 14930 "static data member %qE declared %qs", 14931 unqualified_id, "consteval"); 14932 14933 if (inlinep) 14934 mark_inline_variable (decl, declspecs->locations[ds_inline]); 14935 14936 if (!DECL_VAR_DECLARED_INLINE_P (decl) 14937 && !(cxx_dialect >= cxx17 && constexpr_p)) 14938 /* Even if there is an in-class initialization, DECL 14939 is considered undefined until an out-of-class 14940 definition is provided, unless this is an inline 14941 variable. */ 14942 DECL_EXTERNAL (decl) = 1; 14943 14944 if (thread_p) 14945 { 14946 CP_DECL_THREAD_LOCAL_P (decl) = true; 14947 if (!processing_template_decl) 14948 set_decl_tls_model (decl, decl_default_tls_model (decl)); 14949 if (declspecs->gnu_thread_keyword_p) 14950 SET_DECL_GNU_TLS_P (decl); 14951 } 14952 14953 /* Set the constraints on the declaration. */ 14954 bool memtmpl = (current_template_depth 14955 > template_class_depth (current_class_type)); 14956 if (memtmpl) 14957 { 14958 tree ci = current_template_constraints (); 14959 set_constraints (decl, ci); 14960 } 14961 } 14962 else 14963 { 14964 if (concept_p) 14965 { 14966 error_at (declspecs->locations[ds_concept], 14967 "non-static data member %qE declared %qs", 14968 unqualified_id, "concept"); 14969 concept_p = false; 14970 constexpr_p = false; 14971 } 14972 else if (constexpr_p) 14973 { 14974 error_at (declspecs->locations[ds_constexpr], 14975 "non-static data member %qE declared %qs", 14976 unqualified_id, "constexpr"); 14977 constexpr_p = false; 14978 } 14979 if (constinit_p) 14980 { 14981 error_at (declspecs->locations[ds_constinit], 14982 "non-static data member %qE declared %qs", 14983 unqualified_id, "constinit"); 14984 constinit_p = false; 14985 } 14986 if (consteval_p) 14987 { 14988 error_at (declspecs->locations[ds_consteval], 14989 "non-static data member %qE declared %qs", 14990 unqualified_id, "consteval"); 14991 consteval_p = false; 14992 } 14993 decl = build_decl (id_loc, FIELD_DECL, unqualified_id, type); 14994 DECL_NONADDRESSABLE_P (decl) = bitfield; 14995 if (bitfield && !unqualified_id) 14996 DECL_PADDING_P (decl) = 1; 14997 14998 if (storage_class == sc_mutable) 14999 { 15000 DECL_MUTABLE_P (decl) = 1; 15001 storage_class = sc_none; 15002 } 15003 15004 if (initialized) 15005 { 15006 /* An attempt is being made to initialize a non-static 15007 member. This is new in C++11. */ 15008 maybe_warn_cpp0x (CPP0X_NSDMI, init_loc); 15009 15010 /* If this has been parsed with static storage class, but 15011 errors forced staticp to be cleared, ensure NSDMI is 15012 not present. */ 15013 if (declspecs->storage_class == sc_static) 15014 DECL_INITIAL (decl) = error_mark_node; 15015 } 15016 } 15017 15018 bad_specifiers (decl, BSP_FIELD, virtualp, 15019 memfn_quals != TYPE_UNQUALIFIED, 15020 staticp ? false : inlinep, friendp, 15021 raises != NULL_TREE, 15022 declspecs->locations); 15023 } 15024 } 15025 else if (FUNC_OR_METHOD_TYPE_P (type)) 15026 { 15027 tree original_name; 15028 int publicp = 0; 15029 15030 if (!unqualified_id) 15031 return error_mark_node; 15032 15033 if (TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR) 15034 original_name = dname; 15035 else 15036 original_name = unqualified_id; 15037 // FIXME:gcc_assert (original_name == dname); 15038 15039 if (storage_class == sc_auto) 15040 error_at (declspecs->locations[ds_storage_class], 15041 "storage class %<auto%> invalid for function %qs", name); 15042 else if (storage_class == sc_register) 15043 error_at (declspecs->locations[ds_storage_class], 15044 "storage class %<register%> invalid for function %qs", 15045 name); 15046 else if (thread_p) 15047 { 15048 if (declspecs->gnu_thread_keyword_p) 15049 error_at (declspecs->locations[ds_thread], 15050 "storage class %<__thread%> invalid for function %qs", 15051 name); 15052 else 15053 error_at (declspecs->locations[ds_thread], 15054 "storage class %<thread_local%> invalid for " 15055 "function %qs", name); 15056 } 15057 else if (constinit_p) 15058 error_at (declspecs->locations[ds_constinit], 15059 "%<constinit%> specifier invalid for function %qs", name); 15060 15061 if (virt_specifiers) 15062 error ("virt-specifiers in %qs not allowed outside a class " 15063 "definition", name); 15064 /* Function declaration not at top level. 15065 Storage classes other than `extern' are not allowed 15066 and `extern' makes no difference. */ 15067 if (! toplevel_bindings_p () 15068 && (storage_class == sc_static 15069 || decl_spec_seq_has_spec_p (declspecs, ds_inline)) 15070 && pedantic) 15071 { 15072 if (storage_class == sc_static) 15073 pedwarn (declspecs->locations[ds_storage_class], OPT_Wpedantic, 15074 "%<static%> specifier invalid for function %qs " 15075 "declared out of global scope", name); 15076 else 15077 pedwarn (declspecs->locations[ds_inline], OPT_Wpedantic, 15078 "%<inline%> specifier invalid for function %qs " 15079 "declared out of global scope", name); 15080 } 15081 15082 if (ctype == NULL_TREE) 15083 { 15084 if (virtualp) 15085 { 15086 error ("virtual non-class function %qs", name); 15087 virtualp = 0; 15088 } 15089 else if (sfk == sfk_constructor 15090 || sfk == sfk_destructor) 15091 { 15092 error (funcdef_flag 15093 ? G_("%qs defined in a non-class scope") 15094 : G_("%qs declared in a non-class scope"), name); 15095 sfk = sfk_none; 15096 } 15097 } 15098 if (consteval_p 15099 && identifier_p (unqualified_id) 15100 && IDENTIFIER_NEWDEL_OP_P (unqualified_id)) 15101 { 15102 error_at (declspecs->locations[ds_consteval], 15103 "%qD cannot be %qs", unqualified_id, "consteval"); 15104 consteval_p = false; 15105 } 15106 15107 /* Record whether the function is public. */ 15108 publicp = (ctype != NULL_TREE 15109 || storage_class != sc_static); 15110 15111 decl = grokfndecl (ctype, type, original_name, parms, unqualified_id, 15112 declspecs, 15113 reqs, virtualp, flags, memfn_quals, rqual, raises, 15114 1, friendp, 15115 publicp, 15116 inlinep | (2 * constexpr_p) | (4 * concept_p) 15117 | (8 * consteval_p), 15118 initialized == SD_DELETED, 15119 is_xobj_member_function, sfk, 15120 funcdef_flag, 15121 late_return_type_p, 15122 template_count, in_namespace, attrlist, 15123 id_loc); 15124 if (decl == NULL_TREE) 15125 return error_mark_node; 15126 15127 if (explicitp == 2) 15128 DECL_NONCONVERTING_P (decl) = 1; 15129 if (staticp == 1) 15130 { 15131 int invalid_static = 0; 15132 15133 /* Don't allow a static member function in a class, and forbid 15134 declaring main to be static. */ 15135 if (TREE_CODE (type) == METHOD_TYPE) 15136 { 15137 permerror (input_location, "cannot declare member function %qD to have " 15138 "static linkage", decl); 15139 invalid_static = 1; 15140 } 15141 else if (current_function_decl) 15142 { 15143 /* 7.1.1: There can be no static function declarations within a 15144 block. */ 15145 error_at (declspecs->locations[ds_storage_class], 15146 "cannot declare static function inside another function"); 15147 invalid_static = 1; 15148 } 15149 15150 if (invalid_static) 15151 { 15152 staticp = 0; 15153 storage_class = sc_none; 15154 } 15155 } 15156 if (declspecs->explicit_specifier) 15157 store_explicit_specifier (decl, declspecs->explicit_specifier); 15158 } 15159 else 15160 { 15161 /* It's a variable. */ 15162 15163 /* An uninitialized decl with `extern' is a reference. */ 15164 decl = grokvardecl (type, dname, unqualified_id, 15165 declspecs, 15166 initialized, 15167 type_quals, 15168 inlinep, 15169 concept_p, 15170 template_count, 15171 ctype ? ctype : in_namespace, 15172 id_loc); 15173 if (decl == NULL_TREE) 15174 return error_mark_node; 15175 15176 bad_specifiers (decl, BSP_VAR, virtualp, 15177 memfn_quals != TYPE_UNQUALIFIED, 15178 inlinep, friendp, raises != NULL_TREE, 15179 declspecs->locations); 15180 15181 if (ctype) 15182 { 15183 DECL_CONTEXT (decl) = ctype; 15184 if (staticp == 1) 15185 { 15186 permerror (declspecs->locations[ds_storage_class], 15187 "%<static%> may not be used when defining " 15188 "(as opposed to declaring) a static data member"); 15189 staticp = 0; 15190 storage_class = sc_none; 15191 } 15192 if (storage_class == sc_register && TREE_STATIC (decl)) 15193 { 15194 error ("static member %qD declared %<register%>", decl); 15195 storage_class = sc_none; 15196 } 15197 if (storage_class == sc_extern && pedantic) 15198 { 15199 pedwarn (input_location, OPT_Wpedantic, 15200 "cannot explicitly declare member %q#D to have " 15201 "extern linkage", decl); 15202 storage_class = sc_none; 15203 } 15204 } 15205 else if (constexpr_p && DECL_EXTERNAL (decl)) 15206 { 15207 error_at (DECL_SOURCE_LOCATION (decl), 15208 "declaration of %<constexpr%> variable %qD " 15209 "is not a definition", decl); 15210 constexpr_p = false; 15211 } 15212 if (consteval_p) 15213 { 15214 error_at (DECL_SOURCE_LOCATION (decl), 15215 "a variable cannot be declared %<consteval%>"); 15216 consteval_p = false; 15217 } 15218 15219 if (inlinep) 15220 mark_inline_variable (decl, declspecs->locations[ds_inline]); 15221 if (innermost_code == cdk_decomp) 15222 { 15223 gcc_assert (declarator && declarator->kind == cdk_decomp); 15224 DECL_SOURCE_LOCATION (decl) = id_loc; 15225 DECL_ARTIFICIAL (decl) = 1; 15226 fit_decomposition_lang_decl (decl, NULL_TREE); 15227 } 15228 } 15229 15230 if (VAR_P (decl) && !initialized) 15231 if (tree auto_node = type_uses_auto (type)) 15232 if (!CLASS_PLACEHOLDER_TEMPLATE (auto_node)) 15233 { 15234 location_t loc = declspecs->locations[ds_type_spec]; 15235 error_at (loc, "declaration of %q#D has no initializer", decl); 15236 TREE_TYPE (decl) = error_mark_node; 15237 } 15238 15239 if (storage_class == sc_extern && initialized && !funcdef_flag) 15240 { 15241 if (toplevel_bindings_p ()) 15242 { 15243 /* It's common practice (and completely valid) to have a const 15244 be initialized and declared extern. */ 15245 if (!(type_quals & TYPE_QUAL_CONST)) 15246 warning_at (DECL_SOURCE_LOCATION (decl), 0, 15247 "%qs initialized and declared %<extern%>", name); 15248 } 15249 else 15250 { 15251 error_at (DECL_SOURCE_LOCATION (decl), 15252 "%qs has both %<extern%> and initializer", name); 15253 return error_mark_node; 15254 } 15255 } 15256 15257 /* Record `register' declaration for warnings on & 15258 and in case doing stupid register allocation. */ 15259 15260 if (storage_class == sc_register) 15261 { 15262 DECL_REGISTER (decl) = 1; 15263 /* Warn about register storage specifiers on PARM_DECLs. */ 15264 if (TREE_CODE (decl) == PARM_DECL) 15265 { 15266 if (cxx_dialect >= cxx17) 15267 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wregister, 15268 "ISO C++17 does not allow %<register%> storage " 15269 "class specifier"); 15270 else 15271 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wregister, 15272 "%<register%> storage class specifier used"); 15273 } 15274 } 15275 else if (storage_class == sc_extern) 15276 DECL_THIS_EXTERN (decl) = 1; 15277 else if (storage_class == sc_static) 15278 DECL_THIS_STATIC (decl) = 1; 15279 15280 if (VAR_P (decl)) 15281 { 15282 /* Set constexpr flag on vars (functions got it in grokfndecl). */ 15283 if (constexpr_p) 15284 DECL_DECLARED_CONSTEXPR_P (decl) = true; 15285 /* And the constinit flag (which only applies to variables). */ 15286 else if (constinit_p) 15287 DECL_DECLARED_CONSTINIT_P (decl) = true; 15288 } 15289 else if (TREE_CODE (decl) == FUNCTION_DECL) 15290 { 15291 /* If we saw a return type, record its location. */ 15292 location_t loc = declspecs->locations[ds_type_spec]; 15293 if (loc == UNKNOWN_LOCATION) 15294 /* Build DECL_RESULT in start_preparsed_function. */; 15295 else if (!DECL_RESULT (decl)) 15296 { 15297 tree restype = TREE_TYPE (TREE_TYPE (decl)); 15298 tree resdecl = build_decl (loc, RESULT_DECL, 0, restype); 15299 DECL_ARTIFICIAL (resdecl) = 1; 15300 DECL_IGNORED_P (resdecl) = 1; 15301 DECL_RESULT (decl) = resdecl; 15302 } 15303 else if (funcdef_flag) 15304 DECL_SOURCE_LOCATION (DECL_RESULT (decl)) = loc; 15305 } 15306 15307 /* Record constancy and volatility on the DECL itself . There's 15308 no need to do this when processing a template; we'll do this 15309 for the instantiated declaration based on the type of DECL. */ 15310 if (!processing_template_decl 15311 /* Don't do it for instantiated variable templates either, 15312 cp_apply_type_quals_to_decl should have been called on it 15313 already and might have been overridden in cp_finish_decl 15314 if initializer needs runtime initialization. */ 15315 && (!VAR_P (decl) || !DECL_TEMPLATE_INSTANTIATED (decl))) 15316 cp_apply_type_quals_to_decl (type_quals, decl); 15317 15318 return decl; 15319 } 15320 } 15321 15322 /* Subroutine of start_function. Ensure that each of the parameter 15324 types (as listed in PARMS) is complete, as is required for a 15325 function definition. */ 15326 15327 static void 15328 require_complete_types_for_parms (tree parms) 15329 { 15330 for (; parms; parms = DECL_CHAIN (parms)) 15331 { 15332 if (dependent_type_p (TREE_TYPE (parms))) 15333 continue; 15334 if (!VOID_TYPE_P (TREE_TYPE (parms)) 15335 && complete_type_or_else (TREE_TYPE (parms), parms)) 15336 { 15337 relayout_decl (parms); 15338 DECL_ARG_TYPE (parms) = type_passed_as (TREE_TYPE (parms)); 15339 15340 abstract_virtuals_error (parms, TREE_TYPE (parms)); 15341 maybe_warn_parm_abi (TREE_TYPE (parms), 15342 DECL_SOURCE_LOCATION (parms)); 15343 } 15344 else 15345 /* grokparms or complete_type_or_else will have already issued 15346 an error. */ 15347 TREE_TYPE (parms) = error_mark_node; 15348 } 15349 } 15350 15351 /* Returns nonzero if T is a local variable. */ 15352 15353 int 15354 local_variable_p (const_tree t) 15355 { 15356 if ((VAR_P (t) 15357 && (DECL_LOCAL_DECL_P (t) 15358 || !DECL_CONTEXT (t) 15359 || TREE_CODE (DECL_CONTEXT (t)) == FUNCTION_DECL)) 15360 || (TREE_CODE (t) == PARM_DECL)) 15361 return 1; 15362 15363 return 0; 15364 } 15365 15366 /* Like local_variable_p, but suitable for use as a tree-walking 15367 function. */ 15368 15369 static tree 15370 local_variable_p_walkfn (tree *tp, int *walk_subtrees, 15371 void * /*data*/) 15372 { 15373 if (unevaluated_p (TREE_CODE (*tp))) 15374 { 15375 /* DR 2082 permits local variables in unevaluated contexts 15376 within a default argument. */ 15377 *walk_subtrees = 0; 15378 return NULL_TREE; 15379 } 15380 15381 if (local_variable_p (*tp) 15382 && (!DECL_ARTIFICIAL (*tp) || DECL_NAME (*tp) == this_identifier)) 15383 return *tp; 15384 else if (TYPE_P (*tp)) 15385 *walk_subtrees = 0; 15386 15387 return NULL_TREE; 15388 } 15389 15390 /* Check that ARG, which is a default-argument expression for a 15391 parameter DECL, is valid. Returns ARG, or ERROR_MARK_NODE, if 15392 something goes wrong. DECL may also be a _TYPE node, rather than a 15393 DECL, if there is no DECL available. */ 15394 15395 tree 15396 check_default_argument (tree decl, tree arg, tsubst_flags_t complain) 15397 { 15398 tree var; 15399 tree decl_type; 15400 15401 if (TREE_CODE (arg) == DEFERRED_PARSE) 15402 /* We get a DEFERRED_PARSE when looking at an in-class declaration 15403 with a default argument. Ignore the argument for now; we'll 15404 deal with it after the class is complete. */ 15405 return arg; 15406 15407 if (TYPE_P (decl)) 15408 { 15409 decl_type = decl; 15410 decl = NULL_TREE; 15411 } 15412 else 15413 decl_type = TREE_TYPE (decl); 15414 15415 if (arg == error_mark_node 15416 || decl == error_mark_node 15417 || TREE_TYPE (arg) == error_mark_node 15418 || decl_type == error_mark_node) 15419 /* Something already went wrong. There's no need to check 15420 further. */ 15421 return error_mark_node; 15422 15423 /* [dcl.fct.default] 15424 15425 A default argument expression is implicitly converted to the 15426 parameter type. */ 15427 ++cp_unevaluated_operand; 15428 /* Avoid digest_init clobbering the initializer. */ 15429 tree carg = BRACE_ENCLOSED_INITIALIZER_P (arg) ? unshare_expr (arg): arg; 15430 perform_implicit_conversion_flags (decl_type, carg, complain, 15431 LOOKUP_IMPLICIT); 15432 --cp_unevaluated_operand; 15433 15434 /* Avoid redundant -Wzero-as-null-pointer-constant warnings at 15435 the call sites. */ 15436 if (TYPE_PTR_OR_PTRMEM_P (decl_type) 15437 && null_ptr_cst_p (arg) 15438 /* Don't lose side-effects as in PR90473. */ 15439 && !TREE_SIDE_EFFECTS (arg)) 15440 return nullptr_node; 15441 15442 /* [dcl.fct.default] 15443 15444 Local variables shall not be used in default argument 15445 expressions. 15446 15447 The keyword `this' shall not be used in a default argument of a 15448 member function. */ 15449 var = cp_walk_tree_without_duplicates (&arg, local_variable_p_walkfn, NULL); 15450 if (var) 15451 { 15452 if (complain & tf_warning_or_error) 15453 { 15454 if (DECL_NAME (var) == this_identifier) 15455 permerror (input_location, "default argument %qE uses %qD", 15456 arg, var); 15457 else 15458 error ("default argument %qE uses local variable %qD", arg, var); 15459 } 15460 return error_mark_node; 15461 } 15462 15463 /* All is well. */ 15464 return arg; 15465 } 15466 15467 /* Returns a deprecated type used within TYPE, or NULL_TREE if none. */ 15468 15469 static tree 15470 type_is_deprecated (tree type) 15471 { 15472 enum tree_code code; 15473 if (TREE_DEPRECATED (type)) 15474 return type; 15475 if (TYPE_NAME (type)) 15476 { 15477 if (TREE_DEPRECATED (TYPE_NAME (type))) 15478 return type; 15479 else 15480 { 15481 cp_warn_deprecated_use_scopes (CP_DECL_CONTEXT (TYPE_NAME (type))); 15482 return NULL_TREE; 15483 } 15484 } 15485 15486 /* Do warn about using typedefs to a deprecated class. */ 15487 if (OVERLOAD_TYPE_P (type) && type != TYPE_MAIN_VARIANT (type)) 15488 return type_is_deprecated (TYPE_MAIN_VARIANT (type)); 15489 15490 code = TREE_CODE (type); 15491 15492 if (code == POINTER_TYPE || code == REFERENCE_TYPE 15493 || code == OFFSET_TYPE || code == FUNCTION_TYPE 15494 || code == METHOD_TYPE || code == ARRAY_TYPE) 15495 return type_is_deprecated (TREE_TYPE (type)); 15496 15497 if (TYPE_PTRMEMFUNC_P (type)) 15498 return type_is_deprecated 15499 (TREE_TYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type)))); 15500 15501 return NULL_TREE; 15502 } 15503 15504 /* Returns an unavailable type used within TYPE, or NULL_TREE if none. */ 15505 15506 static tree 15507 type_is_unavailable (tree type) 15508 { 15509 enum tree_code code; 15510 if (TREE_UNAVAILABLE (type)) 15511 return type; 15512 if (TYPE_NAME (type)) 15513 { 15514 if (TREE_UNAVAILABLE (TYPE_NAME (type))) 15515 return type; 15516 else 15517 { 15518 cp_warn_deprecated_use_scopes (CP_DECL_CONTEXT (TYPE_NAME (type))); 15519 return NULL_TREE; 15520 } 15521 } 15522 15523 /* Do warn about using typedefs to a deprecated class. */ 15524 if (OVERLOAD_TYPE_P (type) && type != TYPE_MAIN_VARIANT (type)) 15525 return type_is_deprecated (TYPE_MAIN_VARIANT (type)); 15526 15527 code = TREE_CODE (type); 15528 15529 if (code == POINTER_TYPE || code == REFERENCE_TYPE 15530 || code == OFFSET_TYPE || code == FUNCTION_TYPE 15531 || code == METHOD_TYPE || code == ARRAY_TYPE) 15532 return type_is_unavailable (TREE_TYPE (type)); 15533 15534 if (TYPE_PTRMEMFUNC_P (type)) 15535 return type_is_unavailable 15536 (TREE_TYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type)))); 15537 15538 return NULL_TREE; 15539 } 15540 15541 /* Decode the list of parameter types for a function type. 15542 Given the list of things declared inside the parens, 15543 return a list of types. 15544 15545 If this parameter does not end with an ellipsis, we append 15546 void_list_node. 15547 15548 *PARMS is set to the chain of PARM_DECLs created. */ 15549 15550 tree 15551 grokparms (tree parmlist, tree *parms) 15552 { 15553 tree result = NULL_TREE; 15554 tree decls = NULL_TREE; 15555 tree parm; 15556 int any_error = 0; 15557 15558 for (parm = parmlist; parm != NULL_TREE; parm = TREE_CHAIN (parm)) 15559 { 15560 tree type = NULL_TREE; 15561 tree init = TREE_PURPOSE (parm); 15562 tree decl = TREE_VALUE (parm); 15563 15564 if (parm == void_list_node || parm == explicit_void_list_node) 15565 break; 15566 15567 if (! decl || TREE_TYPE (decl) == error_mark_node) 15568 { 15569 any_error = 1; 15570 continue; 15571 } 15572 15573 type = TREE_TYPE (decl); 15574 if (VOID_TYPE_P (type)) 15575 { 15576 if (same_type_p (type, void_type_node) 15577 && !init 15578 && !DECL_NAME (decl) && !result 15579 && TREE_CHAIN (parm) == void_list_node) 15580 /* DR 577: A parameter list consisting of a single 15581 unnamed parameter of non-dependent type 'void'. */ 15582 break; 15583 else if (cv_qualified_p (type)) 15584 error_at (DECL_SOURCE_LOCATION (decl), 15585 "invalid use of cv-qualified type %qT in " 15586 "parameter declaration", type); 15587 else 15588 error_at (DECL_SOURCE_LOCATION (decl), 15589 "invalid use of type %<void%> in parameter " 15590 "declaration"); 15591 /* It's not a good idea to actually create parameters of 15592 type `void'; other parts of the compiler assume that a 15593 void type terminates the parameter list. */ 15594 type = error_mark_node; 15595 TREE_TYPE (decl) = error_mark_node; 15596 } 15597 15598 if (type != error_mark_node) 15599 { 15600 if (deprecated_state != UNAVAILABLE_DEPRECATED_SUPPRESS) 15601 { 15602 tree unavailtype = type_is_unavailable (type); 15603 if (unavailtype) 15604 cp_handle_deprecated_or_unavailable (unavailtype); 15605 } 15606 if (deprecated_state != DEPRECATED_SUPPRESS 15607 && deprecated_state != UNAVAILABLE_DEPRECATED_SUPPRESS) 15608 { 15609 tree deptype = type_is_deprecated (type); 15610 if (deptype) 15611 cp_handle_deprecated_or_unavailable (deptype); 15612 } 15613 15614 /* [dcl.fct] "A parameter with volatile-qualified type is 15615 deprecated." */ 15616 if (CP_TYPE_VOLATILE_P (type)) 15617 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wvolatile, 15618 "%<volatile%>-qualified parameter is " 15619 "deprecated"); 15620 15621 /* Top-level qualifiers on the parameters are 15622 ignored for function types. */ 15623 type = cp_build_qualified_type (type, 0); 15624 if (TREE_CODE (type) == METHOD_TYPE) 15625 { 15626 error ("parameter %qD invalidly declared method type", decl); 15627 type = build_pointer_type (type); 15628 TREE_TYPE (decl) = type; 15629 } 15630 else if (cxx_dialect < cxx17 && INDIRECT_TYPE_P (type)) 15631 { 15632 /* Before C++17 DR 393: 15633 [dcl.fct]/6, parameter types cannot contain pointers 15634 (references) to arrays of unknown bound. */ 15635 tree t = TREE_TYPE (type); 15636 int ptr = TYPE_PTR_P (type); 15637 15638 while (1) 15639 { 15640 if (TYPE_PTR_P (t)) 15641 ptr = 1; 15642 else if (TREE_CODE (t) != ARRAY_TYPE) 15643 break; 15644 else if (!TYPE_DOMAIN (t)) 15645 break; 15646 t = TREE_TYPE (t); 15647 } 15648 if (TREE_CODE (t) == ARRAY_TYPE) 15649 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic, 15650 ptr 15651 ? G_("parameter %qD includes pointer to array of " 15652 "unknown bound %qT") 15653 : G_("parameter %qD includes reference to array of " 15654 "unknown bound %qT"), 15655 decl, t); 15656 } 15657 15658 if (init && !processing_template_decl) 15659 init = check_default_argument (decl, init, tf_warning_or_error); 15660 } 15661 15662 DECL_CHAIN (decl) = decls; 15663 decls = decl; 15664 result = tree_cons (init, type, result); 15665 } 15666 decls = nreverse (decls); 15667 result = nreverse (result); 15668 if (parm) 15669 result = chainon (result, void_list_node); 15670 *parms = decls; 15671 if (any_error) 15672 result = NULL_TREE; 15673 15674 if (any_error) 15675 /* We had parm errors, recover by giving the function (...) type. */ 15676 result = NULL_TREE; 15677 15678 return result; 15679 } 15680 15681 15682 /* D is a constructor or overloaded `operator='. 15684 15685 Let T be the class in which D is declared. Then, this function 15686 returns: 15687 15688 -1 if D's is an ill-formed constructor or copy assignment operator 15689 whose first parameter is of type `T'. 15690 0 if D is not a copy constructor or copy assignment 15691 operator. 15692 1 if D is a copy constructor or copy assignment operator whose 15693 first parameter is a reference to non-const qualified T. 15694 2 if D is a copy constructor or copy assignment operator whose 15695 first parameter is a reference to const qualified T. 15696 15697 This function can be used as a predicate. Positive values indicate 15698 a copy constructor and nonzero values indicate a copy assignment 15699 operator. */ 15700 15701 int 15702 copy_fn_p (const_tree d) 15703 { 15704 tree args; 15705 tree arg_type; 15706 int result = 1; 15707 15708 gcc_assert (DECL_FUNCTION_MEMBER_P (d)); 15709 15710 if (TREE_CODE (d) == TEMPLATE_DECL 15711 || (DECL_TEMPLATE_INFO (d) 15712 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d)))) 15713 /* Instantiations of template member functions are never copy 15714 functions. Note that member functions of templated classes are 15715 represented as template functions internally, and we must 15716 accept those as copy functions. */ 15717 return 0; 15718 15719 if (!DECL_CONSTRUCTOR_P (d) 15720 && DECL_NAME (d) != assign_op_identifier) 15721 return 0; 15722 15723 if (DECL_XOBJ_MEMBER_FUNCTION_P (d)) 15724 { 15725 tree object_param = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (d))); 15726 if (!TYPE_REF_P (object_param) 15727 || TYPE_REF_IS_RVALUE (object_param) 15728 /* Reject unrelated object parameters. */ 15729 || TYPE_MAIN_VARIANT (TREE_TYPE (object_param)) != DECL_CONTEXT (d) 15730 || CP_TYPE_CONST_P (TREE_TYPE (object_param))) 15731 return 0; 15732 args = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (d))); 15733 } 15734 else 15735 args = FUNCTION_FIRST_USER_PARMTYPE (d); 15736 if (!args) 15737 return 0; 15738 15739 arg_type = TREE_VALUE (args); 15740 if (arg_type == error_mark_node) 15741 return 0; 15742 15743 if (TYPE_MAIN_VARIANT (arg_type) == DECL_CONTEXT (d)) 15744 { 15745 /* Pass by value copy assignment operator. */ 15746 result = -1; 15747 } 15748 else if (TYPE_REF_P (arg_type) 15749 && !TYPE_REF_IS_RVALUE (arg_type) 15750 && TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)) == DECL_CONTEXT (d)) 15751 { 15752 if (CP_TYPE_CONST_P (TREE_TYPE (arg_type))) 15753 result = 2; 15754 } 15755 else 15756 return 0; 15757 15758 args = TREE_CHAIN (args); 15759 15760 if (args && args != void_list_node && !TREE_PURPOSE (args)) 15761 /* There are more non-optional args. */ 15762 return 0; 15763 15764 return result; 15765 } 15766 15767 /* D is a constructor or overloaded `operator='. 15768 15769 Let T be the class in which D is declared. Then, this function 15770 returns true when D is a move constructor or move assignment 15771 operator, false otherwise. */ 15772 15773 bool 15774 move_fn_p (const_tree d) 15775 { 15776 if (cxx_dialect == cxx98) 15777 /* There are no move constructors if we are in C++98 mode. */ 15778 return false; 15779 15780 if (TREE_CODE (d) == TEMPLATE_DECL 15781 || (DECL_TEMPLATE_INFO (d) 15782 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d)))) 15783 /* Instantiations of template member functions are never move 15784 functions. Note that member functions of templated classes are 15785 represented as template functions internally, and we must 15786 accept those as move functions. */ 15787 return 0; 15788 15789 return move_signature_fn_p (d); 15790 } 15791 15792 /* D is a constructor or overloaded `operator='. 15793 15794 Then, this function returns true when D has the same signature as a move 15795 constructor or move assignment operator (because either it is such a 15796 ctor/op= or it is a template specialization with the same signature), 15797 false otherwise. */ 15798 15799 bool 15800 move_signature_fn_p (const_tree d) 15801 { 15802 tree args; 15803 tree arg_type; 15804 bool result = false; 15805 15806 if (!DECL_CONSTRUCTOR_P (d) 15807 && DECL_NAME (d) != assign_op_identifier) 15808 return 0; 15809 15810 if (DECL_XOBJ_MEMBER_FUNCTION_P (d)) 15811 { 15812 tree object_param = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (d))); 15813 if (!TYPE_REF_P (object_param) 15814 || TYPE_REF_IS_RVALUE (object_param) 15815 /* Reject unrelated object parameters. */ 15816 || TYPE_MAIN_VARIANT (TREE_TYPE (object_param)) != DECL_CONTEXT (d) 15817 || CP_TYPE_CONST_P (TREE_TYPE (object_param))) 15818 return 0; 15819 args = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (d))); 15820 } 15821 else 15822 args = FUNCTION_FIRST_USER_PARMTYPE (d); 15823 if (!args) 15824 return 0; 15825 15826 arg_type = TREE_VALUE (args); 15827 if (arg_type == error_mark_node) 15828 return 0; 15829 15830 if (TYPE_REF_P (arg_type) 15831 && TYPE_REF_IS_RVALUE (arg_type) 15832 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)), 15833 DECL_CONTEXT (d))) 15834 result = true; 15835 15836 args = TREE_CHAIN (args); 15837 15838 if (args && args != void_list_node && !TREE_PURPOSE (args)) 15839 /* There are more non-optional args. */ 15840 return false; 15841 15842 return result; 15843 } 15844 15845 /* Remember any special properties of member function DECL. */ 15846 15847 void 15848 grok_special_member_properties (tree decl) 15849 { 15850 tree class_type; 15851 15852 if (TREE_CODE (decl) == USING_DECL 15853 || !DECL_OBJECT_MEMBER_FUNCTION_P (decl)) 15854 return; 15855 15856 class_type = DECL_CONTEXT (decl); 15857 if (IDENTIFIER_CTOR_P (DECL_NAME (decl))) 15858 { 15859 int ctor = copy_fn_p (decl); 15860 15861 if (!DECL_ARTIFICIAL (decl)) 15862 TYPE_HAS_USER_CONSTRUCTOR (class_type) = 1; 15863 15864 if (ctor > 0) 15865 { 15866 /* [class.copy] 15867 15868 A non-template constructor for class X is a copy 15869 constructor if its first parameter is of type X&, const 15870 X&, volatile X& or const volatile X&, and either there 15871 are no other parameters or else all other parameters have 15872 default arguments. */ 15873 TYPE_HAS_COPY_CTOR (class_type) = 1; 15874 if (ctor > 1) 15875 TYPE_HAS_CONST_COPY_CTOR (class_type) = 1; 15876 } 15877 15878 if (sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (decl))) 15879 TYPE_HAS_DEFAULT_CONSTRUCTOR (class_type) = 1; 15880 15881 if (is_list_ctor (decl)) 15882 TYPE_HAS_LIST_CTOR (class_type) = 1; 15883 15884 if (maybe_constexpr_fn (decl) 15885 && !ctor && !move_fn_p (decl)) 15886 TYPE_HAS_CONSTEXPR_CTOR (class_type) = 1; 15887 } 15888 else if (DECL_NAME (decl) == assign_op_identifier) 15889 { 15890 /* [class.copy] 15891 15892 A non-template assignment operator for class X is a copy 15893 assignment operator if its parameter is of type X, X&, const 15894 X&, volatile X& or const volatile X&. */ 15895 15896 int assop = copy_fn_p (decl); 15897 15898 if (assop) 15899 { 15900 TYPE_HAS_COPY_ASSIGN (class_type) = 1; 15901 if (assop != 1) 15902 TYPE_HAS_CONST_COPY_ASSIGN (class_type) = 1; 15903 } 15904 } 15905 else if (IDENTIFIER_CONV_OP_P (DECL_NAME (decl))) 15906 TYPE_HAS_CONVERSION (class_type) = true; 15907 15908 /* Destructors are handled in check_methods. */ 15909 } 15910 15911 /* Check a constructor DECL has the correct form. Complains 15912 if the class has a constructor of the form X(X). */ 15913 15914 bool 15915 grok_ctor_properties (const_tree ctype, const_tree decl) 15916 { 15917 int ctor_parm = copy_fn_p (decl); 15918 15919 if (ctor_parm < 0) 15920 { 15921 /* [class.copy] 15922 15923 A declaration of a constructor for a class X is ill-formed if 15924 its first parameter is of type (optionally cv-qualified) X 15925 and either there are no other parameters or else all other 15926 parameters have default arguments. 15927 15928 We *don't* complain about member template instantiations that 15929 have this form, though; they can occur as we try to decide 15930 what constructor to use during overload resolution. Since 15931 overload resolution will never prefer such a constructor to 15932 the non-template copy constructor (which is either explicitly 15933 or implicitly defined), there's no need to worry about their 15934 existence. Theoretically, they should never even be 15935 instantiated, but that's hard to forestall. */ 15936 error_at (DECL_SOURCE_LOCATION (decl), 15937 "invalid constructor; you probably meant %<%T (const %T&)%>", 15938 ctype, ctype); 15939 return false; 15940 } 15941 15942 return true; 15943 } 15944 15945 /* DECL is a declaration for an overloaded or conversion operator. If 15946 COMPLAIN is true, errors are issued for invalid declarations. */ 15947 15948 bool 15949 grok_op_properties (tree decl, bool complain) 15950 { 15951 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl)); 15952 bool const methodp = DECL_IOBJ_MEMBER_FUNCTION_P (decl); 15953 tree name = DECL_NAME (decl); 15954 location_t loc = DECL_SOURCE_LOCATION (decl); 15955 15956 tree class_type = DECL_CONTEXT (decl); 15957 if (class_type && !CLASS_TYPE_P (class_type)) 15958 class_type = NULL_TREE; 15959 15960 tree_code operator_code; 15961 unsigned op_flags; 15962 if (IDENTIFIER_CONV_OP_P (name)) 15963 { 15964 /* Conversion operators are TYPE_EXPR for the purposes of this 15965 function. */ 15966 operator_code = TYPE_EXPR; 15967 op_flags = OVL_OP_FLAG_UNARY; 15968 } 15969 else 15970 { 15971 const ovl_op_info_t *ovl_op = IDENTIFIER_OVL_OP_INFO (name); 15972 15973 operator_code = ovl_op->tree_code; 15974 op_flags = ovl_op->flags; 15975 gcc_checking_assert (operator_code != ERROR_MARK); 15976 DECL_OVERLOADED_OPERATOR_CODE_RAW (decl) = ovl_op->ovl_op_code; 15977 } 15978 15979 if (op_flags & OVL_OP_FLAG_ALLOC) 15980 { 15981 /* operator new and operator delete are quite special. */ 15982 if (class_type) 15983 switch (op_flags) 15984 { 15985 case OVL_OP_FLAG_ALLOC: 15986 TYPE_HAS_NEW_OPERATOR (class_type) = 1; 15987 break; 15988 15989 case OVL_OP_FLAG_ALLOC | OVL_OP_FLAG_DELETE: 15990 TYPE_GETS_DELETE (class_type) |= 1; 15991 break; 15992 15993 case OVL_OP_FLAG_ALLOC | OVL_OP_FLAG_VEC: 15994 TYPE_HAS_ARRAY_NEW_OPERATOR (class_type) = 1; 15995 break; 15996 15997 case OVL_OP_FLAG_ALLOC | OVL_OP_FLAG_DELETE | OVL_OP_FLAG_VEC: 15998 TYPE_GETS_DELETE (class_type) |= 2; 15999 break; 16000 16001 default: 16002 gcc_unreachable (); 16003 } 16004 16005 /* [basic.std.dynamic.allocation]/1: 16006 16007 A program is ill-formed if an allocation function is declared 16008 in a namespace scope other than global scope or declared 16009 static in global scope. 16010 16011 The same also holds true for deallocation functions. */ 16012 if (DECL_NAMESPACE_SCOPE_P (decl)) 16013 { 16014 if (CP_DECL_CONTEXT (decl) != global_namespace) 16015 { 16016 error_at (loc, "%qD may not be declared within a namespace", 16017 decl); 16018 return false; 16019 } 16020 16021 if (!TREE_PUBLIC (decl)) 16022 { 16023 error_at (loc, "%qD may not be declared as static", decl); 16024 return false; 16025 } 16026 } 16027 16028 if (op_flags & OVL_OP_FLAG_DELETE) 16029 { 16030 DECL_SET_IS_OPERATOR_DELETE (decl, true); 16031 coerce_delete_type (decl, loc); 16032 } 16033 else 16034 { 16035 DECL_SET_IS_OPERATOR_NEW (decl, true); 16036 TREE_TYPE (decl) = coerce_new_type (TREE_TYPE (decl), loc); 16037 } 16038 16039 return true; 16040 } 16041 16042 /* An operator function must either be a non-static member function 16043 or have at least one parameter of a class, a reference to a class, 16044 an enumeration, or a reference to an enumeration. 13.4.0.6 */ 16045 if (!DECL_OBJECT_MEMBER_FUNCTION_P (decl)) 16046 { 16047 if (operator_code == TYPE_EXPR 16048 || operator_code == COMPONENT_REF 16049 || operator_code == NOP_EXPR) 16050 { 16051 error_at (loc, "%qD must be a non-static member function", decl); 16052 return false; 16053 } 16054 16055 if (operator_code == CALL_EXPR || operator_code == ARRAY_REF) 16056 { 16057 if (! DECL_STATIC_FUNCTION_P (decl)) 16058 { 16059 error_at (loc, "%qD must be a member function", decl); 16060 return false; 16061 } 16062 if (cxx_dialect < cxx23 16063 /* For lambdas we diagnose static lambda specifier elsewhere. */ 16064 && (operator_code == ARRAY_REF || ! LAMBDA_FUNCTION_P (decl)) 16065 /* For instantiations, we have diagnosed this already. */ 16066 && ! DECL_USE_TEMPLATE (decl)) 16067 pedwarn (loc, OPT_Wc__23_extensions, "%qD may be a static member " 16068 "function only with %<-std=c++23%> or %<-std=gnu++23%>", 16069 decl); 16070 if (operator_code == ARRAY_REF) 16071 /* static operator[] should have exactly one argument 16072 for C++20 and earlier, so that it isn't multidimensional. */ 16073 op_flags = OVL_OP_FLAG_UNARY; 16074 } 16075 else if (DECL_STATIC_FUNCTION_P (decl)) 16076 { 16077 error_at (loc, "%qD must be either a non-static member " 16078 "function or a non-member function", decl); 16079 return false; 16080 } 16081 else 16082 for (tree arg = argtypes; ; arg = TREE_CHAIN (arg)) 16083 { 16084 if (!arg || arg == void_list_node) 16085 { 16086 if (complain) 16087 error_at (loc, "%qD must have an argument of class or " 16088 "enumerated type", decl); 16089 return false; 16090 } 16091 16092 tree type = non_reference (TREE_VALUE (arg)); 16093 if (type == error_mark_node) 16094 return false; 16095 16096 /* MAYBE_CLASS_TYPE_P, rather than CLASS_TYPE_P, is used 16097 because these checks are performed even on template 16098 functions. */ 16099 if (MAYBE_CLASS_TYPE_P (type) 16100 || TREE_CODE (type) == ENUMERAL_TYPE) 16101 break; 16102 } 16103 } 16104 16105 if (operator_code == CALL_EXPR) 16106 /* There are no further restrictions on the arguments to an overloaded 16107 "operator ()". */ 16108 return true; 16109 16110 /* C++23 allows an arbitrary number of parameters and default arguments for 16111 operator[], and none of the other checks below apply. */ 16112 if (operator_code == ARRAY_REF && cxx_dialect >= cxx23) 16113 return true; 16114 16115 if (operator_code == COND_EXPR) 16116 { 16117 /* 13.4.0.3 */ 16118 error_at (loc, "ISO C++ prohibits overloading %<operator ?:%>"); 16119 return false; 16120 } 16121 16122 /* Count the number of arguments and check for ellipsis. */ 16123 int arity = 0; 16124 for (tree arg = argtypes; arg != void_list_node; arg = TREE_CHAIN (arg)) 16125 { 16126 if (!arg) 16127 { 16128 error_at (loc, "%qD must not have variable number of arguments", 16129 decl); 16130 return false; 16131 } 16132 ++arity; 16133 } 16134 /* FIXME: We need tests for these errors with xobj member functions. */ 16135 /* Verify correct number of arguments. */ 16136 switch (op_flags) 16137 { 16138 case OVL_OP_FLAG_AMBIARY: 16139 if (arity == 1) 16140 { 16141 /* We have a unary instance of an ambi-ary op. Remap to the 16142 unary one. */ 16143 unsigned alt = ovl_op_alternate[ovl_op_mapping [operator_code]]; 16144 const ovl_op_info_t *ovl_op = &ovl_op_info[false][alt]; 16145 gcc_checking_assert (ovl_op->flags == OVL_OP_FLAG_UNARY); 16146 operator_code = ovl_op->tree_code; 16147 DECL_OVERLOADED_OPERATOR_CODE_RAW (decl) = ovl_op->ovl_op_code; 16148 } 16149 else if (arity != 2) 16150 { 16151 /* This was an ambiguous operator but is invalid. */ 16152 error_at (loc, 16153 methodp 16154 ? G_("%qD must have either zero or one argument") 16155 : G_("%qD must have either one or two arguments"), decl); 16156 return false; 16157 } 16158 else if ((operator_code == POSTINCREMENT_EXPR 16159 || operator_code == POSTDECREMENT_EXPR) 16160 && ! processing_template_decl 16161 /* x++ and x--'s second argument must be an int. */ 16162 && ! same_type_p (TREE_VALUE (TREE_CHAIN (argtypes)), 16163 integer_type_node)) 16164 { 16165 error_at (loc, 16166 methodp 16167 ? G_("postfix %qD must have %<int%> as its argument") 16168 : G_("postfix %qD must have %<int%> as its second argument"), 16169 decl); 16170 return false; 16171 } 16172 break; 16173 16174 case OVL_OP_FLAG_UNARY: 16175 if (arity != 1) 16176 { 16177 error_at (loc, 16178 methodp 16179 ? G_("%qD must have no arguments") 16180 : G_("%qD must have exactly one argument"), decl); 16181 return false; 16182 } 16183 break; 16184 16185 case OVL_OP_FLAG_BINARY: 16186 if (arity != 2) 16187 { 16188 error_at (loc, 16189 methodp 16190 ? G_("%qD must have exactly one argument") 16191 : G_("%qD must have exactly two arguments"), decl); 16192 return false; 16193 } 16194 break; 16195 16196 default: 16197 gcc_unreachable (); 16198 } 16199 16200 /* There can be no default arguments. */ 16201 for (tree arg = argtypes; arg && arg != void_list_node; 16202 arg = TREE_CHAIN (arg)) 16203 if (TREE_PURPOSE (arg)) 16204 { 16205 TREE_PURPOSE (arg) = NULL_TREE; 16206 error_at (loc, "%qD cannot have default arguments", decl); 16207 return false; 16208 } 16209 16210 /* At this point the declaration is well-formed. It may not be 16211 sensible though. */ 16212 16213 /* Check member function warnings only on the in-class declaration. 16214 There's no point warning on an out-of-class definition. */ 16215 if (class_type && class_type != current_class_type) 16216 return true; 16217 16218 /* Warn about conversion operators that will never be used. */ 16219 if (IDENTIFIER_CONV_OP_P (name) 16220 && ! DECL_TEMPLATE_INFO (decl) 16221 && warn_class_conversion) 16222 { 16223 tree t = TREE_TYPE (name); 16224 int ref = TYPE_REF_P (t); 16225 16226 if (ref) 16227 t = TYPE_MAIN_VARIANT (TREE_TYPE (t)); 16228 16229 if (VOID_TYPE_P (t)) 16230 warning_at (loc, OPT_Wclass_conversion, "converting %qT to %<void%> " 16231 "will never use a type conversion operator", class_type); 16232 else if (class_type) 16233 { 16234 if (same_type_ignoring_top_level_qualifiers_p (t, class_type)) 16235 warning_at (loc, OPT_Wclass_conversion, 16236 ref 16237 ? G_("converting %qT to a reference to the same type " 16238 "will never use a type conversion operator") 16239 : G_("converting %qT to the same type " 16240 "will never use a type conversion operator"), 16241 class_type); 16242 /* Don't force t to be complete here. */ 16243 else if (MAYBE_CLASS_TYPE_P (t) 16244 && COMPLETE_TYPE_P (t) 16245 && DERIVED_FROM_P (t, class_type)) 16246 warning_at (loc, OPT_Wclass_conversion, 16247 ref 16248 ? G_("converting %qT to a reference to a base class " 16249 "%qT will never use a type conversion operator") 16250 : G_("converting %qT to a base class %qT " 16251 "will never use a type conversion operator"), 16252 class_type, t); 16253 } 16254 } 16255 16256 if (!warn_ecpp) 16257 return true; 16258 16259 /* Effective C++ rules below. */ 16260 16261 /* More Effective C++ rule 7. */ 16262 if (operator_code == TRUTH_ANDIF_EXPR 16263 || operator_code == TRUTH_ORIF_EXPR 16264 || operator_code == COMPOUND_EXPR) 16265 warning_at (loc, OPT_Weffc__, 16266 "user-defined %qD always evaluates both arguments", decl); 16267 16268 /* More Effective C++ rule 6. */ 16269 if (operator_code == POSTINCREMENT_EXPR 16270 || operator_code == POSTDECREMENT_EXPR 16271 || operator_code == PREINCREMENT_EXPR 16272 || operator_code == PREDECREMENT_EXPR) 16273 { 16274 tree arg = TREE_VALUE (argtypes); 16275 tree ret = TREE_TYPE (TREE_TYPE (decl)); 16276 if (methodp || TYPE_REF_P (arg)) 16277 arg = TREE_TYPE (arg); 16278 arg = TYPE_MAIN_VARIANT (arg); 16279 16280 if (operator_code == PREINCREMENT_EXPR 16281 || operator_code == PREDECREMENT_EXPR) 16282 { 16283 if (!TYPE_REF_P (ret) 16284 || !same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (ret)), arg)) 16285 warning_at (loc, OPT_Weffc__, "prefix %qD should return %qT", decl, 16286 build_reference_type (arg)); 16287 } 16288 else 16289 { 16290 if (!same_type_p (TYPE_MAIN_VARIANT (ret), arg)) 16291 warning_at (loc, OPT_Weffc__, 16292 "postfix %qD should return %qT", decl, arg); 16293 } 16294 } 16295 16296 /* Effective C++ rule 23. */ 16297 if (!DECL_ASSIGNMENT_OPERATOR_P (decl) 16298 && (operator_code == PLUS_EXPR 16299 || operator_code == MINUS_EXPR 16300 || operator_code == TRUNC_DIV_EXPR 16301 || operator_code == MULT_EXPR 16302 || operator_code == TRUNC_MOD_EXPR) 16303 && TYPE_REF_P (TREE_TYPE (TREE_TYPE (decl)))) 16304 warning_at (loc, OPT_Weffc__, "%qD should return by value", decl); 16305 16306 return true; 16307 } 16308 16309 /* Return a string giving the keyword associate with CODE. */ 16311 16312 static const char * 16313 tag_name (enum tag_types code) 16314 { 16315 switch (code) 16316 { 16317 case record_type: 16318 return "struct"; 16319 case class_type: 16320 return "class"; 16321 case union_type: 16322 return "union"; 16323 case enum_type: 16324 return "enum"; 16325 case typename_type: 16326 return "typename"; 16327 default: 16328 gcc_unreachable (); 16329 } 16330 } 16331 16332 /* Name lookup in an elaborated-type-specifier (after the keyword 16333 indicated by TAG_CODE) has found the TYPE_DECL DECL. If the 16334 elaborated-type-specifier is invalid, issue a diagnostic and return 16335 error_mark_node; otherwise, return the *_TYPE to which it referred. 16336 If ALLOW_TEMPLATE_P is true, TYPE may be a class template. */ 16337 16338 tree 16339 check_elaborated_type_specifier (enum tag_types tag_code, 16340 tree decl, 16341 bool allow_template_p) 16342 { 16343 tree type; 16344 16345 /* In the case of: 16346 16347 struct S { struct S *p; }; 16348 16349 name lookup will find the TYPE_DECL for the implicit "S::S" 16350 typedef. Adjust for that here. */ 16351 if (DECL_SELF_REFERENCE_P (decl)) 16352 decl = TYPE_NAME (TREE_TYPE (decl)); 16353 16354 type = TREE_TYPE (decl); 16355 16356 /* Check TEMPLATE_TYPE_PARM first because DECL_IMPLICIT_TYPEDEF_P 16357 is false for this case as well. */ 16358 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM) 16359 { 16360 error ("using template type parameter %qT after %qs", 16361 type, tag_name (tag_code)); 16362 return error_mark_node; 16363 } 16364 /* Accept template template parameters. */ 16365 else if (allow_template_p 16366 && (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM 16367 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)) 16368 ; 16369 /* [dcl.type.elab] 16370 16371 If the identifier resolves to a typedef-name or the 16372 simple-template-id resolves to an alias template 16373 specialization, the elaborated-type-specifier is ill-formed. 16374 16375 In other words, the only legitimate declaration to use in the 16376 elaborated type specifier is the implicit typedef created when 16377 the type is declared. */ 16378 else if (!DECL_IMPLICIT_TYPEDEF_P (decl) 16379 && !DECL_SELF_REFERENCE_P (decl) 16380 && tag_code != typename_type) 16381 { 16382 if (alias_template_specialization_p (type, nt_opaque)) 16383 error ("using alias template specialization %qT after %qs", 16384 type, tag_name (tag_code)); 16385 else 16386 error ("using typedef-name %qD after %qs", decl, tag_name (tag_code)); 16387 inform (DECL_SOURCE_LOCATION (decl), 16388 "%qD has a previous declaration here", decl); 16389 return error_mark_node; 16390 } 16391 else if (TREE_CODE (type) != RECORD_TYPE 16392 && TREE_CODE (type) != UNION_TYPE 16393 && tag_code != enum_type 16394 && tag_code != typename_type) 16395 { 16396 error ("%qT referred to as %qs", type, tag_name (tag_code)); 16397 inform (location_of (type), "%qT has a previous declaration here", type); 16398 return error_mark_node; 16399 } 16400 else if (TREE_CODE (type) != ENUMERAL_TYPE 16401 && tag_code == enum_type) 16402 { 16403 error ("%qT referred to as enum", type); 16404 inform (location_of (type), "%qT has a previous declaration here", type); 16405 return error_mark_node; 16406 } 16407 else if (!allow_template_p 16408 && TREE_CODE (type) == RECORD_TYPE 16409 && CLASSTYPE_IS_TEMPLATE (type)) 16410 { 16411 /* If a class template appears as elaborated type specifier 16412 without a template header such as: 16413 16414 template <class T> class C {}; 16415 void f(class C); // No template header here 16416 16417 then the required template argument is missing. */ 16418 error ("template argument required for %<%s %T%>", 16419 tag_name (tag_code), 16420 DECL_NAME (CLASSTYPE_TI_TEMPLATE (type))); 16421 return error_mark_node; 16422 } 16423 16424 return type; 16425 } 16426 16427 /* Lookup NAME of an elaborated type specifier according to SCOPE and 16428 issue diagnostics if necessary. Return *_TYPE node upon success, 16429 NULL_TREE when the NAME is not found, and ERROR_MARK_NODE for type 16430 error. */ 16431 16432 static tree 16433 lookup_and_check_tag (enum tag_types tag_code, tree name, 16434 TAG_how how, bool template_header_p) 16435 { 16436 tree decl; 16437 if (how == TAG_how::GLOBAL) 16438 { 16439 /* First try ordinary name lookup, ignoring hidden class name 16440 injected via friend declaration. */ 16441 decl = lookup_name (name, LOOK_want::TYPE); 16442 decl = strip_using_decl (decl); 16443 /* If that fails, the name will be placed in the smallest 16444 non-class, non-function-prototype scope according to 3.3.1/5. 16445 We may already have a hidden name declared as friend in this 16446 scope. So lookup again but not ignoring hidden names. 16447 If we find one, that name will be made visible rather than 16448 creating a new tag. */ 16449 if (!decl) 16450 decl = lookup_elaborated_type (name, TAG_how::INNERMOST_NON_CLASS); 16451 } 16452 else 16453 decl = lookup_elaborated_type (name, how); 16454 16455 if (!decl) 16456 /* We found nothing. */ 16457 return NULL_TREE; 16458 16459 if (TREE_CODE (decl) == TREE_LIST) 16460 { 16461 error ("reference to %qD is ambiguous", name); 16462 print_candidates (decl); 16463 return error_mark_node; 16464 } 16465 16466 if (DECL_CLASS_TEMPLATE_P (decl) 16467 && !template_header_p 16468 && how == TAG_how::CURRENT_ONLY) 16469 { 16470 error ("class template %qD redeclared as non-template", name); 16471 inform (location_of (decl), "previous declaration here"); 16472 CLASSTYPE_ERRONEOUS (TREE_TYPE (decl)) = true; 16473 return error_mark_node; 16474 } 16475 16476 if (DECL_CLASS_TEMPLATE_P (decl) 16477 /* If scope is TAG_how::CURRENT_ONLY we're defining a class, 16478 so ignore a template template parameter. */ 16479 || (how != TAG_how::CURRENT_ONLY && DECL_TEMPLATE_TEMPLATE_PARM_P (decl))) 16480 decl = DECL_TEMPLATE_RESULT (decl); 16481 16482 if (TREE_CODE (decl) != TYPE_DECL) 16483 /* Found not-a-type. */ 16484 return NULL_TREE; 16485 16486 /* Look for invalid nested type: 16487 class C { 16488 class C {}; 16489 }; */ 16490 if (how == TAG_how::CURRENT_ONLY && DECL_SELF_REFERENCE_P (decl)) 16491 { 16492 error ("%qD has the same name as the class in which it is " 16493 "declared", decl); 16494 return error_mark_node; 16495 } 16496 16497 /* Two cases we need to consider when deciding if a class 16498 template is allowed as an elaborated type specifier: 16499 1. It is a self reference to its own class. 16500 2. It comes with a template header. 16501 16502 For example: 16503 16504 template <class T> class C { 16505 class C *c1; // DECL_SELF_REFERENCE_P is true 16506 class D; 16507 }; 16508 template <class U> class C; // template_header_p is true 16509 template <class T> class C<T>::D { 16510 class C *c2; // DECL_SELF_REFERENCE_P is true 16511 }; */ 16512 16513 tree t = check_elaborated_type_specifier (tag_code, decl, 16514 template_header_p 16515 | DECL_SELF_REFERENCE_P (decl)); 16516 if (template_header_p && t && CLASS_TYPE_P (t) 16517 && (!CLASSTYPE_TEMPLATE_INFO (t) 16518 || (!PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t))))) 16519 { 16520 error ("%qT is not a template", t); 16521 inform (location_of (t), "previous declaration here"); 16522 if (TYPE_CLASS_SCOPE_P (t) 16523 && CLASSTYPE_TEMPLATE_INFO (TYPE_CONTEXT (t))) 16524 inform (input_location, 16525 "perhaps you want to explicitly add %<%T::%>", 16526 TYPE_CONTEXT (t)); 16527 return error_mark_node; 16528 } 16529 16530 return t; 16531 } 16532 16533 /* Get the struct, enum or union (TAG_CODE says which) with tag NAME. 16534 Define the tag as a forward-reference if it is not defined. 16535 16536 If a declaration is given, process it here, and report an error if 16537 multiple declarations are not identical. 16538 16539 SCOPE is TS_CURRENT when this is also a definition. Only look in 16540 the current frame for the name (since C++ allows new names in any 16541 scope.) It is TS_WITHIN_ENCLOSING_NON_CLASS if this is a friend 16542 declaration. Only look beginning from the current scope outward up 16543 till the nearest non-class scope. Otherwise it is TS_GLOBAL. 16544 16545 TEMPLATE_HEADER_P is true when this declaration is preceded by 16546 a set of template parameters. */ 16547 16548 tree 16549 xref_tag (enum tag_types tag_code, tree name, 16550 TAG_how how, bool template_header_p) 16551 { 16552 enum tree_code code; 16553 tree context = NULL_TREE; 16554 16555 auto_cond_timevar tv (TV_NAME_LOOKUP); 16556 16557 gcc_assert (identifier_p (name)); 16558 16559 switch (tag_code) 16560 { 16561 case record_type: 16562 case class_type: 16563 code = RECORD_TYPE; 16564 break; 16565 case union_type: 16566 code = UNION_TYPE; 16567 break; 16568 case enum_type: 16569 code = ENUMERAL_TYPE; 16570 break; 16571 default: 16572 gcc_unreachable (); 16573 } 16574 16575 /* In case of anonymous name, xref_tag is only called to 16576 make type node and push name. Name lookup is not required. */ 16577 tree t = NULL_TREE; 16578 if (!IDENTIFIER_ANON_P (name)) 16579 t = lookup_and_check_tag (tag_code, name, how, template_header_p); 16580 16581 if (t == error_mark_node) 16582 return error_mark_node; 16583 16584 if (how != TAG_how::CURRENT_ONLY && t && current_class_type 16585 && template_class_depth (current_class_type) 16586 && template_header_p) 16587 { 16588 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM) 16589 return t; 16590 16591 /* Since HOW is not TAG_how::CURRENT_ONLY, we are not looking at 16592 a definition of this tag. Since, in addition, we are 16593 currently processing a (member) template declaration of a 16594 template class, we must be very careful; consider: 16595 16596 template <class X> struct S1 16597 16598 template <class U> struct S2 16599 { 16600 template <class V> friend struct S1; 16601 }; 16602 16603 Here, the S2::S1 declaration should not be confused with the 16604 outer declaration. In particular, the inner version should 16605 have a template parameter of level 2, not level 1. 16606 16607 On the other hand, when presented with: 16608 16609 template <class T> struct S1 16610 { 16611 template <class U> struct S2 {}; 16612 template <class U> friend struct S2; 16613 }; 16614 16615 the friend must find S1::S2 eventually. We accomplish this 16616 by making sure that the new type we create to represent this 16617 declaration has the right TYPE_CONTEXT. */ 16618 context = TYPE_CONTEXT (t); 16619 t = NULL_TREE; 16620 } 16621 16622 if (! t) 16623 { 16624 /* If no such tag is yet defined, create a forward-reference node 16625 and record it as the "definition". 16626 When a real declaration of this type is found, 16627 the forward-reference will be altered into a real type. */ 16628 if (code == ENUMERAL_TYPE) 16629 { 16630 error ("use of enum %q#D without previous declaration", name); 16631 return error_mark_node; 16632 } 16633 16634 t = make_class_type (code); 16635 TYPE_CONTEXT (t) = context; 16636 if (IDENTIFIER_LAMBDA_P (name)) 16637 /* Mark it as a lambda type right now. Our caller will 16638 correct the value. */ 16639 CLASSTYPE_LAMBDA_EXPR (t) = error_mark_node; 16640 t = pushtag (name, t, how); 16641 } 16642 else 16643 { 16644 if (template_header_p && MAYBE_CLASS_TYPE_P (t)) 16645 { 16646 /* Check that we aren't trying to overload a class with different 16647 constraints. */ 16648 if (!redeclare_class_template (t, current_template_parms, 16649 current_template_constraints ())) 16650 return error_mark_node; 16651 } 16652 else if (!processing_template_decl 16653 && CLASS_TYPE_P (t) 16654 && CLASSTYPE_IS_TEMPLATE (t)) 16655 { 16656 error ("redeclaration of %qT as a non-template", t); 16657 inform (location_of (t), "previous declaration %qD", t); 16658 return error_mark_node; 16659 } 16660 16661 if (modules_p () 16662 && how == TAG_how::CURRENT_ONLY) 16663 { 16664 tree decl = TYPE_NAME (t); 16665 if (!module_may_redeclare (decl)) 16666 return error_mark_node; 16667 16668 tree not_tmpl = STRIP_TEMPLATE (decl); 16669 if (DECL_LANG_SPECIFIC (not_tmpl) 16670 && DECL_MODULE_ATTACH_P (not_tmpl) 16671 && !DECL_MODULE_EXPORT_P (not_tmpl) 16672 && module_exporting_p ()) 16673 { 16674 auto_diagnostic_group d; 16675 error ("conflicting exporting for declaration %qD", decl); 16676 inform (DECL_SOURCE_LOCATION (decl), 16677 "previously declared here without exporting"); 16678 } 16679 16680 tree maybe_tmpl = decl; 16681 if (CLASS_TYPE_P (t) && CLASSTYPE_IS_TEMPLATE (t)) 16682 maybe_tmpl = CLASSTYPE_TI_TEMPLATE (t); 16683 16684 if (DECL_LANG_SPECIFIC (decl) 16685 && DECL_MODULE_IMPORT_P (decl) 16686 && TREE_CODE (CP_DECL_CONTEXT (decl)) == NAMESPACE_DECL) 16687 { 16688 /* Push it into this TU's symbol slot. */ 16689 gcc_checking_assert (current_namespace == CP_DECL_CONTEXT (decl)); 16690 if (maybe_tmpl != decl) 16691 /* We're in the template parm binding level. 16692 Pushtag has logic to slide under that, but we're 16693 not pushing a *new* type. */ 16694 push_nested_namespace (CP_DECL_CONTEXT (decl)); 16695 16696 pushdecl (maybe_tmpl); 16697 if (maybe_tmpl != decl) 16698 pop_nested_namespace (CP_DECL_CONTEXT (decl)); 16699 } 16700 16701 set_instantiating_module (maybe_tmpl); 16702 } 16703 } 16704 16705 return t; 16706 } 16707 16708 /* Create the binfo hierarchy for REF with (possibly NULL) base list 16709 BASE_LIST. For each element on BASE_LIST the TREE_PURPOSE is an 16710 access_* node, and the TREE_VALUE is the type of the base-class. 16711 Non-NULL TREE_TYPE indicates virtual inheritance. */ 16712 16713 void 16714 xref_basetypes (tree ref, tree base_list) 16715 { 16716 tree *basep; 16717 tree binfo, base_binfo; 16718 unsigned max_vbases = 0; /* Maximum direct & indirect virtual bases. */ 16719 unsigned max_bases = 0; /* Maximum direct bases. */ 16720 unsigned max_dvbases = 0; /* Maximum direct virtual bases. */ 16721 int i; 16722 tree default_access; 16723 tree igo_prev; /* Track Inheritance Graph Order. */ 16724 16725 if (ref == error_mark_node) 16726 return; 16727 16728 /* The base of a derived class is private by default, all others are 16729 public. */ 16730 default_access = (TREE_CODE (ref) == RECORD_TYPE 16731 && CLASSTYPE_DECLARED_CLASS (ref) 16732 ? access_private_node : access_public_node); 16733 16734 /* First, make sure that any templates in base-classes are 16735 instantiated. This ensures that if we call ourselves recursively 16736 we do not get confused about which classes are marked and which 16737 are not. */ 16738 basep = &base_list; 16739 while (*basep) 16740 { 16741 tree basetype = TREE_VALUE (*basep); 16742 16743 /* The dependent_type_p call below should really be dependent_scope_p 16744 so that we give a hard error about using an incomplete type as a 16745 base, but we allow it with a pedwarn for backward 16746 compatibility. */ 16747 if (processing_template_decl 16748 && CLASS_TYPE_P (basetype) && TYPE_BEING_DEFINED (basetype)) 16749 cxx_incomplete_type_diagnostic (NULL_TREE, basetype, DK_PEDWARN); 16750 if (!dependent_type_p (basetype) 16751 && !complete_type_or_else (basetype, NULL)) 16752 /* An incomplete type. Remove it from the list. */ 16753 *basep = TREE_CHAIN (*basep); 16754 else 16755 { 16756 max_bases++; 16757 if (TREE_TYPE (*basep)) 16758 max_dvbases++; 16759 if (CLASS_TYPE_P (basetype)) 16760 max_vbases += vec_safe_length (CLASSTYPE_VBASECLASSES (basetype)); 16761 basep = &TREE_CHAIN (*basep); 16762 } 16763 } 16764 max_vbases += max_dvbases; 16765 16766 TYPE_MARKED_P (ref) = 1; 16767 16768 /* The binfo slot should be empty, unless this is an (ill-formed) 16769 redefinition. */ 16770 gcc_assert (!TYPE_BINFO (ref) || TYPE_SIZE (ref)); 16771 16772 gcc_assert (TYPE_MAIN_VARIANT (ref) == ref); 16773 16774 binfo = make_tree_binfo (max_bases); 16775 16776 TYPE_BINFO (ref) = binfo; 16777 BINFO_OFFSET (binfo) = size_zero_node; 16778 BINFO_TYPE (binfo) = ref; 16779 16780 /* Apply base-class info set up to the variants of this type. */ 16781 fixup_type_variants (ref); 16782 16783 if (max_bases) 16784 { 16785 vec_alloc (BINFO_BASE_ACCESSES (binfo), max_bases); 16786 /* A C++98 POD cannot have base classes. */ 16787 CLASSTYPE_NON_LAYOUT_POD_P (ref) = true; 16788 16789 if (TREE_CODE (ref) == UNION_TYPE) 16790 { 16791 error ("derived union %qT invalid", ref); 16792 return; 16793 } 16794 } 16795 16796 if (max_bases > 1) 16797 warning (OPT_Wmultiple_inheritance, 16798 "%qT defined with multiple direct bases", ref); 16799 16800 if (max_vbases) 16801 { 16802 /* An aggregate can't have virtual base classes. */ 16803 CLASSTYPE_NON_AGGREGATE (ref) = true; 16804 16805 vec_alloc (CLASSTYPE_VBASECLASSES (ref), max_vbases); 16806 16807 if (max_dvbases) 16808 warning (OPT_Wvirtual_inheritance, 16809 "%qT defined with direct virtual base", ref); 16810 } 16811 16812 for (igo_prev = binfo; base_list; base_list = TREE_CHAIN (base_list)) 16813 { 16814 tree access = TREE_PURPOSE (base_list); 16815 int via_virtual = TREE_TYPE (base_list) != NULL_TREE; 16816 tree basetype = TREE_VALUE (base_list); 16817 16818 if (access == access_default_node) 16819 access = default_access; 16820 16821 /* Before C++17, an aggregate cannot have base classes. In C++17, an 16822 aggregate can't have virtual, private, or protected base classes. */ 16823 if (cxx_dialect < cxx17 16824 || access != access_public_node 16825 || via_virtual) 16826 CLASSTYPE_NON_AGGREGATE (ref) = true; 16827 16828 if (PACK_EXPANSION_P (basetype)) 16829 basetype = PACK_EXPANSION_PATTERN (basetype); 16830 if (TREE_CODE (basetype) == TYPE_DECL) 16831 basetype = TREE_TYPE (basetype); 16832 if (!MAYBE_CLASS_TYPE_P (basetype) || TREE_CODE (basetype) == UNION_TYPE) 16833 { 16834 error ("base type %qT fails to be a struct or class type", 16835 basetype); 16836 goto dropped_base; 16837 } 16838 16839 base_binfo = NULL_TREE; 16840 if (CLASS_TYPE_P (basetype) && !dependent_scope_p (basetype)) 16841 { 16842 base_binfo = TYPE_BINFO (basetype); 16843 /* The original basetype could have been a typedef'd type. */ 16844 basetype = BINFO_TYPE (base_binfo); 16845 16846 /* Inherit flags from the base. */ 16847 TYPE_HAS_NEW_OPERATOR (ref) 16848 |= TYPE_HAS_NEW_OPERATOR (basetype); 16849 TYPE_HAS_ARRAY_NEW_OPERATOR (ref) 16850 |= TYPE_HAS_ARRAY_NEW_OPERATOR (basetype); 16851 TYPE_GETS_DELETE (ref) |= TYPE_GETS_DELETE (basetype); 16852 TYPE_HAS_CONVERSION (ref) |= TYPE_HAS_CONVERSION (basetype); 16853 CLASSTYPE_DIAMOND_SHAPED_P (ref) 16854 |= CLASSTYPE_DIAMOND_SHAPED_P (basetype); 16855 CLASSTYPE_REPEATED_BASE_P (ref) 16856 |= CLASSTYPE_REPEATED_BASE_P (basetype); 16857 } 16858 16859 /* We must do this test after we've seen through a typedef 16860 type. */ 16861 if (TYPE_MARKED_P (basetype)) 16862 { 16863 if (basetype == ref) 16864 error ("recursive type %qT undefined", basetype); 16865 else 16866 error ("duplicate base type %qT invalid", basetype); 16867 goto dropped_base; 16868 } 16869 16870 if (PACK_EXPANSION_P (TREE_VALUE (base_list))) 16871 /* Regenerate the pack expansion for the bases. */ 16872 basetype = make_pack_expansion (basetype); 16873 16874 TYPE_MARKED_P (basetype) = 1; 16875 16876 base_binfo = copy_binfo (base_binfo, basetype, ref, 16877 &igo_prev, via_virtual); 16878 if (!BINFO_INHERITANCE_CHAIN (base_binfo)) 16879 BINFO_INHERITANCE_CHAIN (base_binfo) = binfo; 16880 16881 BINFO_BASE_APPEND (binfo, base_binfo); 16882 BINFO_BASE_ACCESS_APPEND (binfo, access); 16883 continue; 16884 16885 dropped_base: 16886 /* Update max_vbases to reflect the reality that we are dropping 16887 this base: if it reaches zero we want to undo the vec_alloc 16888 above to avoid inconsistencies during error-recovery: eg, in 16889 build_special_member_call, CLASSTYPE_VBASECLASSES non null 16890 and vtt null (c++/27952). */ 16891 if (via_virtual) 16892 max_vbases--; 16893 if (CLASS_TYPE_P (basetype)) 16894 max_vbases 16895 -= vec_safe_length (CLASSTYPE_VBASECLASSES (basetype)); 16896 } 16897 16898 if (CLASSTYPE_VBASECLASSES (ref) 16899 && max_vbases == 0) 16900 vec_free (CLASSTYPE_VBASECLASSES (ref)); 16901 16902 if (vec_safe_length (CLASSTYPE_VBASECLASSES (ref)) < max_vbases) 16903 /* If we didn't get max_vbases vbases, we must have shared at 16904 least one of them, and are therefore diamond shaped. */ 16905 CLASSTYPE_DIAMOND_SHAPED_P (ref) = 1; 16906 16907 /* Unmark all the types. */ 16908 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++) 16909 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 0; 16910 TYPE_MARKED_P (ref) = 0; 16911 16912 /* Now see if we have a repeated base type. */ 16913 if (!CLASSTYPE_REPEATED_BASE_P (ref)) 16914 { 16915 for (base_binfo = binfo; base_binfo; 16916 base_binfo = TREE_CHAIN (base_binfo)) 16917 { 16918 if (TYPE_MARKED_P (BINFO_TYPE (base_binfo))) 16919 { 16920 CLASSTYPE_REPEATED_BASE_P (ref) = 1; 16921 break; 16922 } 16923 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 1; 16924 } 16925 for (base_binfo = binfo; base_binfo; 16926 base_binfo = TREE_CHAIN (base_binfo)) 16927 if (TYPE_MARKED_P (BINFO_TYPE (base_binfo))) 16928 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 0; 16929 else 16930 break; 16931 } 16932 } 16933 16934 16935 /* Copies the enum-related properties from type SRC to type DST. 16937 Used with the underlying type of an enum and the enum itself. */ 16938 static void 16939 copy_type_enum (tree dst, tree src) 16940 { 16941 tree t; 16942 for (t = dst; t; t = TYPE_NEXT_VARIANT (t)) 16943 { 16944 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (src); 16945 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (src); 16946 TYPE_SIZE (t) = TYPE_SIZE (src); 16947 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (src); 16948 SET_TYPE_MODE (dst, TYPE_MODE (src)); 16949 TYPE_PRECISION (t) = TYPE_PRECISION (src); 16950 unsigned valign = TYPE_ALIGN (src); 16951 if (TYPE_USER_ALIGN (t)) 16952 valign = MAX (valign, TYPE_ALIGN (t)); 16953 else 16954 TYPE_USER_ALIGN (t) = TYPE_USER_ALIGN (src); 16955 SET_TYPE_ALIGN (t, valign); 16956 TYPE_UNSIGNED (t) = TYPE_UNSIGNED (src); 16957 } 16958 } 16959 16960 /* Begin compiling the definition of an enumeration type. 16961 NAME is its name, 16962 16963 if ENUMTYPE is not NULL_TREE then the type has alredy been found. 16964 16965 UNDERLYING_TYPE is the type that will be used as the storage for 16966 the enumeration type. This should be NULL_TREE if no storage type 16967 was specified. 16968 16969 ATTRIBUTES are any attributes specified after the enum-key. 16970 16971 SCOPED_ENUM_P is true if this is a scoped enumeration type. 16972 16973 if IS_NEW is not NULL, gets TRUE iff a new type is created. 16974 16975 Returns the type object, as yet incomplete. 16976 Also records info about it so that build_enumerator 16977 may be used to declare the individual values as they are read. */ 16978 16979 tree 16980 start_enum (tree name, tree enumtype, tree underlying_type, 16981 tree attributes, bool scoped_enum_p, bool *is_new) 16982 { 16983 tree prevtype = NULL_TREE; 16984 gcc_assert (identifier_p (name)); 16985 16986 if (is_new) 16987 *is_new = false; 16988 /* [C++0x dcl.enum]p5: 16989 16990 If not explicitly specified, the underlying type of a scoped 16991 enumeration type is int. */ 16992 if (!underlying_type && scoped_enum_p) 16993 underlying_type = integer_type_node; 16994 16995 if (underlying_type) 16996 underlying_type = cv_unqualified (underlying_type); 16997 16998 /* If this is the real definition for a previous forward reference, 16999 fill in the contents in the same object that used to be the 17000 forward reference. */ 17001 if (!enumtype) 17002 enumtype = lookup_and_check_tag (enum_type, name, 17003 /*tag_scope=*/TAG_how::CURRENT_ONLY, 17004 /*template_header_p=*/false); 17005 17006 /* In case of a template_decl, the only check that should be deferred 17007 to instantiation time is the comparison of underlying types. */ 17008 if (enumtype && TREE_CODE (enumtype) == ENUMERAL_TYPE) 17009 { 17010 /* Attempt to set the declaring module. */ 17011 if (modules_p ()) 17012 { 17013 tree decl = TYPE_NAME (enumtype); 17014 if (!module_may_redeclare (decl)) 17015 enumtype = error_mark_node; 17016 else 17017 set_instantiating_module (decl); 17018 } 17019 17020 if (enumtype == error_mark_node) 17021 ; 17022 else if (scoped_enum_p != SCOPED_ENUM_P (enumtype)) 17023 { 17024 auto_diagnostic_group d; 17025 error_at (input_location, "scoped/unscoped mismatch " 17026 "in enum %q#T", enumtype); 17027 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)), 17028 "previous definition here"); 17029 enumtype = error_mark_node; 17030 } 17031 else if (ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) != !! underlying_type) 17032 { 17033 auto_diagnostic_group d; 17034 error_at (input_location, "underlying type mismatch " 17035 "in enum %q#T", enumtype); 17036 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)), 17037 "previous definition here"); 17038 enumtype = error_mark_node; 17039 } 17040 else if (underlying_type && ENUM_UNDERLYING_TYPE (enumtype) 17041 && !same_type_p (underlying_type, 17042 ENUM_UNDERLYING_TYPE (enumtype))) 17043 { 17044 auto_diagnostic_group d; 17045 error_at (input_location, "different underlying type " 17046 "in enum %q#T", enumtype); 17047 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)), 17048 "previous definition here"); 17049 underlying_type = NULL_TREE; 17050 } 17051 } 17052 17053 if (!enumtype || TREE_CODE (enumtype) != ENUMERAL_TYPE 17054 || processing_template_decl) 17055 { 17056 /* In case of error, make a dummy enum to allow parsing to 17057 continue. */ 17058 if (enumtype == error_mark_node) 17059 { 17060 name = make_anon_name (); 17061 enumtype = NULL_TREE; 17062 } 17063 17064 /* enumtype may be an ENUMERAL_TYPE if this is a redefinition 17065 of an opaque enum, or an opaque enum of an already defined 17066 enumeration (C++11). 17067 In any other case, it'll be NULL_TREE. */ 17068 if (!enumtype) 17069 { 17070 if (is_new) 17071 *is_new = true; 17072 } 17073 prevtype = enumtype; 17074 17075 /* Do not push the decl more than once. */ 17076 if (!enumtype 17077 || TREE_CODE (enumtype) != ENUMERAL_TYPE) 17078 { 17079 enumtype = cxx_make_type (ENUMERAL_TYPE); 17080 enumtype = pushtag (name, enumtype); 17081 17082 /* std::byte aliases anything. */ 17083 if (enumtype != error_mark_node 17084 && TYPE_CONTEXT (enumtype) == std_node 17085 && !strcmp ("byte", TYPE_NAME_STRING (enumtype))) 17086 TYPE_ALIAS_SET (enumtype) = 0; 17087 } 17088 else 17089 enumtype = xref_tag (enum_type, name); 17090 17091 if (enumtype == error_mark_node) 17092 return error_mark_node; 17093 17094 /* The enum is considered opaque until the opening '{' of the 17095 enumerator list. */ 17096 SET_OPAQUE_ENUM_P (enumtype, true); 17097 ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) = !! underlying_type; 17098 } 17099 17100 SET_SCOPED_ENUM_P (enumtype, scoped_enum_p); 17101 17102 cplus_decl_attributes (&enumtype, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE); 17103 17104 if (underlying_type) 17105 { 17106 if (ENUM_UNDERLYING_TYPE (enumtype)) 17107 /* We already checked that it matches, don't change it to a different 17108 typedef variant. */; 17109 else if (CP_INTEGRAL_TYPE_P (underlying_type)) 17110 { 17111 copy_type_enum (enumtype, underlying_type); 17112 ENUM_UNDERLYING_TYPE (enumtype) = underlying_type; 17113 } 17114 else if (dependent_type_p (underlying_type)) 17115 ENUM_UNDERLYING_TYPE (enumtype) = underlying_type; 17116 else 17117 { 17118 error ("underlying type %qT of %qT must be an integral type", 17119 underlying_type, enumtype); 17120 ENUM_UNDERLYING_TYPE (enumtype) = integer_type_node; 17121 } 17122 } 17123 17124 /* If into a template class, the returned enum is always the first 17125 declaration (opaque or not) seen. This way all the references to 17126 this type will be to the same declaration. The following ones are used 17127 only to check for definition errors. */ 17128 if (prevtype && processing_template_decl) 17129 return prevtype; 17130 else 17131 return enumtype; 17132 } 17133 17134 /* After processing and defining all the values of an enumeration type, 17135 install their decls in the enumeration type. 17136 ENUMTYPE is the type object. */ 17137 17138 void 17139 finish_enum_value_list (tree enumtype) 17140 { 17141 tree values; 17142 tree underlying_type; 17143 tree decl; 17144 tree value; 17145 tree minnode, maxnode; 17146 tree t; 17147 17148 bool fixed_underlying_type_p 17149 = ENUM_UNDERLYING_TYPE (enumtype) != NULL_TREE; 17150 17151 /* We built up the VALUES in reverse order. */ 17152 TYPE_VALUES (enumtype) = nreverse (TYPE_VALUES (enumtype)); 17153 17154 /* For an enum defined in a template, just set the type of the values; 17155 all further processing is postponed until the template is 17156 instantiated. We need to set the type so that tsubst of a CONST_DECL 17157 works. */ 17158 if (processing_template_decl) 17159 { 17160 for (values = TYPE_VALUES (enumtype); 17161 values; 17162 values = TREE_CHAIN (values)) 17163 TREE_TYPE (TREE_VALUE (values)) = enumtype; 17164 return; 17165 } 17166 17167 /* Determine the minimum and maximum values of the enumerators. */ 17168 if (TYPE_VALUES (enumtype)) 17169 { 17170 minnode = maxnode = NULL_TREE; 17171 17172 for (values = TYPE_VALUES (enumtype); 17173 values; 17174 values = TREE_CHAIN (values)) 17175 { 17176 decl = TREE_VALUE (values); 17177 17178 /* [dcl.enum]: Following the closing brace of an enum-specifier, 17179 each enumerator has the type of its enumeration. Prior to the 17180 closing brace, the type of each enumerator is the type of its 17181 initializing value. */ 17182 TREE_TYPE (decl) = enumtype; 17183 17184 /* Update the minimum and maximum values, if appropriate. */ 17185 value = DECL_INITIAL (decl); 17186 if (TREE_CODE (value) != INTEGER_CST) 17187 value = integer_zero_node; 17188 /* Figure out what the minimum and maximum values of the 17189 enumerators are. */ 17190 if (!minnode) 17191 minnode = maxnode = value; 17192 else if (tree_int_cst_lt (maxnode, value)) 17193 maxnode = value; 17194 else if (tree_int_cst_lt (value, minnode)) 17195 minnode = value; 17196 } 17197 } 17198 else 17199 /* [dcl.enum] 17200 17201 If the enumerator-list is empty, the underlying type is as if 17202 the enumeration had a single enumerator with value 0. */ 17203 minnode = maxnode = integer_zero_node; 17204 17205 if (!fixed_underlying_type_p) 17206 { 17207 /* Compute the number of bits require to represent all values of the 17208 enumeration. We must do this before the type of MINNODE and 17209 MAXNODE are transformed, since tree_int_cst_min_precision relies 17210 on the TREE_TYPE of the value it is passed. */ 17211 signop sgn = tree_int_cst_sgn (minnode) >= 0 ? UNSIGNED : SIGNED; 17212 int lowprec = tree_int_cst_min_precision (minnode, sgn); 17213 int highprec = tree_int_cst_min_precision (maxnode, sgn); 17214 int precision = MAX (lowprec, highprec); 17215 unsigned int itk; 17216 bool use_short_enum; 17217 17218 /* Determine the underlying type of the enumeration. 17219 17220 [dcl.enum] 17221 17222 The underlying type of an enumeration is an integral type that 17223 can represent all the enumerator values defined in the 17224 enumeration. It is implementation-defined which integral type is 17225 used as the underlying type for an enumeration except that the 17226 underlying type shall not be larger than int unless the value of 17227 an enumerator cannot fit in an int or unsigned int. 17228 17229 We use "int" or an "unsigned int" as the underlying type, even if 17230 a smaller integral type would work, unless the user has 17231 explicitly requested that we use the smallest possible type. The 17232 user can request that for all enumerations with a command line 17233 flag, or for just one enumeration with an attribute. */ 17234 17235 use_short_enum = flag_short_enums 17236 || lookup_attribute ("packed", TYPE_ATTRIBUTES (enumtype)); 17237 17238 /* If the precision of the type was specified with an attribute and it 17239 was too small, give an error. Otherwise, use it. */ 17240 if (TYPE_PRECISION (enumtype)) 17241 { 17242 if (precision > TYPE_PRECISION (enumtype)) 17243 error ("specified mode too small for enumerated values"); 17244 else 17245 { 17246 use_short_enum = true; 17247 precision = TYPE_PRECISION (enumtype); 17248 } 17249 } 17250 17251 for (itk = (use_short_enum ? itk_char : itk_int); 17252 itk != itk_none; 17253 itk++) 17254 { 17255 underlying_type = integer_types[itk]; 17256 if (underlying_type != NULL_TREE 17257 && TYPE_PRECISION (underlying_type) >= precision 17258 && TYPE_SIGN (underlying_type) == sgn) 17259 break; 17260 } 17261 if (itk == itk_none) 17262 { 17263 /* DR 377 17264 17265 IF no integral type can represent all the enumerator values, the 17266 enumeration is ill-formed. */ 17267 error ("no integral type can represent all of the enumerator values " 17268 "for %qT", enumtype); 17269 precision = TYPE_PRECISION (long_long_integer_type_node); 17270 underlying_type = integer_types[itk_unsigned_long_long]; 17271 } 17272 17273 /* [dcl.enum] 17274 17275 The value of sizeof() applied to an enumeration type, an object 17276 of an enumeration type, or an enumerator, is the value of sizeof() 17277 applied to the underlying type. */ 17278 copy_type_enum (enumtype, underlying_type); 17279 17280 /* Compute the minimum and maximum values for the type. 17281 17282 [dcl.enum] 17283 17284 For an enumeration where emin is the smallest enumerator and emax 17285 is the largest, the values of the enumeration are the values of the 17286 underlying type in the range bmin to bmax, where bmin and bmax are, 17287 respectively, the smallest and largest values of the smallest bit- 17288 field that can store emin and emax. */ 17289 17290 /* The middle-end currently assumes that types with TYPE_PRECISION 17291 narrower than their underlying type are suitably zero or sign 17292 extended to fill their mode. Similarly, it assumes that the front 17293 end assures that a value of a particular type must be within 17294 TYPE_MIN_VALUE and TYPE_MAX_VALUE. 17295 17296 We used to set these fields based on bmin and bmax, but that led 17297 to invalid assumptions like optimizing away bounds checking. So 17298 now we just set the TYPE_PRECISION, TYPE_MIN_VALUE, and 17299 TYPE_MAX_VALUE to the values for the mode above and only restrict 17300 the ENUM_UNDERLYING_TYPE for the benefit of diagnostics. */ 17301 ENUM_UNDERLYING_TYPE (enumtype) 17302 = build_distinct_type_copy (underlying_type); 17303 TYPE_PRECISION (ENUM_UNDERLYING_TYPE (enumtype)) = precision; 17304 set_min_and_max_values_for_integral_type 17305 (ENUM_UNDERLYING_TYPE (enumtype), precision, sgn); 17306 17307 /* If -fstrict-enums, still constrain TYPE_MIN/MAX_VALUE. */ 17308 if (flag_strict_enums) 17309 set_min_and_max_values_for_integral_type (enumtype, precision, sgn); 17310 17311 if (use_short_enum) 17312 { 17313 TYPE_PACKED (enumtype) = use_short_enum; 17314 fixup_attribute_variants (enumtype); 17315 } 17316 } 17317 else 17318 underlying_type = ENUM_UNDERLYING_TYPE (enumtype); 17319 17320 /* If the enum is exported, mark the consts too. */ 17321 bool export_p = (UNSCOPED_ENUM_P (enumtype) 17322 && DECL_MODULE_EXPORT_P (TYPE_STUB_DECL (enumtype)) 17323 && at_namespace_scope_p ()); 17324 17325 /* Convert each of the enumerators to the type of the underlying 17326 type of the enumeration. */ 17327 for (values = TYPE_VALUES (enumtype); values; values = TREE_CHAIN (values)) 17328 { 17329 decl = TREE_VALUE (values); 17330 iloc_sentinel ils (DECL_SOURCE_LOCATION (decl)); 17331 if (fixed_underlying_type_p) 17332 /* If the enumeration type has a fixed underlying type, we 17333 already checked all of the enumerator values. */ 17334 value = DECL_INITIAL (decl); 17335 else 17336 value = perform_implicit_conversion (underlying_type, 17337 DECL_INITIAL (decl), 17338 tf_warning_or_error); 17339 /* Do not clobber shared ints. */ 17340 if (value != error_mark_node) 17341 { 17342 value = copy_node (value); 17343 17344 TREE_TYPE (value) = enumtype; 17345 } 17346 DECL_INITIAL (decl) = value; 17347 if (export_p) 17348 DECL_MODULE_EXPORT_P (decl) = true; 17349 } 17350 17351 /* Fix up all variant types of this enum type. */ 17352 for (t = TYPE_MAIN_VARIANT (enumtype); t; t = TYPE_NEXT_VARIANT (t)) 17353 TYPE_VALUES (t) = TYPE_VALUES (enumtype); 17354 17355 if (at_class_scope_p () 17356 && COMPLETE_TYPE_P (current_class_type) 17357 && UNSCOPED_ENUM_P (enumtype)) 17358 { 17359 insert_late_enum_def_bindings (current_class_type, enumtype); 17360 /* TYPE_FIELDS needs fixup. */ 17361 fixup_type_variants (current_class_type); 17362 } 17363 17364 /* Finish debugging output for this type. */ 17365 rest_of_type_compilation (enumtype, namespace_bindings_p ()); 17366 17367 /* Each enumerator now has the type of its enumeration. Clear the cache 17368 so that this change in types doesn't confuse us later on. */ 17369 clear_cv_and_fold_caches (); 17370 } 17371 17372 /* Finishes the enum type. This is called only the first time an 17373 enumeration is seen, be it opaque or odinary. 17374 ENUMTYPE is the type object. */ 17375 17376 void 17377 finish_enum (tree enumtype) 17378 { 17379 if (processing_template_decl) 17380 { 17381 if (at_function_scope_p ()) 17382 add_stmt (build_min (TAG_DEFN, enumtype)); 17383 return; 17384 } 17385 17386 /* If this is a forward declaration, there should not be any variants, 17387 though we can get a variant in the middle of an enum-specifier with 17388 wacky code like 'enum E { e = sizeof(const E*) };' */ 17389 gcc_assert (enumtype == TYPE_MAIN_VARIANT (enumtype) 17390 && (TYPE_VALUES (enumtype) 17391 || !TYPE_NEXT_VARIANT (enumtype))); 17392 } 17393 17394 /* Build and install a CONST_DECL for an enumeration constant of the 17395 enumeration type ENUMTYPE whose NAME and VALUE (if any) are provided. 17396 Apply ATTRIBUTES if available. LOC is the location of NAME. 17397 Assignment of sequential values by default is handled here. */ 17398 17399 tree 17400 build_enumerator (tree name, tree value, tree enumtype, tree attributes, 17401 location_t loc) 17402 { 17403 tree decl; 17404 tree context; 17405 tree type; 17406 17407 /* scalar_constant_value will pull out this expression, so make sure 17408 it's folded as appropriate. 17409 17410 Creating a TARGET_EXPR in a template breaks when substituting, and 17411 here we would create it for instance when using a class prvalue with 17412 a user-defined conversion function. So don't use such a tree. We 17413 instantiate VALUE here to get errors about bad enumerators even in 17414 a template that does not get instantiated. */ 17415 if (processing_template_decl) 17416 value = maybe_fold_non_dependent_expr (value); 17417 17418 /* If the VALUE was erroneous, pretend it wasn't there; that will 17419 result in the enum being assigned the next value in sequence. */ 17420 if (value == error_mark_node) 17421 value = NULL_TREE; 17422 17423 /* Remove no-op casts from the value. */ 17424 if (value) 17425 STRIP_TYPE_NOPS (value); 17426 17427 if (! processing_template_decl) 17428 { 17429 /* Validate and default VALUE. */ 17430 if (value != NULL_TREE) 17431 { 17432 if (!ENUM_UNDERLYING_TYPE (enumtype)) 17433 { 17434 tree tmp_value = build_expr_type_conversion (WANT_INT | WANT_ENUM, 17435 value, true); 17436 if (tmp_value) 17437 value = tmp_value; 17438 } 17439 else if (! INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P 17440 (TREE_TYPE (value))) 17441 value = perform_implicit_conversion_flags 17442 (ENUM_UNDERLYING_TYPE (enumtype), value, tf_warning_or_error, 17443 LOOKUP_IMPLICIT | LOOKUP_NO_NARROWING); 17444 17445 if (value == error_mark_node) 17446 value = NULL_TREE; 17447 17448 if (value != NULL_TREE) 17449 { 17450 if (! INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P 17451 (TREE_TYPE (value))) 17452 { 17453 error_at (cp_expr_loc_or_input_loc (value), 17454 "enumerator value for %qD must have integral or " 17455 "unscoped enumeration type", name); 17456 value = NULL_TREE; 17457 } 17458 else 17459 { 17460 value = cxx_constant_value (value); 17461 17462 if (TREE_CODE (value) != INTEGER_CST) 17463 { 17464 error ("enumerator value for %qD is not an integer " 17465 "constant", name); 17466 value = NULL_TREE; 17467 } 17468 } 17469 } 17470 } 17471 17472 /* Default based on previous value. */ 17473 if (value == NULL_TREE) 17474 { 17475 if (TYPE_VALUES (enumtype)) 17476 { 17477 tree prev_value; 17478 17479 /* C++03 7.2/4: If no initializer is specified for the first 17480 enumerator, the type is an unspecified integral 17481 type. Otherwise the type is the same as the type of the 17482 initializing value of the preceding enumerator unless the 17483 incremented value is not representable in that type, in 17484 which case the type is an unspecified integral type 17485 sufficient to contain the incremented value. */ 17486 prev_value = DECL_INITIAL (TREE_VALUE (TYPE_VALUES (enumtype))); 17487 if (TREE_CODE (prev_value) != INTEGER_CST) 17488 value = error_mark_node; 17489 else 17490 { 17491 wi::overflow_type overflowed; 17492 tree type = TREE_TYPE (prev_value); 17493 signop sgn = TYPE_SIGN (type); 17494 widest_int wi = wi::add (wi::to_widest (prev_value), 1, sgn, 17495 &overflowed); 17496 if (!overflowed) 17497 { 17498 bool pos = !wi::neg_p (wi, sgn); 17499 if (!wi::fits_to_tree_p (wi, type)) 17500 { 17501 unsigned int itk; 17502 for (itk = itk_int; itk != itk_none; itk++) 17503 { 17504 type = integer_types[itk]; 17505 if (type != NULL_TREE 17506 && (pos || !TYPE_UNSIGNED (type)) 17507 && wi::fits_to_tree_p (wi, type)) 17508 break; 17509 } 17510 if (type && cxx_dialect < cxx11 17511 && itk > itk_unsigned_long) 17512 pedwarn (input_location, OPT_Wlong_long, 17513 pos ? G_("\ 17514 incremented enumerator value is too large for %<unsigned long%>") : G_("\ 17515 incremented enumerator value is too large for %<long%>")); 17516 } 17517 if (type == NULL_TREE) 17518 overflowed = wi::OVF_UNKNOWN; 17519 else 17520 value = wide_int_to_tree (type, wi); 17521 } 17522 17523 if (overflowed) 17524 { 17525 error ("overflow in enumeration values at %qD", name); 17526 value = error_mark_node; 17527 } 17528 } 17529 } 17530 else 17531 value = integer_zero_node; 17532 } 17533 17534 /* Remove no-op casts from the value. */ 17535 STRIP_TYPE_NOPS (value); 17536 17537 /* If the underlying type of the enum is fixed, check whether 17538 the enumerator values fits in the underlying type. If it 17539 does not fit, the program is ill-formed [C++0x dcl.enum]. */ 17540 if (ENUM_UNDERLYING_TYPE (enumtype) 17541 && value 17542 && TREE_CODE (value) == INTEGER_CST) 17543 { 17544 if (!int_fits_type_p (value, ENUM_UNDERLYING_TYPE (enumtype))) 17545 error ("enumerator value %qE is outside the range of underlying " 17546 "type %qT", value, ENUM_UNDERLYING_TYPE (enumtype)); 17547 17548 /* Convert the value to the appropriate type. */ 17549 value = fold_convert (ENUM_UNDERLYING_TYPE (enumtype), value); 17550 } 17551 } 17552 17553 /* C++ associates enums with global, function, or class declarations. */ 17554 context = current_scope (); 17555 17556 /* Build the actual enumeration constant. Note that the enumeration 17557 constants have the underlying type of the enum (if it is fixed) 17558 or the type of their initializer (if the underlying type of the 17559 enum is not fixed): 17560 17561 [ C++0x dcl.enum ] 17562 17563 If the underlying type is fixed, the type of each enumerator 17564 prior to the closing brace is the underlying type; if the 17565 initializing value of an enumerator cannot be represented by 17566 the underlying type, the program is ill-formed. If the 17567 underlying type is not fixed, the type of each enumerator is 17568 the type of its initializing value. 17569 17570 If the underlying type is not fixed, it will be computed by 17571 finish_enum and we will reset the type of this enumerator. Of 17572 course, if we're processing a template, there may be no value. */ 17573 type = value ? TREE_TYPE (value) : NULL_TREE; 17574 17575 decl = build_decl (loc, CONST_DECL, name, type); 17576 17577 DECL_CONTEXT (decl) = enumtype; 17578 TREE_CONSTANT (decl) = 1; 17579 TREE_READONLY (decl) = 1; 17580 DECL_INITIAL (decl) = value; 17581 17582 if (attributes) 17583 cplus_decl_attributes (&decl, attributes, 0); 17584 17585 if (context && context == current_class_type && !SCOPED_ENUM_P (enumtype)) 17586 { 17587 /* In something like `struct S { enum E { i = 7 }; };' we put `i' 17588 on the TYPE_FIELDS list for `S'. (That's so that you can say 17589 things like `S::i' later.) */ 17590 17591 /* The enumerator may be getting declared outside of its enclosing 17592 class, like so: 17593 17594 class S { public: enum E : int; }; enum S::E : int { i = 7; }; 17595 17596 For which case we need to make sure that the access of `S::i' 17597 matches the access of `S::E'. */ 17598 auto cas = make_temp_override (current_access_specifier); 17599 set_current_access_from_decl (TYPE_NAME (enumtype)); 17600 finish_member_declaration (decl); 17601 } 17602 else 17603 pushdecl (decl); 17604 17605 /* Add this enumeration constant to the list for this type. */ 17606 TYPE_VALUES (enumtype) = tree_cons (name, decl, TYPE_VALUES (enumtype)); 17607 17608 return decl; 17609 } 17610 17611 /* Look for an enumerator with the given NAME within the enumeration 17612 type ENUMTYPE. This routine is used primarily for qualified name 17613 lookup into an enumerator in C++0x, e.g., 17614 17615 enum class Color { Red, Green, Blue }; 17616 17617 Color color = Color::Red; 17618 17619 Returns the value corresponding to the enumerator, or 17620 NULL_TREE if no such enumerator was found. */ 17621 tree 17622 lookup_enumerator (tree enumtype, tree name) 17623 { 17624 tree e; 17625 gcc_assert (enumtype && TREE_CODE (enumtype) == ENUMERAL_TYPE); 17626 17627 e = purpose_member (name, TYPE_VALUES (enumtype)); 17628 return e? TREE_VALUE (e) : NULL_TREE; 17629 } 17630 17631 /* Implement LANG_HOOKS_SIMULATE_ENUM_DECL. */ 17632 17633 tree 17634 cxx_simulate_enum_decl (location_t loc, const char *name, 17635 vec<string_int_pair> *values) 17636 { 17637 location_t saved_loc = input_location; 17638 input_location = loc; 17639 17640 tree enumtype = start_enum (get_identifier (name), NULL_TREE, NULL_TREE, 17641 NULL_TREE, false, NULL); 17642 if (!OPAQUE_ENUM_P (enumtype)) 17643 { 17644 error_at (loc, "multiple definition of %q#T", enumtype); 17645 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)), 17646 "previous definition here"); 17647 return enumtype; 17648 } 17649 SET_OPAQUE_ENUM_P (enumtype, false); 17650 DECL_SOURCE_LOCATION (TYPE_NAME (enumtype)) = loc; 17651 17652 for (const string_int_pair &value : values) 17653 build_enumerator (get_identifier (value.first), 17654 build_int_cst (integer_type_node, value.second), 17655 enumtype, NULL_TREE, loc); 17656 17657 finish_enum_value_list (enumtype); 17658 finish_enum (enumtype); 17659 17660 input_location = saved_loc; 17661 return enumtype; 17662 } 17663 17664 /* Implement LANG_HOOKS_SIMULATE_RECORD_DECL. */ 17665 17666 tree 17667 cxx_simulate_record_decl (location_t loc, const char *name, 17668 array_slice<const tree> fields) 17669 { 17670 iloc_sentinel ils (loc); 17671 17672 tree ident = get_identifier (name); 17673 tree type = xref_tag (/*tag_code=*/record_type, ident); 17674 if (type != error_mark_node 17675 && (TREE_CODE (type) != RECORD_TYPE || COMPLETE_TYPE_P (type))) 17676 { 17677 error ("redefinition of %q#T", type); 17678 type = error_mark_node; 17679 } 17680 if (type == error_mark_node) 17681 return lhd_simulate_record_decl (loc, name, fields); 17682 17683 xref_basetypes (type, NULL_TREE); 17684 type = begin_class_definition (type); 17685 if (type == error_mark_node) 17686 return lhd_simulate_record_decl (loc, name, fields); 17687 17688 for (tree field : fields) 17689 finish_member_declaration (field); 17690 17691 type = finish_struct (type, NULL_TREE); 17692 17693 tree decl = build_decl (loc, TYPE_DECL, ident, type); 17694 set_underlying_type (decl); 17695 lang_hooks.decls.pushdecl (decl); 17696 17697 return type; 17698 } 17699 17700 /* We're defining DECL. Make sure that its type is OK. */ 17702 17703 static void 17704 check_function_type (tree decl, tree current_function_parms) 17705 { 17706 tree fntype = TREE_TYPE (decl); 17707 tree return_type = complete_type (TREE_TYPE (fntype)); 17708 17709 /* In a function definition, arg types must be complete. */ 17710 require_complete_types_for_parms (current_function_parms); 17711 17712 if (dependent_type_p (return_type) 17713 || type_uses_auto (return_type)) 17714 return; 17715 if (!COMPLETE_OR_VOID_TYPE_P (return_type)) 17716 { 17717 tree args = TYPE_ARG_TYPES (fntype); 17718 17719 error ("return type %q#T is incomplete", return_type); 17720 17721 /* Make it return void instead. */ 17722 if (TREE_CODE (fntype) == METHOD_TYPE) 17723 fntype = build_method_type_directly (TREE_TYPE (TREE_VALUE (args)), 17724 void_type_node, 17725 TREE_CHAIN (args)); 17726 else 17727 fntype = build_function_type (void_type_node, args); 17728 fntype = (cp_build_type_attribute_variant 17729 (fntype, TYPE_ATTRIBUTES (TREE_TYPE (decl)))); 17730 fntype = cxx_copy_lang_qualifiers (fntype, TREE_TYPE (decl)); 17731 TREE_TYPE (decl) = fntype; 17732 } 17733 else 17734 { 17735 abstract_virtuals_error (decl, TREE_TYPE (fntype)); 17736 maybe_warn_parm_abi (TREE_TYPE (fntype), 17737 DECL_SOURCE_LOCATION (decl)); 17738 } 17739 } 17740 17741 /* True iff FN is an implicitly-defined default constructor. */ 17742 17743 static bool 17744 implicit_default_ctor_p (tree fn) 17745 { 17746 return (DECL_CONSTRUCTOR_P (fn) 17747 && !user_provided_p (fn) 17748 && sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (fn))); 17749 } 17750 17751 /* Clobber the contents of *this to let the back end know that the object 17752 storage is dead when we enter the constructor or leave the destructor. */ 17753 17754 static tree 17755 build_clobber_this (clobber_kind kind) 17756 { 17757 /* Clobbering an empty base is pointless, and harmful if its one byte 17758 TYPE_SIZE overlays real data. */ 17759 if (is_empty_class (current_class_type)) 17760 return void_node; 17761 17762 /* If we have virtual bases, clobber the whole object, but only if we're in 17763 charge. If we don't have virtual bases, clobber the as-base type so we 17764 don't mess with tail padding. */ 17765 bool vbases = CLASSTYPE_VBASECLASSES (current_class_type); 17766 17767 tree ctype = current_class_type; 17768 if (!vbases) 17769 ctype = CLASSTYPE_AS_BASE (ctype); 17770 17771 tree clobber = build_clobber (ctype, kind); 17772 17773 tree thisref = current_class_ref; 17774 if (ctype != current_class_type) 17775 { 17776 thisref = build_nop (build_reference_type (ctype), current_class_ptr); 17777 thisref = convert_from_reference (thisref); 17778 } 17779 17780 tree exprstmt = build2 (MODIFY_EXPR, void_type_node, thisref, clobber); 17781 if (vbases) 17782 exprstmt = build_if_in_charge (exprstmt); 17783 17784 return exprstmt; 17785 } 17786 17787 /* Create the FUNCTION_DECL for a function definition. 17788 DECLSPECS and DECLARATOR are the parts of the declaration; 17789 they describe the function's name and the type it returns, 17790 but twisted together in a fashion that parallels the syntax of C. 17791 17792 FLAGS is a bitwise or of SF_PRE_PARSED (indicating that the 17793 DECLARATOR is really the DECL for the function we are about to 17794 process and that DECLSPECS should be ignored), SF_INCLASS_INLINE 17795 indicating that the function is an inline defined in-class. 17796 17797 This function creates a binding context for the function body 17798 as well as setting up the FUNCTION_DECL in current_function_decl. 17799 17800 For C++, we must first check whether that datum makes any sense. 17801 For example, "class A local_a(1,2);" means that variable local_a 17802 is an aggregate of type A, which should have a constructor 17803 applied to it with the argument list [1, 2]. 17804 17805 On entry, DECL_INITIAL (decl1) should be NULL_TREE or error_mark_node, 17806 or may be a BLOCK if the function has been defined previously 17807 in this translation unit. On exit, DECL_INITIAL (decl1) will be 17808 error_mark_node if the function has never been defined, or 17809 a BLOCK if the function has been defined somewhere. */ 17810 17811 bool 17812 start_preparsed_function (tree decl1, tree attrs, int flags) 17813 { 17814 tree ctype = NULL_TREE; 17815 bool doing_friend = false; 17816 17817 /* Sanity check. */ 17818 gcc_assert (VOID_TYPE_P (TREE_VALUE (void_list_node))); 17819 gcc_assert (TREE_CHAIN (void_list_node) == NULL_TREE); 17820 17821 tree fntype = TREE_TYPE (decl1); 17822 if (DECL_CLASS_SCOPE_P (decl1)) 17823 ctype = DECL_CONTEXT (decl1); 17824 else 17825 { 17826 ctype = DECL_FRIEND_CONTEXT (decl1); 17827 17828 if (ctype) 17829 doing_friend = true; 17830 } 17831 17832 if (DECL_DECLARED_INLINE_P (decl1) 17833 && lookup_attribute ("noinline", attrs)) 17834 warning_at (DECL_SOURCE_LOCATION (decl1), 0, 17835 "inline function %qD given attribute %qs", decl1, "noinline"); 17836 17837 /* Handle gnu_inline attribute. */ 17838 if (GNU_INLINE_P (decl1)) 17839 { 17840 DECL_EXTERNAL (decl1) = 1; 17841 DECL_NOT_REALLY_EXTERN (decl1) = 0; 17842 DECL_INTERFACE_KNOWN (decl1) = 1; 17843 DECL_DISREGARD_INLINE_LIMITS (decl1) = 1; 17844 } 17845 17846 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl1)) 17847 /* This is a constructor, we must ensure that any default args 17848 introduced by this definition are propagated to the clones 17849 now. The clones are used directly in overload resolution. */ 17850 adjust_clone_args (decl1); 17851 17852 /* Sometimes we don't notice that a function is a static member, and 17853 build a METHOD_TYPE for it. Fix that up now. */ 17854 gcc_assert (!(DECL_STATIC_FUNCTION_P (decl1) 17855 && TREE_CODE (TREE_TYPE (decl1)) == METHOD_TYPE)); 17856 17857 /* Set up current_class_type, and enter the scope of the class, if 17858 appropriate. */ 17859 if (ctype) 17860 push_nested_class (ctype); 17861 17862 /* Now that we have entered the scope of the class, we must restore 17863 the bindings for any template parameters surrounding DECL1, if it 17864 is an inline member template. (Order is important; consider the 17865 case where a template parameter has the same name as a field of 17866 the class.) It is not until after this point that 17867 PROCESSING_TEMPLATE_DECL is guaranteed to be set up correctly. */ 17868 if (flags & SF_INCLASS_INLINE) 17869 maybe_begin_member_template_processing (decl1); 17870 17871 /* Effective C++ rule 15. */ 17872 if (warn_ecpp 17873 && DECL_ASSIGNMENT_OPERATOR_P (decl1) 17874 && DECL_OVERLOADED_OPERATOR_IS (decl1, NOP_EXPR) 17875 && VOID_TYPE_P (TREE_TYPE (fntype))) 17876 warning (OPT_Weffc__, 17877 "%<operator=%> should return a reference to %<*this%>"); 17878 17879 /* Make the init_value nonzero so pushdecl knows this is not tentative. 17880 error_mark_node is replaced below (in poplevel) with the BLOCK. */ 17881 if (!DECL_INITIAL (decl1)) 17882 DECL_INITIAL (decl1) = error_mark_node; 17883 17884 /* This function exists in static storage. 17885 (This does not mean `static' in the C sense!) */ 17886 TREE_STATIC (decl1) = 1; 17887 17888 /* We must call push_template_decl after current_class_type is set 17889 up. (If we are processing inline definitions after exiting a 17890 class scope, current_class_type will be NULL_TREE until set above 17891 by push_nested_class.) */ 17892 if (processing_template_decl) 17893 { 17894 tree newdecl1 = push_template_decl (decl1, doing_friend); 17895 if (newdecl1 == error_mark_node) 17896 { 17897 if (ctype) 17898 pop_nested_class (); 17899 return false; 17900 } 17901 decl1 = newdecl1; 17902 } 17903 17904 /* Make sure the parameter and return types are reasonable. When 17905 you declare a function, these types can be incomplete, but they 17906 must be complete when you define the function. */ 17907 check_function_type (decl1, DECL_ARGUMENTS (decl1)); 17908 17909 /* Build the return declaration for the function. */ 17910 tree restype = TREE_TYPE (fntype); 17911 17912 if (DECL_RESULT (decl1) == NULL_TREE) 17913 { 17914 /* In a template instantiation, copy the return type location. When 17915 parsing, the location will be set in grokdeclarator. */ 17916 location_t loc = input_location; 17917 if (DECL_TEMPLATE_INSTANTIATION (decl1)) 17918 { 17919 tree tmpl = template_for_substitution (decl1); 17920 if (tree res = DECL_RESULT (DECL_TEMPLATE_RESULT (tmpl))) 17921 loc = DECL_SOURCE_LOCATION (res); 17922 } 17923 17924 tree resdecl = build_decl (loc, RESULT_DECL, 0, restype); 17925 DECL_ARTIFICIAL (resdecl) = 1; 17926 DECL_IGNORED_P (resdecl) = 1; 17927 DECL_RESULT (decl1) = resdecl; 17928 17929 cp_apply_type_quals_to_decl (cp_type_quals (restype), resdecl); 17930 } 17931 17932 /* Record the decl so that the function name is defined. 17933 If we already have a decl for this name, and it is a FUNCTION_DECL, 17934 use the old decl. */ 17935 if (!processing_template_decl && !(flags & SF_PRE_PARSED)) 17936 { 17937 /* A specialization is not used to guide overload resolution. */ 17938 if (!DECL_FUNCTION_MEMBER_P (decl1) 17939 && !(DECL_USE_TEMPLATE (decl1) && 17940 PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl1)))) 17941 { 17942 tree olddecl = pushdecl (decl1); 17943 17944 if (olddecl == error_mark_node) 17945 /* If something went wrong when registering the declaration, 17946 use DECL1; we have to have a FUNCTION_DECL to use when 17947 parsing the body of the function. */ 17948 ; 17949 else 17950 { 17951 /* Otherwise, OLDDECL is either a previous declaration 17952 of the same function or DECL1 itself. */ 17953 17954 if (warn_missing_declarations 17955 && olddecl == decl1 17956 && !DECL_MAIN_P (decl1) 17957 && TREE_PUBLIC (decl1) 17958 && !DECL_DECLARED_INLINE_P (decl1)) 17959 { 17960 tree context; 17961 17962 /* Check whether DECL1 is in an anonymous 17963 namespace. */ 17964 for (context = DECL_CONTEXT (decl1); 17965 context; 17966 context = DECL_CONTEXT (context)) 17967 { 17968 if (TREE_CODE (context) == NAMESPACE_DECL 17969 && DECL_NAME (context) == NULL_TREE) 17970 break; 17971 } 17972 17973 if (context == NULL) 17974 warning_at (DECL_SOURCE_LOCATION (decl1), 17975 OPT_Wmissing_declarations, 17976 "no previous declaration for %qD", decl1); 17977 } 17978 17979 decl1 = olddecl; 17980 } 17981 } 17982 else 17983 { 17984 /* We need to set the DECL_CONTEXT. */ 17985 if (!DECL_CONTEXT (decl1) && DECL_TEMPLATE_INFO (decl1)) 17986 DECL_CONTEXT (decl1) = DECL_CONTEXT (DECL_TI_TEMPLATE (decl1)); 17987 } 17988 fntype = TREE_TYPE (decl1); 17989 restype = TREE_TYPE (fntype); 17990 17991 /* If #pragma weak applies, mark the decl appropriately now. 17992 The pragma only applies to global functions. Because 17993 determining whether or not the #pragma applies involves 17994 computing the mangled name for the declaration, we cannot 17995 apply the pragma until after we have merged this declaration 17996 with any previous declarations; if the original declaration 17997 has a linkage specification, that specification applies to 17998 the definition as well, and may affect the mangled name. */ 17999 if (DECL_FILE_SCOPE_P (decl1)) 18000 maybe_apply_pragma_weak (decl1); 18001 } 18002 18003 /* We are now in the scope of the function being defined. */ 18004 current_function_decl = decl1; 18005 18006 /* Save the parm names or decls from this function's declarator 18007 where store_parm_decls will find them. */ 18008 tree current_function_parms = DECL_ARGUMENTS (decl1); 18009 18010 /* Let the user know we're compiling this function. */ 18011 announce_function (decl1); 18012 18013 gcc_assert (DECL_INITIAL (decl1)); 18014 18015 /* This function may already have been parsed, in which case just 18016 return; our caller will skip over the body without parsing. */ 18017 if (DECL_INITIAL (decl1) != error_mark_node) 18018 return true; 18019 18020 /* Initialize RTL machinery. We cannot do this until 18021 CURRENT_FUNCTION_DECL and DECL_RESULT are set up. We do this 18022 even when processing a template; this is how we get 18023 CFUN set up, and our per-function variables initialized. 18024 FIXME factor out the non-RTL stuff. */ 18025 cp_binding_level *bl = current_binding_level; 18026 allocate_struct_function (decl1, processing_template_decl); 18027 18028 /* Initialize the language data structures. Whenever we start 18029 a new function, we destroy temporaries in the usual way. */ 18030 cfun->language = ggc_cleared_alloc<language_function> (); 18031 current_stmt_tree ()->stmts_are_full_exprs_p = 1; 18032 current_binding_level = bl; 18033 18034 /* If we are (erroneously) defining a function that we have already 18035 defined before, wipe out what we knew before. */ 18036 gcc_checking_assert (!DECL_PENDING_INLINE_P (decl1)); 18037 FNDECL_USED_AUTO (decl1) = false; 18038 DECL_SAVED_AUTO_RETURN_TYPE (decl1) = NULL; 18039 18040 if (!processing_template_decl && type_uses_auto (restype)) 18041 { 18042 FNDECL_USED_AUTO (decl1) = true; 18043 DECL_SAVED_AUTO_RETURN_TYPE (decl1) = restype; 18044 } 18045 18046 /* Start the statement-tree, start the tree now. */ 18047 DECL_SAVED_TREE (decl1) = push_stmt_list (); 18048 18049 if (DECL_IOBJ_MEMBER_FUNCTION_P (decl1)) 18050 { 18051 /* We know that this was set up by `grokclassfn'. We do not 18052 wait until `store_parm_decls', since evil parse errors may 18053 never get us to that point. Here we keep the consistency 18054 between `current_class_type' and `current_class_ptr'. */ 18055 tree t = DECL_ARGUMENTS (decl1); 18056 18057 gcc_assert (t != NULL_TREE && TREE_CODE (t) == PARM_DECL); 18058 gcc_assert (TYPE_PTR_P (TREE_TYPE (t))); 18059 18060 cp_function_chain->x_current_class_ref 18061 = cp_build_fold_indirect_ref (t); 18062 /* Set this second to avoid shortcut in cp_build_indirect_ref. */ 18063 cp_function_chain->x_current_class_ptr = t; 18064 18065 /* Constructors and destructors need to know whether they're "in 18066 charge" of initializing virtual base classes. */ 18067 t = DECL_CHAIN (t); 18068 if (DECL_HAS_IN_CHARGE_PARM_P (decl1)) 18069 { 18070 current_in_charge_parm = t; 18071 t = DECL_CHAIN (t); 18072 } 18073 if (DECL_HAS_VTT_PARM_P (decl1)) 18074 { 18075 gcc_assert (DECL_NAME (t) == vtt_parm_identifier); 18076 current_vtt_parm = t; 18077 } 18078 } 18079 18080 bool honor_interface = (!DECL_TEMPLATE_INSTANTIATION (decl1) 18081 /* Implicitly-defined methods (like the 18082 destructor for a class in which no destructor 18083 is explicitly declared) must not be defined 18084 until their definition is needed. So, we 18085 ignore interface specifications for 18086 compiler-generated functions. */ 18087 && !DECL_ARTIFICIAL (decl1)); 18088 struct c_fileinfo *finfo 18089 = get_fileinfo (LOCATION_FILE (DECL_SOURCE_LOCATION (decl1))); 18090 18091 if (processing_template_decl) 18092 /* Don't mess with interface flags. */; 18093 else if (DECL_INTERFACE_KNOWN (decl1)) 18094 { 18095 tree ctx = decl_function_context (decl1); 18096 18097 if (DECL_NOT_REALLY_EXTERN (decl1)) 18098 DECL_EXTERNAL (decl1) = 0; 18099 18100 if (ctx != NULL_TREE && vague_linkage_p (ctx)) 18101 /* This is a function in a local class in an extern inline 18102 or template function. */ 18103 comdat_linkage (decl1); 18104 } 18105 /* If this function belongs to an interface, it is public. 18106 If it belongs to someone else's interface, it is also external. 18107 This only affects inlines and template instantiations. */ 18108 else if (!finfo->interface_unknown && honor_interface) 18109 { 18110 if (DECL_DECLARED_INLINE_P (decl1) 18111 || DECL_TEMPLATE_INSTANTIATION (decl1)) 18112 { 18113 DECL_EXTERNAL (decl1) 18114 = (finfo->interface_only 18115 || (DECL_DECLARED_INLINE_P (decl1) 18116 && ! flag_implement_inlines 18117 && !DECL_VINDEX (decl1))); 18118 18119 /* For WIN32 we also want to put these in linkonce sections. */ 18120 maybe_make_one_only (decl1); 18121 } 18122 else 18123 DECL_EXTERNAL (decl1) = 0; 18124 DECL_INTERFACE_KNOWN (decl1) = 1; 18125 /* If this function is in an interface implemented in this file, 18126 make sure that the back end knows to emit this function 18127 here. */ 18128 if (!DECL_EXTERNAL (decl1)) 18129 mark_needed (decl1); 18130 } 18131 else if (finfo->interface_unknown && finfo->interface_only 18132 && honor_interface) 18133 { 18134 /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma 18135 interface, we will have both finfo->interface_unknown and 18136 finfo->interface_only set. In that case, we don't want to 18137 use the normal heuristics because someone will supply a 18138 #pragma implementation elsewhere, and deducing it here would 18139 produce a conflict. */ 18140 comdat_linkage (decl1); 18141 DECL_EXTERNAL (decl1) = 0; 18142 DECL_INTERFACE_KNOWN (decl1) = 1; 18143 DECL_DEFER_OUTPUT (decl1) = 1; 18144 } 18145 else 18146 { 18147 /* This is a definition, not a reference. 18148 So clear DECL_EXTERNAL, unless this is a GNU extern inline. */ 18149 if (!GNU_INLINE_P (decl1)) 18150 DECL_EXTERNAL (decl1) = 0; 18151 18152 if ((DECL_DECLARED_INLINE_P (decl1) 18153 || DECL_TEMPLATE_INSTANTIATION (decl1)) 18154 && ! DECL_INTERFACE_KNOWN (decl1)) 18155 DECL_DEFER_OUTPUT (decl1) = 1; 18156 else 18157 DECL_INTERFACE_KNOWN (decl1) = 1; 18158 } 18159 18160 /* Determine the ELF visibility attribute for the function. We must not 18161 do this before calling "pushdecl", as we must allow "duplicate_decls" 18162 to merge any attributes appropriately. We also need to wait until 18163 linkage is set. */ 18164 if (!DECL_CLONED_FUNCTION_P (decl1)) 18165 determine_visibility (decl1); 18166 18167 if (!processing_template_decl) 18168 maybe_instantiate_noexcept (decl1); 18169 18170 begin_scope (sk_function_parms, decl1); 18171 18172 ++function_depth; 18173 18174 start_fname_decls (); 18175 18176 store_parm_decls (current_function_parms); 18177 18178 start_function_contracts (decl1); 18179 18180 if (!processing_template_decl 18181 && (flag_lifetime_dse > 1) 18182 && DECL_CONSTRUCTOR_P (decl1) 18183 && !DECL_CLONED_FUNCTION_P (decl1) 18184 /* Clobbering an empty base is harmful if it overlays real data. */ 18185 && !is_empty_class (current_class_type) 18186 /* We can't clobber safely for an implicitly-defined default constructor 18187 because part of the initialization might happen before we enter the 18188 constructor, via AGGR_INIT_ZERO_FIRST (c++/68006). */ 18189 && !implicit_default_ctor_p (decl1)) 18190 finish_expr_stmt (build_clobber_this (CLOBBER_OBJECT_BEGIN)); 18191 18192 if (!processing_template_decl 18193 && DECL_CONSTRUCTOR_P (decl1) 18194 && sanitize_flags_p (SANITIZE_VPTR) 18195 && !DECL_CLONED_FUNCTION_P (decl1) 18196 && !implicit_default_ctor_p (decl1)) 18197 cp_ubsan_maybe_initialize_vtbl_ptrs (current_class_ptr); 18198 18199 if (!DECL_OMP_DECLARE_REDUCTION_P (decl1)) 18200 start_lambda_scope (decl1); 18201 18202 return true; 18203 } 18204 18205 18206 /* Like start_preparsed_function, except that instead of a 18207 FUNCTION_DECL, this function takes DECLSPECS and DECLARATOR. 18208 18209 Returns true on success. If the DECLARATOR is not suitable 18210 for a function, we return false, which tells the parser to 18211 skip the entire function. */ 18212 18213 bool 18214 start_function (cp_decl_specifier_seq *declspecs, 18215 const cp_declarator *declarator, 18216 tree attrs) 18217 { 18218 tree decl1; 18219 18220 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, &attrs); 18221 invoke_plugin_callbacks (PLUGIN_START_PARSE_FUNCTION, decl1); 18222 if (decl1 == error_mark_node) 18223 return false; 18224 18225 if (DECL_MAIN_P (decl1)) 18226 /* main must return int. grokfndecl should have corrected it 18227 (and issued a diagnostic) if the user got it wrong. */ 18228 gcc_assert (same_type_p (TREE_TYPE (TREE_TYPE (decl1)), 18229 integer_type_node)); 18230 18231 return start_preparsed_function (decl1, attrs, /*flags=*/SF_DEFAULT); 18232 } 18233 18234 /* Returns true iff an EH_SPEC_BLOCK should be created in the body of 18236 FN. */ 18237 18238 static bool 18239 use_eh_spec_block (tree fn) 18240 { 18241 return (flag_exceptions && flag_enforce_eh_specs 18242 && !processing_template_decl 18243 /* We insert the EH_SPEC_BLOCK only in the original 18244 function; then, it is copied automatically to the 18245 clones. */ 18246 && !DECL_CLONED_FUNCTION_P (fn) 18247 /* Implicitly-generated constructors and destructors have 18248 exception specifications. However, those specifications 18249 are the union of the possible exceptions specified by the 18250 constructors/destructors for bases and members, so no 18251 unallowed exception will ever reach this function. By 18252 not creating the EH_SPEC_BLOCK we save a little memory, 18253 and we avoid spurious warnings about unreachable 18254 code. */ 18255 && !DECL_DEFAULTED_FN (fn) 18256 && !type_throw_all_p (TREE_TYPE (fn))); 18257 } 18258 18259 /* Helper function to push ARGS into the current lexical scope. DECL 18260 is the function declaration. NONPARMS is used to handle enum 18261 constants. */ 18262 18263 void 18264 do_push_parm_decls (tree decl, tree args, tree *nonparms) 18265 { 18266 /* If we're doing semantic analysis, then we'll call pushdecl 18267 for each of these. We must do them in reverse order so that 18268 they end in the correct forward order. */ 18269 args = nreverse (args); 18270 18271 tree next; 18272 for (tree parm = args; parm; parm = next) 18273 { 18274 next = DECL_CHAIN (parm); 18275 if (TREE_CODE (parm) == PARM_DECL) 18276 pushdecl (parm); 18277 else if (nonparms) 18278 { 18279 /* If we find an enum constant or a type tag, put it aside for 18280 the moment. */ 18281 TREE_CHAIN (parm) = NULL_TREE; 18282 *nonparms = chainon (*nonparms, parm); 18283 } 18284 } 18285 18286 /* Get the decls in their original chain order and record in the 18287 function. This is all and only the PARM_DECLs that were 18288 pushed into scope by the loop above. */ 18289 DECL_ARGUMENTS (decl) = get_local_decls (); 18290 } 18291 18292 /* Store the parameter declarations into the current function declaration. 18293 This is called after parsing the parameter declarations, before 18294 digesting the body of the function. 18295 18296 Also install to binding contour return value identifier, if any. */ 18297 18298 static void 18299 store_parm_decls (tree current_function_parms) 18300 { 18301 tree fndecl = current_function_decl; 18302 18303 /* This is a chain of any other decls that came in among the parm 18304 declarations. If a parm is declared with enum {foo, bar} x; 18305 then CONST_DECLs for foo and bar are put here. */ 18306 tree nonparms = NULL_TREE; 18307 18308 if (current_function_parms) 18309 { 18310 /* This case is when the function was defined with an ANSI prototype. 18311 The parms already have decls, so we need not do anything here 18312 except record them as in effect 18313 and complain if any redundant old-style parm decls were written. */ 18314 18315 tree specparms = current_function_parms; 18316 18317 /* Must clear this because it might contain TYPE_DECLs declared 18318 at class level. */ 18319 current_binding_level->names = NULL; 18320 18321 do_push_parm_decls (fndecl, specparms, &nonparms); 18322 } 18323 else 18324 DECL_ARGUMENTS (fndecl) = NULL_TREE; 18325 18326 /* Now store the final chain of decls for the arguments 18327 as the decl-chain of the current lexical scope. 18328 Put the enumerators in as well, at the front so that 18329 DECL_ARGUMENTS is not modified. */ 18330 current_binding_level->names = chainon (nonparms, DECL_ARGUMENTS (fndecl)); 18331 18332 if (use_eh_spec_block (current_function_decl)) 18333 current_eh_spec_block = begin_eh_spec_block (); 18334 } 18335 18336 18337 /* Mark CDTOR's implicit THIS argument for returning, if required by 18339 the ABI.. Return the decl for THIS, if it is to be returned, and 18340 NULL otherwise. */ 18341 18342 tree 18343 maybe_prepare_return_this (tree cdtor) 18344 { 18345 if (targetm.cxx.cdtor_returns_this ()) 18346 if (tree val = DECL_ARGUMENTS (cdtor)) 18347 { 18348 suppress_warning (val, OPT_Wuse_after_free); 18349 return val; 18350 } 18351 18352 return NULL_TREE; 18353 } 18354 18355 /* Set the return value of the [cd]tor if the ABI wants that. */ 18356 18357 void 18358 maybe_return_this () 18359 { 18360 if (tree val = maybe_prepare_return_this (current_function_decl)) 18361 { 18362 /* Return the address of the object. */ 18363 val = fold_convert (TREE_TYPE (DECL_RESULT (current_function_decl)), val); 18364 val = build2 (MODIFY_EXPR, TREE_TYPE (val), 18365 DECL_RESULT (current_function_decl), val); 18366 tree exprstmt = build_stmt (input_location, RETURN_EXPR, val); 18367 add_stmt (exprstmt); 18368 } 18369 } 18370 18371 /* Do all the processing for the beginning of a destructor; set up the 18372 vtable pointers and cleanups for bases and members. */ 18373 18374 static void 18375 begin_destructor_body (void) 18376 { 18377 tree compound_stmt; 18378 18379 /* If the CURRENT_CLASS_TYPE is incomplete, we will have already 18380 issued an error message. We still want to try to process the 18381 body of the function, but initialize_vtbl_ptrs will crash if 18382 TYPE_BINFO is NULL. */ 18383 if (COMPLETE_TYPE_P (current_class_type)) 18384 { 18385 compound_stmt = begin_compound_stmt (0); 18386 /* Make all virtual function table pointers in non-virtual base 18387 classes point to CURRENT_CLASS_TYPE's virtual function 18388 tables. */ 18389 initialize_vtbl_ptrs (current_class_ptr); 18390 finish_compound_stmt (compound_stmt); 18391 18392 if (flag_lifetime_dse 18393 /* Clobbering an empty base is harmful if it overlays real data. */ 18394 && !is_empty_class (current_class_type)) 18395 { 18396 if (sanitize_flags_p (SANITIZE_VPTR) 18397 && (flag_sanitize_recover & SANITIZE_VPTR) == 0 18398 && TYPE_CONTAINS_VPTR_P (current_class_type)) 18399 { 18400 tree binfo = TYPE_BINFO (current_class_type); 18401 tree ref 18402 = cp_build_fold_indirect_ref (current_class_ptr); 18403 18404 tree vtbl_ptr = build_vfield_ref (ref, TREE_TYPE (binfo)); 18405 tree vtbl = build_zero_cst (TREE_TYPE (vtbl_ptr)); 18406 tree stmt = cp_build_modify_expr (input_location, vtbl_ptr, 18407 NOP_EXPR, vtbl, 18408 tf_warning_or_error); 18409 /* If the vptr is shared with some virtual nearly empty base, 18410 don't clear it if not in charge, the dtor of the virtual 18411 nearly empty base will do that later. */ 18412 if (CLASSTYPE_VBASECLASSES (current_class_type)) 18413 { 18414 tree c = current_class_type; 18415 while (CLASSTYPE_PRIMARY_BINFO (c)) 18416 { 18417 if (BINFO_VIRTUAL_P (CLASSTYPE_PRIMARY_BINFO (c))) 18418 { 18419 stmt = convert_to_void (stmt, ICV_STATEMENT, 18420 tf_warning_or_error); 18421 stmt = build_if_in_charge (stmt); 18422 break; 18423 } 18424 c = BINFO_TYPE (CLASSTYPE_PRIMARY_BINFO (c)); 18425 } 18426 } 18427 finish_decl_cleanup (NULL_TREE, stmt); 18428 } 18429 else 18430 finish_decl_cleanup (NULL_TREE, 18431 build_clobber_this (CLOBBER_OBJECT_END)); 18432 } 18433 18434 /* And insert cleanups for our bases and members so that they 18435 will be properly destroyed if we throw. */ 18436 push_base_cleanups (); 18437 } 18438 } 18439 18440 /* Do the necessary processing for the beginning of a function body, which 18441 in this case includes member-initializers, but not the catch clauses of 18442 a function-try-block. Currently, this means opening a binding level 18443 for the member-initializers (in a ctor), member cleanups (in a dtor), 18444 and capture proxies (in a lambda operator()). */ 18445 18446 tree 18447 begin_function_body (void) 18448 { 18449 if (! FUNCTION_NEEDS_BODY_BLOCK (current_function_decl)) 18450 return NULL_TREE; 18451 18452 if (processing_template_decl) 18453 /* Do nothing now. */; 18454 else 18455 /* Always keep the BLOCK node associated with the outermost pair of 18456 curly braces of a function. These are needed for correct 18457 operation of dwarfout.c. */ 18458 keep_next_level (true); 18459 18460 tree stmt = begin_compound_stmt (BCS_FN_BODY); 18461 current_binding_level->artificial = 1; 18462 18463 if (processing_template_decl) 18464 /* Do nothing now. */; 18465 else if (DECL_DESTRUCTOR_P (current_function_decl)) 18466 begin_destructor_body (); 18467 18468 return stmt; 18469 } 18470 18471 /* Do the processing for the end of a function body. Currently, this means 18472 closing out the cleanups for fully-constructed bases and members, and in 18473 the case of the destructor, deleting the object if desired. Again, this 18474 is only meaningful for [cd]tors, since they are the only functions where 18475 there is a significant distinction between the main body and any 18476 function catch clauses. Handling, say, main() return semantics here 18477 would be wrong, as flowing off the end of a function catch clause for 18478 main() would also need to return 0. */ 18479 18480 void 18481 finish_function_body (tree compstmt) 18482 { 18483 if (compstmt == NULL_TREE) 18484 return; 18485 18486 /* Close the block. */ 18487 finish_compound_stmt (compstmt); 18488 18489 if (processing_template_decl) 18490 /* Do nothing now. */; 18491 else if (DECL_CONSTRUCTOR_P (current_function_decl) 18492 || DECL_DESTRUCTOR_P (current_function_decl)) 18493 maybe_return_this (); 18494 } 18495 18496 /* Given a function, returns the BLOCK corresponding to the outermost level 18497 of curly braces, skipping the artificial block created for constructor 18498 initializers. */ 18499 18500 tree 18501 outer_curly_brace_block (tree fndecl) 18502 { 18503 tree block = DECL_INITIAL (fndecl); 18504 if (BLOCK_OUTER_CURLY_BRACE_P (block)) 18505 return block; 18506 block = BLOCK_SUBBLOCKS (block); 18507 if (BLOCK_OUTER_CURLY_BRACE_P (block)) 18508 return block; 18509 block = BLOCK_SUBBLOCKS (block); 18510 gcc_assert (BLOCK_OUTER_CURLY_BRACE_P (block)); 18511 return block; 18512 } 18513 18514 /* If FNDECL is a class's key method, add the class to the list of 18515 keyed classes that should be emitted. */ 18516 18517 static void 18518 record_key_method_defined (tree fndecl) 18519 { 18520 if (DECL_OBJECT_MEMBER_FUNCTION_P (fndecl) 18521 && DECL_VIRTUAL_P (fndecl) 18522 && !processing_template_decl) 18523 { 18524 tree fnclass = DECL_CONTEXT (fndecl); 18525 if (fndecl == CLASSTYPE_KEY_METHOD (fnclass)) 18526 vec_safe_push (keyed_classes, fnclass); 18527 } 18528 } 18529 18530 /* Attempt to add a fix-it hint to RICHLOC suggesting the insertion 18531 of "return *this;" immediately before its location, using FNDECL's 18532 first statement (if any) to give the indentation, if appropriate. */ 18533 18534 static void 18535 add_return_star_this_fixit (gcc_rich_location *richloc, tree fndecl) 18536 { 18537 location_t indent = UNKNOWN_LOCATION; 18538 tree stmts = expr_first (DECL_SAVED_TREE (fndecl)); 18539 if (stmts) 18540 indent = EXPR_LOCATION (stmts); 18541 richloc->add_fixit_insert_formatted ("return *this;", 18542 richloc->get_loc (), 18543 indent); 18544 } 18545 18546 /* This function carries out the subset of finish_function operations needed 18547 to emit the compiler-generated outlined helper functions used by the 18548 coroutines implementation. */ 18549 18550 static void 18551 emit_coro_helper (tree helper) 18552 { 18553 /* This is a partial set of the operations done by finish_function() 18554 plus emitting the result. */ 18555 set_cfun (NULL); 18556 current_function_decl = helper; 18557 begin_scope (sk_function_parms, NULL); 18558 store_parm_decls (DECL_ARGUMENTS (helper)); 18559 announce_function (helper); 18560 allocate_struct_function (helper, false); 18561 cfun->language = ggc_cleared_alloc<language_function> (); 18562 poplevel (1, 0, 1); 18563 maybe_save_constexpr_fundef (helper); 18564 /* We must start each function with a clear fold cache. */ 18565 clear_fold_cache (); 18566 cp_fold_function (helper); 18567 DECL_CONTEXT (DECL_RESULT (helper)) = helper; 18568 BLOCK_SUPERCONTEXT (DECL_INITIAL (helper)) = helper; 18569 /* This function has coroutine IFNs that we should handle in middle 18570 end lowering. */ 18571 cfun->coroutine_component = true; 18572 cp_genericize (helper); 18573 expand_or_defer_fn (helper); 18574 } 18575 18576 /* Finish up a function declaration and compile that function 18577 all the way to assembler language output. The free the storage 18578 for the function definition. INLINE_P is TRUE if we just 18579 finished processing the body of an in-class inline function 18580 definition. (This processing will have taken place after the 18581 class definition is complete.) */ 18582 18583 tree 18584 finish_function (bool inline_p) 18585 { 18586 tree fndecl = current_function_decl; 18587 tree fntype, ctype = NULL_TREE; 18588 tree resumer = NULL_TREE, destroyer = NULL_TREE; 18589 18590 /* When we get some parse errors, we can end up without a 18591 current_function_decl, so cope. */ 18592 if (fndecl == NULL_TREE || fndecl == error_mark_node) 18593 return error_mark_node; 18594 18595 bool coro_p = (flag_coroutines 18596 && !processing_template_decl 18597 && DECL_COROUTINE_P (fndecl)); 18598 bool coro_emit_helpers = false; 18599 bool do_contracts = (DECL_HAS_CONTRACTS_P (fndecl) 18600 && !processing_template_decl); 18601 18602 if (!DECL_OMP_DECLARE_REDUCTION_P (fndecl)) 18603 finish_lambda_scope (); 18604 18605 if (c_dialect_objc ()) 18606 objc_finish_function (); 18607 18608 record_key_method_defined (fndecl); 18609 18610 fntype = TREE_TYPE (fndecl); 18611 18612 /* TREE_READONLY (fndecl) = 1; 18613 This caused &foo to be of type ptr-to-const-function 18614 which then got a warning when stored in a ptr-to-function variable. */ 18615 18616 gcc_assert (building_stmt_list_p ()); 18617 /* The current function is being defined, so its DECL_INITIAL should 18618 be set, and unless there's a multiple definition, it should be 18619 error_mark_node. */ 18620 gcc_assert (DECL_INITIAL (fndecl) == error_mark_node); 18621 18622 if (coro_p) 18623 { 18624 /* Only try to emit the coroutine outlined helper functions if the 18625 transforms succeeded. Otherwise, treat errors in the same way as 18626 a regular function. */ 18627 coro_emit_helpers = morph_fn_to_coro (fndecl, &resumer, &destroyer); 18628 18629 /* We should handle coroutine IFNs in middle end lowering. */ 18630 cfun->coroutine_component = true; 18631 18632 /* Do not try to process the ramp's EH unless outlining succeeded. */ 18633 if (coro_emit_helpers && use_eh_spec_block (fndecl)) 18634 finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS 18635 (TREE_TYPE (fndecl)), 18636 current_eh_spec_block); 18637 18638 /* If outlining succeeded, then add contracts handling if needed. */ 18639 if (coro_emit_helpers && do_contracts) 18640 maybe_apply_function_contracts (fndecl); 18641 } 18642 else 18643 /* For a cloned function, we've already got all the code we need; 18644 there's no need to add any extra bits. */ 18645 if (!DECL_CLONED_FUNCTION_P (fndecl)) 18646 { 18647 /* Make it so that `main' always returns 0 by default. */ 18648 if (DECL_MAIN_FREESTANDING_P (current_function_decl) 18649 && !TREE_THIS_VOLATILE (current_function_decl)) 18650 finish_return_stmt (integer_zero_node); 18651 18652 if (use_eh_spec_block (current_function_decl)) 18653 finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS 18654 (TREE_TYPE (current_function_decl)), 18655 current_eh_spec_block); 18656 18657 if (do_contracts) 18658 maybe_apply_function_contracts (current_function_decl); 18659 18660 } 18661 18662 /* If we're saving up tree structure, tie off the function now. */ 18663 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl)); 18664 18665 finish_fname_decls (); 18666 18667 /* This must come after expand_function_end because cleanups might 18668 have declarations (from inline functions) that need to go into 18669 this function's blocks. */ 18670 18671 /* If the current binding level isn't the outermost binding level 18672 for this function, either there is a bug, or we have experienced 18673 syntax errors and the statement tree is malformed. */ 18674 if (current_binding_level->kind != sk_function_parms) 18675 { 18676 /* Make sure we have already experienced errors. */ 18677 gcc_assert (errorcount); 18678 18679 /* Throw away the broken statement tree and extra binding 18680 levels. */ 18681 DECL_SAVED_TREE (fndecl) = alloc_stmt_list (); 18682 18683 while (current_binding_level->kind != sk_function_parms) 18684 { 18685 if (current_binding_level->kind == sk_class) 18686 pop_nested_class (); 18687 else 18688 poplevel (0, 0, 0); 18689 } 18690 } 18691 poplevel (1, 0, 1); 18692 18693 /* Statements should always be full-expressions at the outermost set 18694 of curly braces for a function. */ 18695 gcc_assert (stmts_are_full_exprs_p ()); 18696 18697 /* If there are no return statements in a function with auto return type, 18698 the return type is void. But if the declared type is something like 18699 auto*, this is an error. */ 18700 if (!processing_template_decl && FNDECL_USED_AUTO (fndecl) 18701 && TREE_TYPE (fntype) == DECL_SAVED_AUTO_RETURN_TYPE (fndecl)) 18702 { 18703 if (is_auto (DECL_SAVED_AUTO_RETURN_TYPE (fndecl)) 18704 && !current_function_returns_value 18705 && !current_function_returns_null) 18706 { 18707 /* We haven't applied return type deduction because we haven't 18708 seen any return statements. Do that now. */ 18709 tree node = type_uses_auto (DECL_SAVED_AUTO_RETURN_TYPE (fndecl)); 18710 do_auto_deduction (DECL_SAVED_AUTO_RETURN_TYPE (fndecl), 18711 void_node, node, tf_warning_or_error, 18712 adc_return_type); 18713 18714 apply_deduced_return_type (fndecl, void_type_node); 18715 fntype = TREE_TYPE (fndecl); 18716 } 18717 else if (!current_function_returns_value 18718 && !current_function_returns_null) 18719 { 18720 error ("no return statements in function returning %qT", 18721 DECL_SAVED_AUTO_RETURN_TYPE (fndecl)); 18722 inform (input_location, "only plain %<auto%> return type can be " 18723 "deduced to %<void%>"); 18724 } 18725 } 18726 18727 /* Remember that we were in class scope. */ 18728 if (current_class_name) 18729 ctype = current_class_type; 18730 18731 if (DECL_DELETED_FN (fndecl)) 18732 { 18733 DECL_INITIAL (fndecl) = error_mark_node; 18734 DECL_SAVED_TREE (fndecl) = NULL_TREE; 18735 goto cleanup; 18736 } 18737 18738 // If this is a concept, check that the definition is reasonable. 18739 if (DECL_DECLARED_CONCEPT_P (fndecl)) 18740 check_function_concept (fndecl); 18741 18742 if (flag_openmp) 18743 if (tree attr = lookup_attribute ("omp declare variant base", 18744 DECL_ATTRIBUTES (fndecl))) 18745 omp_declare_variant_finalize (fndecl, attr); 18746 18747 /* Complain if there's just no return statement. */ 18748 if ((warn_return_type 18749 || (cxx_dialect >= cxx14 18750 && DECL_DECLARED_CONSTEXPR_P (fndecl))) 18751 && !VOID_TYPE_P (TREE_TYPE (fntype)) 18752 && !dependent_type_p (TREE_TYPE (fntype)) 18753 && !current_function_returns_value && !current_function_returns_null 18754 /* Don't complain if we abort or throw. */ 18755 && !current_function_returns_abnormally 18756 /* Don't complain if there's an infinite loop. */ 18757 && !current_function_infinite_loop 18758 /* Don't complain if we are declared noreturn. */ 18759 && !TREE_THIS_VOLATILE (fndecl) 18760 && !DECL_NAME (DECL_RESULT (fndecl)) 18761 && !warning_suppressed_p (fndecl, OPT_Wreturn_type) 18762 /* Structor return values (if any) are set by the compiler. */ 18763 && !DECL_CONSTRUCTOR_P (fndecl) 18764 && !DECL_DESTRUCTOR_P (fndecl) 18765 && targetm.warn_func_return (fndecl)) 18766 { 18767 gcc_rich_location richloc (input_location); 18768 /* Potentially add a "return *this;" fix-it hint for 18769 assignment operators. */ 18770 if (IDENTIFIER_ASSIGN_OP_P (DECL_NAME (fndecl))) 18771 { 18772 tree valtype = TREE_TYPE (DECL_RESULT (fndecl)); 18773 if (TREE_CODE (valtype) == REFERENCE_TYPE 18774 && current_class_ref 18775 && same_type_ignoring_top_level_qualifiers_p 18776 (TREE_TYPE (valtype), TREE_TYPE (current_class_ref)) 18777 && global_dc->option_enabled_p (OPT_Wreturn_type)) 18778 add_return_star_this_fixit (&richloc, fndecl); 18779 } 18780 if (cxx_dialect >= cxx14 18781 && DECL_DECLARED_CONSTEXPR_P (fndecl)) 18782 error_at (&richloc, "no return statement in %<constexpr%> function " 18783 "returning non-void"); 18784 else if (warning_at (&richloc, OPT_Wreturn_type, 18785 "no return statement in function returning " 18786 "non-void")) 18787 suppress_warning (fndecl, OPT_Wreturn_type); 18788 } 18789 18790 /* Lambda closure members are implicitly constexpr if possible. */ 18791 if (cxx_dialect >= cxx17 18792 && LAMBDA_TYPE_P (CP_DECL_CONTEXT (fndecl))) 18793 DECL_DECLARED_CONSTEXPR_P (fndecl) 18794 = ((processing_template_decl 18795 || is_valid_constexpr_fn (fndecl, /*complain*/false)) 18796 && potential_constant_expression (DECL_SAVED_TREE (fndecl))); 18797 18798 /* Save constexpr function body before it gets munged by 18799 the NRV transformation. */ 18800 maybe_save_constexpr_fundef (fndecl); 18801 18802 /* Invoke the pre-genericize plugin before we start munging things. */ 18803 if (!processing_template_decl) 18804 invoke_plugin_callbacks (PLUGIN_PRE_GENERICIZE, fndecl); 18805 18806 /* Perform delayed folding before NRV transformation. */ 18807 if (!processing_template_decl 18808 && !DECL_IMMEDIATE_FUNCTION_P (fndecl) 18809 && !DECL_OMP_DECLARE_REDUCTION_P (fndecl)) 18810 cp_fold_function (fndecl); 18811 18812 /* Set up the named return value optimization, if we can. Candidate 18813 variables are selected in check_return_expr. */ 18814 if (tree r = current_function_return_value) 18815 { 18816 if (r != error_mark_node) 18817 finalize_nrv (fndecl, r); 18818 current_function_return_value = NULL_TREE; 18819 } 18820 18821 /* Must mark the RESULT_DECL as being in this function. */ 18822 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl; 18823 18824 /* Set the BLOCK_SUPERCONTEXT of the outermost function scope to point 18825 to the FUNCTION_DECL node itself. */ 18826 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl; 18827 18828 /* Store the end of the function, so that we get good line number 18829 info for the epilogue. */ 18830 cfun->function_end_locus = input_location; 18831 18832 /* Complain about parameters that are only set, but never otherwise used. */ 18833 if (warn_unused_but_set_parameter 18834 && !processing_template_decl 18835 && errorcount == unused_but_set_errorcount 18836 && !DECL_CLONED_FUNCTION_P (fndecl)) 18837 { 18838 tree decl; 18839 18840 for (decl = DECL_ARGUMENTS (fndecl); 18841 decl; 18842 decl = DECL_CHAIN (decl)) 18843 if (TREE_USED (decl) 18844 && TREE_CODE (decl) == PARM_DECL 18845 && !DECL_READ_P (decl) 18846 && DECL_NAME (decl) 18847 && !DECL_ARTIFICIAL (decl) 18848 && !warning_suppressed_p (decl,OPT_Wunused_but_set_parameter) 18849 && !DECL_IN_SYSTEM_HEADER (decl) 18850 && TREE_TYPE (decl) != error_mark_node 18851 && !TYPE_REF_P (TREE_TYPE (decl)) 18852 && (!CLASS_TYPE_P (TREE_TYPE (decl)) 18853 || !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))) 18854 warning_at (DECL_SOURCE_LOCATION (decl), 18855 OPT_Wunused_but_set_parameter, 18856 "parameter %qD set but not used", decl); 18857 unused_but_set_errorcount = errorcount; 18858 } 18859 18860 /* Complain about locally defined typedefs that are not used in this 18861 function. */ 18862 maybe_warn_unused_local_typedefs (); 18863 18864 /* Possibly warn about unused parameters. */ 18865 if (warn_unused_parameter 18866 && !processing_template_decl 18867 && !DECL_CLONED_FUNCTION_P (fndecl)) 18868 do_warn_unused_parameter (fndecl); 18869 18870 /* Genericize before inlining. */ 18871 if (!processing_template_decl 18872 && !DECL_IMMEDIATE_FUNCTION_P (fndecl) 18873 && !DECL_OMP_DECLARE_REDUCTION_P (fndecl)) 18874 cp_genericize (fndecl); 18875 18876 /* If this function can't throw any exceptions, remember that. */ 18877 if (!processing_template_decl 18878 && !cp_function_chain->can_throw 18879 && !flag_non_call_exceptions 18880 && !decl_replaceable_p (fndecl, 18881 opt_for_fn (fndecl, flag_semantic_interposition))) 18882 TREE_NOTHROW (fndecl) = 1; 18883 18884 /* Emit the resumer and destroyer functions now, providing that we have 18885 not encountered some fatal error. */ 18886 if (coro_emit_helpers) 18887 { 18888 emit_coro_helper (resumer); 18889 emit_coro_helper (destroyer); 18890 } 18891 18892 cleanup: 18893 /* We're leaving the context of this function, so zap cfun. It's still in 18894 DECL_STRUCT_FUNCTION, and we'll restore it in tree_rest_of_compilation. */ 18895 set_cfun (NULL); 18896 current_function_decl = NULL; 18897 18898 /* If this is an in-class inline definition, we may have to pop the 18899 bindings for the template parameters that we added in 18900 maybe_begin_member_template_processing when start_function was 18901 called. */ 18902 if (inline_p) 18903 maybe_end_member_template_processing (); 18904 18905 /* Leave the scope of the class. */ 18906 if (ctype) 18907 pop_nested_class (); 18908 18909 --function_depth; 18910 18911 /* Clean up. */ 18912 invoke_plugin_callbacks (PLUGIN_FINISH_PARSE_FUNCTION, fndecl); 18913 18914 /* If we have used outlined contracts checking functions, build and emit 18915 them here. */ 18916 finish_function_contracts (fndecl); 18917 18918 return fndecl; 18919 } 18920 18921 /* Create the FUNCTION_DECL for a function definition. 18923 DECLSPECS and DECLARATOR are the parts of the declaration; 18924 they describe the return type and the name of the function, 18925 but twisted together in a fashion that parallels the syntax of C. 18926 18927 This function creates a binding context for the function body 18928 as well as setting up the FUNCTION_DECL in current_function_decl. 18929 18930 Returns a FUNCTION_DECL on success. 18931 18932 If the DECLARATOR is not suitable for a function (it defines a datum 18933 instead), we return 0, which tells yyparse to report a parse error. 18934 18935 May return void_type_node indicating that this method is actually 18936 a friend. See grokfield for more details. 18937 18938 Came here with a `.pushlevel' . 18939 18940 DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING 18941 CHANGES TO CODE IN `grokfield'. */ 18942 18943 tree 18944 grokmethod (cp_decl_specifier_seq *declspecs, 18945 const cp_declarator *declarator, tree attrlist) 18946 { 18947 tree fndecl = grokdeclarator (declarator, declspecs, MEMFUNCDEF, 0, 18948 &attrlist); 18949 18950 if (fndecl == error_mark_node) 18951 return error_mark_node; 18952 18953 if (attrlist) 18954 cplus_decl_attributes (&fndecl, attrlist, 0); 18955 18956 /* Pass friends other than inline friend functions back. */ 18957 if (fndecl == void_type_node) 18958 return fndecl; 18959 18960 if (DECL_IN_AGGR_P (fndecl)) 18961 { 18962 if (DECL_CLASS_SCOPE_P (fndecl)) 18963 error ("%qD is already defined in class %qT", fndecl, 18964 DECL_CONTEXT (fndecl)); 18965 return error_mark_node; 18966 } 18967 18968 check_template_shadow (fndecl); 18969 18970 /* p1779 ABI-Isolation makes inline not a default for in-class 18971 definitions attached to a named module. If the user explicitly 18972 made it inline, grokdeclarator will already have done the right 18973 things. */ 18974 if ((!named_module_attach_p () 18975 || flag_module_implicit_inline 18976 /* Lambda's operator function remains inline. */ 18977 || LAMBDA_TYPE_P (DECL_CONTEXT (fndecl))) 18978 /* If the user explicitly asked for this to be inline, we don't 18979 need to do more, but more importantly we want to warn if we 18980 can't inline it. */ 18981 && !DECL_DECLARED_INLINE_P (fndecl)) 18982 { 18983 if (TREE_PUBLIC (fndecl)) 18984 DECL_COMDAT (fndecl) = 1; 18985 DECL_DECLARED_INLINE_P (fndecl) = 1; 18986 /* It's ok if we can't inline this. */ 18987 DECL_NO_INLINE_WARNING_P (fndecl) = 1; 18988 } 18989 18990 /* We process method specializations in finish_struct_1. */ 18991 if (processing_template_decl && !DECL_TEMPLATE_SPECIALIZATION (fndecl)) 18992 { 18993 /* Avoid calling decl_spec_seq... until we have to. */ 18994 bool friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend); 18995 fndecl = push_template_decl (fndecl, friendp); 18996 if (fndecl == error_mark_node) 18997 return fndecl; 18998 } 18999 19000 if (DECL_CHAIN (fndecl) && !decl_spec_seq_has_spec_p (declspecs, ds_friend)) 19001 { 19002 fndecl = copy_node (fndecl); 19003 TREE_CHAIN (fndecl) = NULL_TREE; 19004 } 19005 19006 cp_finish_decl (fndecl, NULL_TREE, false, NULL_TREE, 0); 19007 19008 DECL_IN_AGGR_P (fndecl) = 1; 19009 return fndecl; 19010 } 19011 19012 19014 /* VAR is a VAR_DECL. If its type is incomplete, remember VAR so that 19015 we can lay it out later, when and if its type becomes complete. 19016 19017 Also handle constexpr variables where the initializer involves 19018 an unlowered PTRMEM_CST because the class isn't complete yet. */ 19019 19020 void 19021 maybe_register_incomplete_var (tree var) 19022 { 19023 gcc_assert (VAR_P (var)); 19024 19025 /* Keep track of variables with incomplete types. */ 19026 if (!processing_template_decl && TREE_TYPE (var) != error_mark_node 19027 && DECL_EXTERNAL (var)) 19028 { 19029 tree inner_type = TREE_TYPE (var); 19030 19031 while (TREE_CODE (inner_type) == ARRAY_TYPE) 19032 inner_type = TREE_TYPE (inner_type); 19033 inner_type = TYPE_MAIN_VARIANT (inner_type); 19034 19035 if ((!COMPLETE_TYPE_P (inner_type) && CLASS_TYPE_P (inner_type)) 19036 /* RTTI TD entries are created while defining the type_info. */ 19037 || (TYPE_LANG_SPECIFIC (inner_type) 19038 && TYPE_BEING_DEFINED (inner_type))) 19039 { 19040 incomplete_var iv = {var, inner_type}; 19041 vec_safe_push (incomplete_vars, iv); 19042 } 19043 else if (!(DECL_LANG_SPECIFIC (var) && DECL_TEMPLATE_INFO (var)) 19044 && DECL_CLASS_SCOPE_P (var) 19045 && TYPE_BEING_DEFINED (DECL_CONTEXT (var)) 19046 && decl_constant_var_p (var) 19047 && (TYPE_PTRMEM_P (inner_type) || CLASS_TYPE_P (inner_type))) 19048 { 19049 /* When the outermost open class is complete we can resolve any 19050 pointers-to-members. */ 19051 tree context = outermost_open_class (); 19052 incomplete_var iv = {var, context}; 19053 vec_safe_push (incomplete_vars, iv); 19054 } 19055 } 19056 } 19057 19058 /* Called when a class type (given by TYPE) is defined. If there are 19059 any existing VAR_DECLs whose type has been completed by this 19060 declaration, update them now. */ 19061 19062 void 19063 complete_vars (tree type) 19064 { 19065 unsigned ix; 19066 incomplete_var *iv; 19067 19068 for (ix = 0; vec_safe_iterate (incomplete_vars, ix, &iv); ) 19069 { 19070 if (same_type_p (type, iv->incomplete_type)) 19071 { 19072 tree var = iv->decl; 19073 tree type = TREE_TYPE (var); 19074 19075 if (type != error_mark_node 19076 && (TYPE_MAIN_VARIANT (strip_array_types (type)) 19077 == iv->incomplete_type)) 19078 { 19079 /* Complete the type of the variable. */ 19080 complete_type (type); 19081 cp_apply_type_quals_to_decl (cp_type_quals (type), var); 19082 if (COMPLETE_TYPE_P (type)) 19083 layout_var_decl (var); 19084 } 19085 19086 /* Remove this entry from the list. */ 19087 incomplete_vars->unordered_remove (ix); 19088 } 19089 else 19090 ix++; 19091 } 19092 } 19093 19094 /* If DECL is of a type which needs a cleanup, build and return an 19095 expression to perform that cleanup here. Return NULL_TREE if no 19096 cleanup need be done. DECL can also be a _REF when called from 19097 split_nonconstant_init_1. */ 19098 19099 tree 19100 cxx_maybe_build_cleanup (tree decl, tsubst_flags_t complain) 19101 { 19102 tree type; 19103 tree attr; 19104 tree cleanup; 19105 19106 /* Assume no cleanup is required. */ 19107 cleanup = NULL_TREE; 19108 19109 if (!decl || error_operand_p (decl)) 19110 return cleanup; 19111 19112 /* Handle "__attribute__((cleanup))". We run the cleanup function 19113 before the destructor since the destructor is what actually 19114 terminates the lifetime of the object. */ 19115 if (DECL_P (decl)) 19116 attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl)); 19117 else 19118 attr = NULL_TREE; 19119 if (attr) 19120 { 19121 tree id; 19122 tree fn; 19123 tree arg; 19124 19125 /* Get the name specified by the user for the cleanup function. */ 19126 id = TREE_VALUE (TREE_VALUE (attr)); 19127 /* Look up the name to find the cleanup function to call. It is 19128 important to use lookup_name here because that is what is 19129 used in c-common.cc:handle_cleanup_attribute when performing 19130 initial checks on the attribute. Note that those checks 19131 include ensuring that the function found is not an overloaded 19132 function, or an object with an overloaded call operator, 19133 etc.; we can rely on the fact that the function found is an 19134 ordinary FUNCTION_DECL. */ 19135 fn = lookup_name (id); 19136 arg = build_address (decl); 19137 if (!mark_used (decl, complain) && !(complain & tf_error)) 19138 return error_mark_node; 19139 cleanup = cp_build_function_call_nary (fn, complain, arg, NULL_TREE); 19140 if (cleanup == error_mark_node) 19141 return error_mark_node; 19142 } 19143 /* Handle ordinary C++ destructors. */ 19144 type = TREE_TYPE (decl); 19145 if (type_build_dtor_call (type)) 19146 { 19147 int flags = LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR; 19148 tree addr; 19149 tree call; 19150 19151 if (TREE_CODE (type) == ARRAY_TYPE) 19152 addr = decl; 19153 else 19154 addr = build_address (decl); 19155 19156 call = build_delete (input_location, TREE_TYPE (addr), addr, 19157 sfk_complete_destructor, flags, 0, complain); 19158 if (call == error_mark_node) 19159 cleanup = error_mark_node; 19160 else if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type)) 19161 /* Discard the call. */; 19162 else if (decl_maybe_constant_destruction (decl, type) 19163 && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl)) 19164 cxx_constant_dtor (call, decl); 19165 else if (cleanup) 19166 cleanup = cp_build_compound_expr (cleanup, call, complain); 19167 else 19168 cleanup = call; 19169 } 19170 19171 /* build_delete sets the location of the destructor call to the 19172 current location, even though the destructor is going to be 19173 called later, at the end of the current scope. This can lead to 19174 a "jumpy" behavior for users of debuggers when they step around 19175 the end of the block. So let's unset the location of the 19176 destructor call instead. */ 19177 protected_set_expr_location (cleanup, UNKNOWN_LOCATION); 19178 if (cleanup && CONVERT_EXPR_P (cleanup)) 19179 protected_set_expr_location (TREE_OPERAND (cleanup, 0), UNKNOWN_LOCATION); 19180 19181 if (cleanup 19182 && DECL_P (decl) 19183 && !lookup_attribute ("warn_unused", TYPE_ATTRIBUTES (TREE_TYPE (decl))) 19184 /* Treat objects with destructors as used; the destructor may do 19185 something substantive. */ 19186 && !mark_used (decl, complain) && !(complain & tf_error)) 19187 return error_mark_node; 19188 19189 if (cleanup && cfun && !processing_template_decl 19190 && !expr_noexcept_p (cleanup, tf_none)) 19191 cp_function_chain->throwing_cleanup = true; 19192 19193 return cleanup; 19194 } 19195 19196 19197 /* Return the FUNCTION_TYPE that corresponds to MEMFNTYPE, which can be a 19199 FUNCTION_DECL, METHOD_TYPE, FUNCTION_TYPE, pointer or reference to 19200 METHOD_TYPE or FUNCTION_TYPE, or pointer to member function. */ 19201 19202 tree 19203 static_fn_type (tree memfntype) 19204 { 19205 tree fntype; 19206 tree args; 19207 19208 if (TYPE_PTRMEMFUNC_P (memfntype)) 19209 memfntype = TYPE_PTRMEMFUNC_FN_TYPE (memfntype); 19210 if (INDIRECT_TYPE_P (memfntype) 19211 || TREE_CODE (memfntype) == FUNCTION_DECL) 19212 memfntype = TREE_TYPE (memfntype); 19213 if (TREE_CODE (memfntype) == FUNCTION_TYPE) 19214 return memfntype; 19215 gcc_assert (TREE_CODE (memfntype) == METHOD_TYPE); 19216 args = TYPE_ARG_TYPES (memfntype); 19217 fntype = build_function_type (TREE_TYPE (memfntype), TREE_CHAIN (args)); 19218 fntype = apply_memfn_quals (fntype, type_memfn_quals (memfntype)); 19219 fntype = (cp_build_type_attribute_variant 19220 (fntype, TYPE_ATTRIBUTES (memfntype))); 19221 fntype = cxx_copy_lang_qualifiers (fntype, memfntype); 19222 return fntype; 19223 } 19224 19225 /* DECL was originally constructed as a non-static member function, 19226 but turned out to be static. Update it accordingly. */ 19227 19228 void 19229 revert_static_member_fn (tree decl) 19230 { 19231 tree stype = static_fn_type (decl); 19232 cp_cv_quals quals = type_memfn_quals (stype); 19233 cp_ref_qualifier rqual = type_memfn_rqual (stype); 19234 19235 if (quals != TYPE_UNQUALIFIED || rqual != REF_QUAL_NONE) 19236 stype = apply_memfn_quals (stype, TYPE_UNQUALIFIED, REF_QUAL_NONE); 19237 19238 TREE_TYPE (decl) = stype; 19239 19240 if (DECL_ARGUMENTS (decl)) 19241 DECL_ARGUMENTS (decl) = DECL_CHAIN (DECL_ARGUMENTS (decl)); 19242 DECL_STATIC_FUNCTION_P (decl) = 1; 19243 } 19244 19245 /* Return which tree structure is used by T, or TS_CP_GENERIC if T is 19246 one of the language-independent trees. */ 19247 19248 enum cp_tree_node_structure_enum 19249 cp_tree_node_structure (union lang_tree_node * t) 19250 { 19251 switch (TREE_CODE (&t->generic)) 19252 { 19253 case ARGUMENT_PACK_SELECT: return TS_CP_ARGUMENT_PACK_SELECT; 19254 case BASELINK: return TS_CP_BASELINK; 19255 case CONSTRAINT_INFO: return TS_CP_CONSTRAINT_INFO; 19256 case DEFERRED_NOEXCEPT: return TS_CP_DEFERRED_NOEXCEPT; 19257 case DEFERRED_PARSE: return TS_CP_DEFERRED_PARSE; 19258 case IDENTIFIER_NODE: return TS_CP_IDENTIFIER; 19259 case LAMBDA_EXPR: return TS_CP_LAMBDA_EXPR; 19260 case BINDING_VECTOR: return TS_CP_BINDING_VECTOR; 19261 case OVERLOAD: return TS_CP_OVERLOAD; 19262 case PTRMEM_CST: return TS_CP_PTRMEM; 19263 case STATIC_ASSERT: return TS_CP_STATIC_ASSERT; 19264 case TEMPLATE_DECL: return TS_CP_TEMPLATE_DECL; 19265 case TEMPLATE_INFO: return TS_CP_TEMPLATE_INFO; 19266 case TEMPLATE_PARM_INDEX: return TS_CP_TPI; 19267 case TRAIT_EXPR: return TS_CP_TRAIT_EXPR; 19268 case USERDEF_LITERAL: return TS_CP_USERDEF_LITERAL; 19269 default: return TS_CP_GENERIC; 19270 } 19271 } 19272 19273 bool 19274 cp_missing_noreturn_ok_p (tree decl) 19275 { 19276 /* A missing noreturn is ok for the `main' function. */ 19277 return DECL_MAIN_P (decl); 19278 } 19279 19280 /* Return the decl used to identify the COMDAT group into which DECL should 19281 be placed. */ 19282 19283 tree 19284 cxx_comdat_group (tree decl) 19285 { 19286 /* Virtual tables, construction virtual tables, and virtual table 19287 tables all go in a single COMDAT group, named after the primary 19288 virtual table. */ 19289 if (VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl)) 19290 decl = CLASSTYPE_VTABLES (DECL_CONTEXT (decl)); 19291 /* For all other DECLs, the COMDAT group is the mangled name of the 19292 declaration itself. */ 19293 else 19294 { 19295 while (DECL_THUNK_P (decl)) 19296 { 19297 /* If TARGET_USE_LOCAL_THUNK_ALIAS_P, use_thunk puts the thunk 19298 into the same section as the target function. In that case 19299 we must return target's name. */ 19300 tree target = THUNK_TARGET (decl); 19301 if (TARGET_USE_LOCAL_THUNK_ALIAS_P (target) 19302 && DECL_SECTION_NAME (target) != NULL 19303 && DECL_ONE_ONLY (target)) 19304 decl = target; 19305 else 19306 break; 19307 } 19308 /* If a ctor/dtor has already set the comdat group by 19309 maybe_clone_body, don't override it. */ 19310 if (SUPPORTS_ONE_ONLY 19311 && TREE_CODE (decl) == FUNCTION_DECL 19312 && DECL_CLONED_FUNCTION_P (decl)) 19313 if (tree comdat = DECL_COMDAT_GROUP (decl)) 19314 return comdat; 19315 } 19316 19317 return decl; 19318 } 19319 19320 /* Returns the return type for FN as written by the user, which may include 19321 a placeholder for a deduced return type. */ 19322 19323 tree 19324 fndecl_declared_return_type (tree fn) 19325 { 19326 fn = STRIP_TEMPLATE (fn); 19327 if (FNDECL_USED_AUTO (fn)) 19328 return DECL_SAVED_AUTO_RETURN_TYPE (fn); 19329 19330 return TREE_TYPE (TREE_TYPE (fn)); 19331 } 19332 19333 /* Returns true iff DECL is a variable or function declared with an auto type 19334 that has not yet been deduced to a real type. */ 19335 19336 bool 19337 undeduced_auto_decl (tree decl) 19338 { 19339 if (cxx_dialect < cxx11) 19340 return false; 19341 STRIP_ANY_LOCATION_WRAPPER (decl); 19342 return ((VAR_OR_FUNCTION_DECL_P (decl) 19343 || TREE_CODE (decl) == TEMPLATE_DECL) 19344 && type_uses_auto (TREE_TYPE (decl))); 19345 } 19346 19347 /* Complain if DECL has an undeduced return type. */ 19348 19349 bool 19350 require_deduced_type (tree decl, tsubst_flags_t complain) 19351 { 19352 if (undeduced_auto_decl (decl)) 19353 { 19354 if (TREE_CODE (decl) == FUNCTION_DECL 19355 && fndecl_built_in_p (decl, BUILT_IN_FRONTEND) 19356 && DECL_FE_FUNCTION_CODE (decl) == CP_BUILT_IN_SOURCE_LOCATION) 19357 { 19358 /* Set the return type of __builtin_source_location. */ 19359 tree type = get_source_location_impl_type (); 19360 if (type == error_mark_node) 19361 { 19362 inform (input_location, "using %qs", "__builtin_source_location"); 19363 return false; 19364 } 19365 type = cp_build_qualified_type (type, TYPE_QUAL_CONST); 19366 type = build_pointer_type (type); 19367 apply_deduced_return_type (decl, type); 19368 return true; 19369 } 19370 19371 if (warning_suppressed_p (decl) && seen_error ()) 19372 /* We probably already complained about deduction failure. */; 19373 else if (complain & tf_error) 19374 error ("use of %qD before deduction of %<auto%>", decl); 19375 note_failed_type_completion_for_satisfaction (decl); 19376 return false; 19377 } 19378 return true; 19379 } 19380 19381 /* Create a representation of the explicit-specifier with 19382 constant-expression of EXPR. COMPLAIN is as for tsubst. */ 19383 19384 tree 19385 build_explicit_specifier (tree expr, tsubst_flags_t complain) 19386 { 19387 if (check_for_bare_parameter_packs (expr)) 19388 return error_mark_node; 19389 19390 if (instantiation_dependent_expression_p (expr)) 19391 /* Wait for instantiation, tsubst_function_decl will handle it. */ 19392 return expr; 19393 19394 expr = build_converted_constant_bool_expr (expr, complain); 19395 expr = instantiate_non_dependent_expr (expr, complain); 19396 expr = cxx_constant_value (expr, complain); 19397 return expr; 19398 } 19399 19400 #include "gt-cp-decl.h" 19401