• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===-- Redeclarable.h - Base for Decls that can be redeclared -*- C++ -*-====//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 //  This file defines the Redeclarable interface.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_CLANG_AST_REDECLARABLE_H
15 #define LLVM_CLANG_AST_REDECLARABLE_H
16 
17 #include "clang/AST/ExternalASTSource.h"
18 #include "llvm/ADT/PointerIntPair.h"
19 #include "llvm/Support/Casting.h"
20 #include <iterator>
21 
22 namespace clang {
23 
24 /// \brief Provides common interface for the Decls that can be redeclared.
25 template<typename decl_type>
26 class Redeclarable {
27 protected:
28   class DeclLink {
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>
33                                           KnownLatest;
34 
35     typedef const ASTContext *UninitializedLatest;
36     typedef Decl *Previous;
37 
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;
42 
43     mutable llvm::PointerUnion<NotKnownLatest, KnownLatest> Next;
44 
45   public:
46     enum PreviousTag { PreviousLink };
47     enum LatestTag { LatestLink };
48 
DeclLink(LatestTag,const ASTContext & Ctx)49     DeclLink(LatestTag, const ASTContext &Ctx)
50         : Next(NotKnownLatest(&Ctx)) {}
DeclLink(PreviousTag,decl_type * D)51     DeclLink(PreviousTag, decl_type *D)
52         : Next(NotKnownLatest(Previous(D))) {}
53 
NextIsPrevious()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>();
59     }
60 
NextIsLatest()61     bool NextIsLatest() const { return !NextIsPrevious(); }
62 
getNext(const decl_type * D)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>());
68 
69         // Allocate the generational 'most recent' cache now, if needed.
70         Next = KnownLatest(*NKL.get<UninitializedLatest>(),
71                            const_cast<decl_type *>(D));
72       }
73 
74       return static_cast<decl_type*>(Next.get<KnownLatest>().get(D));
75     }
76 
setPrevious(decl_type * D)77     void setPrevious(decl_type *D) {
78       assert(NextIsPrevious() && "decl became non-canonical unexpectedly");
79       Next = Previous(D);
80     }
81 
setLatest(decl_type * D)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);
87       } else {
88         auto Latest = Next.get<KnownLatest>();
89         Latest.set(D);
90         Next = Latest;
91       }
92     }
93 
markIncomplete()94     void markIncomplete() { Next.get<KnownLatest>().markIncomplete(); }
95   };
96 
PreviousDeclLink(decl_type * D)97   static DeclLink PreviousDeclLink(decl_type *D) {
98     return DeclLink(DeclLink::PreviousLink, D);
99   }
100 
LatestDeclLink(const ASTContext & Ctx)101   static DeclLink LatestDeclLink(const ASTContext &Ctx) {
102     return DeclLink(DeclLink::LatestLink, Ctx);
103   }
104 
105   /// \brief Points to the next redeclaration in the chain.
106   ///
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:
110   ///
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>
114   ///
115   /// If there is only one declaration, it is <pointer to self, true>
116   DeclLink RedeclLink;
117 
getNextRedeclaration()118   decl_type *getNextRedeclaration() const {
119     return RedeclLink.getNext(static_cast<const decl_type *>(this));
120   }
121 
122 public:
Redeclarable(const ASTContext & Ctx)123   Redeclarable(const ASTContext &Ctx)
124       : RedeclLink(LatestDeclLink(Ctx)) {}
125 
126   /// \brief Return the previous declaration of this declaration or NULL if this
127   /// is the first declaration.
getPreviousDecl()128   decl_type *getPreviousDecl() {
129     if (RedeclLink.NextIsPrevious())
130       return getNextRedeclaration();
131     return nullptr;
132   }
getPreviousDecl()133   const decl_type *getPreviousDecl() const {
134     return const_cast<decl_type *>(
135                  static_cast<const decl_type*>(this))->getPreviousDecl();
136   }
137 
138   /// \brief Return the first declaration of this declaration or itself if this
139   /// is the only declaration.
getFirstDecl()140   decl_type *getFirstDecl() {
141     decl_type *D = static_cast<decl_type*>(this);
142     while (D->getPreviousDecl())
143       D = D->getPreviousDecl();
144     return D;
145   }
146 
147   /// \brief Return the first declaration of this declaration or itself if this
148   /// is the only declaration.
getFirstDecl()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();
153     return D;
154   }
155 
156   /// \brief True if this is the first declaration in its redeclaration chain.
isFirstDecl()157   bool isFirstDecl() const { return RedeclLink.NextIsLatest(); }
158 
159   /// \brief Returns the most recent (re)declaration of this declaration.
getMostRecentDecl()160   decl_type *getMostRecentDecl() {
161     return getFirstDecl()->getNextRedeclaration();
162   }
163 
164   /// \brief Returns the most recent (re)declaration of this declaration.
getMostRecentDecl()165   const decl_type *getMostRecentDecl() const {
166     return getFirstDecl()->getNextRedeclaration();
167   }
168 
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);
172 
173   /// \brief Iterates through all the redeclarations of the same decl.
174   class redecl_iterator {
175     /// Current - The current declaration.
176     decl_type *Current;
177     decl_type *Starter;
178     bool PassedFirst;
179 
180   public:
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;
186 
redecl_iterator()187     redecl_iterator() : Current(nullptr) { }
redecl_iterator(decl_type * C)188     explicit redecl_iterator(decl_type *C)
189       : Current(C), Starter(C), PassedFirst(false) { }
190 
191     reference operator*() const { return Current; }
192     pointer operator->() const { return Current; }
193 
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()) {
198         if (PassedFirst) {
199           assert(0 && "Passed first decl twice, invalid redecl chain!");
200           Current = nullptr;
201           return *this;
202         }
203         PassedFirst = true;
204       }
205 
206       // Get either previous decl or latest decl.
207       decl_type *Next = Current->getNextRedeclaration();
208       Current = (Next != Starter) ? Next : nullptr;
209       return *this;
210     }
211 
212     redecl_iterator operator++(int) {
213       redecl_iterator tmp(*this);
214       ++(*this);
215       return tmp;
216     }
217 
218     friend bool operator==(redecl_iterator x, redecl_iterator y) {
219       return x.Current == y.Current;
220     }
221     friend bool operator!=(redecl_iterator x, redecl_iterator y) {
222       return x.Current != y.Current;
223     }
224   };
225 
226   typedef llvm::iterator_range<redecl_iterator> redecl_range;
227 
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).
redecls()230   redecl_range redecls() const {
231     return redecl_range(redecl_iterator(const_cast<decl_type *>(
232                             static_cast<const decl_type *>(this))),
233                         redecl_iterator());
234   }
235 
redecls_begin()236   redecl_iterator redecls_begin() const { return redecls().begin(); }
redecls_end()237   redecl_iterator redecls_end() const { return redecls().end(); }
238 
239   friend class ASTDeclReader;
240   friend class ASTDeclWriter;
241 };
242 
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);
246 
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
249 /// modules.
250 template<typename decl_type>
251 class Mergeable {
252 public:
Mergeable()253   Mergeable() {}
254 
255   /// \brief Return the first declaration of this declaration or itself if this
256   /// is the only declaration.
getFirstDecl()257   decl_type *getFirstDecl() {
258     decl_type *D = static_cast<decl_type*>(this);
259     if (!D->isFromASTFile())
260       return D;
261     return cast<decl_type>(getPrimaryMergedDecl(const_cast<decl_type*>(D)));
262   }
263 
264   /// \brief Return the first declaration of this declaration or itself if this
265   /// is the only declaration.
getFirstDecl()266   const decl_type *getFirstDecl() const {
267     const decl_type *D = static_cast<const decl_type*>(this);
268     if (!D->isFromASTFile())
269       return D;
270     return cast<decl_type>(getPrimaryMergedDecl(const_cast<decl_type*>(D)));
271   }
272 
273   /// \brief Returns true if this is the first declaration.
isFirstDecl()274   bool isFirstDecl() const { return getFirstDecl() == this; }
275 };
276 
277 }
278 
279 #endif
280