From af92ab368ad709fcd26b360dc1cf4e50b0cd6185 Mon Sep 17 00:00:00 2001 From: Ollie Wild Date: Sun, 1 Jul 2007 20:49:29 +0000 Subject: [PATCH] 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 --- gcc/cp/ChangeLog | 11 +++ gcc/cp/decl.c | 6 ++ gcc/cp/name-lookup.c | 126 +++++++++++--------------- gcc/cp/parser.c | 8 +- gcc/testsuite/ChangeLog | 5 + gcc/testsuite/g++.dg/lookup/using16.C | 19 ++++ gcc/testsuite/g++.dg/lookup/using17.C | 15 +++ 7 files changed, 115 insertions(+), 75 deletions(-) create mode 100644 gcc/testsuite/g++.dg/lookup/using16.C create mode 100644 gcc/testsuite/g++.dg/lookup/using17.C diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index 86227638e03..aeb0e5c720a 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,14 @@ +2007-07-01 Ollie Wild + + * 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 PR c++/31724 diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c index 8f9db1eef89..1fae5f8cd6a 100644 --- a/gcc/cp/decl.c +++ b/gcc/cp/decl.c @@ -9933,6 +9933,12 @@ lookup_and_check_tag (enum tag_types tag_code, tree name, | DECL_SELF_REFERENCE_P (decl)); 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 return NULL_TREE; } diff --git a/gcc/cp/name-lookup.c b/gcc/cp/name-lookup.c index befc2d3a037..068725859ff 100644 --- a/gcc/cp/name-lookup.c +++ b/gcc/cp/name-lookup.c @@ -42,7 +42,6 @@ struct scope_binding { #define EMPTY_SCOPE_BINDING { NULL_TREE, NULL_TREE } 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 tree lookup_name_innermost_nonclass_level (tree); static tree push_overloaded_decl (tree, int, bool); @@ -3497,36 +3496,55 @@ ambiguous_decl (tree name, struct scope_binding *old, cxx_binding *new, { tree val, type; 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. */ val = new->value; 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_CLASS_TEMPLATE_P (val)) - || hidden_name_p (val)) - val = NULL_TREE; - break; - case TYPE_DECL: - if (LOOKUP_NAMESPACES_ONLY (flags) || hidden_name_p (val)) - 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) || hidden_name_p (val)) - val = NULL_TREE; - break; - default: - if (LOOKUP_QUALIFIERS_ONLY (flags)) - val = NULL_TREE; - } + { + if (hidden_name_p (val) && !(flags & LOOKUP_HIDDEN)) + val = NULL_TREE; + else + 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_CLASS_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; + } + } + + /* If val is hidden, shift down any class or enumeration name. */ + if (!val) + { + val = type; + type = NULL_TREE; + } if (!old->value) old->value = val; @@ -3537,14 +3555,11 @@ ambiguous_decl (tree name, struct scope_binding *old, cxx_binding *new, else { 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; } } - /* ... and copy the type. */ - type = new->type; - if (LOOKUP_NAMESPACES_ONLY (flags) || (type && hidden_name_p (type))) - type = NULL_TREE; + if (!old->type) old->type = type; else if (type && old->type != type) @@ -3644,36 +3659,6 @@ remove_hidden_names (tree 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, considering using-directives. */ @@ -3685,24 +3670,17 @@ unqualified_namespace_lookup (tree name, int flags) tree siter; struct cp_binding_level *level; tree val = NULL_TREE; - struct scope_binding binding = EMPTY_SCOPE_BINDING; timevar_push (TV_NAME_LOOKUP); for (; !val; scope = CP_DECL_CONTEXT (scope)) { + struct scope_binding binding = EMPTY_SCOPE_BINDING; cxx_binding *b = cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (scope), name); if (b) - { - 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; - } + ambiguous_decl (name, &binding, b, flags); /* Add all _DECLs seen through local using-directives. */ for (level = current_binding_level; @@ -3727,7 +3705,7 @@ unqualified_namespace_lookup (tree name, int flags) siter = CP_DECL_CONTEXT (siter); } - val = select_decl (&binding, flags); + val = binding.value; if (scope == global_namespace) break; } @@ -3757,7 +3735,7 @@ lookup_qualified_name (tree scope, tree name, bool is_type_p, bool complain) if (is_type_p) flags |= LOOKUP_PREFER_TYPES; if (qualified_lookup_using_namespace (name, scope, &binding, flags)) - t = select_decl (&binding, flags); + t = binding.value; } else if (is_aggr_type (scope, complain)) t = lookup_member (scope, name, 2, is_type_p); diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c index ca6620c71b7..484c6b5a5cc 100644 --- a/gcc/cp/parser.c +++ b/gcc/cp/parser.c @@ -10855,13 +10855,19 @@ cp_parser_elaborated_type_specifier (cp_parser* parser, if (parser->scope) { tree decl; + tree ambiguous_decls; decl = cp_parser_lookup_name (parser, identifier, tag_type, /*is_template=*/false, /*is_namespace=*/false, /*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 TEMPLATE_DECL tree node here. However, we need to check diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 6a556cd57b8..5b390d32a19 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2007-07-01 Ollie Wild + + * g++.dg/lookup/using16.C: New test. + * g++.dg/lookup/using17.C: New test. + 2007-07-01 Janne Blomqvist PR fortran/32239 diff --git a/gcc/testsuite/g++.dg/lookup/using16.C b/gcc/testsuite/g++.dg/lookup/using16.C new file mode 100644 index 00000000000..e373f377897 --- /dev/null +++ b/gcc/testsuite/g++.dg/lookup/using16.C @@ -0,0 +1,19 @@ +// Copyright (C) 2007 Free Software Foundation +// Contributed by Ollie Wild +// { 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 } diff --git a/gcc/testsuite/g++.dg/lookup/using17.C b/gcc/testsuite/g++.dg/lookup/using17.C new file mode 100644 index 00000000000..59888852c42 --- /dev/null +++ b/gcc/testsuite/g++.dg/lookup/using17.C @@ -0,0 +1,15 @@ +// Copyright (C) 2007 Free Software Foundation +// Contributed by Ollie Wild +// { 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 } -- 2.30.2