]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - contrib/llvm/tools/clang/include/clang/Sema/IdentifierResolver.h
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[FreeBSD/releng/10.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     /// \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 3 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 & 0x3 == 0x1)
80     /// 3) A IdDeclInfo::DeclsTy::iterator that traverses the decls of parent
81     ///    declaration contexts too. (Ptr & 0x3 == 0x3)
82     uintptr_t Ptr;
83     typedef IdDeclInfo::DeclsTy::iterator BaseIter;
84
85     /// A single NamedDecl. (Ptr & 0x1 == 0)
86     iterator(NamedDecl *D) {
87       Ptr = reinterpret_cast<uintptr_t>(D);
88       assert((Ptr & 0x1) == 0 && "Invalid Ptr!");
89     }
90     /// A IdDeclInfo::DeclsTy::iterator that walks or not the parent declaration
91     /// contexts depending on 'LookInParentCtx'.
92     iterator(BaseIter I) {
93       Ptr = reinterpret_cast<uintptr_t>(I) | 0x1;
94     }
95
96     bool isIterator() const { return (Ptr & 0x1); }
97
98     BaseIter getIterator() const {
99       assert(isIterator() && "Ptr not an iterator!");
100       return reinterpret_cast<BaseIter>(Ptr & ~0x3);
101     }
102
103     friend class IdentifierResolver;
104
105     void incrementSlowCase();
106   public:
107     iterator() : Ptr(0) {}
108
109     NamedDecl *operator*() const {
110       if (isIterator())
111         return *getIterator();
112       else
113         return reinterpret_cast<NamedDecl*>(Ptr);
114     }
115
116     bool operator==(const iterator &RHS) const {
117       return Ptr == RHS.Ptr;
118     }
119     bool operator!=(const iterator &RHS) const {
120       return Ptr != RHS.Ptr;
121     }
122
123     // Preincrement.
124     iterator& operator++() {
125       if (!isIterator()) // common case.
126         Ptr = 0;
127       else
128         incrementSlowCase();
129       return *this;
130     }
131
132     uintptr_t getAsOpaqueValue() const { return Ptr; }
133
134     static iterator getFromOpaqueValue(uintptr_t P) {
135       iterator Result;
136       Result.Ptr = P;
137       return Result;
138     }
139   };
140
141   /// begin - Returns an iterator for decls with the name 'Name'.
142   iterator begin(DeclarationName Name);
143
144   /// end - Returns an iterator that has 'finished'.
145   iterator end() {
146     return iterator();
147   }
148
149   /// isDeclInScope - If 'Ctx' is a function/method, isDeclInScope returns true
150   /// if 'D' is in Scope 'S', otherwise 'S' is ignored and isDeclInScope returns
151   /// true if 'D' belongs to the given declaration context.
152   ///
153   /// \param ExplicitInstantiationOrSpecialization When true, we are checking
154   /// whether the declaration is in scope for the purposes of explicit template
155   /// instantiation or specialization. The default is false.
156   bool isDeclInScope(Decl *D, DeclContext *Ctx, Scope *S = 0,
157                      bool ExplicitInstantiationOrSpecialization = false) const;
158
159   /// AddDecl - Link the decl to its shadowed decl chain.
160   void AddDecl(NamedDecl *D);
161
162   /// RemoveDecl - Unlink the decl from its shadowed decl chain.
163   /// The decl must already be part of the decl chain.
164   void RemoveDecl(NamedDecl *D);
165
166   /// \brief Insert the given declaration after the given iterator
167   /// position.
168   void InsertDeclAfter(iterator Pos, NamedDecl *D);
169
170   /// \brief Try to add the given declaration to the top level scope, if it
171   /// (or a redeclaration of it) hasn't already been added.
172   ///
173   /// \param D The externally-produced declaration to add.
174   ///
175   /// \param Name The name of the externally-produced declaration.
176   ///
177   /// \returns true if the declaration was added, false otherwise.
178   bool tryAddTopLevelDecl(NamedDecl *D, DeclarationName Name);
179   
180   explicit IdentifierResolver(Preprocessor &PP);
181   ~IdentifierResolver();
182
183 private:
184   const LangOptions &LangOpt;
185   Preprocessor &PP;
186   
187   class IdDeclInfoMap;
188   IdDeclInfoMap *IdDeclInfos;
189
190   void updatingIdentifier(IdentifierInfo &II);
191   void readingIdentifier(IdentifierInfo &II);
192   
193   /// FETokenInfo contains a Decl pointer if lower bit == 0.
194   static inline bool isDeclPtr(void *Ptr) {
195     return (reinterpret_cast<uintptr_t>(Ptr) & 0x1) == 0;
196   }
197
198   /// FETokenInfo contains a IdDeclInfo pointer if lower bit == 1.
199   static inline IdDeclInfo *toIdDeclInfo(void *Ptr) {
200     assert((reinterpret_cast<uintptr_t>(Ptr) & 0x1) == 1
201           && "Ptr not a IdDeclInfo* !");
202     return reinterpret_cast<IdDeclInfo*>(
203                     reinterpret_cast<uintptr_t>(Ptr) & ~0x1
204                                                             );
205   }
206 };
207
208 } // end namespace clang
209
210 #endif