1 /* Process declarations and variables for -*- C++ -*- compiler. 2 Copyright (C) 1988-2022 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 #include "system.h" 31 #include "coretypes.h" 32 #include "target.h" 33 #include "c-family/c-target.h" 34 #include "cp-tree.h" 35 #include "timevar.h" 36 #include "stringpool.h" 37 #include "cgraph.h" 38 #include "stor-layout.h" 39 #include "varasm.h" 40 #include "attribs.h" 41 #include "flags.h" 42 #include "tree-iterator.h" 43 #include "decl.h" 44 #include "intl.h" 45 #include "toplev.h" 46 #include "c-family/c-objc.h" 47 #include "c-family/c-pragma.h" 48 #include "c-family/c-ubsan.h" 49 #include "debug.h" 50 #include "plugin.h" 51 #include "builtins.h" 52 #include "gimplify.h" 53 #include "asan.h" 54 #include "gcc-rich-location.h" 55 #include "langhooks.h" 56 #include "context.h" /* For 'g'. */ 57 #include "omp-general.h" 58 #include "omp-offload.h" /* For offload_vars. */ 59 #include "opts.h" 60 #include "langhooks-def.h" /* For lhd_simulate_record_decl */ 61 62 /* Possible cases of bad specifiers type used by bad_specifiers. */ 63 enum bad_spec_place { 64 BSP_VAR, /* variable */ 65 BSP_PARM, /* parameter */ 66 BSP_TYPE, /* type */ 67 BSP_FIELD /* field */ 68 }; 69 70 static const char *redeclaration_error_message (tree, tree); 71 72 static int decl_jump_unsafe (tree); 73 static void require_complete_types_for_parms (tree); 74 static tree grok_reference_init (tree, tree, tree, int); 75 static tree grokvardecl (tree, tree, tree, const cp_decl_specifier_seq *, 76 int, int, int, bool, int, tree, location_t); 77 static void check_static_variable_definition (tree, tree); 78 static void record_unknown_type (tree, const char *); 79 static int member_function_or_else (tree, tree, enum overload_flags); 80 static tree local_variable_p_walkfn (tree *, int *, void *); 81 static const char *tag_name (enum tag_types); 82 static tree lookup_and_check_tag (enum tag_types, tree, TAG_how, bool); 83 static void maybe_deduce_size_from_array_init (tree, tree); 84 static void layout_var_decl (tree); 85 static tree check_initializer (tree, tree, int, vec<tree, va_gc> **); 86 static void make_rtl_for_nonlocal_decl (tree, tree, const char *); 87 static void copy_type_enum (tree , tree); 88 static void check_function_type (tree, tree); 89 static void finish_constructor_body (void); 90 static void begin_destructor_body (void); 91 static void finish_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 (void); 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 109 /* The following symbols are subsumed in the cp_global_trees array, and 110 listed here individually for documentation purposes. 111 112 C++ extensions 113 tree wchar_decl_node; 114 115 tree vtable_entry_type; 116 tree delta_type_node; 117 tree __t_desc_type_node; 118 119 tree class_type_node; 120 tree unknown_type_node; 121 122 Array type `vtable_entry_type[]' 123 124 tree vtbl_type_node; 125 tree vtbl_ptr_type_node; 126 127 Namespaces, 128 129 tree std_node; 130 tree abi_node; 131 132 A FUNCTION_DECL which can call `abort'. Not necessarily the 133 one that the user will declare, but sufficient to be called 134 by routines that want to abort the program. 135 136 tree abort_fndecl; 137 138 Used by RTTI 139 tree type_info_type_node, tinfo_decl_id, tinfo_decl_type; 140 tree tinfo_var_id; */ 141 142 tree cp_global_trees[CPTI_MAX]; 143 144 /* A list of objects which have constructors or destructors 145 which reside in namespace scope. The decl is stored in 146 the TREE_VALUE slot and the initializer is stored 147 in the TREE_PURPOSE slot. */ 148 tree static_aggregates; 149 150 /* Like static_aggregates, but for thread_local variables. */ 151 tree tls_aggregates; 152 153 /* A hash-map mapping from variable decls to the dynamic initializer for 154 the decl. This is currently only used by OpenMP. */ 155 decl_tree_map *dynamic_initializers; 156 157 /* -- end of C++ */ 158 159 /* A node for the integer constant 2. */ 160 161 tree integer_two_node; 162 163 /* vector of static decls. */ 164 vec<tree, va_gc> *static_decls; 165 166 /* vector of keyed classes. */ 167 vec<tree, va_gc> *keyed_classes; 168 169 /* Used only for jumps to as-yet undefined labels, since jumps to 170 defined labels can have their validity checked immediately. */ 171 172 struct GTY((chain_next ("%h.next"))) named_label_use_entry { 173 struct named_label_use_entry *next; 174 /* The binding level to which this entry is *currently* attached. 175 This is initially the binding level in which the goto appeared, 176 but is modified as scopes are closed. */ 177 cp_binding_level *binding_level; 178 /* The head of the names list that was current when the goto appeared, 179 or the inner scope popped. These are the decls that will *not* be 180 skipped when jumping to the label. */ 181 tree names_in_scope; 182 /* The location of the goto, for error reporting. */ 183 location_t o_goto_locus; 184 /* True if an OpenMP structured block scope has been closed since 185 the goto appeared. This means that the branch from the label will 186 illegally exit an OpenMP scope. */ 187 bool in_omp_scope; 188 }; 189 190 /* A list of all LABEL_DECLs in the function that have names. Here so 191 we can clear out their names' definitions at the end of the 192 function, and so we can check the validity of jumps to these labels. */ 193 194 struct GTY((for_user)) named_label_entry { 195 196 tree name; /* Name of decl. */ 197 198 tree label_decl; /* LABEL_DECL, unless deleted local label. */ 199 200 named_label_entry *outer; /* Outer shadowed chain. */ 201 202 /* The binding level to which the label is *currently* attached. 203 This is initially set to the binding level in which the label 204 is defined, but is modified as scopes are closed. */ 205 cp_binding_level *binding_level; 206 207 /* The head of the names list that was current when the label was 208 defined, or the inner scope popped. These are the decls that will 209 be skipped when jumping to the label. */ 210 tree names_in_scope; 211 212 /* A vector of all decls from all binding levels that would be 213 crossed by a backward branch to the label. */ 214 vec<tree, va_gc> *bad_decls; 215 216 /* A list of uses of the label, before the label is defined. */ 217 named_label_use_entry *uses; 218 219 /* The following bits are set after the label is defined, and are 220 updated as scopes are popped. They indicate that a jump to the 221 label will illegally enter a scope of the given flavor. */ 222 bool in_try_scope; 223 bool in_catch_scope; 224 bool in_omp_scope; 225 bool in_transaction_scope; 226 bool in_constexpr_if; 227 bool in_consteval_if; 228 }; 229 230 #define named_labels cp_function_chain->x_named_labels 231 232 /* The number of function bodies which we are currently processing. 234 (Zero if we are at namespace scope, one inside the body of a 235 function, two inside the body of a function in a local class, etc.) */ 236 int function_depth; 237 238 /* Whether the exception-specifier is part of a function type (i.e. C++17). */ 239 bool flag_noexcept_type; 240 241 /* States indicating how grokdeclarator() should handle declspecs marked 242 with __attribute__((deprecated)). An object declared as 243 __attribute__((deprecated)) suppresses warnings of uses of other 244 deprecated items. */ 245 enum deprecated_states deprecated_state = DEPRECATED_NORMAL; 246 247 248 /* A list of VAR_DECLs whose type was incomplete at the time the 250 variable was declared. */ 251 252 struct GTY(()) incomplete_var { 253 tree decl; 254 tree incomplete_type; 255 }; 256 257 258 static GTY(()) vec<incomplete_var, va_gc> *incomplete_vars; 259 260 /* Returns the kind of template specialization we are currently 262 processing, given that it's declaration contained N_CLASS_SCOPES 263 explicit scope qualifications. */ 264 265 tmpl_spec_kind 266 current_tmpl_spec_kind (int n_class_scopes) 267 { 268 int n_template_parm_scopes = 0; 269 int seen_specialization_p = 0; 270 int innermost_specialization_p = 0; 271 cp_binding_level *b; 272 273 /* Scan through the template parameter scopes. */ 274 for (b = current_binding_level; 275 b->kind == sk_template_parms; 276 b = b->level_chain) 277 { 278 /* If we see a specialization scope inside a parameter scope, 279 then something is wrong. That corresponds to a declaration 280 like: 281 282 template <class T> template <> ... 283 284 which is always invalid since [temp.expl.spec] forbids the 285 specialization of a class member template if the enclosing 286 class templates are not explicitly specialized as well. */ 287 if (b->explicit_spec_p) 288 { 289 if (n_template_parm_scopes == 0) 290 innermost_specialization_p = 1; 291 else 292 seen_specialization_p = 1; 293 } 294 else if (seen_specialization_p == 1) 295 return tsk_invalid_member_spec; 296 297 ++n_template_parm_scopes; 298 } 299 300 /* Handle explicit instantiations. */ 301 if (processing_explicit_instantiation) 302 { 303 if (n_template_parm_scopes != 0) 304 /* We've seen a template parameter list during an explicit 305 instantiation. For example: 306 307 template <class T> template void f(int); 308 309 This is erroneous. */ 310 return tsk_invalid_expl_inst; 311 else 312 return tsk_expl_inst; 313 } 314 315 if (n_template_parm_scopes < n_class_scopes) 316 /* We've not seen enough template headers to match all the 317 specialized classes present. For example: 318 319 template <class T> void R<T>::S<T>::f(int); 320 321 This is invalid; there needs to be one set of template 322 parameters for each class. */ 323 return tsk_insufficient_parms; 324 else if (n_template_parm_scopes == n_class_scopes) 325 /* We're processing a non-template declaration (even though it may 326 be a member of a template class.) For example: 327 328 template <class T> void S<T>::f(int); 329 330 The `class T' matches the `S<T>', leaving no template headers 331 corresponding to the `f'. */ 332 return tsk_none; 333 else if (n_template_parm_scopes > n_class_scopes + 1) 334 /* We've got too many template headers. For example: 335 336 template <> template <class T> void f (T); 337 338 There need to be more enclosing classes. */ 339 return tsk_excessive_parms; 340 else 341 /* This must be a template. It's of the form: 342 343 template <class T> template <class U> void S<T>::f(U); 344 345 This is a specialization if the innermost level was a 346 specialization; otherwise it's just a definition of the 347 template. */ 348 return innermost_specialization_p ? tsk_expl_spec : tsk_template; 349 } 350 351 /* Exit the current scope. */ 352 353 void 354 finish_scope (void) 355 { 356 poplevel (0, 0, 0); 357 } 358 359 /* When a label goes out of scope, check to see if that label was used 360 in a valid manner, and issue any appropriate warnings or errors. */ 361 362 static void 363 check_label_used (tree label) 364 { 365 if (!processing_template_decl) 366 { 367 if (DECL_INITIAL (label) == NULL_TREE) 368 { 369 location_t location; 370 371 error ("label %q+D used but not defined", label); 372 location = input_location; 373 /* FIXME want (LOCATION_FILE (input_location), (line)0) */ 374 /* Avoid crashing later. */ 375 define_label (location, DECL_NAME (label)); 376 } 377 else 378 warn_for_unused_label (label); 379 } 380 } 381 382 /* Helper function to sort named label entries in a vector by DECL_UID. */ 383 384 static int 385 sort_labels (const void *a, const void *b) 386 { 387 tree label1 = *(tree const *) a; 388 tree label2 = *(tree const *) b; 389 390 /* DECL_UIDs can never be equal. */ 391 return DECL_UID (label1) > DECL_UID (label2) ? -1 : +1; 392 } 393 394 /* At the end of a function, all labels declared within the function 395 go out of scope. BLOCK is the top-level block for the 396 function. */ 397 398 static void 399 pop_labels (tree block) 400 { 401 if (!named_labels) 402 return; 403 404 /* We need to add the labels to the block chain, so debug 405 information is emitted. But, we want the order to be stable so 406 need to sort them first. Otherwise the debug output could be 407 randomly ordered. I guess it's mostly stable, unless the hash 408 table implementation changes. */ 409 auto_vec<tree, 32> labels (named_labels->elements ()); 410 hash_table<named_label_hash>::iterator end (named_labels->end ()); 411 for (hash_table<named_label_hash>::iterator iter 412 (named_labels->begin ()); iter != end; ++iter) 413 { 414 named_label_entry *ent = *iter; 415 416 gcc_checking_assert (!ent->outer); 417 if (ent->label_decl) 418 labels.quick_push (ent->label_decl); 419 ggc_free (ent); 420 } 421 named_labels = NULL; 422 labels.qsort (sort_labels); 423 424 while (labels.length ()) 425 { 426 tree label = labels.pop (); 427 428 DECL_CHAIN (label) = BLOCK_VARS (block); 429 BLOCK_VARS (block) = label; 430 431 check_label_used (label); 432 } 433 } 434 435 /* At the end of a block with local labels, restore the outer definition. */ 436 437 static void 438 pop_local_label (tree id, tree label) 439 { 440 check_label_used (label); 441 named_label_entry **slot = named_labels->find_slot_with_hash 442 (id, IDENTIFIER_HASH_VALUE (id), NO_INSERT); 443 named_label_entry *ent = *slot; 444 445 if (ent->outer) 446 ent = ent->outer; 447 else 448 { 449 ent = ggc_cleared_alloc<named_label_entry> (); 450 ent->name = id; 451 } 452 *slot = ent; 453 } 454 455 /* The following two routines are used to interface to Objective-C++. 456 The binding level is purposely treated as an opaque type. */ 457 458 void * 459 objc_get_current_scope (void) 460 { 461 return current_binding_level; 462 } 463 464 /* The following routine is used by the NeXT-style SJLJ exceptions; 465 variables get marked 'volatile' so as to not be clobbered by 466 _setjmp()/_longjmp() calls. All variables in the current scope, 467 as well as parent scopes up to (but not including) ENCLOSING_BLK 468 shall be thusly marked. */ 469 470 void 471 objc_mark_locals_volatile (void *enclosing_blk) 472 { 473 cp_binding_level *scope; 474 475 for (scope = current_binding_level; 476 scope && scope != enclosing_blk; 477 scope = scope->level_chain) 478 { 479 tree decl; 480 481 for (decl = scope->names; decl; decl = TREE_CHAIN (decl)) 482 objc_volatilize_decl (decl); 483 484 /* Do not climb up past the current function. */ 485 if (scope->kind == sk_function_parms) 486 break; 487 } 488 } 489 490 /* True if B is the level for the condition of a constexpr if. */ 491 492 static bool 493 level_for_constexpr_if (cp_binding_level *b) 494 { 495 return (b->kind == sk_cond && b->this_entity 496 && TREE_CODE (b->this_entity) == IF_STMT 497 && IF_STMT_CONSTEXPR_P (b->this_entity)); 498 } 499 500 /* True if B is the level for the condition of a consteval if. */ 501 502 static bool 503 level_for_consteval_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_CONSTEVAL_P (b->this_entity)); 508 } 509 510 /* Update data for defined and undefined labels when leaving a scope. */ 511 512 int 513 poplevel_named_label_1 (named_label_entry **slot, cp_binding_level *bl) 514 { 515 named_label_entry *ent = *slot; 516 cp_binding_level *obl = bl->level_chain; 517 518 if (ent->binding_level == bl) 519 { 520 tree decl; 521 522 /* ENT->NAMES_IN_SCOPE may contain a mixture of DECLs and 523 TREE_LISTs representing OVERLOADs, so be careful. */ 524 for (decl = ent->names_in_scope; decl; decl = (DECL_P (decl) 525 ? DECL_CHAIN (decl) 526 : TREE_CHAIN (decl))) 527 if (decl_jump_unsafe (decl)) 528 vec_safe_push (ent->bad_decls, decl); 529 530 ent->binding_level = obl; 531 ent->names_in_scope = obl->names; 532 switch (bl->kind) 533 { 534 case sk_try: 535 ent->in_try_scope = true; 536 break; 537 case sk_catch: 538 ent->in_catch_scope = true; 539 break; 540 case sk_omp: 541 ent->in_omp_scope = true; 542 break; 543 case sk_transaction: 544 ent->in_transaction_scope = true; 545 break; 546 case sk_block: 547 if (level_for_constexpr_if (bl->level_chain)) 548 ent->in_constexpr_if = true; 549 else if (level_for_consteval_if (bl->level_chain)) 550 ent->in_consteval_if = true; 551 break; 552 default: 553 break; 554 } 555 } 556 else if (ent->uses) 557 { 558 struct named_label_use_entry *use; 559 560 for (use = ent->uses; use ; use = use->next) 561 if (use->binding_level == bl) 562 { 563 use->binding_level = obl; 564 use->names_in_scope = obl->names; 565 if (bl->kind == sk_omp) 566 use->in_omp_scope = true; 567 } 568 } 569 570 return 1; 571 } 572 573 /* Saved errorcount to avoid -Wunused-but-set-{parameter,variable} warnings 574 when errors were reported, except for -Werror-unused-but-set-*. */ 575 static int unused_but_set_errorcount; 576 577 /* Exit a binding level. 578 Pop the level off, and restore the state of the identifier-decl mappings 579 that were in effect when this level was entered. 580 581 If KEEP == 1, this level had explicit declarations, so 582 and create a "block" (a BLOCK node) for the level 583 to record its declarations and subblocks for symbol table output. 584 585 If FUNCTIONBODY is nonzero, this level is the body of a function, 586 so create a block as if KEEP were set and also clear out all 587 label names. 588 589 If REVERSE is nonzero, reverse the order of decls before putting 590 them into the BLOCK. */ 591 592 tree 593 poplevel (int keep, int reverse, int functionbody) 594 { 595 tree link; 596 /* The chain of decls was accumulated in reverse order. 597 Put it into forward order, just for cleanliness. */ 598 tree decls; 599 tree subblocks; 600 tree block; 601 tree decl; 602 scope_kind kind; 603 604 auto_cond_timevar tv (TV_NAME_LOOKUP); 605 restart: 606 607 block = NULL_TREE; 608 609 gcc_assert (current_binding_level->kind != sk_class 610 && current_binding_level->kind != sk_namespace); 611 612 if (current_binding_level->kind == sk_cleanup) 613 functionbody = 0; 614 subblocks = functionbody >= 0 ? current_binding_level->blocks : 0; 615 616 gcc_assert (!vec_safe_length (current_binding_level->class_shadowed)); 617 618 /* We used to use KEEP == 2 to indicate that the new block should go 619 at the beginning of the list of blocks at this binding level, 620 rather than the end. This hack is no longer used. */ 621 gcc_assert (keep == 0 || keep == 1); 622 623 if (current_binding_level->keep) 624 keep = 1; 625 626 /* Any uses of undefined labels, and any defined labels, now operate 627 under constraints of next binding contour. */ 628 if (cfun && !functionbody && named_labels) 629 named_labels->traverse<cp_binding_level *, poplevel_named_label_1> 630 (current_binding_level); 631 632 /* Get the decls in the order they were written. 633 Usually current_binding_level->names is in reverse order. 634 But parameter decls were previously put in forward order. */ 635 636 decls = current_binding_level->names; 637 if (reverse) 638 { 639 decls = nreverse (decls); 640 current_binding_level->names = decls; 641 } 642 643 /* If there were any declarations or structure tags in that level, 644 or if this level is a function body, 645 create a BLOCK to record them for the life of this function. */ 646 block = NULL_TREE; 647 /* Avoid function body block if possible. */ 648 if (functionbody && subblocks && BLOCK_CHAIN (subblocks) == NULL_TREE) 649 keep = 0; 650 else if (keep == 1 || functionbody) 651 block = make_node (BLOCK); 652 if (block != NULL_TREE) 653 { 654 BLOCK_VARS (block) = decls; 655 BLOCK_SUBBLOCKS (block) = subblocks; 656 } 657 658 /* In each subblock, record that this is its superior. */ 659 if (keep >= 0) 660 for (link = subblocks; link; link = BLOCK_CHAIN (link)) 661 BLOCK_SUPERCONTEXT (link) = block; 662 663 /* Before we remove the declarations first check for unused variables. */ 664 if ((warn_unused_variable || warn_unused_but_set_variable) 665 && current_binding_level->kind != sk_template_parms 666 && !processing_template_decl) 667 for (tree d = get_local_decls (); d; d = TREE_CHAIN (d)) 668 { 669 /* There are cases where D itself is a TREE_LIST. See in 670 push_local_binding where the list of decls returned by 671 getdecls is built. */ 672 decl = TREE_CODE (d) == TREE_LIST ? TREE_VALUE (d) : d; 673 674 tree type = TREE_TYPE (decl); 675 if (VAR_P (decl) 676 && (! TREE_USED (decl) || !DECL_READ_P (decl)) 677 && ! DECL_IN_SYSTEM_HEADER (decl) 678 /* For structured bindings, consider only real variables, not 679 subobjects. */ 680 && (DECL_DECOMPOSITION_P (decl) ? !DECL_DECOMP_BASE (decl) 681 : (DECL_NAME (decl) && !DECL_ARTIFICIAL (decl))) 682 && type != error_mark_node 683 && (!CLASS_TYPE_P (type) 684 || !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type) 685 || lookup_attribute ("warn_unused", 686 TYPE_ATTRIBUTES (TREE_TYPE (decl))))) 687 { 688 if (! TREE_USED (decl)) 689 { 690 if (!DECL_NAME (decl) && DECL_DECOMPOSITION_P (decl)) 691 warning_at (DECL_SOURCE_LOCATION (decl), 692 OPT_Wunused_variable, 693 "unused structured binding declaration"); 694 else 695 warning_at (DECL_SOURCE_LOCATION (decl), 696 OPT_Wunused_variable, "unused variable %qD", decl); 697 suppress_warning (decl, OPT_Wunused_variable); 698 } 699 else if (DECL_CONTEXT (decl) == current_function_decl 700 // For -Wunused-but-set-variable leave references alone. 701 && !TYPE_REF_P (TREE_TYPE (decl)) 702 && errorcount == unused_but_set_errorcount) 703 { 704 if (!DECL_NAME (decl) && DECL_DECOMPOSITION_P (decl)) 705 warning_at (DECL_SOURCE_LOCATION (decl), 706 OPT_Wunused_but_set_variable, "structured " 707 "binding declaration set but not used"); 708 else 709 warning_at (DECL_SOURCE_LOCATION (decl), 710 OPT_Wunused_but_set_variable, 711 "variable %qD set but not used", decl); 712 unused_but_set_errorcount = errorcount; 713 } 714 } 715 } 716 717 /* Remove declarations for all the DECLs in this level. */ 718 for (link = decls; link; link = TREE_CHAIN (link)) 719 { 720 tree name; 721 if (TREE_CODE (link) == TREE_LIST) 722 { 723 decl = TREE_VALUE (link); 724 name = TREE_PURPOSE (link); 725 gcc_checking_assert (name); 726 } 727 else 728 { 729 decl = link; 730 name = DECL_NAME (decl); 731 } 732 733 /* Remove the binding. */ 734 if (TREE_CODE (decl) == LABEL_DECL) 735 pop_local_label (name, decl); 736 else 737 pop_local_binding (name, decl); 738 } 739 740 /* Restore the IDENTIFIER_TYPE_VALUEs. */ 741 for (link = current_binding_level->type_shadowed; 742 link; link = TREE_CHAIN (link)) 743 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (link), TREE_VALUE (link)); 744 745 /* There may be OVERLOADs (wrapped in TREE_LISTs) on the BLOCK_VARs 746 list if a `using' declaration put them there. The debugging 747 back ends won't understand OVERLOAD, so we remove them here. 748 Because the BLOCK_VARS are (temporarily) shared with 749 CURRENT_BINDING_LEVEL->NAMES we must do this fixup after we have 750 popped all the bindings. Also remove undeduced 'auto' decls, 751 which LTO doesn't understand, and can't have been used by anything. */ 752 if (block) 753 { 754 tree* d; 755 756 for (d = &BLOCK_VARS (block); *d; ) 757 { 758 if (TREE_CODE (*d) == TREE_LIST 759 || (!processing_template_decl 760 && undeduced_auto_decl (*d))) 761 *d = TREE_CHAIN (*d); 762 else 763 d = &DECL_CHAIN (*d); 764 } 765 } 766 767 /* If the level being exited is the top level of a function, 768 check over all the labels. */ 769 if (functionbody) 770 { 771 if (block) 772 { 773 /* Since this is the top level block of a function, the vars are 774 the function's parameters. Don't leave them in the BLOCK 775 because they are found in the FUNCTION_DECL instead. */ 776 BLOCK_VARS (block) = 0; 777 pop_labels (block); 778 } 779 else 780 pop_labels (subblocks); 781 } 782 783 kind = current_binding_level->kind; 784 if (kind == sk_cleanup) 785 { 786 tree stmt; 787 788 /* If this is a temporary binding created for a cleanup, then we'll 789 have pushed a statement list level. Pop that, create a new 790 BIND_EXPR for the block, and insert it into the stream. */ 791 stmt = pop_stmt_list (current_binding_level->statement_list); 792 stmt = c_build_bind_expr (input_location, block, stmt); 793 add_stmt (stmt); 794 } 795 796 leave_scope (); 797 if (functionbody) 798 { 799 /* The current function is being defined, so its DECL_INITIAL 800 should be error_mark_node. */ 801 gcc_assert (DECL_INITIAL (current_function_decl) == error_mark_node); 802 DECL_INITIAL (current_function_decl) = block ? block : subblocks; 803 if (subblocks) 804 { 805 if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl)) 806 { 807 if (BLOCK_SUBBLOCKS (subblocks)) 808 BLOCK_OUTER_CURLY_BRACE_P (BLOCK_SUBBLOCKS (subblocks)) = 1; 809 } 810 else 811 BLOCK_OUTER_CURLY_BRACE_P (subblocks) = 1; 812 } 813 } 814 else if (block) 815 current_binding_level->blocks 816 = block_chainon (current_binding_level->blocks, block); 817 818 /* If we did not make a block for the level just exited, 819 any blocks made for inner levels 820 (since they cannot be recorded as subblocks in that level) 821 must be carried forward so they will later become subblocks 822 of something else. */ 823 else if (subblocks) 824 current_binding_level->blocks 825 = block_chainon (current_binding_level->blocks, subblocks); 826 827 /* Each and every BLOCK node created here in `poplevel' is important 828 (e.g. for proper debugging information) so if we created one 829 earlier, mark it as "used". */ 830 if (block) 831 TREE_USED (block) = 1; 832 833 /* All temporary bindings created for cleanups are popped silently. */ 834 if (kind == sk_cleanup) 835 goto restart; 836 837 return block; 838 } 839 840 /* Call wrapup_globals_declarations for the globals in NAMESPACE. */ 841 /* Diagnose odr-used extern inline variables without definitions 842 in the current TU. */ 843 844 int 845 wrapup_namespace_globals () 846 { 847 if (vec<tree, va_gc> *statics = static_decls) 848 { 849 for (tree decl : *statics) 850 { 851 if (warn_unused_function 852 && TREE_CODE (decl) == FUNCTION_DECL 853 && DECL_INITIAL (decl) == 0 854 && DECL_EXTERNAL (decl) 855 && !TREE_PUBLIC (decl) 856 && !DECL_ARTIFICIAL (decl) 857 && !DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (decl) 858 && !warning_suppressed_p (decl, OPT_Wunused_function)) 859 warning_at (DECL_SOURCE_LOCATION (decl), 860 OPT_Wunused_function, 861 "%qF declared %<static%> but never defined", decl); 862 863 if (VAR_P (decl) 864 && DECL_EXTERNAL (decl) 865 && DECL_INLINE_VAR_P (decl) 866 && DECL_ODR_USED (decl)) 867 error_at (DECL_SOURCE_LOCATION (decl), 868 "odr-used inline variable %qD is not defined", decl); 869 } 870 871 /* Clear out the list, so we don't rescan next time. */ 872 static_decls = NULL; 873 874 /* Write out any globals that need to be output. */ 875 return wrapup_global_declarations (statics->address (), 876 statics->length ()); 877 } 878 return 0; 879 } 880 881 /* In C++, you don't have to write `struct S' to refer to `S'; you 883 can just use `S'. We accomplish this by creating a TYPE_DECL as 884 if the user had written `typedef struct S S'. Create and return 885 the TYPE_DECL for TYPE. */ 886 887 tree 888 create_implicit_typedef (tree name, tree type) 889 { 890 tree decl; 891 892 decl = build_decl (input_location, TYPE_DECL, name, type); 893 DECL_ARTIFICIAL (decl) = 1; 894 /* There are other implicit type declarations, like the one *within* 895 a class that allows you to write `S::S'. We must distinguish 896 amongst these. */ 897 SET_DECL_IMPLICIT_TYPEDEF_P (decl); 898 TYPE_NAME (type) = decl; 899 TYPE_STUB_DECL (type) = decl; 900 901 return decl; 902 } 903 904 /* Function-scope local entities that need discriminators. Each entry 905 is a {decl,name} pair. VAR_DECLs for anon unions get their name 906 smashed, so we cannot rely on DECL_NAME. */ 907 908 static GTY((deletable)) vec<tree, va_gc> *local_entities; 909 910 /* Determine the mangling discriminator of local DECL. There are 911 generally very few of these in any particular function. */ 912 913 void 914 determine_local_discriminator (tree decl) 915 { 916 auto_cond_timevar tv (TV_NAME_LOOKUP); 917 retrofit_lang_decl (decl); 918 tree ctx = DECL_CONTEXT (decl); 919 tree name = (TREE_CODE (decl) == TYPE_DECL 920 && TYPE_UNNAMED_P (TREE_TYPE (decl)) 921 ? NULL_TREE : DECL_NAME (decl)); 922 size_t nelts = vec_safe_length (local_entities); 923 for (size_t i = 0; i < nelts; i += 2) 924 { 925 tree *pair = &(*local_entities)[i]; 926 tree d = pair[0]; 927 tree n = pair[1]; 928 gcc_checking_assert (d != decl); 929 if (name == n 930 && TREE_CODE (decl) == TREE_CODE (d) 931 && ctx == DECL_CONTEXT (d)) 932 { 933 tree disc = integer_one_node; 934 if (DECL_DISCRIMINATOR (d)) 935 disc = build_int_cst (TREE_TYPE (disc), 936 TREE_INT_CST_LOW (DECL_DISCRIMINATOR (d)) + 1); 937 DECL_DISCRIMINATOR (decl) = disc; 938 /* Replace the saved decl. */ 939 pair[0] = decl; 940 decl = NULL_TREE; 941 break; 942 } 943 } 944 945 if (decl) 946 { 947 vec_safe_reserve (local_entities, 2); 948 local_entities->quick_push (decl); 949 local_entities->quick_push (name); 950 } 951 } 952 953 954 956 /* Returns true if functions FN1 and FN2 have equivalent trailing 957 requires clauses. */ 958 959 static bool 960 function_requirements_equivalent_p (tree newfn, tree oldfn) 961 { 962 /* In the concepts TS, the combined constraints are compared. */ 963 if (cxx_dialect < cxx20) 964 { 965 tree ci1 = get_constraints (oldfn); 966 tree ci2 = get_constraints (newfn); 967 tree req1 = ci1 ? CI_ASSOCIATED_CONSTRAINTS (ci1) : NULL_TREE; 968 tree req2 = ci2 ? CI_ASSOCIATED_CONSTRAINTS (ci2) : NULL_TREE; 969 return cp_tree_equal (req1, req2); 970 } 971 972 /* Compare only trailing requirements. */ 973 tree reqs1 = get_trailing_function_requirements (newfn); 974 tree reqs2 = get_trailing_function_requirements (oldfn); 975 if ((reqs1 != NULL_TREE) != (reqs2 != NULL_TREE)) 976 return false; 977 978 /* Substitution is needed when friends are involved. */ 979 reqs1 = maybe_substitute_reqs_for (reqs1, newfn); 980 reqs2 = maybe_substitute_reqs_for (reqs2, oldfn); 981 982 return cp_tree_equal (reqs1, reqs2); 983 } 984 985 /* Subroutine of duplicate_decls: return truthvalue of whether 986 or not types of these decls match. 987 988 For C++, we must compare the parameter list so that `int' can match 989 `int&' in a parameter position, but `int&' is not confused with 990 `const int&'. */ 991 992 int 993 decls_match (tree newdecl, tree olddecl, bool record_versions /* = true */) 994 { 995 int types_match; 996 997 if (newdecl == olddecl) 998 return 1; 999 1000 if (TREE_CODE (newdecl) != TREE_CODE (olddecl)) 1001 /* If the two DECLs are not even the same kind of thing, we're not 1002 interested in their types. */ 1003 return 0; 1004 1005 gcc_assert (DECL_P (newdecl)); 1006 1007 if (TREE_CODE (newdecl) == FUNCTION_DECL) 1008 { 1009 /* Specializations of different templates are different functions 1010 even if they have the same type. */ 1011 tree t1 = (DECL_USE_TEMPLATE (newdecl) 1012 ? DECL_TI_TEMPLATE (newdecl) 1013 : NULL_TREE); 1014 tree t2 = (DECL_USE_TEMPLATE (olddecl) 1015 ? DECL_TI_TEMPLATE (olddecl) 1016 : NULL_TREE); 1017 if (t1 != t2) 1018 return 0; 1019 1020 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl) 1021 && ! (DECL_EXTERN_C_P (newdecl) 1022 && DECL_EXTERN_C_P (olddecl))) 1023 return 0; 1024 1025 /* A new declaration doesn't match a built-in one unless it 1026 is also extern "C". */ 1027 if (DECL_IS_UNDECLARED_BUILTIN (olddecl) 1028 && DECL_EXTERN_C_P (olddecl) && !DECL_EXTERN_C_P (newdecl)) 1029 return 0; 1030 1031 tree f1 = TREE_TYPE (newdecl); 1032 tree f2 = TREE_TYPE (olddecl); 1033 if (TREE_CODE (f1) != TREE_CODE (f2)) 1034 return 0; 1035 1036 /* A declaration with deduced return type should use its pre-deduction 1037 type for declaration matching. */ 1038 tree r2 = fndecl_declared_return_type (olddecl); 1039 tree r1 = fndecl_declared_return_type (newdecl); 1040 1041 tree p1 = TYPE_ARG_TYPES (f1); 1042 tree p2 = TYPE_ARG_TYPES (f2); 1043 1044 if (same_type_p (r1, r2)) 1045 { 1046 if (!prototype_p (f2) && DECL_EXTERN_C_P (olddecl) 1047 && fndecl_built_in_p (olddecl)) 1048 { 1049 types_match = self_promoting_args_p (p1); 1050 if (p1 == void_list_node) 1051 TREE_TYPE (newdecl) = TREE_TYPE (olddecl); 1052 } 1053 else 1054 types_match = 1055 compparms (p1, p2) 1056 && type_memfn_rqual (f1) == type_memfn_rqual (f2) 1057 && (TYPE_ATTRIBUTES (TREE_TYPE (newdecl)) == NULL_TREE 1058 || comp_type_attributes (TREE_TYPE (newdecl), 1059 TREE_TYPE (olddecl)) != 0); 1060 } 1061 else 1062 types_match = 0; 1063 1064 /* Two function declarations match if either has a requires-clause 1065 then both have a requires-clause and their constraints-expressions 1066 are equivalent. */ 1067 if (types_match && flag_concepts) 1068 types_match = function_requirements_equivalent_p (newdecl, olddecl); 1069 1070 /* The decls dont match if they correspond to two different versions 1071 of the same function. Disallow extern "C" functions to be 1072 versions for now. */ 1073 if (types_match 1074 && !DECL_EXTERN_C_P (newdecl) 1075 && !DECL_EXTERN_C_P (olddecl) 1076 && targetm.target_option.function_versions (newdecl, olddecl)) 1077 { 1078 if (record_versions) 1079 maybe_version_functions (newdecl, olddecl, 1080 (!DECL_FUNCTION_VERSIONED (newdecl) 1081 || !DECL_FUNCTION_VERSIONED (olddecl))); 1082 return 0; 1083 } 1084 } 1085 else if (TREE_CODE (newdecl) == TEMPLATE_DECL) 1086 { 1087 if (!template_heads_equivalent_p (newdecl, olddecl)) 1088 return 0; 1089 1090 tree oldres = DECL_TEMPLATE_RESULT (olddecl); 1091 tree newres = DECL_TEMPLATE_RESULT (newdecl); 1092 1093 if (TREE_CODE (newres) != TREE_CODE (oldres)) 1094 return 0; 1095 1096 /* Two template types match if they are the same. Otherwise, compare 1097 the underlying declarations. */ 1098 if (TREE_CODE (newres) == TYPE_DECL) 1099 types_match = same_type_p (TREE_TYPE (newres), TREE_TYPE (oldres)); 1100 else 1101 types_match = decls_match (newres, oldres); 1102 } 1103 else 1104 { 1105 /* Need to check scope for variable declaration (VAR_DECL). 1106 For typedef (TYPE_DECL), scope is ignored. */ 1107 if (VAR_P (newdecl) 1108 && CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl) 1109 /* [dcl.link] 1110 Two declarations for an object with C language linkage 1111 with the same name (ignoring the namespace that qualify 1112 it) that appear in different namespace scopes refer to 1113 the same object. */ 1114 && !(DECL_EXTERN_C_P (olddecl) && DECL_EXTERN_C_P (newdecl))) 1115 return 0; 1116 1117 if (TREE_TYPE (newdecl) == error_mark_node) 1118 types_match = TREE_TYPE (olddecl) == error_mark_node; 1119 else if (TREE_TYPE (olddecl) == NULL_TREE) 1120 types_match = TREE_TYPE (newdecl) == NULL_TREE; 1121 else if (TREE_TYPE (newdecl) == NULL_TREE) 1122 types_match = 0; 1123 else 1124 types_match = comptypes (TREE_TYPE (newdecl), 1125 TREE_TYPE (olddecl), 1126 COMPARE_REDECLARATION); 1127 } 1128 1129 return types_match; 1130 } 1131 1132 /* Mark DECL as versioned if it isn't already. */ 1133 1134 static void 1135 maybe_mark_function_versioned (tree decl) 1136 { 1137 if (!DECL_FUNCTION_VERSIONED (decl)) 1138 { 1139 DECL_FUNCTION_VERSIONED (decl) = 1; 1140 /* If DECL_ASSEMBLER_NAME has already been set, re-mangle 1141 to include the version marker. */ 1142 if (DECL_ASSEMBLER_NAME_SET_P (decl)) 1143 mangle_decl (decl); 1144 } 1145 } 1146 1147 /* NEWDECL and OLDDECL have identical signatures. If they are 1148 different versions adjust them and return true. 1149 If RECORD is set to true, record function versions. */ 1150 1151 bool 1152 maybe_version_functions (tree newdecl, tree olddecl, bool record) 1153 { 1154 if (!targetm.target_option.function_versions (newdecl, olddecl)) 1155 return false; 1156 1157 maybe_mark_function_versioned (olddecl); 1158 if (DECL_LOCAL_DECL_P (olddecl)) 1159 { 1160 olddecl = DECL_LOCAL_DECL_ALIAS (olddecl); 1161 maybe_mark_function_versioned (olddecl); 1162 } 1163 1164 maybe_mark_function_versioned (newdecl); 1165 if (DECL_LOCAL_DECL_P (newdecl)) 1166 { 1167 /* Unfortunately, we can get here before pushdecl naturally calls 1168 push_local_extern_decl_alias, so we need to call it directly. */ 1169 if (!DECL_LOCAL_DECL_ALIAS (newdecl)) 1170 push_local_extern_decl_alias (newdecl); 1171 newdecl = DECL_LOCAL_DECL_ALIAS (newdecl); 1172 maybe_mark_function_versioned (newdecl); 1173 } 1174 1175 if (record) 1176 cgraph_node::record_function_versions (olddecl, newdecl); 1177 1178 return true; 1179 } 1180 1181 /* If NEWDECL is `static' and an `extern' was seen previously, 1182 warn about it. OLDDECL is the previous declaration. 1183 1184 Note that this does not apply to the C++ case of declaring 1185 a variable `extern const' and then later `const'. 1186 1187 Don't complain about built-in functions, since they are beyond 1188 the user's control. */ 1189 1190 void 1191 warn_extern_redeclared_static (tree newdecl, tree olddecl) 1192 { 1193 if (TREE_CODE (newdecl) == TYPE_DECL 1194 || TREE_CODE (newdecl) == TEMPLATE_DECL 1195 || TREE_CODE (newdecl) == CONST_DECL 1196 || TREE_CODE (newdecl) == NAMESPACE_DECL) 1197 return; 1198 1199 /* Don't get confused by static member functions; that's a different 1200 use of `static'. */ 1201 if (TREE_CODE (newdecl) == FUNCTION_DECL 1202 && DECL_STATIC_FUNCTION_P (newdecl)) 1203 return; 1204 1205 /* If the old declaration was `static', or the new one isn't, then 1206 everything is OK. */ 1207 if (DECL_THIS_STATIC (olddecl) || !DECL_THIS_STATIC (newdecl)) 1208 return; 1209 1210 /* It's OK to declare a builtin function as `static'. */ 1211 if (TREE_CODE (olddecl) == FUNCTION_DECL 1212 && DECL_ARTIFICIAL (olddecl)) 1213 return; 1214 1215 auto_diagnostic_group d; 1216 if (permerror (DECL_SOURCE_LOCATION (newdecl), 1217 "%qD was declared %<extern%> and later %<static%>", newdecl)) 1218 inform (DECL_SOURCE_LOCATION (olddecl), 1219 "previous declaration of %qD", olddecl); 1220 } 1221 1222 /* NEW_DECL is a redeclaration of OLD_DECL; both are functions or 1223 function templates. If their exception specifications do not 1224 match, issue a diagnostic. */ 1225 1226 static void 1227 check_redeclaration_exception_specification (tree new_decl, 1228 tree old_decl) 1229 { 1230 tree new_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (new_decl)); 1231 tree old_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old_decl)); 1232 1233 /* Two default specs are equivalent, don't force evaluation. */ 1234 if (UNEVALUATED_NOEXCEPT_SPEC_P (new_exceptions) 1235 && UNEVALUATED_NOEXCEPT_SPEC_P (old_exceptions)) 1236 return; 1237 1238 if (!type_dependent_expression_p (old_decl)) 1239 { 1240 maybe_instantiate_noexcept (new_decl); 1241 maybe_instantiate_noexcept (old_decl); 1242 } 1243 new_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (new_decl)); 1244 old_exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old_decl)); 1245 1246 /* [except.spec] 1247 1248 If any declaration of a function has an exception-specification, 1249 all declarations, including the definition and an explicit 1250 specialization, of that function shall have an 1251 exception-specification with the same set of type-ids. */ 1252 if (!DECL_IS_UNDECLARED_BUILTIN (old_decl) 1253 && !DECL_IS_UNDECLARED_BUILTIN (new_decl) 1254 && !comp_except_specs (new_exceptions, old_exceptions, ce_normal)) 1255 { 1256 const char *const msg 1257 = G_("declaration of %qF has a different exception specifier"); 1258 bool complained = true; 1259 location_t new_loc = DECL_SOURCE_LOCATION (new_decl); 1260 auto_diagnostic_group d; 1261 if (DECL_IN_SYSTEM_HEADER (old_decl)) 1262 complained = pedwarn (new_loc, OPT_Wsystem_headers, msg, new_decl); 1263 else if (!flag_exceptions) 1264 /* We used to silently permit mismatched eh specs with 1265 -fno-exceptions, so make them a pedwarn now. */ 1266 complained = pedwarn (new_loc, OPT_Wpedantic, msg, new_decl); 1267 else 1268 error_at (new_loc, msg, new_decl); 1269 if (complained) 1270 inform (DECL_SOURCE_LOCATION (old_decl), 1271 "from previous declaration %qF", old_decl); 1272 } 1273 } 1274 1275 /* Return true if OLD_DECL and NEW_DECL agree on constexprness. 1276 Otherwise issue diagnostics. */ 1277 1278 static bool 1279 validate_constexpr_redeclaration (tree old_decl, tree new_decl) 1280 { 1281 old_decl = STRIP_TEMPLATE (old_decl); 1282 new_decl = STRIP_TEMPLATE (new_decl); 1283 if (!VAR_OR_FUNCTION_DECL_P (old_decl) 1284 || !VAR_OR_FUNCTION_DECL_P (new_decl)) 1285 return true; 1286 if (DECL_DECLARED_CONSTEXPR_P (old_decl) 1287 == DECL_DECLARED_CONSTEXPR_P (new_decl)) 1288 { 1289 if (TREE_CODE (old_decl) != FUNCTION_DECL) 1290 return true; 1291 if (DECL_IMMEDIATE_FUNCTION_P (old_decl) 1292 == DECL_IMMEDIATE_FUNCTION_P (new_decl)) 1293 return true; 1294 } 1295 if (TREE_CODE (old_decl) == FUNCTION_DECL) 1296 { 1297 /* With -fimplicit-constexpr, ignore changes in the constexpr 1298 keyword. */ 1299 if (flag_implicit_constexpr 1300 && (DECL_IMMEDIATE_FUNCTION_P (new_decl) 1301 == DECL_IMMEDIATE_FUNCTION_P (old_decl))) 1302 return true; 1303 if (fndecl_built_in_p (old_decl)) 1304 { 1305 /* Hide a built-in declaration. */ 1306 DECL_DECLARED_CONSTEXPR_P (old_decl) 1307 = DECL_DECLARED_CONSTEXPR_P (new_decl); 1308 if (DECL_IMMEDIATE_FUNCTION_P (new_decl)) 1309 SET_DECL_IMMEDIATE_FUNCTION_P (old_decl); 1310 return true; 1311 } 1312 /* 7.1.5 [dcl.constexpr] 1313 Note: An explicit specialization can differ from the template 1314 declaration with respect to the constexpr specifier. */ 1315 if (! DECL_TEMPLATE_SPECIALIZATION (old_decl) 1316 && DECL_TEMPLATE_SPECIALIZATION (new_decl)) 1317 return true; 1318 1319 const char *kind = "constexpr"; 1320 if (DECL_IMMEDIATE_FUNCTION_P (old_decl) 1321 || DECL_IMMEDIATE_FUNCTION_P (new_decl)) 1322 kind = "consteval"; 1323 error_at (DECL_SOURCE_LOCATION (new_decl), 1324 "redeclaration %qD differs in %qs " 1325 "from previous declaration", new_decl, 1326 kind); 1327 inform (DECL_SOURCE_LOCATION (old_decl), 1328 "previous declaration %qD", old_decl); 1329 return false; 1330 } 1331 return true; 1332 } 1333 1334 // If OLDDECL and NEWDECL are concept declarations with the same type 1335 // (i.e., and template parameters), but different requirements, 1336 // emit diagnostics and return true. Otherwise, return false. 1337 static inline bool 1338 check_concept_refinement (tree olddecl, tree newdecl) 1339 { 1340 if (!DECL_DECLARED_CONCEPT_P (olddecl) || !DECL_DECLARED_CONCEPT_P (newdecl)) 1341 return false; 1342 1343 tree d1 = DECL_TEMPLATE_RESULT (olddecl); 1344 tree d2 = DECL_TEMPLATE_RESULT (newdecl); 1345 if (TREE_CODE (d1) != TREE_CODE (d2)) 1346 return false; 1347 1348 tree t1 = TREE_TYPE (d1); 1349 tree t2 = TREE_TYPE (d2); 1350 if (TREE_CODE (d1) == FUNCTION_DECL) 1351 { 1352 if (compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2)) 1353 && comp_template_parms (DECL_TEMPLATE_PARMS (olddecl), 1354 DECL_TEMPLATE_PARMS (newdecl)) 1355 && !equivalently_constrained (olddecl, newdecl)) 1356 { 1357 error ("cannot specialize concept %q#D", olddecl); 1358 return true; 1359 } 1360 } 1361 return false; 1362 } 1363 1364 /* DECL is a redeclaration of a function or function template. If 1365 it does have default arguments issue a diagnostic. Note: this 1366 function is used to enforce the requirements in C++11 8.3.6 about 1367 no default arguments in redeclarations. */ 1368 1369 static void 1370 check_redeclaration_no_default_args (tree decl) 1371 { 1372 gcc_assert (DECL_DECLARES_FUNCTION_P (decl)); 1373 1374 for (tree t = FUNCTION_FIRST_USER_PARMTYPE (decl); 1375 t && t != void_list_node; t = TREE_CHAIN (t)) 1376 if (TREE_PURPOSE (t)) 1377 { 1378 permerror (DECL_SOURCE_LOCATION (decl), 1379 "redeclaration of %q#D may not have default " 1380 "arguments", decl); 1381 return; 1382 } 1383 } 1384 1385 /* NEWDECL is a redeclaration of a function or function template OLDDECL, 1386 in any case represented as FUNCTION_DECLs (the DECL_TEMPLATE_RESULTs of 1387 the TEMPLATE_DECLs in case of function templates). This function is used 1388 to enforce the final part of C++17 11.3.6/4, about a single declaration: 1389 "If a friend declaration specifies a default argument expression, that 1390 declaration shall be a definition and shall be the only declaration of 1391 the function or function template in the translation unit." */ 1392 1393 static void 1394 check_no_redeclaration_friend_default_args (tree olddecl, tree newdecl) 1395 { 1396 if (!DECL_UNIQUE_FRIEND_P (olddecl) && !DECL_UNIQUE_FRIEND_P (newdecl)) 1397 return; 1398 1399 for (tree t1 = FUNCTION_FIRST_USER_PARMTYPE (olddecl), 1400 t2 = FUNCTION_FIRST_USER_PARMTYPE (newdecl); 1401 t1 && t1 != void_list_node; 1402 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2)) 1403 if ((DECL_UNIQUE_FRIEND_P (olddecl) && TREE_PURPOSE (t1)) 1404 || (DECL_UNIQUE_FRIEND_P (newdecl) && TREE_PURPOSE (t2))) 1405 { 1406 auto_diagnostic_group d; 1407 if (permerror (DECL_SOURCE_LOCATION (newdecl), 1408 "friend declaration of %q#D specifies default " 1409 "arguments and isn%'t the only declaration", newdecl)) 1410 inform (DECL_SOURCE_LOCATION (olddecl), 1411 "previous declaration of %q#D", olddecl); 1412 return; 1413 } 1414 } 1415 1416 /* Merge tree bits that correspond to attributes noreturn, nothrow, 1417 const, malloc, and pure from NEWDECL with those of OLDDECL. */ 1418 1419 static void 1420 merge_attribute_bits (tree newdecl, tree olddecl) 1421 { 1422 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl); 1423 TREE_THIS_VOLATILE (olddecl) |= TREE_THIS_VOLATILE (newdecl); 1424 TREE_NOTHROW (newdecl) |= TREE_NOTHROW (olddecl); 1425 TREE_NOTHROW (olddecl) |= TREE_NOTHROW (newdecl); 1426 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl); 1427 TREE_READONLY (olddecl) |= TREE_READONLY (newdecl); 1428 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl); 1429 DECL_IS_MALLOC (olddecl) |= DECL_IS_MALLOC (newdecl); 1430 DECL_PURE_P (newdecl) |= DECL_PURE_P (olddecl); 1431 DECL_PURE_P (olddecl) |= DECL_PURE_P (newdecl); 1432 DECL_UNINLINABLE (newdecl) |= DECL_UNINLINABLE (olddecl); 1433 DECL_UNINLINABLE (olddecl) |= DECL_UNINLINABLE (newdecl); 1434 } 1435 1436 #define GNU_INLINE_P(fn) (DECL_DECLARED_INLINE_P (fn) \ 1437 && lookup_attribute ("gnu_inline", \ 1438 DECL_ATTRIBUTES (fn))) 1439 1440 /* A subroutine of duplicate_decls. Emits a diagnostic when newdecl 1441 ambiguates olddecl. Returns true if an error occurs. */ 1442 1443 static bool 1444 duplicate_function_template_decls (tree newdecl, tree olddecl) 1445 { 1446 1447 tree newres = DECL_TEMPLATE_RESULT (newdecl); 1448 tree oldres = DECL_TEMPLATE_RESULT (olddecl); 1449 /* Function template declarations can be differentiated by parameter 1450 and return type. */ 1451 if (compparms (TYPE_ARG_TYPES (TREE_TYPE (oldres)), 1452 TYPE_ARG_TYPES (TREE_TYPE (newres))) 1453 && same_type_p (TREE_TYPE (TREE_TYPE (newdecl)), 1454 TREE_TYPE (TREE_TYPE (olddecl)))) 1455 { 1456 /* ... and also by their template-heads and requires-clauses. */ 1457 if (template_heads_equivalent_p (newdecl, olddecl) 1458 && function_requirements_equivalent_p (newres, oldres)) 1459 { 1460 error ("ambiguating new declaration %q+#D", newdecl); 1461 inform (DECL_SOURCE_LOCATION (olddecl), 1462 "old declaration %q#D", olddecl); 1463 return true; 1464 } 1465 1466 /* FIXME: The types are the same but the are differences 1467 in either the template heads or function requirements. 1468 We should be able to diagnose a set of common errors 1469 stemming from these declarations. For example: 1470 1471 template<typename T> requires C void f(...); 1472 template<typename T> void f(...) requires C; 1473 1474 These are functionally equivalent but not equivalent. */ 1475 } 1476 1477 return false; 1478 } 1479 1480 /* OLD_PARMS is the innermost set of template parameters for some template 1481 declaration, and NEW_PARMS is the corresponding set of template parameters 1482 for a redeclaration of that template. Merge the default arguments within 1483 these two sets of parameters. CLASS_P is true iff the template in 1484 question is a class template. */ 1485 1486 bool 1487 merge_default_template_args (tree new_parms, tree old_parms, bool class_p) 1488 { 1489 gcc_checking_assert (TREE_VEC_LENGTH (new_parms) 1490 == TREE_VEC_LENGTH (old_parms)); 1491 for (int i = 0; i < TREE_VEC_LENGTH (new_parms); i++) 1492 { 1493 tree new_parm = TREE_VALUE (TREE_VEC_ELT (new_parms, i)); 1494 tree old_parm = TREE_VALUE (TREE_VEC_ELT (old_parms, i)); 1495 tree& new_default = TREE_PURPOSE (TREE_VEC_ELT (new_parms, i)); 1496 tree& old_default = TREE_PURPOSE (TREE_VEC_ELT (old_parms, i)); 1497 if (error_operand_p (new_parm) || error_operand_p (old_parm)) 1498 return false; 1499 if (new_default != NULL_TREE && old_default != NULL_TREE) 1500 { 1501 auto_diagnostic_group d; 1502 error ("redefinition of default argument for %q+#D", new_parm); 1503 inform (DECL_SOURCE_LOCATION (old_parm), 1504 "original definition appeared here"); 1505 return false; 1506 } 1507 else if (new_default != NULL_TREE) 1508 /* Update the previous template parameters (which are the ones 1509 that will really count) with the new default value. */ 1510 old_default = new_default; 1511 else if (class_p && old_default != NULL_TREE) 1512 /* Update the new parameters, too; they'll be used as the 1513 parameters for any members. */ 1514 new_default = old_default; 1515 } 1516 return true; 1517 } 1518 1519 /* If NEWDECL is a redeclaration of OLDDECL, merge the declarations. 1520 If the redeclaration is invalid, a diagnostic is issued, and the 1521 error_mark_node is returned. Otherwise, OLDDECL is returned. 1522 1523 If NEWDECL is not a redeclaration of OLDDECL, NULL_TREE is 1524 returned. 1525 1526 HIDING is true if the new decl is being hidden. WAS_HIDDEN is true 1527 if the old decl was hidden. 1528 1529 Hidden decls can be anticipated builtins, injected friends, or 1530 (coming soon) injected from a local-extern decl. */ 1531 1532 tree 1533 duplicate_decls (tree newdecl, tree olddecl, bool hiding, bool was_hidden) 1534 { 1535 unsigned olddecl_uid = DECL_UID (olddecl); 1536 int types_match = 0; 1537 int new_defines_function = 0; 1538 tree new_template_info; 1539 location_t olddecl_loc = DECL_SOURCE_LOCATION (olddecl); 1540 location_t newdecl_loc = DECL_SOURCE_LOCATION (newdecl); 1541 1542 if (newdecl == olddecl) 1543 return olddecl; 1544 1545 types_match = decls_match (newdecl, olddecl); 1546 1547 /* If either the type of the new decl or the type of the old decl is an 1548 error_mark_node, then that implies that we have already issued an 1549 error (earlier) for some bogus type specification, and in that case, 1550 it is rather pointless to harass the user with yet more error message 1551 about the same declaration, so just pretend the types match here. */ 1552 if (TREE_TYPE (newdecl) == error_mark_node 1553 || TREE_TYPE (olddecl) == error_mark_node) 1554 return error_mark_node; 1555 1556 /* Check for redeclaration and other discrepancies. */ 1557 if (TREE_CODE (olddecl) == FUNCTION_DECL 1558 && DECL_IS_UNDECLARED_BUILTIN (olddecl)) 1559 { 1560 if (TREE_CODE (newdecl) != FUNCTION_DECL) 1561 { 1562 /* Avoid warnings redeclaring built-ins which have not been 1563 explicitly declared. */ 1564 if (was_hidden) 1565 { 1566 if (TREE_PUBLIC (newdecl) 1567 && CP_DECL_CONTEXT (newdecl) == global_namespace) 1568 warning_at (newdecl_loc, 1569 OPT_Wbuiltin_declaration_mismatch, 1570 "built-in function %qD declared as non-function", 1571 newdecl); 1572 return NULL_TREE; 1573 } 1574 1575 /* If you declare a built-in or predefined function name as static, 1576 the old definition is overridden, but optionally warn this was a 1577 bad choice of name. */ 1578 if (! TREE_PUBLIC (newdecl)) 1579 { 1580 warning_at (newdecl_loc, 1581 OPT_Wshadow, 1582 fndecl_built_in_p (olddecl) 1583 ? G_("shadowing built-in function %q#D") 1584 : G_("shadowing library function %q#D"), olddecl); 1585 /* Discard the old built-in function. */ 1586 return NULL_TREE; 1587 } 1588 /* If the built-in is not ansi, then programs can override 1589 it even globally without an error. */ 1590 else if (! fndecl_built_in_p (olddecl)) 1591 warning_at (newdecl_loc, 0, 1592 "library function %q#D redeclared as non-function %q#D", 1593 olddecl, newdecl); 1594 else 1595 error_at (newdecl_loc, 1596 "declaration of %q#D conflicts with built-in " 1597 "declaration %q#D", newdecl, olddecl); 1598 return NULL_TREE; 1599 } 1600 else if (!types_match) 1601 { 1602 /* Avoid warnings redeclaring built-ins which have not been 1603 explicitly declared. */ 1604 if (was_hidden) 1605 { 1606 tree t1, t2; 1607 1608 /* A new declaration doesn't match a built-in one unless it 1609 is also extern "C". */ 1610 gcc_assert (DECL_IS_UNDECLARED_BUILTIN (olddecl)); 1611 gcc_assert (DECL_EXTERN_C_P (olddecl)); 1612 if (!DECL_EXTERN_C_P (newdecl)) 1613 return NULL_TREE; 1614 1615 for (t1 = TYPE_ARG_TYPES (TREE_TYPE (newdecl)), 1616 t2 = TYPE_ARG_TYPES (TREE_TYPE (olddecl)); 1617 t1 || t2; 1618 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2)) 1619 { 1620 if (!t1 || !t2) 1621 break; 1622 /* FILE, tm types are not known at the time 1623 we create the builtins. */ 1624 for (unsigned i = 0; 1625 i < sizeof (builtin_structptr_types) 1626 / sizeof (builtin_structptr_type); 1627 ++i) 1628 if (TREE_VALUE (t2) == builtin_structptr_types[i].node) 1629 { 1630 tree t = TREE_VALUE (t1); 1631 1632 if (TYPE_PTR_P (t) 1633 && TYPE_IDENTIFIER (TREE_TYPE (t)) 1634 == get_identifier (builtin_structptr_types[i].str) 1635 && compparms (TREE_CHAIN (t1), TREE_CHAIN (t2))) 1636 { 1637 tree oldargs = TYPE_ARG_TYPES (TREE_TYPE (olddecl)); 1638 1639 TYPE_ARG_TYPES (TREE_TYPE (olddecl)) 1640 = TYPE_ARG_TYPES (TREE_TYPE (newdecl)); 1641 types_match = decls_match (newdecl, olddecl); 1642 if (types_match) 1643 return duplicate_decls (newdecl, olddecl, 1644 hiding, was_hidden); 1645 TYPE_ARG_TYPES (TREE_TYPE (olddecl)) = oldargs; 1646 } 1647 goto next_arg; 1648 } 1649 1650 if (! same_type_p (TREE_VALUE (t1), TREE_VALUE (t2))) 1651 break; 1652 next_arg:; 1653 } 1654 1655 warning_at (newdecl_loc, 1656 OPT_Wbuiltin_declaration_mismatch, 1657 "declaration of %q#D conflicts with built-in " 1658 "declaration %q#D", newdecl, olddecl); 1659 } 1660 else if ((DECL_EXTERN_C_P (newdecl) 1661 && DECL_EXTERN_C_P (olddecl)) 1662 || compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)), 1663 TYPE_ARG_TYPES (TREE_TYPE (olddecl)))) 1664 { 1665 /* Don't really override olddecl for __* prefixed builtins 1666 except for __[^b]*_chk, the compiler might be using those 1667 explicitly. */ 1668 if (fndecl_built_in_p (olddecl)) 1669 { 1670 tree id = DECL_NAME (olddecl); 1671 const char *name = IDENTIFIER_POINTER (id); 1672 size_t len; 1673 1674 if (name[0] == '_' 1675 && name[1] == '_' 1676 && (startswith (name + 2, "builtin_") 1677 || (len = strlen (name)) <= strlen ("___chk") 1678 || memcmp (name + len - strlen ("_chk"), 1679 "_chk", strlen ("_chk") + 1) != 0)) 1680 { 1681 if (DECL_INITIAL (newdecl)) 1682 { 1683 error_at (newdecl_loc, 1684 "definition of %q#D ambiguates built-in " 1685 "declaration %q#D", newdecl, olddecl); 1686 return error_mark_node; 1687 } 1688 auto_diagnostic_group d; 1689 if (permerror (newdecl_loc, 1690 "new declaration %q#D ambiguates built-in" 1691 " declaration %q#D", newdecl, olddecl) 1692 && flag_permissive) 1693 inform (newdecl_loc, 1694 "ignoring the %q#D declaration", newdecl); 1695 return flag_permissive ? olddecl : error_mark_node; 1696 } 1697 } 1698 1699 /* A near match; override the builtin. */ 1700 1701 if (TREE_PUBLIC (newdecl)) 1702 warning_at (newdecl_loc, 1703 OPT_Wbuiltin_declaration_mismatch, 1704 "new declaration %q#D ambiguates built-in " 1705 "declaration %q#D", newdecl, olddecl); 1706 else 1707 warning (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 } 1712 else 1713 /* Discard the old built-in function. */ 1714 return NULL_TREE; 1715 1716 /* Replace the old RTL to avoid problems with inlining. */ 1717 COPY_DECL_RTL (newdecl, olddecl); 1718 } 1719 else 1720 { 1721 /* Even if the types match, prefer the new declarations type 1722 for built-ins which have not been explicitly declared, 1723 for exception lists, etc... */ 1724 tree type = TREE_TYPE (newdecl); 1725 tree attribs = (*targetm.merge_type_attributes) 1726 (TREE_TYPE (olddecl), type); 1727 1728 type = cp_build_type_attribute_variant (type, attribs); 1729 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = type; 1730 } 1731 1732 /* If a function is explicitly declared "throw ()", propagate that to 1733 the corresponding builtin. */ 1734 if (DECL_BUILT_IN_CLASS (olddecl) == BUILT_IN_NORMAL 1735 && was_hidden 1736 && TREE_NOTHROW (newdecl) 1737 && !TREE_NOTHROW (olddecl)) 1738 { 1739 enum built_in_function fncode = DECL_FUNCTION_CODE (olddecl); 1740 tree tmpdecl = builtin_decl_explicit (fncode); 1741 if (tmpdecl && tmpdecl != olddecl && types_match) 1742 TREE_NOTHROW (tmpdecl) = 1; 1743 } 1744 1745 /* Whether or not the builtin can throw exceptions has no 1746 bearing on this declarator. */ 1747 TREE_NOTHROW (olddecl) = 0; 1748 1749 if (DECL_THIS_STATIC (newdecl) && !DECL_THIS_STATIC (olddecl)) 1750 { 1751 /* If a builtin function is redeclared as `static', merge 1752 the declarations, but make the original one static. */ 1753 DECL_THIS_STATIC (olddecl) = 1; 1754 TREE_PUBLIC (olddecl) = 0; 1755 1756 /* Make the old declaration consistent with the new one so 1757 that all remnants of the builtin-ness of this function 1758 will be banished. */ 1759 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl)); 1760 COPY_DECL_RTL (newdecl, olddecl); 1761 } 1762 } 1763 else if (TREE_CODE (olddecl) != TREE_CODE (newdecl)) 1764 { 1765 /* C++ Standard, 3.3, clause 4: 1766 "[Note: a namespace name or a class template name must be unique 1767 in its declarative region (7.3.2, clause 14). ]" */ 1768 if (TREE_CODE (olddecl) == NAMESPACE_DECL 1769 || TREE_CODE (newdecl) == NAMESPACE_DECL) 1770 /* Namespace conflicts with not namespace. */; 1771 else if (DECL_TYPE_TEMPLATE_P (olddecl) 1772 || DECL_TYPE_TEMPLATE_P (newdecl)) 1773 /* Class template conflicts. */; 1774 else if ((TREE_CODE (olddecl) == TEMPLATE_DECL 1775 && DECL_TEMPLATE_RESULT (olddecl) 1776 && TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == VAR_DECL) 1777 || (TREE_CODE (newdecl) == TEMPLATE_DECL 1778 && DECL_TEMPLATE_RESULT (newdecl) 1779 && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == VAR_DECL)) 1780 /* Variable template conflicts. */; 1781 else if (concept_definition_p (olddecl) 1782 || concept_definition_p (newdecl)) 1783 /* Concept conflicts. */; 1784 else if ((TREE_CODE (newdecl) == FUNCTION_DECL 1785 && DECL_FUNCTION_TEMPLATE_P (olddecl)) 1786 || (TREE_CODE (olddecl) == FUNCTION_DECL 1787 && DECL_FUNCTION_TEMPLATE_P (newdecl))) 1788 { 1789 /* One is a function and the other is a template 1790 function. */ 1791 if (!UDLIT_OPER_P (DECL_NAME (newdecl))) 1792 return NULL_TREE; 1793 1794 /* There can only be one! */ 1795 if (TREE_CODE (newdecl) == TEMPLATE_DECL 1796 && check_raw_literal_operator (olddecl)) 1797 error_at (newdecl_loc, 1798 "literal operator %q#D conflicts with" 1799 " raw literal operator", newdecl); 1800 else if (check_raw_literal_operator (newdecl)) 1801 error_at (newdecl_loc, 1802 "raw literal operator %q#D conflicts with" 1803 " literal operator template", newdecl); 1804 else 1805 return NULL_TREE; 1806 1807 inform (olddecl_loc, "previous declaration %q#D", olddecl); 1808 return error_mark_node; 1809 } 1810 else if ((VAR_P (olddecl) && DECL_DECOMPOSITION_P (olddecl)) 1811 || (VAR_P (newdecl) && DECL_DECOMPOSITION_P (newdecl))) 1812 /* A structured binding must be unique in its declarative region. */; 1813 else if (DECL_IMPLICIT_TYPEDEF_P (olddecl) 1814 || DECL_IMPLICIT_TYPEDEF_P (newdecl)) 1815 /* One is an implicit typedef, that's ok. */ 1816 return NULL_TREE; 1817 1818 error ("%q#D redeclared as different kind of entity", newdecl); 1819 inform (olddecl_loc, "previous declaration %q#D", olddecl); 1820 1821 return error_mark_node; 1822 } 1823 else if (!types_match) 1824 { 1825 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)) 1826 /* These are certainly not duplicate declarations; they're 1827 from different scopes. */ 1828 return NULL_TREE; 1829 1830 if (TREE_CODE (newdecl) == TEMPLATE_DECL) 1831 { 1832 tree oldres = DECL_TEMPLATE_RESULT (olddecl); 1833 tree newres = DECL_TEMPLATE_RESULT (newdecl); 1834 1835 /* The name of a class template may not be declared to refer to 1836 any other template, class, function, object, namespace, value, 1837 or type in the same scope. */ 1838 if (TREE_CODE (oldres) == TYPE_DECL 1839 || TREE_CODE (newres) == TYPE_DECL) 1840 { 1841 error_at (newdecl_loc, 1842 "conflicting declaration of template %q#D", newdecl); 1843 inform (olddecl_loc, 1844 "previous declaration %q#D", olddecl); 1845 return error_mark_node; 1846 } 1847 1848 else if (TREE_CODE (oldres) == FUNCTION_DECL 1849 && TREE_CODE (newres) == FUNCTION_DECL) 1850 { 1851 if (duplicate_function_template_decls (newdecl, olddecl)) 1852 return error_mark_node; 1853 return NULL_TREE; 1854 } 1855 else if (check_concept_refinement (olddecl, newdecl)) 1856 return error_mark_node; 1857 return NULL_TREE; 1858 } 1859 if (TREE_CODE (newdecl) == FUNCTION_DECL) 1860 { 1861 if (DECL_EXTERN_C_P (newdecl) && DECL_EXTERN_C_P (olddecl)) 1862 { 1863 error_at (newdecl_loc, 1864 "conflicting declaration of C function %q#D", 1865 newdecl); 1866 inform (olddecl_loc, 1867 "previous declaration %q#D", olddecl); 1868 return error_mark_node; 1869 } 1870 /* For function versions, params and types match, but they 1871 are not ambiguous. */ 1872 else if ((!DECL_FUNCTION_VERSIONED (newdecl) 1873 && !DECL_FUNCTION_VERSIONED (olddecl)) 1874 // The functions have the same parameter types. 1875 && compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)), 1876 TYPE_ARG_TYPES (TREE_TYPE (olddecl))) 1877 // And the same constraints. 1878 && equivalently_constrained (newdecl, olddecl)) 1879 { 1880 error_at (newdecl_loc, 1881 "ambiguating new declaration of %q#D", newdecl); 1882 inform (olddecl_loc, 1883 "old declaration %q#D", olddecl); 1884 return error_mark_node; 1885 } 1886 else 1887 return NULL_TREE; 1888 } 1889 else 1890 { 1891 error_at (newdecl_loc, "conflicting declaration %q#D", newdecl); 1892 inform (olddecl_loc, 1893 "previous declaration as %q#D", olddecl); 1894 return error_mark_node; 1895 } 1896 } 1897 else if (TREE_CODE (newdecl) == FUNCTION_DECL 1898 && DECL_OMP_DECLARE_REDUCTION_P (newdecl)) 1899 { 1900 /* OMP UDRs are never duplicates. */ 1901 gcc_assert (DECL_OMP_DECLARE_REDUCTION_P (olddecl)); 1902 error_at (newdecl_loc, 1903 "redeclaration of %<pragma omp declare reduction%>"); 1904 inform (olddecl_loc, 1905 "previous %<pragma omp declare reduction%> declaration"); 1906 return error_mark_node; 1907 } 1908 else if (TREE_CODE (newdecl) == FUNCTION_DECL 1909 && ((DECL_TEMPLATE_SPECIALIZATION (olddecl) 1910 && (!DECL_TEMPLATE_INFO (newdecl) 1911 || (DECL_TI_TEMPLATE (newdecl) 1912 != DECL_TI_TEMPLATE (olddecl)))) 1913 || (DECL_TEMPLATE_SPECIALIZATION (newdecl) 1914 && (!DECL_TEMPLATE_INFO (olddecl) 1915 || (DECL_TI_TEMPLATE (olddecl) 1916 != DECL_TI_TEMPLATE (newdecl)))))) 1917 /* It's OK to have a template specialization and a non-template 1918 with the same type, or to have specializations of two 1919 different templates with the same type. Note that if one is a 1920 specialization, and the other is an instantiation of the same 1921 template, that we do not exit at this point. That situation 1922 can occur if we instantiate a template class, and then 1923 specialize one of its methods. This situation is valid, but 1924 the declarations must be merged in the usual way. */ 1925 return NULL_TREE; 1926 else if (TREE_CODE (newdecl) == FUNCTION_DECL 1927 && ((DECL_TEMPLATE_INSTANTIATION (olddecl) 1928 && !DECL_USE_TEMPLATE (newdecl)) 1929 || (DECL_TEMPLATE_INSTANTIATION (newdecl) 1930 && !DECL_USE_TEMPLATE (olddecl)))) 1931 /* One of the declarations is a template instantiation, and the 1932 other is not a template at all. That's OK. */ 1933 return NULL_TREE; 1934 else if (TREE_CODE (newdecl) == NAMESPACE_DECL) 1935 { 1936 /* In [namespace.alias] we have: 1937 1938 In a declarative region, a namespace-alias-definition can be 1939 used to redefine a namespace-alias declared in that declarative 1940 region to refer only to the namespace to which it already 1941 refers. 1942 1943 Therefore, if we encounter a second alias directive for the same 1944 alias, we can just ignore the second directive. */ 1945 if (DECL_NAMESPACE_ALIAS (newdecl) 1946 && (DECL_NAMESPACE_ALIAS (newdecl) 1947 == DECL_NAMESPACE_ALIAS (olddecl))) 1948 return olddecl; 1949 1950 /* Leave it to update_binding to merge or report error. */ 1951 return NULL_TREE; 1952 } 1953 else 1954 { 1955 const char *errmsg = redeclaration_error_message (newdecl, olddecl); 1956 if (errmsg) 1957 { 1958 auto_diagnostic_group d; 1959 error_at (newdecl_loc, errmsg, newdecl); 1960 if (DECL_NAME (olddecl) != NULL_TREE) 1961 inform (olddecl_loc, 1962 (DECL_INITIAL (olddecl) && namespace_bindings_p ()) 1963 ? G_("%q#D previously defined here") 1964 : G_("%q#D previously declared here"), olddecl); 1965 return error_mark_node; 1966 } 1967 else if (TREE_CODE (olddecl) == FUNCTION_DECL 1968 && DECL_INITIAL (olddecl) != NULL_TREE 1969 && !prototype_p (TREE_TYPE (olddecl)) 1970 && prototype_p (TREE_TYPE (newdecl))) 1971 { 1972 /* Prototype decl follows defn w/o prototype. */ 1973 auto_diagnostic_group d; 1974 if (warning_at (newdecl_loc, 0, 1975 "prototype specified for %q#D", newdecl)) 1976 inform (olddecl_loc, 1977 "previous non-prototype definition here"); 1978 } 1979 else if (VAR_OR_FUNCTION_DECL_P (olddecl) 1980 && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl)) 1981 { 1982 /* [dcl.link] 1983 If two declarations of the same function or object 1984 specify different linkage-specifications ..., the program 1985 is ill-formed.... Except for functions with C++ linkage, 1986 a function declaration without a linkage specification 1987 shall not precede the first linkage specification for 1988 that function. A function can be declared without a 1989 linkage specification after an explicit linkage 1990 specification has been seen; the linkage explicitly 1991 specified in the earlier declaration is not affected by 1992 such a function declaration. 1993 1994 DR 563 raises the question why the restrictions on 1995 functions should not also apply to objects. Older 1996 versions of G++ silently ignore the linkage-specification 1997 for this example: 1998 1999 namespace N { 2000 extern int i; 2001 extern "C" int i; 2002 } 2003 2004 which is clearly wrong. Therefore, we now treat objects 2005 like functions. */ 2006 if (current_lang_depth () == 0) 2007 { 2008 /* There is no explicit linkage-specification, so we use 2009 the linkage from the previous declaration. */ 2010 retrofit_lang_decl (newdecl); 2011 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl)); 2012 } 2013 else 2014 { 2015 auto_diagnostic_group d; 2016 error_at (newdecl_loc, 2017 "conflicting declaration of %q#D with %qL linkage", 2018 newdecl, DECL_LANGUAGE (newdecl)); 2019 inform (olddecl_loc, 2020 "previous declaration with %qL linkage", 2021 DECL_LANGUAGE (olddecl)); 2022 } 2023 } 2024 2025 if (DECL_LANG_SPECIFIC (olddecl) && DECL_USE_TEMPLATE (olddecl)) 2026 ; 2027 else if (TREE_CODE (olddecl) == FUNCTION_DECL) 2028 { 2029 /* Note: free functions, as TEMPLATE_DECLs, are handled below. */ 2030 if (DECL_FUNCTION_MEMBER_P (olddecl) 2031 && (/* grokfndecl passes member function templates too 2032 as FUNCTION_DECLs. */ 2033 DECL_TEMPLATE_INFO (olddecl) 2034 /* C++11 8.3.6/6. 2035 Default arguments for a member function of a class 2036 template shall be specified on the initial declaration 2037 of the member function within the class template. */ 2038 || CLASSTYPE_TEMPLATE_INFO (CP_DECL_CONTEXT (olddecl)))) 2039 { 2040 check_redeclaration_no_default_args (newdecl); 2041 2042 if (DECL_TEMPLATE_INFO (olddecl) 2043 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (olddecl))) 2044 { 2045 tree new_parms = DECL_TEMPLATE_INFO (newdecl) 2046 ? DECL_INNERMOST_TEMPLATE_PARMS (DECL_TI_TEMPLATE (newdecl)) 2047 : INNERMOST_TEMPLATE_PARMS (current_template_parms); 2048 tree old_parms 2049 = DECL_INNERMOST_TEMPLATE_PARMS (DECL_TI_TEMPLATE (olddecl)); 2050 merge_default_template_args (new_parms, old_parms, 2051 /*class_p=*/false); 2052 } 2053 } 2054 else 2055 { 2056 tree t1 = FUNCTION_FIRST_USER_PARMTYPE (olddecl); 2057 tree t2 = FUNCTION_FIRST_USER_PARMTYPE (newdecl); 2058 int i = 1; 2059 2060 for (; t1 && t1 != void_list_node; 2061 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2), i++) 2062 if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2)) 2063 { 2064 if (simple_cst_equal (TREE_PURPOSE (t1), 2065 TREE_PURPOSE (t2)) == 1) 2066 { 2067 auto_diagnostic_group d; 2068 if (permerror (newdecl_loc, 2069 "default argument given for parameter " 2070 "%d of %q#D", i, newdecl)) 2071 inform (olddecl_loc, 2072 "previous specification in %q#D here", 2073 olddecl); 2074 } 2075 else 2076 { 2077 auto_diagnostic_group d; 2078 error_at (newdecl_loc, 2079 "default argument given for parameter %d " 2080 "of %q#D", i, newdecl); 2081 inform (olddecl_loc, 2082 "previous specification in %q#D here", 2083 olddecl); 2084 } 2085 } 2086 2087 /* C++17 11.3.6/4: "If a friend declaration specifies a default 2088 argument expression, that declaration... shall be the only 2089 declaration of the function or function template in the 2090 translation unit." */ 2091 check_no_redeclaration_friend_default_args (olddecl, newdecl); 2092 } 2093 } 2094 } 2095 2096 /* Do not merge an implicit typedef with an explicit one. In: 2097 2098 class A; 2099 ... 2100 typedef class A A __attribute__ ((foo)); 2101 2102 the attribute should apply only to the typedef. */ 2103 if (TREE_CODE (olddecl) == TYPE_DECL 2104 && (DECL_IMPLICIT_TYPEDEF_P (olddecl) 2105 || DECL_IMPLICIT_TYPEDEF_P (newdecl))) 2106 return NULL_TREE; 2107 2108 if (DECL_TEMPLATE_PARM_P (olddecl) != DECL_TEMPLATE_PARM_P (newdecl)) 2109 return NULL_TREE; 2110 2111 if (!validate_constexpr_redeclaration (olddecl, newdecl)) 2112 return error_mark_node; 2113 2114 if (modules_p () 2115 && TREE_CODE (CP_DECL_CONTEXT (olddecl)) == NAMESPACE_DECL 2116 && TREE_CODE (olddecl) != NAMESPACE_DECL 2117 && !hiding) 2118 { 2119 if (DECL_ARTIFICIAL (olddecl)) 2120 { 2121 if (!(global_purview_p () || not_module_p ())) 2122 error ("declaration %qD conflicts with builtin", newdecl); 2123 else 2124 DECL_MODULE_EXPORT_P (olddecl) = DECL_MODULE_EXPORT_P (newdecl); 2125 } 2126 else 2127 { 2128 if (!module_may_redeclare (olddecl)) 2129 { 2130 error ("declaration %qD conflicts with import", newdecl); 2131 inform (olddecl_loc, "import declared %q#D here", olddecl); 2132 2133 return error_mark_node; 2134 } 2135 2136 if (DECL_MODULE_EXPORT_P (newdecl) 2137 && !DECL_MODULE_EXPORT_P (olddecl)) 2138 { 2139 error ("conflicting exporting declaration %qD", newdecl); 2140 inform (olddecl_loc, "previous declaration %q#D here", olddecl); 2141 } 2142 } 2143 } 2144 2145 /* We have committed to returning OLDDECL at this point. */ 2146 2147 /* If new decl is `static' and an `extern' was seen previously, 2148 warn about it. */ 2149 warn_extern_redeclared_static (newdecl, olddecl); 2150 2151 /* True to merge attributes between the declarations, false to 2152 set OLDDECL's attributes to those of NEWDECL (for template 2153 explicit specializations that specify their own attributes 2154 independent of those specified for the primary template). */ 2155 const bool merge_attr = (TREE_CODE (newdecl) != FUNCTION_DECL 2156 || !DECL_TEMPLATE_SPECIALIZATION (newdecl) 2157 || DECL_TEMPLATE_SPECIALIZATION (olddecl)); 2158 2159 if (TREE_CODE (newdecl) == FUNCTION_DECL) 2160 { 2161 if (merge_attr) 2162 { 2163 if (diagnose_mismatched_attributes (olddecl, newdecl)) 2164 inform (olddecl_loc, DECL_INITIAL (olddecl) 2165 ? G_("previous definition of %qD here") 2166 : G_("previous declaration of %qD here"), olddecl); 2167 2168 /* [dcl.attr.noreturn]: The first declaration of a function shall 2169 specify the noreturn attribute if any declaration of that function 2170 specifies the noreturn attribute. */ 2171 tree a; 2172 if (TREE_THIS_VOLATILE (newdecl) 2173 && !TREE_THIS_VOLATILE (olddecl) 2174 /* This applies to [[noreturn]] only, not its GNU variants. */ 2175 && (a = lookup_attribute ("noreturn", DECL_ATTRIBUTES (newdecl))) 2176 && cxx11_attribute_p (a) 2177 && get_attribute_namespace (a) == NULL_TREE) 2178 { 2179 error_at (newdecl_loc, "function %qD declared %<[[noreturn]]%> " 2180 "but its first declaration was not", newdecl); 2181 inform (olddecl_loc, "previous declaration of %qD", olddecl); 2182 } 2183 } 2184 2185 /* Now that functions must hold information normally held 2186 by field decls, there is extra work to do so that 2187 declaration information does not get destroyed during 2188 definition. */ 2189 if (DECL_VINDEX (olddecl)) 2190 DECL_VINDEX (newdecl) = DECL_VINDEX (olddecl); 2191 if (DECL_CONTEXT (olddecl)) 2192 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl); 2193 DECL_STATIC_CONSTRUCTOR (newdecl) |= DECL_STATIC_CONSTRUCTOR (olddecl); 2194 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl); 2195 DECL_PURE_VIRTUAL_P (newdecl) |= DECL_PURE_VIRTUAL_P (olddecl); 2196 DECL_VIRTUAL_P (newdecl) |= DECL_VIRTUAL_P (olddecl); 2197 DECL_INVALID_OVERRIDER_P (newdecl) |= DECL_INVALID_OVERRIDER_P (olddecl); 2198 DECL_FINAL_P (newdecl) |= DECL_FINAL_P (olddecl); 2199 DECL_OVERRIDE_P (newdecl) |= DECL_OVERRIDE_P (olddecl); 2200 DECL_THIS_STATIC (newdecl) |= DECL_THIS_STATIC (olddecl); 2201 DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (newdecl) 2202 |= DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (olddecl); 2203 if (DECL_OVERLOADED_OPERATOR_P (olddecl)) 2204 DECL_OVERLOADED_OPERATOR_CODE_RAW (newdecl) 2205 = DECL_OVERLOADED_OPERATOR_CODE_RAW (olddecl); 2206 new_defines_function = DECL_INITIAL (newdecl) != NULL_TREE; 2207 2208 /* Optionally warn about more than one declaration for the same 2209 name, but don't warn about a function declaration followed by a 2210 definition. */ 2211 if (warn_redundant_decls && ! DECL_ARTIFICIAL (olddecl) 2212 && !(new_defines_function && DECL_INITIAL (olddecl) == NULL_TREE) 2213 /* Don't warn about extern decl followed by definition. */ 2214 && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl)) 2215 /* Don't warn if at least one is/was hidden. */ 2216 && !(hiding || was_hidden) 2217 /* Don't warn about declaration followed by specialization. */ 2218 && (! DECL_TEMPLATE_SPECIALIZATION (newdecl) 2219 || DECL_TEMPLATE_SPECIALIZATION (olddecl))) 2220 { 2221 auto_diagnostic_group d; 2222 if (warning_at (newdecl_loc, 2223 OPT_Wredundant_decls, 2224 "redundant redeclaration of %qD in same scope", 2225 newdecl)) 2226 inform (olddecl_loc, 2227 "previous declaration of %qD", olddecl); 2228 } 2229 2230 /* [dcl.fct.def.delete] A deleted definition of a function shall be the 2231 first declaration of the function or, for an explicit specialization 2232 of a function template, the first declaration of that 2233 specialization. */ 2234 if (!(DECL_TEMPLATE_INSTANTIATION (olddecl) 2235 && DECL_TEMPLATE_SPECIALIZATION (newdecl))) 2236 { 2237 if (DECL_DELETED_FN (newdecl)) 2238 { 2239 auto_diagnostic_group d; 2240 if (pedwarn (newdecl_loc, 0, "deleted definition of %qD " 2241 "is not first declaration", newdecl)) 2242 inform (olddecl_loc, 2243 "previous declaration of %qD", olddecl); 2244 } 2245 DECL_DELETED_FN (newdecl) |= DECL_DELETED_FN (olddecl); 2246 } 2247 } 2248 2249 /* Deal with C++: must preserve virtual function table size. */ 2250 if (TREE_CODE (olddecl) == TYPE_DECL) 2251 { 2252 tree newtype = TREE_TYPE (newdecl); 2253 tree oldtype = TREE_TYPE (olddecl); 2254 2255 if (newtype != error_mark_node && oldtype != error_mark_node 2256 && TYPE_LANG_SPECIFIC (newtype) && TYPE_LANG_SPECIFIC (oldtype)) 2257 CLASSTYPE_FRIEND_CLASSES (newtype) 2258 = CLASSTYPE_FRIEND_CLASSES (oldtype); 2259 2260 DECL_ORIGINAL_TYPE (newdecl) = DECL_ORIGINAL_TYPE (olddecl); 2261 } 2262 2263 /* Copy all the DECL_... slots specified in the new decl except for 2264 any that we copy here from the old type. */ 2265 if (merge_attr) 2266 DECL_ATTRIBUTES (newdecl) 2267 = (*targetm.merge_decl_attributes) (olddecl, newdecl); 2268 else 2269 DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl); 2270 2271 if (TREE_CODE (newdecl) == TEMPLATE_DECL) 2272 { 2273 tree old_result = DECL_TEMPLATE_RESULT (olddecl); 2274 tree new_result = DECL_TEMPLATE_RESULT (newdecl); 2275 TREE_TYPE (olddecl) = TREE_TYPE (old_result); 2276 2277 /* The new decl should not already have gathered any 2278 specializations. */ 2279 gcc_assert (!DECL_TEMPLATE_SPECIALIZATIONS (newdecl)); 2280 2281 DECL_ATTRIBUTES (old_result) 2282 = (*targetm.merge_decl_attributes) (old_result, new_result); 2283 2284 if (DECL_FUNCTION_TEMPLATE_P (newdecl)) 2285 { 2286 if (DECL_SOURCE_LOCATION (newdecl) 2287 != DECL_SOURCE_LOCATION (olddecl)) 2288 { 2289 /* Per C++11 8.3.6/4, default arguments cannot be added in 2290 later declarations of a function template. */ 2291 check_redeclaration_no_default_args (newdecl); 2292 /* C++17 11.3.6/4: "If a friend declaration specifies a default 2293 argument expression, that declaration... shall be the only 2294 declaration of the function or function template in the 2295 translation unit." */ 2296 check_no_redeclaration_friend_default_args 2297 (old_result, new_result); 2298 2299 tree new_parms = DECL_INNERMOST_TEMPLATE_PARMS (newdecl); 2300 tree old_parms = DECL_INNERMOST_TEMPLATE_PARMS (olddecl); 2301 merge_default_template_args (new_parms, old_parms, 2302 /*class_p=*/false); 2303 } 2304 if (!DECL_UNIQUE_FRIEND_P (new_result)) 2305 DECL_UNIQUE_FRIEND_P (old_result) = false; 2306 2307 check_default_args (newdecl); 2308 2309 if (GNU_INLINE_P (old_result) != GNU_INLINE_P (new_result) 2310 && DECL_INITIAL (new_result)) 2311 { 2312 if (DECL_INITIAL (old_result)) 2313 DECL_UNINLINABLE (old_result) = 1; 2314 else 2315 DECL_UNINLINABLE (old_result) = DECL_UNINLINABLE (new_result); 2316 DECL_EXTERNAL (old_result) = DECL_EXTERNAL (new_result); 2317 DECL_NOT_REALLY_EXTERN (old_result) 2318 = DECL_NOT_REALLY_EXTERN (new_result); 2319 DECL_INTERFACE_KNOWN (old_result) 2320 = DECL_INTERFACE_KNOWN (new_result); 2321 DECL_DECLARED_INLINE_P (old_result) 2322 = DECL_DECLARED_INLINE_P (new_result); 2323 DECL_DISREGARD_INLINE_LIMITS (old_result) 2324 |= DECL_DISREGARD_INLINE_LIMITS (new_result); 2325 2326 } 2327 else 2328 { 2329 DECL_DECLARED_INLINE_P (old_result) 2330 |= DECL_DECLARED_INLINE_P (new_result); 2331 DECL_DISREGARD_INLINE_LIMITS (old_result) 2332 |= DECL_DISREGARD_INLINE_LIMITS (new_result); 2333 check_redeclaration_exception_specification (newdecl, olddecl); 2334 2335 merge_attribute_bits (new_result, old_result); 2336 } 2337 } 2338 2339 /* If the new declaration is a definition, update the file and 2340 line information on the declaration, and also make 2341 the old declaration the same definition. */ 2342 if (DECL_INITIAL (new_result) != NULL_TREE) 2343 { 2344 DECL_SOURCE_LOCATION (olddecl) 2345 = DECL_SOURCE_LOCATION (old_result) 2346 = DECL_SOURCE_LOCATION (newdecl); 2347 DECL_INITIAL (old_result) = DECL_INITIAL (new_result); 2348 if (DECL_FUNCTION_TEMPLATE_P (newdecl)) 2349 { 2350 tree parm; 2351 DECL_ARGUMENTS (old_result) 2352 = DECL_ARGUMENTS (new_result); 2353 for (parm = DECL_ARGUMENTS (old_result); parm; 2354 parm = DECL_CHAIN (parm)) 2355 DECL_CONTEXT (parm) = old_result; 2356 2357 if (tree fc = DECL_FRIEND_CONTEXT (new_result)) 2358 SET_DECL_FRIEND_CONTEXT (old_result, fc); 2359 } 2360 } 2361 2362 return olddecl; 2363 } 2364 2365 if (types_match) 2366 { 2367 if (TREE_CODE (newdecl) == FUNCTION_DECL) 2368 check_redeclaration_exception_specification (newdecl, olddecl); 2369 2370 /* Automatically handles default parameters. */ 2371 tree oldtype = TREE_TYPE (olddecl); 2372 tree newtype; 2373 2374 /* For typedefs use the old type, as the new type's DECL_NAME points 2375 at newdecl, which will be ggc_freed. */ 2376 if (TREE_CODE (newdecl) == TYPE_DECL) 2377 { 2378 /* But NEWTYPE might have an attribute, honor that. */ 2379 tree tem = TREE_TYPE (newdecl); 2380 newtype = oldtype; 2381 2382 if (TYPE_USER_ALIGN (tem)) 2383 { 2384 if (TYPE_ALIGN (tem) > TYPE_ALIGN (newtype)) 2385 SET_TYPE_ALIGN (newtype, TYPE_ALIGN (tem)); 2386 TYPE_USER_ALIGN (newtype) = true; 2387 } 2388 2389 /* And remove the new type from the variants list. */ 2390 if (TYPE_NAME (TREE_TYPE (newdecl)) == newdecl) 2391 { 2392 tree remove = TREE_TYPE (newdecl); 2393 if (TYPE_MAIN_VARIANT (remove) == remove) 2394 { 2395 gcc_assert (TYPE_NEXT_VARIANT (remove) == NULL_TREE); 2396 /* If remove is the main variant, no need to remove that 2397 from the list. One of the DECL_ORIGINAL_TYPE 2398 variants, e.g. created for aligned attribute, might still 2399 refer to the newdecl TYPE_DECL though, so remove that one 2400 in that case. */ 2401 if (tree orig = DECL_ORIGINAL_TYPE (newdecl)) 2402 if (orig != remove) 2403 for (tree t = TYPE_MAIN_VARIANT (orig); t; 2404 t = TYPE_MAIN_VARIANT (t)) 2405 if (TYPE_NAME (TYPE_NEXT_VARIANT (t)) == newdecl) 2406 { 2407 TYPE_NEXT_VARIANT (t) 2408 = TYPE_NEXT_VARIANT (TYPE_NEXT_VARIANT (t)); 2409 break; 2410 } 2411 } 2412 else 2413 for (tree t = TYPE_MAIN_VARIANT (remove); ; 2414 t = TYPE_NEXT_VARIANT (t)) 2415 if (TYPE_NEXT_VARIANT (t) == remove) 2416 { 2417 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (remove); 2418 break; 2419 } 2420 } 2421 } 2422 else if (merge_attr) 2423 newtype = merge_types (TREE_TYPE (newdecl), TREE_TYPE (olddecl)); 2424 else 2425 newtype = TREE_TYPE (newdecl); 2426 2427 if (VAR_P (newdecl)) 2428 { 2429 DECL_THIS_EXTERN (newdecl) |= DECL_THIS_EXTERN (olddecl); 2430 /* For already initialized vars, TREE_READONLY could have been 2431 cleared in cp_finish_decl, because the var needs runtime 2432 initialization or destruction. Make sure not to set 2433 TREE_READONLY on it again. */ 2434 if (DECL_INITIALIZED_P (olddecl) 2435 && !DECL_EXTERNAL (olddecl) 2436 && !TREE_READONLY (olddecl)) 2437 TREE_READONLY (newdecl) = 0; 2438 DECL_INITIALIZED_P (newdecl) |= DECL_INITIALIZED_P (olddecl); 2439 DECL_NONTRIVIALLY_INITIALIZED_P (newdecl) 2440 |= DECL_NONTRIVIALLY_INITIALIZED_P (olddecl); 2441 if (DECL_DEPENDENT_INIT_P (olddecl)) 2442 SET_DECL_DEPENDENT_INIT_P (newdecl, true); 2443 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (newdecl) 2444 |= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (olddecl); 2445 DECL_DECLARED_CONSTEXPR_P (newdecl) 2446 |= DECL_DECLARED_CONSTEXPR_P (olddecl); 2447 DECL_DECLARED_CONSTINIT_P (newdecl) 2448 |= DECL_DECLARED_CONSTINIT_P (olddecl); 2449 2450 /* Merge the threadprivate attribute from OLDDECL into NEWDECL. */ 2451 if (DECL_LANG_SPECIFIC (olddecl) 2452 && CP_DECL_THREADPRIVATE_P (olddecl)) 2453 { 2454 /* Allocate a LANG_SPECIFIC structure for NEWDECL, if needed. */ 2455 retrofit_lang_decl (newdecl); 2456 CP_DECL_THREADPRIVATE_P (newdecl) = 1; 2457 } 2458 } 2459 2460 /* An explicit specialization of a function template or of a member 2461 function of a class template can be declared transaction_safe 2462 independently of whether the corresponding template entity is declared 2463 transaction_safe. */ 2464 if (flag_tm && TREE_CODE (newdecl) == FUNCTION_DECL 2465 && DECL_TEMPLATE_INSTANTIATION (olddecl) 2466 && DECL_TEMPLATE_SPECIALIZATION (newdecl) 2467 && tx_safe_fn_type_p (newtype) 2468 && !tx_safe_fn_type_p (TREE_TYPE (newdecl))) 2469 newtype = tx_unsafe_fn_variant (newtype); 2470 2471 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = newtype; 2472 2473 if (TREE_CODE (newdecl) == FUNCTION_DECL) 2474 check_default_args (newdecl); 2475 2476 /* Lay the type out, unless already done. */ 2477 if (! same_type_p (newtype, oldtype) 2478 && TREE_TYPE (newdecl) != error_mark_node 2479 && !(processing_template_decl && uses_template_parms (newdecl))) 2480 layout_type (TREE_TYPE (newdecl)); 2481 2482 if ((VAR_P (newdecl) 2483 || TREE_CODE (newdecl) == PARM_DECL 2484 || TREE_CODE (newdecl) == RESULT_DECL 2485 || TREE_CODE (newdecl) == FIELD_DECL 2486 || TREE_CODE (newdecl) == TYPE_DECL) 2487 && !(processing_template_decl && uses_template_parms (newdecl))) 2488 layout_decl (newdecl, 0); 2489 2490 /* Merge deprecatedness. */ 2491 if (TREE_DEPRECATED (newdecl)) 2492 TREE_DEPRECATED (olddecl) = 1; 2493 2494 /* Merge unavailability. */ 2495 if (TREE_UNAVAILABLE (newdecl)) 2496 TREE_UNAVAILABLE (olddecl) = 1; 2497 2498 /* Preserve function specific target and optimization options */ 2499 if (TREE_CODE (newdecl) == FUNCTION_DECL) 2500 { 2501 if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl) 2502 && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl)) 2503 DECL_FUNCTION_SPECIFIC_TARGET (newdecl) 2504 = DECL_FUNCTION_SPECIFIC_TARGET (olddecl); 2505 2506 if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl) 2507 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl)) 2508 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl) 2509 = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl); 2510 2511 if (!DECL_UNIQUE_FRIEND_P (olddecl)) 2512 DECL_UNIQUE_FRIEND_P (newdecl) = false; 2513 } 2514 else 2515 { 2516 /* Merge the const type qualifier. */ 2517 if (TREE_READONLY (newdecl)) 2518 TREE_READONLY (olddecl) = 1; 2519 /* Merge the volatile type qualifier. */ 2520 if (TREE_THIS_VOLATILE (newdecl)) 2521 TREE_THIS_VOLATILE (olddecl) = 1; 2522 } 2523 2524 /* Merge the initialization information. */ 2525 if (DECL_INITIAL (newdecl) == NULL_TREE 2526 && DECL_INITIAL (olddecl) != NULL_TREE) 2527 { 2528 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl); 2529 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl); 2530 if (TREE_CODE (newdecl) == FUNCTION_DECL) 2531 { 2532 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl); 2533 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl); 2534 } 2535 } 2536 2537 if (TREE_CODE (newdecl) == FUNCTION_DECL) 2538 { 2539 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl) 2540 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl); 2541 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl); 2542 if (DECL_IS_OPERATOR_NEW_P (olddecl)) 2543 DECL_SET_IS_OPERATOR_NEW (newdecl, true); 2544 DECL_LOOPING_CONST_OR_PURE_P (newdecl) 2545 |= DECL_LOOPING_CONST_OR_PURE_P (olddecl); 2546 DECL_IS_REPLACEABLE_OPERATOR (newdecl) 2547 |= DECL_IS_REPLACEABLE_OPERATOR (olddecl); 2548 2549 if (merge_attr) 2550 merge_attribute_bits (newdecl, olddecl); 2551 else 2552 { 2553 /* Merge the noreturn bit. */ 2554 TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl); 2555 TREE_READONLY (olddecl) = TREE_READONLY (newdecl); 2556 TREE_NOTHROW (olddecl) = TREE_NOTHROW (newdecl); 2557 DECL_IS_MALLOC (olddecl) = DECL_IS_MALLOC (newdecl); 2558 DECL_PURE_P (olddecl) = DECL_PURE_P (newdecl); 2559 } 2560 /* Keep the old RTL. */ 2561 COPY_DECL_RTL (olddecl, newdecl); 2562 } 2563 else if (VAR_P (newdecl) 2564 && (DECL_SIZE (olddecl) || !DECL_SIZE (newdecl))) 2565 { 2566 /* Keep the old RTL. We cannot keep the old RTL if the old 2567 declaration was for an incomplete object and the new 2568 declaration is not since many attributes of the RTL will 2569 change. */ 2570 COPY_DECL_RTL (olddecl, newdecl); 2571 } 2572 } 2573 /* If cannot merge, then use the new type and qualifiers, 2574 and don't preserve the old rtl. */ 2575 else 2576 { 2577 /* Clean out any memory we had of the old declaration. */ 2578 tree oldstatic = value_member (olddecl, static_aggregates); 2579 if (oldstatic) 2580 TREE_VALUE (oldstatic) = error_mark_node; 2581 2582 TREE_TYPE (olddecl) = TREE_TYPE (newdecl); 2583 TREE_READONLY (olddecl) = TREE_READONLY (newdecl); 2584 TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl); 2585 TREE_NOTHROW (olddecl) = TREE_NOTHROW (newdecl); 2586 TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl); 2587 } 2588 2589 /* Merge the storage class information. */ 2590 merge_weak (newdecl, olddecl); 2591 2592 DECL_DEFER_OUTPUT (newdecl) |= DECL_DEFER_OUTPUT (olddecl); 2593 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl); 2594 TREE_STATIC (olddecl) = TREE_STATIC (newdecl) |= TREE_STATIC (olddecl); 2595 if (! DECL_EXTERNAL (olddecl)) 2596 DECL_EXTERNAL (newdecl) = 0; 2597 if (! DECL_COMDAT (olddecl)) 2598 DECL_COMDAT (newdecl) = 0; 2599 2600 if (VAR_OR_FUNCTION_DECL_P (newdecl) && DECL_LOCAL_DECL_P (newdecl)) 2601 { 2602 if (!DECL_LOCAL_DECL_P (olddecl)) 2603 /* This can happen if olddecl was brought in from the 2604 enclosing namespace via a using-decl. The new decl is 2605 then not a block-scope extern at all. */ 2606 DECL_LOCAL_DECL_P (newdecl) = false; 2607 else 2608 { 2609 retrofit_lang_decl (newdecl); 2610 tree alias = DECL_LOCAL_DECL_ALIAS (newdecl) 2611 = DECL_LOCAL_DECL_ALIAS (olddecl); 2612 DECL_ATTRIBUTES (alias) 2613 = (*targetm.merge_decl_attributes) (alias, newdecl); 2614 if (TREE_CODE (newdecl) == FUNCTION_DECL) 2615 merge_attribute_bits (newdecl, alias); 2616 } 2617 } 2618 2619 new_template_info = NULL_TREE; 2620 if (DECL_LANG_SPECIFIC (newdecl) && DECL_LANG_SPECIFIC (olddecl)) 2621 { 2622 bool new_redefines_gnu_inline = false; 2623 2624 if (new_defines_function 2625 && ((DECL_INTERFACE_KNOWN (olddecl) 2626 && TREE_CODE (olddecl) == FUNCTION_DECL) 2627 || (TREE_CODE (olddecl) == TEMPLATE_DECL 2628 && (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) 2629 == FUNCTION_DECL)))) 2630 new_redefines_gnu_inline = GNU_INLINE_P (STRIP_TEMPLATE (olddecl)); 2631 2632 if (!new_redefines_gnu_inline) 2633 { 2634 DECL_INTERFACE_KNOWN (newdecl) |= DECL_INTERFACE_KNOWN (olddecl); 2635 DECL_NOT_REALLY_EXTERN (newdecl) |= DECL_NOT_REALLY_EXTERN (olddecl); 2636 DECL_COMDAT (newdecl) |= DECL_COMDAT (olddecl); 2637 } 2638 2639 if (TREE_CODE (newdecl) != TYPE_DECL) 2640 { 2641 DECL_TEMPLATE_INSTANTIATED (newdecl) 2642 |= DECL_TEMPLATE_INSTANTIATED (olddecl); 2643 DECL_ODR_USED (newdecl) |= DECL_ODR_USED (olddecl); 2644 2645 /* If the OLDDECL is an instantiation and/or specialization, 2646 then the NEWDECL must be too. But, it may not yet be marked 2647 as such if the caller has created NEWDECL, but has not yet 2648 figured out that it is a redeclaration. */ 2649 if (!DECL_USE_TEMPLATE (newdecl)) 2650 DECL_USE_TEMPLATE (newdecl) = DECL_USE_TEMPLATE (olddecl); 2651 2652 if (!DECL_TEMPLATE_SPECIALIZATION (newdecl)) 2653 DECL_INITIALIZED_IN_CLASS_P (newdecl) 2654 |= DECL_INITIALIZED_IN_CLASS_P (olddecl); 2655 } 2656 2657 /* Don't really know how much of the language-specific 2658 values we should copy from old to new. */ 2659 DECL_IN_AGGR_P (newdecl) = DECL_IN_AGGR_P (olddecl); 2660 2661 if (LANG_DECL_HAS_MIN (newdecl)) 2662 { 2663 DECL_ACCESS (newdecl) = DECL_ACCESS (olddecl); 2664 if (DECL_TEMPLATE_INFO (newdecl)) 2665 { 2666 new_template_info = DECL_TEMPLATE_INFO (newdecl); 2667 if (DECL_TEMPLATE_INSTANTIATION (olddecl) 2668 && DECL_TEMPLATE_SPECIALIZATION (newdecl)) 2669 /* Remember the presence of explicit specialization args. */ 2670 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (olddecl)) 2671 = TINFO_USED_TEMPLATE_ID (new_template_info); 2672 } 2673 2674 /* We don't want to copy template info from a non-templated friend 2675 (PR105761), but these shouldn't have DECL_TEMPLATE_INFO now. */ 2676 gcc_checking_assert (!DECL_TEMPLATE_INFO (olddecl) 2677 || !non_templated_friend_p (olddecl)); 2678 DECL_TEMPLATE_INFO (newdecl) = DECL_TEMPLATE_INFO (olddecl); 2679 } 2680 2681 if (DECL_DECLARES_FUNCTION_P (newdecl)) 2682 { 2683 /* Only functions have these fields. */ 2684 DECL_NONCONVERTING_P (newdecl) = DECL_NONCONVERTING_P (olddecl); 2685 DECL_BEFRIENDING_CLASSES (newdecl) 2686 = chainon (DECL_BEFRIENDING_CLASSES (newdecl), 2687 DECL_BEFRIENDING_CLASSES (olddecl)); 2688 /* DECL_THUNKS is only valid for virtual functions, 2689 otherwise it is a DECL_FRIEND_CONTEXT. */ 2690 if (DECL_VIRTUAL_P (newdecl)) 2691 SET_DECL_THUNKS (newdecl, DECL_THUNKS (olddecl)); 2692 else if (tree fc = DECL_FRIEND_CONTEXT (newdecl)) 2693 SET_DECL_FRIEND_CONTEXT (olddecl, fc); 2694 } 2695 else if (VAR_P (newdecl)) 2696 { 2697 /* Only variables have this field. */ 2698 if (VAR_HAD_UNKNOWN_BOUND (olddecl)) 2699 SET_VAR_HAD_UNKNOWN_BOUND (newdecl); 2700 } 2701 } 2702 2703 if (TREE_CODE (newdecl) == FUNCTION_DECL) 2704 { 2705 tree parm; 2706 2707 /* Merge parameter attributes. */ 2708 tree oldarg, newarg; 2709 for (oldarg = DECL_ARGUMENTS(olddecl), newarg = DECL_ARGUMENTS(newdecl); 2710 oldarg && newarg; 2711 oldarg = DECL_CHAIN(oldarg), newarg = DECL_CHAIN(newarg)) 2712 { 2713 DECL_ATTRIBUTES (newarg) 2714 = (*targetm.merge_decl_attributes) (oldarg, newarg); 2715 DECL_ATTRIBUTES (oldarg) = DECL_ATTRIBUTES (newarg); 2716 } 2717 2718 if (DECL_TEMPLATE_INSTANTIATION (olddecl) 2719 && !DECL_TEMPLATE_INSTANTIATION (newdecl)) 2720 { 2721 /* If newdecl is not a specialization, then it is not a 2722 template-related function at all. And that means that we 2723 should have exited above, returning 0. */ 2724 gcc_assert (DECL_TEMPLATE_SPECIALIZATION (newdecl)); 2725 2726 if (DECL_ODR_USED (olddecl)) 2727 /* From [temp.expl.spec]: 2728 2729 If a template, a member template or the member of a class 2730 template is explicitly specialized then that 2731 specialization shall be declared before the first use of 2732 that specialization that would cause an implicit 2733 instantiation to take place, in every translation unit in 2734 which such a use occurs. */ 2735 error ("explicit specialization of %qD after first use", 2736 olddecl); 2737 2738 SET_DECL_TEMPLATE_SPECIALIZATION (olddecl); 2739 DECL_COMDAT (newdecl) = (TREE_PUBLIC (newdecl) 2740 && DECL_DECLARED_INLINE_P (newdecl)); 2741 2742 /* Don't propagate visibility from the template to the 2743 specialization here. We'll do that in determine_visibility if 2744 appropriate. */ 2745 DECL_VISIBILITY_SPECIFIED (olddecl) = 0; 2746 2747 /* [temp.expl.spec/14] We don't inline explicit specialization 2748 just because the primary template says so. */ 2749 gcc_assert (!merge_attr); 2750 2751 DECL_DECLARED_INLINE_P (olddecl) 2752 = DECL_DECLARED_INLINE_P (newdecl); 2753 2754 DECL_DISREGARD_INLINE_LIMITS (olddecl) 2755 = DECL_DISREGARD_INLINE_LIMITS (newdecl); 2756 2757 DECL_UNINLINABLE (olddecl) = DECL_UNINLINABLE (newdecl); 2758 } 2759 else if (new_defines_function && DECL_INITIAL (olddecl)) 2760 { 2761 /* Never inline re-defined extern inline functions. 2762 FIXME: this could be better handled by keeping both 2763 function as separate declarations. */ 2764 DECL_UNINLINABLE (newdecl) = 1; 2765 } 2766 else 2767 { 2768 if (DECL_PENDING_INLINE_P (olddecl)) 2769 { 2770 DECL_PENDING_INLINE_P (newdecl) = 1; 2771 DECL_PENDING_INLINE_INFO (newdecl) 2772 = DECL_PENDING_INLINE_INFO (olddecl); 2773 } 2774 else if (DECL_PENDING_INLINE_P (newdecl)) 2775 ; 2776 else if (DECL_SAVED_AUTO_RETURN_TYPE (newdecl) == NULL) 2777 DECL_SAVED_AUTO_RETURN_TYPE (newdecl) 2778 = DECL_SAVED_AUTO_RETURN_TYPE (olddecl); 2779 2780 DECL_DECLARED_INLINE_P (newdecl) |= DECL_DECLARED_INLINE_P (olddecl); 2781 2782 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl) 2783 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl)); 2784 2785 DECL_DISREGARD_INLINE_LIMITS (newdecl) 2786 = DECL_DISREGARD_INLINE_LIMITS (olddecl) 2787 = (DECL_DISREGARD_INLINE_LIMITS (newdecl) 2788 || DECL_DISREGARD_INLINE_LIMITS (olddecl)); 2789 } 2790 2791 /* Preserve abstractness on cloned [cd]tors. */ 2792 DECL_ABSTRACT_P (newdecl) = DECL_ABSTRACT_P (olddecl); 2793 2794 /* Update newdecl's parms to point at olddecl. */ 2795 for (parm = DECL_ARGUMENTS (newdecl); parm; 2796 parm = DECL_CHAIN (parm)) 2797 DECL_CONTEXT (parm) = olddecl; 2798 2799 if (! types_match) 2800 { 2801 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl)); 2802 COPY_DECL_ASSEMBLER_NAME (newdecl, olddecl); 2803 COPY_DECL_RTL (newdecl, olddecl); 2804 } 2805 if (! types_match || new_defines_function) 2806 { 2807 /* These need to be copied so that the names are available. 2808 Note that if the types do match, we'll preserve inline 2809 info and other bits, but if not, we won't. */ 2810 DECL_ARGUMENTS (olddecl) = DECL_ARGUMENTS (newdecl); 2811 DECL_RESULT (olddecl) = DECL_RESULT (newdecl); 2812 } 2813 /* If redeclaring a builtin function, it stays built in 2814 if newdecl is a gnu_inline definition, or if newdecl is just 2815 a declaration. */ 2816 if (fndecl_built_in_p (olddecl) 2817 && (new_defines_function ? GNU_INLINE_P (newdecl) : types_match)) 2818 { 2819 copy_decl_built_in_function (newdecl, olddecl); 2820 /* If we're keeping the built-in definition, keep the rtl, 2821 regardless of declaration matches. */ 2822 COPY_DECL_RTL (olddecl, newdecl); 2823 if (DECL_BUILT_IN_CLASS (newdecl) == BUILT_IN_NORMAL) 2824 { 2825 enum built_in_function fncode = DECL_FUNCTION_CODE (newdecl); 2826 if (builtin_decl_explicit_p (fncode)) 2827 { 2828 /* A compatible prototype of these builtin functions 2829 is seen, assume the runtime implements it with 2830 the expected semantics. */ 2831 switch (fncode) 2832 { 2833 case BUILT_IN_STPCPY: 2834 set_builtin_decl_implicit_p (fncode, true); 2835 break; 2836 default: 2837 set_builtin_decl_declared_p (fncode, true); 2838 break; 2839 } 2840 } 2841 2842 copy_attributes_to_builtin (newdecl); 2843 } 2844 } 2845 if (new_defines_function) 2846 /* If defining a function declared with other language 2847 linkage, use the previously declared language linkage. */ 2848 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl)); 2849 else if (types_match) 2850 { 2851 DECL_RESULT (newdecl) = DECL_RESULT (olddecl); 2852 /* Don't clear out the arguments if we're just redeclaring a 2853 function. */ 2854 if (DECL_ARGUMENTS (olddecl)) 2855 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl); 2856 } 2857 } 2858 else if (TREE_CODE (newdecl) == NAMESPACE_DECL) 2859 NAMESPACE_LEVEL (newdecl) = NAMESPACE_LEVEL (olddecl); 2860 2861 /* Now preserve various other info from the definition. */ 2862 TREE_ADDRESSABLE (newdecl) = TREE_ADDRESSABLE (olddecl); 2863 TREE_ASM_WRITTEN (newdecl) = TREE_ASM_WRITTEN (olddecl); 2864 DECL_COMMON (newdecl) = DECL_COMMON (olddecl); 2865 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl); 2866 2867 /* Warn about conflicting visibility specifications. */ 2868 if (DECL_VISIBILITY_SPECIFIED (olddecl) 2869 && DECL_VISIBILITY_SPECIFIED (newdecl) 2870 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl)) 2871 { 2872 auto_diagnostic_group d; 2873 if (warning_at (newdecl_loc, OPT_Wattributes, 2874 "%qD: visibility attribute ignored because it " 2875 "conflicts with previous declaration", newdecl)) 2876 inform (olddecl_loc, 2877 "previous declaration of %qD", olddecl); 2878 } 2879 /* Choose the declaration which specified visibility. */ 2880 if (DECL_VISIBILITY_SPECIFIED (olddecl)) 2881 { 2882 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl); 2883 DECL_VISIBILITY_SPECIFIED (newdecl) = 1; 2884 } 2885 /* Init priority used to be merged from newdecl to olddecl by the memcpy, 2886 so keep this behavior. */ 2887 if (VAR_P (newdecl) && DECL_HAS_INIT_PRIORITY_P (newdecl)) 2888 { 2889 SET_DECL_INIT_PRIORITY (olddecl, DECL_INIT_PRIORITY (newdecl)); 2890 DECL_HAS_INIT_PRIORITY_P (olddecl) = 1; 2891 } 2892 /* Likewise for DECL_ALIGN, DECL_USER_ALIGN and DECL_PACKED. */ 2893 if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl)) 2894 { 2895 SET_DECL_ALIGN (newdecl, DECL_ALIGN (olddecl)); 2896 DECL_USER_ALIGN (newdecl) |= DECL_USER_ALIGN (olddecl); 2897 } 2898 else if (DECL_ALIGN (olddecl) == DECL_ALIGN (newdecl) 2899 && DECL_USER_ALIGN (olddecl) != DECL_USER_ALIGN (newdecl)) 2900 DECL_USER_ALIGN (newdecl) = 1; 2901 2902 DECL_USER_ALIGN (olddecl) = DECL_USER_ALIGN (newdecl); 2903 if (DECL_WARN_IF_NOT_ALIGN (olddecl) 2904 > DECL_WARN_IF_NOT_ALIGN (newdecl)) 2905 SET_DECL_WARN_IF_NOT_ALIGN (newdecl, 2906 DECL_WARN_IF_NOT_ALIGN (olddecl)); 2907 if (TREE_CODE (newdecl) == FIELD_DECL) 2908 DECL_PACKED (olddecl) = DECL_PACKED (newdecl); 2909 2910 /* The DECL_LANG_SPECIFIC information in OLDDECL will be replaced 2911 with that from NEWDECL below. */ 2912 if (DECL_LANG_SPECIFIC (olddecl)) 2913 { 2914 gcc_checking_assert (DECL_LANG_SPECIFIC (olddecl) 2915 != DECL_LANG_SPECIFIC (newdecl)); 2916 ggc_free (DECL_LANG_SPECIFIC (olddecl)); 2917 } 2918 2919 /* Merge the USED information. */ 2920 if (TREE_USED (olddecl)) 2921 TREE_USED (newdecl) = 1; 2922 else if (TREE_USED (newdecl)) 2923 TREE_USED (olddecl) = 1; 2924 2925 if (VAR_P (newdecl)) 2926 { 2927 if (DECL_READ_P (olddecl)) 2928 DECL_READ_P (newdecl) = 1; 2929 else if (DECL_READ_P (newdecl)) 2930 DECL_READ_P (olddecl) = 1; 2931 } 2932 2933 if (DECL_PRESERVE_P (olddecl)) 2934 DECL_PRESERVE_P (newdecl) = 1; 2935 else if (DECL_PRESERVE_P (newdecl)) 2936 DECL_PRESERVE_P (olddecl) = 1; 2937 2938 /* Merge the DECL_FUNCTION_VERSIONED information. newdecl will be copied 2939 to olddecl and deleted. */ 2940 if (TREE_CODE (newdecl) == FUNCTION_DECL 2941 && DECL_FUNCTION_VERSIONED (olddecl)) 2942 { 2943 /* Set the flag for newdecl so that it gets copied to olddecl. */ 2944 DECL_FUNCTION_VERSIONED (newdecl) = 1; 2945 /* newdecl will be purged after copying to olddecl and is no longer 2946 a version. */ 2947 cgraph_node::delete_function_version_by_decl (newdecl); 2948 } 2949 2950 if (TREE_CODE (newdecl) == FUNCTION_DECL) 2951 { 2952 int function_size; 2953 struct symtab_node *snode = symtab_node::get (olddecl); 2954 2955 function_size = sizeof (struct tree_decl_common); 2956 2957 memcpy ((char *) olddecl + sizeof (struct tree_common), 2958 (char *) newdecl + sizeof (struct tree_common), 2959 function_size - sizeof (struct tree_common)); 2960 2961 memcpy ((char *) olddecl + sizeof (struct tree_decl_common), 2962 (char *) newdecl + sizeof (struct tree_decl_common), 2963 sizeof (struct tree_function_decl) - sizeof (struct tree_decl_common)); 2964 2965 /* Preserve symtab node mapping. */ 2966 olddecl->decl_with_vis.symtab_node = snode; 2967 2968 if (new_template_info) 2969 /* If newdecl is a template instantiation, it is possible that 2970 the following sequence of events has occurred: 2971 2972 o A friend function was declared in a class template. The 2973 class template was instantiated. 2974 2975 o The instantiation of the friend declaration was 2976 recorded on the instantiation list, and is newdecl. 2977 2978 o Later, however, instantiate_class_template called pushdecl 2979 on the newdecl to perform name injection. But, pushdecl in 2980 turn called duplicate_decls when it discovered that another 2981 declaration of a global function with the same name already 2982 existed. 2983 2984 o Here, in duplicate_decls, we decided to clobber newdecl. 2985 2986 If we're going to do that, we'd better make sure that 2987 olddecl, and not newdecl, is on the list of 2988 instantiations so that if we try to do the instantiation 2989 again we won't get the clobbered declaration. */ 2990 reregister_specialization (newdecl, 2991 new_template_info, 2992 olddecl); 2993 } 2994 else 2995 { 2996 size_t size = tree_code_size (TREE_CODE (newdecl)); 2997 2998 memcpy ((char *) olddecl + sizeof (struct tree_common), 2999 (char *) newdecl + sizeof (struct tree_common), 3000 sizeof (struct tree_decl_common) - sizeof (struct tree_common)); 3001 3002 switch (TREE_CODE (newdecl)) 3003 { 3004 case LABEL_DECL: 3005 case VAR_DECL: 3006 case RESULT_DECL: 3007 case PARM_DECL: 3008 case FIELD_DECL: 3009 case TYPE_DECL: 3010 case CONST_DECL: 3011 { 3012 struct symtab_node *snode = NULL; 3013 3014 if (VAR_P (newdecl) 3015 && (TREE_STATIC (olddecl) || TREE_PUBLIC (olddecl) 3016 || DECL_EXTERNAL (olddecl))) 3017 snode = symtab_node::get (olddecl); 3018 memcpy ((char *) olddecl + sizeof (struct tree_decl_common), 3019 (char *) newdecl + sizeof (struct tree_decl_common), 3020 size - sizeof (struct tree_decl_common) 3021 + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *)); 3022 if (VAR_P (newdecl)) 3023 olddecl->decl_with_vis.symtab_node = snode; 3024 } 3025 break; 3026 default: 3027 memcpy ((char *) olddecl + sizeof (struct tree_decl_common), 3028 (char *) newdecl + sizeof (struct tree_decl_common), 3029 sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common) 3030 + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *)); 3031 break; 3032 } 3033 } 3034 3035 if (VAR_OR_FUNCTION_DECL_P (newdecl)) 3036 { 3037 if (DECL_EXTERNAL (olddecl) 3038 || TREE_PUBLIC (olddecl) 3039 || TREE_STATIC (olddecl)) 3040 { 3041 /* Merge the section attribute. 3042 We want to issue an error if the sections conflict but that must be 3043 done later in decl_attributes since we are called before attributes 3044 are assigned. */ 3045 if (DECL_SECTION_NAME (newdecl) != NULL) 3046 set_decl_section_name (olddecl, newdecl); 3047 3048 if (DECL_ONE_ONLY (newdecl)) 3049 { 3050 struct symtab_node *oldsym, *newsym; 3051 if (TREE_CODE (olddecl) == FUNCTION_DECL) 3052 oldsym = cgraph_node::get_create (olddecl); 3053 else 3054 oldsym = varpool_node::get_create (olddecl); 3055 newsym = symtab_node::get (newdecl); 3056 oldsym->set_comdat_group (newsym->get_comdat_group ()); 3057 } 3058 } 3059 3060 if (VAR_P (newdecl) 3061 && CP_DECL_THREAD_LOCAL_P (newdecl)) 3062 { 3063 CP_DECL_THREAD_LOCAL_P (olddecl) = true; 3064 if (!processing_template_decl) 3065 set_decl_tls_model (olddecl, DECL_TLS_MODEL (newdecl)); 3066 } 3067 } 3068 3069 DECL_UID (olddecl) = olddecl_uid; 3070 3071 /* NEWDECL contains the merged attribute lists. 3072 Update OLDDECL to be the same. */ 3073 DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl); 3074 3075 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl 3076 so that encode_section_info has a chance to look at the new decl 3077 flags and attributes. */ 3078 if (DECL_RTL_SET_P (olddecl) 3079 && (TREE_CODE (olddecl) == FUNCTION_DECL 3080 || (VAR_P (olddecl) 3081 && TREE_STATIC (olddecl)))) 3082 make_decl_rtl (olddecl); 3083 3084 /* The NEWDECL will no longer be needed. Because every out-of-class 3085 declaration of a member results in a call to duplicate_decls, 3086 freeing these nodes represents in a significant savings. 3087 3088 Before releasing the node, be sore to remove function from symbol 3089 table that might have been inserted there to record comdat group. 3090 Be sure to however do not free DECL_STRUCT_FUNCTION because this 3091 structure is shared in between newdecl and oldecl. */ 3092 if (TREE_CODE (newdecl) == FUNCTION_DECL) 3093 DECL_STRUCT_FUNCTION (newdecl) = NULL; 3094 if (VAR_OR_FUNCTION_DECL_P (newdecl)) 3095 { 3096 struct symtab_node *snode = symtab_node::get (newdecl); 3097 if (snode) 3098 snode->remove (); 3099 } 3100 3101 if (TREE_CODE (olddecl) == FUNCTION_DECL) 3102 { 3103 tree clone; 3104 FOR_EACH_CLONE (clone, olddecl) 3105 { 3106 DECL_ATTRIBUTES (clone) = DECL_ATTRIBUTES (olddecl); 3107 DECL_PRESERVE_P (clone) |= DECL_PRESERVE_P (olddecl); 3108 } 3109 } 3110 3111 /* Remove the associated constraints for newdecl, if any, before 3112 reclaiming memory. */ 3113 if (flag_concepts) 3114 remove_constraints (newdecl); 3115 3116 ggc_free (newdecl); 3117 3118 return olddecl; 3119 } 3120 3121 /* Return zero if the declaration NEWDECL is valid 3123 when the declaration OLDDECL (assumed to be for the same name) 3124 has already been seen. 3125 Otherwise return an error message format string with a %s 3126 where the identifier should go. */ 3127 3128 static const char * 3129 redeclaration_error_message (tree newdecl, tree olddecl) 3130 { 3131 if (TREE_CODE (newdecl) == TYPE_DECL) 3132 { 3133 /* Because C++ can put things into name space for free, 3134 constructs like "typedef struct foo { ... } foo" 3135 would look like an erroneous redeclaration. */ 3136 if (same_type_p (TREE_TYPE (newdecl), TREE_TYPE (olddecl))) 3137 return NULL; 3138 else 3139 return G_("redefinition of %q#D"); 3140 } 3141 else if (TREE_CODE (newdecl) == FUNCTION_DECL) 3142 { 3143 /* If this is a pure function, its olddecl will actually be 3144 the original initialization to `0' (which we force to call 3145 abort()). Don't complain about redefinition in this case. */ 3146 if (DECL_LANG_SPECIFIC (olddecl) && DECL_PURE_VIRTUAL_P (olddecl) 3147 && DECL_INITIAL (olddecl) == NULL_TREE) 3148 return NULL; 3149 3150 /* If both functions come from different namespaces, this is not 3151 a redeclaration - this is a conflict with a used function. */ 3152 if (DECL_NAMESPACE_SCOPE_P (olddecl) 3153 && DECL_CONTEXT (olddecl) != DECL_CONTEXT (newdecl) 3154 && ! decls_match (olddecl, newdecl)) 3155 return G_("%qD conflicts with used function"); 3156 3157 /* We'll complain about linkage mismatches in 3158 warn_extern_redeclared_static. */ 3159 3160 /* Defining the same name twice is no good. */ 3161 if (decl_defined_p (olddecl) 3162 && decl_defined_p (newdecl)) 3163 { 3164 if (DECL_NAME (olddecl) == NULL_TREE) 3165 return G_("%q#D not declared in class"); 3166 else if (!GNU_INLINE_P (olddecl) 3167 || GNU_INLINE_P (newdecl)) 3168 return G_("redefinition of %q#D"); 3169 } 3170 3171 if (DECL_DECLARED_INLINE_P (olddecl) && DECL_DECLARED_INLINE_P (newdecl)) 3172 { 3173 bool olda = GNU_INLINE_P (olddecl); 3174 bool newa = GNU_INLINE_P (newdecl); 3175 3176 if (olda != newa) 3177 { 3178 if (newa) 3179 return G_("%q+D redeclared inline with " 3180 "%<gnu_inline%> attribute"); 3181 else 3182 return G_("%q+D redeclared inline without " 3183 "%<gnu_inline%> attribute"); 3184 } 3185 } 3186 3187 if (deduction_guide_p (olddecl) 3188 && deduction_guide_p (newdecl)) 3189 return G_("deduction guide %q+D redeclared"); 3190 3191 /* [class.compare.default]: A definition of a comparison operator as 3192 defaulted that appears in a class shall be the first declaration of 3193 that function. */ 3194 special_function_kind sfk = special_function_p (olddecl); 3195 if (sfk == sfk_comparison && DECL_DEFAULTED_FN (newdecl)) 3196 return G_("comparison operator %q+D defaulted after " 3197 "its first declaration"); 3198 3199 check_abi_tag_redeclaration 3200 (olddecl, lookup_attribute ("abi_tag", DECL_ATTRIBUTES (olddecl)), 3201 lookup_attribute ("abi_tag", DECL_ATTRIBUTES (newdecl))); 3202 3203 return NULL; 3204 } 3205 else if (TREE_CODE (newdecl) == TEMPLATE_DECL) 3206 { 3207 tree nt, ot; 3208 3209 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == CONCEPT_DECL) 3210 return G_("redefinition of %q#D"); 3211 3212 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) != FUNCTION_DECL) 3213 return redeclaration_error_message (DECL_TEMPLATE_RESULT (newdecl), 3214 DECL_TEMPLATE_RESULT (olddecl)); 3215 3216 if (DECL_TEMPLATE_RESULT (newdecl) == DECL_TEMPLATE_RESULT (olddecl)) 3217 return NULL; 3218 3219 nt = DECL_TEMPLATE_RESULT (newdecl); 3220 if (DECL_TEMPLATE_INFO (nt)) 3221 nt = DECL_TEMPLATE_RESULT (template_for_substitution (nt)); 3222 ot = DECL_TEMPLATE_RESULT (olddecl); 3223 if (DECL_TEMPLATE_INFO (ot)) 3224 ot = DECL_TEMPLATE_RESULT (template_for_substitution (ot)); 3225 if (DECL_INITIAL (nt) && DECL_INITIAL (ot) 3226 && (!GNU_INLINE_P (ot) || GNU_INLINE_P (nt))) 3227 return G_("redefinition of %q#D"); 3228 3229 if (DECL_DECLARED_INLINE_P (ot) && DECL_DECLARED_INLINE_P (nt)) 3230 { 3231 bool olda = GNU_INLINE_P (ot); 3232 bool newa = GNU_INLINE_P (nt); 3233 3234 if (olda != newa) 3235 { 3236 if (newa) 3237 return G_("%q+D redeclared inline with " 3238 "%<gnu_inline%> attribute"); 3239 else 3240 return G_("%q+D redeclared inline without " 3241 "%<gnu_inline%> attribute"); 3242 } 3243 } 3244 3245 if (deduction_guide_p (olddecl) 3246 && deduction_guide_p (newdecl)) 3247 return G_("deduction guide %q+D redeclared"); 3248 3249 /* Core issue #226 (C++11): 3250 3251 If a friend function template declaration specifies a 3252 default template-argument, that declaration shall be a 3253 definition and shall be the only declaration of the 3254 function template in the translation unit. */ 3255 if ((cxx_dialect != cxx98) 3256 && TREE_CODE (ot) == FUNCTION_DECL && DECL_UNIQUE_FRIEND_P (ot) 3257 && !check_default_tmpl_args (nt, DECL_TEMPLATE_PARMS (newdecl), 3258 /*is_primary=*/true, 3259 /*is_partial=*/false, 3260 /*is_friend_decl=*/2)) 3261 return G_("redeclaration of friend %q#D " 3262 "may not have default template arguments"); 3263 3264 return NULL; 3265 } 3266 else if (VAR_P (newdecl) 3267 && (CP_DECL_THREAD_LOCAL_P (newdecl) 3268 != CP_DECL_THREAD_LOCAL_P (olddecl)) 3269 && (! DECL_LANG_SPECIFIC (olddecl) 3270 || ! CP_DECL_THREADPRIVATE_P (olddecl) 3271 || CP_DECL_THREAD_LOCAL_P (newdecl))) 3272 { 3273 /* Only variables can be thread-local, and all declarations must 3274 agree on this property. */ 3275 if (CP_DECL_THREAD_LOCAL_P (newdecl)) 3276 return G_("thread-local declaration of %q#D follows " 3277 "non-thread-local declaration"); 3278 else 3279 return G_("non-thread-local declaration of %q#D follows " 3280 "thread-local declaration"); 3281 } 3282 else if (toplevel_bindings_p () || DECL_NAMESPACE_SCOPE_P (newdecl)) 3283 { 3284 /* The objects have been declared at namespace scope. If either 3285 is a member of an anonymous union, then this is an invalid 3286 redeclaration. For example: 3287 3288 int i; 3289 union { int i; }; 3290 3291 is invalid. */ 3292 if ((VAR_P (newdecl) && DECL_ANON_UNION_VAR_P (newdecl)) 3293 || (VAR_P (olddecl) && DECL_ANON_UNION_VAR_P (olddecl))) 3294 return G_("redeclaration of %q#D"); 3295 /* If at least one declaration is a reference, there is no 3296 conflict. For example: 3297 3298 int i = 3; 3299 extern int i; 3300 3301 is valid. */ 3302 if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl)) 3303 return NULL; 3304 3305 /* Static data member declared outside a class definition 3306 if the variable is defined within the class with constexpr 3307 specifier is declaration rather than definition (and 3308 deprecated). */ 3309 if (cxx_dialect >= cxx17 3310 && VAR_P (olddecl) 3311 && DECL_CLASS_SCOPE_P (olddecl) 3312 && DECL_DECLARED_CONSTEXPR_P (olddecl) 3313 && !DECL_INITIAL (newdecl)) 3314 { 3315 DECL_EXTERNAL (newdecl) = 1; 3316 /* For now, only warn with explicit -Wdeprecated. */ 3317 if (OPTION_SET_P (warn_deprecated)) 3318 { 3319 auto_diagnostic_group d; 3320 if (warning_at (DECL_SOURCE_LOCATION (newdecl), OPT_Wdeprecated, 3321 "redundant redeclaration of %<constexpr%> " 3322 "static data member %qD", newdecl)) 3323 inform (DECL_SOURCE_LOCATION (olddecl), 3324 "previous declaration of %qD", olddecl); 3325 } 3326 return NULL; 3327 } 3328 3329 /* Reject two definitions. */ 3330 return G_("redefinition of %q#D"); 3331 } 3332 else 3333 { 3334 /* Objects declared with block scope: */ 3335 /* Reject two definitions, and reject a definition 3336 together with an external reference. */ 3337 if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl))) 3338 return G_("redeclaration of %q#D"); 3339 return NULL; 3340 } 3341 } 3342 3343 3345 /* Hash and equality functions for the named_label table. */ 3346 3347 hashval_t 3348 named_label_hash::hash (const value_type entry) 3349 { 3350 return IDENTIFIER_HASH_VALUE (entry->name); 3351 } 3352 3353 bool 3354 named_label_hash::equal (const value_type entry, compare_type name) 3355 { 3356 return name == entry->name; 3357 } 3358 3359 /* Look for a label named ID in the current function. If one cannot 3360 be found, create one. Return the named_label_entry, or NULL on 3361 failure. */ 3362 3363 static named_label_entry * 3364 lookup_label_1 (tree id, bool making_local_p) 3365 { 3366 auto_cond_timevar tv (TV_NAME_LOOKUP); 3367 3368 /* You can't use labels at global scope. */ 3369 if (current_function_decl == NULL_TREE) 3370 { 3371 error ("label %qE referenced outside of any function", id); 3372 return NULL; 3373 } 3374 3375 if (!named_labels) 3376 named_labels = hash_table<named_label_hash>::create_ggc (13); 3377 3378 hashval_t hash = IDENTIFIER_HASH_VALUE (id); 3379 named_label_entry **slot 3380 = named_labels->find_slot_with_hash (id, hash, INSERT); 3381 named_label_entry *old = *slot; 3382 3383 if (old && old->label_decl) 3384 { 3385 if (!making_local_p) 3386 return old; 3387 3388 if (old->binding_level == current_binding_level) 3389 { 3390 error ("local label %qE conflicts with existing label", id); 3391 inform (DECL_SOURCE_LOCATION (old->label_decl), "previous label"); 3392 return NULL; 3393 } 3394 } 3395 3396 /* We are making a new decl, create or reuse the named_label_entry */ 3397 named_label_entry *ent = NULL; 3398 if (old && !old->label_decl) 3399 ent = old; 3400 else 3401 { 3402 ent = ggc_cleared_alloc<named_label_entry> (); 3403 ent->name = id; 3404 ent->outer = old; 3405 *slot = ent; 3406 } 3407 3408 /* Now create the LABEL_DECL. */ 3409 tree decl = build_decl (input_location, LABEL_DECL, id, void_type_node); 3410 3411 DECL_CONTEXT (decl) = current_function_decl; 3412 SET_DECL_MODE (decl, VOIDmode); 3413 if (making_local_p) 3414 { 3415 C_DECLARED_LABEL_FLAG (decl) = true; 3416 DECL_CHAIN (decl) = current_binding_level->names; 3417 current_binding_level->names = decl; 3418 } 3419 3420 ent->label_decl = decl; 3421 3422 return ent; 3423 } 3424 3425 /* Wrapper for lookup_label_1. */ 3426 3427 tree 3428 lookup_label (tree id) 3429 { 3430 named_label_entry *ent = lookup_label_1 (id, false); 3431 return ent ? ent->label_decl : NULL_TREE; 3432 } 3433 3434 tree 3435 declare_local_label (tree id) 3436 { 3437 named_label_entry *ent = lookup_label_1 (id, true); 3438 return ent ? ent->label_decl : NULL_TREE; 3439 } 3440 3441 /* Returns nonzero if it is ill-formed to jump past the declaration of 3442 DECL. Returns 2 if it's also a real problem. */ 3443 3444 static int 3445 decl_jump_unsafe (tree decl) 3446 { 3447 /* [stmt.dcl]/3: A program that jumps from a point where a local variable 3448 with automatic storage duration is not in scope to a point where it is 3449 in scope is ill-formed unless the variable has scalar type, class type 3450 with a trivial default constructor and a trivial destructor, a 3451 cv-qualified version of one of these types, or an array of one of the 3452 preceding types and is declared without an initializer (8.5). */ 3453 tree type = TREE_TYPE (decl); 3454 3455 if (!VAR_P (decl) || TREE_STATIC (decl) 3456 || type == error_mark_node) 3457 return 0; 3458 3459 if (DECL_NONTRIVIALLY_INITIALIZED_P (decl) 3460 || variably_modified_type_p (type, NULL_TREE)) 3461 return 2; 3462 3463 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)) 3464 return 1; 3465 3466 return 0; 3467 } 3468 3469 /* A subroutine of check_previous_goto_1 and check_goto to identify a branch 3470 to the user. */ 3471 3472 static bool 3473 identify_goto (tree decl, location_t loc, const location_t *locus, 3474 diagnostic_t diag_kind) 3475 { 3476 bool complained 3477 = emit_diagnostic (diag_kind, loc, 0, 3478 decl ? N_("jump to label %qD") 3479 : N_("jump to case label"), decl); 3480 if (complained && locus) 3481 inform (*locus, " from here"); 3482 return complained; 3483 } 3484 3485 /* Check that a single previously seen jump to a newly defined label 3486 is OK. DECL is the LABEL_DECL or 0; LEVEL is the binding_level for 3487 the jump context; NAMES are the names in scope in LEVEL at the jump 3488 context; LOCUS is the source position of the jump or 0. Returns 3489 true if all is well. */ 3490 3491 static bool 3492 check_previous_goto_1 (tree decl, cp_binding_level* level, tree names, 3493 bool exited_omp, const location_t *locus) 3494 { 3495 cp_binding_level *b; 3496 bool complained = false; 3497 int identified = 0; 3498 bool saw_eh = false, saw_omp = false, saw_tm = false, saw_cxif = false; 3499 bool saw_ceif = false; 3500 3501 if (exited_omp) 3502 { 3503 complained = identify_goto (decl, input_location, locus, DK_ERROR); 3504 if (complained) 3505 inform (input_location, " exits OpenMP structured block"); 3506 saw_omp = true; 3507 identified = 2; 3508 } 3509 3510 for (b = current_binding_level; b ; b = b->level_chain) 3511 { 3512 tree new_decls, old_decls = (b == level ? names : NULL_TREE); 3513 3514 for (new_decls = b->names; new_decls != old_decls; 3515 new_decls = (DECL_P (new_decls) ? DECL_CHAIN (new_decls) 3516 : TREE_CHAIN (new_decls))) 3517 { 3518 int problem = decl_jump_unsafe (new_decls); 3519 if (! problem) 3520 continue; 3521 3522 if (!identified) 3523 { 3524 complained = identify_goto (decl, input_location, locus, 3525 problem > 1 3526 ? DK_ERROR : DK_PERMERROR); 3527 identified = 1; 3528 } 3529 if (complained) 3530 { 3531 if (problem > 1) 3532 inform (DECL_SOURCE_LOCATION (new_decls), 3533 " crosses initialization of %q#D", new_decls); 3534 else 3535 inform (DECL_SOURCE_LOCATION (new_decls), 3536 " enters scope of %q#D, which has " 3537 "non-trivial destructor", new_decls); 3538 } 3539 } 3540 3541 if (b == level) 3542 break; 3543 3544 const char *inf = NULL; 3545 location_t loc = input_location; 3546 switch (b->kind) 3547 { 3548 case sk_try: 3549 if (!saw_eh) 3550 inf = G_(" enters %<try%> block"); 3551 saw_eh = true; 3552 break; 3553 3554 case sk_catch: 3555 if (!saw_eh) 3556 inf = G_(" enters %<catch%> block"); 3557 saw_eh = true; 3558 break; 3559 3560 case sk_omp: 3561 if (!saw_omp) 3562 inf = G_(" enters OpenMP structured block"); 3563 saw_omp = true; 3564 break; 3565 3566 case sk_transaction: 3567 if (!saw_tm) 3568 inf = G_(" enters synchronized or atomic statement"); 3569 saw_tm = true; 3570 break; 3571 3572 case sk_block: 3573 if (!saw_cxif && level_for_constexpr_if (b->level_chain)) 3574 { 3575 inf = G_(" enters %<constexpr if%> statement"); 3576 loc = EXPR_LOCATION (b->level_chain->this_entity); 3577 saw_cxif = true; 3578 } 3579 else if (!saw_ceif && level_for_consteval_if (b->level_chain)) 3580 { 3581 inf = G_(" enters %<consteval if%> statement"); 3582 loc = EXPR_LOCATION (b->level_chain->this_entity); 3583 saw_ceif = true; 3584 } 3585 break; 3586 3587 default: 3588 break; 3589 } 3590 3591 if (inf) 3592 { 3593 if (identified < 2) 3594 complained = identify_goto (decl, input_location, locus, DK_ERROR); 3595 identified = 2; 3596 if (complained) 3597 inform (loc, inf); 3598 } 3599 } 3600 3601 return !identified; 3602 } 3603 3604 static void 3605 check_previous_goto (tree decl, struct named_label_use_entry *use) 3606 { 3607 check_previous_goto_1 (decl, use->binding_level, 3608 use->names_in_scope, use->in_omp_scope, 3609 &use->o_goto_locus); 3610 } 3611 3612 static bool 3613 check_switch_goto (cp_binding_level* level) 3614 { 3615 return check_previous_goto_1 (NULL_TREE, level, level->names, false, NULL); 3616 } 3617 3618 /* Check that a new jump to a label DECL is OK. Called by 3619 finish_goto_stmt. */ 3620 3621 void 3622 check_goto (tree decl) 3623 { 3624 /* We can't know where a computed goto is jumping. 3625 So we assume that it's OK. */ 3626 if (TREE_CODE (decl) != LABEL_DECL) 3627 return; 3628 3629 /* We didn't record any information about this label when we created it, 3630 and there's not much point since it's trivial to analyze as a return. */ 3631 if (decl == cdtor_label) 3632 return; 3633 3634 hashval_t hash = IDENTIFIER_HASH_VALUE (DECL_NAME (decl)); 3635 named_label_entry **slot 3636 = named_labels->find_slot_with_hash (DECL_NAME (decl), hash, NO_INSERT); 3637 named_label_entry *ent = *slot; 3638 3639 /* If the label hasn't been defined yet, defer checking. */ 3640 if (! DECL_INITIAL (decl)) 3641 { 3642 /* Don't bother creating another use if the last goto had the 3643 same data, and will therefore create the same set of errors. */ 3644 if (ent->uses 3645 && ent->uses->names_in_scope == current_binding_level->names) 3646 return; 3647 3648 named_label_use_entry *new_use 3649 = ggc_alloc<named_label_use_entry> (); 3650 new_use->binding_level = current_binding_level; 3651 new_use->names_in_scope = current_binding_level->names; 3652 new_use->o_goto_locus = input_location; 3653 new_use->in_omp_scope = false; 3654 3655 new_use->next = ent->uses; 3656 ent->uses = new_use; 3657 return; 3658 } 3659 3660 cp_function_chain->backward_goto = true; 3661 3662 bool saw_catch = false, complained = false; 3663 int identified = 0; 3664 tree bad; 3665 unsigned ix; 3666 3667 if (ent->in_try_scope || ent->in_catch_scope || ent->in_transaction_scope 3668 || ent->in_constexpr_if || ent->in_consteval_if 3669 || ent->in_omp_scope || !vec_safe_is_empty (ent->bad_decls)) 3670 { 3671 diagnostic_t diag_kind = DK_PERMERROR; 3672 if (ent->in_try_scope || ent->in_catch_scope || ent->in_constexpr_if 3673 || ent->in_consteval_if || ent->in_transaction_scope 3674 || ent->in_omp_scope) 3675 diag_kind = DK_ERROR; 3676 complained = identify_goto (decl, DECL_SOURCE_LOCATION (decl), 3677 &input_location, diag_kind); 3678 identified = 1 + (diag_kind == DK_ERROR); 3679 } 3680 3681 FOR_EACH_VEC_SAFE_ELT (ent->bad_decls, ix, bad) 3682 { 3683 int u = decl_jump_unsafe (bad); 3684 3685 if (u > 1 && DECL_ARTIFICIAL (bad)) 3686 { 3687 /* Can't skip init of __exception_info. */ 3688 if (identified == 1) 3689 { 3690 complained = identify_goto (decl, DECL_SOURCE_LOCATION (decl), 3691 &input_location, DK_ERROR); 3692 identified = 2; 3693 } 3694 if (complained) 3695 inform (DECL_SOURCE_LOCATION (bad), " enters %<catch%> block"); 3696 saw_catch = true; 3697 } 3698 else if (complained) 3699 { 3700 if (u > 1) 3701 inform (DECL_SOURCE_LOCATION (bad), 3702 " skips initialization of %q#D", bad); 3703 else 3704 inform (DECL_SOURCE_LOCATION (bad), 3705 " enters scope of %q#D which has " 3706 "non-trivial destructor", bad); 3707 } 3708 } 3709 3710 if (complained) 3711 { 3712 if (ent->in_try_scope) 3713 inform (input_location, " enters %<try%> block"); 3714 else if (ent->in_catch_scope && !saw_catch) 3715 inform (input_location, " enters %<catch%> block"); 3716 else if (ent->in_transaction_scope) 3717 inform (input_location, " enters synchronized or atomic statement"); 3718 else if (ent->in_constexpr_if) 3719 inform (input_location, " enters %<constexpr if%> statement"); 3720 else if (ent->in_consteval_if) 3721 inform (input_location, " enters %<consteval if%> statement"); 3722 } 3723 3724 if (ent->in_omp_scope) 3725 { 3726 if (complained) 3727 inform (input_location, " enters OpenMP structured block"); 3728 } 3729 else if (flag_openmp) 3730 for (cp_binding_level *b = current_binding_level; b ; b = b->level_chain) 3731 { 3732 if (b == ent->binding_level) 3733 break; 3734 if (b->kind == sk_omp) 3735 { 3736 if (identified < 2) 3737 { 3738 complained = identify_goto (decl, 3739 DECL_SOURCE_LOCATION (decl), 3740 &input_location, DK_ERROR); 3741 identified = 2; 3742 } 3743 if (complained) 3744 inform (input_location, " exits OpenMP structured block"); 3745 break; 3746 } 3747 } 3748 } 3749 3750 /* Check that a return is ok wrt OpenMP structured blocks. 3751 Called by finish_return_stmt. Returns true if all is well. */ 3752 3753 bool 3754 check_omp_return (void) 3755 { 3756 for (cp_binding_level *b = current_binding_level; b ; b = b->level_chain) 3757 if (b->kind == sk_omp) 3758 { 3759 error ("invalid exit from OpenMP structured block"); 3760 return false; 3761 } 3762 else if (b->kind == sk_function_parms) 3763 break; 3764 return true; 3765 } 3766 3767 /* Define a label, specifying the location in the source file. 3768 Return the LABEL_DECL node for the label. */ 3769 3770 tree 3771 define_label (location_t location, tree name) 3772 { 3773 auto_cond_timevar tv (TV_NAME_LOOKUP); 3774 3775 /* After labels, make any new cleanups in the function go into their 3776 own new (temporary) binding contour. */ 3777 for (cp_binding_level *p = current_binding_level; 3778 p->kind != sk_function_parms; 3779 p = p->level_chain) 3780 p->more_cleanups_ok = 0; 3781 3782 named_label_entry *ent = lookup_label_1 (name, false); 3783 tree decl = ent->label_decl; 3784 3785 if (DECL_INITIAL (decl) != NULL_TREE) 3786 { 3787 error ("duplicate label %qD", decl); 3788 return error_mark_node; 3789 } 3790 else 3791 { 3792 /* Mark label as having been defined. */ 3793 DECL_INITIAL (decl) = error_mark_node; 3794 /* Say where in the source. */ 3795 DECL_SOURCE_LOCATION (decl) = location; 3796 3797 ent->binding_level = current_binding_level; 3798 ent->names_in_scope = current_binding_level->names; 3799 3800 for (named_label_use_entry *use = ent->uses; use; use = use->next) 3801 check_previous_goto (decl, use); 3802 ent->uses = NULL; 3803 } 3804 3805 return decl; 3806 } 3807 3808 struct cp_switch 3809 { 3810 cp_binding_level *level; 3811 struct cp_switch *next; 3812 /* The SWITCH_STMT being built. */ 3813 tree switch_stmt; 3814 /* A splay-tree mapping the low element of a case range to the high 3815 element, or NULL_TREE if there is no high element. Used to 3816 determine whether or not a new case label duplicates an old case 3817 label. We need a tree, rather than simply a hash table, because 3818 of the GNU case range extension. */ 3819 splay_tree cases; 3820 /* Remember whether a default: case label has been seen. */ 3821 bool has_default_p; 3822 /* Remember whether a BREAK_STMT has been seen in this SWITCH_STMT. */ 3823 bool break_stmt_seen_p; 3824 /* Set if inside of {FOR,DO,WHILE}_BODY nested inside of a switch, 3825 where BREAK_STMT doesn't belong to the SWITCH_STMT. */ 3826 bool in_loop_body_p; 3827 }; 3828 3829 /* A stack of the currently active switch statements. The innermost 3830 switch statement is on the top of the stack. There is no need to 3831 mark the stack for garbage collection because it is only active 3832 during the processing of the body of a function, and we never 3833 collect at that point. */ 3834 3835 static struct cp_switch *switch_stack; 3836 3837 /* Called right after a switch-statement condition is parsed. 3838 SWITCH_STMT is the switch statement being parsed. */ 3839 3840 void 3841 push_switch (tree switch_stmt) 3842 { 3843 struct cp_switch *p = XNEW (struct cp_switch); 3844 p->level = current_binding_level; 3845 p->next = switch_stack; 3846 p->switch_stmt = switch_stmt; 3847 p->cases = splay_tree_new (case_compare, NULL, NULL); 3848 p->has_default_p = false; 3849 p->break_stmt_seen_p = false; 3850 p->in_loop_body_p = false; 3851 switch_stack = p; 3852 } 3853 3854 void 3855 pop_switch (void) 3856 { 3857 struct cp_switch *cs = switch_stack; 3858 3859 /* Emit warnings as needed. */ 3860 location_t switch_location = cp_expr_loc_or_input_loc (cs->switch_stmt); 3861 tree cond = SWITCH_STMT_COND (cs->switch_stmt); 3862 const bool bool_cond_p 3863 = (SWITCH_STMT_TYPE (cs->switch_stmt) 3864 && TREE_CODE (SWITCH_STMT_TYPE (cs->switch_stmt)) == BOOLEAN_TYPE); 3865 if (!processing_template_decl) 3866 c_do_switch_warnings (cs->cases, switch_location, 3867 SWITCH_STMT_TYPE (cs->switch_stmt), cond, 3868 bool_cond_p); 3869 3870 /* For the benefit of block_may_fallthru remember if the switch body 3871 case labels cover all possible values and if there are break; stmts. */ 3872 if (cs->has_default_p 3873 || (!processing_template_decl 3874 && c_switch_covers_all_cases_p (cs->cases, 3875 SWITCH_STMT_TYPE (cs->switch_stmt)))) 3876 SWITCH_STMT_ALL_CASES_P (cs->switch_stmt) = 1; 3877 if (!cs->break_stmt_seen_p) 3878 SWITCH_STMT_NO_BREAK_P (cs->switch_stmt) = 1; 3879 /* Now that we're done with the switch warnings, set the switch type 3880 to the type of the condition if the index type was of scoped enum type. 3881 (Such types don't participate in the integer promotions.) We do this 3882 because of bit-fields whose declared type is a scoped enum type: 3883 gimplification will use the lowered index type, but convert the 3884 case values to SWITCH_STMT_TYPE, which would have been the declared type 3885 and verify_gimple_switch doesn't accept that. */ 3886 if (is_bitfield_expr_with_lowered_type (cond)) 3887 SWITCH_STMT_TYPE (cs->switch_stmt) = TREE_TYPE (cond); 3888 gcc_assert (!cs->in_loop_body_p); 3889 splay_tree_delete (cs->cases); 3890 switch_stack = switch_stack->next; 3891 free (cs); 3892 } 3893 3894 /* Note that a BREAK_STMT is about to be added. If it is inside of 3895 a SWITCH_STMT and not inside of a loop body inside of it, note 3896 in switch_stack we've seen a BREAK_STMT. */ 3897 3898 void 3899 note_break_stmt (void) 3900 { 3901 if (switch_stack && !switch_stack->in_loop_body_p) 3902 switch_stack->break_stmt_seen_p = true; 3903 } 3904 3905 /* Note the start of processing of an iteration statement's body. 3906 The note_break_stmt function will do nothing while processing it. 3907 Return a flag that should be passed to note_iteration_stmt_body_end. */ 3908 3909 bool 3910 note_iteration_stmt_body_start (void) 3911 { 3912 if (!switch_stack) 3913 return false; 3914 bool ret = switch_stack->in_loop_body_p; 3915 switch_stack->in_loop_body_p = true; 3916 return ret; 3917 } 3918 3919 /* Note the end of processing of an iteration statement's body. */ 3920 3921 void 3922 note_iteration_stmt_body_end (bool prev) 3923 { 3924 if (switch_stack) 3925 switch_stack->in_loop_body_p = prev; 3926 } 3927 3928 /* Convert a case constant VALUE in a switch to the type TYPE of the switch 3929 condition. Note that if TYPE and VALUE are already integral we don't 3930 really do the conversion because the language-independent 3931 warning/optimization code will work better that way. */ 3932 3933 static tree 3934 case_conversion (tree type, tree value) 3935 { 3936 if (value == NULL_TREE) 3937 return value; 3938 3939 value = mark_rvalue_use (value); 3940 3941 if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type)) 3942 type = type_promotes_to (type); 3943 3944 tree ovalue = value; 3945 /* The constant-expression VALUE shall be a converted constant expression 3946 of the adjusted type of the switch condition, which doesn't allow 3947 narrowing conversions. */ 3948 value = build_converted_constant_expr (type, value, tf_warning_or_error); 3949 3950 if (cxx_dialect >= cxx11 3951 && (SCOPED_ENUM_P (type) 3952 || !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (ovalue)))) 3953 /* Use the converted value. */; 3954 else 3955 /* The already integral case. */ 3956 value = ovalue; 3957 3958 return cxx_constant_value (value); 3959 } 3960 3961 /* Note that we've seen a definition of a case label, and complain if this 3962 is a bad place for one. */ 3963 3964 tree 3965 finish_case_label (location_t loc, tree low_value, tree high_value) 3966 { 3967 tree cond, r; 3968 cp_binding_level *p; 3969 tree type; 3970 3971 if (low_value == NULL_TREE && high_value == NULL_TREE) 3972 switch_stack->has_default_p = true; 3973 3974 if (processing_template_decl) 3975 { 3976 tree label; 3977 3978 /* For templates, just add the case label; we'll do semantic 3979 analysis at instantiation-time. */ 3980 label = build_decl (loc, LABEL_DECL, NULL_TREE, void_type_node); 3981 return add_stmt (build_case_label (low_value, high_value, label)); 3982 } 3983 3984 /* Find the condition on which this switch statement depends. */ 3985 cond = SWITCH_STMT_COND (switch_stack->switch_stmt); 3986 if (cond && TREE_CODE (cond) == TREE_LIST) 3987 cond = TREE_VALUE (cond); 3988 3989 if (!check_switch_goto (switch_stack->level)) 3990 return error_mark_node; 3991 3992 type = SWITCH_STMT_TYPE (switch_stack->switch_stmt); 3993 if (type == error_mark_node) 3994 return error_mark_node; 3995 3996 low_value = case_conversion (type, low_value); 3997 high_value = case_conversion (type, high_value); 3998 3999 r = c_add_case_label (loc, switch_stack->cases, cond, low_value, high_value); 4000 4001 /* After labels, make any new cleanups in the function go into their 4002 own new (temporary) binding contour. */ 4003 for (p = current_binding_level; 4004 p->kind != sk_function_parms; 4005 p = p->level_chain) 4006 p->more_cleanups_ok = 0; 4007 4008 return r; 4009 } 4010 4011 struct typename_info { 4013 tree scope; 4014 tree name; 4015 tree template_id; 4016 bool enum_p; 4017 bool class_p; 4018 }; 4019 4020 struct typename_hasher : ggc_ptr_hash<tree_node> 4021 { 4022 typedef typename_info *compare_type; 4023 4024 /* Hash a TYPENAME_TYPE. */ 4025 4026 static hashval_t 4027 hash (tree t) 4028 { 4029 hashval_t hash; 4030 4031 hash = (htab_hash_pointer (TYPE_CONTEXT (t)) 4032 ^ htab_hash_pointer (TYPE_IDENTIFIER (t))); 4033 4034 return hash; 4035 } 4036 4037 /* Compare two TYPENAME_TYPEs. */ 4038 4039 static bool 4040 equal (tree t1, const typename_info *t2) 4041 { 4042 return (TYPE_IDENTIFIER (t1) == t2->name 4043 && TYPE_CONTEXT (t1) == t2->scope 4044 && TYPENAME_TYPE_FULLNAME (t1) == t2->template_id 4045 && TYPENAME_IS_ENUM_P (t1) == t2->enum_p 4046 && TYPENAME_IS_CLASS_P (t1) == t2->class_p); 4047 } 4048 }; 4049 4050 /* Build a TYPENAME_TYPE. If the type is `typename T::t', CONTEXT is 4051 the type of `T', NAME is the IDENTIFIER_NODE for `t'. 4052 4053 Returns the new TYPENAME_TYPE. */ 4054 4055 static GTY (()) hash_table<typename_hasher> *typename_htab; 4056 4057 tree 4058 build_typename_type (tree context, tree name, tree fullname, 4059 enum tag_types tag_type) 4060 { 4061 typename_info ti; 4062 4063 if (typename_htab == NULL) 4064 typename_htab = hash_table<typename_hasher>::create_ggc (61); 4065 4066 ti.scope = FROB_CONTEXT (context); 4067 ti.name = name; 4068 ti.template_id = fullname; 4069 ti.enum_p = tag_type == enum_type; 4070 ti.class_p = (tag_type == class_type 4071 || tag_type == record_type 4072 || tag_type == union_type); 4073 hashval_t hash = (htab_hash_pointer (ti.scope) 4074 ^ htab_hash_pointer (ti.name)); 4075 4076 /* See if we already have this type. */ 4077 tree *e = typename_htab->find_slot_with_hash (&ti, hash, INSERT); 4078 tree t = *e; 4079 if (*e) 4080 t = *e; 4081 else 4082 { 4083 /* Build the TYPENAME_TYPE. */ 4084 t = cxx_make_type (TYPENAME_TYPE); 4085 TYPE_CONTEXT (t) = ti.scope; 4086 TYPENAME_TYPE_FULLNAME (t) = ti.template_id; 4087 TYPENAME_IS_ENUM_P (t) = ti.enum_p; 4088 TYPENAME_IS_CLASS_P (t) = ti.class_p; 4089 4090 /* Build the corresponding TYPE_DECL. */ 4091 tree d = build_decl (input_location, TYPE_DECL, name, t); 4092 TYPE_NAME (t) = d; 4093 TYPE_STUB_DECL (t) = d; 4094 DECL_CONTEXT (d) = ti.scope; 4095 DECL_ARTIFICIAL (d) = 1; 4096 4097 /* Store it in the hash table. */ 4098 *e = t; 4099 4100 /* TYPENAME_TYPEs must always be compared structurally, because 4101 they may or may not resolve down to another type depending on 4102 the currently open classes. */ 4103 SET_TYPE_STRUCTURAL_EQUALITY (t); 4104 } 4105 4106 return t; 4107 } 4108 4109 /* Resolve `typename CONTEXT::NAME'. TAG_TYPE indicates the tag 4110 provided to name the type. Returns an appropriate type, unless an 4111 error occurs, in which case error_mark_node is returned. If we 4112 locate a non-artificial TYPE_DECL and TF_KEEP_TYPE_DECL is set, we 4113 return that, rather than the _TYPE it corresponds to, in other 4114 cases we look through the type decl. If TF_ERROR is set, complain 4115 about errors, otherwise be quiet. */ 4116 4117 tree 4118 make_typename_type (tree context, tree name, enum tag_types tag_type, 4119 tsubst_flags_t complain) 4120 { 4121 tree fullname; 4122 tree t; 4123 bool want_template; 4124 4125 if (name == error_mark_node 4126 || context == NULL_TREE 4127 || context == error_mark_node) 4128 return error_mark_node; 4129 4130 if (TYPE_P (name)) 4131 { 4132 if (!(TYPE_LANG_SPECIFIC (name) 4133 && (CLASSTYPE_IS_TEMPLATE (name) 4134 || CLASSTYPE_USE_TEMPLATE (name)))) 4135 name = TYPE_IDENTIFIER (name); 4136 else 4137 /* Create a TEMPLATE_ID_EXPR for the type. */ 4138 name = build_nt (TEMPLATE_ID_EXPR, 4139 CLASSTYPE_TI_TEMPLATE (name), 4140 CLASSTYPE_TI_ARGS (name)); 4141 } 4142 else if (TREE_CODE (name) == TYPE_DECL) 4143 name = DECL_NAME (name); 4144 4145 fullname = name; 4146 4147 if (TREE_CODE (name) == TEMPLATE_ID_EXPR) 4148 { 4149 name = TREE_OPERAND (name, 0); 4150 if (DECL_TYPE_TEMPLATE_P (name)) 4151 name = TREE_OPERAND (fullname, 0) = DECL_NAME (name); 4152 if (TREE_CODE (name) != IDENTIFIER_NODE) 4153 { 4154 if (complain & tf_error) 4155 error ("%qD is not a type", name); 4156 return error_mark_node; 4157 } 4158 } 4159 if (TREE_CODE (name) == TEMPLATE_DECL) 4160 { 4161 if (complain & tf_error) 4162 error ("%qD used without template arguments", name); 4163 return error_mark_node; 4164 } 4165 else if (is_overloaded_fn (name)) 4166 { 4167 if (complain & tf_error) 4168 error ("%qD is a function, not a type", name); 4169 return error_mark_node; 4170 } 4171 gcc_assert (identifier_p (name)); 4172 gcc_assert (TYPE_P (context)); 4173 4174 if (TREE_CODE (context) == TYPE_PACK_EXPANSION) 4175 /* This can happen for C++17 variadic using (c++/88986). */; 4176 else if (!MAYBE_CLASS_TYPE_P (context)) 4177 { 4178 if (complain & tf_error) 4179 error ("%q#T is not a class", context); 4180 return error_mark_node; 4181 } 4182 4183 /* When the CONTEXT is a dependent type, NAME could refer to a 4184 dependent base class of CONTEXT. But look inside it anyway 4185 if CONTEXT is a currently open scope, in case it refers to a 4186 member of the current instantiation or a non-dependent base; 4187 lookup will stop when we hit a dependent base. */ 4188 if (!dependent_scope_p (context)) 4189 /* We should only set WANT_TYPE when we're a nested typename type. 4190 Then we can give better diagnostics if we find a non-type. */ 4191 t = lookup_field (context, name, 2, /*want_type=*/true); 4192 else 4193 t = NULL_TREE; 4194 4195 if ((!t || TREE_CODE (t) == TREE_LIST) && dependent_type_p (context)) 4196 return build_typename_type (context, name, fullname, tag_type); 4197 4198 want_template = TREE_CODE (fullname) == TEMPLATE_ID_EXPR; 4199 4200 if (!t) 4201 { 4202 if (complain & tf_error) 4203 { 4204 if (!COMPLETE_TYPE_P (context)) 4205 cxx_incomplete_type_error (NULL_TREE, context); 4206 else 4207 error (want_template ? G_("no class template named %q#T in %q#T") 4208 : G_("no type named %q#T in %q#T"), name, context); 4209 } 4210 return error_mark_node; 4211 } 4212 4213 /* Pull out the template from an injected-class-name (or multiple). */ 4214 if (want_template) 4215 t = maybe_get_template_decl_from_type_decl (t); 4216 4217 if (TREE_CODE (t) == TREE_LIST) 4218 { 4219 if (complain & tf_error) 4220 { 4221 error ("lookup of %qT in %qT is ambiguous", name, context); 4222 print_candidates (t); 4223 } 4224 return error_mark_node; 4225 } 4226 4227 if (want_template && !DECL_TYPE_TEMPLATE_P (t)) 4228 { 4229 if (complain & tf_error) 4230 error ("%<typename %T::%D%> names %q#T, which is not a class template", 4231 context, name, t); 4232 return error_mark_node; 4233 } 4234 if (!want_template && TREE_CODE (t) != TYPE_DECL) 4235 { 4236 if ((complain & tf_tst_ok) && cxx_dialect >= cxx17 4237 && DECL_TYPE_TEMPLATE_P (t)) 4238 /* The caller permits this typename-specifier to name a template 4239 (because it appears in a CTAD-enabled context). */; 4240 else 4241 { 4242 if (complain & tf_error) 4243 error ("%<typename %T::%D%> names %q#T, which is not a type", 4244 context, name, t); 4245 return error_mark_node; 4246 } 4247 } 4248 4249 if (!check_accessibility_of_qualified_id (t, /*object_type=*/NULL_TREE, 4250 context, complain)) 4251 return error_mark_node; 4252 4253 if (!want_template && DECL_TYPE_TEMPLATE_P (t)) 4254 return make_template_placeholder (t); 4255 4256 if (want_template) 4257 { 4258 t = lookup_template_class (t, TREE_OPERAND (fullname, 1), 4259 NULL_TREE, context, 4260 /*entering_scope=*/0, 4261 complain | tf_user); 4262 if (t == error_mark_node) 4263 return error_mark_node; 4264 t = TYPE_NAME (t); 4265 } 4266 4267 if (DECL_ARTIFICIAL (t) || !(complain & tf_keep_type_decl)) 4268 t = TREE_TYPE (t); 4269 4270 maybe_record_typedef_use (t); 4271 4272 return t; 4273 } 4274 4275 /* Resolve `CONTEXT::template NAME'. Returns a TEMPLATE_DECL if the name 4276 can be resolved or an UNBOUND_CLASS_TEMPLATE, unless an error occurs, 4277 in which case error_mark_node is returned. 4278 4279 If PARM_LIST is non-NULL, also make sure that the template parameter 4280 list of TEMPLATE_DECL matches. 4281 4282 If COMPLAIN zero, don't complain about any errors that occur. */ 4283 4284 tree 4285 make_unbound_class_template (tree context, tree name, tree parm_list, 4286 tsubst_flags_t complain) 4287 { 4288 if (TYPE_P (name)) 4289 name = TYPE_IDENTIFIER (name); 4290 else if (DECL_P (name)) 4291 name = DECL_NAME (name); 4292 gcc_assert (identifier_p (name)); 4293 4294 if (!dependent_type_p (context) 4295 || currently_open_class (context)) 4296 { 4297 tree tmpl = NULL_TREE; 4298 4299 if (MAYBE_CLASS_TYPE_P (context)) 4300 tmpl = lookup_field (context, name, 0, false); 4301 4302 if (tmpl && TREE_CODE (tmpl) == TYPE_DECL) 4303 tmpl = maybe_get_template_decl_from_type_decl (tmpl); 4304 4305 if (!tmpl || !DECL_TYPE_TEMPLATE_P (tmpl)) 4306 { 4307 if (complain & tf_error) 4308 error ("no class template named %q#T in %q#T", name, context); 4309 return error_mark_node; 4310 } 4311 4312 if (parm_list 4313 && !comp_template_parms (DECL_TEMPLATE_PARMS (tmpl), parm_list)) 4314 { 4315 if (complain & tf_error) 4316 { 4317 error ("template parameters do not match template %qD", tmpl); 4318 inform (DECL_SOURCE_LOCATION (tmpl), 4319 "%qD declared here", tmpl); 4320 } 4321 return error_mark_node; 4322 } 4323 4324 if (!perform_or_defer_access_check (TYPE_BINFO (context), tmpl, tmpl, 4325 complain)) 4326 return error_mark_node; 4327 4328 return tmpl; 4329 } 4330 4331 return make_unbound_class_template_raw (context, name, parm_list); 4332 } 4333 4334 /* Build an UNBOUND_CLASS_TEMPLATE. */ 4335 4336 tree 4337 make_unbound_class_template_raw (tree context, tree name, tree parm_list) 4338 { 4339 /* Build the UNBOUND_CLASS_TEMPLATE. */ 4340 tree t = cxx_make_type (UNBOUND_CLASS_TEMPLATE); 4341 TYPE_CONTEXT (t) = FROB_CONTEXT (context); 4342 TREE_TYPE (t) = NULL_TREE; 4343 SET_TYPE_STRUCTURAL_EQUALITY (t); 4344 4345 /* Build the corresponding TEMPLATE_DECL. */ 4346 tree d = build_decl (input_location, TEMPLATE_DECL, name, t); 4347 TYPE_NAME (t) = d; 4348 TYPE_STUB_DECL (t) = d; 4349 DECL_CONTEXT (d) = TYPE_CONTEXT (t); 4350 DECL_ARTIFICIAL (d) = 1; 4351 DECL_TEMPLATE_PARMS (d) = parm_list; 4352 4353 return t; 4354 } 4355 4356 4357 4359 /* Push the declarations of builtin types into the global namespace. 4360 RID_INDEX is the index of the builtin type in the array 4361 RID_POINTERS. NAME is the name used when looking up the builtin 4362 type. TYPE is the _TYPE node for the builtin type. 4363 4364 The calls to set_global_binding below should be 4365 eliminated. Built-in types should not be looked up name; their 4366 names are keywords that the parser can recognize. However, there 4367 is code in c-common.cc that uses identifier_global_value to look up 4368 built-in types by name. */ 4369 4370 void 4371 record_builtin_type (enum rid rid_index, 4372 const char* name, 4373 tree type) 4374 { 4375 tree decl = NULL_TREE; 4376 4377 if (name) 4378 { 4379 tree tname = get_identifier (name); 4380 tree tdecl = build_decl (BUILTINS_LOCATION, TYPE_DECL, tname, type); 4381 DECL_ARTIFICIAL (tdecl) = 1; 4382 set_global_binding (tdecl); 4383 decl = tdecl; 4384 } 4385 4386 if ((int) rid_index < (int) RID_MAX) 4387 if (tree rname = ridpointers[(int) rid_index]) 4388 if (!decl || DECL_NAME (decl) != rname) 4389 { 4390 tree rdecl = build_decl (BUILTINS_LOCATION, TYPE_DECL, rname, type); 4391 DECL_ARTIFICIAL (rdecl) = 1; 4392 set_global_binding (rdecl); 4393 if (!decl) 4394 decl = rdecl; 4395 } 4396 4397 if (decl) 4398 { 4399 if (!TYPE_NAME (type)) 4400 TYPE_NAME (type) = decl; 4401 debug_hooks->type_decl (decl, 0); 4402 } 4403 } 4404 4405 /* Push a type into the namespace so that the back ends ignore it. */ 4406 4407 static void 4408 record_unknown_type (tree type, const char* name) 4409 { 4410 tree decl = pushdecl (build_decl (UNKNOWN_LOCATION, 4411 TYPE_DECL, get_identifier (name), type)); 4412 /* Make sure the "unknown type" typedecl gets ignored for debug info. */ 4413 DECL_IGNORED_P (decl) = 1; 4414 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1; 4415 TYPE_SIZE (type) = TYPE_SIZE (void_type_node); 4416 SET_TYPE_ALIGN (type, 1); 4417 TYPE_USER_ALIGN (type) = 0; 4418 SET_TYPE_MODE (type, TYPE_MODE (void_type_node)); 4419 } 4420 4421 /* Create all the predefined identifiers. */ 4422 4423 static void 4424 initialize_predefined_identifiers (void) 4425 { 4426 struct predefined_identifier 4427 { 4428 const char *name; /* Name. */ 4429 tree *node; /* Node to store it in. */ 4430 cp_identifier_kind kind; /* Kind of identifier. */ 4431 }; 4432 4433 /* A table of identifiers to create at startup. */ 4434 static const predefined_identifier predefined_identifiers[] = { 4435 {"C++", &lang_name_cplusplus, cik_normal}, 4436 {"C", &lang_name_c, cik_normal}, 4437 /* Some of these names have a trailing space so that it is 4438 impossible for them to conflict with names written by users. */ 4439 {"__ct ", &ctor_identifier, cik_ctor}, 4440 {"__ct_base ", &base_ctor_identifier, cik_ctor}, 4441 {"__ct_comp ", &complete_ctor_identifier, cik_ctor}, 4442 {"__dt ", &dtor_identifier, cik_dtor}, 4443 {"__dt_base ", &base_dtor_identifier, cik_dtor}, 4444 {"__dt_comp ", &complete_dtor_identifier, cik_dtor}, 4445 {"__dt_del ", &deleting_dtor_identifier, cik_dtor}, 4446 {"__conv_op ", &conv_op_identifier, cik_conv_op}, 4447 {"__in_chrg", &in_charge_identifier, cik_normal}, 4448 {"__as_base ", &as_base_identifier, cik_normal}, 4449 {"this", &this_identifier, cik_normal}, 4450 {"__delta", &delta_identifier, cik_normal}, 4451 {"__pfn", &pfn_identifier, cik_normal}, 4452 {"_vptr", &vptr_identifier, cik_normal}, 4453 {"__vtt_parm", &vtt_parm_identifier, cik_normal}, 4454 {"::", &global_identifier, cik_normal}, 4455 /* The demangler expects anonymous namespaces to be called 4456 something starting with '_GLOBAL__N_'. It no longer needs 4457 to be unique to the TU. */ 4458 {"_GLOBAL__N_1", &anon_identifier, cik_normal}, 4459 {"auto", &auto_identifier, cik_normal}, 4460 {"decltype(auto)", &decltype_auto_identifier, cik_normal}, 4461 {"initializer_list", &init_list_identifier, cik_normal}, 4462 {"__for_range ", &for_range__identifier, cik_normal}, 4463 {"__for_begin ", &for_begin__identifier, cik_normal}, 4464 {"__for_end ", &for_end__identifier, cik_normal}, 4465 {"__for_range", &for_range_identifier, cik_normal}, 4466 {"__for_begin", &for_begin_identifier, cik_normal}, 4467 {"__for_end", &for_end_identifier, cik_normal}, 4468 {"abi_tag", &abi_tag_identifier, cik_normal}, 4469 {"aligned", &aligned_identifier, cik_normal}, 4470 {"begin", &begin_identifier, cik_normal}, 4471 {"end", &end_identifier, cik_normal}, 4472 {"get", &get__identifier, cik_normal}, 4473 {"gnu", &gnu_identifier, cik_normal}, 4474 {"tuple_element", &tuple_element_identifier, cik_normal}, 4475 {"tuple_size", &tuple_size_identifier, cik_normal}, 4476 {"type", &type_identifier, cik_normal}, 4477 {"value", &value_identifier, cik_normal}, 4478 {"_FUN", &fun_identifier, cik_normal}, 4479 {"__closure", &closure_identifier, cik_normal}, 4480 {"heap uninit", &heap_uninit_identifier, cik_normal}, 4481 {"heap ", &heap_identifier, cik_normal}, 4482 {"heap deleted", &heap_deleted_identifier, cik_normal}, 4483 {"heap [] uninit", &heap_vec_uninit_identifier, cik_normal}, 4484 {"heap []", &heap_vec_identifier, cik_normal}, 4485 {"omp", &omp_identifier, cik_normal}, 4486 {NULL, NULL, cik_normal} 4487 }; 4488 4489 for (const predefined_identifier *pid = predefined_identifiers; 4490 pid->name; ++pid) 4491 { 4492 *pid->node = get_identifier (pid->name); 4493 /* Some of these identifiers already have a special kind. */ 4494 if (pid->kind != cik_normal) 4495 set_identifier_kind (*pid->node, pid->kind); 4496 } 4497 } 4498 4499 /* Create the predefined scalar types of C, 4500 and some nodes representing standard constants (0, 1, (void *)0). 4501 Initialize the global binding level. 4502 Make definitions for built-in primitive functions. */ 4503 4504 void 4505 cxx_init_decl_processing (void) 4506 { 4507 tree void_ftype; 4508 tree void_ftype_ptr; 4509 4510 /* Create all the identifiers we need. */ 4511 initialize_predefined_identifiers (); 4512 4513 /* Create the global variables. */ 4514 push_to_top_level (); 4515 4516 current_function_decl = NULL_TREE; 4517 current_binding_level = NULL; 4518 /* Enter the global namespace. */ 4519 gcc_assert (global_namespace == NULL_TREE); 4520 global_namespace = build_lang_decl (NAMESPACE_DECL, global_identifier, 4521 void_type_node); 4522 TREE_PUBLIC (global_namespace) = true; 4523 DECL_MODULE_EXPORT_P (global_namespace) = true; 4524 DECL_CONTEXT (global_namespace) 4525 = build_translation_unit_decl (get_identifier (main_input_filename)); 4526 /* Remember whether we want the empty class passing ABI change warning 4527 in this TU. */ 4528 TRANSLATION_UNIT_WARN_EMPTY_P (DECL_CONTEXT (global_namespace)) 4529 = warn_abi && abi_version_crosses (12); 4530 debug_hooks->register_main_translation_unit 4531 (DECL_CONTEXT (global_namespace)); 4532 begin_scope (sk_namespace, global_namespace); 4533 current_namespace = global_namespace; 4534 4535 if (flag_visibility_ms_compat) 4536 default_visibility = VISIBILITY_HIDDEN; 4537 4538 /* Initially, C. */ 4539 current_lang_name = lang_name_c; 4540 4541 /* Create the `std' namespace. */ 4542 push_namespace (get_identifier ("std")); 4543 std_node = current_namespace; 4544 pop_namespace (); 4545 4546 flag_noexcept_type = (cxx_dialect >= cxx17); 4547 4548 c_common_nodes_and_builtins (); 4549 4550 tree bool_ftype = build_function_type_list (boolean_type_node, NULL_TREE); 4551 tree decl 4552 = add_builtin_function ("__builtin_is_constant_evaluated", 4553 bool_ftype, CP_BUILT_IN_IS_CONSTANT_EVALUATED, 4554 BUILT_IN_FRONTEND, NULL, NULL_TREE); 4555 set_call_expr_flags (decl, ECF_CONST | ECF_NOTHROW | ECF_LEAF); 4556 4557 tree cptr_ftype = build_function_type_list (const_ptr_type_node, NULL_TREE); 4558 decl = add_builtin_function ("__builtin_source_location", 4559 cptr_ftype, CP_BUILT_IN_SOURCE_LOCATION, 4560 BUILT_IN_FRONTEND, NULL, NULL_TREE); 4561 set_call_expr_flags (decl, ECF_CONST | ECF_NOTHROW | ECF_LEAF); 4562 4563 tree bool_vaftype = build_varargs_function_type_list (boolean_type_node, 4564 NULL_TREE); 4565 decl 4566 = add_builtin_function ("__builtin_is_corresponding_member", 4567 bool_vaftype, 4568 CP_BUILT_IN_IS_CORRESPONDING_MEMBER, 4569 BUILT_IN_FRONTEND, NULL, NULL_TREE); 4570 set_call_expr_flags (decl, ECF_CONST | ECF_NOTHROW | ECF_LEAF); 4571 4572 decl 4573 = add_builtin_function ("__builtin_is_pointer_interconvertible_with_class", 4574 bool_vaftype, 4575 CP_BUILT_IN_IS_POINTER_INTERCONVERTIBLE_WITH_CLASS, 4576 BUILT_IN_FRONTEND, NULL, NULL_TREE); 4577 set_call_expr_flags (decl, ECF_CONST | ECF_NOTHROW | ECF_LEAF); 4578 4579 integer_two_node = build_int_cst (NULL_TREE, 2); 4580 4581 /* Guess at the initial static decls size. */ 4582 vec_alloc (static_decls, 500); 4583 4584 /* ... and keyed classes. */ 4585 vec_alloc (keyed_classes, 100); 4586 4587 record_builtin_type (RID_BOOL, "bool", boolean_type_node); 4588 truthvalue_type_node = boolean_type_node; 4589 truthvalue_false_node = boolean_false_node; 4590 truthvalue_true_node = boolean_true_node; 4591 4592 empty_except_spec = build_tree_list (NULL_TREE, NULL_TREE); 4593 noexcept_true_spec = build_tree_list (boolean_true_node, NULL_TREE); 4594 noexcept_false_spec = build_tree_list (boolean_false_node, NULL_TREE); 4595 noexcept_deferred_spec = build_tree_list (make_node (DEFERRED_NOEXCEPT), 4596 NULL_TREE); 4597 4598 #if 0 4599 record_builtin_type (RID_MAX, NULL, string_type_node); 4600 #endif 4601 4602 delta_type_node = ptrdiff_type_node; 4603 vtable_index_type = ptrdiff_type_node; 4604 4605 vtt_parm_type = build_pointer_type (const_ptr_type_node); 4606 void_ftype = build_function_type_list (void_type_node, NULL_TREE); 4607 void_ftype_ptr = build_function_type_list (void_type_node, 4608 ptr_type_node, NULL_TREE); 4609 void_ftype_ptr 4610 = build_exception_variant (void_ftype_ptr, empty_except_spec); 4611 4612 /* Create the conversion operator marker. This operator's DECL_NAME 4613 is in the identifier table, so we can use identifier equality to 4614 find it. */ 4615 conv_op_marker = build_lang_decl (FUNCTION_DECL, conv_op_identifier, 4616 void_ftype); 4617 4618 /* C++ extensions */ 4619 4620 unknown_type_node = make_node (LANG_TYPE); 4621 record_unknown_type (unknown_type_node, "unknown type"); 4622 4623 /* Indirecting an UNKNOWN_TYPE node yields an UNKNOWN_TYPE node. */ 4624 TREE_TYPE (unknown_type_node) = unknown_type_node; 4625 4626 /* Looking up TYPE_POINTER_TO and TYPE_REFERENCE_TO yield the same 4627 result. */ 4628 TYPE_POINTER_TO (unknown_type_node) = unknown_type_node; 4629 TYPE_REFERENCE_TO (unknown_type_node) = unknown_type_node; 4630 4631 init_list_type_node = make_node (LANG_TYPE); 4632 record_unknown_type (init_list_type_node, "init list"); 4633 4634 /* Used when parsing to distinguish parameter-lists () and (void). */ 4635 explicit_void_list_node = build_void_list_node (); 4636 4637 { 4638 /* Make sure we get a unique function type, so we can give 4639 its pointer type a name. (This wins for gdb.) */ 4640 tree vfunc_type = make_node (FUNCTION_TYPE); 4641 TREE_TYPE (vfunc_type) = integer_type_node; 4642 TYPE_ARG_TYPES (vfunc_type) = NULL_TREE; 4643 layout_type (vfunc_type); 4644 4645 vtable_entry_type = build_pointer_type (vfunc_type); 4646 } 4647 record_builtin_type (RID_MAX, "__vtbl_ptr_type", vtable_entry_type); 4648 4649 vtbl_type_node 4650 = build_cplus_array_type (vtable_entry_type, NULL_TREE); 4651 layout_type (vtbl_type_node); 4652 vtbl_type_node = cp_build_qualified_type (vtbl_type_node, TYPE_QUAL_CONST); 4653 record_builtin_type (RID_MAX, NULL, vtbl_type_node); 4654 vtbl_ptr_type_node = build_pointer_type (vtable_entry_type); 4655 layout_type (vtbl_ptr_type_node); 4656 record_builtin_type (RID_MAX, NULL, vtbl_ptr_type_node); 4657 4658 push_namespace (get_identifier ("__cxxabiv1")); 4659 abi_node = current_namespace; 4660 pop_namespace (); 4661 4662 any_targ_node = make_node (LANG_TYPE); 4663 record_unknown_type (any_targ_node, "any type"); 4664 4665 /* Now, C++. */ 4666 current_lang_name = lang_name_cplusplus; 4667 4668 if (aligned_new_threshold > 1 4669 && !pow2p_hwi (aligned_new_threshold)) 4670 { 4671 error ("%<-faligned-new=%d%> is not a power of two", 4672 aligned_new_threshold); 4673 aligned_new_threshold = 1; 4674 } 4675 if (aligned_new_threshold == -1) 4676 aligned_new_threshold = (cxx_dialect >= cxx17) ? 1 : 0; 4677 if (aligned_new_threshold == 1) 4678 aligned_new_threshold = malloc_alignment () / BITS_PER_UNIT; 4679 4680 { 4681 tree newattrs, extvisattr; 4682 tree newtype, deltype; 4683 tree ptr_ftype_sizetype; 4684 tree new_eh_spec; 4685 4686 ptr_ftype_sizetype 4687 = build_function_type_list (ptr_type_node, size_type_node, NULL_TREE); 4688 if (cxx_dialect == cxx98) 4689 { 4690 tree bad_alloc_id; 4691 tree bad_alloc_type_node; 4692 tree bad_alloc_decl; 4693 4694 push_nested_namespace (std_node); 4695 bad_alloc_id = get_identifier ("bad_alloc"); 4696 bad_alloc_type_node = make_class_type (RECORD_TYPE); 4697 TYPE_CONTEXT (bad_alloc_type_node) = current_namespace; 4698 bad_alloc_decl 4699 = create_implicit_typedef (bad_alloc_id, bad_alloc_type_node); 4700 DECL_CONTEXT (bad_alloc_decl) = current_namespace; 4701 pop_nested_namespace (std_node); 4702 4703 new_eh_spec 4704 = add_exception_specifier (NULL_TREE, bad_alloc_type_node, -1); 4705 } 4706 else 4707 new_eh_spec = noexcept_false_spec; 4708 4709 /* Ensure attribs.cc is initialized. */ 4710 init_attributes (); 4711 4712 extvisattr = build_tree_list (get_identifier ("externally_visible"), 4713 NULL_TREE); 4714 newattrs = tree_cons (get_identifier ("alloc_size"), 4715 build_tree_list (NULL_TREE, integer_one_node), 4716 extvisattr); 4717 newtype = cp_build_type_attribute_variant (ptr_ftype_sizetype, newattrs); 4718 newtype = build_exception_variant (newtype, new_eh_spec); 4719 deltype = cp_build_type_attribute_variant (void_ftype_ptr, extvisattr); 4720 deltype = build_exception_variant (deltype, empty_except_spec); 4721 tree opnew = push_cp_library_fn (NEW_EXPR, newtype, 0); 4722 DECL_IS_MALLOC (opnew) = 1; 4723 DECL_SET_IS_OPERATOR_NEW (opnew, true); 4724 DECL_IS_REPLACEABLE_OPERATOR (opnew) = 1; 4725 opnew = push_cp_library_fn (VEC_NEW_EXPR, newtype, 0); 4726 DECL_IS_MALLOC (opnew) = 1; 4727 DECL_SET_IS_OPERATOR_NEW (opnew, true); 4728 DECL_IS_REPLACEABLE_OPERATOR (opnew) = 1; 4729 tree opdel = push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW); 4730 DECL_SET_IS_OPERATOR_DELETE (opdel, true); 4731 DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1; 4732 opdel = push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW); 4733 DECL_SET_IS_OPERATOR_DELETE (opdel, true); 4734 DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1; 4735 if (flag_sized_deallocation) 4736 { 4737 /* Also push the sized deallocation variants: 4738 void operator delete(void*, std::size_t) throw(); 4739 void operator delete[](void*, std::size_t) throw(); */ 4740 tree void_ftype_ptr_size 4741 = build_function_type_list (void_type_node, ptr_type_node, 4742 size_type_node, NULL_TREE); 4743 deltype = cp_build_type_attribute_variant (void_ftype_ptr_size, 4744 extvisattr); 4745 deltype = build_exception_variant (deltype, empty_except_spec); 4746 opdel = push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW); 4747 DECL_SET_IS_OPERATOR_DELETE (opdel, true); 4748 DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1; 4749 opdel = push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW); 4750 DECL_SET_IS_OPERATOR_DELETE (opdel, true); 4751 DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1; 4752 } 4753 4754 if (aligned_new_threshold) 4755 { 4756 push_nested_namespace (std_node); 4757 tree align_id = get_identifier ("align_val_t"); 4758 align_type_node = start_enum (align_id, NULL_TREE, size_type_node, 4759 NULL_TREE, /*scoped*/true, NULL); 4760 pop_nested_namespace (std_node); 4761 4762 /* operator new (size_t, align_val_t); */ 4763 newtype = build_function_type_list (ptr_type_node, size_type_node, 4764 align_type_node, NULL_TREE); 4765 newtype = cp_build_type_attribute_variant (newtype, newattrs); 4766 newtype = build_exception_variant (newtype, new_eh_spec); 4767 opnew = push_cp_library_fn (NEW_EXPR, newtype, 0); 4768 DECL_IS_MALLOC (opnew) = 1; 4769 DECL_SET_IS_OPERATOR_NEW (opnew, true); 4770 DECL_IS_REPLACEABLE_OPERATOR (opnew) = 1; 4771 opnew = push_cp_library_fn (VEC_NEW_EXPR, newtype, 0); 4772 DECL_IS_MALLOC (opnew) = 1; 4773 DECL_SET_IS_OPERATOR_NEW (opnew, true); 4774 DECL_IS_REPLACEABLE_OPERATOR (opnew) = 1; 4775 4776 /* operator delete (void *, align_val_t); */ 4777 deltype = build_function_type_list (void_type_node, ptr_type_node, 4778 align_type_node, NULL_TREE); 4779 deltype = cp_build_type_attribute_variant (deltype, extvisattr); 4780 deltype = build_exception_variant (deltype, empty_except_spec); 4781 opdel = push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW); 4782 DECL_SET_IS_OPERATOR_DELETE (opdel, true); 4783 DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1; 4784 opdel = push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW); 4785 DECL_SET_IS_OPERATOR_DELETE (opdel, true); 4786 DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1; 4787 4788 if (flag_sized_deallocation) 4789 { 4790 /* operator delete (void *, size_t, align_val_t); */ 4791 deltype = build_function_type_list (void_type_node, ptr_type_node, 4792 size_type_node, align_type_node, 4793 NULL_TREE); 4794 deltype = cp_build_type_attribute_variant (deltype, extvisattr); 4795 deltype = build_exception_variant (deltype, empty_except_spec); 4796 opdel = push_cp_library_fn (DELETE_EXPR, deltype, ECF_NOTHROW); 4797 DECL_SET_IS_OPERATOR_DELETE (opdel, true); 4798 DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1; 4799 opdel = push_cp_library_fn (VEC_DELETE_EXPR, deltype, ECF_NOTHROW); 4800 DECL_SET_IS_OPERATOR_DELETE (opdel, true); 4801 DECL_IS_REPLACEABLE_OPERATOR (opdel) = 1; 4802 } 4803 } 4804 4805 nullptr_type_node = make_node (NULLPTR_TYPE); 4806 TYPE_SIZE (nullptr_type_node) = bitsize_int (GET_MODE_BITSIZE (ptr_mode)); 4807 TYPE_SIZE_UNIT (nullptr_type_node) = size_int (GET_MODE_SIZE (ptr_mode)); 4808 TYPE_UNSIGNED (nullptr_type_node) = 1; 4809 TYPE_PRECISION (nullptr_type_node) = GET_MODE_BITSIZE (ptr_mode); 4810 if (abi_version_at_least (9)) 4811 SET_TYPE_ALIGN (nullptr_type_node, GET_MODE_ALIGNMENT (ptr_mode)); 4812 SET_TYPE_MODE (nullptr_type_node, ptr_mode); 4813 record_builtin_type (RID_MAX, "decltype(nullptr)", nullptr_type_node); 4814 nullptr_node = build_int_cst (nullptr_type_node, 0); 4815 } 4816 4817 if (! supports_one_only ()) 4818 flag_weak = 0; 4819 4820 abort_fndecl 4821 = build_library_fn_ptr ("__cxa_pure_virtual", void_ftype, 4822 ECF_NORETURN | ECF_NOTHROW | ECF_COLD); 4823 if (flag_weak) 4824 /* If no definition is available, resolve references to NULL. */ 4825 declare_weak (abort_fndecl); 4826 4827 /* Perform other language dependent initializations. */ 4828 init_class_processing (); 4829 init_rtti_processing (); 4830 init_template_processing (); 4831 4832 if (flag_exceptions) 4833 init_exception_processing (); 4834 4835 if (modules_p ()) 4836 init_modules (parse_in); 4837 4838 make_fname_decl = cp_make_fname_decl; 4839 start_fname_decls (); 4840 4841 /* Show we use EH for cleanups. */ 4842 if (flag_exceptions) 4843 using_eh_for_cleanups (); 4844 4845 /* Check that the hardware interference sizes are at least 4846 alignof(max_align_t), as required by the standard. */ 4847 const int max_align = max_align_t_align () / BITS_PER_UNIT; 4848 if (OPTION_SET_P (param_destruct_interfere_size)) 4849 { 4850 if (param_destruct_interfere_size < max_align) 4851 error ("%<--param destructive-interference-size=%d%> is less than " 4852 "%d", param_destruct_interfere_size, max_align); 4853 else if (param_destruct_interfere_size < param_l1_cache_line_size) 4854 warning (OPT_Winterference_size, 4855 "%<--param destructive-interference-size=%d%> " 4856 "is less than %<--param l1-cache-line-size=%d%>", 4857 param_destruct_interfere_size, param_l1_cache_line_size); 4858 } 4859 else if (param_destruct_interfere_size) 4860 /* Assume the internal value is OK. */; 4861 else if (param_l1_cache_line_size >= max_align) 4862 param_destruct_interfere_size = param_l1_cache_line_size; 4863 /* else leave it unset. */ 4864 4865 if (OPTION_SET_P (param_construct_interfere_size)) 4866 { 4867 if (param_construct_interfere_size < max_align) 4868 error ("%<--param constructive-interference-size=%d%> is less than " 4869 "%d", param_construct_interfere_size, max_align); 4870 else if (param_construct_interfere_size > param_l1_cache_line_size 4871 && param_l1_cache_line_size >= max_align) 4872 warning (OPT_Winterference_size, 4873 "%<--param constructive-interference-size=%d%> " 4874 "is greater than %<--param l1-cache-line-size=%d%>", 4875 param_construct_interfere_size, param_l1_cache_line_size); 4876 } 4877 else if (param_construct_interfere_size) 4878 /* Assume the internal value is OK. */; 4879 else if (param_l1_cache_line_size >= max_align) 4880 param_construct_interfere_size = param_l1_cache_line_size; 4881 } 4882 4883 /* Enter an abi node in global-module context. returns a cookie to 4884 give to pop_abi_namespace. */ 4885 4886 unsigned 4887 push_abi_namespace (tree node) 4888 { 4889 push_nested_namespace (node); 4890 push_visibility ("default", 2); 4891 unsigned flags = module_kind; 4892 module_kind = 0; 4893 return flags; 4894 } 4895 4896 /* Pop an abi namespace, FLAGS is the cookie push_abi_namespace gave 4897 you. */ 4898 4899 void 4900 pop_abi_namespace (unsigned flags, tree node) 4901 { 4902 module_kind = flags; 4903 pop_visibility (2); 4904 pop_nested_namespace (node); 4905 } 4906 4907 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give 4908 the decl, LOC is the location to give the decl, NAME is the 4909 initialization string and TYPE_DEP indicates whether NAME depended 4910 on the type of the function. We make use of that to detect 4911 __PRETTY_FUNCTION__ inside a template fn. This is being done lazily 4912 at the point of first use, so we mustn't push the decl now. */ 4913 4914 static tree 4915 cp_make_fname_decl (location_t loc, tree id, int type_dep) 4916 { 4917 tree domain = NULL_TREE; 4918 tree init = NULL_TREE; 4919 4920 if (!(type_dep && in_template_function ())) 4921 { 4922 const char *name = NULL; 4923 bool release_name = false; 4924 4925 if (current_function_decl == NULL_TREE) 4926 name = "top level"; 4927 else if (type_dep == 0) 4928 { 4929 /* __FUNCTION__ */ 4930 name = fname_as_string (type_dep); 4931 release_name = true; 4932 } 4933 else 4934 { 4935 /* __PRETTY_FUNCTION__ */ 4936 gcc_checking_assert (type_dep == 1); 4937 name = cxx_printable_name (current_function_decl, 2); 4938 } 4939 4940 size_t length = strlen (name); 4941 domain = build_index_type (size_int (length)); 4942 init = build_string (length + 1, name); 4943 if (release_name) 4944 free (const_cast<char *> (name)); 4945 } 4946 4947 tree type = cp_build_qualified_type (char_type_node, TYPE_QUAL_CONST); 4948 type = build_cplus_array_type (type, domain); 4949 4950 if (init) 4951 TREE_TYPE (init) = type; 4952 else 4953 init = error_mark_node; 4954 4955 tree decl = build_decl (loc, VAR_DECL, id, type); 4956 4957 TREE_READONLY (decl) = 1; 4958 DECL_ARTIFICIAL (decl) = 1; 4959 DECL_DECLARED_CONSTEXPR_P (decl) = 1; 4960 TREE_STATIC (decl) = 1; 4961 4962 TREE_USED (decl) = 1; 4963 4964 SET_DECL_VALUE_EXPR (decl, init); 4965 DECL_HAS_VALUE_EXPR_P (decl) = 1; 4966 /* For decl_constant_var_p. */ 4967 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1; 4968 4969 if (current_function_decl) 4970 { 4971 DECL_CONTEXT (decl) = current_function_decl; 4972 decl = pushdecl_outermost_localscope (decl); 4973 if (decl != error_mark_node) 4974 add_decl_expr (decl); 4975 } 4976 else 4977 { 4978 DECL_THIS_STATIC (decl) = true; 4979 decl = pushdecl_top_level_and_finish (decl, NULL_TREE); 4980 } 4981 4982 return decl; 4983 } 4984 4985 /* Install DECL as a builtin function at current global scope. Return 4986 the new decl (if we found an existing version). Also installs it 4987 into ::std, if it's not '_*'. */ 4988 4989 tree 4990 cxx_builtin_function (tree decl) 4991 { 4992 retrofit_lang_decl (decl); 4993 4994 DECL_ARTIFICIAL (decl) = 1; 4995 SET_DECL_LANGUAGE (decl, lang_c); 4996 /* Runtime library routines are, by definition, available in an 4997 external shared object. */ 4998 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT; 4999 DECL_VISIBILITY_SPECIFIED (decl) = 1; 5000 5001 tree id = DECL_NAME (decl); 5002 const char *name = IDENTIFIER_POINTER (id); 5003 bool hiding = false; 5004 if (name[0] != '_' || name[1] != '_') 5005 /* In the user's namespace, it must be declared before use. */ 5006 hiding = true; 5007 else if (IDENTIFIER_LENGTH (id) > strlen ("___chk") 5008 && !startswith (name + 2, "builtin_") 5009 && 0 == memcmp (name + IDENTIFIER_LENGTH (id) - strlen ("_chk"), 5010 "_chk", strlen ("_chk") + 1)) 5011 /* Treat __*_chk fortification functions as anticipated as well, 5012 unless they are __builtin_*_chk. */ 5013 hiding = true; 5014 5015 /* All builtins that don't begin with an '_' should additionally 5016 go in the 'std' namespace. */ 5017 if (name[0] != '_') 5018 { 5019 tree std_decl = copy_decl (decl); 5020 5021 push_nested_namespace (std_node); 5022 DECL_CONTEXT (std_decl) = FROB_CONTEXT (std_node); 5023 pushdecl (std_decl, hiding); 5024 pop_nested_namespace (std_node); 5025 } 5026 5027 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace); 5028 decl = pushdecl (decl, hiding); 5029 5030 return decl; 5031 } 5032 5033 /* Like cxx_builtin_function, but guarantee the function is added to the global 5034 scope. This is to allow function specific options to add new machine 5035 dependent builtins when the target ISA changes via attribute((target(...))) 5036 which saves space on program startup if the program does not use non-generic 5037 ISAs. */ 5038 5039 tree 5040 cxx_builtin_function_ext_scope (tree decl) 5041 { 5042 push_nested_namespace (global_namespace); 5043 decl = cxx_builtin_function (decl); 5044 pop_nested_namespace (global_namespace); 5045 5046 return decl; 5047 } 5048 5049 /* Implement LANG_HOOKS_SIMULATE_BUILTIN_FUNCTION_DECL. */ 5050 5051 tree 5052 cxx_simulate_builtin_function_decl (tree decl) 5053 { 5054 retrofit_lang_decl (decl); 5055 5056 DECL_ARTIFICIAL (decl) = 1; 5057 SET_DECL_LANGUAGE (decl, lang_cplusplus); 5058 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace); 5059 return pushdecl (decl); 5060 } 5061 5062 /* Generate a FUNCTION_DECL with the typical flags for a runtime library 5063 function. Not called directly. */ 5064 5065 static tree 5066 build_library_fn (tree name, enum tree_code operator_code, tree type, 5067 int ecf_flags) 5068 { 5069 tree fn = build_lang_decl (FUNCTION_DECL, name, type); 5070 DECL_EXTERNAL (fn) = 1; 5071 TREE_PUBLIC (fn) = 1; 5072 DECL_ARTIFICIAL (fn) = 1; 5073 DECL_OVERLOADED_OPERATOR_CODE_RAW (fn) 5074 = OVL_OP_INFO (false, operator_code)->ovl_op_code; 5075 SET_DECL_LANGUAGE (fn, lang_c); 5076 /* Runtime library routines are, by definition, available in an 5077 external shared object. */ 5078 DECL_VISIBILITY (fn) = VISIBILITY_DEFAULT; 5079 DECL_VISIBILITY_SPECIFIED (fn) = 1; 5080 set_call_expr_flags (fn, ecf_flags); 5081 return fn; 5082 } 5083 5084 /* Returns the _DECL for a library function with C++ linkage. */ 5085 5086 static tree 5087 build_cp_library_fn (tree name, enum tree_code operator_code, tree type, 5088 int ecf_flags) 5089 { 5090 tree fn = build_library_fn (name, operator_code, type, ecf_flags); 5091 DECL_CONTEXT (fn) = FROB_CONTEXT (current_namespace); 5092 SET_DECL_LANGUAGE (fn, lang_cplusplus); 5093 return fn; 5094 } 5095 5096 /* Like build_library_fn, but takes a C string instead of an 5097 IDENTIFIER_NODE. */ 5098 5099 tree 5100 build_library_fn_ptr (const char* name, tree type, int ecf_flags) 5101 { 5102 return build_library_fn (get_identifier (name), ERROR_MARK, type, ecf_flags); 5103 } 5104 5105 /* Like build_cp_library_fn, but takes a C string instead of an 5106 IDENTIFIER_NODE. */ 5107 5108 tree 5109 build_cp_library_fn_ptr (const char* name, tree type, int ecf_flags) 5110 { 5111 return build_cp_library_fn (get_identifier (name), ERROR_MARK, type, 5112 ecf_flags); 5113 } 5114 5115 /* Like build_library_fn, but also pushes the function so that we will 5116 be able to find it via get_global_binding. Also, the function 5117 may throw exceptions listed in RAISES. */ 5118 5119 tree 5120 push_library_fn (tree name, tree type, tree raises, int ecf_flags) 5121 { 5122 if (raises) 5123 type = build_exception_variant (type, raises); 5124 5125 tree fn = build_library_fn (name, ERROR_MARK, type, ecf_flags); 5126 return pushdecl_top_level (fn); 5127 } 5128 5129 /* Like build_cp_library_fn, but also pushes the function so that it 5130 will be found by normal lookup. */ 5131 5132 static tree 5133 push_cp_library_fn (enum tree_code operator_code, tree type, 5134 int ecf_flags) 5135 { 5136 tree fn = build_cp_library_fn (ovl_op_identifier (false, operator_code), 5137 operator_code, type, ecf_flags); 5138 pushdecl (fn); 5139 if (flag_tm) 5140 apply_tm_attr (fn, get_identifier ("transaction_safe")); 5141 return fn; 5142 } 5143 5144 /* Like push_library_fn, but also note that this function throws 5145 and does not return. Used for __throw_foo and the like. */ 5146 5147 tree 5148 push_throw_library_fn (tree name, tree type) 5149 { 5150 tree fn = push_library_fn (name, type, NULL_TREE, ECF_NORETURN | ECF_COLD); 5151 return fn; 5152 } 5153 5154 /* When we call finish_struct for an anonymous union, we create 5156 default copy constructors and such. But, an anonymous union 5157 shouldn't have such things; this function undoes the damage to the 5158 anonymous union type T. 5159 5160 (The reason that we create the synthesized methods is that we don't 5161 distinguish `union { int i; }' from `typedef union { int i; } U'. 5162 The first is an anonymous union; the second is just an ordinary 5163 union type.) */ 5164 5165 void 5166 fixup_anonymous_aggr (tree t) 5167 { 5168 /* Wipe out memory of synthesized methods. */ 5169 TYPE_HAS_USER_CONSTRUCTOR (t) = 0; 5170 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 0; 5171 TYPE_HAS_COPY_CTOR (t) = 0; 5172 TYPE_HAS_CONST_COPY_CTOR (t) = 0; 5173 TYPE_HAS_COPY_ASSIGN (t) = 0; 5174 TYPE_HAS_CONST_COPY_ASSIGN (t) = 0; 5175 5176 /* Splice the implicitly generated functions out of TYPE_FIELDS and diagnose 5177 invalid members. */ 5178 for (tree probe, *prev_p = &TYPE_FIELDS (t); (probe = *prev_p);) 5179 { 5180 if (TREE_CODE (probe) == FUNCTION_DECL && DECL_ARTIFICIAL (probe)) 5181 *prev_p = DECL_CHAIN (probe); 5182 else 5183 prev_p = &DECL_CHAIN (probe); 5184 5185 if (DECL_ARTIFICIAL (probe) 5186 && (!DECL_IMPLICIT_TYPEDEF_P (probe) 5187 || TYPE_ANON_P (TREE_TYPE (probe)))) 5188 continue; 5189 5190 if (TREE_CODE (probe) != FIELD_DECL 5191 || (TREE_PRIVATE (probe) || TREE_PROTECTED (probe))) 5192 { 5193 /* We already complained about static data members in 5194 finish_static_data_member_decl. */ 5195 if (!VAR_P (probe)) 5196 { 5197 auto_diagnostic_group d; 5198 if (permerror (DECL_SOURCE_LOCATION (probe), 5199 TREE_CODE (t) == UNION_TYPE 5200 ? "%q#D invalid; an anonymous union may " 5201 "only have public non-static data members" 5202 : "%q#D invalid; an anonymous struct may " 5203 "only have public non-static data members", probe)) 5204 { 5205 static bool hint; 5206 if (flag_permissive && !hint) 5207 { 5208 hint = true; 5209 inform (DECL_SOURCE_LOCATION (probe), 5210 "this flexibility is deprecated and will be " 5211 "removed"); 5212 } 5213 } 5214 } 5215 } 5216 } 5217 5218 /* Splice all functions out of CLASSTYPE_MEMBER_VEC. */ 5219 vec<tree,va_gc>* vec = CLASSTYPE_MEMBER_VEC (t); 5220 unsigned store = 0; 5221 for (tree elt : vec) 5222 if (!is_overloaded_fn (elt)) 5223 (*vec)[store++] = elt; 5224 vec_safe_truncate (vec, store); 5225 5226 /* Wipe RTTI info. */ 5227 CLASSTYPE_TYPEINFO_VAR (t) = NULL_TREE; 5228 5229 /* Anonymous aggregates cannot have fields with ctors, dtors or complex 5230 assignment operators (because they cannot have these methods themselves). 5231 For anonymous unions this is already checked because they are not allowed 5232 in any union, otherwise we have to check it. */ 5233 if (TREE_CODE (t) != UNION_TYPE) 5234 { 5235 tree field, type; 5236 5237 if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t))) 5238 { 5239 error_at (location_of (t), "anonymous struct with base classes"); 5240 /* Avoid ICE after error on anon-struct9.C. */ 5241 TYPE_NEEDS_CONSTRUCTING (t) = false; 5242 } 5243 5244 for (field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field)) 5245 if (TREE_CODE (field) == FIELD_DECL) 5246 { 5247 type = TREE_TYPE (field); 5248 if (CLASS_TYPE_P (type)) 5249 { 5250 if (TYPE_NEEDS_CONSTRUCTING (type)) 5251 error ("member %q+#D with constructor not allowed " 5252 "in anonymous aggregate", field); 5253 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)) 5254 error ("member %q+#D with destructor not allowed " 5255 "in anonymous aggregate", field); 5256 if (TYPE_HAS_COMPLEX_COPY_ASSIGN (type)) 5257 error ("member %q+#D with copy assignment operator " 5258 "not allowed in anonymous aggregate", field); 5259 } 5260 } 5261 } 5262 } 5263 5264 /* Warn for an attribute located at LOCATION that appertains to the 5265 class type CLASS_TYPE that has not been properly placed after its 5266 class-key, in it class-specifier. */ 5267 5268 void 5269 warn_misplaced_attr_for_class_type (location_t location, 5270 tree class_type) 5271 { 5272 gcc_assert (OVERLOAD_TYPE_P (class_type)); 5273 5274 auto_diagnostic_group d; 5275 if (warning_at (location, OPT_Wattributes, 5276 "attribute ignored in declaration " 5277 "of %q#T", class_type)) 5278 inform (location, 5279 "attribute for %q#T must follow the %qs keyword", 5280 class_type, class_key_or_enum_as_string (class_type)); 5281 } 5282 5283 /* Returns the cv-qualifiers that apply to the type specified 5284 by the DECLSPECS. */ 5285 5286 static int 5287 get_type_quals (const cp_decl_specifier_seq *declspecs) 5288 { 5289 int type_quals = TYPE_UNQUALIFIED; 5290 5291 if (decl_spec_seq_has_spec_p (declspecs, ds_const)) 5292 type_quals |= TYPE_QUAL_CONST; 5293 if (decl_spec_seq_has_spec_p (declspecs, ds_volatile)) 5294 type_quals |= TYPE_QUAL_VOLATILE; 5295 if (decl_spec_seq_has_spec_p (declspecs, ds_restrict)) 5296 type_quals |= TYPE_QUAL_RESTRICT; 5297 5298 return type_quals; 5299 } 5300 5301 /* Make sure that a declaration with no declarator is well-formed, i.e. 5302 just declares a tagged type or anonymous union. 5303 5304 Returns the type declared; or NULL_TREE if none. */ 5305 5306 tree 5307 check_tag_decl (cp_decl_specifier_seq *declspecs, 5308 bool explicit_type_instantiation_p) 5309 { 5310 int saw_friend = decl_spec_seq_has_spec_p (declspecs, ds_friend); 5311 int saw_typedef = decl_spec_seq_has_spec_p (declspecs, ds_typedef); 5312 /* If a class, struct, or enum type is declared by the DECLSPECS 5313 (i.e, if a class-specifier, enum-specifier, or non-typename 5314 elaborated-type-specifier appears in the DECLSPECS), 5315 DECLARED_TYPE is set to the corresponding type. */ 5316 tree declared_type = NULL_TREE; 5317 bool error_p = false; 5318 5319 if (declspecs->multiple_types_p) 5320 error_at (smallest_type_location (declspecs), 5321 "multiple types in one declaration"); 5322 else if (declspecs->redefined_builtin_type) 5323 { 5324 location_t loc = declspecs->locations[ds_redefined_builtin_type_spec]; 5325 if (!in_system_header_at (loc)) 5326 permerror (loc, "redeclaration of C++ built-in type %qT", 5327 declspecs->redefined_builtin_type); 5328 return NULL_TREE; 5329 } 5330 5331 if (declspecs->type 5332 && TYPE_P (declspecs->type) 5333 && ((TREE_CODE (declspecs->type) != TYPENAME_TYPE 5334 && MAYBE_CLASS_TYPE_P (declspecs->type)) 5335 || TREE_CODE (declspecs->type) == ENUMERAL_TYPE)) 5336 declared_type = declspecs->type; 5337 else if (declspecs->type == error_mark_node) 5338 error_p = true; 5339 5340 if (type_uses_auto (declared_type)) 5341 { 5342 error_at (declspecs->locations[ds_type_spec], 5343 "%<auto%> can only be specified for variables " 5344 "or function declarations"); 5345 return error_mark_node; 5346 } 5347 5348 if (declared_type && !OVERLOAD_TYPE_P (declared_type)) 5349 declared_type = NULL_TREE; 5350 5351 if (!declared_type && !saw_friend && !error_p) 5352 permerror (input_location, "declaration does not declare anything"); 5353 /* Check for an anonymous union. */ 5354 else if (declared_type && RECORD_OR_UNION_CODE_P (TREE_CODE (declared_type)) 5355 && TYPE_UNNAMED_P (declared_type)) 5356 { 5357 /* 7/3 In a simple-declaration, the optional init-declarator-list 5358 can be omitted only when declaring a class (clause 9) or 5359 enumeration (7.2), that is, when the decl-specifier-seq contains 5360 either a class-specifier, an elaborated-type-specifier with 5361 a class-key (9.1), or an enum-specifier. In these cases and 5362 whenever a class-specifier or enum-specifier is present in the 5363 decl-specifier-seq, the identifiers in these specifiers are among 5364 the names being declared by the declaration (as class-name, 5365 enum-names, or enumerators, depending on the syntax). In such 5366 cases, and except for the declaration of an unnamed bit-field (9.6), 5367 the decl-specifier-seq shall introduce one or more names into the 5368 program, or shall redeclare a name introduced by a previous 5369 declaration. [Example: 5370 enum { }; // ill-formed 5371 typedef class { }; // ill-formed 5372 --end example] */ 5373 if (saw_typedef) 5374 { 5375 error_at (declspecs->locations[ds_typedef], 5376 "missing type-name in typedef-declaration"); 5377 return NULL_TREE; 5378 } 5379 /* Anonymous unions are objects, so they can have specifiers. */; 5380 SET_ANON_AGGR_TYPE_P (declared_type); 5381 5382 if (TREE_CODE (declared_type) != UNION_TYPE) 5383 pedwarn (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (declared_type)), 5384 OPT_Wpedantic, "ISO C++ prohibits anonymous structs"); 5385 } 5386 5387 else 5388 { 5389 if (decl_spec_seq_has_spec_p (declspecs, ds_inline)) 5390 error_at (declspecs->locations[ds_inline], 5391 "%<inline%> can only be specified for functions"); 5392 else if (decl_spec_seq_has_spec_p (declspecs, ds_virtual)) 5393 error_at (declspecs->locations[ds_virtual], 5394 "%<virtual%> can only be specified for functions"); 5395 else if (saw_friend 5396 && (!current_class_type 5397 || current_scope () != current_class_type)) 5398 error_at (declspecs->locations[ds_friend], 5399 "%<friend%> can only be specified inside a class"); 5400 else if (decl_spec_seq_has_spec_p (declspecs, ds_explicit)) 5401 error_at (declspecs->locations[ds_explicit], 5402 "%<explicit%> can only be specified for constructors"); 5403 else if (declspecs->storage_class) 5404 error_at (declspecs->locations[ds_storage_class], 5405 "a storage class can only be specified for objects " 5406 "and functions"); 5407 else if (decl_spec_seq_has_spec_p (declspecs, ds_const)) 5408 error_at (declspecs->locations[ds_const], 5409 "%<const%> can only be specified for objects and " 5410 "functions"); 5411 else if (decl_spec_seq_has_spec_p (declspecs, ds_volatile)) 5412 error_at (declspecs->locations[ds_volatile], 5413 "%<volatile%> can only be specified for objects and " 5414 "functions"); 5415 else if (decl_spec_seq_has_spec_p (declspecs, ds_restrict)) 5416 error_at (declspecs->locations[ds_restrict], 5417 "%<__restrict%> can only be specified for objects and " 5418 "functions"); 5419 else if (decl_spec_seq_has_spec_p (declspecs, ds_thread)) 5420 error_at (declspecs->locations[ds_thread], 5421 "%<__thread%> can only be specified for objects " 5422 "and functions"); 5423 else if (saw_typedef) 5424 warning_at (declspecs->locations[ds_typedef], 0, 5425 "%<typedef%> was ignored in this declaration"); 5426 else if (decl_spec_seq_has_spec_p (declspecs, ds_constexpr)) 5427 error_at (declspecs->locations[ds_constexpr], 5428 "%qs cannot be used for type declarations", "constexpr"); 5429 else if (decl_spec_seq_has_spec_p (declspecs, ds_constinit)) 5430 error_at (declspecs->locations[ds_constinit], 5431 "%qs cannot be used for type declarations", "constinit"); 5432 else if (decl_spec_seq_has_spec_p (declspecs, ds_consteval)) 5433 error_at (declspecs->locations[ds_consteval], 5434 "%qs cannot be used for type declarations", "consteval"); 5435 } 5436 5437 if (declspecs->attributes && warn_attributes && declared_type) 5438 { 5439 location_t loc; 5440 if (!CLASS_TYPE_P (declared_type) 5441 || !CLASSTYPE_TEMPLATE_INSTANTIATION (declared_type)) 5442 /* For a non-template class, use the name location. */ 5443 loc = location_of (declared_type); 5444 else 5445 /* For a template class (an explicit instantiation), use the 5446 current location. */ 5447 loc = input_location; 5448 5449 if (explicit_type_instantiation_p) 5450 /* [dcl.attr.grammar]/4: 5451 5452 No attribute-specifier-seq shall appertain to an explicit 5453 instantiation. */ 5454 { 5455 if (warning_at (loc, OPT_Wattributes, 5456 "attribute ignored in explicit instantiation %q#T", 5457 declared_type)) 5458 inform (loc, 5459 "no attribute can be applied to " 5460 "an explicit instantiation"); 5461 } 5462 else 5463 warn_misplaced_attr_for_class_type (loc, declared_type); 5464 } 5465 5466 return declared_type; 5467 } 5468 5469 /* Called when a declaration is seen that contains no names to declare. 5470 If its type is a reference to a structure, union or enum inherited 5471 from a containing scope, shadow that tag name for the current scope 5472 with a forward reference. 5473 If its type defines a new named structure or union 5474 or defines an enum, it is valid but we need not do anything here. 5475 Otherwise, it is an error. 5476 5477 C++: may have to grok the declspecs to learn about static, 5478 complain for anonymous unions. 5479 5480 Returns the TYPE declared -- or NULL_TREE if none. */ 5481 5482 tree 5483 shadow_tag (cp_decl_specifier_seq *declspecs) 5484 { 5485 tree t = check_tag_decl (declspecs, 5486 /*explicit_type_instantiation_p=*/false); 5487 5488 if (!t) 5489 return NULL_TREE; 5490 5491 t = maybe_process_partial_specialization (t); 5492 if (t == error_mark_node) 5493 return NULL_TREE; 5494 5495 /* This is where the variables in an anonymous union are 5496 declared. An anonymous union declaration looks like: 5497 union { ... } ; 5498 because there is no declarator after the union, the parser 5499 sends that declaration here. */ 5500 if (ANON_AGGR_TYPE_P (t)) 5501 { 5502 fixup_anonymous_aggr (t); 5503 5504 if (TYPE_FIELDS (t)) 5505 { 5506 tree decl = grokdeclarator (/*declarator=*/NULL, 5507 declspecs, NORMAL, 0, NULL); 5508 finish_anon_union (decl); 5509 } 5510 } 5511 5512 return t; 5513 } 5514 5515 /* Decode a "typename", such as "int **", returning a ..._TYPE node. */ 5517 5518 tree 5519 groktypename (cp_decl_specifier_seq *type_specifiers, 5520 const cp_declarator *declarator, 5521 bool is_template_arg) 5522 { 5523 tree attrs; 5524 tree type; 5525 enum decl_context context 5526 = is_template_arg ? TEMPLATE_TYPE_ARG : TYPENAME; 5527 attrs = type_specifiers->attributes; 5528 type_specifiers->attributes = NULL_TREE; 5529 type = grokdeclarator (declarator, type_specifiers, context, 0, &attrs); 5530 if (attrs && type != error_mark_node) 5531 { 5532 if (CLASS_TYPE_P (type)) 5533 warning (OPT_Wattributes, "ignoring attributes applied to class type %qT " 5534 "outside of definition", type); 5535 else if (MAYBE_CLASS_TYPE_P (type)) 5536 /* A template type parameter or other dependent type. */ 5537 warning (OPT_Wattributes, "ignoring attributes applied to dependent " 5538 "type %qT without an associated declaration", type); 5539 else 5540 cplus_decl_attributes (&type, attrs, 0); 5541 } 5542 return type; 5543 } 5544 5545 /* Process a DECLARATOR for a function-scope or namespace-scope 5546 variable or function declaration. 5547 (Function definitions go through start_function; class member 5548 declarations appearing in the body of the class go through 5549 grokfield.) The DECL corresponding to the DECLARATOR is returned. 5550 If an error occurs, the error_mark_node is returned instead. 5551 5552 DECLSPECS are the decl-specifiers for the declaration. INITIALIZED is 5553 SD_INITIALIZED if an explicit initializer is present, or SD_DEFAULTED 5554 for an explicitly defaulted function, or SD_DELETED for an explicitly 5555 deleted function, but 0 (SD_UNINITIALIZED) if this is a variable 5556 implicitly initialized via a default constructor. It can also be 5557 SD_DECOMPOSITION which behaves much like SD_INITIALIZED, but we also 5558 mark the new decl as DECL_DECOMPOSITION_P. 5559 5560 ATTRIBUTES and PREFIX_ATTRIBUTES are GNU attributes associated with this 5561 declaration. 5562 5563 The scope represented by the context of the returned DECL is pushed 5564 (if it is not the global namespace) and is assigned to 5565 *PUSHED_SCOPE_P. The caller is then responsible for calling 5566 pop_scope on *PUSHED_SCOPE_P if it is set. */ 5567 5568 tree 5569 start_decl (const cp_declarator *declarator, 5570 cp_decl_specifier_seq *declspecs, 5571 int initialized, 5572 tree attributes, 5573 tree prefix_attributes, 5574 tree *pushed_scope_p) 5575 { 5576 tree decl; 5577 tree context; 5578 bool was_public; 5579 int flags; 5580 bool alias; 5581 tree initial; 5582 5583 *pushed_scope_p = NULL_TREE; 5584 5585 if (prefix_attributes != error_mark_node) 5586 attributes = chainon (attributes, prefix_attributes); 5587 5588 decl = grokdeclarator (declarator, declspecs, NORMAL, initialized, 5589 &attributes); 5590 5591 if (decl == NULL_TREE || VOID_TYPE_P (decl) 5592 || decl == error_mark_node 5593 || prefix_attributes == error_mark_node) 5594 return error_mark_node; 5595 5596 context = CP_DECL_CONTEXT (decl); 5597 if (context != global_namespace) 5598 *pushed_scope_p = push_scope (context); 5599 5600 if (initialized && TREE_CODE (decl) == TYPE_DECL) 5601 { 5602 error_at (DECL_SOURCE_LOCATION (decl), 5603 "typedef %qD is initialized (use %qs instead)", 5604 decl, "decltype"); 5605 return error_mark_node; 5606 } 5607 5608 /* Save the DECL_INITIAL value in case it gets clobbered to assist 5609 with attribute validation. */ 5610 initial = DECL_INITIAL (decl); 5611 5612 if (initialized) 5613 { 5614 if (! toplevel_bindings_p () 5615 && DECL_EXTERNAL (decl)) 5616 warning (0, "declaration of %q#D has %<extern%> and is initialized", 5617 decl); 5618 DECL_EXTERNAL (decl) = 0; 5619 if (toplevel_bindings_p ()) 5620 TREE_STATIC (decl) = 1; 5621 /* Tell 'cplus_decl_attributes' this is an initialized decl, 5622 even though we might not yet have the initializer expression. */ 5623 if (!DECL_INITIAL (decl)) 5624 DECL_INITIAL (decl) = error_mark_node; 5625 } 5626 alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl)) != 0; 5627 5628 if (alias && TREE_CODE (decl) == FUNCTION_DECL) 5629 record_key_method_defined (decl); 5630 5631 /* If this is a typedef that names the class for linkage purposes 5632 (7.1.3p8), apply any attributes directly to the type. */ 5633 if (TREE_CODE (decl) == TYPE_DECL 5634 && OVERLOAD_TYPE_P (TREE_TYPE (decl)) 5635 && decl == TYPE_NAME (TYPE_MAIN_VARIANT (TREE_TYPE (decl)))) 5636 flags = ATTR_FLAG_TYPE_IN_PLACE; 5637 else 5638 flags = 0; 5639 5640 /* Set attributes here so if duplicate decl, will have proper attributes. */ 5641 cplus_decl_attributes (&decl, attributes, flags); 5642 5643 /* Restore the original DECL_INITIAL that we may have clobbered earlier to 5644 assist with attribute validation. */ 5645 DECL_INITIAL (decl) = initial; 5646 5647 /* Dllimported symbols cannot be defined. Static data members (which 5648 can be initialized in-class and dllimported) go through grokfield, 5649 not here, so we don't need to exclude those decls when checking for 5650 a definition. */ 5651 if (initialized && DECL_DLLIMPORT_P (decl)) 5652 { 5653 error_at (DECL_SOURCE_LOCATION (decl), 5654 "definition of %q#D is marked %<dllimport%>", decl); 5655 DECL_DLLIMPORT_P (decl) = 0; 5656 } 5657 5658 /* If #pragma weak was used, mark the decl weak now. */ 5659 if (!processing_template_decl && !DECL_DECOMPOSITION_P (decl)) 5660 maybe_apply_pragma_weak (decl); 5661 5662 if (TREE_CODE (decl) == FUNCTION_DECL 5663 && DECL_DECLARED_INLINE_P (decl) 5664 && DECL_UNINLINABLE (decl) 5665 && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl))) 5666 warning_at (DECL_SOURCE_LOCATION (decl), 0, 5667 "inline function %qD given attribute %qs", decl, "noinline"); 5668 5669 if (TYPE_P (context) && COMPLETE_TYPE_P (complete_type (context))) 5670 { 5671 bool this_tmpl = (current_template_depth 5672 > template_class_depth (context)); 5673 if (VAR_P (decl)) 5674 { 5675 tree field = lookup_field (context, DECL_NAME (decl), 0, false); 5676 if (field == NULL_TREE 5677 || !(VAR_P (field) || variable_template_p (field))) 5678 error ("%q+#D is not a static data member of %q#T", decl, context); 5679 else if (variable_template_p (field) 5680 && (DECL_LANG_SPECIFIC (decl) 5681 && DECL_TEMPLATE_SPECIALIZATION (decl))) 5682 /* OK, specialization was already checked. */; 5683 else if (variable_template_p (field) && !this_tmpl) 5684 { 5685 error_at (DECL_SOURCE_LOCATION (decl), 5686 "non-member-template declaration of %qD", decl); 5687 inform (DECL_SOURCE_LOCATION (field), "does not match " 5688 "member template declaration here"); 5689 return error_mark_node; 5690 } 5691 else 5692 { 5693 if (variable_template_p (field)) 5694 field = DECL_TEMPLATE_RESULT (field); 5695 5696 if (DECL_CONTEXT (field) != context) 5697 { 5698 if (!same_type_p (DECL_CONTEXT (field), context)) 5699 permerror (input_location, "ISO C++ does not permit %<%T::%D%> " 5700 "to be defined as %<%T::%D%>", 5701 DECL_CONTEXT (field), DECL_NAME (decl), 5702 context, DECL_NAME (decl)); 5703 DECL_CONTEXT (decl) = DECL_CONTEXT (field); 5704 } 5705 /* Static data member are tricky; an in-class initialization 5706 still doesn't provide a definition, so the in-class 5707 declaration will have DECL_EXTERNAL set, but will have an 5708 initialization. Thus, duplicate_decls won't warn 5709 about this situation, and so we check here. */ 5710 if (initialized && DECL_INITIALIZED_IN_CLASS_P (field)) 5711 error ("duplicate initialization of %qD", decl); 5712 field = duplicate_decls (decl, field); 5713 if (field == error_mark_node) 5714 return error_mark_node; 5715 else if (field) 5716 decl = field; 5717 } 5718 } 5719 else 5720 { 5721 tree field = check_classfn (context, decl, 5722 this_tmpl 5723 ? current_template_parms 5724 : NULL_TREE); 5725 if (field && field != error_mark_node 5726 && duplicate_decls (decl, field)) 5727 decl = field; 5728 } 5729 5730 /* cp_finish_decl sets DECL_EXTERNAL if DECL_IN_AGGR_P is set. */ 5731 DECL_IN_AGGR_P (decl) = 0; 5732 /* Do not mark DECL as an explicit specialization if it was not 5733 already marked as an instantiation; a declaration should 5734 never be marked as a specialization unless we know what 5735 template is being specialized. */ 5736 if (DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl)) 5737 { 5738 SET_DECL_TEMPLATE_SPECIALIZATION (decl); 5739 if (TREE_CODE (decl) == FUNCTION_DECL) 5740 DECL_COMDAT (decl) = (TREE_PUBLIC (decl) 5741 && DECL_DECLARED_INLINE_P (decl)); 5742 else 5743 DECL_COMDAT (decl) = false; 5744 5745 /* [temp.expl.spec] An explicit specialization of a static data 5746 member of a template is a definition if the declaration 5747 includes an initializer; otherwise, it is a declaration. 5748 5749 We check for processing_specialization so this only applies 5750 to the new specialization syntax. */ 5751 if (!initialized && processing_specialization) 5752 DECL_EXTERNAL (decl) = 1; 5753 } 5754 5755 if (DECL_EXTERNAL (decl) && ! DECL_TEMPLATE_SPECIALIZATION (decl) 5756 /* Aliases are definitions. */ 5757 && !alias) 5758 permerror (declarator->id_loc, 5759 "declaration of %q#D outside of class is not definition", 5760 decl); 5761 } 5762 5763 /* Create a DECL_LANG_SPECIFIC so that DECL_DECOMPOSITION_P works. */ 5764 if (initialized == SD_DECOMPOSITION) 5765 fit_decomposition_lang_decl (decl, NULL_TREE); 5766 5767 was_public = TREE_PUBLIC (decl); 5768 5769 if ((DECL_EXTERNAL (decl) || TREE_CODE (decl) == FUNCTION_DECL) 5770 && current_function_decl) 5771 { 5772 /* A function-scope decl of some namespace-scope decl. */ 5773 DECL_LOCAL_DECL_P (decl) = true; 5774 if (named_module_purview_p ()) 5775 error_at (declarator->id_loc, 5776 "block-scope extern declaration %q#D not permitted" 5777 " in module purview", decl); 5778 } 5779 5780 /* Enter this declaration into the symbol table. Don't push the plain 5781 VAR_DECL for a variable template. */ 5782 if (!template_parm_scope_p () 5783 || !VAR_P (decl)) 5784 decl = maybe_push_decl (decl); 5785 5786 if (processing_template_decl) 5787 decl = push_template_decl (decl); 5788 5789 if (decl == error_mark_node) 5790 return error_mark_node; 5791 5792 if (VAR_P (decl) 5793 && DECL_NAMESPACE_SCOPE_P (decl) && !TREE_PUBLIC (decl) && !was_public 5794 && !DECL_THIS_STATIC (decl) && !DECL_ARTIFICIAL (decl) 5795 /* But not templated variables. */ 5796 && !(DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))) 5797 { 5798 /* This is a const variable with implicit 'static'. Set 5799 DECL_THIS_STATIC so we can tell it from variables that are 5800 !TREE_PUBLIC because of the anonymous namespace. */ 5801 gcc_assert (CP_TYPE_CONST_P (TREE_TYPE (decl)) || errorcount); 5802 DECL_THIS_STATIC (decl) = 1; 5803 } 5804 5805 if (current_function_decl && VAR_P (decl) 5806 && DECL_DECLARED_CONSTEXPR_P (current_function_decl) 5807 && cxx_dialect < cxx23) 5808 { 5809 bool ok = false; 5810 if (CP_DECL_THREAD_LOCAL_P (decl) && !DECL_REALLY_EXTERN (decl)) 5811 error_at (DECL_SOURCE_LOCATION (decl), 5812 "%qD defined %<thread_local%> in %qs function only " 5813 "available with %<-std=c++2b%> or %<-std=gnu++2b%>", decl, 5814 DECL_IMMEDIATE_FUNCTION_P (current_function_decl) 5815 ? "consteval" : "constexpr"); 5816 else if (TREE_STATIC (decl)) 5817 error_at (DECL_SOURCE_LOCATION (decl), 5818 "%qD defined %<static%> in %qs function only available " 5819 "with %<-std=c++2b%> or %<-std=gnu++2b%>", decl, 5820 DECL_IMMEDIATE_FUNCTION_P (current_function_decl) 5821 ? "consteval" : "constexpr"); 5822 else 5823 ok = true; 5824 if (!ok) 5825 cp_function_chain->invalid_constexpr = true; 5826 } 5827 5828 if (!processing_template_decl && VAR_P (decl)) 5829 start_decl_1 (decl, initialized); 5830 5831 return decl; 5832 } 5833 5834 /* Process the declaration of a variable DECL. INITIALIZED is true 5835 iff DECL is explicitly initialized. (INITIALIZED is false if the 5836 variable is initialized via an implicitly-called constructor.) 5837 This function must be called for ordinary variables (including, for 5838 example, implicit instantiations of templates), but must not be 5839 called for template declarations. */ 5840 5841 void 5842 start_decl_1 (tree decl, bool initialized) 5843 { 5844 gcc_checking_assert (!processing_template_decl); 5845 5846 if (error_operand_p (decl)) 5847 return; 5848 5849 gcc_checking_assert (VAR_P (decl)); 5850 5851 tree type = TREE_TYPE (decl); 5852 bool complete_p = COMPLETE_TYPE_P (type); 5853 bool aggregate_definition_p 5854 = MAYBE_CLASS_TYPE_P (type) && !DECL_EXTERNAL (decl); 5855 5856 /* If an explicit initializer is present, or if this is a definition 5857 of an aggregate, then we need a complete type at this point. 5858 (Scalars are always complete types, so there is nothing to 5859 check.) This code just sets COMPLETE_P; errors (if necessary) 5860 are issued below. */ 5861 if ((initialized || aggregate_definition_p) 5862 && !complete_p 5863 && COMPLETE_TYPE_P (complete_type (type))) 5864 { 5865 complete_p = true; 5866 /* We will not yet have set TREE_READONLY on DECL if the type 5867 was "const", but incomplete, before this point. But, now, we 5868 have a complete type, so we can try again. */ 5869 cp_apply_type_quals_to_decl (cp_type_quals (type), decl); 5870 } 5871 5872 if (initialized) 5873 /* Is it valid for this decl to have an initializer at all? */ 5874 { 5875 /* Don't allow initializations for incomplete types except for 5876 arrays which might be completed by the initialization. */ 5877 if (complete_p) 5878 ; /* A complete type is ok. */ 5879 else if (type_uses_auto (type)) 5880 ; /* An auto type is ok. */ 5881 else if (TREE_CODE (type) != ARRAY_TYPE) 5882 { 5883 error ("variable %q#D has initializer but incomplete type", decl); 5884 type = TREE_TYPE (decl) = error_mark_node; 5885 } 5886 else if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (type)))) 5887 { 5888 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)) 5889 error ("elements of array %q#D have incomplete type", decl); 5890 /* else we already gave an error in start_decl. */ 5891 } 5892 } 5893 else if (aggregate_definition_p && !complete_p) 5894 { 5895 if (type_uses_auto (type)) 5896 gcc_assert (CLASS_PLACEHOLDER_TEMPLATE (type)); 5897 else 5898 { 5899 error ("aggregate %q#D has incomplete type and cannot be defined", 5900 decl); 5901 /* Change the type so that assemble_variable will give 5902 DECL an rtl we can live with: (mem (const_int 0)). */ 5903 type = TREE_TYPE (decl) = error_mark_node; 5904 } 5905 } 5906 5907 /* Create a new scope to hold this declaration if necessary. 5908 Whether or not a new scope is necessary cannot be determined 5909 until after the type has been completed; if the type is a 5910 specialization of a class template it is not until after 5911 instantiation has occurred that TYPE_HAS_NONTRIVIAL_DESTRUCTOR 5912 will be set correctly. */ 5913 maybe_push_cleanup_level (type); 5914 } 5915 5916 /* Given a parenthesized list of values INIT, create a CONSTRUCTOR to handle 5917 C++20 P0960. TYPE is the type of the object we're initializing. */ 5918 5919 tree 5920 do_aggregate_paren_init (tree init, tree type) 5921 { 5922 tree val = TREE_VALUE (init); 5923 5924 if (TREE_CHAIN (init) == NULL_TREE) 5925 { 5926 /* If the list has a single element and it's a string literal, 5927 then it's the initializer for the array as a whole. */ 5928 if (TREE_CODE (type) == ARRAY_TYPE 5929 && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type))) 5930 && TREE_CODE (tree_strip_any_location_wrapper (val)) 5931 == STRING_CST) 5932 return val; 5933 /* Handle non-standard extensions like compound literals. This also 5934 prevents triggering aggregate parenthesized-initialization in 5935 compiler-generated code for =default. */ 5936 else if (same_type_ignoring_top_level_qualifiers_p (type, 5937 TREE_TYPE (val))) 5938 return val; 5939 } 5940 5941 init = build_constructor_from_list (init_list_type_node, init); 5942 CONSTRUCTOR_IS_DIRECT_INIT (init) = true; 5943 CONSTRUCTOR_IS_PAREN_INIT (init) = true; 5944 return init; 5945 } 5946 5947 /* Handle initialization of references. DECL, TYPE, and INIT have the 5948 same meaning as in cp_finish_decl. *CLEANUP must be NULL on entry, 5949 but will be set to a new CLEANUP_STMT if a temporary is created 5950 that must be destroyed subsequently. 5951 5952 Returns an initializer expression to use to initialize DECL, or 5953 NULL if the initialization can be performed statically. 5954 5955 Quotes on semantics can be found in ARM 8.4.3. */ 5956 5957 static tree 5958 grok_reference_init (tree decl, tree type, tree init, int flags) 5959 { 5960 if (init == NULL_TREE) 5961 { 5962 if ((DECL_LANG_SPECIFIC (decl) == 0 5963 || DECL_IN_AGGR_P (decl) == 0) 5964 && ! DECL_THIS_EXTERN (decl)) 5965 error_at (DECL_SOURCE_LOCATION (decl), 5966 "%qD declared as reference but not initialized", decl); 5967 return NULL_TREE; 5968 } 5969 5970 tree ttype = TREE_TYPE (type); 5971 if (TREE_CODE (init) == TREE_LIST) 5972 { 5973 /* This handles (C++20 only) code like 5974 5975 const A& r(1, 2, 3); 5976 5977 where we treat the parenthesized list as a CONSTRUCTOR. */ 5978 if (TREE_TYPE (init) == NULL_TREE 5979 && CP_AGGREGATE_TYPE_P (ttype) 5980 && !DECL_DECOMPOSITION_P (decl) 5981 && (cxx_dialect >= cxx20)) 5982 { 5983 /* We don't know yet if we should treat const A& r(1) as 5984 const A& r{1}. */ 5985 if (list_length (init) == 1) 5986 { 5987 flags |= LOOKUP_AGGREGATE_PAREN_INIT; 5988 init = build_x_compound_expr_from_list (init, ELK_INIT, 5989 tf_warning_or_error); 5990 } 5991 /* If the list had more than one element, the code is ill-formed 5992 pre-C++20, so we can build a constructor right away. */ 5993 else 5994 init = do_aggregate_paren_init (init, ttype); 5995 } 5996 else 5997 init = build_x_compound_expr_from_list (init, ELK_INIT, 5998 tf_warning_or_error); 5999 } 6000 6001 if (TREE_CODE (ttype) != ARRAY_TYPE 6002 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE) 6003 /* Note: default conversion is only called in very special cases. */ 6004 init = decay_conversion (init, tf_warning_or_error); 6005 6006 /* check_initializer handles this for non-reference variables, but for 6007 references we need to do it here or the initializer will get the 6008 incomplete array type and confuse later calls to 6009 cp_complete_array_type. */ 6010 if (TREE_CODE (ttype) == ARRAY_TYPE 6011 && TYPE_DOMAIN (ttype) == NULL_TREE 6012 && (BRACE_ENCLOSED_INITIALIZER_P (init) 6013 || TREE_CODE (init) == STRING_CST)) 6014 { 6015 cp_complete_array_type (&ttype, init, false); 6016 if (ttype != TREE_TYPE (type)) 6017 type = cp_build_reference_type (ttype, TYPE_REF_IS_RVALUE (type)); 6018 } 6019 6020 /* Convert INIT to the reference type TYPE. This may involve the 6021 creation of a temporary, whose lifetime must be the same as that 6022 of the reference. If so, a DECL_EXPR for the temporary will be 6023 added just after the DECL_EXPR for DECL. That's why we don't set 6024 DECL_INITIAL for local references (instead assigning to them 6025 explicitly); we need to allow the temporary to be initialized 6026 first. */ 6027 return initialize_reference (type, init, flags, 6028 tf_warning_or_error); 6029 } 6030 6031 /* Designated initializers in arrays are not supported in GNU C++. 6032 The parser cannot detect this error since it does not know whether 6033 a given brace-enclosed initializer is for a class type or for an 6034 array. This function checks that CE does not use a designated 6035 initializer. If it does, an error is issued. Returns true if CE 6036 is valid, i.e., does not have a designated initializer. */ 6037 6038 bool 6039 check_array_designated_initializer (constructor_elt *ce, 6040 unsigned HOST_WIDE_INT index) 6041 { 6042 /* Designated initializers for array elements are not supported. */ 6043 if (ce->index) 6044 { 6045 /* The parser only allows identifiers as designated 6046 initializers. */ 6047 if (ce->index == error_mark_node) 6048 { 6049 error ("name used in a GNU-style designated " 6050 "initializer for an array"); 6051 return false; 6052 } 6053 else if (identifier_p (ce->index)) 6054 { 6055 error ("name %qD used in a GNU-style designated " 6056 "initializer for an array", ce->index); 6057 return false; 6058 } 6059 6060 tree ce_index = build_expr_type_conversion (WANT_INT | WANT_ENUM, 6061 ce->index, true); 6062 if (ce_index 6063 && INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (ce_index)) 6064 && (TREE_CODE (ce_index = fold_non_dependent_expr (ce_index)) 6065 == INTEGER_CST)) 6066 { 6067 /* A C99 designator is OK if it matches the current index. */ 6068 if (wi::to_wide (ce_index) == index) 6069 { 6070 ce->index = ce_index; 6071 return true; 6072 } 6073 else 6074 sorry ("non-trivial designated initializers not supported"); 6075 } 6076 else 6077 error_at (cp_expr_loc_or_input_loc (ce->index), 6078 "C99 designator %qE is not an integral constant-expression", 6079 ce->index); 6080 6081 return false; 6082 } 6083 6084 return true; 6085 } 6086 6087 /* When parsing `int a[] = {1, 2};' we don't know the size of the 6088 array until we finish parsing the initializer. If that's the 6089 situation we're in, update DECL accordingly. */ 6090 6091 static void 6092 maybe_deduce_size_from_array_init (tree decl, tree init) 6093 { 6094 tree type = TREE_TYPE (decl); 6095 6096 if (TREE_CODE (type) == ARRAY_TYPE 6097 && TYPE_DOMAIN (type) == NULL_TREE 6098 && TREE_CODE (decl) != TYPE_DECL) 6099 { 6100 /* do_default is really a C-ism to deal with tentative definitions. 6101 But let's leave it here to ease the eventual merge. */ 6102 int do_default = !DECL_EXTERNAL (decl); 6103 tree initializer = init ? init : DECL_INITIAL (decl); 6104 int failure = 0; 6105 6106 /* Check that there are no designated initializers in INIT, as 6107 those are not supported in GNU C++, and as the middle-end 6108 will crash if presented with a non-numeric designated 6109 initializer. */ 6110 if (initializer && BRACE_ENCLOSED_INITIALIZER_P (initializer)) 6111 { 6112 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (initializer); 6113 constructor_elt *ce; 6114 HOST_WIDE_INT i; 6115 FOR_EACH_VEC_SAFE_ELT (v, i, ce) 6116 { 6117 if (instantiation_dependent_expression_p (ce->index)) 6118 return; 6119 if (!check_array_designated_initializer (ce, i)) 6120 failure = 1; 6121 /* If an un-designated initializer is type-dependent, we can't 6122 check brace elision yet. */ 6123 if (ce->index == NULL_TREE 6124 && type_dependent_expression_p (ce->value)) 6125 return; 6126 } 6127 } 6128 6129 if (failure) 6130 TREE_TYPE (decl) = error_mark_node; 6131 else 6132 { 6133 failure = cp_complete_array_type (&TREE_TYPE (decl), initializer, 6134 do_default); 6135 if (failure == 1) 6136 { 6137 error_at (cp_expr_loc_or_loc (initializer, 6138 DECL_SOURCE_LOCATION (decl)), 6139 "initializer fails to determine size of %qD", decl); 6140 } 6141 else if (failure == 2) 6142 { 6143 if (do_default) 6144 { 6145 error_at (DECL_SOURCE_LOCATION (decl), 6146 "array size missing in %qD", decl); 6147 } 6148 /* If a `static' var's size isn't known, make it extern as 6149 well as static, so it does not get allocated. If it's not 6150 `static', then don't mark it extern; finish_incomplete_decl 6151 will give it a default size and it will get allocated. */ 6152 else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl)) 6153 DECL_EXTERNAL (decl) = 1; 6154 } 6155 else if (failure == 3) 6156 { 6157 error_at (DECL_SOURCE_LOCATION (decl), 6158 "zero-size array %qD", decl); 6159 } 6160 } 6161 6162 cp_apply_type_quals_to_decl (cp_type_quals (TREE_TYPE (decl)), decl); 6163 6164 relayout_decl (decl); 6165 } 6166 } 6167 6168 /* Set DECL_SIZE, DECL_ALIGN, etc. for DECL (a VAR_DECL), and issue 6169 any appropriate error messages regarding the layout. */ 6170 6171 static void 6172 layout_var_decl (tree decl) 6173 { 6174 tree type; 6175 6176 type = TREE_TYPE (decl); 6177 if (type == error_mark_node) 6178 return; 6179 6180 /* If we haven't already laid out this declaration, do so now. 6181 Note that we must not call complete type for an external object 6182 because it's type might involve templates that we are not 6183 supposed to instantiate yet. (And it's perfectly valid to say 6184 `extern X x' for some incomplete type `X'.) */ 6185 if (!DECL_EXTERNAL (decl)) 6186 complete_type (type); 6187 if (!DECL_SIZE (decl) 6188 && TREE_TYPE (decl) != error_mark_node 6189 && complete_or_array_type_p (type)) 6190 layout_decl (decl, 0); 6191 6192 if (!DECL_EXTERNAL (decl) && DECL_SIZE (decl) == NULL_TREE) 6193 { 6194 /* An automatic variable with an incomplete type: that is an error. 6195 Don't talk about array types here, since we took care of that 6196 message in grokdeclarator. */ 6197 error_at (DECL_SOURCE_LOCATION (decl), 6198 "storage size of %qD isn%'t known", decl); 6199 TREE_TYPE (decl) = error_mark_node; 6200 } 6201 #if 0 6202 /* Keep this code around in case we later want to control debug info 6203 based on whether a type is "used". (jason 1999-11-11) */ 6204 6205 else if (!DECL_EXTERNAL (decl) && MAYBE_CLASS_TYPE_P (ttype)) 6206 /* Let debugger know it should output info for this type. */ 6207 note_debug_info_needed (ttype); 6208 6209 if (TREE_STATIC (decl) && DECL_CLASS_SCOPE_P (decl)) 6210 note_debug_info_needed (DECL_CONTEXT (decl)); 6211 #endif 6212 6213 if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl)) 6214 && DECL_SIZE (decl) != NULL_TREE 6215 && ! TREE_CONSTANT (DECL_SIZE (decl))) 6216 { 6217 if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST 6218 && !DECL_LOCAL_DECL_P (decl)) 6219 constant_expression_warning (DECL_SIZE (decl)); 6220 else 6221 { 6222 error_at (DECL_SOURCE_LOCATION (decl), 6223 "storage size of %qD isn%'t constant", decl); 6224 TREE_TYPE (decl) = error_mark_node; 6225 type = error_mark_node; 6226 } 6227 } 6228 6229 /* If the final element initializes a flexible array field, add the size of 6230 that initializer to DECL's size. */ 6231 if (type != error_mark_node 6232 && DECL_INITIAL (decl) 6233 && TREE_CODE (DECL_INITIAL (decl)) == CONSTRUCTOR 6234 && !vec_safe_is_empty (CONSTRUCTOR_ELTS (DECL_INITIAL (decl))) 6235 && DECL_SIZE (decl) != NULL_TREE 6236 && TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST 6237 && TYPE_SIZE (type) != NULL_TREE 6238 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST 6239 && tree_int_cst_equal (DECL_SIZE (decl), TYPE_SIZE (type))) 6240 { 6241 constructor_elt &elt = CONSTRUCTOR_ELTS (DECL_INITIAL (decl))->last (); 6242 if (elt.index) 6243 { 6244 tree itype = TREE_TYPE (elt.index); 6245 tree vtype = TREE_TYPE (elt.value); 6246 if (TREE_CODE (itype) == ARRAY_TYPE 6247 && TYPE_DOMAIN (itype) == NULL 6248 && TREE_CODE (vtype) == ARRAY_TYPE 6249 && COMPLETE_TYPE_P (vtype)) 6250 { 6251 DECL_SIZE (decl) 6252 = size_binop (PLUS_EXPR, DECL_SIZE (decl), TYPE_SIZE (vtype)); 6253 DECL_SIZE_UNIT (decl) 6254 = size_binop (PLUS_EXPR, DECL_SIZE_UNIT (decl), 6255 TYPE_SIZE_UNIT (vtype)); 6256 } 6257 } 6258 } 6259 } 6260 6261 /* If a local static variable is declared in an inline function, or if 6262 we have a weak definition, we must endeavor to create only one 6263 instance of the variable at link-time. */ 6264 6265 void 6266 maybe_commonize_var (tree decl) 6267 { 6268 /* Don't mess with __FUNCTION__ and similar. */ 6269 if (DECL_ARTIFICIAL (decl)) 6270 return; 6271 6272 /* Static data in a function with comdat linkage also has comdat 6273 linkage. */ 6274 if ((TREE_STATIC (decl) 6275 && DECL_FUNCTION_SCOPE_P (decl) 6276 && vague_linkage_p (DECL_CONTEXT (decl))) 6277 || (TREE_PUBLIC (decl) && DECL_INLINE_VAR_P (decl))) 6278 { 6279 if (flag_weak) 6280 { 6281 /* With weak symbols, we simply make the variable COMDAT; 6282 that will cause copies in multiple translations units to 6283 be merged. */ 6284 comdat_linkage (decl); 6285 } 6286 else 6287 { 6288 if (DECL_INITIAL (decl) == NULL_TREE 6289 || DECL_INITIAL (decl) == error_mark_node) 6290 { 6291 /* Without weak symbols, we can use COMMON to merge 6292 uninitialized variables. */ 6293 TREE_PUBLIC (decl) = 1; 6294 DECL_COMMON (decl) = 1; 6295 } 6296 else 6297 { 6298 /* While for initialized variables, we must use internal 6299 linkage -- which means that multiple copies will not 6300 be merged. */ 6301 TREE_PUBLIC (decl) = 0; 6302 DECL_COMMON (decl) = 0; 6303 DECL_INTERFACE_KNOWN (decl) = 1; 6304 const char *msg; 6305 if (DECL_INLINE_VAR_P (decl)) 6306 msg = G_("sorry: semantics of inline variable " 6307 "%q#D are wrong (you%'ll wind up with " 6308 "multiple copies)"); 6309 else 6310 msg = G_("sorry: semantics of inline function " 6311 "static data %q#D are wrong (you%'ll wind " 6312 "up with multiple copies)"); 6313 if (warning_at (DECL_SOURCE_LOCATION (decl), 0, 6314 msg, decl)) 6315 inform (DECL_SOURCE_LOCATION (decl), 6316 "you can work around this by removing the initializer"); 6317 } 6318 } 6319 } 6320 } 6321 6322 /* Issue an error message if DECL is an uninitialized const variable. 6323 CONSTEXPR_CONTEXT_P is true when the function is called in a constexpr 6324 context from potential_constant_expression. Returns true if all is well, 6325 false otherwise. */ 6326 6327 bool 6328 check_for_uninitialized_const_var (tree decl, bool constexpr_context_p, 6329 tsubst_flags_t complain) 6330 { 6331 tree type = strip_array_types (TREE_TYPE (decl)); 6332 6333 /* ``Unless explicitly declared extern, a const object does not have 6334 external linkage and must be initialized. ($8.4; $12.1)'' ARM 6335 7.1.6 */ 6336 if (VAR_P (decl) 6337 && !TYPE_REF_P (type) 6338 && (CP_TYPE_CONST_P (type) 6339 /* C++20 permits trivial default initialization in constexpr 6340 context (P1331R2). */ 6341 || (cxx_dialect < cxx20 6342 && (constexpr_context_p 6343 || var_in_constexpr_fn (decl)))) 6344 && !DECL_NONTRIVIALLY_INITIALIZED_P (decl)) 6345 { 6346 tree field = default_init_uninitialized_part (type); 6347 if (!field) 6348 return true; 6349 6350 bool show_notes = true; 6351 6352 if (!constexpr_context_p || cxx_dialect >= cxx20) 6353 { 6354 if (CP_TYPE_CONST_P (type)) 6355 { 6356 if (complain & tf_error) 6357 show_notes = permerror (DECL_SOURCE_LOCATION (decl), 6358 "uninitialized %<const %D%>", decl); 6359 } 6360 else 6361 { 6362 if (!is_instantiation_of_constexpr (current_function_decl) 6363 && (complain & tf_error)) 6364 error_at (DECL_SOURCE_LOCATION (decl), 6365 "uninitialized variable %qD in %<constexpr%> " 6366 "function", decl); 6367 else 6368 show_notes = false; 6369 cp_function_chain->invalid_constexpr = true; 6370 } 6371 } 6372 else if (complain & tf_error) 6373 error_at (DECL_SOURCE_LOCATION (decl), 6374 "uninitialized variable %qD in %<constexpr%> context", 6375 decl); 6376 6377 if (show_notes && CLASS_TYPE_P (type) && (complain & tf_error)) 6378 { 6379 tree defaulted_ctor; 6380 6381 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)), 6382 "%q#T has no user-provided default constructor", type); 6383 defaulted_ctor = in_class_defaulted_default_constructor (type); 6384 if (defaulted_ctor) 6385 inform (DECL_SOURCE_LOCATION (defaulted_ctor), 6386 "constructor is not user-provided because it is " 6387 "explicitly defaulted in the class body"); 6388 inform (DECL_SOURCE_LOCATION (field), 6389 "and the implicitly-defined constructor does not " 6390 "initialize %q#D", field); 6391 } 6392 6393 return false; 6394 } 6395 6396 return true; 6397 } 6398 6399 /* Structure holding the current initializer being processed by reshape_init. 6401 CUR is a pointer to the current element being processed, END is a pointer 6402 after the last element present in the initializer. */ 6403 struct reshape_iter 6404 { 6405 constructor_elt *cur; 6406 constructor_elt *end; 6407 }; 6408 6409 static tree reshape_init_r (tree, reshape_iter *, tree, tsubst_flags_t); 6410 6411 /* FIELD is an element of TYPE_FIELDS or NULL. In the former case, the value 6412 returned is the next FIELD_DECL (possibly FIELD itself) that can be 6413 initialized. If there are no more such fields, the return value 6414 will be NULL. */ 6415 6416 tree 6417 next_initializable_field (tree field) 6418 { 6419 while (field 6420 && (TREE_CODE (field) != FIELD_DECL 6421 || DECL_UNNAMED_BIT_FIELD (field) 6422 || (DECL_ARTIFICIAL (field) 6423 /* In C++17, don't skip base class fields. */ 6424 && !(cxx_dialect >= cxx17 && DECL_FIELD_IS_BASE (field)) 6425 /* Don't skip vptr fields. We might see them when we're 6426 called from reduced_constant_expression_p. */ 6427 && !DECL_VIRTUAL_P (field)))) 6428 field = DECL_CHAIN (field); 6429 6430 return field; 6431 } 6432 6433 /* FIELD is an element of TYPE_FIELDS or NULL. In the former case, the value 6434 returned is the next FIELD_DECL (possibly FIELD itself) that corresponds 6435 to a subobject. If there are no more such fields, the return value will be 6436 NULL. */ 6437 6438 tree 6439 next_subobject_field (tree field) 6440 { 6441 while (field 6442 && (TREE_CODE (field) != FIELD_DECL 6443 || DECL_UNNAMED_BIT_FIELD (field) 6444 || (DECL_ARTIFICIAL (field) 6445 && !DECL_FIELD_IS_BASE (field) 6446 && !DECL_VIRTUAL_P (field)))) 6447 field = DECL_CHAIN (field); 6448 6449 return field; 6450 } 6451 6452 /* Return true for [dcl.init.list] direct-list-initialization from 6453 single element of enumeration with a fixed underlying type. */ 6454 6455 bool 6456 is_direct_enum_init (tree type, tree init) 6457 { 6458 if (cxx_dialect >= cxx17 6459 && TREE_CODE (type) == ENUMERAL_TYPE 6460 && ENUM_FIXED_UNDERLYING_TYPE_P (type) 6461 && TREE_CODE (init) == CONSTRUCTOR 6462 && CONSTRUCTOR_IS_DIRECT_INIT (init) 6463 && CONSTRUCTOR_NELTS (init) == 1 6464 /* DR 2374: The single element needs to be implicitly 6465 convertible to the underlying type of the enum. */ 6466 && can_convert_arg (ENUM_UNDERLYING_TYPE (type), 6467 TREE_TYPE (CONSTRUCTOR_ELT (init, 0)->value), 6468 CONSTRUCTOR_ELT (init, 0)->value, 6469 LOOKUP_IMPLICIT, tf_none)) 6470 return true; 6471 return false; 6472 } 6473 6474 /* Subroutine of reshape_init_array and reshape_init_vector, which does 6475 the actual work. ELT_TYPE is the element type of the array. MAX_INDEX is an 6476 INTEGER_CST representing the size of the array minus one (the maximum index), 6477 or NULL_TREE if the array was declared without specifying the size. D is 6478 the iterator within the constructor. */ 6479 6480 static tree 6481 reshape_init_array_1 (tree elt_type, tree max_index, reshape_iter *d, 6482 tree first_initializer_p, tsubst_flags_t complain) 6483 { 6484 tree new_init; 6485 bool sized_array_p = (max_index && TREE_CONSTANT (max_index)); 6486 unsigned HOST_WIDE_INT max_index_cst = 0; 6487 unsigned HOST_WIDE_INT index; 6488 6489 /* The initializer for an array is always a CONSTRUCTOR. If this is the 6490 outermost CONSTRUCTOR and the element type is non-aggregate, we don't need 6491 to build a new one. But don't reuse if not complaining; if this is 6492 tentative, we might also reshape to another type (95319). */ 6493 bool reuse = (first_initializer_p 6494 && (complain & tf_error) 6495 && !CP_AGGREGATE_TYPE_P (elt_type) 6496 && !TREE_SIDE_EFFECTS (first_initializer_p)); 6497 if (reuse) 6498 new_init = first_initializer_p; 6499 else 6500 new_init = build_constructor (init_list_type_node, NULL); 6501 6502 if (sized_array_p) 6503 { 6504 /* Minus 1 is used for zero sized arrays. */ 6505 if (integer_all_onesp (max_index)) 6506 return new_init; 6507 6508 if (tree_fits_uhwi_p (max_index)) 6509 max_index_cst = tree_to_uhwi (max_index); 6510 /* sizetype is sign extended, not zero extended. */ 6511 else 6512 max_index_cst = tree_to_uhwi (fold_convert (size_type_node, max_index)); 6513 } 6514 6515 /* Loop until there are no more initializers. */ 6516 for (index = 0; 6517 d->cur != d->end && (!sized_array_p || index <= max_index_cst); 6518 ++index) 6519 { 6520 tree elt_init; 6521 constructor_elt *old_cur = d->cur; 6522 6523 if (d->cur->index) 6524 CONSTRUCTOR_IS_DESIGNATED_INIT (new_init) = true; 6525 check_array_designated_initializer (d->cur, index); 6526 elt_init = reshape_init_r (elt_type, d, 6527 /*first_initializer_p=*/NULL_TREE, 6528 complain); 6529 if (elt_init == error_mark_node) 6530 return error_mark_node; 6531 tree idx = size_int (index); 6532 if (reuse) 6533 { 6534 old_cur->index = idx; 6535 old_cur->value = elt_init; 6536 } 6537 else 6538 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init), 6539 idx, elt_init); 6540 if (!TREE_CONSTANT (elt_init)) 6541 TREE_CONSTANT (new_init) = false; 6542 6543 /* This can happen with an invalid initializer (c++/54501). */ 6544 if (d->cur == old_cur && !sized_array_p) 6545 break; 6546 } 6547 6548 return new_init; 6549 } 6550 6551 /* Subroutine of reshape_init_r, processes the initializers for arrays. 6552 Parameters are the same of reshape_init_r. */ 6553 6554 static tree 6555 reshape_init_array (tree type, reshape_iter *d, tree first_initializer_p, 6556 tsubst_flags_t complain) 6557 { 6558 tree max_index = NULL_TREE; 6559 6560 gcc_assert (TREE_CODE (type) == ARRAY_TYPE); 6561 6562 if (TYPE_DOMAIN (type)) 6563 max_index = array_type_nelts (type); 6564 6565 return reshape_init_array_1 (TREE_TYPE (type), max_index, d, 6566 first_initializer_p, complain); 6567 } 6568 6569 /* Subroutine of reshape_init_r, processes the initializers for vectors. 6570 Parameters are the same of reshape_init_r. */ 6571 6572 static tree 6573 reshape_init_vector (tree type, reshape_iter *d, tsubst_flags_t complain) 6574 { 6575 tree max_index = NULL_TREE; 6576 6577 gcc_assert (VECTOR_TYPE_P (type)); 6578 6579 if (COMPOUND_LITERAL_P (d->cur->value)) 6580 { 6581 tree value = d->cur->value; 6582 if (!same_type_p (TREE_TYPE (value), type)) 6583 { 6584 if (complain & tf_error) 6585 error ("invalid type %qT as initializer for a vector of type %qT", 6586 TREE_TYPE (d->cur->value), type); 6587 value = error_mark_node; 6588 } 6589 ++d->cur; 6590 return value; 6591 } 6592 6593 /* For a vector, we initialize it as an array of the appropriate size. */ 6594 if (VECTOR_TYPE_P (type)) 6595 max_index = size_int (TYPE_VECTOR_SUBPARTS (type) - 1); 6596 6597 return reshape_init_array_1 (TREE_TYPE (type), max_index, d, 6598 NULL_TREE, complain); 6599 } 6600 6601 /* Subroutine of reshape_init*: We're initializing an element with TYPE from 6602 INIT, in isolation from any designator or other initializers. */ 6603 6604 static tree 6605 reshape_single_init (tree type, tree init, tsubst_flags_t complain) 6606 { 6607 /* We could also implement this by wrapping init in a new CONSTRUCTOR and 6608 calling reshape_init, but this way can just live on the stack. */ 6609 constructor_elt elt = { /*index=*/NULL_TREE, init }; 6610 reshape_iter iter = { &elt, &elt + 1 }; 6611 return reshape_init_r (type, &iter, 6612 /*first_initializer_p=*/NULL_TREE, 6613 complain); 6614 } 6615 6616 /* Subroutine of reshape_init_r, processes the initializers for classes 6617 or union. Parameters are the same of reshape_init_r. */ 6618 6619 static tree 6620 reshape_init_class (tree type, reshape_iter *d, bool first_initializer_p, 6621 tsubst_flags_t complain) 6622 { 6623 tree field; 6624 tree new_init; 6625 6626 gcc_assert (CLASS_TYPE_P (type)); 6627 6628 /* The initializer for a class is always a CONSTRUCTOR. */ 6629 new_init = build_constructor (init_list_type_node, NULL); 6630 6631 int binfo_idx = -1; 6632 tree binfo = TYPE_BINFO (type); 6633 tree base_binfo = NULL_TREE; 6634 if (cxx_dialect >= cxx17 && uses_template_parms (type)) 6635 { 6636 /* We get here from maybe_aggr_guide for C++20 class template argument 6637 deduction. In this case we need to look through the binfo because a 6638 template doesn't have base fields. */ 6639 binfo_idx = 0; 6640 BINFO_BASE_ITERATE (binfo, binfo_idx, base_binfo); 6641 } 6642 if (base_binfo) 6643 field = base_binfo; 6644 else 6645 field = next_initializable_field (TYPE_FIELDS (type)); 6646 6647 if (!field) 6648 { 6649 /* [dcl.init.aggr] 6650 6651 An initializer for an aggregate member that is an 6652 empty class shall have the form of an empty 6653 initializer-list {}. */ 6654 if (!first_initializer_p) 6655 { 6656 if (complain & tf_error) 6657 error ("initializer for %qT must be brace-enclosed", type); 6658 return error_mark_node; 6659 } 6660 return new_init; 6661 } 6662 6663 /* For C++20 CTAD, handle pack expansions in the base list. */ 6664 tree last_was_pack_expansion = NULL_TREE; 6665 6666 /* Loop through the initializable fields, gathering initializers. */ 6667 while (d->cur != d->end) 6668 { 6669 tree field_init; 6670 constructor_elt *old_cur = d->cur; 6671 bool direct_desig = false; 6672 6673 /* Handle C++20 designated initializers. */ 6674 if (d->cur->index) 6675 { 6676 if (d->cur->index == error_mark_node) 6677 return error_mark_node; 6678 6679 if (TREE_CODE (d->cur->index) == FIELD_DECL) 6680 { 6681 /* We already reshaped this. */ 6682 if (field != d->cur->index) 6683 { 6684 if (tree id = DECL_NAME (d->cur->index)) 6685 gcc_checking_assert (d->cur->index 6686 == get_class_binding (type, id)); 6687 field = d->cur->index; 6688 } 6689 } 6690 else if (TREE_CODE (d->cur->index) == IDENTIFIER_NODE) 6691 { 6692 CONSTRUCTOR_IS_DESIGNATED_INIT (new_init) = true; 6693 field = get_class_binding (type, d->cur->index); 6694 direct_desig = true; 6695 } 6696 else 6697 { 6698 if (complain & tf_error) 6699 error ("%<[%E] =%> used in a GNU-style designated initializer" 6700 " for class %qT", d->cur->index, type); 6701 return error_mark_node; 6702 } 6703 6704 if (!field && ANON_AGGR_TYPE_P (type)) 6705 /* Apparently the designator isn't for a member of this anonymous 6706 struct, so head back to the enclosing class. */ 6707 break; 6708 6709 if (!field || TREE_CODE (field) != FIELD_DECL) 6710 { 6711 if (complain & tf_error) 6712 error ("%qT has no non-static data member named %qD", type, 6713 d->cur->index); 6714 return error_mark_node; 6715 } 6716 6717 /* If the element is an anonymous union object and the initializer 6718 list is a designated-initializer-list, the anonymous union object 6719 is initialized by the designated-initializer-list { D }, where D 6720 is the designated-initializer-clause naming a member of the 6721 anonymous union object. */ 6722 tree ictx = DECL_CONTEXT (field); 6723 if (!same_type_ignoring_top_level_qualifiers_p (ictx, type)) 6724 { 6725 /* Find the anon aggr that is a direct member of TYPE. */ 6726 while (ANON_AGGR_TYPE_P (ictx)) 6727 { 6728 tree cctx = TYPE_CONTEXT (ictx); 6729 if (same_type_ignoring_top_level_qualifiers_p (cctx, type)) 6730 goto found; 6731 ictx = cctx; 6732 } 6733 6734 /* Not found, e.g. FIELD is a member of a base class. */ 6735 if (complain & tf_error) 6736 error ("%qD is not a direct member of %qT", field, type); 6737 return error_mark_node; 6738 6739 found: 6740 /* Now find the TYPE member with that anon aggr type. */ 6741 tree aafield = TYPE_FIELDS (type); 6742 for (; aafield; aafield = TREE_CHAIN (aafield)) 6743 if (TREE_TYPE (aafield) == ictx) 6744 break; 6745 gcc_assert (aafield); 6746 field = aafield; 6747 direct_desig = false; 6748 } 6749 } 6750 6751 /* If we processed all the member of the class, we are done. */ 6752 if (!field) 6753 break; 6754 6755 last_was_pack_expansion = (PACK_EXPANSION_P (TREE_TYPE (field)) 6756 ? field : NULL_TREE); 6757 if (last_was_pack_expansion) 6758 /* Each non-trailing aggregate element that is a pack expansion is 6759 assumed to correspond to no elements of the initializer list. */ 6760 goto continue_; 6761 6762 if (direct_desig) 6763 { 6764 /* The designated field F is initialized from this one element. 6765 6766 Note that we don't want to do this if we found the designator 6767 inside an anon aggr above; we use the normal code to implement: 6768 6769 "If the element is an anonymous union member and the initializer 6770 list is a brace-enclosed designated- initializer-list, the element 6771 is initialized by the designated-initializer-list { D }, where D 6772 is the designated- initializer-clause naming a member of the 6773 anonymous union member." */ 6774 field_init = reshape_single_init (TREE_TYPE (field), 6775 d->cur->value, complain); 6776 d->cur++; 6777 } 6778 else 6779 field_init = reshape_init_r (TREE_TYPE (field), d, 6780 /*first_initializer_p=*/NULL_TREE, 6781 complain); 6782 6783 if (field_init == error_mark_node) 6784 return error_mark_node; 6785 6786 if (d->cur == old_cur && d->cur->index) 6787 { 6788 /* This can happen with an invalid initializer for a flexible 6789 array member (c++/54441). */ 6790 if (complain & tf_error) 6791 error ("invalid initializer for %q#D", field); 6792 return error_mark_node; 6793 } 6794 6795 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init), field, field_init); 6796 6797 /* [dcl.init.aggr] 6798 6799 When a union is initialized with a brace-enclosed 6800 initializer, the braces shall only contain an 6801 initializer for the first member of the union. */ 6802 if (TREE_CODE (type) == UNION_TYPE) 6803 break; 6804 6805 continue_: 6806 if (base_binfo) 6807 { 6808 if (BINFO_BASE_ITERATE (binfo, ++binfo_idx, base_binfo)) 6809 field = base_binfo; 6810 else 6811 field = next_initializable_field (TYPE_FIELDS (type)); 6812 } 6813 else 6814 field = next_initializable_field (DECL_CHAIN (field)); 6815 } 6816 6817 /* A trailing aggregate element that is a pack expansion is assumed to 6818 correspond to all remaining elements of the initializer list (if any). */ 6819 if (last_was_pack_expansion) 6820 { 6821 CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (new_init), 6822 last_was_pack_expansion, d->cur->value); 6823 while (d->cur != d->end) 6824 d->cur++; 6825 } 6826 6827 return new_init; 6828 } 6829 6830 /* Subroutine of reshape_init_r. We're in a context where C99 initializer 6831 designators are not valid; either complain or return true to indicate 6832 that reshape_init_r should return error_mark_node. */ 6833 6834 static bool 6835 has_designator_problem (reshape_iter *d, tsubst_flags_t complain) 6836 { 6837 if (d->cur->index) 6838 { 6839 if (complain & tf_error) 6840 error_at (cp_expr_loc_or_input_loc (d->cur->index), 6841 "C99 designator %qE outside aggregate initializer", 6842 d->cur->index); 6843 else 6844 return true; 6845 } 6846 return false; 6847 } 6848 6849 /* Subroutine of reshape_init, which processes a single initializer (part of 6850 a CONSTRUCTOR). TYPE is the type of the variable being initialized, D is the 6851 iterator within the CONSTRUCTOR which points to the initializer to process. 6852 If this is the first initializer of the outermost CONSTRUCTOR node, 6853 FIRST_INITIALIZER_P is that CONSTRUCTOR; otherwise, it is NULL_TREE. */ 6854 6855 static tree 6856 reshape_init_r (tree type, reshape_iter *d, tree first_initializer_p, 6857 tsubst_flags_t complain) 6858 { 6859 tree init = d->cur->value; 6860 6861 if (error_operand_p (init)) 6862 return error_mark_node; 6863 6864 if (first_initializer_p && !CP_AGGREGATE_TYPE_P (type) 6865 && has_designator_problem (d, complain)) 6866 return error_mark_node; 6867 6868 tree stripped_init = tree_strip_any_location_wrapper (init); 6869 6870 if (TREE_CODE (type) == COMPLEX_TYPE) 6871 { 6872 /* A complex type can be initialized from one or two initializers, 6873 but braces are not elided. */ 6874 d->cur++; 6875 if (BRACE_ENCLOSED_INITIALIZER_P (stripped_init)) 6876 { 6877 if (CONSTRUCTOR_NELTS (stripped_init) > 2) 6878 { 6879 if (complain & tf_error) 6880 error ("too many initializers for %qT", type); 6881 else 6882 return error_mark_node; 6883 } 6884 } 6885 else if (first_initializer_p && d->cur != d->end) 6886 { 6887 vec<constructor_elt, va_gc> *v = 0; 6888 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, init); 6889 CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, d->cur->value); 6890 if (has_designator_problem (d, complain)) 6891 return error_mark_node; 6892 d->cur++; 6893 init = build_constructor (init_list_type_node, v); 6894 } 6895 return init; 6896 } 6897 6898 /* A non-aggregate type is always initialized with a single 6899 initializer. */ 6900 if (!CP_AGGREGATE_TYPE_P (type) 6901 /* As is an array with dependent bound, which we can see 6902 during C++20 aggregate CTAD. */ 6903 || (cxx_dialect >= cxx20 6904 && TREE_CODE (type) == ARRAY_TYPE 6905 && uses_template_parms (TYPE_DOMAIN (type)))) 6906 { 6907 /* It is invalid to initialize a non-aggregate type with a 6908 brace-enclosed initializer before C++0x. 6909 We need to check for BRACE_ENCLOSED_INITIALIZER_P here because 6910 of g++.old-deja/g++.mike/p7626.C: a pointer-to-member constant is 6911 a CONSTRUCTOR (with a record type). */ 6912 if (TREE_CODE (stripped_init) == CONSTRUCTOR 6913 /* Don't complain about a capture-init. */ 6914 && !CONSTRUCTOR_IS_DIRECT_INIT (stripped_init) 6915 && BRACE_ENCLOSED_INITIALIZER_P (stripped_init)) /* p7626.C */ 6916 { 6917 if (SCALAR_TYPE_P (type)) 6918 { 6919 if (cxx_dialect < cxx11) 6920 { 6921 if (complain & tf_error) 6922 error ("braces around scalar initializer for type %qT", 6923 type); 6924 init = error_mark_node; 6925 } 6926 else if (first_initializer_p 6927 || (CONSTRUCTOR_NELTS (stripped_init) > 0 6928 && (BRACE_ENCLOSED_INITIALIZER_P 6929 (CONSTRUCTOR_ELT (stripped_init,0)->value)))) 6930 { 6931 if (complain & tf_error) 6932 error ("too many braces around scalar initializer " 6933 "for type %qT", type); 6934 init = error_mark_node; 6935 } 6936 } 6937 else 6938 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS); 6939 } 6940 6941 d->cur++; 6942 return init; 6943 } 6944 6945 /* "If T is a class type and the initializer list has a single element of 6946 type cv U, where U is T or a class derived from T, the object is 6947 initialized from that element." Even if T is an aggregate. */ 6948 if (cxx_dialect >= cxx11 && (CLASS_TYPE_P (type) || VECTOR_TYPE_P (type)) 6949 && first_initializer_p 6950 /* But not if it's a designated init. */ 6951 && !d->cur->index 6952 && d->end - d->cur == 1 6953 && reference_related_p (type, TREE_TYPE (init))) 6954 { 6955 d->cur++; 6956 return init; 6957 } 6958 6959 /* [dcl.init.aggr] 6960 6961 All implicit type conversions (clause _conv_) are considered when 6962 initializing the aggregate member with an initializer from an 6963 initializer-list. If the initializer can initialize a member, 6964 the member is initialized. Otherwise, if the member is itself a 6965 non-empty subaggregate, brace elision is assumed and the 6966 initializer is considered for the initialization of the first 6967 member of the subaggregate. */ 6968 if ((TREE_CODE (init) != CONSTRUCTOR || COMPOUND_LITERAL_P (init)) 6969 /* But don't try this for the first initializer, since that would be 6970 looking through the outermost braces; A a2 = { a1 }; is not a 6971 valid aggregate initialization. */ 6972 && !first_initializer_p 6973 && (same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (init)) 6974 || can_convert_arg (type, TREE_TYPE (init), init, LOOKUP_NORMAL, 6975 complain))) 6976 { 6977 d->cur++; 6978 return init; 6979 } 6980 6981 /* [dcl.init.string] 6982 6983 A char array (whether plain char, signed char, or unsigned char) 6984 can be initialized by a string-literal (optionally enclosed in 6985 braces); a wchar_t array can be initialized by a wide 6986 string-literal (optionally enclosed in braces). */ 6987 if (TREE_CODE (type) == ARRAY_TYPE 6988 && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type)))) 6989 { 6990 tree str_init = init; 6991 tree stripped_str_init = stripped_init; 6992 reshape_iter stripd = {}; 6993 6994 /* Strip one level of braces if and only if they enclose a single 6995 element (as allowed by [dcl.init.string]). */ 6996 if (!first_initializer_p 6997 && TREE_CODE (stripped_str_init) == CONSTRUCTOR 6998 && CONSTRUCTOR_NELTS (stripped_str_init) == 1) 6999 { 7000 stripd.cur = CONSTRUCTOR_ELT (stripped_str_init, 0); 7001 str_init = stripd.cur->value; 7002 stripped_str_init = tree_strip_any_location_wrapper (str_init); 7003 } 7004 7005 /* If it's a string literal, then it's the initializer for the array 7006 as a whole. Otherwise, continue with normal initialization for 7007 array types (one value per array element). */ 7008 if (TREE_CODE (stripped_str_init) == STRING_CST) 7009 { 7010 if ((first_initializer_p && has_designator_problem (d, complain)) 7011 || (stripd.cur && has_designator_problem (&stripd, complain))) 7012 return error_mark_node; 7013 d->cur++; 7014 return str_init; 7015 } 7016 } 7017 7018 /* The following cases are about aggregates. If we are not within a full 7019 initializer already, and there is not a CONSTRUCTOR, it means that there 7020 is a missing set of braces (that is, we are processing the case for 7021 which reshape_init exists). */ 7022 bool braces_elided_p = false; 7023 if (!first_initializer_p) 7024 { 7025 if (TREE_CODE (stripped_init) == CONSTRUCTOR) 7026 { 7027 tree init_type = TREE_TYPE (init); 7028 if (init_type && TYPE_PTRMEMFUNC_P (init_type)) 7029 /* There is no need to call reshape_init for pointer-to-member 7030 function initializers, as they are always constructed correctly 7031 by the front end. Here we have e.g. {.__pfn=0B, .__delta=0}, 7032 which is missing outermost braces. We should warn below, and 7033 one of the routines below will wrap it in additional { }. */; 7034 /* For a nested compound literal, proceed to specialized routines, 7035 to handle initialization of arrays and similar. */ 7036 else if (COMPOUND_LITERAL_P (stripped_init)) 7037 gcc_assert (!BRACE_ENCLOSED_INITIALIZER_P (stripped_init)); 7038 /* If we have an unresolved designator, we need to find the member it 7039 designates within TYPE, so proceed to the routines below. For 7040 FIELD_DECL or INTEGER_CST designators, we're already initializing 7041 the designated element. */ 7042 else if (d->cur->index 7043 && TREE_CODE (d->cur->index) == IDENTIFIER_NODE) 7044 /* Brace elision with designators is only permitted for anonymous 7045 aggregates. */ 7046 gcc_checking_assert (ANON_AGGR_TYPE_P (type)); 7047 /* A CONSTRUCTOR of the target's type is a previously 7048 digested initializer. */ 7049 else if (same_type_ignoring_top_level_qualifiers_p (type, init_type)) 7050 { 7051 ++d->cur; 7052 return init; 7053 } 7054 else 7055 { 7056 /* Something that hasn't been reshaped yet. */ 7057 ++d->cur; 7058 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (stripped_init)); 7059 return reshape_init (type, init, complain); 7060 } 7061 } 7062 7063 if (complain & tf_warning) 7064 warning (OPT_Wmissing_braces, 7065 "missing braces around initializer for %qT", 7066 type); 7067 braces_elided_p = true; 7068 } 7069 7070 /* Dispatch to specialized routines. */ 7071 tree new_init; 7072 if (CLASS_TYPE_P (type)) 7073 new_init = reshape_init_class (type, d, first_initializer_p, complain); 7074 else if (TREE_CODE (type) == ARRAY_TYPE) 7075 new_init = reshape_init_array (type, d, first_initializer_p, complain); 7076 else if (VECTOR_TYPE_P (type)) 7077 new_init = reshape_init_vector (type, d, complain); 7078 else 7079 gcc_unreachable(); 7080 7081 if (braces_elided_p 7082 && TREE_CODE (new_init) == CONSTRUCTOR) 7083 CONSTRUCTOR_BRACES_ELIDED_P (new_init) = true; 7084 7085 return new_init; 7086 } 7087 7088 /* Undo the brace-elision allowed by [dcl.init.aggr] in a 7089 brace-enclosed aggregate initializer. 7090 7091 INIT is the CONSTRUCTOR containing the list of initializers describing 7092 a brace-enclosed initializer for an entity of the indicated aggregate TYPE. 7093 It may not presently match the shape of the TYPE; for example: 7094 7095 struct S { int a; int b; }; 7096 struct S a[] = { 1, 2, 3, 4 }; 7097 7098 Here INIT will hold a vector of four elements, rather than a 7099 vector of two elements, each itself a vector of two elements. This 7100 routine transforms INIT from the former form into the latter. The 7101 revised CONSTRUCTOR node is returned. */ 7102 7103 tree 7104 reshape_init (tree type, tree init, tsubst_flags_t complain) 7105 { 7106 vec<constructor_elt, va_gc> *v; 7107 reshape_iter d; 7108 tree new_init; 7109 7110 gcc_assert (BRACE_ENCLOSED_INITIALIZER_P (init)); 7111 7112 v = CONSTRUCTOR_ELTS (init); 7113 7114 /* An empty constructor does not need reshaping, and it is always a valid 7115 initializer. */ 7116 if (vec_safe_is_empty (v)) 7117 return init; 7118 7119 /* Brace elision is not performed for a CONSTRUCTOR representing 7120 parenthesized aggregate initialization. */ 7121 if (CONSTRUCTOR_IS_PAREN_INIT (init)) 7122 { 7123 tree elt = (*v)[0].value; 7124 /* If we're initializing a char array from a string-literal that is 7125 enclosed in braces, unwrap it here. */ 7126 if (TREE_CODE (type) == ARRAY_TYPE 7127 && vec_safe_length (v) == 1 7128 && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type))) 7129 && TREE_CODE (tree_strip_any_location_wrapper (elt)) == STRING_CST) 7130 return elt; 7131 return init; 7132 } 7133 7134 /* Handle [dcl.init.list] direct-list-initialization from 7135 single element of enumeration with a fixed underlying type. */ 7136 if (is_direct_enum_init (type, init)) 7137 { 7138 tree elt = CONSTRUCTOR_ELT (init, 0)->value; 7139 type = cv_unqualified (type); 7140 if (check_narrowing (ENUM_UNDERLYING_TYPE (type), elt, complain)) 7141 { 7142 warning_sentinel w (warn_useless_cast); 7143 warning_sentinel w2 (warn_ignored_qualifiers); 7144 return cp_build_c_cast (input_location, type, elt, 7145 tf_warning_or_error); 7146 } 7147 else 7148 return error_mark_node; 7149 } 7150 7151 /* Recurse on this CONSTRUCTOR. */ 7152 d.cur = &(*v)[0]; 7153 d.end = d.cur + v->length (); 7154 7155 new_init = reshape_init_r (type, &d, init, complain); 7156 if (new_init == error_mark_node) 7157 return error_mark_node; 7158 7159 /* Make sure all the element of the constructor were used. Otherwise, 7160 issue an error about exceeding initializers. */ 7161 if (d.cur != d.end) 7162 { 7163 if (complain & tf_error) 7164 error ("too many initializers for %qT", type); 7165 return error_mark_node; 7166 } 7167 7168 if (CONSTRUCTOR_IS_DIRECT_INIT (init) 7169 && BRACE_ENCLOSED_INITIALIZER_P (new_init)) 7170 CONSTRUCTOR_IS_DIRECT_INIT (new_init) = true; 7171 if (CONSTRUCTOR_IS_DESIGNATED_INIT (init) 7172 && BRACE_ENCLOSED_INITIALIZER_P (new_init)) 7173 CONSTRUCTOR_IS_DESIGNATED_INIT (new_init) = true; 7174 7175 return new_init; 7176 } 7177 7178 /* Verify array initializer. Returns true if errors have been reported. */ 7179 7180 bool 7181 check_array_initializer (tree decl, tree type, tree init) 7182 { 7183 tree element_type = TREE_TYPE (type); 7184 7185 /* Structured binding when initialized with an array type needs 7186 to have complete type. */ 7187 if (decl 7188 && DECL_DECOMPOSITION_P (decl) 7189 && !DECL_DECOMP_BASE (decl) 7190 && !COMPLETE_TYPE_P (type)) 7191 { 7192 error_at (DECL_SOURCE_LOCATION (decl), 7193 "structured binding has incomplete type %qT", type); 7194 TREE_TYPE (decl) = error_mark_node; 7195 return true; 7196 } 7197 7198 /* The array type itself need not be complete, because the 7199 initializer may tell us how many elements are in the array. 7200 But, the elements of the array must be complete. */ 7201 if (!COMPLETE_TYPE_P (complete_type (element_type))) 7202 { 7203 if (decl) 7204 error_at (DECL_SOURCE_LOCATION (decl), 7205 "elements of array %q#D have incomplete type", decl); 7206 else 7207 error ("elements of array %q#T have incomplete type", type); 7208 return true; 7209 } 7210 7211 location_t loc = (decl ? location_of (decl) : input_location); 7212 if (!verify_type_context (loc, TCTX_ARRAY_ELEMENT, element_type)) 7213 return true; 7214 7215 /* A compound literal can't have variable size. */ 7216 if (init && !decl 7217 && ((COMPLETE_TYPE_P (type) && !TREE_CONSTANT (TYPE_SIZE (type))) 7218 || !TREE_CONSTANT (TYPE_SIZE (element_type)))) 7219 { 7220 error ("variable-sized compound literal"); 7221 return true; 7222 } 7223 return false; 7224 } 7225 7226 /* Subroutine of check_initializer; args are passed down from that function. 7227 Set stmts_are_full_exprs_p to 1 across a call to build_aggr_init. */ 7228 7229 static tree 7230 build_aggr_init_full_exprs (tree decl, tree init, int flags) 7231 7232 { 7233 gcc_assert (stmts_are_full_exprs_p ()); 7234 return build_aggr_init (decl, init, flags, tf_warning_or_error); 7235 } 7236 7237 /* Verify INIT (the initializer for DECL), and record the 7238 initialization in DECL_INITIAL, if appropriate. CLEANUP is as for 7239 grok_reference_init. 7240 7241 If the return value is non-NULL, it is an expression that must be 7242 evaluated dynamically to initialize DECL. */ 7243 7244 static tree 7245 check_initializer (tree decl, tree init, int flags, vec<tree, va_gc> **cleanups) 7246 { 7247 tree type; 7248 tree init_code = NULL; 7249 tree core_type; 7250 7251 /* Things that are going to be initialized need to have complete 7252 type. */ 7253 TREE_TYPE (decl) = type = complete_type (TREE_TYPE (decl)); 7254 7255 if (DECL_HAS_VALUE_EXPR_P (decl)) 7256 { 7257 /* A variable with DECL_HAS_VALUE_EXPR_P set is just a placeholder, 7258 it doesn't have storage to be initialized. */ 7259 gcc_assert (init == NULL_TREE); 7260 return NULL_TREE; 7261 } 7262 7263 if (type == error_mark_node) 7264 /* We will have already complained. */ 7265 return NULL_TREE; 7266 7267 if (TREE_CODE (type) == ARRAY_TYPE) 7268 { 7269 if (check_array_initializer (decl, type, init)) 7270 return NULL_TREE; 7271 } 7272 else if (!COMPLETE_TYPE_P (type)) 7273 { 7274 error_at (DECL_SOURCE_LOCATION (decl), 7275 "%q#D has incomplete type", decl); 7276 TREE_TYPE (decl) = error_mark_node; 7277 return NULL_TREE; 7278 } 7279 else 7280 /* There is no way to make a variable-sized class type in GNU C++. */ 7281 gcc_assert (TREE_CONSTANT (TYPE_SIZE (type))); 7282 7283 if (init && BRACE_ENCLOSED_INITIALIZER_P (init)) 7284 { 7285 int init_len = CONSTRUCTOR_NELTS (init); 7286 if (SCALAR_TYPE_P (type)) 7287 { 7288 if (init_len == 0) 7289 { 7290 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS); 7291 init = build_zero_init (type, NULL_TREE, false); 7292 } 7293 else if (init_len != 1 && TREE_CODE (type) != COMPLEX_TYPE) 7294 { 7295 error_at (cp_expr_loc_or_loc (init, DECL_SOURCE_LOCATION (decl)), 7296 "scalar object %qD requires one element in " 7297 "initializer", decl); 7298 TREE_TYPE (decl) = error_mark_node; 7299 return NULL_TREE; 7300 } 7301 } 7302 } 7303 7304 if (TREE_CODE (decl) == CONST_DECL) 7305 { 7306 gcc_assert (!TYPE_REF_P (type)); 7307 7308 DECL_INITIAL (decl) = init; 7309 7310 gcc_assert (init != NULL_TREE); 7311 init = NULL_TREE; 7312 } 7313 else if (!init && DECL_REALLY_EXTERN (decl)) 7314 ; 7315 else if (init || type_build_ctor_call (type) 7316 || TYPE_REF_P (type)) 7317 { 7318 if (TYPE_REF_P (type)) 7319 { 7320 init = grok_reference_init (decl, type, init, flags); 7321 flags |= LOOKUP_ALREADY_DIGESTED; 7322 } 7323 else if (!init) 7324 check_for_uninitialized_const_var (decl, /*constexpr_context_p=*/false, 7325 tf_warning_or_error); 7326 /* Do not reshape constructors of vectors (they don't need to be 7327 reshaped. */ 7328 else if (BRACE_ENCLOSED_INITIALIZER_P (init)) 7329 { 7330 if (is_std_init_list (type)) 7331 { 7332 init = perform_implicit_conversion (type, init, 7333 tf_warning_or_error); 7334 flags |= LOOKUP_ALREADY_DIGESTED; 7335 } 7336 else if (TYPE_NON_AGGREGATE_CLASS (type)) 7337 { 7338 /* Don't reshape if the class has constructors. */ 7339 if (cxx_dialect == cxx98) 7340 error_at (cp_expr_loc_or_loc (init, DECL_SOURCE_LOCATION (decl)), 7341 "in C++98 %qD must be initialized by " 7342 "constructor, not by %<{...}%>", 7343 decl); 7344 } 7345 else if (VECTOR_TYPE_P (type) && TYPE_VECTOR_OPAQUE (type)) 7346 { 7347 error ("opaque vector types cannot be initialized"); 7348 init = error_mark_node; 7349 } 7350 else 7351 { 7352 init = reshape_init (type, init, tf_warning_or_error); 7353 flags |= LOOKUP_NO_NARROWING; 7354 } 7355 } 7356 /* [dcl.init] "Otherwise, if the destination type is an array, the object 7357 is initialized as follows..." So handle things like 7358 7359 int a[](1, 2, 3); 7360 7361 which is permitted in C++20 by P0960. */ 7362 else if (TREE_CODE (init) == TREE_LIST 7363 && TREE_TYPE (init) == NULL_TREE 7364 && TREE_CODE (type) == ARRAY_TYPE 7365 && !DECL_DECOMPOSITION_P (decl) 7366 && (cxx_dialect >= cxx20)) 7367 init = do_aggregate_paren_init (init, type); 7368 else if (TREE_CODE (init) == TREE_LIST 7369 && TREE_TYPE (init) != unknown_type_node 7370 && !MAYBE_CLASS_TYPE_P (type)) 7371 { 7372 gcc_assert (TREE_CODE (decl) != RESULT_DECL); 7373 7374 /* We get here with code like `int a (2);' */ 7375 init = build_x_compound_expr_from_list (init, ELK_INIT, 7376 tf_warning_or_error); 7377 } 7378 7379 /* If DECL has an array type without a specific bound, deduce the 7380 array size from the initializer. */ 7381 maybe_deduce_size_from_array_init (decl, init); 7382 type = TREE_TYPE (decl); 7383 if (type == error_mark_node) 7384 return NULL_TREE; 7385 7386 if (((type_build_ctor_call (type) || CLASS_TYPE_P (type)) 7387 && !(flags & LOOKUP_ALREADY_DIGESTED) 7388 && !(init && BRACE_ENCLOSED_INITIALIZER_P (init) 7389 && CP_AGGREGATE_TYPE_P (type) 7390 && (CLASS_TYPE_P (type) 7391 /* The call to build_aggr_init below could end up 7392 calling build_vec_init, which may break when we 7393 are processing a template. */ 7394 || processing_template_decl 7395 || !TYPE_NEEDS_CONSTRUCTING (type) 7396 || type_has_extended_temps (type)))) 7397 || (DECL_DECOMPOSITION_P (decl) && TREE_CODE (type) == ARRAY_TYPE)) 7398 { 7399 init_code = build_aggr_init_full_exprs (decl, init, flags); 7400 7401 /* A constructor call is a non-trivial initializer even if 7402 it isn't explicitly written. */ 7403 if (TREE_SIDE_EFFECTS (init_code)) 7404 DECL_NONTRIVIALLY_INITIALIZED_P (decl) = true; 7405 7406 /* If this is a constexpr initializer, expand_default_init will 7407 have returned an INIT_EXPR rather than a CALL_EXPR. In that 7408 case, pull the initializer back out and pass it down into 7409 store_init_value. */ 7410 while (true) 7411 { 7412 if (TREE_CODE (init_code) == EXPR_STMT 7413 || TREE_CODE (init_code) == STMT_EXPR 7414 || TREE_CODE (init_code) == CONVERT_EXPR) 7415 init_code = TREE_OPERAND (init_code, 0); 7416 else if (TREE_CODE (init_code) == BIND_EXPR) 7417 init_code = BIND_EXPR_BODY (init_code); 7418 else 7419 break; 7420 } 7421 if (TREE_CODE (init_code) == INIT_EXPR) 7422 { 7423 /* In C++20, the call to build_aggr_init could have created 7424 an INIT_EXPR with a CONSTRUCTOR as the RHS to handle 7425 A(1, 2). */ 7426 tree rhs = TREE_OPERAND (init_code, 1); 7427 if (processing_template_decl && TREE_CODE (rhs) == TARGET_EXPR) 7428 /* Avoid leaking TARGET_EXPR into template trees. */ 7429 rhs = build_implicit_conv_flags (type, init, flags); 7430 init = rhs; 7431 7432 init_code = NULL_TREE; 7433 /* Don't call digest_init; it's unnecessary and will complain 7434 about aggregate initialization of non-aggregate classes. */ 7435 flags |= LOOKUP_ALREADY_DIGESTED; 7436 } 7437 else if (DECL_DECLARED_CONSTEXPR_P (decl) 7438 || DECL_DECLARED_CONSTINIT_P (decl)) 7439 { 7440 /* Declared constexpr or constinit, but no suitable initializer; 7441 massage init appropriately so we can pass it into 7442 store_init_value for the error. */ 7443 tree new_init = NULL_TREE; 7444 if (!processing_template_decl 7445 && TREE_CODE (init_code) == CALL_EXPR) 7446 new_init = build_cplus_new (type, init_code, tf_none); 7447 else if (CLASS_TYPE_P (type) 7448 && (!init || TREE_CODE (init) == TREE_LIST)) 7449 new_init = build_functional_cast (input_location, type, 7450 init, tf_none); 7451 if (new_init) 7452 { 7453 init = new_init; 7454 if (TREE_CODE (init) == TARGET_EXPR 7455 && !(flags & LOOKUP_ONLYCONVERTING)) 7456 TARGET_EXPR_DIRECT_INIT_P (init) = true; 7457 } 7458 init_code = NULL_TREE; 7459 } 7460 else 7461 init = NULL_TREE; 7462 } 7463 7464 if (init && TREE_CODE (init) != TREE_VEC) 7465 { 7466 init_code = store_init_value (decl, init, cleanups, flags); 7467 7468 if (DECL_INITIAL (decl) 7469 && TREE_CODE (DECL_INITIAL (decl)) == CONSTRUCTOR 7470 && !vec_safe_is_empty (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)))) 7471 { 7472 tree elt = CONSTRUCTOR_ELTS (DECL_INITIAL (decl))->last ().value; 7473 if (TREE_CODE (TREE_TYPE (elt)) == ARRAY_TYPE 7474 && TYPE_SIZE (TREE_TYPE (elt)) == NULL_TREE) 7475 cp_complete_array_type (&TREE_TYPE (elt), elt, false); 7476 } 7477 7478 if (pedantic && TREE_CODE (type) == ARRAY_TYPE 7479 && DECL_INITIAL (decl) 7480 && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST 7481 && PAREN_STRING_LITERAL_P (DECL_INITIAL (decl))) 7482 warning_at (cp_expr_loc_or_loc (DECL_INITIAL (decl), 7483 DECL_SOURCE_LOCATION (decl)), 7484 0, "array %qD initialized by parenthesized " 7485 "string literal %qE", 7486 decl, DECL_INITIAL (decl)); 7487 init = NULL_TREE; 7488 } 7489 } 7490 else 7491 { 7492 if (CLASS_TYPE_P (core_type = strip_array_types (type)) 7493 && (CLASSTYPE_READONLY_FIELDS_NEED_INIT (core_type) 7494 || CLASSTYPE_REF_FIELDS_NEED_INIT (core_type))) 7495 diagnose_uninitialized_cst_or_ref_member (core_type, /*using_new=*/false, 7496 /*complain=*/true); 7497 7498 check_for_uninitialized_const_var (decl, /*constexpr_context_p=*/false, 7499 tf_warning_or_error); 7500 } 7501 7502 if (init && init != error_mark_node) 7503 init_code = build2 (INIT_EXPR, type, decl, init); 7504 7505 if (init_code && !TREE_SIDE_EFFECTS (init_code) 7506 && init_code != error_mark_node) 7507 init_code = NULL_TREE; 7508 7509 if (init_code) 7510 { 7511 /* We might have set these in cp_finish_decl. */ 7512 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = false; 7513 TREE_CONSTANT (decl) = false; 7514 } 7515 7516 if (init_code 7517 && DECL_IN_AGGR_P (decl) 7518 && DECL_INITIALIZED_IN_CLASS_P (decl)) 7519 { 7520 static int explained = 0; 7521 7522 if (cxx_dialect < cxx11) 7523 error ("initializer invalid for static member with constructor"); 7524 else if (cxx_dialect < cxx17) 7525 error ("non-constant in-class initialization invalid for static " 7526 "member %qD", decl); 7527 else 7528 error ("non-constant in-class initialization invalid for non-inline " 7529 "static member %qD", decl); 7530 if (!explained) 7531 { 7532 inform (input_location, 7533 "(an out of class initialization is required)"); 7534 explained = 1; 7535 } 7536 return NULL_TREE; 7537 } 7538 7539 return init_code; 7540 } 7541 7542 /* If DECL is not a local variable, give it RTL. */ 7543 7544 static void 7545 make_rtl_for_nonlocal_decl (tree decl, tree init, const char* asmspec) 7546 { 7547 int toplev = toplevel_bindings_p (); 7548 int defer_p; 7549 7550 /* Set the DECL_ASSEMBLER_NAME for the object. */ 7551 if (asmspec) 7552 { 7553 /* The `register' keyword, when used together with an 7554 asm-specification, indicates that the variable should be 7555 placed in a particular register. */ 7556 if (VAR_P (decl) && DECL_REGISTER (decl)) 7557 { 7558 set_user_assembler_name (decl, asmspec); 7559 DECL_HARD_REGISTER (decl) = 1; 7560 } 7561 else 7562 { 7563 if (TREE_CODE (decl) == FUNCTION_DECL 7564 && fndecl_built_in_p (decl, BUILT_IN_NORMAL)) 7565 set_builtin_user_assembler_name (decl, asmspec); 7566 set_user_assembler_name (decl, asmspec); 7567 if (DECL_LOCAL_DECL_P (decl)) 7568 if (auto ns_decl = DECL_LOCAL_DECL_ALIAS (decl)) 7569 /* We have to propagate the name to the ns-alias. 7570 This is horrible, as we're affecting a 7571 possibly-shared decl. Again, a one-true-decl 7572 model breaks down. */ 7573 if (ns_decl != error_mark_node) 7574 set_user_assembler_name (ns_decl, asmspec); 7575 } 7576 } 7577 7578 /* Handle non-variables up front. */ 7579 if (!VAR_P (decl)) 7580 { 7581 rest_of_decl_compilation (decl, toplev, at_eof); 7582 return; 7583 } 7584 7585 /* If we see a class member here, it should be a static data 7586 member. */ 7587 if (DECL_LANG_SPECIFIC (decl) && DECL_IN_AGGR_P (decl)) 7588 { 7589 gcc_assert (TREE_STATIC (decl)); 7590 /* An in-class declaration of a static data member should be 7591 external; it is only a declaration, and not a definition. */ 7592 if (init == NULL_TREE) 7593 gcc_assert (DECL_EXTERNAL (decl) 7594 || !TREE_PUBLIC (decl)); 7595 } 7596 7597 /* We don't create any RTL for local variables. */ 7598 if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl)) 7599 return; 7600 7601 /* We defer emission of local statics until the corresponding 7602 DECL_EXPR is expanded. But with constexpr its function might never 7603 be expanded, so go ahead and tell cgraph about the variable now. */ 7604 defer_p = ((DECL_FUNCTION_SCOPE_P (decl) 7605 && !var_in_maybe_constexpr_fn (decl)) 7606 || DECL_VIRTUAL_P (decl)); 7607 7608 /* Defer template instantiations. */ 7609 if (DECL_LANG_SPECIFIC (decl) 7610 && DECL_IMPLICIT_INSTANTIATION (decl)) 7611 defer_p = 1; 7612 7613 /* If we're not deferring, go ahead and assemble the variable. */ 7614 if (!defer_p) 7615 rest_of_decl_compilation (decl, toplev, at_eof); 7616 } 7617 7618 /* walk_tree helper for wrap_temporary_cleanups, below. */ 7619 7620 static tree 7621 wrap_cleanups_r (tree *stmt_p, int *walk_subtrees, void *data) 7622 { 7623 /* Stop at types or full-expression boundaries. */ 7624 if (TYPE_P (*stmt_p) 7625 || TREE_CODE (*stmt_p) == CLEANUP_POINT_EXPR) 7626 { 7627 *walk_subtrees = 0; 7628 return NULL_TREE; 7629 } 7630 7631 if (TREE_CODE (*stmt_p) == TARGET_EXPR) 7632 { 7633 tree guard = (tree)data; 7634 tree tcleanup = TARGET_EXPR_CLEANUP (*stmt_p); 7635 7636 if (tcleanup && !CLEANUP_EH_ONLY (*stmt_p) 7637 && !expr_noexcept_p (tcleanup, tf_none)) 7638 { 7639 tcleanup = build2 (TRY_CATCH_EXPR, void_type_node, tcleanup, guard); 7640 /* Tell honor_protect_cleanup_actions to handle this as a separate 7641 cleanup. */ 7642 TRY_CATCH_IS_CLEANUP (tcleanup) = 1; 7643 TARGET_EXPR_CLEANUP (*stmt_p) = tcleanup; 7644 } 7645 } 7646 7647 return NULL_TREE; 7648 } 7649 7650 /* We're initializing a local variable which has a cleanup GUARD. If there 7651 are any temporaries used in the initializer INIT of this variable, we 7652 need to wrap their cleanups with TRY_CATCH_EXPR (, GUARD) so that the 7653 variable will be cleaned up properly if one of them throws. 7654 7655 Unfortunately, there's no way to express this properly in terms of 7656 nesting, as the regions for the temporaries overlap the region for the 7657 variable itself; if there are two temporaries, the variable needs to be 7658 the first thing destroyed if either of them throws. However, we only 7659 want to run the variable's cleanup if it actually got constructed. So 7660 we need to guard the temporary cleanups with the variable's cleanup if 7661 they are run on the normal path, but not if they are run on the 7662 exceptional path. We implement this by telling 7663 honor_protect_cleanup_actions to strip the variable cleanup from the 7664 exceptional path. 7665 7666 Another approach could be to make the variable cleanup region enclose 7667 initialization, but depend on a flag to indicate that the variable is 7668 initialized; that's effectively what we do for arrays. But the current 7669 approach works fine for non-arrays, and has no code overhead in the usual 7670 case where the temporary destructors are noexcept. */ 7671 7672 static void 7673 wrap_temporary_cleanups (tree init, tree guard) 7674 { 7675 if (TREE_CODE (guard) == BIND_EXPR) 7676 { 7677 /* An array cleanup region already encloses any temporary cleanups, 7678 don't wrap it around them again. */ 7679 gcc_checking_assert (BIND_EXPR_VEC_DTOR (guard)); 7680 return; 7681 } 7682 cp_walk_tree_without_duplicates (&init, wrap_cleanups_r, (void *)guard); 7683 } 7684 7685 /* Generate code to initialize DECL (a local variable). */ 7686 7687 static void 7688 initialize_local_var (tree decl, tree init) 7689 { 7690 tree type = TREE_TYPE (decl); 7691 tree cleanup; 7692 int already_used; 7693 7694 gcc_assert (VAR_P (decl) 7695 || TREE_CODE (decl) == RESULT_DECL); 7696 gcc_assert (!TREE_STATIC (decl)); 7697 7698 if (DECL_SIZE (decl) == NULL_TREE) 7699 { 7700 /* If we used it already as memory, it must stay in memory. */ 7701 DECL_INITIAL (decl) = NULL_TREE; 7702 TREE_ADDRESSABLE (decl) = TREE_USED (decl); 7703 return; 7704 } 7705 7706 if (type == error_mark_node) 7707 return; 7708 7709 /* Compute and store the initial value. */ 7710 already_used = TREE_USED (decl) || TREE_USED (type); 7711 if (TREE_USED (type)) 7712 DECL_READ_P (decl) = 1; 7713 7714 /* Generate a cleanup, if necessary. */ 7715 cleanup = cxx_maybe_build_cleanup (decl, tf_warning_or_error); 7716 7717 /* Perform the initialization. */ 7718 if (init) 7719 { 7720 tree rinit = (TREE_CODE (init) == INIT_EXPR 7721 ? TREE_OPERAND (init, 1) : NULL_TREE); 7722 if (rinit && !TREE_SIDE_EFFECTS (rinit) 7723 && TREE_OPERAND (init, 0) == decl) 7724 { 7725 /* Stick simple initializers in DECL_INITIAL so that 7726 -Wno-init-self works (c++/34772). */ 7727 DECL_INITIAL (decl) = rinit; 7728 7729 if (warn_init_self && TYPE_REF_P (type)) 7730 { 7731 STRIP_NOPS (rinit); 7732 if (rinit == decl) 7733 warning_at (DECL_SOURCE_LOCATION (decl), 7734 OPT_Winit_self, 7735 "reference %qD is initialized with itself", decl); 7736 } 7737 } 7738 else 7739 { 7740 int saved_stmts_are_full_exprs_p; 7741 7742 /* If we're only initializing a single object, guard the 7743 destructors of any temporaries used in its initializer with 7744 its destructor. */ 7745 if (cleanup) 7746 wrap_temporary_cleanups (init, cleanup); 7747 7748 gcc_assert (building_stmt_list_p ()); 7749 saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p (); 7750 current_stmt_tree ()->stmts_are_full_exprs_p = 1; 7751 finish_expr_stmt (init); 7752 current_stmt_tree ()->stmts_are_full_exprs_p = 7753 saved_stmts_are_full_exprs_p; 7754 } 7755 } 7756 7757 /* Set this to 0 so we can tell whether an aggregate which was 7758 initialized was ever used. Don't do this if it has a 7759 destructor, so we don't complain about the 'resource 7760 allocation is initialization' idiom. Now set 7761 attribute((unused)) on types so decls of that type will be 7762 marked used. (see TREE_USED, above.) */ 7763 if (TYPE_NEEDS_CONSTRUCTING (type) 7764 && ! already_used 7765 && TYPE_HAS_TRIVIAL_DESTRUCTOR (type) 7766 && DECL_NAME (decl)) 7767 TREE_USED (decl) = 0; 7768 else if (already_used) 7769 TREE_USED (decl) = 1; 7770 7771 if (cleanup) 7772 finish_decl_cleanup (decl, cleanup); 7773 } 7774 7775 /* DECL is a VAR_DECL for a compiler-generated variable with static 7776 storage duration (like a virtual table) whose initializer is a 7777 compile-time constant. Initialize the variable and provide it to the 7778 back end. */ 7779 7780 void 7781 initialize_artificial_var (tree decl, vec<constructor_elt, va_gc> *v) 7782 { 7783 tree init; 7784 gcc_assert (DECL_ARTIFICIAL (decl)); 7785 init = build_constructor (TREE_TYPE (decl), v); 7786 gcc_assert (TREE_CODE (init) == CONSTRUCTOR); 7787 DECL_INITIAL (decl) = init; 7788 DECL_INITIALIZED_P (decl) = 1; 7789 /* Mark the decl as constexpr so that we can access its content 7790 at compile time. */ 7791 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true; 7792 DECL_DECLARED_CONSTEXPR_P (decl) = true; 7793 determine_visibility (decl); 7794 layout_var_decl (decl); 7795 maybe_commonize_var (decl); 7796 make_rtl_for_nonlocal_decl (decl, init, /*asmspec=*/NULL); 7797 } 7798 7799 /* INIT is the initializer for a variable, as represented by the 7800 parser. Returns true iff INIT is value-dependent. */ 7801 7802 static bool 7803 value_dependent_init_p (tree init) 7804 { 7805 if (TREE_CODE (init) == TREE_LIST) 7806 /* A parenthesized initializer, e.g.: int i (3, 2); ? */ 7807 return any_value_dependent_elements_p (init); 7808 else if (TREE_CODE (init) == CONSTRUCTOR) 7809 /* A brace-enclosed initializer, e.g.: int i = { 3 }; ? */ 7810 { 7811 if (dependent_type_p (TREE_TYPE (init))) 7812 return true; 7813 7814 vec<constructor_elt, va_gc> *elts; 7815 size_t nelts; 7816 size_t i; 7817 7818 elts = CONSTRUCTOR_ELTS (init); 7819 nelts = vec_safe_length (elts); 7820 for (i = 0; i < nelts; ++i) 7821 if (value_dependent_init_p ((*elts)[i].value)) 7822 return true; 7823 } 7824 else 7825 /* It must be a simple expression, e.g., int i = 3; */ 7826 return value_dependent_expression_p (init); 7827 7828 return false; 7829 } 7830 7831 // Returns true if a DECL is VAR_DECL with the concept specifier. 7832 static inline bool 7833 is_concept_var (tree decl) 7834 { 7835 return (VAR_P (decl) 7836 // Not all variables have DECL_LANG_SPECIFIC. 7837 && DECL_LANG_SPECIFIC (decl) 7838 && DECL_DECLARED_CONCEPT_P (decl)); 7839 } 7840 7841 /* A helper function to be called via walk_tree. If any label exists 7842 under *TP, it is (going to be) forced. Set has_forced_label_in_static. */ 7843 7844 static tree 7845 notice_forced_label_r (tree *tp, int *walk_subtrees, void *) 7846 { 7847 if (TYPE_P (*tp)) 7848 *walk_subtrees = 0; 7849 if (TREE_CODE (*tp) == LABEL_DECL) 7850 cfun->has_forced_label_in_static = 1; 7851 return NULL_TREE; 7852 } 7853 7854 /* Return true if DECL has either a trivial destructor, or for C++20 7855 is constexpr and has a constexpr destructor. */ 7856 7857 static bool 7858 decl_maybe_constant_destruction (tree decl, tree type) 7859 { 7860 return (TYPE_HAS_TRIVIAL_DESTRUCTOR (type) 7861 || (cxx_dialect >= cxx20 7862 && VAR_P (decl) 7863 && DECL_DECLARED_CONSTEXPR_P (decl) 7864 && type_has_constexpr_destructor (strip_array_types (type)))); 7865 } 7866 7867 static tree declare_simd_adjust_this (tree *, int *, void *); 7868 7869 /* Helper function of omp_declare_variant_finalize. Finalize one 7870 "omp declare variant base" attribute. Return true if it should be 7871 removed. */ 7872 7873 static bool 7874 omp_declare_variant_finalize_one (tree decl, tree attr) 7875 { 7876 if (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE) 7877 { 7878 walk_tree (&TREE_VALUE (TREE_VALUE (attr)), declare_simd_adjust_this, 7879 DECL_ARGUMENTS (decl), NULL); 7880 walk_tree (&TREE_PURPOSE (TREE_VALUE (attr)), declare_simd_adjust_this, 7881 DECL_ARGUMENTS (decl), NULL); 7882 } 7883 7884 tree ctx = TREE_VALUE (TREE_VALUE (attr)); 7885 tree simd = omp_get_context_selector (ctx, "construct", "simd"); 7886 if (simd) 7887 { 7888 TREE_VALUE (simd) 7889 = c_omp_declare_simd_clauses_to_numbers (DECL_ARGUMENTS (decl), 7890 TREE_VALUE (simd)); 7891 /* FIXME, adjusting simd args unimplemented. */ 7892 return true; 7893 } 7894 7895 tree chain = TREE_CHAIN (TREE_VALUE (attr)); 7896 location_t varid_loc 7897 = cp_expr_loc_or_input_loc (TREE_PURPOSE (TREE_CHAIN (chain))); 7898 location_t match_loc = cp_expr_loc_or_input_loc (TREE_PURPOSE (chain)); 7899 cp_id_kind idk = (cp_id_kind) tree_to_uhwi (TREE_VALUE (chain)); 7900 tree variant = TREE_PURPOSE (TREE_VALUE (attr)); 7901 7902 location_t save_loc = input_location; 7903 input_location = varid_loc; 7904 7905 releasing_vec args; 7906 tree parm = DECL_ARGUMENTS (decl); 7907 if (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE) 7908 parm = DECL_CHAIN (parm); 7909 for (; parm; parm = DECL_CHAIN (parm)) 7910 if (type_dependent_expression_p (parm)) 7911 vec_safe_push (args, build_constructor (TREE_TYPE (parm), NULL)); 7912 else if (MAYBE_CLASS_TYPE_P (TREE_TYPE (parm))) 7913 vec_safe_push (args, build_local_temp (TREE_TYPE (parm))); 7914 else 7915 vec_safe_push (args, build_zero_cst (TREE_TYPE (parm))); 7916 7917 bool koenig_p = false; 7918 if (idk == CP_ID_KIND_UNQUALIFIED || idk == CP_ID_KIND_TEMPLATE_ID) 7919 { 7920 if (identifier_p (variant) 7921 /* In C++20, we may need to perform ADL for a template 7922 name. */ 7923 || (TREE_CODE (variant) == TEMPLATE_ID_EXPR 7924 && identifier_p (TREE_OPERAND (variant, 0)))) 7925 { 7926 if (!args->is_empty ()) 7927 { 7928 koenig_p = true; 7929 if (!any_type_dependent_arguments_p (args)) 7930 variant = perform_koenig_lookup (variant, args, 7931 tf_warning_or_error); 7932 } 7933 else 7934 variant = unqualified_fn_lookup_error (variant); 7935 } 7936 else if (!args->is_empty () && is_overloaded_fn (variant)) 7937 { 7938 tree fn = get_first_fn (variant); 7939 fn = STRIP_TEMPLATE (fn); 7940 if (!((TREE_CODE (fn) == USING_DECL && DECL_DEPENDENT_P (fn)) 7941 || DECL_FUNCTION_MEMBER_P (fn) 7942 || DECL_LOCAL_DECL_P (fn))) 7943 { 7944 koenig_p = true; 7945 if (!any_type_dependent_arguments_p (args)) 7946 variant = perform_koenig_lookup (variant, args, 7947 tf_warning_or_error); 7948 } 7949 } 7950 } 7951 7952 if (idk == CP_ID_KIND_QUALIFIED) 7953 variant = finish_call_expr (variant, &args, /*disallow_virtual=*/true, 7954 koenig_p, tf_warning_or_error); 7955 else 7956 variant = finish_call_expr (variant, &args, /*disallow_virtual=*/false, 7957 koenig_p, tf_warning_or_error); 7958 if (variant == error_mark_node && !processing_template_decl) 7959 return true; 7960 7961 variant = cp_get_callee_fndecl_nofold (variant); 7962 input_location = save_loc; 7963 7964 if (variant) 7965 { 7966 const char *varname = IDENTIFIER_POINTER (DECL_NAME (variant)); 7967 if (!comptypes (TREE_TYPE (decl), TREE_TYPE (variant), 0)) 7968 { 7969 error_at (varid_loc, "variant %qD and base %qD have incompatible " 7970 "types", variant, decl); 7971 return true; 7972 } 7973 if (fndecl_built_in_p (variant) 7974 && (startswith (varname, "__builtin_") 7975 || startswith (varname, "__sync_") 7976 || startswith (varname, "__atomic_"))) 7977 { 7978 error_at (varid_loc, "variant %qD is a built-in", variant); 7979 return true; 7980 } 7981 else 7982 { 7983 tree construct = omp_get_context_selector (ctx, "construct", NULL); 7984 omp_mark_declare_variant (match_loc, variant, construct); 7985 if (!omp_context_selector_matches (ctx)) 7986 return true; 7987 TREE_PURPOSE (TREE_VALUE (attr)) = variant; 7988 } 7989 } 7990 else if (!processing_template_decl) 7991 { 7992 error_at (varid_loc, "could not find variant declaration"); 7993 return true; 7994 } 7995 7996 return false; 7997 } 7998 7999 /* Helper function, finish up "omp declare variant base" attribute 8000 now that there is a DECL. ATTR is the first "omp declare variant base" 8001 attribute. */ 8002 8003 void 8004 omp_declare_variant_finalize (tree decl, tree attr) 8005 { 8006 size_t attr_len = strlen ("omp declare variant base"); 8007 tree *list = &DECL_ATTRIBUTES (decl); 8008 bool remove_all = false; 8009 location_t match_loc = DECL_SOURCE_LOCATION (decl); 8010 if (TREE_CHAIN (TREE_VALUE (attr)) 8011 && TREE_PURPOSE (TREE_CHAIN (TREE_VALUE (attr))) 8012 && EXPR_HAS_LOCATION (TREE_PURPOSE (TREE_CHAIN (TREE_VALUE (attr))))) 8013 match_loc = EXPR_LOCATION (TREE_PURPOSE (TREE_CHAIN (TREE_VALUE (attr)))); 8014 if (DECL_CONSTRUCTOR_P (decl)) 8015 { 8016 error_at (match_loc, "%<declare variant%> on constructor %qD", decl); 8017 remove_all = true; 8018 } 8019 else if (DECL_DESTRUCTOR_P (decl)) 8020 { 8021 error_at (match_loc, "%<declare variant%> on destructor %qD", decl); 8022 remove_all = true; 8023 } 8024 else if (DECL_DEFAULTED_FN (decl)) 8025 { 8026 error_at (match_loc, "%<declare variant%> on defaulted %qD", decl); 8027 remove_all = true; 8028 } 8029 else if (DECL_DELETED_FN (decl)) 8030 { 8031 error_at (match_loc, "%<declare variant%> on deleted %qD", decl); 8032 remove_all = true; 8033 } 8034 else if (DECL_VIRTUAL_P (decl)) 8035 { 8036 error_at (match_loc, "%<declare variant%> on virtual %qD", decl); 8037 remove_all = true; 8038 } 8039 /* This loop is like private_lookup_attribute, except that it works 8040 with tree * rather than tree, as we might want to remove the 8041 attributes that are diagnosed as errorneous. */ 8042 while (*list) 8043 { 8044 tree attr = get_attribute_name (*list); 8045 size_t ident_len = IDENTIFIER_LENGTH (attr); 8046 if (cmp_attribs ("omp declare variant base", attr_len, 8047 IDENTIFIER_POINTER (attr), ident_len)) 8048 { 8049 if (remove_all || omp_declare_variant_finalize_one (decl, *list)) 8050 { 8051 *list = TREE_CHAIN (*list); 8052 continue; 8053 } 8054 } 8055 list = &TREE_CHAIN (*list); 8056 } 8057 } 8058 8059 /* Finish processing of a declaration; 8060 install its line number and initial value. 8061 If the length of an array type is not known before, 8062 it must be determined now, from the initial value, or it is an error. 8063 8064 INIT is the initializer (if any) for DECL. If INIT_CONST_EXPR_P is 8065 true, then INIT is an integral constant expression. 8066 8067 FLAGS is LOOKUP_ONLYCONVERTING if the = init syntax was used, else 0 8068 if the (init) syntax was used. */ 8069 8070 void 8071 cp_finish_decl (tree decl, tree init, bool init_const_expr_p, 8072 tree asmspec_tree, int flags) 8073 { 8074 tree type; 8075 vec<tree, va_gc> *cleanups = NULL; 8076 const char *asmspec = NULL; 8077 int was_readonly = 0; 8078 bool var_definition_p = false; 8079 tree auto_node; 8080 8081 if (decl == error_mark_node) 8082 return; 8083 else if (! decl) 8084 { 8085 if (init) 8086 error ("assignment (not initialization) in declaration"); 8087 return; 8088 } 8089 8090 gcc_assert (TREE_CODE (decl) != RESULT_DECL); 8091 /* Parameters are handled by store_parm_decls, not cp_finish_decl. */ 8092 gcc_assert (TREE_CODE (decl) != PARM_DECL); 8093 8094 type = TREE_TYPE (decl); 8095 if (type == error_mark_node) 8096 return; 8097 8098 if (VAR_P (decl) && is_copy_initialization (init)) 8099 flags |= LOOKUP_ONLYCONVERTING; 8100 8101 /* Warn about register storage specifiers except when in GNU global 8102 or local register variable extension. */ 8103 if (VAR_P (decl) && DECL_REGISTER (decl) && asmspec_tree == NULL_TREE) 8104 { 8105 if (cxx_dialect >= cxx17) 8106 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wregister, 8107 "ISO C++17 does not allow %<register%> storage " 8108 "class specifier"); 8109 else 8110 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wregister, 8111 "%<register%> storage class specifier used"); 8112 } 8113 8114 /* If a name was specified, get the string. */ 8115 if (at_namespace_scope_p ()) 8116 asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree); 8117 if (asmspec_tree && asmspec_tree != error_mark_node) 8118 asmspec = TREE_STRING_POINTER (asmspec_tree); 8119 8120 bool in_class_decl 8121 = (current_class_type 8122 && CP_DECL_CONTEXT (decl) == current_class_type 8123 && TYPE_BEING_DEFINED (current_class_type) 8124 && !CLASSTYPE_TEMPLATE_INSTANTIATION (current_class_type)); 8125 8126 if (in_class_decl 8127 && (DECL_INITIAL (decl) || init)) 8128 DECL_INITIALIZED_IN_CLASS_P (decl) = 1; 8129 8130 if (TREE_CODE (decl) != FUNCTION_DECL 8131 && (auto_node = type_uses_auto (type))) 8132 { 8133 tree d_init; 8134 if (init == NULL_TREE) 8135 { 8136 if (DECL_LANG_SPECIFIC (decl) 8137 && DECL_TEMPLATE_INSTANTIATION (decl) 8138 && !DECL_TEMPLATE_INSTANTIATED (decl)) 8139 { 8140 /* init is null because we're deferring instantiating the 8141 initializer until we need it. Well, we need it now. */ 8142 instantiate_decl (decl, /*defer_ok*/true, /*expl*/false); 8143 return; 8144 } 8145 8146 gcc_assert (CLASS_PLACEHOLDER_TEMPLATE (auto_node)); 8147 } 8148 d_init = init; 8149 if (d_init) 8150 { 8151 if (TREE_CODE (d_init) == TREE_LIST 8152 && !CLASS_PLACEHOLDER_TEMPLATE (auto_node)) 8153 d_init = build_x_compound_expr_from_list (d_init, ELK_INIT, 8154 tf_warning_or_error); 8155 d_init = resolve_nondeduced_context (d_init, tf_warning_or_error); 8156 /* Force auto deduction now. Use tf_none to avoid redundant warnings 8157 on deprecated-14.C. */ 8158 mark_single_function (d_init, tf_none); 8159 } 8160 enum auto_deduction_context adc = adc_variable_type; 8161 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl)) 8162 adc = adc_decomp_type; 8163 tree outer_targs = NULL_TREE; 8164 if (PLACEHOLDER_TYPE_CONSTRAINTS_INFO (auto_node) 8165 && VAR_P (decl) 8166 && DECL_LANG_SPECIFIC (decl) 8167 && DECL_TEMPLATE_INFO (decl) 8168 && !DECL_FUNCTION_SCOPE_P (decl)) 8169 /* The outer template arguments might be needed for satisfaction. 8170 (For function scope variables, do_auto_deduction will obtain the 8171 outer template arguments from current_function_decl.) */ 8172 outer_targs = DECL_TI_ARGS (decl); 8173 type = TREE_TYPE (decl) = do_auto_deduction (type, d_init, auto_node, 8174 tf_warning_or_error, adc, 8175 outer_targs, flags); 8176 if (type == error_mark_node) 8177 return; 8178 if (TREE_CODE (type) == FUNCTION_TYPE) 8179 { 8180 error ("initializer for %<decltype(auto) %D%> has function type; " 8181 "did you forget the %<()%>?", decl); 8182 TREE_TYPE (decl) = error_mark_node; 8183 return; 8184 } 8185 cp_apply_type_quals_to_decl (cp_type_quals (type), decl); 8186 } 8187 8188 if (ensure_literal_type_for_constexpr_object (decl) == error_mark_node) 8189 { 8190 DECL_DECLARED_CONSTEXPR_P (decl) = 0; 8191 if (VAR_P (decl) && DECL_CLASS_SCOPE_P (decl)) 8192 { 8193 init = NULL_TREE; 8194 DECL_EXTERNAL (decl) = 1; 8195 } 8196 } 8197 8198 if (VAR_P (decl) 8199 && DECL_CLASS_SCOPE_P (decl) 8200 && verify_type_context (DECL_SOURCE_LOCATION (decl), 8201 TCTX_STATIC_STORAGE, type) 8202 && DECL_INITIALIZED_IN_CLASS_P (decl)) 8203 check_static_variable_definition (decl, type); 8204 8205 if (!processing_template_decl && VAR_P (decl) && is_global_var (decl)) 8206 { 8207 type_context_kind context = (DECL_THREAD_LOCAL_P (decl) 8208 ? TCTX_THREAD_STORAGE 8209 : TCTX_STATIC_STORAGE); 8210 verify_type_context (input_location, context, TREE_TYPE (decl)); 8211 } 8212 8213 if (init && TREE_CODE (decl) == FUNCTION_DECL) 8214 { 8215 tree clone; 8216 if (init == ridpointers[(int)RID_DELETE]) 8217 { 8218 /* FIXME check this is 1st decl. */ 8219 DECL_DELETED_FN (decl) = 1; 8220 DECL_DECLARED_INLINE_P (decl) = 1; 8221 DECL_INITIAL (decl) = error_mark_node; 8222 FOR_EACH_CLONE (clone, decl) 8223 { 8224 DECL_DELETED_FN (clone) = 1; 8225 DECL_DECLARED_INLINE_P (clone) = 1; 8226 DECL_INITIAL (clone) = error_mark_node; 8227 } 8228 init = NULL_TREE; 8229 } 8230 else if (init == ridpointers[(int)RID_DEFAULT]) 8231 { 8232 if (defaultable_fn_check (decl)) 8233 DECL_DEFAULTED_FN (decl) = 1; 8234 else 8235 DECL_INITIAL (decl) = NULL_TREE; 8236 } 8237 } 8238 8239 if (init && VAR_P (decl)) 8240 { 8241 DECL_NONTRIVIALLY_INITIALIZED_P (decl) = 1; 8242 /* If DECL is a reference, then we want to know whether init is a 8243 reference constant; init_const_expr_p as passed tells us whether 8244 it's an rvalue constant. */ 8245 if (TYPE_REF_P (type)) 8246 init_const_expr_p = potential_constant_expression (init); 8247 if (init_const_expr_p) 8248 { 8249 /* Set these flags now for templates. We'll update the flags in 8250 store_init_value for instantiations. */ 8251 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1; 8252 if (decl_maybe_constant_var_p (decl) 8253 /* FIXME setting TREE_CONSTANT on refs breaks the back end. */ 8254 && !TYPE_REF_P (type)) 8255 TREE_CONSTANT (decl) = 1; 8256 } 8257 /* This is handled mostly by gimplify.cc, but we have to deal with 8258 not warning about int x = x; as it is a GCC extension to turn off 8259 this warning but only if warn_init_self is zero. */ 8260 if (!DECL_EXTERNAL (decl) 8261 && !TREE_STATIC (decl) 8262 && decl == tree_strip_any_location_wrapper (init) 8263 && !warning_enabled_at (DECL_SOURCE_LOCATION (decl), OPT_Winit_self)) 8264 suppress_warning (decl, OPT_Winit_self); 8265 } 8266 8267 if (flag_openmp 8268 && TREE_CODE (decl) == FUNCTION_DECL 8269 /* #pragma omp declare variant on methods handled in finish_struct 8270 instead. */ 8271 && (!DECL_NONSTATIC_MEMBER_FUNCTION_P (decl) 8272 || COMPLETE_TYPE_P (DECL_CONTEXT (decl)))) 8273 if (tree attr = lookup_attribute ("omp declare variant base", 8274 DECL_ATTRIBUTES (decl))) 8275 omp_declare_variant_finalize (decl, attr); 8276 8277 if (processing_template_decl) 8278 { 8279 bool type_dependent_p; 8280 8281 /* Add this declaration to the statement-tree. */ 8282 if (at_function_scope_p ()) 8283 add_decl_expr (decl); 8284 8285 type_dependent_p = dependent_type_p (type); 8286 8287 if (check_for_bare_parameter_packs (init)) 8288 { 8289 init = NULL_TREE; 8290 DECL_INITIAL (decl) = NULL_TREE; 8291 } 8292 8293 /* Generally, initializers in templates are expanded when the 8294 template is instantiated. But, if DECL is a variable constant 8295 then it can be used in future constant expressions, so its value 8296 must be available. */ 8297 8298 bool dep_init = false; 8299 8300 if (!VAR_P (decl) || type_dependent_p) 8301 /* We can't do anything if the decl has dependent type. */; 8302 else if (!init && is_concept_var (decl)) 8303 { 8304 error ("variable concept has no initializer"); 8305 init = boolean_true_node; 8306 } 8307 else if (init 8308 && (init_const_expr_p || DECL_DECLARED_CONSTEXPR_P (decl)) 8309 && !TYPE_REF_P (type) 8310 && decl_maybe_constant_var_p (decl) 8311 && !(dep_init = value_dependent_init_p (init))) 8312 { 8313 /* This variable seems to be a non-dependent constant, so process 8314 its initializer. If check_initializer returns non-null the 8315 initialization wasn't constant after all. */ 8316 tree init_code; 8317 cleanups = make_tree_vector (); 8318 init_code = check_initializer (decl, init, flags, &cleanups); 8319 if (init_code == NULL_TREE) 8320 init = NULL_TREE; 8321 release_tree_vector (cleanups); 8322 } 8323 else 8324 { 8325 gcc_assert (!DECL_PRETTY_FUNCTION_P (decl)); 8326 /* Try to deduce array size. */ 8327 maybe_deduce_size_from_array_init (decl, init); 8328 /* And complain about multiple initializers. */ 8329 if (init && TREE_CODE (init) == TREE_LIST && TREE_CHAIN (init) 8330 && !MAYBE_CLASS_TYPE_P (type)) 8331 init = build_x_compound_expr_from_list (init, ELK_INIT, 8332 tf_warning_or_error); 8333 } 8334 8335 if (init) 8336 DECL_INITIAL (decl) = init; 8337 8338 if (dep_init) 8339 { 8340 retrofit_lang_decl (decl); 8341 SET_DECL_DEPENDENT_INIT_P (decl, true); 8342 } 8343 8344 if (VAR_P (decl) && DECL_REGISTER (decl) && asmspec) 8345 { 8346 set_user_assembler_name (decl, asmspec); 8347 DECL_HARD_REGISTER (decl) = 1; 8348 } 8349 return; 8350 } 8351 8352 /* Just store non-static data member initializers for later. */ 8353 if (init && TREE_CODE (decl) == FIELD_DECL) 8354 DECL_INITIAL (decl) = init; 8355 8356 /* Take care of TYPE_DECLs up front. */ 8357 if (TREE_CODE (decl) == TYPE_DECL) 8358 { 8359 if (type != error_mark_node 8360 && MAYBE_CLASS_TYPE_P (type) && DECL_NAME (decl)) 8361 { 8362 if (TREE_TYPE (DECL_NAME (decl)) && TREE_TYPE (decl) != type) 8363 warning (0, "shadowing previous type declaration of %q#D", decl); 8364 set_identifier_type_value (DECL_NAME (decl), decl); 8365 } 8366 8367 /* If we have installed this as the canonical typedef for this 8368 type, and that type has not been defined yet, delay emitting 8369 the debug information for it, as we will emit it later. */ 8370 if (TYPE_MAIN_DECL (TREE_TYPE (decl)) == decl 8371 && !COMPLETE_TYPE_P (TREE_TYPE (decl))) 8372 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1; 8373 8374 rest_of_decl_compilation (decl, DECL_FILE_SCOPE_P (decl), 8375 at_eof); 8376 return; 8377 } 8378 8379 /* A reference will be modified here, as it is initialized. */ 8380 if (! DECL_EXTERNAL (decl) 8381 && TREE_READONLY (decl) 8382 && TYPE_REF_P (type)) 8383 { 8384 was_readonly = 1; 8385 TREE_READONLY (decl) = 0; 8386 } 8387 8388 /* This needs to happen before extend_ref_init_temps. */ 8389 if (VAR_OR_FUNCTION_DECL_P (decl)) 8390 { 8391 if (VAR_P (decl)) 8392 maybe_commonize_var (decl); 8393 determine_visibility (decl); 8394 } 8395 8396 if (VAR_P (decl)) 8397 { 8398 duration_kind dk = decl_storage_duration (decl); 8399 /* [dcl.constinit]/1 "The constinit specifier shall be applied 8400 only to a declaration of a variable with static or thread storage 8401 duration." */ 8402 if (DECL_DECLARED_CONSTINIT_P (decl) 8403 && !(dk == dk_thread || dk == dk_static)) 8404 { 8405 error_at (DECL_SOURCE_LOCATION (decl), 8406 "%<constinit%> can only be applied to a variable with " 8407 "static or thread storage duration"); 8408 return; 8409 } 8410 8411 /* If this is a local variable that will need a mangled name, 8412 register it now. We must do this before processing the 8413 initializer for the variable, since the initialization might 8414 require a guard variable, and since the mangled name of the 8415 guard variable will depend on the mangled name of this 8416 variable. */ 8417 if (DECL_FUNCTION_SCOPE_P (decl) 8418 && TREE_STATIC (decl) 8419 && !DECL_ARTIFICIAL (decl)) 8420 { 8421 /* The variable holding an anonymous union will have had its 8422 discriminator set in finish_anon_union, after which it's 8423 NAME will have been cleared. */ 8424 if (DECL_NAME (decl)) 8425 determine_local_discriminator (decl); 8426 /* Normally has_forced_label_in_static is set during GIMPLE 8427 lowering, but [cd]tors are never actually compiled directly. 8428 We need to set this early so we can deal with the label 8429 address extension. */ 8430 if ((DECL_CONSTRUCTOR_P (current_function_decl) 8431 || DECL_DESTRUCTOR_P (current_function_decl)) 8432 && init) 8433 { 8434 walk_tree (&init, notice_forced_label_r, NULL, NULL); 8435 add_local_decl (cfun, decl); 8436 } 8437 /* And make sure it's in the symbol table for 8438 c_parse_final_cleanups to find. */ 8439 varpool_node::get_create (decl); 8440 } 8441 8442 /* Convert the initializer to the type of DECL, if we have not 8443 already initialized DECL. */ 8444 if (!DECL_INITIALIZED_P (decl) 8445 /* If !DECL_EXTERNAL then DECL is being defined. In the 8446 case of a static data member initialized inside the 8447 class-specifier, there can be an initializer even if DECL 8448 is *not* defined. */ 8449 && (!DECL_EXTERNAL (decl) || init)) 8450 { 8451 cleanups = make_tree_vector (); 8452 init = check_initializer (decl, init, flags, &cleanups); 8453 8454 /* Handle: 8455 8456 [dcl.init] 8457 8458 The memory occupied by any object of static storage 8459 duration is zero-initialized at program startup before 8460 any other initialization takes place. 8461 8462 We cannot create an appropriate initializer until after 8463 the type of DECL is finalized. If DECL_INITIAL is set, 8464 then the DECL is statically initialized, and any 8465 necessary zero-initialization has already been performed. */ 8466 if (TREE_STATIC (decl) && !DECL_INITIAL (decl)) 8467 DECL_INITIAL (decl) = build_zero_init (TREE_TYPE (decl), 8468 /*nelts=*/NULL_TREE, 8469 /*static_storage_p=*/true); 8470 /* Remember that the initialization for this variable has 8471 taken place. */ 8472 DECL_INITIALIZED_P (decl) = 1; 8473 /* This declaration is the definition of this variable, 8474 unless we are initializing a static data member within 8475 the class specifier. */ 8476 if (!DECL_EXTERNAL (decl)) 8477 var_definition_p = true; 8478 } 8479 /* If the variable has an array type, lay out the type, even if 8480 there is no initializer. It is valid to index through the 8481 array, and we must get TYPE_ALIGN set correctly on the array 8482 type. */ 8483 else if (TREE_CODE (type) == ARRAY_TYPE) 8484 layout_type (type); 8485 8486 if (TREE_STATIC (decl) 8487 && !at_function_scope_p () 8488 && current_function_decl == NULL) 8489 /* So decl is a global variable or a static member of a 8490 non local class. Record the types it uses 8491 so that we can decide later to emit debug info for them. */ 8492 record_types_used_by_current_var_decl (decl); 8493 } 8494 8495 /* Add this declaration to the statement-tree. This needs to happen 8496 after the call to check_initializer so that the DECL_EXPR for a 8497 reference temp is added before the DECL_EXPR for the reference itself. */ 8498 if (DECL_FUNCTION_SCOPE_P (decl)) 8499 { 8500 /* If we're building a variable sized type, and we might be 8501 reachable other than via the top of the current binding 8502 level, then create a new BIND_EXPR so that we deallocate 8503 the object at the right time. */ 8504 if (VAR_P (decl) 8505 && DECL_SIZE (decl) 8506 && !TREE_CONSTANT (DECL_SIZE (decl)) 8507 && STATEMENT_LIST_HAS_LABEL (cur_stmt_list)) 8508 { 8509 tree bind; 8510 bind = build3 (BIND_EXPR, void_type_node, NULL, NULL, NULL); 8511 TREE_SIDE_EFFECTS (bind) = 1; 8512 add_stmt (bind); 8513 BIND_EXPR_BODY (bind) = push_stmt_list (); 8514 } 8515 add_decl_expr (decl); 8516 } 8517 8518 /* Let the middle end know about variables and functions -- but not 8519 static data members in uninstantiated class templates. */ 8520 if (VAR_OR_FUNCTION_DECL_P (decl)) 8521 { 8522 if (VAR_P (decl)) 8523 { 8524 layout_var_decl (decl); 8525 if (!flag_weak) 8526 /* Check again now that we have an initializer. */ 8527 maybe_commonize_var (decl); 8528 /* A class-scope constexpr variable with an out-of-class declaration. 8529 C++17 makes them implicitly inline, but still force it out. */ 8530 if (DECL_INLINE_VAR_P (decl) 8531 && !DECL_VAR_DECLARED_INLINE_P (decl) 8532 && !DECL_TEMPLATE_INSTANTIATION (decl) 8533 && !in_class_decl) 8534 mark_needed (decl); 8535 } 8536 8537 if (var_definition_p 8538 /* With -fmerge-all-constants, gimplify_init_constructor 8539 might add TREE_STATIC to aggregate variables. */ 8540 && (TREE_STATIC (decl) 8541 || (flag_merge_constants >= 2 8542 && AGGREGATE_TYPE_P (type)))) 8543 { 8544 /* If a TREE_READONLY variable needs initialization 8545 at runtime, it is no longer readonly and we need to 8546 avoid MEM_READONLY_P being set on RTL created for it. */ 8547 if (init) 8548 { 8549 if (TREE_READONLY (decl)) 8550 TREE_READONLY (decl) = 0; 8551 was_readonly = 0; 8552 } 8553 else if (was_readonly) 8554 TREE_READONLY (decl) = 1; 8555 8556 /* Likewise if it needs destruction. */ 8557 if (!decl_maybe_constant_destruction (decl, type)) 8558 TREE_READONLY (decl) = 0; 8559 } 8560 else if (VAR_P (decl) 8561 && CP_DECL_THREAD_LOCAL_P (decl) 8562 && (!DECL_EXTERNAL (decl) || flag_extern_tls_init) 8563 && (was_readonly || TREE_READONLY (decl)) 8564 && var_needs_tls_wrapper (decl)) 8565 { 8566 /* TLS variables need dynamic initialization by the TLS wrapper 8567 function, we don't want to hoist accesses to it before the 8568 wrapper. */ 8569 was_readonly = 0; 8570 TREE_READONLY (decl) = 0; 8571 } 8572 8573 make_rtl_for_nonlocal_decl (decl, init, asmspec); 8574 8575 /* Check for abstractness of the type. */ 8576 if (var_definition_p) 8577 abstract_virtuals_error (decl, type); 8578 8579 if (TREE_TYPE (decl) == error_mark_node) 8580 /* No initialization required. */ 8581 ; 8582 else if (TREE_CODE (decl) == FUNCTION_DECL) 8583 { 8584 if (init) 8585 { 8586 if (init == ridpointers[(int)RID_DEFAULT]) 8587 { 8588 /* An out-of-class default definition is defined at 8589 the point where it is explicitly defaulted. */ 8590 if (DECL_DELETED_FN (decl)) 8591 maybe_explain_implicit_delete (decl); 8592 else if (DECL_INITIAL (decl) == error_mark_node) 8593 synthesize_method (decl); 8594 } 8595 else 8596 error_at (cp_expr_loc_or_loc (init, 8597 DECL_SOURCE_LOCATION (decl)), 8598 "function %q#D is initialized like a variable", 8599 decl); 8600 } 8601 /* else no initialization required. */ 8602 } 8603 else if (DECL_EXTERNAL (decl) 8604 && ! (DECL_LANG_SPECIFIC (decl) 8605 && DECL_NOT_REALLY_EXTERN (decl))) 8606 { 8607 /* check_initializer will have done any constant initialization. */ 8608 } 8609 /* A variable definition. */ 8610 else if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl)) 8611 /* Initialize the local variable. */ 8612 initialize_local_var (decl, init); 8613 8614 /* If a variable is defined, and then a subsequent 8615 definition with external linkage is encountered, we will 8616 get here twice for the same variable. We want to avoid 8617 calling expand_static_init more than once. For variables 8618 that are not static data members, we can call 8619 expand_static_init only when we actually process the 8620 initializer. It is not legal to redeclare a static data 8621 member, so this issue does not arise in that case. */ 8622 else if (var_definition_p && TREE_STATIC (decl)) 8623 expand_static_init (decl, init); 8624 } 8625 8626 /* If a CLEANUP_STMT was created to destroy a temporary bound to a 8627 reference, insert it in the statement-tree now. */ 8628 if (cleanups) 8629 { 8630 for (tree t : *cleanups) 8631 { 8632 push_cleanup (NULL_TREE, t, false); 8633 /* As in initialize_local_var. */ 8634 wrap_temporary_cleanups (init, t); 8635 } 8636 release_tree_vector (cleanups); 8637 } 8638 8639 if (was_readonly) 8640 TREE_READONLY (decl) = 1; 8641 8642 if (flag_openmp 8643 && VAR_P (decl) 8644 && lookup_attribute ("omp declare target implicit", 8645 DECL_ATTRIBUTES (decl))) 8646 { 8647 DECL_ATTRIBUTES (decl) 8648 = remove_attribute ("omp declare target implicit", 8649 DECL_ATTRIBUTES (decl)); 8650 complete_type (TREE_TYPE (decl)); 8651 if (!cp_omp_mappable_type (TREE_TYPE (decl))) 8652 { 8653 error ("%q+D in declare target directive does not have mappable" 8654 " type", decl); 8655 cp_omp_emit_unmappable_type_notes (TREE_TYPE (decl)); 8656 } 8657 else if (!lookup_attribute ("omp declare target", 8658 DECL_ATTRIBUTES (decl)) 8659 && !lookup_attribute ("omp declare target link", 8660 DECL_ATTRIBUTES (decl))) 8661 { 8662 DECL_ATTRIBUTES (decl) 8663 = tree_cons (get_identifier ("omp declare target"), 8664 NULL_TREE, DECL_ATTRIBUTES (decl)); 8665 symtab_node *node = symtab_node::get (decl); 8666 if (node != NULL) 8667 { 8668 node->offloadable = 1; 8669 if (ENABLE_OFFLOADING) 8670 { 8671 g->have_offload = true; 8672 if (is_a <varpool_node *> (node)) 8673 vec_safe_push (offload_vars, decl); 8674 } 8675 } 8676 } 8677 } 8678 8679 /* This is the last point we can lower alignment so give the target the 8680 chance to do so. */ 8681 if (VAR_P (decl) 8682 && !is_global_var (decl) 8683 && !DECL_HARD_REGISTER (decl)) 8684 targetm.lower_local_decl_alignment (decl); 8685 8686 invoke_plugin_callbacks (PLUGIN_FINISH_DECL, decl); 8687 } 8688 8689 /* For class TYPE return itself or some its bases that contain 8690 any direct non-static data members. Return error_mark_node if an 8691 error has been diagnosed. */ 8692 8693 static tree 8694 find_decomp_class_base (location_t loc, tree type, tree ret) 8695 { 8696 bool member_seen = false; 8697 for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field)) 8698 if (TREE_CODE (field) != FIELD_DECL 8699 || DECL_ARTIFICIAL (field) 8700 || DECL_UNNAMED_BIT_FIELD (field)) 8701 continue; 8702 else if (ret) 8703 return type; 8704 else if (ANON_AGGR_TYPE_P (TREE_TYPE (field))) 8705 { 8706 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE) 8707 error_at (loc, "cannot decompose class type %qT because it has an " 8708 "anonymous struct member", type); 8709 else 8710 error_at (loc, "cannot decompose class type %qT because it has an " 8711 "anonymous union member", type); 8712 inform (DECL_SOURCE_LOCATION (field), "declared here"); 8713 return error_mark_node; 8714 } 8715 else if (!accessible_p (type, field, true)) 8716 { 8717 error_at (loc, "cannot decompose inaccessible member %qD of %qT", 8718 field, type); 8719 inform (DECL_SOURCE_LOCATION (field), 8720 TREE_PRIVATE (field) 8721 ? G_("declared private here") 8722 : G_("declared protected here")); 8723 return error_mark_node; 8724 } 8725 else 8726 member_seen = true; 8727 8728 tree base_binfo, binfo; 8729 tree orig_ret = ret; 8730 int i; 8731 if (member_seen) 8732 ret = type; 8733 for (binfo = TYPE_BINFO (type), i = 0; 8734 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++) 8735 { 8736 tree t = find_decomp_class_base (loc, TREE_TYPE (base_binfo), ret); 8737 if (t == error_mark_node) 8738 return error_mark_node; 8739 if (t != NULL_TREE && t != ret) 8740 { 8741 if (ret == type) 8742 { 8743 error_at (loc, "cannot decompose class type %qT: both it and " 8744 "its base class %qT have non-static data members", 8745 type, t); 8746 return error_mark_node; 8747 } 8748 else if (orig_ret != NULL_TREE) 8749 return t; 8750 else if (ret != NULL_TREE) 8751 { 8752 error_at (loc, "cannot decompose class type %qT: its base " 8753 "classes %qT and %qT have non-static data " 8754 "members", type, ret, t); 8755 return error_mark_node; 8756 } 8757 else 8758 ret = t; 8759 } 8760 } 8761 return ret; 8762 } 8763 8764 /* Return std::tuple_size<TYPE>::value. */ 8765 8766 static tree 8767 get_tuple_size (tree type) 8768 { 8769 tree args = make_tree_vec (1); 8770 TREE_VEC_ELT (args, 0) = type; 8771 tree inst = lookup_template_class (tuple_size_identifier, args, 8772 /*in_decl*/NULL_TREE, 8773 /*context*/std_node, 8774 /*entering_scope*/false, tf_none); 8775 inst = complete_type (inst); 8776 if (inst == error_mark_node || !COMPLETE_TYPE_P (inst)) 8777 return NULL_TREE; 8778 tree val = lookup_qualified_name (inst, value_identifier, 8779 LOOK_want::NORMAL, /*complain*/false); 8780 if (TREE_CODE (val) == VAR_DECL || TREE_CODE (val) == CONST_DECL) 8781 val = maybe_constant_value (val); 8782 if (TREE_CODE (val) == INTEGER_CST) 8783 return val; 8784 else 8785 return error_mark_node; 8786 } 8787 8788 /* Return std::tuple_element<I,TYPE>::type. */ 8789 8790 static tree 8791 get_tuple_element_type (tree type, unsigned i) 8792 { 8793 tree args = make_tree_vec (2); 8794 TREE_VEC_ELT (args, 0) = build_int_cst (integer_type_node, i); 8795 TREE_VEC_ELT (args, 1) = type; 8796 tree inst = lookup_template_class (tuple_element_identifier, args, 8797 /*in_decl*/NULL_TREE, 8798 /*context*/std_node, 8799 /*entering_scope*/false, 8800 tf_warning_or_error); 8801 return make_typename_type (inst, type_identifier, 8802 none_type, tf_warning_or_error); 8803 } 8804 8805 /* Return e.get<i>() or get<i>(e). */ 8806 8807 static tree 8808 get_tuple_decomp_init (tree decl, unsigned i) 8809 { 8810 tree targs = make_tree_vec (1); 8811 TREE_VEC_ELT (targs, 0) = build_int_cst (integer_type_node, i); 8812 8813 tree etype = TREE_TYPE (decl); 8814 tree e = convert_from_reference (decl); 8815 8816 /* [The id-expression] e is an lvalue if the type of the entity e is an 8817 lvalue reference and an xvalue otherwise. */ 8818 if (!TYPE_REF_P (etype) 8819 || TYPE_REF_IS_RVALUE (etype)) 8820 e = move (e); 8821 8822 tree fns = lookup_qualified_name (TREE_TYPE (e), get__identifier, 8823 LOOK_want::NORMAL, /*complain*/false); 8824 bool use_member_get = false; 8825 8826 /* To use a member get, member lookup must find at least one 8827 declaration that is a function template 8828 whose first template parameter is a non-type parameter. */ 8829 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (fns)); iter; ++iter) 8830 { 8831 tree fn = *iter; 8832 if (TREE_CODE (fn) == TEMPLATE_DECL) 8833 { 8834 tree tparms = DECL_TEMPLATE_PARMS (fn); 8835 tree parm = TREE_VEC_ELT (INNERMOST_TEMPLATE_PARMS (tparms), 0); 8836 if (TREE_CODE (TREE_VALUE (parm)) == PARM_DECL) 8837 { 8838 use_member_get = true; 8839 break; 8840 } 8841 } 8842 } 8843 8844 if (use_member_get) 8845 { 8846 fns = lookup_template_function (fns, targs); 8847 return build_new_method_call (e, fns, /*args*/NULL, 8848 /*path*/NULL_TREE, LOOKUP_NORMAL, 8849 /*fn_p*/NULL, tf_warning_or_error); 8850 } 8851 else 8852 { 8853 releasing_vec args (make_tree_vector_single (e)); 8854 fns = lookup_template_function (get__identifier, targs); 8855 fns = perform_koenig_lookup (fns, args, tf_warning_or_error); 8856 return finish_call_expr (fns, &args, /*novirt*/false, 8857 /*koenig*/true, tf_warning_or_error); 8858 } 8859 } 8860 8861 /* It's impossible to recover the decltype of a tuple decomposition variable 8862 based on the actual type of the variable, so store it in a hash table. */ 8863 8864 static GTY((cache)) decl_tree_cache_map *decomp_type_table; 8865 8866 tree 8867 lookup_decomp_type (tree v) 8868 { 8869 return *decomp_type_table->get (v); 8870 } 8871 8872 /* Mangle a decomposition declaration if needed. Arguments like 8873 in cp_finish_decomp. */ 8874 8875 void 8876 cp_maybe_mangle_decomp (tree decl, tree first, unsigned int count) 8877 { 8878 if (!processing_template_decl 8879 && !error_operand_p (decl) 8880 && TREE_STATIC (decl)) 8881 { 8882 auto_vec<tree, 16> v; 8883 v.safe_grow (count, true); 8884 tree d = first; 8885 for (unsigned int i = 0; i < count; i++, d = DECL_CHAIN (d)) 8886 v[count - i - 1] = d; 8887 SET_DECL_ASSEMBLER_NAME (decl, mangle_decomp (decl, v)); 8888 maybe_apply_pragma_weak (decl); 8889 } 8890 } 8891 8892 /* Finish a decomposition declaration. DECL is the underlying declaration 8893 "e", FIRST is the head of a chain of decls for the individual identifiers 8894 chained through DECL_CHAIN in reverse order and COUNT is the number of 8895 those decls. */ 8896 8897 void 8898 cp_finish_decomp (tree decl, tree first, unsigned int count) 8899 { 8900 if (error_operand_p (decl)) 8901 { 8902 error_out: 8903 while (count--) 8904 { 8905 TREE_TYPE (first) = error_mark_node; 8906 if (DECL_HAS_VALUE_EXPR_P (first)) 8907 { 8908 SET_DECL_VALUE_EXPR (first, NULL_TREE); 8909 DECL_HAS_VALUE_EXPR_P (first) = 0; 8910 } 8911 first = DECL_CHAIN (first); 8912 } 8913 if (DECL_P (decl) && DECL_NAMESPACE_SCOPE_P (decl)) 8914 SET_DECL_ASSEMBLER_NAME (decl, get_identifier ("<decomp>")); 8915 return; 8916 } 8917 8918 location_t loc = DECL_SOURCE_LOCATION (decl); 8919 if (type_dependent_expression_p (decl) 8920 /* This happens for range for when not in templates. 8921 Still add the DECL_VALUE_EXPRs for later processing. */ 8922 || (!processing_template_decl 8923 && type_uses_auto (TREE_TYPE (decl)))) 8924 { 8925 for (unsigned int i = 0; i < count; i++) 8926 { 8927 if (!DECL_HAS_VALUE_EXPR_P (first)) 8928 { 8929 tree v = build_nt (ARRAY_REF, decl, 8930 size_int (count - i - 1), 8931 NULL_TREE, NULL_TREE); 8932 SET_DECL_VALUE_EXPR (first, v); 8933 DECL_HAS_VALUE_EXPR_P (first) = 1; 8934 } 8935 if (processing_template_decl) 8936 fit_decomposition_lang_decl (first, decl); 8937 first = DECL_CHAIN (first); 8938 } 8939 return; 8940 } 8941 8942 auto_vec<tree, 16> v; 8943 v.safe_grow (count, true); 8944 tree d = first; 8945 for (unsigned int i = 0; i < count; i++, d = DECL_CHAIN (d)) 8946 { 8947 v[count - i - 1] = d; 8948 fit_decomposition_lang_decl (d, decl); 8949 } 8950 8951 tree type = TREE_TYPE (decl); 8952 tree dexp = decl; 8953 8954 if (TYPE_REF_P (type)) 8955 { 8956 dexp = convert_from_reference (dexp); 8957 type = complete_type (TREE_TYPE (type)); 8958 if (type == error_mark_node) 8959 goto error_out; 8960 if (!COMPLETE_TYPE_P (type)) 8961 { 8962 error_at (loc, "structured binding refers to incomplete type %qT", 8963 type); 8964 goto error_out; 8965 } 8966 } 8967 8968 tree eltype = NULL_TREE; 8969 unsigned HOST_WIDE_INT eltscnt = 0; 8970 if (TREE_CODE (type) == ARRAY_TYPE) 8971 { 8972 tree nelts; 8973 nelts = array_type_nelts_top (type); 8974 if (nelts == error_mark_node) 8975 goto error_out; 8976 if (!tree_fits_uhwi_p (nelts)) 8977 { 8978 error_at (loc, "cannot decompose variable length array %qT", type); 8979 goto error_out; 8980 } 8981 eltscnt = tree_to_uhwi (nelts); 8982 if (count != eltscnt) 8983 { 8984 cnt_mismatch: 8985 if (count > eltscnt) 8986 error_n (loc, count, 8987 "%u name provided for structured binding", 8988 "%u names provided for structured binding", count); 8989 else 8990 error_n (loc, count, 8991 "only %u name provided for structured binding", 8992 "only %u names provided for structured binding", count); 8993 inform_n (loc, eltscnt, 8994 "while %qT decomposes into %wu element", 8995 "while %qT decomposes into %wu elements", 8996 type, eltscnt); 8997 goto error_out; 8998 } 8999 eltype = TREE_TYPE (type); 9000 for (unsigned int i = 0; i < count; i++) 9001 { 9002 TREE_TYPE (v[i]) = eltype; 9003 layout_decl (v[i], 0); 9004 if (processing_template_decl) 9005 continue; 9006 tree t = unshare_expr (dexp); 9007 t = build4_loc (DECL_SOURCE_LOCATION (v[i]), ARRAY_REF, 9008 eltype, t, size_int (i), NULL_TREE, 9009 NULL_TREE); 9010 SET_DECL_VALUE_EXPR (v[i], t); 9011 DECL_HAS_VALUE_EXPR_P (v[i]) = 1; 9012 } 9013 } 9014 /* 2 GNU extensions. */ 9015 else if (TREE_CODE (type) == COMPLEX_TYPE) 9016 { 9017 eltscnt = 2; 9018 if (count != eltscnt) 9019 goto cnt_mismatch; 9020 eltype = cp_build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type)); 9021 for (unsigned int i = 0; i < count; i++) 9022 { 9023 TREE_TYPE (v[i]) = eltype; 9024 layout_decl (v[i], 0); 9025 if (processing_template_decl) 9026 continue; 9027 tree t = unshare_expr (dexp); 9028 t = build1_loc (DECL_SOURCE_LOCATION (v[i]), 9029 i ? IMAGPART_EXPR : REALPART_EXPR, eltype, 9030 t); 9031 SET_DECL_VALUE_EXPR (v[i], t); 9032 DECL_HAS_VALUE_EXPR_P (v[i]) = 1; 9033 } 9034 } 9035 else if (TREE_CODE (type) == VECTOR_TYPE) 9036 { 9037 if (!TYPE_VECTOR_SUBPARTS (type).is_constant (&eltscnt)) 9038 { 9039 error_at (loc, "cannot decompose variable length vector %qT", type); 9040 goto error_out; 9041 } 9042 if (count != eltscnt) 9043 goto cnt_mismatch; 9044 eltype = cp_build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type)); 9045 for (unsigned int i = 0; i < count; i++) 9046 { 9047 TREE_TYPE (v[i]) = eltype; 9048 layout_decl (v[i], 0); 9049 if (processing_template_decl) 9050 continue; 9051 tree t = unshare_expr (dexp); 9052 convert_vector_to_array_for_subscript (DECL_SOURCE_LOCATION (v[i]), 9053 &t, size_int (i)); 9054 t = build4_loc (DECL_SOURCE_LOCATION (v[i]), ARRAY_REF, 9055 eltype, t, size_int (i), NULL_TREE, 9056 NULL_TREE); 9057 SET_DECL_VALUE_EXPR (v[i], t); 9058 DECL_HAS_VALUE_EXPR_P (v[i]) = 1; 9059 } 9060 } 9061 else if (tree tsize = get_tuple_size (type)) 9062 { 9063 if (tsize == error_mark_node) 9064 { 9065 error_at (loc, "%<std::tuple_size<%T>::value%> is not an integral " 9066 "constant expression", type); 9067 goto error_out; 9068 } 9069 if (!tree_fits_uhwi_p (tsize)) 9070 { 9071 error_n (loc, count, 9072 "%u name provided for structured binding", 9073 "%u names provided for structured binding", count); 9074 inform (loc, "while %qT decomposes into %E elements", 9075 type, tsize); 9076 goto error_out; 9077 } 9078 eltscnt = tree_to_uhwi (tsize); 9079 if (count != eltscnt) 9080 goto cnt_mismatch; 9081 int save_read = DECL_READ_P (decl); 9082 for (unsigned i = 0; i < count; ++i) 9083 { 9084 location_t sloc = input_location; 9085 location_t dloc = DECL_SOURCE_LOCATION (v[i]); 9086 9087 input_location = dloc; 9088 tree init = get_tuple_decomp_init (decl, i); 9089 tree eltype = (init == error_mark_node ? error_mark_node 9090 : get_tuple_element_type (type, i)); 9091 input_location = sloc; 9092 9093 if (VOID_TYPE_P (eltype)) 9094 { 9095 error ("%<std::tuple_element<%u, %T>::type%> is %<void%>", 9096 i, type); 9097 eltype = error_mark_node; 9098 } 9099 if (init == error_mark_node || eltype == error_mark_node) 9100 { 9101 inform (dloc, "in initialization of structured binding " 9102 "variable %qD", v[i]); 9103 goto error_out; 9104 } 9105 /* Save the decltype away before reference collapse. */ 9106 hash_map_safe_put<hm_ggc> (decomp_type_table, v[i], eltype); 9107 eltype = cp_build_reference_type (eltype, !lvalue_p (init)); 9108 TREE_TYPE (v[i]) = eltype; 9109 layout_decl (v[i], 0); 9110 if (DECL_HAS_VALUE_EXPR_P (v[i])) 9111 { 9112 /* In this case the names are variables, not just proxies. */ 9113 SET_DECL_VALUE_EXPR (v[i], NULL_TREE); 9114 DECL_HAS_VALUE_EXPR_P (v[i]) = 0; 9115 } 9116 if (!processing_template_decl) 9117 { 9118 copy_linkage (v[i], decl); 9119 cp_finish_decl (v[i], init, /*constexpr*/false, 9120 /*asm*/NULL_TREE, LOOKUP_NORMAL); 9121 } 9122 } 9123 /* Ignore reads from the underlying decl performed during initialization 9124 of the individual variables. If those will be read, we'll mark 9125 the underlying decl as read at that point. */ 9126 DECL_READ_P (decl) = save_read; 9127 } 9128 else if (TREE_CODE (type) == UNION_TYPE) 9129 { 9130 error_at (loc, "cannot decompose union type %qT", type); 9131 goto error_out; 9132 } 9133 else if (!CLASS_TYPE_P (type)) 9134 { 9135 error_at (loc, "cannot decompose non-array non-class type %qT", type); 9136 goto error_out; 9137 } 9138 else if (LAMBDA_TYPE_P (type)) 9139 { 9140 error_at (loc, "cannot decompose lambda closure type %qT", type); 9141 goto error_out; 9142 } 9143 else if (processing_template_decl && complete_type (type) == error_mark_node) 9144 goto error_out; 9145 else if (processing_template_decl && !COMPLETE_TYPE_P (type)) 9146 pedwarn (loc, 0, "structured binding refers to incomplete class type %qT", 9147 type); 9148 else 9149 { 9150 tree btype = find_decomp_class_base (loc, type, NULL_TREE); 9151 if (btype == error_mark_node) 9152 goto error_out; 9153 else if (btype == NULL_TREE) 9154 { 9155 error_at (loc, "cannot decompose class type %qT without non-static " 9156 "data members", type); 9157 goto error_out; 9158 } 9159 for (tree field = TYPE_FIELDS (btype); field; field = TREE_CHAIN (field)) 9160 if (TREE_CODE (field) != FIELD_DECL 9161 || DECL_ARTIFICIAL (field) 9162 || DECL_UNNAMED_BIT_FIELD (field)) 9163 continue; 9164 else 9165 eltscnt++; 9166 if (count != eltscnt) 9167 goto cnt_mismatch; 9168 tree t = dexp; 9169 if (type != btype) 9170 { 9171 t = convert_to_base (t, btype, /*check_access*/true, 9172 /*nonnull*/false, tf_warning_or_error); 9173 type = btype; 9174 } 9175 unsigned int i = 0; 9176 for (tree field = TYPE_FIELDS (btype); field; field = TREE_CHAIN (field)) 9177 if (TREE_CODE (field) != FIELD_DECL 9178 || DECL_ARTIFICIAL (field) 9179 || DECL_UNNAMED_BIT_FIELD (field)) 9180 continue; 9181 else 9182 { 9183 tree tt = finish_non_static_data_member (field, unshare_expr (t), 9184 NULL_TREE); 9185 if (REFERENCE_REF_P (tt)) 9186 tt = TREE_OPERAND (tt, 0); 9187 TREE_TYPE (v[i]) = TREE_TYPE (tt); 9188 layout_decl (v[i], 0); 9189 if (!processing_template_decl) 9190 { 9191 SET_DECL_VALUE_EXPR (v[i], tt); 9192 DECL_HAS_VALUE_EXPR_P (v[i]) = 1; 9193 } 9194 i++; 9195 } 9196 } 9197 if (processing_template_decl) 9198 { 9199 for (unsigned int i = 0; i < count; i++) 9200 if (!DECL_HAS_VALUE_EXPR_P (v[i])) 9201 { 9202 tree a = build_nt (ARRAY_REF, decl, size_int (i), 9203 NULL_TREE, NULL_TREE); 9204 SET_DECL_VALUE_EXPR (v[i], a); 9205 DECL_HAS_VALUE_EXPR_P (v[i]) = 1; 9206 } 9207 } 9208 } 9209 9210 /* Returns a declaration for a VAR_DECL as if: 9211 9212 extern "C" TYPE NAME; 9213 9214 had been seen. Used to create compiler-generated global 9215 variables. */ 9216 9217 static tree 9218 declare_global_var (tree name, tree type) 9219 { 9220 auto cookie = push_abi_namespace (global_namespace); 9221 tree decl = build_decl (input_location, VAR_DECL, name, type); 9222 TREE_PUBLIC (decl) = 1; 9223 DECL_EXTERNAL (decl) = 1; 9224 DECL_ARTIFICIAL (decl) = 1; 9225 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace); 9226 /* If the user has explicitly declared this variable (perhaps 9227 because the code we are compiling is part of a low-level runtime 9228 library), then it is possible that our declaration will be merged 9229 with theirs by pushdecl. */ 9230 decl = pushdecl (decl); 9231 cp_finish_decl (decl, NULL_TREE, false, NULL_TREE, 0); 9232 pop_abi_namespace (cookie, global_namespace); 9233 9234 return decl; 9235 } 9236 9237 /* Returns the type for the argument to "__cxa_atexit" (or "atexit", 9238 if "__cxa_atexit" is not being used) corresponding to the function 9239 to be called when the program exits. */ 9240 9241 static tree 9242 get_atexit_fn_ptr_type (void) 9243 { 9244 tree fn_type; 9245 9246 if (!atexit_fn_ptr_type_node) 9247 { 9248 tree arg_type; 9249 if (flag_use_cxa_atexit 9250 && !targetm.cxx.use_atexit_for_cxa_atexit ()) 9251 /* The parameter to "__cxa_atexit" is "void (*)(void *)". */ 9252 arg_type = ptr_type_node; 9253 else 9254 /* The parameter to "atexit" is "void (*)(void)". */ 9255 arg_type = NULL_TREE; 9256 9257 fn_type = build_function_type_list (void_type_node, 9258 arg_type, NULL_TREE); 9259 atexit_fn_ptr_type_node = build_pointer_type (fn_type); 9260 } 9261 9262 return atexit_fn_ptr_type_node; 9263 } 9264 9265 /* Returns a pointer to the `atexit' function. Note that if 9266 FLAG_USE_CXA_ATEXIT is nonzero, then this will actually be the new 9267 `__cxa_atexit' function specified in the IA64 C++ ABI. */ 9268 9269 static tree 9270 get_atexit_node (void) 9271 { 9272 tree atexit_fndecl; 9273 tree fn_type; 9274 tree fn_ptr_type; 9275 const char *name; 9276 bool use_aeabi_atexit; 9277 tree ctx = global_namespace; 9278 9279 if (atexit_node) 9280 return atexit_node; 9281 9282 if (flag_use_cxa_atexit && !targetm.cxx.use_atexit_for_cxa_atexit ()) 9283 { 9284 /* The declaration for `__cxa_atexit' is: 9285 9286 int __cxa_atexit (void (*)(void *), void *, void *) 9287 9288 We build up the argument types and then the function type 9289 itself. */ 9290 tree argtype0, argtype1, argtype2; 9291 9292 use_aeabi_atexit = targetm.cxx.use_aeabi_atexit (); 9293 /* First, build the pointer-to-function type for the first 9294 argument. */ 9295 fn_ptr_type = get_atexit_fn_ptr_type (); 9296 /* Then, build the rest of the argument types. */ 9297 argtype2 = ptr_type_node; 9298 if (use_aeabi_atexit) 9299 { 9300 argtype1 = fn_ptr_type; 9301 argtype0 = ptr_type_node; 9302 } 9303 else 9304 { 9305 argtype1 = ptr_type_node; 9306 argtype0 = fn_ptr_type; 9307 } 9308 /* And the final __cxa_atexit type. */ 9309 fn_type = build_function_type_list (integer_type_node, 9310 argtype0, argtype1, argtype2, 9311 NULL_TREE); 9312 /* ... which needs noexcept. */ 9313 fn_type = build_exception_variant (fn_type, noexcept_true_spec); 9314 if (use_aeabi_atexit) 9315 { 9316 name = "__aeabi_atexit"; 9317 push_to_top_level (); 9318 int n = push_namespace (get_identifier ("__aeabiv1"), false); 9319 ctx = current_namespace; 9320 while (n--) 9321 pop_namespace (); 9322 pop_from_top_level (); 9323 } 9324 else 9325 { 9326 name = "__cxa_atexit"; 9327 ctx = abi_node; 9328 } 9329 } 9330 else 9331 { 9332 /* The declaration for `atexit' is: 9333 9334 int atexit (void (*)()); 9335 9336 We build up the argument types and then the function type 9337 itself. */ 9338 fn_ptr_type = get_atexit_fn_ptr_type (); 9339 /* Build the final atexit type. */ 9340 fn_type = build_function_type_list (integer_type_node, 9341 fn_ptr_type, NULL_TREE); 9342 /* ... which needs noexcept. */ 9343 fn_type = build_exception_variant (fn_type, noexcept_true_spec); 9344 name = "atexit"; 9345 } 9346 9347 /* Now, build the function declaration. */ 9348 push_lang_context (lang_name_c); 9349 auto cookie = push_abi_namespace (ctx); 9350 atexit_fndecl = build_library_fn_ptr (name, fn_type, ECF_LEAF | ECF_NOTHROW); 9351 DECL_CONTEXT (atexit_fndecl) = FROB_CONTEXT (current_namespace); 9352 /* Install as hidden builtin so we're (a) more relaxed about 9353 exception spec matching and (b) will not give a confusing location 9354 in diagnostic and (c) won't magically appear in user-visible name 9355 lookups. */ 9356 DECL_SOURCE_LOCATION (atexit_fndecl) = BUILTINS_LOCATION; 9357 atexit_fndecl = pushdecl (atexit_fndecl, /*hiding=*/true); 9358 pop_abi_namespace (cookie, ctx); 9359 mark_used (atexit_fndecl); 9360 pop_lang_context (); 9361 atexit_node = decay_conversion (atexit_fndecl, tf_warning_or_error); 9362 9363 return atexit_node; 9364 } 9365 9366 /* Like get_atexit_node, but for thread-local cleanups. */ 9367 9368 static tree 9369 get_thread_atexit_node (void) 9370 { 9371 /* The declaration for `__cxa_thread_atexit' is: 9372 9373 int __cxa_thread_atexit (void (*)(void *), void *, void *) */ 9374 tree fn_type = build_function_type_list (integer_type_node, 9375 get_atexit_fn_ptr_type (), 9376 ptr_type_node, ptr_type_node, 9377 NULL_TREE); 9378 9379 /* Now, build the function declaration. */ 9380 tree atexit_fndecl = build_library_fn_ptr ("__cxa_thread_atexit", fn_type, 9381 ECF_LEAF | ECF_NOTHROW); 9382 return decay_conversion (atexit_fndecl, tf_warning_or_error); 9383 } 9384 9385 /* Returns the __dso_handle VAR_DECL. */ 9386 9387 static tree 9388 get_dso_handle_node (void) 9389 { 9390 if (dso_handle_node) 9391 return dso_handle_node; 9392 9393 /* Declare the variable. */ 9394 dso_handle_node = declare_global_var (get_identifier ("__dso_handle"), 9395 ptr_type_node); 9396 9397 #ifdef HAVE_GAS_HIDDEN 9398 if (dso_handle_node != error_mark_node) 9399 { 9400 DECL_VISIBILITY (dso_handle_node) = VISIBILITY_HIDDEN; 9401 DECL_VISIBILITY_SPECIFIED (dso_handle_node) = 1; 9402 } 9403 #endif 9404 9405 return dso_handle_node; 9406 } 9407 9408 /* Begin a new function with internal linkage whose job will be simply 9409 to destroy some particular variable. */ 9410 9411 static GTY(()) int start_cleanup_cnt; 9412 9413 static tree 9414 start_cleanup_fn (void) 9415 { 9416 char name[32]; 9417 9418 push_to_top_level (); 9419 9420 /* No need to mangle this. */ 9421 push_lang_context (lang_name_c); 9422 9423 /* Build the name of the function. */ 9424 sprintf (name, "__tcf_%d", start_cleanup_cnt++); 9425 /* Build the function declaration. */ 9426 tree fntype = TREE_TYPE (get_atexit_fn_ptr_type ()); 9427 tree fndecl = build_lang_decl (FUNCTION_DECL, get_identifier (name), fntype); 9428 DECL_CONTEXT (fndecl) = FROB_CONTEXT (current_namespace); 9429 /* It's a function with internal linkage, generated by the 9430 compiler. */ 9431 TREE_PUBLIC (fndecl) = 0; 9432 DECL_ARTIFICIAL (fndecl) = 1; 9433 /* Make the function `inline' so that it is only emitted if it is 9434 actually needed. It is unlikely that it will be inlined, since 9435 it is only called via a function pointer, but we avoid unnecessary 9436 emissions this way. */ 9437 DECL_DECLARED_INLINE_P (fndecl) = 1; 9438 DECL_INTERFACE_KNOWN (fndecl) = 1; 9439 if (flag_use_cxa_atexit && !targetm.cxx.use_atexit_for_cxa_atexit ()) 9440 { 9441 /* Build the parameter. */ 9442 tree parmdecl = cp_build_parm_decl (fndecl, NULL_TREE, ptr_type_node); 9443 TREE_USED (parmdecl) = 1; 9444 DECL_READ_P (parmdecl) = 1; 9445 DECL_ARGUMENTS (fndecl) = parmdecl; 9446 } 9447 9448 fndecl = pushdecl (fndecl, /*hidden=*/true); 9449 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED); 9450 9451 pop_lang_context (); 9452 9453 return current_function_decl; 9454 } 9455 9456 /* Finish the cleanup function begun by start_cleanup_fn. */ 9457 9458 static void 9459 end_cleanup_fn (void) 9460 { 9461 expand_or_defer_fn (finish_function (/*inline_p=*/false)); 9462 9463 pop_from_top_level (); 9464 } 9465 9466 /* Generate code to handle the destruction of DECL, an object with 9467 static storage duration. */ 9468 9469 tree 9470 register_dtor_fn (tree decl) 9471 { 9472 tree cleanup; 9473 tree addr; 9474 tree compound_stmt; 9475 tree fcall; 9476 tree type; 9477 bool ob_parm, dso_parm, use_dtor; 9478 tree arg0, arg1, arg2; 9479 tree atex_node; 9480 9481 type = TREE_TYPE (decl); 9482 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type)) 9483 return void_node; 9484 9485 if (decl_maybe_constant_destruction (decl, type) 9486 && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl)) 9487 { 9488 cxx_maybe_build_cleanup (decl, tf_warning_or_error); 9489 return void_node; 9490 } 9491 9492 /* If we're using "__cxa_atexit" (or "__cxa_thread_atexit" or 9493 "__aeabi_atexit"), and DECL is a class object, we can just pass the 9494 destructor to "__cxa_atexit"; we don't have to build a temporary 9495 function to do the cleanup. */ 9496 dso_parm = (flag_use_cxa_atexit 9497 && !targetm.cxx.use_atexit_for_cxa_atexit ()); 9498 ob_parm = (CP_DECL_THREAD_LOCAL_P (decl) || dso_parm); 9499 use_dtor = ob_parm && CLASS_TYPE_P (type); 9500 if (use_dtor) 9501 { 9502 cleanup = get_class_binding (type, complete_dtor_identifier); 9503 9504 /* Make sure it is accessible. */ 9505 perform_or_defer_access_check (TYPE_BINFO (type), cleanup, cleanup, 9506 tf_warning_or_error); 9507 } 9508 else 9509 { 9510 /* Call build_cleanup before we enter the anonymous function so 9511 that any access checks will be done relative to the current 9512 scope, rather than the scope of the anonymous function. */ 9513 build_cleanup (decl); 9514 9515 /* Now start the function. */ 9516 cleanup = start_cleanup_fn (); 9517 9518 /* Now, recompute the cleanup. It may contain SAVE_EXPRs that refer 9519 to the original function, rather than the anonymous one. That 9520 will make the back end think that nested functions are in use, 9521 which causes confusion. */ 9522 push_deferring_access_checks (dk_no_check); 9523 fcall = build_cleanup (decl); 9524 pop_deferring_access_checks (); 9525 9526 /* Create the body of the anonymous function. */ 9527 compound_stmt = begin_compound_stmt (BCS_FN_BODY); 9528 finish_expr_stmt (fcall); 9529 finish_compound_stmt (compound_stmt); 9530 end_cleanup_fn (); 9531 } 9532 9533 /* Call atexit with the cleanup function. */ 9534 mark_used (cleanup); 9535 cleanup = build_address (cleanup); 9536 9537 if (CP_DECL_THREAD_LOCAL_P (decl)) 9538 atex_node = get_thread_atexit_node (); 9539 else 9540 atex_node = get_atexit_node (); 9541 9542 if (use_dtor) 9543 { 9544 /* We must convert CLEANUP to the type that "__cxa_atexit" 9545 expects. */ 9546 cleanup = build_nop (get_atexit_fn_ptr_type (), cleanup); 9547 /* "__cxa_atexit" will pass the address of DECL to the 9548 cleanup function. */ 9549 mark_used (decl); 9550 addr = build_address (decl); 9551 /* The declared type of the parameter to "__cxa_atexit" is 9552 "void *". For plain "T*", we could just let the 9553 machinery in cp_build_function_call convert it -- but if the 9554 type is "cv-qualified T *", then we need to convert it 9555 before passing it in, to avoid spurious errors. */ 9556 addr = build_nop (ptr_type_node, addr); 9557 } 9558 else 9559 /* Since the cleanup functions we build ignore the address 9560 they're given, there's no reason to pass the actual address 9561 in, and, in general, it's cheaper to pass NULL than any 9562 other value. */ 9563 addr = null_pointer_node; 9564 9565 if (dso_parm) 9566 arg2 = cp_build_addr_expr (get_dso_handle_node (), 9567 tf_warning_or_error); 9568 else if (ob_parm) 9569 /* Just pass NULL to the dso handle parm if we don't actually 9570 have a DSO handle on this target. */ 9571 arg2 = null_pointer_node; 9572 else 9573 arg2 = NULL_TREE; 9574 9575 if (ob_parm) 9576 { 9577 if (!CP_DECL_THREAD_LOCAL_P (decl) 9578 && targetm.cxx.use_aeabi_atexit ()) 9579 { 9580 arg1 = cleanup; 9581 arg0 = addr; 9582 } 9583 else 9584 { 9585 arg1 = addr; 9586 arg0 = cleanup; 9587 } 9588 } 9589 else 9590 { 9591 arg0 = cleanup; 9592 arg1 = NULL_TREE; 9593 } 9594 return cp_build_function_call_nary (atex_node, tf_warning_or_error, 9595 arg0, arg1, arg2, NULL_TREE); 9596 } 9597 9598 /* DECL is a VAR_DECL with static storage duration. INIT, if present, 9599 is its initializer. Generate code to handle the construction 9600 and destruction of DECL. */ 9601 9602 static void 9603 expand_static_init (tree decl, tree init) 9604 { 9605 gcc_assert (VAR_P (decl)); 9606 gcc_assert (TREE_STATIC (decl)); 9607 9608 /* Some variables require no dynamic initialization. */ 9609 if (decl_maybe_constant_destruction (decl, TREE_TYPE (decl))) 9610 { 9611 /* Make sure the destructor is callable. */ 9612 cxx_maybe_build_cleanup (decl, tf_warning_or_error); 9613 if (!init) 9614 return; 9615 } 9616 9617 if (CP_DECL_THREAD_LOCAL_P (decl) && DECL_GNU_TLS_P (decl) 9618 && !DECL_FUNCTION_SCOPE_P (decl)) 9619 { 9620 location_t dloc = DECL_SOURCE_LOCATION (decl); 9621 if (init) 9622 error_at (dloc, "non-local variable %qD declared %<__thread%> " 9623 "needs dynamic initialization", decl); 9624 else 9625 error_at (dloc, "non-local variable %qD declared %<__thread%> " 9626 "has a non-trivial destructor", decl); 9627 static bool informed; 9628 if (!informed) 9629 { 9630 inform (dloc, "C++11 %<thread_local%> allows dynamic " 9631 "initialization and destruction"); 9632 informed = true; 9633 } 9634 return; 9635 } 9636 9637 if (DECL_FUNCTION_SCOPE_P (decl)) 9638 { 9639 /* Emit code to perform this initialization but once. */ 9640 tree if_stmt = NULL_TREE, inner_if_stmt = NULL_TREE; 9641 tree then_clause = NULL_TREE, inner_then_clause = NULL_TREE; 9642 tree guard, guard_addr; 9643 tree flag, begin; 9644 /* We don't need thread-safety code for thread-local vars. */ 9645 bool thread_guard = (flag_threadsafe_statics 9646 && !CP_DECL_THREAD_LOCAL_P (decl)); 9647 9648 /* Emit code to perform this initialization but once. This code 9649 looks like: 9650 9651 static <type> guard; 9652 if (!__atomic_load (guard.first_byte)) { 9653 if (__cxa_guard_acquire (&guard)) { 9654 bool flag = false; 9655 try { 9656 // Do initialization. 9657 flag = true; __cxa_guard_release (&guard); 9658 // Register variable for destruction at end of program. 9659 } catch { 9660 if (!flag) __cxa_guard_abort (&guard); 9661 } 9662 } 9663 } 9664 9665 Note that the `flag' variable is only set to 1 *after* the 9666 initialization is complete. This ensures that an exception, 9667 thrown during the construction, will cause the variable to 9668 reinitialized when we pass through this code again, as per: 9669 9670 [stmt.dcl] 9671 9672 If the initialization exits by throwing an exception, the 9673 initialization is not complete, so it will be tried again 9674 the next time control enters the declaration. 9675 9676 This process should be thread-safe, too; multiple threads 9677 should not be able to initialize the variable more than 9678 once. */ 9679 9680 /* Create the guard variable. */ 9681 guard = get_guard (decl); 9682 9683 /* Begin the conditional initialization. */ 9684 if_stmt = begin_if_stmt (); 9685 9686 finish_if_stmt_cond (get_guard_cond (guard, thread_guard), if_stmt); 9687 then_clause = begin_compound_stmt (BCS_NO_SCOPE); 9688 9689 if (thread_guard) 9690 { 9691 tree vfntype = NULL_TREE; 9692 tree acquire_name, release_name, abort_name; 9693 tree acquire_fn, release_fn, abort_fn; 9694 guard_addr = build_address (guard); 9695 9696 acquire_name = get_identifier ("__cxa_guard_acquire"); 9697 release_name = get_identifier ("__cxa_guard_release"); 9698 abort_name = get_identifier ("__cxa_guard_abort"); 9699 acquire_fn = get_global_binding (acquire_name); 9700 release_fn = get_global_binding (release_name); 9701 abort_fn = get_global_binding (abort_name); 9702 if (!acquire_fn) 9703 acquire_fn = push_library_fn 9704 (acquire_name, build_function_type_list (integer_type_node, 9705 TREE_TYPE (guard_addr), 9706 NULL_TREE), 9707 NULL_TREE, ECF_NOTHROW); 9708 if (!release_fn || !abort_fn) 9709 vfntype = build_function_type_list (void_type_node, 9710 TREE_TYPE (guard_addr), 9711 NULL_TREE); 9712 if (!release_fn) 9713 release_fn = push_library_fn (release_name, vfntype, NULL_TREE, 9714 ECF_NOTHROW); 9715 if (!abort_fn) 9716 abort_fn = push_library_fn (abort_name, vfntype, NULL_TREE, 9717 ECF_NOTHROW | ECF_LEAF); 9718 9719 inner_if_stmt = begin_if_stmt (); 9720 finish_if_stmt_cond (build_call_n (acquire_fn, 1, guard_addr), 9721 inner_if_stmt); 9722 9723 inner_then_clause = begin_compound_stmt (BCS_NO_SCOPE); 9724 begin = get_target_expr (boolean_false_node); 9725 flag = TARGET_EXPR_SLOT (begin); 9726 9727 TARGET_EXPR_CLEANUP (begin) 9728 = build3 (COND_EXPR, void_type_node, flag, 9729 void_node, 9730 build_call_n (abort_fn, 1, guard_addr)); 9731 CLEANUP_EH_ONLY (begin) = 1; 9732 9733 /* Do the initialization itself. */ 9734 init = add_stmt_to_compound (begin, init); 9735 init = add_stmt_to_compound (init, 9736 build2 (MODIFY_EXPR, void_type_node, 9737 flag, boolean_true_node)); 9738 9739 /* Use atexit to register a function for destroying this static 9740 variable. Do this before calling __cxa_guard_release. */ 9741 init = add_stmt_to_compound (init, register_dtor_fn (decl)); 9742 9743 init = add_stmt_to_compound (init, build_call_n (release_fn, 1, 9744 guard_addr)); 9745 } 9746 else 9747 { 9748 init = add_stmt_to_compound (init, set_guard (guard)); 9749 9750 /* Use atexit to register a function for destroying this static 9751 variable. */ 9752 init = add_stmt_to_compound (init, register_dtor_fn (decl)); 9753 } 9754 9755 finish_expr_stmt (init); 9756 9757 if (thread_guard) 9758 { 9759 finish_compound_stmt (inner_then_clause); 9760 finish_then_clause (inner_if_stmt); 9761 finish_if_stmt (inner_if_stmt); 9762 } 9763 9764 finish_compound_stmt (then_clause); 9765 finish_then_clause (if_stmt); 9766 finish_if_stmt (if_stmt); 9767 } 9768 else if (CP_DECL_THREAD_LOCAL_P (decl)) 9769 tls_aggregates = tree_cons (init, decl, tls_aggregates); 9770 else 9771 static_aggregates = tree_cons (init, decl, static_aggregates); 9772 } 9773 9774 9775 /* Make TYPE a complete type based on INITIAL_VALUE. 9777 Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered, 9778 2 if there was no information (in which case assume 0 if DO_DEFAULT), 9779 3 if the initializer list is empty (in pedantic mode). */ 9780 9781 int 9782 cp_complete_array_type (tree *ptype, tree initial_value, bool do_default) 9783 { 9784 int failure; 9785 tree type, elt_type; 9786 9787 /* Don't get confused by a CONSTRUCTOR for some other type. */ 9788 if (initial_value && TREE_CODE (initial_value) == CONSTRUCTOR 9789 && !BRACE_ENCLOSED_INITIALIZER_P (initial_value) 9790 && TREE_CODE (TREE_TYPE (initial_value)) != ARRAY_TYPE) 9791 return 1; 9792 9793 if (initial_value) 9794 { 9795 /* An array of character type can be initialized from a 9796 brace-enclosed string constant so call reshape_init to 9797 remove the optional braces from a braced string literal. */ 9798 if (char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (*ptype))) 9799 && BRACE_ENCLOSED_INITIALIZER_P (initial_value)) 9800 initial_value = reshape_init (*ptype, initial_value, 9801 tf_warning_or_error); 9802 9803 /* If any of the elements are parameter packs, we can't actually 9804 complete this type now because the array size is dependent. */ 9805 if (TREE_CODE (initial_value) == CONSTRUCTOR) 9806 for (auto &e: CONSTRUCTOR_ELTS (initial_value)) 9807 if (PACK_EXPANSION_P (e.value)) 9808 return 0; 9809 } 9810 9811 failure = complete_array_type (ptype, initial_value, do_default); 9812 9813 /* We can create the array before the element type is complete, which 9814 means that we didn't have these two bits set in the original type 9815 either. In completing the type, we are expected to propagate these 9816 bits. See also complete_type which does the same thing for arrays 9817 of fixed size. */ 9818 type = *ptype; 9819 if (type != error_mark_node && TYPE_DOMAIN (type)) 9820 { 9821 elt_type = TREE_TYPE (type); 9822 TYPE_NEEDS_CONSTRUCTING (type) = TYPE_NEEDS_CONSTRUCTING (elt_type); 9823 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type) 9824 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (elt_type); 9825 } 9826 9827 return failure; 9828 } 9829 9830 /* As above, but either give an error or reject zero-size arrays, depending 9831 on COMPLAIN. */ 9832 9833 int 9834 cp_complete_array_type_or_error (tree *ptype, tree initial_value, 9835 bool do_default, tsubst_flags_t complain) 9836 { 9837 int failure; 9838 bool sfinae = !(complain & tf_error); 9839 /* In SFINAE context we can't be lenient about zero-size arrays. */ 9840 if (sfinae) 9841 ++pedantic; 9842 failure = cp_complete_array_type (ptype, initial_value, do_default); 9843 if (sfinae) 9844 --pedantic; 9845 if (failure) 9846 { 9847 if (sfinae) 9848 /* Not an error. */; 9849 else if (failure == 1) 9850 error ("initializer fails to determine size of %qT", *ptype); 9851 else if (failure == 2) 9852 { 9853 if (do_default) 9854 error ("array size missing in %qT", *ptype); 9855 } 9856 else if (failure == 3) 9857 error ("zero-size array %qT", *ptype); 9858 *ptype = error_mark_node; 9859 } 9860 return failure; 9861 } 9862 9863 /* Return zero if something is declared to be a member of type 9865 CTYPE when in the context of CUR_TYPE. STRING is the error 9866 message to print in that case. Otherwise, quietly return 1. */ 9867 9868 static int 9869 member_function_or_else (tree ctype, tree cur_type, enum overload_flags flags) 9870 { 9871 if (ctype && ctype != cur_type) 9872 { 9873 if (flags == DTOR_FLAG) 9874 error ("destructor for alien class %qT cannot be a member", ctype); 9875 else 9876 error ("constructor for alien class %qT cannot be a member", ctype); 9877 return 0; 9878 } 9879 return 1; 9880 } 9881 9882 /* Subroutine of `grokdeclarator'. */ 9884 9885 /* Generate errors possibly applicable for a given set of specifiers. 9886 This is for ARM $7.1.2. */ 9887 9888 static void 9889 bad_specifiers (tree object, 9890 enum bad_spec_place type, 9891 int virtualp, 9892 int quals, 9893 int inlinep, 9894 int friendp, 9895 int raises, 9896 const location_t* locations) 9897 { 9898 switch (type) 9899 { 9900 case BSP_VAR: 9901 if (virtualp) 9902 error_at (locations[ds_virtual], 9903 "%qD declared as a %<virtual%> variable", object); 9904 if (quals) 9905 error ("%<const%> and %<volatile%> function specifiers on " 9906 "%qD invalid in variable declaration", object); 9907 break; 9908 case BSP_PARM: 9909 if (virtualp) 9910 error_at (locations[ds_virtual], 9911 "%qD declared as a %<virtual%> parameter", object); 9912 if (inlinep) 9913 error_at (locations[ds_inline], 9914 "%qD declared as an %<inline%> parameter", object); 9915 if (quals) 9916 error ("%<const%> and %<volatile%> function specifiers on " 9917 "%qD invalid in parameter declaration", object); 9918 break; 9919 case BSP_TYPE: 9920 if (virtualp) 9921 error_at (locations[ds_virtual], 9922 "%qD declared as a %<virtual%> type", object); 9923 if (inlinep) 9924 error_at (locations[ds_inline], 9925 "%qD declared as an %<inline%> type", object); 9926 if (quals) 9927 error ("%<const%> and %<volatile%> function specifiers on " 9928 "%qD invalid in type declaration", object); 9929 break; 9930 case BSP_FIELD: 9931 if (virtualp) 9932 error_at (locations[ds_virtual], 9933 "%qD declared as a %<virtual%> field", object); 9934 if (inlinep) 9935 error_at (locations[ds_inline], 9936 "%qD declared as an %<inline%> field", object); 9937 if (quals) 9938 error ("%<const%> and %<volatile%> function specifiers on " 9939 "%qD invalid in field declaration", object); 9940 break; 9941 default: 9942 gcc_unreachable(); 9943 } 9944 if (friendp) 9945 error ("%q+D declared as a friend", object); 9946 if (raises 9947 && !flag_noexcept_type 9948 && (TREE_CODE (object) == TYPE_DECL 9949 || (!TYPE_PTRFN_P (TREE_TYPE (object)) 9950 && !TYPE_REFFN_P (TREE_TYPE (object)) 9951 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (object))))) 9952 error ("%q+D declared with an exception specification", object); 9953 } 9954 9955 /* DECL is a member function or static data member and is presently 9956 being defined. Check that the definition is taking place in a 9957 valid namespace. */ 9958 9959 static void 9960 check_class_member_definition_namespace (tree decl) 9961 { 9962 /* These checks only apply to member functions and static data 9963 members. */ 9964 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl)); 9965 /* We check for problems with specializations in pt.cc in 9966 check_specialization_namespace, where we can issue better 9967 diagnostics. */ 9968 if (processing_specialization) 9969 return; 9970 /* We check this in check_explicit_instantiation_namespace. */ 9971 if (processing_explicit_instantiation) 9972 return; 9973 /* [class.mfct] 9974 9975 A member function definition that appears outside of the 9976 class definition shall appear in a namespace scope enclosing 9977 the class definition. 9978 9979 [class.static.data] 9980 9981 The definition for a static data member shall appear in a 9982 namespace scope enclosing the member's class definition. */ 9983 if (!is_ancestor (current_namespace, DECL_CONTEXT (decl))) 9984 permerror (input_location, "definition of %qD is not in namespace enclosing %qT", 9985 decl, DECL_CONTEXT (decl)); 9986 } 9987 9988 /* Build a PARM_DECL for the "this" parameter of FN. TYPE is the 9989 METHOD_TYPE for a non-static member function; QUALS are the 9990 cv-qualifiers that apply to the function. */ 9991 9992 tree 9993 build_this_parm (tree fn, tree type, cp_cv_quals quals) 9994 { 9995 tree this_type; 9996 tree qual_type; 9997 tree parm; 9998 cp_cv_quals this_quals; 9999 10000 if (CLASS_TYPE_P (type)) 10001 { 10002 this_type 10003 = cp_build_qualified_type (type, quals & ~TYPE_QUAL_RESTRICT); 10004 this_type = build_pointer_type (this_type); 10005 } 10006 else 10007 this_type = type_of_this_parm (type); 10008 /* The `this' parameter is implicitly `const'; it cannot be 10009 assigned to. */ 10010 this_quals = (quals & TYPE_QUAL_RESTRICT) | TYPE_QUAL_CONST; 10011 qual_type = cp_build_qualified_type (this_type, this_quals); 10012 parm = build_artificial_parm (fn, this_identifier, qual_type); 10013 cp_apply_type_quals_to_decl (this_quals, parm); 10014 return parm; 10015 } 10016 10017 /* DECL is a static member function. Complain if it was declared 10018 with function-cv-quals. */ 10019 10020 static void 10021 check_static_quals (tree decl, cp_cv_quals quals) 10022 { 10023 if (quals != TYPE_UNQUALIFIED) 10024 error ("static member function %q#D declared with type qualifiers", 10025 decl); 10026 } 10027 10028 // Check that FN takes no arguments and returns bool. 10029 static void 10030 check_concept_fn (tree fn) 10031 { 10032 // A constraint is nullary. 10033 if (DECL_ARGUMENTS (fn)) 10034 error_at (DECL_SOURCE_LOCATION (fn), 10035 "concept %q#D declared with function parameters", fn); 10036 10037 // The declared return type of the concept shall be bool, and 10038 // it shall not be deduced from it definition. 10039 tree type = TREE_TYPE (TREE_TYPE (fn)); 10040 if (is_auto (type)) 10041 error_at (DECL_SOURCE_LOCATION (fn), 10042 "concept %q#D declared with a deduced return type", fn); 10043 else if (type != boolean_type_node) 10044 error_at (DECL_SOURCE_LOCATION (fn), 10045 "concept %q#D with non-%<bool%> return type %qT", fn, type); 10046 } 10047 10048 /* Helper function. Replace the temporary this parameter injected 10049 during cp_finish_omp_declare_simd with the real this parameter. */ 10050 10051 static tree 10052 declare_simd_adjust_this (tree *tp, int *walk_subtrees, void *data) 10053 { 10054 tree this_parm = (tree) data; 10055 if (TREE_CODE (*tp) == PARM_DECL 10056 && DECL_NAME (*tp) == this_identifier 10057 && *tp != this_parm) 10058 *tp = this_parm; 10059 else if (TYPE_P (*tp)) 10060 *walk_subtrees = 0; 10061 return NULL_TREE; 10062 } 10063 10064 /* CTYPE is class type, or null if non-class. 10065 TYPE is type this FUNCTION_DECL should have, either FUNCTION_TYPE 10066 or METHOD_TYPE. 10067 DECLARATOR is the function's name. 10068 PARMS is a chain of PARM_DECLs for the function. 10069 VIRTUALP is truthvalue of whether the function is virtual or not. 10070 FLAGS are to be passed through to `grokclassfn'. 10071 QUALS are qualifiers indicating whether the function is `const' 10072 or `volatile'. 10073 RAISES is a list of exceptions that this function can raise. 10074 CHECK is 1 if we must find this method in CTYPE, 0 if we should 10075 not look, and -1 if we should not call `grokclassfn' at all. 10076 10077 SFK is the kind of special function (if any) for the new function. 10078 10079 Returns `NULL_TREE' if something goes wrong, after issuing 10080 applicable error messages. */ 10081 10082 static tree 10083 grokfndecl (tree ctype, 10084 tree type, 10085 tree declarator, 10086 tree parms, 10087 tree orig_declarator, 10088 const cp_decl_specifier_seq *declspecs, 10089 tree decl_reqs, 10090 int virtualp, 10091 enum overload_flags flags, 10092 cp_cv_quals quals, 10093 cp_ref_qualifier rqual, 10094 tree raises, 10095 int check, 10096 int friendp, 10097 int publicp, 10098 int inlinep, 10099 bool deletedp, 10100 special_function_kind sfk, 10101 bool funcdef_flag, 10102 bool late_return_type_p, 10103 int template_count, 10104 tree in_namespace, 10105 tree* attrlist, 10106 location_t location) 10107 { 10108 tree decl; 10109 int staticp = ctype && TREE_CODE (type) == FUNCTION_TYPE; 10110 tree t; 10111 10112 if (location == UNKNOWN_LOCATION) 10113 location = input_location; 10114 10115 /* Was the concept specifier present? */ 10116 bool concept_p = inlinep & 4; 10117 10118 /* Concept declarations must have a corresponding definition. */ 10119 if (concept_p && !funcdef_flag) 10120 { 10121 error_at (location, "concept %qD has no definition", declarator); 10122 return NULL_TREE; 10123 } 10124 10125 type = build_cp_fntype_variant (type, rqual, raises, late_return_type_p); 10126 10127 decl = build_lang_decl_loc (location, FUNCTION_DECL, declarator, type); 10128 10129 /* Set the constraints on the declaration. */ 10130 if (flag_concepts) 10131 { 10132 tree tmpl_reqs = NULL_TREE; 10133 tree ctx = friendp ? current_class_type : ctype; 10134 bool block_local = TREE_CODE (current_scope ()) == FUNCTION_DECL; 10135 bool memtmpl = (!block_local 10136 && (current_template_depth 10137 > template_class_depth (ctx))); 10138 if (memtmpl) 10139 { 10140 if (!current_template_parms) 10141 /* If there are no template parameters, something must have 10142 gone wrong. */ 10143 gcc_assert (seen_error ()); 10144 else 10145 tmpl_reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms); 10146 } 10147 tree ci = build_constraints (tmpl_reqs, decl_reqs); 10148 if (concept_p && ci) 10149 { 10150 error_at (location, "a function concept cannot be constrained"); 10151 ci = NULL_TREE; 10152 } 10153 /* C++20 CA378: Remove non-templated constrained functions. */ 10154 if (ci 10155 && (block_local 10156 || (!flag_concepts_ts 10157 && (!processing_template_decl 10158 || (friendp && !memtmpl && !funcdef_flag))))) 10159 { 10160 error_at (location, "constraints on a non-templated function"); 10161 ci = NULL_TREE; 10162 } 10163 set_constraints (decl, ci); 10164 } 10165 10166 if (TREE_CODE (type) == METHOD_TYPE) 10167 { 10168 tree parm = build_this_parm (decl, type, quals); 10169 DECL_CHAIN (parm) = parms; 10170 parms = parm; 10171 10172 /* Allocate space to hold the vptr bit if needed. */ 10173 SET_DECL_ALIGN (decl, MINIMUM_METHOD_BOUNDARY); 10174 } 10175 10176 DECL_ARGUMENTS (decl) = parms; 10177 for (t = parms; t; t = DECL_CHAIN (t)) 10178 DECL_CONTEXT (t) = decl; 10179 10180 /* Propagate volatile out from type to decl. */ 10181 if (TYPE_VOLATILE (type)) 10182 TREE_THIS_VOLATILE (decl) = 1; 10183 10184 /* Setup decl according to sfk. */ 10185 switch (sfk) 10186 { 10187 case sfk_constructor: 10188 case sfk_copy_constructor: 10189 case sfk_move_constructor: 10190 DECL_CXX_CONSTRUCTOR_P (decl) = 1; 10191 DECL_NAME (decl) = ctor_identifier; 10192 break; 10193 case sfk_destructor: 10194 DECL_CXX_DESTRUCTOR_P (decl) = 1; 10195 DECL_NAME (decl) = dtor_identifier; 10196 break; 10197 default: 10198 break; 10199 } 10200 10201 if (friendp && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR) 10202 { 10203 if (funcdef_flag) 10204 error_at (location, 10205 "defining explicit specialization %qD in friend declaration", 10206 orig_declarator); 10207 else 10208 { 10209 tree fns = TREE_OPERAND (orig_declarator, 0); 10210 tree args = TREE_OPERAND (orig_declarator, 1); 10211 10212 if (PROCESSING_REAL_TEMPLATE_DECL_P ()) 10213 { 10214 /* Something like `template <class T> friend void f<T>()'. */ 10215 error_at (location, 10216 "invalid use of template-id %qD in declaration " 10217 "of primary template", 10218 orig_declarator); 10219 return NULL_TREE; 10220 } 10221 10222 10223 /* A friend declaration of the form friend void f<>(). Record 10224 the information in the TEMPLATE_ID_EXPR. */ 10225 SET_DECL_IMPLICIT_INSTANTIATION (decl); 10226 10227 gcc_assert (identifier_p (fns) || OVL_P (fns)); 10228 DECL_TEMPLATE_INFO (decl) = build_template_info (fns, args); 10229 10230 for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); t; t = TREE_CHAIN (t)) 10231 if (TREE_PURPOSE (t) 10232 && TREE_CODE (TREE_PURPOSE (t)) == DEFERRED_PARSE) 10233 { 10234 error_at (defparse_location (TREE_PURPOSE (t)), 10235 "default arguments are not allowed in declaration " 10236 "of friend template specialization %qD", 10237 decl); 10238 return NULL_TREE; 10239 } 10240 10241 if (inlinep & 1) 10242 { 10243 error_at (declspecs->locations[ds_inline], 10244 "%<inline%> is not allowed in declaration of friend " 10245 "template specialization %qD", 10246 decl); 10247 return NULL_TREE; 10248 } 10249 } 10250 } 10251 10252 /* C++17 11.3.6/4: "If a friend declaration specifies a default argument 10253 expression, that declaration shall be a definition..." */ 10254 if (friendp && !funcdef_flag) 10255 { 10256 for (tree t = FUNCTION_FIRST_USER_PARMTYPE (decl); 10257 t && t != void_list_node; t = TREE_CHAIN (t)) 10258 if (TREE_PURPOSE (t)) 10259 { 10260 permerror (DECL_SOURCE_LOCATION (decl), 10261 "friend declaration of %qD specifies default " 10262 "arguments and isn%'t a definition", decl); 10263 break; 10264 } 10265 } 10266 10267 /* If this decl has namespace scope, set that up. */ 10268 if (in_namespace) 10269 set_decl_namespace (decl, in_namespace, friendp); 10270 else if (ctype) 10271 DECL_CONTEXT (decl) = ctype; 10272 else 10273 DECL_CONTEXT (decl) = FROB_CONTEXT (current_decl_namespace ()); 10274 10275 /* `main' and builtins have implicit 'C' linkage. */ 10276 if (ctype == NULL_TREE 10277 && DECL_FILE_SCOPE_P (decl) 10278 && current_lang_name == lang_name_cplusplus 10279 && (MAIN_NAME_P (declarator) 10280 || (IDENTIFIER_LENGTH (declarator) > 10 10281 && IDENTIFIER_POINTER (declarator)[0] == '_' 10282 && IDENTIFIER_POINTER (declarator)[1] == '_' 10283 && startswith (IDENTIFIER_POINTER (declarator) + 2, 10284 "builtin_")) 10285 || (targetcm.cxx_implicit_extern_c 10286 && (targetcm.cxx_implicit_extern_c 10287 (IDENTIFIER_POINTER (declarator)))))) 10288 SET_DECL_LANGUAGE (decl, lang_c); 10289 10290 /* Should probably propagate const out from type to decl I bet (mrs). */ 10291 if (staticp) 10292 { 10293 DECL_STATIC_FUNCTION_P (decl) = 1; 10294 DECL_CONTEXT (decl) = ctype; 10295 } 10296 10297 if (deletedp) 10298 DECL_DELETED_FN (decl) = 1; 10299 10300 if (ctype && funcdef_flag) 10301 check_class_member_definition_namespace (decl); 10302 10303 if (ctype == NULL_TREE && DECL_MAIN_P (decl)) 10304 { 10305 if (PROCESSING_REAL_TEMPLATE_DECL_P()) 10306 error_at (location, "cannot declare %<::main%> to be a template"); 10307 if (inlinep & 1) 10308 error_at (declspecs->locations[ds_inline], 10309 "cannot declare %<::main%> to be inline"); 10310 if (inlinep & 2) 10311 error_at (declspecs->locations[ds_constexpr], 10312 "cannot declare %<::main%> to be %qs", "constexpr"); 10313 if (inlinep & 8) 10314 error_at (declspecs->locations[ds_consteval], 10315 "cannot declare %<::main%> to be %qs", "consteval"); 10316 if (!publicp) 10317 error_at (location, "cannot declare %<::main%> to be static"); 10318 inlinep = 0; 10319 publicp = 1; 10320 } 10321 10322 /* Members of anonymous types and local classes have no linkage; make 10323 them internal. If a typedef is made later, this will be changed. */ 10324 if (ctype && (!TREE_PUBLIC (TYPE_MAIN_DECL (ctype)) 10325 || decl_function_context (TYPE_MAIN_DECL (ctype)))) 10326 publicp = 0; 10327 10328 if (publicp && cxx_dialect == cxx98) 10329 { 10330 /* [basic.link]: A name with no linkage (notably, the name of a class 10331 or enumeration declared in a local scope) shall not be used to 10332 declare an entity with linkage. 10333 10334 DR 757 relaxes this restriction for C++0x. */ 10335 no_linkage_error (decl); 10336 } 10337 10338 TREE_PUBLIC (decl) = publicp; 10339 if (! publicp) 10340 { 10341 DECL_INTERFACE_KNOWN (decl) = 1; 10342 DECL_NOT_REALLY_EXTERN (decl) = 1; 10343 } 10344 10345 /* If the declaration was declared inline, mark it as such. */ 10346 if (inlinep) 10347 { 10348 DECL_DECLARED_INLINE_P (decl) = 1; 10349 if (publicp) 10350 DECL_COMDAT (decl) = 1; 10351 } 10352 if (inlinep & 2) 10353 DECL_DECLARED_CONSTEXPR_P (decl) = true; 10354 else if (inlinep & 8) 10355 { 10356 DECL_DECLARED_CONSTEXPR_P (decl) = true; 10357 SET_DECL_IMMEDIATE_FUNCTION_P (decl); 10358 } 10359 10360 // If the concept declaration specifier was found, check 10361 // that the declaration satisfies the necessary requirements. 10362 if (concept_p) 10363 { 10364 DECL_DECLARED_CONCEPT_P (decl) = true; 10365 check_concept_fn (decl); 10366 } 10367 10368 DECL_EXTERNAL (decl) = 1; 10369 if (TREE_CODE (type) == FUNCTION_TYPE) 10370 { 10371 if (quals || rqual) 10372 TREE_TYPE (decl) = apply_memfn_quals (TREE_TYPE (decl), 10373 TYPE_UNQUALIFIED, 10374 REF_QUAL_NONE); 10375 10376 if (quals) 10377 { 10378 error (ctype 10379 ? G_("static member function %qD cannot have cv-qualifier") 10380 : G_("non-member function %qD cannot have cv-qualifier"), 10381 decl); 10382 quals = TYPE_UNQUALIFIED; 10383 } 10384 10385 if (rqual) 10386 { 10387 error (ctype 10388 ? G_("static member function %qD cannot have ref-qualifier") 10389 : G_("non-member function %qD cannot have ref-qualifier"), 10390 decl); 10391 rqual = REF_QUAL_NONE; 10392 } 10393 } 10394 10395 if (deduction_guide_p (decl)) 10396 { 10397 tree type = TREE_TYPE (DECL_NAME (decl)); 10398 if (in_namespace == NULL_TREE 10399 && CP_DECL_CONTEXT (decl) != CP_TYPE_CONTEXT (type)) 10400 { 10401 error_at (location, "deduction guide %qD must be declared in the " 10402 "same scope as %qT", decl, type); 10403 inform (location_of (type), " declared here"); 10404 return NULL_TREE; 10405 } 10406 if (DECL_CLASS_SCOPE_P (decl) 10407 && current_access_specifier != declared_access (TYPE_NAME (type))) 10408 { 10409 error_at (location, "deduction guide %qD must have the same access " 10410 "as %qT", decl, type); 10411 inform (location_of (type), " declared here"); 10412 } 10413 if (funcdef_flag) 10414 error_at (location, 10415 "deduction guide %qD must not have a function body", decl); 10416 } 10417 else if (IDENTIFIER_ANY_OP_P (DECL_NAME (decl)) 10418 && !grok_op_properties (decl, /*complain=*/true)) 10419 return NULL_TREE; 10420 else if (UDLIT_OPER_P (DECL_NAME (decl))) 10421 { 10422 bool long_long_unsigned_p; 10423 bool long_double_p; 10424 const char *suffix = NULL; 10425 /* [over.literal]/6: Literal operators shall not have C linkage. */ 10426 if (DECL_LANGUAGE (decl) == lang_c) 10427 { 10428 error_at (location, "literal operator with C linkage"); 10429 maybe_show_extern_c_location (); 10430 return NULL_TREE; 10431 } 10432 10433 if (DECL_NAMESPACE_SCOPE_P (decl)) 10434 { 10435 if (!check_literal_operator_args (decl, &long_long_unsigned_p, 10436 &long_double_p)) 10437 { 10438 error_at (location, "%qD has invalid argument list", decl); 10439 return NULL_TREE; 10440 } 10441 10442 suffix = UDLIT_OP_SUFFIX (DECL_NAME (decl)); 10443 if (long_long_unsigned_p) 10444 { 10445 if (cpp_interpret_int_suffix (parse_in, suffix, strlen (suffix))) 10446 warning_at (location, 0, "integer suffix %qs" 10447 " shadowed by implementation", suffix); 10448 } 10449 else if (long_double_p) 10450 { 10451 if (cpp_interpret_float_suffix (parse_in, suffix, strlen (suffix))) 10452 warning_at (location, 0, "floating-point suffix %qs" 10453 " shadowed by implementation", suffix); 10454 } 10455 /* 17.6.3.3.5 */ 10456 if (suffix[0] != '_' 10457 && !current_function_decl && !(friendp && !funcdef_flag)) 10458 warning_at (location, OPT_Wliteral_suffix, 10459 "literal operator suffixes not preceded by %<_%>" 10460 " are reserved for future standardization"); 10461 } 10462 else 10463 { 10464 error_at (location, "%qD must be a non-member function", decl); 10465 return NULL_TREE; 10466 } 10467 } 10468 10469 if (funcdef_flag) 10470 /* Make the init_value nonzero so pushdecl knows this is not 10471 tentative. error_mark_node is replaced later with the BLOCK. */ 10472 DECL_INITIAL (decl) = error_mark_node; 10473 10474 if (TYPE_NOTHROW_P (type) || nothrow_libfn_p (decl)) 10475 TREE_NOTHROW (decl) = 1; 10476 10477 if (flag_openmp || flag_openmp_simd) 10478 { 10479 /* Adjust "omp declare simd" attributes. */ 10480 tree ods = lookup_attribute ("omp declare simd", *attrlist); 10481 if (ods) 10482 { 10483 tree attr; 10484 for (attr = ods; attr; 10485 attr = lookup_attribute ("omp declare simd", TREE_CHAIN (attr))) 10486 { 10487 if (TREE_CODE (type) == METHOD_TYPE) 10488 walk_tree (&TREE_VALUE (attr), declare_simd_adjust_this, 10489 DECL_ARGUMENTS (decl), NULL); 10490 if (TREE_VALUE (attr) != NULL_TREE) 10491 { 10492 tree cl = TREE_VALUE (TREE_VALUE (attr)); 10493 cl = c_omp_declare_simd_clauses_to_numbers 10494 (DECL_ARGUMENTS (decl), cl); 10495 if (cl) 10496 TREE_VALUE (TREE_VALUE (attr)) = cl; 10497 else 10498 TREE_VALUE (attr) = NULL_TREE; 10499 } 10500 } 10501 } 10502 } 10503 10504 /* Caller will do the rest of this. */ 10505 if (check < 0) 10506 return decl; 10507 10508 if (ctype != NULL_TREE) 10509 grokclassfn (ctype, decl, flags); 10510 10511 /* 12.4/3 */ 10512 if (cxx_dialect >= cxx11 10513 && DECL_DESTRUCTOR_P (decl) 10514 && !TYPE_BEING_DEFINED (DECL_CONTEXT (decl)) 10515 && !processing_template_decl) 10516 deduce_noexcept_on_destructor (decl); 10517 10518 set_originating_module (decl); 10519 10520 decl = check_explicit_specialization (orig_declarator, decl, 10521 template_count, 10522 2 * funcdef_flag + 10523 4 * (friendp != 0) + 10524 8 * concept_p, 10525 *attrlist); 10526 if (decl == error_mark_node) 10527 return NULL_TREE; 10528 10529 if (DECL_STATIC_FUNCTION_P (decl)) 10530 check_static_quals (decl, quals); 10531 10532 if (attrlist) 10533 { 10534 cplus_decl_attributes (&decl, *attrlist, 0); 10535 *attrlist = NULL_TREE; 10536 } 10537 10538 /* Check main's type after attributes have been applied. */ 10539 if (ctype == NULL_TREE && DECL_MAIN_P (decl)) 10540 { 10541 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)), 10542 integer_type_node)) 10543 { 10544 tree oldtypeargs = TYPE_ARG_TYPES (TREE_TYPE (decl)); 10545 tree newtype; 10546 error_at (declspecs->locations[ds_type_spec], 10547 "%<::main%> must return %<int%>"); 10548 newtype = build_function_type (integer_type_node, oldtypeargs); 10549 TREE_TYPE (decl) = newtype; 10550 } 10551 if (warn_main) 10552 check_main_parameter_types (decl); 10553 } 10554 10555 if (ctype != NULL_TREE && check) 10556 { 10557 tree old_decl = check_classfn (ctype, decl, 10558 (current_template_depth 10559 > template_class_depth (ctype)) 10560 ? current_template_parms 10561 : NULL_TREE); 10562 10563 if (old_decl == error_mark_node) 10564 return NULL_TREE; 10565 10566 if (old_decl) 10567 { 10568 tree ok; 10569 tree pushed_scope; 10570 10571 if (TREE_CODE (old_decl) == TEMPLATE_DECL) 10572 /* Because grokfndecl is always supposed to return a 10573 FUNCTION_DECL, we pull out the DECL_TEMPLATE_RESULT 10574 here. We depend on our callers to figure out that its 10575 really a template that's being returned. */ 10576 old_decl = DECL_TEMPLATE_RESULT (old_decl); 10577 10578 if (DECL_STATIC_FUNCTION_P (old_decl) 10579 && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE) 10580 { 10581 /* Remove the `this' parm added by grokclassfn. */ 10582 revert_static_member_fn (decl); 10583 check_static_quals (decl, quals); 10584 } 10585 if (DECL_ARTIFICIAL (old_decl)) 10586 { 10587 error ("definition of implicitly-declared %qD", old_decl); 10588 return NULL_TREE; 10589 } 10590 else if (DECL_DEFAULTED_FN (old_decl)) 10591 { 10592 error ("definition of explicitly-defaulted %q+D", decl); 10593 inform (DECL_SOURCE_LOCATION (old_decl), 10594 "%q#D explicitly defaulted here", old_decl); 10595 return NULL_TREE; 10596 } 10597 10598 /* Since we've smashed OLD_DECL to its 10599 DECL_TEMPLATE_RESULT, we must do the same to DECL. */ 10600 if (TREE_CODE (decl) == TEMPLATE_DECL) 10601 decl = DECL_TEMPLATE_RESULT (decl); 10602 10603 /* Attempt to merge the declarations. This can fail, in 10604 the case of some invalid specialization declarations. */ 10605 pushed_scope = push_scope (ctype); 10606 ok = duplicate_decls (decl, old_decl); 10607 if (pushed_scope) 10608 pop_scope (pushed_scope); 10609 if (!ok) 10610 { 10611 error ("no %q#D member function declared in class %qT", 10612 decl, ctype); 10613 return NULL_TREE; 10614 } 10615 if (ok == error_mark_node) 10616 return NULL_TREE; 10617 return old_decl; 10618 } 10619 } 10620 10621 if (DECL_CONSTRUCTOR_P (decl) && !grok_ctor_properties (ctype, decl)) 10622 return NULL_TREE; 10623 10624 if (ctype == NULL_TREE || check) 10625 return decl; 10626 10627 if (virtualp) 10628 DECL_VIRTUAL_P (decl) = 1; 10629 10630 return decl; 10631 } 10632 10633 /* decl is a FUNCTION_DECL. 10634 specifiers are the parsed virt-specifiers. 10635 10636 Set flags to reflect the virt-specifiers. 10637 10638 Returns decl. */ 10639 10640 static tree 10641 set_virt_specifiers (tree decl, cp_virt_specifiers specifiers) 10642 { 10643 if (decl == NULL_TREE) 10644 return decl; 10645 if (specifiers & VIRT_SPEC_OVERRIDE) 10646 DECL_OVERRIDE_P (decl) = 1; 10647 if (specifiers & VIRT_SPEC_FINAL) 10648 DECL_FINAL_P (decl) = 1; 10649 return decl; 10650 } 10651 10652 /* DECL is a VAR_DECL for a static data member. Set flags to reflect 10653 the linkage that DECL will receive in the object file. */ 10654 10655 static void 10656 set_linkage_for_static_data_member (tree decl) 10657 { 10658 /* A static data member always has static storage duration and 10659 external linkage. Note that static data members are forbidden in 10660 local classes -- the only situation in which a class has 10661 non-external linkage. */ 10662 TREE_PUBLIC (decl) = 1; 10663 TREE_STATIC (decl) = 1; 10664 /* For non-template classes, static data members are always put 10665 out in exactly those files where they are defined, just as 10666 with ordinary namespace-scope variables. */ 10667 if (!processing_template_decl) 10668 DECL_INTERFACE_KNOWN (decl) = 1; 10669 } 10670 10671 /* Create a VAR_DECL named NAME with the indicated TYPE. 10672 10673 If SCOPE is non-NULL, it is the class type or namespace containing 10674 the variable. If SCOPE is NULL, the variable should is created in 10675 the innermost enclosing scope. */ 10676 10677 static tree 10678 grokvardecl (tree type, 10679 tree name, 10680 tree orig_declarator, 10681 const cp_decl_specifier_seq *declspecs, 10682 int initialized, 10683 int type_quals, 10684 int inlinep, 10685 bool conceptp, 10686 int template_count, 10687 tree scope, 10688 location_t location) 10689 { 10690 tree decl; 10691 tree explicit_scope; 10692 10693 gcc_assert (!name || identifier_p (name)); 10694 10695 bool constp = (type_quals & TYPE_QUAL_CONST) != 0; 10696 bool volatilep = (type_quals & TYPE_QUAL_VOLATILE) != 0; 10697 10698 /* Compute the scope in which to place the variable, but remember 10699 whether or not that scope was explicitly specified by the user. */ 10700 explicit_scope = scope; 10701 if (!scope) 10702 { 10703 /* An explicit "extern" specifier indicates a namespace-scope 10704 variable. */ 10705 if (declspecs->storage_class == sc_extern) 10706 scope = current_decl_namespace (); 10707 else if (!at_function_scope_p ()) 10708 scope = current_scope (); 10709 } 10710 10711 if (scope 10712 && (/* If the variable is a namespace-scope variable declared in a 10713 template, we need DECL_LANG_SPECIFIC. */ 10714 (TREE_CODE (scope) == NAMESPACE_DECL && processing_template_decl) 10715 /* Similarly for namespace-scope variables with language linkage 10716 other than C++. */ 10717 || (TREE_CODE (scope) == NAMESPACE_DECL 10718 && current_lang_name != lang_name_cplusplus) 10719 /* Similarly for static data members. */ 10720 || TYPE_P (scope) 10721 /* Similarly for explicit specializations. */ 10722 || (orig_declarator 10723 && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR))) 10724 decl = build_lang_decl_loc (location, VAR_DECL, name, type); 10725 else 10726 decl = build_decl (location, VAR_DECL, name, type); 10727 10728 if (explicit_scope && TREE_CODE (explicit_scope) == NAMESPACE_DECL) 10729 set_decl_namespace (decl, explicit_scope, 0); 10730 else 10731 DECL_CONTEXT (decl) = FROB_CONTEXT (scope); 10732 10733 if (declspecs->storage_class == sc_extern) 10734 { 10735 DECL_THIS_EXTERN (decl) = 1; 10736 DECL_EXTERNAL (decl) = !initialized; 10737 } 10738 10739 if (DECL_CLASS_SCOPE_P (decl)) 10740 { 10741 set_linkage_for_static_data_member (decl); 10742 /* This function is only called with out-of-class definitions. */ 10743 DECL_EXTERNAL (decl) = 0; 10744 check_class_member_definition_namespace (decl); 10745 } 10746 /* At top level, either `static' or no s.c. makes a definition 10747 (perhaps tentative), and absence of `static' makes it public. */ 10748 else if (toplevel_bindings_p ()) 10749 { 10750 TREE_PUBLIC (decl) = (declspecs->storage_class != sc_static 10751 && (DECL_THIS_EXTERN (decl) 10752 || ! constp 10753 || volatilep 10754 || inlinep)); 10755 TREE_STATIC (decl) = ! DECL_EXTERNAL (decl); 10756 } 10757 /* Not at top level, only `static' makes a static definition. */ 10758 else 10759 { 10760 TREE_STATIC (decl) = declspecs->storage_class == sc_static; 10761 TREE_PUBLIC (decl) = DECL_EXTERNAL (decl); 10762 } 10763 10764 set_originating_module (decl); 10765 10766 if (decl_spec_seq_has_spec_p (declspecs, ds_thread)) 10767 { 10768 if (DECL_EXTERNAL (decl) || TREE_STATIC (decl)) 10769 { 10770 CP_DECL_THREAD_LOCAL_P (decl) = true; 10771 if (!processing_template_decl) 10772 set_decl_tls_model (decl, decl_default_tls_model (decl)); 10773 } 10774 if (declspecs->gnu_thread_keyword_p) 10775 SET_DECL_GNU_TLS_P (decl); 10776 } 10777 10778 /* If the type of the decl has no linkage, make sure that we'll 10779 notice that in mark_used. */ 10780 if (cxx_dialect > cxx98 10781 && decl_linkage (decl) != lk_none 10782 && DECL_LANG_SPECIFIC (decl) == NULL 10783 && !DECL_EXTERN_C_P (decl) 10784 && no_linkage_check (TREE_TYPE (decl), /*relaxed_p=*/false)) 10785 retrofit_lang_decl (decl); 10786 10787 if (TREE_PUBLIC (decl)) 10788 { 10789 /* [basic.link]: A name with no linkage (notably, the name of a class 10790 or enumeration declared in a local scope) shall not be used to 10791 declare an entity with linkage. 10792 10793 DR 757 relaxes this restriction for C++0x. */ 10794 if (cxx_dialect < cxx11) 10795 no_linkage_error (decl); 10796 } 10797 else 10798 DECL_INTERFACE_KNOWN (decl) = 1; 10799 10800 if (DECL_NAME (decl) 10801 && MAIN_NAME_P (DECL_NAME (decl)) 10802 && scope == global_namespace) 10803 error_at (DECL_SOURCE_LOCATION (decl), 10804 "cannot declare %<::main%> to be a global variable"); 10805 10806 /* Check that the variable can be safely declared as a concept. 10807 Note that this also forbids explicit specializations. */ 10808 if (conceptp) 10809 { 10810 if (!processing_template_decl) 10811 { 10812 error_at (declspecs->locations[ds_concept], 10813 "a non-template variable cannot be %<concept%>"); 10814 return NULL_TREE; 10815 } 10816 else if (!at_namespace_scope_p ()) 10817 { 10818 error_at (declspecs->locations[ds_concept], 10819 "concept must be defined at namespace scope"); 10820 return NULL_TREE; 10821 } 10822 else 10823 DECL_DECLARED_CONCEPT_P (decl) = true; 10824 if (!same_type_ignoring_top_level_qualifiers_p (type, boolean_type_node)) 10825 error_at (declspecs->locations[ds_type_spec], 10826 "concept must have type %<bool%>"); 10827 if (TEMPLATE_PARMS_CONSTRAINTS (current_template_parms)) 10828 { 10829 error_at (location, "a variable concept cannot be constrained"); 10830 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = NULL_TREE; 10831 } 10832 } 10833 else if (flag_concepts 10834 && current_template_depth > template_class_depth (scope)) 10835 { 10836 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms); 10837 tree ci = build_constraints (reqs, NULL_TREE); 10838 10839 set_constraints (decl, ci); 10840 } 10841 10842 // Handle explicit specializations and instantiations of variable templates. 10843 if (orig_declarator) 10844 decl = check_explicit_specialization (orig_declarator, decl, 10845 template_count, conceptp * 8); 10846 10847 return decl != error_mark_node ? decl : NULL_TREE; 10848 } 10849 10850 /* Create and return a canonical pointer to member function type, for 10851 TYPE, which is a POINTER_TYPE to a METHOD_TYPE. */ 10852 10853 tree 10854 build_ptrmemfunc_type (tree type) 10855 { 10856 tree field, fields; 10857 tree t; 10858 10859 if (type == error_mark_node) 10860 return type; 10861 10862 /* Make sure that we always have the unqualified pointer-to-member 10863 type first. */ 10864 if (cp_cv_quals quals = cp_type_quals (type)) 10865 { 10866 tree unqual = build_ptrmemfunc_type (TYPE_MAIN_VARIANT (type)); 10867 return cp_build_qualified_type (unqual, quals); 10868 } 10869 10870 /* If a canonical type already exists for this type, use it. We use 10871 this method instead of type_hash_canon, because it only does a 10872 simple equality check on the list of field members. */ 10873 10874 t = TYPE_PTRMEMFUNC_TYPE (type); 10875 if (t) 10876 return t; 10877 10878 t = make_node (RECORD_TYPE); 10879 10880 /* Let the front end know this is a pointer to member function. */ 10881 TYPE_PTRMEMFUNC_FLAG (t) = 1; 10882 10883 field = build_decl (input_location, FIELD_DECL, pfn_identifier, type); 10884 DECL_NONADDRESSABLE_P (field) = 1; 10885 fields = field; 10886 10887 field = build_decl (input_location, FIELD_DECL, delta_identifier, 10888 delta_type_node); 10889 DECL_NONADDRESSABLE_P (field) = 1; 10890 DECL_CHAIN (field) = fields; 10891 fields = field; 10892 10893 finish_builtin_struct (t, "__ptrmemfunc_type", fields, ptr_type_node); 10894 10895 /* Zap out the name so that the back end will give us the debugging 10896 information for this anonymous RECORD_TYPE. */ 10897 TYPE_NAME (t) = NULL_TREE; 10898 10899 /* Cache this pointer-to-member type so that we can find it again 10900 later. */ 10901 TYPE_PTRMEMFUNC_TYPE (type) = t; 10902 10903 if (TYPE_STRUCTURAL_EQUALITY_P (type)) 10904 SET_TYPE_STRUCTURAL_EQUALITY (t); 10905 else if (TYPE_CANONICAL (type) != type) 10906 TYPE_CANONICAL (t) = build_ptrmemfunc_type (TYPE_CANONICAL (type)); 10907 10908 return t; 10909 } 10910 10911 /* Create and return a pointer to data member type. */ 10912 10913 tree 10914 build_ptrmem_type (tree class_type, tree member_type) 10915 { 10916 if (TREE_CODE (member_type) == METHOD_TYPE) 10917 { 10918 cp_cv_quals quals = type_memfn_quals (member_type); 10919 cp_ref_qualifier rqual = type_memfn_rqual (member_type); 10920 member_type = build_memfn_type (member_type, class_type, quals, rqual); 10921 return build_ptrmemfunc_type (build_pointer_type (member_type)); 10922 } 10923 else 10924 { 10925 gcc_assert (TREE_CODE (member_type) != FUNCTION_TYPE); 10926 return build_offset_type (class_type, member_type); 10927 } 10928 } 10929 10930 /* DECL is a VAR_DECL defined in-class, whose TYPE is also given. 10931 Check to see that the definition is valid. Issue appropriate error 10932 messages. */ 10933 10934 static void 10935 check_static_variable_definition (tree decl, tree type) 10936 { 10937 /* Avoid redundant diagnostics on out-of-class definitions. */ 10938 if (!current_class_type || !TYPE_BEING_DEFINED (current_class_type)) 10939 ; 10940 /* Can't check yet if we don't know the type. */ 10941 else if (dependent_type_p (type)) 10942 ; 10943 /* If DECL is declared constexpr, we'll do the appropriate checks 10944 in check_initializer. Similarly for inline static data members. */ 10945 else if (DECL_P (decl) 10946 && (DECL_DECLARED_CONSTEXPR_P (decl) 10947 || DECL_VAR_DECLARED_INLINE_P (decl))) 10948 ; 10949 else if (cxx_dialect >= cxx11 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)) 10950 { 10951 if (!COMPLETE_TYPE_P (type)) 10952 error_at (DECL_SOURCE_LOCATION (decl), 10953 "in-class initialization of static data member %q#D of " 10954 "incomplete type", decl); 10955 else if (literal_type_p (type)) 10956 permerror (DECL_SOURCE_LOCATION (decl), 10957 "%<constexpr%> needed for in-class initialization of " 10958 "static data member %q#D of non-integral type", decl); 10959 else 10960 error_at (DECL_SOURCE_LOCATION (decl), 10961 "in-class initialization of static data member %q#D of " 10962 "non-literal type", decl); 10963 } 10964 /* Motion 10 at San Diego: If a static const integral data member is 10965 initialized with an integral constant expression, the initializer 10966 may appear either in the declaration (within the class), or in 10967 the definition, but not both. If it appears in the class, the 10968 member is a member constant. The file-scope definition is always 10969 required. */ 10970 else if (!ARITHMETIC_TYPE_P (type) && TREE_CODE (type) != ENUMERAL_TYPE) 10971 error_at (DECL_SOURCE_LOCATION (decl), 10972 "invalid in-class initialization of static data member " 10973 "of non-integral type %qT", 10974 type); 10975 else if (!CP_TYPE_CONST_P (type)) 10976 error_at (DECL_SOURCE_LOCATION (decl), 10977 "ISO C++ forbids in-class initialization of non-const " 10978 "static member %qD", 10979 decl); 10980 else if (!INTEGRAL_OR_ENUMERATION_TYPE_P (type)) 10981 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic, 10982 "ISO C++ forbids initialization of member constant " 10983 "%qD of non-integral type %qT", decl, type); 10984 } 10985 10986 /* *expr_p is part of the TYPE_SIZE of a variably-sized array. If any 10987 SAVE_EXPRs in *expr_p wrap expressions with side-effects, break those 10988 expressions out into temporary variables so that walk_tree doesn't 10989 step into them (c++/15764). */ 10990 10991 static tree 10992 stabilize_save_expr_r (tree *expr_p, int *walk_subtrees, void *data) 10993 { 10994 hash_set<tree> *pset = (hash_set<tree> *)data; 10995 tree expr = *expr_p; 10996 if (TREE_CODE (expr) == SAVE_EXPR) 10997 { 10998 tree op = TREE_OPERAND (expr, 0); 10999 cp_walk_tree (&op, stabilize_save_expr_r, data, pset); 11000 if (TREE_SIDE_EFFECTS (op)) 11001 TREE_OPERAND (expr, 0) = get_temp_regvar (TREE_TYPE (op), op); 11002 *walk_subtrees = 0; 11003 } 11004 else if (!EXPR_P (expr) || !TREE_SIDE_EFFECTS (expr)) 11005 *walk_subtrees = 0; 11006 return NULL; 11007 } 11008 11009 /* Entry point for the above. */ 11010 11011 static void 11012 stabilize_vla_size (tree size) 11013 { 11014 hash_set<tree> pset; 11015 /* Break out any function calls into temporary variables. */ 11016 cp_walk_tree (&size, stabilize_save_expr_r, &pset, &pset); 11017 } 11018 11019 /* Reduce a SIZEOF_EXPR to its value. */ 11020 11021 tree 11022 fold_sizeof_expr (tree t) 11023 { 11024 tree r; 11025 if (SIZEOF_EXPR_TYPE_P (t)) 11026 r = cxx_sizeof_or_alignof_type (EXPR_LOCATION (t), 11027 TREE_TYPE (TREE_OPERAND (t, 0)), 11028 SIZEOF_EXPR, false, false); 11029 else if (TYPE_P (TREE_OPERAND (t, 0))) 11030 r = cxx_sizeof_or_alignof_type (EXPR_LOCATION (t), 11031 TREE_OPERAND (t, 0), SIZEOF_EXPR, 11032 false, false); 11033 else 11034 r = cxx_sizeof_or_alignof_expr (EXPR_LOCATION (t), 11035 TREE_OPERAND (t, 0), SIZEOF_EXPR, 11036 false, false); 11037 if (r == error_mark_node) 11038 r = size_one_node; 11039 return r; 11040 } 11041 11042 /* Given the SIZE (i.e., number of elements) in an array, compute 11043 an appropriate index type for the array. If non-NULL, NAME is 11044 the name of the entity being declared. */ 11045 11046 static tree 11047 compute_array_index_type_loc (location_t name_loc, tree name, tree size, 11048 tsubst_flags_t complain) 11049 { 11050 if (error_operand_p (size)) 11051 return error_mark_node; 11052 11053 /* The type of the index being computed. */ 11054 tree itype; 11055 11056 /* The original numeric size as seen in the source code before 11057 conversion to size_t. */ 11058 tree origsize = size; 11059 11060 location_t loc = cp_expr_loc_or_loc (size, name ? name_loc : input_location); 11061 11062 if (!type_dependent_expression_p (size)) 11063 { 11064 origsize = size = mark_rvalue_use (size); 11065 11066 if (cxx_dialect < cxx11 && TREE_CODE (size) == NOP_EXPR 11067 && TREE_SIDE_EFFECTS (size)) 11068 /* In C++98, we mark a non-constant array bound with a magic 11069 NOP_EXPR with TREE_SIDE_EFFECTS; don't fold in that case. */; 11070 else 11071 { 11072 size = build_converted_constant_expr (size_type_node, size, complain); 11073 /* Pedantically a constant expression is required here and so 11074 __builtin_is_constant_evaluated () should fold to true if it 11075 is successfully folded into a constant. */ 11076 size = fold_non_dependent_expr (size, complain, 11077 /*manifestly_const_eval=*/true); 11078 11079 if (!TREE_CONSTANT (size)) 11080 size = origsize; 11081 } 11082 11083 if (error_operand_p (size)) 11084 return error_mark_node; 11085 11086 /* The array bound must be an integer type. */ 11087 tree type = TREE_TYPE (size); 11088 if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type)) 11089 { 11090 if (!(complain & tf_error)) 11091 return error_mark_node; 11092 if (name) 11093 error_at (loc, "size of array %qD has non-integral type %qT", 11094 name, type); 11095 else 11096 error_at (loc, "size of array has non-integral type %qT", type); 11097 size = integer_one_node; 11098 } 11099 } 11100 11101 /* A type is dependent if it is...an array type constructed from any 11102 dependent type or whose size is specified by a constant expression 11103 that is value-dependent. */ 11104 /* We can only call value_dependent_expression_p on integral constant 11105 expressions. */ 11106 if (processing_template_decl 11107 && potential_constant_expression (size) 11108 && value_dependent_expression_p (size)) 11109 { 11110 /* Just build the index type and mark that it requires 11111 structural equality checks. */ 11112 in_template: 11113 itype = build_index_type (build_min (MINUS_EXPR, sizetype, 11114 size, size_one_node)); 11115 TYPE_DEPENDENT_P (itype) = 1; 11116 TYPE_DEPENDENT_P_VALID (itype) = 1; 11117 SET_TYPE_STRUCTURAL_EQUALITY (itype); 11118 return itype; 11119 } 11120 11121 if (TREE_CODE (size) != INTEGER_CST) 11122 { 11123 tree folded = cp_fully_fold (size); 11124 if (TREE_CODE (folded) == INTEGER_CST) 11125 { 11126 if (name) 11127 pedwarn (loc, OPT_Wpedantic, "size of array %qD is not an " 11128 "integral constant-expression", name); 11129 else 11130 pedwarn (loc, OPT_Wpedantic, 11131 "size of array is not an integral constant-expression"); 11132 } 11133 if (TREE_CONSTANT (size) && !TREE_CONSTANT (folded)) 11134 /* We might have lost the TREE_CONSTANT flag e.g. when we are 11135 folding a conversion from a pointer to integral type. In that 11136 case issue an error below and don't treat this as a VLA. */; 11137 else 11138 /* Use the folded result for VLAs, too; it will have resolved 11139 SIZEOF_EXPR. */ 11140 size = folded; 11141 } 11142 11143 /* Normally, the array-bound will be a constant. */ 11144 if (TREE_CODE (size) == INTEGER_CST) 11145 { 11146 /* The size to use in diagnostics that reflects the constant 11147 size used in the source, rather than SIZE massaged above. */ 11148 tree diagsize = size; 11149 11150 /* If the original size before conversion to size_t was signed 11151 and negative, convert it to ssizetype to restore the sign. */ 11152 if (!TYPE_UNSIGNED (TREE_TYPE (origsize)) 11153 && TREE_CODE (size) == INTEGER_CST 11154 && tree_int_cst_sign_bit (size)) 11155 { 11156 diagsize = fold_convert (ssizetype, size); 11157 11158 /* Clear the overflow bit that may have been set as a result 11159 of the conversion from the sizetype of the new size to 11160 ssizetype. */ 11161 TREE_OVERFLOW (diagsize) = false; 11162 } 11163 11164 /* Verify that the array has a positive number of elements 11165 and issue the appropriate diagnostic if it doesn't. */ 11166 if (!valid_array_size_p (loc, diagsize, name, (complain & tf_error))) 11167 { 11168 if (!(complain & tf_error)) 11169 return error_mark_node; 11170 size = integer_one_node; 11171 } 11172 /* As an extension we allow zero-sized arrays. */ 11173 else if (integer_zerop (size)) 11174 { 11175 if (!(complain & tf_error)) 11176 /* We must fail if performing argument deduction (as 11177 indicated by the state of complain), so that 11178 another substitution can be found. */ 11179 return error_mark_node; 11180 else if (name) 11181 pedwarn (loc, OPT_Wpedantic, 11182 "ISO C++ forbids zero-size array %qD", name); 11183 else 11184 pedwarn (loc, OPT_Wpedantic, 11185 "ISO C++ forbids zero-size array"); 11186 } 11187 } 11188 else if (TREE_CONSTANT (size) 11189 /* We don't allow VLAs at non-function scopes, or during 11190 tentative template substitution. */ 11191 || !at_function_scope_p () 11192 || !(complain & tf_error)) 11193 { 11194 if (!(complain & tf_error)) 11195 return error_mark_node; 11196 /* `(int) &fn' is not a valid array bound. */ 11197 if (name) 11198 error_at (loc, 11199 "size of array %qD is not an integral constant-expression", 11200 name); 11201 else 11202 error_at (loc, "size of array is not an integral constant-expression"); 11203 size = integer_one_node; 11204 } 11205 else if (pedantic && warn_vla != 0) 11206 { 11207 if (name) 11208 pedwarn (name_loc, OPT_Wvla, 11209 "ISO C++ forbids variable length array %qD", name); 11210 else 11211 pedwarn (input_location, OPT_Wvla, 11212 "ISO C++ forbids variable length array"); 11213 } 11214 else if (warn_vla > 0) 11215 { 11216 if (name) 11217 warning_at (name_loc, OPT_Wvla, 11218 "variable length array %qD is used", name); 11219 else 11220 warning (OPT_Wvla, 11221 "variable length array is used"); 11222 } 11223 11224 if (processing_template_decl && !TREE_CONSTANT (size)) 11225 goto in_template; 11226 else 11227 { 11228 if (!TREE_CONSTANT (size)) 11229 { 11230 /* A variable sized array. Arrange for the SAVE_EXPR on the inside 11231 of the MINUS_EXPR, which allows the -1 to get folded with the +1 11232 that happens when building TYPE_SIZE. */ 11233 size = variable_size (size); 11234 stabilize_vla_size (size); 11235 } 11236 11237 /* Compute the index of the largest element in the array. It is 11238 one less than the number of elements in the array. We save 11239 and restore PROCESSING_TEMPLATE_DECL so that computations in 11240 cp_build_binary_op will be appropriately folded. */ 11241 { 11242 processing_template_decl_sentinel s; 11243 itype = cp_build_binary_op (input_location, 11244 MINUS_EXPR, 11245 cp_convert (ssizetype, size, complain), 11246 cp_convert (ssizetype, integer_one_node, 11247 complain), 11248 complain); 11249 itype = maybe_constant_value (itype, NULL_TREE, true); 11250 } 11251 11252 if (!TREE_CONSTANT (itype)) 11253 { 11254 if (sanitize_flags_p (SANITIZE_VLA) 11255 && current_function_decl != NULL_TREE) 11256 { 11257 /* We have to add 1 -- in the ubsan routine we generate 11258 LE_EXPR rather than LT_EXPR. */ 11259 tree t = fold_build2 (PLUS_EXPR, TREE_TYPE (itype), itype, 11260 build_one_cst (TREE_TYPE (itype))); 11261 t = ubsan_instrument_vla (input_location, t); 11262 finish_expr_stmt (t); 11263 } 11264 } 11265 /* Make sure that there was no overflow when creating to a signed 11266 index type. (For example, on a 32-bit machine, an array with 11267 size 2^32 - 1 is too big.) */ 11268 else if (TREE_CODE (itype) == INTEGER_CST 11269 && TREE_OVERFLOW (itype)) 11270 { 11271 if (!(complain & tf_error)) 11272 return error_mark_node; 11273 error ("overflow in array dimension"); 11274 TREE_OVERFLOW (itype) = 0; 11275 } 11276 } 11277 11278 /* Create and return the appropriate index type. */ 11279 itype = build_index_type (itype); 11280 11281 /* If the index type were dependent, we would have returned early, so 11282 remember that it isn't. */ 11283 TYPE_DEPENDENT_P (itype) = 0; 11284 TYPE_DEPENDENT_P_VALID (itype) = 1; 11285 return itype; 11286 } 11287 11288 tree 11289 compute_array_index_type (tree name, tree size, tsubst_flags_t complain) 11290 { 11291 return compute_array_index_type_loc (input_location, name, size, complain); 11292 } 11293 11294 /* Returns the scope (if any) in which the entity declared by 11295 DECLARATOR will be located. If the entity was declared with an 11296 unqualified name, NULL_TREE is returned. */ 11297 11298 tree 11299 get_scope_of_declarator (const cp_declarator *declarator) 11300 { 11301 while (declarator && declarator->kind != cdk_id) 11302 declarator = declarator->declarator; 11303 11304 /* If the declarator-id is a SCOPE_REF, the scope in which the 11305 declaration occurs is the first operand. */ 11306 if (declarator 11307 && declarator->u.id.qualifying_scope) 11308 return declarator->u.id.qualifying_scope; 11309 11310 /* Otherwise, the declarator is not a qualified name; the entity will 11311 be declared in the current scope. */ 11312 return NULL_TREE; 11313 } 11314 11315 /* Returns an ARRAY_TYPE for an array with SIZE elements of the 11316 indicated TYPE. If non-NULL, NAME is the NAME of the declaration 11317 with this type. */ 11318 11319 static tree 11320 create_array_type_for_decl (tree name, tree type, tree size, location_t loc) 11321 { 11322 tree itype = NULL_TREE; 11323 11324 /* If things have already gone awry, bail now. */ 11325 if (type == error_mark_node || size == error_mark_node) 11326 return error_mark_node; 11327 11328 /* [dcl.type.class.deduct] prohibits forming an array of placeholder 11329 for a deduced class type. */ 11330 if (template_placeholder_p (type)) 11331 { 11332 if (name) 11333 error_at (loc, "%qD declared as array of template placeholder " 11334 "type %qT", name, type); 11335 else 11336 error ("creating array of template placeholder type %qT", type); 11337 return error_mark_node; 11338 } 11339 11340 /* If there are some types which cannot be array elements, 11341 issue an error-message and return. */ 11342 switch (TREE_CODE (type)) 11343 { 11344 case VOID_TYPE: 11345 if (name) 11346 error_at (loc, "declaration of %qD as array of void", name); 11347 else 11348 error ("creating array of void"); 11349 return error_mark_node; 11350 11351 case FUNCTION_TYPE: 11352 if (name) 11353 error_at (loc, "declaration of %qD as array of functions", name); 11354 else 11355 error ("creating array of functions"); 11356 return error_mark_node; 11357 11358 case REFERENCE_TYPE: 11359 if (name) 11360 error_at (loc, "declaration of %qD as array of references", name); 11361 else 11362 error ("creating array of references"); 11363 return error_mark_node; 11364 11365 case METHOD_TYPE: 11366 if (name) 11367 error_at (loc, "declaration of %qD as array of function members", 11368 name); 11369 else 11370 error ("creating array of function members"); 11371 return error_mark_node; 11372 11373 default: 11374 break; 11375 } 11376 11377 if (!verify_type_context (name ? loc : input_location, 11378 TCTX_ARRAY_ELEMENT, type)) 11379 return error_mark_node; 11380 11381 /* [dcl.array] 11382 11383 The constant expressions that specify the bounds of the arrays 11384 can be omitted only for the first member of the sequence. */ 11385 if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)) 11386 { 11387 if (name) 11388 error_at (loc, "declaration of %qD as multidimensional array must " 11389 "have bounds for all dimensions except the first", 11390 name); 11391 else 11392 error ("multidimensional array must have bounds for all " 11393 "dimensions except the first"); 11394 11395 return error_mark_node; 11396 } 11397 11398 /* Figure out the index type for the array. */ 11399 if (size) 11400 { 11401 itype = compute_array_index_type_loc (loc, name, size, 11402 tf_warning_or_error); 11403 if (type_uses_auto (type) 11404 && variably_modified_type_p (itype, /*fn=*/NULL_TREE)) 11405 { 11406 sorry_at (loc, "variable-length array of %<auto%>"); 11407 return error_mark_node; 11408 } 11409 } 11410 11411 return build_cplus_array_type (type, itype); 11412 } 11413 11414 /* Returns the smallest location that is not UNKNOWN_LOCATION. */ 11415 11416 static location_t 11417 min_location (location_t loca, location_t locb) 11418 { 11419 if (loca == UNKNOWN_LOCATION 11420 || (locb != UNKNOWN_LOCATION 11421 && linemap_location_before_p (line_table, locb, loca))) 11422 return locb; 11423 return loca; 11424 } 11425 11426 /* Returns the smallest location != UNKNOWN_LOCATION among the 11427 three stored in LOCATIONS[ds_const], LOCATIONS[ds_volatile], 11428 and LOCATIONS[ds_restrict]. */ 11429 11430 static location_t 11431 smallest_type_quals_location (int type_quals, const location_t* locations) 11432 { 11433 location_t loc = UNKNOWN_LOCATION; 11434 11435 if (type_quals & TYPE_QUAL_CONST) 11436 loc = locations[ds_const]; 11437 11438 if (type_quals & TYPE_QUAL_VOLATILE) 11439 loc = min_location (loc, locations[ds_volatile]); 11440 11441 if (type_quals & TYPE_QUAL_RESTRICT) 11442 loc = min_location (loc, locations[ds_restrict]); 11443 11444 return loc; 11445 } 11446 11447 /* Returns the smallest among the latter and locations[ds_type_spec]. */ 11448 11449 static location_t 11450 smallest_type_location (int type_quals, const location_t* locations) 11451 { 11452 location_t loc = smallest_type_quals_location (type_quals, locations); 11453 return min_location (loc, locations[ds_type_spec]); 11454 } 11455 11456 static location_t 11457 smallest_type_location (const cp_decl_specifier_seq *declspecs) 11458 { 11459 int type_quals = get_type_quals (declspecs); 11460 return smallest_type_location (type_quals, declspecs->locations); 11461 } 11462 11463 /* Check that it's OK to declare a function with the indicated TYPE 11464 and TYPE_QUALS. SFK indicates the kind of special function (if any) 11465 that this function is. OPTYPE is the type given in a conversion 11466 operator declaration, or the class type for a constructor/destructor. 11467 Returns the actual return type of the function; that may be different 11468 than TYPE if an error occurs, or for certain special functions. */ 11469 11470 static tree 11471 check_special_function_return_type (special_function_kind sfk, 11472 tree type, 11473 tree optype, 11474 int type_quals, 11475 const location_t* locations) 11476 { 11477 switch (sfk) 11478 { 11479 case sfk_constructor: 11480 if (type) 11481 error_at (smallest_type_location (type_quals, locations), 11482 "return type specification for constructor invalid"); 11483 else if (type_quals != TYPE_UNQUALIFIED) 11484 error_at (smallest_type_quals_location (type_quals, locations), 11485 "qualifiers are not allowed on constructor declaration"); 11486 11487 if (targetm.cxx.cdtor_returns_this ()) 11488 type = build_pointer_type (optype); 11489 else 11490 type = void_type_node; 11491 break; 11492 11493 case sfk_destructor: 11494 if (type) 11495 error_at (smallest_type_location (type_quals, locations), 11496 "return type specification for destructor invalid"); 11497 else if (type_quals != TYPE_UNQUALIFIED) 11498 error_at (smallest_type_quals_location (type_quals, locations), 11499 "qualifiers are not allowed on destructor declaration"); 11500 11501 /* We can't use the proper return type here because we run into 11502 problems with ambiguous bases and covariant returns. */ 11503 if (targetm.cxx.cdtor_returns_this ()) 11504 type = build_pointer_type (void_type_node); 11505 else 11506 type = void_type_node; 11507 break; 11508 11509 case sfk_conversion: 11510 if (type) 11511 error_at (smallest_type_location (type_quals, locations), 11512 "return type specified for %<operator %T%>", optype); 11513 else if (type_quals != TYPE_UNQUALIFIED) 11514 error_at (smallest_type_quals_location (type_quals, locations), 11515 "qualifiers are not allowed on declaration of " 11516 "%<operator %T%>", optype); 11517 11518 type = optype; 11519 break; 11520 11521 case sfk_deduction_guide: 11522 if (type) 11523 error_at (smallest_type_location (type_quals, locations), 11524 "return type specified for deduction guide"); 11525 else if (type_quals != TYPE_UNQUALIFIED) 11526 error_at (smallest_type_quals_location (type_quals, locations), 11527 "qualifiers are not allowed on declaration of " 11528 "deduction guide"); 11529 if (TREE_CODE (optype) == TEMPLATE_TEMPLATE_PARM) 11530 { 11531 error ("template template parameter %qT in declaration of " 11532 "deduction guide", optype); 11533 type = error_mark_node; 11534 } 11535 else 11536 type = make_template_placeholder (CLASSTYPE_TI_TEMPLATE (optype)); 11537 for (int i = 0; i < ds_last; ++i) 11538 if (i != ds_explicit && locations[i]) 11539 error_at (locations[i], 11540 "%<decl-specifier%> in declaration of deduction guide"); 11541 break; 11542 11543 default: 11544 gcc_unreachable (); 11545 } 11546 11547 return type; 11548 } 11549 11550 /* A variable or data member (whose unqualified name is IDENTIFIER) 11551 has been declared with the indicated TYPE. If the TYPE is not 11552 acceptable, issue an error message and return a type to use for 11553 error-recovery purposes. */ 11554 11555 tree 11556 check_var_type (tree identifier, tree type, location_t loc) 11557 { 11558 if (VOID_TYPE_P (type)) 11559 { 11560 if (!identifier) 11561 error_at (loc, "unnamed variable or field declared void"); 11562 else if (identifier_p (identifier)) 11563 { 11564 gcc_assert (!IDENTIFIER_ANY_OP_P (identifier)); 11565 error_at (loc, "variable or field %qE declared void", 11566 identifier); 11567 } 11568 else 11569 error_at (loc, "variable or field declared void"); 11570 type = error_mark_node; 11571 } 11572 11573 return type; 11574 } 11575 11576 /* Handle declaring DECL as an inline variable. */ 11577 11578 static void 11579 mark_inline_variable (tree decl, location_t loc) 11580 { 11581 bool inlinep = true; 11582 if (! toplevel_bindings_p ()) 11583 { 11584 error_at (loc, "%<inline%> specifier invalid for variable " 11585 "%qD declared at block scope", decl); 11586 inlinep = false; 11587 } 11588 else if (cxx_dialect < cxx17) 11589 pedwarn (loc, OPT_Wc__17_extensions, "inline variables are only available " 11590 "with %<-std=c++17%> or %<-std=gnu++17%>"); 11591 if (inlinep) 11592 { 11593 retrofit_lang_decl (decl); 11594 SET_DECL_VAR_DECLARED_INLINE_P (decl); 11595 } 11596 } 11597 11598 11599 /* Assign a typedef-given name to a class or enumeration type declared 11600 as anonymous at first. This was split out of grokdeclarator 11601 because it is also used in libcc1. */ 11602 11603 void 11604 name_unnamed_type (tree type, tree decl) 11605 { 11606 gcc_assert (TYPE_UNNAMED_P (type)); 11607 11608 /* Replace the anonymous decl with the real decl. Be careful not to 11609 rename other typedefs (such as the self-reference) of type. */ 11610 tree orig = TYPE_NAME (type); 11611 for (tree t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t)) 11612 if (TYPE_NAME (t) == orig) 11613 TYPE_NAME (t) = decl; 11614 11615 /* If this is a typedef within a template class, the nested 11616 type is a (non-primary) template. The name for the 11617 template needs updating as well. */ 11618 if (TYPE_LANG_SPECIFIC (type) && CLASSTYPE_TEMPLATE_INFO (type)) 11619 DECL_NAME (CLASSTYPE_TI_TEMPLATE (type)) = DECL_NAME (decl); 11620 11621 /* Adjust linkage now that we aren't unnamed anymore. */ 11622 reset_type_linkage (type); 11623 11624 /* FIXME remangle member functions; member functions of a 11625 type with external linkage have external linkage. */ 11626 11627 /* Check that our job is done, and that it would fail if we 11628 attempted to do it again. */ 11629 gcc_assert (!TYPE_UNNAMED_P (type)); 11630 } 11631 11632 /* Check that decltype(auto) was well-formed: only plain decltype(auto) 11633 is allowed. TYPE might contain a decltype(auto). Returns true if 11634 there was a problem, false otherwise. */ 11635 11636 static bool 11637 check_decltype_auto (location_t loc, tree type) 11638 { 11639 if (tree a = type_uses_auto (type)) 11640 { 11641 if (AUTO_IS_DECLTYPE (a)) 11642 { 11643 if (a != type) 11644 { 11645 error_at (loc, "%qT as type rather than plain " 11646 "%<decltype(auto)%>", type); 11647 return true; 11648 } 11649 else if (TYPE_QUALS (type) != TYPE_UNQUALIFIED) 11650 { 11651 error_at (loc, "%<decltype(auto)%> cannot be cv-qualified"); 11652 return true; 11653 } 11654 } 11655 } 11656 return false; 11657 } 11658 11659 /* Given declspecs and a declarator (abstract or otherwise), determine 11660 the name and type of the object declared and construct a DECL node 11661 for it. 11662 11663 DECLSPECS points to the representation of declaration-specifier 11664 sequence that precedes declarator. 11665 11666 DECL_CONTEXT says which syntactic context this declaration is in: 11667 NORMAL for most contexts. Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL. 11668 FUNCDEF for a function definition. Like NORMAL but a few different 11669 error messages in each case. Return value may be zero meaning 11670 this definition is too screwy to try to parse. 11671 MEMFUNCDEF for a function definition. Like FUNCDEF but prepares to 11672 handle member functions (which have FIELD context). 11673 Return value may be zero meaning this definition is too screwy to 11674 try to parse. 11675 PARM for a parameter declaration (either within a function prototype 11676 or before a function body). Make a PARM_DECL, or return void_type_node. 11677 TPARM for a template parameter declaration. 11678 CATCHPARM for a parameter declaration before a catch clause. 11679 TYPENAME if for a typename (in a cast or sizeof). 11680 Don't make a DECL node; just return the ..._TYPE node. 11681 FIELD for a struct or union field; make a FIELD_DECL. 11682 BITFIELD for a field with specified width. 11683 11684 INITIALIZED is as for start_decl. 11685 11686 ATTRLIST is a pointer to the list of attributes, which may be NULL 11687 if there are none; *ATTRLIST may be modified if attributes from inside 11688 the declarator should be applied to the declaration. 11689 11690 When this function is called, scoping variables (such as 11691 CURRENT_CLASS_TYPE) should reflect the scope in which the 11692 declaration occurs, not the scope in which the new declaration will 11693 be placed. For example, on: 11694 11695 void S::f() { ... } 11696 11697 when grokdeclarator is called for `S::f', the CURRENT_CLASS_TYPE 11698 should not be `S'. 11699 11700 Returns a DECL (if a declarator is present), a TYPE (if there is no 11701 declarator, in cases like "struct S;"), or the ERROR_MARK_NODE if an 11702 error occurs. */ 11703 11704 tree 11705 grokdeclarator (const cp_declarator *declarator, 11706 cp_decl_specifier_seq *declspecs, 11707 enum decl_context decl_context, 11708 int initialized, 11709 tree* attrlist) 11710 { 11711 tree type = NULL_TREE; 11712 int longlong = 0; 11713 int explicit_intN = 0; 11714 int int_n_alt = 0; 11715 int virtualp, explicitp, friendp, inlinep, staticp; 11716 int explicit_int = 0; 11717 int explicit_char = 0; 11718 int defaulted_int = 0; 11719 11720 tree typedef_decl = NULL_TREE; 11721 const char *name = NULL; 11722 tree typedef_type = NULL_TREE; 11723 /* True if this declarator is a function definition. */ 11724 bool funcdef_flag = false; 11725 cp_declarator_kind innermost_code = cdk_error; 11726 int bitfield = 0; 11727 #if 0 11728 /* See the code below that used this. */ 11729 tree decl_attr = NULL_TREE; 11730 #endif 11731 11732 /* Keep track of what sort of function is being processed 11733 so that we can warn about default return values, or explicit 11734 return values which do not match prescribed defaults. */ 11735 special_function_kind sfk = sfk_none; 11736 11737 tree dname = NULL_TREE; 11738 tree ctor_return_type = NULL_TREE; 11739 enum overload_flags flags = NO_SPECIAL; 11740 /* cv-qualifiers that apply to the declarator, for a declaration of 11741 a member function. */ 11742 cp_cv_quals memfn_quals = TYPE_UNQUALIFIED; 11743 /* virt-specifiers that apply to the declarator, for a declaration of 11744 a member function. */ 11745 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED; 11746 /* ref-qualifier that applies to the declarator, for a declaration of 11747 a member function. */ 11748 cp_ref_qualifier rqual = REF_QUAL_NONE; 11749 /* cv-qualifiers that apply to the type specified by the DECLSPECS. */ 11750 int type_quals = get_type_quals (declspecs); 11751 tree raises = NULL_TREE; 11752 int template_count = 0; 11753 tree returned_attrs = NULL_TREE; 11754 tree parms = NULL_TREE; 11755 const cp_declarator *id_declarator; 11756 /* The unqualified name of the declarator; either an 11757 IDENTIFIER_NODE, BIT_NOT_EXPR, or TEMPLATE_ID_EXPR. */ 11758 tree unqualified_id; 11759 /* The class type, if any, in which this entity is located, 11760 or NULL_TREE if none. Note that this value may be different from 11761 the current class type; for example if an attempt is made to declare 11762 "A::f" inside "B", this value will be "A". */ 11763 tree ctype = current_class_type; 11764 /* The NAMESPACE_DECL for the namespace in which this entity is 11765 located. If an unqualified name is used to declare the entity, 11766 this value will be NULL_TREE, even if the entity is located at 11767 namespace scope. */ 11768 tree in_namespace = NULL_TREE; 11769 cp_storage_class storage_class; 11770 bool unsigned_p, signed_p, short_p, long_p, thread_p; 11771 bool type_was_error_mark_node = false; 11772 bool parameter_pack_p = declarator ? declarator->parameter_pack_p : false; 11773 bool template_type_arg = false; 11774 bool template_parm_flag = false; 11775 bool typedef_p = decl_spec_seq_has_spec_p (declspecs, ds_typedef); 11776 bool constexpr_p = decl_spec_seq_has_spec_p (declspecs, ds_constexpr); 11777 bool constinit_p = decl_spec_seq_has_spec_p (declspecs, ds_constinit); 11778 bool consteval_p = decl_spec_seq_has_spec_p (declspecs, ds_consteval); 11779 bool late_return_type_p = false; 11780 bool array_parameter_p = false; 11781 tree reqs = NULL_TREE; 11782 11783 signed_p = decl_spec_seq_has_spec_p (declspecs, ds_signed); 11784 unsigned_p = decl_spec_seq_has_spec_p (declspecs, ds_unsigned); 11785 short_p = decl_spec_seq_has_spec_p (declspecs, ds_short); 11786 long_p = decl_spec_seq_has_spec_p (declspecs, ds_long); 11787 longlong = decl_spec_seq_has_spec_p (declspecs, ds_long_long); 11788 explicit_intN = declspecs->explicit_intN_p; 11789 int_n_alt = declspecs->int_n_alt; 11790 thread_p = decl_spec_seq_has_spec_p (declspecs, ds_thread); 11791 11792 // Was concept_p specified? Note that ds_concept 11793 // implies ds_constexpr! 11794 bool concept_p = decl_spec_seq_has_spec_p (declspecs, ds_concept); 11795 if (concept_p) 11796 constexpr_p = true; 11797 11798 if (decl_context == FUNCDEF) 11799 funcdef_flag = true, decl_context = NORMAL; 11800 else if (decl_context == MEMFUNCDEF) 11801 funcdef_flag = true, decl_context = FIELD; 11802 else if (decl_context == BITFIELD) 11803 bitfield = 1, decl_context = FIELD; 11804 else if (decl_context == TEMPLATE_TYPE_ARG) 11805 template_type_arg = true, decl_context = TYPENAME; 11806 else if (decl_context == TPARM) 11807 template_parm_flag = true, decl_context = PARM; 11808 11809 if (initialized == SD_DEFAULTED || initialized == SD_DELETED) 11810 funcdef_flag = true; 11811 11812 location_t typespec_loc = loc_or_input_loc (smallest_type_location 11813 (type_quals, 11814 declspecs->locations)); 11815 location_t id_loc; 11816 location_t init_loc; 11817 if (declarator) 11818 { 11819 id_loc = loc_or_input_loc (declarator->id_loc); 11820 init_loc = loc_or_input_loc (declarator->init_loc); 11821 } 11822 else 11823 init_loc = id_loc = input_location; 11824 11825 /* Look inside a declarator for the name being declared 11826 and get it as a string, for an error message. */ 11827 for (id_declarator = declarator; 11828 id_declarator; 11829 id_declarator = id_declarator->declarator) 11830 { 11831 if (id_declarator->kind != cdk_id) 11832 innermost_code = id_declarator->kind; 11833 11834 switch (id_declarator->kind) 11835 { 11836 case cdk_function: 11837 if (id_declarator->declarator 11838 && id_declarator->declarator->kind == cdk_id) 11839 { 11840 sfk = id_declarator->declarator->u.id.sfk; 11841 if (sfk == sfk_destructor) 11842 flags = DTOR_FLAG; 11843 } 11844 break; 11845 11846 case cdk_id: 11847 { 11848 tree qualifying_scope = id_declarator->u.id.qualifying_scope; 11849 tree decl = id_declarator->u.id.unqualified_name; 11850 if (!decl) 11851 break; 11852 if (qualifying_scope) 11853 { 11854 if (check_for_bare_parameter_packs (qualifying_scope, 11855 id_declarator->id_loc)) 11856 return error_mark_node; 11857 if (at_function_scope_p ()) 11858 { 11859 /* [dcl.meaning] 11860 11861 A declarator-id shall not be qualified except 11862 for ... 11863 11864 None of the cases are permitted in block 11865 scope. */ 11866 if (qualifying_scope == global_namespace) 11867 error ("invalid use of qualified-name %<::%D%>", 11868 decl); 11869 else if (TYPE_P (qualifying_scope)) 11870 error ("invalid use of qualified-name %<%T::%D%>", 11871 qualifying_scope, decl); 11872 else 11873 error ("invalid use of qualified-name %<%D::%D%>", 11874 qualifying_scope, decl); 11875 return error_mark_node; 11876 } 11877 else if (TYPE_P (qualifying_scope)) 11878 { 11879 ctype = qualifying_scope; 11880 if (!MAYBE_CLASS_TYPE_P (ctype)) 11881 { 11882 error_at (id_declarator->id_loc, 11883 "%q#T is not a class or namespace", ctype); 11884 ctype = NULL_TREE; 11885 } 11886 else if (innermost_code != cdk_function 11887 && current_class_type 11888 && !uniquely_derived_from_p (ctype, 11889 current_class_type)) 11890 { 11891 error_at (id_declarator->id_loc, 11892 "invalid use of qualified-name %<%T::%D%>", 11893 qualifying_scope, decl); 11894 return error_mark_node; 11895 } 11896 } 11897 else if (TREE_CODE (qualifying_scope) == NAMESPACE_DECL) 11898 in_namespace = qualifying_scope; 11899 } 11900 switch (TREE_CODE (decl)) 11901 { 11902 case BIT_NOT_EXPR: 11903 { 11904 if (innermost_code != cdk_function) 11905 { 11906 error_at (EXPR_LOCATION (decl), 11907 "declaration of %qE as non-function", decl); 11908 return error_mark_node; 11909 } 11910 else if (!qualifying_scope 11911 && !(current_class_type && at_class_scope_p ())) 11912 { 11913 error_at (EXPR_LOCATION (decl), 11914 "declaration of %qE as non-member", decl); 11915 return error_mark_node; 11916 } 11917 11918 tree type = TREE_OPERAND (decl, 0); 11919 if (TYPE_P (type)) 11920 type = constructor_name (type); 11921 name = identifier_to_locale (IDENTIFIER_POINTER (type)); 11922 dname = decl; 11923 } 11924 break; 11925 11926 case TEMPLATE_ID_EXPR: 11927 { 11928 tree fns = TREE_OPERAND (decl, 0); 11929 11930 dname = fns; 11931 if (!identifier_p (dname)) 11932 dname = OVL_NAME (dname); 11933 } 11934 /* Fall through. */ 11935 11936 case IDENTIFIER_NODE: 11937 if (identifier_p (decl)) 11938 dname = decl; 11939 11940 if (IDENTIFIER_KEYWORD_P (dname)) 11941 { 11942 error ("declarator-id missing; using reserved word %qD", 11943 dname); 11944 name = identifier_to_locale (IDENTIFIER_POINTER (dname)); 11945 } 11946 else if (!IDENTIFIER_CONV_OP_P (dname)) 11947 name = identifier_to_locale (IDENTIFIER_POINTER (dname)); 11948 else 11949 { 11950 gcc_assert (flags == NO_SPECIAL); 11951 flags = TYPENAME_FLAG; 11952 sfk = sfk_conversion; 11953 tree glob = get_global_binding (dname); 11954 if (glob && TREE_CODE (glob) == TYPE_DECL) 11955 name = identifier_to_locale (IDENTIFIER_POINTER (dname)); 11956 else 11957 name = "<invalid operator>"; 11958 } 11959 break; 11960 11961 default: 11962 gcc_unreachable (); 11963 } 11964 break; 11965 } 11966 11967 case cdk_array: 11968 case cdk_pointer: 11969 case cdk_reference: 11970 case cdk_ptrmem: 11971 break; 11972 11973 case cdk_decomp: 11974 name = "structured binding"; 11975 break; 11976 11977 case cdk_error: 11978 return error_mark_node; 11979 11980 default: 11981 gcc_unreachable (); 11982 } 11983 if (id_declarator->kind == cdk_id) 11984 break; 11985 } 11986 11987 /* [dcl.fct.edf] 11988 11989 The declarator in a function-definition shall have the form 11990 D1 ( parameter-declaration-clause) ... */ 11991 if (funcdef_flag && innermost_code != cdk_function) 11992 { 11993 error_at (id_loc, "function definition does not declare parameters"); 11994 return error_mark_node; 11995 } 11996 11997 if (flags == TYPENAME_FLAG 11998 && innermost_code != cdk_function 11999 && ! (ctype && !declspecs->any_specifiers_p)) 12000 { 12001 error_at (id_loc, "declaration of %qD as non-function", dname); 12002 return error_mark_node; 12003 } 12004 12005 if (dname && identifier_p (dname)) 12006 { 12007 if (UDLIT_OPER_P (dname) 12008 && innermost_code != cdk_function) 12009 { 12010 error_at (id_loc, "declaration of %qD as non-function", dname); 12011 return error_mark_node; 12012 } 12013 12014 if (IDENTIFIER_ANY_OP_P (dname)) 12015 { 12016 if (typedef_p) 12017 { 12018 error_at (id_loc, "declaration of %qD as %<typedef%>", dname); 12019 return error_mark_node; 12020 } 12021 else if (decl_context == PARM || decl_context == CATCHPARM) 12022 { 12023 error_at (id_loc, "declaration of %qD as parameter", dname); 12024 return error_mark_node; 12025 } 12026 } 12027 } 12028 12029 /* Anything declared one level down from the top level 12030 must be one of the parameters of a function 12031 (because the body is at least two levels down). */ 12032 12033 /* This heuristic cannot be applied to C++ nodes! Fixed, however, 12034 by not allowing C++ class definitions to specify their parameters 12035 with xdecls (must be spec.d in the parmlist). 12036 12037 Since we now wait to push a class scope until we are sure that 12038 we are in a legitimate method context, we must set oldcname 12039 explicitly (since current_class_name is not yet alive). 12040 12041 We also want to avoid calling this a PARM if it is in a namespace. */ 12042 12043 if (decl_context == NORMAL && !toplevel_bindings_p ()) 12044 { 12045 cp_binding_level *b = current_binding_level; 12046 current_binding_level = b->level_chain; 12047 if (current_binding_level != 0 && toplevel_bindings_p ()) 12048 decl_context = PARM; 12049 current_binding_level = b; 12050 } 12051 12052 if (name == NULL) 12053 name = decl_context == PARM ? "parameter" : "type name"; 12054 12055 if (consteval_p && constexpr_p) 12056 { 12057 error_at (declspecs->locations[ds_consteval], 12058 "both %qs and %qs specified", "constexpr", "consteval"); 12059 return error_mark_node; 12060 } 12061 12062 if (concept_p && typedef_p) 12063 { 12064 error_at (declspecs->locations[ds_concept], 12065 "%qs cannot appear in a typedef declaration", "concept"); 12066 return error_mark_node; 12067 } 12068 12069 if (constexpr_p && typedef_p) 12070 { 12071 error_at (declspecs->locations[ds_constexpr], 12072 "%qs cannot appear in a typedef declaration", "constexpr"); 12073 return error_mark_node; 12074 } 12075 12076 if (consteval_p && typedef_p) 12077 { 12078 error_at (declspecs->locations[ds_consteval], 12079 "%qs cannot appear in a typedef declaration", "consteval"); 12080 return error_mark_node; 12081 } 12082 12083 if (constinit_p && typedef_p) 12084 { 12085 error_at (declspecs->locations[ds_constinit], 12086 "%qs cannot appear in a typedef declaration", "constinit"); 12087 return error_mark_node; 12088 } 12089 12090 /* [dcl.spec]/2 "At most one of the constexpr, consteval, and constinit 12091 keywords shall appear in a decl-specifier-seq." */ 12092 if (constinit_p && constexpr_p) 12093 { 12094 gcc_rich_location richloc (declspecs->locations[ds_constinit]); 12095 richloc.add_range (declspecs->locations[ds_constexpr]); 12096 error_at (&richloc, 12097 "can use at most one of the %<constinit%> and %<constexpr%> " 12098 "specifiers"); 12099 return error_mark_node; 12100 } 12101 12102 /* If there were multiple types specified in the decl-specifier-seq, 12103 issue an error message. */ 12104 if (declspecs->multiple_types_p) 12105 { 12106 error_at (typespec_loc, 12107 "two or more data types in declaration of %qs", name); 12108 return error_mark_node; 12109 } 12110 12111 if (declspecs->conflicting_specifiers_p) 12112 { 12113 error_at (min_location (declspecs->locations[ds_typedef], 12114 declspecs->locations[ds_storage_class]), 12115 "conflicting specifiers in declaration of %qs", name); 12116 return error_mark_node; 12117 } 12118 12119 /* Extract the basic type from the decl-specifier-seq. */ 12120 type = declspecs->type; 12121 if (type == error_mark_node) 12122 { 12123 type = NULL_TREE; 12124 type_was_error_mark_node = true; 12125 } 12126 12127 /* Ignore erroneous attributes. */ 12128 if (attrlist && *attrlist == error_mark_node) 12129 *attrlist = NULL_TREE; 12130 12131 /* An object declared as __attribute__((unavailable)) suppresses 12132 any reports of being declared with unavailable or deprecated 12133 items. An object declared as __attribute__((deprecated)) 12134 suppresses warnings of uses of other deprecated items. */ 12135 auto ds = make_temp_override (deprecated_state); 12136 if (attrlist && lookup_attribute ("unavailable", *attrlist)) 12137 deprecated_state = UNAVAILABLE_DEPRECATED_SUPPRESS; 12138 else if (attrlist && lookup_attribute ("deprecated", *attrlist)) 12139 deprecated_state = DEPRECATED_SUPPRESS; 12140 12141 cp_handle_deprecated_or_unavailable (type); 12142 if (type && TREE_CODE (type) == TYPE_DECL) 12143 { 12144 cp_warn_deprecated_use_scopes (CP_DECL_CONTEXT (type)); 12145 typedef_decl = type; 12146 type = TREE_TYPE (typedef_decl); 12147 if (DECL_ARTIFICIAL (typedef_decl)) 12148 cp_handle_deprecated_or_unavailable (type); 12149 } 12150 /* No type at all: default to `int', and set DEFAULTED_INT 12151 because it was not a user-defined typedef. */ 12152 if (type == NULL_TREE) 12153 { 12154 if (signed_p || unsigned_p || long_p || short_p) 12155 { 12156 /* These imply 'int'. */ 12157 type = integer_type_node; 12158 defaulted_int = 1; 12159 } 12160 /* If we just have "complex", it is equivalent to "complex double". */ 12161 else if (!longlong && !explicit_intN 12162 && decl_spec_seq_has_spec_p (declspecs, ds_complex)) 12163 { 12164 type = double_type_node; 12165 pedwarn (declspecs->locations[ds_complex], OPT_Wpedantic, 12166 "ISO C++ does not support plain %<complex%> meaning " 12167 "%<double complex%>"); 12168 } 12169 } 12170 /* Gather flags. */ 12171 explicit_int = declspecs->explicit_int_p; 12172 explicit_char = declspecs->explicit_char_p; 12173 12174 #if 0 12175 /* See the code below that used this. */ 12176 if (typedef_decl) 12177 decl_attr = DECL_ATTRIBUTES (typedef_decl); 12178 #endif 12179 typedef_type = type; 12180 12181 if (sfk == sfk_conversion || sfk == sfk_deduction_guide) 12182 ctor_return_type = TREE_TYPE (dname); 12183 else 12184 ctor_return_type = ctype; 12185 12186 if (sfk != sfk_none) 12187 { 12188 type = check_special_function_return_type (sfk, type, 12189 ctor_return_type, 12190 type_quals, 12191 declspecs->locations); 12192 type_quals = TYPE_UNQUALIFIED; 12193 } 12194 else if (type == NULL_TREE) 12195 { 12196 int is_main; 12197 12198 explicit_int = -1; 12199 12200 /* We handle `main' specially here, because 'main () { }' is so 12201 common. With no options, it is allowed. With -Wreturn-type, 12202 it is a warning. It is only an error with -pedantic-errors. */ 12203 is_main = (funcdef_flag 12204 && dname && identifier_p (dname) 12205 && MAIN_NAME_P (dname) 12206 && ctype == NULL_TREE 12207 && in_namespace == NULL_TREE 12208 && current_namespace == global_namespace); 12209 12210 if (type_was_error_mark_node) 12211 /* We've already issued an error, don't complain more. */; 12212 else if (in_system_header_at (id_loc) || flag_ms_extensions) 12213 /* Allow it, sigh. */; 12214 else if (! is_main) 12215 permerror (id_loc, "ISO C++ forbids declaration of %qs with no type", 12216 name); 12217 else if (pedantic) 12218 pedwarn (id_loc, OPT_Wpedantic, 12219 "ISO C++ forbids declaration of %qs with no type", name); 12220 else 12221 warning_at (id_loc, OPT_Wreturn_type, 12222 "ISO C++ forbids declaration of %qs with no type", name); 12223 12224 if (type_was_error_mark_node && template_parm_flag) 12225 /* FIXME we should be able to propagate the error_mark_node as is 12226 for other contexts too. */ 12227 type = error_mark_node; 12228 else 12229 type = integer_type_node; 12230 } 12231 12232 ctype = NULL_TREE; 12233 12234 if (explicit_intN) 12235 { 12236 if (! int_n_enabled_p[declspecs->int_n_idx]) 12237 { 12238 error_at (declspecs->locations[ds_type_spec], 12239 "%<__int%d%> is not supported by this target", 12240 int_n_data[declspecs->int_n_idx].bitsize); 12241 explicit_intN = false; 12242 } 12243 /* Don't pedwarn if the alternate "__intN__" form has been used instead 12244 of "__intN". */ 12245 else if (!int_n_alt && pedantic) 12246 pedwarn (declspecs->locations[ds_type_spec], OPT_Wpedantic, 12247 "ISO C++ does not support %<__int%d%> for %qs", 12248 int_n_data[declspecs->int_n_idx].bitsize, name); 12249 } 12250 12251 /* Now process the modifiers that were specified 12252 and check for invalid combinations. */ 12253 12254 /* Long double is a special combination. */ 12255 if (long_p && !longlong && TYPE_MAIN_VARIANT (type) == double_type_node) 12256 { 12257 long_p = false; 12258 type = cp_build_qualified_type (long_double_type_node, 12259 cp_type_quals (type)); 12260 } 12261 12262 /* Check all other uses of type modifiers. */ 12263 12264 if (unsigned_p || signed_p || long_p || short_p) 12265 { 12266 location_t loc; 12267 const char *key; 12268 if (unsigned_p) 12269 { 12270 key = "unsigned"; 12271 loc = declspecs->locations[ds_unsigned]; 12272 } 12273 else if (signed_p) 12274 { 12275 key = "signed"; 12276 loc = declspecs->locations[ds_signed]; 12277 } 12278 else if (longlong) 12279 { 12280 key = "long long"; 12281 loc = declspecs->locations[ds_long_long]; 12282 } 12283 else if (long_p) 12284 { 12285 key = "long"; 12286 loc = declspecs->locations[ds_long]; 12287 } 12288 else /* if (short_p) */ 12289 { 12290 key = "short"; 12291 loc = declspecs->locations[ds_short]; 12292 } 12293 12294 int ok = 0; 12295 12296 if (signed_p && unsigned_p) 12297 { 12298 gcc_rich_location richloc (declspecs->locations[ds_signed]); 12299 richloc.add_range (declspecs->locations[ds_unsigned]); 12300 error_at (&richloc, 12301 "%<signed%> and %<unsigned%> specified together"); 12302 } 12303 else if (long_p && short_p) 12304 { 12305 gcc_rich_location richloc (declspecs->locations[ds_long]); 12306 richloc.add_range (declspecs->locations[ds_short]); 12307 error_at (&richloc, "%<long%> and %<short%> specified together"); 12308 } 12309 else if (TREE_CODE (type) != INTEGER_TYPE 12310 || type == char8_type_node 12311 || type == char16_type_node 12312 || type == char32_type_node 12313 || ((long_p || short_p) 12314 && (explicit_char || explicit_intN))) 12315 error_at (loc, "%qs specified with %qT", key, type); 12316 else if (!explicit_int && !defaulted_int 12317 && !explicit_char && !explicit_intN) 12318 { 12319 if (typedef_decl) 12320 { 12321 pedwarn (loc, OPT_Wpedantic, 12322 "%qs specified with typedef-name %qD", 12323 key, typedef_decl); 12324 ok = !flag_pedantic_errors; 12325 /* PR108099: __int128_t comes from c_common_nodes_and_builtins, 12326 and is not built as a typedef. */ 12327 if (is_typedef_decl (typedef_decl)) 12328 type = DECL_ORIGINAL_TYPE (typedef_decl); 12329 } 12330 else if (declspecs->decltype_p) 12331 error_at (loc, "%qs specified with %<decltype%>", key); 12332 else 12333 error_at (loc, "%qs specified with %<typeof%>", key); 12334 } 12335 else 12336 ok = 1; 12337 12338 /* Discard the type modifiers if they are invalid. */ 12339 if (! ok) 12340 { 12341 unsigned_p = false; 12342 signed_p = false; 12343 long_p = false; 12344 short_p = false; 12345 longlong = 0; 12346 } 12347 } 12348 12349 /* Decide whether an integer type is signed or not. 12350 Optionally treat bitfields as signed by default. */ 12351 if (unsigned_p 12352 /* [class.bit] 12353 12354 It is implementation-defined whether a plain (neither 12355 explicitly signed or unsigned) char, short, int, or long 12356 bit-field is signed or unsigned. 12357 12358 Naturally, we extend this to long long as well. Note that 12359 this does not include wchar_t. */ 12360 || (bitfield && !flag_signed_bitfields 12361 && !signed_p 12362 /* A typedef for plain `int' without `signed' can be 12363 controlled just like plain `int', but a typedef for 12364 `signed int' cannot be so controlled. */ 12365 && !(typedef_decl 12366 && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)) 12367 && TREE_CODE (type) == INTEGER_TYPE 12368 && !same_type_p (TYPE_MAIN_VARIANT (type), wchar_type_node))) 12369 { 12370 if (explicit_intN) 12371 type = int_n_trees[declspecs->int_n_idx].unsigned_type; 12372 else if (longlong) 12373 type = long_long_unsigned_type_node; 12374 else if (long_p) 12375 type = long_unsigned_type_node; 12376 else if (short_p) 12377 type = short_unsigned_type_node; 12378 else if (type == char_type_node) 12379 type = unsigned_char_type_node; 12380 else if (typedef_decl) 12381 type = c_common_unsigned_type (type); 12382 else 12383 type = unsigned_type_node; 12384 } 12385 else if (signed_p && type == char_type_node) 12386 type = signed_char_type_node; 12387 else if (explicit_intN) 12388 type = int_n_trees[declspecs->int_n_idx].signed_type; 12389 else if (longlong) 12390 type = long_long_integer_type_node; 12391 else if (long_p) 12392 type = long_integer_type_node; 12393 else if (short_p) 12394 type = short_integer_type_node; 12395 else if (signed_p && typedef_decl) 12396 type = c_common_signed_type (type); 12397 12398 if (decl_spec_seq_has_spec_p (declspecs, ds_complex)) 12399 { 12400 if (TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE) 12401 error_at (declspecs->locations[ds_complex], 12402 "complex invalid for %qs", name); 12403 /* If a modifier is specified, the resulting complex is the complex 12404 form of TYPE. E.g, "complex short" is "complex short int". */ 12405 else if (type == integer_type_node) 12406 type = complex_integer_type_node; 12407 else if (type == float_type_node) 12408 type = complex_float_type_node; 12409 else if (type == double_type_node) 12410 type = complex_double_type_node; 12411 else if (type == long_double_type_node) 12412 type = complex_long_double_type_node; 12413 else 12414 type = build_complex_type (type); 12415 } 12416 12417 /* If we're using the injected-class-name to form a compound type or a 12418 declaration, replace it with the underlying class so we don't get 12419 redundant typedefs in the debug output. But if we are returning the 12420 type unchanged, leave it alone so that it's available to 12421 maybe_get_template_decl_from_type_decl. */ 12422 if (CLASS_TYPE_P (type) 12423 && DECL_SELF_REFERENCE_P (TYPE_NAME (type)) 12424 && type == TREE_TYPE (TYPE_NAME (type)) 12425 && (declarator || type_quals)) 12426 type = DECL_ORIGINAL_TYPE (TYPE_NAME (type)); 12427 12428 type_quals |= cp_type_quals (type); 12429 type = cp_build_qualified_type_real 12430 (type, type_quals, ((((typedef_decl && !DECL_ARTIFICIAL (typedef_decl)) 12431 || declspecs->decltype_p) 12432 ? tf_ignore_bad_quals : 0) | tf_warning_or_error)); 12433 /* We might have ignored or rejected some of the qualifiers. */ 12434 type_quals = cp_type_quals (type); 12435 12436 if (cxx_dialect >= cxx17 && type && is_auto (type) 12437 && innermost_code != cdk_function 12438 && id_declarator && declarator != id_declarator) 12439 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (type)) 12440 { 12441 error_at (typespec_loc, "template placeholder type %qT must be followed " 12442 "by a simple declarator-id", type); 12443 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here", tmpl); 12444 type = error_mark_node; 12445 } 12446 12447 staticp = 0; 12448 inlinep = decl_spec_seq_has_spec_p (declspecs, ds_inline); 12449 virtualp = decl_spec_seq_has_spec_p (declspecs, ds_virtual); 12450 explicitp = decl_spec_seq_has_spec_p (declspecs, ds_explicit); 12451 12452 storage_class = declspecs->storage_class; 12453 if (storage_class == sc_static) 12454 staticp = 1 + (decl_context == FIELD); 12455 else if (decl_context == FIELD && sfk == sfk_deduction_guide) 12456 /* Treat class-scope deduction guides as static member functions 12457 so that they get a FUNCTION_TYPE instead of a METHOD_TYPE. */ 12458 staticp = 2; 12459 12460 if (virtualp) 12461 { 12462 if (staticp == 2) 12463 { 12464 gcc_rich_location richloc (declspecs->locations[ds_virtual]); 12465 richloc.add_range (declspecs->locations[ds_storage_class]); 12466 error_at (&richloc, "member %qD cannot be declared both %<virtual%> " 12467 "and %<static%>", dname); 12468 storage_class = sc_none; 12469 staticp = 0; 12470 } 12471 if (constexpr_p && pedantic && cxx_dialect < cxx20) 12472 { 12473 gcc_rich_location richloc (declspecs->locations[ds_virtual]); 12474 richloc.add_range (declspecs->locations[ds_constexpr]); 12475 pedwarn (&richloc, OPT_Wc__20_extensions, "member %qD can be " 12476 "declared both %<virtual%> and %<constexpr%> only in " 12477 "%<-std=c++20%> or %<-std=gnu++20%>", dname); 12478 } 12479 } 12480 friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend); 12481 12482 /* Issue errors about use of storage classes for parameters. */ 12483 if (decl_context == PARM) 12484 { 12485 if (typedef_p) 12486 { 12487 error_at (declspecs->locations[ds_typedef], 12488 "typedef declaration invalid in parameter declaration"); 12489 return error_mark_node; 12490 } 12491 else if (template_parm_flag && storage_class != sc_none) 12492 { 12493 error_at (min_location (declspecs->locations[ds_thread], 12494 declspecs->locations[ds_storage_class]), 12495 "storage class specified for template parameter %qs", 12496 name); 12497 return error_mark_node; 12498 } 12499 else if (storage_class == sc_static 12500 || storage_class == sc_extern 12501 || thread_p) 12502 { 12503 error_at (min_location (declspecs->locations[ds_thread], 12504 declspecs->locations[ds_storage_class]), 12505 "storage class specified for parameter %qs", name); 12506 return error_mark_node; 12507 } 12508 12509 /* Function parameters cannot be concept. */ 12510 if (concept_p) 12511 { 12512 error_at (declspecs->locations[ds_concept], 12513 "a parameter cannot be declared %qs", "concept"); 12514 concept_p = 0; 12515 constexpr_p = 0; 12516 } 12517 /* Function parameters cannot be constexpr. If we saw one, moan 12518 and pretend it wasn't there. */ 12519 else if (constexpr_p) 12520 { 12521 error_at (declspecs->locations[ds_constexpr], 12522 "a parameter cannot be declared %qs", "constexpr"); 12523 constexpr_p = 0; 12524 } 12525 if (constinit_p) 12526 { 12527 error_at (declspecs->locations[ds_constinit], 12528 "a parameter cannot be declared %qs", "constinit"); 12529 constinit_p = 0; 12530 } 12531 if (consteval_p) 12532 { 12533 error_at (declspecs->locations[ds_consteval], 12534 "a parameter cannot be declared %qs", "consteval"); 12535 consteval_p = 0; 12536 } 12537 } 12538 12539 /* Give error if `virtual' is used outside of class declaration. */ 12540 if (virtualp 12541 && (current_class_name == NULL_TREE || decl_context != FIELD)) 12542 { 12543 error_at (declspecs->locations[ds_virtual], 12544 "%<virtual%> outside class declaration"); 12545 virtualp = 0; 12546 } 12547 12548 if (innermost_code == cdk_decomp) 12549 { 12550 location_t loc = (declarator->kind == cdk_reference 12551 ? declarator->declarator->id_loc : declarator->id_loc); 12552 if (inlinep) 12553 error_at (declspecs->locations[ds_inline], 12554 "structured binding declaration cannot be %qs", "inline"); 12555 if (typedef_p) 12556 error_at (declspecs->locations[ds_typedef], 12557 "structured binding declaration cannot be %qs", "typedef"); 12558 if (constexpr_p && !concept_p) 12559 error_at (declspecs->locations[ds_constexpr], "structured " 12560 "binding declaration cannot be %qs", "constexpr"); 12561 if (consteval_p) 12562 error_at (declspecs->locations[ds_consteval], "structured " 12563 "binding declaration cannot be %qs", "consteval"); 12564 if (thread_p && cxx_dialect < cxx20) 12565 pedwarn (declspecs->locations[ds_thread], OPT_Wc__20_extensions, 12566 "structured binding declaration can be %qs only in " 12567 "%<-std=c++20%> or %<-std=gnu++20%>", 12568 declspecs->gnu_thread_keyword_p 12569 ? "__thread" : "thread_local"); 12570 if (concept_p) 12571 error_at (declspecs->locations[ds_concept], 12572 "structured binding declaration cannot be %qs", "concept"); 12573 /* [dcl.struct.bind] "A cv that includes volatile is deprecated." */ 12574 if (type_quals & TYPE_QUAL_VOLATILE) 12575 warning_at (declspecs->locations[ds_volatile], OPT_Wvolatile, 12576 "%<volatile%>-qualified structured binding is deprecated"); 12577 switch (storage_class) 12578 { 12579 case sc_none: 12580 break; 12581 case sc_register: 12582 error_at (loc, "structured binding declaration cannot be %qs", 12583 "register"); 12584 break; 12585 case sc_static: 12586 if (cxx_dialect < cxx20) 12587 pedwarn (loc, OPT_Wc__20_extensions, 12588 "structured binding declaration can be %qs only in " 12589 "%<-std=c++20%> or %<-std=gnu++20%>", "static"); 12590 break; 12591 case sc_extern: 12592 error_at (loc, "structured binding declaration cannot be %qs", 12593 "extern"); 12594 break; 12595 case sc_mutable: 12596 error_at (loc, "structured binding declaration cannot be %qs", 12597 "mutable"); 12598 break; 12599 case sc_auto: 12600 error_at (loc, "structured binding declaration cannot be " 12601 "C++98 %<auto%>"); 12602 break; 12603 default: 12604 gcc_unreachable (); 12605 } 12606 if (TREE_CODE (type) != TEMPLATE_TYPE_PARM 12607 || TYPE_IDENTIFIER (type) != auto_identifier) 12608 { 12609 if (type != error_mark_node) 12610 { 12611 error_at (loc, "structured binding declaration cannot have " 12612 "type %qT", type); 12613 inform (loc, 12614 "type must be cv-qualified %<auto%> or reference to " 12615 "cv-qualified %<auto%>"); 12616 } 12617 type = build_qualified_type (make_auto (), type_quals); 12618 declspecs->type = type; 12619 } 12620 inlinep = 0; 12621 typedef_p = 0; 12622 constexpr_p = 0; 12623 consteval_p = 0; 12624 concept_p = 0; 12625 if (storage_class != sc_static) 12626 { 12627 storage_class = sc_none; 12628 declspecs->storage_class = sc_none; 12629 } 12630 } 12631 12632 /* Static anonymous unions are dealt with here. */ 12633 if (staticp && decl_context == TYPENAME 12634 && declspecs->type 12635 && ANON_AGGR_TYPE_P (declspecs->type)) 12636 decl_context = FIELD; 12637 12638 /* Warn about storage classes that are invalid for certain 12639 kinds of declarations (parameters, typenames, etc.). */ 12640 if (thread_p 12641 && ((storage_class 12642 && storage_class != sc_extern 12643 && storage_class != sc_static) 12644 || typedef_p)) 12645 { 12646 location_t loc 12647 = min_location (declspecs->locations[ds_thread], 12648 declspecs->locations[ds_storage_class]); 12649 error_at (loc, "multiple storage classes in declaration of %qs", name); 12650 thread_p = false; 12651 } 12652 if (decl_context != NORMAL 12653 && ((storage_class != sc_none 12654 && storage_class != sc_mutable) 12655 || thread_p)) 12656 { 12657 if ((decl_context == PARM || decl_context == CATCHPARM) 12658 && (storage_class == sc_register 12659 || storage_class == sc_auto)) 12660 ; 12661 else if (typedef_p) 12662 ; 12663 else if (decl_context == FIELD 12664 /* C++ allows static class elements. */ 12665 && storage_class == sc_static) 12666 /* C++ also allows inlines and signed and unsigned elements, 12667 but in those cases we don't come in here. */ 12668 ; 12669 else 12670 { 12671 location_t loc 12672 = min_location (declspecs->locations[ds_thread], 12673 declspecs->locations[ds_storage_class]); 12674 if (decl_context == FIELD) 12675 error_at (loc, "storage class specified for %qs", name); 12676 else if (decl_context == PARM || decl_context == CATCHPARM) 12677 error_at (loc, "storage class specified for parameter %qs", name); 12678 else 12679 error_at (loc, "storage class specified for typename"); 12680 if (storage_class == sc_register 12681 || storage_class == sc_auto 12682 || storage_class == sc_extern 12683 || thread_p) 12684 storage_class = sc_none; 12685 } 12686 } 12687 else if (storage_class == sc_extern && funcdef_flag 12688 && ! toplevel_bindings_p ()) 12689 error ("nested function %qs declared %<extern%>", name); 12690 else if (toplevel_bindings_p ()) 12691 { 12692 if (storage_class == sc_auto) 12693 error_at (declspecs->locations[ds_storage_class], 12694 "top-level declaration of %qs specifies %<auto%>", name); 12695 } 12696 else if (thread_p 12697 && storage_class != sc_extern 12698 && storage_class != sc_static) 12699 { 12700 if (declspecs->gnu_thread_keyword_p) 12701 pedwarn (declspecs->locations[ds_thread], 12702 0, "function-scope %qs implicitly auto and " 12703 "declared %<__thread%>", name); 12704 12705 /* When thread_local is applied to a variable of block scope the 12706 storage-class-specifier static is implied if it does not appear 12707 explicitly. */ 12708 storage_class = declspecs->storage_class = sc_static; 12709 staticp = 1; 12710 } 12711 12712 if (storage_class && friendp) 12713 { 12714 error_at (min_location (declspecs->locations[ds_thread], 12715 declspecs->locations[ds_storage_class]), 12716 "storage class specifiers invalid in friend function " 12717 "declarations"); 12718 storage_class = sc_none; 12719 staticp = 0; 12720 } 12721 12722 if (!id_declarator) 12723 unqualified_id = NULL_TREE; 12724 else 12725 { 12726 unqualified_id = id_declarator->u.id.unqualified_name; 12727 switch (TREE_CODE (unqualified_id)) 12728 { 12729 case BIT_NOT_EXPR: 12730 unqualified_id = TREE_OPERAND (unqualified_id, 0); 12731 if (TYPE_P (unqualified_id)) 12732 unqualified_id = constructor_name (unqualified_id); 12733 break; 12734 12735 case IDENTIFIER_NODE: 12736 case TEMPLATE_ID_EXPR: 12737 break; 12738 12739 default: 12740 gcc_unreachable (); 12741 } 12742 } 12743 12744 if (declspecs->std_attributes) 12745 { 12746 location_t attr_loc = declspecs->locations[ds_std_attribute]; 12747 if (warning_at (attr_loc, OPT_Wattributes, "attribute ignored")) 12748 inform (attr_loc, "an attribute that appertains to a type-specifier " 12749 "is ignored"); 12750 } 12751 12752 /* Determine the type of the entity declared by recurring on the 12753 declarator. */ 12754 for (; declarator; declarator = declarator->declarator) 12755 { 12756 const cp_declarator *inner_declarator; 12757 tree attrs; 12758 12759 if (type == error_mark_node) 12760 return error_mark_node; 12761 12762 attrs = declarator->attributes; 12763 if (attrs) 12764 { 12765 int attr_flags; 12766 12767 attr_flags = 0; 12768 if (declarator->kind == cdk_id) 12769 attr_flags |= (int) ATTR_FLAG_DECL_NEXT; 12770 if (declarator->kind == cdk_function) 12771 attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT; 12772 if (declarator->kind == cdk_array) 12773 attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT; 12774 tree late_attrs = NULL_TREE; 12775 if (decl_context != PARM && decl_context != TYPENAME) 12776 /* Assume that any attributes that get applied late to 12777 templates will DTRT when applied to the declaration 12778 as a whole. */ 12779 late_attrs = splice_template_attributes (&attrs, type); 12780 returned_attrs = decl_attributes (&type, 12781 chainon (returned_attrs, attrs), 12782 attr_flags); 12783 returned_attrs = chainon (late_attrs, returned_attrs); 12784 } 12785 12786 inner_declarator = declarator->declarator; 12787 12788 /* We don't want to warn in parameter context because we don't 12789 yet know if the parse will succeed, and this might turn out 12790 to be a constructor call. */ 12791 if (decl_context != PARM 12792 && decl_context != TYPENAME 12793 && !typedef_p 12794 && declarator->parenthesized != UNKNOWN_LOCATION 12795 /* If the type is class-like and the inner name used a 12796 global namespace qualifier, we need the parens. 12797 Unfortunately all we can tell is whether a qualified name 12798 was used or not. */ 12799 && !(inner_declarator 12800 && inner_declarator->kind == cdk_id 12801 && inner_declarator->u.id.qualifying_scope 12802 && (MAYBE_CLASS_TYPE_P (type) 12803 || TREE_CODE (type) == ENUMERAL_TYPE))) 12804 { 12805 if (warning_at (declarator->parenthesized, OPT_Wparentheses, 12806 "unnecessary parentheses in declaration of %qs", 12807 name)) 12808 { 12809 gcc_rich_location iloc (declarator->parenthesized); 12810 iloc.add_fixit_remove (get_start (declarator->parenthesized)); 12811 iloc.add_fixit_remove (get_finish (declarator->parenthesized)); 12812 inform (&iloc, "remove parentheses"); 12813 } 12814 } 12815 if (declarator->kind == cdk_id || declarator->kind == cdk_decomp) 12816 break; 12817 12818 switch (declarator->kind) 12819 { 12820 case cdk_array: 12821 type = create_array_type_for_decl (dname, type, 12822 declarator->u.array.bounds, 12823 declarator->id_loc); 12824 if (!valid_array_size_p (dname 12825 ? declarator->id_loc : input_location, 12826 type, dname)) 12827 type = error_mark_node; 12828 12829 if (declarator->std_attributes) 12830 /* [dcl.array]/1: 12831 12832 The optional attribute-specifier-seq appertains to the 12833 array. */ 12834 returned_attrs = chainon (returned_attrs, 12835 declarator->std_attributes); 12836 break; 12837 12838 case cdk_function: 12839 { 12840 tree arg_types; 12841 int funcdecl_p; 12842 12843 /* Declaring a function type. */ 12844 12845 /* Pick up type qualifiers which should be applied to `this'. */ 12846 memfn_quals = declarator->u.function.qualifiers; 12847 /* Pick up virt-specifiers. */ 12848 virt_specifiers = declarator->u.function.virt_specifiers; 12849 /* And ref-qualifier, too */ 12850 rqual = declarator->u.function.ref_qualifier; 12851 /* And tx-qualifier. */ 12852 tree tx_qual = declarator->u.function.tx_qualifier; 12853 /* Pick up the exception specifications. */ 12854 raises = declarator->u.function.exception_specification; 12855 /* If the exception-specification is ill-formed, let's pretend 12856 there wasn't one. */ 12857 if (raises == error_mark_node) 12858 raises = NULL_TREE; 12859 12860 if (reqs) 12861 error_at (location_of (reqs), "requires-clause on return type"); 12862 reqs = declarator->u.function.requires_clause; 12863 12864 /* Say it's a definition only for the CALL_EXPR 12865 closest to the identifier. */ 12866 funcdecl_p = inner_declarator && inner_declarator->kind == cdk_id; 12867 12868 /* Handle a late-specified return type. */ 12869 tree late_return_type = declarator->u.function.late_return_type; 12870 if (tree auto_node = type_uses_auto (type)) 12871 { 12872 if (!late_return_type) 12873 { 12874 if (!funcdecl_p) 12875 /* auto (*fp)() = f; is OK. */; 12876 else if (current_class_type 12877 && LAMBDA_TYPE_P (current_class_type)) 12878 /* OK for C++11 lambdas. */; 12879 else if (cxx_dialect < cxx14) 12880 { 12881 error_at (typespec_loc, "%qs function uses " 12882 "%<auto%> type specifier without " 12883 "trailing return type", name); 12884 inform (typespec_loc, 12885 "deduced return type only available " 12886 "with %<-std=c++14%> or %<-std=gnu++14%>"); 12887 } 12888 else if (virtualp) 12889 { 12890 error_at (typespec_loc, "virtual function " 12891 "cannot have deduced return type"); 12892 virtualp = false; 12893 } 12894 } 12895 else if (!is_auto (type) && sfk != sfk_conversion) 12896 { 12897 error_at (typespec_loc, "%qs function with trailing " 12898 "return type has %qT as its type rather " 12899 "than plain %<auto%>", name, type); 12900 return error_mark_node; 12901 } 12902 else if (is_auto (type) && AUTO_IS_DECLTYPE (type)) 12903 { 12904 if (funcdecl_p) 12905 error_at (typespec_loc, 12906 "%qs function with trailing return type " 12907 "has %<decltype(auto)%> as its type " 12908 "rather than plain %<auto%>", name); 12909 else 12910 error_at (typespec_loc, 12911 "invalid use of %<decltype(auto)%>"); 12912 return error_mark_node; 12913 } 12914 tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node); 12915 if (!tmpl) 12916 if (tree late_auto = type_uses_auto (late_return_type)) 12917 tmpl = CLASS_PLACEHOLDER_TEMPLATE (late_auto); 12918 if (tmpl) 12919 { 12920 if (!funcdecl_p || !dguide_name_p (unqualified_id)) 12921 { 12922 error_at (typespec_loc, "deduced class " 12923 "type %qD in function return type", 12924 DECL_NAME (tmpl)); 12925 inform (DECL_SOURCE_LOCATION (tmpl), 12926 "%qD declared here", tmpl); 12927 return error_mark_node; 12928 } 12929 else if (!late_return_type) 12930 { 12931 error_at (declarator->id_loc, "deduction guide " 12932 "for %qT must have trailing return " 12933 "type", TREE_TYPE (tmpl)); 12934 inform (DECL_SOURCE_LOCATION (tmpl), 12935 "%qD declared here", tmpl); 12936 return error_mark_node; 12937 } 12938 else if (CLASS_TYPE_P (late_return_type) 12939 && CLASSTYPE_TEMPLATE_INFO (late_return_type) 12940 && (CLASSTYPE_TI_TEMPLATE (late_return_type) 12941 == tmpl)) 12942 /* OK */; 12943 else 12944 error ("trailing return type %qT of deduction guide " 12945 "is not a specialization of %qT", 12946 late_return_type, TREE_TYPE (tmpl)); 12947 } 12948 } 12949 else if (late_return_type 12950 && sfk != sfk_conversion) 12951 { 12952 if (late_return_type == error_mark_node) 12953 return error_mark_node; 12954 if (cxx_dialect < cxx11) 12955 /* Not using maybe_warn_cpp0x because this should 12956 always be an error. */ 12957 error_at (typespec_loc, 12958 "trailing return type only available " 12959 "with %<-std=c++11%> or %<-std=gnu++11%>"); 12960 else 12961 error_at (typespec_loc, "%qs function with trailing " 12962 "return type not declared with %<auto%> " 12963 "type specifier", name); 12964 return error_mark_node; 12965 } 12966 if (late_return_type && sfk == sfk_conversion) 12967 { 12968 error ("a conversion function cannot have a trailing return type"); 12969 return error_mark_node; 12970 } 12971 type = splice_late_return_type (type, late_return_type); 12972 if (type == error_mark_node) 12973 return error_mark_node; 12974 12975 if (late_return_type) 12976 { 12977 late_return_type_p = true; 12978 type_quals = cp_type_quals (type); 12979 } 12980 12981 if (type_quals != TYPE_UNQUALIFIED) 12982 { 12983 if (SCALAR_TYPE_P (type) || VOID_TYPE_P (type)) 12984 warning_at (typespec_loc, OPT_Wignored_qualifiers, "type " 12985 "qualifiers ignored on function return type"); 12986 /* [dcl.fct] "A volatile-qualified return type is 12987 deprecated." */ 12988 if (type_quals & TYPE_QUAL_VOLATILE) 12989 warning_at (typespec_loc, OPT_Wvolatile, 12990 "%<volatile%>-qualified return type is " 12991 "deprecated"); 12992 12993 /* We now know that the TYPE_QUALS don't apply to the 12994 decl, but to its return type. */ 12995 type_quals = TYPE_UNQUALIFIED; 12996 } 12997 12998 /* Error about some types functions can't return. */ 12999 13000 if (TREE_CODE (type) == FUNCTION_TYPE) 13001 { 13002 error_at (typespec_loc, "%qs declared as function returning " 13003 "a function", name); 13004 return error_mark_node; 13005 } 13006 if (TREE_CODE (type) == ARRAY_TYPE) 13007 { 13008 error_at (typespec_loc, "%qs declared as function returning " 13009 "an array", name); 13010 return error_mark_node; 13011 } 13012 if (constinit_p && funcdecl_p) 13013 { 13014 error_at (declspecs->locations[ds_constinit], 13015 "%<constinit%> on function return type is not " 13016 "allowed"); 13017 return error_mark_node; 13018 } 13019 13020 if (check_decltype_auto (typespec_loc, type)) 13021 return error_mark_node; 13022 13023 if (ctype == NULL_TREE 13024 && decl_context == FIELD 13025 && funcdecl_p 13026 && friendp == 0) 13027 ctype = current_class_type; 13028 13029 if (ctype && (sfk == sfk_constructor 13030 || sfk == sfk_destructor)) 13031 { 13032 /* We are within a class's scope. If our declarator name 13033 is the same as the class name, and we are defining 13034 a function, then it is a constructor/destructor, and 13035 therefore returns a void type. */ 13036 13037 /* ISO C++ 12.4/2. A destructor may not be declared 13038 const or volatile. A destructor may not be static. 13039 A destructor may not be declared with ref-qualifier. 13040 13041 ISO C++ 12.1. A constructor may not be declared 13042 const or volatile. A constructor may not be 13043 virtual. A constructor may not be static. 13044 A constructor may not be declared with ref-qualifier. */ 13045 if (staticp == 2) 13046 error_at (declspecs->locations[ds_storage_class], 13047 (flags == DTOR_FLAG) 13048 ? G_("destructor cannot be static member " 13049 "function") 13050 : G_("constructor cannot be static member " 13051 "function")); 13052 if (memfn_quals) 13053 { 13054 error ((flags == DTOR_FLAG) 13055 ? G_("destructors may not be cv-qualified") 13056 : G_("constructors may not be cv-qualified")); 13057 memfn_quals = TYPE_UNQUALIFIED; 13058 } 13059 13060 if (rqual) 13061 { 13062 maybe_warn_cpp0x (CPP0X_REF_QUALIFIER); 13063 error ((flags == DTOR_FLAG) 13064 ? G_("destructors may not be ref-qualified") 13065 : G_("constructors may not be ref-qualified")); 13066 rqual = REF_QUAL_NONE; 13067 } 13068 13069 if (decl_context == FIELD 13070 && !member_function_or_else (ctype, 13071 current_class_type, 13072 flags)) 13073 return error_mark_node; 13074 13075 if (flags != DTOR_FLAG) 13076 { 13077 /* It's a constructor. */ 13078 if (explicitp == 1) 13079 explicitp = 2; 13080 if (virtualp) 13081 { 13082 permerror (declspecs->locations[ds_virtual], 13083 "constructors cannot be declared %<virtual%>"); 13084 virtualp = 0; 13085 } 13086 if (decl_context == FIELD 13087 && sfk != sfk_constructor) 13088 return error_mark_node; 13089 } 13090 if (decl_context == FIELD) 13091 staticp = 0; 13092 } 13093 else if (friendp) 13094 { 13095 if (virtualp) 13096 { 13097 /* Cannot be both friend and virtual. */ 13098 gcc_rich_location richloc (declspecs->locations[ds_virtual]); 13099 richloc.add_range (declspecs->locations[ds_friend]); 13100 error_at (&richloc, "virtual functions cannot be friends"); 13101 friendp = 0; 13102 } 13103 if (decl_context == NORMAL) 13104 error_at (declarator->id_loc, 13105 "friend declaration not in class definition"); 13106 if (current_function_decl && funcdef_flag) 13107 { 13108 error_at (declarator->id_loc, 13109 "cannot define friend function %qs in a local " 13110 "class definition", name); 13111 friendp = 0; 13112 } 13113 /* [class.friend]/6: A function can be defined in a friend 13114 declaration if the function name is unqualified. */ 13115 if (funcdef_flag && in_namespace) 13116 { 13117 if (in_namespace == global_namespace) 13118 error_at (declarator->id_loc, 13119 "friend function definition %qs cannot have " 13120 "a name qualified with %<::%>", name); 13121 else 13122 error_at (declarator->id_loc, 13123 "friend function definition %qs cannot have " 13124 "a name qualified with %<%D::%>", name, 13125 in_namespace); 13126 } 13127 } 13128 else if (ctype && sfk == sfk_conversion) 13129 { 13130 if (explicitp == 1) 13131 { 13132 maybe_warn_cpp0x (CPP0X_EXPLICIT_CONVERSION); 13133 explicitp = 2; 13134 } 13135 } 13136 else if (sfk == sfk_deduction_guide) 13137 { 13138 if (explicitp == 1) 13139 explicitp = 2; 13140 } 13141 13142 tree pushed_scope = NULL_TREE; 13143 if (funcdecl_p 13144 && decl_context != FIELD 13145 && inner_declarator->u.id.qualifying_scope 13146 && CLASS_TYPE_P (inner_declarator->u.id.qualifying_scope)) 13147 pushed_scope 13148 = push_scope (inner_declarator->u.id.qualifying_scope); 13149 13150 arg_types = grokparms (declarator->u.function.parameters, &parms); 13151 13152 if (pushed_scope) 13153 pop_scope (pushed_scope); 13154 13155 if (inner_declarator 13156 && inner_declarator->kind == cdk_id 13157 && inner_declarator->u.id.sfk == sfk_destructor 13158 && arg_types != void_list_node) 13159 { 13160 error_at (declarator->id_loc, 13161 "destructors may not have parameters"); 13162 arg_types = void_list_node; 13163 parms = NULL_TREE; 13164 } 13165 13166 type = build_function_type (type, arg_types); 13167 13168 tree attrs = declarator->std_attributes; 13169 if (tx_qual) 13170 { 13171 tree att = build_tree_list (tx_qual, NULL_TREE); 13172 /* transaction_safe applies to the type, but 13173 transaction_safe_dynamic applies to the function. */ 13174 if (is_attribute_p ("transaction_safe", tx_qual)) 13175 attrs = chainon (attrs, att); 13176 else 13177 returned_attrs = chainon (returned_attrs, att); 13178 } 13179 if (attrs) 13180 /* [dcl.fct]/2: 13181 13182 The optional attribute-specifier-seq appertains to 13183 the function type. */ 13184 cplus_decl_attributes (&type, attrs, 0); 13185 13186 if (raises) 13187 type = build_exception_variant (type, raises); 13188 } 13189 break; 13190 13191 case cdk_pointer: 13192 case cdk_reference: 13193 case cdk_ptrmem: 13194 /* Filter out pointers-to-references and references-to-references. 13195 We can get these if a TYPE_DECL is used. */ 13196 13197 if (TYPE_REF_P (type)) 13198 { 13199 if (declarator->kind != cdk_reference) 13200 { 13201 error ("cannot declare pointer to %q#T", type); 13202 type = TREE_TYPE (type); 13203 } 13204 13205 /* In C++0x, we allow reference to reference declarations 13206 that occur indirectly through typedefs [7.1.3/8 dcl.typedef] 13207 and template type arguments [14.3.1/4 temp.arg.type]. The 13208 check for direct reference to reference declarations, which 13209 are still forbidden, occurs below. Reasoning behind the change 13210 can be found in DR106, DR540, and the rvalue reference 13211 proposals. */ 13212 else if (cxx_dialect == cxx98) 13213 { 13214 error ("cannot declare reference to %q#T", type); 13215 type = TREE_TYPE (type); 13216 } 13217 } 13218 else if (VOID_TYPE_P (type)) 13219 { 13220 if (declarator->kind == cdk_reference) 13221 error ("cannot declare reference to %q#T", type); 13222 else if (declarator->kind == cdk_ptrmem) 13223 error ("cannot declare pointer to %q#T member", type); 13224 } 13225 13226 /* We now know that the TYPE_QUALS don't apply to the decl, 13227 but to the target of the pointer. */ 13228 type_quals = TYPE_UNQUALIFIED; 13229 13230 /* This code used to handle METHOD_TYPE, but I don't think it's 13231 possible to get it here anymore. */ 13232 gcc_assert (TREE_CODE (type) != METHOD_TYPE); 13233 if (declarator->kind == cdk_ptrmem 13234 && TREE_CODE (type) == FUNCTION_TYPE) 13235 { 13236 memfn_quals |= type_memfn_quals (type); 13237 type = build_memfn_type (type, 13238 declarator->u.pointer.class_type, 13239 memfn_quals, 13240 rqual); 13241 if (type == error_mark_node) 13242 return error_mark_node; 13243 13244 rqual = REF_QUAL_NONE; 13245 memfn_quals = TYPE_UNQUALIFIED; 13246 } 13247 13248 if (TREE_CODE (type) == FUNCTION_TYPE 13249 && (type_memfn_quals (type) != TYPE_UNQUALIFIED 13250 || type_memfn_rqual (type) != REF_QUAL_NONE)) 13251 error (declarator->kind == cdk_reference 13252 ? G_("cannot declare reference to qualified function type %qT") 13253 : G_("cannot declare pointer to qualified function type %qT"), 13254 type); 13255 13256 /* When the pointed-to type involves components of variable size, 13257 care must be taken to ensure that the size evaluation code is 13258 emitted early enough to dominate all the possible later uses 13259 and late enough for the variables on which it depends to have 13260 been assigned. 13261 13262 This is expected to happen automatically when the pointed-to 13263 type has a name/declaration of it's own, but special attention 13264 is required if the type is anonymous. 13265 13266 We handle the NORMAL and FIELD contexts here by inserting a 13267 dummy statement that just evaluates the size at a safe point 13268 and ensures it is not deferred until e.g. within a deeper 13269 conditional context (c++/43555). 13270 13271 We expect nothing to be needed here for PARM or TYPENAME. 13272 Evaluating the size at this point for TYPENAME would 13273 actually be incorrect, as we might be in the middle of an 13274 expression with side effects on the pointed-to type size 13275 "arguments" prior to the pointer declaration point and the 13276 size evaluation could end up prior to the side effects. */ 13277 13278 if (!TYPE_NAME (type) 13279 && (decl_context == NORMAL || decl_context == FIELD) 13280 && at_function_scope_p () 13281 && variably_modified_type_p (type, NULL_TREE)) 13282 { 13283 TYPE_NAME (type) = build_decl (UNKNOWN_LOCATION, TYPE_DECL, 13284 NULL_TREE, type); 13285 add_decl_expr (TYPE_NAME (type)); 13286 } 13287 13288 if (declarator->kind == cdk_reference) 13289 { 13290 /* In C++0x, the type we are creating a reference to might be 13291 a typedef which is itself a reference type. In that case, 13292 we follow the reference collapsing rules in 13293 [7.1.3/8 dcl.typedef] to create the final reference type: 13294 13295 "If a typedef TD names a type that is a reference to a type 13296 T, an attempt to create the type 'lvalue reference to cv TD' 13297 creates the type 'lvalue reference to T,' while an attempt 13298 to create the type "rvalue reference to cv TD' creates the 13299 type TD." 13300 */ 13301 if (VOID_TYPE_P (type)) 13302 /* We already gave an error. */; 13303 else if (TYPE_REF_P (type)) 13304 { 13305 if (declarator->u.reference.rvalue_ref) 13306 /* Leave type alone. */; 13307 else 13308 type = cp_build_reference_type (TREE_TYPE (type), false); 13309 } 13310 else 13311 type = cp_build_reference_type 13312 (type, declarator->u.reference.rvalue_ref); 13313 13314 /* In C++0x, we need this check for direct reference to 13315 reference declarations, which are forbidden by 13316 [8.3.2/5 dcl.ref]. Reference to reference declarations 13317 are only allowed indirectly through typedefs and template 13318 type arguments. Example: 13319 13320 void foo(int & &); // invalid ref-to-ref decl 13321 13322 typedef int & int_ref; 13323 void foo(int_ref &); // valid ref-to-ref decl 13324 */ 13325 if (inner_declarator && inner_declarator->kind == cdk_reference) 13326 error ("cannot declare reference to %q#T, which is not " 13327 "a typedef or a template type argument", type); 13328 } 13329 else if (TREE_CODE (type) == METHOD_TYPE) 13330 type = build_ptrmemfunc_type (build_pointer_type (type)); 13331 else if (declarator->kind == cdk_ptrmem) 13332 { 13333 gcc_assert (TREE_CODE (declarator->u.pointer.class_type) 13334 != NAMESPACE_DECL); 13335 if (declarator->u.pointer.class_type == error_mark_node) 13336 /* We will already have complained. */ 13337 type = error_mark_node; 13338 else 13339 type = build_ptrmem_type (declarator->u.pointer.class_type, 13340 type); 13341 } 13342 else 13343 type = build_pointer_type (type); 13344 13345 /* Process a list of type modifier keywords (such as 13346 const or volatile) that were given inside the `*' or `&'. */ 13347 13348 if (declarator->u.pointer.qualifiers) 13349 { 13350 type 13351 = cp_build_qualified_type (type, 13352 declarator->u.pointer.qualifiers); 13353 type_quals = cp_type_quals (type); 13354 } 13355 13356 /* Apply C++11 attributes to the pointer, and not to the 13357 type pointed to. This is unlike what is done for GNU 13358 attributes above. It is to comply with [dcl.ptr]/1: 13359 13360 [the optional attribute-specifier-seq (7.6.1) appertains 13361 to the pointer and not to the object pointed to]. */ 13362 if (declarator->std_attributes) 13363 decl_attributes (&type, declarator->std_attributes, 13364 0); 13365 13366 ctype = NULL_TREE; 13367 break; 13368 13369 case cdk_error: 13370 break; 13371 13372 default: 13373 gcc_unreachable (); 13374 } 13375 } 13376 13377 id_loc = declarator ? declarator->id_loc : input_location; 13378 13379 if (innermost_code != cdk_function 13380 /* Don't check this if it can be the artifical decltype(auto) 13381 we created when building a constraint in a compound-requirement: 13382 that the type-constraint is plain is going to be checked in 13383 cp_parser_compound_requirement. */ 13384 && decl_context != TYPENAME 13385 && check_decltype_auto (id_loc, type)) 13386 return error_mark_node; 13387 13388 /* A `constexpr' specifier used in an object declaration declares 13389 the object as `const'. */ 13390 if (constexpr_p && innermost_code != cdk_function) 13391 { 13392 /* DR1688 says that a `constexpr' specifier in combination with 13393 `volatile' is valid. */ 13394 13395 if (!TYPE_REF_P (type)) 13396 { 13397 type_quals |= TYPE_QUAL_CONST; 13398 type = cp_build_qualified_type (type, type_quals); 13399 } 13400 } 13401 13402 if (unqualified_id && TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR 13403 && !FUNC_OR_METHOD_TYPE_P (type) 13404 && !variable_template_p (TREE_OPERAND (unqualified_id, 0))) 13405 { 13406 error ("template-id %qD used as a declarator", 13407 unqualified_id); 13408 unqualified_id = dname; 13409 } 13410 13411 /* If TYPE is a FUNCTION_TYPE, but the function name was explicitly 13412 qualified with a class-name, turn it into a METHOD_TYPE, unless 13413 we know that the function is static. We take advantage of this 13414 opportunity to do other processing that pertains to entities 13415 explicitly declared to be class members. Note that if DECLARATOR 13416 is non-NULL, we know it is a cdk_id declarator; otherwise, we 13417 would not have exited the loop above. */ 13418 if (declarator 13419 && declarator->kind == cdk_id 13420 && declarator->u.id.qualifying_scope 13421 && MAYBE_CLASS_TYPE_P (declarator->u.id.qualifying_scope)) 13422 { 13423 ctype = declarator->u.id.qualifying_scope; 13424 ctype = TYPE_MAIN_VARIANT (ctype); 13425 template_count = num_template_headers_for_class (ctype); 13426 13427 if (ctype == current_class_type) 13428 { 13429 if (friendp) 13430 { 13431 permerror (declspecs->locations[ds_friend], 13432 "member functions are implicitly " 13433 "friends of their class"); 13434 friendp = 0; 13435 } 13436 else 13437 permerror (id_loc, "extra qualification %<%T::%> on member %qs", 13438 ctype, name); 13439 } 13440 else if (/* If the qualifying type is already complete, then we 13441 can skip the following checks. */ 13442 !COMPLETE_TYPE_P (ctype) 13443 && (/* If the function is being defined, then 13444 qualifying type must certainly be complete. */ 13445 funcdef_flag 13446 /* A friend declaration of "T::f" is OK, even if 13447 "T" is a template parameter. But, if this 13448 function is not a friend, the qualifying type 13449 must be a class. */ 13450 || (!friendp && !CLASS_TYPE_P (ctype)) 13451 /* For a declaration, the type need not be 13452 complete, if either it is dependent (since there 13453 is no meaningful definition of complete in that 13454 case) or the qualifying class is currently being 13455 defined. */ 13456 || !(dependent_type_p (ctype) 13457 || currently_open_class (ctype))) 13458 /* Check that the qualifying type is complete. */ 13459 && !complete_type_or_else (ctype, NULL_TREE)) 13460 return error_mark_node; 13461 else if (TREE_CODE (type) == FUNCTION_TYPE) 13462 { 13463 if (current_class_type 13464 && (!friendp || funcdef_flag || initialized)) 13465 { 13466 error_at (id_loc, funcdef_flag || initialized 13467 ? G_("cannot define member function %<%T::%s%> " 13468 "within %qT") 13469 : G_("cannot declare member function %<%T::%s%> " 13470 "within %qT"), 13471 ctype, name, current_class_type); 13472 return error_mark_node; 13473 } 13474 } 13475 else if (typedef_p && current_class_type) 13476 { 13477 error_at (id_loc, "cannot declare member %<%T::%s%> within %qT", 13478 ctype, name, current_class_type); 13479 return error_mark_node; 13480 } 13481 } 13482 13483 if (ctype == NULL_TREE && decl_context == FIELD && friendp == 0) 13484 ctype = current_class_type; 13485 13486 /* Now TYPE has the actual type. */ 13487 13488 if (returned_attrs) 13489 { 13490 if (attrlist) 13491 *attrlist = chainon (returned_attrs, *attrlist); 13492 else 13493 attrlist = &returned_attrs; 13494 } 13495 13496 if (declarator 13497 && declarator->kind == cdk_id 13498 && declarator->std_attributes 13499 && attrlist != NULL) 13500 { 13501 /* [dcl.meaning]/1: The optional attribute-specifier-seq following 13502 a declarator-id appertains to the entity that is declared. */ 13503 if (declarator->std_attributes != error_mark_node) 13504 *attrlist = chainon (*attrlist, declarator->std_attributes); 13505 else 13506 /* We should have already diagnosed the issue (c++/78344). */ 13507 gcc_assert (seen_error ()); 13508 } 13509 13510 /* Handle parameter packs. */ 13511 if (parameter_pack_p) 13512 { 13513 if (decl_context == PARM) 13514 /* Turn the type into a pack expansion.*/ 13515 type = make_pack_expansion (type); 13516 else 13517 error ("non-parameter %qs cannot be a parameter pack", name); 13518 } 13519 13520 if ((decl_context == FIELD || decl_context == PARM) 13521 && !processing_template_decl 13522 && variably_modified_type_p (type, NULL_TREE)) 13523 { 13524 if (decl_context == FIELD) 13525 error_at (id_loc, 13526 "data member may not have variably modified type %qT", type); 13527 else 13528 error_at (id_loc, 13529 "parameter may not have variably modified type %qT", type); 13530 type = error_mark_node; 13531 } 13532 13533 if (explicitp == 1 || (explicitp && friendp)) 13534 { 13535 /* [dcl.fct.spec] (C++11) The explicit specifier shall be used only 13536 in the declaration of a constructor or conversion function within 13537 a class definition. */ 13538 if (!current_class_type) 13539 error_at (declspecs->locations[ds_explicit], 13540 "%<explicit%> outside class declaration"); 13541 else if (friendp) 13542 error_at (declspecs->locations[ds_explicit], 13543 "%<explicit%> in friend declaration"); 13544 else 13545 error_at (declspecs->locations[ds_explicit], 13546 "only declarations of constructors and conversion operators " 13547 "can be %<explicit%>"); 13548 explicitp = 0; 13549 } 13550 13551 if (storage_class == sc_mutable) 13552 { 13553 location_t sloc = declspecs->locations[ds_storage_class]; 13554 if (decl_context != FIELD || friendp) 13555 { 13556 error_at (sloc, "non-member %qs cannot be declared %<mutable%>", 13557 name); 13558 storage_class = sc_none; 13559 } 13560 else if (decl_context == TYPENAME || typedef_p) 13561 { 13562 error_at (sloc, 13563 "non-object member %qs cannot be declared %<mutable%>", 13564 name); 13565 storage_class = sc_none; 13566 } 13567 else if (FUNC_OR_METHOD_TYPE_P (type)) 13568 { 13569 error_at (sloc, "function %qs cannot be declared %<mutable%>", 13570 name); 13571 storage_class = sc_none; 13572 } 13573 else if (staticp) 13574 { 13575 error_at (sloc, "%<static%> %qs cannot be declared %<mutable%>", 13576 name); 13577 storage_class = sc_none; 13578 } 13579 else if (type_quals & TYPE_QUAL_CONST) 13580 { 13581 error_at (sloc, "%<const%> %qs cannot be declared %<mutable%>", 13582 name); 13583 storage_class = sc_none; 13584 } 13585 else if (TYPE_REF_P (type)) 13586 { 13587 permerror (sloc, "reference %qs cannot be declared %<mutable%>", 13588 name); 13589 storage_class = sc_none; 13590 } 13591 } 13592 13593 /* If this is declaring a typedef name, return a TYPE_DECL. */ 13594 if (typedef_p && decl_context != TYPENAME) 13595 { 13596 bool alias_p = decl_spec_seq_has_spec_p (declspecs, ds_alias); 13597 tree decl; 13598 13599 if (funcdef_flag) 13600 { 13601 if (decl_context == NORMAL) 13602 error_at (id_loc, 13603 "typedef may not be a function definition"); 13604 else 13605 error_at (id_loc, 13606 "typedef may not be a member function definition"); 13607 return error_mark_node; 13608 } 13609 13610 /* This declaration: 13611 13612 typedef void f(int) const; 13613 13614 declares a function type which is not a member of any 13615 particular class, but which is cv-qualified; for 13616 example "f S::*" declares a pointer to a const-qualified 13617 member function of S. We record the cv-qualification in the 13618 function type. */ 13619 if ((rqual || memfn_quals) && TREE_CODE (type) == FUNCTION_TYPE) 13620 { 13621 type = apply_memfn_quals (type, memfn_quals, rqual); 13622 13623 /* We have now dealt with these qualifiers. */ 13624 memfn_quals = TYPE_UNQUALIFIED; 13625 rqual = REF_QUAL_NONE; 13626 } 13627 13628 if (type_uses_auto (type)) 13629 { 13630 if (alias_p) 13631 error_at (declspecs->locations[ds_type_spec], 13632 "%<auto%> not allowed in alias declaration"); 13633 else 13634 error_at (declspecs->locations[ds_type_spec], 13635 "typedef declared %<auto%>"); 13636 type = error_mark_node; 13637 } 13638 13639 if (reqs) 13640 error_at (location_of (reqs), "requires-clause on typedef"); 13641 13642 if (id_declarator && declarator->u.id.qualifying_scope) 13643 { 13644 error_at (id_loc, "typedef name may not be a nested-name-specifier"); 13645 type = error_mark_node; 13646 } 13647 13648 if (decl_context == FIELD) 13649 decl = build_lang_decl_loc (id_loc, TYPE_DECL, unqualified_id, type); 13650 else 13651 decl = build_decl (id_loc, TYPE_DECL, unqualified_id, type); 13652 13653 if (decl_context != FIELD) 13654 { 13655 if (!current_function_decl) 13656 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace); 13657 else if (DECL_MAYBE_IN_CHARGE_CDTOR_P (current_function_decl)) 13658 /* The TYPE_DECL is "abstract" because there will be 13659 clones of this constructor/destructor, and there will 13660 be copies of this TYPE_DECL generated in those 13661 clones. The decloning optimization (for space) may 13662 revert this subsequently if it determines that 13663 the clones should share a common implementation. */ 13664 DECL_ABSTRACT_P (decl) = true; 13665 13666 set_originating_module (decl); 13667 } 13668 else if (current_class_type 13669 && constructor_name_p (unqualified_id, current_class_type)) 13670 permerror (id_loc, "ISO C++ forbids nested type %qD with same name " 13671 "as enclosing class", 13672 unqualified_id); 13673 13674 /* If the user declares "typedef struct {...} foo" then the 13675 struct will have an anonymous name. Fill that name in now. 13676 Nothing can refer to it, so nothing needs know about the name 13677 change. */ 13678 if (type != error_mark_node 13679 && unqualified_id 13680 && TYPE_NAME (type) 13681 && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL 13682 && TYPE_UNNAMED_P (type) 13683 && declspecs->type_definition_p 13684 && attributes_naming_typedef_ok (*attrlist) 13685 && cp_type_quals (type) == TYPE_UNQUALIFIED) 13686 name_unnamed_type (type, decl); 13687 13688 if (signed_p 13689 || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl))) 13690 C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1; 13691 13692 bad_specifiers (decl, BSP_TYPE, virtualp, 13693 memfn_quals != TYPE_UNQUALIFIED, 13694 inlinep, friendp, raises != NULL_TREE, 13695 declspecs->locations); 13696 13697 if (alias_p) 13698 /* Acknowledge that this was written: 13699 `using analias = atype;'. */ 13700 TYPE_DECL_ALIAS_P (decl) = 1; 13701 13702 return decl; 13703 } 13704 13705 /* Detect the case of an array type of unspecified size 13706 which came, as such, direct from a typedef name. 13707 We must copy the type, so that the array's domain can be 13708 individually set by the object's initializer. */ 13709 13710 if (type && typedef_type 13711 && TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type) 13712 && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type)) 13713 type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE); 13714 13715 /* Detect where we're using a typedef of function type to declare a 13716 function. PARMS will not be set, so we must create it now. */ 13717 13718 if (type == typedef_type && TREE_CODE (type) == FUNCTION_TYPE) 13719 { 13720 tree decls = NULL_TREE; 13721 tree args; 13722 13723 for (args = TYPE_ARG_TYPES (type); 13724 args && args != void_list_node; 13725 args = TREE_CHAIN (args)) 13726 { 13727 tree decl = cp_build_parm_decl (NULL_TREE, NULL_TREE, 13728 TREE_VALUE (args)); 13729 13730 DECL_CHAIN (decl) = decls; 13731 decls = decl; 13732 } 13733 13734 parms = nreverse (decls); 13735 13736 if (decl_context != TYPENAME) 13737 { 13738 /* The qualifiers on the function type become the qualifiers on 13739 the non-static member function. */ 13740 memfn_quals |= type_memfn_quals (type); 13741 rqual = type_memfn_rqual (type); 13742 type_quals = TYPE_UNQUALIFIED; 13743 raises = TYPE_RAISES_EXCEPTIONS (type); 13744 } 13745 } 13746 13747 /* If this is a type name (such as, in a cast or sizeof), 13748 compute the type and return it now. */ 13749 13750 if (decl_context == TYPENAME) 13751 { 13752 /* Note that here we don't care about type_quals. */ 13753 13754 /* Special case: "friend class foo" looks like a TYPENAME context. */ 13755 if (friendp) 13756 { 13757 if (inlinep) 13758 { 13759 error ("%<inline%> specified for friend class declaration"); 13760 inlinep = 0; 13761 } 13762 13763 if (!current_aggr) 13764 { 13765 /* Don't allow friend declaration without a class-key. */ 13766 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM) 13767 permerror (input_location, "template parameters cannot be friends"); 13768 else if (TREE_CODE (type) == TYPENAME_TYPE) 13769 permerror (input_location, "friend declaration requires class-key, " 13770 "i.e. %<friend class %T::%D%>", 13771 TYPE_CONTEXT (type), TYPENAME_TYPE_FULLNAME (type)); 13772 else 13773 permerror (input_location, "friend declaration requires class-key, " 13774 "i.e. %<friend %#T%>", 13775 type); 13776 } 13777 13778 /* Only try to do this stuff if we didn't already give up. */ 13779 if (type != integer_type_node) 13780 { 13781 /* A friendly class? */ 13782 if (current_class_type) 13783 make_friend_class (current_class_type, TYPE_MAIN_VARIANT (type), 13784 /*complain=*/true); 13785 else 13786 error ("trying to make class %qT a friend of global scope", 13787 type); 13788 13789 type = void_type_node; 13790 } 13791 } 13792 else if (memfn_quals || rqual) 13793 { 13794 if (ctype == NULL_TREE 13795 && TREE_CODE (type) == METHOD_TYPE) 13796 ctype = TYPE_METHOD_BASETYPE (type); 13797 13798 if (ctype) 13799 type = build_memfn_type (type, ctype, memfn_quals, rqual); 13800 /* Core issue #547: need to allow this in template type args. 13801 Allow it in general in C++11 for alias-declarations. */ 13802 else if ((template_type_arg || cxx_dialect >= cxx11) 13803 && TREE_CODE (type) == FUNCTION_TYPE) 13804 type = apply_memfn_quals (type, memfn_quals, rqual); 13805 else 13806 error ("invalid qualifiers on non-member function type"); 13807 } 13808 13809 if (reqs) 13810 error_at (location_of (reqs), "requires-clause on type-id"); 13811 13812 return type; 13813 } 13814 else if (unqualified_id == NULL_TREE && decl_context != PARM 13815 && decl_context != CATCHPARM 13816 && TREE_CODE (type) != UNION_TYPE 13817 && ! bitfield 13818 && innermost_code != cdk_decomp) 13819 { 13820 error ("abstract declarator %qT used as declaration", type); 13821 return error_mark_node; 13822 } 13823 13824 if (!FUNC_OR_METHOD_TYPE_P (type)) 13825 { 13826 /* Only functions may be declared using an operator-function-id. */ 13827 if (dname && IDENTIFIER_ANY_OP_P (dname)) 13828 { 13829 error_at (id_loc, "declaration of %qD as non-function", dname); 13830 return error_mark_node; 13831 } 13832 13833 if (reqs) 13834 error_at (location_of (reqs), 13835 "requires-clause on declaration of non-function type %qT", 13836 type); 13837 } 13838 13839 /* We don't check parameter types here because we can emit a better 13840 error message later. */ 13841 if (decl_context != PARM) 13842 { 13843 type = check_var_type (unqualified_id, type, id_loc); 13844 if (type == error_mark_node) 13845 return error_mark_node; 13846 } 13847 13848 /* Now create the decl, which may be a VAR_DECL, a PARM_DECL 13849 or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE. */ 13850 13851 if (decl_context == PARM || decl_context == CATCHPARM) 13852 { 13853 if (ctype || in_namespace) 13854 error ("cannot use %<::%> in parameter declaration"); 13855 13856 tree auto_node = type_uses_auto (type); 13857 if (auto_node && !(cxx_dialect >= cxx17 && template_parm_flag)) 13858 { 13859 if (cxx_dialect >= cxx14) 13860 { 13861 if (decl_context == PARM && AUTO_IS_DECLTYPE (auto_node)) 13862 error_at (typespec_loc, 13863 "cannot declare a parameter with %<decltype(auto)%>"); 13864 else if (tree c = CLASS_PLACEHOLDER_TEMPLATE (auto_node)) 13865 { 13866 auto_diagnostic_group g; 13867 error_at (typespec_loc, 13868 "class template placeholder %qE not permitted " 13869 "in this context", c); 13870 if (decl_context == PARM && cxx_dialect >= cxx20) 13871 inform (typespec_loc, "use %<auto%> for an " 13872 "abbreviated function template"); 13873 } 13874 else 13875 error_at (typespec_loc, 13876 "%<auto%> parameter not permitted in this context"); 13877 } 13878 else 13879 error_at (typespec_loc, "parameter declared %<auto%>"); 13880 type = error_mark_node; 13881 } 13882 13883 /* A parameter declared as an array of T is really a pointer to T. 13884 One declared as a function is really a pointer to a function. 13885 One declared as a member is really a pointer to member. */ 13886 13887 if (TREE_CODE (type) == ARRAY_TYPE) 13888 { 13889 /* Transfer const-ness of array into that of type pointed to. */ 13890 type = build_pointer_type (TREE_TYPE (type)); 13891 type_quals = TYPE_UNQUALIFIED; 13892 array_parameter_p = true; 13893 } 13894 else if (TREE_CODE (type) == FUNCTION_TYPE) 13895 type = build_pointer_type (type); 13896 } 13897 13898 if (ctype && TREE_CODE (type) == FUNCTION_TYPE && staticp < 2 13899 && !(unqualified_id 13900 && identifier_p (unqualified_id) 13901 && IDENTIFIER_NEWDEL_OP_P (unqualified_id))) 13902 { 13903 cp_cv_quals real_quals = memfn_quals; 13904 if (cxx_dialect < cxx14 && constexpr_p 13905 && sfk != sfk_constructor && sfk != sfk_destructor) 13906 real_quals |= TYPE_QUAL_CONST; 13907 type = build_memfn_type (type, ctype, real_quals, rqual); 13908 } 13909 13910 { 13911 tree decl = NULL_TREE; 13912 13913 if (decl_context == PARM) 13914 { 13915 decl = cp_build_parm_decl (NULL_TREE, unqualified_id, type); 13916 DECL_ARRAY_PARAMETER_P (decl) = array_parameter_p; 13917 13918 bad_specifiers (decl, BSP_PARM, virtualp, 13919 memfn_quals != TYPE_UNQUALIFIED, 13920 inlinep, friendp, raises != NULL_TREE, 13921 declspecs->locations); 13922 } 13923 else if (decl_context == FIELD) 13924 { 13925 if (!staticp && !friendp && !FUNC_OR_METHOD_TYPE_P (type)) 13926 if (tree auto_node = type_uses_auto (type)) 13927 { 13928 location_t tloc = declspecs->locations[ds_type_spec]; 13929 if (CLASS_PLACEHOLDER_TEMPLATE (auto_node)) 13930 error_at (tloc, "invalid use of template-name %qE without an " 13931 "argument list", 13932 CLASS_PLACEHOLDER_TEMPLATE (auto_node)); 13933 else 13934 error_at (tloc, "non-static data member declared with " 13935 "placeholder %qT", auto_node); 13936 type = error_mark_node; 13937 } 13938 13939 /* The C99 flexible array extension. */ 13940 if (!staticp && TREE_CODE (type) == ARRAY_TYPE 13941 && TYPE_DOMAIN (type) == NULL_TREE) 13942 { 13943 if (ctype 13944 && (TREE_CODE (ctype) == UNION_TYPE 13945 || TREE_CODE (ctype) == QUAL_UNION_TYPE)) 13946 { 13947 error_at (id_loc, "flexible array member in union"); 13948 type = error_mark_node; 13949 } 13950 else 13951 { 13952 /* Array is a flexible member. */ 13953 if (name) 13954 pedwarn (id_loc, OPT_Wpedantic, 13955 "ISO C++ forbids flexible array member %qs", name); 13956 else 13957 pedwarn (input_location, OPT_Wpedantic, 13958 "ISO C++ forbids flexible array members"); 13959 13960 /* Flexible array member has a null domain. */ 13961 type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE); 13962 } 13963 } 13964 13965 if (type == error_mark_node) 13966 { 13967 /* Happens when declaring arrays of sizes which 13968 are error_mark_node, for example. */ 13969 decl = NULL_TREE; 13970 } 13971 else if (in_namespace && !friendp) 13972 { 13973 /* Something like struct S { int N::j; }; */ 13974 error_at (id_loc, "invalid use of %<::%>"); 13975 return error_mark_node; 13976 } 13977 else if (FUNC_OR_METHOD_TYPE_P (type) && unqualified_id) 13978 { 13979 int publicp = 0; 13980 tree function_context; 13981 13982 if (friendp == 0) 13983 { 13984 /* This should never happen in pure C++ (the check 13985 could be an assert). It could happen in 13986 Objective-C++ if someone writes invalid code that 13987 uses a function declaration for an instance 13988 variable or property (instance variables and 13989 properties are parsed as FIELD_DECLs, but they are 13990 part of an Objective-C class, not a C++ class). 13991 That code is invalid and is caught by this 13992 check. */ 13993 if (!ctype) 13994 { 13995 error ("declaration of function %qD in invalid context", 13996 unqualified_id); 13997 return error_mark_node; 13998 } 13999 14000 /* ``A union may [ ... ] not [ have ] virtual functions.'' 14001 ARM 9.5 */ 14002 if (virtualp && TREE_CODE (ctype) == UNION_TYPE) 14003 { 14004 error_at (declspecs->locations[ds_virtual], 14005 "function %qD declared %<virtual%> inside a union", 14006 unqualified_id); 14007 return error_mark_node; 14008 } 14009 14010 if (virtualp 14011 && identifier_p (unqualified_id) 14012 && IDENTIFIER_NEWDEL_OP_P (unqualified_id)) 14013 { 14014 error_at (declspecs->locations[ds_virtual], 14015 "%qD cannot be declared %<virtual%>, since it " 14016 "is always static", unqualified_id); 14017 virtualp = 0; 14018 } 14019 } 14020 14021 /* Check that the name used for a destructor makes sense. */ 14022 if (sfk == sfk_destructor) 14023 { 14024 tree uqname = id_declarator->u.id.unqualified_name; 14025 14026 if (!ctype) 14027 { 14028 gcc_assert (friendp); 14029 error_at (id_loc, "expected qualified name in friend " 14030 "declaration for destructor %qD", uqname); 14031 return error_mark_node; 14032 } 14033 14034 if (!check_dtor_name (ctype, TREE_OPERAND (uqname, 0))) 14035 { 14036 error_at (id_loc, "declaration of %qD as member of %qT", 14037 uqname, ctype); 14038 return error_mark_node; 14039 } 14040 if (concept_p) 14041 { 14042 error_at (declspecs->locations[ds_concept], 14043 "a destructor cannot be %qs", "concept"); 14044 return error_mark_node; 14045 } 14046 if (constexpr_p && cxx_dialect < cxx20) 14047 { 14048 error_at (declspecs->locations[ds_constexpr], 14049 "%<constexpr%> destructors only available" 14050 " with %<-std=c++20%> or %<-std=gnu++20%>"); 14051 return error_mark_node; 14052 } 14053 if (consteval_p) 14054 { 14055 error_at (declspecs->locations[ds_consteval], 14056 "a destructor cannot be %qs", "consteval"); 14057 return error_mark_node; 14058 } 14059 } 14060 else if (sfk == sfk_constructor && friendp && !ctype) 14061 { 14062 error ("expected qualified name in friend declaration " 14063 "for constructor %qD", 14064 id_declarator->u.id.unqualified_name); 14065 return error_mark_node; 14066 } 14067 if (sfk == sfk_constructor) 14068 if (concept_p) 14069 { 14070 error_at (declspecs->locations[ds_concept], 14071 "a constructor cannot be %<concept%>"); 14072 return error_mark_node; 14073 } 14074 if (concept_p) 14075 { 14076 error_at (declspecs->locations[ds_concept], 14077 "a concept cannot be a member function"); 14078 concept_p = false; 14079 } 14080 else if (consteval_p 14081 && identifier_p (unqualified_id) 14082 && IDENTIFIER_NEWDEL_OP_P (unqualified_id)) 14083 { 14084 error_at (declspecs->locations[ds_consteval], 14085 "%qD cannot be %qs", unqualified_id, "consteval"); 14086 consteval_p = false; 14087 } 14088 14089 if (TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR) 14090 { 14091 tree tmpl = TREE_OPERAND (unqualified_id, 0); 14092 if (variable_template_p (tmpl)) 14093 { 14094 error_at (id_loc, "specialization of variable template " 14095 "%qD declared as function", tmpl); 14096 inform (DECL_SOURCE_LOCATION (tmpl), 14097 "variable template declared here"); 14098 return error_mark_node; 14099 } 14100 } 14101 14102 /* Tell grokfndecl if it needs to set TREE_PUBLIC on the node. */ 14103 function_context 14104 = (ctype != NULL_TREE 14105 ? decl_function_context (TYPE_MAIN_DECL (ctype)) : NULL_TREE); 14106 publicp = ((! friendp || ! staticp) 14107 && function_context == NULL_TREE); 14108 14109 decl = grokfndecl (ctype, type, 14110 TREE_CODE (unqualified_id) != TEMPLATE_ID_EXPR 14111 ? unqualified_id : dname, 14112 parms, 14113 unqualified_id, 14114 declspecs, 14115 reqs, 14116 virtualp, flags, memfn_quals, rqual, raises, 14117 friendp ? -1 : 0, friendp, publicp, 14118 inlinep | (2 * constexpr_p) | (4 * concept_p) 14119 | (8 * consteval_p), 14120 initialized == SD_DELETED, sfk, 14121 funcdef_flag, late_return_type_p, 14122 template_count, in_namespace, 14123 attrlist, id_loc); 14124 decl = set_virt_specifiers (decl, virt_specifiers); 14125 if (decl == NULL_TREE) 14126 return error_mark_node; 14127 #if 0 14128 /* This clobbers the attrs stored in `decl' from `attrlist'. */ 14129 /* The decl and setting of decl_attr is also turned off. */ 14130 decl = build_decl_attribute_variant (decl, decl_attr); 14131 #endif 14132 14133 /* [class.conv.ctor] 14134 14135 A constructor declared without the function-specifier 14136 explicit that can be called with a single parameter 14137 specifies a conversion from the type of its first 14138 parameter to the type of its class. Such a constructor 14139 is called a converting constructor. */ 14140 if (explicitp == 2) 14141 DECL_NONCONVERTING_P (decl) = 1; 14142 14143 if (declspecs->explicit_specifier) 14144 store_explicit_specifier (decl, declspecs->explicit_specifier); 14145 } 14146 else if (!staticp 14147 && ((current_class_type 14148 && same_type_p (TYPE_MAIN_VARIANT (type), 14149 current_class_type)) 14150 || (!dependent_type_p (type) 14151 && !COMPLETE_TYPE_P (complete_type (type)) 14152 && (!complete_or_array_type_p (type) 14153 || initialized == SD_UNINITIALIZED)))) 14154 { 14155 if (TREE_CODE (type) != ARRAY_TYPE 14156 || !COMPLETE_TYPE_P (TREE_TYPE (type))) 14157 { 14158 if (unqualified_id) 14159 { 14160 error_at (id_loc, "field %qD has incomplete type %qT", 14161 unqualified_id, type); 14162 cxx_incomplete_type_inform (strip_array_types (type)); 14163 } 14164 else 14165 error ("name %qT has incomplete type", type); 14166 14167 type = error_mark_node; 14168 decl = NULL_TREE; 14169 } 14170 } 14171 else if (!verify_type_context (input_location, 14172 staticp 14173 ? TCTX_STATIC_STORAGE 14174 : TCTX_FIELD, type)) 14175 { 14176 type = error_mark_node; 14177 decl = NULL_TREE; 14178 } 14179 else 14180 { 14181 if (friendp) 14182 { 14183 if (unqualified_id) 14184 error_at (id_loc, 14185 "%qE is neither function nor member function; " 14186 "cannot be declared friend", unqualified_id); 14187 else 14188 error ("unnamed field is neither function nor member " 14189 "function; cannot be declared friend"); 14190 return error_mark_node; 14191 } 14192 decl = NULL_TREE; 14193 } 14194 14195 if (friendp) 14196 { 14197 /* Packages tend to use GNU attributes on friends, so we only 14198 warn for standard attributes. */ 14199 if (attrlist && !funcdef_flag && cxx11_attribute_p (*attrlist)) 14200 { 14201 *attrlist = NULL_TREE; 14202 if (warning_at (id_loc, OPT_Wattributes, "attribute ignored")) 14203 inform (id_loc, "an attribute that appertains to a friend " 14204 "declaration that is not a definition is ignored"); 14205 } 14206 /* Friends are treated specially. */ 14207 if (ctype == current_class_type) 14208 ; /* We already issued a permerror. */ 14209 else if (decl && DECL_NAME (decl)) 14210 { 14211 set_originating_module (decl, true); 14212 14213 if (initialized) 14214 /* Kludge: We need funcdef_flag to be true in do_friend for 14215 in-class defaulted functions, but that breaks grokfndecl. 14216 So set it here. */ 14217 funcdef_flag = true; 14218 14219 if (template_class_depth (current_class_type) == 0) 14220 { 14221 decl = check_explicit_specialization 14222 (unqualified_id, decl, template_count, 14223 2 * funcdef_flag + 4); 14224 if (decl == error_mark_node) 14225 return error_mark_node; 14226 } 14227 14228 tree scope = ctype ? ctype : in_namespace; 14229 decl = do_friend (scope, unqualified_id, decl, 14230 flags, funcdef_flag); 14231 return decl; 14232 } 14233 else 14234 return error_mark_node; 14235 } 14236 14237 /* Structure field. It may not be a function, except for C++. */ 14238 14239 if (decl == NULL_TREE) 14240 { 14241 if (staticp) 14242 { 14243 /* C++ allows static class members. All other work 14244 for this is done by grokfield. */ 14245 decl = build_lang_decl_loc (id_loc, VAR_DECL, 14246 unqualified_id, type); 14247 set_linkage_for_static_data_member (decl); 14248 if (concept_p) 14249 error_at (declspecs->locations[ds_concept], 14250 "static data member %qE declared %qs", 14251 unqualified_id, "concept"); 14252 else if (constexpr_p && !initialized) 14253 { 14254 error_at (DECL_SOURCE_LOCATION (decl), 14255 "%<constexpr%> static data member %qD must " 14256 "have an initializer", decl); 14257 constexpr_p = false; 14258 } 14259 if (consteval_p) 14260 error_at (declspecs->locations[ds_consteval], 14261 "static data member %qE declared %qs", 14262 unqualified_id, "consteval"); 14263 14264 if (inlinep) 14265 mark_inline_variable (decl, declspecs->locations[ds_inline]); 14266 14267 if (!DECL_VAR_DECLARED_INLINE_P (decl) 14268 && !(cxx_dialect >= cxx17 && constexpr_p)) 14269 /* Even if there is an in-class initialization, DECL 14270 is considered undefined until an out-of-class 14271 definition is provided, unless this is an inline 14272 variable. */ 14273 DECL_EXTERNAL (decl) = 1; 14274 14275 if (thread_p) 14276 { 14277 CP_DECL_THREAD_LOCAL_P (decl) = true; 14278 if (!processing_template_decl) 14279 set_decl_tls_model (decl, decl_default_tls_model (decl)); 14280 if (declspecs->gnu_thread_keyword_p) 14281 SET_DECL_GNU_TLS_P (decl); 14282 } 14283 14284 /* Set the constraints on the declaration. */ 14285 bool memtmpl = (current_template_depth 14286 > template_class_depth (current_class_type)); 14287 if (memtmpl) 14288 { 14289 tree tmpl_reqs 14290 = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms); 14291 tree ci = build_constraints (tmpl_reqs, NULL_TREE); 14292 set_constraints (decl, ci); 14293 } 14294 } 14295 else 14296 { 14297 if (concept_p) 14298 { 14299 error_at (declspecs->locations[ds_concept], 14300 "non-static data member %qE declared %qs", 14301 unqualified_id, "concept"); 14302 concept_p = false; 14303 constexpr_p = false; 14304 } 14305 else if (constexpr_p) 14306 { 14307 error_at (declspecs->locations[ds_constexpr], 14308 "non-static data member %qE declared %qs", 14309 unqualified_id, "constexpr"); 14310 constexpr_p = false; 14311 } 14312 if (constinit_p) 14313 { 14314 error_at (declspecs->locations[ds_constinit], 14315 "non-static data member %qE declared %qs", 14316 unqualified_id, "constinit"); 14317 constinit_p = false; 14318 } 14319 if (consteval_p) 14320 { 14321 error_at (declspecs->locations[ds_consteval], 14322 "non-static data member %qE declared %qs", 14323 unqualified_id, "consteval"); 14324 consteval_p = false; 14325 } 14326 decl = build_decl (id_loc, FIELD_DECL, unqualified_id, type); 14327 DECL_NONADDRESSABLE_P (decl) = bitfield; 14328 if (bitfield && !unqualified_id) 14329 DECL_PADDING_P (decl) = 1; 14330 14331 if (storage_class == sc_mutable) 14332 { 14333 DECL_MUTABLE_P (decl) = 1; 14334 storage_class = sc_none; 14335 } 14336 14337 if (initialized) 14338 { 14339 /* An attempt is being made to initialize a non-static 14340 member. This is new in C++11. */ 14341 maybe_warn_cpp0x (CPP0X_NSDMI, init_loc); 14342 14343 /* If this has been parsed with static storage class, but 14344 errors forced staticp to be cleared, ensure NSDMI is 14345 not present. */ 14346 if (declspecs->storage_class == sc_static) 14347 DECL_INITIAL (decl) = error_mark_node; 14348 } 14349 } 14350 14351 bad_specifiers (decl, BSP_FIELD, virtualp, 14352 memfn_quals != TYPE_UNQUALIFIED, 14353 staticp ? false : inlinep, friendp, 14354 raises != NULL_TREE, 14355 declspecs->locations); 14356 } 14357 } 14358 else if (FUNC_OR_METHOD_TYPE_P (type)) 14359 { 14360 tree original_name; 14361 int publicp = 0; 14362 14363 if (!unqualified_id) 14364 return error_mark_node; 14365 14366 if (TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR) 14367 original_name = dname; 14368 else 14369 original_name = unqualified_id; 14370 // FIXME:gcc_assert (original_name == dname); 14371 14372 if (storage_class == sc_auto) 14373 error_at (declspecs->locations[ds_storage_class], 14374 "storage class %<auto%> invalid for function %qs", name); 14375 else if (storage_class == sc_register) 14376 error_at (declspecs->locations[ds_storage_class], 14377 "storage class %<register%> invalid for function %qs", 14378 name); 14379 else if (thread_p) 14380 { 14381 if (declspecs->gnu_thread_keyword_p) 14382 error_at (declspecs->locations[ds_thread], 14383 "storage class %<__thread%> invalid for function %qs", 14384 name); 14385 else 14386 error_at (declspecs->locations[ds_thread], 14387 "storage class %<thread_local%> invalid for " 14388 "function %qs", name); 14389 } 14390 14391 if (virt_specifiers) 14392 error ("virt-specifiers in %qs not allowed outside a class " 14393 "definition", name); 14394 /* Function declaration not at top level. 14395 Storage classes other than `extern' are not allowed 14396 and `extern' makes no difference. */ 14397 if (! toplevel_bindings_p () 14398 && (storage_class == sc_static 14399 || decl_spec_seq_has_spec_p (declspecs, ds_inline)) 14400 && pedantic) 14401 { 14402 if (storage_class == sc_static) 14403 pedwarn (declspecs->locations[ds_storage_class], OPT_Wpedantic, 14404 "%<static%> specifier invalid for function %qs " 14405 "declared out of global scope", name); 14406 else 14407 pedwarn (declspecs->locations[ds_inline], OPT_Wpedantic, 14408 "%<inline%> specifier invalid for function %qs " 14409 "declared out of global scope", name); 14410 } 14411 14412 if (ctype == NULL_TREE) 14413 { 14414 if (virtualp) 14415 { 14416 error ("virtual non-class function %qs", name); 14417 virtualp = 0; 14418 } 14419 else if (sfk == sfk_constructor 14420 || sfk == sfk_destructor) 14421 { 14422 error (funcdef_flag 14423 ? G_("%qs defined in a non-class scope") 14424 : G_("%qs declared in a non-class scope"), name); 14425 sfk = sfk_none; 14426 } 14427 } 14428 if (consteval_p 14429 && identifier_p (unqualified_id) 14430 && IDENTIFIER_NEWDEL_OP_P (unqualified_id)) 14431 { 14432 error_at (declspecs->locations[ds_consteval], 14433 "%qD cannot be %qs", unqualified_id, "consteval"); 14434 consteval_p = false; 14435 } 14436 14437 /* Record whether the function is public. */ 14438 publicp = (ctype != NULL_TREE 14439 || storage_class != sc_static); 14440 14441 decl = grokfndecl (ctype, type, original_name, parms, unqualified_id, 14442 declspecs, 14443 reqs, virtualp, flags, memfn_quals, rqual, raises, 14444 1, friendp, 14445 publicp, 14446 inlinep | (2 * constexpr_p) | (4 * concept_p) 14447 | (8 * consteval_p), 14448 initialized == SD_DELETED, 14449 sfk, 14450 funcdef_flag, 14451 late_return_type_p, 14452 template_count, in_namespace, attrlist, 14453 id_loc); 14454 if (decl == NULL_TREE) 14455 return error_mark_node; 14456 14457 if (explicitp == 2) 14458 DECL_NONCONVERTING_P (decl) = 1; 14459 if (staticp == 1) 14460 { 14461 int invalid_static = 0; 14462 14463 /* Don't allow a static member function in a class, and forbid 14464 declaring main to be static. */ 14465 if (TREE_CODE (type) == METHOD_TYPE) 14466 { 14467 permerror (input_location, "cannot declare member function %qD to have " 14468 "static linkage", decl); 14469 invalid_static = 1; 14470 } 14471 else if (current_function_decl) 14472 { 14473 /* 7.1.1: There can be no static function declarations within a 14474 block. */ 14475 error_at (declspecs->locations[ds_storage_class], 14476 "cannot declare static function inside another function"); 14477 invalid_static = 1; 14478 } 14479 14480 if (invalid_static) 14481 { 14482 staticp = 0; 14483 storage_class = sc_none; 14484 } 14485 } 14486 if (declspecs->explicit_specifier) 14487 store_explicit_specifier (decl, declspecs->explicit_specifier); 14488 } 14489 else 14490 { 14491 /* It's a variable. */ 14492 14493 /* An uninitialized decl with `extern' is a reference. */ 14494 decl = grokvardecl (type, dname, unqualified_id, 14495 declspecs, 14496 initialized, 14497 type_quals, 14498 inlinep, 14499 concept_p, 14500 template_count, 14501 ctype ? ctype : in_namespace, 14502 id_loc); 14503 if (decl == NULL_TREE) 14504 return error_mark_node; 14505 14506 bad_specifiers (decl, BSP_VAR, virtualp, 14507 memfn_quals != TYPE_UNQUALIFIED, 14508 inlinep, friendp, raises != NULL_TREE, 14509 declspecs->locations); 14510 14511 if (ctype) 14512 { 14513 DECL_CONTEXT (decl) = ctype; 14514 if (staticp == 1) 14515 { 14516 permerror (declspecs->locations[ds_storage_class], 14517 "%<static%> may not be used when defining " 14518 "(as opposed to declaring) a static data member"); 14519 staticp = 0; 14520 storage_class = sc_none; 14521 } 14522 if (storage_class == sc_register && TREE_STATIC (decl)) 14523 { 14524 error ("static member %qD declared %<register%>", decl); 14525 storage_class = sc_none; 14526 } 14527 if (storage_class == sc_extern && pedantic) 14528 { 14529 pedwarn (input_location, OPT_Wpedantic, 14530 "cannot explicitly declare member %q#D to have " 14531 "extern linkage", decl); 14532 storage_class = sc_none; 14533 } 14534 } 14535 else if (constexpr_p && DECL_EXTERNAL (decl)) 14536 { 14537 error_at (DECL_SOURCE_LOCATION (decl), 14538 "declaration of %<constexpr%> variable %qD " 14539 "is not a definition", decl); 14540 constexpr_p = false; 14541 } 14542 if (consteval_p) 14543 { 14544 error_at (DECL_SOURCE_LOCATION (decl), 14545 "a variable cannot be declared %<consteval%>"); 14546 consteval_p = false; 14547 } 14548 14549 if (inlinep) 14550 mark_inline_variable (decl, declspecs->locations[ds_inline]); 14551 if (innermost_code == cdk_decomp) 14552 { 14553 gcc_assert (declarator && declarator->kind == cdk_decomp); 14554 DECL_SOURCE_LOCATION (decl) = id_loc; 14555 DECL_ARTIFICIAL (decl) = 1; 14556 fit_decomposition_lang_decl (decl, NULL_TREE); 14557 } 14558 } 14559 14560 if (VAR_P (decl) && !initialized) 14561 if (tree auto_node = type_uses_auto (type)) 14562 if (!CLASS_PLACEHOLDER_TEMPLATE (auto_node)) 14563 { 14564 location_t loc = declspecs->locations[ds_type_spec]; 14565 error_at (loc, "declaration of %q#D has no initializer", decl); 14566 TREE_TYPE (decl) = error_mark_node; 14567 } 14568 14569 if (storage_class == sc_extern && initialized && !funcdef_flag) 14570 { 14571 if (toplevel_bindings_p ()) 14572 { 14573 /* It's common practice (and completely valid) to have a const 14574 be initialized and declared extern. */ 14575 if (!(type_quals & TYPE_QUAL_CONST)) 14576 warning_at (DECL_SOURCE_LOCATION (decl), 0, 14577 "%qs initialized and declared %<extern%>", name); 14578 } 14579 else 14580 { 14581 error_at (DECL_SOURCE_LOCATION (decl), 14582 "%qs has both %<extern%> and initializer", name); 14583 return error_mark_node; 14584 } 14585 } 14586 14587 /* Record `register' declaration for warnings on & 14588 and in case doing stupid register allocation. */ 14589 14590 if (storage_class == sc_register) 14591 { 14592 DECL_REGISTER (decl) = 1; 14593 /* Warn about register storage specifiers on PARM_DECLs. */ 14594 if (TREE_CODE (decl) == PARM_DECL) 14595 { 14596 if (cxx_dialect >= cxx17) 14597 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wregister, 14598 "ISO C++17 does not allow %<register%> storage " 14599 "class specifier"); 14600 else 14601 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wregister, 14602 "%<register%> storage class specifier used"); 14603 } 14604 } 14605 else if (storage_class == sc_extern) 14606 DECL_THIS_EXTERN (decl) = 1; 14607 else if (storage_class == sc_static) 14608 DECL_THIS_STATIC (decl) = 1; 14609 14610 if (VAR_P (decl)) 14611 { 14612 /* Set constexpr flag on vars (functions got it in grokfndecl). */ 14613 if (constexpr_p) 14614 DECL_DECLARED_CONSTEXPR_P (decl) = true; 14615 /* And the constinit flag (which only applies to variables). */ 14616 else if (constinit_p) 14617 DECL_DECLARED_CONSTINIT_P (decl) = true; 14618 } 14619 14620 /* Record constancy and volatility on the DECL itself . There's 14621 no need to do this when processing a template; we'll do this 14622 for the instantiated declaration based on the type of DECL. */ 14623 if (!processing_template_decl 14624 /* Don't do it for instantiated variable templates either, 14625 cp_apply_type_quals_to_decl should have been called on it 14626 already and might have been overridden in cp_finish_decl 14627 if initializer needs runtime initialization. */ 14628 && (!VAR_P (decl) || !DECL_TEMPLATE_INSTANTIATED (decl))) 14629 cp_apply_type_quals_to_decl (type_quals, decl); 14630 14631 return decl; 14632 } 14633 } 14634 14635 /* Subroutine of start_function. Ensure that each of the parameter 14637 types (as listed in PARMS) is complete, as is required for a 14638 function definition. */ 14639 14640 static void 14641 require_complete_types_for_parms (tree parms) 14642 { 14643 for (; parms; parms = DECL_CHAIN (parms)) 14644 { 14645 if (dependent_type_p (TREE_TYPE (parms))) 14646 continue; 14647 if (!VOID_TYPE_P (TREE_TYPE (parms)) 14648 && complete_type_or_else (TREE_TYPE (parms), parms)) 14649 { 14650 relayout_decl (parms); 14651 DECL_ARG_TYPE (parms) = type_passed_as (TREE_TYPE (parms)); 14652 14653 abstract_virtuals_error (parms, TREE_TYPE (parms)); 14654 maybe_warn_parm_abi (TREE_TYPE (parms), 14655 DECL_SOURCE_LOCATION (parms)); 14656 } 14657 else 14658 /* grokparms or complete_type_or_else will have already issued 14659 an error. */ 14660 TREE_TYPE (parms) = error_mark_node; 14661 } 14662 } 14663 14664 /* Returns nonzero if T is a local variable. */ 14665 14666 int 14667 local_variable_p (const_tree t) 14668 { 14669 if ((VAR_P (t) 14670 && (DECL_LOCAL_DECL_P (t) 14671 || !DECL_CONTEXT (t) 14672 || TREE_CODE (DECL_CONTEXT (t)) == FUNCTION_DECL)) 14673 || (TREE_CODE (t) == PARM_DECL)) 14674 return 1; 14675 14676 return 0; 14677 } 14678 14679 /* Like local_variable_p, but suitable for use as a tree-walking 14680 function. */ 14681 14682 static tree 14683 local_variable_p_walkfn (tree *tp, int *walk_subtrees, 14684 void * /*data*/) 14685 { 14686 if (unevaluated_p (TREE_CODE (*tp))) 14687 { 14688 /* DR 2082 permits local variables in unevaluated contexts 14689 within a default argument. */ 14690 *walk_subtrees = 0; 14691 return NULL_TREE; 14692 } 14693 14694 if (local_variable_p (*tp) 14695 && (!DECL_ARTIFICIAL (*tp) || DECL_NAME (*tp) == this_identifier)) 14696 return *tp; 14697 else if (TYPE_P (*tp)) 14698 *walk_subtrees = 0; 14699 14700 return NULL_TREE; 14701 } 14702 14703 /* Check that ARG, which is a default-argument expression for a 14704 parameter DECL, is valid. Returns ARG, or ERROR_MARK_NODE, if 14705 something goes wrong. DECL may also be a _TYPE node, rather than a 14706 DECL, if there is no DECL available. */ 14707 14708 tree 14709 check_default_argument (tree decl, tree arg, tsubst_flags_t complain) 14710 { 14711 tree var; 14712 tree decl_type; 14713 14714 if (TREE_CODE (arg) == DEFERRED_PARSE) 14715 /* We get a DEFERRED_PARSE when looking at an in-class declaration 14716 with a default argument. Ignore the argument for now; we'll 14717 deal with it after the class is complete. */ 14718 return arg; 14719 14720 if (TYPE_P (decl)) 14721 { 14722 decl_type = decl; 14723 decl = NULL_TREE; 14724 } 14725 else 14726 decl_type = TREE_TYPE (decl); 14727 14728 if (arg == error_mark_node 14729 || decl == error_mark_node 14730 || TREE_TYPE (arg) == error_mark_node 14731 || decl_type == error_mark_node) 14732 /* Something already went wrong. There's no need to check 14733 further. */ 14734 return error_mark_node; 14735 14736 /* [dcl.fct.default] 14737 14738 A default argument expression is implicitly converted to the 14739 parameter type. */ 14740 ++cp_unevaluated_operand; 14741 /* Avoid digest_init clobbering the initializer. */ 14742 tree carg = BRACE_ENCLOSED_INITIALIZER_P (arg) ? unshare_expr (arg): arg; 14743 perform_implicit_conversion_flags (decl_type, carg, complain, 14744 LOOKUP_IMPLICIT); 14745 --cp_unevaluated_operand; 14746 14747 /* Avoid redundant -Wzero-as-null-pointer-constant warnings at 14748 the call sites. */ 14749 if (TYPE_PTR_OR_PTRMEM_P (decl_type) 14750 && null_ptr_cst_p (arg) 14751 /* Don't lose side-effects as in PR90473. */ 14752 && !TREE_SIDE_EFFECTS (arg)) 14753 return nullptr_node; 14754 14755 /* [dcl.fct.default] 14756 14757 Local variables shall not be used in default argument 14758 expressions. 14759 14760 The keyword `this' shall not be used in a default argument of a 14761 member function. */ 14762 var = cp_walk_tree_without_duplicates (&arg, local_variable_p_walkfn, NULL); 14763 if (var) 14764 { 14765 if (complain & tf_warning_or_error) 14766 { 14767 if (DECL_NAME (var) == this_identifier) 14768 permerror (input_location, "default argument %qE uses %qD", 14769 arg, var); 14770 else 14771 error ("default argument %qE uses local variable %qD", arg, var); 14772 } 14773 return error_mark_node; 14774 } 14775 14776 /* All is well. */ 14777 return arg; 14778 } 14779 14780 /* Returns a deprecated type used within TYPE, or NULL_TREE if none. */ 14781 14782 static tree 14783 type_is_deprecated (tree type) 14784 { 14785 enum tree_code code; 14786 if (TREE_DEPRECATED (type)) 14787 return type; 14788 if (TYPE_NAME (type)) 14789 { 14790 if (TREE_DEPRECATED (TYPE_NAME (type))) 14791 return type; 14792 else 14793 { 14794 cp_warn_deprecated_use_scopes (CP_DECL_CONTEXT (TYPE_NAME (type))); 14795 return NULL_TREE; 14796 } 14797 } 14798 14799 /* Do warn about using typedefs to a deprecated class. */ 14800 if (OVERLOAD_TYPE_P (type) && type != TYPE_MAIN_VARIANT (type)) 14801 return type_is_deprecated (TYPE_MAIN_VARIANT (type)); 14802 14803 code = TREE_CODE (type); 14804 14805 if (code == POINTER_TYPE || code == REFERENCE_TYPE 14806 || code == OFFSET_TYPE || code == FUNCTION_TYPE 14807 || code == METHOD_TYPE || code == ARRAY_TYPE) 14808 return type_is_deprecated (TREE_TYPE (type)); 14809 14810 if (TYPE_PTRMEMFUNC_P (type)) 14811 return type_is_deprecated 14812 (TREE_TYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type)))); 14813 14814 return NULL_TREE; 14815 } 14816 14817 /* Returns an unavailable type used within TYPE, or NULL_TREE if none. */ 14818 14819 static tree 14820 type_is_unavailable (tree type) 14821 { 14822 enum tree_code code; 14823 if (TREE_UNAVAILABLE (type)) 14824 return type; 14825 if (TYPE_NAME (type)) 14826 { 14827 if (TREE_UNAVAILABLE (TYPE_NAME (type))) 14828 return type; 14829 else 14830 { 14831 cp_warn_deprecated_use_scopes (CP_DECL_CONTEXT (TYPE_NAME (type))); 14832 return NULL_TREE; 14833 } 14834 } 14835 14836 /* Do warn about using typedefs to a deprecated class. */ 14837 if (OVERLOAD_TYPE_P (type) && type != TYPE_MAIN_VARIANT (type)) 14838 return type_is_deprecated (TYPE_MAIN_VARIANT (type)); 14839 14840 code = TREE_CODE (type); 14841 14842 if (code == POINTER_TYPE || code == REFERENCE_TYPE 14843 || code == OFFSET_TYPE || code == FUNCTION_TYPE 14844 || code == METHOD_TYPE || code == ARRAY_TYPE) 14845 return type_is_unavailable (TREE_TYPE (type)); 14846 14847 if (TYPE_PTRMEMFUNC_P (type)) 14848 return type_is_unavailable 14849 (TREE_TYPE (TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (type)))); 14850 14851 return NULL_TREE; 14852 } 14853 14854 /* Decode the list of parameter types for a function type. 14855 Given the list of things declared inside the parens, 14856 return a list of types. 14857 14858 If this parameter does not end with an ellipsis, we append 14859 void_list_node. 14860 14861 *PARMS is set to the chain of PARM_DECLs created. */ 14862 14863 tree 14864 grokparms (tree parmlist, tree *parms) 14865 { 14866 tree result = NULL_TREE; 14867 tree decls = NULL_TREE; 14868 tree parm; 14869 int any_error = 0; 14870 14871 for (parm = parmlist; parm != NULL_TREE; parm = TREE_CHAIN (parm)) 14872 { 14873 tree type = NULL_TREE; 14874 tree init = TREE_PURPOSE (parm); 14875 tree decl = TREE_VALUE (parm); 14876 14877 if (parm == void_list_node || parm == explicit_void_list_node) 14878 break; 14879 14880 if (! decl || TREE_TYPE (decl) == error_mark_node) 14881 { 14882 any_error = 1; 14883 continue; 14884 } 14885 14886 type = TREE_TYPE (decl); 14887 if (VOID_TYPE_P (type)) 14888 { 14889 if (same_type_p (type, void_type_node) 14890 && !init 14891 && !DECL_NAME (decl) && !result 14892 && TREE_CHAIN (parm) == void_list_node) 14893 /* DR 577: A parameter list consisting of a single 14894 unnamed parameter of non-dependent type 'void'. */ 14895 break; 14896 else if (cv_qualified_p (type)) 14897 error_at (DECL_SOURCE_LOCATION (decl), 14898 "invalid use of cv-qualified type %qT in " 14899 "parameter declaration", type); 14900 else 14901 error_at (DECL_SOURCE_LOCATION (decl), 14902 "invalid use of type %<void%> in parameter " 14903 "declaration"); 14904 /* It's not a good idea to actually create parameters of 14905 type `void'; other parts of the compiler assume that a 14906 void type terminates the parameter list. */ 14907 type = error_mark_node; 14908 TREE_TYPE (decl) = error_mark_node; 14909 } 14910 14911 if (type != error_mark_node) 14912 { 14913 if (deprecated_state != UNAVAILABLE_DEPRECATED_SUPPRESS) 14914 { 14915 tree unavailtype = type_is_unavailable (type); 14916 if (unavailtype) 14917 cp_handle_deprecated_or_unavailable (unavailtype); 14918 } 14919 if (deprecated_state != DEPRECATED_SUPPRESS 14920 && deprecated_state != UNAVAILABLE_DEPRECATED_SUPPRESS) 14921 { 14922 tree deptype = type_is_deprecated (type); 14923 if (deptype) 14924 cp_handle_deprecated_or_unavailable (deptype); 14925 } 14926 14927 /* [dcl.fct] "A parameter with volatile-qualified type is 14928 deprecated." */ 14929 if (CP_TYPE_VOLATILE_P (type)) 14930 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wvolatile, 14931 "%<volatile%>-qualified parameter is " 14932 "deprecated"); 14933 14934 /* Top-level qualifiers on the parameters are 14935 ignored for function types. */ 14936 type = cp_build_qualified_type (type, 0); 14937 if (TREE_CODE (type) == METHOD_TYPE) 14938 { 14939 error ("parameter %qD invalidly declared method type", decl); 14940 type = build_pointer_type (type); 14941 TREE_TYPE (decl) = type; 14942 } 14943 else if (cxx_dialect < cxx17 && INDIRECT_TYPE_P (type)) 14944 { 14945 /* Before C++17 DR 393: 14946 [dcl.fct]/6, parameter types cannot contain pointers 14947 (references) to arrays of unknown bound. */ 14948 tree t = TREE_TYPE (type); 14949 int ptr = TYPE_PTR_P (type); 14950 14951 while (1) 14952 { 14953 if (TYPE_PTR_P (t)) 14954 ptr = 1; 14955 else if (TREE_CODE (t) != ARRAY_TYPE) 14956 break; 14957 else if (!TYPE_DOMAIN (t)) 14958 break; 14959 t = TREE_TYPE (t); 14960 } 14961 if (TREE_CODE (t) == ARRAY_TYPE) 14962 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wpedantic, 14963 ptr 14964 ? G_("parameter %qD includes pointer to array of " 14965 "unknown bound %qT") 14966 : G_("parameter %qD includes reference to array of " 14967 "unknown bound %qT"), 14968 decl, t); 14969 } 14970 14971 if (init && !processing_template_decl) 14972 init = check_default_argument (decl, init, tf_warning_or_error); 14973 } 14974 14975 DECL_CHAIN (decl) = decls; 14976 decls = decl; 14977 result = tree_cons (init, type, result); 14978 } 14979 decls = nreverse (decls); 14980 result = nreverse (result); 14981 if (parm) 14982 result = chainon (result, void_list_node); 14983 *parms = decls; 14984 if (any_error) 14985 result = NULL_TREE; 14986 14987 if (any_error) 14988 /* We had parm errors, recover by giving the function (...) type. */ 14989 result = NULL_TREE; 14990 14991 return result; 14992 } 14993 14994 14995 /* D is a constructor or overloaded `operator='. 14997 14998 Let T be the class in which D is declared. Then, this function 14999 returns: 15000 15001 -1 if D's is an ill-formed constructor or copy assignment operator 15002 whose first parameter is of type `T'. 15003 0 if D is not a copy constructor or copy assignment 15004 operator. 15005 1 if D is a copy constructor or copy assignment operator whose 15006 first parameter is a reference to non-const qualified T. 15007 2 if D is a copy constructor or copy assignment operator whose 15008 first parameter is a reference to const qualified T. 15009 15010 This function can be used as a predicate. Positive values indicate 15011 a copy constructor and nonzero values indicate a copy assignment 15012 operator. */ 15013 15014 int 15015 copy_fn_p (const_tree d) 15016 { 15017 tree args; 15018 tree arg_type; 15019 int result = 1; 15020 15021 gcc_assert (DECL_FUNCTION_MEMBER_P (d)); 15022 15023 if (TREE_CODE (d) == TEMPLATE_DECL 15024 || (DECL_TEMPLATE_INFO (d) 15025 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d)))) 15026 /* Instantiations of template member functions are never copy 15027 functions. Note that member functions of templated classes are 15028 represented as template functions internally, and we must 15029 accept those as copy functions. */ 15030 return 0; 15031 15032 if (!DECL_CONSTRUCTOR_P (d) 15033 && DECL_NAME (d) != assign_op_identifier) 15034 return 0; 15035 15036 args = FUNCTION_FIRST_USER_PARMTYPE (d); 15037 if (!args) 15038 return 0; 15039 15040 arg_type = TREE_VALUE (args); 15041 if (arg_type == error_mark_node) 15042 return 0; 15043 15044 if (TYPE_MAIN_VARIANT (arg_type) == DECL_CONTEXT (d)) 15045 { 15046 /* Pass by value copy assignment operator. */ 15047 result = -1; 15048 } 15049 else if (TYPE_REF_P (arg_type) 15050 && !TYPE_REF_IS_RVALUE (arg_type) 15051 && TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)) == DECL_CONTEXT (d)) 15052 { 15053 if (CP_TYPE_CONST_P (TREE_TYPE (arg_type))) 15054 result = 2; 15055 } 15056 else 15057 return 0; 15058 15059 args = TREE_CHAIN (args); 15060 15061 if (args && args != void_list_node && !TREE_PURPOSE (args)) 15062 /* There are more non-optional args. */ 15063 return 0; 15064 15065 return result; 15066 } 15067 15068 /* D is a constructor or overloaded `operator='. 15069 15070 Let T be the class in which D is declared. Then, this function 15071 returns true when D is a move constructor or move assignment 15072 operator, false otherwise. */ 15073 15074 bool 15075 move_fn_p (const_tree d) 15076 { 15077 if (cxx_dialect == cxx98) 15078 /* There are no move constructors if we are in C++98 mode. */ 15079 return false; 15080 15081 if (TREE_CODE (d) == TEMPLATE_DECL 15082 || (DECL_TEMPLATE_INFO (d) 15083 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (d)))) 15084 /* Instantiations of template member functions are never move 15085 functions. Note that member functions of templated classes are 15086 represented as template functions internally, and we must 15087 accept those as move functions. */ 15088 return 0; 15089 15090 return move_signature_fn_p (d); 15091 } 15092 15093 /* D is a constructor or overloaded `operator='. 15094 15095 Then, this function returns true when D has the same signature as a move 15096 constructor or move assignment operator (because either it is such a 15097 ctor/op= or it is a template specialization with the same signature), 15098 false otherwise. */ 15099 15100 bool 15101 move_signature_fn_p (const_tree d) 15102 { 15103 tree args; 15104 tree arg_type; 15105 bool result = false; 15106 15107 if (!DECL_CONSTRUCTOR_P (d) 15108 && DECL_NAME (d) != assign_op_identifier) 15109 return 0; 15110 15111 args = FUNCTION_FIRST_USER_PARMTYPE (d); 15112 if (!args) 15113 return 0; 15114 15115 arg_type = TREE_VALUE (args); 15116 if (arg_type == error_mark_node) 15117 return 0; 15118 15119 if (TYPE_REF_P (arg_type) 15120 && TYPE_REF_IS_RVALUE (arg_type) 15121 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)), 15122 DECL_CONTEXT (d))) 15123 result = true; 15124 15125 args = TREE_CHAIN (args); 15126 15127 if (args && args != void_list_node && !TREE_PURPOSE (args)) 15128 /* There are more non-optional args. */ 15129 return false; 15130 15131 return result; 15132 } 15133 15134 /* Remember any special properties of member function DECL. */ 15135 15136 void 15137 grok_special_member_properties (tree decl) 15138 { 15139 tree class_type; 15140 15141 if (TREE_CODE (decl) == USING_DECL 15142 || !DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)) 15143 return; 15144 15145 class_type = DECL_CONTEXT (decl); 15146 if (IDENTIFIER_CTOR_P (DECL_NAME (decl))) 15147 { 15148 int ctor = copy_fn_p (decl); 15149 15150 if (!DECL_ARTIFICIAL (decl)) 15151 TYPE_HAS_USER_CONSTRUCTOR (class_type) = 1; 15152 15153 if (ctor > 0) 15154 { 15155 /* [class.copy] 15156 15157 A non-template constructor for class X is a copy 15158 constructor if its first parameter is of type X&, const 15159 X&, volatile X& or const volatile X&, and either there 15160 are no other parameters or else all other parameters have 15161 default arguments. */ 15162 TYPE_HAS_COPY_CTOR (class_type) = 1; 15163 if (ctor > 1) 15164 TYPE_HAS_CONST_COPY_CTOR (class_type) = 1; 15165 } 15166 15167 if (sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (decl))) 15168 TYPE_HAS_DEFAULT_CONSTRUCTOR (class_type) = 1; 15169 15170 if (is_list_ctor (decl)) 15171 TYPE_HAS_LIST_CTOR (class_type) = 1; 15172 15173 if (maybe_constexpr_fn (decl) 15174 && !ctor && !move_fn_p (decl)) 15175 TYPE_HAS_CONSTEXPR_CTOR (class_type) = 1; 15176 } 15177 else if (DECL_NAME (decl) == assign_op_identifier) 15178 { 15179 /* [class.copy] 15180 15181 A non-template assignment operator for class X is a copy 15182 assignment operator if its parameter is of type X, X&, const 15183 X&, volatile X& or const volatile X&. */ 15184 15185 int assop = copy_fn_p (decl); 15186 15187 if (assop) 15188 { 15189 TYPE_HAS_COPY_ASSIGN (class_type) = 1; 15190 if (assop != 1) 15191 TYPE_HAS_CONST_COPY_ASSIGN (class_type) = 1; 15192 } 15193 } 15194 else if (IDENTIFIER_CONV_OP_P (DECL_NAME (decl))) 15195 TYPE_HAS_CONVERSION (class_type) = true; 15196 15197 /* Destructors are handled in check_methods. */ 15198 } 15199 15200 /* Check a constructor DECL has the correct form. Complains 15201 if the class has a constructor of the form X(X). */ 15202 15203 bool 15204 grok_ctor_properties (const_tree ctype, const_tree decl) 15205 { 15206 int ctor_parm = copy_fn_p (decl); 15207 15208 if (ctor_parm < 0) 15209 { 15210 /* [class.copy] 15211 15212 A declaration of a constructor for a class X is ill-formed if 15213 its first parameter is of type (optionally cv-qualified) X 15214 and either there are no other parameters or else all other 15215 parameters have default arguments. 15216 15217 We *don't* complain about member template instantiations that 15218 have this form, though; they can occur as we try to decide 15219 what constructor to use during overload resolution. Since 15220 overload resolution will never prefer such a constructor to 15221 the non-template copy constructor (which is either explicitly 15222 or implicitly defined), there's no need to worry about their 15223 existence. Theoretically, they should never even be 15224 instantiated, but that's hard to forestall. */ 15225 error_at (DECL_SOURCE_LOCATION (decl), 15226 "invalid constructor; you probably meant %<%T (const %T&)%>", 15227 ctype, ctype); 15228 return false; 15229 } 15230 15231 return true; 15232 } 15233 15234 /* DECL is a declaration for an overloaded or conversion operator. If 15235 COMPLAIN is true, errors are issued for invalid declarations. */ 15236 15237 bool 15238 grok_op_properties (tree decl, bool complain) 15239 { 15240 tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl)); 15241 bool methodp = TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE; 15242 tree name = DECL_NAME (decl); 15243 location_t loc = DECL_SOURCE_LOCATION (decl); 15244 15245 tree class_type = DECL_CONTEXT (decl); 15246 if (class_type && !CLASS_TYPE_P (class_type)) 15247 class_type = NULL_TREE; 15248 15249 tree_code operator_code; 15250 unsigned op_flags; 15251 if (IDENTIFIER_CONV_OP_P (name)) 15252 { 15253 /* Conversion operators are TYPE_EXPR for the purposes of this 15254 function. */ 15255 operator_code = TYPE_EXPR; 15256 op_flags = OVL_OP_FLAG_UNARY; 15257 } 15258 else 15259 { 15260 const ovl_op_info_t *ovl_op = IDENTIFIER_OVL_OP_INFO (name); 15261 15262 operator_code = ovl_op->tree_code; 15263 op_flags = ovl_op->flags; 15264 gcc_checking_assert (operator_code != ERROR_MARK); 15265 DECL_OVERLOADED_OPERATOR_CODE_RAW (decl) = ovl_op->ovl_op_code; 15266 } 15267 15268 if (op_flags & OVL_OP_FLAG_ALLOC) 15269 { 15270 /* operator new and operator delete are quite special. */ 15271 if (class_type) 15272 switch (op_flags) 15273 { 15274 case OVL_OP_FLAG_ALLOC: 15275 TYPE_HAS_NEW_OPERATOR (class_type) = 1; 15276 break; 15277 15278 case OVL_OP_FLAG_ALLOC | OVL_OP_FLAG_DELETE: 15279 TYPE_GETS_DELETE (class_type) |= 1; 15280 break; 15281 15282 case OVL_OP_FLAG_ALLOC | OVL_OP_FLAG_VEC: 15283 TYPE_HAS_ARRAY_NEW_OPERATOR (class_type) = 1; 15284 break; 15285 15286 case OVL_OP_FLAG_ALLOC | OVL_OP_FLAG_DELETE | OVL_OP_FLAG_VEC: 15287 TYPE_GETS_DELETE (class_type) |= 2; 15288 break; 15289 15290 default: 15291 gcc_unreachable (); 15292 } 15293 15294 /* [basic.std.dynamic.allocation]/1: 15295 15296 A program is ill-formed if an allocation function is declared 15297 in a namespace scope other than global scope or declared 15298 static in global scope. 15299 15300 The same also holds true for deallocation functions. */ 15301 if (DECL_NAMESPACE_SCOPE_P (decl)) 15302 { 15303 if (CP_DECL_CONTEXT (decl) != global_namespace) 15304 { 15305 error_at (loc, "%qD may not be declared within a namespace", 15306 decl); 15307 return false; 15308 } 15309 15310 if (!TREE_PUBLIC (decl)) 15311 { 15312 error_at (loc, "%qD may not be declared as static", decl); 15313 return false; 15314 } 15315 } 15316 15317 if (op_flags & OVL_OP_FLAG_DELETE) 15318 { 15319 DECL_SET_IS_OPERATOR_DELETE (decl, true); 15320 coerce_delete_type (decl, loc); 15321 } 15322 else 15323 { 15324 DECL_SET_IS_OPERATOR_NEW (decl, true); 15325 TREE_TYPE (decl) = coerce_new_type (TREE_TYPE (decl), loc); 15326 } 15327 15328 return true; 15329 } 15330 15331 /* An operator function must either be a non-static member function 15332 or have at least one parameter of a class, a reference to a class, 15333 an enumeration, or a reference to an enumeration. 13.4.0.6 */ 15334 if (! methodp || DECL_STATIC_FUNCTION_P (decl)) 15335 { 15336 if (operator_code == TYPE_EXPR 15337 || operator_code == CALL_EXPR 15338 || operator_code == COMPONENT_REF 15339 || operator_code == ARRAY_REF 15340 || operator_code == NOP_EXPR) 15341 { 15342 error_at (loc, "%qD must be a non-static member function", decl); 15343 return false; 15344 } 15345 15346 if (DECL_STATIC_FUNCTION_P (decl)) 15347 { 15348 error_at (loc, "%qD must be either a non-static member " 15349 "function or a non-member function", decl); 15350 return false; 15351 } 15352 15353 for (tree arg = argtypes; ; arg = TREE_CHAIN (arg)) 15354 { 15355 if (!arg || arg == void_list_node) 15356 { 15357 if (complain) 15358 error_at(loc, "%qD must have an argument of class or " 15359 "enumerated type", decl); 15360 return false; 15361 } 15362 15363 tree type = non_reference (TREE_VALUE (arg)); 15364 if (type == error_mark_node) 15365 return false; 15366 15367 /* MAYBE_CLASS_TYPE_P, rather than CLASS_TYPE_P, is used 15368 because these checks are performed even on template 15369 functions. */ 15370 if (MAYBE_CLASS_TYPE_P (type) 15371 || TREE_CODE (type) == ENUMERAL_TYPE) 15372 break; 15373 } 15374 } 15375 15376 if (operator_code == CALL_EXPR) 15377 /* There are no further restrictions on the arguments to an overloaded 15378 "operator ()". */ 15379 return true; 15380 15381 if (operator_code == COND_EXPR) 15382 { 15383 /* 13.4.0.3 */ 15384 error_at (loc, "ISO C++ prohibits overloading %<operator ?:%>"); 15385 return false; 15386 } 15387 15388 /* Count the number of arguments and check for ellipsis. */ 15389 int arity = 0; 15390 for (tree arg = argtypes; arg != void_list_node; arg = TREE_CHAIN (arg)) 15391 { 15392 if (!arg) 15393 { 15394 /* Variadic. */ 15395 if (operator_code == ARRAY_REF && cxx_dialect >= cxx23) 15396 break; 15397 15398 error_at (loc, "%qD must not have variable number of arguments", 15399 decl); 15400 return false; 15401 } 15402 ++arity; 15403 } 15404 15405 /* Verify correct number of arguments. */ 15406 switch (op_flags) 15407 { 15408 case OVL_OP_FLAG_AMBIARY: 15409 if (arity == 1) 15410 { 15411 /* We have a unary instance of an ambi-ary op. Remap to the 15412 unary one. */ 15413 unsigned alt = ovl_op_alternate[ovl_op_mapping [operator_code]]; 15414 const ovl_op_info_t *ovl_op = &ovl_op_info[false][alt]; 15415 gcc_checking_assert (ovl_op->flags == OVL_OP_FLAG_UNARY); 15416 operator_code = ovl_op->tree_code; 15417 DECL_OVERLOADED_OPERATOR_CODE_RAW (decl) = ovl_op->ovl_op_code; 15418 } 15419 else if (arity != 2) 15420 { 15421 /* This was an ambiguous operator but is invalid. */ 15422 error_at (loc, 15423 methodp 15424 ? G_("%qD must have either zero or one argument") 15425 : G_("%qD must have either one or two arguments"), decl); 15426 return false; 15427 } 15428 else if ((operator_code == POSTINCREMENT_EXPR 15429 || operator_code == POSTDECREMENT_EXPR) 15430 && ! processing_template_decl 15431 /* x++ and x--'s second argument must be an int. */ 15432 && ! same_type_p (TREE_VALUE (TREE_CHAIN (argtypes)), 15433 integer_type_node)) 15434 { 15435 error_at (loc, 15436 methodp 15437 ? G_("postfix %qD must have %<int%> as its argument") 15438 : G_("postfix %qD must have %<int%> as its second argument"), 15439 decl); 15440 return false; 15441 } 15442 break; 15443 15444 case OVL_OP_FLAG_UNARY: 15445 if (arity != 1) 15446 { 15447 error_at (loc, 15448 methodp 15449 ? G_("%qD must have no arguments") 15450 : G_("%qD must have exactly one argument"), decl); 15451 return false; 15452 } 15453 break; 15454 15455 case OVL_OP_FLAG_BINARY: 15456 if (arity != 2) 15457 { 15458 if (operator_code == ARRAY_REF && cxx_dialect >= cxx23) 15459 break; 15460 error_at (loc, 15461 methodp 15462 ? G_("%qD must have exactly one argument") 15463 : G_("%qD must have exactly two arguments"), decl); 15464 return false; 15465 } 15466 break; 15467 15468 default: 15469 gcc_unreachable (); 15470 } 15471 15472 /* There can be no default arguments. */ 15473 for (tree arg = argtypes; arg && arg != void_list_node; 15474 arg = TREE_CHAIN (arg)) 15475 if (TREE_PURPOSE (arg)) 15476 { 15477 TREE_PURPOSE (arg) = NULL_TREE; 15478 error_at (loc, "%qD cannot have default arguments", decl); 15479 return false; 15480 } 15481 15482 /* At this point the declaration is well-formed. It may not be 15483 sensible though. */ 15484 15485 /* Check member function warnings only on the in-class declaration. 15486 There's no point warning on an out-of-class definition. */ 15487 if (class_type && class_type != current_class_type) 15488 return true; 15489 15490 /* Warn about conversion operators that will never be used. */ 15491 if (IDENTIFIER_CONV_OP_P (name) 15492 && ! DECL_TEMPLATE_INFO (decl) 15493 && warn_class_conversion) 15494 { 15495 tree t = TREE_TYPE (name); 15496 int ref = TYPE_REF_P (t); 15497 15498 if (ref) 15499 t = TYPE_MAIN_VARIANT (TREE_TYPE (t)); 15500 15501 if (VOID_TYPE_P (t)) 15502 warning_at (loc, OPT_Wclass_conversion, "converting %qT to %<void%> " 15503 "will never use a type conversion operator", class_type); 15504 else if (class_type) 15505 { 15506 if (same_type_ignoring_top_level_qualifiers_p (t, class_type)) 15507 warning_at (loc, OPT_Wclass_conversion, 15508 ref 15509 ? G_("converting %qT to a reference to the same type " 15510 "will never use a type conversion operator") 15511 : G_("converting %qT to the same type " 15512 "will never use a type conversion operator"), 15513 class_type); 15514 /* Don't force t to be complete here. */ 15515 else if (MAYBE_CLASS_TYPE_P (t) 15516 && COMPLETE_TYPE_P (t) 15517 && DERIVED_FROM_P (t, class_type)) 15518 warning_at (loc, OPT_Wclass_conversion, 15519 ref 15520 ? G_("converting %qT to a reference to a base class " 15521 "%qT will never use a type conversion operator") 15522 : G_("converting %qT to a base class %qT " 15523 "will never use a type conversion operator"), 15524 class_type, t); 15525 } 15526 } 15527 15528 if (!warn_ecpp) 15529 return true; 15530 15531 /* Effective C++ rules below. */ 15532 15533 /* More Effective C++ rule 7. */ 15534 if (operator_code == TRUTH_ANDIF_EXPR 15535 || operator_code == TRUTH_ORIF_EXPR 15536 || operator_code == COMPOUND_EXPR) 15537 warning_at (loc, OPT_Weffc__, 15538 "user-defined %qD always evaluates both arguments", decl); 15539 15540 /* More Effective C++ rule 6. */ 15541 if (operator_code == POSTINCREMENT_EXPR 15542 || operator_code == POSTDECREMENT_EXPR 15543 || operator_code == PREINCREMENT_EXPR 15544 || operator_code == PREDECREMENT_EXPR) 15545 { 15546 tree arg = TREE_VALUE (argtypes); 15547 tree ret = TREE_TYPE (TREE_TYPE (decl)); 15548 if (methodp || TYPE_REF_P (arg)) 15549 arg = TREE_TYPE (arg); 15550 arg = TYPE_MAIN_VARIANT (arg); 15551 15552 if (operator_code == PREINCREMENT_EXPR 15553 || operator_code == PREDECREMENT_EXPR) 15554 { 15555 if (!TYPE_REF_P (ret) 15556 || !same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (ret)), arg)) 15557 warning_at (loc, OPT_Weffc__, "prefix %qD should return %qT", decl, 15558 build_reference_type (arg)); 15559 } 15560 else 15561 { 15562 if (!same_type_p (TYPE_MAIN_VARIANT (ret), arg)) 15563 warning_at (loc, OPT_Weffc__, 15564 "postfix %qD should return %qT", decl, arg); 15565 } 15566 } 15567 15568 /* Effective C++ rule 23. */ 15569 if (!DECL_ASSIGNMENT_OPERATOR_P (decl) 15570 && (operator_code == PLUS_EXPR 15571 || operator_code == MINUS_EXPR 15572 || operator_code == TRUNC_DIV_EXPR 15573 || operator_code == MULT_EXPR 15574 || operator_code == TRUNC_MOD_EXPR) 15575 && TYPE_REF_P (TREE_TYPE (TREE_TYPE (decl)))) 15576 warning_at (loc, OPT_Weffc__, "%qD should return by value", decl); 15577 15578 return true; 15579 } 15580 15581 /* Return a string giving the keyword associate with CODE. */ 15583 15584 static const char * 15585 tag_name (enum tag_types code) 15586 { 15587 switch (code) 15588 { 15589 case record_type: 15590 return "struct"; 15591 case class_type: 15592 return "class"; 15593 case union_type: 15594 return "union"; 15595 case enum_type: 15596 return "enum"; 15597 case typename_type: 15598 return "typename"; 15599 default: 15600 gcc_unreachable (); 15601 } 15602 } 15603 15604 /* Name lookup in an elaborated-type-specifier (after the keyword 15605 indicated by TAG_CODE) has found the TYPE_DECL DECL. If the 15606 elaborated-type-specifier is invalid, issue a diagnostic and return 15607 error_mark_node; otherwise, return the *_TYPE to which it referred. 15608 If ALLOW_TEMPLATE_P is true, TYPE may be a class template. */ 15609 15610 tree 15611 check_elaborated_type_specifier (enum tag_types tag_code, 15612 tree decl, 15613 bool allow_template_p) 15614 { 15615 tree type; 15616 15617 /* In the case of: 15618 15619 struct S { struct S *p; }; 15620 15621 name lookup will find the TYPE_DECL for the implicit "S::S" 15622 typedef. Adjust for that here. */ 15623 if (DECL_SELF_REFERENCE_P (decl)) 15624 decl = TYPE_NAME (TREE_TYPE (decl)); 15625 15626 type = TREE_TYPE (decl); 15627 15628 /* Check TEMPLATE_TYPE_PARM first because DECL_IMPLICIT_TYPEDEF_P 15629 is false for this case as well. */ 15630 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM) 15631 { 15632 error ("using template type parameter %qT after %qs", 15633 type, tag_name (tag_code)); 15634 return error_mark_node; 15635 } 15636 /* Accept template template parameters. */ 15637 else if (allow_template_p 15638 && (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM 15639 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)) 15640 ; 15641 /* [dcl.type.elab] 15642 15643 If the identifier resolves to a typedef-name or the 15644 simple-template-id resolves to an alias template 15645 specialization, the elaborated-type-specifier is ill-formed. 15646 15647 In other words, the only legitimate declaration to use in the 15648 elaborated type specifier is the implicit typedef created when 15649 the type is declared. */ 15650 else if (!DECL_IMPLICIT_TYPEDEF_P (decl) 15651 && !DECL_SELF_REFERENCE_P (decl) 15652 && tag_code != typename_type) 15653 { 15654 if (alias_template_specialization_p (type, nt_opaque)) 15655 error ("using alias template specialization %qT after %qs", 15656 type, tag_name (tag_code)); 15657 else 15658 error ("using typedef-name %qD after %qs", decl, tag_name (tag_code)); 15659 inform (DECL_SOURCE_LOCATION (decl), 15660 "%qD has a previous declaration here", decl); 15661 return error_mark_node; 15662 } 15663 else if (TREE_CODE (type) != RECORD_TYPE 15664 && TREE_CODE (type) != UNION_TYPE 15665 && tag_code != enum_type 15666 && tag_code != typename_type) 15667 { 15668 error ("%qT referred to as %qs", type, tag_name (tag_code)); 15669 inform (location_of (type), "%qT has a previous declaration here", type); 15670 return error_mark_node; 15671 } 15672 else if (TREE_CODE (type) != ENUMERAL_TYPE 15673 && tag_code == enum_type) 15674 { 15675 error ("%qT referred to as enum", type); 15676 inform (location_of (type), "%qT has a previous declaration here", type); 15677 return error_mark_node; 15678 } 15679 else if (!allow_template_p 15680 && TREE_CODE (type) == RECORD_TYPE 15681 && CLASSTYPE_IS_TEMPLATE (type)) 15682 { 15683 /* If a class template appears as elaborated type specifier 15684 without a template header such as: 15685 15686 template <class T> class C {}; 15687 void f(class C); // No template header here 15688 15689 then the required template argument is missing. */ 15690 error ("template argument required for %<%s %T%>", 15691 tag_name (tag_code), 15692 DECL_NAME (CLASSTYPE_TI_TEMPLATE (type))); 15693 return error_mark_node; 15694 } 15695 15696 return type; 15697 } 15698 15699 /* Lookup NAME of an elaborated type specifier according to SCOPE and 15700 issue diagnostics if necessary. Return *_TYPE node upon success, 15701 NULL_TREE when the NAME is not found, and ERROR_MARK_NODE for type 15702 error. */ 15703 15704 static tree 15705 lookup_and_check_tag (enum tag_types tag_code, tree name, 15706 TAG_how how, bool template_header_p) 15707 { 15708 tree decl; 15709 if (how == TAG_how::GLOBAL) 15710 { 15711 /* First try ordinary name lookup, ignoring hidden class name 15712 injected via friend declaration. */ 15713 decl = lookup_name (name, LOOK_want::TYPE); 15714 decl = strip_using_decl (decl); 15715 /* If that fails, the name will be placed in the smallest 15716 non-class, non-function-prototype scope according to 3.3.1/5. 15717 We may already have a hidden name declared as friend in this 15718 scope. So lookup again but not ignoring hidden names. 15719 If we find one, that name will be made visible rather than 15720 creating a new tag. */ 15721 if (!decl) 15722 decl = lookup_elaborated_type (name, TAG_how::INNERMOST_NON_CLASS); 15723 } 15724 else 15725 decl = lookup_elaborated_type (name, how); 15726 15727 if (!decl) 15728 /* We found nothing. */ 15729 return NULL_TREE; 15730 15731 if (TREE_CODE (decl) == TREE_LIST) 15732 { 15733 error ("reference to %qD is ambiguous", name); 15734 print_candidates (decl); 15735 return error_mark_node; 15736 } 15737 15738 if (DECL_CLASS_TEMPLATE_P (decl) 15739 && !template_header_p 15740 && how == TAG_how::CURRENT_ONLY) 15741 { 15742 error ("class template %qD redeclared as non-template", name); 15743 inform (location_of (decl), "previous declaration here"); 15744 CLASSTYPE_ERRONEOUS (TREE_TYPE (decl)) = true; 15745 return error_mark_node; 15746 } 15747 15748 if (DECL_CLASS_TEMPLATE_P (decl) 15749 /* If scope is TAG_how::CURRENT_ONLY we're defining a class, 15750 so ignore a template template parameter. */ 15751 || (how != TAG_how::CURRENT_ONLY && DECL_TEMPLATE_TEMPLATE_PARM_P (decl))) 15752 decl = DECL_TEMPLATE_RESULT (decl); 15753 15754 if (TREE_CODE (decl) != TYPE_DECL) 15755 /* Found not-a-type. */ 15756 return NULL_TREE; 15757 15758 /* Look for invalid nested type: 15759 class C { 15760 class C {}; 15761 }; */ 15762 if (how == TAG_how::CURRENT_ONLY && DECL_SELF_REFERENCE_P (decl)) 15763 { 15764 error ("%qD has the same name as the class in which it is " 15765 "declared", decl); 15766 return error_mark_node; 15767 } 15768 15769 /* Two cases we need to consider when deciding if a class 15770 template is allowed as an elaborated type specifier: 15771 1. It is a self reference to its own class. 15772 2. It comes with a template header. 15773 15774 For example: 15775 15776 template <class T> class C { 15777 class C *c1; // DECL_SELF_REFERENCE_P is true 15778 class D; 15779 }; 15780 template <class U> class C; // template_header_p is true 15781 template <class T> class C<T>::D { 15782 class C *c2; // DECL_SELF_REFERENCE_P is true 15783 }; */ 15784 15785 tree t = check_elaborated_type_specifier (tag_code, decl, 15786 template_header_p 15787 | DECL_SELF_REFERENCE_P (decl)); 15788 if (template_header_p && t && CLASS_TYPE_P (t) 15789 && (!CLASSTYPE_TEMPLATE_INFO (t) 15790 || (!PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t))))) 15791 { 15792 error ("%qT is not a template", t); 15793 inform (location_of (t), "previous declaration here"); 15794 if (TYPE_CLASS_SCOPE_P (t) 15795 && CLASSTYPE_TEMPLATE_INFO (TYPE_CONTEXT (t))) 15796 inform (input_location, 15797 "perhaps you want to explicitly add %<%T::%>", 15798 TYPE_CONTEXT (t)); 15799 return error_mark_node; 15800 } 15801 15802 return t; 15803 } 15804 15805 /* Get the struct, enum or union (TAG_CODE says which) with tag NAME. 15806 Define the tag as a forward-reference if it is not defined. 15807 15808 If a declaration is given, process it here, and report an error if 15809 multiple declarations are not identical. 15810 15811 SCOPE is TS_CURRENT when this is also a definition. Only look in 15812 the current frame for the name (since C++ allows new names in any 15813 scope.) It is TS_WITHIN_ENCLOSING_NON_CLASS if this is a friend 15814 declaration. Only look beginning from the current scope outward up 15815 till the nearest non-class scope. Otherwise it is TS_GLOBAL. 15816 15817 TEMPLATE_HEADER_P is true when this declaration is preceded by 15818 a set of template parameters. */ 15819 15820 tree 15821 xref_tag (enum tag_types tag_code, tree name, 15822 TAG_how how, bool template_header_p) 15823 { 15824 enum tree_code code; 15825 tree context = NULL_TREE; 15826 15827 auto_cond_timevar tv (TV_NAME_LOOKUP); 15828 15829 gcc_assert (identifier_p (name)); 15830 15831 switch (tag_code) 15832 { 15833 case record_type: 15834 case class_type: 15835 code = RECORD_TYPE; 15836 break; 15837 case union_type: 15838 code = UNION_TYPE; 15839 break; 15840 case enum_type: 15841 code = ENUMERAL_TYPE; 15842 break; 15843 default: 15844 gcc_unreachable (); 15845 } 15846 15847 /* In case of anonymous name, xref_tag is only called to 15848 make type node and push name. Name lookup is not required. */ 15849 tree t = NULL_TREE; 15850 if (!IDENTIFIER_ANON_P (name)) 15851 t = lookup_and_check_tag (tag_code, name, how, template_header_p); 15852 15853 if (t == error_mark_node) 15854 return error_mark_node; 15855 15856 if (how != TAG_how::CURRENT_ONLY && t && current_class_type 15857 && template_class_depth (current_class_type) 15858 && template_header_p) 15859 { 15860 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM) 15861 return t; 15862 15863 /* Since HOW is not TAG_how::CURRENT_ONLY, we are not looking at 15864 a definition of this tag. Since, in addition, we are 15865 currently processing a (member) template declaration of a 15866 template class, we must be very careful; consider: 15867 15868 template <class X> struct S1 15869 15870 template <class U> struct S2 15871 { 15872 template <class V> friend struct S1; 15873 }; 15874 15875 Here, the S2::S1 declaration should not be confused with the 15876 outer declaration. In particular, the inner version should 15877 have a template parameter of level 2, not level 1. 15878 15879 On the other hand, when presented with: 15880 15881 template <class T> struct S1 15882 { 15883 template <class U> struct S2 {}; 15884 template <class U> friend struct S2; 15885 }; 15886 15887 the friend must find S1::S2 eventually. We accomplish this 15888 by making sure that the new type we create to represent this 15889 declaration has the right TYPE_CONTEXT. */ 15890 context = TYPE_CONTEXT (t); 15891 t = NULL_TREE; 15892 } 15893 15894 if (! t) 15895 { 15896 /* If no such tag is yet defined, create a forward-reference node 15897 and record it as the "definition". 15898 When a real declaration of this type is found, 15899 the forward-reference will be altered into a real type. */ 15900 if (code == ENUMERAL_TYPE) 15901 { 15902 error ("use of enum %q#D without previous declaration", name); 15903 return error_mark_node; 15904 } 15905 15906 t = make_class_type (code); 15907 TYPE_CONTEXT (t) = context; 15908 if (IDENTIFIER_LAMBDA_P (name)) 15909 /* Mark it as a lambda type right now. Our caller will 15910 correct the value. */ 15911 CLASSTYPE_LAMBDA_EXPR (t) = error_mark_node; 15912 t = pushtag (name, t, how); 15913 } 15914 else 15915 { 15916 if (template_header_p && MAYBE_CLASS_TYPE_P (t)) 15917 { 15918 /* Check that we aren't trying to overload a class with different 15919 constraints. */ 15920 tree constr = NULL_TREE; 15921 if (current_template_parms) 15922 { 15923 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms); 15924 constr = build_constraints (reqs, NULL_TREE); 15925 } 15926 if (!redeclare_class_template (t, current_template_parms, constr)) 15927 return error_mark_node; 15928 } 15929 else if (!processing_template_decl 15930 && CLASS_TYPE_P (t) 15931 && CLASSTYPE_IS_TEMPLATE (t)) 15932 { 15933 error ("redeclaration of %qT as a non-template", t); 15934 inform (location_of (t), "previous declaration %qD", t); 15935 return error_mark_node; 15936 } 15937 15938 if (modules_p () 15939 && how == TAG_how::CURRENT_ONLY) 15940 { 15941 tree decl = TYPE_NAME (t); 15942 if (!module_may_redeclare (decl)) 15943 { 15944 error ("cannot declare %qD in a different module", decl); 15945 inform (DECL_SOURCE_LOCATION (decl), "declared here"); 15946 return error_mark_node; 15947 } 15948 15949 tree maybe_tmpl = decl; 15950 if (CLASS_TYPE_P (t) && CLASSTYPE_IS_TEMPLATE (t)) 15951 maybe_tmpl = CLASSTYPE_TI_TEMPLATE (t); 15952 15953 if (DECL_LANG_SPECIFIC (decl) 15954 && DECL_MODULE_IMPORT_P (decl) 15955 && TREE_CODE (CP_DECL_CONTEXT (decl)) == NAMESPACE_DECL) 15956 { 15957 /* Push it into this TU's symbol slot. */ 15958 gcc_checking_assert (current_namespace == CP_DECL_CONTEXT (decl)); 15959 if (maybe_tmpl != decl) 15960 /* We're in the template parm binding level. 15961 Pushtag has logic to slide under that, but we're 15962 not pushing a *new* type. */ 15963 push_nested_namespace (CP_DECL_CONTEXT (decl)); 15964 15965 pushdecl (maybe_tmpl); 15966 if (maybe_tmpl != decl) 15967 pop_nested_namespace (CP_DECL_CONTEXT (decl)); 15968 } 15969 15970 set_instantiating_module (maybe_tmpl); 15971 } 15972 } 15973 15974 return t; 15975 } 15976 15977 /* Create the binfo hierarchy for REF with (possibly NULL) base list 15978 BASE_LIST. For each element on BASE_LIST the TREE_PURPOSE is an 15979 access_* node, and the TREE_VALUE is the type of the base-class. 15980 Non-NULL TREE_TYPE indicates virtual inheritance. */ 15981 15982 void 15983 xref_basetypes (tree ref, tree base_list) 15984 { 15985 tree *basep; 15986 tree binfo, base_binfo; 15987 unsigned max_vbases = 0; /* Maximum direct & indirect virtual bases. */ 15988 unsigned max_bases = 0; /* Maximum direct bases. */ 15989 unsigned max_dvbases = 0; /* Maximum direct virtual bases. */ 15990 int i; 15991 tree default_access; 15992 tree igo_prev; /* Track Inheritance Graph Order. */ 15993 15994 if (ref == error_mark_node) 15995 return; 15996 15997 /* The base of a derived class is private by default, all others are 15998 public. */ 15999 default_access = (TREE_CODE (ref) == RECORD_TYPE 16000 && CLASSTYPE_DECLARED_CLASS (ref) 16001 ? access_private_node : access_public_node); 16002 16003 /* First, make sure that any templates in base-classes are 16004 instantiated. This ensures that if we call ourselves recursively 16005 we do not get confused about which classes are marked and which 16006 are not. */ 16007 basep = &base_list; 16008 while (*basep) 16009 { 16010 tree basetype = TREE_VALUE (*basep); 16011 16012 /* The dependent_type_p call below should really be dependent_scope_p 16013 so that we give a hard error about using an incomplete type as a 16014 base, but we allow it with a pedwarn for backward 16015 compatibility. */ 16016 if (processing_template_decl 16017 && CLASS_TYPE_P (basetype) && TYPE_BEING_DEFINED (basetype)) 16018 cxx_incomplete_type_diagnostic (NULL_TREE, basetype, DK_PEDWARN); 16019 if (!dependent_type_p (basetype) 16020 && !complete_type_or_else (basetype, NULL)) 16021 /* An incomplete type. Remove it from the list. */ 16022 *basep = TREE_CHAIN (*basep); 16023 else 16024 { 16025 max_bases++; 16026 if (TREE_TYPE (*basep)) 16027 max_dvbases++; 16028 if (CLASS_TYPE_P (basetype)) 16029 max_vbases += vec_safe_length (CLASSTYPE_VBASECLASSES (basetype)); 16030 basep = &TREE_CHAIN (*basep); 16031 } 16032 } 16033 max_vbases += max_dvbases; 16034 16035 TYPE_MARKED_P (ref) = 1; 16036 16037 /* The binfo slot should be empty, unless this is an (ill-formed) 16038 redefinition. */ 16039 gcc_assert (!TYPE_BINFO (ref) || TYPE_SIZE (ref)); 16040 16041 gcc_assert (TYPE_MAIN_VARIANT (ref) == ref); 16042 16043 binfo = make_tree_binfo (max_bases); 16044 16045 TYPE_BINFO (ref) = binfo; 16046 BINFO_OFFSET (binfo) = size_zero_node; 16047 BINFO_TYPE (binfo) = ref; 16048 16049 /* Apply base-class info set up to the variants of this type. */ 16050 fixup_type_variants (ref); 16051 16052 if (max_bases) 16053 { 16054 vec_alloc (BINFO_BASE_ACCESSES (binfo), max_bases); 16055 /* A C++98 POD cannot have base classes. */ 16056 CLASSTYPE_NON_LAYOUT_POD_P (ref) = true; 16057 16058 if (TREE_CODE (ref) == UNION_TYPE) 16059 { 16060 error ("derived union %qT invalid", ref); 16061 return; 16062 } 16063 } 16064 16065 if (max_bases > 1) 16066 warning (OPT_Wmultiple_inheritance, 16067 "%qT defined with multiple direct bases", ref); 16068 16069 if (max_vbases) 16070 { 16071 /* An aggregate can't have virtual base classes. */ 16072 CLASSTYPE_NON_AGGREGATE (ref) = true; 16073 16074 vec_alloc (CLASSTYPE_VBASECLASSES (ref), max_vbases); 16075 16076 if (max_dvbases) 16077 warning (OPT_Wvirtual_inheritance, 16078 "%qT defined with direct virtual base", ref); 16079 } 16080 16081 for (igo_prev = binfo; base_list; base_list = TREE_CHAIN (base_list)) 16082 { 16083 tree access = TREE_PURPOSE (base_list); 16084 int via_virtual = TREE_TYPE (base_list) != NULL_TREE; 16085 tree basetype = TREE_VALUE (base_list); 16086 16087 if (access == access_default_node) 16088 access = default_access; 16089 16090 /* Before C++17, an aggregate cannot have base classes. In C++17, an 16091 aggregate can't have virtual, private, or protected base classes. */ 16092 if (cxx_dialect < cxx17 16093 || access != access_public_node 16094 || via_virtual) 16095 CLASSTYPE_NON_AGGREGATE (ref) = true; 16096 16097 if (PACK_EXPANSION_P (basetype)) 16098 basetype = PACK_EXPANSION_PATTERN (basetype); 16099 if (TREE_CODE (basetype) == TYPE_DECL) 16100 basetype = TREE_TYPE (basetype); 16101 if (!MAYBE_CLASS_TYPE_P (basetype) || TREE_CODE (basetype) == UNION_TYPE) 16102 { 16103 error ("base type %qT fails to be a struct or class type", 16104 basetype); 16105 goto dropped_base; 16106 } 16107 16108 base_binfo = NULL_TREE; 16109 if (CLASS_TYPE_P (basetype) && !dependent_scope_p (basetype)) 16110 { 16111 base_binfo = TYPE_BINFO (basetype); 16112 /* The original basetype could have been a typedef'd type. */ 16113 basetype = BINFO_TYPE (base_binfo); 16114 16115 /* Inherit flags from the base. */ 16116 TYPE_HAS_NEW_OPERATOR (ref) 16117 |= TYPE_HAS_NEW_OPERATOR (basetype); 16118 TYPE_HAS_ARRAY_NEW_OPERATOR (ref) 16119 |= TYPE_HAS_ARRAY_NEW_OPERATOR (basetype); 16120 TYPE_GETS_DELETE (ref) |= TYPE_GETS_DELETE (basetype); 16121 TYPE_HAS_CONVERSION (ref) |= TYPE_HAS_CONVERSION (basetype); 16122 CLASSTYPE_DIAMOND_SHAPED_P (ref) 16123 |= CLASSTYPE_DIAMOND_SHAPED_P (basetype); 16124 CLASSTYPE_REPEATED_BASE_P (ref) 16125 |= CLASSTYPE_REPEATED_BASE_P (basetype); 16126 } 16127 16128 /* We must do this test after we've seen through a typedef 16129 type. */ 16130 if (TYPE_MARKED_P (basetype)) 16131 { 16132 if (basetype == ref) 16133 error ("recursive type %qT undefined", basetype); 16134 else 16135 error ("duplicate base type %qT invalid", basetype); 16136 goto dropped_base; 16137 } 16138 16139 if (PACK_EXPANSION_P (TREE_VALUE (base_list))) 16140 /* Regenerate the pack expansion for the bases. */ 16141 basetype = make_pack_expansion (basetype); 16142 16143 TYPE_MARKED_P (basetype) = 1; 16144 16145 base_binfo = copy_binfo (base_binfo, basetype, ref, 16146 &igo_prev, via_virtual); 16147 if (!BINFO_INHERITANCE_CHAIN (base_binfo)) 16148 BINFO_INHERITANCE_CHAIN (base_binfo) = binfo; 16149 16150 BINFO_BASE_APPEND (binfo, base_binfo); 16151 BINFO_BASE_ACCESS_APPEND (binfo, access); 16152 continue; 16153 16154 dropped_base: 16155 /* Update max_vbases to reflect the reality that we are dropping 16156 this base: if it reaches zero we want to undo the vec_alloc 16157 above to avoid inconsistencies during error-recovery: eg, in 16158 build_special_member_call, CLASSTYPE_VBASECLASSES non null 16159 and vtt null (c++/27952). */ 16160 if (via_virtual) 16161 max_vbases--; 16162 if (CLASS_TYPE_P (basetype)) 16163 max_vbases 16164 -= vec_safe_length (CLASSTYPE_VBASECLASSES (basetype)); 16165 } 16166 16167 if (CLASSTYPE_VBASECLASSES (ref) 16168 && max_vbases == 0) 16169 vec_free (CLASSTYPE_VBASECLASSES (ref)); 16170 16171 if (vec_safe_length (CLASSTYPE_VBASECLASSES (ref)) < max_vbases) 16172 /* If we didn't get max_vbases vbases, we must have shared at 16173 least one of them, and are therefore diamond shaped. */ 16174 CLASSTYPE_DIAMOND_SHAPED_P (ref) = 1; 16175 16176 /* Unmark all the types. */ 16177 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++) 16178 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 0; 16179 TYPE_MARKED_P (ref) = 0; 16180 16181 /* Now see if we have a repeated base type. */ 16182 if (!CLASSTYPE_REPEATED_BASE_P (ref)) 16183 { 16184 for (base_binfo = binfo; base_binfo; 16185 base_binfo = TREE_CHAIN (base_binfo)) 16186 { 16187 if (TYPE_MARKED_P (BINFO_TYPE (base_binfo))) 16188 { 16189 CLASSTYPE_REPEATED_BASE_P (ref) = 1; 16190 break; 16191 } 16192 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 1; 16193 } 16194 for (base_binfo = binfo; base_binfo; 16195 base_binfo = TREE_CHAIN (base_binfo)) 16196 if (TYPE_MARKED_P (BINFO_TYPE (base_binfo))) 16197 TYPE_MARKED_P (BINFO_TYPE (base_binfo)) = 0; 16198 else 16199 break; 16200 } 16201 } 16202 16203 16204 /* Copies the enum-related properties from type SRC to type DST. 16206 Used with the underlying type of an enum and the enum itself. */ 16207 static void 16208 copy_type_enum (tree dst, tree src) 16209 { 16210 tree t; 16211 for (t = dst; t; t = TYPE_NEXT_VARIANT (t)) 16212 { 16213 TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (src); 16214 TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (src); 16215 TYPE_SIZE (t) = TYPE_SIZE (src); 16216 TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (src); 16217 SET_TYPE_MODE (dst, TYPE_MODE (src)); 16218 TYPE_PRECISION (t) = TYPE_PRECISION (src); 16219 unsigned valign = TYPE_ALIGN (src); 16220 if (TYPE_USER_ALIGN (t)) 16221 valign = MAX (valign, TYPE_ALIGN (t)); 16222 else 16223 TYPE_USER_ALIGN (t) = TYPE_USER_ALIGN (src); 16224 SET_TYPE_ALIGN (t, valign); 16225 TYPE_UNSIGNED (t) = TYPE_UNSIGNED (src); 16226 } 16227 } 16228 16229 /* Begin compiling the definition of an enumeration type. 16230 NAME is its name, 16231 16232 if ENUMTYPE is not NULL_TREE then the type has alredy been found. 16233 16234 UNDERLYING_TYPE is the type that will be used as the storage for 16235 the enumeration type. This should be NULL_TREE if no storage type 16236 was specified. 16237 16238 ATTRIBUTES are any attributes specified after the enum-key. 16239 16240 SCOPED_ENUM_P is true if this is a scoped enumeration type. 16241 16242 if IS_NEW is not NULL, gets TRUE iff a new type is created. 16243 16244 Returns the type object, as yet incomplete. 16245 Also records info about it so that build_enumerator 16246 may be used to declare the individual values as they are read. */ 16247 16248 tree 16249 start_enum (tree name, tree enumtype, tree underlying_type, 16250 tree attributes, bool scoped_enum_p, bool *is_new) 16251 { 16252 tree prevtype = NULL_TREE; 16253 gcc_assert (identifier_p (name)); 16254 16255 if (is_new) 16256 *is_new = false; 16257 /* [C++0x dcl.enum]p5: 16258 16259 If not explicitly specified, the underlying type of a scoped 16260 enumeration type is int. */ 16261 if (!underlying_type && scoped_enum_p) 16262 underlying_type = integer_type_node; 16263 16264 if (underlying_type) 16265 underlying_type = cv_unqualified (underlying_type); 16266 16267 /* If this is the real definition for a previous forward reference, 16268 fill in the contents in the same object that used to be the 16269 forward reference. */ 16270 if (!enumtype) 16271 enumtype = lookup_and_check_tag (enum_type, name, 16272 /*tag_scope=*/TAG_how::CURRENT_ONLY, 16273 /*template_header_p=*/false); 16274 16275 /* In case of a template_decl, the only check that should be deferred 16276 to instantiation time is the comparison of underlying types. */ 16277 if (enumtype && TREE_CODE (enumtype) == ENUMERAL_TYPE) 16278 { 16279 if (scoped_enum_p != SCOPED_ENUM_P (enumtype)) 16280 { 16281 error_at (input_location, "scoped/unscoped mismatch " 16282 "in enum %q#T", enumtype); 16283 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)), 16284 "previous definition here"); 16285 enumtype = error_mark_node; 16286 } 16287 else if (ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) != !! underlying_type) 16288 { 16289 error_at (input_location, "underlying type mismatch " 16290 "in enum %q#T", enumtype); 16291 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)), 16292 "previous definition here"); 16293 enumtype = error_mark_node; 16294 } 16295 else if (underlying_type && ENUM_UNDERLYING_TYPE (enumtype) 16296 && !same_type_p (underlying_type, 16297 ENUM_UNDERLYING_TYPE (enumtype))) 16298 { 16299 error_at (input_location, "different underlying type " 16300 "in enum %q#T", enumtype); 16301 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)), 16302 "previous definition here"); 16303 underlying_type = NULL_TREE; 16304 } 16305 16306 if (modules_p ()) 16307 { 16308 if (!module_may_redeclare (TYPE_NAME (enumtype))) 16309 { 16310 error ("cannot define %qD in different module", 16311 TYPE_NAME (enumtype)); 16312 inform (DECL_SOURCE_LOCATION (TYPE_NAME (enumtype)), 16313 "declared here"); 16314 enumtype = error_mark_node; 16315 } 16316 set_instantiating_module (TYPE_NAME (enumtype)); 16317 } 16318 } 16319 16320 if (!enumtype || TREE_CODE (enumtype) != ENUMERAL_TYPE 16321 || processing_template_decl) 16322 { 16323 /* In case of error, make a dummy enum to allow parsing to 16324 continue. */ 16325 if (enumtype == error_mark_node) 16326 { 16327 name = make_anon_name (); 16328 enumtype = NULL_TREE; 16329 } 16330 16331 /* enumtype may be an ENUMERAL_TYPE if this is a redefinition 16332 of an opaque enum, or an opaque enum of an already defined 16333 enumeration (C++11). 16334 In any other case, it'll be NULL_TREE. */ 16335 if (!enumtype) 16336 { 16337 if (is_new) 16338 *is_new = true; 16339 } 16340 prevtype = enumtype; 16341 16342 /* Do not push the decl more than once. */ 16343 if (!enumtype 16344 || TREE_CODE (enumtype) != ENUMERAL_TYPE) 16345 { 16346 enumtype = cxx_make_type (ENUMERAL_TYPE); 16347 enumtype = pushtag (name, enumtype); 16348 16349 /* std::byte aliases anything. */ 16350 if (enumtype != error_mark_node 16351 && TYPE_CONTEXT (enumtype) == std_node 16352 && !strcmp ("byte", TYPE_NAME_STRING (enumtype))) 16353 TYPE_ALIAS_SET (enumtype) = 0; 16354 } 16355 else 16356 enumtype = xref_tag (enum_type, name); 16357 16358 if (enumtype == error_mark_node) 16359 return error_mark_node; 16360 16361 /* The enum is considered opaque until the opening '{' of the 16362 enumerator list. */ 16363 SET_OPAQUE_ENUM_P (enumtype, true); 16364 ENUM_FIXED_UNDERLYING_TYPE_P (enumtype) = !! underlying_type; 16365 } 16366 16367 SET_SCOPED_ENUM_P (enumtype, scoped_enum_p); 16368 16369 cplus_decl_attributes (&enumtype, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE); 16370 16371 if (underlying_type) 16372 { 16373 if (ENUM_UNDERLYING_TYPE (enumtype)) 16374 /* We already checked that it matches, don't change it to a different 16375 typedef variant. */; 16376 else if (CP_INTEGRAL_TYPE_P (underlying_type)) 16377 { 16378 copy_type_enum (enumtype, underlying_type); 16379 ENUM_UNDERLYING_TYPE (enumtype) = underlying_type; 16380 } 16381 else if (dependent_type_p (underlying_type)) 16382 ENUM_UNDERLYING_TYPE (enumtype) = underlying_type; 16383 else 16384 error ("underlying type %qT of %qT must be an integral type", 16385 underlying_type, enumtype); 16386 } 16387 16388 /* If into a template class, the returned enum is always the first 16389 declaration (opaque or not) seen. This way all the references to 16390 this type will be to the same declaration. The following ones are used 16391 only to check for definition errors. */ 16392 if (prevtype && processing_template_decl) 16393 return prevtype; 16394 else 16395 return enumtype; 16396 } 16397 16398 /* After processing and defining all the values of an enumeration type, 16399 install their decls in the enumeration type. 16400 ENUMTYPE is the type object. */ 16401 16402 void 16403 finish_enum_value_list (tree enumtype) 16404 { 16405 tree values; 16406 tree underlying_type; 16407 tree decl; 16408 tree value; 16409 tree minnode, maxnode; 16410 tree t; 16411 16412 bool fixed_underlying_type_p 16413 = ENUM_UNDERLYING_TYPE (enumtype) != NULL_TREE; 16414 16415 /* We built up the VALUES in reverse order. */ 16416 TYPE_VALUES (enumtype) = nreverse (TYPE_VALUES (enumtype)); 16417 16418 /* For an enum defined in a template, just set the type of the values; 16419 all further processing is postponed until the template is 16420 instantiated. We need to set the type so that tsubst of a CONST_DECL 16421 works. */ 16422 if (processing_template_decl) 16423 { 16424 for (values = TYPE_VALUES (enumtype); 16425 values; 16426 values = TREE_CHAIN (values)) 16427 TREE_TYPE (TREE_VALUE (values)) = enumtype; 16428 return; 16429 } 16430 16431 /* Determine the minimum and maximum values of the enumerators. */ 16432 if (TYPE_VALUES (enumtype)) 16433 { 16434 minnode = maxnode = NULL_TREE; 16435 16436 for (values = TYPE_VALUES (enumtype); 16437 values; 16438 values = TREE_CHAIN (values)) 16439 { 16440 decl = TREE_VALUE (values); 16441 16442 /* [dcl.enum]: Following the closing brace of an enum-specifier, 16443 each enumerator has the type of its enumeration. Prior to the 16444 closing brace, the type of each enumerator is the type of its 16445 initializing value. */ 16446 TREE_TYPE (decl) = enumtype; 16447 16448 /* Update the minimum and maximum values, if appropriate. */ 16449 value = DECL_INITIAL (decl); 16450 if (TREE_CODE (value) != INTEGER_CST) 16451 value = integer_zero_node; 16452 /* Figure out what the minimum and maximum values of the 16453 enumerators are. */ 16454 if (!minnode) 16455 minnode = maxnode = value; 16456 else if (tree_int_cst_lt (maxnode, value)) 16457 maxnode = value; 16458 else if (tree_int_cst_lt (value, minnode)) 16459 minnode = value; 16460 } 16461 } 16462 else 16463 /* [dcl.enum] 16464 16465 If the enumerator-list is empty, the underlying type is as if 16466 the enumeration had a single enumerator with value 0. */ 16467 minnode = maxnode = integer_zero_node; 16468 16469 if (!fixed_underlying_type_p) 16470 { 16471 /* Compute the number of bits require to represent all values of the 16472 enumeration. We must do this before the type of MINNODE and 16473 MAXNODE are transformed, since tree_int_cst_min_precision relies 16474 on the TREE_TYPE of the value it is passed. */ 16475 signop sgn = tree_int_cst_sgn (minnode) >= 0 ? UNSIGNED : SIGNED; 16476 int lowprec = tree_int_cst_min_precision (minnode, sgn); 16477 int highprec = tree_int_cst_min_precision (maxnode, sgn); 16478 int precision = MAX (lowprec, highprec); 16479 unsigned int itk; 16480 bool use_short_enum; 16481 16482 /* Determine the underlying type of the enumeration. 16483 16484 [dcl.enum] 16485 16486 The underlying type of an enumeration is an integral type that 16487 can represent all the enumerator values defined in the 16488 enumeration. It is implementation-defined which integral type is 16489 used as the underlying type for an enumeration except that the 16490 underlying type shall not be larger than int unless the value of 16491 an enumerator cannot fit in an int or unsigned int. 16492 16493 We use "int" or an "unsigned int" as the underlying type, even if 16494 a smaller integral type would work, unless the user has 16495 explicitly requested that we use the smallest possible type. The 16496 user can request that for all enumerations with a command line 16497 flag, or for just one enumeration with an attribute. */ 16498 16499 use_short_enum = flag_short_enums 16500 || lookup_attribute ("packed", TYPE_ATTRIBUTES (enumtype)); 16501 16502 /* If the precision of the type was specified with an attribute and it 16503 was too small, give an error. Otherwise, use it. */ 16504 if (TYPE_PRECISION (enumtype)) 16505 { 16506 if (precision > TYPE_PRECISION (enumtype)) 16507 error ("specified mode too small for enumerated values"); 16508 else 16509 { 16510 use_short_enum = true; 16511 precision = TYPE_PRECISION (enumtype); 16512 } 16513 } 16514 16515 for (itk = (use_short_enum ? itk_char : itk_int); 16516 itk != itk_none; 16517 itk++) 16518 { 16519 underlying_type = integer_types[itk]; 16520 if (underlying_type != NULL_TREE 16521 && TYPE_PRECISION (underlying_type) >= precision 16522 && TYPE_SIGN (underlying_type) == sgn) 16523 break; 16524 } 16525 if (itk == itk_none) 16526 { 16527 /* DR 377 16528 16529 IF no integral type can represent all the enumerator values, the 16530 enumeration is ill-formed. */ 16531 error ("no integral type can represent all of the enumerator values " 16532 "for %qT", enumtype); 16533 precision = TYPE_PRECISION (long_long_integer_type_node); 16534 underlying_type = integer_types[itk_unsigned_long_long]; 16535 } 16536 16537 /* [dcl.enum] 16538 16539 The value of sizeof() applied to an enumeration type, an object 16540 of an enumeration type, or an enumerator, is the value of sizeof() 16541 applied to the underlying type. */ 16542 copy_type_enum (enumtype, underlying_type); 16543 16544 /* Compute the minimum and maximum values for the type. 16545 16546 [dcl.enum] 16547 16548 For an enumeration where emin is the smallest enumerator and emax 16549 is the largest, the values of the enumeration are the values of the 16550 underlying type in the range bmin to bmax, where bmin and bmax are, 16551 respectively, the smallest and largest values of the smallest bit- 16552 field that can store emin and emax. */ 16553 16554 /* The middle-end currently assumes that types with TYPE_PRECISION 16555 narrower than their underlying type are suitably zero or sign 16556 extended to fill their mode. Similarly, it assumes that the front 16557 end assures that a value of a particular type must be within 16558 TYPE_MIN_VALUE and TYPE_MAX_VALUE. 16559 16560 We used to set these fields based on bmin and bmax, but that led 16561 to invalid assumptions like optimizing away bounds checking. So 16562 now we just set the TYPE_PRECISION, TYPE_MIN_VALUE, and 16563 TYPE_MAX_VALUE to the values for the mode above and only restrict 16564 the ENUM_UNDERLYING_TYPE for the benefit of diagnostics. */ 16565 ENUM_UNDERLYING_TYPE (enumtype) 16566 = build_distinct_type_copy (underlying_type); 16567 TYPE_PRECISION (ENUM_UNDERLYING_TYPE (enumtype)) = precision; 16568 set_min_and_max_values_for_integral_type 16569 (ENUM_UNDERLYING_TYPE (enumtype), precision, sgn); 16570 16571 /* If -fstrict-enums, still constrain TYPE_MIN/MAX_VALUE. */ 16572 if (flag_strict_enums) 16573 set_min_and_max_values_for_integral_type (enumtype, precision, sgn); 16574 } 16575 else 16576 underlying_type = ENUM_UNDERLYING_TYPE (enumtype); 16577 16578 /* If the enum is exported, mark the consts too. */ 16579 bool export_p = (UNSCOPED_ENUM_P (enumtype) 16580 && DECL_MODULE_EXPORT_P (TYPE_STUB_DECL (enumtype)) 16581 && at_namespace_scope_p ()); 16582 16583 /* Convert each of the enumerators to the type of the underlying 16584 type of the enumeration. */ 16585 for (values = TYPE_VALUES (enumtype); values; values = TREE_CHAIN (values)) 16586 { 16587 decl = TREE_VALUE (values); 16588 iloc_sentinel ils (DECL_SOURCE_LOCATION (decl)); 16589 if (fixed_underlying_type_p) 16590 /* If the enumeration type has a fixed underlying type, we 16591 already checked all of the enumerator values. */ 16592 value = DECL_INITIAL (decl); 16593 else 16594 value = perform_implicit_conversion (underlying_type, 16595 DECL_INITIAL (decl), 16596 tf_warning_or_error); 16597 /* Do not clobber shared ints. */ 16598 if (value != error_mark_node) 16599 { 16600 value = copy_node (value); 16601 16602 TREE_TYPE (value) = enumtype; 16603 } 16604 DECL_INITIAL (decl) = value; 16605 if (export_p) 16606 DECL_MODULE_EXPORT_P (decl) = true; 16607 } 16608 16609 /* Fix up all variant types of this enum type. */ 16610 for (t = TYPE_MAIN_VARIANT (enumtype); t; t = TYPE_NEXT_VARIANT (t)) 16611 TYPE_VALUES (t) = TYPE_VALUES (enumtype); 16612 16613 if (at_class_scope_p () 16614 && COMPLETE_TYPE_P (current_class_type) 16615 && UNSCOPED_ENUM_P (enumtype)) 16616 { 16617 insert_late_enum_def_bindings (current_class_type, enumtype); 16618 /* TYPE_FIELDS needs fixup. */ 16619 fixup_type_variants (current_class_type); 16620 } 16621 16622 /* Finish debugging output for this type. */ 16623 rest_of_type_compilation (enumtype, namespace_bindings_p ()); 16624 16625 /* Each enumerator now has the type of its enumeration. Clear the cache 16626 so that this change in types doesn't confuse us later on. */ 16627 clear_cv_and_fold_caches (); 16628 } 16629 16630 /* Finishes the enum type. This is called only the first time an 16631 enumeration is seen, be it opaque or odinary. 16632 ENUMTYPE is the type object. */ 16633 16634 void 16635 finish_enum (tree enumtype) 16636 { 16637 if (processing_template_decl) 16638 { 16639 if (at_function_scope_p ()) 16640 add_stmt (build_min (TAG_DEFN, enumtype)); 16641 return; 16642 } 16643 16644 /* If this is a forward declaration, there should not be any variants, 16645 though we can get a variant in the middle of an enum-specifier with 16646 wacky code like 'enum E { e = sizeof(const E*) };' */ 16647 gcc_assert (enumtype == TYPE_MAIN_VARIANT (enumtype) 16648 && (TYPE_VALUES (enumtype) 16649 || !TYPE_NEXT_VARIANT (enumtype))); 16650 } 16651 16652 /* Build and install a CONST_DECL for an enumeration constant of the 16653 enumeration type ENUMTYPE whose NAME and VALUE (if any) are provided. 16654 Apply ATTRIBUTES if available. LOC is the location of NAME. 16655 Assignment of sequential values by default is handled here. */ 16656 16657 tree 16658 build_enumerator (tree name, tree value, tree enumtype, tree attributes, 16659 location_t loc) 16660 { 16661 tree decl; 16662 tree context; 16663 tree type; 16664 16665 /* scalar_constant_value will pull out this expression, so make sure 16666 it's folded as appropriate. */ 16667 if (processing_template_decl) 16668 value = fold_non_dependent_expr (value); 16669 16670 /* If the VALUE was erroneous, pretend it wasn't there; that will 16671 result in the enum being assigned the next value in sequence. */ 16672 if (value == error_mark_node) 16673 value = NULL_TREE; 16674 16675 /* Remove no-op casts from the value. */ 16676 if (value) 16677 STRIP_TYPE_NOPS (value); 16678 16679 if (! processing_template_decl) 16680 { 16681 /* Validate and default VALUE. */ 16682 if (value != NULL_TREE) 16683 { 16684 if (!ENUM_UNDERLYING_TYPE (enumtype)) 16685 { 16686 tree tmp_value = build_expr_type_conversion (WANT_INT | WANT_ENUM, 16687 value, true); 16688 if (tmp_value) 16689 value = tmp_value; 16690 } 16691 else if (! INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P 16692 (TREE_TYPE (value))) 16693 value = perform_implicit_conversion_flags 16694 (ENUM_UNDERLYING_TYPE (enumtype), value, tf_warning_or_error, 16695 LOOKUP_IMPLICIT | LOOKUP_NO_NARROWING); 16696 16697 if (value == error_mark_node) 16698 value = NULL_TREE; 16699 16700 if (value != NULL_TREE) 16701 { 16702 if (! INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P 16703 (TREE_TYPE (value))) 16704 { 16705 error_at (cp_expr_loc_or_input_loc (value), 16706 "enumerator value for %qD must have integral or " 16707 "unscoped enumeration type", name); 16708 value = NULL_TREE; 16709 } 16710 else 16711 { 16712 value = cxx_constant_value (value); 16713 16714 if (TREE_CODE (value) != INTEGER_CST) 16715 { 16716 error ("enumerator value for %qD is not an integer " 16717 "constant", name); 16718 value = NULL_TREE; 16719 } 16720 } 16721 } 16722 } 16723 16724 /* Default based on previous value. */ 16725 if (value == NULL_TREE) 16726 { 16727 if (TYPE_VALUES (enumtype)) 16728 { 16729 tree prev_value; 16730 16731 /* C++03 7.2/4: If no initializer is specified for the first 16732 enumerator, the type is an unspecified integral 16733 type. Otherwise the type is the same as the type of the 16734 initializing value of the preceding enumerator unless the 16735 incremented value is not representable in that type, in 16736 which case the type is an unspecified integral type 16737 sufficient to contain the incremented value. */ 16738 prev_value = DECL_INITIAL (TREE_VALUE (TYPE_VALUES (enumtype))); 16739 if (TREE_CODE (prev_value) != INTEGER_CST) 16740 value = error_mark_node; 16741 else 16742 { 16743 wi::overflow_type overflowed; 16744 tree type = TREE_TYPE (prev_value); 16745 signop sgn = TYPE_SIGN (type); 16746 widest_int wi = wi::add (wi::to_widest (prev_value), 1, sgn, 16747 &overflowed); 16748 if (!overflowed) 16749 { 16750 bool pos = !wi::neg_p (wi, sgn); 16751 if (!wi::fits_to_tree_p (wi, type)) 16752 { 16753 unsigned int itk; 16754 for (itk = itk_int; itk != itk_none; itk++) 16755 { 16756 type = integer_types[itk]; 16757 if (type != NULL_TREE 16758 && (pos || !TYPE_UNSIGNED (type)) 16759 && wi::fits_to_tree_p (wi, type)) 16760 break; 16761 } 16762 if (type && cxx_dialect < cxx11 16763 && itk > itk_unsigned_long) 16764 pedwarn (input_location, OPT_Wlong_long, 16765 pos ? G_("\ 16766 incremented enumerator value is too large for %<unsigned long%>") : G_("\ 16767 incremented enumerator value is too large for %<long%>")); 16768 } 16769 if (type == NULL_TREE) 16770 overflowed = wi::OVF_UNKNOWN; 16771 else 16772 value = wide_int_to_tree (type, wi); 16773 } 16774 16775 if (overflowed) 16776 { 16777 error ("overflow in enumeration values at %qD", name); 16778 value = error_mark_node; 16779 } 16780 } 16781 } 16782 else 16783 value = integer_zero_node; 16784 } 16785 16786 /* Remove no-op casts from the value. */ 16787 STRIP_TYPE_NOPS (value); 16788 16789 /* If the underlying type of the enum is fixed, check whether 16790 the enumerator values fits in the underlying type. If it 16791 does not fit, the program is ill-formed [C++0x dcl.enum]. */ 16792 if (ENUM_UNDERLYING_TYPE (enumtype) 16793 && value 16794 && TREE_CODE (value) == INTEGER_CST) 16795 { 16796 if (!int_fits_type_p (value, ENUM_UNDERLYING_TYPE (enumtype))) 16797 error ("enumerator value %qE is outside the range of underlying " 16798 "type %qT", value, ENUM_UNDERLYING_TYPE (enumtype)); 16799 16800 /* Convert the value to the appropriate type. */ 16801 value = fold_convert (ENUM_UNDERLYING_TYPE (enumtype), value); 16802 } 16803 } 16804 16805 /* C++ associates enums with global, function, or class declarations. */ 16806 context = current_scope (); 16807 16808 /* Build the actual enumeration constant. Note that the enumeration 16809 constants have the underlying type of the enum (if it is fixed) 16810 or the type of their initializer (if the underlying type of the 16811 enum is not fixed): 16812 16813 [ C++0x dcl.enum ] 16814 16815 If the underlying type is fixed, the type of each enumerator 16816 prior to the closing brace is the underlying type; if the 16817 initializing value of an enumerator cannot be represented by 16818 the underlying type, the program is ill-formed. If the 16819 underlying type is not fixed, the type of each enumerator is 16820 the type of its initializing value. 16821 16822 If the underlying type is not fixed, it will be computed by 16823 finish_enum and we will reset the type of this enumerator. Of 16824 course, if we're processing a template, there may be no value. */ 16825 type = value ? TREE_TYPE (value) : NULL_TREE; 16826 16827 decl = build_decl (loc, CONST_DECL, name, type); 16828 16829 DECL_CONTEXT (decl) = enumtype; 16830 TREE_CONSTANT (decl) = 1; 16831 TREE_READONLY (decl) = 1; 16832 DECL_INITIAL (decl) = value; 16833 16834 if (attributes) 16835 cplus_decl_attributes (&decl, attributes, 0); 16836 16837 if (context && context == current_class_type && !SCOPED_ENUM_P (enumtype)) 16838 { 16839 /* In something like `struct S { enum E { i = 7 }; };' we put `i' 16840 on the TYPE_FIELDS list for `S'. (That's so that you can say 16841 things like `S::i' later.) */ 16842 16843 /* The enumerator may be getting declared outside of its enclosing 16844 class, like so: 16845 16846 class S { public: enum E : int; }; enum S::E : int { i = 7; }; 16847 16848 For which case we need to make sure that the access of `S::i' 16849 matches the access of `S::E'. */ 16850 auto cas = make_temp_override (current_access_specifier); 16851 set_current_access_from_decl (TYPE_NAME (enumtype)); 16852 finish_member_declaration (decl); 16853 } 16854 else 16855 pushdecl (decl); 16856 16857 /* Add this enumeration constant to the list for this type. */ 16858 TYPE_VALUES (enumtype) = tree_cons (name, decl, TYPE_VALUES (enumtype)); 16859 16860 return decl; 16861 } 16862 16863 /* Look for an enumerator with the given NAME within the enumeration 16864 type ENUMTYPE. This routine is used primarily for qualified name 16865 lookup into an enumerator in C++0x, e.g., 16866 16867 enum class Color { Red, Green, Blue }; 16868 16869 Color color = Color::Red; 16870 16871 Returns the value corresponding to the enumerator, or 16872 NULL_TREE if no such enumerator was found. */ 16873 tree 16874 lookup_enumerator (tree enumtype, tree name) 16875 { 16876 tree e; 16877 gcc_assert (enumtype && TREE_CODE (enumtype) == ENUMERAL_TYPE); 16878 16879 e = purpose_member (name, TYPE_VALUES (enumtype)); 16880 return e? TREE_VALUE (e) : NULL_TREE; 16881 } 16882 16883 /* Implement LANG_HOOKS_SIMULATE_ENUM_DECL. */ 16884 16885 tree 16886 cxx_simulate_enum_decl (location_t loc, const char *name, 16887 vec<string_int_pair> *values) 16888 { 16889 location_t saved_loc = input_location; 16890 input_location = loc; 16891 16892 tree enumtype = start_enum (get_identifier (name), NULL_TREE, NULL_TREE, 16893 NULL_TREE, false, NULL); 16894 if (!OPAQUE_ENUM_P (enumtype)) 16895 { 16896 error_at (loc, "multiple definition of %q#T", enumtype); 16897 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (enumtype)), 16898 "previous definition here"); 16899 return enumtype; 16900 } 16901 SET_OPAQUE_ENUM_P (enumtype, false); 16902 DECL_SOURCE_LOCATION (TYPE_NAME (enumtype)) = loc; 16903 16904 for (const string_int_pair &value : values) 16905 build_enumerator (get_identifier (value.first), 16906 build_int_cst (integer_type_node, value.second), 16907 enumtype, NULL_TREE, loc); 16908 16909 finish_enum_value_list (enumtype); 16910 finish_enum (enumtype); 16911 16912 input_location = saved_loc; 16913 return enumtype; 16914 } 16915 16916 /* Implement LANG_HOOKS_SIMULATE_RECORD_DECL. */ 16917 16918 tree 16919 cxx_simulate_record_decl (location_t loc, const char *name, 16920 array_slice<const tree> fields) 16921 { 16922 iloc_sentinel ils (loc); 16923 16924 tree ident = get_identifier (name); 16925 tree type = xref_tag (/*tag_code=*/record_type, ident); 16926 if (type != error_mark_node 16927 && (TREE_CODE (type) != RECORD_TYPE || COMPLETE_TYPE_P (type))) 16928 { 16929 error ("redefinition of %q#T", type); 16930 type = error_mark_node; 16931 } 16932 if (type == error_mark_node) 16933 return lhd_simulate_record_decl (loc, name, fields); 16934 16935 xref_basetypes (type, NULL_TREE); 16936 type = begin_class_definition (type); 16937 if (type == error_mark_node) 16938 return lhd_simulate_record_decl (loc, name, fields); 16939 16940 for (tree field : fields) 16941 finish_member_declaration (field); 16942 16943 type = finish_struct (type, NULL_TREE); 16944 16945 tree decl = build_decl (loc, TYPE_DECL, ident, type); 16946 set_underlying_type (decl); 16947 lang_hooks.decls.pushdecl (decl); 16948 16949 return type; 16950 } 16951 16952 /* We're defining DECL. Make sure that its type is OK. */ 16954 16955 static void 16956 check_function_type (tree decl, tree current_function_parms) 16957 { 16958 tree fntype = TREE_TYPE (decl); 16959 tree return_type = complete_type (TREE_TYPE (fntype)); 16960 16961 /* In a function definition, arg types must be complete. */ 16962 require_complete_types_for_parms (current_function_parms); 16963 16964 if (dependent_type_p (return_type) 16965 || type_uses_auto (return_type)) 16966 return; 16967 if (!COMPLETE_OR_VOID_TYPE_P (return_type)) 16968 { 16969 tree args = TYPE_ARG_TYPES (fntype); 16970 16971 error ("return type %q#T is incomplete", return_type); 16972 16973 /* Make it return void instead. */ 16974 if (TREE_CODE (fntype) == METHOD_TYPE) 16975 fntype = build_method_type_directly (TREE_TYPE (TREE_VALUE (args)), 16976 void_type_node, 16977 TREE_CHAIN (args)); 16978 else 16979 fntype = build_function_type (void_type_node, args); 16980 fntype = (cp_build_type_attribute_variant 16981 (fntype, TYPE_ATTRIBUTES (TREE_TYPE (decl)))); 16982 fntype = cxx_copy_lang_qualifiers (fntype, TREE_TYPE (decl)); 16983 TREE_TYPE (decl) = fntype; 16984 } 16985 else 16986 { 16987 abstract_virtuals_error (decl, TREE_TYPE (fntype)); 16988 maybe_warn_parm_abi (TREE_TYPE (fntype), 16989 DECL_SOURCE_LOCATION (decl)); 16990 } 16991 } 16992 16993 /* True iff FN is an implicitly-defined default constructor. */ 16994 16995 static bool 16996 implicit_default_ctor_p (tree fn) 16997 { 16998 return (DECL_CONSTRUCTOR_P (fn) 16999 && !user_provided_p (fn) 17000 && sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (fn))); 17001 } 17002 17003 /* Clobber the contents of *this to let the back end know that the object 17004 storage is dead when we enter the constructor or leave the destructor. */ 17005 17006 static tree 17007 build_clobber_this () 17008 { 17009 /* Clobbering an empty base is pointless, and harmful if its one byte 17010 TYPE_SIZE overlays real data. */ 17011 if (is_empty_class (current_class_type)) 17012 return void_node; 17013 17014 /* If we have virtual bases, clobber the whole object, but only if we're in 17015 charge. If we don't have virtual bases, clobber the as-base type so we 17016 don't mess with tail padding. */ 17017 bool vbases = CLASSTYPE_VBASECLASSES (current_class_type); 17018 17019 tree ctype = current_class_type; 17020 if (!vbases) 17021 ctype = CLASSTYPE_AS_BASE (ctype); 17022 17023 tree clobber = build_clobber (ctype); 17024 17025 tree thisref = current_class_ref; 17026 if (ctype != current_class_type) 17027 { 17028 thisref = build_nop (build_reference_type (ctype), current_class_ptr); 17029 thisref = convert_from_reference (thisref); 17030 } 17031 17032 tree exprstmt = build2 (MODIFY_EXPR, void_type_node, thisref, clobber); 17033 if (vbases) 17034 exprstmt = build_if_in_charge (exprstmt); 17035 17036 return exprstmt; 17037 } 17038 17039 /* Create the FUNCTION_DECL for a function definition. 17040 DECLSPECS and DECLARATOR are the parts of the declaration; 17041 they describe the function's name and the type it returns, 17042 but twisted together in a fashion that parallels the syntax of C. 17043 17044 FLAGS is a bitwise or of SF_PRE_PARSED (indicating that the 17045 DECLARATOR is really the DECL for the function we are about to 17046 process and that DECLSPECS should be ignored), SF_INCLASS_INLINE 17047 indicating that the function is an inline defined in-class. 17048 17049 This function creates a binding context for the function body 17050 as well as setting up the FUNCTION_DECL in current_function_decl. 17051 17052 For C++, we must first check whether that datum makes any sense. 17053 For example, "class A local_a(1,2);" means that variable local_a 17054 is an aggregate of type A, which should have a constructor 17055 applied to it with the argument list [1, 2]. 17056 17057 On entry, DECL_INITIAL (decl1) should be NULL_TREE or error_mark_node, 17058 or may be a BLOCK if the function has been defined previously 17059 in this translation unit. On exit, DECL_INITIAL (decl1) will be 17060 error_mark_node if the function has never been defined, or 17061 a BLOCK if the function has been defined somewhere. */ 17062 17063 bool 17064 start_preparsed_function (tree decl1, tree attrs, int flags) 17065 { 17066 tree ctype = NULL_TREE; 17067 bool doing_friend = false; 17068 17069 /* Sanity check. */ 17070 gcc_assert (VOID_TYPE_P (TREE_VALUE (void_list_node))); 17071 gcc_assert (TREE_CHAIN (void_list_node) == NULL_TREE); 17072 17073 tree fntype = TREE_TYPE (decl1); 17074 if (TREE_CODE (fntype) == METHOD_TYPE) 17075 ctype = TYPE_METHOD_BASETYPE (fntype); 17076 else 17077 { 17078 ctype = DECL_FRIEND_CONTEXT (decl1); 17079 17080 if (ctype) 17081 doing_friend = true; 17082 } 17083 17084 if (DECL_DECLARED_INLINE_P (decl1) 17085 && lookup_attribute ("noinline", attrs)) 17086 warning_at (DECL_SOURCE_LOCATION (decl1), 0, 17087 "inline function %qD given attribute %qs", decl1, "noinline"); 17088 17089 /* Handle gnu_inline attribute. */ 17090 if (GNU_INLINE_P (decl1)) 17091 { 17092 DECL_EXTERNAL (decl1) = 1; 17093 DECL_NOT_REALLY_EXTERN (decl1) = 0; 17094 DECL_INTERFACE_KNOWN (decl1) = 1; 17095 DECL_DISREGARD_INLINE_LIMITS (decl1) = 1; 17096 } 17097 17098 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl1)) 17099 /* This is a constructor, we must ensure that any default args 17100 introduced by this definition are propagated to the clones 17101 now. The clones are used directly in overload resolution. */ 17102 adjust_clone_args (decl1); 17103 17104 /* Sometimes we don't notice that a function is a static member, and 17105 build a METHOD_TYPE for it. Fix that up now. */ 17106 gcc_assert (!(ctype != NULL_TREE && DECL_STATIC_FUNCTION_P (decl1) 17107 && TREE_CODE (TREE_TYPE (decl1)) == METHOD_TYPE)); 17108 17109 /* Set up current_class_type, and enter the scope of the class, if 17110 appropriate. */ 17111 if (ctype) 17112 push_nested_class (ctype); 17113 else if (DECL_STATIC_FUNCTION_P (decl1)) 17114 push_nested_class (DECL_CONTEXT (decl1)); 17115 17116 /* Now that we have entered the scope of the class, we must restore 17117 the bindings for any template parameters surrounding DECL1, if it 17118 is an inline member template. (Order is important; consider the 17119 case where a template parameter has the same name as a field of 17120 the class.) It is not until after this point that 17121 PROCESSING_TEMPLATE_DECL is guaranteed to be set up correctly. */ 17122 if (flags & SF_INCLASS_INLINE) 17123 maybe_begin_member_template_processing (decl1); 17124 17125 /* Effective C++ rule 15. */ 17126 if (warn_ecpp 17127 && DECL_ASSIGNMENT_OPERATOR_P (decl1) 17128 && DECL_OVERLOADED_OPERATOR_IS (decl1, NOP_EXPR) 17129 && VOID_TYPE_P (TREE_TYPE (fntype))) 17130 warning (OPT_Weffc__, 17131 "%<operator=%> should return a reference to %<*this%>"); 17132 17133 /* Make the init_value nonzero so pushdecl knows this is not tentative. 17134 error_mark_node is replaced below (in poplevel) with the BLOCK. */ 17135 if (!DECL_INITIAL (decl1)) 17136 DECL_INITIAL (decl1) = error_mark_node; 17137 17138 /* This function exists in static storage. 17139 (This does not mean `static' in the C sense!) */ 17140 TREE_STATIC (decl1) = 1; 17141 17142 /* We must call push_template_decl after current_class_type is set 17143 up. (If we are processing inline definitions after exiting a 17144 class scope, current_class_type will be NULL_TREE until set above 17145 by push_nested_class.) */ 17146 if (processing_template_decl) 17147 { 17148 tree newdecl1 = push_template_decl (decl1, doing_friend); 17149 if (newdecl1 == error_mark_node) 17150 { 17151 if (ctype || DECL_STATIC_FUNCTION_P (decl1)) 17152 pop_nested_class (); 17153 return false; 17154 } 17155 decl1 = newdecl1; 17156 } 17157 17158 /* Make sure the parameter and return types are reasonable. When 17159 you declare a function, these types can be incomplete, but they 17160 must be complete when you define the function. */ 17161 check_function_type (decl1, DECL_ARGUMENTS (decl1)); 17162 17163 /* Build the return declaration for the function. */ 17164 tree restype = TREE_TYPE (fntype); 17165 17166 if (DECL_RESULT (decl1) == NULL_TREE) 17167 { 17168 tree resdecl; 17169 17170 resdecl = build_decl (input_location, RESULT_DECL, 0, restype); 17171 DECL_ARTIFICIAL (resdecl) = 1; 17172 DECL_IGNORED_P (resdecl) = 1; 17173 DECL_RESULT (decl1) = resdecl; 17174 17175 cp_apply_type_quals_to_decl (cp_type_quals (restype), resdecl); 17176 } 17177 17178 /* Record the decl so that the function name is defined. 17179 If we already have a decl for this name, and it is a FUNCTION_DECL, 17180 use the old decl. */ 17181 if (!processing_template_decl && !(flags & SF_PRE_PARSED)) 17182 { 17183 /* A specialization is not used to guide overload resolution. */ 17184 if (!DECL_FUNCTION_MEMBER_P (decl1) 17185 && !(DECL_USE_TEMPLATE (decl1) && 17186 PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl1)))) 17187 { 17188 tree olddecl = pushdecl (decl1); 17189 17190 if (olddecl == error_mark_node) 17191 /* If something went wrong when registering the declaration, 17192 use DECL1; we have to have a FUNCTION_DECL to use when 17193 parsing the body of the function. */ 17194 ; 17195 else 17196 { 17197 /* Otherwise, OLDDECL is either a previous declaration 17198 of the same function or DECL1 itself. */ 17199 17200 if (warn_missing_declarations 17201 && olddecl == decl1 17202 && !DECL_MAIN_P (decl1) 17203 && TREE_PUBLIC (decl1) 17204 && !DECL_DECLARED_INLINE_P (decl1)) 17205 { 17206 tree context; 17207 17208 /* Check whether DECL1 is in an anonymous 17209 namespace. */ 17210 for (context = DECL_CONTEXT (decl1); 17211 context; 17212 context = DECL_CONTEXT (context)) 17213 { 17214 if (TREE_CODE (context) == NAMESPACE_DECL 17215 && DECL_NAME (context) == NULL_TREE) 17216 break; 17217 } 17218 17219 if (context == NULL) 17220 warning_at (DECL_SOURCE_LOCATION (decl1), 17221 OPT_Wmissing_declarations, 17222 "no previous declaration for %qD", decl1); 17223 } 17224 17225 decl1 = olddecl; 17226 } 17227 } 17228 else 17229 { 17230 /* We need to set the DECL_CONTEXT. */ 17231 if (!DECL_CONTEXT (decl1) && DECL_TEMPLATE_INFO (decl1)) 17232 DECL_CONTEXT (decl1) = DECL_CONTEXT (DECL_TI_TEMPLATE (decl1)); 17233 } 17234 fntype = TREE_TYPE (decl1); 17235 restype = TREE_TYPE (fntype); 17236 17237 /* If #pragma weak applies, mark the decl appropriately now. 17238 The pragma only applies to global functions. Because 17239 determining whether or not the #pragma applies involves 17240 computing the mangled name for the declaration, we cannot 17241 apply the pragma until after we have merged this declaration 17242 with any previous declarations; if the original declaration 17243 has a linkage specification, that specification applies to 17244 the definition as well, and may affect the mangled name. */ 17245 if (DECL_FILE_SCOPE_P (decl1)) 17246 maybe_apply_pragma_weak (decl1); 17247 } 17248 17249 /* We are now in the scope of the function being defined. */ 17250 current_function_decl = decl1; 17251 17252 /* Save the parm names or decls from this function's declarator 17253 where store_parm_decls will find them. */ 17254 tree current_function_parms = DECL_ARGUMENTS (decl1); 17255 17256 /* Let the user know we're compiling this function. */ 17257 announce_function (decl1); 17258 17259 gcc_assert (DECL_INITIAL (decl1)); 17260 17261 /* This function may already have been parsed, in which case just 17262 return; our caller will skip over the body without parsing. */ 17263 if (DECL_INITIAL (decl1) != error_mark_node) 17264 return true; 17265 17266 /* Initialize RTL machinery. We cannot do this until 17267 CURRENT_FUNCTION_DECL and DECL_RESULT are set up. We do this 17268 even when processing a template; this is how we get 17269 CFUN set up, and our per-function variables initialized. 17270 FIXME factor out the non-RTL stuff. */ 17271 cp_binding_level *bl = current_binding_level; 17272 allocate_struct_function (decl1, processing_template_decl); 17273 17274 /* Initialize the language data structures. Whenever we start 17275 a new function, we destroy temporaries in the usual way. */ 17276 cfun->language = ggc_cleared_alloc<language_function> (); 17277 current_stmt_tree ()->stmts_are_full_exprs_p = 1; 17278 current_binding_level = bl; 17279 17280 /* If we are (erroneously) defining a function that we have already 17281 defined before, wipe out what we knew before. */ 17282 gcc_checking_assert (!DECL_PENDING_INLINE_P (decl1)); 17283 FNDECL_USED_AUTO (decl1) = false; 17284 DECL_SAVED_AUTO_RETURN_TYPE (decl1) = NULL; 17285 17286 if (!processing_template_decl && type_uses_auto (restype)) 17287 { 17288 FNDECL_USED_AUTO (decl1) = true; 17289 DECL_SAVED_AUTO_RETURN_TYPE (decl1) = restype; 17290 } 17291 17292 /* Start the statement-tree, start the tree now. */ 17293 DECL_SAVED_TREE (decl1) = push_stmt_list (); 17294 17295 if (ctype && !doing_friend && !DECL_STATIC_FUNCTION_P (decl1)) 17296 { 17297 /* We know that this was set up by `grokclassfn'. We do not 17298 wait until `store_parm_decls', since evil parse errors may 17299 never get us to that point. Here we keep the consistency 17300 between `current_class_type' and `current_class_ptr'. */ 17301 tree t = DECL_ARGUMENTS (decl1); 17302 17303 gcc_assert (t != NULL_TREE && TREE_CODE (t) == PARM_DECL); 17304 gcc_assert (TYPE_PTR_P (TREE_TYPE (t))); 17305 17306 cp_function_chain->x_current_class_ref 17307 = cp_build_fold_indirect_ref (t); 17308 /* Set this second to avoid shortcut in cp_build_indirect_ref. */ 17309 cp_function_chain->x_current_class_ptr = t; 17310 17311 /* Constructors and destructors need to know whether they're "in 17312 charge" of initializing virtual base classes. */ 17313 t = DECL_CHAIN (t); 17314 if (DECL_HAS_IN_CHARGE_PARM_P (decl1)) 17315 { 17316 current_in_charge_parm = t; 17317 t = DECL_CHAIN (t); 17318 } 17319 if (DECL_HAS_VTT_PARM_P (decl1)) 17320 { 17321 gcc_assert (DECL_NAME (t) == vtt_parm_identifier); 17322 current_vtt_parm = t; 17323 } 17324 } 17325 17326 bool honor_interface = (!DECL_TEMPLATE_INSTANTIATION (decl1) 17327 /* Implicitly-defined methods (like the 17328 destructor for a class in which no destructor 17329 is explicitly declared) must not be defined 17330 until their definition is needed. So, we 17331 ignore interface specifications for 17332 compiler-generated functions. */ 17333 && !DECL_ARTIFICIAL (decl1)); 17334 struct c_fileinfo *finfo 17335 = get_fileinfo (LOCATION_FILE (DECL_SOURCE_LOCATION (decl1))); 17336 17337 if (processing_template_decl) 17338 /* Don't mess with interface flags. */; 17339 else if (DECL_INTERFACE_KNOWN (decl1)) 17340 { 17341 tree ctx = decl_function_context (decl1); 17342 17343 if (DECL_NOT_REALLY_EXTERN (decl1)) 17344 DECL_EXTERNAL (decl1) = 0; 17345 17346 if (ctx != NULL_TREE && vague_linkage_p (ctx)) 17347 /* This is a function in a local class in an extern inline 17348 or template function. */ 17349 comdat_linkage (decl1); 17350 } 17351 /* If this function belongs to an interface, it is public. 17352 If it belongs to someone else's interface, it is also external. 17353 This only affects inlines and template instantiations. */ 17354 else if (!finfo->interface_unknown && honor_interface) 17355 { 17356 if (DECL_DECLARED_INLINE_P (decl1) 17357 || DECL_TEMPLATE_INSTANTIATION (decl1)) 17358 { 17359 DECL_EXTERNAL (decl1) 17360 = (finfo->interface_only 17361 || (DECL_DECLARED_INLINE_P (decl1) 17362 && ! flag_implement_inlines 17363 && !DECL_VINDEX (decl1))); 17364 17365 /* For WIN32 we also want to put these in linkonce sections. */ 17366 maybe_make_one_only (decl1); 17367 } 17368 else 17369 DECL_EXTERNAL (decl1) = 0; 17370 DECL_INTERFACE_KNOWN (decl1) = 1; 17371 /* If this function is in an interface implemented in this file, 17372 make sure that the back end knows to emit this function 17373 here. */ 17374 if (!DECL_EXTERNAL (decl1)) 17375 mark_needed (decl1); 17376 } 17377 else if (finfo->interface_unknown && finfo->interface_only 17378 && honor_interface) 17379 { 17380 /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma 17381 interface, we will have both finfo->interface_unknown and 17382 finfo->interface_only set. In that case, we don't want to 17383 use the normal heuristics because someone will supply a 17384 #pragma implementation elsewhere, and deducing it here would 17385 produce a conflict. */ 17386 comdat_linkage (decl1); 17387 DECL_EXTERNAL (decl1) = 0; 17388 DECL_INTERFACE_KNOWN (decl1) = 1; 17389 DECL_DEFER_OUTPUT (decl1) = 1; 17390 } 17391 else 17392 { 17393 /* This is a definition, not a reference. 17394 So clear DECL_EXTERNAL, unless this is a GNU extern inline. */ 17395 if (!GNU_INLINE_P (decl1)) 17396 DECL_EXTERNAL (decl1) = 0; 17397 17398 if ((DECL_DECLARED_INLINE_P (decl1) 17399 || DECL_TEMPLATE_INSTANTIATION (decl1)) 17400 && ! DECL_INTERFACE_KNOWN (decl1)) 17401 DECL_DEFER_OUTPUT (decl1) = 1; 17402 else 17403 DECL_INTERFACE_KNOWN (decl1) = 1; 17404 } 17405 17406 /* Determine the ELF visibility attribute for the function. We must not 17407 do this before calling "pushdecl", as we must allow "duplicate_decls" 17408 to merge any attributes appropriately. We also need to wait until 17409 linkage is set. */ 17410 if (!DECL_CLONED_FUNCTION_P (decl1)) 17411 determine_visibility (decl1); 17412 17413 if (!processing_template_decl) 17414 maybe_instantiate_noexcept (decl1); 17415 17416 begin_scope (sk_function_parms, decl1); 17417 17418 ++function_depth; 17419 17420 if (DECL_DESTRUCTOR_P (decl1) 17421 || (DECL_CONSTRUCTOR_P (decl1) 17422 && targetm.cxx.cdtor_returns_this ())) 17423 { 17424 cdtor_label = create_artificial_label (input_location); 17425 LABEL_DECL_CDTOR (cdtor_label) = true; 17426 } 17427 17428 start_fname_decls (); 17429 17430 store_parm_decls (current_function_parms); 17431 17432 if (!processing_template_decl 17433 && (flag_lifetime_dse > 1) 17434 && DECL_CONSTRUCTOR_P (decl1) 17435 && !DECL_CLONED_FUNCTION_P (decl1) 17436 /* Clobbering an empty base is harmful if it overlays real data. */ 17437 && !is_empty_class (current_class_type) 17438 /* We can't clobber safely for an implicitly-defined default constructor 17439 because part of the initialization might happen before we enter the 17440 constructor, via AGGR_INIT_ZERO_FIRST (c++/68006). */ 17441 && !implicit_default_ctor_p (decl1)) 17442 finish_expr_stmt (build_clobber_this ()); 17443 17444 if (!processing_template_decl 17445 && DECL_CONSTRUCTOR_P (decl1) 17446 && sanitize_flags_p (SANITIZE_VPTR) 17447 && !DECL_CLONED_FUNCTION_P (decl1) 17448 && !implicit_default_ctor_p (decl1)) 17449 cp_ubsan_maybe_initialize_vtbl_ptrs (current_class_ptr); 17450 17451 if (!DECL_OMP_DECLARE_REDUCTION_P (decl1)) 17452 start_lambda_scope (decl1); 17453 17454 return true; 17455 } 17456 17457 17458 /* Like start_preparsed_function, except that instead of a 17459 FUNCTION_DECL, this function takes DECLSPECS and DECLARATOR. 17460 17461 Returns true on success. If the DECLARATOR is not suitable 17462 for a function, we return false, which tells the parser to 17463 skip the entire function. */ 17464 17465 bool 17466 start_function (cp_decl_specifier_seq *declspecs, 17467 const cp_declarator *declarator, 17468 tree attrs) 17469 { 17470 tree decl1; 17471 17472 decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, &attrs); 17473 invoke_plugin_callbacks (PLUGIN_START_PARSE_FUNCTION, decl1); 17474 if (decl1 == error_mark_node) 17475 return false; 17476 17477 if (DECL_MAIN_P (decl1)) 17478 /* main must return int. grokfndecl should have corrected it 17479 (and issued a diagnostic) if the user got it wrong. */ 17480 gcc_assert (same_type_p (TREE_TYPE (TREE_TYPE (decl1)), 17481 integer_type_node)); 17482 17483 return start_preparsed_function (decl1, attrs, /*flags=*/SF_DEFAULT); 17484 } 17485 17486 /* Returns true iff an EH_SPEC_BLOCK should be created in the body of 17488 FN. */ 17489 17490 static bool 17491 use_eh_spec_block (tree fn) 17492 { 17493 return (flag_exceptions && flag_enforce_eh_specs 17494 && !processing_template_decl 17495 /* We insert the EH_SPEC_BLOCK only in the original 17496 function; then, it is copied automatically to the 17497 clones. */ 17498 && !DECL_CLONED_FUNCTION_P (fn) 17499 /* Implicitly-generated constructors and destructors have 17500 exception specifications. However, those specifications 17501 are the union of the possible exceptions specified by the 17502 constructors/destructors for bases and members, so no 17503 unallowed exception will ever reach this function. By 17504 not creating the EH_SPEC_BLOCK we save a little memory, 17505 and we avoid spurious warnings about unreachable 17506 code. */ 17507 && !DECL_DEFAULTED_FN (fn) 17508 && !type_throw_all_p (TREE_TYPE (fn))); 17509 } 17510 17511 /* Helper function to push ARGS into the current lexical scope. DECL 17512 is the function declaration. NONPARMS is used to handle enum 17513 constants. */ 17514 17515 void 17516 do_push_parm_decls (tree decl, tree args, tree *nonparms) 17517 { 17518 /* If we're doing semantic analysis, then we'll call pushdecl 17519 for each of these. We must do them in reverse order so that 17520 they end in the correct forward order. */ 17521 args = nreverse (args); 17522 17523 tree next; 17524 for (tree parm = args; parm; parm = next) 17525 { 17526 next = DECL_CHAIN (parm); 17527 if (TREE_CODE (parm) == PARM_DECL) 17528 pushdecl (parm); 17529 else if (nonparms) 17530 { 17531 /* If we find an enum constant or a type tag, put it aside for 17532 the moment. */ 17533 TREE_CHAIN (parm) = NULL_TREE; 17534 *nonparms = chainon (*nonparms, parm); 17535 } 17536 } 17537 17538 /* Get the decls in their original chain order and record in the 17539 function. This is all and only the PARM_DECLs that were 17540 pushed into scope by the loop above. */ 17541 DECL_ARGUMENTS (decl) = get_local_decls (); 17542 } 17543 17544 /* Store the parameter declarations into the current function declaration. 17545 This is called after parsing the parameter declarations, before 17546 digesting the body of the function. 17547 17548 Also install to binding contour return value identifier, if any. */ 17549 17550 static void 17551 store_parm_decls (tree current_function_parms) 17552 { 17553 tree fndecl = current_function_decl; 17554 17555 /* This is a chain of any other decls that came in among the parm 17556 declarations. If a parm is declared with enum {foo, bar} x; 17557 then CONST_DECLs for foo and bar are put here. */ 17558 tree nonparms = NULL_TREE; 17559 17560 if (current_function_parms) 17561 { 17562 /* This case is when the function was defined with an ANSI prototype. 17563 The parms already have decls, so we need not do anything here 17564 except record them as in effect 17565 and complain if any redundant old-style parm decls were written. */ 17566 17567 tree specparms = current_function_parms; 17568 17569 /* Must clear this because it might contain TYPE_DECLs declared 17570 at class level. */ 17571 current_binding_level->names = NULL; 17572 17573 do_push_parm_decls (fndecl, specparms, &nonparms); 17574 } 17575 else 17576 DECL_ARGUMENTS (fndecl) = NULL_TREE; 17577 17578 /* Now store the final chain of decls for the arguments 17579 as the decl-chain of the current lexical scope. 17580 Put the enumerators in as well, at the front so that 17581 DECL_ARGUMENTS is not modified. */ 17582 current_binding_level->names = chainon (nonparms, DECL_ARGUMENTS (fndecl)); 17583 17584 if (use_eh_spec_block (current_function_decl)) 17585 current_eh_spec_block = begin_eh_spec_block (); 17586 } 17587 17588 17589 /* Set the return value of the constructor (if present). */ 17591 17592 static void 17593 finish_constructor_body (void) 17594 { 17595 tree val; 17596 tree exprstmt; 17597 17598 if (targetm.cxx.cdtor_returns_this ()) 17599 { 17600 /* Any return from a constructor will end up here. */ 17601 add_stmt (build_stmt (input_location, LABEL_EXPR, cdtor_label)); 17602 17603 val = DECL_ARGUMENTS (current_function_decl); 17604 suppress_warning (val, OPT_Wuse_after_free); 17605 val = build2 (MODIFY_EXPR, TREE_TYPE (val), 17606 DECL_RESULT (current_function_decl), val); 17607 /* Return the address of the object. */ 17608 exprstmt = build_stmt (input_location, RETURN_EXPR, val); 17609 add_stmt (exprstmt); 17610 } 17611 } 17612 17613 /* Do all the processing for the beginning of a destructor; set up the 17614 vtable pointers and cleanups for bases and members. */ 17615 17616 static void 17617 begin_destructor_body (void) 17618 { 17619 tree compound_stmt; 17620 17621 /* If the CURRENT_CLASS_TYPE is incomplete, we will have already 17622 issued an error message. We still want to try to process the 17623 body of the function, but initialize_vtbl_ptrs will crash if 17624 TYPE_BINFO is NULL. */ 17625 if (COMPLETE_TYPE_P (current_class_type)) 17626 { 17627 compound_stmt = begin_compound_stmt (0); 17628 /* Make all virtual function table pointers in non-virtual base 17629 classes point to CURRENT_CLASS_TYPE's virtual function 17630 tables. */ 17631 initialize_vtbl_ptrs (current_class_ptr); 17632 finish_compound_stmt (compound_stmt); 17633 17634 if (flag_lifetime_dse 17635 /* Clobbering an empty base is harmful if it overlays real data. */ 17636 && !is_empty_class (current_class_type)) 17637 { 17638 if (sanitize_flags_p (SANITIZE_VPTR) 17639 && (flag_sanitize_recover & SANITIZE_VPTR) == 0 17640 && TYPE_CONTAINS_VPTR_P (current_class_type)) 17641 { 17642 tree binfo = TYPE_BINFO (current_class_type); 17643 tree ref 17644 = cp_build_fold_indirect_ref (current_class_ptr); 17645 17646 tree vtbl_ptr = build_vfield_ref (ref, TREE_TYPE (binfo)); 17647 tree vtbl = build_zero_cst (TREE_TYPE (vtbl_ptr)); 17648 tree stmt = cp_build_modify_expr (input_location, vtbl_ptr, 17649 NOP_EXPR, vtbl, 17650 tf_warning_or_error); 17651 /* If the vptr is shared with some virtual nearly empty base, 17652 don't clear it if not in charge, the dtor of the virtual 17653 nearly empty base will do that later. */ 17654 if (CLASSTYPE_VBASECLASSES (current_class_type)) 17655 { 17656 tree c = current_class_type; 17657 while (CLASSTYPE_PRIMARY_BINFO (c)) 17658 { 17659 if (BINFO_VIRTUAL_P (CLASSTYPE_PRIMARY_BINFO (c))) 17660 { 17661 stmt = convert_to_void (stmt, ICV_STATEMENT, 17662 tf_warning_or_error); 17663 stmt = build_if_in_charge (stmt); 17664 break; 17665 } 17666 c = BINFO_TYPE (CLASSTYPE_PRIMARY_BINFO (c)); 17667 } 17668 } 17669 finish_decl_cleanup (NULL_TREE, stmt); 17670 } 17671 else 17672 finish_decl_cleanup (NULL_TREE, build_clobber_this ()); 17673 } 17674 17675 /* And insert cleanups for our bases and members so that they 17676 will be properly destroyed if we throw. */ 17677 push_base_cleanups (); 17678 } 17679 } 17680 17681 /* At the end of every destructor we generate code to delete the object if 17682 necessary. Do that now. */ 17683 17684 static void 17685 finish_destructor_body (void) 17686 { 17687 tree exprstmt; 17688 17689 /* Any return from a destructor will end up here; that way all base 17690 and member cleanups will be run when the function returns. */ 17691 add_stmt (build_stmt (input_location, LABEL_EXPR, cdtor_label)); 17692 17693 if (targetm.cxx.cdtor_returns_this ()) 17694 { 17695 tree val; 17696 17697 val = DECL_ARGUMENTS (current_function_decl); 17698 suppress_warning (val, OPT_Wuse_after_free); 17699 val = build2 (MODIFY_EXPR, TREE_TYPE (val), 17700 DECL_RESULT (current_function_decl), val); 17701 /* Return the address of the object. */ 17702 exprstmt = build_stmt (input_location, RETURN_EXPR, val); 17703 add_stmt (exprstmt); 17704 } 17705 } 17706 17707 /* Do the necessary processing for the beginning of a function body, which 17708 in this case includes member-initializers, but not the catch clauses of 17709 a function-try-block. Currently, this means opening a binding level 17710 for the member-initializers (in a ctor), member cleanups (in a dtor), 17711 and capture proxies (in a lambda operator()). */ 17712 17713 tree 17714 begin_function_body (void) 17715 { 17716 if (! FUNCTION_NEEDS_BODY_BLOCK (current_function_decl)) 17717 return NULL_TREE; 17718 17719 if (processing_template_decl) 17720 /* Do nothing now. */; 17721 else 17722 /* Always keep the BLOCK node associated with the outermost pair of 17723 curly braces of a function. These are needed for correct 17724 operation of dwarfout.c. */ 17725 keep_next_level (true); 17726 17727 tree stmt = begin_compound_stmt (BCS_FN_BODY); 17728 17729 if (processing_template_decl) 17730 /* Do nothing now. */; 17731 else if (DECL_DESTRUCTOR_P (current_function_decl)) 17732 begin_destructor_body (); 17733 17734 return stmt; 17735 } 17736 17737 /* Do the processing for the end of a function body. Currently, this means 17738 closing out the cleanups for fully-constructed bases and members, and in 17739 the case of the destructor, deleting the object if desired. Again, this 17740 is only meaningful for [cd]tors, since they are the only functions where 17741 there is a significant distinction between the main body and any 17742 function catch clauses. Handling, say, main() return semantics here 17743 would be wrong, as flowing off the end of a function catch clause for 17744 main() would also need to return 0. */ 17745 17746 void 17747 finish_function_body (tree compstmt) 17748 { 17749 if (compstmt == NULL_TREE) 17750 return; 17751 17752 /* Close the block. */ 17753 finish_compound_stmt (compstmt); 17754 17755 if (processing_template_decl) 17756 /* Do nothing now. */; 17757 else if (DECL_CONSTRUCTOR_P (current_function_decl)) 17758 finish_constructor_body (); 17759 else if (DECL_DESTRUCTOR_P (current_function_decl)) 17760 finish_destructor_body (); 17761 } 17762 17763 /* Given a function, returns the BLOCK corresponding to the outermost level 17764 of curly braces, skipping the artificial block created for constructor 17765 initializers. */ 17766 17767 tree 17768 outer_curly_brace_block (tree fndecl) 17769 { 17770 tree block = DECL_INITIAL (fndecl); 17771 if (BLOCK_OUTER_CURLY_BRACE_P (block)) 17772 return block; 17773 block = BLOCK_SUBBLOCKS (block); 17774 if (BLOCK_OUTER_CURLY_BRACE_P (block)) 17775 return block; 17776 block = BLOCK_SUBBLOCKS (block); 17777 gcc_assert (BLOCK_OUTER_CURLY_BRACE_P (block)); 17778 return block; 17779 } 17780 17781 /* If FNDECL is a class's key method, add the class to the list of 17782 keyed classes that should be emitted. */ 17783 17784 static void 17785 record_key_method_defined (tree fndecl) 17786 { 17787 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fndecl) 17788 && DECL_VIRTUAL_P (fndecl) 17789 && !processing_template_decl) 17790 { 17791 tree fnclass = DECL_CONTEXT (fndecl); 17792 if (fndecl == CLASSTYPE_KEY_METHOD (fnclass)) 17793 vec_safe_push (keyed_classes, fnclass); 17794 } 17795 } 17796 17797 /* Attempt to add a fix-it hint to RICHLOC suggesting the insertion 17798 of "return *this;" immediately before its location, using FNDECL's 17799 first statement (if any) to give the indentation, if appropriate. */ 17800 17801 static void 17802 add_return_star_this_fixit (gcc_rich_location *richloc, tree fndecl) 17803 { 17804 location_t indent = UNKNOWN_LOCATION; 17805 tree stmts = expr_first (DECL_SAVED_TREE (fndecl)); 17806 if (stmts) 17807 indent = EXPR_LOCATION (stmts); 17808 richloc->add_fixit_insert_formatted ("return *this;", 17809 richloc->get_loc (), 17810 indent); 17811 } 17812 17813 /* This function carries out the subset of finish_function operations needed 17814 to emit the compiler-generated outlined helper functions used by the 17815 coroutines implementation. */ 17816 17817 static void 17818 emit_coro_helper (tree helper) 17819 { 17820 /* This is a partial set of the operations done by finish_function() 17821 plus emitting the result. */ 17822 set_cfun (NULL); 17823 current_function_decl = helper; 17824 begin_scope (sk_function_parms, NULL); 17825 store_parm_decls (DECL_ARGUMENTS (helper)); 17826 announce_function (helper); 17827 allocate_struct_function (helper, false); 17828 cfun->language = ggc_cleared_alloc<language_function> (); 17829 poplevel (1, 0, 1); 17830 maybe_save_constexpr_fundef (helper); 17831 /* We must start each function with a clear fold cache. */ 17832 clear_fold_cache (); 17833 cp_fold_function (helper); 17834 DECL_CONTEXT (DECL_RESULT (helper)) = helper; 17835 BLOCK_SUPERCONTEXT (DECL_INITIAL (helper)) = helper; 17836 /* This function has coroutine IFNs that we should handle in middle 17837 end lowering. */ 17838 cfun->coroutine_component = true; 17839 cp_genericize (helper); 17840 expand_or_defer_fn (helper); 17841 } 17842 17843 /* Finish up a function declaration and compile that function 17844 all the way to assembler language output. The free the storage 17845 for the function definition. INLINE_P is TRUE if we just 17846 finished processing the body of an in-class inline function 17847 definition. (This processing will have taken place after the 17848 class definition is complete.) */ 17849 17850 tree 17851 finish_function (bool inline_p) 17852 { 17853 tree fndecl = current_function_decl; 17854 tree fntype, ctype = NULL_TREE; 17855 tree resumer = NULL_TREE, destroyer = NULL_TREE; 17856 bool coro_p = flag_coroutines 17857 && !processing_template_decl 17858 && DECL_COROUTINE_P (fndecl); 17859 bool coro_emit_helpers = false; 17860 17861 /* When we get some parse errors, we can end up without a 17862 current_function_decl, so cope. */ 17863 if (fndecl == NULL_TREE) 17864 return error_mark_node; 17865 17866 if (!DECL_OMP_DECLARE_REDUCTION_P (fndecl)) 17867 finish_lambda_scope (); 17868 17869 if (c_dialect_objc ()) 17870 objc_finish_function (); 17871 17872 record_key_method_defined (fndecl); 17873 17874 fntype = TREE_TYPE (fndecl); 17875 17876 /* TREE_READONLY (fndecl) = 1; 17877 This caused &foo to be of type ptr-to-const-function 17878 which then got a warning when stored in a ptr-to-function variable. */ 17879 17880 gcc_assert (building_stmt_list_p ()); 17881 /* The current function is being defined, so its DECL_INITIAL should 17882 be set, and unless there's a multiple definition, it should be 17883 error_mark_node. */ 17884 gcc_assert (DECL_INITIAL (fndecl) == error_mark_node); 17885 17886 if (coro_p) 17887 { 17888 /* Only try to emit the coroutine outlined helper functions if the 17889 transforms succeeded. Otherwise, treat errors in the same way as 17890 a regular function. */ 17891 coro_emit_helpers = morph_fn_to_coro (fndecl, &resumer, &destroyer); 17892 17893 /* We should handle coroutine IFNs in middle end lowering. */ 17894 cfun->coroutine_component = true; 17895 17896 /* Do not try to process the ramp's EH unless outlining succeeded. */ 17897 if (coro_emit_helpers && use_eh_spec_block (fndecl)) 17898 finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS 17899 (TREE_TYPE (fndecl)), 17900 current_eh_spec_block); 17901 } 17902 else 17903 /* For a cloned function, we've already got all the code we need; 17904 there's no need to add any extra bits. */ 17905 if (!DECL_CLONED_FUNCTION_P (fndecl)) 17906 { 17907 /* Make it so that `main' always returns 0 by default. */ 17908 if (DECL_MAIN_P (current_function_decl)) 17909 finish_return_stmt (integer_zero_node); 17910 17911 if (use_eh_spec_block (current_function_decl)) 17912 finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS 17913 (TREE_TYPE (current_function_decl)), 17914 current_eh_spec_block); 17915 } 17916 17917 /* If we're saving up tree structure, tie off the function now. */ 17918 DECL_SAVED_TREE (fndecl) = pop_stmt_list (DECL_SAVED_TREE (fndecl)); 17919 17920 finish_fname_decls (); 17921 17922 /* If this function can't throw any exceptions, remember that. */ 17923 if (!processing_template_decl 17924 && !cp_function_chain->can_throw 17925 && !flag_non_call_exceptions 17926 && !decl_replaceable_p (fndecl, 17927 opt_for_fn (fndecl, flag_semantic_interposition))) 17928 TREE_NOTHROW (fndecl) = 1; 17929 17930 /* This must come after expand_function_end because cleanups might 17931 have declarations (from inline functions) that need to go into 17932 this function's blocks. */ 17933 17934 /* If the current binding level isn't the outermost binding level 17935 for this function, either there is a bug, or we have experienced 17936 syntax errors and the statement tree is malformed. */ 17937 if (current_binding_level->kind != sk_function_parms) 17938 { 17939 /* Make sure we have already experienced errors. */ 17940 gcc_assert (errorcount); 17941 17942 /* Throw away the broken statement tree and extra binding 17943 levels. */ 17944 DECL_SAVED_TREE (fndecl) = alloc_stmt_list (); 17945 17946 while (current_binding_level->kind != sk_function_parms) 17947 { 17948 if (current_binding_level->kind == sk_class) 17949 pop_nested_class (); 17950 else 17951 poplevel (0, 0, 0); 17952 } 17953 } 17954 poplevel (1, 0, 1); 17955 17956 /* Statements should always be full-expressions at the outermost set 17957 of curly braces for a function. */ 17958 gcc_assert (stmts_are_full_exprs_p ()); 17959 17960 /* If there are no return statements in a function with auto return type, 17961 the return type is void. But if the declared type is something like 17962 auto*, this is an error. */ 17963 if (!processing_template_decl && FNDECL_USED_AUTO (fndecl) 17964 && TREE_TYPE (fntype) == DECL_SAVED_AUTO_RETURN_TYPE (fndecl)) 17965 { 17966 if (is_auto (DECL_SAVED_AUTO_RETURN_TYPE (fndecl)) 17967 && !current_function_returns_value 17968 && !current_function_returns_null) 17969 { 17970 /* We haven't applied return type deduction because we haven't 17971 seen any return statements. Do that now. */ 17972 tree node = type_uses_auto (DECL_SAVED_AUTO_RETURN_TYPE (fndecl)); 17973 do_auto_deduction (DECL_SAVED_AUTO_RETURN_TYPE (fndecl), 17974 void_node, node, tf_warning_or_error, 17975 adc_return_type); 17976 17977 apply_deduced_return_type (fndecl, void_type_node); 17978 fntype = TREE_TYPE (fndecl); 17979 } 17980 else if (!current_function_returns_value 17981 && !current_function_returns_null) 17982 { 17983 error ("no return statements in function returning %qT", 17984 DECL_SAVED_AUTO_RETURN_TYPE (fndecl)); 17985 inform (input_location, "only plain %<auto%> return type can be " 17986 "deduced to %<void%>"); 17987 } 17988 } 17989 17990 /* Remember that we were in class scope. */ 17991 if (current_class_name) 17992 ctype = current_class_type; 17993 17994 if (DECL_DELETED_FN (fndecl)) 17995 { 17996 DECL_INITIAL (fndecl) = error_mark_node; 17997 DECL_SAVED_TREE (fndecl) = NULL_TREE; 17998 goto cleanup; 17999 } 18000 18001 // If this is a concept, check that the definition is reasonable. 18002 if (DECL_DECLARED_CONCEPT_P (fndecl)) 18003 check_function_concept (fndecl); 18004 18005 if (flag_openmp) 18006 if (tree attr = lookup_attribute ("omp declare variant base", 18007 DECL_ATTRIBUTES (fndecl))) 18008 omp_declare_variant_finalize (fndecl, attr); 18009 18010 /* Complain if there's just no return statement. */ 18011 if ((warn_return_type 18012 || (cxx_dialect >= cxx14 18013 && DECL_DECLARED_CONSTEXPR_P (fndecl))) 18014 && !VOID_TYPE_P (TREE_TYPE (fntype)) 18015 && !dependent_type_p (TREE_TYPE (fntype)) 18016 && !current_function_returns_value && !current_function_returns_null 18017 /* Don't complain if we abort or throw. */ 18018 && !current_function_returns_abnormally 18019 /* Don't complain if there's an infinite loop. */ 18020 && !current_function_infinite_loop 18021 /* Don't complain if we are declared noreturn. */ 18022 && !TREE_THIS_VOLATILE (fndecl) 18023 && !DECL_NAME (DECL_RESULT (fndecl)) 18024 && !warning_suppressed_p (fndecl, OPT_Wreturn_type) 18025 /* Structor return values (if any) are set by the compiler. */ 18026 && !DECL_CONSTRUCTOR_P (fndecl) 18027 && !DECL_DESTRUCTOR_P (fndecl) 18028 && targetm.warn_func_return (fndecl)) 18029 { 18030 gcc_rich_location richloc (input_location); 18031 /* Potentially add a "return *this;" fix-it hint for 18032 assignment operators. */ 18033 if (IDENTIFIER_ASSIGN_OP_P (DECL_NAME (fndecl))) 18034 { 18035 tree valtype = TREE_TYPE (DECL_RESULT (fndecl)); 18036 if (TREE_CODE (valtype) == REFERENCE_TYPE 18037 && current_class_ref 18038 && same_type_ignoring_top_level_qualifiers_p 18039 (TREE_TYPE (valtype), TREE_TYPE (current_class_ref)) 18040 && global_dc->option_enabled (OPT_Wreturn_type, 18041 global_dc->lang_mask, 18042 global_dc->option_state)) 18043 add_return_star_this_fixit (&richloc, fndecl); 18044 } 18045 if (cxx_dialect >= cxx14 18046 && DECL_DECLARED_CONSTEXPR_P (fndecl)) 18047 error_at (&richloc, "no return statement in %<constexpr%> function " 18048 "returning non-void"); 18049 else if (warning_at (&richloc, OPT_Wreturn_type, 18050 "no return statement in function returning " 18051 "non-void")) 18052 suppress_warning (fndecl, OPT_Wreturn_type); 18053 } 18054 18055 /* Lambda closure members are implicitly constexpr if possible. */ 18056 if (cxx_dialect >= cxx17 18057 && LAMBDA_TYPE_P (CP_DECL_CONTEXT (fndecl))) 18058 DECL_DECLARED_CONSTEXPR_P (fndecl) 18059 = ((processing_template_decl 18060 || is_valid_constexpr_fn (fndecl, /*complain*/false)) 18061 && potential_constant_expression (DECL_SAVED_TREE (fndecl))); 18062 18063 /* Save constexpr function body before it gets munged by 18064 the NRV transformation. */ 18065 maybe_save_constexpr_fundef (fndecl); 18066 18067 /* Invoke the pre-genericize plugin before we start munging things. */ 18068 if (!processing_template_decl) 18069 invoke_plugin_callbacks (PLUGIN_PRE_GENERICIZE, fndecl); 18070 18071 /* Perform delayed folding before NRV transformation. */ 18072 if (!processing_template_decl 18073 && !DECL_IMMEDIATE_FUNCTION_P (fndecl) 18074 && !DECL_OMP_DECLARE_REDUCTION_P (fndecl)) 18075 cp_fold_function (fndecl); 18076 18077 /* Set up the named return value optimization, if we can. Candidate 18078 variables are selected in check_return_expr. */ 18079 if (current_function_return_value) 18080 { 18081 tree r = current_function_return_value; 18082 tree outer; 18083 18084 if (r != error_mark_node 18085 /* This is only worth doing for fns that return in memory--and 18086 simpler, since we don't have to worry about promoted modes. */ 18087 && aggregate_value_p (TREE_TYPE (TREE_TYPE (fndecl)), fndecl) 18088 /* Only allow this for variables declared in the outer scope of 18089 the function so we know that their lifetime always ends with a 18090 return; see g++.dg/opt/nrv6.C. We could be more flexible if 18091 we were to do this optimization in tree-ssa. */ 18092 && (outer = outer_curly_brace_block (fndecl)) 18093 && chain_member (r, BLOCK_VARS (outer))) 18094 finalize_nrv (&DECL_SAVED_TREE (fndecl), r, DECL_RESULT (fndecl)); 18095 18096 current_function_return_value = NULL_TREE; 18097 } 18098 18099 /* Must mark the RESULT_DECL as being in this function. */ 18100 DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl; 18101 18102 /* Set the BLOCK_SUPERCONTEXT of the outermost function scope to point 18103 to the FUNCTION_DECL node itself. */ 18104 BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl; 18105 18106 /* Store the end of the function, so that we get good line number 18107 info for the epilogue. */ 18108 cfun->function_end_locus = input_location; 18109 18110 /* Complain about parameters that are only set, but never otherwise used. */ 18111 if (warn_unused_but_set_parameter 18112 && !processing_template_decl 18113 && errorcount == unused_but_set_errorcount 18114 && !DECL_CLONED_FUNCTION_P (fndecl)) 18115 { 18116 tree decl; 18117 18118 for (decl = DECL_ARGUMENTS (fndecl); 18119 decl; 18120 decl = DECL_CHAIN (decl)) 18121 if (TREE_USED (decl) 18122 && TREE_CODE (decl) == PARM_DECL 18123 && !DECL_READ_P (decl) 18124 && DECL_NAME (decl) 18125 && !DECL_ARTIFICIAL (decl) 18126 && !warning_suppressed_p (decl,OPT_Wunused_but_set_parameter) 18127 && !DECL_IN_SYSTEM_HEADER (decl) 18128 && TREE_TYPE (decl) != error_mark_node 18129 && !TYPE_REF_P (TREE_TYPE (decl)) 18130 && (!CLASS_TYPE_P (TREE_TYPE (decl)) 18131 || !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))) 18132 warning_at (DECL_SOURCE_LOCATION (decl), 18133 OPT_Wunused_but_set_parameter, 18134 "parameter %qD set but not used", decl); 18135 unused_but_set_errorcount = errorcount; 18136 } 18137 18138 /* Complain about locally defined typedefs that are not used in this 18139 function. */ 18140 maybe_warn_unused_local_typedefs (); 18141 18142 /* Possibly warn about unused parameters. */ 18143 if (warn_unused_parameter 18144 && !processing_template_decl 18145 && !DECL_CLONED_FUNCTION_P (fndecl)) 18146 do_warn_unused_parameter (fndecl); 18147 18148 /* Genericize before inlining. */ 18149 if (!processing_template_decl 18150 && !DECL_IMMEDIATE_FUNCTION_P (fndecl) 18151 && !DECL_OMP_DECLARE_REDUCTION_P (fndecl)) 18152 cp_genericize (fndecl); 18153 18154 /* Emit the resumer and destroyer functions now, providing that we have 18155 not encountered some fatal error. */ 18156 if (coro_emit_helpers) 18157 { 18158 emit_coro_helper (resumer); 18159 emit_coro_helper (destroyer); 18160 } 18161 18162 cleanup: 18163 /* We're leaving the context of this function, so zap cfun. It's still in 18164 DECL_STRUCT_FUNCTION, and we'll restore it in tree_rest_of_compilation. */ 18165 set_cfun (NULL); 18166 current_function_decl = NULL; 18167 18168 /* If this is an in-class inline definition, we may have to pop the 18169 bindings for the template parameters that we added in 18170 maybe_begin_member_template_processing when start_function was 18171 called. */ 18172 if (inline_p) 18173 maybe_end_member_template_processing (); 18174 18175 /* Leave the scope of the class. */ 18176 if (ctype) 18177 pop_nested_class (); 18178 18179 --function_depth; 18180 18181 /* Clean up. */ 18182 current_function_decl = NULL_TREE; 18183 18184 invoke_plugin_callbacks (PLUGIN_FINISH_PARSE_FUNCTION, fndecl); 18185 return fndecl; 18186 } 18187 18188 /* Create the FUNCTION_DECL for a function definition. 18190 DECLSPECS and DECLARATOR are the parts of the declaration; 18191 they describe the return type and the name of the function, 18192 but twisted together in a fashion that parallels the syntax of C. 18193 18194 This function creates a binding context for the function body 18195 as well as setting up the FUNCTION_DECL in current_function_decl. 18196 18197 Returns a FUNCTION_DECL on success. 18198 18199 If the DECLARATOR is not suitable for a function (it defines a datum 18200 instead), we return 0, which tells yyparse to report a parse error. 18201 18202 May return void_type_node indicating that this method is actually 18203 a friend. See grokfield for more details. 18204 18205 Came here with a `.pushlevel' . 18206 18207 DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING 18208 CHANGES TO CODE IN `grokfield'. */ 18209 18210 tree 18211 grokmethod (cp_decl_specifier_seq *declspecs, 18212 const cp_declarator *declarator, tree attrlist) 18213 { 18214 tree fndecl = grokdeclarator (declarator, declspecs, MEMFUNCDEF, 0, 18215 &attrlist); 18216 18217 if (fndecl == error_mark_node) 18218 return error_mark_node; 18219 18220 if (attrlist) 18221 cplus_decl_attributes (&fndecl, attrlist, 0); 18222 18223 /* Pass friends other than inline friend functions back. */ 18224 if (fndecl == void_type_node) 18225 return fndecl; 18226 18227 if (DECL_IN_AGGR_P (fndecl)) 18228 { 18229 if (DECL_CLASS_SCOPE_P (fndecl)) 18230 error ("%qD is already defined in class %qT", fndecl, 18231 DECL_CONTEXT (fndecl)); 18232 return error_mark_node; 18233 } 18234 18235 check_template_shadow (fndecl); 18236 18237 /* p1779 ABI-Isolation makes inline not a default for in-class 18238 definitions in named module purview. If the user explicitly 18239 made it inline, grokdeclarator will already have done the right 18240 things. */ 18241 if ((!named_module_purview_p () 18242 || flag_module_implicit_inline 18243 /* Lambda's operator function remains inline. */ 18244 || LAMBDA_TYPE_P (DECL_CONTEXT (fndecl))) 18245 /* If the user explicitly asked for this to be inline, we don't 18246 need to do more, but more importantly we want to warn if we 18247 can't inline it. */ 18248 && !DECL_DECLARED_INLINE_P (fndecl)) 18249 { 18250 if (TREE_PUBLIC (fndecl)) 18251 DECL_COMDAT (fndecl) = 1; 18252 DECL_DECLARED_INLINE_P (fndecl) = 1; 18253 /* It's ok if we can't inline this. */ 18254 DECL_NO_INLINE_WARNING_P (fndecl) = 1; 18255 } 18256 18257 /* We process method specializations in finish_struct_1. */ 18258 if (processing_template_decl && !DECL_TEMPLATE_SPECIALIZATION (fndecl)) 18259 { 18260 /* Avoid calling decl_spec_seq... until we have to. */ 18261 bool friendp = decl_spec_seq_has_spec_p (declspecs, ds_friend); 18262 fndecl = push_template_decl (fndecl, friendp); 18263 if (fndecl == error_mark_node) 18264 return fndecl; 18265 } 18266 18267 if (DECL_CHAIN (fndecl) && !decl_spec_seq_has_spec_p (declspecs, ds_friend)) 18268 { 18269 fndecl = copy_node (fndecl); 18270 TREE_CHAIN (fndecl) = NULL_TREE; 18271 } 18272 18273 cp_finish_decl (fndecl, NULL_TREE, false, NULL_TREE, 0); 18274 18275 DECL_IN_AGGR_P (fndecl) = 1; 18276 return fndecl; 18277 } 18278 18279 18281 /* VAR is a VAR_DECL. If its type is incomplete, remember VAR so that 18282 we can lay it out later, when and if its type becomes complete. 18283 18284 Also handle constexpr variables where the initializer involves 18285 an unlowered PTRMEM_CST because the class isn't complete yet. */ 18286 18287 void 18288 maybe_register_incomplete_var (tree var) 18289 { 18290 gcc_assert (VAR_P (var)); 18291 18292 /* Keep track of variables with incomplete types. */ 18293 if (!processing_template_decl && TREE_TYPE (var) != error_mark_node 18294 && DECL_EXTERNAL (var)) 18295 { 18296 tree inner_type = TREE_TYPE (var); 18297 18298 while (TREE_CODE (inner_type) == ARRAY_TYPE) 18299 inner_type = TREE_TYPE (inner_type); 18300 inner_type = TYPE_MAIN_VARIANT (inner_type); 18301 18302 if ((!COMPLETE_TYPE_P (inner_type) && CLASS_TYPE_P (inner_type)) 18303 /* RTTI TD entries are created while defining the type_info. */ 18304 || (TYPE_LANG_SPECIFIC (inner_type) 18305 && TYPE_BEING_DEFINED (inner_type))) 18306 { 18307 incomplete_var iv = {var, inner_type}; 18308 vec_safe_push (incomplete_vars, iv); 18309 } 18310 else if (!(DECL_LANG_SPECIFIC (var) && DECL_TEMPLATE_INFO (var)) 18311 && decl_constant_var_p (var) 18312 && (TYPE_PTRMEM_P (inner_type) || CLASS_TYPE_P (inner_type))) 18313 { 18314 /* When the outermost open class is complete we can resolve any 18315 pointers-to-members. */ 18316 tree context = outermost_open_class (); 18317 incomplete_var iv = {var, context}; 18318 vec_safe_push (incomplete_vars, iv); 18319 } 18320 } 18321 } 18322 18323 /* Called when a class type (given by TYPE) is defined. If there are 18324 any existing VAR_DECLs whose type has been completed by this 18325 declaration, update them now. */ 18326 18327 void 18328 complete_vars (tree type) 18329 { 18330 unsigned ix; 18331 incomplete_var *iv; 18332 18333 for (ix = 0; vec_safe_iterate (incomplete_vars, ix, &iv); ) 18334 { 18335 if (same_type_p (type, iv->incomplete_type)) 18336 { 18337 tree var = iv->decl; 18338 tree type = TREE_TYPE (var); 18339 18340 if (type != error_mark_node 18341 && (TYPE_MAIN_VARIANT (strip_array_types (type)) 18342 == iv->incomplete_type)) 18343 { 18344 /* Complete the type of the variable. */ 18345 complete_type (type); 18346 cp_apply_type_quals_to_decl (cp_type_quals (type), var); 18347 if (COMPLETE_TYPE_P (type)) 18348 layout_var_decl (var); 18349 } 18350 18351 /* Remove this entry from the list. */ 18352 incomplete_vars->unordered_remove (ix); 18353 } 18354 else 18355 ix++; 18356 } 18357 } 18358 18359 /* If DECL is of a type which needs a cleanup, build and return an 18360 expression to perform that cleanup here. Return NULL_TREE if no 18361 cleanup need be done. DECL can also be a _REF when called from 18362 split_nonconstant_init_1. */ 18363 18364 tree 18365 cxx_maybe_build_cleanup (tree decl, tsubst_flags_t complain) 18366 { 18367 tree type; 18368 tree attr; 18369 tree cleanup; 18370 18371 /* Assume no cleanup is required. */ 18372 cleanup = NULL_TREE; 18373 18374 if (error_operand_p (decl)) 18375 return cleanup; 18376 18377 /* Handle "__attribute__((cleanup))". We run the cleanup function 18378 before the destructor since the destructor is what actually 18379 terminates the lifetime of the object. */ 18380 if (DECL_P (decl)) 18381 attr = lookup_attribute ("cleanup", DECL_ATTRIBUTES (decl)); 18382 else 18383 attr = NULL_TREE; 18384 if (attr) 18385 { 18386 tree id; 18387 tree fn; 18388 tree arg; 18389 18390 /* Get the name specified by the user for the cleanup function. */ 18391 id = TREE_VALUE (TREE_VALUE (attr)); 18392 /* Look up the name to find the cleanup function to call. It is 18393 important to use lookup_name here because that is what is 18394 used in c-common.cc:handle_cleanup_attribute when performing 18395 initial checks on the attribute. Note that those checks 18396 include ensuring that the function found is not an overloaded 18397 function, or an object with an overloaded call operator, 18398 etc.; we can rely on the fact that the function found is an 18399 ordinary FUNCTION_DECL. */ 18400 fn = lookup_name (id); 18401 arg = build_address (decl); 18402 if (!mark_used (decl, complain) && !(complain & tf_error)) 18403 return error_mark_node; 18404 cleanup = cp_build_function_call_nary (fn, complain, arg, NULL_TREE); 18405 if (cleanup == error_mark_node) 18406 return error_mark_node; 18407 } 18408 /* Handle ordinary C++ destructors. */ 18409 type = TREE_TYPE (decl); 18410 if (type_build_dtor_call (type)) 18411 { 18412 int flags = LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR; 18413 tree addr; 18414 tree call; 18415 18416 if (TREE_CODE (type) == ARRAY_TYPE) 18417 addr = decl; 18418 else 18419 addr = build_address (decl); 18420 18421 call = build_delete (input_location, TREE_TYPE (addr), addr, 18422 sfk_complete_destructor, flags, 0, complain); 18423 if (call == error_mark_node) 18424 cleanup = error_mark_node; 18425 else if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type)) 18426 /* Discard the call. */; 18427 else if (decl_maybe_constant_destruction (decl, type) 18428 && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl)) 18429 cxx_constant_dtor (call, decl); 18430 else if (cleanup) 18431 cleanup = cp_build_compound_expr (cleanup, call, complain); 18432 else 18433 cleanup = call; 18434 } 18435 18436 /* build_delete sets the location of the destructor call to the 18437 current location, even though the destructor is going to be 18438 called later, at the end of the current scope. This can lead to 18439 a "jumpy" behavior for users of debuggers when they step around 18440 the end of the block. So let's unset the location of the 18441 destructor call instead. */ 18442 protected_set_expr_location (cleanup, UNKNOWN_LOCATION); 18443 if (cleanup && CONVERT_EXPR_P (cleanup)) 18444 protected_set_expr_location (TREE_OPERAND (cleanup, 0), UNKNOWN_LOCATION); 18445 18446 if (cleanup 18447 && DECL_P (decl) 18448 && !lookup_attribute ("warn_unused", TYPE_ATTRIBUTES (TREE_TYPE (decl))) 18449 /* Treat objects with destructors as used; the destructor may do 18450 something substantive. */ 18451 && !mark_used (decl, complain) && !(complain & tf_error)) 18452 return error_mark_node; 18453 18454 if (cleanup && cfun && !processing_template_decl 18455 && !expr_noexcept_p (cleanup, tf_none)) 18456 cp_function_chain->throwing_cleanup = true; 18457 18458 return cleanup; 18459 } 18460 18461 18462 /* Return the FUNCTION_TYPE that corresponds to MEMFNTYPE, which can be a 18464 FUNCTION_DECL, METHOD_TYPE, FUNCTION_TYPE, pointer or reference to 18465 METHOD_TYPE or FUNCTION_TYPE, or pointer to member function. */ 18466 18467 tree 18468 static_fn_type (tree memfntype) 18469 { 18470 tree fntype; 18471 tree args; 18472 18473 if (TYPE_PTRMEMFUNC_P (memfntype)) 18474 memfntype = TYPE_PTRMEMFUNC_FN_TYPE (memfntype); 18475 if (INDIRECT_TYPE_P (memfntype) 18476 || TREE_CODE (memfntype) == FUNCTION_DECL) 18477 memfntype = TREE_TYPE (memfntype); 18478 if (TREE_CODE (memfntype) == FUNCTION_TYPE) 18479 return memfntype; 18480 gcc_assert (TREE_CODE (memfntype) == METHOD_TYPE); 18481 args = TYPE_ARG_TYPES (memfntype); 18482 fntype = build_function_type (TREE_TYPE (memfntype), TREE_CHAIN (args)); 18483 fntype = apply_memfn_quals (fntype, type_memfn_quals (memfntype)); 18484 fntype = (cp_build_type_attribute_variant 18485 (fntype, TYPE_ATTRIBUTES (memfntype))); 18486 fntype = cxx_copy_lang_qualifiers (fntype, memfntype); 18487 return fntype; 18488 } 18489 18490 /* DECL was originally constructed as a non-static member function, 18491 but turned out to be static. Update it accordingly. */ 18492 18493 void 18494 revert_static_member_fn (tree decl) 18495 { 18496 tree stype = static_fn_type (decl); 18497 cp_cv_quals quals = type_memfn_quals (stype); 18498 cp_ref_qualifier rqual = type_memfn_rqual (stype); 18499 18500 if (quals != TYPE_UNQUALIFIED || rqual != REF_QUAL_NONE) 18501 stype = apply_memfn_quals (stype, TYPE_UNQUALIFIED, REF_QUAL_NONE); 18502 18503 TREE_TYPE (decl) = stype; 18504 18505 if (DECL_ARGUMENTS (decl)) 18506 DECL_ARGUMENTS (decl) = DECL_CHAIN (DECL_ARGUMENTS (decl)); 18507 DECL_STATIC_FUNCTION_P (decl) = 1; 18508 } 18509 18510 /* Return which tree structure is used by T, or TS_CP_GENERIC if T is 18511 one of the language-independent trees. */ 18512 18513 enum cp_tree_node_structure_enum 18514 cp_tree_node_structure (union lang_tree_node * t) 18515 { 18516 switch (TREE_CODE (&t->generic)) 18517 { 18518 case ARGUMENT_PACK_SELECT: return TS_CP_ARGUMENT_PACK_SELECT; 18519 case BASELINK: return TS_CP_BASELINK; 18520 case CONSTRAINT_INFO: return TS_CP_CONSTRAINT_INFO; 18521 case DEFERRED_NOEXCEPT: return TS_CP_DEFERRED_NOEXCEPT; 18522 case DEFERRED_PARSE: return TS_CP_DEFERRED_PARSE; 18523 case IDENTIFIER_NODE: return TS_CP_IDENTIFIER; 18524 case LAMBDA_EXPR: return TS_CP_LAMBDA_EXPR; 18525 case BINDING_VECTOR: return TS_CP_BINDING_VECTOR; 18526 case OVERLOAD: return TS_CP_OVERLOAD; 18527 case PTRMEM_CST: return TS_CP_PTRMEM; 18528 case STATIC_ASSERT: return TS_CP_STATIC_ASSERT; 18529 case TEMPLATE_DECL: return TS_CP_TEMPLATE_DECL; 18530 case TEMPLATE_INFO: return TS_CP_TEMPLATE_INFO; 18531 case TEMPLATE_PARM_INDEX: return TS_CP_TPI; 18532 case TRAIT_EXPR: return TS_CP_TRAIT_EXPR; 18533 case USERDEF_LITERAL: return TS_CP_USERDEF_LITERAL; 18534 default: return TS_CP_GENERIC; 18535 } 18536 } 18537 18538 /* Build the void_list_node (void_type_node having been created). */ 18539 tree 18540 build_void_list_node (void) 18541 { 18542 tree t = build_tree_list (NULL_TREE, void_type_node); 18543 return t; 18544 } 18545 18546 bool 18547 cp_missing_noreturn_ok_p (tree decl) 18548 { 18549 /* A missing noreturn is ok for the `main' function. */ 18550 return DECL_MAIN_P (decl); 18551 } 18552 18553 /* Return the decl used to identify the COMDAT group into which DECL should 18554 be placed. */ 18555 18556 tree 18557 cxx_comdat_group (tree decl) 18558 { 18559 /* Virtual tables, construction virtual tables, and virtual table 18560 tables all go in a single COMDAT group, named after the primary 18561 virtual table. */ 18562 if (VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl)) 18563 decl = CLASSTYPE_VTABLES (DECL_CONTEXT (decl)); 18564 /* For all other DECLs, the COMDAT group is the mangled name of the 18565 declaration itself. */ 18566 else 18567 { 18568 while (DECL_THUNK_P (decl)) 18569 { 18570 /* If TARGET_USE_LOCAL_THUNK_ALIAS_P, use_thunk puts the thunk 18571 into the same section as the target function. In that case 18572 we must return target's name. */ 18573 tree target = THUNK_TARGET (decl); 18574 if (TARGET_USE_LOCAL_THUNK_ALIAS_P (target) 18575 && DECL_SECTION_NAME (target) != NULL 18576 && DECL_ONE_ONLY (target)) 18577 decl = target; 18578 else 18579 break; 18580 } 18581 } 18582 18583 return decl; 18584 } 18585 18586 /* Returns the return type for FN as written by the user, which may include 18587 a placeholder for a deduced return type. */ 18588 18589 tree 18590 fndecl_declared_return_type (tree fn) 18591 { 18592 fn = STRIP_TEMPLATE (fn); 18593 if (FNDECL_USED_AUTO (fn)) 18594 return DECL_SAVED_AUTO_RETURN_TYPE (fn); 18595 18596 return TREE_TYPE (TREE_TYPE (fn)); 18597 } 18598 18599 /* Returns true iff DECL is a variable or function declared with an auto type 18600 that has not yet been deduced to a real type. */ 18601 18602 bool 18603 undeduced_auto_decl (tree decl) 18604 { 18605 if (cxx_dialect < cxx11) 18606 return false; 18607 STRIP_ANY_LOCATION_WRAPPER (decl); 18608 return ((VAR_OR_FUNCTION_DECL_P (decl) 18609 || TREE_CODE (decl) == TEMPLATE_DECL) 18610 && type_uses_auto (TREE_TYPE (decl))); 18611 } 18612 18613 /* Complain if DECL has an undeduced return type. */ 18614 18615 bool 18616 require_deduced_type (tree decl, tsubst_flags_t complain) 18617 { 18618 if (undeduced_auto_decl (decl)) 18619 { 18620 if (warning_suppressed_p (decl) && seen_error ()) 18621 /* We probably already complained about deduction failure. */; 18622 else if (complain & tf_error) 18623 error ("use of %qD before deduction of %<auto%>", decl); 18624 note_failed_type_completion_for_satisfaction (decl); 18625 return false; 18626 } 18627 return true; 18628 } 18629 18630 /* Create a representation of the explicit-specifier with 18631 constant-expression of EXPR. COMPLAIN is as for tsubst. */ 18632 18633 tree 18634 build_explicit_specifier (tree expr, tsubst_flags_t complain) 18635 { 18636 if (check_for_bare_parameter_packs (expr)) 18637 return error_mark_node; 18638 18639 if (instantiation_dependent_expression_p (expr)) 18640 /* Wait for instantiation, tsubst_function_decl will handle it. */ 18641 return expr; 18642 18643 expr = build_converted_constant_bool_expr (expr, complain); 18644 expr = instantiate_non_dependent_expr_sfinae (expr, complain); 18645 expr = cxx_constant_value (expr); 18646 return expr; 18647 } 18648 18649 #include "gt-cp-decl.h" 18650