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 &getASTContext() const override { return Parent.getASTContext(); }
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 }
71 bool keepEvaluatingAfterSideEffect() const override {
72 return Parent.keepEvaluatingAfterSideEffect();
73 }
75 return Parent.checkingPotentialConstantExpression();
76 }
77 bool noteUndefinedBehavior() override {
78 return Parent.noteUndefinedBehavior();
79 }
80 bool inConstantContext() const;
81 bool hasActiveDiagnostic() override { return Parent.hasActiveDiagnostic(); }
82 void setActiveDiagnostic(bool Flag) override {
83 Parent.setActiveDiagnostic(Flag);
84 }
85 void setFoldFailureDiagnostic(bool Flag) override {
86 Parent.setFoldFailureDiagnostic(Flag);
87 }
88 bool hasPriorDiagnostic() override { return Parent.hasPriorDiagnostic(); }
89 bool noteSideEffect() override { return Parent.noteSideEffect(); }
90
91 /// Reports overflow and return true if evaluation should continue.
92 bool reportOverflow(const Expr *E, const llvm::APSInt &Value);
93
94 /// Deallocates a pointer.
95 void deallocate(Block *B);
96
97 /// Delegates source mapping to the mapper.
98 SourceInfo getSource(const Function *F, CodePtr PC) const override {
99 if (M)
100 return M->getSource(F, PC);
101
102 assert(F && "Function cannot be null");
103 return F->getSource(PC);
104 }
105
106 Context &getContext() const { return Ctx; }
107
109
110 DynamicAllocator &getAllocator() { return Alloc; }
111
112 /// Diagnose any dynamic allocations that haven't been freed yet.
113 /// Will return \c false if there were any allocations to diagnose,
114 /// \c true otherwise.
116
117private:
118 friend class EvaluationResult;
120 /// AST Walker state.
121 State &Parent;
122 /// Dead block chain.
123 DeadBlock *DeadBlocks = nullptr;
124 /// Reference to the offset-source mapping.
125 SourceMapper *M;
126 /// Allocator used for dynamic allocations performed via the program.
127 DynamicAllocator Alloc;
128 std::optional<bool> ConstantContextOverride;
129
130public:
131 /// Reference to the module containing all bytecode.
133 /// Temporary stack.
135 /// Interpreter Context.
137 /// The current frame.
139 /// Source location of the evaluating expression
141 /// Declaration we're initializing/evaluting, if any.
142 const VarDecl *EvaluatingDecl = nullptr;
143
145 std::pair<const Expr *, const LifetimeExtendedTemporaryDecl *>>
147};
148
150public:
152 : Ctx(Ctx), OldCC(Ctx.ConstantContextOverride) {
153 // We only override this if the new value is true.
154 Enabled = Value;
155 if (Enabled)
156 Ctx.ConstantContextOverride = Value;
157 }
159 if (Enabled)
160 Ctx.ConstantContextOverride = OldCC;
161 }
162
163private:
164 bool Enabled;
165 InterpState &Ctx;
166 std::optional<bool> OldCC;
167};
168
169} // namespace interp
170} // namespace clang
171
172#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:188
This represents one expression.
Definition: Expr.h:110
Encodes a location in the source.
Represents a variable declaration or definition.
Definition: Decl.h:882
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:184
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:36
Frame storing local variables.
Definition: InterpFrame.h:26
unsigned getDepth() const
Definition: InterpFrame.h:118
Stack frame storing temporaries and parameters.
Definition: InterpStack.h:28
InterpStateCCOverride(InterpState &Ctx, bool Value)
Definition: InterpState.h:151
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:106
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:61
bool noteUndefinedBehavior() override
Definition: InterpState.h:77
DynamicAllocator & getAllocator()
Definition: InterpState.h:110
Context & Ctx
Interpreter Context.
Definition: InterpState.h:136
ASTContext & getASTContext() const override
Definition: InterpState.h:62
SourceInfo getSource(const Function *F, CodePtr PC) const override
Delegates source mapping to the mapper.
Definition: InterpState.h:98
Frame * getCurrentFrame() override
Definition: InterpState.cpp:55
InterpState(const InterpState &)=delete
llvm::SmallVector< std::pair< const Expr *, const LifetimeExtendedTemporaryDecl * > > SeenGlobalTemporaries
Definition: InterpState.h:146
InterpStack & Stk
Temporary stack.
Definition: InterpState.h:134
bool maybeDiagnoseDanglingAllocations()
Diagnose any dynamic allocations that haven't been freed yet.
Definition: InterpState.cpp:96
SourceLocation EvalLocation
Source location of the evaluating expression.
Definition: InterpState.h:140
bool keepEvaluatingAfterSideEffect() const override
Definition: InterpState.h:71
bool noteSideEffect() override
Definition: InterpState.h:89
const VarDecl * EvaluatingDecl
Declaration we're initializing/evaluting, if any.
Definition: InterpState.h:142
InterpFrame * Current
The current frame.
Definition: InterpState.h:138
bool hasActiveDiagnostic() override
Definition: InterpState.h:81
void setActiveDiagnostic(bool Flag) override
Definition: InterpState.h:82
bool checkingForUndefinedBehavior() const override
Definition: InterpState.h:65
void setFoldFailureDiagnostic(bool Flag) override
Definition: InterpState.h:85
InterpState & operator=(const InterpState &)=delete
void deallocate(Block *B)
Deallocates a pointer.
Definition: InterpState.cpp:67
void setEvalLocation(SourceLocation SL)
Definition: InterpState.h:108
bool checkingPotentialConstantExpression() const override
Definition: InterpState.h:74
bool inConstantContext() const
Definition: InterpState.cpp:22
bool hasPriorDiagnostic() override
Definition: InterpState.h:88
Program & P
Reference to the module containing all bytecode.
Definition: InterpState.h:132
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:103
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:57
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