blob: 5815d820aef609ccbda77470ed40231a28968098 [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 {
Anders Carlsson58badb72009-06-26 05:26:50 +0000100 if (const UsingDecl *UD = dyn_cast<UsingDecl>(this))
101 return UD->getTargetDecl()->isFunctionOrFunctionTemplate();
102
Douglas Gregore53060f2009-06-25 22:08:12 +0000103 return isa<FunctionDecl>(this) || isa<FunctionTemplateDecl>(this);
104}
105
Eli Friedman56d29372008-06-07 16:52:53 +0000106//===----------------------------------------------------------------------===//
Chris Lattner49f28ca2009-03-05 08:00:35 +0000107// PrettyStackTraceDecl Implementation
108//===----------------------------------------------------------------------===//
109
110void PrettyStackTraceDecl::print(llvm::raw_ostream &OS) const {
111 SourceLocation TheLoc = Loc;
112 if (TheLoc.isInvalid() && TheDecl)
113 TheLoc = TheDecl->getLocation();
114
115 if (TheLoc.isValid()) {
116 TheLoc.print(OS, SM);
117 OS << ": ";
118 }
119
120 OS << Message;
121
122 if (NamedDecl *DN = dyn_cast_or_null<NamedDecl>(TheDecl))
123 OS << " '" << DN->getQualifiedNameAsString() << '\'';
124 OS << '\n';
125}
126
127//===----------------------------------------------------------------------===//
Eli Friedman56d29372008-06-07 16:52:53 +0000128// Decl Implementation
129//===----------------------------------------------------------------------===//
130
Chris Lattner769dbdf2009-03-27 20:18:19 +0000131// Out-of-line virtual method providing a home for Decl.
132Decl::~Decl() {
133 if (isOutOfSemaDC())
134 delete getMultipleDC();
135
136 assert(!HasAttrs && "attributes should have been freed by Destroy");
137}
138
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000139void Decl::setDeclContext(DeclContext *DC) {
140 if (isOutOfSemaDC())
141 delete getMultipleDC();
142
Chris Lattneree219fd2009-03-29 06:06:59 +0000143 DeclCtx = DC;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000144}
145
146void Decl::setLexicalDeclContext(DeclContext *DC) {
147 if (DC == getLexicalDeclContext())
148 return;
149
150 if (isInSemaDC()) {
151 MultipleDC *MDC = new MultipleDC();
152 MDC->SemanticDC = getDeclContext();
153 MDC->LexicalDC = DC;
Chris Lattneree219fd2009-03-29 06:06:59 +0000154 DeclCtx = MDC;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000155 } else {
156 getMultipleDC()->LexicalDC = DC;
157 }
158}
159
Chris Lattner769dbdf2009-03-27 20:18:19 +0000160unsigned Decl::getIdentifierNamespaceForKind(Kind DeclKind) {
161 switch (DeclKind) {
162 default:
163 if (DeclKind >= FunctionFirst && DeclKind <= FunctionLast)
164 return IDNS_Ordinary;
165 assert(0 && "Unknown decl kind!");
166 case OverloadedFunction:
167 case Typedef:
168 case EnumConstant:
169 case Var:
170 case ImplicitParam:
171 case ParmVar:
172 case OriginalParmVar:
173 case NonTypeTemplateParm:
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000174 case Using:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000175 case ObjCMethod:
176 case ObjCContainer:
177 case ObjCCategory:
178 case ObjCInterface:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000179 case ObjCProperty:
180 case ObjCCompatibleAlias:
181 return IDNS_Ordinary;
182
183 case ObjCProtocol:
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000184 return IDNS_ObjCProtocol;
Chris Lattner769dbdf2009-03-27 20:18:19 +0000185
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000186 case ObjCImplementation:
187 return IDNS_ObjCImplementation;
188
189 case ObjCCategoryImpl:
190 return IDNS_ObjCCategoryImpl;
191
Chris Lattner769dbdf2009-03-27 20:18:19 +0000192 case Field:
193 case ObjCAtDefsField:
194 case ObjCIvar:
195 return IDNS_Member;
196
197 case Record:
198 case CXXRecord:
199 case Enum:
200 case TemplateTypeParm:
201 return IDNS_Tag;
202
203 case Namespace:
204 case Template:
205 case FunctionTemplate:
206 case ClassTemplate:
207 case TemplateTemplateParm:
Anders Carlssonfaf0e872009-03-28 23:02:53 +0000208 case NamespaceAlias:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000209 return IDNS_Tag | IDNS_Ordinary;
210
211 // Never have names.
212 case LinkageSpec:
213 case FileScopeAsm:
214 case StaticAssert:
215 case ObjCClass:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000216 case ObjCPropertyImpl:
217 case ObjCForwardProtocol:
218 case Block:
219 case TranslationUnit:
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000220
Chris Lattner769dbdf2009-03-27 20:18:19 +0000221 // Aren't looked up?
222 case UsingDirective:
223 case ClassTemplateSpecialization:
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000224 case ClassTemplatePartialSpecialization:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000225 return 0;
226 }
Eli Friedman56d29372008-06-07 16:52:53 +0000227}
228
Douglas Gregor68584ed2009-06-18 16:11:24 +0000229void Decl::addAttr(ASTContext &Context, Attr *NewAttr) {
230 Attr *&ExistingAttr = Context.getDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000231
232 NewAttr->setNext(ExistingAttr);
233 ExistingAttr = NewAttr;
234
235 HasAttrs = true;
236}
237
Douglas Gregor68584ed2009-06-18 16:11:24 +0000238void Decl::invalidateAttrs(ASTContext &Context) {
Eli Friedman56d29372008-06-07 16:52:53 +0000239 if (!HasAttrs) return;
Douglas Gregor68584ed2009-06-18 16:11:24 +0000240
Eli Friedman56d29372008-06-07 16:52:53 +0000241 HasAttrs = false;
Douglas Gregor68584ed2009-06-18 16:11:24 +0000242 Context.eraseDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000243}
244
Douglas Gregor68584ed2009-06-18 16:11:24 +0000245const Attr *Decl::getAttrsImpl(ASTContext &Context) const {
Chris Lattner81abbdd2009-03-21 06:27:31 +0000246 assert(HasAttrs && "getAttrs() should verify this!");
Douglas Gregor68584ed2009-06-18 16:11:24 +0000247 return Context.getDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000248}
249
Douglas Gregor68584ed2009-06-18 16:11:24 +0000250void Decl::swapAttrs(ASTContext &Context, Decl *RHS) {
Eli Friedman56d29372008-06-07 16:52:53 +0000251 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)
Douglas Gregor68584ed2009-06-18 16:11:24 +0000259 return RHS->swapAttrs(Context, this);
Eli Friedman56d29372008-06-07 16:52:53 +0000260
261 // Handle the case when both decls have attrs.
262 if (HasRHSAttr) {
Douglas Gregor68584ed2009-06-18 16:11:24 +0000263 std::swap(Context.getDeclAttrs(this), Context.getDeclAttrs(RHS));
Eli Friedman56d29372008-06-07 16:52:53 +0000264 return;
265 }
266
267 // Otherwise, LHS has an attr and RHS doesn't.
Douglas Gregor68584ed2009-06-18 16:11:24 +0000268 Context.getDeclAttrs(RHS) = Context.getDeclAttrs(this);
269 Context.eraseDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000270 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) {
Douglas Gregor68584ed2009-06-18 16:11:24 +0000278 C.getDeclAttrs(this)->Destroy(C);
279 invalidateAttrs(C);
Chris Lattnercc581472009-03-04 06:05:19 +0000280 HasAttrs = false;
281 }
282
Douglas Gregora0fc55f2009-01-13 19:47:12 +0000283#if 0
Douglas Gregor00ad0ef2009-01-20 04:25:11 +0000284 // FIXME: Once ownership is fully understood, we can enable this code
285 if (DeclContext *DC = dyn_cast<DeclContext>(this))
286 DC->decls_begin()->Destroy(C);
Eli Friedman56d29372008-06-07 16:52:53 +0000287
Chris Lattner244a67d2009-03-28 06:04:26 +0000288 // Observe the unrolled recursion. By setting N->NextDeclInContext = 0x0
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000289 // within the loop, only the Destroy method for the first Decl
290 // will deallocate all of the Decls in a chain.
291
Chris Lattner244a67d2009-03-28 06:04:26 +0000292 Decl* N = getNextDeclInContext();
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000293
294 while (N) {
Chris Lattner244a67d2009-03-28 06:04:26 +0000295 Decl* Tmp = N->getNextDeclInContext();
296 N->NextDeclInContext = 0;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000297 N->Destroy(C);
298 N = Tmp;
Eli Friedman56d29372008-06-07 16:52:53 +0000299 }
Douglas Gregora0fc55f2009-01-13 19:47:12 +0000300
Eli Friedman56d29372008-06-07 16:52:53 +0000301 this->~Decl();
Steve Naroff3e970492009-01-27 21:25:57 +0000302 C.Deallocate((void *)this);
Douglas Gregor00ad0ef2009-01-20 04:25:11 +0000303#endif
Eli Friedman56d29372008-06-07 16:52:53 +0000304}
305
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000306Decl *Decl::castFromDeclContext (const DeclContext *D) {
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000307 Decl::Kind DK = D->getDeclKind();
308 switch(DK) {
309#define DECL_CONTEXT(Name) \
310 case Decl::Name: \
311 return static_cast<Name##Decl*>(const_cast<DeclContext*>(D));
312#define DECL_CONTEXT_BASE(Name)
313#include "clang/AST/DeclNodes.def"
314 default:
315#define DECL_CONTEXT_BASE(Name) \
316 if (DK >= Decl::Name##First && DK <= Decl::Name##Last) \
317 return static_cast<Name##Decl*>(const_cast<DeclContext*>(D));
318#include "clang/AST/DeclNodes.def"
319 assert(false && "a decl that inherits DeclContext isn't handled");
320 return 0;
321 }
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000322}
323
324DeclContext *Decl::castToDeclContext(const Decl *D) {
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000325 Decl::Kind DK = D->getKind();
326 switch(DK) {
327#define DECL_CONTEXT(Name) \
328 case Decl::Name: \
329 return static_cast<Name##Decl*>(const_cast<Decl*>(D));
330#define DECL_CONTEXT_BASE(Name)
331#include "clang/AST/DeclNodes.def"
332 default:
333#define DECL_CONTEXT_BASE(Name) \
334 if (DK >= Decl::Name##First && DK <= Decl::Name##Last) \
335 return static_cast<Name##Decl*>(const_cast<Decl*>(D));
336#include "clang/AST/DeclNodes.def"
337 assert(false && "a decl that inherits DeclContext isn't handled");
338 return 0;
339 }
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000340}
341
Sebastian Redld3a413d2009-04-26 20:35:05 +0000342CompoundStmt* Decl::getCompoundBody(ASTContext &Context) const {
343 return dyn_cast_or_null<CompoundStmt>(getBody(Context));
344}
345
346SourceLocation Decl::getBodyRBrace(ASTContext &Context) const {
347 Stmt *Body = getBody(Context);
348 if (!Body)
349 return SourceLocation();
350 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Body))
351 return CS->getRBracLoc();
352 assert(isa<CXXTryStmt>(Body) &&
353 "Body can only be CompoundStmt or CXXTryStmt");
354 return cast<CXXTryStmt>(Body)->getSourceRange().getEnd();
355}
356
Anders Carlsson1329c272009-03-25 23:38:06 +0000357#ifndef NDEBUG
358void Decl::CheckAccessDeclContext() const {
Douglas Gregor5c27f2b2009-04-07 20:58:25 +0000359 assert((Access != AS_none || isa<TranslationUnitDecl>(this) ||
360 !isa<CXXRecordDecl>(getDeclContext())) &&
Anders Carlsson1329c272009-03-25 23:38:06 +0000361 "Access specifier is AS_none inside a record decl");
362}
363
364#endif
365
Eli Friedman56d29372008-06-07 16:52:53 +0000366//===----------------------------------------------------------------------===//
367// DeclContext Implementation
368//===----------------------------------------------------------------------===//
369
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000370bool DeclContext::classof(const Decl *D) {
371 switch (D->getKind()) {
372#define DECL_CONTEXT(Name) case Decl::Name:
373#define DECL_CONTEXT_BASE(Name)
374#include "clang/AST/DeclNodes.def"
375 return true;
376 default:
377#define DECL_CONTEXT_BASE(Name) \
378 if (D->getKind() >= Decl::Name##First && \
379 D->getKind() <= Decl::Name##Last) \
380 return true;
381#include "clang/AST/DeclNodes.def"
382 return false;
383 }
384}
385
Douglas Gregor44b43212008-12-11 16:49:14 +0000386DeclContext::~DeclContext() {
Douglas Gregorc36c5402009-04-09 17:29:08 +0000387 delete static_cast<StoredDeclsMap*>(LookupPtr);
Douglas Gregor44b43212008-12-11 16:49:14 +0000388}
389
390void DeclContext::DestroyDecls(ASTContext &C) {
Douglas Gregor6ab35242009-04-09 21:40:53 +0000391 for (decl_iterator D = decls_begin(C); D != decls_end(C); )
Douglas Gregor00ad0ef2009-01-20 04:25:11 +0000392 (*D++)->Destroy(C);
Douglas Gregor44b43212008-12-11 16:49:14 +0000393}
394
Douglas Gregorbc221632009-05-28 16:34:51 +0000395bool DeclContext::isDependentContext() const {
396 if (isFileContext())
397 return false;
398
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000399 if (isa<ClassTemplatePartialSpecializationDecl>(this))
400 return true;
401
Douglas Gregorbc221632009-05-28 16:34:51 +0000402 if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this))
403 if (Record->getDescribedClassTemplate())
404 return true;
405
406 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this))
407 if (Function->getDescribedFunctionTemplate())
408 return true;
409
410 return getParent() && getParent()->isDependentContext();
411}
412
Douglas Gregor074149e2009-01-05 19:45:36 +0000413bool DeclContext::isTransparentContext() const {
414 if (DeclKind == Decl::Enum)
415 return true; // FIXME: Check for C++0x scoped enums
416 else if (DeclKind == Decl::LinkageSpec)
417 return true;
Douglas Gregor65100792009-02-26 00:02:51 +0000418 else if (DeclKind >= Decl::RecordFirst && DeclKind <= Decl::RecordLast)
Douglas Gregorbcbffc42009-01-07 00:43:41 +0000419 return cast<RecordDecl>(this)->isAnonymousStructOrUnion();
Douglas Gregor074149e2009-01-05 19:45:36 +0000420 else if (DeclKind == Decl::Namespace)
421 return false; // FIXME: Check for C++0x inline namespaces
422
423 return false;
424}
425
Steve Naroff0701bbb2009-01-08 17:28:14 +0000426DeclContext *DeclContext::getPrimaryContext() {
Douglas Gregor44b43212008-12-11 16:49:14 +0000427 switch (DeclKind) {
Douglas Gregor44b43212008-12-11 16:49:14 +0000428 case Decl::TranslationUnit:
Douglas Gregor074149e2009-01-05 19:45:36 +0000429 case Decl::LinkageSpec:
430 case Decl::Block:
Douglas Gregor44b43212008-12-11 16:49:14 +0000431 // There is only one DeclContext for these entities.
432 return this;
433
434 case Decl::Namespace:
435 // The original namespace is our primary context.
436 return static_cast<NamespaceDecl*>(this)->getOriginalNamespace();
437
Douglas Gregor44b43212008-12-11 16:49:14 +0000438 case Decl::ObjCMethod:
439 return this;
440
441 case Decl::ObjCInterface:
Steve Naroff0701bbb2009-01-08 17:28:14 +0000442 case Decl::ObjCProtocol:
443 case Decl::ObjCCategory:
Douglas Gregor44b43212008-12-11 16:49:14 +0000444 // FIXME: Can Objective-C interfaces be forward-declared?
445 return this;
446
Steve Naroff0701bbb2009-01-08 17:28:14 +0000447 case Decl::ObjCImplementation:
448 case Decl::ObjCCategoryImpl:
449 return this;
450
Douglas Gregor44b43212008-12-11 16:49:14 +0000451 default:
Douglas Gregorcc636682009-02-17 23:15:12 +0000452 if (DeclKind >= Decl::TagFirst && DeclKind <= Decl::TagLast) {
453 // If this is a tag type that has a definition or is currently
454 // being defined, that definition is our primary context.
Chris Lattner244a67d2009-03-28 06:04:26 +0000455 if (const TagType *TagT =cast<TagDecl>(this)->TypeForDecl->getAsTagType())
Douglas Gregorcc636682009-02-17 23:15:12 +0000456 if (TagT->isBeingDefined() ||
457 (TagT->getDecl() && TagT->getDecl()->isDefinition()))
458 return TagT->getDecl();
459 return this;
460 }
461
Douglas Gregor44b43212008-12-11 16:49:14 +0000462 assert(DeclKind >= Decl::FunctionFirst && DeclKind <= Decl::FunctionLast &&
463 "Unknown DeclContext kind");
464 return this;
465 }
466}
467
468DeclContext *DeclContext::getNextContext() {
469 switch (DeclKind) {
Douglas Gregor44b43212008-12-11 16:49:14 +0000470 case Decl::Namespace:
471 // Return the next namespace
472 return static_cast<NamespaceDecl*>(this)->getNextNamespace();
473
474 default:
Douglas Gregor44b43212008-12-11 16:49:14 +0000475 return 0;
476 }
477}
478
Douglas Gregor2cf26342009-04-09 22:27:44 +0000479/// \brief Load the declarations within this lexical storage from an
480/// external source.
481void
482DeclContext::LoadLexicalDeclsFromExternalStorage(ASTContext &Context) const {
483 ExternalASTSource *Source = Context.getExternalSource();
484 assert(hasExternalLexicalStorage() && Source && "No external storage?");
485
Eli Friedmanb0156ea2009-04-27 23:43:36 +0000486 llvm::SmallVector<uint32_t, 64> Decls;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000487 if (Source->ReadDeclsLexicallyInContext(const_cast<DeclContext *>(this),
488 Decls))
489 return;
490
491 // There is no longer any lexical storage in this context
492 ExternalLexicalStorage = false;
493
494 if (Decls.empty())
495 return;
496
497 // Resolve all of the declaration IDs into declarations, building up
498 // a chain of declarations via the Decl::NextDeclInContext field.
499 Decl *FirstNewDecl = 0;
500 Decl *PrevDecl = 0;
501 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
502 Decl *D = Source->GetDecl(Decls[I]);
503 if (PrevDecl)
504 PrevDecl->NextDeclInContext = D;
505 else
506 FirstNewDecl = D;
507
508 PrevDecl = D;
509 }
510
511 // Splice the newly-read declarations into the beginning of the list
512 // of declarations.
513 PrevDecl->NextDeclInContext = FirstDecl;
514 FirstDecl = FirstNewDecl;
515 if (!LastDecl)
516 LastDecl = PrevDecl;
517}
518
519void
520DeclContext::LoadVisibleDeclsFromExternalStorage(ASTContext &Context) const {
521 DeclContext *This = const_cast<DeclContext *>(this);
522 ExternalASTSource *Source = Context.getExternalSource();
523 assert(hasExternalVisibleStorage() && Source && "No external storage?");
524
525 llvm::SmallVector<VisibleDeclaration, 64> Decls;
526 if (Source->ReadDeclsVisibleInContext(This, Decls))
527 return;
528
529 // There is no longer any visible storage in this context
530 ExternalVisibleStorage = false;
531
532 // Load the declaration IDs for all of the names visible in this
533 // context.
534 assert(!LookupPtr && "Have a lookup map before de-serialization?");
535 StoredDeclsMap *Map = new StoredDeclsMap;
536 LookupPtr = Map;
537 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
538 (*Map)[Decls[I].Name].setFromDeclIDs(Decls[I].Declarations);
539 }
540}
541
Douglas Gregor6ab35242009-04-09 21:40:53 +0000542DeclContext::decl_iterator DeclContext::decls_begin(ASTContext &Context) const {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000543 if (hasExternalLexicalStorage())
544 LoadLexicalDeclsFromExternalStorage(Context);
545
546 // FIXME: Check whether we need to load some declarations from
547 // external storage.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000548 return decl_iterator(FirstDecl);
549}
550
551DeclContext::decl_iterator DeclContext::decls_end(ASTContext &Context) const {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000552 if (hasExternalLexicalStorage())
553 LoadLexicalDeclsFromExternalStorage(Context);
554
Douglas Gregor6ab35242009-04-09 21:40:53 +0000555 return decl_iterator();
556}
557
Douglas Gregor8038d512009-04-10 17:25:41 +0000558bool DeclContext::decls_empty(ASTContext &Context) const {
559 if (hasExternalLexicalStorage())
560 LoadLexicalDeclsFromExternalStorage(Context);
561
562 return !FirstDecl;
563}
564
Douglas Gregor6ab35242009-04-09 21:40:53 +0000565void DeclContext::addDecl(ASTContext &Context, Decl *D) {
Chris Lattner7f0be132009-02-20 00:56:18 +0000566 assert(D->getLexicalDeclContext() == this &&
567 "Decl inserted into wrong lexical context");
Chris Lattner244a67d2009-03-28 06:04:26 +0000568 assert(!D->getNextDeclInContext() && D != LastDecl &&
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000569 "Decl already inserted into a DeclContext");
570
571 if (FirstDecl) {
Chris Lattner244a67d2009-03-28 06:04:26 +0000572 LastDecl->NextDeclInContext = D;
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000573 LastDecl = D;
574 } else {
575 FirstDecl = LastDecl = D;
576 }
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000577
578 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
Douglas Gregor6ab35242009-04-09 21:40:53 +0000579 ND->getDeclContext()->makeDeclVisibleInContext(Context, ND);
Douglas Gregor44b43212008-12-11 16:49:14 +0000580}
581
Douglas Gregor074149e2009-01-05 19:45:36 +0000582/// buildLookup - Build the lookup data structure with all of the
583/// declarations in DCtx (and any other contexts linked to it or
584/// transparent contexts nested within it).
Douglas Gregor6ab35242009-04-09 21:40:53 +0000585void DeclContext::buildLookup(ASTContext &Context, DeclContext *DCtx) {
Douglas Gregor074149e2009-01-05 19:45:36 +0000586 for (; DCtx; DCtx = DCtx->getNextContext()) {
Douglas Gregor6ab35242009-04-09 21:40:53 +0000587 for (decl_iterator D = DCtx->decls_begin(Context),
588 DEnd = DCtx->decls_end(Context);
Douglas Gregor4f3b8f82009-01-06 07:17:58 +0000589 D != DEnd; ++D) {
Douglas Gregor074149e2009-01-05 19:45:36 +0000590 // Insert this declaration into the lookup structure
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000591 if (NamedDecl *ND = dyn_cast<NamedDecl>(*D))
Douglas Gregor6ab35242009-04-09 21:40:53 +0000592 makeDeclVisibleInContextImpl(Context, ND);
Douglas Gregor074149e2009-01-05 19:45:36 +0000593
594 // If this declaration is itself a transparent declaration context,
595 // add its members (recursively).
596 if (DeclContext *InnerCtx = dyn_cast<DeclContext>(*D))
597 if (InnerCtx->isTransparentContext())
Douglas Gregor6ab35242009-04-09 21:40:53 +0000598 buildLookup(Context, InnerCtx->getPrimaryContext());
Douglas Gregor074149e2009-01-05 19:45:36 +0000599 }
600 }
601}
602
Douglas Gregor44b43212008-12-11 16:49:14 +0000603DeclContext::lookup_result
Douglas Gregor6ab35242009-04-09 21:40:53 +0000604DeclContext::lookup(ASTContext &Context, DeclarationName Name) {
Steve Naroff0701bbb2009-01-08 17:28:14 +0000605 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor44b43212008-12-11 16:49:14 +0000606 if (PrimaryContext != this)
Douglas Gregor6ab35242009-04-09 21:40:53 +0000607 return PrimaryContext->lookup(Context, Name);
Douglas Gregor44b43212008-12-11 16:49:14 +0000608
Douglas Gregor2cf26342009-04-09 22:27:44 +0000609 if (hasExternalVisibleStorage())
610 LoadVisibleDeclsFromExternalStorage(Context);
611
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000612 /// If there is no lookup data structure, build one now by walking
Douglas Gregor44b43212008-12-11 16:49:14 +0000613 /// all of the linked DeclContexts (in declaration order!) and
614 /// inserting their values.
Douglas Gregorc36c5402009-04-09 17:29:08 +0000615 if (!LookupPtr) {
Douglas Gregor6ab35242009-04-09 21:40:53 +0000616 buildLookup(Context, this);
Douglas Gregor44b43212008-12-11 16:49:14 +0000617
Douglas Gregorc36c5402009-04-09 17:29:08 +0000618 if (!LookupPtr)
Chris Lattner91942502009-02-20 00:55:03 +0000619 return lookup_result(0, 0);
Douglas Gregorc36c5402009-04-09 17:29:08 +0000620 }
Douglas Gregor44b43212008-12-11 16:49:14 +0000621
Douglas Gregorc36c5402009-04-09 17:29:08 +0000622 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(LookupPtr);
623 StoredDeclsMap::iterator Pos = Map->find(Name);
624 if (Pos == Map->end())
625 return lookup_result(0, 0);
Douglas Gregor6ab35242009-04-09 21:40:53 +0000626 return Pos->second.getLookupResult(Context);
Douglas Gregor44b43212008-12-11 16:49:14 +0000627}
628
629DeclContext::lookup_const_result
Douglas Gregor6ab35242009-04-09 21:40:53 +0000630DeclContext::lookup(ASTContext &Context, DeclarationName Name) const {
631 return const_cast<DeclContext*>(this)->lookup(Context, Name);
Douglas Gregor44b43212008-12-11 16:49:14 +0000632}
633
Chris Lattner0cf2b192009-03-27 19:19:59 +0000634DeclContext *DeclContext::getLookupContext() {
635 DeclContext *Ctx = this;
Douglas Gregor72de6672009-01-08 20:45:30 +0000636 // Skip through transparent contexts.
Douglas Gregorce356072009-01-06 23:51:29 +0000637 while (Ctx->isTransparentContext())
638 Ctx = Ctx->getParent();
639 return Ctx;
640}
641
Douglas Gregor88b70942009-02-25 22:02:03 +0000642DeclContext *DeclContext::getEnclosingNamespaceContext() {
643 DeclContext *Ctx = this;
644 // Skip through non-namespace, non-translation-unit contexts.
645 while (!Ctx->isFileContext() || Ctx->isTransparentContext())
646 Ctx = Ctx->getParent();
647 return Ctx->getPrimaryContext();
648}
649
Douglas Gregor6ab35242009-04-09 21:40:53 +0000650void DeclContext::makeDeclVisibleInContext(ASTContext &Context, NamedDecl *D) {
Douglas Gregorcc636682009-02-17 23:15:12 +0000651 // FIXME: This feels like a hack. Should DeclarationName support
652 // template-ids, or is there a better way to keep specializations
653 // from being visible?
654 if (isa<ClassTemplateSpecializationDecl>(D))
655 return;
656
Steve Naroff0701bbb2009-01-08 17:28:14 +0000657 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor44b43212008-12-11 16:49:14 +0000658 if (PrimaryContext != this) {
Douglas Gregor6ab35242009-04-09 21:40:53 +0000659 PrimaryContext->makeDeclVisibleInContext(Context, D);
Douglas Gregor44b43212008-12-11 16:49:14 +0000660 return;
661 }
662
663 // If we already have a lookup data structure, perform the insertion
664 // into it. Otherwise, be lazy and don't build that structure until
665 // someone asks for it.
Douglas Gregorc36c5402009-04-09 17:29:08 +0000666 if (LookupPtr)
Douglas Gregor6ab35242009-04-09 21:40:53 +0000667 makeDeclVisibleInContextImpl(Context, D);
Douglas Gregor074149e2009-01-05 19:45:36 +0000668
Douglas Gregor074149e2009-01-05 19:45:36 +0000669 // If we are a transparent context, insert into our parent context,
670 // too. This operation is recursive.
671 if (isTransparentContext())
Douglas Gregor6ab35242009-04-09 21:40:53 +0000672 getParent()->makeDeclVisibleInContext(Context, D);
Douglas Gregor44b43212008-12-11 16:49:14 +0000673}
674
Douglas Gregor6ab35242009-04-09 21:40:53 +0000675void DeclContext::makeDeclVisibleInContextImpl(ASTContext &Context,
676 NamedDecl *D) {
Douglas Gregor074149e2009-01-05 19:45:36 +0000677 // Skip unnamed declarations.
678 if (!D->getDeclName())
679 return;
680
Douglas Gregorcc636682009-02-17 23:15:12 +0000681 // FIXME: This feels like a hack. Should DeclarationName support
682 // template-ids, or is there a better way to keep specializations
683 // from being visible?
684 if (isa<ClassTemplateSpecializationDecl>(D))
685 return;
686
Douglas Gregorc36c5402009-04-09 17:29:08 +0000687 if (!LookupPtr)
688 LookupPtr = new StoredDeclsMap;
Douglas Gregor44b43212008-12-11 16:49:14 +0000689
690 // Insert this declaration into the map.
Douglas Gregorc36c5402009-04-09 17:29:08 +0000691 StoredDeclsMap &Map = *static_cast<StoredDeclsMap*>(LookupPtr);
Chris Lattner67762a32009-02-20 01:44:05 +0000692 StoredDeclsList &DeclNameEntries = Map[D->getDeclName()];
693 if (DeclNameEntries.isNull()) {
694 DeclNameEntries.setOnlyValue(D);
Chris Lattnerbd6c8002009-02-19 07:00:44 +0000695 return;
Douglas Gregor44b43212008-12-11 16:49:14 +0000696 }
Chris Lattner91942502009-02-20 00:55:03 +0000697
Chris Lattnerbdc3d002009-02-20 01:10:07 +0000698 // If it is possible that this is a redeclaration, check to see if there is
699 // already a decl for which declarationReplaces returns true. If there is
700 // one, just replace it and return.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000701 if (DeclNameEntries.HandleRedeclaration(Context, D))
Chris Lattner67762a32009-02-20 01:44:05 +0000702 return;
Chris Lattner91942502009-02-20 00:55:03 +0000703
Chris Lattnerbd6c8002009-02-19 07:00:44 +0000704 // Put this declaration into the appropriate slot.
Chris Lattner67762a32009-02-20 01:44:05 +0000705 DeclNameEntries.AddSubsequentDecl(D);
Douglas Gregor44b43212008-12-11 16:49:14 +0000706}
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000707
708/// Returns iterator range [First, Last) of UsingDirectiveDecls stored within
709/// this context.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000710DeclContext::udir_iterator_range
711DeclContext::getUsingDirectives(ASTContext &Context) const {
712 lookup_const_result Result = lookup(Context, UsingDirectiveDecl::getName());
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000713 return udir_iterator_range(reinterpret_cast<udir_iterator>(Result.first),
714 reinterpret_cast<udir_iterator>(Result.second));
715}
Douglas Gregor2cf26342009-04-09 22:27:44 +0000716
717void StoredDeclsList::materializeDecls(ASTContext &Context) {
718 if (isNull())
719 return;
720
721 switch ((DataKind)(Data & 0x03)) {
722 case DK_Decl:
723 case DK_Decl_Vector:
724 break;
725
726 case DK_DeclID: {
727 // Resolve this declaration ID to an actual declaration by
728 // querying the external AST source.
729 unsigned DeclID = Data >> 2;
730
731 ExternalASTSource *Source = Context.getExternalSource();
732 assert(Source && "No external AST source available!");
733
734 Data = reinterpret_cast<uintptr_t>(Source->GetDecl(DeclID));
735 break;
736 }
737
738 case DK_ID_Vector: {
739 // We have a vector of declaration IDs. Resolve all of them to
740 // actual declarations.
741 VectorTy &Vector = *getAsVector();
742 ExternalASTSource *Source = Context.getExternalSource();
743 assert(Source && "No external AST source available!");
744
745 for (unsigned I = 0, N = Vector.size(); I != N; ++I)
746 Vector[I] = reinterpret_cast<uintptr_t>(Source->GetDecl(Vector[I]));
747
748 Data = (Data & ~0x03) | DK_Decl_Vector;
749 break;
750 }
751 }
752}