xref: /minix3/external/bsd/llvm/dist/clang/lib/Sema/IdentifierResolver.cpp (revision f4a2713ac843a11c696ec80c0a5e3e5d80b4d338)
1*f4a2713aSLionel Sambuc //===- IdentifierResolver.cpp - Lexical Scope Name lookup -------*- C++ -*-===//
2*f4a2713aSLionel Sambuc //
3*f4a2713aSLionel Sambuc //                     The LLVM Compiler Infrastructure
4*f4a2713aSLionel Sambuc //
5*f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6*f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7*f4a2713aSLionel Sambuc //
8*f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9*f4a2713aSLionel Sambuc //
10*f4a2713aSLionel Sambuc // This file implements the IdentifierResolver class, which is used for lexical
11*f4a2713aSLionel Sambuc // scoped lookup, based on declaration names.
12*f4a2713aSLionel Sambuc //
13*f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
14*f4a2713aSLionel Sambuc 
15*f4a2713aSLionel Sambuc #include "clang/Sema/IdentifierResolver.h"
16*f4a2713aSLionel Sambuc #include "clang/AST/Decl.h"
17*f4a2713aSLionel Sambuc #include "clang/Basic/LangOptions.h"
18*f4a2713aSLionel Sambuc #include "clang/Lex/ExternalPreprocessorSource.h"
19*f4a2713aSLionel Sambuc #include "clang/Lex/Preprocessor.h"
20*f4a2713aSLionel Sambuc #include "clang/Sema/Scope.h"
21*f4a2713aSLionel Sambuc 
22*f4a2713aSLionel Sambuc using namespace clang;
23*f4a2713aSLionel Sambuc 
24*f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
25*f4a2713aSLionel Sambuc // IdDeclInfoMap class
26*f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
27*f4a2713aSLionel Sambuc 
28*f4a2713aSLionel Sambuc /// IdDeclInfoMap - Associates IdDeclInfos with declaration names.
29*f4a2713aSLionel Sambuc /// Allocates 'pools' (vectors of IdDeclInfos) to avoid allocating each
30*f4a2713aSLionel Sambuc /// individual IdDeclInfo to heap.
31*f4a2713aSLionel Sambuc class IdentifierResolver::IdDeclInfoMap {
32*f4a2713aSLionel Sambuc   static const unsigned int POOL_SIZE = 512;
33*f4a2713aSLionel Sambuc 
34*f4a2713aSLionel Sambuc   /// We use our own linked-list implementation because it is sadly
35*f4a2713aSLionel Sambuc   /// impossible to add something to a pre-C++0x STL container without
36*f4a2713aSLionel Sambuc   /// a completely unnecessary copy.
37*f4a2713aSLionel Sambuc   struct IdDeclInfoPool {
38*f4a2713aSLionel Sambuc     IdDeclInfoPool(IdDeclInfoPool *Next) : Next(Next) {}
39*f4a2713aSLionel Sambuc 
40*f4a2713aSLionel Sambuc     IdDeclInfoPool *Next;
41*f4a2713aSLionel Sambuc     IdDeclInfo Pool[POOL_SIZE];
42*f4a2713aSLionel Sambuc   };
43*f4a2713aSLionel Sambuc 
44*f4a2713aSLionel Sambuc   IdDeclInfoPool *CurPool;
45*f4a2713aSLionel Sambuc   unsigned int CurIndex;
46*f4a2713aSLionel Sambuc 
47*f4a2713aSLionel Sambuc public:
48*f4a2713aSLionel Sambuc   IdDeclInfoMap() : CurPool(0), CurIndex(POOL_SIZE) {}
49*f4a2713aSLionel Sambuc 
50*f4a2713aSLionel Sambuc   ~IdDeclInfoMap() {
51*f4a2713aSLionel Sambuc     IdDeclInfoPool *Cur = CurPool;
52*f4a2713aSLionel Sambuc     while (IdDeclInfoPool *P = Cur) {
53*f4a2713aSLionel Sambuc       Cur = Cur->Next;
54*f4a2713aSLionel Sambuc       delete P;
55*f4a2713aSLionel Sambuc     }
56*f4a2713aSLionel Sambuc   }
57*f4a2713aSLionel Sambuc 
58*f4a2713aSLionel Sambuc   /// Returns the IdDeclInfo associated to the DeclarationName.
59*f4a2713aSLionel Sambuc   /// It creates a new IdDeclInfo if one was not created before for this id.
60*f4a2713aSLionel Sambuc   IdDeclInfo &operator[](DeclarationName Name);
61*f4a2713aSLionel Sambuc };
62*f4a2713aSLionel Sambuc 
63*f4a2713aSLionel Sambuc 
64*f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
65*f4a2713aSLionel Sambuc // IdDeclInfo Implementation
66*f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
67*f4a2713aSLionel Sambuc 
68*f4a2713aSLionel Sambuc /// RemoveDecl - Remove the decl from the scope chain.
69*f4a2713aSLionel Sambuc /// The decl must already be part of the decl chain.
70*f4a2713aSLionel Sambuc void IdentifierResolver::IdDeclInfo::RemoveDecl(NamedDecl *D) {
71*f4a2713aSLionel Sambuc   for (DeclsTy::iterator I = Decls.end(); I != Decls.begin(); --I) {
72*f4a2713aSLionel Sambuc     if (D == *(I-1)) {
73*f4a2713aSLionel Sambuc       Decls.erase(I-1);
74*f4a2713aSLionel Sambuc       return;
75*f4a2713aSLionel Sambuc     }
76*f4a2713aSLionel Sambuc   }
77*f4a2713aSLionel Sambuc 
78*f4a2713aSLionel Sambuc   llvm_unreachable("Didn't find this decl on its identifier's chain!");
79*f4a2713aSLionel Sambuc }
80*f4a2713aSLionel Sambuc 
81*f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
82*f4a2713aSLionel Sambuc // IdentifierResolver Implementation
83*f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
84*f4a2713aSLionel Sambuc 
85*f4a2713aSLionel Sambuc IdentifierResolver::IdentifierResolver(Preprocessor &PP)
86*f4a2713aSLionel Sambuc   : LangOpt(PP.getLangOpts()), PP(PP),
87*f4a2713aSLionel Sambuc     IdDeclInfos(new IdDeclInfoMap) {
88*f4a2713aSLionel Sambuc }
89*f4a2713aSLionel Sambuc 
90*f4a2713aSLionel Sambuc IdentifierResolver::~IdentifierResolver() {
91*f4a2713aSLionel Sambuc   delete IdDeclInfos;
92*f4a2713aSLionel Sambuc }
93*f4a2713aSLionel Sambuc 
94*f4a2713aSLionel Sambuc /// isDeclInScope - If 'Ctx' is a function/method, isDeclInScope returns true
95*f4a2713aSLionel Sambuc /// if 'D' is in Scope 'S', otherwise 'S' is ignored and isDeclInScope returns
96*f4a2713aSLionel Sambuc /// true if 'D' belongs to the given declaration context.
97*f4a2713aSLionel Sambuc bool IdentifierResolver::isDeclInScope(Decl *D, DeclContext *Ctx, Scope *S,
98*f4a2713aSLionel Sambuc                              bool ExplicitInstantiationOrSpecialization) const {
99*f4a2713aSLionel Sambuc   Ctx = Ctx->getRedeclContext();
100*f4a2713aSLionel Sambuc 
101*f4a2713aSLionel Sambuc   if (Ctx->isFunctionOrMethod() || S->isFunctionPrototypeScope()) {
102*f4a2713aSLionel Sambuc     // Ignore the scopes associated within transparent declaration contexts.
103*f4a2713aSLionel Sambuc     while (S->getEntity() && S->getEntity()->isTransparentContext())
104*f4a2713aSLionel Sambuc       S = S->getParent();
105*f4a2713aSLionel Sambuc 
106*f4a2713aSLionel Sambuc     if (S->isDeclScope(D))
107*f4a2713aSLionel Sambuc       return true;
108*f4a2713aSLionel Sambuc     if (LangOpt.CPlusPlus) {
109*f4a2713aSLionel Sambuc       // C++ 3.3.2p3:
110*f4a2713aSLionel Sambuc       // The name declared in a catch exception-declaration is local to the
111*f4a2713aSLionel Sambuc       // handler and shall not be redeclared in the outermost block of the
112*f4a2713aSLionel Sambuc       // handler.
113*f4a2713aSLionel Sambuc       // C++ 3.3.2p4:
114*f4a2713aSLionel Sambuc       // Names declared in the for-init-statement, and in the condition of if,
115*f4a2713aSLionel Sambuc       // while, for, and switch statements are local to the if, while, for, or
116*f4a2713aSLionel Sambuc       // switch statement (including the controlled statement), and shall not be
117*f4a2713aSLionel Sambuc       // redeclared in a subsequent condition of that statement nor in the
118*f4a2713aSLionel Sambuc       // outermost block (or, for the if statement, any of the outermost blocks)
119*f4a2713aSLionel Sambuc       // of the controlled statement.
120*f4a2713aSLionel Sambuc       //
121*f4a2713aSLionel Sambuc       assert(S->getParent() && "No TUScope?");
122*f4a2713aSLionel Sambuc       if (S->getParent()->getFlags() & Scope::ControlScope) {
123*f4a2713aSLionel Sambuc         S = S->getParent();
124*f4a2713aSLionel Sambuc         if (S->isDeclScope(D))
125*f4a2713aSLionel Sambuc           return true;
126*f4a2713aSLionel Sambuc       }
127*f4a2713aSLionel Sambuc       if (S->getFlags() & Scope::FnTryCatchScope)
128*f4a2713aSLionel Sambuc         return S->getParent()->isDeclScope(D);
129*f4a2713aSLionel Sambuc     }
130*f4a2713aSLionel Sambuc     return false;
131*f4a2713aSLionel Sambuc   }
132*f4a2713aSLionel Sambuc 
133*f4a2713aSLionel Sambuc   DeclContext *DCtx = D->getDeclContext()->getRedeclContext();
134*f4a2713aSLionel Sambuc   return ExplicitInstantiationOrSpecialization
135*f4a2713aSLionel Sambuc            ? Ctx->InEnclosingNamespaceSetOf(DCtx)
136*f4a2713aSLionel Sambuc            : Ctx->Equals(DCtx);
137*f4a2713aSLionel Sambuc }
138*f4a2713aSLionel Sambuc 
139*f4a2713aSLionel Sambuc /// AddDecl - Link the decl to its shadowed decl chain.
140*f4a2713aSLionel Sambuc void IdentifierResolver::AddDecl(NamedDecl *D) {
141*f4a2713aSLionel Sambuc   DeclarationName Name = D->getDeclName();
142*f4a2713aSLionel Sambuc   if (IdentifierInfo *II = Name.getAsIdentifierInfo())
143*f4a2713aSLionel Sambuc     updatingIdentifier(*II);
144*f4a2713aSLionel Sambuc 
145*f4a2713aSLionel Sambuc   void *Ptr = Name.getFETokenInfo<void>();
146*f4a2713aSLionel Sambuc 
147*f4a2713aSLionel Sambuc   if (!Ptr) {
148*f4a2713aSLionel Sambuc     Name.setFETokenInfo(D);
149*f4a2713aSLionel Sambuc     return;
150*f4a2713aSLionel Sambuc   }
151*f4a2713aSLionel Sambuc 
152*f4a2713aSLionel Sambuc   IdDeclInfo *IDI;
153*f4a2713aSLionel Sambuc 
154*f4a2713aSLionel Sambuc   if (isDeclPtr(Ptr)) {
155*f4a2713aSLionel Sambuc     Name.setFETokenInfo(NULL);
156*f4a2713aSLionel Sambuc     IDI = &(*IdDeclInfos)[Name];
157*f4a2713aSLionel Sambuc     NamedDecl *PrevD = static_cast<NamedDecl*>(Ptr);
158*f4a2713aSLionel Sambuc     IDI->AddDecl(PrevD);
159*f4a2713aSLionel Sambuc   } else
160*f4a2713aSLionel Sambuc     IDI = toIdDeclInfo(Ptr);
161*f4a2713aSLionel Sambuc 
162*f4a2713aSLionel Sambuc   IDI->AddDecl(D);
163*f4a2713aSLionel Sambuc }
164*f4a2713aSLionel Sambuc 
165*f4a2713aSLionel Sambuc void IdentifierResolver::InsertDeclAfter(iterator Pos, NamedDecl *D) {
166*f4a2713aSLionel Sambuc   DeclarationName Name = D->getDeclName();
167*f4a2713aSLionel Sambuc   if (IdentifierInfo *II = Name.getAsIdentifierInfo())
168*f4a2713aSLionel Sambuc     updatingIdentifier(*II);
169*f4a2713aSLionel Sambuc 
170*f4a2713aSLionel Sambuc   void *Ptr = Name.getFETokenInfo<void>();
171*f4a2713aSLionel Sambuc 
172*f4a2713aSLionel Sambuc   if (!Ptr) {
173*f4a2713aSLionel Sambuc     AddDecl(D);
174*f4a2713aSLionel Sambuc     return;
175*f4a2713aSLionel Sambuc   }
176*f4a2713aSLionel Sambuc 
177*f4a2713aSLionel Sambuc   if (isDeclPtr(Ptr)) {
178*f4a2713aSLionel Sambuc     // We only have a single declaration: insert before or after it,
179*f4a2713aSLionel Sambuc     // as appropriate.
180*f4a2713aSLionel Sambuc     if (Pos == iterator()) {
181*f4a2713aSLionel Sambuc       // Add the new declaration before the existing declaration.
182*f4a2713aSLionel Sambuc       NamedDecl *PrevD = static_cast<NamedDecl*>(Ptr);
183*f4a2713aSLionel Sambuc       RemoveDecl(PrevD);
184*f4a2713aSLionel Sambuc       AddDecl(D);
185*f4a2713aSLionel Sambuc       AddDecl(PrevD);
186*f4a2713aSLionel Sambuc     } else {
187*f4a2713aSLionel Sambuc       // Add new declaration after the existing declaration.
188*f4a2713aSLionel Sambuc       AddDecl(D);
189*f4a2713aSLionel Sambuc     }
190*f4a2713aSLionel Sambuc 
191*f4a2713aSLionel Sambuc     return;
192*f4a2713aSLionel Sambuc   }
193*f4a2713aSLionel Sambuc 
194*f4a2713aSLionel Sambuc   // General case: insert the declaration at the appropriate point in the
195*f4a2713aSLionel Sambuc   // list, which already has at least two elements.
196*f4a2713aSLionel Sambuc   IdDeclInfo *IDI = toIdDeclInfo(Ptr);
197*f4a2713aSLionel Sambuc   if (Pos.isIterator()) {
198*f4a2713aSLionel Sambuc     IDI->InsertDecl(Pos.getIterator() + 1, D);
199*f4a2713aSLionel Sambuc   } else
200*f4a2713aSLionel Sambuc     IDI->InsertDecl(IDI->decls_begin(), D);
201*f4a2713aSLionel Sambuc }
202*f4a2713aSLionel Sambuc 
203*f4a2713aSLionel Sambuc /// RemoveDecl - Unlink the decl from its shadowed decl chain.
204*f4a2713aSLionel Sambuc /// The decl must already be part of the decl chain.
205*f4a2713aSLionel Sambuc void IdentifierResolver::RemoveDecl(NamedDecl *D) {
206*f4a2713aSLionel Sambuc   assert(D && "null param passed");
207*f4a2713aSLionel Sambuc   DeclarationName Name = D->getDeclName();
208*f4a2713aSLionel Sambuc   if (IdentifierInfo *II = Name.getAsIdentifierInfo())
209*f4a2713aSLionel Sambuc     updatingIdentifier(*II);
210*f4a2713aSLionel Sambuc 
211*f4a2713aSLionel Sambuc   void *Ptr = Name.getFETokenInfo<void>();
212*f4a2713aSLionel Sambuc 
213*f4a2713aSLionel Sambuc   assert(Ptr && "Didn't find this decl on its identifier's chain!");
214*f4a2713aSLionel Sambuc 
215*f4a2713aSLionel Sambuc   if (isDeclPtr(Ptr)) {
216*f4a2713aSLionel Sambuc     assert(D == Ptr && "Didn't find this decl on its identifier's chain!");
217*f4a2713aSLionel Sambuc     Name.setFETokenInfo(NULL);
218*f4a2713aSLionel Sambuc     return;
219*f4a2713aSLionel Sambuc   }
220*f4a2713aSLionel Sambuc 
221*f4a2713aSLionel Sambuc   return toIdDeclInfo(Ptr)->RemoveDecl(D);
222*f4a2713aSLionel Sambuc }
223*f4a2713aSLionel Sambuc 
224*f4a2713aSLionel Sambuc /// begin - Returns an iterator for decls with name 'Name'.
225*f4a2713aSLionel Sambuc IdentifierResolver::iterator
226*f4a2713aSLionel Sambuc IdentifierResolver::begin(DeclarationName Name) {
227*f4a2713aSLionel Sambuc   if (IdentifierInfo *II = Name.getAsIdentifierInfo())
228*f4a2713aSLionel Sambuc     readingIdentifier(*II);
229*f4a2713aSLionel Sambuc 
230*f4a2713aSLionel Sambuc   void *Ptr = Name.getFETokenInfo<void>();
231*f4a2713aSLionel Sambuc   if (!Ptr) return end();
232*f4a2713aSLionel Sambuc 
233*f4a2713aSLionel Sambuc   if (isDeclPtr(Ptr))
234*f4a2713aSLionel Sambuc     return iterator(static_cast<NamedDecl*>(Ptr));
235*f4a2713aSLionel Sambuc 
236*f4a2713aSLionel Sambuc   IdDeclInfo *IDI = toIdDeclInfo(Ptr);
237*f4a2713aSLionel Sambuc 
238*f4a2713aSLionel Sambuc   IdDeclInfo::DeclsTy::iterator I = IDI->decls_end();
239*f4a2713aSLionel Sambuc   if (I != IDI->decls_begin())
240*f4a2713aSLionel Sambuc     return iterator(I-1);
241*f4a2713aSLionel Sambuc   // No decls found.
242*f4a2713aSLionel Sambuc   return end();
243*f4a2713aSLionel Sambuc }
244*f4a2713aSLionel Sambuc 
245*f4a2713aSLionel Sambuc namespace {
246*f4a2713aSLionel Sambuc   enum DeclMatchKind {
247*f4a2713aSLionel Sambuc     DMK_Different,
248*f4a2713aSLionel Sambuc     DMK_Replace,
249*f4a2713aSLionel Sambuc     DMK_Ignore
250*f4a2713aSLionel Sambuc   };
251*f4a2713aSLionel Sambuc }
252*f4a2713aSLionel Sambuc 
253*f4a2713aSLionel Sambuc /// \brief Compare two declarations to see whether they are different or,
254*f4a2713aSLionel Sambuc /// if they are the same, whether the new declaration should replace the
255*f4a2713aSLionel Sambuc /// existing declaration.
256*f4a2713aSLionel Sambuc static DeclMatchKind compareDeclarations(NamedDecl *Existing, NamedDecl *New) {
257*f4a2713aSLionel Sambuc   // If the declarations are identical, ignore the new one.
258*f4a2713aSLionel Sambuc   if (Existing == New)
259*f4a2713aSLionel Sambuc     return DMK_Ignore;
260*f4a2713aSLionel Sambuc 
261*f4a2713aSLionel Sambuc   // If the declarations have different kinds, they're obviously different.
262*f4a2713aSLionel Sambuc   if (Existing->getKind() != New->getKind())
263*f4a2713aSLionel Sambuc     return DMK_Different;
264*f4a2713aSLionel Sambuc 
265*f4a2713aSLionel Sambuc   // If the declarations are redeclarations of each other, keep the newest one.
266*f4a2713aSLionel Sambuc   if (Existing->getCanonicalDecl() == New->getCanonicalDecl()) {
267*f4a2713aSLionel Sambuc     // If either of these is the most recent declaration, use it.
268*f4a2713aSLionel Sambuc     Decl *MostRecent = Existing->getMostRecentDecl();
269*f4a2713aSLionel Sambuc     if (Existing == MostRecent)
270*f4a2713aSLionel Sambuc       return DMK_Ignore;
271*f4a2713aSLionel Sambuc 
272*f4a2713aSLionel Sambuc     if (New == MostRecent)
273*f4a2713aSLionel Sambuc       return DMK_Replace;
274*f4a2713aSLionel Sambuc 
275*f4a2713aSLionel Sambuc     // If the existing declaration is somewhere in the previous declaration
276*f4a2713aSLionel Sambuc     // chain of the new declaration, then prefer the new declaration.
277*f4a2713aSLionel Sambuc     for (Decl::redecl_iterator RD = New->redecls_begin(),
278*f4a2713aSLionel Sambuc                             RDEnd = New->redecls_end();
279*f4a2713aSLionel Sambuc          RD != RDEnd; ++RD) {
280*f4a2713aSLionel Sambuc       if (*RD == Existing)
281*f4a2713aSLionel Sambuc         return DMK_Replace;
282*f4a2713aSLionel Sambuc 
283*f4a2713aSLionel Sambuc       if (RD->isCanonicalDecl())
284*f4a2713aSLionel Sambuc         break;
285*f4a2713aSLionel Sambuc     }
286*f4a2713aSLionel Sambuc 
287*f4a2713aSLionel Sambuc     return DMK_Ignore;
288*f4a2713aSLionel Sambuc   }
289*f4a2713aSLionel Sambuc 
290*f4a2713aSLionel Sambuc   return DMK_Different;
291*f4a2713aSLionel Sambuc }
292*f4a2713aSLionel Sambuc 
293*f4a2713aSLionel Sambuc bool IdentifierResolver::tryAddTopLevelDecl(NamedDecl *D, DeclarationName Name){
294*f4a2713aSLionel Sambuc   if (IdentifierInfo *II = Name.getAsIdentifierInfo())
295*f4a2713aSLionel Sambuc     readingIdentifier(*II);
296*f4a2713aSLionel Sambuc 
297*f4a2713aSLionel Sambuc   void *Ptr = Name.getFETokenInfo<void>();
298*f4a2713aSLionel Sambuc 
299*f4a2713aSLionel Sambuc   if (!Ptr) {
300*f4a2713aSLionel Sambuc     Name.setFETokenInfo(D);
301*f4a2713aSLionel Sambuc     return true;
302*f4a2713aSLionel Sambuc   }
303*f4a2713aSLionel Sambuc 
304*f4a2713aSLionel Sambuc   IdDeclInfo *IDI;
305*f4a2713aSLionel Sambuc 
306*f4a2713aSLionel Sambuc   if (isDeclPtr(Ptr)) {
307*f4a2713aSLionel Sambuc     NamedDecl *PrevD = static_cast<NamedDecl*>(Ptr);
308*f4a2713aSLionel Sambuc 
309*f4a2713aSLionel Sambuc     switch (compareDeclarations(PrevD, D)) {
310*f4a2713aSLionel Sambuc     case DMK_Different:
311*f4a2713aSLionel Sambuc       break;
312*f4a2713aSLionel Sambuc 
313*f4a2713aSLionel Sambuc     case DMK_Ignore:
314*f4a2713aSLionel Sambuc       return false;
315*f4a2713aSLionel Sambuc 
316*f4a2713aSLionel Sambuc     case DMK_Replace:
317*f4a2713aSLionel Sambuc       Name.setFETokenInfo(D);
318*f4a2713aSLionel Sambuc       return true;
319*f4a2713aSLionel Sambuc     }
320*f4a2713aSLionel Sambuc 
321*f4a2713aSLionel Sambuc     Name.setFETokenInfo(NULL);
322*f4a2713aSLionel Sambuc     IDI = &(*IdDeclInfos)[Name];
323*f4a2713aSLionel Sambuc 
324*f4a2713aSLionel Sambuc     // If the existing declaration is not visible in translation unit scope,
325*f4a2713aSLionel Sambuc     // then add the new top-level declaration first.
326*f4a2713aSLionel Sambuc     if (!PrevD->getDeclContext()->getRedeclContext()->isTranslationUnit()) {
327*f4a2713aSLionel Sambuc       IDI->AddDecl(D);
328*f4a2713aSLionel Sambuc       IDI->AddDecl(PrevD);
329*f4a2713aSLionel Sambuc     } else {
330*f4a2713aSLionel Sambuc       IDI->AddDecl(PrevD);
331*f4a2713aSLionel Sambuc       IDI->AddDecl(D);
332*f4a2713aSLionel Sambuc     }
333*f4a2713aSLionel Sambuc     return true;
334*f4a2713aSLionel Sambuc   }
335*f4a2713aSLionel Sambuc 
336*f4a2713aSLionel Sambuc   IDI = toIdDeclInfo(Ptr);
337*f4a2713aSLionel Sambuc 
338*f4a2713aSLionel Sambuc   // See whether this declaration is identical to any existing declarations.
339*f4a2713aSLionel Sambuc   // If not, find the right place to insert it.
340*f4a2713aSLionel Sambuc   for (IdDeclInfo::DeclsTy::iterator I = IDI->decls_begin(),
341*f4a2713aSLionel Sambuc                                   IEnd = IDI->decls_end();
342*f4a2713aSLionel Sambuc        I != IEnd; ++I) {
343*f4a2713aSLionel Sambuc 
344*f4a2713aSLionel Sambuc     switch (compareDeclarations(*I, D)) {
345*f4a2713aSLionel Sambuc     case DMK_Different:
346*f4a2713aSLionel Sambuc       break;
347*f4a2713aSLionel Sambuc 
348*f4a2713aSLionel Sambuc     case DMK_Ignore:
349*f4a2713aSLionel Sambuc       return false;
350*f4a2713aSLionel Sambuc 
351*f4a2713aSLionel Sambuc     case DMK_Replace:
352*f4a2713aSLionel Sambuc       *I = D;
353*f4a2713aSLionel Sambuc       return true;
354*f4a2713aSLionel Sambuc     }
355*f4a2713aSLionel Sambuc 
356*f4a2713aSLionel Sambuc     if (!(*I)->getDeclContext()->getRedeclContext()->isTranslationUnit()) {
357*f4a2713aSLionel Sambuc       // We've found a declaration that is not visible from the translation
358*f4a2713aSLionel Sambuc       // unit (it's in an inner scope). Insert our declaration here.
359*f4a2713aSLionel Sambuc       IDI->InsertDecl(I, D);
360*f4a2713aSLionel Sambuc       return true;
361*f4a2713aSLionel Sambuc     }
362*f4a2713aSLionel Sambuc   }
363*f4a2713aSLionel Sambuc 
364*f4a2713aSLionel Sambuc   // Add the declaration to the end.
365*f4a2713aSLionel Sambuc   IDI->AddDecl(D);
366*f4a2713aSLionel Sambuc   return true;
367*f4a2713aSLionel Sambuc }
368*f4a2713aSLionel Sambuc 
369*f4a2713aSLionel Sambuc void IdentifierResolver::readingIdentifier(IdentifierInfo &II) {
370*f4a2713aSLionel Sambuc   if (II.isOutOfDate())
371*f4a2713aSLionel Sambuc     PP.getExternalSource()->updateOutOfDateIdentifier(II);
372*f4a2713aSLionel Sambuc }
373*f4a2713aSLionel Sambuc 
374*f4a2713aSLionel Sambuc void IdentifierResolver::updatingIdentifier(IdentifierInfo &II) {
375*f4a2713aSLionel Sambuc   if (II.isOutOfDate())
376*f4a2713aSLionel Sambuc     PP.getExternalSource()->updateOutOfDateIdentifier(II);
377*f4a2713aSLionel Sambuc 
378*f4a2713aSLionel Sambuc   if (II.isFromAST())
379*f4a2713aSLionel Sambuc     II.setChangedSinceDeserialization();
380*f4a2713aSLionel Sambuc }
381*f4a2713aSLionel Sambuc 
382*f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
383*f4a2713aSLionel Sambuc // IdDeclInfoMap Implementation
384*f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
385*f4a2713aSLionel Sambuc 
386*f4a2713aSLionel Sambuc /// Returns the IdDeclInfo associated to the DeclarationName.
387*f4a2713aSLionel Sambuc /// It creates a new IdDeclInfo if one was not created before for this id.
388*f4a2713aSLionel Sambuc IdentifierResolver::IdDeclInfo &
389*f4a2713aSLionel Sambuc IdentifierResolver::IdDeclInfoMap::operator[](DeclarationName Name) {
390*f4a2713aSLionel Sambuc   void *Ptr = Name.getFETokenInfo<void>();
391*f4a2713aSLionel Sambuc 
392*f4a2713aSLionel Sambuc   if (Ptr) return *toIdDeclInfo(Ptr);
393*f4a2713aSLionel Sambuc 
394*f4a2713aSLionel Sambuc   if (CurIndex == POOL_SIZE) {
395*f4a2713aSLionel Sambuc     CurPool = new IdDeclInfoPool(CurPool);
396*f4a2713aSLionel Sambuc     CurIndex = 0;
397*f4a2713aSLionel Sambuc   }
398*f4a2713aSLionel Sambuc   IdDeclInfo *IDI = &CurPool->Pool[CurIndex];
399*f4a2713aSLionel Sambuc   Name.setFETokenInfo(reinterpret_cast<void*>(
400*f4a2713aSLionel Sambuc                               reinterpret_cast<uintptr_t>(IDI) | 0x1)
401*f4a2713aSLionel Sambuc                                                                      );
402*f4a2713aSLionel Sambuc   ++CurIndex;
403*f4a2713aSLionel Sambuc   return *IDI;
404*f4a2713aSLionel Sambuc }
405*f4a2713aSLionel Sambuc 
406*f4a2713aSLionel Sambuc void IdentifierResolver::iterator::incrementSlowCase() {
407*f4a2713aSLionel Sambuc   NamedDecl *D = **this;
408*f4a2713aSLionel Sambuc   void *InfoPtr = D->getDeclName().getFETokenInfo<void>();
409*f4a2713aSLionel Sambuc   assert(!isDeclPtr(InfoPtr) && "Decl with wrong id ?");
410*f4a2713aSLionel Sambuc   IdDeclInfo *Info = toIdDeclInfo(InfoPtr);
411*f4a2713aSLionel Sambuc 
412*f4a2713aSLionel Sambuc   BaseIter I = getIterator();
413*f4a2713aSLionel Sambuc   if (I != Info->decls_begin())
414*f4a2713aSLionel Sambuc     *this = iterator(I-1);
415*f4a2713aSLionel Sambuc   else // No more decls.
416*f4a2713aSLionel Sambuc     *this = iterator();
417*f4a2713aSLionel Sambuc }
418