clang 17.0.0git
Pointer.h
Go to the documentation of this file.
1//===--- Pointer.h - Types 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// Defines the classes responsible for pointer tracking.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_CLANG_AST_INTERP_POINTER_H
14#define LLVM_CLANG_AST_INTERP_POINTER_H
15
16#include "Descriptor.h"
17#include "InterpBlock.h"
19#include "clang/AST/Decl.h"
20#include "clang/AST/DeclCXX.h"
21#include "clang/AST/Expr.h"
22#include "llvm/ADT/PointerUnion.h"
23#include "llvm/Support/raw_ostream.h"
24
25namespace clang {
26namespace interp {
27class Block;
28class DeadBlock;
29class Pointer;
30enum PrimType : unsigned;
31
32/// A pointer to a memory block, live or dead.
33///
34/// This object can be allocated into interpreter stack frames. If pointing to
35/// a live block, it is a link in the chain of pointers pointing to the block.
36///
37/// In the simplest form, a Pointer has a Block* (the pointee) and both Base
38/// and Offset are 0, which means it will point to raw data.
39///
40/// The Base field is used to access metadata about the data. For primitive
41/// arrays, the Base is followed by an InitMap. In a variety of cases, the
42/// Base is preceded by an InlineDescriptor, which is used to track the
43/// initialization state, among other things.
44///
45/// The Offset field is used to access the actual data. In other words, the
46/// data the pointer decribes can be found at
47/// Pointee->rawData() + Pointer.Offset.
48///
49///
50/// Pointee Offset
51/// │ │
52/// │ │
53/// ▼ ▼
54/// ┌───────┬────────────┬─────────┬────────────────────────────┐
55/// │ Block │ InlineDesc │ InitMap │ Actual Data │
56/// └───────┴────────────┴─────────┴────────────────────────────┘
57/// ▲
58/// │
59/// │
60/// Base
61class Pointer {
62private:
63 static constexpr unsigned PastEndMark = ~0u;
64 static constexpr unsigned RootPtrMark = ~0u;
65
66public:
68 Pointer(Block *B);
69 Pointer(Block *B, unsigned BaseAndOffset);
70 Pointer(const Pointer &P);
71 Pointer(Pointer &&P);
72 ~Pointer();
73
74 void operator=(const Pointer &P);
75 void operator=(Pointer &&P);
76
77 /// Converts the pointer to an APValue.
78 APValue toAPValue() const;
79
80 /// Offsets a pointer inside an array.
81 Pointer atIndex(unsigned Idx) const {
82 if (Base == RootPtrMark)
83 return Pointer(Pointee, RootPtrMark, getDeclDesc()->getSize());
84 unsigned Off = Idx * elemSize();
85 if (getFieldDesc()->ElemDesc)
86 Off += sizeof(InlineDescriptor);
87 else
88 Off += sizeof(InitMap *);
89 return Pointer(Pointee, Base, Base + Off);
90 }
91
92 /// Creates a pointer to a field.
93 Pointer atField(unsigned Off) const {
94 unsigned Field = Offset + Off;
95 return Pointer(Pointee, Field, Field);
96 }
97
98 /// Restricts the scope of an array element pointer.
99 Pointer narrow() const {
100 // Null pointers cannot be narrowed.
101 if (isZero() || isUnknownSizeArray())
102 return *this;
103
104 // Pointer to an array of base types - enter block.
105 if (Base == RootPtrMark)
106 return Pointer(Pointee, 0, Offset == 0 ? Offset : PastEndMark);
107
108 // Pointer is one past end - magic offset marks that.
109 if (isOnePastEnd())
110 return Pointer(Pointee, Base, PastEndMark);
111
112 // Primitive arrays are a bit special since they do not have inline
113 // descriptors. If Offset != Base, then the pointer already points to
114 // an element and there is nothing to do. Otherwise, the pointer is
115 // adjusted to the first element of the array.
116 if (inPrimitiveArray()) {
117 if (Offset != Base)
118 return *this;
119 return Pointer(Pointee, Base, Offset + sizeof(InitMap *));
120 }
121
122 // Pointer is to a field or array element - enter it.
123 if (Offset != Base)
124 return Pointer(Pointee, Offset, Offset);
125
126 // Enter the first element of an array.
127 if (!getFieldDesc()->isArray())
128 return *this;
129
130 const unsigned NewBase = Base + sizeof(InlineDescriptor);
131 return Pointer(Pointee, NewBase, NewBase);
132 }
133
134 /// Expands a pointer to the containing array, undoing narrowing.
135 Pointer expand() const {
136 if (isElementPastEnd()) {
137 // Revert to an outer one-past-end pointer.
138 unsigned Adjust;
139 if (inPrimitiveArray())
140 Adjust = sizeof(InitMap *);
141 else
142 Adjust = sizeof(InlineDescriptor);
143 return Pointer(Pointee, Base, Base + getSize() + Adjust);
144 }
145
146 // Do not step out of array elements.
147 if (Base != Offset)
148 return *this;
149
150 // If at base, point to an array of base types.
151 if (Base == 0)
152 return Pointer(Pointee, RootPtrMark, 0);
153
154 // Step into the containing array, if inside one.
155 unsigned Next = Base - getInlineDesc()->Offset;
156 Descriptor *Desc = Next == 0 ? getDeclDesc() : getDescriptor(Next)->Desc;
157 if (!Desc->IsArray)
158 return *this;
159 return Pointer(Pointee, Next, Offset);
160 }
161
162 /// Checks if the pointer is null.
163 bool isZero() const { return Pointee == nullptr; }
164 /// Checks if the pointer is live.
165 bool isLive() const { return Pointee && !Pointee->IsDead; }
166 /// Checks if the item is a field in an object.
167 bool isField() const { return Base != 0 && Base != RootPtrMark; }
168
169 /// Accessor for information about the declaration site.
170 Descriptor *getDeclDesc() const { return Pointee->Desc; }
172
173 /// Returns a pointer to the object of which this pointer is a field.
174 Pointer getBase() const {
175 if (Base == RootPtrMark) {
176 assert(Offset == PastEndMark && "cannot get base of a block");
177 return Pointer(Pointee, Base, 0);
178 }
179 assert(Offset == Base && "not an inner field");
180 unsigned NewBase = Base - getInlineDesc()->Offset;
181 return Pointer(Pointee, NewBase, NewBase);
182 }
183 /// Returns the parent array.
185 if (Base == RootPtrMark) {
186 assert(Offset != 0 && Offset != PastEndMark && "not an array element");
187 return Pointer(Pointee, Base, 0);
188 }
189 assert(Offset != Base && "not an array element");
190 return Pointer(Pointee, Base, Base);
191 }
192
193 /// Accessors for information about the innermost field.
195 if (Base == 0 || Base == RootPtrMark)
196 return getDeclDesc();
197 return getInlineDesc()->Desc;
198 }
199
200 /// Returns the type of the innermost field.
201 QualType getType() const { return getFieldDesc()->getType(); }
202
203 /// Returns the element size of the innermost field.
204 size_t elemSize() const {
205 if (Base == RootPtrMark)
206 return getDeclDesc()->getSize();
207 return getFieldDesc()->getElemSize();
208 }
209 /// Returns the total size of the innermost field.
210 size_t getSize() const { return getFieldDesc()->getSize(); }
211
212 /// Returns the offset into an array.
213 unsigned getOffset() const {
214 assert(Offset != PastEndMark && "invalid offset");
215 if (Base == RootPtrMark)
216 return Offset;
217
218 unsigned Adjust = 0;
219 if (Offset != Base) {
220 if (getFieldDesc()->ElemDesc)
221 Adjust = sizeof(InlineDescriptor);
222 else
223 Adjust = sizeof(InitMap *);
224 }
225 return Offset - Base - Adjust;
226 }
227
228 /// Whether this array refers to an array, but not
229 /// to the first element.
230 bool isArrayRoot() const { return inArray() && Offset == Base; }
231
232 /// Checks if the innermost field is an array.
233 bool inArray() const { return getFieldDesc()->IsArray; }
234 /// Checks if the structure is a primitive array.
235 bool inPrimitiveArray() const { return getFieldDesc()->isPrimitiveArray(); }
236 /// Checks if the structure is an array of unknown size.
237 bool isUnknownSizeArray() const {
239 }
240 /// Checks if the pointer points to an array.
241 bool isArrayElement() const { return Base != Offset; }
242 /// Pointer points directly to a block.
243 bool isRoot() const {
244 return (Base == 0 || Base == RootPtrMark) && Offset == 0;
245 }
246
247 /// Returns the record descriptor of a class.
248 Record *getRecord() const { return getFieldDesc()->ElemRecord; }
249 // Returns the element record type, if this is a non-primive array.
251 /// Returns the field information.
252 const FieldDecl *getField() const { return getFieldDesc()->asFieldDecl(); }
253
254 /// Checks if the object is a union.
255 bool isUnion() const;
256
257 /// Checks if the storage is extern.
258 bool isExtern() const { return Pointee->isExtern(); }
259 /// Checks if the storage is static.
260 bool isStatic() const { return Pointee->isStatic(); }
261 /// Checks if the storage is temporary.
262 bool isTemporary() const { return Pointee->isTemporary(); }
263 /// Checks if the storage is a static temporary.
264 bool isStaticTemporary() const { return isStatic() && isTemporary(); }
265
266 /// Checks if the field is mutable.
267 bool isMutable() const {
268 return Base != 0 && getInlineDesc()->IsFieldMutable;
269 }
270 /// Checks if an object was initialized.
271 bool isInitialized() const;
272 /// Checks if the object is active.
273 bool isActive() const { return Base == 0 || getInlineDesc()->IsActive; }
274 /// Checks if a structure is a base class.
275 bool isBaseClass() const { return isField() && getInlineDesc()->IsBase; }
276
277 /// Checks if an object or a subfield is mutable.
278 bool isConst() const {
279 return Base == 0 ? getDeclDesc()->IsConst : getInlineDesc()->IsConst;
280 }
281
282 /// Returns the declaration ID.
283 std::optional<unsigned> getDeclID() const { return Pointee->getDeclID(); }
284
285 /// Returns the byte offset from the start.
286 unsigned getByteOffset() const {
287 return Offset;
288 }
289
290 /// Returns the number of elements.
291 unsigned getNumElems() const { return getSize() / elemSize(); }
292
293 Block *block() const { return Pointee; }
294
295 /// Returns the index into an array.
296 int64_t getIndex() const {
297 if (isElementPastEnd())
298 return 1;
299 if (auto ElemSize = elemSize())
300 return getOffset() / ElemSize;
301 return 0;
302 }
303
304 /// Checks if the index is one past end.
305 bool isOnePastEnd() const {
306 return isElementPastEnd() || getSize() == getOffset();
307 }
308
309 /// Checks if the pointer is an out-of-bounds element pointer.
310 bool isElementPastEnd() const { return Offset == PastEndMark; }
311
312 /// Dereferences the pointer, if it's live.
313 template <typename T> T &deref() const {
314 assert(isLive() && "Invalid pointer");
315 if (isArrayRoot())
316 return *reinterpret_cast<T *>(Pointee->rawData() + Base +
317 sizeof(InitMap *));
318
319 return *reinterpret_cast<T *>(Pointee->rawData() + Offset);
320 }
321
322 /// Dereferences a primitive element.
323 template <typename T> T &elem(unsigned I) const {
324 return reinterpret_cast<T *>(Pointee->rawData())[I];
325 }
326
327 /// Initializes a field.
328 void initialize() const;
329 /// Activats a field.
330 void activate() const;
331 /// Deactivates an entire strurcutre.
332 void deactivate() const;
333
334 /// Checks if two pointers are comparable.
335 static bool hasSameBase(const Pointer &A, const Pointer &B);
336 /// Checks if two pointers can be subtracted.
337 static bool hasSameArray(const Pointer &A, const Pointer &B);
338
339 /// Prints the pointer.
340 void print(llvm::raw_ostream &OS) const {
341 OS << Pointee << " {" << Base << ", " << Offset << ", ";
342 if (Pointee)
343 OS << Pointee->getSize();
344 else
345 OS << "nullptr";
346 OS << "}";
347 }
348
349private:
350 friend class Block;
351 friend class DeadBlock;
352
353 Pointer(Block *Pointee, unsigned Base, unsigned Offset);
354
355 /// Returns the embedded descriptor preceding a field.
356 InlineDescriptor *getInlineDesc() const { return getDescriptor(Base); }
357
358 /// Returns a descriptor at a given offset.
359 InlineDescriptor *getDescriptor(unsigned Offset) const {
360 assert(Offset != 0 && "Not a nested pointer");
361 return reinterpret_cast<InlineDescriptor *>(Pointee->rawData() + Offset) -
362 1;
363 }
364
365 /// Returns a reference to the pointer which stores the initialization map.
366 InitMap *&getInitMap() const {
367 return *reinterpret_cast<InitMap **>(Pointee->rawData() + Base);
368 }
369
370 /// The block the pointer is pointing to.
371 Block *Pointee = nullptr;
372 /// Start of the current subfield.
373 unsigned Base = 0;
374 /// Offset into the block.
375 unsigned Offset = 0;
376
377 /// Previous link in the pointer chain.
378 Pointer *Prev = nullptr;
379 /// Next link in the pointer chain.
380 Pointer *Next = nullptr;
381};
382
383inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Pointer &P) {
384 P.print(OS);
385 return OS;
386}
387
388} // namespace interp
389} // namespace clang
390
391#endif
StringRef P
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
unsigned Offset
Definition: Format.cpp:2776
llvm::raw_ostream & OS
Definition: Logger.cpp:24
APValue - This class implements a discriminated union of [uninitialized] [APSInt] [APFloat],...
Definition: APValue.h:122
Represents a member of a struct/union/class.
Definition: Decl.h:2941
A (possibly-)qualified type.
Definition: Type.h:736
Encodes a location in the source.
A memory block, either on the stack or in the heap.
Definition: InterpBlock.h:49
unsigned getSize() const
Returns the size of the block.
Definition: InterpBlock.h:71
bool isExtern() const
Checks if the block is extern.
Definition: InterpBlock.h:65
Descriptor * Desc
Pointer to the stack slot descriptor.
Definition: InterpBlock.h:140
bool isStatic() const
Checks if the block has static storage duration.
Definition: InterpBlock.h:67
bool isTemporary() const
Checks if the block is temporary.
Definition: InterpBlock.h:69
bool IsDead
Flag indicating if the pointer is dead.
Definition: InterpBlock.h:138
char * rawData()
Returns a pointer to the raw data, including metadata.
Definition: InterpBlock.h:90
std::optional< unsigned > getDeclID() const
Returns the declaration ID.
Definition: InterpBlock.h:73
Descriptor for a dead block.
Definition: InterpBlock.h:147
A pointer to a memory block, live or dead.
Definition: Pointer.h:61
static bool hasSameBase(const Pointer &A, const Pointer &B)
Checks if two pointers are comparable.
Definition: Pointer.cpp:205
Pointer narrow() const
Restricts the scope of an array element pointer.
Definition: Pointer.h:99
void deactivate() const
Deactivates an entire strurcutre.
Definition: Pointer.cpp:201
bool isInitialized() const
Checks if an object was initialized.
Definition: Pointer.cpp:148
bool isStatic() const
Checks if the storage is static.
Definition: Pointer.h:260
bool inPrimitiveArray() const
Checks if the structure is a primitive array.
Definition: Pointer.h:235
bool isExtern() const
Checks if the storage is extern.
Definition: Pointer.h:258
int64_t getIndex() const
Returns the index into an array.
Definition: Pointer.h:296
Pointer atIndex(unsigned Idx) const
Offsets a pointer inside an array.
Definition: Pointer.h:81
Block * block() const
Definition: Pointer.h:293
bool isActive() const
Checks if the object is active.
Definition: Pointer.h:273
bool isConst() const
Checks if an object or a subfield is mutable.
Definition: Pointer.h:278
Pointer atField(unsigned Off) const
Creates a pointer to a field.
Definition: Pointer.h:93
bool isUnion() const
Checks if the object is a union.
T & deref() const
Dereferences the pointer, if it's live.
Definition: Pointer.h:313
bool isMutable() const
Checks if the field is mutable.
Definition: Pointer.h:267
unsigned getNumElems() const
Returns the number of elements.
Definition: Pointer.h:291
Pointer getArray() const
Returns the parent array.
Definition: Pointer.h:184
bool isUnknownSizeArray() const
Checks if the structure is an array of unknown size.
Definition: Pointer.h:237
void activate() const
Activats a field.
Definition: Pointer.cpp:195
void operator=(const Pointer &P)
Definition: Pointer.cpp:44
QualType getType() const
Returns the type of the innermost field.
Definition: Pointer.h:201
bool isArrayElement() const
Checks if the pointer points to an array.
Definition: Pointer.h:241
bool isArrayRoot() const
Whether this array refers to an array, but not to the first element.
Definition: Pointer.h:230
bool isLive() const
Checks if the pointer is live.
Definition: Pointer.h:165
bool inArray() const
Checks if the innermost field is an array.
Definition: Pointer.h:233
bool isStaticTemporary() const
Checks if the storage is a static temporary.
Definition: Pointer.h:264
Descriptor * getFieldDesc() const
Accessors for information about the innermost field.
Definition: Pointer.h:194
T & elem(unsigned I) const
Dereferences a primitive element.
Definition: Pointer.h:323
Pointer getBase() const
Returns a pointer to the object of which this pointer is a field.
Definition: Pointer.h:174
bool isZero() const
Checks if the pointer is null.
Definition: Pointer.h:163
bool isRoot() const
Pointer points directly to a block.
Definition: Pointer.h:243
unsigned getOffset() const
Returns the offset into an array.
Definition: Pointer.h:213
Descriptor * getDeclDesc() const
Accessor for information about the declaration site.
Definition: Pointer.h:170
Record * getElemRecord() const
Definition: Pointer.h:250
bool isOnePastEnd() const
Checks if the index is one past end.
Definition: Pointer.h:305
static bool hasSameArray(const Pointer &A, const Pointer &B)
Checks if two pointers can be subtracted.
Definition: Pointer.cpp:209
const FieldDecl * getField() const
Returns the field information.
Definition: Pointer.h:252
void print(llvm::raw_ostream &OS) const
Prints the pointer.
Definition: Pointer.h:340
Pointer expand() const
Expands a pointer to the containing array, undoing narrowing.
Definition: Pointer.h:135
friend class Block
Definition: Pointer.h:350
bool isElementPastEnd() const
Checks if the pointer is an out-of-bounds element pointer.
Definition: Pointer.h:310
Record * getRecord() const
Returns the record descriptor of a class.
Definition: Pointer.h:248
size_t getSize() const
Returns the total size of the innermost field.
Definition: Pointer.h:210
bool isTemporary() const
Checks if the storage is temporary.
Definition: Pointer.h:262
SourceLocation getDeclLoc() const
Definition: Pointer.h:171
APValue toAPValue() const
Converts the pointer to an APValue.
Definition: Pointer.cpp:78
std::optional< unsigned > getDeclID() const
Returns the declaration ID.
Definition: Pointer.h:283
bool isBaseClass() const
Checks if a structure is a base class.
Definition: Pointer.h:275
size_t elemSize() const
Returns the element size of the innermost field.
Definition: Pointer.h:204
void initialize() const
Initializes a field.
Definition: Pointer.cpp:168
bool isField() const
Checks if the item is a field in an object.
Definition: Pointer.h:167
unsigned getByteOffset() const
Returns the byte offset from the start.
Definition: Pointer.h:286
Structure/Class descriptor.
Definition: Record.h:25
PrimType
Enumeration of the primitive types of the VM.
Definition: PrimType.h:29
llvm::raw_ostream & operator<<(llvm::raw_ostream &OS, const Boolean &B)
Definition: Boolean.h:154
Describes a memory block created by an allocation site.
Definition: Descriptor.h:76
const bool IsConst
Flag indicating if the block is mutable.
Definition: Descriptor.h:104
unsigned getSize() const
Returns the size of the object without metadata.
Definition: Descriptor.h:158
QualType getType() const
Definition: Descriptor.cpp:268
SourceLocation getLocation() const
Definition: Descriptor.cpp:276
bool isUnknownSizeArray() const
Checks if the descriptor is of an array of unknown size.
Definition: Descriptor.h:180
unsigned getElemSize() const
returns the size of an element when the structure is viewed as an array.
Definition: Descriptor.h:166
const bool IsArray
Flag indicating if the block is an array.
Definition: Descriptor.h:110
Descriptor *const ElemDesc
Descriptor of the array element.
Definition: Descriptor.h:102
bool isPrimitiveArray() const
Checks if the descriptor is of an array of primitives.
Definition: Descriptor.h:176
const FieldDecl * asFieldDecl() const
Definition: Descriptor.h:149
Record *const ElemRecord
Pointer to the record, if block contains records.
Definition: Descriptor.h:100
Bitfield tracking the initialisation status of elements of primitive arrays.
Definition: Descriptor.h:193
Inline descriptor embedded in structures and arrays.
Definition: Descriptor.h:53
unsigned IsActive
Flag indicating if the field is the active member of a union.
Definition: Descriptor.h:68
unsigned IsBase
Flag indicating if the field is an embedded base class.
Definition: Descriptor.h:66
unsigned Offset
Offset inside the structure/array.
Definition: Descriptor.h:55
unsigned IsConst
Flag indicating if the storage is constant or not.
Definition: Descriptor.h:59
unsigned IsFieldMutable
Flag indicating if the field is mutable (if in a record).
Definition: Descriptor.h:70