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"
24 /// \brief Provides common interface for the Decls that can be redeclared.
25 template<typename decl_type>
29 /// A pointer to a known latest declaration, either statically known or
30 /// generationally updated as decls are added by an external source.
31 typedef LazyGenerationalUpdatePtr<const Decl*, Decl*,
32 &ExternalASTSource::CompleteRedeclChain>
35 typedef const ASTContext *UninitializedLatest;
36 typedef Decl *Previous;
38 /// A pointer to either an uninitialized latest declaration (where either
39 /// we've not yet set the previous decl or there isn't one), or to a known
40 /// previous declaration.
41 typedef llvm::PointerUnion<Previous, UninitializedLatest> NotKnownLatest;
43 mutable llvm::PointerUnion<NotKnownLatest, KnownLatest> Next;
46 enum PreviousTag { PreviousLink };
47 enum LatestTag { LatestLink };
49 DeclLink(LatestTag, const ASTContext &Ctx)
50 : Next(NotKnownLatest(&Ctx)) {}
51 DeclLink(PreviousTag, decl_type *D)
52 : Next(NotKnownLatest(Previous(D))) {}
54 bool NextIsPrevious() const {
55 return Next.is<NotKnownLatest>() &&
56 // FIXME: 'template' is required on the next line due to an
57 // apparent clang bug.
58 Next.get<NotKnownLatest>().template is<Previous>();
61 bool NextIsLatest() const { return !NextIsPrevious(); }
63 decl_type *getNext(const decl_type *D) const {
64 if (Next.is<NotKnownLatest>()) {
65 NotKnownLatest NKL = Next.get<NotKnownLatest>();
66 if (NKL.is<Previous>())
67 return static_cast<decl_type*>(NKL.get<Previous>());
69 // Allocate the generational 'most recent' cache now, if needed.
70 Next = KnownLatest(*NKL.get<UninitializedLatest>(),
71 const_cast<decl_type *>(D));
74 return static_cast<decl_type*>(Next.get<KnownLatest>().get(D));
77 void setPrevious(decl_type *D) {
78 assert(NextIsPrevious() && "decl became non-canonical unexpectedly");
82 void setLatest(decl_type *D) {
83 assert(NextIsLatest() && "decl became canonical unexpectedly");
84 if (Next.is<NotKnownLatest>()) {
85 NotKnownLatest NKL = Next.get<NotKnownLatest>();
86 Next = KnownLatest(*NKL.get<UninitializedLatest>(), D);
88 auto Latest = Next.get<KnownLatest>();
94 void markIncomplete() { Next.get<KnownLatest>().markIncomplete(); }
97 static DeclLink PreviousDeclLink(decl_type *D) {
98 return DeclLink(DeclLink::PreviousLink, D);
101 static DeclLink LatestDeclLink(const ASTContext &Ctx) {
102 return DeclLink(DeclLink::LatestLink, Ctx);
105 /// \brief Points to the next redeclaration in the chain.
107 /// If NextIsPrevious() is true, this is a link to the previous declaration
108 /// of this same Decl. If NextIsLatest() is true, this is the first
109 /// declaration and Link points to the latest declaration. For example:
111 /// #1 int f(int x, int y = 1); // <pointer to #3, true>
112 /// #2 int f(int x = 0, int y); // <pointer to #1, false>
113 /// #3 int f(int x, int y) { return x + y; } // <pointer to #2, false>
115 /// If there is only one declaration, it is <pointer to self, true>
118 decl_type *getNextRedeclaration() const {
119 return RedeclLink.getNext(static_cast<const decl_type *>(this));
123 Redeclarable(const ASTContext &Ctx)
124 : RedeclLink(LatestDeclLink(Ctx)) {}
126 /// \brief Return the previous declaration of this declaration or NULL if this
127 /// is the first declaration.
128 decl_type *getPreviousDecl() {
129 if (RedeclLink.NextIsPrevious())
130 return getNextRedeclaration();
133 const decl_type *getPreviousDecl() const {
134 return const_cast<decl_type *>(
135 static_cast<const decl_type*>(this))->getPreviousDecl();
138 /// \brief Return the first declaration of this declaration or itself if this
139 /// is the only declaration.
140 decl_type *getFirstDecl() {
141 decl_type *D = static_cast<decl_type*>(this);
142 while (D->getPreviousDecl())
143 D = D->getPreviousDecl();
147 /// \brief Return the first declaration of this declaration or itself if this
148 /// is the only declaration.
149 const decl_type *getFirstDecl() const {
150 const decl_type *D = static_cast<const decl_type*>(this);
151 while (D->getPreviousDecl())
152 D = D->getPreviousDecl();
156 /// \brief True if this is the first declaration in its redeclaration chain.
157 bool isFirstDecl() const { return RedeclLink.NextIsLatest(); }
159 /// \brief Returns the most recent (re)declaration of this declaration.
160 decl_type *getMostRecentDecl() {
161 return getFirstDecl()->getNextRedeclaration();
164 /// \brief Returns the most recent (re)declaration of this declaration.
165 const decl_type *getMostRecentDecl() const {
166 return getFirstDecl()->getNextRedeclaration();
169 /// \brief Set the previous declaration. If PrevDecl is NULL, set this as the
170 /// first and only declaration.
171 void setPreviousDecl(decl_type *PrevDecl);
173 /// \brief Iterates through all the redeclarations of the same decl.
174 class redecl_iterator {
175 /// Current - The current declaration.
181 typedef decl_type* value_type;
182 typedef decl_type* reference;
183 typedef decl_type* pointer;
184 typedef std::forward_iterator_tag iterator_category;
185 typedef std::ptrdiff_t difference_type;
187 redecl_iterator() : Current(nullptr) { }
188 explicit redecl_iterator(decl_type *C)
189 : Current(C), Starter(C), PassedFirst(false) { }
191 reference operator*() const { return Current; }
192 pointer operator->() const { return Current; }
194 redecl_iterator& operator++() {
195 assert(Current && "Advancing while iterator has reached end");
196 // Sanity check to avoid infinite loop on invalid redecl chain.
197 if (Current->isFirstDecl()) {
199 assert(0 && "Passed first decl twice, invalid redecl chain!");
206 // Get either previous decl or latest decl.
207 decl_type *Next = Current->getNextRedeclaration();
208 Current = (Next != Starter) ? Next : nullptr;
212 redecl_iterator operator++(int) {
213 redecl_iterator tmp(*this);
218 friend bool operator==(redecl_iterator x, redecl_iterator y) {
219 return x.Current == y.Current;
221 friend bool operator!=(redecl_iterator x, redecl_iterator y) {
222 return x.Current != y.Current;
226 typedef llvm::iterator_range<redecl_iterator> redecl_range;
228 /// \brief Returns an iterator range for all the redeclarations of the same
229 /// decl. It will iterate at least once (when this decl is the only one).
230 redecl_range redecls() const {
231 return redecl_range(redecl_iterator(const_cast<decl_type *>(
232 static_cast<const decl_type *>(this))),
236 redecl_iterator redecls_begin() const { return redecls().begin(); }
237 redecl_iterator redecls_end() const { return redecls().end(); }
239 friend class ASTDeclReader;
240 friend class ASTDeclWriter;
243 /// \brief Get the primary declaration for a declaration from an AST file. That
244 /// will be the first-loaded declaration.
245 Decl *getPrimaryMergedDecl(Decl *D);
247 /// \brief Provides common interface for the Decls that cannot be redeclared,
248 /// but can be merged if the same declaration is brought in from multiple
250 template<typename decl_type>
255 /// \brief Return the first declaration of this declaration or itself if this
256 /// is the only declaration.
257 decl_type *getFirstDecl() {
258 decl_type *D = static_cast<decl_type*>(this);
259 if (!D->isFromASTFile())
261 return cast<decl_type>(getPrimaryMergedDecl(const_cast<decl_type*>(D)));
264 /// \brief Return the first declaration of this declaration or itself if this
265 /// is the only declaration.
266 const decl_type *getFirstDecl() const {
267 const decl_type *D = static_cast<const decl_type*>(this);
268 if (!D->isFromASTFile())
270 return cast<decl_type>(getPrimaryMergedDecl(const_cast<decl_type*>(D)));
273 /// \brief Returns true if this is the first declaration.
274 bool isFirstDecl() const { return getFirstDecl() == this; }