Skip to content

Commit f009991

Browse files
committed
fp conversions
1 parent 453460f commit f009991

File tree

3 files changed

+48
-24
lines changed

3 files changed

+48
-24
lines changed

clang/include/clang/Sema/Sema.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7492,10 +7492,15 @@ class Sema final : public SemaBase {
74927492
return K == ConditionKind::Switch ? Context.IntTy : Context.BoolTy;
74937493
}
74947494

7495-
// UsualUnaryConversions - promotes integers (C99 6.3.1.1p2) and converts
7496-
// functions and arrays to their respective pointers (C99 6.3.2.1).
7495+
// UsualUnaryConversions - promotes integers (C99 6.3.1.1p2), converts
7496+
// functions and arrays to their respective pointers (C99 6.3.2.1), and
7497+
// promotes floating-piont types according to the language semantics.
74977498
ExprResult UsualUnaryConversions(Expr *E);
74987499

7500+
// UsualUnaryFPConversions - promotes floating-point types according to the
7501+
// current language semantics.
7502+
ExprResult UsualUnaryFPConversions(Expr *E);
7503+
74997504
/// CallExprUnaryConversions - a special case of an unary conversion
75007505
/// performed on a function designator of a call expression.
75017506
ExprResult CallExprUnaryConversions(Expr *E);

clang/lib/Sema/SemaChecking.cpp

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14594,18 +14594,23 @@ void Sema::CheckAddressOfPackedMember(Expr *rhs) {
1459414594
_2, _3, _4));
1459514595
}
1459614596

14597-
static ExprResult UsualUnaryConversionsNoPromoteInt(Sema &S, Expr *E) {
14598-
// Don't promote integer types
14599-
if (QualType Ty = E->getType(); S.getASTContext().isPromotableIntegerType(Ty))
14600-
return S.DefaultFunctionArrayLvalueConversion(E);
14601-
return S.UsualUnaryConversions(E);
14597+
// Performs a similar job to Sema::UsualUnaryConversions, but without any
14598+
// implicit promotion of integral/enumeration types.
14599+
static ExprResult BuiltinVectorMathConversions(Sema &S, Expr *E) {
14600+
// First, convert to an r-value.
14601+
ExprResult Res = S.DefaultFunctionArrayLvalueConversion(E);
14602+
if (Res.isInvalid())
14603+
return ExprError();
14604+
14605+
// Promote floating-point types.
14606+
return S.UsualUnaryFPConversions(Res.get());
1460214607
}
1460314608

1460414609
bool Sema::PrepareBuiltinElementwiseMathOneArgCall(CallExpr *TheCall) {
1460514610
if (checkArgCount(TheCall, 1))
1460614611
return true;
1460714612

14608-
ExprResult A = UsualUnaryConversionsNoPromoteInt(*this, TheCall->getArg(0));
14613+
ExprResult A = BuiltinVectorMathConversions(*this, TheCall->getArg(0));
1460914614
if (A.isInvalid())
1461014615
return true;
1461114616

@@ -14651,7 +14656,7 @@ std::optional<QualType> Sema::BuiltinVectorMath(CallExpr *TheCall,
1465114656
Expr *Args[2];
1465214657
for (int I = 0; I < 2; ++I) {
1465314658
ExprResult Converted =
14654-
UsualUnaryConversionsNoPromoteInt(*this, TheCall->getArg(I));
14659+
BuiltinVectorMathConversions(*this, TheCall->getArg(I));
1465514660
if (Converted.isInvalid())
1465614661
return std::nullopt;
1465714662
Args[I] = Converted.get();
@@ -14687,7 +14692,7 @@ bool Sema::BuiltinElementwiseTernaryMath(CallExpr *TheCall,
1468714692
Expr *Args[3];
1468814693
for (int I = 0; I < 3; ++I) {
1468914694
ExprResult Converted =
14690-
UsualUnaryConversionsNoPromoteInt(*this, TheCall->getArg(I));
14695+
BuiltinVectorMathConversions(*this, TheCall->getArg(I));
1469114696
if (Converted.isInvalid())
1469214697
return true;
1469314698
Args[I] = Converted.get();

clang/lib/Sema/SemaExpr.cpp

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -776,20 +776,11 @@ ExprResult Sema::CallExprUnaryConversions(Expr *E) {
776776
return Res.get();
777777
}
778778

779-
/// UsualUnaryConversions - Performs various conversions that are common to most
780-
/// operators (C99 6.3). The conversions of array and function types are
781-
/// sometimes suppressed. For example, the array->pointer conversion doesn't
782-
/// apply if the array is an argument to the sizeof or address (&) operators.
783-
/// In these instances, this routine should *not* be called.
784-
ExprResult Sema::UsualUnaryConversions(Expr *E) {
785-
// First, convert to an r-value.
786-
ExprResult Res = DefaultFunctionArrayLvalueConversion(E);
787-
if (Res.isInvalid())
788-
return ExprError();
789-
E = Res.get();
790-
779+
/// UsualUnaryFPConversions - Promotes floating-point types according to the
780+
/// current language semantics.
781+
ExprResult Sema::UsualUnaryFPConversions(Expr *E) {
791782
QualType Ty = E->getType();
792-
assert(!Ty.isNull() && "UsualUnaryConversions - missing type");
783+
assert(!Ty.isNull() && "UsualUnaryFPConversions - missing type");
793784

794785
LangOptions::FPEvalMethodKind EvalMethod = CurFPFeatures.getFPEvalMethod();
795786
if (EvalMethod != LangOptions::FEM_Source && Ty->isFloatingType() &&
@@ -827,7 +818,30 @@ ExprResult Sema::UsualUnaryConversions(Expr *E) {
827818

828819
// Half FP have to be promoted to float unless it is natively supported
829820
if (Ty->isHalfType() && !getLangOpts().NativeHalfType)
830-
return ImpCastExprToType(Res.get(), Context.FloatTy, CK_FloatingCast);
821+
return ImpCastExprToType(E, Context.FloatTy, CK_FloatingCast);
822+
823+
return E;
824+
}
825+
826+
/// UsualUnaryConversions - Performs various conversions that are common to most
827+
/// operators (C99 6.3). The conversions of array and function types are
828+
/// sometimes suppressed. For example, the array->pointer conversion doesn't
829+
/// apply if the array is an argument to the sizeof or address (&) operators.
830+
/// In these instances, this routine should *not* be called.
831+
ExprResult Sema::UsualUnaryConversions(Expr *E) {
832+
// First, convert to an r-value.
833+
ExprResult Res = DefaultFunctionArrayLvalueConversion(E);
834+
if (Res.isInvalid())
835+
return ExprError();
836+
837+
// Promote floating-point types.
838+
Res = UsualUnaryFPConversions(Res.get());
839+
if (Res.isInvalid())
840+
return ExprError();
841+
E = Res.get();
842+
843+
QualType Ty = E->getType();
844+
assert(!Ty.isNull() && "UsualUnaryConversions - missing type");
831845

832846
// Try to perform integral promotions if the object has a theoretically
833847
// promotable type.

0 commit comments

Comments
 (0)