Pointer.h 13 KB

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