2017-06-09 Jason Merrill <jason@redhat.com>
+ Missing bits from N4268, constant evaluation for all non-type args.
+ * call.c (build_converted_constant_expr): Rename from
+ build_integral_nontype_arg_conv, handle all types.
+ * pt.c (convert_nontype_argument): In C++17 call it for all types.
+ Move NOP stripping inside pointer case, don't strip ADDR_EXPR.
+ * cvt.c (strip_fnptr_conv): Also strip conversions to the same type.
+
Overhaul pointer-to-member conversion and template argument handling.
* call.c (standard_conversion): Avoid creating ck_pmem when the
class type is the same.
/* Subroutine of convert_nontype_argument.
- EXPR is an argument for a template non-type parameter of integral or
- enumeration type. Do any necessary conversions (that are permitted for
- non-type arguments) to convert it to the parameter type.
+ EXPR is an expression used in a context that requires a converted
+ constant-expression, such as a template non-type parameter. Do any
+ necessary conversions (that are permitted for converted
+ constant-expressions) to convert it to the desired type.
If conversion is successful, returns the converted expression;
otherwise, returns error_mark_node. */
tree
-build_integral_nontype_arg_conv (tree type, tree expr, tsubst_flags_t complain)
+build_converted_constant_expr (tree type, tree expr, tsubst_flags_t complain)
{
conversion *conv;
void *p;
if (error_operand_p (expr))
return error_mark_node;
- gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
-
/* Get the high-water mark for the CONVERSION_OBSTACK. */
p = conversion_obstack_alloc (0);
/*c_cast_p=*/false,
LOOKUP_IMPLICIT, complain);
- /* for a non-type template-parameter of integral or
- enumeration type, integral promotions (4.5) and integral
- conversions (4.7) are applied. */
- /* It should be sufficient to check the outermost conversion step, since
- there are no qualification conversions to integer type. */
- if (conv)
- switch (conv->kind)
- {
- /* A conversion function is OK. If it isn't constexpr, we'll
- complain later that the argument isn't constant. */
- case ck_user:
- /* The lvalue-to-rvalue conversion is OK. */
- case ck_rvalue:
- case ck_identity:
- break;
+ /* A converted constant expression of type T is an expression, implicitly
+ converted to type T, where the converted expression is a constant
+ expression and the implicit conversion sequence contains only
+
+ * user-defined conversions,
+ * lvalue-to-rvalue conversions (7.1),
+ * array-to-pointer conversions (7.2),
+ * function-to-pointer conversions (7.3),
+ * qualification conversions (7.5),
+ * integral promotions (7.6),
+ * integral conversions (7.8) other than narrowing conversions (11.6.4),
+ * null pointer conversions (7.11) from std::nullptr_t,
+ * null member pointer conversions (7.12) from std::nullptr_t, and
+ * function pointer conversions (7.13),
+
+ and where the reference binding (if any) binds directly. */
+
+ for (conversion *c = conv;
+ conv && c->kind != ck_identity;
+ c = next_conversion (c))
+ {
+ switch (c->kind)
+ {
+ /* A conversion function is OK. If it isn't constexpr, we'll
+ complain later that the argument isn't constant. */
+ case ck_user:
+ /* The lvalue-to-rvalue conversion is OK. */
+ case ck_rvalue:
+ /* Array-to-pointer and function-to-pointer. */
+ case ck_lvalue:
+ /* Function pointer conversions. */
+ case ck_fnptr:
+ /* Qualification conversions. */
+ case ck_qual:
+ break;
- case ck_std:
- t = next_conversion (conv)->type;
- if (INTEGRAL_OR_ENUMERATION_TYPE_P (t))
+ case ck_ref_bind:
+ if (c->need_temporary_p)
+ {
+ if (complain & tf_error)
+ error_at (loc, "initializing %qH with %qI in converted "
+ "constant expression does not bind directly",
+ type, next_conversion (c)->type);
+ conv = NULL;
+ }
break;
- if (complain & tf_error)
- error_at (loc, "conversion from %qH to %qI not considered for "
- "non-type template argument", t, type);
- /* fall through. */
+ case ck_base:
+ case ck_pmem:
+ case ck_ptr:
+ case ck_std:
+ t = next_conversion (c)->type;
+ if (INTEGRAL_OR_ENUMERATION_TYPE_P (t)
+ && INTEGRAL_OR_ENUMERATION_TYPE_P (type))
+ /* Integral promotion or conversion. */
+ break;
+ if (NULLPTR_TYPE_P (t))
+ /* Conversion from nullptr to pointer or pointer-to-member. */
+ break;
- default:
- conv = NULL;
- break;
- }
+ if (complain & tf_error)
+ error_at (loc, "conversion from %qH to %qI in a "
+ "converted constant expression", t, type);
+ /* fall through. */
+
+ default:
+ conv = NULL;
+ break;
+ }
+ }
+
+ /* Avoid confusing convert_nontype_argument by introducing
+ a redundant conversion to the same reference type. */
+ if (conv && conv->kind == ck_ref_bind
+ && REFERENCE_REF_P (expr))
+ {
+ tree ref = TREE_OPERAND (expr, 0);
+ if (same_type_p (type, TREE_TYPE (ref)))
+ return ref;
+ }
if (conv)
expr = convert_like (conv, expr, complain);
extern int remaining_arguments (tree);
extern tree perform_implicit_conversion (tree, tree, tsubst_flags_t);
extern tree perform_implicit_conversion_flags (tree, tree, tsubst_flags_t, int);
-extern tree build_integral_nontype_arg_conv (tree, tree, tsubst_flags_t);
+extern tree build_converted_constant_expr (tree, tree, tsubst_flags_t);
extern tree perform_direct_initialization_if_possible (tree, tree, bool,
tsubst_flags_t);
extern tree in_charge_arg_for_name (tree);
|| can_convert_tx_safety (t, f));
}
-/* Return FN with any NOP_EXPRs that represent function pointer
- conversions stripped. */
+/* Return FN with any NOP_EXPRs stripped that represent function pointer
+ conversions or conversions to the same type. */
tree
strip_fnptr_conv (tree fn)
while (TREE_CODE (fn) == NOP_EXPR)
{
tree op = TREE_OPERAND (fn, 0);
- if (fnptr_conv_p (TREE_TYPE (fn), TREE_TYPE (op)))
+ tree ft = TREE_TYPE (fn);
+ tree ot = TREE_TYPE (op);
+ if (same_type_p (ft, ot)
+ || fnptr_conv_p (ft, ot))
fn = op;
else
break;
convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
{
tree expr_type;
+ location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
+ tree orig_expr = expr;
/* Detect immediately string literals as invalid non-type argument.
This special-case is not needed for correctness (we would easily
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))
- /* Constant value checking is done later with type conversion. */;
- else if (cxx_dialect >= cxx1z)
+ else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
+ || cxx_dialect >= cxx1z)
{
- if (TREE_CODE (type) != REFERENCE_TYPE)
- expr = maybe_constant_value (expr);
- else if (REFERENCE_REF_P (expr))
- {
- expr = TREE_OPERAND (expr, 0);
- expr = maybe_constant_value (expr);
- expr = convert_from_reference (expr);
- }
+ /* C++17: A template-argument for a non-type template-parameter shall
+ be a converted constant expression (8.20) of the type of the
+ template-parameter. */
+ expr = build_converted_constant_expr (type, expr, complain);
+ if (expr == error_mark_node)
+ return error_mark_node;
+ expr = maybe_constant_value (expr);
+ expr = convert_from_reference (expr);
}
else if (TYPE_PTR_OR_PTRMEM_P (type))
{
}
}
- /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
- parameter is a pointer to object, through decay and
- qualification conversion. Let's strip everything. */
- else if (TREE_CODE (expr) == NOP_EXPR && TYPE_PTROBV_P (type))
- {
- tree probe = expr;
- STRIP_NOPS (probe);
- if (TREE_CODE (probe) == ADDR_EXPR
- && TYPE_PTR_P (TREE_TYPE (probe)))
- {
- /* Skip the ADDR_EXPR only if it is part of the decay for
- an array. Otherwise, it is part of the original argument
- in the source code. */
- if (TREE_CODE (TREE_TYPE (TREE_OPERAND (probe, 0))) == ARRAY_TYPE)
- probe = TREE_OPERAND (probe, 0);
- expr = probe;
- expr_type = TREE_TYPE (expr);
- }
- }
-
/* [temp.arg.nontype]/5, bullet 1
For a non-type template-parameter of integral or enumeration type,
(_conv.integral_) are applied. */
if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
{
- tree t = build_integral_nontype_arg_conv (type, expr, complain);
- t = maybe_constant_value (t);
- if (t != error_mark_node)
- expr = t;
+ if (cxx_dialect < cxx11)
+ {
+ tree t = build_converted_constant_expr (type, expr, complain);
+ t = maybe_constant_value (t);
+ if (t != error_mark_node)
+ expr = t;
+ }
if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
return error_mark_node;
return NULL_TREE;
expr = cxx_constant_value (expr);
if (errorcount > errs || warningcount + werrorcount > warns)
- inform (EXPR_LOC_OR_LOC (expr, input_location),
- "in template argument for type %qT ", type);
+ inform (loc, "in template argument for type %qT ", type);
if (expr == error_mark_node)
return NULL_TREE;
/* else cxx_constant_value complained but gave us
conversion (_conv.array_) are applied. */
else if (TYPE_PTROBV_P (type))
{
+ tree decayed = expr;
+
+ /* Look through any NOP_EXPRs around an ADDR_EXPR, whether they come from
+ decay_conversion or an explicit cast. If it's a problematic cast,
+ we'll complain about it below. */
+ if (TREE_CODE (expr) == NOP_EXPR)
+ {
+ tree probe = expr;
+ STRIP_NOPS (probe);
+ if (TREE_CODE (probe) == ADDR_EXPR
+ && TYPE_PTR_P (TREE_TYPE (probe)))
+ {
+ expr = probe;
+ expr_type = TREE_TYPE (expr);
+ }
+ }
+
/* [temp.arg.nontype]/1 (TC1 version, DR 49):
A template-argument for a non-type, non-template template-parameter
;
else if (cxx_dialect >= cxx11 && integer_zerop (expr))
/* Null pointer values are OK in C++11. */;
- else if (TREE_CODE (expr) != ADDR_EXPR
- && TREE_CODE (expr_type) != ARRAY_TYPE)
+ else if (TREE_CODE (expr) != ADDR_EXPR)
{
if (VAR_P (expr))
{
if (complain & tf_error)
error ("%qD is not a valid template argument "
"because %qD is a variable, not the address of "
- "a variable", expr, expr);
+ "a variable", orig_expr, expr);
return NULL_TREE;
}
if (POINTER_TYPE_P (expr_type))
if (complain & tf_error)
error ("%qE is not a valid template argument for %qT "
"because it is not the address of a variable",
- expr, type);
+ orig_expr, type);
return NULL_TREE;
}
/* Other values, like integer constants, might be valid
}
else
{
- tree decl;
+ tree decl = TREE_OPERAND (expr, 0);
- decl = ((TREE_CODE (expr) == ADDR_EXPR)
- ? TREE_OPERAND (expr, 0) : expr);
if (!VAR_P (decl))
{
if (complain & tf_error)
error ("%qE is not a valid template argument of type %qT "
- "because %qE is not a variable", expr, type, decl);
+ "because %qE is not a variable", orig_expr, type, decl);
return NULL_TREE;
}
else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl))
if (complain & tf_error)
error ("%qE is not a valid template argument of type %qT "
"because %qD does not have external linkage",
- expr, type, decl);
+ orig_expr, type, decl);
return NULL_TREE;
}
else if ((cxx_dialect >= cxx11 && cxx_dialect < cxx1z)
{
if (complain & tf_error)
error ("%qE is not a valid template argument of type %qT "
- "because %qD has no linkage", expr, type, decl);
+ "because %qD has no linkage", orig_expr, type, decl);
return NULL_TREE;
}
/* C++17: For a non-type template-parameter of reference or pointer
}
}
- expr = decay_conversion (expr, complain);
- if (expr == error_mark_node)
- return error_mark_node;
+ expr = decayed;
expr = perform_qualification_conversions (type, expr);
if (expr == error_mark_node)
{
tree t = TREE_OPERAND (expression, i);
- /* In some cases, some of the operands may be missing.l
+ /* In some cases, some of the operands may be missing.
(For example, in the case of PREDECREMENT_EXPR, the
amount to increment by may be missing.) That doesn't
make the expression dependent. */
{ };
constexpr A a { };
-B<a> b; // { dg-error "template argument|invalid type" }
+B<a> b; // { dg-error "template argument|converted constant" }
B<> b; // { dg-message "non-type" }
-// { dg-prune-output "could not convert" }
+// { dg-prune-output "(could not convert|no matches)" }
foo ()
{
a<int> a1; // OK
- a<int>::b<a,int> b1; // { dg-error "template argument" }
+ a<int>::b<a,int> b1; // { dg-error "template argument|converted constant" }
}
// { dg-prune-output "invalid type in declaration" }
class BUG2 : BUG
{
public:
- typedef BUG1_5<name> ptr; // { dg-error "could not convert template argument" }
+ typedef BUG1_5<name> ptr; // { dg-error "convert" }
};
int main()
B<int> b; // { dg-error "type/value mismatch|expected a constant|invalid type" }
-// { dg-prune-output "could not convert" }
+// { dg-prune-output "(could not convert|no match)" }
constexpr char const * f() { return s3; }
+using fn_p = char const * (*)();
+template <fn_p> struct A { };
+constexpr fn_p f2() { return f; }
+
+struct B
+{
+ constexpr B() { }
+ constexpr operator const char *() { return s3; }
+};
+
int main()
{
Message<s1> m1; // OK (all versions)
Message<s2> m2; // OK for clang since C++14, for gcc since C++17
Message<s3> m3; // OK for clang/gcc since C++11
+ A<f2()> a1; // { dg-error "" "" { target c++14_down } }
+
static char const s4[] = "hi";
static constexpr char const s5[] = "hi"; // OK since C++11
Message<s4> m4; // { dg-error "no linkage" "" { target c++14_down } }
Message<s5> m5; // { dg-error "no linkage" "" { target c++14_down } }
Message<f()> m6; // { dg-error "" "" { target c++14_down } }
+ Message<B{}> m7; // { dg-error "" "" { target c++14_down } }
+
char const s8[] = "hi";
Message<s8> m8; // { dg-error "" }
template<bool B>
void tester()
{
- bar<evil>()(); // { dg-error "constant|template" }
+ bar<evil>()(); // { dg-error "constant|template|convert" }
}
template<bool B>
struct bar
extern char const xyz;
-class SomethingElse:public Something<xyz> { }; // { dg-error "xyz. is a variable" }
+class SomethingElse:public Something<xyz> { }; // { dg-error "" }
template<const int& i> class X {
public:
void foo() {
- doit<i>(); // { dg-error "cv-qualification|no matching" }
+ doit<i>(); // { dg-error "qualifi|template arg|no matching" }
}
};
void baz()
{
- bar<&B::foo>(); // { dg-error "not a valid template argument|no match" }
+ bar<&B::foo>(); // { dg-error "template argument|no match" }
}
int main ()
{
- Get<&B::I>(); // { dg-error "not a valid template argument" "not valid" }
+ Get<&B::I>(); // { dg-error "template argument|converted constant" "not valid" }
// { dg-error "no match" "no match" { target *-*-* } .-1 }
- // { dg-message "note" "note" { target *-*-* } .-2 }
- Get<&D::I>(); // { dg-error "not a valid template argument" "not valid" }
+ Get<&D::I>(); // { dg-error "template argument|converted constant" "not valid" }
// { dg-error "no match" "no match" { target *-*-* } .-1 }
- // { dg-message "note" "note" { target *-*-* } .-2 }
}