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"
12 #include "InterpFrame.h"
13 #include "InterpStack.h"
19 using namespace clang;
20 using namespace clang::interp;
22 using APSInt = llvm::APSInt;
24 InterpState::InterpState(State &Parent, Program &P, InterpStack &Stk,
25 Context &Ctx, SourceMapper *M)
26 : Parent(Parent), M(M), P(P), Stk(Stk), Ctx(Ctx), Current(nullptr),
27 CallStackDepth(Parent.getCallStackDepth() + 1) {}
29 InterpState::~InterpState() {
31 InterpFrame *Next = Current->Caller;
37 DeadBlock *Next = DeadBlocks->Next;
43 Frame *InterpState::getCurrentFrame() {
44 if (Current && Current->Caller) {
47 return Parent.getCurrentFrame();
51 bool InterpState::reportOverflow(const Expr *E, const llvm::APSInt &Value) {
52 QualType Type = E->getType();
53 CCEDiag(E, diag::note_constexpr_overflow) << Value << Type;
54 return noteUndefinedBehavior();
57 void InterpState::deallocate(Block *B) {
58 Descriptor *Desc = B->getDescriptor();
59 if (B->hasPointers()) {
60 size_t Size = B->getSize();
62 // Allocate a new block, transferring over pointers.
63 char *Memory = reinterpret_cast<char *>(malloc(sizeof(DeadBlock) + Size));
64 auto *D = new (Memory) DeadBlock(DeadBlocks, B);
66 // Move data from one block to another.
68 Desc->MoveFn(B, B->data(), D->data(), Desc);
70 // Free storage, if necessary.
72 Desc->DtorFn(B, B->data(), Desc);