[clang][Interp] Array initialization via ImplicitValueInitExpr
authorTimm Bäder <tbaeder@redhat.com>
Fri, 14 Oct 2022 06:19:30 +0000 (08:19 +0200)
committerTimm Bäder <tbaeder@redhat.com>
Fri, 21 Oct 2022 08:49:45 +0000 (10:49 +0200)
Differential Revision: https://reviews.llvm.org/D135013

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

index ad3348cee4e2fe8fcb640f6884fa45e128b305f8..872bbf045cef23ab67fa1d0efe669fabde9c7dc2 100644 (file)
@@ -721,6 +721,27 @@ bool ByteCodeExprGen<Emitter>::visitArrayInitializer(const Expr *Initializer) {
       if (!this->emitPopPtr(Initializer))
         return false;
     }
+    return true;
+  } else if (const auto *IVIE = dyn_cast<ImplicitValueInitExpr>(Initializer)) {
+    const ArrayType *AT = IVIE->getType()->getAsArrayTypeUnsafe();
+    assert(AT);
+    const auto *CAT = cast<ConstantArrayType>(AT);
+    size_t NumElems = CAT->getSize().getZExtValue();
+
+    if (Optional<PrimType> ElemT = classify(CAT->getElementType())) {
+      // 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;
   }
 
index 318793ff777a03e55dee40a42ae9669c5189c08c..d41999414f5b65c8c0b1e5f0f7d29f4b5c628ebd 100644 (file)
@@ -117,3 +117,15 @@ namespace indices {
                                        // expected-error {{must be initialized by a constant expression}} \
                                        // expected-note {{cannot refer to element -2 of array of 10}}
 };
+
+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];
+  }
+};