clang 20.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 "DynamicAllocator.h"
18#include "Function.h"
19#include "InterpFrame.h"
20#include "InterpStack.h"
21#include "State.h"
22#include "clang/AST/APValue.h"
24#include "clang/AST/Expr.h"
26
27namespace clang {
28namespace interp {
29class Context;
30class Function;
31class InterpStack;
32class InterpFrame;
33class SourceMapper;
34
35/// Interpreter context.
36class InterpState final : public State, public SourceMapper {
37public:
39 SourceMapper *M = nullptr);
40
42
43 void cleanup();
44
45 InterpState(const InterpState &) = delete;
46 InterpState &operator=(const InterpState &) = delete;
47
48 // Stack frame accessors.
49 Frame *getSplitFrame() { return Parent.getCurrentFrame(); }
50 Frame *getCurrentFrame() override;
51 unsigned getCallStackDepth() override {
52 return Current ? (Current->getDepth() + 1) : 1;
53 }
54 const Frame *getBottomFrame() const override {
55 return Parent.getBottomFrame();
56 }
57
58 // Access objects from the walker context.
59 Expr::EvalStatus &getEvalStatus() const override {
60 return Parent.getEvalStatus();
61 }
62 ASTContext &getCtx() const override { return Parent.getCtx(); }
63
64 // Forward status checks and updates to the walker.
65 bool checkingForUndefinedBehavior() const override {
66 return Parent.checkingForUndefinedBehavior();
67 }
68 bool keepEvaluatingAfterFailure() const override {
69 return Parent.keepEvaluatingAfterFailure();
70 }
72 return Parent.checkingPotentialConstantExpression();
73 }
74 bool noteUndefinedBehavior() override {
75 return Parent.noteUndefinedBehavior();
76 }
77 bool inConstantContext() const { return Parent.InConstantContext; }
78 bool hasActiveDiagnostic() override { return Parent.hasActiveDiagnostic(); }
79 void setActiveDiagnostic(bool Flag) override {
80 Parent.setActiveDiagnostic(Flag);
81 }
82 void setFoldFailureDiagnostic(bool Flag) override {
83 Parent.setFoldFailureDiagnostic(Flag);
84 }
85 bool hasPriorDiagnostic() override { return Parent.hasPriorDiagnostic(); }
86
87 /// Reports overflow and return true if evaluation should continue.
88 bool reportOverflow(const Expr *E, const llvm::APSInt &Value);
89
90 /// Deallocates a pointer.
91 void deallocate(Block *B);
92
93 /// Delegates source mapping to the mapper.
94 SourceInfo getSource(const Function *F, CodePtr PC) const override {
95 if (M)
96 return M->getSource(F, PC);
97
98 assert(F && "Function cannot be null");
99 return F->getSource(PC);
100 }
101
102 Context &getContext() const { return Ctx; }
103
105
106 DynamicAllocator &getAllocator() { return Alloc; }
107
108 /// Diagnose any dynamic allocations that haven't been freed yet.
109 /// Will return \c false if there were any allocations to diagnose,
110 /// \c true otherwise.
112
113private:
114 friend class EvaluationResult;
115 /// AST Walker state.
116 State &Parent;
117 /// Dead block chain.
118 DeadBlock *DeadBlocks = nullptr;
119 /// Reference to the offset-source mapping.
120 SourceMapper *M;
121 /// Allocator used for dynamic allocations performed via the program.
122 DynamicAllocator Alloc;
123
124public:
125 /// Reference to the module containing all bytecode.
127 /// Temporary stack.
129 /// Interpreter Context.
131 /// The current frame.
133 /// Source location of the evaluating expression
135 /// Declaration we're initializing/evaluting, if any.
136 const VarDecl *EvaluatingDecl = nullptr;
137
139 std::pair<const Expr *, const LifetimeExtendedTemporaryDecl *>>
141};
142
143} // namespace interp
144} // namespace clang
145
146#endif
NodeId Parent
Definition: ASTDiff.cpp:191
Expr * E
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:187
This represents one expression.
Definition: Expr.h:110
Encodes a location in the source.
Represents a variable declaration or definition.
Definition: Decl.h:879
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:181
Manages dynamic memory allocations done during bytecode interpretation.
Defines the result of an evaluation.
Base class for stack frames, shared between VM and walker.
Definition: Frame.h:25
Bytecode function.
Definition: Function.h:81
SourceInfo getSource(CodePtr PC) const
Returns the source information at a given PC.
Definition: Function.cpp:38
Frame storing local variables.
Definition: InterpFrame.h:26
unsigned getDepth() const
Definition: InterpFrame.h:121
Stack frame storing temporaries and parameters.
Definition: InterpStack.h:27
Interpreter context.
Definition: InterpState.h:36
const Frame * getBottomFrame() const override
Definition: InterpState.h:54
unsigned getCallStackDepth() override
Definition: InterpState.h:51
Expr::EvalStatus & getEvalStatus() const override
Definition: InterpState.h:59
Context & getContext() const
Definition: InterpState.h:102
bool keepEvaluatingAfterFailure() const override
Definition: InterpState.h:68
bool reportOverflow(const Expr *E, const llvm::APSInt &Value)
Reports overflow and return true if evaluation should continue.
Definition: InterpState.cpp:54
bool noteUndefinedBehavior() override
Definition: InterpState.h:74
DynamicAllocator & getAllocator()
Definition: InterpState.h:106
Context & Ctx
Interpreter Context.
Definition: InterpState.h:130
SourceInfo getSource(const Function *F, CodePtr PC) const override
Delegates source mapping to the mapper.
Definition: InterpState.h:94
Frame * getCurrentFrame() override
Definition: InterpState.cpp:48
InterpState(const InterpState &)=delete
llvm::SmallVector< std::pair< const Expr *, const LifetimeExtendedTemporaryDecl * > > SeenGlobalTemporaries
Definition: InterpState.h:140
InterpStack & Stk
Temporary stack.
Definition: InterpState.h:128
bool maybeDiagnoseDanglingAllocations()
Diagnose any dynamic allocations that haven't been freed yet.
Definition: InterpState.cpp:89
SourceLocation EvalLocation
Source location of the evaluating expression.
Definition: InterpState.h:134
bool inConstantContext() const
Definition: InterpState.h:77
const VarDecl * EvaluatingDecl
Declaration we're initializing/evaluting, if any.
Definition: InterpState.h:136
InterpFrame * Current
The current frame.
Definition: InterpState.h:132
bool hasActiveDiagnostic() override
Definition: InterpState.h:78
void setActiveDiagnostic(bool Flag) override
Definition: InterpState.h:79
bool checkingForUndefinedBehavior() const override
Definition: InterpState.h:65
void setFoldFailureDiagnostic(bool Flag) override
Definition: InterpState.h:82
InterpState & operator=(const InterpState &)=delete
void deallocate(Block *B)
Deallocates a pointer.
Definition: InterpState.cpp:60
ASTContext & getCtx() const override
Definition: InterpState.h:62
void setEvalLocation(SourceLocation SL)
Definition: InterpState.h:104
bool checkingPotentialConstantExpression() const override
Definition: InterpState.h:71
bool hasPriorDiagnostic() override
Definition: InterpState.h:85
Program & P
Reference to the module containing all bytecode.
Definition: InterpState.h:126
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:77
Interface for classes which map locations to sources.
Definition: Source.h:99
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:56
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