Redeclarable.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- Redeclarable.h - Base for Decls that can be redeclared --*- C++ -*-====//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file defines the Redeclarable interface.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_CLANG_AST_REDECLARABLE_H
  18. #define LLVM_CLANG_AST_REDECLARABLE_H
  19. #include "clang/AST/ExternalASTSource.h"
  20. #include "llvm/ADT/DenseMapInfo.h"
  21. #include "llvm/ADT/PointerUnion.h"
  22. #include "llvm/ADT/iterator_range.h"
  23. #include "llvm/Support/Casting.h"
  24. #include <cassert>
  25. #include <cstddef>
  26. #include <iterator>
  27. namespace clang {
  28. class ASTContext;
  29. class Decl;
  30. // Some notes on redeclarables:
  31. //
  32. // - Every redeclarable is on a circular linked list.
  33. //
  34. // - Every decl has a pointer to the first element of the chain _and_ a
  35. // DeclLink that may point to one of 3 possible states:
  36. // - the "previous" (temporal) element in the chain
  37. // - the "latest" (temporal) element in the chain
  38. // - the "uninitialized-latest" value (when newly-constructed)
  39. //
  40. // - The first element is also often called the canonical element. Every
  41. // element has a pointer to it so that "getCanonical" can be fast.
  42. //
  43. // - Most links in the chain point to previous, except the link out of
  44. // the first; it points to latest.
  45. //
  46. // - Elements are called "first", "previous", "latest" or
  47. // "most-recent" when referring to temporal order: order of addition
  48. // to the chain.
  49. //
  50. // - It's easiest to just ignore the implementation of DeclLink when making
  51. // sense of the redeclaration chain.
  52. //
  53. // - There's also a "definition" link for several types of
  54. // redeclarable, where only one definition should exist at any given
  55. // time (and the defn pointer is stored in the decl's "data" which
  56. // is copied to every element on the chain when it's changed).
  57. //
  58. // Here is some ASCII art:
  59. //
  60. // "first" "latest"
  61. // "canonical" "most recent"
  62. // +------------+ first +--------------+
  63. // | | <--------------------------- | |
  64. // | | | |
  65. // | | | |
  66. // | | +--------------+ | |
  67. // | | first | | | |
  68. // | | <---- | | | |
  69. // | | | | | |
  70. // | @class A | link | @interface A | link | @class A |
  71. // | seen first | <---- | seen second | <---- | seen third |
  72. // | | | | | |
  73. // +------------+ +--------------+ +--------------+
  74. // | data | defn | data | defn | data |
  75. // | | ----> | | <---- | |
  76. // +------------+ +--------------+ +--------------+
  77. // | | ^ ^
  78. // | |defn | |
  79. // | link +-----+ |
  80. // +-->-------------------------------------------+
  81. /// Provides common interface for the Decls that can be redeclared.
  82. template<typename decl_type>
  83. class Redeclarable {
  84. protected:
  85. class DeclLink {
  86. /// A pointer to a known latest declaration, either statically known or
  87. /// generationally updated as decls are added by an external source.
  88. using KnownLatest =
  89. LazyGenerationalUpdatePtr<const Decl *, Decl *,
  90. &ExternalASTSource::CompleteRedeclChain>;
  91. /// We store a pointer to the ASTContext in the UninitializedLatest
  92. /// pointer, but to avoid circular type dependencies when we steal the low
  93. /// bits of this pointer, we use a raw void* here.
  94. using UninitializedLatest = const void *;
  95. using Previous = Decl *;
  96. /// A pointer to either an uninitialized latest declaration (where either
  97. /// we've not yet set the previous decl or there isn't one), or to a known
  98. /// previous declaration.
  99. using NotKnownLatest = llvm::PointerUnion<Previous, UninitializedLatest>;
  100. mutable llvm::PointerUnion<NotKnownLatest, KnownLatest> Link;
  101. public:
  102. enum PreviousTag { PreviousLink };
  103. enum LatestTag { LatestLink };
  104. DeclLink(LatestTag, const ASTContext &Ctx)
  105. : Link(NotKnownLatest(reinterpret_cast<UninitializedLatest>(&Ctx))) {}
  106. DeclLink(PreviousTag, decl_type *D) : Link(NotKnownLatest(Previous(D))) {}
  107. bool isFirst() const {
  108. return Link.is<KnownLatest>() ||
  109. // FIXME: 'template' is required on the next line due to an
  110. // apparent clang bug.
  111. Link.get<NotKnownLatest>().template is<UninitializedLatest>();
  112. }
  113. decl_type *getPrevious(const decl_type *D) const {
  114. if (Link.is<NotKnownLatest>()) {
  115. NotKnownLatest NKL = Link.get<NotKnownLatest>();
  116. if (NKL.is<Previous>())
  117. return static_cast<decl_type*>(NKL.get<Previous>());
  118. // Allocate the generational 'most recent' cache now, if needed.
  119. Link = KnownLatest(*reinterpret_cast<const ASTContext *>(
  120. NKL.get<UninitializedLatest>()),
  121. const_cast<decl_type *>(D));
  122. }
  123. return static_cast<decl_type*>(Link.get<KnownLatest>().get(D));
  124. }
  125. void setPrevious(decl_type *D) {
  126. assert(!isFirst() && "decl became non-canonical unexpectedly");
  127. Link = Previous(D);
  128. }
  129. void setLatest(decl_type *D) {
  130. assert(isFirst() && "decl became canonical unexpectedly");
  131. if (Link.is<NotKnownLatest>()) {
  132. NotKnownLatest NKL = Link.get<NotKnownLatest>();
  133. Link = KnownLatest(*reinterpret_cast<const ASTContext *>(
  134. NKL.get<UninitializedLatest>()),
  135. D);
  136. } else {
  137. auto Latest = Link.get<KnownLatest>();
  138. Latest.set(D);
  139. Link = Latest;
  140. }
  141. }
  142. void markIncomplete() { Link.get<KnownLatest>().markIncomplete(); }
  143. Decl *getLatestNotUpdated() const {
  144. assert(isFirst() && "expected a canonical decl");
  145. if (Link.is<NotKnownLatest>())
  146. return nullptr;
  147. return Link.get<KnownLatest>().getNotUpdated();
  148. }
  149. };
  150. static DeclLink PreviousDeclLink(decl_type *D) {
  151. return DeclLink(DeclLink::PreviousLink, D);
  152. }
  153. static DeclLink LatestDeclLink(const ASTContext &Ctx) {
  154. return DeclLink(DeclLink::LatestLink, Ctx);
  155. }
  156. /// Points to the next redeclaration in the chain.
  157. ///
  158. /// If isFirst() is false, this is a link to the previous declaration
  159. /// of this same Decl. If isFirst() is true, this is the first
  160. /// declaration and Link points to the latest declaration. For example:
  161. ///
  162. /// #1 int f(int x, int y = 1); // <pointer to #3, true>
  163. /// #2 int f(int x = 0, int y); // <pointer to #1, false>
  164. /// #3 int f(int x, int y) { return x + y; } // <pointer to #2, false>
  165. ///
  166. /// If there is only one declaration, it is <pointer to self, true>
  167. DeclLink RedeclLink;
  168. decl_type *First;
  169. decl_type *getNextRedeclaration() const {
  170. return RedeclLink.getPrevious(static_cast<const decl_type *>(this));
  171. }
  172. public:
  173. friend class ASTDeclReader;
  174. friend class ASTDeclWriter;
  175. friend class IncrementalParser;
  176. Redeclarable(const ASTContext &Ctx)
  177. : RedeclLink(LatestDeclLink(Ctx)),
  178. First(static_cast<decl_type *>(this)) {}
  179. /// Return the previous declaration of this declaration or NULL if this
  180. /// is the first declaration.
  181. decl_type *getPreviousDecl() {
  182. if (!RedeclLink.isFirst())
  183. return getNextRedeclaration();
  184. return nullptr;
  185. }
  186. const decl_type *getPreviousDecl() const {
  187. return const_cast<decl_type *>(
  188. static_cast<const decl_type*>(this))->getPreviousDecl();
  189. }
  190. /// Return the first declaration of this declaration or itself if this
  191. /// is the only declaration.
  192. decl_type *getFirstDecl() { return First; }
  193. /// Return the first declaration of this declaration or itself if this
  194. /// is the only declaration.
  195. const decl_type *getFirstDecl() const { return First; }
  196. /// True if this is the first declaration in its redeclaration chain.
  197. bool isFirstDecl() const { return RedeclLink.isFirst(); }
  198. /// Returns the most recent (re)declaration of this declaration.
  199. decl_type *getMostRecentDecl() {
  200. return getFirstDecl()->getNextRedeclaration();
  201. }
  202. /// Returns the most recent (re)declaration of this declaration.
  203. const decl_type *getMostRecentDecl() const {
  204. return getFirstDecl()->getNextRedeclaration();
  205. }
  206. /// Set the previous declaration. If PrevDecl is NULL, set this as the
  207. /// first and only declaration.
  208. void setPreviousDecl(decl_type *PrevDecl);
  209. /// Iterates through all the redeclarations of the same decl.
  210. class redecl_iterator {
  211. /// Current - The current declaration.
  212. decl_type *Current = nullptr;
  213. decl_type *Starter;
  214. bool PassedFirst = false;
  215. public:
  216. using value_type = decl_type *;
  217. using reference = decl_type *;
  218. using pointer = decl_type *;
  219. using iterator_category = std::forward_iterator_tag;
  220. using difference_type = std::ptrdiff_t;
  221. redecl_iterator() = default;
  222. explicit redecl_iterator(decl_type *C) : Current(C), Starter(C) {}
  223. reference operator*() const { return Current; }
  224. pointer operator->() const { return Current; }
  225. redecl_iterator& operator++() {
  226. assert(Current && "Advancing while iterator has reached end");
  227. // Make sure we don't infinitely loop on an invalid redecl chain. This
  228. // should never happen.
  229. if (Current->isFirstDecl()) {
  230. if (PassedFirst) {
  231. assert(0 && "Passed first decl twice, invalid redecl chain!");
  232. Current = nullptr;
  233. return *this;
  234. }
  235. PassedFirst = true;
  236. }
  237. // Get either previous decl or latest decl.
  238. decl_type *Next = Current->getNextRedeclaration();
  239. Current = (Next != Starter) ? Next : nullptr;
  240. return *this;
  241. }
  242. redecl_iterator operator++(int) {
  243. redecl_iterator tmp(*this);
  244. ++(*this);
  245. return tmp;
  246. }
  247. friend bool operator==(redecl_iterator x, redecl_iterator y) {
  248. return x.Current == y.Current;
  249. }
  250. friend bool operator!=(redecl_iterator x, redecl_iterator y) {
  251. return x.Current != y.Current;
  252. }
  253. };
  254. using redecl_range = llvm::iterator_range<redecl_iterator>;
  255. /// Returns an iterator range for all the redeclarations of the same
  256. /// decl. It will iterate at least once (when this decl is the only one).
  257. redecl_range redecls() const {
  258. return redecl_range(redecl_iterator(const_cast<decl_type *>(
  259. static_cast<const decl_type *>(this))),
  260. redecl_iterator());
  261. }
  262. redecl_iterator redecls_begin() const { return redecls().begin(); }
  263. redecl_iterator redecls_end() const { return redecls().end(); }
  264. };
  265. /// Get the primary declaration for a declaration from an AST file. That
  266. /// will be the first-loaded declaration.
  267. Decl *getPrimaryMergedDecl(Decl *D);
  268. /// Provides common interface for the Decls that cannot be redeclared,
  269. /// but can be merged if the same declaration is brought in from multiple
  270. /// modules.
  271. template<typename decl_type>
  272. class Mergeable {
  273. public:
  274. Mergeable() = default;
  275. /// Return the first declaration of this declaration or itself if this
  276. /// is the only declaration.
  277. decl_type *getFirstDecl() {
  278. auto *D = static_cast<decl_type *>(this);
  279. if (!D->isFromASTFile())
  280. return D;
  281. return cast<decl_type>(getPrimaryMergedDecl(const_cast<decl_type*>(D)));
  282. }
  283. /// Return the first declaration of this declaration or itself if this
  284. /// is the only declaration.
  285. const decl_type *getFirstDecl() const {
  286. const auto *D = static_cast<const decl_type *>(this);
  287. if (!D->isFromASTFile())
  288. return D;
  289. return cast<decl_type>(getPrimaryMergedDecl(const_cast<decl_type*>(D)));
  290. }
  291. /// Returns true if this is the first declaration.
  292. bool isFirstDecl() const { return getFirstDecl() == this; }
  293. };
  294. /// A wrapper class around a pointer that always points to its canonical
  295. /// declaration.
  296. ///
  297. /// CanonicalDeclPtr<decl_type> behaves just like decl_type*, except we call
  298. /// decl_type::getCanonicalDecl() on construction.
  299. ///
  300. /// This is useful for hashtables that you want to be keyed on a declaration's
  301. /// canonical decl -- if you use CanonicalDeclPtr as the key, you don't need to
  302. /// remember to call getCanonicalDecl() everywhere.
  303. template <typename decl_type> class CanonicalDeclPtr {
  304. public:
  305. CanonicalDeclPtr() = default;
  306. CanonicalDeclPtr(decl_type *Ptr)
  307. : Ptr(Ptr ? Ptr->getCanonicalDecl() : nullptr) {}
  308. CanonicalDeclPtr(const CanonicalDeclPtr &) = default;
  309. CanonicalDeclPtr &operator=(const CanonicalDeclPtr &) = default;
  310. operator decl_type *() { return Ptr; }
  311. operator const decl_type *() const { return Ptr; }
  312. decl_type *operator->() { return Ptr; }
  313. const decl_type *operator->() const { return Ptr; }
  314. decl_type &operator*() { return *Ptr; }
  315. const decl_type &operator*() const { return *Ptr; }
  316. friend bool operator==(CanonicalDeclPtr LHS, CanonicalDeclPtr RHS) {
  317. return LHS.Ptr == RHS.Ptr;
  318. }
  319. friend bool operator!=(CanonicalDeclPtr LHS, CanonicalDeclPtr RHS) {
  320. return LHS.Ptr != RHS.Ptr;
  321. }
  322. private:
  323. friend struct llvm::DenseMapInfo<CanonicalDeclPtr<decl_type>>;
  324. friend struct llvm::PointerLikeTypeTraits<CanonicalDeclPtr<decl_type>>;
  325. decl_type *Ptr = nullptr;
  326. };
  327. } // namespace clang
  328. namespace llvm {
  329. template <typename decl_type>
  330. struct DenseMapInfo<clang::CanonicalDeclPtr<decl_type>> {
  331. using CanonicalDeclPtr = clang::CanonicalDeclPtr<decl_type>;
  332. using BaseInfo = DenseMapInfo<decl_type *>;
  333. static CanonicalDeclPtr getEmptyKey() {
  334. // Construct our CanonicalDeclPtr this way because the regular constructor
  335. // would dereference P.Ptr, which is not allowed.
  336. CanonicalDeclPtr P;
  337. P.Ptr = BaseInfo::getEmptyKey();
  338. return P;
  339. }
  340. static CanonicalDeclPtr getTombstoneKey() {
  341. CanonicalDeclPtr P;
  342. P.Ptr = BaseInfo::getTombstoneKey();
  343. return P;
  344. }
  345. static unsigned getHashValue(const CanonicalDeclPtr &P) {
  346. return BaseInfo::getHashValue(P);
  347. }
  348. static bool isEqual(const CanonicalDeclPtr &LHS,
  349. const CanonicalDeclPtr &RHS) {
  350. return BaseInfo::isEqual(LHS, RHS);
  351. }
  352. };
  353. template <typename decl_type>
  354. struct PointerLikeTypeTraits<clang::CanonicalDeclPtr<decl_type>> {
  355. static inline void *getAsVoidPointer(clang::CanonicalDeclPtr<decl_type> P) {
  356. return P.Ptr;
  357. }
  358. static inline clang::CanonicalDeclPtr<decl_type> getFromVoidPointer(void *P) {
  359. clang::CanonicalDeclPtr<decl_type> C;
  360. C.Ptr = PointerLikeTypeTraits<decl_type *>::getFromVoidPtr(P);
  361. return C;
  362. }
  363. static constexpr int NumLowBitsAvailable =
  364. PointerLikeTypeTraits<decl_type *>::NumLowBitsAvailable;
  365. };
  366. } // namespace llvm
  367. #endif // LLVM_CLANG_AST_REDECLARABLE_H
  368. #ifdef __GNUC__
  369. #pragma GCC diagnostic pop
  370. #endif