Commit af92ab36 by Ollie Wild Committed by Ollie Wild

name-lookup.c (ambiguous_decl): Fix case when new->value is hidden.

	* name-lookup.c (ambiguous_decl): Fix case when new->value is hidden.
	(select_decl): Remove function.
	(unqualified_namespace_lookup): Populate binding by calling
	ambiguous_decl.  Remove select_decl call.
	(lookup_qualified_name): Remove select_decl call.
	* decl.c (lookup_and_check_tag): Check for ambiguous references.
	* parser.c (cp_parser_elaborated_type_specifier): Skip redundant error
	generation when name lookup is ambiguous.

	* g++.dg/lookup/using16.C: New test.
	* g++.dg/lookup/using17.C: New test.

From-SVN: r126177
parent 31c97dfe
2007-07-01 Ollie Wild <aaw@google.com>
* name-lookup.c (ambiguous_decl): Fix case when new->value is hidden.
(select_decl): Remove function.
(unqualified_namespace_lookup): Populate binding by calling
ambiguous_decl. Remove select_decl call.
(lookup_qualified_name): Remove select_decl call.
* decl.c (lookup_and_check_tag): Check for ambiguous references.
* parser.c (cp_parser_elaborated_type_specifier): Skip redundant error
generation when name lookup is ambiguous.
2007-06-29 Douglas Gregor <doug.gregor@gmail.com> 2007-06-29 Douglas Gregor <doug.gregor@gmail.com>
PR c++/31724 PR c++/31724
......
...@@ -9933,6 +9933,12 @@ lookup_and_check_tag (enum tag_types tag_code, tree name, ...@@ -9933,6 +9933,12 @@ lookup_and_check_tag (enum tag_types tag_code, tree name,
| DECL_SELF_REFERENCE_P (decl)); | DECL_SELF_REFERENCE_P (decl));
return t; return t;
} }
else if (decl && TREE_CODE (decl) == TREE_LIST)
{
error ("reference to %qD is ambiguous", name);
print_candidates (decl);
return error_mark_node;
}
else else
return NULL_TREE; return NULL_TREE;
} }
......
...@@ -42,7 +42,6 @@ struct scope_binding { ...@@ -42,7 +42,6 @@ struct scope_binding {
#define EMPTY_SCOPE_BINDING { NULL_TREE, NULL_TREE } #define EMPTY_SCOPE_BINDING { NULL_TREE, NULL_TREE }
static cxx_scope *innermost_nonclass_level (void); static cxx_scope *innermost_nonclass_level (void);
static tree select_decl (const struct scope_binding *, int);
static cxx_binding *binding_for_name (cxx_scope *, tree); static cxx_binding *binding_for_name (cxx_scope *, tree);
static tree lookup_name_innermost_nonclass_level (tree); static tree lookup_name_innermost_nonclass_level (tree);
static tree push_overloaded_decl (tree, int, bool); static tree push_overloaded_decl (tree, int, bool);
...@@ -3497,36 +3496,55 @@ ambiguous_decl (tree name, struct scope_binding *old, cxx_binding *new, ...@@ -3497,36 +3496,55 @@ ambiguous_decl (tree name, struct scope_binding *old, cxx_binding *new,
{ {
tree val, type; tree val, type;
gcc_assert (old != NULL); gcc_assert (old != NULL);
/* Copy the type. */
type = new->type;
if (LOOKUP_NAMESPACES_ONLY (flags)
|| (type && hidden_name_p (type) && !(flags & LOOKUP_HIDDEN)))
type = NULL_TREE;
/* Copy the value. */ /* Copy the value. */
val = new->value; val = new->value;
if (val) if (val)
switch (TREE_CODE (val)) {
{ if (hidden_name_p (val) && !(flags & LOOKUP_HIDDEN))
case TEMPLATE_DECL: val = NULL_TREE;
/* If we expect types or namespaces, and not templates, else
or this is not a template class. */ switch (TREE_CODE (val))
if ((LOOKUP_QUALIFIERS_ONLY (flags) {
&& !DECL_CLASS_TEMPLATE_P (val)) case TEMPLATE_DECL:
|| hidden_name_p (val)) /* If we expect types or namespaces, and not templates,
val = NULL_TREE; or this is not a template class. */
break; if ((LOOKUP_QUALIFIERS_ONLY (flags)
case TYPE_DECL: && !DECL_CLASS_TEMPLATE_P (val)))
if (LOOKUP_NAMESPACES_ONLY (flags) || hidden_name_p (val)) val = NULL_TREE;
val = NULL_TREE; break;
break; case TYPE_DECL:
case NAMESPACE_DECL: if (LOOKUP_NAMESPACES_ONLY (flags)
if (LOOKUP_TYPES_ONLY (flags)) || (type && (flags & LOOKUP_PREFER_TYPES)))
val = NULL_TREE; val = NULL_TREE;
break; break;
case FUNCTION_DECL: case NAMESPACE_DECL:
/* Ignore built-in functions that are still anticipated. */ if (LOOKUP_TYPES_ONLY (flags))
if (LOOKUP_QUALIFIERS_ONLY (flags) || hidden_name_p (val)) val = NULL_TREE;
val = NULL_TREE; break;
break; case FUNCTION_DECL:
default: /* Ignore built-in functions that are still anticipated. */
if (LOOKUP_QUALIFIERS_ONLY (flags)) if (LOOKUP_QUALIFIERS_ONLY (flags))
val = NULL_TREE; val = NULL_TREE;
} break;
default:
if (LOOKUP_QUALIFIERS_ONLY (flags))
val = NULL_TREE;
}
}
/* If val is hidden, shift down any class or enumeration name. */
if (!val)
{
val = type;
type = NULL_TREE;
}
if (!old->value) if (!old->value)
old->value = val; old->value = val;
...@@ -3537,14 +3555,11 @@ ambiguous_decl (tree name, struct scope_binding *old, cxx_binding *new, ...@@ -3537,14 +3555,11 @@ ambiguous_decl (tree name, struct scope_binding *old, cxx_binding *new,
else else
{ {
old->value = tree_cons (NULL_TREE, old->value, old->value = tree_cons (NULL_TREE, old->value,
build_tree_list (NULL_TREE, new->value)); build_tree_list (NULL_TREE, val));
TREE_TYPE (old->value) = error_mark_node; TREE_TYPE (old->value) = error_mark_node;
} }
} }
/* ... and copy the type. */
type = new->type;
if (LOOKUP_NAMESPACES_ONLY (flags) || (type && hidden_name_p (type)))
type = NULL_TREE;
if (!old->type) if (!old->type)
old->type = type; old->type = type;
else if (type && old->type != type) else if (type && old->type != type)
...@@ -3644,36 +3659,6 @@ remove_hidden_names (tree fns) ...@@ -3644,36 +3659,6 @@ remove_hidden_names (tree fns)
return fns; return fns;
} }
/* Select the right _DECL from multiple choices. */
static tree
select_decl (const struct scope_binding *binding, int flags)
{
tree val;
val = binding->value;
timevar_push (TV_NAME_LOOKUP);
if (LOOKUP_NAMESPACES_ONLY (flags))
{
/* We are not interested in types. */
if (val && (TREE_CODE (val) == NAMESPACE_DECL
|| TREE_CODE (val) == TREE_LIST))
POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
}
/* If looking for a type, or if there is no non-type binding, select
the value binding. */
if (binding->type && (!val || (flags & LOOKUP_PREFER_TYPES)))
val = binding->type;
/* Don't return non-types if we really prefer types. */
else if (val && LOOKUP_TYPES_ONLY (flags)
&& ! DECL_DECLARES_TYPE_P (val))
val = NULL_TREE;
POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
}
/* Unscoped lookup of a global: iterate over current namespaces, /* Unscoped lookup of a global: iterate over current namespaces,
considering using-directives. */ considering using-directives. */
...@@ -3685,24 +3670,17 @@ unqualified_namespace_lookup (tree name, int flags) ...@@ -3685,24 +3670,17 @@ unqualified_namespace_lookup (tree name, int flags)
tree siter; tree siter;
struct cp_binding_level *level; struct cp_binding_level *level;
tree val = NULL_TREE; tree val = NULL_TREE;
struct scope_binding binding = EMPTY_SCOPE_BINDING;
timevar_push (TV_NAME_LOOKUP); timevar_push (TV_NAME_LOOKUP);
for (; !val; scope = CP_DECL_CONTEXT (scope)) for (; !val; scope = CP_DECL_CONTEXT (scope))
{ {
struct scope_binding binding = EMPTY_SCOPE_BINDING;
cxx_binding *b = cxx_binding *b =
cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (scope), name); cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
if (b) if (b)
{ ambiguous_decl (name, &binding, b, flags);
if (b->value
&& ((flags & LOOKUP_HIDDEN) || !hidden_name_p (b->value)))
binding.value = b->value;
if (b->type
&& ((flags & LOOKUP_HIDDEN) || !hidden_name_p (b->type)))
binding.type = b->type;
}
/* Add all _DECLs seen through local using-directives. */ /* Add all _DECLs seen through local using-directives. */
for (level = current_binding_level; for (level = current_binding_level;
...@@ -3727,7 +3705,7 @@ unqualified_namespace_lookup (tree name, int flags) ...@@ -3727,7 +3705,7 @@ unqualified_namespace_lookup (tree name, int flags)
siter = CP_DECL_CONTEXT (siter); siter = CP_DECL_CONTEXT (siter);
} }
val = select_decl (&binding, flags); val = binding.value;
if (scope == global_namespace) if (scope == global_namespace)
break; break;
} }
...@@ -3757,7 +3735,7 @@ lookup_qualified_name (tree scope, tree name, bool is_type_p, bool complain) ...@@ -3757,7 +3735,7 @@ lookup_qualified_name (tree scope, tree name, bool is_type_p, bool complain)
if (is_type_p) if (is_type_p)
flags |= LOOKUP_PREFER_TYPES; flags |= LOOKUP_PREFER_TYPES;
if (qualified_lookup_using_namespace (name, scope, &binding, flags)) if (qualified_lookup_using_namespace (name, scope, &binding, flags))
t = select_decl (&binding, flags); t = binding.value;
} }
else if (is_aggr_type (scope, complain)) else if (is_aggr_type (scope, complain))
t = lookup_member (scope, name, 2, is_type_p); t = lookup_member (scope, name, 2, is_type_p);
......
...@@ -10855,13 +10855,19 @@ cp_parser_elaborated_type_specifier (cp_parser* parser, ...@@ -10855,13 +10855,19 @@ cp_parser_elaborated_type_specifier (cp_parser* parser,
if (parser->scope) if (parser->scope)
{ {
tree decl; tree decl;
tree ambiguous_decls;
decl = cp_parser_lookup_name (parser, identifier, decl = cp_parser_lookup_name (parser, identifier,
tag_type, tag_type,
/*is_template=*/false, /*is_template=*/false,
/*is_namespace=*/false, /*is_namespace=*/false,
/*check_dependency=*/true, /*check_dependency=*/true,
/*ambiguous_decls=*/NULL); &ambiguous_decls);
/* If the lookup was ambiguous, an error will already have been
issued. */
if (ambiguous_decls)
return error_mark_node;
/* If we are parsing friend declaration, DECL may be a /* If we are parsing friend declaration, DECL may be a
TEMPLATE_DECL tree node here. However, we need to check TEMPLATE_DECL tree node here. However, we need to check
......
2007-07-01 Ollie Wild <aaw@google.com>
* g++.dg/lookup/using16.C: New test.
* g++.dg/lookup/using17.C: New test.
2007-07-01 Janne Blomqvist <jb@gcc.gnu.org> 2007-07-01 Janne Blomqvist <jb@gcc.gnu.org>
PR fortran/32239 PR fortran/32239
// Copyright (C) 2007 Free Software Foundation
// Contributed by Ollie Wild <aaw@google.com>
// { dg-do compile }
namespace M {
struct S {}; // { dg-error "candidates are: struct M::S" "candidate 1" }
}
namespace N {
int S;
struct S {}; // { dg-error "struct N::S" "candidate 2" }
}
using namespace M;
using namespace N;
struct ::S s; // { dg-bogus "ambiguous.*ambiguous" "duplicate error" }
// { dg-error "reference to 'S' is ambiguous" "" { target *-*-* } 17 }}
// { dg-error "invalid type" "" { target *-*-* } 17 }
// Copyright (C) 2007 Free Software Foundation
// Contributed by Ollie Wild <aaw@google.com>
// { dg-do compile }
namespace M {
struct S {}; // { dg-error "struct M::S" "candidate 2" }
}
int S;
struct S {}; // { dg-error "candidates are: struct S" "candidate 1" }
using namespace M;
struct S s; // { dg-error "reference to 'S' is ambiguous" "" }
// { dg-error "invalid type in declaration" "" { target *-*-* } 14 }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment