Pointer.h 11 KB

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