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

Timm Bäder via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Mar 31 23:46:01 PDT 2023


tbaeder created this revision.
tbaeder added reviewers: aaron.ballman, erichkeane, tahonermann, shafik.
Herald added a project: All.
tbaeder requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D147369

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


Index: clang/test/AST/Interp/complex.cpp
===================================================================
--- clang/test/AST/Interp/complex.cpp
+++ clang/test/AST/Interp/complex.cpp
@@ -29,5 +29,28 @@
 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};
Index: clang/lib/AST/Interp/ByteCodeExprGen.cpp
===================================================================
--- clang/lib/AST/Interp/ByteCodeExprGen.cpp
+++ clang/lib/AST/Interp/ByteCodeExprGen.cpp
@@ -1563,19 +1563,35 @@
 template <class Emitter>
 bool ByteCodeExprGen<Emitter>::visitComplexInitializer(
     const Expr *Initializer) {
+  assert(Initializer->getType()->isAnyComplexType());
   if (const auto *InitList = dyn_cast<InitListExpr>(Initializer)) {
-    unsigned InitIndex = 0;
-    for (const Expr *Init : InitList->inits()) {
-      PrimType InitT = classifyPrim(Init->getType());
-
-      if (!this->visit(Init))
-        return false;
+    unsigned NumInits = InitList->getNumInits();
+    QualType ElemQT =
+        InitList->getType()->getAs<ComplexType>()->getElementType();
+    PrimType ElemT = classifyPrim(ElemQT);
+
+    // FIXME: Initlists with one element contain a
+    //   FloatingRealToComplex cast, not a single floating type.
+
+    if (NumInits == 0) {
+      // Zero-initialize both elements.
+      for (unsigned I = 0; I < 2; ++I) {
+        if (!this->visitZeroInitializer(ElemQT, InitList))
+          return false;
+        if (!this->emitInitElem(ElemT, I, InitList))
+          return false;
+      }
+    } else if (NumInits == 2) {
+      unsigned InitIndex = 0;
+      for (const Expr *Init : InitList->inits()) {
+        if (!this->visit(Init))
+          return false;
 
-      if (!this->emitInitElem(InitT, InitIndex, InitList))
-        return false;
-      ++InitIndex;
+        if (!this->emitInitElem(ElemT, InitIndex, InitList))
+          return false;
+        ++InitIndex;
+      }
     }
-    assert(InitIndex == 2);
     return true;
   } else if (const auto *CE = dyn_cast<CallExpr>(Initializer)) {
     if (!this->emitDupPtr(Initializer))


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D147369.510185.patch
Type: text/x-patch
Size: 2690 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230401/37625100/attachment-0001.bin>


More information about the cfe-commits mailing list