Thanks to visit codestin.com
Credit goes to clang.llvm.org

clang 22.0.0git
InterpBlock.h
Go to the documentation of this file.
1//===-- InterpBlock.h - Allocated blocks for the interpreter -*- 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// Defines the classes describing allocated blocks.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_CLANG_AST_INTERP_BLOCK_H
14#define LLVM_CLANG_AST_INTERP_BLOCK_H
15
16#include "Descriptor.h"
17#include "llvm/Support/raw_ostream.h"
18
19namespace clang {
20namespace interp {
21class Block;
22class DeadBlock;
23class InterpState;
24class Pointer;
25enum PrimType : uint8_t;
26
27/// A memory block, either on the stack or in the heap.
28///
29/// The storage described by the block is immediately followed by
30/// optional metadata, which is followed by the actual data.
31///
32/// Block* rawData() data()
33/// │ │ │
34/// │ │ │
35/// ▼ ▼ ▼
36/// ┌───────────────┬─────────────────────────┬─────────────────┐
37/// │ Block │ Metadata │ Data │
38/// │ sizeof(Block) │ Desc->getMetadataSize() │ Desc->getSize() │
39/// └───────────────┴─────────────────────────┴─────────────────┘
40///
41/// Desc->getAllocSize() describes the size after the Block, i.e.
42/// the data size and the metadata size.
43///
44class Block final {
45private:
46 static constexpr uint8_t ExternFlag = 1 << 0;
47 static constexpr uint8_t DeadFlag = 1 << 1;
48 static constexpr uint8_t WeakFlag = 1 << 2;
49 static constexpr uint8_t DummyFlag = 1 << 3;
50
51public:
52 /// Creates a new block.
53 Block(unsigned EvalID, UnsignedOrNone DeclID, const Descriptor *Desc,
54 bool IsStatic = false, bool IsExtern = false, bool IsWeak = false,
55 bool IsDummy = false)
56 : Desc(Desc), DeclID(DeclID), EvalID(EvalID), IsStatic(IsStatic) {
57 assert(Desc);
58 AccessFlags |= (ExternFlag * IsExtern);
59 AccessFlags |= (WeakFlag * IsWeak);
60 AccessFlags |= (DummyFlag * IsDummy);
61 }
62
63 Block(unsigned EvalID, const Descriptor *Desc, bool IsStatic = false,
64 bool IsExtern = false, bool IsWeak = false, bool IsDummy = false)
65 : Desc(Desc), EvalID(EvalID), IsStatic(IsStatic) {
66 assert(Desc);
67 AccessFlags |= (ExternFlag * IsExtern);
68 AccessFlags |= (WeakFlag * IsWeak);
69 AccessFlags |= (DummyFlag * IsDummy);
70 }
71
72 /// Returns the block's descriptor.
73 const Descriptor *getDescriptor() const { return Desc; }
74 /// Checks if the block has any live pointers.
75 bool hasPointers() const { return Pointers; }
76 /// Checks if the block is extern.
77 bool isExtern() const { return AccessFlags & ExternFlag; }
78 /// Checks if the block has static storage duration.
79 bool isStatic() const { return IsStatic; }
80 /// Checks if the block is temporary.
81 bool isTemporary() const { return Desc->IsTemporary; }
82 bool isWeak() const { return AccessFlags & WeakFlag; }
83 bool isDynamic() const { return (DynAllocId != std::nullopt); }
84 bool isDummy() const { return AccessFlags & DummyFlag; }
85 bool isDead() const { return AccessFlags & DeadFlag; }
86 /// Returns the size of the block.
87 unsigned getSize() const { return Desc->getAllocSize(); }
88 /// Returns the declaration ID.
89 UnsignedOrNone getDeclID() const { return DeclID; }
90 /// Returns whether the data of this block has been initialized via
91 /// invoking the Ctor func.
92 bool isInitialized() const { return IsInitialized; }
93 /// The Evaluation ID this block was created in.
94 unsigned getEvalID() const { return EvalID; }
95
96 /// Returns a pointer to the stored data.
97 /// You are allowed to read Desc->getSize() bytes from this address.
98 std::byte *data() {
99 // rawData might contain metadata as well.
100 size_t DataOffset = Desc->getMetadataSize();
101 return rawData() + DataOffset;
102 }
103 const std::byte *data() const {
104 // rawData might contain metadata as well.
105 size_t DataOffset = Desc->getMetadataSize();
106 return rawData() + DataOffset;
107 }
108
109 /// Returns a pointer to the raw data, including metadata.
110 /// You are allowed to read Desc->getAllocSize() bytes from this address.
111 std::byte *rawData() {
112 return reinterpret_cast<std::byte *>(this) + sizeof(Block);
113 }
114 const std::byte *rawData() const {
115 return reinterpret_cast<const std::byte *>(this) + sizeof(Block);
116 }
117
118 template <typename T> const T &deref() const {
119 return *reinterpret_cast<const T *>(data());
120 }
121 template <typename T> T &deref() { return *reinterpret_cast<T *>(data()); }
122
123 /// Invokes the constructor.
124 void invokeCtor() {
125 assert(!IsInitialized);
126 std::memset(rawData(), 0, Desc->getAllocSize());
127 if (Desc->CtorFn) {
128 Desc->CtorFn(this, data(), Desc->IsConst, Desc->IsMutable,
129 Desc->IsVolatile,
130 /*isActive=*/true, /*InUnion=*/false, Desc);
131 }
132 IsInitialized = true;
133 }
134
135 /// Invokes the Destructor.
136 void invokeDtor() {
137 assert(IsInitialized);
138 if (Desc->DtorFn)
139 Desc->DtorFn(this, data(), Desc);
140 IsInitialized = false;
141 }
142
143 void dump() const { dump(llvm::errs()); }
144 void dump(llvm::raw_ostream &OS) const;
145
146 bool isAccessible() const { return AccessFlags == 0; }
147
148private:
149 friend class Pointer;
150 friend class DeadBlock;
151 friend class InterpState;
152 friend class DynamicAllocator;
153 friend class Program;
154
155 Block(unsigned EvalID, const Descriptor *Desc, bool IsExtern, bool IsStatic,
156 bool IsWeak, bool IsDummy, bool IsDead)
157 : Desc(Desc), EvalID(EvalID), IsStatic(IsStatic) {
158 assert(Desc);
159 AccessFlags |= (ExternFlag * IsExtern);
160 AccessFlags |= (DeadFlag * IsDead);
161 AccessFlags |= (WeakFlag * IsWeak);
162 AccessFlags |= (DummyFlag * IsDummy);
163 }
164
165 /// To be called by DynamicAllocator.
166 void setDynAllocId(unsigned ID) { DynAllocId = ID; }
167
168 /// Deletes a dead block at the end of its lifetime.
169 void cleanup();
170
171 /// Pointer chain management.
172 void addPointer(Pointer *P);
173 void removePointer(Pointer *P);
174 void replacePointer(Pointer *Old, Pointer *New);
175#ifndef NDEBUG
176 bool hasPointer(const Pointer *P) const;
177#endif
178
179 /// Pointer to the stack slot descriptor.
180 const Descriptor *Desc;
181 /// Start of the chain of pointers.
182 Pointer *Pointers = nullptr;
183 /// Unique identifier of the declaration.
184 UnsignedOrNone DeclID = std::nullopt;
185 const unsigned EvalID = ~0u;
186 /// Flag indicating if the block has static storage duration.
187 bool IsStatic = false;
188 /// Flag indicating if the block contents have been initialized
189 /// via invokeCtor.
190 bool IsInitialized = false;
191 /// Allocation ID for this dynamic allocation, if it is one.
192 UnsignedOrNone DynAllocId = std::nullopt;
193 /// AccessFlags containing IsExtern, IsDead, IsWeak, and IsDummy bits.
194 uint8_t AccessFlags = 0;
195};
196
197/// Descriptor for a dead block.
198///
199/// Dead blocks are chained in a double-linked list to deallocate them
200/// whenever pointers become dead.
201class DeadBlock final {
202public:
203 /// Copies the block.
204 DeadBlock(DeadBlock *&Root, Block *Blk);
205
206 /// Returns a pointer to the stored data.
207 std::byte *data() { return B.data(); }
208 std::byte *rawData() { return B.rawData(); }
209
210private:
211 friend class Block;
212 friend class InterpState;
213
214 void free();
215
216 /// Root pointer of the list.
217 DeadBlock *&Root;
218 /// Previous block in the list.
219 DeadBlock *Prev;
220 /// Next block in the list.
221 DeadBlock *Next;
222
223 /// Actual block storing data and tracking pointers.
224 Block B;
225};
226
227} // namespace interp
228} // namespace clang
229
230#endif
tooling::Replacements cleanup(const FormatStyle &Style, StringRef Code, ArrayRef< tooling::Range > Ranges, StringRef FileName="<stdin>")
Clean up any erroneous/redundant code in the given Ranges in Code.
A memory block, either on the stack or in the heap.
Definition InterpBlock.h:44
unsigned getSize() const
Returns the size of the block.
Definition InterpBlock.h:87
friend class Program
void invokeDtor()
Invokes the Destructor.
const T & deref() const
bool isExtern() const
Checks if the block is extern.
Definition InterpBlock.h:77
friend class Pointer
std::byte * data()
Returns a pointer to the stored data.
Definition InterpBlock.h:98
const Descriptor * getDescriptor() const
Returns the block's descriptor.
Definition InterpBlock.h:73
Block(unsigned EvalID, const Descriptor *Desc, bool IsStatic=false, bool IsExtern=false, bool IsWeak=false, bool IsDummy=false)
Definition InterpBlock.h:63
const std::byte * rawData() const
bool isDead() const
Definition InterpBlock.h:85
void invokeCtor()
Invokes the constructor.
bool isStatic() const
Checks if the block has static storage duration.
Definition InterpBlock.h:79
Block(unsigned EvalID, UnsignedOrNone DeclID, const Descriptor *Desc, bool IsStatic=false, bool IsExtern=false, bool IsWeak=false, bool IsDummy=false)
Creates a new block.
Definition InterpBlock.h:53
friend class DynamicAllocator
friend class InterpState
bool isTemporary() const
Checks if the block is temporary.
Definition InterpBlock.h:81
std::byte * rawData()
Returns a pointer to the raw data, including metadata.
const std::byte * data() const
bool isInitialized() const
Returns whether the data of this block has been initialized via invoking the Ctor func.
Definition InterpBlock.h:92
bool isDynamic() const
Definition InterpBlock.h:83
UnsignedOrNone getDeclID() const
Returns the declaration ID.
Definition InterpBlock.h:89
friend class DeadBlock
bool isDummy() const
Definition InterpBlock.h:84
unsigned getEvalID() const
The Evaluation ID this block was created in.
Definition InterpBlock.h:94
bool isWeak() const
Definition InterpBlock.h:82
bool isAccessible() const
bool hasPointers() const
Checks if the block has any live pointers.
Definition InterpBlock.h:75
Descriptor for a dead block.
std::byte * data()
Returns a pointer to the stored data.
DeadBlock(DeadBlock *&Root, Block *Blk)
Copies the block.
Interpreter context.
Definition InterpState.h:43
A pointer to a memory block, live or dead.
Definition Pointer.h:91
PrimType
Enumeration of the primitive types of the VM.
Definition PrimType.h:34
The JSON file list parser is used to communicate input to InstallAPI.
const FunctionProtoType * T
Describes a memory block created by an allocation site.
Definition Descriptor.h:122