[PATCH] D133522: [clang] Fix Complex x Float x Int conversions so it handles type sugar

Matheus Izvekov via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Sep 8 14:40:17 PDT 2022


mizvekov updated this revision to Diff 458881.

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D133522/new/

https://reviews.llvm.org/D133522

Files:
  clang/lib/Sema/SemaExpr.cpp
  clang/test/Sema/complex-int.c
  clang/test/SemaCXX/complex-conversion.cpp


Index: clang/test/SemaCXX/complex-conversion.cpp
===================================================================
--- clang/test/SemaCXX/complex-conversion.cpp
+++ clang/test/SemaCXX/complex-conversion.cpp
@@ -15,4 +15,8 @@
 
   // Conversion to bool doesn't actually discard the imaginary part.
   take<bool>(Complex);
+
+  using B = _Complex double;
+  B c;
+  c *= double();
 }
Index: clang/test/Sema/complex-int.c
===================================================================
--- clang/test/Sema/complex-int.c
+++ clang/test/Sema/complex-int.c
@@ -11,6 +11,9 @@
 int bb = 0;
 bb += 1i;
 
+typedef __complex__ float ComplexFloat;
+int cc = 1 + (ComplexFloat)(1.0iF);
+
 result = arr*ii;
 result = ii*brr;
 
Index: clang/lib/Sema/SemaExpr.cpp
===================================================================
--- clang/lib/Sema/SemaExpr.cpp
+++ clang/lib/Sema/SemaExpr.cpp
@@ -1088,7 +1088,7 @@
   if (IntTy->isComplexType() || IntTy->isRealFloatingType()) return true;
   if (SkipCast) return false;
   if (IntTy->isIntegerType()) {
-    QualType fpTy = cast<ComplexType>(ComplexTy)->getElementType();
+    QualType fpTy = ComplexTy->castAs<ComplexType>()->getElementType();
     IntExpr = S.ImpCastExprToType(IntExpr.get(), fpTy, CK_IntegralToFloating);
     IntExpr = S.ImpCastExprToType(IntExpr.get(), ComplexTy,
                                   CK_FloatingRealToComplex);
@@ -1108,10 +1108,10 @@
                                              bool IsCompAssign) {
   // if we have an integer operand, the result is the complex type.
   if (!handleIntegerToComplexFloatConversion(S, RHS, LHS, RHSType, LHSType,
-                                             /*skipCast*/false))
+                                             /*SkipCast=*/false))
     return LHSType;
   if (!handleIntegerToComplexFloatConversion(S, LHS, RHS, LHSType, RHSType,
-                                             /*skipCast*/IsCompAssign))
+                                             /*SkipCast=*/IsCompAssign))
     return RHSType;
 
   // This handles complex/complex, complex/float, or float/complex.
@@ -1125,20 +1125,17 @@
   // when combining a "long double" with a "double _Complex", the
   // "double _Complex" is promoted to "long double _Complex".
 
+  auto *LHSComplexType = LHSType->getAs<ComplexType>();
+  auto *RHSComplexType = RHSType->getAs<ComplexType>();
+
   // Compute the rank of the two types, regardless of whether they are complex.
   int Order = S.Context.getFloatingTypeOrder(LHSType, RHSType);
-
-  auto *LHSComplexType = dyn_cast<ComplexType>(LHSType);
-  auto *RHSComplexType = dyn_cast<ComplexType>(RHSType);
-  QualType LHSElementType =
-      LHSComplexType ? LHSComplexType->getElementType() : LHSType;
-  QualType RHSElementType =
-      RHSComplexType ? RHSComplexType->getElementType() : RHSType;
-
-  QualType ResultType = S.Context.getComplexType(LHSElementType);
   if (Order < 0) {
+    QualType RHSElementType =
+        RHSComplexType ? RHSComplexType->getElementType() : RHSType;
+    QualType ResultType =
+        RHSComplexType ? RHSType : S.Context.getComplexType(RHSElementType);
     // Promote the precision of the LHS if not an assignment.
-    ResultType = S.Context.getComplexType(RHSElementType);
     if (!IsCompAssign) {
       if (LHSComplexType)
         LHS =
@@ -1146,7 +1143,13 @@
       else
         LHS = S.ImpCastExprToType(LHS.get(), RHSElementType, CK_FloatingCast);
     }
-  } else if (Order > 0) {
+    return ResultType;
+  }
+  QualType LHSElementType =
+      LHSComplexType ? LHSComplexType->getElementType() : LHSType;
+  QualType ResultType =
+      LHSComplexType ? LHSType : S.Context.getComplexType(LHSElementType);
+  if (Order > 0) {
     // Promote the precision of the RHS.
     if (RHSComplexType)
       RHS = S.ImpCastExprToType(RHS.get(), ResultType, CK_FloatingComplexCast);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D133522.458881.patch
Type: text/x-patch
Size: 3859 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220908/637adf5e/attachment-0001.bin>


More information about the cfe-commits mailing list