blob: c661145d89f6d76c7381799336ef1365ab063e5e [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.
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +000046 static DeclContext *getContext(Decl *D);
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +000047
48 public:
49 LookupContext(Decl *D) {
50 Ctx = getContext(D);
51 }
52 LookupContext(DeclContext *DC) {
53 if (!DC || isa<TranslationUnitDecl>(DC))
54 Ctx = TUCtx();
55 else
56 Ctx = DC;
57 }
58
59 bool isTU() const {
60 return (Ctx == TUCtx());
61 }
62
63 /// getParent - Returns the parent context. This should not be called for
64 /// a translation unit context.
65 LookupContext getParent() const {
66 assert(!isTU() && "TU has no parent!");
67 return LookupContext(Ctx->getParent());
68 }
69
70 /// isEqOrContainedBy - Returns true of the given context is the same or a
71 /// parent of this one.
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +000072 bool isEqOrContainedBy(const LookupContext &PC) const;
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +000073
74 bool operator==(const LookupContext &RHS) const {
75 return Ctx == RHS.Ctx;
76 }
77 bool operator!=(const LookupContext &RHS) const {
78 return Ctx != RHS.Ctx;
79 }
80 };
81
82 /// IdDeclInfo - Keeps track of information about decls associated to a
83 /// particular identifier. IdDeclInfos are lazily constructed and assigned
84 /// to an identifier the first time a decl with that identifier is shadowed
85 /// in some scope.
86 class IdDeclInfo {
87 public:
88 typedef llvm::SmallVector<NamedDecl*, 2> DeclsTy;
89
90 inline DeclsTy::iterator decls_begin() { return Decls.begin(); }
91 inline DeclsTy::iterator decls_end() { return Decls.end(); }
92
93 /// FindContext - Returns an iterator pointing just after the decl that is
94 /// in the given context or in a parent of it. The search is in reverse
95 /// order, from end to begin.
96 DeclsTy::iterator FindContext(const LookupContext &Ctx) {
97 return FindContext(Ctx, Decls.end());
98 }
99
100 /// FindContext - Returns an iterator pointing just after the decl that is
101 /// in the given context or in a parent of it. The search is in reverse
102 /// order, from end to begin.
103 DeclsTy::iterator FindContext(const LookupContext &Ctx,
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +0000104 const DeclsTy::iterator &Start);
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000105
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000106 void AddDecl(NamedDecl *D) {
107 Decls.insert(FindContext(LookupContext(D)), D);
108 }
109
110 /// AddShadowed - Add a decl by putting it directly above the 'Shadow' decl.
111 /// Later lookups will find the 'Shadow' decl first. The 'Shadow' decl must
112 /// be already added to the scope chain and must be in the same context as
113 /// the decl that we want to add.
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +0000114 void AddShadowed(NamedDecl *D, NamedDecl *Shadow);
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000115
116 /// RemoveDecl - Remove the decl from the scope chain.
117 /// The decl must already be part of the decl chain.
Argyrios Kyrtzidis81bebb12008-09-09 19:28:27 +0000118 void RemoveDecl(NamedDecl *D);
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000119
120 private:
121 DeclsTy Decls;
122 };
123
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000124public:
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000125
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000126 /// iterator - Iterate over the decls of a specified identifier.
127 /// It will walk or not the parent declaration contexts depending on how
128 /// it was instantiated.
129 class iterator {
130 /// Ptr - There are 3 forms that 'Ptr' represents:
131 /// 1) A single NamedDecl. (Ptr & 0x1 == 0)
132 /// 2) A IdDeclInfo::DeclsTy::iterator that traverses only the decls of the
133 /// same declaration context. (Ptr & 0x3 == 0x1)
134 /// 3) A IdDeclInfo::DeclsTy::iterator that traverses the decls of parent
135 /// declaration contexts too. (Ptr & 0x3 == 0x3)
136 uintptr_t Ptr;
137 typedef IdDeclInfo::DeclsTy::iterator BaseIter;
138
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000139 /// A single NamedDecl. (Ptr & 0x1 == 0)
140 iterator(NamedDecl *D) {
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000141 Ptr = reinterpret_cast<uintptr_t>(D);
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000142 assert((Ptr & 0x1) == 0 && "Invalid Ptr!");
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000143 }
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000144 /// A IdDeclInfo::DeclsTy::iterator that walks or not the parent declaration
145 /// contexts depending on 'LookInParentCtx'.
146 iterator(BaseIter I, bool LookInParentCtx) {
147 Ptr = reinterpret_cast<uintptr_t>(I) | 0x1;
148 assert((Ptr & 0x2) == 0 && "Invalid Ptr!");
149 if (LookInParentCtx) Ptr |= 0x2;
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000150 }
151
152 bool isIterator() const { return (Ptr & 0x1); }
153
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000154 bool LookInParentCtx() const {
155 assert(isIterator() && "Ptr not an iterator!");
156 return (Ptr & 0x2) != 0;
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000157 }
158
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000159 BaseIter getIterator() const {
160 assert(isIterator() && "Ptr not an iterator!");
161 return reinterpret_cast<BaseIter>(Ptr & ~0x3);
162 }
163
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000164 friend class IdentifierResolver;
165 public:
Argyrios Kyrtzidisb53c7842008-08-01 10:20:48 +0000166 iterator() : Ptr(0) {}
167
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000168 NamedDecl *operator*() const {
169 if (isIterator())
170 return *getIterator();
171 else
172 return reinterpret_cast<NamedDecl*>(Ptr);
173 }
174
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000175 bool operator==(const iterator &RHS) const {
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000176 return Ptr == RHS.Ptr;
177 }
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000178 bool operator!=(const iterator &RHS) const {
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000179 return Ptr != RHS.Ptr;
180 }
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000181
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000182 // Preincrement.
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000183 iterator& operator++() {
184 if (!isIterator()) // common case.
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000185 Ptr = 0;
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000186 else
187 PreIncIter();
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000188 return *this;
189 }
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000190
191 private:
192 void PreIncIter();
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000193 };
194
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000195 /// begin - Returns an iterator for decls of identifier 'II', starting at
196 /// declaration context 'Ctx'. If 'LookInParentCtx' is true, it will walk the
197 /// decls of parent declaration contexts too.
198 /// Default for 'LookInParentCtx is true.
199 static iterator begin(const IdentifierInfo *II, DeclContext *Ctx,
200 bool LookInParentCtx = true);
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000201
Argyrios Kyrtzidis90eb5392008-07-17 17:49:50 +0000202 /// end - Returns an iterator that has 'finished'.
203 static iterator end() {
204 return iterator();
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000205 }
206
Argyrios Kyrtzidise29f0a42008-05-14 10:49:47 +0000207 /// isDeclInScope - If 'Ctx' is a function/method, isDeclInScope returns true
208 /// if 'D' is in Scope 'S', otherwise 'S' is ignored and isDeclInScope returns
209 /// true if 'D' belongs to the given declaration context.
Argyrios Kyrtzidisf99cb052008-09-09 21:57:58 +0000210 bool isDeclInScope(Decl *D, DeclContext *Ctx, Scope *S = 0) const;
Chris Lattnera2f42b12008-04-11 07:06:57 +0000211
Argyrios Kyrtzidis321f2782008-04-12 01:50:47 +0000212 /// AddDecl - Link the decl to its shadowed decl chain.
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000213 void AddDecl(NamedDecl *D);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000214
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000215 /// AddShadowedDecl - Link the decl to its shadowed decl chain putting it
Argyrios Kyrtzidis3d0d83a2008-05-15 17:26:35 +0000216 /// after the decl that the iterator points to, thus the 'Shadow' decl will be
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000217 /// encountered before the 'D' decl.
218 void AddShadowedDecl(NamedDecl *D, NamedDecl *Shadow);
Chris Lattnera2f42b12008-04-11 07:06:57 +0000219
Argyrios Kyrtzidis321f2782008-04-12 01:50:47 +0000220 /// RemoveDecl - Unlink the decl from its shadowed decl chain.
Chris Lattnera2f42b12008-04-11 07:06:57 +0000221 /// The decl must already be part of the decl chain.
222 void RemoveDecl(NamedDecl *D);
223
Argyrios Kyrtzidiseb7c3882008-09-09 21:32:02 +0000224 explicit IdentifierResolver(const LangOptions &LangOpt);
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000225 ~IdentifierResolver();
Chris Lattnera2f42b12008-04-11 07:06:57 +0000226
227private:
Argyrios Kyrtzidiseb7c3882008-09-09 21:32:02 +0000228 const LangOptions &LangOpt;
229
Argyrios Kyrtzidis00bc6452008-05-09 23:39:43 +0000230 class IdDeclInfoMap;
231 IdDeclInfoMap *IdDeclInfos;
232
233 /// Identifier's FETokenInfo contains a Decl pointer if lower bit == 0.
234 static inline bool isDeclPtr(void *Ptr) {
235 return (reinterpret_cast<uintptr_t>(Ptr) & 0x1) == 0;
236 }
237
238 /// Identifier's FETokenInfo contains a IdDeclInfo pointer if lower bit == 1.
239 static inline IdDeclInfo *toIdDeclInfo(void *Ptr) {
240 assert((reinterpret_cast<uintptr_t>(Ptr) & 0x1) == 1
241 && "Ptr not a IdDeclInfo* !");
242 return reinterpret_cast<IdDeclInfo*>(
243 reinterpret_cast<uintptr_t>(Ptr) & ~0x1
244 );
245 }
Chris Lattnera2f42b12008-04-11 07:06:57 +0000246};
247
248} // end namespace clang
249
250#endif