From b655c31048e115ba4fc42072533de4de2c8f8821 Mon Sep 17 00:00:00 2001 From: Nathan Sidwell Date: Thu, 4 May 2017 12:35:05 +0000 Subject: [PATCH] name-lookup.c: Reorder functions to make merging from modules branch simpler. * name-lookup.c: Reorder functions to make merging from modules branch simpler. From-SVN: r247592 --- gcc/cp/ChangeLog | 5 + gcc/cp/name-lookup.c | 9858 +++++++++++++++++++++--------------------- 2 files changed, 4934 insertions(+), 4929 deletions(-) diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index 7f395b461a4..c942b46b931 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,8 @@ +2017-05-04 Nathan Sidwell + + * name-lookup.c: Reorder functions to make merging from modules + branch simpler. + 2017-05-03 Jason Merrill * constexpr.c (cxx_eval_outermost_constant_expr): Use TV_CONSTEXPR. diff --git a/gcc/cp/name-lookup.c b/gcc/cp/name-lookup.c index 0c5df937980..6b0219a542e 100644 --- a/gcc/cp/name-lookup.c +++ b/gcc/cp/name-lookup.c @@ -33,6 +33,10 @@ along with GCC; see the file COPYING3. If not see #include "spellcheck-tree.h" #include "parser.h" +static cxx_binding *cxx_binding_make (tree value, tree type); +static cp_binding_level *innermost_nonclass_level (void); +static void set_identifier_type_value_with_scope (tree id, tree decl, + cp_binding_level *b); /* The bindings for a particular name in a particular scope. */ struct scope_binding { @@ -41,7 +45,6 @@ struct scope_binding { }; #define EMPTY_SCOPE_BINDING { NULL_TREE, NULL_TREE } -static cp_binding_level *innermost_nonclass_level (void); static cxx_binding *binding_for_name (cp_binding_level *, tree); static tree push_overloaded_decl (tree, int, bool); static bool lookup_using_namespace (tree, struct scope_binding *, tree, @@ -66,6029 +69,5880 @@ tree global_namespace; unit. */ static GTY(()) tree anonymous_namespace_name; -/* Initialize anonymous_namespace_name if necessary, and return it. */ +/* Add DECL to the list of things declared in B. */ -static tree -get_anonymous_namespace_name (void) +static void +add_decl_to_level (tree decl, cp_binding_level *b) { - if (!anonymous_namespace_name) + /* We used to record virtual tables as if they were ordinary + variables, but no longer do so. */ + gcc_assert (!(VAR_P (decl) && DECL_VIRTUAL_P (decl))); + + if (TREE_CODE (decl) == NAMESPACE_DECL + && !DECL_NAMESPACE_ALIAS (decl)) { - /* We used to use get_file_function_name here, but that isn't - necessary now that anonymous namespace typeinfos - are !TREE_PUBLIC, and thus compared by address. */ - /* The demangler expects anonymous namespaces to be called - something starting with '_GLOBAL__N_'. */ - anonymous_namespace_name = get_identifier ("_GLOBAL__N_1"); + DECL_CHAIN (decl) = b->namespaces; + b->namespaces = decl; } - return anonymous_namespace_name; -} - -/* Compute the chain index of a binding_entry given the HASH value of its - name and the total COUNT of chains. COUNT is assumed to be a power - of 2. */ - -#define ENTRY_INDEX(HASH, COUNT) (((HASH) >> 3) & ((COUNT) - 1)) + else + { + /* We build up the list in reverse order, and reverse it later if + necessary. */ + TREE_CHAIN (decl) = b->names; + b->names = decl; -/* A free list of "binding_entry"s awaiting for re-use. */ + /* If appropriate, add decl to separate list of statics. We + include extern variables because they might turn out to be + static later. It's OK for this list to contain a few false + positives. */ + if (b->kind == sk_namespace) + if ((VAR_P (decl) + && (TREE_STATIC (decl) || DECL_EXTERNAL (decl))) + || (TREE_CODE (decl) == FUNCTION_DECL + && (!TREE_PUBLIC (decl) + || decl_anon_ns_mem_p (decl) + || DECL_DECLARED_INLINE_P (decl)))) + vec_safe_push (b->static_decls, decl); + } +} -static GTY((deletable)) binding_entry free_binding_entry = NULL; +/* [basic.lookup.koenig] */ +/* A nonzero return value in the functions below indicates an error. */ -/* The binding oracle; see cp-tree.h. */ +struct arg_lookup +{ + tree name; + vec *args; + vec *namespaces; + vec *classes; + tree functions; + hash_set *fn_set; +}; -cp_binding_oracle_function *cp_binding_oracle; +static bool arg_assoc (struct arg_lookup*, tree); +static bool arg_assoc_args (struct arg_lookup*, tree); +static bool arg_assoc_args_vec (struct arg_lookup*, vec *); +static bool arg_assoc_type (struct arg_lookup*, tree); +static bool add_function (struct arg_lookup *, tree); +static bool arg_assoc_namespace (struct arg_lookup *, tree); +static bool arg_assoc_class_only (struct arg_lookup *, tree); +static bool arg_assoc_bases (struct arg_lookup *, tree); +static bool arg_assoc_class (struct arg_lookup *, tree); +static bool arg_assoc_template_arg (struct arg_lookup*, tree); -/* If we have a binding oracle, ask it for all namespace-scoped - definitions of NAME. */ +/* Add a function to the lookup structure. + Returns true on error. */ -static inline void -query_oracle (tree name) +static bool +add_function (struct arg_lookup *k, tree fn) { - if (!cp_binding_oracle) - return; - - /* LOOKED_UP holds the set of identifiers that we have already - looked up with the oracle. */ - static hash_set looked_up; - if (looked_up.add (name)) - return; + if (!is_overloaded_fn (fn)) + /* All names except those of (possibly overloaded) functions and + function templates are ignored. */; + else if (k->fn_set && k->fn_set->add (fn)) + /* It's already in the list. */; + else if (!k->functions && TREE_CODE (fn) != TEMPLATE_DECL) + k->functions = fn; + else if (fn == k->functions) + ; + else + { + k->functions = build_overload (fn, k->functions); + if (TREE_CODE (k->functions) == OVERLOAD) + OVL_ARG_DEPENDENT (k->functions) = true; + } - cp_binding_oracle (CP_ORACLE_IDENTIFIER, name); + return false; } -/* Create a binding_entry object for (NAME, TYPE). */ +/* Returns true iff CURRENT has declared itself to be an associated + namespace of SCOPE via a strong using-directive (or transitive chain + thereof). Both are namespaces. */ -static inline binding_entry -binding_entry_make (tree name, tree type) +bool +is_associated_namespace (tree current, tree scope) { - binding_entry entry; + vec *seen = make_tree_vector (); + vec *todo = make_tree_vector (); + tree t; + bool ret; - if (free_binding_entry) + while (1) { - entry = free_binding_entry; - free_binding_entry = entry->chain; + if (scope == current) + { + ret = true; + break; + } + vec_safe_push (seen, scope); + for (t = DECL_NAMESPACE_ASSOCIATIONS (scope); t; t = TREE_CHAIN (t)) + if (!vec_member (TREE_PURPOSE (t), seen)) + vec_safe_push (todo, TREE_PURPOSE (t)); + if (!todo->is_empty ()) + { + scope = todo->last (); + todo->pop (); + } + else + { + ret = false; + break; + } } - else - entry = ggc_alloc (); - - entry->name = name; - entry->type = type; - entry->chain = NULL; - return entry; -} + release_tree_vector (seen); + release_tree_vector (todo); -/* Put ENTRY back on the free list. */ -#if 0 -static inline void -binding_entry_free (binding_entry entry) -{ - entry->name = NULL; - entry->type = NULL; - entry->chain = free_binding_entry; - free_binding_entry = entry; + return ret; } -#endif - -/* The datatype used to implement the mapping from names to types at - a given scope. */ -struct GTY(()) binding_table_s { - /* Array of chains of "binding_entry"s */ - binding_entry * GTY((length ("%h.chain_count"))) chain; - /* The number of chains in this table. This is the length of the - member "chain" considered as an array. */ - size_t chain_count; +/* Add functions of a namespace to the lookup structure. + Returns true on error. */ - /* Number of "binding_entry"s in this table. */ - size_t entry_count; -}; +static bool +arg_assoc_namespace (struct arg_lookup *k, tree scope) +{ + tree value; -/* Construct TABLE with an initial CHAIN_COUNT. */ + if (vec_member (scope, k->namespaces)) + return false; + vec_safe_push (k->namespaces, scope); -static inline void -binding_table_construct (binding_table table, size_t chain_count) -{ - table->chain_count = chain_count; - table->entry_count = 0; - table->chain = ggc_cleared_vec_alloc (table->chain_count); -} + /* Check out our super-users. */ + for (value = DECL_NAMESPACE_ASSOCIATIONS (scope); value; + value = TREE_CHAIN (value)) + if (arg_assoc_namespace (k, TREE_PURPOSE (value))) + return true; -/* Make TABLE's entries ready for reuse. */ -#if 0 -static void -binding_table_free (binding_table table) -{ - size_t i; - size_t count; + /* Also look down into inline namespaces. */ + for (value = DECL_NAMESPACE_USING (scope); value; + value = TREE_CHAIN (value)) + if (is_associated_namespace (scope, TREE_PURPOSE (value))) + if (arg_assoc_namespace (k, TREE_PURPOSE (value))) + return true; - if (table == NULL) - return; + value = namespace_binding (k->name, scope); + if (!value) + return false; - for (i = 0, count = table->chain_count; i < count; ++i) + for (; value; value = OVL_NEXT (value)) { - binding_entry temp = table->chain[i]; - while (temp != NULL) - { - binding_entry entry = temp; - temp = entry->chain; - binding_entry_free (entry); - } - table->chain[i] = NULL; - } - table->entry_count = 0; -} -#endif + /* We don't want to find arbitrary hidden functions via argument + dependent lookup. We only want to find friends of associated + classes, which we'll do via arg_assoc_class. */ + if (hidden_name_p (OVL_CURRENT (value))) + continue; -/* Allocate a table with CHAIN_COUNT, assumed to be a power of two. */ + if (add_function (k, OVL_CURRENT (value))) + return true; + } -static inline binding_table -binding_table_new (size_t chain_count) -{ - binding_table table = ggc_alloc (); - table->chain = NULL; - binding_table_construct (table, chain_count); - return table; + return false; } -/* Expand TABLE to twice its current chain_count. */ +/* Adds everything associated with a template argument to the lookup + structure. Returns true on error. */ -static void -binding_table_expand (binding_table table) +static bool +arg_assoc_template_arg (struct arg_lookup *k, tree arg) { - const size_t old_chain_count = table->chain_count; - const size_t old_entry_count = table->entry_count; - const size_t new_chain_count = 2 * old_chain_count; - binding_entry *old_chains = table->chain; - size_t i; + /* [basic.lookup.koenig] - binding_table_construct (table, new_chain_count); - for (i = 0; i < old_chain_count; ++i) + If T is a template-id, its associated namespaces and classes are + ... the namespaces and classes associated with the types of the + template arguments provided for template type parameters + (excluding template template parameters); the namespaces in which + any template template arguments are defined; and the classes in + which any member templates used as template template arguments + are defined. [Note: non-type template arguments do not + contribute to the set of associated namespaces. ] */ + + /* Consider first template template arguments. */ + if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM + || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE) + return false; + else if (TREE_CODE (arg) == TEMPLATE_DECL) { - binding_entry entry = old_chains[i]; - for (; entry != NULL; entry = old_chains[i]) - { - const unsigned int hash = IDENTIFIER_HASH_VALUE (entry->name); - const size_t j = ENTRY_INDEX (hash, new_chain_count); + tree ctx = CP_DECL_CONTEXT (arg); - old_chains[i] = entry->chain; - entry->chain = table->chain[j]; - table->chain[j] = entry; - } + /* It's not a member template. */ + if (TREE_CODE (ctx) == NAMESPACE_DECL) + return arg_assoc_namespace (k, ctx); + /* Otherwise, it must be member template. */ + else + return arg_assoc_class_only (k, ctx); } - table->entry_count = old_entry_count; + /* It's an argument pack; handle it recursively. */ + else if (ARGUMENT_PACK_P (arg)) + { + tree args = ARGUMENT_PACK_ARGS (arg); + int i, len = TREE_VEC_LENGTH (args); + for (i = 0; i < len; ++i) + if (arg_assoc_template_arg (k, TREE_VEC_ELT (args, i))) + return true; + + return false; + } + /* It's not a template template argument, but it is a type template + argument. */ + else if (TYPE_P (arg)) + return arg_assoc_type (k, arg); + /* It's a non-type template argument. */ + else + return false; } -/* Insert a binding for NAME to TYPE into TABLE. */ +/* Adds the class and its friends to the lookup structure. + Returns true on error. */ -static void -binding_table_insert (binding_table table, tree name, tree type) +static bool +arg_assoc_class_only (struct arg_lookup *k, tree type) { - const unsigned int hash = IDENTIFIER_HASH_VALUE (name); - const size_t i = ENTRY_INDEX (hash, table->chain_count); - binding_entry entry = binding_entry_make (name, type); + tree list, friends, context; - entry->chain = table->chain[i]; - table->chain[i] = entry; - ++table->entry_count; + /* Backend-built structures, such as __builtin_va_list, aren't + affected by all this. */ + if (!CLASS_TYPE_P (type)) + return false; - if (3 * table->chain_count < 5 * table->entry_count) - binding_table_expand (table); -} + context = decl_namespace_context (type); + if (arg_assoc_namespace (k, context)) + return true; -/* Return the binding_entry, if any, that maps NAME. */ + complete_type (type); -binding_entry -binding_table_find (binding_table table, tree name) -{ - const unsigned int hash = IDENTIFIER_HASH_VALUE (name); - binding_entry entry = table->chain[ENTRY_INDEX (hash, table->chain_count)]; + /* Process friends. */ + for (list = DECL_FRIENDLIST (TYPE_MAIN_DECL (type)); list; + list = TREE_CHAIN (list)) + if (k->name == FRIEND_NAME (list)) + for (friends = FRIEND_DECLS (list); friends; + friends = TREE_CHAIN (friends)) + { + tree fn = TREE_VALUE (friends); - while (entry != NULL && entry->name != name) - entry = entry->chain; + /* Only interested in global functions with potentially hidden + (i.e. unqualified) declarations. */ + if (CP_DECL_CONTEXT (fn) != context) + continue; + /* Template specializations are never found by name lookup. + (Templates themselves can be found, but not template + specializations.) */ + if (TREE_CODE (fn) == FUNCTION_DECL && DECL_USE_TEMPLATE (fn)) + continue; + if (add_function (k, fn)) + return true; + } - return entry; + return false; } -/* Apply PROC -- with DATA -- to all entries in TABLE. */ +/* Adds the class and its bases to the lookup structure. + Returns true on error. */ -void -binding_table_foreach (binding_table table, bt_foreach_proc proc, void *data) +static bool +arg_assoc_bases (struct arg_lookup *k, tree type) { - size_t chain_count; - size_t i; - - if (!table) - return; + if (arg_assoc_class_only (k, type)) + return true; - chain_count = table->chain_count; - for (i = 0; i < chain_count; ++i) + if (TYPE_BINFO (type)) { - binding_entry entry = table->chain[i]; - for (; entry != NULL; entry = entry->chain) - proc (entry, data); - } -} - -#ifndef ENABLE_SCOPE_CHECKING -# define ENABLE_SCOPE_CHECKING 0 -#else -# define ENABLE_SCOPE_CHECKING 1 -#endif - -/* A free list of "cxx_binding"s, connected by their PREVIOUS. */ - -static GTY((deletable)) cxx_binding *free_bindings; + /* Process baseclasses. */ + tree binfo, base_binfo; + int i; -/* Initialize VALUE and TYPE field for BINDING, and set the PREVIOUS - field to NULL. */ + for (binfo = TYPE_BINFO (type), i = 0; + BINFO_BASE_ITERATE (binfo, i, base_binfo); i++) + if (arg_assoc_bases (k, BINFO_TYPE (base_binfo))) + return true; + } -static inline void -cxx_binding_init (cxx_binding *binding, tree value, tree type) -{ - binding->value = value; - binding->type = type; - binding->previous = NULL; + return false; } -/* (GC)-allocate a binding object with VALUE and TYPE member initialized. */ +/* Adds everything associated with a class argument type to the lookup + structure. Returns true on error. -static cxx_binding * -cxx_binding_make (tree value, tree type) + If T is a class type (including unions), its associated classes are: the + class itself; the class of which it is a member, if any; and its direct + and indirect base classes. Its associated namespaces are the namespaces + of which its associated classes are members. Furthermore, if T is a + class template specialization, its associated namespaces and classes + also include: the namespaces and classes associated with the types of + the template arguments provided for template type parameters (excluding + template template parameters); the namespaces of which any template + template arguments are members; and the classes of which any member + templates used as template template arguments are members. [ Note: + non-type template arguments do not contribute to the set of associated + namespaces. --end note] */ + +static bool +arg_assoc_class (struct arg_lookup *k, tree type) { - cxx_binding *binding; - if (free_bindings) - { - binding = free_bindings; - free_bindings = binding->previous; - } - else - binding = ggc_alloc (); + tree list; + int i; - cxx_binding_init (binding, value, type); + /* Backend build structures, such as __builtin_va_list, aren't + affected by all this. */ + if (!CLASS_TYPE_P (type)) + return false; - return binding; -} + if (vec_member (type, k->classes)) + return false; + vec_safe_push (k->classes, type); -/* Put BINDING back on the free list. */ + if (TYPE_CLASS_SCOPE_P (type) + && arg_assoc_class_only (k, TYPE_CONTEXT (type))) + return true; -static inline void -cxx_binding_free (cxx_binding *binding) -{ - binding->scope = NULL; - binding->previous = free_bindings; - free_bindings = binding; -} + if (arg_assoc_bases (k, type)) + return true; -/* Create a new binding for NAME (with the indicated VALUE and TYPE - bindings) in the class scope indicated by SCOPE. */ + /* Process template arguments. */ + if (CLASSTYPE_TEMPLATE_INFO (type) + && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type))) + { + list = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)); + for (i = 0; i < TREE_VEC_LENGTH (list); ++i) + if (arg_assoc_template_arg (k, TREE_VEC_ELT (list, i))) + return true; + } -static cxx_binding * -new_class_binding (tree name, tree value, tree type, cp_binding_level *scope) -{ - cp_class_binding cb = {cxx_binding_make (value, type), name}; - cxx_binding *binding = cb.base; - vec_safe_push (scope->class_shadowed, cb); - binding->scope = scope; - return binding; + return false; } -/* Make DECL the innermost binding for ID. The LEVEL is the binding - level at which this declaration is being bound. */ +/* Adds everything associated with a given type. + Returns 1 on error. */ -void -push_binding (tree id, tree decl, cp_binding_level* level) +static bool +arg_assoc_type (struct arg_lookup *k, tree type) { - cxx_binding *binding; + /* As we do not get the type of non-type dependent expressions + right, we can end up with such things without a type. */ + if (!type) + return false; - if (level != class_binding_level) + if (TYPE_PTRDATAMEM_P (type)) { - binding = cxx_binding_make (decl, NULL_TREE); - binding->scope = level; + /* Pointer to member: associate class type and value type. */ + if (arg_assoc_type (k, TYPE_PTRMEM_CLASS_TYPE (type))) + return true; + return arg_assoc_type (k, TYPE_PTRMEM_POINTED_TO_TYPE (type)); } - else - binding = new_class_binding (id, decl, /*type=*/NULL_TREE, level); + else switch (TREE_CODE (type)) + { + case ERROR_MARK: + return false; + case VOID_TYPE: + case INTEGER_TYPE: + case REAL_TYPE: + case COMPLEX_TYPE: + case VECTOR_TYPE: + case BOOLEAN_TYPE: + case FIXED_POINT_TYPE: + case DECLTYPE_TYPE: + case NULLPTR_TYPE: + return false; + case RECORD_TYPE: + if (TYPE_PTRMEMFUNC_P (type)) + return arg_assoc_type (k, TYPE_PTRMEMFUNC_FN_TYPE (type)); + /* FALLTHRU */ + case UNION_TYPE: + return arg_assoc_class (k, type); + case POINTER_TYPE: + case REFERENCE_TYPE: + case ARRAY_TYPE: + return arg_assoc_type (k, TREE_TYPE (type)); + case ENUMERAL_TYPE: + if (TYPE_CLASS_SCOPE_P (type) + && arg_assoc_class_only (k, TYPE_CONTEXT (type))) + return true; + return arg_assoc_namespace (k, decl_namespace_context (type)); + case METHOD_TYPE: + /* The basetype is referenced in the first arg type, so just + fall through. */ + case FUNCTION_TYPE: + /* Associate the parameter types. */ + if (arg_assoc_args (k, TYPE_ARG_TYPES (type))) + return true; + /* Associate the return type. */ + return arg_assoc_type (k, TREE_TYPE (type)); + case TEMPLATE_TYPE_PARM: + case BOUND_TEMPLATE_TEMPLATE_PARM: + return false; + case TYPENAME_TYPE: + return false; + case LANG_TYPE: + gcc_assert (type == unknown_type_node + || type == init_list_type_node); + return false; + case TYPE_PACK_EXPANSION: + return arg_assoc_type (k, PACK_EXPANSION_PATTERN (type)); - /* Now, fill in the binding information. */ - binding->previous = IDENTIFIER_BINDING (id); - INHERITED_VALUE_BINDING_P (binding) = 0; - LOCAL_BINDING_P (binding) = (level != class_binding_level); + default: + gcc_unreachable (); + } + return false; +} - /* And put it on the front of the list of bindings for ID. */ - IDENTIFIER_BINDING (id) = binding; +/* Adds everything associated with arguments. Returns true on error. */ + +static bool +arg_assoc_args (struct arg_lookup *k, tree args) +{ + for (; args; args = TREE_CHAIN (args)) + if (arg_assoc (k, TREE_VALUE (args))) + return true; + return false; } -/* Remove the binding for DECL which should be the innermost binding - for ID. */ +/* Adds everything associated with an argument vector. Returns true + on error. */ -void -pop_binding (tree id, tree decl) +static bool +arg_assoc_args_vec (struct arg_lookup *k, vec *args) { - cxx_binding *binding; + unsigned int ix; + tree arg; - if (id == NULL_TREE) - /* It's easiest to write the loops that call this function without - checking whether or not the entities involved have names. We - get here for such an entity. */ - return; + FOR_EACH_VEC_SAFE_ELT (args, ix, arg) + if (arg_assoc (k, arg)) + return true; + return false; +} - /* Get the innermost binding for ID. */ - binding = IDENTIFIER_BINDING (id); +/* Adds everything associated with a given tree_node. Returns 1 on error. */ - /* The name should be bound. */ - gcc_assert (binding != NULL); +static bool +arg_assoc (struct arg_lookup *k, tree n) +{ + if (n == error_mark_node) + return false; - /* The DECL will be either the ordinary binding or the type - binding for this identifier. Remove that binding. */ - if (binding->value == decl) - binding->value = NULL_TREE; - else - { - gcc_assert (binding->type == decl); - binding->type = NULL_TREE; - } + if (TYPE_P (n)) + return arg_assoc_type (k, n); - if (!binding->value && !binding->type) + if (! type_unknown_p (n)) + return arg_assoc_type (k, TREE_TYPE (n)); + + if (TREE_CODE (n) == ADDR_EXPR) + n = TREE_OPERAND (n, 0); + if (TREE_CODE (n) == COMPONENT_REF) + n = TREE_OPERAND (n, 1); + if (TREE_CODE (n) == OFFSET_REF) + n = TREE_OPERAND (n, 1); + while (TREE_CODE (n) == TREE_LIST) + n = TREE_VALUE (n); + if (BASELINK_P (n)) + n = BASELINK_FUNCTIONS (n); + + if (TREE_CODE (n) == FUNCTION_DECL) + return arg_assoc_type (k, TREE_TYPE (n)); + if (TREE_CODE (n) == TEMPLATE_ID_EXPR) { - /* We're completely done with the innermost binding for this - identifier. Unhook it from the list of bindings. */ - IDENTIFIER_BINDING (id) = binding->previous; + /* The working paper doesn't currently say how to handle template-id + arguments. The sensible thing would seem to be to handle the list + of template candidates like a normal overload set, and handle the + template arguments like we do for class template + specializations. */ + tree templ = TREE_OPERAND (n, 0); + tree args = TREE_OPERAND (n, 1); + int ix; - /* Add it to the free list. */ - cxx_binding_free (binding); + /* First the templates. */ + if (arg_assoc (k, templ)) + return true; + + /* Now the arguments. */ + if (args) + for (ix = TREE_VEC_LENGTH (args); ix--;) + if (arg_assoc_template_arg (k, TREE_VEC_ELT (args, ix)) == 1) + return true; } + else if (TREE_CODE (n) == OVERLOAD) + { + for (; n; n = OVL_NEXT (n)) + if (arg_assoc_type (k, TREE_TYPE (OVL_CURRENT (n)))) + return true; + } + + return false; } -/* Remove the bindings for the decls of the current level and leave - the current scope. */ +/* Performs Koenig lookup depending on arguments, where fns + are the functions found in normal lookup. */ -void -pop_bindings_and_leave_scope (void) +static cp_expr +lookup_arg_dependent_1 (tree name, tree fns, vec *args) { - for (tree t = getdecls (); t; t = DECL_CHAIN (t)) - pop_binding (DECL_NAME (t), t); - leave_scope (); -} + struct arg_lookup k; -/* Strip non dependent using declarations. If DECL is dependent, - surreptitiously create a typename_type and return it. */ + /* Remove any hidden friend functions from the list of functions + found so far. They will be added back by arg_assoc_class as + appropriate. */ + fns = remove_hidden_names (fns); -tree -strip_using_decl (tree decl) -{ - if (decl == NULL_TREE) - return NULL_TREE; + k.name = name; + k.args = args; + k.functions = fns; + k.classes = make_tree_vector (); - while (TREE_CODE (decl) == USING_DECL && !DECL_DEPENDENT_P (decl)) - decl = USING_DECL_DECLS (decl); + /* We previously performed an optimization here by setting + NAMESPACES to the current namespace when it was safe. However, DR + 164 says that namespaces that were already searched in the first + stage of template processing are searched again (potentially + picking up later definitions) in the second stage. */ + k.namespaces = make_tree_vector (); - if (TREE_CODE (decl) == USING_DECL && DECL_DEPENDENT_P (decl) - && USING_DECL_TYPENAME_P (decl)) + /* We used to allow duplicates and let joust discard them, but + since the above change for DR 164 we end up with duplicates of + all the functions found by unqualified lookup. So keep track + of which ones we've seen. */ + if (fns) { - /* We have found a type introduced by a using - declaration at class scope that refers to a dependent - type. - - using typename :: [opt] nested-name-specifier unqualified-id ; - */ - decl = make_typename_type (TREE_TYPE (decl), - DECL_NAME (decl), - typename_type, tf_error); - if (decl != error_mark_node) - decl = TYPE_NAME (decl); + tree ovl; + /* We shouldn't be here if lookup found something other than + namespace-scope functions. */ + gcc_assert (DECL_NAMESPACE_SCOPE_P (OVL_CURRENT (fns))); + k.fn_set = new hash_set; + for (ovl = fns; ovl; ovl = OVL_NEXT (ovl)) + k.fn_set->add (OVL_CURRENT (ovl)); } + else + k.fn_set = NULL; - return decl; -} + arg_assoc_args_vec (&k, args); -/* BINDING records an existing declaration for a name in the current scope. - But, DECL is another declaration for that same identifier in the - same scope. This is the `struct stat' hack whereby a non-typedef - class name or enum-name can be bound at the same level as some other - kind of entity. - 3.3.7/1 + fns = k.functions; + + if (fns + && !VAR_P (fns) + && !is_overloaded_fn (fns)) + { + error ("argument dependent lookup finds %q+D", fns); + error (" in call to %qD", name); + fns = error_mark_node; + } - A class name (9.1) or enumeration name (7.2) can be hidden by the - name of an object, function, or enumerator declared in the same scope. - If a class or enumeration name and an object, function, or enumerator - are declared in the same scope (in any order) with the same name, the - class or enumeration name is hidden wherever the object, function, or - enumerator name is visible. + release_tree_vector (k.classes); + release_tree_vector (k.namespaces); + delete k.fn_set; + + return fns; +} - It's the responsibility of the caller to check that - inserting this name is valid here. Returns nonzero if the new binding - was successful. */ +/* Wrapper for lookup_arg_dependent_1. */ -static bool -supplement_binding_1 (cxx_binding *binding, tree decl) +cp_expr +lookup_arg_dependent (tree name, tree fns, vec *args) { - tree bval = binding->value; - bool ok = true; - tree target_bval = strip_using_decl (bval); - tree target_decl = strip_using_decl (decl); + cp_expr ret; + bool subtime; + subtime = timevar_cond_start (TV_NAME_LOOKUP); + ret = lookup_arg_dependent_1 (name, fns, args); + timevar_cond_stop (TV_NAME_LOOKUP, subtime); + return ret; +} - if (TREE_CODE (target_decl) == TYPE_DECL && DECL_ARTIFICIAL (target_decl) - && target_decl != target_bval - && (TREE_CODE (target_bval) != TYPE_DECL - /* We allow pushing an enum multiple times in a class - template in order to handle late matching of underlying - type on an opaque-enum-declaration followed by an - enum-specifier. */ - || (processing_template_decl - && TREE_CODE (TREE_TYPE (target_decl)) == ENUMERAL_TYPE - && TREE_CODE (TREE_TYPE (target_bval)) == ENUMERAL_TYPE - && (dependent_type_p (ENUM_UNDERLYING_TYPE - (TREE_TYPE (target_decl))) - || dependent_type_p (ENUM_UNDERLYING_TYPE - (TREE_TYPE (target_bval))))))) - /* The new name is the type name. */ - binding->type = decl; - else if (/* TARGET_BVAL is null when push_class_level_binding moves - an inherited type-binding out of the way to make room - for a new value binding. */ - !target_bval - /* TARGET_BVAL is error_mark_node when TARGET_DECL's name - has been used in a non-class scope prior declaration. - In that case, we should have already issued a - diagnostic; for graceful error recovery purpose, pretend - this was the intended declaration for that name. */ - || target_bval == error_mark_node - /* If TARGET_BVAL is anticipated but has not yet been - declared, pretend it is not there at all. */ - || (TREE_CODE (target_bval) == FUNCTION_DECL - && DECL_ANTICIPATED (target_bval) - && !DECL_HIDDEN_FRIEND_P (target_bval))) - binding->value = decl; - else if (TREE_CODE (target_bval) == TYPE_DECL - && DECL_ARTIFICIAL (target_bval) - && target_decl != target_bval - && (TREE_CODE (target_decl) != TYPE_DECL - || same_type_p (TREE_TYPE (target_decl), - TREE_TYPE (target_bval)))) +/* Initialize anonymous_namespace_name if necessary, and return it. */ + +static tree +get_anonymous_namespace_name (void) +{ + if (!anonymous_namespace_name) { - /* The old binding was a type name. It was placed in - VALUE field because it was thought, at the point it was - declared, to be the only entity with such a name. Move the - type name into the type slot; it is now hidden by the new - binding. */ - binding->type = bval; - binding->value = decl; - binding->value_is_inherited = false; + /* We used to use get_file_function_name here, but that isn't + necessary now that anonymous namespace typeinfos + are !TREE_PUBLIC, and thus compared by address. */ + /* The demangler expects anonymous namespaces to be called + something starting with '_GLOBAL__N_'. */ + anonymous_namespace_name = get_identifier ("_GLOBAL__N_1"); } - else if (TREE_CODE (target_bval) == TYPE_DECL - && TREE_CODE (target_decl) == TYPE_DECL - && DECL_NAME (target_decl) == DECL_NAME (target_bval) - && binding->scope->kind != sk_class - && (same_type_p (TREE_TYPE (target_decl), TREE_TYPE (target_bval)) - /* If either type involves template parameters, we must - wait until instantiation. */ - || uses_template_parms (TREE_TYPE (target_decl)) - || uses_template_parms (TREE_TYPE (target_bval)))) - /* We have two typedef-names, both naming the same type to have - the same name. In general, this is OK because of: - - [dcl.typedef] + return anonymous_namespace_name; +} - In a given scope, a typedef specifier can be used to redefine - the name of any type declared in that scope to refer to the - type to which it already refers. +/* Compute the chain index of a binding_entry given the HASH value of its + name and the total COUNT of chains. COUNT is assumed to be a power + of 2. */ - However, in class scopes, this rule does not apply due to the - stricter language in [class.mem] prohibiting redeclarations of - members. */ - ok = false; - /* There can be two block-scope declarations of the same variable, - so long as they are `extern' declarations. However, there cannot - be two declarations of the same static data member: +#define ENTRY_INDEX(HASH, COUNT) (((HASH) >> 3) & ((COUNT) - 1)) - [class.mem] +/* A free list of "binding_entry"s awaiting for re-use. */ - A member shall not be declared twice in the - member-specification. */ - else if (VAR_P (target_decl) - && VAR_P (target_bval) - && DECL_EXTERNAL (target_decl) && DECL_EXTERNAL (target_bval) - && !DECL_CLASS_SCOPE_P (target_decl)) - { - duplicate_decls (decl, binding->value, /*newdecl_is_friend=*/false); - ok = false; - } - else if (TREE_CODE (decl) == NAMESPACE_DECL - && TREE_CODE (bval) == NAMESPACE_DECL - && DECL_NAMESPACE_ALIAS (decl) - && DECL_NAMESPACE_ALIAS (bval) - && ORIGINAL_NAMESPACE (bval) == ORIGINAL_NAMESPACE (decl)) - /* [namespace.alias] +static GTY((deletable)) binding_entry free_binding_entry = NULL; - In a declarative region, a namespace-alias-definition can be - used to redefine a namespace-alias declared in that declarative - region to refer only to the namespace to which it already - refers. */ - ok = false; - else if (maybe_remove_implicit_alias (bval)) - { - /* There was a mangling compatibility alias using this mangled name, - but now we have a real decl that wants to use it instead. */ - binding->value = decl; - } - else - { - if (!error_operand_p (bval)) - diagnose_name_conflict (decl, bval); - ok = false; - } +/* The binding oracle; see cp-tree.h. */ - return ok; -} +cp_binding_oracle_function *cp_binding_oracle; -/* Diagnose a name conflict between DECL and BVAL. */ +/* If we have a binding oracle, ask it for all namespace-scoped + definitions of NAME. */ -static void -diagnose_name_conflict (tree decl, tree bval) +static inline void +query_oracle (tree name) { - if (TREE_CODE (decl) == TREE_CODE (bval) - && (TREE_CODE (decl) != TYPE_DECL - || (DECL_ARTIFICIAL (decl) && DECL_ARTIFICIAL (bval)) - || (!DECL_ARTIFICIAL (decl) && !DECL_ARTIFICIAL (bval))) - && !is_overloaded_fn (decl)) - error ("redeclaration of %q#D", decl); - else - error ("%q#D conflicts with a previous declaration", decl); - - inform (location_of (bval), "previous declaration %q#D", bval); -} + if (!cp_binding_oracle) + return; -/* Wrapper for supplement_binding_1. */ + /* LOOKED_UP holds the set of identifiers that we have already + looked up with the oracle. */ + static hash_set looked_up; + if (looked_up.add (name)) + return; -static bool -supplement_binding (cxx_binding *binding, tree decl) -{ - bool ret; - bool subtime = timevar_cond_start (TV_NAME_LOOKUP); - ret = supplement_binding_1 (binding, decl); - timevar_cond_stop (TV_NAME_LOOKUP, subtime); - return ret; + cp_binding_oracle (CP_ORACLE_IDENTIFIER, name); } -/* Add DECL to the list of things declared in B. */ +/* Create a binding_entry object for (NAME, TYPE). */ -static void -add_decl_to_level (tree decl, cp_binding_level *b) +static inline binding_entry +binding_entry_make (tree name, tree type) { - /* We used to record virtual tables as if they were ordinary - variables, but no longer do so. */ - gcc_assert (!(VAR_P (decl) && DECL_VIRTUAL_P (decl))); + binding_entry entry; - if (TREE_CODE (decl) == NAMESPACE_DECL - && !DECL_NAMESPACE_ALIAS (decl)) + if (free_binding_entry) { - DECL_CHAIN (decl) = b->namespaces; - b->namespaces = decl; + entry = free_binding_entry; + free_binding_entry = entry->chain; } else - { - /* We build up the list in reverse order, and reverse it later if - necessary. */ - TREE_CHAIN (decl) = b->names; - b->names = decl; - - /* If appropriate, add decl to separate list of statics. We - include extern variables because they might turn out to be - static later. It's OK for this list to contain a few false - positives. */ - if (b->kind == sk_namespace) - if ((VAR_P (decl) - && (TREE_STATIC (decl) || DECL_EXTERNAL (decl))) - || (TREE_CODE (decl) == FUNCTION_DECL - && (!TREE_PUBLIC (decl) - || decl_anon_ns_mem_p (decl) - || DECL_DECLARED_INLINE_P (decl)))) - vec_safe_push (b->static_decls, decl); - } -} + entry = ggc_alloc (); -/* Record a decl-node X as belonging to the current lexical scope. - Check for errors (such as an incompatible declaration for the same - name already seen in the same scope). IS_FRIEND is true if X is - declared as a friend. + entry->name = name; + entry->type = type; + entry->chain = NULL; - Returns either X or an old decl for the same name. - If an old decl is returned, it may have been smashed - to agree with what X says. */ + return entry; +} -static tree -pushdecl_maybe_friend_1 (tree x, bool is_friend) +/* Put ENTRY back on the free list. */ +#if 0 +static inline void +binding_entry_free (binding_entry entry) { - tree t; - tree name; - int need_new_binding; + entry->name = NULL; + entry->type = NULL; + entry->chain = free_binding_entry; + free_binding_entry = entry; +} +#endif - if (x == error_mark_node) - return error_mark_node; +/* The datatype used to implement the mapping from names to types at + a given scope. */ +struct GTY(()) binding_table_s { + /* Array of chains of "binding_entry"s */ + binding_entry * GTY((length ("%h.chain_count"))) chain; - need_new_binding = 1; + /* The number of chains in this table. This is the length of the + member "chain" considered as an array. */ + size_t chain_count; - if (DECL_TEMPLATE_PARM_P (x)) - /* Template parameters have no context; they are not X::T even - when declared within a class or namespace. */ - ; - else - { - if (current_function_decl && x != current_function_decl - /* A local declaration for a function doesn't constitute - nesting. */ - && TREE_CODE (x) != FUNCTION_DECL - /* A local declaration for an `extern' variable is in the - scope of the current namespace, not the current - function. */ - && !(VAR_P (x) && DECL_EXTERNAL (x)) - /* When parsing the parameter list of a function declarator, - don't set DECL_CONTEXT to an enclosing function. When we - push the PARM_DECLs in order to process the function body, - current_binding_level->this_entity will be set. */ - && !(TREE_CODE (x) == PARM_DECL - && current_binding_level->kind == sk_function_parms - && current_binding_level->this_entity == NULL) - && !DECL_CONTEXT (x)) - DECL_CONTEXT (x) = current_function_decl; + /* Number of "binding_entry"s in this table. */ + size_t entry_count; +}; - /* If this is the declaration for a namespace-scope function, - but the declaration itself is in a local scope, mark the - declaration. */ - if (TREE_CODE (x) == FUNCTION_DECL - && DECL_NAMESPACE_SCOPE_P (x) - && current_function_decl - && x != current_function_decl) - DECL_LOCAL_FUNCTION_P (x) = 1; - } +/* Construct TABLE with an initial CHAIN_COUNT. */ - name = DECL_NAME (x); - if (name) - { - int different_binding_level = 0; +static inline void +binding_table_construct (binding_table table, size_t chain_count) +{ + table->chain_count = chain_count; + table->entry_count = 0; + table->chain = ggc_cleared_vec_alloc (table->chain_count); +} - if (TREE_CODE (name) == TEMPLATE_ID_EXPR) - name = TREE_OPERAND (name, 0); +/* Make TABLE's entries ready for reuse. */ +#if 0 +static void +binding_table_free (binding_table table) +{ + size_t i; + size_t count; - /* In case this decl was explicitly namespace-qualified, look it - up in its namespace context. */ - if (DECL_NAMESPACE_SCOPE_P (x) && namespace_bindings_p ()) - t = namespace_binding (name, DECL_CONTEXT (x)); - else - t = lookup_name_innermost_nonclass_level (name); + if (table == NULL) + return; - /* [basic.link] If there is a visible declaration of an entity - with linkage having the same name and type, ignoring entities - declared outside the innermost enclosing namespace scope, the - block scope declaration declares that same entity and - receives the linkage of the previous declaration. */ - if (! t && current_function_decl && x != current_function_decl - && VAR_OR_FUNCTION_DECL_P (x) - && DECL_EXTERNAL (x)) + for (i = 0, count = table->chain_count; i < count; ++i) + { + binding_entry temp = table->chain[i]; + while (temp != NULL) { - /* Look in block scope. */ - t = innermost_non_namespace_value (name); - /* Or in the innermost namespace. */ - if (! t) - t = namespace_binding (name, DECL_CONTEXT (x)); - /* Does it have linkage? Note that if this isn't a DECL, it's an - OVERLOAD, which is OK. */ - if (t && DECL_P (t) && ! (TREE_STATIC (t) || DECL_EXTERNAL (t))) - t = NULL_TREE; - if (t) - different_binding_level = 1; + binding_entry entry = temp; + temp = entry->chain; + binding_entry_free (entry); } + table->chain[i] = NULL; + } + table->entry_count = 0; +} +#endif - /* If we are declaring a function, and the result of name-lookup - was an OVERLOAD, look for an overloaded instance that is - actually the same as the function we are declaring. (If - there is one, we have to merge our declaration with the - previous declaration.) */ - if (t && TREE_CODE (t) == OVERLOAD) - { - tree match; +/* Allocate a table with CHAIN_COUNT, assumed to be a power of two. */ - if (TREE_CODE (x) == FUNCTION_DECL) - for (match = t; match; match = OVL_NEXT (match)) - { - if (decls_match (OVL_CURRENT (match), x)) - break; - } - else - /* Just choose one. */ - match = t; +static inline binding_table +binding_table_new (size_t chain_count) +{ + binding_table table = ggc_alloc (); + table->chain = NULL; + binding_table_construct (table, chain_count); + return table; +} - if (match) - t = OVL_CURRENT (match); - else - t = NULL_TREE; - } +/* Expand TABLE to twice its current chain_count. */ - if (t && t != error_mark_node) +static void +binding_table_expand (binding_table table) +{ + const size_t old_chain_count = table->chain_count; + const size_t old_entry_count = table->entry_count; + const size_t new_chain_count = 2 * old_chain_count; + binding_entry *old_chains = table->chain; + size_t i; + + binding_table_construct (table, new_chain_count); + for (i = 0; i < old_chain_count; ++i) + { + binding_entry entry = old_chains[i]; + for (; entry != NULL; entry = old_chains[i]) { - if (different_binding_level) - { - if (decls_match (x, t)) - /* The standard only says that the local extern - inherits linkage from the previous decl; in - particular, default args are not shared. Add - the decl into a hash table to make sure only - the previous decl in this case is seen by the - middle end. */ - { - struct cxx_int_tree_map *h; + const unsigned int hash = IDENTIFIER_HASH_VALUE (entry->name); + const size_t j = ENTRY_INDEX (hash, new_chain_count); - TREE_PUBLIC (x) = TREE_PUBLIC (t); + old_chains[i] = entry->chain; + entry->chain = table->chain[j]; + table->chain[j] = entry; + } + } + table->entry_count = old_entry_count; +} - if (cp_function_chain->extern_decl_map == NULL) - cp_function_chain->extern_decl_map - = hash_table::create_ggc (20); +/* Insert a binding for NAME to TYPE into TABLE. */ - h = ggc_alloc (); - h->uid = DECL_UID (x); - h->to = t; - cxx_int_tree_map **loc = cp_function_chain->extern_decl_map - ->find_slot (h, INSERT); - *loc = h; - } - } - else if (TREE_CODE (t) == PARM_DECL) - { - /* Check for duplicate params. */ - tree d = duplicate_decls (x, t, is_friend); - if (d) - return d; - } - else if ((DECL_EXTERN_C_FUNCTION_P (x) - || DECL_FUNCTION_TEMPLATE_P (x)) - && is_overloaded_fn (t)) - /* Don't do anything just yet. */; - else if (t == wchar_decl_node) - { - if (! DECL_IN_SYSTEM_HEADER (x)) - pedwarn (input_location, OPT_Wpedantic, "redeclaration of % as %qT", - TREE_TYPE (x)); - - /* Throw away the redeclaration. */ - return t; - } - else - { - tree olddecl = duplicate_decls (x, t, is_friend); +static void +binding_table_insert (binding_table table, tree name, tree type) +{ + const unsigned int hash = IDENTIFIER_HASH_VALUE (name); + const size_t i = ENTRY_INDEX (hash, table->chain_count); + binding_entry entry = binding_entry_make (name, type); - /* If the redeclaration failed, we can stop at this - point. */ - if (olddecl == error_mark_node) - return error_mark_node; + entry->chain = table->chain[i]; + table->chain[i] = entry; + ++table->entry_count; - if (olddecl) - { - if (TREE_CODE (t) == TYPE_DECL) - SET_IDENTIFIER_TYPE_VALUE (name, TREE_TYPE (t)); + if (3 * table->chain_count < 5 * table->entry_count) + binding_table_expand (table); +} - return t; - } - else if (DECL_MAIN_P (x) && TREE_CODE (t) == FUNCTION_DECL) - { - /* A redeclaration of main, but not a duplicate of the - previous one. +/* Return the binding_entry, if any, that maps NAME. */ - [basic.start.main] +binding_entry +binding_table_find (binding_table table, tree name) +{ + const unsigned int hash = IDENTIFIER_HASH_VALUE (name); + binding_entry entry = table->chain[ENTRY_INDEX (hash, table->chain_count)]; - This function shall not be overloaded. */ - error ("invalid redeclaration of %q+D", t); - error ("as %qD", x); - /* We don't try to push this declaration since that - causes a crash. */ - return x; - } - } - } + while (entry != NULL && entry->name != name) + entry = entry->chain; - /* If x has C linkage-specification, (extern "C"), - lookup its binding, in case it's already bound to an object. - The lookup is done in all namespaces. - If we find an existing binding, make sure it has the same - exception specification as x, otherwise, bail in error [7.5, 7.6]. */ - if ((TREE_CODE (x) == FUNCTION_DECL) - && DECL_EXTERN_C_P (x) - /* We should ignore declarations happening in system headers. */ - && !DECL_ARTIFICIAL (x) - && !DECL_IN_SYSTEM_HEADER (x)) - { - tree previous = lookup_extern_c_fun_in_all_ns (x); - if (previous - && !DECL_ARTIFICIAL (previous) - && !DECL_IN_SYSTEM_HEADER (previous) - && DECL_CONTEXT (previous) != DECL_CONTEXT (x)) - { - /* In case either x or previous is declared to throw an exception, - make sure both exception specifications are equal. */ - if (decls_match (x, previous)) - { - tree x_exception_spec = NULL_TREE; - tree previous_exception_spec = NULL_TREE; - - x_exception_spec = - TYPE_RAISES_EXCEPTIONS (TREE_TYPE (x)); - previous_exception_spec = - TYPE_RAISES_EXCEPTIONS (TREE_TYPE (previous)); - if (!comp_except_specs (previous_exception_spec, - x_exception_spec, - ce_normal)) - { - pedwarn (input_location, 0, - "declaration of %q#D with C language linkage", - x); - pedwarn (DECL_SOURCE_LOCATION (previous), 0, - "conflicts with previous declaration %q#D", - previous); - pedwarn (input_location, 0, - "due to different exception specifications"); - return error_mark_node; - } - if (DECL_ASSEMBLER_NAME_SET_P (previous)) - SET_DECL_ASSEMBLER_NAME (x, - DECL_ASSEMBLER_NAME (previous)); - } - else - { - pedwarn (input_location, 0, - "declaration of %q#D with C language linkage", x); - pedwarn (DECL_SOURCE_LOCATION (previous), 0, - "conflicts with previous declaration %q#D", - previous); - } - } - } + return entry; +} - check_template_shadow (x); +/* Apply PROC -- with DATA -- to all entries in TABLE. */ - /* If this is a function conjured up by the back end, massage it - so it looks friendly. */ - if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_LANG_SPECIFIC (x)) - { - retrofit_lang_decl (x); - SET_DECL_LANGUAGE (x, lang_c); - } +void +binding_table_foreach (binding_table table, bt_foreach_proc proc, void *data) +{ + size_t chain_count; + size_t i; - t = x; - if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_FUNCTION_MEMBER_P (x)) - { - t = push_overloaded_decl (x, PUSH_LOCAL, is_friend); - if (!namespace_bindings_p ()) - /* We do not need to create a binding for this name; - push_overloaded_decl will have already done so if - necessary. */ - need_new_binding = 0; - } - else if (DECL_FUNCTION_TEMPLATE_P (x) && DECL_NAMESPACE_SCOPE_P (x)) - { - t = push_overloaded_decl (x, PUSH_GLOBAL, is_friend); - if (t == x) - add_decl_to_level (x, NAMESPACE_LEVEL (CP_DECL_CONTEXT (t))); - } + if (!table) + return; - if (DECL_DECLARES_FUNCTION_P (t)) - { - check_default_args (t); + chain_count = table->chain_count; + for (i = 0; i < chain_count; ++i) + { + binding_entry entry = table->chain[i]; + for (; entry != NULL; entry = entry->chain) + proc (entry, data); + } +} + +#ifndef ENABLE_SCOPE_CHECKING +# define ENABLE_SCOPE_CHECKING 0 +#else +# define ENABLE_SCOPE_CHECKING 1 +#endif - if (is_friend && t == x && !flag_friend_injection) - { - /* This is a new friend declaration of a function or a - function template, so hide it from ordinary function - lookup. */ - DECL_ANTICIPATED (t) = 1; - DECL_HIDDEN_FRIEND_P (t) = 1; - } - } +/* A free list of "cxx_binding"s, connected by their PREVIOUS. */ - if (t != x || DECL_FUNCTION_TEMPLATE_P (t)) - return t; +static GTY((deletable)) cxx_binding *free_bindings; - /* If declaring a type as a typedef, copy the type (unless we're - at line 0), and install this TYPE_DECL as the new type's typedef - name. See the extensive comment of set_underlying_type (). */ - if (TREE_CODE (x) == TYPE_DECL) - { - tree type = TREE_TYPE (x); +/* Initialize VALUE and TYPE field for BINDING, and set the PREVIOUS + field to NULL. */ - if (DECL_IS_BUILTIN (x) - || (TREE_TYPE (x) != error_mark_node - && TYPE_NAME (type) != x - /* We don't want to copy the type when all we're - doing is making a TYPE_DECL for the purposes of - inlining. */ - && (!TYPE_NAME (type) - || TYPE_NAME (type) != DECL_ABSTRACT_ORIGIN (x)))) - set_underlying_type (x); +static inline void +cxx_binding_init (cxx_binding *binding, tree value, tree type) +{ + binding->value = value; + binding->type = type; + binding->previous = NULL; +} - if (type != error_mark_node - && TYPE_IDENTIFIER (type)) - set_identifier_type_value (DECL_NAME (x), x); +/* (GC)-allocate a binding object with VALUE and TYPE member initialized. */ - /* If this is a locally defined typedef in a function that - is not a template instantation, record it to implement - -Wunused-local-typedefs. */ - if (!instantiating_current_function_p ()) - record_locally_defined_typedef (x); - } +static cxx_binding * +cxx_binding_make (tree value, tree type) +{ + cxx_binding *binding; + if (free_bindings) + { + binding = free_bindings; + free_bindings = binding->previous; + } + else + binding = ggc_alloc (); - /* Multiple external decls of the same identifier ought to match. + cxx_binding_init (binding, value, type); - We get warnings about inline functions where they are defined. - We get warnings about other functions from push_overloaded_decl. + return binding; +} - Avoid duplicate warnings where they are used. */ - if (TREE_PUBLIC (x) && TREE_CODE (x) != FUNCTION_DECL) - { - tree decl; +/* Put BINDING back on the free list. */ - decl = IDENTIFIER_NAMESPACE_VALUE (name); - if (decl && TREE_CODE (decl) == OVERLOAD) - decl = OVL_FUNCTION (decl); +static inline void +cxx_binding_free (cxx_binding *binding) +{ + binding->scope = NULL; + binding->previous = free_bindings; + free_bindings = binding; +} - if (decl && decl != error_mark_node - && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl)) - /* If different sort of thing, we already gave an error. */ - && TREE_CODE (decl) == TREE_CODE (x) - && !comptypes (TREE_TYPE (x), TREE_TYPE (decl), - COMPARE_REDECLARATION)) - { - if (permerror (input_location, "type mismatch with previous " - "external decl of %q#D", x)) - inform (DECL_SOURCE_LOCATION (decl), - "previous external decl of %q#D", decl); - } - } +/* Create a new binding for NAME (with the indicated VALUE and TYPE + bindings) in the class scope indicated by SCOPE. */ - /* This name is new in its binding level. - Install the new declaration and return it. */ - if (namespace_bindings_p ()) - { - /* Install a global value. */ +static cxx_binding * +new_class_binding (tree name, tree value, tree type, cp_binding_level *scope) +{ + cp_class_binding cb = {cxx_binding_make (value, type), name}; + cxx_binding *binding = cb.base; + vec_safe_push (scope->class_shadowed, cb); + binding->scope = scope; + return binding; +} - /* If the first global decl has external linkage, - warn if we later see static one. */ - if (IDENTIFIER_GLOBAL_VALUE (name) == NULL_TREE && TREE_PUBLIC (x)) - TREE_PUBLIC (name) = 1; +/* Make DECL the innermost binding for ID. The LEVEL is the binding + level at which this declaration is being bound. */ - /* Bind the name for the entity. */ - if (!(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x) - && t != NULL_TREE) - && (TREE_CODE (x) == TYPE_DECL - || VAR_P (x) - || TREE_CODE (x) == NAMESPACE_DECL - || TREE_CODE (x) == CONST_DECL - || TREE_CODE (x) == TEMPLATE_DECL)) - SET_IDENTIFIER_NAMESPACE_VALUE (name, x); +void +push_binding (tree id, tree decl, cp_binding_level* level) +{ + cxx_binding *binding; - /* If new decl is `static' and an `extern' was seen previously, - warn about it. */ - if (x != NULL_TREE && t != NULL_TREE && decls_match (x, t)) - warn_extern_redeclared_static (x, t); - } - else - { - /* Here to install a non-global value. */ - tree oldglobal = IDENTIFIER_NAMESPACE_VALUE (name); - tree oldlocal = NULL_TREE; - cp_binding_level *oldscope = NULL; - cxx_binding *oldbinding = outer_binding (name, NULL, true); - if (oldbinding) - { - oldlocal = oldbinding->value; - oldscope = oldbinding->scope; - } + if (level != class_binding_level) + { + binding = cxx_binding_make (decl, NULL_TREE); + binding->scope = level; + } + else + binding = new_class_binding (id, decl, /*type=*/NULL_TREE, level); - if (need_new_binding) - { - push_local_binding (name, x, 0); - /* Because push_local_binding will hook X on to the - current_binding_level's name list, we don't want to - do that again below. */ - need_new_binding = 0; - } + /* Now, fill in the binding information. */ + binding->previous = IDENTIFIER_BINDING (id); + INHERITED_VALUE_BINDING_P (binding) = 0; + LOCAL_BINDING_P (binding) = (level != class_binding_level); - /* If this is a TYPE_DECL, push it into the type value slot. */ - if (TREE_CODE (x) == TYPE_DECL) - set_identifier_type_value (name, x); - - /* Clear out any TYPE_DECL shadowed by a namespace so that - we won't think this is a type. The C struct hack doesn't - go through namespaces. */ - if (TREE_CODE (x) == NAMESPACE_DECL) - set_identifier_type_value (name, NULL_TREE); - - if (oldlocal) - { - tree d = oldlocal; - - while (oldlocal - && VAR_P (oldlocal) - && DECL_DEAD_FOR_LOCAL (oldlocal)) - oldlocal = DECL_SHADOWED_FOR_VAR (oldlocal); - - if (oldlocal == NULL_TREE) - oldlocal = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (d)); - } - - /* If this is an extern function declaration, see if we - have a global definition or declaration for the function. */ - if (oldlocal == NULL_TREE - && DECL_EXTERNAL (x) - && oldglobal != NULL_TREE - && TREE_CODE (x) == FUNCTION_DECL - && TREE_CODE (oldglobal) == FUNCTION_DECL) - { - /* We have one. Their types must agree. */ - if (decls_match (x, oldglobal)) - /* OK */; - else - { - warning (0, "extern declaration of %q#D doesn%'t match", x); - warning_at (DECL_SOURCE_LOCATION (oldglobal), 0, - "global declaration %q#D", oldglobal); - } - } - /* If we have a local external declaration, - and no file-scope declaration has yet been seen, - then if we later have a file-scope decl it must not be static. */ - if (oldlocal == NULL_TREE - && oldglobal == NULL_TREE - && DECL_EXTERNAL (x) - && TREE_PUBLIC (x)) - TREE_PUBLIC (name) = 1; - - /* Don't complain about the parms we push and then pop - while tentatively parsing a function declarator. */ - if (TREE_CODE (x) == PARM_DECL && DECL_CONTEXT (x) == NULL_TREE) - /* Ignore. */; - - /* Warn if shadowing an argument at the top level of the body. */ - else if (oldlocal != NULL_TREE && !DECL_EXTERNAL (x) - /* Inline decls shadow nothing. */ - && !DECL_FROM_INLINE (x) - && (TREE_CODE (oldlocal) == PARM_DECL - || VAR_P (oldlocal) - /* If the old decl is a type decl, only warn if the - old decl is an explicit typedef or if both the old - and new decls are type decls. */ - || (TREE_CODE (oldlocal) == TYPE_DECL - && (!DECL_ARTIFICIAL (oldlocal) - || TREE_CODE (x) == TYPE_DECL))) - /* Don't check for internally generated vars unless - it's an implicit typedef (see create_implicit_typedef - in decl.c) or anonymous union variable. */ - && (!DECL_ARTIFICIAL (x) - || DECL_IMPLICIT_TYPEDEF_P (x) - || (VAR_P (x) && DECL_ANON_UNION_VAR_P (x)))) - { - bool nowarn = false; - - /* Don't complain if it's from an enclosing function. */ - if (DECL_CONTEXT (oldlocal) == current_function_decl - && TREE_CODE (x) != PARM_DECL - && TREE_CODE (oldlocal) == PARM_DECL) - { - /* Go to where the parms should be and see if we find - them there. */ - cp_binding_level *b = current_binding_level->level_chain; - - if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl)) - /* Skip the ctor/dtor cleanup level. */ - b = b->level_chain; - - /* ARM $8.3 */ - if (b->kind == sk_function_parms) - { - error ("declaration of %q#D shadows a parameter", x); - nowarn = true; - } - } - - /* The local structure or class can't use parameters of - the containing function anyway. */ - if (DECL_CONTEXT (oldlocal) != current_function_decl) - { - cp_binding_level *scope = current_binding_level; - tree context = DECL_CONTEXT (oldlocal); - for (; scope; scope = scope->level_chain) - { - if (scope->kind == sk_function_parms - && scope->this_entity == context) - break; - if (scope->kind == sk_class - && !LAMBDA_TYPE_P (scope->this_entity)) - { - nowarn = true; - break; - } - } - } - /* Error if redeclaring a local declared in a - init-statement or in the condition of an if or - switch statement when the new declaration is in the - outermost block of the controlled statement. - Redeclaring a variable from a for or while condition is - detected elsewhere. */ - else if (VAR_P (oldlocal) - && oldscope == current_binding_level->level_chain - && (oldscope->kind == sk_cond - || oldscope->kind == sk_for)) - { - error ("redeclaration of %q#D", x); - inform (DECL_SOURCE_LOCATION (oldlocal), - "%q#D previously declared here", oldlocal); - nowarn = true; - } - /* C++11: - 3.3.3/3: The name declared in an exception-declaration (...) - shall not be redeclared in the outermost block of the handler. - 3.3.3/2: A parameter name shall not be redeclared (...) in - the outermost block of any handler associated with a - function-try-block. - 3.4.1/15: The function parameter names shall not be redeclared - in the exception-declaration nor in the outermost block of a - handler for the function-try-block. */ - else if ((VAR_P (oldlocal) - && oldscope == current_binding_level->level_chain - && oldscope->kind == sk_catch) - || (TREE_CODE (oldlocal) == PARM_DECL - && (current_binding_level->kind == sk_catch - || (current_binding_level->level_chain->kind - == sk_catch)) - && in_function_try_handler)) - { - if (permerror (input_location, "redeclaration of %q#D", x)) - inform (DECL_SOURCE_LOCATION (oldlocal), - "%q#D previously declared here", oldlocal); - nowarn = true; - } - - if ((warn_shadow - || warn_shadow_local - || warn_shadow_compatible_local) - && !nowarn) - { - bool warned; - enum opt_code warning_code; - /* If '-Wshadow=compatible-local' is specified without other - -Wshadow= flags, we will warn only when the type of the - shadowing variable (i.e. x) can be converted to that of - the shadowed parameter (oldlocal). The reason why we only - check if x's type can be converted to oldlocal's type - (but not the other way around) is because when users - accidentally shadow a parameter, more than often they - would use the variable thinking (mistakenly) it's still - the parameter. It would be rare that users would use the - variable in the place that expects the parameter but - thinking it's a new decl. */ - if (warn_shadow) - warning_code = OPT_Wshadow; - else if (can_convert (TREE_TYPE (oldlocal), TREE_TYPE (x), - tf_none)) - warning_code = OPT_Wshadow_compatible_local; - else - warning_code = OPT_Wshadow_local; - - if (TREE_CODE (oldlocal) == PARM_DECL) - warned = warning_at (input_location, warning_code, - "declaration of %q#D shadows a parameter", x); - else if (is_capture_proxy (oldlocal)) - warned = warning_at (input_location, warning_code, - "declaration of %qD shadows a lambda capture", - x); - else - warned = warning_at (input_location, warning_code, - "declaration of %qD shadows a previous local", - x); - - if (warned) - inform (DECL_SOURCE_LOCATION (oldlocal), - "shadowed declaration is here"); - } - } - - /* Maybe warn if shadowing something else. */ - else if (warn_shadow && !DECL_EXTERNAL (x) - /* No shadow warnings for internally generated vars unless - it's an implicit typedef (see create_implicit_typedef - in decl.c). */ - && (! DECL_ARTIFICIAL (x) || DECL_IMPLICIT_TYPEDEF_P (x)) - /* No shadow warnings for vars made for inlining. */ - && ! DECL_FROM_INLINE (x)) - { - tree member; - - if (nonlambda_method_basetype ()) - member = lookup_member (current_nonlambda_class_type (), - name, - /*protect=*/0, - /*want_type=*/false, - tf_warning_or_error); - else - member = NULL_TREE; - - if (member && !TREE_STATIC (member)) - { - if (BASELINK_P (member)) - member = BASELINK_FUNCTIONS (member); - member = OVL_CURRENT (member); - - /* Do not warn if a variable shadows a function, unless - the variable is a function or a pointer-to-function. */ - if (TREE_CODE (member) != FUNCTION_DECL - || TREE_CODE (x) == FUNCTION_DECL - || TYPE_PTRFN_P (TREE_TYPE (x)) - || TYPE_PTRMEMFUNC_P (TREE_TYPE (x))) - { - if (warning_at (input_location, OPT_Wshadow, - "declaration of %qD shadows a member of %qT", - x, current_nonlambda_class_type ()) - && DECL_P (member)) - inform (DECL_SOURCE_LOCATION (member), - "shadowed declaration is here"); - } - } - else if (oldglobal != NULL_TREE - && (VAR_P (oldglobal) - /* If the old decl is a type decl, only warn if the - old decl is an explicit typedef or if both the - old and new decls are type decls. */ - || (TREE_CODE (oldglobal) == TYPE_DECL - && (!DECL_ARTIFICIAL (oldglobal) - || TREE_CODE (x) == TYPE_DECL))) - && !instantiating_current_function_p ()) - /* XXX shadow warnings in outer-more namespaces */ - { - if (warning_at (input_location, OPT_Wshadow, - "declaration of %qD shadows a " - "global declaration", x)) - inform (DECL_SOURCE_LOCATION (oldglobal), - "shadowed declaration is here"); - } - } - } - - if (VAR_P (x)) - maybe_register_incomplete_var (x); - } - - if (need_new_binding) - add_decl_to_level (x, - DECL_NAMESPACE_SCOPE_P (x) - ? NAMESPACE_LEVEL (CP_DECL_CONTEXT (x)) - : current_binding_level); - - return x; + /* And put it on the front of the list of bindings for ID. */ + IDENTIFIER_BINDING (id) = binding; } -/* Wrapper for pushdecl_maybe_friend_1. */ +/* Remove the binding for DECL which should be the innermost binding + for ID. */ -tree -pushdecl_maybe_friend (tree x, bool is_friend) +void +pop_binding (tree id, tree decl) { - tree ret; - bool subtime = timevar_cond_start (TV_NAME_LOOKUP); - ret = pushdecl_maybe_friend_1 (x, is_friend); - timevar_cond_stop (TV_NAME_LOOKUP, subtime); - return ret; -} - -/* Record a decl-node X as belonging to the current lexical scope. */ + cxx_binding *binding; -tree -pushdecl (tree x) -{ - return pushdecl_maybe_friend (x, false); -} + if (id == NULL_TREE) + /* It's easiest to write the loops that call this function without + checking whether or not the entities involved have names. We + get here for such an entity. */ + return; -/* Enter DECL into the symbol table, if that's appropriate. Returns - DECL, or a modified version thereof. */ + /* Get the innermost binding for ID. */ + binding = IDENTIFIER_BINDING (id); -tree -maybe_push_decl (tree decl) -{ - tree type = TREE_TYPE (decl); + /* The name should be bound. */ + gcc_assert (binding != NULL); - /* Add this decl to the current binding level, but not if it comes - from another scope, e.g. a static member variable. TEM may equal - DECL or it may be a previous decl of the same name. */ - if (decl == error_mark_node - || (TREE_CODE (decl) != PARM_DECL - && DECL_CONTEXT (decl) != NULL_TREE - /* Definitions of namespace members outside their namespace are - possible. */ - && !DECL_NAMESPACE_SCOPE_P (decl)) - || (TREE_CODE (decl) == TEMPLATE_DECL && !namespace_bindings_p ()) - || type == unknown_type_node - /* The declaration of a template specialization does not affect - the functions available for overload resolution, so we do not - call pushdecl. */ - || (TREE_CODE (decl) == FUNCTION_DECL - && DECL_TEMPLATE_SPECIALIZATION (decl))) - return decl; + /* The DECL will be either the ordinary binding or the type + binding for this identifier. Remove that binding. */ + if (binding->value == decl) + binding->value = NULL_TREE; else - return pushdecl (decl); -} - -/* Bind DECL to ID in the current_binding_level, assumed to be a local - binding level. If PUSH_USING is set in FLAGS, we know that DECL - doesn't really belong to this binding level, that it got here - through a using-declaration. */ - -void -push_local_binding (tree id, tree decl, int flags) -{ - cp_binding_level *b; - - /* Skip over any local classes. This makes sense if we call - push_local_binding with a friend decl of a local class. */ - b = innermost_nonclass_level (); + { + gcc_assert (binding->type == decl); + binding->type = NULL_TREE; + } - if (lookup_name_innermost_nonclass_level (id)) + if (!binding->value && !binding->type) { - /* Supplement the existing binding. */ - if (!supplement_binding (IDENTIFIER_BINDING (id), decl)) - /* It didn't work. Something else must be bound at this - level. Do not add DECL to the list of things to pop - later. */ - return; + /* We're completely done with the innermost binding for this + identifier. Unhook it from the list of bindings. */ + IDENTIFIER_BINDING (id) = binding->previous; + + /* Add it to the free list. */ + cxx_binding_free (binding); } - else - /* Create a new binding. */ - push_binding (id, decl, b); +} - if (TREE_CODE (decl) == OVERLOAD || (flags & PUSH_USING)) - /* We must put the OVERLOAD into a TREE_LIST since the - TREE_CHAIN of an OVERLOAD is already used. Similarly for - decls that got here through a using-declaration. */ - decl = build_tree_list (NULL_TREE, decl); +/* Remove the bindings for the decls of the current level and leave + the current scope. */ - /* And put DECL on the list of things declared by the current - binding level. */ - add_decl_to_level (decl, b); +void +pop_bindings_and_leave_scope (void) +{ + for (tree t = getdecls (); t; t = DECL_CHAIN (t)) + pop_binding (DECL_NAME (t), t); + leave_scope (); } -/* Check to see whether or not DECL is a variable that would have been - in scope under the ARM, but is not in scope under the ANSI/ISO - standard. If so, issue an error message. If name lookup would - work in both cases, but return a different result, this function - returns the result of ANSI/ISO lookup. Otherwise, it returns - DECL. */ +/* Strip non dependent using declarations. If DECL is dependent, + surreptitiously create a typename_type and return it. */ tree -check_for_out_of_scope_variable (tree decl) +strip_using_decl (tree decl) { - tree shadowed; + if (decl == NULL_TREE) + return NULL_TREE; - /* We only care about out of scope variables. */ - if (!(VAR_P (decl) && DECL_DEAD_FOR_LOCAL (decl))) - return decl; + while (TREE_CODE (decl) == USING_DECL && !DECL_DEPENDENT_P (decl)) + decl = USING_DECL_DECLS (decl); - shadowed = DECL_HAS_SHADOWED_FOR_VAR_P (decl) - ? DECL_SHADOWED_FOR_VAR (decl) : NULL_TREE ; - while (shadowed != NULL_TREE && VAR_P (shadowed) - && DECL_DEAD_FOR_LOCAL (shadowed)) - shadowed = DECL_HAS_SHADOWED_FOR_VAR_P (shadowed) - ? DECL_SHADOWED_FOR_VAR (shadowed) : NULL_TREE; - if (!shadowed) - shadowed = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (decl)); - if (shadowed) + if (TREE_CODE (decl) == USING_DECL && DECL_DEPENDENT_P (decl) + && USING_DECL_TYPENAME_P (decl)) { - if (!DECL_ERROR_REPORTED (decl)) - { - warning (0, "name lookup of %qD changed", DECL_NAME (decl)); - warning_at (DECL_SOURCE_LOCATION (shadowed), 0, - " matches this %qD under ISO standard rules", - shadowed); - warning_at (DECL_SOURCE_LOCATION (decl), 0, - " matches this %qD under old rules", decl); - DECL_ERROR_REPORTED (decl) = 1; - } - return shadowed; + /* We have found a type introduced by a using + declaration at class scope that refers to a dependent + type. + + using typename :: [opt] nested-name-specifier unqualified-id ; + */ + decl = make_typename_type (TREE_TYPE (decl), + DECL_NAME (decl), + typename_type, tf_error); + if (decl != error_mark_node) + decl = TYPE_NAME (decl); } - /* If we have already complained about this declaration, there's no - need to do it again. */ - if (DECL_ERROR_REPORTED (decl)) - return decl; + return decl; +} - DECL_ERROR_REPORTED (decl) = 1; +/* BINDING records an existing declaration for a name in the current scope. + But, DECL is another declaration for that same identifier in the + same scope. This is the `struct stat' hack whereby a non-typedef + class name or enum-name can be bound at the same level as some other + kind of entity. + 3.3.7/1 - if (TREE_TYPE (decl) == error_mark_node) - return decl; + A class name (9.1) or enumeration name (7.2) can be hidden by the + name of an object, function, or enumerator declared in the same scope. + If a class or enumeration name and an object, function, or enumerator + are declared in the same scope (in any order) with the same name, the + class or enumeration name is hidden wherever the object, function, or + enumerator name is visible. - if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl))) - { - error ("name lookup of %qD changed for ISO % scoping", - DECL_NAME (decl)); - error (" cannot use obsolete binding at %q+D because " - "it has a destructor", decl); - return error_mark_node; - } - else + It's the responsibility of the caller to check that + inserting this name is valid here. Returns nonzero if the new binding + was successful. */ + +static bool +supplement_binding_1 (cxx_binding *binding, tree decl) +{ + tree bval = binding->value; + bool ok = true; + tree target_bval = strip_using_decl (bval); + tree target_decl = strip_using_decl (decl); + + if (TREE_CODE (target_decl) == TYPE_DECL && DECL_ARTIFICIAL (target_decl) + && target_decl != target_bval + && (TREE_CODE (target_bval) != TYPE_DECL + /* We allow pushing an enum multiple times in a class + template in order to handle late matching of underlying + type on an opaque-enum-declaration followed by an + enum-specifier. */ + || (processing_template_decl + && TREE_CODE (TREE_TYPE (target_decl)) == ENUMERAL_TYPE + && TREE_CODE (TREE_TYPE (target_bval)) == ENUMERAL_TYPE + && (dependent_type_p (ENUM_UNDERLYING_TYPE + (TREE_TYPE (target_decl))) + || dependent_type_p (ENUM_UNDERLYING_TYPE + (TREE_TYPE (target_bval))))))) + /* The new name is the type name. */ + binding->type = decl; + else if (/* TARGET_BVAL is null when push_class_level_binding moves + an inherited type-binding out of the way to make room + for a new value binding. */ + !target_bval + /* TARGET_BVAL is error_mark_node when TARGET_DECL's name + has been used in a non-class scope prior declaration. + In that case, we should have already issued a + diagnostic; for graceful error recovery purpose, pretend + this was the intended declaration for that name. */ + || target_bval == error_mark_node + /* If TARGET_BVAL is anticipated but has not yet been + declared, pretend it is not there at all. */ + || (TREE_CODE (target_bval) == FUNCTION_DECL + && DECL_ANTICIPATED (target_bval) + && !DECL_HIDDEN_FRIEND_P (target_bval))) + binding->value = decl; + else if (TREE_CODE (target_bval) == TYPE_DECL + && DECL_ARTIFICIAL (target_bval) + && target_decl != target_bval + && (TREE_CODE (target_decl) != TYPE_DECL + || same_type_p (TREE_TYPE (target_decl), + TREE_TYPE (target_bval)))) { - permerror (input_location, "name lookup of %qD changed for ISO % scoping", - DECL_NAME (decl)); - if (flag_permissive) - permerror (DECL_SOURCE_LOCATION (decl), - " using obsolete binding at %qD", decl); - else - { - static bool hint; - if (!hint) - { - inform (input_location, "(if you use %<-fpermissive%> G++ will accept your code)"); - hint = true; - } - } + /* The old binding was a type name. It was placed in + VALUE field because it was thought, at the point it was + declared, to be the only entity with such a name. Move the + type name into the type slot; it is now hidden by the new + binding. */ + binding->type = bval; + binding->value = decl; + binding->value_is_inherited = false; } + else if (TREE_CODE (target_bval) == TYPE_DECL + && TREE_CODE (target_decl) == TYPE_DECL + && DECL_NAME (target_decl) == DECL_NAME (target_bval) + && binding->scope->kind != sk_class + && (same_type_p (TREE_TYPE (target_decl), TREE_TYPE (target_bval)) + /* If either type involves template parameters, we must + wait until instantiation. */ + || uses_template_parms (TREE_TYPE (target_decl)) + || uses_template_parms (TREE_TYPE (target_bval)))) + /* We have two typedef-names, both naming the same type to have + the same name. In general, this is OK because of: - return decl; -} - -/* true means unconditionally make a BLOCK for the next level pushed. */ + [dcl.typedef] -static bool keep_next_level_flag; + In a given scope, a typedef specifier can be used to redefine + the name of any type declared in that scope to refer to the + type to which it already refers. -static int binding_depth = 0; + However, in class scopes, this rule does not apply due to the + stricter language in [class.mem] prohibiting redeclarations of + members. */ + ok = false; + /* There can be two block-scope declarations of the same variable, + so long as they are `extern' declarations. However, there cannot + be two declarations of the same static data member: -static void -indent (int depth) -{ - int i; + [class.mem] - for (i = 0; i < depth * 2; i++) - putc (' ', stderr); -} + A member shall not be declared twice in the + member-specification. */ + else if (VAR_P (target_decl) + && VAR_P (target_bval) + && DECL_EXTERNAL (target_decl) && DECL_EXTERNAL (target_bval) + && !DECL_CLASS_SCOPE_P (target_decl)) + { + duplicate_decls (decl, binding->value, /*newdecl_is_friend=*/false); + ok = false; + } + else if (TREE_CODE (decl) == NAMESPACE_DECL + && TREE_CODE (bval) == NAMESPACE_DECL + && DECL_NAMESPACE_ALIAS (decl) + && DECL_NAMESPACE_ALIAS (bval) + && ORIGINAL_NAMESPACE (bval) == ORIGINAL_NAMESPACE (decl)) + /* [namespace.alias] -/* Return a string describing the kind of SCOPE we have. */ -static const char * -cp_binding_level_descriptor (cp_binding_level *scope) -{ - /* The order of this table must match the "scope_kind" - enumerators. */ - static const char* scope_kind_names[] = { - "block-scope", - "cleanup-scope", - "try-scope", - "catch-scope", - "for-scope", - "function-parameter-scope", - "class-scope", - "namespace-scope", - "template-parameter-scope", - "template-explicit-spec-scope" - }; - const scope_kind kind = scope->explicit_spec_p - ? sk_template_spec : scope->kind; + In a declarative region, a namespace-alias-definition can be + used to redefine a namespace-alias declared in that declarative + region to refer only to the namespace to which it already + refers. */ + ok = false; + else if (maybe_remove_implicit_alias (bval)) + { + /* There was a mangling compatibility alias using this mangled name, + but now we have a real decl that wants to use it instead. */ + binding->value = decl; + } + else + { + if (!error_operand_p (bval)) + diagnose_name_conflict (decl, bval); + ok = false; + } - return scope_kind_names[kind]; + return ok; } -/* Output a debugging information about SCOPE when performing - ACTION at LINE. */ +/* Diagnose a name conflict between DECL and BVAL. */ + static void -cp_binding_level_debug (cp_binding_level *scope, int line, const char *action) -{ - const char *desc = cp_binding_level_descriptor (scope); - if (scope->this_entity) - verbatim ("%s %s(%E) %p %d\n", action, desc, - scope->this_entity, (void *) scope, line); +diagnose_name_conflict (tree decl, tree bval) +{ + if (TREE_CODE (decl) == TREE_CODE (bval) + && (TREE_CODE (decl) != TYPE_DECL + || (DECL_ARTIFICIAL (decl) && DECL_ARTIFICIAL (bval)) + || (!DECL_ARTIFICIAL (decl) && !DECL_ARTIFICIAL (bval))) + && !is_overloaded_fn (decl)) + error ("redeclaration of %q#D", decl); else - verbatim ("%s %s %p %d\n", action, desc, (void *) scope, line); + error ("%q#D conflicts with a previous declaration", decl); + + inform (location_of (bval), "previous declaration %q#D", bval); } -/* Return the estimated initial size of the hashtable of a NAMESPACE - scope. */ +/* Wrapper for supplement_binding_1. */ -static inline size_t -namespace_scope_ht_size (tree ns) +static bool +supplement_binding (cxx_binding *binding, tree decl) { - tree name = DECL_NAME (ns); - - return name == std_identifier - ? NAMESPACE_STD_HT_SIZE - : (name == global_scope_name - ? GLOBAL_SCOPE_HT_SIZE - : NAMESPACE_ORDINARY_HT_SIZE); + bool ret; + bool subtime = timevar_cond_start (TV_NAME_LOOKUP); + ret = supplement_binding_1 (binding, decl); + timevar_cond_stop (TV_NAME_LOOKUP, subtime); + return ret; } -/* A chain of binding_level structures awaiting reuse. */ - -static GTY((deletable)) cp_binding_level *free_binding_level; +/* Record a decl-node X as belonging to the current lexical scope. + Check for errors (such as an incompatible declaration for the same + name already seen in the same scope). IS_FRIEND is true if X is + declared as a friend. -/* Insert SCOPE as the innermost binding level. */ + Returns either X or an old decl for the same name. + If an old decl is returned, it may have been smashed + to agree with what X says. */ -void -push_binding_level (cp_binding_level *scope) +static tree +pushdecl_maybe_friend_1 (tree x, bool is_friend) { - /* Add it to the front of currently active scopes stack. */ - scope->level_chain = current_binding_level; - current_binding_level = scope; - keep_next_level_flag = false; - - if (ENABLE_SCOPE_CHECKING) - { - scope->binding_depth = binding_depth; - indent (binding_depth); - cp_binding_level_debug (scope, LOCATION_LINE (input_location), - "push"); - binding_depth++; - } -} + tree t; + tree name; + int need_new_binding; -/* Create a new KIND scope and make it the top of the active scopes stack. - ENTITY is the scope of the associated C++ entity (namespace, class, - function, C++0x enumeration); it is NULL otherwise. */ + if (x == error_mark_node) + return error_mark_node; -cp_binding_level * -begin_scope (scope_kind kind, tree entity) -{ - cp_binding_level *scope; + need_new_binding = 1; - /* Reuse or create a struct for this binding level. */ - if (!ENABLE_SCOPE_CHECKING && free_binding_level) + if (DECL_TEMPLATE_PARM_P (x)) + /* Template parameters have no context; they are not X::T even + when declared within a class or namespace. */ + ; + else { - scope = free_binding_level; - free_binding_level = scope->level_chain; - memset (scope, 0, sizeof (cp_binding_level)); + if (current_function_decl && x != current_function_decl + /* A local declaration for a function doesn't constitute + nesting. */ + && TREE_CODE (x) != FUNCTION_DECL + /* A local declaration for an `extern' variable is in the + scope of the current namespace, not the current + function. */ + && !(VAR_P (x) && DECL_EXTERNAL (x)) + /* When parsing the parameter list of a function declarator, + don't set DECL_CONTEXT to an enclosing function. When we + push the PARM_DECLs in order to process the function body, + current_binding_level->this_entity will be set. */ + && !(TREE_CODE (x) == PARM_DECL + && current_binding_level->kind == sk_function_parms + && current_binding_level->this_entity == NULL) + && !DECL_CONTEXT (x)) + DECL_CONTEXT (x) = current_function_decl; + + /* If this is the declaration for a namespace-scope function, + but the declaration itself is in a local scope, mark the + declaration. */ + if (TREE_CODE (x) == FUNCTION_DECL + && DECL_NAMESPACE_SCOPE_P (x) + && current_function_decl + && x != current_function_decl) + DECL_LOCAL_FUNCTION_P (x) = 1; } - else - scope = ggc_cleared_alloc (); - scope->this_entity = entity; - scope->more_cleanups_ok = true; - switch (kind) + name = DECL_NAME (x); + if (name) { - case sk_cleanup: - scope->keep = true; - break; + int different_binding_level = 0; - case sk_template_spec: - scope->explicit_spec_p = true; - kind = sk_template_parms; - /* Fall through. */ - case sk_template_parms: - case sk_block: - case sk_try: - case sk_catch: - case sk_for: - case sk_cond: - case sk_class: - case sk_scoped_enum: - case sk_function_parms: - case sk_transaction: - case sk_omp: - scope->keep = keep_next_level_flag; - break; + if (TREE_CODE (name) == TEMPLATE_ID_EXPR) + name = TREE_OPERAND (name, 0); - case sk_namespace: - NAMESPACE_LEVEL (entity) = scope; - vec_alloc (scope->static_decls, - (DECL_NAME (entity) == std_identifier - || DECL_NAME (entity) == global_scope_name) ? 200 : 10); - break; + /* In case this decl was explicitly namespace-qualified, look it + up in its namespace context. */ + if (DECL_NAMESPACE_SCOPE_P (x) && namespace_bindings_p ()) + t = namespace_binding (name, DECL_CONTEXT (x)); + else + t = lookup_name_innermost_nonclass_level (name); - default: - /* Should not happen. */ - gcc_unreachable (); - break; - } - scope->kind = kind; + /* [basic.link] If there is a visible declaration of an entity + with linkage having the same name and type, ignoring entities + declared outside the innermost enclosing namespace scope, the + block scope declaration declares that same entity and + receives the linkage of the previous declaration. */ + if (! t && current_function_decl && x != current_function_decl + && VAR_OR_FUNCTION_DECL_P (x) + && DECL_EXTERNAL (x)) + { + /* Look in block scope. */ + t = innermost_non_namespace_value (name); + /* Or in the innermost namespace. */ + if (! t) + t = namespace_binding (name, DECL_CONTEXT (x)); + /* Does it have linkage? Note that if this isn't a DECL, it's an + OVERLOAD, which is OK. */ + if (t && DECL_P (t) && ! (TREE_STATIC (t) || DECL_EXTERNAL (t))) + t = NULL_TREE; + if (t) + different_binding_level = 1; + } - push_binding_level (scope); + /* If we are declaring a function, and the result of name-lookup + was an OVERLOAD, look for an overloaded instance that is + actually the same as the function we are declaring. (If + there is one, we have to merge our declaration with the + previous declaration.) */ + if (t && TREE_CODE (t) == OVERLOAD) + { + tree match; - return scope; -} + if (TREE_CODE (x) == FUNCTION_DECL) + for (match = t; match; match = OVL_NEXT (match)) + { + if (decls_match (OVL_CURRENT (match), x)) + break; + } + else + /* Just choose one. */ + match = t; + + if (match) + t = OVL_CURRENT (match); + else + t = NULL_TREE; + } + + if (t && t != error_mark_node) + { + if (different_binding_level) + { + if (decls_match (x, t)) + /* The standard only says that the local extern + inherits linkage from the previous decl; in + particular, default args are not shared. Add + the decl into a hash table to make sure only + the previous decl in this case is seen by the + middle end. */ + { + struct cxx_int_tree_map *h; + + TREE_PUBLIC (x) = TREE_PUBLIC (t); + + if (cp_function_chain->extern_decl_map == NULL) + cp_function_chain->extern_decl_map + = hash_table::create_ggc (20); + + h = ggc_alloc (); + h->uid = DECL_UID (x); + h->to = t; + cxx_int_tree_map **loc = cp_function_chain->extern_decl_map + ->find_slot (h, INSERT); + *loc = h; + } + } + else if (TREE_CODE (t) == PARM_DECL) + { + /* Check for duplicate params. */ + tree d = duplicate_decls (x, t, is_friend); + if (d) + return d; + } + else if ((DECL_EXTERN_C_FUNCTION_P (x) + || DECL_FUNCTION_TEMPLATE_P (x)) + && is_overloaded_fn (t)) + /* Don't do anything just yet. */; + else if (t == wchar_decl_node) + { + if (! DECL_IN_SYSTEM_HEADER (x)) + pedwarn (input_location, OPT_Wpedantic, "redeclaration of % as %qT", + TREE_TYPE (x)); + + /* Throw away the redeclaration. */ + return t; + } + else + { + tree olddecl = duplicate_decls (x, t, is_friend); + + /* If the redeclaration failed, we can stop at this + point. */ + if (olddecl == error_mark_node) + return error_mark_node; -/* We're about to leave current scope. Pop the top of the stack of - currently active scopes. Return the enclosing scope, now active. */ + if (olddecl) + { + if (TREE_CODE (t) == TYPE_DECL) + SET_IDENTIFIER_TYPE_VALUE (name, TREE_TYPE (t)); -cp_binding_level * -leave_scope (void) -{ - cp_binding_level *scope = current_binding_level; + return t; + } + else if (DECL_MAIN_P (x) && TREE_CODE (t) == FUNCTION_DECL) + { + /* A redeclaration of main, but not a duplicate of the + previous one. - if (scope->kind == sk_namespace && class_binding_level) - current_binding_level = class_binding_level; + [basic.start.main] - /* We cannot leave a scope, if there are none left. */ - if (NAMESPACE_LEVEL (global_namespace)) - gcc_assert (!global_scope_p (scope)); + This function shall not be overloaded. */ + error ("invalid redeclaration of %q+D", t); + error ("as %qD", x); + /* We don't try to push this declaration since that + causes a crash. */ + return x; + } + } + } - if (ENABLE_SCOPE_CHECKING) - { - indent (--binding_depth); - cp_binding_level_debug (scope, LOCATION_LINE (input_location), - "leave"); - } + /* If x has C linkage-specification, (extern "C"), + lookup its binding, in case it's already bound to an object. + The lookup is done in all namespaces. + If we find an existing binding, make sure it has the same + exception specification as x, otherwise, bail in error [7.5, 7.6]. */ + if ((TREE_CODE (x) == FUNCTION_DECL) + && DECL_EXTERN_C_P (x) + /* We should ignore declarations happening in system headers. */ + && !DECL_ARTIFICIAL (x) + && !DECL_IN_SYSTEM_HEADER (x)) + { + tree previous = lookup_extern_c_fun_in_all_ns (x); + if (previous + && !DECL_ARTIFICIAL (previous) + && !DECL_IN_SYSTEM_HEADER (previous) + && DECL_CONTEXT (previous) != DECL_CONTEXT (x)) + { + /* In case either x or previous is declared to throw an exception, + make sure both exception specifications are equal. */ + if (decls_match (x, previous)) + { + tree x_exception_spec = NULL_TREE; + tree previous_exception_spec = NULL_TREE; - /* Move one nesting level up. */ - current_binding_level = scope->level_chain; + x_exception_spec = + TYPE_RAISES_EXCEPTIONS (TREE_TYPE (x)); + previous_exception_spec = + TYPE_RAISES_EXCEPTIONS (TREE_TYPE (previous)); + if (!comp_except_specs (previous_exception_spec, + x_exception_spec, + ce_normal)) + { + pedwarn (input_location, 0, + "declaration of %q#D with C language linkage", + x); + pedwarn (DECL_SOURCE_LOCATION (previous), 0, + "conflicts with previous declaration %q#D", + previous); + pedwarn (input_location, 0, + "due to different exception specifications"); + return error_mark_node; + } + if (DECL_ASSEMBLER_NAME_SET_P (previous)) + SET_DECL_ASSEMBLER_NAME (x, + DECL_ASSEMBLER_NAME (previous)); + } + else + { + pedwarn (input_location, 0, + "declaration of %q#D with C language linkage", x); + pedwarn (DECL_SOURCE_LOCATION (previous), 0, + "conflicts with previous declaration %q#D", + previous); + } + } + } - /* Namespace-scopes are left most probably temporarily, not - completely; they can be reopened later, e.g. in namespace-extension - or any name binding activity that requires us to resume a - namespace. For classes, we cache some binding levels. For other - scopes, we just make the structure available for reuse. */ - if (scope->kind != sk_namespace - && scope->kind != sk_class) - { - scope->level_chain = free_binding_level; - gcc_assert (!ENABLE_SCOPE_CHECKING - || scope->binding_depth == binding_depth); - free_binding_level = scope; - } + check_template_shadow (x); - if (scope->kind == sk_class) - { - /* Reset DEFINING_CLASS_P to allow for reuse of a - class-defining scope in a non-defining context. */ - scope->defining_class_p = 0; + /* If this is a function conjured up by the back end, massage it + so it looks friendly. */ + if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_LANG_SPECIFIC (x)) + { + retrofit_lang_decl (x); + SET_DECL_LANGUAGE (x, lang_c); + } - /* Find the innermost enclosing class scope, and reset - CLASS_BINDING_LEVEL appropriately. */ - class_binding_level = NULL; - for (scope = current_binding_level; scope; scope = scope->level_chain) - if (scope->kind == sk_class) - { - class_binding_level = scope; - break; - } - } + t = x; + if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_FUNCTION_MEMBER_P (x)) + { + t = push_overloaded_decl (x, PUSH_LOCAL, is_friend); + if (!namespace_bindings_p ()) + /* We do not need to create a binding for this name; + push_overloaded_decl will have already done so if + necessary. */ + need_new_binding = 0; + } + else if (DECL_FUNCTION_TEMPLATE_P (x) && DECL_NAMESPACE_SCOPE_P (x)) + { + t = push_overloaded_decl (x, PUSH_GLOBAL, is_friend); + if (t == x) + add_decl_to_level (x, NAMESPACE_LEVEL (CP_DECL_CONTEXT (t))); + } - return current_binding_level; -} + if (DECL_DECLARES_FUNCTION_P (t)) + { + check_default_args (t); -static void -resume_scope (cp_binding_level* b) -{ - /* Resuming binding levels is meant only for namespaces, - and those cannot nest into classes. */ - gcc_assert (!class_binding_level); - /* Also, resuming a non-directly nested namespace is a no-no. */ - gcc_assert (b->level_chain == current_binding_level); - current_binding_level = b; - if (ENABLE_SCOPE_CHECKING) - { - b->binding_depth = binding_depth; - indent (binding_depth); - cp_binding_level_debug (b, LOCATION_LINE (input_location), "resume"); - binding_depth++; - } -} + if (is_friend && t == x && !flag_friend_injection) + { + /* This is a new friend declaration of a function or a + function template, so hide it from ordinary function + lookup. */ + DECL_ANTICIPATED (t) = 1; + DECL_HIDDEN_FRIEND_P (t) = 1; + } + } -/* Return the innermost binding level that is not for a class scope. */ + if (t != x || DECL_FUNCTION_TEMPLATE_P (t)) + return t; -static cp_binding_level * -innermost_nonclass_level (void) -{ - cp_binding_level *b; + /* If declaring a type as a typedef, copy the type (unless we're + at line 0), and install this TYPE_DECL as the new type's typedef + name. See the extensive comment of set_underlying_type (). */ + if (TREE_CODE (x) == TYPE_DECL) + { + tree type = TREE_TYPE (x); - b = current_binding_level; - while (b->kind == sk_class) - b = b->level_chain; + if (DECL_IS_BUILTIN (x) + || (TREE_TYPE (x) != error_mark_node + && TYPE_NAME (type) != x + /* We don't want to copy the type when all we're + doing is making a TYPE_DECL for the purposes of + inlining. */ + && (!TYPE_NAME (type) + || TYPE_NAME (type) != DECL_ABSTRACT_ORIGIN (x)))) + set_underlying_type (x); - return b; -} + if (type != error_mark_node + && TYPE_IDENTIFIER (type)) + set_identifier_type_value (DECL_NAME (x), x); -/* We're defining an object of type TYPE. If it needs a cleanup, but - we're not allowed to add any more objects with cleanups to the current - scope, create a new binding level. */ + /* If this is a locally defined typedef in a function that + is not a template instantation, record it to implement + -Wunused-local-typedefs. */ + if (!instantiating_current_function_p ()) + record_locally_defined_typedef (x); + } -void -maybe_push_cleanup_level (tree type) -{ - if (type != error_mark_node - && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type) - && current_binding_level->more_cleanups_ok == 0) - { - begin_scope (sk_cleanup, NULL); - current_binding_level->statement_list = push_stmt_list (); - } -} + /* Multiple external decls of the same identifier ought to match. -/* Return true if we are in the global binding level. */ + We get warnings about inline functions where they are defined. + We get warnings about other functions from push_overloaded_decl. -bool -global_bindings_p (void) -{ - return global_scope_p (current_binding_level); -} + Avoid duplicate warnings where they are used. */ + if (TREE_PUBLIC (x) && TREE_CODE (x) != FUNCTION_DECL) + { + tree decl; -/* True if we are currently in a toplevel binding level. This - means either the global binding level or a namespace in a toplevel - binding level. Since there are no non-toplevel namespace levels, - this really means any namespace or template parameter level. We - also include a class whose context is toplevel. */ + decl = IDENTIFIER_NAMESPACE_VALUE (name); + if (decl && TREE_CODE (decl) == OVERLOAD) + decl = OVL_FUNCTION (decl); -bool -toplevel_bindings_p (void) -{ - cp_binding_level *b = innermost_nonclass_level (); + if (decl && decl != error_mark_node + && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl)) + /* If different sort of thing, we already gave an error. */ + && TREE_CODE (decl) == TREE_CODE (x) + && !comptypes (TREE_TYPE (x), TREE_TYPE (decl), + COMPARE_REDECLARATION)) + { + if (permerror (input_location, "type mismatch with previous " + "external decl of %q#D", x)) + inform (DECL_SOURCE_LOCATION (decl), + "previous external decl of %q#D", decl); + } + } - return b->kind == sk_namespace || b->kind == sk_template_parms; -} + /* This name is new in its binding level. + Install the new declaration and return it. */ + if (namespace_bindings_p ()) + { + /* Install a global value. */ -/* True if this is a namespace scope, or if we are defining a class - which is itself at namespace scope, or whose enclosing class is - such a class, etc. */ + /* If the first global decl has external linkage, + warn if we later see static one. */ + if (IDENTIFIER_GLOBAL_VALUE (name) == NULL_TREE && TREE_PUBLIC (x)) + TREE_PUBLIC (name) = 1; -bool -namespace_bindings_p (void) -{ - cp_binding_level *b = innermost_nonclass_level (); + /* Bind the name for the entity. */ + if (!(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x) + && t != NULL_TREE) + && (TREE_CODE (x) == TYPE_DECL + || VAR_P (x) + || TREE_CODE (x) == NAMESPACE_DECL + || TREE_CODE (x) == CONST_DECL + || TREE_CODE (x) == TEMPLATE_DECL)) + SET_IDENTIFIER_NAMESPACE_VALUE (name, x); - return b->kind == sk_namespace; -} + /* If new decl is `static' and an `extern' was seen previously, + warn about it. */ + if (x != NULL_TREE && t != NULL_TREE && decls_match (x, t)) + warn_extern_redeclared_static (x, t); + } + else + { + /* Here to install a non-global value. */ + tree oldglobal = IDENTIFIER_NAMESPACE_VALUE (name); + tree oldlocal = NULL_TREE; + cp_binding_level *oldscope = NULL; + cxx_binding *oldbinding = outer_binding (name, NULL, true); + if (oldbinding) + { + oldlocal = oldbinding->value; + oldscope = oldbinding->scope; + } -/* True if the innermost non-class scope is a block scope. */ + if (need_new_binding) + { + push_local_binding (name, x, 0); + /* Because push_local_binding will hook X on to the + current_binding_level's name list, we don't want to + do that again below. */ + need_new_binding = 0; + } -bool -local_bindings_p (void) -{ - cp_binding_level *b = innermost_nonclass_level (); - return b->kind < sk_function_parms || b->kind == sk_omp; -} + /* If this is a TYPE_DECL, push it into the type value slot. */ + if (TREE_CODE (x) == TYPE_DECL) + set_identifier_type_value (name, x); -/* True if the current level needs to have a BLOCK made. */ + /* Clear out any TYPE_DECL shadowed by a namespace so that + we won't think this is a type. The C struct hack doesn't + go through namespaces. */ + if (TREE_CODE (x) == NAMESPACE_DECL) + set_identifier_type_value (name, NULL_TREE); -bool -kept_level_p (void) -{ - return (current_binding_level->blocks != NULL_TREE - || current_binding_level->keep - || current_binding_level->kind == sk_cleanup - || current_binding_level->names != NULL_TREE - || current_binding_level->using_directives); -} + if (oldlocal) + { + tree d = oldlocal; -/* Returns the kind of the innermost scope. */ + while (oldlocal + && VAR_P (oldlocal) + && DECL_DEAD_FOR_LOCAL (oldlocal)) + oldlocal = DECL_SHADOWED_FOR_VAR (oldlocal); -scope_kind -innermost_scope_kind (void) -{ - return current_binding_level->kind; -} + if (oldlocal == NULL_TREE) + oldlocal = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (d)); + } -/* Returns true if this scope was created to store template parameters. */ + /* If this is an extern function declaration, see if we + have a global definition or declaration for the function. */ + if (oldlocal == NULL_TREE + && DECL_EXTERNAL (x) + && oldglobal != NULL_TREE + && TREE_CODE (x) == FUNCTION_DECL + && TREE_CODE (oldglobal) == FUNCTION_DECL) + { + /* We have one. Their types must agree. */ + if (decls_match (x, oldglobal)) + /* OK */; + else + { + warning (0, "extern declaration of %q#D doesn%'t match", x); + warning_at (DECL_SOURCE_LOCATION (oldglobal), 0, + "global declaration %q#D", oldglobal); + } + } + /* If we have a local external declaration, + and no file-scope declaration has yet been seen, + then if we later have a file-scope decl it must not be static. */ + if (oldlocal == NULL_TREE + && oldglobal == NULL_TREE + && DECL_EXTERNAL (x) + && TREE_PUBLIC (x)) + TREE_PUBLIC (name) = 1; -bool -template_parm_scope_p (void) -{ - return innermost_scope_kind () == sk_template_parms; -} + /* Don't complain about the parms we push and then pop + while tentatively parsing a function declarator. */ + if (TREE_CODE (x) == PARM_DECL && DECL_CONTEXT (x) == NULL_TREE) + /* Ignore. */; -/* If KEEP is true, make a BLOCK node for the next binding level, - unconditionally. Otherwise, use the normal logic to decide whether - or not to create a BLOCK. */ + /* Warn if shadowing an argument at the top level of the body. */ + else if (oldlocal != NULL_TREE && !DECL_EXTERNAL (x) + /* Inline decls shadow nothing. */ + && !DECL_FROM_INLINE (x) + && (TREE_CODE (oldlocal) == PARM_DECL + || VAR_P (oldlocal) + /* If the old decl is a type decl, only warn if the + old decl is an explicit typedef or if both the old + and new decls are type decls. */ + || (TREE_CODE (oldlocal) == TYPE_DECL + && (!DECL_ARTIFICIAL (oldlocal) + || TREE_CODE (x) == TYPE_DECL))) + /* Don't check for internally generated vars unless + it's an implicit typedef (see create_implicit_typedef + in decl.c) or anonymous union variable. */ + && (!DECL_ARTIFICIAL (x) + || DECL_IMPLICIT_TYPEDEF_P (x) + || (VAR_P (x) && DECL_ANON_UNION_VAR_P (x)))) + { + bool nowarn = false; -void -keep_next_level (bool keep) -{ - keep_next_level_flag = keep; -} + /* Don't complain if it's from an enclosing function. */ + if (DECL_CONTEXT (oldlocal) == current_function_decl + && TREE_CODE (x) != PARM_DECL + && TREE_CODE (oldlocal) == PARM_DECL) + { + /* Go to where the parms should be and see if we find + them there. */ + cp_binding_level *b = current_binding_level->level_chain; -/* Return the list of declarations of the current level. - Note that this list is in reverse order unless/until - you nreverse it; and when you do nreverse it, you must - store the result back using `storedecls' or you will lose. */ + if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl)) + /* Skip the ctor/dtor cleanup level. */ + b = b->level_chain; -tree -getdecls (void) -{ - return current_binding_level->names; -} + /* ARM $8.3 */ + if (b->kind == sk_function_parms) + { + error ("declaration of %q#D shadows a parameter", x); + nowarn = true; + } + } -/* Return how many function prototypes we are currently nested inside. */ + /* The local structure or class can't use parameters of + the containing function anyway. */ + if (DECL_CONTEXT (oldlocal) != current_function_decl) + { + cp_binding_level *scope = current_binding_level; + tree context = DECL_CONTEXT (oldlocal); + for (; scope; scope = scope->level_chain) + { + if (scope->kind == sk_function_parms + && scope->this_entity == context) + break; + if (scope->kind == sk_class + && !LAMBDA_TYPE_P (scope->this_entity)) + { + nowarn = true; + break; + } + } + } + /* Error if redeclaring a local declared in a + init-statement or in the condition of an if or + switch statement when the new declaration is in the + outermost block of the controlled statement. + Redeclaring a variable from a for or while condition is + detected elsewhere. */ + else if (VAR_P (oldlocal) + && oldscope == current_binding_level->level_chain + && (oldscope->kind == sk_cond + || oldscope->kind == sk_for)) + { + error ("redeclaration of %q#D", x); + inform (DECL_SOURCE_LOCATION (oldlocal), + "%q#D previously declared here", oldlocal); + nowarn = true; + } + /* C++11: + 3.3.3/3: The name declared in an exception-declaration (...) + shall not be redeclared in the outermost block of the handler. + 3.3.3/2: A parameter name shall not be redeclared (...) in + the outermost block of any handler associated with a + function-try-block. + 3.4.1/15: The function parameter names shall not be redeclared + in the exception-declaration nor in the outermost block of a + handler for the function-try-block. */ + else if ((VAR_P (oldlocal) + && oldscope == current_binding_level->level_chain + && oldscope->kind == sk_catch) + || (TREE_CODE (oldlocal) == PARM_DECL + && (current_binding_level->kind == sk_catch + || (current_binding_level->level_chain->kind + == sk_catch)) + && in_function_try_handler)) + { + if (permerror (input_location, "redeclaration of %q#D", x)) + inform (DECL_SOURCE_LOCATION (oldlocal), + "%q#D previously declared here", oldlocal); + nowarn = true; + } -int -function_parm_depth (void) -{ - int level = 0; - cp_binding_level *b; + if ((warn_shadow + || warn_shadow_local + || warn_shadow_compatible_local) + && !nowarn) + { + bool warned; + enum opt_code warning_code; + /* If '-Wshadow=compatible-local' is specified without other + -Wshadow= flags, we will warn only when the type of the + shadowing variable (i.e. x) can be converted to that of + the shadowed parameter (oldlocal). The reason why we only + check if x's type can be converted to oldlocal's type + (but not the other way around) is because when users + accidentally shadow a parameter, more than often they + would use the variable thinking (mistakenly) it's still + the parameter. It would be rare that users would use the + variable in the place that expects the parameter but + thinking it's a new decl. */ + if (warn_shadow) + warning_code = OPT_Wshadow; + else if (can_convert (TREE_TYPE (oldlocal), TREE_TYPE (x), + tf_none)) + warning_code = OPT_Wshadow_compatible_local; + else + warning_code = OPT_Wshadow_local; - for (b = current_binding_level; - b->kind == sk_function_parms; - b = b->level_chain) - ++level; + if (TREE_CODE (oldlocal) == PARM_DECL) + warned = warning_at (input_location, warning_code, + "declaration of %q#D shadows a parameter", x); + else if (is_capture_proxy (oldlocal)) + warned = warning_at (input_location, warning_code, + "declaration of %qD shadows a lambda capture", + x); + else + warned = warning_at (input_location, warning_code, + "declaration of %qD shadows a previous local", + x); - return level; -} + if (warned) + inform (DECL_SOURCE_LOCATION (oldlocal), + "shadowed declaration is here"); + } + } -/* For debugging. */ -static int no_print_functions = 0; -static int no_print_builtins = 0; + /* Maybe warn if shadowing something else. */ + else if (warn_shadow && !DECL_EXTERNAL (x) + /* No shadow warnings for internally generated vars unless + it's an implicit typedef (see create_implicit_typedef + in decl.c). */ + && (! DECL_ARTIFICIAL (x) || DECL_IMPLICIT_TYPEDEF_P (x)) + /* No shadow warnings for vars made for inlining. */ + && ! DECL_FROM_INLINE (x)) + { + tree member; -static void -print_binding_level (cp_binding_level* lvl) -{ - tree t; - int i = 0, len; - fprintf (stderr, " blocks=%p", (void *) lvl->blocks); - if (lvl->more_cleanups_ok) - fprintf (stderr, " more-cleanups-ok"); - if (lvl->have_cleanups) - fprintf (stderr, " have-cleanups"); - fprintf (stderr, "\n"); - if (lvl->names) - { - fprintf (stderr, " names:\t"); - /* We can probably fit 3 names to a line? */ - for (t = lvl->names; t; t = TREE_CHAIN (t)) - { - if (no_print_functions && (TREE_CODE (t) == FUNCTION_DECL)) - continue; - if (no_print_builtins - && (TREE_CODE (t) == TYPE_DECL) - && DECL_IS_BUILTIN (t)) - continue; + if (nonlambda_method_basetype ()) + member = lookup_member (current_nonlambda_class_type (), + name, + /*protect=*/0, + /*want_type=*/false, + tf_warning_or_error); + else + member = NULL_TREE; - /* Function decls tend to have longer names. */ - if (TREE_CODE (t) == FUNCTION_DECL) - len = 3; - else - len = 2; - i += len; - if (i > 6) - { - fprintf (stderr, "\n\t"); - i = len; + if (member && !TREE_STATIC (member)) + { + if (BASELINK_P (member)) + member = BASELINK_FUNCTIONS (member); + member = OVL_CURRENT (member); + + /* Do not warn if a variable shadows a function, unless + the variable is a function or a pointer-to-function. */ + if (TREE_CODE (member) != FUNCTION_DECL + || TREE_CODE (x) == FUNCTION_DECL + || TYPE_PTRFN_P (TREE_TYPE (x)) + || TYPE_PTRMEMFUNC_P (TREE_TYPE (x))) + { + if (warning_at (input_location, OPT_Wshadow, + "declaration of %qD shadows a member of %qT", + x, current_nonlambda_class_type ()) + && DECL_P (member)) + inform (DECL_SOURCE_LOCATION (member), + "shadowed declaration is here"); + } + } + else if (oldglobal != NULL_TREE + && (VAR_P (oldglobal) + /* If the old decl is a type decl, only warn if the + old decl is an explicit typedef or if both the + old and new decls are type decls. */ + || (TREE_CODE (oldglobal) == TYPE_DECL + && (!DECL_ARTIFICIAL (oldglobal) + || TREE_CODE (x) == TYPE_DECL))) + && !instantiating_current_function_p ()) + /* XXX shadow warnings in outer-more namespaces */ + { + if (warning_at (input_location, OPT_Wshadow, + "declaration of %qD shadows a " + "global declaration", x)) + inform (DECL_SOURCE_LOCATION (oldglobal), + "shadowed declaration is here"); + } } - print_node_brief (stderr, "", t, 0); - if (t == error_mark_node) - break; - } - if (i) - fprintf (stderr, "\n"); - } - if (vec_safe_length (lvl->class_shadowed)) - { - size_t i; - cp_class_binding *b; - fprintf (stderr, " class-shadowed:"); - FOR_EACH_VEC_ELT (*lvl->class_shadowed, i, b) - fprintf (stderr, " %s ", IDENTIFIER_POINTER (b->identifier)); - fprintf (stderr, "\n"); - } - if (lvl->type_shadowed) - { - fprintf (stderr, " type-shadowed:"); - for (t = lvl->type_shadowed; t; t = TREE_CHAIN (t)) - { - fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t))); } - fprintf (stderr, "\n"); - } -} - -DEBUG_FUNCTION void -debug (cp_binding_level &ref) -{ - print_binding_level (&ref); -} - -DEBUG_FUNCTION void -debug (cp_binding_level *ptr) -{ - if (ptr) - debug (*ptr); - else - fprintf (stderr, "\n"); -} - -void -print_other_binding_stack (cp_binding_level *stack) -{ - cp_binding_level *level; - for (level = stack; !global_scope_p (level); level = level->level_chain) - { - fprintf (stderr, "binding level %p\n", (void *) level); - print_binding_level (level); + if (VAR_P (x)) + maybe_register_incomplete_var (x); } -} -void -print_binding_stack (void) -{ - cp_binding_level *b; - fprintf (stderr, "current_binding_level=%p\n" - "class_binding_level=%p\n" - "NAMESPACE_LEVEL (global_namespace)=%p\n", - (void *) current_binding_level, (void *) class_binding_level, - (void *) NAMESPACE_LEVEL (global_namespace)); - if (class_binding_level) - { - for (b = class_binding_level; b; b = b->level_chain) - if (b == current_binding_level) - break; - if (b) - b = class_binding_level; - else - b = current_binding_level; - } - else - b = current_binding_level; - print_other_binding_stack (b); - fprintf (stderr, "global:\n"); - print_binding_level (NAMESPACE_LEVEL (global_namespace)); -} - -/* Return the type associated with ID. */ + if (need_new_binding) + add_decl_to_level (x, + DECL_NAMESPACE_SCOPE_P (x) + ? NAMESPACE_LEVEL (CP_DECL_CONTEXT (x)) + : current_binding_level); -static tree -identifier_type_value_1 (tree id) -{ - /* There is no type with that name, anywhere. */ - if (REAL_IDENTIFIER_TYPE_VALUE (id) == NULL_TREE) - return NULL_TREE; - /* This is not the type marker, but the real thing. */ - if (REAL_IDENTIFIER_TYPE_VALUE (id) != global_type_node) - return REAL_IDENTIFIER_TYPE_VALUE (id); - /* Have to search for it. It must be on the global level, now. - Ask lookup_name not to return non-types. */ - id = lookup_name_real (id, 2, 1, /*block_p=*/true, 0, 0); - if (id) - return TREE_TYPE (id); - return NULL_TREE; + return x; } -/* Wrapper for identifier_type_value_1. */ +/* Wrapper for pushdecl_maybe_friend_1. */ tree -identifier_type_value (tree id) +pushdecl_maybe_friend (tree x, bool is_friend) { tree ret; - timevar_start (TV_NAME_LOOKUP); - ret = identifier_type_value_1 (id); - timevar_stop (TV_NAME_LOOKUP); + bool subtime = timevar_cond_start (TV_NAME_LOOKUP); + ret = pushdecl_maybe_friend_1 (x, is_friend); + timevar_cond_stop (TV_NAME_LOOKUP, subtime); return ret; } - -/* Return the IDENTIFIER_GLOBAL_VALUE of T, for use in common code, since - the definition of IDENTIFIER_GLOBAL_VALUE is different for C and C++. */ +/* Record a decl-node X as belonging to the current lexical scope. */ tree -identifier_global_value (tree t) +pushdecl (tree x) { - return IDENTIFIER_GLOBAL_VALUE (t); + return pushdecl_maybe_friend (x, false); } -/* Push a definition of struct, union or enum tag named ID. into - binding_level B. DECL is a TYPE_DECL for the type. We assume that - the tag ID is not already defined. */ +/* Enter DECL into the symbol table, if that's appropriate. Returns + DECL, or a modified version thereof. */ -static void -set_identifier_type_value_with_scope (tree id, tree decl, cp_binding_level *b) +tree +maybe_push_decl (tree decl) { - tree type; + tree type = TREE_TYPE (decl); - if (b->kind != sk_namespace) - { - /* Shadow the marker, not the real thing, so that the marker - gets restored later. */ - tree old_type_value = REAL_IDENTIFIER_TYPE_VALUE (id); - b->type_shadowed - = tree_cons (id, old_type_value, b->type_shadowed); - type = decl ? TREE_TYPE (decl) : NULL_TREE; - TREE_TYPE (b->type_shadowed) = type; - } + /* Add this decl to the current binding level, but not if it comes + from another scope, e.g. a static member variable. TEM may equal + DECL or it may be a previous decl of the same name. */ + if (decl == error_mark_node + || (TREE_CODE (decl) != PARM_DECL + && DECL_CONTEXT (decl) != NULL_TREE + /* Definitions of namespace members outside their namespace are + possible. */ + && !DECL_NAMESPACE_SCOPE_P (decl)) + || (TREE_CODE (decl) == TEMPLATE_DECL && !namespace_bindings_p ()) + || type == unknown_type_node + /* The declaration of a template specialization does not affect + the functions available for overload resolution, so we do not + call pushdecl. */ + || (TREE_CODE (decl) == FUNCTION_DECL + && DECL_TEMPLATE_SPECIALIZATION (decl))) + return decl; else - { - cxx_binding *binding = - binding_for_name (NAMESPACE_LEVEL (current_namespace), id); - gcc_assert (decl); - if (binding->value) - supplement_binding (binding, decl); - else - binding->value = decl; - - /* Store marker instead of real type. */ - type = global_type_node; - } - SET_IDENTIFIER_TYPE_VALUE (id, type); + return pushdecl (decl); } -/* As set_identifier_type_value_with_scope, but using - current_binding_level. */ +/* Bind DECL to ID in the current_binding_level, assumed to be a local + binding level. If PUSH_USING is set in FLAGS, we know that DECL + doesn't really belong to this binding level, that it got here + through a using-declaration. */ void -set_identifier_type_value (tree id, tree decl) +push_local_binding (tree id, tree decl, int flags) { - set_identifier_type_value_with_scope (id, decl, current_binding_level); -} + cp_binding_level *b; -/* Return the name for the constructor (or destructor) for the - specified class TYPE. When given a template, this routine doesn't - lose the specialization. */ + /* Skip over any local classes. This makes sense if we call + push_local_binding with a friend decl of a local class. */ + b = innermost_nonclass_level (); -static inline tree -constructor_name_full (tree type) -{ - return TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type)); -} + if (lookup_name_innermost_nonclass_level (id)) + { + /* Supplement the existing binding. */ + if (!supplement_binding (IDENTIFIER_BINDING (id), decl)) + /* It didn't work. Something else must be bound at this + level. Do not add DECL to the list of things to pop + later. */ + return; + } + else + /* Create a new binding. */ + push_binding (id, decl, b); -/* Return the name for the constructor (or destructor) for the - specified class. When given a template, return the plain - unspecialized name. */ + if (TREE_CODE (decl) == OVERLOAD || (flags & PUSH_USING)) + /* We must put the OVERLOAD into a TREE_LIST since the + TREE_CHAIN of an OVERLOAD is already used. Similarly for + decls that got here through a using-declaration. */ + decl = build_tree_list (NULL_TREE, decl); -tree -constructor_name (tree type) -{ - tree name; - name = constructor_name_full (type); - if (IDENTIFIER_TEMPLATE (name)) - name = IDENTIFIER_TEMPLATE (name); - return name; + /* And put DECL on the list of things declared by the current + binding level. */ + add_decl_to_level (decl, b); } -/* Returns TRUE if NAME is the name for the constructor for TYPE, - which must be a class type. */ +/* Check to see whether or not DECL is a variable that would have been + in scope under the ARM, but is not in scope under the ANSI/ISO + standard. If so, issue an error message. If name lookup would + work in both cases, but return a different result, this function + returns the result of ANSI/ISO lookup. Otherwise, it returns + DECL. */ -bool -constructor_name_p (tree name, tree type) +tree +check_for_out_of_scope_variable (tree decl) { - tree ctor_name; - - gcc_assert (MAYBE_CLASS_TYPE_P (type)); - - if (!name) - return false; - - if (!identifier_p (name)) - return false; + tree shadowed; - /* These don't have names. */ - if (TREE_CODE (type) == DECLTYPE_TYPE - || TREE_CODE (type) == TYPEOF_TYPE) - return false; + /* We only care about out of scope variables. */ + if (!(VAR_P (decl) && DECL_DEAD_FOR_LOCAL (decl))) + return decl; - ctor_name = constructor_name_full (type); - if (name == ctor_name) - return true; - if (IDENTIFIER_TEMPLATE (ctor_name) - && name == IDENTIFIER_TEMPLATE (ctor_name)) - return true; - return false; -} + shadowed = DECL_HAS_SHADOWED_FOR_VAR_P (decl) + ? DECL_SHADOWED_FOR_VAR (decl) : NULL_TREE ; + while (shadowed != NULL_TREE && VAR_P (shadowed) + && DECL_DEAD_FOR_LOCAL (shadowed)) + shadowed = DECL_HAS_SHADOWED_FOR_VAR_P (shadowed) + ? DECL_SHADOWED_FOR_VAR (shadowed) : NULL_TREE; + if (!shadowed) + shadowed = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (decl)); + if (shadowed) + { + if (!DECL_ERROR_REPORTED (decl)) + { + warning (0, "name lookup of %qD changed", DECL_NAME (decl)); + warning_at (DECL_SOURCE_LOCATION (shadowed), 0, + " matches this %qD under ISO standard rules", + shadowed); + warning_at (DECL_SOURCE_LOCATION (decl), 0, + " matches this %qD under old rules", decl); + DECL_ERROR_REPORTED (decl) = 1; + } + return shadowed; + } -/* Counter used to create anonymous type names. */ + /* If we have already complained about this declaration, there's no + need to do it again. */ + if (DECL_ERROR_REPORTED (decl)) + return decl; -static GTY(()) int anon_cnt; + DECL_ERROR_REPORTED (decl) = 1; -/* Return an IDENTIFIER which can be used as a name for - unnamed structs and unions. */ + if (TREE_TYPE (decl) == error_mark_node) + return decl; -tree -make_anon_name (void) -{ - char buf[32]; + if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl))) + { + error ("name lookup of %qD changed for ISO % scoping", + DECL_NAME (decl)); + error (" cannot use obsolete binding at %q+D because " + "it has a destructor", decl); + return error_mark_node; + } + else + { + permerror (input_location, "name lookup of %qD changed for ISO % scoping", + DECL_NAME (decl)); + if (flag_permissive) + permerror (DECL_SOURCE_LOCATION (decl), + " using obsolete binding at %qD", decl); + else + { + static bool hint; + if (!hint) + { + inform (input_location, "(if you use %<-fpermissive%> G++ will accept your code)"); + hint = true; + } + } + } - sprintf (buf, anon_aggrname_format (), anon_cnt++); - return get_identifier (buf); + return decl; } + +/* true means unconditionally make a BLOCK for the next level pushed. */ -/* This code is practically identical to that for creating - anonymous names, but is just used for lambdas instead. This isn't really - necessary, but it's convenient to avoid treating lambdas like other - unnamed types. */ +static bool keep_next_level_flag; -static GTY(()) int lambda_cnt = 0; +static int binding_depth = 0; -tree -make_lambda_name (void) +static void +indent (int depth) { - char buf[32]; + int i; - sprintf (buf, LAMBDANAME_FORMAT, lambda_cnt++); - return get_identifier (buf); + for (i = 0; i < depth * 2; i++) + putc (' ', stderr); } -/* Return (from the stack of) the BINDING, if any, established at SCOPE. */ - -static inline cxx_binding * -find_binding (cp_binding_level *scope, cxx_binding *binding) +/* Return a string describing the kind of SCOPE we have. */ +static const char * +cp_binding_level_descriptor (cp_binding_level *scope) { - for (; binding != NULL; binding = binding->previous) - if (binding->scope == scope) - return binding; + /* The order of this table must match the "scope_kind" + enumerators. */ + static const char* scope_kind_names[] = { + "block-scope", + "cleanup-scope", + "try-scope", + "catch-scope", + "for-scope", + "function-parameter-scope", + "class-scope", + "namespace-scope", + "template-parameter-scope", + "template-explicit-spec-scope" + }; + const scope_kind kind = scope->explicit_spec_p + ? sk_template_spec : scope->kind; - return (cxx_binding *)0; + return scope_kind_names[kind]; } -/* Return the binding for NAME in SCOPE, if any. Otherwise, return NULL. */ - -static inline cxx_binding * -cp_binding_level_find_binding_for_name (cp_binding_level *scope, tree name) +/* Output a debugging information about SCOPE when performing + ACTION at LINE. */ +static void +cp_binding_level_debug (cp_binding_level *scope, int line, const char *action) { - cxx_binding *b = IDENTIFIER_NAMESPACE_BINDINGS (name); - if (b) - { - /* Fold-in case where NAME is used only once. */ - if (scope == b->scope && b->previous == NULL) - return b; - return find_binding (scope, b); - } - return NULL; + const char *desc = cp_binding_level_descriptor (scope); + if (scope->this_entity) + verbatim ("%s %s(%E) %p %d\n", action, desc, + scope->this_entity, (void *) scope, line); + else + verbatim ("%s %s %p %d\n", action, desc, (void *) scope, line); } -/* Always returns a binding for name in scope. If no binding is - found, make a new one. */ +/* Return the estimated initial size of the hashtable of a NAMESPACE + scope. */ -static cxx_binding * -binding_for_name (cp_binding_level *scope, tree name) +static inline size_t +namespace_scope_ht_size (tree ns) { - cxx_binding *result; + tree name = DECL_NAME (ns); - result = cp_binding_level_find_binding_for_name (scope, name); - if (result) - return result; - /* Not found, make a new one. */ - result = cxx_binding_make (NULL, NULL); - result->previous = IDENTIFIER_NAMESPACE_BINDINGS (name); - result->scope = scope; - result->is_local = false; - result->value_is_inherited = false; - IDENTIFIER_NAMESPACE_BINDINGS (name) = result; - return result; + return name == std_identifier + ? NAMESPACE_STD_HT_SIZE + : (name == global_scope_name + ? GLOBAL_SCOPE_HT_SIZE + : NAMESPACE_ORDINARY_HT_SIZE); } -/* Walk through the bindings associated to the name of FUNCTION, - and return the first declaration of a function with a - "C" linkage specification, a.k.a 'extern "C"'. - This function looks for the binding, regardless of which scope it - has been defined in. It basically looks in all the known scopes. - Note that this function does not lookup for bindings of builtin functions - or for functions declared in system headers. */ -static tree -lookup_extern_c_fun_in_all_ns (tree function) -{ - tree name; - cxx_binding *iter; +/* A chain of binding_level structures awaiting reuse. */ - gcc_assert (function && TREE_CODE (function) == FUNCTION_DECL); +static GTY((deletable)) cp_binding_level *free_binding_level; - name = DECL_NAME (function); - gcc_assert (name && identifier_p (name)); +/* Insert SCOPE as the innermost binding level. */ - for (iter = IDENTIFIER_NAMESPACE_BINDINGS (name); - iter; - iter = iter->previous) +void +push_binding_level (cp_binding_level *scope) +{ + /* Add it to the front of currently active scopes stack. */ + scope->level_chain = current_binding_level; + current_binding_level = scope; + keep_next_level_flag = false; + + if (ENABLE_SCOPE_CHECKING) { - tree ovl; - for (ovl = iter->value; ovl; ovl = OVL_NEXT (ovl)) - { - tree decl = OVL_CURRENT (ovl); - if (decl - && TREE_CODE (decl) == FUNCTION_DECL - && DECL_EXTERN_C_P (decl) - && !DECL_ARTIFICIAL (decl)) - { - return decl; - } - } + scope->binding_depth = binding_depth; + indent (binding_depth); + cp_binding_level_debug (scope, LOCATION_LINE (input_location), + "push"); + binding_depth++; } - return NULL; } -/* Returns a list of C-linkage decls with the name NAME. */ +/* Create a new KIND scope and make it the top of the active scopes stack. + ENTITY is the scope of the associated C++ entity (namespace, class, + function, C++0x enumeration); it is NULL otherwise. */ -tree -c_linkage_bindings (tree name) +cp_binding_level * +begin_scope (scope_kind kind, tree entity) { - tree decls = NULL_TREE; - cxx_binding *iter; + cp_binding_level *scope; - for (iter = IDENTIFIER_NAMESPACE_BINDINGS (name); - iter; - iter = iter->previous) + /* Reuse or create a struct for this binding level. */ + if (!ENABLE_SCOPE_CHECKING && free_binding_level) { - tree ovl; - for (ovl = iter->value; ovl; ovl = OVL_NEXT (ovl)) - { - tree decl = OVL_CURRENT (ovl); - if (decl - && DECL_EXTERN_C_P (decl) - && !DECL_ARTIFICIAL (decl)) - { - if (decls == NULL_TREE) - decls = decl; - else - decls = tree_cons (NULL_TREE, decl, decls); - } - } + scope = free_binding_level; + free_binding_level = scope->level_chain; + memset (scope, 0, sizeof (cp_binding_level)); } - return decls; -} + else + scope = ggc_cleared_alloc (); -/* Insert another USING_DECL into the current binding level, returning - this declaration. If this is a redeclaration, do nothing, and - return NULL_TREE if this not in namespace scope (in namespace - scope, a using decl might extend any previous bindings). */ + scope->this_entity = entity; + scope->more_cleanups_ok = true; + switch (kind) + { + case sk_cleanup: + scope->keep = true; + break; -static tree -push_using_decl_1 (tree scope, tree name) -{ - tree decl; + case sk_template_spec: + scope->explicit_spec_p = true; + kind = sk_template_parms; + /* Fall through. */ + case sk_template_parms: + case sk_block: + case sk_try: + case sk_catch: + case sk_for: + case sk_cond: + case sk_class: + case sk_scoped_enum: + case sk_function_parms: + case sk_transaction: + case sk_omp: + scope->keep = keep_next_level_flag; + break; - gcc_assert (TREE_CODE (scope) == NAMESPACE_DECL); - gcc_assert (identifier_p (name)); - for (decl = current_binding_level->usings; decl; decl = DECL_CHAIN (decl)) - if (USING_DECL_SCOPE (decl) == scope && DECL_NAME (decl) == name) + case sk_namespace: + NAMESPACE_LEVEL (entity) = scope; + vec_alloc (scope->static_decls, + (DECL_NAME (entity) == std_identifier + || DECL_NAME (entity) == global_scope_name) ? 200 : 10); break; - if (decl) - return namespace_bindings_p () ? decl : NULL_TREE; - decl = build_lang_decl (USING_DECL, name, NULL_TREE); - USING_DECL_SCOPE (decl) = scope; - DECL_CHAIN (decl) = current_binding_level->usings; - current_binding_level->usings = decl; - return decl; + + default: + /* Should not happen. */ + gcc_unreachable (); + break; + } + scope->kind = kind; + + push_binding_level (scope); + + return scope; } -/* Wrapper for push_using_decl_1. */ +/* We're about to leave current scope. Pop the top of the stack of + currently active scopes. Return the enclosing scope, now active. */ -static tree -push_using_decl (tree scope, tree name) +cp_binding_level * +leave_scope (void) { - tree ret; - timevar_start (TV_NAME_LOOKUP); - ret = push_using_decl_1 (scope, name); - timevar_stop (TV_NAME_LOOKUP); - return ret; -} + cp_binding_level *scope = current_binding_level; -/* Same as pushdecl, but define X in binding-level LEVEL. We rely on the - caller to set DECL_CONTEXT properly. + if (scope->kind == sk_namespace && class_binding_level) + current_binding_level = class_binding_level; - Note that this must only be used when X will be the new innermost - binding for its name, as we tack it onto the front of IDENTIFIER_BINDING - without checking to see if the current IDENTIFIER_BINDING comes from a - closer binding level than LEVEL. */ + /* We cannot leave a scope, if there are none left. */ + if (NAMESPACE_LEVEL (global_namespace)) + gcc_assert (!global_scope_p (scope)); -static tree -pushdecl_with_scope_1 (tree x, cp_binding_level *level, bool is_friend) -{ - cp_binding_level *b; - tree function_decl = current_function_decl; + if (ENABLE_SCOPE_CHECKING) + { + indent (--binding_depth); + cp_binding_level_debug (scope, LOCATION_LINE (input_location), + "leave"); + } - current_function_decl = NULL_TREE; - if (level->kind == sk_class) + /* Move one nesting level up. */ + current_binding_level = scope->level_chain; + + /* Namespace-scopes are left most probably temporarily, not + completely; they can be reopened later, e.g. in namespace-extension + or any name binding activity that requires us to resume a + namespace. For classes, we cache some binding levels. For other + scopes, we just make the structure available for reuse. */ + if (scope->kind != sk_namespace + && scope->kind != sk_class) { - b = class_binding_level; - class_binding_level = level; - pushdecl_class_level (x); - class_binding_level = b; + scope->level_chain = free_binding_level; + gcc_assert (!ENABLE_SCOPE_CHECKING + || scope->binding_depth == binding_depth); + free_binding_level = scope; } - else + + if (scope->kind == sk_class) { - b = current_binding_level; - current_binding_level = level; - x = pushdecl_maybe_friend (x, is_friend); - current_binding_level = b; + /* Reset DEFINING_CLASS_P to allow for reuse of a + class-defining scope in a non-defining context. */ + scope->defining_class_p = 0; + + /* Find the innermost enclosing class scope, and reset + CLASS_BINDING_LEVEL appropriately. */ + class_binding_level = NULL; + for (scope = current_binding_level; scope; scope = scope->level_chain) + if (scope->kind == sk_class) + { + class_binding_level = scope; + break; + } } - current_function_decl = function_decl; - return x; + + return current_binding_level; } - -/* Wrapper for pushdecl_with_scope_1. */ -tree -pushdecl_with_scope (tree x, cp_binding_level *level, bool is_friend) +static void +resume_scope (cp_binding_level* b) { - tree ret; - bool subtime = timevar_cond_start (TV_NAME_LOOKUP); - ret = pushdecl_with_scope_1 (x, level, is_friend); - timevar_cond_stop (TV_NAME_LOOKUP, subtime); - return ret; + /* Resuming binding levels is meant only for namespaces, + and those cannot nest into classes. */ + gcc_assert (!class_binding_level); + /* Also, resuming a non-directly nested namespace is a no-no. */ + gcc_assert (b->level_chain == current_binding_level); + current_binding_level = b; + if (ENABLE_SCOPE_CHECKING) + { + b->binding_depth = binding_depth; + indent (binding_depth); + cp_binding_level_debug (b, LOCATION_LINE (input_location), "resume"); + binding_depth++; + } } -/* Helper function for push_overloaded_decl_1 and do_nonmember_using_decl. - Compares the parameter-type-lists of DECL1 and DECL2 and returns false - if they are different. If the DECLs are template functions, the return - types and the template parameter lists are compared too (DR 565). */ +/* Return the innermost binding level that is not for a class scope. */ -static bool -compparms_for_decl_and_using_decl (tree decl1, tree decl2) +static cp_binding_level * +innermost_nonclass_level (void) { - if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (decl1)), - TYPE_ARG_TYPES (TREE_TYPE (decl2)))) - return false; + cp_binding_level *b; - if (! DECL_FUNCTION_TEMPLATE_P (decl1) - || ! DECL_FUNCTION_TEMPLATE_P (decl2)) - return true; + b = current_binding_level; + while (b->kind == sk_class) + b = b->level_chain; - return (comp_template_parms (DECL_TEMPLATE_PARMS (decl1), - DECL_TEMPLATE_PARMS (decl2)) - && same_type_p (TREE_TYPE (TREE_TYPE (decl1)), - TREE_TYPE (TREE_TYPE (decl2)))); + return b; } -/* DECL is a FUNCTION_DECL for a non-member function, which may have - other definitions already in place. We get around this by making - the value of the identifier point to a list of all the things that - want to be referenced by that name. It is then up to the users of - that name to decide what to do with that list. - - DECL may also be a TEMPLATE_DECL, with a FUNCTION_DECL in its - DECL_TEMPLATE_RESULT. It is dealt with the same way. - - FLAGS is a bitwise-or of the following values: - PUSH_LOCAL: Bind DECL in the current scope, rather than at - namespace scope. - PUSH_USING: DECL is being pushed as the result of a using - declaration. - - IS_FRIEND is true if this is a friend declaration. - - The value returned may be a previous declaration if we guessed wrong - about what language DECL should belong to (C or C++). Otherwise, - it's always DECL (and never something that's not a _DECL). */ +/* We're defining an object of type TYPE. If it needs a cleanup, but + we're not allowed to add any more objects with cleanups to the current + scope, create a new binding level. */ -static tree -push_overloaded_decl_1 (tree decl, int flags, bool is_friend) +void +maybe_push_cleanup_level (tree type) { - tree name = DECL_NAME (decl); - tree old; - tree new_binding; - int doing_global = (namespace_bindings_p () || !(flags & PUSH_LOCAL)); - - if (doing_global) - old = namespace_binding (name, DECL_CONTEXT (decl)); - else - old = lookup_name_innermost_nonclass_level (name); - - if (old) + if (type != error_mark_node + && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type) + && current_binding_level->more_cleanups_ok == 0) { - if (TREE_CODE (old) == TYPE_DECL && DECL_ARTIFICIAL (old)) - { - tree t = TREE_TYPE (old); - if (MAYBE_CLASS_TYPE_P (t) && warn_shadow - && (! DECL_IN_SYSTEM_HEADER (decl) - || ! DECL_IN_SYSTEM_HEADER (old))) - warning (OPT_Wshadow, "%q#D hides constructor for %q#T", decl, t); - old = NULL_TREE; - } - else if (is_overloaded_fn (old)) - { - tree tmp; + begin_scope (sk_cleanup, NULL); + current_binding_level->statement_list = push_stmt_list (); + } +} - for (tmp = old; tmp; tmp = OVL_NEXT (tmp)) - { - tree fn = OVL_CURRENT (tmp); - tree dup; +/* Return true if we are in the global binding level. */ - if (TREE_CODE (tmp) == OVERLOAD && OVL_USED (tmp) - && !(flags & PUSH_USING) - && compparms_for_decl_and_using_decl (fn, decl) - && ! decls_match (fn, decl)) - diagnose_name_conflict (decl, fn); +bool +global_bindings_p (void) +{ + return global_scope_p (current_binding_level); +} - dup = duplicate_decls (decl, fn, is_friend); - /* If DECL was a redeclaration of FN -- even an invalid - one -- pass that information along to our caller. */ - if (dup == fn || dup == error_mark_node) - return dup; - } +/* True if we are currently in a toplevel binding level. This + means either the global binding level or a namespace in a toplevel + binding level. Since there are no non-toplevel namespace levels, + this really means any namespace or template parameter level. We + also include a class whose context is toplevel. */ - /* We don't overload implicit built-ins. duplicate_decls() - may fail to merge the decls if the new decl is e.g. a - template function. */ - if (TREE_CODE (old) == FUNCTION_DECL - && DECL_ANTICIPATED (old) - && !DECL_HIDDEN_FRIEND_P (old)) - old = NULL; - } - else if (old == error_mark_node) - /* Ignore the undefined symbol marker. */ - old = NULL_TREE; - else - { - error ("previous non-function declaration %q+#D", old); - error ("conflicts with function declaration %q#D", decl); - return decl; - } - } +bool +toplevel_bindings_p (void) +{ + cp_binding_level *b = innermost_nonclass_level (); - if (old || TREE_CODE (decl) == TEMPLATE_DECL - /* If it's a using declaration, we always need to build an OVERLOAD, - because it's the only way to remember that the declaration comes - from 'using', and have the lookup behave correctly. */ - || (flags & PUSH_USING)) - { - if (old && TREE_CODE (old) != OVERLOAD) - /* Wrap the existing single decl in an overload. */ - new_binding = ovl_cons (old, NULL_TREE); - else - new_binding = old; - new_binding = ovl_cons (decl, new_binding); - if (flags & PUSH_USING) - OVL_USED (new_binding) = 1; - } - else - /* NAME is not ambiguous. */ - new_binding = decl; + return b->kind == sk_namespace || b->kind == sk_template_parms; +} - if (doing_global) - set_namespace_binding (name, current_namespace, new_binding); - else - { - /* We only create an OVERLOAD if there was a previous binding at - this level, or if decl is a template. In the former case, we - need to remove the old binding and replace it with the new - binding. We must also run through the NAMES on the binding - level where the name was bound to update the chain. */ +/* True if this is a namespace scope, or if we are defining a class + which is itself at namespace scope, or whose enclosing class is + such a class, etc. */ - if (TREE_CODE (new_binding) == OVERLOAD && old) - { - tree *d; +bool +namespace_bindings_p (void) +{ + cp_binding_level *b = innermost_nonclass_level (); - for (d = &IDENTIFIER_BINDING (name)->scope->names; - *d; - d = &TREE_CHAIN (*d)) - if (*d == old - || (TREE_CODE (*d) == TREE_LIST - && TREE_VALUE (*d) == old)) - { - if (TREE_CODE (*d) == TREE_LIST) - /* Just replace the old binding with the new. */ - TREE_VALUE (*d) = new_binding; - else - /* Build a TREE_LIST to wrap the OVERLOAD. */ - *d = tree_cons (NULL_TREE, new_binding, - TREE_CHAIN (*d)); + return b->kind == sk_namespace; +} - /* And update the cxx_binding node. */ - IDENTIFIER_BINDING (name)->value = new_binding; - return decl; - } +/* True if the innermost non-class scope is a block scope. */ - /* We should always find a previous binding in this case. */ - gcc_unreachable (); - } +bool +local_bindings_p (void) +{ + cp_binding_level *b = innermost_nonclass_level (); + return b->kind < sk_function_parms || b->kind == sk_omp; +} - /* Install the new binding. */ - push_local_binding (name, new_binding, flags); - } +/* True if the current level needs to have a BLOCK made. */ - return decl; +bool +kept_level_p (void) +{ + return (current_binding_level->blocks != NULL_TREE + || current_binding_level->keep + || current_binding_level->kind == sk_cleanup + || current_binding_level->names != NULL_TREE + || current_binding_level->using_directives); } -/* Wrapper for push_overloaded_decl_1. */ +/* Returns the kind of the innermost scope. */ -static tree -push_overloaded_decl (tree decl, int flags, bool is_friend) +scope_kind +innermost_scope_kind (void) { - tree ret; - bool subtime = timevar_cond_start (TV_NAME_LOOKUP); - ret = push_overloaded_decl_1 (decl, flags, is_friend); - timevar_cond_stop (TV_NAME_LOOKUP, subtime); - return ret; + return current_binding_level->kind; } -/* Check a non-member using-declaration. Return the name and scope - being used, and the USING_DECL, or NULL_TREE on failure. */ +/* Returns true if this scope was created to store template parameters. */ -static tree -validate_nonmember_using_decl (tree decl, tree scope, tree name) +bool +template_parm_scope_p (void) { - /* [namespace.udecl] - A using-declaration for a class member shall be a - member-declaration. */ - if (TYPE_P (scope)) - { - error ("%qT is not a namespace or unscoped enum", scope); - return NULL_TREE; - } - else if (scope == error_mark_node) - return NULL_TREE; + return innermost_scope_kind () == sk_template_parms; +} - if (TREE_CODE (decl) == TEMPLATE_ID_EXPR) - { - /* 7.3.3/5 - A using-declaration shall not name a template-id. */ - error ("a using-declaration cannot specify a template-id. " - "Try %", name); - return NULL_TREE; - } +/* If KEEP is true, make a BLOCK node for the next binding level, + unconditionally. Otherwise, use the normal logic to decide whether + or not to create a BLOCK. */ - if (TREE_CODE (decl) == NAMESPACE_DECL) - { - error ("namespace %qD not allowed in using-declaration", decl); - return NULL_TREE; - } +void +keep_next_level (bool keep) +{ + keep_next_level_flag = keep; +} - if (TREE_CODE (decl) == SCOPE_REF) - { - /* It's a nested name with template parameter dependent scope. - This can only be using-declaration for class member. */ - error ("%qT is not a namespace", TREE_OPERAND (decl, 0)); - return NULL_TREE; - } +/* Return the list of declarations of the current level. + Note that this list is in reverse order unless/until + you nreverse it; and when you do nreverse it, you must + store the result back using `storedecls' or you will lose. */ - if (is_overloaded_fn (decl)) - decl = get_first_fn (decl); +tree +getdecls (void) +{ + return current_binding_level->names; +} - gcc_assert (DECL_P (decl)); +/* Return how many function prototypes we are currently nested inside. */ - /* Make a USING_DECL. */ - tree using_decl = push_using_decl (scope, name); +int +function_parm_depth (void) +{ + int level = 0; + cp_binding_level *b; - if (using_decl == NULL_TREE - && at_function_scope_p () - && VAR_P (decl)) - /* C++11 7.3.3/10. */ - error ("%qD is already declared in this scope", name); - - return using_decl; + for (b = current_binding_level; + b->kind == sk_function_parms; + b = b->level_chain) + ++level; + + return level; } -/* Process local and global using-declarations. */ +/* For debugging. */ +static int no_print_functions = 0; +static int no_print_builtins = 0; static void -do_nonmember_using_decl (tree scope, tree name, tree oldval, tree oldtype, - tree *newval, tree *newtype) +print_binding_level (cp_binding_level* lvl) { - struct scope_binding decls = EMPTY_SCOPE_BINDING; - - *newval = *newtype = NULL_TREE; - if (!qualified_lookup_using_namespace (name, scope, &decls, 0)) - /* Lookup error */ - return; + tree t; + int i = 0, len; + fprintf (stderr, " blocks=%p", (void *) lvl->blocks); + if (lvl->more_cleanups_ok) + fprintf (stderr, " more-cleanups-ok"); + if (lvl->have_cleanups) + fprintf (stderr, " have-cleanups"); + fprintf (stderr, "\n"); + if (lvl->names) + { + fprintf (stderr, " names:\t"); + /* We can probably fit 3 names to a line? */ + for (t = lvl->names; t; t = TREE_CHAIN (t)) + { + if (no_print_functions && (TREE_CODE (t) == FUNCTION_DECL)) + continue; + if (no_print_builtins + && (TREE_CODE (t) == TYPE_DECL) + && DECL_IS_BUILTIN (t)) + continue; - if (!decls.value && !decls.type) + /* Function decls tend to have longer names. */ + if (TREE_CODE (t) == FUNCTION_DECL) + len = 3; + else + len = 2; + i += len; + if (i > 6) + { + fprintf (stderr, "\n\t"); + i = len; + } + print_node_brief (stderr, "", t, 0); + if (t == error_mark_node) + break; + } + if (i) + fprintf (stderr, "\n"); + } + if (vec_safe_length (lvl->class_shadowed)) { - error ("%qD not declared", name); - return; + size_t i; + cp_class_binding *b; + fprintf (stderr, " class-shadowed:"); + FOR_EACH_VEC_ELT (*lvl->class_shadowed, i, b) + fprintf (stderr, " %s ", IDENTIFIER_POINTER (b->identifier)); + fprintf (stderr, "\n"); } - - /* Shift the old and new bindings around so we're comparing class and - enumeration names to each other. */ - if (oldval && DECL_IMPLICIT_TYPEDEF_P (oldval)) + if (lvl->type_shadowed) { - oldtype = oldval; - oldval = NULL_TREE; + fprintf (stderr, " type-shadowed:"); + for (t = lvl->type_shadowed; t; t = TREE_CHAIN (t)) + { + fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t))); + } + fprintf (stderr, "\n"); } +} - if (decls.value && DECL_IMPLICIT_TYPEDEF_P (decls.value)) +DEBUG_FUNCTION void +debug (cp_binding_level &ref) +{ + print_binding_level (&ref); +} + +DEBUG_FUNCTION void +debug (cp_binding_level *ptr) +{ + if (ptr) + debug (*ptr); + else + fprintf (stderr, "\n"); +} + + +void +print_other_binding_stack (cp_binding_level *stack) +{ + cp_binding_level *level; + for (level = stack; !global_scope_p (level); level = level->level_chain) { - decls.type = decls.value; - decls.value = NULL_TREE; + fprintf (stderr, "binding level %p\n", (void *) level); + print_binding_level (level); } +} - if (decls.value) +void +print_binding_stack (void) +{ + cp_binding_level *b; + fprintf (stderr, "current_binding_level=%p\n" + "class_binding_level=%p\n" + "NAMESPACE_LEVEL (global_namespace)=%p\n", + (void *) current_binding_level, (void *) class_binding_level, + (void *) NAMESPACE_LEVEL (global_namespace)); + if (class_binding_level) { - /* Check for using functions. */ - if (is_overloaded_fn (decls.value)) - { - tree tmp, tmp1; - - if (oldval && !is_overloaded_fn (oldval)) - { - error ("%qD is already declared in this scope", name); - oldval = NULL_TREE; - } + for (b = class_binding_level; b; b = b->level_chain) + if (b == current_binding_level) + break; + if (b) + b = class_binding_level; + else + b = current_binding_level; + } + else + b = current_binding_level; + print_other_binding_stack (b); + fprintf (stderr, "global:\n"); + print_binding_level (NAMESPACE_LEVEL (global_namespace)); +} + +/* Return the type associated with ID. */ - *newval = oldval; - for (tmp = decls.value; tmp; tmp = OVL_NEXT (tmp)) - { - tree new_fn = OVL_CURRENT (tmp); +static tree +identifier_type_value_1 (tree id) +{ + /* There is no type with that name, anywhere. */ + if (REAL_IDENTIFIER_TYPE_VALUE (id) == NULL_TREE) + return NULL_TREE; + /* This is not the type marker, but the real thing. */ + if (REAL_IDENTIFIER_TYPE_VALUE (id) != global_type_node) + return REAL_IDENTIFIER_TYPE_VALUE (id); + /* Have to search for it. It must be on the global level, now. + Ask lookup_name not to return non-types. */ + id = lookup_name_real (id, 2, 1, /*block_p=*/true, 0, 0); + if (id) + return TREE_TYPE (id); + return NULL_TREE; +} - /* Don't import functions that haven't been declared. */ - if (DECL_ANTICIPATED (new_fn)) - continue; +/* Wrapper for identifier_type_value_1. */ - /* [namespace.udecl] +tree +identifier_type_value (tree id) +{ + tree ret; + timevar_start (TV_NAME_LOOKUP); + ret = identifier_type_value_1 (id); + timevar_stop (TV_NAME_LOOKUP); + return ret; +} - If a function declaration in namespace scope or block - scope has the same name and the same parameter types as a - function introduced by a using declaration the program is - ill-formed. */ - for (tmp1 = oldval; tmp1; tmp1 = OVL_NEXT (tmp1)) - { - tree old_fn = OVL_CURRENT (tmp1); - if (new_fn == old_fn) - /* The function already exists in the current namespace. */ - break; - else if (TREE_CODE (tmp1) == OVERLOAD && OVL_USED (tmp1)) - continue; /* this is a using decl */ - else if (compparms_for_decl_and_using_decl (new_fn, old_fn)) - { - /* There was already a non-using declaration in - this scope with the same parameter types. If both - are the same extern "C" functions, that's ok. */ - if (DECL_ANTICIPATED (old_fn) - && !DECL_HIDDEN_FRIEND_P (old_fn)) - /* Ignore anticipated built-ins. */; - else if (decls_match (new_fn, old_fn)) - break; - else - { - diagnose_name_conflict (new_fn, old_fn); - break; - } - } - } +/* Return the IDENTIFIER_GLOBAL_VALUE of T, for use in common code, since + the definition of IDENTIFIER_GLOBAL_VALUE is different for C and C++. */ - /* If we broke out of the loop, there's no reason to add - this function to the using declarations for this - scope. */ - if (tmp1) - continue; +tree +identifier_global_value (tree t) +{ + return IDENTIFIER_GLOBAL_VALUE (t); +} - /* If we are adding to an existing OVERLOAD, then we no - longer know the type of the set of functions. */ - if (*newval && TREE_CODE (*newval) == OVERLOAD) - TREE_TYPE (*newval) = unknown_type_node; - /* Add this new function to the set. */ - *newval = build_overload (OVL_CURRENT (tmp), *newval); - /* If there is only one function, then we use its type. (A - using-declaration naming a single function can be used in - contexts where overload resolution cannot be - performed.) */ - if (TREE_CODE (*newval) != OVERLOAD) - { - *newval = ovl_cons (*newval, NULL_TREE); - TREE_TYPE (*newval) = TREE_TYPE (OVL_CURRENT (tmp)); - } - OVL_USED (*newval) = 1; - } - } - else - { - /* If we're declaring a non-function and OLDVAL is an anticipated - built-in, just pretend it isn't there. */ - if (oldval - && TREE_CODE (oldval) == FUNCTION_DECL - && DECL_ANTICIPATED (oldval) - && !DECL_HIDDEN_FRIEND_P (oldval)) - oldval = NULL_TREE; +/* Push a definition of struct, union or enum tag named ID. into + binding_level B. DECL is a TYPE_DECL for the type. We assume that + the tag ID is not already defined. */ - *newval = decls.value; - if (oldval && !decls_match (*newval, oldval)) - error ("%qD is already declared in this scope", name); - } - } - else - *newval = oldval; +static void +set_identifier_type_value_with_scope (tree id, tree decl, cp_binding_level *b) +{ + tree type; - if (decls.type && TREE_CODE (decls.type) == TREE_LIST) + if (b->kind != sk_namespace) { - error ("reference to %qD is ambiguous", name); - print_candidates (decls.type); + /* Shadow the marker, not the real thing, so that the marker + gets restored later. */ + tree old_type_value = REAL_IDENTIFIER_TYPE_VALUE (id); + b->type_shadowed + = tree_cons (id, old_type_value, b->type_shadowed); + type = decl ? TREE_TYPE (decl) : NULL_TREE; + TREE_TYPE (b->type_shadowed) = type; } else { - *newtype = decls.type; - if (oldtype && *newtype && !decls_match (oldtype, *newtype)) - error ("%qD is already declared in this scope", name); - } + cxx_binding *binding = + binding_for_name (NAMESPACE_LEVEL (current_namespace), id); + gcc_assert (decl); + if (binding->value) + supplement_binding (binding, decl); + else + binding->value = decl; - /* If *newval is empty, shift any class or enumeration name down. */ - if (!*newval) - { - *newval = *newtype; - *newtype = NULL_TREE; - } + /* Store marker instead of real type. */ + type = global_type_node; + } + SET_IDENTIFIER_TYPE_VALUE (id, type); } -/* Process a using-declaration at function scope. */ +/* As set_identifier_type_value_with_scope, but using + current_binding_level. */ void -do_local_using_decl (tree decl, tree scope, tree name) +set_identifier_type_value (tree id, tree decl) { - tree oldval, oldtype, newval, newtype; - tree orig_decl = decl; + set_identifier_type_value_with_scope (id, decl, current_binding_level); +} - decl = validate_nonmember_using_decl (decl, scope, name); - if (decl == NULL_TREE) - return; +/* Return the name for the constructor (or destructor) for the + specified class TYPE. When given a template, this routine doesn't + lose the specialization. */ - if (building_stmt_list_p () - && at_function_scope_p ()) - add_decl_expr (decl); +static inline tree +constructor_name_full (tree type) +{ + return TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type)); +} - oldval = lookup_name_innermost_nonclass_level (name); - oldtype = lookup_type_current_level (name); +/* Return the name for the constructor (or destructor) for the + specified class. When given a template, return the plain + unspecialized name. */ - do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype); +tree +constructor_name (tree type) +{ + tree name; + name = constructor_name_full (type); + if (IDENTIFIER_TEMPLATE (name)) + name = IDENTIFIER_TEMPLATE (name); + return name; +} - if (newval) - { - if (is_overloaded_fn (newval)) - { - tree fn, term; +/* Returns TRUE if NAME is the name for the constructor for TYPE, + which must be a class type. */ - /* We only need to push declarations for those functions - that were not already bound in the current level. - The old value might be NULL_TREE, it might be a single - function, or an OVERLOAD. */ - if (oldval && TREE_CODE (oldval) == OVERLOAD) - term = OVL_FUNCTION (oldval); - else - term = oldval; - for (fn = newval; fn && OVL_CURRENT (fn) != term; - fn = OVL_NEXT (fn)) - push_overloaded_decl (OVL_CURRENT (fn), - PUSH_LOCAL | PUSH_USING, - false); - } - else - push_local_binding (name, newval, PUSH_USING); - } - if (newtype) - { - push_local_binding (name, newtype, PUSH_USING); - set_identifier_type_value (name, newtype); - } +bool +constructor_name_p (tree name, tree type) +{ + tree ctor_name; - /* Emit debug info. */ - if (!processing_template_decl) - cp_emit_debug_info_for_using (orig_decl, current_scope()); + gcc_assert (MAYBE_CLASS_TYPE_P (type)); + + if (!name) + return false; + + if (!identifier_p (name)) + return false; + + /* These don't have names. */ + if (TREE_CODE (type) == DECLTYPE_TYPE + || TREE_CODE (type) == TYPEOF_TYPE) + return false; + + ctor_name = constructor_name_full (type); + if (name == ctor_name) + return true; + if (IDENTIFIER_TEMPLATE (ctor_name) + && name == IDENTIFIER_TEMPLATE (ctor_name)) + return true; + return false; } -/* Returns true if ROOT (a namespace, class, or function) encloses - CHILD. CHILD may be either a class type or a namespace. */ +/* Counter used to create anonymous type names. */ -bool -is_ancestor (tree root, tree child) +static GTY(()) int anon_cnt; + +/* Return an IDENTIFIER which can be used as a name for + unnamed structs and unions. */ + +tree +make_anon_name (void) { - gcc_assert ((TREE_CODE (root) == NAMESPACE_DECL - || TREE_CODE (root) == FUNCTION_DECL - || CLASS_TYPE_P (root))); - gcc_assert ((TREE_CODE (child) == NAMESPACE_DECL - || CLASS_TYPE_P (child))); + char buf[32]; + + sprintf (buf, anon_aggrname_format (), anon_cnt++); + return get_identifier (buf); +} + +/* This code is practically identical to that for creating + anonymous names, but is just used for lambdas instead. This isn't really + necessary, but it's convenient to avoid treating lambdas like other + unnamed types. */ + +static GTY(()) int lambda_cnt = 0; + +tree +make_lambda_name (void) +{ + char buf[32]; + + sprintf (buf, LAMBDANAME_FORMAT, lambda_cnt++); + return get_identifier (buf); +} - /* The global namespace encloses everything. */ - if (root == global_namespace) - return true; +/* Return (from the stack of) the BINDING, if any, established at SCOPE. */ - while (true) - { - /* If we've run out of scopes, stop. */ - if (!child) - return false; - /* If we've reached the ROOT, it encloses CHILD. */ - if (root == child) - return true; - /* Go out one level. */ - if (TYPE_P (child)) - child = TYPE_NAME (child); - child = DECL_CONTEXT (child); - } +static inline cxx_binding * +find_binding (cp_binding_level *scope, cxx_binding *binding) +{ + for (; binding != NULL; binding = binding->previous) + if (binding->scope == scope) + return binding; + + return (cxx_binding *)0; } -/* Enter the class or namespace scope indicated by T suitable for name - lookup. T can be arbitrary scope, not necessary nested inside the - current scope. Returns a non-null scope to pop iff pop_scope - should be called later to exit this scope. */ +/* Return the binding for NAME in SCOPE, if any. Otherwise, return NULL. */ -tree -push_scope (tree t) +static inline cxx_binding * +cp_binding_level_find_binding_for_name (cp_binding_level *scope, tree name) { - if (TREE_CODE (t) == NAMESPACE_DECL) - push_decl_namespace (t); - else if (CLASS_TYPE_P (t)) + cxx_binding *b = IDENTIFIER_NAMESPACE_BINDINGS (name); + if (b) { - if (!at_class_scope_p () - || !same_type_p (current_class_type, t)) - push_nested_class (t); - else - /* T is the same as the current scope. There is therefore no - need to re-enter the scope. Since we are not actually - pushing a new scope, our caller should not call - pop_scope. */ - t = NULL_TREE; + /* Fold-in case where NAME is used only once. */ + if (scope == b->scope && b->previous == NULL) + return b; + return find_binding (scope, b); } - - return t; + return NULL; } -/* Leave scope pushed by push_scope. */ +/* Always returns a binding for name in scope. If no binding is + found, make a new one. */ -void -pop_scope (tree t) +static cxx_binding * +binding_for_name (cp_binding_level *scope, tree name) { - if (t == NULL_TREE) - return; - if (TREE_CODE (t) == NAMESPACE_DECL) - pop_decl_namespace (); - else if CLASS_TYPE_P (t) - pop_nested_class (); -} + cxx_binding *result; -/* Subroutine of push_inner_scope. */ + result = cp_binding_level_find_binding_for_name (scope, name); + if (result) + return result; + /* Not found, make a new one. */ + result = cxx_binding_make (NULL, NULL); + result->previous = IDENTIFIER_NAMESPACE_BINDINGS (name); + result->scope = scope; + result->is_local = false; + result->value_is_inherited = false; + IDENTIFIER_NAMESPACE_BINDINGS (name) = result; + return result; +} -static void -push_inner_scope_r (tree outer, tree inner) +/* Walk through the bindings associated to the name of FUNCTION, + and return the first declaration of a function with a + "C" linkage specification, a.k.a 'extern "C"'. + This function looks for the binding, regardless of which scope it + has been defined in. It basically looks in all the known scopes. + Note that this function does not lookup for bindings of builtin functions + or for functions declared in system headers. */ +static tree +lookup_extern_c_fun_in_all_ns (tree function) { - tree prev; + tree name; + cxx_binding *iter; - if (outer == inner - || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner))) - return; + gcc_assert (function && TREE_CODE (function) == FUNCTION_DECL); - prev = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner)); - if (outer != prev) - push_inner_scope_r (outer, prev); - if (TREE_CODE (inner) == NAMESPACE_DECL) + name = DECL_NAME (function); + gcc_assert (name && identifier_p (name)); + + for (iter = IDENTIFIER_NAMESPACE_BINDINGS (name); + iter; + iter = iter->previous) { - cp_binding_level *save_template_parm = 0; - /* Temporary take out template parameter scopes. They are saved - in reversed order in save_template_parm. */ - while (current_binding_level->kind == sk_template_parms) + tree ovl; + for (ovl = iter->value; ovl; ovl = OVL_NEXT (ovl)) { - cp_binding_level *b = current_binding_level; - current_binding_level = b->level_chain; - b->level_chain = save_template_parm; - save_template_parm = b; + tree decl = OVL_CURRENT (ovl); + if (decl + && TREE_CODE (decl) == FUNCTION_DECL + && DECL_EXTERN_C_P (decl) + && !DECL_ARTIFICIAL (decl)) + { + return decl; + } } + } + return NULL; +} - resume_scope (NAMESPACE_LEVEL (inner)); - current_namespace = inner; +/* Returns a list of C-linkage decls with the name NAME. */ - /* Restore template parameter scopes. */ - while (save_template_parm) +tree +c_linkage_bindings (tree name) +{ + tree decls = NULL_TREE; + cxx_binding *iter; + + for (iter = IDENTIFIER_NAMESPACE_BINDINGS (name); + iter; + iter = iter->previous) + { + tree ovl; + for (ovl = iter->value; ovl; ovl = OVL_NEXT (ovl)) { - cp_binding_level *b = save_template_parm; - save_template_parm = b->level_chain; - b->level_chain = current_binding_level; - current_binding_level = b; + tree decl = OVL_CURRENT (ovl); + if (decl + && DECL_EXTERN_C_P (decl) + && !DECL_ARTIFICIAL (decl)) + { + if (decls == NULL_TREE) + decls = decl; + else + decls = tree_cons (NULL_TREE, decl, decls); + } } } - else - pushclass (inner); + return decls; } -/* Enter the scope INNER from current scope. INNER must be a scope - nested inside current scope. This works with both name lookup and - pushing name into scope. In case a template parameter scope is present, - namespace is pushed under the template parameter scope according to - name lookup rule in 14.6.1/6. - - Return the former current scope suitable for pop_inner_scope. */ +/* Insert another USING_DECL into the current binding level, returning + this declaration. If this is a redeclaration, do nothing, and + return NULL_TREE if this not in namespace scope (in namespace + scope, a using decl might extend any previous bindings). */ -tree -push_inner_scope (tree inner) +static tree +push_using_decl_1 (tree scope, tree name) { - tree outer = current_scope (); - if (!outer) - outer = current_namespace; + tree decl; - push_inner_scope_r (outer, inner); - return outer; + gcc_assert (TREE_CODE (scope) == NAMESPACE_DECL); + gcc_assert (identifier_p (name)); + for (decl = current_binding_level->usings; decl; decl = DECL_CHAIN (decl)) + if (USING_DECL_SCOPE (decl) == scope && DECL_NAME (decl) == name) + break; + if (decl) + return namespace_bindings_p () ? decl : NULL_TREE; + decl = build_lang_decl (USING_DECL, name, NULL_TREE); + USING_DECL_SCOPE (decl) = scope; + DECL_CHAIN (decl) = current_binding_level->usings; + current_binding_level->usings = decl; + return decl; } -/* Exit the current scope INNER back to scope OUTER. */ +/* Wrapper for push_using_decl_1. */ -void -pop_inner_scope (tree outer, tree inner) +static tree +push_using_decl (tree scope, tree name) { - if (outer == inner - || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner))) - return; + tree ret; + timevar_start (TV_NAME_LOOKUP); + ret = push_using_decl_1 (scope, name); + timevar_stop (TV_NAME_LOOKUP); + return ret; +} - while (outer != inner) - { - if (TREE_CODE (inner) == NAMESPACE_DECL) - { - cp_binding_level *save_template_parm = 0; - /* Temporary take out template parameter scopes. They are saved - in reversed order in save_template_parm. */ - while (current_binding_level->kind == sk_template_parms) - { - cp_binding_level *b = current_binding_level; - current_binding_level = b->level_chain; - b->level_chain = save_template_parm; - save_template_parm = b; - } +/* Same as pushdecl, but define X in binding-level LEVEL. We rely on the + caller to set DECL_CONTEXT properly. - pop_namespace (); + Note that this must only be used when X will be the new innermost + binding for its name, as we tack it onto the front of IDENTIFIER_BINDING + without checking to see if the current IDENTIFIER_BINDING comes from a + closer binding level than LEVEL. */ - /* Restore template parameter scopes. */ - while (save_template_parm) - { - cp_binding_level *b = save_template_parm; - save_template_parm = b->level_chain; - b->level_chain = current_binding_level; - current_binding_level = b; - } - } - else - popclass (); +static tree +pushdecl_with_scope_1 (tree x, cp_binding_level *level, bool is_friend) +{ + cp_binding_level *b; + tree function_decl = current_function_decl; - inner = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner)); + current_function_decl = NULL_TREE; + if (level->kind == sk_class) + { + b = class_binding_level; + class_binding_level = level; + pushdecl_class_level (x); + class_binding_level = b; + } + else + { + b = current_binding_level; + current_binding_level = level; + x = pushdecl_maybe_friend (x, is_friend); + current_binding_level = b; } + current_function_decl = function_decl; + return x; } - -/* Do a pushlevel for class declarations. */ + +/* Wrapper for pushdecl_with_scope_1. */ -void -pushlevel_class (void) +tree +pushdecl_with_scope (tree x, cp_binding_level *level, bool is_friend) { - class_binding_level = begin_scope (sk_class, current_class_type); + tree ret; + bool subtime = timevar_cond_start (TV_NAME_LOOKUP); + ret = pushdecl_with_scope_1 (x, level, is_friend); + timevar_cond_stop (TV_NAME_LOOKUP, subtime); + return ret; } -/* ...and a poplevel for class declarations. */ +/* Helper function for push_overloaded_decl_1 and do_nonmember_using_decl. + Compares the parameter-type-lists of DECL1 and DECL2 and returns false + if they are different. If the DECLs are template functions, the return + types and the template parameter lists are compared too (DR 565). */ -void -poplevel_class (void) +static bool +compparms_for_decl_and_using_decl (tree decl1, tree decl2) { - cp_binding_level *level = class_binding_level; - cp_class_binding *cb; - size_t i; - tree shadowed; - - bool subtime = timevar_cond_start (TV_NAME_LOOKUP); - gcc_assert (level != 0); - - /* If we're leaving a toplevel class, cache its binding level. */ - if (current_class_depth == 1) - previous_class_level = level; - for (shadowed = level->type_shadowed; - shadowed; - shadowed = TREE_CHAIN (shadowed)) - SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed), TREE_VALUE (shadowed)); + if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (decl1)), + TYPE_ARG_TYPES (TREE_TYPE (decl2)))) + return false; - /* Remove the bindings for all of the class-level declarations. */ - if (level->class_shadowed) - { - FOR_EACH_VEC_ELT (*level->class_shadowed, i, cb) - { - IDENTIFIER_BINDING (cb->identifier) = cb->base->previous; - cxx_binding_free (cb->base); - } - ggc_free (level->class_shadowed); - level->class_shadowed = NULL; - } + if (! DECL_FUNCTION_TEMPLATE_P (decl1) + || ! DECL_FUNCTION_TEMPLATE_P (decl2)) + return true; - /* Now, pop out of the binding level which we created up in the - `pushlevel_class' routine. */ - gcc_assert (current_binding_level == level); - leave_scope (); - timevar_cond_stop (TV_NAME_LOOKUP, subtime); + return (comp_template_parms (DECL_TEMPLATE_PARMS (decl1), + DECL_TEMPLATE_PARMS (decl2)) + && same_type_p (TREE_TYPE (TREE_TYPE (decl1)), + TREE_TYPE (TREE_TYPE (decl2)))); } -/* Set INHERITED_VALUE_BINDING_P on BINDING to true or false, as - appropriate. DECL is the value to which a name has just been - bound. CLASS_TYPE is the class in which the lookup occurred. */ +/* DECL is a FUNCTION_DECL for a non-member function, which may have + other definitions already in place. We get around this by making + the value of the identifier point to a list of all the things that + want to be referenced by that name. It is then up to the users of + that name to decide what to do with that list. -static void -set_inherited_value_binding_p (cxx_binding *binding, tree decl, - tree class_type) -{ - if (binding->value == decl && TREE_CODE (decl) != TREE_LIST) - { - tree context; + DECL may also be a TEMPLATE_DECL, with a FUNCTION_DECL in its + DECL_TEMPLATE_RESULT. It is dealt with the same way. - if (TREE_CODE (decl) == OVERLOAD) - context = ovl_scope (decl); - else - { - gcc_assert (DECL_P (decl)); - context = context_for_name_lookup (decl); - } + FLAGS is a bitwise-or of the following values: + PUSH_LOCAL: Bind DECL in the current scope, rather than at + namespace scope. + PUSH_USING: DECL is being pushed as the result of a using + declaration. - if (is_properly_derived_from (class_type, context)) - INHERITED_VALUE_BINDING_P (binding) = 1; - else - INHERITED_VALUE_BINDING_P (binding) = 0; - } - else if (binding->value == decl) - /* We only encounter a TREE_LIST when there is an ambiguity in the - base classes. Such an ambiguity can be overridden by a - definition in this class. */ - INHERITED_VALUE_BINDING_P (binding) = 1; - else - INHERITED_VALUE_BINDING_P (binding) = 0; -} + IS_FRIEND is true if this is a friend declaration. -/* Make the declaration of X appear in CLASS scope. */ + The value returned may be a previous declaration if we guessed wrong + about what language DECL should belong to (C or C++). Otherwise, + it's always DECL (and never something that's not a _DECL). */ -bool -pushdecl_class_level (tree x) +static tree +push_overloaded_decl_1 (tree decl, int flags, bool is_friend) { - tree name; - bool is_valid = true; - bool subtime; - - /* Do nothing if we're adding to an outer lambda closure type, - outer_binding will add it later if it's needed. */ - if (current_class_type != class_binding_level->this_entity) - return true; + tree name = DECL_NAME (decl); + tree old; + tree new_binding; + int doing_global = (namespace_bindings_p () || !(flags & PUSH_LOCAL)); - subtime = timevar_cond_start (TV_NAME_LOOKUP); - /* Get the name of X. */ - if (TREE_CODE (x) == OVERLOAD) - name = DECL_NAME (get_first_fn (x)); + if (doing_global) + old = namespace_binding (name, DECL_CONTEXT (decl)); else - name = DECL_NAME (x); + old = lookup_name_innermost_nonclass_level (name); - if (name) - { - is_valid = push_class_level_binding (name, x); - if (TREE_CODE (x) == TYPE_DECL) - set_identifier_type_value (name, x); - } - else if (ANON_AGGR_TYPE_P (TREE_TYPE (x))) + if (old) { - /* If X is an anonymous aggregate, all of its members are - treated as if they were members of the class containing the - aggregate, for naming purposes. */ - tree f; - - for (f = TYPE_FIELDS (TREE_TYPE (x)); f; f = DECL_CHAIN (f)) + if (TREE_CODE (old) == TYPE_DECL && DECL_ARTIFICIAL (old)) { - location_t save_location = input_location; - input_location = DECL_SOURCE_LOCATION (f); - if (!pushdecl_class_level (f)) - is_valid = false; - input_location = save_location; + tree t = TREE_TYPE (old); + if (MAYBE_CLASS_TYPE_P (t) && warn_shadow + && (! DECL_IN_SYSTEM_HEADER (decl) + || ! DECL_IN_SYSTEM_HEADER (old))) + warning (OPT_Wshadow, "%q#D hides constructor for %q#T", decl, t); + old = NULL_TREE; } - } - timevar_cond_stop (TV_NAME_LOOKUP, subtime); - return is_valid; -} - -/* Return the BINDING (if any) for NAME in SCOPE, which is a class - scope. If the value returned is non-NULL, and the PREVIOUS field - is not set, callers must set the PREVIOUS field explicitly. */ + else if (is_overloaded_fn (old)) + { + tree tmp; -static cxx_binding * -get_class_binding (tree name, cp_binding_level *scope) -{ - tree class_type; - tree type_binding; - tree value_binding; - cxx_binding *binding; + for (tmp = old; tmp; tmp = OVL_NEXT (tmp)) + { + tree fn = OVL_CURRENT (tmp); + tree dup; - class_type = scope->this_entity; + if (TREE_CODE (tmp) == OVERLOAD && OVL_USED (tmp) + && !(flags & PUSH_USING) + && compparms_for_decl_and_using_decl (fn, decl) + && ! decls_match (fn, decl)) + diagnose_name_conflict (decl, fn); - /* Get the type binding. */ - type_binding = lookup_member (class_type, name, - /*protect=*/2, /*want_type=*/true, - tf_warning_or_error); - /* Get the value binding. */ - value_binding = lookup_member (class_type, name, - /*protect=*/2, /*want_type=*/false, - tf_warning_or_error); + dup = duplicate_decls (decl, fn, is_friend); + /* If DECL was a redeclaration of FN -- even an invalid + one -- pass that information along to our caller. */ + if (dup == fn || dup == error_mark_node) + return dup; + } - if (value_binding - && (TREE_CODE (value_binding) == TYPE_DECL - || DECL_CLASS_TEMPLATE_P (value_binding) - || (TREE_CODE (value_binding) == TREE_LIST - && TREE_TYPE (value_binding) == error_mark_node - && (TREE_CODE (TREE_VALUE (value_binding)) - == TYPE_DECL)))) - /* We found a type binding, even when looking for a non-type - binding. This means that we already processed this binding - above. */ - ; - else if (value_binding) - { - if (TREE_CODE (value_binding) == TREE_LIST - && TREE_TYPE (value_binding) == error_mark_node) - /* NAME is ambiguous. */ - ; - else if (BASELINK_P (value_binding)) - /* NAME is some overloaded functions. */ - value_binding = BASELINK_FUNCTIONS (value_binding); + /* We don't overload implicit built-ins. duplicate_decls() + may fail to merge the decls if the new decl is e.g. a + template function. */ + if (TREE_CODE (old) == FUNCTION_DECL + && DECL_ANTICIPATED (old) + && !DECL_HIDDEN_FRIEND_P (old)) + old = NULL; + } + else if (old == error_mark_node) + /* Ignore the undefined symbol marker. */ + old = NULL_TREE; + else + { + error ("previous non-function declaration %q+#D", old); + error ("conflicts with function declaration %q#D", decl); + return decl; + } } - /* If we found either a type binding or a value binding, create a - new binding object. */ - if (type_binding || value_binding) + if (old || TREE_CODE (decl) == TEMPLATE_DECL + /* If it's a using declaration, we always need to build an OVERLOAD, + because it's the only way to remember that the declaration comes + from 'using', and have the lookup behave correctly. */ + || (flags & PUSH_USING)) { - binding = new_class_binding (name, - value_binding, - type_binding, - scope); - /* This is a class-scope binding, not a block-scope binding. */ - LOCAL_BINDING_P (binding) = 0; - set_inherited_value_binding_p (binding, value_binding, class_type); + if (old && TREE_CODE (old) != OVERLOAD) + /* Wrap the existing single decl in an overload. */ + new_binding = ovl_cons (old, NULL_TREE); + else + new_binding = old; + new_binding = ovl_cons (decl, new_binding); + if (flags & PUSH_USING) + OVL_USED (new_binding) = 1; } else - binding = NULL; - - return binding; -} - -/* Make the declaration(s) of X appear in CLASS scope under the name - NAME. Returns true if the binding is valid. */ - -static bool -push_class_level_binding_1 (tree name, tree x) -{ - cxx_binding *binding; - tree decl = x; - bool ok; - - /* The class_binding_level will be NULL if x is a template - parameter name in a member template. */ - if (!class_binding_level) - return true; - - if (name == error_mark_node) - return false; + /* NAME is not ambiguous. */ + new_binding = decl; - /* Can happen for an erroneous declaration (c++/60384). */ - if (!identifier_p (name)) + if (doing_global) + set_namespace_binding (name, current_namespace, new_binding); + else { - gcc_assert (errorcount || sorrycount); - return false; - } + /* We only create an OVERLOAD if there was a previous binding at + this level, or if decl is a template. In the former case, we + need to remove the old binding and replace it with the new + binding. We must also run through the NAMES on the binding + level where the name was bound to update the chain. */ - /* Check for invalid member names. But don't worry about a default - argument-scope lambda being pushed after the class is complete. */ - gcc_assert (TYPE_BEING_DEFINED (current_class_type) - || LAMBDA_TYPE_P (TREE_TYPE (decl))); - /* Check that we're pushing into the right binding level. */ - gcc_assert (current_class_type == class_binding_level->this_entity); + if (TREE_CODE (new_binding) == OVERLOAD && old) + { + tree *d; - /* We could have been passed a tree list if this is an ambiguous - declaration. If so, pull the declaration out because - check_template_shadow will not handle a TREE_LIST. */ - if (TREE_CODE (decl) == TREE_LIST - && TREE_TYPE (decl) == error_mark_node) - decl = TREE_VALUE (decl); + for (d = &IDENTIFIER_BINDING (name)->scope->names; + *d; + d = &TREE_CHAIN (*d)) + if (*d == old + || (TREE_CODE (*d) == TREE_LIST + && TREE_VALUE (*d) == old)) + { + if (TREE_CODE (*d) == TREE_LIST) + /* Just replace the old binding with the new. */ + TREE_VALUE (*d) = new_binding; + else + /* Build a TREE_LIST to wrap the OVERLOAD. */ + *d = tree_cons (NULL_TREE, new_binding, + TREE_CHAIN (*d)); - if (!check_template_shadow (decl)) - return false; + /* And update the cxx_binding node. */ + IDENTIFIER_BINDING (name)->value = new_binding; + return decl; + } - /* [class.mem] + /* We should always find a previous binding in this case. */ + gcc_unreachable (); + } - If T is the name of a class, then each of the following shall - have a name different from T: + /* Install the new binding. */ + push_local_binding (name, new_binding, flags); + } - -- every static data member of class T; + return decl; +} - -- every member of class T that is itself a type; +/* Wrapper for push_overloaded_decl_1. */ - -- every enumerator of every member of class T that is an - enumerated type; +static tree +push_overloaded_decl (tree decl, int flags, bool is_friend) +{ + tree ret; + bool subtime = timevar_cond_start (TV_NAME_LOOKUP); + ret = push_overloaded_decl_1 (decl, flags, is_friend); + timevar_cond_stop (TV_NAME_LOOKUP, subtime); + return ret; +} - -- every member of every anonymous union that is a member of - class T. +/* Check a non-member using-declaration. Return the name and scope + being used, and the USING_DECL, or NULL_TREE on failure. */ - (Non-static data members were also forbidden to have the same - name as T until TC1.) */ - if ((VAR_P (x) - || TREE_CODE (x) == CONST_DECL - || (TREE_CODE (x) == TYPE_DECL - && !DECL_SELF_REFERENCE_P (x)) - /* A data member of an anonymous union. */ - || (TREE_CODE (x) == FIELD_DECL - && DECL_CONTEXT (x) != current_class_type)) - && DECL_NAME (x) == constructor_name (current_class_type)) +static tree +validate_nonmember_using_decl (tree decl, tree scope, tree name) +{ + /* [namespace.udecl] + A using-declaration for a class member shall be a + member-declaration. */ + if (TYPE_P (scope)) { - tree scope = context_for_name_lookup (x); - if (TYPE_P (scope) && same_type_p (scope, current_class_type)) - { - error ("%qD has the same name as the class in which it is " - "declared", - x); - return false; - } + error ("%qT is not a namespace or unscoped enum", scope); + return NULL_TREE; } + else if (scope == error_mark_node) + return NULL_TREE; - /* Get the current binding for NAME in this class, if any. */ - binding = IDENTIFIER_BINDING (name); - if (!binding || binding->scope != class_binding_level) + if (TREE_CODE (decl) == TEMPLATE_ID_EXPR) { - binding = get_class_binding (name, class_binding_level); - /* If a new binding was created, put it at the front of the - IDENTIFIER_BINDING list. */ - if (binding) - { - binding->previous = IDENTIFIER_BINDING (name); - IDENTIFIER_BINDING (name) = binding; - } + /* 7.3.3/5 + A using-declaration shall not name a template-id. */ + error ("a using-declaration cannot specify a template-id. " + "Try %", name); + return NULL_TREE; } - /* If there is already a binding, then we may need to update the - current value. */ - if (binding && binding->value) + if (TREE_CODE (decl) == NAMESPACE_DECL) { - tree bval = binding->value; - tree old_decl = NULL_TREE; - tree target_decl = strip_using_decl (decl); - tree target_bval = strip_using_decl (bval); - - if (INHERITED_VALUE_BINDING_P (binding)) - { - /* If the old binding was from a base class, and was for a - tag name, slide it over to make room for the new binding. - The old binding is still visible if explicitly qualified - with a class-key. */ - if (TREE_CODE (target_bval) == TYPE_DECL - && DECL_ARTIFICIAL (target_bval) - && !(TREE_CODE (target_decl) == TYPE_DECL - && DECL_ARTIFICIAL (target_decl))) - { - old_decl = binding->type; - binding->type = bval; - binding->value = NULL_TREE; - INHERITED_VALUE_BINDING_P (binding) = 0; - } - else - { - old_decl = bval; - /* Any inherited type declaration is hidden by the type - declaration in the derived class. */ - if (TREE_CODE (target_decl) == TYPE_DECL - && DECL_ARTIFICIAL (target_decl)) - binding->type = NULL_TREE; - } - } - else if (TREE_CODE (target_decl) == OVERLOAD - && is_overloaded_fn (target_bval)) - old_decl = bval; - else if (TREE_CODE (decl) == USING_DECL - && TREE_CODE (bval) == USING_DECL - && same_type_p (USING_DECL_SCOPE (decl), - USING_DECL_SCOPE (bval))) - /* This is a using redeclaration that will be diagnosed later - in supplement_binding */ - ; - else if (TREE_CODE (decl) == USING_DECL - && TREE_CODE (bval) == USING_DECL - && DECL_DEPENDENT_P (decl) - && DECL_DEPENDENT_P (bval)) - return true; - else if (TREE_CODE (decl) == USING_DECL - && is_overloaded_fn (target_bval)) - old_decl = bval; - else if (TREE_CODE (bval) == USING_DECL - && is_overloaded_fn (target_decl)) - return true; - - if (old_decl && binding->scope == class_binding_level) - { - binding->value = x; - /* It is always safe to clear INHERITED_VALUE_BINDING_P - here. This function is only used to register bindings - from with the class definition itself. */ - INHERITED_VALUE_BINDING_P (binding) = 0; - return true; - } + error ("namespace %qD not allowed in using-declaration", decl); + return NULL_TREE; } - /* Note that we declared this value so that we can issue an error if - this is an invalid redeclaration of a name already used for some - other purpose. */ - note_name_declared_in_class (name, decl); - - /* If we didn't replace an existing binding, put the binding on the - stack of bindings for the identifier, and update the shadowed - list. */ - if (binding && binding->scope == class_binding_level) - /* Supplement the existing binding. */ - ok = supplement_binding (binding, decl); - else + if (TREE_CODE (decl) == SCOPE_REF) { - /* Create a new binding. */ - push_binding (name, decl, class_binding_level); - ok = true; + /* It's a nested name with template parameter dependent scope. + This can only be using-declaration for class member. */ + error ("%qT is not a namespace", TREE_OPERAND (decl, 0)); + return NULL_TREE; } - return ok; -} + if (is_overloaded_fn (decl)) + decl = get_first_fn (decl); + + gcc_assert (DECL_P (decl)); -/* Wrapper for push_class_level_binding_1. */ + /* Make a USING_DECL. */ + tree using_decl = push_using_decl (scope, name); -bool -push_class_level_binding (tree name, tree x) -{ - bool ret; - bool subtime = timevar_cond_start (TV_NAME_LOOKUP); - ret = push_class_level_binding_1 (name, x); - timevar_cond_stop (TV_NAME_LOOKUP, subtime); - return ret; + if (using_decl == NULL_TREE + && at_function_scope_p () + && VAR_P (decl)) + /* C++11 7.3.3/10. */ + error ("%qD is already declared in this scope", name); + + return using_decl; } -/* Process "using SCOPE::NAME" in a class scope. Return the - USING_DECL created. */ +/* Process local and global using-declarations. */ -tree -do_class_using_decl (tree scope, tree name) +static void +do_nonmember_using_decl (tree scope, tree name, tree oldval, tree oldtype, + tree *newval, tree *newtype) { - /* The USING_DECL returned by this function. */ - tree value; - /* The declaration (or declarations) name by this using - declaration. NULL if we are in a template and cannot figure out - what has been named. */ - tree decl; - /* True if SCOPE is a dependent type. */ - bool scope_dependent_p; - /* True if SCOPE::NAME is dependent. */ - bool name_dependent_p; - /* True if any of the bases of CURRENT_CLASS_TYPE are dependent. */ - bool bases_dependent_p; - tree binfo; + struct scope_binding decls = EMPTY_SCOPE_BINDING; - if (name == error_mark_node) - return NULL_TREE; + *newval = *newtype = NULL_TREE; + if (!qualified_lookup_using_namespace (name, scope, &decls, 0)) + /* Lookup error */ + return; - if (!scope || !TYPE_P (scope)) + if (!decls.value && !decls.type) { - error ("using-declaration for non-member at class scope"); - return NULL_TREE; + error ("%qD not declared", name); + return; } - /* Make sure the name is not invalid */ - if (TREE_CODE (name) == BIT_NOT_EXPR) + /* Shift the old and new bindings around so we're comparing class and + enumeration names to each other. */ + if (oldval && DECL_IMPLICIT_TYPEDEF_P (oldval)) { - error ("%<%T::%D%> names destructor", scope, name); - return NULL_TREE; + oldtype = oldval; + oldval = NULL_TREE; } - /* Using T::T declares inheriting ctors, even if T is a typedef. */ - if (MAYBE_CLASS_TYPE_P (scope) - && (name == TYPE_IDENTIFIER (scope) - || constructor_name_p (name, scope))) + + if (decls.value && DECL_IMPLICIT_TYPEDEF_P (decls.value)) { - maybe_warn_cpp0x (CPP0X_INHERITING_CTORS); - name = ctor_identifier; - CLASSTYPE_NON_AGGREGATE (current_class_type) = true; + decls.type = decls.value; + decls.value = NULL_TREE; } - if (constructor_name_p (name, current_class_type)) + + if (decls.value) { - error ("%<%T::%D%> names constructor in %qT", - scope, name, current_class_type); - return NULL_TREE; - } + /* Check for using functions. */ + if (is_overloaded_fn (decls.value)) + { + tree tmp, tmp1; - scope_dependent_p = dependent_scope_p (scope); - name_dependent_p = (scope_dependent_p - || (IDENTIFIER_TYPENAME_P (name) - && dependent_type_p (TREE_TYPE (name)))); + if (oldval && !is_overloaded_fn (oldval)) + { + error ("%qD is already declared in this scope", name); + oldval = NULL_TREE; + } - bases_dependent_p = any_dependent_bases_p (); + *newval = oldval; + for (tmp = decls.value; tmp; tmp = OVL_NEXT (tmp)) + { + tree new_fn = OVL_CURRENT (tmp); - decl = NULL_TREE; + /* Don't import functions that haven't been declared. */ + if (DECL_ANTICIPATED (new_fn)) + continue; - /* From [namespace.udecl]: + /* [namespace.udecl] - A using-declaration used as a member-declaration shall refer to a - member of a base class of the class being defined. + If a function declaration in namespace scope or block + scope has the same name and the same parameter types as a + function introduced by a using declaration the program is + ill-formed. */ + for (tmp1 = oldval; tmp1; tmp1 = OVL_NEXT (tmp1)) + { + tree old_fn = OVL_CURRENT (tmp1); - In general, we cannot check this constraint in a template because - we do not know the entire set of base classes of the current - class type. Morover, if SCOPE is dependent, it might match a - non-dependent base. */ + if (new_fn == old_fn) + /* The function already exists in the current namespace. */ + break; + else if (TREE_CODE (tmp1) == OVERLOAD && OVL_USED (tmp1)) + continue; /* this is a using decl */ + else if (compparms_for_decl_and_using_decl (new_fn, old_fn)) + { + /* There was already a non-using declaration in + this scope with the same parameter types. If both + are the same extern "C" functions, that's ok. */ + if (DECL_ANTICIPATED (old_fn) + && !DECL_HIDDEN_FRIEND_P (old_fn)) + /* Ignore anticipated built-ins. */; + else if (decls_match (new_fn, old_fn)) + break; + else + { + diagnose_name_conflict (new_fn, old_fn); + break; + } + } + } - if (!scope_dependent_p) - { - base_kind b_kind; - binfo = lookup_base (current_class_type, scope, ba_any, &b_kind, - tf_warning_or_error); - if (b_kind < bk_proper_base) - { - if (!bases_dependent_p || b_kind == bk_same_type) - { - error_not_base_type (scope, current_class_type); - return NULL_TREE; + /* If we broke out of the loop, there's no reason to add + this function to the using declarations for this + scope. */ + if (tmp1) + continue; + + /* If we are adding to an existing OVERLOAD, then we no + longer know the type of the set of functions. */ + if (*newval && TREE_CODE (*newval) == OVERLOAD) + TREE_TYPE (*newval) = unknown_type_node; + /* Add this new function to the set. */ + *newval = build_overload (OVL_CURRENT (tmp), *newval); + /* If there is only one function, then we use its type. (A + using-declaration naming a single function can be used in + contexts where overload resolution cannot be + performed.) */ + if (TREE_CODE (*newval) != OVERLOAD) + { + *newval = ovl_cons (*newval, NULL_TREE); + TREE_TYPE (*newval) = TREE_TYPE (OVL_CURRENT (tmp)); + } + OVL_USED (*newval) = 1; } } - else if (name == ctor_identifier - && BINFO_INHERITANCE_CHAIN (BINFO_INHERITANCE_CHAIN (binfo))) + else { - error ("cannot inherit constructors from indirect base %qT", scope); - return NULL_TREE; + /* If we're declaring a non-function and OLDVAL is an anticipated + built-in, just pretend it isn't there. */ + if (oldval + && TREE_CODE (oldval) == FUNCTION_DECL + && DECL_ANTICIPATED (oldval) + && !DECL_HIDDEN_FRIEND_P (oldval)) + oldval = NULL_TREE; + + *newval = decls.value; + if (oldval && !decls_match (*newval, oldval)) + error ("%qD is already declared in this scope", name); } - else if (!name_dependent_p) + } + else + *newval = oldval; + + if (decls.type && TREE_CODE (decls.type) == TREE_LIST) + { + error ("reference to %qD is ambiguous", name); + print_candidates (decls.type); + } + else + { + *newtype = decls.type; + if (oldtype && *newtype && !decls_match (oldtype, *newtype)) + error ("%qD is already declared in this scope", name); + } + + /* If *newval is empty, shift any class or enumeration name down. */ + if (!*newval) + { + *newval = *newtype; + *newtype = NULL_TREE; + } +} + +/* Process a using-declaration at function scope. */ + +void +do_local_using_decl (tree decl, tree scope, tree name) +{ + tree oldval, oldtype, newval, newtype; + tree orig_decl = decl; + + decl = validate_nonmember_using_decl (decl, scope, name); + if (decl == NULL_TREE) + return; + + if (building_stmt_list_p () + && at_function_scope_p ()) + add_decl_expr (decl); + + oldval = lookup_name_innermost_nonclass_level (name); + oldtype = lookup_type_current_level (name); + + do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype); + + if (newval) + { + if (is_overloaded_fn (newval)) { - decl = lookup_member (binfo, name, 0, false, tf_warning_or_error); - if (!decl) - { - error ("no members matching %<%T::%D%> in %q#T", scope, name, - scope); - return NULL_TREE; - } - /* The binfo from which the functions came does not matter. */ - if (BASELINK_P (decl)) - decl = BASELINK_FUNCTIONS (decl); + tree fn, term; + + /* We only need to push declarations for those functions + that were not already bound in the current level. + The old value might be NULL_TREE, it might be a single + function, or an OVERLOAD. */ + if (oldval && TREE_CODE (oldval) == OVERLOAD) + term = OVL_FUNCTION (oldval); + else + term = oldval; + for (fn = newval; fn && OVL_CURRENT (fn) != term; + fn = OVL_NEXT (fn)) + push_overloaded_decl (OVL_CURRENT (fn), + PUSH_LOCAL | PUSH_USING, + false); } + else + push_local_binding (name, newval, PUSH_USING); + } + if (newtype) + { + push_local_binding (name, newtype, PUSH_USING); + set_identifier_type_value (name, newtype); } - value = build_lang_decl (USING_DECL, name, NULL_TREE); - USING_DECL_DECLS (value) = decl; - USING_DECL_SCOPE (value) = scope; - DECL_DEPENDENT_P (value) = !decl; - - return value; + /* Emit debug info. */ + if (!processing_template_decl) + cp_emit_debug_info_for_using (orig_decl, current_scope()); } - -/* Return the binding value for name in scope. */ - +/* Returns true if ROOT (a namespace, class, or function) encloses + CHILD. CHILD may be either a class type or a namespace. */ -static tree -namespace_binding_1 (tree name, tree scope) +bool +is_ancestor (tree root, tree child) { - cxx_binding *binding; - - if (SCOPE_FILE_SCOPE_P (scope)) - scope = global_namespace; - else - /* Unnecessary for the global namespace because it can't be an alias. */ - scope = ORIGINAL_NAMESPACE (scope); - - binding = cp_binding_level_find_binding_for_name (NAMESPACE_LEVEL (scope), name); + gcc_assert ((TREE_CODE (root) == NAMESPACE_DECL + || TREE_CODE (root) == FUNCTION_DECL + || CLASS_TYPE_P (root))); + gcc_assert ((TREE_CODE (child) == NAMESPACE_DECL + || CLASS_TYPE_P (child))); - return binding ? binding->value : NULL_TREE; -} + /* The global namespace encloses everything. */ + if (root == global_namespace) + return true; -tree -namespace_binding (tree name, tree scope) -{ - tree ret; - bool subtime = timevar_cond_start (TV_NAME_LOOKUP); - ret = namespace_binding_1 (name, scope); - timevar_cond_stop (TV_NAME_LOOKUP, subtime); - return ret; + while (true) + { + /* If we've run out of scopes, stop. */ + if (!child) + return false; + /* If we've reached the ROOT, it encloses CHILD. */ + if (root == child) + return true; + /* Go out one level. */ + if (TYPE_P (child)) + child = TYPE_NAME (child); + child = DECL_CONTEXT (child); + } } -/* Set the binding value for name in scope. */ +/* Enter the class or namespace scope indicated by T suitable for name + lookup. T can be arbitrary scope, not necessary nested inside the + current scope. Returns a non-null scope to pop iff pop_scope + should be called later to exit this scope. */ -static void -set_namespace_binding_1 (tree name, tree scope, tree val) +tree +push_scope (tree t) { - cxx_binding *b; + if (TREE_CODE (t) == NAMESPACE_DECL) + push_decl_namespace (t); + else if (CLASS_TYPE_P (t)) + { + if (!at_class_scope_p () + || !same_type_p (current_class_type, t)) + push_nested_class (t); + else + /* T is the same as the current scope. There is therefore no + need to re-enter the scope. Since we are not actually + pushing a new scope, our caller should not call + pop_scope. */ + t = NULL_TREE; + } - if (scope == NULL_TREE) - scope = global_namespace; - b = binding_for_name (NAMESPACE_LEVEL (scope), name); - if (!b->value - /* For templates and using we create a single element OVERLOAD. - Look for the chain to know whether this is really augmenting - an existing overload. */ - || (TREE_CODE (val) == OVERLOAD && OVL_CHAIN (val)) - || val == error_mark_node) - b->value = val; - else - supplement_binding (b, val); + return t; } -/* Wrapper for set_namespace_binding_1. */ +/* Leave scope pushed by push_scope. */ void -set_namespace_binding (tree name, tree scope, tree val) +pop_scope (tree t) { - bool subtime = timevar_cond_start (TV_NAME_LOOKUP); - set_namespace_binding_1 (name, scope, val); - timevar_cond_stop (TV_NAME_LOOKUP, subtime); + if (t == NULL_TREE) + return; + if (TREE_CODE (t) == NAMESPACE_DECL) + pop_decl_namespace (); + else if CLASS_TYPE_P (t) + pop_nested_class (); } -/* Set the context of a declaration to scope. Complain if we are not - outside scope. */ +/* Subroutine of push_inner_scope. */ -void -set_decl_namespace (tree decl, tree scope, bool friendp) +static void +push_inner_scope_r (tree outer, tree inner) { - tree old; - - /* Get rid of namespace aliases. */ - scope = ORIGINAL_NAMESPACE (scope); - - /* It is ok for friends to be qualified in parallel space. */ - if (!friendp && !is_ancestor (current_namespace, scope)) - error ("declaration of %qD not in a namespace surrounding %qD", - decl, scope); - DECL_CONTEXT (decl) = FROB_CONTEXT (scope); - - /* Writing "int N::i" to declare a variable within "N" is invalid. */ - if (scope == current_namespace) - { - if (at_namespace_scope_p ()) - error ("explicit qualification in declaration of %qD", - decl); - return; - } + tree prev; - /* See whether this has been declared in the namespace. */ - old = lookup_qualified_name (scope, DECL_NAME (decl), /*type*/false, - /*complain*/true, /*hidden*/true); - if (old == error_mark_node) - /* No old declaration at all. */ - goto complain; - /* If it's a TREE_LIST, the result of the lookup was ambiguous. */ - if (TREE_CODE (old) == TREE_LIST) - { - error ("reference to %qD is ambiguous", decl); - print_candidates (old); - return; - } - if (!is_overloaded_fn (decl)) - { - /* We might have found OLD in an inline namespace inside SCOPE. */ - if (TREE_CODE (decl) == TREE_CODE (old)) - DECL_CONTEXT (decl) = DECL_CONTEXT (old); - /* Don't compare non-function decls with decls_match here, since - it can't check for the correct constness at this - point. pushdecl will find those errors later. */ - return; - } - /* Since decl is a function, old should contain a function decl. */ - if (!is_overloaded_fn (old)) - goto complain; - /* We handle these in check_explicit_instantiation_namespace. */ - if (processing_explicit_instantiation) - return; - if (processing_template_decl || processing_specialization) - /* We have not yet called push_template_decl to turn a - FUNCTION_DECL into a TEMPLATE_DECL, so the declarations won't - match. But, we'll check later, when we construct the - template. */ - return; - /* Instantiations or specializations of templates may be declared as - friends in any namespace. */ - if (friendp && DECL_USE_TEMPLATE (decl)) + if (outer == inner + || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner))) return; - if (is_overloaded_fn (old)) + + prev = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner)); + if (outer != prev) + push_inner_scope_r (outer, prev); + if (TREE_CODE (inner) == NAMESPACE_DECL) { - tree found = NULL_TREE; - tree elt = old; - for (; elt; elt = OVL_NEXT (elt)) + cp_binding_level *save_template_parm = 0; + /* Temporary take out template parameter scopes. They are saved + in reversed order in save_template_parm. */ + while (current_binding_level->kind == sk_template_parms) { - tree ofn = OVL_CURRENT (elt); - /* Adjust DECL_CONTEXT first so decls_match will return true - if DECL will match a declaration in an inline namespace. */ - DECL_CONTEXT (decl) = DECL_CONTEXT (ofn); - if (decls_match (decl, ofn)) - { - if (found && !decls_match (found, ofn)) - { - DECL_CONTEXT (decl) = FROB_CONTEXT (scope); - error ("reference to %qD is ambiguous", decl); - print_candidates (old); - return; - } - found = ofn; - } + cp_binding_level *b = current_binding_level; + current_binding_level = b->level_chain; + b->level_chain = save_template_parm; + save_template_parm = b; } - if (found) + + resume_scope (NAMESPACE_LEVEL (inner)); + current_namespace = inner; + + /* Restore template parameter scopes. */ + while (save_template_parm) { - if (!is_associated_namespace (scope, CP_DECL_CONTEXT (found))) - goto complain; - if (DECL_HIDDEN_FRIEND_P (found)) - { - pedwarn (DECL_SOURCE_LOCATION (decl), 0, - "%qD has not been declared within %D", decl, scope); - inform (DECL_SOURCE_LOCATION (found), "only here as a friend"); - } - DECL_CONTEXT (decl) = DECL_CONTEXT (found); - return; + cp_binding_level *b = save_template_parm; + save_template_parm = b->level_chain; + b->level_chain = current_binding_level; + current_binding_level = b; } } - else - { - DECL_CONTEXT (decl) = DECL_CONTEXT (old); - if (decls_match (decl, old)) - return; - } - - /* It didn't work, go back to the explicit scope. */ - DECL_CONTEXT (decl) = FROB_CONTEXT (scope); - complain: - error ("%qD should have been declared inside %qD", decl, scope); + else + pushclass (inner); } -/* Return the namespace where the current declaration is declared. */ +/* Enter the scope INNER from current scope. INNER must be a scope + nested inside current scope. This works with both name lookup and + pushing name into scope. In case a template parameter scope is present, + namespace is pushed under the template parameter scope according to + name lookup rule in 14.6.1/6. + + Return the former current scope suitable for pop_inner_scope. */ tree -current_decl_namespace (void) +push_inner_scope (tree inner) { - tree result; - /* If we have been pushed into a different namespace, use it. */ - if (!vec_safe_is_empty (decl_namespace_list)) - return decl_namespace_list->last (); + tree outer = current_scope (); + if (!outer) + outer = current_namespace; - if (current_class_type) - result = decl_namespace_context (current_class_type); - else if (current_function_decl) - result = decl_namespace_context (current_function_decl); - else - result = current_namespace; - return result; + push_inner_scope_r (outer, inner); + return outer; } -/* Process any ATTRIBUTES on a namespace definition. Returns true if - attribute visibility is seen. */ +/* Exit the current scope INNER back to scope OUTER. */ -bool -handle_namespace_attrs (tree ns, tree attributes) +void +pop_inner_scope (tree outer, tree inner) { - tree d; - bool saw_vis = false; + if (outer == inner + || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner))) + return; - for (d = attributes; d; d = TREE_CHAIN (d)) + while (outer != inner) { - tree name = get_attribute_name (d); - tree args = TREE_VALUE (d); - - if (is_attribute_p ("visibility", name)) + if (TREE_CODE (inner) == NAMESPACE_DECL) { - /* attribute visibility is a property of the syntactic block - rather than the namespace as a whole, so we don't touch the - NAMESPACE_DECL at all. */ - tree x = args ? TREE_VALUE (args) : NULL_TREE; - if (x == NULL_TREE || TREE_CODE (x) != STRING_CST || TREE_CHAIN (args)) + cp_binding_level *save_template_parm = 0; + /* Temporary take out template parameter scopes. They are saved + in reversed order in save_template_parm. */ + while (current_binding_level->kind == sk_template_parms) { - warning (OPT_Wattributes, - "%qD attribute requires a single NTBS argument", - name); - continue; + cp_binding_level *b = current_binding_level; + current_binding_level = b->level_chain; + b->level_chain = save_template_parm; + save_template_parm = b; } - if (!TREE_PUBLIC (ns)) - warning (OPT_Wattributes, - "%qD attribute is meaningless since members of the " - "anonymous namespace get local symbols", name); + pop_namespace (); - push_visibility (TREE_STRING_POINTER (x), 1); - saw_vis = true; - } - else if (is_attribute_p ("abi_tag", name)) - { - if (!DECL_NAMESPACE_ASSOCIATIONS (ns)) - { - warning (OPT_Wattributes, "ignoring %qD attribute on non-inline " - "namespace", name); - continue; - } - if (!DECL_NAME (ns)) - { - warning (OPT_Wattributes, "ignoring %qD attribute on anonymous " - "namespace", name); - continue; - } - if (!args) + /* Restore template parameter scopes. */ + while (save_template_parm) { - tree dn = DECL_NAME (ns); - args = build_string (IDENTIFIER_LENGTH (dn) + 1, - IDENTIFIER_POINTER (dn)); - TREE_TYPE (args) = char_array_type_node; - args = fix_string_type (args); - args = build_tree_list (NULL_TREE, args); + cp_binding_level *b = save_template_parm; + save_template_parm = b->level_chain; + b->level_chain = current_binding_level; + current_binding_level = b; } - if (check_abi_tag_args (args, name)) - DECL_ATTRIBUTES (ns) = tree_cons (name, args, - DECL_ATTRIBUTES (ns)); } else - { - warning (OPT_Wattributes, "%qD attribute directive ignored", - name); - continue; - } + popclass (); + + inner = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner)); } +} + +/* Do a pushlevel for class declarations. */ - return saw_vis; +void +pushlevel_class (void) +{ + class_binding_level = begin_scope (sk_class, current_class_type); } - -/* Push into the scope of the NAME namespace. If NAME is NULL_TREE, then we - select a name that is unique to this compilation unit. Returns FALSE if - pushdecl fails, TRUE otherwise. */ -bool -push_namespace (tree name) +/* ...and a poplevel for class declarations. */ + +void +poplevel_class (void) { - tree d = NULL_TREE; - bool need_new = true; - bool implicit_use = false; - bool anon = !name; + cp_binding_level *level = class_binding_level; + cp_class_binding *cb; + size_t i; + tree shadowed; bool subtime = timevar_cond_start (TV_NAME_LOOKUP); + gcc_assert (level != 0); - /* We should not get here if the global_namespace is not yet constructed - nor if NAME designates the global namespace: The global scope is - constructed elsewhere. */ - gcc_assert (global_namespace != NULL && name != global_scope_name); + /* If we're leaving a toplevel class, cache its binding level. */ + if (current_class_depth == 1) + previous_class_level = level; + for (shadowed = level->type_shadowed; + shadowed; + shadowed = TREE_CHAIN (shadowed)) + SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed), TREE_VALUE (shadowed)); - if (anon) - { - name = get_anonymous_namespace_name(); - d = IDENTIFIER_NAMESPACE_VALUE (name); - if (d) - /* Reopening anonymous namespace. */ - need_new = false; - implicit_use = true; - } - else + /* Remove the bindings for all of the class-level declarations. */ + if (level->class_shadowed) { - /* Check whether this is an extended namespace definition. */ - d = IDENTIFIER_NAMESPACE_VALUE (name); - if (d != NULL_TREE && TREE_CODE (d) == NAMESPACE_DECL) + FOR_EACH_VEC_ELT (*level->class_shadowed, i, cb) { - tree dna = DECL_NAMESPACE_ALIAS (d); - if (dna) - { - /* We do some error recovery for, eg, the redeclaration - of M here: - - namespace N {} - namespace M = N; - namespace M {} - - However, in nasty cases like: - - namespace N - { - namespace M = N; - namespace M {} - } - - we just error out below, in duplicate_decls. */ - if (NAMESPACE_LEVEL (dna)->level_chain - == current_binding_level) - { - error ("namespace alias %qD not allowed here, " - "assuming %qD", d, dna); - d = dna; - need_new = false; - } - } - else - need_new = false; + IDENTIFIER_BINDING (cb->identifier) = cb->base->previous; + cxx_binding_free (cb->base); } + ggc_free (level->class_shadowed); + level->class_shadowed = NULL; } - if (need_new) + /* Now, pop out of the binding level which we created up in the + `pushlevel_class' routine. */ + gcc_assert (current_binding_level == level); + leave_scope (); + timevar_cond_stop (TV_NAME_LOOKUP, subtime); +} + +/* Set INHERITED_VALUE_BINDING_P on BINDING to true or false, as + appropriate. DECL is the value to which a name has just been + bound. CLASS_TYPE is the class in which the lookup occurred. */ + +static void +set_inherited_value_binding_p (cxx_binding *binding, tree decl, + tree class_type) +{ + if (binding->value == decl && TREE_CODE (decl) != TREE_LIST) { - /* Make a new namespace, binding the name to it. */ - d = build_lang_decl (NAMESPACE_DECL, name, void_type_node); - DECL_CONTEXT (d) = FROB_CONTEXT (current_namespace); - /* The name of this namespace is not visible to other translation - units if it is an anonymous namespace or member thereof. */ - if (anon || decl_anon_ns_mem_p (current_namespace)) - TREE_PUBLIC (d) = 0; + tree context; + + if (TREE_CODE (decl) == OVERLOAD) + context = ovl_scope (decl); else - TREE_PUBLIC (d) = 1; - if (pushdecl (d) == error_mark_node) - { - timevar_cond_stop (TV_NAME_LOOKUP, subtime); - return false; - } - if (anon) { - /* Clear DECL_NAME for the benefit of debugging back ends. */ - SET_DECL_ASSEMBLER_NAME (d, name); - DECL_NAME (d) = NULL_TREE; + gcc_assert (DECL_P (decl)); + context = context_for_name_lookup (decl); } - begin_scope (sk_namespace, d); - } - else - resume_scope (NAMESPACE_LEVEL (d)); - - if (implicit_use) - do_using_directive (d); - /* Enter the name space. */ - current_namespace = d; - timevar_cond_stop (TV_NAME_LOOKUP, subtime); - return true; + if (is_properly_derived_from (class_type, context)) + INHERITED_VALUE_BINDING_P (binding) = 1; + else + INHERITED_VALUE_BINDING_P (binding) = 0; + } + else if (binding->value == decl) + /* We only encounter a TREE_LIST when there is an ambiguity in the + base classes. Such an ambiguity can be overridden by a + definition in this class. */ + INHERITED_VALUE_BINDING_P (binding) = 1; + else + INHERITED_VALUE_BINDING_P (binding) = 0; } -/* Pop from the scope of the current namespace. */ +/* Make the declaration of X appear in CLASS scope. */ -void -pop_namespace (void) +bool +pushdecl_class_level (tree x) { - gcc_assert (current_namespace != global_namespace); - current_namespace = CP_DECL_CONTEXT (current_namespace); - /* The binding level is not popped, as it might be re-opened later. */ - leave_scope (); -} + tree name; + bool is_valid = true; + bool subtime; -/* Push into the scope of the namespace NS, even if it is deeply - nested within another namespace. */ + /* Do nothing if we're adding to an outer lambda closure type, + outer_binding will add it later if it's needed. */ + if (current_class_type != class_binding_level->this_entity) + return true; -void -push_nested_namespace (tree ns) -{ - if (ns == global_namespace) - push_to_top_level (); + subtime = timevar_cond_start (TV_NAME_LOOKUP); + /* Get the name of X. */ + if (TREE_CODE (x) == OVERLOAD) + name = DECL_NAME (get_first_fn (x)); else - { - push_nested_namespace (CP_DECL_CONTEXT (ns)); - push_namespace (DECL_NAME (ns)); - } -} - -/* Pop back from the scope of the namespace NS, which was previously - entered with push_nested_namespace. */ + name = DECL_NAME (x); -void -pop_nested_namespace (tree ns) -{ - bool subtime = timevar_cond_start (TV_NAME_LOOKUP); - gcc_assert (current_namespace == ns); - while (ns != global_namespace) + if (name) { - pop_namespace (); - ns = CP_DECL_CONTEXT (ns); + is_valid = push_class_level_binding (name, x); + if (TREE_CODE (x) == TYPE_DECL) + set_identifier_type_value (name, x); } + else if (ANON_AGGR_TYPE_P (TREE_TYPE (x))) + { + /* If X is an anonymous aggregate, all of its members are + treated as if they were members of the class containing the + aggregate, for naming purposes. */ + tree f; - pop_from_top_level (); + for (f = TYPE_FIELDS (TREE_TYPE (x)); f; f = DECL_CHAIN (f)) + { + location_t save_location = input_location; + input_location = DECL_SOURCE_LOCATION (f); + if (!pushdecl_class_level (f)) + is_valid = false; + input_location = save_location; + } + } timevar_cond_stop (TV_NAME_LOOKUP, subtime); + return is_valid; } -/* Temporarily set the namespace for the current declaration. */ +/* Return the BINDING (if any) for NAME in SCOPE, which is a class + scope. If the value returned is non-NULL, and the PREVIOUS field + is not set, callers must set the PREVIOUS field explicitly. */ -void -push_decl_namespace (tree decl) +static cxx_binding * +get_class_binding (tree name, cp_binding_level *scope) { - if (TREE_CODE (decl) != NAMESPACE_DECL) - decl = decl_namespace_context (decl); - vec_safe_push (decl_namespace_list, ORIGINAL_NAMESPACE (decl)); -} + tree class_type; + tree type_binding; + tree value_binding; + cxx_binding *binding; -/* [namespace.memdef]/2 */ + class_type = scope->this_entity; -void -pop_decl_namespace (void) -{ - decl_namespace_list->pop (); -} + /* Get the type binding. */ + type_binding = lookup_member (class_type, name, + /*protect=*/2, /*want_type=*/true, + tf_warning_or_error); + /* Get the value binding. */ + value_binding = lookup_member (class_type, name, + /*protect=*/2, /*want_type=*/false, + tf_warning_or_error); -/* Return the namespace that is the common ancestor - of two given namespaces. */ + if (value_binding + && (TREE_CODE (value_binding) == TYPE_DECL + || DECL_CLASS_TEMPLATE_P (value_binding) + || (TREE_CODE (value_binding) == TREE_LIST + && TREE_TYPE (value_binding) == error_mark_node + && (TREE_CODE (TREE_VALUE (value_binding)) + == TYPE_DECL)))) + /* We found a type binding, even when looking for a non-type + binding. This means that we already processed this binding + above. */ + ; + else if (value_binding) + { + if (TREE_CODE (value_binding) == TREE_LIST + && TREE_TYPE (value_binding) == error_mark_node) + /* NAME is ambiguous. */ + ; + else if (BASELINK_P (value_binding)) + /* NAME is some overloaded functions. */ + value_binding = BASELINK_FUNCTIONS (value_binding); + } -static tree -namespace_ancestor_1 (tree ns1, tree ns2) -{ - tree nsr; - if (is_ancestor (ns1, ns2)) - nsr = ns1; + /* If we found either a type binding or a value binding, create a + new binding object. */ + if (type_binding || value_binding) + { + binding = new_class_binding (name, + value_binding, + type_binding, + scope); + /* This is a class-scope binding, not a block-scope binding. */ + LOCAL_BINDING_P (binding) = 0; + set_inherited_value_binding_p (binding, value_binding, class_type); + } else - nsr = namespace_ancestor_1 (CP_DECL_CONTEXT (ns1), ns2); - return nsr; + binding = NULL; + + return binding; } -/* Wrapper for namespace_ancestor_1. */ +/* Make the declaration(s) of X appear in CLASS scope under the name + NAME. Returns true if the binding is valid. */ -static tree -namespace_ancestor (tree ns1, tree ns2) +static bool +push_class_level_binding_1 (tree name, tree x) { - tree nsr; - bool subtime = timevar_cond_start (TV_NAME_LOOKUP); - nsr = namespace_ancestor_1 (ns1, ns2); - timevar_cond_stop (TV_NAME_LOOKUP, subtime); - return nsr; -} + cxx_binding *binding; + tree decl = x; + bool ok; -/* Process a namespace-alias declaration. */ + /* The class_binding_level will be NULL if x is a template + parameter name in a member template. */ + if (!class_binding_level) + return true; -void -do_namespace_alias (tree alias, tree name_space) -{ - if (name_space == error_mark_node) - return; + if (name == error_mark_node) + return false; - gcc_assert (TREE_CODE (name_space) == NAMESPACE_DECL); + /* Can happen for an erroneous declaration (c++/60384). */ + if (!identifier_p (name)) + { + gcc_assert (errorcount || sorrycount); + return false; + } - name_space = ORIGINAL_NAMESPACE (name_space); + /* Check for invalid member names. But don't worry about a default + argument-scope lambda being pushed after the class is complete. */ + gcc_assert (TYPE_BEING_DEFINED (current_class_type) + || LAMBDA_TYPE_P (TREE_TYPE (decl))); + /* Check that we're pushing into the right binding level. */ + gcc_assert (current_class_type == class_binding_level->this_entity); - /* Build the alias. */ - alias = build_lang_decl (NAMESPACE_DECL, alias, void_type_node); - DECL_NAMESPACE_ALIAS (alias) = name_space; - DECL_EXTERNAL (alias) = 1; - DECL_CONTEXT (alias) = FROB_CONTEXT (current_scope ()); - pushdecl (alias); + /* We could have been passed a tree list if this is an ambiguous + declaration. If so, pull the declaration out because + check_template_shadow will not handle a TREE_LIST. */ + if (TREE_CODE (decl) == TREE_LIST + && TREE_TYPE (decl) == error_mark_node) + decl = TREE_VALUE (decl); - /* Emit debug info for namespace alias. */ - if (!building_stmt_list_p ()) - (*debug_hooks->early_global_decl) (alias); -} + if (!check_template_shadow (decl)) + return false; -/* Like pushdecl, only it places X in the current namespace, - if appropriate. */ + /* [class.mem] -tree -pushdecl_namespace_level (tree x, bool is_friend) -{ - cp_binding_level *b = current_binding_level; - tree t; + If T is the name of a class, then each of the following shall + have a name different from T: - bool subtime = timevar_cond_start (TV_NAME_LOOKUP); - t = pushdecl_with_scope (x, NAMESPACE_LEVEL (current_namespace), is_friend); + -- every static data member of class T; - /* Now, the type_shadowed stack may screw us. Munge it so it does - what we want. */ - if (TREE_CODE (t) == TYPE_DECL) + -- every member of class T that is itself a type; + + -- every enumerator of every member of class T that is an + enumerated type; + + -- every member of every anonymous union that is a member of + class T. + + (Non-static data members were also forbidden to have the same + name as T until TC1.) */ + if ((VAR_P (x) + || TREE_CODE (x) == CONST_DECL + || (TREE_CODE (x) == TYPE_DECL + && !DECL_SELF_REFERENCE_P (x)) + /* A data member of an anonymous union. */ + || (TREE_CODE (x) == FIELD_DECL + && DECL_CONTEXT (x) != current_class_type)) + && DECL_NAME (x) == constructor_name (current_class_type)) + { + tree scope = context_for_name_lookup (x); + if (TYPE_P (scope) && same_type_p (scope, current_class_type)) + { + error ("%qD has the same name as the class in which it is " + "declared", + x); + return false; + } + } + + /* Get the current binding for NAME in this class, if any. */ + binding = IDENTIFIER_BINDING (name); + if (!binding || binding->scope != class_binding_level) { - tree name = DECL_NAME (t); - tree newval; - tree *ptr = (tree *)0; - for (; !global_scope_p (b); b = b->level_chain) + binding = get_class_binding (name, class_binding_level); + /* If a new binding was created, put it at the front of the + IDENTIFIER_BINDING list. */ + if (binding) { - tree shadowed = b->type_shadowed; - for (; shadowed; shadowed = TREE_CHAIN (shadowed)) - if (TREE_PURPOSE (shadowed) == name) - { - ptr = &TREE_VALUE (shadowed); - /* Can't break out of the loop here because sometimes - a binding level will have duplicate bindings for - PT names. It's gross, but I haven't time to fix it. */ - } + binding->previous = IDENTIFIER_BINDING (name); + IDENTIFIER_BINDING (name) = binding; } - newval = TREE_TYPE (t); - if (ptr == (tree *)0) + } + + /* If there is already a binding, then we may need to update the + current value. */ + if (binding && binding->value) + { + tree bval = binding->value; + tree old_decl = NULL_TREE; + tree target_decl = strip_using_decl (decl); + tree target_bval = strip_using_decl (bval); + + if (INHERITED_VALUE_BINDING_P (binding)) { - /* @@ This shouldn't be needed. My test case "zstring.cc" trips - up here if this is changed to an assertion. --KR */ - SET_IDENTIFIER_TYPE_VALUE (name, t); + /* If the old binding was from a base class, and was for a + tag name, slide it over to make room for the new binding. + The old binding is still visible if explicitly qualified + with a class-key. */ + if (TREE_CODE (target_bval) == TYPE_DECL + && DECL_ARTIFICIAL (target_bval) + && !(TREE_CODE (target_decl) == TYPE_DECL + && DECL_ARTIFICIAL (target_decl))) + { + old_decl = binding->type; + binding->type = bval; + binding->value = NULL_TREE; + INHERITED_VALUE_BINDING_P (binding) = 0; + } + else + { + old_decl = bval; + /* Any inherited type declaration is hidden by the type + declaration in the derived class. */ + if (TREE_CODE (target_decl) == TYPE_DECL + && DECL_ARTIFICIAL (target_decl)) + binding->type = NULL_TREE; + } } - else + else if (TREE_CODE (target_decl) == OVERLOAD + && is_overloaded_fn (target_bval)) + old_decl = bval; + else if (TREE_CODE (decl) == USING_DECL + && TREE_CODE (bval) == USING_DECL + && same_type_p (USING_DECL_SCOPE (decl), + USING_DECL_SCOPE (bval))) + /* This is a using redeclaration that will be diagnosed later + in supplement_binding */ + ; + else if (TREE_CODE (decl) == USING_DECL + && TREE_CODE (bval) == USING_DECL + && DECL_DEPENDENT_P (decl) + && DECL_DEPENDENT_P (bval)) + return true; + else if (TREE_CODE (decl) == USING_DECL + && is_overloaded_fn (target_bval)) + old_decl = bval; + else if (TREE_CODE (bval) == USING_DECL + && is_overloaded_fn (target_decl)) + return true; + + if (old_decl && binding->scope == class_binding_level) { - *ptr = newval; + binding->value = x; + /* It is always safe to clear INHERITED_VALUE_BINDING_P + here. This function is only used to register bindings + from with the class definition itself. */ + INHERITED_VALUE_BINDING_P (binding) = 0; + return true; } } - timevar_cond_stop (TV_NAME_LOOKUP, subtime); - return t; -} -/* Insert USED into the using list of USER. Set INDIRECT_flag if this - directive is not directly from the source. Also find the common - ancestor and let our users know about the new namespace */ + /* Note that we declared this value so that we can issue an error if + this is an invalid redeclaration of a name already used for some + other purpose. */ + note_name_declared_in_class (name, decl); -static void -add_using_namespace_1 (tree user, tree used, bool indirect) -{ - tree t; - /* Using oneself is a no-op. */ - if (user == used) - return; - gcc_assert (TREE_CODE (user) == NAMESPACE_DECL); - gcc_assert (TREE_CODE (used) == NAMESPACE_DECL); - /* Check if we already have this. */ - t = purpose_member (used, DECL_NAMESPACE_USING (user)); - if (t != NULL_TREE) + /* If we didn't replace an existing binding, put the binding on the + stack of bindings for the identifier, and update the shadowed + list. */ + if (binding && binding->scope == class_binding_level) + /* Supplement the existing binding. */ + ok = supplement_binding (binding, decl); + else { - if (!indirect) - /* Promote to direct usage. */ - TREE_INDIRECT_USING (t) = 0; - return; + /* Create a new binding. */ + push_binding (name, decl, class_binding_level); + ok = true; } - /* Add used to the user's using list. */ - DECL_NAMESPACE_USING (user) - = tree_cons (used, namespace_ancestor (user, used), - DECL_NAMESPACE_USING (user)); - - TREE_INDIRECT_USING (DECL_NAMESPACE_USING (user)) = indirect; - - /* Add user to the used's users list. */ - DECL_NAMESPACE_USERS (used) - = tree_cons (user, 0, DECL_NAMESPACE_USERS (used)); - - /* Recursively add all namespaces used. */ - for (t = DECL_NAMESPACE_USING (used); t; t = TREE_CHAIN (t)) - /* indirect usage */ - add_using_namespace_1 (user, TREE_PURPOSE (t), 1); - - /* Tell everyone using us about the new used namespaces. */ - for (t = DECL_NAMESPACE_USERS (user); t; t = TREE_CHAIN (t)) - add_using_namespace_1 (TREE_PURPOSE (t), used, 1); + return ok; } -/* Wrapper for add_using_namespace_1. */ +/* Wrapper for push_class_level_binding_1. */ -static void -add_using_namespace (tree user, tree used, bool indirect) +bool +push_class_level_binding (tree name, tree x) { + bool ret; bool subtime = timevar_cond_start (TV_NAME_LOOKUP); - add_using_namespace_1 (user, used, indirect); + ret = push_class_level_binding_1 (name, x); timevar_cond_stop (TV_NAME_LOOKUP, subtime); + return ret; } -/* Process a using-declaration not appearing in class or local scope. */ - -void -do_toplevel_using_decl (tree decl, tree scope, tree name) -{ - tree oldval, oldtype, newval, newtype; - tree orig_decl = decl; - cxx_binding *binding; - - decl = validate_nonmember_using_decl (decl, scope, name); - if (decl == NULL_TREE) - return; - - binding = binding_for_name (NAMESPACE_LEVEL (current_namespace), name); - - oldval = binding->value; - oldtype = binding->type; - - do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype); - - /* Emit debug info. */ - if (!processing_template_decl) - cp_emit_debug_info_for_using (orig_decl, current_namespace); - - /* Copy declarations found. */ - if (newval) - binding->value = newval; - if (newtype) - binding->type = newtype; -} - -/* Process a using-directive. */ +/* Process "using SCOPE::NAME" in a class scope. Return the + USING_DECL created. */ -void -do_using_directive (tree name_space) +tree +do_class_using_decl (tree scope, tree name) { - tree context = NULL_TREE; - - if (name_space == error_mark_node) - return; + /* The USING_DECL returned by this function. */ + tree value; + /* The declaration (or declarations) name by this using + declaration. NULL if we are in a template and cannot figure out + what has been named. */ + tree decl; + /* True if SCOPE is a dependent type. */ + bool scope_dependent_p; + /* True if SCOPE::NAME is dependent. */ + bool name_dependent_p; + /* True if any of the bases of CURRENT_CLASS_TYPE are dependent. */ + bool bases_dependent_p; + tree binfo; - gcc_assert (TREE_CODE (name_space) == NAMESPACE_DECL); + if (name == error_mark_node) + return NULL_TREE; - if (building_stmt_list_p ()) - add_stmt (build_stmt (input_location, USING_STMT, name_space)); - name_space = ORIGINAL_NAMESPACE (name_space); + if (!scope || !TYPE_P (scope)) + { + error ("using-declaration for non-member at class scope"); + return NULL_TREE; + } - if (!toplevel_bindings_p ()) + /* Make sure the name is not invalid */ + if (TREE_CODE (name) == BIT_NOT_EXPR) { - push_using_directive (name_space); + error ("%<%T::%D%> names destructor", scope, name); + return NULL_TREE; } - else + /* Using T::T declares inheriting ctors, even if T is a typedef. */ + if (MAYBE_CLASS_TYPE_P (scope) + && (name == TYPE_IDENTIFIER (scope) + || constructor_name_p (name, scope))) { - /* direct usage */ - add_using_namespace (current_namespace, name_space, 0); - if (current_namespace != global_namespace) - context = current_namespace; - - /* Emit debugging info. */ - if (!processing_template_decl) - (*debug_hooks->imported_module_or_decl) (name_space, NULL_TREE, - context, false); + maybe_warn_cpp0x (CPP0X_INHERITING_CTORS); + name = ctor_identifier; + CLASSTYPE_NON_AGGREGATE (current_class_type) = true; } -} + if (constructor_name_p (name, current_class_type)) + { + error ("%<%T::%D%> names constructor in %qT", + scope, name, current_class_type); + return NULL_TREE; + } + + scope_dependent_p = dependent_scope_p (scope); + name_dependent_p = (scope_dependent_p + || (IDENTIFIER_TYPENAME_P (name) + && dependent_type_p (TREE_TYPE (name)))); -/* Deal with a using-directive seen by the parser. Currently we only - handle attributes here, since they cannot appear inside a template. */ + bases_dependent_p = any_dependent_bases_p (); -void -parse_using_directive (tree name_space, tree attribs) -{ - do_using_directive (name_space); + decl = NULL_TREE; - if (attribs == error_mark_node) - return; + /* From [namespace.udecl]: - for (tree a = attribs; a; a = TREE_CHAIN (a)) + A using-declaration used as a member-declaration shall refer to a + member of a base class of the class being defined. + + In general, we cannot check this constraint in a template because + we do not know the entire set of base classes of the current + class type. Morover, if SCOPE is dependent, it might match a + non-dependent base. */ + + if (!scope_dependent_p) { - tree name = get_attribute_name (a); - if (is_attribute_p ("strong", name)) + base_kind b_kind; + binfo = lookup_base (current_class_type, scope, ba_any, &b_kind, + tf_warning_or_error); + if (b_kind < bk_proper_base) { - warning (OPT_Wdeprecated, "strong using is deprecated; use inline " - "namespaces instead"); - if (!toplevel_bindings_p ()) - error ("strong using only meaningful at namespace scope"); - else if (name_space != error_mark_node) + if (!bases_dependent_p || b_kind == bk_same_type) { - if (!is_ancestor (current_namespace, name_space)) - error ("current namespace %qD does not enclose strongly used namespace %qD", - current_namespace, name_space); - DECL_NAMESPACE_ASSOCIATIONS (name_space) - = tree_cons (current_namespace, 0, - DECL_NAMESPACE_ASSOCIATIONS (name_space)); + error_not_base_type (scope, current_class_type); + return NULL_TREE; } } - else - warning (OPT_Wattributes, "%qD attribute directive ignored", name); + else if (name == ctor_identifier + && BINFO_INHERITANCE_CHAIN (BINFO_INHERITANCE_CHAIN (binfo))) + { + error ("cannot inherit constructors from indirect base %qT", scope); + return NULL_TREE; + } + else if (!name_dependent_p) + { + decl = lookup_member (binfo, name, 0, false, tf_warning_or_error); + if (!decl) + { + error ("no members matching %<%T::%D%> in %q#T", scope, name, + scope); + return NULL_TREE; + } + /* The binfo from which the functions came does not matter. */ + if (BASELINK_P (decl)) + decl = BASELINK_FUNCTIONS (decl); + } } -} -/* Like pushdecl, only it places X in the global scope if appropriate. - Calls cp_finish_decl to register the variable, initializing it with - *INIT, if INIT is non-NULL. */ + value = build_lang_decl (USING_DECL, name, NULL_TREE); + USING_DECL_DECLS (value) = decl; + USING_DECL_SCOPE (value) = scope; + DECL_DEPENDENT_P (value) = !decl; -static tree -pushdecl_top_level_1 (tree x, tree *init, bool is_friend) -{ - bool subtime = timevar_cond_start (TV_NAME_LOOKUP); - push_to_top_level (); - x = pushdecl_namespace_level (x, is_friend); - if (init) - cp_finish_decl (x, *init, false, NULL_TREE, 0); - pop_from_top_level (); - timevar_cond_stop (TV_NAME_LOOKUP, subtime); - return x; + return value; } -/* Like pushdecl, only it places X in the global scope if appropriate. */ + +/* Return the binding value for name in scope. */ -tree -pushdecl_top_level (tree x) + +static tree +namespace_binding_1 (tree name, tree scope) { - return pushdecl_top_level_1 (x, NULL, false); -} + cxx_binding *binding; -/* Like pushdecl_top_level, but adding the IS_FRIEND parameter. */ + if (SCOPE_FILE_SCOPE_P (scope)) + scope = global_namespace; + else + /* Unnecessary for the global namespace because it can't be an alias. */ + scope = ORIGINAL_NAMESPACE (scope); -tree -pushdecl_top_level_maybe_friend (tree x, bool is_friend) -{ - return pushdecl_top_level_1 (x, NULL, is_friend); -} + binding = cp_binding_level_find_binding_for_name (NAMESPACE_LEVEL (scope), name); -/* Like pushdecl, only it places X in the global scope if - appropriate. Calls cp_finish_decl to register the variable, - initializing it with INIT. */ + return binding ? binding->value : NULL_TREE; +} tree -pushdecl_top_level_and_finish (tree x, tree init) +namespace_binding (tree name, tree scope) { - return pushdecl_top_level_1 (x, &init, false); + tree ret; + bool subtime = timevar_cond_start (TV_NAME_LOOKUP); + ret = namespace_binding_1 (name, scope); + timevar_cond_stop (TV_NAME_LOOKUP, subtime); + return ret; } -/* Combines two sets of overloaded functions into an OVERLOAD chain, removing - duplicates. The first list becomes the tail of the result. - - The algorithm is O(n^2). We could get this down to O(n log n) by - doing a sort on the addresses of the functions, if that becomes - necessary. */ +/* Set the binding value for name in scope. */ -static tree -merge_functions (tree s1, tree s2) +static void +set_namespace_binding_1 (tree name, tree scope, tree val) { - for (; s2; s2 = OVL_NEXT (s2)) - { - tree fn2 = OVL_CURRENT (s2); - tree fns1; - - for (fns1 = s1; fns1; fns1 = OVL_NEXT (fns1)) - { - tree fn1 = OVL_CURRENT (fns1); - - /* If the function from S2 is already in S1, there is no - need to add it again. For `extern "C"' functions, we - might have two FUNCTION_DECLs for the same function, in - different namespaces, but let's leave them in case - they have different default arguments. */ - if (fn1 == fn2) - break; - } + cxx_binding *b; - /* If we exhausted all of the functions in S1, FN2 is new. */ - if (!fns1) - s1 = build_overload (fn2, s1); - } - return s1; + if (scope == NULL_TREE) + scope = global_namespace; + b = binding_for_name (NAMESPACE_LEVEL (scope), name); + if (!b->value + /* For templates and using we create a single element OVERLOAD. + Look for the chain to know whether this is really augmenting + an existing overload. */ + || (TREE_CODE (val) == OVERLOAD && OVL_CHAIN (val)) + || val == error_mark_node) + b->value = val; + else + supplement_binding (b, val); } -/* Returns TRUE iff OLD and NEW are the same entity. - - 3 [basic]/3: An entity is a value, object, reference, function, - enumerator, type, class member, template, template specialization, - namespace, parameter pack, or this. - - 7.3.4 [namespace.udir]/4: If name lookup finds a declaration for a name - in two different namespaces, and the declarations do not declare the - same entity and do not declare functions, the use of the name is - ill-formed. */ +/* Wrapper for set_namespace_binding_1. */ -static bool -same_entity_p (tree one, tree two) +void +set_namespace_binding (tree name, tree scope, tree val) { - if (one == two) - return true; - if (!one || !two) - return false; - if (TREE_CODE (one) == TYPE_DECL - && TREE_CODE (two) == TYPE_DECL - && same_type_p (TREE_TYPE (one), TREE_TYPE (two))) - return true; - return false; + bool subtime = timevar_cond_start (TV_NAME_LOOKUP); + set_namespace_binding_1 (name, scope, val); + timevar_cond_stop (TV_NAME_LOOKUP, subtime); } -/* This should return an error not all definitions define functions. - It is not an error if we find two functions with exactly the - same signature, only if these are selected in overload resolution. - old is the current set of bindings, new_binding the freshly-found binding. - XXX Do we want to give *all* candidates in case of ambiguity? - XXX In what way should I treat extern declarations? - XXX I don't want to repeat the entire duplicate_decls here */ +/* Set the context of a declaration to scope. Complain if we are not + outside scope. */ -static void -ambiguous_decl (struct scope_binding *old, cxx_binding *new_binding, int flags) +void +set_decl_namespace (tree decl, tree scope, bool friendp) { - tree val, type; - gcc_assert (old != NULL); + tree old; - /* Copy the type. */ - type = new_binding->type; - if (LOOKUP_NAMESPACES_ONLY (flags) - || (type && hidden_name_p (type) && !(flags & LOOKUP_HIDDEN))) - type = NULL_TREE; + /* Get rid of namespace aliases. */ + scope = ORIGINAL_NAMESPACE (scope); - /* Copy the value. */ - val = new_binding->value; - if (val) - { - if (!(flags & LOOKUP_HIDDEN)) - val = remove_hidden_names (val); - if (val) - switch (TREE_CODE (val)) - { - case TEMPLATE_DECL: - /* If we expect types or namespaces, and not templates, - or this is not a template class. */ - if ((LOOKUP_QUALIFIERS_ONLY (flags) - && !DECL_TYPE_TEMPLATE_P (val))) - val = NULL_TREE; - break; - case TYPE_DECL: - if (LOOKUP_NAMESPACES_ONLY (flags) - || (type && (flags & LOOKUP_PREFER_TYPES))) - val = NULL_TREE; - break; - case NAMESPACE_DECL: - if (LOOKUP_TYPES_ONLY (flags)) - val = NULL_TREE; - break; - case FUNCTION_DECL: - /* Ignore built-in functions that are still anticipated. */ - if (LOOKUP_QUALIFIERS_ONLY (flags)) - val = NULL_TREE; - break; - default: - if (LOOKUP_QUALIFIERS_ONLY (flags)) - val = NULL_TREE; - } - } + /* It is ok for friends to be qualified in parallel space. */ + if (!friendp && !is_ancestor (current_namespace, scope)) + error ("declaration of %qD not in a namespace surrounding %qD", + decl, scope); + DECL_CONTEXT (decl) = FROB_CONTEXT (scope); - /* If val is hidden, shift down any class or enumeration name. */ - if (!val) + /* Writing "int N::i" to declare a variable within "N" is invalid. */ + if (scope == current_namespace) { - val = type; - type = NULL_TREE; + if (at_namespace_scope_p ()) + error ("explicit qualification in declaration of %qD", + decl); + return; } - if (!old->value) - old->value = val; - else if (val && !same_entity_p (val, old->value)) + /* See whether this has been declared in the namespace. */ + old = lookup_qualified_name (scope, DECL_NAME (decl), /*type*/false, + /*complain*/true, /*hidden*/true); + if (old == error_mark_node) + /* No old declaration at all. */ + goto complain; + /* If it's a TREE_LIST, the result of the lookup was ambiguous. */ + if (TREE_CODE (old) == TREE_LIST) { - if (is_overloaded_fn (old->value) && is_overloaded_fn (val)) - old->value = merge_functions (old->value, val); - else + error ("reference to %qD is ambiguous", decl); + print_candidates (old); + return; + } + if (!is_overloaded_fn (decl)) + { + /* We might have found OLD in an inline namespace inside SCOPE. */ + if (TREE_CODE (decl) == TREE_CODE (old)) + DECL_CONTEXT (decl) = DECL_CONTEXT (old); + /* Don't compare non-function decls with decls_match here, since + it can't check for the correct constness at this + point. pushdecl will find those errors later. */ + return; + } + /* Since decl is a function, old should contain a function decl. */ + if (!is_overloaded_fn (old)) + goto complain; + /* We handle these in check_explicit_instantiation_namespace. */ + if (processing_explicit_instantiation) + return; + if (processing_template_decl || processing_specialization) + /* We have not yet called push_template_decl to turn a + FUNCTION_DECL into a TEMPLATE_DECL, so the declarations won't + match. But, we'll check later, when we construct the + template. */ + return; + /* Instantiations or specializations of templates may be declared as + friends in any namespace. */ + if (friendp && DECL_USE_TEMPLATE (decl)) + return; + if (is_overloaded_fn (old)) + { + tree found = NULL_TREE; + tree elt = old; + for (; elt; elt = OVL_NEXT (elt)) { - old->value = tree_cons (NULL_TREE, old->value, - build_tree_list (NULL_TREE, val)); - TREE_TYPE (old->value) = error_mark_node; + tree ofn = OVL_CURRENT (elt); + /* Adjust DECL_CONTEXT first so decls_match will return true + if DECL will match a declaration in an inline namespace. */ + DECL_CONTEXT (decl) = DECL_CONTEXT (ofn); + if (decls_match (decl, ofn)) + { + if (found && !decls_match (found, ofn)) + { + DECL_CONTEXT (decl) = FROB_CONTEXT (scope); + error ("reference to %qD is ambiguous", decl); + print_candidates (old); + return; + } + found = ofn; + } + } + if (found) + { + if (!is_associated_namespace (scope, CP_DECL_CONTEXT (found))) + goto complain; + if (DECL_HIDDEN_FRIEND_P (found)) + { + pedwarn (DECL_SOURCE_LOCATION (decl), 0, + "%qD has not been declared within %D", decl, scope); + inform (DECL_SOURCE_LOCATION (found), "only here as a friend"); + } + DECL_CONTEXT (decl) = DECL_CONTEXT (found); + return; } } - - if (!old->type) - old->type = type; - else if (type && old->type != type) + else { - old->type = tree_cons (NULL_TREE, old->type, - build_tree_list (NULL_TREE, type)); - TREE_TYPE (old->type) = error_mark_node; + DECL_CONTEXT (decl) = DECL_CONTEXT (old); + if (decls_match (decl, old)) + return; } + + /* It didn't work, go back to the explicit scope. */ + DECL_CONTEXT (decl) = FROB_CONTEXT (scope); + complain: + error ("%qD should have been declared inside %qD", decl, scope); } -/* Return the declarations that are members of the namespace NS. */ +/* Return the namespace where the current declaration is declared. */ tree -cp_namespace_decls (tree ns) +current_decl_namespace (void) { - return NAMESPACE_LEVEL (ns)->names; -} - -/* Combine prefer_type and namespaces_only into flags. */ + tree result; + /* If we have been pushed into a different namespace, use it. */ + if (!vec_safe_is_empty (decl_namespace_list)) + return decl_namespace_list->last (); -static int -lookup_flags (int prefer_type, int namespaces_only) -{ - if (namespaces_only) - return LOOKUP_PREFER_NAMESPACES; - if (prefer_type > 1) - return LOOKUP_PREFER_TYPES; - if (prefer_type > 0) - return LOOKUP_PREFER_BOTH; - return 0; + if (current_class_type) + result = decl_namespace_context (current_class_type); + else if (current_function_decl) + result = decl_namespace_context (current_function_decl); + else + result = current_namespace; + return result; } -/* Given a lookup that returned VAL, use FLAGS to decide if we want to - ignore it or not. Subroutine of lookup_name_real and - lookup_type_scope. */ +/* Process any ATTRIBUTES on a namespace definition. Returns true if + attribute visibility is seen. */ -static bool -qualify_lookup (tree val, int flags) +bool +handle_namespace_attrs (tree ns, tree attributes) { - if (val == NULL_TREE) - return false; - if ((flags & LOOKUP_PREFER_NAMESPACES) && TREE_CODE (val) == NAMESPACE_DECL) - return true; - if (flags & LOOKUP_PREFER_TYPES) + tree d; + bool saw_vis = false; + + for (d = attributes; d; d = TREE_CHAIN (d)) { - tree target_val = strip_using_decl (val); - if (TREE_CODE (target_val) == TYPE_DECL - || TREE_CODE (target_val) == TEMPLATE_DECL) - return true; + tree name = get_attribute_name (d); + tree args = TREE_VALUE (d); + + if (is_attribute_p ("visibility", name)) + { + /* attribute visibility is a property of the syntactic block + rather than the namespace as a whole, so we don't touch the + NAMESPACE_DECL at all. */ + tree x = args ? TREE_VALUE (args) : NULL_TREE; + if (x == NULL_TREE || TREE_CODE (x) != STRING_CST || TREE_CHAIN (args)) + { + warning (OPT_Wattributes, + "%qD attribute requires a single NTBS argument", + name); + continue; + } + + if (!TREE_PUBLIC (ns)) + warning (OPT_Wattributes, + "%qD attribute is meaningless since members of the " + "anonymous namespace get local symbols", name); + + push_visibility (TREE_STRING_POINTER (x), 1); + saw_vis = true; + } + else if (is_attribute_p ("abi_tag", name)) + { + if (!DECL_NAMESPACE_ASSOCIATIONS (ns)) + { + warning (OPT_Wattributes, "ignoring %qD attribute on non-inline " + "namespace", name); + continue; + } + if (!DECL_NAME (ns)) + { + warning (OPT_Wattributes, "ignoring %qD attribute on anonymous " + "namespace", name); + continue; + } + if (!args) + { + tree dn = DECL_NAME (ns); + args = build_string (IDENTIFIER_LENGTH (dn) + 1, + IDENTIFIER_POINTER (dn)); + TREE_TYPE (args) = char_array_type_node; + args = fix_string_type (args); + args = build_tree_list (NULL_TREE, args); + } + if (check_abi_tag_args (args, name)) + DECL_ATTRIBUTES (ns) = tree_cons (name, args, + DECL_ATTRIBUTES (ns)); + } + else + { + warning (OPT_Wattributes, "%qD attribute directive ignored", + name); + continue; + } } - if (flags & (LOOKUP_PREFER_NAMESPACES | LOOKUP_PREFER_TYPES)) - return false; - /* Look through lambda things that we shouldn't be able to see. */ - if (is_lambda_ignored_entity (val)) - return false; - return true; -} -/* Given a lookup that returned VAL, decide if we want to ignore it or - not based on DECL_ANTICIPATED. */ + return saw_vis; +} +/* Temporarily set the namespace for the current declaration. */ -bool -hidden_name_p (tree val) +void +push_decl_namespace (tree decl) { - if (DECL_P (val) - && DECL_LANG_SPECIFIC (val) - && TYPE_FUNCTION_OR_TEMPLATE_DECL_P (val) - && DECL_ANTICIPATED (val)) - return true; - if (TREE_CODE (val) == OVERLOAD) - { - for (tree o = val; o; o = OVL_CHAIN (o)) - if (!hidden_name_p (OVL_FUNCTION (o))) - return false; - return true; - } - return false; + if (TREE_CODE (decl) != NAMESPACE_DECL) + decl = decl_namespace_context (decl); + vec_safe_push (decl_namespace_list, ORIGINAL_NAMESPACE (decl)); } -/* Remove any hidden declarations from a possibly overloaded set - of functions. */ +/* [namespace.memdef]/2 */ -tree -remove_hidden_names (tree fns) +void +pop_decl_namespace (void) { - if (!fns) - return fns; + decl_namespace_list->pop (); +} - if (DECL_P (fns) && hidden_name_p (fns)) - fns = NULL_TREE; - else if (TREE_CODE (fns) == OVERLOAD) - { - tree o; +/* Return the namespace that is the common ancestor + of two given namespaces. */ - for (o = fns; o; o = OVL_NEXT (o)) - if (hidden_name_p (OVL_CURRENT (o))) - break; - if (o) - { - tree n = NULL_TREE; +static tree +namespace_ancestor_1 (tree ns1, tree ns2) +{ + tree nsr; + if (is_ancestor (ns1, ns2)) + nsr = ns1; + else + nsr = namespace_ancestor_1 (CP_DECL_CONTEXT (ns1), ns2); + return nsr; +} - for (o = fns; o; o = OVL_NEXT (o)) - if (!hidden_name_p (OVL_CURRENT (o))) - n = build_overload (OVL_CURRENT (o), n); - fns = n; - } - } +/* Wrapper for namespace_ancestor_1. */ - return fns; +static tree +namespace_ancestor (tree ns1, tree ns2) +{ + tree nsr; + bool subtime = timevar_cond_start (TV_NAME_LOOKUP); + nsr = namespace_ancestor_1 (ns1, ns2); + timevar_cond_stop (TV_NAME_LOOKUP, subtime); + return nsr; } -/* Suggest alternatives for NAME, an IDENTIFIER_NODE for which name - lookup failed. Search through all available namespaces and print out - possible candidates. If no exact matches are found, and - SUGGEST_MISSPELLINGS is true, then also look for near-matches and - suggest the best near-match, if there is one. */ +/* Process a namespace-alias declaration. */ void -suggest_alternatives_for (location_t location, tree name, - bool suggest_misspellings) +do_namespace_alias (tree alias, tree name_space) { - vec candidates = vNULL; - vec namespaces_to_search = vNULL; - int max_to_search = PARAM_VALUE (CXX_MAX_NAMESPACES_FOR_DIAGNOSTIC_HELP); - int n_searched = 0; - tree t; - unsigned ix; - - namespaces_to_search.safe_push (global_namespace); + if (name_space == error_mark_node) + return; - while (!namespaces_to_search.is_empty () - && n_searched < max_to_search) - { - tree scope = namespaces_to_search.pop (); - struct scope_binding binding = EMPTY_SCOPE_BINDING; - cp_binding_level *level = NAMESPACE_LEVEL (scope); + gcc_assert (TREE_CODE (name_space) == NAMESPACE_DECL); - /* Look in this namespace. */ - qualified_lookup_using_namespace (name, scope, &binding, 0); + name_space = ORIGINAL_NAMESPACE (name_space); - n_searched++; + /* Build the alias. */ + alias = build_lang_decl (NAMESPACE_DECL, alias, void_type_node); + DECL_NAMESPACE_ALIAS (alias) = name_space; + DECL_EXTERNAL (alias) = 1; + DECL_CONTEXT (alias) = FROB_CONTEXT (current_scope ()); + pushdecl (alias); - if (binding.value) - candidates.safe_push (binding.value); + /* Emit debug info for namespace alias. */ + if (!building_stmt_list_p ()) + (*debug_hooks->early_global_decl) (alias); +} - /* Add child namespaces. */ - for (t = level->namespaces; t; t = DECL_CHAIN (t)) - namespaces_to_search.safe_push (t); - } +/* Like pushdecl, only it places X in the current namespace, + if appropriate. */ - /* If we stopped before we could examine all namespaces, inform the - user. Do this even if we don't have any candidates, since there - might be more candidates further down that we weren't able to - find. */ - if (n_searched >= max_to_search - && !namespaces_to_search.is_empty ()) - inform (location, - "maximum limit of %d namespaces searched for %qE", - max_to_search, name); +tree +pushdecl_namespace_level (tree x, bool is_friend) +{ + cp_binding_level *b = current_binding_level; + tree t; - namespaces_to_search.release (); + bool subtime = timevar_cond_start (TV_NAME_LOOKUP); + t = pushdecl_with_scope (x, NAMESPACE_LEVEL (current_namespace), is_friend); - /* Nothing useful to report for NAME. Report on likely misspellings, - or do nothing. */ - if (candidates.is_empty ()) + /* Now, the type_shadowed stack may screw us. Munge it so it does + what we want. */ + if (TREE_CODE (t) == TYPE_DECL) { - if (suggest_misspellings) + tree name = DECL_NAME (t); + tree newval; + tree *ptr = (tree *)0; + for (; !global_scope_p (b); b = b->level_chain) { - const char *fuzzy_name = lookup_name_fuzzy (name, FUZZY_LOOKUP_NAME); - if (fuzzy_name) - { - gcc_rich_location richloc (location); - richloc.add_fixit_replace (fuzzy_name); - inform_at_rich_loc (&richloc, "suggested alternative: %qs", - fuzzy_name); - } + tree shadowed = b->type_shadowed; + for (; shadowed; shadowed = TREE_CHAIN (shadowed)) + if (TREE_PURPOSE (shadowed) == name) + { + ptr = &TREE_VALUE (shadowed); + /* Can't break out of the loop here because sometimes + a binding level will have duplicate bindings for + PT names. It's gross, but I haven't time to fix it. */ + } + } + newval = TREE_TYPE (t); + if (ptr == (tree *)0) + { + /* @@ This shouldn't be needed. My test case "zstring.cc" trips + up here if this is changed to an assertion. --KR */ + SET_IDENTIFIER_TYPE_VALUE (name, t); + } + else + { + *ptr = newval; } - return; } - - inform_n (location, candidates.length (), - "suggested alternative:", - "suggested alternatives:"); - - FOR_EACH_VEC_ELT (candidates, ix, t) - inform (location_of (t), " %qE", t); - - candidates.release (); + timevar_cond_stop (TV_NAME_LOOKUP, subtime); + return t; } -/* Subroutine of maybe_suggest_missing_header for handling unrecognized names - for some of the most common names within "std::". - Given non-NULL NAME, a name for lookup within "std::", return the header - name defining it within the C++ Standard Library (without '<' and '>'), - or NULL. */ +/* Insert USED into the using list of USER. Set INDIRECT_flag if this + directive is not directly from the source. Also find the common + ancestor and let our users know about the new namespace */ -static const char * -get_std_name_hint (const char *name) +static void +add_using_namespace_1 (tree user, tree used, bool indirect) { - struct std_name_hint - { - const char *name; - const char *header; - }; - static const std_name_hint hints[] = { - /* . */ - {"array", "array"}, // C++11 - /* . */ - {"deque", "deque"}, - /* . */ - {"forward_list", "forward_list"}, // C++11 - /* . */ - {"basic_filebuf", "fstream"}, - {"basic_ifstream", "fstream"}, - {"basic_ofstream", "fstream"}, - {"basic_fstream", "fstream"}, - /* . */ - {"cin", "iostream"}, - {"cout", "iostream"}, - {"cerr", "iostream"}, - {"clog", "iostream"}, - {"wcin", "iostream"}, - {"wcout", "iostream"}, - {"wclog", "iostream"}, - /* . */ - {"list", "list"}, - /* . */ - {"map", "map"}, - {"multimap", "map"}, - /* . */ - {"queue", "queue"}, - {"priority_queue", "queue"}, - /* . */ - {"ostream", "ostream"}, - {"wostream", "ostream"}, - {"ends", "ostream"}, - {"flush", "ostream"}, - {"endl", "ostream"}, - /* . */ - {"set", "set"}, - {"multiset", "set"}, - /* . */ - {"basic_stringbuf", "sstream"}, - {"basic_istringstream", "sstream"}, - {"basic_ostringstream", "sstream"}, - {"basic_stringstream", "sstream"}, - /* . */ - {"stack", "stack"}, - /* . */ - {"string", "string"}, - {"wstring", "string"}, - {"u16string", "string"}, - {"u32string", "string"}, - /* . */ - {"unordered_map", "unordered_map"}, // C++11 - {"unordered_multimap", "unordered_map"}, // C++11 - /* . */ - {"unordered_set", "unordered_set"}, // C++11 - {"unordered_multiset", "unordered_set"}, // C++11 - /* . */ - {"vector", "vector"}, - }; - const size_t num_hints = sizeof (hints) / sizeof (hints[0]); - for (size_t i = 0; i < num_hints; i++) + tree t; + /* Using oneself is a no-op. */ + if (user == used) + return; + gcc_assert (TREE_CODE (user) == NAMESPACE_DECL); + gcc_assert (TREE_CODE (used) == NAMESPACE_DECL); + /* Check if we already have this. */ + t = purpose_member (used, DECL_NAMESPACE_USING (user)); + if (t != NULL_TREE) { - if (0 == strcmp (name, hints[i].name)) - return hints[i].header; + if (!indirect) + /* Promote to direct usage. */ + TREE_INDIRECT_USING (t) = 0; + return; } - return NULL; + + /* Add used to the user's using list. */ + DECL_NAMESPACE_USING (user) + = tree_cons (used, namespace_ancestor (user, used), + DECL_NAMESPACE_USING (user)); + + TREE_INDIRECT_USING (DECL_NAMESPACE_USING (user)) = indirect; + + /* Add user to the used's users list. */ + DECL_NAMESPACE_USERS (used) + = tree_cons (user, 0, DECL_NAMESPACE_USERS (used)); + + /* Recursively add all namespaces used. */ + for (t = DECL_NAMESPACE_USING (used); t; t = TREE_CHAIN (t)) + /* indirect usage */ + add_using_namespace_1 (user, TREE_PURPOSE (t), 1); + + /* Tell everyone using us about the new used namespaces. */ + for (t = DECL_NAMESPACE_USERS (user); t; t = TREE_CHAIN (t)) + add_using_namespace_1 (TREE_PURPOSE (t), used, 1); } -/* Subroutine of suggest_alternative_in_explicit_scope, for use when we have no - suggestions to offer. - If SCOPE is the "std" namespace, then suggest pertinent header - files for NAME. */ +/* Wrapper for add_using_namespace_1. */ static void -maybe_suggest_missing_header (location_t location, tree name, tree scope) +add_using_namespace (tree user, tree used, bool indirect) { - if (scope == NULL_TREE) - return; - if (TREE_CODE (scope) != NAMESPACE_DECL) - return; - /* We only offer suggestions for the "std" namespace. */ - if (scope != std_node) - return; - gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE); - - const char *name_str = IDENTIFIER_POINTER (name); - const char *header_hint = get_std_name_hint (name_str); - if (header_hint) - inform (location, - "% is defined in header %<<%s>%>;" - " did you forget to %<#include <%s>%>?", - name_str, header_hint, header_hint); + bool subtime = timevar_cond_start (TV_NAME_LOOKUP); + add_using_namespace_1 (user, used, indirect); + timevar_cond_stop (TV_NAME_LOOKUP, subtime); } -/* Look for alternatives for NAME, an IDENTIFIER_NODE for which name - lookup failed within the explicitly provided SCOPE. Suggest the - the best meaningful candidates (if any) as a fix-it hint. - Return true iff a suggestion was provided. */ +/* Process a using-declaration not appearing in class or local scope. */ -bool -suggest_alternative_in_explicit_scope (location_t location, tree name, - tree scope) +void +do_toplevel_using_decl (tree decl, tree scope, tree name) { - /* Resolve any namespace aliases. */ - scope = ORIGINAL_NAMESPACE (scope); + tree oldval, oldtype, newval, newtype; + tree orig_decl = decl; + cxx_binding *binding; - cp_binding_level *level = NAMESPACE_LEVEL (scope); + decl = validate_nonmember_using_decl (decl, scope, name); + if (decl == NULL_TREE) + return; - best_match bm (name); - consider_binding_level (name, bm, level, false, FUZZY_LOOKUP_NAME); + binding = binding_for_name (NAMESPACE_LEVEL (current_namespace), name); - /* See if we have a good suggesion for the user. */ - const char *fuzzy_name = bm.get_best_meaningful_candidate (); - if (fuzzy_name) - { - gcc_rich_location richloc (location); - richloc.add_fixit_replace (fuzzy_name); - inform_at_rich_loc (&richloc, "suggested alternative: %qs", - fuzzy_name); - return true; - } - else - maybe_suggest_missing_header (location, name, scope); + oldval = binding->value; + oldtype = binding->type; - return false; + do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype); + + /* Emit debug info. */ + if (!processing_template_decl) + cp_emit_debug_info_for_using (orig_decl, current_namespace); + + /* Copy declarations found. */ + if (newval) + binding->value = newval; + if (newtype) + binding->type = newtype; } -/* Unscoped lookup of a global: iterate over current namespaces, - considering using-directives. */ +/* Process a using-directive. */ -static tree -unqualified_namespace_lookup_1 (tree name, int flags) +void +do_using_directive (tree name_space) { - tree initial = current_decl_namespace (); - tree scope = initial; - tree siter; - cp_binding_level *level; - tree val = NULL_TREE; + tree context = NULL_TREE; - for (; !val; scope = CP_DECL_CONTEXT (scope)) - { - struct scope_binding binding = EMPTY_SCOPE_BINDING; - cxx_binding *b = - cp_binding_level_find_binding_for_name (NAMESPACE_LEVEL (scope), name); + if (name_space == error_mark_node) + return; - if (b) - ambiguous_decl (&binding, b, flags); + gcc_assert (TREE_CODE (name_space) == NAMESPACE_DECL); - /* Add all _DECLs seen through local using-directives. */ - for (level = current_binding_level; - level->kind != sk_namespace; - level = level->level_chain) - if (!lookup_using_namespace (name, &binding, level->using_directives, - scope, flags)) - /* Give up because of error. */ - return error_mark_node; + if (building_stmt_list_p ()) + add_stmt (build_stmt (input_location, USING_STMT, name_space)); + name_space = ORIGINAL_NAMESPACE (name_space); - /* Add all _DECLs seen through global using-directives. */ - /* XXX local and global using lists should work equally. */ - siter = initial; - while (1) - { - if (!lookup_using_namespace (name, &binding, - DECL_NAMESPACE_USING (siter), - scope, flags)) - /* Give up because of error. */ - return error_mark_node; - if (siter == scope) break; - siter = CP_DECL_CONTEXT (siter); - } + if (!toplevel_bindings_p ()) + { + push_using_directive (name_space); + } + else + { + /* direct usage */ + add_using_namespace (current_namespace, name_space, 0); + if (current_namespace != global_namespace) + context = current_namespace; - val = binding.value; - if (scope == global_namespace) - break; + /* Emit debugging info. */ + if (!processing_template_decl) + (*debug_hooks->imported_module_or_decl) (name_space, NULL_TREE, + context, false); } - return val; } -/* Wrapper for unqualified_namespace_lookup_1. */ +/* Deal with a using-directive seen by the parser. Currently we only + handle attributes here, since they cannot appear inside a template. */ -static tree -unqualified_namespace_lookup (tree name, int flags) +void +parse_using_directive (tree name_space, tree attribs) { - tree ret; - bool subtime = timevar_cond_start (TV_NAME_LOOKUP); - ret = unqualified_namespace_lookup_1 (name, flags); - timevar_cond_stop (TV_NAME_LOOKUP, subtime); - return ret; -} - -/* Look up NAME (an IDENTIFIER_NODE) in SCOPE (either a NAMESPACE_DECL - or a class TYPE). - - If PREFER_TYPE is > 0, we only return TYPE_DECLs or namespaces. - If PREFER_TYPE is > 1, we only return TYPE_DECLs. - - Returns a DECL (or OVERLOAD, or BASELINK) representing the - declaration found. If no suitable declaration can be found, - ERROR_MARK_NODE is returned. If COMPLAIN is true and SCOPE is - neither a class-type nor a namespace a diagnostic is issued. */ + do_using_directive (name_space); -tree -lookup_qualified_name (tree scope, tree name, int prefer_type, bool complain, - bool find_hidden) -{ - tree t = NULL_TREE; + if (attribs == error_mark_node) + return; - if (TREE_CODE (scope) == NAMESPACE_DECL) + for (tree a = attribs; a; a = TREE_CHAIN (a)) { - struct scope_binding binding = EMPTY_SCOPE_BINDING; - - int flags = lookup_flags (prefer_type, /*namespaces_only*/false); - if (find_hidden) - flags |= LOOKUP_HIDDEN; - if (qualified_lookup_using_namespace (name, scope, &binding, flags)) - t = binding.value; + tree name = get_attribute_name (a); + if (is_attribute_p ("strong", name)) + { + warning (OPT_Wdeprecated, "strong using is deprecated; use inline " + "namespaces instead"); + if (!toplevel_bindings_p ()) + error ("strong using only meaningful at namespace scope"); + else if (name_space != error_mark_node) + { + if (!is_ancestor (current_namespace, name_space)) + error ("current namespace %qD does not enclose strongly used namespace %qD", + current_namespace, name_space); + DECL_NAMESPACE_ASSOCIATIONS (name_space) + = tree_cons (current_namespace, 0, + DECL_NAMESPACE_ASSOCIATIONS (name_space)); + } + } + else + warning (OPT_Wattributes, "%qD attribute directive ignored", name); } - else if (cxx_dialect != cxx98 && TREE_CODE (scope) == ENUMERAL_TYPE) - t = lookup_enumerator (scope, name); - else if (is_class_type (scope, complain)) - t = lookup_member (scope, name, 2, prefer_type, tf_warning_or_error); - - if (!t) - return error_mark_node; - return t; } -/* Subroutine of unqualified_namespace_lookup: - Add the bindings of NAME in used namespaces to VAL. - We are currently looking for names in namespace SCOPE, so we - look through USINGS for using-directives of namespaces - which have SCOPE as a common ancestor with the current scope. - Returns false on errors. */ +/* Like pushdecl, only it places X in the global scope if appropriate. + Calls cp_finish_decl to register the variable, initializing it with + *INIT, if INIT is non-NULL. */ -static bool -lookup_using_namespace (tree name, struct scope_binding *val, - tree usings, tree scope, int flags) +static tree +pushdecl_top_level_1 (tree x, tree *init, bool is_friend) { - tree iter; bool subtime = timevar_cond_start (TV_NAME_LOOKUP); - /* Iterate over all used namespaces in current, searching for using - directives of scope. */ - for (iter = usings; iter; iter = TREE_CHAIN (iter)) - if (TREE_VALUE (iter) == scope) - { - tree used = ORIGINAL_NAMESPACE (TREE_PURPOSE (iter)); - cxx_binding *val1 = - cp_binding_level_find_binding_for_name (NAMESPACE_LEVEL (used), name); - /* Resolve ambiguities. */ - if (val1) - ambiguous_decl (val, val1, flags); - } + push_to_top_level (); + x = pushdecl_namespace_level (x, is_friend); + if (init) + cp_finish_decl (x, *init, false, NULL_TREE, 0); + pop_from_top_level (); timevar_cond_stop (TV_NAME_LOOKUP, subtime); - return val->value != error_mark_node; + return x; +} + +/* Like pushdecl, only it places X in the global scope if appropriate. */ + +tree +pushdecl_top_level (tree x) +{ + return pushdecl_top_level_1 (x, NULL, false); } -/* Returns true iff VEC contains TARGET. */ +/* Like pushdecl_top_level, but adding the IS_FRIEND parameter. */ -static bool -tree_vec_contains (vec *vec, tree target) +tree +pushdecl_top_level_maybe_friend (tree x, bool is_friend) { - unsigned int i; - tree elt; - FOR_EACH_VEC_SAFE_ELT (vec,i,elt) - if (elt == target) - return true; - return false; + return pushdecl_top_level_1 (x, NULL, is_friend); } -/* [namespace.qual] - Accepts the NAME to lookup and its qualifying SCOPE. - Returns the name/type pair found into the cxx_binding *RESULT, - or false on error. */ +/* Like pushdecl, only it places X in the global scope if + appropriate. Calls cp_finish_decl to register the variable, + initializing it with INIT. */ -static bool -qualified_lookup_using_namespace (tree name, tree scope, - struct scope_binding *result, int flags) +tree +pushdecl_top_level_and_finish (tree x, tree init) { - /* Maintain a list of namespaces visited... */ - vec *seen = NULL; - vec *seen_inline = NULL; - /* ... and a list of namespace yet to see. */ - vec *todo = NULL; - vec *todo_maybe = NULL; - vec *todo_inline = NULL; - tree usings; - timevar_start (TV_NAME_LOOKUP); - /* Look through namespace aliases. */ - scope = ORIGINAL_NAMESPACE (scope); + return pushdecl_top_level_1 (x, &init, false); +} - query_oracle (name); +/* Combines two sets of overloaded functions into an OVERLOAD chain, removing + duplicates. The first list becomes the tail of the result. - /* Algorithm: Starting with SCOPE, walk through the set of used - namespaces. For each used namespace, look through its inline - namespace set for any bindings and usings. If no bindings are - found, add any usings seen to the set of used namespaces. */ - vec_safe_push (todo, scope); + The algorithm is O(n^2). We could get this down to O(n log n) by + doing a sort on the addresses of the functions, if that becomes + necessary. */ - while (todo->length ()) +static tree +merge_functions (tree s1, tree s2) +{ + for (; s2; s2 = OVL_NEXT (s2)) { - bool found_here; - scope = todo->pop (); - if (tree_vec_contains (seen, scope)) - continue; - vec_safe_push (seen, scope); - vec_safe_push (todo_inline, scope); + tree fn2 = OVL_CURRENT (s2); + tree fns1; - found_here = false; - while (todo_inline->length ()) + for (fns1 = s1; fns1; fns1 = OVL_NEXT (fns1)) { - cxx_binding *binding; - - scope = todo_inline->pop (); - if (tree_vec_contains (seen_inline, scope)) - continue; - vec_safe_push (seen_inline, scope); - - binding = - cp_binding_level_find_binding_for_name (NAMESPACE_LEVEL (scope), name); - if (binding) - { - ambiguous_decl (result, binding, flags); - if (result->type || result->value) - found_here = true; - } + tree fn1 = OVL_CURRENT (fns1); - for (usings = DECL_NAMESPACE_USING (scope); usings; - usings = TREE_CHAIN (usings)) - if (!TREE_INDIRECT_USING (usings)) - { - if (is_associated_namespace (scope, TREE_PURPOSE (usings))) - vec_safe_push (todo_inline, TREE_PURPOSE (usings)); - else - vec_safe_push (todo_maybe, TREE_PURPOSE (usings)); - } + /* If the function from S2 is already in S1, there is no + need to add it again. For `extern "C"' functions, we + might have two FUNCTION_DECLs for the same function, in + different namespaces, but let's leave them in case + they have different default arguments. */ + if (fn1 == fn2) + break; } - if (found_here) - vec_safe_truncate (todo_maybe, 0); - else - while (vec_safe_length (todo_maybe)) - vec_safe_push (todo, todo_maybe->pop ()); + /* If we exhausted all of the functions in S1, FN2 is new. */ + if (!fns1) + s1 = build_overload (fn2, s1); } - vec_free (todo); - vec_free (todo_maybe); - vec_free (todo_inline); - vec_free (seen); - vec_free (seen_inline); - timevar_stop (TV_NAME_LOOKUP); - return result->value != error_mark_node; + return s1; } -/* Helper function for lookup_name_fuzzy. - Traverse binding level LVL, looking for good name matches for NAME - (and BM). */ -static void -consider_binding_level (tree name, best_match &bm, - cp_binding_level *lvl, bool look_within_fields, - enum lookup_name_fuzzy_kind kind) -{ - if (look_within_fields) - if (lvl->this_entity && TREE_CODE (lvl->this_entity) == RECORD_TYPE) - { - tree type = lvl->this_entity; - bool want_type_p = (kind == FUZZY_LOOKUP_TYPENAME); - tree best_matching_field - = lookup_member_fuzzy (type, name, want_type_p); - if (best_matching_field) - bm.consider (IDENTIFIER_POINTER (best_matching_field)); - } - - for (tree t = lvl->names; t; t = TREE_CHAIN (t)) - { - tree d = t; - - /* OVERLOADs or decls from using declaration are wrapped into - TREE_LIST. */ - if (TREE_CODE (d) == TREE_LIST) - { - d = TREE_VALUE (d); - d = OVL_CURRENT (d); - } +/* Returns TRUE iff OLD and NEW are the same entity. - /* Don't use bindings from implicitly declared functions, - as they were likely misspellings themselves. */ - if (TREE_TYPE (d) == error_mark_node) - continue; + 3 [basic]/3: An entity is a value, object, reference, function, + enumerator, type, class member, template, template specialization, + namespace, parameter pack, or this. - /* Skip anticipated decls of builtin functions. */ - if (TREE_CODE (d) == FUNCTION_DECL - && DECL_BUILT_IN (d) - && DECL_ANTICIPATED (d)) - continue; + 7.3.4 [namespace.udir]/4: If name lookup finds a declaration for a name + in two different namespaces, and the declarations do not declare the + same entity and do not declare functions, the use of the name is + ill-formed. */ - if (tree name = DECL_NAME (d)) - /* Ignore internal names with spaces in them. */ - if (!strchr (IDENTIFIER_POINTER (name), ' ')) - bm.consider (IDENTIFIER_POINTER (name)); - } +static bool +same_entity_p (tree one, tree two) +{ + if (one == two) + return true; + if (!one || !two) + return false; + if (TREE_CODE (one) == TYPE_DECL + && TREE_CODE (two) == TYPE_DECL + && same_type_p (TREE_TYPE (one), TREE_TYPE (two))) + return true; + return false; } -/* Search for near-matches for NAME within the current bindings, and within - macro names, returning the best match as a const char *, or NULL if - no reasonable match is found. */ +/* This should return an error not all definitions define functions. + It is not an error if we find two functions with exactly the + same signature, only if these are selected in overload resolution. + old is the current set of bindings, new_binding the freshly-found binding. + XXX Do we want to give *all* candidates in case of ambiguity? + XXX In what way should I treat extern declarations? + XXX I don't want to repeat the entire duplicate_decls here */ -const char * -lookup_name_fuzzy (tree name, enum lookup_name_fuzzy_kind kind) +static void +ambiguous_decl (struct scope_binding *old, cxx_binding *new_binding, int flags) { - gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE); - - best_match bm (name); - - cp_binding_level *lvl; - for (lvl = scope_chain->class_bindings; lvl; lvl = lvl->level_chain) - consider_binding_level (name, bm, lvl, true, kind); - - for (lvl = current_binding_level; lvl; lvl = lvl->level_chain) - consider_binding_level (name, bm, lvl, false, kind); + tree val, type; + gcc_assert (old != NULL); - /* Consider macros: if the user misspelled a macro name e.g. "SOME_MACRO" - as: - x = SOME_OTHER_MACRO (y); - then "SOME_OTHER_MACRO" will survive to the frontend and show up - as a misspelled identifier. + /* Copy the type. */ + type = new_binding->type; + if (LOOKUP_NAMESPACES_ONLY (flags) + || (type && hidden_name_p (type) && !(flags & LOOKUP_HIDDEN))) + type = NULL_TREE; - Use the best distance so far so that a candidate is only set if - a macro is better than anything so far. This allows early rejection - (without calculating the edit distance) of macro names that must have - distance >= bm.get_best_distance (), and means that we only get a - non-NULL result for best_macro_match if it's better than any of - the identifiers already checked. */ - best_macro_match bmm (name, bm.get_best_distance (), parse_in); - cpp_hashnode *best_macro = bmm.get_best_meaningful_candidate (); - /* If a macro is the closest so far to NAME, consider it. */ - if (best_macro) - bm.consider ((const char *)best_macro->ident.str); + /* Copy the value. */ + val = new_binding->value; + if (val) + { + if (!(flags & LOOKUP_HIDDEN)) + val = remove_hidden_names (val); + if (val) + switch (TREE_CODE (val)) + { + case TEMPLATE_DECL: + /* If we expect types or namespaces, and not templates, + or this is not a template class. */ + if ((LOOKUP_QUALIFIERS_ONLY (flags) + && !DECL_TYPE_TEMPLATE_P (val))) + val = NULL_TREE; + break; + case TYPE_DECL: + if (LOOKUP_NAMESPACES_ONLY (flags) + || (type && (flags & LOOKUP_PREFER_TYPES))) + val = NULL_TREE; + break; + case NAMESPACE_DECL: + if (LOOKUP_TYPES_ONLY (flags)) + val = NULL_TREE; + break; + case FUNCTION_DECL: + /* Ignore built-in functions that are still anticipated. */ + if (LOOKUP_QUALIFIERS_ONLY (flags)) + val = NULL_TREE; + break; + default: + if (LOOKUP_QUALIFIERS_ONLY (flags)) + val = NULL_TREE; + } + } - /* Try the "starts_decl_specifier_p" keywords to detect - "singed" vs "signed" typos. */ - for (unsigned i = 0; i < num_c_common_reswords; i++) + /* If val is hidden, shift down any class or enumeration name. */ + if (!val) { - const c_common_resword *resword = &c_common_reswords[i]; - - if (kind == FUZZY_LOOKUP_TYPENAME) - if (!cp_keyword_starts_decl_specifier_p (resword->rid)) - continue; - - tree resword_identifier = ridpointers [resword->rid]; - if (!resword_identifier) - continue; - gcc_assert (TREE_CODE (resword_identifier) == IDENTIFIER_NODE); + val = type; + type = NULL_TREE; + } - /* Only consider reserved words that survived the - filtering in init_reswords (e.g. for -std). */ - if (!C_IS_RESERVED_WORD (resword_identifier)) - continue; + if (!old->value) + old->value = val; + else if (val && !same_entity_p (val, old->value)) + { + if (is_overloaded_fn (old->value) && is_overloaded_fn (val)) + old->value = merge_functions (old->value, val); + else + { + old->value = tree_cons (NULL_TREE, old->value, + build_tree_list (NULL_TREE, val)); + TREE_TYPE (old->value) = error_mark_node; + } + } - bm.consider (IDENTIFIER_POINTER (resword_identifier)); + if (!old->type) + old->type = type; + else if (type && old->type != type) + { + old->type = tree_cons (NULL_TREE, old->type, + build_tree_list (NULL_TREE, type)); + TREE_TYPE (old->type) = error_mark_node; } +} - return bm.get_best_meaningful_candidate (); +/* Return the declarations that are members of the namespace NS. */ + +tree +cp_namespace_decls (tree ns) +{ + return NAMESPACE_LEVEL (ns)->names; } -/* Subroutine of outer_binding. +/* Combine prefer_type and namespaces_only into flags. */ - Returns TRUE if BINDING is a binding to a template parameter of - SCOPE. In that case SCOPE is the scope of a primary template - parameter -- in the sense of G++, i.e, a template that has its own - template header. +static int +lookup_flags (int prefer_type, int namespaces_only) +{ + if (namespaces_only) + return LOOKUP_PREFER_NAMESPACES; + if (prefer_type > 1) + return LOOKUP_PREFER_TYPES; + if (prefer_type > 0) + return LOOKUP_PREFER_BOTH; + return 0; +} - Returns FALSE otherwise. */ +/* Given a lookup that returned VAL, use FLAGS to decide if we want to + ignore it or not. Subroutine of lookup_name_real and + lookup_type_scope. */ static bool -binding_to_template_parms_of_scope_p (cxx_binding *binding, - cp_binding_level *scope) +qualify_lookup (tree val, int flags) { - tree binding_value, tmpl, tinfo; - int level; - - if (!binding || !scope || !scope->this_entity) + if (val == NULL_TREE) return false; - - binding_value = binding->value ? binding->value : binding->type; - tinfo = get_template_info (scope->this_entity); - - /* BINDING_VALUE must be a template parm. */ - if (binding_value == NULL_TREE - || (!DECL_P (binding_value) - || !DECL_TEMPLATE_PARM_P (binding_value))) + if ((flags & LOOKUP_PREFER_NAMESPACES) && TREE_CODE (val) == NAMESPACE_DECL) + return true; + if (flags & LOOKUP_PREFER_TYPES) + { + tree target_val = strip_using_decl (val); + if (TREE_CODE (target_val) == TYPE_DECL + || TREE_CODE (target_val) == TEMPLATE_DECL) + return true; + } + if (flags & (LOOKUP_PREFER_NAMESPACES | LOOKUP_PREFER_TYPES)) return false; + /* Look through lambda things that we shouldn't be able to see. */ + if (is_lambda_ignored_entity (val)) + return false; + return true; +} - /* The level of BINDING_VALUE. */ - level = - template_type_parameter_p (binding_value) - ? TEMPLATE_PARM_LEVEL (TEMPLATE_TYPE_PARM_INDEX - (TREE_TYPE (binding_value))) - : TEMPLATE_PARM_LEVEL (DECL_INITIAL (binding_value)); - - /* The template of the current scope, iff said scope is a primary - template. */ - tmpl = (tinfo - && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo)) - ? TI_TEMPLATE (tinfo) - : NULL_TREE); +/* Given a lookup that returned VAL, decide if we want to ignore it or + not based on DECL_ANTICIPATED. */ - /* If the level of the parm BINDING_VALUE equals the depth of TMPL, - then BINDING_VALUE is a parameter of TMPL. */ - return (tmpl && level == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl))); +bool +hidden_name_p (tree val) +{ + if (DECL_P (val) + && DECL_LANG_SPECIFIC (val) + && TYPE_FUNCTION_OR_TEMPLATE_DECL_P (val) + && DECL_ANTICIPATED (val)) + return true; + if (TREE_CODE (val) == OVERLOAD) + { + for (tree o = val; o; o = OVL_CHAIN (o)) + if (!hidden_name_p (OVL_FUNCTION (o))) + return false; + return true; + } + return false; } -/* Return the innermost non-namespace binding for NAME from a scope - containing BINDING, or, if BINDING is NULL, the current scope. - Please note that for a given template, the template parameters are - considered to be in the scope containing the current scope. - If CLASS_P is false, then class bindings are ignored. */ +/* Remove any hidden declarations from a possibly overloaded set + of functions. */ -cxx_binding * -outer_binding (tree name, - cxx_binding *binding, - bool class_p) +tree +remove_hidden_names (tree fns) { - cxx_binding *outer; - cp_binding_level *scope; - cp_binding_level *outer_scope; + if (!fns) + return fns; - if (binding) - { - scope = binding->scope->level_chain; - outer = binding->previous; - } - else + if (DECL_P (fns) && hidden_name_p (fns)) + fns = NULL_TREE; + else if (TREE_CODE (fns) == OVERLOAD) { - scope = current_binding_level; - outer = IDENTIFIER_BINDING (name); - } - outer_scope = outer ? outer->scope : NULL; - - /* Because we create class bindings lazily, we might be missing a - class binding for NAME. If there are any class binding levels - between the LAST_BINDING_LEVEL and the scope in which OUTER was - declared, we must lookup NAME in those class scopes. */ - if (class_p) - while (scope && scope != outer_scope && scope->kind != sk_namespace) - { - if (scope->kind == sk_class) - { - cxx_binding *class_binding; + tree o; - class_binding = get_class_binding (name, scope); - if (class_binding) - { - /* Thread this new class-scope binding onto the - IDENTIFIER_BINDING list so that future lookups - find it quickly. */ - class_binding->previous = outer; - if (binding) - binding->previous = class_binding; - else - IDENTIFIER_BINDING (name) = class_binding; - return class_binding; - } - } - /* If we are in a member template, the template parms of the member - template are considered to be inside the scope of the containing - class, but within G++ the class bindings are all pushed between the - template parms and the function body. So if the outer binding is - a template parm for the current scope, return it now rather than - look for a class binding. */ - if (outer_scope && outer_scope->kind == sk_template_parms - && binding_to_template_parms_of_scope_p (outer, scope)) - return outer; + for (o = fns; o; o = OVL_NEXT (o)) + if (hidden_name_p (OVL_CURRENT (o))) + break; + if (o) + { + tree n = NULL_TREE; - scope = scope->level_chain; - } + for (o = fns; o; o = OVL_NEXT (o)) + if (!hidden_name_p (OVL_CURRENT (o))) + n = build_overload (OVL_CURRENT (o), n); + fns = n; + } + } - return outer; + return fns; } -/* Return the innermost block-scope or class-scope value binding for - NAME, or NULL_TREE if there is no such binding. */ +/* Suggest alternatives for NAME, an IDENTIFIER_NODE for which name + lookup failed. Search through all available namespaces and print out + possible candidates. If no exact matches are found, and + SUGGEST_MISSPELLINGS is true, then also look for near-matches and + suggest the best near-match, if there is one. */ -tree -innermost_non_namespace_value (tree name) +void +suggest_alternatives_for (location_t location, tree name, + bool suggest_misspellings) { - cxx_binding *binding; - binding = outer_binding (name, /*binding=*/NULL, /*class_p=*/true); - return binding ? binding->value : NULL_TREE; -} + vec candidates = vNULL; + vec namespaces_to_search = vNULL; + int max_to_search = PARAM_VALUE (CXX_MAX_NAMESPACES_FOR_DIAGNOSTIC_HELP); + int n_searched = 0; + tree t; + unsigned ix; + + namespaces_to_search.safe_push (global_namespace); + + while (!namespaces_to_search.is_empty () + && n_searched < max_to_search) + { + tree scope = namespaces_to_search.pop (); + struct scope_binding binding = EMPTY_SCOPE_BINDING; + cp_binding_level *level = NAMESPACE_LEVEL (scope); -/* Look up NAME in the current binding level and its superiors in the - namespace of variables, functions and typedefs. Return a ..._DECL - node of some kind representing its definition if there is only one - such declaration, or return a TREE_LIST with all the overloaded - definitions if there are many, or return 0 if it is undefined. - Hidden name, either friend declaration or built-in function, are - not ignored. + /* Look in this namespace. */ + qualified_lookup_using_namespace (name, scope, &binding, 0); - If PREFER_TYPE is > 0, we prefer TYPE_DECLs or namespaces. - If PREFER_TYPE is > 1, we reject non-type decls (e.g. namespaces). - Otherwise we prefer non-TYPE_DECLs. + n_searched++; - If NONCLASS is nonzero, bindings in class scopes are ignored. If - BLOCK_P is false, bindings in block scopes are ignored. */ + if (binding.value) + candidates.safe_push (binding.value); -static tree -lookup_name_real_1 (tree name, int prefer_type, int nonclass, bool block_p, - int namespaces_only, int flags) -{ - cxx_binding *iter; - tree val = NULL_TREE; + /* Add child namespaces. */ + for (t = level->namespaces; t; t = DECL_CHAIN (t)) + namespaces_to_search.safe_push (t); + } - query_oracle (name); + /* If we stopped before we could examine all namespaces, inform the + user. Do this even if we don't have any candidates, since there + might be more candidates further down that we weren't able to + find. */ + if (n_searched >= max_to_search + && !namespaces_to_search.is_empty ()) + inform (location, + "maximum limit of %d namespaces searched for %qE", + max_to_search, name); - /* Conversion operators are handled specially because ordinary - unqualified name lookup will not find template conversion - operators. */ - if (IDENTIFIER_TYPENAME_P (name)) - { - cp_binding_level *level; + namespaces_to_search.release (); - for (level = current_binding_level; - level && level->kind != sk_namespace; - level = level->level_chain) + /* Nothing useful to report for NAME. Report on likely misspellings, + or do nothing. */ + if (candidates.is_empty ()) + { + if (suggest_misspellings) { - tree class_type; - tree operators; + const char *fuzzy_name = lookup_name_fuzzy (name, FUZZY_LOOKUP_NAME); + if (fuzzy_name) + { + gcc_rich_location richloc (location); + richloc.add_fixit_replace (fuzzy_name); + inform_at_rich_loc (&richloc, "suggested alternative: %qs", + fuzzy_name); + } + } + return; + } - /* A conversion operator can only be declared in a class - scope. */ - if (level->kind != sk_class) - continue; + inform_n (location, candidates.length (), + "suggested alternative:", + "suggested alternatives:"); - /* Lookup the conversion operator in the class. */ - class_type = level->this_entity; - operators = lookup_fnfields (class_type, name, /*protect=*/0); - if (operators) - return operators; - } + FOR_EACH_VEC_ELT (candidates, ix, t) + inform (location_of (t), " %qE", t); - return NULL_TREE; - } + candidates.release (); +} - flags |= lookup_flags (prefer_type, namespaces_only); +/* Subroutine of maybe_suggest_missing_header for handling unrecognized names + for some of the most common names within "std::". + Given non-NULL NAME, a name for lookup within "std::", return the header + name defining it within the C++ Standard Library (without '<' and '>'), + or NULL. */ - /* First, look in non-namespace scopes. */ +static const char * +get_std_name_hint (const char *name) +{ + struct std_name_hint + { + const char *name; + const char *header; + }; + static const std_name_hint hints[] = { + /* . */ + {"array", "array"}, // C++11 + /* . */ + {"deque", "deque"}, + /* . */ + {"forward_list", "forward_list"}, // C++11 + /* . */ + {"basic_filebuf", "fstream"}, + {"basic_ifstream", "fstream"}, + {"basic_ofstream", "fstream"}, + {"basic_fstream", "fstream"}, + /* . */ + {"cin", "iostream"}, + {"cout", "iostream"}, + {"cerr", "iostream"}, + {"clog", "iostream"}, + {"wcin", "iostream"}, + {"wcout", "iostream"}, + {"wclog", "iostream"}, + /* . */ + {"list", "list"}, + /* . */ + {"map", "map"}, + {"multimap", "map"}, + /* . */ + {"queue", "queue"}, + {"priority_queue", "queue"}, + /* . */ + {"ostream", "ostream"}, + {"wostream", "ostream"}, + {"ends", "ostream"}, + {"flush", "ostream"}, + {"endl", "ostream"}, + /* . */ + {"set", "set"}, + {"multiset", "set"}, + /* . */ + {"basic_stringbuf", "sstream"}, + {"basic_istringstream", "sstream"}, + {"basic_ostringstream", "sstream"}, + {"basic_stringstream", "sstream"}, + /* . */ + {"stack", "stack"}, + /* . */ + {"string", "string"}, + {"wstring", "string"}, + {"u16string", "string"}, + {"u32string", "string"}, + /* . */ + {"unordered_map", "unordered_map"}, // C++11 + {"unordered_multimap", "unordered_map"}, // C++11 + /* . */ + {"unordered_set", "unordered_set"}, // C++11 + {"unordered_multiset", "unordered_set"}, // C++11 + /* . */ + {"vector", "vector"}, + }; + const size_t num_hints = sizeof (hints) / sizeof (hints[0]); + for (size_t i = 0; i < num_hints; i++) + { + if (0 == strcmp (name, hints[i].name)) + return hints[i].header; + } + return NULL; +} - if (current_class_type == NULL_TREE) - nonclass = 1; +/* Subroutine of suggest_alternative_in_explicit_scope, for use when we have no + suggestions to offer. + If SCOPE is the "std" namespace, then suggest pertinent header + files for NAME. */ - if (block_p || !nonclass) - for (iter = outer_binding (name, NULL, !nonclass); - iter; - iter = outer_binding (name, iter, !nonclass)) - { - tree binding; +static void +maybe_suggest_missing_header (location_t location, tree name, tree scope) +{ + if (scope == NULL_TREE) + return; + if (TREE_CODE (scope) != NAMESPACE_DECL) + return; + /* We only offer suggestions for the "std" namespace. */ + if (scope != std_node) + return; + gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE); - /* Skip entities we don't want. */ - if (LOCAL_BINDING_P (iter) ? !block_p : nonclass) - continue; + const char *name_str = IDENTIFIER_POINTER (name); + const char *header_hint = get_std_name_hint (name_str); + if (header_hint) + inform (location, + "% is defined in header %<<%s>%>;" + " did you forget to %<#include <%s>%>?", + name_str, header_hint, header_hint); +} - /* If this is the kind of thing we're looking for, we're done. */ - if (qualify_lookup (iter->value, flags)) - binding = iter->value; - else if ((flags & LOOKUP_PREFER_TYPES) - && qualify_lookup (iter->type, flags)) - binding = iter->type; - else - binding = NULL_TREE; +/* Look for alternatives for NAME, an IDENTIFIER_NODE for which name + lookup failed within the explicitly provided SCOPE. Suggest the + the best meaningful candidates (if any) as a fix-it hint. + Return true iff a suggestion was provided. */ - if (binding) - { - if (hidden_name_p (binding)) - { - /* A non namespace-scope binding can only be hidden in the - presence of a local class, due to friend declarations. +bool +suggest_alternative_in_explicit_scope (location_t location, tree name, + tree scope) +{ + /* Resolve any namespace aliases. */ + scope = ORIGINAL_NAMESPACE (scope); - In particular, consider: + cp_binding_level *level = NAMESPACE_LEVEL (scope); - struct C; - void f() { - struct A { - friend struct B; - friend struct C; - void g() { - B* b; // error: B is hidden - C* c; // OK, finds ::C - } - }; - B *b; // error: B is hidden - C *c; // OK, finds ::C - struct B {}; - B *bb; // OK - } + best_match bm (name); + consider_binding_level (name, bm, level, false, FUZZY_LOOKUP_NAME); - The standard says that "B" is a local class in "f" - (but not nested within "A") -- but that name lookup - for "B" does not find this declaration until it is - declared directly with "f". + /* See if we have a good suggesion for the user. */ + const char *fuzzy_name = bm.get_best_meaningful_candidate (); + if (fuzzy_name) + { + gcc_rich_location richloc (location); + richloc.add_fixit_replace (fuzzy_name); + inform_at_rich_loc (&richloc, "suggested alternative: %qs", + fuzzy_name); + return true; + } + else + maybe_suggest_missing_header (location, name, scope); - In particular: + return false; +} - [class.friend] +/* Unscoped lookup of a global: iterate over current namespaces, + considering using-directives. */ - If a friend declaration appears in a local class and - the name specified is an unqualified name, a prior - declaration is looked up without considering scopes - that are outside the innermost enclosing non-class - scope. For a friend function declaration, if there is - no prior declaration, the program is ill-formed. For a - friend class declaration, if there is no prior - declaration, the class that is specified belongs to the - innermost enclosing non-class scope, but if it is - subsequently referenced, its name is not found by name - lookup until a matching declaration is provided in the - innermost enclosing nonclass scope. +static tree +unqualified_namespace_lookup_1 (tree name, int flags) +{ + tree initial = current_decl_namespace (); + tree scope = initial; + tree siter; + cp_binding_level *level; + tree val = NULL_TREE; - So just keep looking for a non-hidden binding. - */ - gcc_assert (TREE_CODE (binding) == TYPE_DECL); - continue; - } - val = binding; - break; - } - } + for (; !val; scope = CP_DECL_CONTEXT (scope)) + { + struct scope_binding binding = EMPTY_SCOPE_BINDING; + cxx_binding *b = + cp_binding_level_find_binding_for_name (NAMESPACE_LEVEL (scope), name); - /* Now lookup in namespace scopes. */ - if (!val) - val = unqualified_namespace_lookup (name, flags); + if (b) + ambiguous_decl (&binding, b, flags); - /* Anticipated built-ins and friends aren't found by normal lookup. */ - if (val && !(flags & LOOKUP_HIDDEN)) - val = remove_hidden_names (val); + /* Add all _DECLs seen through local using-directives. */ + for (level = current_binding_level; + level->kind != sk_namespace; + level = level->level_chain) + if (!lookup_using_namespace (name, &binding, level->using_directives, + scope, flags)) + /* Give up because of error. */ + return error_mark_node; - /* If we have a single function from a using decl, pull it out. */ - if (val && TREE_CODE (val) == OVERLOAD && !really_overloaded_fn (val)) - val = OVL_FUNCTION (val); + /* Add all _DECLs seen through global using-directives. */ + /* XXX local and global using lists should work equally. */ + siter = initial; + while (1) + { + if (!lookup_using_namespace (name, &binding, + DECL_NAMESPACE_USING (siter), + scope, flags)) + /* Give up because of error. */ + return error_mark_node; + if (siter == scope) break; + siter = CP_DECL_CONTEXT (siter); + } + val = binding.value; + if (scope == global_namespace) + break; + } return val; } -/* Wrapper for lookup_name_real_1. */ +/* Wrapper for unqualified_namespace_lookup_1. */ -tree -lookup_name_real (tree name, int prefer_type, int nonclass, bool block_p, - int namespaces_only, int flags) +static tree +unqualified_namespace_lookup (tree name, int flags) { tree ret; bool subtime = timevar_cond_start (TV_NAME_LOOKUP); - ret = lookup_name_real_1 (name, prefer_type, nonclass, block_p, - namespaces_only, flags); + ret = unqualified_namespace_lookup_1 (name, flags); timevar_cond_stop (TV_NAME_LOOKUP, subtime); return ret; } -tree -lookup_name_nonclass (tree name) -{ - return lookup_name_real (name, 0, 1, /*block_p=*/true, 0, 0); -} +/* Look up NAME (an IDENTIFIER_NODE) in SCOPE (either a NAMESPACE_DECL + or a class TYPE). -tree -lookup_function_nonclass (tree name, vec *args, bool block_p) -{ - return - lookup_arg_dependent (name, - lookup_name_real (name, 0, 1, block_p, 0, 0), - args); -} + If PREFER_TYPE is > 0, we only return TYPE_DECLs or namespaces. + If PREFER_TYPE is > 1, we only return TYPE_DECLs. -tree -lookup_name (tree name) -{ - return lookup_name_real (name, 0, 0, /*block_p=*/true, 0, 0); -} + Returns a DECL (or OVERLOAD, or BASELINK) representing the + declaration found. If no suitable declaration can be found, + ERROR_MARK_NODE is returned. If COMPLAIN is true and SCOPE is + neither a class-type nor a namespace a diagnostic is issued. */ tree -lookup_name_prefer_type (tree name, int prefer_type) +lookup_qualified_name (tree scope, tree name, int prefer_type, bool complain, + bool find_hidden) { - return lookup_name_real (name, prefer_type, 0, /*block_p=*/true, 0, 0); + tree t = NULL_TREE; + + if (TREE_CODE (scope) == NAMESPACE_DECL) + { + struct scope_binding binding = EMPTY_SCOPE_BINDING; + + int flags = lookup_flags (prefer_type, /*namespaces_only*/false); + if (find_hidden) + flags |= LOOKUP_HIDDEN; + if (qualified_lookup_using_namespace (name, scope, &binding, flags)) + t = binding.value; + } + else if (cxx_dialect != cxx98 && TREE_CODE (scope) == ENUMERAL_TYPE) + t = lookup_enumerator (scope, name); + else if (is_class_type (scope, complain)) + t = lookup_member (scope, name, 2, prefer_type, tf_warning_or_error); + + if (!t) + return error_mark_node; + return t; } -/* Look up NAME for type used in elaborated name specifier in - the scopes given by SCOPE. SCOPE can be either TS_CURRENT or - TS_WITHIN_ENCLOSING_NON_CLASS. Although not implied by the - name, more scopes are checked if cleanup or template parameter - scope is encountered. +/* Subroutine of unqualified_namespace_lookup: + Add the bindings of NAME in used namespaces to VAL. + We are currently looking for names in namespace SCOPE, so we + look through USINGS for using-directives of namespaces + which have SCOPE as a common ancestor with the current scope. + Returns false on errors. */ - Unlike lookup_name_real, we make sure that NAME is actually - declared in the desired scope, not from inheritance, nor using - directive. For using declaration, there is DR138 still waiting - to be resolved. Hidden name coming from an earlier friend - declaration is also returned. +static bool +lookup_using_namespace (tree name, struct scope_binding *val, + tree usings, tree scope, int flags) +{ + tree iter; + bool subtime = timevar_cond_start (TV_NAME_LOOKUP); + /* Iterate over all used namespaces in current, searching for using + directives of scope. */ + for (iter = usings; iter; iter = TREE_CHAIN (iter)) + if (TREE_VALUE (iter) == scope) + { + tree used = ORIGINAL_NAMESPACE (TREE_PURPOSE (iter)); + cxx_binding *val1 = + cp_binding_level_find_binding_for_name (NAMESPACE_LEVEL (used), name); + /* Resolve ambiguities. */ + if (val1) + ambiguous_decl (val, val1, flags); + } + timevar_cond_stop (TV_NAME_LOOKUP, subtime); + return val->value != error_mark_node; +} - A TYPE_DECL best matching the NAME is returned. Catching error - and issuing diagnostics are caller's responsibility. */ +/* Returns true iff VEC contains TARGET. */ -static tree -lookup_type_scope_1 (tree name, tag_scope scope) +static bool +tree_vec_contains (vec *vec, tree target) { - cxx_binding *iter = NULL; - tree val = NULL_TREE; + unsigned int i; + tree elt; + FOR_EACH_VEC_SAFE_ELT (vec,i,elt) + if (elt == target) + return true; + return false; +} - /* Look in non-namespace scope first. */ - if (current_binding_level->kind != sk_namespace) - iter = outer_binding (name, NULL, /*class_p=*/ true); - for (; iter; iter = outer_binding (name, iter, /*class_p=*/ true)) - { - /* Check if this is the kind of thing we're looking for. - If SCOPE is TS_CURRENT, also make sure it doesn't come from - base class. For ITER->VALUE, we can simply use - INHERITED_VALUE_BINDING_P. For ITER->TYPE, we have to use - our own check. +/* [namespace.qual] + Accepts the NAME to lookup and its qualifying SCOPE. + Returns the name/type pair found into the cxx_binding *RESULT, + or false on error. */ - We check ITER->TYPE before ITER->VALUE in order to handle - typedef struct C {} C; - correctly. */ +static bool +qualified_lookup_using_namespace (tree name, tree scope, + struct scope_binding *result, int flags) +{ + /* Maintain a list of namespaces visited... */ + vec *seen = NULL; + vec *seen_inline = NULL; + /* ... and a list of namespace yet to see. */ + vec *todo = NULL; + vec *todo_maybe = NULL; + vec *todo_inline = NULL; + tree usings; + timevar_start (TV_NAME_LOOKUP); + /* Look through namespace aliases. */ + scope = ORIGINAL_NAMESPACE (scope); - if (qualify_lookup (iter->type, LOOKUP_PREFER_TYPES) - && (scope != ts_current - || LOCAL_BINDING_P (iter) - || DECL_CONTEXT (iter->type) == iter->scope->this_entity)) - val = iter->type; - else if ((scope != ts_current - || !INHERITED_VALUE_BINDING_P (iter)) - && qualify_lookup (iter->value, LOOKUP_PREFER_TYPES)) - val = iter->value; + query_oracle (name); - if (val) - break; - } + /* Algorithm: Starting with SCOPE, walk through the set of used + namespaces. For each used namespace, look through its inline + namespace set for any bindings and usings. If no bindings are + found, add any usings seen to the set of used namespaces. */ + vec_safe_push (todo, scope); - /* Look in namespace scope. */ - if (!val) + while (todo->length ()) { - iter = cp_binding_level_find_binding_for_name - (NAMESPACE_LEVEL (current_decl_namespace ()), name); + bool found_here; + scope = todo->pop (); + if (tree_vec_contains (seen, scope)) + continue; + vec_safe_push (seen, scope); + vec_safe_push (todo_inline, scope); - if (iter) + found_here = false; + while (todo_inline->length ()) { - /* If this is the kind of thing we're looking for, we're done. */ - if (qualify_lookup (iter->type, LOOKUP_PREFER_TYPES)) - val = iter->type; - else if (qualify_lookup (iter->value, LOOKUP_PREFER_TYPES)) - val = iter->value; - } + cxx_binding *binding; - } + scope = todo_inline->pop (); + if (tree_vec_contains (seen_inline, scope)) + continue; + vec_safe_push (seen_inline, scope); - /* Type found, check if it is in the allowed scopes, ignoring cleanup - and template parameter scopes. */ - if (val) - { - cp_binding_level *b = current_binding_level; - while (b) - { - if (iter->scope == b) - return val; + binding = + cp_binding_level_find_binding_for_name (NAMESPACE_LEVEL (scope), name); + if (binding) + { + ambiguous_decl (result, binding, flags); + if (result->type || result->value) + found_here = true; + } - if (b->kind == sk_cleanup || b->kind == sk_template_parms - || b->kind == sk_function_parms) - b = b->level_chain; - else if (b->kind == sk_class - && scope == ts_within_enclosing_non_class) - b = b->level_chain; - else - break; + for (usings = DECL_NAMESPACE_USING (scope); usings; + usings = TREE_CHAIN (usings)) + if (!TREE_INDIRECT_USING (usings)) + { + if (is_associated_namespace (scope, TREE_PURPOSE (usings))) + vec_safe_push (todo_inline, TREE_PURPOSE (usings)); + else + vec_safe_push (todo_maybe, TREE_PURPOSE (usings)); + } } - } - - return NULL_TREE; -} - -/* Wrapper for lookup_type_scope_1. */ -tree -lookup_type_scope (tree name, tag_scope scope) -{ - tree ret; - bool subtime = timevar_cond_start (TV_NAME_LOOKUP); - ret = lookup_type_scope_1 (name, scope); - timevar_cond_stop (TV_NAME_LOOKUP, subtime); - return ret; + if (found_here) + vec_safe_truncate (todo_maybe, 0); + else + while (vec_safe_length (todo_maybe)) + vec_safe_push (todo, todo_maybe->pop ()); + } + vec_free (todo); + vec_free (todo_maybe); + vec_free (todo_inline); + vec_free (seen); + vec_free (seen_inline); + timevar_stop (TV_NAME_LOOKUP); + return result->value != error_mark_node; } - -/* Similar to `lookup_name' but look only in the innermost non-class - binding level. */ - -static tree -lookup_name_innermost_nonclass_level_1 (tree name) +/* Helper function for lookup_name_fuzzy. + Traverse binding level LVL, looking for good name matches for NAME + (and BM). */ +static void +consider_binding_level (tree name, best_match &bm, + cp_binding_level *lvl, bool look_within_fields, + enum lookup_name_fuzzy_kind kind) { - cp_binding_level *b; - tree t = NULL_TREE; - - b = innermost_nonclass_level (); + if (look_within_fields) + if (lvl->this_entity && TREE_CODE (lvl->this_entity) == RECORD_TYPE) + { + tree type = lvl->this_entity; + bool want_type_p = (kind == FUZZY_LOOKUP_TYPENAME); + tree best_matching_field + = lookup_member_fuzzy (type, name, want_type_p); + if (best_matching_field) + bm.consider (IDENTIFIER_POINTER (best_matching_field)); + } - if (b->kind == sk_namespace) + for (tree t = lvl->names; t; t = TREE_CHAIN (t)) { - t = IDENTIFIER_NAMESPACE_VALUE (name); + tree d = t; - /* extern "C" function() */ - if (t != NULL_TREE && TREE_CODE (t) == TREE_LIST) - t = TREE_VALUE (t); - } - else if (IDENTIFIER_BINDING (name) - && LOCAL_BINDING_P (IDENTIFIER_BINDING (name))) - { - cxx_binding *binding; - binding = IDENTIFIER_BINDING (name); - while (1) + /* OVERLOADs or decls from using declaration are wrapped into + TREE_LIST. */ + if (TREE_CODE (d) == TREE_LIST) { - if (binding->scope == b - && !(VAR_P (binding->value) - && DECL_DEAD_FOR_LOCAL (binding->value))) - return binding->value; - - if (b->kind == sk_cleanup) - b = b->level_chain; - else - break; + d = TREE_VALUE (d); + d = OVL_CURRENT (d); } - } - return t; -} + /* Don't use bindings from implicitly declared functions, + as they were likely misspellings themselves. */ + if (TREE_TYPE (d) == error_mark_node) + continue; -/* Wrapper for lookup_name_innermost_nonclass_level_1. */ + /* Skip anticipated decls of builtin functions. */ + if (TREE_CODE (d) == FUNCTION_DECL + && DECL_BUILT_IN (d) + && DECL_ANTICIPATED (d)) + continue; -tree -lookup_name_innermost_nonclass_level (tree name) -{ - tree ret; - bool subtime = timevar_cond_start (TV_NAME_LOOKUP); - ret = lookup_name_innermost_nonclass_level_1 (name); - timevar_cond_stop (TV_NAME_LOOKUP, subtime); - return ret; + if (tree name = DECL_NAME (d)) + /* Ignore internal names with spaces in them. */ + if (!strchr (IDENTIFIER_POINTER (name), ' ')) + bm.consider (IDENTIFIER_POINTER (name)); + } } +/* Search for near-matches for NAME within the current bindings, and within + macro names, returning the best match as a const char *, or NULL if + no reasonable match is found. */ -/* Returns true iff DECL is a block-scope extern declaration of a function - or variable. */ - -bool -is_local_extern (tree decl) +const char * +lookup_name_fuzzy (tree name, enum lookup_name_fuzzy_kind kind) { - cxx_binding *binding; + gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE); - /* For functions, this is easy. */ - if (TREE_CODE (decl) == FUNCTION_DECL) - return DECL_LOCAL_FUNCTION_P (decl); + best_match bm (name); - if (!VAR_P (decl)) - return false; - if (!current_function_decl) - return false; + cp_binding_level *lvl; + for (lvl = scope_chain->class_bindings; lvl; lvl = lvl->level_chain) + consider_binding_level (name, bm, lvl, true, kind); - /* For variables, this is not easy. We need to look at the binding stack - for the identifier to see whether the decl we have is a local. */ - for (binding = IDENTIFIER_BINDING (DECL_NAME (decl)); - binding && binding->scope->kind != sk_namespace; - binding = binding->previous) - if (binding->value == decl) - return LOCAL_BINDING_P (binding); + for (lvl = current_binding_level; lvl; lvl = lvl->level_chain) + consider_binding_level (name, bm, lvl, false, kind); - return false; -} + /* Consider macros: if the user misspelled a macro name e.g. "SOME_MACRO" + as: + x = SOME_OTHER_MACRO (y); + then "SOME_OTHER_MACRO" will survive to the frontend and show up + as a misspelled identifier. -/* Like lookup_name_innermost_nonclass_level, but for types. */ + Use the best distance so far so that a candidate is only set if + a macro is better than anything so far. This allows early rejection + (without calculating the edit distance) of macro names that must have + distance >= bm.get_best_distance (), and means that we only get a + non-NULL result for best_macro_match if it's better than any of + the identifiers already checked. */ + best_macro_match bmm (name, bm.get_best_distance (), parse_in); + cpp_hashnode *best_macro = bmm.get_best_meaningful_candidate (); + /* If a macro is the closest so far to NAME, consider it. */ + if (best_macro) + bm.consider ((const char *)best_macro->ident.str); -static tree -lookup_type_current_level (tree name) -{ - tree t = NULL_TREE; + /* Try the "starts_decl_specifier_p" keywords to detect + "singed" vs "signed" typos. */ + for (unsigned i = 0; i < num_c_common_reswords; i++) + { + const c_common_resword *resword = &c_common_reswords[i]; - timevar_start (TV_NAME_LOOKUP); - gcc_assert (current_binding_level->kind != sk_namespace); + if (kind == FUZZY_LOOKUP_TYPENAME) + if (!cp_keyword_starts_decl_specifier_p (resword->rid)) + continue; - if (REAL_IDENTIFIER_TYPE_VALUE (name) != NULL_TREE - && REAL_IDENTIFIER_TYPE_VALUE (name) != global_type_node) - { - cp_binding_level *b = current_binding_level; - while (1) - { - if (purpose_member (name, b->type_shadowed)) - { - t = REAL_IDENTIFIER_TYPE_VALUE (name); - break; - } - if (b->kind == sk_cleanup) - b = b->level_chain; - else - break; - } - } + tree resword_identifier = ridpointers [resword->rid]; + if (!resword_identifier) + continue; + gcc_assert (TREE_CODE (resword_identifier) == IDENTIFIER_NODE); - timevar_stop (TV_NAME_LOOKUP); - return t; -} + /* Only consider reserved words that survived the + filtering in init_reswords (e.g. for -std). */ + if (!C_IS_RESERVED_WORD (resword_identifier)) + continue; -/* [basic.lookup.koenig] */ -/* A nonzero return value in the functions below indicates an error. */ + bm.consider (IDENTIFIER_POINTER (resword_identifier)); + } + + return bm.get_best_meaningful_candidate (); +} -struct arg_lookup -{ - tree name; - vec *args; - vec *namespaces; - vec *classes; - tree functions; - hash_set *fn_set; -}; +/* Subroutine of outer_binding. -static bool arg_assoc (struct arg_lookup*, tree); -static bool arg_assoc_args (struct arg_lookup*, tree); -static bool arg_assoc_args_vec (struct arg_lookup*, vec *); -static bool arg_assoc_type (struct arg_lookup*, tree); -static bool add_function (struct arg_lookup *, tree); -static bool arg_assoc_namespace (struct arg_lookup *, tree); -static bool arg_assoc_class_only (struct arg_lookup *, tree); -static bool arg_assoc_bases (struct arg_lookup *, tree); -static bool arg_assoc_class (struct arg_lookup *, tree); -static bool arg_assoc_template_arg (struct arg_lookup*, tree); + Returns TRUE if BINDING is a binding to a template parameter of + SCOPE. In that case SCOPE is the scope of a primary template + parameter -- in the sense of G++, i.e, a template that has its own + template header. -/* Add a function to the lookup structure. - Returns true on error. */ + Returns FALSE otherwise. */ static bool -add_function (struct arg_lookup *k, tree fn) +binding_to_template_parms_of_scope_p (cxx_binding *binding, + cp_binding_level *scope) { - if (!is_overloaded_fn (fn)) - /* All names except those of (possibly overloaded) functions and - function templates are ignored. */; - else if (k->fn_set && k->fn_set->add (fn)) - /* It's already in the list. */; - else if (!k->functions && TREE_CODE (fn) != TEMPLATE_DECL) - k->functions = fn; - else if (fn == k->functions) - ; - else - { - k->functions = build_overload (fn, k->functions); - if (TREE_CODE (k->functions) == OVERLOAD) - OVL_ARG_DEPENDENT (k->functions) = true; - } + tree binding_value, tmpl, tinfo; + int level; - return false; + if (!binding || !scope || !scope->this_entity) + return false; + + binding_value = binding->value ? binding->value : binding->type; + tinfo = get_template_info (scope->this_entity); + + /* BINDING_VALUE must be a template parm. */ + if (binding_value == NULL_TREE + || (!DECL_P (binding_value) + || !DECL_TEMPLATE_PARM_P (binding_value))) + return false; + + /* The level of BINDING_VALUE. */ + level = + template_type_parameter_p (binding_value) + ? TEMPLATE_PARM_LEVEL (TEMPLATE_TYPE_PARM_INDEX + (TREE_TYPE (binding_value))) + : TEMPLATE_PARM_LEVEL (DECL_INITIAL (binding_value)); + + /* The template of the current scope, iff said scope is a primary + template. */ + tmpl = (tinfo + && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo)) + ? TI_TEMPLATE (tinfo) + : NULL_TREE); + + /* If the level of the parm BINDING_VALUE equals the depth of TMPL, + then BINDING_VALUE is a parameter of TMPL. */ + return (tmpl && level == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl))); } -/* Returns true iff CURRENT has declared itself to be an associated - namespace of SCOPE via a strong using-directive (or transitive chain - thereof). Both are namespaces. */ +/* Return the innermost non-namespace binding for NAME from a scope + containing BINDING, or, if BINDING is NULL, the current scope. + Please note that for a given template, the template parameters are + considered to be in the scope containing the current scope. + If CLASS_P is false, then class bindings are ignored. */ -bool -is_associated_namespace (tree current, tree scope) +cxx_binding * +outer_binding (tree name, + cxx_binding *binding, + bool class_p) { - vec *seen = make_tree_vector (); - vec *todo = make_tree_vector (); - tree t; - bool ret; + cxx_binding *outer; + cp_binding_level *scope; + cp_binding_level *outer_scope; - while (1) + if (binding) { - if (scope == current) - { - ret = true; - break; - } - vec_safe_push (seen, scope); - for (t = DECL_NAMESPACE_ASSOCIATIONS (scope); t; t = TREE_CHAIN (t)) - if (!vec_member (TREE_PURPOSE (t), seen)) - vec_safe_push (todo, TREE_PURPOSE (t)); - if (!todo->is_empty ()) - { - scope = todo->last (); - todo->pop (); - } - else - { - ret = false; - break; - } + scope = binding->scope->level_chain; + outer = binding->previous; + } + else + { + scope = current_binding_level; + outer = IDENTIFIER_BINDING (name); } + outer_scope = outer ? outer->scope : NULL; - release_tree_vector (seen); - release_tree_vector (todo); + /* Because we create class bindings lazily, we might be missing a + class binding for NAME. If there are any class binding levels + between the LAST_BINDING_LEVEL and the scope in which OUTER was + declared, we must lookup NAME in those class scopes. */ + if (class_p) + while (scope && scope != outer_scope && scope->kind != sk_namespace) + { + if (scope->kind == sk_class) + { + cxx_binding *class_binding; - return ret; + class_binding = get_class_binding (name, scope); + if (class_binding) + { + /* Thread this new class-scope binding onto the + IDENTIFIER_BINDING list so that future lookups + find it quickly. */ + class_binding->previous = outer; + if (binding) + binding->previous = class_binding; + else + IDENTIFIER_BINDING (name) = class_binding; + return class_binding; + } + } + /* If we are in a member template, the template parms of the member + template are considered to be inside the scope of the containing + class, but within G++ the class bindings are all pushed between the + template parms and the function body. So if the outer binding is + a template parm for the current scope, return it now rather than + look for a class binding. */ + if (outer_scope && outer_scope->kind == sk_template_parms + && binding_to_template_parms_of_scope_p (outer, scope)) + return outer; + + scope = scope->level_chain; + } + + return outer; } -/* Add functions of a namespace to the lookup structure. - Returns true on error. */ +/* Return the innermost block-scope or class-scope value binding for + NAME, or NULL_TREE if there is no such binding. */ -static bool -arg_assoc_namespace (struct arg_lookup *k, tree scope) +tree +innermost_non_namespace_value (tree name) { - tree value; + cxx_binding *binding; + binding = outer_binding (name, /*binding=*/NULL, /*class_p=*/true); + return binding ? binding->value : NULL_TREE; +} - if (vec_member (scope, k->namespaces)) - return false; - vec_safe_push (k->namespaces, scope); +/* Look up NAME in the current binding level and its superiors in the + namespace of variables, functions and typedefs. Return a ..._DECL + node of some kind representing its definition if there is only one + such declaration, or return a TREE_LIST with all the overloaded + definitions if there are many, or return 0 if it is undefined. + Hidden name, either friend declaration or built-in function, are + not ignored. - /* Check out our super-users. */ - for (value = DECL_NAMESPACE_ASSOCIATIONS (scope); value; - value = TREE_CHAIN (value)) - if (arg_assoc_namespace (k, TREE_PURPOSE (value))) - return true; + If PREFER_TYPE is > 0, we prefer TYPE_DECLs or namespaces. + If PREFER_TYPE is > 1, we reject non-type decls (e.g. namespaces). + Otherwise we prefer non-TYPE_DECLs. - /* Also look down into inline namespaces. */ - for (value = DECL_NAMESPACE_USING (scope); value; - value = TREE_CHAIN (value)) - if (is_associated_namespace (scope, TREE_PURPOSE (value))) - if (arg_assoc_namespace (k, TREE_PURPOSE (value))) - return true; + If NONCLASS is nonzero, bindings in class scopes are ignored. If + BLOCK_P is false, bindings in block scopes are ignored. */ - value = namespace_binding (k->name, scope); - if (!value) - return false; +static tree +lookup_name_real_1 (tree name, int prefer_type, int nonclass, bool block_p, + int namespaces_only, int flags) +{ + cxx_binding *iter; + tree val = NULL_TREE; - for (; value; value = OVL_NEXT (value)) + query_oracle (name); + + /* Conversion operators are handled specially because ordinary + unqualified name lookup will not find template conversion + operators. */ + if (IDENTIFIER_TYPENAME_P (name)) { - /* We don't want to find arbitrary hidden functions via argument - dependent lookup. We only want to find friends of associated - classes, which we'll do via arg_assoc_class. */ - if (hidden_name_p (OVL_CURRENT (value))) - continue; + cp_binding_level *level; - if (add_function (k, OVL_CURRENT (value))) - return true; + for (level = current_binding_level; + level && level->kind != sk_namespace; + level = level->level_chain) + { + tree class_type; + tree operators; + + /* A conversion operator can only be declared in a class + scope. */ + if (level->kind != sk_class) + continue; + + /* Lookup the conversion operator in the class. */ + class_type = level->this_entity; + operators = lookup_fnfields (class_type, name, /*protect=*/0); + if (operators) + return operators; + } + + return NULL_TREE; } - return false; -} + flags |= lookup_flags (prefer_type, namespaces_only); + + /* First, look in non-namespace scopes. */ + + if (current_class_type == NULL_TREE) + nonclass = 1; -/* Adds everything associated with a template argument to the lookup - structure. Returns true on error. */ + if (block_p || !nonclass) + for (iter = outer_binding (name, NULL, !nonclass); + iter; + iter = outer_binding (name, iter, !nonclass)) + { + tree binding; -static bool -arg_assoc_template_arg (struct arg_lookup *k, tree arg) -{ - /* [basic.lookup.koenig] + /* Skip entities we don't want. */ + if (LOCAL_BINDING_P (iter) ? !block_p : nonclass) + continue; - If T is a template-id, its associated namespaces and classes are - ... the namespaces and classes associated with the types of the - template arguments provided for template type parameters - (excluding template template parameters); the namespaces in which - any template template arguments are defined; and the classes in - which any member templates used as template template arguments - are defined. [Note: non-type template arguments do not - contribute to the set of associated namespaces. ] */ + /* If this is the kind of thing we're looking for, we're done. */ + if (qualify_lookup (iter->value, flags)) + binding = iter->value; + else if ((flags & LOOKUP_PREFER_TYPES) + && qualify_lookup (iter->type, flags)) + binding = iter->type; + else + binding = NULL_TREE; - /* Consider first template template arguments. */ - if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM - || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE) - return false; - else if (TREE_CODE (arg) == TEMPLATE_DECL) - { - tree ctx = CP_DECL_CONTEXT (arg); + if (binding) + { + if (hidden_name_p (binding)) + { + /* A non namespace-scope binding can only be hidden in the + presence of a local class, due to friend declarations. - /* It's not a member template. */ - if (TREE_CODE (ctx) == NAMESPACE_DECL) - return arg_assoc_namespace (k, ctx); - /* Otherwise, it must be member template. */ - else - return arg_assoc_class_only (k, ctx); - } - /* It's an argument pack; handle it recursively. */ - else if (ARGUMENT_PACK_P (arg)) - { - tree args = ARGUMENT_PACK_ARGS (arg); - int i, len = TREE_VEC_LENGTH (args); - for (i = 0; i < len; ++i) - if (arg_assoc_template_arg (k, TREE_VEC_ELT (args, i))) - return true; + In particular, consider: - return false; - } - /* It's not a template template argument, but it is a type template - argument. */ - else if (TYPE_P (arg)) - return arg_assoc_type (k, arg); - /* It's a non-type template argument. */ - else - return false; -} + struct C; + void f() { + struct A { + friend struct B; + friend struct C; + void g() { + B* b; // error: B is hidden + C* c; // OK, finds ::C + } + }; + B *b; // error: B is hidden + C *c; // OK, finds ::C + struct B {}; + B *bb; // OK + } -/* Adds the class and its friends to the lookup structure. - Returns true on error. */ + The standard says that "B" is a local class in "f" + (but not nested within "A") -- but that name lookup + for "B" does not find this declaration until it is + declared directly with "f". -static bool -arg_assoc_class_only (struct arg_lookup *k, tree type) -{ - tree list, friends, context; + In particular: - /* Backend-built structures, such as __builtin_va_list, aren't - affected by all this. */ - if (!CLASS_TYPE_P (type)) - return false; + [class.friend] - context = decl_namespace_context (type); - if (arg_assoc_namespace (k, context)) - return true; + If a friend declaration appears in a local class and + the name specified is an unqualified name, a prior + declaration is looked up without considering scopes + that are outside the innermost enclosing non-class + scope. For a friend function declaration, if there is + no prior declaration, the program is ill-formed. For a + friend class declaration, if there is no prior + declaration, the class that is specified belongs to the + innermost enclosing non-class scope, but if it is + subsequently referenced, its name is not found by name + lookup until a matching declaration is provided in the + innermost enclosing nonclass scope. - complete_type (type); + So just keep looking for a non-hidden binding. + */ + gcc_assert (TREE_CODE (binding) == TYPE_DECL); + continue; + } + val = binding; + break; + } + } - /* Process friends. */ - for (list = DECL_FRIENDLIST (TYPE_MAIN_DECL (type)); list; - list = TREE_CHAIN (list)) - if (k->name == FRIEND_NAME (list)) - for (friends = FRIEND_DECLS (list); friends; - friends = TREE_CHAIN (friends)) - { - tree fn = TREE_VALUE (friends); + /* Now lookup in namespace scopes. */ + if (!val) + val = unqualified_namespace_lookup (name, flags); - /* Only interested in global functions with potentially hidden - (i.e. unqualified) declarations. */ - if (CP_DECL_CONTEXT (fn) != context) - continue; - /* Template specializations are never found by name lookup. - (Templates themselves can be found, but not template - specializations.) */ - if (TREE_CODE (fn) == FUNCTION_DECL && DECL_USE_TEMPLATE (fn)) - continue; - if (add_function (k, fn)) - return true; - } + /* Anticipated built-ins and friends aren't found by normal lookup. */ + if (val && !(flags & LOOKUP_HIDDEN)) + val = remove_hidden_names (val); - return false; + /* If we have a single function from a using decl, pull it out. */ + if (val && TREE_CODE (val) == OVERLOAD && !really_overloaded_fn (val)) + val = OVL_FUNCTION (val); + + return val; } -/* Adds the class and its bases to the lookup structure. - Returns true on error. */ +/* Wrapper for lookup_name_real_1. */ -static bool -arg_assoc_bases (struct arg_lookup *k, tree type) +tree +lookup_name_real (tree name, int prefer_type, int nonclass, bool block_p, + int namespaces_only, int flags) { - if (arg_assoc_class_only (k, type)) - return true; - - if (TYPE_BINFO (type)) - { - /* Process baseclasses. */ - tree binfo, base_binfo; - int i; - - for (binfo = TYPE_BINFO (type), i = 0; - BINFO_BASE_ITERATE (binfo, i, base_binfo); i++) - if (arg_assoc_bases (k, BINFO_TYPE (base_binfo))) - return true; - } + tree ret; + bool subtime = timevar_cond_start (TV_NAME_LOOKUP); + ret = lookup_name_real_1 (name, prefer_type, nonclass, block_p, + namespaces_only, flags); + timevar_cond_stop (TV_NAME_LOOKUP, subtime); + return ret; +} - return false; +tree +lookup_name_nonclass (tree name) +{ + return lookup_name_real (name, 0, 1, /*block_p=*/true, 0, 0); } -/* Adds everything associated with a class argument type to the lookup - structure. Returns true on error. +tree +lookup_function_nonclass (tree name, vec *args, bool block_p) +{ + return + lookup_arg_dependent (name, + lookup_name_real (name, 0, 1, block_p, 0, 0), + args); +} - If T is a class type (including unions), its associated classes are: the - class itself; the class of which it is a member, if any; and its direct - and indirect base classes. Its associated namespaces are the namespaces - of which its associated classes are members. Furthermore, if T is a - class template specialization, its associated namespaces and classes - also include: the namespaces and classes associated with the types of - the template arguments provided for template type parameters (excluding - template template parameters); the namespaces of which any template - template arguments are members; and the classes of which any member - templates used as template template arguments are members. [ Note: - non-type template arguments do not contribute to the set of associated - namespaces. --end note] */ +tree +lookup_name (tree name) +{ + return lookup_name_real (name, 0, 0, /*block_p=*/true, 0, 0); +} -static bool -arg_assoc_class (struct arg_lookup *k, tree type) +tree +lookup_name_prefer_type (tree name, int prefer_type) { - tree list; - int i; + return lookup_name_real (name, prefer_type, 0, /*block_p=*/true, 0, 0); +} - /* Backend build structures, such as __builtin_va_list, aren't - affected by all this. */ - if (!CLASS_TYPE_P (type)) - return false; +/* Look up NAME for type used in elaborated name specifier in + the scopes given by SCOPE. SCOPE can be either TS_CURRENT or + TS_WITHIN_ENCLOSING_NON_CLASS. Although not implied by the + name, more scopes are checked if cleanup or template parameter + scope is encountered. - if (vec_member (type, k->classes)) - return false; - vec_safe_push (k->classes, type); + Unlike lookup_name_real, we make sure that NAME is actually + declared in the desired scope, not from inheritance, nor using + directive. For using declaration, there is DR138 still waiting + to be resolved. Hidden name coming from an earlier friend + declaration is also returned. - if (TYPE_CLASS_SCOPE_P (type) - && arg_assoc_class_only (k, TYPE_CONTEXT (type))) - return true; + A TYPE_DECL best matching the NAME is returned. Catching error + and issuing diagnostics are caller's responsibility. */ - if (arg_assoc_bases (k, type)) - return true; +static tree +lookup_type_scope_1 (tree name, tag_scope scope) +{ + cxx_binding *iter = NULL; + tree val = NULL_TREE; - /* Process template arguments. */ - if (CLASSTYPE_TEMPLATE_INFO (type) - && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type))) + /* Look in non-namespace scope first. */ + if (current_binding_level->kind != sk_namespace) + iter = outer_binding (name, NULL, /*class_p=*/ true); + for (; iter; iter = outer_binding (name, iter, /*class_p=*/ true)) { - list = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)); - for (i = 0; i < TREE_VEC_LENGTH (list); ++i) - if (arg_assoc_template_arg (k, TREE_VEC_ELT (list, i))) - return true; - } + /* Check if this is the kind of thing we're looking for. + If SCOPE is TS_CURRENT, also make sure it doesn't come from + base class. For ITER->VALUE, we can simply use + INHERITED_VALUE_BINDING_P. For ITER->TYPE, we have to use + our own check. - return false; -} + We check ITER->TYPE before ITER->VALUE in order to handle + typedef struct C {} C; + correctly. */ -/* Adds everything associated with a given type. - Returns 1 on error. */ + if (qualify_lookup (iter->type, LOOKUP_PREFER_TYPES) + && (scope != ts_current + || LOCAL_BINDING_P (iter) + || DECL_CONTEXT (iter->type) == iter->scope->this_entity)) + val = iter->type; + else if ((scope != ts_current + || !INHERITED_VALUE_BINDING_P (iter)) + && qualify_lookup (iter->value, LOOKUP_PREFER_TYPES)) + val = iter->value; -static bool -arg_assoc_type (struct arg_lookup *k, tree type) -{ - /* As we do not get the type of non-type dependent expressions - right, we can end up with such things without a type. */ - if (!type) - return false; + if (val) + break; + } - if (TYPE_PTRDATAMEM_P (type)) + /* Look in namespace scope. */ + if (!val) { - /* Pointer to member: associate class type and value type. */ - if (arg_assoc_type (k, TYPE_PTRMEM_CLASS_TYPE (type))) - return true; - return arg_assoc_type (k, TYPE_PTRMEM_POINTED_TO_TYPE (type)); + iter = cp_binding_level_find_binding_for_name + (NAMESPACE_LEVEL (current_decl_namespace ()), name); + + if (iter) + { + /* If this is the kind of thing we're looking for, we're done. */ + if (qualify_lookup (iter->type, LOOKUP_PREFER_TYPES)) + val = iter->type; + else if (qualify_lookup (iter->value, LOOKUP_PREFER_TYPES)) + val = iter->value; + } + } - else switch (TREE_CODE (type)) + + /* Type found, check if it is in the allowed scopes, ignoring cleanup + and template parameter scopes. */ + if (val) { - case ERROR_MARK: - return false; - case VOID_TYPE: - case INTEGER_TYPE: - case REAL_TYPE: - case COMPLEX_TYPE: - case VECTOR_TYPE: - case BOOLEAN_TYPE: - case FIXED_POINT_TYPE: - case DECLTYPE_TYPE: - case NULLPTR_TYPE: - return false; - case RECORD_TYPE: - if (TYPE_PTRMEMFUNC_P (type)) - return arg_assoc_type (k, TYPE_PTRMEMFUNC_FN_TYPE (type)); - /* FALLTHRU */ - case UNION_TYPE: - return arg_assoc_class (k, type); - case POINTER_TYPE: - case REFERENCE_TYPE: - case ARRAY_TYPE: - return arg_assoc_type (k, TREE_TYPE (type)); - case ENUMERAL_TYPE: - if (TYPE_CLASS_SCOPE_P (type) - && arg_assoc_class_only (k, TYPE_CONTEXT (type))) - return true; - return arg_assoc_namespace (k, decl_namespace_context (type)); - case METHOD_TYPE: - /* The basetype is referenced in the first arg type, so just - fall through. */ - case FUNCTION_TYPE: - /* Associate the parameter types. */ - if (arg_assoc_args (k, TYPE_ARG_TYPES (type))) - return true; - /* Associate the return type. */ - return arg_assoc_type (k, TREE_TYPE (type)); - case TEMPLATE_TYPE_PARM: - case BOUND_TEMPLATE_TEMPLATE_PARM: - return false; - case TYPENAME_TYPE: - return false; - case LANG_TYPE: - gcc_assert (type == unknown_type_node - || type == init_list_type_node); - return false; - case TYPE_PACK_EXPANSION: - return arg_assoc_type (k, PACK_EXPANSION_PATTERN (type)); + cp_binding_level *b = current_binding_level; + while (b) + { + if (iter->scope == b) + return val; - default: - gcc_unreachable (); + if (b->kind == sk_cleanup || b->kind == sk_template_parms + || b->kind == sk_function_parms) + b = b->level_chain; + else if (b->kind == sk_class + && scope == ts_within_enclosing_non_class) + b = b->level_chain; + else + break; + } } - return false; -} -/* Adds everything associated with arguments. Returns true on error. */ - -static bool -arg_assoc_args (struct arg_lookup *k, tree args) -{ - for (; args; args = TREE_CHAIN (args)) - if (arg_assoc (k, TREE_VALUE (args))) - return true; - return false; + return NULL_TREE; } + +/* Wrapper for lookup_type_scope_1. */ -/* Adds everything associated with an argument vector. Returns true - on error. */ - -static bool -arg_assoc_args_vec (struct arg_lookup *k, vec *args) +tree +lookup_type_scope (tree name, tag_scope scope) { - unsigned int ix; - tree arg; - - FOR_EACH_VEC_SAFE_ELT (args, ix, arg) - if (arg_assoc (k, arg)) - return true; - return false; + tree ret; + bool subtime = timevar_cond_start (TV_NAME_LOOKUP); + ret = lookup_type_scope_1 (name, scope); + timevar_cond_stop (TV_NAME_LOOKUP, subtime); + return ret; } -/* Adds everything associated with a given tree_node. Returns 1 on error. */ - -static bool -arg_assoc (struct arg_lookup *k, tree n) -{ - if (n == error_mark_node) - return false; - if (TYPE_P (n)) - return arg_assoc_type (k, n); +/* Similar to `lookup_name' but look only in the innermost non-class + binding level. */ - if (! type_unknown_p (n)) - return arg_assoc_type (k, TREE_TYPE (n)); +static tree +lookup_name_innermost_nonclass_level_1 (tree name) +{ + cp_binding_level *b; + tree t = NULL_TREE; - if (TREE_CODE (n) == ADDR_EXPR) - n = TREE_OPERAND (n, 0); - if (TREE_CODE (n) == COMPONENT_REF) - n = TREE_OPERAND (n, 1); - if (TREE_CODE (n) == OFFSET_REF) - n = TREE_OPERAND (n, 1); - while (TREE_CODE (n) == TREE_LIST) - n = TREE_VALUE (n); - if (BASELINK_P (n)) - n = BASELINK_FUNCTIONS (n); + b = innermost_nonclass_level (); - if (TREE_CODE (n) == FUNCTION_DECL) - return arg_assoc_type (k, TREE_TYPE (n)); - if (TREE_CODE (n) == TEMPLATE_ID_EXPR) + if (b->kind == sk_namespace) { - /* The working paper doesn't currently say how to handle template-id - arguments. The sensible thing would seem to be to handle the list - of template candidates like a normal overload set, and handle the - template arguments like we do for class template - specializations. */ - tree templ = TREE_OPERAND (n, 0); - tree args = TREE_OPERAND (n, 1); - int ix; - - /* First the templates. */ - if (arg_assoc (k, templ)) - return true; + t = IDENTIFIER_NAMESPACE_VALUE (name); - /* Now the arguments. */ - if (args) - for (ix = TREE_VEC_LENGTH (args); ix--;) - if (arg_assoc_template_arg (k, TREE_VEC_ELT (args, ix)) == 1) - return true; + /* extern "C" function() */ + if (t != NULL_TREE && TREE_CODE (t) == TREE_LIST) + t = TREE_VALUE (t); } - else if (TREE_CODE (n) == OVERLOAD) + else if (IDENTIFIER_BINDING (name) + && LOCAL_BINDING_P (IDENTIFIER_BINDING (name))) { - for (; n; n = OVL_NEXT (n)) - if (arg_assoc_type (k, TREE_TYPE (OVL_CURRENT (n)))) - return true; + cxx_binding *binding; + binding = IDENTIFIER_BINDING (name); + while (1) + { + if (binding->scope == b + && !(VAR_P (binding->value) + && DECL_DEAD_FOR_LOCAL (binding->value))) + return binding->value; + + if (b->kind == sk_cleanup) + b = b->level_chain; + else + break; + } } - return false; + return t; +} + +/* Wrapper for lookup_name_innermost_nonclass_level_1. */ + +tree +lookup_name_innermost_nonclass_level (tree name) +{ + tree ret; + bool subtime = timevar_cond_start (TV_NAME_LOOKUP); + ret = lookup_name_innermost_nonclass_level_1 (name); + timevar_cond_stop (TV_NAME_LOOKUP, subtime); + return ret; } -/* Performs Koenig lookup depending on arguments, where fns - are the functions found in normal lookup. */ -static cp_expr -lookup_arg_dependent_1 (tree name, tree fns, vec *args) +/* Returns true iff DECL is a block-scope extern declaration of a function + or variable. */ + +bool +is_local_extern (tree decl) { - struct arg_lookup k; + cxx_binding *binding; - /* Remove any hidden friend functions from the list of functions - found so far. They will be added back by arg_assoc_class as - appropriate. */ - fns = remove_hidden_names (fns); + /* For functions, this is easy. */ + if (TREE_CODE (decl) == FUNCTION_DECL) + return DECL_LOCAL_FUNCTION_P (decl); - k.name = name; - k.args = args; - k.functions = fns; - k.classes = make_tree_vector (); + if (!VAR_P (decl)) + return false; + if (!current_function_decl) + return false; - /* We previously performed an optimization here by setting - NAMESPACES to the current namespace when it was safe. However, DR - 164 says that namespaces that were already searched in the first - stage of template processing are searched again (potentially - picking up later definitions) in the second stage. */ - k.namespaces = make_tree_vector (); + /* For variables, this is not easy. We need to look at the binding stack + for the identifier to see whether the decl we have is a local. */ + for (binding = IDENTIFIER_BINDING (DECL_NAME (decl)); + binding && binding->scope->kind != sk_namespace; + binding = binding->previous) + if (binding->value == decl) + return LOCAL_BINDING_P (binding); - /* We used to allow duplicates and let joust discard them, but - since the above change for DR 164 we end up with duplicates of - all the functions found by unqualified lookup. So keep track - of which ones we've seen. */ - if (fns) - { - tree ovl; - /* We shouldn't be here if lookup found something other than - namespace-scope functions. */ - gcc_assert (DECL_NAMESPACE_SCOPE_P (OVL_CURRENT (fns))); - k.fn_set = new hash_set; - for (ovl = fns; ovl; ovl = OVL_NEXT (ovl)) - k.fn_set->add (OVL_CURRENT (ovl)); - } - else - k.fn_set = NULL; + return false; +} - arg_assoc_args_vec (&k, args); +/* Like lookup_name_innermost_nonclass_level, but for types. */ - fns = k.functions; - - if (fns - && !VAR_P (fns) - && !is_overloaded_fn (fns)) - { - error ("argument dependent lookup finds %q+D", fns); - error (" in call to %qD", name); - fns = error_mark_node; - } +static tree +lookup_type_current_level (tree name) +{ + tree t = NULL_TREE; - release_tree_vector (k.classes); - release_tree_vector (k.namespaces); - delete k.fn_set; - - return fns; -} + timevar_start (TV_NAME_LOOKUP); + gcc_assert (current_binding_level->kind != sk_namespace); -/* Wrapper for lookup_arg_dependent_1. */ + if (REAL_IDENTIFIER_TYPE_VALUE (name) != NULL_TREE + && REAL_IDENTIFIER_TYPE_VALUE (name) != global_type_node) + { + cp_binding_level *b = current_binding_level; + while (1) + { + if (purpose_member (name, b->type_shadowed)) + { + t = REAL_IDENTIFIER_TYPE_VALUE (name); + break; + } + if (b->kind == sk_cleanup) + b = b->level_chain; + else + break; + } + } -cp_expr -lookup_arg_dependent (tree name, tree fns, vec *args) -{ - cp_expr ret; - bool subtime; - subtime = timevar_cond_start (TV_NAME_LOOKUP); - ret = lookup_arg_dependent_1 (name, fns, args); - timevar_cond_stop (TV_NAME_LOOKUP, subtime); - return ret; + timevar_stop (TV_NAME_LOOKUP); + return t; } - /* Add namespace to using_directives. Return NULL_TREE if nothing was changed (i.e. there was already a directive), or the fresh TREE_LIST otherwise. */ @@ -6618,6 +6472,152 @@ pop_from_top_level (void) timevar_cond_stop (TV_NAME_LOOKUP, subtime); } +/* Push into the scope of the NAME namespace. If NAME is NULL_TREE, then we + select a name that is unique to this compilation unit. Returns FALSE if + pushdecl fails, TRUE otherwise. */ + +bool +push_namespace (tree name) +{ + tree d = NULL_TREE; + bool need_new = true; + bool implicit_use = false; + bool anon = !name; + + bool subtime = timevar_cond_start (TV_NAME_LOOKUP); + + /* We should not get here if the global_namespace is not yet constructed + nor if NAME designates the global namespace: The global scope is + constructed elsewhere. */ + gcc_assert (global_namespace != NULL && name != global_scope_name); + + if (anon) + { + name = get_anonymous_namespace_name(); + d = IDENTIFIER_NAMESPACE_VALUE (name); + if (d) + /* Reopening anonymous namespace. */ + need_new = false; + implicit_use = true; + } + else + { + /* Check whether this is an extended namespace definition. */ + d = IDENTIFIER_NAMESPACE_VALUE (name); + if (d != NULL_TREE && TREE_CODE (d) == NAMESPACE_DECL) + { + tree dna = DECL_NAMESPACE_ALIAS (d); + if (dna) + { + /* We do some error recovery for, eg, the redeclaration + of M here: + + namespace N {} + namespace M = N; + namespace M {} + + However, in nasty cases like: + + namespace N + { + namespace M = N; + namespace M {} + } + + we just error out below, in duplicate_decls. */ + if (NAMESPACE_LEVEL (dna)->level_chain + == current_binding_level) + { + error ("namespace alias %qD not allowed here, " + "assuming %qD", d, dna); + d = dna; + need_new = false; + } + } + else + need_new = false; + } + } + + if (need_new) + { + /* Make a new namespace, binding the name to it. */ + d = build_lang_decl (NAMESPACE_DECL, name, void_type_node); + DECL_CONTEXT (d) = FROB_CONTEXT (current_namespace); + /* The name of this namespace is not visible to other translation + units if it is an anonymous namespace or member thereof. */ + if (anon || decl_anon_ns_mem_p (current_namespace)) + TREE_PUBLIC (d) = 0; + else + TREE_PUBLIC (d) = 1; + if (pushdecl (d) == error_mark_node) + { + timevar_cond_stop (TV_NAME_LOOKUP, subtime); + return false; + } + if (anon) + { + /* Clear DECL_NAME for the benefit of debugging back ends. */ + SET_DECL_ASSEMBLER_NAME (d, name); + DECL_NAME (d) = NULL_TREE; + } + begin_scope (sk_namespace, d); + } + else + resume_scope (NAMESPACE_LEVEL (d)); + + if (implicit_use) + do_using_directive (d); + /* Enter the name space. */ + current_namespace = d; + + timevar_cond_stop (TV_NAME_LOOKUP, subtime); + return true; +} + +/* Pop from the scope of the current namespace. */ + +void +pop_namespace (void) +{ + gcc_assert (current_namespace != global_namespace); + current_namespace = CP_DECL_CONTEXT (current_namespace); + /* The binding level is not popped, as it might be re-opened later. */ + leave_scope (); +} + +/* Push into the scope of the namespace NS, even if it is deeply + nested within another namespace. */ + +void +push_nested_namespace (tree ns) +{ + if (ns == global_namespace) + push_to_top_level (); + else + { + push_nested_namespace (CP_DECL_CONTEXT (ns)); + push_namespace (DECL_NAME (ns)); + } +} + +/* Pop back from the scope of the namespace NS, which was previously + entered with push_nested_namespace. */ + +void +pop_nested_namespace (tree ns) +{ + bool subtime = timevar_cond_start (TV_NAME_LOOKUP); + gcc_assert (current_namespace == ns); + while (ns != global_namespace) + { + pop_namespace (); + ns = CP_DECL_CONTEXT (ns); + } + + pop_from_top_level (); + timevar_cond_stop (TV_NAME_LOOKUP, subtime); +} /* Pop off extraneous binding levels left over due to syntax errors. -- 2.30.2