ASTRecordLayoutBuilder is no longer a friend of ASTRecordLayout.
[clang.git] / lib / Sema / IdentifierResolver.h
blob59bd834073f3dae46e6649f2e857c83f6dd856e7
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.
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 "clang/Parse/Scope.h"
20 #include "clang/AST/Decl.h"
21 #include "clang/AST/DeclarationName.h"
22 #include "clang/AST/DeclCXX.h"
24 namespace clang {
26 /// IdentifierResolver - Keeps track of shadowed decls on enclosing
27 /// scopes. It manages the shadowing chains of declaration names and
28 /// implements efficent decl lookup based on a declaration name.
29 class IdentifierResolver {
31 /// IdDeclInfo - Keeps track of information about decls associated
32 /// to a particular declaration name. IdDeclInfos are lazily
33 /// constructed and assigned to a declaration name the first time a
34 /// decl with that declaration name is shadowed in some scope.
35 class IdDeclInfo {
36 public:
37 typedef llvm::SmallVector<NamedDecl*, 2> DeclsTy;
39 inline DeclsTy::iterator decls_begin() { return Decls.begin(); }
40 inline DeclsTy::iterator decls_end() { return Decls.end(); }
42 void AddDecl(NamedDecl *D) { Decls.push_back(D); }
44 /// RemoveDecl - Remove the decl from the scope chain.
45 /// The decl must already be part of the decl chain.
46 void RemoveDecl(NamedDecl *D);
48 /// Replaces the Old declaration with the New declaration. If the
49 /// replacement is successful, returns true. If the old
50 /// declaration was not found, returns false.
51 bool ReplaceDecl(NamedDecl *Old, NamedDecl *New);
53 private:
54 DeclsTy Decls;
57 public:
59 /// iterator - Iterate over the decls of a specified declaration name.
60 /// It will walk or not the parent declaration contexts depending on how
61 /// it was instantiated.
62 class iterator {
63 public:
64 typedef NamedDecl * value_type;
65 typedef NamedDecl * reference;
66 typedef NamedDecl * pointer;
67 typedef std::input_iterator_tag iterator_category;
68 typedef std::ptrdiff_t difference_type;
70 /// Ptr - There are 3 forms that 'Ptr' represents:
71 /// 1) A single NamedDecl. (Ptr & 0x1 == 0)
72 /// 2) A IdDeclInfo::DeclsTy::iterator that traverses only the decls of the
73 /// same declaration context. (Ptr & 0x3 == 0x1)
74 /// 3) A IdDeclInfo::DeclsTy::iterator that traverses the decls of parent
75 /// declaration contexts too. (Ptr & 0x3 == 0x3)
76 uintptr_t Ptr;
77 typedef IdDeclInfo::DeclsTy::iterator BaseIter;
79 /// A single NamedDecl. (Ptr & 0x1 == 0)
80 iterator(NamedDecl *D) {
81 Ptr = reinterpret_cast<uintptr_t>(D);
82 assert((Ptr & 0x1) == 0 && "Invalid Ptr!");
84 /// A IdDeclInfo::DeclsTy::iterator that walks or not the parent declaration
85 /// contexts depending on 'LookInParentCtx'.
86 iterator(BaseIter I) {
87 Ptr = reinterpret_cast<uintptr_t>(I) | 0x1;
90 bool isIterator() const { return (Ptr & 0x1); }
92 BaseIter getIterator() const {
93 assert(isIterator() && "Ptr not an iterator!");
94 return reinterpret_cast<BaseIter>(Ptr & ~0x3);
97 friend class IdentifierResolver;
98 public:
99 iterator() : Ptr(0) {}
101 NamedDecl *operator*() const {
102 if (isIterator())
103 return *getIterator();
104 else
105 return reinterpret_cast<NamedDecl*>(Ptr);
108 bool operator==(const iterator &RHS) const {
109 return Ptr == RHS.Ptr;
111 bool operator!=(const iterator &RHS) const {
112 return Ptr != RHS.Ptr;
115 // Preincrement.
116 iterator& operator++() {
117 if (!isIterator()) // common case.
118 Ptr = 0;
119 else {
120 NamedDecl *D = **this;
121 void *InfoPtr = D->getDeclName().getFETokenInfo<void>();
122 assert(!isDeclPtr(InfoPtr) && "Decl with wrong id ?");
123 IdDeclInfo *Info = toIdDeclInfo(InfoPtr);
125 BaseIter I = getIterator();
126 if (I != Info->decls_begin())
127 *this = iterator(I-1);
128 else // No more decls.
129 *this = iterator();
131 return *this;
134 uintptr_t getAsOpaqueValue() const { return Ptr; }
136 static iterator getFromOpaqueValue(uintptr_t P) {
137 iterator Result;
138 Result.Ptr = P;
139 return Result;
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() {
148 return iterator();
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.
154 bool isDeclInScope(Decl *D, DeclContext *Ctx, ASTContext &Context,
155 Scope *S = 0) const;
157 /// AddDecl - Link the decl to its shadowed decl chain.
158 void AddDecl(NamedDecl *D);
160 /// RemoveDecl - Unlink the decl from its shadowed decl chain.
161 /// The decl must already be part of the decl chain.
162 void RemoveDecl(NamedDecl *D);
164 /// Replace the decl Old with the new declaration New on its
165 /// identifier chain. Returns true if the old declaration was found
166 /// (and, therefore, replaced).
167 bool ReplaceDecl(NamedDecl *Old, NamedDecl *New);
169 /// \brief Link the declaration into the chain of declarations for
170 /// the given identifier.
172 /// This is a lower-level routine used by the PCH reader to link a
173 /// declaration into a specific IdentifierInfo before the
174 /// declaration actually has a name.
175 void AddDeclToIdentifierChain(IdentifierInfo *II, NamedDecl *D);
177 explicit IdentifierResolver(const LangOptions &LangOpt);
178 ~IdentifierResolver();
180 private:
181 const LangOptions &LangOpt;
183 class IdDeclInfoMap;
184 IdDeclInfoMap *IdDeclInfos;
186 /// FETokenInfo contains a Decl pointer if lower bit == 0.
187 static inline bool isDeclPtr(void *Ptr) {
188 return (reinterpret_cast<uintptr_t>(Ptr) & 0x1) == 0;
191 /// FETokenInfo contains a IdDeclInfo pointer if lower bit == 1.
192 static inline IdDeclInfo *toIdDeclInfo(void *Ptr) {
193 assert((reinterpret_cast<uintptr_t>(Ptr) & 0x1) == 1
194 && "Ptr not a IdDeclInfo* !");
195 return reinterpret_cast<IdDeclInfo*>(
196 reinterpret_cast<uintptr_t>(Ptr) & ~0x1
201 } // end namespace clang
203 #endif