Skip to content

[clang][bytecode] Support ImplicitValueInitExpr for multi-dim arrays #117312

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 51 additions & 28 deletions clang/lib/AST/ByteCode/Compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1642,22 +1642,8 @@ bool Compiler<Emitter>::VisitImplicitValueInitExpr(
if (QT->isIncompleteArrayType())
return true;

if (QT->isArrayType()) {
const ArrayType *AT = QT->getAsArrayTypeUnsafe();
assert(AT);
const auto *CAT = cast<ConstantArrayType>(AT);
size_t NumElems = CAT->getZExtSize();
PrimType ElemT = classifyPrim(CAT->getElementType());

for (size_t I = 0; I != NumElems; ++I) {
if (!this->visitZeroInitializer(ElemT, CAT->getElementType(), E))
return false;
if (!this->emitInitElem(ElemT, I, E))
return false;
}

return true;
}
if (QT->isArrayType())
return this->visitZeroArrayInitializer(QT, E);

if (const auto *ComplexTy = E->getType()->getAs<ComplexType>()) {
assert(Initializing);
Expand Down Expand Up @@ -3916,18 +3902,9 @@ bool Compiler<Emitter>::visitZeroRecordInitializer(const Record *R,
return false;
}
} else if (D->isCompositeArray()) {
const Record *ElemRecord = D->ElemDesc->ElemRecord;
assert(D->ElemDesc->ElemRecord);
for (uint32_t I = 0, N = D->getNumElems(); I != N; ++I) {
if (!this->emitConstUint32(I, E))
return false;
if (!this->emitArrayElemPtr(PT_Uint32, E))
return false;
if (!this->visitZeroRecordInitializer(ElemRecord, E))
return false;
if (!this->emitPopPtr(E))
return false;
}
// Can't be a vector or complex field.
if (!this->visitZeroArrayInitializer(D->getType(), E))
return false;
} else if (D->isRecord()) {
if (!this->visitZeroRecordInitializer(D->ElemRecord, E))
return false;
Expand Down Expand Up @@ -3958,6 +3935,52 @@ bool Compiler<Emitter>::visitZeroRecordInitializer(const Record *R,
return true;
}

template <class Emitter>
bool Compiler<Emitter>::visitZeroArrayInitializer(QualType T, const Expr *E) {
assert(T->isArrayType() || T->isAnyComplexType() || T->isVectorType());
const ArrayType *AT = T->getAsArrayTypeUnsafe();
QualType ElemType = AT->getElementType();
size_t NumElems = cast<ConstantArrayType>(AT)->getZExtSize();

if (std::optional<PrimType> ElemT = classify(ElemType)) {
for (size_t I = 0; I != NumElems; ++I) {
if (!this->visitZeroInitializer(*ElemT, ElemType, E))
return false;
if (!this->emitInitElem(*ElemT, I, E))
return false;
}
return true;
} else if (ElemType->isRecordType()) {
const Record *R = getRecord(ElemType);

for (size_t I = 0; I != NumElems; ++I) {
if (!this->emitConstUint32(I, E))
return false;
if (!this->emitArrayElemPtr(PT_Uint32, E))
return false;
if (!this->visitZeroRecordInitializer(R, E))
return false;
if (!this->emitPopPtr(E))
return false;
}
return true;
} else if (ElemType->isArrayType()) {
for (size_t I = 0; I != NumElems; ++I) {
if (!this->emitConstUint32(I, E))
return false;
if (!this->emitArrayElemPtr(PT_Uint32, E))
return false;
if (!this->visitZeroArrayInitializer(ElemType, E))
return false;
if (!this->emitPopPtr(E))
return false;
}
return true;
}

return false;
}

template <class Emitter>
template <typename T>
bool Compiler<Emitter>::emitConst(T Value, PrimType Ty, const Expr *E) {
Expand Down
1 change: 1 addition & 0 deletions clang/lib/AST/ByteCode/Compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,7 @@ class Compiler : public ConstStmtVisitor<Compiler<Emitter>, bool>,
/// Emits a zero initializer.
bool visitZeroInitializer(PrimType T, QualType QT, const Expr *E);
bool visitZeroRecordInitializer(const Record *R, const Expr *E);
bool visitZeroArrayInitializer(QualType T, const Expr *E);

/// Emits an APSInt constant.
bool emitConst(const llvm::APSInt &Value, PrimType Ty, const Expr *E);
Expand Down
7 changes: 6 additions & 1 deletion clang/lib/AST/ByteCode/InterpFrame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,12 @@ SourceInfo InterpFrame::getSource(CodePtr PC) const {
if (Func && !funcHasUsableBody(Func) && Caller)
return Caller->getSource(RetPC);

return S.getSource(Func, PC);
// Similarly, if the resulting source location is invalid anyway,
// point to the caller instead.
SourceInfo Result = S.getSource(Func, PC);
if (Result.getLoc().isInvalid() && Caller)
return Caller->getSource(RetPC);
return Result;
}

const Expr *InterpFrame::getExpr(CodePtr PC) const {
Expand Down
16 changes: 15 additions & 1 deletion clang/test/AST/ByteCode/placement-new.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: %clang_cc1 -std=c++2c -fcxx-exceptions -fexperimental-new-constant-interpreter -verify=expected,both %s
// RUN: %clang_cc1 -std=c++2c -fcxx-exceptions -fexperimental-new-constant-interpreter -verify=expected,both %s -DBYTECODE
// RUN: %clang_cc1 -std=c++2c -fcxx-exceptions -verify=ref,both %s

namespace std {
Expand Down Expand Up @@ -338,3 +338,17 @@ namespace PR48606 {
}
static_assert(f());
}

#ifdef BYTECODE
constexpr int N = [] // expected-error {{must be initialized by a constant expression}} \
// expected-note {{assignment to dereferenced one-past-the-end pointer is not allowed in a constant expression}} \
// expected-note {{in call to}}
{
struct S {
int a[1];
};
S s;
::new (s.a) int[1][2][3][4]();
return s.a[0];
}();
#endif
Loading