Skip to content

Commit 07e3c24

Browse files
committed
[clang][Interp] Support empty initlist initializers for complex types
Differential Revision: https://reviews.llvm.org/D147369
1 parent 2ab926d commit 07e3c24

File tree

2 files changed

+43
-10
lines changed

2 files changed

+43
-10
lines changed

clang/lib/AST/Interp/ByteCodeExprGen.cpp

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -672,18 +672,28 @@ bool ByteCodeExprGen<Emitter>::VisitInitListExpr(const InitListExpr *E) {
672672
}
673673

674674
if (T->isAnyComplexType()) {
675-
unsigned InitIndex = 0;
676-
for (const Expr *Init : E->inits()) {
677-
PrimType InitT = classifyPrim(Init->getType());
678-
679-
if (!this->visit(Init))
680-
return false;
675+
unsigned NumInits = E->getNumInits();
676+
QualType ElemQT = E->getType()->getAs<ComplexType>()->getElementType();
677+
PrimType ElemT = classifyPrim(ElemQT);
678+
if (NumInits == 0) {
679+
// Zero-initialize both elements.
680+
for (unsigned I = 0; I < 2; ++I) {
681+
if (!this->visitZeroInitializer(ElemT, ElemQT, E))
682+
return false;
683+
if (!this->emitInitElem(ElemT, I, E))
684+
return false;
685+
}
686+
} else if (NumInits == 2) {
687+
unsigned InitIndex = 0;
688+
for (const Expr *Init : E->inits()) {
689+
if (!this->visit(Init))
690+
return false;
681691

682-
if (!this->emitInitElem(InitT, InitIndex, E))
683-
return false;
684-
++InitIndex;
692+
if (!this->emitInitElem(ElemT, InitIndex, E))
693+
return false;
694+
++InitIndex;
695+
}
685696
}
686-
assert(InitIndex == 2);
687697
return true;
688698
}
689699

clang/test/AST/Interp/complex.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,28 @@ static_assert(__real(I1) == 1, "");
2929
static_assert(__imag(I1) == 2, "");
3030

3131

32+
constexpr _Complex double D1 = {};
33+
static_assert(__real(D1) == 0, "");
34+
static_assert(__imag(D1) == 0, "");
35+
36+
constexpr _Complex int I2 = {};
37+
static_assert(__real(I2) == 0, "");
38+
static_assert(__imag(I2) == 0, "");
39+
40+
41+
#if 0
42+
/// FIXME: This should work in the new interpreter.
43+
constexpr _Complex double D2 = {12};
44+
static_assert(__real(D2) == 12, "");
45+
static_assert(__imag(D2) == 12, "");
46+
47+
constexpr _Complex int I3 = {15};
48+
static_assert(__real(I3) == 15, "");
49+
static_assert(__imag(I3) == 15, "");
50+
#endif
51+
52+
53+
54+
3255
/// FIXME: This should work in the new interpreter as well.
3356
// constexpr _Complex _BitInt(8) A = 0;// = {4};

0 commit comments

Comments
 (0)