]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - include/clang/Sema/IdentifierResolver.h
Vendor import of clang trunk r300422:
[FreeBSD/FreeBSD.git] / include / clang / Sema / IdentifierResolver.h
1 //===- IdentifierResolver.h - Lexical Scope Name lookup ---------*- 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 IdentifierResolver class, which is used for lexical
11 // scoped lookup, based on declaration names.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_CLANG_SEMA_IDENTIFIERRESOLVER_H
16 #define LLVM_CLANG_SEMA_IDENTIFIERRESOLVER_H
17
18 #include "clang/Basic/IdentifierTable.h"
19 #include "llvm/ADT/SmallVector.h"
20
21 namespace clang {
22
23 class ASTContext;
24 class Decl;
25 class DeclContext;
26 class DeclarationName;
27 class ExternalPreprocessorSource;
28 class NamedDecl;
29 class Preprocessor;
30 class Scope;
31   
32 /// IdentifierResolver - Keeps track of shadowed decls on enclosing
33 /// scopes.  It manages the shadowing chains of declaration names and
34 /// implements efficient decl lookup based on a declaration name.
35 class IdentifierResolver {
36
37   /// IdDeclInfo - Keeps track of information about decls associated
38   /// to a particular declaration name. IdDeclInfos are lazily
39   /// constructed and assigned to a declaration name the first time a
40   /// decl with that declaration name is shadowed in some scope.
41   class IdDeclInfo {
42   public:
43     typedef SmallVector<NamedDecl*, 2> DeclsTy;
44
45     inline DeclsTy::iterator decls_begin() { return Decls.begin(); }
46     inline DeclsTy::iterator decls_end() { return Decls.end(); }
47
48     void AddDecl(NamedDecl *D) { Decls.push_back(D); }
49
50     /// RemoveDecl - Remove the decl from the scope chain.
51     /// The decl must already be part of the decl chain.
52     void RemoveDecl(NamedDecl *D);
53
54     /// \brief Insert the given declaration at the given position in the list.
55     void InsertDecl(DeclsTy::iterator Pos, NamedDecl *D) {
56       Decls.insert(Pos, D);
57     }
58                     
59   private:
60     DeclsTy Decls;
61   };
62
63 public:
64
65   /// iterator - Iterate over the decls of a specified declaration name.
66   /// It will walk or not the parent declaration contexts depending on how
67   /// it was instantiated.
68   class iterator {
69   public:
70     typedef NamedDecl *             value_type;
71     typedef NamedDecl *             reference;
72     typedef NamedDecl *             pointer;
73     typedef std::input_iterator_tag iterator_category;
74     typedef std::ptrdiff_t          difference_type;
75
76     /// Ptr - There are 2 forms that 'Ptr' represents:
77     /// 1) A single NamedDecl. (Ptr & 0x1 == 0)
78     /// 2) A IdDeclInfo::DeclsTy::iterator that traverses only the decls of the
79     ///    same declaration context. (Ptr & 0x1 == 0x1)
80     uintptr_t Ptr;
81     typedef IdDeclInfo::DeclsTy::iterator BaseIter;
82
83     /// A single NamedDecl. (Ptr & 0x1 == 0)
84     iterator(NamedDecl *D) {
85       Ptr = reinterpret_cast<uintptr_t>(D);
86       assert((Ptr & 0x1) == 0 && "Invalid Ptr!");
87     }
88     /// A IdDeclInfo::DeclsTy::iterator that walks or not the parent declaration
89     /// contexts depending on 'LookInParentCtx'.
90     iterator(BaseIter I) {
91       Ptr = reinterpret_cast<uintptr_t>(I) | 0x1;
92     }
93
94     bool isIterator() const { return (Ptr & 0x1); }
95
96     BaseIter getIterator() const {
97       assert(isIterator() && "Ptr not an iterator!");
98       return reinterpret_cast<BaseIter>(Ptr & ~0x1);
99     }
100
101     friend class IdentifierResolver;
102
103     void incrementSlowCase();
104   public:
105     iterator() : Ptr(0) {}
106
107     NamedDecl *operator*() const {
108       if (isIterator())
109         return *getIterator();
110       else
111         return reinterpret_cast<NamedDecl*>(Ptr);
112     }
113
114     bool operator==(const iterator &RHS) const {
115       return Ptr == RHS.Ptr;
116     }
117     bool operator!=(const iterator &RHS) const {
118       return Ptr != RHS.Ptr;
119     }
120
121     // Preincrement.
122     iterator& operator++() {
123       if (!isIterator()) // common case.
124         Ptr = 0;
125       else
126         incrementSlowCase();
127       return *this;
128     }
129   };
130
131   /// begin - Returns an iterator for decls with the name 'Name'.
132   iterator begin(DeclarationName Name);
133
134   /// end - Returns an iterator that has 'finished'.
135   iterator end() {
136     return iterator();
137   }
138
139   /// isDeclInScope - If 'Ctx' is a function/method, isDeclInScope returns true
140   /// if 'D' is in Scope 'S', otherwise 'S' is ignored and isDeclInScope returns
141   /// true if 'D' belongs to the given declaration context.
142   ///
143   /// \param AllowInlineNamespace If \c true, we are checking whether a prior
144   ///        declaration is in scope in a declaration that requires a prior
145   ///        declaration (because it is either explicitly qualified or is a
146   ///        template instantiation or specialization). In this case, a
147   ///        declaration is in scope if it's in the inline namespace set of the
148   ///        context.
149   bool isDeclInScope(Decl *D, DeclContext *Ctx, Scope *S = nullptr,
150                      bool AllowInlineNamespace = false) const;
151
152   /// AddDecl - Link the decl to its shadowed decl chain.
153   void AddDecl(NamedDecl *D);
154
155   /// RemoveDecl - Unlink the decl from its shadowed decl chain.
156   /// The decl must already be part of the decl chain.
157   void RemoveDecl(NamedDecl *D);
158
159   /// \brief Insert the given declaration after the given iterator
160   /// position.
161   void InsertDeclAfter(iterator Pos, NamedDecl *D);
162
163   /// \brief Try to add the given declaration to the top level scope, if it
164   /// (or a redeclaration of it) hasn't already been added.
165   ///
166   /// \param D The externally-produced declaration to add.
167   ///
168   /// \param Name The name of the externally-produced declaration.
169   ///
170   /// \returns true if the declaration was added, false otherwise.
171   bool tryAddTopLevelDecl(NamedDecl *D, DeclarationName Name);
172   
173   explicit IdentifierResolver(Preprocessor &PP);
174   ~IdentifierResolver();
175
176 private:
177   const LangOptions &LangOpt;
178   Preprocessor &PP;
179   
180   class IdDeclInfoMap;
181   IdDeclInfoMap *IdDeclInfos;
182
183   void updatingIdentifier(IdentifierInfo &II);
184   void readingIdentifier(IdentifierInfo &II);
185   
186   /// FETokenInfo contains a Decl pointer if lower bit == 0.
187   static inline bool isDeclPtr(void *Ptr) {
188     return (reinterpret_cast<uintptr_t>(Ptr) & 0x1) == 0;
189   }
190
191   /// FETokenInfo contains a IdDeclInfo pointer if lower bit == 1.
192   static inline IdDeclInfo *toIdDeclInfo(void *Ptr) {
193     assert((reinterpret_cast<uintptr_t>(Ptr) & 0x1) == 1
194           && "Ptr not a IdDeclInfo* !");
195     return reinterpret_cast<IdDeclInfo*>(
196                     reinterpret_cast<uintptr_t>(Ptr) & ~0x1
197                                                             );
198   }
199 };
200
201 } // end namespace clang
202
203 #endif