/* Definitions for C++ name lookup routines.
- Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
+ Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
Free Software Foundation, Inc.
Contributed by Gabriel Dos Reis <gdr@integrable-solutions.net>
#include "debug.h"
#include "c-family/c-pragma.h"
#include "params.h"
+#include "pointer-set.h"
/* The bindings for a particular name in a particular scope. */
cp_class_binding *cb;
cxx_binding *binding;
- if (VEC_length (cp_class_binding, scope->class_shadowed))
- {
- cp_class_binding *old_base;
- old_base = VEC_index (cp_class_binding, scope->class_shadowed, 0);
- if (VEC_reserve (cp_class_binding, gc, scope->class_shadowed, 1))
- {
- /* Fixup the current bindings, as they might have moved. */
- size_t i;
-
- FOR_EACH_VEC_ELT (cp_class_binding, scope->class_shadowed, i, cb)
- {
- cxx_binding **b;
- b = &IDENTIFIER_BINDING (cb->identifier);
- while (*b != &old_base[i].base)
- b = &((*b)->previous);
- *b = &cb->base;
- }
- }
- cb = VEC_quick_push (cp_class_binding, scope->class_shadowed, NULL);
- }
- else
cb = VEC_safe_push (cp_class_binding, gc, scope->class_shadowed, NULL);
cb->identifier = name;
- binding = &cb->base;
+ cb->base = binding = cxx_binding_make (value, type);
binding->scope = scope;
- cxx_binding_init (binding, value, type);
return binding;
}
&& DECL_ANTICIPATED (bval)
&& !DECL_HIDDEN_FRIEND_P (bval)))
binding->value = decl;
- else if (TREE_CODE (bval) == TYPE_DECL && DECL_ARTIFICIAL (bval))
+ else if (TREE_CODE (bval) == TYPE_DECL && DECL_ARTIFICIAL (bval)
+ && (TREE_CODE (decl) != TYPE_DECL
+ || same_type_p (TREE_TYPE (decl), TREE_TYPE (bval))))
{
/* The old binding was a type name. It was placed in
VALUE field because it was thought, at the point it was
necessary. */
TREE_CHAIN (decl) = b->names;
b->names = decl;
- b->names_size++;
/* If appropriate, add decl to separate list of statics. We
include extern variables because they might turn out to be
else
{
/* Here to install a non-global value. */
- tree oldlocal = innermost_non_namespace_value (name);
tree oldglobal = IDENTIFIER_NAMESPACE_VALUE (name);
+ tree oldlocal = NULL_TREE;
+ cxx_scope *oldscope = NULL;
+ cxx_binding *oldbinding = outer_binding (name, NULL, true);
+ if (oldbinding)
+ {
+ oldlocal = oldbinding->value;
+ oldscope = oldbinding->scope;
+ }
if (need_new_binding)
{
|| (TREE_CODE (oldlocal) == TYPE_DECL
&& (!DECL_ARTIFICIAL (oldlocal)
|| TREE_CODE (x) == TYPE_DECL)))
- /* Don't check the `this' parameter or internally generated
- vars unless it's an implicit typedef (see
- create_implicit_typedef in decl.c). */
- && (!DECL_ARTIFICIAL (oldlocal)
- || DECL_IMPLICIT_TYPEDEF_P (oldlocal))
/* Don't check for internally generated vars unless
it's an implicit typedef (see create_implicit_typedef
in decl.c). */
}
}
}
+ /* Error if redeclaring a local declared in a
+ for-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 (TREE_CODE (oldlocal) == VAR_DECL
+ && oldscope == current_binding_level->level_chain
+ && (oldscope->kind == sk_cond
+ || oldscope->kind == sk_for))
+ {
+ error ("redeclaration of %q#D", x);
+ error ("%q+#D previously declared here", oldlocal);
+ }
if (warn_shadow && !nowarn)
{
if (TREE_CODE (oldlocal) == PARM_DECL)
warning_at (input_location, OPT_Wshadow,
"declaration of %q#D shadows a parameter", x);
+ else if (is_capture_proxy (oldlocal))
+ warning_at (input_location, OPT_Wshadow,
+ "declaration of %qD shadows a lambda capture",
+ x);
else
warning_at (input_location, OPT_Wshadow,
"declaration of %qD shadows a previous local",
case sk_try:
case sk_catch:
case sk_for:
+ case sk_cond:
case sk_class:
case sk_scoped_enum:
case sk_function_parms:
}
/* Same as pushdecl, but define X in binding-level LEVEL. We rely on the
- caller to set DECL_CONTEXT properly. */
+ caller to set DECL_CONTEXT properly.
+
+ 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. */
static tree
pushdecl_with_scope_1 (tree x, cxx_scope *level, bool is_friend)
if (decl == NULL_TREE)
return;
- if (building_stmt_tree ()
+ if (building_stmt_list_p ()
&& at_function_scope_p ())
add_decl_expr (decl);
if (level->class_shadowed)
{
FOR_EACH_VEC_ELT (cp_class_binding, level->class_shadowed, i, cb)
- IDENTIFIER_BINDING (cb->identifier) = cb->base.previous;
+ {
+ IDENTIFIER_BINDING (cb->identifier) = cb->base->previous;
+ cxx_binding_free (cb->base);
+ }
ggc_free (level->class_shadowed);
level->class_shadowed = NULL;
}
pushdecl (alias);
/* Emit debug info for namespace alias. */
- if (!building_stmt_tree ())
+ if (!building_stmt_list_p ())
(*debug_hooks->global_decl) (alias);
}
gcc_assert (TREE_CODE (name_space) == NAMESPACE_DECL);
- if (building_stmt_tree ())
+ if (building_stmt_list_p ())
add_stmt (build_stmt (input_location, USING_STMT, name_space));
name_space = ORIGINAL_NAMESPACE (name_space);
return true;
if (flags & (LOOKUP_PREFER_NAMESPACES | LOOKUP_PREFER_TYPES))
return false;
- /* In unevaluated context, look past normal capture fields. */
- if (cp_unevaluated_operand && TREE_CODE (val) == FIELD_DECL
- && DECL_NORMAL_CAPTURE_P (val))
- return false;
- /* None of the lookups that use qualify_lookup want the op() from the
- lambda; they want the one from the enclosing class. */
- if (TREE_CODE (val) == FUNCTION_DECL && LAMBDA_FUNCTION_P (val))
+ /* Look through lambda things that we shouldn't be able to see. */
+ if (is_lambda_ignored_entity (val))
return false;
return true;
}
VEC(tree,gc) *namespaces;
VEC(tree,gc) *classes;
tree functions;
+ struct pointer_set_t *fn_set;
};
static bool arg_assoc (struct arg_lookup*, tree);
static bool
add_function (struct arg_lookup *k, tree fn)
{
- /* We used to check here to see if the function was already in the list,
- but that's O(n^2), which is just too expensive for function lookup.
- Now we deal with the occasional duplicate in joust. In doing this, we
- assume that the number of duplicates will be small compared to the
- total number of functions being compared, which should usually be the
- case. */
-
if (!is_overloaded_fn (fn))
/* All names except those of (possibly overloaded) functions and
function templates are ignored. */;
+ else if (k->fn_set && pointer_set_insert (k->fn_set, fn))
+ /* It's already in the list. */;
else if (!k->functions)
k->functions = fn;
else if (fn == k->functions)
picking up later definitions) in the second stage. */
k.namespaces = make_tree_vector ();
+ /* 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 = pointer_set_create ();
+ for (ovl = fns; ovl; ovl = OVL_NEXT (ovl))
+ pointer_set_insert (k.fn_set, OVL_CURRENT (ovl));
+ }
+ else
+ k.fn_set = NULL;
+
if (include_std)
arg_assoc_namespace (&k, std_node);
arg_assoc_args_vec (&k, args);
release_tree_vector (k.classes);
release_tree_vector (k.namespaces);
+ if (k.fn_set)
+ pointer_set_destroy (k.fn_set);
return fns;
}
for (t = OVL_CURRENT (t); t; t = OVL_NEXT (t))
if (TREE_CODE (t) != TEMPLATE_DECL)
{
- if (building_stmt_tree ())
+ if (building_stmt_list_p ())
add_stmt (build_stmt (input_location, USING_STMT, t));
else
(*debug_hooks->imported_module_or_decl) (t, NULL_TREE, context, false);