blob: ba8f3351c3a9c7ec92de14583eb19de8aefd9ec2 [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 Gregorbc221632009-05-28 16:34:51 +0000399bool DeclContext::isDependentContext() const {
400 if (isFileContext())
401 return false;
402
403 if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this))
404 if (Record->getDescribedClassTemplate())
405 return true;
406
407 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this))
408 if (Function->getDescribedFunctionTemplate())
409 return true;
410
411 return getParent() && getParent()->isDependentContext();
412}
413
Douglas Gregor074149e2009-01-05 19:45:36 +0000414bool DeclContext::isTransparentContext() const {
415 if (DeclKind == Decl::Enum)
416 return true; // FIXME: Check for C++0x scoped enums
417 else if (DeclKind == Decl::LinkageSpec)
418 return true;
Douglas Gregor65100792009-02-26 00:02:51 +0000419 else if (DeclKind >= Decl::RecordFirst && DeclKind <= Decl::RecordLast)
Douglas Gregorbcbffc42009-01-07 00:43:41 +0000420 return cast<RecordDecl>(this)->isAnonymousStructOrUnion();
Douglas Gregor074149e2009-01-05 19:45:36 +0000421 else if (DeclKind == Decl::Namespace)
422 return false; // FIXME: Check for C++0x inline namespaces
423
424 return false;
425}
426
Steve Naroff0701bbb2009-01-08 17:28:14 +0000427DeclContext *DeclContext::getPrimaryContext() {
Douglas Gregor44b43212008-12-11 16:49:14 +0000428 switch (DeclKind) {
Douglas Gregor44b43212008-12-11 16:49:14 +0000429 case Decl::TranslationUnit:
Douglas Gregor074149e2009-01-05 19:45:36 +0000430 case Decl::LinkageSpec:
431 case Decl::Block:
Douglas Gregor44b43212008-12-11 16:49:14 +0000432 // There is only one DeclContext for these entities.
433 return this;
434
435 case Decl::Namespace:
436 // The original namespace is our primary context.
437 return static_cast<NamespaceDecl*>(this)->getOriginalNamespace();
438
Douglas Gregor44b43212008-12-11 16:49:14 +0000439 case Decl::ObjCMethod:
440 return this;
441
442 case Decl::ObjCInterface:
Steve Naroff0701bbb2009-01-08 17:28:14 +0000443 case Decl::ObjCProtocol:
444 case Decl::ObjCCategory:
Douglas Gregor44b43212008-12-11 16:49:14 +0000445 // FIXME: Can Objective-C interfaces be forward-declared?
446 return this;
447
Steve Naroff0701bbb2009-01-08 17:28:14 +0000448 case Decl::ObjCImplementation:
449 case Decl::ObjCCategoryImpl:
450 return this;
451
Douglas Gregor44b43212008-12-11 16:49:14 +0000452 default:
Douglas Gregorcc636682009-02-17 23:15:12 +0000453 if (DeclKind >= Decl::TagFirst && DeclKind <= Decl::TagLast) {
454 // If this is a tag type that has a definition or is currently
455 // being defined, that definition is our primary context.
Chris Lattner244a67d2009-03-28 06:04:26 +0000456 if (const TagType *TagT =cast<TagDecl>(this)->TypeForDecl->getAsTagType())
Douglas Gregorcc636682009-02-17 23:15:12 +0000457 if (TagT->isBeingDefined() ||
458 (TagT->getDecl() && TagT->getDecl()->isDefinition()))
459 return TagT->getDecl();
460 return this;
461 }
462
Douglas Gregor44b43212008-12-11 16:49:14 +0000463 assert(DeclKind >= Decl::FunctionFirst && DeclKind <= Decl::FunctionLast &&
464 "Unknown DeclContext kind");
465 return this;
466 }
467}
468
469DeclContext *DeclContext::getNextContext() {
470 switch (DeclKind) {
Douglas Gregor44b43212008-12-11 16:49:14 +0000471 case Decl::Namespace:
472 // Return the next namespace
473 return static_cast<NamespaceDecl*>(this)->getNextNamespace();
474
475 default:
Douglas Gregor44b43212008-12-11 16:49:14 +0000476 return 0;
477 }
478}
479
Douglas Gregor2cf26342009-04-09 22:27:44 +0000480/// \brief Load the declarations within this lexical storage from an
481/// external source.
482void
483DeclContext::LoadLexicalDeclsFromExternalStorage(ASTContext &Context) const {
484 ExternalASTSource *Source = Context.getExternalSource();
485 assert(hasExternalLexicalStorage() && Source && "No external storage?");
486
Eli Friedmanb0156ea2009-04-27 23:43:36 +0000487 llvm::SmallVector<uint32_t, 64> Decls;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000488 if (Source->ReadDeclsLexicallyInContext(const_cast<DeclContext *>(this),
489 Decls))
490 return;
491
492 // There is no longer any lexical storage in this context
493 ExternalLexicalStorage = false;
494
495 if (Decls.empty())
496 return;
497
498 // Resolve all of the declaration IDs into declarations, building up
499 // a chain of declarations via the Decl::NextDeclInContext field.
500 Decl *FirstNewDecl = 0;
501 Decl *PrevDecl = 0;
502 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
503 Decl *D = Source->GetDecl(Decls[I]);
504 if (PrevDecl)
505 PrevDecl->NextDeclInContext = D;
506 else
507 FirstNewDecl = D;
508
509 PrevDecl = D;
510 }
511
512 // Splice the newly-read declarations into the beginning of the list
513 // of declarations.
514 PrevDecl->NextDeclInContext = FirstDecl;
515 FirstDecl = FirstNewDecl;
516 if (!LastDecl)
517 LastDecl = PrevDecl;
518}
519
520void
521DeclContext::LoadVisibleDeclsFromExternalStorage(ASTContext &Context) const {
522 DeclContext *This = const_cast<DeclContext *>(this);
523 ExternalASTSource *Source = Context.getExternalSource();
524 assert(hasExternalVisibleStorage() && Source && "No external storage?");
525
526 llvm::SmallVector<VisibleDeclaration, 64> Decls;
527 if (Source->ReadDeclsVisibleInContext(This, Decls))
528 return;
529
530 // There is no longer any visible storage in this context
531 ExternalVisibleStorage = false;
532
533 // Load the declaration IDs for all of the names visible in this
534 // context.
535 assert(!LookupPtr && "Have a lookup map before de-serialization?");
536 StoredDeclsMap *Map = new StoredDeclsMap;
537 LookupPtr = Map;
538 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
539 (*Map)[Decls[I].Name].setFromDeclIDs(Decls[I].Declarations);
540 }
541}
542
Douglas Gregor6ab35242009-04-09 21:40:53 +0000543DeclContext::decl_iterator DeclContext::decls_begin(ASTContext &Context) const {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000544 if (hasExternalLexicalStorage())
545 LoadLexicalDeclsFromExternalStorage(Context);
546
547 // FIXME: Check whether we need to load some declarations from
548 // external storage.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000549 return decl_iterator(FirstDecl);
550}
551
552DeclContext::decl_iterator DeclContext::decls_end(ASTContext &Context) const {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000553 if (hasExternalLexicalStorage())
554 LoadLexicalDeclsFromExternalStorage(Context);
555
Douglas Gregor6ab35242009-04-09 21:40:53 +0000556 return decl_iterator();
557}
558
Douglas Gregor8038d512009-04-10 17:25:41 +0000559bool DeclContext::decls_empty(ASTContext &Context) const {
560 if (hasExternalLexicalStorage())
561 LoadLexicalDeclsFromExternalStorage(Context);
562
563 return !FirstDecl;
564}
565
Douglas Gregor6ab35242009-04-09 21:40:53 +0000566void DeclContext::addDecl(ASTContext &Context, Decl *D) {
Chris Lattner7f0be132009-02-20 00:56:18 +0000567 assert(D->getLexicalDeclContext() == this &&
568 "Decl inserted into wrong lexical context");
Chris Lattner244a67d2009-03-28 06:04:26 +0000569 assert(!D->getNextDeclInContext() && D != LastDecl &&
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000570 "Decl already inserted into a DeclContext");
571
572 if (FirstDecl) {
Chris Lattner244a67d2009-03-28 06:04:26 +0000573 LastDecl->NextDeclInContext = D;
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000574 LastDecl = D;
575 } else {
576 FirstDecl = LastDecl = D;
577 }
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000578
579 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
Douglas Gregor6ab35242009-04-09 21:40:53 +0000580 ND->getDeclContext()->makeDeclVisibleInContext(Context, ND);
Douglas Gregor44b43212008-12-11 16:49:14 +0000581}
582
Douglas Gregor074149e2009-01-05 19:45:36 +0000583/// buildLookup - Build the lookup data structure with all of the
584/// declarations in DCtx (and any other contexts linked to it or
585/// transparent contexts nested within it).
Douglas Gregor6ab35242009-04-09 21:40:53 +0000586void DeclContext::buildLookup(ASTContext &Context, DeclContext *DCtx) {
Douglas Gregor074149e2009-01-05 19:45:36 +0000587 for (; DCtx; DCtx = DCtx->getNextContext()) {
Douglas Gregor6ab35242009-04-09 21:40:53 +0000588 for (decl_iterator D = DCtx->decls_begin(Context),
589 DEnd = DCtx->decls_end(Context);
Douglas Gregor4f3b8f82009-01-06 07:17:58 +0000590 D != DEnd; ++D) {
Douglas Gregor074149e2009-01-05 19:45:36 +0000591 // Insert this declaration into the lookup structure
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000592 if (NamedDecl *ND = dyn_cast<NamedDecl>(*D))
Douglas Gregor6ab35242009-04-09 21:40:53 +0000593 makeDeclVisibleInContextImpl(Context, ND);
Douglas Gregor074149e2009-01-05 19:45:36 +0000594
595 // If this declaration is itself a transparent declaration context,
596 // add its members (recursively).
597 if (DeclContext *InnerCtx = dyn_cast<DeclContext>(*D))
598 if (InnerCtx->isTransparentContext())
Douglas Gregor6ab35242009-04-09 21:40:53 +0000599 buildLookup(Context, InnerCtx->getPrimaryContext());
Douglas Gregor074149e2009-01-05 19:45:36 +0000600 }
601 }
602}
603
Douglas Gregor44b43212008-12-11 16:49:14 +0000604DeclContext::lookup_result
Douglas Gregor6ab35242009-04-09 21:40:53 +0000605DeclContext::lookup(ASTContext &Context, DeclarationName Name) {
Steve Naroff0701bbb2009-01-08 17:28:14 +0000606 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor44b43212008-12-11 16:49:14 +0000607 if (PrimaryContext != this)
Douglas Gregor6ab35242009-04-09 21:40:53 +0000608 return PrimaryContext->lookup(Context, Name);
Douglas Gregor44b43212008-12-11 16:49:14 +0000609
Douglas Gregor2cf26342009-04-09 22:27:44 +0000610 if (hasExternalVisibleStorage())
611 LoadVisibleDeclsFromExternalStorage(Context);
612
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000613 /// If there is no lookup data structure, build one now by walking
Douglas Gregor44b43212008-12-11 16:49:14 +0000614 /// all of the linked DeclContexts (in declaration order!) and
615 /// inserting their values.
Douglas Gregorc36c5402009-04-09 17:29:08 +0000616 if (!LookupPtr) {
Douglas Gregor6ab35242009-04-09 21:40:53 +0000617 buildLookup(Context, this);
Douglas Gregor44b43212008-12-11 16:49:14 +0000618
Douglas Gregorc36c5402009-04-09 17:29:08 +0000619 if (!LookupPtr)
Chris Lattner91942502009-02-20 00:55:03 +0000620 return lookup_result(0, 0);
Douglas Gregorc36c5402009-04-09 17:29:08 +0000621 }
Douglas Gregor44b43212008-12-11 16:49:14 +0000622
Douglas Gregorc36c5402009-04-09 17:29:08 +0000623 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(LookupPtr);
624 StoredDeclsMap::iterator Pos = Map->find(Name);
625 if (Pos == Map->end())
626 return lookup_result(0, 0);
Douglas Gregor6ab35242009-04-09 21:40:53 +0000627 return Pos->second.getLookupResult(Context);
Douglas Gregor44b43212008-12-11 16:49:14 +0000628}
629
630DeclContext::lookup_const_result
Douglas Gregor6ab35242009-04-09 21:40:53 +0000631DeclContext::lookup(ASTContext &Context, DeclarationName Name) const {
632 return const_cast<DeclContext*>(this)->lookup(Context, Name);
Douglas Gregor44b43212008-12-11 16:49:14 +0000633}
634
Chris Lattner0cf2b192009-03-27 19:19:59 +0000635DeclContext *DeclContext::getLookupContext() {
636 DeclContext *Ctx = this;
Douglas Gregor72de6672009-01-08 20:45:30 +0000637 // Skip through transparent contexts.
Douglas Gregorce356072009-01-06 23:51:29 +0000638 while (Ctx->isTransparentContext())
639 Ctx = Ctx->getParent();
640 return Ctx;
641}
642
Douglas Gregor88b70942009-02-25 22:02:03 +0000643DeclContext *DeclContext::getEnclosingNamespaceContext() {
644 DeclContext *Ctx = this;
645 // Skip through non-namespace, non-translation-unit contexts.
646 while (!Ctx->isFileContext() || Ctx->isTransparentContext())
647 Ctx = Ctx->getParent();
648 return Ctx->getPrimaryContext();
649}
650
Douglas Gregor6ab35242009-04-09 21:40:53 +0000651void DeclContext::makeDeclVisibleInContext(ASTContext &Context, NamedDecl *D) {
Douglas Gregorcc636682009-02-17 23:15:12 +0000652 // FIXME: This feels like a hack. Should DeclarationName support
653 // template-ids, or is there a better way to keep specializations
654 // from being visible?
655 if (isa<ClassTemplateSpecializationDecl>(D))
656 return;
657
Steve Naroff0701bbb2009-01-08 17:28:14 +0000658 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor44b43212008-12-11 16:49:14 +0000659 if (PrimaryContext != this) {
Douglas Gregor6ab35242009-04-09 21:40:53 +0000660 PrimaryContext->makeDeclVisibleInContext(Context, D);
Douglas Gregor44b43212008-12-11 16:49:14 +0000661 return;
662 }
663
664 // If we already have a lookup data structure, perform the insertion
665 // into it. Otherwise, be lazy and don't build that structure until
666 // someone asks for it.
Douglas Gregorc36c5402009-04-09 17:29:08 +0000667 if (LookupPtr)
Douglas Gregor6ab35242009-04-09 21:40:53 +0000668 makeDeclVisibleInContextImpl(Context, D);
Douglas Gregor074149e2009-01-05 19:45:36 +0000669
Douglas Gregor074149e2009-01-05 19:45:36 +0000670 // If we are a transparent context, insert into our parent context,
671 // too. This operation is recursive.
672 if (isTransparentContext())
Douglas Gregor6ab35242009-04-09 21:40:53 +0000673 getParent()->makeDeclVisibleInContext(Context, D);
Douglas Gregor44b43212008-12-11 16:49:14 +0000674}
675
Douglas Gregor6ab35242009-04-09 21:40:53 +0000676void DeclContext::makeDeclVisibleInContextImpl(ASTContext &Context,
677 NamedDecl *D) {
Douglas Gregor074149e2009-01-05 19:45:36 +0000678 // Skip unnamed declarations.
679 if (!D->getDeclName())
680 return;
681
Douglas Gregorcc636682009-02-17 23:15:12 +0000682 // FIXME: This feels like a hack. Should DeclarationName support
683 // template-ids, or is there a better way to keep specializations
684 // from being visible?
685 if (isa<ClassTemplateSpecializationDecl>(D))
686 return;
687
Douglas Gregorc36c5402009-04-09 17:29:08 +0000688 if (!LookupPtr)
689 LookupPtr = new StoredDeclsMap;
Douglas Gregor44b43212008-12-11 16:49:14 +0000690
691 // Insert this declaration into the map.
Douglas Gregorc36c5402009-04-09 17:29:08 +0000692 StoredDeclsMap &Map = *static_cast<StoredDeclsMap*>(LookupPtr);
Chris Lattner67762a32009-02-20 01:44:05 +0000693 StoredDeclsList &DeclNameEntries = Map[D->getDeclName()];
694 if (DeclNameEntries.isNull()) {
695 DeclNameEntries.setOnlyValue(D);
Chris Lattnerbd6c8002009-02-19 07:00:44 +0000696 return;
Douglas Gregor44b43212008-12-11 16:49:14 +0000697 }
Chris Lattner91942502009-02-20 00:55:03 +0000698
Chris Lattnerbdc3d002009-02-20 01:10:07 +0000699 // If it is possible that this is a redeclaration, check to see if there is
700 // already a decl for which declarationReplaces returns true. If there is
701 // one, just replace it and return.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000702 if (DeclNameEntries.HandleRedeclaration(Context, D))
Chris Lattner67762a32009-02-20 01:44:05 +0000703 return;
Chris Lattner91942502009-02-20 00:55:03 +0000704
Chris Lattnerbd6c8002009-02-19 07:00:44 +0000705 // Put this declaration into the appropriate slot.
Chris Lattner67762a32009-02-20 01:44:05 +0000706 DeclNameEntries.AddSubsequentDecl(D);
Douglas Gregor44b43212008-12-11 16:49:14 +0000707}
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000708
709/// Returns iterator range [First, Last) of UsingDirectiveDecls stored within
710/// this context.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000711DeclContext::udir_iterator_range
712DeclContext::getUsingDirectives(ASTContext &Context) const {
713 lookup_const_result Result = lookup(Context, UsingDirectiveDecl::getName());
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000714 return udir_iterator_range(reinterpret_cast<udir_iterator>(Result.first),
715 reinterpret_cast<udir_iterator>(Result.second));
716}
Douglas Gregor2cf26342009-04-09 22:27:44 +0000717
718void StoredDeclsList::materializeDecls(ASTContext &Context) {
719 if (isNull())
720 return;
721
722 switch ((DataKind)(Data & 0x03)) {
723 case DK_Decl:
724 case DK_Decl_Vector:
725 break;
726
727 case DK_DeclID: {
728 // Resolve this declaration ID to an actual declaration by
729 // querying the external AST source.
730 unsigned DeclID = Data >> 2;
731
732 ExternalASTSource *Source = Context.getExternalSource();
733 assert(Source && "No external AST source available!");
734
735 Data = reinterpret_cast<uintptr_t>(Source->GetDecl(DeclID));
736 break;
737 }
738
739 case DK_ID_Vector: {
740 // We have a vector of declaration IDs. Resolve all of them to
741 // actual declarations.
742 VectorTy &Vector = *getAsVector();
743 ExternalASTSource *Source = Context.getExternalSource();
744 assert(Source && "No external AST source available!");
745
746 for (unsigned I = 0, N = Vector.size(); I != N; ++I)
747 Vector[I] = reinterpret_cast<uintptr_t>(Source->GetDecl(Vector[I]));
748
749 Data = (Data & ~0x03) | DK_Decl_Vector;
750 break;
751 }
752 }
753}