Skip to content

Commit e524ada

Browse files
authored
[clang][Interp] Support zero init for complex types (#79728)
Initialize both elements to 0.
1 parent de46dc9 commit e524ada

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

clang/lib/AST/Interp/ByteCodeExprGen.cpp

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1939,10 +1939,32 @@ bool ByteCodeExprGen<Emitter>::VisitCXXScalarValueInitExpr(
19391939
const CXXScalarValueInitExpr *E) {
19401940
QualType Ty = E->getType();
19411941

1942-
if (Ty->isVoidType())
1942+
if (DiscardResult || Ty->isVoidType())
19431943
return true;
19441944

1945-
return this->visitZeroInitializer(classifyPrim(Ty), Ty, E);
1945+
if (std::optional<PrimType> T = classify(Ty))
1946+
return this->visitZeroInitializer(*T, Ty, E);
1947+
1948+
assert(Ty->isAnyComplexType());
1949+
if (!Initializing) {
1950+
std::optional<unsigned> LocalIndex = allocateLocal(E, /*IsExtended=*/false);
1951+
if (!LocalIndex)
1952+
return false;
1953+
if (!this->emitGetPtrLocal(*LocalIndex, E))
1954+
return false;
1955+
}
1956+
1957+
// Initialize both fields to 0.
1958+
QualType ElemQT = Ty->getAs<ComplexType>()->getElementType();
1959+
PrimType ElemT = classifyPrim(ElemQT);
1960+
1961+
for (unsigned I = 0; I != 2; ++I) {
1962+
if (!this->visitZeroInitializer(ElemT, ElemQT, E))
1963+
return false;
1964+
if (!this->emitInitElem(ElemT, I, E))
1965+
return false;
1966+
}
1967+
return true;
19461968
}
19471969

19481970
template <class Emitter>

clang/test/AST/Interp/complex.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,3 +179,18 @@ namespace Sub {
179179
}
180180

181181
}
182+
183+
namespace ZeroInit {
184+
typedef _Complex float fcomplex;
185+
typedef _Complex unsigned icomplex;
186+
187+
constexpr fcomplex test7 = fcomplex();
188+
static_assert(__real(test7) == 0.0f, "");
189+
static_assert(__imag(test7) == 0.0f, "");
190+
191+
constexpr icomplex test8 = icomplex();
192+
static_assert(__real(test8) == 0, "");
193+
static_assert(__imag(test8) == 0, "");
194+
195+
constexpr int ignored = (fcomplex(), 0);
196+
}

0 commit comments

Comments
 (0)