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, ASTContext &Context, 162 Scope *S = 0, 163 bool ExplicitInstantiationOrSpecialization = false) const; 164 165 /// AddDecl - Link the decl to its shadowed decl chain. 166 void AddDecl(NamedDecl *D); 167 168 /// RemoveDecl - Unlink the decl from its shadowed decl chain. 169 /// The decl must already be part of the decl chain. 170 void RemoveDecl(NamedDecl *D); 171 172 /// Replace the decl Old with the new declaration New on its 173 /// identifier chain. Returns true if the old declaration was found 174 /// (and, therefore, replaced). 175 bool ReplaceDecl(NamedDecl *Old, NamedDecl *New); 176 177 /// \brief Insert the given declaration after the given iterator 178 /// position. 179 void InsertDeclAfter(iterator Pos, NamedDecl *D); 180 181 /// \brief Try to add the given declaration to the top level scope, if it 182 /// (or a redeclaration of it) hasn't already been added. 183 /// 184 /// \param D The externally-produced declaration to add. 185 /// 186 /// \param Name The name of the externally-produced declaration. 187 /// 188 /// \returns true if the declaration was added, false otherwise. 189 bool tryAddTopLevelDecl(NamedDecl *D, DeclarationName Name); 190 191 explicit IdentifierResolver(Preprocessor &PP); 192 ~IdentifierResolver(); 193 194 private: 195 const LangOptions &LangOpt; 196 Preprocessor &PP; 197 198 class IdDeclInfoMap; 199 IdDeclInfoMap *IdDeclInfos; 200 201 void updatingIdentifier(IdentifierInfo &II); 202 void readingIdentifier(IdentifierInfo &II); 203 204 /// FETokenInfo contains a Decl pointer if lower bit == 0. 205 static inline bool isDeclPtr(void *Ptr) { 206 return (reinterpret_cast<uintptr_t>(Ptr) & 0x1) == 0; 207 } 208 209 /// FETokenInfo contains a IdDeclInfo pointer if lower bit == 1. 210 static inline IdDeclInfo *toIdDeclInfo(void *Ptr) { 211 assert((reinterpret_cast<uintptr_t>(Ptr) & 0x1) == 1 212 && "Ptr not a IdDeclInfo* !"); 213 return reinterpret_cast<IdDeclInfo*>( 214 reinterpret_cast<uintptr_t>(Ptr) & ~0x1 215 ); 216 } 217 }; 218 219 } // end namespace clang 220 221 #endif 222