clang  19.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 "InterpFrame.h"
19 #include "InterpStack.h"
20 #include "State.h"
21 #include "clang/AST/APValue.h"
23 #include "clang/AST/Expr.h"
25 
26 namespace clang {
27 namespace interp {
28 class Context;
29 class Function;
30 class InterpStack;
31 class InterpFrame;
32 class SourceMapper;
33 
34 /// Interpreter context.
35 class InterpState final : public State, public SourceMapper {
36 public:
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 
101  void setEvalLocation(SourceLocation SL) { this->EvalLocation = SL; }
102 
103 private:
104  /// AST Walker state.
105  State &Parent;
106  /// Dead block chain.
107  DeadBlock *DeadBlocks = nullptr;
108  /// Reference to the offset-source mapping.
109  SourceMapper *M;
110 
111 public:
112  /// Reference to the module containing all bytecode.
114  /// Temporary stack.
116  /// Interpreter Context.
118  /// The current frame.
119  InterpFrame *Current = nullptr;
120  /// Source location of the evaluating expression
122 };
123 
124 } // namespace interp
125 } // namespace clang
126 
127 #endif
NodeId Parent
Definition: ASTDiff.cpp:191
llvm::APSInt APSInt
Implements a partial diagnostic which may not be emitted.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:185
This represents one expression.
Definition: Expr.h:110
Encodes a location in the source.
A memory block, either on the stack or in the heap.
Definition: InterpBlock.h:49
Pointer into the code segment.
Definition: Source.h:30
Holds all information required to evaluate constexpr code in a module.
Definition: Context.h:40
Descriptor for a dead block.
Definition: InterpBlock.h:169
Base class for stack frames, shared between VM and walker.
Definition: Frame.h:25
Bytecode function.
Definition: Function.h:77
SourceInfo getSource(CodePtr PC) const
Returns the source information at a given PC.
Definition: Function.cpp:36
Frame storing local variables.
Definition: InterpFrame.h:28
unsigned getDepth() const
Definition: InterpFrame.h:124
Stack frame storing temporaries and parameters.
Definition: InterpStack.h:26
Interpreter context.
Definition: InterpState.h:35
unsigned getCallStackDepth() override
Definition: InterpState.h:48
bool keepEvaluatingAfterFailure() const override
Definition: InterpState.h:65
bool reportOverflow(const Expr *E, const llvm::APSInt &Value)
Reports overflow and return true if evaluation should continue.
Definition: InterpState.cpp:42
bool noteUndefinedBehavior() override
Definition: InterpState.h:71
Context & Ctx
Interpreter Context.
Definition: InterpState.h:117
Context & getContext() const
Definition: InterpState.h:99
SourceInfo getSource(const Function *F, CodePtr PC) const override
Delegates source mapping to the mapper.
Definition: InterpState.h:91
InterpState & operator=(const InterpState &)=delete
Frame * getCurrentFrame() override
Definition: InterpState.cpp:36
InterpState(const InterpState &)=delete
InterpStack & Stk
Temporary stack.
Definition: InterpState.h:115
SourceLocation EvalLocation
Source location of the evaluating expression.
Definition: InterpState.h:121
bool inConstantContext() const
Definition: InterpState.h:74
InterpFrame * Current
The current frame.
Definition: InterpState.h:119
InterpState(State &Parent, Program &P, InterpStack &Stk, Context &Ctx, SourceMapper *M=nullptr)
Definition: InterpState.cpp:18
const Frame * getBottomFrame() const override
Definition: InterpState.h:51
bool hasActiveDiagnostic() override
Definition: InterpState.h:75
ASTContext & getCtx() const override
Definition: InterpState.h:59
void setActiveDiagnostic(bool Flag) override
Definition: InterpState.h:76
bool checkingForUndefinedBehavior() const override
Definition: InterpState.h:62
void setFoldFailureDiagnostic(bool Flag) override
Definition: InterpState.h:79
void deallocate(Block *B)
Deallocates a pointer.
Definition: InterpState.cpp:48
Expr::EvalStatus & getEvalStatus() const override
Definition: InterpState.h:56
void setEvalLocation(SourceLocation SL)
Definition: InterpState.h:101
bool checkingPotentialConstantExpression() const override
Definition: InterpState.h:68
bool hasPriorDiagnostic() override
Definition: InterpState.h:82
Program & P
Reference to the module containing all bytecode.
Definition: InterpState.h:113
The program contains and links the bytecode for all functions.
Definition: Program.h:39
Describes the statement/declaration an opcode was generated from.
Definition: Source.h:72
Interface for classes which map locations to sources.
Definition: Source.h:94
virtual SourceInfo getSource(const Function *F, CodePtr PC) const =0
Returns source information for a given PC in a function.
Interface for the VM to interact with the AST walker's context.
Definition: State.h:55
The JSON file list parser is used to communicate input to InstallAPI.
EvalStatus is a struct with detailed info about an evaluation in progress.
Definition: Expr.h:606