1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
|
//===- IdentifierResolver.h - Lexical Scope Name lookup ---------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file defines the IdentifierResolver class, which is used for lexical
// scoped lookup, based on declaration names.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_CLANG_AST_SEMA_IDENTIFIERRESOLVER_H
#define LLVM_CLANG_AST_SEMA_IDENTIFIERRESOLVER_H
#include "clang/Basic/IdentifierTable.h"
namespace clang {
class ASTContext;
class Decl;
class DeclContext;
class DeclarationName;
class NamedDecl;
class Scope;
/// IdentifierResolver - Keeps track of shadowed decls on enclosing
/// scopes. It manages the shadowing chains of declaration names and
/// implements efficient decl lookup based on a declaration name.
class IdentifierResolver {
/// IdDeclInfo - Keeps track of information about decls associated
/// to a particular declaration name. IdDeclInfos are lazily
/// constructed and assigned to a declaration name the first time a
/// decl with that declaration name is shadowed in some scope.
class IdDeclInfo {
public:
typedef llvm::SmallVector<NamedDecl*, 2> DeclsTy;
inline DeclsTy::iterator decls_begin() { return Decls.begin(); }
inline DeclsTy::iterator decls_end() { return Decls.end(); }
void AddDecl(NamedDecl *D) { Decls.push_back(D); }
/// RemoveDecl - Remove the decl from the scope chain.
/// The decl must already be part of the decl chain.
void RemoveDecl(NamedDecl *D);
/// Replaces the Old declaration with the New declaration. If the
/// replacement is successful, returns true. If the old
/// declaration was not found, returns false.
bool ReplaceDecl(NamedDecl *Old, NamedDecl *New);
/// \brief Insert the given declaration at the given position in the list.
void InsertDecl(DeclsTy::iterator Pos, NamedDecl *D) {
Decls.insert(Pos, D);
}
private:
DeclsTy Decls;
};
public:
/// iterator - Iterate over the decls of a specified declaration name.
/// It will walk or not the parent declaration contexts depending on how
/// it was instantiated.
class iterator {
public:
typedef NamedDecl * value_type;
typedef NamedDecl * reference;
typedef NamedDecl * pointer;
typedef std::input_iterator_tag iterator_category;
typedef std::ptrdiff_t difference_type;
/// Ptr - There are 3 forms that 'Ptr' represents:
/// 1) A single NamedDecl. (Ptr & 0x1 == 0)
/// 2) A IdDeclInfo::DeclsTy::iterator that traverses only the decls of the
/// same declaration context. (Ptr & 0x3 == 0x1)
/// 3) A IdDeclInfo::DeclsTy::iterator that traverses the decls of parent
/// declaration contexts too. (Ptr & 0x3 == 0x3)
uintptr_t Ptr;
typedef IdDeclInfo::DeclsTy::iterator BaseIter;
/// A single NamedDecl. (Ptr & 0x1 == 0)
iterator(NamedDecl *D) {
Ptr = reinterpret_cast<uintptr_t>(D);
assert((Ptr & 0x1) == 0 && "Invalid Ptr!");
}
/// A IdDeclInfo::DeclsTy::iterator that walks or not the parent declaration
/// contexts depending on 'LookInParentCtx'.
iterator(BaseIter I) {
Ptr = reinterpret_cast<uintptr_t>(I) | 0x1;
}
bool isIterator() const { return (Ptr & 0x1); }
BaseIter getIterator() const {
assert(isIterator() && "Ptr not an iterator!");
return reinterpret_cast<BaseIter>(Ptr & ~0x3);
}
friend class IdentifierResolver;
void incrementSlowCase();
public:
iterator() : Ptr(0) {}
NamedDecl *operator*() const {
if (isIterator())
return *getIterator();
else
return reinterpret_cast<NamedDecl*>(Ptr);
}
bool operator==(const iterator &RHS) const {
return Ptr == RHS.Ptr;
}
bool operator!=(const iterator &RHS) const {
return Ptr != RHS.Ptr;
}
// Preincrement.
iterator& operator++() {
if (!isIterator()) // common case.
Ptr = 0;
else
incrementSlowCase();
return *this;
}
uintptr_t getAsOpaqueValue() const { return Ptr; }
static iterator getFromOpaqueValue(uintptr_t P) {
iterator Result;
Result.Ptr = P;
return Result;
}
};
/// begin - Returns an iterator for decls with the name 'Name'.
static iterator begin(DeclarationName Name);
/// end - Returns an iterator that has 'finished'.
static iterator end() {
return iterator();
}
/// isDeclInScope - If 'Ctx' is a function/method, isDeclInScope returns true
/// if 'D' is in Scope 'S', otherwise 'S' is ignored and isDeclInScope returns
/// true if 'D' belongs to the given declaration context.
///
/// \param ExplicitInstantiationOrSpecialization When true, we are checking
/// whether the declaration is in scope for the purposes of explicit template
/// instantiation or specialization. The default is false.
bool isDeclInScope(Decl *D, DeclContext *Ctx, ASTContext &Context,
Scope *S = 0,
bool ExplicitInstantiationOrSpecialization = false) const;
/// AddDecl - Link the decl to its shadowed decl chain.
void AddDecl(NamedDecl *D);
/// RemoveDecl - Unlink the decl from its shadowed decl chain.
/// The decl must already be part of the decl chain.
void RemoveDecl(NamedDecl *D);
/// Replace the decl Old with the new declaration New on its
/// identifier chain. Returns true if the old declaration was found
/// (and, therefore, replaced).
bool ReplaceDecl(NamedDecl *Old, NamedDecl *New);
/// \brief Insert the given declaration after the given iterator
/// position.
void InsertDeclAfter(iterator Pos, NamedDecl *D);
/// \brief Link the declaration into the chain of declarations for
/// the given identifier.
///
/// This is a lower-level routine used by the AST reader to link a
/// declaration into a specific IdentifierInfo before the
/// declaration actually has a name.
void AddDeclToIdentifierChain(IdentifierInfo *II, NamedDecl *D);
explicit IdentifierResolver(const LangOptions &LangOpt);
~IdentifierResolver();
private:
const LangOptions &LangOpt;
class IdDeclInfoMap;
IdDeclInfoMap *IdDeclInfos;
/// FETokenInfo contains a Decl pointer if lower bit == 0.
static inline bool isDeclPtr(void *Ptr) {
return (reinterpret_cast<uintptr_t>(Ptr) & 0x1) == 0;
}
/// FETokenInfo contains a IdDeclInfo pointer if lower bit == 1.
static inline IdDeclInfo *toIdDeclInfo(void *Ptr) {
assert((reinterpret_cast<uintptr_t>(Ptr) & 0x1) == 1
&& "Ptr not a IdDeclInfo* !");
return reinterpret_cast<IdDeclInfo*>(
reinterpret_cast<uintptr_t>(Ptr) & ~0x1
);
}
};
} // end namespace clang
#endif
|