diff options
author | PiJoules <6019989+PiJoules@users.noreply.github.com> | 2023-11-16 13:11:15 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-11-16 13:11:15 -0800 |
commit | b2d62c9a58433e2a2ca8d2c9cd6b0b612dca2e76 (patch) | |
tree | f07039a27f9578a0e5c0d2c69279850831abea9d /clang | |
parent | cdfb51295d814a875925974364931ef4337641e1 (diff) | |
download | llvm-b2d62c9a58433e2a2ca8d2c9cd6b0b612dca2e76.zip llvm-b2d62c9a58433e2a2ca8d2c9cd6b0b612dca2e76.tar.gz llvm-b2d62c9a58433e2a2ca8d2c9cd6b0b612dca2e76.tar.bz2 |
[clang] Ensure fixed point conversions work in C++ (#68344)
Diffstat (limited to 'clang')
-rw-r--r-- | clang/include/clang/Sema/Overload.h | 3 | ||||
-rw-r--r-- | clang/lib/Sema/SemaDecl.cpp | 3 | ||||
-rw-r--r-- | clang/lib/Sema/SemaExprCXX.cpp | 30 | ||||
-rw-r--r-- | clang/lib/Sema/SemaOverload.cpp | 10 | ||||
-rw-r--r-- | clang/test/Frontend/fixed_point_conversions.c | 14 |
5 files changed, 56 insertions, 4 deletions
diff --git a/clang/include/clang/Sema/Overload.h b/clang/include/clang/Sema/Overload.h index a97968d..333309a 100644 --- a/clang/include/clang/Sema/Overload.h +++ b/clang/include/clang/Sema/Overload.h @@ -192,6 +192,9 @@ class Sema; /// C-only conversion between pointers with incompatible types ICK_Incompatible_Pointer_Conversion, + /// Fixed point type conversions according to N1169. + ICK_Fixed_Point_Conversion, + /// The number of conversion kinds ICK_Num_Conversion_Kinds, }; diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp index a6cd0bb..da8e2db 100644 --- a/clang/lib/Sema/SemaDecl.cpp +++ b/clang/lib/Sema/SemaDecl.cpp @@ -148,6 +148,9 @@ bool Sema::isSimpleTypeSpecifier(tok::TokenKind Kind) const { case tok::kw___ibm128: case tok::kw_wchar_t: case tok::kw_bool: + case tok::kw__Accum: + case tok::kw__Fract: + case tok::kw__Sat: #define TRANSFORM_TYPE_TRAIT_DEF(_, Trait) case tok::kw___##Trait: #include "clang/Basic/TransformTypeTraits.def" case tok::kw___auto_type: diff --git a/clang/lib/Sema/SemaExprCXX.cpp b/clang/lib/Sema/SemaExprCXX.cpp index 023411c..081b568 100644 --- a/clang/lib/Sema/SemaExprCXX.cpp +++ b/clang/lib/Sema/SemaExprCXX.cpp @@ -4506,6 +4506,36 @@ Sema::PerformImplicitConversion(Expr *From, QualType ToType, .get(); break; + case ICK_Fixed_Point_Conversion: + assert((FromType->isFixedPointType() || ToType->isFixedPointType()) && + "Attempting implicit fixed point conversion without a fixed " + "point operand"); + if (FromType->isFloatingType()) + From = ImpCastExprToType(From, ToType, CK_FloatingToFixedPoint, + VK_PRValue, + /*BasePath=*/nullptr, CCK).get(); + else if (ToType->isFloatingType()) + From = ImpCastExprToType(From, ToType, CK_FixedPointToFloating, + VK_PRValue, + /*BasePath=*/nullptr, CCK).get(); + else if (FromType->isIntegralType(Context)) + From = ImpCastExprToType(From, ToType, CK_IntegralToFixedPoint, + VK_PRValue, + /*BasePath=*/nullptr, CCK).get(); + else if (ToType->isIntegralType(Context)) + From = ImpCastExprToType(From, ToType, CK_FixedPointToIntegral, + VK_PRValue, + /*BasePath=*/nullptr, CCK).get(); + else if (ToType->isBooleanType()) + From = ImpCastExprToType(From, ToType, CK_FixedPointToBoolean, + VK_PRValue, + /*BasePath=*/nullptr, CCK).get(); + else + From = ImpCastExprToType(From, ToType, CK_FixedPointCast, + VK_PRValue, + /*BasePath=*/nullptr, CCK).get(); + break; + case ICK_Compatible_Conversion: From = ImpCastExprToType(From, ToType, CK_NoOp, From->getValueKind(), /*BasePath=*/nullptr, CCK).get(); diff --git a/clang/lib/Sema/SemaOverload.cpp b/clang/lib/Sema/SemaOverload.cpp index 858654e..9800d7f 100644 --- a/clang/lib/Sema/SemaOverload.cpp +++ b/clang/lib/Sema/SemaOverload.cpp @@ -158,7 +158,8 @@ ImplicitConversionRank clang::GetConversionRank(ImplicitConversionKind Kind) { // it was omitted by the patch that added // ICK_Zero_Queue_Conversion ICR_C_Conversion, - ICR_C_Conversion_Extension + ICR_C_Conversion_Extension, + ICR_Conversion, }; static_assert(std::size(Rank) == (int)ICK_Num_Conversion_Kinds); return Rank[(int)Kind]; @@ -197,7 +198,8 @@ static const char* GetImplicitConversionName(ImplicitConversionKind Kind) { "OpenCL Zero Event Conversion", "OpenCL Zero Queue Conversion", "C specific type conversion", - "Incompatible pointer conversion" + "Incompatible pointer conversion", + "Fixed point conversion", }; static_assert(std::size(Name) == (int)ICK_Num_Conversion_Kinds); return Name[Kind]; @@ -2189,6 +2191,9 @@ static bool IsStandardConversion(Sema &S, Expr* From, QualType ToType, From->isIntegerConstantExpr(S.getASTContext())) { SCS.Second = ICK_Compatible_Conversion; FromType = ToType; + } else if (ToType->isFixedPointType() || FromType->isFixedPointType()) { + SCS.Second = ICK_Fixed_Point_Conversion; + FromType = ToType; } else { // No second conversion required. SCS.Second = ICK_Identity; @@ -5947,6 +5952,7 @@ static bool CheckConvertedConstantConversions(Sema &S, case ICK_Zero_Event_Conversion: case ICK_C_Only_Conversion: case ICK_Incompatible_Pointer_Conversion: + case ICK_Fixed_Point_Conversion: return false; case ICK_Lvalue_To_Rvalue: diff --git a/clang/test/Frontend/fixed_point_conversions.c b/clang/test/Frontend/fixed_point_conversions.c index ebd1d7e..efa3f1b 100644 --- a/clang/test/Frontend/fixed_point_conversions.c +++ b/clang/test/Frontend/fixed_point_conversions.c @@ -1,6 +1,12 @@ // NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py -// RUN: %clang_cc1 -ffixed-point -triple x86_64-unknown-linux-gnu -S -emit-llvm %s -o - | FileCheck %s --check-prefixes=CHECK,SIGNED -// RUN: %clang_cc1 -ffixed-point -triple x86_64-unknown-linux-gnu -S -emit-llvm %s -o - -fpadding-on-unsigned-fixed-point | FileCheck %s --check-prefixes=CHECK,UNSIGNED +// RUN: %clang_cc1 -x c -ffixed-point -triple x86_64-unknown-linux-gnu -S -emit-llvm %s -o - | FileCheck %s --check-prefixes=CHECK,SIGNED +// RUN: %clang_cc1 -x c -ffixed-point -triple x86_64-unknown-linux-gnu -S -emit-llvm %s -o - -fpadding-on-unsigned-fixed-point | FileCheck %s --check-prefixes=CHECK,UNSIGNED +// RUN: %clang_cc1 -x c++ -ffixed-point -triple x86_64-unknown-linux-gnu -S -emit-llvm %s -o - | FileCheck %s --check-prefixes=CHECK,SIGNED +// RUN: %clang_cc1 -x c++ -ffixed-point -triple x86_64-unknown-linux-gnu -S -emit-llvm %s -o - -fpadding-on-unsigned-fixed-point | FileCheck %s --check-prefixes=CHECK,UNSIGNED + +#ifdef __cplusplus +extern "C" { +#endif short _Accum sa; _Accum a, a2; @@ -994,3 +1000,7 @@ void float_sat5(void) { void float_sat6(void) { sat_uf = fl; } + +#ifdef __cplusplus +} +#endif |