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
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:
38 SourceMapper *M = nullptr);
39
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 }
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.
112 /// Temporary stack.
114 /// Interpreter Context.
116 /// The current frame.
118};
119
120} // namespace interp
121} // namespace clang
122
123#endif
NodeId Parent
Definition: ASTDiff.cpp:191
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:182
This represents one expression.
Definition: Expr.h:110
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:165
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
const Frame * getBottomFrame() const override
Definition: InterpState.h:51
unsigned getCallStackDepth() override
Definition: InterpState.h:48
Expr::EvalStatus & getEvalStatus() const override
Definition: InterpState.h:56
Context & getContext() const
Definition: InterpState.h:99
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:115
SourceInfo getSource(const Function *F, CodePtr PC) const override
Delegates source mapping to the mapper.
Definition: InterpState.h:91
Frame * getCurrentFrame() override
Definition: InterpState.cpp:36
InterpState(const InterpState &)=delete
InterpStack & Stk
Temporary stack.
Definition: InterpState.h:113
bool inConstantContext() const
Definition: InterpState.h:74
InterpFrame * Current
The current frame.
Definition: InterpState.h:117
bool hasActiveDiagnostic() override
Definition: InterpState.h:75
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
InterpState & operator=(const InterpState &)=delete
void deallocate(Block *B)
Deallocates a pointer.
Definition: InterpState.cpp:48
ASTContext & getCtx() const override
Definition: InterpState.h:59
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:111
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