Pointer.h 11 KB

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