From 09bbc903a5b454d39d5ce69cf8bf6d5e1b46e3c4 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Timm=20B=C3=A4der?= Date: Fri, 14 Oct 2022 08:19:30 +0200 Subject: [PATCH] [clang][Interp] Array initialization via ImplicitValueInitExpr Differential Revision: https://reviews.llvm.org/D135013 --- clang/lib/AST/Interp/ByteCodeExprGen.cpp | 21 +++++++++++++++++++++ clang/test/AST/Interp/arrays.cpp | 12 ++++++++++++ 2 files changed, 33 insertions(+) diff --git a/clang/lib/AST/Interp/ByteCodeExprGen.cpp b/clang/lib/AST/Interp/ByteCodeExprGen.cpp index ad3348c..872bbf0 100644 --- a/clang/lib/AST/Interp/ByteCodeExprGen.cpp +++ b/clang/lib/AST/Interp/ByteCodeExprGen.cpp @@ -722,6 +722,27 @@ bool ByteCodeExprGen::visitArrayInitializer(const Expr *Initializer) { return false; } return true; + } else if (const auto *IVIE = dyn_cast(Initializer)) { + const ArrayType *AT = IVIE->getType()->getAsArrayTypeUnsafe(); + assert(AT); + const auto *CAT = cast(AT); + size_t NumElems = CAT->getSize().getZExtValue(); + + if (Optional 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; } assert(false && "Unknown expression for array initialization"); diff --git a/clang/test/AST/Interp/arrays.cpp b/clang/test/AST/Interp/arrays.cpp index 318793f..d419994 100644 --- a/clang/test/AST/Interp/arrays.cpp +++ b/clang/test/AST/Interp/arrays.cpp @@ -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 + struct B { + T a[N]; + }; + + int f() { + constexpr B arr = {}; + constexpr int x = arr.a[0]; + } +}; -- 2.7.4