blob: 3e968c7896262711edf4008b8f405ff803071bae [file] [log] [blame]
Eli Friedmana8a09ac2008-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 Gregor469fc9a2009-02-02 23:39:07 +000015#include "clang/AST/Decl.h"
Argiris Kirtzidis6df1fc02008-06-09 21:05:31 +000016#include "clang/AST/DeclCXX.h"
Douglas Gregor279272e2009-02-04 19:02:06 +000017#include "clang/AST/DeclObjC.h"
18#include "clang/AST/DeclTemplate.h"
Eli Friedmana8a09ac2008-06-07 16:52:53 +000019#include "clang/AST/ASTContext.h"
Douglas Gregor8acb7272008-12-11 16:49:14 +000020#include "clang/AST/Type.h"
Eli Friedmana8a09ac2008-06-07 16:52:53 +000021#include "llvm/ADT/DenseMap.h"
Douglas Gregor6e71edc2008-12-23 21:05:05 +000022#include <algorithm>
Chris Lattner7d6220c2009-03-02 22:20:04 +000023#include <cstdio>
Douglas Gregor6e71edc2008-12-23 21:05:05 +000024#include <functional>
Douglas Gregorddfd9d52008-12-23 00:26:44 +000025#include <vector>
Eli Friedmana8a09ac2008-06-07 16:52:53 +000026using namespace clang;
27
28//===----------------------------------------------------------------------===//
29// Statistics
30//===----------------------------------------------------------------------===//
31
Douglas Gregor469fc9a2009-02-02 23:39:07 +000032#define DECL(Derived, Base) static int n##Derived##s = 0;
33#include "clang/AST/DeclNodes.def"
Eli Friedmana8a09ac2008-06-07 16:52:53 +000034
35static bool StatSwitch = false;
36
37// This keeps track of all decl attributes. Since so few decls have attrs, we
38// keep them in a hash map instead of wasting space in the Decl class.
39typedef llvm::DenseMap<const Decl*, Attr*> DeclAttrMapTy;
40
41static DeclAttrMapTy *DeclAttrs = 0;
42
43const char *Decl::getDeclKindName() const {
44 switch (DeclKind) {
Douglas Gregor469fc9a2009-02-02 23:39:07 +000045 default: assert(0 && "Declaration not in DeclNodes.def!");
46#define DECL(Derived, Base) case Derived: return #Derived;
47#include "clang/AST/DeclNodes.def"
Eli Friedmana8a09ac2008-06-07 16:52:53 +000048 }
49}
50
Steve Naroffe5f128a2009-01-20 19:53:53 +000051const char *DeclContext::getDeclKindName() const {
52 switch (DeclKind) {
Douglas Gregor469fc9a2009-02-02 23:39:07 +000053 default: assert(0 && "Declaration context not in DeclNodes.def!");
Argiris Kirtzidis2d9b7612009-02-16 14:28:33 +000054#define DECL(Derived, Base) case Decl::Derived: return #Derived;
Douglas Gregor469fc9a2009-02-02 23:39:07 +000055#include "clang/AST/DeclNodes.def"
Steve Naroffe5f128a2009-01-20 19:53:53 +000056 }
57}
58
Eli Friedmana8a09ac2008-06-07 16:52:53 +000059bool Decl::CollectingStats(bool Enable) {
60 if (Enable)
61 StatSwitch = true;
62 return StatSwitch;
63}
64
65void Decl::PrintStats() {
66 fprintf(stderr, "*** Decl Stats:\n");
Eli Friedmana8a09ac2008-06-07 16:52:53 +000067
Douglas Gregor469fc9a2009-02-02 23:39:07 +000068 int totalDecls = 0;
69#define DECL(Derived, Base) totalDecls += n##Derived##s;
70#include "clang/AST/DeclNodes.def"
71 fprintf(stderr, " %d decls total.\n", totalDecls);
72
73 int totalBytes = 0;
74#define DECL(Derived, Base) \
75 if (n##Derived##s > 0) { \
76 totalBytes += (int)(n##Derived##s * sizeof(Derived##Decl)); \
77 fprintf(stderr, " %d " #Derived " decls, %d each (%d bytes)\n", \
78 n##Derived##s, (int)sizeof(Derived##Decl), \
79 (int)(n##Derived##s * sizeof(Derived##Decl))); \
80 }
81#include "clang/AST/DeclNodes.def"
Eli Friedmana8a09ac2008-06-07 16:52:53 +000082
Douglas Gregor469fc9a2009-02-02 23:39:07 +000083 fprintf(stderr, "Total bytes = %d\n", totalBytes);
Eli Friedmana8a09ac2008-06-07 16:52:53 +000084}
85
86void Decl::addDeclKind(Kind k) {
87 switch (k) {
Douglas Gregor469fc9a2009-02-02 23:39:07 +000088 default: assert(0 && "Declaration not in DeclNodes.def!");
89#define DECL(Derived, Base) case Derived: ++n##Derived##s; break;
90#include "clang/AST/DeclNodes.def"
Eli Friedmana8a09ac2008-06-07 16:52:53 +000091 }
92}
93
94//===----------------------------------------------------------------------===//
95// Decl Implementation
96//===----------------------------------------------------------------------===//
97
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +000098void Decl::setDeclContext(DeclContext *DC) {
99 if (isOutOfSemaDC())
100 delete getMultipleDC();
101
102 DeclCtx = reinterpret_cast<uintptr_t>(DC);
103}
104
105void Decl::setLexicalDeclContext(DeclContext *DC) {
106 if (DC == getLexicalDeclContext())
107 return;
108
109 if (isInSemaDC()) {
110 MultipleDC *MDC = new MultipleDC();
111 MDC->SemanticDC = getDeclContext();
112 MDC->LexicalDC = DC;
113 DeclCtx = reinterpret_cast<uintptr_t>(MDC) | 0x1;
114 } else {
115 getMultipleDC()->LexicalDC = DC;
116 }
117}
118
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000119// Out-of-line virtual method providing a home for Decl.
120Decl::~Decl() {
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000121 if (isOutOfSemaDC())
122 delete getMultipleDC();
123
Chris Lattner0fc6d642009-03-04 06:05:19 +0000124 assert(!HasAttrs && "attributes should have been freed by Destroy");
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000125}
126
127void Decl::addAttr(Attr *NewAttr) {
128 if (!DeclAttrs)
129 DeclAttrs = new DeclAttrMapTy();
130
131 Attr *&ExistingAttr = (*DeclAttrs)[this];
132
133 NewAttr->setNext(ExistingAttr);
134 ExistingAttr = NewAttr;
135
136 HasAttrs = true;
137}
138
139void Decl::invalidateAttrs() {
140 if (!HasAttrs) return;
141
142 HasAttrs = false;
143 (*DeclAttrs)[this] = 0;
144 DeclAttrs->erase(this);
145
146 if (DeclAttrs->empty()) {
147 delete DeclAttrs;
148 DeclAttrs = 0;
149 }
150}
151
152const Attr *Decl::getAttrs() const {
153 if (!HasAttrs)
154 return 0;
155
156 return (*DeclAttrs)[this];
157}
158
159void Decl::swapAttrs(Decl *RHS) {
160 bool HasLHSAttr = this->HasAttrs;
161 bool HasRHSAttr = RHS->HasAttrs;
162
163 // Usually, neither decl has attrs, nothing to do.
164 if (!HasLHSAttr && !HasRHSAttr) return;
165
166 // If 'this' has no attrs, swap the other way.
167 if (!HasLHSAttr)
168 return RHS->swapAttrs(this);
169
170 // Handle the case when both decls have attrs.
171 if (HasRHSAttr) {
172 std::swap((*DeclAttrs)[this], (*DeclAttrs)[RHS]);
173 return;
174 }
175
176 // Otherwise, LHS has an attr and RHS doesn't.
177 (*DeclAttrs)[RHS] = (*DeclAttrs)[this];
178 (*DeclAttrs).erase(this);
179 this->HasAttrs = false;
180 RHS->HasAttrs = true;
181}
182
183
Chris Lattner0fc6d642009-03-04 06:05:19 +0000184void Decl::Destroy(ASTContext &C) {
185 // Free attributes for this decl.
186 if (HasAttrs) {
187 DeclAttrMapTy::iterator it = DeclAttrs->find(this);
188 assert(it != DeclAttrs->end() && "No attrs found but HasAttrs is true!");
189
190 // release attributes.
191 it->second->Destroy(C);
192 invalidateAttrs();
193 HasAttrs = false;
194 }
195
Douglas Gregor8314d742009-01-13 19:47:12 +0000196#if 0
Douglas Gregorf4006be2009-01-20 04:25:11 +0000197 // FIXME: Once ownership is fully understood, we can enable this code
198 if (DeclContext *DC = dyn_cast<DeclContext>(this))
199 DC->decls_begin()->Destroy(C);
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000200
Douglas Gregorf4006be2009-01-20 04:25:11 +0000201 // Observe the unrolled recursion. By setting N->NextDeclInScope = 0x0
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000202 // within the loop, only the Destroy method for the first Decl
203 // will deallocate all of the Decls in a chain.
204
Douglas Gregorf4006be2009-01-20 04:25:11 +0000205 Decl* N = NextDeclInScope;
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000206
207 while (N) {
Douglas Gregorf4006be2009-01-20 04:25:11 +0000208 Decl* Tmp = N->NextDeclInScope;
209 N->NextDeclInScope = 0;
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000210 N->Destroy(C);
211 N = Tmp;
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000212 }
Douglas Gregor8314d742009-01-13 19:47:12 +0000213
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000214 this->~Decl();
Steve Naroff5abb0282009-01-27 21:25:57 +0000215 C.Deallocate((void *)this);
Douglas Gregorf4006be2009-01-20 04:25:11 +0000216#endif
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000217}
218
Argiris Kirtzidis4b662ea2008-10-12 16:14:48 +0000219Decl *Decl::castFromDeclContext (const DeclContext *D) {
Argiris Kirtzidis981aa132009-02-16 14:29:28 +0000220 Decl::Kind DK = D->getDeclKind();
221 switch(DK) {
222#define DECL_CONTEXT(Name) \
223 case Decl::Name: \
224 return static_cast<Name##Decl*>(const_cast<DeclContext*>(D));
225#define DECL_CONTEXT_BASE(Name)
226#include "clang/AST/DeclNodes.def"
227 default:
228#define DECL_CONTEXT_BASE(Name) \
229 if (DK >= Decl::Name##First && DK <= Decl::Name##Last) \
230 return static_cast<Name##Decl*>(const_cast<DeclContext*>(D));
231#include "clang/AST/DeclNodes.def"
232 assert(false && "a decl that inherits DeclContext isn't handled");
233 return 0;
234 }
Argiris Kirtzidis4b662ea2008-10-12 16:14:48 +0000235}
236
237DeclContext *Decl::castToDeclContext(const Decl *D) {
Argiris Kirtzidis981aa132009-02-16 14:29:28 +0000238 Decl::Kind DK = D->getKind();
239 switch(DK) {
240#define DECL_CONTEXT(Name) \
241 case Decl::Name: \
242 return static_cast<Name##Decl*>(const_cast<Decl*>(D));
243#define DECL_CONTEXT_BASE(Name)
244#include "clang/AST/DeclNodes.def"
245 default:
246#define DECL_CONTEXT_BASE(Name) \
247 if (DK >= Decl::Name##First && DK <= Decl::Name##Last) \
248 return static_cast<Name##Decl*>(const_cast<Decl*>(D));
249#include "clang/AST/DeclNodes.def"
250 assert(false && "a decl that inherits DeclContext isn't handled");
251 return 0;
252 }
Argiris Kirtzidis4b662ea2008-10-12 16:14:48 +0000253}
254
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000255//===----------------------------------------------------------------------===//
256// DeclContext Implementation
257//===----------------------------------------------------------------------===//
258
Argiris Kirtzidis981aa132009-02-16 14:29:28 +0000259bool DeclContext::classof(const Decl *D) {
260 switch (D->getKind()) {
261#define DECL_CONTEXT(Name) case Decl::Name:
262#define DECL_CONTEXT_BASE(Name)
263#include "clang/AST/DeclNodes.def"
264 return true;
265 default:
266#define DECL_CONTEXT_BASE(Name) \
267 if (D->getKind() >= Decl::Name##First && \
268 D->getKind() <= Decl::Name##Last) \
269 return true;
270#include "clang/AST/DeclNodes.def"
271 return false;
272 }
273}
274
Chris Lattner77820d52009-02-20 01:44:05 +0000275/// StoredDeclsList - This is an array of decls optimized a common case of only
276/// containing one entry.
277struct StoredDeclsList {
278 /// Data - If the integer is 0, then the pointer is a NamedDecl*. If the
279 /// integer is 1, then it is a VectorTy;
280 llvm::PointerIntPair<void*, 1, bool> Data;
281
282 /// VectorTy - When in vector form, this is what the Data pointer points to.
283 typedef llvm::SmallVector<NamedDecl*, 4> VectorTy;
284public:
285 StoredDeclsList() {}
286 StoredDeclsList(const StoredDeclsList &RHS) : Data(RHS.Data) {
287 if (isVector())
288 Data.setPointer(new VectorTy(getVector()));
289 }
290
291 ~StoredDeclsList() {
292 // If this is a vector-form, free the vector.
293 if (isVector())
294 delete &getVector();
295 }
296
Chris Lattner51e57e62009-02-23 18:17:44 +0000297 StoredDeclsList &operator=(const StoredDeclsList &RHS) {
298 if (isVector())
299 delete &getVector();
300 Data = RHS.Data;
301 if (isVector())
302 Data.setPointer(new VectorTy(getVector()));
303 return *this;
304 }
305
Chris Lattner77820d52009-02-20 01:44:05 +0000306 bool isVector() const { return Data.getInt() != 0; }
307 bool isInline() const { return Data.getInt() == 0; }
308 bool isNull() const { return Data.getPointer() == 0; }
309
310 void setOnlyValue(NamedDecl *ND) {
311 assert(isInline() && "Not inline");
312 Data.setPointer(ND);
313 }
314
315 /// getLookupResult - Return an array of all the decls that this list
316 /// represents.
317 DeclContext::lookup_result getLookupResult() {
318 // If we have a single inline unit, return it.
319 if (isInline()) {
320 assert(!isNull() && "Empty list isn't allowed");
321
322 // Data is a raw pointer to a NamedDecl*, return it.
323 void *Ptr = &Data;
324 return DeclContext::lookup_result((NamedDecl**)Ptr, (NamedDecl**)Ptr+1);
325 }
326
327 // Otherwise, we have a range result.
328 VectorTy &V = getVector();
329 return DeclContext::lookup_result(&V[0], &V[0]+V.size());
330 }
331
332 /// HandleRedeclaration - If this is a redeclaration of an existing decl,
333 /// replace the old one with D and return true. Otherwise return false.
334 bool HandleRedeclaration(NamedDecl *D) {
335 // Most decls only have one entry in their list, special case it.
336 if (isInline()) {
337 if (!D->declarationReplaces(getInlineValue()))
338 return false;
339 setOnlyValue(D);
340 return true;
341 }
342
343 // Determine if this declaration is actually a redeclaration.
344 VectorTy &Vec = getVector();
345 VectorTy::iterator RDI
346 = std::find_if(Vec.begin(), Vec.end(),
347 std::bind1st(std::mem_fun(&NamedDecl::declarationReplaces),
348 D));
349 if (RDI == Vec.end())
350 return false;
351 *RDI = D;
352 return true;
353 }
354
355 /// AddSubsequentDecl - This is called on the second and later decl when it is
356 /// not a redeclaration to merge it into the appropriate place in our list.
357 ///
358 void AddSubsequentDecl(NamedDecl *D) {
359 // If this is the second decl added to the list, convert this to vector
360 // form.
361 if (isInline()) {
362 NamedDecl *OldD = getInlineValue();
363 Data.setInt(1);
364 VectorTy *VT = new VectorTy();
365 VT->push_back(OldD);
366 Data.setPointer(VT);
367 }
368
369 VectorTy &Vec = getVector();
370 if (isa<UsingDirectiveDecl>(D) ||
371 D->getIdentifierNamespace() == Decl::IDNS_Tag)
372 Vec.push_back(D);
373 else if (Vec.back()->getIdentifierNamespace() == Decl::IDNS_Tag) {
374 NamedDecl *TagD = Vec.back();
375 Vec.back() = D;
376 Vec.push_back(TagD);
377 } else
378 Vec.push_back(D);
379 }
380
381
382private:
383 VectorTy &getVector() const {
384 assert(isVector() && "Not in vector form");
385 return *static_cast<VectorTy*>(Data.getPointer());
386 }
387
388 NamedDecl *getInlineValue() const {
389 assert(isInline() && "Not in inline form");
390 return (NamedDecl*)Data.getPointer();
391 }
392};
393
394
395
396typedef llvm::DenseMap<DeclarationName, StoredDeclsList> StoredDeclsMap;
Douglas Gregor8acb7272008-12-11 16:49:14 +0000397
398DeclContext::~DeclContext() {
399 unsigned Size = LookupPtr.getInt();
Chris Lattner265f01d2009-02-20 00:55:03 +0000400 if (Size == LookupIsMap)
401 delete static_cast<StoredDeclsMap*>(LookupPtr.getPointer());
402 else
403 delete [] static_cast<NamedDecl**>(LookupPtr.getPointer());
Douglas Gregor8acb7272008-12-11 16:49:14 +0000404}
405
406void DeclContext::DestroyDecls(ASTContext &C) {
Douglas Gregorf4006be2009-01-20 04:25:11 +0000407 for (decl_iterator D = decls_begin(); D != decls_end(); )
408 (*D++)->Destroy(C);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000409}
410
Douglas Gregord8028382009-01-05 19:45:36 +0000411bool DeclContext::isTransparentContext() const {
412 if (DeclKind == Decl::Enum)
413 return true; // FIXME: Check for C++0x scoped enums
414 else if (DeclKind == Decl::LinkageSpec)
415 return true;
Douglas Gregor1db12932009-02-26 00:02:51 +0000416 else if (DeclKind >= Decl::RecordFirst && DeclKind <= Decl::RecordLast)
Douglas Gregor723d3332009-01-07 00:43:41 +0000417 return cast<RecordDecl>(this)->isAnonymousStructOrUnion();
Douglas Gregord8028382009-01-05 19:45:36 +0000418 else if (DeclKind == Decl::Namespace)
419 return false; // FIXME: Check for C++0x inline namespaces
420
421 return false;
422}
423
Steve Naroffab63fd62009-01-08 17:28:14 +0000424DeclContext *DeclContext::getPrimaryContext() {
Douglas Gregor8acb7272008-12-11 16:49:14 +0000425 switch (DeclKind) {
Douglas Gregor8acb7272008-12-11 16:49:14 +0000426 case Decl::TranslationUnit:
Douglas Gregord8028382009-01-05 19:45:36 +0000427 case Decl::LinkageSpec:
428 case Decl::Block:
Douglas Gregor8acb7272008-12-11 16:49:14 +0000429 // There is only one DeclContext for these entities.
430 return this;
431
432 case Decl::Namespace:
433 // The original namespace is our primary context.
434 return static_cast<NamespaceDecl*>(this)->getOriginalNamespace();
435
Douglas Gregor8acb7272008-12-11 16:49:14 +0000436 case Decl::ObjCMethod:
437 return this;
438
439 case Decl::ObjCInterface:
Steve Naroffab63fd62009-01-08 17:28:14 +0000440 case Decl::ObjCProtocol:
441 case Decl::ObjCCategory:
Douglas Gregor8acb7272008-12-11 16:49:14 +0000442 // FIXME: Can Objective-C interfaces be forward-declared?
443 return this;
444
Steve Naroffab63fd62009-01-08 17:28:14 +0000445 case Decl::ObjCImplementation:
446 case Decl::ObjCCategoryImpl:
447 return this;
448
Douglas Gregor8acb7272008-12-11 16:49:14 +0000449 default:
Douglas Gregora08b6c72009-02-17 23:15:12 +0000450 if (DeclKind >= Decl::TagFirst && DeclKind <= Decl::TagLast) {
451 // If this is a tag type that has a definition or is currently
452 // being defined, that definition is our primary context.
Douglas Gregor9c7825b2009-02-26 22:19:44 +0000453 if (const TagType *TagT = cast<TagDecl>(this)->TypeForDecl->getAsTagType())
Douglas Gregora08b6c72009-02-17 23:15:12 +0000454 if (TagT->isBeingDefined() ||
455 (TagT->getDecl() && TagT->getDecl()->isDefinition()))
456 return TagT->getDecl();
457 return this;
458 }
459
Douglas Gregor8acb7272008-12-11 16:49:14 +0000460 assert(DeclKind >= Decl::FunctionFirst && DeclKind <= Decl::FunctionLast &&
461 "Unknown DeclContext kind");
462 return this;
463 }
464}
465
466DeclContext *DeclContext::getNextContext() {
467 switch (DeclKind) {
Douglas Gregor8acb7272008-12-11 16:49:14 +0000468 case Decl::Namespace:
469 // Return the next namespace
470 return static_cast<NamespaceDecl*>(this)->getNextNamespace();
471
472 default:
Douglas Gregor8acb7272008-12-11 16:49:14 +0000473 return 0;
474 }
475}
476
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000477void DeclContext::addDecl(Decl *D) {
Chris Lattner53602072009-02-20 00:56:18 +0000478 assert(D->getLexicalDeclContext() == this &&
479 "Decl inserted into wrong lexical context");
Douglas Gregord1675382009-01-09 19:42:16 +0000480 assert(!D->NextDeclInScope && D != LastDecl &&
481 "Decl already inserted into a DeclContext");
482
483 if (FirstDecl) {
484 LastDecl->NextDeclInScope = D;
485 LastDecl = D;
486 } else {
487 FirstDecl = LastDecl = D;
488 }
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000489
490 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
Douglas Gregor9ac66d22009-01-20 16:54:50 +0000491 ND->getDeclContext()->makeDeclVisibleInContext(ND);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000492}
493
Douglas Gregord8028382009-01-05 19:45:36 +0000494/// buildLookup - Build the lookup data structure with all of the
495/// declarations in DCtx (and any other contexts linked to it or
496/// transparent contexts nested within it).
Steve Naroffab63fd62009-01-08 17:28:14 +0000497void DeclContext::buildLookup(DeclContext *DCtx) {
Douglas Gregord8028382009-01-05 19:45:36 +0000498 for (; DCtx; DCtx = DCtx->getNextContext()) {
Douglas Gregorf0464ec2009-01-06 07:17:58 +0000499 for (decl_iterator D = DCtx->decls_begin(), DEnd = DCtx->decls_end();
500 D != DEnd; ++D) {
Douglas Gregord8028382009-01-05 19:45:36 +0000501 // Insert this declaration into the lookup structure
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000502 if (NamedDecl *ND = dyn_cast<NamedDecl>(*D))
Douglas Gregor9ac66d22009-01-20 16:54:50 +0000503 makeDeclVisibleInContextImpl(ND);
Douglas Gregord8028382009-01-05 19:45:36 +0000504
505 // If this declaration is itself a transparent declaration context,
506 // add its members (recursively).
507 if (DeclContext *InnerCtx = dyn_cast<DeclContext>(*D))
508 if (InnerCtx->isTransparentContext())
Steve Naroffab63fd62009-01-08 17:28:14 +0000509 buildLookup(InnerCtx->getPrimaryContext());
Douglas Gregord8028382009-01-05 19:45:36 +0000510 }
511 }
512}
513
Douglas Gregor8acb7272008-12-11 16:49:14 +0000514DeclContext::lookup_result
Steve Naroffab63fd62009-01-08 17:28:14 +0000515DeclContext::lookup(DeclarationName Name) {
516 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor8acb7272008-12-11 16:49:14 +0000517 if (PrimaryContext != this)
Steve Naroffab63fd62009-01-08 17:28:14 +0000518 return PrimaryContext->lookup(Name);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000519
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000520 /// If there is no lookup data structure, build one now by walking
Douglas Gregor8acb7272008-12-11 16:49:14 +0000521 /// all of the linked DeclContexts (in declaration order!) and
522 /// inserting their values.
Douglas Gregord8028382009-01-05 19:45:36 +0000523 if (LookupPtr.getPointer() == 0)
Steve Naroffab63fd62009-01-08 17:28:14 +0000524 buildLookup(this);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000525
Douglas Gregor8acb7272008-12-11 16:49:14 +0000526 if (isLookupMap()) {
527 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(LookupPtr.getPointer());
528 StoredDeclsMap::iterator Pos = Map->find(Name);
Chris Lattner265f01d2009-02-20 00:55:03 +0000529 if (Pos == Map->end())
530 return lookup_result(0, 0);
Chris Lattner77820d52009-02-20 01:44:05 +0000531 return Pos->second.getLookupResult();
Douglas Gregor8acb7272008-12-11 16:49:14 +0000532 }
533
534 // We have a small array. Look into it.
535 unsigned Size = LookupPtr.getInt();
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000536 NamedDecl **Array = static_cast<NamedDecl**>(LookupPtr.getPointer());
Douglas Gregor39677622008-12-11 20:41:00 +0000537 for (unsigned Idx = 0; Idx != Size; ++Idx)
Douglas Gregor8acb7272008-12-11 16:49:14 +0000538 if (Array[Idx]->getDeclName() == Name) {
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000539 unsigned Last = Idx + 1;
540 while (Last != Size && Array[Last]->getDeclName() == Name)
541 ++Last;
542 return lookup_result(&Array[Idx], &Array[Last]);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000543 }
544
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000545 return lookup_result(0, 0);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000546}
547
548DeclContext::lookup_const_result
Steve Naroffab63fd62009-01-08 17:28:14 +0000549DeclContext::lookup(DeclarationName Name) const {
550 return const_cast<DeclContext*>(this)->lookup(Name);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000551}
552
Douglas Gregor38d38a02009-01-07 02:48:43 +0000553const DeclContext *DeclContext::getLookupContext() const {
554 const DeclContext *Ctx = this;
Douglas Gregordb568cf2009-01-08 20:45:30 +0000555 // Skip through transparent contexts.
Douglas Gregor69e781f2009-01-06 23:51:29 +0000556 while (Ctx->isTransparentContext())
557 Ctx = Ctx->getParent();
558 return Ctx;
559}
560
Douglas Gregor0d93f692009-02-25 22:02:03 +0000561DeclContext *DeclContext::getEnclosingNamespaceContext() {
562 DeclContext *Ctx = this;
563 // Skip through non-namespace, non-translation-unit contexts.
564 while (!Ctx->isFileContext() || Ctx->isTransparentContext())
565 Ctx = Ctx->getParent();
566 return Ctx->getPrimaryContext();
567}
568
Douglas Gregor9ac66d22009-01-20 16:54:50 +0000569void DeclContext::makeDeclVisibleInContext(NamedDecl *D) {
Douglas Gregora08b6c72009-02-17 23:15:12 +0000570 // FIXME: This feels like a hack. Should DeclarationName support
571 // template-ids, or is there a better way to keep specializations
572 // from being visible?
573 if (isa<ClassTemplateSpecializationDecl>(D))
574 return;
575
Steve Naroffab63fd62009-01-08 17:28:14 +0000576 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor8acb7272008-12-11 16:49:14 +0000577 if (PrimaryContext != this) {
Douglas Gregor9ac66d22009-01-20 16:54:50 +0000578 PrimaryContext->makeDeclVisibleInContext(D);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000579 return;
580 }
581
582 // If we already have a lookup data structure, perform the insertion
583 // into it. Otherwise, be lazy and don't build that structure until
584 // someone asks for it.
585 if (LookupPtr.getPointer())
Douglas Gregor9ac66d22009-01-20 16:54:50 +0000586 makeDeclVisibleInContextImpl(D);
Douglas Gregord8028382009-01-05 19:45:36 +0000587
Douglas Gregord8028382009-01-05 19:45:36 +0000588 // If we are a transparent context, insert into our parent context,
589 // too. This operation is recursive.
590 if (isTransparentContext())
Douglas Gregor9ac66d22009-01-20 16:54:50 +0000591 getParent()->makeDeclVisibleInContext(D);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000592}
593
Douglas Gregor9ac66d22009-01-20 16:54:50 +0000594void DeclContext::makeDeclVisibleInContextImpl(NamedDecl *D) {
Douglas Gregord8028382009-01-05 19:45:36 +0000595 // Skip unnamed declarations.
596 if (!D->getDeclName())
597 return;
598
Douglas Gregora08b6c72009-02-17 23:15:12 +0000599 // FIXME: This feels like a hack. Should DeclarationName support
600 // template-ids, or is there a better way to keep specializations
601 // from being visible?
602 if (isa<ClassTemplateSpecializationDecl>(D))
603 return;
604
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000605 bool MayBeRedeclaration = true;
606
Douglas Gregor8acb7272008-12-11 16:49:14 +0000607 if (!isLookupMap()) {
608 unsigned Size = LookupPtr.getInt();
609
610 // The lookup data is stored as an array. Search through the array
611 // to find the insertion location.
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000612 NamedDecl **Array;
Douglas Gregor8acb7272008-12-11 16:49:14 +0000613 if (Size == 0) {
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000614 Array = new NamedDecl*[LookupIsMap - 1];
Douglas Gregor8acb7272008-12-11 16:49:14 +0000615 LookupPtr.setPointer(Array);
616 } else {
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000617 Array = static_cast<NamedDecl **>(LookupPtr.getPointer());
Douglas Gregor8acb7272008-12-11 16:49:14 +0000618 }
619
620 // We always keep declarations of the same name next to each other
621 // in the array, so that it is easy to return multiple results
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000622 // from lookup().
623 unsigned FirstMatch;
624 for (FirstMatch = 0; FirstMatch != Size; ++FirstMatch)
625 if (Array[FirstMatch]->getDeclName() == D->getDeclName())
Douglas Gregor39677622008-12-11 20:41:00 +0000626 break;
Douglas Gregor8acb7272008-12-11 16:49:14 +0000627
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000628 unsigned InsertPos = FirstMatch;
629 if (FirstMatch != Size) {
630 // We found another declaration with the same name. First
631 // determine whether this is a redeclaration of an existing
632 // declaration in this scope, in which case we will replace the
633 // existing declaration.
634 unsigned LastMatch = FirstMatch;
635 for (; LastMatch != Size; ++LastMatch) {
636 if (Array[LastMatch]->getDeclName() != D->getDeclName())
637 break;
638
Douglas Gregor6e71edc2008-12-23 21:05:05 +0000639 if (D->declarationReplaces(Array[LastMatch])) {
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000640 // D is a redeclaration of an existing element in the
641 // array. Replace that element with D.
642 Array[LastMatch] = D;
643 return;
644 }
Douglas Gregor8acb7272008-12-11 16:49:14 +0000645 }
646
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000647 // [FirstMatch, LastMatch) contains the set of declarations that
648 // have the same name as this declaration. Determine where the
Douglas Gregor7a7be652009-02-03 19:21:40 +0000649 // declaration D will be inserted into this range.
650 if (D->getKind() == Decl::UsingDirective ||
651 D->getIdentifierNamespace() == Decl::IDNS_Tag)
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000652 InsertPos = LastMatch;
653 else if (Array[LastMatch-1]->getIdentifierNamespace() == Decl::IDNS_Tag)
654 InsertPos = LastMatch - 1;
655 else
656 InsertPos = LastMatch;
Douglas Gregor8acb7272008-12-11 16:49:14 +0000657 }
658
659 if (Size < LookupIsMap - 1) {
660 // The new declaration will fit in the array. Insert the new
661 // declaration at the position Match in the array.
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000662 for (unsigned Idx = Size; Idx > InsertPos; --Idx)
Douglas Gregor8acb7272008-12-11 16:49:14 +0000663 Array[Idx] = Array[Idx-1];
664
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000665 Array[InsertPos] = D;
Douglas Gregor8acb7272008-12-11 16:49:14 +0000666 LookupPtr.setInt(Size + 1);
667 return;
668 }
669
670 // We've reached capacity in this array. Create a map and copy in
671 // all of the declarations that were stored in the array.
672 StoredDeclsMap *Map = new StoredDeclsMap(16);
673 LookupPtr.setPointer(Map);
674 LookupPtr.setInt(LookupIsMap);
Douglas Gregor39677622008-12-11 20:41:00 +0000675 for (unsigned Idx = 0; Idx != LookupIsMap - 1; ++Idx)
Douglas Gregor9ac66d22009-01-20 16:54:50 +0000676 makeDeclVisibleInContextImpl(Array[Idx]);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000677 delete [] Array;
678
679 // Fall through to perform insertion into the map.
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000680 MayBeRedeclaration = false;
681 }
Douglas Gregor8acb7272008-12-11 16:49:14 +0000682
683 // Insert this declaration into the map.
Chris Lattner77820d52009-02-20 01:44:05 +0000684 StoredDeclsMap &Map = *static_cast<StoredDeclsMap*>(LookupPtr.getPointer());
685 StoredDeclsList &DeclNameEntries = Map[D->getDeclName()];
686 if (DeclNameEntries.isNull()) {
687 DeclNameEntries.setOnlyValue(D);
Chris Lattner6719b1f2009-02-19 07:00:44 +0000688 return;
Douglas Gregor8acb7272008-12-11 16:49:14 +0000689 }
Chris Lattner265f01d2009-02-20 00:55:03 +0000690
Chris Lattnerba589d42009-02-20 01:10:07 +0000691 // If it is possible that this is a redeclaration, check to see if there is
692 // already a decl for which declarationReplaces returns true. If there is
693 // one, just replace it and return.
Chris Lattner77820d52009-02-20 01:44:05 +0000694 if (MayBeRedeclaration && DeclNameEntries.HandleRedeclaration(D))
695 return;
Chris Lattner265f01d2009-02-20 00:55:03 +0000696
Chris Lattner6719b1f2009-02-19 07:00:44 +0000697 // Put this declaration into the appropriate slot.
Chris Lattner77820d52009-02-20 01:44:05 +0000698 DeclNameEntries.AddSubsequentDecl(D);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000699}
Douglas Gregor7a7be652009-02-03 19:21:40 +0000700
701/// Returns iterator range [First, Last) of UsingDirectiveDecls stored within
702/// this context.
703DeclContext::udir_iterator_range DeclContext::getUsingDirectives() const {
704 lookup_const_result Result = lookup(UsingDirectiveDecl::getName());
705 return udir_iterator_range(reinterpret_cast<udir_iterator>(Result.first),
706 reinterpret_cast<udir_iterator>(Result.second));
707}
708