]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - contrib/llvm/tools/clang/include/clang/Sema/IdentifierResolver.h
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / contrib / llvm / tools / clang / 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_AST_SEMA_IDENTIFIERRESOLVER_H
16 #define LLVM_CLANG_AST_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     /// Replaces the Old declaration with the New declaration. If the
55     /// replacement is successful, returns true. If the old
56     /// declaration was not found, returns false.
57     bool ReplaceDecl(NamedDecl *Old, NamedDecl *New);
58
59     /// \brief Insert the given declaration at the given position in the list.
60     void InsertDecl(DeclsTy::iterator Pos, NamedDecl *D) {
61       Decls.insert(Pos, D);
62     }
63                     
64   private:
65     DeclsTy Decls;
66   };
67
68 public:
69
70   /// iterator - Iterate over the decls of a specified declaration name.
71   /// It will walk or not the parent declaration contexts depending on how
72   /// it was instantiated.
73   class iterator {
74   public:
75     typedef NamedDecl *             value_type;
76     typedef NamedDecl *             reference;
77     typedef NamedDecl *             pointer;
78     typedef std::input_iterator_tag iterator_category;
79     typedef std::ptrdiff_t          difference_type;
80
81     /// Ptr - There are 3 forms that 'Ptr' represents:
82     /// 1) A single NamedDecl. (Ptr & 0x1 == 0)
83     /// 2) A IdDeclInfo::DeclsTy::iterator that traverses only the decls of the
84     ///    same declaration context. (Ptr & 0x3 == 0x1)
85     /// 3) A IdDeclInfo::DeclsTy::iterator that traverses the decls of parent
86     ///    declaration contexts too. (Ptr & 0x3 == 0x3)
87     uintptr_t Ptr;
88     typedef IdDeclInfo::DeclsTy::iterator BaseIter;
89
90     /// A single NamedDecl. (Ptr & 0x1 == 0)
91     iterator(NamedDecl *D) {
92       Ptr = reinterpret_cast<uintptr_t>(D);
93       assert((Ptr & 0x1) == 0 && "Invalid Ptr!");
94     }
95     /// A IdDeclInfo::DeclsTy::iterator that walks or not the parent declaration
96     /// contexts depending on 'LookInParentCtx'.
97     iterator(BaseIter I) {
98       Ptr = reinterpret_cast<uintptr_t>(I) | 0x1;
99     }
100
101     bool isIterator() const { return (Ptr & 0x1); }
102
103     BaseIter getIterator() const {
104       assert(isIterator() && "Ptr not an iterator!");
105       return reinterpret_cast<BaseIter>(Ptr & ~0x3);
106     }
107
108     friend class IdentifierResolver;
109
110     void incrementSlowCase();
111   public:
112     iterator() : Ptr(0) {}
113
114     NamedDecl *operator*() const {
115       if (isIterator())
116         return *getIterator();
117       else
118         return reinterpret_cast<NamedDecl*>(Ptr);
119     }
120
121     bool operator==(const iterator &RHS) const {
122       return Ptr == RHS.Ptr;
123     }
124     bool operator!=(const iterator &RHS) const {
125       return Ptr != RHS.Ptr;
126     }
127
128     // Preincrement.
129     iterator& operator++() {
130       if (!isIterator()) // common case.
131         Ptr = 0;
132       else
133         incrementSlowCase();
134       return *this;
135     }
136
137     uintptr_t getAsOpaqueValue() const { return Ptr; }
138
139     static iterator getFromOpaqueValue(uintptr_t P) {
140       iterator Result;
141       Result.Ptr = P;
142       return Result;
143     }
144   };
145
146   /// begin - Returns an iterator for decls with the name 'Name'.
147   iterator begin(DeclarationName Name);
148
149   /// end - Returns an iterator that has 'finished'.
150   iterator end() {
151     return iterator();
152   }
153
154   /// isDeclInScope - If 'Ctx' is a function/method, isDeclInScope returns true
155   /// if 'D' is in Scope 'S', otherwise 'S' is ignored and isDeclInScope returns
156   /// true if 'D' belongs to the given declaration context.
157   ///
158   /// \param ExplicitInstantiationOrSpecialization When true, we are checking
159   /// whether the declaration is in scope for the purposes of explicit template
160   /// instantiation or specialization. The default is false.
161   bool isDeclInScope(Decl *D, DeclContext *Ctx, Scope *S = 0,
162                      bool ExplicitInstantiationOrSpecialization = false) const;
163
164   /// AddDecl - Link the decl to its shadowed decl chain.
165   void AddDecl(NamedDecl *D);
166
167   /// RemoveDecl - Unlink the decl from its shadowed decl chain.
168   /// The decl must already be part of the decl chain.
169   void RemoveDecl(NamedDecl *D);
170
171   /// Replace the decl Old with the new declaration New on its
172   /// identifier chain. Returns true if the old declaration was found
173   /// (and, therefore, replaced).
174   bool ReplaceDecl(NamedDecl *Old, NamedDecl *New);
175
176   /// \brief Insert the given declaration after the given iterator
177   /// position.
178   void InsertDeclAfter(iterator Pos, NamedDecl *D);
179
180   /// \brief Try to add the given declaration to the top level scope, if it
181   /// (or a redeclaration of it) hasn't already been added.
182   ///
183   /// \param D The externally-produced declaration to add.
184   ///
185   /// \param Name The name of the externally-produced declaration.
186   ///
187   /// \returns true if the declaration was added, false otherwise.
188   bool tryAddTopLevelDecl(NamedDecl *D, DeclarationName Name);
189   
190   explicit IdentifierResolver(Preprocessor &PP);
191   ~IdentifierResolver();
192
193 private:
194   const LangOptions &LangOpt;
195   Preprocessor &PP;
196   
197   class IdDeclInfoMap;
198   IdDeclInfoMap *IdDeclInfos;
199
200   void updatingIdentifier(IdentifierInfo &II);
201   void readingIdentifier(IdentifierInfo &II);
202   
203   /// FETokenInfo contains a Decl pointer if lower bit == 0.
204   static inline bool isDeclPtr(void *Ptr) {
205     return (reinterpret_cast<uintptr_t>(Ptr) & 0x1) == 0;
206   }
207
208   /// FETokenInfo contains a IdDeclInfo pointer if lower bit == 1.
209   static inline IdDeclInfo *toIdDeclInfo(void *Ptr) {
210     assert((reinterpret_cast<uintptr_t>(Ptr) & 0x1) == 1
211           && "Ptr not a IdDeclInfo* !");
212     return reinterpret_cast<IdDeclInfo*>(
213                     reinterpret_cast<uintptr_t>(Ptr) & ~0x1
214                                                             );
215   }
216 };
217
218 } // end namespace clang
219
220 #endif