[PATCH] D135013: [clang][Interp] Array initialization via ImplicitValueInitExpr

Timm Bäder via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Oct 13 23:21:31 PDT 2022


tbaeder updated this revision to Diff 467691.
tbaeder marked 2 inline comments as done.

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

https://reviews.llvm.org/D135013

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


Index: clang/test/AST/Interp/arrays.cpp
===================================================================
--- clang/test/AST/Interp/arrays.cpp
+++ clang/test/AST/Interp/arrays.cpp
@@ -98,3 +98,15 @@
 struct fred y [] = { [0] = { .s[0] = 'q' } };
 #endif
 #pragma clang diagnostic pop
+
+namespace DefaultInit {
+  template <typename T, unsigned N>
+  struct B {
+    T a[N];
+  };
+
+  int f() {
+     constexpr B<int,10> arr = {};
+     constexpr int x = arr.a[0];
+  }
+};
Index: clang/lib/AST/Interp/ByteCodeExprGen.cpp
===================================================================
--- clang/lib/AST/Interp/ByteCodeExprGen.cpp
+++ clang/lib/AST/Interp/ByteCodeExprGen.cpp
@@ -720,6 +720,28 @@
       if (!this->emitPopPtr(Initializer))
         return false;
     }
+    return true;
+  } else if (const auto *IVIE = dyn_cast<ImplicitValueInitExpr>(Initializer)) {
+    auto ArrayType = IVIE->getType()->getAsArrayTypeUnsafe();
+    assert(ArrayType);
+    const auto *CAT = dyn_cast<ConstantArrayType>(ArrayType);
+    const size_t NumElems = CAT->getSize().getZExtValue();
+    Optional<PrimType> ElemT = classify(CAT->getElementType());
+
+    if (ElemT) {
+      // TODO(perf): For int and bool types, we can probably just skip this
+      //   since we memset our Block*s to 0 and so we have the desired value
+      //   without this.
+      for (size_t I = 0; I != NumElems; ++I) {
+        if (!this->emitZero(*ElemT, Initializer))
+          return false;
+        if (!this->emitInitElem(*ElemT, I, Initializer))
+          return false;
+      }
+    } else {
+      assert(false && "default initializer for non-primitive type");
+    }
+
     return true;
   }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D135013.467691.patch
Type: text/x-patch
Size: 1686 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20221014/e78fc5af/attachment.bin>


More information about the cfe-commits mailing list