clang 23.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 "Floating.h"
19#include "Function.h"
20#include "InterpFrame.h"
21#include "InterpStack.h"
22#include "State.h"
23
24namespace clang {
25namespace interp {
26class Context;
27class SourceMapper;
28
30 const Expr *Call = nullptr;
32 explicit operator bool() { return Call; }
33};
34
35// FIXME: Create one for the "checking potential constant expression"
36// evaluation.
37enum class EvaluationKind : uint8_t {
39 Dtor, /// We're checking for constant destruction of a global variable.
40};
41
42/// Interpreter context.
43class InterpState final : public State, public SourceMapper {
44public:
45 InterpState(const State &Parent, Program &P, InterpStack &Stk, Context &Ctx,
46 SourceMapper *M = nullptr);
47 InterpState(const State &Parent, Program &P, InterpStack &Stk, Context &Ctx,
48 const Function *Func);
49
51
52 void cleanup();
53
54 InterpState(const InterpState &) = delete;
55 InterpState &operator=(const InterpState &) = delete;
56
57 bool diagnosing() const { return getEvalStatus().Diag != nullptr; }
58
59 // Stack frame accessors.
60 const Frame *getCurrentFrame() override;
61 unsigned getCallStackDepth() override {
62 return Current ? (Current->getDepth() + 1) : 1;
63 }
64 bool stepsLeft() const override { return true; }
65 bool inConstantContext() const;
66
67 /// Deallocates a pointer.
68 void deallocate(Block *B);
69
70 /// Delegates source mapping to the mapper.
71 SourceInfo getSource(const Function *F, CodePtr PC) const override {
72 if (M)
73 return M->getSource(F, PC);
74
75 assert(F && "Function cannot be null");
76 return F->getSource(PC);
77 }
78
79 Context &getContext() const { return Ctx; }
80
82
84 if (!Alloc) {
85 Alloc = std::make_unique<DynamicAllocator>();
86 }
87
88 return *Alloc;
89 }
90
91 /// Diagnose any dynamic allocations that haven't been freed yet.
92 /// Will return \c false if there were any allocations to diagnose,
93 /// \c true otherwise.
95
96 StdAllocatorCaller getStdAllocatorCaller(StringRef Name) const;
97
98 void *allocate(size_t Size, unsigned Align = 8) const {
99 if (!Allocator)
100 Allocator.emplace();
101 return Allocator->Allocate(Size, Align);
102 }
103 template <typename T> T *allocate(size_t Num = 1) const {
104 return static_cast<T *>(allocate(Num * sizeof(T), alignof(T)));
105 }
106
107 template <typename T> T allocAP(unsigned BitWidth) {
108 unsigned NumWords = APInt::getNumWords(BitWidth);
109 if (NumWords == 1)
110 return T(BitWidth);
111 uint64_t *Mem = (uint64_t *)this->allocate(NumWords * sizeof(uint64_t));
112 // std::memset(Mem, 0, NumWords * sizeof(uint64_t)); // Debug
113 return T(Mem, BitWidth);
114 }
115
116 Floating allocFloat(const llvm::fltSemantics &Sem) {
117 if (Floating::singleWord(Sem))
118 return Floating(llvm::APFloatBase::SemanticsToEnum(Sem));
119
120 unsigned NumWords =
121 APInt::getNumWords(llvm::APFloatBase::getSizeInBits(Sem));
122 uint64_t *Mem = (uint64_t *)this->allocate(NumWords * sizeof(uint64_t));
123 // std::memset(Mem, 0, NumWords * sizeof(uint64_t)); // Debug
124 return Floating(Mem, llvm::APFloatBase::SemanticsToEnum(Sem));
125 }
126 const CXXRecordDecl **allocMemberPointerPath(unsigned Length) {
127 return reinterpret_cast<const CXXRecordDecl **>(
128 this->allocate(Length * sizeof(CXXRecordDecl *)));
129 }
130
131 /// Note that a step has been executed. If there are no more steps remaining,
132 /// diagnoses and returns \c false.
133 bool noteStep(CodePtr OpPC);
134
135 bool initializingBlock(const Block *B) const {
137 if (V.block() == B)
138 return true;
139 return false;
140 }
141
142 bool lifetimeStartedInEvaluation(const Block *B) const {
144 return B->getEvalID() == EvalID;
145
147 assert(EvaluatingDecl);
149 return EvaluatingDecl->getType().isConstQualified();
150 }
151 return false;
152 }
153
154 /// Return if we're checking if a global variable has a constant destructor.
157 }
158 /// Return if we're checking if a global variable has a constant destructor
159 /// and the given pointer is pointing to the variable we're checking that for.
160 bool checkingConstantDestruction(const Pointer &Ptr) const {
162 }
163 bool checkingConstantDestruction(const VarDecl *VD) const {
165 }
166
167private:
168 friend class EvaluationResult;
170 /// Dead block chain.
171 DeadBlock *DeadBlocks = nullptr;
172 /// Reference to the offset-source mapping.
173 SourceMapper *M;
174 /// Allocator used for dynamic allocations performed via the program.
175 std::unique_ptr<DynamicAllocator> Alloc;
176 /// Allocator for everything else, e.g. floating-point values.
177 mutable std::optional<llvm::BumpPtrAllocator> Allocator;
178
179public:
181 /// Reference to the module containing all bytecode.
183 /// Temporary stack.
185 /// Interpreter Context.
187 /// Bottom function frame.
189 /// The current frame.
191 /// Source location of the evaluating expression
193 /// Declaration we're initializing/evaluting, if any.
194 const VarDecl *EvaluatingDecl = nullptr;
195 /// Steps left during evaluation.
196 unsigned StepsLeft = 1;
197 /// Whether infinite evaluation steps have been requested. If this is false,
198 /// we use the StepsLeft value above.
199 const bool InfiniteSteps = false;
200 /// ID identifying this evaluation.
201 const unsigned EvalID;
202
204
205 /// Things needed to do speculative execution.
207 bool PrevDiagsEmitted = false;
208#ifndef NDEBUG
209 unsigned SpeculationDepth = 0;
210#endif
211 unsigned DiagIgnoreDepth = 0;
212 std::optional<bool> ConstantContextOverride;
213
215 std::pair<const Expr *, const LifetimeExtendedTemporaryDecl *>>
217
218 /// List of blocks we're currently running either constructors or destructors
219 /// for.
221};
222
224public:
226 : Ctx(Ctx), OldCC(Ctx.ConstantContextOverride) {
227 // We only override this if the new value is true.
228 Enabled = Value;
229 if (Enabled)
230 Ctx.ConstantContextOverride = Value;
231 }
233 if (Enabled)
234 Ctx.ConstantContextOverride = OldCC;
235 }
236
237private:
238 bool Enabled;
239 InterpState &Ctx;
240 std::optional<bool> OldCC;
241};
242
243} // namespace interp
244} // namespace clang
245
246#endif
#define V(N, I)
Represents a C++ struct/union/class.
Definition DeclCXX.h:258
This represents one expression.
Definition Expr.h:112
A (possibly-)qualified type.
Definition TypeBase.h:937
Encodes a location in the source.
Represents a variable declaration or definition.
Definition Decl.h:932
A memory block, either on the stack or in the heap.
Definition InterpBlock.h:44
const Descriptor * getDescriptor() const
Returns the block's descriptor.
Definition InterpBlock.h:73
unsigned getEvalID() const
The Evaluation ID this block was created in.
Definition InterpBlock.h:94
Pointer into the code segment.
Definition Source.h:30
Holds all information required to evaluate constexpr code in a module.
Definition Context.h:47
Descriptor for a dead block.
Manages dynamic memory allocations done during bytecode interpretation.
If a Floating is constructed from Memory, it DOES NOT OWN THAT MEMORY.
Definition Floating.h:35
bool singleWord() const
Definition Floating.h:107
Base class for stack frames, shared between VM and walker.
Definition Frame.h:25
Bytecode function.
Definition Function.h:99
SourceInfo getSource(CodePtr PC) const
Returns the source information at a given PC.
Definition Function.cpp:61
Frame storing local variables.
Definition InterpFrame.h:27
Stack frame storing temporaries and parameters.
Definition InterpStack.h:25
InterpStateCCOverride(InterpState &Ctx, bool Value)
Interpreter context.
Definition InterpState.h:43
SmallVectorImpl< PartialDiagnosticAt > * PrevDiags
Things needed to do speculative execution.
bool lifetimeStartedInEvaluation(const Block *B) const
unsigned getCallStackDepth() override
Definition InterpState.h:61
InterpFrame BottomFrame
Bottom function frame.
Context & getContext() const
Definition InterpState.h:79
bool initializingBlock(const Block *B) const
DynamicAllocator & getAllocator()
Definition InterpState.h:83
Context & Ctx
Interpreter Context.
void * allocate(size_t Size, unsigned Align=8) const
Definition InterpState.h:98
bool noteStep(CodePtr OpPC)
Note that a step has been executed.
Floating allocFloat(const llvm::fltSemantics &Sem)
const unsigned EvalID
ID identifying this evaluation.
SourceInfo getSource(const Function *F, CodePtr PC) const override
Delegates source mapping to the mapper.
Definition InterpState.h:71
InterpState(const State &Parent, Program &P, InterpStack &Stk, Context &Ctx, SourceMapper *M=nullptr)
InterpState(const InterpState &)=delete
bool stepsLeft() const override
Definition InterpState.h:64
llvm::SmallVector< std::pair< const Expr *, const LifetimeExtendedTemporaryDecl * > > SeenGlobalTemporaries
InterpStack & Stk
Temporary stack.
bool maybeDiagnoseDanglingAllocations()
Diagnose any dynamic allocations that haven't been freed yet.
SourceLocation EvalLocation
Source location of the evaluating expression.
bool checkingConstantDestruction() const
Return if we're checking if a global variable has a constant destructor.
unsigned StepsLeft
Steps left during evaluation.
const VarDecl * EvaluatingDecl
Declaration we're initializing/evaluting, if any.
bool checkingConstantDestruction(const Pointer &Ptr) const
Return if we're checking if a global variable has a constant destructor and the given pointer is poin...
InterpFrame * Current
The current frame.
const CXXRecordDecl ** allocMemberPointerPath(unsigned Length)
const Frame * getCurrentFrame() override
std::optional< bool > ConstantContextOverride
const bool InfiniteSteps
Whether infinite evaluation steps have been requested.
InterpState & operator=(const InterpState &)=delete
friend class InterpStateCCOverride
T * allocate(size_t Num=1) const
void deallocate(Block *B)
Deallocates a pointer.
llvm::SmallVector< PtrView > InitializingPtrs
List of blocks we're currently running either constructors or destructors for.
T allocAP(unsigned BitWidth)
void setEvalLocation(SourceLocation SL)
Definition InterpState.h:81
StdAllocatorCaller getStdAllocatorCaller(StringRef Name) const
bool checkingConstantDestruction(const VarDecl *VD) const
Program & P
Reference to the module containing all bytecode.
A pointer to a memory block, live or dead.
Definition Pointer.h:394
const Descriptor * getDeclDesc() const
Accessor for information about the declaration site.
Definition Pointer.h:525
The program contains and links the bytecode for all functions.
Definition Program.h:36
Describes the statement/declaration an opcode was generated from.
Definition Source.h:76
Interface for classes which map locations to sources.
Definition Source.h:103
Expr::EvalStatus & getEvalStatus() const
Definition State.h:91
State(ASTContext &ASTCtx, Expr::EvalStatus &EvalStatus)
Definition State.h:83
The JSON file list parser is used to communicate input to InstallAPI.
__packed_splat4 __packed_splat2 __packed_splat8 __packed_splat4 __packed_splat2 uint8_t
SmallVectorImpl< PartialDiagnosticAt > * Diag
Diag - If this is non-null, it will be filled in with a stack of notes indicating why evaluation fail...
Definition Expr.h:640
const VarDecl * asVarDecl() const
Definition Descriptor.h:220