blob: be349428ecfda5992e0fcf256119c9c0a45858e9 [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"
Douglas Gregorc2ee10d2009-04-07 17:20:56 +000016#include "clang/AST/DeclContextInternals.h"
Argyrios Kyrtzidisd3bb44f2008-06-09 21:05:31 +000017#include "clang/AST/DeclCXX.h"
Douglas Gregoraaba5e32009-02-04 19:02:06 +000018#include "clang/AST/DeclObjC.h"
19#include "clang/AST/DeclTemplate.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000020#include "clang/AST/ExternalASTSource.h"
Eli Friedman56d29372008-06-07 16:52:53 +000021#include "clang/AST/ASTContext.h"
Douglas Gregor44b43212008-12-11 16:49:14 +000022#include "clang/AST/Type.h"
Eli Friedman56d29372008-06-07 16:52:53 +000023#include "llvm/ADT/DenseMap.h"
Chris Lattner49f28ca2009-03-05 08:00:35 +000024#include "llvm/Support/raw_ostream.h"
Douglas Gregor6ed40e32008-12-23 21:05:05 +000025#include <algorithm>
Chris Lattner3daed522009-03-02 22:20:04 +000026#include <cstdio>
Douglas Gregor3fc749d2008-12-23 00:26:44 +000027#include <vector>
Eli Friedman56d29372008-06-07 16:52:53 +000028using namespace clang;
29
30//===----------------------------------------------------------------------===//
31// Statistics
32//===----------------------------------------------------------------------===//
33
Douglas Gregor64650af2009-02-02 23:39:07 +000034#define DECL(Derived, Base) static int n##Derived##s = 0;
35#include "clang/AST/DeclNodes.def"
Eli Friedman56d29372008-06-07 16:52:53 +000036
37static bool StatSwitch = false;
38
39// This keeps track of all decl attributes. Since so few decls have attrs, we
40// keep them in a hash map instead of wasting space in the Decl class.
41typedef llvm::DenseMap<const Decl*, Attr*> DeclAttrMapTy;
42
43static DeclAttrMapTy *DeclAttrs = 0;
44
45const char *Decl::getDeclKindName() const {
46 switch (DeclKind) {
Douglas Gregor64650af2009-02-02 23:39:07 +000047 default: assert(0 && "Declaration not in DeclNodes.def!");
48#define DECL(Derived, Base) case Derived: return #Derived;
49#include "clang/AST/DeclNodes.def"
Eli Friedman56d29372008-06-07 16:52:53 +000050 }
51}
52
Steve Naroff0a473932009-01-20 19:53:53 +000053const char *DeclContext::getDeclKindName() const {
54 switch (DeclKind) {
Douglas Gregor64650af2009-02-02 23:39:07 +000055 default: assert(0 && "Declaration context not in DeclNodes.def!");
Argyrios Kyrtzidis1ad4dd72009-02-16 14:28:33 +000056#define DECL(Derived, Base) case Decl::Derived: return #Derived;
Douglas Gregor64650af2009-02-02 23:39:07 +000057#include "clang/AST/DeclNodes.def"
Steve Naroff0a473932009-01-20 19:53:53 +000058 }
59}
60
Eli Friedman56d29372008-06-07 16:52:53 +000061bool Decl::CollectingStats(bool Enable) {
62 if (Enable)
63 StatSwitch = true;
64 return StatSwitch;
65}
66
67void Decl::PrintStats() {
68 fprintf(stderr, "*** Decl Stats:\n");
Eli Friedman56d29372008-06-07 16:52:53 +000069
Douglas Gregor64650af2009-02-02 23:39:07 +000070 int totalDecls = 0;
71#define DECL(Derived, Base) totalDecls += n##Derived##s;
72#include "clang/AST/DeclNodes.def"
73 fprintf(stderr, " %d decls total.\n", totalDecls);
74
75 int totalBytes = 0;
76#define DECL(Derived, Base) \
77 if (n##Derived##s > 0) { \
78 totalBytes += (int)(n##Derived##s * sizeof(Derived##Decl)); \
79 fprintf(stderr, " %d " #Derived " decls, %d each (%d bytes)\n", \
80 n##Derived##s, (int)sizeof(Derived##Decl), \
81 (int)(n##Derived##s * sizeof(Derived##Decl))); \
82 }
83#include "clang/AST/DeclNodes.def"
Eli Friedman56d29372008-06-07 16:52:53 +000084
Douglas Gregor64650af2009-02-02 23:39:07 +000085 fprintf(stderr, "Total bytes = %d\n", totalBytes);
Eli Friedman56d29372008-06-07 16:52:53 +000086}
87
88void Decl::addDeclKind(Kind k) {
89 switch (k) {
Douglas Gregor64650af2009-02-02 23:39:07 +000090 default: assert(0 && "Declaration not in DeclNodes.def!");
91#define DECL(Derived, Base) case Derived: ++n##Derived##s; break;
92#include "clang/AST/DeclNodes.def"
Eli Friedman56d29372008-06-07 16:52:53 +000093 }
94}
95
96//===----------------------------------------------------------------------===//
Chris Lattner49f28ca2009-03-05 08:00:35 +000097// PrettyStackTraceDecl Implementation
98//===----------------------------------------------------------------------===//
99
100void PrettyStackTraceDecl::print(llvm::raw_ostream &OS) const {
101 SourceLocation TheLoc = Loc;
102 if (TheLoc.isInvalid() && TheDecl)
103 TheLoc = TheDecl->getLocation();
104
105 if (TheLoc.isValid()) {
106 TheLoc.print(OS, SM);
107 OS << ": ";
108 }
109
110 OS << Message;
111
112 if (NamedDecl *DN = dyn_cast_or_null<NamedDecl>(TheDecl))
113 OS << " '" << DN->getQualifiedNameAsString() << '\'';
114 OS << '\n';
115}
116
117//===----------------------------------------------------------------------===//
Eli Friedman56d29372008-06-07 16:52:53 +0000118// Decl Implementation
119//===----------------------------------------------------------------------===//
120
Chris Lattner769dbdf2009-03-27 20:18:19 +0000121// Out-of-line virtual method providing a home for Decl.
122Decl::~Decl() {
123 if (isOutOfSemaDC())
124 delete getMultipleDC();
125
126 assert(!HasAttrs && "attributes should have been freed by Destroy");
127}
128
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000129void Decl::setDeclContext(DeclContext *DC) {
130 if (isOutOfSemaDC())
131 delete getMultipleDC();
132
Chris Lattneree219fd2009-03-29 06:06:59 +0000133 DeclCtx = DC;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000134}
135
136void Decl::setLexicalDeclContext(DeclContext *DC) {
137 if (DC == getLexicalDeclContext())
138 return;
139
140 if (isInSemaDC()) {
141 MultipleDC *MDC = new MultipleDC();
142 MDC->SemanticDC = getDeclContext();
143 MDC->LexicalDC = DC;
Chris Lattneree219fd2009-03-29 06:06:59 +0000144 DeclCtx = MDC;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000145 } else {
146 getMultipleDC()->LexicalDC = DC;
147 }
148}
149
Chris Lattner769dbdf2009-03-27 20:18:19 +0000150unsigned Decl::getIdentifierNamespaceForKind(Kind DeclKind) {
151 switch (DeclKind) {
152 default:
153 if (DeclKind >= FunctionFirst && DeclKind <= FunctionLast)
154 return IDNS_Ordinary;
155 assert(0 && "Unknown decl kind!");
156 case OverloadedFunction:
157 case Typedef:
158 case EnumConstant:
159 case Var:
160 case ImplicitParam:
161 case ParmVar:
162 case OriginalParmVar:
163 case NonTypeTemplateParm:
164 case ObjCMethod:
165 case ObjCContainer:
166 case ObjCCategory:
167 case ObjCInterface:
168 case ObjCCategoryImpl:
169 case ObjCProperty:
170 case ObjCCompatibleAlias:
171 return IDNS_Ordinary;
172
173 case ObjCProtocol:
174 return IDNS_Protocol;
175
176 case Field:
177 case ObjCAtDefsField:
178 case ObjCIvar:
179 return IDNS_Member;
180
181 case Record:
182 case CXXRecord:
183 case Enum:
184 case TemplateTypeParm:
185 return IDNS_Tag;
186
187 case Namespace:
188 case Template:
189 case FunctionTemplate:
190 case ClassTemplate:
191 case TemplateTemplateParm:
Anders Carlssonfaf0e872009-03-28 23:02:53 +0000192 case NamespaceAlias:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000193 return IDNS_Tag | IDNS_Ordinary;
194
195 // Never have names.
196 case LinkageSpec:
197 case FileScopeAsm:
198 case StaticAssert:
199 case ObjCClass:
200 case ObjCImplementation:
201 case ObjCPropertyImpl:
202 case ObjCForwardProtocol:
203 case Block:
204 case TranslationUnit:
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000205
Chris Lattner769dbdf2009-03-27 20:18:19 +0000206 // Aren't looked up?
207 case UsingDirective:
208 case ClassTemplateSpecialization:
209 return 0;
210 }
Eli Friedman56d29372008-06-07 16:52:53 +0000211}
212
213void Decl::addAttr(Attr *NewAttr) {
214 if (!DeclAttrs)
215 DeclAttrs = new DeclAttrMapTy();
216
217 Attr *&ExistingAttr = (*DeclAttrs)[this];
218
219 NewAttr->setNext(ExistingAttr);
220 ExistingAttr = NewAttr;
221
222 HasAttrs = true;
223}
224
225void Decl::invalidateAttrs() {
226 if (!HasAttrs) return;
227
228 HasAttrs = false;
229 (*DeclAttrs)[this] = 0;
230 DeclAttrs->erase(this);
231
232 if (DeclAttrs->empty()) {
233 delete DeclAttrs;
234 DeclAttrs = 0;
235 }
236}
237
Chris Lattner81abbdd2009-03-21 06:27:31 +0000238const Attr *Decl::getAttrsImpl() const {
239 assert(HasAttrs && "getAttrs() should verify this!");
Eli Friedman56d29372008-06-07 16:52:53 +0000240 return (*DeclAttrs)[this];
241}
242
243void Decl::swapAttrs(Decl *RHS) {
244 bool HasLHSAttr = this->HasAttrs;
245 bool HasRHSAttr = RHS->HasAttrs;
246
247 // Usually, neither decl has attrs, nothing to do.
248 if (!HasLHSAttr && !HasRHSAttr) return;
249
250 // If 'this' has no attrs, swap the other way.
251 if (!HasLHSAttr)
252 return RHS->swapAttrs(this);
253
254 // Handle the case when both decls have attrs.
255 if (HasRHSAttr) {
256 std::swap((*DeclAttrs)[this], (*DeclAttrs)[RHS]);
257 return;
258 }
259
260 // Otherwise, LHS has an attr and RHS doesn't.
261 (*DeclAttrs)[RHS] = (*DeclAttrs)[this];
262 (*DeclAttrs).erase(this);
263 this->HasAttrs = false;
264 RHS->HasAttrs = true;
265}
266
267
Chris Lattnercc581472009-03-04 06:05:19 +0000268void Decl::Destroy(ASTContext &C) {
269 // Free attributes for this decl.
270 if (HasAttrs) {
271 DeclAttrMapTy::iterator it = DeclAttrs->find(this);
272 assert(it != DeclAttrs->end() && "No attrs found but HasAttrs is true!");
273
274 // release attributes.
275 it->second->Destroy(C);
276 invalidateAttrs();
277 HasAttrs = false;
278 }
279
Douglas Gregora0fc55f2009-01-13 19:47:12 +0000280#if 0
Douglas Gregor00ad0ef2009-01-20 04:25:11 +0000281 // FIXME: Once ownership is fully understood, we can enable this code
282 if (DeclContext *DC = dyn_cast<DeclContext>(this))
283 DC->decls_begin()->Destroy(C);
Eli Friedman56d29372008-06-07 16:52:53 +0000284
Chris Lattner244a67d2009-03-28 06:04:26 +0000285 // Observe the unrolled recursion. By setting N->NextDeclInContext = 0x0
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000286 // within the loop, only the Destroy method for the first Decl
287 // will deallocate all of the Decls in a chain.
288
Chris Lattner244a67d2009-03-28 06:04:26 +0000289 Decl* N = getNextDeclInContext();
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000290
291 while (N) {
Chris Lattner244a67d2009-03-28 06:04:26 +0000292 Decl* Tmp = N->getNextDeclInContext();
293 N->NextDeclInContext = 0;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000294 N->Destroy(C);
295 N = Tmp;
Eli Friedman56d29372008-06-07 16:52:53 +0000296 }
Douglas Gregora0fc55f2009-01-13 19:47:12 +0000297
Eli Friedman56d29372008-06-07 16:52:53 +0000298 this->~Decl();
Steve Naroff3e970492009-01-27 21:25:57 +0000299 C.Deallocate((void *)this);
Douglas Gregor00ad0ef2009-01-20 04:25:11 +0000300#endif
Eli Friedman56d29372008-06-07 16:52:53 +0000301}
302
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000303Decl *Decl::castFromDeclContext (const DeclContext *D) {
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000304 Decl::Kind DK = D->getDeclKind();
305 switch(DK) {
306#define DECL_CONTEXT(Name) \
307 case Decl::Name: \
308 return static_cast<Name##Decl*>(const_cast<DeclContext*>(D));
309#define DECL_CONTEXT_BASE(Name)
310#include "clang/AST/DeclNodes.def"
311 default:
312#define DECL_CONTEXT_BASE(Name) \
313 if (DK >= Decl::Name##First && DK <= Decl::Name##Last) \
314 return static_cast<Name##Decl*>(const_cast<DeclContext*>(D));
315#include "clang/AST/DeclNodes.def"
316 assert(false && "a decl that inherits DeclContext isn't handled");
317 return 0;
318 }
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000319}
320
321DeclContext *Decl::castToDeclContext(const Decl *D) {
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000322 Decl::Kind DK = D->getKind();
323 switch(DK) {
324#define DECL_CONTEXT(Name) \
325 case Decl::Name: \
326 return static_cast<Name##Decl*>(const_cast<Decl*>(D));
327#define DECL_CONTEXT_BASE(Name)
328#include "clang/AST/DeclNodes.def"
329 default:
330#define DECL_CONTEXT_BASE(Name) \
331 if (DK >= Decl::Name##First && DK <= Decl::Name##Last) \
332 return static_cast<Name##Decl*>(const_cast<Decl*>(D));
333#include "clang/AST/DeclNodes.def"
334 assert(false && "a decl that inherits DeclContext isn't handled");
335 return 0;
336 }
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000337}
338
Anders Carlsson1329c272009-03-25 23:38:06 +0000339#ifndef NDEBUG
340void Decl::CheckAccessDeclContext() const {
Douglas Gregor5c27f2b2009-04-07 20:58:25 +0000341 assert((Access != AS_none || isa<TranslationUnitDecl>(this) ||
342 !isa<CXXRecordDecl>(getDeclContext())) &&
Anders Carlsson1329c272009-03-25 23:38:06 +0000343 "Access specifier is AS_none inside a record decl");
344}
345
346#endif
347
Eli Friedman56d29372008-06-07 16:52:53 +0000348//===----------------------------------------------------------------------===//
349// DeclContext Implementation
350//===----------------------------------------------------------------------===//
351
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000352bool DeclContext::classof(const Decl *D) {
353 switch (D->getKind()) {
354#define DECL_CONTEXT(Name) case Decl::Name:
355#define DECL_CONTEXT_BASE(Name)
356#include "clang/AST/DeclNodes.def"
357 return true;
358 default:
359#define DECL_CONTEXT_BASE(Name) \
360 if (D->getKind() >= Decl::Name##First && \
361 D->getKind() <= Decl::Name##Last) \
362 return true;
363#include "clang/AST/DeclNodes.def"
364 return false;
365 }
366}
367
Douglas Gregor44b43212008-12-11 16:49:14 +0000368DeclContext::~DeclContext() {
Douglas Gregorc36c5402009-04-09 17:29:08 +0000369 delete static_cast<StoredDeclsMap*>(LookupPtr);
Douglas Gregor44b43212008-12-11 16:49:14 +0000370}
371
372void DeclContext::DestroyDecls(ASTContext &C) {
Douglas Gregor6ab35242009-04-09 21:40:53 +0000373 for (decl_iterator D = decls_begin(C); D != decls_end(C); )
Douglas Gregor00ad0ef2009-01-20 04:25:11 +0000374 (*D++)->Destroy(C);
Douglas Gregor44b43212008-12-11 16:49:14 +0000375}
376
Douglas Gregor074149e2009-01-05 19:45:36 +0000377bool DeclContext::isTransparentContext() const {
378 if (DeclKind == Decl::Enum)
379 return true; // FIXME: Check for C++0x scoped enums
380 else if (DeclKind == Decl::LinkageSpec)
381 return true;
Douglas Gregor65100792009-02-26 00:02:51 +0000382 else if (DeclKind >= Decl::RecordFirst && DeclKind <= Decl::RecordLast)
Douglas Gregorbcbffc42009-01-07 00:43:41 +0000383 return cast<RecordDecl>(this)->isAnonymousStructOrUnion();
Douglas Gregor074149e2009-01-05 19:45:36 +0000384 else if (DeclKind == Decl::Namespace)
385 return false; // FIXME: Check for C++0x inline namespaces
386
387 return false;
388}
389
Steve Naroff0701bbb2009-01-08 17:28:14 +0000390DeclContext *DeclContext::getPrimaryContext() {
Douglas Gregor44b43212008-12-11 16:49:14 +0000391 switch (DeclKind) {
Douglas Gregor44b43212008-12-11 16:49:14 +0000392 case Decl::TranslationUnit:
Douglas Gregor074149e2009-01-05 19:45:36 +0000393 case Decl::LinkageSpec:
394 case Decl::Block:
Douglas Gregor44b43212008-12-11 16:49:14 +0000395 // There is only one DeclContext for these entities.
396 return this;
397
398 case Decl::Namespace:
399 // The original namespace is our primary context.
400 return static_cast<NamespaceDecl*>(this)->getOriginalNamespace();
401
Douglas Gregor44b43212008-12-11 16:49:14 +0000402 case Decl::ObjCMethod:
403 return this;
404
405 case Decl::ObjCInterface:
Steve Naroff0701bbb2009-01-08 17:28:14 +0000406 case Decl::ObjCProtocol:
407 case Decl::ObjCCategory:
Douglas Gregor44b43212008-12-11 16:49:14 +0000408 // FIXME: Can Objective-C interfaces be forward-declared?
409 return this;
410
Steve Naroff0701bbb2009-01-08 17:28:14 +0000411 case Decl::ObjCImplementation:
412 case Decl::ObjCCategoryImpl:
413 return this;
414
Douglas Gregor44b43212008-12-11 16:49:14 +0000415 default:
Douglas Gregorcc636682009-02-17 23:15:12 +0000416 if (DeclKind >= Decl::TagFirst && DeclKind <= Decl::TagLast) {
417 // If this is a tag type that has a definition or is currently
418 // being defined, that definition is our primary context.
Chris Lattner244a67d2009-03-28 06:04:26 +0000419 if (const TagType *TagT =cast<TagDecl>(this)->TypeForDecl->getAsTagType())
Douglas Gregorcc636682009-02-17 23:15:12 +0000420 if (TagT->isBeingDefined() ||
421 (TagT->getDecl() && TagT->getDecl()->isDefinition()))
422 return TagT->getDecl();
423 return this;
424 }
425
Douglas Gregor44b43212008-12-11 16:49:14 +0000426 assert(DeclKind >= Decl::FunctionFirst && DeclKind <= Decl::FunctionLast &&
427 "Unknown DeclContext kind");
428 return this;
429 }
430}
431
432DeclContext *DeclContext::getNextContext() {
433 switch (DeclKind) {
Douglas Gregor44b43212008-12-11 16:49:14 +0000434 case Decl::Namespace:
435 // Return the next namespace
436 return static_cast<NamespaceDecl*>(this)->getNextNamespace();
437
438 default:
Douglas Gregor44b43212008-12-11 16:49:14 +0000439 return 0;
440 }
441}
442
Douglas Gregor2cf26342009-04-09 22:27:44 +0000443/// \brief Load the declarations within this lexical storage from an
444/// external source.
445void
446DeclContext::LoadLexicalDeclsFromExternalStorage(ASTContext &Context) const {
447 ExternalASTSource *Source = Context.getExternalSource();
448 assert(hasExternalLexicalStorage() && Source && "No external storage?");
449
450 llvm::SmallVector<unsigned, 64> Decls;
451 if (Source->ReadDeclsLexicallyInContext(const_cast<DeclContext *>(this),
452 Decls))
453 return;
454
455 // There is no longer any lexical storage in this context
456 ExternalLexicalStorage = false;
457
458 if (Decls.empty())
459 return;
460
461 // Resolve all of the declaration IDs into declarations, building up
462 // a chain of declarations via the Decl::NextDeclInContext field.
463 Decl *FirstNewDecl = 0;
464 Decl *PrevDecl = 0;
465 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
466 Decl *D = Source->GetDecl(Decls[I]);
467 if (PrevDecl)
468 PrevDecl->NextDeclInContext = D;
469 else
470 FirstNewDecl = D;
471
472 PrevDecl = D;
473 }
474
475 // Splice the newly-read declarations into the beginning of the list
476 // of declarations.
477 PrevDecl->NextDeclInContext = FirstDecl;
478 FirstDecl = FirstNewDecl;
479 if (!LastDecl)
480 LastDecl = PrevDecl;
481}
482
483void
484DeclContext::LoadVisibleDeclsFromExternalStorage(ASTContext &Context) const {
485 DeclContext *This = const_cast<DeclContext *>(this);
486 ExternalASTSource *Source = Context.getExternalSource();
487 assert(hasExternalVisibleStorage() && Source && "No external storage?");
488
489 llvm::SmallVector<VisibleDeclaration, 64> Decls;
490 if (Source->ReadDeclsVisibleInContext(This, Decls))
491 return;
492
493 // There is no longer any visible storage in this context
494 ExternalVisibleStorage = false;
495
496 // Load the declaration IDs for all of the names visible in this
497 // context.
498 assert(!LookupPtr && "Have a lookup map before de-serialization?");
499 StoredDeclsMap *Map = new StoredDeclsMap;
500 LookupPtr = Map;
501 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
502 (*Map)[Decls[I].Name].setFromDeclIDs(Decls[I].Declarations);
503 }
504}
505
Douglas Gregor6ab35242009-04-09 21:40:53 +0000506DeclContext::decl_iterator DeclContext::decls_begin(ASTContext &Context) const {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000507 if (hasExternalLexicalStorage())
508 LoadLexicalDeclsFromExternalStorage(Context);
509
510 // FIXME: Check whether we need to load some declarations from
511 // external storage.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000512 return decl_iterator(FirstDecl);
513}
514
515DeclContext::decl_iterator DeclContext::decls_end(ASTContext &Context) const {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000516 if (hasExternalLexicalStorage())
517 LoadLexicalDeclsFromExternalStorage(Context);
518
Douglas Gregor6ab35242009-04-09 21:40:53 +0000519 return decl_iterator();
520}
521
522void DeclContext::addDecl(ASTContext &Context, Decl *D) {
Chris Lattner7f0be132009-02-20 00:56:18 +0000523 assert(D->getLexicalDeclContext() == this &&
524 "Decl inserted into wrong lexical context");
Chris Lattner244a67d2009-03-28 06:04:26 +0000525 assert(!D->getNextDeclInContext() && D != LastDecl &&
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000526 "Decl already inserted into a DeclContext");
527
528 if (FirstDecl) {
Chris Lattner244a67d2009-03-28 06:04:26 +0000529 LastDecl->NextDeclInContext = D;
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000530 LastDecl = D;
531 } else {
532 FirstDecl = LastDecl = D;
533 }
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000534
535 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
Douglas Gregor6ab35242009-04-09 21:40:53 +0000536 ND->getDeclContext()->makeDeclVisibleInContext(Context, ND);
Douglas Gregor44b43212008-12-11 16:49:14 +0000537}
538
Douglas Gregor074149e2009-01-05 19:45:36 +0000539/// buildLookup - Build the lookup data structure with all of the
540/// declarations in DCtx (and any other contexts linked to it or
541/// transparent contexts nested within it).
Douglas Gregor6ab35242009-04-09 21:40:53 +0000542void DeclContext::buildLookup(ASTContext &Context, DeclContext *DCtx) {
Douglas Gregor074149e2009-01-05 19:45:36 +0000543 for (; DCtx; DCtx = DCtx->getNextContext()) {
Douglas Gregor6ab35242009-04-09 21:40:53 +0000544 for (decl_iterator D = DCtx->decls_begin(Context),
545 DEnd = DCtx->decls_end(Context);
Douglas Gregor4f3b8f82009-01-06 07:17:58 +0000546 D != DEnd; ++D) {
Douglas Gregor074149e2009-01-05 19:45:36 +0000547 // Insert this declaration into the lookup structure
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000548 if (NamedDecl *ND = dyn_cast<NamedDecl>(*D))
Douglas Gregor6ab35242009-04-09 21:40:53 +0000549 makeDeclVisibleInContextImpl(Context, ND);
Douglas Gregor074149e2009-01-05 19:45:36 +0000550
551 // If this declaration is itself a transparent declaration context,
552 // add its members (recursively).
553 if (DeclContext *InnerCtx = dyn_cast<DeclContext>(*D))
554 if (InnerCtx->isTransparentContext())
Douglas Gregor6ab35242009-04-09 21:40:53 +0000555 buildLookup(Context, InnerCtx->getPrimaryContext());
Douglas Gregor074149e2009-01-05 19:45:36 +0000556 }
557 }
558}
559
Douglas Gregor44b43212008-12-11 16:49:14 +0000560DeclContext::lookup_result
Douglas Gregor6ab35242009-04-09 21:40:53 +0000561DeclContext::lookup(ASTContext &Context, DeclarationName Name) {
Steve Naroff0701bbb2009-01-08 17:28:14 +0000562 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor44b43212008-12-11 16:49:14 +0000563 if (PrimaryContext != this)
Douglas Gregor6ab35242009-04-09 21:40:53 +0000564 return PrimaryContext->lookup(Context, Name);
Douglas Gregor44b43212008-12-11 16:49:14 +0000565
Douglas Gregor2cf26342009-04-09 22:27:44 +0000566 if (hasExternalVisibleStorage())
567 LoadVisibleDeclsFromExternalStorage(Context);
568
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000569 /// If there is no lookup data structure, build one now by walking
Douglas Gregor44b43212008-12-11 16:49:14 +0000570 /// all of the linked DeclContexts (in declaration order!) and
571 /// inserting their values.
Douglas Gregorc36c5402009-04-09 17:29:08 +0000572 if (!LookupPtr) {
Douglas Gregor6ab35242009-04-09 21:40:53 +0000573 buildLookup(Context, this);
Douglas Gregor44b43212008-12-11 16:49:14 +0000574
Douglas Gregorc36c5402009-04-09 17:29:08 +0000575 if (!LookupPtr)
Chris Lattner91942502009-02-20 00:55:03 +0000576 return lookup_result(0, 0);
Douglas Gregorc36c5402009-04-09 17:29:08 +0000577 }
Douglas Gregor44b43212008-12-11 16:49:14 +0000578
Douglas Gregorc36c5402009-04-09 17:29:08 +0000579 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(LookupPtr);
580 StoredDeclsMap::iterator Pos = Map->find(Name);
581 if (Pos == Map->end())
582 return lookup_result(0, 0);
Douglas Gregor6ab35242009-04-09 21:40:53 +0000583 return Pos->second.getLookupResult(Context);
Douglas Gregor44b43212008-12-11 16:49:14 +0000584}
585
586DeclContext::lookup_const_result
Douglas Gregor6ab35242009-04-09 21:40:53 +0000587DeclContext::lookup(ASTContext &Context, DeclarationName Name) const {
588 return const_cast<DeclContext*>(this)->lookup(Context, Name);
Douglas Gregor44b43212008-12-11 16:49:14 +0000589}
590
Chris Lattner0cf2b192009-03-27 19:19:59 +0000591DeclContext *DeclContext::getLookupContext() {
592 DeclContext *Ctx = this;
Douglas Gregor72de6672009-01-08 20:45:30 +0000593 // Skip through transparent contexts.
Douglas Gregorce356072009-01-06 23:51:29 +0000594 while (Ctx->isTransparentContext())
595 Ctx = Ctx->getParent();
596 return Ctx;
597}
598
Douglas Gregor88b70942009-02-25 22:02:03 +0000599DeclContext *DeclContext::getEnclosingNamespaceContext() {
600 DeclContext *Ctx = this;
601 // Skip through non-namespace, non-translation-unit contexts.
602 while (!Ctx->isFileContext() || Ctx->isTransparentContext())
603 Ctx = Ctx->getParent();
604 return Ctx->getPrimaryContext();
605}
606
Douglas Gregor6ab35242009-04-09 21:40:53 +0000607void DeclContext::makeDeclVisibleInContext(ASTContext &Context, NamedDecl *D) {
Douglas Gregorcc636682009-02-17 23:15:12 +0000608 // FIXME: This feels like a hack. Should DeclarationName support
609 // template-ids, or is there a better way to keep specializations
610 // from being visible?
611 if (isa<ClassTemplateSpecializationDecl>(D))
612 return;
613
Steve Naroff0701bbb2009-01-08 17:28:14 +0000614 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor44b43212008-12-11 16:49:14 +0000615 if (PrimaryContext != this) {
Douglas Gregor6ab35242009-04-09 21:40:53 +0000616 PrimaryContext->makeDeclVisibleInContext(Context, D);
Douglas Gregor44b43212008-12-11 16:49:14 +0000617 return;
618 }
619
620 // If we already have a lookup data structure, perform the insertion
621 // into it. Otherwise, be lazy and don't build that structure until
622 // someone asks for it.
Douglas Gregorc36c5402009-04-09 17:29:08 +0000623 if (LookupPtr)
Douglas Gregor6ab35242009-04-09 21:40:53 +0000624 makeDeclVisibleInContextImpl(Context, D);
Douglas Gregor074149e2009-01-05 19:45:36 +0000625
Douglas Gregor074149e2009-01-05 19:45:36 +0000626 // If we are a transparent context, insert into our parent context,
627 // too. This operation is recursive.
628 if (isTransparentContext())
Douglas Gregor6ab35242009-04-09 21:40:53 +0000629 getParent()->makeDeclVisibleInContext(Context, D);
Douglas Gregor44b43212008-12-11 16:49:14 +0000630}
631
Douglas Gregor6ab35242009-04-09 21:40:53 +0000632void DeclContext::makeDeclVisibleInContextImpl(ASTContext &Context,
633 NamedDecl *D) {
Douglas Gregor074149e2009-01-05 19:45:36 +0000634 // Skip unnamed declarations.
635 if (!D->getDeclName())
636 return;
637
Douglas Gregorcc636682009-02-17 23:15:12 +0000638 // FIXME: This feels like a hack. Should DeclarationName support
639 // template-ids, or is there a better way to keep specializations
640 // from being visible?
641 if (isa<ClassTemplateSpecializationDecl>(D))
642 return;
643
Douglas Gregorc36c5402009-04-09 17:29:08 +0000644 if (!LookupPtr)
645 LookupPtr = new StoredDeclsMap;
Douglas Gregor44b43212008-12-11 16:49:14 +0000646
647 // Insert this declaration into the map.
Douglas Gregorc36c5402009-04-09 17:29:08 +0000648 StoredDeclsMap &Map = *static_cast<StoredDeclsMap*>(LookupPtr);
Chris Lattner67762a32009-02-20 01:44:05 +0000649 StoredDeclsList &DeclNameEntries = Map[D->getDeclName()];
650 if (DeclNameEntries.isNull()) {
651 DeclNameEntries.setOnlyValue(D);
Chris Lattnerbd6c8002009-02-19 07:00:44 +0000652 return;
Douglas Gregor44b43212008-12-11 16:49:14 +0000653 }
Chris Lattner91942502009-02-20 00:55:03 +0000654
Chris Lattnerbdc3d002009-02-20 01:10:07 +0000655 // If it is possible that this is a redeclaration, check to see if there is
656 // already a decl for which declarationReplaces returns true. If there is
657 // one, just replace it and return.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000658 if (DeclNameEntries.HandleRedeclaration(Context, D))
Chris Lattner67762a32009-02-20 01:44:05 +0000659 return;
Chris Lattner91942502009-02-20 00:55:03 +0000660
Chris Lattnerbd6c8002009-02-19 07:00:44 +0000661 // Put this declaration into the appropriate slot.
Chris Lattner67762a32009-02-20 01:44:05 +0000662 DeclNameEntries.AddSubsequentDecl(D);
Douglas Gregor44b43212008-12-11 16:49:14 +0000663}
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000664
665/// Returns iterator range [First, Last) of UsingDirectiveDecls stored within
666/// this context.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000667DeclContext::udir_iterator_range
668DeclContext::getUsingDirectives(ASTContext &Context) const {
669 lookup_const_result Result = lookup(Context, UsingDirectiveDecl::getName());
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000670 return udir_iterator_range(reinterpret_cast<udir_iterator>(Result.first),
671 reinterpret_cast<udir_iterator>(Result.second));
672}
Douglas Gregor2cf26342009-04-09 22:27:44 +0000673
674void StoredDeclsList::materializeDecls(ASTContext &Context) {
675 if (isNull())
676 return;
677
678 switch ((DataKind)(Data & 0x03)) {
679 case DK_Decl:
680 case DK_Decl_Vector:
681 break;
682
683 case DK_DeclID: {
684 // Resolve this declaration ID to an actual declaration by
685 // querying the external AST source.
686 unsigned DeclID = Data >> 2;
687
688 ExternalASTSource *Source = Context.getExternalSource();
689 assert(Source && "No external AST source available!");
690
691 Data = reinterpret_cast<uintptr_t>(Source->GetDecl(DeclID));
692 break;
693 }
694
695 case DK_ID_Vector: {
696 // We have a vector of declaration IDs. Resolve all of them to
697 // actual declarations.
698 VectorTy &Vector = *getAsVector();
699 ExternalASTSource *Source = Context.getExternalSource();
700 assert(Source && "No external AST source available!");
701
702 for (unsigned I = 0, N = Vector.size(); I != N; ++I)
703 Vector[I] = reinterpret_cast<uintptr_t>(Source->GetDecl(Vector[I]));
704
705 Data = (Data & ~0x03) | DK_Decl_Vector;
706 break;
707 }
708 }
709}