OSDN Git Service

* cp-tree.h (ICS_USER_FLAG): Remove comment about obsolete flag.
[pf3gnuchains/gcc-fork.git] / gcc / cp / pt.c
index 3125c83..0d24d06 100644 (file)
@@ -117,12 +117,9 @@ static int for_each_template_parm (tree, tree_fn_t, void*, htab_t);
 static tree build_template_parm_index (int, int, int, tree, tree);
 static int inline_needs_template_parms (tree);
 static void push_inline_template_parms_recursive (tree, int);
-static tree retrieve_specialization (tree, tree);
 static tree retrieve_local_specialization (tree);
-static tree register_specialization (tree, tree, tree);
 static void register_local_specialization (tree, tree);
 static tree reduce_template_parm_level (tree, tree, int);
-static tree build_template_decl (tree, tree);
 static int mark_template_parm (tree, void *);
 static int template_parm_this_level_p (tree, void *);
 static tree tsubst_friend_function (tree, tree);
@@ -148,7 +145,6 @@ static tree process_partial_specialization (tree);
 static void set_current_access_from_decl (tree);
 static void check_default_tmpl_args (tree, tree, int, int);
 static tree tsubst_call_declarator_parms (tree, tree, tsubst_flags_t, tree);
-static tree get_template_base_r (tree, void *);
 static tree get_template_base (tree, tree, tree, tree);
 static int verify_class_unification (tree, tree, tree);
 static tree try_class_unification (tree, tree, tree, tree);
@@ -238,7 +234,7 @@ finish_member_template_decl (tree decl)
       return NULL_TREE;
     }
   else if (TREE_CODE (decl) == FIELD_DECL)
-    error ("data member `%D' cannot be a member template", decl);
+    error ("data member %qD cannot be a member template", decl);
   else if (DECL_TEMPLATE_INFO (decl))
     {
       if (!DECL_TEMPLATE_SPECIALIZATION (decl))
@@ -250,7 +246,7 @@ finish_member_template_decl (tree decl)
        return decl;
     } 
   else
-    error ("invalid member template declaration `%D'", decl);
+    error ("invalid member template declaration %qD", decl);
 
   return error_mark_node;
 }
@@ -442,67 +438,6 @@ maybe_end_member_template_processing (void)
     }
 }
 
-/* Returns nonzero iff T is a member template function.  We must be
-   careful as in
-
-     template <class T> class C { void f(); }
-
-   Here, f is a template function, and a member, but not a member
-   template.  This function does not concern itself with the origin of
-   T, only its present state.  So if we have 
-
-     template <class T> class C { template <class U> void f(U); }
-
-   then neither C<int>::f<char> nor C<T>::f<double> is considered
-   to be a member template.  But, `template <class U> void
-   C<int>::f(U)' is considered a member template.  */
-
-int
-is_member_template (tree t)
-{
-  if (!DECL_FUNCTION_TEMPLATE_P (t))
-    /* Anything that isn't a function or a template function is
-       certainly not a member template.  */
-    return 0;
-
-  /* A local class can't have member templates.  */
-  if (decl_function_context (t))
-    return 0;
-
-  return (DECL_FUNCTION_MEMBER_P (DECL_TEMPLATE_RESULT (t))
-         /* If there are more levels of template parameters than
-            there are template classes surrounding the declaration,
-            then we have a member template.  */
-         && (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (t)) > 
-             template_class_depth (DECL_CONTEXT (t))));
-}
-
-#if 0 /* UNUSED */
-/* Returns nonzero iff T is a member template class.  See
-   is_member_template for a description of what precisely constitutes
-   a member template.  */
-
-int
-is_member_template_class (tree t)
-{
-  if (!DECL_CLASS_TEMPLATE_P (t))
-    /* Anything that isn't a class template, is certainly not a member
-       template.  */
-    return 0;
-
-  if (!DECL_CLASS_SCOPE_P (t))
-    /* Anything whose context isn't a class type is surely not a
-       member template.  */
-    return 0;
-
-  /* If there are more levels of template parameters than there are
-     template classes surrounding the declaration, then we have a
-     member template.  */
-  return  (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (t)) > 
-          template_class_depth (DECL_CONTEXT (t)));
-}
-#endif
-
 /* Return a new template argument vector which contains all of ARGS,
    but has as its innermost set of arguments the EXTRA_ARGS.  */
 
@@ -633,8 +568,7 @@ check_specialization_scope (void)
      shall be declared in the namespace of which the class template
      is a member.  */
   if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
-    error ("explicit specialization in non-namespace scope `%D'",
-             scope);
+    error ("explicit specialization in non-namespace scope %qD", scope);
 
   /* [temp.expl.spec] 
 
@@ -729,8 +663,8 @@ check_specialization_namespace (tree tmpl)
     return true;
   else
     {
-      pedwarn ("specialization of `%D' in different namespace", tmpl);
-      cp_pedwarn_at ("  from definition of `%#D'", tmpl);
+      pedwarn ("specialization of %qD in different namespace", tmpl);
+      cp_pedwarn_at ("  from definition of %q#D", tmpl);
       return false;
     }
 }
@@ -766,7 +700,7 @@ maybe_process_partial_specialization (tree type)
            push_template_decl (TYPE_MAIN_DECL (type));
        }
       else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
-       error ("specialization of `%T' after instantiation", type);
+       error ("specialization of %qT after instantiation", type);
     }
   else if (CLASS_TYPE_P (type)
           && !CLASSTYPE_USE_TEMPLATE (type)
@@ -798,8 +732,8 @@ maybe_process_partial_specialization (tree type)
          if (current_namespace
              != decl_namespace_context (CLASSTYPE_TI_TEMPLATE (type)))
            {
-             pedwarn ("specializing `%#T' in different namespace", type);
-             cp_pedwarn_at ("  from definition of `%#D'",
+             pedwarn ("specializing %q#T in different namespace", type);
+             cp_pedwarn_at ("  from definition of %q#D",
                             CLASSTYPE_TI_TEMPLATE (type));
            }
 
@@ -813,7 +747,7 @@ maybe_process_partial_specialization (tree type)
               t; t = TREE_CHAIN (t))
            if (TREE_VALUE (t) != type
                && TYPE_CONTEXT (TREE_VALUE (t)) == context)
-             error ("specialization `%T' after instantiation `%T'",
+             error ("specialization %qT after instantiation %qT",
                     type, TREE_VALUE (t));
 
          /* Mark TYPE as a specialization.  And as a result, we only
@@ -825,7 +759,34 @@ maybe_process_partial_specialization (tree type)
        }
     }
   else if (processing_specialization)
-    error ("explicit specialization of non-template `%T'", type);
+    error ("explicit specialization of non-template %qT", type);
+}
+
+/* Returns onzero if we can optimize the retrieval of specializations
+   for TMPL, a TEMPLATE_DECL.  In particular, for such a template, we
+   do not use DECL_TEMPLATE_SPECIALIZATIONS at all.  */
+
+static inline bool 
+optimize_specialization_lookup_p (tree tmpl)
+{
+  return (DECL_FUNCTION_TEMPLATE_P (tmpl)
+         && DECL_CLASS_SCOPE_P (tmpl)
+         /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
+            parameter.  */
+         && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
+         && !DECL_MEMBER_TEMPLATE_P (tmpl)
+         && !DECL_CONV_FN_P (tmpl)
+         /* It is possible to have a template that is not a member
+            template and is not a member of a template class:
+            
+            template <typename T> 
+            struct S { friend A::f(); };
+            
+            Here, the friend function is a template, but the context does
+            not have template information.  The optimized lookup relies
+            on having ARGS be the template arguments for both the class
+            and the function template.  */
+         && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
 }
 
 /* Retrieve the specialization (in the sense of [temp.spec] - a
@@ -833,25 +794,90 @@ maybe_process_partial_specialization (tree type)
    specialization) of TMPL for the given template ARGS.  If there is
    no such specialization, return NULL_TREE.  The ARGS are a vector of
    arguments, or a vector of vectors of arguments, in the case of
-   templates with more than one level of parameters.  */
+   templates with more than one level of parameters.  
+
+   If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
+   then we search for a partial specialization matching ARGS.  This
+   parameter is ignored if TMPL is not a class template.  */
    
 static tree
-retrieve_specialization (tree tmpl, tree args)
+retrieve_specialization (tree tmpl, tree args, 
+                        bool class_specializations_p)
 {
-  tree s;
-
   gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
 
   /* There should be as many levels of arguments as there are
      levels of parameters.  */
   gcc_assert (TMPL_ARGS_DEPTH (args) 
              == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)));
-                     
-  for (s = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
-       s != NULL_TREE;
-       s = TREE_CHAIN (s))
-    if (comp_template_args (TREE_PURPOSE (s), args))
-      return TREE_VALUE (s);
+               
+  if (optimize_specialization_lookup_p (tmpl))
+    {
+      tree class_template;
+      tree class_specialization;
+      VEC(tree) *methods;
+      tree fns;
+      int idx;
+
+      /* The template arguments actually apply to the containing
+        class.  Find the class specialization with those
+        arguments.  */
+      class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
+      class_specialization 
+       = retrieve_specialization (class_template, args,
+                                  /*class_specializations_p=*/false);
+      if (!class_specialization)
+       return NULL_TREE;
+      /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC
+        for the specialization.  */
+      idx = class_method_index_for_fn (class_specialization, tmpl);
+      if (idx == -1)
+       return NULL_TREE;
+      /* Iterate through the methods with the indicated name, looking
+        for the one that has an instance of TMPL.  */
+      methods = CLASSTYPE_METHOD_VEC (class_specialization);
+      for (fns = VEC_index (tree, methods, idx); fns; fns = OVL_NEXT (fns))
+       {
+         tree fn = OVL_CURRENT (fns);
+         if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl)
+           return fn;
+       }
+      return NULL_TREE;
+    }
+  else
+    {
+      tree *sp;
+      tree *head;
+
+      /* Class templates store their instantiations on the
+        DECL_TEMPLATE_INSTANTIATIONS list; other templates use the
+        DECL_TEMPLATE_SPECIALIZATIONS list.  */
+      if (!class_specializations_p
+         && TREE_CODE (DECL_TEMPLATE_RESULT (tmpl)) == TYPE_DECL)
+       sp = &DECL_TEMPLATE_INSTANTIATIONS (tmpl);
+      else
+       sp = &DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
+      head = sp;
+      /* Iterate through the list until we find a matching template.  */
+      while (*sp != NULL_TREE)
+       {
+         tree spec = *sp;
+             
+         if (comp_template_args (TREE_PURPOSE (spec), args))
+           {
+             /* Use the move-to-front heuristic to speed up future
+                searches.  */ 
+             if (spec != *head)
+               {
+                 *sp = TREE_CHAIN (*sp);
+                 TREE_CHAIN (spec) = *head;
+                 *head = spec;
+               }
+             return TREE_VALUE (spec);
+           }
+         sp = &TREE_CHAIN (spec);
+       }
+    }
 
   return NULL_TREE;
 }
@@ -1037,7 +1063,7 @@ is_specialization_of_friend (tree decl, tree friend)
 static tree
 register_specialization (tree spec, tree tmpl, tree args)
 {
-  tree s;
+  tree fn;
 
   gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
 
@@ -1061,74 +1087,67 @@ register_specialization (tree spec, tree tmpl, tree args)
   gcc_assert (TMPL_ARGS_DEPTH (args) 
              == TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)));
 
-  for (s = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
-       s != NULL_TREE;
-       s = TREE_CHAIN (s))
+  fn = retrieve_specialization (tmpl, args, 
+                               /*class_specializations_p=*/false);
+  /* We can sometimes try to re-register a specialization that we've
+     already got.  In particular, regenerate_decl_from_template calls
+     duplicate_decls which will update the specialization list.  But,
+     we'll still get called again here anyhow.  It's more convenient
+     to simply allow this than to try to prevent it.  */
+  if (fn == spec)
+    return spec;
+  else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
     {
-      tree fn = TREE_VALUE (s);
-
-      /* We can sometimes try to re-register a specialization that we've
-        already got.  In particular, regenerate_decl_from_template
-        calls duplicate_decls which will update the specialization
-        list.  But, we'll still get called again here anyhow.  It's
-        more convenient to simply allow this than to try to prevent it.  */
-      if (fn == spec)
-       return spec;
-      else if (DECL_TEMPLATE_SPECIALIZATION (spec)
-              && comp_template_args (TREE_PURPOSE (s), args))
+      if (DECL_TEMPLATE_INSTANTIATION (fn))
        {
-         if (DECL_TEMPLATE_INSTANTIATION (fn))
+         if (TREE_USED (fn) 
+             || DECL_EXPLICIT_INSTANTIATION (fn))
            {
-             if (TREE_USED (fn) 
-                 || DECL_EXPLICIT_INSTANTIATION (fn))
-               {
-                 error ("specialization of %D after instantiation",
-                        fn);
-                 return spec;
-               }
-             else
-               {
-                 /* This situation should occur only if the first
-                    specialization is an implicit instantiation, the
-                    second is an explicit specialization, and the
-                    implicit instantiation has not yet been used.
-                    That situation can occur if we have implicitly
-                    instantiated a member function and then
-                    specialized it later.
-
-                    We can also wind up here if a friend declaration
-                    that looked like an instantiation turns out to be
-                    a specialization:
-
-                      template <class T> void foo(T);
-                      class S { friend void foo<>(int) };
-                      template <> void foo(int);  
-
-                    We transform the existing DECL in place so that
-                    any pointers to it become pointers to the updated
-                    declaration.
-
-                    If there was a definition for the template, but
-                    not for the specialization, we want this to look
-                    as if there were no definition, and vice
-                    versa.  */
-                 DECL_INITIAL (fn) = NULL_TREE;
-                 duplicate_decls (spec, fn);
-                 
-                 return fn;
-               }
+             error ("specialization of %qD after instantiation",
+                    fn);
+             return spec;
            }
-         else if (DECL_TEMPLATE_SPECIALIZATION (fn))
+         else
            {
-             if (!duplicate_decls (spec, fn) && DECL_INITIAL (spec))
-               /* Dup decl failed, but this is a new definition. Set
-                  the line number so any errors match this new
-                  definition.  */
-               DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
+             /* This situation should occur only if the first
+                specialization is an implicit instantiation, the
+                second is an explicit specialization, and the
+                implicit instantiation has not yet been used.  That
+                situation can occur if we have implicitly
+                instantiated a member function and then specialized
+                it later.
+                
+                We can also wind up here if a friend declaration that
+                looked like an instantiation turns out to be a
+                specialization:
+                
+                  template <class T> void foo(T);
+                  class S { friend void foo<>(int) };
+                  template <> void foo(int);  
+                
+                We transform the existing DECL in place so that any
+                pointers to it become pointers to the updated
+                declaration.
+
+                If there was a definition for the template, but not
+                for the specialization, we want this to look as if
+                there were no definition, and vice versa.  */
+             DECL_INITIAL (fn) = NULL_TREE;
+             duplicate_decls (spec, fn);
              
              return fn;
            }
        }
+      else if (DECL_TEMPLATE_SPECIALIZATION (fn))
+       {
+         if (!duplicate_decls (spec, fn) && DECL_INITIAL (spec))
+           /* Dup decl failed, but this is a new definition. Set the
+              line number so any errors match this new
+              definition.  */
+           DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
+         
+         return fn;
+       }
     }
 
   /* A specialization must be declared in the same namespace as the
@@ -1137,8 +1156,9 @@ register_specialization (tree spec, tree tmpl, tree args)
       && !check_specialization_namespace (tmpl))
     DECL_CONTEXT (spec) = decl_namespace_context (tmpl);
 
-  DECL_TEMPLATE_SPECIALIZATIONS (tmpl)
-     = tree_cons (args, spec, DECL_TEMPLATE_SPECIALIZATIONS (tmpl));
+  if (!optimize_specialization_lookup_p (tmpl))
+    DECL_TEMPLATE_SPECIALIZATIONS (tmpl)
+      = tree_cons (args, spec, DECL_TEMPLATE_SPECIALIZATIONS (tmpl));
 
   return spec;
 }
@@ -1268,7 +1288,7 @@ determine_specialization (tree template_id,
 
   if (!is_overloaded_fn (fns))
     {
-      error ("`%D' is not a function template", fns);
+      error ("%qD is not a function template", fns);
       return error_mark_node;
     }
 
@@ -1453,7 +1473,8 @@ determine_specialization (tree template_id,
 
   if (templates == NULL_TREE && candidates == NULL_TREE)
     {
-      cp_error_at ("template-id `%D' for `%+D' does not match any template declaration",
+      cp_error_at ("template-id %qD for %q+D does not match any template "
+                   "declaration",
                   template_id, decl);
       return error_mark_node;
     }
@@ -1461,7 +1482,7 @@ determine_specialization (tree template_id,
           || (candidates && TREE_CHAIN (candidates))
           || (templates && candidates))
     {
-      cp_error_at ("ambiguous template specialization `%D' for `%+D'",
+      cp_error_at ("ambiguous template specialization %qD for %q+D",
                   template_id, decl);
       chainon (candidates, templates);
       print_candidates (candidates);
@@ -1684,8 +1705,8 @@ check_explicit_specialization (tree declarator,
              /* This case handles bogus declarations like template <>
                 template <class T> void f<int>(); */
 
-             error ("template-id `%D' in declaration of primary template",
-                       declarator);
+             error ("template-id %qD in declaration of primary template",
+                     declarator);
              return decl;
            }
        }
@@ -1711,13 +1732,12 @@ check_explicit_specialization (tree declarator,
     case tsk_excessive_parms:
     case tsk_insufficient_parms:
       if (tsk == tsk_excessive_parms)
-        error ("too many template parameter lists in declaration of `%D'",
+        error ("too many template parameter lists in declaration of %qD",
               decl);
       else if (template_header_count)
-       error("too few template parameter lists in declaration of `%D'",
-             decl);
+       error("too few template parameter lists in declaration of %qD", decl);
       else
-       error("explicit specialization of `%D' must be introduced by "
+       error("explicit specialization of %qD must be introduced by "
              "`template <>'", decl);
 
       /* Fall through.  */
@@ -1736,11 +1756,11 @@ check_explicit_specialization (tree declarator,
             template <class T> void f<int>(); */
 
          if (uses_template_parms (declarator))
-           error ("function template partial specialization `%D' "
+           error ("function template partial specialization %qD "
                   "is not allowed", declarator);
          else
-           error ("template-id `%D' in declaration of primary template",
-                     declarator);
+           error ("template-id %qD in declaration of primary template",
+                   declarator);
          return decl;
        }
 
@@ -1900,8 +1920,7 @@ check_explicit_specialization (tree declarator,
              
          if (fns == NULL_TREE) 
            {
-             error ("no member function `%D' declared in `%T'",
-                       name, ctype);
+             error ("no member function %qD declared in %qT", name, ctype);
              return error_mark_node;
            }
          else
@@ -2051,7 +2070,7 @@ maybe_check_template_type (tree type)
        ; 
       else if (template_header_count > context_depth + 1)
        /* There are two many template parameter lists.  */
-       error ("too many template parameter lists in declaration of `%T'", type); 
+       error ("too many template parameter lists in declaration of %qT", type); 
     }
 }
 
@@ -2144,8 +2163,8 @@ check_template_shadow (tree decl)
       || TEMPLATE_PARMS_FOR_INLINE (current_template_parms))
     return;
 
-  cp_error_at ("declaration of `%#D'", decl);
-  cp_error_at (" shadows template parm `%#D'", olddecl);
+  cp_error_at ("declaration of %q#D", decl);
+  cp_error_at (" shadows template parm %q#D", olddecl);
 }
 
 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
@@ -2397,14 +2416,16 @@ current_template_args (void)
 }
 
 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
-   template PARMS.  Used by push_template_decl below.  */
+   template PARMS.  If MEMBER_TEMPLATE_P is true, the new template is
+   a mebmer template.  Used by push_template_decl below.  */
 
 static tree
-build_template_decl (tree decl, tree parms)
+build_template_decl (tree decl, tree parms, bool member_template_p)
 {
   tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
   DECL_TEMPLATE_PARMS (tmpl) = parms;
   DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
+  DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
   if (DECL_LANG_SPECIFIC (decl))
     {
       DECL_STATIC_FUNCTION_P (tmpl) = DECL_STATIC_FUNCTION_P (decl);
@@ -2549,8 +2570,7 @@ process_partial_specialization (tree decl)
            did_error_intro = 1;
          }
 
-       error ("        `%D'", 
-                 TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
+       error ("        %qD", TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
       }
 
   /* [temp.class.spec]
@@ -2561,7 +2581,7 @@ process_partial_specialization (tree decl)
       (inner_args, 
        INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE
                                                   (maintmpl)))))
-    error ("partial specialization `%T' does not specialize any template arguments", type);
+    error ("partial specialization %qT does not specialize any template arguments", type);
 
   /* [temp.class.spec]
 
@@ -2586,7 +2606,7 @@ process_partial_specialization (tree decl)
          && TREE_CODE (arg) != TEMPLATE_PARM_INDEX)
        {
          if (tpd.arg_uses_template_parms[i])
-           error ("template argument `%E' involves template parameter(s)", arg);
+           error ("template argument %qE involves template parameter(s)", arg);
          else 
            {
              /* Look at the corresponding template parameter,
@@ -2630,9 +2650,10 @@ process_partial_specialization (tree decl)
                    if (tpd2.parms[j] != 0
                        && tpd.arg_uses_template_parms [j])
                      {
-                       error ("type `%T' of template argument `%E' depends on template parameter(s)", 
-                                 type,
-                                 arg);
+                       error ("type %qT of template argument %qE depends "
+                               "on template parameter(s)", 
+                               type,
+                               arg);
                        break;
                      }
                }
@@ -2640,7 +2661,8 @@ process_partial_specialization (tree decl)
        }
     }
 
-  if (retrieve_specialization (maintmpl, specargs))
+  if (retrieve_specialization (maintmpl, specargs, 
+                              /*class_specializations_p=*/true))
     /* We've already got this specialization.  */
     return decl;
 
@@ -2715,7 +2737,7 @@ check_default_tmpl_args (tree decl, tree parms, int is_primary, int is_partial)
            seen_def_arg_p = 1;
          else if (seen_def_arg_p)
            {
-             error ("no default argument for `%D'", TREE_VALUE (parm));
+             error ("no default argument for %qD", TREE_VALUE (parm));
              /* For better subsequent error-recovery, we indicate that
                 there should have been a default argument.  */
              TREE_PURPOSE (parm) = error_mark_node;
@@ -2749,7 +2771,7 @@ check_default_tmpl_args (tree decl, tree parms, int is_primary, int is_partial)
   else if (is_partial)
     msg = "default template arguments may not be used in partial specializations";
   else
-    msg = "default argument for template parameter for class enclosing `%D'";
+    msg = "default argument for template parameter for class enclosing %qD";
 
   if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
     /* If we're inside a class definition, there's no need to
@@ -2791,7 +2813,7 @@ check_default_tmpl_args (tree decl, tree parms, int is_primary, int is_partial)
       /* At this point, if we're still interested in issuing messages,
         they must apply to classes surrounding the object declared.  */
       if (msg)
-       msg = "default argument for template parameter for class enclosing `%D'"; 
+       msg = "default argument for template parameter for class enclosing %qD"; 
     }
 }
 
@@ -2830,6 +2852,9 @@ push_template_decl_real (tree decl, int is_friend)
   int primary;
   int is_partial;
   int new_template_p = 0;
+  /* True if the template is a member template, in the sense of
+     [temp.mem].  */
+  bool member_template_p = false;
 
   if (decl == error_mark_node)
     return decl;
@@ -2866,6 +2891,8 @@ push_template_decl_real (tree decl, int is_friend)
 
   if (primary)
     {
+      if (DECL_CLASS_SCOPE_P (decl))
+       member_template_p = true;
       if (current_lang_name == lang_name_c)
        error ("template with C linkage");
       else if (TREE_CODE (decl) == TYPE_DECL 
@@ -2878,7 +2905,7 @@ push_template_decl_real (tree decl, int is_friend)
              /* [temp.mem]
                 
                 A destructor shall not be a member template.  */
-             error ("destructor `%D' declared as member template", decl);
+             error ("destructor %qD declared as member template", decl);
              return error_mark_node;
            }
          if (NEW_DELETE_OPNAME_P (DECL_NAME (decl))
@@ -2893,7 +2920,7 @@ push_template_decl_real (tree decl, int is_friend)
                 An allocation function can be a function
                 template. ... Template allocation functions shall
                 have two or more parameters.  */
-             error ("invalid template declaration of `%D'", decl);
+             error ("invalid template declaration of %qD", decl);
              return decl;
            }
        }
@@ -2903,7 +2930,7 @@ push_template_decl_real (tree decl, int is_friend)
        /* OK */;
       else
        {
-         error ("template declaration of `%#D'", decl);
+         error ("template declaration of %q#D", decl);
          return error_mark_node;
        }
     }
@@ -2945,7 +2972,8 @@ push_template_decl_real (tree decl, int is_friend)
        }
       else
        {
-         tmpl = build_template_decl (decl, current_template_parms);
+         tmpl = build_template_decl (decl, current_template_parms,
+                                     member_template_p);
          new_template_p = 1;
 
          if (DECL_LANG_SPECIFIC (decl)
@@ -2973,13 +3001,13 @@ push_template_decl_real (tree decl, int is_friend)
            tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
          else
            {
-             error ("`%D' does not declare a template type", decl);
+             error ("%qD does not declare a template type", decl);
              return decl;
            }
        }
       else if (!DECL_LANG_SPECIFIC (decl) || !DECL_TEMPLATE_INFO (decl))
        {
-         error ("template definition of non-template `%#D'", decl);
+         error ("template definition of non-template %q#D", decl);
          return decl;
        }
       else
@@ -2988,7 +3016,7 @@ push_template_decl_real (tree decl, int is_friend)
       if (DECL_FUNCTION_TEMPLATE_P (tmpl)
          && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl) 
          && DECL_TEMPLATE_SPECIALIZATION (decl)
-         && is_member_template (tmpl))
+         && DECL_MEMBER_TEMPLATE_P (tmpl))
        {
          tree new_tmpl;
 
@@ -3000,7 +3028,8 @@ push_template_decl_real (tree decl, int is_friend)
          args = DECL_TI_ARGS (decl);
 
          new_tmpl 
-           = build_template_decl (decl, current_template_parms);
+           = build_template_decl (decl, current_template_parms,
+                                  member_template_p);
          DECL_TEMPLATE_RESULT (new_tmpl) = decl;
          TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
          DECL_TI_TEMPLATE (decl) = new_tmpl;
@@ -3020,8 +3049,8 @@ push_template_decl_real (tree decl, int is_friend)
       i = TMPL_PARMS_DEPTH (parms);
       if (TMPL_ARGS_DEPTH (args) != i)
        {
-         error ("expected %d levels of template parms for `%#D', got %d",
-                   i, decl, TMPL_ARGS_DEPTH (args));
+         error ("expected %d levels of template parms for %q#D, got %d",
+                 i, decl, TMPL_ARGS_DEPTH (args));
        }
       else
        for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
@@ -3032,11 +3061,11 @@ push_template_decl_real (tree decl, int is_friend)
            if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
              {
                if (current == decl)
-                 error ("got %d template parameters for `%#D'",
-                           TREE_VEC_LENGTH (a), decl);
+                 error ("got %d template parameters for %q#D",
+                         TREE_VEC_LENGTH (a), decl);
                else
-                 error ("got %d template parameters for `%#T'",
-                           TREE_VEC_LENGTH (a), current);
+                 error ("got %d template parameters for %q#T",
+                         TREE_VEC_LENGTH (a), current);
                error ("  but %d required", TREE_VEC_LENGTH (t));
              }
 
@@ -3122,7 +3151,7 @@ redeclare_class_template (tree type, tree parms)
 
   if (!TYPE_TEMPLATE_INFO (type))
     {
-      error ("`%T' is not a template type", type);
+      error ("%qT is not a template type", type);
       return;
     }
 
@@ -3138,7 +3167,7 @@ redeclare_class_template (tree type, tree parms)
 
   if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
     {
-      cp_error_at ("previous declaration `%D'", tmpl);
+      cp_error_at ("previous declaration %qD", tmpl);
       error ("used %d template parameter%s instead of %d",
                TREE_VEC_LENGTH (tmpl_parms), 
                TREE_VEC_LENGTH (tmpl_parms) == 1 ? "" : "s",
@@ -3159,8 +3188,8 @@ redeclare_class_template (tree type, tree parms)
          || (TREE_CODE (tmpl_parm) != TYPE_DECL
              && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm))))
        {
-         cp_error_at ("template parameter `%#D'", tmpl_parm);
-         error ("redeclared here as `%#D'", parm);
+         cp_error_at ("template parameter %q#D", tmpl_parm);
+         error ("redeclared here as %q#D", parm);
          return;
        }
 
@@ -3170,7 +3199,7 @@ redeclare_class_template (tree type, tree parms)
 
             A template-parameter may not be given default arguments
             by two different declarations in the same scope.  */
-         error ("redefinition of default argument for `%#D'", parm);
+         error ("redefinition of default argument for %q#D", parm);
          error ("%J  original definition appeared here", tmpl_parm);
          return;
        }
@@ -3317,7 +3346,7 @@ convert_nontype_argument (tree type, tree expr)
          if (TREE_CODE (e) != ADDR_EXPR)
            {
            bad_argument:
-             error ("`%E' is not a valid template argument", expr);
+             error ("%qE is not a valid template argument", expr);
              if (TYPE_PTR_P (expr_type))
                {
                  if (TREE_CODE (TREE_TYPE (expr_type)) == FUNCTION_TYPE)
@@ -3326,7 +3355,7 @@ convert_nontype_argument (tree type, tree expr)
                    error ("it must be the address of an object with external linkage");
                }
              else if (TYPE_PTR_TO_MEMBER_P (expr_type))
-               error ("it must be a pointer-to-member of the form `&X::Y'");
+               error ("it must be a pointer-to-member of the form %<&X::Y%>");
 
              return NULL_TREE;
            }
@@ -3337,8 +3366,9 @@ convert_nontype_argument (tree type, tree expr)
 
       if (TREE_CODE (referent) == STRING_CST)
        {
-         error ("string literal %E is not a valid template argument because it is the address of an object with static linkage", 
-                   referent);
+         error ("string literal %qE is not a valid template argument "
+                 "because it is the address of an object with static linkage", 
+                 referent);
          return NULL_TREE;
        }
 
@@ -3352,7 +3382,8 @@ convert_nontype_argument (tree type, tree expr)
        goto bad_argument;
       else if (!DECL_EXTERNAL_LINKAGE_P (referent))
        {
-         error ("address of non-extern `%E' cannot be used as template argument", referent); 
+         error ("address of non-extern %qE cannot be used as "
+                 "template argument", referent); 
          return error_mark_node;
        }
     }
@@ -3361,26 +3392,27 @@ convert_nontype_argument (tree type, tree expr)
       if (! TREE_CONSTANT (expr))
        {
        non_constant:
-         error ("non-constant `%E' cannot be used as template argument",
-                   expr);
+         error ("non-constant %qE cannot be used as template argument", expr);
          return NULL_TREE;
        }
     }
   else 
     {
       if (TYPE_P (expr))
-        error ("type '%T' cannot be used as a value for a non-type "
+         error ("type %qT cannot be used as a value for a non-type "
                "template-parameter", expr);
       else if (DECL_P (expr))
-        error ("invalid use of '%D' as a non-type template-argument", expr);
+        error ("invalid use of %qD as a non-type template-argument", expr);
       else
-        error ("invalid use of '%E' as a non-type template-argument", expr);
+        error ("invalid use of %qE as a non-type template-argument", expr);
 
       return NULL_TREE;
     }
 
   switch (TREE_CODE (type))
     {
+      HOST_WIDE_INT saved_processing_template_decl;
+
     case INTEGER_TYPE:
     case BOOLEAN_TYPE:
     case ENUMERAL_TYPE:
@@ -3398,8 +3430,12 @@ convert_nontype_argument (tree type, tree expr)
          return error_mark_node;
 
       /* It's safe to call digest_init in this case; we know we're
-        just converting one integral constant expression to another.  */
+        just converting one integral constant expression to another.
+        */
+      saved_processing_template_decl = processing_template_decl;
+      processing_template_decl = 0;
       expr = digest_init (type, expr, (tree*) 0);
+      processing_template_decl = saved_processing_template_decl;
 
       if (TREE_CODE (expr) != INTEGER_CST)
        /* Curiously, some TREE_CONSTANT integral expressions do not
@@ -3746,7 +3782,8 @@ convert_template_argument (tree parm,
   if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
       && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
     {
-      pedwarn ("to refer to a type member of a template parameter, use `typename %E'", arg);
+      pedwarn ("to refer to a type member of a template parameter, "
+               "use %<typename %E%>", arg);
       
       arg = make_typename_type (TREE_OPERAND (arg, 0),
                                TREE_OPERAND (arg, 1),
@@ -3759,16 +3796,17 @@ convert_template_argument (tree parm,
        {
          if (complain & tf_error)
            {
-             error ("type/value mismatch at argument %d in template parameter list for `%D'",
-                       i + 1, in_decl);
+             error ("type/value mismatch at argument %d in template "
+                     "parameter list for %qD",
+                     i + 1, in_decl);
              if (is_type)
-               error ("  expected a constant of type `%T', got `%T'",
-                         TREE_TYPE (parm),
-                         (is_tmpl_type ? DECL_NAME (arg) : arg));
+               error ("  expected a constant of type %qT, got %qT",
+                       TREE_TYPE (parm),
+                       (is_tmpl_type ? DECL_NAME (arg) : arg));
              else if (requires_tmpl_type)
-               error ("  expected a class template, got `%E'", arg);
+               error ("  expected a class template, got %qE", arg);
              else
-               error ("  expected a type, got `%E'", arg);
+               error ("  expected a type, got %qE", arg);
            }
        }
       return error_mark_node;
@@ -3777,12 +3815,13 @@ convert_template_argument (tree parm,
     {
       if (in_decl && (complain & tf_error))
        {
-         error ("type/value mismatch at argument %d in template parameter list for `%D'",
-                   i + 1, in_decl);
+         error ("type/value mismatch at argument %d in template "
+                 "parameter list for %qD",
+                 i + 1, in_decl);
          if (is_tmpl_type)
-           error ("  expected a type, got `%T'", DECL_NAME (arg));
+           error ("  expected a type, got %qT", DECL_NAME (arg));
          else
-           error ("  expected a class template, got `%T'", arg);
+           error ("  expected a class template, got %qT", arg);
        }
       return error_mark_node;
     }
@@ -3816,9 +3855,11 @@ convert_template_argument (tree parm,
                {
                  if (in_decl && (complain & tf_error))
                    {
-                     error ("type/value mismatch at argument %d in template parameter list for `%D'",
-                               i + 1, in_decl);
-                     error ("  expected a template of type `%D', got `%D'", parm, arg);
+                     error ("type/value mismatch at argument %d in "
+                             "template parameter list for %qD",
+                             i + 1, in_decl);
+                     error ("  expected a template of type %qD, got %qD",
+                             parm, arg);
                    }
                  
                  val = error_mark_node;
@@ -3853,8 +3894,7 @@ convert_template_argument (tree parm,
       if (val == NULL_TREE)
        val = error_mark_node;
       else if (val == error_mark_node && (complain & tf_error))
-       error ("could not convert template argument `%E' to `%T'", 
-                 arg, t);
+       error ("could not convert template argument %qE to %qT",  arg, t);
     }
 
   return val;
@@ -3894,10 +3934,10 @@ coerce_template_parms (tree parms,
       if (complain & tf_error) 
        {
          error ("wrong number of template arguments (%d, should be %d)",
-                   nargs, nparms);
+                 nargs, nparms);
          
          if (in_decl)
-           cp_error_at ("provided for `%D'", in_decl);
+           cp_error_at ("provided for %qD", in_decl);
        }
 
       return error_mark_node;
@@ -4269,7 +4309,7 @@ lookup_template_class (tree d1,
   if (! template)
     {
       if (complain & tf_error)
-        error ("`%T' is not a template", d1);
+        error ("%qT is not a template", d1);
       POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
     }
 
@@ -4281,9 +4321,9 @@ lookup_template_class (tree d1,
     {
       if (complain & tf_error)
         {
-          error ("non-template type `%T' used as a template", d1);
+          error ("non-template type %qT used as a template", d1);
           if (in_decl)
-           cp_error_at ("for template declaration `%D'", in_decl);
+           cp_error_at ("for template declaration %qD", in_decl);
        }
       POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
     }
@@ -4335,7 +4375,6 @@ lookup_template_class (tree d1,
       tree gen_tmpl;
       tree type_decl;
       tree found = NULL_TREE;
-      tree *tp;
       int arg_depth;
       int parm_depth;
       int is_partial_instantiation;
@@ -4459,22 +4498,11 @@ lookup_template_class (tree d1,
       if (found)
         POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found);
 
-      for (tp = &DECL_TEMPLATE_INSTANTIATIONS (template);
-          *tp;
-          tp = &TREE_CHAIN (*tp))
-       if (comp_template_args (TREE_PURPOSE (*tp), arglist))
-         {
-           found = *tp;
-
-           /* Use the move-to-front heuristic to speed up future
-              searches.  */
-           *tp = TREE_CHAIN (*tp);
-           TREE_CHAIN (found) 
-             = DECL_TEMPLATE_INSTANTIATIONS (template);
-           DECL_TEMPLATE_INSTANTIATIONS (template) = found;
-
-           POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, TREE_VALUE (found));
-         }
+      /* If we already have this specialization, return it.  */
+      found = retrieve_specialization (template, arglist,
+                                      /*class_specializations_p=*/false);
+      if (found)
+       POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found);
 
       /* This type is a "partial instantiation" if any of the template
         arguments still involve template parameters.  Note that we set
@@ -4932,7 +4960,8 @@ push_tinst_level (tree d)
        return 0;
 
       last_template_error_tick = tinst_level_tick;
-      error ("template instantiation depth exceeds maximum of %d (use -ftemplate-depth-NN to increase the maximum) instantiating `%D'",
+      error ("template instantiation depth exceeds maximum of %d (use "
+             "-ftemplate-depth-NN to increase the maximum) instantiating %qD",
             max_tinst_depth, d);
 
       print_instantiation_context ();
@@ -5370,7 +5399,7 @@ instantiate_class_template (tree type)
   if (t == error_mark_node)
     {
       const char *str = "candidates are:";
-      error ("ambiguous class template instantiation for `%#T'", type);
+      error ("ambiguous class template instantiation for %q#T", type);
       for (t = DECL_TEMPLATE_SPECIALIZATIONS (template); t; 
           t = TREE_CHAIN (t))
        {
@@ -6061,7 +6090,8 @@ tsubst_decl (tree t, tree args, tree type, tsubst_flags_t complain)
               changed.  */
            gcc_assert (full_args != tmpl_args);
 
-           spec = retrieve_specialization (t, full_args);
+           spec = retrieve_specialization (t, full_args,
+                                           /*class_specializations_p=*/true);
            if (spec != NULL_TREE)
              {
                r = spec;
@@ -6173,7 +6203,8 @@ tsubst_decl (tree t, tree args, tree type, tsubst_flags_t complain)
                                           args, complain, in_decl); 
 
            /* Check to see if we already have this specialization.  */
-           spec = retrieve_specialization (gen_tmpl, argvec);
+           spec = retrieve_specialization (gen_tmpl, argvec,
+                                           /*class_specializations_p=*/false);
 
            if (spec)
              {
@@ -6397,7 +6428,7 @@ tsubst_decl (tree t, tree args, tree type, tsubst_flags_t complain)
                                        complain, in_decl);
        TREE_CHAIN (r) = NULL_TREE;
        if (VOID_TYPE_P (type)) 
-         cp_error_at ("instantiation of `%D' as type `%T'", r, type);
+         cp_error_at ("instantiation of %qD as type %qT", r, type);
       }
       break;
 
@@ -6459,7 +6490,8 @@ tsubst_decl (tree t, tree args, tree type, tsubst_flags_t complain)
            tmpl = DECL_TI_TEMPLATE (t);
            gen_tmpl = most_general_template (tmpl);
            argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
-           spec = retrieve_specialization (gen_tmpl, argvec);
+           spec = retrieve_specialization (gen_tmpl, argvec,
+                                           /*class_specializations_p=*/false);
          }
        else
          spec = retrieve_local_specialization (t);
@@ -6517,7 +6549,7 @@ tsubst_decl (tree t, tree args, tree type, tsubst_flags_t complain)
 
        TREE_CHAIN (r) = NULL_TREE;
        if (TREE_CODE (r) == VAR_DECL && VOID_TYPE_P (type))
-         cp_error_at ("instantiation of `%D' as type `%T'", r, type);
+         cp_error_at ("instantiation of %qD as type %qT", r, type);
        /* Compute the size, alignment, etc. of R.  */
        layout_decl (r, 0);
       }
@@ -6559,9 +6591,9 @@ tsubst_arg_types (tree arg_types,
     {
       if (complain & tf_error)
         {
-          error ("invalid parameter type `%T'", type);
+          error ("invalid parameter type %qT", type);
           if (in_decl)
-            cp_error_at ("in declaration `%D'", in_decl);
+            cp_error_at ("in declaration %qD", in_decl);
         }
       return error_mark_node;
     }
@@ -6635,7 +6667,7 @@ tsubst_function_type (tree t,
             -- Attempting to create "pointer to member of T" when T
             is not a class type.  */
          if (complain & tf_error)
-           error ("creating pointer to member function of non-class type `%T'",
+           error ("creating pointer to member function of non-class type %qT",
                      r);
          return error_mark_node;
        }
@@ -6649,6 +6681,39 @@ tsubst_function_type (tree t,
   return fntype;  
 }
 
+/* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE.  Substitute the template
+   ARGS into that specification, and return the substituted
+   specification.  If there is no specification, return NULL_TREE.  */
+
+static tree
+tsubst_exception_specification (tree fntype, 
+                               tree args, 
+                               tsubst_flags_t complain,
+                               tree in_decl)
+{
+  tree specs;
+  tree new_specs;
+
+  specs = TYPE_RAISES_EXCEPTIONS (fntype);
+  new_specs = NULL_TREE;
+  if (specs)
+    {
+      if (! TREE_VALUE (specs))
+       new_specs = specs;
+      else
+       while (specs)
+         {
+           tree spec;
+           spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
+           if (spec == error_mark_node)
+             return spec;
+           new_specs = add_exception_specifier (new_specs, spec, complain);
+           specs = TREE_CHAIN (specs);
+         }
+    }
+  return new_specs;
+}
+
 /* Substitute into the PARMS of a call-declarator.  */
 
 static tree
@@ -6786,7 +6851,7 @@ tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
                 Attempting to create an array with a size that is
                 zero or negative.  */
            if (complain & tf_error)
-             error ("creating array with size zero (`%E')", max);
+             error ("creating array with size zero (%qE)", max);
 
            return error_mark_node;
          }
@@ -7005,9 +7070,9 @@ tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
                if (TREE_CODE (type) == VOID_TYPE)
                  error ("forming reference to void");
                else
-                 error ("forming %s to reference type `%T'",
-                           (code == POINTER_TYPE) ? "pointer" : "reference",
-                           type);
+                 error ("forming %s to reference type %qT",
+                         (code == POINTER_TYPE) ? "pointer" : "reference",
+                         type);
                last_loc = input_location;
              }
 
@@ -7042,13 +7107,13 @@ tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
               -- Attempting to create "pointer to member of T" when T
                  is not a class type.  */
            if (complain & tf_error)
-             error ("creating pointer to member of non-class type `%T'", r);
+             error ("creating pointer to member of non-class type %qT", r);
            return error_mark_node;
          }
        if (TREE_CODE (type) == REFERENCE_TYPE)
          {
            if (complain & tf_error)
-             error ("creating pointer to member reference type `%T'", type);
+             error ("creating pointer to member reference type %qT", type);
            
            return error_mark_node;
          }
@@ -7081,32 +7146,16 @@ tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
     case METHOD_TYPE:
       {
        tree fntype;
-       tree raises;
-
+       tree specs;
        fntype = tsubst_function_type (t, args, complain, in_decl);
        if (fntype == error_mark_node)
          return error_mark_node;
 
        /* Substitute the exception specification.  */
-       raises = TYPE_RAISES_EXCEPTIONS (t);
-       if (raises)
-         {
-           tree   list = NULL_TREE;
-           
-           if (! TREE_VALUE (raises))
-             list = raises;
-           else
-             for (; raises != NULL_TREE; raises = TREE_CHAIN (raises))
-               {
-                 tree spec = TREE_VALUE (raises);
-                 
-                 spec = tsubst (spec, args, complain, in_decl);
-                 if (spec == error_mark_node)
-                   return spec;
-                 list = add_exception_specifier (list, spec, complain);
-               }
-           fntype = build_exception_variant (fntype, list);
-         }
+       specs = tsubst_exception_specification (t, args, complain, 
+                                               in_decl);
+       if (specs)
+         fntype = build_exception_variant (fntype, specs);
        return fntype;
       }
     case ARRAY_TYPE:
@@ -7134,13 +7183,13 @@ tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
            || TREE_CODE (type) == REFERENCE_TYPE)
          {
            if (complain & tf_error)
-             error ("creating array of `%T'", type);
+             error ("creating array of %qT", type);
            return error_mark_node;
          }
        if (CLASS_TYPE_P (type) && CLASSTYPE_PURE_VIRTUALS (type))
          {
            if (complain & tf_error)
-             error ("creating array of `%T', which is an abstract class type", 
+             error ("creating array of %qT, which is an abstract class type", 
                     type);
            return error_mark_node;         
          }
@@ -7184,8 +7233,7 @@ tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
        if (!IS_AGGR_TYPE (ctx))
          {
            if (complain & tf_error)
-             error ("`%T' is not a class, struct, or union type",
-                       ctx);
+             error ("%qT is not a class, struct, or union type", ctx);
            return error_mark_node;
          }
        else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
@@ -7396,9 +7444,9 @@ tsubst_qualified_id (tree qualified_id, tree args,
        {
          if (complain & tf_error)
            {
-             error ("dependent-name `%E' is parsed as a non-type, but "
+             error ("dependent-name %qE is parsed as a non-type, but "
                     "instantiation yields a type", qualified_id);
-             inform ("say `typename %E' if a type is meant", qualified_id);
+             inform ("say %<typename %E%> if a type is meant", qualified_id);
            }
          return error_mark_node;
        }
@@ -7530,7 +7578,7 @@ tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
       if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
        return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)), 
                       args, complain, in_decl);
-      else if (is_member_template (t))
+      else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
        return tsubst (t, args, complain, in_decl);
       else if (DECL_CLASS_SCOPE_P (t)
               && uses_template_parms (DECL_CONTEXT (t)))
@@ -8535,10 +8583,10 @@ tsubst_copy_and_build (tree t,
            if (complain & tf_error)
              {
                if (TYPE_P (TREE_OPERAND (member, 0)))
-                 error ("`%T' is not a class or namespace", 
+                 error ("%qT is not a class or namespace", 
                         TREE_OPERAND (member, 0));
                else
-                 error ("`%D' is not a class or namespace", 
+                 error ("%qD is not a class or namespace", 
                         TREE_OPERAND (member, 0));
              }
            return error_mark_node;
@@ -8663,9 +8711,9 @@ check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
              if (!(complain & tf_error))
                /*OK*/;
              else if (TYPE_ANONYMOUS_P (nt))
-               error ("`%T' uses anonymous type", t);
+               error ("%qT uses anonymous type", t);
              else
-               error ("`%T' uses local type `%T'", t, nt);
+               error ("%qT uses local type %qT", t, nt);
              result = true;
            }
          /* In order to avoid all sorts of complications, we do not
@@ -8673,7 +8721,7 @@ check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
          else if (variably_modified_type_p (t, NULL_TREE))
            {
              if (complain & tf_error)
-               error ("`%T' is a variably modified type", t);
+               error ("%qT is a variably modified type", t);
              result = true;
            }
        }
@@ -8684,12 +8732,12 @@ check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
               && !TREE_CONSTANT (t))
        {
          if (complain & tf_error)
-           error ("integral expression `%E' is not constant", t);
+           error ("integral expression %qE is not constant", t);
          result = true;
        }
     }
   if (result && complain & tf_error)
-    error ("  trying to instantiate `%D'", tmpl);
+    error ("  trying to instantiate %qD", tmpl);
   return result;
 }
 
@@ -8729,7 +8777,8 @@ instantiate_template (tree tmpl, tree targ_ptr, tsubst_flags_t complain)
     }
   
   /* Check to see if we already have this specialization.  */
-  spec = retrieve_specialization (tmpl, targ_ptr);
+  spec = retrieve_specialization (tmpl, targ_ptr, 
+                                 /*class_specializations_p=*/false);
   if (spec != NULL_TREE)
     return spec;
 
@@ -8743,7 +8792,8 @@ instantiate_template (tree tmpl, tree targ_ptr, tsubst_flags_t complain)
                                              targ_ptr);
 
       /* Check to see if we already have this specialization.  */
-      spec = retrieve_specialization (gen_tmpl, targ_ptr);
+      spec = retrieve_specialization (gen_tmpl, targ_ptr,
+                                     /*class_specializations_p=*/false);
       if (spec != NULL_TREE)
        return spec;
     }
@@ -9469,30 +9519,31 @@ try_class_unification (tree tparms, tree targs, tree parm, tree arg)
   return arg;
 }
 
-typedef struct get_template_base_data_s 
-{
-  /* Parameters for unification.  */
-  tree tparms;
-  tree targs;
-  tree parm;
-  /* Base we've found to be satisfactory.  */
-  tree rval;
-} get_template_base_data;
-
-/* Called from get_template_base via dfs_walk.  */
+/* Given a template type PARM and a class type ARG, find the unique
+   base type in ARG that is an instance of PARM.  We do not examine
+   ARG itself; only its base-classes.  If there is not exactly one
+   appropriate base class, return NULL_TREE.  PARM may be the type of
+   a partial specialization, as well as a plain template type.  Used
+   by unify.  */
 
 static tree
-get_template_base_r (tree arg_binfo,
-                    void *data_)
+get_template_base (tree tparms, tree targs, tree parm, tree arg)
 {
-  get_template_base_data *data = data_;
+  tree rval = NULL_TREE;
+  tree binfo;
 
-  /* Do not look at the most derived binfo -- that's not a proper
-     base.  */
-  if (BINFO_INHERITANCE_CHAIN (arg_binfo))
+  gcc_assert (IS_AGGR_TYPE_CODE (TREE_CODE (arg)));
+  
+  binfo = TYPE_BINFO (complete_type (arg));
+  if (!binfo)
+    /* The type could not be completed.  */
+    return NULL_TREE;
+
+  /* Walk in inheritance graph order.  The search order is not
+     important, and this avoids multiple walks of virtual bases.  */
+  for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
     {
-      tree r = try_class_unification (data->tparms, data->targs,
-                                     data->parm, BINFO_TYPE (arg_binfo));
+      tree r = try_class_unification (tparms, targs, parm, BINFO_TYPE (binfo));
 
       if (r)
        {
@@ -9504,48 +9555,14 @@ get_template_base_r (tree arg_binfo,
              deduction fails.
 
             applies.  */
-         if (data->rval && !same_type_p (r, data->rval))
-           {
-             data->rval = NULL_TREE;
-             /* Terminate the walk with any non-NULL value.  */
-             return r;
-           }
+         if (rval && !same_type_p (r, rval))
+           return NULL_TREE;
          
-         data->rval = r;
+         rval = r;
        }
     }
 
-  return NULL_TREE;
-}
-
-/* Given a template type PARM and a class type ARG, find the unique
-   base type in ARG that is an instance of PARM.  We do not examine
-   ARG itself; only its base-classes.  If there is no appropriate base
-   class, return NULL_TREE.  If there is more than one, return
-   error_mark_node.  PARM may be the type of a partial specialization,
-   as well as a plain template type.  Used by unify.  */
-
-static tree
-get_template_base (tree tparms, tree targs, tree parm, tree arg)
-{
-  get_template_base_data data;
-  tree arg_binfo;
-
-  gcc_assert (IS_AGGR_TYPE_CODE (TREE_CODE (arg)));
-  
-  arg_binfo = TYPE_BINFO (complete_type (arg));
-  if (!arg_binfo)
-    /* The type could not be completed.  */
-    return NULL_TREE;
-
-  data.tparms = tparms;
-  data.targs = targs;
-  data.parm = parm;
-  data.rval = NULL_TREE;
-
-  dfs_walk_real (arg_binfo, get_template_base_r, 0, 0, &data);
-
-  return data.rval;
+  return rval;
 }
 
 /* Returns the level of DECL, which declares a template parameter.  */
@@ -10554,7 +10571,7 @@ do_decl_instantiation (tree decl, tree storage)
     return;
   else if (! DECL_LANG_SPECIFIC (decl))
     {
-      error ("explicit instantiation of non-template `%#D'", decl);
+      error ("explicit instantiation of non-template %q#D", decl);
       return;
     }
   else if (TREE_CODE (decl) == VAR_DECL)
@@ -10570,13 +10587,13 @@ do_decl_instantiation (tree decl, tree storage)
       result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
       if (!result || TREE_CODE (result) != VAR_DECL)
        {
-         error ("no matching template for `%D' found", decl);
+         error ("no matching template for %qD found", decl);
          return;
        }
     }
   else if (TREE_CODE (decl) != FUNCTION_DECL)
     {
-      error ("explicit instantiation of `%#D'", decl);
+      error ("explicit instantiation of %q#D", decl);
       return;
     }
   else
@@ -10612,7 +10629,7 @@ do_decl_instantiation (tree decl, tree storage)
         the first instantiation was `extern' and the second is not,
         and EXTERN_P for the opposite case.  */
       if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
-       pedwarn ("duplicate explicit instantiation of `%#D'", result);
+       pedwarn ("duplicate explicit instantiation of %q#D", result);
       /* If an "extern" explicit instantiation follows an ordinary
         explicit instantiation, the template is instantiated.  */
       if (extern_p)
@@ -10620,12 +10637,12 @@ do_decl_instantiation (tree decl, tree storage)
     }
   else if (!DECL_IMPLICIT_INSTANTIATION (result))
     {
-      error ("no matching template for `%D' found", result);
+      error ("no matching template for %qD found", result);
       return;
     }
   else if (!DECL_TEMPLATE_INFO (result))
     {
-      pedwarn ("explicit instantiation of non-template `%#D'", result);
+      pedwarn ("explicit instantiation of non-template %q#D", result);
       return;
     }
 
@@ -10634,12 +10651,12 @@ do_decl_instantiation (tree decl, tree storage)
   else if (storage == ridpointers[(int) RID_EXTERN])
     {
       if (pedantic && !in_system_header)
-       pedwarn ("ISO C++ forbids the use of `extern' on explicit instantiations");
+       pedwarn ("ISO C++ forbids the use of %<extern%> on explicit "
+                 "instantiations");
       extern_p = 1;
     }
   else
-    error ("storage class `%D' applied to template instantiation",
-             storage);
+    error ("storage class %qD applied to template instantiation", storage);
 
   mark_decl_instantiated (result, extern_p);
   if (! extern_p)
@@ -10701,7 +10718,7 @@ do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
 
   if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
     {
-      error ("explicit instantiation of non-template type `%T'", t);
+      error ("explicit instantiation of non-template type %qT", t);
       return;
     }
 
@@ -10710,8 +10727,8 @@ do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
   if (!COMPLETE_TYPE_P (t))
     {
       if (complain & tf_error)
-       error ("explicit instantiation of `%#T' before definition of template",
-                 t);
+       error ("explicit instantiation of %q#T before definition of template",
+               t);
       return;
     }
 
@@ -10729,8 +10746,8 @@ do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
        static_p = 1;
       else
        {
-         error ("storage class `%D' applied to template instantiation",
-                   storage);
+         error ("storage class %qD applied to template instantiation",
+                 storage);
          extern_p = 0;
        }
     }
@@ -10763,7 +10780,7 @@ do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
 
       if (!previous_instantiation_extern_p && !extern_p
          && (complain & tf_error))
-       pedwarn ("duplicate explicit instantiation of `%#T'", t);
+       pedwarn ("duplicate explicit instantiation of %q#T", t);
       
       /* If we've already instantiated the template, just return now.  */
       if (!CLASSTYPE_INTERFACE_ONLY (t))
@@ -10834,75 +10851,76 @@ do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
 static void
 regenerate_decl_from_template (tree decl, tree tmpl)
 {
-  /* The most general version of TMPL.  */
-  tree gen_tmpl;
   /* The arguments used to instantiate DECL, from the most general
      template.  */
   tree args;
   tree code_pattern;
-  tree new_decl;
-  bool unregistered;
 
   args = DECL_TI_ARGS (decl);
   code_pattern = DECL_TEMPLATE_RESULT (tmpl);
 
-  /* Unregister the specialization so that when we tsubst we will not
-     just return DECL.  We don't have to unregister DECL from TMPL
-     because if would only be registered there if it were a partial
-     instantiation of a specialization, which it isn't: it's a full
-     instantiation.  */
-  gen_tmpl = most_general_template (tmpl);
-  unregistered = reregister_specialization (decl, gen_tmpl,
-                                           /*new_spec=*/NULL_TREE);
-
-  /* If the DECL was not unregistered then something peculiar is
-     happening: we created a specialization but did not call
-     register_specialization for it.  */
-  gcc_assert (unregistered);
-
   /* Make sure that we can see identifiers, and compute access
      correctly.  */
   push_access_scope (decl);
 
-  /* Do the substitution to get the new declaration.  */
-  new_decl = tsubst (code_pattern, args, tf_error, NULL_TREE);
+  if (TREE_CODE (decl) == FUNCTION_DECL)
+    {
+      tree decl_parm;
+      tree pattern_parm;
+      tree specs;
+      int args_depth;
+      int parms_depth;
+      
+      args_depth = TMPL_ARGS_DEPTH (args);
+      parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)); 
+      if (args_depth > parms_depth)
+       args = get_innermost_template_args (args, parms_depth);
+
+      specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
+                                             args, tf_error, NULL_TREE);
+      if (specs)
+       TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
+                                                   specs);
+
+      /* Merge parameter declarations.  */
+      decl_parm = skip_artificial_parms_for (decl, 
+                                            DECL_ARGUMENTS (decl));
+      pattern_parm 
+       = skip_artificial_parms_for (code_pattern,
+                                    DECL_ARGUMENTS (code_pattern));
+      while (decl_parm)
+       {
+         tree parm_type;
+
+         if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
+           DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
+         parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
+                             NULL_TREE);
+         if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
+           TREE_TYPE (decl_parm) = parm_type;
+         decl_parm = TREE_CHAIN (decl_parm);
+         pattern_parm = TREE_CHAIN (pattern_parm);
+       }
 
-  if (TREE_CODE (decl) == VAR_DECL)
+      /* Merge additional specifiers from the CODE_PATTERN.  */
+      if (DECL_DECLARED_INLINE_P (code_pattern)
+         && !DECL_DECLARED_INLINE_P (decl))
+       DECL_DECLARED_INLINE_P (decl) = 1;
+      if (DECL_INLINE (code_pattern) && !DECL_INLINE (decl))
+       DECL_INLINE (decl) = 1;
+    }
+  else if (TREE_CODE (decl) == VAR_DECL)
     {
-      /* Set up DECL_INITIAL, since tsubst doesn't.  */
-      if (!DECL_INITIALIZED_IN_CLASS_P (decl))
-       DECL_INITIAL (new_decl) = 
+      if (!DECL_INITIALIZED_IN_CLASS_P (decl)
+         && DECL_INITIAL (code_pattern))
+       DECL_INITIAL (decl) = 
          tsubst_expr (DECL_INITIAL (code_pattern), args, 
                       tf_error, DECL_TI_TEMPLATE (decl));
     }
-  else if (TREE_CODE (decl) == FUNCTION_DECL)
-    {
-      /* Convince duplicate_decls to use the DECL_ARGUMENTS from the
-        new decl.  */ 
-      DECL_INITIAL (new_decl) = error_mark_node;
-      /* And don't complain about a duplicate definition.  */
-      DECL_INITIAL (decl) = NULL_TREE;
-    }
+  else
+    gcc_unreachable ();
 
   pop_access_scope (decl);
-
-  /* The immediate parent of the new template is still whatever it was
-     before, even though tsubst sets DECL_TI_TEMPLATE up as the most
-     general template.  We also reset the DECL_ASSEMBLER_NAME since
-     tsubst always calculates the name as if the function in question
-     were really a template instance, and sometimes, with friend
-     functions, this is not so.  See tsubst_friend_function for
-     details.  */
-  DECL_TI_TEMPLATE (new_decl) = DECL_TI_TEMPLATE (decl);
-  COPY_DECL_ASSEMBLER_NAME (decl, new_decl);
-  COPY_DECL_RTL (decl, new_decl);
-  DECL_USE_TEMPLATE (new_decl) = DECL_USE_TEMPLATE (decl);
-
-  /* Call duplicate decls to merge the old and new declarations.  */
-  duplicate_decls (new_decl, decl);
-
-  /* Now, re-register the specialization.  */
-  register_specialization (decl, gen_tmpl, args);
 }
 
 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
@@ -11020,7 +11038,8 @@ instantiate_decl (tree d, int defer_ok, int undefined_ok)
      DECL_TEMPLATE_SPECIALIZATION.  */
   gen_tmpl = most_general_template (tmpl);
   gen_args = DECL_TI_ARGS (d);
-  spec = retrieve_specialization (gen_tmpl, gen_args);
+  spec = retrieve_specialization (gen_tmpl, gen_args,
+                                 /*class_specializations_p=*/false);
   if (spec != NULL_TREE && DECL_TEMPLATE_SPECIALIZATION (spec))
     return spec;
 
@@ -11126,7 +11145,7 @@ instantiate_decl (tree d, int defer_ok, int undefined_ok)
           shall be present in every translation unit in which it is
           explicitly instantiated.  */
        pedwarn
-         ("explicit instantiation of `%D' but no definition available", d);
+         ("explicit instantiation of %qD but no definition available", d);
 
       add_pending_template (d);
       goto out;
@@ -11272,7 +11291,7 @@ instantiate_pending_templates (int retries)
     {
       cp_error_at ("template instantiation depth exceeds maximum of %d"
                   " (use -ftemplate-depth-NN to increase the maximum)"
-                  " instantiating `%+D', possibly from virtual table"
+                  " instantiating %q+D, possibly from virtual table"
                   " generation",
                   max_tinst_depth, TREE_VALUE (pending_templates));
       return;
@@ -11549,8 +11568,7 @@ invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
     return 0;
            
   if (complain & tf_error)
-    error ("`%#T' is not a valid type for a template constant parameter",
-              type);
+    error ("%q#T is not a valid type for a template constant parameter", type);
   return 1;
 }