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 void cleanup();
43
44 InterpState(const InterpState &) = delete;
45 InterpState &operator=(const InterpState &) = delete;
46
47 // Stack frame accessors.
48 Frame *getSplitFrame() { return Parent.getCurrentFrame(); }
49 Frame *getCurrentFrame() override;
50 unsigned getCallStackDepth() override {
51 return Current ? (Current->getDepth() + 1) : 1;
52 }
53 const Frame *getBottomFrame() const override {
54 return Parent.getBottomFrame();
55 }
56
57 // Access objects from the walker context.
58 Expr::EvalStatus &getEvalStatus() const override {
59 return Parent.getEvalStatus();
60 }
61 ASTContext &getCtx() const override { return Parent.getCtx(); }
62
63 // Forward status checks and updates to the walker.
64 bool checkingForUndefinedBehavior() const override {
65 return Parent.checkingForUndefinedBehavior();
66 }
67 bool keepEvaluatingAfterFailure() const override {
68 return Parent.keepEvaluatingAfterFailure();
69 }
71 return Parent.checkingPotentialConstantExpression();
72 }
73 bool noteUndefinedBehavior() override {
74 return Parent.noteUndefinedBehavior();
75 }
76 bool inConstantContext() const { return Parent.InConstantContext; }
77 bool hasActiveDiagnostic() override { return Parent.hasActiveDiagnostic(); }
78 void setActiveDiagnostic(bool Flag) override {
79 Parent.setActiveDiagnostic(Flag);
80 }
81 void setFoldFailureDiagnostic(bool Flag) override {
82 Parent.setFoldFailureDiagnostic(Flag);
83 }
84 bool hasPriorDiagnostic() override { return Parent.hasPriorDiagnostic(); }
85
86 /// Reports overflow and return true if evaluation should continue.
87 bool reportOverflow(const Expr *E, const llvm::APSInt &Value);
88
89 /// Deallocates a pointer.
90 void deallocate(Block *B);
91
92 /// Delegates source mapping to the mapper.
93 SourceInfo getSource(const Function *F, CodePtr PC) const override {
94 if (M)
95 return M->getSource(F, PC);
96
97 assert(F && "Function cannot be null");
98 return F->getSource(PC);
99 }
100
101 Context &getContext() const { return Ctx; }
102
104
105private:
106 /// AST Walker state.
107 State &Parent;
108 /// Dead block chain.
109 DeadBlock *DeadBlocks = nullptr;
110 /// Reference to the offset-source mapping.
111 SourceMapper *M;
112
113public:
114 /// Reference to the module containing all bytecode.
116 /// Temporary stack.
118 /// Interpreter Context.
120 /// The current frame.
122 /// Source location of the evaluating expression
124};
125
126} // namespace interp
127} // namespace clang
128
129#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: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:171
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:27
Interpreter context.
Definition: InterpState.h:35
const Frame * getBottomFrame() const override
Definition: InterpState.h:53
unsigned getCallStackDepth() override
Definition: InterpState.h:50
Expr::EvalStatus & getEvalStatus() const override
Definition: InterpState.h:58
Context & getContext() const
Definition: InterpState.h:101
bool keepEvaluatingAfterFailure() const override
Definition: InterpState.h:67
bool reportOverflow(const Expr *E, const llvm::APSInt &Value)
Reports overflow and return true if evaluation should continue.
Definition: InterpState.cpp:44
bool noteUndefinedBehavior() override
Definition: InterpState.h:73
Context & Ctx
Interpreter Context.
Definition: InterpState.h:119
SourceInfo getSource(const Function *F, CodePtr PC) const override
Delegates source mapping to the mapper.
Definition: InterpState.h:93
Frame * getCurrentFrame() override
Definition: InterpState.cpp:38
InterpState(const InterpState &)=delete
InterpStack & Stk
Temporary stack.
Definition: InterpState.h:117
SourceLocation EvalLocation
Source location of the evaluating expression.
Definition: InterpState.h:123
bool inConstantContext() const
Definition: InterpState.h:76
InterpFrame * Current
The current frame.
Definition: InterpState.h:121
bool hasActiveDiagnostic() override
Definition: InterpState.h:77
void setActiveDiagnostic(bool Flag) override
Definition: InterpState.h:78
bool checkingForUndefinedBehavior() const override
Definition: InterpState.h:64
void setFoldFailureDiagnostic(bool Flag) override
Definition: InterpState.h:81
InterpState & operator=(const InterpState &)=delete
void deallocate(Block *B)
Deallocates a pointer.
Definition: InterpState.cpp:50
ASTContext & getCtx() const override
Definition: InterpState.h:61
void setEvalLocation(SourceLocation SL)
Definition: InterpState.h:103
bool checkingPotentialConstantExpression() const override
Definition: InterpState.h:70
bool hasPriorDiagnostic() override
Definition: InterpState.h:84
Program & P
Reference to the module containing all bytecode.
Definition: InterpState.h:115
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