blob: b83a503dbb2fb9f906a585abf71a9b01be7d7b2d [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
Eli Friedman56d29372008-06-07 16:52:53 +000041const char *Decl::getDeclKindName() const {
42 switch (DeclKind) {
Douglas Gregor64650af2009-02-02 23:39:07 +000043 default: assert(0 && "Declaration not in DeclNodes.def!");
44#define DECL(Derived, Base) case Derived: return #Derived;
45#include "clang/AST/DeclNodes.def"
Eli Friedman56d29372008-06-07 16:52:53 +000046 }
47}
48
Steve Naroff0a473932009-01-20 19:53:53 +000049const char *DeclContext::getDeclKindName() const {
50 switch (DeclKind) {
Douglas Gregor64650af2009-02-02 23:39:07 +000051 default: assert(0 && "Declaration context not in DeclNodes.def!");
Argyrios Kyrtzidis1ad4dd72009-02-16 14:28:33 +000052#define DECL(Derived, Base) case Decl::Derived: return #Derived;
Douglas Gregor64650af2009-02-02 23:39:07 +000053#include "clang/AST/DeclNodes.def"
Steve Naroff0a473932009-01-20 19:53:53 +000054 }
55}
56
Eli Friedman56d29372008-06-07 16:52:53 +000057bool Decl::CollectingStats(bool Enable) {
58 if (Enable)
59 StatSwitch = true;
60 return StatSwitch;
61}
62
63void Decl::PrintStats() {
64 fprintf(stderr, "*** Decl Stats:\n");
Eli Friedman56d29372008-06-07 16:52:53 +000065
Douglas Gregor64650af2009-02-02 23:39:07 +000066 int totalDecls = 0;
67#define DECL(Derived, Base) totalDecls += n##Derived##s;
68#include "clang/AST/DeclNodes.def"
69 fprintf(stderr, " %d decls total.\n", totalDecls);
70
71 int totalBytes = 0;
72#define DECL(Derived, Base) \
73 if (n##Derived##s > 0) { \
74 totalBytes += (int)(n##Derived##s * sizeof(Derived##Decl)); \
75 fprintf(stderr, " %d " #Derived " decls, %d each (%d bytes)\n", \
76 n##Derived##s, (int)sizeof(Derived##Decl), \
77 (int)(n##Derived##s * sizeof(Derived##Decl))); \
78 }
79#include "clang/AST/DeclNodes.def"
Eli Friedman56d29372008-06-07 16:52:53 +000080
Douglas Gregor64650af2009-02-02 23:39:07 +000081 fprintf(stderr, "Total bytes = %d\n", totalBytes);
Eli Friedman56d29372008-06-07 16:52:53 +000082}
83
84void Decl::addDeclKind(Kind k) {
85 switch (k) {
Douglas Gregor64650af2009-02-02 23:39:07 +000086 default: assert(0 && "Declaration not in DeclNodes.def!");
87#define DECL(Derived, Base) case Derived: ++n##Derived##s; break;
88#include "clang/AST/DeclNodes.def"
Eli Friedman56d29372008-06-07 16:52:53 +000089 }
90}
91
Anders Carlsson67e33202009-06-13 00:08:58 +000092bool Decl::isTemplateParameterPack() const {
93 if (const TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(this))
94 return TTP->isParameterPack();
95
96 return false;
97}
98
Eli Friedman56d29372008-06-07 16:52:53 +000099//===----------------------------------------------------------------------===//
Chris Lattner49f28ca2009-03-05 08:00:35 +0000100// PrettyStackTraceDecl Implementation
101//===----------------------------------------------------------------------===//
102
103void PrettyStackTraceDecl::print(llvm::raw_ostream &OS) const {
104 SourceLocation TheLoc = Loc;
105 if (TheLoc.isInvalid() && TheDecl)
106 TheLoc = TheDecl->getLocation();
107
108 if (TheLoc.isValid()) {
109 TheLoc.print(OS, SM);
110 OS << ": ";
111 }
112
113 OS << Message;
114
115 if (NamedDecl *DN = dyn_cast_or_null<NamedDecl>(TheDecl))
116 OS << " '" << DN->getQualifiedNameAsString() << '\'';
117 OS << '\n';
118}
119
120//===----------------------------------------------------------------------===//
Eli Friedman56d29372008-06-07 16:52:53 +0000121// Decl Implementation
122//===----------------------------------------------------------------------===//
123
Chris Lattner769dbdf2009-03-27 20:18:19 +0000124// Out-of-line virtual method providing a home for Decl.
125Decl::~Decl() {
126 if (isOutOfSemaDC())
127 delete getMultipleDC();
128
129 assert(!HasAttrs && "attributes should have been freed by Destroy");
130}
131
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000132void Decl::setDeclContext(DeclContext *DC) {
133 if (isOutOfSemaDC())
134 delete getMultipleDC();
135
Chris Lattneree219fd2009-03-29 06:06:59 +0000136 DeclCtx = DC;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000137}
138
139void Decl::setLexicalDeclContext(DeclContext *DC) {
140 if (DC == getLexicalDeclContext())
141 return;
142
143 if (isInSemaDC()) {
144 MultipleDC *MDC = new MultipleDC();
145 MDC->SemanticDC = getDeclContext();
146 MDC->LexicalDC = DC;
Chris Lattneree219fd2009-03-29 06:06:59 +0000147 DeclCtx = MDC;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000148 } else {
149 getMultipleDC()->LexicalDC = DC;
150 }
151}
152
Chris Lattner769dbdf2009-03-27 20:18:19 +0000153unsigned Decl::getIdentifierNamespaceForKind(Kind DeclKind) {
154 switch (DeclKind) {
155 default:
156 if (DeclKind >= FunctionFirst && DeclKind <= FunctionLast)
157 return IDNS_Ordinary;
158 assert(0 && "Unknown decl kind!");
159 case OverloadedFunction:
160 case Typedef:
161 case EnumConstant:
162 case Var:
163 case ImplicitParam:
164 case ParmVar:
165 case OriginalParmVar:
166 case NonTypeTemplateParm:
167 case ObjCMethod:
168 case ObjCContainer:
169 case ObjCCategory:
170 case ObjCInterface:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000171 case ObjCProperty:
172 case ObjCCompatibleAlias:
173 return IDNS_Ordinary;
174
175 case ObjCProtocol:
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000176 return IDNS_ObjCProtocol;
Chris Lattner769dbdf2009-03-27 20:18:19 +0000177
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000178 case ObjCImplementation:
179 return IDNS_ObjCImplementation;
180
181 case ObjCCategoryImpl:
182 return IDNS_ObjCCategoryImpl;
183
Chris Lattner769dbdf2009-03-27 20:18:19 +0000184 case Field:
185 case ObjCAtDefsField:
186 case ObjCIvar:
187 return IDNS_Member;
188
189 case Record:
190 case CXXRecord:
191 case Enum:
192 case TemplateTypeParm:
193 return IDNS_Tag;
194
195 case Namespace:
196 case Template:
197 case FunctionTemplate:
198 case ClassTemplate:
199 case TemplateTemplateParm:
Anders Carlssonfaf0e872009-03-28 23:02:53 +0000200 case NamespaceAlias:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000201 return IDNS_Tag | IDNS_Ordinary;
202
203 // Never have names.
204 case LinkageSpec:
205 case FileScopeAsm:
206 case StaticAssert:
207 case ObjCClass:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000208 case ObjCPropertyImpl:
209 case ObjCForwardProtocol:
210 case Block:
211 case TranslationUnit:
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:
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000216 case ClassTemplatePartialSpecialization:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000217 return 0;
218 }
Eli Friedman56d29372008-06-07 16:52:53 +0000219}
220
Douglas Gregor68584ed2009-06-18 16:11:24 +0000221void Decl::addAttr(ASTContext &Context, Attr *NewAttr) {
222 Attr *&ExistingAttr = Context.getDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000223
224 NewAttr->setNext(ExistingAttr);
225 ExistingAttr = NewAttr;
226
227 HasAttrs = true;
228}
229
Douglas Gregor68584ed2009-06-18 16:11:24 +0000230void Decl::invalidateAttrs(ASTContext &Context) {
Eli Friedman56d29372008-06-07 16:52:53 +0000231 if (!HasAttrs) return;
Douglas Gregor68584ed2009-06-18 16:11:24 +0000232
Eli Friedman56d29372008-06-07 16:52:53 +0000233 HasAttrs = false;
Douglas Gregor68584ed2009-06-18 16:11:24 +0000234 Context.eraseDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000235}
236
Douglas Gregor68584ed2009-06-18 16:11:24 +0000237const Attr *Decl::getAttrsImpl(ASTContext &Context) const {
Chris Lattner81abbdd2009-03-21 06:27:31 +0000238 assert(HasAttrs && "getAttrs() should verify this!");
Douglas Gregor68584ed2009-06-18 16:11:24 +0000239 return Context.getDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000240}
241
Douglas Gregor68584ed2009-06-18 16:11:24 +0000242void Decl::swapAttrs(ASTContext &Context, Decl *RHS) {
Eli Friedman56d29372008-06-07 16:52:53 +0000243 bool HasLHSAttr = this->HasAttrs;
244 bool HasRHSAttr = RHS->HasAttrs;
245
246 // Usually, neither decl has attrs, nothing to do.
247 if (!HasLHSAttr && !HasRHSAttr) return;
248
249 // If 'this' has no attrs, swap the other way.
250 if (!HasLHSAttr)
Douglas Gregor68584ed2009-06-18 16:11:24 +0000251 return RHS->swapAttrs(Context, this);
Eli Friedman56d29372008-06-07 16:52:53 +0000252
253 // Handle the case when both decls have attrs.
254 if (HasRHSAttr) {
Douglas Gregor68584ed2009-06-18 16:11:24 +0000255 std::swap(Context.getDeclAttrs(this), Context.getDeclAttrs(RHS));
Eli Friedman56d29372008-06-07 16:52:53 +0000256 return;
257 }
258
259 // Otherwise, LHS has an attr and RHS doesn't.
Douglas Gregor68584ed2009-06-18 16:11:24 +0000260 Context.getDeclAttrs(RHS) = Context.getDeclAttrs(this);
261 Context.eraseDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000262 this->HasAttrs = false;
263 RHS->HasAttrs = true;
264}
265
266
Chris Lattnercc581472009-03-04 06:05:19 +0000267void Decl::Destroy(ASTContext &C) {
268 // Free attributes for this decl.
269 if (HasAttrs) {
Douglas Gregor68584ed2009-06-18 16:11:24 +0000270 C.getDeclAttrs(this)->Destroy(C);
271 invalidateAttrs(C);
Chris Lattnercc581472009-03-04 06:05:19 +0000272 HasAttrs = false;
273 }
274
Douglas Gregora0fc55f2009-01-13 19:47:12 +0000275#if 0
Douglas Gregor00ad0ef2009-01-20 04:25:11 +0000276 // FIXME: Once ownership is fully understood, we can enable this code
277 if (DeclContext *DC = dyn_cast<DeclContext>(this))
278 DC->decls_begin()->Destroy(C);
Eli Friedman56d29372008-06-07 16:52:53 +0000279
Chris Lattner244a67d2009-03-28 06:04:26 +0000280 // Observe the unrolled recursion. By setting N->NextDeclInContext = 0x0
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000281 // within the loop, only the Destroy method for the first Decl
282 // will deallocate all of the Decls in a chain.
283
Chris Lattner244a67d2009-03-28 06:04:26 +0000284 Decl* N = getNextDeclInContext();
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000285
286 while (N) {
Chris Lattner244a67d2009-03-28 06:04:26 +0000287 Decl* Tmp = N->getNextDeclInContext();
288 N->NextDeclInContext = 0;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000289 N->Destroy(C);
290 N = Tmp;
Eli Friedman56d29372008-06-07 16:52:53 +0000291 }
Douglas Gregora0fc55f2009-01-13 19:47:12 +0000292
Eli Friedman56d29372008-06-07 16:52:53 +0000293 this->~Decl();
Steve Naroff3e970492009-01-27 21:25:57 +0000294 C.Deallocate((void *)this);
Douglas Gregor00ad0ef2009-01-20 04:25:11 +0000295#endif
Eli Friedman56d29372008-06-07 16:52:53 +0000296}
297
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000298Decl *Decl::castFromDeclContext (const DeclContext *D) {
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000299 Decl::Kind DK = D->getDeclKind();
300 switch(DK) {
301#define DECL_CONTEXT(Name) \
302 case Decl::Name: \
303 return static_cast<Name##Decl*>(const_cast<DeclContext*>(D));
304#define DECL_CONTEXT_BASE(Name)
305#include "clang/AST/DeclNodes.def"
306 default:
307#define DECL_CONTEXT_BASE(Name) \
308 if (DK >= Decl::Name##First && DK <= Decl::Name##Last) \
309 return static_cast<Name##Decl*>(const_cast<DeclContext*>(D));
310#include "clang/AST/DeclNodes.def"
311 assert(false && "a decl that inherits DeclContext isn't handled");
312 return 0;
313 }
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000314}
315
316DeclContext *Decl::castToDeclContext(const Decl *D) {
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000317 Decl::Kind DK = D->getKind();
318 switch(DK) {
319#define DECL_CONTEXT(Name) \
320 case Decl::Name: \
321 return static_cast<Name##Decl*>(const_cast<Decl*>(D));
322#define DECL_CONTEXT_BASE(Name)
323#include "clang/AST/DeclNodes.def"
324 default:
325#define DECL_CONTEXT_BASE(Name) \
326 if (DK >= Decl::Name##First && DK <= Decl::Name##Last) \
327 return static_cast<Name##Decl*>(const_cast<Decl*>(D));
328#include "clang/AST/DeclNodes.def"
329 assert(false && "a decl that inherits DeclContext isn't handled");
330 return 0;
331 }
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000332}
333
Sebastian Redld3a413d2009-04-26 20:35:05 +0000334CompoundStmt* Decl::getCompoundBody(ASTContext &Context) const {
335 return dyn_cast_or_null<CompoundStmt>(getBody(Context));
336}
337
338SourceLocation Decl::getBodyRBrace(ASTContext &Context) const {
339 Stmt *Body = getBody(Context);
340 if (!Body)
341 return SourceLocation();
342 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Body))
343 return CS->getRBracLoc();
344 assert(isa<CXXTryStmt>(Body) &&
345 "Body can only be CompoundStmt or CXXTryStmt");
346 return cast<CXXTryStmt>(Body)->getSourceRange().getEnd();
347}
348
Anders Carlsson1329c272009-03-25 23:38:06 +0000349#ifndef NDEBUG
350void Decl::CheckAccessDeclContext() const {
Douglas Gregor5c27f2b2009-04-07 20:58:25 +0000351 assert((Access != AS_none || isa<TranslationUnitDecl>(this) ||
352 !isa<CXXRecordDecl>(getDeclContext())) &&
Anders Carlsson1329c272009-03-25 23:38:06 +0000353 "Access specifier is AS_none inside a record decl");
354}
355
356#endif
357
Eli Friedman56d29372008-06-07 16:52:53 +0000358//===----------------------------------------------------------------------===//
359// DeclContext Implementation
360//===----------------------------------------------------------------------===//
361
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000362bool DeclContext::classof(const Decl *D) {
363 switch (D->getKind()) {
364#define DECL_CONTEXT(Name) case Decl::Name:
365#define DECL_CONTEXT_BASE(Name)
366#include "clang/AST/DeclNodes.def"
367 return true;
368 default:
369#define DECL_CONTEXT_BASE(Name) \
370 if (D->getKind() >= Decl::Name##First && \
371 D->getKind() <= Decl::Name##Last) \
372 return true;
373#include "clang/AST/DeclNodes.def"
374 return false;
375 }
376}
377
Douglas Gregor44b43212008-12-11 16:49:14 +0000378DeclContext::~DeclContext() {
Douglas Gregorc36c5402009-04-09 17:29:08 +0000379 delete static_cast<StoredDeclsMap*>(LookupPtr);
Douglas Gregor44b43212008-12-11 16:49:14 +0000380}
381
382void DeclContext::DestroyDecls(ASTContext &C) {
Douglas Gregor6ab35242009-04-09 21:40:53 +0000383 for (decl_iterator D = decls_begin(C); D != decls_end(C); )
Douglas Gregor00ad0ef2009-01-20 04:25:11 +0000384 (*D++)->Destroy(C);
Douglas Gregor44b43212008-12-11 16:49:14 +0000385}
386
Douglas Gregorbc221632009-05-28 16:34:51 +0000387bool DeclContext::isDependentContext() const {
388 if (isFileContext())
389 return false;
390
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000391 if (isa<ClassTemplatePartialSpecializationDecl>(this))
392 return true;
393
Douglas Gregorbc221632009-05-28 16:34:51 +0000394 if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this))
395 if (Record->getDescribedClassTemplate())
396 return true;
397
398 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this))
399 if (Function->getDescribedFunctionTemplate())
400 return true;
401
402 return getParent() && getParent()->isDependentContext();
403}
404
Douglas Gregor074149e2009-01-05 19:45:36 +0000405bool DeclContext::isTransparentContext() const {
406 if (DeclKind == Decl::Enum)
407 return true; // FIXME: Check for C++0x scoped enums
408 else if (DeclKind == Decl::LinkageSpec)
409 return true;
Douglas Gregor65100792009-02-26 00:02:51 +0000410 else if (DeclKind >= Decl::RecordFirst && DeclKind <= Decl::RecordLast)
Douglas Gregorbcbffc42009-01-07 00:43:41 +0000411 return cast<RecordDecl>(this)->isAnonymousStructOrUnion();
Douglas Gregor074149e2009-01-05 19:45:36 +0000412 else if (DeclKind == Decl::Namespace)
413 return false; // FIXME: Check for C++0x inline namespaces
414
415 return false;
416}
417
Steve Naroff0701bbb2009-01-08 17:28:14 +0000418DeclContext *DeclContext::getPrimaryContext() {
Douglas Gregor44b43212008-12-11 16:49:14 +0000419 switch (DeclKind) {
Douglas Gregor44b43212008-12-11 16:49:14 +0000420 case Decl::TranslationUnit:
Douglas Gregor074149e2009-01-05 19:45:36 +0000421 case Decl::LinkageSpec:
422 case Decl::Block:
Douglas Gregor44b43212008-12-11 16:49:14 +0000423 // There is only one DeclContext for these entities.
424 return this;
425
426 case Decl::Namespace:
427 // The original namespace is our primary context.
428 return static_cast<NamespaceDecl*>(this)->getOriginalNamespace();
429
Douglas Gregor44b43212008-12-11 16:49:14 +0000430 case Decl::ObjCMethod:
431 return this;
432
433 case Decl::ObjCInterface:
Steve Naroff0701bbb2009-01-08 17:28:14 +0000434 case Decl::ObjCProtocol:
435 case Decl::ObjCCategory:
Douglas Gregor44b43212008-12-11 16:49:14 +0000436 // FIXME: Can Objective-C interfaces be forward-declared?
437 return this;
438
Steve Naroff0701bbb2009-01-08 17:28:14 +0000439 case Decl::ObjCImplementation:
440 case Decl::ObjCCategoryImpl:
441 return this;
442
Douglas Gregor44b43212008-12-11 16:49:14 +0000443 default:
Douglas Gregorcc636682009-02-17 23:15:12 +0000444 if (DeclKind >= Decl::TagFirst && DeclKind <= Decl::TagLast) {
445 // If this is a tag type that has a definition or is currently
446 // being defined, that definition is our primary context.
Chris Lattner244a67d2009-03-28 06:04:26 +0000447 if (const TagType *TagT =cast<TagDecl>(this)->TypeForDecl->getAsTagType())
Douglas Gregorcc636682009-02-17 23:15:12 +0000448 if (TagT->isBeingDefined() ||
449 (TagT->getDecl() && TagT->getDecl()->isDefinition()))
450 return TagT->getDecl();
451 return this;
452 }
453
Douglas Gregor44b43212008-12-11 16:49:14 +0000454 assert(DeclKind >= Decl::FunctionFirst && DeclKind <= Decl::FunctionLast &&
455 "Unknown DeclContext kind");
456 return this;
457 }
458}
459
460DeclContext *DeclContext::getNextContext() {
461 switch (DeclKind) {
Douglas Gregor44b43212008-12-11 16:49:14 +0000462 case Decl::Namespace:
463 // Return the next namespace
464 return static_cast<NamespaceDecl*>(this)->getNextNamespace();
465
466 default:
Douglas Gregor44b43212008-12-11 16:49:14 +0000467 return 0;
468 }
469}
470
Douglas Gregor2cf26342009-04-09 22:27:44 +0000471/// \brief Load the declarations within this lexical storage from an
472/// external source.
473void
474DeclContext::LoadLexicalDeclsFromExternalStorage(ASTContext &Context) const {
475 ExternalASTSource *Source = Context.getExternalSource();
476 assert(hasExternalLexicalStorage() && Source && "No external storage?");
477
Eli Friedmanb0156ea2009-04-27 23:43:36 +0000478 llvm::SmallVector<uint32_t, 64> Decls;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000479 if (Source->ReadDeclsLexicallyInContext(const_cast<DeclContext *>(this),
480 Decls))
481 return;
482
483 // There is no longer any lexical storage in this context
484 ExternalLexicalStorage = false;
485
486 if (Decls.empty())
487 return;
488
489 // Resolve all of the declaration IDs into declarations, building up
490 // a chain of declarations via the Decl::NextDeclInContext field.
491 Decl *FirstNewDecl = 0;
492 Decl *PrevDecl = 0;
493 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
494 Decl *D = Source->GetDecl(Decls[I]);
495 if (PrevDecl)
496 PrevDecl->NextDeclInContext = D;
497 else
498 FirstNewDecl = D;
499
500 PrevDecl = D;
501 }
502
503 // Splice the newly-read declarations into the beginning of the list
504 // of declarations.
505 PrevDecl->NextDeclInContext = FirstDecl;
506 FirstDecl = FirstNewDecl;
507 if (!LastDecl)
508 LastDecl = PrevDecl;
509}
510
511void
512DeclContext::LoadVisibleDeclsFromExternalStorage(ASTContext &Context) const {
513 DeclContext *This = const_cast<DeclContext *>(this);
514 ExternalASTSource *Source = Context.getExternalSource();
515 assert(hasExternalVisibleStorage() && Source && "No external storage?");
516
517 llvm::SmallVector<VisibleDeclaration, 64> Decls;
518 if (Source->ReadDeclsVisibleInContext(This, Decls))
519 return;
520
521 // There is no longer any visible storage in this context
522 ExternalVisibleStorage = false;
523
524 // Load the declaration IDs for all of the names visible in this
525 // context.
526 assert(!LookupPtr && "Have a lookup map before de-serialization?");
527 StoredDeclsMap *Map = new StoredDeclsMap;
528 LookupPtr = Map;
529 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
530 (*Map)[Decls[I].Name].setFromDeclIDs(Decls[I].Declarations);
531 }
532}
533
Douglas Gregor6ab35242009-04-09 21:40:53 +0000534DeclContext::decl_iterator DeclContext::decls_begin(ASTContext &Context) const {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000535 if (hasExternalLexicalStorage())
536 LoadLexicalDeclsFromExternalStorage(Context);
537
538 // FIXME: Check whether we need to load some declarations from
539 // external storage.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000540 return decl_iterator(FirstDecl);
541}
542
543DeclContext::decl_iterator DeclContext::decls_end(ASTContext &Context) const {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000544 if (hasExternalLexicalStorage())
545 LoadLexicalDeclsFromExternalStorage(Context);
546
Douglas Gregor6ab35242009-04-09 21:40:53 +0000547 return decl_iterator();
548}
549
Douglas Gregor8038d512009-04-10 17:25:41 +0000550bool DeclContext::decls_empty(ASTContext &Context) const {
551 if (hasExternalLexicalStorage())
552 LoadLexicalDeclsFromExternalStorage(Context);
553
554 return !FirstDecl;
555}
556
Douglas Gregor6ab35242009-04-09 21:40:53 +0000557void DeclContext::addDecl(ASTContext &Context, Decl *D) {
Chris Lattner7f0be132009-02-20 00:56:18 +0000558 assert(D->getLexicalDeclContext() == this &&
559 "Decl inserted into wrong lexical context");
Chris Lattner244a67d2009-03-28 06:04:26 +0000560 assert(!D->getNextDeclInContext() && D != LastDecl &&
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000561 "Decl already inserted into a DeclContext");
562
563 if (FirstDecl) {
Chris Lattner244a67d2009-03-28 06:04:26 +0000564 LastDecl->NextDeclInContext = D;
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000565 LastDecl = D;
566 } else {
567 FirstDecl = LastDecl = D;
568 }
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000569
570 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
Douglas Gregor6ab35242009-04-09 21:40:53 +0000571 ND->getDeclContext()->makeDeclVisibleInContext(Context, ND);
Douglas Gregor44b43212008-12-11 16:49:14 +0000572}
573
Douglas Gregor074149e2009-01-05 19:45:36 +0000574/// buildLookup - Build the lookup data structure with all of the
575/// declarations in DCtx (and any other contexts linked to it or
576/// transparent contexts nested within it).
Douglas Gregor6ab35242009-04-09 21:40:53 +0000577void DeclContext::buildLookup(ASTContext &Context, DeclContext *DCtx) {
Douglas Gregor074149e2009-01-05 19:45:36 +0000578 for (; DCtx; DCtx = DCtx->getNextContext()) {
Douglas Gregor6ab35242009-04-09 21:40:53 +0000579 for (decl_iterator D = DCtx->decls_begin(Context),
580 DEnd = DCtx->decls_end(Context);
Douglas Gregor4f3b8f82009-01-06 07:17:58 +0000581 D != DEnd; ++D) {
Douglas Gregor074149e2009-01-05 19:45:36 +0000582 // Insert this declaration into the lookup structure
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000583 if (NamedDecl *ND = dyn_cast<NamedDecl>(*D))
Douglas Gregor6ab35242009-04-09 21:40:53 +0000584 makeDeclVisibleInContextImpl(Context, ND);
Douglas Gregor074149e2009-01-05 19:45:36 +0000585
586 // If this declaration is itself a transparent declaration context,
587 // add its members (recursively).
588 if (DeclContext *InnerCtx = dyn_cast<DeclContext>(*D))
589 if (InnerCtx->isTransparentContext())
Douglas Gregor6ab35242009-04-09 21:40:53 +0000590 buildLookup(Context, InnerCtx->getPrimaryContext());
Douglas Gregor074149e2009-01-05 19:45:36 +0000591 }
592 }
593}
594
Douglas Gregor44b43212008-12-11 16:49:14 +0000595DeclContext::lookup_result
Douglas Gregor6ab35242009-04-09 21:40:53 +0000596DeclContext::lookup(ASTContext &Context, DeclarationName Name) {
Steve Naroff0701bbb2009-01-08 17:28:14 +0000597 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor44b43212008-12-11 16:49:14 +0000598 if (PrimaryContext != this)
Douglas Gregor6ab35242009-04-09 21:40:53 +0000599 return PrimaryContext->lookup(Context, Name);
Douglas Gregor44b43212008-12-11 16:49:14 +0000600
Douglas Gregor2cf26342009-04-09 22:27:44 +0000601 if (hasExternalVisibleStorage())
602 LoadVisibleDeclsFromExternalStorage(Context);
603
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000604 /// If there is no lookup data structure, build one now by walking
Douglas Gregor44b43212008-12-11 16:49:14 +0000605 /// all of the linked DeclContexts (in declaration order!) and
606 /// inserting their values.
Douglas Gregorc36c5402009-04-09 17:29:08 +0000607 if (!LookupPtr) {
Douglas Gregor6ab35242009-04-09 21:40:53 +0000608 buildLookup(Context, this);
Douglas Gregor44b43212008-12-11 16:49:14 +0000609
Douglas Gregorc36c5402009-04-09 17:29:08 +0000610 if (!LookupPtr)
Chris Lattner91942502009-02-20 00:55:03 +0000611 return lookup_result(0, 0);
Douglas Gregorc36c5402009-04-09 17:29:08 +0000612 }
Douglas Gregor44b43212008-12-11 16:49:14 +0000613
Douglas Gregorc36c5402009-04-09 17:29:08 +0000614 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(LookupPtr);
615 StoredDeclsMap::iterator Pos = Map->find(Name);
616 if (Pos == Map->end())
617 return lookup_result(0, 0);
Douglas Gregor6ab35242009-04-09 21:40:53 +0000618 return Pos->second.getLookupResult(Context);
Douglas Gregor44b43212008-12-11 16:49:14 +0000619}
620
621DeclContext::lookup_const_result
Douglas Gregor6ab35242009-04-09 21:40:53 +0000622DeclContext::lookup(ASTContext &Context, DeclarationName Name) const {
623 return const_cast<DeclContext*>(this)->lookup(Context, Name);
Douglas Gregor44b43212008-12-11 16:49:14 +0000624}
625
Chris Lattner0cf2b192009-03-27 19:19:59 +0000626DeclContext *DeclContext::getLookupContext() {
627 DeclContext *Ctx = this;
Douglas Gregor72de6672009-01-08 20:45:30 +0000628 // Skip through transparent contexts.
Douglas Gregorce356072009-01-06 23:51:29 +0000629 while (Ctx->isTransparentContext())
630 Ctx = Ctx->getParent();
631 return Ctx;
632}
633
Douglas Gregor88b70942009-02-25 22:02:03 +0000634DeclContext *DeclContext::getEnclosingNamespaceContext() {
635 DeclContext *Ctx = this;
636 // Skip through non-namespace, non-translation-unit contexts.
637 while (!Ctx->isFileContext() || Ctx->isTransparentContext())
638 Ctx = Ctx->getParent();
639 return Ctx->getPrimaryContext();
640}
641
Douglas Gregor6ab35242009-04-09 21:40:53 +0000642void DeclContext::makeDeclVisibleInContext(ASTContext &Context, NamedDecl *D) {
Douglas Gregorcc636682009-02-17 23:15:12 +0000643 // FIXME: This feels like a hack. Should DeclarationName support
644 // template-ids, or is there a better way to keep specializations
645 // from being visible?
646 if (isa<ClassTemplateSpecializationDecl>(D))
647 return;
648
Steve Naroff0701bbb2009-01-08 17:28:14 +0000649 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor44b43212008-12-11 16:49:14 +0000650 if (PrimaryContext != this) {
Douglas Gregor6ab35242009-04-09 21:40:53 +0000651 PrimaryContext->makeDeclVisibleInContext(Context, D);
Douglas Gregor44b43212008-12-11 16:49:14 +0000652 return;
653 }
654
655 // If we already have a lookup data structure, perform the insertion
656 // into it. Otherwise, be lazy and don't build that structure until
657 // someone asks for it.
Douglas Gregorc36c5402009-04-09 17:29:08 +0000658 if (LookupPtr)
Douglas Gregor6ab35242009-04-09 21:40:53 +0000659 makeDeclVisibleInContextImpl(Context, D);
Douglas Gregor074149e2009-01-05 19:45:36 +0000660
Douglas Gregor074149e2009-01-05 19:45:36 +0000661 // If we are a transparent context, insert into our parent context,
662 // too. This operation is recursive.
663 if (isTransparentContext())
Douglas Gregor6ab35242009-04-09 21:40:53 +0000664 getParent()->makeDeclVisibleInContext(Context, D);
Douglas Gregor44b43212008-12-11 16:49:14 +0000665}
666
Douglas Gregor6ab35242009-04-09 21:40:53 +0000667void DeclContext::makeDeclVisibleInContextImpl(ASTContext &Context,
668 NamedDecl *D) {
Douglas Gregor074149e2009-01-05 19:45:36 +0000669 // Skip unnamed declarations.
670 if (!D->getDeclName())
671 return;
672
Douglas Gregorcc636682009-02-17 23:15:12 +0000673 // FIXME: This feels like a hack. Should DeclarationName support
674 // template-ids, or is there a better way to keep specializations
675 // from being visible?
676 if (isa<ClassTemplateSpecializationDecl>(D))
677 return;
678
Douglas Gregorc36c5402009-04-09 17:29:08 +0000679 if (!LookupPtr)
680 LookupPtr = new StoredDeclsMap;
Douglas Gregor44b43212008-12-11 16:49:14 +0000681
682 // Insert this declaration into the map.
Douglas Gregorc36c5402009-04-09 17:29:08 +0000683 StoredDeclsMap &Map = *static_cast<StoredDeclsMap*>(LookupPtr);
Chris Lattner67762a32009-02-20 01:44:05 +0000684 StoredDeclsList &DeclNameEntries = Map[D->getDeclName()];
685 if (DeclNameEntries.isNull()) {
686 DeclNameEntries.setOnlyValue(D);
Chris Lattnerbd6c8002009-02-19 07:00:44 +0000687 return;
Douglas Gregor44b43212008-12-11 16:49:14 +0000688 }
Chris Lattner91942502009-02-20 00:55:03 +0000689
Chris Lattnerbdc3d002009-02-20 01:10:07 +0000690 // If it is possible that this is a redeclaration, check to see if there is
691 // already a decl for which declarationReplaces returns true. If there is
692 // one, just replace it and return.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000693 if (DeclNameEntries.HandleRedeclaration(Context, D))
Chris Lattner67762a32009-02-20 01:44:05 +0000694 return;
Chris Lattner91942502009-02-20 00:55:03 +0000695
Chris Lattnerbd6c8002009-02-19 07:00:44 +0000696 // Put this declaration into the appropriate slot.
Chris Lattner67762a32009-02-20 01:44:05 +0000697 DeclNameEntries.AddSubsequentDecl(D);
Douglas Gregor44b43212008-12-11 16:49:14 +0000698}
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000699
700/// Returns iterator range [First, Last) of UsingDirectiveDecls stored within
701/// this context.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000702DeclContext::udir_iterator_range
703DeclContext::getUsingDirectives(ASTContext &Context) const {
704 lookup_const_result Result = lookup(Context, UsingDirectiveDecl::getName());
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000705 return udir_iterator_range(reinterpret_cast<udir_iterator>(Result.first),
706 reinterpret_cast<udir_iterator>(Result.second));
707}
Douglas Gregor2cf26342009-04-09 22:27:44 +0000708
709void StoredDeclsList::materializeDecls(ASTContext &Context) {
710 if (isNull())
711 return;
712
713 switch ((DataKind)(Data & 0x03)) {
714 case DK_Decl:
715 case DK_Decl_Vector:
716 break;
717
718 case DK_DeclID: {
719 // Resolve this declaration ID to an actual declaration by
720 // querying the external AST source.
721 unsigned DeclID = Data >> 2;
722
723 ExternalASTSource *Source = Context.getExternalSource();
724 assert(Source && "No external AST source available!");
725
726 Data = reinterpret_cast<uintptr_t>(Source->GetDecl(DeclID));
727 break;
728 }
729
730 case DK_ID_Vector: {
731 // We have a vector of declaration IDs. Resolve all of them to
732 // actual declarations.
733 VectorTy &Vector = *getAsVector();
734 ExternalASTSource *Source = Context.getExternalSource();
735 assert(Source && "No external AST source available!");
736
737 for (unsigned I = 0, N = Vector.size(); I != N; ++I)
738 Vector[I] = reinterpret_cast<uintptr_t>(Source->GetDecl(Vector[I]));
739
740 Data = (Data & ~0x03) | DK_Decl_Vector;
741 break;
742 }
743 }
744}