blob: 30846dcd1744963fcf026e85e3f7f893a99325bf [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
Douglas Gregor2def4832008-11-17 20:34:05 +000011// scoped lookup, based on declaration names.
Chris Lattnera2f42b12008-04-11 07:06:57 +000012//
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"
Douglas Gregor2def4832008-11-17 20:34:05 +000021#include "clang/AST/DeclarationName.h"
Argyrios Kyrtzidis32a5ba02008-06-24 23:08:34 +000022#include "clang/AST/DeclCXX.h"
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +000023
Chris Lattnera2f42b12008-04-11 07:06:57 +000024namespace clang {
Chris Lattnera2f42b12008-04-11 07:06:57 +000025
Douglas Gregor2def4832008-11-17 20:34:05 +000026/// 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.
Chris Lattnera2f42b12008-04-11 07:06:57 +000029class IdentifierResolver {
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +000030
31 /// LookupContext - A wrapper for DeclContext. DeclContext is only part of
32 /// ScopedDecls, LookupContext can be used with all decls (assumes
33 /// translation unit context for non ScopedDecls).
34 class LookupContext {
Sebastian Redlc42e1182008-11-11 11:37:55 +000035 const DeclContext *Ctx;
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +000036
37 /// TUCtx - Provides a common value for translation unit context for all
38 /// decls.
39 /// FIXME: When (if ?) all decls can point to their translation unit context
40 /// remove this hack.
41 static inline DeclContext *TUCtx() {
42 return reinterpret_cast<DeclContext*>(-1);
43 }
44
45 /// getContext - Returns translation unit context for non ScopedDecls and
46 /// for EnumConstantDecls returns the parent context of their EnumDecl.
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +000047 static DeclContext *getContext(Decl *D);
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +000048
49 public:
50 LookupContext(Decl *D) {
51 Ctx = getContext(D);
52 }
Sebastian Redlc42e1182008-11-11 11:37:55 +000053 LookupContext(const DeclContext *DC) {
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +000054 if (!DC || isa<TranslationUnitDecl>(DC))
55 Ctx = TUCtx();
56 else
57 Ctx = DC;
58 }
59
60 bool isTU() const {
61 return (Ctx == TUCtx());
62 }
63
64 /// getParent - Returns the parent context. This should not be called for
65 /// a translation unit context.
66 LookupContext getParent() const {
67 assert(!isTU() && "TU has no parent!");
68 return LookupContext(Ctx->getParent());
69 }
70
71 /// isEqOrContainedBy - Returns true of the given context is the same or a
72 /// parent of this one.
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +000073 bool isEqOrContainedBy(const LookupContext &PC) const;
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +000074
75 bool operator==(const LookupContext &RHS) const {
76 return Ctx == RHS.Ctx;
77 }
78 bool operator!=(const LookupContext &RHS) const {
79 return Ctx != RHS.Ctx;
80 }
81 };
82
Douglas Gregor2def4832008-11-17 20:34:05 +000083 /// IdDeclInfo - Keeps track of information about decls associated
84 /// to a particular declaration name. IdDeclInfos are lazily
85 /// constructed and assigned to a declaration name the first time a
86 /// decl with that declaration name is shadowed in some scope.
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +000087 class IdDeclInfo {
88 public:
89 typedef llvm::SmallVector<NamedDecl*, 2> DeclsTy;
90
91 inline DeclsTy::iterator decls_begin() { return Decls.begin(); }
92 inline DeclsTy::iterator decls_end() { return Decls.end(); }
93
94 /// FindContext - Returns an iterator pointing just after the decl that is
95 /// in the given context or in a parent of it. The search is in reverse
96 /// order, from end to begin.
Zhongxing Xuebed7972008-12-05 01:57:57 +000097 DeclsTy::iterator FindDeclVisibleInContext(const LookupContext &Ctx) {
98 return FindDeclVisibleInContext(Ctx, Decls.end());
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +000099 }
100
101 /// FindContext - Returns an iterator pointing just after the decl that is
102 /// in the given context or in a parent of it. The search is in reverse
103 /// order, from end to begin.
Zhongxing Xuebed7972008-12-05 01:57:57 +0000104 DeclsTy::iterator FindDeclVisibleInContext(const LookupContext &Ctx,
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +0000105 const DeclsTy::iterator &Start);
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000106
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000107 void AddDecl(NamedDecl *D) {
Zhongxing Xuebed7972008-12-05 01:57:57 +0000108 Decls.insert(FindDeclVisibleInContext(LookupContext(D)), D);
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000109 }
110
111 /// AddShadowed - Add a decl by putting it directly above the 'Shadow' decl.
112 /// Later lookups will find the 'Shadow' decl first. The 'Shadow' decl must
113 /// be already added to the scope chain and must be in the same context as
114 /// the decl that we want to add.
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +0000115 void AddShadowed(NamedDecl *D, NamedDecl *Shadow);
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000116
117 /// RemoveDecl - Remove the decl from the scope chain.
118 /// The decl must already be part of the decl chain.
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +0000119 void RemoveDecl(NamedDecl *D);
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000120
121 private:
122 DeclsTy Decls;
123 };
124
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000125public:
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000126
Douglas Gregor2def4832008-11-17 20:34:05 +0000127 /// iterator - Iterate over the decls of a specified declaration name.
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000128 /// It will walk or not the parent declaration contexts depending on how
129 /// it was instantiated.
130 class iterator {
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000131 public:
132 typedef NamedDecl * value_type;
133 typedef NamedDecl * reference;
134 typedef NamedDecl * pointer;
135 typedef std::input_iterator_tag iterator_category;
136 typedef std::ptrdiff_t difference_type;
137
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000138 /// Ptr - There are 3 forms that 'Ptr' represents:
139 /// 1) A single NamedDecl. (Ptr & 0x1 == 0)
140 /// 2) A IdDeclInfo::DeclsTy::iterator that traverses only the decls of the
141 /// same declaration context. (Ptr & 0x3 == 0x1)
142 /// 3) A IdDeclInfo::DeclsTy::iterator that traverses the decls of parent
143 /// declaration contexts too. (Ptr & 0x3 == 0x3)
144 uintptr_t Ptr;
145 typedef IdDeclInfo::DeclsTy::iterator BaseIter;
146
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000147 /// A single NamedDecl. (Ptr & 0x1 == 0)
148 iterator(NamedDecl *D) {
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000149 Ptr = reinterpret_cast<uintptr_t>(D);
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000150 assert((Ptr & 0x1) == 0 && "Invalid Ptr!");
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000151 }
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000152 /// A IdDeclInfo::DeclsTy::iterator that walks or not the parent declaration
153 /// contexts depending on 'LookInParentCtx'.
154 iterator(BaseIter I, bool LookInParentCtx) {
155 Ptr = reinterpret_cast<uintptr_t>(I) | 0x1;
156 assert((Ptr & 0x2) == 0 && "Invalid Ptr!");
157 if (LookInParentCtx) Ptr |= 0x2;
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000158 }
159
160 bool isIterator() const { return (Ptr & 0x1); }
161
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000162 bool LookInParentCtx() const {
163 assert(isIterator() && "Ptr not an iterator!");
164 return (Ptr & 0x2) != 0;
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000165 }
166
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000167 BaseIter getIterator() const {
168 assert(isIterator() && "Ptr not an iterator!");
169 return reinterpret_cast<BaseIter>(Ptr & ~0x3);
170 }
171
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000172 friend class IdentifierResolver;
173 public:
Argyrios Kyrtzidisb53c7842008-08-01 10:20:48 +0000174 iterator() : Ptr(0) {}
175
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000176 NamedDecl *operator*() const {
177 if (isIterator())
178 return *getIterator();
179 else
180 return reinterpret_cast<NamedDecl*>(Ptr);
181 }
182
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000183 bool operator==(const iterator &RHS) const {
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000184 return Ptr == RHS.Ptr;
185 }
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000186 bool operator!=(const iterator &RHS) const {
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000187 return Ptr != RHS.Ptr;
188 }
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000189
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000190 // Preincrement.
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000191 iterator& operator++() {
192 if (!isIterator()) // common case.
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000193 Ptr = 0;
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000194 else
195 PreIncIter();
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000196 return *this;
197 }
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000198
199 private:
200 void PreIncIter();
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000201 };
202
Douglas Gregor2def4832008-11-17 20:34:05 +0000203 /// begin - Returns an iterator for decls with the name 'Name', starting at
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000204 /// declaration context 'Ctx'. If 'LookInParentCtx' is true, it will walk the
205 /// decls of parent declaration contexts too.
206 /// Default for 'LookInParentCtx is true.
Douglas Gregor2def4832008-11-17 20:34:05 +0000207 static iterator begin(DeclarationName Name, const DeclContext *Ctx,
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000208 bool LookInParentCtx = true);
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000209
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000210 /// end - Returns an iterator that has 'finished'.
211 static iterator end() {
212 return iterator();
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000213 }
214
Argyrios Kyrtzidise29f0a42008-05-14 10:49:47 +0000215 /// isDeclInScope - If 'Ctx' is a function/method, isDeclInScope returns true
216 /// if 'D' is in Scope 'S', otherwise 'S' is ignored and isDeclInScope returns
217 /// true if 'D' belongs to the given declaration context.
Douglas Gregor44b43212008-12-11 16:49:14 +0000218 bool isDeclInScope(Decl *D, DeclContext *Ctx, ASTContext &Context,
219 Scope *S = 0) const;
Chris Lattnera2f42b12008-04-11 07:06:57 +0000220
Argyrios Kyrtzidis321f2782008-04-12 01:50:47 +0000221 /// AddDecl - Link the decl to its shadowed decl chain.
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000222 void AddDecl(NamedDecl *D);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000223
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000224 /// AddShadowedDecl - Link the decl to its shadowed decl chain putting it
Argyrios Kyrtzidis3d0d83a2008-05-15 17:26:35 +0000225 /// after the decl that the iterator points to, thus the 'Shadow' decl will be
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000226 /// encountered before the 'D' decl.
227 void AddShadowedDecl(NamedDecl *D, NamedDecl *Shadow);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000228
Argyrios Kyrtzidis321f2782008-04-12 01:50:47 +0000229 /// RemoveDecl - Unlink the decl from its shadowed decl chain.
Chris Lattnera2f42b12008-04-11 07:06:57 +0000230 /// The decl must already be part of the decl chain.
231 void RemoveDecl(NamedDecl *D);
232
Argyrios Kyrtzidiseb7c3882008-09-09 21:32:02 +0000233 explicit IdentifierResolver(const LangOptions &LangOpt);
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000234 ~IdentifierResolver();
Chris Lattnera2f42b12008-04-11 07:06:57 +0000235
236private:
Argyrios Kyrtzidiseb7c3882008-09-09 21:32:02 +0000237 const LangOptions &LangOpt;
238
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000239 class IdDeclInfoMap;
240 IdDeclInfoMap *IdDeclInfos;
241
Douglas Gregor2def4832008-11-17 20:34:05 +0000242 /// FETokenInfo contains a Decl pointer if lower bit == 0.
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000243 static inline bool isDeclPtr(void *Ptr) {
244 return (reinterpret_cast<uintptr_t>(Ptr) & 0x1) == 0;
245 }
246
Douglas Gregor2def4832008-11-17 20:34:05 +0000247 /// FETokenInfo contains a IdDeclInfo pointer if lower bit == 1.
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000248 static inline IdDeclInfo *toIdDeclInfo(void *Ptr) {
249 assert((reinterpret_cast<uintptr_t>(Ptr) & 0x1) == 1
250 && "Ptr not a IdDeclInfo* !");
251 return reinterpret_cast<IdDeclInfo*>(
252 reinterpret_cast<uintptr_t>(Ptr) & ~0x1
253 );
254 }
Chris Lattnera2f42b12008-04-11 07:06:57 +0000255};
256
257} // end namespace clang
258
259#endif