pt.c (value_dependent_expression_p): Tweak new cases to better match the wording...
[gcc.git] / gcc / cp / pt.c
index 403c5ac0bea813d7cc8c28b2362aac4aa5f8744d..fde3091951747e7f9ed4c7195b41baa71ade989a 100644 (file)
@@ -178,6 +178,7 @@ static int check_cv_quals_for_unify (int, tree, tree);
 static void template_parm_level_and_index (tree, int*, int*);
 static int unify_pack_expansion (tree, tree, tree,
                                 tree, unification_kind_t, bool, bool);
+static tree copy_template_args (tree);
 static tree tsubst_template_arg (tree, tree, tsubst_flags_t, tree);
 static tree tsubst_template_args (tree, tree, tsubst_flags_t, tree);
 static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
@@ -214,6 +215,7 @@ static tree listify_autos (tree, tree);
 static tree tsubst_template_parm (tree, tree, tsubst_flags_t);
 static tree instantiate_alias_template (tree, tree, tsubst_flags_t);
 static bool complex_alias_template_p (const_tree tmpl);
+static tree tsubst_attributes (tree, tree, tsubst_flags_t, tree);
 
 /* Make the current scope suitable for access checking when we are
    processing T.  T can be FUNCTION_DECL for instantiated function
@@ -328,7 +330,8 @@ get_template_info (const_tree t)
   if (!t || t == error_mark_node)
     return NULL;
 
-  if (TREE_CODE (t) == NAMESPACE_DECL)
+  if (TREE_CODE (t) == NAMESPACE_DECL
+      || TREE_CODE (t) == PARM_DECL)
     return NULL;
 
   if (DECL_P (t) && DECL_LANG_SPECIFIC (t))
@@ -368,16 +371,20 @@ template_class_depth (tree type)
 {
   int depth;
 
-  for (depth = 0;
-       type && TREE_CODE (type) != NAMESPACE_DECL;
-       type = (TREE_CODE (type) == FUNCTION_DECL)
-        ? CP_DECL_CONTEXT (type) : CP_TYPE_CONTEXT (type))
+  for (depth = 0; type && TREE_CODE (type) != NAMESPACE_DECL; )
     {
       tree tinfo = get_template_info (type);
 
       if (tinfo && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
          && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo))))
        ++depth;
+
+      if (DECL_P (type))
+       type = CP_DECL_CONTEXT (type);
+      else if (LAMBDA_TYPE_P (type))
+       type = LAMBDA_TYPE_EXTRA_SCOPE (type);
+      else
+       type = CP_TYPE_CONTEXT (type);
     }
 
   return depth;
@@ -3689,6 +3696,8 @@ make_pack_expansion (tree arg)
       /* Propagate type and const-expression information.  */
       TREE_TYPE (result) = TREE_TYPE (arg);
       TREE_CONSTANT (result) = TREE_CONSTANT (arg);
+      /* Mark this read now, since the expansion might be length 0.  */
+      mark_exp_read (arg);
     }
   else
     /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
@@ -5648,8 +5657,7 @@ instantiate_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
 
      as two declarations of the same function, for example.  */
   if (processing_template_decl
-      && !instantiation_dependent_expression_p (expr)
-      && potential_constant_expression (expr))
+      && potential_nondependent_constant_expression (expr))
     {
       processing_template_decl_sentinel s;
       expr = instantiate_non_dependent_expr_internal (expr, complain);
@@ -5673,8 +5681,7 @@ instantiate_non_dependent_or_null (tree expr)
     return NULL_TREE;
   if (processing_template_decl)
     {
-      if (instantiation_dependent_expression_p (expr)
-         || !potential_constant_expression (expr))
+      if (!potential_nondependent_constant_expression (expr))
        expr = NULL_TREE;
       else
        {
@@ -6233,10 +6240,8 @@ convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
   if (TYPE_REF_OBJ_P (type)
       && has_value_dependent_address (expr))
     /* If we want the address and it's value-dependent, don't fold.  */;
-  else if (!type_unknown_p (expr)
-          && processing_template_decl
-          && !instantiation_dependent_expression_p (expr)
-          && potential_constant_expression (expr))
+  else if (processing_template_decl
+          && potential_nondependent_constant_expression (expr))
     non_dep = true;
   if (error_operand_p (expr))
     return error_mark_node;
@@ -6943,7 +6948,8 @@ canonicalize_type_argument (tree arg, tsubst_flags_t complain)
   tree canon = strip_typedefs (arg, &removed_attributes);
   if (removed_attributes
       && (complain & tf_warning))
-    warning (0, "ignoring attributes on template argument %qT", arg);
+    warning (OPT_Wignored_attributes,
+            "ignoring attributes on template argument %qT", arg);
   return canon;
 }
 
@@ -8398,6 +8404,8 @@ lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
              t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
                              tsubst (ENUM_UNDERLYING_TYPE (template_type),
                                      arglist, complain, in_decl),
+                             tsubst_attributes (TYPE_ATTRIBUTES (template_type),
+                                                arglist, complain, in_decl),
                              SCOPED_ENUM_P (template_type), NULL);
 
              if (t == error_mark_node)
@@ -8479,11 +8487,7 @@ lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
              tree attributes
                = lookup_attribute (tags[ix], TYPE_ATTRIBUTES (template_type));
 
-             if (!attributes)
-               ;
-             else if (!TREE_CHAIN (attributes) && !TYPE_ATTRIBUTES (t))
-               TYPE_ATTRIBUTES (t) = attributes;
-             else
+             if (attributes)
                TYPE_ATTRIBUTES (t)
                  = tree_cons (TREE_PURPOSE (attributes),
                               TREE_VALUE (attributes),
@@ -8548,6 +8552,8 @@ lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
                    arglist, complain, NULL_TREE);
          --processing_template_decl;
          TREE_VEC_LENGTH (arglist)++;
+         if (partial_inst_args == error_mark_node)
+           return error_mark_node;
          use_partial_inst_tmpl =
            /*...and we must not be looking at the partial instantiation
             itself. */
@@ -8695,6 +8701,24 @@ finish_template_variable (tree var, tsubst_flags_t complain)
 
   return instantiate_template (templ, arglist, complain);
 }
+
+/* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
+   TARGS template args, and instantiate it if it's not dependent.  */
+
+static tree
+lookup_and_finish_template_variable (tree templ, tree targs,
+                                    tsubst_flags_t complain)
+{
+  templ = lookup_template_variable (templ, targs);
+  if (!any_dependent_template_arguments_p (targs))
+    {
+      templ = finish_template_variable (templ, complain);
+      mark_used (templ);
+    }
+
+  return convert_from_reference (templ);
+}
+
 \f
 struct pair_fn_data
 {
@@ -8826,8 +8850,9 @@ for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
       break;
 
     case TYPENAME_TYPE:
-      if (!fn)
-       WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t));
+      /* A template-id in a TYPENAME_TYPE might be a deduced context after
+        partial instantiation.  */
+      WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t));
       break;
 
     case CONSTRUCTOR:
@@ -9538,7 +9563,111 @@ can_complete_type_without_circularity (tree type)
     return 1;
 }
 
-static tree tsubst_omp_clauses (tree, bool, bool, tree, tsubst_flags_t, tree);
+static tree tsubst_omp_clauses (tree, enum c_omp_region_type, tree,
+                               tsubst_flags_t, tree);
+
+/* Instantiate a single dependent attribute T (a TREE_LIST), and return either
+   T or a new TREE_LIST, possibly a chain in the case of a pack expansion.  */
+
+static tree
+tsubst_attribute (tree t, tree *decl_p, tree args,
+                 tsubst_flags_t complain, tree in_decl)
+{
+  gcc_assert (ATTR_IS_DEPENDENT (t));
+
+  tree val = TREE_VALUE (t);
+  if (val == NULL_TREE)
+    /* Nothing to do.  */;
+  else if ((flag_openmp || flag_openmp_simd || flag_cilkplus)
+          && is_attribute_p ("omp declare simd",
+                             get_attribute_name (t)))
+    {
+      tree clauses = TREE_VALUE (val);
+      clauses = tsubst_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD, args,
+                                   complain, in_decl);
+      c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
+      clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
+      tree parms = DECL_ARGUMENTS (*decl_p);
+      clauses
+       = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
+      if (clauses)
+       val = build_tree_list (NULL_TREE, clauses);
+      else
+       val = NULL_TREE;
+    }
+  /* If the first attribute argument is an identifier, don't
+     pass it through tsubst.  Attributes like mode, format,
+     cleanup and several target specific attributes expect it
+     unmodified.  */
+  else if (attribute_takes_identifier_p (get_attribute_name (t)))
+    {
+      tree chain
+       = tsubst_expr (TREE_CHAIN (val), args, complain, in_decl,
+                      /*integral_constant_expression_p=*/false);
+      if (chain != TREE_CHAIN (val))
+       val = tree_cons (NULL_TREE, TREE_VALUE (val), chain);
+    }
+  else if (PACK_EXPANSION_P (val))
+    {
+      /* An attribute pack expansion.  */
+      tree purp = TREE_PURPOSE (t);
+      tree pack = tsubst_pack_expansion (val, args, complain, in_decl);
+      int len = TREE_VEC_LENGTH (pack);
+      tree list = NULL_TREE;
+      tree *q = &list;
+      for (int i = 0; i < len; ++i)
+       {
+         tree elt = TREE_VEC_ELT (pack, i);
+         *q = build_tree_list (purp, elt);
+         q = &TREE_CHAIN (*q);
+       }
+      return list;
+    }
+  else
+    val = tsubst_expr (val, args, complain, in_decl,
+                      /*integral_constant_expression_p=*/false);
+
+  if (val != TREE_VALUE (t))
+    return build_tree_list (TREE_PURPOSE (t), val);
+  return t;
+}
+
+/* Instantiate any dependent attributes in ATTRIBUTES, returning either it
+   unchanged or a new TREE_LIST chain.  */
+
+static tree
+tsubst_attributes (tree attributes, tree args,
+                  tsubst_flags_t complain, tree in_decl)
+{
+  tree last_dep = NULL_TREE;
+
+  for (tree t = attributes; t; t = TREE_CHAIN (t))
+    if (ATTR_IS_DEPENDENT (t))
+      {
+       last_dep = t;
+       attributes = copy_list (attributes);
+       break;
+      }
+
+  if (last_dep)
+    for (tree *p = &attributes; *p; p = &TREE_CHAIN (*p))
+      {
+       tree t = *p;
+       if (ATTR_IS_DEPENDENT (t))
+         {
+           tree subst = tsubst_attribute (t, NULL, args, complain, in_decl);
+           if (subst == t)
+             continue;
+           *p = subst;
+           do
+             p = &TREE_CHAIN (*p);
+           while (*p);
+           *p = TREE_CHAIN (t);
+         }
+      }
+
+  return attributes;
+}
 
 /* Apply any attributes which had to be deferred until instantiation
    time.  DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
@@ -9581,61 +9710,10 @@ apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
            {
              *p = TREE_CHAIN (t);
              TREE_CHAIN (t) = NULL_TREE;
-             if ((flag_openmp || flag_openmp_simd || flag_cilkplus)
-                 && is_attribute_p ("omp declare simd",
-                                    get_attribute_name (t))
-                 && TREE_VALUE (t))
-               {
-                 tree clauses = TREE_VALUE (TREE_VALUE (t));
-                 clauses = tsubst_omp_clauses (clauses, true, false, args,
-                                               complain, in_decl);
-                 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
-                 clauses = finish_omp_clauses (clauses, false, true);
-                 tree parms = DECL_ARGUMENTS (*decl_p);
-                 clauses
-                   = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
-                 if (clauses)
-                   TREE_VALUE (TREE_VALUE (t)) = clauses;
-                 else
-                   TREE_VALUE (t) = NULL_TREE;
-               }
-             /* If the first attribute argument is an identifier, don't
-                pass it through tsubst.  Attributes like mode, format,
-                cleanup and several target specific attributes expect it
-                unmodified.  */
-             else if (attribute_takes_identifier_p (get_attribute_name (t))
-                      && TREE_VALUE (t))
-               {
-                 tree chain
-                   = tsubst_expr (TREE_CHAIN (TREE_VALUE (t)), args, complain,
-                                  in_decl,
-                                  /*integral_constant_expression_p=*/false);
-                 if (chain != TREE_CHAIN (TREE_VALUE (t)))
-                   TREE_VALUE (t)
-                     = tree_cons (NULL_TREE, TREE_VALUE (TREE_VALUE (t)),
-                                  chain);
-               }
-             else if (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t)))
-               {
-                 /* An attribute pack expansion.  */
-                 tree purp = TREE_PURPOSE (t);
-                 tree pack = (tsubst_pack_expansion
-                              (TREE_VALUE (t), args, complain, in_decl));
-                 int len = TREE_VEC_LENGTH (pack);
-                 for (int i = 0; i < len; ++i)
-                   {
-                     tree elt = TREE_VEC_ELT (pack, i);
-                     *q = build_tree_list (purp, elt);
-                     q = &TREE_CHAIN (*q);
-                   }
-                 continue;
-               }
-             else
-               TREE_VALUE (t)
-                 = tsubst_expr (TREE_VALUE (t), args, complain, in_decl,
-                                /*integral_constant_expression_p=*/false);
-             *q = t;
-             q = &TREE_CHAIN (t);
+             *q = tsubst_attribute (t, decl_p, args, complain, in_decl);
+             do
+               q = &TREE_CHAIN (*q);
+             while (*q);
            }
          else
            p = &TREE_CHAIN (t);
@@ -9781,7 +9859,7 @@ instantiate_class_template_1 (tree type)
     DECL_SOURCE_LOCATION (typedecl);
 
   TYPE_PACKED (type) = TYPE_PACKED (pattern);
-  TYPE_ALIGN (type) = TYPE_ALIGN (pattern);
+  SET_TYPE_ALIGN (type, TYPE_ALIGN (pattern));
   TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
   TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern); /* For libjava's JArray<T> */
   if (ANON_AGGR_TYPE_P (pattern))
@@ -10109,11 +10187,11 @@ instantiate_class_template_1 (tree type)
                       template <class U> friend class T::C;
 
                     otherwise.  */
+                 /* Bump processing_template_decl in case this is something like
+                    template <class T> friend struct A<T>::B.  */
+                 ++processing_template_decl;
                  friend_type = tsubst (friend_type, args,
                                        tf_warning_or_error, NULL_TREE);
-                 /* Bump processing_template_decl for correct
-                    dependent_type_p calculation.  */
-                 ++processing_template_decl;
                  if (dependent_type_p (friend_type))
                    adjust_processing_template_decl = true;
                  --processing_template_decl;
@@ -10552,10 +10630,6 @@ gen_elem_of_pack_expansion_instantiation (tree pattern,
    sequence, the value of the expression is as follows; the program is
    ill-formed if the operator is not listed in this table.
 
-   *   1
-   +   0
-   &   -1
-   |   0
    &&  true
    ||  false
    ,   void()  */
@@ -10567,14 +10641,6 @@ expand_empty_fold (tree t, tsubst_flags_t complain)
   if (!FOLD_EXPR_MODIFY_P (t))
     switch (code)
       {
-      case MULT_EXPR:
-       return integer_one_node;
-      case PLUS_EXPR:
-       return integer_zero_node;
-      case BIT_AND_EXPR:
-       return integer_minus_one_node;
-      case BIT_IOR_EXPR:
-       return integer_zero_node;
       case TRUTH_ANDIF_EXPR:
        return boolean_true_node;
       case TRUTH_ORIF_EXPR:
@@ -10898,6 +10964,7 @@ tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
          /* 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.  */
+         gcc_assert (processing_template_decl);
          unsubstituted_packs = true;
        }
     }
@@ -10966,11 +11033,12 @@ tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
   /* For each argument in each argument pack, substitute into the
      pattern.  */
   result = make_tree_vec (len);
+  tree elem_args = copy_template_args (args);
   for (i = 0; i < len; ++i)
     {
       t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
                                                    i,
-                                                   args, complain,
+                                                   elem_args, complain,
                                                    in_decl);
       TREE_VEC_ELT (result, i) = t;
       if (t == error_mark_node)
@@ -11046,6 +11114,51 @@ get_pattern_parm (tree parm, tree tmpl)
   return patparm;
 }
 
+/* Make an argument pack out of the TREE_VEC VEC.  */
+
+static tree
+make_argument_pack (tree vec)
+{
+  tree pack;
+  tree elt = TREE_VEC_ELT (vec, 0);
+  if (TYPE_P (elt))
+    pack = cxx_make_type (TYPE_ARGUMENT_PACK);
+  else
+    {
+      pack = make_node (NONTYPE_ARGUMENT_PACK);
+      TREE_TYPE (pack) = TREE_TYPE (elt);
+      TREE_CONSTANT (pack) = 1;
+    }
+  SET_ARGUMENT_PACK_ARGS (pack, vec);
+  return pack;
+}
+
+/* Return an exact copy of template args T that can be modified
+   independently.  */
+
+static tree
+copy_template_args (tree t)
+{
+  if (t == error_mark_node)
+    return t;
+
+  int len = TREE_VEC_LENGTH (t);
+  tree new_vec = make_tree_vec (len);
+
+  for (int i = 0; i < len; ++i)
+    {
+      tree elt = TREE_VEC_ELT (t, i);
+      if (elt && TREE_CODE (elt) == TREE_VEC)
+       elt = copy_template_args (elt);
+      TREE_VEC_ELT (new_vec, i) = elt;
+    }
+
+  NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec)
+    = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
+
+  return new_vec;
+}
+
 /* Substitute ARGS into the vector or list of template arguments T.  */
 
 static tree
@@ -11587,16 +11700,9 @@ tsubst_decl (tree t, tree args, tsubst_flags_t complain)
        if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
          {
            tree spec;
-           bool dependent_p;
 
-           /* If T is not dependent, just return it.  We have to
-              increment PROCESSING_TEMPLATE_DECL because
-              value_dependent_expression_p assumes that nothing is
-              dependent when PROCESSING_TEMPLATE_DECL is zero.  */
-           ++processing_template_decl;
-           dependent_p = value_dependent_expression_p (t);
-           --processing_template_decl;
-           if (!dependent_p)
+           /* If T is not dependent, just return it.  */
+           if (!uses_template_parms (DECL_TI_ARGS (t)))
              RETURN (t);
 
            /* Calculate the most general template of which R is a
@@ -12262,6 +12368,8 @@ tsubst_decl (tree t, tree args, tsubst_flags_t complain)
        /* The initializer must not be expanded until it is required;
           see [temp.inst].  */
        DECL_INITIAL (r) = NULL_TREE;
+       if (VAR_P (r))
+         DECL_MODE (r) = VOIDmode;
        if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
          SET_DECL_RTL (r, NULL);
        DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
@@ -12795,14 +12903,9 @@ tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
       if (t == integer_type_node)
        return t;
 
-      if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST)
-        {
-          if (!TYPE_MAX_VALUE (t))
-            return compute_array_index_type (NULL_TREE, NULL_TREE, complain);
-          
-          if (TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
-            return t;
-        }
+      if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
+          && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
+        return t;
 
       {
        tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
@@ -13297,7 +13400,7 @@ tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
 
        if (TYPE_USER_ALIGN (t))
          {
-           TYPE_ALIGN (r) = TYPE_ALIGN (t);
+           SET_TYPE_ALIGN (r, TYPE_ALIGN (t));
            TYPE_USER_ALIGN (r) = 1;
          }
 
@@ -13537,7 +13640,15 @@ tsubst_baselink (tree baselink, tree object_type,
       name = mangle_conv_op_name_for_type (optype);
     baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
     if (!baselink)
-      return error_mark_node;
+      {
+       if (constructor_name_p (name, qualifying_scope))
+         {
+           if (complain & tf_error)
+             error ("cannot call constructor %<%T::%D%> directly",
+                    qualifying_scope, name);
+         }
+       return error_mark_node;
+      }
 
     /* If lookup found a single function, mark it as used at this
        point.  (If it lookup found multiple functions the one selected
@@ -13552,9 +13663,10 @@ tsubst_baselink (tree baselink, tree object_type,
     /* Add back the template arguments, if present.  */
     if (BASELINK_P (baselink) && template_id_p)
       BASELINK_FUNCTIONS (baselink)
-       = build_nt (TEMPLATE_ID_EXPR,
-                   BASELINK_FUNCTIONS (baselink),
-                   template_args);
+       = build2 (TEMPLATE_ID_EXPR,
+                 unknown_type_node,
+                 BASELINK_FUNCTIONS (baselink),
+                 template_args);
     /* Update the conversion operator type.  */
     BASELINK_OPTYPE (baselink) = optype;
 
@@ -13676,7 +13788,13 @@ tsubst_qualified_id (tree qualified_id, tree args,
     }
 
   if (is_template)
-    expr = lookup_template_function (expr, template_args);
+    {
+      if (variable_template_p (expr))
+       expr = lookup_and_finish_template_variable (expr, template_args,
+                                                   complain);
+      else
+       expr = lookup_template_function (expr, template_args);
+    }
 
   if (expr == error_mark_node && complain & tf_error)
     qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
@@ -13754,10 +13872,13 @@ tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
 
       if (r == NULL_TREE)
        {
-         /* We get here for a use of 'this' in an NSDMI.  */
+         /* We get here for a use of 'this' in an NSDMI as part of a
+            constructor call or as part of an aggregate initialization.  */
          if (DECL_NAME (t) == this_identifier
-             && current_function_decl
-             && DECL_CONSTRUCTOR_P (current_function_decl))
+             && ((current_function_decl
+                  && DECL_CONSTRUCTOR_P (current_function_decl))
+                 || (current_class_ref
+                     && TREE_CODE (current_class_ref) == PLACEHOLDER_EXPR)))
            return current_class_ptr;
 
          /* This can happen for a parameter name used later in a function
@@ -13994,7 +14115,8 @@ tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
       }
 
     case SIZEOF_EXPR:
-      if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
+      if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
+         || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
         {
           tree expanded, op = TREE_OPERAND (t, 0);
          int len = 0;
@@ -14005,24 +14127,35 @@ tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
          ++cp_unevaluated_operand;
          ++c_inhibit_evaluation_warnings;
          /* We only want to compute the number of arguments.  */
-         expanded = tsubst_pack_expansion (op, args, complain, in_decl);
+         if (PACK_EXPANSION_P (op))
+           expanded = tsubst_pack_expansion (op, args, complain, in_decl);
+         else
+           expanded = tsubst_template_args (ARGUMENT_PACK_ARGS (op),
+                                            args, complain, in_decl);
          --cp_unevaluated_operand;
          --c_inhibit_evaluation_warnings;
 
          if (TREE_CODE (expanded) == TREE_VEC)
-           len = TREE_VEC_LENGTH (expanded);
+           {
+             len = TREE_VEC_LENGTH (expanded);
+             /* Set TREE_USED for the benefit of -Wunused.  */
+             for (int i = 0; i < len; i++)
+               TREE_USED (TREE_VEC_ELT (expanded, i)) = true;
+           }
 
          if (expanded == error_mark_node)
            return error_mark_node;
          else if (PACK_EXPANSION_P (expanded)
                   || (TREE_CODE (expanded) == TREE_VEC
-                      && len > 0
-                      && PACK_EXPANSION_P (TREE_VEC_ELT (expanded, len-1))))
+                      && pack_expansion_args_count (expanded)))
+
            {
-             if (TREE_CODE (expanded) == TREE_VEC)
-               expanded = TREE_VEC_ELT (expanded, len - 1);
+             if (PACK_EXPANSION_P (expanded))
+               /* OK.  */;
+             else if (TREE_VEC_LENGTH (expanded) == 1)
+               expanded = TREE_VEC_ELT (expanded, 0);
              else
-               PACK_EXPANSION_SIZEOF_P (expanded) = true;
+               expanded = make_argument_pack (expanded);
 
              if (TYPE_P (expanded))
                return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR, 
@@ -14396,7 +14529,7 @@ tsubst_omp_clause_decl (tree decl, tree args, tsubst_flags_t complain,
 /* Like tsubst_copy, but specifically for OpenMP clauses.  */
 
 static tree
-tsubst_omp_clauses (tree clauses, bool declare_simd, bool allow_fields,
+tsubst_omp_clauses (tree clauses, enum c_omp_region_type ort,
                    tree args, tsubst_flags_t complain, tree in_decl)
 {
   tree new_clauses = NULL_TREE, nc, oc;
@@ -14546,7 +14679,7 @@ tsubst_omp_clauses (tree clauses, bool declare_simd, bool allow_fields,
        default:
          gcc_unreachable ();
        }
-      if (allow_fields)
+      if ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP)
        switch (OMP_CLAUSE_CODE (nc))
          {
          case OMP_CLAUSE_SHARED:
@@ -14608,9 +14741,9 @@ tsubst_omp_clauses (tree clauses, bool declare_simd, bool allow_fields,
     }
 
   new_clauses = nreverse (new_clauses);
-  if (!declare_simd)
+  if (ort != C_ORT_OMP_DECLARE_SIMD)
     {
-      new_clauses = finish_omp_clauses (new_clauses, allow_fields);
+      new_clauses = finish_omp_clauses (new_clauses, ort);
       if (linear_no_step)
        for (nc = new_clauses; nc; nc = OMP_CLAUSE_CHAIN (nc))
          if (nc == linear_no_step)
@@ -14831,7 +14964,7 @@ tsubst_omp_for_iterator (tree t, int i, tree declv, tree orig_declv,
        {
          tree c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
          OMP_CLAUSE_DECL (c) = decl;
-         c = finish_omp_clauses (c, true);
+         c = finish_omp_clauses (c, C_ORT_OMP);
          if (c)
            {
              OMP_CLAUSE_CHAIN (c) = *clauses;
@@ -14999,7 +15132,6 @@ tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
          {
            tree scope = USING_DECL_SCOPE (decl);
            tree name = DECL_NAME (decl);
-           tree decl;
 
            scope = tsubst (scope, args, complain, in_decl);
            decl = lookup_qualified_name (scope, name,
@@ -15314,7 +15446,7 @@ tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
 
     case OACC_KERNELS:
     case OACC_PARALLEL:
-      tmp = tsubst_omp_clauses (OMP_CLAUSES (t), false, false, args, complain,
+      tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_ACC, args, complain,
                                in_decl);
       stmt = begin_omp_parallel ();
       RECUR (OMP_BODY (t));
@@ -15323,8 +15455,8 @@ tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
 
     case OMP_PARALLEL:
       r = push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t));
-      tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), false, true,
-                               args, complain, in_decl);
+      tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), C_ORT_OMP, args,
+                               complain, in_decl);
       if (OMP_PARALLEL_COMBINED (t))
        omp_parallel_combined_clauses = &tmp;
       stmt = begin_omp_parallel ();
@@ -15337,8 +15469,8 @@ tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
 
     case OMP_TASK:
       r = push_omp_privatization_clauses (false);
-      tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), false, true,
-                               args, complain, in_decl);
+      tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
+                               complain, in_decl);
       stmt = begin_omp_task ();
       RECUR (OMP_TASK_BODY (t));
       finish_omp_task (tmp, stmt);
@@ -15357,12 +15489,17 @@ tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
        tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
        tree orig_declv = NULL_TREE;
        tree incrv = NULL_TREE;
+       enum c_omp_region_type ort = C_ORT_OMP;
        int i;
 
+       if (TREE_CODE (t) == CILK_SIMD || TREE_CODE (t) == CILK_FOR)
+         ort = C_ORT_CILK;
+       else if (TREE_CODE (t) == OACC_LOOP)
+         ort = C_ORT_ACC;
+
        r = push_omp_privatization_clauses (OMP_FOR_INIT (t) == NULL_TREE);
-       clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), false,
-                                     TREE_CODE (t) != OACC_LOOP,
-                                     args, complain, in_decl);
+       clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), ort, args, complain,
+                                     in_decl);
        if (OMP_FOR_INIT (t) != NULL_TREE)
          {
            declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
@@ -15418,8 +15555,8 @@ tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
     case OMP_CRITICAL:
       r = push_omp_privatization_clauses (TREE_CODE (t) == OMP_TEAMS
                                          && OMP_TEAMS_COMBINED (t));
-      tmp = tsubst_omp_clauses (OMP_CLAUSES (t), false, true,
-                               args, complain, in_decl);
+      tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_OMP, args, complain,
+                               in_decl);
       stmt = push_stmt_list ();
       RECUR (OMP_BODY (t));
       stmt = pop_stmt_list (stmt);
@@ -15434,9 +15571,9 @@ tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
     case OACC_DATA:
     case OMP_TARGET_DATA:
     case OMP_TARGET:
-      tmp = tsubst_omp_clauses (OMP_CLAUSES (t), false,
-                               TREE_CODE (t) != OACC_DATA,
-                               args, complain, in_decl);
+      tmp = tsubst_omp_clauses (OMP_CLAUSES (t), (TREE_CODE (t) == OACC_DATA)
+                               ? C_ORT_ACC : C_ORT_OMP, args, complain,
+                               in_decl);
       keep_next_level (true);
       stmt = begin_omp_structured_block ();
 
@@ -15481,8 +15618,8 @@ tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
 
     case OACC_DECLARE:
       t = copy_node (t);
-      tmp = tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t), false, false,
-                               args, complain, in_decl);
+      tmp = tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t), C_ORT_ACC, args,
+                               complain, in_decl);
       OACC_DECLARE_CLAUSES (t) = tmp;
       add_stmt (t);
       break;
@@ -15490,8 +15627,8 @@ tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
     case OMP_TARGET_UPDATE:
     case OMP_TARGET_ENTER_DATA:
     case OMP_TARGET_EXIT_DATA:
-      tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), false, true,
-                               args, complain, in_decl);
+      tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_OMP, args,
+                               complain, in_decl);
       t = copy_node (t);
       OMP_STANDALONE_CLAUSES (t) = tmp;
       add_stmt (t);
@@ -15500,16 +15637,16 @@ tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
     case OACC_ENTER_DATA:
     case OACC_EXIT_DATA:
     case OACC_UPDATE:
-      tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), false, false,
-                               args, complain, in_decl);
+      tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_ACC, args,
+                               complain, in_decl);
       t = copy_node (t);
       OMP_STANDALONE_CLAUSES (t) = tmp;
       add_stmt (t);
       break;
 
     case OMP_ORDERED:
-      tmp = tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t), false, true,
-                               args, complain, in_decl);
+      tmp = tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t), C_ORT_OMP, args,
+                               complain, in_decl);
       stmt = push_stmt_list ();
       RECUR (OMP_BODY (t));
       stmt = pop_stmt_list (stmt);
@@ -15851,15 +15988,7 @@ tsubst_copy_and_build (tree t,
          return error_mark_node;
 
        if (variable_template_p (templ))
-         {
-           templ = lookup_template_variable (templ, targs);
-           if (!any_dependent_template_arguments_p (targs))
-             {
-               templ = finish_template_variable (templ, complain);
-               mark_used (templ);
-             }
-           RETURN (convert_from_reference (templ));
-         }
+         RETURN (lookup_and_finish_template_variable (templ, targs, complain));
 
        if (TREE_CODE (templ) == COMPONENT_REF)
          {
@@ -15892,6 +16021,10 @@ tsubst_copy_and_build (tree t,
        else
          r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
                                    complain|decltype_flag);
+
+       if (TREE_CODE (r) == INDIRECT_REF)
+         REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
+
        RETURN (r);
       }
 
@@ -16089,7 +16222,8 @@ tsubst_copy_and_build (tree t,
                                          length, stride, TREE_TYPE (op1)));
       }
     case SIZEOF_EXPR:
-      if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
+      if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
+         || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
        RETURN (tsubst_copy (t, args, complain, in_decl));
       /* Fall through */
       
@@ -16967,8 +17101,6 @@ tsubst_copy_and_build (tree t,
        else
          gcc_unreachable ();
        LAMBDA_EXPR_EXTRA_SCOPE (r) = scope;
-       LAMBDA_EXPR_RETURN_TYPE (r)
-         = tsubst (LAMBDA_EXPR_RETURN_TYPE (t), args, complain, in_decl);
 
        gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
                    && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
@@ -16979,6 +17111,9 @@ tsubst_copy_and_build (tree t,
           declaration of the op() for later calls to lambda_function.  */
        complete_type (type);
 
+       if (tree fn = lambda_function (type))
+         LAMBDA_EXPR_RETURN_TYPE (r) = TREE_TYPE (TREE_TYPE (fn));
+
        LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
 
        insert_pending_capture_proxies ();
@@ -17186,12 +17321,14 @@ instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
 
   /* Check to see if we already have this specialization.  */
   gen_tmpl = most_general_template (tmpl);
-  if (tmpl != gen_tmpl)
-    /* The TMPL is a partial instantiation.  To get a full set of
-       arguments we must add the arguments used to perform the
-       partial instantiation.  */
-    targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl),
-                                           targ_ptr);
+  if (TMPL_ARGS_DEPTH (targ_ptr)
+      < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)))
+    /* targ_ptr only has the innermost template args, so add the outer ones
+       from tmpl, which could be either a partial instantiation or gen_tmpl (in
+       the case of a non-dependent call within a template definition).  */
+    targ_ptr = (add_outermost_template_args
+               (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl)),
+                targ_ptr));
 
   /* It would be nice to avoid hashing here and then again in tsubst_decl,
      but it doesn't seem to be on the hot path.  */
@@ -18516,7 +18653,7 @@ resolve_overloaded_unification (tree tparms,
    lvalue for the function template specialization.  */
 
 tree
-resolve_nondeduced_context (tree orig_expr)
+resolve_nondeduced_context (tree orig_expr, tsubst_flags_t complain)
 {
   tree expr, offset, baselink;
   bool addr;
@@ -18599,16 +18736,16 @@ resolve_nondeduced_context (tree orig_expr)
            {
              tree base
                = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
-             expr = build_offset_ref (base, expr, addr, tf_warning_or_error);
+             expr = build_offset_ref (base, expr, addr, complain);
            }
          if (addr)
-           expr = cp_build_addr_expr (expr, tf_warning_or_error);
+           expr = cp_build_addr_expr (expr, complain);
          return expr;
        }
-      else if (good == 0 && badargs)
+      else if (good == 0 && badargs && (complain & tf_error))
        /* There were no good options and at least one bad one, so let the
           user know what the problem is.  */
-       instantiate_template (badfn, badargs, tf_warning_or_error);
+       instantiate_template (badfn, badargs, complain);
     }
   return orig_expr;
 }
@@ -18680,6 +18817,28 @@ try_one_overload (tree tparms,
           template args used in the function parm list with our own
           template parms.  Discard them.  */
        TREE_VEC_ELT (tempargs, i) = NULL_TREE;
+      else if (oldelt && ARGUMENT_PACK_P (oldelt))
+       {
+         /* Check that the argument at each index of the deduced argument pack
+            is equivalent to the corresponding explicitly specified argument.
+            We may have deduced more arguments than were explicitly specified,
+            and that's OK.  */
+         gcc_assert (ARGUMENT_PACK_INCOMPLETE_P (oldelt));
+         gcc_assert (ARGUMENT_PACK_ARGS (oldelt)
+                     == ARGUMENT_PACK_EXPLICIT_ARGS (oldelt));
+
+         tree explicit_pack = ARGUMENT_PACK_ARGS (oldelt);
+         tree deduced_pack = ARGUMENT_PACK_ARGS (elt);
+
+         if (TREE_VEC_LENGTH (deduced_pack)
+             < TREE_VEC_LENGTH (explicit_pack))
+           return 0;
+
+         for (int j = 0; j < TREE_VEC_LENGTH (explicit_pack); j++)
+           if (!template_args_equal (TREE_VEC_ELT (explicit_pack, j),
+                                     TREE_VEC_ELT (deduced_pack, j)))
+             return 0;
+       }
       else if (oldelt && !template_args_equal (oldelt, elt))
        return 0;
     }
@@ -19904,11 +20063,20 @@ unify (tree tparms, tree targs, tree parm, tree arg, int strict,
       return unify_template_argument_mismatch (explain_p, parm, arg);
 
     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 ();
+      /* We might get a variable as a non-type template argument in parm if the
+        corresponding parameter is type-dependent.  Make any necessary
+        adjustments based on whether arg is a reference.  */
+      if (CONSTANT_CLASS_P (arg))
+       parm = fold_non_dependent_expr (parm);
+      else if (REFERENCE_REF_P (arg))
+       {
+         tree sub = TREE_OPERAND (arg, 0);
+         STRIP_NOPS (sub);
+         if (TREE_CODE (sub) == ADDR_EXPR)
+           arg = TREE_OPERAND (sub, 0);
+       }
+      /* Now use the normal expression code to check whether they match.  */
+      goto expr;
 
     case TYPE_ARGUMENT_PACK:
     case NONTYPE_ARGUMENT_PACK:
@@ -19941,7 +20109,7 @@ unify (tree tparms, tree targs, tree parm, tree arg, int strict,
       if (is_overloaded_fn (parm) || type_unknown_p (parm))
        return unify_success (explain_p);
       gcc_assert (EXPR_P (parm));
-
+    expr:
       /* We must be looking at an expression.  This can happen with
         something like:
 
@@ -20188,7 +20356,7 @@ more_specialized_fn (tree pat1, tree pat2, int len)
 
       if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
         {
-          int i, len2 = list_length (args2);
+          int i, len2 = remaining_arguments (args2);
           tree parmvec = make_tree_vec (1);
           tree argvec = make_tree_vec (len2);
           tree ta = args2;
@@ -20212,7 +20380,7 @@ more_specialized_fn (tree pat1, tree pat2, int len)
         }
       else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
         {
-          int i, len1 = list_length (args1);
+          int i, len1 = remaining_arguments (args1);
           tree parmvec = make_tree_vec (1);
           tree argvec = make_tree_vec (len1);
           tree ta = args1;
@@ -20681,6 +20849,36 @@ most_general_template (tree decl)
   return decl;
 }
 
+/* True iff the TEMPLATE_DECL tmpl is a partial specialization.  */
+
+static bool
+partial_specialization_p (tree tmpl)
+{
+  /* Any specialization has DECL_TEMPLATE_SPECIALIZATION.  */
+  if (!DECL_TEMPLATE_SPECIALIZATION (tmpl))
+    return false;
+  tree t = DECL_TI_TEMPLATE (tmpl);
+  /* A specialization that fully specializes one of the containing classes is
+     not a partial specialization.  */
+  return (list_length (DECL_TEMPLATE_PARMS (tmpl))
+         == list_length (DECL_TEMPLATE_PARMS (t)));
+}
+
+/* If TMPL is a partial specialization, return the arguments for its primary
+   template.  */
+
+static tree
+impartial_args (tree tmpl, tree args)
+{
+  if (!partial_specialization_p (tmpl))
+    return args;
+
+  /* If TMPL is a partial specialization, we need to substitute to get
+     the args for the primary template.  */
+  return tsubst_template_args (DECL_TI_ARGS (tmpl), args,
+                              tf_warning_or_error, tmpl);
+}
+
 /* Return the most specialized of the template partial specializations
    which can produce TARGET, a specialization of some class or variable
    template.  The value returned is actually a TREE_LIST; the TREE_VALUE is
@@ -21505,7 +21703,7 @@ instantiate_decl (tree d, int defer_ok,
     return d;
 
   gen_tmpl = most_general_template (tmpl);
-  gen_args = DECL_TI_ARGS (d);
+  gen_args = impartial_args (tmpl, DECL_TI_ARGS (d));
 
   if (tmpl != gen_tmpl)
     /* We should already have the extra args.  */
@@ -21543,7 +21741,8 @@ instantiate_decl (tree d, int defer_ok,
   if (TREE_CODE (d) == FUNCTION_DECL)
     {
       deleted_p = DECL_DELETED_FN (code_pattern);
-      pattern_defined = (DECL_SAVED_TREE (code_pattern) != NULL_TREE
+      pattern_defined = ((DECL_SAVED_TREE (code_pattern) != NULL_TREE
+                         && DECL_INITIAL (code_pattern) != error_mark_node)
                         || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern)
                         || deleted_p);
     }
@@ -21743,7 +21942,7 @@ instantiate_decl (tree d, int defer_ok,
       if (enter_context)
         pop_nested_class ();
 
-      if (variable_template_p (td))
+      if (variable_template_p (gen_tmpl))
        note_variable_template_instantiation (d);
     }
   else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
@@ -22449,6 +22648,17 @@ value_dependent_expression_p (tree expression)
 
   switch (TREE_CODE (expression))
     {
+    case BASELINK:
+      /* A dependent member function of the current instantiation.  */
+      return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression)));
+
+    case FUNCTION_DECL:
+      /* A dependent member function of the current instantiation.  */
+      if (DECL_CLASS_SCOPE_P (expression)
+         && dependent_type_p (DECL_CONTEXT (expression)))
+       return true;
+      break;
+
     case IDENTIFIER_NODE:
       /* A name that has not been looked up -- must be dependent.  */
       return true;
@@ -22475,6 +22685,7 @@ value_dependent_expression_p (tree expression)
          && (TREE_CODE (DECL_INITIAL (expression)) == TREE_LIST
              /* cp_finish_decl doesn't fold reference initializers.  */
              || TREE_CODE (TREE_TYPE (expression)) == REFERENCE_TYPE
+             || type_dependent_expression_p (DECL_INITIAL (expression))
              || value_dependent_expression_p (DECL_INITIAL (expression))))
        return true;
       return false;
@@ -22525,7 +22736,7 @@ value_dependent_expression_p (tree expression)
         return true;
       else if (TYPE_P (expression))
        return dependent_type_p (expression);
-      return instantiation_dependent_expression_p (expression);
+      return instantiation_dependent_uneval_expression_p (expression);
 
     case AT_ENCODE_EXPR:
       /* An 'encode' expression is value-dependent if the operand is
@@ -22535,7 +22746,7 @@ value_dependent_expression_p (tree expression)
 
     case NOEXCEPT_EXPR:
       expression = TREE_OPERAND (expression, 0);
-      return instantiation_dependent_expression_p (expression);
+      return instantiation_dependent_uneval_expression_p (expression);
 
     case SCOPE_REF:
       /* All instantiation-dependent expressions should also be considered
@@ -22592,10 +22803,10 @@ value_dependent_expression_p (tree expression)
 
     case CALL_EXPR:
       {
+       if (value_dependent_expression_p (CALL_EXPR_FN (expression)))
+         return true;
        tree fn = get_callee_fndecl (expression);
        int i, nargs;
-       if (!fn && value_dependent_expression_p (CALL_EXPR_FN (expression)))
-         return true;
        nargs = call_expr_nargs (expression);
        for (i = 0; i < nargs; ++i)
          {
@@ -22759,13 +22970,6 @@ type_dependent_expression_p (tree expression)
              || dependent_scope_p (scope));
     }
 
-  /* A function template specialization is type-dependent if it has any
-     dependent template arguments.  */
-  if (TREE_CODE (expression) == FUNCTION_DECL
-      && DECL_LANG_SPECIFIC (expression)
-      && DECL_TEMPLATE_INFO (expression))
-    return any_dependent_template_arguments_p (DECL_TI_ARGS (expression));
-
   if (TREE_CODE (expression) == TEMPLATE_DECL
       && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
     return false;
@@ -22818,13 +23022,18 @@ type_dependent_expression_p (tree expression)
       && DECL_INITIAL (expression))
    return true;
 
-  /* A variable template specialization is type-dependent if it has any
-     dependent template arguments.  */
-  if (VAR_P (expression)
+  /* A function or variable template-id is type-dependent if it has any
+     dependent template arguments.  Note that we only consider the innermost
+     template arguments here, since those are the ones that come from the
+     template-id; the template arguments for the enclosing class do not make it
+     type-dependent, they only make a member function value-dependent.  */
+  if (VAR_OR_FUNCTION_DECL_P (expression)
       && DECL_LANG_SPECIFIC (expression)
       && DECL_TEMPLATE_INFO (expression)
-      && variable_template_p (DECL_TI_TEMPLATE (expression)))
-    return any_dependent_template_arguments_p (DECL_TI_ARGS (expression));
+      && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (expression))
+      && (any_dependent_template_arguments_p
+         (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
+    return true;
 
   /* Always dependent, on the number of arguments if nothing else.  */
   if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
@@ -22882,6 +23091,22 @@ type_dependent_expression_p (tree expression)
   return (dependent_type_p (TREE_TYPE (expression)));
 }
 
+/* [temp.dep.expr]/5: A class member access expression (5.2.5) is
+   type-dependent if the expression refers to a member of the current
+   instantiation and the type of the referenced member is dependent, or the
+   class member access expression refers to a member of an unknown
+   specialization.
+
+   This function returns true if the OBJECT in such a class member access
+   expression is of an unknown specialization.  */
+
+bool
+type_dependent_object_expression_p (tree object)
+{
+  tree scope = TREE_TYPE (object);
+  return (!scope || dependent_scope_p (scope));
+}
+
 /* walk_tree callback function for instantiation_dependent_expression_p,
    below.  Returns non-zero if a dependent subexpression is found.  */
 
@@ -22906,13 +23131,6 @@ instantiation_dependent_r (tree *tp, int *walk_subtrees,
     case TREE_VEC:
       return NULL_TREE;
 
-    case VAR_DECL:
-    case CONST_DECL:
-      /* A constant with a dependent initializer is dependent.  */
-      if (value_dependent_expression_p (*tp))
-       return *tp;
-      break;
-
     case TEMPLATE_PARM_INDEX:
       return *tp;
 
@@ -22938,12 +23156,6 @@ instantiation_dependent_r (tree *tp, int *walk_subtrees,
        break;
       }
 
-    case TRAIT_EXPR:
-      if (value_dependent_expression_p (*tp))
-       return *tp;
-      *walk_subtrees = false;
-      return NULL_TREE;
-
     case COMPONENT_REF:
       if (identifier_p (TREE_OPERAND (*tp, 1)))
        /* In a template, finish_class_member_access_expr creates a
@@ -22994,10 +23206,15 @@ instantiation_dependent_r (tree *tp, int *walk_subtrees,
 
    "An expression is instantiation-dependent if it is type-dependent
    or value-dependent, or it has a subexpression that is type-dependent
-   or value-dependent."  */
+   or value-dependent."
+
+   Except don't actually check value-dependence for unevaluated expressions,
+   because in sizeof(i) we don't care about the value of i.  Checking
+   type-dependence will in turn check value-dependence of array bounds/template
+   arguments as needed.  */
 
 bool
-instantiation_dependent_expression_p (tree expression)
+instantiation_dependent_uneval_expression_p (tree expression)
 {
   tree result;
 
@@ -23012,6 +23229,15 @@ instantiation_dependent_expression_p (tree expression)
   return result != NULL_TREE;
 }
 
+/* As above, but also check value-dependence of the expression as a whole.  */
+
+bool
+instantiation_dependent_expression_p (tree expression)
+{
+  return (instantiation_dependent_uneval_expression_p (expression)
+         || value_dependent_expression_p (expression));
+}
+
 /* Like type_dependent_expression_p, but it also works while not processing
    a template definition, i.e. during substitution or mangling.  */
 
@@ -23085,9 +23311,18 @@ dependent_template_arg_p (tree arg)
   if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
     arg = ARGUMENT_PACK_SELECT_ARG (arg);
 
-  if (TREE_CODE (arg) == TEMPLATE_DECL
-      || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
-    return dependent_template_p (arg);
+  if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
+    return true;
+  if (TREE_CODE (arg) == TEMPLATE_DECL)
+    {
+      if (DECL_TEMPLATE_PARM_P (arg))
+       return true;
+      /* A member template of a dependent class is not necessarily
+        type-dependent, but it is a dependent template argument because it
+        will be a member of an unknown specialization to that template.  */
+      tree scope = CP_DECL_CONTEXT (arg);
+      return TYPE_P (scope) && dependent_type_p (scope);
+    }
   else if (ARGUMENT_PACK_P (arg))
     {
       tree args = ARGUMENT_PACK_ARGS (arg);
@@ -23183,7 +23418,7 @@ any_dependent_template_arguments_p (const_tree args)
   return false;
 }
 
-/* Returns TRUE if the template TMPL is dependent.  */
+/* Returns TRUE if the template TMPL is type-dependent.  */
 
 bool
 dependent_template_p (tree tmpl)
@@ -23206,9 +23441,6 @@ dependent_template_p (tree tmpl)
   /* So are names that have not been looked up.  */
   if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
     return true;
-  /* So are member templates of dependent classes.  */
-  if (TYPE_P (CP_DECL_CONTEXT (tmpl)))
-    return dependent_type_p (DECL_CONTEXT (tmpl));
   return false;
 }
 
@@ -23399,9 +23631,9 @@ resolve_typename_type (tree type, bool only_current_p)
     {
       /* Ill-formed programs can cause infinite recursion here, so we
         must catch that.  */
-      TYPENAME_IS_RESOLVING_P (type) = 1;
+      TYPENAME_IS_RESOLVING_P (result) = 1;
       result = resolve_typename_type (result, only_current_p);
-      TYPENAME_IS_RESOLVING_P (type) = 0;
+      TYPENAME_IS_RESOLVING_P (result) = 0;
     }
   
   /* Qualify the resulting type.  */
@@ -23423,8 +23655,10 @@ build_non_dependent_expr (tree expr)
 
   /* When checking, try to get a constant value for all non-dependent
      expressions in order to expose bugs in *_dependent_expression_p
-     and constexpr.  */
-  if (flag_checking && cxx_dialect >= cxx11
+     and constexpr.  This can affect code generation, see PR70704, so
+     only do this for -fchecking=2.  */
+  if (flag_checking > 1
+      && cxx_dialect >= cxx11
       /* Don't do this during nsdmi parsing as it can lead to
         unexpected recursive instantiations.  */
       && !parsing_nsdmi ())
@@ -23760,7 +23994,7 @@ do_auto_deduction (tree type, tree init, tree auto_node,
   if (type == error_mark_node)
     return error_mark_node;
 
-  init = resolve_nondeduced_context (init);
+  init = resolve_nondeduced_context (init, complain);
 
   if (AUTO_IS_DECLTYPE (auto_node))
     {