/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/AST/Interp/Pointer.h
Line | Count | Source (jump to first uncovered line) |
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" |
18 | | #include "clang/AST/ComparisonCategories.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 | | |
25 | | namespace clang { |
26 | | namespace interp { |
27 | | class Block; |
28 | | class DeadBlock; |
29 | | class Pointer; |
30 | | enum 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 | | class Pointer { |
37 | | private: |
38 | | static constexpr unsigned PastEndMark = (unsigned)-1; |
39 | | static constexpr unsigned RootPtrMark = (unsigned)-1; |
40 | | |
41 | | public: |
42 | 14 | Pointer() {} |
43 | | Pointer(Block *B); |
44 | | Pointer(const Pointer &P); |
45 | | Pointer(Pointer &&P); |
46 | | ~Pointer(); |
47 | | |
48 | | void operator=(const Pointer &P); |
49 | | void operator=(Pointer &&P); |
50 | | |
51 | | /// Converts the pointer to an APValue. |
52 | | APValue toAPValue() const; |
53 | | |
54 | | /// Offsets a pointer inside an array. |
55 | 0 | Pointer atIndex(unsigned Idx) const { |
56 | 0 | if (Base == RootPtrMark) |
57 | 0 | return Pointer(Pointee, RootPtrMark, getDeclDesc()->getSize()); |
58 | 0 | unsigned Off = Idx * elemSize(); |
59 | 0 | if (getFieldDesc()->ElemDesc) |
60 | 0 | Off += sizeof(InlineDescriptor); |
61 | 0 | else |
62 | 0 | Off += sizeof(InitMap *); |
63 | 0 | return Pointer(Pointee, Base, Base + Off); |
64 | 0 | } |
65 | | |
66 | | /// Creates a pointer to a field. |
67 | 0 | Pointer atField(unsigned Off) const { |
68 | 0 | unsigned Field = Offset + Off; |
69 | 0 | return Pointer(Pointee, Field, Field); |
70 | 0 | } |
71 | | |
72 | | /// Restricts the scope of an array element pointer. |
73 | 0 | Pointer narrow() const { |
74 | | // Null pointers cannot be narrowed. |
75 | 0 | if (isZero() || isUnknownSizeArray()) |
76 | 0 | return *this; |
77 | | |
78 | | // Pointer to an array of base types - enter block. |
79 | 0 | if (Base == RootPtrMark) |
80 | 0 | return Pointer(Pointee, 0, Offset == 0 ? Offset : PastEndMark); |
81 | | |
82 | | // Pointer is one past end - magic offset marks that. |
83 | 0 | if (isOnePastEnd()) |
84 | 0 | return Pointer(Pointee, Base, PastEndMark); |
85 | | |
86 | | // Primitive arrays are a bit special since they do not have inline |
87 | | // descriptors. If Offset != Base, then the pointer already points to |
88 | | // an element and there is nothing to do. Otherwise, the pointer is |
89 | | // adjusted to the first element of the array. |
90 | 0 | if (inPrimitiveArray()) { |
91 | 0 | if (Offset != Base) |
92 | 0 | return *this; |
93 | 0 | return Pointer(Pointee, Base, Offset + sizeof(InitMap *)); |
94 | 0 | } |
95 | | |
96 | | // Pointer is to a field or array element - enter it. |
97 | 0 | if (Offset != Base) |
98 | 0 | return Pointer(Pointee, Offset, Offset); |
99 | | |
100 | | // Enter the first element of an array. |
101 | 0 | if (!getFieldDesc()->isArray()) |
102 | 0 | return *this; |
103 | | |
104 | 0 | const unsigned NewBase = Base + sizeof(InlineDescriptor); |
105 | 0 | return Pointer(Pointee, NewBase, NewBase); |
106 | 0 | } |
107 | | |
108 | | /// Expands a pointer to the containing array, undoing narrowing. |
109 | 0 | Pointer expand() const { |
110 | 0 | if (isElementPastEnd()) { |
111 | | // Revert to an outer one-past-end pointer. |
112 | 0 | unsigned Adjust; |
113 | 0 | if (inPrimitiveArray()) |
114 | 0 | Adjust = sizeof(InitMap *); |
115 | 0 | else |
116 | 0 | Adjust = sizeof(InlineDescriptor); |
117 | 0 | return Pointer(Pointee, Base, Base + getSize() + Adjust); |
118 | 0 | } |
119 | | |
120 | | // Do not step out of array elements. |
121 | 0 | if (Base != Offset) |
122 | 0 | return *this; |
123 | | |
124 | | // If at base, point to an array of base types. |
125 | 0 | if (Base == 0) |
126 | 0 | return Pointer(Pointee, RootPtrMark, 0); |
127 | | |
128 | | // Step into the containing array, if inside one. |
129 | 0 | unsigned Next = Base - getInlineDesc()->Offset; |
130 | 0 | Descriptor *Desc = Next == 0 ? getDeclDesc() : getDescriptor(Next)->Desc; |
131 | 0 | if (!Desc->IsArray) |
132 | 0 | return *this; |
133 | 0 | return Pointer(Pointee, Next, Offset); |
134 | 0 | } |
135 | | |
136 | | /// Checks if the pointer is null. |
137 | 0 | bool isZero() const { return Pointee == nullptr; } |
138 | | /// Checks if the pointer is live. |
139 | 0 | bool isLive() const { return Pointee && !Pointee->IsDead; } |
140 | | /// Checks if the item is a field in an object. |
141 | 0 | bool isField() const { return Base != 0 && Base != RootPtrMark; } |
142 | | |
143 | | /// Accessor for information about the declaration site. |
144 | 0 | Descriptor *getDeclDesc() const { return Pointee->Desc; } |
145 | 0 | SourceLocation getDeclLoc() const { return getDeclDesc()->getLocation(); } |
146 | | |
147 | | /// Returns a pointer to the object of which this pointer is a field. |
148 | 0 | Pointer getBase() const { |
149 | 0 | if (Base == RootPtrMark) { |
150 | 0 | assert(Offset == PastEndMark && "cannot get base of a block"); |
151 | 0 | return Pointer(Pointee, Base, 0); |
152 | 0 | } |
153 | 0 | assert(Offset == Base && "not an inner field"); |
154 | 0 | unsigned NewBase = Base - getInlineDesc()->Offset; |
155 | 0 | return Pointer(Pointee, NewBase, NewBase); |
156 | 0 | } |
157 | | /// Returns the parent array. |
158 | 0 | Pointer getArray() const { |
159 | 0 | if (Base == RootPtrMark) { |
160 | 0 | assert(Offset != 0 && Offset != PastEndMark && "not an array element"); |
161 | 0 | return Pointer(Pointee, Base, 0); |
162 | 0 | } |
163 | 0 | assert(Offset != Base && "not an array element"); |
164 | 0 | return Pointer(Pointee, Base, Base); |
165 | 0 | } |
166 | | |
167 | | /// Accessors for information about the innermost field. |
168 | 0 | Descriptor *getFieldDesc() const { |
169 | 0 | if (Base == 0 || Base == RootPtrMark) |
170 | 0 | return getDeclDesc(); |
171 | 0 | return getInlineDesc()->Desc; |
172 | 0 | } |
173 | | |
174 | | /// Returns the type of the innermost field. |
175 | 0 | QualType getType() const { return getFieldDesc()->getType(); } |
176 | | |
177 | | /// Returns the element size of the innermost field. |
178 | 0 | size_t elemSize() const { |
179 | 0 | if (Base == RootPtrMark) |
180 | 0 | return getDeclDesc()->getSize(); |
181 | 0 | return getFieldDesc()->getElemSize(); |
182 | 0 | } |
183 | | /// Returns the total size of the innermost field. |
184 | 0 | size_t getSize() const { return getFieldDesc()->getSize(); } |
185 | | |
186 | | /// Returns the offset into an array. |
187 | 0 | unsigned getOffset() const { |
188 | 0 | assert(Offset != PastEndMark && "invalid offset"); |
189 | 0 | if (Base == RootPtrMark) |
190 | 0 | return Offset; |
191 | | |
192 | 0 | unsigned Adjust = 0; |
193 | 0 | if (Offset != Base) { |
194 | 0 | if (getFieldDesc()->ElemDesc) |
195 | 0 | Adjust = sizeof(InlineDescriptor); |
196 | 0 | else |
197 | 0 | Adjust = sizeof(InitMap *); |
198 | 0 | } |
199 | 0 | return Offset - Base - Adjust; |
200 | 0 | } |
201 | | |
202 | | /// Checks if the innermost field is an array. |
203 | 0 | bool inArray() const { return getFieldDesc()->IsArray; } |
204 | | /// Checks if the structure is a primitive array. |
205 | 0 | bool inPrimitiveArray() const { return getFieldDesc()->isPrimitiveArray(); } |
206 | | /// Checks if the structure is an array of unknown size. |
207 | 0 | bool isUnknownSizeArray() const { |
208 | 0 | return getFieldDesc()->isUnknownSizeArray(); |
209 | 0 | } |
210 | | /// Checks if the pointer points to an array. |
211 | 0 | bool isArrayElement() const { return Base != Offset; } |
212 | | /// Pointer points directly to a block. |
213 | 0 | bool isRoot() const { |
214 | 0 | return (Base == 0 || Base == RootPtrMark) && Offset == 0; |
215 | 0 | } |
216 | | |
217 | | /// Returns the record descriptor of a class. |
218 | 0 | Record *getRecord() const { return getFieldDesc()->ElemRecord; } |
219 | | /// Returns the field information. |
220 | 0 | const FieldDecl *getField() const { return getFieldDesc()->asFieldDecl(); } |
221 | | |
222 | | /// Checks if the object is a union. |
223 | | bool isUnion() const; |
224 | | |
225 | | /// Checks if the storage is extern. |
226 | 0 | bool isExtern() const { return Pointee->isExtern(); } |
227 | | /// Checks if the storage is static. |
228 | 0 | bool isStatic() const { return Pointee->isStatic(); } |
229 | | /// Checks if the storage is temporary. |
230 | 0 | bool isTemporary() const { return Pointee->isTemporary(); } |
231 | | /// Checks if the storage is a static temporary. |
232 | 0 | bool isStaticTemporary() const { return isStatic() && isTemporary(); } |
233 | | |
234 | | /// Checks if the field is mutable. |
235 | 0 | bool isMutable() const { return Base != 0 && getInlineDesc()->IsMutable; } |
236 | | /// Checks if an object was initialized. |
237 | | bool isInitialized() const; |
238 | | /// Checks if the object is active. |
239 | 0 | bool isActive() const { return Base == 0 || getInlineDesc()->IsActive; } |
240 | | /// Checks if a structure is a base class. |
241 | 0 | bool isBaseClass() const { return isField() && getInlineDesc()->IsBase; } |
242 | | |
243 | | /// Checks if an object or a subfield is mutable. |
244 | 0 | bool isConst() const { |
245 | 0 | return Base == 0 ? getDeclDesc()->IsConst : getInlineDesc()->IsConst; |
246 | 0 | } |
247 | | |
248 | | /// Returns the declaration ID. |
249 | 0 | llvm::Optional<unsigned> getDeclID() const { return Pointee->getDeclID(); } |
250 | | |
251 | | /// Returns the byte offset from the start. |
252 | 0 | unsigned getByteOffset() const { |
253 | 0 | return Offset; |
254 | 0 | } |
255 | | |
256 | | /// Returns the number of elements. |
257 | 0 | unsigned getNumElems() const { return getSize() / elemSize(); } |
258 | | |
259 | | /// Returns the index into an array. |
260 | 0 | int64_t getIndex() const { |
261 | 0 | if (isElementPastEnd()) |
262 | 0 | return 1; |
263 | 0 | if (auto ElemSize = elemSize()) |
264 | 0 | return getOffset() / ElemSize; |
265 | 0 | return 0; |
266 | 0 | } |
267 | | |
268 | | /// Checks if the index is one past end. |
269 | 0 | bool isOnePastEnd() const { |
270 | 0 | return isElementPastEnd() || getSize() == getOffset(); |
271 | 0 | } |
272 | | |
273 | | /// Checks if the pointer is an out-of-bounds element pointer. |
274 | 0 | bool isElementPastEnd() const { return Offset == PastEndMark; } |
275 | | |
276 | | /// Dereferences the pointer, if it's live. |
277 | 0 | template <typename T> T &deref() const { |
278 | 0 | assert(isLive() && "Invalid pointer"); |
279 | 0 | return *reinterpret_cast<T *>(Pointee->data() + Offset); |
280 | 0 | } Unexecuted instantiation: clang::interp::Integral<8u, true>& clang::interp::Pointer::deref<clang::interp::Integral<8u, true> >() const Unexecuted instantiation: clang::interp::Integral<8u, false>& clang::interp::Pointer::deref<clang::interp::Integral<8u, false> >() const Unexecuted instantiation: clang::interp::Integral<16u, true>& clang::interp::Pointer::deref<clang::interp::Integral<16u, true> >() const Unexecuted instantiation: clang::interp::Integral<16u, false>& clang::interp::Pointer::deref<clang::interp::Integral<16u, false> >() const Unexecuted instantiation: clang::interp::Integral<32u, true>& clang::interp::Pointer::deref<clang::interp::Integral<32u, true> >() const Unexecuted instantiation: clang::interp::Integral<32u, false>& clang::interp::Pointer::deref<clang::interp::Integral<32u, false> >() const Unexecuted instantiation: clang::interp::Integral<64u, true>& clang::interp::Pointer::deref<clang::interp::Integral<64u, true> >() const Unexecuted instantiation: clang::interp::Integral<64u, false>& clang::interp::Pointer::deref<clang::interp::Integral<64u, false> >() const Unexecuted instantiation: clang::interp::Boolean& clang::interp::Pointer::deref<clang::interp::Boolean>() const Unexecuted instantiation: clang::interp::Pointer& clang::interp::Pointer::deref<clang::interp::Pointer>() const |
281 | | |
282 | | /// Dereferences a primitive element. |
283 | | template <typename T> T &elem(unsigned I) const { |
284 | | return reinterpret_cast<T *>(Pointee->data())[I]; |
285 | | } |
286 | | |
287 | | /// Initializes a field. |
288 | | void initialize() const; |
289 | | /// Activats a field. |
290 | | void activate() const; |
291 | | /// Deactivates an entire strurcutre. |
292 | | void deactivate() const; |
293 | | |
294 | | /// Checks if two pointers are comparable. |
295 | | static bool hasSameBase(const Pointer &A, const Pointer &B); |
296 | | /// Checks if two pointers can be subtracted. |
297 | | static bool hasSameArray(const Pointer &A, const Pointer &B); |
298 | | |
299 | | /// Prints the pointer. |
300 | 0 | void print(llvm::raw_ostream &OS) const { |
301 | 0 | OS << "{" << Base << ", " << Offset << ", "; |
302 | 0 | if (Pointee) |
303 | 0 | OS << Pointee->getSize(); |
304 | 0 | else |
305 | 0 | OS << "nullptr"; |
306 | 0 | OS << "}"; |
307 | 0 | } |
308 | | |
309 | | private: |
310 | | friend class Block; |
311 | | friend class DeadBlock; |
312 | | |
313 | | Pointer(Block *Pointee, unsigned Base, unsigned Offset); |
314 | | |
315 | | /// Returns the embedded descriptor preceding a field. |
316 | 0 | InlineDescriptor *getInlineDesc() const { return getDescriptor(Base); } |
317 | | |
318 | | /// Returns a descriptor at a given offset. |
319 | 0 | InlineDescriptor *getDescriptor(unsigned Offset) const { |
320 | 0 | assert(Offset != 0 && "Not a nested pointer"); |
321 | 0 | return reinterpret_cast<InlineDescriptor *>(Pointee->data() + Offset) - 1; |
322 | 0 | } |
323 | | |
324 | | /// Returns a reference to the pointer which stores the initialization map. |
325 | 0 | InitMap *&getInitMap() const { |
326 | 0 | return *reinterpret_cast<InitMap **>(Pointee->data() + Base); |
327 | 0 | } |
328 | | |
329 | | /// The block the pointer is pointing to. |
330 | | Block *Pointee = nullptr; |
331 | | /// Start of the current subfield. |
332 | | unsigned Base = 0; |
333 | | /// Offset into the block. |
334 | | unsigned Offset = 0; |
335 | | |
336 | | /// Previous link in the pointer chain. |
337 | | Pointer *Prev = nullptr; |
338 | | /// Next link in the pointer chain. |
339 | | Pointer *Next = nullptr; |
340 | | }; |
341 | | |
342 | 0 | inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Pointer &P) { |
343 | 0 | P.print(OS); |
344 | 0 | return OS; |
345 | 0 | } |
346 | | |
347 | | } // namespace interp |
348 | | } // namespace clang |
349 | | |
350 | | #endif |