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

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 }
266 /// Checks if the pointer is live.
267 bool isLive() const {
268 if (!isBlockPointer())
269 return true;
270 return BS.Pointee && !BS.Pointee->isDead();
271 }
272 /// Checks if the item is a field in an object.
273 bool isField() const {
274 if (!isBlockPointer())
275 return false;
276
277 return !isRoot() && getFieldDesc()->asDecl();
278 }
279
280 /// Accessor for information about the declaration site.
281 const Descriptor *getDeclDesc() const {
282 if (isIntegralPointer())
283 return Int.Desc;
285 return nullptr;
286
287 assert(isBlockPointer());
288 assert(BS.Pointee);
289 return BS.Pointee->Desc;
290 }
292
293 /// Returns the expression or declaration the pointer has been created for.
295 if (isBlockPointer())
296 return getDeclDesc()->getSource();
297 if (isFunctionPointer()) {
298 const Function *F = Fn.getFunction();
299 return F ? F->getDecl() : DeclTy();
300 }
301 assert(isIntegralPointer());
302 return Int.Desc ? Int.Desc->getSource() : DeclTy();
303 }
304
305 /// Returns a pointer to the object of which this pointer is a field.
306 [[nodiscard]] Pointer getBase() const {
307 if (BS.Base == RootPtrMark) {
308 assert(Offset == PastEndMark && "cannot get base of a block");
309 return Pointer(BS.Pointee, BS.Base, 0);
310 }
311 unsigned NewBase = BS.Base - getInlineDesc()->Offset;
312 return Pointer(BS.Pointee, NewBase, NewBase);
313 }
314 /// Returns the parent array.
315 [[nodiscard]] Pointer getArray() const {
316 if (BS.Base == RootPtrMark) {
317 assert(Offset != 0 && Offset != PastEndMark && "not an array element");
318 return Pointer(BS.Pointee, BS.Base, 0);
319 }
320 assert(Offset != BS.Base && "not an array element");
321 return Pointer(BS.Pointee, BS.Base, BS.Base);
322 }
323
324 /// Accessors for information about the innermost field.
325 const Descriptor *getFieldDesc() const {
326 if (isIntegralPointer())
327 return Int.Desc;
328
329 if (isRoot())
330 return getDeclDesc();
331 return getInlineDesc()->Desc;
332 }
333
334 /// Returns the type of the innermost field.
336 if (isTypeidPointer())
337 return QualType(Typeid.TypeInfoType, 0);
338 if (isFunctionPointer())
339 return Fn.getFunction()->getDecl()->getType();
340
341 if (inPrimitiveArray() && Offset != BS.Base) {
342 // Unfortunately, complex and vector types are not array types in clang,
343 // but they are for us.
344 if (const auto *AT = getFieldDesc()->getType()->getAsArrayTypeUnsafe())
345 return AT->getElementType();
346 if (const auto *CT = getFieldDesc()->getType()->getAs<ComplexType>())
347 return CT->getElementType();
348 if (const auto *CT = getFieldDesc()->getType()->getAs<VectorType>())
349 return CT->getElementType();
350 }
351 return getFieldDesc()->getType();
352 }
353
354 [[nodiscard]] Pointer getDeclPtr() const { return Pointer(BS.Pointee); }
355
356 /// Returns the element size of the innermost field.
357 size_t elemSize() const {
358 if (isIntegralPointer()) {
359 if (!Int.Desc)
360 return 1;
361 return Int.Desc->getElemSize();
362 }
363
364 if (BS.Base == RootPtrMark)
365 return getDeclDesc()->getSize();
366 return getFieldDesc()->getElemSize();
367 }
368 /// Returns the total size of the innermost field.
369 size_t getSize() const {
370 assert(isBlockPointer());
371 return getFieldDesc()->getSize();
372 }
373
374 /// Returns the offset into an array.
375 unsigned getOffset() const {
376 assert(Offset != PastEndMark && "invalid offset");
377 assert(isBlockPointer());
378 if (BS.Base == RootPtrMark)
379 return Offset;
380
381 unsigned Adjust = 0;
382 if (Offset != BS.Base) {
383 if (getFieldDesc()->ElemDesc)
384 Adjust = sizeof(InlineDescriptor);
385 else
386 Adjust = sizeof(InitMapPtr);
387 }
388 return Offset - BS.Base - Adjust;
389 }
390
391 /// Whether this array refers to an array, but not
392 /// to the first element.
393 bool isArrayRoot() const { return inArray() && Offset == BS.Base; }
394
395 /// Checks if the innermost field is an array.
396 bool inArray() const {
397 if (isBlockPointer())
398 return getFieldDesc()->IsArray;
399 return false;
400 }
401 bool inUnion() const {
402 if (isBlockPointer() && BS.Base >= sizeof(InlineDescriptor))
403 return getInlineDesc()->InUnion;
404 return false;
405 };
406
407 /// Checks if the structure is a primitive array.
408 bool inPrimitiveArray() const {
409 if (isBlockPointer())
410 return getFieldDesc()->isPrimitiveArray();
411 return false;
412 }
413 /// Checks if the structure is an array of unknown size.
414 bool isUnknownSizeArray() const {
415 if (!isBlockPointer())
416 return false;
418 }
419 /// Checks if the pointer points to an array.
420 bool isArrayElement() const {
421 if (!isBlockPointer())
422 return false;
423
424 const BlockPointer &BP = BS;
425 if (inArray() && BP.Base != Offset)
426 return true;
427
428 // Might be a narrow()'ed element in a composite array.
429 // Check the inline descriptor.
430 if (BP.Base >= sizeof(InlineDescriptor) && getInlineDesc()->IsArrayElement)
431 return true;
432
433 return false;
434 }
435 /// Pointer points directly to a block.
436 bool isRoot() const {
437 if (isZero() || !isBlockPointer())
438 return true;
439 return (BS.Base == BS.Pointee->getDescriptor()->getMetadataSize() ||
440 BS.Base == 0);
441 }
442 /// If this pointer has an InlineDescriptor we can use to initialize.
443 bool canBeInitialized() const {
444 if (!isBlockPointer())
445 return false;
446
447 return BS.Pointee && BS.Base > 0;
448 }
449
450 [[nodiscard]] const BlockPointer &asBlockPointer() const {
451 assert(isBlockPointer());
452 return BS;
453 }
454 [[nodiscard]] const IntPointer &asIntPointer() const {
455 assert(isIntegralPointer());
456 return Int;
457 }
458 [[nodiscard]] const FunctionPointer &asFunctionPointer() const {
459 assert(isFunctionPointer());
460 return Fn;
461 }
462 [[nodiscard]] const TypeidPointer &asTypeidPointer() const {
463 assert(isTypeidPointer());
464 return Typeid;
465 }
466
467 bool isBlockPointer() const { return StorageKind == Storage::Block; }
468 bool isIntegralPointer() const { return StorageKind == Storage::Int; }
469 bool isFunctionPointer() const { return StorageKind == Storage::Fn; }
470 bool isTypeidPointer() const { return StorageKind == Storage::Typeid; }
471
472 /// Returns the record descriptor of a class.
473 const Record *getRecord() const { return getFieldDesc()->ElemRecord; }
474 /// Returns the element record type, if this is a non-primive array.
475 const Record *getElemRecord() const {
476 const Descriptor *ElemDesc = getFieldDesc()->ElemDesc;
477 return ElemDesc ? ElemDesc->ElemRecord : nullptr;
478 }
479 /// Returns the field information.
480 const FieldDecl *getField() const {
481 if (const Descriptor *FD = getFieldDesc())
482 return FD->asFieldDecl();
483 return nullptr;
484 }
485
486 /// Checks if the storage is extern.
487 bool isExtern() const {
488 if (isBlockPointer())
489 return BS.Pointee && BS.Pointee->isExtern();
490 return false;
491 }
492 /// Checks if the storage is static.
493 bool isStatic() const {
494 if (!isBlockPointer())
495 return true;
496 assert(BS.Pointee);
497 return BS.Pointee->isStatic();
498 }
499 /// Checks if the storage is temporary.
500 bool isTemporary() const {
501 if (isBlockPointer()) {
502 assert(BS.Pointee);
503 return BS.Pointee->isTemporary();
504 }
505 return false;
506 }
507 /// Checks if the storage has been dynamically allocated.
508 bool isDynamic() const {
509 if (isBlockPointer()) {
510 assert(BS.Pointee);
511 return BS.Pointee->isDynamic();
512 }
513 return false;
514 }
515 /// Checks if the storage is a static temporary.
516 bool isStaticTemporary() const { return isStatic() && isTemporary(); }
517
518 /// Checks if the field is mutable.
519 bool isMutable() const {
520 if (!isBlockPointer())
521 return false;
522 return !isRoot() && getInlineDesc()->IsFieldMutable;
523 }
524
525 bool isWeak() const {
526 if (isFunctionPointer())
527 return Fn.isWeak();
528 if (!isBlockPointer())
529 return false;
530
531 assert(isBlockPointer());
532 return BS.Pointee->isWeak();
533 }
534 /// Checks if the object is active.
535 bool isActive() const {
536 if (!isBlockPointer())
537 return true;
538 return isRoot() || getInlineDesc()->IsActive;
539 }
540 /// Checks if a structure is a base class.
541 bool isBaseClass() const { return isField() && getInlineDesc()->IsBase; }
542 bool isVirtualBaseClass() const {
543 return isField() && getInlineDesc()->IsVirtualBase;
544 }
545 /// Checks if the pointer points to a dummy value.
546 bool isDummy() const {
547 if (!isBlockPointer())
548 return false;
549
550 if (const Block *Pointee = BS.Pointee)
551 return Pointee->isDummy();
552 return false;
553 }
554
555 /// Checks if an object or a subfield is mutable.
556 bool isConst() const {
557 if (isIntegralPointer())
558 return true;
559 return isRoot() ? getDeclDesc()->IsConst : getInlineDesc()->IsConst;
560 }
561 bool isConstInMutable() const {
562 if (!isBlockPointer())
563 return false;
564 return isRoot() ? false : getInlineDesc()->IsConstInMutable;
565 }
566
567 /// Checks if an object or a subfield is volatile.
568 bool isVolatile() const {
569 if (!isBlockPointer())
570 return false;
571 return isRoot() ? getDeclDesc()->IsVolatile : getInlineDesc()->IsVolatile;
572 }
573
574 /// Returns the declaration ID.
576 if (isBlockPointer()) {
577 assert(BS.Pointee);
578 return BS.Pointee->getDeclID();
579 }
580 return std::nullopt;
581 }
582
583 /// Returns the byte offset from the start.
584 uint64_t getByteOffset() const {
585 if (isIntegralPointer())
586 return Int.Value + Offset;
587 if (isTypeidPointer())
588 return reinterpret_cast<uintptr_t>(Typeid.TypePtr) + Offset;
589 if (isOnePastEnd())
590 return PastEndMark;
591 return Offset;
592 }
593
594 /// Returns the number of elements.
595 unsigned getNumElems() const {
596 if (!isBlockPointer())
597 return ~0u;
598 return getSize() / elemSize();
599 }
600
601 const Block *block() const { return BS.Pointee; }
602
603 /// If backed by actual data (i.e. a block pointer), return
604 /// an address to that data.
605 const std::byte *getRawAddress() const {
606 assert(isBlockPointer());
607 return BS.Pointee->rawData() + Offset;
608 }
609
610 /// Returns the index into an array.
611 int64_t getIndex() const {
612 if (!isBlockPointer())
614
615 if (isZero())
616 return 0;
617
618 // narrow()ed element in a composite array.
619 if (BS.Base > sizeof(InlineDescriptor) && BS.Base == Offset)
620 return 0;
621
622 if (auto ElemSize = elemSize())
623 return getOffset() / ElemSize;
624 return 0;
625 }
626
627 /// Checks if the index is one past end.
628 bool isOnePastEnd() const {
629 if (!isBlockPointer())
630 return false;
631
632 if (!BS.Pointee)
633 return false;
634
635 if (isUnknownSizeArray())
636 return false;
637
638 return isPastEnd() || (getSize() == getOffset());
639 }
640
641 /// Checks if the pointer points past the end of the object.
642 bool isPastEnd() const {
643 if (isIntegralPointer())
644 return false;
645
646 return !isZero() && Offset > BS.Pointee->getSize();
647 }
648
649 /// Checks if the pointer is an out-of-bounds element pointer.
650 bool isElementPastEnd() const { return Offset == PastEndMark; }
651
652 /// Checks if the pointer is pointing to a zero-size array.
653 bool isZeroSizeArray() const {
654 if (isFunctionPointer())
655 return false;
656 if (const auto *Desc = getFieldDesc())
657 return Desc->isZeroSizeArray();
658 return false;
659 }
660
661 /// Dereferences the pointer, if it's live.
662 template <typename T> T &deref() const {
663 assert(isLive() && "Invalid pointer");
664 assert(isBlockPointer());
665 assert(BS.Pointee);
666 assert(isDereferencable());
667 assert(Offset + sizeof(T) <= BS.Pointee->getDescriptor()->getAllocSize());
668
669 if (isArrayRoot())
670 return *reinterpret_cast<T *>(BS.Pointee->rawData() + BS.Base +
671 sizeof(InitMapPtr));
672
673 return *reinterpret_cast<T *>(BS.Pointee->rawData() + Offset);
674 }
675
676 /// Dereferences the element at index \p I.
677 /// This is equivalent to atIndex(I).deref<T>().
678 template <typename T> T &elem(unsigned I) const {
679 assert(isLive() && "Invalid pointer");
680 assert(isBlockPointer());
681 assert(BS.Pointee);
682 assert(isDereferencable());
683 assert(getFieldDesc()->isPrimitiveArray());
684 assert(I < getFieldDesc()->getNumElems());
685
686 unsigned ElemByteOffset = I * getFieldDesc()->getElemSize();
687 unsigned ReadOffset = BS.Base + sizeof(InitMapPtr) + ElemByteOffset;
688 assert(ReadOffset + sizeof(T) <=
689 BS.Pointee->getDescriptor()->getAllocSize());
690
691 return *reinterpret_cast<T *>(BS.Pointee->rawData() + ReadOffset);
692 }
693
694 /// Whether this block can be read from at all. This is only true for
695 /// block pointers that point to a valid location inside that block.
696 bool isDereferencable() const {
697 if (!isBlockPointer())
698 return false;
699 if (isPastEnd())
700 return false;
701
702 return true;
703 }
704
705 /// Initializes a field.
706 void initialize() const;
707 /// Initialized the given element of a primitive array.
708 void initializeElement(unsigned Index) const;
709 /// Initialize all elements of a primitive array at once. This can be
710 /// used in situations where we *know* we have initialized *all* elements
711 /// of a primtive array.
712 void initializeAllElements() const;
713 /// Checks if an object was initialized.
714 bool isInitialized() const;
715 /// Like isInitialized(), but for primitive arrays.
716 bool isElementInitialized(unsigned Index) const;
717 bool allElementsInitialized() const;
718 /// Activats a field.
719 void activate() const;
720 /// Deactivates an entire strurcutre.
721 void deactivate() const;
722
724 if (!isBlockPointer())
725 return Lifetime::Started;
726 if (BS.Base < sizeof(InlineDescriptor))
727 return Lifetime::Started;
728 return getInlineDesc()->LifeState;
729 }
730
731 void endLifetime() const {
732 if (!isBlockPointer())
733 return;
734 if (BS.Base < sizeof(InlineDescriptor))
735 return;
736 getInlineDesc()->LifeState = Lifetime::Ended;
737 }
738
739 void startLifetime() const {
740 if (!isBlockPointer())
741 return;
742 if (BS.Base < sizeof(InlineDescriptor))
743 return;
744 getInlineDesc()->LifeState = Lifetime::Started;
745 }
746
747 /// Compare two pointers.
749 if (!hasSameBase(*this, Other))
751
752 if (Offset < Other.Offset)
754 if (Offset > Other.Offset)
756
758 }
759
760 /// Checks if two pointers are comparable.
761 static bool hasSameBase(const Pointer &A, const Pointer &B);
762 /// Checks if two pointers can be subtracted.
763 static bool hasSameArray(const Pointer &A, const Pointer &B);
764 /// Checks if both given pointers point to the same block.
765 static bool pointToSameBlock(const Pointer &A, const Pointer &B);
766
767 static std::optional<std::pair<Pointer, Pointer>>
768 computeSplitPoint(const Pointer &A, const Pointer &B);
769
770 /// Whether this points to a block that's been created for a "literal lvalue",
771 /// i.e. a non-MaterializeTemporaryExpr Expr.
772 bool pointsToLiteral() const;
773 bool pointsToStringLiteral() const;
774
775 /// Prints the pointer.
776 void print(llvm::raw_ostream &OS) const;
777
778 /// Compute an integer that can be used to compare this pointer to
779 /// another one. This is usually NOT the same as the pointer offset
780 /// regarding the AST record layout.
781 size_t computeOffsetForComparison() const;
782
783private:
784 friend class Block;
785 friend class DeadBlock;
786 friend class MemberPointer;
787 friend class InterpState;
788 friend struct InitMap;
789 friend class DynamicAllocator;
790 friend class Program;
791
792 /// Returns the embedded descriptor preceding a field.
793 InlineDescriptor *getInlineDesc() const {
794 assert(isBlockPointer());
795 assert(BS.Base != sizeof(GlobalInlineDescriptor));
796 assert(BS.Base <= BS.Pointee->getSize());
797 assert(BS.Base >= sizeof(InlineDescriptor));
798 return getDescriptor(BS.Base);
799 }
800
801 /// Returns a descriptor at a given offset.
802 InlineDescriptor *getDescriptor(unsigned Offset) const {
803 assert(Offset != 0 && "Not a nested pointer");
804 assert(isBlockPointer());
805 assert(!isZero());
806 return reinterpret_cast<InlineDescriptor *>(BS.Pointee->rawData() +
807 Offset) -
808 1;
809 }
810
811 /// Returns a reference to the InitMapPtr which stores the initialization map.
812 InitMapPtr &getInitMap() const {
813 assert(isBlockPointer());
814 assert(!isZero());
815 return *reinterpret_cast<InitMapPtr *>(BS.Pointee->rawData() + BS.Base);
816 }
817
818 /// Offset into the storage.
819 uint64_t Offset = 0;
820
821 Storage StorageKind = Storage::Int;
822 union {
827 };
828};
829
830inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Pointer &P) {
831 P.print(OS);
832 return OS;
833}
834
835} // namespace interp
836} // namespace clang
837
838#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:3157
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:790
UnsignedOrNone getDeclID() const
Returns the declaration ID.
Definition Pointer.h:575
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:568
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:493
bool isDynamic() const
Checks if the storage has been dynamically allocated.
Definition Pointer.h:508
bool inUnion() const
Definition Pointer.h:401
bool isZeroSizeArray() const
Checks if the pointer is pointing to a zero-size array.
Definition Pointer.h:653
bool isElementInitialized(unsigned Index) const
Like isInitialized(), but for primitive arrays.
Definition Pointer.cpp:463
FunctionPointer Fn
Definition Pointer.h:825
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:546
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:408
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:487
int64_t getIndex() const
Returns the index into an array.
Definition Pointer.h:611
friend class MemberPointer
Definition Pointer.h:786
bool isActive() const
Checks if the object is active.
Definition Pointer.h:535
bool isConst() const
Checks if an object or a subfield is mutable.
Definition Pointer.h:556
Pointer atField(unsigned Off) const
Creates a pointer to a field.
Definition Pointer.h:173
bool isWeak() const
Definition Pointer.h:525
T & deref() const
Dereferences the pointer, if it's live.
Definition Pointer.h:662
Pointer(IntPointer &&IntPtr)
Definition Pointer.h:98
bool isMutable() const
Checks if the field is mutable.
Definition Pointer.h:519
bool isConstInMutable() const
Definition Pointer.h:561
DeclTy getSource() const
Returns the expression or declaration the pointer has been created for.
Definition Pointer.h:294
unsigned getNumElems() const
Returns the number of elements.
Definition Pointer.h:595
Pointer getArray() const
Returns the parent array.
Definition Pointer.h:315
bool isUnknownSizeArray() const
Checks if the structure is an array of unknown size.
Definition Pointer.h:414
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:462
bool isIntegralPointer() const
Definition Pointer.h:468
QualType getType() const
Returns the type of the innermost field.
Definition Pointer.h:335
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:420
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:393
bool isLive() const
Checks if the pointer is live.
Definition Pointer.h:267
bool inArray() const
Checks if the innermost field is an array.
Definition Pointer.h:396
bool isStaticTemporary() const
Checks if the storage is a static temporary.
Definition Pointer.h:516
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:678
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:306
uint64_t getByteOffset() const
Returns the byte offset from the start.
Definition Pointer.h:584
bool isTypeidPointer() const
Definition Pointer.h:470
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:748
const IntPointer & asIntPointer() const
Definition Pointer.h:454
bool isRoot() const
Pointer points directly to a block.
Definition Pointer.h:436
const Descriptor * getDeclDesc() const
Accessor for information about the declaration site.
Definition Pointer.h:281
const Record * getElemRecord() const
Returns the element record type, if this is a non-primive array.
Definition Pointer.h:475
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:375
friend class DynamicAllocator
Definition Pointer.h:789
void endLifetime() const
Definition Pointer.h:731
bool isOnePastEnd() const
Checks if the index is one past end.
Definition Pointer.h:628
friend class InterpState
Definition Pointer.h:787
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:642
Pointer(const Function *F, uint64_t Offset=0)
Definition Pointer.h:106
const FieldDecl * getField() const
Returns the field information.
Definition Pointer.h:480
Pointer expand() const
Expands a pointer to the containing array, undoing narrowing.
Definition Pointer.h:221
friend class Block
Definition Pointer.h:784
bool isElementPastEnd() const
Checks if the pointer is an out-of-bounds element pointer.
Definition Pointer.h:650
bool isDereferencable() const
Whether this block can be read from at all.
Definition Pointer.h:696
void startLifetime() const
Definition Pointer.h:739
bool isBlockPointer() const
Definition Pointer.h:467
bool operator!=(const Pointer &P) const
Definition Pointer.h:135
BlockPointer BS
Definition Pointer.h:824
friend struct InitMap
Definition Pointer.h:788
friend class DeadBlock
Definition Pointer.h:785
TypeidPointer Typeid
Definition Pointer.h:826
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:369
bool isTemporary() const
Checks if the storage is temporary.
Definition Pointer.h:500
const FunctionPointer & asFunctionPointer() const
Definition Pointer.h:458
bool allElementsInitialized() const
Definition Pointer.cpp:558
SourceLocation getDeclLoc() const
Definition Pointer.h:291
const Block * block() const
Definition Pointer.h:601
bool isFunctionPointer() const
Definition Pointer.h:469
Pointer getDeclPtr() const
Definition Pointer.h:354
const Descriptor * getFieldDesc() const
Accessors for information about the innermost field.
Definition Pointer.h:325
bool isVirtualBaseClass() const
Definition Pointer.h:542
bool isBaseClass() const
Checks if a structure is a base class.
Definition Pointer.h:541
size_t elemSize() const
Returns the element size of the innermost field.
Definition Pointer.h:357
bool canBeInitialized() const
If this pointer has an InlineDescriptor we can use to initialize.
Definition Pointer.h:443
Lifetime getLifetime() const
Definition Pointer.h:723
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:450
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:605
bool isField() const
Checks if the item is a field in an object.
Definition Pointer.h:273
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:473
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:1745
__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