[clang] 07e3c24 - [clang][Interp] Support empty initlist initializers for complex types

Timm Bäder via cfe-commits cfe-commits at lists.llvm.org
Thu Dec 14 03:53:49 PST 2023


Author: Timm Bäder
Date: 2023-12-14T12:53:40+01:00
New Revision: 07e3c245ba2c85222260123cf4559678e0ac2542

URL: https://github.com/llvm/llvm-project/commit/07e3c245ba2c85222260123cf4559678e0ac2542
DIFF: https://github.com/llvm/llvm-project/commit/07e3c245ba2c85222260123cf4559678e0ac2542.diff

LOG: [clang][Interp] Support empty initlist initializers for complex types

Differential Revision: https://reviews.llvm.org/D147369

Added: 
    

Modified: 
    clang/lib/AST/Interp/ByteCodeExprGen.cpp
    clang/test/AST/Interp/complex.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/Interp/ByteCodeExprGen.cpp b/clang/lib/AST/Interp/ByteCodeExprGen.cpp
index efa98c6517a2ef..a4a00ddab65036 100644
--- a/clang/lib/AST/Interp/ByteCodeExprGen.cpp
+++ b/clang/lib/AST/Interp/ByteCodeExprGen.cpp
@@ -672,18 +672,28 @@ bool ByteCodeExprGen<Emitter>::VisitInitListExpr(const InitListExpr *E) {
   }
 
   if (T->isAnyComplexType()) {
-    unsigned InitIndex = 0;
-    for (const Expr *Init : E->inits()) {
-      PrimType InitT = classifyPrim(Init->getType());
-
-      if (!this->visit(Init))
-        return false;
+    unsigned NumInits = E->getNumInits();
+    QualType ElemQT = E->getType()->getAs<ComplexType>()->getElementType();
+    PrimType ElemT = classifyPrim(ElemQT);
+    if (NumInits == 0) {
+      // Zero-initialize both elements.
+      for (unsigned I = 0; I < 2; ++I) {
+        if (!this->visitZeroInitializer(ElemT, ElemQT, E))
+          return false;
+        if (!this->emitInitElem(ElemT, I, E))
+          return false;
+      }
+    } else if (NumInits == 2) {
+      unsigned InitIndex = 0;
+      for (const Expr *Init : E->inits()) {
+        if (!this->visit(Init))
+          return false;
 
-      if (!this->emitInitElem(InitT, InitIndex, E))
-        return false;
-      ++InitIndex;
+        if (!this->emitInitElem(ElemT, InitIndex, E))
+          return false;
+        ++InitIndex;
+      }
     }
-    assert(InitIndex == 2);
     return true;
   }
 

diff  --git a/clang/test/AST/Interp/complex.cpp b/clang/test/AST/Interp/complex.cpp
index 4fd2b5cfd73640..ba9fcd39fdd777 100644
--- a/clang/test/AST/Interp/complex.cpp
+++ b/clang/test/AST/Interp/complex.cpp
@@ -29,5 +29,28 @@ static_assert(__real(I1) == 1, "");
 static_assert(__imag(I1) == 2, "");
 
 
+constexpr _Complex double D1 = {};
+static_assert(__real(D1) == 0, "");
+static_assert(__imag(D1) == 0, "");
+
+constexpr _Complex int I2 = {};
+static_assert(__real(I2) == 0, "");
+static_assert(__imag(I2) == 0, "");
+
+
+#if 0
+/// FIXME: This should work in the new interpreter.
+constexpr _Complex double D2 = {12};
+static_assert(__real(D2) == 12, "");
+static_assert(__imag(D2) == 12, "");
+
+constexpr _Complex int I3 = {15};
+static_assert(__real(I3) == 15, "");
+static_assert(__imag(I3) == 15, "");
+#endif
+
+
+
+
 /// FIXME: This should work in the new interpreter as well.
 // constexpr _Complex _BitInt(8) A = 0;// = {4};


        


More information about the cfe-commits mailing list