OSDN Git Service

PR c++/50303
[pf3gnuchains/gcc-fork.git] / gcc / cp / pt.c
index bbe1139..ea20323 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, 2010, 2011
+   2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010, 2011, 2012
    Free Software Foundation, Inc.
    Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
    Rewritten by Jason Merrill (jason@cygnus.com).
@@ -814,7 +814,13 @@ maybe_process_partial_specialization (tree type)
 
   context = TYPE_CONTEXT (type);
 
-  if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
+  if ((CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
+      /* Consider non-class instantiations of alias templates as
+        well.  */
+      || (TYPE_P (type)
+         && TYPE_TEMPLATE_INFO (type)
+         && DECL_LANG_SPECIFIC (TYPE_NAME (type))
+         && DECL_USE_TEMPLATE (TYPE_NAME (type))))
     {
       /* This is for ordinary explicit specialization and partial
         specialization of a template class such as:
@@ -827,7 +833,8 @@ maybe_process_partial_specialization (tree type)
 
         Make sure that `C<int>' and `C<T*>' are implicit instantiations.  */
 
-      if (CLASSTYPE_IMPLICIT_INSTANTIATION (type)
+      if (CLASS_TYPE_P (type)
+         && CLASSTYPE_IMPLICIT_INSTANTIATION (type)
          && !COMPLETE_TYPE_P (type))
        {
          check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type));
@@ -839,8 +846,16 @@ maybe_process_partial_specialization (tree type)
                return error_mark_node;
            }
        }
-      else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
+      else if (CLASS_TYPE_P (type)
+              && CLASSTYPE_TEMPLATE_INSTANTIATION (type))
        error ("specialization of %qT after instantiation", type);
+
+      if (DECL_ALIAS_TEMPLATE_P (TYPE_TI_TEMPLATE (type)))
+       {
+         error ("partial specialization of alias template %qD",
+                TYPE_TI_TEMPLATE (type));
+         return error_mark_node;
+       }
     }
   else if (CLASS_TYPE_P (type)
           && !CLASSTYPE_USE_TEMPLATE (type)
@@ -1519,7 +1534,8 @@ iterative_hash_template_arg (tree arg, hashval_t val)
 
     case TYPE_PACK_EXPANSION:
     case EXPR_PACK_EXPANSION:
-      return iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
+      val = iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
+      return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg), val);
 
     case TYPE_ARGUMENT_PACK:
     case NONTYPE_ARGUMENT_PACK:
@@ -2842,8 +2858,8 @@ make_ith_pack_parameter_name (tree name, int i)
   return get_identifier (newname);
 }
 
-/* Return true if T is a primary function
-   or class template instantiation.  */
+/* Return true if T is a primary function, class or alias template
+   instantiation.  */
 
 bool
 primary_template_instantiation_p (const_tree t)
@@ -2855,9 +2871,14 @@ primary_template_instantiation_p (const_tree t)
     return DECL_LANG_SPECIFIC (t)
           && DECL_TEMPLATE_INSTANTIATION (t)
           && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t));
-  else if (CLASS_TYPE_P (t))
+  else if (CLASS_TYPE_P (t) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
     return CLASSTYPE_TEMPLATE_INSTANTIATION (t)
           && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t));
+  else if (TYPE_P (t)
+          && TYPE_TEMPLATE_INFO (t)
+          && PRIMARY_TEMPLATE_P (TYPE_TI_TEMPLATE (t))
+          && DECL_TEMPLATE_INSTANTIATION (TYPE_NAME (t)))
+    return true;
   return false;
 }
 
@@ -2955,6 +2976,20 @@ find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
     (struct find_parameter_pack_data*)data;
   bool parameter_pack_p = false;
 
+  /* Handle type aliases/typedefs.  */
+  if (TYPE_P (t)
+      && TYPE_NAME (t)
+      && TREE_CODE (TYPE_NAME (t)) == TYPE_DECL
+      && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
+    {
+      if (TYPE_TEMPLATE_INFO (t))
+       cp_walk_tree (&TYPE_TI_ARGS (t),
+                     &find_parameter_packs_r,
+                     ppd, ppd->visited);
+      *walk_subtrees = 0;
+      return NULL_TREE;
+    }
+
   /* Identify whether this is a parameter pack or not.  */
   switch (TREE_CODE (t))
     {
@@ -2980,6 +3015,9 @@ find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
         }
       break;
 
+    case BASES:
+      parameter_pack_p = true;
+      break;
     default:
       /* Not a parameter pack.  */
       break;
@@ -3200,6 +3238,8 @@ make_pack_expansion (tree arg)
     }
   PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
 
+  PACK_EXPANSION_LOCAL_P (result) = at_function_scope_p ();
+
   return result;
 }
 
@@ -4647,7 +4687,7 @@ check_default_tmpl_args (tree decl, tree parms, int is_primary,
             "friend declarations");
   else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
     msg = G_("default template arguments may not be used in function templates "
-            "without -std=c++0x or -std=gnu++0x");
+            "without -std=c++11 or -std=gnu++11");
   else if (is_partial)
     msg = G_("default template arguments may not be used in "
             "partial specializations");
@@ -4828,6 +4868,10 @@ push_template_decl_real (tree decl, bool is_friend)
       else if (DECL_IMPLICIT_TYPEDEF_P (decl)
               && CLASS_TYPE_P (TREE_TYPE (decl)))
        /* OK */;
+      else if (TREE_CODE (decl) == TYPE_DECL
+              && TYPE_DECL_ALIAS_P (decl))
+       /* alias-declaration */
+       gcc_assert (!DECL_ARTIFICIAL (decl));
       else
        {
          error ("template declaration of %q#D", decl);
@@ -4877,7 +4921,10 @@ push_template_decl_real (tree decl, bool is_friend)
       if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
        TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
     }
-  else if (check_for_bare_parameter_packs (TREE_TYPE (decl)))
+  else if (check_for_bare_parameter_packs ((TREE_CODE (decl) == TYPE_DECL
+                                           && TYPE_DECL_ALIAS_P (decl))
+                                          ? DECL_ORIGINAL_TYPE (decl)
+                                          : TREE_TYPE (decl)))
     {
       TREE_TYPE (decl) = error_mark_node;
       return error_mark_node;
@@ -5092,8 +5139,13 @@ template arguments to %qD do not match original template %qD",
 
   if (DECL_IMPLICIT_TYPEDEF_P (decl))
     SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
-  else if (DECL_LANG_SPECIFIC (decl))
-    DECL_TEMPLATE_INFO (decl) = info;
+  else
+    {
+      if (primary && !DECL_LANG_SPECIFIC (decl))
+       retrofit_lang_decl (decl);
+      if (DECL_LANG_SPECIFIC (decl))
+       DECL_TEMPLATE_INFO (decl) = info;
+    }
 
   return DECL_TEMPLATE_RESULT (tmpl);
 }
@@ -5256,6 +5308,32 @@ fold_non_dependent_expr (tree expr)
   return fold_non_dependent_expr_sfinae (expr, tf_error);
 }
 
+/* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
+   template declaration, or a TYPE_DECL for an alias declaration.  */
+
+bool
+alias_type_or_template_p (tree t)
+{
+  if (t == NULL_TREE)
+    return false;
+  return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
+         || (TYPE_P (t)
+             && TYPE_NAME (t)
+             && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
+         || DECL_ALIAS_TEMPLATE_P (t));
+}
+
+/* Return TRUE iff is a specialization of an alias template.  */
+
+bool
+alias_template_specialization_p (tree t)
+{
+  if (t == NULL_TREE)
+    return false;
+  return (primary_template_instantiation_p (t)
+         && DECL_ALIAS_TEMPLATE_P (TYPE_TI_TEMPLATE (t)));
+}
+
 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
    must be a function or a pointer-to-function type, as specified
    in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
@@ -5266,6 +5344,7 @@ convert_nontype_argument_function (tree type, tree expr)
 {
   tree fns = expr;
   tree fn, fn_no_ptr;
+  linkage_kind linkage;
 
   fn = instantiate_type (type, fns, tf_none);
   if (fn == error_mark_node)
@@ -5274,7 +5353,7 @@ convert_nontype_argument_function (tree type, tree expr)
   fn_no_ptr = fn;
   if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
     fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
-  if (TREE_CODE (fn_no_ptr) == BASELINK)
+  if (BASELINK_P (fn_no_ptr))
     fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
  
   /* [temp.arg.nontype]/1
@@ -5282,12 +5361,19 @@ convert_nontype_argument_function (tree type, tree expr)
      A template-argument for a non-type, non-template template-parameter
      shall be one of:
      [...]
-     -- the address of an object or function with external linkage.  */
-  if (!DECL_EXTERNAL_LINKAGE_P (fn_no_ptr))
+     -- the address of an object or function with external [C++11: or
+        internal] linkage.  */
+  linkage = decl_linkage (fn_no_ptr);
+  if (cxx_dialect >= cxx0x ? linkage == lk_none : linkage != lk_external)
     {
-      error ("%qE is not a valid template argument for type %qT "
-            "because function %qD has not external linkage",
-            expr, type, fn_no_ptr);
+      if (cxx_dialect >= cxx0x)
+       error ("%qE is not a valid template argument for type %qT "
+              "because %qD has no linkage",
+              expr, type, fn_no_ptr);
+      else
+       error ("%qE is not a valid template argument for type %qT "
+              "because %qD does not have external linkage",
+              expr, type, fn_no_ptr);
       return NULL_TREE;
     }
 
@@ -5434,9 +5520,16 @@ static int
 unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
 {
   if (explain_p)
-    inform (input_location,
-           "  deduced conflicting types for parameter %qT (%qT and %qT)",
-           parm, first, second);
+    {
+      if (TYPE_P (parm))
+       inform (input_location,
+               "  deduced conflicting types for parameter %qT (%qT and %qT)",
+               parm, first, second);
+      else
+       inform (input_location,
+               "  deduced conflicting values for non-type parameter "
+               "%qE (%qE and %qE)", parm, first, second);
+    }
   return 1;
 }
 
@@ -5623,10 +5716,28 @@ convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
       && (TYPE_PTR_P (type) || TYPE_PTR_TO_MEMBER_P (type)))
     expr = convert (type, expr);
 
-  /* In C++11, non-type template arguments can be arbitrary constant
-     expressions.  But don't fold a PTRMEM_CST to a CONSTRUCTOR yet.  */
-  if (cxx_dialect >= cxx0x && TREE_CODE (expr) != PTRMEM_CST)
-    expr = maybe_constant_value (expr);
+  /* In C++11, integral or enumeration non-type template arguments can be
+     arbitrary constant expressions.  Pointer and pointer to
+     member arguments can be general constant expressions that evaluate
+     to a null value, but otherwise still need to be of a specific form.  */
+  if (cxx_dialect >= cxx0x)
+    {
+      if (TREE_CODE (expr) == PTRMEM_CST)
+       /* A PTRMEM_CST is already constant, and a valid template
+          argument for a parameter of pointer to member type, we just want
+          to leave it in that form rather than lower it to a
+          CONSTRUCTOR.  */;
+      else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
+       expr = maybe_constant_value (expr);
+      else if (TYPE_PTR_P (type)
+              || TYPE_PTR_TO_MEMBER_P (type))
+       {
+         tree folded = maybe_constant_value (expr);
+         if (TYPE_PTR_P (type) ? integer_zerop (folded)
+             : null_member_pointer_value_p (folded))
+           expr = folded;
+       }
+    }
 
   /* HACK: Due to double coercion, we can get a
      NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
@@ -5698,6 +5809,9 @@ convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
          if (complain & tf_error)
            {
              int errs = errorcount, warns = warningcount;
+             if (processing_template_decl
+                 && !require_potential_constant_expression (expr))
+               return NULL_TREE;
              expr = cxx_constant_value (expr);
              if (errorcount > errs || warningcount > warns)
                inform (EXPR_LOC_OR_HERE (expr),
@@ -5766,13 +5880,20 @@ convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
                     expr, type, decl);
              return NULL_TREE;
            }
-         else if (!DECL_EXTERNAL_LINKAGE_P (decl))
+         else if (cxx_dialect < cxx0x && !DECL_EXTERNAL_LINKAGE_P (decl))
            {
              error ("%qE is not a valid template argument of type %qT "
                     "because %qD does not have external linkage",
                     expr, type, decl);
              return NULL_TREE;
            }
+         else if (cxx_dialect >= cxx0x && decl_linkage (decl) == lk_none)
+           {
+             error ("%qE is not a valid template argument of type %qT "
+                    "because %qD has no linkage",
+                    expr, type, decl);
+             return NULL_TREE;
+           }
        }
 
       expr = decay_conversion (expr);
@@ -6329,8 +6450,16 @@ convert_template_argument (tree parm,
   if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
       && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
     {
-      permerror (input_location, "to refer to a type member of a template parameter, "
-                "use %<typename %E%>", orig_arg);
+      if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
+       {
+         if (complain & tf_error)
+           error ("invalid use of destructor %qE as a type", orig_arg);
+         return error_mark_node;
+       }
+
+      permerror (input_location,
+                "to refer to a type member of a template parameter, "
+                "use %<typename %E%>", orig_arg);
 
       orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
                                     TREE_OPERAND (arg, 1),
@@ -6605,6 +6734,20 @@ coerce_template_parameter_pack (tree parms,
   return argument_pack;
 }
 
+/* Returns true if the template argument vector ARGS contains
+   any pack expansions, false otherwise.  */
+
+static bool
+any_pack_expanson_args_p (tree args)
+{
+  int i;
+  if (args)
+    for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
+      if (PACK_EXPANSION_P (TREE_VEC_ELT (args, i)))
+       return true;
+  return false;
+}
+
 /* Convert all template arguments to their appropriate types, and
    return a vector containing the innermost resulting template
    arguments.  If any error occurs, return error_mark_node. Error and
@@ -6670,6 +6813,7 @@ coerce_template_parms (tree parms,
   if ((nargs > nparms && !variadic_p)
       || (nargs < nparms - variadic_p
          && require_all_args
+         && !any_pack_expanson_args_p (inner_args)
          && (!use_default_args
              || (TREE_VEC_ELT (parms, nargs) != error_mark_node
                   && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
@@ -6747,7 +6891,7 @@ coerce_template_parms (tree parms,
             {
               /* We don't know how many args we have yet, just
                  use the unconverted ones for now.  */
-              new_inner_args = args;
+              new_inner_args = inner_args;
               break;
             }
         }
@@ -6815,9 +6959,11 @@ template_args_equal (tree ot, tree nt)
     /* For member templates */
     return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
   else if (PACK_EXPANSION_P (ot))
-    return PACK_EXPANSION_P (nt) 
-      && template_args_equal (PACK_EXPANSION_PATTERN (ot),
-                              PACK_EXPANSION_PATTERN (nt));
+    return (PACK_EXPANSION_P (nt)
+           && template_args_equal (PACK_EXPANSION_PATTERN (ot),
+                                   PACK_EXPANSION_PATTERN (nt))
+           && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
+                                   PACK_EXPANSION_EXTRA_ARGS (nt)));
   else if (ARGUMENT_PACK_P (ot))
     {
       int i, len;
@@ -6867,6 +7013,12 @@ comp_template_args_with_info (tree oldargs, tree newargs,
 {
   int i;
 
+  if (oldargs == newargs)
+    return 1;
+
+  if (!oldargs || !newargs)
+    return 0;
+
   if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
     return 0;
 
@@ -7183,6 +7335,12 @@ lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
       int is_dependent_type;
       int use_partial_inst_tmpl = false;
 
+      if (template_type == error_mark_node)
+       /* An error occured while building the template TEMPL, and a
+          diagnostic has most certainly been emitted for that
+          already.  Let's propagate that error.  */
+       return error_mark_node;
+
       gen_tmpl = most_general_template (templ);
       parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
       parm_depth = TMPL_PARMS_DEPTH (parmlist);
@@ -7325,6 +7483,9 @@ lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
 
       context = tsubst (DECL_CONTEXT (gen_tmpl), arglist,
                        complain, in_decl);
+      if (context == error_mark_node)
+       return error_mark_node;
+
       if (!context)
        context = global_namespace;
 
@@ -7352,7 +7513,31 @@ lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
          ENUM_FIXED_UNDERLYING_TYPE_P (t)
            = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
        }
-      else
+      else if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
+       {
+         /* The user referred to a specialization of an alias
+           template represented by GEN_TMPL.
+
+           [temp.alias]/2 says:
+
+               When a template-id refers to the specialization of an
+               alias template, it is equivalent to the associated
+               type obtained by substitution of its
+               template-arguments for the template-parameters in the
+               type-id of the alias template.  */
+
+         t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl);
+         /* Note that the call above (by indirectly calling
+            register_specialization in tsubst_decl) registers the
+            TYPE_DECL representing the specialization of the alias
+            template.  So next time someone substitutes ARGLIST for
+            the template parms into the alias template (GEN_TMPL),
+            she'll get that TYPE_DECL back.  */
+
+         if (t == error_mark_node)
+           return t;
+       }
+      else if (CLASS_TYPE_P (template_type))
        {
          t = make_class_type (TREE_CODE (template_type));
          CLASSTYPE_DECLARED_CLASS (t)
@@ -7375,6 +7560,8 @@ lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
               structural equality testing. */
            SET_TYPE_STRUCTURAL_EQUALITY (t);
        }
+      else
+       gcc_unreachable ();
 
       /* If we called start_enum or pushtag above, this information
         will already be set up.  */
@@ -7390,14 +7577,17 @@ lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
       else
        type_decl = TYPE_NAME (t);
 
-      TREE_PRIVATE (type_decl)
-       = TREE_PRIVATE (TYPE_STUB_DECL (template_type));
-      TREE_PROTECTED (type_decl)
-       = TREE_PROTECTED (TYPE_STUB_DECL (template_type));
-      if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
+      if (CLASS_TYPE_P (template_type))
        {
-         DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
-         DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
+         TREE_PRIVATE (type_decl)
+           = TREE_PRIVATE (TYPE_STUB_DECL (template_type));
+         TREE_PROTECTED (type_decl)
+           = TREE_PROTECTED (TYPE_STUB_DECL (template_type));
+         if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
+           {
+             DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
+             DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
+           }
        }
 
       /* Let's consider the explicit specialization of a member
@@ -7453,7 +7643,7 @@ lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
          ++processing_template_decl;
          partial_inst_args =
            tsubst (INNERMOST_TEMPLATE_ARGS
-                       (CLASSTYPE_TI_ARGS (TREE_TYPE (gen_tmpl))),
+                       (TYPE_TI_ARGS (TREE_TYPE (gen_tmpl))),
                    arglist, complain, NULL_TREE);
          --processing_template_decl;
          TREE_VEC_LENGTH (arglist)++;
@@ -7477,7 +7667,15 @@ lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
          TREE_VEC_LENGTH (arglist)--;
          found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
          TREE_VEC_LENGTH (arglist)++;
-         found = CLASSTYPE_TI_TEMPLATE (found);
+         /* FOUND is either a proper class type, or an alias
+            template specialization.  In the later case, it's a
+            TYPE_DECL, resulting from the substituting of arguments
+            for parameters in the TYPE_DECL of the alias template
+            done earlier.  So be careful while getting the template
+            of FOUND.  */
+         found = TREE_CODE (found) == TYPE_DECL
+           ? TYPE_TI_TEMPLATE (TREE_TYPE (found))
+           : CLASSTYPE_TI_TEMPLATE (found);
        }
 
       SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
@@ -7505,7 +7703,7 @@ lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
           the instantiation and exit above.  */
        tsubst_enum (template_type, t, arglist);
 
-      if (is_dependent_type)
+      if (CLASS_TYPE_P (template_type) && 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;
@@ -7798,7 +7996,7 @@ uses_template_parms (tree t)
           || EXPR_P (t)
           || TREE_CODE (t) == TEMPLATE_PARM_INDEX
           || TREE_CODE (t) == OVERLOAD
-          || TREE_CODE (t) == BASELINK
+          || BASELINK_P (t)
           || TREE_CODE (t) == IDENTIFIER_NODE
           || TREE_CODE (t) == TRAIT_EXPR
           || TREE_CODE (t) == CONSTRUCTOR
@@ -7976,6 +8174,9 @@ parameter_of_template_p (tree parm, tree templ)
   for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
     {
       tree p = TREE_VALUE (TREE_VEC_ELT (parms, i));
+      if (p == error_mark_node)
+       continue;
+
       if (parm == p
          || (DECL_INITIAL (parm)
              && DECL_INITIAL (parm) == DECL_INITIAL (p)))
@@ -8597,6 +8798,8 @@ instantiate_class_template_1 (tree type)
     {
       CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
       CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
+      /* Adjust visibility for template arguments.  */
+      determine_visibility (TYPE_MAIN_DECL (type));
     }
   CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
 
@@ -8962,14 +9165,20 @@ instantiate_class_template_1 (tree type)
 
   if (CLASSTYPE_LAMBDA_EXPR (type))
     {
-      tree lambda = CLASSTYPE_LAMBDA_EXPR (type);
-      if (LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda))
+      tree decl = lambda_function (type);
+      if (decl)
        {
-         apply_lambda_return_type (lambda, void_type_node);
-         LAMBDA_EXPR_RETURN_TYPE (lambda) = NULL_TREE;
+         tree lambda = CLASSTYPE_LAMBDA_EXPR (type);
+         if (LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda))
+           {
+             apply_lambda_return_type (lambda, void_type_node);
+             LAMBDA_EXPR_RETURN_TYPE (lambda) = NULL_TREE;
+           }
+         instantiate_decl (decl, false, false);
+         maybe_add_lambda_conv_op (type);
        }
-      instantiate_decl (lambda_function (type), false, false);
-      maybe_add_lambda_conv_op (type);
+      else
+       gcc_assert (errorcount);
     }
 
   /* Set the file and line number information to whatever is given for
@@ -9109,13 +9318,22 @@ tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
   tree pattern;
   tree pack, packs = NULL_TREE;
   bool unsubstituted_packs = false;
+  bool real_packs = false;
+  int missing_level = 0;
   int i, len = -1;
   tree result;
   htab_t saved_local_specializations = NULL;
+  bool need_local_specializations = false;
+  int levels;
 
   gcc_assert (PACK_EXPANSION_P (t));
   pattern = PACK_EXPANSION_PATTERN (t);
 
+  /* Add in any args remembered from an earlier partial instantiation.  */
+  args = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (t), args);
+
+  levels = TMPL_ARGS_DEPTH (args);
+
   /* Determine the argument packs that will instantiate the parameter
      packs used in the expansion expression. While we're at it,
      compute the number of arguments to be expanded and make sure it
@@ -9126,10 +9344,20 @@ tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
       tree parm_pack = TREE_VALUE (pack);
       tree arg_pack = NULL_TREE;
       tree orig_arg = NULL_TREE;
+      int level = 0;
 
+      if (TREE_CODE (parm_pack) == BASES)
+       {
+         if (BASES_DIRECT (parm_pack))
+           return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
+                                                        args, complain, in_decl, false));
+         else
+           return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
+                                                 args, complain, in_decl, false));
+       }
       if (TREE_CODE (parm_pack) == PARM_DECL)
        {
-         if (!cp_unevaluated_operand)
+         if (PACK_EXPANSION_LOCAL_P (t))
            arg_pack = retrieve_local_specialization (parm_pack);
          else
            {
@@ -9145,14 +9373,14 @@ tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
                arg_pack = NULL_TREE;
              else
                arg_pack = make_fnparm_pack (arg_pack);
+             need_local_specializations = true;
            }
        }
       else
         {
-          int level, idx, levels;
+         int idx;
           template_parm_level_and_index (parm_pack, &level, &idx);
 
-          levels = TMPL_ARGS_DEPTH (args);
           if (level <= levels)
             arg_pack = TMPL_ARG (args, level, idx);
         }
@@ -9192,7 +9420,9 @@ tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
            len = my_len;
           else if (len != my_len)
             {
-              if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
+             if (!(complain & tf_error))
+               /* Fail quietly.  */;
+              else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
                 error ("mismatched argument pack lengths while expanding "
                        "%<%T%>",
                        pattern);
@@ -9203,6 +9433,13 @@ tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
               return error_mark_node;
             }
 
+         if (TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack)) == 1
+             && PACK_EXPANSION_P (TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg_pack),
+                                                0)))
+           /* This isn't a real argument pack yet.  */;
+         else
+           real_packs = true;
+
           /* Keep track of the parameter packs and their corresponding
              argument packs.  */
           packs = tree_cons (parm_pack, arg_pack, packs);
@@ -9210,32 +9447,64 @@ tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
         }
       else
        {
-         /* We can't substitute for this parameter pack.  */
+         /* We can't substitute for this parameter pack.  We use a flag as
+            well as the missing_level counter because function parameter
+            packs don't have a level.  */
          unsubstituted_packs = true;
-         break;
+         if (!missing_level || missing_level > level)
+           missing_level = level;
        }
     }
 
   /* We cannot expand this expansion expression, because we don't have
-     all of the argument packs we need. Substitute into the pattern
-     and return a PACK_EXPANSION_*. The caller will need to deal with
-     that.  */
+     all of the argument packs we need.  */
   if (unsubstituted_packs)
     {
-      tree new_pat;
-      if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
-       new_pat = tsubst_expr (pattern, args, complain, in_decl,
-                              /*integral_constant_expression_p=*/false);
+      if (real_packs)
+       {
+         /* We got some full packs, but we can't substitute them in until we
+            have values for all the packs.  So remember these until then.  */
+         tree save_args;
+
+         t = make_pack_expansion (pattern);
+
+         /* The call to add_to_template_args above assumes no overlap
+            between saved args and new args, so prune away any fake
+            args, i.e. those that satisfied arg_from_parm_pack_p above.  */
+         if (missing_level && levels >= missing_level)
+           {
+             gcc_assert (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args)
+                         && missing_level > 1);
+             TREE_VEC_LENGTH (args) = missing_level - 1;
+             save_args = copy_node (args);
+             TREE_VEC_LENGTH (args) = levels;
+           }
+         else
+           save_args = args;
+
+         PACK_EXPANSION_EXTRA_ARGS (t) = save_args;
+       }
       else
-       new_pat = tsubst (pattern, args, complain, in_decl);
-      return make_pack_expansion (new_pat);
+       {
+         /* There were no real arguments, we're just replacing a parameter
+            pack with another version of itself. Substitute into the
+            pattern and return a PACK_EXPANSION_*. The caller will need to
+            deal with that.  */
+         if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
+           t = tsubst_expr (pattern, args, complain, in_decl,
+                            /*integral_constant_expression_p=*/false);
+         else
+           t = tsubst (pattern, args, complain, in_decl);
+         t = make_pack_expansion (t);
+       }
+      return t;
     }
 
   /* We could not find any argument packs that work.  */
   if (len < 0)
     return error_mark_node;
 
-  if (cp_unevaluated_operand)
+  if (need_local_specializations)
     {
       /* We're in a late-specified return type, so create our own local
         specializations table; the current table is either NULL or (in the
@@ -9294,7 +9563,7 @@ tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
         }
 
       /* Substitute into the PATTERN with the altered arguments.  */
-      if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
+      if (!TYPE_P (pattern))
         TREE_VEC_ELT (result, i) = 
           tsubst_expr (pattern, args, complain, in_decl,
                        /*integral_constant_expression_p=*/false);
@@ -9330,7 +9599,7 @@ tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
         }
     }
 
-  if (saved_local_specializations)
+  if (need_local_specializations)
     {
       htab_delete (local_specializations);
       local_specializations = saved_local_specializations;
@@ -9831,7 +10100,8 @@ tsubst_decl (tree t, tree args, tsubst_flags_t complain)
 
        DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
 
-       if (TREE_CODE (decl) == TYPE_DECL)
+       if (TREE_CODE (decl) == TYPE_DECL
+           && !TYPE_DECL_ALIAS_P (decl))
          {
            tree new_type;
            ++processing_template_decl;
@@ -10364,8 +10634,15 @@ tsubst_decl (tree t, tree args, tsubst_flags_t complain)
                   referencing a static data member within in its own
                   class.  We can use pointer equality, rather than
                   same_type_p, because DECL_CONTEXT is always
-                  canonical.  */
-               if (ctx == DECL_CONTEXT (t))
+                  canonical...  */
+               if (ctx == DECL_CONTEXT (t)
+                   && (TREE_CODE (t) != TYPE_DECL
+                       /* ... unless T is a member template; in which
+                          case our caller can be willing to create a
+                          specialization of that template represented
+                          by T.  */
+                       || !(DECL_TI_TEMPLATE (t)
+                            && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t)))))
                  spec = t;
              }
 
@@ -10402,7 +10679,9 @@ tsubst_decl (tree t, tree args, tsubst_flags_t complain)
              type = DECL_ORIGINAL_TYPE (t);
            else
              type = TREE_TYPE (t);
-           if (TREE_CODE (t) == VAR_DECL && VAR_HAD_UNKNOWN_BOUND (t))
+           if (TREE_CODE (t) == VAR_DECL
+               && VAR_HAD_UNKNOWN_BOUND (t)
+               && type != error_mark_node)
              type = strip_array_domain (type);
            type = tsubst (type, args, complain, in_decl);
          }
@@ -10846,7 +11125,7 @@ tree
 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
 {
   enum tree_code code;
-  tree type, r;
+  tree type, r = NULL_TREE;
 
   if (t == NULL_TREE || t == error_mark_node
       || t == integer_type_node
@@ -10878,10 +11157,21 @@ tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
       && typedef_variant_p (t))
     {
       tree decl = TYPE_NAME (t);
-      
-      if (DECL_CLASS_SCOPE_P (decl)
-         && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
-         && uses_template_parms (DECL_CONTEXT (decl)))
+
+      if (TYPE_DECL_ALIAS_P (decl)
+         && DECL_LANG_SPECIFIC (decl)
+         && DECL_TEMPLATE_INFO (decl)
+         && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
+       {
+         /* DECL represents an alias template and we want to
+            instantiate it.  Let's substitute our arguments for the
+            template parameters into the declaration and get the
+            resulting type.  */
+         r = tsubst (decl, args, complain, decl);
+       }
+      else if (DECL_CLASS_SCOPE_P (decl)
+              && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
+              && uses_template_parms (DECL_CONTEXT (decl)))
        {
          tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
          tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
@@ -10903,7 +11193,9 @@ tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
             complain | tf_ignore_bad_quals);
          return r;
        }
-      /* Else we must be instantiating the typedef, so fall through.  */
+      else
+       /* We don't have an instantiation yet, so drop the typedef.  */
+       t = DECL_ORIGINAL_TYPE (decl);
     }
 
   if (type
@@ -11031,6 +11323,46 @@ tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
                if (argvec == error_mark_node)
                  return error_mark_node;
 
+               gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
+                           || TREE_CODE (arg) == TEMPLATE_DECL
+                           || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
+
+               if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
+                 /* Consider this code:
+
+                       template <template <class> class Template>
+                       struct Internal {
+                       template <class Arg> using Bind = Template<Arg>;
+                       };
+
+                       template <template <class> class Template, class Arg>
+                       using Instantiate = Template<Arg>; //#0
+
+                       template <template <class> class Template,
+                                  class Argument>
+                       using Bind =
+                         Instantiate<Internal<Template>::template Bind,
+                                     Argument>; //#1
+
+                    When #1 is parsed, the
+                    BOUND_TEMPLATE_TEMPLATE_PARM representing the
+                    parameter `Template' in #0 matches the
+                    UNBOUND_CLASS_TEMPLATE representing the argument
+                    `Internal<Template>::template Bind'; We then want
+                    to assemble the type `Bind<Argument>' that can't
+                    be fully created right now, because
+                    `Internal<Template>' not being complete, the Bind
+                    template cannot be looked up in that context.  So
+                    we need to "store" `Bind<Argument>' for later
+                    when the context of Bind becomes complete.  Let's
+                    store that in a TYPENAME_TYPE.  */
+                 return make_typename_type (TYPE_CONTEXT (arg),
+                                            build_nt (TEMPLATE_ID_EXPR,
+                                                      TYPE_IDENTIFIER (arg),
+                                                      argvec),
+                                            typename_type,
+                                            complain);
+
                /* We can get a TEMPLATE_TEMPLATE_PARM here when we
                   are resolving nested-types in the signature of a
                   member function templates.  Otherwise ARG is a
@@ -11541,6 +11873,7 @@ tsubst_baselink (tree baselink, tree object_type,
     tree optype;
     tree template_args = 0;
     bool template_id_p = false;
+    bool qualified = BASELINK_QUALIFIED_P (baselink);
 
     /* A baselink indicates a function from a base class.  Both the
        BASELINK_ACCESS_BINFO and the base class referenced may
@@ -11589,9 +11922,12 @@ tsubst_baselink (tree baselink, tree object_type,
 
     if (!object_type)
       object_type = current_class_type;
-    return adjust_result_of_qualified_name_lookup (baselink,
-                                                  qualifying_scope,
-                                                  object_type);
+
+    if (qualified)
+      baselink = adjust_result_of_qualified_name_lookup (baselink,
+                                                        qualifying_scope,
+                                                        object_type);
+    return baselink;
 }
 
 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID.  DONE is
@@ -11845,6 +12181,9 @@ tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
       mark_used (t);
       return t;
 
+    case NAMESPACE_DECL:
+      return t;
+
     case OVERLOAD:
       /* An OVERLOAD will always be a non-dependent overload set; an
         overload set from function scope will just be represented with an
@@ -11981,7 +12320,7 @@ tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
                                         base, name,
                                         /*template_p=*/false);
          }
-       else if (TREE_CODE (name) == BASELINK)
+       else if (BASELINK_P (name))
          name = tsubst_baselink (name,
                                  non_reference (TREE_TYPE (object)),
                                  args, complain,
@@ -12292,8 +12631,17 @@ tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
   if (purpose)
     purpose = RECUR (purpose);
   value = TREE_VALUE (t);
-  if (value && TREE_CODE (value) != LABEL_DECL)
-    value = RECUR (value);
+  if (value)
+    {
+      if (TREE_CODE (value) != LABEL_DECL)
+       value = RECUR (value);
+      else
+       {
+         value = lookup_label (DECL_NAME (value));
+         gcc_assert (TREE_CODE (value) == LABEL_DECL);
+         TREE_USED (value) = 1;
+       }
+    }
   chain = TREE_CHAIN (t);
   if (chain && chain != void_type_node)
     chain = RECUR (chain);
@@ -12534,6 +12882,11 @@ tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
                    && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
                  /* Anonymous aggregates are a special case.  */
                  finish_anon_union (decl);
+               else if (is_capture_proxy (DECL_EXPR_DECL (t)))
+                 {
+                   DECL_CONTEXT (decl) = current_function_decl;
+                   insert_capture_proxy (decl);
+                 }
                else
                  {
                    int const_init = false;
@@ -12940,6 +13293,41 @@ tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
        }
       break;
 
+    case TRANSACTION_EXPR:
+      {
+       int flags = 0;
+       flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
+       flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
+
+        if (TRANSACTION_EXPR_IS_STMT (t))
+          {
+           tree body = TRANSACTION_EXPR_BODY (t);
+           tree noex = NULL_TREE;
+           if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
+             {
+               noex = MUST_NOT_THROW_COND (body);
+               if (noex == NULL_TREE)
+                 noex = boolean_true_node;
+               body = TREE_OPERAND (body, 0);
+             }
+            stmt = begin_transaction_stmt (input_location, NULL, flags);
+            RECUR (body);
+            finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
+          }
+        else
+          {
+            stmt = build_transaction_expr (EXPR_LOCATION (t),
+                                          RECUR (TRANSACTION_EXPR_BODY (t)),
+                                          flags, NULL_TREE);
+            return stmt;
+          }
+      }
+      break;
+
+    case MUST_NOT_THROW_EXPR:
+      return build_must_not_throw_expr (RECUR (TREE_OPERAND (t, 0)),
+                                       RECUR (MUST_NOT_THROW_COND (t)));
+
     case EXPR_PACK_EXPANSION:
       error ("invalid use of pack expansion expression");
       return error_mark_node;
@@ -13029,7 +13417,7 @@ tsubst_copy_and_build (tree t,
        decl = finish_id_expression (t, decl, NULL_TREE,
                                     &idk,
                                     integral_constant_expression_p,
-                                    /*allow_non_integral_constant_expression_p=*/false,
+          /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx0x),
                                     &non_integral_constant_expression_p,
                                     /*template_p=*/false,
                                     /*done=*/true,
@@ -13168,6 +13556,10 @@ tsubst_copy_and_build (tree t,
       return build_x_unary_op (TREE_CODE (t), RECUR (TREE_OPERAND (t, 0)),
                                complain);
 
+    case FIX_TRUNC_EXPR:
+      return cp_build_unary_op (FIX_TRUNC_EXPR, RECUR (TREE_OPERAND (t, 0)),
+                               0, complain);
+
     case ADDR_EXPR:
       op1 = TREE_OPERAND (t, 0);
       if (TREE_CODE (op1) == LABEL_DECL)
@@ -13212,18 +13604,23 @@ tsubst_copy_and_build (tree t,
     case GT_EXPR:
     case MEMBER_REF:
     case DOTSTAR_EXPR:
-      return build_x_binary_op
-       (TREE_CODE (t),
-        RECUR (TREE_OPERAND (t, 0)),
-        (TREE_NO_WARNING (TREE_OPERAND (t, 0))
-         ? ERROR_MARK
-         : TREE_CODE (TREE_OPERAND (t, 0))),
-        RECUR (TREE_OPERAND (t, 1)),
-        (TREE_NO_WARNING (TREE_OPERAND (t, 1))
-         ? ERROR_MARK
-         : TREE_CODE (TREE_OPERAND (t, 1))),
-        /*overload=*/NULL,
-        complain);
+      {
+       tree r = build_x_binary_op
+         (TREE_CODE (t),
+          RECUR (TREE_OPERAND (t, 0)),
+          (TREE_NO_WARNING (TREE_OPERAND (t, 0))
+           ? ERROR_MARK
+           : TREE_CODE (TREE_OPERAND (t, 0))),
+          RECUR (TREE_OPERAND (t, 1)),
+          (TREE_NO_WARNING (TREE_OPERAND (t, 1))
+           ? ERROR_MARK
+           : TREE_CODE (TREE_OPERAND (t, 1))),
+          /*overload=*/NULL,
+          complain);
+       if (EXPR_P (r) && TREE_NO_WARNING (t))
+         TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
+       return r;
+      }
 
     case SCOPE_REF:
       return tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
@@ -13499,9 +13896,14 @@ tsubst_copy_and_build (tree t,
                tree unq = (tsubst_copy_and_build
                            (function, args, complain, in_decl, true,
                             integral_constant_expression_p));
+               if (unq == error_mark_node)
+                 return error_mark_node;
+
                if (unq != function)
                  {
                    tree fn = unq;
+                   if (TREE_CODE (fn) == INDIRECT_REF)
+                     fn = TREE_OPERAND (fn, 0);
                    if (TREE_CODE (fn) == COMPONENT_REF)
                      fn = TREE_OPERAND (fn, 1);
                    if (is_overloaded_fn (fn))
@@ -13511,7 +13913,9 @@ tsubst_copy_and_build (tree t,
                               "and no declarations were found by "
                               "argument-dependent lookup at the point "
                               "of instantiation", function);
-                   if (DECL_CLASS_SCOPE_P (fn))
+                   if (!DECL_P (fn))
+                     /* Can't say anything more.  */;
+                   else if (DECL_CLASS_SCOPE_P (fn))
                      {
                        inform (EXPR_LOC_OR_HERE (t),
                                "declarations in dependent base %qT are "
@@ -13703,7 +14107,9 @@ tsubst_copy_and_build (tree t,
        if (member == error_mark_node)
          return error_mark_node;
 
-       if (object_type && !CLASS_TYPE_P (object_type))
+       if (type_dependent_expression_p (object))
+         /* We can't do much here.  */;
+       else if (!CLASS_TYPE_P (object_type))
          {
            if (SCALAR_TYPE_P (object_type))
              {
@@ -13726,14 +14132,12 @@ tsubst_copy_and_build (tree t,
        else if (TREE_CODE (member) == SCOPE_REF
                 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
          {
-           tree tmpl;
-           tree args;
-
            /* Lookup the template functions now that we know what the
               scope is.  */
-           tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
-           args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
-           member = lookup_qualified_name (TREE_OPERAND (member, 0), tmpl,
+           tree scope = TREE_OPERAND (member, 0);
+           tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
+           tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
+           member = lookup_qualified_name (scope, tmpl,
                                            /*is_type_p=*/false,
                                            /*complain=*/false);
            if (BASELINK_P (member))
@@ -13747,7 +14151,7 @@ tsubst_copy_and_build (tree t,
              }
            else
              {
-               qualified_name_lookup_error (object_type, tmpl, member,
+               qualified_name_lookup_error (scope, tmpl, member,
                                             input_location);
                return error_mark_node;
              }
@@ -13996,6 +14400,10 @@ tsubst_copy_and_build (tree t,
        return r;
       }
 
+    case TRANSACTION_EXPR:
+      return tsubst_expr(t, args, complain, in_decl,
+            integral_constant_expression_p);
+
     default:
       /* Handle Objective-C++ constructs, if appropriate.  */
       {
@@ -14164,7 +14572,6 @@ instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
   tree fndecl;
   tree gen_tmpl;
   tree spec;
-  HOST_WIDE_INT saved_processing_template_decl;
 
   if (tmpl == error_mark_node)
     return error_mark_node;
@@ -14225,18 +14632,22 @@ instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
      deferring all checks until we have the FUNCTION_DECL.  */
   push_deferring_access_checks (dk_deferred);
 
-  /* Although PROCESSING_TEMPLATE_DECL may be true at this point
-     (because, for example, we have encountered a non-dependent
-     function call in the body of a template function and must now
-     determine which of several overloaded functions will be called),
-     within the instantiation itself we are not processing a
-     template.  */  
-  saved_processing_template_decl = processing_template_decl;
-  processing_template_decl = 0;
+  /* Instantiation of the function happens in the context of the function
+     template, not the context of the overload resolution we're doing.  */
+  push_to_top_level ();
+  if (DECL_CLASS_SCOPE_P (gen_tmpl))
+    {
+      tree ctx = tsubst (DECL_CONTEXT (gen_tmpl), targ_ptr,
+                        complain, gen_tmpl);
+      push_nested_class (ctx);
+    }
   /* Substitute template parameters to obtain the specialization.  */
   fndecl = tsubst (DECL_TEMPLATE_RESULT (gen_tmpl),
                   targ_ptr, complain, gen_tmpl);
-  processing_template_decl = saved_processing_template_decl;
+  if (DECL_CLASS_SCOPE_P (gen_tmpl))
+    pop_nested_class ();
+  pop_from_top_level ();
+
   if (fndecl == error_mark_node)
     return error_mark_node;
 
@@ -14982,9 +15393,13 @@ type_unification_real (tree tparms,
            {
              tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
              tree arg = TREE_PURPOSE (TREE_VEC_ELT (tparms, i));
+             location_t save_loc = input_location;
+             if (DECL_P (parm))
+               input_location = DECL_SOURCE_LOCATION (parm);
              arg = tsubst_template_arg (arg, targs, complain, NULL_TREE);
              arg = convert_template_argument (parm, arg, targs, complain,
                                               i, NULL_TREE);
+             input_location = save_loc;
              if (arg == error_mark_node)
                return 1;
              else
@@ -15097,7 +15512,7 @@ resolve_overloaded_unification (tree tparms,
              elem = tsubst (TREE_TYPE (fn), subargs, tf_none, NULL_TREE);
              if (try_one_overload (tparms, targs, tempargs, parm,
                                    elem, strict, sub_strict, addr_p, explain_p)
-                 && (!goodfn || !decls_match (goodfn, elem)))
+                 && (!goodfn || !same_type_p (goodfn, elem)))
                {
                  goodfn = elem;
                  ++good;
@@ -15182,7 +15597,7 @@ resolve_nondeduced_context (tree orig_expr)
       offset = expr;
       expr = TREE_OPERAND (expr, 1);
     }
-  if (TREE_CODE (expr) == BASELINK)
+  if (BASELINK_P (expr))
     {
       baselink = expr;
       expr = BASELINK_FUNCTIONS (expr);
@@ -15702,7 +16117,7 @@ unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
         }
       else
        {
-         tree bad_old_arg, bad_new_arg;
+         tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
          tree old_args = ARGUMENT_PACK_ARGS (old_pack);
 
          if (!comp_template_args_with_info (old_args, new_args,
@@ -15898,6 +16313,8 @@ unify (tree tparms, tree targs, tree parm, tree arg, int strict,
       idx = TEMPLATE_TYPE_IDX (parm);
       targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
       tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
+      if (tparm == error_mark_node)
+       return unify_invalid (explain_p);
 
       /* Check for mixed types and values.  */
       if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
@@ -16258,6 +16675,7 @@ unify (tree tparms, tree targs, tree parm, tree arg, int strict,
     case BOOLEAN_TYPE:
     case ENUMERAL_TYPE:
     case VOID_TYPE:
+    case NULLPTR_TYPE:
       if (TREE_CODE (arg) != TREE_CODE (parm))
        return unify_type_mismatch (explain_p, parm, arg);
 
@@ -16510,7 +16928,7 @@ unify (tree tparms, tree targs, tree parm, tree arg, int strict,
 
     default:
       /* An unresolved overload is a nondeduced context.  */
-      if (type_unknown_p (parm))
+      if (is_overloaded_fn (parm) || type_unknown_p (parm))
        return unify_success (explain_p);
       gcc_assert (EXPR_P (parm));
 
@@ -17593,7 +18011,12 @@ 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 %qT", t);
+      tree tmpl =
+       (TYPE_TEMPLATE_INFO (t)) ? TYPE_TI_TEMPLATE (t) : NULL;
+      if (tmpl)
+       error ("explicit instantiation of non-class template %qD", tmpl);
+      else
+       error ("explicit instantiation of non-template type %qT", t);
       return;
     }
 
@@ -17950,17 +18373,22 @@ maybe_instantiate_noexcept (tree fn)
 
   if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
     {
-      push_tinst_level (fn);
-      push_access_scope (fn);
-      input_location = DECL_SOURCE_LOCATION (fn);
-      noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
-                                   DEFERRED_NOEXCEPT_ARGS (noex),
-                                   tf_warning_or_error, fn, /*function_p=*/false,
-                                   /*integral_constant_expression_p=*/true);
-      pop_access_scope (fn);
-      pop_tinst_level ();
-      spec = build_noexcept_spec (noex, tf_warning_or_error);
-      if (spec == error_mark_node)
+      if (push_tinst_level (fn))
+       {
+         push_access_scope (fn);
+         input_location = DECL_SOURCE_LOCATION (fn);
+         noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
+                                       DEFERRED_NOEXCEPT_ARGS (noex),
+                                       tf_warning_or_error, fn,
+                                       /*function_p=*/false,
+                                       /*integral_constant_expression_p=*/true);
+         pop_access_scope (fn);
+         pop_tinst_level ();
+         spec = build_noexcept_spec (noex, tf_warning_or_error);
+         if (spec == error_mark_node)
+           spec = noexcept_false_spec;
+       }
+      else
        spec = noexcept_false_spec;
     }
   else
@@ -18033,6 +18461,8 @@ instantiate_decl (tree d, int defer_ok,
     d = DECL_CLONED_FUNCTION (d);
 
   if (DECL_TEMPLATE_INSTANTIATED (d)
+      || (TREE_CODE (d) == FUNCTION_DECL
+         && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
       || DECL_TEMPLATE_SPECIALIZATION (d))
     /* D has already been instantiated or explicitly specialized, so
        there's nothing for us to do here.
@@ -18507,6 +18937,7 @@ tsubst_initializer_list (tree t, tree argvec)
           /* Build a dummy EXPR_PACK_EXPANSION that will be used to
              expand each argument in the TREE_VALUE of t.  */
           expr = make_node (EXPR_PACK_EXPANSION);
+         PACK_EXPANSION_LOCAL_P (expr) = true;
           PACK_EXPANSION_PARAMETER_PACKS (expr) =
             PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
 
@@ -18567,6 +18998,7 @@ tsubst_initializer_list (tree t, tree argvec)
             }
           else
             {
+             tree tmp;
               decl = tsubst_copy (TREE_PURPOSE (t), argvec, 
                                   tf_warning_or_error, NULL_TREE);
 
@@ -18575,10 +19007,17 @@ tsubst_initializer_list (tree t, tree argvec)
                 in_base_initializer = 1;
 
              init = TREE_VALUE (t);
+             tmp = init;
              if (init != void_type_node)
                init = tsubst_expr (init, argvec,
                                    tf_warning_or_error, NULL_TREE,
                                    /*integral_constant_expression_p=*/false);
+             if (init == NULL_TREE && tmp != NULL_TREE)
+               /* If we had an initializer but it instantiated to nothing,
+                  value-initialize the object.  This will only occur when
+                  the initializer was a pack expansion where the parameter
+                  packs used in that expansion were of length zero.  */
+               init = void_type_node;
               in_base_initializer = 0;
             }
 
@@ -18758,7 +19197,13 @@ invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
     return 0;
 
   if (complain & tf_error)
-    error ("%q#T is not a valid type for a template constant parameter", type);
+    {
+      if (type == error_mark_node)
+       inform (input_location, "invalid template non-type parameter");
+      else
+       error ("%q#T is not a valid type for a template non-type parameter",
+              type);
+    }
   return 1;
 }
 
@@ -19131,6 +19576,11 @@ value_dependent_expression_p (tree expression)
        return false;
       }
 
+    case STMT_EXPR:
+      /* Treat a GNU statement expression as dependent to avoid crashing
+        under fold_non_dependent_expr; it can't be constant.  */
+      return true;
+
     default:
       /* A constant expression is value-dependent if any subexpression is
         value-dependent.  */
@@ -19297,7 +19747,7 @@ type_dependent_expression_p (tree expression)
       if (TREE_CODE (expression) == SCOPE_REF)
        return false;
 
-      if (TREE_CODE (expression) == BASELINK)
+      if (BASELINK_P (expression))
        expression = BASELINK_FUNCTIONS (expression);
 
       if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
@@ -19657,7 +20107,8 @@ resolve_typename_type (tree type, bool only_current_p)
      longer be considered a dependent type.  */
   pushed_scope = push_scope (scope);
   /* Look up the declaration.  */
-  decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true);
+  decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
+                       tf_warning_or_error);
 
   result = NULL_TREE;