1 //===-- Redeclarable.h - Base for Decls that can be redeclared -*- C++ -*-====//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the Redeclarable interface.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_AST_REDECLARABLE_H
15 #define LLVM_CLANG_AST_REDECLARABLE_H
17 #include "clang/AST/ExternalASTSource.h"
18 #include "llvm/ADT/PointerIntPair.h"
19 #include "llvm/Support/Casting.h"
25 /// \brief Provides common interface for the Decls that can be redeclared.
26 template<typename decl_type>
30 /// A pointer to a known latest declaration, either statically known or
31 /// generationally updated as decls are added by an external source.
32 typedef LazyGenerationalUpdatePtr<const Decl*, Decl*,
33 &ExternalASTSource::CompleteRedeclChain>
36 /// We store a pointer to the ASTContext in the UninitializedLatest
37 /// pointer, but to avoid circular type dependencies when we steal the low
38 /// bits of this pointer, we use a raw void* here.
39 typedef const void *UninitializedLatest;
41 typedef Decl *Previous;
43 /// A pointer to either an uninitialized latest declaration (where either
44 /// we've not yet set the previous decl or there isn't one), or to a known
45 /// previous declaration.
46 typedef llvm::PointerUnion<Previous, UninitializedLatest> NotKnownLatest;
48 mutable llvm::PointerUnion<NotKnownLatest, KnownLatest> Next;
51 enum PreviousTag { PreviousLink };
52 enum LatestTag { LatestLink };
54 DeclLink(LatestTag, const ASTContext &Ctx)
55 : Next(NotKnownLatest(reinterpret_cast<UninitializedLatest>(&Ctx))) {}
56 DeclLink(PreviousTag, decl_type *D)
57 : Next(NotKnownLatest(Previous(D))) {}
59 bool NextIsPrevious() const {
60 return Next.is<NotKnownLatest>() &&
61 // FIXME: 'template' is required on the next line due to an
62 // apparent clang bug.
63 Next.get<NotKnownLatest>().template is<Previous>();
66 bool NextIsLatest() const { return !NextIsPrevious(); }
68 decl_type *getNext(const decl_type *D) const {
69 if (Next.is<NotKnownLatest>()) {
70 NotKnownLatest NKL = Next.get<NotKnownLatest>();
71 if (NKL.is<Previous>())
72 return static_cast<decl_type*>(NKL.get<Previous>());
74 // Allocate the generational 'most recent' cache now, if needed.
75 Next = KnownLatest(*reinterpret_cast<const ASTContext *>(
76 NKL.get<UninitializedLatest>()),
77 const_cast<decl_type *>(D));
80 return static_cast<decl_type*>(Next.get<KnownLatest>().get(D));
83 void setPrevious(decl_type *D) {
84 assert(NextIsPrevious() && "decl became non-canonical unexpectedly");
88 void setLatest(decl_type *D) {
89 assert(NextIsLatest() && "decl became canonical unexpectedly");
90 if (Next.is<NotKnownLatest>()) {
91 NotKnownLatest NKL = Next.get<NotKnownLatest>();
92 Next = KnownLatest(*reinterpret_cast<const ASTContext *>(
93 NKL.get<UninitializedLatest>()),
96 auto Latest = Next.get<KnownLatest>();
102 void markIncomplete() { Next.get<KnownLatest>().markIncomplete(); }
104 Decl *getLatestNotUpdated() const {
105 assert(NextIsLatest() && "expected a canonical decl");
106 if (Next.is<NotKnownLatest>())
108 return Next.get<KnownLatest>().getNotUpdated();
112 static DeclLink PreviousDeclLink(decl_type *D) {
113 return DeclLink(DeclLink::PreviousLink, D);
116 static DeclLink LatestDeclLink(const ASTContext &Ctx) {
117 return DeclLink(DeclLink::LatestLink, Ctx);
120 /// \brief Points to the next redeclaration in the chain.
122 /// If NextIsPrevious() is true, this is a link to the previous declaration
123 /// of this same Decl. If NextIsLatest() is true, this is the first
124 /// declaration and Link points to the latest declaration. For example:
126 /// #1 int f(int x, int y = 1); // <pointer to #3, true>
127 /// #2 int f(int x = 0, int y); // <pointer to #1, false>
128 /// #3 int f(int x, int y) { return x + y; } // <pointer to #2, false>
130 /// If there is only one declaration, it is <pointer to self, true>
134 decl_type *getNextRedeclaration() const {
135 return RedeclLink.getNext(static_cast<const decl_type *>(this));
139 Redeclarable(const ASTContext &Ctx)
140 : RedeclLink(LatestDeclLink(Ctx)), First(static_cast<decl_type *>(this)) {}
142 /// \brief Return the previous declaration of this declaration or NULL if this
143 /// is the first declaration.
144 decl_type *getPreviousDecl() {
145 if (RedeclLink.NextIsPrevious())
146 return getNextRedeclaration();
149 const decl_type *getPreviousDecl() const {
150 return const_cast<decl_type *>(
151 static_cast<const decl_type*>(this))->getPreviousDecl();
154 /// \brief Return the first declaration of this declaration or itself if this
155 /// is the only declaration.
156 decl_type *getFirstDecl() { return First; }
158 /// \brief Return the first declaration of this declaration or itself if this
159 /// is the only declaration.
160 const decl_type *getFirstDecl() const { return First; }
162 /// \brief True if this is the first declaration in its redeclaration chain.
163 bool isFirstDecl() const { return RedeclLink.NextIsLatest(); }
165 /// \brief Returns the most recent (re)declaration of this declaration.
166 decl_type *getMostRecentDecl() {
167 return getFirstDecl()->getNextRedeclaration();
170 /// \brief Returns the most recent (re)declaration of this declaration.
171 const decl_type *getMostRecentDecl() const {
172 return getFirstDecl()->getNextRedeclaration();
175 /// \brief Set the previous declaration. If PrevDecl is NULL, set this as the
176 /// first and only declaration.
177 void setPreviousDecl(decl_type *PrevDecl);
179 /// \brief Iterates through all the redeclarations of the same decl.
180 class redecl_iterator {
181 /// Current - The current declaration.
187 typedef decl_type* value_type;
188 typedef decl_type* reference;
189 typedef decl_type* pointer;
190 typedef std::forward_iterator_tag iterator_category;
191 typedef std::ptrdiff_t difference_type;
193 redecl_iterator() : Current(nullptr) { }
194 explicit redecl_iterator(decl_type *C)
195 : Current(C), Starter(C), PassedFirst(false) { }
197 reference operator*() const { return Current; }
198 pointer operator->() const { return Current; }
200 redecl_iterator& operator++() {
201 assert(Current && "Advancing while iterator has reached end");
202 // Sanity check to avoid infinite loop on invalid redecl chain.
203 if (Current->isFirstDecl()) {
205 assert(0 && "Passed first decl twice, invalid redecl chain!");
212 // Get either previous decl or latest decl.
213 decl_type *Next = Current->getNextRedeclaration();
214 Current = (Next != Starter) ? Next : nullptr;
218 redecl_iterator operator++(int) {
219 redecl_iterator tmp(*this);
224 friend bool operator==(redecl_iterator x, redecl_iterator y) {
225 return x.Current == y.Current;
227 friend bool operator!=(redecl_iterator x, redecl_iterator y) {
228 return x.Current != y.Current;
232 typedef llvm::iterator_range<redecl_iterator> redecl_range;
234 /// \brief Returns an iterator range for all the redeclarations of the same
235 /// decl. It will iterate at least once (when this decl is the only one).
236 redecl_range redecls() const {
237 return redecl_range(redecl_iterator(const_cast<decl_type *>(
238 static_cast<const decl_type *>(this))),
242 redecl_iterator redecls_begin() const { return redecls().begin(); }
243 redecl_iterator redecls_end() const { return redecls().end(); }
245 friend class ASTDeclReader;
246 friend class ASTDeclWriter;
249 /// \brief Get the primary declaration for a declaration from an AST file. That
250 /// will be the first-loaded declaration.
251 Decl *getPrimaryMergedDecl(Decl *D);
253 /// \brief Provides common interface for the Decls that cannot be redeclared,
254 /// but can be merged if the same declaration is brought in from multiple
256 template<typename decl_type>
261 /// \brief Return the first declaration of this declaration or itself if this
262 /// is the only declaration.
263 decl_type *getFirstDecl() {
264 decl_type *D = static_cast<decl_type*>(this);
265 if (!D->isFromASTFile())
267 return cast<decl_type>(getPrimaryMergedDecl(const_cast<decl_type*>(D)));
270 /// \brief Return the first declaration of this declaration or itself if this
271 /// is the only declaration.
272 const decl_type *getFirstDecl() const {
273 const decl_type *D = static_cast<const decl_type*>(this);
274 if (!D->isFromASTFile())
276 return cast<decl_type>(getPrimaryMergedDecl(const_cast<decl_type*>(D)));
279 /// \brief Returns true if this is the first declaration.
280 bool isFirstDecl() const { return getFirstDecl() == this; }