blob: a8ca34dca54ebc9cc6c5eda76e1c8683262875ee [file] [log] [blame]
Eli Friedman56d29372008-06-07 16:52:53 +00001//===--- DeclBase.cpp - Declaration AST Node Implementation ---------------===//
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 Decl and DeclContext classes.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/DeclBase.h"
Douglas Gregor64650af2009-02-02 23:39:07 +000015#include "clang/AST/Decl.h"
Argyrios Kyrtzidisd3bb44f2008-06-09 21:05:31 +000016#include "clang/AST/DeclCXX.h"
Douglas Gregoraaba5e32009-02-04 19:02:06 +000017#include "clang/AST/DeclObjC.h"
18#include "clang/AST/DeclTemplate.h"
Eli Friedman56d29372008-06-07 16:52:53 +000019#include "clang/AST/ASTContext.h"
Douglas Gregor44b43212008-12-11 16:49:14 +000020#include "clang/AST/Type.h"
Eli Friedman56d29372008-06-07 16:52:53 +000021#include "llvm/ADT/DenseMap.h"
Douglas Gregor6ed40e32008-12-23 21:05:05 +000022#include <algorithm>
23#include <functional>
Douglas Gregor3fc749d2008-12-23 00:26:44 +000024#include <vector>
Eli Friedman56d29372008-06-07 16:52:53 +000025using namespace clang;
26
27//===----------------------------------------------------------------------===//
28// Statistics
29//===----------------------------------------------------------------------===//
30
Douglas Gregor64650af2009-02-02 23:39:07 +000031#define DECL(Derived, Base) static int n##Derived##s = 0;
32#include "clang/AST/DeclNodes.def"
Eli Friedman56d29372008-06-07 16:52:53 +000033
34static bool StatSwitch = false;
35
36// This keeps track of all decl attributes. Since so few decls have attrs, we
37// keep them in a hash map instead of wasting space in the Decl class.
38typedef llvm::DenseMap<const Decl*, Attr*> DeclAttrMapTy;
39
40static DeclAttrMapTy *DeclAttrs = 0;
41
42const char *Decl::getDeclKindName() const {
43 switch (DeclKind) {
Douglas Gregor64650af2009-02-02 23:39:07 +000044 default: assert(0 && "Declaration not in DeclNodes.def!");
45#define DECL(Derived, Base) case Derived: return #Derived;
46#include "clang/AST/DeclNodes.def"
Eli Friedman56d29372008-06-07 16:52:53 +000047 }
48}
49
Steve Naroff0a473932009-01-20 19:53:53 +000050const char *DeclContext::getDeclKindName() const {
51 switch (DeclKind) {
Douglas Gregor64650af2009-02-02 23:39:07 +000052 default: assert(0 && "Declaration context not in DeclNodes.def!");
Argyrios Kyrtzidis1ad4dd72009-02-16 14:28:33 +000053#define DECL(Derived, Base) case Decl::Derived: return #Derived;
Douglas Gregor64650af2009-02-02 23:39:07 +000054#include "clang/AST/DeclNodes.def"
Steve Naroff0a473932009-01-20 19:53:53 +000055 }
56}
57
Eli Friedman56d29372008-06-07 16:52:53 +000058bool Decl::CollectingStats(bool Enable) {
59 if (Enable)
60 StatSwitch = true;
61 return StatSwitch;
62}
63
64void Decl::PrintStats() {
65 fprintf(stderr, "*** Decl Stats:\n");
Eli Friedman56d29372008-06-07 16:52:53 +000066
Douglas Gregor64650af2009-02-02 23:39:07 +000067 int totalDecls = 0;
68#define DECL(Derived, Base) totalDecls += n##Derived##s;
69#include "clang/AST/DeclNodes.def"
70 fprintf(stderr, " %d decls total.\n", totalDecls);
71
72 int totalBytes = 0;
73#define DECL(Derived, Base) \
74 if (n##Derived##s > 0) { \
75 totalBytes += (int)(n##Derived##s * sizeof(Derived##Decl)); \
76 fprintf(stderr, " %d " #Derived " decls, %d each (%d bytes)\n", \
77 n##Derived##s, (int)sizeof(Derived##Decl), \
78 (int)(n##Derived##s * sizeof(Derived##Decl))); \
79 }
80#include "clang/AST/DeclNodes.def"
Eli Friedman56d29372008-06-07 16:52:53 +000081
Douglas Gregor64650af2009-02-02 23:39:07 +000082 fprintf(stderr, "Total bytes = %d\n", totalBytes);
Eli Friedman56d29372008-06-07 16:52:53 +000083}
84
85void Decl::addDeclKind(Kind k) {
86 switch (k) {
Douglas Gregor64650af2009-02-02 23:39:07 +000087 default: assert(0 && "Declaration not in DeclNodes.def!");
88#define DECL(Derived, Base) case Derived: ++n##Derived##s; break;
89#include "clang/AST/DeclNodes.def"
Eli Friedman56d29372008-06-07 16:52:53 +000090 }
91}
92
93//===----------------------------------------------------------------------===//
94// Decl Implementation
95//===----------------------------------------------------------------------===//
96
Douglas Gregor4afa39d2009-01-20 01:17:11 +000097void Decl::setDeclContext(DeclContext *DC) {
98 if (isOutOfSemaDC())
99 delete getMultipleDC();
100
101 DeclCtx = reinterpret_cast<uintptr_t>(DC);
102}
103
104void Decl::setLexicalDeclContext(DeclContext *DC) {
105 if (DC == getLexicalDeclContext())
106 return;
107
108 if (isInSemaDC()) {
109 MultipleDC *MDC = new MultipleDC();
110 MDC->SemanticDC = getDeclContext();
111 MDC->LexicalDC = DC;
112 DeclCtx = reinterpret_cast<uintptr_t>(MDC) | 0x1;
113 } else {
114 getMultipleDC()->LexicalDC = DC;
115 }
116}
117
Eli Friedman56d29372008-06-07 16:52:53 +0000118// Out-of-line virtual method providing a home for Decl.
119Decl::~Decl() {
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000120 if (isOutOfSemaDC())
121 delete getMultipleDC();
122
Eli Friedman56d29372008-06-07 16:52:53 +0000123 if (!HasAttrs)
124 return;
125
126 DeclAttrMapTy::iterator it = DeclAttrs->find(this);
127 assert(it != DeclAttrs->end() && "No attrs found but HasAttrs is true!");
128
129 // release attributes.
130 delete it->second;
131 invalidateAttrs();
132}
133
134void Decl::addAttr(Attr *NewAttr) {
135 if (!DeclAttrs)
136 DeclAttrs = new DeclAttrMapTy();
137
138 Attr *&ExistingAttr = (*DeclAttrs)[this];
139
140 NewAttr->setNext(ExistingAttr);
141 ExistingAttr = NewAttr;
142
143 HasAttrs = true;
144}
145
146void Decl::invalidateAttrs() {
147 if (!HasAttrs) return;
148
149 HasAttrs = false;
150 (*DeclAttrs)[this] = 0;
151 DeclAttrs->erase(this);
152
153 if (DeclAttrs->empty()) {
154 delete DeclAttrs;
155 DeclAttrs = 0;
156 }
157}
158
159const Attr *Decl::getAttrs() const {
160 if (!HasAttrs)
161 return 0;
162
163 return (*DeclAttrs)[this];
164}
165
166void Decl::swapAttrs(Decl *RHS) {
167 bool HasLHSAttr = this->HasAttrs;
168 bool HasRHSAttr = RHS->HasAttrs;
169
170 // Usually, neither decl has attrs, nothing to do.
171 if (!HasLHSAttr && !HasRHSAttr) return;
172
173 // If 'this' has no attrs, swap the other way.
174 if (!HasLHSAttr)
175 return RHS->swapAttrs(this);
176
177 // Handle the case when both decls have attrs.
178 if (HasRHSAttr) {
179 std::swap((*DeclAttrs)[this], (*DeclAttrs)[RHS]);
180 return;
181 }
182
183 // Otherwise, LHS has an attr and RHS doesn't.
184 (*DeclAttrs)[RHS] = (*DeclAttrs)[this];
185 (*DeclAttrs).erase(this);
186 this->HasAttrs = false;
187 RHS->HasAttrs = true;
188}
189
190
191void Decl::Destroy(ASTContext& C) {
Douglas Gregora0fc55f2009-01-13 19:47:12 +0000192#if 0
Douglas Gregor00ad0ef2009-01-20 04:25:11 +0000193 // FIXME: Once ownership is fully understood, we can enable this code
194 if (DeclContext *DC = dyn_cast<DeclContext>(this))
195 DC->decls_begin()->Destroy(C);
Eli Friedman56d29372008-06-07 16:52:53 +0000196
Douglas Gregor00ad0ef2009-01-20 04:25:11 +0000197 // Observe the unrolled recursion. By setting N->NextDeclInScope = 0x0
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000198 // within the loop, only the Destroy method for the first Decl
199 // will deallocate all of the Decls in a chain.
200
Douglas Gregor00ad0ef2009-01-20 04:25:11 +0000201 Decl* N = NextDeclInScope;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000202
203 while (N) {
Douglas Gregor00ad0ef2009-01-20 04:25:11 +0000204 Decl* Tmp = N->NextDeclInScope;
205 N->NextDeclInScope = 0;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000206 N->Destroy(C);
207 N = Tmp;
Eli Friedman56d29372008-06-07 16:52:53 +0000208 }
Douglas Gregora0fc55f2009-01-13 19:47:12 +0000209
Eli Friedman56d29372008-06-07 16:52:53 +0000210 this->~Decl();
Steve Naroff3e970492009-01-27 21:25:57 +0000211 C.Deallocate((void *)this);
Douglas Gregor00ad0ef2009-01-20 04:25:11 +0000212#endif
Eli Friedman56d29372008-06-07 16:52:53 +0000213}
214
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000215Decl *Decl::castFromDeclContext (const DeclContext *D) {
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000216 Decl::Kind DK = D->getDeclKind();
217 switch(DK) {
218#define DECL_CONTEXT(Name) \
219 case Decl::Name: \
220 return static_cast<Name##Decl*>(const_cast<DeclContext*>(D));
221#define DECL_CONTEXT_BASE(Name)
222#include "clang/AST/DeclNodes.def"
223 default:
224#define DECL_CONTEXT_BASE(Name) \
225 if (DK >= Decl::Name##First && DK <= Decl::Name##Last) \
226 return static_cast<Name##Decl*>(const_cast<DeclContext*>(D));
227#include "clang/AST/DeclNodes.def"
228 assert(false && "a decl that inherits DeclContext isn't handled");
229 return 0;
230 }
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000231}
232
233DeclContext *Decl::castToDeclContext(const Decl *D) {
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000234 Decl::Kind DK = D->getKind();
235 switch(DK) {
236#define DECL_CONTEXT(Name) \
237 case Decl::Name: \
238 return static_cast<Name##Decl*>(const_cast<Decl*>(D));
239#define DECL_CONTEXT_BASE(Name)
240#include "clang/AST/DeclNodes.def"
241 default:
242#define DECL_CONTEXT_BASE(Name) \
243 if (DK >= Decl::Name##First && DK <= Decl::Name##Last) \
244 return static_cast<Name##Decl*>(const_cast<Decl*>(D));
245#include "clang/AST/DeclNodes.def"
246 assert(false && "a decl that inherits DeclContext isn't handled");
247 return 0;
248 }
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000249}
250
Eli Friedman56d29372008-06-07 16:52:53 +0000251//===----------------------------------------------------------------------===//
252// DeclContext Implementation
253//===----------------------------------------------------------------------===//
254
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000255bool DeclContext::classof(const Decl *D) {
256 switch (D->getKind()) {
257#define DECL_CONTEXT(Name) case Decl::Name:
258#define DECL_CONTEXT_BASE(Name)
259#include "clang/AST/DeclNodes.def"
260 return true;
261 default:
262#define DECL_CONTEXT_BASE(Name) \
263 if (D->getKind() >= Decl::Name##First && \
264 D->getKind() <= Decl::Name##Last) \
265 return true;
266#include "clang/AST/DeclNodes.def"
267 return false;
268 }
269}
270
Argyrios Kyrtzidis20bc6762008-11-19 17:36:39 +0000271const DeclContext *DeclContext::getParent() const {
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000272 if (const Decl *D = dyn_cast<Decl>(this))
273 return D->getDeclContext();
274
275 return NULL;
Eli Friedman56d29372008-06-07 16:52:53 +0000276}
Argyrios Kyrtzidis77407b82008-11-19 18:01:13 +0000277
278const DeclContext *DeclContext::getLexicalParent() const {
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000279 if (const Decl *D = dyn_cast<Decl>(this))
280 return D->getLexicalDeclContext();
281
Argyrios Kyrtzidis051c13a2008-11-19 18:07:24 +0000282 return getParent();
Argyrios Kyrtzidis77407b82008-11-19 18:01:13 +0000283}
Douglas Gregor44b43212008-12-11 16:49:14 +0000284
Douglas Gregor44b43212008-12-11 16:49:14 +0000285// FIXME: We really want to use a DenseSet here to eliminate the
286// redundant storage of the declaration names, but (1) it doesn't give
287// us the ability to search based on DeclarationName, (2) we really
288// need something more like a DenseMultiSet, and (3) it's
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000289// implemented in terms of DenseMap anyway. However, this data
290// structure is really space-inefficient, so we'll have to do
291// something.
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000292typedef llvm::DenseMap<DeclarationName, std::vector<NamedDecl*> >
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000293 StoredDeclsMap;
Douglas Gregor44b43212008-12-11 16:49:14 +0000294
295DeclContext::~DeclContext() {
296 unsigned Size = LookupPtr.getInt();
297 if (Size == LookupIsMap) {
298 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(LookupPtr.getPointer());
299 delete Map;
300 } else {
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000301 NamedDecl **Array = static_cast<NamedDecl**>(LookupPtr.getPointer());
Douglas Gregor44b43212008-12-11 16:49:14 +0000302 delete [] Array;
303 }
304}
305
306void DeclContext::DestroyDecls(ASTContext &C) {
Douglas Gregor00ad0ef2009-01-20 04:25:11 +0000307 for (decl_iterator D = decls_begin(); D != decls_end(); )
308 (*D++)->Destroy(C);
Douglas Gregor44b43212008-12-11 16:49:14 +0000309}
310
Douglas Gregor074149e2009-01-05 19:45:36 +0000311bool DeclContext::isTransparentContext() const {
312 if (DeclKind == Decl::Enum)
313 return true; // FIXME: Check for C++0x scoped enums
314 else if (DeclKind == Decl::LinkageSpec)
315 return true;
316 else if (DeclKind == Decl::Record || DeclKind == Decl::CXXRecord)
Douglas Gregorbcbffc42009-01-07 00:43:41 +0000317 return cast<RecordDecl>(this)->isAnonymousStructOrUnion();
Douglas Gregor074149e2009-01-05 19:45:36 +0000318 else if (DeclKind == Decl::Namespace)
319 return false; // FIXME: Check for C++0x inline namespaces
320
321 return false;
322}
323
Steve Naroff0701bbb2009-01-08 17:28:14 +0000324DeclContext *DeclContext::getPrimaryContext() {
Douglas Gregor44b43212008-12-11 16:49:14 +0000325 switch (DeclKind) {
Douglas Gregor44b43212008-12-11 16:49:14 +0000326 case Decl::TranslationUnit:
Douglas Gregor074149e2009-01-05 19:45:36 +0000327 case Decl::LinkageSpec:
328 case Decl::Block:
Douglas Gregor44b43212008-12-11 16:49:14 +0000329 // There is only one DeclContext for these entities.
330 return this;
331
332 case Decl::Namespace:
333 // The original namespace is our primary context.
334 return static_cast<NamespaceDecl*>(this)->getOriginalNamespace();
335
336 case Decl::Enum:
Douglas Gregor44b43212008-12-11 16:49:14 +0000337 case Decl::Record:
Douglas Gregor0b7a1582009-01-17 00:42:38 +0000338 case Decl::CXXRecord:
339 // If this is a tag type that has a definition or is currently
340 // being defined, that definition is our primary context.
341 if (TagType *TagT = cast_or_null<TagType>(cast<TagDecl>(this)->TypeForDecl))
342 if (TagT->isBeingDefined() ||
343 (TagT->getDecl() && TagT->getDecl()->isDefinition()))
344 return TagT->getDecl();
Douglas Gregor44b43212008-12-11 16:49:14 +0000345 return this;
Douglas Gregor44b43212008-12-11 16:49:14 +0000346
347 case Decl::ObjCMethod:
348 return this;
349
350 case Decl::ObjCInterface:
Steve Naroff0701bbb2009-01-08 17:28:14 +0000351 case Decl::ObjCProtocol:
352 case Decl::ObjCCategory:
Douglas Gregor44b43212008-12-11 16:49:14 +0000353 // FIXME: Can Objective-C interfaces be forward-declared?
354 return this;
355
Steve Naroff0701bbb2009-01-08 17:28:14 +0000356 case Decl::ObjCImplementation:
357 case Decl::ObjCCategoryImpl:
358 return this;
359
Douglas Gregor44b43212008-12-11 16:49:14 +0000360 default:
361 assert(DeclKind >= Decl::FunctionFirst && DeclKind <= Decl::FunctionLast &&
362 "Unknown DeclContext kind");
363 return this;
364 }
365}
366
367DeclContext *DeclContext::getNextContext() {
368 switch (DeclKind) {
Douglas Gregor44b43212008-12-11 16:49:14 +0000369 case Decl::TranslationUnit:
370 case Decl::Enum:
371 case Decl::Record:
372 case Decl::CXXRecord:
373 case Decl::ObjCMethod:
374 case Decl::ObjCInterface:
Steve Naroff0701bbb2009-01-08 17:28:14 +0000375 case Decl::ObjCCategory:
376 case Decl::ObjCProtocol:
377 case Decl::ObjCImplementation:
378 case Decl::ObjCCategoryImpl:
Douglas Gregor074149e2009-01-05 19:45:36 +0000379 case Decl::LinkageSpec:
380 case Decl::Block:
Douglas Gregor44b43212008-12-11 16:49:14 +0000381 // There is only one DeclContext for these entities.
382 return 0;
383
384 case Decl::Namespace:
385 // Return the next namespace
386 return static_cast<NamespaceDecl*>(this)->getNextNamespace();
387
388 default:
389 assert(DeclKind >= Decl::FunctionFirst && DeclKind <= Decl::FunctionLast &&
390 "Unknown DeclContext kind");
391 return 0;
392 }
393}
394
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000395void DeclContext::addDecl(Decl *D) {
Douglas Gregora8cc8ce2009-01-09 18:51:29 +0000396 assert(D->getLexicalDeclContext() == this && "Decl inserted into wrong lexical context");
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000397 assert(!D->NextDeclInScope && D != LastDecl &&
398 "Decl already inserted into a DeclContext");
399
400 if (FirstDecl) {
401 LastDecl->NextDeclInScope = D;
402 LastDecl = D;
403 } else {
404 FirstDecl = LastDecl = D;
405 }
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000406
407 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
Douglas Gregor40f4e692009-01-20 16:54:50 +0000408 ND->getDeclContext()->makeDeclVisibleInContext(ND);
Douglas Gregor44b43212008-12-11 16:49:14 +0000409}
410
Douglas Gregor074149e2009-01-05 19:45:36 +0000411/// buildLookup - Build the lookup data structure with all of the
412/// declarations in DCtx (and any other contexts linked to it or
413/// transparent contexts nested within it).
Steve Naroff0701bbb2009-01-08 17:28:14 +0000414void DeclContext::buildLookup(DeclContext *DCtx) {
Douglas Gregor074149e2009-01-05 19:45:36 +0000415 for (; DCtx; DCtx = DCtx->getNextContext()) {
Douglas Gregor4f3b8f82009-01-06 07:17:58 +0000416 for (decl_iterator D = DCtx->decls_begin(), DEnd = DCtx->decls_end();
417 D != DEnd; ++D) {
Douglas Gregor074149e2009-01-05 19:45:36 +0000418 // Insert this declaration into the lookup structure
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000419 if (NamedDecl *ND = dyn_cast<NamedDecl>(*D))
Douglas Gregor40f4e692009-01-20 16:54:50 +0000420 makeDeclVisibleInContextImpl(ND);
Douglas Gregor074149e2009-01-05 19:45:36 +0000421
422 // If this declaration is itself a transparent declaration context,
423 // add its members (recursively).
424 if (DeclContext *InnerCtx = dyn_cast<DeclContext>(*D))
425 if (InnerCtx->isTransparentContext())
Steve Naroff0701bbb2009-01-08 17:28:14 +0000426 buildLookup(InnerCtx->getPrimaryContext());
Douglas Gregor074149e2009-01-05 19:45:36 +0000427 }
428 }
429}
430
Douglas Gregor44b43212008-12-11 16:49:14 +0000431DeclContext::lookup_result
Steve Naroff0701bbb2009-01-08 17:28:14 +0000432DeclContext::lookup(DeclarationName Name) {
433 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor44b43212008-12-11 16:49:14 +0000434 if (PrimaryContext != this)
Steve Naroff0701bbb2009-01-08 17:28:14 +0000435 return PrimaryContext->lookup(Name);
Douglas Gregor44b43212008-12-11 16:49:14 +0000436
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000437 /// If there is no lookup data structure, build one now by walking
Douglas Gregor44b43212008-12-11 16:49:14 +0000438 /// all of the linked DeclContexts (in declaration order!) and
439 /// inserting their values.
Douglas Gregor074149e2009-01-05 19:45:36 +0000440 if (LookupPtr.getPointer() == 0)
Steve Naroff0701bbb2009-01-08 17:28:14 +0000441 buildLookup(this);
Douglas Gregor44b43212008-12-11 16:49:14 +0000442
Douglas Gregor44b43212008-12-11 16:49:14 +0000443 if (isLookupMap()) {
444 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(LookupPtr.getPointer());
445 StoredDeclsMap::iterator Pos = Map->find(Name);
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000446 if (Pos != Map->end())
447 return lookup_result(&Pos->second.front(),
448 &Pos->second.front() + Pos->second.size());
449 return lookup_result(0, 0);
Douglas Gregor44b43212008-12-11 16:49:14 +0000450 }
451
452 // We have a small array. Look into it.
453 unsigned Size = LookupPtr.getInt();
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000454 NamedDecl **Array = static_cast<NamedDecl**>(LookupPtr.getPointer());
Douglas Gregore267ff32008-12-11 20:41:00 +0000455 for (unsigned Idx = 0; Idx != Size; ++Idx)
Douglas Gregor44b43212008-12-11 16:49:14 +0000456 if (Array[Idx]->getDeclName() == Name) {
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000457 unsigned Last = Idx + 1;
458 while (Last != Size && Array[Last]->getDeclName() == Name)
459 ++Last;
460 return lookup_result(&Array[Idx], &Array[Last]);
Douglas Gregor44b43212008-12-11 16:49:14 +0000461 }
462
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000463 return lookup_result(0, 0);
Douglas Gregor44b43212008-12-11 16:49:14 +0000464}
465
466DeclContext::lookup_const_result
Steve Naroff0701bbb2009-01-08 17:28:14 +0000467DeclContext::lookup(DeclarationName Name) const {
468 return const_cast<DeclContext*>(this)->lookup(Name);
Douglas Gregor44b43212008-12-11 16:49:14 +0000469}
470
Douglas Gregor17a9b9e2009-01-07 02:48:43 +0000471const DeclContext *DeclContext::getLookupContext() const {
472 const DeclContext *Ctx = this;
Douglas Gregor72de6672009-01-08 20:45:30 +0000473 // Skip through transparent contexts.
Douglas Gregorce356072009-01-06 23:51:29 +0000474 while (Ctx->isTransparentContext())
475 Ctx = Ctx->getParent();
476 return Ctx;
477}
478
Douglas Gregor40f4e692009-01-20 16:54:50 +0000479void DeclContext::makeDeclVisibleInContext(NamedDecl *D) {
Steve Naroff0701bbb2009-01-08 17:28:14 +0000480 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor44b43212008-12-11 16:49:14 +0000481 if (PrimaryContext != this) {
Douglas Gregor40f4e692009-01-20 16:54:50 +0000482 PrimaryContext->makeDeclVisibleInContext(D);
Douglas Gregor44b43212008-12-11 16:49:14 +0000483 return;
484 }
485
486 // If we already have a lookup data structure, perform the insertion
487 // into it. Otherwise, be lazy and don't build that structure until
488 // someone asks for it.
489 if (LookupPtr.getPointer())
Douglas Gregor40f4e692009-01-20 16:54:50 +0000490 makeDeclVisibleInContextImpl(D);
Douglas Gregor074149e2009-01-05 19:45:36 +0000491
Douglas Gregor074149e2009-01-05 19:45:36 +0000492 // If we are a transparent context, insert into our parent context,
493 // too. This operation is recursive.
494 if (isTransparentContext())
Douglas Gregor40f4e692009-01-20 16:54:50 +0000495 getParent()->makeDeclVisibleInContext(D);
Douglas Gregor44b43212008-12-11 16:49:14 +0000496}
497
Douglas Gregor40f4e692009-01-20 16:54:50 +0000498void DeclContext::makeDeclVisibleInContextImpl(NamedDecl *D) {
Douglas Gregor074149e2009-01-05 19:45:36 +0000499 // Skip unnamed declarations.
500 if (!D->getDeclName())
501 return;
502
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000503 bool MayBeRedeclaration = true;
504
Douglas Gregor44b43212008-12-11 16:49:14 +0000505 if (!isLookupMap()) {
506 unsigned Size = LookupPtr.getInt();
507
508 // The lookup data is stored as an array. Search through the array
509 // to find the insertion location.
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000510 NamedDecl **Array;
Douglas Gregor44b43212008-12-11 16:49:14 +0000511 if (Size == 0) {
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000512 Array = new NamedDecl*[LookupIsMap - 1];
Douglas Gregor44b43212008-12-11 16:49:14 +0000513 LookupPtr.setPointer(Array);
514 } else {
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000515 Array = static_cast<NamedDecl **>(LookupPtr.getPointer());
Douglas Gregor44b43212008-12-11 16:49:14 +0000516 }
517
518 // We always keep declarations of the same name next to each other
519 // in the array, so that it is easy to return multiple results
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000520 // from lookup().
521 unsigned FirstMatch;
522 for (FirstMatch = 0; FirstMatch != Size; ++FirstMatch)
523 if (Array[FirstMatch]->getDeclName() == D->getDeclName())
Douglas Gregore267ff32008-12-11 20:41:00 +0000524 break;
Douglas Gregor44b43212008-12-11 16:49:14 +0000525
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000526 unsigned InsertPos = FirstMatch;
527 if (FirstMatch != Size) {
528 // We found another declaration with the same name. First
529 // determine whether this is a redeclaration of an existing
530 // declaration in this scope, in which case we will replace the
531 // existing declaration.
532 unsigned LastMatch = FirstMatch;
533 for (; LastMatch != Size; ++LastMatch) {
534 if (Array[LastMatch]->getDeclName() != D->getDeclName())
535 break;
536
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000537 if (D->declarationReplaces(Array[LastMatch])) {
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000538 // D is a redeclaration of an existing element in the
539 // array. Replace that element with D.
540 Array[LastMatch] = D;
541 return;
542 }
Douglas Gregor44b43212008-12-11 16:49:14 +0000543 }
544
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000545 // [FirstMatch, LastMatch) contains the set of declarations that
546 // have the same name as this declaration. Determine where the
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000547 // declaration D will be inserted into this range.
548 if (D->getKind() == Decl::UsingDirective ||
549 D->getIdentifierNamespace() == Decl::IDNS_Tag)
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000550 InsertPos = LastMatch;
551 else if (Array[LastMatch-1]->getIdentifierNamespace() == Decl::IDNS_Tag)
552 InsertPos = LastMatch - 1;
553 else
554 InsertPos = LastMatch;
Douglas Gregor44b43212008-12-11 16:49:14 +0000555 }
556
557 if (Size < LookupIsMap - 1) {
558 // The new declaration will fit in the array. Insert the new
559 // declaration at the position Match in the array.
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000560 for (unsigned Idx = Size; Idx > InsertPos; --Idx)
Douglas Gregor44b43212008-12-11 16:49:14 +0000561 Array[Idx] = Array[Idx-1];
562
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000563 Array[InsertPos] = D;
Douglas Gregor44b43212008-12-11 16:49:14 +0000564 LookupPtr.setInt(Size + 1);
565 return;
566 }
567
568 // We've reached capacity in this array. Create a map and copy in
569 // all of the declarations that were stored in the array.
570 StoredDeclsMap *Map = new StoredDeclsMap(16);
571 LookupPtr.setPointer(Map);
572 LookupPtr.setInt(LookupIsMap);
Douglas Gregore267ff32008-12-11 20:41:00 +0000573 for (unsigned Idx = 0; Idx != LookupIsMap - 1; ++Idx)
Douglas Gregor40f4e692009-01-20 16:54:50 +0000574 makeDeclVisibleInContextImpl(Array[Idx]);
Douglas Gregor44b43212008-12-11 16:49:14 +0000575 delete [] Array;
576
577 // Fall through to perform insertion into the map.
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000578 MayBeRedeclaration = false;
579 }
Douglas Gregor44b43212008-12-11 16:49:14 +0000580
581 // Insert this declaration into the map.
582 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(LookupPtr.getPointer());
583 StoredDeclsMap::iterator Pos = Map->find(D->getDeclName());
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000584 if (Pos != Map->end()) {
585 if (MayBeRedeclaration) {
586 // Determine if this declaration is actually a redeclaration.
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000587 std::vector<NamedDecl *>::iterator Redecl
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000588 = std::find_if(Pos->second.begin(), Pos->second.end(),
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000589 std::bind1st(std::mem_fun(&NamedDecl::declarationReplaces),
Douglas Gregor6ed40e32008-12-23 21:05:05 +0000590 D));
591 if (Redecl != Pos->second.end()) {
592 *Redecl = D;
593 return;
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000594 }
595 }
Douglas Gregor44b43212008-12-11 16:49:14 +0000596
Douglas Gregor44b43212008-12-11 16:49:14 +0000597 // Put this declaration into the appropriate slot.
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000598 if (D->getKind() == Decl::UsingDirective ||
599 D->getIdentifierNamespace() == Decl::IDNS_Tag
600 || Pos->second.empty())
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000601 Pos->second.push_back(D);
602 else if (Pos->second.back()->getIdentifierNamespace() == Decl::IDNS_Tag) {
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000603 NamedDecl *TagD = Pos->second.back();
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000604 Pos->second.back() = D;
605 Pos->second.push_back(TagD);
606 } else
607 Pos->second.push_back(D);
Douglas Gregor44b43212008-12-11 16:49:14 +0000608 } else {
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000609 (*Map)[D->getDeclName()].push_back(D);
Douglas Gregor44b43212008-12-11 16:49:14 +0000610 }
611}
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000612
613/// Returns iterator range [First, Last) of UsingDirectiveDecls stored within
614/// this context.
615DeclContext::udir_iterator_range DeclContext::getUsingDirectives() const {
616 lookup_const_result Result = lookup(UsingDirectiveDecl::getName());
617 return udir_iterator_range(reinterpret_cast<udir_iterator>(Result.first),
618 reinterpret_cast<udir_iterator>(Result.second));
619}
620