]> gcc.gnu.org Git - gcc.git/blobdiff - gcc/cp/name-lookup.c
tree.h: Include vec.h
[gcc.git] / gcc / cp / name-lookup.c
index 7b1ff39ca9146de2d8981f6fc6b5e8f7a68331ab..ffb03eac64cc72ba6b087088aa9990ea02fb476b 100644 (file)
@@ -32,14 +32,23 @@ Boston, MA 02111-1307, USA.  */
 #include "diagnostic.h"
 #include "debug.h"
 
+/* The bindings for a particular name in a particular scope.  */
+
+struct scope_binding {
+  tree value;
+  tree type;
+};
+#define EMPTY_SCOPE_BINDING { NULL_TREE, NULL_TREE }
+
 static cxx_scope *innermost_nonclass_level (void);
-static tree select_decl (cxx_binding *, int);
+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 push_overloaded_decl (tree, int);
-static bool lookup_using_namespace (tree, cxx_binding *, tree,
+static bool lookup_using_namespace (tree, struct scope_binding *, tree,
                                     tree, int);
-static bool qualified_lookup_using_namespace (tree, tree, cxx_binding *, int);
+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 void cp_emit_debug_info_for_using (tree, tree);
@@ -319,8 +328,16 @@ binding_table_foreach (binding_table table, bt_foreach_proc proc, void *data)
 
 static GTY((deletable)) cxx_binding *free_bindings;
 
-/* Zero out a cxx_binding pointed to by B.  */
-#define cxx_binding_clear(B) memset ((B), 0, sizeof (cxx_binding))
+/* Initialize VALUE and TYPE field for BINDING, and set the PREVIOUS
+   field to NULL.  */
+
+static inline void
+cxx_binding_init (cxx_binding *binding, tree value, tree type)
+{
+  binding->value = value;
+  binding->type = type;
+  binding->previous = NULL;
+}
 
 /* (GC)-allocate a binding object with VALUE and TYPE member initialized.  */
 
@@ -336,9 +353,7 @@ cxx_binding_make (tree value, tree type)
   else
     binding = ggc_alloc (sizeof (cxx_binding));
 
-  binding->value = value;
-  binding->type = type;
-  binding->previous = NULL;
+  cxx_binding_init (binding, value, type);
 
   return binding;
 }
@@ -353,17 +368,65 @@ cxx_binding_free (cxx_binding *binding)
   free_bindings = binding;
 }
 
+/* Create a new binding for NAME (with the indicated VALUE and TYPE
+   bindings) in the class scope indicated by SCOPE.  */
+
+static cxx_binding *
+new_class_binding (tree name, tree value, tree type, cxx_scope *scope)
+{
+  cp_class_binding *cb;
+  cxx_binding *binding;
+  
+  if (VEC_length (cp_class_binding, scope->class_shadowed))
+    {
+      cp_class_binding *old_base;
+      old_base = VEC_index (cp_class_binding, scope->class_shadowed, 0);
+      if (VEC_reserve (cp_class_binding, scope->class_shadowed, -1))
+       {
+         /* 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++)
+           {
+             cxx_binding **b;
+             b = &IDENTIFIER_BINDING (cb->identifier);
+             while (*b != &old_base[i].base)
+               b = &((*b)->previous);
+             *b = &cb->base;
+           }
+       }
+      cb = VEC_quick_push (cp_class_binding, scope->class_shadowed, NULL);
+    }
+  else
+    cb = VEC_safe_push (cp_class_binding, scope->class_shadowed, NULL);
+  
+  cb->identifier = name;
+  binding = &cb->base;
+  binding->scope = scope;
+  cxx_binding_init (binding, value, type);
+  return binding;
+}
+
 /* Make DECL the innermost binding for ID.  The LEVEL is the binding
    level at which this declaration is being bound.  */
 
 static void
 push_binding (tree id, tree decl, cxx_scope* level)
 {
-   cxx_binding *binding = cxx_binding_make (decl, NULL);
+  cxx_binding *binding;
 
+  if (level != class_binding_level)
+    {
+      binding = cxx_binding_make (decl, NULL_TREE);
+      binding->scope = level;
+    }
+  else
+    binding = new_class_binding (id, decl, /*type=*/NULL_TREE, level);
+                             
   /* Now, fill in the binding information.  */
   binding->previous = IDENTIFIER_BINDING (id);
-  binding->scope = level;
   INHERITED_VALUE_BINDING_P (binding) = 0;
   LOCAL_BINDING_P (binding) = (level != class_binding_level);
 
@@ -497,11 +560,22 @@ supplement_binding (cxx_binding *binding, tree decl)
       duplicate_decls (decl, binding->value);
       ok = false;
     }
+  else if (TREE_CODE (decl) == NAMESPACE_DECL
+          && TREE_CODE (bval) == NAMESPACE_DECL
+          && DECL_NAMESPACE_ALIAS (decl)
+          && DECL_NAMESPACE_ALIAS (bval)
+          && ORIGINAL_NAMESPACE (bval) == ORIGINAL_NAMESPACE (decl))
+    /* [namespace.alias]
+       
+      In a declarative region, a namespace-alias-definition can be
+      used to redefine a namespace-alias declared in that declarative
+      region to refer only to the namespace to which it already
+      refers.  */
+    ok = false;
   else
     {
       error ("declaration of `%#D'", decl);
-      cp_error_at ("conflicts with previous declaration `%#D'",
-                  binding->value);
+      cp_error_at ("conflicts with previous declaration `%#D'", bval);
       ok = false;
     }
 
@@ -535,7 +609,7 @@ add_decl_to_level (tree decl, cxx_scope *b)
       /* If appropriate, add decl to separate list of statics.  We
         include extern variables because they might turn out to be 
         static later.  It's OK for this list to contain a few false
-        positives. */
+        positives.  */
       if (b->kind == sk_namespace)
        if ((TREE_CODE (decl) == VAR_DECL
             && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
@@ -616,7 +690,7 @@ pushdecl (tree x)
          && DECL_EXTERNAL (x))
        {
          /* Look in block scope.  */
-         t = IDENTIFIER_VALUE (name);
+         t = innermost_non_namespace_value (name);
          /* Or in the innermost namespace.  */
          if (! t)
            t = namespace_binding (name, DECL_CONTEXT (x));
@@ -660,9 +734,15 @@ pushdecl (tree x)
              if (decls_match (x, t))
                /* The standard only says that the local extern
                   inherits linkage from the previous decl; in
-                  particular, default args are not shared.  It would
-                  be nice to propagate inlining info, though.  FIXME.  */
-               TREE_PUBLIC (x) = TREE_PUBLIC (t);
+                  particular, default args are not shared.  We must
+                  also tell cgraph to treat these decls as the same,
+                  or we may neglect to emit an "unused" static - we
+                  do this by making the DECL_UIDs equal, which should
+                  be viewed as a kludge.  FIXME.  */
+               {
+                 TREE_PUBLIC (x) = TREE_PUBLIC (t);
+                 DECL_UID (x) = DECL_UID (t);
+               }
            }
          else if (TREE_CODE (t) == PARM_DECL)
            {
@@ -758,7 +838,7 @@ pushdecl (tree x)
       if (TREE_CODE (x) == TYPE_DECL)
        {
          tree type = TREE_TYPE (x);
-         if (DECL_SOURCE_LINE (x) == 0)
+         if (DECL_IS_BUILTIN (x))
             {
              if (TYPE_NAME (type) == 0)
                TYPE_NAME (type) = x;
@@ -838,7 +918,7 @@ pushdecl (tree x)
       else
        {
          /* Here to install a non-global value.  */
-         tree oldlocal = IDENTIFIER_VALUE (name);
+         tree oldlocal = innermost_non_namespace_value (name);
          tree oldglobal = IDENTIFIER_NAMESPACE_VALUE (name);
 
          if (need_new_binding)
@@ -942,9 +1022,17 @@ pushdecl (tree x)
              /* No shadow warnings for vars made for inlining.  */
              && ! DECL_FROM_INLINE (x))
            {
-             if (IDENTIFIER_CLASS_VALUE (name) != NULL_TREE
-                      && current_class_ptr
-                      && !TREE_STATIC (name))
+             tree member;
+
+             if (current_class_ptr)
+               member = lookup_member (current_class_type,
+                                       name,
+                                       /*protect=*/0,
+                                       /*want_type=*/false);
+             else
+               member = NULL_TREE;
+                 
+             if (member && !TREE_STATIC (member))
                {
                  /* Location of previous decl is not useful in this case.  */
                  warning ("declaration of '%D' shadows a member of 'this'",
@@ -1050,59 +1138,6 @@ push_local_binding (tree id, tree decl, int flags)
   add_decl_to_level (decl, b);
 }
 
-/* The old ARM scoping rules injected variables declared in the
-   initialization statement of a for-statement into the surrounding
-   scope.  We support this usage, in order to be backward-compatible.
-   DECL is a just-declared VAR_DECL; if necessary inject its
-   declaration into the surrounding scope.  */
-
-void
-maybe_inject_for_scope_var (tree decl)
-{
-  timevar_push (TV_NAME_LOOKUP);
-  if (!DECL_NAME (decl))
-    {
-      timevar_pop (TV_NAME_LOOKUP);
-      return;
-    }
-  
-  /* Declarations of __FUNCTION__ and its ilk appear magically when
-     the variable is first used.  If that happens to be inside a
-     for-loop, we don't want to do anything special.  */
-  if (DECL_PRETTY_FUNCTION_P (decl))
-    {
-      timevar_pop (TV_NAME_LOOKUP);
-      return;
-    }
-
-  if (current_binding_level->kind == sk_for)
-    {
-      struct cp_binding_level *outer
-       = current_binding_level->level_chain;
-
-      /* Check to see if the same name is already bound at the outer
-        level, either because it was directly declared, or because a
-        dead for-decl got preserved.  In either case, the code would
-        not have been valid under the ARM scope rules, so clear
-        is_for_scope for the current_binding_level.
-
-        Otherwise, we need to preserve the temp slot for decl to last
-        into the outer binding level.  */
-
-      cxx_binding *outer_binding
-       = IDENTIFIER_BINDING (DECL_NAME (decl))->previous;
-
-      if (outer_binding && outer_binding->scope == outer
-         && (TREE_CODE (outer_binding->value) == VAR_DECL)
-         && DECL_DEAD_FOR_LOCAL (outer_binding->value))
-       {
-         outer_binding->value = DECL_SHADOWED_FOR_VAR (outer_binding->value);
-         current_binding_level->kind = sk_block;
-       }
-    }
-  timevar_pop (TV_NAME_LOOKUP);
-}
-
 /* Check to see whether or not DECL is a variable that would have been
    in scope under the ARM, but is not in scope under the ANSI/ISO
    standard.  If so, issue an error message.  If name lookup would
@@ -1145,6 +1180,10 @@ check_for_out_of_scope_variable (tree decl)
     return decl;
 
   DECL_ERROR_REPORTED (decl) = 1;
+
+  if (TREE_TYPE (decl) == error_mark_node)
+    return decl;
+
   if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
     {
       error ("name lookup of `%D' changed for new ISO `for' scoping",
@@ -1234,6 +1273,26 @@ namespace_scope_ht_size (tree ns)
 
 static GTY((deletable)) struct cp_binding_level *free_binding_level;
 
+/* Insert SCOPE as the innermost binding level.  */
+
+void
+push_binding_level (struct cp_binding_level *scope)
+{
+  /* Add it to the front of currently active scopes stack.  */
+  scope->level_chain = current_binding_level;
+  current_binding_level = scope;
+  keep_next_level_flag = false;
+
+  if (ENABLE_SCOPE_CHECKING)
+    {
+      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.  */
@@ -1292,19 +1351,7 @@ begin_scope (scope_kind kind, tree entity)
     }
   scope->kind = kind;
 
-  /* Add it to the front of currently active scopes stack.  */
-  scope->level_chain = current_binding_level;
-  current_binding_level = scope;
-  keep_next_level_flag = false;
-
-  if (ENABLE_SCOPE_CHECKING)
-    {
-      scope->binding_depth = binding_depth;
-      indent (binding_depth);
-      cxx_scope_debug (scope, input_location.line, "push");
-      is_class_level = 0;
-      binding_depth++;
-    }
+  push_binding_level (scope);
 
   return scope;
 }
@@ -1327,7 +1374,7 @@ leave_scope (void)
   if (ENABLE_SCOPE_CHECKING)
     {
       indent (--binding_depth);
-      cxx_scope_debug (scope, input_location.line, "leave");
+      cxx_scope_debug (scope, input_line, "leave");
       if (is_class_level != (scope == class_binding_level))
         {
           indent (binding_depth);
@@ -1339,11 +1386,13 @@ leave_scope (void)
   /* Move one nesting level up.  */
   current_binding_level = scope->level_chain;
 
-  /* Namespace-scopes are left most probably temporarily, not completely;
-     they can be reopen later, e.g. in namespace-extension or any name
-     binding activity that requires us to resume a namespace.  For other
+  /* Namespace-scopes are left most probably temporarily, not
+     completely; they can be reopen later, e.g. in namespace-extension
+     or any name binding activity that requires us to resume a
+     namespace.  For classes, we cache some binding levels.  For other
      scopes, we just make the structure available for reuse.  */
-  if (scope->kind != sk_namespace)
+  if (scope->kind != sk_namespace
+      && scope->kind != sk_class)
     {
       scope->level_chain = free_binding_level;
       if (scope->kind == sk_class)
@@ -1380,7 +1429,7 @@ resume_scope (struct cp_binding_level* b)
     {
       b->binding_depth = binding_depth;
       indent (binding_depth);
-      cxx_scope_debug (b, input_location.line, "resume");
+      cxx_scope_debug (b, input_line, "resume");
       is_class_level = 0;
       binding_depth++;
     }
@@ -1412,8 +1461,7 @@ maybe_push_cleanup_level (tree type)
       && current_binding_level->more_cleanups_ok == 0)
     {
       begin_scope (sk_cleanup, NULL);
-      clear_last_expr ();
-      add_scope_stmt (/*begin_p=*/1, /*partial_p=*/1);
+      current_binding_level->statement_list = push_stmt_list ();
     }
 }
 
@@ -1573,7 +1621,7 @@ print_binding_level (struct cp_binding_level* lvl)
            continue;
          if (no_print_builtins
              && (TREE_CODE (t) == TYPE_DECL)
-             && (!strcmp (DECL_SOURCE_FILE (t),"<built-in>")))
+             && DECL_IS_BUILTIN (t))
            continue;
 
          /* Function decls tend to have longer names.  */
@@ -1602,13 +1650,15 @@ print_binding_level (struct cp_binding_level* lvl)
       if (i)
        fprintf (stderr, "\n");
     }
-  if (lvl->class_shadowed)
+  if (VEC_length (cp_class_binding, lvl->class_shadowed))
     {
+      size_t i;
+      cp_class_binding *b;
       fprintf (stderr, " class-shadowed:");
-      for (t = lvl->class_shadowed; t; t = TREE_CHAIN (t))
-       {
-         fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
-       }
+      for (i = 0; 
+          VEC_iterate(cp_class_binding, lvl->class_shadowed, i, b);
+          ++i) 
+       fprintf (stderr, " %s ", IDENTIFIER_POINTER (b->identifier));
       fprintf (stderr, "\n");
     }
   if (lvl->type_shadowed)
@@ -1673,7 +1723,7 @@ identifier_type_value (tree id)
     POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, REAL_IDENTIFIER_TYPE_VALUE (id));
   /* Have to search for it. It must be on the global level, now.
      Ask lookup_name not to return non-types.  */
-  id = lookup_name_real (id, 2, 1, 0, LOOKUP_COMPLAIN);
+  id = lookup_name_real (id, 2, 1, /*block_p=*/true, 0, LOOKUP_COMPLAIN);
   if (id)
     POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, TREE_TYPE (id));
   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
@@ -1705,6 +1755,7 @@ set_identifier_type_value_with_scope (tree id, tree decl, cxx_scope *b)
       b->type_shadowed
        = tree_cons (id, old_type_value, b->type_shadowed);
       type = decl ? TREE_TYPE (decl) : NULL_TREE;
+      TREE_TYPE (b->type_shadowed) = type;
     }
   else
     {
@@ -1741,12 +1792,7 @@ set_identifier_type_value (tree id, tree decl)
 tree
 constructor_name_full (tree type)
 {
-  type = TYPE_MAIN_VARIANT (type);
-  if (CLASS_TYPE_P (type) && TYPE_WAS_ANONYMOUS (type) 
-      && TYPE_HAS_CONSTRUCTOR (type))
-    return DECL_NAME (OVL_CURRENT (CLASSTYPE_CONSTRUCTORS (type)));
-  else
-    return TYPE_IDENTIFIER (type);
+  return TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
 }
 
 /* Return the name for the constructor (or destructor) for the
@@ -2116,10 +2162,9 @@ static void
 do_nonmember_using_decl (tree scope, tree name, tree oldval, tree oldtype,
                          tree *newval, tree *newtype)
 {
-  cxx_binding decls;
+  struct scope_binding decls = EMPTY_SCOPE_BINDING;
 
   *newval = *newtype = NULL_TREE;
-  cxx_binding_clear (&decls);
   if (!qualified_lookup_using_namespace (name, scope, &decls, 0))
     /* Lookup error */
     return;
@@ -2243,7 +2288,7 @@ do_local_using_decl (tree decl, tree scope, tree name)
 
   if (building_stmt_tree ()
       && at_function_scope_p ())
-    add_decl_stmt (decl);
+    add_decl_expr (decl);
 
   oldval = lookup_name_current_level (name);
   oldtype = lookup_type_current_level (name);
@@ -2362,23 +2407,24 @@ lookup_tag (enum tree_code form, tree name,
          {
             cxx_binding *binding =
               cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (tail), name);
-           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 && binding->value
-               && DECL_CLASS_TEMPLATE_P (binding->value))
-             old = binding->value;
-           else if (binding)
-             old = select_decl (binding, LOOKUP_PREFER_TYPES);
-            else
-              old = NULL_TREE;
-
-           if (old)
+
+           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.  */
@@ -2550,65 +2596,31 @@ void
 poplevel_class (void)
 {
   struct cp_binding_level *level = class_binding_level;
+  cp_class_binding *cb;
+  size_t i;
   tree shadowed;
 
   timevar_push (TV_NAME_LOOKUP);
   my_friendly_assert (level != 0, 354);
 
-  /* If we're leaving a toplevel class, don't bother to do the setting
-     of IDENTIFIER_CLASS_VALUE to NULL_TREE, since first of all this slot
-     shouldn't even be used when current_class_type isn't set, and second,
-     if we don't touch it here, we're able to use the cache effect if the
-     next time we're entering a class scope, it is the same class.  */
-  if (current_class_depth != 1)
-    {
-      struct cp_binding_level* b;
-
-      /* Clear out our IDENTIFIER_CLASS_VALUEs.  */
-      for (shadowed = level->class_shadowed;
-          shadowed;
-          shadowed = TREE_CHAIN (shadowed))
-       IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (shadowed)) = NULL_TREE;
-
-      /* Find the next enclosing class, and recreate
-        IDENTIFIER_CLASS_VALUEs appropriate for that class.  */
-      b = level->level_chain;
-      while (b && b->kind != sk_class)
-       b = b->level_chain;
-
-      if (b)
-       for (shadowed = b->class_shadowed;
-            shadowed;
-            shadowed = TREE_CHAIN (shadowed))
-         {
-           cxx_binding *binding;
-            
-           binding = IDENTIFIER_BINDING (TREE_PURPOSE (shadowed));
-           while (binding && binding->scope != b)
-             binding = binding->previous;
-
-           if (binding)
-             IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (shadowed))
-               = binding->value;
-         }
-    }
-  else
-    /* Remember to save what IDENTIFIER's were bound in this scope so we
-       can recover from cache misses.  */
-    {
-      previous_class_type = current_class_type;
-      previous_class_values = class_binding_level->class_shadowed;
-    }
+  /* If we're leaving a toplevel class, cache its binding level.  */
+  if (current_class_depth == 1)
+    previous_class_level = level;
   for (shadowed = level->type_shadowed;
        shadowed;
        shadowed = TREE_CHAIN (shadowed))
     SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed), TREE_VALUE (shadowed));
 
   /* Remove the bindings for all of the class-level declarations.  */
-  for (shadowed = level->class_shadowed;
-       shadowed;
-       shadowed = TREE_CHAIN (shadowed))
-    pop_binding (TREE_PURPOSE (shadowed), TREE_TYPE (shadowed));
+  if (level->class_shadowed)
+    {
+      for (i = 0;
+          VEC_iterate (cp_class_binding, level->class_shadowed, i, cb);
+          ++i)
+       IDENTIFIER_BINDING (cb->identifier) = cb->base.previous;
+      ggc_free (level->class_shadowed);
+      level->class_shadowed = NULL;
+    }
 
   /* Now, pop out of the binding level which we created up in the
      `pushlevel_class' routine.  */
@@ -2619,40 +2631,18 @@ poplevel_class (void)
   timevar_pop (TV_NAME_LOOKUP);
 }
 
-/* Bind DECL to ID in the class_binding_level.  Returns nonzero if the
-   binding was successful.  */
+/* Set INHERITED_VALUE_BINDING_P on BINDING to true or false, as
+   appropriate.  DECL is the value to which a name has just been
+   bound.  CLASS_TYPE is the class in which the lookup occurred.  */
 
-int
-push_class_binding (tree id, tree decl)
+static void
+set_inherited_value_binding_p (cxx_binding *binding, tree decl,
+                              tree class_type)
 {
-  int result = 1;
-  cxx_binding *binding = IDENTIFIER_BINDING (id);
-  tree context;
-
-  timevar_push (TV_NAME_LOOKUP);
-  /* Note that we declared this value so that we can issue an error if
-     this is an invalid redeclaration of a name already used for some
-     other purpose.  */
-  note_name_declared_in_class (id, decl);
-
-  if (binding && binding->scope == class_binding_level)
-    /* Supplement the existing binding.  */
-    result = supplement_binding (IDENTIFIER_BINDING (id), decl);
-  else
-    /* Create a new binding.  */
-    push_binding (id, decl, class_binding_level);
-
-  /* Update the IDENTIFIER_CLASS_VALUE for this ID to be the
-     class-level declaration.  Note that we do not use DECL here
-     because of the possibility of the `struct stat' hack; if DECL is
-     a class-name or enum-name we might prefer a field-name, or some
-     such.  */
-  IDENTIFIER_CLASS_VALUE (id) = IDENTIFIER_BINDING (id)->value;
-
-  /* If this is a binding from a base class, mark it as such.  */
-  binding = IDENTIFIER_BINDING (id);
   if (binding->value == decl && TREE_CODE (decl) != TREE_LIST)
     {
+      tree context;
+
       if (TREE_CODE (decl) == OVERLOAD)
        context = CP_DECL_CONTEXT (OVL_CURRENT (decl));
       else
@@ -2661,35 +2651,18 @@ push_class_binding (tree id, tree decl)
          context = context_for_name_lookup (decl);
        }
 
-      if (is_properly_derived_from (current_class_type, context))
+      if (is_properly_derived_from (class_type, context))
        INHERITED_VALUE_BINDING_P (binding) = 1;
       else
        INHERITED_VALUE_BINDING_P (binding) = 0;
     }
   else if (binding->value == decl)
-    /* We only encounter a TREE_LIST when push_class_decls detects an
-       ambiguity.  Such an ambiguity can be overridden by a definition
-       in this class.  */
+    /* We only encounter a TREE_LIST when there is an ambiguity in the
+       base classes.  Such an ambiguity can be overridden by a
+       definition in this class.  */
     INHERITED_VALUE_BINDING_P (binding) = 1;
-
-  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, result);
-}
-
-/* We are entering the scope of a class.  Clear IDENTIFIER_CLASS_VALUE
-   for any names in enclosing classes.  */
-
-void
-clear_identifier_class_values (void)
-{
-  tree t;
-
-  if (!class_binding_level)
-    return;
-
-  for (t = class_binding_level->class_shadowed;
-       t;
-       t = TREE_CHAIN (t))
-    IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (t)) = NULL_TREE;
+  else
+    INHERITED_VALUE_BINDING_P (binding) = 0;
 }
 
 /* Make the declaration of X appear in CLASS scope.  */
@@ -2734,6 +2707,67 @@ pushdecl_class_level (tree x)
   return is_valid;
 }
 
+/* Return the BINDING (if any) for NAME in SCOPE, which is a class
+   scope.  If the value returned is non-NULL, and the PREVIOUS field
+   is not set, callers must set the PREVIOUS field explicitly.  */
+
+static cxx_binding *
+get_class_binding (tree name, cxx_scope *scope)
+{
+  tree class_type;
+  tree type_binding;
+  tree value_binding;
+  cxx_binding *binding;
+
+  class_type = scope->this_entity;
+
+  /* Get the type binding.  */
+  type_binding = lookup_member (class_type, name,
+                               /*protect=*/2, /*want_type=*/true);
+  /* Get the value binding.  */
+  value_binding = lookup_member (class_type, name,
+                                /*protect=*/2, /*want_type=*/false);
+
+  if (value_binding
+      && (TREE_CODE (value_binding) == TYPE_DECL
+         || DECL_CLASS_TEMPLATE_P (value_binding)
+         || (TREE_CODE (value_binding) == TREE_LIST
+             && TREE_TYPE (value_binding) == error_mark_node
+             && (TREE_CODE (TREE_VALUE (value_binding))
+                 == TYPE_DECL))))
+    /* We found a type binding, even when looking for a non-type
+       binding.  This means that we already processed this binding
+       above.  */
+    ;
+  else if (value_binding)
+    {
+      if (TREE_CODE (value_binding) == TREE_LIST 
+         && TREE_TYPE (value_binding) == error_mark_node)
+       /* NAME is ambiguous.  */
+       ;
+      else if (BASELINK_P (value_binding))
+       /* NAME is some overloaded functions.  */
+       value_binding = BASELINK_FUNCTIONS (value_binding);
+    }
+
+  /* If we found either a type binding or a value binding, create a
+     new binding object.  */
+  if (type_binding || value_binding)
+    {
+      binding = new_class_binding (name, 
+                                  value_binding, 
+                                  type_binding,
+                                  scope);
+      /* This is a class-scope binding, not a block-scope binding.  */
+      LOCAL_BINDING_P (binding) = 0;
+      set_inherited_value_binding_p (binding, value_binding, class_type);
+    }
+  else
+    binding = NULL;
+
+  return binding;
+}
+                  
 /* Make the declaration(s) of X appear in CLASS scope under the name
    NAME.  Returns true if the binding is valid.  */
 
@@ -2741,6 +2775,8 @@ bool
 push_class_level_binding (tree name, tree x)
 {
   cxx_binding *binding;
+  tree decl = x;
+  bool ok;
 
   timevar_push (TV_NAME_LOOKUP);
   /* The class_binding_level will be NULL if x is a template
@@ -2748,10 +2784,16 @@ push_class_level_binding (tree name, tree x)
   if (!class_binding_level)
     POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true);
 
-  /* Make sure that this new member does not have the same name
-     as a template parameter.  */
-  if (TYPE_BEING_DEFINED (current_class_type))
-    check_template_shadow (x);
+  /* Check for invalid member names.  */
+  my_friendly_assert (TYPE_BEING_DEFINED (current_class_type), 20040713);
+  /* 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. */
+  if (TREE_CODE (decl) == TREE_LIST 
+      && TREE_TYPE (decl) == error_mark_node)
+    decl = TREE_VALUE (decl);
+
+  check_template_shadow (decl);
 
   /* [class.mem]
 
@@ -2782,16 +2824,29 @@ 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 declared",
+         error ("`%D' has the same name as the class in which it is "
+                "declared",
                 x);
          POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, false);
        }
     }
 
-  /* If this declaration shadows a declaration from an enclosing
-     class, then we will need to restore IDENTIFIER_CLASS_VALUE when
-     we leave this class.  Record the shadowed declaration here.  */
+  /* Get the current binding for NAME in this class, if any.  */
   binding = IDENTIFIER_BINDING (name);
+  if (!binding || binding->scope != class_binding_level)
+    {
+      binding = get_class_binding (name, class_binding_level);
+      /* If a new binding was created, put it at the front of the
+        IDENTIFIER_BINDING list.  */
+      if (binding)
+       {
+         binding->previous = IDENTIFIER_BINDING (name);
+         IDENTIFIER_BINDING (name) = binding;
+       }
+    }
+
+  /* If there is already a binding, then we may need to update the
+     current value.  */
   if (binding && binding->value)
     {
       tree bval = binding->value;
@@ -2822,42 +2877,37 @@ push_class_level_binding (tree name, tree x)
        old_decl = bval;
       else if (TREE_CODE (bval) == USING_DECL && is_overloaded_fn (x))
        POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true);
-      
-      if (old_decl)
+
+      if (old_decl && binding->scope == class_binding_level)
        {
-         tree shadow;
-         
-         /* Find the previous binding of name on the class-shadowed
-             list, and update it.  */
-         for (shadow = class_binding_level->class_shadowed;
-              shadow;
-              shadow = TREE_CHAIN (shadow))
-           if (TREE_PURPOSE (shadow) == name
-               && TREE_TYPE (shadow) == old_decl)
-             {
-               binding->value = x;
-               INHERITED_VALUE_BINDING_P (binding) = 0;
-               TREE_TYPE (shadow) = x;
-               IDENTIFIER_CLASS_VALUE (name) = x;
-               POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true);
-             }
+         binding->value = x;
+         /* It is always safe to clear INHERITED_VALUE_BINDING_P
+            here.  This function is only used to register bindings
+            from with the class definition itself.  */
+         INHERITED_VALUE_BINDING_P (binding) = 0;
+         POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true);
        }
     }
 
+  /* Note that we declared this value so that we can issue an error if
+     this is an invalid redeclaration of a name already used for some
+     other purpose.  */
+  note_name_declared_in_class (name, decl);
+
   /* If we didn't replace an existing binding, put the binding on the
-     stack of bindings for the identifier, and update the shadowed list.  */
-  if (push_class_binding (name, x))
+     stack of bindings for the identifier, and update the shadowed
+     list.  */
+  if (binding && binding->scope == class_binding_level)
+    /* Supplement the existing binding.  */
+    ok = supplement_binding (binding, decl);
+  else
     {
-      class_binding_level->class_shadowed
-       = tree_cons (name, NULL,
-                    class_binding_level->class_shadowed);
-      /* Record the value we are binding NAME to so that we can know
-        what to pop later.  */
-      TREE_TYPE (class_binding_level->class_shadowed) = x;
-      POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true);
+      /* Create a new binding.  */
+      push_binding (name, decl, class_binding_level);
+      ok = true;
     }
 
-  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, false);
+  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ok);
 }
 
 tree
@@ -2908,11 +2958,6 @@ do_class_using_decl (tree decl)
   return value;
 }
 
-void
-set_class_shadows (tree shadows)
-{
-  class_binding_level->class_shadowed = shadows;
-}
 \f
 /* Return the binding value for name in scope.  */
 
@@ -3493,8 +3538,9 @@ merge_functions (tree s1, tree s2)
    XXX In what way should I treat extern declarations?
    XXX I don't want to repeat the entire duplicate_decls here */
 
-static cxx_binding *
-ambiguous_decl (tree name, cxx_binding *old, cxx_binding *new, int flags)
+static void
+ambiguous_decl (tree name, struct scope_binding *old, cxx_binding *new,
+               int flags)
 {
   tree val, type;
   my_friendly_assert (old != NULL, 393);
@@ -3568,7 +3614,6 @@ ambiguous_decl (tree name, cxx_binding *old, cxx_binding *new, int flags)
           error ("%J  other type here", TYPE_MAIN_DECL (type));
         }
     }
-  return old;
 }
 
 /* Return the declarations that are members of the namespace NS.  */
@@ -3618,7 +3663,7 @@ lookup_namespace_name (tree namespace, tree name)
 {
   tree val;
   tree template_id = NULL_TREE;
-  cxx_binding binding;
+  struct scope_binding binding = EMPTY_SCOPE_BINDING;
 
   timevar_push (TV_NAME_LOOKUP);
   my_friendly_assert (TREE_CODE (namespace) == NAMESPACE_DECL, 370);
@@ -3648,7 +3693,6 @@ lookup_namespace_name (tree namespace, tree name)
 
   my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 373);
 
-  cxx_binding_clear (&binding);
   if (!qualified_lookup_using_namespace (name, namespace, &binding, 0))
     POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
 
@@ -3695,7 +3739,7 @@ lookup_namespace_name (tree namespace, tree name)
 /* Select the right _DECL from multiple choices.  */
 
 static tree
-select_decl (cxx_binding *binding, int flags)
+select_decl (const struct scope_binding *binding, int flags)
 {
   tree val;
   val = binding->value;
@@ -3714,9 +3758,8 @@ select_decl (cxx_binding *binding, int flags)
   if (binding->type && (!val || (flags & LOOKUP_PREFER_TYPES)))
     val = binding->type;
   /* Don't return non-types if we really prefer types.  */
-  else if (val && LOOKUP_TYPES_ONLY (flags)  && TREE_CODE (val) != TYPE_DECL
-          && (TREE_CODE (val) != TEMPLATE_DECL
-              || !DECL_CLASS_TEMPLATE_P (val)))
+  else if (val && LOOKUP_TYPES_ONLY (flags) 
+          && ! DECL_DECLARES_TYPE_P (val))
     val = NULL_TREE;
 
   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
@@ -3733,26 +3776,26 @@ unqualified_namespace_lookup (tree name, int flags)
   tree siter;
   struct cp_binding_level *level;
   tree val = NULL_TREE;
-  cxx_binding binding;
+  struct scope_binding binding = EMPTY_SCOPE_BINDING;
 
   timevar_push (TV_NAME_LOOKUP);
-  cxx_binding_clear (&binding);
 
   for (; !val; scope = CP_DECL_CONTEXT (scope))
     {
       cxx_binding *b =
          cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
 
-      /* Ignore anticipated built-in functions.  */
-      if (b && b->value && DECL_P (b->value)
-          && DECL_LANG_SPECIFIC (b->value) && DECL_ANTICIPATED (b->value))
-        /* Keep binding cleared.  */;
-      else if (b)
-        {
-          /* Initialize binding for this context.  */
-          binding.value = b->value;
-          binding.type = b->type;
-        }
+      if (b)
+       {
+         if (b->value && DECL_P (b->value)
+             && DECL_LANG_SPECIFIC (b->value) 
+             && DECL_ANTICIPATED (b->value))
+           /* Ignore anticipated built-in functions.  */
+           ;
+         else
+           binding.value = b->value;
+         binding.type = b->type;
+       }
 
       /* Add all _DECLs seen through local using-directives.  */
       for (level = current_binding_level;
@@ -3800,9 +3843,8 @@ lookup_qualified_name (tree scope, tree name, bool is_type_p, bool complain)
 
   if (TREE_CODE (scope) == NAMESPACE_DECL)
     {
-      cxx_binding binding;
+      struct scope_binding binding = EMPTY_SCOPE_BINDING;
 
-      cxx_binding_clear (&binding);
       flags |= LOOKUP_COMPLAIN;
       if (is_type_p)
        flags |= LOOKUP_PREFER_TYPES;
@@ -3828,8 +3870,8 @@ lookup_qualified_name (tree scope, tree name, bool is_type_p, bool complain)
    Returns false on errors.  */
 
 static bool
-lookup_using_namespace (tree name, cxx_binding *val, tree usings, tree scope,
-                        int flags)
+lookup_using_namespace (tree name, struct scope_binding *val,
+                       tree usings, tree scope, int flags)
 {
   tree iter;
   timevar_push (TV_NAME_LOOKUP);
@@ -3843,7 +3885,7 @@ lookup_using_namespace (tree name, cxx_binding *val, tree usings, tree scope,
           cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (used), name);
         /* Resolve ambiguities.  */
         if (val1)
-          val = ambiguous_decl (name, val, val1, flags);
+          ambiguous_decl (name, val, val1, flags);
       }
   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val->value != error_mark_node);
 }
@@ -3854,8 +3896,8 @@ lookup_using_namespace (tree name, cxx_binding *val, tree usings, tree scope,
    or false on error.  */
 
 static bool
-qualified_lookup_using_namespace (tree name, tree scope, cxx_binding *result,
-                                  int flags)
+qualified_lookup_using_namespace (tree name, tree scope,
+                                 struct scope_binding *result, int flags)
 {
   /* Maintain a list of namespaces visited...  */
   tree seen = NULL_TREE;
@@ -3872,7 +3914,7 @@ qualified_lookup_using_namespace (tree name, tree scope, cxx_binding *result,
        cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
       seen = tree_cons (scope, NULL_TREE, seen);
       if (binding)
-        result = ambiguous_decl (name, result, binding, flags);
+        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
@@ -3917,6 +3959,73 @@ qualified_lookup_using_namespace (tree name, tree scope, cxx_binding *result,
   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, result->value != error_mark_node);
 }
 
+/* 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.  */
+
+cxx_binding *
+outer_binding (tree name, 
+              cxx_binding *binding,
+              bool class_p)
+{
+  cxx_binding *outer;
+  cxx_scope *scope;
+  cxx_scope *outer_scope;
+
+  if (binding)
+    {
+      scope = binding->scope->level_chain;
+      outer = binding->previous;
+    }
+  else
+    {
+      scope = current_binding_level;
+      outer = IDENTIFIER_BINDING (name);
+    }
+  outer_scope = outer ? outer->scope : NULL;
+
+  /* Because we create class bindings lazily, we might be missing a
+     class binding for NAME.  If there are any class binding levels
+     between the LAST_BINDING_LEVEL and the scope in which OUTER was
+     declared, we must lookup NAME in those class scopes.  */
+  if (class_p)
+    while (scope && scope != outer_scope && scope->kind != sk_namespace)
+      {
+       if (scope->kind == sk_class) 
+         {
+           cxx_binding *class_binding;
+           
+           class_binding = get_class_binding (name, scope);
+           if (class_binding)
+             {
+               /* Thread this new class-scope binding onto the
+                  IDENTIFIER_BINDING list so that future lookups
+                  find it quickly.  */
+               class_binding->previous = outer;
+               if (binding)
+                 binding->previous = class_binding;
+               else
+                 IDENTIFIER_BINDING (name) = class_binding;
+               return class_binding;
+             }
+         }
+       scope = scope->level_chain;
+      }
+
+  return outer;
+}
+
+/* Return the innermost block-scope or class-scope value binding for
+   NAME, or NULL_TREE if there is no such binding.  */
+
+tree
+innermost_non_namespace_value (tree name)
+{
+  cxx_binding *binding;
+  binding = outer_binding (name, /*binding=*/NULL, /*class_p=*/true);
+  return binding ? binding->value : NULL_TREE;
+}
+
 /* Look up NAME in the current binding level and its superiors in the
    namespace of variables, functions and typedefs.  Return a ..._DECL
    node of some kind representing its definition if there is only one
@@ -3927,11 +4036,11 @@ qualified_lookup_using_namespace (tree name, tree scope, cxx_binding *result,
    If PREFER_TYPE is > 1, we reject non-type decls (e.g. namespaces).
    Otherwise we prefer non-TYPE_DECLs.
 
-   If NONCLASS is nonzero, we don't look for the NAME in class scope,
-   using IDENTIFIER_CLASS_VALUE.  */
+   If NONCLASS is nonzero, bindings in class scopes are ignored.  If
+   BLOCK_P is false, bindings in block scopes are ignored.  */
 
 tree
-lookup_name_real (tree name, int prefer_type, int nonclass, 
+lookup_name_real (tree name, int prefer_type, int nonclass, bool block_p,
                  int namespaces_only, int flags)
 {
   cxx_binding *iter;
@@ -3974,29 +4083,32 @@ lookup_name_real (tree name, int prefer_type, int nonclass,
   if (current_class_type == NULL_TREE)
     nonclass = 1;
 
-  for (iter = IDENTIFIER_BINDING (name); iter; iter = iter->previous)
-    {
-      tree binding;
-
-      if (!LOCAL_BINDING_P (iter) && nonclass)
-       /* We're not looking for class-scoped bindings, so keep going.  */
-       continue;
-
-      /* If this is the kind of thing we're looking for, we're done.  */
-      if (qualify_lookup (iter->value, flags))
-       binding = iter->value;
-      else if ((flags & LOOKUP_PREFER_TYPES)
-              && qualify_lookup (iter->type, flags))
-       binding = iter->type;
-      else
-       binding = NULL_TREE;
-
-      if (binding)
-       {
-         val = binding;
-         break;
-       }
-    }
+  if (block_p || !nonclass)
+    for (iter = outer_binding (name, NULL, !nonclass);
+        iter;
+        iter = outer_binding (name, iter, !nonclass))
+      {
+       tree binding;
+       
+       /* Skip entities we don't want.  */
+       if (LOCAL_BINDING_P (iter) ? !block_p : nonclass)
+         continue;
+       
+       /* If this is the kind of thing we're looking for, we're done.  */
+       if (qualify_lookup (iter->value, flags))
+         binding = iter->value;
+       else if ((flags & LOOKUP_PREFER_TYPES)
+                && qualify_lookup (iter->type, flags))
+         binding = iter->type;
+       else
+         binding = NULL_TREE;
+       
+       if (binding)
+         {
+           val = binding;
+           break;
+         }
+      }
 
   /* Now lookup in namespace scopes.  */
   if (!val)
@@ -4019,19 +4131,24 @@ lookup_name_real (tree name, int prefer_type, int nonclass,
 tree
 lookup_name_nonclass (tree name)
 {
-  return lookup_name_real (name, 0, 1, 0, LOOKUP_COMPLAIN);
+  return lookup_name_real (name, 0, 1, /*block_p=*/true, 0, LOOKUP_COMPLAIN);
 }
 
 tree
-lookup_function_nonclass (tree name, tree args)
+lookup_function_nonclass (tree name, tree args, bool block_p)
 {
-  return lookup_arg_dependent (name, lookup_name_nonclass (name), args);
+  return 
+    lookup_arg_dependent (name, 
+                         lookup_name_real (name, 0, 1, block_p, 0, 
+                                           LOOKUP_COMPLAIN),
+                         args);
 }
 
 tree
 lookup_name (tree name, int prefer_type)
 {
-  return lookup_name_real (name, prefer_type, 0, 0, LOOKUP_COMPLAIN);
+  return lookup_name_real (name, prefer_type, 0, /*block_p=*/true, 
+                          0, LOOKUP_COMPLAIN);
 }
 
 /* Similar to `lookup_name' but look only in the innermost non-class
@@ -4057,10 +4174,14 @@ lookup_name_current_level (tree name)
   else if (IDENTIFIER_BINDING (name)
           && LOCAL_BINDING_P (IDENTIFIER_BINDING (name)))
     {
+      cxx_binding *binding;
+      binding = IDENTIFIER_BINDING (name);
       while (1)
        {
-         if (IDENTIFIER_BINDING (name)->scope == b)
-           POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, IDENTIFIER_VALUE (name));
+         if (binding->scope == b
+             && !(TREE_CODE (binding->value) == VAR_DECL
+                  && DECL_DEAD_FOR_LOCAL (binding->value)))
+           POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, binding->value);
 
          if (b->kind == sk_cleanup)
            b = b->level_chain;
@@ -4277,11 +4398,17 @@ arg_assoc_class (struct arg_lookup *k, tree type)
   context = decl_namespace (TYPE_MAIN_DECL (type));
   if (arg_assoc_namespace (k, context))
     return true;
-  
-  /* Process baseclasses.  */
-  for (i = 0; i < CLASSTYPE_N_BASECLASSES (type); i++)
-    if (arg_assoc_class (k, TYPE_BINFO_BASETYPE (type, i)))
-      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;
+    }
   
   /* Process friends.  */
   for (list = DECL_FRIENDLIST (TYPE_MAIN_DECL (type)); list; 
@@ -4289,11 +4416,21 @@ arg_assoc_class (struct arg_lookup *k, tree type)
     if (k->name == FRIEND_NAME (list))
       for (friends = FRIEND_DECLS (list); friends; 
           friends = TREE_CHAIN (friends))
-       /* Only interested in global functions with potentially hidden
-           (i.e. unqualified) declarations.  */
-       if (CP_DECL_CONTEXT (TREE_VALUE (friends)) == context)
-         if (add_function (k, TREE_VALUE (friends)))
+       {
+         tree fn = TREE_VALUE (friends);
+
+         /* Only interested in global functions with potentially hidden
+            (i.e. unqualified) declarations.  */
+         if (CP_DECL_CONTEXT (fn) != context)
+           continue;
+         /* Template specializations are never found by name lookup.
+            (Templates themselves can be found, but not template
+            specializations.)  */
+         if (TREE_CODE (fn) == FUNCTION_DECL && DECL_USE_TEMPLATE (fn))
+           continue;
+         if (add_function (k, fn))
            return true;
+       }
 
   /* Process template arguments.  */
   if (CLASSTYPE_TEMPLATE_INFO (type) 
@@ -4447,10 +4584,8 @@ arg_assoc (struct arg_lookup *k, tree n)
        if (arg_assoc_template_arg (k, TREE_VEC_ELT (args, ix)) == 1)
          return true;
     }
-  else
+  else if (TREE_CODE (n) == OVERLOAD)
     {
-      my_friendly_assert (TREE_CODE (n) == OVERLOAD, 980715);
-      
       for (; n; n = OVL_CHAIN (n))
        if (arg_assoc_type (k, TREE_TYPE (OVL_FUNCTION (n))))
          return true;
@@ -4609,7 +4744,16 @@ pushtag (tree name, tree type, int globalize)
 
   timevar_push (TV_NAME_LOOKUP);
   b = current_binding_level;
-  while (b->kind == sk_cleanup
+  while (/* Cleanup scopes are not scopes from the point of view of
+           the language.  */
+        b->kind == sk_cleanup
+        /* 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
+           view of the language -- but we need a place to stash
+           things that will go in the containing namespace when the
+           template is instantiated.  */
+        || (b->kind == sk_template_parms && b->explicit_spec_p)
         || (b->kind == sk_class
             && (globalize
                 /* We may be defining a new type in the initializer
@@ -4721,22 +4865,6 @@ pushtag (tree name, tree type, int globalize)
   timevar_pop (TV_NAME_LOOKUP);
 }
 \f
-/* Allocate storage for saving a C++ binding.  */
-#define cxx_saved_binding_make() \
-  (ggc_alloc (sizeof (cxx_saved_binding)))
-
-struct cxx_saved_binding GTY(())
-{
-  /* Link that chains saved C++ bindings for a given name into a stack.  */
-  cxx_saved_binding *previous;
-  /* The name of the current binding.  */
-  tree identifier;
-  /* The binding we're saving.  */
-  cxx_binding *binding;
-  tree class_value;
-  tree real_type_value;
-};
-
 /* Subroutines for reverting temporarily to top-level for instantiation
    of templates and such.  We actually need to clear out the class- and
    local-value slots of all identifiers, so that only the global values
@@ -4744,49 +4872,63 @@ struct cxx_saved_binding GTY(())
    scope isn't enough, because more binding levels may be pushed.  */
 struct saved_scope *scope_chain;
 
-static cxx_saved_binding *
-store_bindings (tree names, cxx_saved_binding *old_bindings)
+/* If ID has not already been marked, add an appropriate binding to
+   *OLD_BINDINGS.  */
+
+static void
+store_binding (tree id, VEC(cxx_saved_binding) **old_bindings)
+{
+  cxx_saved_binding *saved;
+
+  if (!id || !IDENTIFIER_BINDING (id))
+    return;
+
+  if (IDENTIFIER_MARKED (id))
+    return;
+  
+  IDENTIFIER_MARKED (id) = 1;
+
+  saved = VEC_safe_push (cxx_saved_binding, *old_bindings, NULL);
+  saved->identifier = id;
+  saved->binding = IDENTIFIER_BINDING (id);
+  saved->real_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
+  IDENTIFIER_BINDING (id) = NULL;
+}
+
+static void
+store_bindings (tree names, VEC(cxx_saved_binding) **old_bindings)
 {
   tree t;
-  cxx_saved_binding *search_bindings = old_bindings;
 
   timevar_push (TV_NAME_LOOKUP);
   for (t = names; t; t = TREE_CHAIN (t))
     {
       tree id;
-      cxx_saved_binding *saved;
-      cxx_saved_binding *t1;
 
       if (TREE_CODE (t) == TREE_LIST)
        id = TREE_PURPOSE (t);
       else
        id = DECL_NAME (t);
 
-      if (!id
-         /* Note that we may have an IDENTIFIER_CLASS_VALUE even when
-            we have no IDENTIFIER_BINDING if we have left the class
-            scope, but cached the class-level declarations.  */
-         || !(IDENTIFIER_BINDING (id) || IDENTIFIER_CLASS_VALUE (id)))
-       continue;
-
-      for (t1 = search_bindings; t1; t1 = t1->previous)
-       if (t1->identifier == id)
-         goto skip_it;
-
-      my_friendly_assert (TREE_CODE (id) == IDENTIFIER_NODE, 135);
-      saved = cxx_saved_binding_make ();
-      saved->previous = old_bindings;
-      saved->identifier = id;
-      saved->binding = IDENTIFIER_BINDING (id);
-      saved->class_value = IDENTIFIER_CLASS_VALUE (id);;
-      saved->real_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
-      IDENTIFIER_BINDING (id) = NULL;
-      IDENTIFIER_CLASS_VALUE (id) = NULL_TREE;
-      old_bindings = saved;
-    skip_it:
-      ;
+      store_binding (id, old_bindings);
     }
-  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, old_bindings);
+  timevar_pop (TV_NAME_LOOKUP);
+}
+
+/* Like store_bindings, but NAMES is a vector of cp_class_binding
+   objects, rather than a TREE_LIST.  */
+
+static void
+store_class_bindings (VEC(cp_class_binding) *names, 
+                     VEC(cxx_saved_binding) **old_bindings)
+{
+  size_t i;
+  cp_class_binding *cb;
+
+  timevar_push (TV_NAME_LOOKUP);
+  for (i = 0; VEC_iterate(cp_class_binding, names, i, cb); ++i)
+    store_binding (cb->identifier, old_bindings);
+  timevar_pop (TV_NAME_LOOKUP);
 }
 
 void
@@ -4794,7 +4936,8 @@ push_to_top_level (void)
 {
   struct saved_scope *s;
   struct cp_binding_level *b;
-  cxx_saved_binding *old_bindings;
+  cxx_saved_binding *sb;
+  size_t i;
   int need_pop;
 
   timevar_push (TV_NAME_LOOKUP);
@@ -4811,9 +4954,9 @@ push_to_top_level (void)
   else
     need_pop = 0;
 
-  old_bindings = NULL;
-  if (scope_chain && previous_class_type)
-    old_bindings = store_bindings (previous_class_values, old_bindings);
+  if (scope_chain && previous_class_level)
+    store_class_bindings (previous_class_level->class_shadowed,
+                         &s->old_bindings);
 
   /* Have to include the global scope, because class-scope decls
      aren't listed anywhere useful.  */
@@ -4828,18 +4971,21 @@ push_to_top_level (void)
       if (global_scope_p (b))
        break;
 
-      old_bindings = store_bindings (b->names, old_bindings);
+      store_bindings (b->names, &s->old_bindings);
       /* We also need to check class_shadowed to save class-level type
         bindings, since pushclass doesn't fill in b->names.  */
       if (b->kind == sk_class)
-       old_bindings = store_bindings (b->class_shadowed, old_bindings);
+       store_class_bindings (b->class_shadowed, &s->old_bindings);
 
       /* Unwind type-value slots back to top level.  */
       for (t = b->type_shadowed; t; t = TREE_CHAIN (t))
        SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t), TREE_VALUE (t));
     }
+
+  for (i = 0; VEC_iterate (cxx_saved_binding, s->old_bindings, i, sb); ++i)
+    IDENTIFIER_MARKED (sb->identifier) = 0;
+
   s->prev = scope_chain;
-  s->old_bindings = old_bindings;
   s->bindings = b;
   s->need_pop_function_context = need_pop;
   s->function_decl = current_function_decl;
@@ -4857,21 +5003,21 @@ pop_from_top_level (void)
 {
   struct saved_scope *s = scope_chain;
   cxx_saved_binding *saved;
+  size_t i;
 
   timevar_push (TV_NAME_LOOKUP); 
   /* Clear out class-level bindings cache.  */
-  if (previous_class_type)
+  if (previous_class_level)
     invalidate_class_lookup_cache ();
 
   current_lang_base = 0;
 
   scope_chain = s->prev;
-  for (saved = s->old_bindings; saved; saved = saved->previous)
+  for (i = 0; VEC_iterate (cxx_saved_binding, s->old_bindings, i, saved); ++i)
     {
       tree id = saved->identifier;
 
       IDENTIFIER_BINDING (id) = saved->binding;
-      IDENTIFIER_CLASS_VALUE (id) = saved->class_value;
       SET_IDENTIFIER_TYPE_VALUE (id, saved->real_type_value);
     }
 
This page took 0.069085 seconds and 5 git commands to generate.