clang 22.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 "FunctionPointer.h"
18#include "InterpBlock.h"
20#include "clang/AST/Decl.h"
21#include "clang/AST/DeclCXX.h"
22#include "clang/AST/Expr.h"
23#include "llvm/Support/raw_ostream.h"
24
25namespace clang {
26namespace interp {
27class Block;
28class DeadBlock;
29class Pointer;
30class Context;
31
32class Pointer;
33inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Pointer &P);
34
36 /// The block the pointer is pointing to.
38 /// Start of the current subfield.
39 unsigned Base;
40 /// Previous link in the pointer chain.
42 /// Next link in the pointer chain.
44};
45
46struct IntPointer {
48 uint64_t Value;
49
50 IntPointer atOffset(const ASTContext &ASTCtx, unsigned Offset) const;
51 IntPointer baseCast(const ASTContext &ASTCtx, unsigned BaseOffset) const;
52};
53
55 const Type *TypePtr;
57};
58
59enum class Storage { Int, Block, Fn, Typeid };
60
61/// A pointer to a memory block, live or dead.
62///
63/// This object can be allocated into interpreter stack frames. If pointing to
64/// a live block, it is a link in the chain of pointers pointing to the block.
65///
66/// In the simplest form, a Pointer has a Block* (the pointee) and both Base
67/// and Offset are 0, which means it will point to raw data.
68///
69/// The Base field is used to access metadata about the data. For primitive
70/// arrays, the Base is followed by an InitMap. In a variety of cases, the
71/// Base is preceded by an InlineDescriptor, which is used to track the
72/// initialization state, among other things.
73///
74/// The Offset field is used to access the actual data. In other words, the
75/// data the pointer decribes can be found at
76/// Pointee->rawData() + Pointer.Offset.
77///
78/// \verbatim
79/// Pointee Offset
80/// │ │
81/// │ │
82/// ▼ ▼
83/// ┌───────┬────────────┬─────────┬────────────────────────────┐
84/// │ Block │ InlineDesc │ InitMap │ Actual Data │
85/// └───────┴────────────┴─────────┴────────────────────────────┘
86/// ▲
87/// │
88/// │
89/// Base
90/// \endverbatim
91class Pointer {
92private:
93 static constexpr unsigned PastEndMark = ~0u;
94 static constexpr unsigned RootPtrMark = ~0u;
95
96public:
97 Pointer() : StorageKind(Storage::Int), Int{nullptr, 0} {}
99 : StorageKind(Storage::Int), Int(std::move(IntPtr)) {}
100 Pointer(Block *B);
101 Pointer(Block *B, uint64_t BaseAndOffset);
102 Pointer(const Pointer &P);
103 Pointer(Pointer &&P);
104 Pointer(uint64_t Address, const Descriptor *Desc, uint64_t Offset = 0)
105 : Offset(Offset), StorageKind(Storage::Int), Int{Desc, Address} {}
106 Pointer(const Function *F, uint64_t Offset = 0)
107 : Offset(Offset), StorageKind(Storage::Fn), Fn(F) {}
108 Pointer(const Type *TypePtr, const Type *TypeInfoType, uint64_t Offset = 0)
109 : Offset(Offset), StorageKind(Storage::Typeid) {
110 Typeid.TypePtr = TypePtr;
111 Typeid.TypeInfoType = TypeInfoType;
112 }
113 Pointer(Block *Pointee, unsigned Base, uint64_t Offset);
114 ~Pointer();
115
116 Pointer &operator=(const Pointer &P);
118
119 /// Equality operators are just for tests.
120 bool operator==(const Pointer &P) const {
121 if (P.StorageKind != StorageKind)
122 return false;
123 if (isIntegralPointer())
124 return P.Int.Value == Int.Value && P.Int.Desc == Int.Desc &&
125 P.Offset == Offset;
126
127 if (isFunctionPointer())
128 return P.Fn.getFunction() == Fn.getFunction() && P.Offset == Offset;
129
130 assert(isBlockPointer());
131 return P.BS.Pointee == BS.Pointee && P.BS.Base == BS.Base &&
132 P.Offset == Offset;
133 }
134
135 bool operator!=(const Pointer &P) const { return !(P == *this); }
136
137 /// Converts the pointer to an APValue.
138 APValue toAPValue(const ASTContext &ASTCtx) const;
139
140 /// Converts the pointer to a string usable in diagnostics.
141 std::string toDiagnosticString(const ASTContext &Ctx) const;
142
143 uint64_t getIntegerRepresentation() const {
144 if (isIntegralPointer())
145 return Int.Value + (Offset * elemSize());
146 if (isFunctionPointer())
147 return Fn.getIntegerRepresentation() + Offset;
148 return reinterpret_cast<uint64_t>(BS.Pointee) + Offset;
149 }
150
151 /// Converts the pointer to an APValue that is an rvalue.
152 std::optional<APValue> toRValue(const Context &Ctx,
153 QualType ResultType) const;
154
155 /// Offsets a pointer inside an array.
156 [[nodiscard]] Pointer atIndex(uint64_t Idx) const {
157 if (isIntegralPointer())
158 return Pointer(Int.Value, Int.Desc, Idx);
159 if (isFunctionPointer())
160 return Pointer(Fn.getFunction(), Idx);
161
162 if (BS.Base == RootPtrMark)
163 return Pointer(BS.Pointee, RootPtrMark, getDeclDesc()->getSize());
164 uint64_t Off = Idx * elemSize();
165 if (getFieldDesc()->ElemDesc)
166 Off += sizeof(InlineDescriptor);
167 else
168 Off += sizeof(InitMapPtr);
169 return Pointer(BS.Pointee, BS.Base, BS.Base + Off);
170 }
171
172 /// Creates a pointer to a field.
173 [[nodiscard]] Pointer atField(unsigned Off) const {
174 assert(isBlockPointer());
175 unsigned Field = Offset + Off;
176 return Pointer(BS.Pointee, Field, Field);
177 }
178
179 /// Subtract the given offset from the current Base and Offset
180 /// of the pointer.
181 [[nodiscard]] Pointer atFieldSub(unsigned Off) const {
182 assert(Offset >= Off);
183 unsigned O = Offset - Off;
184 return Pointer(BS.Pointee, O, O);
185 }
186
187 /// Restricts the scope of an array element pointer.
188 [[nodiscard]] Pointer narrow() const {
189 if (!isBlockPointer())
190 return *this;
191 assert(isBlockPointer());
192 // Null pointers cannot be narrowed.
193 if (isZero() || isUnknownSizeArray())
194 return *this;
195
196 unsigned Base = BS.Base;
197 // Pointer to an array of base types - enter block.
198 if (Base == RootPtrMark)
199 return Pointer(BS.Pointee, sizeof(InlineDescriptor),
200 Offset == 0 ? Offset : PastEndMark);
201
202 // Pointer is one past end - magic offset marks that.
203 if (isOnePastEnd())
204 return Pointer(BS.Pointee, Base, PastEndMark);
205
206 if (Offset != Base) {
207 // If we're pointing to a primitive array element, there's nothing to do.
208 if (inPrimitiveArray())
209 return *this;
210 // Pointer is to a composite array element - enter it.
211 if (Offset != Base)
212 return Pointer(BS.Pointee, Offset, Offset);
213 }
214
215 // Otherwise, we're pointing to a non-array element or
216 // are already narrowed to a composite array element. Nothing to do.
217 return *this;
218 }
219
220 /// Expands a pointer to the containing array, undoing narrowing.
221 [[nodiscard]] Pointer expand() const {
222 assert(isBlockPointer());
223 Block *Pointee = BS.Pointee;
224
225 if (isElementPastEnd()) {
226 // Revert to an outer one-past-end pointer.
227 unsigned Adjust;
228 if (inPrimitiveArray())
229 Adjust = sizeof(InitMapPtr);
230 else
231 Adjust = sizeof(InlineDescriptor);
232 return Pointer(Pointee, BS.Base, BS.Base + getSize() + Adjust);
233 }
234
235 // Do not step out of array elements.
236 if (BS.Base != Offset)
237 return *this;
238
239 if (isRoot())
240 return Pointer(Pointee, BS.Base, BS.Base);
241
242 // Step into the containing array, if inside one.
243 unsigned Next = BS.Base - getInlineDesc()->Offset;
244 const Descriptor *Desc =
245 (Next == Pointee->getDescriptor()->getMetadataSize())
246 ? getDeclDesc()
247 : getDescriptor(Next)->Desc;
248 if (!Desc->IsArray)
249 return *this;
250 return Pointer(Pointee, Next, Offset);
251 }
252
253 /// Checks if the pointer is null.
254 bool isZero() const {
255 switch (StorageKind) {
256 case Storage::Int:
257 return Int.Value == 0 && Offset == 0;
258 case Storage::Block:
259 return BS.Pointee == nullptr;
260 case Storage::Fn:
261 return Fn.isZero();
262 case Storage::Typeid:
263 return false;
264 }
265 llvm_unreachable("Unknown clang::interp::Storage enum");
266 }
267 /// Checks if the pointer is live.
268 bool isLive() const {
269 if (!isBlockPointer())
270 return true;
271 return BS.Pointee && !BS.Pointee->isDead();
272 }
273 /// Checks if the item is a field in an object.
274 bool isField() const {
275 if (!isBlockPointer())
276 return false;
277
278 return !isRoot() && getFieldDesc()->asDecl();
279 }
280
281 /// Accessor for information about the declaration site.
282 const Descriptor *getDeclDesc() const {
283 if (isIntegralPointer())
284 return Int.Desc;
286 return nullptr;
287
288 assert(isBlockPointer());
289 assert(BS.Pointee);
290 return BS.Pointee->Desc;
291 }
293
294 /// Returns the expression or declaration the pointer has been created for.
296 if (isBlockPointer())
297 return getDeclDesc()->getSource();
298 if (isFunctionPointer()) {
299 const Function *F = Fn.getFunction();
300 return F ? F->getDecl() : DeclTy();
301 }
302 assert(isIntegralPointer());
303 return Int.Desc ? Int.Desc->getSource() : DeclTy();
304 }
305
306 /// Returns a pointer to the object of which this pointer is a field.
307 [[nodiscard]] Pointer getBase() const {
308 if (BS.Base == RootPtrMark) {
309 assert(Offset == PastEndMark && "cannot get base of a block");
310 return Pointer(BS.Pointee, BS.Base, 0);
311 }
312 unsigned NewBase = BS.Base - getInlineDesc()->Offset;
313 return Pointer(BS.Pointee, NewBase, NewBase);
314 }
315 /// Returns the parent array.
316 [[nodiscard]] Pointer getArray() const {
317 if (BS.Base == RootPtrMark) {
318 assert(Offset != 0 && Offset != PastEndMark && "not an array element");
319 return Pointer(BS.Pointee, BS.Base, 0);
320 }
321 assert(Offset != BS.Base && "not an array element");
322 return Pointer(BS.Pointee, BS.Base, BS.Base);
323 }
324
325 /// Accessors for information about the innermost field.
326 const Descriptor *getFieldDesc() const {
327 if (isIntegralPointer())
328 return Int.Desc;
329
330 if (isRoot())
331 return getDeclDesc();
332 return getInlineDesc()->Desc;
333 }
334
335 /// Returns the type of the innermost field.
337 if (isTypeidPointer())
338 return QualType(Typeid.TypeInfoType, 0);
339 if (isFunctionPointer())
340 return Fn.getFunction()->getDecl()->getType();
341
342 if (inPrimitiveArray() && Offset != BS.Base) {
343 // Unfortunately, complex and vector types are not array types in clang,
344 // but they are for us.
345 if (const auto *AT = getFieldDesc()->getType()->getAsArrayTypeUnsafe())
346 return AT->getElementType();
347 if (const auto *CT = getFieldDesc()->getType()->getAs<ComplexType>())
348 return CT->getElementType();
349 if (const auto *CT = getFieldDesc()->getType()->getAs<VectorType>())
350 return CT->getElementType();
351 }
352 return getFieldDesc()->getType();
353 }
354
355 [[nodiscard]] Pointer getDeclPtr() const { return Pointer(BS.Pointee); }
356
357 /// Returns the element size of the innermost field.
358 size_t elemSize() const {
359 if (isIntegralPointer()) {
360 if (!Int.Desc)
361 return 1;
362 return Int.Desc->getElemSize();
363 }
364
365 if (BS.Base == RootPtrMark)
366 return getDeclDesc()->getSize();
367 return getFieldDesc()->getElemSize();
368 }
369 /// Returns the total size of the innermost field.
370 size_t getSize() const {
371 assert(isBlockPointer());
372 return getFieldDesc()->getSize();
373 }
374
375 /// Returns the offset into an array.
376 unsigned getOffset() const {
377 assert(Offset != PastEndMark && "invalid offset");
378 assert(isBlockPointer());
379 if (BS.Base == RootPtrMark)
380 return Offset;
381
382 unsigned Adjust = 0;
383 if (Offset != BS.Base) {
384 if (getFieldDesc()->ElemDesc)
385 Adjust = sizeof(InlineDescriptor);
386 else
387 Adjust = sizeof(InitMapPtr);
388 }
389 return Offset - BS.Base - Adjust;
390 }
391
392 /// Whether this array refers to an array, but not
393 /// to the first element.
394 bool isArrayRoot() const { return inArray() && Offset == BS.Base; }
395
396 /// Checks if the innermost field is an array.
397 bool inArray() const {
398 if (isBlockPointer())
399 return getFieldDesc()->IsArray;
400 return false;
401 }
402 bool inUnion() const {
403 if (isBlockPointer() && BS.Base >= sizeof(InlineDescriptor))
404 return getInlineDesc()->InUnion;
405 return false;
406 };
407
408 /// Checks if the structure is a primitive array.
409 bool inPrimitiveArray() const {
410 if (isBlockPointer())
411 return getFieldDesc()->isPrimitiveArray();
412 return false;
413 }
414 /// Checks if the structure is an array of unknown size.
415 bool isUnknownSizeArray() const {
416 if (!isBlockPointer())
417 return false;
419 }
420 /// Checks if the pointer points to an array.
421 bool isArrayElement() const {
422 if (!isBlockPointer())
423 return false;
424
425 const BlockPointer &BP = BS;
426 if (inArray() && BP.Base != Offset)
427 return true;
428
429 // Might be a narrow()'ed element in a composite array.
430 // Check the inline descriptor.
431 if (BP.Base >= sizeof(InlineDescriptor) && getInlineDesc()->IsArrayElement)
432 return true;
433
434 return false;
435 }
436 /// Pointer points directly to a block.
437 bool isRoot() const {
438 if (isZero() || !isBlockPointer())
439 return true;
440 return (BS.Base == BS.Pointee->getDescriptor()->getMetadataSize() ||
441 BS.Base == 0);
442 }
443 /// If this pointer has an InlineDescriptor we can use to initialize.
444 bool canBeInitialized() const {
445 if (!isBlockPointer())
446 return false;
447
448 return BS.Pointee && BS.Base > 0;
449 }
450
451 [[nodiscard]] const BlockPointer &asBlockPointer() const {
452 assert(isBlockPointer());
453 return BS;
454 }
455 [[nodiscard]] const IntPointer &asIntPointer() const {
456 assert(isIntegralPointer());
457 return Int;
458 }
459 [[nodiscard]] const FunctionPointer &asFunctionPointer() const {
460 assert(isFunctionPointer());
461 return Fn;
462 }
463 [[nodiscard]] const TypeidPointer &asTypeidPointer() const {
464 assert(isTypeidPointer());
465 return Typeid;
466 }
467
468 bool isBlockPointer() const { return StorageKind == Storage::Block; }
469 bool isIntegralPointer() const { return StorageKind == Storage::Int; }
470 bool isFunctionPointer() const { return StorageKind == Storage::Fn; }
471 bool isTypeidPointer() const { return StorageKind == Storage::Typeid; }
472
473 /// Returns the record descriptor of a class.
474 const Record *getRecord() const { return getFieldDesc()->ElemRecord; }
475 /// Returns the element record type, if this is a non-primive array.
476 const Record *getElemRecord() const {
477 const Descriptor *ElemDesc = getFieldDesc()->ElemDesc;
478 return ElemDesc ? ElemDesc->ElemRecord : nullptr;
479 }
480 /// Returns the field information.
481 const FieldDecl *getField() const {
482 if (const Descriptor *FD = getFieldDesc())
483 return FD->asFieldDecl();
484 return nullptr;
485 }
486
487 /// Checks if the storage is extern.
488 bool isExtern() const {
489 if (isBlockPointer())
490 return BS.Pointee && BS.Pointee->isExtern();
491 return false;
492 }
493 /// Checks if the storage is static.
494 bool isStatic() const {
495 if (!isBlockPointer())
496 return true;
497 assert(BS.Pointee);
498 return BS.Pointee->isStatic();
499 }
500 /// Checks if the storage is temporary.
501 bool isTemporary() const {
502 if (isBlockPointer()) {
503 assert(BS.Pointee);
504 return BS.Pointee->isTemporary();
505 }
506 return false;
507 }
508 /// Checks if the storage has been dynamically allocated.
509 bool isDynamic() const {
510 if (isBlockPointer()) {
511 assert(BS.Pointee);
512 return BS.Pointee->isDynamic();
513 }
514 return false;
515 }
516 /// Checks if the storage is a static temporary.
517 bool isStaticTemporary() const { return isStatic() && isTemporary(); }
518
519 /// Checks if the field is mutable.
520 bool isMutable() const {
521 if (!isBlockPointer())
522 return false;
523 return !isRoot() && getInlineDesc()->IsFieldMutable;
524 }
525
526 bool isWeak() const {
527 if (isFunctionPointer())
528 return Fn.isWeak();
529 if (!isBlockPointer())
530 return false;
531
532 assert(isBlockPointer());
533 return BS.Pointee->isWeak();
534 }
535 /// Checks if the object is active.
536 bool isActive() const {
537 if (!isBlockPointer())
538 return true;
539 return isRoot() || getInlineDesc()->IsActive;
540 }
541 /// Checks if a structure is a base class.
542 bool isBaseClass() const { return isField() && getInlineDesc()->IsBase; }
543 bool isVirtualBaseClass() const {
544 return isField() && getInlineDesc()->IsVirtualBase;
545 }
546 /// Checks if the pointer points to a dummy value.
547 bool isDummy() const {
548 if (!isBlockPointer())
549 return false;
550
551 if (const Block *Pointee = BS.Pointee)
552 return Pointee->isDummy();
553 return false;
554 }
555
556 /// Checks if an object or a subfield is mutable.
557 bool isConst() const {
558 if (isIntegralPointer())
559 return true;
560 return isRoot() ? getDeclDesc()->IsConst : getInlineDesc()->IsConst;
561 }
562 bool isConstInMutable() const {
563 if (!isBlockPointer())
564 return false;
565 return isRoot() ? false : getInlineDesc()->IsConstInMutable;
566 }
567
568 /// Checks if an object or a subfield is volatile.
569 bool isVolatile() const {
570 if (!isBlockPointer())
571 return false;
572 return isRoot() ? getDeclDesc()->IsVolatile : getInlineDesc()->IsVolatile;
573 }
574
575 /// Returns the declaration ID.
577 if (isBlockPointer()) {
578 assert(BS.Pointee);
579 return BS.Pointee->getDeclID();
580 }
581 return std::nullopt;
582 }
583
584 /// Returns the byte offset from the start.
585 uint64_t getByteOffset() const {
586 if (isIntegralPointer())
587 return Int.Value + Offset;
588 if (isTypeidPointer())
589 return reinterpret_cast<uintptr_t>(Typeid.TypePtr) + Offset;
590 if (isOnePastEnd())
591 return PastEndMark;
592 return Offset;
593 }
594
595 /// Returns the number of elements.
596 unsigned getNumElems() const {
597 if (!isBlockPointer())
598 return ~0u;
599 return getSize() / elemSize();
600 }
601
602 const Block *block() const { return BS.Pointee; }
603
604 /// If backed by actual data (i.e. a block pointer), return
605 /// an address to that data.
606 const std::byte *getRawAddress() const {
607 assert(isBlockPointer());
608 return BS.Pointee->rawData() + Offset;
609 }
610
611 /// Returns the index into an array.
612 int64_t getIndex() const {
613 if (!isBlockPointer())
615
616 if (isZero())
617 return 0;
618
619 // narrow()ed element in a composite array.
620 if (BS.Base > sizeof(InlineDescriptor) && BS.Base == Offset)
621 return 0;
622
623 if (auto ElemSize = elemSize())
624 return getOffset() / ElemSize;
625 return 0;
626 }
627
628 /// Checks if the index is one past end.
629 bool isOnePastEnd() const {
630 if (!isBlockPointer())
631 return false;
632
633 if (!BS.Pointee)
634 return false;
635
636 if (isUnknownSizeArray())
637 return false;
638
639 return isPastEnd() || (getSize() == getOffset());
640 }
641
642 /// Checks if the pointer points past the end of the object.
643 bool isPastEnd() const {
644 if (isIntegralPointer())
645 return false;
646
647 return !isZero() && Offset > BS.Pointee->getSize();
648 }
649
650 /// Checks if the pointer is an out-of-bounds element pointer.
651 bool isElementPastEnd() const { return Offset == PastEndMark; }
652
653 /// Checks if the pointer is pointing to a zero-size array.
654 bool isZeroSizeArray() const {
655 if (isFunctionPointer())
656 return false;
657 if (const auto *Desc = getFieldDesc())
658 return Desc->isZeroSizeArray();
659 return false;
660 }
661
662 /// Dereferences the pointer, if it's live.
663 template <typename T> T &deref() const {
664 assert(isLive() && "Invalid pointer");
665 assert(isBlockPointer());
666 assert(BS.Pointee);
667 assert(isDereferencable());
668 assert(Offset + sizeof(T) <= BS.Pointee->getDescriptor()->getAllocSize());
669
670 if (isArrayRoot())
671 return *reinterpret_cast<T *>(BS.Pointee->rawData() + BS.Base +
672 sizeof(InitMapPtr));
673
674 return *reinterpret_cast<T *>(BS.Pointee->rawData() + Offset);
675 }
676
677 /// Dereferences the element at index \p I.
678 /// This is equivalent to atIndex(I).deref<T>().
679 template <typename T> T &elem(unsigned I) const {
680 assert(isLive() && "Invalid pointer");
681 assert(isBlockPointer());
682 assert(BS.Pointee);
683 assert(isDereferencable());
684 assert(getFieldDesc()->isPrimitiveArray());
685 assert(I < getFieldDesc()->getNumElems());
686
687 unsigned ElemByteOffset = I * getFieldDesc()->getElemSize();
688 unsigned ReadOffset = BS.Base + sizeof(InitMapPtr) + ElemByteOffset;
689 assert(ReadOffset + sizeof(T) <=
690 BS.Pointee->getDescriptor()->getAllocSize());
691
692 return *reinterpret_cast<T *>(BS.Pointee->rawData() + ReadOffset);
693 }
694
695 /// Whether this block can be read from at all. This is only true for
696 /// block pointers that point to a valid location inside that block.
697 bool isDereferencable() const {
698 if (!isBlockPointer())
699 return false;
700 if (isPastEnd())
701 return false;
702
703 return true;
704 }
705
706 /// Initializes a field.
707 void initialize() const;
708 /// Initialized the given element of a primitive array.
709 void initializeElement(unsigned Index) const;
710 /// Initialize all elements of a primitive array at once. This can be
711 /// used in situations where we *know* we have initialized *all* elements
712 /// of a primtive array.
713 void initializeAllElements() const;
714 /// Checks if an object was initialized.
715 bool isInitialized() const;
716 /// Like isInitialized(), but for primitive arrays.
717 bool isElementInitialized(unsigned Index) const;
718 bool allElementsInitialized() const;
719 /// Activats a field.
720 void activate() const;
721 /// Deactivates an entire strurcutre.
722 void deactivate() const;
723
725 if (!isBlockPointer())
726 return Lifetime::Started;
727 if (BS.Base < sizeof(InlineDescriptor))
728 return Lifetime::Started;
729 return getInlineDesc()->LifeState;
730 }
731
732 void endLifetime() const {
733 if (!isBlockPointer())
734 return;
735 if (BS.Base < sizeof(InlineDescriptor))
736 return;
737 getInlineDesc()->LifeState = Lifetime::Ended;
738 }
739
740 void startLifetime() const {
741 if (!isBlockPointer())
742 return;
743 if (BS.Base < sizeof(InlineDescriptor))
744 return;
745 getInlineDesc()->LifeState = Lifetime::Started;
746 }
747
748 /// Compare two pointers.
750 if (!hasSameBase(*this, Other))
752
753 if (Offset < Other.Offset)
755 if (Offset > Other.Offset)
757
759 }
760
761 /// Checks if two pointers are comparable.
762 static bool hasSameBase(const Pointer &A, const Pointer &B);
763 /// Checks if two pointers can be subtracted.
764 static bool hasSameArray(const Pointer &A, const Pointer &B);
765 /// Checks if both given pointers point to the same block.
766 static bool pointToSameBlock(const Pointer &A, const Pointer &B);
767
768 static std::optional<std::pair<Pointer, Pointer>>
769 computeSplitPoint(const Pointer &A, const Pointer &B);
770
771 /// Whether this points to a block that's been created for a "literal lvalue",
772 /// i.e. a non-MaterializeTemporaryExpr Expr.
773 bool pointsToLiteral() const;
774 bool pointsToStringLiteral() const;
775
776 /// Prints the pointer.
777 void print(llvm::raw_ostream &OS) const;
778
779 /// Compute an integer that can be used to compare this pointer to
780 /// another one. This is usually NOT the same as the pointer offset
781 /// regarding the AST record layout.
782 size_t computeOffsetForComparison() const;
783
784private:
785 friend class Block;
786 friend class DeadBlock;
787 friend class MemberPointer;
788 friend class InterpState;
789 friend struct InitMap;
790 friend class DynamicAllocator;
791 friend class Program;
792
793 /// Returns the embedded descriptor preceding a field.
794 InlineDescriptor *getInlineDesc() const {
795 assert(isBlockPointer());
796 assert(BS.Base != sizeof(GlobalInlineDescriptor));
797 assert(BS.Base <= BS.Pointee->getSize());
798 assert(BS.Base >= sizeof(InlineDescriptor));
799 return getDescriptor(BS.Base);
800 }
801
802 /// Returns a descriptor at a given offset.
803 InlineDescriptor *getDescriptor(unsigned Offset) const {
804 assert(Offset != 0 && "Not a nested pointer");
805 assert(isBlockPointer());
806 assert(!isZero());
807 return reinterpret_cast<InlineDescriptor *>(BS.Pointee->rawData() +
808 Offset) -
809 1;
810 }
811
812 /// Returns a reference to the InitMapPtr which stores the initialization map.
813 InitMapPtr &getInitMap() const {
814 assert(isBlockPointer());
815 assert(!isZero());
816 return *reinterpret_cast<InitMapPtr *>(BS.Pointee->rawData() + BS.Base);
817 }
818
819 /// Offset into the storage.
820 uint64_t Offset = 0;
821
822 Storage StorageKind = Storage::Int;
823 union {
828 };
829};
830
831inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Pointer &P) {
832 P.print(OS);
833 return OS;
834}
835
836} // namespace interp
837} // namespace clang
838
839#endif
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
FormatToken * Next
The next token in the unwrapped line.
APValue - This class implements a discriminated union of [uninitialized] [APSInt] [APFloat],...
Definition APValue.h:122
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition ASTContext.h:220
Represents a member of a struct/union/class.
Definition Decl.h:3160
A (possibly-)qualified type.
Definition TypeBase.h:937
Encodes a location in the source.
The base class of the type hierarchy.
Definition TypeBase.h:1833
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
std::byte * rawData()
Returns a pointer to the raw data, including metadata.
Holds all information required to evaluate constexpr code in a module.
Definition Context.h:41
Descriptor for a dead block.
const Function * getFunction() const
Bytecode function.
Definition Function.h:86
const FunctionDecl * getDecl() const
Returns the original FunctionDecl.
Definition Function.h:109
A pointer to a memory block, live or dead.
Definition Pointer.h:91
static bool hasSameBase(const Pointer &A, const Pointer &B)
Checks if two pointers are comparable.
Definition Pointer.cpp:634
Pointer narrow() const
Restricts the scope of an array element pointer.
Definition Pointer.h:188
friend class Program
Definition Pointer.h:791
UnsignedOrNone getDeclID() const
Returns the declaration ID.
Definition Pointer.h:576
void deactivate() const
Deactivates an entire strurcutre.
Definition Pointer.cpp:630
bool isVolatile() const
Checks if an object or a subfield is volatile.
Definition Pointer.h:569
bool isInitialized() const
Checks if an object was initialized.
Definition Pointer.cpp:440
bool isStatic() const
Checks if the storage is static.
Definition Pointer.h:494
bool isDynamic() const
Checks if the storage has been dynamically allocated.
Definition Pointer.h:509
bool inUnion() const
Definition Pointer.h:402
bool isZeroSizeArray() const
Checks if the pointer is pointing to a zero-size array.
Definition Pointer.h:654
bool isElementInitialized(unsigned Index) const
Like isInitialized(), but for primitive arrays.
Definition Pointer.cpp:463
FunctionPointer Fn
Definition Pointer.h:826
Pointer atIndex(uint64_t Idx) const
Offsets a pointer inside an array.
Definition Pointer.h:156
bool isDummy() const
Checks if the pointer points to a dummy value.
Definition Pointer.h:547
Pointer atFieldSub(unsigned Off) const
Subtract the given offset from the current Base and Offset of the pointer.
Definition Pointer.h:181
bool inPrimitiveArray() const
Checks if the structure is a primitive array.
Definition Pointer.h:409
void print(llvm::raw_ostream &OS) const
Prints the pointer.
Definition Pointer.cpp:326
bool isExtern() const
Checks if the storage is extern.
Definition Pointer.h:488
int64_t getIndex() const
Returns the index into an array.
Definition Pointer.h:612
friend class MemberPointer
Definition Pointer.h:787
bool isActive() const
Checks if the object is active.
Definition Pointer.h:536
bool isConst() const
Checks if an object or a subfield is mutable.
Definition Pointer.h:557
Pointer atField(unsigned Off) const
Creates a pointer to a field.
Definition Pointer.h:173
bool isWeak() const
Definition Pointer.h:526
T & deref() const
Dereferences the pointer, if it's live.
Definition Pointer.h:663
Pointer(IntPointer &&IntPtr)
Definition Pointer.h:98
bool isMutable() const
Checks if the field is mutable.
Definition Pointer.h:520
bool isConstInMutable() const
Definition Pointer.h:562
DeclTy getSource() const
Returns the expression or declaration the pointer has been created for.
Definition Pointer.h:295
unsigned getNumElems() const
Returns the number of elements.
Definition Pointer.h:596
Pointer getArray() const
Returns the parent array.
Definition Pointer.h:316
bool isUnknownSizeArray() const
Checks if the structure is an array of unknown size.
Definition Pointer.h:415
void activate() const
Activats a field.
Definition Pointer.cpp:576
static std::optional< std::pair< Pointer, Pointer > > computeSplitPoint(const Pointer &A, const Pointer &B)
Definition Pointer.cpp:686
const TypeidPointer & asTypeidPointer() const
Definition Pointer.h:463
bool isIntegralPointer() const
Definition Pointer.h:469
QualType getType() const
Returns the type of the innermost field.
Definition Pointer.h:336
bool operator==(const Pointer &P) const
Equality operators are just for tests.
Definition Pointer.h:120
bool isArrayElement() const
Checks if the pointer points to an array.
Definition Pointer.h:421
void initializeAllElements() const
Initialize all elements of a primitive array at once.
Definition Pointer.cpp:545
bool pointsToStringLiteral() const
Definition Pointer.cpp:674
bool isArrayRoot() const
Whether this array refers to an array, but not to the first element.
Definition Pointer.h:394
bool isLive() const
Checks if the pointer is live.
Definition Pointer.h:268
bool inArray() const
Checks if the innermost field is an array.
Definition Pointer.h:397
bool isStaticTemporary() const
Checks if the storage is a static temporary.
Definition Pointer.h:517
Pointer(const Type *TypePtr, const Type *TypeInfoType, uint64_t Offset=0)
Definition Pointer.h:108
T & elem(unsigned I) const
Dereferences the element at index I.
Definition Pointer.h:679
bool pointsToLiteral() const
Whether this points to a block that's been created for a "literal lvalue", i.e.
Definition Pointer.cpp:663
Pointer(uint64_t Address, const Descriptor *Desc, uint64_t Offset=0)
Definition Pointer.h:104
Pointer getBase() const
Returns a pointer to the object of which this pointer is a field.
Definition Pointer.h:307
uint64_t getByteOffset() const
Returns the byte offset from the start.
Definition Pointer.h:585
bool isTypeidPointer() const
Definition Pointer.h:471
std::string toDiagnosticString(const ASTContext &Ctx) const
Converts the pointer to a string usable in diagnostics.
Definition Pointer.cpp:427
bool isZero() const
Checks if the pointer is null.
Definition Pointer.h:254
Pointer & operator=(const Pointer &P)
Definition Pointer.cpp:93
ComparisonCategoryResult compare(const Pointer &Other) const
Compare two pointers.
Definition Pointer.h:749
const IntPointer & asIntPointer() const
Definition Pointer.h:455
bool isRoot() const
Pointer points directly to a block.
Definition Pointer.h:437
const Descriptor * getDeclDesc() const
Accessor for information about the declaration site.
Definition Pointer.h:282
const Record * getElemRecord() const
Returns the element record type, if this is a non-primive array.
Definition Pointer.h:476
static bool pointToSameBlock(const Pointer &A, const Pointer &B)
Checks if both given pointers point to the same block.
Definition Pointer.cpp:652
APValue toAPValue(const ASTContext &ASTCtx) const
Converts the pointer to an APValue.
Definition Pointer.cpp:171
unsigned getOffset() const
Returns the offset into an array.
Definition Pointer.h:376
friend class DynamicAllocator
Definition Pointer.h:790
void endLifetime() const
Definition Pointer.h:732
bool isOnePastEnd() const
Checks if the index is one past end.
Definition Pointer.h:629
friend class InterpState
Definition Pointer.h:788
static bool hasSameArray(const Pointer &A, const Pointer &B)
Checks if two pointers can be subtracted.
Definition Pointer.cpp:658
uint64_t getIntegerRepresentation() const
Definition Pointer.h:143
bool isPastEnd() const
Checks if the pointer points past the end of the object.
Definition Pointer.h:643
Pointer(const Function *F, uint64_t Offset=0)
Definition Pointer.h:106
const FieldDecl * getField() const
Returns the field information.
Definition Pointer.h:481
Pointer expand() const
Expands a pointer to the containing array, undoing narrowing.
Definition Pointer.h:221
friend class Block
Definition Pointer.h:785
bool isElementPastEnd() const
Checks if the pointer is an out-of-bounds element pointer.
Definition Pointer.h:651
bool isDereferencable() const
Whether this block can be read from at all.
Definition Pointer.h:697
void startLifetime() const
Definition Pointer.h:740
bool isBlockPointer() const
Definition Pointer.h:468
bool operator!=(const Pointer &P) const
Definition Pointer.h:135
BlockPointer BS
Definition Pointer.h:825
friend struct InitMap
Definition Pointer.h:789
friend class DeadBlock
Definition Pointer.h:786
TypeidPointer Typeid
Definition Pointer.h:827
std::optional< APValue > toRValue(const Context &Ctx, QualType ResultType) const
Converts the pointer to an APValue that is an rvalue.
Definition Pointer.cpp:727
size_t getSize() const
Returns the total size of the innermost field.
Definition Pointer.h:370
bool isTemporary() const
Checks if the storage is temporary.
Definition Pointer.h:501
const FunctionPointer & asFunctionPointer() const
Definition Pointer.h:459
bool allElementsInitialized() const
Definition Pointer.cpp:558
SourceLocation getDeclLoc() const
Definition Pointer.h:292
const Block * block() const
Definition Pointer.h:602
bool isFunctionPointer() const
Definition Pointer.h:470
Pointer getDeclPtr() const
Definition Pointer.h:355
const Descriptor * getFieldDesc() const
Accessors for information about the innermost field.
Definition Pointer.h:326
bool isVirtualBaseClass() const
Definition Pointer.h:543
bool isBaseClass() const
Checks if a structure is a base class.
Definition Pointer.h:542
size_t elemSize() const
Returns the element size of the innermost field.
Definition Pointer.h:358
bool canBeInitialized() const
If this pointer has an InlineDescriptor we can use to initialize.
Definition Pointer.h:444
Lifetime getLifetime() const
Definition Pointer.h:724
size_t computeOffsetForComparison() const
Compute an integer that can be used to compare this pointer to another one.
Definition Pointer.cpp:364
const BlockPointer & asBlockPointer() const
Definition Pointer.h:451
void initialize() const
Initializes a field.
Definition Pointer.cpp:493
const std::byte * getRawAddress() const
If backed by actual data (i.e.
Definition Pointer.h:606
bool isField() const
Checks if the item is a field in an object.
Definition Pointer.h:274
void initializeElement(unsigned Index) const
Initialized the given element of a primitive array.
Definition Pointer.cpp:520
const Record * getRecord() const
Returns the record descriptor of a class.
Definition Pointer.h:474
Structure/Class descriptor.
Definition Record.h:25
llvm::PointerUnion< const Decl *, const Expr * > DeclTy
Definition Descriptor.h:29
llvm::raw_ostream & operator<<(llvm::raw_ostream &OS, const Boolean &B)
Definition Boolean.h:154
std::optional< std::pair< bool, std::shared_ptr< InitMap > > > InitMapPtr
Definition Descriptor.h:30
The JSON file list parser is used to communicate input to InstallAPI.
nullptr
This class represents a compute construct, representing a 'Kind' of ‘parallel’, 'serial',...
ComparisonCategoryResult
An enumeration representing the possible results of a three-way comparison.
const FunctionProtoType * T
@ Other
Other implicit parameter.
Definition Decl.h:1746
__UINTPTR_TYPE__ uintptr_t
An unsigned integer type with the property that any valid pointer to void can be converted to this ty...
#define false
Definition stdbool.h:26
Pointer * Prev
Previous link in the pointer chain.
Definition Pointer.h:41
Pointer * Next
Next link in the pointer chain.
Definition Pointer.h:43
unsigned Base
Start of the current subfield.
Definition Pointer.h:39
Block * Pointee
The block the pointer is pointing to.
Definition Pointer.h:37
Describes a memory block created by an allocation site.
Definition Descriptor.h:122
const bool IsConst
Flag indicating if the block is mutable.
Definition Descriptor.h:161
unsigned getSize() const
Returns the size of the object without metadata.
Definition Descriptor.h:231
QualType getType() const
const DeclTy & getSource() const
Definition Descriptor.h:212
const Decl * asDecl() const
Definition Descriptor.h:210
const Descriptor *const ElemDesc
Descriptor of the array element.
Definition Descriptor.h:155
SourceLocation getLocation() const
bool isUnknownSizeArray() const
Checks if the descriptor is of an array of unknown size.
Definition Descriptor.h:260
unsigned getElemSize() const
returns the size of an element when the structure is viewed as an array.
Definition Descriptor.h:244
const bool IsArray
Flag indicating if the block is an array.
Definition Descriptor.h:168
bool isPrimitiveArray() const
Checks if the descriptor is of an array of primitives.
Definition Descriptor.h:254
const Record *const ElemRecord
Pointer to the record, if block contains records.
Definition Descriptor.h:153
Descriptor used for global variables.
Definition Descriptor.h:51
Inline descriptor embedded in structures and arrays.
Definition Descriptor.h:67
IntPointer baseCast(const ASTContext &ASTCtx, unsigned BaseOffset) const
Definition Pointer.cpp:924
IntPointer atOffset(const ASTContext &ASTCtx, unsigned Offset) const
Definition Pointer.cpp:897
const Descriptor * Desc
Definition Pointer.h:47
const Type * TypeInfoType
Definition Pointer.h:56