clang 24.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 "Block.h"
17#include "Descriptor.h"
18#include "Function.h"
19#include "InitMap.h"
21#include "clang/AST/Decl.h"
22#include "clang/AST/DeclCXX.h"
23#include "clang/AST/Expr.h"
24#include "llvm/Support/raw_ostream.h"
25
26namespace clang {
27namespace interp {
28class Block;
29class DeadBlock;
30class Pointer;
31class Context;
32
33class Pointer;
34inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Pointer &P);
35
36struct PtrView {
37 static constexpr unsigned PastEndMark = ~0u;
38
40 unsigned Base;
41 uint64_t Offset;
42
43 bool isZero() const { return !Pointee; }
44 bool isLive() const { return Pointee && !Pointee->isDead(); }
45 bool isActive() const { return isRoot() || getInlineDesc()->IsActive; }
46 bool isArrayRoot() const { return inArray() && Offset == Base; }
47 bool isElementPastEnd() const { return Offset == PastEndMark; }
48 bool isZeroSizeArray() const { return getFieldDesc()->isZeroSizeArray(); }
49 bool isMutable() const {
50 return !isRoot() && getInlineDesc()->IsFieldMutable;
51 }
52 bool isConstInMutable() const {
54 }
55 bool isExtern() const { return Pointee && Pointee->isExtern(); }
56 bool isVolatile() const {
58 }
59 bool inUnion() const { return getInlineDesc()->InUnion; };
60 bool inArray() const { return getFieldDesc()->IsArray; }
61 bool inPrimitiveArray() const { return getFieldDesc()->isPrimitiveArray(); }
62 const Block *block() const { return Pointee; }
63
64 bool isRoot() const { return Base == 0; }
65
66 bool isConst() const {
68 }
69
72 assert(Base != 0);
73 return getDescriptor(Base);
74 }
75
77 assert(Offset != 0 && "Not a nested pointer");
78 assert(Offset >= sizeof(InlineDescriptor));
79 return reinterpret_cast<InlineDescriptor *>(Pointee->data() + Offset) - 1;
80 }
81
82 const Descriptor *getFieldDesc() const {
83 if (isRoot())
84 return Pointee->getDescriptor();
85 return getInlineDesc()->Desc;
86 }
87
88 const Descriptor *getDeclDesc() const { return Pointee->getDescriptor(); }
89
90 size_t elemSize() const { return getFieldDesc()->getElemSize(); }
91
92 [[nodiscard]] PtrView narrow() const {
93 // Null pointers cannot be narrowed.
94 if (isZero() || isUnknownSizeArray())
95 return *this;
96
97 if (inArray()) {
98 // Pointer is one past end - magic offset marks that.
99 if (isOnePastEnd())
101
102 if (Offset != Base) {
103 // If we're pointing to a primitive array element, there's nothing to
104 // do.
105 if (inPrimitiveArray())
106 return *this;
107 // Pointer is to a composite array element - enter it.
108 return PtrView{Pointee, static_cast<unsigned>(Offset), Offset};
109 }
110 }
111 // Otherwise, we're pointing to a non-array element or
112 // are already narrowed to a composite array element. Nothing to do.
113 return *this;
114 }
115
116 [[nodiscard]] PtrView expand() const {
117 if (isElementPastEnd()) {
118 // Revert to an outer one-past-end pointer.
119 unsigned Adjust;
120 if (inPrimitiveArray())
121 Adjust = sizeof(InitMapPtr);
122 else
123 Adjust = sizeof(InlineDescriptor);
124 return PtrView{Pointee, Base, Base + getSize() + Adjust};
125 }
126
127 // Do not step out of array elements.
128 if (Base != Offset)
129 return *this;
130
131 if (isRoot())
132 return PtrView{Pointee, Base, Base};
133
134 // Step into the containing array, if inside one.
135 unsigned Next = Base - getInlineDesc()->Offset;
136 const Descriptor *Desc =
137 (Next == 0) ? getDeclDesc() : getDescriptor(Next)->Desc;
138 if (!Desc->IsArray)
139 return *this;
140 return PtrView{Pointee, Next, Offset};
141 }
142
143 [[nodiscard]] PtrView stripBaseCasts() const {
144 PtrView V = *this;
145 while (V.isBaseClass())
146 V = V.getBase();
147 return V;
148 }
149
150 [[nodiscard]] PtrView getArray() const {
151 assert(Offset != Base && "not an array element");
152 return PtrView{Pointee, Base, Base};
153 }
154
155 const Record *getRecord() const { return getFieldDesc()->ElemRecord; }
156 const Record *getElemRecord() const {
157 const Descriptor *ElemDesc = getFieldDesc()->ElemDesc;
158 return ElemDesc ? ElemDesc->ElemRecord : nullptr;
159 }
160 const FieldDecl *getField() const { return getFieldDesc()->asFieldDecl(); }
161
162 bool isField() const {
163 return !isZero() && !isRoot() && getFieldDesc()->asDecl();
164 }
165
166 bool isBaseClass() const { return isField() && getInlineDesc()->IsBase; }
167 bool isVirtualBaseClass() const {
168 return isField() && getInlineDesc()->IsVirtualBase;
169 }
170 bool isUnknownSizeArray() const {
172 }
173
174 bool isPastEnd() const { return Offset > Pointee->getSize(); }
175
176 unsigned getOffset() const {
177 assert(Offset != PastEndMark);
178
179 unsigned Adjust = 0;
180 if (Offset != Base) {
181 if (getFieldDesc()->ElemDesc)
182 Adjust = sizeof(InlineDescriptor);
183 else
184 Adjust = sizeof(InitMapPtr);
185 }
186 return Offset - Base - Adjust;
187 }
188 size_t getSize() const { return getFieldDesc()->getSize(); }
189
190 bool isOnePastEnd() const {
191 if (!Pointee)
192 return false;
193
194 const Descriptor *Desc = getFieldDesc();
195 if (Desc->isUnknownSizeArray())
196 return false;
197
198 if (isPastEnd())
199 return true;
200
201 if (Offset != Base) {
202 unsigned Adjust =
203 Desc->ElemDesc ? sizeof(InlineDescriptor) : sizeof(InitMapPtr);
204 unsigned Off = Offset - Base - Adjust;
205 return Desc->getSize() == Off;
206 }
207
208 return Desc->getSize() == 0;
209 }
210
211 PtrView atIndex(unsigned Idx) const {
212 unsigned Off = Idx * elemSize();
213 if (getFieldDesc()->ElemDesc)
214 Off += sizeof(InlineDescriptor);
215 else
216 Off += sizeof(InitMapPtr);
217 return PtrView{Pointee, Base, Base + Off};
218 }
219
220 int64_t getIndex() const {
221 if (isZero())
222 return 0;
223 // narrow()ed element in a composite array.
224 if (Base != 0 && Base == Offset)
225 return 0;
226
227 if (auto ElemSize = elemSize())
228 return getOffset() / ElemSize;
229 return 0;
230 }
231
232 unsigned getNumElems() const { return getSize() / elemSize(); }
233
234 bool isArrayElement() const {
235 if (inArray() && Base != Offset)
236 return true;
237
238 // Might be a narrow()'ed element in a composite array.
239 // Check the inline descriptor.
240 if (Base != 0 && getInlineDesc()->IsArrayElement)
241 return true;
242
243 return false;
244 }
245
246 template <typename T> T &deref() const {
247 assert(isLive() && "Invalid pointer");
248 assert(Pointee);
249 assert(!isArrayRoot());
250 return *reinterpret_cast<T *>(Pointee->data() + Offset);
251 }
252
253 template <typename T> T &elem(unsigned I) const {
254 assert(isLive() && "Invalid pointer");
255 assert(Pointee);
256 assert(getFieldDesc()->isPrimitiveArray());
257 assert(I < getFieldDesc()->getNumElems());
258
259 unsigned ElemByteOffset = I * getFieldDesc()->getElemSize();
260 unsigned ReadOffset = Base + sizeof(InitMapPtr) + ElemByteOffset;
261 assert(ReadOffset + sizeof(T) <= Pointee->getSize());
262
263 return *reinterpret_cast<T *>(Pointee->data() + ReadOffset);
264 }
265
266 [[nodiscard]] PtrView getBase() const {
267 unsigned NewBase = Base - getInlineDesc()->Offset;
268 return PtrView{Pointee, NewBase, NewBase};
269 }
270
271 [[nodiscard]] PtrView atField(unsigned Offset) const {
272 unsigned F = this->Offset + Offset;
273 return PtrView{Pointee, F, F};
274 }
275
277 if (isRoot() && Base == Offset) {
278 // If this pointer points to the root of a declaration, try to consult
279 // the ValueDecl directly, since that has a type with more information,
280 // e.g. the correct ElaboratedTypeKeyword.
281 if (const ValueDecl *VD = getDeclDesc()->asValueDecl())
282 return VD->getType();
283 return getDeclDesc()->getType();
284 }
285 if (inPrimitiveArray() && Offset != Base) {
286 // Unfortunately, complex and vector types are not array types in clang,
287 // but they are for us.
288 if (const auto *AT = getFieldDesc()->getType()->getAsArrayTypeUnsafe())
289 return AT->getElementType();
290 if (const auto *CT = getFieldDesc()->getType()->getAs<ComplexType>())
291 return CT->getElementType();
292 if (const auto *CT = getFieldDesc()->getType()->getAs<VectorType>())
293 return CT->getElementType();
294 }
295
296 return getFieldDesc()->getType();
297 }
298
299 bool isInitialized() const {
300 if (!Pointee->isInitialized())
301 return false;
302
303 if (Offset == 0) {
304 if (Pointee->isGlobal()) {
305 const auto &GD = Pointee->getBlockDesc<GlobalInlineDescriptor>();
307 }
308 if (Pointee->isLocal()) {
309 const auto &LD = Pointee->getBlockDesc<LocalInlineDescriptor>();
310 return LD.IsInitialized;
311 }
312 return true;
313 }
314
315 assert(Pointee && "Cannot check if null pointer was initialized");
316 const Descriptor *Desc = getFieldDesc();
317 assert(Desc);
318 if (Offset != Base && Desc->isPrimitiveArray())
320
321 // Field has its bit in an inline descriptor.
323 }
324
325 void initializeElement(unsigned Index) const;
326 bool allElementsInitialized() const;
327 bool isElementInitialized(unsigned Index) const;
329 return *reinterpret_cast<InitMapPtr *>(Pointee->data() + Base);
330 }
331 void initialize() const;
332 void activate() const;
333
334 void setLifeState(Lifetime L) const;
335 Lifetime getLifetime() const;
338
339 bool operator==(const PtrView &Other) const {
340 return Other.Pointee == Pointee && Base == Other.Base &&
341 Offset == Other.Offset;
342 }
343
344 bool operator!=(const PtrView &Other) const { return !(Other == *this); }
345};
346
348 /// The block the pointer is pointing to.
350 /// Start of the current subfield.
351 unsigned Base;
352 /// Previous link in the pointer chain.
354 /// Next link in the pointer chain.
356};
357
359 llvm::PointerIntPair<const Type *, 1, bool> TypeAndIsNull;
360 uint64_t Value;
361
362 std::optional<IntPointer> atOffset(const Context &Ctx, unsigned Offset) const;
363 IntPointer baseCast(const Context &Ctx, unsigned BaseOffset) const;
364
365 const Type *getType() const { return TypeAndIsNull.getPointer(); }
366 bool isNull() const { return TypeAndIsNull.getInt(); }
367
369 if (!getType())
370 return QualType();
371
372 QualType QT(getType(), 0);
373 if (QT->isPointerOrReferenceType())
374 QT = QT->getPointeeType();
375 else if (QT->isArrayType())
377
378 return QT.IgnoreParens();
379 }
380};
381
384};
385
387 const Type *TypePtr;
389};
390
392 const Expr *Base = nullptr;
393 unsigned ID = 0;
394 bool Decayed = false;
395
396 StringPointer decay() const { return StringPointer{Base, ID, true}; }
397 const StringLiteral *getLiteral() const {
398 if (const auto *PE = dyn_cast<PredefinedExpr>(Base))
399 return PE->getFunctionName();
401 }
402};
403
405 enum { Base, Field, Array, NegativeArray } Kind;
406 union {
407 uint64_t Index;
408 const FieldDecl *FD;
409 llvm::PointerIntPair<const CXXRecordDecl *, 1, bool> RD = {};
410 };
411
412 static PointerPathEntry base(const CXXRecordDecl *RD, bool Virtual = false) {
414 E.Kind = Base;
415 E.RD = {RD, Virtual};
416 return E;
417 }
418
419 static PointerPathEntry array(int64_t Index) {
421 E.Kind = Array;
422 E.Index = Index;
423 return E;
424 }
425
429 E.Index = Index;
430 return E;
431 }
432
435 E.Kind = Field;
436 E.FD = FD;
437 return E;
438 }
439};
440
443 // FieldType and IsOnePastEnd/IsConstexprUnknown bits.
444 llvm::PointerIntPair<const Type *, 2, unsigned> FieldType = {};
445 const PointerPathEntry *Path = nullptr;
446 unsigned PathLength = 0;
447
449 bool hasDeclBase() const { return Base.isDecl(); }
450 const ValueDecl *getBaseDecl() const { return Base.asValueDecl(); }
451 const VarDecl *getBaseVarDecl() const { return Base.asVarDecl(); }
452 const Expr *getBaseExpr() const { return Base.asExpr(); }
453 bool hasValidBase() const;
454
456 withFieldType(const Type *FieldTy,
457 std::optional<bool> PastEnd = std::nullopt) const {
458 unsigned NewBitFieldValue = FieldType.getInt();
459 if (PastEnd)
460 NewBitFieldValue =
461 (isConstexprUnknown() ? 2u : 0u) + static_cast<unsigned>(*PastEnd);
462 return OpaquePointer{Base, {FieldTy, NewBitFieldValue}, Path, PathLength};
463 }
464
466 const Type *FieldTy,
467 std::optional<bool> PastEnd = std::nullopt) const {
468 unsigned NewBitFieldValue = FieldType.getInt();
469 if (PastEnd)
470 NewBitFieldValue =
471 (isConstexprUnknown() ? 2u : 0u) + static_cast<unsigned>(*PastEnd);
472 return OpaquePointer{Base, {FieldTy, NewBitFieldValue}, Path, PathLength};
473 }
474
475 OpaquePointer withPastEnd(bool PastEnd) const {
476 return OpaquePointer{Base,
477 {FieldType.getPointer(),
478 FieldType.getInt() | static_cast<unsigned>(PastEnd)},
479 Path,
480 PathLength};
481 }
482
484 QualType T = Base.getType();
485 if (T->isPointerOrReferenceType())
486 return T->getPointeeType();
487 return T;
488 }
489
491 if (FieldType.getPointer()->isPointerOrReferenceType() && Base.isDecl())
492 return FieldType.getPointer()->getPointeeType();
493 return QualType(FieldType.getPointer(), 0);
494 }
495
496 bool isArrayElement() const {
497 return PathLength != 0 &&
499 }
500
501 std::optional<size_t> computeLayoutOffset(const ASTContext &ASTCtx) const;
502 /// If this is pointing to an array element, return the array.
504
505 bool isOnePastEnd() const { return FieldType.getInt() & 1u; }
506 bool isOnePastEndOrElementPastEnd() const;
507 bool isConstexprUnknown() const { return FieldType.getInt() & 2u; }
508 bool isUnknownSizeArray() const;
509 bool isRoot() const;
510};
511
512enum class Storage { Int, Block, Fn, Typeid, String, Opaque };
513
514/// A pointer to a memory block, live or dead.
515///
516/// This object can be allocated into interpreter stack frames. If pointing to
517/// a live block, it is a link in the chain of pointers pointing to the block.
518///
519/// In the simplest form, a Pointer has a Block* (the pointee) and both Base
520/// and Offset are 0, which means it will point to raw data.
521///
522/// The Base field is used to access metadata about the data. For primitive
523/// arrays, the Base is followed by an InitMap. In a variety of cases, the
524/// Base is preceded by an InlineDescriptor, which is used to track the
525/// initialization state, among other things.
526///
527/// The Offset field is used to access the actual data. In other words, the
528/// data the pointer decribes can be found at
529/// Pointee->data() + Pointer.Offset.
530///
531/// \verbatim
532/// Pointee Offset
533/// │ │
534/// │ │
535/// ▼ ▼
536/// ┌───────┬────────────┬─────────┬────────────────────────────┐
537/// │ Block │ InlineDesc │ InitMap │ Actual Data │
538/// └───────┴────────────┴─────────┴────────────────────────────┘
539/// ▲
540/// │
541/// │
542/// Base
543/// \endverbatim
544class Pointer {
545public:
546 Pointer() : StorageKind(Storage::Int), Int{{nullptr, true}, 0} {}
548 : StorageKind(Storage::Int), Int(std::move(IntPtr)) {}
549 Pointer(Block *B);
550 Pointer(Block *B, uint64_t BaseAndOffset);
551 Pointer(const Pointer &P);
552 Pointer(Pointer &&P);
553 Pointer(uint64_t Address, const Type *Ty, uint64_t Offset = 0,
554 std::optional<bool> IsNull = std::nullopt)
555 : Offset(Offset), StorageKind(Storage::Int),
556 Int{{Ty, IsNull.value_or(Address == 0)}, Address} {}
557 Pointer(const Function *F, uint64_t Offset = 0)
558 : Offset(Offset), StorageKind(Storage::Fn), Fn{F} {}
559 Pointer(const Type *TypePtr, const Type *TypeInfoType, uint64_t Offset = 0)
560 : Offset(Offset), StorageKind(Storage::Typeid) {
561 Typeid.TypePtr = TypePtr;
562 Typeid.TypeInfoType = TypeInfoType;
563 }
564 Pointer(const Expr *Base, unsigned Id)
565 : Offset(0), StorageKind(Storage::String), Str{Base, Id} {}
566 Pointer(StringPointer Str, uint64_t Offset = 0)
567 : Offset(Offset), StorageKind(Storage::String), Str(Str) {}
568
569 Pointer(DeclOrExpr DOE, bool ConstexprUnknown = false)
570 : Offset(0), StorageKind(Storage::Opaque) {
571 Opaque.Base = DOE;
572 Opaque.FieldType = {DOE.getType().getTypePtr(), ConstexprUnknown ? 2u : 0u};
573 Opaque.Path = nullptr;
574 Opaque.PathLength = 0;
575 }
576 Pointer(OpaquePointer OP, uint64_t Offset = 0)
577 : Offset(Offset), StorageKind(Storage::Opaque), Opaque(OP) {}
578
579 Pointer(Block *Pointee, unsigned Base, uint64_t Offset);
580 explicit Pointer(PtrView V) : Pointer(V.Pointee, V.Base, V.Offset) {}
581 ~Pointer();
582
583 Pointer &operator=(const Pointer &P);
584 Pointer &operator=(Pointer &&P);
585
586 bool operator==(const Pointer &P) const;
587 bool operator!=(const Pointer &P) const { return !(P == *this); }
588
589 /// Converts the pointer to an APValue.
590 APValue toAPValue(const ASTContext &ASTCtx) const;
591
592 /// Converts the pointer to a string usable in diagnostics.
593 std::string toDiagnosticString(const ASTContext &Ctx) const;
594
595 uint64_t getIntegerRepresentation() const {
596 if (isIntegralPointer())
597 return Int.Value + (Offset * elemSize());
598 if (isFunctionPointer())
599 return reinterpret_cast<uint64_t>(Fn.Func) + Offset;
600 return reinterpret_cast<uint64_t>(BS.Pointee) + Offset;
601 }
602
603 PtrView view() const {
604 assert(isBlockPointer());
605 return PtrView{BS.Pointee, BS.Base, Offset};
606 }
607
608 /// Converts the pointer to an APValue that is an rvalue.
609 std::optional<APValue> toRValue(const Context &Ctx,
610 QualType ResultType) const;
611
612 /// Offsets a pointer inside an array.
613 [[nodiscard]] Pointer atIndex(uint64_t Idx) const {
614 switch (StorageKind) {
615 case Storage::Int:
616 return Pointer(Int.Value, Int.getType(), Idx);
617 case Storage::Block:
618 return Pointer(view().atIndex(Idx));
619 case Storage::Fn:
620 return Pointer(Fn.Func, Idx);
621 case Storage::String:
622 return Pointer(Str, Idx);
623 default:
624 llvm_unreachable("Unexpected pointer type in atIndex()");
625 }
626 }
627
628 /// Creates a pointer to a field.
629 [[nodiscard]] Pointer atField(unsigned Off) const {
630 return Pointer(view().atField(Off));
631 }
632
633 /// Subtract the given offset from the current Base and Offset
634 /// of the pointer.
635 [[nodiscard]] Pointer atFieldSub(unsigned Off) const {
636 assert(Offset >= Off);
637 unsigned O = Offset - Off;
638 return Pointer(BS.Pointee, O, O);
639 }
640
641 /// Restricts the scope of an array element pointer.
642 [[nodiscard]] Pointer narrow() const {
643 if (!isBlockPointer())
644 return *this;
645 return Pointer(view().narrow());
646 }
647
648 /// Expands a pointer to the containing array, undoing narrowing.
649 [[nodiscard]] Pointer expand() const {
650 if (!isBlockPointer())
651 return *this;
652 return Pointer(view().expand());
653 }
654
655 /// Checks if the pointer is null.
656 bool isZero() const {
657 switch (StorageKind) {
658 case Storage::Int:
659 return Int.isNull();
660 case Storage::Block:
661 return BS.Pointee == nullptr;
662 case Storage::Fn:
663 return !Fn.Func;
664 case Storage::Typeid:
665 case Storage::String:
666 case Storage::Opaque:
667 return false;
668 }
669 llvm_unreachable("Unknown clang::interp::Storage enum");
670 }
671 /// Checks if the pointer is live.
672 bool isLive() const {
673 if (!isBlockPointer())
674 return true;
675 return view().isLive();
676 }
677 /// Checks if the item is a field in an object.
678 bool isField() const {
679 if (!isBlockPointer())
680 return false;
681
682 return view().isField();
683 }
684
685 /// Accessor for information about the declaration site.
686 const Descriptor *getDeclDesc() const {
687 if (!isBlockPointer())
688 return nullptr;
689
690 assert(isBlockPointer());
691 assert(BS.Pointee);
692 return BS.Pointee->Desc;
693 }
694 SourceLocation getDeclLoc() const { return getDeclDesc()->getLocation(); }
695
696 /// Returns the expression or declaration the pointer has been created for.
698 if (isBlockPointer())
699 return getDeclDesc()->getSource();
700 if (isFunctionPointer()) {
701 const Function *F = Fn.Func;
702 return F ? F->getDecl() : DeclOrExpr();
703 }
704 llvm_unreachable("Unsupported pointer type in getSource()");
705 return DeclOrExpr();
706 }
707
708 /// Returns a pointer to the object of which this pointer is a field.
709 [[nodiscard]] Pointer getBase() const { return Pointer(view().getBase()); }
710 /// Returns the parent array.
711 [[nodiscard]] Pointer getArray() const { return Pointer(view().getArray()); }
712
713 /// Accessors for information about the innermost field.
714 const Descriptor *getFieldDesc() const {
715 if (!isBlockPointer())
716 return nullptr;
717
718 if (isRoot())
719 return getDeclDesc();
720 return getInlineDesc()->Desc;
721 }
722
723 /// Returns the type of the innermost field.
725 switch (StorageKind) {
726 case Storage::Int:
727 return Int.getPointeeType();
728 case Storage::Block:
729 return view().getType();
730 case Storage::Fn:
731 return Fn.Func->getType();
732 case Storage::Typeid:
733 return QualType(Typeid.TypeInfoType, 0);
734 case Storage::String:
735 if (Str.Decayed)
736 return Str.getLiteral()
737 ->getType()
738 ->getAsArrayTypeUnsafe()
739 ->getElementType();
740 return Str.getLiteral()->getType();
741 case Storage::Opaque:
742 return Opaque.getFieldType();
743 }
744 llvm_unreachable("Unhandled StorageKind");
745 }
746
747 const VarDecl *getRootVarDecl() const;
748 const ValueDecl *getRootValueDecl() const;
749 const Expr *getRootExpr() const;
750
751 [[nodiscard]] Pointer getDeclPtr() const { return Pointer(BS.Pointee); }
752
753 /// Returns the element size of the innermost field.
754 size_t elemSize() const {
755 if (isIntegralPointer()) {
756 // FIXME: Remove this and handle int ptrs specially?
757 return 1;
758 }
759 if (isStringPointer())
760 return Str.getLiteral()->getCharByteWidth();
761
762 return view().elemSize();
763 }
764 /// Returns the total size of the innermost field.
765 size_t getSize() const {
766 assert(isBlockPointer());
767 return getFieldDesc()->getSize();
768 }
769
770 /// Returns the offset into an array.
771 unsigned getOffset() const {
772 assert(Offset != PtrView::PastEndMark && "invalid offset");
773 return view().getOffset();
774 }
775
776 /// Whether this array refers to an array, but not
777 /// to the first element.
778 bool isArrayRoot() const {
779 switch (StorageKind) {
780 case Storage::Block:
781 return view().isArrayRoot();
782 case Storage::String:
783 return Offset == 0;
784 case Storage::Opaque:
785 return asOpaquePointer().getFieldType()->isArrayType();
786 default:
787 return false;
788 }
789 llvm_unreachable("Unhandled storage kind");
790 }
791
792 /// Checks if the innermost field is an array.
793 bool inArray() const {
794 switch (StorageKind) {
795 case Storage::Block:
796 return view().inArray();
797 case Storage::String:
798 return true;
799 case Storage::Opaque:
800 return asOpaquePointer().isArrayElement();
801 default:
802 return false;
803 }
804
805 llvm_unreachable("Unhandled storage kind");
806 }
807 bool inUnion() const {
808 if (isBlockPointer() && BS.Base != 0)
809 return view().inUnion();
810 return false;
811 };
812
813 /// Checks if the structure is a primitive array.
814 bool inPrimitiveArray() const {
815 if (isBlockPointer())
816 return view().inPrimitiveArray();
817 return false;
818 }
819 /// Checks if the structure is an array of unknown size.
820 bool isUnknownSizeArray() const {
821 if (isBlockPointer())
822 return getFieldDesc()->isUnknownSizeArray();
823 if (isOpaquePointer())
824 return Opaque.isUnknownSizeArray();
825 return false;
826 }
827 /// Checks if the pointer points to an array.
828 bool isArrayElement() const {
829 if (!isBlockPointer())
830 return false;
831
832 return view().isArrayElement();
833 }
834 /// Pointer points directly to a block.
835 bool isRoot() const {
836 if (isZero())
837 return true;
838 if (isBlockPointer())
839 return view().isRoot();
840 if (isOpaquePointer())
841 return Opaque.isRoot();
842 return true;
843 }
844
845 [[nodiscard]] const BlockPointer &asBlockPointer() const {
846 assert(isBlockPointer());
847 return BS;
848 }
849 [[nodiscard]] const IntPointer &asIntPointer() const {
850 assert(isIntegralPointer());
851 return Int;
852 }
853 [[nodiscard]] const FunctionPointer &asFunctionPointer() const {
854 assert(isFunctionPointer());
855 return Fn;
856 }
857 [[nodiscard]] const TypeidPointer &asTypeidPointer() const {
858 assert(isTypeidPointer());
859 return Typeid;
860 }
861 [[nodiscard]] const StringPointer &asStringPointer() const {
862 assert(isStringPointer());
863 return Str;
864 }
865 [[nodiscard]] const OpaquePointer &asOpaquePointer() const {
866 assert(isOpaquePointer());
867 return Opaque;
868 }
869
870 bool isBlockPointer() const { return StorageKind == Storage::Block; }
871 bool isIntegralPointer() const { return StorageKind == Storage::Int; }
872 bool isFunctionPointer() const { return StorageKind == Storage::Fn; }
873 bool isTypeidPointer() const { return StorageKind == Storage::Typeid; }
874 bool isStringPointer() const { return StorageKind == Storage::String; }
875 bool isOpaquePointer() const { return StorageKind == Storage::Opaque; }
876
877 /// Returns the record descriptor of a class.
878 const Record *getRecord() const {
879 if (!isBlockPointer())
880 return nullptr;
881 return view().getRecord();
882 }
883 /// Returns the element record type, if this is a non-primive array.
884 const Record *getElemRecord() const { return view().getElemRecord(); }
885 /// Returns the field information.
886 const FieldDecl *getField() const {
887 if (const Descriptor *FD = getFieldDesc())
888 return FD->asFieldDecl();
889 return nullptr;
890 }
891
892 /// Checks if the storage is extern.
893 bool isExtern() const {
894 if (isBlockPointer())
895 return view().isExtern();
896 return false;
897 }
898 /// Checks if the storage is static.
899 bool isStatic() const {
900 if (!isBlockPointer())
901 return true;
902 assert(BS.Pointee);
903 return BS.Pointee->isGlobal();
904 }
905 /// Checks if the storage is temporary.
906 bool isTemporary() const {
907 if (isBlockPointer()) {
908 assert(BS.Pointee);
909 return BS.Pointee->isTemporary();
910 }
911 return false;
912 }
913 /// Checks if the storage has been dynamically allocated.
914 bool isDynamic() const {
915 if (isBlockPointer()) {
916 assert(BS.Pointee);
917 return BS.Pointee->isDynamic();
918 }
919 return false;
920 }
921 /// Checks if the storage is a static temporary.
922 bool isStaticTemporary() const { return isStatic() && isTemporary(); }
923
924 /// Checks if the field is mutable.
925 bool isMutable() const {
926 if (!isBlockPointer())
927 return false;
928 return view().isMutable();
929 }
930
931 bool isWeak() const {
932 switch (StorageKind) {
933 case Storage::Fn:
934 if (!Fn.Func || !Fn.Func->getDecl())
935 return false;
936 return Fn.Func->getDecl()->isWeak();
937 case Storage::Opaque:
938 if (const VarDecl *BaseDecl = Opaque.getBaseVarDecl())
939 return BaseDecl->isWeak();
940 return false;
941 case Storage::Block:
942 return BS.Pointee->isWeak();
943 default:
944 return false;
945 }
946 llvm_unreachable("Unhandled storage kind");
947 }
948 /// Checks if the object is active.
949 bool isActive() const {
950 if (!isBlockPointer())
951 return true;
952 return view().isActive();
953 }
954 /// Checks if a structure is a base class.
955 bool isBaseClass() const { return view().isBaseClass(); }
956 bool isVirtualBaseClass() const { return view().isVirtualBaseClass(); }
957
958 /// Checks if the pointer points to a dummy value.
959 bool isDummy() const { return isOpaquePointer(); }
960
961 /// Checks if an object or a subfield is mutable.
962 bool isConst() const {
963 if (isIntegralPointer())
964 return true;
965 if (isStringPointer())
966 return true;
967 if (!isBlockPointer())
968 return false;
969 return view().isConst();
970 }
971
972 /// Checks if an object or a subfield is volatile.
973 bool isVolatile() const {
974 if (!isBlockPointer())
975 return false;
976 return view().isVolatile();
977 }
978
979 /// Returns the declaration ID.
981 if (isBlockPointer()) {
982 assert(BS.Pointee);
983 return BS.Pointee->getDeclID();
984 }
985 return std::nullopt;
986 }
987
988 /// Returns the byte offset from the start.
989 uint64_t getByteOffset() const {
990 if (isBlockPointer())
991 return isOnePastEnd() ? PtrView::PastEndMark : Offset;
992 return Offset;
993 }
994
995 /// Returns the number of elements.
996 unsigned getNumElems() const {
997 if (isStringPointer())
998 return Str.getLiteral()->getLength() + 1;
999 if (isOpaquePointer()) {
1000 const ArrayType *AT =
1001 Opaque.getSurroundingArray()->getAsArrayTypeUnsafe();
1002 if (const auto *CAT = dyn_cast_if_present<ConstantArrayType>(AT))
1003 return CAT->getZExtSize();
1004 }
1005 if (!isBlockPointer())
1006 return ~0u;
1007 return view().getNumElems();
1008 }
1009
1010 const Block *block() const {
1011 assert(isBlockPointer());
1012 return BS.Pointee;
1013 }
1014
1015 /// If backed by actual data (i.e. a block or string pointer), return
1016 /// an address to that data.
1017 const std::byte *getRawAddress() const {
1018 if (isStringPointer()) {
1019 const StringLiteral *Lit = Str.getLiteral();
1020 return reinterpret_cast<const std::byte *>(
1021 Lit->getBytes().data() + (Offset * Lit->getCharByteWidth()));
1022 }
1023 assert(isBlockPointer());
1024 return BS.Pointee->data() + Offset;
1025 }
1026
1027 /// Returns the index into an array.
1028 int64_t getIndex() const {
1029 if (isStringPointer())
1030 return Offset;
1031 if (isOpaquePointer()) {
1032 if (Opaque.isArrayElement())
1033 return Opaque.Path[Opaque.PathLength - 1].Index;
1034 return 0;
1035 }
1036 if (!isBlockPointer())
1037 return getIntegerRepresentation();
1038
1039 return view().getIndex();
1040 }
1041
1042 /// Checks if the index is one past end.
1043 bool isOnePastEnd() const {
1044 if (isStringPointer())
1045 return Offset == (Str.getLiteral()->getLength() + 1);
1046 if (isOpaquePointer())
1047 return Opaque.isOnePastEndOrElementPastEnd();
1048
1049 if (!isBlockPointer())
1050 return false;
1051
1052 if (!BS.Pointee)
1053 return false;
1054
1055 return view().isOnePastEnd();
1056 }
1057
1058 /// Checks if the pointer points past the end of the object.
1059 bool isPastEnd() const {
1060 if (isIntegralPointer())
1061 return false;
1062 if (isStringPointer())
1063 return Offset >= (Str.getLiteral()->getLength() + 1);
1064
1065 return !isZero() && Offset > BS.Pointee->getSize();
1066 }
1067
1068 /// Checks if the pointer is an out-of-bounds element pointer.
1069 bool isElementPastEnd() const { return Offset == PtrView::PastEndMark; }
1070
1071 /// Checks if the pointer is pointing to a zero-size array.
1072 bool isZeroSizeArray() const {
1073 if (isFunctionPointer())
1074 return false;
1075 if (isOpaquePointer())
1076 return false; // FIXME: Can actually happen I think?
1077 if (const auto *Desc = getFieldDesc())
1078 return Desc->isZeroSizeArray();
1079 return false;
1080 }
1081
1082 /// Checks whether the pointer can be dereferenced to the given PrimType.
1083 bool canDeref(PrimType T) const {
1084 if (isStringPointer()) {
1085 switch (Str.getLiteral()->getCharByteWidth()) {
1086 case 1:
1087 return T == PT_Sint8 || T == PT_Uint8;
1088 case 2:
1089 return T == PT_Sint16 || T == PT_Uint16;
1090 case 4:
1091 return T == PT_Sint32 || T == PT_Uint32;
1092 }
1093
1094 return false;
1095 }
1096
1097 assert(isBlockPointer());
1098 if (const Descriptor *FieldDesc = getFieldDesc()) {
1099 return (FieldDesc->isPrimitive() || FieldDesc->isPrimitiveArray()) &&
1100 FieldDesc->getPrimType() == T;
1101 }
1102 return false;
1103 }
1104
1105 /// Dereferences the pointer, if it's live.
1106 template <typename T> T &deref() const {
1107 assert(isLive() && "Invalid pointer");
1108 assert(isBlockPointer());
1109 assert(BS.Pointee);
1110 assert(isDereferencable());
1111 assert(Offset + sizeof(T) <= BS.Pointee->getSize());
1112 return view().deref<T>();
1113 }
1114
1115 template <typename T> T load() const {
1116 assert(isLive() && "Invalid pointer");
1117 if (isBlockPointer()) {
1118 assert(BS.Pointee);
1119 assert(isDereferencable());
1120 assert(Offset + sizeof(T) <= BS.Pointee->getSize());
1121 return view().deref<T>();
1122 }
1123
1124 if (isStringPointer()) {
1125 const StringLiteral *Lit = Str.getLiteral();
1126
1127 if constexpr (isFixedSizeIntegralType<T>()) {
1128 // The literal does not include the nul byte.
1129 if (Offset >= Lit->getLength())
1130 return T::from('\0');
1131 return T::from(Lit->getCodeUnit(Offset));
1132 } else if constexpr (std::is_integral_v<T>) {
1133 if (Offset >= Lit->getLength())
1134 return '\0';
1135 return Lit->getCodeUnit(Offset);
1136 }
1137 }
1138
1139 llvm_unreachable("Unexpected pointer type in load()");
1140 }
1141
1142 /// Dereferences the element at index \p I.
1143 /// This is equivalent to atIndex(I).deref<T>().
1144 template <typename T> T &elem(unsigned I) const {
1145 assert(isLive() && "Invalid pointer");
1146 assert(isBlockPointer());
1147 assert(BS.Pointee);
1148 assert(isDereferencable());
1149 assert(getFieldDesc()->isPrimitiveArray());
1150 assert(I < getFieldDesc()->getNumElems());
1151
1152 return view().elem<T>(I);
1153 }
1154
1155 template <typename T> T loadElem(unsigned I) const {
1156 assert(isLive() && "Invalid pointer");
1157 if (isBlockPointer()) {
1158 assert(BS.Pointee);
1159 assert(isDereferencable());
1160 assert(getFieldDesc()->isPrimitiveArray());
1161 assert(I < getFieldDesc()->getNumElems());
1162
1163 return view().elem<T>(I);
1164 }
1165
1166 assert(isStringPointer());
1167 const StringLiteral *Lit = Str.getLiteral();
1168 unsigned Index = Offset + I;
1169 if constexpr (isFixedSizeIntegralType<T>()) {
1170 // The literal does not include the nul byte.
1171 if (Index >= Lit->getLength())
1172 return T::from('\0');
1173 return T::from(Lit->getCodeUnit(Index));
1174 } else if constexpr (std::is_integral_v<T>) {
1175 if (Index >= Lit->getLength())
1176 return '\0';
1177 return Lit->getCodeUnit(Index);
1178 }
1179 llvm_unreachable("Unexpected pointer type in loadElem()");
1180 }
1181
1182 bool isConstexprUnknown() const {
1183 if (isOpaquePointer())
1184 return Opaque.isConstexprUnknown();
1185 if (isBlockPointer())
1186 return block()->isConstexprUnknown();
1187 return false;
1188 }
1189
1190 /// Whether this block can be read from at all. This is only true for
1191 /// block pointers that point to a valid location inside that block.
1192 bool isDereferencable() const {
1193 if (!isBlockPointer())
1194 return false;
1195 if (isDummy())
1196 return false;
1197 if (isConstexprUnknown())
1198 return false;
1199 if (isPastEnd())
1200 return false;
1201
1202 return true;
1203 }
1204
1206 return StorageKind == Storage::Block || StorageKind == Storage::String;
1207 }
1208
1209 /// Initializes a field.
1210 void initialize() const {
1211 if (!isBlockPointer())
1212 return;
1213 view().initialize();
1214 }
1215 /// Initialized the given element of a primitive array.
1216 void initializeElement(unsigned Index) const {
1217 view().initializeElement(Index);
1218 }
1219 /// Initialize all elements of a primitive array at once. This can be
1220 /// used in situations where we *know* we have initialized *all* elements
1221 /// of a primtive array.
1222 void initializeAllElements() const;
1223 /// Checks if an object was initialized.
1224 bool isInitialized() const {
1225 if (!isBlockPointer())
1226 return true;
1227
1228 return view().isInitialized();
1229 }
1230
1231 /// Like isInitialized(), but for primitive arrays.
1232 bool isElementInitialized(unsigned Index) const {
1233 if (!isBlockPointer())
1234 return true;
1235
1236 return view().isElementInitialized(Index);
1237 }
1239 assert(getFieldDesc()->isPrimitiveArray());
1240 assert(isArrayRoot());
1241 return view().allElementsInitialized();
1242 }
1243 bool allElementsAlive() const;
1244 bool isElementAlive(unsigned Index) const;
1245
1246 /// Activates a field.
1247 void activate() const {
1248 if (!isBlockPointer())
1249 return;
1250 view().activate();
1251 }
1252 /// Deactivates an entire strurcutre.
1253 void deactivate() const {
1254 // TODO: this only appears in constructors, so nothing to deactivate.
1255 }
1256
1258 if (!isBlockPointer())
1259 return Lifetime::Started;
1260 return view().getLifetime();
1261 }
1262
1263 /// Start the lifetime of this pointer. This works for pointer with an
1264 /// InlineDescriptor as well as primitive array elements. Pointers are usually
1265 /// alive by default, unless the underlying object has been allocated with
1266 /// std::allocator. This function is used by std::construct_at.
1268 /// Ends the lifetime of the pointer. This works for pointer with an
1269 /// InlineDescriptor as well as primitive array elements. This function is
1270 /// used by std::destroy_at.
1272
1273 void setLifeState(Lifetime L) const {
1274 if (!isBlockPointer())
1275 return;
1276 view().setLifeState(L);
1277 };
1278
1279 /// Strip base casts from this Pointer.
1280 /// The result is either a root pointer or something
1281 /// that isn't a base class anymore.
1282 [[nodiscard]] Pointer stripBaseCasts() const {
1283 return Pointer(view().stripBaseCasts());
1284 }
1285 /// Checks if two pointers are comparable.
1286 static bool hasSameBase(const Pointer &A, const Pointer &B);
1287 /// Checks if two pointers can be subtracted.
1288 static bool elemsOfSameArray(const Pointer &A, const Pointer &B);
1289 /// Checks if both given pointers point to the same block.
1290 static bool pointToSameBlock(const Pointer &A, const Pointer &B);
1291
1292 static std::optional<std::pair<PtrView, PtrView>>
1293 computeSplitPoint(const Pointer &A, const Pointer &B);
1294
1295 /// Whether this points to a block that's been created for a "literal lvalue",
1296 /// i.e. a non-MaterializeTemporaryExpr Expr.
1297 bool pointsToLiteral() const;
1298 /// Whether this points to a block created for an AddrLabelExpr.
1299 bool pointsToLabel() const;
1300 /// Returns the AddrLabelExpr the Pointer points to, if any.
1302 return dyn_cast_if_present<AddrLabelExpr>(getRootExpr());
1303 }
1304
1305 /// Prints the pointer.
1306 void print(llvm::raw_ostream &OS) const;
1307
1308 /// Compute an integer that can be used to compare this pointer to
1309 /// another one. This is usually NOT the same as the pointer offset
1310 /// regarding the AST record layout.
1311 std::optional<size_t>
1312 computeOffsetForComparison(const ASTContext &ASTCtx) const;
1313 /// Compute the pointer offset as given by the ASTRecordLayout.
1314 /// Returns the result in bytes.
1315 std::optional<size_t> computeLayoutOffset(const ASTContext &ASTCtx) const;
1316
1317private:
1318 friend class Block;
1319 friend class DeadBlock;
1320 friend class MemberPointer;
1321 friend class InterpState;
1322 friend class DynamicAllocator;
1323 friend class Program;
1324
1325 /// Returns the embedded descriptor preceding a field.
1326 InlineDescriptor *getInlineDesc() const {
1327 assert(isBlockPointer());
1328 assert(BS.Base <= BS.Pointee->getSize());
1329 assert(BS.Base != 0);
1330 return getDescriptor(BS.Base);
1331 }
1332
1333 /// Returns a descriptor at a given offset.
1334 InlineDescriptor *getDescriptor(unsigned Offset) const {
1335 assert(Offset != 0 && "Not a nested pointer");
1336 assert(isBlockPointer());
1337 assert(!isZero());
1338 return view().getDescriptor(Offset);
1339 }
1340
1341 /// Returns a reference to the InitMapPtr which stores the initialization map.
1342 InitMapPtr &getInitMap() const {
1343 assert(isBlockPointer());
1344 assert(!isZero());
1345 return view().getInitMap();
1346 }
1347
1348 /// Offset into the storage.
1349 uint64_t Offset = 0;
1350
1351 Storage StorageKind = Storage::Int;
1352 union {
1359 };
1360};
1361
1362inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Pointer &P) {
1363 P.print(OS);
1364 OS << ' ';
1365 if (P.isZero())
1366 return OS;
1367
1368 if (const Descriptor *D = P.getFieldDesc())
1369 D->dump(OS);
1370 if (P.isArrayElement()) {
1371 if (P.isOnePastEnd())
1372 OS << " one-past-the-end";
1373 else {
1374 OS << ' ';
1375 std::string Indices;
1376 llvm::raw_string_ostream SS(Indices);
1377 Pointer K = P;
1378 while (K.isArrayElement()) {
1379 SS << ']' << K.expand().getIndex() << '[';
1380 K = K.expand().getArray();
1381 }
1382 std::reverse(Indices.begin(), Indices.end());
1383 OS << Indices;
1384 }
1385 } else if (P.isBlockPointer() && P.isArrayRoot())
1386 OS << " arrayroot";
1387
1388 if (P.isDummy())
1389 OS << " dummy";
1390 if (!P.isLive())
1391 OS << " dead";
1392 if (P.isBlockPointer() && P.isBaseClass())
1393 OS << " base-class";
1394 if (!P.isInitialized()) {
1395 ColorScope SC(OS, true, {llvm::raw_ostream::BRIGHT_RED, true});
1396 OS << " uninitialized";
1397 }
1398 return OS;
1399}
1400
1401} // namespace interp
1402} // namespace clang
1403
1404#endif
#define V(N, I)
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
FormatToken * Next
The next token in the unwrapped line.
static void print(llvm::raw_ostream &OS, const T &V, const Context &Ctx, QualType Ty)
static bool toRValue(const Context &Ctx, QualType Ty, PtrView Ptr, APValue &R)
Convert a pointer to a composite value to an rvalue.
Definition Pointer.cpp:1112
APValue - This class implements a discriminated union of [uninitialized] [APSInt] [APFloat],...
Definition APValue.h:158
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition ASTContext.h:245
AddrLabelExpr - The GNU address of label extension, representing &&label.
Definition Expr.h:4619
Represents an array type, per C99 6.7.5.2 - Array Declarators.
Definition TypeBase.h:3820
QualType getElementType() const
Definition TypeBase.h:3832
Represents a C++ struct/union/class.
Definition DeclCXX.h:258
This represents one expression.
Definition Expr.h:114
Represents a member of a struct/union/class.
Definition Decl.h:3341
A (possibly-)qualified type.
Definition TypeBase.h:938
QualType IgnoreParens() const
Returns the specified type after dropping any outer-level parentheses.
Definition TypeBase.h:1331
const Type * getTypePtr() const
Retrieves a pointer to the underlying (unqualified) type.
Definition TypeBase.h:8578
Encodes a location in the source.
StringLiteral - This represents a string literal expression, e.g.
Definition Expr.h:1844
unsigned getLength() const
Definition Expr.h:1969
uint32_t getCodeUnit(size_t I) const
Return the code unit at the given position.
Definition Expr.h:1931
StringRef getBytes() const
Allow access to clients that need the byte representation, such as ASTWriterStmt::VisitStringLiteral(...
Definition Expr.h:1920
unsigned getCharByteWidth() const
Definition Expr.h:1971
The base class of the type hierarchy.
Definition TypeBase.h:1879
bool isArrayType() const
Definition TypeBase.h:8914
QualType getPointeeType() const
If this is a pointer, ObjC object pointer, or block pointer, this returns the respective pointee.
Definition Type.cpp:885
const ArrayType * getAsArrayTypeUnsafe() const
A variant of getAs<> for array types which silently discards qualifiers from the outermost type.
Definition TypeBase.h:9484
bool isPointerOrReferenceType() const
Definition TypeBase.h:8819
Represent the declaration of a variable (in which case it is an lvalue) a function (in which case it ...
Definition Decl.h:714
Represents a variable declaration or definition.
Definition Decl.h:934
A memory block, either on the stack or in the heap.
Definition Block.h:68
Holds all information required to evaluate constexpr code in a module.
Definition Context.h:49
Descriptor for a dead block.
Definition Block.h:249
Bytecode function.
Definition Function.h:98
const FunctionDecl * getDecl() const
Returns the original FunctionDecl.
Definition Function.h:133
A pointer to a memory block, live or dead.
Definition Pointer.h:544
Pointer narrow() const
Restricts the scope of an array element pointer.
Definition Pointer.h:642
friend class Program
Definition Pointer.h:1323
OpaquePointer Opaque
Definition Pointer.h:1358
UnsignedOrNone getDeclID() const
Returns the declaration ID.
Definition Pointer.h:980
Pointer stripBaseCasts() const
Strip base casts from this Pointer.
Definition Pointer.h:1282
const Expr * getRootExpr() const
Definition Pointer.cpp:1324
bool isVolatile() const
Checks if an object or a subfield is volatile.
Definition Pointer.h:973
bool isStatic() const
Checks if the storage is static.
Definition Pointer.h:899
bool isDynamic() const
Checks if the storage has been dynamically allocated.
Definition Pointer.h:914
bool inUnion() const
Definition Pointer.h:807
bool isZeroSizeArray() const
Checks if the pointer is pointing to a zero-size array.
Definition Pointer.h:1072
FunctionPointer Fn
Definition Pointer.h:1355
bool allElementsInitialized() const
Definition Pointer.h:1238
T loadElem(unsigned I) const
Definition Pointer.h:1155
Pointer atIndex(uint64_t Idx) const
Offsets a pointer inside an array.
Definition Pointer.h:613
bool isDummy() const
Checks if the pointer points to a dummy value.
Definition Pointer.h:959
const AddrLabelExpr * getPointedToLabel() const
Returns the AddrLabelExpr the Pointer points to, if any.
Definition Pointer.h:1301
Pointer atFieldSub(unsigned Off) const
Subtract the given offset from the current Base and Offset of the pointer.
Definition Pointer.h:635
bool inPrimitiveArray() const
Checks if the structure is a primitive array.
Definition Pointer.h:814
void print(llvm::raw_ostream &OS) const
Prints the pointer.
Definition Pointer.cpp:466
bool isExtern() const
Checks if the storage is extern.
Definition Pointer.h:893
int64_t getIndex() const
Returns the index into an array.
Definition Pointer.h:1028
friend class MemberPointer
Definition Pointer.h:1320
bool isOpaquePointer() const
Definition Pointer.h:875
bool isActive() const
Checks if the object is active.
Definition Pointer.h:949
bool isStringPointer() const
Definition Pointer.h:874
bool canDeref(PrimType T) const
Checks whether the pointer can be dereferenced to the given PrimType.
Definition Pointer.h:1083
bool isConst() const
Checks if an object or a subfield is mutable.
Definition Pointer.h:962
DeclOrExpr getSource() const
Returns the expression or declaration the pointer has been created for.
Definition Pointer.h:697
Pointer atField(unsigned Off) const
Creates a pointer to a field.
Definition Pointer.h:629
bool isWeak() const
Definition Pointer.h:931
T & deref() const
Dereferences the pointer, if it's live.
Definition Pointer.h:1106
Pointer(IntPointer &&IntPtr)
Definition Pointer.h:547
bool isMutable() const
Checks if the field is mutable.
Definition Pointer.h:925
unsigned getNumElems() const
Returns the number of elements.
Definition Pointer.h:996
Pointer getArray() const
Returns the parent array.
Definition Pointer.h:711
bool isUnknownSizeArray() const
Checks if the structure is an array of unknown size.
Definition Pointer.h:820
const TypeidPointer & asTypeidPointer() const
Definition Pointer.h:857
bool isIntegralPointer() const
Definition Pointer.h:871
QualType getType() const
Returns the type of the innermost field.
Definition Pointer.h:724
bool isArrayElement() const
Checks if the pointer points to an array.
Definition Pointer.h:828
void initialize() const
Initializes a field.
Definition Pointer.h:1210
Pointer(uint64_t Address, const Type *Ty, uint64_t Offset=0, std::optional< bool > IsNull=std::nullopt)
Definition Pointer.h:553
bool isArrayRoot() const
Whether this array refers to an array, but not to the first element.
Definition Pointer.h:778
bool isLive() const
Checks if the pointer is live.
Definition Pointer.h:672
bool inArray() const
Checks if the innermost field is an array.
Definition Pointer.h:793
const StringPointer & asStringPointer() const
Definition Pointer.h:861
bool isStaticTemporary() const
Checks if the storage is a static temporary.
Definition Pointer.h:922
Pointer(const Type *TypePtr, const Type *TypeInfoType, uint64_t Offset=0)
Definition Pointer.h:559
T & elem(unsigned I) const
Dereferences the element at index I.
Definition Pointer.h:1144
Pointer getBase() const
Returns a pointer to the object of which this pointer is a field.
Definition Pointer.h:709
uint64_t getByteOffset() const
Returns the byte offset from the start.
Definition Pointer.h:989
bool isTypeidPointer() const
Definition Pointer.h:873
bool isZero() const
Checks if the pointer is null.
Definition Pointer.h:656
bool isConstexprUnknown() const
Definition Pointer.h:1182
const IntPointer & asIntPointer() const
Definition Pointer.h:849
bool isRoot() const
Pointer points directly to a block.
Definition Pointer.h:835
const Descriptor * getDeclDesc() const
Accessor for information about the declaration site.
Definition Pointer.h:686
void activate() const
Activates a field.
Definition Pointer.h:1247
const Record * getElemRecord() const
Returns the element record type, if this is a non-primive array.
Definition Pointer.h:884
const OpaquePointer & asOpaquePointer() const
Definition Pointer.h:865
unsigned getOffset() const
Returns the offset into an array.
Definition Pointer.h:771
friend class DynamicAllocator
Definition Pointer.h:1322
void endLifetime() const
Ends the lifetime of the pointer.
Definition Pointer.h:1271
void setLifeState(Lifetime L) const
Definition Pointer.h:1273
bool isOnePastEnd() const
Checks if the index is one past end.
Definition Pointer.h:1043
friend class InterpState
Definition Pointer.h:1321
uint64_t getIntegerRepresentation() const
Definition Pointer.h:595
bool isPastEnd() const
Checks if the pointer points past the end of the object.
Definition Pointer.h:1059
Pointer(const Function *F, uint64_t Offset=0)
Definition Pointer.h:557
const FieldDecl * getField() const
Returns the field information.
Definition Pointer.h:886
Pointer expand() const
Expands a pointer to the containing array, undoing narrowing.
Definition Pointer.h:649
friend class Block
Definition Pointer.h:1318
bool isElementPastEnd() const
Checks if the pointer is an out-of-bounds element pointer.
Definition Pointer.h:1069
bool isDereferencable() const
Whether this block can be read from at all.
Definition Pointer.h:1192
void startLifetime() const
Start the lifetime of this pointer.
Definition Pointer.h:1267
Pointer(OpaquePointer OP, uint64_t Offset=0)
Definition Pointer.h:576
bool isBlockPointer() const
Definition Pointer.h:870
bool operator!=(const Pointer &P) const
Definition Pointer.h:587
void deactivate() const
Deactivates an entire strurcutre.
Definition Pointer.h:1253
friend class DeadBlock
Definition Pointer.h:1319
TypeidPointer Typeid
Definition Pointer.h:1356
bool isInitialized() const
Checks if an object was initialized.
Definition Pointer.h:1224
size_t getSize() const
Returns the total size of the innermost field.
Definition Pointer.h:765
bool isTemporary() const
Checks if the storage is temporary.
Definition Pointer.h:906
StringPointer Str
Definition Pointer.h:1357
const FunctionPointer & asFunctionPointer() const
Definition Pointer.h:853
SourceLocation getDeclLoc() const
Definition Pointer.h:694
const Block * block() const
Definition Pointer.h:1010
void initializeElement(unsigned Index) const
Initialized the given element of a primitive array.
Definition Pointer.h:1216
Pointer(DeclOrExpr DOE, bool ConstexprUnknown=false)
Definition Pointer.h:569
bool isFunctionPointer() const
Definition Pointer.h:872
Pointer getDeclPtr() const
Definition Pointer.h:751
bool isReadablePointerType() const
Definition Pointer.h:1205
const Descriptor * getFieldDesc() const
Accessors for information about the innermost field.
Definition Pointer.h:714
PtrView view() const
Definition Pointer.h:603
bool isVirtualBaseClass() const
Definition Pointer.h:956
Pointer(const Expr *Base, unsigned Id)
Definition Pointer.h:564
bool isBaseClass() const
Checks if a structure is a base class.
Definition Pointer.h:955
size_t elemSize() const
Returns the element size of the innermost field.
Definition Pointer.h:754
Lifetime getLifetime() const
Definition Pointer.h:1257
const BlockPointer & asBlockPointer() const
Definition Pointer.h:845
Pointer(StringPointer Str, uint64_t Offset=0)
Definition Pointer.h:566
const std::byte * getRawAddress() const
If backed by actual data (i.e.
Definition Pointer.h:1017
bool isField() const
Checks if the item is a field in an object.
Definition Pointer.h:678
bool isElementInitialized(unsigned Index) const
Like isInitialized(), but for primitive arrays.
Definition Pointer.h:1232
const Record * getRecord() const
Returns the record descriptor of a class.
Definition Pointer.h:878
Structure/Class descriptor.
Definition Record.h:27
@ Address
A pointer to a ValueDecl.
Definition Primitives.h:28
llvm::raw_ostream & operator<<(llvm::raw_ostream &OS, const Boolean &B)
Definition Boolean.h:148
static bool isFixedSizeIntegralType(PrimType T)
PrimType
Enumeration of the primitive types of the VM.
Definition PrimType.h:35
Top level wrappers for InstallAPI frontend operations.
bool operator==(const CallGraphNode::CallRecord &LHS, const CallGraphNode::CallRecord &RHS)
Definition CallGraph.h:218
OptionalUnsigned< unsigned > UnsignedOrNone
const FunctionProtoType * T
static bool isBlockPointer(Expr *Arg)
@ Off
Never emit colors regardless of the output stream.
const StreamingDiagnostic & operator<<(const StreamingDiagnostic &DB, const ConceptReference *C)
Insertion operator for diagnostics.
U cast(CodeGen::Address addr)
Definition Address.h:327
__builtin_elementwise_add_sat __builtin_elementwise_sub_sat uint32_t uint64_t __builtin_riscv_ssha_i32 __builtin_riscv_sshl_u32 uint64_t
Pointer * Prev
Previous link in the pointer chain.
Definition Pointer.h:353
Pointer * Next
Next link in the pointer chain.
Definition Pointer.h:355
unsigned Base
Start of the current subfield.
Definition Pointer.h:351
Block * Pointee
The block the pointer is pointing to.
Definition Pointer.h:349
QualType getType() const
Definition DeclOrExpr.h:48
Describes a memory block created by an allocation site.
Definition Descriptor.h:126
const bool IsConst
Flag indicating if the block is mutable.
Definition Descriptor.h:158
unsigned getSize() const
Returns the size of the object without metadata.
Definition Descriptor.h:229
QualType getType() const
const Decl * asDecl() const
Definition Descriptor.h:204
const Descriptor *const ElemDesc
Descriptor of the array element.
Definition Descriptor.h:152
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:242
const bool IsArray
Flag indicating if the block is an array.
Definition Descriptor.h:165
bool isPrimitiveArray() const
Checks if the descriptor is of an array of primitives.
Definition Descriptor.h:254
bool isZeroSizeArray() const
Checks if the descriptor is of an array of zero size.
Definition Descriptor.h:258
const FieldDecl * asFieldDecl() const
Definition Descriptor.h:216
const Record *const ElemRecord
Pointer to the record, if block contains records.
Definition Descriptor.h:150
Descriptor used for global variables.
Definition Descriptor.h:48
A pointer-sized struct we use to allocate into data storage.
Definition InitMap.h:79
Inline descriptor embedded in structures and arrays.
Definition Descriptor.h:78
unsigned IsActive
Flag indicating if the field is the active member of a union.
Definition Descriptor.h:100
unsigned IsConstInMutable
Flag indicating if this field is a const field nested in a mutable parent field.
Definition Descriptor.h:110
unsigned IsBase
Flag indicating if the field is an embedded base class.
Definition Descriptor.h:94
unsigned IsVirtualBase
Flag inidcating if the field is a virtual base class.
Definition Descriptor.h:97
unsigned InUnion
Flag indicating if this field is in a union (even if nested).
Definition Descriptor.h:103
unsigned Offset
Offset inside the structure/array.
Definition Descriptor.h:80
unsigned IsInitialized
For primitive fields, it indicates if the field was initialized.
Definition Descriptor.h:91
unsigned IsConst
Flag indicating if the storage is constant or not.
Definition Descriptor.h:85
unsigned IsFieldMutable
Flag indicating if the field is mutable (if in a record).
Definition Descriptor.h:106
QualType getPointeeType() const
Definition Pointer.h:368
IntPointer baseCast(const Context &Ctx, unsigned BaseOffset) const
Definition Pointer.cpp:1363
const Type * getType() const
Definition Pointer.h:365
llvm::PointerIntPair< const Type *, 1, bool > TypeAndIsNull
Definition Pointer.h:359
std::optional< IntPointer > atOffset(const Context &Ctx, unsigned Offset) const
Definition Pointer.cpp:1334
OpaquePointer withFieldType(const Type *FieldTy, std::optional< bool > PastEnd=std::nullopt) const
Definition Pointer.h:456
llvm::PointerIntPair< const Type *, 2, unsigned > FieldType
Definition Pointer.h:444
OpaquePointer withPastEnd(bool PastEnd) const
Definition Pointer.h:475
QualType getObjectType() const
Definition Pointer.h:483
bool isRoot() const
Check if the pointer has offset 0.
Definition Pointer.cpp:1494
const VarDecl * getBaseVarDecl() const
Definition Pointer.h:451
QualType getSurroundingArray() const
If this is pointing to an array element, return the array.
Definition Pointer.cpp:1463
const Expr * getBaseExpr() const
Definition Pointer.h:452
bool isOnePastEndOrElementPastEnd() const
This is used in Pointer::isOnePastEnd().
Definition Pointer.cpp:1551
const PointerPathEntry * Path
Definition Pointer.h:445
ArrayRef< PointerPathEntry > path() const
Definition Pointer.h:448
const ValueDecl * getBaseDecl() const
Definition Pointer.h:450
bool isArrayElement() const
Definition Pointer.h:496
QualType getFieldType() const
Definition Pointer.h:490
bool isConstexprUnknown() const
Definition Pointer.h:507
OpaquePointer withPath(const PointerPathEntry *Path, unsigned PathLength, const Type *FieldTy, std::optional< bool > PastEnd=std::nullopt) const
Definition Pointer.h:465
std::optional< size_t > computeLayoutOffset(const ASTContext &ASTCtx) const
Definition Pointer.cpp:1399
static PointerPathEntry array(int64_t Index)
Definition Pointer.h:419
static PointerPathEntry field(const FieldDecl *FD)
Definition Pointer.h:433
llvm::PointerIntPair< const CXXRecordDecl *, 1, bool > RD
Definition Pointer.h:409
enum clang::interp::PointerPathEntry::@133156275124227243235357227301330162015140142322 Kind
static PointerPathEntry negativeArray(int64_t Index)
Definition Pointer.h:426
static PointerPathEntry base(const CXXRecordDecl *RD, bool Virtual=false)
Definition Pointer.h:412
bool isUnknownSizeArray() const
Definition Pointer.h:170
const Descriptor * getDeclDesc() const
Definition Pointer.h:88
bool allElementsInitialized() const
Definition Pointer.cpp:868
PtrView atField(unsigned Offset) const
Definition Pointer.h:271
bool isField() const
Definition Pointer.h:162
size_t elemSize() const
Definition Pointer.h:90
const Record * getRecord() const
Definition Pointer.h:155
const Descriptor * getFieldDesc() const
Definition Pointer.h:82
const FieldDecl * getField() const
Definition Pointer.h:160
bool isElementInitialized(unsigned Index) const
Definition Pointer.cpp:726
static constexpr unsigned PastEndMark
Definition Pointer.h:37
PtrView atIndex(unsigned Idx) const
Definition Pointer.h:211
bool isBaseClass() const
Definition Pointer.h:166
bool inPrimitiveArray() const
Definition Pointer.h:61
void activate() const
Definition Pointer.cpp:893
InlineDescriptor * getDescriptor(unsigned Offset) const
Definition Pointer.h:76
bool inArray() const
Definition Pointer.h:60
void startLifetime() const
Definition Pointer.h:336
bool isConstInMutable() const
Definition Pointer.h:52
PtrView narrow() const
Definition Pointer.h:92
T & elem(unsigned I) const
Definition Pointer.h:253
bool isElementPastEnd() const
Definition Pointer.h:47
const Block * block() const
Definition Pointer.h:62
bool isVolatile() const
Definition Pointer.h:56
bool isInitialized() const
Definition Pointer.h:299
bool isArrayElement() const
Definition Pointer.h:234
bool isPastEnd() const
Definition Pointer.h:174
PtrView getArray() const
Definition Pointer.h:150
bool isZero() const
Definition Pointer.h:43
const Record * getElemRecord() const
Definition Pointer.h:156
unsigned getNumElems() const
Definition Pointer.h:232
InitMapPtr & getInitMap() const
Definition Pointer.h:328
InlineDescriptor * getInlineDesc() const
Definition Pointer.h:70
bool inUnion() const
Definition Pointer.h:59
bool isMutable() const
Definition Pointer.h:49
void endLifetime() const
Definition Pointer.h:337
void initializeElement(unsigned Index) const
Definition Pointer.cpp:844
bool operator==(const PtrView &Other) const
Definition Pointer.h:339
void initialize() const
Definition Pointer.cpp:817
QualType getType() const
Definition Pointer.h:276
bool isOnePastEnd() const
Definition Pointer.h:190
bool isConst() const
Definition Pointer.h:66
bool isRoot() const
Definition Pointer.h:64
void setLifeState(Lifetime L) const
Definition Pointer.cpp:789
Lifetime getLifetime() const
Definition Pointer.cpp:763
PtrView getBase() const
Definition Pointer.h:266
unsigned getOffset() const
Definition Pointer.h:176
bool isActive() const
Definition Pointer.h:45
PtrView expand() const
Definition Pointer.h:116
bool isVirtualBaseClass() const
Definition Pointer.h:167
bool isZeroSizeArray() const
Definition Pointer.h:48
bool isLive() const
Definition Pointer.h:44
bool isArrayRoot() const
Definition Pointer.h:46
T & deref() const
Definition Pointer.h:246
bool operator!=(const PtrView &Other) const
Definition Pointer.h:344
PtrView stripBaseCasts() const
Definition Pointer.h:143
bool isExtern() const
Definition Pointer.h:55
size_t getSize() const
Definition Pointer.h:188
int64_t getIndex() const
Definition Pointer.h:220
const StringLiteral * getLiteral() const
Definition Pointer.h:397
StringPointer decay() const
Definition Pointer.h:396