diff options
author | Richard Smith <richard-llvm@metafoo.co.uk> | 2012-01-18 23:55:52 +0000 |
---|---|---|
committer | Richard Smith <richard-llvm@metafoo.co.uk> | 2012-01-18 23:55:52 +0000 |
commit | 8ef7b203332b0c8d65876a1f5e6d1db4e6f40e4b (patch) | |
tree | dee9b186a48a66f3c8be5c56c4e774338408bece /lib/Sema/SemaTemplate.cpp | |
parent | d6c7c67313634b317a0d63c32be0511a121bb33d (diff) |
constexpr: converted constant expression handling for enumerator values, case
values and non-type template arguments of integral and enumeration types.
This change causes some legal C++98 code to no longer compile in C++11 mode, by
enforcing the C++11 rule that narrowing integral conversions are not permitted
in the final implicit conversion sequence for the above cases.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@148439 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Sema/SemaTemplate.cpp')
-rw-r--r-- | lib/Sema/SemaTemplate.cpp | 81 |
1 files changed, 62 insertions, 19 deletions
diff --git a/lib/Sema/SemaTemplate.cpp b/lib/Sema/SemaTemplate.cpp index f58e965934..0c66133968 100644 --- a/lib/Sema/SemaTemplate.cpp +++ b/lib/Sema/SemaTemplate.cpp @@ -3737,14 +3737,70 @@ ExprResult Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param, // template-argument cannot be converted to the type of the // corresponding template-parameter then the program is // ill-formed. - // - // -- for a non-type template-parameter of integral or - // enumeration type, integral promotions (4.5) and integral - // conversions (4.7) are applied. QualType ParamType = InstantiatedParamType; if (ParamType->isIntegralOrEnumerationType()) { - // FIXME: In C++11, the argument is a converted constant expression of the - // type of the template parameter. + // C++11: + // -- for a non-type template-parameter of integral or + // enumeration type, conversions permitted in a converted + // constant expression are applied. + // + // C++98: + // -- for a non-type template-parameter of integral or + // enumeration type, integral promotions (4.5) and integral + // conversions (4.7) are applied. + + if (CTAK == CTAK_Deduced && + !Context.hasSameUnqualifiedType(ParamType, Arg->getType())) { + // C++ [temp.deduct.type]p17: + // If, in the declaration of a function template with a non-type + // template-parameter, the non-type template-parameter is used + // in an expression in the function parameter-list and, if the + // corresponding template-argument is deduced, the + // template-argument type shall match the type of the + // template-parameter exactly, except that a template-argument + // deduced from an array bound may be of any integral type. + Diag(StartLoc, diag::err_deduced_non_type_template_arg_type_mismatch) + << Arg->getType().getUnqualifiedType() + << ParamType.getUnqualifiedType(); + Diag(Param->getLocation(), diag::note_template_param_here); + return ExprError(); + } + + if (getLangOptions().CPlusPlus0x) { + // We can't check arbitrary value-dependent arguments. + // FIXME: If there's no viable conversion to the template parameter type, + // we should be able to diagnose that prior to instantiation. + if (Arg->isValueDependent()) { + Converted = TemplateArgument(Arg); + return Owned(Arg); + } + + // C++ [temp.arg.nontype]p1: + // A template-argument for a non-type, non-template template-parameter + // shall be one of: + // + // -- for a non-type template-parameter of integral or enumeration + // type, a converted constant expression of the type of the + // template-parameter; or + llvm::APSInt Value; + ExprResult ArgResult = + CheckConvertedConstantExpression(Arg, ParamType, Value, + CCEK_TemplateArg); + if (ArgResult.isInvalid()) + return ExprError(); + + // Widen the argument value to sizeof(parameter type). This is almost + // always a no-op, except when the parameter type is bool. In + // that case, this may extend the argument from 1 bit to 8 bits. + QualType IntegerType = ParamType; + if (const EnumType *Enum = IntegerType->getAs<EnumType>()) + IntegerType = Enum->getDecl()->getIntegerType(); + Value = Value.extOrTrunc(Context.getTypeSize(IntegerType)); + + Converted = TemplateArgument(Value, Context.getCanonicalType(ParamType)); + return ArgResult; + } + ExprResult ArgResult = DefaultLvalueConversion(Arg); if (ArgResult.isInvalid()) return ExprError(); @@ -3782,19 +3838,6 @@ ExprResult Sema::CheckTemplateArgument(NonTypeTemplateParmDecl *Param, // Try to convert the argument to the parameter's type. if (Context.hasSameType(ParamType, ArgType)) { // Okay: no conversion necessary - } else if (CTAK == CTAK_Deduced) { - // C++ [temp.deduct.type]p17: - // If, in the declaration of a function template with a non-type - // template-parameter, the non-type template- parameter is used - // in an expression in the function parameter-list and, if the - // corresponding template-argument is deduced, the - // template-argument type shall match the type of the - // template-parameter exactly, except that a template-argument - // deduced from an array bound may be of any integral type. - Diag(StartLoc, diag::err_deduced_non_type_template_arg_type_mismatch) - << ArgType << ParamType; - Diag(Param->getLocation(), diag::note_template_param_here); - return ExprError(); } else if (ParamType->isBooleanType()) { // This is an integral-to-boolean conversion. Arg = ImpCastExprToType(Arg, ParamType, CK_IntegralToBoolean).take(); |