1 /* Definitions for C++ name lookup routines. 2 Copyright (C) 2003-2019 Free Software Foundation, Inc. 3 Contributed by Gabriel Dos Reis <gdr@integrable-solutions.net> 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 #include "config.h" 22 #define INCLUDE_UNIQUE_PTR 23 #include "system.h" 24 #include "coretypes.h" 25 #include "cp-tree.h" 26 #include "timevar.h" 27 #include "stringpool.h" 28 #include "print-tree.h" 29 #include "attribs.h" 30 #include "debug.h" 31 #include "c-family/c-pragma.h" 32 #include "params.h" 33 #include "gcc-rich-location.h" 34 #include "spellcheck-tree.h" 35 #include "parser.h" 36 #include "c-family/name-hint.h" 37 #include "c-family/known-headers.h" 38 #include "c-family/c-spellcheck.h" 39 40 static cxx_binding *cxx_binding_make (tree value, tree type); 41 static cp_binding_level *innermost_nonclass_level (void); 42 static void set_identifier_type_value_with_scope (tree id, tree decl, 43 cp_binding_level *b); 44 static name_hint maybe_suggest_missing_std_header (location_t location, 45 tree name); 46 static name_hint suggest_alternatives_for_1 (location_t location, tree name, 47 bool suggest_misspellings); 48 49 /* Create an overload suitable for recording an artificial TYPE_DECL 50 and another decl. We use this machanism to implement the struct 51 stat hack within a namespace. It'd be nice to use it everywhere. */ 52 53 #define STAT_HACK_P(N) ((N) && TREE_CODE (N) == OVERLOAD && OVL_LOOKUP_P (N)) 54 #define STAT_TYPE(N) TREE_TYPE (N) 55 #define STAT_DECL(N) OVL_FUNCTION (N) 56 #define MAYBE_STAT_DECL(N) (STAT_HACK_P (N) ? STAT_DECL (N) : N) 57 #define MAYBE_STAT_TYPE(N) (STAT_HACK_P (N) ? STAT_TYPE (N) : NULL_TREE) 58 59 /* Create a STAT_HACK node with DECL as the value binding and TYPE as 60 the type binding. */ 61 62 static tree 63 stat_hack (tree decl = NULL_TREE, tree type = NULL_TREE) 64 { 65 tree result = make_node (OVERLOAD); 66 67 /* Mark this as a lookup, so we can tell this is a stat hack. */ 68 OVL_LOOKUP_P (result) = true; 69 STAT_DECL (result) = decl; 70 STAT_TYPE (result) = type; 71 return result; 72 } 73 74 /* Create a local binding level for NAME. */ 75 76 static cxx_binding * 77 create_local_binding (cp_binding_level *level, tree name) 78 { 79 cxx_binding *binding = cxx_binding_make (NULL, NULL); 80 81 INHERITED_VALUE_BINDING_P (binding) = false; 82 LOCAL_BINDING_P (binding) = true; 83 binding->scope = level; 84 binding->previous = IDENTIFIER_BINDING (name); 85 86 IDENTIFIER_BINDING (name) = binding; 87 88 return binding; 89 } 90 91 /* Find the binding for NAME in namespace NS. If CREATE_P is true, 92 make an empty binding if there wasn't one. */ 93 94 static tree * 95 find_namespace_slot (tree ns, tree name, bool create_p = false) 96 { 97 tree *slot = DECL_NAMESPACE_BINDINGS (ns) 98 ->find_slot_with_hash (name, name ? IDENTIFIER_HASH_VALUE (name) : 0, 99 create_p ? INSERT : NO_INSERT); 100 return slot; 101 } 102 103 static tree 104 find_namespace_value (tree ns, tree name) 105 { 106 tree *b = find_namespace_slot (ns, name); 107 108 return b ? MAYBE_STAT_DECL (*b) : NULL_TREE; 109 } 110 111 /* Add DECL to the list of things declared in B. */ 112 113 static void 114 add_decl_to_level (cp_binding_level *b, tree decl) 115 { 116 gcc_assert (b->kind != sk_class); 117 118 /* Make sure we don't create a circular list. xref_tag can end 119 up pushing the same artificial decl more than once. We 120 should have already detected that in update_binding. */ 121 gcc_assert (b->names != decl); 122 123 /* We build up the list in reverse order, and reverse it later if 124 necessary. */ 125 TREE_CHAIN (decl) = b->names; 126 b->names = decl; 127 128 /* If appropriate, add decl to separate list of statics. We 129 include extern variables because they might turn out to be 130 static later. It's OK for this list to contain a few false 131 positives. */ 132 if (b->kind == sk_namespace 133 && ((VAR_P (decl) 134 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl))) 135 || (TREE_CODE (decl) == FUNCTION_DECL 136 && (!TREE_PUBLIC (decl) 137 || decl_anon_ns_mem_p (decl) 138 || DECL_DECLARED_INLINE_P (decl))))) 139 vec_safe_push (static_decls, decl); 140 } 141 142 /* Find the binding for NAME in the local binding level B. */ 143 144 static cxx_binding * 145 find_local_binding (cp_binding_level *b, tree name) 146 { 147 if (cxx_binding *binding = IDENTIFIER_BINDING (name)) 148 for (;; b = b->level_chain) 149 { 150 if (binding->scope == b) 151 return binding; 152 153 /* Cleanup contours are transparent to the language. */ 154 if (b->kind != sk_cleanup) 155 break; 156 } 157 return NULL; 158 } 159 160 struct name_lookup 161 { 162 public: 163 typedef std::pair<tree, tree> using_pair; 164 typedef vec<using_pair, va_heap, vl_embed> using_queue; 165 166 public: 167 tree name; /* The identifier being looked for. */ 168 tree value; /* A (possibly ambiguous) set of things found. */ 169 tree type; /* A type that has been found. */ 170 int flags; /* Lookup flags. */ 171 bool deduping; /* Full deduping is needed because using declarations 172 are in play. */ 173 vec<tree, va_heap, vl_embed> *scopes; 174 name_lookup *previous; /* Previously active lookup. */ 175 176 protected: 177 /* Marked scope stack for outermost name lookup. */ 178 static vec<tree, va_heap, vl_embed> *shared_scopes; 179 /* Currently active lookup. */ 180 static name_lookup *active; 181 182 public: 183 name_lookup (tree n, int f = 0) 184 : name (n), value (NULL_TREE), type (NULL_TREE), flags (f), 185 deduping (false), scopes (NULL), previous (NULL) 186 { 187 preserve_state (); 188 } 189 ~name_lookup () 190 { 191 restore_state (); 192 } 193 194 private: /* Uncopyable, unmovable, unassignable. I am a rock. */ 195 name_lookup (const name_lookup &); 196 name_lookup &operator= (const name_lookup &); 197 198 protected: 199 static bool seen_p (tree scope) 200 { 201 return LOOKUP_SEEN_P (scope); 202 } 203 static bool found_p (tree scope) 204 { 205 return LOOKUP_FOUND_P (scope); 206 } 207 208 void mark_seen (tree scope); /* Mark and add to scope vector. */ 209 static void mark_found (tree scope) 210 { 211 gcc_checking_assert (seen_p (scope)); 212 LOOKUP_FOUND_P (scope) = true; 213 } 214 bool see_and_mark (tree scope) 215 { 216 bool ret = seen_p (scope); 217 if (!ret) 218 mark_seen (scope); 219 return ret; 220 } 221 bool find_and_mark (tree scope); 222 223 private: 224 void preserve_state (); 225 void restore_state (); 226 227 private: 228 static tree ambiguous (tree thing, tree current); 229 void add_overload (tree fns); 230 void add_value (tree new_val); 231 void add_type (tree new_type); 232 bool process_binding (tree val_bind, tree type_bind); 233 234 /* Look in only namespace. */ 235 bool search_namespace_only (tree scope); 236 /* Look in namespace and its (recursive) inlines. Ignore using 237 directives. Return true if something found (inc dups). */ 238 bool search_namespace (tree scope); 239 /* Look in the using directives of namespace + inlines using 240 qualified lookup rules. */ 241 bool search_usings (tree scope); 242 243 private: 244 using_queue *queue_namespace (using_queue *queue, int depth, tree scope); 245 using_queue *do_queue_usings (using_queue *queue, int depth, 246 vec<tree, va_gc> *usings); 247 using_queue *queue_usings (using_queue *queue, int depth, 248 vec<tree, va_gc> *usings) 249 { 250 if (usings) 251 queue = do_queue_usings (queue, depth, usings); 252 return queue; 253 } 254 255 private: 256 void add_fns (tree); 257 258 void adl_expr (tree); 259 void adl_type (tree); 260 void adl_template_arg (tree); 261 void adl_class (tree); 262 void adl_bases (tree); 263 void adl_class_only (tree); 264 void adl_namespace (tree); 265 void adl_namespace_only (tree); 266 267 public: 268 /* Search namespace + inlines + maybe usings as qualified lookup. */ 269 bool search_qualified (tree scope, bool usings = true); 270 271 /* Search namespace + inlines + usings as unqualified lookup. */ 272 bool search_unqualified (tree scope, cp_binding_level *); 273 274 /* ADL lookup of ARGS. */ 275 tree search_adl (tree fns, vec<tree, va_gc> *args); 276 }; 277 278 /* Scope stack shared by all outermost lookups. This avoids us 279 allocating and freeing on every single lookup. */ 280 vec<tree, va_heap, vl_embed> *name_lookup::shared_scopes; 281 282 /* Currently active lookup. */ 283 name_lookup *name_lookup::active; 284 285 /* Name lookup is recursive, becase ADL can cause template 286 instatiation. This is of course a rare event, so we optimize for 287 it not happening. When we discover an active name-lookup, which 288 must be an ADL lookup, we need to unmark the marked scopes and also 289 unmark the lookup we might have been accumulating. */ 290 291 void 292 name_lookup::preserve_state () 293 { 294 previous = active; 295 if (previous) 296 { 297 unsigned length = vec_safe_length (previous->scopes); 298 vec_safe_reserve (previous->scopes, length * 2); 299 for (unsigned ix = length; ix--;) 300 { 301 tree decl = (*previous->scopes)[ix]; 302 303 gcc_checking_assert (LOOKUP_SEEN_P (decl)); 304 LOOKUP_SEEN_P (decl) = false; 305 306 /* Preserve the FOUND_P state on the interrupted lookup's 307 stack. */ 308 if (LOOKUP_FOUND_P (decl)) 309 { 310 LOOKUP_FOUND_P (decl) = false; 311 previous->scopes->quick_push (decl); 312 } 313 } 314 315 /* Unmark the outer partial lookup. */ 316 if (previous->deduping) 317 lookup_mark (previous->value, false); 318 } 319 else 320 scopes = shared_scopes; 321 active = this; 322 } 323 324 /* Restore the marking state of a lookup we interrupted. */ 325 326 void 327 name_lookup::restore_state () 328 { 329 if (deduping) 330 lookup_mark (value, false); 331 332 /* Unmark and empty this lookup's scope stack. */ 333 for (unsigned ix = vec_safe_length (scopes); ix--;) 334 { 335 tree decl = scopes->pop (); 336 gcc_checking_assert (LOOKUP_SEEN_P (decl)); 337 LOOKUP_SEEN_P (decl) = false; 338 LOOKUP_FOUND_P (decl) = false; 339 } 340 341 active = previous; 342 if (previous) 343 { 344 free (scopes); 345 346 unsigned length = vec_safe_length (previous->scopes); 347 for (unsigned ix = 0; ix != length; ix++) 348 { 349 tree decl = (*previous->scopes)[ix]; 350 if (LOOKUP_SEEN_P (decl)) 351 { 352 /* The remainder of the scope stack must be recording 353 FOUND_P decls, which we want to pop off. */ 354 do 355 { 356 tree decl = previous->scopes->pop (); 357 gcc_checking_assert (LOOKUP_SEEN_P (decl) 358 && !LOOKUP_FOUND_P (decl)); 359 LOOKUP_FOUND_P (decl) = true; 360 } 361 while (++ix != length); 362 break; 363 } 364 365 gcc_checking_assert (!LOOKUP_FOUND_P (decl)); 366 LOOKUP_SEEN_P (decl) = true; 367 } 368 369 /* Remark the outer partial lookup. */ 370 if (previous->deduping) 371 lookup_mark (previous->value, true); 372 } 373 else 374 shared_scopes = scopes; 375 } 376 377 void 378 name_lookup::mark_seen (tree scope) 379 { 380 gcc_checking_assert (!seen_p (scope)); 381 LOOKUP_SEEN_P (scope) = true; 382 vec_safe_push (scopes, scope); 383 } 384 385 bool 386 name_lookup::find_and_mark (tree scope) 387 { 388 bool result = LOOKUP_FOUND_P (scope); 389 if (!result) 390 { 391 LOOKUP_FOUND_P (scope) = true; 392 if (!LOOKUP_SEEN_P (scope)) 393 vec_safe_push (scopes, scope); 394 } 395 396 return result; 397 } 398 399 /* THING and CURRENT are ambiguous, concatenate them. */ 400 401 tree 402 name_lookup::ambiguous (tree thing, tree current) 403 { 404 if (TREE_CODE (current) != TREE_LIST) 405 { 406 current = build_tree_list (NULL_TREE, current); 407 TREE_TYPE (current) = error_mark_node; 408 } 409 current = tree_cons (NULL_TREE, thing, current); 410 TREE_TYPE (current) = error_mark_node; 411 412 return current; 413 } 414 415 /* FNS is a new overload set to add to the exising set. */ 416 417 void 418 name_lookup::add_overload (tree fns) 419 { 420 if (!deduping && TREE_CODE (fns) == OVERLOAD) 421 { 422 tree probe = fns; 423 if (flags & LOOKUP_HIDDEN) 424 probe = ovl_skip_hidden (probe); 425 if (probe && TREE_CODE (probe) == OVERLOAD 426 && OVL_DEDUP_P (probe)) 427 { 428 /* We're about to add something found by a using 429 declaration, so need to engage deduping mode. */ 430 lookup_mark (value, true); 431 deduping = true; 432 } 433 } 434 435 value = lookup_maybe_add (fns, value, deduping); 436 } 437 438 /* Add a NEW_VAL, a found value binding into the current value binding. */ 439 440 void 441 name_lookup::add_value (tree new_val) 442 { 443 if (OVL_P (new_val) && (!value || OVL_P (value))) 444 add_overload (new_val); 445 else if (!value) 446 value = new_val; 447 else if (value == new_val) 448 ; 449 else if ((TREE_CODE (value) == TYPE_DECL 450 && TREE_CODE (new_val) == TYPE_DECL 451 && same_type_p (TREE_TYPE (value), TREE_TYPE (new_val)))) 452 /* Typedefs to the same type. */; 453 else if (TREE_CODE (value) == NAMESPACE_DECL 454 && TREE_CODE (new_val) == NAMESPACE_DECL 455 && ORIGINAL_NAMESPACE (value) == ORIGINAL_NAMESPACE (new_val)) 456 /* Namespace (possibly aliased) to the same namespace. Locate 457 the namespace*/ 458 value = ORIGINAL_NAMESPACE (value); 459 else 460 { 461 if (deduping) 462 { 463 /* Disengage deduping mode. */ 464 lookup_mark (value, false); 465 deduping = false; 466 } 467 value = ambiguous (new_val, value); 468 } 469 } 470 471 /* Add a NEW_TYPE, a found type binding into the current type binding. */ 472 473 void 474 name_lookup::add_type (tree new_type) 475 { 476 if (!type) 477 type = new_type; 478 else if (TREE_CODE (type) == TREE_LIST 479 || !same_type_p (TREE_TYPE (type), TREE_TYPE (new_type))) 480 type = ambiguous (new_type, type); 481 } 482 483 /* Process a found binding containing NEW_VAL and NEW_TYPE. Returns 484 true if we actually found something noteworthy. */ 485 486 bool 487 name_lookup::process_binding (tree new_val, tree new_type) 488 { 489 /* Did we really see a type? */ 490 if (new_type 491 && (LOOKUP_NAMESPACES_ONLY (flags) 492 || (!(flags & LOOKUP_HIDDEN) 493 && DECL_LANG_SPECIFIC (new_type) 494 && DECL_ANTICIPATED (new_type)))) 495 new_type = NULL_TREE; 496 497 if (new_val && !(flags & LOOKUP_HIDDEN)) 498 new_val = ovl_skip_hidden (new_val); 499 500 /* Do we really see a value? */ 501 if (new_val) 502 switch (TREE_CODE (new_val)) 503 { 504 case TEMPLATE_DECL: 505 /* If we expect types or namespaces, and not templates, 506 or this is not a template class. */ 507 if ((LOOKUP_QUALIFIERS_ONLY (flags) 508 && !DECL_TYPE_TEMPLATE_P (new_val))) 509 new_val = NULL_TREE; 510 break; 511 case TYPE_DECL: 512 if (LOOKUP_NAMESPACES_ONLY (flags) 513 || (new_type && (flags & LOOKUP_PREFER_TYPES))) 514 new_val = NULL_TREE; 515 break; 516 case NAMESPACE_DECL: 517 if (LOOKUP_TYPES_ONLY (flags)) 518 new_val = NULL_TREE; 519 break; 520 default: 521 if (LOOKUP_QUALIFIERS_ONLY (flags)) 522 new_val = NULL_TREE; 523 } 524 525 if (!new_val) 526 { 527 new_val = new_type; 528 new_type = NULL_TREE; 529 } 530 531 /* Merge into the lookup */ 532 if (new_val) 533 add_value (new_val); 534 if (new_type) 535 add_type (new_type); 536 537 return new_val != NULL_TREE; 538 } 539 540 /* Look in exactly namespace SCOPE. */ 541 542 bool 543 name_lookup::search_namespace_only (tree scope) 544 { 545 bool found = false; 546 547 if (tree *binding = find_namespace_slot (scope, name)) 548 found |= process_binding (MAYBE_STAT_DECL (*binding), 549 MAYBE_STAT_TYPE (*binding)); 550 551 return found; 552 } 553 554 /* Conditionally look in namespace SCOPE and inline children. */ 555 556 bool 557 name_lookup::search_namespace (tree scope) 558 { 559 if (see_and_mark (scope)) 560 /* We've visited this scope before. Return what we found then. */ 561 return found_p (scope); 562 563 /* Look in exactly namespace. */ 564 bool found = search_namespace_only (scope); 565 566 /* Don't look into inline children, if we're looking for an 567 anonymous name -- it must be in the current scope, if anywhere. */ 568 if (name) 569 /* Recursively look in its inline children. */ 570 if (vec<tree, va_gc> *inlinees = DECL_NAMESPACE_INLINEES (scope)) 571 for (unsigned ix = inlinees->length (); ix--;) 572 found |= search_namespace ((*inlinees)[ix]); 573 574 if (found) 575 mark_found (scope); 576 577 return found; 578 } 579 580 /* Recursively follow using directives of SCOPE & its inline children. 581 Such following is essentially a flood-fill algorithm. */ 582 583 bool 584 name_lookup::search_usings (tree scope) 585 { 586 /* We do not check seen_p here, as that was already set during the 587 namespace_only walk. */ 588 if (found_p (scope)) 589 return true; 590 591 bool found = false; 592 if (vec<tree, va_gc> *usings = DECL_NAMESPACE_USING (scope)) 593 for (unsigned ix = usings->length (); ix--;) 594 found |= search_qualified ((*usings)[ix], true); 595 596 /* Look in its inline children. */ 597 if (vec<tree, va_gc> *inlinees = DECL_NAMESPACE_INLINEES (scope)) 598 for (unsigned ix = inlinees->length (); ix--;) 599 found |= search_usings ((*inlinees)[ix]); 600 601 if (found) 602 mark_found (scope); 603 604 return found; 605 } 606 607 /* Qualified namespace lookup in SCOPE. 608 1) Look in SCOPE (+inlines). If found, we're done. 609 2) Otherwise, if USINGS is true, 610 recurse for every using directive of SCOPE (+inlines). 611 612 Trickiness is (a) loops and (b) multiple paths to same namespace. 613 In both cases we want to not repeat any lookups, and know whether 614 to stop the caller's step #2. Do this via the FOUND_P marker. */ 615 616 bool 617 name_lookup::search_qualified (tree scope, bool usings) 618 { 619 bool found = false; 620 621 if (seen_p (scope)) 622 found = found_p (scope); 623 else 624 { 625 found = search_namespace (scope); 626 if (!found && usings) 627 found = search_usings (scope); 628 } 629 630 return found; 631 } 632 633 /* Add SCOPE to the unqualified search queue, recursively add its 634 inlines and those via using directives. */ 635 636 name_lookup::using_queue * 637 name_lookup::queue_namespace (using_queue *queue, int depth, tree scope) 638 { 639 if (see_and_mark (scope)) 640 return queue; 641 642 /* Record it. */ 643 tree common = scope; 644 while (SCOPE_DEPTH (common) > depth) 645 common = CP_DECL_CONTEXT (common); 646 vec_safe_push (queue, using_pair (common, scope)); 647 648 /* Queue its inline children. */ 649 if (vec<tree, va_gc> *inlinees = DECL_NAMESPACE_INLINEES (scope)) 650 for (unsigned ix = inlinees->length (); ix--;) 651 queue = queue_namespace (queue, depth, (*inlinees)[ix]); 652 653 /* Queue its using targets. */ 654 queue = queue_usings (queue, depth, DECL_NAMESPACE_USING (scope)); 655 656 return queue; 657 } 658 659 /* Add the namespaces in USINGS to the unqualified search queue. */ 660 661 name_lookup::using_queue * 662 name_lookup::do_queue_usings (using_queue *queue, int depth, 663 vec<tree, va_gc> *usings) 664 { 665 for (unsigned ix = usings->length (); ix--;) 666 queue = queue_namespace (queue, depth, (*usings)[ix]); 667 668 return queue; 669 } 670 671 /* Unqualified namespace lookup in SCOPE. 672 1) add scope+inlins to worklist. 673 2) recursively add target of every using directive 674 3) for each worklist item where SCOPE is common ancestor, search it 675 4) if nothing find, scope=parent, goto 1. */ 676 677 bool 678 name_lookup::search_unqualified (tree scope, cp_binding_level *level) 679 { 680 /* Make static to avoid continual reallocation. We're not 681 recursive. */ 682 static using_queue *queue = NULL; 683 bool found = false; 684 int length = vec_safe_length (queue); 685 686 /* Queue local using-directives. */ 687 for (; level->kind != sk_namespace; level = level->level_chain) 688 queue = queue_usings (queue, SCOPE_DEPTH (scope), level->using_directives); 689 690 for (; !found; scope = CP_DECL_CONTEXT (scope)) 691 { 692 gcc_assert (!DECL_NAMESPACE_ALIAS (scope)); 693 int depth = SCOPE_DEPTH (scope); 694 695 /* Queue namespaces reachable from SCOPE. */ 696 queue = queue_namespace (queue, depth, scope); 697 698 /* Search every queued namespace where SCOPE is the common 699 ancestor. Adjust the others. */ 700 unsigned ix = length; 701 do 702 { 703 using_pair &pair = (*queue)[ix]; 704 while (pair.first == scope) 705 { 706 found |= search_namespace_only (pair.second); 707 pair = queue->pop (); 708 if (ix == queue->length ()) 709 goto done; 710 } 711 /* The depth is the same as SCOPE, find the parent scope. */ 712 if (SCOPE_DEPTH (pair.first) == depth) 713 pair.first = CP_DECL_CONTEXT (pair.first); 714 ix++; 715 } 716 while (ix < queue->length ()); 717 done:; 718 if (scope == global_namespace) 719 break; 720 721 /* If looking for hidden names, we only look in the innermost 722 namespace scope. [namespace.memdef]/3 If a friend 723 declaration in a non-local class first declares a class, 724 function, class template or function template the friend is a 725 member of the innermost enclosing namespace. See also 726 [basic.lookup.unqual]/7 */ 727 if (flags & LOOKUP_HIDDEN) 728 break; 729 } 730 731 vec_safe_truncate (queue, length); 732 733 return found; 734 } 735 736 /* FNS is a value binding. If it is a (set of overloaded) functions, 737 add them into the current value. */ 738 739 void 740 name_lookup::add_fns (tree fns) 741 { 742 if (!fns) 743 return; 744 else if (TREE_CODE (fns) == OVERLOAD) 745 { 746 if (TREE_TYPE (fns) != unknown_type_node) 747 fns = OVL_FUNCTION (fns); 748 } 749 else if (!DECL_DECLARES_FUNCTION_P (fns)) 750 return; 751 752 add_overload (fns); 753 } 754 755 /* Add functions of a namespace to the lookup structure. */ 756 757 void 758 name_lookup::adl_namespace_only (tree scope) 759 { 760 mark_seen (scope); 761 762 /* Look down into inline namespaces. */ 763 if (vec<tree, va_gc> *inlinees = DECL_NAMESPACE_INLINEES (scope)) 764 for (unsigned ix = inlinees->length (); ix--;) 765 adl_namespace_only ((*inlinees)[ix]); 766 767 if (tree fns = find_namespace_value (scope, name)) 768 add_fns (ovl_skip_hidden (fns)); 769 } 770 771 /* Find the containing non-inlined namespace, add it and all its 772 inlinees. */ 773 774 void 775 name_lookup::adl_namespace (tree scope) 776 { 777 if (seen_p (scope)) 778 return; 779 780 /* Find the containing non-inline namespace. */ 781 while (DECL_NAMESPACE_INLINE_P (scope)) 782 scope = CP_DECL_CONTEXT (scope); 783 784 adl_namespace_only (scope); 785 } 786 787 /* Adds the class and its friends to the lookup structure. */ 788 789 void 790 name_lookup::adl_class_only (tree type) 791 { 792 /* Backend-built structures, such as __builtin_va_list, aren't 793 affected by all this. */ 794 if (!CLASS_TYPE_P (type)) 795 return; 796 797 type = TYPE_MAIN_VARIANT (type); 798 799 if (see_and_mark (type)) 800 return; 801 802 tree context = decl_namespace_context (type); 803 adl_namespace (context); 804 805 complete_type (type); 806 807 /* Add friends. */ 808 for (tree list = DECL_FRIENDLIST (TYPE_MAIN_DECL (type)); list; 809 list = TREE_CHAIN (list)) 810 if (name == FRIEND_NAME (list)) 811 for (tree friends = FRIEND_DECLS (list); friends; 812 friends = TREE_CHAIN (friends)) 813 { 814 tree fn = TREE_VALUE (friends); 815 816 /* Only interested in global functions with potentially hidden 817 (i.e. unqualified) declarations. */ 818 if (CP_DECL_CONTEXT (fn) != context) 819 continue; 820 821 /* Only interested in anticipated friends. (Non-anticipated 822 ones will have been inserted during the namespace 823 adl.) */ 824 if (!DECL_ANTICIPATED (fn)) 825 continue; 826 827 /* Template specializations are never found by name lookup. 828 (Templates themselves can be found, but not template 829 specializations.) */ 830 if (TREE_CODE (fn) == FUNCTION_DECL && DECL_USE_TEMPLATE (fn)) 831 continue; 832 833 add_fns (fn); 834 } 835 } 836 837 /* Adds the class and its bases to the lookup structure. 838 Returns true on error. */ 839 840 void 841 name_lookup::adl_bases (tree type) 842 { 843 adl_class_only (type); 844 845 /* Process baseclasses. */ 846 if (tree binfo = TYPE_BINFO (type)) 847 { 848 tree base_binfo; 849 int i; 850 851 for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++) 852 adl_bases (BINFO_TYPE (base_binfo)); 853 } 854 } 855 856 /* Adds everything associated with a class argument type to the lookup 857 structure. Returns true on error. 858 859 If T is a class type (including unions), its associated classes are: the 860 class itself; the class of which it is a member, if any; and its direct 861 and indirect base classes. Its associated namespaces are the namespaces 862 of which its associated classes are members. Furthermore, if T is a 863 class template specialization, its associated namespaces and classes 864 also include: the namespaces and classes associated with the types of 865 the template arguments provided for template type parameters (excluding 866 template template parameters); the namespaces of which any template 867 template arguments are members; and the classes of which any member 868 templates used as template template arguments are members. [ Note: 869 non-type template arguments do not contribute to the set of associated 870 namespaces. --end note] */ 871 872 void 873 name_lookup::adl_class (tree type) 874 { 875 /* Backend build structures, such as __builtin_va_list, aren't 876 affected by all this. */ 877 if (!CLASS_TYPE_P (type)) 878 return; 879 880 type = TYPE_MAIN_VARIANT (type); 881 /* We don't set found here because we have to have set seen first, 882 which is done in the adl_bases walk. */ 883 if (found_p (type)) 884 return; 885 886 adl_bases (type); 887 mark_found (type); 888 889 if (TYPE_CLASS_SCOPE_P (type)) 890 adl_class_only (TYPE_CONTEXT (type)); 891 892 /* Process template arguments. */ 893 if (CLASSTYPE_TEMPLATE_INFO (type) 894 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type))) 895 { 896 tree list = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)); 897 for (int i = 0; i < TREE_VEC_LENGTH (list); ++i) 898 adl_template_arg (TREE_VEC_ELT (list, i)); 899 } 900 } 901 902 void 903 name_lookup::adl_expr (tree expr) 904 { 905 if (!expr) 906 return; 907 908 gcc_assert (!TYPE_P (expr)); 909 910 if (TREE_TYPE (expr) != unknown_type_node) 911 { 912 adl_type (unlowered_expr_type (expr)); 913 return; 914 } 915 916 if (TREE_CODE (expr) == ADDR_EXPR) 917 expr = TREE_OPERAND (expr, 0); 918 if (TREE_CODE (expr) == COMPONENT_REF 919 || TREE_CODE (expr) == OFFSET_REF) 920 expr = TREE_OPERAND (expr, 1); 921 expr = MAYBE_BASELINK_FUNCTIONS (expr); 922 923 if (OVL_P (expr)) 924 for (lkp_iterator iter (expr); iter; ++iter) 925 adl_type (TREE_TYPE (*iter)); 926 else if (TREE_CODE (expr) == TEMPLATE_ID_EXPR) 927 { 928 /* The working paper doesn't currently say how to handle 929 template-id arguments. The sensible thing would seem to be 930 to handle the list of template candidates like a normal 931 overload set, and handle the template arguments like we do 932 for class template specializations. */ 933 934 /* First the templates. */ 935 adl_expr (TREE_OPERAND (expr, 0)); 936 937 /* Now the arguments. */ 938 if (tree args = TREE_OPERAND (expr, 1)) 939 for (int ix = TREE_VEC_LENGTH (args); ix--;) 940 adl_template_arg (TREE_VEC_ELT (args, ix)); 941 } 942 } 943 944 void 945 name_lookup::adl_type (tree type) 946 { 947 if (!type) 948 return; 949 950 if (TYPE_PTRDATAMEM_P (type)) 951 { 952 /* Pointer to member: associate class type and value type. */ 953 adl_type (TYPE_PTRMEM_CLASS_TYPE (type)); 954 adl_type (TYPE_PTRMEM_POINTED_TO_TYPE (type)); 955 return; 956 } 957 958 switch (TREE_CODE (type)) 959 { 960 case RECORD_TYPE: 961 if (TYPE_PTRMEMFUNC_P (type)) 962 { 963 adl_type (TYPE_PTRMEMFUNC_FN_TYPE (type)); 964 return; 965 } 966 /* FALLTHRU */ 967 case UNION_TYPE: 968 adl_class (type); 969 return; 970 971 case METHOD_TYPE: 972 /* The basetype is referenced in the first arg type, so just 973 fall through. */ 974 case FUNCTION_TYPE: 975 /* Associate the parameter types. */ 976 for (tree args = TYPE_ARG_TYPES (type); args; args = TREE_CHAIN (args)) 977 adl_type (TREE_VALUE (args)); 978 /* FALLTHROUGH */ 979 980 case POINTER_TYPE: 981 case REFERENCE_TYPE: 982 case ARRAY_TYPE: 983 adl_type (TREE_TYPE (type)); 984 return; 985 986 case ENUMERAL_TYPE: 987 if (TYPE_CLASS_SCOPE_P (type)) 988 adl_class_only (TYPE_CONTEXT (type)); 989 adl_namespace (decl_namespace_context (type)); 990 return; 991 992 case LANG_TYPE: 993 gcc_assert (type == unknown_type_node 994 || type == init_list_type_node); 995 return; 996 997 case TYPE_PACK_EXPANSION: 998 adl_type (PACK_EXPANSION_PATTERN (type)); 999 return; 1000 1001 default: 1002 break; 1003 } 1004 } 1005 1006 /* Adds everything associated with a template argument to the lookup 1007 structure. */ 1008 1009 void 1010 name_lookup::adl_template_arg (tree arg) 1011 { 1012 /* [basic.lookup.koenig] 1013 1014 If T is a template-id, its associated namespaces and classes are 1015 ... the namespaces and classes associated with the types of the 1016 template arguments provided for template type parameters 1017 (excluding template template parameters); the namespaces in which 1018 any template template arguments are defined; and the classes in 1019 which any member templates used as template template arguments 1020 are defined. [Note: non-type template arguments do not 1021 contribute to the set of associated namespaces. ] */ 1022 1023 /* Consider first template template arguments. */ 1024 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM 1025 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE) 1026 ; 1027 else if (TREE_CODE (arg) == TEMPLATE_DECL) 1028 { 1029 tree ctx = CP_DECL_CONTEXT (arg); 1030 1031 /* It's not a member template. */ 1032 if (TREE_CODE (ctx) == NAMESPACE_DECL) 1033 adl_namespace (ctx); 1034 /* Otherwise, it must be member template. */ 1035 else 1036 adl_class_only (ctx); 1037 } 1038 /* It's an argument pack; handle it recursively. */ 1039 else if (ARGUMENT_PACK_P (arg)) 1040 { 1041 tree args = ARGUMENT_PACK_ARGS (arg); 1042 int i, len = TREE_VEC_LENGTH (args); 1043 for (i = 0; i < len; ++i) 1044 adl_template_arg (TREE_VEC_ELT (args, i)); 1045 } 1046 /* It's not a template template argument, but it is a type template 1047 argument. */ 1048 else if (TYPE_P (arg)) 1049 adl_type (arg); 1050 } 1051 1052 /* Perform ADL lookup. FNS is the existing lookup result and ARGS are 1053 the call arguments. */ 1054 1055 tree 1056 name_lookup::search_adl (tree fns, vec<tree, va_gc> *args) 1057 { 1058 if (fns) 1059 { 1060 deduping = true; 1061 lookup_mark (fns, true); 1062 } 1063 value = fns; 1064 1065 unsigned ix; 1066 tree arg; 1067 1068 FOR_EACH_VEC_ELT_REVERSE (*args, ix, arg) 1069 /* OMP reduction operators put an ADL-significant type as the 1070 first arg. */ 1071 if (TYPE_P (arg)) 1072 adl_type (arg); 1073 else 1074 adl_expr (arg); 1075 1076 fns = value; 1077 1078 return fns; 1079 } 1080 1081 static bool qualified_namespace_lookup (tree, name_lookup *); 1082 static void consider_binding_level (tree name, 1083 best_match <tree, const char *> &bm, 1084 cp_binding_level *lvl, 1085 bool look_within_fields, 1086 enum lookup_name_fuzzy_kind kind); 1087 static void diagnose_name_conflict (tree, tree); 1088 1089 /* ADL lookup of NAME. FNS is the result of regular lookup, and we 1090 don't add duplicates to it. ARGS is the vector of call 1091 arguments (which will not be empty). */ 1092 1093 tree 1094 lookup_arg_dependent (tree name, tree fns, vec<tree, va_gc> *args) 1095 { 1096 bool subtime = timevar_cond_start (TV_NAME_LOOKUP); 1097 name_lookup lookup (name); 1098 fns = lookup.search_adl (fns, args); 1099 timevar_cond_stop (TV_NAME_LOOKUP, subtime); 1100 return fns; 1101 } 1102 1103 /* FNS is an overload set of conversion functions. Return the 1104 overloads converting to TYPE. */ 1105 1106 static tree 1107 extract_conversion_operator (tree fns, tree type) 1108 { 1109 tree convs = NULL_TREE; 1110 tree tpls = NULL_TREE; 1111 1112 for (ovl_iterator iter (fns); iter; ++iter) 1113 { 1114 if (same_type_p (DECL_CONV_FN_TYPE (*iter), type)) 1115 convs = lookup_add (*iter, convs); 1116 1117 if (TREE_CODE (*iter) == TEMPLATE_DECL) 1118 tpls = lookup_add (*iter, tpls); 1119 } 1120 1121 if (!convs) 1122 convs = tpls; 1123 1124 return convs; 1125 } 1126 1127 /* Binary search of (ordered) MEMBER_VEC for NAME. */ 1128 1129 static tree 1130 member_vec_binary_search (vec<tree, va_gc> *member_vec, tree name) 1131 { 1132 for (unsigned lo = 0, hi = member_vec->length (); lo < hi;) 1133 { 1134 unsigned mid = (lo + hi) / 2; 1135 tree binding = (*member_vec)[mid]; 1136 tree binding_name = OVL_NAME (binding); 1137 1138 if (binding_name > name) 1139 hi = mid; 1140 else if (binding_name < name) 1141 lo = mid + 1; 1142 else 1143 return binding; 1144 } 1145 1146 return NULL_TREE; 1147 } 1148 1149 /* Linear search of (unordered) MEMBER_VEC for NAME. */ 1150 1151 static tree 1152 member_vec_linear_search (vec<tree, va_gc> *member_vec, tree name) 1153 { 1154 for (int ix = member_vec->length (); ix--;) 1155 if (tree binding = (*member_vec)[ix]) 1156 if (OVL_NAME (binding) == name) 1157 return binding; 1158 1159 return NULL_TREE; 1160 } 1161 1162 /* Linear search of (partially ordered) fields of KLASS for NAME. */ 1163 1164 static tree 1165 fields_linear_search (tree klass, tree name, bool want_type) 1166 { 1167 for (tree fields = TYPE_FIELDS (klass); fields; fields = DECL_CHAIN (fields)) 1168 { 1169 tree decl = fields; 1170 1171 if (TREE_CODE (decl) == FIELD_DECL 1172 && ANON_AGGR_TYPE_P (TREE_TYPE (decl))) 1173 { 1174 if (tree temp = search_anon_aggr (TREE_TYPE (decl), name, want_type)) 1175 return temp; 1176 } 1177 1178 if (DECL_NAME (decl) != name) 1179 continue; 1180 1181 if (TREE_CODE (decl) == USING_DECL) 1182 { 1183 decl = strip_using_decl (decl); 1184 if (is_overloaded_fn (decl)) 1185 continue; 1186 } 1187 1188 if (DECL_DECLARES_FUNCTION_P (decl)) 1189 /* Functions are found separately. */ 1190 continue; 1191 1192 if (!want_type || DECL_DECLARES_TYPE_P (decl)) 1193 return decl; 1194 } 1195 1196 return NULL_TREE; 1197 } 1198 1199 /* Look for NAME member inside of anonymous aggregate ANON. Although 1200 such things should only contain FIELD_DECLs, we check that too 1201 late, and would give very confusing errors if we weren't 1202 permissive here. */ 1203 1204 tree 1205 search_anon_aggr (tree anon, tree name, bool want_type) 1206 { 1207 gcc_assert (COMPLETE_TYPE_P (anon)); 1208 tree ret = get_class_binding_direct (anon, name, want_type); 1209 return ret; 1210 } 1211 1212 /* Look for NAME as an immediate member of KLASS (including 1213 anon-members or unscoped enum member). TYPE_OR_FNS is zero for 1214 regular search. >0 to get a type binding (if there is one) and <0 1215 if you want (just) the member function binding. 1216 1217 Use this if you do not want lazy member creation. */ 1218 1219 tree 1220 get_class_binding_direct (tree klass, tree name, int type_or_fns) 1221 { 1222 gcc_checking_assert (RECORD_OR_UNION_TYPE_P (klass)); 1223 1224 /* Conversion operators can only be found by the marker conversion 1225 operator name. */ 1226 bool conv_op = IDENTIFIER_CONV_OP_P (name); 1227 tree lookup = conv_op ? conv_op_identifier : name; 1228 tree val = NULL_TREE; 1229 vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass); 1230 1231 if (COMPLETE_TYPE_P (klass) && member_vec) 1232 { 1233 val = member_vec_binary_search (member_vec, lookup); 1234 if (!val) 1235 ; 1236 else if (type_or_fns > 0) 1237 { 1238 if (STAT_HACK_P (val)) 1239 val = STAT_TYPE (val); 1240 else if (!DECL_DECLARES_TYPE_P (val)) 1241 val = NULL_TREE; 1242 } 1243 else if (STAT_HACK_P (val)) 1244 val = STAT_DECL (val); 1245 } 1246 else 1247 { 1248 if (member_vec && type_or_fns <= 0) 1249 val = member_vec_linear_search (member_vec, lookup); 1250 1251 if (type_or_fns < 0) 1252 /* Don't bother looking for field. We don't want it. */; 1253 else if (!val || (TREE_CODE (val) == OVERLOAD 1254 && OVL_DEDUP_P (val))) 1255 /* Dependent using declarations are a 'field', make sure we 1256 return that even if we saw an overload already. */ 1257 if (tree field_val = fields_linear_search (klass, lookup, 1258 type_or_fns > 0)) 1259 if (!val || TREE_CODE (field_val) == USING_DECL) 1260 val = field_val; 1261 } 1262 1263 /* Extract the conversion operators asked for, unless the general 1264 conversion operator was requested. */ 1265 if (val && conv_op) 1266 { 1267 gcc_checking_assert (OVL_FUNCTION (val) == conv_op_marker); 1268 val = OVL_CHAIN (val); 1269 if (tree type = TREE_TYPE (name)) 1270 val = extract_conversion_operator (val, type); 1271 } 1272 1273 return val; 1274 } 1275 1276 /* Look for NAME's binding in exactly KLASS. See 1277 get_class_binding_direct for argument description. Does lazy 1278 special function creation as necessary. */ 1279 1280 tree 1281 get_class_binding (tree klass, tree name, int type_or_fns) 1282 { 1283 klass = complete_type (klass); 1284 1285 if (COMPLETE_TYPE_P (klass)) 1286 { 1287 /* Lazily declare functions, if we're going to search these. */ 1288 if (IDENTIFIER_CTOR_P (name)) 1289 { 1290 if (CLASSTYPE_LAZY_DEFAULT_CTOR (klass)) 1291 lazily_declare_fn (sfk_constructor, klass); 1292 if (CLASSTYPE_LAZY_COPY_CTOR (klass)) 1293 lazily_declare_fn (sfk_copy_constructor, klass); 1294 if (CLASSTYPE_LAZY_MOVE_CTOR (klass)) 1295 lazily_declare_fn (sfk_move_constructor, klass); 1296 } 1297 else if (IDENTIFIER_DTOR_P (name)) 1298 { 1299 if (CLASSTYPE_LAZY_DESTRUCTOR (klass)) 1300 lazily_declare_fn (sfk_destructor, klass); 1301 } 1302 else if (name == assign_op_identifier) 1303 { 1304 if (CLASSTYPE_LAZY_COPY_ASSIGN (klass)) 1305 lazily_declare_fn (sfk_copy_assignment, klass); 1306 if (CLASSTYPE_LAZY_MOVE_ASSIGN (klass)) 1307 lazily_declare_fn (sfk_move_assignment, klass); 1308 } 1309 } 1310 1311 return get_class_binding_direct (klass, name, type_or_fns); 1312 } 1313 1314 /* Find the slot containing overloads called 'NAME'. If there is no 1315 such slot and the class is complete, create an empty one, at the 1316 correct point in the sorted member vector. Otherwise return NULL. 1317 Deals with conv_op marker handling. */ 1318 1319 tree * 1320 find_member_slot (tree klass, tree name) 1321 { 1322 bool complete_p = COMPLETE_TYPE_P (klass); 1323 1324 vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass); 1325 if (!member_vec) 1326 { 1327 vec_alloc (member_vec, 8); 1328 CLASSTYPE_MEMBER_VEC (klass) = member_vec; 1329 if (complete_p) 1330 { 1331 /* If the class is complete but had no member_vec, we need 1332 to add the TYPE_FIELDS into it. We're also most likely 1333 to be adding ctors & dtors, so ask for 6 spare slots (the 1334 abstract cdtors and their clones). */ 1335 set_class_bindings (klass, 6); 1336 member_vec = CLASSTYPE_MEMBER_VEC (klass); 1337 } 1338 } 1339 1340 if (IDENTIFIER_CONV_OP_P (name)) 1341 name = conv_op_identifier; 1342 1343 unsigned ix, length = member_vec->length (); 1344 for (ix = 0; ix < length; ix++) 1345 { 1346 tree *slot = &(*member_vec)[ix]; 1347 tree fn_name = OVL_NAME (*slot); 1348 1349 if (fn_name == name) 1350 { 1351 /* If we found an existing slot, it must be a function set. 1352 Even with insertion after completion, because those only 1353 happen with artificial fns that have unspellable names. 1354 This means we do not have to deal with the stat hack 1355 either. */ 1356 gcc_checking_assert (OVL_P (*slot)); 1357 if (name == conv_op_identifier) 1358 { 1359 gcc_checking_assert (OVL_FUNCTION (*slot) == conv_op_marker); 1360 /* Skip the conv-op marker. */ 1361 slot = &OVL_CHAIN (*slot); 1362 } 1363 return slot; 1364 } 1365 1366 if (complete_p && fn_name > name) 1367 break; 1368 } 1369 1370 /* No slot found, add one if the class is complete. */ 1371 if (complete_p) 1372 { 1373 /* Do exact allocation, as we don't expect to add many. */ 1374 gcc_assert (name != conv_op_identifier); 1375 vec_safe_reserve_exact (member_vec, 1); 1376 CLASSTYPE_MEMBER_VEC (klass) = member_vec; 1377 member_vec->quick_insert (ix, NULL_TREE); 1378 return &(*member_vec)[ix]; 1379 } 1380 1381 return NULL; 1382 } 1383 1384 /* KLASS is an incomplete class to which we're adding a method NAME. 1385 Add a slot and deal with conv_op marker handling. */ 1386 1387 tree * 1388 add_member_slot (tree klass, tree name) 1389 { 1390 gcc_assert (!COMPLETE_TYPE_P (klass)); 1391 1392 vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass); 1393 vec_safe_push (member_vec, NULL_TREE); 1394 CLASSTYPE_MEMBER_VEC (klass) = member_vec; 1395 1396 tree *slot = &member_vec->last (); 1397 if (IDENTIFIER_CONV_OP_P (name)) 1398 { 1399 /* Install the marker prefix. */ 1400 *slot = ovl_make (conv_op_marker, NULL_TREE); 1401 slot = &OVL_CHAIN (*slot); 1402 } 1403 1404 return slot; 1405 } 1406 1407 /* Comparison function to compare two MEMBER_VEC entries by name. 1408 Because we can have duplicates during insertion of TYPE_FIELDS, we 1409 do extra checking so deduping doesn't have to deal with so many 1410 cases. */ 1411 1412 static int 1413 member_name_cmp (const void *a_p, const void *b_p) 1414 { 1415 tree a = *(const tree *)a_p; 1416 tree b = *(const tree *)b_p; 1417 tree name_a = DECL_NAME (TREE_CODE (a) == OVERLOAD ? OVL_FUNCTION (a) : a); 1418 tree name_b = DECL_NAME (TREE_CODE (b) == OVERLOAD ? OVL_FUNCTION (b) : b); 1419 1420 gcc_checking_assert (name_a && name_b); 1421 if (name_a != name_b) 1422 return name_a < name_b ? -1 : +1; 1423 1424 if (name_a == conv_op_identifier) 1425 { 1426 /* Strip the conv-op markers. */ 1427 gcc_checking_assert (OVL_FUNCTION (a) == conv_op_marker 1428 && OVL_FUNCTION (b) == conv_op_marker); 1429 a = OVL_CHAIN (a); 1430 b = OVL_CHAIN (b); 1431 } 1432 1433 if (TREE_CODE (a) == OVERLOAD) 1434 a = OVL_FUNCTION (a); 1435 if (TREE_CODE (b) == OVERLOAD) 1436 b = OVL_FUNCTION (b); 1437 1438 /* We're in STAT_HACK or USING_DECL territory (or possibly error-land). */ 1439 if (TREE_CODE (a) != TREE_CODE (b)) 1440 { 1441 /* If one of them is a TYPE_DECL, it loses. */ 1442 if (TREE_CODE (a) == TYPE_DECL) 1443 return +1; 1444 else if (TREE_CODE (b) == TYPE_DECL) 1445 return -1; 1446 1447 /* If one of them is a USING_DECL, it loses. */ 1448 if (TREE_CODE (a) == USING_DECL) 1449 return +1; 1450 else if (TREE_CODE (b) == USING_DECL) 1451 return -1; 1452 1453 /* There are no other cases with different kinds of decls, as 1454 duplicate detection should have kicked in earlier. However, 1455 some erroneous cases get though. */ 1456 gcc_assert (errorcount); 1457 } 1458 1459 /* Using source location would be the best thing here, but we can 1460 get identically-located decls in the following circumstances: 1461 1462 1) duplicate artificial type-decls for the same type. 1463 1464 2) pack expansions of using-decls. 1465 1466 We should not be doing #1, but in either case it doesn't matter 1467 how we order these. Use UID as a proxy for source ordering, so 1468 that identically-located decls still have a well-defined stable 1469 ordering. */ 1470 if (DECL_UID (a) != DECL_UID (b)) 1471 return DECL_UID (a) < DECL_UID (b) ? -1 : +1; 1472 gcc_assert (a == b); 1473 return 0; 1474 } 1475 1476 static struct { 1477 gt_pointer_operator new_value; 1478 void *cookie; 1479 } resort_data; 1480 1481 /* This routine compares two fields like member_name_cmp but using the 1482 pointer operator in resort_field_decl_data. We don't have to deal 1483 with duplicates here. */ 1484 1485 static int 1486 resort_member_name_cmp (const void *a_p, const void *b_p) 1487 { 1488 tree a = *(const tree *)a_p; 1489 tree b = *(const tree *)b_p; 1490 tree name_a = OVL_NAME (a); 1491 tree name_b = OVL_NAME (b); 1492 1493 resort_data.new_value (&name_a, resort_data.cookie); 1494 resort_data.new_value (&name_b, resort_data.cookie); 1495 1496 gcc_checking_assert (name_a != name_b); 1497 1498 return name_a < name_b ? -1 : +1; 1499 } 1500 1501 /* Resort CLASSTYPE_MEMBER_VEC because pointers have been reordered. */ 1502 1503 void 1504 resort_type_member_vec (void *obj, void */*orig_obj*/, 1505 gt_pointer_operator new_value, void* cookie) 1506 { 1507 if (vec<tree, va_gc> *member_vec = (vec<tree, va_gc> *) obj) 1508 { 1509 resort_data.new_value = new_value; 1510 resort_data.cookie = cookie; 1511 member_vec->qsort (resort_member_name_cmp); 1512 } 1513 } 1514 1515 /* Recursively count the number of fields in KLASS, including anonymous 1516 union members. */ 1517 1518 static unsigned 1519 count_class_fields (tree klass) 1520 { 1521 unsigned n_fields = 0; 1522 1523 for (tree fields = TYPE_FIELDS (klass); fields; fields = DECL_CHAIN (fields)) 1524 if (DECL_DECLARES_FUNCTION_P (fields)) 1525 /* Functions are dealt with separately. */; 1526 else if (TREE_CODE (fields) == FIELD_DECL 1527 && ANON_AGGR_TYPE_P (TREE_TYPE (fields))) 1528 n_fields += count_class_fields (TREE_TYPE (fields)); 1529 else if (DECL_NAME (fields)) 1530 n_fields += 1; 1531 1532 return n_fields; 1533 } 1534 1535 /* Append all the nonfunction members fields of KLASS to MEMBER_VEC. 1536 Recurse for anonymous members. MEMBER_VEC must have space. */ 1537 1538 static void 1539 member_vec_append_class_fields (vec<tree, va_gc> *member_vec, tree klass) 1540 { 1541 for (tree fields = TYPE_FIELDS (klass); fields; fields = DECL_CHAIN (fields)) 1542 if (DECL_DECLARES_FUNCTION_P (fields)) 1543 /* Functions are handled separately. */; 1544 else if (TREE_CODE (fields) == FIELD_DECL 1545 && ANON_AGGR_TYPE_P (TREE_TYPE (fields))) 1546 member_vec_append_class_fields (member_vec, TREE_TYPE (fields)); 1547 else if (DECL_NAME (fields)) 1548 { 1549 tree field = fields; 1550 /* Mark a conv-op USING_DECL with the conv-op-marker. */ 1551 if (TREE_CODE (field) == USING_DECL 1552 && IDENTIFIER_CONV_OP_P (DECL_NAME (field))) 1553 field = ovl_make (conv_op_marker, field); 1554 member_vec->quick_push (field); 1555 } 1556 } 1557 1558 /* Append all of the enum values of ENUMTYPE to MEMBER_VEC. 1559 MEMBER_VEC must have space. */ 1560 1561 static void 1562 member_vec_append_enum_values (vec<tree, va_gc> *member_vec, tree enumtype) 1563 { 1564 for (tree values = TYPE_VALUES (enumtype); 1565 values; values = TREE_CHAIN (values)) 1566 member_vec->quick_push (TREE_VALUE (values)); 1567 } 1568 1569 /* MEMBER_VEC has just had new DECLs added to it, but is sorted. 1570 DeDup adjacent DECLS of the same name. We already dealt with 1571 conflict resolution when adding the fields or methods themselves. 1572 There are three cases (which could all be combined): 1573 1) a TYPE_DECL and non TYPE_DECL. Deploy STAT_HACK as appropriate. 1574 2) a USING_DECL and an overload. If the USING_DECL is dependent, 1575 it wins. Otherwise the OVERLOAD does. 1576 3) two USING_DECLS. ... 1577 1578 member_name_cmp will have ordered duplicates as 1579 <fns><using><type> */ 1580 1581 static void 1582 member_vec_dedup (vec<tree, va_gc> *member_vec) 1583 { 1584 unsigned len = member_vec->length (); 1585 unsigned store = 0; 1586 1587 if (!len) 1588 return; 1589 1590 tree name = OVL_NAME ((*member_vec)[0]); 1591 for (unsigned jx, ix = 0; ix < len; ix = jx) 1592 { 1593 tree current = NULL_TREE; 1594 tree to_type = NULL_TREE; 1595 tree to_using = NULL_TREE; 1596 tree marker = NULL_TREE; 1597 1598 for (jx = ix; jx < len; jx++) 1599 { 1600 tree next = (*member_vec)[jx]; 1601 if (jx != ix) 1602 { 1603 tree next_name = OVL_NAME (next); 1604 if (next_name != name) 1605 { 1606 name = next_name; 1607 break; 1608 } 1609 } 1610 1611 if (IDENTIFIER_CONV_OP_P (name)) 1612 { 1613 marker = next; 1614 next = OVL_CHAIN (next); 1615 } 1616 1617 if (TREE_CODE (next) == USING_DECL) 1618 { 1619 if (IDENTIFIER_CTOR_P (name)) 1620 /* Dependent inherited ctor. */ 1621 continue; 1622 1623 next = strip_using_decl (next); 1624 if (TREE_CODE (next) == USING_DECL) 1625 { 1626 to_using = next; 1627 continue; 1628 } 1629 1630 if (is_overloaded_fn (next)) 1631 continue; 1632 } 1633 1634 if (DECL_DECLARES_TYPE_P (next)) 1635 { 1636 to_type = next; 1637 continue; 1638 } 1639 1640 if (!current) 1641 current = next; 1642 } 1643 1644 if (to_using) 1645 { 1646 if (!current) 1647 current = to_using; 1648 else 1649 current = ovl_make (to_using, current); 1650 } 1651 1652 if (to_type) 1653 { 1654 if (!current) 1655 current = to_type; 1656 else 1657 current = stat_hack (current, to_type); 1658 } 1659 1660 if (current) 1661 { 1662 if (marker) 1663 { 1664 OVL_CHAIN (marker) = current; 1665 current = marker; 1666 } 1667 (*member_vec)[store++] = current; 1668 } 1669 } 1670 1671 while (store++ < len) 1672 member_vec->pop (); 1673 } 1674 1675 /* Add the non-function members to CLASSTYPE_MEMBER_VEC. If there is 1676 no existing MEMBER_VEC and fewer than 8 fields, do nothing. We 1677 know there must be at least 1 field -- the self-reference 1678 TYPE_DECL, except for anon aggregates, which will have at least 1679 one field. */ 1680 1681 void 1682 set_class_bindings (tree klass, unsigned extra) 1683 { 1684 unsigned n_fields = count_class_fields (klass); 1685 vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass); 1686 1687 if (member_vec || n_fields >= 8) 1688 { 1689 /* Append the new fields. */ 1690 vec_safe_reserve_exact (member_vec, extra + n_fields); 1691 member_vec_append_class_fields (member_vec, klass); 1692 } 1693 1694 if (member_vec) 1695 { 1696 CLASSTYPE_MEMBER_VEC (klass) = member_vec; 1697 member_vec->qsort (member_name_cmp); 1698 member_vec_dedup (member_vec); 1699 } 1700 } 1701 1702 /* Insert lately defined enum ENUMTYPE into KLASS for the sorted case. */ 1703 1704 void 1705 insert_late_enum_def_bindings (tree klass, tree enumtype) 1706 { 1707 int n_fields; 1708 vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (klass); 1709 1710 /* The enum bindings will already be on the TYPE_FIELDS, so don't 1711 count them twice. */ 1712 if (!member_vec) 1713 n_fields = count_class_fields (klass); 1714 else 1715 n_fields = list_length (TYPE_VALUES (enumtype)); 1716 1717 if (member_vec || n_fields >= 8) 1718 { 1719 vec_safe_reserve_exact (member_vec, n_fields); 1720 if (CLASSTYPE_MEMBER_VEC (klass)) 1721 member_vec_append_enum_values (member_vec, enumtype); 1722 else 1723 member_vec_append_class_fields (member_vec, klass); 1724 CLASSTYPE_MEMBER_VEC (klass) = member_vec; 1725 member_vec->qsort (member_name_cmp); 1726 member_vec_dedup (member_vec); 1727 } 1728 } 1729 1730 /* Compute the chain index of a binding_entry given the HASH value of its 1731 name and the total COUNT of chains. COUNT is assumed to be a power 1732 of 2. */ 1733 1734 #define ENTRY_INDEX(HASH, COUNT) (((HASH) >> 3) & ((COUNT) - 1)) 1735 1736 /* A free list of "binding_entry"s awaiting for re-use. */ 1737 1738 static GTY((deletable)) binding_entry free_binding_entry = NULL; 1739 1740 /* The binding oracle; see cp-tree.h. */ 1741 1742 cp_binding_oracle_function *cp_binding_oracle; 1743 1744 /* If we have a binding oracle, ask it for all namespace-scoped 1745 definitions of NAME. */ 1746 1747 static inline void 1748 query_oracle (tree name) 1749 { 1750 if (!cp_binding_oracle) 1751 return; 1752 1753 /* LOOKED_UP holds the set of identifiers that we have already 1754 looked up with the oracle. */ 1755 static hash_set<tree> looked_up; 1756 if (looked_up.add (name)) 1757 return; 1758 1759 cp_binding_oracle (CP_ORACLE_IDENTIFIER, name); 1760 } 1761 1762 /* Create a binding_entry object for (NAME, TYPE). */ 1763 1764 static inline binding_entry 1765 binding_entry_make (tree name, tree type) 1766 { 1767 binding_entry entry; 1768 1769 if (free_binding_entry) 1770 { 1771 entry = free_binding_entry; 1772 free_binding_entry = entry->chain; 1773 } 1774 else 1775 entry = ggc_alloc<binding_entry_s> (); 1776 1777 entry->name = name; 1778 entry->type = type; 1779 entry->chain = NULL; 1780 1781 return entry; 1782 } 1783 1784 /* Put ENTRY back on the free list. */ 1785 #if 0 1786 static inline void 1787 binding_entry_free (binding_entry entry) 1788 { 1789 entry->name = NULL; 1790 entry->type = NULL; 1791 entry->chain = free_binding_entry; 1792 free_binding_entry = entry; 1793 } 1794 #endif 1795 1796 /* The datatype used to implement the mapping from names to types at 1797 a given scope. */ 1798 struct GTY(()) binding_table_s { 1799 /* Array of chains of "binding_entry"s */ 1800 binding_entry * GTY((length ("%h.chain_count"))) chain; 1801 1802 /* The number of chains in this table. This is the length of the 1803 member "chain" considered as an array. */ 1804 size_t chain_count; 1805 1806 /* Number of "binding_entry"s in this table. */ 1807 size_t entry_count; 1808 }; 1809 1810 /* Construct TABLE with an initial CHAIN_COUNT. */ 1811 1812 static inline void 1813 binding_table_construct (binding_table table, size_t chain_count) 1814 { 1815 table->chain_count = chain_count; 1816 table->entry_count = 0; 1817 table->chain = ggc_cleared_vec_alloc<binding_entry> (table->chain_count); 1818 } 1819 1820 /* Make TABLE's entries ready for reuse. */ 1821 #if 0 1822 static void 1823 binding_table_free (binding_table table) 1824 { 1825 size_t i; 1826 size_t count; 1827 1828 if (table == NULL) 1829 return; 1830 1831 for (i = 0, count = table->chain_count; i < count; ++i) 1832 { 1833 binding_entry temp = table->chain[i]; 1834 while (temp != NULL) 1835 { 1836 binding_entry entry = temp; 1837 temp = entry->chain; 1838 binding_entry_free (entry); 1839 } 1840 table->chain[i] = NULL; 1841 } 1842 table->entry_count = 0; 1843 } 1844 #endif 1845 1846 /* Allocate a table with CHAIN_COUNT, assumed to be a power of two. */ 1847 1848 static inline binding_table 1849 binding_table_new (size_t chain_count) 1850 { 1851 binding_table table = ggc_alloc<binding_table_s> (); 1852 table->chain = NULL; 1853 binding_table_construct (table, chain_count); 1854 return table; 1855 } 1856 1857 /* Expand TABLE to twice its current chain_count. */ 1858 1859 static void 1860 binding_table_expand (binding_table table) 1861 { 1862 const size_t old_chain_count = table->chain_count; 1863 const size_t old_entry_count = table->entry_count; 1864 const size_t new_chain_count = 2 * old_chain_count; 1865 binding_entry *old_chains = table->chain; 1866 size_t i; 1867 1868 binding_table_construct (table, new_chain_count); 1869 for (i = 0; i < old_chain_count; ++i) 1870 { 1871 binding_entry entry = old_chains[i]; 1872 for (; entry != NULL; entry = old_chains[i]) 1873 { 1874 const unsigned int hash = IDENTIFIER_HASH_VALUE (entry->name); 1875 const size_t j = ENTRY_INDEX (hash, new_chain_count); 1876 1877 old_chains[i] = entry->chain; 1878 entry->chain = table->chain[j]; 1879 table->chain[j] = entry; 1880 } 1881 } 1882 table->entry_count = old_entry_count; 1883 } 1884 1885 /* Insert a binding for NAME to TYPE into TABLE. */ 1886 1887 static void 1888 binding_table_insert (binding_table table, tree name, tree type) 1889 { 1890 const unsigned int hash = IDENTIFIER_HASH_VALUE (name); 1891 const size_t i = ENTRY_INDEX (hash, table->chain_count); 1892 binding_entry entry = binding_entry_make (name, type); 1893 1894 entry->chain = table->chain[i]; 1895 table->chain[i] = entry; 1896 ++table->entry_count; 1897 1898 if (3 * table->chain_count < 5 * table->entry_count) 1899 binding_table_expand (table); 1900 } 1901 1902 /* Return the binding_entry, if any, that maps NAME. */ 1903 1904 binding_entry 1905 binding_table_find (binding_table table, tree name) 1906 { 1907 const unsigned int hash = IDENTIFIER_HASH_VALUE (name); 1908 binding_entry entry = table->chain[ENTRY_INDEX (hash, table->chain_count)]; 1909 1910 while (entry != NULL && entry->name != name) 1911 entry = entry->chain; 1912 1913 return entry; 1914 } 1915 1916 /* Apply PROC -- with DATA -- to all entries in TABLE. */ 1917 1918 void 1919 binding_table_foreach (binding_table table, bt_foreach_proc proc, void *data) 1920 { 1921 size_t chain_count; 1922 size_t i; 1923 1924 if (!table) 1925 return; 1926 1927 chain_count = table->chain_count; 1928 for (i = 0; i < chain_count; ++i) 1929 { 1930 binding_entry entry = table->chain[i]; 1931 for (; entry != NULL; entry = entry->chain) 1932 proc (entry, data); 1933 } 1934 } 1935 1936 #ifndef ENABLE_SCOPE_CHECKING 1937 # define ENABLE_SCOPE_CHECKING 0 1938 #else 1939 # define ENABLE_SCOPE_CHECKING 1 1940 #endif 1941 1942 /* A free list of "cxx_binding"s, connected by their PREVIOUS. */ 1943 1944 static GTY((deletable)) cxx_binding *free_bindings; 1945 1946 /* Initialize VALUE and TYPE field for BINDING, and set the PREVIOUS 1947 field to NULL. */ 1948 1949 static inline void 1950 cxx_binding_init (cxx_binding *binding, tree value, tree type) 1951 { 1952 binding->value = value; 1953 binding->type = type; 1954 binding->previous = NULL; 1955 } 1956 1957 /* (GC)-allocate a binding object with VALUE and TYPE member initialized. */ 1958 1959 static cxx_binding * 1960 cxx_binding_make (tree value, tree type) 1961 { 1962 cxx_binding *binding; 1963 if (free_bindings) 1964 { 1965 binding = free_bindings; 1966 free_bindings = binding->previous; 1967 } 1968 else 1969 binding = ggc_alloc<cxx_binding> (); 1970 1971 cxx_binding_init (binding, value, type); 1972 1973 return binding; 1974 } 1975 1976 /* Put BINDING back on the free list. */ 1977 1978 static inline void 1979 cxx_binding_free (cxx_binding *binding) 1980 { 1981 binding->scope = NULL; 1982 binding->previous = free_bindings; 1983 free_bindings = binding; 1984 } 1985 1986 /* Create a new binding for NAME (with the indicated VALUE and TYPE 1987 bindings) in the class scope indicated by SCOPE. */ 1988 1989 static cxx_binding * 1990 new_class_binding (tree name, tree value, tree type, cp_binding_level *scope) 1991 { 1992 cp_class_binding cb = {cxx_binding_make (value, type), name}; 1993 cxx_binding *binding = cb.base; 1994 vec_safe_push (scope->class_shadowed, cb); 1995 binding->scope = scope; 1996 return binding; 1997 } 1998 1999 /* Make DECL the innermost binding for ID. The LEVEL is the binding 2000 level at which this declaration is being bound. */ 2001 2002 void 2003 push_binding (tree id, tree decl, cp_binding_level* level) 2004 { 2005 cxx_binding *binding; 2006 2007 if (level != class_binding_level) 2008 { 2009 binding = cxx_binding_make (decl, NULL_TREE); 2010 binding->scope = level; 2011 } 2012 else 2013 binding = new_class_binding (id, decl, /*type=*/NULL_TREE, level); 2014 2015 /* Now, fill in the binding information. */ 2016 binding->previous = IDENTIFIER_BINDING (id); 2017 INHERITED_VALUE_BINDING_P (binding) = 0; 2018 LOCAL_BINDING_P (binding) = (level != class_binding_level); 2019 2020 /* And put it on the front of the list of bindings for ID. */ 2021 IDENTIFIER_BINDING (id) = binding; 2022 } 2023 2024 /* Remove the binding for DECL which should be the innermost binding 2025 for ID. */ 2026 2027 void 2028 pop_local_binding (tree id, tree decl) 2029 { 2030 cxx_binding *binding; 2031 2032 if (id == NULL_TREE) 2033 /* It's easiest to write the loops that call this function without 2034 checking whether or not the entities involved have names. We 2035 get here for such an entity. */ 2036 return; 2037 2038 /* Get the innermost binding for ID. */ 2039 binding = IDENTIFIER_BINDING (id); 2040 2041 /* The name should be bound. */ 2042 gcc_assert (binding != NULL); 2043 2044 /* The DECL will be either the ordinary binding or the type 2045 binding for this identifier. Remove that binding. */ 2046 if (binding->value == decl) 2047 binding->value = NULL_TREE; 2048 else 2049 { 2050 gcc_assert (binding->type == decl); 2051 binding->type = NULL_TREE; 2052 } 2053 2054 if (!binding->value && !binding->type) 2055 { 2056 /* We're completely done with the innermost binding for this 2057 identifier. Unhook it from the list of bindings. */ 2058 IDENTIFIER_BINDING (id) = binding->previous; 2059 2060 /* Add it to the free list. */ 2061 cxx_binding_free (binding); 2062 } 2063 } 2064 2065 /* Remove the bindings for the decls of the current level and leave 2066 the current scope. */ 2067 2068 void 2069 pop_bindings_and_leave_scope (void) 2070 { 2071 for (tree t = get_local_decls (); t; t = DECL_CHAIN (t)) 2072 { 2073 tree decl = TREE_CODE (t) == TREE_LIST ? TREE_VALUE (t) : t; 2074 tree name = OVL_NAME (decl); 2075 2076 pop_local_binding (name, decl); 2077 } 2078 2079 leave_scope (); 2080 } 2081 2082 /* Strip non dependent using declarations. If DECL is dependent, 2083 surreptitiously create a typename_type and return it. */ 2084 2085 tree 2086 strip_using_decl (tree decl) 2087 { 2088 if (decl == NULL_TREE) 2089 return NULL_TREE; 2090 2091 while (TREE_CODE (decl) == USING_DECL && !DECL_DEPENDENT_P (decl)) 2092 decl = USING_DECL_DECLS (decl); 2093 2094 if (TREE_CODE (decl) == USING_DECL && DECL_DEPENDENT_P (decl) 2095 && USING_DECL_TYPENAME_P (decl)) 2096 { 2097 /* We have found a type introduced by a using 2098 declaration at class scope that refers to a dependent 2099 type. 2100 2101 using typename :: [opt] nested-name-specifier unqualified-id ; 2102 */ 2103 decl = make_typename_type (USING_DECL_SCOPE (decl), 2104 DECL_NAME (decl), 2105 typename_type, tf_error); 2106 if (decl != error_mark_node) 2107 decl = TYPE_NAME (decl); 2108 } 2109 2110 return decl; 2111 } 2112 2113 /* Return true if OVL is an overload for an anticipated builtin. */ 2114 2115 static bool 2116 anticipated_builtin_p (tree ovl) 2117 { 2118 if (TREE_CODE (ovl) != OVERLOAD) 2119 return false; 2120 2121 if (!OVL_HIDDEN_P (ovl)) 2122 return false; 2123 2124 tree fn = OVL_FUNCTION (ovl); 2125 gcc_checking_assert (DECL_ANTICIPATED (fn)); 2126 2127 if (DECL_HIDDEN_FRIEND_P (fn)) 2128 return false; 2129 2130 return true; 2131 } 2132 2133 /* BINDING records an existing declaration for a name in the current scope. 2134 But, DECL is another declaration for that same identifier in the 2135 same scope. This is the `struct stat' hack whereby a non-typedef 2136 class name or enum-name can be bound at the same level as some other 2137 kind of entity. 2138 3.3.7/1 2139 2140 A class name (9.1) or enumeration name (7.2) can be hidden by the 2141 name of an object, function, or enumerator declared in the same scope. 2142 If a class or enumeration name and an object, function, or enumerator 2143 are declared in the same scope (in any order) with the same name, the 2144 class or enumeration name is hidden wherever the object, function, or 2145 enumerator name is visible. 2146 2147 It's the responsibility of the caller to check that 2148 inserting this name is valid here. Returns nonzero if the new binding 2149 was successful. */ 2150 2151 static bool 2152 supplement_binding_1 (cxx_binding *binding, tree decl) 2153 { 2154 tree bval = binding->value; 2155 bool ok = true; 2156 tree target_bval = strip_using_decl (bval); 2157 tree target_decl = strip_using_decl (decl); 2158 2159 if (TREE_CODE (target_decl) == TYPE_DECL && DECL_ARTIFICIAL (target_decl) 2160 && target_decl != target_bval 2161 && (TREE_CODE (target_bval) != TYPE_DECL 2162 /* We allow pushing an enum multiple times in a class 2163 template in order to handle late matching of underlying 2164 type on an opaque-enum-declaration followed by an 2165 enum-specifier. */ 2166 || (processing_template_decl 2167 && TREE_CODE (TREE_TYPE (target_decl)) == ENUMERAL_TYPE 2168 && TREE_CODE (TREE_TYPE (target_bval)) == ENUMERAL_TYPE 2169 && (dependent_type_p (ENUM_UNDERLYING_TYPE 2170 (TREE_TYPE (target_decl))) 2171 || dependent_type_p (ENUM_UNDERLYING_TYPE 2172 (TREE_TYPE (target_bval))))))) 2173 /* The new name is the type name. */ 2174 binding->type = decl; 2175 else if (/* TARGET_BVAL is null when push_class_level_binding moves 2176 an inherited type-binding out of the way to make room 2177 for a new value binding. */ 2178 !target_bval 2179 /* TARGET_BVAL is error_mark_node when TARGET_DECL's name 2180 has been used in a non-class scope prior declaration. 2181 In that case, we should have already issued a 2182 diagnostic; for graceful error recovery purpose, pretend 2183 this was the intended declaration for that name. */ 2184 || target_bval == error_mark_node 2185 /* If TARGET_BVAL is anticipated but has not yet been 2186 declared, pretend it is not there at all. */ 2187 || anticipated_builtin_p (target_bval)) 2188 binding->value = decl; 2189 else if (TREE_CODE (target_bval) == TYPE_DECL 2190 && DECL_ARTIFICIAL (target_bval) 2191 && target_decl != target_bval 2192 && (TREE_CODE (target_decl) != TYPE_DECL 2193 || same_type_p (TREE_TYPE (target_decl), 2194 TREE_TYPE (target_bval)))) 2195 { 2196 /* The old binding was a type name. It was placed in 2197 VALUE field because it was thought, at the point it was 2198 declared, to be the only entity with such a name. Move the 2199 type name into the type slot; it is now hidden by the new 2200 binding. */ 2201 binding->type = bval; 2202 binding->value = decl; 2203 binding->value_is_inherited = false; 2204 } 2205 else if (TREE_CODE (target_bval) == TYPE_DECL 2206 && TREE_CODE (target_decl) == TYPE_DECL 2207 && DECL_NAME (target_decl) == DECL_NAME (target_bval) 2208 && binding->scope->kind != sk_class 2209 && (same_type_p (TREE_TYPE (target_decl), TREE_TYPE (target_bval)) 2210 /* If either type involves template parameters, we must 2211 wait until instantiation. */ 2212 || uses_template_parms (TREE_TYPE (target_decl)) 2213 || uses_template_parms (TREE_TYPE (target_bval)))) 2214 /* We have two typedef-names, both naming the same type to have 2215 the same name. In general, this is OK because of: 2216 2217 [dcl.typedef] 2218 2219 In a given scope, a typedef specifier can be used to redefine 2220 the name of any type declared in that scope to refer to the 2221 type to which it already refers. 2222 2223 However, in class scopes, this rule does not apply due to the 2224 stricter language in [class.mem] prohibiting redeclarations of 2225 members. */ 2226 ok = false; 2227 /* There can be two block-scope declarations of the same variable, 2228 so long as they are `extern' declarations. However, there cannot 2229 be two declarations of the same static data member: 2230 2231 [class.mem] 2232 2233 A member shall not be declared twice in the 2234 member-specification. */ 2235 else if (VAR_P (target_decl) 2236 && VAR_P (target_bval) 2237 && DECL_EXTERNAL (target_decl) && DECL_EXTERNAL (target_bval) 2238 && !DECL_CLASS_SCOPE_P (target_decl)) 2239 { 2240 duplicate_decls (decl, binding->value, /*newdecl_is_friend=*/false); 2241 ok = false; 2242 } 2243 else if (TREE_CODE (decl) == NAMESPACE_DECL 2244 && TREE_CODE (bval) == NAMESPACE_DECL 2245 && DECL_NAMESPACE_ALIAS (decl) 2246 && DECL_NAMESPACE_ALIAS (bval) 2247 && ORIGINAL_NAMESPACE (bval) == ORIGINAL_NAMESPACE (decl)) 2248 /* [namespace.alias] 2249 2250 In a declarative region, a namespace-alias-definition can be 2251 used to redefine a namespace-alias declared in that declarative 2252 region to refer only to the namespace to which it already 2253 refers. */ 2254 ok = false; 2255 else 2256 { 2257 if (!error_operand_p (bval)) 2258 diagnose_name_conflict (decl, bval); 2259 ok = false; 2260 } 2261 2262 return ok; 2263 } 2264 2265 /* Diagnose a name conflict between DECL and BVAL. */ 2266 2267 static void 2268 diagnose_name_conflict (tree decl, tree bval) 2269 { 2270 if (TREE_CODE (decl) == TREE_CODE (bval) 2271 && TREE_CODE (decl) != NAMESPACE_DECL 2272 && !DECL_DECLARES_FUNCTION_P (decl) 2273 && (TREE_CODE (decl) != TYPE_DECL 2274 || DECL_ARTIFICIAL (decl) == DECL_ARTIFICIAL (bval)) 2275 && CP_DECL_CONTEXT (decl) == CP_DECL_CONTEXT (bval)) 2276 error ("redeclaration of %q#D", decl); 2277 else 2278 error ("%q#D conflicts with a previous declaration", decl); 2279 2280 inform (location_of (bval), "previous declaration %q#D", bval); 2281 } 2282 2283 /* Wrapper for supplement_binding_1. */ 2284 2285 static bool 2286 supplement_binding (cxx_binding *binding, tree decl) 2287 { 2288 bool ret; 2289 bool subtime = timevar_cond_start (TV_NAME_LOOKUP); 2290 ret = supplement_binding_1 (binding, decl); 2291 timevar_cond_stop (TV_NAME_LOOKUP, subtime); 2292 return ret; 2293 } 2294 2295 /* Replace BINDING's current value on its scope's name list with 2296 NEWVAL. */ 2297 2298 static void 2299 update_local_overload (cxx_binding *binding, tree newval) 2300 { 2301 tree *d; 2302 2303 for (d = &binding->scope->names; ; d = &TREE_CHAIN (*d)) 2304 if (*d == binding->value) 2305 { 2306 /* Stitch new list node in. */ 2307 *d = tree_cons (NULL_TREE, NULL_TREE, TREE_CHAIN (*d)); 2308 break; 2309 } 2310 else if (TREE_CODE (*d) == TREE_LIST && TREE_VALUE (*d) == binding->value) 2311 break; 2312 2313 TREE_VALUE (*d) = newval; 2314 } 2315 2316 /* Compares the parameter-type-lists of ONE and TWO and 2317 returns false if they are different. If the DECLs are template 2318 functions, the return types and the template parameter lists are 2319 compared too (DR 565). */ 2320 2321 static bool 2322 matching_fn_p (tree one, tree two) 2323 { 2324 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (one)), 2325 TYPE_ARG_TYPES (TREE_TYPE (two)))) 2326 return false; 2327 2328 if (TREE_CODE (one) == TEMPLATE_DECL 2329 && TREE_CODE (two) == TEMPLATE_DECL) 2330 { 2331 /* Compare template parms. */ 2332 if (!comp_template_parms (DECL_TEMPLATE_PARMS (one), 2333 DECL_TEMPLATE_PARMS (two))) 2334 return false; 2335 2336 /* And return type. */ 2337 if (!same_type_p (TREE_TYPE (TREE_TYPE (one)), 2338 TREE_TYPE (TREE_TYPE (two)))) 2339 return false; 2340 } 2341 2342 return true; 2343 } 2344 2345 /* Push DECL into nonclass LEVEL BINDING or SLOT. OLD is the current 2346 binding value (possibly with anticipated builtins stripped). 2347 Diagnose conflicts and return updated decl. */ 2348 2349 static tree 2350 update_binding (cp_binding_level *level, cxx_binding *binding, tree *slot, 2351 tree old, tree decl, bool is_friend) 2352 { 2353 tree to_val = decl; 2354 tree old_type = slot ? MAYBE_STAT_TYPE (*slot) : binding->type; 2355 tree to_type = old_type; 2356 2357 gcc_assert (level->kind == sk_namespace ? !binding 2358 : level->kind != sk_class && !slot); 2359 if (old == error_mark_node) 2360 old = NULL_TREE; 2361 2362 if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl)) 2363 { 2364 tree other = to_type; 2365 2366 if (old && TREE_CODE (old) == TYPE_DECL && DECL_ARTIFICIAL (old)) 2367 other = old; 2368 2369 /* Pushing an artificial typedef. See if this matches either 2370 the type slot or the old value slot. */ 2371 if (!other) 2372 ; 2373 else if (same_type_p (TREE_TYPE (other), TREE_TYPE (decl))) 2374 /* Two artificial decls to same type. Do nothing. */ 2375 return other; 2376 else 2377 goto conflict; 2378 2379 if (old) 2380 { 2381 /* Slide decl into the type slot, keep old unaltered */ 2382 to_type = decl; 2383 to_val = old; 2384 goto done; 2385 } 2386 } 2387 2388 if (old && TREE_CODE (old) == TYPE_DECL && DECL_ARTIFICIAL (old)) 2389 { 2390 /* Slide old into the type slot. */ 2391 to_type = old; 2392 old = NULL_TREE; 2393 } 2394 2395 if (DECL_DECLARES_FUNCTION_P (decl)) 2396 { 2397 if (!old) 2398 ; 2399 else if (OVL_P (old)) 2400 { 2401 for (ovl_iterator iter (old); iter; ++iter) 2402 { 2403 tree fn = *iter; 2404 2405 if (iter.using_p () && matching_fn_p (fn, decl)) 2406 { 2407 /* If a function declaration in namespace scope or 2408 block scope has the same name and the same 2409 parameter-type- list (8.3.5) as a function 2410 introduced by a using-declaration, and the 2411 declarations do not declare the same function, 2412 the program is ill-formed. [namespace.udecl]/14 */ 2413 if (tree match = duplicate_decls (decl, fn, is_friend)) 2414 return match; 2415 else 2416 /* FIXME: To preserve existing error behavior, we 2417 still push the decl. This might change. */ 2418 diagnose_name_conflict (decl, fn); 2419 } 2420 } 2421 } 2422 else 2423 goto conflict; 2424 2425 if (to_type != old_type 2426 && warn_shadow 2427 && MAYBE_CLASS_TYPE_P (TREE_TYPE (to_type)) 2428 && !(DECL_IN_SYSTEM_HEADER (decl) 2429 && DECL_IN_SYSTEM_HEADER (to_type))) 2430 warning (OPT_Wshadow, "%q#D hides constructor for %q#D", 2431 decl, to_type); 2432 2433 to_val = ovl_insert (decl, old); 2434 } 2435 else if (!old) 2436 ; 2437 else if (TREE_CODE (old) != TREE_CODE (decl)) 2438 /* Different kinds of decls conflict. */ 2439 goto conflict; 2440 else if (TREE_CODE (old) == TYPE_DECL) 2441 { 2442 if (same_type_p (TREE_TYPE (old), TREE_TYPE (decl))) 2443 /* Two type decls to the same type. Do nothing. */ 2444 return old; 2445 else 2446 goto conflict; 2447 } 2448 else if (TREE_CODE (old) == NAMESPACE_DECL) 2449 { 2450 /* Two maybe-aliased namespaces. If they're to the same target 2451 namespace, that's ok. */ 2452 if (ORIGINAL_NAMESPACE (old) != ORIGINAL_NAMESPACE (decl)) 2453 goto conflict; 2454 2455 /* The new one must be an alias at this point. */ 2456 gcc_assert (DECL_NAMESPACE_ALIAS (decl)); 2457 return old; 2458 } 2459 else if (TREE_CODE (old) == VAR_DECL) 2460 { 2461 /* There can be two block-scope declarations of the same 2462 variable, so long as they are `extern' declarations. */ 2463 if (!DECL_EXTERNAL (old) || !DECL_EXTERNAL (decl)) 2464 goto conflict; 2465 else if (tree match = duplicate_decls (decl, old, false)) 2466 return match; 2467 else 2468 goto conflict; 2469 } 2470 else 2471 { 2472 conflict: 2473 diagnose_name_conflict (decl, old); 2474 to_val = NULL_TREE; 2475 } 2476 2477 done: 2478 if (to_val) 2479 { 2480 if (level->kind == sk_namespace || to_type == decl || to_val == decl) 2481 add_decl_to_level (level, decl); 2482 else 2483 { 2484 gcc_checking_assert (binding->value && OVL_P (binding->value)); 2485 update_local_overload (binding, to_val); 2486 } 2487 2488 if (slot) 2489 { 2490 if (STAT_HACK_P (*slot)) 2491 { 2492 STAT_TYPE (*slot) = to_type; 2493 STAT_DECL (*slot) = to_val; 2494 } 2495 else if (to_type) 2496 *slot = stat_hack (to_val, to_type); 2497 else 2498 *slot = to_val; 2499 } 2500 else 2501 { 2502 binding->type = to_type; 2503 binding->value = to_val; 2504 } 2505 } 2506 2507 return decl; 2508 } 2509 2510 /* Table of identifiers to extern C declarations (or LISTS thereof). */ 2511 2512 static GTY(()) hash_table<named_decl_hash> *extern_c_decls; 2513 2514 /* DECL has C linkage. If we have an existing instance, make sure the 2515 new one is compatible. Make sure it has the same exception 2516 specification [7.5, 7.6]. Add DECL to the map. */ 2517 2518 static void 2519 check_extern_c_conflict (tree decl) 2520 { 2521 /* Ignore artificial or system header decls. */ 2522 if (DECL_ARTIFICIAL (decl) || DECL_IN_SYSTEM_HEADER (decl)) 2523 return; 2524 2525 /* This only applies to decls at namespace scope. */ 2526 if (!DECL_NAMESPACE_SCOPE_P (decl)) 2527 return; 2528 2529 if (!extern_c_decls) 2530 extern_c_decls = hash_table<named_decl_hash>::create_ggc (127); 2531 2532 tree *slot = extern_c_decls 2533 ->find_slot_with_hash (DECL_NAME (decl), 2534 IDENTIFIER_HASH_VALUE (DECL_NAME (decl)), INSERT); 2535 if (tree old = *slot) 2536 { 2537 if (TREE_CODE (old) == OVERLOAD) 2538 old = OVL_FUNCTION (old); 2539 2540 int mismatch = 0; 2541 if (DECL_CONTEXT (old) == DECL_CONTEXT (decl)) 2542 ; /* If they're in the same context, we'll have already complained 2543 about a (possible) mismatch, when inserting the decl. */ 2544 else if (!decls_match (decl, old)) 2545 mismatch = 1; 2546 else if (TREE_CODE (decl) == FUNCTION_DECL 2547 && !comp_except_specs (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (old)), 2548 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl)), 2549 ce_normal)) 2550 mismatch = -1; 2551 else if (DECL_ASSEMBLER_NAME_SET_P (old)) 2552 SET_DECL_ASSEMBLER_NAME (decl, DECL_ASSEMBLER_NAME (old)); 2553 2554 if (mismatch) 2555 { 2556 auto_diagnostic_group d; 2557 pedwarn (input_location, 0, 2558 "conflicting C language linkage declaration %q#D", decl); 2559 inform (DECL_SOURCE_LOCATION (old), 2560 "previous declaration %q#D", old); 2561 if (mismatch < 0) 2562 inform (input_location, 2563 "due to different exception specifications"); 2564 } 2565 else 2566 { 2567 if (old == *slot) 2568 /* The hash table expects OVERLOADS, so construct one with 2569 OLD as both the function and the chain. This allocate 2570 an excess OVERLOAD node, but it's rare to have multiple 2571 extern "C" decls of the same name. And we save 2572 complicating the hash table logic (which is used 2573 elsewhere). */ 2574 *slot = ovl_make (old, old); 2575 2576 slot = &OVL_CHAIN (*slot); 2577 2578 /* Chain it on for c_linkage_binding's use. */ 2579 *slot = tree_cons (NULL_TREE, decl, *slot); 2580 } 2581 } 2582 else 2583 *slot = decl; 2584 } 2585 2586 /* Returns a list of C-linkage decls with the name NAME. Used in 2587 c-family/c-pragma.c to implement redefine_extname pragma. */ 2588 2589 tree 2590 c_linkage_bindings (tree name) 2591 { 2592 if (extern_c_decls) 2593 if (tree *slot = extern_c_decls 2594 ->find_slot_with_hash (name, IDENTIFIER_HASH_VALUE (name), NO_INSERT)) 2595 { 2596 tree result = *slot; 2597 if (TREE_CODE (result) == OVERLOAD) 2598 result = OVL_CHAIN (result); 2599 return result; 2600 } 2601 2602 return NULL_TREE; 2603 } 2604 2605 /* Subroutine of check_local_shadow. */ 2606 2607 static void 2608 inform_shadowed (tree shadowed) 2609 { 2610 inform (DECL_SOURCE_LOCATION (shadowed), 2611 "shadowed declaration is here"); 2612 } 2613 2614 /* DECL is being declared at a local scope. Emit suitable shadow 2615 warnings. */ 2616 2617 static void 2618 check_local_shadow (tree decl) 2619 { 2620 /* Don't complain about the parms we push and then pop 2621 while tentatively parsing a function declarator. */ 2622 if (TREE_CODE (decl) == PARM_DECL && !DECL_CONTEXT (decl)) 2623 return; 2624 2625 /* External decls are something else. */ 2626 if (DECL_EXTERNAL (decl)) 2627 return; 2628 2629 tree old = NULL_TREE; 2630 cp_binding_level *old_scope = NULL; 2631 if (cxx_binding *binding = outer_binding (DECL_NAME (decl), NULL, true)) 2632 { 2633 old = binding->value; 2634 old_scope = binding->scope; 2635 } 2636 2637 if (old 2638 && (TREE_CODE (old) == PARM_DECL 2639 || VAR_P (old) 2640 || (TREE_CODE (old) == TYPE_DECL 2641 && (!DECL_ARTIFICIAL (old) 2642 || TREE_CODE (decl) == TYPE_DECL))) 2643 && DECL_FUNCTION_SCOPE_P (old) 2644 && (!DECL_ARTIFICIAL (decl) 2645 || is_capture_proxy (decl) 2646 || DECL_IMPLICIT_TYPEDEF_P (decl) 2647 || (VAR_P (decl) && DECL_ANON_UNION_VAR_P (decl)))) 2648 { 2649 /* DECL shadows a local thing possibly of interest. */ 2650 2651 /* DR 2211: check that captures and parameters 2652 do not have the same name. */ 2653 if (is_capture_proxy (decl)) 2654 { 2655 if (current_lambda_expr () 2656 && DECL_CONTEXT (old) == lambda_function (current_lambda_expr ()) 2657 && TREE_CODE (old) == PARM_DECL 2658 && DECL_NAME (decl) != this_identifier) 2659 { 2660 error_at (DECL_SOURCE_LOCATION (old), 2661 "lambda parameter %qD " 2662 "previously declared as a capture", old); 2663 } 2664 return; 2665 } 2666 /* Don't complain if it's from an enclosing function. */ 2667 else if (DECL_CONTEXT (old) == current_function_decl 2668 && TREE_CODE (decl) != PARM_DECL 2669 && TREE_CODE (old) == PARM_DECL) 2670 { 2671 /* Go to where the parms should be and see if we find 2672 them there. */ 2673 cp_binding_level *b = current_binding_level->level_chain; 2674 2675 if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl)) 2676 /* Skip the ctor/dtor cleanup level. */ 2677 b = b->level_chain; 2678 2679 /* ARM $8.3 */ 2680 if (b->kind == sk_function_parms) 2681 { 2682 error ("declaration of %q#D shadows a parameter", decl); 2683 return; 2684 } 2685 } 2686 2687 /* The local structure or class can't use parameters of 2688 the containing function anyway. */ 2689 if (DECL_CONTEXT (old) != current_function_decl) 2690 { 2691 for (cp_binding_level *scope = current_binding_level; 2692 scope != old_scope; scope = scope->level_chain) 2693 if (scope->kind == sk_class 2694 && !LAMBDA_TYPE_P (scope->this_entity)) 2695 return; 2696 } 2697 /* Error if redeclaring a local declared in a 2698 init-statement or in the condition of an if or 2699 switch statement when the new declaration is in the 2700 outermost block of the controlled statement. 2701 Redeclaring a variable from a for or while condition is 2702 detected elsewhere. */ 2703 else if (VAR_P (old) 2704 && old_scope == current_binding_level->level_chain 2705 && (old_scope->kind == sk_cond || old_scope->kind == sk_for)) 2706 { 2707 auto_diagnostic_group d; 2708 error ("redeclaration of %q#D", decl); 2709 inform (DECL_SOURCE_LOCATION (old), 2710 "%q#D previously declared here", old); 2711 return; 2712 } 2713 /* C++11: 2714 3.3.3/3: The name declared in an exception-declaration (...) 2715 shall not be redeclared in the outermost block of the handler. 2716 3.3.3/2: A parameter name shall not be redeclared (...) in 2717 the outermost block of any handler associated with a 2718 function-try-block. 2719 3.4.1/15: The function parameter names shall not be redeclared 2720 in the exception-declaration nor in the outermost block of a 2721 handler for the function-try-block. */ 2722 else if ((TREE_CODE (old) == VAR_DECL 2723 && old_scope == current_binding_level->level_chain 2724 && old_scope->kind == sk_catch) 2725 || (TREE_CODE (old) == PARM_DECL 2726 && (current_binding_level->kind == sk_catch 2727 || current_binding_level->level_chain->kind == sk_catch) 2728 && in_function_try_handler)) 2729 { 2730 auto_diagnostic_group d; 2731 if (permerror (input_location, "redeclaration of %q#D", decl)) 2732 inform (DECL_SOURCE_LOCATION (old), 2733 "%q#D previously declared here", old); 2734 return; 2735 } 2736 2737 /* If '-Wshadow=compatible-local' is specified without other 2738 -Wshadow= flags, we will warn only when the type of the 2739 shadowing variable (DECL) can be converted to that of the 2740 shadowed parameter (OLD_LOCAL). The reason why we only check 2741 if DECL's type can be converted to OLD_LOCAL's type (but not the 2742 other way around) is because when users accidentally shadow a 2743 parameter, more than often they would use the variable 2744 thinking (mistakenly) it's still the parameter. It would be 2745 rare that users would use the variable in the place that 2746 expects the parameter but thinking it's a new decl. */ 2747 2748 enum opt_code warning_code; 2749 if (warn_shadow) 2750 warning_code = OPT_Wshadow; 2751 else if (warn_shadow_local) 2752 warning_code = OPT_Wshadow_local; 2753 else if (warn_shadow_compatible_local 2754 && (same_type_p (TREE_TYPE (old), TREE_TYPE (decl)) 2755 || (!dependent_type_p (TREE_TYPE (decl)) 2756 && !dependent_type_p (TREE_TYPE (old)) 2757 /* If the new decl uses auto, we don't yet know 2758 its type (the old type cannot be using auto 2759 at this point, without also being 2760 dependent). This is an indication we're 2761 (now) doing the shadow checking too 2762 early. */ 2763 && !type_uses_auto (TREE_TYPE (decl)) 2764 && can_convert (TREE_TYPE (old), TREE_TYPE (decl), 2765 tf_none)))) 2766 warning_code = OPT_Wshadow_compatible_local; 2767 else 2768 return; 2769 2770 const char *msg; 2771 if (TREE_CODE (old) == PARM_DECL) 2772 msg = "declaration of %q#D shadows a parameter"; 2773 else if (is_capture_proxy (old)) 2774 msg = "declaration of %qD shadows a lambda capture"; 2775 else 2776 msg = "declaration of %qD shadows a previous local"; 2777 2778 auto_diagnostic_group d; 2779 if (warning_at (input_location, warning_code, msg, decl)) 2780 inform_shadowed (old); 2781 return; 2782 } 2783 2784 if (!warn_shadow) 2785 return; 2786 2787 /* Don't warn for artificial things that are not implicit typedefs. */ 2788 if (DECL_ARTIFICIAL (decl) && !DECL_IMPLICIT_TYPEDEF_P (decl)) 2789 return; 2790 2791 if (nonlambda_method_basetype ()) 2792 if (tree member = lookup_member (current_nonlambda_class_type (), 2793 DECL_NAME (decl), /*protect=*/0, 2794 /*want_type=*/false, tf_warning_or_error)) 2795 { 2796 member = MAYBE_BASELINK_FUNCTIONS (member); 2797 2798 /* Warn if a variable shadows a non-function, or the variable 2799 is a function or a pointer-to-function. */ 2800 if (!OVL_P (member) 2801 || TREE_CODE (decl) == FUNCTION_DECL 2802 || TYPE_PTRFN_P (TREE_TYPE (decl)) 2803 || TYPE_PTRMEMFUNC_P (TREE_TYPE (decl))) 2804 { 2805 auto_diagnostic_group d; 2806 if (warning_at (input_location, OPT_Wshadow, 2807 "declaration of %qD shadows a member of %qT", 2808 decl, current_nonlambda_class_type ()) 2809 && DECL_P (member)) 2810 inform_shadowed (member); 2811 } 2812 return; 2813 } 2814 2815 /* Now look for a namespace shadow. */ 2816 old = find_namespace_value (current_namespace, DECL_NAME (decl)); 2817 if (old 2818 && (VAR_P (old) 2819 || (TREE_CODE (old) == TYPE_DECL 2820 && (!DECL_ARTIFICIAL (old) 2821 || TREE_CODE (decl) == TYPE_DECL))) 2822 && !instantiating_current_function_p ()) 2823 /* XXX shadow warnings in outer-more namespaces */ 2824 { 2825 auto_diagnostic_group d; 2826 if (warning_at (input_location, OPT_Wshadow, 2827 "declaration of %qD shadows a global declaration", 2828 decl)) 2829 inform_shadowed (old); 2830 return; 2831 } 2832 2833 return; 2834 } 2835 2836 /* DECL is being pushed inside function CTX. Set its context, if 2837 needed. */ 2838 2839 static void 2840 set_decl_context_in_fn (tree ctx, tree decl) 2841 { 2842 if (!DECL_CONTEXT (decl) 2843 /* A local declaration for a function doesn't constitute 2844 nesting. */ 2845 && TREE_CODE (decl) != FUNCTION_DECL 2846 /* A local declaration for an `extern' variable is in the 2847 scope of the current namespace, not the current 2848 function. */ 2849 && !(VAR_P (decl) && DECL_EXTERNAL (decl)) 2850 /* When parsing the parameter list of a function declarator, 2851 don't set DECL_CONTEXT to an enclosing function. When we 2852 push the PARM_DECLs in order to process the function body, 2853 current_binding_level->this_entity will be set. */ 2854 && !(TREE_CODE (decl) == PARM_DECL 2855 && current_binding_level->kind == sk_function_parms 2856 && current_binding_level->this_entity == NULL)) 2857 DECL_CONTEXT (decl) = ctx; 2858 2859 /* If this is the declaration for a namespace-scope function, 2860 but the declaration itself is in a local scope, mark the 2861 declaration. */ 2862 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (decl)) 2863 DECL_LOCAL_FUNCTION_P (decl) = 1; 2864 } 2865 2866 /* DECL is a local-scope decl with linkage. SHADOWED is true if the 2867 name is already bound at the current level. 2868 2869 [basic.link] If there is a visible declaration of an entity with 2870 linkage having the same name and type, ignoring entities declared 2871 outside the innermost enclosing namespace scope, the block scope 2872 declaration declares that same entity and receives the linkage of 2873 the previous declaration. 2874 2875 Also, make sure that this decl matches any existing external decl 2876 in the enclosing namespace. */ 2877 2878 static void 2879 set_local_extern_decl_linkage (tree decl, bool shadowed) 2880 { 2881 tree ns_value = decl; /* Unique marker. */ 2882 2883 if (!shadowed) 2884 { 2885 tree loc_value = innermost_non_namespace_value (DECL_NAME (decl)); 2886 if (!loc_value) 2887 { 2888 ns_value 2889 = find_namespace_value (current_namespace, DECL_NAME (decl)); 2890 loc_value = ns_value; 2891 } 2892 if (loc_value == error_mark_node 2893 /* An ambiguous lookup. */ 2894 || (loc_value && TREE_CODE (loc_value) == TREE_LIST)) 2895 loc_value = NULL_TREE; 2896 2897 for (ovl_iterator iter (loc_value); iter; ++iter) 2898 if (!iter.hidden_p () 2899 && (TREE_STATIC (*iter) || DECL_EXTERNAL (*iter)) 2900 && decls_match (*iter, decl)) 2901 { 2902 /* The standard only says that the local extern inherits 2903 linkage from the previous decl; in particular, default 2904 args are not shared. Add the decl into a hash table to 2905 make sure only the previous decl in this case is seen 2906 by the middle end. */ 2907 struct cxx_int_tree_map *h; 2908 2909 /* We inherit the outer decl's linkage. But we're a 2910 different decl. */ 2911 TREE_PUBLIC (decl) = TREE_PUBLIC (*iter); 2912 2913 if (cp_function_chain->extern_decl_map == NULL) 2914 cp_function_chain->extern_decl_map 2915 = hash_table<cxx_int_tree_map_hasher>::create_ggc (20); 2916 2917 h = ggc_alloc<cxx_int_tree_map> (); 2918 h->uid = DECL_UID (decl); 2919 h->to = *iter; 2920 cxx_int_tree_map **loc = cp_function_chain->extern_decl_map 2921 ->find_slot (h, INSERT); 2922 *loc = h; 2923 break; 2924 } 2925 } 2926 2927 if (TREE_PUBLIC (decl)) 2928 { 2929 /* DECL is externally visible. Make sure it matches a matching 2930 decl in the namespace scope. We only really need to check 2931 this when inserting the decl, not when we find an existing 2932 match in the current scope. However, in practice we're 2933 going to be inserting a new decl in the majority of cases -- 2934 who writes multiple extern decls for the same thing in the 2935 same local scope? Doing it here often avoids a duplicate 2936 namespace lookup. */ 2937 2938 /* Avoid repeating a lookup. */ 2939 if (ns_value == decl) 2940 ns_value = find_namespace_value (current_namespace, DECL_NAME (decl)); 2941 2942 if (ns_value == error_mark_node 2943 || (ns_value && TREE_CODE (ns_value) == TREE_LIST)) 2944 ns_value = NULL_TREE; 2945 2946 for (ovl_iterator iter (ns_value); iter; ++iter) 2947 { 2948 tree other = *iter; 2949 2950 if (!(TREE_PUBLIC (other) || DECL_EXTERNAL (other))) 2951 ; /* Not externally visible. */ 2952 else if (DECL_EXTERN_C_P (decl) && DECL_EXTERN_C_P (other)) 2953 ; /* Both are extern "C", we'll check via that mechanism. */ 2954 else if (TREE_CODE (other) != TREE_CODE (decl) 2955 || ((VAR_P (decl) || matching_fn_p (other, decl)) 2956 && !comptypes (TREE_TYPE (decl), TREE_TYPE (other), 2957 COMPARE_REDECLARATION))) 2958 { 2959 auto_diagnostic_group d; 2960 if (permerror (DECL_SOURCE_LOCATION (decl), 2961 "local external declaration %q#D", decl)) 2962 inform (DECL_SOURCE_LOCATION (other), 2963 "does not match previous declaration %q#D", other); 2964 break; 2965 } 2966 } 2967 } 2968 } 2969 2970 /* Record DECL as belonging to the current lexical scope. Check for 2971 errors (such as an incompatible declaration for the same name 2972 already seen in the same scope). IS_FRIEND is true if DECL is 2973 declared as a friend. 2974 2975 Returns either DECL or an old decl for the same name. If an old 2976 decl is returned, it may have been smashed to agree with what DECL 2977 says. */ 2978 2979 static tree 2980 do_pushdecl (tree decl, bool is_friend) 2981 { 2982 if (decl == error_mark_node) 2983 return error_mark_node; 2984 2985 if (!DECL_TEMPLATE_PARM_P (decl) && current_function_decl) 2986 set_decl_context_in_fn (current_function_decl, decl); 2987 2988 /* The binding level we will be pushing into. During local class 2989 pushing, we want to push to the containing scope. */ 2990 cp_binding_level *level = current_binding_level; 2991 while (level->kind == sk_class) 2992 level = level->level_chain; 2993 2994 /* An anonymous namespace has a NULL DECL_NAME, but we still want to 2995 insert it. Other NULL-named decls, not so much. */ 2996 tree name = DECL_NAME (decl); 2997 if (name || TREE_CODE (decl) == NAMESPACE_DECL) 2998 { 2999 cxx_binding *binding = NULL; /* Local scope binding. */ 3000 tree ns = NULL_TREE; /* Searched namespace. */ 3001 tree *slot = NULL; /* Binding slot in namespace. */ 3002 tree old = NULL_TREE; 3003 3004 if (level->kind == sk_namespace) 3005 { 3006 /* We look in the decl's namespace for an existing 3007 declaration, even though we push into the current 3008 namespace. */ 3009 ns = (DECL_NAMESPACE_SCOPE_P (decl) 3010 ? CP_DECL_CONTEXT (decl) : current_namespace); 3011 /* Create the binding, if this is current namespace, because 3012 that's where we'll be pushing anyway. */ 3013 slot = find_namespace_slot (ns, name, ns == current_namespace); 3014 if (slot) 3015 old = MAYBE_STAT_DECL (*slot); 3016 } 3017 else 3018 { 3019 binding = find_local_binding (level, name); 3020 if (binding) 3021 old = binding->value; 3022 } 3023 3024 if (current_function_decl && VAR_OR_FUNCTION_DECL_P (decl) 3025 && DECL_EXTERNAL (decl)) 3026 set_local_extern_decl_linkage (decl, old != NULL_TREE); 3027 3028 if (old == error_mark_node) 3029 old = NULL_TREE; 3030 3031 for (ovl_iterator iter (old); iter; ++iter) 3032 if (iter.using_p ()) 3033 ; /* Ignore using decls here. */ 3034 else if (tree match = duplicate_decls (decl, *iter, is_friend)) 3035 { 3036 if (match == error_mark_node) 3037 ; 3038 else if (TREE_CODE (match) == TYPE_DECL) 3039 /* The IDENTIFIER will have the type referring to the 3040 now-smashed TYPE_DECL, because ...? Reset it. */ 3041 SET_IDENTIFIER_TYPE_VALUE (name, TREE_TYPE (match)); 3042 else if (iter.hidden_p () && !DECL_HIDDEN_P (match)) 3043 { 3044 /* Unhiding a previously hidden decl. */ 3045 tree head = iter.reveal_node (old); 3046 if (head != old) 3047 { 3048 if (!ns) 3049 { 3050 update_local_overload (binding, head); 3051 binding->value = head; 3052 } 3053 else if (STAT_HACK_P (*slot)) 3054 STAT_DECL (*slot) = head; 3055 else 3056 *slot = head; 3057 } 3058 if (DECL_EXTERN_C_P (match)) 3059 /* We need to check and register the decl now. */ 3060 check_extern_c_conflict (match); 3061 } 3062 return match; 3063 } 3064 3065 /* We are pushing a new decl. */ 3066 3067 /* Skip a hidden builtin we failed to match already. There can 3068 only be one. */ 3069 if (old && anticipated_builtin_p (old)) 3070 old = OVL_CHAIN (old); 3071 3072 check_template_shadow (decl); 3073 3074 if (DECL_DECLARES_FUNCTION_P (decl)) 3075 { 3076 check_default_args (decl); 3077 3078 if (is_friend) 3079 { 3080 if (level->kind != sk_namespace) 3081 { 3082 /* In a local class, a friend function declaration must 3083 find a matching decl in the innermost non-class scope. 3084 [class.friend/11] */ 3085 error ("friend declaration %qD in local class without " 3086 "prior local declaration", decl); 3087 /* Don't attempt to push it. */ 3088 return error_mark_node; 3089 } 3090 /* Hide it from ordinary lookup. */ 3091 DECL_ANTICIPATED (decl) = DECL_HIDDEN_FRIEND_P (decl) = true; 3092 } 3093 } 3094 3095 if (level->kind != sk_namespace) 3096 { 3097 check_local_shadow (decl); 3098 3099 if (TREE_CODE (decl) == NAMESPACE_DECL) 3100 /* A local namespace alias. */ 3101 set_identifier_type_value (name, NULL_TREE); 3102 3103 if (!binding) 3104 binding = create_local_binding (level, name); 3105 } 3106 else if (!slot) 3107 { 3108 ns = current_namespace; 3109 slot = find_namespace_slot (ns, name, true); 3110 /* Update OLD to reflect the namespace we're going to be 3111 pushing into. */ 3112 old = MAYBE_STAT_DECL (*slot); 3113 } 3114 3115 old = update_binding (level, binding, slot, old, decl, is_friend); 3116 3117 if (old != decl) 3118 /* An existing decl matched, use it. */ 3119 decl = old; 3120 else if (TREE_CODE (decl) == TYPE_DECL) 3121 { 3122 tree type = TREE_TYPE (decl); 3123 3124 if (type != error_mark_node) 3125 { 3126 if (TYPE_NAME (type) != decl) 3127 set_underlying_type (decl); 3128 3129 if (!ns) 3130 set_identifier_type_value_with_scope (name, decl, level); 3131 else 3132 SET_IDENTIFIER_TYPE_VALUE (name, global_type_node); 3133 } 3134 3135 /* If this is a locally defined typedef in a function that 3136 is not a template instantation, record it to implement 3137 -Wunused-local-typedefs. */ 3138 if (!instantiating_current_function_p ()) 3139 record_locally_defined_typedef (decl); 3140 } 3141 else if (VAR_P (decl)) 3142 maybe_register_incomplete_var (decl); 3143 3144 if ((VAR_P (decl) || TREE_CODE (decl) == FUNCTION_DECL) 3145 && DECL_EXTERN_C_P (decl)) 3146 check_extern_c_conflict (decl); 3147 } 3148 else 3149 add_decl_to_level (level, decl); 3150 3151 return decl; 3152 } 3153 3154 /* Record a decl-node X as belonging to the current lexical scope. 3155 It's a friend if IS_FRIEND is true -- which affects exactly where 3156 we push it. */ 3157 3158 tree 3159 pushdecl (tree x, bool is_friend) 3160 { 3161 bool subtime = timevar_cond_start (TV_NAME_LOOKUP); 3162 tree ret = do_pushdecl (x, is_friend); 3163 timevar_cond_stop (TV_NAME_LOOKUP, subtime); 3164 return ret; 3165 } 3166 3167 /* Enter DECL into the symbol table, if that's appropriate. Returns 3168 DECL, or a modified version thereof. */ 3169 3170 tree 3171 maybe_push_decl (tree decl) 3172 { 3173 tree type = TREE_TYPE (decl); 3174 3175 /* Add this decl to the current binding level, but not if it comes 3176 from another scope, e.g. a static member variable. TEM may equal 3177 DECL or it may be a previous decl of the same name. */ 3178 if (decl == error_mark_node 3179 || (TREE_CODE (decl) != PARM_DECL 3180 && DECL_CONTEXT (decl) != NULL_TREE 3181 /* Definitions of namespace members outside their namespace are 3182 possible. */ 3183 && !DECL_NAMESPACE_SCOPE_P (decl)) 3184 || (TREE_CODE (decl) == TEMPLATE_DECL && !namespace_bindings_p ()) 3185 || type == unknown_type_node 3186 /* The declaration of a template specialization does not affect 3187 the functions available for overload resolution, so we do not 3188 call pushdecl. */ 3189 || (TREE_CODE (decl) == FUNCTION_DECL 3190 && DECL_TEMPLATE_SPECIALIZATION (decl))) 3191 return decl; 3192 else 3193 return pushdecl (decl); 3194 } 3195 3196 /* Bind DECL to ID in the current_binding_level, assumed to be a local 3197 binding level. If IS_USING is true, DECL got here through a 3198 using-declaration. */ 3199 3200 static void 3201 push_local_binding (tree id, tree decl, bool is_using) 3202 { 3203 /* Skip over any local classes. This makes sense if we call 3204 push_local_binding with a friend decl of a local class. */ 3205 cp_binding_level *b = innermost_nonclass_level (); 3206 3207 gcc_assert (b->kind != sk_namespace); 3208 if (find_local_binding (b, id)) 3209 { 3210 /* Supplement the existing binding. */ 3211 if (!supplement_binding (IDENTIFIER_BINDING (id), decl)) 3212 /* It didn't work. Something else must be bound at this 3213 level. Do not add DECL to the list of things to pop 3214 later. */ 3215 return; 3216 } 3217 else 3218 /* Create a new binding. */ 3219 push_binding (id, decl, b); 3220 3221 if (TREE_CODE (decl) == OVERLOAD || is_using) 3222 /* We must put the OVERLOAD or using into a TREE_LIST since we 3223 cannot use the decl's chain itself. */ 3224 decl = build_tree_list (NULL_TREE, decl); 3225 3226 /* And put DECL on the list of things declared by the current 3227 binding level. */ 3228 add_decl_to_level (b, decl); 3229 } 3230 3231 3232 /* true means unconditionally make a BLOCK for the next level pushed. */ 3233 3234 static bool keep_next_level_flag; 3235 3236 static int binding_depth = 0; 3237 3238 static void 3239 indent (int depth) 3240 { 3241 int i; 3242 3243 for (i = 0; i < depth * 2; i++) 3244 putc (' ', stderr); 3245 } 3246 3247 /* Return a string describing the kind of SCOPE we have. */ 3248 static const char * 3249 cp_binding_level_descriptor (cp_binding_level *scope) 3250 { 3251 /* The order of this table must match the "scope_kind" 3252 enumerators. */ 3253 static const char* scope_kind_names[] = { 3254 "block-scope", 3255 "cleanup-scope", 3256 "try-scope", 3257 "catch-scope", 3258 "for-scope", 3259 "function-parameter-scope", 3260 "class-scope", 3261 "namespace-scope", 3262 "template-parameter-scope", 3263 "template-explicit-spec-scope" 3264 }; 3265 const scope_kind kind = scope->explicit_spec_p 3266 ? sk_template_spec : scope->kind; 3267 3268 return scope_kind_names[kind]; 3269 } 3270 3271 /* Output a debugging information about SCOPE when performing 3272 ACTION at LINE. */ 3273 static void 3274 cp_binding_level_debug (cp_binding_level *scope, int line, const char *action) 3275 { 3276 const char *desc = cp_binding_level_descriptor (scope); 3277 if (scope->this_entity) 3278 verbatim ("%s %<%s(%E)%> %p %d\n", action, desc, 3279 scope->this_entity, (void *) scope, line); 3280 else 3281 verbatim ("%s %s %p %d\n", action, desc, (void *) scope, line); 3282 } 3283 3284 /* A chain of binding_level structures awaiting reuse. */ 3285 3286 static GTY((deletable)) cp_binding_level *free_binding_level; 3287 3288 /* Insert SCOPE as the innermost binding level. */ 3289 3290 void 3291 push_binding_level (cp_binding_level *scope) 3292 { 3293 /* Add it to the front of currently active scopes stack. */ 3294 scope->level_chain = current_binding_level; 3295 current_binding_level = scope; 3296 keep_next_level_flag = false; 3297 3298 if (ENABLE_SCOPE_CHECKING) 3299 { 3300 scope->binding_depth = binding_depth; 3301 indent (binding_depth); 3302 cp_binding_level_debug (scope, LOCATION_LINE (input_location), 3303 "push"); 3304 binding_depth++; 3305 } 3306 } 3307 3308 /* Create a new KIND scope and make it the top of the active scopes stack. 3309 ENTITY is the scope of the associated C++ entity (namespace, class, 3310 function, C++0x enumeration); it is NULL otherwise. */ 3311 3312 cp_binding_level * 3313 begin_scope (scope_kind kind, tree entity) 3314 { 3315 cp_binding_level *scope; 3316 3317 /* Reuse or create a struct for this binding level. */ 3318 if (!ENABLE_SCOPE_CHECKING && free_binding_level) 3319 { 3320 scope = free_binding_level; 3321 free_binding_level = scope->level_chain; 3322 memset (scope, 0, sizeof (cp_binding_level)); 3323 } 3324 else 3325 scope = ggc_cleared_alloc<cp_binding_level> (); 3326 3327 scope->this_entity = entity; 3328 scope->more_cleanups_ok = true; 3329 switch (kind) 3330 { 3331 case sk_cleanup: 3332 scope->keep = true; 3333 break; 3334 3335 case sk_template_spec: 3336 scope->explicit_spec_p = true; 3337 kind = sk_template_parms; 3338 /* Fall through. */ 3339 case sk_template_parms: 3340 case sk_block: 3341 case sk_try: 3342 case sk_catch: 3343 case sk_for: 3344 case sk_cond: 3345 case sk_class: 3346 case sk_scoped_enum: 3347 case sk_function_parms: 3348 case sk_transaction: 3349 case sk_omp: 3350 scope->keep = keep_next_level_flag; 3351 break; 3352 3353 case sk_namespace: 3354 NAMESPACE_LEVEL (entity) = scope; 3355 break; 3356 3357 default: 3358 /* Should not happen. */ 3359 gcc_unreachable (); 3360 break; 3361 } 3362 scope->kind = kind; 3363 3364 push_binding_level (scope); 3365 3366 return scope; 3367 } 3368 3369 /* We're about to leave current scope. Pop the top of the stack of 3370 currently active scopes. Return the enclosing scope, now active. */ 3371 3372 cp_binding_level * 3373 leave_scope (void) 3374 { 3375 cp_binding_level *scope = current_binding_level; 3376 3377 if (scope->kind == sk_namespace && class_binding_level) 3378 current_binding_level = class_binding_level; 3379 3380 /* We cannot leave a scope, if there are none left. */ 3381 if (NAMESPACE_LEVEL (global_namespace)) 3382 gcc_assert (!global_scope_p (scope)); 3383 3384 if (ENABLE_SCOPE_CHECKING) 3385 { 3386 indent (--binding_depth); 3387 cp_binding_level_debug (scope, LOCATION_LINE (input_location), 3388 "leave"); 3389 } 3390 3391 /* Move one nesting level up. */ 3392 current_binding_level = scope->level_chain; 3393 3394 /* Namespace-scopes are left most probably temporarily, not 3395 completely; they can be reopened later, e.g. in namespace-extension 3396 or any name binding activity that requires us to resume a 3397 namespace. For classes, we cache some binding levels. For other 3398 scopes, we just make the structure available for reuse. */ 3399 if (scope->kind != sk_namespace 3400 && scope->kind != sk_class) 3401 { 3402 scope->level_chain = free_binding_level; 3403 gcc_assert (!ENABLE_SCOPE_CHECKING 3404 || scope->binding_depth == binding_depth); 3405 free_binding_level = scope; 3406 } 3407 3408 if (scope->kind == sk_class) 3409 { 3410 /* Reset DEFINING_CLASS_P to allow for reuse of a 3411 class-defining scope in a non-defining context. */ 3412 scope->defining_class_p = 0; 3413 3414 /* Find the innermost enclosing class scope, and reset 3415 CLASS_BINDING_LEVEL appropriately. */ 3416 class_binding_level = NULL; 3417 for (scope = current_binding_level; scope; scope = scope->level_chain) 3418 if (scope->kind == sk_class) 3419 { 3420 class_binding_level = scope; 3421 break; 3422 } 3423 } 3424 3425 return current_binding_level; 3426 } 3427 3428 static void 3429 resume_scope (cp_binding_level* b) 3430 { 3431 /* Resuming binding levels is meant only for namespaces, 3432 and those cannot nest into classes. */ 3433 gcc_assert (!class_binding_level); 3434 /* Also, resuming a non-directly nested namespace is a no-no. */ 3435 gcc_assert (b->level_chain == current_binding_level); 3436 current_binding_level = b; 3437 if (ENABLE_SCOPE_CHECKING) 3438 { 3439 b->binding_depth = binding_depth; 3440 indent (binding_depth); 3441 cp_binding_level_debug (b, LOCATION_LINE (input_location), "resume"); 3442 binding_depth++; 3443 } 3444 } 3445 3446 /* Return the innermost binding level that is not for a class scope. */ 3447 3448 static cp_binding_level * 3449 innermost_nonclass_level (void) 3450 { 3451 cp_binding_level *b; 3452 3453 b = current_binding_level; 3454 while (b->kind == sk_class) 3455 b = b->level_chain; 3456 3457 return b; 3458 } 3459 3460 /* We're defining an object of type TYPE. If it needs a cleanup, but 3461 we're not allowed to add any more objects with cleanups to the current 3462 scope, create a new binding level. */ 3463 3464 void 3465 maybe_push_cleanup_level (tree type) 3466 { 3467 if (type != error_mark_node 3468 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type) 3469 && current_binding_level->more_cleanups_ok == 0) 3470 { 3471 begin_scope (sk_cleanup, NULL); 3472 current_binding_level->statement_list = push_stmt_list (); 3473 } 3474 } 3475 3476 /* Return true if we are in the global binding level. */ 3477 3478 bool 3479 global_bindings_p (void) 3480 { 3481 return global_scope_p (current_binding_level); 3482 } 3483 3484 /* True if we are currently in a toplevel binding level. This 3485 means either the global binding level or a namespace in a toplevel 3486 binding level. Since there are no non-toplevel namespace levels, 3487 this really means any namespace or template parameter level. We 3488 also include a class whose context is toplevel. */ 3489 3490 bool 3491 toplevel_bindings_p (void) 3492 { 3493 cp_binding_level *b = innermost_nonclass_level (); 3494 3495 return b->kind == sk_namespace || b->kind == sk_template_parms; 3496 } 3497 3498 /* True if this is a namespace scope, or if we are defining a class 3499 which is itself at namespace scope, or whose enclosing class is 3500 such a class, etc. */ 3501 3502 bool 3503 namespace_bindings_p (void) 3504 { 3505 cp_binding_level *b = innermost_nonclass_level (); 3506 3507 return b->kind == sk_namespace; 3508 } 3509 3510 /* True if the innermost non-class scope is a block scope. */ 3511 3512 bool 3513 local_bindings_p (void) 3514 { 3515 cp_binding_level *b = innermost_nonclass_level (); 3516 return b->kind < sk_function_parms || b->kind == sk_omp; 3517 } 3518 3519 /* True if the current level needs to have a BLOCK made. */ 3520 3521 bool 3522 kept_level_p (void) 3523 { 3524 return (current_binding_level->blocks != NULL_TREE 3525 || current_binding_level->keep 3526 || current_binding_level->kind == sk_cleanup 3527 || current_binding_level->names != NULL_TREE 3528 || current_binding_level->using_directives); 3529 } 3530 3531 /* Returns the kind of the innermost scope. */ 3532 3533 scope_kind 3534 innermost_scope_kind (void) 3535 { 3536 return current_binding_level->kind; 3537 } 3538 3539 /* Returns true if this scope was created to store template parameters. */ 3540 3541 bool 3542 template_parm_scope_p (void) 3543 { 3544 return innermost_scope_kind () == sk_template_parms; 3545 } 3546 3547 /* If KEEP is true, make a BLOCK node for the next binding level, 3548 unconditionally. Otherwise, use the normal logic to decide whether 3549 or not to create a BLOCK. */ 3550 3551 void 3552 keep_next_level (bool keep) 3553 { 3554 keep_next_level_flag = keep; 3555 } 3556 3557 /* Return the list of declarations of the current local scope. */ 3558 3559 tree 3560 get_local_decls (void) 3561 { 3562 gcc_assert (current_binding_level->kind != sk_namespace 3563 && current_binding_level->kind != sk_class); 3564 return current_binding_level->names; 3565 } 3566 3567 /* Return how many function prototypes we are currently nested inside. */ 3568 3569 int 3570 function_parm_depth (void) 3571 { 3572 int level = 0; 3573 cp_binding_level *b; 3574 3575 for (b = current_binding_level; 3576 b->kind == sk_function_parms; 3577 b = b->level_chain) 3578 ++level; 3579 3580 return level; 3581 } 3582 3583 /* For debugging. */ 3584 static int no_print_functions = 0; 3585 static int no_print_builtins = 0; 3586 3587 static void 3588 print_binding_level (cp_binding_level* lvl) 3589 { 3590 tree t; 3591 int i = 0, len; 3592 if (lvl->this_entity) 3593 print_node_brief (stderr, "entity=", lvl->this_entity, 1); 3594 fprintf (stderr, " blocks=%p", (void *) lvl->blocks); 3595 if (lvl->more_cleanups_ok) 3596 fprintf (stderr, " more-cleanups-ok"); 3597 if (lvl->have_cleanups) 3598 fprintf (stderr, " have-cleanups"); 3599 fprintf (stderr, "\n"); 3600 if (lvl->names) 3601 { 3602 fprintf (stderr, " names:\t"); 3603 /* We can probably fit 3 names to a line? */ 3604 for (t = lvl->names; t; t = TREE_CHAIN (t)) 3605 { 3606 if (no_print_functions && (TREE_CODE (t) == FUNCTION_DECL)) 3607 continue; 3608 if (no_print_builtins 3609 && (TREE_CODE (t) == TYPE_DECL) 3610 && DECL_IS_BUILTIN (t)) 3611 continue; 3612 3613 /* Function decls tend to have longer names. */ 3614 if (TREE_CODE (t) == FUNCTION_DECL) 3615 len = 3; 3616 else 3617 len = 2; 3618 i += len; 3619 if (i > 6) 3620 { 3621 fprintf (stderr, "\n\t"); 3622 i = len; 3623 } 3624 print_node_brief (stderr, "", t, 0); 3625 if (t == error_mark_node) 3626 break; 3627 } 3628 if (i) 3629 fprintf (stderr, "\n"); 3630 } 3631 if (vec_safe_length (lvl->class_shadowed)) 3632 { 3633 size_t i; 3634 cp_class_binding *b; 3635 fprintf (stderr, " class-shadowed:"); 3636 FOR_EACH_VEC_ELT (*lvl->class_shadowed, i, b) 3637 fprintf (stderr, " %s ", IDENTIFIER_POINTER (b->identifier)); 3638 fprintf (stderr, "\n"); 3639 } 3640 if (lvl->type_shadowed) 3641 { 3642 fprintf (stderr, " type-shadowed:"); 3643 for (t = lvl->type_shadowed; t; t = TREE_CHAIN (t)) 3644 { 3645 fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t))); 3646 } 3647 fprintf (stderr, "\n"); 3648 } 3649 } 3650 3651 DEBUG_FUNCTION void 3652 debug (cp_binding_level &ref) 3653 { 3654 print_binding_level (&ref); 3655 } 3656 3657 DEBUG_FUNCTION void 3658 debug (cp_binding_level *ptr) 3659 { 3660 if (ptr) 3661 debug (*ptr); 3662 else 3663 fprintf (stderr, "<nil>\n"); 3664 } 3665 3666 3667 static void 3668 print_other_binding_stack (cp_binding_level *stack) 3669 { 3670 cp_binding_level *level; 3671 for (level = stack; !global_scope_p (level); level = level->level_chain) 3672 { 3673 fprintf (stderr, "binding level %p\n", (void *) level); 3674 print_binding_level (level); 3675 } 3676 } 3677 3678 void 3679 print_binding_stack (void) 3680 { 3681 cp_binding_level *b; 3682 fprintf (stderr, "current_binding_level=%p\n" 3683 "class_binding_level=%p\n" 3684 "NAMESPACE_LEVEL (global_namespace)=%p\n", 3685 (void *) current_binding_level, (void *) class_binding_level, 3686 (void *) NAMESPACE_LEVEL (global_namespace)); 3687 if (class_binding_level) 3688 { 3689 for (b = class_binding_level; b; b = b->level_chain) 3690 if (b == current_binding_level) 3691 break; 3692 if (b) 3693 b = class_binding_level; 3694 else 3695 b = current_binding_level; 3696 } 3697 else 3698 b = current_binding_level; 3699 print_other_binding_stack (b); 3700 fprintf (stderr, "global:\n"); 3701 print_binding_level (NAMESPACE_LEVEL (global_namespace)); 3702 } 3703 3704 /* Return the type associated with ID. */ 3705 3706 static tree 3707 identifier_type_value_1 (tree id) 3708 { 3709 /* There is no type with that name, anywhere. */ 3710 if (REAL_IDENTIFIER_TYPE_VALUE (id) == NULL_TREE) 3711 return NULL_TREE; 3712 /* This is not the type marker, but the real thing. */ 3713 if (REAL_IDENTIFIER_TYPE_VALUE (id) != global_type_node) 3714 return REAL_IDENTIFIER_TYPE_VALUE (id); 3715 /* Have to search for it. It must be on the global level, now. 3716 Ask lookup_name not to return non-types. */ 3717 id = lookup_name_real (id, 2, 1, /*block_p=*/true, 0, 0); 3718 if (id) 3719 return TREE_TYPE (id); 3720 return NULL_TREE; 3721 } 3722 3723 /* Wrapper for identifier_type_value_1. */ 3724 3725 tree 3726 identifier_type_value (tree id) 3727 { 3728 tree ret; 3729 timevar_start (TV_NAME_LOOKUP); 3730 ret = identifier_type_value_1 (id); 3731 timevar_stop (TV_NAME_LOOKUP); 3732 return ret; 3733 } 3734 3735 /* Push a definition of struct, union or enum tag named ID. into 3736 binding_level B. DECL is a TYPE_DECL for the type. We assume that 3737 the tag ID is not already defined. */ 3738 3739 static void 3740 set_identifier_type_value_with_scope (tree id, tree decl, cp_binding_level *b) 3741 { 3742 tree type; 3743 3744 if (b->kind != sk_namespace) 3745 { 3746 /* Shadow the marker, not the real thing, so that the marker 3747 gets restored later. */ 3748 tree old_type_value = REAL_IDENTIFIER_TYPE_VALUE (id); 3749 b->type_shadowed 3750 = tree_cons (id, old_type_value, b->type_shadowed); 3751 type = decl ? TREE_TYPE (decl) : NULL_TREE; 3752 TREE_TYPE (b->type_shadowed) = type; 3753 } 3754 else 3755 { 3756 tree *slot = find_namespace_slot (current_namespace, id, true); 3757 gcc_assert (decl); 3758 update_binding (b, NULL, slot, MAYBE_STAT_DECL (*slot), decl, false); 3759 3760 /* Store marker instead of real type. */ 3761 type = global_type_node; 3762 } 3763 SET_IDENTIFIER_TYPE_VALUE (id, type); 3764 } 3765 3766 /* As set_identifier_type_value_with_scope, but using 3767 current_binding_level. */ 3768 3769 void 3770 set_identifier_type_value (tree id, tree decl) 3771 { 3772 set_identifier_type_value_with_scope (id, decl, current_binding_level); 3773 } 3774 3775 /* Return the name for the constructor (or destructor) for the 3776 specified class. */ 3777 3778 tree 3779 constructor_name (tree type) 3780 { 3781 tree decl = TYPE_NAME (TYPE_MAIN_VARIANT (type)); 3782 3783 return decl ? DECL_NAME (decl) : NULL_TREE; 3784 } 3785 3786 /* Returns TRUE if NAME is the name for the constructor for TYPE, 3787 which must be a class type. */ 3788 3789 bool 3790 constructor_name_p (tree name, tree type) 3791 { 3792 gcc_assert (MAYBE_CLASS_TYPE_P (type)); 3793 3794 /* These don't have names. */ 3795 if (TREE_CODE (type) == DECLTYPE_TYPE 3796 || TREE_CODE (type) == TYPEOF_TYPE) 3797 return false; 3798 3799 if (name && name == constructor_name (type)) 3800 return true; 3801 3802 return false; 3803 } 3804 3805 /* Counter used to create anonymous type names. */ 3806 3807 static GTY(()) int anon_cnt; 3808 3809 /* Return an IDENTIFIER which can be used as a name for 3810 unnamed structs and unions. */ 3811 3812 tree 3813 make_anon_name (void) 3814 { 3815 char buf[32]; 3816 3817 sprintf (buf, anon_aggrname_format (), anon_cnt++); 3818 return get_identifier (buf); 3819 } 3820 3821 /* This code is practically identical to that for creating 3822 anonymous names, but is just used for lambdas instead. This isn't really 3823 necessary, but it's convenient to avoid treating lambdas like other 3824 unnamed types. */ 3825 3826 static GTY(()) int lambda_cnt = 0; 3827 3828 tree 3829 make_lambda_name (void) 3830 { 3831 char buf[32]; 3832 3833 sprintf (buf, LAMBDANAME_FORMAT, lambda_cnt++); 3834 return get_identifier (buf); 3835 } 3836 3837 /* Insert another USING_DECL into the current binding level, returning 3838 this declaration. If this is a redeclaration, do nothing, and 3839 return NULL_TREE if this not in namespace scope (in namespace 3840 scope, a using decl might extend any previous bindings). */ 3841 3842 static tree 3843 push_using_decl_1 (tree scope, tree name) 3844 { 3845 tree decl; 3846 3847 gcc_assert (TREE_CODE (scope) == NAMESPACE_DECL); 3848 gcc_assert (identifier_p (name)); 3849 for (decl = current_binding_level->usings; decl; decl = DECL_CHAIN (decl)) 3850 if (USING_DECL_SCOPE (decl) == scope && DECL_NAME (decl) == name) 3851 break; 3852 if (decl) 3853 return namespace_bindings_p () ? decl : NULL_TREE; 3854 decl = build_lang_decl (USING_DECL, name, NULL_TREE); 3855 USING_DECL_SCOPE (decl) = scope; 3856 DECL_CHAIN (decl) = current_binding_level->usings; 3857 current_binding_level->usings = decl; 3858 return decl; 3859 } 3860 3861 /* Wrapper for push_using_decl_1. */ 3862 3863 static tree 3864 push_using_decl (tree scope, tree name) 3865 { 3866 tree ret; 3867 timevar_start (TV_NAME_LOOKUP); 3868 ret = push_using_decl_1 (scope, name); 3869 timevar_stop (TV_NAME_LOOKUP); 3870 return ret; 3871 } 3872 3873 /* Same as pushdecl, but define X in binding-level LEVEL. We rely on the 3874 caller to set DECL_CONTEXT properly. 3875 3876 Note that this must only be used when X will be the new innermost 3877 binding for its name, as we tack it onto the front of IDENTIFIER_BINDING 3878 without checking to see if the current IDENTIFIER_BINDING comes from a 3879 closer binding level than LEVEL. */ 3880 3881 static tree 3882 do_pushdecl_with_scope (tree x, cp_binding_level *level, bool is_friend) 3883 { 3884 cp_binding_level *b; 3885 3886 if (level->kind == sk_class) 3887 { 3888 b = class_binding_level; 3889 class_binding_level = level; 3890 pushdecl_class_level (x); 3891 class_binding_level = b; 3892 } 3893 else 3894 { 3895 tree function_decl = current_function_decl; 3896 if (level->kind == sk_namespace) 3897 current_function_decl = NULL_TREE; 3898 b = current_binding_level; 3899 current_binding_level = level; 3900 x = pushdecl (x, is_friend); 3901 current_binding_level = b; 3902 current_function_decl = function_decl; 3903 } 3904 return x; 3905 } 3906 3907 /* Inject X into the local scope just before the function parms. */ 3908 3909 tree 3910 pushdecl_outermost_localscope (tree x) 3911 { 3912 cp_binding_level *b = NULL; 3913 bool subtime = timevar_cond_start (TV_NAME_LOOKUP); 3914 3915 /* Find the scope just inside the function parms. */ 3916 for (cp_binding_level *n = current_binding_level; 3917 n->kind != sk_function_parms; n = b->level_chain) 3918 b = n; 3919 3920 tree ret = b ? do_pushdecl_with_scope (x, b, false) : error_mark_node; 3921 timevar_cond_stop (TV_NAME_LOOKUP, subtime); 3922 3923 return ret; 3924 } 3925 3926 /* Check a non-member using-declaration. Return the name and scope 3927 being used, and the USING_DECL, or NULL_TREE on failure. */ 3928 3929 static tree 3930 validate_nonmember_using_decl (tree decl, tree scope, tree name) 3931 { 3932 /* [namespace.udecl] 3933 A using-declaration for a class member shall be a 3934 member-declaration. */ 3935 if (TYPE_P (scope)) 3936 { 3937 error ("%qT is not a namespace or unscoped enum", scope); 3938 return NULL_TREE; 3939 } 3940 else if (scope == error_mark_node) 3941 return NULL_TREE; 3942 3943 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR) 3944 { 3945 /* 7.3.3/5 3946 A using-declaration shall not name a template-id. */ 3947 error ("a using-declaration cannot specify a template-id. " 3948 "Try %<using %D%>", name); 3949 return NULL_TREE; 3950 } 3951 3952 if (TREE_CODE (decl) == NAMESPACE_DECL) 3953 { 3954 error ("namespace %qD not allowed in using-declaration", decl); 3955 return NULL_TREE; 3956 } 3957 3958 if (TREE_CODE (decl) == SCOPE_REF) 3959 { 3960 /* It's a nested name with template parameter dependent scope. 3961 This can only be using-declaration for class member. */ 3962 error ("%qT is not a namespace", TREE_OPERAND (decl, 0)); 3963 return NULL_TREE; 3964 } 3965 3966 decl = OVL_FIRST (decl); 3967 3968 /* Make a USING_DECL. */ 3969 tree using_decl = push_using_decl (scope, name); 3970 3971 if (using_decl == NULL_TREE 3972 && at_function_scope_p () 3973 && VAR_P (decl)) 3974 /* C++11 7.3.3/10. */ 3975 error ("%qD is already declared in this scope", name); 3976 3977 return using_decl; 3978 } 3979 3980 /* Process a local-scope or namespace-scope using declaration. SCOPE 3981 is the nominated scope to search for NAME. VALUE_P and TYPE_P 3982 point to the binding for NAME in the current scope and are 3983 updated. */ 3984 3985 static void 3986 do_nonmember_using_decl (tree scope, tree name, tree *value_p, tree *type_p) 3987 { 3988 name_lookup lookup (name, 0); 3989 3990 if (!qualified_namespace_lookup (scope, &lookup)) 3991 { 3992 error ("%qD not declared", name); 3993 return; 3994 } 3995 else if (TREE_CODE (lookup.value) == TREE_LIST) 3996 { 3997 error ("reference to %qD is ambiguous", name); 3998 print_candidates (lookup.value); 3999 lookup.value = NULL_TREE; 4000 } 4001 4002 if (lookup.type && TREE_CODE (lookup.type) == TREE_LIST) 4003 { 4004 error ("reference to %qD is ambiguous", name); 4005 print_candidates (lookup.type); 4006 lookup.type = NULL_TREE; 4007 } 4008 4009 tree value = *value_p; 4010 tree type = *type_p; 4011 4012 /* Shift the old and new bindings around so we're comparing class and 4013 enumeration names to each other. */ 4014 if (value && DECL_IMPLICIT_TYPEDEF_P (value)) 4015 { 4016 type = value; 4017 value = NULL_TREE; 4018 } 4019 4020 if (lookup.value && DECL_IMPLICIT_TYPEDEF_P (lookup.value)) 4021 { 4022 lookup.type = lookup.value; 4023 lookup.value = NULL_TREE; 4024 } 4025 4026 if (lookup.value && lookup.value != value) 4027 { 4028 /* Check for using functions. */ 4029 if (OVL_P (lookup.value) && (!value || OVL_P (value))) 4030 { 4031 for (lkp_iterator usings (lookup.value); usings; ++usings) 4032 { 4033 tree new_fn = *usings; 4034 4035 /* [namespace.udecl] 4036 4037 If a function declaration in namespace scope or block 4038 scope has the same name and the same parameter types as a 4039 function introduced by a using declaration the program is 4040 ill-formed. */ 4041 bool found = false; 4042 for (ovl_iterator old (value); !found && old; ++old) 4043 { 4044 tree old_fn = *old; 4045 4046 if (new_fn == old_fn) 4047 /* The function already exists in the current 4048 namespace. */ 4049 found = true; 4050 else if (old.using_p ()) 4051 continue; /* This is a using decl. */ 4052 else if (old.hidden_p () && !DECL_HIDDEN_FRIEND_P (old_fn)) 4053 continue; /* This is an anticipated builtin. */ 4054 else if (!matching_fn_p (new_fn, old_fn)) 4055 continue; /* Parameters do not match. */ 4056 else if (decls_match (new_fn, old_fn)) 4057 found = true; 4058 else 4059 { 4060 diagnose_name_conflict (new_fn, old_fn); 4061 found = true; 4062 } 4063 } 4064 4065 if (!found) 4066 /* Unlike the overload case we don't drop anticipated 4067 builtins here. They don't cause a problem, and 4068 we'd like to match them with a future 4069 declaration. */ 4070 value = ovl_insert (new_fn, value, true); 4071 } 4072 } 4073 else if (value 4074 /* Ignore anticipated builtins. */ 4075 && !anticipated_builtin_p (value) 4076 && !decls_match (lookup.value, value)) 4077 diagnose_name_conflict (lookup.value, value); 4078 else 4079 value = lookup.value; 4080 } 4081 4082 if (lookup.type && lookup.type != type) 4083 { 4084 if (type && !decls_match (lookup.type, type)) 4085 diagnose_name_conflict (lookup.type, type); 4086 else 4087 type = lookup.type; 4088 } 4089 4090 /* If bind->value is empty, shift any class or enumeration name back. */ 4091 if (!value) 4092 { 4093 value = type; 4094 type = NULL_TREE; 4095 } 4096 4097 *value_p = value; 4098 *type_p = type; 4099 } 4100 4101 /* Returns true if ANCESTOR encloses DESCENDANT, including matching. 4102 Both are namespaces. */ 4103 4104 bool 4105 is_nested_namespace (tree ancestor, tree descendant, bool inline_only) 4106 { 4107 int depth = SCOPE_DEPTH (ancestor); 4108 4109 if (!depth && !inline_only) 4110 /* The global namespace encloses everything. */ 4111 return true; 4112 4113 while (SCOPE_DEPTH (descendant) > depth 4114 && (!inline_only || DECL_NAMESPACE_INLINE_P (descendant))) 4115 descendant = CP_DECL_CONTEXT (descendant); 4116 4117 return ancestor == descendant; 4118 } 4119 4120 /* Returns true if ROOT (a non-alias namespace, class, or function) 4121 encloses CHILD. CHILD may be either a class type or a namespace 4122 (maybe alias). */ 4123 4124 bool 4125 is_ancestor (tree root, tree child) 4126 { 4127 gcc_checking_assert ((TREE_CODE (root) == NAMESPACE_DECL 4128 && !DECL_NAMESPACE_ALIAS (root)) 4129 || TREE_CODE (root) == FUNCTION_DECL 4130 || CLASS_TYPE_P (root)); 4131 gcc_checking_assert (TREE_CODE (child) == NAMESPACE_DECL 4132 || CLASS_TYPE_P (child)); 4133 4134 /* The global namespace encloses everything. Early-out for the 4135 common case. */ 4136 if (root == global_namespace) 4137 return true; 4138 4139 /* Search CHILD until we reach namespace scope. */ 4140 while (TREE_CODE (child) != NAMESPACE_DECL) 4141 { 4142 /* If we've reached the ROOT, it encloses CHILD. */ 4143 if (root == child) 4144 return true; 4145 4146 /* Go out one level. */ 4147 if (TYPE_P (child)) 4148 child = TYPE_NAME (child); 4149 child = CP_DECL_CONTEXT (child); 4150 } 4151 4152 if (TREE_CODE (root) != NAMESPACE_DECL) 4153 /* Failed to meet the non-namespace we were looking for. */ 4154 return false; 4155 4156 if (tree alias = DECL_NAMESPACE_ALIAS (child)) 4157 child = alias; 4158 4159 return is_nested_namespace (root, child); 4160 } 4161 4162 /* Enter the class or namespace scope indicated by T suitable for name 4163 lookup. T can be arbitrary scope, not necessary nested inside the 4164 current scope. Returns a non-null scope to pop iff pop_scope 4165 should be called later to exit this scope. */ 4166 4167 tree 4168 push_scope (tree t) 4169 { 4170 if (TREE_CODE (t) == NAMESPACE_DECL) 4171 push_decl_namespace (t); 4172 else if (CLASS_TYPE_P (t)) 4173 { 4174 if (!at_class_scope_p () 4175 || !same_type_p (current_class_type, t)) 4176 push_nested_class (t); 4177 else 4178 /* T is the same as the current scope. There is therefore no 4179 need to re-enter the scope. Since we are not actually 4180 pushing a new scope, our caller should not call 4181 pop_scope. */ 4182 t = NULL_TREE; 4183 } 4184 4185 return t; 4186 } 4187 4188 /* Leave scope pushed by push_scope. */ 4189 4190 void 4191 pop_scope (tree t) 4192 { 4193 if (t == NULL_TREE) 4194 return; 4195 if (TREE_CODE (t) == NAMESPACE_DECL) 4196 pop_decl_namespace (); 4197 else if CLASS_TYPE_P (t) 4198 pop_nested_class (); 4199 } 4200 4201 /* Subroutine of push_inner_scope. */ 4202 4203 static void 4204 push_inner_scope_r (tree outer, tree inner) 4205 { 4206 tree prev; 4207 4208 if (outer == inner 4209 || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner))) 4210 return; 4211 4212 prev = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner)); 4213 if (outer != prev) 4214 push_inner_scope_r (outer, prev); 4215 if (TREE_CODE (inner) == NAMESPACE_DECL) 4216 { 4217 cp_binding_level *save_template_parm = 0; 4218 /* Temporary take out template parameter scopes. They are saved 4219 in reversed order in save_template_parm. */ 4220 while (current_binding_level->kind == sk_template_parms) 4221 { 4222 cp_binding_level *b = current_binding_level; 4223 current_binding_level = b->level_chain; 4224 b->level_chain = save_template_parm; 4225 save_template_parm = b; 4226 } 4227 4228 resume_scope (NAMESPACE_LEVEL (inner)); 4229 current_namespace = inner; 4230 4231 /* Restore template parameter scopes. */ 4232 while (save_template_parm) 4233 { 4234 cp_binding_level *b = save_template_parm; 4235 save_template_parm = b->level_chain; 4236 b->level_chain = current_binding_level; 4237 current_binding_level = b; 4238 } 4239 } 4240 else 4241 pushclass (inner); 4242 } 4243 4244 /* Enter the scope INNER from current scope. INNER must be a scope 4245 nested inside current scope. This works with both name lookup and 4246 pushing name into scope. In case a template parameter scope is present, 4247 namespace is pushed under the template parameter scope according to 4248 name lookup rule in 14.6.1/6. 4249 4250 Return the former current scope suitable for pop_inner_scope. */ 4251 4252 tree 4253 push_inner_scope (tree inner) 4254 { 4255 tree outer = current_scope (); 4256 if (!outer) 4257 outer = current_namespace; 4258 4259 push_inner_scope_r (outer, inner); 4260 return outer; 4261 } 4262 4263 /* Exit the current scope INNER back to scope OUTER. */ 4264 4265 void 4266 pop_inner_scope (tree outer, tree inner) 4267 { 4268 if (outer == inner 4269 || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner))) 4270 return; 4271 4272 while (outer != inner) 4273 { 4274 if (TREE_CODE (inner) == NAMESPACE_DECL) 4275 { 4276 cp_binding_level *save_template_parm = 0; 4277 /* Temporary take out template parameter scopes. They are saved 4278 in reversed order in save_template_parm. */ 4279 while (current_binding_level->kind == sk_template_parms) 4280 { 4281 cp_binding_level *b = current_binding_level; 4282 current_binding_level = b->level_chain; 4283 b->level_chain = save_template_parm; 4284 save_template_parm = b; 4285 } 4286 4287 pop_namespace (); 4288 4289 /* Restore template parameter scopes. */ 4290 while (save_template_parm) 4291 { 4292 cp_binding_level *b = save_template_parm; 4293 save_template_parm = b->level_chain; 4294 b->level_chain = current_binding_level; 4295 current_binding_level = b; 4296 } 4297 } 4298 else 4299 popclass (); 4300 4301 inner = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner)); 4302 } 4303 } 4304 4305 /* Do a pushlevel for class declarations. */ 4306 4307 void 4308 pushlevel_class (void) 4309 { 4310 class_binding_level = begin_scope (sk_class, current_class_type); 4311 } 4312 4313 /* ...and a poplevel for class declarations. */ 4314 4315 void 4316 poplevel_class (void) 4317 { 4318 cp_binding_level *level = class_binding_level; 4319 cp_class_binding *cb; 4320 size_t i; 4321 tree shadowed; 4322 4323 bool subtime = timevar_cond_start (TV_NAME_LOOKUP); 4324 gcc_assert (level != 0); 4325 4326 /* If we're leaving a toplevel class, cache its binding level. */ 4327 if (current_class_depth == 1) 4328 previous_class_level = level; 4329 for (shadowed = level->type_shadowed; 4330 shadowed; 4331 shadowed = TREE_CHAIN (shadowed)) 4332 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed), TREE_VALUE (shadowed)); 4333 4334 /* Remove the bindings for all of the class-level declarations. */ 4335 if (level->class_shadowed) 4336 { 4337 FOR_EACH_VEC_ELT (*level->class_shadowed, i, cb) 4338 { 4339 IDENTIFIER_BINDING (cb->identifier) = cb->base->previous; 4340 cxx_binding_free (cb->base); 4341 } 4342 ggc_free (level->class_shadowed); 4343 level->class_shadowed = NULL; 4344 } 4345 4346 /* Now, pop out of the binding level which we created up in the 4347 `pushlevel_class' routine. */ 4348 gcc_assert (current_binding_level == level); 4349 leave_scope (); 4350 timevar_cond_stop (TV_NAME_LOOKUP, subtime); 4351 } 4352 4353 /* Set INHERITED_VALUE_BINDING_P on BINDING to true or false, as 4354 appropriate. DECL is the value to which a name has just been 4355 bound. CLASS_TYPE is the class in which the lookup occurred. */ 4356 4357 static void 4358 set_inherited_value_binding_p (cxx_binding *binding, tree decl, 4359 tree class_type) 4360 { 4361 if (binding->value == decl && TREE_CODE (decl) != TREE_LIST) 4362 { 4363 tree context; 4364 4365 if (TREE_CODE (decl) == OVERLOAD) 4366 context = ovl_scope (decl); 4367 else 4368 { 4369 gcc_assert (DECL_P (decl)); 4370 context = context_for_name_lookup (decl); 4371 } 4372 4373 if (is_properly_derived_from (class_type, context)) 4374 INHERITED_VALUE_BINDING_P (binding) = 1; 4375 else 4376 INHERITED_VALUE_BINDING_P (binding) = 0; 4377 } 4378 else if (binding->value == decl) 4379 /* We only encounter a TREE_LIST when there is an ambiguity in the 4380 base classes. Such an ambiguity can be overridden by a 4381 definition in this class. */ 4382 INHERITED_VALUE_BINDING_P (binding) = 1; 4383 else 4384 INHERITED_VALUE_BINDING_P (binding) = 0; 4385 } 4386 4387 /* Make the declaration of X appear in CLASS scope. */ 4388 4389 bool 4390 pushdecl_class_level (tree x) 4391 { 4392 bool is_valid = true; 4393 bool subtime; 4394 4395 /* Do nothing if we're adding to an outer lambda closure type, 4396 outer_binding will add it later if it's needed. */ 4397 if (current_class_type != class_binding_level->this_entity) 4398 return true; 4399 4400 subtime = timevar_cond_start (TV_NAME_LOOKUP); 4401 /* Get the name of X. */ 4402 tree name = OVL_NAME (x); 4403 4404 if (name) 4405 { 4406 is_valid = push_class_level_binding (name, x); 4407 if (TREE_CODE (x) == TYPE_DECL) 4408 set_identifier_type_value (name, x); 4409 } 4410 else if (ANON_AGGR_TYPE_P (TREE_TYPE (x))) 4411 { 4412 /* If X is an anonymous aggregate, all of its members are 4413 treated as if they were members of the class containing the 4414 aggregate, for naming purposes. */ 4415 location_t save_location = input_location; 4416 tree anon = TREE_TYPE (x); 4417 if (vec<tree, va_gc> *member_vec = CLASSTYPE_MEMBER_VEC (anon)) 4418 for (unsigned ix = member_vec->length (); ix--;) 4419 { 4420 tree binding = (*member_vec)[ix]; 4421 if (STAT_HACK_P (binding)) 4422 { 4423 if (!pushdecl_class_level (STAT_TYPE (binding))) 4424 is_valid = false; 4425 binding = STAT_DECL (binding); 4426 } 4427 if (!pushdecl_class_level (binding)) 4428 is_valid = false; 4429 } 4430 else 4431 for (tree f = TYPE_FIELDS (anon); f; f = DECL_CHAIN (f)) 4432 if (TREE_CODE (f) == FIELD_DECL) 4433 { 4434 input_location = DECL_SOURCE_LOCATION (f); 4435 if (!pushdecl_class_level (f)) 4436 is_valid = false; 4437 } 4438 input_location = save_location; 4439 } 4440 timevar_cond_stop (TV_NAME_LOOKUP, subtime); 4441 return is_valid; 4442 } 4443 4444 /* Return the BINDING (if any) for NAME in SCOPE, which is a class 4445 scope. If the value returned is non-NULL, and the PREVIOUS field 4446 is not set, callers must set the PREVIOUS field explicitly. */ 4447 4448 static cxx_binding * 4449 get_class_binding (tree name, cp_binding_level *scope) 4450 { 4451 tree class_type; 4452 tree type_binding; 4453 tree value_binding; 4454 cxx_binding *binding; 4455 4456 class_type = scope->this_entity; 4457 4458 /* Get the type binding. */ 4459 type_binding = lookup_member (class_type, name, 4460 /*protect=*/2, /*want_type=*/true, 4461 tf_warning_or_error); 4462 /* Get the value binding. */ 4463 value_binding = lookup_member (class_type, name, 4464 /*protect=*/2, /*want_type=*/false, 4465 tf_warning_or_error); 4466 4467 if (value_binding 4468 && (TREE_CODE (value_binding) == TYPE_DECL 4469 || DECL_CLASS_TEMPLATE_P (value_binding) 4470 || (TREE_CODE (value_binding) == TREE_LIST 4471 && TREE_TYPE (value_binding) == error_mark_node 4472 && (TREE_CODE (TREE_VALUE (value_binding)) 4473 == TYPE_DECL)))) 4474 /* We found a type binding, even when looking for a non-type 4475 binding. This means that we already processed this binding 4476 above. */ 4477 ; 4478 else if (value_binding) 4479 { 4480 if (TREE_CODE (value_binding) == TREE_LIST 4481 && TREE_TYPE (value_binding) == error_mark_node) 4482 /* NAME is ambiguous. */ 4483 ; 4484 else if (BASELINK_P (value_binding)) 4485 /* NAME is some overloaded functions. */ 4486 value_binding = BASELINK_FUNCTIONS (value_binding); 4487 } 4488 4489 /* If we found either a type binding or a value binding, create a 4490 new binding object. */ 4491 if (type_binding || value_binding) 4492 { 4493 binding = new_class_binding (name, 4494 value_binding, 4495 type_binding, 4496 scope); 4497 /* This is a class-scope binding, not a block-scope binding. */ 4498 LOCAL_BINDING_P (binding) = 0; 4499 set_inherited_value_binding_p (binding, value_binding, class_type); 4500 } 4501 else 4502 binding = NULL; 4503 4504 return binding; 4505 } 4506 4507 /* Make the declaration(s) of X appear in CLASS scope under the name 4508 NAME. Returns true if the binding is valid. */ 4509 4510 static bool 4511 push_class_level_binding_1 (tree name, tree x) 4512 { 4513 cxx_binding *binding; 4514 tree decl = x; 4515 bool ok; 4516 4517 /* The class_binding_level will be NULL if x is a template 4518 parameter name in a member template. */ 4519 if (!class_binding_level) 4520 return true; 4521 4522 if (name == error_mark_node) 4523 return false; 4524 4525 /* Can happen for an erroneous declaration (c++/60384). */ 4526 if (!identifier_p (name)) 4527 { 4528 gcc_assert (errorcount || sorrycount); 4529 return false; 4530 } 4531 4532 /* Check for invalid member names. But don't worry about a default 4533 argument-scope lambda being pushed after the class is complete. */ 4534 gcc_assert (TYPE_BEING_DEFINED (current_class_type) 4535 || LAMBDA_TYPE_P (TREE_TYPE (decl))); 4536 /* Check that we're pushing into the right binding level. */ 4537 gcc_assert (current_class_type == class_binding_level->this_entity); 4538 4539 /* We could have been passed a tree list if this is an ambiguous 4540 declaration. If so, pull the declaration out because 4541 check_template_shadow will not handle a TREE_LIST. */ 4542 if (TREE_CODE (decl) == TREE_LIST 4543 && TREE_TYPE (decl) == error_mark_node) 4544 decl = TREE_VALUE (decl); 4545 4546 if (!check_template_shadow (decl)) 4547 return false; 4548 4549 /* [class.mem] 4550 4551 If T is the name of a class, then each of the following shall 4552 have a name different from T: 4553 4554 -- every static data member of class T; 4555 4556 -- every member of class T that is itself a type; 4557 4558 -- every enumerator of every member of class T that is an 4559 enumerated type; 4560 4561 -- every member of every anonymous union that is a member of 4562 class T. 4563 4564 (Non-static data members were also forbidden to have the same 4565 name as T until TC1.) */ 4566 if ((VAR_P (x) 4567 || TREE_CODE (x) == CONST_DECL 4568 || (TREE_CODE (x) == TYPE_DECL 4569 && !DECL_SELF_REFERENCE_P (x)) 4570 /* A data member of an anonymous union. */ 4571 || (TREE_CODE (x) == FIELD_DECL 4572 && DECL_CONTEXT (x) != current_class_type)) 4573 && DECL_NAME (x) == DECL_NAME (TYPE_NAME (current_class_type))) 4574 { 4575 tree scope = context_for_name_lookup (x); 4576 if (TYPE_P (scope) && same_type_p (scope, current_class_type)) 4577 { 4578 error ("%qD has the same name as the class in which it is " 4579 "declared", 4580 x); 4581 return false; 4582 } 4583 } 4584 4585 /* Get the current binding for NAME in this class, if any. */ 4586 binding = IDENTIFIER_BINDING (name); 4587 if (!binding || binding->scope != class_binding_level) 4588 { 4589 binding = get_class_binding (name, class_binding_level); 4590 /* If a new binding was created, put it at the front of the 4591 IDENTIFIER_BINDING list. */ 4592 if (binding) 4593 { 4594 binding->previous = IDENTIFIER_BINDING (name); 4595 IDENTIFIER_BINDING (name) = binding; 4596 } 4597 } 4598 4599 /* If there is already a binding, then we may need to update the 4600 current value. */ 4601 if (binding && binding->value) 4602 { 4603 tree bval = binding->value; 4604 tree old_decl = NULL_TREE; 4605 tree target_decl = strip_using_decl (decl); 4606 tree target_bval = strip_using_decl (bval); 4607 4608 if (INHERITED_VALUE_BINDING_P (binding)) 4609 { 4610 /* If the old binding was from a base class, and was for a 4611 tag name, slide it over to make room for the new binding. 4612 The old binding is still visible if explicitly qualified 4613 with a class-key. */ 4614 if (TREE_CODE (target_bval) == TYPE_DECL 4615 && DECL_ARTIFICIAL (target_bval) 4616 && !(TREE_CODE (target_decl) == TYPE_DECL 4617 && DECL_ARTIFICIAL (target_decl))) 4618 { 4619 old_decl = binding->type; 4620 binding->type = bval; 4621 binding->value = NULL_TREE; 4622 INHERITED_VALUE_BINDING_P (binding) = 0; 4623 } 4624 else 4625 { 4626 old_decl = bval; 4627 /* Any inherited type declaration is hidden by the type 4628 declaration in the derived class. */ 4629 if (TREE_CODE (target_decl) == TYPE_DECL 4630 && DECL_ARTIFICIAL (target_decl)) 4631 binding->type = NULL_TREE; 4632 } 4633 } 4634 else if (TREE_CODE (target_decl) == OVERLOAD 4635 && OVL_P (target_bval)) 4636 old_decl = bval; 4637 else if (TREE_CODE (decl) == USING_DECL 4638 && TREE_CODE (bval) == USING_DECL 4639 && same_type_p (USING_DECL_SCOPE (decl), 4640 USING_DECL_SCOPE (bval))) 4641 /* This is a using redeclaration that will be diagnosed later 4642 in supplement_binding */ 4643 ; 4644 else if (TREE_CODE (decl) == USING_DECL 4645 && TREE_CODE (bval) == USING_DECL 4646 && DECL_DEPENDENT_P (decl) 4647 && DECL_DEPENDENT_P (bval)) 4648 return true; 4649 else if (TREE_CODE (decl) == USING_DECL 4650 && OVL_P (target_bval)) 4651 old_decl = bval; 4652 else if (TREE_CODE (bval) == USING_DECL 4653 && OVL_P (target_decl)) 4654 return true; 4655 4656 if (old_decl && binding->scope == class_binding_level) 4657 { 4658 binding->value = x; 4659 /* It is always safe to clear INHERITED_VALUE_BINDING_P 4660 here. This function is only used to register bindings 4661 from with the class definition itself. */ 4662 INHERITED_VALUE_BINDING_P (binding) = 0; 4663 return true; 4664 } 4665 } 4666 4667 /* Note that we declared this value so that we can issue an error if 4668 this is an invalid redeclaration of a name already used for some 4669 other purpose. */ 4670 note_name_declared_in_class (name, decl); 4671 4672 /* If we didn't replace an existing binding, put the binding on the 4673 stack of bindings for the identifier, and update the shadowed 4674 list. */ 4675 if (binding && binding->scope == class_binding_level) 4676 /* Supplement the existing binding. */ 4677 ok = supplement_binding (binding, decl); 4678 else 4679 { 4680 /* Create a new binding. */ 4681 push_binding (name, decl, class_binding_level); 4682 ok = true; 4683 } 4684 4685 return ok; 4686 } 4687 4688 /* Wrapper for push_class_level_binding_1. */ 4689 4690 bool 4691 push_class_level_binding (tree name, tree x) 4692 { 4693 bool ret; 4694 bool subtime = timevar_cond_start (TV_NAME_LOOKUP); 4695 ret = push_class_level_binding_1 (name, x); 4696 timevar_cond_stop (TV_NAME_LOOKUP, subtime); 4697 return ret; 4698 } 4699 4700 /* Process "using SCOPE::NAME" in a class scope. Return the 4701 USING_DECL created. */ 4702 4703 tree 4704 do_class_using_decl (tree scope, tree name) 4705 { 4706 if (name == error_mark_node) 4707 return NULL_TREE; 4708 4709 if (!scope || !TYPE_P (scope)) 4710 { 4711 error ("using-declaration for non-member at class scope"); 4712 return NULL_TREE; 4713 } 4714 4715 /* Make sure the name is not invalid */ 4716 if (TREE_CODE (name) == BIT_NOT_EXPR) 4717 { 4718 error ("%<%T::%D%> names destructor", scope, name); 4719 return NULL_TREE; 4720 } 4721 4722 /* Using T::T declares inheriting ctors, even if T is a typedef. */ 4723 if (MAYBE_CLASS_TYPE_P (scope) 4724 && (name == TYPE_IDENTIFIER (scope) 4725 || constructor_name_p (name, scope))) 4726 { 4727 maybe_warn_cpp0x (CPP0X_INHERITING_CTORS); 4728 name = ctor_identifier; 4729 CLASSTYPE_NON_AGGREGATE (current_class_type) = true; 4730 } 4731 4732 /* Cannot introduce a constructor name. */ 4733 if (constructor_name_p (name, current_class_type)) 4734 { 4735 error ("%<%T::%D%> names constructor in %qT", 4736 scope, name, current_class_type); 4737 return NULL_TREE; 4738 } 4739 4740 /* From [namespace.udecl]: 4741 4742 A using-declaration used as a member-declaration shall refer to a 4743 member of a base class of the class being defined. 4744 4745 In general, we cannot check this constraint in a template because 4746 we do not know the entire set of base classes of the current 4747 class type. Morover, if SCOPE is dependent, it might match a 4748 non-dependent base. */ 4749 4750 tree decl = NULL_TREE; 4751 if (!dependent_scope_p (scope)) 4752 { 4753 base_kind b_kind; 4754 tree binfo = lookup_base (current_class_type, scope, ba_any, &b_kind, 4755 tf_warning_or_error); 4756 if (b_kind < bk_proper_base) 4757 { 4758 /* If there are dependent bases, scope might resolve at 4759 instantiation time, even if it isn't exactly one of the 4760 dependent bases. */ 4761 if (b_kind == bk_same_type || !any_dependent_bases_p ()) 4762 { 4763 error_not_base_type (scope, current_class_type); 4764 return NULL_TREE; 4765 } 4766 } 4767 else if (name == ctor_identifier && !binfo_direct_p (binfo)) 4768 { 4769 error ("cannot inherit constructors from indirect base %qT", scope); 4770 return NULL_TREE; 4771 } 4772 else if (!IDENTIFIER_CONV_OP_P (name) 4773 || !dependent_type_p (TREE_TYPE (name))) 4774 { 4775 decl = lookup_member (binfo, name, 0, false, tf_warning_or_error); 4776 if (!decl) 4777 { 4778 error ("no members matching %<%T::%D%> in %q#T", scope, name, 4779 scope); 4780 return NULL_TREE; 4781 } 4782 4783 /* The binfo from which the functions came does not matter. */ 4784 if (BASELINK_P (decl)) 4785 decl = BASELINK_FUNCTIONS (decl); 4786 } 4787 } 4788 4789 tree value = build_lang_decl (USING_DECL, name, NULL_TREE); 4790 USING_DECL_DECLS (value) = decl; 4791 USING_DECL_SCOPE (value) = scope; 4792 DECL_DEPENDENT_P (value) = !decl; 4793 4794 return value; 4795 } 4796 4797 4798 /* Return the binding for NAME in NS. If NS is NULL, look in 4799 global_namespace. */ 4800 4801 tree 4802 get_namespace_binding (tree ns, tree name) 4803 { 4804 bool subtime = timevar_cond_start (TV_NAME_LOOKUP); 4805 if (!ns) 4806 ns = global_namespace; 4807 gcc_checking_assert (!DECL_NAMESPACE_ALIAS (ns)); 4808 tree ret = find_namespace_value (ns, name); 4809 timevar_cond_stop (TV_NAME_LOOKUP, subtime); 4810 return ret; 4811 } 4812 4813 /* Push internal DECL into the global namespace. Does not do the 4814 full overload fn handling and does not add it to the list of things 4815 in the namespace. */ 4816 4817 void 4818 set_global_binding (tree decl) 4819 { 4820 bool subtime = timevar_cond_start (TV_NAME_LOOKUP); 4821 4822 tree *slot = find_namespace_slot (global_namespace, DECL_NAME (decl), true); 4823 4824 if (*slot) 4825 /* The user's placed something in the implementor's namespace. */ 4826 diagnose_name_conflict (decl, MAYBE_STAT_DECL (*slot)); 4827 4828 /* Force the binding, so compiler internals continue to work. */ 4829 *slot = decl; 4830 4831 timevar_cond_stop (TV_NAME_LOOKUP, subtime); 4832 } 4833 4834 /* Set the context of a declaration to scope. Complain if we are not 4835 outside scope. */ 4836 4837 void 4838 set_decl_namespace (tree decl, tree scope, bool friendp) 4839 { 4840 /* Get rid of namespace aliases. */ 4841 scope = ORIGINAL_NAMESPACE (scope); 4842 4843 /* It is ok for friends to be qualified in parallel space. */ 4844 if (!friendp && !is_nested_namespace (current_namespace, scope)) 4845 error ("declaration of %qD not in a namespace surrounding %qD", 4846 decl, scope); 4847 DECL_CONTEXT (decl) = FROB_CONTEXT (scope); 4848 4849 /* See whether this has been declared in the namespace or inline 4850 children. */ 4851 tree old = NULL_TREE; 4852 { 4853 name_lookup lookup (DECL_NAME (decl), LOOKUP_HIDDEN); 4854 if (!lookup.search_qualified (scope, /*usings=*/false)) 4855 /* No old declaration at all. */ 4856 goto not_found; 4857 old = lookup.value; 4858 } 4859 4860 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */ 4861 if (TREE_CODE (old) == TREE_LIST) 4862 { 4863 ambiguous: 4864 DECL_CONTEXT (decl) = FROB_CONTEXT (scope); 4865 error ("reference to %qD is ambiguous", decl); 4866 print_candidates (old); 4867 return; 4868 } 4869 4870 if (!DECL_DECLARES_FUNCTION_P (decl)) 4871 { 4872 /* Don't compare non-function decls with decls_match here, since 4873 it can't check for the correct constness at this 4874 point. pushdecl will find those errors later. */ 4875 4876 /* We might have found it in an inline namespace child of SCOPE. */ 4877 if (TREE_CODE (decl) == TREE_CODE (old)) 4878 DECL_CONTEXT (decl) = DECL_CONTEXT (old); 4879 4880 found: 4881 /* Writing "N::i" to declare something directly in "N" is invalid. */ 4882 if (CP_DECL_CONTEXT (decl) == current_namespace 4883 && at_namespace_scope_p ()) 4884 error ("explicit qualification in declaration of %qD", decl); 4885 return; 4886 } 4887 4888 /* Since decl is a function, old should contain a function decl. */ 4889 if (!OVL_P (old)) 4890 goto not_found; 4891 4892 /* We handle these in check_explicit_instantiation_namespace. */ 4893 if (processing_explicit_instantiation) 4894 return; 4895 if (processing_template_decl || processing_specialization) 4896 /* We have not yet called push_template_decl to turn a 4897 FUNCTION_DECL into a TEMPLATE_DECL, so the declarations won't 4898 match. But, we'll check later, when we construct the 4899 template. */ 4900 return; 4901 /* Instantiations or specializations of templates may be declared as 4902 friends in any namespace. */ 4903 if (friendp && DECL_USE_TEMPLATE (decl)) 4904 return; 4905 4906 tree found; 4907 found = NULL_TREE; 4908 4909 for (lkp_iterator iter (old); iter; ++iter) 4910 { 4911 if (iter.using_p ()) 4912 continue; 4913 4914 tree ofn = *iter; 4915 4916 /* Adjust DECL_CONTEXT first so decls_match will return true 4917 if DECL will match a declaration in an inline namespace. */ 4918 DECL_CONTEXT (decl) = DECL_CONTEXT (ofn); 4919 if (decls_match (decl, ofn)) 4920 { 4921 if (found) 4922 { 4923 /* We found more than one matching declaration. */ 4924 DECL_CONTEXT (decl) = FROB_CONTEXT (scope); 4925 goto ambiguous; 4926 } 4927 found = ofn; 4928 } 4929 } 4930 4931 if (found) 4932 { 4933 if (DECL_HIDDEN_FRIEND_P (found)) 4934 { 4935 pedwarn (DECL_SOURCE_LOCATION (decl), 0, 4936 "%qD has not been declared within %qD", decl, scope); 4937 inform (DECL_SOURCE_LOCATION (found), 4938 "only here as a %<friend%>"); 4939 } 4940 DECL_CONTEXT (decl) = DECL_CONTEXT (found); 4941 goto found; 4942 } 4943 4944 not_found: 4945 /* It didn't work, go back to the explicit scope. */ 4946 DECL_CONTEXT (decl) = FROB_CONTEXT (scope); 4947 error ("%qD should have been declared inside %qD", decl, scope); 4948 } 4949 4950 /* Return the namespace where the current declaration is declared. */ 4951 4952 tree 4953 current_decl_namespace (void) 4954 { 4955 tree result; 4956 /* If we have been pushed into a different namespace, use it. */ 4957 if (!vec_safe_is_empty (decl_namespace_list)) 4958 return decl_namespace_list->last (); 4959 4960 if (current_class_type) 4961 result = decl_namespace_context (current_class_type); 4962 else if (current_function_decl) 4963 result = decl_namespace_context (current_function_decl); 4964 else 4965 result = current_namespace; 4966 return result; 4967 } 4968 4969 /* Process any ATTRIBUTES on a namespace definition. Returns true if 4970 attribute visibility is seen. */ 4971 4972 bool 4973 handle_namespace_attrs (tree ns, tree attributes) 4974 { 4975 tree d; 4976 bool saw_vis = false; 4977 4978 if (attributes == error_mark_node) 4979 return false; 4980 4981 for (d = attributes; d; d = TREE_CHAIN (d)) 4982 { 4983 tree name = get_attribute_name (d); 4984 tree args = TREE_VALUE (d); 4985 4986 if (is_attribute_p ("visibility", name)) 4987 { 4988 /* attribute visibility is a property of the syntactic block 4989 rather than the namespace as a whole, so we don't touch the 4990 NAMESPACE_DECL at all. */ 4991 tree x = args ? TREE_VALUE (args) : NULL_TREE; 4992 if (x == NULL_TREE || TREE_CODE (x) != STRING_CST || TREE_CHAIN (args)) 4993 { 4994 warning (OPT_Wattributes, 4995 "%qD attribute requires a single NTBS argument", 4996 name); 4997 continue; 4998 } 4999 5000 if (!TREE_PUBLIC (ns)) 5001 warning (OPT_Wattributes, 5002 "%qD attribute is meaningless since members of the " 5003 "anonymous namespace get local symbols", name); 5004 5005 push_visibility (TREE_STRING_POINTER (x), 1); 5006 saw_vis = true; 5007 } 5008 else if (is_attribute_p ("abi_tag", name)) 5009 { 5010 if (!DECL_NAME (ns)) 5011 { 5012 warning (OPT_Wattributes, "ignoring %qD attribute on anonymous " 5013 "namespace", name); 5014 continue; 5015 } 5016 if (!DECL_NAMESPACE_INLINE_P (ns)) 5017 { 5018 warning (OPT_Wattributes, "ignoring %qD attribute on non-inline " 5019 "namespace", name); 5020 continue; 5021 } 5022 if (!args) 5023 { 5024 tree dn = DECL_NAME (ns); 5025 args = build_string (IDENTIFIER_LENGTH (dn) + 1, 5026 IDENTIFIER_POINTER (dn)); 5027 TREE_TYPE (args) = char_array_type_node; 5028 args = fix_string_type (args); 5029 args = build_tree_list (NULL_TREE, args); 5030 } 5031 if (check_abi_tag_args (args, name)) 5032 DECL_ATTRIBUTES (ns) = tree_cons (name, args, 5033 DECL_ATTRIBUTES (ns)); 5034 } 5035 else 5036 { 5037 warning (OPT_Wattributes, "%qD attribute directive ignored", 5038 name); 5039 continue; 5040 } 5041 } 5042 5043 return saw_vis; 5044 } 5045 5046 /* Temporarily set the namespace for the current declaration. */ 5047 5048 void 5049 push_decl_namespace (tree decl) 5050 { 5051 if (TREE_CODE (decl) != NAMESPACE_DECL) 5052 decl = decl_namespace_context (decl); 5053 vec_safe_push (decl_namespace_list, ORIGINAL_NAMESPACE (decl)); 5054 } 5055 5056 /* [namespace.memdef]/2 */ 5057 5058 void 5059 pop_decl_namespace (void) 5060 { 5061 decl_namespace_list->pop (); 5062 } 5063 5064 /* Process a namespace-alias declaration. */ 5065 5066 void 5067 do_namespace_alias (tree alias, tree name_space) 5068 { 5069 if (name_space == error_mark_node) 5070 return; 5071 5072 gcc_assert (TREE_CODE (name_space) == NAMESPACE_DECL); 5073 5074 name_space = ORIGINAL_NAMESPACE (name_space); 5075 5076 /* Build the alias. */ 5077 alias = build_lang_decl (NAMESPACE_DECL, alias, void_type_node); 5078 DECL_NAMESPACE_ALIAS (alias) = name_space; 5079 DECL_EXTERNAL (alias) = 1; 5080 DECL_CONTEXT (alias) = FROB_CONTEXT (current_scope ()); 5081 pushdecl (alias); 5082 5083 /* Emit debug info for namespace alias. */ 5084 if (!building_stmt_list_p ()) 5085 (*debug_hooks->early_global_decl) (alias); 5086 } 5087 5088 /* Like pushdecl, only it places X in the current namespace, 5089 if appropriate. */ 5090 5091 tree 5092 pushdecl_namespace_level (tree x, bool is_friend) 5093 { 5094 cp_binding_level *b = current_binding_level; 5095 tree t; 5096 5097 bool subtime = timevar_cond_start (TV_NAME_LOOKUP); 5098 t = do_pushdecl_with_scope 5099 (x, NAMESPACE_LEVEL (current_namespace), is_friend); 5100 5101 /* Now, the type_shadowed stack may screw us. Munge it so it does 5102 what we want. */ 5103 if (TREE_CODE (t) == TYPE_DECL) 5104 { 5105 tree name = DECL_NAME (t); 5106 tree newval; 5107 tree *ptr = (tree *)0; 5108 for (; !global_scope_p (b); b = b->level_chain) 5109 { 5110 tree shadowed = b->type_shadowed; 5111 for (; shadowed; shadowed = TREE_CHAIN (shadowed)) 5112 if (TREE_PURPOSE (shadowed) == name) 5113 { 5114 ptr = &TREE_VALUE (shadowed); 5115 /* Can't break out of the loop here because sometimes 5116 a binding level will have duplicate bindings for 5117 PT names. It's gross, but I haven't time to fix it. */ 5118 } 5119 } 5120 newval = TREE_TYPE (t); 5121 if (ptr == (tree *)0) 5122 { 5123 /* @@ This shouldn't be needed. My test case "zstring.cc" trips 5124 up here if this is changed to an assertion. --KR */ 5125 SET_IDENTIFIER_TYPE_VALUE (name, t); 5126 } 5127 else 5128 { 5129 *ptr = newval; 5130 } 5131 } 5132 timevar_cond_stop (TV_NAME_LOOKUP, subtime); 5133 return t; 5134 } 5135 5136 /* Process a using-declaration appearing in namespace scope. */ 5137 5138 void 5139 finish_namespace_using_decl (tree decl, tree scope, tree name) 5140 { 5141 tree orig_decl = decl; 5142 5143 gcc_checking_assert (current_binding_level->kind == sk_namespace 5144 && !processing_template_decl); 5145 decl = validate_nonmember_using_decl (decl, scope, name); 5146 if (decl == NULL_TREE) 5147 return; 5148 5149 tree *slot = find_namespace_slot (current_namespace, name, true); 5150 tree val = slot ? MAYBE_STAT_DECL (*slot) : NULL_TREE; 5151 tree type = slot ? MAYBE_STAT_TYPE (*slot) : NULL_TREE; 5152 do_nonmember_using_decl (scope, name, &val, &type); 5153 if (STAT_HACK_P (*slot)) 5154 { 5155 STAT_DECL (*slot) = val; 5156 STAT_TYPE (*slot) = type; 5157 } 5158 else if (type) 5159 *slot = stat_hack (val, type); 5160 else 5161 *slot = val; 5162 5163 /* Emit debug info. */ 5164 cp_emit_debug_info_for_using (orig_decl, current_namespace); 5165 } 5166 5167 /* Process a using-declaration at function scope. */ 5168 5169 void 5170 finish_local_using_decl (tree decl, tree scope, tree name) 5171 { 5172 tree orig_decl = decl; 5173 5174 gcc_checking_assert (current_binding_level->kind != sk_class 5175 && current_binding_level->kind != sk_namespace); 5176 decl = validate_nonmember_using_decl (decl, scope, name); 5177 if (decl == NULL_TREE) 5178 return; 5179 5180 add_decl_expr (decl); 5181 5182 cxx_binding *binding = find_local_binding (current_binding_level, name); 5183 tree value = binding ? binding->value : NULL_TREE; 5184 tree type = binding ? binding->type : NULL_TREE; 5185 5186 do_nonmember_using_decl (scope, name, &value, &type); 5187 5188 if (!value) 5189 ; 5190 else if (binding && value == binding->value) 5191 ; 5192 else if (binding && binding->value && TREE_CODE (value) == OVERLOAD) 5193 { 5194 update_local_overload (IDENTIFIER_BINDING (name), value); 5195 IDENTIFIER_BINDING (name)->value = value; 5196 } 5197 else 5198 /* Install the new binding. */ 5199 push_local_binding (name, value, true); 5200 5201 if (!type) 5202 ; 5203 else if (binding && type == binding->type) 5204 ; 5205 else 5206 { 5207 push_local_binding (name, type, true); 5208 set_identifier_type_value (name, type); 5209 } 5210 5211 /* Emit debug info. */ 5212 if (!processing_template_decl) 5213 cp_emit_debug_info_for_using (orig_decl, current_scope ()); 5214 } 5215 5216 /* Return the declarations that are members of the namespace NS. */ 5217 5218 tree 5219 cp_namespace_decls (tree ns) 5220 { 5221 return NAMESPACE_LEVEL (ns)->names; 5222 } 5223 5224 /* Combine prefer_type and namespaces_only into flags. */ 5225 5226 static int 5227 lookup_flags (int prefer_type, int namespaces_only) 5228 { 5229 if (namespaces_only) 5230 return LOOKUP_PREFER_NAMESPACES; 5231 if (prefer_type > 1) 5232 return LOOKUP_PREFER_TYPES; 5233 if (prefer_type > 0) 5234 return LOOKUP_PREFER_BOTH; 5235 return 0; 5236 } 5237 5238 /* Given a lookup that returned VAL, use FLAGS to decide if we want to 5239 ignore it or not. Subroutine of lookup_name_real and 5240 lookup_type_scope. */ 5241 5242 static bool 5243 qualify_lookup (tree val, int flags) 5244 { 5245 if (val == NULL_TREE) 5246 return false; 5247 if ((flags & LOOKUP_PREFER_NAMESPACES) && TREE_CODE (val) == NAMESPACE_DECL) 5248 return true; 5249 if (flags & LOOKUP_PREFER_TYPES) 5250 { 5251 tree target_val = strip_using_decl (val); 5252 if (TREE_CODE (target_val) == TYPE_DECL 5253 || TREE_CODE (target_val) == TEMPLATE_DECL) 5254 return true; 5255 } 5256 if (flags & (LOOKUP_PREFER_NAMESPACES | LOOKUP_PREFER_TYPES)) 5257 return false; 5258 /* Look through lambda things that we shouldn't be able to see. */ 5259 if (!(flags & LOOKUP_HIDDEN) && is_lambda_ignored_entity (val)) 5260 return false; 5261 return true; 5262 } 5263 5264 /* Is there a "using namespace std;" directive within USINGS? */ 5265 5266 static bool 5267 using_directives_contain_std_p (vec<tree, va_gc> *usings) 5268 { 5269 if (!usings) 5270 return false; 5271 5272 for (unsigned ix = usings->length (); ix--;) 5273 if ((*usings)[ix] == std_node) 5274 return true; 5275 5276 return false; 5277 } 5278 5279 /* Is there a "using namespace std;" directive within the current 5280 namespace (or its ancestors)? 5281 Compare with name_lookup::search_unqualified. */ 5282 5283 static bool 5284 has_using_namespace_std_directive_p () 5285 { 5286 /* Look at local using-directives. */ 5287 for (cp_binding_level *level = current_binding_level; 5288 level->kind != sk_namespace; 5289 level = level->level_chain) 5290 if (using_directives_contain_std_p (level->using_directives)) 5291 return true; 5292 5293 /* Look at this namespace and its ancestors. */ 5294 for (tree scope = current_namespace; scope; scope = CP_DECL_CONTEXT (scope)) 5295 { 5296 if (using_directives_contain_std_p (DECL_NAMESPACE_USING (scope))) 5297 return true; 5298 5299 if (scope == global_namespace) 5300 break; 5301 } 5302 5303 return false; 5304 } 5305 5306 /* Subclass of deferred_diagnostic, for issuing a note when 5307 --param cxx-max-namespaces-for-diagnostic-help is reached. 5308 5309 The note should be issued after the error, but before any other 5310 deferred diagnostics. This is handled by decorating a wrapped 5311 deferred_diagnostic, and emitting a note before that wrapped note is 5312 deleted. */ 5313 5314 class namespace_limit_reached : public deferred_diagnostic 5315 { 5316 public: 5317 namespace_limit_reached (location_t loc, unsigned limit, tree name, 5318 gnu::unique_ptr<deferred_diagnostic> wrapped) 5319 : deferred_diagnostic (loc), 5320 m_limit (limit), m_name (name), 5321 m_wrapped (move (wrapped)) 5322 { 5323 } 5324 5325 ~namespace_limit_reached () 5326 { 5327 /* Unconditionally warn that the search was truncated. */ 5328 inform (get_location (), 5329 "maximum limit of %d namespaces searched for %qE", 5330 m_limit, m_name); 5331 /* m_wrapped will be implicitly deleted after this, emitting any followup 5332 diagnostic after the above note. */ 5333 } 5334 5335 private: 5336 unsigned m_limit; 5337 tree m_name; 5338 gnu::unique_ptr<deferred_diagnostic> m_wrapped; 5339 }; 5340 5341 /* Subclass of deferred_diagnostic, for use when issuing a single suggestion. 5342 Emit a note showing the location of the declaration of the suggestion. */ 5343 5344 class show_candidate_location : public deferred_diagnostic 5345 { 5346 public: 5347 show_candidate_location (location_t loc, tree candidate) 5348 : deferred_diagnostic (loc), 5349 m_candidate (candidate) 5350 { 5351 } 5352 5353 ~show_candidate_location () 5354 { 5355 inform (location_of (m_candidate), "%qE declared here", m_candidate); 5356 } 5357 5358 private: 5359 tree m_candidate; 5360 }; 5361 5362 /* Subclass of deferred_diagnostic, for use when there are multiple candidates 5363 to be suggested by suggest_alternatives_for. 5364 5365 Emit a series of notes showing the various suggestions. */ 5366 5367 class suggest_alternatives : public deferred_diagnostic 5368 { 5369 public: 5370 suggest_alternatives (location_t loc, vec<tree> candidates) 5371 : deferred_diagnostic (loc), 5372 m_candidates (candidates) 5373 { 5374 } 5375 5376 ~suggest_alternatives () 5377 { 5378 if (m_candidates.length ()) 5379 { 5380 inform_n (get_location (), m_candidates.length (), 5381 "suggested alternative:", 5382 "suggested alternatives:"); 5383 for (unsigned ix = 0; ix != m_candidates.length (); ix++) 5384 { 5385 tree val = m_candidates[ix]; 5386 5387 inform (location_of (val), " %qE", val); 5388 } 5389 } 5390 m_candidates.release (); 5391 } 5392 5393 private: 5394 vec<tree> m_candidates; 5395 }; 5396 5397 /* A class for encapsulating the result of a search across 5398 multiple namespaces (and scoped enums within them) for an 5399 unrecognized name seen at a given source location. */ 5400 5401 class namespace_hints 5402 { 5403 public: 5404 namespace_hints (location_t loc, tree name); 5405 5406 name_hint convert_candidates_to_name_hint (); 5407 name_hint maybe_decorate_with_limit (name_hint); 5408 5409 private: 5410 void maybe_add_candidate_for_scoped_enum (tree scoped_enum, tree name); 5411 5412 location_t m_loc; 5413 tree m_name; 5414 vec<tree> m_candidates; 5415 5416 /* Value of "--param cxx-max-namespaces-for-diagnostic-help". */ 5417 unsigned m_limit; 5418 5419 /* Was the limit reached? */ 5420 bool m_limited; 5421 }; 5422 5423 /* Constructor for namespace_hints. Search namespaces and scoped enums, 5424 looking for an exact match for unrecognized NAME seen at LOC. */ 5425 5426 namespace_hints::namespace_hints (location_t loc, tree name) 5427 : m_loc(loc), m_name (name) 5428 { 5429 auto_vec<tree> worklist; 5430 5431 m_candidates = vNULL; 5432 m_limited = false; 5433 m_limit = PARAM_VALUE (CXX_MAX_NAMESPACES_FOR_DIAGNOSTIC_HELP); 5434 5435 /* Breadth-first search of namespaces. Up to limit namespaces 5436 searched (limit zero == unlimited). */ 5437 worklist.safe_push (global_namespace); 5438 for (unsigned ix = 0; ix != worklist.length (); ix++) 5439 { 5440 tree ns = worklist[ix]; 5441 name_lookup lookup (name); 5442 5443 if (lookup.search_qualified (ns, false)) 5444 m_candidates.safe_push (lookup.value); 5445 5446 if (!m_limited) 5447 { 5448 /* Look for child namespaces. We have to do this 5449 indirectly because they are chained in reverse order, 5450 which is confusing to the user. */ 5451 auto_vec<tree> children; 5452 5453 for (tree decl = NAMESPACE_LEVEL (ns)->names; 5454 decl; decl = TREE_CHAIN (decl)) 5455 { 5456 if (TREE_CODE (decl) == NAMESPACE_DECL 5457 && !DECL_NAMESPACE_ALIAS (decl) 5458 && !DECL_NAMESPACE_INLINE_P (decl)) 5459 children.safe_push (decl); 5460 5461 /* Look for exact matches for NAME within scoped enums. 5462 These aren't added to the worklist, and so don't count 5463 against the search limit. */ 5464 if (TREE_CODE (decl) == TYPE_DECL) 5465 { 5466 tree type = TREE_TYPE (decl); 5467 if (SCOPED_ENUM_P (type)) 5468 maybe_add_candidate_for_scoped_enum (type, name); 5469 } 5470 } 5471 5472 while (!m_limited && !children.is_empty ()) 5473 { 5474 if (worklist.length () == m_limit) 5475 m_limited = true; 5476 else 5477 worklist.safe_push (children.pop ()); 5478 } 5479 } 5480 } 5481 } 5482 5483 /* Drop ownership of m_candidates, using it to generate a name_hint at m_loc 5484 for m_name, an IDENTIFIER_NODE for which name lookup failed. 5485 5486 If m_candidates is non-empty, use it to generate a suggestion and/or 5487 a deferred diagnostic that lists the possible candidate(s). 5488 */ 5489 5490 name_hint 5491 namespace_hints::convert_candidates_to_name_hint () 5492 { 5493 /* How many candidates do we have? */ 5494 5495 /* If we have just one candidate, issue a name_hint with it as a suggestion 5496 (so that consumers are able to suggest it within the error message and emit 5497 it as a fix-it hint), and with a note showing the candidate's location. */ 5498 if (m_candidates.length () == 1) 5499 { 5500 tree candidate = m_candidates[0]; 5501 /* Clean up CANDIDATES. */ 5502 m_candidates.release (); 5503 return name_hint (expr_to_string (candidate), 5504 new show_candidate_location (m_loc, candidate)); 5505 } 5506 else if (m_candidates.length () > 1) 5507 /* If we have more than one candidate, issue a name_hint without a single 5508 "suggestion", but with a deferred diagnostic that lists the 5509 various candidates. This takes ownership of m_candidates. */ 5510 return name_hint (NULL, new suggest_alternatives (m_loc, m_candidates)); 5511 5512 /* Otherwise, m_candidates ought to be empty, so no cleanup is necessary. */ 5513 gcc_assert (m_candidates.length () == 0); 5514 gcc_assert (m_candidates == vNULL); 5515 5516 return name_hint (); 5517 } 5518 5519 /* If --param cxx-max-namespaces-for-diagnostic-help was reached, 5520 then we want to emit a note about after the error, but before 5521 any other deferred diagnostics. 5522 5523 Handle this by figuring out what hint is needed, then optionally 5524 decorating HINT with a namespace_limit_reached wrapper. */ 5525 5526 name_hint 5527 namespace_hints::maybe_decorate_with_limit (name_hint hint) 5528 { 5529 if (m_limited) 5530 return name_hint (hint.suggestion (), 5531 new namespace_limit_reached (m_loc, m_limit, 5532 m_name, 5533 hint.take_deferred ())); 5534 else 5535 return hint; 5536 } 5537 5538 /* Look inside SCOPED_ENUM for exact matches for NAME. 5539 If one is found, add its CONST_DECL to m_candidates. */ 5540 5541 void 5542 namespace_hints::maybe_add_candidate_for_scoped_enum (tree scoped_enum, 5543 tree name) 5544 { 5545 gcc_assert (SCOPED_ENUM_P (scoped_enum)); 5546 5547 for (tree iter = TYPE_VALUES (scoped_enum); iter; iter = TREE_CHAIN (iter)) 5548 { 5549 tree id = TREE_PURPOSE (iter); 5550 if (id == name) 5551 { 5552 m_candidates.safe_push (TREE_VALUE (iter)); 5553 return; 5554 } 5555 } 5556 } 5557 5558 /* Generate a name_hint at LOCATION for NAME, an IDENTIFIER_NODE for which 5559 name lookup failed. 5560 5561 Search through all available namespaces and any scoped enums within them 5562 and generate a suggestion and/or a deferred diagnostic that lists possible 5563 candidate(s). 5564 5565 If no exact matches are found, and SUGGEST_MISSPELLINGS is true, then also 5566 look for near-matches and suggest the best near-match, if there is one. 5567 5568 If nothing is found, then an empty name_hint is returned. */ 5569 5570 name_hint 5571 suggest_alternatives_for (location_t location, tree name, 5572 bool suggest_misspellings) 5573 { 5574 /* First, search for exact matches in other namespaces. */ 5575 namespace_hints ns_hints (location, name); 5576 name_hint result = ns_hints.convert_candidates_to_name_hint (); 5577 5578 /* Otherwise, try other approaches. */ 5579 if (!result) 5580 result = suggest_alternatives_for_1 (location, name, suggest_misspellings); 5581 5582 return ns_hints.maybe_decorate_with_limit (gnu::move (result)); 5583 } 5584 5585 /* The second half of suggest_alternatives_for, for when no exact matches 5586 were found in other namespaces. */ 5587 5588 static name_hint 5589 suggest_alternatives_for_1 (location_t location, tree name, 5590 bool suggest_misspellings) 5591 { 5592 /* No candidates were found in the available namespaces. */ 5593 5594 /* If there's a "using namespace std;" active, and this 5595 is one of the most common "std::" names, then it's probably a 5596 missing #include. */ 5597 if (has_using_namespace_std_directive_p ()) 5598 { 5599 name_hint hint = maybe_suggest_missing_std_header (location, name); 5600 if (hint) 5601 return hint; 5602 } 5603 5604 /* Otherwise, consider misspellings. */ 5605 if (!suggest_misspellings) 5606 return name_hint (); 5607 5608 return lookup_name_fuzzy (name, FUZZY_LOOKUP_NAME, location); 5609 } 5610 5611 /* Generate a name_hint at LOCATION for NAME, an IDENTIFIER_NODE for which 5612 name lookup failed. 5613 5614 Search through all available namespaces and generate a suggestion and/or 5615 a deferred diagnostic that lists possible candidate(s). 5616 5617 This is similiar to suggest_alternatives_for, but doesn't fallback to 5618 the other approaches used by that function. */ 5619 5620 name_hint 5621 suggest_alternatives_in_other_namespaces (location_t location, tree name) 5622 { 5623 namespace_hints ns_hints (location, name); 5624 5625 name_hint result = ns_hints.convert_candidates_to_name_hint (); 5626 5627 return ns_hints.maybe_decorate_with_limit (gnu::move (result)); 5628 } 5629 5630 /* A well-known name within the C++ standard library, returned by 5631 get_std_name_hint. */ 5632 5633 struct std_name_hint 5634 { 5635 /* A name within "std::". */ 5636 const char *name; 5637 5638 /* The header name defining it within the C++ Standard Library 5639 (with '<' and '>'). */ 5640 const char *header; 5641 5642 /* The dialect of C++ in which this was added. */ 5643 enum cxx_dialect min_dialect; 5644 }; 5645 5646 /* Subroutine of maybe_suggest_missing_header for handling unrecognized names 5647 for some of the most common names within "std::". 5648 Given non-NULL NAME, return the std_name_hint for it, or NULL. */ 5649 5650 static const std_name_hint * 5651 get_std_name_hint (const char *name) 5652 { 5653 static const std_name_hint hints[] = { 5654 /* <any>. */ 5655 {"any", "<any>", cxx17}, 5656 {"any_cast", "<any>", cxx17}, 5657 {"make_any", "<any>", cxx17}, 5658 /* <array>. */ 5659 {"array", "<array>", cxx11}, 5660 /* <atomic>. */ 5661 {"atomic", "<atomic>", cxx11}, 5662 {"atomic_flag", "<atomic>", cxx11}, 5663 /* <bitset>. */ 5664 {"bitset", "<bitset>", cxx11}, 5665 /* <complex>. */ 5666 {"complex", "<complex>", cxx98}, 5667 {"complex_literals", "<complex>", cxx14}, 5668 /* <condition_variable>. */ 5669 {"condition_variable", "<condition_variable>", cxx11}, 5670 {"condition_variable_any", "<condition_variable>", cxx11}, 5671 /* <deque>. */ 5672 {"deque", "<deque>", cxx98}, 5673 /* <forward_list>. */ 5674 {"forward_list", "<forward_list>", cxx11}, 5675 /* <fstream>. */ 5676 {"basic_filebuf", "<fstream>", cxx98}, 5677 {"basic_ifstream", "<fstream>", cxx98}, 5678 {"basic_ofstream", "<fstream>", cxx98}, 5679 {"basic_fstream", "<fstream>", cxx98}, 5680 {"fstream", "<fstream>", cxx98}, 5681 {"ifstream", "<fstream>", cxx98}, 5682 {"ofstream", "<fstream>", cxx98}, 5683 /* <functional>. */ 5684 {"bind", "<functional>", cxx11}, 5685 {"function", "<functional>", cxx11}, 5686 {"hash", "<functional>", cxx11}, 5687 {"mem_fn", "<functional>", cxx11}, 5688 /* <future>. */ 5689 {"async", "<future>", cxx11}, 5690 {"future", "<future>", cxx11}, 5691 {"packaged_task", "<future>", cxx11}, 5692 {"promise", "<future>", cxx11}, 5693 /* <iostream>. */ 5694 {"cin", "<iostream>", cxx98}, 5695 {"cout", "<iostream>", cxx98}, 5696 {"cerr", "<iostream>", cxx98}, 5697 {"clog", "<iostream>", cxx98}, 5698 {"wcin", "<iostream>", cxx98}, 5699 {"wcout", "<iostream>", cxx98}, 5700 {"wclog", "<iostream>", cxx98}, 5701 /* <istream>. */ 5702 {"istream", "<istream>", cxx98}, 5703 /* <iterator>. */ 5704 {"advance", "<iterator>", cxx98}, 5705 {"back_inserter", "<iterator>", cxx98}, 5706 {"begin", "<iterator>", cxx11}, 5707 {"distance", "<iterator>", cxx98}, 5708 {"end", "<iterator>", cxx11}, 5709 {"front_inserter", "<iterator>", cxx98}, 5710 {"inserter", "<iterator>", cxx98}, 5711 {"istream_iterator", "<iterator>", cxx98}, 5712 {"istreambuf_iterator", "<iterator>", cxx98}, 5713 {"iterator_traits", "<iterator>", cxx98}, 5714 {"move_iterator", "<iterator>", cxx11}, 5715 {"next", "<iterator>", cxx11}, 5716 {"ostream_iterator", "<iterator>", cxx98}, 5717 {"ostreambuf_iterator", "<iterator>", cxx98}, 5718 {"prev", "<iterator>", cxx11}, 5719 {"reverse_iterator", "<iterator>", cxx98}, 5720 /* <ostream>. */ 5721 {"ostream", "<ostream>", cxx98}, 5722 /* <list>. */ 5723 {"list", "<list>", cxx98}, 5724 /* <map>. */ 5725 {"map", "<map>", cxx98}, 5726 {"multimap", "<map>", cxx98}, 5727 /* <memory>. */ 5728 {"make_shared", "<memory>", cxx11}, 5729 {"make_unique", "<memory>", cxx14}, 5730 {"shared_ptr", "<memory>", cxx11}, 5731 {"unique_ptr", "<memory>", cxx11}, 5732 {"weak_ptr", "<memory>", cxx11}, 5733 /* <mutex>. */ 5734 {"mutex", "<mutex>", cxx11}, 5735 {"timed_mutex", "<mutex>", cxx11}, 5736 {"recursive_mutex", "<mutex>", cxx11}, 5737 {"recursive_timed_mutex", "<mutex>", cxx11}, 5738 {"once_flag", "<mutex>", cxx11}, 5739 {"call_once,", "<mutex>", cxx11}, 5740 {"lock", "<mutex>", cxx11}, 5741 {"scoped_lock", "<mutex>", cxx17}, 5742 {"try_lock", "<mutex>", cxx11}, 5743 {"lock_guard", "<mutex>", cxx11}, 5744 {"unique_lock", "<mutex>", cxx11}, 5745 /* <optional>. */ 5746 {"optional", "<optional>", cxx17}, 5747 {"make_optional", "<optional>", cxx17}, 5748 /* <ostream>. */ 5749 {"ostream", "<ostream>", cxx98}, 5750 {"wostream", "<ostream>", cxx98}, 5751 {"ends", "<ostream>", cxx98}, 5752 {"flush", "<ostream>", cxx98}, 5753 {"endl", "<ostream>", cxx98}, 5754 /* <queue>. */ 5755 {"queue", "<queue>", cxx98}, 5756 {"priority_queue", "<queue>", cxx98}, 5757 /* <set>. */ 5758 {"set", "<set>", cxx98}, 5759 {"multiset", "<set>", cxx98}, 5760 /* <shared_mutex>. */ 5761 {"shared_lock", "<shared_mutex>", cxx14}, 5762 {"shared_mutex", "<shared_mutex>", cxx17}, 5763 {"shared_timed_mutex", "<shared_mutex>", cxx14}, 5764 /* <sstream>. */ 5765 {"basic_stringbuf", "<sstream>", cxx98}, 5766 {"basic_istringstream", "<sstream>", cxx98}, 5767 {"basic_ostringstream", "<sstream>", cxx98}, 5768 {"basic_stringstream", "<sstream>", cxx98}, 5769 {"istringstream", "<sstream>", cxx98}, 5770 {"ostringstream", "<sstream>", cxx98}, 5771 {"stringstream", "<sstream>", cxx98}, 5772 /* <stack>. */ 5773 {"stack", "<stack>", cxx98}, 5774 /* <string>. */ 5775 {"basic_string", "<string>", cxx98}, 5776 {"string", "<string>", cxx98}, 5777 {"wstring", "<string>", cxx98}, 5778 {"u8string", "<string>", cxx2a}, 5779 {"u16string", "<string>", cxx11}, 5780 {"u32string", "<string>", cxx11}, 5781 /* <string_view>. */ 5782 {"string_view", "<string_view>", cxx17}, 5783 /* <thread>. */ 5784 {"thread", "<thread>", cxx11}, 5785 /* <tuple>. */ 5786 {"make_tuple", "<tuple>", cxx11}, 5787 {"tuple", "<tuple>", cxx11}, 5788 {"tuple_element", "<tuple>", cxx11}, 5789 {"tuple_size", "<tuple>", cxx11}, 5790 /* <unordered_map>. */ 5791 {"unordered_map", "<unordered_map>", cxx11}, 5792 {"unordered_multimap", "<unordered_map>", cxx11}, 5793 /* <unordered_set>. */ 5794 {"unordered_set", "<unordered_set>", cxx11}, 5795 {"unordered_multiset", "<unordered_set>", cxx11}, 5796 /* <utility>. */ 5797 {"declval", "<utility>", cxx11}, 5798 {"forward", "<utility>", cxx11}, 5799 {"make_pair", "<utility>", cxx98}, 5800 {"move", "<utility>", cxx11}, 5801 {"pair", "<utility>", cxx98}, 5802 /* <variant>. */ 5803 {"variant", "<variant>", cxx17}, 5804 {"visit", "<variant>", cxx17}, 5805 /* <vector>. */ 5806 {"vector", "<vector>", cxx98}, 5807 }; 5808 const size_t num_hints = sizeof (hints) / sizeof (hints[0]); 5809 for (size_t i = 0; i < num_hints; i++) 5810 { 5811 if (strcmp (name, hints[i].name) == 0) 5812 return &hints[i]; 5813 } 5814 return NULL; 5815 } 5816 5817 /* Describe DIALECT. */ 5818 5819 static const char * 5820 get_cxx_dialect_name (enum cxx_dialect dialect) 5821 { 5822 switch (dialect) 5823 { 5824 default: 5825 gcc_unreachable (); 5826 case cxx98: 5827 return "C++98"; 5828 case cxx11: 5829 return "C++11"; 5830 case cxx14: 5831 return "C++14"; 5832 case cxx17: 5833 return "C++17"; 5834 case cxx2a: 5835 return "C++2a"; 5836 } 5837 } 5838 5839 /* Subclass of deferred_diagnostic for use for names in the "std" namespace 5840 that weren't recognized, but for which we know which header it ought to be 5841 in. 5842 5843 Emit a note either suggesting the header to be included, or noting that 5844 the current dialect is too early for the given name. */ 5845 5846 class missing_std_header : public deferred_diagnostic 5847 { 5848 public: 5849 missing_std_header (location_t loc, 5850 const char *name_str, 5851 const std_name_hint *header_hint) 5852 : deferred_diagnostic (loc), 5853 m_name_str (name_str), 5854 m_header_hint (header_hint) 5855 {} 5856 ~missing_std_header () 5857 { 5858 gcc_rich_location richloc (get_location ()); 5859 if (cxx_dialect >= m_header_hint->min_dialect) 5860 { 5861 const char *header = m_header_hint->header; 5862 maybe_add_include_fixit (&richloc, header, true); 5863 inform (&richloc, 5864 "%<std::%s%> is defined in header %qs;" 5865 " did you forget to %<#include %s%>?", 5866 m_name_str, header, header); 5867 } 5868 else 5869 inform (&richloc, 5870 "%<std::%s%> is only available from %s onwards", 5871 m_name_str, get_cxx_dialect_name (m_header_hint->min_dialect)); 5872 } 5873 5874 private: 5875 const char *m_name_str; 5876 const std_name_hint *m_header_hint; 5877 }; 5878 5879 /* Attempt to generate a name_hint that suggests pertinent header files 5880 for NAME at LOCATION, for common names within the "std" namespace, 5881 or an empty name_hint if this isn't applicable. */ 5882 5883 static name_hint 5884 maybe_suggest_missing_std_header (location_t location, tree name) 5885 { 5886 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE); 5887 5888 const char *name_str = IDENTIFIER_POINTER (name); 5889 const std_name_hint *header_hint = get_std_name_hint (name_str); 5890 if (!header_hint) 5891 return name_hint (); 5892 5893 return name_hint (NULL, new missing_std_header (location, name_str, 5894 header_hint)); 5895 } 5896 5897 /* Attempt to generate a name_hint that suggests a missing header file 5898 for NAME within SCOPE at LOCATION, or an empty name_hint if this isn't 5899 applicable. */ 5900 5901 static name_hint 5902 maybe_suggest_missing_header (location_t location, tree name, tree scope) 5903 { 5904 if (scope == NULL_TREE) 5905 return name_hint (); 5906 if (TREE_CODE (scope) != NAMESPACE_DECL) 5907 return name_hint (); 5908 /* We only offer suggestions for the "std" namespace. */ 5909 if (scope != std_node) 5910 return name_hint (); 5911 return maybe_suggest_missing_std_header (location, name); 5912 } 5913 5914 /* Generate a name_hint at LOCATION for NAME, an IDENTIFIER_NODE for which name 5915 lookup failed within the explicitly provided SCOPE. 5916 5917 Suggest the the best meaningful candidates (if any), otherwise 5918 an empty name_hint is returned. */ 5919 5920 name_hint 5921 suggest_alternative_in_explicit_scope (location_t location, tree name, 5922 tree scope) 5923 { 5924 /* Something went very wrong; don't suggest anything. */ 5925 if (name == error_mark_node) 5926 return name_hint (); 5927 5928 /* Resolve any namespace aliases. */ 5929 scope = ORIGINAL_NAMESPACE (scope); 5930 5931 name_hint hint = maybe_suggest_missing_header (location, name, scope); 5932 if (hint) 5933 return hint; 5934 5935 cp_binding_level *level = NAMESPACE_LEVEL (scope); 5936 5937 best_match <tree, const char *> bm (name); 5938 consider_binding_level (name, bm, level, false, FUZZY_LOOKUP_NAME); 5939 5940 /* See if we have a good suggesion for the user. */ 5941 const char *fuzzy_name = bm.get_best_meaningful_candidate (); 5942 if (fuzzy_name) 5943 return name_hint (fuzzy_name, NULL); 5944 5945 return name_hint (); 5946 } 5947 5948 /* Given NAME, look within SCOPED_ENUM for possible spell-correction 5949 candidates. */ 5950 5951 name_hint 5952 suggest_alternative_in_scoped_enum (tree name, tree scoped_enum) 5953 { 5954 gcc_assert (SCOPED_ENUM_P (scoped_enum)); 5955 5956 best_match <tree, const char *> bm (name); 5957 for (tree iter = TYPE_VALUES (scoped_enum); iter; iter = TREE_CHAIN (iter)) 5958 { 5959 tree id = TREE_PURPOSE (iter); 5960 bm.consider (IDENTIFIER_POINTER (id)); 5961 } 5962 return name_hint (bm.get_best_meaningful_candidate (), NULL); 5963 } 5964 5965 /* Look up NAME (an IDENTIFIER_NODE) in SCOPE (either a NAMESPACE_DECL 5966 or a class TYPE). 5967 5968 If PREFER_TYPE is > 0, we only return TYPE_DECLs or namespaces. 5969 If PREFER_TYPE is > 1, we only return TYPE_DECLs. 5970 5971 Returns a DECL (or OVERLOAD, or BASELINK) representing the 5972 declaration found. If no suitable declaration can be found, 5973 ERROR_MARK_NODE is returned. If COMPLAIN is true and SCOPE is 5974 neither a class-type nor a namespace a diagnostic is issued. */ 5975 5976 tree 5977 lookup_qualified_name (tree scope, tree name, int prefer_type, bool complain, 5978 bool find_hidden) 5979 { 5980 tree t = NULL_TREE; 5981 5982 if (TREE_CODE (scope) == NAMESPACE_DECL) 5983 { 5984 int flags = lookup_flags (prefer_type, /*namespaces_only*/false); 5985 if (find_hidden) 5986 flags |= LOOKUP_HIDDEN; 5987 name_lookup lookup (name, flags); 5988 5989 if (qualified_namespace_lookup (scope, &lookup)) 5990 t = lookup.value; 5991 } 5992 else if (cxx_dialect != cxx98 && TREE_CODE (scope) == ENUMERAL_TYPE) 5993 t = lookup_enumerator (scope, name); 5994 else if (is_class_type (scope, complain)) 5995 t = lookup_member (scope, name, 2, prefer_type, tf_warning_or_error); 5996 5997 if (!t) 5998 return error_mark_node; 5999 return t; 6000 } 6001 6002 /* [namespace.qual] 6003 Accepts the NAME to lookup and its qualifying SCOPE. 6004 Returns the name/type pair found into the cxx_binding *RESULT, 6005 or false on error. */ 6006 6007 static bool 6008 qualified_namespace_lookup (tree scope, name_lookup *lookup) 6009 { 6010 timevar_start (TV_NAME_LOOKUP); 6011 query_oracle (lookup->name); 6012 bool found = lookup->search_qualified (ORIGINAL_NAMESPACE (scope)); 6013 timevar_stop (TV_NAME_LOOKUP); 6014 return found; 6015 } 6016 6017 /* Helper function for lookup_name_fuzzy. 6018 Traverse binding level LVL, looking for good name matches for NAME 6019 (and BM). */ 6020 static void 6021 consider_binding_level (tree name, best_match <tree, const char *> &bm, 6022 cp_binding_level *lvl, bool look_within_fields, 6023 enum lookup_name_fuzzy_kind kind) 6024 { 6025 if (look_within_fields) 6026 if (lvl->this_entity && TREE_CODE (lvl->this_entity) == RECORD_TYPE) 6027 { 6028 tree type = lvl->this_entity; 6029 bool want_type_p = (kind == FUZZY_LOOKUP_TYPENAME); 6030 tree best_matching_field 6031 = lookup_member_fuzzy (type, name, want_type_p); 6032 if (best_matching_field) 6033 bm.consider (IDENTIFIER_POINTER (best_matching_field)); 6034 } 6035 6036 /* Only suggest names reserved for the implementation if NAME begins 6037 with an underscore. */ 6038 bool consider_implementation_names = (IDENTIFIER_POINTER (name)[0] == '_'); 6039 6040 for (tree t = lvl->names; t; t = TREE_CHAIN (t)) 6041 { 6042 tree d = t; 6043 6044 /* OVERLOADs or decls from using declaration are wrapped into 6045 TREE_LIST. */ 6046 if (TREE_CODE (d) == TREE_LIST) 6047 d = OVL_FIRST (TREE_VALUE (d)); 6048 6049 /* Don't use bindings from implicitly declared functions, 6050 as they were likely misspellings themselves. */ 6051 if (TREE_TYPE (d) == error_mark_node) 6052 continue; 6053 6054 /* Skip anticipated decls of builtin functions. */ 6055 if (TREE_CODE (d) == FUNCTION_DECL 6056 && fndecl_built_in_p (d) 6057 && DECL_ANTICIPATED (d)) 6058 continue; 6059 6060 /* Skip compiler-generated variables (e.g. __for_begin/__for_end 6061 within range for). */ 6062 if (TREE_CODE (d) == VAR_DECL 6063 && DECL_ARTIFICIAL (d)) 6064 continue; 6065 6066 tree suggestion = DECL_NAME (d); 6067 if (!suggestion) 6068 continue; 6069 6070 /* Don't suggest names that are for anonymous aggregate types, as 6071 they are an implementation detail generated by the compiler. */ 6072 if (anon_aggrname_p (suggestion)) 6073 continue; 6074 6075 const char *suggestion_str = IDENTIFIER_POINTER (suggestion); 6076 6077 /* Ignore internal names with spaces in them. */ 6078 if (strchr (suggestion_str, ' ')) 6079 continue; 6080 6081 /* Don't suggest names that are reserved for use by the 6082 implementation, unless NAME began with an underscore. */ 6083 if (name_reserved_for_implementation_p (suggestion_str) 6084 && !consider_implementation_names) 6085 continue; 6086 6087 bm.consider (suggestion_str); 6088 } 6089 } 6090 6091 /* Subclass of deferred_diagnostic. Notify the user that the 6092 given macro was used before it was defined. 6093 This can be done in the C++ frontend since tokenization happens 6094 upfront. */ 6095 6096 class macro_use_before_def : public deferred_diagnostic 6097 { 6098 public: 6099 /* Factory function. Return a new macro_use_before_def instance if 6100 appropriate, or return NULL. */ 6101 static macro_use_before_def * 6102 maybe_make (location_t use_loc, cpp_hashnode *macro) 6103 { 6104 location_t def_loc = cpp_macro_definition_location (macro); 6105 if (def_loc == UNKNOWN_LOCATION) 6106 return NULL; 6107 6108 /* We only want to issue a note if the macro was used *before* it was 6109 defined. 6110 We don't want to issue a note for cases where a macro was incorrectly 6111 used, leaving it unexpanded (e.g. by using the wrong argument 6112 count). */ 6113 if (!linemap_location_before_p (line_table, use_loc, def_loc)) 6114 return NULL; 6115 6116 return new macro_use_before_def (use_loc, macro); 6117 } 6118 6119 private: 6120 /* Ctor. LOC is the location of the usage. MACRO is the 6121 macro that was used. */ 6122 macro_use_before_def (location_t loc, cpp_hashnode *macro) 6123 : deferred_diagnostic (loc), m_macro (macro) 6124 { 6125 gcc_assert (macro); 6126 } 6127 6128 ~macro_use_before_def () 6129 { 6130 if (is_suppressed_p ()) 6131 return; 6132 6133 inform (get_location (), "the macro %qs had not yet been defined", 6134 (const char *)m_macro->ident.str); 6135 inform (cpp_macro_definition_location (m_macro), 6136 "it was later defined here"); 6137 } 6138 6139 private: 6140 cpp_hashnode *m_macro; 6141 }; 6142 6143 /* Determine if it can ever make sense to offer RID as a suggestion for 6144 a misspelling. 6145 6146 Subroutine of lookup_name_fuzzy. */ 6147 6148 static bool 6149 suggest_rid_p (enum rid rid) 6150 { 6151 switch (rid) 6152 { 6153 /* Support suggesting function-like keywords. */ 6154 case RID_STATIC_ASSERT: 6155 return true; 6156 6157 default: 6158 /* Support suggesting the various decl-specifier words, to handle 6159 e.g. "singed" vs "signed" typos. */ 6160 if (cp_keyword_starts_decl_specifier_p (rid)) 6161 return true; 6162 6163 /* Otherwise, don't offer it. This avoids suggesting e.g. "if" 6164 and "do" for short misspellings, which are likely to lead to 6165 nonsensical results. */ 6166 return false; 6167 } 6168 } 6169 6170 /* Search for near-matches for NAME within the current bindings, and within 6171 macro names, returning the best match as a const char *, or NULL if 6172 no reasonable match is found. 6173 6174 Use LOC for any deferred diagnostics. */ 6175 6176 name_hint 6177 lookup_name_fuzzy (tree name, enum lookup_name_fuzzy_kind kind, location_t loc) 6178 { 6179 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE); 6180 6181 /* First, try some well-known names in the C++ standard library, in case 6182 the user forgot a #include. */ 6183 const char *header_hint 6184 = get_cp_stdlib_header_for_name (IDENTIFIER_POINTER (name)); 6185 if (header_hint) 6186 return name_hint (NULL, 6187 new suggest_missing_header (loc, 6188 IDENTIFIER_POINTER (name), 6189 header_hint)); 6190 6191 best_match <tree, const char *> bm (name); 6192 6193 cp_binding_level *lvl; 6194 for (lvl = scope_chain->class_bindings; lvl; lvl = lvl->level_chain) 6195 consider_binding_level (name, bm, lvl, true, kind); 6196 6197 for (lvl = current_binding_level; lvl; lvl = lvl->level_chain) 6198 consider_binding_level (name, bm, lvl, false, kind); 6199 6200 /* Consider macros: if the user misspelled a macro name e.g. "SOME_MACRO" 6201 as: 6202 x = SOME_OTHER_MACRO (y); 6203 then "SOME_OTHER_MACRO" will survive to the frontend and show up 6204 as a misspelled identifier. 6205 6206 Use the best distance so far so that a candidate is only set if 6207 a macro is better than anything so far. This allows early rejection 6208 (without calculating the edit distance) of macro names that must have 6209 distance >= bm.get_best_distance (), and means that we only get a 6210 non-NULL result for best_macro_match if it's better than any of 6211 the identifiers already checked. */ 6212 best_macro_match bmm (name, bm.get_best_distance (), parse_in); 6213 cpp_hashnode *best_macro = bmm.get_best_meaningful_candidate (); 6214 /* If a macro is the closest so far to NAME, consider it. */ 6215 if (best_macro) 6216 bm.consider ((const char *)best_macro->ident.str); 6217 else if (bmm.get_best_distance () == 0) 6218 { 6219 /* If we have an exact match for a macro name, then either the 6220 macro was used with the wrong argument count, or the macro 6221 has been used before it was defined. */ 6222 if (cpp_hashnode *macro = bmm.blithely_get_best_candidate ()) 6223 if (cpp_user_macro_p (macro)) 6224 return name_hint (NULL, 6225 macro_use_before_def::maybe_make (loc, macro)); 6226 } 6227 6228 /* Try the "starts_decl_specifier_p" keywords to detect 6229 "singed" vs "signed" typos. */ 6230 for (unsigned i = 0; i < num_c_common_reswords; i++) 6231 { 6232 const c_common_resword *resword = &c_common_reswords[i]; 6233 6234 if (!suggest_rid_p (resword->rid)) 6235 continue; 6236 6237 tree resword_identifier = ridpointers [resword->rid]; 6238 if (!resword_identifier) 6239 continue; 6240 gcc_assert (TREE_CODE (resword_identifier) == IDENTIFIER_NODE); 6241 6242 /* Only consider reserved words that survived the 6243 filtering in init_reswords (e.g. for -std). */ 6244 if (!IDENTIFIER_KEYWORD_P (resword_identifier)) 6245 continue; 6246 6247 bm.consider (IDENTIFIER_POINTER (resword_identifier)); 6248 } 6249 6250 return name_hint (bm.get_best_meaningful_candidate (), NULL); 6251 } 6252 6253 /* Subroutine of outer_binding. 6254 6255 Returns TRUE if BINDING is a binding to a template parameter of 6256 SCOPE. In that case SCOPE is the scope of a primary template 6257 parameter -- in the sense of G++, i.e, a template that has its own 6258 template header. 6259 6260 Returns FALSE otherwise. */ 6261 6262 static bool 6263 binding_to_template_parms_of_scope_p (cxx_binding *binding, 6264 cp_binding_level *scope) 6265 { 6266 tree binding_value, tmpl, tinfo; 6267 int level; 6268 6269 if (!binding || !scope || !scope->this_entity) 6270 return false; 6271 6272 binding_value = binding->value ? binding->value : binding->type; 6273 tinfo = get_template_info (scope->this_entity); 6274 6275 /* BINDING_VALUE must be a template parm. */ 6276 if (binding_value == NULL_TREE 6277 || (!DECL_P (binding_value) 6278 || !DECL_TEMPLATE_PARM_P (binding_value))) 6279 return false; 6280 6281 /* The level of BINDING_VALUE. */ 6282 level = 6283 template_type_parameter_p (binding_value) 6284 ? TEMPLATE_PARM_LEVEL (TEMPLATE_TYPE_PARM_INDEX 6285 (TREE_TYPE (binding_value))) 6286 : TEMPLATE_PARM_LEVEL (DECL_INITIAL (binding_value)); 6287 6288 /* The template of the current scope, iff said scope is a primary 6289 template. */ 6290 tmpl = (tinfo 6291 && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo)) 6292 ? TI_TEMPLATE (tinfo) 6293 : NULL_TREE); 6294 6295 /* If the level of the parm BINDING_VALUE equals the depth of TMPL, 6296 then BINDING_VALUE is a parameter of TMPL. */ 6297 return (tmpl && level == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl))); 6298 } 6299 6300 /* Return the innermost non-namespace binding for NAME from a scope 6301 containing BINDING, or, if BINDING is NULL, the current scope. 6302 Please note that for a given template, the template parameters are 6303 considered to be in the scope containing the current scope. 6304 If CLASS_P is false, then class bindings are ignored. */ 6305 6306 cxx_binding * 6307 outer_binding (tree name, 6308 cxx_binding *binding, 6309 bool class_p) 6310 { 6311 cxx_binding *outer; 6312 cp_binding_level *scope; 6313 cp_binding_level *outer_scope; 6314 6315 if (binding) 6316 { 6317 scope = binding->scope->level_chain; 6318 outer = binding->previous; 6319 } 6320 else 6321 { 6322 scope = current_binding_level; 6323 outer = IDENTIFIER_BINDING (name); 6324 } 6325 outer_scope = outer ? outer->scope : NULL; 6326 6327 /* Because we create class bindings lazily, we might be missing a 6328 class binding for NAME. If there are any class binding levels 6329 between the LAST_BINDING_LEVEL and the scope in which OUTER was 6330 declared, we must lookup NAME in those class scopes. */ 6331 if (class_p) 6332 while (scope && scope != outer_scope && scope->kind != sk_namespace) 6333 { 6334 if (scope->kind == sk_class) 6335 { 6336 cxx_binding *class_binding; 6337 6338 class_binding = get_class_binding (name, scope); 6339 if (class_binding) 6340 { 6341 /* Thread this new class-scope binding onto the 6342 IDENTIFIER_BINDING list so that future lookups 6343 find it quickly. */ 6344 class_binding->previous = outer; 6345 if (binding) 6346 binding->previous = class_binding; 6347 else 6348 IDENTIFIER_BINDING (name) = class_binding; 6349 return class_binding; 6350 } 6351 } 6352 /* If we are in a member template, the template parms of the member 6353 template are considered to be inside the scope of the containing 6354 class, but within G++ the class bindings are all pushed between the 6355 template parms and the function body. So if the outer binding is 6356 a template parm for the current scope, return it now rather than 6357 look for a class binding. */ 6358 if (outer_scope && outer_scope->kind == sk_template_parms 6359 && binding_to_template_parms_of_scope_p (outer, scope)) 6360 return outer; 6361 6362 scope = scope->level_chain; 6363 } 6364 6365 return outer; 6366 } 6367 6368 /* Return the innermost block-scope or class-scope value binding for 6369 NAME, or NULL_TREE if there is no such binding. */ 6370 6371 tree 6372 innermost_non_namespace_value (tree name) 6373 { 6374 cxx_binding *binding; 6375 binding = outer_binding (name, /*binding=*/NULL, /*class_p=*/true); 6376 return binding ? binding->value : NULL_TREE; 6377 } 6378 6379 /* Look up NAME in the current binding level and its superiors in the 6380 namespace of variables, functions and typedefs. Return a ..._DECL 6381 node of some kind representing its definition if there is only one 6382 such declaration, or return a TREE_LIST with all the overloaded 6383 definitions if there are many, or return 0 if it is undefined. 6384 Hidden name, either friend declaration or built-in function, are 6385 not ignored. 6386 6387 If PREFER_TYPE is > 0, we prefer TYPE_DECLs or namespaces. 6388 If PREFER_TYPE is > 1, we reject non-type decls (e.g. namespaces). 6389 Otherwise we prefer non-TYPE_DECLs. 6390 6391 If NONCLASS is nonzero, bindings in class scopes are ignored. If 6392 BLOCK_P is false, bindings in block scopes are ignored. */ 6393 6394 static tree 6395 lookup_name_real_1 (tree name, int prefer_type, int nonclass, bool block_p, 6396 int namespaces_only, int flags) 6397 { 6398 cxx_binding *iter; 6399 tree val = NULL_TREE; 6400 6401 query_oracle (name); 6402 6403 /* Conversion operators are handled specially because ordinary 6404 unqualified name lookup will not find template conversion 6405 operators. */ 6406 if (IDENTIFIER_CONV_OP_P (name)) 6407 { 6408 cp_binding_level *level; 6409 6410 for (level = current_binding_level; 6411 level && level->kind != sk_namespace; 6412 level = level->level_chain) 6413 { 6414 tree class_type; 6415 tree operators; 6416 6417 /* A conversion operator can only be declared in a class 6418 scope. */ 6419 if (level->kind != sk_class) 6420 continue; 6421 6422 /* Lookup the conversion operator in the class. */ 6423 class_type = level->this_entity; 6424 operators = lookup_fnfields (class_type, name, /*protect=*/0); 6425 if (operators) 6426 return operators; 6427 } 6428 6429 return NULL_TREE; 6430 } 6431 6432 flags |= lookup_flags (prefer_type, namespaces_only); 6433 6434 /* First, look in non-namespace scopes. */ 6435 6436 if (current_class_type == NULL_TREE) 6437 nonclass = 1; 6438 6439 if (block_p || !nonclass) 6440 for (iter = outer_binding (name, NULL, !nonclass); 6441 iter; 6442 iter = outer_binding (name, iter, !nonclass)) 6443 { 6444 tree binding; 6445 6446 /* Skip entities we don't want. */ 6447 if (LOCAL_BINDING_P (iter) ? !block_p : nonclass) 6448 continue; 6449 6450 /* If this is the kind of thing we're looking for, we're done. */ 6451 if (qualify_lookup (iter->value, flags)) 6452 binding = iter->value; 6453 else if ((flags & LOOKUP_PREFER_TYPES) 6454 && qualify_lookup (iter->type, flags)) 6455 binding = iter->type; 6456 else 6457 binding = NULL_TREE; 6458 6459 if (binding) 6460 { 6461 if (TREE_CODE (binding) == TYPE_DECL && DECL_HIDDEN_P (binding)) 6462 { 6463 /* A non namespace-scope binding can only be hidden in the 6464 presence of a local class, due to friend declarations. 6465 6466 In particular, consider: 6467 6468 struct C; 6469 void f() { 6470 struct A { 6471 friend struct B; 6472 friend struct C; 6473 void g() { 6474 B* b; // error: B is hidden 6475 C* c; // OK, finds ::C 6476 } 6477 }; 6478 B *b; // error: B is hidden 6479 C *c; // OK, finds ::C 6480 struct B {}; 6481 B *bb; // OK 6482 } 6483 6484 The standard says that "B" is a local class in "f" 6485 (but not nested within "A") -- but that name lookup 6486 for "B" does not find this declaration until it is 6487 declared directly with "f". 6488 6489 In particular: 6490 6491 [class.friend] 6492 6493 If a friend declaration appears in a local class and 6494 the name specified is an unqualified name, a prior 6495 declaration is looked up without considering scopes 6496 that are outside the innermost enclosing non-class 6497 scope. For a friend function declaration, if there is 6498 no prior declaration, the program is ill-formed. For a 6499 friend class declaration, if there is no prior 6500 declaration, the class that is specified belongs to the 6501 innermost enclosing non-class scope, but if it is 6502 subsequently referenced, its name is not found by name 6503 lookup until a matching declaration is provided in the 6504 innermost enclosing nonclass scope. 6505 6506 So just keep looking for a non-hidden binding. 6507 */ 6508 gcc_assert (TREE_CODE (binding) == TYPE_DECL); 6509 continue; 6510 } 6511 val = binding; 6512 break; 6513 } 6514 } 6515 6516 /* Now lookup in namespace scopes. */ 6517 if (!val) 6518 { 6519 name_lookup lookup (name, flags); 6520 if (lookup.search_unqualified 6521 (current_decl_namespace (), current_binding_level)) 6522 val = lookup.value; 6523 } 6524 6525 /* If we have a single function from a using decl, pull it out. */ 6526 if (val && TREE_CODE (val) == OVERLOAD && !really_overloaded_fn (val)) 6527 val = OVL_FUNCTION (val); 6528 6529 return val; 6530 } 6531 6532 /* Wrapper for lookup_name_real_1. */ 6533 6534 tree 6535 lookup_name_real (tree name, int prefer_type, int nonclass, bool block_p, 6536 int namespaces_only, int flags) 6537 { 6538 tree ret; 6539 bool subtime = timevar_cond_start (TV_NAME_LOOKUP); 6540 ret = lookup_name_real_1 (name, prefer_type, nonclass, block_p, 6541 namespaces_only, flags); 6542 timevar_cond_stop (TV_NAME_LOOKUP, subtime); 6543 return ret; 6544 } 6545 6546 tree 6547 lookup_name_nonclass (tree name) 6548 { 6549 return lookup_name_real (name, 0, 1, /*block_p=*/true, 0, 0); 6550 } 6551 6552 tree 6553 lookup_name (tree name) 6554 { 6555 return lookup_name_real (name, 0, 0, /*block_p=*/true, 0, 0); 6556 } 6557 6558 tree 6559 lookup_name_prefer_type (tree name, int prefer_type) 6560 { 6561 return lookup_name_real (name, prefer_type, 0, /*block_p=*/true, 0, 0); 6562 } 6563 6564 /* Look up NAME for type used in elaborated name specifier in 6565 the scopes given by SCOPE. SCOPE can be either TS_CURRENT or 6566 TS_WITHIN_ENCLOSING_NON_CLASS. Although not implied by the 6567 name, more scopes are checked if cleanup or template parameter 6568 scope is encountered. 6569 6570 Unlike lookup_name_real, we make sure that NAME is actually 6571 declared in the desired scope, not from inheritance, nor using 6572 directive. For using declaration, there is DR138 still waiting 6573 to be resolved. Hidden name coming from an earlier friend 6574 declaration is also returned. 6575 6576 A TYPE_DECL best matching the NAME is returned. Catching error 6577 and issuing diagnostics are caller's responsibility. */ 6578 6579 static tree 6580 lookup_type_scope_1 (tree name, tag_scope scope) 6581 { 6582 cxx_binding *iter = NULL; 6583 tree val = NULL_TREE; 6584 cp_binding_level *level = NULL; 6585 6586 /* Look in non-namespace scope first. */ 6587 if (current_binding_level->kind != sk_namespace) 6588 iter = outer_binding (name, NULL, /*class_p=*/ true); 6589 for (; iter; iter = outer_binding (name, iter, /*class_p=*/ true)) 6590 { 6591 /* Check if this is the kind of thing we're looking for. 6592 If SCOPE is TS_CURRENT, also make sure it doesn't come from 6593 base class. For ITER->VALUE, we can simply use 6594 INHERITED_VALUE_BINDING_P. For ITER->TYPE, we have to use 6595 our own check. 6596 6597 We check ITER->TYPE before ITER->VALUE in order to handle 6598 typedef struct C {} C; 6599 correctly. */ 6600 6601 if (qualify_lookup (iter->type, LOOKUP_PREFER_TYPES) 6602 && (scope != ts_current 6603 || LOCAL_BINDING_P (iter) 6604 || DECL_CONTEXT (iter->type) == iter->scope->this_entity)) 6605 val = iter->type; 6606 else if ((scope != ts_current 6607 || !INHERITED_VALUE_BINDING_P (iter)) 6608 && qualify_lookup (iter->value, LOOKUP_PREFER_TYPES)) 6609 val = iter->value; 6610 6611 if (val) 6612 break; 6613 } 6614 6615 /* Look in namespace scope. */ 6616 if (val) 6617 level = iter->scope; 6618 else 6619 { 6620 tree ns = current_decl_namespace (); 6621 6622 if (tree *slot = find_namespace_slot (ns, name)) 6623 { 6624 /* If this is the kind of thing we're looking for, we're done. */ 6625 if (tree type = MAYBE_STAT_TYPE (*slot)) 6626 if (qualify_lookup (type, LOOKUP_PREFER_TYPES)) 6627 val = type; 6628 if (!val) 6629 { 6630 if (tree decl = MAYBE_STAT_DECL (*slot)) 6631 if (qualify_lookup (decl, LOOKUP_PREFER_TYPES)) 6632 val = decl; 6633 } 6634 level = NAMESPACE_LEVEL (ns); 6635 } 6636 } 6637 6638 /* Type found, check if it is in the allowed scopes, ignoring cleanup 6639 and template parameter scopes. */ 6640 if (val) 6641 { 6642 cp_binding_level *b = current_binding_level; 6643 while (b) 6644 { 6645 if (level == b) 6646 return val; 6647 6648 if (b->kind == sk_cleanup || b->kind == sk_template_parms 6649 || b->kind == sk_function_parms) 6650 b = b->level_chain; 6651 else if (b->kind == sk_class 6652 && scope == ts_within_enclosing_non_class) 6653 b = b->level_chain; 6654 else 6655 break; 6656 } 6657 } 6658 6659 return NULL_TREE; 6660 } 6661 6662 /* Wrapper for lookup_type_scope_1. */ 6663 6664 tree 6665 lookup_type_scope (tree name, tag_scope scope) 6666 { 6667 tree ret; 6668 bool subtime = timevar_cond_start (TV_NAME_LOOKUP); 6669 ret = lookup_type_scope_1 (name, scope); 6670 timevar_cond_stop (TV_NAME_LOOKUP, subtime); 6671 return ret; 6672 } 6673 6674 /* Returns true iff DECL is a block-scope extern declaration of a function 6675 or variable. */ 6676 6677 bool 6678 is_local_extern (tree decl) 6679 { 6680 cxx_binding *binding; 6681 6682 /* For functions, this is easy. */ 6683 if (TREE_CODE (decl) == FUNCTION_DECL) 6684 return DECL_LOCAL_FUNCTION_P (decl); 6685 6686 if (!VAR_P (decl)) 6687 return false; 6688 if (!current_function_decl) 6689 return false; 6690 6691 /* For variables, this is not easy. We need to look at the binding stack 6692 for the identifier to see whether the decl we have is a local. */ 6693 for (binding = IDENTIFIER_BINDING (DECL_NAME (decl)); 6694 binding && binding->scope->kind != sk_namespace; 6695 binding = binding->previous) 6696 if (binding->value == decl) 6697 return LOCAL_BINDING_P (binding); 6698 6699 return false; 6700 } 6701 6702 /* The type TYPE is being declared. If it is a class template, or a 6703 specialization of a class template, do any processing required and 6704 perform error-checking. If IS_FRIEND is nonzero, this TYPE is 6705 being declared a friend. B is the binding level at which this TYPE 6706 should be bound. 6707 6708 Returns the TYPE_DECL for TYPE, which may have been altered by this 6709 processing. */ 6710 6711 static tree 6712 maybe_process_template_type_declaration (tree type, int is_friend, 6713 cp_binding_level *b) 6714 { 6715 tree decl = TYPE_NAME (type); 6716 6717 if (processing_template_parmlist) 6718 /* You can't declare a new template type in a template parameter 6719 list. But, you can declare a non-template type: 6720 6721 template <class A*> struct S; 6722 6723 is a forward-declaration of `A'. */ 6724 ; 6725 else if (b->kind == sk_namespace 6726 && current_binding_level->kind != sk_namespace) 6727 /* If this new type is being injected into a containing scope, 6728 then it's not a template type. */ 6729 ; 6730 else 6731 { 6732 gcc_assert (MAYBE_CLASS_TYPE_P (type) 6733 || TREE_CODE (type) == ENUMERAL_TYPE); 6734 6735 if (processing_template_decl) 6736 { 6737 /* This may change after the call to 6738 push_template_decl_real, but we want the original value. */ 6739 tree name = DECL_NAME (decl); 6740 6741 decl = push_template_decl_real (decl, is_friend); 6742 if (decl == error_mark_node) 6743 return error_mark_node; 6744 6745 /* If the current binding level is the binding level for the 6746 template parameters (see the comment in 6747 begin_template_parm_list) and the enclosing level is a class 6748 scope, and we're not looking at a friend, push the 6749 declaration of the member class into the class scope. In the 6750 friend case, push_template_decl will already have put the 6751 friend into global scope, if appropriate. */ 6752 if (TREE_CODE (type) != ENUMERAL_TYPE 6753 && !is_friend && b->kind == sk_template_parms 6754 && b->level_chain->kind == sk_class) 6755 { 6756 finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type)); 6757 6758 if (!COMPLETE_TYPE_P (current_class_type)) 6759 { 6760 maybe_add_class_template_decl_list (current_class_type, 6761 type, /*friend_p=*/0); 6762 /* Put this UTD in the table of UTDs for the class. */ 6763 if (CLASSTYPE_NESTED_UTDS (current_class_type) == NULL) 6764 CLASSTYPE_NESTED_UTDS (current_class_type) = 6765 binding_table_new (SCOPE_DEFAULT_HT_SIZE); 6766 6767 binding_table_insert 6768 (CLASSTYPE_NESTED_UTDS (current_class_type), name, type); 6769 } 6770 } 6771 } 6772 } 6773 6774 return decl; 6775 } 6776 6777 /* Push a tag name NAME for struct/class/union/enum type TYPE. In case 6778 that the NAME is a class template, the tag is processed but not pushed. 6779 6780 The pushed scope depend on the SCOPE parameter: 6781 - When SCOPE is TS_CURRENT, put it into the inner-most non-sk_cleanup 6782 scope. 6783 - When SCOPE is TS_GLOBAL, put it in the inner-most non-class and 6784 non-template-parameter scope. This case is needed for forward 6785 declarations. 6786 - When SCOPE is TS_WITHIN_ENCLOSING_NON_CLASS, this is similar to 6787 TS_GLOBAL case except that names within template-parameter scopes 6788 are not pushed at all. 6789 6790 Returns TYPE upon success and ERROR_MARK_NODE otherwise. */ 6791 6792 static tree 6793 do_pushtag (tree name, tree type, tag_scope scope) 6794 { 6795 tree decl; 6796 6797 cp_binding_level *b = current_binding_level; 6798 while (true) 6799 { 6800 if (/* Cleanup scopes are not scopes from the point of view of 6801 the language. */ 6802 b->kind == sk_cleanup 6803 /* Neither are function parameter scopes. */ 6804 || b->kind == sk_function_parms 6805 /* Neither are the scopes used to hold template parameters 6806 for an explicit specialization. For an ordinary template 6807 declaration, these scopes are not scopes from the point of 6808 view of the language. */ 6809 || (b->kind == sk_template_parms 6810 && (b->explicit_spec_p || scope == ts_global))) 6811 b = b->level_chain; 6812 else if (b->kind == sk_class 6813 && scope != ts_current) 6814 { 6815 b = b->level_chain; 6816 if (b->kind == sk_template_parms) 6817 b = b->level_chain; 6818 } 6819 else 6820 break; 6821 } 6822 6823 gcc_assert (identifier_p (name)); 6824 6825 /* Do C++ gratuitous typedefing. */ 6826 if (identifier_type_value_1 (name) != type) 6827 { 6828 tree tdef; 6829 int in_class = 0; 6830 tree context = TYPE_CONTEXT (type); 6831 6832 if (! context) 6833 { 6834 cp_binding_level *cb = b; 6835 while (cb->kind != sk_namespace 6836 && cb->kind != sk_class 6837 && (cb->kind != sk_function_parms 6838 || !cb->this_entity)) 6839 cb = cb->level_chain; 6840 tree cs = cb->this_entity; 6841 6842 gcc_checking_assert (TREE_CODE (cs) == FUNCTION_DECL 6843 ? cs == current_function_decl 6844 : TYPE_P (cs) ? cs == current_class_type 6845 : cs == current_namespace); 6846 6847 if (scope == ts_current 6848 || (cs && TREE_CODE (cs) == FUNCTION_DECL)) 6849 context = cs; 6850 else if (cs && TYPE_P (cs)) 6851 /* When declaring a friend class of a local class, we want 6852 to inject the newly named class into the scope 6853 containing the local class, not the namespace 6854 scope. */ 6855 context = decl_function_context (get_type_decl (cs)); 6856 } 6857 if (!context) 6858 context = current_namespace; 6859 6860 if (b->kind == sk_class 6861 || (b->kind == sk_template_parms 6862 && b->level_chain->kind == sk_class)) 6863 in_class = 1; 6864 6865 tdef = create_implicit_typedef (name, type); 6866 DECL_CONTEXT (tdef) = FROB_CONTEXT (context); 6867 if (scope == ts_within_enclosing_non_class) 6868 { 6869 /* This is a friend. Make this TYPE_DECL node hidden from 6870 ordinary name lookup. Its corresponding TEMPLATE_DECL 6871 will be marked in push_template_decl_real. */ 6872 retrofit_lang_decl (tdef); 6873 DECL_ANTICIPATED (tdef) = 1; 6874 DECL_FRIEND_P (tdef) = 1; 6875 } 6876 6877 decl = maybe_process_template_type_declaration 6878 (type, scope == ts_within_enclosing_non_class, b); 6879 if (decl == error_mark_node) 6880 return decl; 6881 6882 if (b->kind == sk_class) 6883 { 6884 if (!TYPE_BEING_DEFINED (current_class_type)) 6885 /* Don't push anywhere if the class is complete; a lambda in an 6886 NSDMI is not a member of the class. */ 6887 ; 6888 else if (!PROCESSING_REAL_TEMPLATE_DECL_P ()) 6889 /* Put this TYPE_DECL on the TYPE_FIELDS list for the 6890 class. But if it's a member template class, we want 6891 the TEMPLATE_DECL, not the TYPE_DECL, so this is done 6892 later. */ 6893 finish_member_declaration (decl); 6894 else 6895 pushdecl_class_level (decl); 6896 } 6897 else if (b->kind != sk_template_parms) 6898 { 6899 decl = do_pushdecl_with_scope (decl, b, /*is_friend=*/false); 6900 if (decl == error_mark_node) 6901 return decl; 6902 6903 if (DECL_CONTEXT (decl) == std_node 6904 && init_list_identifier == DECL_NAME (TYPE_NAME (type)) 6905 && !CLASSTYPE_TEMPLATE_INFO (type)) 6906 { 6907 error ("declaration of %<std::initializer_list%> does not match " 6908 "%<#include <initializer_list>%>, isn%'t a template"); 6909 return error_mark_node; 6910 } 6911 } 6912 6913 if (! in_class) 6914 set_identifier_type_value_with_scope (name, tdef, b); 6915 6916 TYPE_CONTEXT (type) = DECL_CONTEXT (decl); 6917 6918 /* If this is a local class, keep track of it. We need this 6919 information for name-mangling, and so that it is possible to 6920 find all function definitions in a translation unit in a 6921 convenient way. (It's otherwise tricky to find a member 6922 function definition it's only pointed to from within a local 6923 class.) */ 6924 if (TYPE_FUNCTION_SCOPE_P (type)) 6925 { 6926 if (processing_template_decl) 6927 { 6928 /* Push a DECL_EXPR so we call pushtag at the right time in 6929 template instantiation rather than in some nested context. */ 6930 add_decl_expr (decl); 6931 } 6932 /* Lambdas use LAMBDA_EXPR_DISCRIMINATOR instead. */ 6933 else if (!LAMBDA_TYPE_P (type)) 6934 determine_local_discriminator (TYPE_NAME (type)); 6935 } 6936 } 6937 6938 if (b->kind == sk_class 6939 && !COMPLETE_TYPE_P (current_class_type)) 6940 { 6941 maybe_add_class_template_decl_list (current_class_type, 6942 type, /*friend_p=*/0); 6943 6944 if (CLASSTYPE_NESTED_UTDS (current_class_type) == NULL) 6945 CLASSTYPE_NESTED_UTDS (current_class_type) 6946 = binding_table_new (SCOPE_DEFAULT_HT_SIZE); 6947 6948 binding_table_insert 6949 (CLASSTYPE_NESTED_UTDS (current_class_type), name, type); 6950 } 6951 6952 decl = TYPE_NAME (type); 6953 gcc_assert (TREE_CODE (decl) == TYPE_DECL); 6954 6955 /* Set type visibility now if this is a forward declaration. */ 6956 TREE_PUBLIC (decl) = 1; 6957 determine_visibility (decl); 6958 6959 return type; 6960 } 6961 6962 /* Wrapper for do_pushtag. */ 6963 6964 tree 6965 pushtag (tree name, tree type, tag_scope scope) 6966 { 6967 tree ret; 6968 bool subtime = timevar_cond_start (TV_NAME_LOOKUP); 6969 ret = do_pushtag (name, type, scope); 6970 timevar_cond_stop (TV_NAME_LOOKUP, subtime); 6971 return ret; 6972 } 6973 6974 6975 /* Subroutines for reverting temporarily to top-level for instantiation 6976 of templates and such. We actually need to clear out the class- and 6977 local-value slots of all identifiers, so that only the global values 6978 are at all visible. Simply setting current_binding_level to the global 6979 scope isn't enough, because more binding levels may be pushed. */ 6980 struct saved_scope *scope_chain; 6981 6982 /* Return true if ID has not already been marked. */ 6983 6984 static inline bool 6985 store_binding_p (tree id) 6986 { 6987 if (!id || !IDENTIFIER_BINDING (id)) 6988 return false; 6989 6990 if (IDENTIFIER_MARKED (id)) 6991 return false; 6992 6993 return true; 6994 } 6995 6996 /* Add an appropriate binding to *OLD_BINDINGS which needs to already 6997 have enough space reserved. */ 6998 6999 static void 7000 store_binding (tree id, vec<cxx_saved_binding, va_gc> **old_bindings) 7001 { 7002 cxx_saved_binding saved; 7003 7004 gcc_checking_assert (store_binding_p (id)); 7005 7006 IDENTIFIER_MARKED (id) = 1; 7007 7008 saved.identifier = id; 7009 saved.binding = IDENTIFIER_BINDING (id); 7010 saved.real_type_value = REAL_IDENTIFIER_TYPE_VALUE (id); 7011 (*old_bindings)->quick_push (saved); 7012 IDENTIFIER_BINDING (id) = NULL; 7013 } 7014 7015 static void 7016 store_bindings (tree names, vec<cxx_saved_binding, va_gc> **old_bindings) 7017 { 7018 static vec<tree> bindings_need_stored; 7019 tree t, id; 7020 size_t i; 7021 7022 bool subtime = timevar_cond_start (TV_NAME_LOOKUP); 7023 for (t = names; t; t = TREE_CHAIN (t)) 7024 { 7025 if (TREE_CODE (t) == TREE_LIST) 7026 id = TREE_PURPOSE (t); 7027 else 7028 id = DECL_NAME (t); 7029 7030 if (store_binding_p (id)) 7031 bindings_need_stored.safe_push (id); 7032 } 7033 if (!bindings_need_stored.is_empty ()) 7034 { 7035 vec_safe_reserve_exact (*old_bindings, bindings_need_stored.length ()); 7036 for (i = 0; bindings_need_stored.iterate (i, &id); ++i) 7037 { 7038 /* We can apparently have duplicates in NAMES. */ 7039 if (store_binding_p (id)) 7040 store_binding (id, old_bindings); 7041 } 7042 bindings_need_stored.truncate (0); 7043 } 7044 timevar_cond_stop (TV_NAME_LOOKUP, subtime); 7045 } 7046 7047 /* Like store_bindings, but NAMES is a vector of cp_class_binding 7048 objects, rather than a TREE_LIST. */ 7049 7050 static void 7051 store_class_bindings (vec<cp_class_binding, va_gc> *names, 7052 vec<cxx_saved_binding, va_gc> **old_bindings) 7053 { 7054 static vec<tree> bindings_need_stored; 7055 size_t i; 7056 cp_class_binding *cb; 7057 7058 for (i = 0; vec_safe_iterate (names, i, &cb); ++i) 7059 if (store_binding_p (cb->identifier)) 7060 bindings_need_stored.safe_push (cb->identifier); 7061 if (!bindings_need_stored.is_empty ()) 7062 { 7063 tree id; 7064 vec_safe_reserve_exact (*old_bindings, bindings_need_stored.length ()); 7065 for (i = 0; bindings_need_stored.iterate (i, &id); ++i) 7066 store_binding (id, old_bindings); 7067 bindings_need_stored.truncate (0); 7068 } 7069 } 7070 7071 /* A chain of saved_scope structures awaiting reuse. */ 7072 7073 static GTY((deletable)) struct saved_scope *free_saved_scope; 7074 7075 static void 7076 do_push_to_top_level (void) 7077 { 7078 struct saved_scope *s; 7079 cp_binding_level *b; 7080 cxx_saved_binding *sb; 7081 size_t i; 7082 bool need_pop; 7083 7084 /* Reuse or create a new structure for this saved scope. */ 7085 if (free_saved_scope != NULL) 7086 { 7087 s = free_saved_scope; 7088 free_saved_scope = s->prev; 7089 7090 vec<cxx_saved_binding, va_gc> *old_bindings = s->old_bindings; 7091 memset (s, 0, sizeof (*s)); 7092 /* Also reuse the structure's old_bindings vector. */ 7093 vec_safe_truncate (old_bindings, 0); 7094 s->old_bindings = old_bindings; 7095 } 7096 else 7097 s = ggc_cleared_alloc<saved_scope> (); 7098 7099 b = scope_chain ? current_binding_level : 0; 7100 7101 /* If we're in the middle of some function, save our state. */ 7102 if (cfun) 7103 { 7104 need_pop = true; 7105 push_function_context (); 7106 } 7107 else 7108 need_pop = false; 7109 7110 if (scope_chain && previous_class_level) 7111 store_class_bindings (previous_class_level->class_shadowed, 7112 &s->old_bindings); 7113 7114 /* Have to include the global scope, because class-scope decls 7115 aren't listed anywhere useful. */ 7116 for (; b; b = b->level_chain) 7117 { 7118 tree t; 7119 7120 /* Template IDs are inserted into the global level. If they were 7121 inserted into namespace level, finish_file wouldn't find them 7122 when doing pending instantiations. Therefore, don't stop at 7123 namespace level, but continue until :: . */ 7124 if (global_scope_p (b)) 7125 break; 7126 7127 store_bindings (b->names, &s->old_bindings); 7128 /* We also need to check class_shadowed to save class-level type 7129 bindings, since pushclass doesn't fill in b->names. */ 7130 if (b->kind == sk_class) 7131 store_class_bindings (b->class_shadowed, &s->old_bindings); 7132 7133 /* Unwind type-value slots back to top level. */ 7134 for (t = b->type_shadowed; t; t = TREE_CHAIN (t)) 7135 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t), TREE_VALUE (t)); 7136 } 7137 7138 FOR_EACH_VEC_SAFE_ELT (s->old_bindings, i, sb) 7139 IDENTIFIER_MARKED (sb->identifier) = 0; 7140 7141 s->prev = scope_chain; 7142 s->bindings = b; 7143 s->need_pop_function_context = need_pop; 7144 s->function_decl = current_function_decl; 7145 s->unevaluated_operand = cp_unevaluated_operand; 7146 s->inhibit_evaluation_warnings = c_inhibit_evaluation_warnings; 7147 s->suppress_location_wrappers = suppress_location_wrappers; 7148 s->x_stmt_tree.stmts_are_full_exprs_p = true; 7149 7150 scope_chain = s; 7151 current_function_decl = NULL_TREE; 7152 current_lang_base = NULL; 7153 current_lang_name = lang_name_cplusplus; 7154 current_namespace = global_namespace; 7155 push_class_stack (); 7156 cp_unevaluated_operand = 0; 7157 c_inhibit_evaluation_warnings = 0; 7158 suppress_location_wrappers = 0; 7159 } 7160 7161 static void 7162 do_pop_from_top_level (void) 7163 { 7164 struct saved_scope *s = scope_chain; 7165 cxx_saved_binding *saved; 7166 size_t i; 7167 7168 /* Clear out class-level bindings cache. */ 7169 if (previous_class_level) 7170 invalidate_class_lookup_cache (); 7171 pop_class_stack (); 7172 7173 release_tree_vector (current_lang_base); 7174 7175 scope_chain = s->prev; 7176 FOR_EACH_VEC_SAFE_ELT (s->old_bindings, i, saved) 7177 { 7178 tree id = saved->identifier; 7179 7180 IDENTIFIER_BINDING (id) = saved->binding; 7181 SET_IDENTIFIER_TYPE_VALUE (id, saved->real_type_value); 7182 } 7183 7184 /* If we were in the middle of compiling a function, restore our 7185 state. */ 7186 if (s->need_pop_function_context) 7187 pop_function_context (); 7188 current_function_decl = s->function_decl; 7189 cp_unevaluated_operand = s->unevaluated_operand; 7190 c_inhibit_evaluation_warnings = s->inhibit_evaluation_warnings; 7191 suppress_location_wrappers = s->suppress_location_wrappers; 7192 7193 /* Make this saved_scope structure available for reuse by 7194 push_to_top_level. */ 7195 s->prev = free_saved_scope; 7196 free_saved_scope = s; 7197 } 7198 7199 /* Push into the scope of the namespace NS, even if it is deeply 7200 nested within another namespace. */ 7201 7202 static void 7203 do_push_nested_namespace (tree ns) 7204 { 7205 if (ns == global_namespace) 7206 do_push_to_top_level (); 7207 else 7208 { 7209 do_push_nested_namespace (CP_DECL_CONTEXT (ns)); 7210 gcc_checking_assert 7211 (find_namespace_value (current_namespace, DECL_NAME (ns)) == ns); 7212 resume_scope (NAMESPACE_LEVEL (ns)); 7213 current_namespace = ns; 7214 } 7215 } 7216 7217 /* Pop back from the scope of the namespace NS, which was previously 7218 entered with push_nested_namespace. */ 7219 7220 static void 7221 do_pop_nested_namespace (tree ns) 7222 { 7223 while (ns != global_namespace) 7224 { 7225 ns = CP_DECL_CONTEXT (ns); 7226 current_namespace = ns; 7227 leave_scope (); 7228 } 7229 7230 do_pop_from_top_level (); 7231 } 7232 7233 /* Add TARGET to USINGS, if it does not already exist there. 7234 We used to build the complete graph of usings at this point, from 7235 the POV of the source namespaces. Now we build that as we perform 7236 the unqualified search. */ 7237 7238 static void 7239 add_using_namespace (vec<tree, va_gc> *&usings, tree target) 7240 { 7241 if (usings) 7242 for (unsigned ix = usings->length (); ix--;) 7243 if ((*usings)[ix] == target) 7244 return; 7245 7246 vec_safe_push (usings, target); 7247 } 7248 7249 /* Tell the debug system of a using directive. */ 7250 7251 static void 7252 emit_debug_info_using_namespace (tree from, tree target, bool implicit) 7253 { 7254 /* Emit debugging info. */ 7255 tree context = from != global_namespace ? from : NULL_TREE; 7256 debug_hooks->imported_module_or_decl (target, NULL_TREE, context, false, 7257 implicit); 7258 } 7259 7260 /* Process a namespace-scope using directive. */ 7261 7262 void 7263 finish_namespace_using_directive (tree target, tree attribs) 7264 { 7265 gcc_checking_assert (namespace_bindings_p ()); 7266 if (target == error_mark_node) 7267 return; 7268 7269 add_using_namespace (DECL_NAMESPACE_USING (current_namespace), 7270 ORIGINAL_NAMESPACE (target)); 7271 emit_debug_info_using_namespace (current_namespace, 7272 ORIGINAL_NAMESPACE (target), false); 7273 7274 if (attribs == error_mark_node) 7275 return; 7276 7277 for (tree a = attribs; a; a = TREE_CHAIN (a)) 7278 { 7279 tree name = get_attribute_name (a); 7280 if (is_attribute_p ("strong", name)) 7281 { 7282 warning (0, "strong using directive no longer supported"); 7283 if (CP_DECL_CONTEXT (target) == current_namespace) 7284 inform (DECL_SOURCE_LOCATION (target), 7285 "you may use an inline namespace instead"); 7286 } 7287 else 7288 warning (OPT_Wattributes, "%qD attribute directive ignored", name); 7289 } 7290 } 7291 7292 /* Process a function-scope using-directive. */ 7293 7294 void 7295 finish_local_using_directive (tree target, tree attribs) 7296 { 7297 gcc_checking_assert (local_bindings_p ()); 7298 if (target == error_mark_node) 7299 return; 7300 7301 if (attribs) 7302 warning (OPT_Wattributes, "attributes ignored on local using directive"); 7303 7304 add_stmt (build_stmt (input_location, USING_STMT, target)); 7305 7306 add_using_namespace (current_binding_level->using_directives, 7307 ORIGINAL_NAMESPACE (target)); 7308 } 7309 7310 /* Pushes X into the global namespace. */ 7311 7312 tree 7313 pushdecl_top_level (tree x, bool is_friend) 7314 { 7315 bool subtime = timevar_cond_start (TV_NAME_LOOKUP); 7316 do_push_to_top_level (); 7317 x = pushdecl_namespace_level (x, is_friend); 7318 do_pop_from_top_level (); 7319 timevar_cond_stop (TV_NAME_LOOKUP, subtime); 7320 return x; 7321 } 7322 7323 /* Pushes X into the global namespace and calls cp_finish_decl to 7324 register the variable, initializing it with INIT. */ 7325 7326 tree 7327 pushdecl_top_level_and_finish (tree x, tree init) 7328 { 7329 bool subtime = timevar_cond_start (TV_NAME_LOOKUP); 7330 do_push_to_top_level (); 7331 x = pushdecl_namespace_level (x, false); 7332 cp_finish_decl (x, init, false, NULL_TREE, 0); 7333 do_pop_from_top_level (); 7334 timevar_cond_stop (TV_NAME_LOOKUP, subtime); 7335 return x; 7336 } 7337 7338 /* Enter the namespaces from current_namerspace to NS. */ 7339 7340 static int 7341 push_inline_namespaces (tree ns) 7342 { 7343 int count = 0; 7344 if (ns != current_namespace) 7345 { 7346 gcc_assert (ns != global_namespace); 7347 count += push_inline_namespaces (CP_DECL_CONTEXT (ns)); 7348 resume_scope (NAMESPACE_LEVEL (ns)); 7349 current_namespace = ns; 7350 count++; 7351 } 7352 return count; 7353 } 7354 7355 /* Push into the scope of the NAME namespace. If NAME is NULL_TREE, 7356 then we enter an anonymous namespace. If MAKE_INLINE is true, then 7357 we create an inline namespace (it is up to the caller to check upon 7358 redefinition). Return the number of namespaces entered. */ 7359 7360 int 7361 push_namespace (tree name, bool make_inline) 7362 { 7363 bool subtime = timevar_cond_start (TV_NAME_LOOKUP); 7364 int count = 0; 7365 7366 /* We should not get here if the global_namespace is not yet constructed 7367 nor if NAME designates the global namespace: The global scope is 7368 constructed elsewhere. */ 7369 gcc_checking_assert (global_namespace != NULL && name != global_identifier); 7370 7371 tree ns = NULL_TREE; 7372 { 7373 name_lookup lookup (name, 0); 7374 if (!lookup.search_qualified (current_namespace, /*usings=*/false)) 7375 ; 7376 else if (TREE_CODE (lookup.value) != NAMESPACE_DECL) 7377 ; 7378 else if (tree dna = DECL_NAMESPACE_ALIAS (lookup.value)) 7379 { 7380 /* A namespace alias is not allowed here, but if the alias 7381 is for a namespace also inside the current scope, 7382 accept it with a diagnostic. That's better than dying 7383 horribly. */ 7384 if (is_nested_namespace (current_namespace, CP_DECL_CONTEXT (dna))) 7385 { 7386 error ("namespace alias %qD not allowed here, " 7387 "assuming %qD", lookup.value, dna); 7388 ns = dna; 7389 } 7390 } 7391 else 7392 ns = lookup.value; 7393 } 7394 7395 bool new_ns = false; 7396 if (ns) 7397 /* DR2061. NS might be a member of an inline namespace. We 7398 need to push into those namespaces. */ 7399 count += push_inline_namespaces (CP_DECL_CONTEXT (ns)); 7400 else 7401 { 7402 ns = build_lang_decl (NAMESPACE_DECL, name, void_type_node); 7403 SCOPE_DEPTH (ns) = SCOPE_DEPTH (current_namespace) + 1; 7404 if (!SCOPE_DEPTH (ns)) 7405 /* We only allow depth 255. */ 7406 sorry ("cannot nest more than %d namespaces", 7407 SCOPE_DEPTH (current_namespace)); 7408 DECL_CONTEXT (ns) = FROB_CONTEXT (current_namespace); 7409 new_ns = true; 7410 7411 if (pushdecl (ns) == error_mark_node) 7412 ns = NULL_TREE; 7413 else 7414 { 7415 if (!name) 7416 { 7417 SET_DECL_ASSEMBLER_NAME (ns, anon_identifier); 7418 7419 if (!make_inline) 7420 add_using_namespace (DECL_NAMESPACE_USING (current_namespace), 7421 ns); 7422 } 7423 else if (TREE_PUBLIC (current_namespace)) 7424 TREE_PUBLIC (ns) = 1; 7425 7426 if (make_inline) 7427 { 7428 DECL_NAMESPACE_INLINE_P (ns) = true; 7429 vec_safe_push (DECL_NAMESPACE_INLINEES (current_namespace), ns); 7430 } 7431 7432 if (!name || make_inline) 7433 emit_debug_info_using_namespace (current_namespace, ns, true); 7434 } 7435 } 7436 7437 if (ns) 7438 { 7439 if (make_inline && !DECL_NAMESPACE_INLINE_P (ns)) 7440 { 7441 error ("inline namespace must be specified at initial definition"); 7442 inform (DECL_SOURCE_LOCATION (ns), "%qD defined here", ns); 7443 } 7444 if (new_ns) 7445 begin_scope (sk_namespace, ns); 7446 else 7447 resume_scope (NAMESPACE_LEVEL (ns)); 7448 current_namespace = ns; 7449 count++; 7450 } 7451 7452 timevar_cond_stop (TV_NAME_LOOKUP, subtime); 7453 return count; 7454 } 7455 7456 /* Pop from the scope of the current namespace. */ 7457 7458 void 7459 pop_namespace (void) 7460 { 7461 bool subtime = timevar_cond_start (TV_NAME_LOOKUP); 7462 7463 gcc_assert (current_namespace != global_namespace); 7464 current_namespace = CP_DECL_CONTEXT (current_namespace); 7465 /* The binding level is not popped, as it might be re-opened later. */ 7466 leave_scope (); 7467 7468 timevar_cond_stop (TV_NAME_LOOKUP, subtime); 7469 } 7470 7471 /* External entry points for do_{push_to/pop_from}_top_level. */ 7472 7473 void 7474 push_to_top_level (void) 7475 { 7476 bool subtime = timevar_cond_start (TV_NAME_LOOKUP); 7477 do_push_to_top_level (); 7478 timevar_cond_stop (TV_NAME_LOOKUP, subtime); 7479 } 7480 7481 void 7482 pop_from_top_level (void) 7483 { 7484 bool subtime = timevar_cond_start (TV_NAME_LOOKUP); 7485 do_pop_from_top_level (); 7486 timevar_cond_stop (TV_NAME_LOOKUP, subtime); 7487 } 7488 7489 /* External entry points for do_{push,pop}_nested_namespace. */ 7490 7491 void 7492 push_nested_namespace (tree ns) 7493 { 7494 bool subtime = timevar_cond_start (TV_NAME_LOOKUP); 7495 do_push_nested_namespace (ns); 7496 timevar_cond_stop (TV_NAME_LOOKUP, subtime); 7497 } 7498 7499 void 7500 pop_nested_namespace (tree ns) 7501 { 7502 bool subtime = timevar_cond_start (TV_NAME_LOOKUP); 7503 gcc_assert (current_namespace == ns); 7504 do_pop_nested_namespace (ns); 7505 timevar_cond_stop (TV_NAME_LOOKUP, subtime); 7506 } 7507 7508 /* Pop off extraneous binding levels left over due to syntax errors. 7509 We don't pop past namespaces, as they might be valid. */ 7510 7511 void 7512 pop_everything (void) 7513 { 7514 if (ENABLE_SCOPE_CHECKING) 7515 verbatim ("XXX entering pop_everything ()\n"); 7516 while (!namespace_bindings_p ()) 7517 { 7518 if (current_binding_level->kind == sk_class) 7519 pop_nested_class (); 7520 else 7521 poplevel (0, 0, 0); 7522 } 7523 if (ENABLE_SCOPE_CHECKING) 7524 verbatim ("XXX leaving pop_everything ()\n"); 7525 } 7526 7527 /* Emit debugging information for using declarations and directives. 7528 If input tree is overloaded fn then emit debug info for all 7529 candidates. */ 7530 7531 void 7532 cp_emit_debug_info_for_using (tree t, tree context) 7533 { 7534 /* Don't try to emit any debug information if we have errors. */ 7535 if (seen_error ()) 7536 return; 7537 7538 /* Ignore this FUNCTION_DECL if it refers to a builtin declaration 7539 of a builtin function. */ 7540 if (TREE_CODE (t) == FUNCTION_DECL 7541 && DECL_EXTERNAL (t) 7542 && fndecl_built_in_p (t)) 7543 return; 7544 7545 /* Do not supply context to imported_module_or_decl, if 7546 it is a global namespace. */ 7547 if (context == global_namespace) 7548 context = NULL_TREE; 7549 7550 t = MAYBE_BASELINK_FUNCTIONS (t); 7551 7552 /* FIXME: Handle TEMPLATE_DECLs. */ 7553 for (lkp_iterator iter (t); iter; ++iter) 7554 { 7555 tree fn = *iter; 7556 if (TREE_CODE (fn) != TEMPLATE_DECL) 7557 { 7558 if (building_stmt_list_p ()) 7559 add_stmt (build_stmt (input_location, USING_STMT, fn)); 7560 else 7561 debug_hooks->imported_module_or_decl (fn, NULL_TREE, context, 7562 false, false); 7563 } 7564 } 7565 } 7566 7567 /* Return the result of unqualified lookup for the overloaded operator 7568 designated by CODE, if we are in a template and the binding we find is 7569 not. */ 7570 7571 static tree 7572 op_unqualified_lookup (tree fnname) 7573 { 7574 if (cxx_binding *binding = IDENTIFIER_BINDING (fnname)) 7575 { 7576 cp_binding_level *l = binding->scope; 7577 while (l && !l->this_entity) 7578 l = l->level_chain; 7579 if (l && uses_template_parms (l->this_entity)) 7580 /* Don't preserve decls from an uninstantiated template, 7581 wait until that template is instantiated. */ 7582 return NULL_TREE; 7583 } 7584 tree fns = lookup_name (fnname); 7585 if (fns && fns == get_global_binding (fnname)) 7586 /* The instantiation can find these. */ 7587 return NULL_TREE; 7588 return fns; 7589 } 7590 7591 /* E is an expression representing an operation with dependent type, so we 7592 don't know yet whether it will use the built-in meaning of the operator or a 7593 function. Remember declarations of that operator in scope. */ 7594 7595 const char *const op_bind_attrname = "operator bindings"; 7596 7597 void 7598 maybe_save_operator_binding (tree e) 7599 { 7600 /* This is only useful in a generic lambda. */ 7601 if (!processing_template_decl) 7602 return; 7603 tree cfn = current_function_decl; 7604 if (!cfn) 7605 return; 7606 7607 /* Let's only do this for generic lambdas for now, we could do it for all 7608 function templates if we wanted to. */ 7609 if (!current_lambda_expr()) 7610 return; 7611 7612 tree fnname = ovl_op_identifier (false, TREE_CODE (e)); 7613 if (!fnname) 7614 return; 7615 7616 tree attributes = DECL_ATTRIBUTES (cfn); 7617 tree attr = lookup_attribute (op_bind_attrname, attributes); 7618 tree bindings = NULL_TREE; 7619 tree fns = NULL_TREE; 7620 if (attr) 7621 { 7622 bindings = TREE_VALUE (attr); 7623 if (tree elt = purpose_member (fnname, bindings)) 7624 fns = TREE_VALUE (elt); 7625 } 7626 7627 if (!fns && (fns = op_unqualified_lookup (fnname))) 7628 { 7629 tree fn = get_first_fn (fns); 7630 if (DECL_CLASS_SCOPE_P (fn)) 7631 /* We don't need to remember class-scope functions, normal unqualified 7632 lookup will find them again. */ 7633 return; 7634 7635 bindings = tree_cons (fnname, fns, bindings); 7636 if (attr) 7637 TREE_VALUE (attr) = bindings; 7638 else 7639 DECL_ATTRIBUTES (cfn) 7640 = tree_cons (get_identifier (op_bind_attrname), 7641 bindings, 7642 attributes); 7643 } 7644 } 7645 7646 /* Called from cp_free_lang_data so we don't put this into LTO. */ 7647 7648 void 7649 discard_operator_bindings (tree decl) 7650 { 7651 DECL_ATTRIBUTES (decl) = remove_attribute (op_bind_attrname, 7652 DECL_ATTRIBUTES (decl)); 7653 } 7654 7655 /* Subroutine of start_preparsed_function: push the bindings we saved away in 7656 maybe_save_op_lookup into the function parameter binding level. */ 7657 7658 void 7659 push_operator_bindings () 7660 { 7661 tree decl1 = current_function_decl; 7662 if (tree attr = lookup_attribute (op_bind_attrname, 7663 DECL_ATTRIBUTES (decl1))) 7664 for (tree binds = TREE_VALUE (attr); binds; binds = TREE_CHAIN (binds)) 7665 { 7666 tree name = TREE_PURPOSE (binds); 7667 tree val = TREE_VALUE (binds); 7668 push_local_binding (name, val, /*using*/true); 7669 } 7670 } 7671 7672 #include "gt-cp-name-lookup.h" 7673