Skip to content
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
37 changes: 29 additions & 8 deletions clang/lib/AST/ByteCode/Interp.h
Original file line number Diff line number Diff line change
Expand Up @@ -1863,27 +1863,48 @@ bool InitPop(InterpState &S, CodePtr OpPC) {
template <PrimType Name, class T = typename PrimConv<Name>::T>
bool InitElem(InterpState &S, CodePtr OpPC, uint32_t Idx) {
const T &Value = S.Stk.pop<T>();
const Pointer &Ptr = S.Stk.peek<Pointer>().atIndex(Idx);
const Pointer &Ptr = S.Stk.peek<Pointer>();

if (Ptr.isUnknownSizeArray())
return false;
if (!CheckInit(S, OpPC, Ptr))

// In the unlikely event that we're initializing the first item of
// a non-array, skip the atIndex().
if (Idx == 0 && !Ptr.getFieldDesc()->isArray()) {
Ptr.initialize();
new (&Ptr.deref<T>()) T(Value);
return true;
}

const Pointer &ElemPtr = Ptr.atIndex(Idx);
if (!CheckInit(S, OpPC, ElemPtr))
return false;
Ptr.initialize();
new (&Ptr.deref<T>()) T(Value);
ElemPtr.initialize();
new (&ElemPtr.deref<T>()) T(Value);
return true;
}

/// The same as InitElem, but pops the pointer as well.
template <PrimType Name, class T = typename PrimConv<Name>::T>
bool InitElemPop(InterpState &S, CodePtr OpPC, uint32_t Idx) {
const T &Value = S.Stk.pop<T>();
const Pointer &Ptr = S.Stk.pop<Pointer>().atIndex(Idx);
const Pointer &Ptr = S.Stk.pop<Pointer>();
if (Ptr.isUnknownSizeArray())
return false;
if (!CheckInit(S, OpPC, Ptr))

// In the unlikely event that we're initializing the first item of
// a non-array, skip the atIndex().
if (Idx == 0 && !Ptr.getFieldDesc()->isArray()) {
Ptr.initialize();
new (&Ptr.deref<T>()) T(Value);
return true;
}

const Pointer &ElemPtr = Ptr.atIndex(Idx);
if (!CheckInit(S, OpPC, ElemPtr))
return false;
Ptr.initialize();
new (&Ptr.deref<T>()) T(Value);
ElemPtr.initialize();
new (&ElemPtr.deref<T>()) T(Value);
return true;
}

Expand Down
14 changes: 14 additions & 0 deletions clang/test/AST/ByteCode/placement-new.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,20 @@ consteval auto ok4() {
}
static_assert(ok4() == 37);

consteval int ok5() {
int i;
new (&i) int[1]{1};

struct S {
int a; int b;
} s;
new (&s) S[1]{{12, 13}};

return 25;
// return s.a + s.b; FIXME: Broken in the current interpreter.
}
static_assert(ok5() == 25);

/// FIXME: Broken in both interpreters.
#if 0
consteval int ok5() {
Expand Down
Loading