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