OSDN Git Service

Fix filename typo.
[pf3gnuchains/gcc-fork.git] / gcc / cp / name-lookup.c
index ccef113..3690e87 100644 (file)
@@ -1,5 +1,5 @@
 /* Definitions for C++ name lookup routines.
-   Copyright (C) 2003, 2004 Free Software Foundation, Inc.
+   Copyright (C) 2003, 2004, 2005  Free Software Foundation, Inc.
    Contributed by Gabriel Dos Reis <gdr@integrable-solutions.net>
 
 This file is part of GCC.
@@ -43,7 +43,7 @@ struct scope_binding {
 static cxx_scope *innermost_nonclass_level (void);
 static tree select_decl (const struct scope_binding *, int);
 static cxx_binding *binding_for_name (cxx_scope *, tree);
-static tree lookup_name_current_level (tree);
+static tree lookup_name_innermost_nonclass_level (tree);
 static tree push_overloaded_decl (tree, int);
 static bool lookup_using_namespace (tree, struct scope_binding *, tree,
                                     tree, int);
@@ -95,7 +95,7 @@ binding_entry_make (tree name, tree type)
 }
 
 /* Put ENTRY back on the free list.  */
-
+#if 0
 static inline void
 binding_entry_free (binding_entry entry)
 {
@@ -104,6 +104,7 @@ binding_entry_free (binding_entry entry)
   entry->chain = free_binding_entry;
   free_binding_entry = entry;
 }
+#endif
 
 /* The datatype used to implement the mapping from names to types at
    a given scope.  */
@@ -131,7 +132,7 @@ binding_table_construct (binding_table table, size_t chain_count)
 }
 
 /* Make TABLE's entries ready for reuse.  */
-
+#if 0
 static void
 binding_table_free (binding_table table)
 {
@@ -154,6 +155,7 @@ binding_table_free (binding_table table)
     }
   table->entry_count = 0;
 }
+#endif
 
 /* Allocate a table with CHAIN_COUNT, assumed to be a power of two.  */
 
@@ -225,82 +227,6 @@ binding_table_find (binding_table table, tree name)
   return entry;
 }
 
-/* Return the binding_entry, if any, that maps NAME to an anonymous type.  */
-
-static tree
-binding_table_find_anon_type (binding_table table, tree name)
-{
-  const unsigned int hash = IDENTIFIER_HASH_VALUE (name);
-  binding_entry entry = table->chain[ENTRY_INDEX (hash, table->chain_count)];
-
-  while (entry != NULL && TYPE_IDENTIFIER (entry->type) != name)
-    entry = entry->chain;
-
-  return entry ? entry->type : NULL;
-}
-
-/* Return the binding_entry, if any, that has TYPE as target.  If NAME
-   is non-null, then set the domain and rehash that entry.  */
-
-static binding_entry
-binding_table_reverse_maybe_remap (binding_table table, tree type, tree name)
-{
-  const size_t chain_count = table->chain_count;
-  binding_entry entry = NULL;
-  binding_entry *p = NULL;
-  size_t i;
-
-  for (i = 0; i < chain_count && entry == NULL; ++i)
-    {
-      p = &table->chain[i];
-      while (*p != NULL && entry == NULL)
-        if ((*p)->type == type)
-          entry = *p;
-        else
-          p = &(*p)->chain;
-    }
-
-  if (entry != NULL && name != NULL && entry->name != name)
-    {
-      /* Remove the bucket from the previous chain.  */
-      *p = (*p)->chain;
-
-      /* Remap the name type to type.  */
-      i = ENTRY_INDEX (IDENTIFIER_HASH_VALUE (name), chain_count);
-      entry->chain = table->chain[i];
-      entry->name = name;
-      table->chain[i] = entry;
-    }
-
-  return entry;
-}
-
-/* Remove from TABLE all entries that map to anonymous enums or
-   class-types.  */
-
-void
-binding_table_remove_anonymous_types (binding_table table)
-{
-  const size_t chain_count = table->chain_count;
-  size_t i;
-
-  for (i = 0; i < chain_count; ++i)
-    {
-      binding_entry *p = &table->chain[i];
-
-      while (*p != NULL)
-        if (ANON_AGGRNAME_P ((*p)->name))
-          {
-            binding_entry e = *p;
-            *p = (*p)->chain;
-            --table->entry_count;
-            binding_entry_free (e);
-          }
-        else
-          p = &(*p)->chain;
-    }
-}
-
 /* Apply PROC -- with DATA -- to all entries in TABLE.  */
 
 void
@@ -451,16 +377,17 @@ pop_binding (tree id, tree decl)
   binding = IDENTIFIER_BINDING (id);
 
   /* The name should be bound.  */
-  my_friendly_assert (binding != NULL, 0);
+  gcc_assert (binding != NULL);
 
   /* The DECL will be either the ordinary binding or the type
      binding for this identifier.  Remove that binding.  */
   if (binding->value == decl)
     binding->value = NULL_TREE;
-  else if (binding->type == decl)
-    binding->type = NULL_TREE;
   else
-    abort ();
+    {
+      gcc_assert (binding->type == decl);
+      binding->type = NULL_TREE;
+    }
 
   if (!binding->value && !binding->type)
     {
@@ -530,19 +457,24 @@ supplement_binding (cxx_binding *binding, tree decl)
   else if (TREE_CODE (bval) == TYPE_DECL
           && TREE_CODE (decl) == TYPE_DECL
           && DECL_NAME (decl) == DECL_NAME (bval)
+          && binding->scope->kind != sk_class
           && (same_type_p (TREE_TYPE (decl), TREE_TYPE (bval))
               /* If either type involves template parameters, we must
                  wait until instantiation.  */
               || uses_template_parms (TREE_TYPE (decl))
               || uses_template_parms (TREE_TYPE (bval))))
     /* We have two typedef-names, both naming the same type to have
-       the same name.  This is OK because of:
+       the same name.  In general, this is OK because of:
 
          [dcl.typedef]
 
         In a given scope, a typedef specifier can be used to redefine
         the name of any type declared in that scope to refer to the
-        type to which it already refers.  */
+        type to which it already refers.  
+
+       However, in class scopes, this rule does not apply due to the
+       stricter language in [class.mem] prohibiting redeclarations of
+       members.  */
     ok = false;
   /* There can be two block-scope declarations of the same variable,
      so long as they are `extern' declarations.  However, there cannot
@@ -573,8 +505,8 @@ supplement_binding (cxx_binding *binding, tree decl)
     ok = false;
   else
     {
-      error ("declaration of `%#D'", decl);
-      cp_error_at ("conflicts with previous declaration `%#D'", bval);
+      error ("declaration of %q#D", decl);
+      cp_error_at ("conflicts with previous declaration %q#D", bval);
       ok = false;
     }
 
@@ -677,7 +609,7 @@ pushdecl (tree x)
       if (DECL_NAMESPACE_SCOPE_P (x) && namespace_bindings_p ())
        t = namespace_binding (name, DECL_CONTEXT (x));
       else
-       t = lookup_name_current_level (name);
+       t = lookup_name_innermost_nonclass_level (name);
 
       /* [basic.link] If there is a visible declaration of an entity
         with linkage having the same name and type, ignoring entities
@@ -745,10 +677,7 @@ pushdecl (tree x)
            }
          else if (TREE_CODE (t) == PARM_DECL)
            {
-             if (DECL_CONTEXT (t) == NULL_TREE)
-               /* This is probably caused by too many errors, but calling
-                  abort will say that if errors have occurred.  */
-               abort ();
+             gcc_assert (DECL_CONTEXT (t));
 
              /* Check for duplicate params.  */
              if (duplicate_decls (x, t))
@@ -761,8 +690,8 @@ pushdecl (tree x)
          else if (t == wchar_decl_node)
            {
              if (pedantic && ! DECL_IN_SYSTEM_HEADER (x))
-               pedwarn ("redeclaration of `wchar_t' as `%T'",
-                           TREE_TYPE (x));
+               pedwarn ("redeclaration of %<wchar_t%> as %qT",
+                         TREE_TYPE (x));
 
              /* Throw away the redeclaration.  */
              POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
@@ -793,8 +722,8 @@ pushdecl (tree x)
                     [basic.start.main]
                     
                     This function shall not be overloaded.  */
-                 cp_error_at ("invalid redeclaration of `%D'", t);
-                 error ("as `%D'", x);
+                 cp_error_at ("invalid redeclaration of %qD", t);
+                 error ("as %qD", x);
                  /* We don't try to push this declaration since that
                     causes a crash.  */
                  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
@@ -850,7 +779,7 @@ pushdecl (tree x)
                       || TYPE_NAME (type) != DECL_ABSTRACT_ORIGIN (x)))
             {
              DECL_ORIGINAL_TYPE (x) = type;
-              type = build_type_copy (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;
@@ -882,8 +811,8 @@ pushdecl (tree x)
              && TREE_CODE (decl) == TREE_CODE (x)
              && !same_type_p (TREE_TYPE (x), TREE_TYPE (decl)))
            {
-             pedwarn ("type mismatch with previous external decl of `%#D'", x);
-             cp_pedwarn_at ("previous external decl of `%#D'", decl);
+             pedwarn ("type mismatch with previous external decl of %q#D", x);
+             cp_pedwarn_at ("previous external decl of %q#D", decl);
            }
        }
 
@@ -965,8 +894,8 @@ pushdecl (tree x)
                /* OK */;
              else
                {
-                 warning ("extern declaration of `%#D' doesn't match", x);
-                 cp_warning_at ("global declaration `%#D'", oldglobal);
+                 warning ("extern declaration of %q#D doesn't match", x);
+                 cp_warning_at ("global declaration %q#D", oldglobal);
                }
            }
          /* If we have a local external declaration,
@@ -1002,14 +931,14 @@ pushdecl (tree x)
                  /* ARM $8.3 */
                  if (b->kind == sk_function_parms)
                    {
-                     error ("declaration of '%#D' shadows a parameter", x);
+                     error ("declaration of %q#D shadows a parameter", x);
                      err = true;
                    }
                }
 
              if (warn_shadow && !err)
                {
-                 warning ("declaration of '%#D' shadows a parameter", x);
+                 warning ("declaration of %q#D shadows a parameter", x);
                  warning ("%Jshadowed declaration is here", oldlocal);
                }
            }
@@ -1034,20 +963,20 @@ pushdecl (tree x)
              if (member && !TREE_STATIC (member))
                {
                  /* Location of previous decl is not useful in this case.  */
-                 warning ("declaration of '%D' shadows a member of 'this'",
+                 warning ("declaration of %qD shadows a member of 'this'",
                           x);
                }
              else if (oldlocal != NULL_TREE
                       && TREE_CODE (oldlocal) == VAR_DECL)
                {
-                 warning ("declaration of '%D' shadows a previous local", x);
+                 warning ("declaration of %qD shadows a previous local", x);
                  warning ("%Jshadowed declaration is here", oldlocal);
                }
              else if (oldglobal != NULL_TREE
                       && TREE_CODE (oldglobal) == VAR_DECL)
                /* XXX shadow warnings in outer-more namespaces */
                {
-                 warning ("declaration of '%D' shadows a global declaration",
+                 warning ("declaration of %qD shadows a global declaration",
                           x);
                  warning ("%Jshadowed declaration is here", oldglobal);
                }
@@ -1113,7 +1042,7 @@ push_local_binding (tree id, tree decl, int flags)
      push_local_binding with a friend decl of a local class.  */
   b = innermost_nonclass_level ();
 
-  if (lookup_name_current_level (id))
+  if (lookup_name_innermost_nonclass_level (id))
     {
       /* Supplement the existing binding.  */
       if (!supplement_binding (IDENTIFIER_BINDING (id), decl))
@@ -1163,11 +1092,10 @@ check_for_out_of_scope_variable (tree decl)
     {
       if (!DECL_ERROR_REPORTED (decl))
        {
-         warning ("name lookup of `%D' changed",
-                     DECL_NAME (decl));
-         cp_warning_at ("  matches this `%D' under ISO standard rules",
+         warning ("name lookup of %qD changed", DECL_NAME (decl));
+         cp_warning_at ("  matches this %qD under ISO standard rules",
                         shadowed);
-         cp_warning_at ("  matches this `%D' under old rules", decl);
+         cp_warning_at ("  matches this %qD under old rules", decl);
          DECL_ERROR_REPORTED (decl) = 1;
        }
       return shadowed;
@@ -1185,16 +1113,17 @@ check_for_out_of_scope_variable (tree decl)
 
   if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
     {
-      error ("name lookup of `%D' changed for new ISO `for' scoping",
+      error ("name lookup of %qD changed for new ISO %<for%> scoping",
             DECL_NAME (decl));
-      cp_error_at ("  cannot use obsolete binding at `%D' because it has a destructor", decl);
+      cp_error_at ("  cannot use obsolete binding at %qD because "
+                   "it has a destructor", decl);
       return error_mark_node;
     }
   else
     {
-      pedwarn ("name lookup of `%D' changed for new ISO `for' scoping",
+      pedwarn ("name lookup of %qD changed for new ISO %<for%> scoping",
               DECL_NAME (decl));
-      cp_pedwarn_at ("  using obsolete binding at `%D'", decl);
+      cp_pedwarn_at ("  using obsolete binding at %qD", decl);
     }
 
   return decl;
@@ -1334,7 +1263,6 @@ begin_scope (scope_kind kind, tree entity)
       break;
 
     case sk_namespace:
-      scope->type_decls = binding_table_new (namespace_scope_ht_size (entity));
       NAMESPACE_LEVEL (entity) = scope;
       VARRAY_TREE_INIT (scope->static_decls,
                         DECL_NAME (entity) == std_identifier
@@ -1345,7 +1273,7 @@ begin_scope (scope_kind kind, tree entity)
 
     default:
       /* Should not happen.  */
-      my_friendly_assert (false, 20030922);
+      gcc_unreachable ();
       break;
     }
   scope->kind = kind;
@@ -1368,7 +1296,7 @@ leave_scope (void)
 
   /* We cannot leave a scope, if there are none left.  */
   if (NAMESPACE_LEVEL (global_namespace))
-    my_friendly_assert (!global_scope_p (scope), 20030527);
+    gcc_assert (!global_scope_p (scope));
   
   if (ENABLE_SCOPE_CHECKING)
     {
@@ -1394,23 +1322,23 @@ leave_scope (void)
       && scope->kind != sk_class)
     {
       scope->level_chain = free_binding_level;
-      if (scope->kind == sk_class)
-        scope->type_decls = NULL;
-      else
-        binding_table_free (scope->type_decls);
-      my_friendly_assert (!ENABLE_SCOPE_CHECKING
-                          || scope->binding_depth == binding_depth,
-                          20030529);
+      gcc_assert (!ENABLE_SCOPE_CHECKING
+                 || scope->binding_depth == binding_depth);
       free_binding_level = scope;
     }
 
   /* Find the innermost enclosing class scope, and reset
      CLASS_BINDING_LEVEL appropriately.  */
-  for (scope = current_binding_level;
-       scope && scope->kind != sk_class;
-       scope = scope->level_chain)
-    ;
-  class_binding_level = scope && scope->kind == sk_class ? scope : NULL;
+  if (scope->kind == sk_class)
+    {
+      class_binding_level = NULL;
+      for (scope = current_binding_level; scope; scope = scope->level_chain)
+       if (scope->kind == sk_class)
+         {
+           class_binding_level = scope;
+           break;
+         }
+    }
 
   return current_binding_level;
 }
@@ -1420,9 +1348,9 @@ resume_scope (struct cp_binding_level* b)
 {
   /* Resuming binding levels is meant only for namespaces,
      and those cannot nest into classes.  */
-  my_friendly_assert(!class_binding_level, 386);
+  gcc_assert (!class_binding_level);
   /* Also, resuming a non-directly nested namespace is a no-no.  */
-  my_friendly_assert(b->level_chain == current_binding_level, 386);
+  gcc_assert (b->level_chain == current_binding_level);
   current_binding_level = b;
   if (ENABLE_SCOPE_CHECKING)
     {
@@ -1506,8 +1434,7 @@ 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->type_decls != NULL);
+         || current_binding_level->names != NULL_TREE);
 }
 
 /* Returns the kind of the innermost scope.  */
@@ -1547,59 +1474,11 @@ getdecls (void)
   return current_binding_level->names;
 }
 
-/* Set the current binding TABLE for type declarations..  This is a
-   temporary workaround of the fact that the data structure classtypes
-   does not currently carry its allocated cxx_scope structure.  */
-void
-cxx_remember_type_decls (binding_table table)
-{
-  current_binding_level->type_decls = table;
-}
-
 /* For debugging.  */
 static int no_print_functions = 0;
 static int no_print_builtins = 0;
 
-/* Called from print_binding_level through binding_table_foreach to
-   print the content of binding ENTRY.  DATA is a pointer to line offset
-   marker.  */
 static void
-bt_print_entry (binding_entry entry, void *data)
-{
-  int *p = (int *) data;
-  int len;
-
-  if (entry->name == NULL)
-    len = 3;
-  else if (entry->name == TYPE_IDENTIFIER (entry->type))
-    len = 2;
-  else
-    len = 4;
-    len = 4;
-
-  *p += len;
-
-  if (*p > 5)
-    {
-      fprintf (stderr, "\n\t");
-      *p = len;
-    }
-  if (entry->name == NULL)
-    {
-      print_node_brief (stderr, "<unnamed-typedef", entry->type, 0);
-      fprintf (stderr, ">");
-    }
-  else if (entry->name == TYPE_IDENTIFIER (entry->type))
-    print_node_brief (stderr, "", entry->type, 0);
-  else
-    {
-      print_node_brief (stderr, "<typedef", entry->name, 0);
-      print_node_brief (stderr, "", entry->type, 0);
-      fprintf (stderr, ">");
-    }
-}
-
-void
 print_binding_level (struct cp_binding_level* lvl)
 {
   tree t;
@@ -1641,14 +1520,6 @@ print_binding_level (struct cp_binding_level* lvl)
       if (i)
         fprintf (stderr, "\n");
     }
-  if (lvl->type_decls)
-    {
-      fprintf (stderr, " tags:\t");
-      i = 0;
-      binding_table_foreach (lvl->type_decls, bt_print_entry, &i);
-      if (i)
-       fprintf (stderr, "\n");
-    }
   if (VEC_length (cp_class_binding, lvl->class_shadowed))
     {
       size_t i;
@@ -1760,15 +1631,12 @@ set_identifier_type_value_with_scope (tree id, tree decl, cxx_scope *b)
     {
       cxx_binding *binding =
        binding_for_name (NAMESPACE_LEVEL (current_namespace), id);
-      if (decl)
-       {
-         if (binding->value)
-           supplement_binding (binding, decl);
-         else
-           binding->value = decl;
-       }
+      gcc_assert (decl);
+      if (binding->value)
+       supplement_binding (binding, decl);
       else
-       abort ();
+       binding->value = decl;
+      
       /* Store marker instead of real type.  */
       type = global_type_node;
     }
@@ -1788,7 +1656,7 @@ set_identifier_type_value (tree id, tree decl)
    specified class TYPE.  When given a template, this routine doesn't
    lose the specialization.  */
 
-tree
+static inline tree
 constructor_name_full (tree type)
 {
   return TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
@@ -1846,27 +1714,6 @@ make_anon_name (void)
   return get_identifier (buf);
 }
 
-/* Clear the TREE_PURPOSE slot of UTDs which have anonymous typenames.
-   This keeps dbxout from getting confused.  */
-
-void
-clear_anon_tags (void)
-{
-  struct cp_binding_level *b;
-  static int last_cnt = 0;
-
-  /* Fast out if no new anon names were declared.  */
-  if (last_cnt == anon_cnt)
-    return;
-
-  b = current_binding_level;
-  while (b->kind == sk_cleanup)
-    b = b->level_chain;
-  if (b->type_decls != NULL)
-    binding_table_remove_anonymous_types (b->type_decls);
-  last_cnt = anon_cnt;
-}
-\f
 /* Return (from the stack of) the BINDING, if any, established at SCOPE.  */ 
 
 static inline cxx_binding *
@@ -1929,8 +1776,8 @@ push_using_decl (tree scope, tree name)
   tree decl;
 
   timevar_push (TV_NAME_LOOKUP);
-  my_friendly_assert (TREE_CODE (scope) == NAMESPACE_DECL, 383);
-  my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 384);
+  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))
     if (DECL_INITIAL (decl) == scope && DECL_NAME (decl) == name)
       break;
@@ -2004,7 +1851,7 @@ push_overloaded_decl (tree decl, int flags)
   if (doing_global)
     old = namespace_binding (name, DECL_CONTEXT (decl));
   else
-    old = lookup_name_current_level (name);
+    old = lookup_name_innermost_nonclass_level (name);
 
   if (old)
     {
@@ -2014,7 +1861,7 @@ push_overloaded_decl (tree decl, int flags)
          if (IS_AGGR_TYPE (t) && warn_shadow
              && (! DECL_IN_SYSTEM_HEADER (decl)
                  || ! DECL_IN_SYSTEM_HEADER (old)))
-           warning ("`%#D' hides constructor for `%#T'", decl, t);
+           warning ("%q#D hides constructor for %q#T", decl, t);
          old = NULL_TREE;
        }
       else if (is_overloaded_fn (old))
@@ -2029,8 +1876,8 @@ push_overloaded_decl (tree decl, int flags)
                  && !(flags & PUSH_USING)
                  && compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
                                TYPE_ARG_TYPES (TREE_TYPE (decl))))
-               error ("`%#D' conflicts with previous using declaration `%#D'",
-                         decl, fn);
+               error ("%q#D conflicts with previous using declaration %q#D",
+                       decl, fn);
 
              if (duplicate_decls (decl, fn) == fn)
                POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, fn);
@@ -2041,8 +1888,8 @@ push_overloaded_decl (tree decl, int flags)
        old = NULL_TREE;
       else
        {
-         cp_error_at ("previous non-function declaration `%#D'", old);
-         error ("conflicts with function declaration `%#D'", decl);
+         cp_error_at ("previous non-function declaration %q#D", old);
+         error ("conflicts with function declaration %q#D", decl);
          POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
        }
     }
@@ -2099,7 +1946,7 @@ push_overloaded_decl (tree decl, int flags)
              }
 
          /* We should always find a previous binding in this case.  */
-         abort ();
+         gcc_unreachable ();
        }
 
       /* Install the new binding.  */
@@ -2120,7 +1967,7 @@ validate_nonmember_using_decl (tree decl, tree scope, tree name)
        member-declaration.  */
   if (TYPE_P (scope))
     {
-      error ("`%T' is not a namespace", scope);
+      error ("%qT is not a namespace", scope);
       return NULL_TREE;
     }
   else if (scope == error_mark_node)
@@ -2130,13 +1977,14 @@ validate_nonmember_using_decl (tree decl, tree scope, tree name)
     {
       /* 7.3.3/5
           A using-declaration shall not name a template-id.  */
-      error ("a using-declaration cannot specify a template-id.  Try `using %D'", name);
+      error ("a using-declaration cannot specify a template-id.  "
+             "Try %<using %D%>", name);
       return NULL_TREE;
     }
 
   if (TREE_CODE (decl) == NAMESPACE_DECL)
     {
-      error ("namespace `%D' not allowed in using-declaration", decl);
+      error ("namespace %qD not allowed in using-declaration", decl);
       return NULL_TREE;
     }
 
@@ -2144,14 +1992,14 @@ validate_nonmember_using_decl (tree decl, tree scope, tree name)
     {
       /* It's a nested name with template parameter dependent scope.
         This can only be using-declaration for class member.  */
-      error ("`%T' is not a namespace", TREE_OPERAND (decl, 0));
+      error ("%qT is not a namespace", TREE_OPERAND (decl, 0));
       return NULL_TREE;
     }
 
   if (is_overloaded_fn (decl))
     decl = get_first_fn (decl);
 
-  my_friendly_assert (DECL_P (decl), 20020908);
+  gcc_assert (DECL_P (decl));
 
   /* Make a USING_DECL.  */
   return push_using_decl (scope, name);
@@ -2172,7 +2020,7 @@ do_nonmember_using_decl (tree scope, tree name, tree oldval, tree oldtype,
 
   if (!decls.value && !decls.type)
     {
-      error ("`%D' not declared", name);
+      error ("%qD not declared", name);
       return;
     }
 
@@ -2184,7 +2032,7 @@ do_nonmember_using_decl (tree scope, tree name, tree oldval, tree oldtype,
       if (oldval && !is_overloaded_fn (oldval))
        {
          if (!DECL_IMPLICIT_TYPEDEF_P (oldval))
-           error ("`%D' is already declared in this scope", name);
+           error ("%qD is already declared in this scope", name);
          oldval = NULL_TREE;
        }
 
@@ -2216,17 +2064,23 @@ do_nonmember_using_decl (tree scope, tree name, tree oldval, tree oldtype,
                     are the same extern "C" functions, that's ok.  */
                   if (decls_match (new_fn, old_fn))
                    {
-                     /* If the OLD_FN was a builtin, there is now a
-                        real declaration.  */
+                     /* If the OLD_FN was a builtin, we've seen a real 
+                        declaration in another namespace.  Use it instead.
+                        Set tmp1 to NULL so we can use the existing
+                        OVERLOAD logic at the end of this inner loop.
+                     */
                      if (DECL_ANTICIPATED (old_fn))
-                       DECL_ANTICIPATED (old_fn) = 0;
+                       {
+                         gcc_assert (! DECL_ANTICIPATED (new_fn));
+                         tmp1 = NULL;
+                       }
                      break;
                    }
                  else if (!DECL_ANTICIPATED (old_fn))
                    {
                      /* If the OLD_FN was really declared, the
                         declarations don't match.  */
-                     error ("`%D' is already declared in this scope", name);
+                     error ("%qD is already declared in this scope", name);
                      break;
                    }
 
@@ -2263,14 +2117,14 @@ do_nonmember_using_decl (tree scope, tree name, tree oldval, tree oldtype,
     {
       *newval = decls.value;
       if (oldval && !decls_match (*newval, oldval))
-       error ("`%D' is already declared in this scope", name);
+       error ("%qD is already declared in this scope", name);
     }
 
   *newtype = decls.type;
   if (oldtype && *newtype && !same_type_p (oldtype, *newtype))
     {
-      error ("using declaration `%D' introduced ambiguous type `%T'",
-               name, oldtype);
+      error ("using declaration %qD introduced ambiguous type %qT",
+             name, oldtype);
       return;
     }
 }
@@ -2291,7 +2145,7 @@ do_local_using_decl (tree decl, tree scope, tree name)
       && at_function_scope_p ())
     add_decl_expr (decl);
 
-  oldval = lookup_name_current_level (name);
+  oldval = lookup_name_innermost_nonclass_level (name);
   oldtype = lookup_type_current_level (name);
 
   do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype);
@@ -2329,200 +2183,17 @@ do_local_using_decl (tree decl, tree scope, tree name)
     cp_emit_debug_info_for_using (orig_decl, current_scope());
 }
 
-/* Return the type that should be used when TYPE's name is preceded
-   by a tag such as 'struct' or 'union', or null if the name cannot
-   be used in this way.
-
-   For example, when processing the third line of:
-
-       struct A;
-       typedef struct A A;
-       struct A;
-
-   lookup of A will find the typedef.  Given A's typedef, this function
-   will return the type associated with "struct A".  For the tag to be
-   anything other than TYPE, TYPE must be a typedef whose original type
-   has the same name and context as TYPE itself.
-
-   It is not valid for a typedef of an anonymous type to be used with
-   an explicit tag:
-
-       typedef struct { ... } B;
-       struct B;
-
-   Return null for this case.  */
-
-static tree
-follow_tag_typedef (tree type)
-{
-  tree original;
-
-  original = original_type (type);
-  if (! TYPE_NAME (original))
-    return NULL_TREE;
-  if (TYPE_IDENTIFIER (original) == TYPE_IDENTIFIER (type)
-      && (CP_DECL_CONTEXT (TYPE_NAME (original))
-         == CP_DECL_CONTEXT (TYPE_NAME (type)))
-      && !(CLASS_TYPE_P (original) && TYPE_WAS_ANONYMOUS (original)))
-    return original;
-  else
-    return NULL_TREE;
-}
-
-/* Given NAME, an IDENTIFIER_NODE,
-   return the structure (or union or enum) definition for that name.
-   Searches binding levels from its SCOPE up to the global level.
-   If THISLEVEL_ONLY is nonzero, searches only the specified context
-   (but skips any sk_cleanup contexts to find one that is
-   meaningful for tags).
-   FORM says which kind of type the caller wants;
-   it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
-   If the wrong kind of type is found, and it's not a template, an error is
-   reported.  */
-
-tree
-lookup_tag (enum tree_code form, tree name,
-            cxx_scope *binding_level, int thislevel_only)
-{
-  struct cp_binding_level *level;
-  /* Nonzero if, we should look past a template parameter level, even
-     if THISLEVEL_ONLY.  */
-  int allow_template_parms_p = 1;
-  bool type_is_anonymous = ANON_AGGRNAME_P (name);
-
-  timevar_push (TV_NAME_LOOKUP);
-  for (level = binding_level; level; level = level->level_chain)
-    {
-      tree tail;
-      if (type_is_anonymous && level->type_decls != NULL)
-        {
-          tree type = binding_table_find_anon_type (level->type_decls, name);
-          /* There is no need for error checking here, because
-           anon names are unique throughout the compilation.  */
-          if (type != NULL)
-            POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, type);
-        }
-      else if (level->kind == sk_namespace)
-       /* Do namespace lookup.  */
-       for (tail = current_namespace; 1; tail = CP_DECL_CONTEXT (tail))
-         {
-            cxx_binding *binding =
-              cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (tail), name);
-
-           if (binding && (binding->type
-                           || (binding->value 
-                               && DECL_DECLARES_TYPE_P (binding->value))))
-             {
-               tree old;
-               
-               /* If we just skipped past a template parameter level,
-                  even though THISLEVEL_ONLY, and we find a template
-                  class declaration, then we use the _TYPE node for the
-                  template.  See the example below.  */
-               if (thislevel_only && !allow_template_parms_p
-                   && binding->value
-                   && DECL_CLASS_TEMPLATE_P (binding->value))
-                 old = binding->value;
-               else
-                 old = binding->type ? binding->type : binding->value;
-
-               /* We've found something at this binding level.  If it is
-                  a typedef, extract the tag it refers to.  Lookup fails
-                  if the typedef doesn't refer to a taggable type.  */
-               old = TREE_TYPE (old);
-               old = follow_tag_typedef (old);
-               if (!old)
-                 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
-               if (TREE_CODE (old) != form
-                   && (form == ENUMERAL_TYPE
-                       || TREE_CODE (old) == ENUMERAL_TYPE))
-                 {
-                   error ("`%#D' redeclared as %C", old, form);
-                   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
-                 }
-               POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, old);
-             }
-           if (thislevel_only || tail == global_namespace)
-             POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
-         }
-      else if (level->type_decls != NULL)
-        {
-          binding_entry entry = binding_table_find (level->type_decls, name);
-          if (entry != NULL)
-            {
-              enum tree_code code = TREE_CODE (entry->type);
-               
-              if (code != form
-                  && (form == ENUMERAL_TYPE || code == ENUMERAL_TYPE))
-                {
-                  /* Definition isn't the kind we were looking for.  */
-                  error ("`%#D' redeclared as %C", entry->type, form);
-                  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
-                }
-              POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, entry->type);
-            }
-         }
-      if (thislevel_only && level->kind != sk_cleanup)
-       {
-         if (level->kind == sk_template_parms && allow_template_parms_p)
-           {
-             /* We must deal with cases like this:
-
-                  template <class T> struct S;
-                  template <class T> struct S {};
-
-                When looking up `S', for the second declaration, we
-                would like to find the first declaration.  But, we
-                are in the pseudo-global level created for the
-                template parameters, rather than the (surrounding)
-                namespace level.  Thus, we keep going one more level,
-                even though THISLEVEL_ONLY is nonzero.  */
-             allow_template_parms_p = 0;
-             continue;
-           }
-         else
-           POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
-       }
-    }
-  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
-}
-
-/* Given a type, find the tag that was defined for it and return the tag name.
-   Otherwise return 0.  However, the value can never be 0
-   in the cases in which this is used.
-
-   C++: If NAME is nonzero, this is the new name to install.  This is
-   done when replacing anonymous tags with real tag names.  */
-
-tree
-lookup_tag_reverse (tree type, tree name)
-{
-  struct cp_binding_level *level;
-
-  timevar_push (TV_NAME_LOOKUP);
-  for (level = current_binding_level; level; level = level->level_chain)
-    {
-      binding_entry entry = level->type_decls == NULL
-        ? NULL
-        : binding_table_reverse_maybe_remap (level->type_decls, type, name);
-      if (entry)
-        POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, entry->name);
-    }
-  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
-}
-
 /* Returns true if ROOT (a namespace, class, or function) encloses
    CHILD.  CHILD may be either a class type or a namespace.  */
 
 bool
 is_ancestor (tree root, tree child)
 {
-  my_friendly_assert ((TREE_CODE (root) == NAMESPACE_DECL
-                      || TREE_CODE (root) == FUNCTION_DECL
-                      || CLASS_TYPE_P (root)), 20030307);
-  my_friendly_assert ((TREE_CODE (child) == NAMESPACE_DECL
-                      || CLASS_TYPE_P (child)),
-                     20030307);
+  gcc_assert ((TREE_CODE (root) == NAMESPACE_DECL
+              || TREE_CODE (root) == FUNCTION_DECL
+              || CLASS_TYPE_P (root)));
+  gcc_assert ((TREE_CODE (child) == NAMESPACE_DECL
+              || CLASS_TYPE_P (child)));
   
   /* The global namespace encloses everything.  */
   if (root == global_namespace)
@@ -2543,14 +2214,14 @@ is_ancestor (tree root, tree child)
     }
 }
 
-/* Enter the class or namespace scope indicated by T.  Returns TRUE iff
-   pop_scope should be called later to exit this scope.  */
+/* Enter the class or namespace scope indicated by T suitable for name
+   lookup.  T can be arbitrary scope, not necessary nested inside the
+   current scope.  Returns a non-null scope to pop iff pop_scope
+   should be called later to exit this scope.  */
 
-bool
+tree
 push_scope (tree t)
 {
-  bool pop = true;
-
   if (TREE_CODE (t) == NAMESPACE_DECL)
     push_decl_namespace (t);
   else if (CLASS_TYPE_P (t))
@@ -2563,10 +2234,10 @@ push_scope (tree t)
           need to re-enter the scope.  Since we are not actually
           pushing a new scope, our caller should not call
           pop_scope.  */
-       pop = false;
+       t = NULL_TREE;
     }
 
-  return pop;
+  return t;
 }
 
 /* Leave scope pushed by push_scope.  */
@@ -2579,6 +2250,110 @@ pop_scope (tree t)
   else if CLASS_TYPE_P (t)
     pop_nested_class ();
 }
+
+/* Subroutine of push_inner_scope.  */
+
+static void
+push_inner_scope_r (tree outer, tree inner)
+{
+  tree prev;
+
+  if (outer == inner
+      || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner)))
+    return;
+
+  prev = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner));
+  if (outer != prev)
+    push_inner_scope_r (outer, prev);
+  if (TREE_CODE (inner) == NAMESPACE_DECL)
+    {
+      struct cp_binding_level *save_template_parm = 0;
+      /* Temporary take out template parameter scopes.  They are saved
+        in reversed order in save_template_parm.  */
+      while (current_binding_level->kind == sk_template_parms)
+       {
+         struct cp_binding_level *b = current_binding_level;
+         current_binding_level = b->level_chain;
+         b->level_chain = save_template_parm;
+         save_template_parm = b;
+       }
+
+      resume_scope (NAMESPACE_LEVEL (inner));
+      current_namespace = inner;
+
+      /* Restore template parameter scopes.  */
+      while (save_template_parm)
+       {
+         struct cp_binding_level *b = save_template_parm;
+         save_template_parm = b->level_chain;
+         b->level_chain = current_binding_level;
+         current_binding_level = b;
+       }
+    }
+  else
+    pushclass (inner);
+}
+
+/* Enter the scope INNER from current scope.  INNER must be a scope
+   nested inside current scope.  This works with both name lookup and
+   pushing name into scope.  In case a template parameter scope is present,
+   namespace is pushed under the template parameter scope according to
+   name lookup rule in 14.6.1/6.
+   
+   Return the former current scope suitable for pop_inner_scope.  */
+
+tree
+push_inner_scope (tree inner)
+{
+  tree outer = current_scope ();
+  if (!outer)
+    outer = current_namespace;
+
+  push_inner_scope_r (outer, inner);
+  return outer;
+}
+
+/* Exit the current scope INNER back to scope OUTER.  */
+
+void
+pop_inner_scope (tree outer, tree inner)
+{
+  if (outer == inner
+      || (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner)))
+    return;
+
+  while (outer != inner)
+    {
+      if (TREE_CODE (inner) == NAMESPACE_DECL)
+       {
+         struct cp_binding_level *save_template_parm = 0;
+         /* Temporary take out template parameter scopes.  They are saved
+            in reversed order in save_template_parm.  */
+         while (current_binding_level->kind == sk_template_parms)
+           {
+             struct cp_binding_level *b = current_binding_level;
+             current_binding_level = b->level_chain;
+             b->level_chain = save_template_parm;
+             save_template_parm = b;
+           }
+
+         pop_namespace ();
+
+         /* Restore template parameter scopes.  */
+         while (save_template_parm)
+           {
+             struct cp_binding_level *b = save_template_parm;
+             save_template_parm = b->level_chain;
+             b->level_chain = current_binding_level;
+             current_binding_level = b;
+           }
+       }
+      else
+       popclass ();
+
+      inner = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner));
+    }
+}
 \f
 /* Do a pushlevel for class declarations.  */
 
@@ -2602,7 +2377,7 @@ poplevel_class (void)
   tree shadowed;
 
   timevar_push (TV_NAME_LOOKUP);
-  my_friendly_assert (level != 0, 354);
+  gcc_assert (level != 0);
 
   /* If we're leaving a toplevel class, cache its binding level.  */
   if (current_class_depth == 1)
@@ -2648,7 +2423,7 @@ set_inherited_value_binding_p (cxx_binding *binding, tree decl,
        context = CP_DECL_CONTEXT (OVL_CURRENT (decl));
       else
        {
-         my_friendly_assert (DECL_P (decl), 0);
+         gcc_assert (DECL_P (decl));
          context = context_for_name_lookup (decl);
        }
 
@@ -2703,9 +2478,7 @@ pushdecl_class_level (tree x)
          input_location = save_location;
        }
     }
-  timevar_pop (TV_NAME_LOOKUP);
-
-  return is_valid;
+  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, is_valid);
 }
 
 /* Return the BINDING (if any) for NAME in SCOPE, which is a class
@@ -2786,10 +2559,10 @@ push_class_level_binding (tree name, tree x)
     POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true);
 
   /* Check for invalid member names.  */
-  my_friendly_assert (TYPE_BEING_DEFINED (current_class_type), 20040713);
+  gcc_assert (TYPE_BEING_DEFINED (current_class_type));
   /* 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. */
+     check_template_shadow will not handle a TREE_LIST.  */
   if (TREE_CODE (decl) == TREE_LIST 
       && TREE_TYPE (decl) == error_mark_node)
     decl = TREE_VALUE (decl);
@@ -2825,7 +2598,7 @@ push_class_level_binding (tree name, tree x)
       tree scope = context_for_name_lookup (x);
       if (TYPE_P (scope) && same_type_p (scope, current_class_type))
        {
-         error ("`%D' has the same name as the class in which it is "
+         error ("%qD has the same name as the class in which it is "
                 "declared",
                 x);
          POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, false);
@@ -2911,36 +2684,26 @@ push_class_level_binding (tree name, tree x)
   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ok);
 }
 
+/* Process "using SCOPE::NAME" in a class scope.  Return the
+   USING_DECL created.  */
+
 tree
-do_class_using_decl (tree decl)
+do_class_using_decl (tree scope, tree name)
 {
-  tree name, value, scope, type;
+  tree value, type;
   
-  if (TREE_CODE (decl) != SCOPE_REF
-      || !TREE_OPERAND (decl, 0)
-      || !TYPE_P (TREE_OPERAND (decl, 0)))
+  if (!scope || !TYPE_P (scope))
     {
       error ("using-declaration for non-member at class scope");
       return NULL_TREE;
     }
-  scope = TREE_OPERAND (decl, 0);
-  name = TREE_OPERAND (decl, 1);
   if (TREE_CODE (name) == BIT_NOT_EXPR)
     {
       error ("using-declaration cannot name destructor");
       return NULL_TREE;
     }
-  if (TREE_CODE (name) == TYPE_DECL)
-    name = DECL_NAME (name);
-  else if (TREE_CODE (name) == TEMPLATE_DECL)
-     name = DECL_NAME (name);
-  else if (BASELINK_P (name))
-    {
-      tree fns = BASELINK_FUNCTIONS (name);
-      name = DECL_NAME (get_first_fn (fns));
-    }
 
-  my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 980716);
+  gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
 
   /* Dependent using decls have a NULL type, non-dependent ones have a
      void type.  */
@@ -2969,7 +2732,10 @@ namespace_binding (tree name, tree scope)
 
   if (scope == NULL)
     scope = global_namespace;
-  scope = ORIGINAL_NAMESPACE (scope);
+  else
+    /* Unnecessary for the global namespace because it can't be an alias. */
+    scope = ORIGINAL_NAMESPACE (scope);
+
   binding = cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
 
   return binding ? binding->value : NULL_TREE;
@@ -3006,53 +2772,56 @@ set_decl_namespace (tree decl, tree scope, bool friendp)
   
   /* It is ok for friends to be qualified in parallel space.  */
   if (!friendp && !is_ancestor (current_namespace, scope))
-    error ("declaration of `%D' not in a namespace surrounding `%D'",
-             decl, scope);
+    error ("declaration of %qD not in a namespace surrounding %qD",
+           decl, scope);
   DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
-  if (scope != current_namespace)
+
+  /* Writing "int N::i" to declare a variable within "N" is invalid.  */ 
+  if (scope == current_namespace) 
     {
-      /* See whether this has been declared in the namespace.  */
-      old = namespace_binding (DECL_NAME (decl), scope);
-      if (!old)
-       /* No old declaration at all.  */
-       goto complain;
-      /* A template can be explicitly specialized in any namespace.  */
-      if (processing_explicit_instantiation)
-       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;
-      /* Since decl is a function, old should contain a function decl.  */
-      if (!is_overloaded_fn (old))
-       goto complain;
-      if (processing_template_decl || processing_specialization)
-       /* We have not yet called push_template_decl to turn a
-          FUNCTION_DECL into a TEMPLATE_DECL, so the declarations
-          won't match.  But, we'll check later, when we construct the
-          template.  */
-       return;
-      if (is_overloaded_fn (old))
-       {
-         for (; old; old = OVL_NEXT (old))
-           if (decls_match (decl, OVL_CURRENT (old)))
-             return;
-       }
-      else
-       if (decls_match (decl, old))
-         return;
+      if (at_namespace_scope_p ())
+       error ("explicit qualification in declaration of `%D'",
+              decl);
+      return;
     }
-  else
+
+  /* See whether this has been declared in the namespace.  */
+  old = namespace_binding (DECL_NAME (decl), scope);
+  if (!old)
+    /* No old declaration at all.  */
+    goto complain;
+  /* A template can be explicitly specialized in any namespace.  */
+  if (processing_explicit_instantiation)
+    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;
+  /* Since decl is a function, old should contain a function decl.  */
+  if (!is_overloaded_fn (old))
+    goto complain;
+  if (processing_template_decl || processing_specialization)
+    /* We have not yet called push_template_decl to turn a
+       FUNCTION_DECL into a TEMPLATE_DECL, so the declarations won't
+       match.  But, we'll check later, when we construct the
+       template.  */
     return;
+  if (is_overloaded_fn (old))
+    {
+      for (; old; old = OVL_NEXT (old))
+       if (decls_match (decl, OVL_CURRENT (old)))
+         return;
+    }
+  else if (decls_match (decl, old))
+      return;
  complain:
-  error ("`%D' should have been declared inside `%D'",
-           decl, scope);
+  error ("%qD should have been declared inside %qD", decl, scope);
 } 
 
 /* Return the namespace where the current declaration is declared.  */
 
-tree
+static tree
 current_decl_namespace (void)
 {
   tree result;
@@ -3085,8 +2854,7 @@ push_namespace (tree name)
   /* We should not get here if the global_namespace is not yet constructed
      nor if NAME designates the global namespace:  The global scope is
      constructed elsewhere.  */
-  my_friendly_assert (global_namespace != NULL && name != global_scope_name,
-                      20030531);
+  gcc_assert (global_namespace != NULL && name != global_scope_name);
 
   if (anon)
     {
@@ -3110,8 +2878,8 @@ push_namespace (tree name)
           need_new = 0;
           if (DECL_NAMESPACE_ALIAS (d))
             {
-              error ("namespace alias `%D' not allowed here, assuming `%D'",
-                        d, DECL_NAMESPACE_ALIAS (d));
+              error ("namespace alias %qD not allowed here, assuming %qD",
+                     d, DECL_NAMESPACE_ALIAS (d));
               d = DECL_NAMESPACE_ALIAS (d);
             }
         }
@@ -3147,7 +2915,7 @@ push_namespace (tree name)
 void
 pop_namespace (void)
 {
-  my_friendly_assert (current_namespace != global_namespace, 20010801);
+  gcc_assert (current_namespace != global_namespace);
   current_namespace = CP_DECL_CONTEXT (current_namespace);
   /* The binding level is not popped, as it might be re-opened later.  */
   leave_scope ();
@@ -3225,7 +2993,7 @@ do_namespace_alias (tree alias, tree namespace)
   if (TREE_CODE (namespace) != NAMESPACE_DECL)
     {
       /* The parser did not find it, so it's not there.  */
-      error ("unknown namespace `%D'", namespace);
+      error ("unknown namespace %qD", namespace);
       return;
     }
 
@@ -3235,6 +3003,7 @@ do_namespace_alias (tree alias, tree namespace)
   alias = build_lang_decl (NAMESPACE_DECL, alias, void_type_node);     
   DECL_NAMESPACE_ALIAS (alias) = namespace;
   DECL_EXTERNAL (alias) = 1;
+  DECL_CONTEXT (alias) = FROB_CONTEXT (current_scope ());
   pushdecl (alias);
 
   /* Emit debug info for namespace alias.  */
@@ -3301,8 +3070,8 @@ add_using_namespace (tree user, tree used, bool indirect)
       timevar_pop (TV_NAME_LOOKUP);
       return;
     }
-  my_friendly_assert (TREE_CODE (user) == NAMESPACE_DECL, 380);
-  my_friendly_assert (TREE_CODE (used) == NAMESPACE_DECL, 380);
+  gcc_assert (TREE_CODE (user) == NAMESPACE_DECL);
+  gcc_assert (TREE_CODE (used) == NAMESPACE_DECL);
   /* Check if we already have this.  */
   t = purpose_member (used, DECL_NAMESPACE_USING (user));
   if (t != NULL_TREE)
@@ -3385,13 +3154,13 @@ do_using_directive (tree namespace)
     {
       /* Lookup in lexer did not find a namespace.  */
       if (!processing_template_decl)
-       error ("namespace `%T' undeclared", namespace);
+       error ("namespace %qT undeclared", namespace);
       return;
     }
   if (TREE_CODE (namespace) != NAMESPACE_DECL)
     {
       if (!processing_template_decl)
-       error ("`%T' is not a namespace", namespace);
+       error ("%qT is not a namespace", namespace);
       return;
     }
   namespace = ORIGINAL_NAMESPACE (namespace);
@@ -3430,13 +3199,13 @@ parse_using_directive (tree namespace, tree attribs)
        {
          if (!toplevel_bindings_p ())
            error ("strong using only meaningful at namespace scope");
-         else
+         else if (namespace != error_mark_node)
            DECL_NAMESPACE_ASSOCIATIONS (namespace)
              = tree_cons (current_namespace, 0,
                           DECL_NAMESPACE_ASSOCIATIONS (namespace));
        }
       else
-       warning ("`%D' attribute directive ignored", name);
+       warning ("%qD attribute directive ignored", name);
     }
 }
 
@@ -3523,7 +3292,7 @@ ambiguous_decl (tree name, struct scope_binding *old, cxx_binding *new,
                int flags)
 {
   tree val, type;
-  my_friendly_assert (old != NULL, 393);
+  gcc_assert (old != NULL);
   /* Copy the value.  */
   val = new->value;
   if (val)
@@ -3570,11 +3339,10 @@ ambiguous_decl (tree name, struct scope_binding *old, cxx_binding *new,
                 repeat ourselves.  */
              if (old->value != error_mark_node)
                {
-                 error ("use of `%D' is ambiguous", name);
-                 cp_error_at ("  first declared as `%#D' here",
-                              old->value);
+                 error ("use of %qD is ambiguous", name);
+                 cp_error_at ("  first declared as %q#D here", old->value);
                }
-              cp_error_at ("  also declared as `%#D' here", val);
+              cp_error_at ("  also declared as %q#D here", val);
             }
          old->value = error_mark_node;
        }
@@ -3589,7 +3357,7 @@ ambiguous_decl (tree name, struct scope_binding *old, cxx_binding *new,
     {
       if (flags & LOOKUP_COMPLAIN)
         {
-          error ("`%D' denotes an ambiguous type",name);
+          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));
         }
@@ -3646,7 +3414,7 @@ lookup_namespace_name (tree namespace, tree name)
   struct scope_binding binding = EMPTY_SCOPE_BINDING;
 
   timevar_push (TV_NAME_LOOKUP);
-  my_friendly_assert (TREE_CODE (namespace) == NAMESPACE_DECL, 370);
+  gcc_assert (TREE_CODE (namespace) == NAMESPACE_DECL);
 
   if (TREE_CODE (name) == NAMESPACE_DECL)
     /* This happens for A::B<int> when B is a namespace.  */
@@ -3655,7 +3423,7 @@ lookup_namespace_name (tree namespace, tree name)
     {
       /* This happens for A::B where B is a template, and there are no
         template arguments.  */
-      error ("invalid use of `%D'", name);
+      error ("invalid use of %qD", name);
       POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
     }
 
@@ -3671,7 +3439,7 @@ lookup_namespace_name (tree namespace, tree name)
        name = DECL_NAME (name);
     }
 
-  my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 373);
+  gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
 
   if (!qualified_lookup_using_namespace (name, namespace, &binding, 0))
     POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
@@ -3695,8 +3463,7 @@ lookup_namespace_name (tree namespace, tree name)
                                            TREE_OPERAND (template_id, 1));
          else
            {
-             error ("`%D::%D' is not a template",
-                       namespace, name);
+             error ("%<%D::%D%> is not a template", namespace, name);
              POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
            }
        }
@@ -3712,7 +3479,7 @@ lookup_namespace_name (tree namespace, tree name)
         POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
     }
 
-  error ("`%D' undeclared in namespace `%D'", name, namespace);
+  error ("%qD undeclared in namespace %qD", name, namespace);
   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
 }
 
@@ -4092,11 +3859,7 @@ lookup_name_real (tree name, int prefer_type, int nonclass, bool block_p,
 
   /* Now lookup in namespace scopes.  */
   if (!val)
-    {
-      tree t = unqualified_namespace_lookup (name, flags);
-      if (t)
-       val = t;
-    }
+    val = unqualified_namespace_lookup (name, flags);
 
   if (val)
     {
@@ -4131,11 +3894,104 @@ lookup_name (tree name, int prefer_type)
                           0, LOOKUP_COMPLAIN);
 }
 
+/* Look up NAME for type used in elaborated name specifier in
+   the scopes given by SCOPE.  SCOPE can be either TS_CURRENT or
+   TS_WITHIN_ENCLOSING_NON_CLASS.  Although not implied by the
+   name, more scopes are checked if cleanup or template parameter
+   scope is encountered.
+
+   Unlike lookup_name_real, we make sure that NAME is actually
+   declared in the desired scope, not from inheritance, nor using
+   directive.  For using declaration, there is DR138 still waiting
+   to be resolved.
+
+   A TYPE_DECL best matching the NAME is returned.  Catching error
+   and issuing diagnostics are caller's responsibility.  */
+
+tree
+lookup_type_scope (tree name, tag_scope scope)
+{
+  cxx_binding *iter = NULL;
+  tree val = NULL_TREE;
+
+  timevar_push (TV_NAME_LOOKUP);
+
+  /* Look in non-namespace scope first.  */
+  if (current_binding_level->kind != sk_namespace)
+    iter = outer_binding (name, NULL, /*class_p=*/ true);
+  for (; iter; iter = outer_binding (name, iter, /*class_p=*/ true))
+    {
+      /* Check if this is the kind of thing we're looking for.
+        If SCOPE is TS_CURRENT, also make sure it doesn't come from 
+        base class.  For ITER->VALUE, we can simply use
+        INHERITED_VALUE_BINDING_P.  For ITER->TYPE, we have to use 
+        our own check.
+
+        We check ITER->TYPE before ITER->VALUE in order to handle
+          typedef struct C {} C;
+        correctly.  */
+
+      if (qualify_lookup (iter->type, LOOKUP_PREFER_TYPES)
+         && (scope != ts_current
+             || LOCAL_BINDING_P (iter)
+             || DECL_CONTEXT (iter->type) == iter->scope->this_entity))
+       val = iter->type;
+      else if ((scope != ts_current
+               || !INHERITED_VALUE_BINDING_P (iter))
+              && qualify_lookup (iter->value, LOOKUP_PREFER_TYPES))
+       val = iter->value;
+
+      if (val)
+       break;
+    }
+
+  /* Look in namespace scope.  */
+  if (!val)
+    {
+      iter = cxx_scope_find_binding_for_name
+              (NAMESPACE_LEVEL (current_decl_namespace ()), name);
+
+      if (iter)
+       {
+         /* If this is the kind of thing we're looking for, we're done.
+            Ignore names found via using declaration.  See DR138 for
+            current status.  */
+         if (qualify_lookup (iter->type, LOOKUP_PREFER_TYPES))
+           val = iter->type;
+         else if (qualify_lookup (iter->value, LOOKUP_PREFER_TYPES))
+           val = iter->value;
+       }
+       
+    }
+
+  /* Type found, check if it is in the allowed scopes, ignoring cleanup
+     and template parameter scopes.  */
+  if (val)
+    {
+      struct cp_binding_level *b = current_binding_level;
+      while (b)
+       {
+         if (iter->scope == b)
+           POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
+
+         if (b->kind == sk_cleanup || b->kind == sk_template_parms)
+           b = b->level_chain;
+         else if (b->kind == sk_class
+                  && scope == ts_within_enclosing_non_class)
+           b = b->level_chain;
+         else
+           break;
+       }
+    }
+
+  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
+}
+
 /* Similar to `lookup_name' but look only in the innermost non-class
    binding level.  */
 
 static tree
-lookup_name_current_level (tree name)
+lookup_name_innermost_nonclass_level (tree name)
 {
   struct cp_binding_level *b;
   tree t = NULL_TREE;
@@ -4173,7 +4029,7 @@ lookup_name_current_level (tree name)
   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
 }
 
-/* Like lookup_name_current_level, but for types.  */
+/* Like lookup_name_innermost_nonclass_level, but for types.  */
 
 static tree
 lookup_type_current_level (tree name)
@@ -4181,8 +4037,7 @@ lookup_type_current_level (tree name)
   tree t = NULL_TREE;
 
   timevar_push (TV_NAME_LOOKUP);
-  my_friendly_assert (current_binding_level->kind != sk_namespace, 
-                     980716);
+  gcc_assert (current_binding_level->kind != sk_namespace);
 
   if (REAL_IDENTIFIER_TYPE_VALUE (name) != NULL_TREE
       && REAL_IDENTIFIER_TYPE_VALUE (name) != global_type_node)
@@ -4250,9 +4105,9 @@ add_function (struct arg_lookup *k, tree fn)
        {
          fn = f1; f1 = f2; f2 = fn;
        }
-      cp_error_at ("`%D' is not a function,", f1);
-      cp_error_at ("  conflict with `%D'", f2);
-      error ("  in call to `%D'", k->name);
+      cp_error_at ("%qD is not a function,", f1);
+      cp_error_at ("  conflict with %qD", f2);
+      error ("  in call to %qD", k->name);
       return true;
     }
 
@@ -4480,11 +4335,10 @@ arg_assoc_type (struct arg_lookup *k, tree type)
     case TYPENAME_TYPE:
       return false;
     case LANG_TYPE:
-      if (type == unknown_type_node)
-       return false;
-      /* else fall through */
+      gcc_assert (type == unknown_type_node);
+      return false;
     default:
-      abort ();
+      gcc_unreachable ();
     }
   return false;
 }
@@ -4668,9 +4522,7 @@ maybe_process_template_type_declaration (tree type, int globalize,
     {
       maybe_check_template_type (type);
 
-      my_friendly_assert (IS_AGGR_TYPE (type)
-                         || TREE_CODE (type) == ENUMERAL_TYPE, 0);
-
+      gcc_assert (IS_AGGR_TYPE (type) || TREE_CODE (type) == ENUMERAL_TYPE);
 
       if (processing_template_decl)
        {
@@ -4691,19 +4543,18 @@ maybe_process_template_type_declaration (tree type, int globalize,
              && b->level_chain->kind == sk_class)
            {
              finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type));
-             /* Put this UDT in the table of UDTs for the class, since
-                that won't happen below because B is not the class
-                binding level, but is instead the pseudo-global level.  */
-              if (b->level_chain->type_decls == NULL)
-                b->level_chain->type_decls =
-                  binding_table_new (SCOPE_DEFAULT_HT_SIZE);
-              binding_table_insert (b->level_chain->type_decls, name, type);
+
              if (!COMPLETE_TYPE_P (current_class_type))
                {
                  maybe_add_class_template_decl_list (current_class_type,
                                                      type, /*friend_p=*/0);
-                 CLASSTYPE_NESTED_UTDS (current_class_type) =
-                    b->level_chain->type_decls;
+                 /* Put this UDT in the table of UDTs for the class.  */
+                 if (CLASSTYPE_NESTED_UTDS (current_class_type) == NULL)
+                   CLASSTYPE_NESTED_UTDS (current_class_type) =
+                     binding_table_new (SCOPE_DEFAULT_HT_SIZE);
+
+                 binding_table_insert
+                   (CLASSTYPE_NESTED_UTDS (current_class_type), name, type);
                }
            }
        }
@@ -4715,9 +4566,10 @@ maybe_process_template_type_declaration (tree type, int globalize,
 /* Push a tag name NAME for struct/class/union/enum type TYPE.
    Normally put it into the inner-most non-sk_cleanup scope,
    but if GLOBALIZE is true, put it in the inner-most non-class scope.
-   The latter is needed for implicit declarations.  */
+   The latter is needed for implicit declarations.
+   Returns TYPE upon success and ERROR_MARK_NODE otherwise.  */
 
-void
+tree
 pushtag (tree name, tree type, int globalize)
 {
   struct cp_binding_level *b;
@@ -4743,10 +4595,6 @@ pushtag (tree name, tree type, int globalize)
                 || COMPLETE_TYPE_P (b->this_entity))))
     b = b->level_chain;
 
-  if (b->type_decls == NULL)
-    b->type_decls = binding_table_new (SCOPE_DEFAULT_HT_SIZE);
-  binding_table_insert (b->type_decls, name, type);
-
   if (name)
     {
       /* Do C++ gratuitous typedefing.  */
@@ -4786,6 +4634,8 @@ pushtag (tree name, tree type, int globalize)
 
          d = maybe_process_template_type_declaration (type,
                                                       globalize, b);
+         if (d == error_mark_node)
+           POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
 
          if (b->kind == sk_class)
            {
@@ -4822,7 +4672,13 @@ pushtag (tree name, tree type, int globalize)
        {
          maybe_add_class_template_decl_list (current_class_type,
                                              type, /*friend_p=*/0);
-         CLASSTYPE_NESTED_UTDS (current_class_type) = b->type_decls;
+
+         if (CLASSTYPE_NESTED_UTDS (current_class_type) == NULL)
+           CLASSTYPE_NESTED_UTDS (current_class_type)
+             = binding_table_new (SCOPE_DEFAULT_HT_SIZE);
+
+         binding_table_insert
+           (CLASSTYPE_NESTED_UTDS (current_class_type), name, type);
        }
     }
 
@@ -4842,7 +4698,7 @@ pushtag (tree name, tree type, int globalize)
       tree d = build_decl (TYPE_DECL, NULL_TREE, type);
       TYPE_STUB_DECL (type) = pushdecl_with_scope (d, b);
     }
-  timevar_pop (TV_NAME_LOOKUP);
+  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, type);
 }
 \f
 /* Subroutines for reverting temporarily to top-level for instantiation