1//===--- InterpState.h - Interpreter state for the constexpr VM -*- C++ -*-===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8//
9// Definition of the interpreter state and entry point.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_CLANG_AST_INTERP_INTERPSTATE_H
14#define LLVM_CLANG_AST_INTERP_INTERPSTATE_H
15
16#include "Context.h"
17#include "Function.h"
18#include "InterpFrame.h"
19#include "InterpStack.h"
20#include "State.h"
21#include "clang/AST/APValue.h"
22#include "clang/AST/ASTDiagnostic.h"
23#include "clang/AST/Expr.h"
24#include "clang/AST/OptionalDiagnostic.h"
25
26namespace clang {
27namespace interp {
28class Context;
29class Function;
30class InterpStack;
31class InterpFrame;
32class SourceMapper;
33
34/// Interpreter context.
35class InterpState final : public State, public SourceMapper {
36public:
37 InterpState(State &Parent, Program &P, InterpStack &Stk, Context &Ctx,
38 SourceMapper *M = nullptr);
39
40 ~InterpState();
41
42 InterpState(const InterpState &) = delete;
43 InterpState &operator=(const InterpState &) = delete;
44
45 // Stack frame accessors.
46 Frame *getSplitFrame() { return Parent.getCurrentFrame(); }
47 Frame *getCurrentFrame() override;
48 unsigned getCallStackDepth() override {
49 return Current ? (Current->getDepth() + 1) : 1;
50 }
51 const Frame *getBottomFrame() const override {
52 return Parent.getBottomFrame();
53 }
54
55 // Access objects from the walker context.
56 Expr::EvalStatus &getEvalStatus() const override {
57 return Parent.getEvalStatus();
58 }
59 ASTContext &getCtx() const override { return Parent.getCtx(); }
60
61 // Forward status checks and updates to the walker.
62 bool checkingForUndefinedBehavior() const override {
63 return Parent.checkingForUndefinedBehavior();
64 }
65 bool keepEvaluatingAfterFailure() const override {
66 return Parent.keepEvaluatingAfterFailure();
67 }
68 bool checkingPotentialConstantExpression() const override {
69 return Parent.checkingPotentialConstantExpression();
70 }
71 bool noteUndefinedBehavior() override {
72 return Parent.noteUndefinedBehavior();
73 }
74 bool inConstantContext() const { return Parent.InConstantContext; }
75 bool hasActiveDiagnostic() override { return Parent.hasActiveDiagnostic(); }
76 void setActiveDiagnostic(bool Flag) override {
77 Parent.setActiveDiagnostic(Flag);
78 }
79 void setFoldFailureDiagnostic(bool Flag) override {
80 Parent.setFoldFailureDiagnostic(Flag);
81 }
82 bool hasPriorDiagnostic() override { return Parent.hasPriorDiagnostic(); }
83
84 /// Reports overflow and return true if evaluation should continue.
85 bool reportOverflow(const Expr *E, const llvm::APSInt &Value);
86
87 /// Deallocates a pointer.
88 void deallocate(Block *B);
89
90 /// Delegates source mapping to the mapper.
91 SourceInfo getSource(const Function *F, CodePtr PC) const override {
92 if (M)
93 return M->getSource(F, PC);
94
95 assert(F && "Function cannot be null");
96 return F->getSource(PC);
97 }
98
99 Context &getContext() const { return Ctx; }
100
101private:
102 /// AST Walker state.
103 State &Parent;
104 /// Dead block chain.
105 DeadBlock *DeadBlocks = nullptr;
106 /// Reference to the offset-source mapping.
107 SourceMapper *M;
108
109public:
110 /// Reference to the module containing all bytecode.
111 Program &P;
112 /// Temporary stack.
113 InterpStack &Stk;
114 /// Interpreter Context.
115 Context &Ctx;
116 /// The current frame.
117 InterpFrame *Current = nullptr;
118};
119
120} // namespace interp
121} // namespace clang
122
123#endif
124

source code of clang/lib/AST/Interp/InterpState.h