blob: 010dc677afe0005ad457ea459b33cf12a1ff34c2 [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:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000168 case ObjCProperty:
169 case ObjCCompatibleAlias:
170 return IDNS_Ordinary;
171
172 case ObjCProtocol:
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000173 return IDNS_ObjCProtocol;
Chris Lattner769dbdf2009-03-27 20:18:19 +0000174
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000175 case ObjCImplementation:
176 return IDNS_ObjCImplementation;
177
178 case ObjCCategoryImpl:
179 return IDNS_ObjCCategoryImpl;
180
Chris Lattner769dbdf2009-03-27 20:18:19 +0000181 case Field:
182 case ObjCAtDefsField:
183 case ObjCIvar:
184 return IDNS_Member;
185
186 case Record:
187 case CXXRecord:
188 case Enum:
189 case TemplateTypeParm:
190 return IDNS_Tag;
191
192 case Namespace:
193 case Template:
194 case FunctionTemplate:
195 case ClassTemplate:
196 case TemplateTemplateParm:
Anders Carlssonfaf0e872009-03-28 23:02:53 +0000197 case NamespaceAlias:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000198 return IDNS_Tag | IDNS_Ordinary;
199
200 // Never have names.
201 case LinkageSpec:
202 case FileScopeAsm:
203 case StaticAssert:
204 case ObjCClass:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000205 case ObjCPropertyImpl:
206 case ObjCForwardProtocol:
207 case Block:
208 case TranslationUnit:
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000209
Chris Lattner769dbdf2009-03-27 20:18:19 +0000210 // Aren't looked up?
211 case UsingDirective:
212 case ClassTemplateSpecialization:
213 return 0;
214 }
Eli Friedman56d29372008-06-07 16:52:53 +0000215}
216
217void Decl::addAttr(Attr *NewAttr) {
218 if (!DeclAttrs)
219 DeclAttrs = new DeclAttrMapTy();
220
221 Attr *&ExistingAttr = (*DeclAttrs)[this];
222
223 NewAttr->setNext(ExistingAttr);
224 ExistingAttr = NewAttr;
225
226 HasAttrs = true;
227}
228
229void Decl::invalidateAttrs() {
230 if (!HasAttrs) return;
231
232 HasAttrs = false;
233 (*DeclAttrs)[this] = 0;
234 DeclAttrs->erase(this);
235
236 if (DeclAttrs->empty()) {
237 delete DeclAttrs;
238 DeclAttrs = 0;
239 }
240}
241
Chris Lattner81abbdd2009-03-21 06:27:31 +0000242const Attr *Decl::getAttrsImpl() const {
243 assert(HasAttrs && "getAttrs() should verify this!");
Eli Friedman56d29372008-06-07 16:52:53 +0000244 return (*DeclAttrs)[this];
245}
246
247void Decl::swapAttrs(Decl *RHS) {
248 bool HasLHSAttr = this->HasAttrs;
249 bool HasRHSAttr = RHS->HasAttrs;
250
251 // Usually, neither decl has attrs, nothing to do.
252 if (!HasLHSAttr && !HasRHSAttr) return;
253
254 // If 'this' has no attrs, swap the other way.
255 if (!HasLHSAttr)
256 return RHS->swapAttrs(this);
257
258 // Handle the case when both decls have attrs.
259 if (HasRHSAttr) {
260 std::swap((*DeclAttrs)[this], (*DeclAttrs)[RHS]);
261 return;
262 }
263
264 // Otherwise, LHS has an attr and RHS doesn't.
265 (*DeclAttrs)[RHS] = (*DeclAttrs)[this];
266 (*DeclAttrs).erase(this);
267 this->HasAttrs = false;
268 RHS->HasAttrs = true;
269}
270
271
Chris Lattnercc581472009-03-04 06:05:19 +0000272void Decl::Destroy(ASTContext &C) {
273 // Free attributes for this decl.
274 if (HasAttrs) {
275 DeclAttrMapTy::iterator it = DeclAttrs->find(this);
276 assert(it != DeclAttrs->end() && "No attrs found but HasAttrs is true!");
277
278 // release attributes.
279 it->second->Destroy(C);
280 invalidateAttrs();
281 HasAttrs = false;
282 }
283
Douglas Gregora0fc55f2009-01-13 19:47:12 +0000284#if 0
Douglas Gregor00ad0ef2009-01-20 04:25:11 +0000285 // FIXME: Once ownership is fully understood, we can enable this code
286 if (DeclContext *DC = dyn_cast<DeclContext>(this))
287 DC->decls_begin()->Destroy(C);
Eli Friedman56d29372008-06-07 16:52:53 +0000288
Chris Lattner244a67d2009-03-28 06:04:26 +0000289 // Observe the unrolled recursion. By setting N->NextDeclInContext = 0x0
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000290 // within the loop, only the Destroy method for the first Decl
291 // will deallocate all of the Decls in a chain.
292
Chris Lattner244a67d2009-03-28 06:04:26 +0000293 Decl* N = getNextDeclInContext();
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000294
295 while (N) {
Chris Lattner244a67d2009-03-28 06:04:26 +0000296 Decl* Tmp = N->getNextDeclInContext();
297 N->NextDeclInContext = 0;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000298 N->Destroy(C);
299 N = Tmp;
Eli Friedman56d29372008-06-07 16:52:53 +0000300 }
Douglas Gregora0fc55f2009-01-13 19:47:12 +0000301
Eli Friedman56d29372008-06-07 16:52:53 +0000302 this->~Decl();
Steve Naroff3e970492009-01-27 21:25:57 +0000303 C.Deallocate((void *)this);
Douglas Gregor00ad0ef2009-01-20 04:25:11 +0000304#endif
Eli Friedman56d29372008-06-07 16:52:53 +0000305}
306
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000307Decl *Decl::castFromDeclContext (const DeclContext *D) {
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000308 Decl::Kind DK = D->getDeclKind();
309 switch(DK) {
310#define DECL_CONTEXT(Name) \
311 case Decl::Name: \
312 return static_cast<Name##Decl*>(const_cast<DeclContext*>(D));
313#define DECL_CONTEXT_BASE(Name)
314#include "clang/AST/DeclNodes.def"
315 default:
316#define DECL_CONTEXT_BASE(Name) \
317 if (DK >= Decl::Name##First && DK <= Decl::Name##Last) \
318 return static_cast<Name##Decl*>(const_cast<DeclContext*>(D));
319#include "clang/AST/DeclNodes.def"
320 assert(false && "a decl that inherits DeclContext isn't handled");
321 return 0;
322 }
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000323}
324
325DeclContext *Decl::castToDeclContext(const Decl *D) {
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000326 Decl::Kind DK = D->getKind();
327 switch(DK) {
328#define DECL_CONTEXT(Name) \
329 case Decl::Name: \
330 return static_cast<Name##Decl*>(const_cast<Decl*>(D));
331#define DECL_CONTEXT_BASE(Name)
332#include "clang/AST/DeclNodes.def"
333 default:
334#define DECL_CONTEXT_BASE(Name) \
335 if (DK >= Decl::Name##First && DK <= Decl::Name##Last) \
336 return static_cast<Name##Decl*>(const_cast<Decl*>(D));
337#include "clang/AST/DeclNodes.def"
338 assert(false && "a decl that inherits DeclContext isn't handled");
339 return 0;
340 }
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000341}
342
Anders Carlsson1329c272009-03-25 23:38:06 +0000343#ifndef NDEBUG
344void Decl::CheckAccessDeclContext() const {
Douglas Gregor5c27f2b2009-04-07 20:58:25 +0000345 assert((Access != AS_none || isa<TranslationUnitDecl>(this) ||
346 !isa<CXXRecordDecl>(getDeclContext())) &&
Anders Carlsson1329c272009-03-25 23:38:06 +0000347 "Access specifier is AS_none inside a record decl");
348}
349
350#endif
351
Eli Friedman56d29372008-06-07 16:52:53 +0000352//===----------------------------------------------------------------------===//
353// DeclContext Implementation
354//===----------------------------------------------------------------------===//
355
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000356bool DeclContext::classof(const Decl *D) {
357 switch (D->getKind()) {
358#define DECL_CONTEXT(Name) case Decl::Name:
359#define DECL_CONTEXT_BASE(Name)
360#include "clang/AST/DeclNodes.def"
361 return true;
362 default:
363#define DECL_CONTEXT_BASE(Name) \
364 if (D->getKind() >= Decl::Name##First && \
365 D->getKind() <= Decl::Name##Last) \
366 return true;
367#include "clang/AST/DeclNodes.def"
368 return false;
369 }
370}
371
Douglas Gregor44b43212008-12-11 16:49:14 +0000372DeclContext::~DeclContext() {
Douglas Gregorc36c5402009-04-09 17:29:08 +0000373 delete static_cast<StoredDeclsMap*>(LookupPtr);
Douglas Gregor44b43212008-12-11 16:49:14 +0000374}
375
376void DeclContext::DestroyDecls(ASTContext &C) {
Douglas Gregor6ab35242009-04-09 21:40:53 +0000377 for (decl_iterator D = decls_begin(C); D != decls_end(C); )
Douglas Gregor00ad0ef2009-01-20 04:25:11 +0000378 (*D++)->Destroy(C);
Douglas Gregor44b43212008-12-11 16:49:14 +0000379}
380
Douglas Gregor074149e2009-01-05 19:45:36 +0000381bool DeclContext::isTransparentContext() const {
382 if (DeclKind == Decl::Enum)
383 return true; // FIXME: Check for C++0x scoped enums
384 else if (DeclKind == Decl::LinkageSpec)
385 return true;
Douglas Gregor65100792009-02-26 00:02:51 +0000386 else if (DeclKind >= Decl::RecordFirst && DeclKind <= Decl::RecordLast)
Douglas Gregorbcbffc42009-01-07 00:43:41 +0000387 return cast<RecordDecl>(this)->isAnonymousStructOrUnion();
Douglas Gregor074149e2009-01-05 19:45:36 +0000388 else if (DeclKind == Decl::Namespace)
389 return false; // FIXME: Check for C++0x inline namespaces
390
391 return false;
392}
393
Steve Naroff0701bbb2009-01-08 17:28:14 +0000394DeclContext *DeclContext::getPrimaryContext() {
Douglas Gregor44b43212008-12-11 16:49:14 +0000395 switch (DeclKind) {
Douglas Gregor44b43212008-12-11 16:49:14 +0000396 case Decl::TranslationUnit:
Douglas Gregor074149e2009-01-05 19:45:36 +0000397 case Decl::LinkageSpec:
398 case Decl::Block:
Douglas Gregor44b43212008-12-11 16:49:14 +0000399 // There is only one DeclContext for these entities.
400 return this;
401
402 case Decl::Namespace:
403 // The original namespace is our primary context.
404 return static_cast<NamespaceDecl*>(this)->getOriginalNamespace();
405
Douglas Gregor44b43212008-12-11 16:49:14 +0000406 case Decl::ObjCMethod:
407 return this;
408
409 case Decl::ObjCInterface:
Steve Naroff0701bbb2009-01-08 17:28:14 +0000410 case Decl::ObjCProtocol:
411 case Decl::ObjCCategory:
Douglas Gregor44b43212008-12-11 16:49:14 +0000412 // FIXME: Can Objective-C interfaces be forward-declared?
413 return this;
414
Steve Naroff0701bbb2009-01-08 17:28:14 +0000415 case Decl::ObjCImplementation:
416 case Decl::ObjCCategoryImpl:
417 return this;
418
Douglas Gregor44b43212008-12-11 16:49:14 +0000419 default:
Douglas Gregorcc636682009-02-17 23:15:12 +0000420 if (DeclKind >= Decl::TagFirst && DeclKind <= Decl::TagLast) {
421 // If this is a tag type that has a definition or is currently
422 // being defined, that definition is our primary context.
Chris Lattner244a67d2009-03-28 06:04:26 +0000423 if (const TagType *TagT =cast<TagDecl>(this)->TypeForDecl->getAsTagType())
Douglas Gregorcc636682009-02-17 23:15:12 +0000424 if (TagT->isBeingDefined() ||
425 (TagT->getDecl() && TagT->getDecl()->isDefinition()))
426 return TagT->getDecl();
427 return this;
428 }
429
Douglas Gregor44b43212008-12-11 16:49:14 +0000430 assert(DeclKind >= Decl::FunctionFirst && DeclKind <= Decl::FunctionLast &&
431 "Unknown DeclContext kind");
432 return this;
433 }
434}
435
436DeclContext *DeclContext::getNextContext() {
437 switch (DeclKind) {
Douglas Gregor44b43212008-12-11 16:49:14 +0000438 case Decl::Namespace:
439 // Return the next namespace
440 return static_cast<NamespaceDecl*>(this)->getNextNamespace();
441
442 default:
Douglas Gregor44b43212008-12-11 16:49:14 +0000443 return 0;
444 }
445}
446
Douglas Gregor2cf26342009-04-09 22:27:44 +0000447/// \brief Load the declarations within this lexical storage from an
448/// external source.
449void
450DeclContext::LoadLexicalDeclsFromExternalStorage(ASTContext &Context) const {
451 ExternalASTSource *Source = Context.getExternalSource();
452 assert(hasExternalLexicalStorage() && Source && "No external storage?");
453
454 llvm::SmallVector<unsigned, 64> Decls;
455 if (Source->ReadDeclsLexicallyInContext(const_cast<DeclContext *>(this),
456 Decls))
457 return;
458
459 // There is no longer any lexical storage in this context
460 ExternalLexicalStorage = false;
461
462 if (Decls.empty())
463 return;
464
465 // Resolve all of the declaration IDs into declarations, building up
466 // a chain of declarations via the Decl::NextDeclInContext field.
467 Decl *FirstNewDecl = 0;
468 Decl *PrevDecl = 0;
469 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
470 Decl *D = Source->GetDecl(Decls[I]);
471 if (PrevDecl)
472 PrevDecl->NextDeclInContext = D;
473 else
474 FirstNewDecl = D;
475
476 PrevDecl = D;
477 }
478
479 // Splice the newly-read declarations into the beginning of the list
480 // of declarations.
481 PrevDecl->NextDeclInContext = FirstDecl;
482 FirstDecl = FirstNewDecl;
483 if (!LastDecl)
484 LastDecl = PrevDecl;
485}
486
487void
488DeclContext::LoadVisibleDeclsFromExternalStorage(ASTContext &Context) const {
489 DeclContext *This = const_cast<DeclContext *>(this);
490 ExternalASTSource *Source = Context.getExternalSource();
491 assert(hasExternalVisibleStorage() && Source && "No external storage?");
492
493 llvm::SmallVector<VisibleDeclaration, 64> Decls;
494 if (Source->ReadDeclsVisibleInContext(This, Decls))
495 return;
496
497 // There is no longer any visible storage in this context
498 ExternalVisibleStorage = false;
499
500 // Load the declaration IDs for all of the names visible in this
501 // context.
502 assert(!LookupPtr && "Have a lookup map before de-serialization?");
503 StoredDeclsMap *Map = new StoredDeclsMap;
504 LookupPtr = Map;
505 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
506 (*Map)[Decls[I].Name].setFromDeclIDs(Decls[I].Declarations);
507 }
508}
509
Douglas Gregor6ab35242009-04-09 21:40:53 +0000510DeclContext::decl_iterator DeclContext::decls_begin(ASTContext &Context) const {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000511 if (hasExternalLexicalStorage())
512 LoadLexicalDeclsFromExternalStorage(Context);
513
514 // FIXME: Check whether we need to load some declarations from
515 // external storage.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000516 return decl_iterator(FirstDecl);
517}
518
519DeclContext::decl_iterator DeclContext::decls_end(ASTContext &Context) const {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000520 if (hasExternalLexicalStorage())
521 LoadLexicalDeclsFromExternalStorage(Context);
522
Douglas Gregor6ab35242009-04-09 21:40:53 +0000523 return decl_iterator();
524}
525
Douglas Gregor8038d512009-04-10 17:25:41 +0000526bool DeclContext::decls_empty(ASTContext &Context) const {
527 if (hasExternalLexicalStorage())
528 LoadLexicalDeclsFromExternalStorage(Context);
529
530 return !FirstDecl;
531}
532
Douglas Gregor6ab35242009-04-09 21:40:53 +0000533void DeclContext::addDecl(ASTContext &Context, Decl *D) {
Chris Lattner7f0be132009-02-20 00:56:18 +0000534 assert(D->getLexicalDeclContext() == this &&
535 "Decl inserted into wrong lexical context");
Chris Lattner244a67d2009-03-28 06:04:26 +0000536 assert(!D->getNextDeclInContext() && D != LastDecl &&
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000537 "Decl already inserted into a DeclContext");
538
539 if (FirstDecl) {
Chris Lattner244a67d2009-03-28 06:04:26 +0000540 LastDecl->NextDeclInContext = D;
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000541 LastDecl = D;
542 } else {
543 FirstDecl = LastDecl = D;
544 }
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000545
546 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
Douglas Gregor6ab35242009-04-09 21:40:53 +0000547 ND->getDeclContext()->makeDeclVisibleInContext(Context, ND);
Douglas Gregor44b43212008-12-11 16:49:14 +0000548}
549
Douglas Gregor074149e2009-01-05 19:45:36 +0000550/// buildLookup - Build the lookup data structure with all of the
551/// declarations in DCtx (and any other contexts linked to it or
552/// transparent contexts nested within it).
Douglas Gregor6ab35242009-04-09 21:40:53 +0000553void DeclContext::buildLookup(ASTContext &Context, DeclContext *DCtx) {
Douglas Gregor074149e2009-01-05 19:45:36 +0000554 for (; DCtx; DCtx = DCtx->getNextContext()) {
Douglas Gregor6ab35242009-04-09 21:40:53 +0000555 for (decl_iterator D = DCtx->decls_begin(Context),
556 DEnd = DCtx->decls_end(Context);
Douglas Gregor4f3b8f82009-01-06 07:17:58 +0000557 D != DEnd; ++D) {
Douglas Gregor074149e2009-01-05 19:45:36 +0000558 // Insert this declaration into the lookup structure
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000559 if (NamedDecl *ND = dyn_cast<NamedDecl>(*D))
Douglas Gregor6ab35242009-04-09 21:40:53 +0000560 makeDeclVisibleInContextImpl(Context, ND);
Douglas Gregor074149e2009-01-05 19:45:36 +0000561
562 // If this declaration is itself a transparent declaration context,
563 // add its members (recursively).
564 if (DeclContext *InnerCtx = dyn_cast<DeclContext>(*D))
565 if (InnerCtx->isTransparentContext())
Douglas Gregor6ab35242009-04-09 21:40:53 +0000566 buildLookup(Context, InnerCtx->getPrimaryContext());
Douglas Gregor074149e2009-01-05 19:45:36 +0000567 }
568 }
569}
570
Douglas Gregor44b43212008-12-11 16:49:14 +0000571DeclContext::lookup_result
Douglas Gregor6ab35242009-04-09 21:40:53 +0000572DeclContext::lookup(ASTContext &Context, DeclarationName Name) {
Steve Naroff0701bbb2009-01-08 17:28:14 +0000573 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor44b43212008-12-11 16:49:14 +0000574 if (PrimaryContext != this)
Douglas Gregor6ab35242009-04-09 21:40:53 +0000575 return PrimaryContext->lookup(Context, Name);
Douglas Gregor44b43212008-12-11 16:49:14 +0000576
Douglas Gregor2cf26342009-04-09 22:27:44 +0000577 if (hasExternalVisibleStorage())
578 LoadVisibleDeclsFromExternalStorage(Context);
579
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000580 /// If there is no lookup data structure, build one now by walking
Douglas Gregor44b43212008-12-11 16:49:14 +0000581 /// all of the linked DeclContexts (in declaration order!) and
582 /// inserting their values.
Douglas Gregorc36c5402009-04-09 17:29:08 +0000583 if (!LookupPtr) {
Douglas Gregor6ab35242009-04-09 21:40:53 +0000584 buildLookup(Context, this);
Douglas Gregor44b43212008-12-11 16:49:14 +0000585
Douglas Gregorc36c5402009-04-09 17:29:08 +0000586 if (!LookupPtr)
Chris Lattner91942502009-02-20 00:55:03 +0000587 return lookup_result(0, 0);
Douglas Gregorc36c5402009-04-09 17:29:08 +0000588 }
Douglas Gregor44b43212008-12-11 16:49:14 +0000589
Douglas Gregorc36c5402009-04-09 17:29:08 +0000590 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(LookupPtr);
591 StoredDeclsMap::iterator Pos = Map->find(Name);
592 if (Pos == Map->end())
593 return lookup_result(0, 0);
Douglas Gregor6ab35242009-04-09 21:40:53 +0000594 return Pos->second.getLookupResult(Context);
Douglas Gregor44b43212008-12-11 16:49:14 +0000595}
596
597DeclContext::lookup_const_result
Douglas Gregor6ab35242009-04-09 21:40:53 +0000598DeclContext::lookup(ASTContext &Context, DeclarationName Name) const {
599 return const_cast<DeclContext*>(this)->lookup(Context, Name);
Douglas Gregor44b43212008-12-11 16:49:14 +0000600}
601
Chris Lattner0cf2b192009-03-27 19:19:59 +0000602DeclContext *DeclContext::getLookupContext() {
603 DeclContext *Ctx = this;
Douglas Gregor72de6672009-01-08 20:45:30 +0000604 // Skip through transparent contexts.
Douglas Gregorce356072009-01-06 23:51:29 +0000605 while (Ctx->isTransparentContext())
606 Ctx = Ctx->getParent();
607 return Ctx;
608}
609
Douglas Gregor88b70942009-02-25 22:02:03 +0000610DeclContext *DeclContext::getEnclosingNamespaceContext() {
611 DeclContext *Ctx = this;
612 // Skip through non-namespace, non-translation-unit contexts.
613 while (!Ctx->isFileContext() || Ctx->isTransparentContext())
614 Ctx = Ctx->getParent();
615 return Ctx->getPrimaryContext();
616}
617
Douglas Gregor6ab35242009-04-09 21:40:53 +0000618void DeclContext::makeDeclVisibleInContext(ASTContext &Context, NamedDecl *D) {
Douglas Gregorcc636682009-02-17 23:15:12 +0000619 // FIXME: This feels like a hack. Should DeclarationName support
620 // template-ids, or is there a better way to keep specializations
621 // from being visible?
622 if (isa<ClassTemplateSpecializationDecl>(D))
623 return;
624
Steve Naroff0701bbb2009-01-08 17:28:14 +0000625 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor44b43212008-12-11 16:49:14 +0000626 if (PrimaryContext != this) {
Douglas Gregor6ab35242009-04-09 21:40:53 +0000627 PrimaryContext->makeDeclVisibleInContext(Context, D);
Douglas Gregor44b43212008-12-11 16:49:14 +0000628 return;
629 }
630
631 // If we already have a lookup data structure, perform the insertion
632 // into it. Otherwise, be lazy and don't build that structure until
633 // someone asks for it.
Douglas Gregorc36c5402009-04-09 17:29:08 +0000634 if (LookupPtr)
Douglas Gregor6ab35242009-04-09 21:40:53 +0000635 makeDeclVisibleInContextImpl(Context, D);
Douglas Gregor074149e2009-01-05 19:45:36 +0000636
Douglas Gregor074149e2009-01-05 19:45:36 +0000637 // If we are a transparent context, insert into our parent context,
638 // too. This operation is recursive.
639 if (isTransparentContext())
Douglas Gregor6ab35242009-04-09 21:40:53 +0000640 getParent()->makeDeclVisibleInContext(Context, D);
Douglas Gregor44b43212008-12-11 16:49:14 +0000641}
642
Douglas Gregor6ab35242009-04-09 21:40:53 +0000643void DeclContext::makeDeclVisibleInContextImpl(ASTContext &Context,
644 NamedDecl *D) {
Douglas Gregor074149e2009-01-05 19:45:36 +0000645 // Skip unnamed declarations.
646 if (!D->getDeclName())
647 return;
648
Douglas Gregorcc636682009-02-17 23:15:12 +0000649 // FIXME: This feels like a hack. Should DeclarationName support
650 // template-ids, or is there a better way to keep specializations
651 // from being visible?
652 if (isa<ClassTemplateSpecializationDecl>(D))
653 return;
654
Douglas Gregorc36c5402009-04-09 17:29:08 +0000655 if (!LookupPtr)
656 LookupPtr = new StoredDeclsMap;
Douglas Gregor44b43212008-12-11 16:49:14 +0000657
658 // Insert this declaration into the map.
Douglas Gregorc36c5402009-04-09 17:29:08 +0000659 StoredDeclsMap &Map = *static_cast<StoredDeclsMap*>(LookupPtr);
Chris Lattner67762a32009-02-20 01:44:05 +0000660 StoredDeclsList &DeclNameEntries = Map[D->getDeclName()];
661 if (DeclNameEntries.isNull()) {
662 DeclNameEntries.setOnlyValue(D);
Chris Lattnerbd6c8002009-02-19 07:00:44 +0000663 return;
Douglas Gregor44b43212008-12-11 16:49:14 +0000664 }
Chris Lattner91942502009-02-20 00:55:03 +0000665
Chris Lattnerbdc3d002009-02-20 01:10:07 +0000666 // If it is possible that this is a redeclaration, check to see if there is
667 // already a decl for which declarationReplaces returns true. If there is
668 // one, just replace it and return.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000669 if (DeclNameEntries.HandleRedeclaration(Context, D))
Chris Lattner67762a32009-02-20 01:44:05 +0000670 return;
Chris Lattner91942502009-02-20 00:55:03 +0000671
Chris Lattnerbd6c8002009-02-19 07:00:44 +0000672 // Put this declaration into the appropriate slot.
Chris Lattner67762a32009-02-20 01:44:05 +0000673 DeclNameEntries.AddSubsequentDecl(D);
Douglas Gregor44b43212008-12-11 16:49:14 +0000674}
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000675
676/// Returns iterator range [First, Last) of UsingDirectiveDecls stored within
677/// this context.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000678DeclContext::udir_iterator_range
679DeclContext::getUsingDirectives(ASTContext &Context) const {
680 lookup_const_result Result = lookup(Context, UsingDirectiveDecl::getName());
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000681 return udir_iterator_range(reinterpret_cast<udir_iterator>(Result.first),
682 reinterpret_cast<udir_iterator>(Result.second));
683}
Douglas Gregor2cf26342009-04-09 22:27:44 +0000684
685void StoredDeclsList::materializeDecls(ASTContext &Context) {
686 if (isNull())
687 return;
688
689 switch ((DataKind)(Data & 0x03)) {
690 case DK_Decl:
691 case DK_Decl_Vector:
692 break;
693
694 case DK_DeclID: {
695 // Resolve this declaration ID to an actual declaration by
696 // querying the external AST source.
697 unsigned DeclID = Data >> 2;
698
699 ExternalASTSource *Source = Context.getExternalSource();
700 assert(Source && "No external AST source available!");
701
702 Data = reinterpret_cast<uintptr_t>(Source->GetDecl(DeclID));
703 break;
704 }
705
706 case DK_ID_Vector: {
707 // We have a vector of declaration IDs. Resolve all of them to
708 // actual declarations.
709 VectorTy &Vector = *getAsVector();
710 ExternalASTSource *Source = Context.getExternalSource();
711 assert(Source && "No external AST source available!");
712
713 for (unsigned I = 0, N = Vector.size(); I != N; ++I)
714 Vector[I] = reinterpret_cast<uintptr_t>(Source->GetDecl(Vector[I]));
715
716 Data = (Data & ~0x03) | DK_Decl_Vector;
717 break;
718 }
719 }
720}