blob: 93bee84cff540cbe058cc1ea3a45d9ecf2d5850d [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
Douglas Gregore53060f2009-06-25 22:08:12 +000099bool Decl::isFunctionOrFunctionTemplate() const {
100 return isa<FunctionDecl>(this) || isa<FunctionTemplateDecl>(this);
101}
102
Eli Friedman56d29372008-06-07 16:52:53 +0000103//===----------------------------------------------------------------------===//
Chris Lattner49f28ca2009-03-05 08:00:35 +0000104// PrettyStackTraceDecl Implementation
105//===----------------------------------------------------------------------===//
106
107void PrettyStackTraceDecl::print(llvm::raw_ostream &OS) const {
108 SourceLocation TheLoc = Loc;
109 if (TheLoc.isInvalid() && TheDecl)
110 TheLoc = TheDecl->getLocation();
111
112 if (TheLoc.isValid()) {
113 TheLoc.print(OS, SM);
114 OS << ": ";
115 }
116
117 OS << Message;
118
119 if (NamedDecl *DN = dyn_cast_or_null<NamedDecl>(TheDecl))
120 OS << " '" << DN->getQualifiedNameAsString() << '\'';
121 OS << '\n';
122}
123
124//===----------------------------------------------------------------------===//
Eli Friedman56d29372008-06-07 16:52:53 +0000125// Decl Implementation
126//===----------------------------------------------------------------------===//
127
Chris Lattner769dbdf2009-03-27 20:18:19 +0000128// Out-of-line virtual method providing a home for Decl.
129Decl::~Decl() {
130 if (isOutOfSemaDC())
131 delete getMultipleDC();
132
133 assert(!HasAttrs && "attributes should have been freed by Destroy");
134}
135
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000136void Decl::setDeclContext(DeclContext *DC) {
137 if (isOutOfSemaDC())
138 delete getMultipleDC();
139
Chris Lattneree219fd2009-03-29 06:06:59 +0000140 DeclCtx = DC;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000141}
142
143void Decl::setLexicalDeclContext(DeclContext *DC) {
144 if (DC == getLexicalDeclContext())
145 return;
146
147 if (isInSemaDC()) {
148 MultipleDC *MDC = new MultipleDC();
149 MDC->SemanticDC = getDeclContext();
150 MDC->LexicalDC = DC;
Chris Lattneree219fd2009-03-29 06:06:59 +0000151 DeclCtx = MDC;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000152 } else {
153 getMultipleDC()->LexicalDC = DC;
154 }
155}
156
Chris Lattner769dbdf2009-03-27 20:18:19 +0000157unsigned Decl::getIdentifierNamespaceForKind(Kind DeclKind) {
158 switch (DeclKind) {
159 default:
160 if (DeclKind >= FunctionFirst && DeclKind <= FunctionLast)
161 return IDNS_Ordinary;
162 assert(0 && "Unknown decl kind!");
163 case OverloadedFunction:
164 case Typedef:
165 case EnumConstant:
166 case Var:
167 case ImplicitParam:
168 case ParmVar:
169 case OriginalParmVar:
170 case NonTypeTemplateParm:
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000171 case Using:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000172 case ObjCMethod:
173 case ObjCContainer:
174 case ObjCCategory:
175 case ObjCInterface:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000176 case ObjCProperty:
177 case ObjCCompatibleAlias:
178 return IDNS_Ordinary;
179
180 case ObjCProtocol:
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000181 return IDNS_ObjCProtocol;
Chris Lattner769dbdf2009-03-27 20:18:19 +0000182
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000183 case ObjCImplementation:
184 return IDNS_ObjCImplementation;
185
186 case ObjCCategoryImpl:
187 return IDNS_ObjCCategoryImpl;
188
Chris Lattner769dbdf2009-03-27 20:18:19 +0000189 case Field:
190 case ObjCAtDefsField:
191 case ObjCIvar:
192 return IDNS_Member;
193
194 case Record:
195 case CXXRecord:
196 case Enum:
197 case TemplateTypeParm:
198 return IDNS_Tag;
199
200 case Namespace:
201 case Template:
202 case FunctionTemplate:
203 case ClassTemplate:
204 case TemplateTemplateParm:
Anders Carlssonfaf0e872009-03-28 23:02:53 +0000205 case NamespaceAlias:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000206 return IDNS_Tag | IDNS_Ordinary;
207
208 // Never have names.
209 case LinkageSpec:
210 case FileScopeAsm:
211 case StaticAssert:
212 case ObjCClass:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000213 case ObjCPropertyImpl:
214 case ObjCForwardProtocol:
215 case Block:
216 case TranslationUnit:
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000217
Chris Lattner769dbdf2009-03-27 20:18:19 +0000218 // Aren't looked up?
219 case UsingDirective:
220 case ClassTemplateSpecialization:
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000221 case ClassTemplatePartialSpecialization:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000222 return 0;
223 }
Eli Friedman56d29372008-06-07 16:52:53 +0000224}
225
Douglas Gregor68584ed2009-06-18 16:11:24 +0000226void Decl::addAttr(ASTContext &Context, Attr *NewAttr) {
227 Attr *&ExistingAttr = Context.getDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000228
229 NewAttr->setNext(ExistingAttr);
230 ExistingAttr = NewAttr;
231
232 HasAttrs = true;
233}
234
Douglas Gregor68584ed2009-06-18 16:11:24 +0000235void Decl::invalidateAttrs(ASTContext &Context) {
Eli Friedman56d29372008-06-07 16:52:53 +0000236 if (!HasAttrs) return;
Douglas Gregor68584ed2009-06-18 16:11:24 +0000237
Eli Friedman56d29372008-06-07 16:52:53 +0000238 HasAttrs = false;
Douglas Gregor68584ed2009-06-18 16:11:24 +0000239 Context.eraseDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000240}
241
Douglas Gregor68584ed2009-06-18 16:11:24 +0000242const Attr *Decl::getAttrsImpl(ASTContext &Context) const {
Chris Lattner81abbdd2009-03-21 06:27:31 +0000243 assert(HasAttrs && "getAttrs() should verify this!");
Douglas Gregor68584ed2009-06-18 16:11:24 +0000244 return Context.getDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000245}
246
Douglas Gregor68584ed2009-06-18 16:11:24 +0000247void Decl::swapAttrs(ASTContext &Context, Decl *RHS) {
Eli Friedman56d29372008-06-07 16:52:53 +0000248 bool HasLHSAttr = this->HasAttrs;
249 bool HasRHSAttr = RHS->HasAttrs;
250
251 // Usually, neither decl has attrs, nothing to do.
252 if (!HasLHSAttr && !HasRHSAttr) return;
253
254 // If 'this' has no attrs, swap the other way.
255 if (!HasLHSAttr)
Douglas Gregor68584ed2009-06-18 16:11:24 +0000256 return RHS->swapAttrs(Context, this);
Eli Friedman56d29372008-06-07 16:52:53 +0000257
258 // Handle the case when both decls have attrs.
259 if (HasRHSAttr) {
Douglas Gregor68584ed2009-06-18 16:11:24 +0000260 std::swap(Context.getDeclAttrs(this), Context.getDeclAttrs(RHS));
Eli Friedman56d29372008-06-07 16:52:53 +0000261 return;
262 }
263
264 // Otherwise, LHS has an attr and RHS doesn't.
Douglas Gregor68584ed2009-06-18 16:11:24 +0000265 Context.getDeclAttrs(RHS) = Context.getDeclAttrs(this);
266 Context.eraseDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000267 this->HasAttrs = false;
268 RHS->HasAttrs = true;
269}
270
271
Chris Lattnercc581472009-03-04 06:05:19 +0000272void Decl::Destroy(ASTContext &C) {
273 // Free attributes for this decl.
274 if (HasAttrs) {
Douglas Gregor68584ed2009-06-18 16:11:24 +0000275 C.getDeclAttrs(this)->Destroy(C);
276 invalidateAttrs(C);
Chris Lattnercc581472009-03-04 06:05:19 +0000277 HasAttrs = false;
278 }
279
Douglas Gregora0fc55f2009-01-13 19:47:12 +0000280#if 0
Douglas Gregor00ad0ef2009-01-20 04:25:11 +0000281 // FIXME: Once ownership is fully understood, we can enable this code
282 if (DeclContext *DC = dyn_cast<DeclContext>(this))
283 DC->decls_begin()->Destroy(C);
Eli Friedman56d29372008-06-07 16:52:53 +0000284
Chris Lattner244a67d2009-03-28 06:04:26 +0000285 // Observe the unrolled recursion. By setting N->NextDeclInContext = 0x0
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000286 // within the loop, only the Destroy method for the first Decl
287 // will deallocate all of the Decls in a chain.
288
Chris Lattner244a67d2009-03-28 06:04:26 +0000289 Decl* N = getNextDeclInContext();
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000290
291 while (N) {
Chris Lattner244a67d2009-03-28 06:04:26 +0000292 Decl* Tmp = N->getNextDeclInContext();
293 N->NextDeclInContext = 0;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000294 N->Destroy(C);
295 N = Tmp;
Eli Friedman56d29372008-06-07 16:52:53 +0000296 }
Douglas Gregora0fc55f2009-01-13 19:47:12 +0000297
Eli Friedman56d29372008-06-07 16:52:53 +0000298 this->~Decl();
Steve Naroff3e970492009-01-27 21:25:57 +0000299 C.Deallocate((void *)this);
Douglas Gregor00ad0ef2009-01-20 04:25:11 +0000300#endif
Eli Friedman56d29372008-06-07 16:52:53 +0000301}
302
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000303Decl *Decl::castFromDeclContext (const DeclContext *D) {
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000304 Decl::Kind DK = D->getDeclKind();
305 switch(DK) {
306#define DECL_CONTEXT(Name) \
307 case Decl::Name: \
308 return static_cast<Name##Decl*>(const_cast<DeclContext*>(D));
309#define DECL_CONTEXT_BASE(Name)
310#include "clang/AST/DeclNodes.def"
311 default:
312#define DECL_CONTEXT_BASE(Name) \
313 if (DK >= Decl::Name##First && DK <= Decl::Name##Last) \
314 return static_cast<Name##Decl*>(const_cast<DeclContext*>(D));
315#include "clang/AST/DeclNodes.def"
316 assert(false && "a decl that inherits DeclContext isn't handled");
317 return 0;
318 }
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000319}
320
321DeclContext *Decl::castToDeclContext(const Decl *D) {
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000322 Decl::Kind DK = D->getKind();
323 switch(DK) {
324#define DECL_CONTEXT(Name) \
325 case Decl::Name: \
326 return static_cast<Name##Decl*>(const_cast<Decl*>(D));
327#define DECL_CONTEXT_BASE(Name)
328#include "clang/AST/DeclNodes.def"
329 default:
330#define DECL_CONTEXT_BASE(Name) \
331 if (DK >= Decl::Name##First && DK <= Decl::Name##Last) \
332 return static_cast<Name##Decl*>(const_cast<Decl*>(D));
333#include "clang/AST/DeclNodes.def"
334 assert(false && "a decl that inherits DeclContext isn't handled");
335 return 0;
336 }
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000337}
338
Sebastian Redld3a413d2009-04-26 20:35:05 +0000339CompoundStmt* Decl::getCompoundBody(ASTContext &Context) const {
340 return dyn_cast_or_null<CompoundStmt>(getBody(Context));
341}
342
343SourceLocation Decl::getBodyRBrace(ASTContext &Context) const {
344 Stmt *Body = getBody(Context);
345 if (!Body)
346 return SourceLocation();
347 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Body))
348 return CS->getRBracLoc();
349 assert(isa<CXXTryStmt>(Body) &&
350 "Body can only be CompoundStmt or CXXTryStmt");
351 return cast<CXXTryStmt>(Body)->getSourceRange().getEnd();
352}
353
Anders Carlsson1329c272009-03-25 23:38:06 +0000354#ifndef NDEBUG
355void Decl::CheckAccessDeclContext() const {
Douglas Gregor5c27f2b2009-04-07 20:58:25 +0000356 assert((Access != AS_none || isa<TranslationUnitDecl>(this) ||
357 !isa<CXXRecordDecl>(getDeclContext())) &&
Anders Carlsson1329c272009-03-25 23:38:06 +0000358 "Access specifier is AS_none inside a record decl");
359}
360
361#endif
362
Eli Friedman56d29372008-06-07 16:52:53 +0000363//===----------------------------------------------------------------------===//
364// DeclContext Implementation
365//===----------------------------------------------------------------------===//
366
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000367bool DeclContext::classof(const Decl *D) {
368 switch (D->getKind()) {
369#define DECL_CONTEXT(Name) case Decl::Name:
370#define DECL_CONTEXT_BASE(Name)
371#include "clang/AST/DeclNodes.def"
372 return true;
373 default:
374#define DECL_CONTEXT_BASE(Name) \
375 if (D->getKind() >= Decl::Name##First && \
376 D->getKind() <= Decl::Name##Last) \
377 return true;
378#include "clang/AST/DeclNodes.def"
379 return false;
380 }
381}
382
Douglas Gregor44b43212008-12-11 16:49:14 +0000383DeclContext::~DeclContext() {
Douglas Gregorc36c5402009-04-09 17:29:08 +0000384 delete static_cast<StoredDeclsMap*>(LookupPtr);
Douglas Gregor44b43212008-12-11 16:49:14 +0000385}
386
387void DeclContext::DestroyDecls(ASTContext &C) {
Douglas Gregor6ab35242009-04-09 21:40:53 +0000388 for (decl_iterator D = decls_begin(C); D != decls_end(C); )
Douglas Gregor00ad0ef2009-01-20 04:25:11 +0000389 (*D++)->Destroy(C);
Douglas Gregor44b43212008-12-11 16:49:14 +0000390}
391
Douglas Gregorbc221632009-05-28 16:34:51 +0000392bool DeclContext::isDependentContext() const {
393 if (isFileContext())
394 return false;
395
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000396 if (isa<ClassTemplatePartialSpecializationDecl>(this))
397 return true;
398
Douglas Gregorbc221632009-05-28 16:34:51 +0000399 if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this))
400 if (Record->getDescribedClassTemplate())
401 return true;
402
403 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this))
404 if (Function->getDescribedFunctionTemplate())
405 return true;
406
407 return getParent() && getParent()->isDependentContext();
408}
409
Douglas Gregor074149e2009-01-05 19:45:36 +0000410bool DeclContext::isTransparentContext() const {
411 if (DeclKind == Decl::Enum)
412 return true; // FIXME: Check for C++0x scoped enums
413 else if (DeclKind == Decl::LinkageSpec)
414 return true;
Douglas Gregor65100792009-02-26 00:02:51 +0000415 else if (DeclKind >= Decl::RecordFirst && DeclKind <= Decl::RecordLast)
Douglas Gregorbcbffc42009-01-07 00:43:41 +0000416 return cast<RecordDecl>(this)->isAnonymousStructOrUnion();
Douglas Gregor074149e2009-01-05 19:45:36 +0000417 else if (DeclKind == Decl::Namespace)
418 return false; // FIXME: Check for C++0x inline namespaces
419
420 return false;
421}
422
Steve Naroff0701bbb2009-01-08 17:28:14 +0000423DeclContext *DeclContext::getPrimaryContext() {
Douglas Gregor44b43212008-12-11 16:49:14 +0000424 switch (DeclKind) {
Douglas Gregor44b43212008-12-11 16:49:14 +0000425 case Decl::TranslationUnit:
Douglas Gregor074149e2009-01-05 19:45:36 +0000426 case Decl::LinkageSpec:
427 case Decl::Block:
Douglas Gregor44b43212008-12-11 16:49:14 +0000428 // There is only one DeclContext for these entities.
429 return this;
430
431 case Decl::Namespace:
432 // The original namespace is our primary context.
433 return static_cast<NamespaceDecl*>(this)->getOriginalNamespace();
434
Douglas Gregor44b43212008-12-11 16:49:14 +0000435 case Decl::ObjCMethod:
436 return this;
437
438 case Decl::ObjCInterface:
Steve Naroff0701bbb2009-01-08 17:28:14 +0000439 case Decl::ObjCProtocol:
440 case Decl::ObjCCategory:
Douglas Gregor44b43212008-12-11 16:49:14 +0000441 // FIXME: Can Objective-C interfaces be forward-declared?
442 return this;
443
Steve Naroff0701bbb2009-01-08 17:28:14 +0000444 case Decl::ObjCImplementation:
445 case Decl::ObjCCategoryImpl:
446 return this;
447
Douglas Gregor44b43212008-12-11 16:49:14 +0000448 default:
Douglas Gregorcc636682009-02-17 23:15:12 +0000449 if (DeclKind >= Decl::TagFirst && DeclKind <= Decl::TagLast) {
450 // If this is a tag type that has a definition or is currently
451 // being defined, that definition is our primary context.
Chris Lattner244a67d2009-03-28 06:04:26 +0000452 if (const TagType *TagT =cast<TagDecl>(this)->TypeForDecl->getAsTagType())
Douglas Gregorcc636682009-02-17 23:15:12 +0000453 if (TagT->isBeingDefined() ||
454 (TagT->getDecl() && TagT->getDecl()->isDefinition()))
455 return TagT->getDecl();
456 return this;
457 }
458
Douglas Gregor44b43212008-12-11 16:49:14 +0000459 assert(DeclKind >= Decl::FunctionFirst && DeclKind <= Decl::FunctionLast &&
460 "Unknown DeclContext kind");
461 return this;
462 }
463}
464
465DeclContext *DeclContext::getNextContext() {
466 switch (DeclKind) {
Douglas Gregor44b43212008-12-11 16:49:14 +0000467 case Decl::Namespace:
468 // Return the next namespace
469 return static_cast<NamespaceDecl*>(this)->getNextNamespace();
470
471 default:
Douglas Gregor44b43212008-12-11 16:49:14 +0000472 return 0;
473 }
474}
475
Douglas Gregor2cf26342009-04-09 22:27:44 +0000476/// \brief Load the declarations within this lexical storage from an
477/// external source.
478void
479DeclContext::LoadLexicalDeclsFromExternalStorage(ASTContext &Context) const {
480 ExternalASTSource *Source = Context.getExternalSource();
481 assert(hasExternalLexicalStorage() && Source && "No external storage?");
482
Eli Friedmanb0156ea2009-04-27 23:43:36 +0000483 llvm::SmallVector<uint32_t, 64> Decls;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000484 if (Source->ReadDeclsLexicallyInContext(const_cast<DeclContext *>(this),
485 Decls))
486 return;
487
488 // There is no longer any lexical storage in this context
489 ExternalLexicalStorage = false;
490
491 if (Decls.empty())
492 return;
493
494 // Resolve all of the declaration IDs into declarations, building up
495 // a chain of declarations via the Decl::NextDeclInContext field.
496 Decl *FirstNewDecl = 0;
497 Decl *PrevDecl = 0;
498 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
499 Decl *D = Source->GetDecl(Decls[I]);
500 if (PrevDecl)
501 PrevDecl->NextDeclInContext = D;
502 else
503 FirstNewDecl = D;
504
505 PrevDecl = D;
506 }
507
508 // Splice the newly-read declarations into the beginning of the list
509 // of declarations.
510 PrevDecl->NextDeclInContext = FirstDecl;
511 FirstDecl = FirstNewDecl;
512 if (!LastDecl)
513 LastDecl = PrevDecl;
514}
515
516void
517DeclContext::LoadVisibleDeclsFromExternalStorage(ASTContext &Context) const {
518 DeclContext *This = const_cast<DeclContext *>(this);
519 ExternalASTSource *Source = Context.getExternalSource();
520 assert(hasExternalVisibleStorage() && Source && "No external storage?");
521
522 llvm::SmallVector<VisibleDeclaration, 64> Decls;
523 if (Source->ReadDeclsVisibleInContext(This, Decls))
524 return;
525
526 // There is no longer any visible storage in this context
527 ExternalVisibleStorage = false;
528
529 // Load the declaration IDs for all of the names visible in this
530 // context.
531 assert(!LookupPtr && "Have a lookup map before de-serialization?");
532 StoredDeclsMap *Map = new StoredDeclsMap;
533 LookupPtr = Map;
534 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
535 (*Map)[Decls[I].Name].setFromDeclIDs(Decls[I].Declarations);
536 }
537}
538
Douglas Gregor6ab35242009-04-09 21:40:53 +0000539DeclContext::decl_iterator DeclContext::decls_begin(ASTContext &Context) const {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000540 if (hasExternalLexicalStorage())
541 LoadLexicalDeclsFromExternalStorage(Context);
542
543 // FIXME: Check whether we need to load some declarations from
544 // external storage.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000545 return decl_iterator(FirstDecl);
546}
547
548DeclContext::decl_iterator DeclContext::decls_end(ASTContext &Context) const {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000549 if (hasExternalLexicalStorage())
550 LoadLexicalDeclsFromExternalStorage(Context);
551
Douglas Gregor6ab35242009-04-09 21:40:53 +0000552 return decl_iterator();
553}
554
Douglas Gregor8038d512009-04-10 17:25:41 +0000555bool DeclContext::decls_empty(ASTContext &Context) const {
556 if (hasExternalLexicalStorage())
557 LoadLexicalDeclsFromExternalStorage(Context);
558
559 return !FirstDecl;
560}
561
Douglas Gregor6ab35242009-04-09 21:40:53 +0000562void DeclContext::addDecl(ASTContext &Context, Decl *D) {
Chris Lattner7f0be132009-02-20 00:56:18 +0000563 assert(D->getLexicalDeclContext() == this &&
564 "Decl inserted into wrong lexical context");
Chris Lattner244a67d2009-03-28 06:04:26 +0000565 assert(!D->getNextDeclInContext() && D != LastDecl &&
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000566 "Decl already inserted into a DeclContext");
567
568 if (FirstDecl) {
Chris Lattner244a67d2009-03-28 06:04:26 +0000569 LastDecl->NextDeclInContext = D;
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000570 LastDecl = D;
571 } else {
572 FirstDecl = LastDecl = D;
573 }
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000574
575 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
Douglas Gregor6ab35242009-04-09 21:40:53 +0000576 ND->getDeclContext()->makeDeclVisibleInContext(Context, ND);
Douglas Gregor44b43212008-12-11 16:49:14 +0000577}
578
Douglas Gregor074149e2009-01-05 19:45:36 +0000579/// buildLookup - Build the lookup data structure with all of the
580/// declarations in DCtx (and any other contexts linked to it or
581/// transparent contexts nested within it).
Douglas Gregor6ab35242009-04-09 21:40:53 +0000582void DeclContext::buildLookup(ASTContext &Context, DeclContext *DCtx) {
Douglas Gregor074149e2009-01-05 19:45:36 +0000583 for (; DCtx; DCtx = DCtx->getNextContext()) {
Douglas Gregor6ab35242009-04-09 21:40:53 +0000584 for (decl_iterator D = DCtx->decls_begin(Context),
585 DEnd = DCtx->decls_end(Context);
Douglas Gregor4f3b8f82009-01-06 07:17:58 +0000586 D != DEnd; ++D) {
Douglas Gregor074149e2009-01-05 19:45:36 +0000587 // Insert this declaration into the lookup structure
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000588 if (NamedDecl *ND = dyn_cast<NamedDecl>(*D))
Douglas Gregor6ab35242009-04-09 21:40:53 +0000589 makeDeclVisibleInContextImpl(Context, ND);
Douglas Gregor074149e2009-01-05 19:45:36 +0000590
591 // If this declaration is itself a transparent declaration context,
592 // add its members (recursively).
593 if (DeclContext *InnerCtx = dyn_cast<DeclContext>(*D))
594 if (InnerCtx->isTransparentContext())
Douglas Gregor6ab35242009-04-09 21:40:53 +0000595 buildLookup(Context, InnerCtx->getPrimaryContext());
Douglas Gregor074149e2009-01-05 19:45:36 +0000596 }
597 }
598}
599
Douglas Gregor44b43212008-12-11 16:49:14 +0000600DeclContext::lookup_result
Douglas Gregor6ab35242009-04-09 21:40:53 +0000601DeclContext::lookup(ASTContext &Context, DeclarationName Name) {
Steve Naroff0701bbb2009-01-08 17:28:14 +0000602 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor44b43212008-12-11 16:49:14 +0000603 if (PrimaryContext != this)
Douglas Gregor6ab35242009-04-09 21:40:53 +0000604 return PrimaryContext->lookup(Context, Name);
Douglas Gregor44b43212008-12-11 16:49:14 +0000605
Douglas Gregor2cf26342009-04-09 22:27:44 +0000606 if (hasExternalVisibleStorage())
607 LoadVisibleDeclsFromExternalStorage(Context);
608
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000609 /// If there is no lookup data structure, build one now by walking
Douglas Gregor44b43212008-12-11 16:49:14 +0000610 /// all of the linked DeclContexts (in declaration order!) and
611 /// inserting their values.
Douglas Gregorc36c5402009-04-09 17:29:08 +0000612 if (!LookupPtr) {
Douglas Gregor6ab35242009-04-09 21:40:53 +0000613 buildLookup(Context, this);
Douglas Gregor44b43212008-12-11 16:49:14 +0000614
Douglas Gregorc36c5402009-04-09 17:29:08 +0000615 if (!LookupPtr)
Chris Lattner91942502009-02-20 00:55:03 +0000616 return lookup_result(0, 0);
Douglas Gregorc36c5402009-04-09 17:29:08 +0000617 }
Douglas Gregor44b43212008-12-11 16:49:14 +0000618
Douglas Gregorc36c5402009-04-09 17:29:08 +0000619 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(LookupPtr);
620 StoredDeclsMap::iterator Pos = Map->find(Name);
621 if (Pos == Map->end())
622 return lookup_result(0, 0);
Douglas Gregor6ab35242009-04-09 21:40:53 +0000623 return Pos->second.getLookupResult(Context);
Douglas Gregor44b43212008-12-11 16:49:14 +0000624}
625
626DeclContext::lookup_const_result
Douglas Gregor6ab35242009-04-09 21:40:53 +0000627DeclContext::lookup(ASTContext &Context, DeclarationName Name) const {
628 return const_cast<DeclContext*>(this)->lookup(Context, Name);
Douglas Gregor44b43212008-12-11 16:49:14 +0000629}
630
Chris Lattner0cf2b192009-03-27 19:19:59 +0000631DeclContext *DeclContext::getLookupContext() {
632 DeclContext *Ctx = this;
Douglas Gregor72de6672009-01-08 20:45:30 +0000633 // Skip through transparent contexts.
Douglas Gregorce356072009-01-06 23:51:29 +0000634 while (Ctx->isTransparentContext())
635 Ctx = Ctx->getParent();
636 return Ctx;
637}
638
Douglas Gregor88b70942009-02-25 22:02:03 +0000639DeclContext *DeclContext::getEnclosingNamespaceContext() {
640 DeclContext *Ctx = this;
641 // Skip through non-namespace, non-translation-unit contexts.
642 while (!Ctx->isFileContext() || Ctx->isTransparentContext())
643 Ctx = Ctx->getParent();
644 return Ctx->getPrimaryContext();
645}
646
Douglas Gregor6ab35242009-04-09 21:40:53 +0000647void DeclContext::makeDeclVisibleInContext(ASTContext &Context, NamedDecl *D) {
Douglas Gregorcc636682009-02-17 23:15:12 +0000648 // FIXME: This feels like a hack. Should DeclarationName support
649 // template-ids, or is there a better way to keep specializations
650 // from being visible?
651 if (isa<ClassTemplateSpecializationDecl>(D))
652 return;
653
Steve Naroff0701bbb2009-01-08 17:28:14 +0000654 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor44b43212008-12-11 16:49:14 +0000655 if (PrimaryContext != this) {
Douglas Gregor6ab35242009-04-09 21:40:53 +0000656 PrimaryContext->makeDeclVisibleInContext(Context, D);
Douglas Gregor44b43212008-12-11 16:49:14 +0000657 return;
658 }
659
660 // If we already have a lookup data structure, perform the insertion
661 // into it. Otherwise, be lazy and don't build that structure until
662 // someone asks for it.
Douglas Gregorc36c5402009-04-09 17:29:08 +0000663 if (LookupPtr)
Douglas Gregor6ab35242009-04-09 21:40:53 +0000664 makeDeclVisibleInContextImpl(Context, D);
Douglas Gregor074149e2009-01-05 19:45:36 +0000665
Douglas Gregor074149e2009-01-05 19:45:36 +0000666 // If we are a transparent context, insert into our parent context,
667 // too. This operation is recursive.
668 if (isTransparentContext())
Douglas Gregor6ab35242009-04-09 21:40:53 +0000669 getParent()->makeDeclVisibleInContext(Context, D);
Douglas Gregor44b43212008-12-11 16:49:14 +0000670}
671
Douglas Gregor6ab35242009-04-09 21:40:53 +0000672void DeclContext::makeDeclVisibleInContextImpl(ASTContext &Context,
673 NamedDecl *D) {
Douglas Gregor074149e2009-01-05 19:45:36 +0000674 // Skip unnamed declarations.
675 if (!D->getDeclName())
676 return;
677
Douglas Gregorcc636682009-02-17 23:15:12 +0000678 // FIXME: This feels like a hack. Should DeclarationName support
679 // template-ids, or is there a better way to keep specializations
680 // from being visible?
681 if (isa<ClassTemplateSpecializationDecl>(D))
682 return;
683
Douglas Gregorc36c5402009-04-09 17:29:08 +0000684 if (!LookupPtr)
685 LookupPtr = new StoredDeclsMap;
Douglas Gregor44b43212008-12-11 16:49:14 +0000686
687 // Insert this declaration into the map.
Douglas Gregorc36c5402009-04-09 17:29:08 +0000688 StoredDeclsMap &Map = *static_cast<StoredDeclsMap*>(LookupPtr);
Chris Lattner67762a32009-02-20 01:44:05 +0000689 StoredDeclsList &DeclNameEntries = Map[D->getDeclName()];
690 if (DeclNameEntries.isNull()) {
691 DeclNameEntries.setOnlyValue(D);
Chris Lattnerbd6c8002009-02-19 07:00:44 +0000692 return;
Douglas Gregor44b43212008-12-11 16:49:14 +0000693 }
Chris Lattner91942502009-02-20 00:55:03 +0000694
Chris Lattnerbdc3d002009-02-20 01:10:07 +0000695 // If it is possible that this is a redeclaration, check to see if there is
696 // already a decl for which declarationReplaces returns true. If there is
697 // one, just replace it and return.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000698 if (DeclNameEntries.HandleRedeclaration(Context, D))
Chris Lattner67762a32009-02-20 01:44:05 +0000699 return;
Chris Lattner91942502009-02-20 00:55:03 +0000700
Chris Lattnerbd6c8002009-02-19 07:00:44 +0000701 // Put this declaration into the appropriate slot.
Chris Lattner67762a32009-02-20 01:44:05 +0000702 DeclNameEntries.AddSubsequentDecl(D);
Douglas Gregor44b43212008-12-11 16:49:14 +0000703}
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000704
705/// Returns iterator range [First, Last) of UsingDirectiveDecls stored within
706/// this context.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000707DeclContext::udir_iterator_range
708DeclContext::getUsingDirectives(ASTContext &Context) const {
709 lookup_const_result Result = lookup(Context, UsingDirectiveDecl::getName());
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000710 return udir_iterator_range(reinterpret_cast<udir_iterator>(Result.first),
711 reinterpret_cast<udir_iterator>(Result.second));
712}
Douglas Gregor2cf26342009-04-09 22:27:44 +0000713
714void StoredDeclsList::materializeDecls(ASTContext &Context) {
715 if (isNull())
716 return;
717
718 switch ((DataKind)(Data & 0x03)) {
719 case DK_Decl:
720 case DK_Decl_Vector:
721 break;
722
723 case DK_DeclID: {
724 // Resolve this declaration ID to an actual declaration by
725 // querying the external AST source.
726 unsigned DeclID = Data >> 2;
727
728 ExternalASTSource *Source = Context.getExternalSource();
729 assert(Source && "No external AST source available!");
730
731 Data = reinterpret_cast<uintptr_t>(Source->GetDecl(DeclID));
732 break;
733 }
734
735 case DK_ID_Vector: {
736 // We have a vector of declaration IDs. Resolve all of them to
737 // actual declarations.
738 VectorTy &Vector = *getAsVector();
739 ExternalASTSource *Source = Context.getExternalSource();
740 assert(Source && "No external AST source available!");
741
742 for (unsigned I = 0, N = Vector.size(); I != N; ++I)
743 Vector[I] = reinterpret_cast<uintptr_t>(Source->GetDecl(Vector[I]));
744
745 Data = (Data & ~0x03) | DK_Decl_Vector;
746 break;
747 }
748 }
749}