Skip to content

Commit 09bbc90

Browse files
committed
[clang][Interp] Array initialization via ImplicitValueInitExpr
Differential Revision: https://reviews.llvm.org/D135013
1 parent d6cb1fd commit 09bbc90

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

clang/lib/AST/Interp/ByteCodeExprGen.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -721,6 +721,27 @@ bool ByteCodeExprGen<Emitter>::visitArrayInitializer(const Expr *Initializer) {
721721
if (!this->emitPopPtr(Initializer))
722722
return false;
723723
}
724+
return true;
725+
} else if (const auto *IVIE = dyn_cast<ImplicitValueInitExpr>(Initializer)) {
726+
const ArrayType *AT = IVIE->getType()->getAsArrayTypeUnsafe();
727+
assert(AT);
728+
const auto *CAT = cast<ConstantArrayType>(AT);
729+
size_t NumElems = CAT->getSize().getZExtValue();
730+
731+
if (Optional<PrimType> ElemT = classify(CAT->getElementType())) {
732+
// TODO(perf): For int and bool types, we can probably just skip this
733+
// since we memset our Block*s to 0 and so we have the desired value
734+
// without this.
735+
for (size_t I = 0; I != NumElems; ++I) {
736+
if (!this->emitZero(*ElemT, Initializer))
737+
return false;
738+
if (!this->emitInitElem(*ElemT, I, Initializer))
739+
return false;
740+
}
741+
} else {
742+
assert(false && "default initializer for non-primitive type");
743+
}
744+
724745
return true;
725746
}
726747

clang/test/AST/Interp/arrays.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,3 +117,15 @@ namespace indices {
117117
// expected-error {{must be initialized by a constant expression}} \
118118
// expected-note {{cannot refer to element -2 of array of 10}}
119119
};
120+
121+
namespace DefaultInit {
122+
template <typename T, unsigned N>
123+
struct B {
124+
T a[N];
125+
};
126+
127+
int f() {
128+
constexpr B<int,10> arr = {};
129+
constexpr int x = arr.a[0];
130+
}
131+
};

0 commit comments

Comments
 (0)