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"
25 class DeclarationName;
29 /// IdentifierResolver - Keeps track of shadowed decls on enclosing
30 /// scopes. It manages the shadowing chains of declaration names and
31 /// implements efficient decl lookup based on a declaration name.
32 class IdentifierResolver {
34 /// IdDeclInfo - Keeps track of information about decls associated
35 /// to a particular declaration name. IdDeclInfos are lazily
36 /// constructed and assigned to a declaration name the first time a
37 /// decl with that declaration name is shadowed in some scope.
40 typedef SmallVector<NamedDecl*, 2> DeclsTy;
42 inline DeclsTy::iterator decls_begin() { return Decls.begin(); }
43 inline DeclsTy::iterator decls_end() { return Decls.end(); }
45 void AddDecl(NamedDecl *D) { Decls.push_back(D); }
47 /// RemoveDecl - Remove the decl from the scope chain.
48 /// The decl must already be part of the decl chain.
49 void RemoveDecl(NamedDecl *D);
51 /// Replaces the Old declaration with the New declaration. If the
52 /// replacement is successful, returns true. If the old
53 /// declaration was not found, returns false.
54 bool ReplaceDecl(NamedDecl *Old, NamedDecl *New);
56 /// \brief Insert the given declaration at the given position in the list.
57 void InsertDecl(DeclsTy::iterator Pos, NamedDecl *D) {
67 /// iterator - Iterate over the decls of a specified declaration name.
68 /// It will walk or not the parent declaration contexts depending on how
69 /// it was instantiated.
72 typedef NamedDecl * value_type;
73 typedef NamedDecl * reference;
74 typedef NamedDecl * pointer;
75 typedef std::input_iterator_tag iterator_category;
76 typedef std::ptrdiff_t difference_type;
78 /// Ptr - There are 3 forms that 'Ptr' represents:
79 /// 1) A single NamedDecl. (Ptr & 0x1 == 0)
80 /// 2) A IdDeclInfo::DeclsTy::iterator that traverses only the decls of the
81 /// same declaration context. (Ptr & 0x3 == 0x1)
82 /// 3) A IdDeclInfo::DeclsTy::iterator that traverses the decls of parent
83 /// declaration contexts too. (Ptr & 0x3 == 0x3)
85 typedef IdDeclInfo::DeclsTy::iterator BaseIter;
87 /// A single NamedDecl. (Ptr & 0x1 == 0)
88 iterator(NamedDecl *D) {
89 Ptr = reinterpret_cast<uintptr_t>(D);
90 assert((Ptr & 0x1) == 0 && "Invalid Ptr!");
92 /// A IdDeclInfo::DeclsTy::iterator that walks or not the parent declaration
93 /// contexts depending on 'LookInParentCtx'.
94 iterator(BaseIter I) {
95 Ptr = reinterpret_cast<uintptr_t>(I) | 0x1;
98 bool isIterator() const { return (Ptr & 0x1); }
100 BaseIter getIterator() const {
101 assert(isIterator() && "Ptr not an iterator!");
102 return reinterpret_cast<BaseIter>(Ptr & ~0x3);
105 friend class IdentifierResolver;
107 void incrementSlowCase();
109 iterator() : Ptr(0) {}
111 NamedDecl *operator*() const {
113 return *getIterator();
115 return reinterpret_cast<NamedDecl*>(Ptr);
118 bool operator==(const iterator &RHS) const {
119 return Ptr == RHS.Ptr;
121 bool operator!=(const iterator &RHS) const {
122 return Ptr != RHS.Ptr;
126 iterator& operator++() {
127 if (!isIterator()) // common case.
134 uintptr_t getAsOpaqueValue() const { return Ptr; }
136 static iterator getFromOpaqueValue(uintptr_t P) {
143 /// begin - Returns an iterator for decls with the name 'Name'.
144 static iterator begin(DeclarationName Name);
146 /// end - Returns an iterator that has 'finished'.
147 static iterator end() {
151 /// isDeclInScope - If 'Ctx' is a function/method, isDeclInScope returns true
152 /// if 'D' is in Scope 'S', otherwise 'S' is ignored and isDeclInScope returns
153 /// true if 'D' belongs to the given declaration context.
155 /// \param ExplicitInstantiationOrSpecialization When true, we are checking
156 /// whether the declaration is in scope for the purposes of explicit template
157 /// instantiation or specialization. The default is false.
158 bool isDeclInScope(Decl *D, DeclContext *Ctx, ASTContext &Context,
160 bool ExplicitInstantiationOrSpecialization = false) const;
162 /// AddDecl - Link the decl to its shadowed decl chain.
163 void AddDecl(NamedDecl *D);
165 /// RemoveDecl - Unlink the decl from its shadowed decl chain.
166 /// The decl must already be part of the decl chain.
167 void RemoveDecl(NamedDecl *D);
169 /// Replace the decl Old with the new declaration New on its
170 /// identifier chain. Returns true if the old declaration was found
171 /// (and, therefore, replaced).
172 bool ReplaceDecl(NamedDecl *Old, NamedDecl *New);
174 /// \brief Insert the given declaration after the given iterator
176 void InsertDeclAfter(iterator Pos, NamedDecl *D);
178 /// \brief Link the declaration into the chain of declarations for
179 /// the given identifier.
181 /// This is a lower-level routine used by the AST reader to link a
182 /// declaration into a specific IdentifierInfo before the
183 /// declaration actually has a name.
184 void AddDeclToIdentifierChain(IdentifierInfo *II, NamedDecl *D);
186 explicit IdentifierResolver(const LangOptions &LangOpt);
187 ~IdentifierResolver();
190 const LangOptions &LangOpt;
193 IdDeclInfoMap *IdDeclInfos;
195 /// FETokenInfo contains a Decl pointer if lower bit == 0.
196 static inline bool isDeclPtr(void *Ptr) {
197 return (reinterpret_cast<uintptr_t>(Ptr) & 0x1) == 0;
200 /// FETokenInfo contains a IdDeclInfo pointer if lower bit == 1.
201 static inline IdDeclInfo *toIdDeclInfo(void *Ptr) {
202 assert((reinterpret_cast<uintptr_t>(Ptr) & 0x1) == 1
203 && "Ptr not a IdDeclInfo* !");
204 return reinterpret_cast<IdDeclInfo*>(
205 reinterpret_cast<uintptr_t>(Ptr) & ~0x1
210 } // end namespace clang