IdentifierResolver.cpp revision f02d2aefb7a9b3d186de72b0109ea00f74bd459d
1//===- IdentifierResolver.cpp - 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 implements the IdentifierResolver class, which is used for lexical
11// scoped lookup, based on declaration names.
12//
13//===----------------------------------------------------------------------===//
14
15#include "IdentifierResolver.h"
16#include "clang/Basic/LangOptions.h"
17#include <list>
18#include <vector>
19
20using namespace clang;
21
22//===----------------------------------------------------------------------===//
23// IdDeclInfoMap class
24//===----------------------------------------------------------------------===//
25
26/// IdDeclInfoMap - Associates IdDeclInfos with declaration names.
27/// Allocates 'pools' (vectors of IdDeclInfos) to avoid allocating each
28/// individual IdDeclInfo to heap.
29class IdentifierResolver::IdDeclInfoMap {
30  static const unsigned int VECTOR_SIZE = 512;
31  // Holds vectors of IdDeclInfos that serve as 'pools'.
32  // New vectors are added when the current one is full.
33  std::list< std::vector<IdDeclInfo> > IDIVecs;
34  unsigned int CurIndex;
35
36public:
37  IdDeclInfoMap() : CurIndex(VECTOR_SIZE) {}
38
39  /// Returns the IdDeclInfo associated to the DeclarationName.
40  /// It creates a new IdDeclInfo if one was not created before for this id.
41  IdDeclInfo &operator[](DeclarationName Name);
42};
43
44
45//===----------------------------------------------------------------------===//
46// IdDeclInfo Implementation
47//===----------------------------------------------------------------------===//
48
49/// AddShadowed - Add a decl by putting it directly above the 'Shadow' decl.
50/// Later lookups will find the 'Shadow' decl first. The 'Shadow' decl must
51/// be already added to the scope chain and must be in the same context as
52/// the decl that we want to add.
53void IdentifierResolver::IdDeclInfo::AddShadowed(NamedDecl *D,
54                                                 NamedDecl *Shadow) {
55  for (DeclsTy::iterator I = Decls.end(); I != Decls.begin(); --I) {
56    if (Shadow == *(I-1)) {
57      Decls.insert(I-1, D);
58      return;
59    }
60  }
61
62  assert(0 && "Shadow wasn't in scope chain!");
63}
64
65/// RemoveDecl - Remove the decl from the scope chain.
66/// The decl must already be part of the decl chain.
67void IdentifierResolver::IdDeclInfo::RemoveDecl(NamedDecl *D) {
68  for (DeclsTy::iterator I = Decls.end(); I != Decls.begin(); --I) {
69    if (D == *(I-1)) {
70      Decls.erase(I-1);
71      return;
72    }
73  }
74
75  assert(0 && "Didn't find this decl on its identifier's chain!");
76}
77
78bool
79IdentifierResolver::IdDeclInfo::ReplaceDecl(NamedDecl *Old, NamedDecl *New) {
80  for (DeclsTy::iterator I = Decls.end(); I != Decls.begin(); --I) {
81    if (Old == *(I-1)) {
82      *(I - 1) = New;
83      return true;
84    }
85  }
86
87  return false;
88}
89
90
91//===----------------------------------------------------------------------===//
92// IdentifierResolver Implementation
93//===----------------------------------------------------------------------===//
94
95IdentifierResolver::IdentifierResolver(const LangOptions &langOpt)
96    : LangOpt(langOpt), IdDeclInfos(new IdDeclInfoMap) {
97}
98IdentifierResolver::~IdentifierResolver() {
99  delete IdDeclInfos;
100}
101
102/// isDeclInScope - If 'Ctx' is a function/method, isDeclInScope returns true
103/// if 'D' is in Scope 'S', otherwise 'S' is ignored and isDeclInScope returns
104/// true if 'D' belongs to the given declaration context.
105bool IdentifierResolver::isDeclInScope(Decl *D, DeclContext *Ctx,
106                                       ASTContext &Context, Scope *S) const {
107  Ctx = Ctx->getLookupContext();
108
109  if (Ctx->isFunctionOrMethod()) {
110    // Ignore the scopes associated within transparent declaration contexts.
111    while (S->getEntity() &&
112           ((DeclContext *)S->getEntity())->isTransparentContext())
113      S = S->getParent();
114
115    if (S->isDeclScope(Action::DeclPtrTy::make(D)))
116      return true;
117    if (LangOpt.CPlusPlus) {
118      // C++ 3.3.2p3:
119      // The name declared in a catch exception-declaration is local to the
120      // handler and shall not be redeclared in the outermost block of the
121      // handler.
122      // C++ 3.3.2p4:
123      // Names declared in the for-init-statement, and in the condition of if,
124      // while, for, and switch statements are local to the if, while, for, or
125      // switch statement (including the controlled statement), and shall not be
126      // redeclared in a subsequent condition of that statement nor in the
127      // outermost block (or, for the if statement, any of the outermost blocks)
128      // of the controlled statement.
129      //
130      assert(S->getParent() && "No TUScope?");
131      if (S->getParent()->getFlags() & Scope::ControlScope)
132        return S->getParent()->isDeclScope(Action::DeclPtrTy::make(D));
133    }
134    return false;
135  }
136
137  return D->getDeclContext()->getLookupContext()->Equals(Ctx);
138}
139
140/// AddDecl - Link the decl to its shadowed decl chain.
141void IdentifierResolver::AddDecl(NamedDecl *D) {
142  DeclarationName Name = D->getDeclName();
143  void *Ptr = Name.getFETokenInfo<void>();
144
145  if (!Ptr) {
146    Name.setFETokenInfo(D);
147    return;
148  }
149
150  IdDeclInfo *IDI;
151
152  if (isDeclPtr(Ptr)) {
153    Name.setFETokenInfo(NULL);
154    IDI = &(*IdDeclInfos)[Name];
155    NamedDecl *PrevD = static_cast<NamedDecl*>(Ptr);
156    IDI->AddDecl(PrevD);
157  } else
158    IDI = toIdDeclInfo(Ptr);
159
160  IDI->AddDecl(D);
161}
162
163/// AddShadowedDecl - Link the decl to its shadowed decl chain putting it
164/// after the decl that the iterator points to, thus the 'Shadow' decl will be
165/// encountered before the 'D' decl.
166void IdentifierResolver::AddShadowedDecl(NamedDecl *D, NamedDecl *Shadow) {
167  assert(D->getDeclName() == Shadow->getDeclName() && "Different ids!");
168
169  DeclarationName Name = D->getDeclName();
170  void *Ptr = Name.getFETokenInfo<void>();
171  assert(Ptr && "No decl from Ptr ?");
172
173  IdDeclInfo *IDI;
174
175  if (isDeclPtr(Ptr)) {
176    Name.setFETokenInfo(NULL);
177    IDI = &(*IdDeclInfos)[Name];
178    NamedDecl *PrevD = static_cast<NamedDecl*>(Ptr);
179    assert(PrevD == Shadow && "Invalid shadow decl ?");
180    IDI->AddDecl(D);
181    IDI->AddDecl(PrevD);
182    return;
183  }
184
185  IDI = toIdDeclInfo(Ptr);
186  IDI->AddShadowed(D, Shadow);
187}
188
189/// RemoveDecl - Unlink the decl from its shadowed decl chain.
190/// The decl must already be part of the decl chain.
191void IdentifierResolver::RemoveDecl(NamedDecl *D) {
192  assert(D && "null param passed");
193  DeclarationName Name = D->getDeclName();
194  void *Ptr = Name.getFETokenInfo<void>();
195
196  assert(Ptr && "Didn't find this decl on its identifier's chain!");
197
198  if (isDeclPtr(Ptr)) {
199    assert(D == Ptr && "Didn't find this decl on its identifier's chain!");
200    Name.setFETokenInfo(NULL);
201    return;
202  }
203
204  return toIdDeclInfo(Ptr)->RemoveDecl(D);
205}
206
207bool IdentifierResolver::ReplaceDecl(NamedDecl *Old, NamedDecl *New) {
208  assert(Old->getDeclName() == New->getDeclName() &&
209         "Cannot replace a decl with another decl of a different name");
210
211  DeclarationName Name = Old->getDeclName();
212  void *Ptr = Name.getFETokenInfo<void>();
213
214  if (!Ptr)
215    return false;
216
217  if (isDeclPtr(Ptr)) {
218    if (Ptr == Old) {
219      Name.setFETokenInfo(New);
220      return true;
221    }
222    return false;
223  }
224
225  return toIdDeclInfo(Ptr)->ReplaceDecl(Old, New);
226}
227
228/// begin - Returns an iterator for decls with name 'Name'.
229IdentifierResolver::iterator
230IdentifierResolver::begin(DeclarationName Name) {
231  void *Ptr = Name.getFETokenInfo<void>();
232  if (!Ptr) return end();
233
234  if (isDeclPtr(Ptr))
235    return iterator(static_cast<NamedDecl*>(Ptr));
236
237  IdDeclInfo *IDI = toIdDeclInfo(Ptr);
238
239  IdDeclInfo::DeclsTy::iterator I = IDI->decls_end();
240  if (I != IDI->decls_begin())
241    return iterator(I-1);
242  // No decls found.
243  return end();
244}
245
246void IdentifierResolver::AddDeclToIdentifierChain(IdentifierInfo *II,
247                                                  NamedDecl *D) {
248  void *Ptr = II->getFETokenInfo<void>();
249
250  if (!Ptr) {
251    II->setFETokenInfo(D);
252    return;
253  }
254
255  IdDeclInfo *IDI;
256
257  if (isDeclPtr(Ptr)) {
258    II->setFETokenInfo(NULL);
259    IDI = &(*IdDeclInfos)[II];
260    NamedDecl *PrevD = static_cast<NamedDecl*>(Ptr);
261    IDI->AddDecl(PrevD);
262  } else
263    IDI = toIdDeclInfo(Ptr);
264
265  IDI->AddDecl(D);
266}
267
268//===----------------------------------------------------------------------===//
269// IdDeclInfoMap Implementation
270//===----------------------------------------------------------------------===//
271
272/// Returns the IdDeclInfo associated to the DeclarationName.
273/// It creates a new IdDeclInfo if one was not created before for this id.
274IdentifierResolver::IdDeclInfo &
275IdentifierResolver::IdDeclInfoMap::operator[](DeclarationName Name) {
276  void *Ptr = Name.getFETokenInfo<void>();
277
278  if (Ptr) return *toIdDeclInfo(Ptr);
279
280  if (CurIndex == VECTOR_SIZE) {
281    // Add a IdDeclInfo vector 'pool'
282    IDIVecs.push_back(std::vector<IdDeclInfo>());
283    // Fill the vector
284    IDIVecs.back().resize(VECTOR_SIZE);
285    CurIndex = 0;
286  }
287  IdDeclInfo *IDI = &IDIVecs.back()[CurIndex];
288  Name.setFETokenInfo(reinterpret_cast<void*>(
289                              reinterpret_cast<uintptr_t>(IDI) | 0x1)
290                                                                     );
291  ++CurIndex;
292  return *IDI;
293}
294