1132718Skan/* Definitions for C++ name lookup routines. 2169689Skan Copyright (C) 2003, 2004, 2005, 2006 Free Software Foundation, Inc. 3132718Skan Contributed by Gabriel Dos Reis <gdr@integrable-solutions.net> 4132718Skan 5132718SkanThis file is part of GCC. 6132718Skan 7132718SkanGCC is free software; you can redistribute it and/or modify 8132718Skanit under the terms of the GNU General Public License as published by 9132718Skanthe Free Software Foundation; either version 2, or (at your option) 10132718Skanany later version. 11132718Skan 12132718SkanGCC is distributed in the hope that it will be useful, 13132718Skanbut WITHOUT ANY WARRANTY; without even the implied warranty of 14132718SkanMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15132718SkanGNU General Public License for more details. 16132718Skan 17132718SkanYou should have received a copy of the GNU General Public License 18132718Skanalong with GCC; see the file COPYING. If not, write to 19169689Skanthe Free Software Foundation, 51 Franklin Street, Fifth Floor, 20169689SkanBoston, MA 02110-1301, USA. */ 21132718Skan 22132718Skan#include "config.h" 23132718Skan#include "system.h" 24132718Skan#include "coretypes.h" 25132718Skan#include "tm.h" 26132718Skan#include "flags.h" 27132718Skan#include "tree.h" 28132718Skan#include "cp-tree.h" 29132718Skan#include "name-lookup.h" 30132718Skan#include "timevar.h" 31132718Skan#include "toplev.h" 32132718Skan#include "diagnostic.h" 33169689Skan#include "debug.h" 34169689Skan#include "c-pragma.h" 35132718Skan 36169689Skan/* The bindings for a particular name in a particular scope. */ 37169689Skan 38169689Skanstruct scope_binding { 39169689Skan tree value; 40169689Skan tree type; 41169689Skan}; 42169689Skan#define EMPTY_SCOPE_BINDING { NULL_TREE, NULL_TREE } 43169689Skan 44132718Skanstatic cxx_scope *innermost_nonclass_level (void); 45169689Skanstatic tree select_decl (const struct scope_binding *, int); 46132718Skanstatic cxx_binding *binding_for_name (cxx_scope *, tree); 47169689Skanstatic tree lookup_name_innermost_nonclass_level (tree); 48169689Skanstatic tree push_overloaded_decl (tree, int, bool); 49169689Skanstatic bool lookup_using_namespace (tree, struct scope_binding *, tree, 50169689Skan tree, int); 51169689Skanstatic bool qualified_lookup_using_namespace (tree, tree, 52169689Skan struct scope_binding *, int); 53132718Skanstatic tree lookup_type_current_level (tree); 54132718Skanstatic tree push_using_directive (tree); 55132718Skan 56132718Skan/* The :: namespace. */ 57132718Skan 58132718Skantree global_namespace; 59132718Skan 60132718Skan/* The name of the anonymous namespace, throughout this translation 61132718Skan unit. */ 62169689Skanstatic GTY(()) tree anonymous_namespace_name; 63132718Skan 64132718Skan 65132718Skan/* Compute the chain index of a binding_entry given the HASH value of its 66132718Skan name and the total COUNT of chains. COUNT is assumed to be a power 67132718Skan of 2. */ 68132718Skan 69132718Skan#define ENTRY_INDEX(HASH, COUNT) (((HASH) >> 3) & ((COUNT) - 1)) 70132718Skan 71132718Skan/* A free list of "binding_entry"s awaiting for re-use. */ 72132718Skan 73169689Skanstatic GTY((deletable)) binding_entry free_binding_entry = NULL; 74132718Skan 75132718Skan/* Create a binding_entry object for (NAME, TYPE). */ 76132718Skan 77132718Skanstatic inline binding_entry 78132718Skanbinding_entry_make (tree name, tree type) 79132718Skan{ 80132718Skan binding_entry entry; 81132718Skan 82132718Skan if (free_binding_entry) 83132718Skan { 84132718Skan entry = free_binding_entry; 85132718Skan free_binding_entry = entry->chain; 86132718Skan } 87132718Skan else 88169689Skan entry = GGC_NEW (struct binding_entry_s); 89132718Skan 90132718Skan entry->name = name; 91132718Skan entry->type = type; 92132718Skan entry->chain = NULL; 93132718Skan 94132718Skan return entry; 95132718Skan} 96132718Skan 97132718Skan/* Put ENTRY back on the free list. */ 98169689Skan#if 0 99132718Skanstatic inline void 100132718Skanbinding_entry_free (binding_entry entry) 101132718Skan{ 102132718Skan entry->name = NULL; 103132718Skan entry->type = NULL; 104132718Skan entry->chain = free_binding_entry; 105132718Skan free_binding_entry = entry; 106132718Skan} 107169689Skan#endif 108132718Skan 109132718Skan/* The datatype used to implement the mapping from names to types at 110132718Skan a given scope. */ 111132718Skanstruct binding_table_s GTY(()) 112132718Skan{ 113132718Skan /* Array of chains of "binding_entry"s */ 114132718Skan binding_entry * GTY((length ("%h.chain_count"))) chain; 115132718Skan 116132718Skan /* The number of chains in this table. This is the length of the 117132718Skan the member "chain" considered as an array. */ 118132718Skan size_t chain_count; 119132718Skan 120132718Skan /* Number of "binding_entry"s in this table. */ 121132718Skan size_t entry_count; 122132718Skan}; 123132718Skan 124132718Skan/* Construct TABLE with an initial CHAIN_COUNT. */ 125132718Skan 126132718Skanstatic inline void 127132718Skanbinding_table_construct (binding_table table, size_t chain_count) 128132718Skan{ 129132718Skan table->chain_count = chain_count; 130132718Skan table->entry_count = 0; 131169689Skan table->chain = GGC_CNEWVEC (binding_entry, table->chain_count); 132132718Skan} 133132718Skan 134132718Skan/* Make TABLE's entries ready for reuse. */ 135169689Skan#if 0 136132718Skanstatic void 137132718Skanbinding_table_free (binding_table table) 138132718Skan{ 139132718Skan size_t i; 140132718Skan size_t count; 141132718Skan 142132718Skan if (table == NULL) 143132718Skan return; 144132718Skan 145132718Skan for (i = 0, count = table->chain_count; i < count; ++i) 146132718Skan { 147132718Skan binding_entry temp = table->chain[i]; 148132718Skan while (temp != NULL) 149169689Skan { 150169689Skan binding_entry entry = temp; 151169689Skan temp = entry->chain; 152169689Skan binding_entry_free (entry); 153169689Skan } 154132718Skan table->chain[i] = NULL; 155132718Skan } 156132718Skan table->entry_count = 0; 157132718Skan} 158169689Skan#endif 159132718Skan 160132718Skan/* Allocate a table with CHAIN_COUNT, assumed to be a power of two. */ 161132718Skan 162132718Skanstatic inline binding_table 163132718Skanbinding_table_new (size_t chain_count) 164132718Skan{ 165169689Skan binding_table table = GGC_NEW (struct binding_table_s); 166132718Skan table->chain = NULL; 167132718Skan binding_table_construct (table, chain_count); 168132718Skan return table; 169132718Skan} 170132718Skan 171132718Skan/* Expand TABLE to twice its current chain_count. */ 172132718Skan 173132718Skanstatic void 174132718Skanbinding_table_expand (binding_table table) 175132718Skan{ 176132718Skan const size_t old_chain_count = table->chain_count; 177132718Skan const size_t old_entry_count = table->entry_count; 178132718Skan const size_t new_chain_count = 2 * old_chain_count; 179132718Skan binding_entry *old_chains = table->chain; 180132718Skan size_t i; 181132718Skan 182132718Skan binding_table_construct (table, new_chain_count); 183132718Skan for (i = 0; i < old_chain_count; ++i) 184132718Skan { 185132718Skan binding_entry entry = old_chains[i]; 186132718Skan for (; entry != NULL; entry = old_chains[i]) 187169689Skan { 188169689Skan const unsigned int hash = IDENTIFIER_HASH_VALUE (entry->name); 189169689Skan const size_t j = ENTRY_INDEX (hash, new_chain_count); 190132718Skan 191169689Skan old_chains[i] = entry->chain; 192169689Skan entry->chain = table->chain[j]; 193169689Skan table->chain[j] = entry; 194169689Skan } 195132718Skan } 196132718Skan table->entry_count = old_entry_count; 197132718Skan} 198132718Skan 199132718Skan/* Insert a binding for NAME to TYPE into TABLE. */ 200132718Skan 201132718Skanstatic void 202132718Skanbinding_table_insert (binding_table table, tree name, tree type) 203132718Skan{ 204132718Skan const unsigned int hash = IDENTIFIER_HASH_VALUE (name); 205132718Skan const size_t i = ENTRY_INDEX (hash, table->chain_count); 206132718Skan binding_entry entry = binding_entry_make (name, type); 207132718Skan 208132718Skan entry->chain = table->chain[i]; 209132718Skan table->chain[i] = entry; 210132718Skan ++table->entry_count; 211132718Skan 212132718Skan if (3 * table->chain_count < 5 * table->entry_count) 213132718Skan binding_table_expand (table); 214132718Skan} 215132718Skan 216132718Skan/* Return the binding_entry, if any, that maps NAME. */ 217132718Skan 218132718Skanbinding_entry 219132718Skanbinding_table_find (binding_table table, tree name) 220132718Skan{ 221132718Skan const unsigned int hash = IDENTIFIER_HASH_VALUE (name); 222132718Skan binding_entry entry = table->chain[ENTRY_INDEX (hash, table->chain_count)]; 223132718Skan 224132718Skan while (entry != NULL && entry->name != name) 225132718Skan entry = entry->chain; 226132718Skan 227132718Skan return entry; 228132718Skan} 229132718Skan 230132718Skan/* Apply PROC -- with DATA -- to all entries in TABLE. */ 231132718Skan 232132718Skanvoid 233132718Skanbinding_table_foreach (binding_table table, bt_foreach_proc proc, void *data) 234132718Skan{ 235132718Skan const size_t chain_count = table->chain_count; 236132718Skan size_t i; 237132718Skan 238132718Skan for (i = 0; i < chain_count; ++i) 239132718Skan { 240132718Skan binding_entry entry = table->chain[i]; 241132718Skan for (; entry != NULL; entry = entry->chain) 242169689Skan proc (entry, data); 243132718Skan } 244132718Skan} 245132718Skan 246132718Skan#ifndef ENABLE_SCOPE_CHECKING 247132718Skan# define ENABLE_SCOPE_CHECKING 0 248132718Skan#else 249132718Skan# define ENABLE_SCOPE_CHECKING 1 250132718Skan#endif 251132718Skan 252132718Skan/* A free list of "cxx_binding"s, connected by their PREVIOUS. */ 253132718Skan 254169689Skanstatic GTY((deletable)) cxx_binding *free_bindings; 255132718Skan 256169689Skan/* Initialize VALUE and TYPE field for BINDING, and set the PREVIOUS 257169689Skan field to NULL. */ 258132718Skan 259169689Skanstatic inline void 260169689Skancxx_binding_init (cxx_binding *binding, tree value, tree type) 261169689Skan{ 262169689Skan binding->value = value; 263169689Skan binding->type = type; 264169689Skan binding->previous = NULL; 265169689Skan} 266169689Skan 267132718Skan/* (GC)-allocate a binding object with VALUE and TYPE member initialized. */ 268132718Skan 269132718Skanstatic cxx_binding * 270132718Skancxx_binding_make (tree value, tree type) 271132718Skan{ 272132718Skan cxx_binding *binding; 273132718Skan if (free_bindings) 274132718Skan { 275132718Skan binding = free_bindings; 276132718Skan free_bindings = binding->previous; 277132718Skan } 278132718Skan else 279169689Skan binding = GGC_NEW (cxx_binding); 280132718Skan 281169689Skan cxx_binding_init (binding, value, type); 282132718Skan 283132718Skan return binding; 284132718Skan} 285132718Skan 286132718Skan/* Put BINDING back on the free list. */ 287132718Skan 288132718Skanstatic inline void 289132718Skancxx_binding_free (cxx_binding *binding) 290132718Skan{ 291132718Skan binding->scope = NULL; 292132718Skan binding->previous = free_bindings; 293132718Skan free_bindings = binding; 294132718Skan} 295132718Skan 296169689Skan/* Create a new binding for NAME (with the indicated VALUE and TYPE 297169689Skan bindings) in the class scope indicated by SCOPE. */ 298169689Skan 299169689Skanstatic cxx_binding * 300169689Skannew_class_binding (tree name, tree value, tree type, cxx_scope *scope) 301169689Skan{ 302169689Skan cp_class_binding *cb; 303169689Skan cxx_binding *binding; 304169689Skan 305169689Skan if (VEC_length (cp_class_binding, scope->class_shadowed)) 306169689Skan { 307169689Skan cp_class_binding *old_base; 308169689Skan old_base = VEC_index (cp_class_binding, scope->class_shadowed, 0); 309169689Skan if (VEC_reserve (cp_class_binding, gc, scope->class_shadowed, 1)) 310169689Skan { 311169689Skan /* Fixup the current bindings, as they might have moved. */ 312169689Skan size_t i; 313169689Skan 314169689Skan for (i = 0; 315169689Skan VEC_iterate (cp_class_binding, scope->class_shadowed, i, cb); 316169689Skan i++) 317169689Skan { 318169689Skan cxx_binding **b; 319169689Skan b = &IDENTIFIER_BINDING (cb->identifier); 320169689Skan while (*b != &old_base[i].base) 321169689Skan b = &((*b)->previous); 322169689Skan *b = &cb->base; 323169689Skan } 324169689Skan } 325169689Skan cb = VEC_quick_push (cp_class_binding, scope->class_shadowed, NULL); 326169689Skan } 327169689Skan else 328169689Skan cb = VEC_safe_push (cp_class_binding, gc, scope->class_shadowed, NULL); 329169689Skan 330169689Skan cb->identifier = name; 331169689Skan binding = &cb->base; 332169689Skan binding->scope = scope; 333169689Skan cxx_binding_init (binding, value, type); 334169689Skan return binding; 335169689Skan} 336169689Skan 337132718Skan/* Make DECL the innermost binding for ID. The LEVEL is the binding 338132718Skan level at which this declaration is being bound. */ 339132718Skan 340132718Skanstatic void 341132718Skanpush_binding (tree id, tree decl, cxx_scope* level) 342132718Skan{ 343169689Skan cxx_binding *binding; 344132718Skan 345169689Skan if (level != class_binding_level) 346169689Skan { 347169689Skan binding = cxx_binding_make (decl, NULL_TREE); 348169689Skan binding->scope = level; 349169689Skan } 350169689Skan else 351169689Skan binding = new_class_binding (id, decl, /*type=*/NULL_TREE, level); 352169689Skan 353132718Skan /* Now, fill in the binding information. */ 354132718Skan binding->previous = IDENTIFIER_BINDING (id); 355132718Skan INHERITED_VALUE_BINDING_P (binding) = 0; 356132718Skan LOCAL_BINDING_P (binding) = (level != class_binding_level); 357132718Skan 358132718Skan /* And put it on the front of the list of bindings for ID. */ 359132718Skan IDENTIFIER_BINDING (id) = binding; 360132718Skan} 361132718Skan 362132718Skan/* Remove the binding for DECL which should be the innermost binding 363132718Skan for ID. */ 364132718Skan 365132718Skanvoid 366132718Skanpop_binding (tree id, tree decl) 367132718Skan{ 368132718Skan cxx_binding *binding; 369132718Skan 370132718Skan if (id == NULL_TREE) 371132718Skan /* It's easiest to write the loops that call this function without 372132718Skan checking whether or not the entities involved have names. We 373132718Skan get here for such an entity. */ 374132718Skan return; 375132718Skan 376132718Skan /* Get the innermost binding for ID. */ 377132718Skan binding = IDENTIFIER_BINDING (id); 378132718Skan 379132718Skan /* The name should be bound. */ 380169689Skan gcc_assert (binding != NULL); 381132718Skan 382132718Skan /* The DECL will be either the ordinary binding or the type 383132718Skan binding for this identifier. Remove that binding. */ 384132718Skan if (binding->value == decl) 385132718Skan binding->value = NULL_TREE; 386132718Skan else 387169689Skan { 388169689Skan gcc_assert (binding->type == decl); 389169689Skan binding->type = NULL_TREE; 390169689Skan } 391132718Skan 392132718Skan if (!binding->value && !binding->type) 393132718Skan { 394132718Skan /* We're completely done with the innermost binding for this 395132718Skan identifier. Unhook it from the list of bindings. */ 396132718Skan IDENTIFIER_BINDING (id) = binding->previous; 397132718Skan 398132718Skan /* Add it to the free list. */ 399132718Skan cxx_binding_free (binding); 400132718Skan } 401132718Skan} 402132718Skan 403169689Skan/* BINDING records an existing declaration for a name in the current scope. 404132718Skan But, DECL is another declaration for that same identifier in the 405132718Skan same scope. This is the `struct stat' hack whereby a non-typedef 406132718Skan class name or enum-name can be bound at the same level as some other 407132718Skan kind of entity. 408132718Skan 3.3.7/1 409132718Skan 410132718Skan A class name (9.1) or enumeration name (7.2) can be hidden by the 411132718Skan name of an object, function, or enumerator declared in the same scope. 412132718Skan If a class or enumeration name and an object, function, or enumerator 413132718Skan are declared in the same scope (in any order) with the same name, the 414132718Skan class or enumeration name is hidden wherever the object, function, or 415132718Skan enumerator name is visible. 416132718Skan 417132718Skan It's the responsibility of the caller to check that 418132718Skan inserting this name is valid here. Returns nonzero if the new binding 419132718Skan was successful. */ 420132718Skan 421132718Skanstatic bool 422132718Skansupplement_binding (cxx_binding *binding, tree decl) 423132718Skan{ 424132718Skan tree bval = binding->value; 425132718Skan bool ok = true; 426132718Skan 427132718Skan timevar_push (TV_NAME_LOOKUP); 428132718Skan if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl)) 429132718Skan /* The new name is the type name. */ 430132718Skan binding->type = decl; 431132718Skan else if (/* BVAL is null when push_class_level_binding moves an 432132718Skan inherited type-binding out of the way to make room for a 433132718Skan new value binding. */ 434169689Skan !bval 435132718Skan /* BVAL is error_mark_node when DECL's name has been used 436132718Skan in a non-class scope prior declaration. In that case, 437132718Skan we should have already issued a diagnostic; for graceful 438132718Skan error recovery purpose, pretend this was the intended 439132718Skan declaration for that name. */ 440132718Skan || bval == error_mark_node 441169689Skan /* If BVAL is anticipated but has not yet been declared, 442132718Skan pretend it is not there at all. */ 443132718Skan || (TREE_CODE (bval) == FUNCTION_DECL 444169689Skan && DECL_ANTICIPATED (bval) 445169689Skan && !DECL_HIDDEN_FRIEND_P (bval))) 446132718Skan binding->value = decl; 447132718Skan else if (TREE_CODE (bval) == TYPE_DECL && DECL_ARTIFICIAL (bval)) 448132718Skan { 449132718Skan /* The old binding was a type name. It was placed in 450132718Skan VALUE field because it was thought, at the point it was 451132718Skan declared, to be the only entity with such a name. Move the 452132718Skan type name into the type slot; it is now hidden by the new 453132718Skan binding. */ 454132718Skan binding->type = bval; 455132718Skan binding->value = decl; 456132718Skan binding->value_is_inherited = false; 457132718Skan } 458132718Skan else if (TREE_CODE (bval) == TYPE_DECL 459132718Skan && TREE_CODE (decl) == TYPE_DECL 460132718Skan && DECL_NAME (decl) == DECL_NAME (bval) 461169689Skan && binding->scope->kind != sk_class 462132718Skan && (same_type_p (TREE_TYPE (decl), TREE_TYPE (bval)) 463132718Skan /* If either type involves template parameters, we must 464132718Skan wait until instantiation. */ 465132718Skan || uses_template_parms (TREE_TYPE (decl)) 466132718Skan || uses_template_parms (TREE_TYPE (bval)))) 467132718Skan /* We have two typedef-names, both naming the same type to have 468169689Skan the same name. In general, this is OK because of: 469132718Skan 470169689Skan [dcl.typedef] 471132718Skan 472132718Skan In a given scope, a typedef specifier can be used to redefine 473132718Skan the name of any type declared in that scope to refer to the 474169689Skan type to which it already refers. 475169689Skan 476169689Skan However, in class scopes, this rule does not apply due to the 477169689Skan stricter language in [class.mem] prohibiting redeclarations of 478169689Skan members. */ 479132718Skan ok = false; 480132718Skan /* There can be two block-scope declarations of the same variable, 481132718Skan so long as they are `extern' declarations. However, there cannot 482132718Skan be two declarations of the same static data member: 483132718Skan 484132718Skan [class.mem] 485132718Skan 486132718Skan A member shall not be declared twice in the 487132718Skan member-specification. */ 488132718Skan else if (TREE_CODE (decl) == VAR_DECL && TREE_CODE (bval) == VAR_DECL 489132718Skan && DECL_EXTERNAL (decl) && DECL_EXTERNAL (bval) 490132718Skan && !DECL_CLASS_SCOPE_P (decl)) 491132718Skan { 492169689Skan duplicate_decls (decl, binding->value, /*newdecl_is_friend=*/false); 493132718Skan ok = false; 494132718Skan } 495132718Skan else if (TREE_CODE (decl) == NAMESPACE_DECL 496132718Skan && TREE_CODE (bval) == NAMESPACE_DECL 497132718Skan && DECL_NAMESPACE_ALIAS (decl) 498132718Skan && DECL_NAMESPACE_ALIAS (bval) 499132718Skan && ORIGINAL_NAMESPACE (bval) == ORIGINAL_NAMESPACE (decl)) 500132718Skan /* [namespace.alias] 501169689Skan 502132718Skan In a declarative region, a namespace-alias-definition can be 503132718Skan used to redefine a namespace-alias declared in that declarative 504132718Skan region to refer only to the namespace to which it already 505132718Skan refers. */ 506132718Skan ok = false; 507132718Skan else 508132718Skan { 509169689Skan error ("declaration of %q#D", decl); 510169689Skan error ("conflicts with previous declaration %q+#D", bval); 511132718Skan ok = false; 512132718Skan } 513132718Skan 514132718Skan POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ok); 515132718Skan} 516132718Skan 517132718Skan/* Add DECL to the list of things declared in B. */ 518132718Skan 519132718Skanstatic void 520132718Skanadd_decl_to_level (tree decl, cxx_scope *b) 521132718Skan{ 522169689Skan if (TREE_CODE (decl) == NAMESPACE_DECL 523132718Skan && !DECL_NAMESPACE_ALIAS (decl)) 524132718Skan { 525132718Skan TREE_CHAIN (decl) = b->namespaces; 526132718Skan b->namespaces = decl; 527132718Skan } 528132718Skan else if (TREE_CODE (decl) == VAR_DECL && DECL_VIRTUAL_P (decl)) 529132718Skan { 530132718Skan TREE_CHAIN (decl) = b->vtables; 531132718Skan b->vtables = decl; 532132718Skan } 533169689Skan else 534132718Skan { 535132718Skan /* We build up the list in reverse order, and reverse it later if 536169689Skan necessary. */ 537132718Skan TREE_CHAIN (decl) = b->names; 538132718Skan b->names = decl; 539132718Skan b->names_size++; 540132718Skan 541132718Skan /* If appropriate, add decl to separate list of statics. We 542169689Skan include extern variables because they might turn out to be 543132718Skan static later. It's OK for this list to contain a few false 544169689Skan positives. */ 545132718Skan if (b->kind == sk_namespace) 546132718Skan if ((TREE_CODE (decl) == VAR_DECL 547132718Skan && (TREE_STATIC (decl) || DECL_EXTERNAL (decl))) 548132718Skan || (TREE_CODE (decl) == FUNCTION_DECL 549132718Skan && (!TREE_PUBLIC (decl) || DECL_DECLARED_INLINE_P (decl)))) 550169689Skan VEC_safe_push (tree, gc, b->static_decls, decl); 551132718Skan } 552132718Skan} 553132718Skan 554132718Skan/* Record a decl-node X as belonging to the current lexical scope. 555132718Skan Check for errors (such as an incompatible declaration for the same 556169689Skan name already seen in the same scope). IS_FRIEND is true if X is 557169689Skan declared as a friend. 558132718Skan 559132718Skan Returns either X or an old decl for the same name. 560132718Skan If an old decl is returned, it may have been smashed 561132718Skan to agree with what X says. */ 562132718Skan 563132718Skantree 564169689Skanpushdecl_maybe_friend (tree x, bool is_friend) 565132718Skan{ 566132718Skan tree t; 567132718Skan tree name; 568132718Skan int need_new_binding; 569132718Skan 570132718Skan timevar_push (TV_NAME_LOOKUP); 571132718Skan 572169689Skan if (x == error_mark_node) 573169689Skan POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node); 574169689Skan 575132718Skan need_new_binding = 1; 576132718Skan 577132718Skan if (DECL_TEMPLATE_PARM_P (x)) 578132718Skan /* Template parameters have no context; they are not X::T even 579132718Skan when declared within a class or namespace. */ 580132718Skan ; 581132718Skan else 582132718Skan { 583132718Skan if (current_function_decl && x != current_function_decl 584132718Skan /* A local declaration for a function doesn't constitute 585169689Skan nesting. */ 586132718Skan && TREE_CODE (x) != FUNCTION_DECL 587132718Skan /* A local declaration for an `extern' variable is in the 588132718Skan scope of the current namespace, not the current 589132718Skan function. */ 590132718Skan && !(TREE_CODE (x) == VAR_DECL && DECL_EXTERNAL (x)) 591132718Skan && !DECL_CONTEXT (x)) 592132718Skan DECL_CONTEXT (x) = current_function_decl; 593132718Skan 594132718Skan /* If this is the declaration for a namespace-scope function, 595132718Skan but the declaration itself is in a local scope, mark the 596132718Skan declaration. */ 597132718Skan if (TREE_CODE (x) == FUNCTION_DECL 598132718Skan && DECL_NAMESPACE_SCOPE_P (x) 599132718Skan && current_function_decl 600132718Skan && x != current_function_decl) 601132718Skan DECL_LOCAL_FUNCTION_P (x) = 1; 602132718Skan } 603132718Skan 604132718Skan name = DECL_NAME (x); 605132718Skan if (name) 606132718Skan { 607132718Skan int different_binding_level = 0; 608132718Skan 609132718Skan if (TREE_CODE (name) == TEMPLATE_ID_EXPR) 610132718Skan name = TREE_OPERAND (name, 0); 611132718Skan 612132718Skan /* In case this decl was explicitly namespace-qualified, look it 613132718Skan up in its namespace context. */ 614132718Skan if (DECL_NAMESPACE_SCOPE_P (x) && namespace_bindings_p ()) 615132718Skan t = namespace_binding (name, DECL_CONTEXT (x)); 616132718Skan else 617169689Skan t = lookup_name_innermost_nonclass_level (name); 618132718Skan 619132718Skan /* [basic.link] If there is a visible declaration of an entity 620132718Skan with linkage having the same name and type, ignoring entities 621132718Skan declared outside the innermost enclosing namespace scope, the 622132718Skan block scope declaration declares that same entity and 623132718Skan receives the linkage of the previous declaration. */ 624132718Skan if (! t && current_function_decl && x != current_function_decl 625132718Skan && (TREE_CODE (x) == FUNCTION_DECL || TREE_CODE (x) == VAR_DECL) 626132718Skan && DECL_EXTERNAL (x)) 627132718Skan { 628132718Skan /* Look in block scope. */ 629169689Skan t = innermost_non_namespace_value (name); 630132718Skan /* Or in the innermost namespace. */ 631132718Skan if (! t) 632132718Skan t = namespace_binding (name, DECL_CONTEXT (x)); 633132718Skan /* Does it have linkage? Note that if this isn't a DECL, it's an 634132718Skan OVERLOAD, which is OK. */ 635132718Skan if (t && DECL_P (t) && ! (TREE_STATIC (t) || DECL_EXTERNAL (t))) 636132718Skan t = NULL_TREE; 637132718Skan if (t) 638132718Skan different_binding_level = 1; 639132718Skan } 640132718Skan 641132718Skan /* If we are declaring a function, and the result of name-lookup 642132718Skan was an OVERLOAD, look for an overloaded instance that is 643132718Skan actually the same as the function we are declaring. (If 644132718Skan there is one, we have to merge our declaration with the 645132718Skan previous declaration.) */ 646132718Skan if (t && TREE_CODE (t) == OVERLOAD) 647132718Skan { 648132718Skan tree match; 649132718Skan 650132718Skan if (TREE_CODE (x) == FUNCTION_DECL) 651132718Skan for (match = t; match; match = OVL_NEXT (match)) 652132718Skan { 653132718Skan if (decls_match (OVL_CURRENT (match), x)) 654132718Skan break; 655132718Skan } 656132718Skan else 657132718Skan /* Just choose one. */ 658132718Skan match = t; 659132718Skan 660132718Skan if (match) 661132718Skan t = OVL_CURRENT (match); 662132718Skan else 663132718Skan t = NULL_TREE; 664132718Skan } 665132718Skan 666169689Skan if (t && t != error_mark_node) 667132718Skan { 668132718Skan if (different_binding_level) 669132718Skan { 670132718Skan if (decls_match (x, t)) 671132718Skan /* The standard only says that the local extern 672132718Skan inherits linkage from the previous decl; in 673169689Skan particular, default args are not shared. Add 674169689Skan the decl into a hash table to make sure only 675169689Skan the previous decl in this case is seen by the 676169689Skan middle end. */ 677169689Skan { 678169689Skan struct cxx_int_tree_map *h; 679169689Skan void **loc; 680169689Skan 681169689Skan TREE_PUBLIC (x) = TREE_PUBLIC (t); 682169689Skan 683169689Skan if (cp_function_chain->extern_decl_map == NULL) 684169689Skan cp_function_chain->extern_decl_map 685169689Skan = htab_create_ggc (20, cxx_int_tree_map_hash, 686169689Skan cxx_int_tree_map_eq, NULL); 687169689Skan 688169689Skan h = GGC_NEW (struct cxx_int_tree_map); 689169689Skan h->uid = DECL_UID (x); 690169689Skan h->to = t; 691169689Skan loc = htab_find_slot_with_hash 692169689Skan (cp_function_chain->extern_decl_map, h, 693169689Skan h->uid, INSERT); 694169689Skan *(struct cxx_int_tree_map **) loc = h; 695169689Skan } 696132718Skan } 697132718Skan else if (TREE_CODE (t) == PARM_DECL) 698132718Skan { 699169689Skan gcc_assert (DECL_CONTEXT (t)); 700132718Skan 701132718Skan /* Check for duplicate params. */ 702169689Skan if (duplicate_decls (x, t, is_friend)) 703132718Skan POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t); 704132718Skan } 705132718Skan else if ((DECL_EXTERN_C_FUNCTION_P (x) 706132718Skan || DECL_FUNCTION_TEMPLATE_P (x)) 707132718Skan && is_overloaded_fn (t)) 708132718Skan /* Don't do anything just yet. */; 709132718Skan else if (t == wchar_decl_node) 710132718Skan { 711132718Skan if (pedantic && ! DECL_IN_SYSTEM_HEADER (x)) 712169689Skan pedwarn ("redeclaration of %<wchar_t%> as %qT", 713169689Skan TREE_TYPE (x)); 714132718Skan 715132718Skan /* Throw away the redeclaration. */ 716132718Skan POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t); 717132718Skan } 718132718Skan else 719132718Skan { 720169689Skan tree olddecl = duplicate_decls (x, t, is_friend); 721169689Skan 722132718Skan /* If the redeclaration failed, we can stop at this 723132718Skan point. */ 724132718Skan if (olddecl == error_mark_node) 725132718Skan POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node); 726132718Skan 727132718Skan if (olddecl) 728132718Skan { 729132718Skan if (TREE_CODE (t) == TYPE_DECL) 730132718Skan SET_IDENTIFIER_TYPE_VALUE (name, TREE_TYPE (t)); 731132718Skan 732132718Skan POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t); 733132718Skan } 734132718Skan else if (DECL_MAIN_P (x) && TREE_CODE (t) == FUNCTION_DECL) 735132718Skan { 736132718Skan /* A redeclaration of main, but not a duplicate of the 737132718Skan previous one. 738169689Skan 739132718Skan [basic.start.main] 740169689Skan 741132718Skan This function shall not be overloaded. */ 742169689Skan error ("invalid redeclaration of %q+D", t); 743169689Skan error ("as %qD", x); 744132718Skan /* We don't try to push this declaration since that 745132718Skan causes a crash. */ 746132718Skan POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x); 747132718Skan } 748132718Skan } 749132718Skan } 750132718Skan 751169689Skan if (TREE_CODE (x) == FUNCTION_DECL || DECL_FUNCTION_TEMPLATE_P (x)) 752169689Skan check_default_args (x); 753169689Skan 754132718Skan check_template_shadow (x); 755132718Skan 756132718Skan /* If this is a function conjured up by the backend, massage it 757132718Skan so it looks friendly. */ 758132718Skan if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_LANG_SPECIFIC (x)) 759132718Skan { 760132718Skan retrofit_lang_decl (x); 761132718Skan SET_DECL_LANGUAGE (x, lang_c); 762132718Skan } 763132718Skan 764132718Skan if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_FUNCTION_MEMBER_P (x)) 765132718Skan { 766169689Skan t = push_overloaded_decl (x, PUSH_LOCAL, is_friend); 767132718Skan if (t != x) 768132718Skan POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t); 769132718Skan if (!namespace_bindings_p ()) 770132718Skan /* We do not need to create a binding for this name; 771132718Skan push_overloaded_decl will have already done so if 772132718Skan necessary. */ 773132718Skan need_new_binding = 0; 774132718Skan } 775132718Skan else if (DECL_FUNCTION_TEMPLATE_P (x) && DECL_NAMESPACE_SCOPE_P (x)) 776132718Skan { 777169689Skan t = push_overloaded_decl (x, PUSH_GLOBAL, is_friend); 778132718Skan if (t == x) 779132718Skan add_decl_to_level (x, NAMESPACE_LEVEL (CP_DECL_CONTEXT (t))); 780132718Skan POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t); 781132718Skan } 782132718Skan 783132718Skan /* If declaring a type as a typedef, copy the type (unless we're 784132718Skan at line 0), and install this TYPE_DECL as the new type's typedef 785132718Skan name. See the extensive comment in ../c-decl.c (pushdecl). */ 786132718Skan if (TREE_CODE (x) == TYPE_DECL) 787132718Skan { 788132718Skan tree type = TREE_TYPE (x); 789169689Skan if (DECL_IS_BUILTIN (x)) 790169689Skan { 791132718Skan if (TYPE_NAME (type) == 0) 792169689Skan TYPE_NAME (type) = x; 793169689Skan } 794169689Skan else if (type != error_mark_node && TYPE_NAME (type) != x 795132718Skan /* We don't want to copy the type when all we're 796132718Skan doing is making a TYPE_DECL for the purposes of 797132718Skan inlining. */ 798132718Skan && (!TYPE_NAME (type) 799132718Skan || TYPE_NAME (type) != DECL_ABSTRACT_ORIGIN (x))) 800169689Skan { 801132718Skan DECL_ORIGINAL_TYPE (x) = type; 802169689Skan type = build_variant_type_copy (type); 803132718Skan TYPE_STUB_DECL (type) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x)); 804169689Skan TYPE_NAME (type) = x; 805169689Skan TREE_TYPE (x) = type; 806169689Skan } 807132718Skan 808132718Skan if (type != error_mark_node 809132718Skan && TYPE_NAME (type) 810132718Skan && TYPE_IDENTIFIER (type)) 811169689Skan set_identifier_type_value (DECL_NAME (x), x); 812132718Skan } 813132718Skan 814132718Skan /* Multiple external decls of the same identifier ought to match. 815132718Skan 816132718Skan We get warnings about inline functions where they are defined. 817132718Skan We get warnings about other functions from push_overloaded_decl. 818132718Skan 819132718Skan Avoid duplicate warnings where they are used. */ 820132718Skan if (TREE_PUBLIC (x) && TREE_CODE (x) != FUNCTION_DECL) 821132718Skan { 822132718Skan tree decl; 823132718Skan 824132718Skan decl = IDENTIFIER_NAMESPACE_VALUE (name); 825132718Skan if (decl && TREE_CODE (decl) == OVERLOAD) 826132718Skan decl = OVL_FUNCTION (decl); 827132718Skan 828132718Skan if (decl && decl != error_mark_node 829132718Skan && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl)) 830132718Skan /* If different sort of thing, we already gave an error. */ 831132718Skan && TREE_CODE (decl) == TREE_CODE (x) 832132718Skan && !same_type_p (TREE_TYPE (x), TREE_TYPE (decl))) 833132718Skan { 834169689Skan pedwarn ("type mismatch with previous external decl of %q#D", x); 835169689Skan pedwarn ("previous external decl of %q+#D", decl); 836132718Skan } 837132718Skan } 838132718Skan 839169689Skan if (TREE_CODE (x) == FUNCTION_DECL 840169689Skan && is_friend 841169689Skan && !flag_friend_injection) 842169689Skan { 843169689Skan /* This is a new declaration of a friend function, so hide 844169689Skan it from ordinary function lookup. */ 845169689Skan DECL_ANTICIPATED (x) = 1; 846169689Skan DECL_HIDDEN_FRIEND_P (x) = 1; 847169689Skan } 848169689Skan 849132718Skan /* This name is new in its binding level. 850132718Skan Install the new declaration and return it. */ 851132718Skan if (namespace_bindings_p ()) 852132718Skan { 853132718Skan /* Install a global value. */ 854132718Skan 855132718Skan /* If the first global decl has external linkage, 856132718Skan warn if we later see static one. */ 857132718Skan if (IDENTIFIER_GLOBAL_VALUE (name) == NULL_TREE && TREE_PUBLIC (x)) 858132718Skan TREE_PUBLIC (name) = 1; 859132718Skan 860169689Skan /* Bind the name for the entity. */ 861169689Skan if (!(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x) 862169689Skan && t != NULL_TREE) 863169689Skan && (TREE_CODE (x) == TYPE_DECL 864169689Skan || TREE_CODE (x) == VAR_DECL 865169689Skan || TREE_CODE (x) == NAMESPACE_DECL 866169689Skan || TREE_CODE (x) == CONST_DECL 867169689Skan || TREE_CODE (x) == TEMPLATE_DECL)) 868169689Skan SET_IDENTIFIER_NAMESPACE_VALUE (name, x); 869132718Skan 870132718Skan /* If new decl is `static' and an `extern' was seen previously, 871132718Skan warn about it. */ 872132718Skan if (x != NULL_TREE && t != NULL_TREE && decls_match (x, t)) 873132718Skan warn_extern_redeclared_static (x, t); 874132718Skan } 875132718Skan else 876132718Skan { 877132718Skan /* Here to install a non-global value. */ 878169689Skan tree oldlocal = innermost_non_namespace_value (name); 879132718Skan tree oldglobal = IDENTIFIER_NAMESPACE_VALUE (name); 880132718Skan 881132718Skan if (need_new_binding) 882132718Skan { 883132718Skan push_local_binding (name, x, 0); 884132718Skan /* Because push_local_binding will hook X on to the 885132718Skan current_binding_level's name list, we don't want to 886132718Skan do that again below. */ 887132718Skan need_new_binding = 0; 888132718Skan } 889132718Skan 890132718Skan /* If this is a TYPE_DECL, push it into the type value slot. */ 891132718Skan if (TREE_CODE (x) == TYPE_DECL) 892132718Skan set_identifier_type_value (name, x); 893132718Skan 894132718Skan /* Clear out any TYPE_DECL shadowed by a namespace so that 895132718Skan we won't think this is a type. The C struct hack doesn't 896132718Skan go through namespaces. */ 897132718Skan if (TREE_CODE (x) == NAMESPACE_DECL) 898132718Skan set_identifier_type_value (name, NULL_TREE); 899132718Skan 900132718Skan if (oldlocal) 901132718Skan { 902132718Skan tree d = oldlocal; 903132718Skan 904132718Skan while (oldlocal 905132718Skan && TREE_CODE (oldlocal) == VAR_DECL 906132718Skan && DECL_DEAD_FOR_LOCAL (oldlocal)) 907132718Skan oldlocal = DECL_SHADOWED_FOR_VAR (oldlocal); 908132718Skan 909132718Skan if (oldlocal == NULL_TREE) 910132718Skan oldlocal = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (d)); 911132718Skan } 912132718Skan 913132718Skan /* If this is an extern function declaration, see if we 914132718Skan have a global definition or declaration for the function. */ 915132718Skan if (oldlocal == NULL_TREE 916132718Skan && DECL_EXTERNAL (x) 917132718Skan && oldglobal != NULL_TREE 918132718Skan && TREE_CODE (x) == FUNCTION_DECL 919132718Skan && TREE_CODE (oldglobal) == FUNCTION_DECL) 920132718Skan { 921132718Skan /* We have one. Their types must agree. */ 922132718Skan if (decls_match (x, oldglobal)) 923132718Skan /* OK */; 924132718Skan else 925132718Skan { 926169689Skan warning (0, "extern declaration of %q#D doesn't match", x); 927169689Skan warning (0, "global declaration %q+#D", oldglobal); 928132718Skan } 929132718Skan } 930132718Skan /* If we have a local external declaration, 931132718Skan and no file-scope declaration has yet been seen, 932132718Skan then if we later have a file-scope decl it must not be static. */ 933132718Skan if (oldlocal == NULL_TREE 934132718Skan && oldglobal == NULL_TREE 935132718Skan && DECL_EXTERNAL (x) 936132718Skan && TREE_PUBLIC (x)) 937132718Skan TREE_PUBLIC (name) = 1; 938132718Skan 939132718Skan /* Warn if shadowing an argument at the top level of the body. */ 940132718Skan if (oldlocal != NULL_TREE && !DECL_EXTERNAL (x) 941132718Skan /* Inline decls shadow nothing. */ 942132718Skan && !DECL_FROM_INLINE (x) 943132718Skan && TREE_CODE (oldlocal) == PARM_DECL 944132718Skan /* Don't check the `this' parameter. */ 945132718Skan && !DECL_ARTIFICIAL (oldlocal)) 946132718Skan { 947132718Skan bool err = false; 948132718Skan 949132718Skan /* Don't complain if it's from an enclosing function. */ 950132718Skan if (DECL_CONTEXT (oldlocal) == current_function_decl 951132718Skan && TREE_CODE (x) != PARM_DECL) 952132718Skan { 953132718Skan /* Go to where the parms should be and see if we find 954132718Skan them there. */ 955132718Skan struct cp_binding_level *b = current_binding_level->level_chain; 956132718Skan 957169689Skan if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl)) 958169689Skan /* Skip the ctor/dtor cleanup level. */ 959169689Skan b = b->level_chain; 960132718Skan 961132718Skan /* ARM $8.3 */ 962132718Skan if (b->kind == sk_function_parms) 963132718Skan { 964169689Skan error ("declaration of %q#D shadows a parameter", x); 965132718Skan err = true; 966132718Skan } 967132718Skan } 968132718Skan 969132718Skan if (warn_shadow && !err) 970132718Skan { 971169689Skan warning (OPT_Wshadow, "declaration of %q#D shadows a parameter", x); 972169689Skan warning (OPT_Wshadow, "%Jshadowed declaration is here", oldlocal); 973132718Skan } 974132718Skan } 975132718Skan 976132718Skan /* Maybe warn if shadowing something else. */ 977132718Skan else if (warn_shadow && !DECL_EXTERNAL (x) 978132718Skan /* No shadow warnings for internally generated vars. */ 979132718Skan && ! DECL_ARTIFICIAL (x) 980132718Skan /* No shadow warnings for vars made for inlining. */ 981132718Skan && ! DECL_FROM_INLINE (x)) 982132718Skan { 983169689Skan tree member; 984169689Skan 985169689Skan if (current_class_ptr) 986169689Skan member = lookup_member (current_class_type, 987169689Skan name, 988169689Skan /*protect=*/0, 989169689Skan /*want_type=*/false); 990169689Skan else 991169689Skan member = NULL_TREE; 992169689Skan 993169689Skan if (member && !TREE_STATIC (member)) 994132718Skan { 995132718Skan /* Location of previous decl is not useful in this case. */ 996169689Skan warning (OPT_Wshadow, "declaration of %qD shadows a member of 'this'", 997132718Skan x); 998132718Skan } 999132718Skan else if (oldlocal != NULL_TREE 1000132718Skan && TREE_CODE (oldlocal) == VAR_DECL) 1001132718Skan { 1002169689Skan warning (OPT_Wshadow, "declaration of %qD shadows a previous local", x); 1003169689Skan warning (OPT_Wshadow, "%Jshadowed declaration is here", oldlocal); 1004132718Skan } 1005132718Skan else if (oldglobal != NULL_TREE 1006132718Skan && TREE_CODE (oldglobal) == VAR_DECL) 1007132718Skan /* XXX shadow warnings in outer-more namespaces */ 1008132718Skan { 1009169689Skan warning (OPT_Wshadow, "declaration of %qD shadows a global declaration", 1010132718Skan x); 1011169689Skan warning (OPT_Wshadow, "%Jshadowed declaration is here", oldglobal); 1012132718Skan } 1013132718Skan } 1014132718Skan } 1015132718Skan 1016132718Skan if (TREE_CODE (x) == VAR_DECL) 1017132718Skan maybe_register_incomplete_var (x); 1018132718Skan } 1019132718Skan 1020132718Skan if (need_new_binding) 1021132718Skan add_decl_to_level (x, 1022132718Skan DECL_NAMESPACE_SCOPE_P (x) 1023132718Skan ? NAMESPACE_LEVEL (CP_DECL_CONTEXT (x)) 1024132718Skan : current_binding_level); 1025132718Skan 1026132718Skan POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x); 1027132718Skan} 1028132718Skan 1029169689Skan/* Record a decl-node X as belonging to the current lexical scope. */ 1030169689Skan 1031169689Skantree 1032169689Skanpushdecl (tree x) 1033169689Skan{ 1034169689Skan return pushdecl_maybe_friend (x, false); 1035169689Skan} 1036169689Skan 1037132718Skan/* Enter DECL into the symbol table, if that's appropriate. Returns 1038132718Skan DECL, or a modified version thereof. */ 1039132718Skan 1040132718Skantree 1041132718Skanmaybe_push_decl (tree decl) 1042132718Skan{ 1043132718Skan tree type = TREE_TYPE (decl); 1044132718Skan 1045132718Skan /* Add this decl to the current binding level, but not if it comes 1046132718Skan from another scope, e.g. a static member variable. TEM may equal 1047132718Skan DECL or it may be a previous decl of the same name. */ 1048132718Skan if (decl == error_mark_node 1049132718Skan || (TREE_CODE (decl) != PARM_DECL 1050132718Skan && DECL_CONTEXT (decl) != NULL_TREE 1051132718Skan /* Definitions of namespace members outside their namespace are 1052132718Skan possible. */ 1053132718Skan && TREE_CODE (DECL_CONTEXT (decl)) != NAMESPACE_DECL) 1054132718Skan || (TREE_CODE (decl) == TEMPLATE_DECL && !namespace_bindings_p ()) 1055132718Skan || TREE_CODE (type) == UNKNOWN_TYPE 1056132718Skan /* The declaration of a template specialization does not affect 1057132718Skan the functions available for overload resolution, so we do not 1058132718Skan call pushdecl. */ 1059132718Skan || (TREE_CODE (decl) == FUNCTION_DECL 1060132718Skan && DECL_TEMPLATE_SPECIALIZATION (decl))) 1061132718Skan return decl; 1062132718Skan else 1063132718Skan return pushdecl (decl); 1064132718Skan} 1065132718Skan 1066132718Skan/* Bind DECL to ID in the current_binding_level, assumed to be a local 1067132718Skan binding level. If PUSH_USING is set in FLAGS, we know that DECL 1068132718Skan doesn't really belong to this binding level, that it got here 1069132718Skan through a using-declaration. */ 1070132718Skan 1071161651Skanvoid 1072132718Skanpush_local_binding (tree id, tree decl, int flags) 1073132718Skan{ 1074132718Skan struct cp_binding_level *b; 1075132718Skan 1076132718Skan /* Skip over any local classes. This makes sense if we call 1077132718Skan push_local_binding with a friend decl of a local class. */ 1078132718Skan b = innermost_nonclass_level (); 1079132718Skan 1080169689Skan if (lookup_name_innermost_nonclass_level (id)) 1081132718Skan { 1082132718Skan /* Supplement the existing binding. */ 1083132718Skan if (!supplement_binding (IDENTIFIER_BINDING (id), decl)) 1084132718Skan /* It didn't work. Something else must be bound at this 1085132718Skan level. Do not add DECL to the list of things to pop 1086132718Skan later. */ 1087132718Skan return; 1088132718Skan } 1089132718Skan else 1090132718Skan /* Create a new binding. */ 1091132718Skan push_binding (id, decl, b); 1092132718Skan 1093132718Skan if (TREE_CODE (decl) == OVERLOAD || (flags & PUSH_USING)) 1094132718Skan /* We must put the OVERLOAD into a TREE_LIST since the 1095132718Skan TREE_CHAIN of an OVERLOAD is already used. Similarly for 1096132718Skan decls that got here through a using-declaration. */ 1097132718Skan decl = build_tree_list (NULL_TREE, decl); 1098132718Skan 1099132718Skan /* And put DECL on the list of things declared by the current 1100132718Skan binding level. */ 1101132718Skan add_decl_to_level (decl, b); 1102132718Skan} 1103132718Skan 1104132718Skan/* Check to see whether or not DECL is a variable that would have been 1105132718Skan in scope under the ARM, but is not in scope under the ANSI/ISO 1106132718Skan standard. If so, issue an error message. If name lookup would 1107132718Skan work in both cases, but return a different result, this function 1108132718Skan returns the result of ANSI/ISO lookup. Otherwise, it returns 1109132718Skan DECL. */ 1110132718Skan 1111132718Skantree 1112132718Skancheck_for_out_of_scope_variable (tree decl) 1113132718Skan{ 1114132718Skan tree shadowed; 1115132718Skan 1116132718Skan /* We only care about out of scope variables. */ 1117132718Skan if (!(TREE_CODE (decl) == VAR_DECL && DECL_DEAD_FOR_LOCAL (decl))) 1118132718Skan return decl; 1119132718Skan 1120169689Skan shadowed = DECL_HAS_SHADOWED_FOR_VAR_P (decl) 1121169689Skan ? DECL_SHADOWED_FOR_VAR (decl) : NULL_TREE ; 1122132718Skan while (shadowed != NULL_TREE && TREE_CODE (shadowed) == VAR_DECL 1123132718Skan && DECL_DEAD_FOR_LOCAL (shadowed)) 1124169689Skan shadowed = DECL_HAS_SHADOWED_FOR_VAR_P (shadowed) 1125169689Skan ? DECL_SHADOWED_FOR_VAR (shadowed) : NULL_TREE; 1126132718Skan if (!shadowed) 1127132718Skan shadowed = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (decl)); 1128132718Skan if (shadowed) 1129132718Skan { 1130132718Skan if (!DECL_ERROR_REPORTED (decl)) 1131132718Skan { 1132169689Skan warning (0, "name lookup of %qD changed", DECL_NAME (decl)); 1133169689Skan warning (0, " matches this %q+D under ISO standard rules", 1134169689Skan shadowed); 1135169689Skan warning (0, " matches this %q+D under old rules", decl); 1136132718Skan DECL_ERROR_REPORTED (decl) = 1; 1137132718Skan } 1138132718Skan return shadowed; 1139132718Skan } 1140132718Skan 1141132718Skan /* If we have already complained about this declaration, there's no 1142132718Skan need to do it again. */ 1143132718Skan if (DECL_ERROR_REPORTED (decl)) 1144132718Skan return decl; 1145132718Skan 1146132718Skan DECL_ERROR_REPORTED (decl) = 1; 1147161651Skan 1148161651Skan if (TREE_TYPE (decl) == error_mark_node) 1149161651Skan return decl; 1150161651Skan 1151132718Skan if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl))) 1152132718Skan { 1153169689Skan error ("name lookup of %qD changed for new ISO %<for%> scoping", 1154132718Skan DECL_NAME (decl)); 1155169689Skan error (" cannot use obsolete binding at %q+D because " 1156169689Skan "it has a destructor", decl); 1157132718Skan return error_mark_node; 1158132718Skan } 1159132718Skan else 1160132718Skan { 1161169689Skan pedwarn ("name lookup of %qD changed for new ISO %<for%> scoping", 1162132718Skan DECL_NAME (decl)); 1163169689Skan pedwarn (" using obsolete binding at %q+D", decl); 1164132718Skan } 1165132718Skan 1166132718Skan return decl; 1167132718Skan} 1168132718Skan 1169132718Skan/* true means unconditionally make a BLOCK for the next level pushed. */ 1170132718Skan 1171132718Skanstatic bool keep_next_level_flag; 1172132718Skan 1173132718Skanstatic int binding_depth = 0; 1174132718Skanstatic int is_class_level = 0; 1175132718Skan 1176132718Skanstatic void 1177132718Skanindent (int depth) 1178132718Skan{ 1179132718Skan int i; 1180132718Skan 1181132718Skan for (i = 0; i < depth * 2; i++) 1182132718Skan putc (' ', stderr); 1183132718Skan} 1184132718Skan 1185132718Skan/* Return a string describing the kind of SCOPE we have. */ 1186132718Skanstatic const char * 1187132718Skancxx_scope_descriptor (cxx_scope *scope) 1188132718Skan{ 1189132718Skan /* The order of this table must match the "scope_kind" 1190132718Skan enumerators. */ 1191132718Skan static const char* scope_kind_names[] = { 1192132718Skan "block-scope", 1193132718Skan "cleanup-scope", 1194132718Skan "try-scope", 1195132718Skan "catch-scope", 1196132718Skan "for-scope", 1197132718Skan "function-parameter-scope", 1198132718Skan "class-scope", 1199132718Skan "namespace-scope", 1200132718Skan "template-parameter-scope", 1201132718Skan "template-explicit-spec-scope" 1202132718Skan }; 1203132718Skan const scope_kind kind = scope->explicit_spec_p 1204132718Skan ? sk_template_spec : scope->kind; 1205132718Skan 1206132718Skan return scope_kind_names[kind]; 1207132718Skan} 1208132718Skan 1209132718Skan/* Output a debugging information about SCOPE when performing 1210132718Skan ACTION at LINE. */ 1211132718Skanstatic void 1212132718Skancxx_scope_debug (cxx_scope *scope, int line, const char *action) 1213132718Skan{ 1214132718Skan const char *desc = cxx_scope_descriptor (scope); 1215132718Skan if (scope->this_entity) 1216132718Skan verbatim ("%s %s(%E) %p %d\n", action, desc, 1217169689Skan scope->this_entity, (void *) scope, line); 1218132718Skan else 1219132718Skan verbatim ("%s %s %p %d\n", action, desc, (void *) scope, line); 1220132718Skan} 1221132718Skan 1222132718Skan/* Return the estimated initial size of the hashtable of a NAMESPACE 1223132718Skan scope. */ 1224132718Skan 1225132718Skanstatic inline size_t 1226132718Skannamespace_scope_ht_size (tree ns) 1227132718Skan{ 1228132718Skan tree name = DECL_NAME (ns); 1229132718Skan 1230132718Skan return name == std_identifier 1231132718Skan ? NAMESPACE_STD_HT_SIZE 1232132718Skan : (name == global_scope_name 1233132718Skan ? GLOBAL_SCOPE_HT_SIZE 1234132718Skan : NAMESPACE_ORDINARY_HT_SIZE); 1235132718Skan} 1236132718Skan 1237132718Skan/* A chain of binding_level structures awaiting reuse. */ 1238132718Skan 1239169689Skanstatic GTY((deletable)) struct cp_binding_level *free_binding_level; 1240132718Skan 1241169689Skan/* Insert SCOPE as the innermost binding level. */ 1242169689Skan 1243169689Skanvoid 1244169689Skanpush_binding_level (struct cp_binding_level *scope) 1245169689Skan{ 1246169689Skan /* Add it to the front of currently active scopes stack. */ 1247169689Skan scope->level_chain = current_binding_level; 1248169689Skan current_binding_level = scope; 1249169689Skan keep_next_level_flag = false; 1250169689Skan 1251169689Skan if (ENABLE_SCOPE_CHECKING) 1252169689Skan { 1253169689Skan scope->binding_depth = binding_depth; 1254169689Skan indent (binding_depth); 1255169689Skan cxx_scope_debug (scope, input_line, "push"); 1256169689Skan is_class_level = 0; 1257169689Skan binding_depth++; 1258169689Skan } 1259169689Skan} 1260169689Skan 1261132718Skan/* Create a new KIND scope and make it the top of the active scopes stack. 1262132718Skan ENTITY is the scope of the associated C++ entity (namespace, class, 1263132718Skan function); it is NULL otherwise. */ 1264132718Skan 1265132718Skancxx_scope * 1266132718Skanbegin_scope (scope_kind kind, tree entity) 1267132718Skan{ 1268132718Skan cxx_scope *scope; 1269169689Skan 1270132718Skan /* Reuse or create a struct for this binding level. */ 1271132718Skan if (!ENABLE_SCOPE_CHECKING && free_binding_level) 1272132718Skan { 1273132718Skan scope = free_binding_level; 1274132718Skan free_binding_level = scope->level_chain; 1275132718Skan } 1276132718Skan else 1277169689Skan scope = GGC_NEW (cxx_scope); 1278132718Skan memset (scope, 0, sizeof (cxx_scope)); 1279132718Skan 1280132718Skan scope->this_entity = entity; 1281132718Skan scope->more_cleanups_ok = true; 1282132718Skan switch (kind) 1283132718Skan { 1284132718Skan case sk_cleanup: 1285132718Skan scope->keep = true; 1286132718Skan break; 1287169689Skan 1288132718Skan case sk_template_spec: 1289132718Skan scope->explicit_spec_p = true; 1290132718Skan kind = sk_template_parms; 1291132718Skan /* Fall through. */ 1292132718Skan case sk_template_parms: 1293132718Skan case sk_block: 1294132718Skan case sk_try: 1295132718Skan case sk_catch: 1296132718Skan case sk_for: 1297132718Skan case sk_class: 1298132718Skan case sk_function_parms: 1299169689Skan case sk_omp: 1300132718Skan scope->keep = keep_next_level_flag; 1301132718Skan break; 1302132718Skan 1303132718Skan case sk_namespace: 1304132718Skan NAMESPACE_LEVEL (entity) = scope; 1305169689Skan scope->static_decls = 1306169689Skan VEC_alloc (tree, gc, 1307169689Skan DECL_NAME (entity) == std_identifier 1308169689Skan || DECL_NAME (entity) == global_scope_name 1309169689Skan ? 200 : 10); 1310132718Skan break; 1311132718Skan 1312132718Skan default: 1313132718Skan /* Should not happen. */ 1314169689Skan gcc_unreachable (); 1315132718Skan break; 1316132718Skan } 1317132718Skan scope->kind = kind; 1318132718Skan 1319169689Skan push_binding_level (scope); 1320132718Skan 1321132718Skan return scope; 1322132718Skan} 1323132718Skan 1324132718Skan/* We're about to leave current scope. Pop the top of the stack of 1325132718Skan currently active scopes. Return the enclosing scope, now active. */ 1326132718Skan 1327132718Skancxx_scope * 1328132718Skanleave_scope (void) 1329132718Skan{ 1330132718Skan cxx_scope *scope = current_binding_level; 1331132718Skan 1332132718Skan if (scope->kind == sk_namespace && class_binding_level) 1333132718Skan current_binding_level = class_binding_level; 1334132718Skan 1335132718Skan /* We cannot leave a scope, if there are none left. */ 1336132718Skan if (NAMESPACE_LEVEL (global_namespace)) 1337169689Skan gcc_assert (!global_scope_p (scope)); 1338169689Skan 1339132718Skan if (ENABLE_SCOPE_CHECKING) 1340132718Skan { 1341132718Skan indent (--binding_depth); 1342169689Skan cxx_scope_debug (scope, input_line, "leave"); 1343132718Skan if (is_class_level != (scope == class_binding_level)) 1344169689Skan { 1345169689Skan indent (binding_depth); 1346169689Skan verbatim ("XXX is_class_level != (current_scope == class_scope)\n"); 1347169689Skan } 1348132718Skan is_class_level = 0; 1349132718Skan } 1350132718Skan 1351169689Skan#ifdef HANDLE_PRAGMA_VISIBILITY 1352169689Skan if (scope->has_visibility) 1353169689Skan pop_visibility (); 1354169689Skan#endif 1355169689Skan 1356132718Skan /* Move one nesting level up. */ 1357132718Skan current_binding_level = scope->level_chain; 1358132718Skan 1359169689Skan /* Namespace-scopes are left most probably temporarily, not 1360169689Skan completely; they can be reopened later, e.g. in namespace-extension 1361169689Skan or any name binding activity that requires us to resume a 1362169689Skan namespace. For classes, we cache some binding levels. For other 1363132718Skan scopes, we just make the structure available for reuse. */ 1364169689Skan if (scope->kind != sk_namespace 1365169689Skan && scope->kind != sk_class) 1366132718Skan { 1367132718Skan scope->level_chain = free_binding_level; 1368169689Skan gcc_assert (!ENABLE_SCOPE_CHECKING 1369169689Skan || scope->binding_depth == binding_depth); 1370132718Skan free_binding_level = scope; 1371132718Skan } 1372132718Skan 1373132718Skan /* Find the innermost enclosing class scope, and reset 1374132718Skan CLASS_BINDING_LEVEL appropriately. */ 1375169689Skan if (scope->kind == sk_class) 1376169689Skan { 1377169689Skan class_binding_level = NULL; 1378169689Skan for (scope = current_binding_level; scope; scope = scope->level_chain) 1379169689Skan if (scope->kind == sk_class) 1380169689Skan { 1381169689Skan class_binding_level = scope; 1382169689Skan break; 1383169689Skan } 1384169689Skan } 1385132718Skan 1386132718Skan return current_binding_level; 1387132718Skan} 1388132718Skan 1389132718Skanstatic void 1390132718Skanresume_scope (struct cp_binding_level* b) 1391132718Skan{ 1392132718Skan /* Resuming binding levels is meant only for namespaces, 1393132718Skan and those cannot nest into classes. */ 1394169689Skan gcc_assert (!class_binding_level); 1395132718Skan /* Also, resuming a non-directly nested namespace is a no-no. */ 1396169689Skan gcc_assert (b->level_chain == current_binding_level); 1397132718Skan current_binding_level = b; 1398132718Skan if (ENABLE_SCOPE_CHECKING) 1399132718Skan { 1400132718Skan b->binding_depth = binding_depth; 1401132718Skan indent (binding_depth); 1402169689Skan cxx_scope_debug (b, input_line, "resume"); 1403132718Skan is_class_level = 0; 1404132718Skan binding_depth++; 1405132718Skan } 1406132718Skan} 1407132718Skan 1408132718Skan/* Return the innermost binding level that is not for a class scope. */ 1409132718Skan 1410132718Skanstatic cxx_scope * 1411132718Skaninnermost_nonclass_level (void) 1412132718Skan{ 1413132718Skan cxx_scope *b; 1414132718Skan 1415132718Skan b = current_binding_level; 1416132718Skan while (b->kind == sk_class) 1417132718Skan b = b->level_chain; 1418132718Skan 1419132718Skan return b; 1420132718Skan} 1421132718Skan 1422132718Skan/* We're defining an object of type TYPE. If it needs a cleanup, but 1423132718Skan we're not allowed to add any more objects with cleanups to the current 1424132718Skan scope, create a new binding level. */ 1425132718Skan 1426132718Skanvoid 1427132718Skanmaybe_push_cleanup_level (tree type) 1428132718Skan{ 1429132718Skan if (type != error_mark_node 1430132718Skan && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type) 1431132718Skan && current_binding_level->more_cleanups_ok == 0) 1432132718Skan { 1433132718Skan begin_scope (sk_cleanup, NULL); 1434169689Skan current_binding_level->statement_list = push_stmt_list (); 1435132718Skan } 1436132718Skan} 1437132718Skan 1438132718Skan/* Nonzero if we are currently in the global binding level. */ 1439132718Skan 1440132718Skanint 1441132718Skanglobal_bindings_p (void) 1442132718Skan{ 1443132718Skan return global_scope_p (current_binding_level); 1444132718Skan} 1445132718Skan 1446132718Skan/* True if we are currently in a toplevel binding level. This 1447132718Skan means either the global binding level or a namespace in a toplevel 1448132718Skan binding level. Since there are no non-toplevel namespace levels, 1449132718Skan this really means any namespace or template parameter level. We 1450132718Skan also include a class whose context is toplevel. */ 1451132718Skan 1452132718Skanbool 1453132718Skantoplevel_bindings_p (void) 1454132718Skan{ 1455132718Skan struct cp_binding_level *b = innermost_nonclass_level (); 1456132718Skan 1457132718Skan return b->kind == sk_namespace || b->kind == sk_template_parms; 1458132718Skan} 1459132718Skan 1460132718Skan/* True if this is a namespace scope, or if we are defining a class 1461132718Skan which is itself at namespace scope, or whose enclosing class is 1462132718Skan such a class, etc. */ 1463132718Skan 1464132718Skanbool 1465132718Skannamespace_bindings_p (void) 1466132718Skan{ 1467132718Skan struct cp_binding_level *b = innermost_nonclass_level (); 1468132718Skan 1469132718Skan return b->kind == sk_namespace; 1470132718Skan} 1471132718Skan 1472132718Skan/* True if the current level needs to have a BLOCK made. */ 1473132718Skan 1474132718Skanbool 1475132718Skankept_level_p (void) 1476132718Skan{ 1477132718Skan return (current_binding_level->blocks != NULL_TREE 1478132718Skan || current_binding_level->keep 1479169689Skan || current_binding_level->kind == sk_cleanup 1480169689Skan || current_binding_level->names != NULL_TREE); 1481132718Skan} 1482132718Skan 1483132718Skan/* Returns the kind of the innermost scope. */ 1484132718Skan 1485132718Skanscope_kind 1486132718Skaninnermost_scope_kind (void) 1487132718Skan{ 1488132718Skan return current_binding_level->kind; 1489132718Skan} 1490132718Skan 1491132718Skan/* Returns true if this scope was created to store template parameters. */ 1492132718Skan 1493132718Skanbool 1494132718Skantemplate_parm_scope_p (void) 1495132718Skan{ 1496132718Skan return innermost_scope_kind () == sk_template_parms; 1497132718Skan} 1498132718Skan 1499132718Skan/* If KEEP is true, make a BLOCK node for the next binding level, 1500132718Skan unconditionally. Otherwise, use the normal logic to decide whether 1501132718Skan or not to create a BLOCK. */ 1502132718Skan 1503132718Skanvoid 1504132718Skankeep_next_level (bool keep) 1505132718Skan{ 1506132718Skan keep_next_level_flag = keep; 1507132718Skan} 1508132718Skan 1509132718Skan/* Return the list of declarations of the current level. 1510132718Skan Note that this list is in reverse order unless/until 1511132718Skan you nreverse it; and when you do nreverse it, you must 1512132718Skan store the result back using `storedecls' or you will lose. */ 1513132718Skan 1514132718Skantree 1515132718Skangetdecls (void) 1516132718Skan{ 1517132718Skan return current_binding_level->names; 1518132718Skan} 1519132718Skan 1520132718Skan/* For debugging. */ 1521132718Skanstatic int no_print_functions = 0; 1522132718Skanstatic int no_print_builtins = 0; 1523132718Skan 1524132718Skanstatic void 1525132718Skanprint_binding_level (struct cp_binding_level* lvl) 1526132718Skan{ 1527132718Skan tree t; 1528132718Skan int i = 0, len; 1529169689Skan fprintf (stderr, " blocks=%p", (void *) lvl->blocks); 1530132718Skan if (lvl->more_cleanups_ok) 1531132718Skan fprintf (stderr, " more-cleanups-ok"); 1532132718Skan if (lvl->have_cleanups) 1533132718Skan fprintf (stderr, " have-cleanups"); 1534132718Skan fprintf (stderr, "\n"); 1535132718Skan if (lvl->names) 1536132718Skan { 1537132718Skan fprintf (stderr, " names:\t"); 1538132718Skan /* We can probably fit 3 names to a line? */ 1539132718Skan for (t = lvl->names; t; t = TREE_CHAIN (t)) 1540132718Skan { 1541132718Skan if (no_print_functions && (TREE_CODE (t) == FUNCTION_DECL)) 1542132718Skan continue; 1543132718Skan if (no_print_builtins 1544132718Skan && (TREE_CODE (t) == TYPE_DECL) 1545169689Skan && DECL_IS_BUILTIN (t)) 1546132718Skan continue; 1547132718Skan 1548132718Skan /* Function decls tend to have longer names. */ 1549132718Skan if (TREE_CODE (t) == FUNCTION_DECL) 1550132718Skan len = 3; 1551132718Skan else 1552132718Skan len = 2; 1553132718Skan i += len; 1554132718Skan if (i > 6) 1555132718Skan { 1556132718Skan fprintf (stderr, "\n\t"); 1557132718Skan i = len; 1558132718Skan } 1559132718Skan print_node_brief (stderr, "", t, 0); 1560132718Skan if (t == error_mark_node) 1561132718Skan break; 1562132718Skan } 1563132718Skan if (i) 1564132718Skan fprintf (stderr, "\n"); 1565132718Skan } 1566169689Skan if (VEC_length (cp_class_binding, lvl->class_shadowed)) 1567132718Skan { 1568169689Skan size_t i; 1569169689Skan cp_class_binding *b; 1570132718Skan fprintf (stderr, " class-shadowed:"); 1571169689Skan for (i = 0; 1572169689Skan VEC_iterate(cp_class_binding, lvl->class_shadowed, i, b); 1573169689Skan ++i) 1574169689Skan fprintf (stderr, " %s ", IDENTIFIER_POINTER (b->identifier)); 1575132718Skan fprintf (stderr, "\n"); 1576132718Skan } 1577132718Skan if (lvl->type_shadowed) 1578132718Skan { 1579132718Skan fprintf (stderr, " type-shadowed:"); 1580132718Skan for (t = lvl->type_shadowed; t; t = TREE_CHAIN (t)) 1581169689Skan { 1582132718Skan fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t))); 1583169689Skan } 1584132718Skan fprintf (stderr, "\n"); 1585132718Skan } 1586132718Skan} 1587132718Skan 1588132718Skanvoid 1589132718Skanprint_other_binding_stack (struct cp_binding_level *stack) 1590132718Skan{ 1591132718Skan struct cp_binding_level *level; 1592132718Skan for (level = stack; !global_scope_p (level); level = level->level_chain) 1593132718Skan { 1594169689Skan fprintf (stderr, "binding level %p\n", (void *) level); 1595132718Skan print_binding_level (level); 1596132718Skan } 1597132718Skan} 1598132718Skan 1599132718Skanvoid 1600132718Skanprint_binding_stack (void) 1601132718Skan{ 1602132718Skan struct cp_binding_level *b; 1603169689Skan fprintf (stderr, "current_binding_level=%p\n" 1604169689Skan "class_binding_level=%p\n" 1605169689Skan "NAMESPACE_LEVEL (global_namespace)=%p\n", 1606132718Skan (void *) current_binding_level, (void *) class_binding_level, 1607169689Skan (void *) NAMESPACE_LEVEL (global_namespace)); 1608132718Skan if (class_binding_level) 1609132718Skan { 1610132718Skan for (b = class_binding_level; b; b = b->level_chain) 1611132718Skan if (b == current_binding_level) 1612132718Skan break; 1613132718Skan if (b) 1614132718Skan b = class_binding_level; 1615132718Skan else 1616132718Skan b = current_binding_level; 1617132718Skan } 1618132718Skan else 1619132718Skan b = current_binding_level; 1620132718Skan print_other_binding_stack (b); 1621132718Skan fprintf (stderr, "global:\n"); 1622132718Skan print_binding_level (NAMESPACE_LEVEL (global_namespace)); 1623132718Skan} 1624132718Skan 1625132718Skan/* Return the type associated with id. */ 1626132718Skan 1627132718Skantree 1628132718Skanidentifier_type_value (tree id) 1629132718Skan{ 1630132718Skan timevar_push (TV_NAME_LOOKUP); 1631132718Skan /* There is no type with that name, anywhere. */ 1632132718Skan if (REAL_IDENTIFIER_TYPE_VALUE (id) == NULL_TREE) 1633132718Skan POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE); 1634132718Skan /* This is not the type marker, but the real thing. */ 1635132718Skan if (REAL_IDENTIFIER_TYPE_VALUE (id) != global_type_node) 1636132718Skan POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, REAL_IDENTIFIER_TYPE_VALUE (id)); 1637132718Skan /* Have to search for it. It must be on the global level, now. 1638132718Skan Ask lookup_name not to return non-types. */ 1639169689Skan id = lookup_name_real (id, 2, 1, /*block_p=*/true, 0, LOOKUP_COMPLAIN); 1640132718Skan if (id) 1641132718Skan POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, TREE_TYPE (id)); 1642132718Skan POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE); 1643132718Skan} 1644132718Skan 1645132718Skan/* Return the IDENTIFIER_GLOBAL_VALUE of T, for use in common code, since 1646132718Skan the definition of IDENTIFIER_GLOBAL_VALUE is different for C and C++. */ 1647132718Skan 1648132718Skantree 1649132718Skanidentifier_global_value (tree t) 1650132718Skan{ 1651132718Skan return IDENTIFIER_GLOBAL_VALUE (t); 1652132718Skan} 1653132718Skan 1654132718Skan/* Push a definition of struct, union or enum tag named ID. into 1655132718Skan binding_level B. DECL is a TYPE_DECL for the type. We assume that 1656132718Skan the tag ID is not already defined. */ 1657132718Skan 1658132718Skanstatic void 1659132718Skanset_identifier_type_value_with_scope (tree id, tree decl, cxx_scope *b) 1660132718Skan{ 1661132718Skan tree type; 1662132718Skan 1663132718Skan if (b->kind != sk_namespace) 1664132718Skan { 1665132718Skan /* Shadow the marker, not the real thing, so that the marker 1666132718Skan gets restored later. */ 1667132718Skan tree old_type_value = REAL_IDENTIFIER_TYPE_VALUE (id); 1668132718Skan b->type_shadowed 1669132718Skan = tree_cons (id, old_type_value, b->type_shadowed); 1670132718Skan type = decl ? TREE_TYPE (decl) : NULL_TREE; 1671169689Skan TREE_TYPE (b->type_shadowed) = type; 1672132718Skan } 1673132718Skan else 1674132718Skan { 1675132718Skan cxx_binding *binding = 1676132718Skan binding_for_name (NAMESPACE_LEVEL (current_namespace), id); 1677169689Skan gcc_assert (decl); 1678169689Skan if (binding->value) 1679169689Skan supplement_binding (binding, decl); 1680132718Skan else 1681169689Skan binding->value = decl; 1682169689Skan 1683132718Skan /* Store marker instead of real type. */ 1684132718Skan type = global_type_node; 1685132718Skan } 1686132718Skan SET_IDENTIFIER_TYPE_VALUE (id, type); 1687132718Skan} 1688132718Skan 1689132718Skan/* As set_identifier_type_value_with_scope, but using 1690132718Skan current_binding_level. */ 1691132718Skan 1692132718Skanvoid 1693132718Skanset_identifier_type_value (tree id, tree decl) 1694132718Skan{ 1695132718Skan set_identifier_type_value_with_scope (id, decl, current_binding_level); 1696132718Skan} 1697132718Skan 1698132718Skan/* Return the name for the constructor (or destructor) for the 1699132718Skan specified class TYPE. When given a template, this routine doesn't 1700132718Skan lose the specialization. */ 1701132718Skan 1702169689Skanstatic inline tree 1703132718Skanconstructor_name_full (tree type) 1704132718Skan{ 1705169689Skan return TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type)); 1706132718Skan} 1707132718Skan 1708132718Skan/* Return the name for the constructor (or destructor) for the 1709132718Skan specified class. When given a template, return the plain 1710132718Skan unspecialized name. */ 1711132718Skan 1712132718Skantree 1713132718Skanconstructor_name (tree type) 1714132718Skan{ 1715132718Skan tree name; 1716132718Skan name = constructor_name_full (type); 1717132718Skan if (IDENTIFIER_TEMPLATE (name)) 1718132718Skan name = IDENTIFIER_TEMPLATE (name); 1719132718Skan return name; 1720132718Skan} 1721132718Skan 1722132718Skan/* Returns TRUE if NAME is the name for the constructor for TYPE. */ 1723132718Skan 1724132718Skanbool 1725132718Skanconstructor_name_p (tree name, tree type) 1726132718Skan{ 1727132718Skan tree ctor_name; 1728132718Skan 1729132718Skan if (!name) 1730132718Skan return false; 1731169689Skan 1732132718Skan if (TREE_CODE (name) != IDENTIFIER_NODE) 1733132718Skan return false; 1734169689Skan 1735132718Skan ctor_name = constructor_name_full (type); 1736132718Skan if (name == ctor_name) 1737132718Skan return true; 1738132718Skan if (IDENTIFIER_TEMPLATE (ctor_name) 1739132718Skan && name == IDENTIFIER_TEMPLATE (ctor_name)) 1740132718Skan return true; 1741132718Skan return false; 1742132718Skan} 1743132718Skan 1744132718Skan/* Counter used to create anonymous type names. */ 1745132718Skan 1746132718Skanstatic GTY(()) int anon_cnt; 1747132718Skan 1748132718Skan/* Return an IDENTIFIER which can be used as a name for 1749132718Skan anonymous structs and unions. */ 1750132718Skan 1751132718Skantree 1752132718Skanmake_anon_name (void) 1753132718Skan{ 1754132718Skan char buf[32]; 1755132718Skan 1756132718Skan sprintf (buf, ANON_AGGRNAME_FORMAT, anon_cnt++); 1757132718Skan return get_identifier (buf); 1758132718Skan} 1759132718Skan 1760169689Skan/* Return (from the stack of) the BINDING, if any, established at SCOPE. */ 1761132718Skan 1762132718Skanstatic inline cxx_binding * 1763132718Skanfind_binding (cxx_scope *scope, cxx_binding *binding) 1764132718Skan{ 1765132718Skan timevar_push (TV_NAME_LOOKUP); 1766132718Skan 1767132718Skan for (; binding != NULL; binding = binding->previous) 1768132718Skan if (binding->scope == scope) 1769132718Skan POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, binding); 1770132718Skan 1771132718Skan POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, (cxx_binding *)0); 1772132718Skan} 1773132718Skan 1774132718Skan/* Return the binding for NAME in SCOPE, if any. Otherwise, return NULL. */ 1775132718Skan 1776132718Skanstatic inline cxx_binding * 1777132718Skancxx_scope_find_binding_for_name (cxx_scope *scope, tree name) 1778132718Skan{ 1779132718Skan cxx_binding *b = IDENTIFIER_NAMESPACE_BINDINGS (name); 1780132718Skan if (b) 1781132718Skan { 1782132718Skan /* Fold-in case where NAME is used only once. */ 1783132718Skan if (scope == b->scope && b->previous == NULL) 1784169689Skan return b; 1785132718Skan return find_binding (scope, b); 1786132718Skan } 1787132718Skan return NULL; 1788132718Skan} 1789132718Skan 1790132718Skan/* Always returns a binding for name in scope. If no binding is 1791132718Skan found, make a new one. */ 1792132718Skan 1793132718Skanstatic cxx_binding * 1794132718Skanbinding_for_name (cxx_scope *scope, tree name) 1795132718Skan{ 1796132718Skan cxx_binding *result; 1797132718Skan 1798132718Skan result = cxx_scope_find_binding_for_name (scope, name); 1799132718Skan if (result) 1800132718Skan return result; 1801132718Skan /* Not found, make a new one. */ 1802132718Skan result = cxx_binding_make (NULL, NULL); 1803132718Skan result->previous = IDENTIFIER_NAMESPACE_BINDINGS (name); 1804132718Skan result->scope = scope; 1805132718Skan result->is_local = false; 1806132718Skan result->value_is_inherited = false; 1807132718Skan IDENTIFIER_NAMESPACE_BINDINGS (name) = result; 1808132718Skan return result; 1809132718Skan} 1810132718Skan 1811132718Skan/* Insert another USING_DECL into the current binding level, returning 1812132718Skan this declaration. If this is a redeclaration, do nothing, and 1813132718Skan return NULL_TREE if this not in namespace scope (in namespace 1814132718Skan scope, a using decl might extend any previous bindings). */ 1815132718Skan 1816169689Skanstatic tree 1817132718Skanpush_using_decl (tree scope, tree name) 1818132718Skan{ 1819132718Skan tree decl; 1820132718Skan 1821132718Skan timevar_push (TV_NAME_LOOKUP); 1822169689Skan gcc_assert (TREE_CODE (scope) == NAMESPACE_DECL); 1823169689Skan gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE); 1824132718Skan for (decl = current_binding_level->usings; decl; decl = TREE_CHAIN (decl)) 1825169689Skan if (USING_DECL_SCOPE (decl) == scope && DECL_NAME (decl) == name) 1826132718Skan break; 1827132718Skan if (decl) 1828132718Skan POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, 1829169689Skan namespace_bindings_p () ? decl : NULL_TREE); 1830169689Skan decl = build_lang_decl (USING_DECL, name, NULL_TREE); 1831169689Skan USING_DECL_SCOPE (decl) = scope; 1832132718Skan TREE_CHAIN (decl) = current_binding_level->usings; 1833132718Skan current_binding_level->usings = decl; 1834132718Skan POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl); 1835132718Skan} 1836132718Skan 1837132718Skan/* Same as pushdecl, but define X in binding-level LEVEL. We rely on the 1838132718Skan caller to set DECL_CONTEXT properly. */ 1839132718Skan 1840132718Skantree 1841169689Skanpushdecl_with_scope (tree x, cxx_scope *level, bool is_friend) 1842132718Skan{ 1843132718Skan struct cp_binding_level *b; 1844132718Skan tree function_decl = current_function_decl; 1845132718Skan 1846132718Skan timevar_push (TV_NAME_LOOKUP); 1847132718Skan current_function_decl = NULL_TREE; 1848132718Skan if (level->kind == sk_class) 1849132718Skan { 1850132718Skan b = class_binding_level; 1851132718Skan class_binding_level = level; 1852132718Skan pushdecl_class_level (x); 1853132718Skan class_binding_level = b; 1854132718Skan } 1855132718Skan else 1856132718Skan { 1857132718Skan b = current_binding_level; 1858132718Skan current_binding_level = level; 1859169689Skan x = pushdecl_maybe_friend (x, is_friend); 1860132718Skan current_binding_level = b; 1861132718Skan } 1862132718Skan current_function_decl = function_decl; 1863132718Skan POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x); 1864132718Skan} 1865132718Skan 1866132718Skan/* DECL is a FUNCTION_DECL for a non-member function, which may have 1867132718Skan other definitions already in place. We get around this by making 1868132718Skan the value of the identifier point to a list of all the things that 1869132718Skan want to be referenced by that name. It is then up to the users of 1870132718Skan that name to decide what to do with that list. 1871132718Skan 1872132718Skan DECL may also be a TEMPLATE_DECL, with a FUNCTION_DECL in its 1873132718Skan DECL_TEMPLATE_RESULT. It is dealt with the same way. 1874132718Skan 1875132718Skan FLAGS is a bitwise-or of the following values: 1876132718Skan PUSH_LOCAL: Bind DECL in the current scope, rather than at 1877169689Skan namespace scope. 1878132718Skan PUSH_USING: DECL is being pushed as the result of a using 1879169689Skan declaration. 1880132718Skan 1881169689Skan IS_FRIEND is true if this is a friend declaration. 1882169689Skan 1883132718Skan The value returned may be a previous declaration if we guessed wrong 1884132718Skan about what language DECL should belong to (C or C++). Otherwise, 1885132718Skan it's always DECL (and never something that's not a _DECL). */ 1886132718Skan 1887132718Skanstatic tree 1888169689Skanpush_overloaded_decl (tree decl, int flags, bool is_friend) 1889132718Skan{ 1890132718Skan tree name = DECL_NAME (decl); 1891132718Skan tree old; 1892132718Skan tree new_binding; 1893132718Skan int doing_global = (namespace_bindings_p () || !(flags & PUSH_LOCAL)); 1894132718Skan 1895132718Skan timevar_push (TV_NAME_LOOKUP); 1896132718Skan if (doing_global) 1897132718Skan old = namespace_binding (name, DECL_CONTEXT (decl)); 1898132718Skan else 1899169689Skan old = lookup_name_innermost_nonclass_level (name); 1900132718Skan 1901132718Skan if (old) 1902132718Skan { 1903132718Skan if (TREE_CODE (old) == TYPE_DECL && DECL_ARTIFICIAL (old)) 1904132718Skan { 1905132718Skan tree t = TREE_TYPE (old); 1906132718Skan if (IS_AGGR_TYPE (t) && warn_shadow 1907132718Skan && (! DECL_IN_SYSTEM_HEADER (decl) 1908132718Skan || ! DECL_IN_SYSTEM_HEADER (old))) 1909169689Skan warning (0, "%q#D hides constructor for %q#T", decl, t); 1910132718Skan old = NULL_TREE; 1911132718Skan } 1912132718Skan else if (is_overloaded_fn (old)) 1913169689Skan { 1914169689Skan tree tmp; 1915132718Skan 1916132718Skan for (tmp = old; tmp; tmp = OVL_NEXT (tmp)) 1917132718Skan { 1918132718Skan tree fn = OVL_CURRENT (tmp); 1919169689Skan tree dup; 1920132718Skan 1921132718Skan if (TREE_CODE (tmp) == OVERLOAD && OVL_USED (tmp) 1922132718Skan && !(flags & PUSH_USING) 1923132718Skan && compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)), 1924146895Skan TYPE_ARG_TYPES (TREE_TYPE (decl))) 1925146895Skan && ! decls_match (fn, decl)) 1926169689Skan error ("%q#D conflicts with previous using declaration %q#D", 1927169689Skan decl, fn); 1928132718Skan 1929169689Skan dup = duplicate_decls (decl, fn, is_friend); 1930169689Skan /* If DECL was a redeclaration of FN -- even an invalid 1931169689Skan one -- pass that information along to our caller. */ 1932169689Skan if (dup == fn || dup == error_mark_node) 1933169689Skan POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, dup); 1934132718Skan } 1935169689Skan 1936169689Skan /* We don't overload implicit built-ins. duplicate_decls() 1937169689Skan may fail to merge the decls if the new decl is e.g. a 1938169689Skan template function. */ 1939169689Skan if (TREE_CODE (old) == FUNCTION_DECL 1940169689Skan && DECL_ANTICIPATED (old) 1941169689Skan && !DECL_HIDDEN_FRIEND_P (old)) 1942169689Skan old = NULL; 1943132718Skan } 1944132718Skan else if (old == error_mark_node) 1945132718Skan /* Ignore the undefined symbol marker. */ 1946132718Skan old = NULL_TREE; 1947132718Skan else 1948132718Skan { 1949169689Skan error ("previous non-function declaration %q+#D", old); 1950169689Skan error ("conflicts with function declaration %q#D", decl); 1951132718Skan POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl); 1952132718Skan } 1953132718Skan } 1954132718Skan 1955132718Skan if (old || TREE_CODE (decl) == TEMPLATE_DECL 1956132718Skan /* If it's a using declaration, we always need to build an OVERLOAD, 1957132718Skan because it's the only way to remember that the declaration comes 1958132718Skan from 'using', and have the lookup behave correctly. */ 1959132718Skan || (flags & PUSH_USING)) 1960132718Skan { 1961132718Skan if (old && TREE_CODE (old) != OVERLOAD) 1962132718Skan new_binding = ovl_cons (decl, ovl_cons (old, NULL_TREE)); 1963132718Skan else 1964132718Skan new_binding = ovl_cons (decl, old); 1965132718Skan if (flags & PUSH_USING) 1966132718Skan OVL_USED (new_binding) = 1; 1967132718Skan } 1968132718Skan else 1969132718Skan /* NAME is not ambiguous. */ 1970132718Skan new_binding = decl; 1971132718Skan 1972132718Skan if (doing_global) 1973132718Skan set_namespace_binding (name, current_namespace, new_binding); 1974132718Skan else 1975132718Skan { 1976132718Skan /* We only create an OVERLOAD if there was a previous binding at 1977132718Skan this level, or if decl is a template. In the former case, we 1978132718Skan need to remove the old binding and replace it with the new 1979132718Skan binding. We must also run through the NAMES on the binding 1980132718Skan level where the name was bound to update the chain. */ 1981132718Skan 1982132718Skan if (TREE_CODE (new_binding) == OVERLOAD && old) 1983132718Skan { 1984132718Skan tree *d; 1985132718Skan 1986132718Skan for (d = &IDENTIFIER_BINDING (name)->scope->names; 1987132718Skan *d; 1988132718Skan d = &TREE_CHAIN (*d)) 1989132718Skan if (*d == old 1990132718Skan || (TREE_CODE (*d) == TREE_LIST 1991132718Skan && TREE_VALUE (*d) == old)) 1992132718Skan { 1993132718Skan if (TREE_CODE (*d) == TREE_LIST) 1994132718Skan /* Just replace the old binding with the new. */ 1995132718Skan TREE_VALUE (*d) = new_binding; 1996132718Skan else 1997132718Skan /* Build a TREE_LIST to wrap the OVERLOAD. */ 1998132718Skan *d = tree_cons (NULL_TREE, new_binding, 1999132718Skan TREE_CHAIN (*d)); 2000132718Skan 2001132718Skan /* And update the cxx_binding node. */ 2002132718Skan IDENTIFIER_BINDING (name)->value = new_binding; 2003132718Skan POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl); 2004132718Skan } 2005132718Skan 2006132718Skan /* We should always find a previous binding in this case. */ 2007169689Skan gcc_unreachable (); 2008132718Skan } 2009132718Skan 2010132718Skan /* Install the new binding. */ 2011132718Skan push_local_binding (name, new_binding, flags); 2012132718Skan } 2013132718Skan 2014132718Skan POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl); 2015132718Skan} 2016132718Skan 2017132718Skan/* Check a non-member using-declaration. Return the name and scope 2018132718Skan being used, and the USING_DECL, or NULL_TREE on failure. */ 2019132718Skan 2020132718Skanstatic tree 2021132718Skanvalidate_nonmember_using_decl (tree decl, tree scope, tree name) 2022132718Skan{ 2023169689Skan /* [namespace.udecl] 2024169689Skan A using-declaration for a class member shall be a 2025169689Skan member-declaration. */ 2026169689Skan if (TYPE_P (scope)) 2027169689Skan { 2028169689Skan error ("%qT is not a namespace", scope); 2029169689Skan return NULL_TREE; 2030169689Skan } 2031169689Skan else if (scope == error_mark_node) 2032169689Skan return NULL_TREE; 2033169689Skan 2034132718Skan if (TREE_CODE (decl) == TEMPLATE_ID_EXPR) 2035132718Skan { 2036132718Skan /* 7.3.3/5 2037132718Skan A using-declaration shall not name a template-id. */ 2038169689Skan error ("a using-declaration cannot specify a template-id. " 2039169689Skan "Try %<using %D%>", name); 2040132718Skan return NULL_TREE; 2041132718Skan } 2042132718Skan 2043132718Skan if (TREE_CODE (decl) == NAMESPACE_DECL) 2044132718Skan { 2045169689Skan error ("namespace %qD not allowed in using-declaration", decl); 2046132718Skan return NULL_TREE; 2047132718Skan } 2048132718Skan 2049132718Skan if (TREE_CODE (decl) == SCOPE_REF) 2050132718Skan { 2051132718Skan /* It's a nested name with template parameter dependent scope. 2052132718Skan This can only be using-declaration for class member. */ 2053169689Skan error ("%qT is not a namespace", TREE_OPERAND (decl, 0)); 2054132718Skan return NULL_TREE; 2055132718Skan } 2056132718Skan 2057132718Skan if (is_overloaded_fn (decl)) 2058132718Skan decl = get_first_fn (decl); 2059132718Skan 2060169689Skan gcc_assert (DECL_P (decl)); 2061132718Skan 2062132718Skan /* Make a USING_DECL. */ 2063132718Skan return push_using_decl (scope, name); 2064132718Skan} 2065132718Skan 2066132718Skan/* Process local and global using-declarations. */ 2067132718Skan 2068132718Skanstatic void 2069132718Skando_nonmember_using_decl (tree scope, tree name, tree oldval, tree oldtype, 2070169689Skan tree *newval, tree *newtype) 2071132718Skan{ 2072169689Skan struct scope_binding decls = EMPTY_SCOPE_BINDING; 2073132718Skan 2074132718Skan *newval = *newtype = NULL_TREE; 2075132718Skan if (!qualified_lookup_using_namespace (name, scope, &decls, 0)) 2076132718Skan /* Lookup error */ 2077132718Skan return; 2078132718Skan 2079132718Skan if (!decls.value && !decls.type) 2080132718Skan { 2081169689Skan error ("%qD not declared", name); 2082132718Skan return; 2083132718Skan } 2084132718Skan 2085169689Skan /* It is impossible to overload a built-in function; any explicit 2086169689Skan declaration eliminates the built-in declaration. So, if OLDVAL 2087169689Skan is a built-in, then we can just pretend it isn't there. */ 2088169689Skan if (oldval 2089169689Skan && TREE_CODE (oldval) == FUNCTION_DECL 2090169689Skan && DECL_ANTICIPATED (oldval) 2091169689Skan && !DECL_HIDDEN_FRIEND_P (oldval)) 2092169689Skan oldval = NULL_TREE; 2093169689Skan 2094132718Skan /* Check for using functions. */ 2095132718Skan if (decls.value && is_overloaded_fn (decls.value)) 2096132718Skan { 2097132718Skan tree tmp, tmp1; 2098132718Skan 2099132718Skan if (oldval && !is_overloaded_fn (oldval)) 2100132718Skan { 2101132718Skan if (!DECL_IMPLICIT_TYPEDEF_P (oldval)) 2102169689Skan error ("%qD is already declared in this scope", name); 2103132718Skan oldval = NULL_TREE; 2104132718Skan } 2105132718Skan 2106132718Skan *newval = oldval; 2107132718Skan for (tmp = decls.value; tmp; tmp = OVL_NEXT (tmp)) 2108132718Skan { 2109132718Skan tree new_fn = OVL_CURRENT (tmp); 2110132718Skan 2111132718Skan /* [namespace.udecl] 2112132718Skan 2113132718Skan If a function declaration in namespace scope or block 2114132718Skan scope has the same name and the same parameter types as a 2115132718Skan function introduced by a using declaration the program is 2116132718Skan ill-formed. */ 2117132718Skan for (tmp1 = oldval; tmp1; tmp1 = OVL_NEXT (tmp1)) 2118132718Skan { 2119132718Skan tree old_fn = OVL_CURRENT (tmp1); 2120132718Skan 2121169689Skan if (new_fn == old_fn) 2122169689Skan /* The function already exists in the current namespace. */ 2123169689Skan break; 2124132718Skan else if (OVL_USED (tmp1)) 2125169689Skan continue; /* this is a using decl */ 2126132718Skan else if (compparms (TYPE_ARG_TYPES (TREE_TYPE (new_fn)), 2127169689Skan TYPE_ARG_TYPES (TREE_TYPE (old_fn)))) 2128132718Skan { 2129169689Skan gcc_assert (!DECL_ANTICIPATED (old_fn) 2130169689Skan || DECL_HIDDEN_FRIEND_P (old_fn)); 2131169689Skan 2132169689Skan /* There was already a non-using declaration in 2133132718Skan this scope with the same parameter types. If both 2134169689Skan are the same extern "C" functions, that's ok. */ 2135169689Skan if (decls_match (new_fn, old_fn)) 2136169689Skan break; 2137169689Skan else 2138132718Skan { 2139169689Skan error ("%qD is already declared in this scope", name); 2140132718Skan break; 2141132718Skan } 2142132718Skan } 2143132718Skan } 2144132718Skan 2145132718Skan /* If we broke out of the loop, there's no reason to add 2146132718Skan this function to the using declarations for this 2147132718Skan scope. */ 2148132718Skan if (tmp1) 2149132718Skan continue; 2150169689Skan 2151132718Skan /* If we are adding to an existing OVERLOAD, then we no 2152132718Skan longer know the type of the set of functions. */ 2153132718Skan if (*newval && TREE_CODE (*newval) == OVERLOAD) 2154132718Skan TREE_TYPE (*newval) = unknown_type_node; 2155132718Skan /* Add this new function to the set. */ 2156132718Skan *newval = build_overload (OVL_CURRENT (tmp), *newval); 2157132718Skan /* If there is only one function, then we use its type. (A 2158132718Skan using-declaration naming a single function can be used in 2159132718Skan contexts where overload resolution cannot be 2160132718Skan performed.) */ 2161132718Skan if (TREE_CODE (*newval) != OVERLOAD) 2162132718Skan { 2163132718Skan *newval = ovl_cons (*newval, NULL_TREE); 2164132718Skan TREE_TYPE (*newval) = TREE_TYPE (OVL_CURRENT (tmp)); 2165132718Skan } 2166132718Skan OVL_USED (*newval) = 1; 2167132718Skan } 2168132718Skan } 2169169689Skan else 2170132718Skan { 2171132718Skan *newval = decls.value; 2172132718Skan if (oldval && !decls_match (*newval, oldval)) 2173169689Skan error ("%qD is already declared in this scope", name); 2174132718Skan } 2175132718Skan 2176132718Skan *newtype = decls.type; 2177132718Skan if (oldtype && *newtype && !same_type_p (oldtype, *newtype)) 2178132718Skan { 2179169689Skan error ("using declaration %qD introduced ambiguous type %qT", 2180169689Skan name, oldtype); 2181132718Skan return; 2182132718Skan } 2183132718Skan} 2184132718Skan 2185132718Skan/* Process a using-declaration at function scope. */ 2186132718Skan 2187132718Skanvoid 2188132718Skando_local_using_decl (tree decl, tree scope, tree name) 2189132718Skan{ 2190132718Skan tree oldval, oldtype, newval, newtype; 2191169689Skan tree orig_decl = decl; 2192132718Skan 2193132718Skan decl = validate_nonmember_using_decl (decl, scope, name); 2194132718Skan if (decl == NULL_TREE) 2195132718Skan return; 2196132718Skan 2197132718Skan if (building_stmt_tree () 2198132718Skan && at_function_scope_p ()) 2199169689Skan add_decl_expr (decl); 2200132718Skan 2201169689Skan oldval = lookup_name_innermost_nonclass_level (name); 2202132718Skan oldtype = lookup_type_current_level (name); 2203132718Skan 2204132718Skan do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype); 2205132718Skan 2206132718Skan if (newval) 2207132718Skan { 2208132718Skan if (is_overloaded_fn (newval)) 2209132718Skan { 2210132718Skan tree fn, term; 2211132718Skan 2212132718Skan /* We only need to push declarations for those functions 2213132718Skan that were not already bound in the current level. 2214132718Skan The old value might be NULL_TREE, it might be a single 2215132718Skan function, or an OVERLOAD. */ 2216132718Skan if (oldval && TREE_CODE (oldval) == OVERLOAD) 2217132718Skan term = OVL_FUNCTION (oldval); 2218132718Skan else 2219132718Skan term = oldval; 2220169689Skan for (fn = newval; fn && OVL_CURRENT (fn) != term; 2221132718Skan fn = OVL_NEXT (fn)) 2222169689Skan push_overloaded_decl (OVL_CURRENT (fn), 2223169689Skan PUSH_LOCAL | PUSH_USING, 2224169689Skan false); 2225132718Skan } 2226132718Skan else 2227132718Skan push_local_binding (name, newval, PUSH_USING); 2228132718Skan } 2229132718Skan if (newtype) 2230132718Skan { 2231132718Skan push_local_binding (name, newtype, PUSH_USING); 2232132718Skan set_identifier_type_value (name, newtype); 2233132718Skan } 2234132718Skan 2235169689Skan /* Emit debug info. */ 2236169689Skan if (!processing_template_decl) 2237169689Skan cp_emit_debug_info_for_using (orig_decl, current_scope()); 2238132718Skan} 2239132718Skan 2240132718Skan/* Returns true if ROOT (a namespace, class, or function) encloses 2241132718Skan CHILD. CHILD may be either a class type or a namespace. */ 2242132718Skan 2243132718Skanbool 2244132718Skanis_ancestor (tree root, tree child) 2245132718Skan{ 2246169689Skan gcc_assert ((TREE_CODE (root) == NAMESPACE_DECL 2247169689Skan || TREE_CODE (root) == FUNCTION_DECL 2248169689Skan || CLASS_TYPE_P (root))); 2249169689Skan gcc_assert ((TREE_CODE (child) == NAMESPACE_DECL 2250169689Skan || CLASS_TYPE_P (child))); 2251169689Skan 2252132718Skan /* The global namespace encloses everything. */ 2253132718Skan if (root == global_namespace) 2254132718Skan return true; 2255132718Skan 2256132718Skan while (true) 2257132718Skan { 2258132718Skan /* If we've run out of scopes, stop. */ 2259132718Skan if (!child) 2260132718Skan return false; 2261132718Skan /* If we've reached the ROOT, it encloses CHILD. */ 2262132718Skan if (root == child) 2263132718Skan return true; 2264132718Skan /* Go out one level. */ 2265132718Skan if (TYPE_P (child)) 2266132718Skan child = TYPE_NAME (child); 2267132718Skan child = DECL_CONTEXT (child); 2268132718Skan } 2269132718Skan} 2270132718Skan 2271169689Skan/* Enter the class or namespace scope indicated by T suitable for name 2272169689Skan lookup. T can be arbitrary scope, not necessary nested inside the 2273169689Skan current scope. Returns a non-null scope to pop iff pop_scope 2274169689Skan should be called later to exit this scope. */ 2275132718Skan 2276169689Skantree 2277132718Skanpush_scope (tree t) 2278132718Skan{ 2279132718Skan if (TREE_CODE (t) == NAMESPACE_DECL) 2280132718Skan push_decl_namespace (t); 2281132718Skan else if (CLASS_TYPE_P (t)) 2282132718Skan { 2283132718Skan if (!at_class_scope_p () 2284132718Skan || !same_type_p (current_class_type, t)) 2285132718Skan push_nested_class (t); 2286132718Skan else 2287132718Skan /* T is the same as the current scope. There is therefore no 2288132718Skan need to re-enter the scope. Since we are not actually 2289132718Skan pushing a new scope, our caller should not call 2290132718Skan pop_scope. */ 2291169689Skan t = NULL_TREE; 2292132718Skan } 2293132718Skan 2294169689Skan return t; 2295132718Skan} 2296132718Skan 2297132718Skan/* Leave scope pushed by push_scope. */ 2298132718Skan 2299132718Skanvoid 2300132718Skanpop_scope (tree t) 2301132718Skan{ 2302132718Skan if (TREE_CODE (t) == NAMESPACE_DECL) 2303132718Skan pop_decl_namespace (); 2304132718Skan else if CLASS_TYPE_P (t) 2305132718Skan pop_nested_class (); 2306132718Skan} 2307169689Skan 2308169689Skan/* Subroutine of push_inner_scope. */ 2309169689Skan 2310169689Skanstatic void 2311169689Skanpush_inner_scope_r (tree outer, tree inner) 2312169689Skan{ 2313169689Skan tree prev; 2314169689Skan 2315169689Skan if (outer == inner 2316169689Skan || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner))) 2317169689Skan return; 2318169689Skan 2319169689Skan prev = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner)); 2320169689Skan if (outer != prev) 2321169689Skan push_inner_scope_r (outer, prev); 2322169689Skan if (TREE_CODE (inner) == NAMESPACE_DECL) 2323169689Skan { 2324169689Skan struct cp_binding_level *save_template_parm = 0; 2325169689Skan /* Temporary take out template parameter scopes. They are saved 2326169689Skan in reversed order in save_template_parm. */ 2327169689Skan while (current_binding_level->kind == sk_template_parms) 2328169689Skan { 2329169689Skan struct cp_binding_level *b = current_binding_level; 2330169689Skan current_binding_level = b->level_chain; 2331169689Skan b->level_chain = save_template_parm; 2332169689Skan save_template_parm = b; 2333169689Skan } 2334169689Skan 2335169689Skan resume_scope (NAMESPACE_LEVEL (inner)); 2336169689Skan current_namespace = inner; 2337169689Skan 2338169689Skan /* Restore template parameter scopes. */ 2339169689Skan while (save_template_parm) 2340169689Skan { 2341169689Skan struct cp_binding_level *b = save_template_parm; 2342169689Skan save_template_parm = b->level_chain; 2343169689Skan b->level_chain = current_binding_level; 2344169689Skan current_binding_level = b; 2345169689Skan } 2346169689Skan } 2347169689Skan else 2348169689Skan pushclass (inner); 2349169689Skan} 2350169689Skan 2351169689Skan/* Enter the scope INNER from current scope. INNER must be a scope 2352169689Skan nested inside current scope. This works with both name lookup and 2353169689Skan pushing name into scope. In case a template parameter scope is present, 2354169689Skan namespace is pushed under the template parameter scope according to 2355169689Skan name lookup rule in 14.6.1/6. 2356169689Skan 2357169689Skan Return the former current scope suitable for pop_inner_scope. */ 2358169689Skan 2359169689Skantree 2360169689Skanpush_inner_scope (tree inner) 2361169689Skan{ 2362169689Skan tree outer = current_scope (); 2363169689Skan if (!outer) 2364169689Skan outer = current_namespace; 2365169689Skan 2366169689Skan push_inner_scope_r (outer, inner); 2367169689Skan return outer; 2368169689Skan} 2369169689Skan 2370169689Skan/* Exit the current scope INNER back to scope OUTER. */ 2371169689Skan 2372169689Skanvoid 2373169689Skanpop_inner_scope (tree outer, tree inner) 2374169689Skan{ 2375169689Skan if (outer == inner 2376169689Skan || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner))) 2377169689Skan return; 2378169689Skan 2379169689Skan while (outer != inner) 2380169689Skan { 2381169689Skan if (TREE_CODE (inner) == NAMESPACE_DECL) 2382169689Skan { 2383169689Skan struct cp_binding_level *save_template_parm = 0; 2384169689Skan /* Temporary take out template parameter scopes. They are saved 2385169689Skan in reversed order in save_template_parm. */ 2386169689Skan while (current_binding_level->kind == sk_template_parms) 2387169689Skan { 2388169689Skan struct cp_binding_level *b = current_binding_level; 2389169689Skan current_binding_level = b->level_chain; 2390169689Skan b->level_chain = save_template_parm; 2391169689Skan save_template_parm = b; 2392169689Skan } 2393169689Skan 2394169689Skan pop_namespace (); 2395169689Skan 2396169689Skan /* Restore template parameter scopes. */ 2397169689Skan while (save_template_parm) 2398169689Skan { 2399169689Skan struct cp_binding_level *b = save_template_parm; 2400169689Skan save_template_parm = b->level_chain; 2401169689Skan b->level_chain = current_binding_level; 2402169689Skan current_binding_level = b; 2403169689Skan } 2404169689Skan } 2405169689Skan else 2406169689Skan popclass (); 2407169689Skan 2408169689Skan inner = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner)); 2409169689Skan } 2410169689Skan} 2411132718Skan 2412132718Skan/* Do a pushlevel for class declarations. */ 2413132718Skan 2414132718Skanvoid 2415132718Skanpushlevel_class (void) 2416132718Skan{ 2417132718Skan if (ENABLE_SCOPE_CHECKING) 2418132718Skan is_class_level = 1; 2419132718Skan 2420132718Skan class_binding_level = begin_scope (sk_class, current_class_type); 2421132718Skan} 2422132718Skan 2423132718Skan/* ...and a poplevel for class declarations. */ 2424132718Skan 2425132718Skanvoid 2426132718Skanpoplevel_class (void) 2427132718Skan{ 2428132718Skan struct cp_binding_level *level = class_binding_level; 2429169689Skan cp_class_binding *cb; 2430169689Skan size_t i; 2431132718Skan tree shadowed; 2432132718Skan 2433132718Skan timevar_push (TV_NAME_LOOKUP); 2434169689Skan gcc_assert (level != 0); 2435132718Skan 2436169689Skan /* If we're leaving a toplevel class, cache its binding level. */ 2437169689Skan if (current_class_depth == 1) 2438169689Skan previous_class_level = level; 2439132718Skan for (shadowed = level->type_shadowed; 2440132718Skan shadowed; 2441132718Skan shadowed = TREE_CHAIN (shadowed)) 2442132718Skan SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed), TREE_VALUE (shadowed)); 2443132718Skan 2444132718Skan /* Remove the bindings for all of the class-level declarations. */ 2445169689Skan if (level->class_shadowed) 2446169689Skan { 2447169689Skan for (i = 0; 2448169689Skan VEC_iterate (cp_class_binding, level->class_shadowed, i, cb); 2449169689Skan ++i) 2450169689Skan IDENTIFIER_BINDING (cb->identifier) = cb->base.previous; 2451169689Skan ggc_free (level->class_shadowed); 2452169689Skan level->class_shadowed = NULL; 2453169689Skan } 2454132718Skan 2455132718Skan /* Now, pop out of the binding level which we created up in the 2456132718Skan `pushlevel_class' routine. */ 2457132718Skan if (ENABLE_SCOPE_CHECKING) 2458132718Skan is_class_level = 1; 2459132718Skan 2460132718Skan leave_scope (); 2461132718Skan timevar_pop (TV_NAME_LOOKUP); 2462132718Skan} 2463132718Skan 2464169689Skan/* Set INHERITED_VALUE_BINDING_P on BINDING to true or false, as 2465169689Skan appropriate. DECL is the value to which a name has just been 2466169689Skan bound. CLASS_TYPE is the class in which the lookup occurred. */ 2467132718Skan 2468169689Skanstatic void 2469169689Skanset_inherited_value_binding_p (cxx_binding *binding, tree decl, 2470169689Skan tree class_type) 2471132718Skan{ 2472132718Skan if (binding->value == decl && TREE_CODE (decl) != TREE_LIST) 2473132718Skan { 2474169689Skan tree context; 2475169689Skan 2476132718Skan if (TREE_CODE (decl) == OVERLOAD) 2477132718Skan context = CP_DECL_CONTEXT (OVL_CURRENT (decl)); 2478132718Skan else 2479132718Skan { 2480169689Skan gcc_assert (DECL_P (decl)); 2481132718Skan context = context_for_name_lookup (decl); 2482132718Skan } 2483132718Skan 2484169689Skan if (is_properly_derived_from (class_type, context)) 2485132718Skan INHERITED_VALUE_BINDING_P (binding) = 1; 2486132718Skan else 2487132718Skan INHERITED_VALUE_BINDING_P (binding) = 0; 2488132718Skan } 2489132718Skan else if (binding->value == decl) 2490169689Skan /* We only encounter a TREE_LIST when there is an ambiguity in the 2491169689Skan base classes. Such an ambiguity can be overridden by a 2492169689Skan definition in this class. */ 2493132718Skan INHERITED_VALUE_BINDING_P (binding) = 1; 2494169689Skan else 2495169689Skan INHERITED_VALUE_BINDING_P (binding) = 0; 2496132718Skan} 2497132718Skan 2498132718Skan/* Make the declaration of X appear in CLASS scope. */ 2499132718Skan 2500132718Skanbool 2501132718Skanpushdecl_class_level (tree x) 2502132718Skan{ 2503132718Skan tree name; 2504132718Skan bool is_valid = true; 2505132718Skan 2506132718Skan timevar_push (TV_NAME_LOOKUP); 2507132718Skan /* Get the name of X. */ 2508132718Skan if (TREE_CODE (x) == OVERLOAD) 2509132718Skan name = DECL_NAME (get_first_fn (x)); 2510132718Skan else 2511132718Skan name = DECL_NAME (x); 2512132718Skan 2513132718Skan if (name) 2514132718Skan { 2515132718Skan is_valid = push_class_level_binding (name, x); 2516132718Skan if (TREE_CODE (x) == TYPE_DECL) 2517132718Skan set_identifier_type_value (name, x); 2518132718Skan } 2519132718Skan else if (ANON_AGGR_TYPE_P (TREE_TYPE (x))) 2520132718Skan { 2521132718Skan /* If X is an anonymous aggregate, all of its members are 2522132718Skan treated as if they were members of the class containing the 2523132718Skan aggregate, for naming purposes. */ 2524132718Skan tree f; 2525132718Skan 2526132718Skan for (f = TYPE_FIELDS (TREE_TYPE (x)); f; f = TREE_CHAIN (f)) 2527132718Skan { 2528132718Skan location_t save_location = input_location; 2529132718Skan input_location = DECL_SOURCE_LOCATION (f); 2530132718Skan if (!pushdecl_class_level (f)) 2531132718Skan is_valid = false; 2532132718Skan input_location = save_location; 2533132718Skan } 2534132718Skan } 2535169689Skan POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, is_valid); 2536169689Skan} 2537132718Skan 2538169689Skan/* Return the BINDING (if any) for NAME in SCOPE, which is a class 2539169689Skan scope. If the value returned is non-NULL, and the PREVIOUS field 2540169689Skan is not set, callers must set the PREVIOUS field explicitly. */ 2541169689Skan 2542169689Skanstatic cxx_binding * 2543169689Skanget_class_binding (tree name, cxx_scope *scope) 2544169689Skan{ 2545169689Skan tree class_type; 2546169689Skan tree type_binding; 2547169689Skan tree value_binding; 2548169689Skan cxx_binding *binding; 2549169689Skan 2550169689Skan class_type = scope->this_entity; 2551169689Skan 2552169689Skan /* Get the type binding. */ 2553169689Skan type_binding = lookup_member (class_type, name, 2554169689Skan /*protect=*/2, /*want_type=*/true); 2555169689Skan /* Get the value binding. */ 2556169689Skan value_binding = lookup_member (class_type, name, 2557169689Skan /*protect=*/2, /*want_type=*/false); 2558169689Skan 2559169689Skan if (value_binding 2560169689Skan && (TREE_CODE (value_binding) == TYPE_DECL 2561169689Skan || DECL_CLASS_TEMPLATE_P (value_binding) 2562169689Skan || (TREE_CODE (value_binding) == TREE_LIST 2563169689Skan && TREE_TYPE (value_binding) == error_mark_node 2564169689Skan && (TREE_CODE (TREE_VALUE (value_binding)) 2565169689Skan == TYPE_DECL)))) 2566169689Skan /* We found a type binding, even when looking for a non-type 2567169689Skan binding. This means that we already processed this binding 2568169689Skan above. */ 2569169689Skan ; 2570169689Skan else if (value_binding) 2571169689Skan { 2572169689Skan if (TREE_CODE (value_binding) == TREE_LIST 2573169689Skan && TREE_TYPE (value_binding) == error_mark_node) 2574169689Skan /* NAME is ambiguous. */ 2575169689Skan ; 2576169689Skan else if (BASELINK_P (value_binding)) 2577169689Skan /* NAME is some overloaded functions. */ 2578169689Skan value_binding = BASELINK_FUNCTIONS (value_binding); 2579169689Skan } 2580169689Skan 2581169689Skan /* If we found either a type binding or a value binding, create a 2582169689Skan new binding object. */ 2583169689Skan if (type_binding || value_binding) 2584169689Skan { 2585169689Skan binding = new_class_binding (name, 2586169689Skan value_binding, 2587169689Skan type_binding, 2588169689Skan scope); 2589169689Skan /* This is a class-scope binding, not a block-scope binding. */ 2590169689Skan LOCAL_BINDING_P (binding) = 0; 2591169689Skan set_inherited_value_binding_p (binding, value_binding, class_type); 2592169689Skan } 2593169689Skan else 2594169689Skan binding = NULL; 2595169689Skan 2596169689Skan return binding; 2597132718Skan} 2598132718Skan 2599132718Skan/* Make the declaration(s) of X appear in CLASS scope under the name 2600132718Skan NAME. Returns true if the binding is valid. */ 2601132718Skan 2602132718Skanbool 2603132718Skanpush_class_level_binding (tree name, tree x) 2604132718Skan{ 2605132718Skan cxx_binding *binding; 2606169689Skan tree decl = x; 2607169689Skan bool ok; 2608132718Skan 2609132718Skan timevar_push (TV_NAME_LOOKUP); 2610132718Skan /* The class_binding_level will be NULL if x is a template 2611132718Skan parameter name in a member template. */ 2612132718Skan if (!class_binding_level) 2613132718Skan POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true); 2614132718Skan 2615169689Skan if (name == error_mark_node) 2616169689Skan POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, false); 2617132718Skan 2618169689Skan /* Check for invalid member names. */ 2619169689Skan gcc_assert (TYPE_BEING_DEFINED (current_class_type)); 2620169689Skan /* We could have been passed a tree list if this is an ambiguous 2621169689Skan declaration. If so, pull the declaration out because 2622169689Skan check_template_shadow will not handle a TREE_LIST. */ 2623169689Skan if (TREE_CODE (decl) == TREE_LIST 2624169689Skan && TREE_TYPE (decl) == error_mark_node) 2625169689Skan decl = TREE_VALUE (decl); 2626169689Skan 2627169689Skan check_template_shadow (decl); 2628169689Skan 2629132718Skan /* [class.mem] 2630132718Skan 2631132718Skan If T is the name of a class, then each of the following shall 2632132718Skan have a name different from T: 2633132718Skan 2634132718Skan -- every static data member of class T; 2635132718Skan 2636132718Skan -- every member of class T that is itself a type; 2637132718Skan 2638132718Skan -- every enumerator of every member of class T that is an 2639132718Skan enumerated type; 2640132718Skan 2641132718Skan -- every member of every anonymous union that is a member of 2642132718Skan class T. 2643132718Skan 2644132718Skan (Non-static data members were also forbidden to have the same 2645132718Skan name as T until TC1.) */ 2646132718Skan if ((TREE_CODE (x) == VAR_DECL 2647132718Skan || TREE_CODE (x) == CONST_DECL 2648132718Skan || (TREE_CODE (x) == TYPE_DECL 2649132718Skan && !DECL_SELF_REFERENCE_P (x)) 2650132718Skan /* A data member of an anonymous union. */ 2651132718Skan || (TREE_CODE (x) == FIELD_DECL 2652132718Skan && DECL_CONTEXT (x) != current_class_type)) 2653132718Skan && DECL_NAME (x) == constructor_name (current_class_type)) 2654132718Skan { 2655132718Skan tree scope = context_for_name_lookup (x); 2656132718Skan if (TYPE_P (scope) && same_type_p (scope, current_class_type)) 2657132718Skan { 2658169689Skan error ("%qD has the same name as the class in which it is " 2659169689Skan "declared", 2660132718Skan x); 2661132718Skan POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, false); 2662132718Skan } 2663132718Skan } 2664132718Skan 2665169689Skan /* Get the current binding for NAME in this class, if any. */ 2666132718Skan binding = IDENTIFIER_BINDING (name); 2667169689Skan if (!binding || binding->scope != class_binding_level) 2668169689Skan { 2669169689Skan binding = get_class_binding (name, class_binding_level); 2670169689Skan /* If a new binding was created, put it at the front of the 2671169689Skan IDENTIFIER_BINDING list. */ 2672169689Skan if (binding) 2673169689Skan { 2674169689Skan binding->previous = IDENTIFIER_BINDING (name); 2675169689Skan IDENTIFIER_BINDING (name) = binding; 2676169689Skan } 2677169689Skan } 2678169689Skan 2679169689Skan /* If there is already a binding, then we may need to update the 2680169689Skan current value. */ 2681132718Skan if (binding && binding->value) 2682132718Skan { 2683132718Skan tree bval = binding->value; 2684132718Skan tree old_decl = NULL_TREE; 2685132718Skan 2686132718Skan if (INHERITED_VALUE_BINDING_P (binding)) 2687132718Skan { 2688132718Skan /* If the old binding was from a base class, and was for a 2689169689Skan tag name, slide it over to make room for the new binding. 2690169689Skan The old binding is still visible if explicitly qualified 2691169689Skan with a class-key. */ 2692132718Skan if (TREE_CODE (bval) == TYPE_DECL && DECL_ARTIFICIAL (bval) 2693132718Skan && !(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x))) 2694132718Skan { 2695132718Skan old_decl = binding->type; 2696132718Skan binding->type = bval; 2697132718Skan binding->value = NULL_TREE; 2698132718Skan INHERITED_VALUE_BINDING_P (binding) = 0; 2699132718Skan } 2700132718Skan else 2701169689Skan { 2702169689Skan old_decl = bval; 2703169689Skan /* Any inherited type declaration is hidden by the type 2704169689Skan declaration in the derived class. */ 2705169689Skan if (TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x)) 2706169689Skan binding->type = NULL_TREE; 2707169689Skan } 2708132718Skan } 2709132718Skan else if (TREE_CODE (x) == OVERLOAD && is_overloaded_fn (bval)) 2710132718Skan old_decl = bval; 2711132718Skan else if (TREE_CODE (x) == USING_DECL && TREE_CODE (bval) == USING_DECL) 2712132718Skan POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true); 2713132718Skan else if (TREE_CODE (x) == USING_DECL && is_overloaded_fn (bval)) 2714132718Skan old_decl = bval; 2715132718Skan else if (TREE_CODE (bval) == USING_DECL && is_overloaded_fn (x)) 2716132718Skan POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true); 2717169689Skan 2718169689Skan if (old_decl && binding->scope == class_binding_level) 2719132718Skan { 2720169689Skan binding->value = x; 2721169689Skan /* It is always safe to clear INHERITED_VALUE_BINDING_P 2722169689Skan here. This function is only used to register bindings 2723169689Skan from with the class definition itself. */ 2724169689Skan INHERITED_VALUE_BINDING_P (binding) = 0; 2725169689Skan POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true); 2726132718Skan } 2727132718Skan } 2728132718Skan 2729169689Skan /* Note that we declared this value so that we can issue an error if 2730169689Skan this is an invalid redeclaration of a name already used for some 2731169689Skan other purpose. */ 2732169689Skan note_name_declared_in_class (name, decl); 2733169689Skan 2734132718Skan /* If we didn't replace an existing binding, put the binding on the 2735169689Skan stack of bindings for the identifier, and update the shadowed 2736169689Skan list. */ 2737169689Skan if (binding && binding->scope == class_binding_level) 2738169689Skan /* Supplement the existing binding. */ 2739169689Skan ok = supplement_binding (binding, decl); 2740169689Skan else 2741132718Skan { 2742169689Skan /* Create a new binding. */ 2743169689Skan push_binding (name, decl, class_binding_level); 2744169689Skan ok = true; 2745132718Skan } 2746132718Skan 2747169689Skan POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ok); 2748132718Skan} 2749132718Skan 2750169689Skan/* Process "using SCOPE::NAME" in a class scope. Return the 2751169689Skan USING_DECL created. */ 2752169689Skan 2753132718Skantree 2754169689Skando_class_using_decl (tree scope, tree name) 2755132718Skan{ 2756169689Skan /* The USING_DECL returned by this function. */ 2757169689Skan tree value; 2758169689Skan /* The declaration (or declarations) name by this using 2759169689Skan declaration. NULL if we are in a template and cannot figure out 2760169689Skan what has been named. */ 2761169689Skan tree decl; 2762169689Skan /* True if SCOPE is a dependent type. */ 2763169689Skan bool scope_dependent_p; 2764169689Skan /* True if SCOPE::NAME is dependent. */ 2765169689Skan bool name_dependent_p; 2766169689Skan /* True if any of the bases of CURRENT_CLASS_TYPE are dependent. */ 2767169689Skan bool bases_dependent_p; 2768169689Skan tree binfo; 2769169689Skan tree base_binfo; 2770169689Skan int i; 2771169689Skan 2772169689Skan if (name == error_mark_node) 2773169689Skan return NULL_TREE; 2774169689Skan 2775169689Skan if (!scope || !TYPE_P (scope)) 2776132718Skan { 2777132718Skan error ("using-declaration for non-member at class scope"); 2778132718Skan return NULL_TREE; 2779132718Skan } 2780169689Skan 2781169689Skan /* Make sure the name is not invalid */ 2782132718Skan if (TREE_CODE (name) == BIT_NOT_EXPR) 2783132718Skan { 2784169689Skan error ("%<%T::%D%> names destructor", scope, name); 2785132718Skan return NULL_TREE; 2786132718Skan } 2787169689Skan if (constructor_name_p (name, scope)) 2788132718Skan { 2789169689Skan error ("%<%T::%D%> names constructor", scope, name); 2790169689Skan return NULL_TREE; 2791132718Skan } 2792169689Skan if (constructor_name_p (name, current_class_type)) 2793169689Skan { 2794169689Skan error ("%<%T::%D%> names constructor in %qT", 2795169689Skan scope, name, current_class_type); 2796169689Skan return NULL_TREE; 2797169689Skan } 2798132718Skan 2799169689Skan scope_dependent_p = dependent_type_p (scope); 2800169689Skan name_dependent_p = (scope_dependent_p 2801169689Skan || (IDENTIFIER_TYPENAME_P (name) 2802169689Skan && dependent_type_p (TREE_TYPE (name)))); 2803132718Skan 2804169689Skan bases_dependent_p = false; 2805169689Skan if (processing_template_decl) 2806169689Skan for (binfo = TYPE_BINFO (current_class_type), i = 0; 2807169689Skan BINFO_BASE_ITERATE (binfo, i, base_binfo); 2808169689Skan i++) 2809169689Skan if (dependent_type_p (TREE_TYPE (base_binfo))) 2810169689Skan { 2811169689Skan bases_dependent_p = true; 2812169689Skan break; 2813169689Skan } 2814169689Skan 2815169689Skan decl = NULL_TREE; 2816169689Skan 2817169689Skan /* From [namespace.udecl]: 2818169689Skan 2819169689Skan A using-declaration used as a member-declaration shall refer to a 2820169689Skan member of a base class of the class being defined. 2821169689Skan 2822169689Skan In general, we cannot check this constraint in a template because 2823169689Skan we do not know the entire set of base classes of the current 2824169689Skan class type. However, if all of the base classes are 2825169689Skan non-dependent, then we can avoid delaying the check until 2826169689Skan instantiation. */ 2827169689Skan if (!scope_dependent_p) 2828169689Skan { 2829169689Skan base_kind b_kind; 2830169689Skan binfo = lookup_base (current_class_type, scope, ba_any, &b_kind); 2831169689Skan if (b_kind < bk_proper_base) 2832169689Skan { 2833169689Skan if (!bases_dependent_p) 2834169689Skan { 2835169689Skan error_not_base_type (scope, current_class_type); 2836169689Skan return NULL_TREE; 2837169689Skan } 2838169689Skan } 2839169689Skan else if (!name_dependent_p) 2840169689Skan { 2841169689Skan decl = lookup_member (binfo, name, 0, false); 2842169689Skan if (!decl) 2843169689Skan { 2844169689Skan error ("no members matching %<%T::%D%> in %q#T", scope, name, 2845169689Skan scope); 2846169689Skan return NULL_TREE; 2847169689Skan } 2848169689Skan /* The binfo from which the functions came does not matter. */ 2849169689Skan if (BASELINK_P (decl)) 2850169689Skan decl = BASELINK_FUNCTIONS (decl); 2851169689Skan } 2852169689Skan } 2853169689Skan 2854169689Skan value = build_lang_decl (USING_DECL, name, NULL_TREE); 2855169689Skan USING_DECL_DECLS (value) = decl; 2856169689Skan USING_DECL_SCOPE (value) = scope; 2857169689Skan DECL_DEPENDENT_P (value) = !decl; 2858169689Skan 2859132718Skan return value; 2860132718Skan} 2861132718Skan 2862132718Skan 2863132718Skan/* Return the binding value for name in scope. */ 2864132718Skan 2865132718Skantree 2866132718Skannamespace_binding (tree name, tree scope) 2867132718Skan{ 2868132718Skan cxx_binding *binding; 2869132718Skan 2870132718Skan if (scope == NULL) 2871132718Skan scope = global_namespace; 2872169689Skan else 2873169689Skan /* Unnecessary for the global namespace because it can't be an alias. */ 2874169689Skan scope = ORIGINAL_NAMESPACE (scope); 2875169689Skan 2876132718Skan binding = cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (scope), name); 2877132718Skan 2878132718Skan return binding ? binding->value : NULL_TREE; 2879132718Skan} 2880132718Skan 2881132718Skan/* Set the binding value for name in scope. */ 2882132718Skan 2883132718Skanvoid 2884132718Skanset_namespace_binding (tree name, tree scope, tree val) 2885132718Skan{ 2886132718Skan cxx_binding *b; 2887132718Skan 2888132718Skan timevar_push (TV_NAME_LOOKUP); 2889132718Skan if (scope == NULL_TREE) 2890132718Skan scope = global_namespace; 2891132718Skan b = binding_for_name (NAMESPACE_LEVEL (scope), name); 2892132718Skan if (!b->value || TREE_CODE (val) == OVERLOAD || val == error_mark_node) 2893132718Skan b->value = val; 2894132718Skan else 2895132718Skan supplement_binding (b, val); 2896132718Skan timevar_pop (TV_NAME_LOOKUP); 2897132718Skan} 2898132718Skan 2899132718Skan/* Set the context of a declaration to scope. Complain if we are not 2900132718Skan outside scope. */ 2901132718Skan 2902132718Skanvoid 2903132718Skanset_decl_namespace (tree decl, tree scope, bool friendp) 2904132718Skan{ 2905169689Skan tree old, fn; 2906169689Skan 2907132718Skan /* Get rid of namespace aliases. */ 2908132718Skan scope = ORIGINAL_NAMESPACE (scope); 2909169689Skan 2910132718Skan /* It is ok for friends to be qualified in parallel space. */ 2911132718Skan if (!friendp && !is_ancestor (current_namespace, scope)) 2912169689Skan error ("declaration of %qD not in a namespace surrounding %qD", 2913169689Skan decl, scope); 2914132718Skan DECL_CONTEXT (decl) = FROB_CONTEXT (scope); 2915169689Skan 2916169689Skan /* Writing "int N::i" to declare a variable within "N" is invalid. */ 2917169689Skan if (scope == current_namespace) 2918132718Skan { 2919161651Skan if (at_namespace_scope_p ()) 2920169689Skan error ("explicit qualification in declaration of %qD", 2921169689Skan decl); 2922161651Skan return; 2923161651Skan } 2924161651Skan 2925169689Skan /* See whether this has been declared in the namespace. */ 2926169689Skan old = lookup_qualified_name (scope, DECL_NAME (decl), false, true); 2927220150Smm if (old == error_mark_node) 2928169689Skan /* No old declaration at all. */ 2929169689Skan goto complain; 2930169689Skan if (!is_overloaded_fn (decl)) 2931169689Skan /* Don't compare non-function decls with decls_match here, since 2932169689Skan it can't check for the correct constness at this 2933169689Skan point. pushdecl will find those errors later. */ 2934169689Skan return; 2935169689Skan /* Since decl is a function, old should contain a function decl. */ 2936169689Skan if (!is_overloaded_fn (old)) 2937169689Skan goto complain; 2938169689Skan fn = OVL_CURRENT (old); 2939169689Skan if (!is_associated_namespace (scope, CP_DECL_CONTEXT (fn))) 2940169689Skan goto complain; 2941169689Skan /* A template can be explicitly specialized in any namespace. */ 2942169689Skan if (processing_explicit_instantiation) 2943169689Skan return; 2944169689Skan if (processing_template_decl || processing_specialization) 2945169689Skan /* We have not yet called push_template_decl to turn a 2946169689Skan FUNCTION_DECL into a TEMPLATE_DECL, so the declarations won't 2947169689Skan match. But, we'll check later, when we construct the 2948169689Skan template. */ 2949169689Skan return; 2950169689Skan /* Instantiations or specializations of templates may be declared as 2951169689Skan friends in any namespace. */ 2952169689Skan if (friendp && DECL_USE_TEMPLATE (decl)) 2953169689Skan return; 2954169689Skan if (is_overloaded_fn (old)) 2955169689Skan { 2956169689Skan for (; old; old = OVL_NEXT (old)) 2957169689Skan if (decls_match (decl, OVL_CURRENT (old))) 2958169689Skan return; 2959169689Skan } 2960169689Skan else if (decls_match (decl, old)) 2961169689Skan return; 2962132718Skan complain: 2963169689Skan error ("%qD should have been declared inside %qD", decl, scope); 2964169689Skan} 2965132718Skan 2966132718Skan/* Return the namespace where the current declaration is declared. */ 2967132718Skan 2968169689Skanstatic tree 2969132718Skancurrent_decl_namespace (void) 2970132718Skan{ 2971132718Skan tree result; 2972132718Skan /* If we have been pushed into a different namespace, use it. */ 2973132718Skan if (decl_namespace_list) 2974132718Skan return TREE_PURPOSE (decl_namespace_list); 2975132718Skan 2976132718Skan if (current_class_type) 2977146895Skan result = decl_namespace_context (current_class_type); 2978132718Skan else if (current_function_decl) 2979146895Skan result = decl_namespace_context (current_function_decl); 2980169689Skan else 2981132718Skan result = current_namespace; 2982132718Skan return result; 2983132718Skan} 2984132718Skan 2985132718Skan/* Push into the scope of the NAME namespace. If NAME is NULL_TREE, then we 2986132718Skan select a name that is unique to this compilation unit. */ 2987132718Skan 2988132718Skanvoid 2989132718Skanpush_namespace (tree name) 2990132718Skan{ 2991169689Skan push_namespace_with_attribs (name, NULL_TREE); 2992169689Skan} 2993169689Skan 2994169689Skan/* Same, but specify attributes to apply to the namespace. The attributes 2995169689Skan only apply to the current namespace-body, not to any later extensions. */ 2996169689Skan 2997169689Skanvoid 2998169689Skanpush_namespace_with_attribs (tree name, tree attributes) 2999169689Skan{ 3000132718Skan tree d = NULL_TREE; 3001132718Skan int need_new = 1; 3002132718Skan int implicit_use = 0; 3003132718Skan bool anon = !name; 3004132718Skan 3005132718Skan timevar_push (TV_NAME_LOOKUP); 3006169689Skan 3007132718Skan /* We should not get here if the global_namespace is not yet constructed 3008132718Skan nor if NAME designates the global namespace: The global scope is 3009132718Skan constructed elsewhere. */ 3010169689Skan gcc_assert (global_namespace != NULL && name != global_scope_name); 3011132718Skan 3012132718Skan if (anon) 3013132718Skan { 3014132718Skan /* The name of anonymous namespace is unique for the translation 3015169689Skan unit. */ 3016132718Skan if (!anonymous_namespace_name) 3017169689Skan anonymous_namespace_name = get_file_function_name ('N'); 3018132718Skan name = anonymous_namespace_name; 3019132718Skan d = IDENTIFIER_NAMESPACE_VALUE (name); 3020132718Skan if (d) 3021169689Skan /* Reopening anonymous namespace. */ 3022169689Skan need_new = 0; 3023132718Skan implicit_use = 1; 3024132718Skan } 3025132718Skan else 3026132718Skan { 3027132718Skan /* Check whether this is an extended namespace definition. */ 3028132718Skan d = IDENTIFIER_NAMESPACE_VALUE (name); 3029132718Skan if (d != NULL_TREE && TREE_CODE (d) == NAMESPACE_DECL) 3030169689Skan { 3031169689Skan need_new = 0; 3032169689Skan if (DECL_NAMESPACE_ALIAS (d)) 3033169689Skan { 3034169689Skan error ("namespace alias %qD not allowed here, assuming %qD", 3035169689Skan d, DECL_NAMESPACE_ALIAS (d)); 3036169689Skan d = DECL_NAMESPACE_ALIAS (d); 3037169689Skan } 3038169689Skan } 3039132718Skan } 3040132718Skan 3041132718Skan if (need_new) 3042132718Skan { 3043132718Skan /* Make a new namespace, binding the name to it. */ 3044132718Skan d = build_lang_decl (NAMESPACE_DECL, name, void_type_node); 3045132718Skan DECL_CONTEXT (d) = FROB_CONTEXT (current_namespace); 3046169689Skan /* The name of this namespace is not visible to other translation 3047169689Skan units if it is an anonymous namespace or member thereof. */ 3048169689Skan if (anon || decl_anon_ns_mem_p (current_namespace)) 3049169689Skan TREE_PUBLIC (d) = 0; 3050169689Skan else 3051169689Skan TREE_PUBLIC (d) = 1; 3052169689Skan pushdecl (d); 3053132718Skan if (anon) 3054132718Skan { 3055132718Skan /* Clear DECL_NAME for the benefit of debugging back ends. */ 3056132718Skan SET_DECL_ASSEMBLER_NAME (d, name); 3057132718Skan DECL_NAME (d) = NULL_TREE; 3058132718Skan } 3059132718Skan begin_scope (sk_namespace, d); 3060132718Skan } 3061132718Skan else 3062132718Skan resume_scope (NAMESPACE_LEVEL (d)); 3063132718Skan 3064132718Skan if (implicit_use) 3065132718Skan do_using_directive (d); 3066132718Skan /* Enter the name space. */ 3067132718Skan current_namespace = d; 3068132718Skan 3069169689Skan#ifdef HANDLE_PRAGMA_VISIBILITY 3070169689Skan /* Clear has_visibility in case a previous namespace-definition had a 3071169689Skan visibility attribute and this one doesn't. */ 3072169689Skan current_binding_level->has_visibility = 0; 3073169689Skan for (d = attributes; d; d = TREE_CHAIN (d)) 3074169689Skan { 3075169689Skan tree name = TREE_PURPOSE (d); 3076169689Skan tree args = TREE_VALUE (d); 3077169689Skan tree x; 3078169689Skan 3079169689Skan if (! is_attribute_p ("visibility", name)) 3080169689Skan { 3081169689Skan warning (OPT_Wattributes, "%qs attribute directive ignored", 3082169689Skan IDENTIFIER_POINTER (name)); 3083169689Skan continue; 3084169689Skan } 3085169689Skan 3086169689Skan x = args ? TREE_VALUE (args) : NULL_TREE; 3087169689Skan if (x == NULL_TREE || TREE_CODE (x) != STRING_CST || TREE_CHAIN (args)) 3088169689Skan { 3089169689Skan warning (OPT_Wattributes, "%qs attribute requires a single NTBS argument", 3090169689Skan IDENTIFIER_POINTER (name)); 3091169689Skan continue; 3092169689Skan } 3093169689Skan 3094169689Skan current_binding_level->has_visibility = 1; 3095169689Skan push_visibility (TREE_STRING_POINTER (x)); 3096169689Skan goto found; 3097169689Skan } 3098169689Skan found: 3099169689Skan#endif 3100169689Skan 3101132718Skan timevar_pop (TV_NAME_LOOKUP); 3102132718Skan} 3103132718Skan 3104132718Skan/* Pop from the scope of the current namespace. */ 3105132718Skan 3106132718Skanvoid 3107132718Skanpop_namespace (void) 3108132718Skan{ 3109169689Skan gcc_assert (current_namespace != global_namespace); 3110132718Skan current_namespace = CP_DECL_CONTEXT (current_namespace); 3111132718Skan /* The binding level is not popped, as it might be re-opened later. */ 3112132718Skan leave_scope (); 3113132718Skan} 3114132718Skan 3115132718Skan/* Push into the scope of the namespace NS, even if it is deeply 3116132718Skan nested within another namespace. */ 3117132718Skan 3118132718Skanvoid 3119132718Skanpush_nested_namespace (tree ns) 3120132718Skan{ 3121132718Skan if (ns == global_namespace) 3122132718Skan push_to_top_level (); 3123132718Skan else 3124132718Skan { 3125132718Skan push_nested_namespace (CP_DECL_CONTEXT (ns)); 3126132718Skan push_namespace (DECL_NAME (ns)); 3127132718Skan } 3128132718Skan} 3129132718Skan 3130132718Skan/* Pop back from the scope of the namespace NS, which was previously 3131132718Skan entered with push_nested_namespace. */ 3132132718Skan 3133132718Skanvoid 3134132718Skanpop_nested_namespace (tree ns) 3135132718Skan{ 3136132718Skan timevar_push (TV_NAME_LOOKUP); 3137132718Skan while (ns != global_namespace) 3138132718Skan { 3139132718Skan pop_namespace (); 3140132718Skan ns = CP_DECL_CONTEXT (ns); 3141132718Skan } 3142132718Skan 3143132718Skan pop_from_top_level (); 3144132718Skan timevar_pop (TV_NAME_LOOKUP); 3145132718Skan} 3146132718Skan 3147132718Skan/* Temporarily set the namespace for the current declaration. */ 3148132718Skan 3149132718Skanvoid 3150132718Skanpush_decl_namespace (tree decl) 3151132718Skan{ 3152132718Skan if (TREE_CODE (decl) != NAMESPACE_DECL) 3153146895Skan decl = decl_namespace_context (decl); 3154132718Skan decl_namespace_list = tree_cons (ORIGINAL_NAMESPACE (decl), 3155169689Skan NULL_TREE, decl_namespace_list); 3156132718Skan} 3157132718Skan 3158132718Skan/* [namespace.memdef]/2 */ 3159132718Skan 3160132718Skanvoid 3161132718Skanpop_decl_namespace (void) 3162132718Skan{ 3163132718Skan decl_namespace_list = TREE_CHAIN (decl_namespace_list); 3164132718Skan} 3165132718Skan 3166169689Skan/* Return the namespace that is the common ancestor 3167132718Skan of two given namespaces. */ 3168132718Skan 3169132718Skanstatic tree 3170132718Skannamespace_ancestor (tree ns1, tree ns2) 3171132718Skan{ 3172132718Skan timevar_push (TV_NAME_LOOKUP); 3173132718Skan if (is_ancestor (ns1, ns2)) 3174132718Skan POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ns1); 3175132718Skan POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, 3176169689Skan namespace_ancestor (CP_DECL_CONTEXT (ns1), ns2)); 3177132718Skan} 3178132718Skan 3179132718Skan/* Process a namespace-alias declaration. */ 3180132718Skan 3181132718Skanvoid 3182132718Skando_namespace_alias (tree alias, tree namespace) 3183132718Skan{ 3184161651Skan if (namespace == error_mark_node) 3185161651Skan return; 3186132718Skan 3187169689Skan gcc_assert (TREE_CODE (namespace) == NAMESPACE_DECL); 3188161651Skan 3189132718Skan namespace = ORIGINAL_NAMESPACE (namespace); 3190132718Skan 3191132718Skan /* Build the alias. */ 3192169689Skan alias = build_lang_decl (NAMESPACE_DECL, alias, void_type_node); 3193132718Skan DECL_NAMESPACE_ALIAS (alias) = namespace; 3194132718Skan DECL_EXTERNAL (alias) = 1; 3195169689Skan DECL_CONTEXT (alias) = FROB_CONTEXT (current_scope ()); 3196132718Skan pushdecl (alias); 3197169689Skan 3198169689Skan /* Emit debug info for namespace alias. */ 3199169689Skan (*debug_hooks->global_decl) (alias); 3200132718Skan} 3201132718Skan 3202132718Skan/* Like pushdecl, only it places X in the current namespace, 3203132718Skan if appropriate. */ 3204132718Skan 3205132718Skantree 3206169689Skanpushdecl_namespace_level (tree x, bool is_friend) 3207132718Skan{ 3208132718Skan struct cp_binding_level *b = current_binding_level; 3209132718Skan tree t; 3210132718Skan 3211132718Skan timevar_push (TV_NAME_LOOKUP); 3212169689Skan t = pushdecl_with_scope (x, NAMESPACE_LEVEL (current_namespace), is_friend); 3213132718Skan 3214132718Skan /* Now, the type_shadowed stack may screw us. Munge it so it does 3215132718Skan what we want. */ 3216169689Skan if (TREE_CODE (t) == TYPE_DECL) 3217132718Skan { 3218169689Skan tree name = DECL_NAME (t); 3219132718Skan tree newval; 3220132718Skan tree *ptr = (tree *)0; 3221132718Skan for (; !global_scope_p (b); b = b->level_chain) 3222169689Skan { 3223169689Skan tree shadowed = b->type_shadowed; 3224169689Skan for (; shadowed; shadowed = TREE_CHAIN (shadowed)) 3225169689Skan if (TREE_PURPOSE (shadowed) == name) 3226169689Skan { 3227132718Skan ptr = &TREE_VALUE (shadowed); 3228132718Skan /* Can't break out of the loop here because sometimes 3229132718Skan a binding level will have duplicate bindings for 3230132718Skan PT names. It's gross, but I haven't time to fix it. */ 3231169689Skan } 3232169689Skan } 3233169689Skan newval = TREE_TYPE (t); 3234132718Skan if (ptr == (tree *)0) 3235169689Skan { 3236169689Skan /* @@ This shouldn't be needed. My test case "zstring.cc" trips 3237169689Skan up here if this is changed to an assertion. --KR */ 3238169689Skan SET_IDENTIFIER_TYPE_VALUE (name, t); 3239132718Skan } 3240132718Skan else 3241169689Skan { 3242132718Skan *ptr = newval; 3243169689Skan } 3244132718Skan } 3245132718Skan POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t); 3246132718Skan} 3247132718Skan 3248132718Skan/* Insert USED into the using list of USER. Set INDIRECT_flag if this 3249132718Skan directive is not directly from the source. Also find the common 3250132718Skan ancestor and let our users know about the new namespace */ 3251169689Skanstatic void 3252132718Skanadd_using_namespace (tree user, tree used, bool indirect) 3253132718Skan{ 3254132718Skan tree t; 3255132718Skan timevar_push (TV_NAME_LOOKUP); 3256132718Skan /* Using oneself is a no-op. */ 3257132718Skan if (user == used) 3258132718Skan { 3259132718Skan timevar_pop (TV_NAME_LOOKUP); 3260132718Skan return; 3261132718Skan } 3262169689Skan gcc_assert (TREE_CODE (user) == NAMESPACE_DECL); 3263169689Skan gcc_assert (TREE_CODE (used) == NAMESPACE_DECL); 3264132718Skan /* Check if we already have this. */ 3265132718Skan t = purpose_member (used, DECL_NAMESPACE_USING (user)); 3266132718Skan if (t != NULL_TREE) 3267132718Skan { 3268132718Skan if (!indirect) 3269132718Skan /* Promote to direct usage. */ 3270132718Skan TREE_INDIRECT_USING (t) = 0; 3271132718Skan timevar_pop (TV_NAME_LOOKUP); 3272132718Skan return; 3273132718Skan } 3274132718Skan 3275132718Skan /* Add used to the user's using list. */ 3276169689Skan DECL_NAMESPACE_USING (user) 3277169689Skan = tree_cons (used, namespace_ancestor (user, used), 3278132718Skan DECL_NAMESPACE_USING (user)); 3279132718Skan 3280132718Skan TREE_INDIRECT_USING (DECL_NAMESPACE_USING (user)) = indirect; 3281132718Skan 3282132718Skan /* Add user to the used's users list. */ 3283132718Skan DECL_NAMESPACE_USERS (used) 3284132718Skan = tree_cons (user, 0, DECL_NAMESPACE_USERS (used)); 3285132718Skan 3286132718Skan /* Recursively add all namespaces used. */ 3287132718Skan for (t = DECL_NAMESPACE_USING (used); t; t = TREE_CHAIN (t)) 3288132718Skan /* indirect usage */ 3289132718Skan add_using_namespace (user, TREE_PURPOSE (t), 1); 3290132718Skan 3291132718Skan /* Tell everyone using us about the new used namespaces. */ 3292132718Skan for (t = DECL_NAMESPACE_USERS (user); t; t = TREE_CHAIN (t)) 3293132718Skan add_using_namespace (TREE_PURPOSE (t), used, 1); 3294132718Skan timevar_pop (TV_NAME_LOOKUP); 3295132718Skan} 3296132718Skan 3297132718Skan/* Process a using-declaration not appearing in class or local scope. */ 3298132718Skan 3299132718Skanvoid 3300132718Skando_toplevel_using_decl (tree decl, tree scope, tree name) 3301132718Skan{ 3302132718Skan tree oldval, oldtype, newval, newtype; 3303169689Skan tree orig_decl = decl; 3304132718Skan cxx_binding *binding; 3305132718Skan 3306132718Skan decl = validate_nonmember_using_decl (decl, scope, name); 3307132718Skan if (decl == NULL_TREE) 3308132718Skan return; 3309169689Skan 3310132718Skan binding = binding_for_name (NAMESPACE_LEVEL (current_namespace), name); 3311132718Skan 3312132718Skan oldval = binding->value; 3313132718Skan oldtype = binding->type; 3314132718Skan 3315132718Skan do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype); 3316132718Skan 3317169689Skan /* Emit debug info. */ 3318169689Skan if (!processing_template_decl) 3319169689Skan cp_emit_debug_info_for_using (orig_decl, current_namespace); 3320169689Skan 3321132718Skan /* Copy declarations found. */ 3322132718Skan if (newval) 3323132718Skan binding->value = newval; 3324132718Skan if (newtype) 3325132718Skan binding->type = newtype; 3326132718Skan} 3327132718Skan 3328132718Skan/* Process a using-directive. */ 3329132718Skan 3330132718Skanvoid 3331132718Skando_using_directive (tree namespace) 3332132718Skan{ 3333169689Skan tree context = NULL_TREE; 3334169689Skan 3335161651Skan if (namespace == error_mark_node) 3336161651Skan return; 3337161651Skan 3338169689Skan gcc_assert (TREE_CODE (namespace) == NAMESPACE_DECL); 3339161651Skan 3340132718Skan if (building_stmt_tree ()) 3341132718Skan add_stmt (build_stmt (USING_STMT, namespace)); 3342132718Skan namespace = ORIGINAL_NAMESPACE (namespace); 3343161651Skan 3344132718Skan if (!toplevel_bindings_p ()) 3345169689Skan { 3346169689Skan push_using_directive (namespace); 3347169689Skan context = current_scope (); 3348169689Skan } 3349132718Skan else 3350169689Skan { 3351169689Skan /* direct usage */ 3352169689Skan add_using_namespace (current_namespace, namespace, 0); 3353169689Skan if (current_namespace != global_namespace) 3354169689Skan context = current_namespace; 3355169689Skan } 3356169689Skan 3357169689Skan /* Emit debugging info. */ 3358169689Skan if (!processing_template_decl) 3359169689Skan (*debug_hooks->imported_module_or_decl) (namespace, context); 3360132718Skan} 3361132718Skan 3362132718Skan/* Deal with a using-directive seen by the parser. Currently we only 3363132718Skan handle attributes here, since they cannot appear inside a template. */ 3364132718Skan 3365132718Skanvoid 3366132718Skanparse_using_directive (tree namespace, tree attribs) 3367132718Skan{ 3368132718Skan tree a; 3369132718Skan 3370132718Skan do_using_directive (namespace); 3371132718Skan 3372132718Skan for (a = attribs; a; a = TREE_CHAIN (a)) 3373132718Skan { 3374132718Skan tree name = TREE_PURPOSE (a); 3375132718Skan if (is_attribute_p ("strong", name)) 3376132718Skan { 3377132718Skan if (!toplevel_bindings_p ()) 3378132718Skan error ("strong using only meaningful at namespace scope"); 3379146895Skan else if (namespace != error_mark_node) 3380169689Skan { 3381169689Skan if (!is_ancestor (current_namespace, namespace)) 3382169689Skan error ("current namespace %qD does not enclose strongly used namespace %qD", 3383169689Skan current_namespace, namespace); 3384169689Skan DECL_NAMESPACE_ASSOCIATIONS (namespace) 3385169689Skan = tree_cons (current_namespace, 0, 3386169689Skan DECL_NAMESPACE_ASSOCIATIONS (namespace)); 3387169689Skan } 3388132718Skan } 3389132718Skan else 3390169689Skan warning (OPT_Wattributes, "%qD attribute directive ignored", name); 3391132718Skan } 3392132718Skan} 3393132718Skan 3394132718Skan/* Like pushdecl, only it places X in the global scope if appropriate. 3395132718Skan Calls cp_finish_decl to register the variable, initializing it with 3396132718Skan *INIT, if INIT is non-NULL. */ 3397132718Skan 3398132718Skanstatic tree 3399169689Skanpushdecl_top_level_1 (tree x, tree *init, bool is_friend) 3400132718Skan{ 3401132718Skan timevar_push (TV_NAME_LOOKUP); 3402132718Skan push_to_top_level (); 3403169689Skan x = pushdecl_namespace_level (x, is_friend); 3404132718Skan if (init) 3405169689Skan finish_decl (x, *init, NULL_TREE); 3406132718Skan pop_from_top_level (); 3407132718Skan POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x); 3408132718Skan} 3409132718Skan 3410132718Skan/* Like pushdecl, only it places X in the global scope if appropriate. */ 3411132718Skan 3412132718Skantree 3413132718Skanpushdecl_top_level (tree x) 3414132718Skan{ 3415169689Skan return pushdecl_top_level_1 (x, NULL, false); 3416132718Skan} 3417132718Skan 3418169689Skan/* Like pushdecl_top_level, but adding the IS_FRIEND parameter. */ 3419169689Skan 3420169689Skantree 3421169689Skanpushdecl_top_level_maybe_friend (tree x, bool is_friend) 3422169689Skan{ 3423169689Skan return pushdecl_top_level_1 (x, NULL, is_friend); 3424169689Skan} 3425169689Skan 3426132718Skan/* Like pushdecl, only it places X in the global scope if 3427132718Skan appropriate. Calls cp_finish_decl to register the variable, 3428132718Skan initializing it with INIT. */ 3429132718Skan 3430132718Skantree 3431132718Skanpushdecl_top_level_and_finish (tree x, tree init) 3432132718Skan{ 3433169689Skan return pushdecl_top_level_1 (x, &init, false); 3434132718Skan} 3435132718Skan 3436132718Skan/* Combines two sets of overloaded functions into an OVERLOAD chain, removing 3437132718Skan duplicates. The first list becomes the tail of the result. 3438132718Skan 3439132718Skan The algorithm is O(n^2). We could get this down to O(n log n) by 3440132718Skan doing a sort on the addresses of the functions, if that becomes 3441132718Skan necessary. */ 3442132718Skan 3443132718Skanstatic tree 3444132718Skanmerge_functions (tree s1, tree s2) 3445132718Skan{ 3446132718Skan for (; s2; s2 = OVL_NEXT (s2)) 3447132718Skan { 3448132718Skan tree fn2 = OVL_CURRENT (s2); 3449132718Skan tree fns1; 3450132718Skan 3451132718Skan for (fns1 = s1; fns1; fns1 = OVL_NEXT (fns1)) 3452132718Skan { 3453132718Skan tree fn1 = OVL_CURRENT (fns1); 3454132718Skan 3455132718Skan /* If the function from S2 is already in S1, there is no 3456132718Skan need to add it again. For `extern "C"' functions, we 3457132718Skan might have two FUNCTION_DECLs for the same function, in 3458132718Skan different namespaces; again, we only need one of them. */ 3459169689Skan if (fn1 == fn2 3460132718Skan || (DECL_EXTERN_C_P (fn1) && DECL_EXTERN_C_P (fn2) 3461132718Skan && DECL_NAME (fn1) == DECL_NAME (fn2))) 3462132718Skan break; 3463132718Skan } 3464169689Skan 3465132718Skan /* If we exhausted all of the functions in S1, FN2 is new. */ 3466132718Skan if (!fns1) 3467132718Skan s1 = build_overload (fn2, s1); 3468132718Skan } 3469132718Skan return s1; 3470132718Skan} 3471132718Skan 3472132718Skan/* This should return an error not all definitions define functions. 3473132718Skan It is not an error if we find two functions with exactly the 3474132718Skan same signature, only if these are selected in overload resolution. 3475132718Skan old is the current set of bindings, new the freshly-found binding. 3476132718Skan XXX Do we want to give *all* candidates in case of ambiguity? 3477132718Skan XXX In what way should I treat extern declarations? 3478132718Skan XXX I don't want to repeat the entire duplicate_decls here */ 3479132718Skan 3480169689Skanstatic void 3481169689Skanambiguous_decl (tree name, struct scope_binding *old, cxx_binding *new, 3482169689Skan int flags) 3483132718Skan{ 3484132718Skan tree val, type; 3485169689Skan gcc_assert (old != NULL); 3486132718Skan /* Copy the value. */ 3487132718Skan val = new->value; 3488132718Skan if (val) 3489132718Skan switch (TREE_CODE (val)) 3490132718Skan { 3491132718Skan case TEMPLATE_DECL: 3492169689Skan /* If we expect types or namespaces, and not templates, 3493169689Skan or this is not a template class. */ 3494169689Skan if ((LOOKUP_QUALIFIERS_ONLY (flags) 3495169689Skan && !DECL_CLASS_TEMPLATE_P (val)) 3496169689Skan || hidden_name_p (val)) 3497169689Skan val = NULL_TREE; 3498169689Skan break; 3499132718Skan case TYPE_DECL: 3500169689Skan if (LOOKUP_NAMESPACES_ONLY (flags) || hidden_name_p (val)) 3501169689Skan val = NULL_TREE; 3502169689Skan break; 3503132718Skan case NAMESPACE_DECL: 3504169689Skan if (LOOKUP_TYPES_ONLY (flags)) 3505169689Skan val = NULL_TREE; 3506169689Skan break; 3507132718Skan case FUNCTION_DECL: 3508169689Skan /* Ignore built-in functions that are still anticipated. */ 3509169689Skan if (LOOKUP_QUALIFIERS_ONLY (flags) || hidden_name_p (val)) 3510169689Skan val = NULL_TREE; 3511169689Skan break; 3512132718Skan default: 3513169689Skan if (LOOKUP_QUALIFIERS_ONLY (flags)) 3514169689Skan val = NULL_TREE; 3515132718Skan } 3516169689Skan 3517132718Skan if (!old->value) 3518132718Skan old->value = val; 3519132718Skan else if (val && val != old->value) 3520132718Skan { 3521132718Skan if (is_overloaded_fn (old->value) && is_overloaded_fn (val)) 3522169689Skan old->value = merge_functions (old->value, val); 3523132718Skan else 3524132718Skan { 3525169689Skan old->value = tree_cons (NULL_TREE, old->value, 3526169689Skan build_tree_list (NULL_TREE, new->value)); 3527169689Skan TREE_TYPE (old->value) = error_mark_node; 3528132718Skan } 3529132718Skan } 3530132718Skan /* ... and copy the type. */ 3531132718Skan type = new->type; 3532132718Skan if (LOOKUP_NAMESPACES_ONLY (flags)) 3533132718Skan type = NULL_TREE; 3534132718Skan if (!old->type) 3535132718Skan old->type = type; 3536132718Skan else if (type && old->type != type) 3537132718Skan { 3538132718Skan if (flags & LOOKUP_COMPLAIN) 3539169689Skan { 3540169689Skan error ("%qD denotes an ambiguous type",name); 3541169689Skan error ("%J first type here", TYPE_MAIN_DECL (old->type)); 3542169689Skan error ("%J other type here", TYPE_MAIN_DECL (type)); 3543169689Skan } 3544132718Skan } 3545132718Skan} 3546132718Skan 3547132718Skan/* Return the declarations that are members of the namespace NS. */ 3548132718Skan 3549132718Skantree 3550132718Skancp_namespace_decls (tree ns) 3551132718Skan{ 3552132718Skan return NAMESPACE_LEVEL (ns)->names; 3553132718Skan} 3554132718Skan 3555132718Skan/* Combine prefer_type and namespaces_only into flags. */ 3556132718Skan 3557132718Skanstatic int 3558132718Skanlookup_flags (int prefer_type, int namespaces_only) 3559132718Skan{ 3560132718Skan if (namespaces_only) 3561132718Skan return LOOKUP_PREFER_NAMESPACES; 3562132718Skan if (prefer_type > 1) 3563132718Skan return LOOKUP_PREFER_TYPES; 3564132718Skan if (prefer_type > 0) 3565132718Skan return LOOKUP_PREFER_BOTH; 3566132718Skan return 0; 3567132718Skan} 3568132718Skan 3569132718Skan/* Given a lookup that returned VAL, use FLAGS to decide if we want to 3570169689Skan ignore it or not. Subroutine of lookup_name_real and 3571169689Skan lookup_type_scope. */ 3572132718Skan 3573169689Skanstatic bool 3574132718Skanqualify_lookup (tree val, int flags) 3575132718Skan{ 3576132718Skan if (val == NULL_TREE) 3577169689Skan return false; 3578132718Skan if ((flags & LOOKUP_PREFER_NAMESPACES) && TREE_CODE (val) == NAMESPACE_DECL) 3579169689Skan return true; 3580132718Skan if ((flags & LOOKUP_PREFER_TYPES) 3581132718Skan && (TREE_CODE (val) == TYPE_DECL || TREE_CODE (val) == TEMPLATE_DECL)) 3582169689Skan return true; 3583132718Skan if (flags & (LOOKUP_PREFER_NAMESPACES | LOOKUP_PREFER_TYPES)) 3584169689Skan return false; 3585169689Skan return true; 3586132718Skan} 3587132718Skan 3588169689Skan/* Given a lookup that returned VAL, decide if we want to ignore it or 3589169689Skan not based on DECL_ANTICIPATED. */ 3590132718Skan 3591169689Skanbool 3592169689Skanhidden_name_p (tree val) 3593132718Skan{ 3594169689Skan if (DECL_P (val) 3595169689Skan && DECL_LANG_SPECIFIC (val) 3596169689Skan && DECL_ANTICIPATED (val)) 3597169689Skan return true; 3598169689Skan return false; 3599169689Skan} 3600132718Skan 3601169689Skan/* Remove any hidden friend functions from a possibly overloaded set 3602169689Skan of functions. */ 3603132718Skan 3604169689Skantree 3605169689Skanremove_hidden_names (tree fns) 3606169689Skan{ 3607169689Skan if (!fns) 3608169689Skan return fns; 3609132718Skan 3610169689Skan if (TREE_CODE (fns) == FUNCTION_DECL && hidden_name_p (fns)) 3611169689Skan fns = NULL_TREE; 3612169689Skan else if (TREE_CODE (fns) == OVERLOAD) 3613132718Skan { 3614169689Skan tree o; 3615132718Skan 3616169689Skan for (o = fns; o; o = OVL_NEXT (o)) 3617169689Skan if (hidden_name_p (OVL_CURRENT (o))) 3618169689Skan break; 3619169689Skan if (o) 3620169689Skan { 3621169689Skan tree n = NULL_TREE; 3622132718Skan 3623169689Skan for (o = fns; o; o = OVL_NEXT (o)) 3624169689Skan if (!hidden_name_p (OVL_CURRENT (o))) 3625169689Skan n = build_overload (OVL_CURRENT (o), n); 3626169689Skan fns = n; 3627132718Skan } 3628132718Skan } 3629132718Skan 3630169689Skan return fns; 3631132718Skan} 3632132718Skan 3633132718Skan/* Select the right _DECL from multiple choices. */ 3634132718Skan 3635132718Skanstatic tree 3636169689Skanselect_decl (const struct scope_binding *binding, int flags) 3637132718Skan{ 3638132718Skan tree val; 3639132718Skan val = binding->value; 3640132718Skan 3641132718Skan timevar_push (TV_NAME_LOOKUP); 3642132718Skan if (LOOKUP_NAMESPACES_ONLY (flags)) 3643132718Skan { 3644132718Skan /* We are not interested in types. */ 3645169689Skan if (val && (TREE_CODE (val) == NAMESPACE_DECL 3646169689Skan || TREE_CODE (val) == TREE_LIST)) 3647169689Skan POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val); 3648132718Skan POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE); 3649132718Skan } 3650132718Skan 3651132718Skan /* If looking for a type, or if there is no non-type binding, select 3652132718Skan the value binding. */ 3653132718Skan if (binding->type && (!val || (flags & LOOKUP_PREFER_TYPES))) 3654132718Skan val = binding->type; 3655132718Skan /* Don't return non-types if we really prefer types. */ 3656169689Skan else if (val && LOOKUP_TYPES_ONLY (flags) 3657169689Skan && ! DECL_DECLARES_TYPE_P (val)) 3658132718Skan val = NULL_TREE; 3659132718Skan 3660132718Skan POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val); 3661132718Skan} 3662132718Skan 3663132718Skan/* Unscoped lookup of a global: iterate over current namespaces, 3664132718Skan considering using-directives. */ 3665132718Skan 3666132718Skanstatic tree 3667132718Skanunqualified_namespace_lookup (tree name, int flags) 3668132718Skan{ 3669132718Skan tree initial = current_decl_namespace (); 3670132718Skan tree scope = initial; 3671132718Skan tree siter; 3672132718Skan struct cp_binding_level *level; 3673132718Skan tree val = NULL_TREE; 3674169689Skan struct scope_binding binding = EMPTY_SCOPE_BINDING; 3675132718Skan 3676132718Skan timevar_push (TV_NAME_LOOKUP); 3677132718Skan 3678132718Skan for (; !val; scope = CP_DECL_CONTEXT (scope)) 3679132718Skan { 3680132718Skan cxx_binding *b = 3681169689Skan cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (scope), name); 3682132718Skan 3683132718Skan if (b) 3684132718Skan { 3685169689Skan if (b->value 3686169689Skan && ((flags & LOOKUP_HIDDEN) || !hidden_name_p (b->value))) 3687132718Skan binding.value = b->value; 3688132718Skan binding.type = b->type; 3689132718Skan } 3690132718Skan 3691132718Skan /* Add all _DECLs seen through local using-directives. */ 3692132718Skan for (level = current_binding_level; 3693132718Skan level->kind != sk_namespace; 3694132718Skan level = level->level_chain) 3695132718Skan if (!lookup_using_namespace (name, &binding, level->using_directives, 3696169689Skan scope, flags)) 3697132718Skan /* Give up because of error. */ 3698132718Skan POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node); 3699132718Skan 3700132718Skan /* Add all _DECLs seen through global using-directives. */ 3701132718Skan /* XXX local and global using lists should work equally. */ 3702132718Skan siter = initial; 3703132718Skan while (1) 3704132718Skan { 3705132718Skan if (!lookup_using_namespace (name, &binding, 3706169689Skan DECL_NAMESPACE_USING (siter), 3707132718Skan scope, flags)) 3708132718Skan /* Give up because of error. */ 3709132718Skan POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node); 3710132718Skan if (siter == scope) break; 3711132718Skan siter = CP_DECL_CONTEXT (siter); 3712132718Skan } 3713132718Skan 3714132718Skan val = select_decl (&binding, flags); 3715132718Skan if (scope == global_namespace) 3716132718Skan break; 3717132718Skan } 3718132718Skan POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val); 3719132718Skan} 3720132718Skan 3721132718Skan/* Look up NAME (an IDENTIFIER_NODE) in SCOPE (either a NAMESPACE_DECL 3722132718Skan or a class TYPE). If IS_TYPE_P is TRUE, then ignore non-type 3723169689Skan bindings. 3724132718Skan 3725132718Skan Returns a DECL (or OVERLOAD, or BASELINK) representing the 3726132718Skan declaration found. If no suitable declaration can be found, 3727169689Skan ERROR_MARK_NODE is returned. If COMPLAIN is true and SCOPE is 3728132718Skan neither a class-type nor a namespace a diagnostic is issued. */ 3729132718Skan 3730132718Skantree 3731132718Skanlookup_qualified_name (tree scope, tree name, bool is_type_p, bool complain) 3732132718Skan{ 3733132718Skan int flags = 0; 3734169689Skan tree t = NULL_TREE; 3735132718Skan 3736132718Skan if (TREE_CODE (scope) == NAMESPACE_DECL) 3737132718Skan { 3738169689Skan struct scope_binding binding = EMPTY_SCOPE_BINDING; 3739132718Skan 3740132718Skan flags |= LOOKUP_COMPLAIN; 3741132718Skan if (is_type_p) 3742132718Skan flags |= LOOKUP_PREFER_TYPES; 3743132718Skan if (qualified_lookup_using_namespace (name, scope, &binding, flags)) 3744169689Skan t = select_decl (&binding, flags); 3745132718Skan } 3746132718Skan else if (is_aggr_type (scope, complain)) 3747169689Skan t = lookup_member (scope, name, 2, is_type_p); 3748132718Skan 3749169689Skan if (!t) 3750169689Skan return error_mark_node; 3751169689Skan return t; 3752132718Skan} 3753132718Skan 3754132718Skan/* Subroutine of unqualified_namespace_lookup: 3755132718Skan Add the bindings of NAME in used namespaces to VAL. 3756132718Skan We are currently looking for names in namespace SCOPE, so we 3757132718Skan look through USINGS for using-directives of namespaces 3758132718Skan which have SCOPE as a common ancestor with the current scope. 3759132718Skan Returns false on errors. */ 3760132718Skan 3761132718Skanstatic bool 3762169689Skanlookup_using_namespace (tree name, struct scope_binding *val, 3763169689Skan tree usings, tree scope, int flags) 3764132718Skan{ 3765132718Skan tree iter; 3766132718Skan timevar_push (TV_NAME_LOOKUP); 3767132718Skan /* Iterate over all used namespaces in current, searching for using 3768132718Skan directives of scope. */ 3769132718Skan for (iter = usings; iter; iter = TREE_CHAIN (iter)) 3770132718Skan if (TREE_VALUE (iter) == scope) 3771132718Skan { 3772169689Skan tree used = ORIGINAL_NAMESPACE (TREE_PURPOSE (iter)); 3773169689Skan cxx_binding *val1 = 3774169689Skan cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (used), name); 3775169689Skan /* Resolve ambiguities. */ 3776169689Skan if (val1) 3777169689Skan ambiguous_decl (name, val, val1, flags); 3778132718Skan } 3779132718Skan POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val->value != error_mark_node); 3780132718Skan} 3781132718Skan 3782132718Skan/* [namespace.qual] 3783132718Skan Accepts the NAME to lookup and its qualifying SCOPE. 3784132718Skan Returns the name/type pair found into the cxx_binding *RESULT, 3785132718Skan or false on error. */ 3786132718Skan 3787132718Skanstatic bool 3788169689Skanqualified_lookup_using_namespace (tree name, tree scope, 3789169689Skan struct scope_binding *result, int flags) 3790132718Skan{ 3791132718Skan /* Maintain a list of namespaces visited... */ 3792132718Skan tree seen = NULL_TREE; 3793132718Skan /* ... and a list of namespace yet to see. */ 3794132718Skan tree todo = NULL_TREE; 3795132718Skan tree todo_maybe = NULL_TREE; 3796132718Skan tree usings; 3797132718Skan timevar_push (TV_NAME_LOOKUP); 3798132718Skan /* Look through namespace aliases. */ 3799132718Skan scope = ORIGINAL_NAMESPACE (scope); 3800132718Skan while (scope && result->value != error_mark_node) 3801132718Skan { 3802132718Skan cxx_binding *binding = 3803132718Skan cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (scope), name); 3804132718Skan seen = tree_cons (scope, NULL_TREE, seen); 3805132718Skan if (binding) 3806169689Skan ambiguous_decl (name, result, binding, flags); 3807132718Skan 3808132718Skan /* Consider strong using directives always, and non-strong ones 3809132718Skan if we haven't found a binding yet. ??? Shouldn't we consider 3810132718Skan non-strong ones if the initial RESULT is non-NULL, but the 3811132718Skan binding in the given namespace is? */ 3812132718Skan for (usings = DECL_NAMESPACE_USING (scope); usings; 3813132718Skan usings = TREE_CHAIN (usings)) 3814132718Skan /* If this was a real directive, and we have not seen it. */ 3815132718Skan if (!TREE_INDIRECT_USING (usings)) 3816132718Skan { 3817132718Skan /* Try to avoid queuing the same namespace more than once, 3818132718Skan the exception being when a namespace was already 3819132718Skan enqueued for todo_maybe and then a strong using is 3820132718Skan found for it. We could try to remove it from 3821132718Skan todo_maybe, but it's probably not worth the effort. */ 3822132718Skan if (is_associated_namespace (scope, TREE_PURPOSE (usings)) 3823132718Skan && !purpose_member (TREE_PURPOSE (usings), seen) 3824132718Skan && !purpose_member (TREE_PURPOSE (usings), todo)) 3825132718Skan todo = tree_cons (TREE_PURPOSE (usings), NULL_TREE, todo); 3826132718Skan else if ((!result->value && !result->type) 3827132718Skan && !purpose_member (TREE_PURPOSE (usings), seen) 3828132718Skan && !purpose_member (TREE_PURPOSE (usings), todo) 3829132718Skan && !purpose_member (TREE_PURPOSE (usings), todo_maybe)) 3830132718Skan todo_maybe = tree_cons (TREE_PURPOSE (usings), NULL_TREE, 3831132718Skan todo_maybe); 3832132718Skan } 3833132718Skan if (todo) 3834132718Skan { 3835132718Skan scope = TREE_PURPOSE (todo); 3836132718Skan todo = TREE_CHAIN (todo); 3837132718Skan } 3838132718Skan else if (todo_maybe 3839132718Skan && (!result->value && !result->type)) 3840132718Skan { 3841132718Skan scope = TREE_PURPOSE (todo_maybe); 3842132718Skan todo = TREE_CHAIN (todo_maybe); 3843132718Skan todo_maybe = NULL_TREE; 3844132718Skan } 3845132718Skan else 3846132718Skan scope = NULL_TREE; /* If there never was a todo list. */ 3847132718Skan } 3848132718Skan POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, result->value != error_mark_node); 3849132718Skan} 3850132718Skan 3851169689Skan/* Return the innermost non-namespace binding for NAME from a scope 3852169689Skan containing BINDING, or, if BINDING is NULL, the current scope. If 3853169689Skan CLASS_P is false, then class bindings are ignored. */ 3854169689Skan 3855169689Skancxx_binding * 3856169689Skanouter_binding (tree name, 3857169689Skan cxx_binding *binding, 3858169689Skan bool class_p) 3859169689Skan{ 3860169689Skan cxx_binding *outer; 3861169689Skan cxx_scope *scope; 3862169689Skan cxx_scope *outer_scope; 3863169689Skan 3864169689Skan if (binding) 3865169689Skan { 3866169689Skan scope = binding->scope->level_chain; 3867169689Skan outer = binding->previous; 3868169689Skan } 3869169689Skan else 3870169689Skan { 3871169689Skan scope = current_binding_level; 3872169689Skan outer = IDENTIFIER_BINDING (name); 3873169689Skan } 3874169689Skan outer_scope = outer ? outer->scope : NULL; 3875169689Skan 3876169689Skan /* Because we create class bindings lazily, we might be missing a 3877169689Skan class binding for NAME. If there are any class binding levels 3878169689Skan between the LAST_BINDING_LEVEL and the scope in which OUTER was 3879169689Skan declared, we must lookup NAME in those class scopes. */ 3880169689Skan if (class_p) 3881169689Skan while (scope && scope != outer_scope && scope->kind != sk_namespace) 3882169689Skan { 3883169689Skan if (scope->kind == sk_class) 3884169689Skan { 3885169689Skan cxx_binding *class_binding; 3886169689Skan 3887169689Skan class_binding = get_class_binding (name, scope); 3888169689Skan if (class_binding) 3889169689Skan { 3890169689Skan /* Thread this new class-scope binding onto the 3891169689Skan IDENTIFIER_BINDING list so that future lookups 3892169689Skan find it quickly. */ 3893169689Skan class_binding->previous = outer; 3894169689Skan if (binding) 3895169689Skan binding->previous = class_binding; 3896169689Skan else 3897169689Skan IDENTIFIER_BINDING (name) = class_binding; 3898169689Skan return class_binding; 3899169689Skan } 3900169689Skan } 3901169689Skan scope = scope->level_chain; 3902169689Skan } 3903169689Skan 3904169689Skan return outer; 3905169689Skan} 3906169689Skan 3907169689Skan/* Return the innermost block-scope or class-scope value binding for 3908169689Skan NAME, or NULL_TREE if there is no such binding. */ 3909169689Skan 3910169689Skantree 3911169689Skaninnermost_non_namespace_value (tree name) 3912169689Skan{ 3913169689Skan cxx_binding *binding; 3914169689Skan binding = outer_binding (name, /*binding=*/NULL, /*class_p=*/true); 3915169689Skan return binding ? binding->value : NULL_TREE; 3916169689Skan} 3917169689Skan 3918132718Skan/* Look up NAME in the current binding level and its superiors in the 3919132718Skan namespace of variables, functions and typedefs. Return a ..._DECL 3920132718Skan node of some kind representing its definition if there is only one 3921132718Skan such declaration, or return a TREE_LIST with all the overloaded 3922132718Skan definitions if there are many, or return 0 if it is undefined. 3923169689Skan Hidden name, either friend declaration or built-in function, are 3924169689Skan not ignored. 3925132718Skan 3926132718Skan If PREFER_TYPE is > 0, we prefer TYPE_DECLs or namespaces. 3927132718Skan If PREFER_TYPE is > 1, we reject non-type decls (e.g. namespaces). 3928132718Skan Otherwise we prefer non-TYPE_DECLs. 3929132718Skan 3930169689Skan If NONCLASS is nonzero, bindings in class scopes are ignored. If 3931169689Skan BLOCK_P is false, bindings in block scopes are ignored. */ 3932132718Skan 3933132718Skantree 3934169689Skanlookup_name_real (tree name, int prefer_type, int nonclass, bool block_p, 3935132718Skan int namespaces_only, int flags) 3936132718Skan{ 3937132718Skan cxx_binding *iter; 3938132718Skan tree val = NULL_TREE; 3939132718Skan 3940132718Skan timevar_push (TV_NAME_LOOKUP); 3941132718Skan /* Conversion operators are handled specially because ordinary 3942132718Skan unqualified name lookup will not find template conversion 3943132718Skan operators. */ 3944169689Skan if (IDENTIFIER_TYPENAME_P (name)) 3945132718Skan { 3946132718Skan struct cp_binding_level *level; 3947132718Skan 3948169689Skan for (level = current_binding_level; 3949132718Skan level && level->kind != sk_namespace; 3950132718Skan level = level->level_chain) 3951132718Skan { 3952132718Skan tree class_type; 3953132718Skan tree operators; 3954169689Skan 3955169689Skan /* A conversion operator can only be declared in a class 3956132718Skan scope. */ 3957132718Skan if (level->kind != sk_class) 3958132718Skan continue; 3959169689Skan 3960132718Skan /* Lookup the conversion operator in the class. */ 3961132718Skan class_type = level->this_entity; 3962132718Skan operators = lookup_fnfields (class_type, name, /*protect=*/0); 3963132718Skan if (operators) 3964132718Skan POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, operators); 3965132718Skan } 3966132718Skan 3967132718Skan POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE); 3968132718Skan } 3969132718Skan 3970132718Skan flags |= lookup_flags (prefer_type, namespaces_only); 3971132718Skan 3972132718Skan /* First, look in non-namespace scopes. */ 3973132718Skan 3974132718Skan if (current_class_type == NULL_TREE) 3975132718Skan nonclass = 1; 3976132718Skan 3977169689Skan if (block_p || !nonclass) 3978169689Skan for (iter = outer_binding (name, NULL, !nonclass); 3979169689Skan iter; 3980169689Skan iter = outer_binding (name, iter, !nonclass)) 3981169689Skan { 3982169689Skan tree binding; 3983132718Skan 3984169689Skan /* Skip entities we don't want. */ 3985169689Skan if (LOCAL_BINDING_P (iter) ? !block_p : nonclass) 3986169689Skan continue; 3987132718Skan 3988169689Skan /* If this is the kind of thing we're looking for, we're done. */ 3989169689Skan if (qualify_lookup (iter->value, flags)) 3990169689Skan binding = iter->value; 3991169689Skan else if ((flags & LOOKUP_PREFER_TYPES) 3992169689Skan && qualify_lookup (iter->type, flags)) 3993169689Skan binding = iter->type; 3994169689Skan else 3995169689Skan binding = NULL_TREE; 3996132718Skan 3997169689Skan if (binding) 3998169689Skan { 3999220150Smm if (hidden_name_p (binding)) 4000220150Smm { 4001220150Smm /* A non namespace-scope binding can only be hidden if 4002220150Smm we are in a local class, due to friend declarations. 4003220150Smm In particular, consider: 4004220150Smm 4005220150Smm void f() { 4006220150Smm struct A { 4007220150Smm friend struct B; 4008220150Smm void g() { B* b; } // error: B is hidden 4009220150Smm } 4010220150Smm struct B {}; 4011220150Smm } 4012220150Smm 4013220150Smm The standard says that "B" is a local class in "f" 4014220150Smm (but not nested within "A") -- but that name lookup 4015220150Smm for "B" does not find this declaration until it is 4016220150Smm declared directly with "f". 4017220150Smm 4018220150Smm In particular: 4019220150Smm 4020220150Smm [class.friend] 4021220150Smm 4022220150Smm If a friend declaration appears in a local class and 4023220150Smm the name specified is an unqualified name, a prior 4024220150Smm declaration is looked up without considering scopes 4025220150Smm that are outside the innermost enclosing non-class 4026220150Smm scope. For a friend class declaration, if there is no 4027220150Smm prior declaration, the class that is specified 4028220150Smm belongs to the innermost enclosing non-class scope, 4029220150Smm but if it is subsequently referenced, its name is not 4030220150Smm found by name lookup until a matching declaration is 4031220150Smm provided in the innermost enclosing nonclass scope. 4032220150Smm */ 4033220150Smm gcc_assert (current_class_type && 4034220150Smm LOCAL_CLASS_P (current_class_type)); 4035220150Smm 4036220150Smm /* This binding comes from a friend declaration in the local 4037220150Smm class. The standard (11.4.8) states that the lookup can 4038220150Smm only succeed if there is a non-hidden declaration in the 4039220150Smm current scope, which is not the case here. */ 4040220150Smm POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE); 4041220150Smm } 4042169689Skan val = binding; 4043169689Skan break; 4044169689Skan } 4045169689Skan } 4046132718Skan 4047132718Skan /* Now lookup in namespace scopes. */ 4048132718Skan if (!val) 4049169689Skan val = unqualified_namespace_lookup (name, flags); 4050132718Skan 4051169689Skan /* If we have a single function from a using decl, pull it out. */ 4052169689Skan if (val && TREE_CODE (val) == OVERLOAD && !really_overloaded_fn (val)) 4053169689Skan val = OVL_FUNCTION (val); 4054132718Skan 4055132718Skan POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val); 4056132718Skan} 4057132718Skan 4058132718Skantree 4059132718Skanlookup_name_nonclass (tree name) 4060132718Skan{ 4061169689Skan return lookup_name_real (name, 0, 1, /*block_p=*/true, 0, LOOKUP_COMPLAIN); 4062132718Skan} 4063132718Skan 4064132718Skantree 4065169689Skanlookup_function_nonclass (tree name, tree args, bool block_p) 4066132718Skan{ 4067169689Skan return 4068169689Skan lookup_arg_dependent (name, 4069169689Skan lookup_name_real (name, 0, 1, block_p, 0, 4070169689Skan LOOKUP_COMPLAIN), 4071169689Skan args); 4072132718Skan} 4073132718Skan 4074132718Skantree 4075169689Skanlookup_name (tree name) 4076132718Skan{ 4077169689Skan return lookup_name_real (name, 0, 0, /*block_p=*/true, 0, LOOKUP_COMPLAIN); 4078132718Skan} 4079132718Skan 4080169689Skantree 4081169689Skanlookup_name_prefer_type (tree name, int prefer_type) 4082169689Skan{ 4083169689Skan return lookup_name_real (name, prefer_type, 0, /*block_p=*/true, 4084169689Skan 0, LOOKUP_COMPLAIN); 4085169689Skan} 4086169689Skan 4087169689Skan/* Look up NAME for type used in elaborated name specifier in 4088169689Skan the scopes given by SCOPE. SCOPE can be either TS_CURRENT or 4089169689Skan TS_WITHIN_ENCLOSING_NON_CLASS. Although not implied by the 4090169689Skan name, more scopes are checked if cleanup or template parameter 4091169689Skan scope is encountered. 4092169689Skan 4093169689Skan Unlike lookup_name_real, we make sure that NAME is actually 4094169689Skan declared in the desired scope, not from inheritance, nor using 4095169689Skan directive. For using declaration, there is DR138 still waiting 4096169689Skan to be resolved. Hidden name coming from an earlier friend 4097169689Skan declaration is also returned. 4098169689Skan 4099169689Skan A TYPE_DECL best matching the NAME is returned. Catching error 4100169689Skan and issuing diagnostics are caller's responsibility. */ 4101169689Skan 4102169689Skantree 4103169689Skanlookup_type_scope (tree name, tag_scope scope) 4104169689Skan{ 4105169689Skan cxx_binding *iter = NULL; 4106169689Skan tree val = NULL_TREE; 4107169689Skan 4108169689Skan timevar_push (TV_NAME_LOOKUP); 4109169689Skan 4110169689Skan /* Look in non-namespace scope first. */ 4111169689Skan if (current_binding_level->kind != sk_namespace) 4112169689Skan iter = outer_binding (name, NULL, /*class_p=*/ true); 4113169689Skan for (; iter; iter = outer_binding (name, iter, /*class_p=*/ true)) 4114169689Skan { 4115169689Skan /* Check if this is the kind of thing we're looking for. 4116169689Skan If SCOPE is TS_CURRENT, also make sure it doesn't come from 4117169689Skan base class. For ITER->VALUE, we can simply use 4118169689Skan INHERITED_VALUE_BINDING_P. For ITER->TYPE, we have to use 4119169689Skan our own check. 4120169689Skan 4121169689Skan We check ITER->TYPE before ITER->VALUE in order to handle 4122169689Skan typedef struct C {} C; 4123169689Skan correctly. */ 4124169689Skan 4125169689Skan if (qualify_lookup (iter->type, LOOKUP_PREFER_TYPES) 4126169689Skan && (scope != ts_current 4127169689Skan || LOCAL_BINDING_P (iter) 4128169689Skan || DECL_CONTEXT (iter->type) == iter->scope->this_entity)) 4129169689Skan val = iter->type; 4130169689Skan else if ((scope != ts_current 4131169689Skan || !INHERITED_VALUE_BINDING_P (iter)) 4132169689Skan && qualify_lookup (iter->value, LOOKUP_PREFER_TYPES)) 4133169689Skan val = iter->value; 4134169689Skan 4135169689Skan if (val) 4136169689Skan break; 4137169689Skan } 4138169689Skan 4139169689Skan /* Look in namespace scope. */ 4140169689Skan if (!val) 4141169689Skan { 4142169689Skan iter = cxx_scope_find_binding_for_name 4143169689Skan (NAMESPACE_LEVEL (current_decl_namespace ()), name); 4144169689Skan 4145169689Skan if (iter) 4146169689Skan { 4147169689Skan /* If this is the kind of thing we're looking for, we're done. */ 4148169689Skan if (qualify_lookup (iter->type, LOOKUP_PREFER_TYPES)) 4149169689Skan val = iter->type; 4150169689Skan else if (qualify_lookup (iter->value, LOOKUP_PREFER_TYPES)) 4151169689Skan val = iter->value; 4152169689Skan } 4153169689Skan 4154169689Skan } 4155169689Skan 4156169689Skan /* Type found, check if it is in the allowed scopes, ignoring cleanup 4157169689Skan and template parameter scopes. */ 4158169689Skan if (val) 4159169689Skan { 4160169689Skan struct cp_binding_level *b = current_binding_level; 4161169689Skan while (b) 4162169689Skan { 4163169689Skan if (iter->scope == b) 4164169689Skan POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val); 4165169689Skan 4166169689Skan if (b->kind == sk_cleanup || b->kind == sk_template_parms) 4167169689Skan b = b->level_chain; 4168169689Skan else if (b->kind == sk_class 4169169689Skan && scope == ts_within_enclosing_non_class) 4170169689Skan b = b->level_chain; 4171169689Skan else 4172169689Skan break; 4173169689Skan } 4174169689Skan } 4175169689Skan 4176169689Skan POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE); 4177169689Skan} 4178169689Skan 4179132718Skan/* Similar to `lookup_name' but look only in the innermost non-class 4180132718Skan binding level. */ 4181132718Skan 4182132718Skanstatic tree 4183169689Skanlookup_name_innermost_nonclass_level (tree name) 4184132718Skan{ 4185132718Skan struct cp_binding_level *b; 4186132718Skan tree t = NULL_TREE; 4187132718Skan 4188132718Skan timevar_push (TV_NAME_LOOKUP); 4189132718Skan b = innermost_nonclass_level (); 4190132718Skan 4191132718Skan if (b->kind == sk_namespace) 4192132718Skan { 4193132718Skan t = IDENTIFIER_NAMESPACE_VALUE (name); 4194132718Skan 4195132718Skan /* extern "C" function() */ 4196132718Skan if (t != NULL_TREE && TREE_CODE (t) == TREE_LIST) 4197132718Skan t = TREE_VALUE (t); 4198132718Skan } 4199132718Skan else if (IDENTIFIER_BINDING (name) 4200132718Skan && LOCAL_BINDING_P (IDENTIFIER_BINDING (name))) 4201132718Skan { 4202169689Skan cxx_binding *binding; 4203169689Skan binding = IDENTIFIER_BINDING (name); 4204132718Skan while (1) 4205132718Skan { 4206169689Skan if (binding->scope == b 4207169689Skan && !(TREE_CODE (binding->value) == VAR_DECL 4208169689Skan && DECL_DEAD_FOR_LOCAL (binding->value))) 4209169689Skan POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, binding->value); 4210132718Skan 4211132718Skan if (b->kind == sk_cleanup) 4212132718Skan b = b->level_chain; 4213132718Skan else 4214132718Skan break; 4215132718Skan } 4216132718Skan } 4217132718Skan 4218132718Skan POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t); 4219132718Skan} 4220132718Skan 4221169689Skan/* Like lookup_name_innermost_nonclass_level, but for types. */ 4222132718Skan 4223132718Skanstatic tree 4224132718Skanlookup_type_current_level (tree name) 4225132718Skan{ 4226132718Skan tree t = NULL_TREE; 4227132718Skan 4228132718Skan timevar_push (TV_NAME_LOOKUP); 4229169689Skan gcc_assert (current_binding_level->kind != sk_namespace); 4230132718Skan 4231132718Skan if (REAL_IDENTIFIER_TYPE_VALUE (name) != NULL_TREE 4232132718Skan && REAL_IDENTIFIER_TYPE_VALUE (name) != global_type_node) 4233132718Skan { 4234132718Skan struct cp_binding_level *b = current_binding_level; 4235132718Skan while (1) 4236132718Skan { 4237132718Skan if (purpose_member (name, b->type_shadowed)) 4238132718Skan POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, 4239169689Skan REAL_IDENTIFIER_TYPE_VALUE (name)); 4240132718Skan if (b->kind == sk_cleanup) 4241132718Skan b = b->level_chain; 4242132718Skan else 4243132718Skan break; 4244132718Skan } 4245132718Skan } 4246132718Skan 4247132718Skan POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t); 4248132718Skan} 4249132718Skan 4250132718Skan/* [basic.lookup.koenig] */ 4251132718Skan/* A nonzero return value in the functions below indicates an error. */ 4252132718Skan 4253132718Skanstruct arg_lookup 4254132718Skan{ 4255132718Skan tree name; 4256169689Skan tree args; 4257132718Skan tree namespaces; 4258132718Skan tree classes; 4259132718Skan tree functions; 4260132718Skan}; 4261132718Skan 4262132718Skanstatic bool arg_assoc (struct arg_lookup*, tree); 4263132718Skanstatic bool arg_assoc_args (struct arg_lookup*, tree); 4264132718Skanstatic bool arg_assoc_type (struct arg_lookup*, tree); 4265132718Skanstatic bool add_function (struct arg_lookup *, tree); 4266132718Skanstatic bool arg_assoc_namespace (struct arg_lookup *, tree); 4267132718Skanstatic bool arg_assoc_class (struct arg_lookup *, tree); 4268132718Skanstatic bool arg_assoc_template_arg (struct arg_lookup*, tree); 4269132718Skan 4270132718Skan/* Add a function to the lookup structure. 4271132718Skan Returns true on error. */ 4272132718Skan 4273132718Skanstatic bool 4274132718Skanadd_function (struct arg_lookup *k, tree fn) 4275132718Skan{ 4276132718Skan /* We used to check here to see if the function was already in the list, 4277132718Skan but that's O(n^2), which is just too expensive for function lookup. 4278132718Skan Now we deal with the occasional duplicate in joust. In doing this, we 4279132718Skan assume that the number of duplicates will be small compared to the 4280132718Skan total number of functions being compared, which should usually be the 4281132718Skan case. */ 4282132718Skan 4283132718Skan /* We must find only functions, or exactly one non-function. */ 4284169689Skan if (!k->functions) 4285132718Skan k->functions = fn; 4286132718Skan else if (fn == k->functions) 4287132718Skan ; 4288132718Skan else if (is_overloaded_fn (k->functions) && is_overloaded_fn (fn)) 4289132718Skan k->functions = build_overload (fn, k->functions); 4290132718Skan else 4291132718Skan { 4292132718Skan tree f1 = OVL_CURRENT (k->functions); 4293132718Skan tree f2 = fn; 4294132718Skan if (is_overloaded_fn (f1)) 4295132718Skan { 4296132718Skan fn = f1; f1 = f2; f2 = fn; 4297132718Skan } 4298169689Skan error ("%q+D is not a function,", f1); 4299169689Skan error (" conflict with %q+D", f2); 4300169689Skan error (" in call to %qD", k->name); 4301132718Skan return true; 4302132718Skan } 4303132718Skan 4304132718Skan return false; 4305132718Skan} 4306132718Skan 4307132718Skan/* Returns true iff CURRENT has declared itself to be an associated 4308132718Skan namespace of SCOPE via a strong using-directive (or transitive chain 4309132718Skan thereof). Both are namespaces. */ 4310132718Skan 4311132718Skanbool 4312132718Skanis_associated_namespace (tree current, tree scope) 4313132718Skan{ 4314132718Skan tree seen = NULL_TREE; 4315132718Skan tree todo = NULL_TREE; 4316132718Skan tree t; 4317132718Skan while (1) 4318132718Skan { 4319132718Skan if (scope == current) 4320132718Skan return true; 4321132718Skan seen = tree_cons (scope, NULL_TREE, seen); 4322132718Skan for (t = DECL_NAMESPACE_ASSOCIATIONS (scope); t; t = TREE_CHAIN (t)) 4323132718Skan if (!purpose_member (TREE_PURPOSE (t), seen)) 4324132718Skan todo = tree_cons (TREE_PURPOSE (t), NULL_TREE, todo); 4325132718Skan if (todo) 4326132718Skan { 4327132718Skan scope = TREE_PURPOSE (todo); 4328132718Skan todo = TREE_CHAIN (todo); 4329132718Skan } 4330132718Skan else 4331132718Skan return false; 4332132718Skan } 4333132718Skan} 4334132718Skan 4335169689Skan/* Return whether FN is a friend of an associated class of ARG. */ 4336169689Skan 4337169689Skanstatic bool 4338169689Skanfriend_of_associated_class_p (tree arg, tree fn) 4339169689Skan{ 4340169689Skan tree type; 4341169689Skan 4342169689Skan if (TYPE_P (arg)) 4343169689Skan type = arg; 4344169689Skan else if (type_unknown_p (arg)) 4345169689Skan return false; 4346169689Skan else 4347169689Skan type = TREE_TYPE (arg); 4348169689Skan 4349169689Skan /* If TYPE is a class, the class itself and all base classes are 4350169689Skan associated classes. */ 4351169689Skan if (CLASS_TYPE_P (type)) 4352169689Skan { 4353169689Skan if (is_friend (type, fn)) 4354169689Skan return true; 4355169689Skan 4356169689Skan if (TYPE_BINFO (type)) 4357169689Skan { 4358169689Skan tree binfo, base_binfo; 4359169689Skan int i; 4360169689Skan 4361169689Skan for (binfo = TYPE_BINFO (type), i = 0; 4362169689Skan BINFO_BASE_ITERATE (binfo, i, base_binfo); 4363169689Skan i++) 4364169689Skan if (is_friend (BINFO_TYPE (base_binfo), fn)) 4365169689Skan return true; 4366169689Skan } 4367169689Skan } 4368169689Skan 4369169689Skan /* If TYPE is a class member, the class of which it is a member is 4370169689Skan an associated class. */ 4371169689Skan if ((CLASS_TYPE_P (type) 4372169689Skan || TREE_CODE (type) == UNION_TYPE 4373169689Skan || TREE_CODE (type) == ENUMERAL_TYPE) 4374169689Skan && TYPE_CONTEXT (type) 4375169689Skan && CLASS_TYPE_P (TYPE_CONTEXT (type)) 4376169689Skan && is_friend (TYPE_CONTEXT (type), fn)) 4377169689Skan return true; 4378169689Skan 4379169689Skan return false; 4380169689Skan} 4381169689Skan 4382132718Skan/* Add functions of a namespace to the lookup structure. 4383132718Skan Returns true on error. */ 4384132718Skan 4385132718Skanstatic bool 4386132718Skanarg_assoc_namespace (struct arg_lookup *k, tree scope) 4387132718Skan{ 4388132718Skan tree value; 4389132718Skan 4390132718Skan if (purpose_member (scope, k->namespaces)) 4391132718Skan return 0; 4392132718Skan k->namespaces = tree_cons (scope, NULL_TREE, k->namespaces); 4393132718Skan 4394132718Skan /* Check out our super-users. */ 4395132718Skan for (value = DECL_NAMESPACE_ASSOCIATIONS (scope); value; 4396132718Skan value = TREE_CHAIN (value)) 4397132718Skan if (arg_assoc_namespace (k, TREE_PURPOSE (value))) 4398132718Skan return true; 4399169689Skan 4400132718Skan value = namespace_binding (k->name, scope); 4401132718Skan if (!value) 4402132718Skan return false; 4403132718Skan 4404132718Skan for (; value; value = OVL_NEXT (value)) 4405169689Skan { 4406169689Skan /* We don't want to find arbitrary hidden functions via argument 4407169689Skan dependent lookup. We only want to find friends of associated 4408169689Skan classes. */ 4409169689Skan if (hidden_name_p (OVL_CURRENT (value))) 4410169689Skan { 4411169689Skan tree args; 4412169689Skan 4413169689Skan for (args = k->args; args; args = TREE_CHAIN (args)) 4414169689Skan if (friend_of_associated_class_p (TREE_VALUE (args), 4415169689Skan OVL_CURRENT (value))) 4416169689Skan break; 4417169689Skan if (!args) 4418169689Skan continue; 4419169689Skan } 4420169689Skan 4421169689Skan if (add_function (k, OVL_CURRENT (value))) 4422169689Skan return true; 4423169689Skan } 4424169689Skan 4425132718Skan return false; 4426132718Skan} 4427132718Skan 4428132718Skan/* Adds everything associated with a template argument to the lookup 4429132718Skan structure. Returns true on error. */ 4430132718Skan 4431132718Skanstatic bool 4432132718Skanarg_assoc_template_arg (struct arg_lookup *k, tree arg) 4433132718Skan{ 4434132718Skan /* [basic.lookup.koenig] 4435132718Skan 4436132718Skan If T is a template-id, its associated namespaces and classes are 4437132718Skan ... the namespaces and classes associated with the types of the 4438132718Skan template arguments provided for template type parameters 4439132718Skan (excluding template template parameters); the namespaces in which 4440132718Skan any template template arguments are defined; and the classes in 4441132718Skan which any member templates used as template template arguments 4442132718Skan are defined. [Note: non-type template arguments do not 4443132718Skan contribute to the set of associated namespaces. ] */ 4444132718Skan 4445132718Skan /* Consider first template template arguments. */ 4446132718Skan if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM 4447132718Skan || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE) 4448132718Skan return false; 4449132718Skan else if (TREE_CODE (arg) == TEMPLATE_DECL) 4450132718Skan { 4451132718Skan tree ctx = CP_DECL_CONTEXT (arg); 4452132718Skan 4453132718Skan /* It's not a member template. */ 4454132718Skan if (TREE_CODE (ctx) == NAMESPACE_DECL) 4455169689Skan return arg_assoc_namespace (k, ctx); 4456132718Skan /* Otherwise, it must be member template. */ 4457169689Skan else 4458169689Skan return arg_assoc_class (k, ctx); 4459132718Skan } 4460132718Skan /* It's not a template template argument, but it is a type template 4461132718Skan argument. */ 4462132718Skan else if (TYPE_P (arg)) 4463132718Skan return arg_assoc_type (k, arg); 4464132718Skan /* It's a non-type template argument. */ 4465132718Skan else 4466132718Skan return false; 4467132718Skan} 4468132718Skan 4469132718Skan/* Adds everything associated with class to the lookup structure. 4470132718Skan Returns true on error. */ 4471132718Skan 4472132718Skanstatic bool 4473132718Skanarg_assoc_class (struct arg_lookup *k, tree type) 4474132718Skan{ 4475132718Skan tree list, friends, context; 4476132718Skan int i; 4477169689Skan 4478132718Skan /* Backend build structures, such as __builtin_va_list, aren't 4479132718Skan affected by all this. */ 4480132718Skan if (!CLASS_TYPE_P (type)) 4481132718Skan return false; 4482132718Skan 4483132718Skan if (purpose_member (type, k->classes)) 4484132718Skan return false; 4485132718Skan k->classes = tree_cons (type, NULL_TREE, k->classes); 4486169689Skan 4487146895Skan context = decl_namespace_context (type); 4488132718Skan if (arg_assoc_namespace (k, context)) 4489132718Skan return true; 4490169689Skan 4491169689Skan if (TYPE_BINFO (type)) 4492169689Skan { 4493169689Skan /* Process baseclasses. */ 4494169689Skan tree binfo, base_binfo; 4495169689Skan 4496169689Skan for (binfo = TYPE_BINFO (type), i = 0; 4497169689Skan BINFO_BASE_ITERATE (binfo, i, base_binfo); i++) 4498169689Skan if (arg_assoc_class (k, BINFO_TYPE (base_binfo))) 4499169689Skan return true; 4500169689Skan } 4501169689Skan 4502132718Skan /* Process friends. */ 4503169689Skan for (list = DECL_FRIENDLIST (TYPE_MAIN_DECL (type)); list; 4504132718Skan list = TREE_CHAIN (list)) 4505132718Skan if (k->name == FRIEND_NAME (list)) 4506169689Skan for (friends = FRIEND_DECLS (list); friends; 4507132718Skan friends = TREE_CHAIN (friends)) 4508132718Skan { 4509132718Skan tree fn = TREE_VALUE (friends); 4510132718Skan 4511132718Skan /* Only interested in global functions with potentially hidden 4512132718Skan (i.e. unqualified) declarations. */ 4513132718Skan if (CP_DECL_CONTEXT (fn) != context) 4514132718Skan continue; 4515132718Skan /* Template specializations are never found by name lookup. 4516132718Skan (Templates themselves can be found, but not template 4517132718Skan specializations.) */ 4518132718Skan if (TREE_CODE (fn) == FUNCTION_DECL && DECL_USE_TEMPLATE (fn)) 4519132718Skan continue; 4520132718Skan if (add_function (k, fn)) 4521132718Skan return true; 4522132718Skan } 4523132718Skan 4524132718Skan /* Process template arguments. */ 4525169689Skan if (CLASSTYPE_TEMPLATE_INFO (type) 4526169689Skan && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type))) 4527132718Skan { 4528132718Skan list = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)); 4529169689Skan for (i = 0; i < TREE_VEC_LENGTH (list); ++i) 4530169689Skan arg_assoc_template_arg (k, TREE_VEC_ELT (list, i)); 4531132718Skan } 4532132718Skan 4533132718Skan return false; 4534132718Skan} 4535132718Skan 4536132718Skan/* Adds everything associated with a given type. 4537132718Skan Returns 1 on error. */ 4538132718Skan 4539132718Skanstatic bool 4540132718Skanarg_assoc_type (struct arg_lookup *k, tree type) 4541132718Skan{ 4542132718Skan /* As we do not get the type of non-type dependent expressions 4543132718Skan right, we can end up with such things without a type. */ 4544132718Skan if (!type) 4545132718Skan return false; 4546132718Skan 4547132718Skan if (TYPE_PTRMEM_P (type)) 4548132718Skan { 4549132718Skan /* Pointer to member: associate class type and value type. */ 4550132718Skan if (arg_assoc_type (k, TYPE_PTRMEM_CLASS_TYPE (type))) 4551132718Skan return true; 4552132718Skan return arg_assoc_type (k, TYPE_PTRMEM_POINTED_TO_TYPE (type)); 4553132718Skan } 4554132718Skan else switch (TREE_CODE (type)) 4555132718Skan { 4556132718Skan case ERROR_MARK: 4557132718Skan return false; 4558132718Skan case VOID_TYPE: 4559132718Skan case INTEGER_TYPE: 4560132718Skan case REAL_TYPE: 4561132718Skan case COMPLEX_TYPE: 4562132718Skan case VECTOR_TYPE: 4563132718Skan case BOOLEAN_TYPE: 4564132718Skan return false; 4565132718Skan case RECORD_TYPE: 4566132718Skan if (TYPE_PTRMEMFUNC_P (type)) 4567132718Skan return arg_assoc_type (k, TYPE_PTRMEMFUNC_FN_TYPE (type)); 4568132718Skan return arg_assoc_class (k, type); 4569132718Skan case POINTER_TYPE: 4570132718Skan case REFERENCE_TYPE: 4571132718Skan case ARRAY_TYPE: 4572132718Skan return arg_assoc_type (k, TREE_TYPE (type)); 4573132718Skan case UNION_TYPE: 4574132718Skan case ENUMERAL_TYPE: 4575146895Skan return arg_assoc_namespace (k, decl_namespace_context (type)); 4576132718Skan case METHOD_TYPE: 4577132718Skan /* The basetype is referenced in the first arg type, so just 4578132718Skan fall through. */ 4579132718Skan case FUNCTION_TYPE: 4580132718Skan /* Associate the parameter types. */ 4581132718Skan if (arg_assoc_args (k, TYPE_ARG_TYPES (type))) 4582132718Skan return true; 4583132718Skan /* Associate the return type. */ 4584132718Skan return arg_assoc_type (k, TREE_TYPE (type)); 4585132718Skan case TEMPLATE_TYPE_PARM: 4586132718Skan case BOUND_TEMPLATE_TEMPLATE_PARM: 4587132718Skan return false; 4588132718Skan case TYPENAME_TYPE: 4589132718Skan return false; 4590132718Skan case LANG_TYPE: 4591169689Skan gcc_assert (type == unknown_type_node); 4592169689Skan return false; 4593132718Skan default: 4594169689Skan gcc_unreachable (); 4595132718Skan } 4596132718Skan return false; 4597132718Skan} 4598132718Skan 4599132718Skan/* Adds everything associated with arguments. Returns true on error. */ 4600132718Skan 4601132718Skanstatic bool 4602132718Skanarg_assoc_args (struct arg_lookup *k, tree args) 4603132718Skan{ 4604132718Skan for (; args; args = TREE_CHAIN (args)) 4605132718Skan if (arg_assoc (k, TREE_VALUE (args))) 4606132718Skan return true; 4607132718Skan return false; 4608132718Skan} 4609132718Skan 4610132718Skan/* Adds everything associated with a given tree_node. Returns 1 on error. */ 4611132718Skan 4612132718Skanstatic bool 4613132718Skanarg_assoc (struct arg_lookup *k, tree n) 4614132718Skan{ 4615132718Skan if (n == error_mark_node) 4616132718Skan return false; 4617132718Skan 4618132718Skan if (TYPE_P (n)) 4619132718Skan return arg_assoc_type (k, n); 4620132718Skan 4621132718Skan if (! type_unknown_p (n)) 4622132718Skan return arg_assoc_type (k, TREE_TYPE (n)); 4623132718Skan 4624132718Skan if (TREE_CODE (n) == ADDR_EXPR) 4625132718Skan n = TREE_OPERAND (n, 0); 4626132718Skan if (TREE_CODE (n) == COMPONENT_REF) 4627132718Skan n = TREE_OPERAND (n, 1); 4628132718Skan if (TREE_CODE (n) == OFFSET_REF) 4629132718Skan n = TREE_OPERAND (n, 1); 4630132718Skan while (TREE_CODE (n) == TREE_LIST) 4631132718Skan n = TREE_VALUE (n); 4632132718Skan if (TREE_CODE (n) == BASELINK) 4633132718Skan n = BASELINK_FUNCTIONS (n); 4634132718Skan 4635132718Skan if (TREE_CODE (n) == FUNCTION_DECL) 4636132718Skan return arg_assoc_type (k, TREE_TYPE (n)); 4637132718Skan if (TREE_CODE (n) == TEMPLATE_ID_EXPR) 4638132718Skan { 4639132718Skan /* [basic.lookup.koenig] 4640132718Skan 4641132718Skan If T is a template-id, its associated namespaces and classes 4642132718Skan are the namespace in which the template is defined; for 4643132718Skan member templates, the member template's class... */ 4644132718Skan tree template = TREE_OPERAND (n, 0); 4645132718Skan tree args = TREE_OPERAND (n, 1); 4646132718Skan tree ctx; 4647132718Skan int ix; 4648132718Skan 4649132718Skan if (TREE_CODE (template) == COMPONENT_REF) 4650169689Skan template = TREE_OPERAND (template, 1); 4651169689Skan 4652132718Skan /* First, the template. There may actually be more than one if 4653132718Skan this is an overloaded function template. But, in that case, 4654132718Skan we only need the first; all the functions will be in the same 4655132718Skan namespace. */ 4656132718Skan template = OVL_CURRENT (template); 4657132718Skan 4658132718Skan ctx = CP_DECL_CONTEXT (template); 4659169689Skan 4660132718Skan if (TREE_CODE (ctx) == NAMESPACE_DECL) 4661132718Skan { 4662132718Skan if (arg_assoc_namespace (k, ctx) == 1) 4663132718Skan return true; 4664132718Skan } 4665132718Skan /* It must be a member template. */ 4666132718Skan else if (arg_assoc_class (k, ctx) == 1) 4667132718Skan return true; 4668132718Skan 4669132718Skan /* Now the arguments. */ 4670161651Skan if (args) 4671161651Skan for (ix = TREE_VEC_LENGTH (args); ix--;) 4672161651Skan if (arg_assoc_template_arg (k, TREE_VEC_ELT (args, ix)) == 1) 4673161651Skan return true; 4674132718Skan } 4675132718Skan else if (TREE_CODE (n) == OVERLOAD) 4676132718Skan { 4677132718Skan for (; n; n = OVL_CHAIN (n)) 4678132718Skan if (arg_assoc_type (k, TREE_TYPE (OVL_FUNCTION (n)))) 4679132718Skan return true; 4680132718Skan } 4681132718Skan 4682132718Skan return false; 4683132718Skan} 4684132718Skan 4685132718Skan/* Performs Koenig lookup depending on arguments, where fns 4686132718Skan are the functions found in normal lookup. */ 4687132718Skan 4688132718Skantree 4689132718Skanlookup_arg_dependent (tree name, tree fns, tree args) 4690132718Skan{ 4691132718Skan struct arg_lookup k; 4692132718Skan 4693132718Skan timevar_push (TV_NAME_LOOKUP); 4694169689Skan 4695169689Skan /* Remove any hidden friend functions from the list of functions 4696169689Skan found so far. They will be added back by arg_assoc_class as 4697169689Skan appropriate. */ 4698169689Skan fns = remove_hidden_names (fns); 4699169689Skan 4700132718Skan k.name = name; 4701169689Skan k.args = args; 4702132718Skan k.functions = fns; 4703132718Skan k.classes = NULL_TREE; 4704132718Skan 4705169689Skan /* We previously performed an optimization here by setting 4706169689Skan NAMESPACES to the current namespace when it was safe. However, DR 4707169689Skan 164 says that namespaces that were already searched in the first 4708169689Skan stage of template processing are searched again (potentially 4709169689Skan picking up later definitions) in the second stage. */ 4710169689Skan k.namespaces = NULL_TREE; 4711132718Skan 4712132718Skan arg_assoc_args (&k, args); 4713169689Skan 4714169689Skan fns = k.functions; 4715169689Skan 4716169689Skan if (fns 4717169689Skan && TREE_CODE (fns) != VAR_DECL 4718169689Skan && !is_overloaded_fn (fns)) 4719169689Skan { 4720169689Skan error ("argument dependent lookup finds %q+D", fns); 4721169689Skan error (" in call to %qD", name); 4722169689Skan fns = error_mark_node; 4723169689Skan } 4724169689Skan 4725169689Skan POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, fns); 4726132718Skan} 4727132718Skan 4728132718Skan/* Add namespace to using_directives. Return NULL_TREE if nothing was 4729132718Skan changed (i.e. there was already a directive), or the fresh 4730132718Skan TREE_LIST otherwise. */ 4731132718Skan 4732132718Skanstatic tree 4733132718Skanpush_using_directive (tree used) 4734132718Skan{ 4735132718Skan tree ud = current_binding_level->using_directives; 4736132718Skan tree iter, ancestor; 4737132718Skan 4738132718Skan timevar_push (TV_NAME_LOOKUP); 4739132718Skan /* Check if we already have this. */ 4740132718Skan if (purpose_member (used, ud) != NULL_TREE) 4741132718Skan POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE); 4742132718Skan 4743132718Skan ancestor = namespace_ancestor (current_decl_namespace (), used); 4744132718Skan ud = current_binding_level->using_directives; 4745132718Skan ud = tree_cons (used, ancestor, ud); 4746132718Skan current_binding_level->using_directives = ud; 4747132718Skan 4748132718Skan /* Recursively add all namespaces used. */ 4749132718Skan for (iter = DECL_NAMESPACE_USING (used); iter; iter = TREE_CHAIN (iter)) 4750132718Skan push_using_directive (TREE_PURPOSE (iter)); 4751132718Skan 4752132718Skan POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ud); 4753132718Skan} 4754132718Skan 4755132718Skan/* The type TYPE is being declared. If it is a class template, or a 4756132718Skan specialization of a class template, do any processing required and 4757132718Skan perform error-checking. If IS_FRIEND is nonzero, this TYPE is 4758132718Skan being declared a friend. B is the binding level at which this TYPE 4759132718Skan should be bound. 4760132718Skan 4761132718Skan Returns the TYPE_DECL for TYPE, which may have been altered by this 4762132718Skan processing. */ 4763132718Skan 4764132718Skanstatic tree 4765169689Skanmaybe_process_template_type_declaration (tree type, int is_friend, 4766169689Skan cxx_scope *b) 4767132718Skan{ 4768132718Skan tree decl = TYPE_NAME (type); 4769132718Skan 4770132718Skan if (processing_template_parmlist) 4771132718Skan /* You can't declare a new template type in a template parameter 4772132718Skan list. But, you can declare a non-template type: 4773132718Skan 4774169689Skan template <class A*> struct S; 4775132718Skan 4776132718Skan is a forward-declaration of `A'. */ 4777132718Skan ; 4778169689Skan else if (b->kind == sk_namespace 4779169689Skan && current_binding_level->kind != sk_namespace) 4780169689Skan /* If this new type is being injected into a containing scope, 4781169689Skan then it's not a template type. */ 4782169689Skan ; 4783132718Skan else 4784132718Skan { 4785169689Skan gcc_assert (IS_AGGR_TYPE (type) || TREE_CODE (type) == ENUMERAL_TYPE); 4786132718Skan 4787132718Skan if (processing_template_decl) 4788132718Skan { 4789132718Skan /* This may change after the call to 4790132718Skan push_template_decl_real, but we want the original value. */ 4791132718Skan tree name = DECL_NAME (decl); 4792132718Skan 4793169689Skan decl = push_template_decl_real (decl, is_friend); 4794132718Skan /* If the current binding level is the binding level for the 4795132718Skan template parameters (see the comment in 4796132718Skan begin_template_parm_list) and the enclosing level is a class 4797132718Skan scope, and we're not looking at a friend, push the 4798132718Skan declaration of the member class into the class scope. In the 4799132718Skan friend case, push_template_decl will already have put the 4800132718Skan friend into global scope, if appropriate. */ 4801132718Skan if (TREE_CODE (type) != ENUMERAL_TYPE 4802169689Skan && !is_friend && b->kind == sk_template_parms 4803132718Skan && b->level_chain->kind == sk_class) 4804132718Skan { 4805132718Skan finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type)); 4806169689Skan 4807132718Skan if (!COMPLETE_TYPE_P (current_class_type)) 4808132718Skan { 4809132718Skan maybe_add_class_template_decl_list (current_class_type, 4810132718Skan type, /*friend_p=*/0); 4811169689Skan /* Put this UTD in the table of UTDs for the class. */ 4812169689Skan if (CLASSTYPE_NESTED_UTDS (current_class_type) == NULL) 4813169689Skan CLASSTYPE_NESTED_UTDS (current_class_type) = 4814169689Skan binding_table_new (SCOPE_DEFAULT_HT_SIZE); 4815169689Skan 4816169689Skan binding_table_insert 4817169689Skan (CLASSTYPE_NESTED_UTDS (current_class_type), name, type); 4818132718Skan } 4819132718Skan } 4820132718Skan } 4821132718Skan } 4822132718Skan 4823132718Skan return decl; 4824132718Skan} 4825132718Skan 4826169689Skan/* Push a tag name NAME for struct/class/union/enum type TYPE. In case 4827169689Skan that the NAME is a class template, the tag is processed but not pushed. 4828132718Skan 4829169689Skan The pushed scope depend on the SCOPE parameter: 4830169689Skan - When SCOPE is TS_CURRENT, put it into the inner-most non-sk_cleanup 4831169689Skan scope. 4832169689Skan - When SCOPE is TS_GLOBAL, put it in the inner-most non-class and 4833169689Skan non-template-parameter scope. This case is needed for forward 4834169689Skan declarations. 4835169689Skan - When SCOPE is TS_WITHIN_ENCLOSING_NON_CLASS, this is similar to 4836169689Skan TS_GLOBAL case except that names within template-parameter scopes 4837169689Skan are not pushed at all. 4838169689Skan 4839169689Skan Returns TYPE upon success and ERROR_MARK_NODE otherwise. */ 4840169689Skan 4841169689Skantree 4842169689Skanpushtag (tree name, tree type, tag_scope scope) 4843132718Skan{ 4844132718Skan struct cp_binding_level *b; 4845169689Skan tree decl; 4846132718Skan 4847132718Skan timevar_push (TV_NAME_LOOKUP); 4848132718Skan b = current_binding_level; 4849132718Skan while (/* Cleanup scopes are not scopes from the point of view of 4850132718Skan the language. */ 4851132718Skan b->kind == sk_cleanup 4852132718Skan /* Neither are the scopes used to hold template parameters 4853132718Skan for an explicit specialization. For an ordinary template 4854132718Skan declaration, these scopes are not scopes from the point of 4855169689Skan view of the language. */ 4856169689Skan || (b->kind == sk_template_parms 4857169689Skan && (b->explicit_spec_p || scope == ts_global)) 4858132718Skan || (b->kind == sk_class 4859169689Skan && (scope != ts_current 4860132718Skan /* We may be defining a new type in the initializer 4861132718Skan of a static member variable. We allow this when 4862132718Skan not pedantic, and it is particularly useful for 4863132718Skan type punning via an anonymous union. */ 4864132718Skan || COMPLETE_TYPE_P (b->this_entity)))) 4865132718Skan b = b->level_chain; 4866132718Skan 4867169689Skan gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE); 4868132718Skan 4869169689Skan /* Do C++ gratuitous typedefing. */ 4870169689Skan if (IDENTIFIER_TYPE_VALUE (name) != type) 4871132718Skan { 4872169689Skan tree tdef; 4873169689Skan int in_class = 0; 4874169689Skan tree context = TYPE_CONTEXT (type); 4875132718Skan 4876169689Skan if (! context) 4877169689Skan { 4878169689Skan tree cs = current_scope (); 4879132718Skan 4880169689Skan if (scope == ts_current) 4881169689Skan context = cs; 4882169689Skan else if (cs != NULL_TREE && TYPE_P (cs)) 4883169689Skan /* When declaring a friend class of a local class, we want 4884169689Skan to inject the newly named class into the scope 4885169689Skan containing the local class, not the namespace 4886169689Skan scope. */ 4887169689Skan context = decl_function_context (get_type_decl (cs)); 4888169689Skan } 4889169689Skan if (!context) 4890169689Skan context = current_namespace; 4891132718Skan 4892169689Skan if (b->kind == sk_class 4893169689Skan || (b->kind == sk_template_parms 4894169689Skan && b->level_chain->kind == sk_class)) 4895169689Skan in_class = 1; 4896132718Skan 4897169689Skan if (current_lang_name == lang_name_java) 4898169689Skan TYPE_FOR_JAVA (type) = 1; 4899132718Skan 4900169689Skan tdef = create_implicit_typedef (name, type); 4901169689Skan DECL_CONTEXT (tdef) = FROB_CONTEXT (context); 4902169689Skan if (scope == ts_within_enclosing_non_class) 4903169689Skan { 4904169689Skan /* This is a friend. Make this TYPE_DECL node hidden from 4905169689Skan ordinary name lookup. Its corresponding TEMPLATE_DECL 4906169689Skan will be marked in push_template_decl_real. */ 4907169689Skan retrofit_lang_decl (tdef); 4908169689Skan DECL_ANTICIPATED (tdef) = 1; 4909169689Skan DECL_FRIEND_P (tdef) = 1; 4910169689Skan } 4911132718Skan 4912169689Skan decl = maybe_process_template_type_declaration 4913169689Skan (type, scope == ts_within_enclosing_non_class, b); 4914169689Skan if (decl == error_mark_node) 4915169689Skan POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl); 4916132718Skan 4917169689Skan if (! in_class) 4918169689Skan set_identifier_type_value_with_scope (name, tdef, b); 4919169689Skan 4920169689Skan if (b->kind == sk_class) 4921169689Skan { 4922169689Skan if (!PROCESSING_REAL_TEMPLATE_DECL_P ()) 4923169689Skan /* Put this TYPE_DECL on the TYPE_FIELDS list for the 4924169689Skan class. But if it's a member template class, we want 4925169689Skan the TEMPLATE_DECL, not the TYPE_DECL, so this is done 4926169689Skan later. */ 4927169689Skan finish_member_declaration (decl); 4928132718Skan else 4929169689Skan pushdecl_class_level (decl); 4930169689Skan } 4931169689Skan else if (b->kind != sk_template_parms) 4932169689Skan { 4933169689Skan decl = pushdecl_with_scope (decl, b, /*is_friend=*/false); 4934169689Skan if (decl == error_mark_node) 4935169689Skan POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl); 4936169689Skan } 4937132718Skan 4938169689Skan TYPE_CONTEXT (type) = DECL_CONTEXT (decl); 4939132718Skan 4940169689Skan /* If this is a local class, keep track of it. We need this 4941169689Skan information for name-mangling, and so that it is possible to 4942169689Skan find all function definitions in a translation unit in a 4943169689Skan convenient way. (It's otherwise tricky to find a member 4944169689Skan function definition it's only pointed to from within a local 4945169689Skan class.) */ 4946169689Skan if (TYPE_CONTEXT (type) 4947169689Skan && TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL) 4948169689Skan VEC_safe_push (tree, gc, local_classes, type); 4949169689Skan } 4950169689Skan if (b->kind == sk_class 4951169689Skan && !COMPLETE_TYPE_P (current_class_type)) 4952169689Skan { 4953169689Skan maybe_add_class_template_decl_list (current_class_type, 4954169689Skan type, /*friend_p=*/0); 4955132718Skan 4956169689Skan if (CLASSTYPE_NESTED_UTDS (current_class_type) == NULL) 4957169689Skan CLASSTYPE_NESTED_UTDS (current_class_type) 4958169689Skan = binding_table_new (SCOPE_DEFAULT_HT_SIZE); 4959169689Skan 4960169689Skan binding_table_insert 4961169689Skan (CLASSTYPE_NESTED_UTDS (current_class_type), name, type); 4962132718Skan } 4963132718Skan 4964169689Skan decl = TYPE_NAME (type); 4965169689Skan gcc_assert (TREE_CODE (decl) == TYPE_DECL); 4966169689Skan TYPE_STUB_DECL (type) = decl; 4967132718Skan 4968169689Skan /* Set type visibility now if this is a forward declaration. */ 4969169689Skan TREE_PUBLIC (decl) = 1; 4970169689Skan determine_visibility (decl); 4971169689Skan 4972169689Skan POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, type); 4973132718Skan} 4974132718Skan 4975132718Skan/* Subroutines for reverting temporarily to top-level for instantiation 4976132718Skan of templates and such. We actually need to clear out the class- and 4977132718Skan local-value slots of all identifiers, so that only the global values 4978132718Skan are at all visible. Simply setting current_binding_level to the global 4979132718Skan scope isn't enough, because more binding levels may be pushed. */ 4980132718Skanstruct saved_scope *scope_chain; 4981132718Skan 4982169689Skan/* If ID has not already been marked, add an appropriate binding to 4983169689Skan *OLD_BINDINGS. */ 4984169689Skan 4985169689Skanstatic void 4986169689Skanstore_binding (tree id, VEC(cxx_saved_binding,gc) **old_bindings) 4987132718Skan{ 4988169689Skan cxx_saved_binding *saved; 4989169689Skan 4990169689Skan if (!id || !IDENTIFIER_BINDING (id)) 4991169689Skan return; 4992169689Skan 4993169689Skan if (IDENTIFIER_MARKED (id)) 4994169689Skan return; 4995169689Skan 4996169689Skan IDENTIFIER_MARKED (id) = 1; 4997169689Skan 4998169689Skan saved = VEC_safe_push (cxx_saved_binding, gc, *old_bindings, NULL); 4999169689Skan saved->identifier = id; 5000169689Skan saved->binding = IDENTIFIER_BINDING (id); 5001169689Skan saved->real_type_value = REAL_IDENTIFIER_TYPE_VALUE (id); 5002169689Skan IDENTIFIER_BINDING (id) = NULL; 5003169689Skan} 5004169689Skan 5005169689Skanstatic void 5006169689Skanstore_bindings (tree names, VEC(cxx_saved_binding,gc) **old_bindings) 5007169689Skan{ 5008132718Skan tree t; 5009132718Skan 5010132718Skan timevar_push (TV_NAME_LOOKUP); 5011132718Skan for (t = names; t; t = TREE_CHAIN (t)) 5012132718Skan { 5013132718Skan tree id; 5014132718Skan 5015132718Skan if (TREE_CODE (t) == TREE_LIST) 5016132718Skan id = TREE_PURPOSE (t); 5017132718Skan else 5018132718Skan id = DECL_NAME (t); 5019132718Skan 5020169689Skan store_binding (id, old_bindings); 5021169689Skan } 5022169689Skan timevar_pop (TV_NAME_LOOKUP); 5023169689Skan} 5024132718Skan 5025169689Skan/* Like store_bindings, but NAMES is a vector of cp_class_binding 5026169689Skan objects, rather than a TREE_LIST. */ 5027132718Skan 5028169689Skanstatic void 5029169689Skanstore_class_bindings (VEC(cp_class_binding,gc) *names, 5030169689Skan VEC(cxx_saved_binding,gc) **old_bindings) 5031169689Skan{ 5032169689Skan size_t i; 5033169689Skan cp_class_binding *cb; 5034169689Skan 5035169689Skan timevar_push (TV_NAME_LOOKUP); 5036169689Skan for (i = 0; VEC_iterate(cp_class_binding, names, i, cb); ++i) 5037169689Skan store_binding (cb->identifier, old_bindings); 5038169689Skan timevar_pop (TV_NAME_LOOKUP); 5039132718Skan} 5040132718Skan 5041132718Skanvoid 5042132718Skanpush_to_top_level (void) 5043132718Skan{ 5044132718Skan struct saved_scope *s; 5045132718Skan struct cp_binding_level *b; 5046169689Skan cxx_saved_binding *sb; 5047169689Skan size_t i; 5048132718Skan int need_pop; 5049132718Skan 5050132718Skan timevar_push (TV_NAME_LOOKUP); 5051169689Skan s = GGC_CNEW (struct saved_scope); 5052132718Skan 5053132718Skan b = scope_chain ? current_binding_level : 0; 5054132718Skan 5055132718Skan /* If we're in the middle of some function, save our state. */ 5056132718Skan if (cfun) 5057132718Skan { 5058132718Skan need_pop = 1; 5059132718Skan push_function_context_to (NULL_TREE); 5060132718Skan } 5061132718Skan else 5062132718Skan need_pop = 0; 5063132718Skan 5064169689Skan if (scope_chain && previous_class_level) 5065169689Skan store_class_bindings (previous_class_level->class_shadowed, 5066169689Skan &s->old_bindings); 5067132718Skan 5068132718Skan /* Have to include the global scope, because class-scope decls 5069132718Skan aren't listed anywhere useful. */ 5070132718Skan for (; b; b = b->level_chain) 5071132718Skan { 5072132718Skan tree t; 5073132718Skan 5074132718Skan /* Template IDs are inserted into the global level. If they were 5075132718Skan inserted into namespace level, finish_file wouldn't find them 5076132718Skan when doing pending instantiations. Therefore, don't stop at 5077132718Skan namespace level, but continue until :: . */ 5078132718Skan if (global_scope_p (b)) 5079132718Skan break; 5080132718Skan 5081169689Skan store_bindings (b->names, &s->old_bindings); 5082132718Skan /* We also need to check class_shadowed to save class-level type 5083132718Skan bindings, since pushclass doesn't fill in b->names. */ 5084132718Skan if (b->kind == sk_class) 5085169689Skan store_class_bindings (b->class_shadowed, &s->old_bindings); 5086132718Skan 5087132718Skan /* Unwind type-value slots back to top level. */ 5088132718Skan for (t = b->type_shadowed; t; t = TREE_CHAIN (t)) 5089132718Skan SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t), TREE_VALUE (t)); 5090132718Skan } 5091169689Skan 5092169689Skan for (i = 0; VEC_iterate (cxx_saved_binding, s->old_bindings, i, sb); ++i) 5093169689Skan IDENTIFIER_MARKED (sb->identifier) = 0; 5094169689Skan 5095132718Skan s->prev = scope_chain; 5096132718Skan s->bindings = b; 5097132718Skan s->need_pop_function_context = need_pop; 5098132718Skan s->function_decl = current_function_decl; 5099169689Skan s->skip_evaluation = skip_evaluation; 5100132718Skan 5101132718Skan scope_chain = s; 5102132718Skan current_function_decl = NULL_TREE; 5103169689Skan current_lang_base = VEC_alloc (tree, gc, 10); 5104132718Skan current_lang_name = lang_name_cplusplus; 5105132718Skan current_namespace = global_namespace; 5106169689Skan push_class_stack (); 5107169689Skan skip_evaluation = 0; 5108132718Skan timevar_pop (TV_NAME_LOOKUP); 5109132718Skan} 5110132718Skan 5111132718Skanvoid 5112132718Skanpop_from_top_level (void) 5113132718Skan{ 5114132718Skan struct saved_scope *s = scope_chain; 5115132718Skan cxx_saved_binding *saved; 5116169689Skan size_t i; 5117132718Skan 5118169689Skan timevar_push (TV_NAME_LOOKUP); 5119132718Skan /* Clear out class-level bindings cache. */ 5120169689Skan if (previous_class_level) 5121132718Skan invalidate_class_lookup_cache (); 5122169689Skan pop_class_stack (); 5123132718Skan 5124132718Skan current_lang_base = 0; 5125132718Skan 5126132718Skan scope_chain = s->prev; 5127169689Skan for (i = 0; VEC_iterate (cxx_saved_binding, s->old_bindings, i, saved); ++i) 5128132718Skan { 5129132718Skan tree id = saved->identifier; 5130132718Skan 5131132718Skan IDENTIFIER_BINDING (id) = saved->binding; 5132132718Skan SET_IDENTIFIER_TYPE_VALUE (id, saved->real_type_value); 5133132718Skan } 5134132718Skan 5135132718Skan /* If we were in the middle of compiling a function, restore our 5136132718Skan state. */ 5137132718Skan if (s->need_pop_function_context) 5138132718Skan pop_function_context_from (NULL_TREE); 5139132718Skan current_function_decl = s->function_decl; 5140169689Skan skip_evaluation = s->skip_evaluation; 5141132718Skan timevar_pop (TV_NAME_LOOKUP); 5142132718Skan} 5143132718Skan 5144132718Skan/* Pop off extraneous binding levels left over due to syntax errors. 5145132718Skan 5146132718Skan We don't pop past namespaces, as they might be valid. */ 5147132718Skan 5148132718Skanvoid 5149132718Skanpop_everything (void) 5150132718Skan{ 5151132718Skan if (ENABLE_SCOPE_CHECKING) 5152132718Skan verbatim ("XXX entering pop_everything ()\n"); 5153132718Skan while (!toplevel_bindings_p ()) 5154132718Skan { 5155132718Skan if (current_binding_level->kind == sk_class) 5156132718Skan pop_nested_class (); 5157132718Skan else 5158132718Skan poplevel (0, 0, 0); 5159132718Skan } 5160132718Skan if (ENABLE_SCOPE_CHECKING) 5161132718Skan verbatim ("XXX leaving pop_everything ()\n"); 5162132718Skan} 5163132718Skan 5164169689Skan/* Emit debugging information for using declarations and directives. 5165169689Skan If input tree is overloaded fn then emit debug info for all 5166169689Skan candidates. */ 5167169689Skan 5168169689Skanvoid 5169169689Skancp_emit_debug_info_for_using (tree t, tree context) 5170169689Skan{ 5171169689Skan /* Don't try to emit any debug information if we have errors. */ 5172169689Skan if (sorrycount || errorcount) 5173169689Skan return; 5174169689Skan 5175169689Skan /* Ignore this FUNCTION_DECL if it refers to a builtin declaration 5176169689Skan of a builtin function. */ 5177169689Skan if (TREE_CODE (t) == FUNCTION_DECL 5178169689Skan && DECL_EXTERNAL (t) 5179169689Skan && DECL_BUILT_IN (t)) 5180169689Skan return; 5181169689Skan 5182169689Skan /* Do not supply context to imported_module_or_decl, if 5183169689Skan it is a global namespace. */ 5184169689Skan if (context == global_namespace) 5185169689Skan context = NULL_TREE; 5186169689Skan 5187169689Skan if (BASELINK_P (t)) 5188169689Skan t = BASELINK_FUNCTIONS (t); 5189169689Skan 5190169689Skan /* FIXME: Handle TEMPLATE_DECLs. */ 5191169689Skan for (t = OVL_CURRENT (t); t; t = OVL_NEXT (t)) 5192169689Skan if (TREE_CODE (t) != TEMPLATE_DECL) 5193169689Skan (*debug_hooks->imported_module_or_decl) (t, context); 5194169689Skan} 5195169689Skan 5196132718Skan#include "gt-cp-name-lookup.h" 5197