OSDN Git Service

Restore canonical type comparison for dependent type(def)s
[pf3gnuchains/gcc-fork.git] / gcc / cp / name-lookup.c
index 0687258..0a93da8 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, 2010
+   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"
@@ -29,9 +29,9 @@ Boston, MA 02110-1301, USA.  */
 #include "name-lookup.h"
 #include "timevar.h"
 #include "toplev.h"
-#include "diagnostic.h"
+#include "diagnostic-core.h"
 #include "debug.h"
-#include "c-pragma.h"
+#include "c-family/c-pragma.h"
 
 /* The bindings for a particular name in a particular scope.  */
 
@@ -43,7 +43,6 @@ struct scope_binding {
 
 static cxx_scope *innermost_nonclass_level (void);
 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);
@@ -51,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.  */
 
@@ -63,18 +63,18 @@ static GTY(()) tree anonymous_namespace_name;
 /* Initialize anonymous_namespace_name if necessary, and return it.  */
 
 static tree
-get_anonymous_namespace_name(void)
+get_anonymous_namespace_name (void)
 {
   if (!anonymous_namespace_name)
     {
       /* The anonymous namespace has to have a unique name
         if typeinfo objects are being compared by name.  */
       if (! flag_weak || ! SUPPORTS_ONE_ONLY)
-       anonymous_namespace_name = get_file_function_name ("N");
+       anonymous_namespace_name = get_file_function_name ("N");
       else
-       /* The demangler expects anonymous namespaces to be called
-          something starting with '_GLOBAL__N_'.  */
-       anonymous_namespace_name = get_identifier ("_GLOBAL__N_1");
+       /* The demangler expects anonymous namespaces to be called
+          something starting with '_GLOBAL__N_'.  */
+       anonymous_namespace_name = get_identifier ("_GLOBAL__N_1");
     }
   return anonymous_namespace_name;
 }
@@ -102,7 +102,7 @@ binding_entry_make (tree name, tree type)
       free_binding_entry = entry->chain;
     }
   else
-    entry = GGC_NEW (struct binding_entry_s);
+    entry = ggc_alloc_binding_entry_s ();
 
   entry->name = name;
   entry->type = type;
@@ -125,13 +125,12 @@ binding_entry_free (binding_entry entry)
 
 /* The datatype used to implement the mapping from names to types at
    a given scope.  */
-struct binding_table_s GTY(())
-{
+struct GTY(()) binding_table_s {
   /* Array of chains of "binding_entry"s  */
   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.  */
@@ -145,7 +144,7 @@ binding_table_construct (binding_table table, size_t chain_count)
 {
   table->chain_count = chain_count;
   table->entry_count = 0;
-  table->chain = GGC_CNEWVEC (binding_entry, table->chain_count);
+  table->chain = ggc_alloc_cleared_vec_binding_entry (table->chain_count);
 }
 
 /* Make TABLE's entries ready for reuse.  */
@@ -179,7 +178,7 @@ binding_table_free (binding_table table)
 static inline binding_table
 binding_table_new (size_t chain_count)
 {
-  binding_table table = GGC_NEW (struct binding_table_s);
+  binding_table table = ggc_alloc_binding_table_s ();
   table->chain = NULL;
   binding_table_construct (table, chain_count);
   return table;
@@ -293,7 +292,7 @@ cxx_binding_make (tree value, tree type)
       free_bindings = binding->previous;
     }
   else
-    binding = GGC_NEW (cxx_binding);
+    binding = ggc_alloc_cxx_binding ();
 
   cxx_binding_init (binding, value, type);
 
@@ -328,9 +327,7 @@ new_class_binding (tree name, tree value, tree type, cxx_scope *scope)
          /* Fixup the current bindings, as they might have moved.  */
          size_t i;
 
-         for (i = 0;
-              VEC_iterate (cp_class_binding, scope->class_shadowed, i, cb);
-              i++)
+         FOR_EACH_VEC_ELT (cp_class_binding, scope->class_shadowed, i, cb)
            {
              cxx_binding **b;
              b = &IDENTIFIER_BINDING (cb->identifier);
@@ -536,17 +533,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;
+      DECL_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
@@ -605,6 +601,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;
 
@@ -702,7 +705,7 @@ pushdecl_maybe_friend (tree x, bool is_friend)
                      = htab_create_ggc (20, cxx_int_tree_map_hash,
                                         cxx_int_tree_map_eq, NULL);
 
-                 h = GGC_NEW (struct cxx_int_tree_map);
+                 h = ggc_alloc_cxx_int_tree_map ();
                  h->uid = DECL_UID (x);
                  h->to = t;
                  loc = htab_find_slot_with_hash
@@ -713,11 +716,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))
@@ -725,10 +727,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);
            }
@@ -765,8 +767,62 @@ pushdecl_maybe_friend (tree x, bool is_friend)
            }
        }
 
-      if (TREE_CODE (x) == FUNCTION_DECL || DECL_FUNCTION_TEMPLATE_P (x))
-       check_default_args (x);
+      /* 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_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);
+         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))
+               {
+                 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,
+                                         ce_normal))
+                   {
+                     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);
+                   }
+               }
+             else
+               {
+                 pedwarn (input_location, 0,
+                          "declaration of %q#D with C language linkage", x);
+                 pedwarn (input_location, 0,
+                          "conflicts with previous declaration %q+#D",
+                          previous);
+               }
+           }
+       }
 
       check_template_shadow (x);
 
@@ -778,11 +834,10 @@ pushdecl_maybe_friend (tree x, bool is_friend)
          SET_DECL_LANGUAGE (x, lang_c);
        }
 
+      t = x;
       if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_FUNCTION_MEMBER_P (x))
        {
          t = push_overloaded_decl (x, PUSH_LOCAL, is_friend);
-         if (t != x)
-           POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
          if (!namespace_bindings_p ())
            /* We do not need to create a binding for this name;
               push_overloaded_decl will have already done so if
@@ -794,33 +849,30 @@ pushdecl_maybe_friend (tree x, bool is_friend)
          t = push_overloaded_decl (x, PUSH_GLOBAL, is_friend);
          if (t == x)
            add_decl_to_level (x, NAMESPACE_LEVEL (CP_DECL_CONTEXT (t)));
-         POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
        }
 
+      if (TREE_CODE (t) == FUNCTION_DECL || DECL_FUNCTION_TEMPLATE_P (t))
+       check_default_args (t);
+
+      if (t != x || DECL_FUNCTION_TEMPLATE_P (t))
+       POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
+
       /* If declaring a type as a typedef, copy the type (unless we're
         at line 0), and install this TYPE_DECL as the new type's typedef
-        name.  See the extensive comment in ../c-decl.c (pushdecl).  */
+        name.  See the extensive comment of set_underlying_type ().  */
       if (TREE_CODE (x) == TYPE_DECL)
        {
          tree type = TREE_TYPE (x);
-         if (DECL_IS_BUILTIN (x))
-           {
-             if (TYPE_NAME (type) == 0)
-               TYPE_NAME (type) = x;
-           }
-         else if (type != error_mark_node && TYPE_NAME (type) != x
-                  /* We don't want to copy the type when all we're
-                     doing is making a TYPE_DECL for the purposes of
-                     inlining.  */
-                  && (!TYPE_NAME (type)
-                      || TYPE_NAME (type) != DECL_ABSTRACT_ORIGIN (x)))
-           {
-             DECL_ORIGINAL_TYPE (x) = type;
-             type = build_variant_type_copy (type);
-             TYPE_STUB_DECL (type) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
-             TYPE_NAME (type) = x;
-             TREE_TYPE (x) = type;
-           }
+
+         if (DECL_IS_BUILTIN (x)
+             || (TREE_TYPE (x) != error_mark_node
+                 && TYPE_NAME (type) != x
+                 /* We don't want to copy the type when all we're
+                    doing is making a TYPE_DECL for the purposes of
+                    inlining.  */
+                 && (!TYPE_NAME (type)
+                     || TYPE_NAME (type) != DECL_ABSTRACT_ORIGIN (x))))
+           set_underlying_type (x);
 
          if (type != error_mark_node
              && TYPE_NAME (type)
@@ -848,8 +900,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);
            }
        }
 
@@ -953,19 +1005,39 @@ pushdecl_maybe_friend (tree x, bool is_friend)
              && TREE_PUBLIC (x))
            TREE_PUBLIC (name) = 1;
 
+         /* Don't complain about the parms we push and then pop
+            while tentatively parsing a function declarator.  */
+         if (TREE_CODE (x) == PARM_DECL && DECL_CONTEXT (x) == NULL_TREE)
+           /* Ignore.  */;
+
          /* Warn if shadowing an argument at the top level of the body.  */
-         if (oldlocal != NULL_TREE && !DECL_EXTERNAL (x)
-             /* Inline decls shadow nothing.  */
-             && !DECL_FROM_INLINE (x)
-             && TREE_CODE (oldlocal) == PARM_DECL
-             /* Don't check the `this' parameter.  */
-             && !DECL_ARTIFICIAL (oldlocal))
+         else if (oldlocal != NULL_TREE && !DECL_EXTERNAL (x)
+                  /* Inline decls shadow nothing.  */
+                  && !DECL_FROM_INLINE (x)
+                  && (TREE_CODE (oldlocal) == PARM_DECL
+                      || TREE_CODE (oldlocal) == VAR_DECL
+                       /* If the old decl is a type decl, only warn if the
+                          old decl is an explicit typedef or if both the old
+                          and new decls are type decls.  */
+                       || (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).  */
+                  && (!DECL_ARTIFICIAL (x) || DECL_IMPLICIT_TYPEDEF_P (x)))
            {
-             bool err = false;
+             bool nowarn = false;
 
              /* Don't complain if it's from an enclosing function.  */
              if (DECL_CONTEXT (oldlocal) == current_function_decl
-                 && TREE_CODE (x) != PARM_DECL)
+                 && TREE_CODE (x) != PARM_DECL
+                 && TREE_CODE (oldlocal) == PARM_DECL)
                {
                  /* Go to where the parms should be and see if we find
                     them there.  */
@@ -979,23 +1051,52 @@ pushdecl_maybe_friend (tree x, bool is_friend)
                  if (b->kind == sk_function_parms)
                    {
                      error ("declaration of %q#D shadows a parameter", x);
-                     err = true;
+                     nowarn = true;
                    }
                }
 
-             if (warn_shadow && !err)
+             /* The local structure or class can't use parameters of
+                the containing function anyway.  */
+             if (DECL_CONTEXT (oldlocal) != current_function_decl)
+               {
+                 cxx_scope *scope = current_binding_level;
+                 tree context = DECL_CONTEXT (oldlocal);
+                 for (; scope; scope = scope->level_chain)
+                  {
+                    if (scope->kind == sk_function_parms
+                        && scope->this_entity == context)
+                     break;
+                    if (scope->kind == sk_class
+                        && !LAMBDA_TYPE_P (scope->this_entity))
+                      {
+                        nowarn = true;
+                        break;
+                      }
+                  }
+               }
+
+             if (warn_shadow && !nowarn)
                {
-                 warning (OPT_Wshadow, "declaration of %q#D shadows a parameter", x);
-                 warning (OPT_Wshadow, "%Jshadowed declaration is here", oldlocal);
+                 if (TREE_CODE (oldlocal) == PARM_DECL)
+                   warning_at (input_location, OPT_Wshadow,
+                               "declaration of %q#D shadows a parameter", x);
+                 else
+                   warning_at (input_location, OPT_Wshadow,
+                               "declaration of %qD shadows a previous local",
+                               x);
+                  warning_at (DECL_SOURCE_LOCATION (oldlocal), OPT_Wshadow,
+                              "shadowed declaration is here");
                }
            }
 
          /* Maybe warn if shadowing something else.  */
          else if (warn_shadow && !DECL_EXTERNAL (x)
-             /* No shadow warnings for internally generated vars.  */
-             && ! DECL_ARTIFICIAL (x)
-             /* No shadow warnings for vars made for inlining.  */
-             && ! DECL_FROM_INLINE (x))
+                   /* No shadow warnings for internally generated vars unless
+                      it's an implicit typedef (see create_implicit_typedef
+                      in decl.c).  */
+                   && (! DECL_ARTIFICIAL (x) || DECL_IMPLICIT_TYPEDEF_P (x))
+                   /* No shadow warnings for vars made for inlining.  */
+                   && ! DECL_FROM_INLINE (x))
            {
              tree member;
 
@@ -1013,19 +1114,20 @@ pushdecl_maybe_friend (tree x, bool is_friend)
                  warning (OPT_Wshadow, "declaration of %qD shadows a member of 'this'",
                           x);
                }
-             else if (oldlocal != NULL_TREE
-                      && TREE_CODE (oldlocal) == VAR_DECL)
-               {
-                 warning (OPT_Wshadow, "declaration of %qD shadows a previous local", x);
-                 warning (OPT_Wshadow, "%Jshadowed declaration is here", oldlocal);
-               }
              else if (oldglobal != NULL_TREE
-                      && TREE_CODE (oldglobal) == VAR_DECL)
+                      && (TREE_CODE (oldglobal) == VAR_DECL
+                           /* If the old decl is a type decl, only warn if the
+                              old decl is an explicit typedef or if both the
+                              old and new decls are type decls.  */
+                           || (TREE_CODE (oldglobal) == TYPE_DECL
+                               && (!DECL_ARTIFICIAL (oldglobal)
+                                   || TREE_CODE (x) == TYPE_DECL))))
                /* XXX shadow warnings in outer-more namespaces */
                {
-                 warning (OPT_Wshadow, "declaration of %qD shadows a global declaration",
-                          x);
-                 warning (OPT_Wshadow, "%Jshadowed declaration is here", oldglobal);
+                 warning_at (input_location, OPT_Wshadow,
+                             "declaration of %qD shadows a global declaration", x);
+                 warning_at (DECL_SOURCE_LOCATION (oldglobal), OPT_Wshadow,
+                             "shadowed declaration is here");
                }
            }
        }
@@ -1067,9 +1169,9 @@ maybe_push_decl (tree decl)
          && DECL_CONTEXT (decl) != NULL_TREE
          /* Definitions of namespace members outside their namespace are
             possible.  */
-         && TREE_CODE (DECL_CONTEXT (decl)) != NAMESPACE_DECL)
+         && !DECL_NAMESPACE_SCOPE_P (decl))
       || (TREE_CODE (decl) == TEMPLATE_DECL && !namespace_bindings_p ())
-      || TREE_CODE (type) == UNKNOWN_TYPE
+      || type == unknown_type_node
       /* The declaration of a template specialization does not affect
         the functions available for overload resolution, so we do not
         call pushdecl.  */
@@ -1167,7 +1269,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);
@@ -1175,9 +1277,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;
@@ -1188,7 +1300,6 @@ check_for_out_of_scope_variable (tree decl)
 static bool keep_next_level_flag;
 
 static int binding_depth = 0;
-static int is_class_level = 0;
 
 static void
 indent (int depth)
@@ -1270,14 +1381,13 @@ push_binding_level (struct cp_binding_level *scope)
       scope->binding_depth = binding_depth;
       indent (binding_depth);
       cxx_scope_debug (scope, input_line, "push");
-      is_class_level = 0;
       binding_depth++;
     }
 }
 
 /* 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)
@@ -1292,7 +1402,7 @@ begin_scope (scope_kind kind, tree entity)
       free_binding_level = scope->level_chain;
     }
   else
-    scope = GGC_CNEW (cxx_scope);
+    scope = ggc_alloc_cleared_cxx_scope ();
 
   scope->this_entity = entity;
   scope->more_cleanups_ok = true;
@@ -1312,6 +1422,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;
@@ -1357,19 +1468,8 @@ leave_scope (void)
     {
       indent (--binding_depth);
       cxx_scope_debug (scope, input_line, "leave");
-      if (is_class_level != (scope == class_binding_level))
-       {
-         indent (binding_depth);
-         verbatim ("XXX is_class_level != (current_scope == class_scope)\n");
-       }
-      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;
 
@@ -1417,7 +1517,6 @@ resume_scope (struct cp_binding_level* b)
       b->binding_depth = binding_depth;
       indent (binding_depth);
       cxx_scope_debug (b, input_line, "resume");
-      is_class_level = 0;
       binding_depth++;
     }
 }
@@ -1494,7 +1593,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.  */
@@ -1585,9 +1685,7 @@ print_binding_level (struct cp_binding_level* lvl)
       size_t i;
       cp_class_binding *b;
       fprintf (stderr, " class-shadowed:");
-      for (i = 0;
-          VEC_iterate(cp_class_binding, lvl->class_shadowed, i, b);
-          ++i)
+      FOR_EACH_VEC_ELT (cp_class_binding, lvl->class_shadowed, i, b)
        fprintf (stderr, " %s ", IDENTIFIER_POINTER (b->identifier));
       fprintf (stderr, "\n");
     }
@@ -1736,13 +1834,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;
 
@@ -1774,6 +1875,23 @@ make_anon_name (void)
   return get_identifier (buf);
 }
 
+/* This code is practically identical to that for creating
+   anonymous names, but is just used for lambdas instead.  This is necessary
+   because anonymous names are recognized and cannot be passed to template
+   functions.  */
+/* FIXME is this still necessary? */
+
+static GTY(()) int lambda_cnt = 0;
+
+tree
+make_lambda_name (void)
+{
+  char buf[32];
+
+  sprintf (buf, LAMBDANAME_FORMAT, lambda_cnt++);
+  return get_identifier (buf);
+}
+
 /* Return (from the stack of) the BINDING, if any, established at SCOPE.  */
 
 static inline cxx_binding *
@@ -1825,6 +1943,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
@@ -1838,7 +1989,7 @@ push_using_decl (tree scope, tree name)
   timevar_push (TV_NAME_LOOKUP);
   gcc_assert (TREE_CODE (scope) == NAMESPACE_DECL);
   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
-  for (decl = current_binding_level->usings; decl; decl = TREE_CHAIN (decl))
+  for (decl = current_binding_level->usings; decl; decl = DECL_CHAIN (decl))
     if (USING_DECL_SCOPE (decl) == scope && DECL_NAME (decl) == name)
       break;
   if (decl)
@@ -1846,7 +1997,7 @@ push_using_decl (tree scope, tree name)
                            namespace_bindings_p () ? decl : NULL_TREE);
   decl = build_lang_decl (USING_DECL, name, NULL_TREE);
   USING_DECL_SCOPE (decl) = scope;
-  TREE_CHAIN (decl) = current_binding_level->usings;
+  DECL_CHAIN (decl) = current_binding_level->usings;
   current_binding_level->usings = decl;
   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
 }
@@ -1920,10 +2071,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))
@@ -2099,6 +2250,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.  */
@@ -2108,95 +2273,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))
+      /* Check for using functions.  */
+      if (is_overloaded_fn (decls.value))
        {
-         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))
-       {
-         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)
     {
-      *newval = decls.value;
-      if (oldval && !decls_match (*newval, oldval))
-       error ("%qD is already declared in this scope", name);
+      error ("reference to %qD is ambiguous", name);
+      print_candidates (decls.type);
     }
-
-  *newtype = decls.type;
-  if (oldtype && *newtype && !same_type_p (oldtype, *newtype))
+  else
     {
-      error ("using declaration %qD introduced ambiguous type %qT",
-            name, oldtype);
-      return;
+      *newtype = decls.type;
+      if (oldtype && *newtype && !decls_match (oldtype, *newtype))
+       error ("%qD is already declared in this scope", name);
     }
+
+    /* 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.  */
@@ -2316,6 +2496,8 @@ push_scope (tree t)
 void
 pop_scope (tree t)
 {
+  if (t == NULL_TREE)
+    return;
   if (TREE_CODE (t) == NAMESPACE_DECL)
     pop_decl_namespace ();
   else if CLASS_TYPE_P (t)
@@ -2431,9 +2613,6 @@ pop_inner_scope (tree outer, tree inner)
 void
 pushlevel_class (void)
 {
-  if (ENABLE_SCOPE_CHECKING)
-    is_class_level = 1;
-
   class_binding_level = begin_scope (sk_class, current_class_type);
 }
 
@@ -2461,9 +2640,7 @@ poplevel_class (void)
   /* Remove the bindings for all of the class-level declarations.  */
   if (level->class_shadowed)
     {
-      for (i = 0;
-          VEC_iterate (cp_class_binding, level->class_shadowed, i, cb);
-          ++i)
+      FOR_EACH_VEC_ELT (cp_class_binding, level->class_shadowed, i, cb)
        IDENTIFIER_BINDING (cb->identifier) = cb->base.previous;
       ggc_free (level->class_shadowed);
       level->class_shadowed = NULL;
@@ -2471,9 +2648,7 @@ poplevel_class (void)
 
   /* Now, pop out of the binding level which we created up in the
      `pushlevel_class' routine.  */
-  if (ENABLE_SCOPE_CHECKING)
-    is_class_level = 1;
-
+  gcc_assert (current_binding_level == level);
   leave_scope ();
   timevar_pop (TV_NAME_LOOKUP);
 }
@@ -2520,6 +2695,11 @@ pushdecl_class_level (tree x)
   tree name;
   bool is_valid = true;
 
+  /* Do nothing if we're adding to an outer lambda closure type,
+     outer_binding will add it later if it's needed.  */
+  if (current_class_type != class_binding_level->this_entity)
+    return true;
+
   timevar_push (TV_NAME_LOOKUP);
   /* Get the name of X.  */
   if (TREE_CODE (x) == OVERLOAD)
@@ -2540,7 +2720,7 @@ pushdecl_class_level (tree x)
         aggregate, for naming purposes.  */
       tree f;
 
-      for (f = TYPE_FIELDS (TREE_TYPE (x)); f; f = TREE_CHAIN (f))
+      for (f = TYPE_FIELDS (TREE_TYPE (x)); f; f = DECL_CHAIN (f))
        {
          location_t save_location = input_location;
          input_location = DECL_SOURCE_LOCATION (f);
@@ -2634,6 +2814,9 @@ push_class_level_binding (tree name, tree x)
 
   /* Check for invalid member names.  */
   gcc_assert (TYPE_BEING_DEFINED (current_class_type));
+  /* Check that we're pushing into the right binding level.  */
+  gcc_assert (current_class_type == class_binding_level->this_entity);
+
   /* We could have been passed a tree list if this is an ambiguous
      declaration. If so, pull the declaration out because
      check_template_shadow will not handle a TREE_LIST.  */
@@ -2641,7 +2824,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]
 
@@ -2801,7 +2985,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;
@@ -2884,7 +3068,7 @@ namespace_binding (tree name, tree scope)
 {
   cxx_binding *binding;
 
-  if (scope == NULL)
+  if (SCOPE_FILE_SCOPE_P (scope))
     scope = global_namespace;
   else
     /* Unnecessary for the global namespace because it can't be an alias. */
@@ -2919,7 +3103,7 @@ set_namespace_binding (tree name, tree scope, tree val)
 void
 set_decl_namespace (tree decl, tree scope, bool friendp)
 {
-  tree old, fn;
+  tree old;
 
   /* Get rid of namespace aliases.  */
   scope = ORIGINAL_NAMESPACE (scope);
@@ -2941,20 +3125,29 @@ 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 it's a TREE_LIST, the result of the lookup was ambiguous.  */
+  if (TREE_CODE (old) == TREE_LIST)
+    {
+      error ("reference to %qD is ambiguous", decl);
+      print_candidates (old);
+      return;
+    }
   if (!is_overloaded_fn (decl))
-    /* Don't compare non-function decls with decls_match here, since
-       it can't check for the correct constness at this
-       point. pushdecl will find those errors later.  */
-    return;
+    {
+      /* We might have found OLD in an inline namespace inside SCOPE.  */
+      if (TREE_CODE (decl) == TREE_CODE (old))
+       DECL_CONTEXT (decl) = DECL_CONTEXT (old);
+      /* Don't compare non-function decls with decls_match here, since
+        it can't check for the correct constness at this
+        point. pushdecl will find those errors later.  */
+      return;
+    }
   /* Since decl is a function, old should contain a function decl.  */
   if (!is_overloaded_fn (old))
     goto complain;
-  fn = OVL_CURRENT (old);
-  if (!is_associated_namespace (scope, CP_DECL_CONTEXT (fn)))
-    goto complain;
   /* A template can be explicitly specialized in any namespace.  */
   if (processing_explicit_instantiation)
     return;
@@ -2970,25 +3163,56 @@ set_decl_namespace (tree decl, tree scope, bool friendp)
     return;
   if (is_overloaded_fn (old))
     {
-      for (; old; old = OVL_NEXT (old))
-       if (decls_match (decl, OVL_CURRENT (old)))
+      tree found = NULL_TREE;
+      tree elt = old;
+      for (; elt; elt = OVL_NEXT (elt))
+       {
+         tree ofn = OVL_CURRENT (elt);
+         /* Adjust DECL_CONTEXT first so decls_match will return true
+            if DECL will match a declaration in an inline namespace.  */
+         DECL_CONTEXT (decl) = DECL_CONTEXT (ofn);
+         if (decls_match (decl, ofn))
+           {
+             if (found && !decls_match (found, ofn))
+               {
+                 DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
+                 error ("reference to %qD is ambiguous", decl);
+                 print_candidates (old);
+                 return;
+               }
+             found = ofn;
+           }
+       }
+      if (found)
+       {
+         if (!is_associated_namespace (scope, CP_DECL_CONTEXT (found)))
+           goto complain;
+         DECL_CONTEXT (decl) = DECL_CONTEXT (found);
          return;
+       }
     }
-  else if (decls_match (decl, old))
-      return;
+  else
+    {
+      DECL_CONTEXT (decl) = DECL_CONTEXT (old);
+      if (decls_match (decl, old))
+       return;
+    }
+
+  /* It didn't work, go back to the explicit scope.  */
+  DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
  complain:
   error ("%qD should have been declared inside %qD", decl, scope);
 }
 
 /* Return the namespace where the current declaration is declared.  */
 
-static tree
+tree
 current_decl_namespace (void)
 {
   tree result;
   /* If we have been pushed into a different namespace, use it.  */
-  if (decl_namespace_list)
-    return TREE_PURPOSE (decl_namespace_list);
+  if (!VEC_empty (tree, decl_namespace_list))
+    return VEC_last (tree, decl_namespace_list);
 
   if (current_class_type)
     result = decl_namespace_context (current_class_type);
@@ -2999,20 +3223,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;
 
-/* Same, but specify attributes to apply to the namespace.  The attributes
-   only apply to the current namespace-body, not to any later extensions. */
+  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);
+
+         push_visibility (TREE_STRING_POINTER (x), 1);
+         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;
@@ -3079,38 +3342,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);
 }
 
@@ -3147,6 +3378,7 @@ void
 pop_nested_namespace (tree ns)
 {
   timevar_push (TV_NAME_LOOKUP);
+  gcc_assert (current_namespace == ns);
   while (ns != global_namespace)
     {
       pop_namespace ();
@@ -3164,8 +3396,7 @@ push_decl_namespace (tree decl)
 {
   if (TREE_CODE (decl) != NAMESPACE_DECL)
     decl = decl_namespace_context (decl);
-  decl_namespace_list = tree_cons (ORIGINAL_NAMESPACE (decl),
-                                  NULL_TREE, decl_namespace_list);
+  VEC_safe_push (tree, gc, decl_namespace_list, ORIGINAL_NAMESPACE (decl));
 }
 
 /* [namespace.memdef]/2 */
@@ -3173,7 +3404,7 @@ push_decl_namespace (tree decl)
 void
 pop_decl_namespace (void)
 {
-  decl_namespace_list = TREE_CHAIN (decl_namespace_list);
+  VEC_pop (tree, decl_namespace_list);
 }
 
 /* Return the namespace that is the common ancestor
@@ -3192,24 +3423,25 @@ 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);
 
   /* Emit debug info for namespace alias.  */
-  (*debug_hooks->global_decl) (alias);
+  if (!building_stmt_tree ())
+    (*debug_hooks->global_decl) (alias);
 }
 
 /* Like pushdecl, only it places X in the current namespace,
@@ -3341,46 +3573,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 (input_location, 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))
     {
@@ -3389,14 +3621,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
@@ -3415,7 +3647,7 @@ pushdecl_top_level_1 (tree x, tree *init, bool is_friend)
   push_to_top_level ();
   x = pushdecl_namespace_level (x, is_friend);
   if (init)
-    finish_decl (x, *init, NULL_TREE);
+    cp_finish_decl (x, *init, false, NULL_TREE, 0);
   pop_from_top_level ();
   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
 }
@@ -3468,10 +3700,9 @@ merge_functions (tree s1, tree s2)
          /* If the function from S2 is already in S1, there is no
             need to add it again.  For `extern "C"' functions, we
             might have two FUNCTION_DECLs for the same function, in
-            different namespaces; again, we only need one of them.  */
-         if (fn1 == fn2
-             || (DECL_EXTERN_C_P (fn1) && DECL_EXTERN_C_P (fn2)
-                 && DECL_NAME (fn1) == DECL_NAME (fn2)))
+            different namespaces, but let's leave them in in case
+            they have different default arguments.  */
+         if (fn1 == fn2)
            break;
        }
 
@@ -3482,29 +3713,53 @@ merge_functions (tree s1, tree s2)
   return s1;
 }
 
+/* Returns TRUE iff OLD and NEW are the same entity.
+
+   3 [basic]/3: An entity is a value, object, reference, function,
+   enumerator, type, class member, template, template specialization,
+   namespace, parameter pack, or this.
+
+   7.3.4 [namespace.udir]/4: If name lookup finds a declaration for a name
+   in two different namespaces, and the declarations do not declare the
+   same entity and do not declare functions, the use of the name is
+   ill-formed.  */
+
+static bool
+same_entity_p (tree one, tree two)
+{
+  if (one == two)
+    return true;
+  if (!one || !two)
+    return false;
+  if (TREE_CODE (one) == TYPE_DECL
+      && TREE_CODE (two) == TYPE_DECL
+      && same_type_p (TREE_TYPE (one), TREE_TYPE (two)))
+    return true;
+  return false;
+}
+
 /* 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->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)
     {
       if (hidden_name_p (val) && !(flags & LOOKUP_HIDDEN))
@@ -3548,7 +3803,7 @@ ambiguous_decl (tree name, struct scope_binding *old, cxx_binding *new,
 
   if (!old->value)
     old->value = val;
-  else if (val && val != old->value)
+  else if (val && !same_entity_p (val, old->value))
     {
       if (is_overloaded_fn (old->value) && is_overloaded_fn (val))
        old->value = merge_functions (old->value, val);
@@ -3564,12 +3819,9 @@ ambiguous_decl (tree name, struct scope_binding *old, cxx_binding *new,
     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;
     }
 }
 
@@ -3611,6 +3863,14 @@ qualify_lookup (tree val, int flags)
     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))
+    return false;
   return true;
 }
 
@@ -3680,7 +3940,7 @@ unqualified_namespace_lookup (tree name, int flags)
         cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
 
       if (b)
-       ambiguous_decl (name, &binding, b, flags);
+       ambiguous_decl (&binding, b, flags);
 
       /* Add all _DECLs seen through local using-directives.  */
       for (level = current_binding_level;
@@ -3737,7 +3997,9 @@ lookup_qualified_name (tree scope, tree name, bool is_type_p, bool complain)
       if (qualified_lookup_using_namespace (name, scope, &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)
@@ -3768,11 +4030,24 @@ 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);
 }
 
+/* Returns true iff VEC contains TARGET.  */
+
+static bool
+tree_vec_contains (VEC(tree,gc)* vec, tree target)
+{
+  unsigned int i;
+  tree elt;
+  FOR_EACH_VEC_ELT (tree,vec,i,elt)
+    if (elt == target)
+      return true;
+  return false;
+}
+
 /* [namespace.qual]
    Accepts the NAME to lookup and its qualifying SCOPE.
    Returns the name/type pair found into the cxx_binding *RESULT,
@@ -3783,68 +4058,103 @@ qualified_lookup_using_namespace (tree name, tree scope,
                                  struct scope_binding *result, int flags)
 {
   /* Maintain a list of namespaces visited...  */
-  tree seen = NULL_TREE;
+  VEC(tree,gc) *seen = NULL;
+  VEC(tree,gc) *seen_inline = NULL;
   /* ... and a list of namespace yet to see.  */
-  tree todo = NULL_TREE;
-  tree todo_maybe = NULL_TREE;
+  VEC(tree,gc) *todo = NULL;
+  VEC(tree,gc) *todo_maybe = NULL;
+  VEC(tree,gc) *todo_inline = NULL;
   tree usings;
   timevar_push (TV_NAME_LOOKUP);
   /* Look through namespace aliases.  */
   scope = ORIGINAL_NAMESPACE (scope);
-  while (scope && result->value != error_mark_node)
+
+  /* Algorithm: Starting with SCOPE, walk through the the set of used
+     namespaces.  For each used namespace, look through its inline
+     namespace set for any bindings and usings.  If no bindings are found,
+     add any usings seen to the set of used namespaces.  */
+  VEC_safe_push (tree, gc, todo, scope);
+
+  while (VEC_length (tree, todo))
     {
-      cxx_binding *binding =
-       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);
-
-      /* Consider strong using directives always, and non-strong ones
-        if we haven't found a binding yet.  ??? Shouldn't we consider
-        non-strong ones if the initial RESULT is non-NULL, but the
-        binding in the given namespace is?  */
-      for (usings = DECL_NAMESPACE_USING (scope); usings;
-          usings = TREE_CHAIN (usings))
-       /* If this was a real directive, and we have not seen it.  */
-       if (!TREE_INDIRECT_USING (usings))
-         {
-           /* Try to avoid queuing the same namespace more than once,
-              the exception being when a namespace was already
-              enqueued for todo_maybe and then a strong using is
-              found for it.  We could try to remove it from
-              todo_maybe, but it's probably not worth the effort.  */
-           if (is_associated_namespace (scope, TREE_PURPOSE (usings))
-               && !purpose_member (TREE_PURPOSE (usings), seen)
-               && !purpose_member (TREE_PURPOSE (usings), todo))
-             todo = tree_cons (TREE_PURPOSE (usings), NULL_TREE, todo);
-           else if ((!result->value && !result->type)
-                    && !purpose_member (TREE_PURPOSE (usings), seen)
-                    && !purpose_member (TREE_PURPOSE (usings), todo)
-                    && !purpose_member (TREE_PURPOSE (usings), todo_maybe))
-             todo_maybe = tree_cons (TREE_PURPOSE (usings), NULL_TREE,
-                                     todo_maybe);
-         }
-      if (todo)
+      bool found_here;
+      scope = VEC_pop (tree, todo);
+      if (tree_vec_contains (seen, scope))
+       continue;
+      VEC_safe_push (tree, gc, seen, scope);
+      VEC_safe_push (tree, gc, todo_inline, scope);
+
+      found_here = false;
+      while (VEC_length (tree, todo_inline))
        {
-         scope = TREE_PURPOSE (todo);
-         todo = TREE_CHAIN (todo);
-       }
-      else if (todo_maybe
-              && (!result->value && !result->type))
-       {
-         scope = TREE_PURPOSE (todo_maybe);
-         todo = TREE_CHAIN (todo_maybe);
-         todo_maybe = NULL_TREE;
+         cxx_binding *binding;
+
+         scope = VEC_pop (tree, todo_inline);
+         if (tree_vec_contains (seen_inline, scope))
+           continue;
+         VEC_safe_push (tree, gc, seen_inline, scope);
+
+         binding =
+           cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
+         if (binding)
+           {
+             found_here = true;
+             ambiguous_decl (result, binding, flags);
+           }
+
+         for (usings = DECL_NAMESPACE_USING (scope); usings;
+              usings = TREE_CHAIN (usings))
+           if (!TREE_INDIRECT_USING (usings))
+             {
+               if (is_associated_namespace (scope, TREE_PURPOSE (usings)))
+                 VEC_safe_push (tree, gc, todo_inline, TREE_PURPOSE (usings));
+               else
+                 VEC_safe_push (tree, gc, todo_maybe, TREE_PURPOSE (usings));
+             }
        }
+
+      if (found_here)
+       VEC_truncate (tree, todo_maybe, 0);
       else
-       scope = NULL_TREE; /* If there never was a todo list.  */
+       while (VEC_length (tree, todo_maybe))
+         VEC_safe_push (tree, gc, todo, VEC_pop (tree, todo_maybe));
     }
+  VEC_free (tree,gc,todo);
+  VEC_free (tree,gc,todo_maybe);
+  VEC_free (tree,gc,todo_inline);
+  VEC_free (tree,gc,seen);
+  VEC_free (tree,gc,seen_inline);
   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,
@@ -3892,6 +4202,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;
       }
 
@@ -3990,8 +4310,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;
          }
@@ -4015,13 +4383,13 @@ lookup_name_nonclass (tree name)
 }
 
 tree
-lookup_function_nonclass (tree name, tree args, bool block_p)
+lookup_function_nonclass (tree name, VEC(tree,gc) *args, bool block_p)
 {
   return
     lookup_arg_dependent (name,
                          lookup_name_real (name, 0, 1, block_p, 0,
                                            LOOKUP_COMPLAIN),
-                         args);
+                         args, false);
 }
 
 tree
@@ -4116,7 +4484,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)
@@ -4132,7 +4501,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;
@@ -4171,6 +4540,34 @@ lookup_name_innermost_nonclass_level (tree name)
   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
 }
 
+/* Returns true iff DECL is a block-scope extern declaration of a function
+   or variable.  */
+
+bool
+is_local_extern (tree decl)
+{
+  cxx_binding *binding;
+
+  /* For functions, this is easy.  */
+  if (TREE_CODE (decl) == FUNCTION_DECL)
+    return DECL_LOCAL_FUNCTION_P (decl);
+
+  if (TREE_CODE (decl) != VAR_DECL)
+    return false;
+  if (!current_function_decl)
+    return false;
+
+  /* For variables, this is not easy.  We need to look at the binding stack
+     for the identifier to see whether the decl we have is a local.  */
+  for (binding = IDENTIFIER_BINDING (DECL_NAME (decl));
+       binding && binding->scope->kind != sk_namespace;
+       binding = binding->previous)
+    if (binding->value == decl)
+      return LOCAL_BINDING_P (binding);
+
+  return false;
+}
+
 /* Like lookup_name_innermost_nonclass_level, but for types.  */
 
 static tree
@@ -4206,17 +4603,20 @@ lookup_type_current_level (tree name)
 struct arg_lookup
 {
   tree name;
-  tree args;
-  tree namespaces;
-  tree classes;
+  VEC(tree,gc) *args;
+  VEC(tree,gc) *namespaces;
+  VEC(tree,gc) *classes;
   tree functions;
 };
 
 static bool arg_assoc (struct arg_lookup*, tree);
 static bool arg_assoc_args (struct arg_lookup*, tree);
+static bool arg_assoc_args_vec (struct arg_lookup*, VEC(tree,gc) *);
 static bool arg_assoc_type (struct arg_lookup*, tree);
 static bool add_function (struct arg_lookup *, tree);
 static bool arg_assoc_namespace (struct arg_lookup *, tree);
+static bool arg_assoc_class_only (struct arg_lookup *, tree);
+static bool arg_assoc_bases (struct arg_lookup *, tree);
 static bool arg_assoc_class (struct arg_lookup *, tree);
 static bool arg_assoc_template_arg (struct arg_lookup*, tree);
 
@@ -4233,26 +4633,15 @@ add_function (struct arg_lookup *k, tree fn)
      total number of functions being compared, which should usually be the
      case.  */
 
-  /* We must find only functions, or exactly one non-function.  */
-  if (!k->functions)
+  if (!is_overloaded_fn (fn))
+    /* All names except those of (possibly overloaded) functions and
+       function templates are ignored.  */;
+  else if (!k->functions)
     k->functions = fn;
   else if (fn == k->functions)
     ;
-  else if (is_overloaded_fn (k->functions) && is_overloaded_fn (fn))
-    k->functions = build_overload (fn, k->functions);
   else
-    {
-      tree f1 = OVL_CURRENT (k->functions);
-      tree f2 = fn;
-      if (is_overloaded_fn (f1))
-       {
-         fn = f1; f1 = f2; f2 = fn;
-       }
-      error ("%q+D is not a function,", f1);
-      error ("  conflict with %q+D", f2);
-      error ("  in call to %qD", k->name);
-      return true;
-    }
+    k->functions = build_overload (fn, k->functions);
 
   return false;
 }
@@ -4264,72 +4653,38 @@ add_function (struct arg_lookup *k, tree fn)
 bool
 is_associated_namespace (tree current, tree scope)
 {
-  tree seen = NULL_TREE;
-  tree todo = NULL_TREE;
+  VEC(tree,gc) *seen = make_tree_vector ();
+  VEC(tree,gc) *todo = make_tree_vector ();
   tree t;
+  bool ret;
+
   while (1)
     {
       if (scope == current)
-       return true;
-      seen = tree_cons (scope, NULL_TREE, seen);
+       {
+         ret = true;
+         break;
+       }
+      VEC_safe_push (tree, gc, seen, scope);
       for (t = DECL_NAMESPACE_ASSOCIATIONS (scope); t; t = TREE_CHAIN (t))
-       if (!purpose_member (TREE_PURPOSE (t), seen))
-         todo = tree_cons (TREE_PURPOSE (t), NULL_TREE, todo);
-      if (todo)
+       if (!vec_member (TREE_PURPOSE (t), seen))
+         VEC_safe_push (tree, gc, todo, TREE_PURPOSE (t));
+      if (!VEC_empty (tree, todo))
        {
-         scope = TREE_PURPOSE (todo);
-         todo = TREE_CHAIN (todo);
+         scope = VEC_last (tree, todo);
+         VEC_pop (tree, todo);
        }
       else
-       return false;
-    }
-}
-
-/* Return whether FN is a friend of an associated class of ARG.  */
-
-static bool
-friend_of_associated_class_p (tree arg, tree fn)
-{
-  tree type;
-
-  if (TYPE_P (arg))
-    type = arg;
-  else if (type_unknown_p (arg))
-    return false;
-  else
-    type = TREE_TYPE (arg);
-
-  /* If TYPE is a class, the class itself and all base classes are
-     associated classes.  */
-  if (CLASS_TYPE_P (type))
-    {
-      if (is_friend (type, fn))
-       return true;
-
-      if (TYPE_BINFO (type))
        {
-         tree binfo, base_binfo;
-         int i;
-
-         for (binfo = TYPE_BINFO (type), i = 0;
-              BINFO_BASE_ITERATE (binfo, i, base_binfo);
-              i++)
-           if (is_friend (BINFO_TYPE (base_binfo), fn))
-             return true;
+         ret = false;
+         break;
        }
     }
 
-  /* If TYPE is a class member, the class of which it is a member is
-     an associated class.  */
-  if ((CLASS_TYPE_P (type)
-       || TREE_CODE (type) == UNION_TYPE
-       || TREE_CODE (type) == ENUMERAL_TYPE)
-      && TYPE_CONTEXT (type)
-      && CLASS_TYPE_P (TYPE_CONTEXT (type))
-      && is_friend (TYPE_CONTEXT (type), fn))
-    return true;
+  release_tree_vector (seen);
+  release_tree_vector (todo);
 
-  return false;
+  return ret;
 }
 
 /* Add functions of a namespace to the lookup structure.
@@ -4340,9 +4695,9 @@ arg_assoc_namespace (struct arg_lookup *k, tree scope)
 {
   tree value;
 
-  if (purpose_member (scope, k->namespaces))
-    return 0;
-  k->namespaces = tree_cons (scope, NULL_TREE, k->namespaces);
+  if (vec_member (scope, k->namespaces))
+    return false;
+  VEC_safe_push (tree, gc, k->namespaces, scope);
 
   /* Check out our super-users.  */
   for (value = DECL_NAMESPACE_ASSOCIATIONS (scope); value;
@@ -4350,6 +4705,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;
@@ -4358,18 +4720,9 @@ arg_assoc_namespace (struct arg_lookup *k, tree scope)
     {
       /* We don't want to find arbitrary hidden functions via argument
         dependent lookup.  We only want to find friends of associated
-        classes.  */
+        classes, which we'll do via arg_assoc_class.  */
       if (hidden_name_p (OVL_CURRENT (value)))
-       {
-         tree args;
-
-         for (args = k->args; args; args = TREE_CHAIN (args))
-           if (friend_of_associated_class_p (TREE_VALUE (args),
-                                             OVL_CURRENT (value)))
-             break;
-         if (!args)
-           continue;
-       }
+       continue;
 
       if (add_function (k, OVL_CURRENT (value)))
        return true;
@@ -4408,7 +4761,18 @@ arg_assoc_template_arg (struct arg_lookup *k, tree arg)
        return arg_assoc_namespace (k, ctx);
       /* Otherwise, it must be member template.  */
       else
-       return arg_assoc_class (k, ctx);
+       return arg_assoc_class_only (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.  */
@@ -4419,38 +4783,24 @@ arg_assoc_template_arg (struct arg_lookup *k, tree arg)
     return false;
 }
 
-/* Adds everything associated with class to the lookup structure.
+/* Adds the class and its friends to the lookup structure.
    Returns true on error.  */
 
 static bool
-arg_assoc_class (struct arg_lookup *k, tree type)
+arg_assoc_class_only (struct arg_lookup *k, tree type)
 {
   tree list, friends, context;
-  int i;
 
-  /* Backend build structures, such as __builtin_va_list, aren't
+  /* Backend-built structures, such as __builtin_va_list, aren't
      affected by all this.  */
   if (!CLASS_TYPE_P (type))
     return false;
 
-  if (purpose_member (type, k->classes))
-    return false;
-  k->classes = tree_cons (type, NULL_TREE, k->classes);
-
   context = decl_namespace_context (type);
   if (arg_assoc_namespace (k, context))
     return true;
 
-  if (TYPE_BINFO (type))
-    {
-      /* Process baseclasses.  */
-      tree binfo, base_binfo;
-
-      for (binfo = TYPE_BINFO (type), i = 0;
-          BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
-       if (arg_assoc_class (k, BINFO_TYPE (base_binfo)))
-         return true;
-    }
+  complete_type (type);
 
   /* Process friends.  */
   for (list = DECL_FRIENDLIST (TYPE_MAIN_DECL (type)); list;
@@ -4474,13 +4824,79 @@ arg_assoc_class (struct arg_lookup *k, tree type)
            return true;
        }
 
+  return false;
+}
+
+/* Adds the class and its bases to the lookup structure.
+   Returns true on error.  */
+
+static bool
+arg_assoc_bases (struct arg_lookup *k, tree type)
+{
+  if (arg_assoc_class_only (k, type))
+    return true;
+
+  if (TYPE_BINFO (type))
+    {
+      /* Process baseclasses.  */
+      tree binfo, base_binfo;
+      int i;
+
+      for (binfo = TYPE_BINFO (type), i = 0;
+          BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
+       if (arg_assoc_bases (k, BINFO_TYPE (base_binfo)))
+         return true;
+    }
+
+  return false;
+}
+
+/* Adds everything associated with a class argument type to the lookup
+   structure.  Returns true on error.
+
+   If T is a class type (including unions), its associated classes are: the
+   class itself; the class of which it is a member, if any; and its direct
+   and indirect base classes. Its associated namespaces are the namespaces
+   of which its associated classes are members. Furthermore, if T is a
+   class template specialization, its associated namespaces and classes
+   also include: the namespaces and classes associated with the types of
+   the template arguments provided for template type parameters (excluding
+   template template parameters); the namespaces of which any template
+   template arguments are members; and the classes of which any member
+   templates used as template template arguments are members. [ Note:
+   non-type template arguments do not contribute to the set of associated
+   namespaces.  --end note] */
+
+static bool
+arg_assoc_class (struct arg_lookup *k, tree type)
+{
+  tree list;
+  int i;
+
+  /* Backend build structures, such as __builtin_va_list, aren't
+     affected by all this.  */
+  if (!CLASS_TYPE_P (type))
+    return false;
+
+  if (vec_member (type, k->classes))
+    return false;
+  VEC_safe_push (tree, gc, k->classes, type);
+
+  if (TYPE_CLASS_SCOPE_P (type)
+      && arg_assoc_class_only (k, TYPE_CONTEXT (type)))
+    return true;
+
+  if (arg_assoc_bases (k, type))
+    return true;
+
   /* Process template arguments.  */
   if (CLASSTYPE_TEMPLATE_INFO (type)
       && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
     {
       list = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
       for (i = 0; i < TREE_VEC_LENGTH (list); ++i)
-       arg_assoc_template_arg (k, TREE_VEC_ELT (list, i));
+       if (arg_assoc_template_arg (k, TREE_VEC_ELT (list, i)))
+         return true;
     }
 
   return false;
@@ -4514,17 +4930,23 @@ 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:
+    case NULLPTR_TYPE:
       return false;
     case RECORD_TYPE:
       if (TYPE_PTRMEMFUNC_P (type))
        return arg_assoc_type (k, TYPE_PTRMEMFUNC_FN_TYPE (type));
+    case UNION_TYPE:
       return arg_assoc_class (k, type);
     case POINTER_TYPE:
     case REFERENCE_TYPE:
     case ARRAY_TYPE:
       return arg_assoc_type (k, TREE_TYPE (type));
-    case UNION_TYPE:
     case ENUMERAL_TYPE:
+      if (TYPE_CLASS_SCOPE_P (type)
+         && arg_assoc_class_only (k, TYPE_CONTEXT (type)))
+       return true;
       return arg_assoc_namespace (k, decl_namespace_context (type));
     case METHOD_TYPE:
       /* The basetype is referenced in the first arg type, so just
@@ -4541,19 +4963,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 ();
@@ -4572,6 +4986,21 @@ arg_assoc_args (struct arg_lookup *k, tree args)
   return false;
 }
 
+/* Adds everything associated with an argument vector.  Returns true
+   on error.  */
+
+static bool
+arg_assoc_args_vec (struct arg_lookup *k, VEC(tree,gc) *args)
+{
+  unsigned int ix;
+  tree arg;
+
+  FOR_EACH_VEC_ELT (tree, args, ix, arg)
+    if (arg_assoc (k, arg))
+      return true;
+  return false;
+}
+
 /* Adds everything associated with a given tree_node.  Returns 1 on error.  */
 
 static bool
@@ -4601,34 +5030,17 @@ arg_assoc (struct arg_lookup *k, tree n)
     return arg_assoc_type (k, TREE_TYPE (n));
   if (TREE_CODE (n) == TEMPLATE_ID_EXPR)
     {
-      /* [basic.lookup.koenig]
-
-        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);
+      /* The working paper doesn't currently say how to handle template-id
+        arguments.  The sensible thing would seem to be to handle the list
+        of template candidates like a normal overload set, and handle the
+        template arguments like we do for class template
+        specializations.  */
+      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);
-
-      /* 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);
-
-      ctx = CP_DECL_CONTEXT (template);
-
-      if (TREE_CODE (ctx) == NAMESPACE_DECL)
-       {
-         if (arg_assoc_namespace (k, ctx) == 1)
-           return true;
-       }
-      /* It must be a member template.  */
-      else if (arg_assoc_class (k, ctx) == 1)
+      /* First the templates.  */
+      if (arg_assoc (k, templ))
        return true;
 
       /* Now the arguments.  */
@@ -4651,7 +5063,8 @@ arg_assoc (struct arg_lookup *k, tree n)
    are the functions found in normal lookup.  */
 
 tree
-lookup_arg_dependent (tree name, tree fns, tree args)
+lookup_arg_dependent (tree name, tree fns, VEC(tree,gc) *args,
+                     bool include_std)
 {
   struct arg_lookup k;
 
@@ -4665,16 +5078,18 @@ lookup_arg_dependent (tree name, tree fns, tree args)
   k.name = name;
   k.args = args;
   k.functions = fns;
-  k.classes = NULL_TREE;
+  k.classes = make_tree_vector ();
 
   /* We previously performed an optimization here by setting
      NAMESPACES to the current namespace when it was safe. However, DR
      164 says that namespaces that were already searched in the first
      stage of template processing are searched again (potentially
      picking up later definitions) in the second stage. */
-  k.namespaces = NULL_TREE;
+  k.namespaces = make_tree_vector ();
 
-  arg_assoc_args (&k, args);
+  if (include_std)
+    arg_assoc_namespace (&k, std_node);
+  arg_assoc_args_vec (&k, args);
 
   fns = k.functions;
   
@@ -4686,6 +5101,9 @@ lookup_arg_dependent (tree name, tree fns, tree args)
       error ("  in call to %qD", name);
       fns = error_mark_node;
     }
+
+  release_tree_vector (k.classes);
+  release_tree_vector (k.namespaces);
     
   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, fns);
 }
@@ -4747,7 +5165,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)
        {
@@ -4756,6 +5175,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
@@ -4814,6 +5236,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
@@ -4842,7 +5266,8 @@ pushtag (tree name, tree type, tag_scope scope)
        {
          tree cs = current_scope ();
 
-         if (scope == ts_current)
+         if (scope == ts_current
+             || (cs && TREE_CODE (cs) == FUNCTION_DECL))
            context = cs;
          else if (cs != NULL_TREE && TYPE_P (cs))
            /* When declaring a friend class of a local class, we want
@@ -4879,11 +5304,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
@@ -4900,6 +5325,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
@@ -4928,7 +5356,6 @@ pushtag (tree name, tree type, tag_scope scope)
 
   decl = TYPE_NAME (type);
   gcc_assert (TREE_CODE (decl) == TYPE_DECL);
-  TYPE_STUB_DECL (type) = decl;
 
   /* Set type visibility now if this is a forward declaration.  */
   TREE_PUBLIC (decl) = 1;
@@ -5013,7 +5440,7 @@ push_to_top_level (void)
   bool need_pop;
 
   timevar_push (TV_NAME_LOOKUP);
-  s = GGC_CNEW (struct saved_scope);
+  s = ggc_alloc_cleared_saved_scope ();
 
   b = scope_chain ? current_binding_level : 0;
 
@@ -5021,7 +5448,7 @@ push_to_top_level (void)
   if (cfun)
     {
       need_pop = true;
-      push_function_context_to (NULL_TREE);
+      push_function_context ();
     }
   else
     need_pop = false;
@@ -5054,14 +5481,15 @@ push_to_top_level (void)
        SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t), TREE_VALUE (t));
     }
 
-  for (i = 0; VEC_iterate (cxx_saved_binding, s->old_bindings, i, sb); ++i)
+  FOR_EACH_VEC_ELT (cxx_saved_binding, s->old_bindings, i, sb)
     IDENTIFIER_MARKED (sb->identifier) = 0;
 
   s->prev = scope_chain;
   s->bindings = b;
   s->need_pop_function_context = need_pop;
   s->function_decl = current_function_decl;
-  s->skip_evaluation = skip_evaluation;
+  s->unevaluated_operand = cp_unevaluated_operand;
+  s->inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
 
   scope_chain = s;
   current_function_decl = NULL_TREE;
@@ -5069,7 +5497,8 @@ push_to_top_level (void)
   current_lang_name = lang_name_cplusplus;
   current_namespace = global_namespace;
   push_class_stack ();
-  skip_evaluation = 0;
+  cp_unevaluated_operand = 0;
+  c_inhibit_evaluation_warnings = 0;
   timevar_pop (TV_NAME_LOOKUP);
 }
 
@@ -5089,7 +5518,7 @@ pop_from_top_level (void)
   current_lang_base = 0;
 
   scope_chain = s->prev;
-  for (i = 0; VEC_iterate (cxx_saved_binding, s->old_bindings, i, saved); ++i)
+  FOR_EACH_VEC_ELT (cxx_saved_binding, s->old_bindings, i, saved)
     {
       tree id = saved->identifier;
 
@@ -5100,9 +5529,10 @@ 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;
+  cp_unevaluated_operand = s->unevaluated_operand;
+  c_inhibit_evaluation_warnings = s->inhibit_evaluation_warnings;
   timevar_pop (TV_NAME_LOOKUP);
 }
 
@@ -5134,7 +5564,7 @@ void
 cp_emit_debug_info_for_using (tree t, tree context)
 {
   /* Don't try to emit any debug information if we have errors.  */
-  if (sorrycount || errorcount)
+  if (seen_error ())
     return;
 
   /* Ignore this FUNCTION_DECL if it refers to a builtin declaration
@@ -5155,7 +5585,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 (input_location, USING_STMT, t));
+       else
+         (*debug_hooks->imported_module_or_decl) (t, NULL_TREE, context, false);
+      }
 }
 
 #include "gt-cp-name-lookup.h"