blob: 707383af3a8f3262734f3305b9e4bfa2d8e90007 [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"
Sebastian Redld3a413d2009-04-26 20:35:05 +000023#include "clang/AST/Stmt.h"
24#include "clang/AST/StmtCXX.h"
Eli Friedman56d29372008-06-07 16:52:53 +000025#include "llvm/ADT/DenseMap.h"
Chris Lattner49f28ca2009-03-05 08:00:35 +000026#include "llvm/Support/raw_ostream.h"
Douglas Gregor6ed40e32008-12-23 21:05:05 +000027#include <algorithm>
Chris Lattner3daed522009-03-02 22:20:04 +000028#include <cstdio>
Douglas Gregor3fc749d2008-12-23 00:26:44 +000029#include <vector>
Eli Friedman56d29372008-06-07 16:52:53 +000030using namespace clang;
31
32//===----------------------------------------------------------------------===//
33// Statistics
34//===----------------------------------------------------------------------===//
35
Douglas Gregor64650af2009-02-02 23:39:07 +000036#define DECL(Derived, Base) static int n##Derived##s = 0;
37#include "clang/AST/DeclNodes.def"
Eli Friedman56d29372008-06-07 16:52:53 +000038
39static bool StatSwitch = false;
40
41// This keeps track of all decl attributes. Since so few decls have attrs, we
42// keep them in a hash map instead of wasting space in the Decl class.
43typedef llvm::DenseMap<const Decl*, Attr*> DeclAttrMapTy;
44
45static DeclAttrMapTy *DeclAttrs = 0;
46
47const char *Decl::getDeclKindName() const {
48 switch (DeclKind) {
Douglas Gregor64650af2009-02-02 23:39:07 +000049 default: assert(0 && "Declaration not in DeclNodes.def!");
50#define DECL(Derived, Base) case Derived: return #Derived;
51#include "clang/AST/DeclNodes.def"
Eli Friedman56d29372008-06-07 16:52:53 +000052 }
53}
54
Steve Naroff0a473932009-01-20 19:53:53 +000055const char *DeclContext::getDeclKindName() const {
56 switch (DeclKind) {
Douglas Gregor64650af2009-02-02 23:39:07 +000057 default: assert(0 && "Declaration context not in DeclNodes.def!");
Argyrios Kyrtzidis1ad4dd72009-02-16 14:28:33 +000058#define DECL(Derived, Base) case Decl::Derived: return #Derived;
Douglas Gregor64650af2009-02-02 23:39:07 +000059#include "clang/AST/DeclNodes.def"
Steve Naroff0a473932009-01-20 19:53:53 +000060 }
61}
62
Eli Friedman56d29372008-06-07 16:52:53 +000063bool Decl::CollectingStats(bool Enable) {
64 if (Enable)
65 StatSwitch = true;
66 return StatSwitch;
67}
68
69void Decl::PrintStats() {
70 fprintf(stderr, "*** Decl Stats:\n");
Eli Friedman56d29372008-06-07 16:52:53 +000071
Douglas Gregor64650af2009-02-02 23:39:07 +000072 int totalDecls = 0;
73#define DECL(Derived, Base) totalDecls += n##Derived##s;
74#include "clang/AST/DeclNodes.def"
75 fprintf(stderr, " %d decls total.\n", totalDecls);
76
77 int totalBytes = 0;
78#define DECL(Derived, Base) \
79 if (n##Derived##s > 0) { \
80 totalBytes += (int)(n##Derived##s * sizeof(Derived##Decl)); \
81 fprintf(stderr, " %d " #Derived " decls, %d each (%d bytes)\n", \
82 n##Derived##s, (int)sizeof(Derived##Decl), \
83 (int)(n##Derived##s * sizeof(Derived##Decl))); \
84 }
85#include "clang/AST/DeclNodes.def"
Eli Friedman56d29372008-06-07 16:52:53 +000086
Douglas Gregor64650af2009-02-02 23:39:07 +000087 fprintf(stderr, "Total bytes = %d\n", totalBytes);
Eli Friedman56d29372008-06-07 16:52:53 +000088}
89
90void Decl::addDeclKind(Kind k) {
91 switch (k) {
Douglas Gregor64650af2009-02-02 23:39:07 +000092 default: assert(0 && "Declaration not in DeclNodes.def!");
93#define DECL(Derived, Base) case Derived: ++n##Derived##s; break;
94#include "clang/AST/DeclNodes.def"
Eli Friedman56d29372008-06-07 16:52:53 +000095 }
96}
97
98//===----------------------------------------------------------------------===//
Chris Lattner49f28ca2009-03-05 08:00:35 +000099// PrettyStackTraceDecl Implementation
100//===----------------------------------------------------------------------===//
101
102void PrettyStackTraceDecl::print(llvm::raw_ostream &OS) const {
103 SourceLocation TheLoc = Loc;
104 if (TheLoc.isInvalid() && TheDecl)
105 TheLoc = TheDecl->getLocation();
106
107 if (TheLoc.isValid()) {
108 TheLoc.print(OS, SM);
109 OS << ": ";
110 }
111
112 OS << Message;
113
114 if (NamedDecl *DN = dyn_cast_or_null<NamedDecl>(TheDecl))
115 OS << " '" << DN->getQualifiedNameAsString() << '\'';
116 OS << '\n';
117}
118
119//===----------------------------------------------------------------------===//
Eli Friedman56d29372008-06-07 16:52:53 +0000120// Decl Implementation
121//===----------------------------------------------------------------------===//
122
Chris Lattner769dbdf2009-03-27 20:18:19 +0000123// Out-of-line virtual method providing a home for Decl.
124Decl::~Decl() {
125 if (isOutOfSemaDC())
126 delete getMultipleDC();
127
128 assert(!HasAttrs && "attributes should have been freed by Destroy");
129}
130
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000131void Decl::setDeclContext(DeclContext *DC) {
132 if (isOutOfSemaDC())
133 delete getMultipleDC();
134
Chris Lattneree219fd2009-03-29 06:06:59 +0000135 DeclCtx = DC;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000136}
137
138void Decl::setLexicalDeclContext(DeclContext *DC) {
139 if (DC == getLexicalDeclContext())
140 return;
141
142 if (isInSemaDC()) {
143 MultipleDC *MDC = new MultipleDC();
144 MDC->SemanticDC = getDeclContext();
145 MDC->LexicalDC = DC;
Chris Lattneree219fd2009-03-29 06:06:59 +0000146 DeclCtx = MDC;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000147 } else {
148 getMultipleDC()->LexicalDC = DC;
149 }
150}
151
Chris Lattner769dbdf2009-03-27 20:18:19 +0000152unsigned Decl::getIdentifierNamespaceForKind(Kind DeclKind) {
153 switch (DeclKind) {
154 default:
155 if (DeclKind >= FunctionFirst && DeclKind <= FunctionLast)
156 return IDNS_Ordinary;
157 assert(0 && "Unknown decl kind!");
158 case OverloadedFunction:
159 case Typedef:
160 case EnumConstant:
161 case Var:
162 case ImplicitParam:
163 case ParmVar:
164 case OriginalParmVar:
165 case NonTypeTemplateParm:
166 case ObjCMethod:
167 case ObjCContainer:
168 case ObjCCategory:
169 case ObjCInterface:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000170 case ObjCProperty:
171 case ObjCCompatibleAlias:
172 return IDNS_Ordinary;
173
174 case ObjCProtocol:
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000175 return IDNS_ObjCProtocol;
Chris Lattner769dbdf2009-03-27 20:18:19 +0000176
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000177 case ObjCImplementation:
178 return IDNS_ObjCImplementation;
179
180 case ObjCCategoryImpl:
181 return IDNS_ObjCCategoryImpl;
182
Chris Lattner769dbdf2009-03-27 20:18:19 +0000183 case Field:
184 case ObjCAtDefsField:
185 case ObjCIvar:
186 return IDNS_Member;
187
188 case Record:
189 case CXXRecord:
190 case Enum:
191 case TemplateTypeParm:
192 return IDNS_Tag;
193
194 case Namespace:
195 case Template:
196 case FunctionTemplate:
197 case ClassTemplate:
198 case TemplateTemplateParm:
Anders Carlssonfaf0e872009-03-28 23:02:53 +0000199 case NamespaceAlias:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000200 return IDNS_Tag | IDNS_Ordinary;
201
202 // Never have names.
203 case LinkageSpec:
204 case FileScopeAsm:
205 case StaticAssert:
206 case ObjCClass:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000207 case ObjCPropertyImpl:
208 case ObjCForwardProtocol:
209 case Block:
210 case TranslationUnit:
Anders Carlsson9a55d3e2009-04-24 06:02:55 +0000211 case CXXTempVar:
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000212
Chris Lattner769dbdf2009-03-27 20:18:19 +0000213 // Aren't looked up?
214 case UsingDirective:
215 case ClassTemplateSpecialization:
216 return 0;
217 }
Eli Friedman56d29372008-06-07 16:52:53 +0000218}
219
220void Decl::addAttr(Attr *NewAttr) {
221 if (!DeclAttrs)
222 DeclAttrs = new DeclAttrMapTy();
223
224 Attr *&ExistingAttr = (*DeclAttrs)[this];
225
226 NewAttr->setNext(ExistingAttr);
227 ExistingAttr = NewAttr;
228
229 HasAttrs = true;
230}
231
232void Decl::invalidateAttrs() {
233 if (!HasAttrs) return;
234
235 HasAttrs = false;
236 (*DeclAttrs)[this] = 0;
237 DeclAttrs->erase(this);
238
239 if (DeclAttrs->empty()) {
240 delete DeclAttrs;
241 DeclAttrs = 0;
242 }
243}
244
Chris Lattner81abbdd2009-03-21 06:27:31 +0000245const Attr *Decl::getAttrsImpl() const {
246 assert(HasAttrs && "getAttrs() should verify this!");
Eli Friedman56d29372008-06-07 16:52:53 +0000247 return (*DeclAttrs)[this];
248}
249
250void Decl::swapAttrs(Decl *RHS) {
251 bool HasLHSAttr = this->HasAttrs;
252 bool HasRHSAttr = RHS->HasAttrs;
253
254 // Usually, neither decl has attrs, nothing to do.
255 if (!HasLHSAttr && !HasRHSAttr) return;
256
257 // If 'this' has no attrs, swap the other way.
258 if (!HasLHSAttr)
259 return RHS->swapAttrs(this);
260
261 // Handle the case when both decls have attrs.
262 if (HasRHSAttr) {
263 std::swap((*DeclAttrs)[this], (*DeclAttrs)[RHS]);
264 return;
265 }
266
267 // Otherwise, LHS has an attr and RHS doesn't.
268 (*DeclAttrs)[RHS] = (*DeclAttrs)[this];
269 (*DeclAttrs).erase(this);
270 this->HasAttrs = false;
271 RHS->HasAttrs = true;
272}
273
274
Chris Lattnercc581472009-03-04 06:05:19 +0000275void Decl::Destroy(ASTContext &C) {
276 // Free attributes for this decl.
277 if (HasAttrs) {
278 DeclAttrMapTy::iterator it = DeclAttrs->find(this);
279 assert(it != DeclAttrs->end() && "No attrs found but HasAttrs is true!");
280
281 // release attributes.
282 it->second->Destroy(C);
283 invalidateAttrs();
284 HasAttrs = false;
285 }
286
Douglas Gregora0fc55f2009-01-13 19:47:12 +0000287#if 0
Douglas Gregor00ad0ef2009-01-20 04:25:11 +0000288 // FIXME: Once ownership is fully understood, we can enable this code
289 if (DeclContext *DC = dyn_cast<DeclContext>(this))
290 DC->decls_begin()->Destroy(C);
Eli Friedman56d29372008-06-07 16:52:53 +0000291
Chris Lattner244a67d2009-03-28 06:04:26 +0000292 // Observe the unrolled recursion. By setting N->NextDeclInContext = 0x0
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000293 // within the loop, only the Destroy method for the first Decl
294 // will deallocate all of the Decls in a chain.
295
Chris Lattner244a67d2009-03-28 06:04:26 +0000296 Decl* N = getNextDeclInContext();
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000297
298 while (N) {
Chris Lattner244a67d2009-03-28 06:04:26 +0000299 Decl* Tmp = N->getNextDeclInContext();
300 N->NextDeclInContext = 0;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000301 N->Destroy(C);
302 N = Tmp;
Eli Friedman56d29372008-06-07 16:52:53 +0000303 }
Douglas Gregora0fc55f2009-01-13 19:47:12 +0000304
Eli Friedman56d29372008-06-07 16:52:53 +0000305 this->~Decl();
Steve Naroff3e970492009-01-27 21:25:57 +0000306 C.Deallocate((void *)this);
Douglas Gregor00ad0ef2009-01-20 04:25:11 +0000307#endif
Eli Friedman56d29372008-06-07 16:52:53 +0000308}
309
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000310Decl *Decl::castFromDeclContext (const DeclContext *D) {
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000311 Decl::Kind DK = D->getDeclKind();
312 switch(DK) {
313#define DECL_CONTEXT(Name) \
314 case Decl::Name: \
315 return static_cast<Name##Decl*>(const_cast<DeclContext*>(D));
316#define DECL_CONTEXT_BASE(Name)
317#include "clang/AST/DeclNodes.def"
318 default:
319#define DECL_CONTEXT_BASE(Name) \
320 if (DK >= Decl::Name##First && DK <= Decl::Name##Last) \
321 return static_cast<Name##Decl*>(const_cast<DeclContext*>(D));
322#include "clang/AST/DeclNodes.def"
323 assert(false && "a decl that inherits DeclContext isn't handled");
324 return 0;
325 }
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000326}
327
328DeclContext *Decl::castToDeclContext(const Decl *D) {
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000329 Decl::Kind DK = D->getKind();
330 switch(DK) {
331#define DECL_CONTEXT(Name) \
332 case Decl::Name: \
333 return static_cast<Name##Decl*>(const_cast<Decl*>(D));
334#define DECL_CONTEXT_BASE(Name)
335#include "clang/AST/DeclNodes.def"
336 default:
337#define DECL_CONTEXT_BASE(Name) \
338 if (DK >= Decl::Name##First && DK <= Decl::Name##Last) \
339 return static_cast<Name##Decl*>(const_cast<Decl*>(D));
340#include "clang/AST/DeclNodes.def"
341 assert(false && "a decl that inherits DeclContext isn't handled");
342 return 0;
343 }
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000344}
345
Sebastian Redld3a413d2009-04-26 20:35:05 +0000346CompoundStmt* Decl::getCompoundBody(ASTContext &Context) const {
347 return dyn_cast_or_null<CompoundStmt>(getBody(Context));
348}
349
350SourceLocation Decl::getBodyRBrace(ASTContext &Context) const {
351 Stmt *Body = getBody(Context);
352 if (!Body)
353 return SourceLocation();
354 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Body))
355 return CS->getRBracLoc();
356 assert(isa<CXXTryStmt>(Body) &&
357 "Body can only be CompoundStmt or CXXTryStmt");
358 return cast<CXXTryStmt>(Body)->getSourceRange().getEnd();
359}
360
Anders Carlsson1329c272009-03-25 23:38:06 +0000361#ifndef NDEBUG
362void Decl::CheckAccessDeclContext() const {
Douglas Gregor5c27f2b2009-04-07 20:58:25 +0000363 assert((Access != AS_none || isa<TranslationUnitDecl>(this) ||
364 !isa<CXXRecordDecl>(getDeclContext())) &&
Anders Carlsson1329c272009-03-25 23:38:06 +0000365 "Access specifier is AS_none inside a record decl");
366}
367
368#endif
369
Eli Friedman56d29372008-06-07 16:52:53 +0000370//===----------------------------------------------------------------------===//
371// DeclContext Implementation
372//===----------------------------------------------------------------------===//
373
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000374bool DeclContext::classof(const Decl *D) {
375 switch (D->getKind()) {
376#define DECL_CONTEXT(Name) case Decl::Name:
377#define DECL_CONTEXT_BASE(Name)
378#include "clang/AST/DeclNodes.def"
379 return true;
380 default:
381#define DECL_CONTEXT_BASE(Name) \
382 if (D->getKind() >= Decl::Name##First && \
383 D->getKind() <= Decl::Name##Last) \
384 return true;
385#include "clang/AST/DeclNodes.def"
386 return false;
387 }
388}
389
Douglas Gregor44b43212008-12-11 16:49:14 +0000390DeclContext::~DeclContext() {
Douglas Gregorc36c5402009-04-09 17:29:08 +0000391 delete static_cast<StoredDeclsMap*>(LookupPtr);
Douglas Gregor44b43212008-12-11 16:49:14 +0000392}
393
394void DeclContext::DestroyDecls(ASTContext &C) {
Douglas Gregor6ab35242009-04-09 21:40:53 +0000395 for (decl_iterator D = decls_begin(C); D != decls_end(C); )
Douglas Gregor00ad0ef2009-01-20 04:25:11 +0000396 (*D++)->Destroy(C);
Douglas Gregor44b43212008-12-11 16:49:14 +0000397}
398
Douglas Gregor074149e2009-01-05 19:45:36 +0000399bool DeclContext::isTransparentContext() const {
400 if (DeclKind == Decl::Enum)
401 return true; // FIXME: Check for C++0x scoped enums
402 else if (DeclKind == Decl::LinkageSpec)
403 return true;
Douglas Gregor65100792009-02-26 00:02:51 +0000404 else if (DeclKind >= Decl::RecordFirst && DeclKind <= Decl::RecordLast)
Douglas Gregorbcbffc42009-01-07 00:43:41 +0000405 return cast<RecordDecl>(this)->isAnonymousStructOrUnion();
Douglas Gregor074149e2009-01-05 19:45:36 +0000406 else if (DeclKind == Decl::Namespace)
407 return false; // FIXME: Check for C++0x inline namespaces
408
409 return false;
410}
411
Steve Naroff0701bbb2009-01-08 17:28:14 +0000412DeclContext *DeclContext::getPrimaryContext() {
Douglas Gregor44b43212008-12-11 16:49:14 +0000413 switch (DeclKind) {
Douglas Gregor44b43212008-12-11 16:49:14 +0000414 case Decl::TranslationUnit:
Douglas Gregor074149e2009-01-05 19:45:36 +0000415 case Decl::LinkageSpec:
416 case Decl::Block:
Douglas Gregor44b43212008-12-11 16:49:14 +0000417 // There is only one DeclContext for these entities.
418 return this;
419
420 case Decl::Namespace:
421 // The original namespace is our primary context.
422 return static_cast<NamespaceDecl*>(this)->getOriginalNamespace();
423
Douglas Gregor44b43212008-12-11 16:49:14 +0000424 case Decl::ObjCMethod:
425 return this;
426
427 case Decl::ObjCInterface:
Steve Naroff0701bbb2009-01-08 17:28:14 +0000428 case Decl::ObjCProtocol:
429 case Decl::ObjCCategory:
Douglas Gregor44b43212008-12-11 16:49:14 +0000430 // FIXME: Can Objective-C interfaces be forward-declared?
431 return this;
432
Steve Naroff0701bbb2009-01-08 17:28:14 +0000433 case Decl::ObjCImplementation:
434 case Decl::ObjCCategoryImpl:
435 return this;
436
Douglas Gregor44b43212008-12-11 16:49:14 +0000437 default:
Douglas Gregorcc636682009-02-17 23:15:12 +0000438 if (DeclKind >= Decl::TagFirst && DeclKind <= Decl::TagLast) {
439 // If this is a tag type that has a definition or is currently
440 // being defined, that definition is our primary context.
Chris Lattner244a67d2009-03-28 06:04:26 +0000441 if (const TagType *TagT =cast<TagDecl>(this)->TypeForDecl->getAsTagType())
Douglas Gregorcc636682009-02-17 23:15:12 +0000442 if (TagT->isBeingDefined() ||
443 (TagT->getDecl() && TagT->getDecl()->isDefinition()))
444 return TagT->getDecl();
445 return this;
446 }
447
Douglas Gregor44b43212008-12-11 16:49:14 +0000448 assert(DeclKind >= Decl::FunctionFirst && DeclKind <= Decl::FunctionLast &&
449 "Unknown DeclContext kind");
450 return this;
451 }
452}
453
454DeclContext *DeclContext::getNextContext() {
455 switch (DeclKind) {
Douglas Gregor44b43212008-12-11 16:49:14 +0000456 case Decl::Namespace:
457 // Return the next namespace
458 return static_cast<NamespaceDecl*>(this)->getNextNamespace();
459
460 default:
Douglas Gregor44b43212008-12-11 16:49:14 +0000461 return 0;
462 }
463}
464
Douglas Gregor2cf26342009-04-09 22:27:44 +0000465/// \brief Load the declarations within this lexical storage from an
466/// external source.
467void
468DeclContext::LoadLexicalDeclsFromExternalStorage(ASTContext &Context) const {
469 ExternalASTSource *Source = Context.getExternalSource();
470 assert(hasExternalLexicalStorage() && Source && "No external storage?");
471
472 llvm::SmallVector<unsigned, 64> Decls;
473 if (Source->ReadDeclsLexicallyInContext(const_cast<DeclContext *>(this),
474 Decls))
475 return;
476
477 // There is no longer any lexical storage in this context
478 ExternalLexicalStorage = false;
479
480 if (Decls.empty())
481 return;
482
483 // Resolve all of the declaration IDs into declarations, building up
484 // a chain of declarations via the Decl::NextDeclInContext field.
485 Decl *FirstNewDecl = 0;
486 Decl *PrevDecl = 0;
487 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
488 Decl *D = Source->GetDecl(Decls[I]);
489 if (PrevDecl)
490 PrevDecl->NextDeclInContext = D;
491 else
492 FirstNewDecl = D;
493
494 PrevDecl = D;
495 }
496
497 // Splice the newly-read declarations into the beginning of the list
498 // of declarations.
499 PrevDecl->NextDeclInContext = FirstDecl;
500 FirstDecl = FirstNewDecl;
501 if (!LastDecl)
502 LastDecl = PrevDecl;
503}
504
505void
506DeclContext::LoadVisibleDeclsFromExternalStorage(ASTContext &Context) const {
507 DeclContext *This = const_cast<DeclContext *>(this);
508 ExternalASTSource *Source = Context.getExternalSource();
509 assert(hasExternalVisibleStorage() && Source && "No external storage?");
510
511 llvm::SmallVector<VisibleDeclaration, 64> Decls;
512 if (Source->ReadDeclsVisibleInContext(This, Decls))
513 return;
514
515 // There is no longer any visible storage in this context
516 ExternalVisibleStorage = false;
517
518 // Load the declaration IDs for all of the names visible in this
519 // context.
520 assert(!LookupPtr && "Have a lookup map before de-serialization?");
521 StoredDeclsMap *Map = new StoredDeclsMap;
522 LookupPtr = Map;
523 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
524 (*Map)[Decls[I].Name].setFromDeclIDs(Decls[I].Declarations);
525 }
526}
527
Douglas Gregor6ab35242009-04-09 21:40:53 +0000528DeclContext::decl_iterator DeclContext::decls_begin(ASTContext &Context) const {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000529 if (hasExternalLexicalStorage())
530 LoadLexicalDeclsFromExternalStorage(Context);
531
532 // FIXME: Check whether we need to load some declarations from
533 // external storage.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000534 return decl_iterator(FirstDecl);
535}
536
537DeclContext::decl_iterator DeclContext::decls_end(ASTContext &Context) const {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000538 if (hasExternalLexicalStorage())
539 LoadLexicalDeclsFromExternalStorage(Context);
540
Douglas Gregor6ab35242009-04-09 21:40:53 +0000541 return decl_iterator();
542}
543
Douglas Gregor8038d512009-04-10 17:25:41 +0000544bool DeclContext::decls_empty(ASTContext &Context) const {
545 if (hasExternalLexicalStorage())
546 LoadLexicalDeclsFromExternalStorage(Context);
547
548 return !FirstDecl;
549}
550
Douglas Gregor6ab35242009-04-09 21:40:53 +0000551void DeclContext::addDecl(ASTContext &Context, Decl *D) {
Chris Lattner7f0be132009-02-20 00:56:18 +0000552 assert(D->getLexicalDeclContext() == this &&
553 "Decl inserted into wrong lexical context");
Chris Lattner244a67d2009-03-28 06:04:26 +0000554 assert(!D->getNextDeclInContext() && D != LastDecl &&
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000555 "Decl already inserted into a DeclContext");
556
557 if (FirstDecl) {
Chris Lattner244a67d2009-03-28 06:04:26 +0000558 LastDecl->NextDeclInContext = D;
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000559 LastDecl = D;
560 } else {
561 FirstDecl = LastDecl = D;
562 }
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000563
564 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
Douglas Gregor6ab35242009-04-09 21:40:53 +0000565 ND->getDeclContext()->makeDeclVisibleInContext(Context, ND);
Douglas Gregor44b43212008-12-11 16:49:14 +0000566}
567
Douglas Gregor074149e2009-01-05 19:45:36 +0000568/// buildLookup - Build the lookup data structure with all of the
569/// declarations in DCtx (and any other contexts linked to it or
570/// transparent contexts nested within it).
Douglas Gregor6ab35242009-04-09 21:40:53 +0000571void DeclContext::buildLookup(ASTContext &Context, DeclContext *DCtx) {
Douglas Gregor074149e2009-01-05 19:45:36 +0000572 for (; DCtx; DCtx = DCtx->getNextContext()) {
Douglas Gregor6ab35242009-04-09 21:40:53 +0000573 for (decl_iterator D = DCtx->decls_begin(Context),
574 DEnd = DCtx->decls_end(Context);
Douglas Gregor4f3b8f82009-01-06 07:17:58 +0000575 D != DEnd; ++D) {
Douglas Gregor074149e2009-01-05 19:45:36 +0000576 // Insert this declaration into the lookup structure
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000577 if (NamedDecl *ND = dyn_cast<NamedDecl>(*D))
Douglas Gregor6ab35242009-04-09 21:40:53 +0000578 makeDeclVisibleInContextImpl(Context, ND);
Douglas Gregor074149e2009-01-05 19:45:36 +0000579
580 // If this declaration is itself a transparent declaration context,
581 // add its members (recursively).
582 if (DeclContext *InnerCtx = dyn_cast<DeclContext>(*D))
583 if (InnerCtx->isTransparentContext())
Douglas Gregor6ab35242009-04-09 21:40:53 +0000584 buildLookup(Context, InnerCtx->getPrimaryContext());
Douglas Gregor074149e2009-01-05 19:45:36 +0000585 }
586 }
587}
588
Douglas Gregor44b43212008-12-11 16:49:14 +0000589DeclContext::lookup_result
Douglas Gregor6ab35242009-04-09 21:40:53 +0000590DeclContext::lookup(ASTContext &Context, DeclarationName Name) {
Steve Naroff0701bbb2009-01-08 17:28:14 +0000591 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor44b43212008-12-11 16:49:14 +0000592 if (PrimaryContext != this)
Douglas Gregor6ab35242009-04-09 21:40:53 +0000593 return PrimaryContext->lookup(Context, Name);
Douglas Gregor44b43212008-12-11 16:49:14 +0000594
Douglas Gregor2cf26342009-04-09 22:27:44 +0000595 if (hasExternalVisibleStorage())
596 LoadVisibleDeclsFromExternalStorage(Context);
597
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000598 /// If there is no lookup data structure, build one now by walking
Douglas Gregor44b43212008-12-11 16:49:14 +0000599 /// all of the linked DeclContexts (in declaration order!) and
600 /// inserting their values.
Douglas Gregorc36c5402009-04-09 17:29:08 +0000601 if (!LookupPtr) {
Douglas Gregor6ab35242009-04-09 21:40:53 +0000602 buildLookup(Context, this);
Douglas Gregor44b43212008-12-11 16:49:14 +0000603
Douglas Gregorc36c5402009-04-09 17:29:08 +0000604 if (!LookupPtr)
Chris Lattner91942502009-02-20 00:55:03 +0000605 return lookup_result(0, 0);
Douglas Gregorc36c5402009-04-09 17:29:08 +0000606 }
Douglas Gregor44b43212008-12-11 16:49:14 +0000607
Douglas Gregorc36c5402009-04-09 17:29:08 +0000608 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(LookupPtr);
609 StoredDeclsMap::iterator Pos = Map->find(Name);
610 if (Pos == Map->end())
611 return lookup_result(0, 0);
Douglas Gregor6ab35242009-04-09 21:40:53 +0000612 return Pos->second.getLookupResult(Context);
Douglas Gregor44b43212008-12-11 16:49:14 +0000613}
614
615DeclContext::lookup_const_result
Douglas Gregor6ab35242009-04-09 21:40:53 +0000616DeclContext::lookup(ASTContext &Context, DeclarationName Name) const {
617 return const_cast<DeclContext*>(this)->lookup(Context, Name);
Douglas Gregor44b43212008-12-11 16:49:14 +0000618}
619
Chris Lattner0cf2b192009-03-27 19:19:59 +0000620DeclContext *DeclContext::getLookupContext() {
621 DeclContext *Ctx = this;
Douglas Gregor72de6672009-01-08 20:45:30 +0000622 // Skip through transparent contexts.
Douglas Gregorce356072009-01-06 23:51:29 +0000623 while (Ctx->isTransparentContext())
624 Ctx = Ctx->getParent();
625 return Ctx;
626}
627
Douglas Gregor88b70942009-02-25 22:02:03 +0000628DeclContext *DeclContext::getEnclosingNamespaceContext() {
629 DeclContext *Ctx = this;
630 // Skip through non-namespace, non-translation-unit contexts.
631 while (!Ctx->isFileContext() || Ctx->isTransparentContext())
632 Ctx = Ctx->getParent();
633 return Ctx->getPrimaryContext();
634}
635
Douglas Gregor6ab35242009-04-09 21:40:53 +0000636void DeclContext::makeDeclVisibleInContext(ASTContext &Context, NamedDecl *D) {
Douglas Gregorcc636682009-02-17 23:15:12 +0000637 // FIXME: This feels like a hack. Should DeclarationName support
638 // template-ids, or is there a better way to keep specializations
639 // from being visible?
640 if (isa<ClassTemplateSpecializationDecl>(D))
641 return;
642
Steve Naroff0701bbb2009-01-08 17:28:14 +0000643 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor44b43212008-12-11 16:49:14 +0000644 if (PrimaryContext != this) {
Douglas Gregor6ab35242009-04-09 21:40:53 +0000645 PrimaryContext->makeDeclVisibleInContext(Context, D);
Douglas Gregor44b43212008-12-11 16:49:14 +0000646 return;
647 }
648
649 // If we already have a lookup data structure, perform the insertion
650 // into it. Otherwise, be lazy and don't build that structure until
651 // someone asks for it.
Douglas Gregorc36c5402009-04-09 17:29:08 +0000652 if (LookupPtr)
Douglas Gregor6ab35242009-04-09 21:40:53 +0000653 makeDeclVisibleInContextImpl(Context, D);
Douglas Gregor074149e2009-01-05 19:45:36 +0000654
Douglas Gregor074149e2009-01-05 19:45:36 +0000655 // If we are a transparent context, insert into our parent context,
656 // too. This operation is recursive.
657 if (isTransparentContext())
Douglas Gregor6ab35242009-04-09 21:40:53 +0000658 getParent()->makeDeclVisibleInContext(Context, D);
Douglas Gregor44b43212008-12-11 16:49:14 +0000659}
660
Douglas Gregor6ab35242009-04-09 21:40:53 +0000661void DeclContext::makeDeclVisibleInContextImpl(ASTContext &Context,
662 NamedDecl *D) {
Douglas Gregor074149e2009-01-05 19:45:36 +0000663 // Skip unnamed declarations.
664 if (!D->getDeclName())
665 return;
666
Douglas Gregorcc636682009-02-17 23:15:12 +0000667 // FIXME: This feels like a hack. Should DeclarationName support
668 // template-ids, or is there a better way to keep specializations
669 // from being visible?
670 if (isa<ClassTemplateSpecializationDecl>(D))
671 return;
672
Douglas Gregorc36c5402009-04-09 17:29:08 +0000673 if (!LookupPtr)
674 LookupPtr = new StoredDeclsMap;
Douglas Gregor44b43212008-12-11 16:49:14 +0000675
676 // Insert this declaration into the map.
Douglas Gregorc36c5402009-04-09 17:29:08 +0000677 StoredDeclsMap &Map = *static_cast<StoredDeclsMap*>(LookupPtr);
Chris Lattner67762a32009-02-20 01:44:05 +0000678 StoredDeclsList &DeclNameEntries = Map[D->getDeclName()];
679 if (DeclNameEntries.isNull()) {
680 DeclNameEntries.setOnlyValue(D);
Chris Lattnerbd6c8002009-02-19 07:00:44 +0000681 return;
Douglas Gregor44b43212008-12-11 16:49:14 +0000682 }
Chris Lattner91942502009-02-20 00:55:03 +0000683
Chris Lattnerbdc3d002009-02-20 01:10:07 +0000684 // If it is possible that this is a redeclaration, check to see if there is
685 // already a decl for which declarationReplaces returns true. If there is
686 // one, just replace it and return.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000687 if (DeclNameEntries.HandleRedeclaration(Context, D))
Chris Lattner67762a32009-02-20 01:44:05 +0000688 return;
Chris Lattner91942502009-02-20 00:55:03 +0000689
Chris Lattnerbd6c8002009-02-19 07:00:44 +0000690 // Put this declaration into the appropriate slot.
Chris Lattner67762a32009-02-20 01:44:05 +0000691 DeclNameEntries.AddSubsequentDecl(D);
Douglas Gregor44b43212008-12-11 16:49:14 +0000692}
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000693
694/// Returns iterator range [First, Last) of UsingDirectiveDecls stored within
695/// this context.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000696DeclContext::udir_iterator_range
697DeclContext::getUsingDirectives(ASTContext &Context) const {
698 lookup_const_result Result = lookup(Context, UsingDirectiveDecl::getName());
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000699 return udir_iterator_range(reinterpret_cast<udir_iterator>(Result.first),
700 reinterpret_cast<udir_iterator>(Result.second));
701}
Douglas Gregor2cf26342009-04-09 22:27:44 +0000702
703void StoredDeclsList::materializeDecls(ASTContext &Context) {
704 if (isNull())
705 return;
706
707 switch ((DataKind)(Data & 0x03)) {
708 case DK_Decl:
709 case DK_Decl_Vector:
710 break;
711
712 case DK_DeclID: {
713 // Resolve this declaration ID to an actual declaration by
714 // querying the external AST source.
715 unsigned DeclID = Data >> 2;
716
717 ExternalASTSource *Source = Context.getExternalSource();
718 assert(Source && "No external AST source available!");
719
720 Data = reinterpret_cast<uintptr_t>(Source->GetDecl(DeclID));
721 break;
722 }
723
724 case DK_ID_Vector: {
725 // We have a vector of declaration IDs. Resolve all of them to
726 // actual declarations.
727 VectorTy &Vector = *getAsVector();
728 ExternalASTSource *Source = Context.getExternalSource();
729 assert(Source && "No external AST source available!");
730
731 for (unsigned I = 0, N = Vector.size(); I != N; ++I)
732 Vector[I] = reinterpret_cast<uintptr_t>(Source->GetDecl(Vector[I]));
733
734 Data = (Data & ~0x03) | DK_Decl_Vector;
735 break;
736 }
737 }
738}