OSDN Git Service

2012-04-15 Fabien ChĂȘne <fabien@gcc.gnu.org>
[pf3gnuchains/gcc-fork.git] / gcc / cp / name-lookup.c
index 935dd2a..fedf626 100644 (file)
@@ -1,5 +1,5 @@
 /* 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, 2012
    Free Software Foundation, Inc.
    Contributed by Gabriel Dos Reis <gdr@integrable-solutions.net>
 
@@ -33,6 +33,7 @@ along with GCC; see the file COPYING3.  If not see
 #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.  */
 
@@ -42,8 +43,8 @@ struct scope_binding {
 };
 #define EMPTY_SCOPE_BINDING { NULL_TREE, NULL_TREE }
 
-static cxx_scope *innermost_nonclass_level (void);
-static cxx_binding *binding_for_name (cxx_scope *, 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,
                                    tree, int);
@@ -51,7 +52,8 @@ 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);
+static tree lookup_extern_c_fun_in_all_ns (tree);
+static void diagnose_name_conflict (tree, tree);
 
 /* The :: namespace.  */
 
@@ -314,7 +316,7 @@ cxx_binding_free (cxx_binding *binding)
    bindings) in the class scope indicated by SCOPE.  */
 
 static cxx_binding *
-new_class_binding (tree name, tree value, tree type, cxx_scope *scope)
+new_class_binding (tree name, tree value, tree type, cp_binding_level *scope)
 {
   cp_class_binding *cb;
   cxx_binding *binding;
@@ -331,7 +333,7 @@ new_class_binding (tree name, tree value, tree type, cxx_scope *scope)
    level at which this declaration is being bound.  */
 
 static void
-push_binding (tree id, tree decl, cxx_scope* level)
+push_binding (tree id, tree decl, cp_binding_level* level)
 {
   cxx_binding *binding;
 
@@ -393,6 +395,19 @@ pop_binding (tree id, tree decl)
     }
 }
 
+/* Strip non dependent using declarations.  */
+
+tree
+strip_using_decl (tree decl)
+{
+  if (decl == NULL_TREE)
+    return NULL_TREE;
+
+  while (TREE_CODE (decl) == USING_DECL && !DECL_DEPENDENT_P (decl))
+    decl = USING_DECL_DECLS (decl);
+  return decl;
+}
+
 /* 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
@@ -416,27 +431,46 @@ supplement_binding_1 (cxx_binding *binding, tree decl)
 {
   tree bval = binding->value;
   bool ok = true;
-
-  if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl))
+  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.  */
+         || (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 (/* 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.  */
-          !bval
-          /* BVAL is error_mark_node when 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.  */
-          || bval == error_mark_node
-          /* If BVAL is anticipated but has not yet been declared,
-             pretend it is not there at all.  */
-          || (TREE_CODE (bval) == FUNCTION_DECL
-              && DECL_ANTICIPATED (bval)
-              && !DECL_HIDDEN_FRIEND_P (bval)))
+  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 (bval) == TYPE_DECL && DECL_ARTIFICIAL (bval))
+  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))))
     {
       /* The old binding was a type name.  It was placed in
         VALUE field because it was thought, at the point it was
@@ -447,15 +481,15 @@ supplement_binding_1 (cxx_binding *binding, tree decl)
       binding->value = decl;
       binding->value_is_inherited = false;
     }
-  else if (TREE_CODE (bval) == TYPE_DECL
-          && TREE_CODE (decl) == TYPE_DECL
-          && DECL_NAME (decl) == DECL_NAME (bval)
+  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 (decl), TREE_TYPE (bval))
+          && (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 (decl))
-              || uses_template_parms (TREE_TYPE (bval))))
+              || 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:
 
@@ -477,9 +511,10 @@ supplement_binding_1 (cxx_binding *binding, tree decl)
 
        A member shall not be declared twice in the
        member-specification.  */
-  else if (TREE_CODE (decl) == VAR_DECL && TREE_CODE (bval) == VAR_DECL
-          && DECL_EXTERNAL (decl) && DECL_EXTERNAL (bval)
-          && !DECL_CLASS_SCOPE_P (decl))
+  else if (TREE_CODE (target_decl) == VAR_DECL
+          && TREE_CODE (target_bval) == VAR_DECL
+          && 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;
@@ -498,14 +533,30 @@ supplement_binding_1 (cxx_binding *binding, tree decl)
     ok = false;
   else
     {
-      error ("declaration of %q#D", decl);
-      error ("conflicts with previous declaration %q+#D", bval);
+      diagnose_name_conflict (decl, bval);
       ok = false;
     }
 
   return ok;
 }
 
+/* Diagnose a name conflict between DECL and BVAL.  */
+
+static void
+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
+    error ("%q#D conflicts with a previous declaration", decl);
+
+  inform (input_location, "previous declaration %q+#D", bval);
+}
+
 /* Wrapper for supplement_binding_1.  */
 
 static bool
@@ -521,7 +572,7 @@ supplement_binding (cxx_binding *binding, tree decl)
 /* Add DECL to the list of things declared in B.  */
 
 static void
-add_decl_to_level (tree decl, cxx_scope *b)
+add_decl_to_level (tree decl, cp_binding_level *b)
 {
   /* We used to record virtual tables as if they were ordinary
      variables, but no longer do so.  */
@@ -539,7 +590,6 @@ add_decl_to_level (tree decl, cxx_scope *b)
         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
@@ -766,18 +816,12 @@ pushdecl_maybe_friend_1 (tree x, bool is_friend)
          && !DECL_ARTIFICIAL (x)
          && !DECL_IN_SYSTEM_HEADER (x))
        {
-         cxx_binding *function_binding =
-             lookup_extern_c_fun_binding_in_all_ns (x);
-         tree previous = (function_binding
-                          ? function_binding->value
-                          : NULL_TREE);
+         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))
            {
-             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))
@@ -803,6 +847,9 @@ pushdecl_maybe_friend_1 (tree x, bool is_friend)
                                "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
                {
@@ -869,6 +916,13 @@ pushdecl_maybe_friend_1 (tree x, bool is_friend)
              && TYPE_NAME (type)
              && TYPE_IDENTIFIER (type))
            set_identifier_type_value (DECL_NAME (x), x);
+
+         /* If this is a locally defined typedef in a function that
+            is not a template instantation, record it to implement
+            -Wunused-local-typedefs.  */
+         if (current_instantiation () == NULL
+             || (current_instantiation ()->decl != current_function_decl))
+         record_locally_defined_typedef (x);
        }
 
       /* Multiple external decls of the same identifier ought to match.
@@ -935,8 +989,15 @@ pushdecl_maybe_friend_1 (tree x, bool is_friend)
       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;
+         cp_binding_level *oldscope = NULL;
+         cxx_binding *oldbinding = outer_binding (name, NULL, true);
+         if (oldbinding)
+           {
+             oldlocal = oldbinding->value;
+             oldscope = oldbinding->scope;
+           }
 
          if (need_new_binding)
            {
@@ -1013,11 +1074,6 @@ pushdecl_maybe_friend_1 (tree x, bool is_friend)
                        || (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).  */
@@ -1032,7 +1088,7 @@ pushdecl_maybe_friend_1 (tree x, bool is_friend)
                {
                  /* Go to where the parms should be and see if we find
                     them there.  */
-                 struct cp_binding_level *b = current_binding_level->level_chain;
+                 cp_binding_level *b = current_binding_level->level_chain;
 
                  if (FUNCTION_NEEDS_BODY_BLOCK (current_function_decl))
                    /* Skip the ctor/dtor cleanup level.  */
@@ -1050,7 +1106,7 @@ pushdecl_maybe_friend_1 (tree x, bool is_friend)
                 the containing function anyway.  */
              if (DECL_CONTEXT (oldlocal) != current_function_decl)
                {
-                 cxx_scope *scope = current_binding_level;
+                 cp_binding_level *scope = current_binding_level;
                  tree context = DECL_CONTEXT (oldlocal);
                  for (; scope; scope = scope->level_chain)
                   {
@@ -1065,12 +1121,30 @@ pushdecl_maybe_friend_1 (tree x, bool is_friend)
                       }
                   }
                }
+             /* 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",
@@ -1095,7 +1169,8 @@ pushdecl_maybe_friend_1 (tree x, bool is_friend)
                member = lookup_member (current_class_type,
                                        name,
                                        /*protect=*/0,
-                                       /*want_type=*/false);
+                                       /*want_type=*/false,
+                                       tf_warning_or_error);
              else
                member = NULL_TREE;
 
@@ -1193,7 +1268,7 @@ maybe_push_decl (tree decl)
 void
 push_local_binding (tree id, tree decl, int flags)
 {
-  struct cp_binding_level *b;
+  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.  */
@@ -1315,7 +1390,7 @@ indent (int depth)
 
 /* Return a string describing the kind of SCOPE we have.  */
 static const char *
-cxx_scope_descriptor (cxx_scope *scope)
+cp_binding_level_descriptor (cp_binding_level *scope)
 {
   /* The order of this table must match the "scope_kind"
      enumerators.  */
@@ -1340,9 +1415,9 @@ cxx_scope_descriptor (cxx_scope *scope)
 /* Output a debugging information about SCOPE when performing
    ACTION at LINE.  */
 static void
-cxx_scope_debug (cxx_scope *scope, int line, const char *action)
+cp_binding_level_debug (cp_binding_level *scope, int line, const char *action)
 {
-  const char *desc = cxx_scope_descriptor (scope);
+  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);
@@ -1367,12 +1442,12 @@ namespace_scope_ht_size (tree ns)
 
 /* A chain of binding_level structures awaiting reuse.  */
 
-static GTY((deletable)) struct cp_binding_level *free_binding_level;
+static GTY((deletable)) cp_binding_level *free_binding_level;
 
 /* Insert SCOPE as the innermost binding level.  */
 
 void
-push_binding_level (struct cp_binding_level *scope)
+push_binding_level (cp_binding_level *scope)
 {
   /* Add it to the front of currently active scopes stack.  */
   scope->level_chain = current_binding_level;
@@ -1383,7 +1458,7 @@ push_binding_level (struct cp_binding_level *scope)
     {
       scope->binding_depth = binding_depth;
       indent (binding_depth);
-      cxx_scope_debug (scope, input_line, "push");
+      cp_binding_level_debug (scope, input_line, "push");
       binding_depth++;
     }
 }
@@ -1392,20 +1467,20 @@ push_binding_level (struct cp_binding_level *scope)
    ENTITY is the scope of the associated C++ entity (namespace, class,
    function, C++0x enumeration); it is NULL otherwise.  */
 
-cxx_scope *
+cp_binding_level *
 begin_scope (scope_kind kind, tree entity)
 {
-  cxx_scope *scope;
+  cp_binding_level *scope;
 
   /* Reuse or create a struct for this binding level.  */
   if (!ENABLE_SCOPE_CHECKING && free_binding_level)
     {
       scope = free_binding_level;
-      memset (scope, 0, sizeof (cxx_scope));
+      memset (scope, 0, sizeof (cp_binding_level));
       free_binding_level = scope->level_chain;
     }
   else
-    scope = ggc_alloc_cleared_cxx_scope ();
+    scope = ggc_alloc_cleared_cp_binding_level ();
 
   scope->this_entity = entity;
   scope->more_cleanups_ok = true;
@@ -1424,6 +1499,7 @@ begin_scope (scope_kind kind, tree entity)
     case sk_try:
     case sk_catch:
     case sk_for:
+    case sk_cond:
     case sk_class:
     case sk_scoped_enum:
     case sk_function_parms:
@@ -1455,10 +1531,10 @@ begin_scope (scope_kind kind, tree entity)
 /* We're about to leave current scope.  Pop the top of the stack of
    currently active scopes.  Return the enclosing scope, now active.  */
 
-cxx_scope *
+cp_binding_level *
 leave_scope (void)
 {
-  cxx_scope *scope = current_binding_level;
+  cp_binding_level *scope = current_binding_level;
 
   if (scope->kind == sk_namespace && class_binding_level)
     current_binding_level = class_binding_level;
@@ -1470,7 +1546,7 @@ leave_scope (void)
   if (ENABLE_SCOPE_CHECKING)
     {
       indent (--binding_depth);
-      cxx_scope_debug (scope, input_line, "leave");
+      cp_binding_level_debug (scope, input_line, "leave");
     }
 
   /* Move one nesting level up.  */
@@ -1507,7 +1583,7 @@ leave_scope (void)
 }
 
 static void
-resume_scope (struct cp_binding_level* b)
+resume_scope (cp_binding_level* b)
 {
   /* Resuming binding levels is meant only for namespaces,
      and those cannot nest into classes.  */
@@ -1519,17 +1595,17 @@ resume_scope (struct cp_binding_level* b)
     {
       b->binding_depth = binding_depth;
       indent (binding_depth);
-      cxx_scope_debug (b, input_line, "resume");
+      cp_binding_level_debug (b, input_line, "resume");
       binding_depth++;
     }
 }
 
 /* Return the innermost binding level that is not for a class scope.  */
 
-static cxx_scope *
+static cp_binding_level *
 innermost_nonclass_level (void)
 {
-  cxx_scope *b;
+  cp_binding_level *b;
 
   b = current_binding_level;
   while (b->kind == sk_class)
@@ -1571,7 +1647,7 @@ global_bindings_p (void)
 bool
 toplevel_bindings_p (void)
 {
-  struct cp_binding_level *b = innermost_nonclass_level ();
+  cp_binding_level *b = innermost_nonclass_level ();
 
   return b->kind == sk_namespace || b->kind == sk_template_parms;
 }
@@ -1583,11 +1659,20 @@ toplevel_bindings_p (void)
 bool
 namespace_bindings_p (void)
 {
-  struct cp_binding_level *b = innermost_nonclass_level ();
+  cp_binding_level *b = innermost_nonclass_level ();
 
   return b->kind == sk_namespace;
 }
 
+/* True if the innermost non-class scope is a block scope.  */
+
+bool
+local_bindings_p (void)
+{
+  cp_binding_level *b = innermost_nonclass_level ();
+  return b->kind < sk_function_parms || b->kind == sk_omp;
+}
+
 /* True if the current level needs to have a BLOCK made.  */
 
 bool
@@ -1643,7 +1728,7 @@ int
 function_parm_depth (void)
 {
   int level = 0;
-  struct cp_binding_level *b;
+  cp_binding_level *b;
 
   for (b = current_binding_level;
        b->kind == sk_function_parms;
@@ -1658,7 +1743,7 @@ static int no_print_functions = 0;
 static int no_print_builtins = 0;
 
 static void
-print_binding_level (struct cp_binding_level* lvl)
+print_binding_level (cp_binding_level* lvl)
 {
   tree t;
   int i = 0, len;
@@ -1720,9 +1805,9 @@ print_binding_level (struct cp_binding_level* lvl)
 }
 
 void
-print_other_binding_stack (struct cp_binding_level *stack)
+print_other_binding_stack (cp_binding_level *stack)
 {
-  struct cp_binding_level *level;
+  cp_binding_level *level;
   for (level = stack; !global_scope_p (level); level = level->level_chain)
     {
       fprintf (stderr, "binding level %p\n", (void *) level);
@@ -1733,7 +1818,7 @@ print_other_binding_stack (struct cp_binding_level *stack)
 void
 print_binding_stack (void)
 {
-  struct cp_binding_level *b;
+  cp_binding_level *b;
   fprintf (stderr, "current_binding_level=%p\n"
           "class_binding_level=%p\n"
           "NAMESPACE_LEVEL (global_namespace)=%p\n",
@@ -1802,7 +1887,7 @@ identifier_global_value   (tree t)
    the tag ID is not already defined.  */
 
 static void
-set_identifier_type_value_with_scope (tree id, tree decl, cxx_scope *b)
+set_identifier_type_value_with_scope (tree id, tree decl, cp_binding_level *b)
 {
   tree type;
 
@@ -1926,7 +2011,7 @@ make_lambda_name (void)
 /* Return (from the stack of) the BINDING, if any, established at SCOPE.  */
 
 static inline cxx_binding *
-find_binding (cxx_scope *scope, cxx_binding *binding)
+find_binding (cp_binding_level *scope, cxx_binding *binding)
 {
   for (; binding != NULL; binding = binding->previous)
     if (binding->scope == scope)
@@ -1938,7 +2023,7 @@ find_binding (cxx_scope *scope, cxx_binding *binding)
 /* Return the binding for NAME in SCOPE, if any.  Otherwise, return NULL.  */
 
 static inline cxx_binding *
-cxx_scope_find_binding_for_name (cxx_scope *scope, tree name)
+cp_binding_level_find_binding_for_name (cp_binding_level *scope, tree name)
 {
   cxx_binding *b = IDENTIFIER_NAMESPACE_BINDINGS (name);
   if (b)
@@ -1955,11 +2040,11 @@ cxx_scope_find_binding_for_name (cxx_scope *scope, tree name)
    found, make a new one.  */
 
 static cxx_binding *
-binding_for_name (cxx_scope *scope, tree name)
+binding_for_name (cp_binding_level *scope, tree name)
 {
   cxx_binding *result;
 
-  result = cxx_scope_find_binding_for_name (scope, name);
+  result = cp_binding_level_find_binding_for_name (scope, name);
   if (result)
     return result;
   /* Not found, make a new one.  */
@@ -1973,14 +2058,14 @@ binding_for_name (cxx_scope *scope, tree name)
 }
 
 /* Walk through the bindings associated to the name of FUNCTION,
-   and return the first binding that declares a function with a
+   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 cxx_binding*
-lookup_extern_c_fun_binding_in_all_ns (tree function)
+static tree
+lookup_extern_c_fun_in_all_ns (tree function)
 {
   tree name;
   cxx_binding *iter;
@@ -1994,17 +2079,52 @@ lookup_extern_c_fun_binding_in_all_ns (tree function)
        iter;
        iter = iter->previous)
     {
-      if (iter->value
-         && TREE_CODE (iter->value) == FUNCTION_DECL
-         && DECL_EXTERN_C_P (iter->value)
-         && !DECL_ARTIFICIAL (iter->value))
+      tree ovl;
+      for (ovl = iter->value; ovl; ovl = OVL_NEXT (ovl))
        {
-         return iter;
+         tree decl = OVL_CURRENT (ovl);
+         if (decl
+             && TREE_CODE (decl) == FUNCTION_DECL
+             && DECL_EXTERN_C_P (decl)
+             && !DECL_ARTIFICIAL (decl))
+           {
+             return decl;
+           }
        }
     }
   return NULL;
 }
 
+/* Returns a list of C-linkage decls with the name NAME.  */
+
+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))
+       {
+         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);
+           }
+       }
+    }
+  return decls;
+}
+
 /* 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
@@ -2042,12 +2162,17 @@ push_using_decl (tree scope, tree name)
 }
 
 /* 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)
+pushdecl_with_scope_1 (tree x, cp_binding_level *level, bool is_friend)
 {
-  struct cp_binding_level *b;
+  cp_binding_level *b;
   tree function_decl = current_function_decl;
 
   current_function_decl = NULL_TREE;
@@ -2072,7 +2197,7 @@ pushdecl_with_scope_1 (tree x, cxx_scope *level, bool is_friend)
 /* Wrapper for pushdecl_with_scope_1.  */
 
 tree
-pushdecl_with_scope (tree x, cxx_scope *level, bool is_friend)
+pushdecl_with_scope (tree x, cp_binding_level *level, bool is_friend)
 {
   tree ret;
   bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
@@ -2453,7 +2578,7 @@ do_local_using_decl (tree decl, tree scope, tree name)
   if (decl == NULL_TREE)
     return;
 
-  if (building_stmt_tree ()
+  if (building_stmt_list_p ()
       && at_function_scope_p ())
     add_decl_expr (decl);
 
@@ -2582,12 +2707,12 @@ push_inner_scope_r (tree outer, tree inner)
     push_inner_scope_r (outer, prev);
   if (TREE_CODE (inner) == NAMESPACE_DECL)
     {
-      struct cp_binding_level *save_template_parm = 0;
+      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)
        {
-         struct cp_binding_level *b = current_binding_level;
+         cp_binding_level *b = current_binding_level;
          current_binding_level = b->level_chain;
          b->level_chain = save_template_parm;
          save_template_parm = b;
@@ -2599,7 +2724,7 @@ push_inner_scope_r (tree outer, tree inner)
       /* Restore template parameter scopes.  */
       while (save_template_parm)
        {
-         struct cp_binding_level *b = 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;
@@ -2641,12 +2766,12 @@ pop_inner_scope (tree outer, tree inner)
     {
       if (TREE_CODE (inner) == NAMESPACE_DECL)
        {
-         struct cp_binding_level *save_template_parm = 0;
+         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)
            {
-             struct cp_binding_level *b = current_binding_level;
+             cp_binding_level *b = current_binding_level;
              current_binding_level = b->level_chain;
              b->level_chain = save_template_parm;
              save_template_parm = b;
@@ -2657,7 +2782,7 @@ pop_inner_scope (tree outer, tree inner)
          /* Restore template parameter scopes.  */
          while (save_template_parm)
            {
-             struct cp_binding_level *b = 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;
@@ -2683,7 +2808,7 @@ pushlevel_class (void)
 void
 poplevel_class (void)
 {
-  struct cp_binding_level *level = class_binding_level;
+  cp_binding_level *level = class_binding_level;
   cp_class_binding *cb;
   size_t i;
   tree shadowed;
@@ -2731,7 +2856,7 @@ set_inherited_value_binding_p (cxx_binding *binding, tree decl,
       tree context;
 
       if (TREE_CODE (decl) == OVERLOAD)
-       context = CP_DECL_CONTEXT (OVL_CURRENT (decl));
+       context = ovl_scope (decl);
       else
        {
          gcc_assert (DECL_P (decl));
@@ -2804,7 +2929,7 @@ pushdecl_class_level (tree x)
    is not set, callers must set the PREVIOUS field explicitly.  */
 
 static cxx_binding *
-get_class_binding (tree name, cxx_scope *scope)
+get_class_binding (tree name, cp_binding_level *scope)
 {
   tree class_type;
   tree type_binding;
@@ -2815,10 +2940,12 @@ get_class_binding (tree name, cxx_scope *scope)
 
   /* Get the type binding.  */
   type_binding = lookup_member (class_type, name,
-                               /*protect=*/2, /*want_type=*/true);
+                               /*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);
+                                /*protect=*/2, /*want_type=*/false,
+                                tf_warning_or_error);
 
   if (value_binding
       && (TREE_CODE (value_binding) == TYPE_DECL
@@ -2949,6 +3076,8 @@ push_class_level_binding_1 (tree name, tree x)
     {
       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))
        {
@@ -2956,8 +3085,10 @@ push_class_level_binding_1 (tree name, tree x)
             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 (bval) == TYPE_DECL && DECL_ARTIFICIAL (bval)
-             && !(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x)))
+         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;
@@ -2969,17 +3100,31 @@ push_class_level_binding_1 (tree name, tree x)
              old_decl = bval;
              /* Any inherited type declaration is hidden by the type
                 declaration in the derived class.  */
-             if (TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x))
+             if (TREE_CODE (target_decl) == TYPE_DECL
+                 && DECL_ARTIFICIAL (target_decl))
                binding->type = NULL_TREE;
            }
        }
-      else if (TREE_CODE (x) == OVERLOAD && is_overloaded_fn (bval))
+      else if (TREE_CODE (target_decl) == OVERLOAD
+              && is_overloaded_fn (target_bval))
        old_decl = bval;
-      else if (TREE_CODE (x) == USING_DECL && TREE_CODE (bval) == USING_DECL)
+      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 (x) == USING_DECL && is_overloaded_fn (bval))
+      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 (x))
+      else if (TREE_CODE (bval) == USING_DECL
+              && is_overloaded_fn (target_decl))
        return true;
 
       if (old_decl && binding->scope == class_binding_level)
@@ -3075,7 +3220,7 @@ do_class_using_decl (tree scope, tree name)
       return NULL_TREE;
     }
 
-  scope_dependent_p = dependent_type_p (scope);
+  scope_dependent_p = dependent_scope_p (scope);
   name_dependent_p = (scope_dependent_p
                      || (IDENTIFIER_TYPENAME_P (name)
                          && dependent_type_p (TREE_TYPE (name))));
@@ -3100,9 +3245,9 @@ do_class_using_decl (tree scope, tree name)
 
      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.  However, if all of the base classes are
-     non-dependent, then we can avoid delaying the check until
-     instantiation.  */
+     class type. Morover, if SCOPE is dependent, it might match a
+     non-dependent base.  */
+
   if (!scope_dependent_p)
     {
       base_kind b_kind;
@@ -3117,7 +3262,7 @@ do_class_using_decl (tree scope, tree name)
        }
       else if (!name_dependent_p)
        {
-         decl = lookup_member (binfo, name, 0, false);
+         decl = lookup_member (binfo, name, 0, false, tf_warning_or_error);
          if (!decl)
            {
              error ("no members matching %<%T::%D%> in %q#T", scope, name,
@@ -3128,7 +3273,7 @@ do_class_using_decl (tree scope, tree name)
          if (BASELINK_P (decl))
            decl = BASELINK_FUNCTIONS (decl);
        }
-   }
+    }
 
   value = build_lang_decl (USING_DECL, name, NULL_TREE);
   USING_DECL_DECLS (value) = decl;
@@ -3153,7 +3298,7 @@ namespace_binding_1 (tree name, tree scope)
     /* Unnecessary for the global namespace because it can't be an alias. */
     scope = ORIGINAL_NAMESPACE (scope);
 
-  binding = cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
+  binding = cp_binding_level_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
 
   return binding ? binding->value : NULL_TREE;
 }
@@ -3548,7 +3693,7 @@ do_namespace_alias (tree alias, tree name_space)
   pushdecl (alias);
 
   /* Emit debug info for namespace alias.  */
-  if (!building_stmt_tree ())
+  if (!building_stmt_list_p ())
     (*debug_hooks->global_decl) (alias);
 }
 
@@ -3558,7 +3703,7 @@ do_namespace_alias (tree alias, tree name_space)
 tree
 pushdecl_namespace_level (tree x, bool is_friend)
 {
-  struct cp_binding_level *b = current_binding_level;
+  cp_binding_level *b = current_binding_level;
   tree t;
 
   bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
@@ -3696,7 +3841,7 @@ do_using_directive (tree name_space)
 
   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);
 
@@ -3973,18 +4118,17 @@ qualify_lookup (tree val, int flags)
     return false;
   if ((flags & LOOKUP_PREFER_NAMESPACES) && TREE_CODE (val) == NAMESPACE_DECL)
     return true;
-  if ((flags & LOOKUP_PREFER_TYPES)
-      && (TREE_CODE (val) == TYPE_DECL || TREE_CODE (val) == TEMPLATE_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;
-  /* 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;
 }
@@ -4055,7 +4199,7 @@ suggest_alternatives_for (location_t location, tree name)
     {
       tree scope = VEC_pop (tree, namespaces_to_search);
       struct scope_binding binding = EMPTY_SCOPE_BINDING;
-      struct cp_binding_level *level = NAMESPACE_LEVEL (scope);
+      cp_binding_level *level = NAMESPACE_LEVEL (scope);
 
       /* Look in this namespace.  */
       qualified_lookup_using_namespace (name, scope, &binding, 0);
@@ -4105,14 +4249,14 @@ unqualified_namespace_lookup_1 (tree name, int flags)
   tree initial = current_decl_namespace ();
   tree scope = initial;
   tree siter;
-  struct cp_binding_level *level;
+  cp_binding_level *level;
   tree val = NULL_TREE;
 
   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);
+        cp_binding_level_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
 
       if (b)
        ambiguous_decl (&binding, b, flags);
@@ -4187,7 +4331,7 @@ lookup_qualified_name (tree scope, tree name, bool is_type_p, bool 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);
+    t = lookup_member (scope, name, 2, is_type_p, tf_warning_or_error);
 
   if (!t)
     return error_mark_node;
@@ -4214,7 +4358,7 @@ lookup_using_namespace (tree name, struct scope_binding *val,
       {
        tree used = ORIGINAL_NAMESPACE (TREE_PURPOSE (iter));
        cxx_binding *val1 =
-         cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (used), name);
+         cp_binding_level_find_binding_for_name (NAMESPACE_LEVEL (used), name);
        /* Resolve ambiguities.  */
        if (val1)
          ambiguous_decl (val, val1, flags);
@@ -4283,7 +4427,7 @@ qualified_lookup_using_namespace (tree name, tree scope,
          VEC_safe_push (tree, gc, seen_inline, scope);
 
          binding =
-           cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
+           cp_binding_level_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
          if (binding)
            {
              found_here = true;
@@ -4327,7 +4471,7 @@ qualified_lookup_using_namespace (tree name, tree scope,
 
 static bool
 binding_to_template_parms_of_scope_p (cxx_binding *binding,
-                                     cxx_scope *scope)
+                                     cp_binding_level *scope)
 {
   tree binding_value;
 
@@ -4358,8 +4502,8 @@ outer_binding (tree name,
               bool class_p)
 {
   cxx_binding *outer;
-  cxx_scope *scope;
-  cxx_scope *outer_scope;
+  cp_binding_level *scope;
+  cp_binding_level *outer_scope;
 
   if (binding)
     {
@@ -4450,9 +4594,9 @@ lookup_name_real_1 (tree name, int prefer_type, int nonclass, bool block_p,
   /* Conversion operators are handled specially because ordinary
      unqualified name lookup will not find template conversion
      operators.  */
-  if (IDENTIFIER_TYPENAME_P (name) && current_class_type)
+  if (IDENTIFIER_TYPENAME_P (name))
     {
-      struct cp_binding_level *level;
+      cp_binding_level *level;
 
       for (level = current_binding_level;
           level && level->kind != sk_namespace;
@@ -4667,7 +4811,7 @@ lookup_type_scope_1 (tree name, tag_scope scope)
   /* Look in namespace scope.  */
   if (!val)
     {
-      iter = cxx_scope_find_binding_for_name
+      iter = cp_binding_level_find_binding_for_name
               (NAMESPACE_LEVEL (current_decl_namespace ()), name);
 
       if (iter)
@@ -4685,7 +4829,7 @@ lookup_type_scope_1 (tree name, tag_scope scope)
      and template parameter scopes.  */
   if (val)
     {
-      struct cp_binding_level *b = current_binding_level;
+      cp_binding_level *b = current_binding_level;
       while (b)
        {
          if (iter->scope == b)
@@ -4724,7 +4868,7 @@ lookup_type_scope (tree name, tag_scope scope)
 static tree
 lookup_name_innermost_nonclass_level_1 (tree name)
 {
-  struct cp_binding_level *b;
+  cp_binding_level *b;
   tree t = NULL_TREE;
 
   b = innermost_nonclass_level ();
@@ -4813,7 +4957,7 @@ lookup_type_current_level (tree name)
   if (REAL_IDENTIFIER_TYPE_VALUE (name) != NULL_TREE
       && REAL_IDENTIFIER_TYPE_VALUE (name) != global_type_node)
     {
-      struct cp_binding_level *b = current_binding_level;
+      cp_binding_level *b = current_binding_level;
       while (1)
        {
          if (purpose_member (name, b->type_shadowed))
@@ -4842,6 +4986,7 @@ struct arg_lookup
   VEC(tree,gc) *namespaces;
   VEC(tree,gc) *classes;
   tree functions;
+  struct pointer_set_t *fn_set;
 };
 
 static bool arg_assoc (struct arg_lookup*, tree);
@@ -4861,16 +5006,11 @@ static bool arg_assoc_template_arg (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)
@@ -5262,7 +5402,7 @@ arg_assoc (struct arg_lookup *k, tree n)
     n = TREE_OPERAND (n, 1);
   while (TREE_CODE (n) == TREE_LIST)
     n = TREE_VALUE (n);
-  if (TREE_CODE (n) == BASELINK)
+  if (BASELINK_P (n))
     n = BASELINK_FUNCTIONS (n);
 
   if (TREE_CODE (n) == FUNCTION_DECL)
@@ -5324,6 +5464,23 @@ lookup_arg_dependent_1 (tree name, tree fns, VEC(tree,gc) *args,
      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);
@@ -5341,6 +5498,8 @@ lookup_arg_dependent_1 (tree name, tree fns, VEC(tree,gc) *args,
 
   release_tree_vector (k.classes);
   release_tree_vector (k.namespaces);
+  if (k.fn_set)
+    pointer_set_destroy (k.fn_set);
     
   return fns;
 }
@@ -5352,9 +5511,10 @@ lookup_arg_dependent (tree name, tree fns, VEC(tree,gc) *args,
                       bool include_std)
 {
   tree ret;
-  timevar_start (TV_NAME_LOOKUP);
+  bool subtime;
+  subtime = timevar_cond_start (TV_NAME_LOOKUP);
   ret = lookup_arg_dependent_1 (name, fns, args, include_std);
-  timevar_stop (TV_NAME_LOOKUP);
+  timevar_cond_stop (TV_NAME_LOOKUP, subtime);
   return ret;
 }
 
@@ -5408,7 +5568,7 @@ push_using_directive (tree used)
 
 static tree
 maybe_process_template_type_declaration (tree type, int is_friend,
-                                        cxx_scope *b)
+                                        cp_binding_level *b)
 {
   tree decl = TYPE_NAME (type);
 
@@ -5490,7 +5650,7 @@ maybe_process_template_type_declaration (tree type, int is_friend,
 static tree
 pushtag_1 (tree name, tree type, tag_scope scope)
 {
-  struct cp_binding_level *b;
+  cp_binding_level *b;
   tree decl;
 
   b = current_binding_level;
@@ -5707,7 +5867,7 @@ void
 push_to_top_level (void)
 {
   struct saved_scope *s;
-  struct cp_binding_level *b;
+  cp_binding_level *b;
   cxx_saved_binding *sb;
   size_t i;
   bool need_pop;
@@ -5763,6 +5923,7 @@ push_to_top_level (void)
   s->function_decl = current_function_decl;
   s->unevaluated_operand = cp_unevaluated_operand;
   s->inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
+  s->x_stmt_tree.stmts_are_full_exprs_p = true;
 
   scope_chain = s;
   current_function_decl = NULL_TREE;
@@ -5868,7 +6029,7 @@ cp_emit_debug_info_for_using (tree t, tree context)
   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);