clang  14.0.0git
InterpState.h
Go to the documentation of this file.
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 "InterpStack.h"
19 #include "State.h"
20 #include "clang/AST/APValue.h"
22 #include "clang/AST/Expr.h"
24 
25 namespace clang {
26 namespace interp {
27 class Context;
28 class Function;
29 class InterpStack;
30 class InterpFrame;
31 class SourceMapper;
32 
33 /// Interpreter context.
34 class InterpState final : public State, public SourceMapper {
35 public:
37  SourceMapper *M = nullptr);
38 
39  ~InterpState();
40 
41  // Stack frame accessors.
42  Frame *getSplitFrame() { return Parent.getCurrentFrame(); }
43  Frame *getCurrentFrame() override;
44  unsigned getCallStackDepth() override { return CallStackDepth; }
45  const Frame *getBottomFrame() const override {
46  return Parent.getBottomFrame();
47  }
48 
49  // Access objects from the walker context.
50  Expr::EvalStatus &getEvalStatus() const override {
51  return Parent.getEvalStatus();
52  }
53  ASTContext &getCtx() const override { return Parent.getCtx(); }
54 
55  // Forward status checks and updates to the walker.
56  bool checkingForUndefinedBehavior() const override {
57  return Parent.checkingForUndefinedBehavior();
58  }
59  bool keepEvaluatingAfterFailure() const override {
60  return Parent.keepEvaluatingAfterFailure();
61  }
62  bool checkingPotentialConstantExpression() const override {
63  return Parent.checkingPotentialConstantExpression();
64  }
65  bool noteUndefinedBehavior() override {
66  return Parent.noteUndefinedBehavior();
67  }
68  bool hasActiveDiagnostic() override { return Parent.hasActiveDiagnostic(); }
69  void setActiveDiagnostic(bool Flag) override {
70  Parent.setActiveDiagnostic(Flag);
71  }
72  void setFoldFailureDiagnostic(bool Flag) override {
73  Parent.setFoldFailureDiagnostic(Flag);
74  }
75  bool hasPriorDiagnostic() override { return Parent.hasPriorDiagnostic(); }
76 
77  /// Reports overflow and return true if evaluation should continue.
78  bool reportOverflow(const Expr *E, const llvm::APSInt &Value);
79 
80  /// Deallocates a pointer.
81  void deallocate(Block *B);
82 
83  /// Delegates source mapping to the mapper.
84  SourceInfo getSource(Function *F, CodePtr PC) const override {
85  return M ? M->getSource(F, PC) : F->getSource(PC);
86  }
87 
88 private:
89  /// AST Walker state.
90  State &Parent;
91  /// Dead block chain.
92  DeadBlock *DeadBlocks = nullptr;
93  /// Reference to the offset-source mapping.
94  SourceMapper *M;
95 
96 public:
97  /// Reference to the module containing all bytecode.
99  /// Temporary stack.
101  /// Interpreter Context.
103  /// The current frame.
104  InterpFrame *Current = nullptr;
105  /// Call stack depth.
106  unsigned CallStackDepth;
107 };
108 
109 } // namespace interp
110 } // namespace clang
111 
112 #endif
clang::interp::DeadBlock
Descriptor for a dead block.
Definition: InterpBlock.h:112
clang::interp::SourceMapper::getSource
virtual SourceInfo getSource(Function *F, CodePtr PC) const =0
Returns source information for a given PC in a function.
clang::interp::InterpState::hasPriorDiagnostic
bool hasPriorDiagnostic() override
Definition: InterpState.h:75
clang::interp::InterpState::checkingForUndefinedBehavior
bool checkingForUndefinedBehavior() const override
Definition: InterpState.h:56
clang::interp::InterpState
Interpreter context.
Definition: InterpState.h:34
clang::interp::InterpState::Current
InterpFrame * Current
The current frame.
Definition: InterpState.h:104
clang::interp::InterpState::keepEvaluatingAfterFailure
bool keepEvaluatingAfterFailure() const override
Definition: InterpState.h:59
clang::interp::Context
Holds all information required to evaluate constexpr code in a module.
Definition: Context.h:37
clang::interp::InterpState::InterpState
InterpState(State &Parent, Program &P, InterpStack &Stk, Context &Ctx, SourceMapper *M=nullptr)
Definition: InterpState.cpp:24
clang::Expr::EvalStatus
EvalStatus is a struct with detailed info about an evaluation in progress.
Definition: Expr.h:576
clang::interp::InterpState::getSource
SourceInfo getSource(Function *F, CodePtr PC) const override
Delegates source mapping to the mapper.
Definition: InterpState.h:84
clang::interp::InterpState::getCallStackDepth
unsigned getCallStackDepth() override
Definition: InterpState.h:44
clang::interp::Function::getSource
SourceInfo getSource(CodePtr PC) const
Returns the source information at a given PC.
Definition: Function.cpp:34
APSInt
llvm::APSInt APSInt
Definition: ByteCodeEmitter.cpp:19
clang::interp::InterpFrame
Frame storing local variables.
Definition: InterpFrame.h:29
clang::interp::InterpState::CallStackDepth
unsigned CallStackDepth
Call stack depth.
Definition: InterpState.h:106
clang::interp::InterpState::setFoldFailureDiagnostic
void setFoldFailureDiagnostic(bool Flag) override
Definition: InterpState.h:72
clang::interp::InterpState::checkingPotentialConstantExpression
bool checkingPotentialConstantExpression() const override
Definition: InterpState.h:62
clang::interp::State
Interface for the VM to interact with the AST walker's context.
Definition: State.h:55
clang::ASTContext
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:212
clang::interp::InterpState::getCtx
ASTContext & getCtx() const override
Definition: InterpState.h:53
APValue.h
clang::interp::Block
A memory block, either on the stack or in the heap.
Definition: InterpBlock.h:37
clang::interp::InterpState::deallocate
void deallocate(Block *B)
Deallocates a pointer.
Definition: InterpState.cpp:57
clang::interp::InterpState::getSplitFrame
Frame * getSplitFrame()
Definition: InterpState.h:42
Expr.h
OptionalDiagnostic.h
clang::interp::SourceInfo
Describes the statement/declaration an opcode was generated from.
Definition: Source.h:66
InterpStack.h
clang::interp::InterpState::reportOverflow
bool reportOverflow(const Expr *E, const llvm::APSInt &Value)
Reports overflow and return true if evaluation should continue.
Definition: InterpState.cpp:51
ASTDiagnostic.h
clang::interp::InterpState::getEvalStatus
Expr::EvalStatus & getEvalStatus() const override
Definition: InterpState.h:50
clang::interp::InterpState::getCurrentFrame
Frame * getCurrentFrame() override
Definition: InterpState.cpp:43
clang::interp::InterpState::getBottomFrame
const Frame * getBottomFrame() const override
Definition: InterpState.h:45
Value
Value
Definition: UninitializedValues.cpp:102
clang::interp::InterpState::~InterpState
~InterpState()
Definition: InterpState.cpp:29
clang::interp::InterpStack
Stack frame storing temporaries and parameters.
Definition: InterpStack.h:22
clang::interp::InterpState::Stk
InterpStack & Stk
Temporary stack.
Definition: InterpState.h:100
clang::interp::CodePtr
Pointer into the code segment.
Definition: Source.h:25
clang::interp::InterpState::setActiveDiagnostic
void setActiveDiagnostic(bool Flag) override
Definition: InterpState.h:69
clang::interp::Frame
Base class for stack frames, shared between VM and walker.
Definition: Frame.h:25
clang
Definition: CalledOnceCheck.h:17
Function.h
clang::interp::Function
Bytecode function.
Definition: Function.h:59
clang::interp::InterpState::Ctx
Context & Ctx
Interpreter Context.
Definition: InterpState.h:102
clang::interp::InterpState::noteUndefinedBehavior
bool noteUndefinedBehavior() override
Definition: InterpState.h:65
State.h
Parent
NodeId Parent
Definition: ASTDiff.cpp:192
clang::index::SymbolKind::Function
@ Function
clang::interp::InterpState::hasActiveDiagnostic
bool hasActiveDiagnostic() override
Definition: InterpState.h:68
clang::interp::Program
The program contains and links the bytecode for all functions.
Definition: Program.h:43
clang::Expr
This represents one expression.
Definition: Expr.h:109
clang::interp::InterpState::P
Program & P
Reference to the module containing all bytecode.
Definition: InterpState.h:98
clang::interp::SourceMapper
Interface for classes which map locations to sources.
Definition: Source.h:87
Context.h