1 //===- IdentifierResolver.h - Lexical Scope Name lookup ---------*- 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 IdentifierResolver class, which is used for lexical
11 // scoped lookup, based on declaration names.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_CLANG_AST_SEMA_IDENTIFIERRESOLVER_H
16 #define LLVM_CLANG_AST_SEMA_IDENTIFIERRESOLVER_H
18 #include "clang/Basic/IdentifierTable.h"
19 #include "llvm/ADT/SmallVector.h"
26 class DeclarationName;
27 class ExternalPreprocessorSource;
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 {
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.
43 typedef SmallVector<NamedDecl*, 2> DeclsTy;
45 inline DeclsTy::iterator decls_begin() { return Decls.begin(); }
46 inline DeclsTy::iterator decls_end() { return Decls.end(); }
48 void AddDecl(NamedDecl *D) { Decls.push_back(D); }
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);
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);
59 /// \brief Insert the given declaration at the given position in the list.
60 void InsertDecl(DeclsTy::iterator Pos, NamedDecl *D) {
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.
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;
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)
88 typedef IdDeclInfo::DeclsTy::iterator BaseIter;
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!");
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;
101 bool isIterator() const { return (Ptr & 0x1); }
103 BaseIter getIterator() const {
104 assert(isIterator() && "Ptr not an iterator!");
105 return reinterpret_cast<BaseIter>(Ptr & ~0x3);
108 friend class IdentifierResolver;
110 void incrementSlowCase();
112 iterator() : Ptr(0) {}
114 NamedDecl *operator*() const {
116 return *getIterator();
118 return reinterpret_cast<NamedDecl*>(Ptr);
121 bool operator==(const iterator &RHS) const {
122 return Ptr == RHS.Ptr;
124 bool operator!=(const iterator &RHS) const {
125 return Ptr != RHS.Ptr;
129 iterator& operator++() {
130 if (!isIterator()) // common case.
137 uintptr_t getAsOpaqueValue() const { return Ptr; }
139 static iterator getFromOpaqueValue(uintptr_t P) {
146 /// begin - Returns an iterator for decls with the name 'Name'.
147 iterator begin(DeclarationName Name);
149 /// end - Returns an iterator that has 'finished'.
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.
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;
164 /// AddDecl - Link the decl to its shadowed decl chain.
165 void AddDecl(NamedDecl *D);
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);
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);
176 /// \brief Insert the given declaration after the given iterator
178 void InsertDeclAfter(iterator Pos, NamedDecl *D);
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.
183 /// \param D The externally-produced declaration to add.
185 /// \param Name The name of the externally-produced declaration.
187 /// \returns true if the declaration was added, false otherwise.
188 bool tryAddTopLevelDecl(NamedDecl *D, DeclarationName Name);
190 explicit IdentifierResolver(Preprocessor &PP);
191 ~IdentifierResolver();
194 const LangOptions &LangOpt;
198 IdDeclInfoMap *IdDeclInfos;
200 void updatingIdentifier(IdentifierInfo &II);
201 void readingIdentifier(IdentifierInfo &II);
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;
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
218 } // end namespace clang