OSDN Git Service

* dwarf2out.c (dwarf2out_imported_module_or_decl_1): Allow
[pf3gnuchains/gcc-fork.git] / gcc / cp / name-lookup.c
index 8efcfed..eec7c44 100644 (file)
@@ -1,12 +1,13 @@
 /* Definitions for C++ name lookup routines.
-   Copyright (C) 2003, 2004, 2005, 2006  Free Software Foundation, Inc.
+   Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009
+   Free Software Foundation, Inc.
    Contributed by Gabriel Dos Reis <gdr@integrable-solutions.net>
 
 This file is part of GCC.
 
 GCC is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
-the Free Software Foundation; either version 2, or (at your option)
+the Free Software Foundation; either version 3, or (at your option)
 any later version.
 
 GCC is distributed in the hope that it will be useful,
@@ -15,9 +16,8 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
-along with GCC; see the file COPYING.  If not, write to
-the Free Software Foundation, 51 Franklin Street, Fifth Floor,
-Boston, MA 02110-1301, USA.  */
+along with GCC; see the file COPYING3.  If not see
+<http://www.gnu.org/licenses/>.  */
 
 #include "config.h"
 #include "system.h"
@@ -42,9 +42,7 @@ 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);
 static bool lookup_using_namespace (tree, struct scope_binding *, tree,
                                    tree, int);
@@ -52,6 +50,7 @@ static bool qualified_lookup_using_namespace (tree, tree,
                                              struct scope_binding *, int);
 static tree lookup_type_current_level (tree);
 static tree push_using_directive (tree);
+static cxx_binding* lookup_extern_c_fun_binding_in_all_ns (tree);
 
 /* The :: namespace.  */
 
@@ -132,7 +131,7 @@ struct binding_table_s GTY(())
   binding_entry * GTY((length ("%h.chain_count"))) chain;
 
   /* The number of chains in this table.  This is the length of the
-     the member "chain" considered as an array.  */
+     member "chain" considered as an array.  */
   size_t chain_count;
 
   /* Number of "binding_entry"s in this table.  */
@@ -537,17 +536,16 @@ supplement_binding (cxx_binding *binding, tree decl)
 static void
 add_decl_to_level (tree decl, cxx_scope *b)
 {
+  /* We used to record virtual tables as if they were ordinary
+     variables, but no longer do so.  */
+  gcc_assert (!(TREE_CODE (decl) == VAR_DECL && DECL_VIRTUAL_P (decl)));
+
   if (TREE_CODE (decl) == NAMESPACE_DECL
       && !DECL_NAMESPACE_ALIAS (decl))
     {
       TREE_CHAIN (decl) = b->namespaces;
       b->namespaces = decl;
     }
-  else if (TREE_CODE (decl) == VAR_DECL && DECL_VIRTUAL_P (decl))
-    {
-      TREE_CHAIN (decl) = b->vtables;
-      b->vtables = decl;
-    }
   else
     {
       /* We build up the list in reverse order, and reverse it later if
@@ -606,6 +604,13 @@ pushdecl_maybe_friend (tree x, bool is_friend)
             scope of the current namespace, not the current
             function.  */
          && !(TREE_CODE (x) == VAR_DECL && 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;
 
@@ -714,11 +719,10 @@ pushdecl_maybe_friend (tree x, bool is_friend)
            }
          else if (TREE_CODE (t) == PARM_DECL)
            {
-             gcc_assert (DECL_CONTEXT (t));
-
              /* Check for duplicate params.  */
-             if (duplicate_decls (x, t, is_friend))
-               POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
+             tree d = duplicate_decls (x, t, is_friend);
+             if (d)
+               POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, d);
            }
          else if ((DECL_EXTERN_C_FUNCTION_P (x)
                    || DECL_FUNCTION_TEMPLATE_P (x))
@@ -726,10 +730,10 @@ pushdecl_maybe_friend (tree x, bool is_friend)
            /* Don't do anything just yet.  */;
          else if (t == wchar_decl_node)
            {
-             if (pedantic && ! DECL_IN_SYSTEM_HEADER (x))
-               pedwarn ("redeclaration of %<wchar_t%> as %qT",
+             if (! DECL_IN_SYSTEM_HEADER (x))
+               pedwarn (input_location, OPT_pedantic, "redeclaration of %<wchar_t%> as %qT",
                         TREE_TYPE (x));
-
+             
              /* Throw away the redeclaration.  */
              POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
            }
@@ -766,6 +770,49 @@ pushdecl_maybe_friend (tree x, bool is_friend)
            }
        }
 
+      /* 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_IN_SYSTEM_HEADER (x))
+       {
+         cxx_binding *function_binding =
+             lookup_extern_c_fun_binding_in_all_ns (x);
+         if (function_binding
+              && !DECL_IN_SYSTEM_HEADER (function_binding->value))
+           {
+             tree previous = function_binding->value;
+
+             /* 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,
+                                         true))
+                   {
+                     pedwarn (input_location, 0, "declaration of %q#D with C language linkage",
+                              x);
+                     pedwarn (input_location, 0, "conflicts with previous declaration %q+#D",
+                              previous);
+                     pedwarn (input_location, 0, "due to different exception specifications");
+                     POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
+                   }
+               }
+           }
+       }
+
       if (TREE_CODE (x) == FUNCTION_DECL || DECL_FUNCTION_TEMPLATE_P (x))
        check_default_args (x);
 
@@ -849,8 +896,8 @@ pushdecl_maybe_friend (tree x, bool is_friend)
              && TREE_CODE (decl) == TREE_CODE (x)
              && !same_type_p (TREE_TYPE (x), TREE_TYPE (decl)))
            {
-             pedwarn ("type mismatch with previous external decl of %q#D", x);
-             pedwarn ("previous external decl of %q+#D", decl);
+             permerror (input_location, "type mismatch with previous external decl of %q#D", x);
+             permerror (input_location, "previous external decl of %q+#D", decl);
            }
        }
 
@@ -1168,7 +1215,7 @@ check_for_out_of_scope_variable (tree decl)
 
   if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
     {
-      error ("name lookup of %qD changed for new ISO %<for%> scoping",
+      error ("name lookup of %qD changed for ISO %<for%> scoping",
             DECL_NAME (decl));
       error ("  cannot use obsolete binding at %q+D because "
             "it has a destructor", decl);
@@ -1176,9 +1223,19 @@ check_for_out_of_scope_variable (tree decl)
     }
   else
     {
-      pedwarn ("name lookup of %qD changed for new ISO %<for%> scoping",
-              DECL_NAME (decl));
-      pedwarn ("  using obsolete binding at %q+D", decl);
+      permerror (input_location, "name lookup of %qD changed for ISO %<for%> scoping",
+                DECL_NAME (decl));
+      if (flag_permissive)
+        permerror (input_location, "  using obsolete binding at %q+D", decl);
+      else
+       {
+         static bool hint;
+         if (!hint)
+           {
+             inform (input_location, "(if you use %<-fpermissive%> G++ will accept your code)");
+             hint = true;
+           }
+       }
     }
 
   return decl;
@@ -1278,7 +1335,7 @@ push_binding_level (struct cp_binding_level *scope)
 
 /* 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); it is NULL otherwise.  */
+   function, C++0x enumeration); it is NULL otherwise.  */
 
 cxx_scope *
 begin_scope (scope_kind kind, tree entity)
@@ -1313,6 +1370,7 @@ begin_scope (scope_kind kind, tree entity)
     case sk_catch:
     case sk_for:
     case sk_class:
+    case sk_scoped_enum:
     case sk_function_parms:
     case sk_omp:
       scope->keep = keep_next_level_flag;
@@ -1366,11 +1424,6 @@ leave_scope (void)
       is_class_level = 0;
     }
 
-#ifdef HANDLE_PRAGMA_VISIBILITY
-  if (scope->has_visibility)
-    pop_visibility ();
-#endif
-
   /* Move one nesting level up.  */
   current_binding_level = scope->level_chain;
 
@@ -1495,7 +1548,8 @@ 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->names != NULL_TREE
+         || current_binding_level->using_directives);
 }
 
 /* Returns the kind of the innermost scope.  */
@@ -1737,13 +1791,16 @@ constructor_name (tree type)
   return name;
 }
 
-/* Returns TRUE if NAME is the name for the constructor for TYPE.  */
+/* Returns TRUE if NAME is the name for the constructor for TYPE,
+   which must be a class type.  */
 
 bool
 constructor_name_p (tree name, tree type)
 {
   tree ctor_name;
 
+  gcc_assert (MAYBE_CLASS_TYPE_P (type));
+
   if (!name)
     return false;
 
@@ -1826,6 +1883,39 @@ binding_for_name (cxx_scope *scope, tree name)
   return result;
 }
 
+/* Walk through the bindings associated to the name of FUNCTION,
+   and return the first binding that declares 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 cxx_binding*
+lookup_extern_c_fun_binding_in_all_ns (tree function)
+{
+  tree name;
+  cxx_binding *iter;
+
+  gcc_assert (function && TREE_CODE (function) == FUNCTION_DECL);
+
+  name = DECL_NAME (function);
+  gcc_assert (name && TREE_CODE (name) == IDENTIFIER_NODE);
+
+  for (iter = IDENTIFIER_NAMESPACE_BINDINGS (name);
+       iter;
+       iter = iter->previous)
+    {
+      if (iter->value
+         && TREE_CODE (iter->value) == FUNCTION_DECL
+         && DECL_EXTERN_C_P (iter->value)
+         && !DECL_ARTIFICIAL (iter->value))
+       {
+         return iter;
+       }
+    }
+  return NULL;
+}
+
 /* 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
@@ -1921,10 +2011,10 @@ push_overloaded_decl (tree decl, int flags, bool is_friend)
       if (TREE_CODE (old) == TYPE_DECL && DECL_ARTIFICIAL (old))
        {
          tree t = TREE_TYPE (old);
-         if (IS_AGGR_TYPE (t) && warn_shadow
+         if (MAYBE_CLASS_TYPE_P (t) && warn_shadow
              && (! DECL_IN_SYSTEM_HEADER (decl)
                  || ! DECL_IN_SYSTEM_HEADER (old)))
-           warning (0, "%q#D hides constructor for %q#T", decl, t);
+           warning (OPT_Wshadow, "%q#D hides constructor for %q#T", decl, t);
          old = NULL_TREE;
        }
       else if (is_overloaded_fn (old))
@@ -2100,6 +2190,20 @@ do_nonmember_using_decl (tree scope, tree name, tree oldval, tree oldtype,
       return;
     }
 
+  /* 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))
+    {
+      oldtype = oldval;
+      oldval = NULL_TREE;
+    }
+
+  if (decls.value && DECL_IMPLICIT_TYPEDEF_P (decls.value))
+    {
+      decls.type = decls.value;
+      decls.value = NULL_TREE;
+    }
+
   /* It is impossible to overload a built-in function; any explicit
      declaration eliminates the built-in declaration.  So, if OLDVAL
      is a built-in, then we can just pretend it isn't there.  */
@@ -2109,95 +2213,110 @@ do_nonmember_using_decl (tree scope, tree name, tree oldval, tree oldtype,
       && !DECL_HIDDEN_FRIEND_P (oldval))
     oldval = NULL_TREE;
 
-  /* Check for using functions.  */
-  if (decls.value && is_overloaded_fn (decls.value))
+  if (decls.value)
     {
-      tree tmp, tmp1;
-
-      if (oldval && !is_overloaded_fn (oldval))
-       {
-         if (!DECL_IMPLICIT_TYPEDEF_P (oldval))
-           error ("%qD is already declared in this scope", name);
-         oldval = NULL_TREE;
-       }
-
-      *newval = oldval;
-      for (tmp = decls.value; tmp; tmp = OVL_NEXT (tmp))
+      /* Check for using functions.  */
+      if (is_overloaded_fn (decls.value))
        {
-         tree new_fn = OVL_CURRENT (tmp);
+         tree tmp, tmp1;
 
-         /* [namespace.udecl]
+         if (oldval && !is_overloaded_fn (oldval))
+           {
+             error ("%qD is already declared in this scope", name);
+             oldval = NULL_TREE;
+           }
 
-            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))
+         *newval = oldval;
+         for (tmp = decls.value; tmp; tmp = OVL_NEXT (tmp))
            {
-             tree old_fn = OVL_CURRENT (tmp1);
-
-             if (new_fn == old_fn)
-               /* The function already exists in the current namespace.  */
-               break;
-             else if (OVL_USED (tmp1))
-               continue; /* this is a using decl */
-             else if (compparms (TYPE_ARG_TYPES (TREE_TYPE (new_fn)),
-                                 TYPE_ARG_TYPES (TREE_TYPE (old_fn))))
+             tree new_fn = OVL_CURRENT (tmp);
+
+             /* [namespace.udecl]
+
+                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))
                {
-                 gcc_assert (!DECL_ANTICIPATED (old_fn)
-                             || DECL_HIDDEN_FRIEND_P (old_fn));
+                 tree old_fn = OVL_CURRENT (tmp1);
 
-                 /* 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 (decls_match (new_fn, old_fn))
+                 if (new_fn == old_fn)
+                   /* The function already exists in the current namespace.  */
                    break;
-                 else
+                 else if (OVL_USED (tmp1))
+                   continue; /* this is a using decl */
+                 else if (compparms (TYPE_ARG_TYPES (TREE_TYPE (new_fn)),
+                                     TYPE_ARG_TYPES (TREE_TYPE (old_fn))))
                    {
-                     error ("%qD is already declared in this scope", name);
-                     break;
+                     gcc_assert (!DECL_ANTICIPATED (old_fn)
+                                 || DECL_HIDDEN_FRIEND_P (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 (decls_match (new_fn, old_fn))
+                       break;
+                     else
+                       {
+                         error ("%qD is already declared in this scope", name);
+                         break;
+                       }
                    }
                }
-           }
 
-         /* 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));
+             /* 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;
            }
-         OVL_USED (*newval) = 1;
        }
+      else
+       {
+         *newval = decls.value;
+         if (oldval && !decls_match (*newval, oldval))
+           error ("%qD is already declared in this scope", name);
+       }
+    }
+  else
+    *newval = oldval;
+
+  if (decls.type && TREE_CODE (decls.type) == TREE_LIST)
+    {
+      error ("reference to %qD is ambiguous", name);
+      print_candidates (decls.type);
     }
   else
     {
-      *newval = decls.value;
-      if (oldval && !decls_match (*newval, oldval))
+      *newtype = decls.type;
+      if (oldtype && *newtype && !decls_match (oldtype, *newtype))
        error ("%qD is already declared in this scope", name);
     }
 
-  *newtype = decls.type;
-  if (oldtype && *newtype && !same_type_p (oldtype, *newtype))
-    {
-      error ("using declaration %qD introduced ambiguous type %qT",
-            name, oldtype);
-      return;
-    }
+    /* 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.  */
@@ -2642,7 +2761,8 @@ push_class_level_binding (tree name, tree x)
       && TREE_TYPE (decl) == error_mark_node)
     decl = TREE_VALUE (decl);
 
-  check_template_shadow (decl);
+  if (!check_template_shadow (decl))
+    POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, false);
 
   /* [class.mem]
 
@@ -2802,7 +2922,7 @@ do_class_using_decl (tree scope, tree name)
       error ("%<%T::%D%> names destructor", scope, name);
       return NULL_TREE;
     }
-  if (constructor_name_p (name, scope))
+  if (MAYBE_CLASS_TYPE_P (scope) && constructor_name_p (name, scope))
     {
       error ("%<%T::%D%> names constructor", scope, name);
       return NULL_TREE;
@@ -2942,7 +3062,7 @@ set_decl_namespace (tree decl, tree scope, bool friendp)
 
   /* See whether this has been declared in the namespace.  */
   old = lookup_qualified_name (scope, DECL_NAME (decl), false, true);
-  if (!old)
+  if (old == error_mark_node)
     /* No old declaration at all.  */
     goto complain;
   if (!is_overloaded_fn (decl))
@@ -3000,20 +3120,59 @@ current_decl_namespace (void)
   return result;
 }
 
-/* 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.  */
+/* Process any ATTRIBUTES on a namespace definition.  Currently only
+   attribute visibility is meaningful, which is a property of the syntactic
+   block rather than the namespace as a whole, so we don't touch the
+   NAMESPACE_DECL at all.  Returns true if attribute visibility is seen.  */
 
-void
-push_namespace (tree name)
+bool
+handle_namespace_attrs (tree ns, tree attributes)
 {
-  push_namespace_with_attribs (name, NULL_TREE);
-}
+  tree d;
+  bool saw_vis = false;
+
+  for (d = attributes; d; d = TREE_CHAIN (d))
+    {
+      tree name = TREE_PURPOSE (d);
+      tree args = TREE_VALUE (d);
+
+#ifdef HANDLE_PRAGMA_VISIBILITY
+      if (is_attribute_p ("visibility", name))
+       {
+         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);
 
-/* Same, but specify attributes to apply to the namespace.  The attributes
-   only apply to the current namespace-body, not to any later extensions. */
+         push_visibility (TREE_STRING_POINTER (x));
+         saw_vis = true;
+       }
+      else
+#endif
+       {
+         warning (OPT_Wattributes, "%qD attribute directive ignored",
+                  name);
+         continue;
+       }
+    }
+
+  return saw_vis;
+}
+  
+/* 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.  */
 
 void
-push_namespace_with_attribs (tree name, tree attributes)
+push_namespace (tree name)
 {
   tree d = NULL_TREE;
   int need_new = 1;
@@ -3080,38 +3239,6 @@ push_namespace_with_attribs (tree name, tree attributes)
   /* Enter the name space.  */
   current_namespace = d;
 
-#ifdef HANDLE_PRAGMA_VISIBILITY
-  /* Clear has_visibility in case a previous namespace-definition had a
-     visibility attribute and this one doesn't.  */
-  current_binding_level->has_visibility = 0;
-  for (d = attributes; d; d = TREE_CHAIN (d))
-    {
-      tree name = TREE_PURPOSE (d);
-      tree args = TREE_VALUE (d);
-      tree x;
-
-      if (! is_attribute_p ("visibility", name))
-       {
-         warning (OPT_Wattributes, "%qs attribute directive ignored",
-                  IDENTIFIER_POINTER (name));
-         continue;
-       }
-
-      x = args ? TREE_VALUE (args) : NULL_TREE;
-      if (x == NULL_TREE || TREE_CODE (x) != STRING_CST || TREE_CHAIN (args))
-       {
-         warning (OPT_Wattributes, "%qs attribute requires a single NTBS argument",
-                  IDENTIFIER_POINTER (name));
-         continue;
-       }
-
-      current_binding_level->has_visibility = 1;
-      push_visibility (TREE_STRING_POINTER (x));
-      goto found;
-    }
- found:
-#endif
-
   timevar_pop (TV_NAME_LOOKUP);
 }
 
@@ -3193,18 +3320,18 @@ namespace_ancestor (tree ns1, tree ns2)
 /* Process a namespace-alias declaration.  */
 
 void
-do_namespace_alias (tree alias, tree namespace)
+do_namespace_alias (tree alias, tree name_space)
 {
-  if (namespace == error_mark_node)
+  if (name_space == error_mark_node)
     return;
 
-  gcc_assert (TREE_CODE (namespace) == NAMESPACE_DECL);
+  gcc_assert (TREE_CODE (name_space) == NAMESPACE_DECL);
 
-  namespace = ORIGINAL_NAMESPACE (namespace);
+  name_space = ORIGINAL_NAMESPACE (name_space);
 
   /* Build the alias.  */
   alias = build_lang_decl (NAMESPACE_DECL, alias, void_type_node);
-  DECL_NAMESPACE_ALIAS (alias) = namespace;
+  DECL_NAMESPACE_ALIAS (alias) = name_space;
   DECL_EXTERNAL (alias) = 1;
   DECL_CONTEXT (alias) = FROB_CONTEXT (current_scope ());
   pushdecl (alias);
@@ -3342,46 +3469,46 @@ do_toplevel_using_decl (tree decl, tree scope, tree name)
 /* Process a using-directive.  */
 
 void
-do_using_directive (tree namespace)
+do_using_directive (tree name_space)
 {
   tree context = NULL_TREE;
 
-  if (namespace == error_mark_node)
+  if (name_space == error_mark_node)
     return;
 
-  gcc_assert (TREE_CODE (namespace) == NAMESPACE_DECL);
+  gcc_assert (TREE_CODE (name_space) == NAMESPACE_DECL);
 
   if (building_stmt_tree ())
-    add_stmt (build_stmt (USING_STMT, namespace));
-  namespace = ORIGINAL_NAMESPACE (namespace);
+    add_stmt (build_stmt (USING_STMT, name_space));
+  name_space = ORIGINAL_NAMESPACE (name_space);
 
   if (!toplevel_bindings_p ())
     {
-      push_using_directive (namespace);
-      context = current_scope ();
+      push_using_directive (name_space);
     }
   else
     {
       /* direct usage */
-      add_using_namespace (current_namespace, namespace, 0);
+      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) (namespace, context);
+      /* Emit debugging info.  */
+      if (!processing_template_decl)
+       (*debug_hooks->imported_module_or_decl) (name_space, NULL_TREE,
+                                                context, false);
+    }
 }
 
 /* Deal with a using-directive seen by the parser.  Currently we only
    handle attributes here, since they cannot appear inside a template.  */
 
 void
-parse_using_directive (tree namespace, tree attribs)
+parse_using_directive (tree name_space, tree attribs)
 {
   tree a;
 
-  do_using_directive (namespace);
+  do_using_directive (name_space);
 
   for (a = attribs; a; a = TREE_CHAIN (a))
     {
@@ -3390,14 +3517,14 @@ parse_using_directive (tree namespace, tree attribs)
        {
          if (!toplevel_bindings_p ())
            error ("strong using only meaningful at namespace scope");
-         else if (namespace != error_mark_node)
+         else if (name_space != error_mark_node)
            {
-             if (!is_ancestor (current_namespace, namespace))
+             if (!is_ancestor (current_namespace, name_space))
                error ("current namespace %qD does not enclose strongly used namespace %qD",
-                      current_namespace, namespace);
-             DECL_NAMESPACE_ASSOCIATIONS (namespace)
+                      current_namespace, name_space);
+             DECL_NAMESPACE_ASSOCIATIONS (name_space)
                = tree_cons (current_namespace, 0,
-                            DECL_NAMESPACE_ASSOCIATIONS (namespace));
+                            DECL_NAMESPACE_ASSOCIATIONS (name_space));
            }
        }
       else
@@ -3486,47 +3613,65 @@ merge_functions (tree s1, tree s2)
 /* 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 the freshly-found binding.
+   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 */
 
 static void
-ambiguous_decl (tree name, struct scope_binding *old, cxx_binding *new,
-               int flags)
+ambiguous_decl (struct scope_binding *old, cxx_binding *new_binding, int flags)
 {
   tree val, type;
   gcc_assert (old != NULL);
+
+  /* Copy the type.  */
+  type = new_binding->type;
+  if (LOOKUP_NAMESPACES_ONLY (flags)
+      || (type && hidden_name_p (type) && !(flags & LOOKUP_HIDDEN)))
+    type = NULL_TREE;
+
   /* Copy the value.  */
-  val = new->value;
+  val = new_binding->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,24 +3682,18 @@ 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 = NULL_TREE;
+
   if (!old->type)
     old->type = type;
   else if (type && old->type != type)
     {
-      if (flags & LOOKUP_COMPLAIN)
-       {
-         error ("%qD denotes an ambiguous type",name);
-         error ("%J  first type here", TYPE_MAIN_DECL (old->type));
-         error ("%J  other type here", TYPE_MAIN_DECL (type));
-       }
+      old->type = tree_cons (NULL_TREE, old->type,
+                            build_tree_list (NULL_TREE, type));
+      TREE_TYPE (old->type) = error_mark_node;
     }
 }
 
@@ -3644,36 +3783,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,22 +3794,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;
-         binding.type = b->type;
-       }
+       ambiguous_decl (&binding, b, flags);
 
       /* Add all _DECLs seen through local using-directives.  */
       for (level = current_binding_level;
@@ -3725,7 +3829,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;
     }
@@ -3755,9 +3859,11 @@ 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))
+  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, is_type_p);
 
   if (!t)
@@ -3788,7 +3894,7 @@ lookup_using_namespace (tree name, struct scope_binding *val,
          cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (used), name);
        /* Resolve ambiguities.  */
        if (val1)
-         ambiguous_decl (name, val, val1, flags);
+         ambiguous_decl (val, val1, flags);
       }
   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val->value != error_mark_node);
 }
@@ -3817,7 +3923,7 @@ qualified_lookup_using_namespace (tree name, tree scope,
        cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
       seen = tree_cons (scope, NULL_TREE, seen);
       if (binding)
-       ambiguous_decl (name, result, binding, flags);
+       ambiguous_decl (result, binding, flags);
 
       /* Consider strong using directives always, and non-strong ones
         if we haven't found a binding yet.  ??? Shouldn't we consider
@@ -3862,9 +3968,34 @@ qualified_lookup_using_namespace (tree name, tree scope,
   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, result->value != error_mark_node);
 }
 
+/* Subroutine of outer_binding.
+   Returns TRUE if BINDING is a binding to a template parameter of SCOPE,
+   FALSE otherwise.  */
+
+static bool
+binding_to_template_parms_of_scope_p (cxx_binding *binding,
+                                     cxx_scope *scope)
+{
+  tree binding_value;
+
+  if (!binding || !scope)
+    return false;
+
+  binding_value = binding->value ?  binding->value : binding->type;
+
+  return (scope
+         && scope->this_entity
+         && get_template_info (scope->this_entity)
+         && parameter_of_template_p (binding_value,
+                                     TI_TEMPLATE (get_template_info \
+                                                   (scope->this_entity))));
+}
+
 /* Return the innermost non-namespace binding for NAME from a scope
-   containing BINDING, or, if BINDING is NULL, the current scope.  If
-   CLASS_P is false, then class bindings are ignored.  */
+   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.  */
 
 cxx_binding *
 outer_binding (tree name,
@@ -3912,6 +4043,16 @@ outer_binding (tree name,
                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;
       }
 
@@ -4010,8 +4151,56 @@ lookup_name_real (tree name, int prefer_type, int nonclass, bool block_p,
 
        if (binding)
          {
-           /* Only namespace-scope bindings can be hidden.  */
-           gcc_assert (!hidden_name_p (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.
+
+                  In particular, consider:
+
+                  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
+                  }
+
+                  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".
+
+                  In particular:
+
+                  [class.friend]
+
+                  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.
+
+                  So just keep looking for a non-hidden binding.
+               */
+               gcc_assert (TREE_CODE (binding) == TYPE_DECL);
+               continue;
+             }
            val = binding;
            break;
          }
@@ -4136,7 +4325,8 @@ lookup_type_scope (tree name, tag_scope scope)
          if (iter->scope == b)
            POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
 
-         if (b->kind == sk_cleanup || b->kind == sk_template_parms)
+         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)
@@ -4152,7 +4342,7 @@ lookup_type_scope (tree name, tag_scope scope)
 /* Similar to `lookup_name' but look only in the innermost non-class
    binding level.  */
 
-static tree
+tree
 lookup_name_innermost_nonclass_level (tree name)
 {
   struct cp_binding_level *b;
@@ -4370,6 +4560,13 @@ arg_assoc_namespace (struct arg_lookup *k, tree scope)
     if (arg_assoc_namespace (k, TREE_PURPOSE (value)))
       return true;
 
+  /* 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;
+
   value = namespace_binding (k->name, scope);
   if (!value)
     return false;
@@ -4430,6 +4627,17 @@ arg_assoc_template_arg (struct arg_lookup *k, tree arg)
       else
        return arg_assoc_class (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;
+
+      return false;
+    }
   /* It's not a template template argument, but it is a type template
      argument.  */
   else if (TYPE_P (arg))
@@ -4534,6 +4742,8 @@ arg_assoc_type (struct arg_lookup *k, tree type)
     case COMPLEX_TYPE:
     case VECTOR_TYPE:
     case BOOLEAN_TYPE:
+    case FIXED_POINT_TYPE:
+    case DECLTYPE_TYPE:
       return false;
     case RECORD_TYPE:
       if (TYPE_PTRMEMFUNC_P (type))
@@ -4561,19 +4771,11 @@ arg_assoc_type (struct arg_lookup *k, tree type)
     case TYPENAME_TYPE:
       return false;
     case LANG_TYPE:
-      gcc_assert (type == unknown_type_node);
+      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));
-    case TYPE_ARGUMENT_PACK:
-      {
-        tree args = ARGUMENT_PACK_ARGS (type);
-        int i, len = TREE_VEC_LENGTH (args);
-        for (i = 0; i < len; i++)
-          if (arg_assoc_type (k, TREE_VEC_ELT (args, i)))
-            return true;
-      }
-      break;
 
     default:
       gcc_unreachable ();
@@ -4626,21 +4828,21 @@ arg_assoc (struct arg_lookup *k, tree n)
         If T is a template-id, its associated namespaces and classes
         are the namespace in which the template is defined; for
         member templates, the member template's class...  */
-      tree template = TREE_OPERAND (n, 0);
+      tree templ = TREE_OPERAND (n, 0);
       tree args = TREE_OPERAND (n, 1);
       tree ctx;
       int ix;
 
-      if (TREE_CODE (template) == COMPONENT_REF)
-       template = TREE_OPERAND (template, 1);
+      if (TREE_CODE (templ) == COMPONENT_REF)
+       templ = TREE_OPERAND (templ, 1);
 
       /* First, the template.  There may actually be more than one if
         this is an overloaded function template.  But, in that case,
         we only need the first; all the functions will be in the same
         namespace.  */
-      template = OVL_CURRENT (template);
+      templ = OVL_CURRENT (templ);
 
-      ctx = CP_DECL_CONTEXT (template);
+      ctx = CP_DECL_CONTEXT (templ);
 
       if (TREE_CODE (ctx) == NAMESPACE_DECL)
        {
@@ -4767,7 +4969,8 @@ maybe_process_template_type_declaration (tree type, int is_friend,
     ;
   else
     {
-      gcc_assert (IS_AGGR_TYPE (type) || TREE_CODE (type) == ENUMERAL_TYPE);
+      gcc_assert (MAYBE_CLASS_TYPE_P (type)
+                 || TREE_CODE (type) == ENUMERAL_TYPE);
 
       if (processing_template_decl)
        {
@@ -4776,6 +4979,9 @@ maybe_process_template_type_declaration (tree type, int is_friend,
          tree name = DECL_NAME (decl);
 
          decl = push_template_decl_real (decl, is_friend);
+         if (decl == error_mark_node)
+           return error_mark_node;
+
          /* If the current binding level is the binding level for the
             template parameters (see the comment in
             begin_template_parm_list) and the enclosing level is a class
@@ -4834,6 +5040,8 @@ pushtag (tree name, tree type, tag_scope scope)
   while (/* Cleanup scopes are not scopes from the point of view of
            the language.  */
         b->kind == sk_cleanup
+        /* Neither are function parameter scopes.  */
+        || b->kind == sk_function_parms
         /* Neither are the scopes used to hold template parameters
            for an explicit specialization.  For an ordinary template
            declaration, these scopes are not scopes from the point of
@@ -4899,11 +5107,11 @@ pushtag (tree name, tree type, tag_scope scope)
       if (decl == error_mark_node)
        POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
 
-      if (! in_class)
-       set_identifier_type_value_with_scope (name, tdef, b);
-
       if (b->kind == sk_class)
        {
+         if (!TYPE_BEING_DEFINED (current_class_type))
+           POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
+
          if (!PROCESSING_REAL_TEMPLATE_DECL_P ())
            /* Put this TYPE_DECL on the TYPE_FIELDS list for the
               class.  But if it's a member template class, we want
@@ -4920,6 +5128,9 @@ pushtag (tree name, tree type, tag_scope scope)
            POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
        }
 
+      if (! in_class)
+       set_identifier_type_value_with_scope (name, tdef, b);
+
       TYPE_CONTEXT (type) = DECL_CONTEXT (decl);
 
       /* If this is a local class, keep track of it.  We need this
@@ -5030,7 +5241,7 @@ push_to_top_level (void)
   struct cp_binding_level *b;
   cxx_saved_binding *sb;
   size_t i;
-  int need_pop;
+  bool need_pop;
 
   timevar_push (TV_NAME_LOOKUP);
   s = GGC_CNEW (struct saved_scope);
@@ -5040,11 +5251,11 @@ push_to_top_level (void)
   /* If we're in the middle of some function, save our state.  */
   if (cfun)
     {
-      need_pop = 1;
-      push_function_context_to (NULL_TREE);
+      need_pop = true;
+      push_function_context ();
     }
   else
-    need_pop = 0;
+    need_pop = false;
 
   if (scope_chain && previous_class_level)
     store_class_bindings (previous_class_level->class_shadowed,
@@ -5120,7 +5331,7 @@ pop_from_top_level (void)
   /* If we were in the middle of compiling a function, restore our
      state.  */
   if (s->need_pop_function_context)
-    pop_function_context_from (NULL_TREE);
+    pop_function_context ();
   current_function_decl = s->function_decl;
   skip_evaluation = s->skip_evaluation;
   timevar_pop (TV_NAME_LOOKUP);
@@ -5175,7 +5386,12 @@ cp_emit_debug_info_for_using (tree t, tree context)
   /* FIXME: Handle TEMPLATE_DECLs.  */
   for (t = OVL_CURRENT (t); t; t = OVL_NEXT (t))
     if (TREE_CODE (t) != TEMPLATE_DECL)
-      (*debug_hooks->imported_module_or_decl) (t, context);
+      {
+       if (building_stmt_tree ())
+         add_stmt (build_stmt (USING_STMT, t));
+       else
+         (*debug_hooks->imported_module_or_decl) (t, NULL_TREE, context, false);
+      }
 }
 
 #include "gt-cp-name-lookup.h"