OSDN Git Service

Fix candidate for PR c++/43206
[pf3gnuchains/gcc-fork.git] / gcc / cp / pt.c
index 2f0fa12..bb8749b 100644 (file)
@@ -1,6 +1,6 @@
 /* Handle parameterized types (templates) for GNU C++.
    Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
-   2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009
+   2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010
    Free Software Foundation, Inc.
    Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
    Rewritten by Jason Merrill (jason@cygnus.com).
@@ -32,6 +32,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "tm.h"
 #include "obstack.h"
 #include "tree.h"
+#include "intl.h"
 #include "pointer-set.h"
 #include "flags.h"
 #include "c-common.h"
@@ -118,8 +119,6 @@ static int try_one_overload (tree, tree, tree, tree, tree,
                             unification_kind_t, int, bool);
 static int unify (tree, tree, tree, tree, int);
 static void add_pending_template (tree);
-static int push_tinst_level (tree);
-static void pop_tinst_level (void);
 static tree reopen_tinst_level (struct tinst_level *);
 static tree tsubst_initializer_list (tree, tree);
 static tree get_class_bindings (tree, tree, tree);
@@ -189,9 +188,9 @@ static tree tsubst_copy     (tree, tree, tsubst_flags_t, tree);
 static tree tsubst_pack_expansion (tree, tree, tsubst_flags_t, tree);
 static tree tsubst_decl (tree, tree, tsubst_flags_t);
 static void perform_typedefs_access_check (tree tmpl, tree targs);
-static void append_type_to_template_for_access_check_1 (tree, tree, tree);
+static void append_type_to_template_for_access_check_1 (tree, tree, tree,
+                                                       location_t);
 static hashval_t iterative_hash_template_arg (tree arg, hashval_t val);
-static bool primary_template_instantiation_p (const_tree);
 static tree listify (tree);
 static tree listify_autos (tree, tree);
 
@@ -287,6 +286,17 @@ finish_member_template_decl (tree decl)
   return error_mark_node;
 }
 
+/* Create a template info node.  */
+
+tree
+build_template_info (tree template_decl, tree template_args)
+{
+  tree result = make_node (TEMPLATE_INFO);
+  TI_TEMPLATE (result) = template_decl;
+  TI_ARGS (result) = template_args;
+  return result;
+}
+
 /* Return the template info node corresponding to T, whatever T is.  */
 
 tree
@@ -305,6 +315,8 @@ get_template_info (const_tree t)
 
   if (TAGGED_TYPE_P (t))
     tinfo = TYPE_TEMPLATE_INFO (t);
+  else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
+    tinfo = TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t);
 
   return tinfo;
 }
@@ -481,6 +493,9 @@ add_to_template_args (tree args, tree extra_args)
   int i;
   int j;
 
+  if (args == NULL_TREE)
+    return extra_args;
+
   extra_depth = TMPL_ARGS_DEPTH (extra_args);
   new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
 
@@ -730,6 +745,12 @@ check_specialization_namespace (tree tmpl)
      function, member class or static data member of a class template
      shall be declared in the namespace of which the class template is
      a member.  */
+  if (current_scope() != DECL_CONTEXT (tmpl)
+      && !at_namespace_scope_p ())
+    {
+      error ("specialization of %qD must appear at namespace scope", tmpl);
+      return false;
+    }
   if (is_associated_namespace (current_namespace, tpl_ns))
     /* Same or super-using namespace.  */
     return true;
@@ -1445,6 +1466,11 @@ iterative_hash_template_arg (tree arg, hashval_t val)
   if (!TYPE_P (arg))
     STRIP_NOPS (arg);
 
+  if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
+    /* We can get one of these when re-hashing a previous entry in the middle
+       of substituting into a pack expansion.  Just look through it.  */
+    arg = ARGUMENT_PACK_SELECT_FROM_PACK (arg);
+
   code = TREE_CODE (arg);
   tclass = TREE_CODE_CLASS (code);
 
@@ -1470,11 +1496,6 @@ iterative_hash_template_arg (tree arg, hashval_t val)
     case EXPR_PACK_EXPANSION:
       return iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
 
-    case ARGUMENT_PACK_SELECT:
-      /* We can get one of these when re-hashing a previous entry in the middle
-         of substituting into a pack expansion.  Just look through it...  */
-      arg = ARGUMENT_PACK_SELECT_FROM_PACK (arg);
-      /* ...and fall through.  */
     case TYPE_ARGUMENT_PACK:
     case NONTYPE_ARGUMENT_PACK:
       return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
@@ -1535,6 +1556,19 @@ iterative_hash_template_arg (tree arg, hashval_t val)
       val = iterative_hash_object (code, val);
       return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
 
+    case ARRAY_TYPE:
+      /* layout_type sets structural equality for arrays of
+        incomplete type, so we can't rely on the canonical type
+        for hashing.  */
+      val = iterative_hash_template_arg (TREE_TYPE (arg), val);
+      return iterative_hash_template_arg (TYPE_DOMAIN (arg), val);
+
+    case LAMBDA_EXPR:
+      /* A lambda can't appear in a template arg, but don't crash on
+        erroneous input.  */
+      gcc_assert (errorcount > 0);
+      return val;
+
     default:
       switch (tclass)
        {
@@ -1635,32 +1669,65 @@ explicit_class_specialization_p (tree type)
   return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
 }
 
-/* Print the list of candidate FNS in an error message.  */
+/* Print the list of functions at FNS, going through all the overloads
+   for each element of the list.  Alternatively, FNS can not be a
+   TREE_LIST, in which case it will be printed together with all the
+   overloads.
 
-void
-print_candidates (tree fns)
+   MORE and *STR should respectively be FALSE and NULL when the function
+   is called from the outside.  They are used internally on recursive
+   calls.  print_candidates manages the two parameters and leaves NULL
+   in *STR when it ends.  */
+
+static void
+print_candidates_1 (tree fns, bool more, const char **str)
 {
-  tree fn;
-  tree f;
+  tree fn, fn2;
+  char *spaces = NULL;
 
-  const char *str = "candidates are:";
+  for (fn = fns; fn; fn = OVL_NEXT (fn))
+    if (TREE_CODE (fn) == TREE_LIST)
+      {
+        gcc_assert (!OVL_NEXT (fn) && !is_overloaded_fn (fn));
+        for (fn2 = fn; fn2 != NULL_TREE; fn2 = TREE_CHAIN (fn2))
+          print_candidates_1 (TREE_VALUE (fn2),
+                              TREE_CHAIN (fn2) || more, str);
+      }
+    else
+      {
+        if (!*str)
+          {
+            /* Pick the prefix string.  */
+            if (!more && !OVL_NEXT (fns))
+              {
+                error ("candidate is: %+#D", OVL_CURRENT (fn));
+                continue;
+              }
 
-  if (is_overloaded_fn (fns))
-    {
-      for (f = fns; f; f = OVL_NEXT (f))
-       {
-         error ("%s %+#D", str, OVL_CURRENT (f));
-         str = "               ";
-       }
-    }
-  else for (fn = fns; fn != NULL_TREE; fn = TREE_CHAIN (fn))
+            *str = _("candidates are:");
+            spaces = get_spaces (*str);
+          }
+        error ("%s %+#D", *str, OVL_CURRENT (fn));
+        *str = spaces ? spaces : *str;
+      }
+
+  if (!more)
     {
-      for (f = TREE_VALUE (fn); f; f = OVL_NEXT (f))
-       error ("%s %+#D", str, OVL_CURRENT (f));
-      str = "               ";
+      free (spaces);
+      *str = NULL;
     }
 }
 
+/* Print the list of candidate FNS in an error message.  */
+
+void
+print_candidates (tree fns)
+{
+  const char *str = NULL;
+  print_candidates_1 (fns, false, &str);
+  gcc_assert (str == NULL);
+}
+
 /* Returns the template (one of the functions given by TEMPLATE_ID)
    which can be specialized to match the indicated DECL with the
    explicit template args given in TEMPLATE_ID.  The DECL may be
@@ -1934,6 +2001,10 @@ determine_specialization (tree template_id,
     {
       error ("template-id %qD for %q+D does not match any template "
             "declaration", template_id, decl);
+      if (header_count && header_count != template_count + 1)
+       inform (input_location, "saw %d %<template<>%>, need %d for "
+               "specializing a member function template",
+               header_count, template_count + 1);
       return error_mark_node;
     }
   else if ((templates && TREE_CHAIN (templates))
@@ -1942,7 +2013,7 @@ determine_specialization (tree template_id,
     {
       error ("ambiguous template specialization %qD for %q+D",
             template_id, decl);
-      chainon (candidates, templates);
+      candidates = chainon (candidates, templates);
       print_candidates (candidates);
       return error_mark_node;
     }
@@ -2493,7 +2564,7 @@ check_explicit_specialization (tree declarator,
            }
 
          /* Set up the DECL_TEMPLATE_INFO for DECL.  */
-         DECL_TEMPLATE_INFO (decl) = tree_cons (tmpl, targs, NULL_TREE);
+         DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
 
          /* Inherit default function arguments from the template
             DECL is specializing.  */
@@ -2681,15 +2752,13 @@ get_function_template_decl (const_tree primary_func_tmpl_inst)
 bool
 function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
 {
-    if (DECL_ARTIFICIAL (param_decl)
-       || !function_parameter_pack_p (pack))
-      return false;
-
-    gcc_assert (DECL_NAME (param_decl) && DECL_NAME (pack));
+  if (DECL_ARTIFICIAL (param_decl)
+      || !function_parameter_pack_p (pack))
+    return false;
 
-    /* The parameter pack and its pack arguments have the same
-       DECL_PARM_INDEX.  */
-    return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
+  /* The parameter pack and its pack arguments have the same
+     DECL_PARM_INDEX.  */
+  return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
 }
 
 /* Determine whether ARGS describes a variadic template args list,
@@ -2739,7 +2808,7 @@ make_ith_pack_parameter_name (tree name, int i)
 /* Return true if T is a primary function
    or class template instantiation.  */
 
-static bool
+bool
 primary_template_instantiation_p (const_tree t)
 {
   if (!t)
@@ -2779,6 +2848,25 @@ get_primary_template_innermost_parameters (const_tree t)
   return parms;
 }
 
+/* Return the template parameters of the LEVELth level from the full list
+   of template parameters PARMS.  */
+
+tree
+get_template_parms_at_level (tree parms, unsigned level)
+{
+  tree p;
+  if (!parms
+      || TREE_CODE (parms) != TREE_LIST
+      || level > TMPL_PARMS_DEPTH (parms))
+    return NULL_TREE;
+
+  for (p = parms; p; p = TREE_CHAIN (p))
+    if (TMPL_PARMS_DEPTH (p) == level)
+      return p;
+
+  return NULL_TREE;
+}
+
 /* Returns the template arguments of T if T is a template instantiation,
    NULL otherwise.  */
 
@@ -2901,7 +2989,7 @@ find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
     case UNION_TYPE:
     case ENUMERAL_TYPE:
       if (TYPE_TEMPLATE_INFO (t))
-       cp_walk_tree (&TREE_VALUE (TYPE_TEMPLATE_INFO (t)), 
+       cp_walk_tree (&TI_ARGS (TYPE_TEMPLATE_INFO (t)),
                      &find_parameter_packs_r, ppd, ppd->visited);
 
       *walk_subtrees = 0;
@@ -3145,12 +3233,15 @@ expand_template_argument_pack (tree args)
   tree result_args = NULL_TREE;
   int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
   int num_result_args = -1;
+  int non_default_args_count = -1;
 
   /* First, determine if we need to expand anything, and the number of
      slots we'll need.  */
   for (in_arg = 0; in_arg < nargs; ++in_arg)
     {
       tree arg = TREE_VEC_ELT (args, in_arg);
+      if (arg == NULL_TREE)
+       return args;
       if (ARGUMENT_PACK_P (arg))
         {
           int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
@@ -3172,6 +3263,9 @@ expand_template_argument_pack (tree args)
 
   /* Expand arguments.  */
   result_args = make_tree_vec (num_result_args);
+  if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
+    non_default_args_count =
+      GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
   for (in_arg = 0; in_arg < nargs; ++in_arg)
     {
       tree arg = TREE_VEC_ELT (args, in_arg);
@@ -3181,6 +3275,8 @@ expand_template_argument_pack (tree args)
           int i, num_packed = TREE_VEC_LENGTH (packed);
           for (i = 0; i < num_packed; ++i, ++out_arg)
             TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
+         if (non_default_args_count > 0)
+           non_default_args_count += num_packed;
         }
       else
         {
@@ -3188,7 +3284,8 @@ expand_template_argument_pack (tree args)
           ++out_arg;
         }
     }
-
+  if (non_default_args_count >= 0)
+    SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
   return result_args;
 }
 
@@ -3301,7 +3398,8 @@ reduce_template_parm_level (tree index, tree type, int levels, tree args,
 {
   if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
       || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
-         != TEMPLATE_PARM_LEVEL (index) - levels))
+         != TEMPLATE_PARM_LEVEL (index) - levels)
+      || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
     {
       tree orig_decl = TEMPLATE_PARM_DECL (index);
       tree decl, t;
@@ -3479,6 +3577,9 @@ end_template_parm_list (tree parms)
       next = TREE_CHAIN (parm);
       TREE_VEC_ELT (saved_parmlist, nparms) = parm;
       TREE_CHAIN (parm) = NULL_TREE;
+      if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL)
+       TEMPLATE_TYPE_PARM_SIBLING_PARMS (TREE_TYPE (TREE_VALUE (parm))) =
+             current_template_parms;
     }
 
   --processing_template_parmlist;
@@ -3551,6 +3652,10 @@ current_template_args (void)
                           /* Turn this argument into a TYPE_ARGUMENT_PACK
                              with a single element, which expands T.  */
                           tree vec = make_tree_vec (1);
+#ifdef ENABLE_CHECKING
+                         SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
+                               (vec, TREE_VEC_LENGTH (vec));
+#endif
                           TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
                           
                           t = cxx_make_type (TYPE_ARGUMENT_PACK);
@@ -3567,6 +3672,10 @@ current_template_args (void)
                              with a single element, which expands T.  */
                           tree vec = make_tree_vec (1);
                           tree type = TREE_TYPE (TEMPLATE_PARM_DECL (t));
+#ifdef ENABLE_CHECKING
+                         SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
+                               (vec, TREE_VEC_LENGTH (vec));
+#endif
                           TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
                           
                           t  = make_node (NONTYPE_ARGUMENT_PACK);
@@ -3579,6 +3688,10 @@ current_template_args (void)
            }
        }
 
+#ifdef ENABLE_CHECKING
+      SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
+#endif
+
       if (length > 1)
        TREE_VEC_ELT (args, --l) = a;
       else
@@ -3588,6 +3701,57 @@ current_template_args (void)
   return args;
 }
 
+/* Update the declared TYPE by doing any lookups which were thought to be
+   dependent, but are not now that we know the SCOPE of the declarator.  */
+
+tree
+maybe_update_decl_type (tree orig_type, tree scope)
+{
+  tree type = orig_type;
+
+  if (type == NULL_TREE)
+    return type;
+
+  if (TREE_CODE (orig_type) == TYPE_DECL)
+    type = TREE_TYPE (type);
+
+  if (scope && TYPE_P (scope) && dependent_type_p (scope)
+      && dependent_type_p (type)
+      /* Don't bother building up the args in this case.  */
+      && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
+    {
+      /* tsubst in the args corresponding to the template parameters,
+        including auto if present.  Most things will be unchanged, but
+        make_typename_type and tsubst_qualified_id will resolve
+        TYPENAME_TYPEs and SCOPE_REFs that were previously dependent.  */
+      tree args = current_template_args ();
+      tree auto_node = type_uses_auto (type);
+      tree pushed;
+      if (auto_node)
+       {
+         tree auto_vec = make_tree_vec (1);
+         TREE_VEC_ELT (auto_vec, 0) = auto_node;
+         args = add_to_template_args (args, auto_vec);
+       }
+      pushed = push_scope (scope);
+      type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
+      if (pushed)
+       pop_scope (scope);
+    }
+
+  if (type == error_mark_node)
+    return orig_type;
+
+  if (TREE_CODE (orig_type) == TYPE_DECL)
+    {
+      if (same_type_p (type, TREE_TYPE (orig_type)))
+       type = orig_type;
+      else
+       type = TYPE_NAME (type);
+    }
+  return type;
+}
+
 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
    template PARMS.  If MEMBER_TEMPLATE_P is true, the new template is
    a member template.  Used by push_template_decl below.  */
@@ -3673,7 +3837,7 @@ process_partial_specialization (tree decl)
   int nargs = TREE_VEC_LENGTH (inner_args);
   int ntparms;
   int  i;
-  int did_error_intro = 0;
+  bool did_error_intro = false;
   struct template_parm_data tpd;
   struct template_parm_data tpd2;
 
@@ -3735,12 +3899,15 @@ process_partial_specialization (tree decl)
        if (!did_error_intro)
          {
            error ("template parameters not used in partial specialization:");
-           did_error_intro = 1;
+           did_error_intro = true;
          }
 
        error ("        %qD", TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
       }
 
+  if (did_error_intro)
+    return error_mark_node;
+
   /* [temp.class.spec]
 
      The argument list of the specialization shall not be identical to
@@ -3794,12 +3961,11 @@ process_partial_specialization (tree decl)
                   || (!packed_args && i < nargs - 1))
                 {
                   if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
-                    error ("parameter pack argument %qE must be at the end of the template argument list", arg);
+                    error ("parameter pack argument %qE must be at the "
+                          "end of the template argument list", arg);
                   else
-                    error ("parameter pack argument %qT must be at the end of the template argument list", arg);
-
-                 if (packed_args)
-                   TREE_VEC_ELT (packed_args, j) = error_mark_node;
+                    error ("parameter pack argument %qT must be at the "
+                          "end of the template argument list", arg);
                 }
             }
 
@@ -3857,16 +4023,19 @@ process_partial_specialization (tree decl)
                          If they are fully specialized in the
                          specialization, that's OK.  */
                       int j;
+                      int count = 0;
                       for (j = 0; j < nargs; ++j)
                         if (tpd2.parms[j] != 0
                             && tpd.arg_uses_template_parms [j])
-                          {
-                            error ("type %qT of template argument %qE depends "
-                                   "on template parameter(s)", 
-                                   type,
-                                   arg);
-                            break;
-                          }
+                          ++count;
+                      if (count != 0)
+                        error_n (input_location, count,
+                                 "type %qT of template argument %qE depends "
+                                 "on a template parameter",
+                                 "type %qT of template argument %qE depends "
+                                 "on template parameters",
+                                 type,
+                                 arg);
                     }
                 }
             }
@@ -4029,16 +4198,19 @@ check_default_tmpl_args (tree decl, tree parms, int is_primary,
 
   /* Figure out what error message to issue.  */
   if (is_friend_decl == 2)
-    msg = "default template arguments may not be used in function template friend re-declaration";
+    msg = G_("default template arguments may not be used in function template "
+            "friend re-declaration");
   else if (is_friend_decl)
-    msg = "default template arguments may not be used in function template friend declarations";
+    msg = G_("default template arguments may not be used in function template "
+            "friend declarations");
   else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
-    msg = ("default template arguments may not be used in function templates "
-          "without -std=c++0x or -std=gnu++0x");
+    msg = G_("default template arguments may not be used in function templates "
+            "without -std=c++0x or -std=gnu++0x");
   else if (is_partial)
-    msg = "default template arguments may not be used in partial specializations";
+    msg = G_("default template arguments may not be used in "
+            "partial specializations");
   else
-    msg = "default argument for template parameter for class enclosing %qD";
+    msg = G_("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
@@ -4089,7 +4261,8 @@ check_default_tmpl_args (tree decl, tree parms, int is_primary,
       /* 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 %qD";
+       msg = G_("default argument for template parameter for class "
+                "enclosing %qD");
     }
 
   return no_errors;
@@ -4351,7 +4524,7 @@ push_template_decl_real (tree decl, bool is_friend)
          DECL_TI_TEMPLATE (decl) = new_tmpl;
          SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
          DECL_TEMPLATE_INFO (new_tmpl)
-           = tree_cons (tmpl, args, NULL_TREE);
+           = build_template_info (tmpl, args);
 
          register_specialization (new_tmpl,
                                   most_general_template (tmpl),
@@ -4389,6 +4562,9 @@ push_template_decl_real (tree decl, bool is_friend)
 
            if (current == decl)
              current = ctx;
+           else if (current == NULL_TREE)
+             /* Can happen in erroneous input.  */
+             break;
            else
              current = (TYPE_P (current)
                         ? TYPE_CONTEXT (current)
@@ -4470,7 +4646,7 @@ template arguments to %qD do not match original template %qD",
   if (DECL_TEMPLATE_INFO (tmpl))
     args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
 
-  info = tree_cons (tmpl, args, NULL_TREE);
+  info = build_template_info (tmpl, args);
 
   if (DECL_IMPLICIT_TYPEDEF_P (decl))
     SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
@@ -4524,10 +4700,14 @@ redeclare_class_template (tree type, tree parms)
 
   if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
     {
-      error ("redeclared with %d template parameter(s)", 
-             TREE_VEC_LENGTH (parms));
-      inform (input_location, "previous declaration %q+D used %d template parameter(s)", 
-             tmpl, TREE_VEC_LENGTH (tmpl_parms));
+      error_n (input_location, TREE_VEC_LENGTH (parms),
+               "redeclared with %d template parameter",
+               "redeclared with %d template parameters",
+               TREE_VEC_LENGTH (parms));
+      inform_n (input_location, TREE_VEC_LENGTH (tmpl_parms),
+                "previous declaration %q+D used %d template parameter",
+                "previous declaration %q+D used %d template parameters",
+                tmpl, TREE_VEC_LENGTH (tmpl_parms));
       return false;
     }
 
@@ -4543,22 +4723,24 @@ redeclare_class_template (tree type, tree parms)
         continue;
 
       tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
+      if (tmpl_parm == error_mark_node)
+       return false;
+
       parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
       tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
       parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
 
       /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
         TEMPLATE_DECL.  */
-      if (tmpl_parm != error_mark_node
-         && (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
-             || (TREE_CODE (tmpl_parm) != TYPE_DECL
-                 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
-             || (TREE_CODE (tmpl_parm) != PARM_DECL
-                 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
-                     != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
-             || (TREE_CODE (tmpl_parm) == PARM_DECL
-                 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
-                     != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))))))
+      if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
+         || (TREE_CODE (tmpl_parm) != TYPE_DECL
+             && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
+         || (TREE_CODE (tmpl_parm) != PARM_DECL
+             && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
+                 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
+         || (TREE_CODE (tmpl_parm) == PARM_DECL
+             && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
+                 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
        {
          error ("template parameter %q+#D", tmpl_parm);
          error ("redeclared here as %q#D", parm);
@@ -4686,6 +4868,22 @@ convert_nontype_argument_function (tree type, tree expr)
   return fn;
 }
 
+/* Subroutine of convert_nontype_argument.
+   Check if EXPR of type TYPE is a valid pointer-to-member constant.
+   Emit an error otherwise.  */
+
+static bool
+check_valid_ptrmem_cst_expr (tree type, tree expr)
+{
+  STRIP_NOPS (expr);
+  if (expr && (null_ptr_cst_p (expr) || TREE_CODE (expr) == PTRMEM_CST))
+    return true;
+  error ("%qE is not a valid template argument for type %qT",
+        expr, type);
+  error ("it must be a pointer-to-member of the form `&X::Y'");
+  return false;
+}
+
 /* Attempt to convert the non-type template parameter EXPR to the
    indicated TYPE.  If the conversion is successful, return the
    converted value.  If the conversion is unsuccessful, return
@@ -4735,6 +4933,7 @@ convert_nontype_argument (tree type, tree expr)
   if (error_operand_p (expr))
     return error_mark_node;
   expr_type = TREE_TYPE (expr);
+  expr = mark_rvalue_use (expr);
 
   /* HACK: Due to double coercion, we can get a
      NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
@@ -4912,6 +5111,27 @@ convert_nontype_argument (tree type, tree expr)
         shall be one of: [...]
 
         -- the address of an object or function with external linkage.  */
+      if (TREE_CODE (expr) == INDIRECT_REF
+         && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
+       {
+         expr = TREE_OPERAND (expr, 0);
+         if (DECL_P (expr))
+           {
+             error ("%q#D is not a valid template argument for type %qT "
+                    "because a reference variable does not have a constant "
+                    "address", expr, type);
+             return NULL_TREE;
+           }
+       }
+
+      if (!DECL_P (expr))
+       {
+         error ("%qE is not a valid template argument for type %qT "
+                "because it is not an object with external linkage",
+                expr, type);
+         return NULL_TREE;
+       }
+
       if (!DECL_EXTERNAL_LINKAGE_P (expr))
        {
          error ("%qE is not a valid template argument for type %qT "
@@ -4985,6 +5205,11 @@ convert_nontype_argument (tree type, tree expr)
       if (expr == error_mark_node)
        return error_mark_node;
 
+      /* [temp.arg.nontype] bullet 1 says the pointer to member
+         expression must be a pointer-to-member constant.  */
+      if (!check_valid_ptrmem_cst_expr (type, expr))
+       return error_mark_node;
+
       /* There is no way to disable standard conversions in
         resolve_address_of_overloaded_function (called by
         instantiate_type). It is possible that the call succeeded by
@@ -4996,12 +5221,13 @@ convert_nontype_argument (tree type, tree expr)
         provide a superior diagnostic.  */
       if (!same_type_p (TREE_TYPE (expr), type))
        {
-         /* Make sure we are just one standard conversion off.  */
-         gcc_assert (can_convert (type, TREE_TYPE (expr)));
          error ("%qE is not a valid template argument for type %qT "
                 "because it is of type %qT", expr, type,
                 TREE_TYPE (expr));
-         inform (input_location, "standard conversions are not allowed in this context");
+         /* If we are just one standard conversion off, explain.  */
+         if (can_convert (type, TREE_TYPE (expr)))
+           inform (input_location,
+                   "standard conversions are not allowed in this context");
          return NULL_TREE;
        }
     }
@@ -5011,6 +5237,11 @@ convert_nontype_argument (tree type, tree expr)
      qualification conversions (_conv.qual_) are applied.  */
   else if (TYPE_PTRMEM_P (type))
     {
+      /* [temp.arg.nontype] bullet 1 says the pointer to member
+         expression must be a pointer-to-member constant.  */
+      if (!check_valid_ptrmem_cst_expr (type, expr))
+       return error_mark_node;
+
       expr = perform_qualification_conversions (type, expr);
       if (expr == error_mark_node)
        return expr;
@@ -5322,6 +5553,22 @@ convert_template_argument (tree parm,
   if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
     arg = PACK_EXPANSION_PATTERN (arg);
 
+  /* Deal with an injected-class-name used as a template template arg.  */
+  if (requires_tmpl_type && CLASS_TYPE_P (arg))
+    {
+      tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
+      if (TREE_CODE (t) == TEMPLATE_DECL)
+       {
+         if (complain & tf_warning_or_error)
+           pedwarn (input_location, OPT_pedantic, "injected-class-name %qD"
+                    " used as template template argument", TYPE_NAME (arg));
+         else if (flag_pedantic_errors)
+           t = arg;
+
+         arg = t;
+       }
+    }
+
   is_tmpl_type = 
     ((TREE_CODE (arg) == TEMPLATE_DECL
       && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
@@ -5403,7 +5650,7 @@ convert_template_argument (tree parm,
                                                  complain, in_decl,
                                                  args))
                {
-                 val = orig_arg;
+                 val = arg;
 
                  /* TEMPLATE_TEMPLATE_PARM node is preferred over
                     TEMPLATE_DECL.  */
@@ -5411,12 +5658,8 @@ convert_template_argument (tree parm,
                     {
                       if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
                         val = TREE_TYPE (val);
-                      else if (TREE_CODE (val) == TYPE_PACK_EXPANSION
-                               && DECL_TEMPLATE_TEMPLATE_PARM_P (arg))
-                        {
-                          val = TREE_TYPE (arg);
-                          val = make_pack_expansion (val);
-                        }
+                     if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
+                       val = make_pack_expansion (val);
                     }
                }
              else
@@ -5426,7 +5669,7 @@ convert_template_argument (tree parm,
                      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",
+                     error ("  expected a template of type %qD, got %qT",
                             parm, orig_arg);
                    }
 
@@ -5482,6 +5725,15 @@ convert_template_argument (tree parm,
        val = error_mark_node;
       else if (val == error_mark_node && (complain & tf_error))
        error ("could not convert template argument %qE to %qT",  orig_arg, t);
+
+      if (TREE_CODE (val) == SCOPE_REF)
+       {
+         /* Strip typedefs from the SCOPE_REF.  */
+         tree type = strip_typedefs (TREE_TYPE (val));
+         tree scope = strip_typedefs (TREE_OPERAND (val, 0));
+         val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
+                                     QUALIFIED_NAME_IS_TEMPLATE (val));
+       }
     }
 
   return val;
@@ -5601,6 +5853,10 @@ coerce_template_parameter_pack (tree parms,
     }
 
   SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
+#ifdef ENABLE_CHECKING
+  SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
+                                      TREE_VEC_LENGTH (packed_args));
+#endif
   return argument_pack;
 }
 
@@ -5756,9 +6012,16 @@ coerce_template_parms (tree parms,
             }
         }
       else if (require_all_args)
-        /* There must be a default arg in this case.  */
-        arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
-                                   complain, in_decl);
+       {
+         /* There must be a default arg in this case.  */
+         arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
+                                    complain, in_decl);
+         /* The position of the first default template argument,
+            is also the number of non-defaulted arguments in NEW_INNER_ARGS.
+            Record that.  */
+         if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
+           SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args, arg_idx);
+       }
       else
        break;
 
@@ -5789,6 +6052,12 @@ coerce_template_parms (tree parms,
   if (lost)
     return error_mark_node;
 
+#ifdef ENABLE_CHECKING
+  if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
+    SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
+                                        TREE_VEC_LENGTH (new_inner_args));
+#endif
+
   return new_inner_args;
 }
 
@@ -5942,15 +6211,43 @@ lookup_template_function (tree fns, tree arglist)
    TEMPLATE_DECL.  If DECL is a TYPE_DECL for current_class_type,
    or one of its enclosing classes, and that type is a template,
    return the associated TEMPLATE_DECL.  Otherwise, the original
-   DECL is returned.  */
+   DECL is returned.
+
+   Also handle the case when DECL is a TREE_LIST of ambiguous
+   injected-class-names from different bases.  */
 
 tree
 maybe_get_template_decl_from_type_decl (tree decl)
 {
+  if (decl == NULL_TREE)
+    return decl;
+
+  /* DR 176: A lookup that finds an injected-class-name (10.2
+     [class.member.lookup]) can result in an ambiguity in certain cases
+     (for example, if it is found in more than one base class). If all of
+     the injected-class-names that are found refer to specializations of
+     the same class template, and if the name is followed by a
+     template-argument-list, the reference refers to the class template
+     itself and not a specialization thereof, and is not ambiguous.  */
+  if (TREE_CODE (decl) == TREE_LIST)
+    {
+      tree t, tmpl = NULL_TREE;
+      for (t = decl; t; t = TREE_CHAIN (t))
+       {
+         tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
+         if (!tmpl)
+           tmpl = elt;
+         else if (tmpl != elt)
+           break;
+       }
+      if (tmpl && t == NULL_TREE)
+       return tmpl;
+      else
+       return decl;
+    }
+
   return (decl != NULL_TREE
-         && TREE_CODE (decl) == TYPE_DECL
-         && DECL_ARTIFICIAL (decl)
-         && CLASS_TYPE_P (TREE_TYPE (decl))
+         && DECL_SELF_REFERENCE_P (decl)
          && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
     ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
 }
@@ -6126,7 +6423,8 @@ lookup_template_class (tree d1,
       tree found = NULL_TREE;
       int arg_depth;
       int parm_depth;
-      int is_partial_instantiation;
+      int is_dependent_type;
+      int use_partial_inst_tmpl = false;
 
       gen_tmpl = most_general_template (templ);
       parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
@@ -6242,21 +6540,17 @@ lookup_template_class (tree d1,
       if (entry)
        POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, entry->spec);
 
-      /* This type is a "partial instantiation" if any of the template
-        arguments still involve template parameters.  Note that we set
-        IS_PARTIAL_INSTANTIATION for partial specializations as
-        well.  */
-      is_partial_instantiation = uses_template_parms (arglist);
+      is_dependent_type = uses_template_parms (arglist);
 
       /* If the deduced arguments are invalid, then the binding
         failed.  */
-      if (!is_partial_instantiation
+      if (!is_dependent_type
          && check_instantiated_args (gen_tmpl,
                                      INNERMOST_TEMPLATE_ARGS (arglist),
                                      complain))
        POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
 
-      if (!is_partial_instantiation
+      if (!is_dependent_type
          && !PRIMARY_TEMPLATE_P (gen_tmpl)
          && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
          && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
@@ -6275,7 +6569,7 @@ lookup_template_class (tree d1,
       /* Create the type.  */
       if (TREE_CODE (template_type) == ENUMERAL_TYPE)
        {
-         if (!is_partial_instantiation)
+         if (!is_dependent_type)
            {
              set_current_access_from_decl (TYPE_NAME (template_type));
              t = start_enum (TYPE_IDENTIFIER (template_type),
@@ -6325,7 +6619,6 @@ lookup_template_class (tree d1,
 
          type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
          DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
-         TYPE_STUB_DECL (t) = type_decl;
          DECL_SOURCE_LOCATION (type_decl)
            = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
        }
@@ -6342,11 +6635,71 @@ lookup_template_class (tree d1,
          DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
        }
 
-      /* Set up the template information.  We have to figure out which
-        template is the immediate parent if this is a full
-        instantiation.  */
-      if (parm_depth == 1 || is_partial_instantiation
-         || !PRIMARY_TEMPLATE_P (gen_tmpl))
+      /* Let's consider the explicit specialization of a member
+         of a class template specialization that is implicitely instantiated,
+        e.g.:
+            template<class T>
+            struct S
+            {
+              template<class U> struct M {}; //#0
+            };
+
+            template<>
+            template<>
+            struct S<int>::M<char> //#1
+            {
+              int i;
+            };
+       [temp.expl.spec]/4 says this is valid.
+
+       In this case, when we write:
+       S<int>::M<char> m;
+
+       M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
+       the one of #0.
+
+       When we encounter #1, we want to store the partial instantiation
+       of M (template<class T> S<int>::M<T>) in it's CLASSTYPE_TI_TEMPLATE.
+
+       For all cases other than this "explicit specialization of member of a
+       class template", we just want to store the most general template into
+       the CLASSTYPE_TI_TEMPLATE of M.
+
+       This case of "explicit specialization of member of a class template"
+       only happens when:
+       1/ the enclosing class is an instantiation of, and therefore not
+       the same as, the context of the most general template, and
+       2/ we aren't looking at the partial instantiation itself, i.e.
+       the innermost arguments are not the same as the innermost parms of
+       the most general template.
+
+       So it's only when 1/ and 2/ happens that we want to use the partial
+       instantiation of the member template in lieu of its most general
+       template.  */
+
+      if (PRIMARY_TEMPLATE_P (gen_tmpl)
+         && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
+         /* the enclosing class must be an instantiation...  */
+         && CLASS_TYPE_P (context)
+         && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
+       {
+         tree partial_inst_args;
+         TREE_VEC_LENGTH (arglist)--;
+         ++processing_template_decl;
+         partial_inst_args =
+           tsubst (INNERMOST_TEMPLATE_ARGS
+                       (CLASSTYPE_TI_ARGS (TREE_TYPE (gen_tmpl))),
+                   arglist, complain, NULL_TREE);
+         --processing_template_decl;
+         TREE_VEC_LENGTH (arglist)++;
+         use_partial_inst_tmpl =
+           /*...and we must not be looking at the partial instantiation
+            itself. */
+           !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
+                                partial_inst_args);
+       }
+
+      if (!use_partial_inst_tmpl)
        /* This case is easy; there are no member templates involved.  */
        found = gen_tmpl;
       else
@@ -6362,7 +6715,7 @@ lookup_template_class (tree d1,
          found = CLASSTYPE_TI_TEMPLATE (found);
        }
 
-      SET_TYPE_TEMPLATE_INFO (t, tree_cons (found, arglist, NULL_TREE));
+      SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
 
       elt.spec = t;
       slot = (spec_entry **) htab_find_slot_with_hash (type_specializations,
@@ -6376,8 +6729,7 @@ lookup_template_class (tree d1,
        = tree_cons (arglist, t,
                     DECL_TEMPLATE_INSTANTIATIONS (templ));
 
-      if (TREE_CODE (t) == ENUMERAL_TYPE
-         && !is_partial_instantiation)
+      if (TREE_CODE (t) == ENUMERAL_TYPE && !is_dependent_type)
        /* Now that the type has been registered on the instantiations
           list, we set up the enumerators.  Because the enumeration
           constants may involve the enumeration type itself, we make
@@ -6387,7 +6739,7 @@ lookup_template_class (tree d1,
           the instantiation and exit above.  */
        tsubst_enum (template_type, t, arglist);
 
-      if (is_partial_instantiation)
+      if (is_dependent_type)
        /* If the type makes use of template parameters, the
           code that generates debugging information will crash.  */
        DECL_IGNORED_P (TYPE_STUB_DECL (t)) = 1;
@@ -6438,7 +6790,7 @@ for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
     case ENUMERAL_TYPE:
       if (!TYPE_TEMPLATE_INFO (t))
        *walk_subtrees = 0;
-      else if (for_each_template_parm (TREE_VALUE (TYPE_TEMPLATE_INFO (t)),
+      else if (for_each_template_parm (TI_ARGS (TYPE_TEMPLATE_INFO (t)),
                                       fn, data, pfd->visited, 
                                       pfd->include_nondeduced_p))
        return error_mark_node;
@@ -6703,7 +7055,7 @@ static int last_template_error_tick;
 /* We're starting to instantiate D; record the template instantiation context
    for diagnostics and to restore it later.  */
 
-static int
+int
 push_tinst_level (tree d)
 {
   struct tinst_level *new_level;
@@ -6718,7 +7070,7 @@ push_tinst_level (tree d)
 
       last_template_error_tick = tinst_level_tick;
       error ("template instantiation depth exceeds maximum of %d (use "
-            "-ftemplate-depth-NN to increase the maximum) instantiating %qD",
+            "-ftemplate-depth= to increase the maximum) instantiating %qD",
             max_tinst_depth, d);
 
       print_instantiation_context ();
@@ -6746,7 +7098,7 @@ push_tinst_level (tree d)
 /* We're done instantiating this template; return to the instantiation
    context.  */
 
-static void
+void
 pop_tinst_level (void)
 {
   /* Restore the filename and line number stashed away when we started
@@ -6988,11 +7340,18 @@ tsubst_friend_function (tree decl, tree args)
              DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
 
              if (TREE_CODE (old_decl) != TEMPLATE_DECL)
-               /* We should have called reregister_specialization in
-                  duplicate_decls.  */
-               gcc_assert (retrieve_specialization (new_template,
-                                                    new_args, 0)
-                           == old_decl);
+               {
+                 /* We should have called reregister_specialization in
+                    duplicate_decls.  */
+                 gcc_assert (retrieve_specialization (new_template,
+                                                      new_args, 0)
+                             == old_decl);
+
+                 /* Instantiate it if the global has already been used.  */
+                 if (DECL_ODR_USED (old_decl))
+                   instantiate_decl (old_decl, /*defer_ok=*/true,
+                                     /*expl_inst_class_mem_p=*/false);
+               }
              else
                {
                  tree t;
@@ -7294,17 +7653,24 @@ apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
 static void
 perform_typedefs_access_check (tree tmpl, tree targs)
 {
-  tree t;
+  location_t saved_location;
+  int i;
+  qualified_typedef_usage_t *iter;
 
   if (!tmpl
       || (!CLASS_TYPE_P (tmpl)
          && TREE_CODE (tmpl) != FUNCTION_DECL))
     return;
 
-  for (t = get_types_needing_access_check (tmpl); t; t = TREE_CHAIN (t))
+  saved_location = input_location;
+  for (i = 0;
+       VEC_iterate (qualified_typedef_usage_t,
+                   get_types_needing_access_check (tmpl),
+                   i, iter);
+       ++i)
     {
-      tree type_decl = TREE_PURPOSE (t);
-      tree type_scope = TREE_VALUE (t);
+      tree type_decl = iter->typedef_decl;
+      tree type_scope = iter->context;
 
       if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
        continue;
@@ -7314,9 +7680,13 @@ perform_typedefs_access_check (tree tmpl, tree targs)
       if (uses_template_parms (type_scope))
        type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
 
+      /* Make access check error messages point to the location
+         of the use of the typedef.  */
+      input_location = iter->locus;
       perform_or_defer_access_check (TYPE_BINFO (type_scope),
                                     type_decl, type_decl);
     }
+    input_location = saved_location;
 }
 
 tree
@@ -7326,13 +7696,14 @@ instantiate_class_template (tree type)
   tree typedecl;
   tree pbinfo;
   tree base_list;
+  unsigned int saved_maximum_field_alignment;
 
   if (type == error_mark_node)
     return error_mark_node;
 
   if (TYPE_BEING_DEFINED (type)
       || COMPLETE_TYPE_P (type)
-      || dependent_type_p (type))
+      || uses_template_parms (type))
     return type;
 
   /* Figure out which template is being instantiated.  */
@@ -7386,6 +7757,9 @@ instantiate_class_template (tree type)
   push_deferring_access_checks (dk_no_deferred);
 
   push_to_top_level ();
+  /* Use #pragma pack from the template context.  */
+  saved_maximum_field_alignment = maximum_field_alignment;
+  maximum_field_alignment = TYPE_PRECISION (pattern);
 
   SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
 
@@ -7801,6 +8175,7 @@ instantiate_class_template (tree type)
   perform_typedefs_access_check (pattern, args);
   perform_deferred_access_checks ();
   pop_nested_class ();
+  maximum_field_alignment = saved_maximum_field_alignment;
   pop_from_top_level ();
   pop_deferring_access_checks ();
   pop_tinst_level ();
@@ -7875,7 +8250,7 @@ tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
 {
   tree pattern;
   tree pack, packs = NULL_TREE, unsubstituted_packs = NULL_TREE;
-  tree first_arg_pack; int i, len = -1;
+  int i, len = -1;
   tree result;
   int incomplete = 0;
   bool very_local_specializations = false;
@@ -7959,10 +8334,7 @@ tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
             incomplete = 1;
 
           if (len < 0)
-            {
-              len = my_len;
-              first_arg_pack = arg_pack;
-            }
+           len = my_len;
           else if (len != my_len)
             {
              if (incomplete)
@@ -8217,6 +8589,19 @@ tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
   /* Make space for the expanded arguments coming from template
      argument packs.  */
   t = make_tree_vec (len + expanded_len_adjust);
+  /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
+     arguments for a member template.
+     In that case each TREE_VEC in ORIG_T represents a level of template
+     arguments, and ORIG_T won't carry any non defaulted argument count.
+     It will rather be the nested TREE_VECs that will carry one.
+     In other words, ORIG_T carries a non defaulted argument count only
+     if it doesn't contain any nested TREE_VEC.  */
+  if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
+    {
+      int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
+      count += expanded_len_adjust;
+      SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
+    }
   for (i = 0, out = 0; i < len; i++)
     {
       if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
@@ -8563,7 +8948,7 @@ tsubst_decl (tree t, tree args, tsubst_flags_t complain)
        gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
        TREE_CHAIN (r) = NULL_TREE;
 
-       DECL_TEMPLATE_INFO (r) = build_tree_list (t, args);
+       DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
 
        if (TREE_CODE (decl) == TYPE_DECL)
          {
@@ -8652,7 +9037,8 @@ tsubst_decl (tree t, tree args, tsubst_flags_t complain)
               specialize R.  */
            gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
            argvec = tsubst_template_args (DECL_TI_ARGS
-                                          (DECL_TEMPLATE_RESULT (gen_tmpl)),
+                                          (DECL_TEMPLATE_RESULT
+                                                 (DECL_TI_TEMPLATE (t))),
                                           args, complain, in_decl);
 
            /* Check to see if we already have this specialization.  */
@@ -8792,7 +9178,7 @@ tsubst_decl (tree t, tree args, tsubst_flags_t complain)
        if (gen_tmpl)
          {
            DECL_TEMPLATE_INFO (r)
-             = tree_cons (gen_tmpl, argvec, NULL_TREE);
+             = build_template_info (gen_tmpl, argvec);
            SET_DECL_IMPLICIT_INSTANTIATION (r);
            register_specialization (r, gen_tmpl, argvec, false, hash);
 
@@ -9115,7 +9501,15 @@ tsubst_decl (tree t, tree args, tsubst_flags_t complain)
        /* Create a new node for the specialization we need.  */
        r = copy_decl (t);
        if (type == NULL_TREE)
-         type = tsubst (TREE_TYPE (t), args, complain, in_decl);
+         {
+           if (is_typedef_decl (t))
+             type = DECL_ORIGINAL_TYPE (t);
+           else
+             type = TREE_TYPE (t);
+           if (TREE_CODE (t) == VAR_DECL && VAR_HAD_UNKNOWN_BOUND (t))
+             type = strip_array_domain (type);
+           type = tsubst (type, args, complain, in_decl);
+         }
        if (TREE_CODE (r) == VAR_DECL)
          {
            /* Even if the original location is out of scope, the
@@ -9186,16 +9580,6 @@ tsubst_decl (tree t, tree args, tsubst_flags_t complain)
              }
            determine_visibility (r);
          }
-       /* Preserve a typedef that names a type.  */
-       else if (TREE_CODE (r) == TYPE_DECL
-                && DECL_ORIGINAL_TYPE (t)
-                && type != error_mark_node)
-         {
-           DECL_ORIGINAL_TYPE (r) = tsubst (DECL_ORIGINAL_TYPE (t),
-                                            args, complain, in_decl);
-           TREE_TYPE (r) = type = build_variant_type_copy (type);
-           TYPE_NAME (type) = r;
-         }
 
        if (!local_p)
          {
@@ -9206,7 +9590,7 @@ tsubst_decl (tree t, tree args, tsubst_flags_t complain)
            DECL_EXTERNAL (r) = 1;
 
            register_specialization (r, gen_tmpl, argvec, false, hash);
-           DECL_TEMPLATE_INFO (r) = tree_cons (tmpl, argvec, NULL_TREE);
+           DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
            SET_DECL_IMPLICIT_INSTANTIATION (r);
          }
        else if (cp_unevaluated_operand)
@@ -9231,8 +9615,16 @@ tsubst_decl (tree t, tree args, tsubst_flags_t complain)
        TREE_CHAIN (r) = NULL_TREE;
 
        apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
-                                       (int) ATTR_FLAG_TYPE_IN_PLACE,
+                                       /*flags=*/0,
                                        args, complain, in_decl);
+
+       /* Preserve a typedef that names a type.  */
+       if (is_typedef_decl (r))
+         {
+           DECL_ORIGINAL_TYPE (r) = NULL_TREE;
+           set_underlying_type (r);
+         }
+
        layout_decl (r, 0);
       }
       break;
@@ -9576,6 +9968,7 @@ tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
 
   if (type
       && TREE_CODE (t) != TYPENAME_TYPE
+      && TREE_CODE (t) != TEMPLATE_TYPE_PARM
       && TREE_CODE (t) != IDENTIFIER_NODE
       && TREE_CODE (t) != FUNCTION_TYPE
       && TREE_CODE (t) != METHOD_TYPE)
@@ -9757,7 +10150,7 @@ tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
              }
            else
              /* TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX.  */
-             return arg;
+             return unshare_expr (arg);
          }
 
        if (level == 1)
@@ -9815,7 +10208,7 @@ tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
                      return error_mark_node;
 
                    TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
-                     = tree_cons (TYPE_TI_TEMPLATE (t), argvec, NULL_TREE);
+                     = build_template_info (TYPE_TI_TEMPLATE (t), argvec);
                  }
              }
            break;
@@ -9912,10 +10305,10 @@ 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 %qT",
-                        (code == POINTER_TYPE) ? "pointer" : "reference",
-                        type);
+               else if (code == POINTER_TYPE)
+                 error ("forming pointer to reference type %qT", type);
+               else
+                 error ("forming reference to reference type %qT", type);
                last_loc = input_location;
              }
 
@@ -9984,13 +10377,8 @@ tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
          {
            /* The type of the implicit object parameter gets its
               cv-qualifiers from the FUNCTION_TYPE. */
-           tree method_type;
-           tree this_type = cp_build_qualified_type (TYPE_MAIN_VARIANT (r),
-                                                     cp_type_quals (type));
            tree memptr;
-           method_type = build_method_type_directly (this_type,
-                                                     TREE_TYPE (type),
-                                                     TYPE_ARG_TYPES (type));
+           tree method_type = build_memfn_type (type, r, cp_type_quals (type));
            memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
            return cp_build_qualified_type_real (memptr, cp_type_quals (t),
                                                 complain);
@@ -10194,10 +10582,17 @@ tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
       {
        tree type;
 
-       type = finish_typeof (tsubst_expr 
-                             (TYPEOF_TYPE_EXPR (t), args,
-                              complain, in_decl,
-                              /*integral_constant_expression_p=*/false));
+       ++cp_unevaluated_operand;
+       ++c_inhibit_evaluation_warnings;
+
+       type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
+                           complain, in_decl,
+                           /*integral_constant_expression_p=*/false);
+
+       --cp_unevaluated_operand;
+       --c_inhibit_evaluation_warnings;
+
+       type = finish_typeof (type);
        return cp_build_qualified_type_real (type,
                                             cp_type_quals (t)
                                             | cp_type_quals (type),
@@ -10285,7 +10680,7 @@ tsubst_baselink (tree baselink, tree object_type,
     qualifying_scope = tsubst (qualifying_scope, args,
                               complain, in_decl);
     fns = BASELINK_FUNCTIONS (baselink);
-    optype = BASELINK_OPTYPE (baselink);
+    optype = tsubst (BASELINK_OPTYPE (baselink), args, complain, in_decl);
     if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
       {
        template_id_p = true;
@@ -10296,6 +10691,8 @@ tsubst_baselink (tree baselink, tree object_type,
                                                complain, in_decl);
       }
     name = DECL_NAME (get_first_fn (fns));
+    if (IDENTIFIER_TYPENAME_P (name))
+      name = mangle_conv_op_name_for_type (optype);
     baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
 
     /* If lookup found a single function, mark it as used at this
@@ -10314,8 +10711,7 @@ tsubst_baselink (tree baselink, tree object_type,
                    BASELINK_FUNCTIONS (baselink),
                    template_args);
     /* Update the conversion operator type.  */
-    BASELINK_OPTYPE (baselink) 
-      = tsubst (optype, args, complain, in_decl);
+    BASELINK_OPTYPE (baselink) = optype;
 
     if (!object_type)
       object_type = current_class_type;
@@ -10373,14 +10769,9 @@ tsubst_qualified_id (tree qualified_id, tree args,
   else
     expr = name;
 
-  if (dependent_type_p (scope))
-    {
-      tree type = NULL_TREE;
-      if (DECL_P (expr) && !dependent_scope_p (scope))
-       type = TREE_TYPE (expr);
-      return build_qualified_name (type, scope, expr,
-                                  QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
-    }
+  if (dependent_scope_p (scope))
+    return build_qualified_name (NULL_TREE, scope, expr,
+                                QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
 
   if (!BASELINK_P (name) && !DECL_P (expr))
     {
@@ -11695,7 +12086,7 @@ tsubst_copy_and_build (tree t,
              r = convert_from_reference (r);
          }
        else
-         r = build_x_indirect_ref (r, "unary *", complain);
+         r = build_x_indirect_ref (r, RO_UNARY_STAR, complain);
        return r;
       }
 
@@ -12416,6 +12807,11 @@ tsubst_copy_and_build (tree t,
        stmt_expr = finish_stmt_expr (stmt_expr, false);
        cur_stmt_expr = old_stmt_expr;
 
+       /* If the resulting list of expression statement is empty,
+          fold it further into void_zero_node.  */
+       if (empty_expr_stmt_p (stmt_expr))
+         stmt_expr = void_zero_node;
+
        return stmt_expr;
       }
 
@@ -12438,6 +12834,8 @@ tsubst_copy_and_build (tree t,
        TREE_TYPE (r) = type;
        CLASSTYPE_LAMBDA_EXPR (type) = r;
 
+       LAMBDA_EXPR_LOCATION (r)
+         = LAMBDA_EXPR_LOCATION (t);
        LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
          = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
        LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
@@ -13060,6 +13458,10 @@ type_unification_real (tree tparms,
   gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
   gcc_assert (ntparms > 0);
 
+  /* Reset the number of non-defaulted template arguments contained
+     in in TARGS.  */
+  NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
+
   switch (strict)
     {
     case DEDUCE_CALL:
@@ -13238,6 +13640,11 @@ type_unification_real (tree tparms,
               else
                 {
                   TREE_VEC_ELT (targs, i) = arg;
+                 /* The position of the first default template argument,
+                    is also the number of non-defaulted arguments in TARGS.
+                    Record that.  */
+                 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
+                   SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
                   continue;
                 }
             }
@@ -13265,6 +13672,10 @@ type_unification_real (tree tparms,
 
          return 2;
        }
+#ifdef ENABLE_CHECKING
+  if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
+    SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
+#endif
 
   return 0;
 }
@@ -13862,6 +14273,9 @@ unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
 
         if (!skip_arg_p)
           {
+           /* For deduction from an init-list we need the actual list.  */
+           if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
+             arg = arg_expr;
             if (unify (tparms, targs, parm, arg, arg_strict))
               return 1;
           }
@@ -13886,8 +14300,15 @@ unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
       tree old_pack = TREE_VALUE (pack);
       tree new_args = TREE_TYPE (pack);
       int i, len = TREE_VEC_LENGTH (new_args);
+      int idx, level;
       bool nondeduced_p = false;
 
+      /* By default keep the original deduced argument pack.
+        If necessary, more specific code is going to update the
+        resulting deduced argument later down in this function.  */
+      template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
+      TMPL_ARG (targs, level, idx) = old_pack;
+
       /* If NEW_ARGS contains any NULL_TREE entries, we didn't
         actually deduce anything.  */
       for (i = 0; i < len && !nondeduced_p; ++i)
@@ -13918,10 +14339,6 @@ unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
       if (!old_pack)
         {
           tree result;
-          int idx, level;
-          
-          template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
-
           /* Build the deduced *_ARGUMENT_PACK.  */
           if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
             {
@@ -13945,12 +14362,7 @@ unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
         {
           /* We only had the explicitly-provided arguments before, but
              now we have a complete set of arguments.  */
-          int idx, level;
           tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
-          template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
-
-          /* Keep the original deduced argument pack.  */
-          TMPL_ARG (targs, level, idx) = old_pack;
 
           SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
           ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
@@ -13960,15 +14372,6 @@ unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
                                     new_args))
         /* Inconsistent unification of this parameter pack.  */
         return 1;
-      else
-        {
-          int idx, level;
-          
-          template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
-
-          /* Keep the original deduced argument pack.  */
-          TMPL_ARG (targs, level, idx) = old_pack;
-        }
     }
 
   return 0;
@@ -14069,6 +14472,10 @@ unify (tree tparms, tree targs, tree parm, tree arg, int strict)
       FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
        {
          int elt_strict = strict;
+
+         if (elt == error_mark_node)
+           return 1;
+
          if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
            {
              tree type = TREE_TYPE (elt);
@@ -14680,6 +15087,13 @@ unify (tree tparms, tree targs, tree parm, tree arg, int strict)
       /* Matched cases are handled by the ARG == PARM test above.  */
       return 1;
 
+    case VAR_DECL:
+      /* A non-type template parameter that is a variable should be a
+        an integral constant, in which case, it whould have been
+        folded into its (constant) value. So we should not be getting
+        a variable here.  */
+      gcc_unreachable ();
+
     case TYPE_ARGUMENT_PACK:
     case NONTYPE_ARGUMENT_PACK:
       {
@@ -14828,6 +15242,35 @@ mark_decl_instantiated (tree result, int extern_p)
   DECL_INTERFACE_KNOWN (result) = 1;
 }
 
+/* Subroutine of more_specialized_fn: check whether TARGS is missing any
+   important template arguments.  If any are missing, we check whether
+   they're important by using error_mark_node for substituting into any
+   args that were used for partial ordering (the ones between ARGS and END)
+   and seeing if it bubbles up.  */
+
+static bool
+check_undeduced_parms (tree targs, tree args, tree end)
+{
+  bool found = false;
+  int i;
+  for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
+    if (TREE_VEC_ELT (targs, i) == NULL_TREE)
+      {
+       found = true;
+       TREE_VEC_ELT (targs, i) = error_mark_node;
+      }
+  if (found)
+    {
+      for (; args != end; args = TREE_CHAIN (args))
+       {
+         tree substed = tsubst (TREE_VALUE (args), targs, tf_none, NULL_TREE);
+         if (substed == error_mark_node)
+           return true;
+       }
+    }
+  return false;
+}
+
 /* Given two function templates PAT1 and PAT2, return:
 
    1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
@@ -14851,8 +15294,12 @@ mark_decl_instantiated (tree result, int extern_p)
    neither is more cv-qualified, they both are equal).  Unlike regular
    deduction, after all the arguments have been deduced in this way,
    we do *not* verify the deduced template argument values can be
-   substituted into non-deduced contexts, nor do we have to verify
-   that all template arguments have been deduced.  */
+   substituted into non-deduced contexts.
+
+   The logic can be a bit confusing here, because we look at deduce1 and
+   targs1 to see if pat2 is at least as specialized, and vice versa; if we
+   can find template arguments for pat1 to make arg1 look like arg2, that
+   means that arg2 is at least as specialized as arg1.  */
 
 int
 more_specialized_fn (tree pat1, tree pat2, int len)
@@ -14865,8 +15312,9 @@ more_specialized_fn (tree pat1, tree pat2, int len)
   tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
   tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
   tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
-  int better1 = 0;
-  int better2 = 0;
+  tree origs1, origs2;
+  bool lose1 = false;
+  bool lose2 = false;
 
   /* Remove the this parameter from non-static member functions.  If
      one is a non-static member function and the other is not a static
@@ -14905,6 +15353,9 @@ more_specialized_fn (tree pat1, tree pat2, int len)
 
   processing_template_decl++;
 
+  origs1 = args1;
+  origs2 = args2;
+
   while (len--
         /* Stop when an ellipsis is seen.  */
         && args1 != NULL_TREE && args2 != NULL_TREE)
@@ -15039,28 +15490,34 @@ more_specialized_fn (tree pat1, tree pat2, int len)
           deduce2 = !unify (tparms2, targs2, arg2, arg1, UNIFY_ALLOW_NONE);
         }
 
+      /* If we couldn't deduce arguments for tparms1 to make arg1 match
+        arg2, then arg2 is not as specialized as arg1.  */
       if (!deduce1)
-       better2 = -1;
+       lose2 = true;
       if (!deduce2)
-       better1 = -1;
-      if (better1 < 0 && better2 < 0)
-       /* We've failed to deduce something in either direction.
-          These must be unordered.  */
-       break;
-
-      if (deduce1 && deduce2 && quals1 >= 0 && quals2 >= 0)
+       lose1 = true;
+
+      /* "If, for a given type, deduction succeeds in both directions
+        (i.e., the types are identical after the transformations above)
+        and if the type from the argument template is more cv-qualified
+        than the type from the parameter template (as described above)
+        that type is considered to be more specialized than the other. If
+        neither type is more cv-qualified than the other then neither type
+        is more specialized than the other."  */
+
+      if (deduce1 && deduce2
+         && quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
        {
-         /* Deduces in both directions, see if quals can
-            disambiguate.  Pretend the worse one failed to deduce. */
          if ((quals1 & quals2) == quals2)
-           deduce1 = 0;
+           lose2 = true;
          if ((quals1 & quals2) == quals1)
-           deduce2 = 0;
+           lose1 = true;
        }
-      if (deduce1 && !deduce2 && !better2)
-       better2 = 1;
-      if (deduce2 && !deduce1 && !better1)
-       better1 = 1;
+
+      if (lose1 && lose2)
+       /* We've failed to deduce something in either direction.
+          These must be unordered.  */
+       break;
 
       if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
           || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
@@ -15072,22 +15529,38 @@ more_specialized_fn (tree pat1, tree pat2, int len)
       args2 = TREE_CHAIN (args2);
     }
 
+  /* "In most cases, all template parameters must have values in order for
+     deduction to succeed, but for partial ordering purposes a template
+     parameter may remain without a value provided it is not used in the
+     types being used for partial ordering."
+
+     Thus, if we are missing any of the targs1 we need to substitute into
+     origs1, then pat2 is not as specialized as pat1.  This can happen when
+     there is a nondeduced context.  */
+  if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
+    lose2 = true;
+  if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
+    lose1 = true;
+
   processing_template_decl--;
 
   /* All things being equal, if the next argument is a pack expansion
      for one function but not for the other, prefer the
-     non-variadic function.  */
-  if ((better1 > 0) - (better2 > 0) == 0
+     non-variadic function.  FIXME this is bogus; see c++/41958.  */
+  if (lose1 == lose2
       && args1 && TREE_VALUE (args1)
       && args2 && TREE_VALUE (args2))
     {
-      if (TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION)
-        return TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION ? 0 : -1;
-      else if (TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION)
-        return 1;
+      lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
+      lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
     }
 
-  return (better1 > 0) - (better2 > 0);
+  if (lose1 == lose2)
+    return 0;
+  else if (!lose1)
+    return 1;
+  else
+    return -1;
 }
 
 /* Determine which of two partial specializations is more specialized.
@@ -15476,12 +15949,13 @@ most_specialized_class (tree type, tree tmpl)
       tree parms = TREE_VALUE (t);
 
       partial_spec_args = CLASSTYPE_TI_ARGS (TREE_TYPE (t));
+
+      ++processing_template_decl;
+
       if (outer_args)
        {
          int i;
 
-         ++processing_template_decl;
-
          /* Discard the outer levels of args, and then substitute in the
             template args from the enclosing class.  */
          partial_spec_args = INNERMOST_TEMPLATE_ARGS (partial_spec_args);
@@ -15498,8 +15972,21 @@ most_specialized_class (tree type, tree tmpl)
            TREE_VEC_ELT (parms, i) =
              tsubst (TREE_VEC_ELT (parms, i), outer_args, tf_none, NULL_TREE);
 
-         --processing_template_decl;
        }
+
+      partial_spec_args =
+         coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
+                                add_to_template_args (outer_args,
+                                                      partial_spec_args),
+                                tmpl, tf_none,
+                                /*require_all_args=*/true,
+                                /*use_default_args=*/true);
+
+      --processing_template_decl;
+
+      if (partial_spec_args == error_mark_node)
+       return error_mark_node;
+
       spec_args = get_class_bindings (parms,
                                      partial_spec_args,
                                      args);
@@ -15552,13 +16039,16 @@ most_specialized_class (tree type, tree tmpl)
 
   if (ambiguous_p)
     {
-      const char *str = "candidates are:";
+      const char *str;
+      char *spaces = NULL;
       error ("ambiguous class template instantiation for %q#T", type);
+      str = TREE_CHAIN (list) ? _("candidates are:") : _("candidate is:");
       for (t = list; t; t = TREE_CHAIN (t))
-       {
-         error ("%s %+#T", str, TREE_TYPE (t));
-         str = "               ";
-       }
+        {
+          error ("%s %+#T", spaces ? spaces : str, TREE_TYPE (t));
+          spaces = spaces ? spaces : get_spaces (str);
+        }
+      free (spaces);
       return error_mark_node;
     }
 
@@ -15984,10 +16474,15 @@ regenerate_decl_from_template (tree decl, tree tmpl)
        DECL_DECLARED_INLINE_P (decl) = 1;
     }
   else if (TREE_CODE (decl) == VAR_DECL)
-    DECL_INITIAL (decl) =
-      tsubst_expr (DECL_INITIAL (code_pattern), args,
-                  tf_error, DECL_TI_TEMPLATE (decl),
-                  /*integral_constant_expression_p=*/false);
+    {
+      DECL_INITIAL (decl) =
+       tsubst_expr (DECL_INITIAL (code_pattern), args,
+                    tf_error, DECL_TI_TEMPLATE (decl),
+                    /*integral_constant_expression_p=*/false);
+      if (VAR_HAD_UNKNOWN_BOUND (decl))
+       TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
+                                  tf_error, DECL_TI_TEMPLATE (decl));
+    }
   else
     gcc_unreachable ();
 
@@ -16447,7 +16942,7 @@ instantiate_pending_templates (int retries)
 
       error ("template instantiation depth exceeds maximum of %d"
             " instantiating %q+D, possibly from virtual table generation"
-            " (use -ftemplate-depth-NN to increase the maximum)",
+            " (use -ftemplate-depth= to increase the maximum)",
             max_tinst_depth, decl);
       if (TREE_CODE (decl) == FUNCTION_DECL)
        /* Pretend that we defined it.  */
@@ -17258,6 +17753,15 @@ type_dependent_expression_p (tree expression)
       return false;
     }
 
+  /* A static data member of the current instantiation with incomplete
+     array type is type-dependent, as the definition and specializations
+     can have different bounds.  */
+  if (TREE_CODE (expression) == VAR_DECL
+      && DECL_CLASS_SCOPE_P (expression)
+      && dependent_type_p (DECL_CONTEXT (expression))
+      && VAR_HAD_UNKNOWN_BOUND (expression))
+    return true;
+
   if (TREE_TYPE (expression) == unknown_type_node)
     {
       if (TREE_CODE (expression) == ADDR_EXPR)
@@ -17594,6 +18098,9 @@ resolve_typename_type (tree type, bool only_current_p)
      to look inside it.  */
   if (only_current_p && !currently_open_class (scope))
     return type;
+  /* If this is a typedef, we don't want to look inside (c++/11987).  */
+  if (typedef_variant_p (type))
+    return type;
   /* If SCOPE isn't the template itself, it will not have a valid
      TYPE_FIELDS list.  */
   if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
@@ -17770,10 +18277,7 @@ make_args_non_dependent (VEC(tree,gc) *args)
 tree
 make_auto (void)
 {
-  tree au;
-
-  /* ??? Is it worth caching this for multiple autos at the same level?  */
-  au = cxx_make_type (TEMPLATE_TYPE_PARM);
+  tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
   TYPE_NAME (au) = build_decl (BUILTINS_LOCATION,
                               TYPE_DECL, get_identifier ("auto"), au);
   TYPE_STUB_DECL (au) = TYPE_NAME (au);
@@ -17820,6 +18324,20 @@ listify_autos (tree type, tree auto_node)
   return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
 }
 
+/* walk_tree helper for do_auto_deduction.  */
+
+static tree
+contains_auto_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
+                void *type)
+{
+  /* Is this a variable with the type we're looking for?  */
+  if (DECL_P (*tp)
+      && TREE_TYPE (*tp) == type)
+    return *tp;
+  else
+    return NULL_TREE;
+}
+
 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
    from INIT.  AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.  */
 
@@ -17828,8 +18346,19 @@ do_auto_deduction (tree type, tree init, tree auto_node)
 {
   tree parms, tparms, targs;
   tree args[1];
+  tree decl;
   int val;
 
+  /* The name of the object being declared shall not appear in the
+     initializer expression.  */
+  decl = cp_walk_tree_without_duplicates (&init, contains_auto_r, type);
+  if (decl)
+    {
+      error ("variable %q#D with %<auto%> type used in its own "
+            "initializer", decl);
+      return error_mark_node;
+    }
+
   /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
      with either a new invented type template parameter U or, if the
      initializer is a braced-init-list (8.5.4), with
@@ -17851,6 +18380,19 @@ do_auto_deduction (tree type, tree init, tree auto_node)
       return error_mark_node;
     }
 
+  /* If the list of declarators contains more than one declarator, the type
+     of each declared variable is determined as described above. If the
+     type deduced for the template parameter U is not the same in each
+     deduction, the program is ill-formed.  */
+  if (TREE_TYPE (auto_node)
+      && !same_type_p (TREE_TYPE (auto_node), TREE_VEC_ELT (targs, 0)))
+    {
+      error ("inconsistent deduction for %qT: %qT and then %qT",
+            auto_node, TREE_TYPE (auto_node), TREE_VEC_ELT (targs, 0));
+      return error_mark_node;
+    }
+  TREE_TYPE (auto_node) = TREE_VEC_ELT (targs, 0);
+
   if (processing_template_decl)
     targs = add_to_template_args (current_template_args (), targs);
   return tsubst (type, targs, tf_warning_or_error, NULL_TREE);
@@ -17910,28 +18452,29 @@ type_uses_auto (tree type)
   return NULL_TREE;
 }
 
-/* For a given template T, return the list of typedefs referenced
+/* For a given template T, return the vector of typedefs referenced
    in T for which access check is needed at T instantiation time.
    T is either  a FUNCTION_DECL or a RECORD_TYPE.
    Those typedefs were added to T by the function
    append_type_to_template_for_access_check.  */
 
-tree
+VEC(qualified_typedef_usage_t,gc)*
 get_types_needing_access_check (tree t)
 {
-  tree ti, result = NULL_TREE;
+  tree ti;
+  VEC(qualified_typedef_usage_t,gc) *result = NULL;
 
   if (!t || t == error_mark_node)
-    return t;
+    return NULL;
 
   if (!(ti = get_template_info (t)))
-    return NULL_TREE;
+    return NULL;
 
   if (CLASS_TYPE_P (t)
       || TREE_CODE (t) == FUNCTION_DECL)
     {
       if (!TI_TEMPLATE (ti))
-       return NULL_TREE;
+       return NULL;
 
       result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
     }
@@ -17945,6 +18488,7 @@ get_types_needing_access_check (tree t)
    T is either a FUNCTION_DECL or a RECORD_TYPE.
    TYPE_DECL is a TYPE_DECL node representing a typedef.
    SCOPE is the scope through which TYPE_DECL is accessed.
+   LOCATION is the location of the usage point of TYPE_DECL.
 
    This function is a subroutine of
    append_type_to_template_for_access_check.  */
@@ -17952,8 +18496,10 @@ get_types_needing_access_check (tree t)
 static void
 append_type_to_template_for_access_check_1 (tree t,
                                            tree type_decl,
-                                           tree scope)
+                                           tree scope,
+                                           location_t location)
 {
+  qualified_typedef_usage_t typedef_usage;
   tree ti;
 
   if (!t || t == error_mark_node)
@@ -17970,14 +18516,20 @@ append_type_to_template_for_access_check_1 (tree t,
 
   gcc_assert (TI_TEMPLATE (ti));
 
-  TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti) =
-    tree_cons (type_decl, scope, TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti));
+  typedef_usage.typedef_decl = type_decl;
+  typedef_usage.context = scope;
+  typedef_usage.locus = location;
+
+  VEC_safe_push (qualified_typedef_usage_t, gc,
+                TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti),
+                &typedef_usage);
 }
 
 /* Append TYPE_DECL to the template TEMPL.
    TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
    At TEMPL instanciation time, TYPE_DECL will be checked to see
    if it can be accessed through SCOPE.
+   LOCATION is the location of the usage point of TYPE_DECL.
 
    e.g. consider the following code snippet:
 
@@ -17988,7 +18540,7 @@ append_type_to_template_for_access_check_1 (tree t,
 
      template<class U> struct S
      {
-       C::myint mi;
+       C::myint mi; // <-- usage point of the typedef C::myint
      };
 
      S<char> s;
@@ -18005,25 +18557,25 @@ append_type_to_template_for_access_check_1 (tree t,
 void
 append_type_to_template_for_access_check (tree templ,
                                           tree type_decl,
-                                         tree scope)
+                                         tree scope,
+                                         location_t location)
 {
-  tree node;
+  qualified_typedef_usage_t *iter;
+  int i;
 
   gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
 
   /* Make sure we don't append the type to the template twice.  */
-  for (node = get_types_needing_access_check (templ);
-       node;
-       node = TREE_CHAIN (node))
-    {
-      tree decl = TREE_PURPOSE (node);
-      tree type_scope = TREE_VALUE (node);
-
-      if (decl == type_decl && type_scope == scope)
-       return;
-    }
+  for (i = 0;
+       VEC_iterate (qualified_typedef_usage_t,
+                   get_types_needing_access_check (templ),
+                   i, iter);
+       ++i)
+    if (iter->typedef_decl == type_decl && scope == iter->context)
+      return;
 
-  append_type_to_template_for_access_check_1 (templ, type_decl, scope);
+  append_type_to_template_for_access_check_1 (templ, type_decl,
+                                             scope, location);
 }
 
 /* Set up the hash tables for template instantiations.  */
@@ -18041,4 +18593,19 @@ init_template_processing (void)
                                          ggc_free);
 }
 
+/* Print stats about the template hash tables for -fstats.  */
+
+void
+print_template_statistics (void)
+{
+  fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
+          "%f collisions\n", (long) htab_size (decl_specializations),
+          (long) htab_elements (decl_specializations),
+          htab_collisions (decl_specializations));
+  fprintf (stderr, "type_specializations: size %ld, %ld elements, "
+          "%f collisions\n", (long) htab_size (type_specializations),
+          (long) htab_elements (type_specializations),
+          htab_collisions (type_specializations));
+}
+
 #include "gt-cp-pt.h"