blob: ca67e28c717387599e77329dbd16af7adbd9eb10 [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
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000124 if (!HasAttrs)
125 return;
126
127 DeclAttrMapTy::iterator it = DeclAttrs->find(this);
128 assert(it != DeclAttrs->end() && "No attrs found but HasAttrs is true!");
129
130 // release attributes.
131 delete it->second;
132 invalidateAttrs();
133}
134
135void Decl::addAttr(Attr *NewAttr) {
136 if (!DeclAttrs)
137 DeclAttrs = new DeclAttrMapTy();
138
139 Attr *&ExistingAttr = (*DeclAttrs)[this];
140
141 NewAttr->setNext(ExistingAttr);
142 ExistingAttr = NewAttr;
143
144 HasAttrs = true;
145}
146
147void Decl::invalidateAttrs() {
148 if (!HasAttrs) return;
149
150 HasAttrs = false;
151 (*DeclAttrs)[this] = 0;
152 DeclAttrs->erase(this);
153
154 if (DeclAttrs->empty()) {
155 delete DeclAttrs;
156 DeclAttrs = 0;
157 }
158}
159
160const Attr *Decl::getAttrs() const {
161 if (!HasAttrs)
162 return 0;
163
164 return (*DeclAttrs)[this];
165}
166
167void Decl::swapAttrs(Decl *RHS) {
168 bool HasLHSAttr = this->HasAttrs;
169 bool HasRHSAttr = RHS->HasAttrs;
170
171 // Usually, neither decl has attrs, nothing to do.
172 if (!HasLHSAttr && !HasRHSAttr) return;
173
174 // If 'this' has no attrs, swap the other way.
175 if (!HasLHSAttr)
176 return RHS->swapAttrs(this);
177
178 // Handle the case when both decls have attrs.
179 if (HasRHSAttr) {
180 std::swap((*DeclAttrs)[this], (*DeclAttrs)[RHS]);
181 return;
182 }
183
184 // Otherwise, LHS has an attr and RHS doesn't.
185 (*DeclAttrs)[RHS] = (*DeclAttrs)[this];
186 (*DeclAttrs).erase(this);
187 this->HasAttrs = false;
188 RHS->HasAttrs = true;
189}
190
191
192void Decl::Destroy(ASTContext& C) {
Douglas Gregor8314d742009-01-13 19:47:12 +0000193#if 0
Douglas Gregorf4006be2009-01-20 04:25:11 +0000194 // FIXME: Once ownership is fully understood, we can enable this code
195 if (DeclContext *DC = dyn_cast<DeclContext>(this))
196 DC->decls_begin()->Destroy(C);
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000197
Douglas Gregorf4006be2009-01-20 04:25:11 +0000198 // Observe the unrolled recursion. By setting N->NextDeclInScope = 0x0
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000199 // within the loop, only the Destroy method for the first Decl
200 // will deallocate all of the Decls in a chain.
201
Douglas Gregorf4006be2009-01-20 04:25:11 +0000202 Decl* N = NextDeclInScope;
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000203
204 while (N) {
Douglas Gregorf4006be2009-01-20 04:25:11 +0000205 Decl* Tmp = N->NextDeclInScope;
206 N->NextDeclInScope = 0;
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000207 N->Destroy(C);
208 N = Tmp;
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000209 }
Douglas Gregor8314d742009-01-13 19:47:12 +0000210
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000211 this->~Decl();
Steve Naroff5abb0282009-01-27 21:25:57 +0000212 C.Deallocate((void *)this);
Douglas Gregorf4006be2009-01-20 04:25:11 +0000213#endif
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000214}
215
Argiris Kirtzidis4b662ea2008-10-12 16:14:48 +0000216Decl *Decl::castFromDeclContext (const DeclContext *D) {
Argiris Kirtzidis981aa132009-02-16 14:29:28 +0000217 Decl::Kind DK = D->getDeclKind();
218 switch(DK) {
219#define DECL_CONTEXT(Name) \
220 case Decl::Name: \
221 return static_cast<Name##Decl*>(const_cast<DeclContext*>(D));
222#define DECL_CONTEXT_BASE(Name)
223#include "clang/AST/DeclNodes.def"
224 default:
225#define DECL_CONTEXT_BASE(Name) \
226 if (DK >= Decl::Name##First && DK <= Decl::Name##Last) \
227 return static_cast<Name##Decl*>(const_cast<DeclContext*>(D));
228#include "clang/AST/DeclNodes.def"
229 assert(false && "a decl that inherits DeclContext isn't handled");
230 return 0;
231 }
Argiris Kirtzidis4b662ea2008-10-12 16:14:48 +0000232}
233
234DeclContext *Decl::castToDeclContext(const Decl *D) {
Argiris Kirtzidis981aa132009-02-16 14:29:28 +0000235 Decl::Kind DK = D->getKind();
236 switch(DK) {
237#define DECL_CONTEXT(Name) \
238 case Decl::Name: \
239 return static_cast<Name##Decl*>(const_cast<Decl*>(D));
240#define DECL_CONTEXT_BASE(Name)
241#include "clang/AST/DeclNodes.def"
242 default:
243#define DECL_CONTEXT_BASE(Name) \
244 if (DK >= Decl::Name##First && DK <= Decl::Name##Last) \
245 return static_cast<Name##Decl*>(const_cast<Decl*>(D));
246#include "clang/AST/DeclNodes.def"
247 assert(false && "a decl that inherits DeclContext isn't handled");
248 return 0;
249 }
Argiris Kirtzidis4b662ea2008-10-12 16:14:48 +0000250}
251
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000252//===----------------------------------------------------------------------===//
253// DeclContext Implementation
254//===----------------------------------------------------------------------===//
255
Argiris Kirtzidis981aa132009-02-16 14:29:28 +0000256bool DeclContext::classof(const Decl *D) {
257 switch (D->getKind()) {
258#define DECL_CONTEXT(Name) case Decl::Name:
259#define DECL_CONTEXT_BASE(Name)
260#include "clang/AST/DeclNodes.def"
261 return true;
262 default:
263#define DECL_CONTEXT_BASE(Name) \
264 if (D->getKind() >= Decl::Name##First && \
265 D->getKind() <= Decl::Name##Last) \
266 return true;
267#include "clang/AST/DeclNodes.def"
268 return false;
269 }
270}
271
Chris Lattner77820d52009-02-20 01:44:05 +0000272/// StoredDeclsList - This is an array of decls optimized a common case of only
273/// containing one entry.
274struct StoredDeclsList {
275 /// Data - If the integer is 0, then the pointer is a NamedDecl*. If the
276 /// integer is 1, then it is a VectorTy;
277 llvm::PointerIntPair<void*, 1, bool> Data;
278
279 /// VectorTy - When in vector form, this is what the Data pointer points to.
280 typedef llvm::SmallVector<NamedDecl*, 4> VectorTy;
281public:
282 StoredDeclsList() {}
283 StoredDeclsList(const StoredDeclsList &RHS) : Data(RHS.Data) {
284 if (isVector())
285 Data.setPointer(new VectorTy(getVector()));
286 }
287
288 ~StoredDeclsList() {
289 // If this is a vector-form, free the vector.
290 if (isVector())
291 delete &getVector();
292 }
293
Chris Lattner51e57e62009-02-23 18:17:44 +0000294 StoredDeclsList &operator=(const StoredDeclsList &RHS) {
295 if (isVector())
296 delete &getVector();
297 Data = RHS.Data;
298 if (isVector())
299 Data.setPointer(new VectorTy(getVector()));
300 return *this;
301 }
302
Chris Lattner77820d52009-02-20 01:44:05 +0000303 bool isVector() const { return Data.getInt() != 0; }
304 bool isInline() const { return Data.getInt() == 0; }
305 bool isNull() const { return Data.getPointer() == 0; }
306
307 void setOnlyValue(NamedDecl *ND) {
308 assert(isInline() && "Not inline");
309 Data.setPointer(ND);
310 }
311
312 /// getLookupResult - Return an array of all the decls that this list
313 /// represents.
314 DeclContext::lookup_result getLookupResult() {
315 // If we have a single inline unit, return it.
316 if (isInline()) {
317 assert(!isNull() && "Empty list isn't allowed");
318
319 // Data is a raw pointer to a NamedDecl*, return it.
320 void *Ptr = &Data;
321 return DeclContext::lookup_result((NamedDecl**)Ptr, (NamedDecl**)Ptr+1);
322 }
323
324 // Otherwise, we have a range result.
325 VectorTy &V = getVector();
326 return DeclContext::lookup_result(&V[0], &V[0]+V.size());
327 }
328
329 /// HandleRedeclaration - If this is a redeclaration of an existing decl,
330 /// replace the old one with D and return true. Otherwise return false.
331 bool HandleRedeclaration(NamedDecl *D) {
332 // Most decls only have one entry in their list, special case it.
333 if (isInline()) {
334 if (!D->declarationReplaces(getInlineValue()))
335 return false;
336 setOnlyValue(D);
337 return true;
338 }
339
340 // Determine if this declaration is actually a redeclaration.
341 VectorTy &Vec = getVector();
342 VectorTy::iterator RDI
343 = std::find_if(Vec.begin(), Vec.end(),
344 std::bind1st(std::mem_fun(&NamedDecl::declarationReplaces),
345 D));
346 if (RDI == Vec.end())
347 return false;
348 *RDI = D;
349 return true;
350 }
351
352 /// AddSubsequentDecl - This is called on the second and later decl when it is
353 /// not a redeclaration to merge it into the appropriate place in our list.
354 ///
355 void AddSubsequentDecl(NamedDecl *D) {
356 // If this is the second decl added to the list, convert this to vector
357 // form.
358 if (isInline()) {
359 NamedDecl *OldD = getInlineValue();
360 Data.setInt(1);
361 VectorTy *VT = new VectorTy();
362 VT->push_back(OldD);
363 Data.setPointer(VT);
364 }
365
366 VectorTy &Vec = getVector();
367 if (isa<UsingDirectiveDecl>(D) ||
368 D->getIdentifierNamespace() == Decl::IDNS_Tag)
369 Vec.push_back(D);
370 else if (Vec.back()->getIdentifierNamespace() == Decl::IDNS_Tag) {
371 NamedDecl *TagD = Vec.back();
372 Vec.back() = D;
373 Vec.push_back(TagD);
374 } else
375 Vec.push_back(D);
376 }
377
378
379private:
380 VectorTy &getVector() const {
381 assert(isVector() && "Not in vector form");
382 return *static_cast<VectorTy*>(Data.getPointer());
383 }
384
385 NamedDecl *getInlineValue() const {
386 assert(isInline() && "Not in inline form");
387 return (NamedDecl*)Data.getPointer();
388 }
389};
390
391
392
393typedef llvm::DenseMap<DeclarationName, StoredDeclsList> StoredDeclsMap;
Douglas Gregor8acb7272008-12-11 16:49:14 +0000394
395DeclContext::~DeclContext() {
396 unsigned Size = LookupPtr.getInt();
Chris Lattner265f01d2009-02-20 00:55:03 +0000397 if (Size == LookupIsMap)
398 delete static_cast<StoredDeclsMap*>(LookupPtr.getPointer());
399 else
400 delete [] static_cast<NamedDecl**>(LookupPtr.getPointer());
Douglas Gregor8acb7272008-12-11 16:49:14 +0000401}
402
403void DeclContext::DestroyDecls(ASTContext &C) {
Douglas Gregorf4006be2009-01-20 04:25:11 +0000404 for (decl_iterator D = decls_begin(); D != decls_end(); )
405 (*D++)->Destroy(C);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000406}
407
Douglas Gregord8028382009-01-05 19:45:36 +0000408bool DeclContext::isTransparentContext() const {
409 if (DeclKind == Decl::Enum)
410 return true; // FIXME: Check for C++0x scoped enums
411 else if (DeclKind == Decl::LinkageSpec)
412 return true;
Douglas Gregor1db12932009-02-26 00:02:51 +0000413 else if (DeclKind >= Decl::RecordFirst && DeclKind <= Decl::RecordLast)
Douglas Gregor723d3332009-01-07 00:43:41 +0000414 return cast<RecordDecl>(this)->isAnonymousStructOrUnion();
Douglas Gregord8028382009-01-05 19:45:36 +0000415 else if (DeclKind == Decl::Namespace)
416 return false; // FIXME: Check for C++0x inline namespaces
417
418 return false;
419}
420
Steve Naroffab63fd62009-01-08 17:28:14 +0000421DeclContext *DeclContext::getPrimaryContext() {
Douglas Gregor8acb7272008-12-11 16:49:14 +0000422 switch (DeclKind) {
Douglas Gregor8acb7272008-12-11 16:49:14 +0000423 case Decl::TranslationUnit:
Douglas Gregord8028382009-01-05 19:45:36 +0000424 case Decl::LinkageSpec:
425 case Decl::Block:
Douglas Gregor8acb7272008-12-11 16:49:14 +0000426 // There is only one DeclContext for these entities.
427 return this;
428
429 case Decl::Namespace:
430 // The original namespace is our primary context.
431 return static_cast<NamespaceDecl*>(this)->getOriginalNamespace();
432
Douglas Gregor8acb7272008-12-11 16:49:14 +0000433 case Decl::ObjCMethod:
434 return this;
435
436 case Decl::ObjCInterface:
Steve Naroffab63fd62009-01-08 17:28:14 +0000437 case Decl::ObjCProtocol:
438 case Decl::ObjCCategory:
Douglas Gregor8acb7272008-12-11 16:49:14 +0000439 // FIXME: Can Objective-C interfaces be forward-declared?
440 return this;
441
Steve Naroffab63fd62009-01-08 17:28:14 +0000442 case Decl::ObjCImplementation:
443 case Decl::ObjCCategoryImpl:
444 return this;
445
Douglas Gregor8acb7272008-12-11 16:49:14 +0000446 default:
Douglas Gregora08b6c72009-02-17 23:15:12 +0000447 if (DeclKind >= Decl::TagFirst && DeclKind <= Decl::TagLast) {
448 // If this is a tag type that has a definition or is currently
449 // being defined, that definition is our primary context.
Douglas Gregor9c7825b2009-02-26 22:19:44 +0000450 if (const TagType *TagT = cast<TagDecl>(this)->TypeForDecl->getAsTagType())
Douglas Gregora08b6c72009-02-17 23:15:12 +0000451 if (TagT->isBeingDefined() ||
452 (TagT->getDecl() && TagT->getDecl()->isDefinition()))
453 return TagT->getDecl();
454 return this;
455 }
456
Douglas Gregor8acb7272008-12-11 16:49:14 +0000457 assert(DeclKind >= Decl::FunctionFirst && DeclKind <= Decl::FunctionLast &&
458 "Unknown DeclContext kind");
459 return this;
460 }
461}
462
463DeclContext *DeclContext::getNextContext() {
464 switch (DeclKind) {
Douglas Gregor8acb7272008-12-11 16:49:14 +0000465 case Decl::Namespace:
466 // Return the next namespace
467 return static_cast<NamespaceDecl*>(this)->getNextNamespace();
468
469 default:
Douglas Gregor8acb7272008-12-11 16:49:14 +0000470 return 0;
471 }
472}
473
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000474void DeclContext::addDecl(Decl *D) {
Chris Lattner53602072009-02-20 00:56:18 +0000475 assert(D->getLexicalDeclContext() == this &&
476 "Decl inserted into wrong lexical context");
Douglas Gregord1675382009-01-09 19:42:16 +0000477 assert(!D->NextDeclInScope && D != LastDecl &&
478 "Decl already inserted into a DeclContext");
479
480 if (FirstDecl) {
481 LastDecl->NextDeclInScope = D;
482 LastDecl = D;
483 } else {
484 FirstDecl = LastDecl = D;
485 }
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000486
487 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
Douglas Gregor9ac66d22009-01-20 16:54:50 +0000488 ND->getDeclContext()->makeDeclVisibleInContext(ND);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000489}
490
Douglas Gregord8028382009-01-05 19:45:36 +0000491/// buildLookup - Build the lookup data structure with all of the
492/// declarations in DCtx (and any other contexts linked to it or
493/// transparent contexts nested within it).
Steve Naroffab63fd62009-01-08 17:28:14 +0000494void DeclContext::buildLookup(DeclContext *DCtx) {
Douglas Gregord8028382009-01-05 19:45:36 +0000495 for (; DCtx; DCtx = DCtx->getNextContext()) {
Douglas Gregorf0464ec2009-01-06 07:17:58 +0000496 for (decl_iterator D = DCtx->decls_begin(), DEnd = DCtx->decls_end();
497 D != DEnd; ++D) {
Douglas Gregord8028382009-01-05 19:45:36 +0000498 // Insert this declaration into the lookup structure
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000499 if (NamedDecl *ND = dyn_cast<NamedDecl>(*D))
Douglas Gregor9ac66d22009-01-20 16:54:50 +0000500 makeDeclVisibleInContextImpl(ND);
Douglas Gregord8028382009-01-05 19:45:36 +0000501
502 // If this declaration is itself a transparent declaration context,
503 // add its members (recursively).
504 if (DeclContext *InnerCtx = dyn_cast<DeclContext>(*D))
505 if (InnerCtx->isTransparentContext())
Steve Naroffab63fd62009-01-08 17:28:14 +0000506 buildLookup(InnerCtx->getPrimaryContext());
Douglas Gregord8028382009-01-05 19:45:36 +0000507 }
508 }
509}
510
Douglas Gregor8acb7272008-12-11 16:49:14 +0000511DeclContext::lookup_result
Steve Naroffab63fd62009-01-08 17:28:14 +0000512DeclContext::lookup(DeclarationName Name) {
513 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor8acb7272008-12-11 16:49:14 +0000514 if (PrimaryContext != this)
Steve Naroffab63fd62009-01-08 17:28:14 +0000515 return PrimaryContext->lookup(Name);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000516
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000517 /// If there is no lookup data structure, build one now by walking
Douglas Gregor8acb7272008-12-11 16:49:14 +0000518 /// all of the linked DeclContexts (in declaration order!) and
519 /// inserting their values.
Douglas Gregord8028382009-01-05 19:45:36 +0000520 if (LookupPtr.getPointer() == 0)
Steve Naroffab63fd62009-01-08 17:28:14 +0000521 buildLookup(this);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000522
Douglas Gregor8acb7272008-12-11 16:49:14 +0000523 if (isLookupMap()) {
524 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(LookupPtr.getPointer());
525 StoredDeclsMap::iterator Pos = Map->find(Name);
Chris Lattner265f01d2009-02-20 00:55:03 +0000526 if (Pos == Map->end())
527 return lookup_result(0, 0);
Chris Lattner77820d52009-02-20 01:44:05 +0000528 return Pos->second.getLookupResult();
Douglas Gregor8acb7272008-12-11 16:49:14 +0000529 }
530
531 // We have a small array. Look into it.
532 unsigned Size = LookupPtr.getInt();
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000533 NamedDecl **Array = static_cast<NamedDecl**>(LookupPtr.getPointer());
Douglas Gregor39677622008-12-11 20:41:00 +0000534 for (unsigned Idx = 0; Idx != Size; ++Idx)
Douglas Gregor8acb7272008-12-11 16:49:14 +0000535 if (Array[Idx]->getDeclName() == Name) {
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000536 unsigned Last = Idx + 1;
537 while (Last != Size && Array[Last]->getDeclName() == Name)
538 ++Last;
539 return lookup_result(&Array[Idx], &Array[Last]);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000540 }
541
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000542 return lookup_result(0, 0);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000543}
544
545DeclContext::lookup_const_result
Steve Naroffab63fd62009-01-08 17:28:14 +0000546DeclContext::lookup(DeclarationName Name) const {
547 return const_cast<DeclContext*>(this)->lookup(Name);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000548}
549
Douglas Gregor38d38a02009-01-07 02:48:43 +0000550const DeclContext *DeclContext::getLookupContext() const {
551 const DeclContext *Ctx = this;
Douglas Gregordb568cf2009-01-08 20:45:30 +0000552 // Skip through transparent contexts.
Douglas Gregor69e781f2009-01-06 23:51:29 +0000553 while (Ctx->isTransparentContext())
554 Ctx = Ctx->getParent();
555 return Ctx;
556}
557
Douglas Gregor0d93f692009-02-25 22:02:03 +0000558DeclContext *DeclContext::getEnclosingNamespaceContext() {
559 DeclContext *Ctx = this;
560 // Skip through non-namespace, non-translation-unit contexts.
561 while (!Ctx->isFileContext() || Ctx->isTransparentContext())
562 Ctx = Ctx->getParent();
563 return Ctx->getPrimaryContext();
564}
565
Douglas Gregor9ac66d22009-01-20 16:54:50 +0000566void DeclContext::makeDeclVisibleInContext(NamedDecl *D) {
Douglas Gregora08b6c72009-02-17 23:15:12 +0000567 // FIXME: This feels like a hack. Should DeclarationName support
568 // template-ids, or is there a better way to keep specializations
569 // from being visible?
570 if (isa<ClassTemplateSpecializationDecl>(D))
571 return;
572
Steve Naroffab63fd62009-01-08 17:28:14 +0000573 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor8acb7272008-12-11 16:49:14 +0000574 if (PrimaryContext != this) {
Douglas Gregor9ac66d22009-01-20 16:54:50 +0000575 PrimaryContext->makeDeclVisibleInContext(D);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000576 return;
577 }
578
579 // If we already have a lookup data structure, perform the insertion
580 // into it. Otherwise, be lazy and don't build that structure until
581 // someone asks for it.
582 if (LookupPtr.getPointer())
Douglas Gregor9ac66d22009-01-20 16:54:50 +0000583 makeDeclVisibleInContextImpl(D);
Douglas Gregord8028382009-01-05 19:45:36 +0000584
Douglas Gregord8028382009-01-05 19:45:36 +0000585 // If we are a transparent context, insert into our parent context,
586 // too. This operation is recursive.
587 if (isTransparentContext())
Douglas Gregor9ac66d22009-01-20 16:54:50 +0000588 getParent()->makeDeclVisibleInContext(D);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000589}
590
Douglas Gregor9ac66d22009-01-20 16:54:50 +0000591void DeclContext::makeDeclVisibleInContextImpl(NamedDecl *D) {
Douglas Gregord8028382009-01-05 19:45:36 +0000592 // Skip unnamed declarations.
593 if (!D->getDeclName())
594 return;
595
Douglas Gregora08b6c72009-02-17 23:15:12 +0000596 // FIXME: This feels like a hack. Should DeclarationName support
597 // template-ids, or is there a better way to keep specializations
598 // from being visible?
599 if (isa<ClassTemplateSpecializationDecl>(D))
600 return;
601
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000602 bool MayBeRedeclaration = true;
603
Douglas Gregor8acb7272008-12-11 16:49:14 +0000604 if (!isLookupMap()) {
605 unsigned Size = LookupPtr.getInt();
606
607 // The lookup data is stored as an array. Search through the array
608 // to find the insertion location.
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000609 NamedDecl **Array;
Douglas Gregor8acb7272008-12-11 16:49:14 +0000610 if (Size == 0) {
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000611 Array = new NamedDecl*[LookupIsMap - 1];
Douglas Gregor8acb7272008-12-11 16:49:14 +0000612 LookupPtr.setPointer(Array);
613 } else {
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000614 Array = static_cast<NamedDecl **>(LookupPtr.getPointer());
Douglas Gregor8acb7272008-12-11 16:49:14 +0000615 }
616
617 // We always keep declarations of the same name next to each other
618 // in the array, so that it is easy to return multiple results
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000619 // from lookup().
620 unsigned FirstMatch;
621 for (FirstMatch = 0; FirstMatch != Size; ++FirstMatch)
622 if (Array[FirstMatch]->getDeclName() == D->getDeclName())
Douglas Gregor39677622008-12-11 20:41:00 +0000623 break;
Douglas Gregor8acb7272008-12-11 16:49:14 +0000624
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000625 unsigned InsertPos = FirstMatch;
626 if (FirstMatch != Size) {
627 // We found another declaration with the same name. First
628 // determine whether this is a redeclaration of an existing
629 // declaration in this scope, in which case we will replace the
630 // existing declaration.
631 unsigned LastMatch = FirstMatch;
632 for (; LastMatch != Size; ++LastMatch) {
633 if (Array[LastMatch]->getDeclName() != D->getDeclName())
634 break;
635
Douglas Gregor6e71edc2008-12-23 21:05:05 +0000636 if (D->declarationReplaces(Array[LastMatch])) {
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000637 // D is a redeclaration of an existing element in the
638 // array. Replace that element with D.
639 Array[LastMatch] = D;
640 return;
641 }
Douglas Gregor8acb7272008-12-11 16:49:14 +0000642 }
643
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000644 // [FirstMatch, LastMatch) contains the set of declarations that
645 // have the same name as this declaration. Determine where the
Douglas Gregor7a7be652009-02-03 19:21:40 +0000646 // declaration D will be inserted into this range.
647 if (D->getKind() == Decl::UsingDirective ||
648 D->getIdentifierNamespace() == Decl::IDNS_Tag)
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000649 InsertPos = LastMatch;
650 else if (Array[LastMatch-1]->getIdentifierNamespace() == Decl::IDNS_Tag)
651 InsertPos = LastMatch - 1;
652 else
653 InsertPos = LastMatch;
Douglas Gregor8acb7272008-12-11 16:49:14 +0000654 }
655
656 if (Size < LookupIsMap - 1) {
657 // The new declaration will fit in the array. Insert the new
658 // declaration at the position Match in the array.
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000659 for (unsigned Idx = Size; Idx > InsertPos; --Idx)
Douglas Gregor8acb7272008-12-11 16:49:14 +0000660 Array[Idx] = Array[Idx-1];
661
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000662 Array[InsertPos] = D;
Douglas Gregor8acb7272008-12-11 16:49:14 +0000663 LookupPtr.setInt(Size + 1);
664 return;
665 }
666
667 // We've reached capacity in this array. Create a map and copy in
668 // all of the declarations that were stored in the array.
669 StoredDeclsMap *Map = new StoredDeclsMap(16);
670 LookupPtr.setPointer(Map);
671 LookupPtr.setInt(LookupIsMap);
Douglas Gregor39677622008-12-11 20:41:00 +0000672 for (unsigned Idx = 0; Idx != LookupIsMap - 1; ++Idx)
Douglas Gregor9ac66d22009-01-20 16:54:50 +0000673 makeDeclVisibleInContextImpl(Array[Idx]);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000674 delete [] Array;
675
676 // Fall through to perform insertion into the map.
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000677 MayBeRedeclaration = false;
678 }
Douglas Gregor8acb7272008-12-11 16:49:14 +0000679
680 // Insert this declaration into the map.
Chris Lattner77820d52009-02-20 01:44:05 +0000681 StoredDeclsMap &Map = *static_cast<StoredDeclsMap*>(LookupPtr.getPointer());
682 StoredDeclsList &DeclNameEntries = Map[D->getDeclName()];
683 if (DeclNameEntries.isNull()) {
684 DeclNameEntries.setOnlyValue(D);
Chris Lattner6719b1f2009-02-19 07:00:44 +0000685 return;
Douglas Gregor8acb7272008-12-11 16:49:14 +0000686 }
Chris Lattner265f01d2009-02-20 00:55:03 +0000687
Chris Lattnerba589d42009-02-20 01:10:07 +0000688 // If it is possible that this is a redeclaration, check to see if there is
689 // already a decl for which declarationReplaces returns true. If there is
690 // one, just replace it and return.
Chris Lattner77820d52009-02-20 01:44:05 +0000691 if (MayBeRedeclaration && DeclNameEntries.HandleRedeclaration(D))
692 return;
Chris Lattner265f01d2009-02-20 00:55:03 +0000693
Chris Lattner6719b1f2009-02-19 07:00:44 +0000694 // Put this declaration into the appropriate slot.
Chris Lattner77820d52009-02-20 01:44:05 +0000695 DeclNameEntries.AddSubsequentDecl(D);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000696}
Douglas Gregor7a7be652009-02-03 19:21:40 +0000697
698/// Returns iterator range [First, Last) of UsingDirectiveDecls stored within
699/// this context.
700DeclContext::udir_iterator_range DeclContext::getUsingDirectives() const {
701 lookup_const_result Result = lookup(UsingDirectiveDecl::getName());
702 return udir_iterator_range(reinterpret_cast<udir_iterator>(Result.first),
703 reinterpret_cast<udir_iterator>(Result.second));
704}
705