1 //===--- InterpState.cpp - Interpreter for the constexpr VM -----*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "InterpState.h"
10 #include "InterpFrame.h"
11 #include "InterpStack.h"
15 using namespace clang
;
16 using namespace clang::interp
;
18 InterpState::InterpState(State
&Parent
, Program
&P
, InterpStack
&Stk
,
19 Context
&Ctx
, SourceMapper
*M
)
20 : Parent(Parent
), M(M
), P(P
), Stk(Stk
), Ctx(Ctx
), Current(nullptr) {}
22 InterpState::~InterpState() {
24 InterpFrame
*Next
= Current
->Caller
;
30 DeadBlock
*Next
= DeadBlocks
->Next
;
31 std::free(DeadBlocks
);
36 Frame
*InterpState::getCurrentFrame() {
37 if (Current
&& Current
->Caller
)
39 return Parent
.getCurrentFrame();
42 bool InterpState::reportOverflow(const Expr
*E
, const llvm::APSInt
&Value
) {
43 QualType Type
= E
->getType();
44 CCEDiag(E
, diag::note_constexpr_overflow
) << Value
<< Type
;
45 return noteUndefinedBehavior();
48 void InterpState::deallocate(Block
*B
) {
50 const Descriptor
*Desc
= B
->getDescriptor();
53 if (B
->hasPointers()) {
54 size_t Size
= B
->getSize();
56 // Allocate a new block, transferring over pointers.
58 reinterpret_cast<char *>(std::malloc(sizeof(DeadBlock
) + Size
));
59 auto *D
= new (Memory
) DeadBlock(DeadBlocks
, B
);
61 // Move data and metadata from the old block to the new (dead)block.
63 Desc
->MoveFn(B
, B
->data(), D
->data(), Desc
);
64 if (Desc
->getMetadataSize() > 0)
65 std::memcpy(D
->rawData(), B
->rawData(), Desc
->getMetadataSize());
68 // We moved the contents over to the DeadBlock.
69 B
->IsInitialized
= false;