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 /// \brief Insert the given declaration at the given position in the list.
55 void InsertDecl(DeclsTy::iterator Pos, NamedDecl *D) {
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.
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;
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)
83 typedef IdDeclInfo::DeclsTy::iterator BaseIter;
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!");
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;
96 bool isIterator() const { return (Ptr & 0x1); }
98 BaseIter getIterator() const {
99 assert(isIterator() && "Ptr not an iterator!");
100 return reinterpret_cast<BaseIter>(Ptr & ~0x3);
103 friend class IdentifierResolver;
105 void incrementSlowCase();
107 iterator() : Ptr(0) {}
109 NamedDecl *operator*() const {
111 return *getIterator();
113 return reinterpret_cast<NamedDecl*>(Ptr);
116 bool operator==(const iterator &RHS) const {
117 return Ptr == RHS.Ptr;
119 bool operator!=(const iterator &RHS) const {
120 return Ptr != RHS.Ptr;
124 iterator& operator++() {
125 if (!isIterator()) // common case.
132 uintptr_t getAsOpaqueValue() const { return Ptr; }
134 static iterator getFromOpaqueValue(uintptr_t P) {
141 /// begin - Returns an iterator for decls with the name 'Name'.
142 iterator begin(DeclarationName Name);
144 /// end - Returns an iterator that has 'finished'.
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.
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;
159 /// AddDecl - Link the decl to its shadowed decl chain.
160 void AddDecl(NamedDecl *D);
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);
166 /// \brief Insert the given declaration after the given iterator
168 void InsertDeclAfter(iterator Pos, NamedDecl *D);
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.
173 /// \param D The externally-produced declaration to add.
175 /// \param Name The name of the externally-produced declaration.
177 /// \returns true if the declaration was added, false otherwise.
178 bool tryAddTopLevelDecl(NamedDecl *D, DeclarationName Name);
180 explicit IdentifierResolver(Preprocessor &PP);
181 ~IdentifierResolver();
184 const LangOptions &LangOpt;
188 IdDeclInfoMap *IdDeclInfos;
190 void updatingIdentifier(IdentifierInfo &II);
191 void readingIdentifier(IdentifierInfo &II);
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;
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
208 } // end namespace clang