blob: 294904271cd7e2dc68d471f6b68db38b54a0e444 [file] [log] [blame]
Chris Lattnera2f42b12008-04-11 07:06:57 +00001//===- 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//
Argyrios Kyrtzidis321f2782008-04-12 01:50:47 +000010// This file defines the IdentifierResolver class, which is used for lexical
Chris Lattnera2f42b12008-04-11 07:06:57 +000011// scoped lookup, based on identifier.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_CLANG_AST_SEMA_IDENTIFIERRESOLVER_H
16#define LLVM_CLANG_AST_SEMA_IDENTIFIERRESOLVER_H
17
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +000018#include "clang/Basic/IdentifierTable.h"
19#include "clang/Parse/Scope.h"
20#include "clang/AST/Decl.h"
Argyrios Kyrtzidis32a5ba02008-06-24 23:08:34 +000021#include "clang/AST/DeclCXX.h"
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +000022
Chris Lattnera2f42b12008-04-11 07:06:57 +000023namespace clang {
Chris Lattnera2f42b12008-04-11 07:06:57 +000024
25/// IdentifierResolver - Keeps track of shadowed decls on enclosing scopes.
Argyrios Kyrtzidis321f2782008-04-12 01:50:47 +000026/// It manages the shadowing chains of identifiers and implements efficent decl
Chris Lattnera2f42b12008-04-11 07:06:57 +000027/// lookup based on an identifier.
28class IdentifierResolver {
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +000029
30 /// LookupContext - A wrapper for DeclContext. DeclContext is only part of
31 /// ScopedDecls, LookupContext can be used with all decls (assumes
32 /// translation unit context for non ScopedDecls).
33 class LookupContext {
34 DeclContext *Ctx;
35
36 /// TUCtx - Provides a common value for translation unit context for all
37 /// decls.
38 /// FIXME: When (if ?) all decls can point to their translation unit context
39 /// remove this hack.
40 static inline DeclContext *TUCtx() {
41 return reinterpret_cast<DeclContext*>(-1);
42 }
43
44 /// getContext - Returns translation unit context for non ScopedDecls and
45 /// for EnumConstantDecls returns the parent context of their EnumDecl.
46 static DeclContext *getContext(Decl *D) {
47 DeclContext *Ctx;
48
Argyrios Kyrtzidis32a5ba02008-06-24 23:08:34 +000049 if (CXXFieldDecl *FD = dyn_cast<CXXFieldDecl>(D))
50 return FD->getParent();
51
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +000052 if (EnumConstantDecl *EnumD = dyn_cast<EnumConstantDecl>(D)) {
53 Ctx = EnumD->getDeclContext()->getParent();
54 } else if (ScopedDecl *SD = dyn_cast<ScopedDecl>(D))
55 Ctx = SD->getDeclContext();
56 else
57 return TUCtx();
58
59 if (isa<TranslationUnitDecl>(Ctx))
60 return TUCtx();
61
62 return Ctx;
63 }
64
65 public:
66 LookupContext(Decl *D) {
67 Ctx = getContext(D);
68 }
69 LookupContext(DeclContext *DC) {
70 if (!DC || isa<TranslationUnitDecl>(DC))
71 Ctx = TUCtx();
72 else
73 Ctx = DC;
74 }
75
76 bool isTU() const {
77 return (Ctx == TUCtx());
78 }
79
80 /// getParent - Returns the parent context. This should not be called for
81 /// a translation unit context.
82 LookupContext getParent() const {
83 assert(!isTU() && "TU has no parent!");
84 return LookupContext(Ctx->getParent());
85 }
86
87 /// isEqOrContainedBy - Returns true of the given context is the same or a
88 /// parent of this one.
89 bool isEqOrContainedBy(const LookupContext &PC) const {
90 if (PC.isTU()) return true;
91
92 for (LookupContext Next = *this; !Next.isTU(); Next = Next.getParent())
93 if (Next.Ctx == PC.Ctx) return true;
94
95 return false;
96 }
97
98 bool operator==(const LookupContext &RHS) const {
99 return Ctx == RHS.Ctx;
100 }
101 bool operator!=(const LookupContext &RHS) const {
102 return Ctx != RHS.Ctx;
103 }
104 };
105
106 /// IdDeclInfo - Keeps track of information about decls associated to a
107 /// particular identifier. IdDeclInfos are lazily constructed and assigned
108 /// to an identifier the first time a decl with that identifier is shadowed
109 /// in some scope.
110 class IdDeclInfo {
111 public:
112 typedef llvm::SmallVector<NamedDecl*, 2> DeclsTy;
113
114 inline DeclsTy::iterator decls_begin() { return Decls.begin(); }
115 inline DeclsTy::iterator decls_end() { return Decls.end(); }
116
117 /// FindContext - Returns an iterator pointing just after the decl that is
118 /// in the given context or in a parent of it. The search is in reverse
119 /// order, from end to begin.
120 DeclsTy::iterator FindContext(const LookupContext &Ctx) {
121 return FindContext(Ctx, Decls.end());
122 }
123
124 /// FindContext - Returns an iterator pointing just after the decl that is
125 /// in the given context or in a parent of it. The search is in reverse
126 /// order, from end to begin.
127 DeclsTy::iterator FindContext(const LookupContext &Ctx,
128 const DeclsTy::iterator &Start) {
129 for (DeclsTy::iterator I = Start; I != Decls.begin(); --I) {
130 if (Ctx.isEqOrContainedBy(LookupContext(*(I-1))))
131 return I;
132 }
133
134 return Decls.begin();
135 }
136
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000137 void AddDecl(NamedDecl *D) {
138 Decls.insert(FindContext(LookupContext(D)), D);
139 }
140
141 /// AddShadowed - Add a decl by putting it directly above the 'Shadow' decl.
142 /// Later lookups will find the 'Shadow' decl first. The 'Shadow' decl must
143 /// be already added to the scope chain and must be in the same context as
144 /// the decl that we want to add.
145 void AddShadowed(NamedDecl *D, NamedDecl *Shadow) {
146 assert(LookupContext(D) == LookupContext(Shadow) &&
147 "Decl and Shadow not in same context!");
148
149 for (DeclsTy::iterator I = Decls.end(); I != Decls.begin(); --I) {
150 if (Shadow == *(I-1)) {
151 Decls.insert(I-1, D);
152 return;
153 }
154 }
155
156 assert(0 && "Shadow wasn't in scope chain!");
157 }
158
159 /// RemoveDecl - Remove the decl from the scope chain.
160 /// The decl must already be part of the decl chain.
161 void RemoveDecl(NamedDecl *D) {
162 for (DeclsTy::iterator I = Decls.end(); I != Decls.begin(); --I) {
163 if (D == *(I-1)) {
164 Decls.erase(I-1);
165 return;
166 }
167 }
168
169 assert(0 && "Didn't find this decl on its identifier's chain!");
170 }
171
172 private:
173 DeclsTy Decls;
174 };
175
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000176public:
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000177
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000178 /// iterator - Iterate over the decls of a specified identifier.
179 /// It will walk or not the parent declaration contexts depending on how
180 /// it was instantiated.
181 class iterator {
182 /// Ptr - There are 3 forms that 'Ptr' represents:
183 /// 1) A single NamedDecl. (Ptr & 0x1 == 0)
184 /// 2) A IdDeclInfo::DeclsTy::iterator that traverses only the decls of the
185 /// same declaration context. (Ptr & 0x3 == 0x1)
186 /// 3) A IdDeclInfo::DeclsTy::iterator that traverses the decls of parent
187 /// declaration contexts too. (Ptr & 0x3 == 0x3)
188 uintptr_t Ptr;
189 typedef IdDeclInfo::DeclsTy::iterator BaseIter;
190
191 iterator() : Ptr(0) {}
192 /// A single NamedDecl. (Ptr & 0x1 == 0)
193 iterator(NamedDecl *D) {
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000194 Ptr = reinterpret_cast<uintptr_t>(D);
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000195 assert((Ptr & 0x1) == 0 && "Invalid Ptr!");
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000196 }
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000197 /// A IdDeclInfo::DeclsTy::iterator that walks or not the parent declaration
198 /// contexts depending on 'LookInParentCtx'.
199 iterator(BaseIter I, bool LookInParentCtx) {
200 Ptr = reinterpret_cast<uintptr_t>(I) | 0x1;
201 assert((Ptr & 0x2) == 0 && "Invalid Ptr!");
202 if (LookInParentCtx) Ptr |= 0x2;
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000203 }
204
205 bool isIterator() const { return (Ptr & 0x1); }
206
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000207 bool LookInParentCtx() const {
208 assert(isIterator() && "Ptr not an iterator!");
209 return (Ptr & 0x2) != 0;
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000210 }
211
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000212 BaseIter getIterator() const {
213 assert(isIterator() && "Ptr not an iterator!");
214 return reinterpret_cast<BaseIter>(Ptr & ~0x3);
215 }
216
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000217 friend class IdentifierResolver;
218 public:
219 NamedDecl *operator*() const {
220 if (isIterator())
221 return *getIterator();
222 else
223 return reinterpret_cast<NamedDecl*>(Ptr);
224 }
225
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000226 bool operator==(const iterator &RHS) const {
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000227 return Ptr == RHS.Ptr;
228 }
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000229 bool operator!=(const iterator &RHS) const {
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000230 return Ptr != RHS.Ptr;
231 }
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000232
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000233 // Preincrement.
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000234 iterator& operator++() {
235 if (!isIterator()) // common case.
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000236 Ptr = 0;
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000237 else
238 PreIncIter();
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000239 return *this;
240 }
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000241
242 private:
243 void PreIncIter();
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000244 };
245
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000246 /// begin - Returns an iterator for decls of identifier 'II', starting at
247 /// declaration context 'Ctx'. If 'LookInParentCtx' is true, it will walk the
248 /// decls of parent declaration contexts too.
249 /// Default for 'LookInParentCtx is true.
250 static iterator begin(const IdentifierInfo *II, DeclContext *Ctx,
251 bool LookInParentCtx = true);
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000252
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000253 /// end - Returns an iterator that has 'finished'.
254 static iterator end() {
255 return iterator();
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000256 }
257
Argyrios Kyrtzidise29f0a42008-05-14 10:49:47 +0000258 /// isDeclInScope - If 'Ctx' is a function/method, isDeclInScope returns true
259 /// if 'D' is in Scope 'S', otherwise 'S' is ignored and isDeclInScope returns
260 /// true if 'D' belongs to the given declaration context.
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000261 static bool isDeclInScope(Decl *D, DeclContext *Ctx, Scope *S = 0) {
Argyrios Kyrtzidise29f0a42008-05-14 10:49:47 +0000262 if (Ctx->isFunctionOrMethod())
263 return S->isDeclScope(D);
264
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000265 return LookupContext(D) == LookupContext(Ctx);
266 }
Chris Lattnera2f42b12008-04-11 07:06:57 +0000267
Argyrios Kyrtzidis321f2782008-04-12 01:50:47 +0000268 /// AddDecl - Link the decl to its shadowed decl chain.
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000269 void AddDecl(NamedDecl *D);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000270
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000271 /// AddShadowedDecl - Link the decl to its shadowed decl chain putting it
Argyrios Kyrtzidis3d0d83a2008-05-15 17:26:35 +0000272 /// after the decl that the iterator points to, thus the 'Shadow' decl will be
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000273 /// encountered before the 'D' decl.
274 void AddShadowedDecl(NamedDecl *D, NamedDecl *Shadow);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000275
Argyrios Kyrtzidis321f2782008-04-12 01:50:47 +0000276 /// RemoveDecl - Unlink the decl from its shadowed decl chain.
Chris Lattnera2f42b12008-04-11 07:06:57 +0000277 /// The decl must already be part of the decl chain.
278 void RemoveDecl(NamedDecl *D);
279
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000280 IdentifierResolver();
281 ~IdentifierResolver();
Chris Lattnera2f42b12008-04-11 07:06:57 +0000282
283private:
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000284 class IdDeclInfoMap;
285 IdDeclInfoMap *IdDeclInfos;
286
287 /// Identifier's FETokenInfo contains a Decl pointer if lower bit == 0.
288 static inline bool isDeclPtr(void *Ptr) {
289 return (reinterpret_cast<uintptr_t>(Ptr) & 0x1) == 0;
290 }
291
292 /// Identifier's FETokenInfo contains a IdDeclInfo pointer if lower bit == 1.
293 static inline IdDeclInfo *toIdDeclInfo(void *Ptr) {
294 assert((reinterpret_cast<uintptr_t>(Ptr) & 0x1) == 1
295 && "Ptr not a IdDeclInfo* !");
296 return reinterpret_cast<IdDeclInfo*>(
297 reinterpret_cast<uintptr_t>(Ptr) & ~0x1
298 );
299 }
Chris Lattnera2f42b12008-04-11 07:06:57 +0000300};
301
302} // end namespace clang
303
304#endif