pt.c (value_dependent_expression_p): Tweak new cases to better match the wording...
[gcc.git] / gcc / cp / pt.c
index 68374389f3beb184932edd4c1de2ddfbd09aa79f..fde3091951747e7f9ed4c7195b41baa71ade989a 100644 (file)
@@ -3696,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;
@@ -5655,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);
@@ -5680,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
        {
@@ -6240,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;
@@ -9565,7 +9563,8 @@ 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.  */
@@ -9584,10 +9583,10 @@ tsubst_attribute (tree t, tree *decl_p, tree args,
                              get_attribute_name (t)))
     {
       tree clauses = TREE_VALUE (val);
-      clauses = tsubst_omp_clauses (clauses, true, false, args,
+      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, false, true);
+      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);
@@ -9860,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))
@@ -10965,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;
        }
     }
@@ -11700,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
@@ -13407,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;
          }
 
@@ -13670,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;
 
@@ -13878,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
@@ -14532,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;
@@ -14682,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:
@@ -14744,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)
@@ -14967,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;
@@ -15135,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,
@@ -15450,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));
@@ -15459,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 ();
@@ -15473,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);
@@ -15493,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)));
@@ -15554,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);
@@ -15570,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 ();
 
@@ -15617,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;
@@ -15626,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);
@@ -15636,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);
@@ -17320,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.  */
@@ -21738,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);
     }
@@ -22644,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;
@@ -22670,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;
@@ -22720,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
@@ -22730,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
@@ -22787,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)
          {
@@ -22954,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;
@@ -23013,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)
@@ -23077,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.  */
 
@@ -23101,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;
 
@@ -23133,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
@@ -23189,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;
 
@@ -23207,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.  */
 
@@ -23280,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);
@@ -23378,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)
@@ -23401,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;
 }
 
@@ -23594,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.  */
@@ -23618,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 ())