blob: d872eae0845b277e614d6b3b26be242c161374c8 [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
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000160TranslationUnitDecl *Decl::getTranslationUnitDecl() {
161 DeclContext *DC = getDeclContext();
162 assert(DC && "This decl is not contained in a translation unit!");
163
164 while (!DC->isTranslationUnit()) {
165 DC = DC->getParent();
166 assert(DC && "This decl is not contained in a translation unit!");
167 }
168
169 return cast<TranslationUnitDecl>(DC);
170}
171
172ASTContext &Decl::getASTContext() const {
173 return getTranslationUnitDecl()->getASTContext();
174}
175
Chris Lattner769dbdf2009-03-27 20:18:19 +0000176unsigned Decl::getIdentifierNamespaceForKind(Kind DeclKind) {
177 switch (DeclKind) {
178 default:
179 if (DeclKind >= FunctionFirst && DeclKind <= FunctionLast)
180 return IDNS_Ordinary;
181 assert(0 && "Unknown decl kind!");
182 case OverloadedFunction:
183 case Typedef:
184 case EnumConstant:
185 case Var:
186 case ImplicitParam:
187 case ParmVar:
188 case OriginalParmVar:
189 case NonTypeTemplateParm:
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000190 case Using:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000191 case ObjCMethod:
192 case ObjCContainer:
193 case ObjCCategory:
194 case ObjCInterface:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000195 case ObjCProperty:
196 case ObjCCompatibleAlias:
197 return IDNS_Ordinary;
198
199 case ObjCProtocol:
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000200 return IDNS_ObjCProtocol;
Chris Lattner769dbdf2009-03-27 20:18:19 +0000201
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000202 case ObjCImplementation:
203 return IDNS_ObjCImplementation;
204
205 case ObjCCategoryImpl:
206 return IDNS_ObjCCategoryImpl;
207
Chris Lattner769dbdf2009-03-27 20:18:19 +0000208 case Field:
209 case ObjCAtDefsField:
210 case ObjCIvar:
211 return IDNS_Member;
212
213 case Record:
214 case CXXRecord:
215 case Enum:
216 case TemplateTypeParm:
217 return IDNS_Tag;
218
219 case Namespace:
220 case Template:
221 case FunctionTemplate:
222 case ClassTemplate:
223 case TemplateTemplateParm:
Anders Carlssonfaf0e872009-03-28 23:02:53 +0000224 case NamespaceAlias:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000225 return IDNS_Tag | IDNS_Ordinary;
226
227 // Never have names.
228 case LinkageSpec:
229 case FileScopeAsm:
230 case StaticAssert:
231 case ObjCClass:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000232 case ObjCPropertyImpl:
233 case ObjCForwardProtocol:
234 case Block:
235 case TranslationUnit:
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000236
Chris Lattner769dbdf2009-03-27 20:18:19 +0000237 // Aren't looked up?
238 case UsingDirective:
239 case ClassTemplateSpecialization:
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000240 case ClassTemplatePartialSpecialization:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000241 return 0;
242 }
Eli Friedman56d29372008-06-07 16:52:53 +0000243}
244
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000245void Decl::addAttr(Attr *NewAttr) {
246 Attr *&ExistingAttr = getASTContext().getDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000247
248 NewAttr->setNext(ExistingAttr);
249 ExistingAttr = NewAttr;
250
251 HasAttrs = true;
252}
253
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000254void Decl::invalidateAttrs() {
Eli Friedman56d29372008-06-07 16:52:53 +0000255 if (!HasAttrs) return;
Douglas Gregor68584ed2009-06-18 16:11:24 +0000256
Eli Friedman56d29372008-06-07 16:52:53 +0000257 HasAttrs = false;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000258 getASTContext().eraseDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000259}
260
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000261const Attr *Decl::getAttrsImpl() const {
Chris Lattner81abbdd2009-03-21 06:27:31 +0000262 assert(HasAttrs && "getAttrs() should verify this!");
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000263 return getASTContext().getDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000264}
265
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000266void Decl::swapAttrs(Decl *RHS) {
Eli Friedman56d29372008-06-07 16:52:53 +0000267 bool HasLHSAttr = this->HasAttrs;
268 bool HasRHSAttr = RHS->HasAttrs;
269
270 // Usually, neither decl has attrs, nothing to do.
271 if (!HasLHSAttr && !HasRHSAttr) return;
272
273 // If 'this' has no attrs, swap the other way.
274 if (!HasLHSAttr)
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000275 return RHS->swapAttrs(this);
276
277 ASTContext &Context = getASTContext();
Eli Friedman56d29372008-06-07 16:52:53 +0000278
279 // Handle the case when both decls have attrs.
280 if (HasRHSAttr) {
Douglas Gregor68584ed2009-06-18 16:11:24 +0000281 std::swap(Context.getDeclAttrs(this), Context.getDeclAttrs(RHS));
Eli Friedman56d29372008-06-07 16:52:53 +0000282 return;
283 }
284
285 // Otherwise, LHS has an attr and RHS doesn't.
Douglas Gregor68584ed2009-06-18 16:11:24 +0000286 Context.getDeclAttrs(RHS) = Context.getDeclAttrs(this);
287 Context.eraseDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000288 this->HasAttrs = false;
289 RHS->HasAttrs = true;
290}
291
292
Chris Lattnercc581472009-03-04 06:05:19 +0000293void Decl::Destroy(ASTContext &C) {
294 // Free attributes for this decl.
295 if (HasAttrs) {
Douglas Gregor68584ed2009-06-18 16:11:24 +0000296 C.getDeclAttrs(this)->Destroy(C);
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000297 invalidateAttrs();
Chris Lattnercc581472009-03-04 06:05:19 +0000298 HasAttrs = false;
299 }
300
Douglas Gregora0fc55f2009-01-13 19:47:12 +0000301#if 0
Douglas Gregor00ad0ef2009-01-20 04:25:11 +0000302 // FIXME: Once ownership is fully understood, we can enable this code
303 if (DeclContext *DC = dyn_cast<DeclContext>(this))
304 DC->decls_begin()->Destroy(C);
Eli Friedman56d29372008-06-07 16:52:53 +0000305
Chris Lattner244a67d2009-03-28 06:04:26 +0000306 // Observe the unrolled recursion. By setting N->NextDeclInContext = 0x0
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000307 // within the loop, only the Destroy method for the first Decl
308 // will deallocate all of the Decls in a chain.
309
Chris Lattner244a67d2009-03-28 06:04:26 +0000310 Decl* N = getNextDeclInContext();
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000311
312 while (N) {
Chris Lattner244a67d2009-03-28 06:04:26 +0000313 Decl* Tmp = N->getNextDeclInContext();
314 N->NextDeclInContext = 0;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000315 N->Destroy(C);
316 N = Tmp;
Eli Friedman56d29372008-06-07 16:52:53 +0000317 }
Douglas Gregora0fc55f2009-01-13 19:47:12 +0000318
Eli Friedman56d29372008-06-07 16:52:53 +0000319 this->~Decl();
Steve Naroff3e970492009-01-27 21:25:57 +0000320 C.Deallocate((void *)this);
Douglas Gregor00ad0ef2009-01-20 04:25:11 +0000321#endif
Eli Friedman56d29372008-06-07 16:52:53 +0000322}
323
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000324Decl *Decl::castFromDeclContext (const DeclContext *D) {
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000325 Decl::Kind DK = D->getDeclKind();
326 switch(DK) {
327#define DECL_CONTEXT(Name) \
328 case Decl::Name: \
329 return static_cast<Name##Decl*>(const_cast<DeclContext*>(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<DeclContext*>(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
342DeclContext *Decl::castToDeclContext(const Decl *D) {
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000343 Decl::Kind DK = D->getKind();
344 switch(DK) {
345#define DECL_CONTEXT(Name) \
346 case Decl::Name: \
347 return static_cast<Name##Decl*>(const_cast<Decl*>(D));
348#define DECL_CONTEXT_BASE(Name)
349#include "clang/AST/DeclNodes.def"
350 default:
351#define DECL_CONTEXT_BASE(Name) \
352 if (DK >= Decl::Name##First && DK <= Decl::Name##Last) \
353 return static_cast<Name##Decl*>(const_cast<Decl*>(D));
354#include "clang/AST/DeclNodes.def"
355 assert(false && "a decl that inherits DeclContext isn't handled");
356 return 0;
357 }
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000358}
359
Sebastian Redld3a413d2009-04-26 20:35:05 +0000360CompoundStmt* Decl::getCompoundBody(ASTContext &Context) const {
361 return dyn_cast_or_null<CompoundStmt>(getBody(Context));
362}
363
364SourceLocation Decl::getBodyRBrace(ASTContext &Context) const {
365 Stmt *Body = getBody(Context);
366 if (!Body)
367 return SourceLocation();
368 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Body))
369 return CS->getRBracLoc();
370 assert(isa<CXXTryStmt>(Body) &&
371 "Body can only be CompoundStmt or CXXTryStmt");
372 return cast<CXXTryStmt>(Body)->getSourceRange().getEnd();
373}
374
Anders Carlsson1329c272009-03-25 23:38:06 +0000375#ifndef NDEBUG
376void Decl::CheckAccessDeclContext() const {
Douglas Gregor5c27f2b2009-04-07 20:58:25 +0000377 assert((Access != AS_none || isa<TranslationUnitDecl>(this) ||
378 !isa<CXXRecordDecl>(getDeclContext())) &&
Anders Carlsson1329c272009-03-25 23:38:06 +0000379 "Access specifier is AS_none inside a record decl");
380}
381
382#endif
383
Eli Friedman56d29372008-06-07 16:52:53 +0000384//===----------------------------------------------------------------------===//
385// DeclContext Implementation
386//===----------------------------------------------------------------------===//
387
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000388bool DeclContext::classof(const Decl *D) {
389 switch (D->getKind()) {
390#define DECL_CONTEXT(Name) case Decl::Name:
391#define DECL_CONTEXT_BASE(Name)
392#include "clang/AST/DeclNodes.def"
393 return true;
394 default:
395#define DECL_CONTEXT_BASE(Name) \
396 if (D->getKind() >= Decl::Name##First && \
397 D->getKind() <= Decl::Name##Last) \
398 return true;
399#include "clang/AST/DeclNodes.def"
400 return false;
401 }
402}
403
Douglas Gregor44b43212008-12-11 16:49:14 +0000404DeclContext::~DeclContext() {
Douglas Gregorc36c5402009-04-09 17:29:08 +0000405 delete static_cast<StoredDeclsMap*>(LookupPtr);
Douglas Gregor44b43212008-12-11 16:49:14 +0000406}
407
408void DeclContext::DestroyDecls(ASTContext &C) {
Douglas Gregor6ab35242009-04-09 21:40:53 +0000409 for (decl_iterator D = decls_begin(C); D != decls_end(C); )
Douglas Gregor00ad0ef2009-01-20 04:25:11 +0000410 (*D++)->Destroy(C);
Douglas Gregor44b43212008-12-11 16:49:14 +0000411}
412
Douglas Gregorbc221632009-05-28 16:34:51 +0000413bool DeclContext::isDependentContext() const {
414 if (isFileContext())
415 return false;
416
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000417 if (isa<ClassTemplatePartialSpecializationDecl>(this))
418 return true;
419
Douglas Gregorbc221632009-05-28 16:34:51 +0000420 if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this))
421 if (Record->getDescribedClassTemplate())
422 return true;
423
424 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this))
425 if (Function->getDescribedFunctionTemplate())
426 return true;
427
428 return getParent() && getParent()->isDependentContext();
429}
430
Douglas Gregor074149e2009-01-05 19:45:36 +0000431bool DeclContext::isTransparentContext() const {
432 if (DeclKind == Decl::Enum)
433 return true; // FIXME: Check for C++0x scoped enums
434 else if (DeclKind == Decl::LinkageSpec)
435 return true;
Douglas Gregor65100792009-02-26 00:02:51 +0000436 else if (DeclKind >= Decl::RecordFirst && DeclKind <= Decl::RecordLast)
Douglas Gregorbcbffc42009-01-07 00:43:41 +0000437 return cast<RecordDecl>(this)->isAnonymousStructOrUnion();
Douglas Gregor074149e2009-01-05 19:45:36 +0000438 else if (DeclKind == Decl::Namespace)
439 return false; // FIXME: Check for C++0x inline namespaces
440
441 return false;
442}
443
Steve Naroff0701bbb2009-01-08 17:28:14 +0000444DeclContext *DeclContext::getPrimaryContext() {
Douglas Gregor44b43212008-12-11 16:49:14 +0000445 switch (DeclKind) {
Douglas Gregor44b43212008-12-11 16:49:14 +0000446 case Decl::TranslationUnit:
Douglas Gregor074149e2009-01-05 19:45:36 +0000447 case Decl::LinkageSpec:
448 case Decl::Block:
Douglas Gregor44b43212008-12-11 16:49:14 +0000449 // There is only one DeclContext for these entities.
450 return this;
451
452 case Decl::Namespace:
453 // The original namespace is our primary context.
454 return static_cast<NamespaceDecl*>(this)->getOriginalNamespace();
455
Douglas Gregor44b43212008-12-11 16:49:14 +0000456 case Decl::ObjCMethod:
457 return this;
458
459 case Decl::ObjCInterface:
Steve Naroff0701bbb2009-01-08 17:28:14 +0000460 case Decl::ObjCProtocol:
461 case Decl::ObjCCategory:
Douglas Gregor44b43212008-12-11 16:49:14 +0000462 // FIXME: Can Objective-C interfaces be forward-declared?
463 return this;
464
Steve Naroff0701bbb2009-01-08 17:28:14 +0000465 case Decl::ObjCImplementation:
466 case Decl::ObjCCategoryImpl:
467 return this;
468
Douglas Gregor44b43212008-12-11 16:49:14 +0000469 default:
Douglas Gregorcc636682009-02-17 23:15:12 +0000470 if (DeclKind >= Decl::TagFirst && DeclKind <= Decl::TagLast) {
471 // If this is a tag type that has a definition or is currently
472 // being defined, that definition is our primary context.
Chris Lattner244a67d2009-03-28 06:04:26 +0000473 if (const TagType *TagT =cast<TagDecl>(this)->TypeForDecl->getAsTagType())
Douglas Gregorcc636682009-02-17 23:15:12 +0000474 if (TagT->isBeingDefined() ||
475 (TagT->getDecl() && TagT->getDecl()->isDefinition()))
476 return TagT->getDecl();
477 return this;
478 }
479
Douglas Gregor44b43212008-12-11 16:49:14 +0000480 assert(DeclKind >= Decl::FunctionFirst && DeclKind <= Decl::FunctionLast &&
481 "Unknown DeclContext kind");
482 return this;
483 }
484}
485
486DeclContext *DeclContext::getNextContext() {
487 switch (DeclKind) {
Douglas Gregor44b43212008-12-11 16:49:14 +0000488 case Decl::Namespace:
489 // Return the next namespace
490 return static_cast<NamespaceDecl*>(this)->getNextNamespace();
491
492 default:
Douglas Gregor44b43212008-12-11 16:49:14 +0000493 return 0;
494 }
495}
496
Douglas Gregor2cf26342009-04-09 22:27:44 +0000497/// \brief Load the declarations within this lexical storage from an
498/// external source.
499void
500DeclContext::LoadLexicalDeclsFromExternalStorage(ASTContext &Context) const {
501 ExternalASTSource *Source = Context.getExternalSource();
502 assert(hasExternalLexicalStorage() && Source && "No external storage?");
503
Eli Friedmanb0156ea2009-04-27 23:43:36 +0000504 llvm::SmallVector<uint32_t, 64> Decls;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000505 if (Source->ReadDeclsLexicallyInContext(const_cast<DeclContext *>(this),
506 Decls))
507 return;
508
509 // There is no longer any lexical storage in this context
510 ExternalLexicalStorage = false;
511
512 if (Decls.empty())
513 return;
514
515 // Resolve all of the declaration IDs into declarations, building up
516 // a chain of declarations via the Decl::NextDeclInContext field.
517 Decl *FirstNewDecl = 0;
518 Decl *PrevDecl = 0;
519 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
520 Decl *D = Source->GetDecl(Decls[I]);
521 if (PrevDecl)
522 PrevDecl->NextDeclInContext = D;
523 else
524 FirstNewDecl = D;
525
526 PrevDecl = D;
527 }
528
529 // Splice the newly-read declarations into the beginning of the list
530 // of declarations.
531 PrevDecl->NextDeclInContext = FirstDecl;
532 FirstDecl = FirstNewDecl;
533 if (!LastDecl)
534 LastDecl = PrevDecl;
535}
536
537void
538DeclContext::LoadVisibleDeclsFromExternalStorage(ASTContext &Context) const {
539 DeclContext *This = const_cast<DeclContext *>(this);
540 ExternalASTSource *Source = Context.getExternalSource();
541 assert(hasExternalVisibleStorage() && Source && "No external storage?");
542
543 llvm::SmallVector<VisibleDeclaration, 64> Decls;
544 if (Source->ReadDeclsVisibleInContext(This, Decls))
545 return;
546
547 // There is no longer any visible storage in this context
548 ExternalVisibleStorage = false;
549
550 // Load the declaration IDs for all of the names visible in this
551 // context.
552 assert(!LookupPtr && "Have a lookup map before de-serialization?");
553 StoredDeclsMap *Map = new StoredDeclsMap;
554 LookupPtr = Map;
555 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
556 (*Map)[Decls[I].Name].setFromDeclIDs(Decls[I].Declarations);
557 }
558}
559
Douglas Gregor6ab35242009-04-09 21:40:53 +0000560DeclContext::decl_iterator DeclContext::decls_begin(ASTContext &Context) const {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000561 if (hasExternalLexicalStorage())
562 LoadLexicalDeclsFromExternalStorage(Context);
563
564 // FIXME: Check whether we need to load some declarations from
565 // external storage.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000566 return decl_iterator(FirstDecl);
567}
568
569DeclContext::decl_iterator DeclContext::decls_end(ASTContext &Context) const {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000570 if (hasExternalLexicalStorage())
571 LoadLexicalDeclsFromExternalStorage(Context);
572
Douglas Gregor6ab35242009-04-09 21:40:53 +0000573 return decl_iterator();
574}
575
Douglas Gregor8038d512009-04-10 17:25:41 +0000576bool DeclContext::decls_empty(ASTContext &Context) const {
577 if (hasExternalLexicalStorage())
578 LoadLexicalDeclsFromExternalStorage(Context);
579
580 return !FirstDecl;
581}
582
Douglas Gregor6ab35242009-04-09 21:40:53 +0000583void DeclContext::addDecl(ASTContext &Context, Decl *D) {
Chris Lattner7f0be132009-02-20 00:56:18 +0000584 assert(D->getLexicalDeclContext() == this &&
585 "Decl inserted into wrong lexical context");
Chris Lattner244a67d2009-03-28 06:04:26 +0000586 assert(!D->getNextDeclInContext() && D != LastDecl &&
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000587 "Decl already inserted into a DeclContext");
588
589 if (FirstDecl) {
Chris Lattner244a67d2009-03-28 06:04:26 +0000590 LastDecl->NextDeclInContext = D;
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000591 LastDecl = D;
592 } else {
593 FirstDecl = LastDecl = D;
594 }
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000595
596 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
Douglas Gregor6ab35242009-04-09 21:40:53 +0000597 ND->getDeclContext()->makeDeclVisibleInContext(Context, ND);
Douglas Gregor44b43212008-12-11 16:49:14 +0000598}
599
Douglas Gregor074149e2009-01-05 19:45:36 +0000600/// buildLookup - Build the lookup data structure with all of the
601/// declarations in DCtx (and any other contexts linked to it or
602/// transparent contexts nested within it).
Douglas Gregor6ab35242009-04-09 21:40:53 +0000603void DeclContext::buildLookup(ASTContext &Context, DeclContext *DCtx) {
Douglas Gregor074149e2009-01-05 19:45:36 +0000604 for (; DCtx; DCtx = DCtx->getNextContext()) {
Douglas Gregor6ab35242009-04-09 21:40:53 +0000605 for (decl_iterator D = DCtx->decls_begin(Context),
606 DEnd = DCtx->decls_end(Context);
Douglas Gregor4f3b8f82009-01-06 07:17:58 +0000607 D != DEnd; ++D) {
Douglas Gregor074149e2009-01-05 19:45:36 +0000608 // Insert this declaration into the lookup structure
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000609 if (NamedDecl *ND = dyn_cast<NamedDecl>(*D))
Douglas Gregor6ab35242009-04-09 21:40:53 +0000610 makeDeclVisibleInContextImpl(Context, ND);
Douglas Gregor074149e2009-01-05 19:45:36 +0000611
612 // If this declaration is itself a transparent declaration context,
613 // add its members (recursively).
614 if (DeclContext *InnerCtx = dyn_cast<DeclContext>(*D))
615 if (InnerCtx->isTransparentContext())
Douglas Gregor6ab35242009-04-09 21:40:53 +0000616 buildLookup(Context, InnerCtx->getPrimaryContext());
Douglas Gregor074149e2009-01-05 19:45:36 +0000617 }
618 }
619}
620
Douglas Gregor44b43212008-12-11 16:49:14 +0000621DeclContext::lookup_result
Douglas Gregor6ab35242009-04-09 21:40:53 +0000622DeclContext::lookup(ASTContext &Context, DeclarationName Name) {
Steve Naroff0701bbb2009-01-08 17:28:14 +0000623 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor44b43212008-12-11 16:49:14 +0000624 if (PrimaryContext != this)
Douglas Gregor6ab35242009-04-09 21:40:53 +0000625 return PrimaryContext->lookup(Context, Name);
Douglas Gregor44b43212008-12-11 16:49:14 +0000626
Douglas Gregor2cf26342009-04-09 22:27:44 +0000627 if (hasExternalVisibleStorage())
628 LoadVisibleDeclsFromExternalStorage(Context);
629
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000630 /// If there is no lookup data structure, build one now by walking
Douglas Gregor44b43212008-12-11 16:49:14 +0000631 /// all of the linked DeclContexts (in declaration order!) and
632 /// inserting their values.
Douglas Gregorc36c5402009-04-09 17:29:08 +0000633 if (!LookupPtr) {
Douglas Gregor6ab35242009-04-09 21:40:53 +0000634 buildLookup(Context, this);
Douglas Gregor44b43212008-12-11 16:49:14 +0000635
Douglas Gregorc36c5402009-04-09 17:29:08 +0000636 if (!LookupPtr)
Chris Lattner91942502009-02-20 00:55:03 +0000637 return lookup_result(0, 0);
Douglas Gregorc36c5402009-04-09 17:29:08 +0000638 }
Douglas Gregor44b43212008-12-11 16:49:14 +0000639
Douglas Gregorc36c5402009-04-09 17:29:08 +0000640 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(LookupPtr);
641 StoredDeclsMap::iterator Pos = Map->find(Name);
642 if (Pos == Map->end())
643 return lookup_result(0, 0);
Douglas Gregor6ab35242009-04-09 21:40:53 +0000644 return Pos->second.getLookupResult(Context);
Douglas Gregor44b43212008-12-11 16:49:14 +0000645}
646
647DeclContext::lookup_const_result
Douglas Gregor6ab35242009-04-09 21:40:53 +0000648DeclContext::lookup(ASTContext &Context, DeclarationName Name) const {
649 return const_cast<DeclContext*>(this)->lookup(Context, Name);
Douglas Gregor44b43212008-12-11 16:49:14 +0000650}
651
Chris Lattner0cf2b192009-03-27 19:19:59 +0000652DeclContext *DeclContext::getLookupContext() {
653 DeclContext *Ctx = this;
Douglas Gregor72de6672009-01-08 20:45:30 +0000654 // Skip through transparent contexts.
Douglas Gregorce356072009-01-06 23:51:29 +0000655 while (Ctx->isTransparentContext())
656 Ctx = Ctx->getParent();
657 return Ctx;
658}
659
Douglas Gregor88b70942009-02-25 22:02:03 +0000660DeclContext *DeclContext::getEnclosingNamespaceContext() {
661 DeclContext *Ctx = this;
662 // Skip through non-namespace, non-translation-unit contexts.
663 while (!Ctx->isFileContext() || Ctx->isTransparentContext())
664 Ctx = Ctx->getParent();
665 return Ctx->getPrimaryContext();
666}
667
Douglas Gregor6ab35242009-04-09 21:40:53 +0000668void DeclContext::makeDeclVisibleInContext(ASTContext &Context, NamedDecl *D) {
Douglas Gregorcc636682009-02-17 23:15:12 +0000669 // FIXME: This feels like a hack. Should DeclarationName support
670 // template-ids, or is there a better way to keep specializations
671 // from being visible?
672 if (isa<ClassTemplateSpecializationDecl>(D))
673 return;
674
Steve Naroff0701bbb2009-01-08 17:28:14 +0000675 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor44b43212008-12-11 16:49:14 +0000676 if (PrimaryContext != this) {
Douglas Gregor6ab35242009-04-09 21:40:53 +0000677 PrimaryContext->makeDeclVisibleInContext(Context, D);
Douglas Gregor44b43212008-12-11 16:49:14 +0000678 return;
679 }
680
681 // If we already have a lookup data structure, perform the insertion
682 // into it. Otherwise, be lazy and don't build that structure until
683 // someone asks for it.
Douglas Gregorc36c5402009-04-09 17:29:08 +0000684 if (LookupPtr)
Douglas Gregor6ab35242009-04-09 21:40:53 +0000685 makeDeclVisibleInContextImpl(Context, D);
Douglas Gregor074149e2009-01-05 19:45:36 +0000686
Douglas Gregor074149e2009-01-05 19:45:36 +0000687 // If we are a transparent context, insert into our parent context,
688 // too. This operation is recursive.
689 if (isTransparentContext())
Douglas Gregor6ab35242009-04-09 21:40:53 +0000690 getParent()->makeDeclVisibleInContext(Context, D);
Douglas Gregor44b43212008-12-11 16:49:14 +0000691}
692
Douglas Gregor6ab35242009-04-09 21:40:53 +0000693void DeclContext::makeDeclVisibleInContextImpl(ASTContext &Context,
694 NamedDecl *D) {
Douglas Gregor074149e2009-01-05 19:45:36 +0000695 // Skip unnamed declarations.
696 if (!D->getDeclName())
697 return;
698
Douglas Gregorcc636682009-02-17 23:15:12 +0000699 // FIXME: This feels like a hack. Should DeclarationName support
700 // template-ids, or is there a better way to keep specializations
701 // from being visible?
702 if (isa<ClassTemplateSpecializationDecl>(D))
703 return;
704
Douglas Gregorc36c5402009-04-09 17:29:08 +0000705 if (!LookupPtr)
706 LookupPtr = new StoredDeclsMap;
Douglas Gregor44b43212008-12-11 16:49:14 +0000707
708 // Insert this declaration into the map.
Douglas Gregorc36c5402009-04-09 17:29:08 +0000709 StoredDeclsMap &Map = *static_cast<StoredDeclsMap*>(LookupPtr);
Chris Lattner67762a32009-02-20 01:44:05 +0000710 StoredDeclsList &DeclNameEntries = Map[D->getDeclName()];
711 if (DeclNameEntries.isNull()) {
712 DeclNameEntries.setOnlyValue(D);
Chris Lattnerbd6c8002009-02-19 07:00:44 +0000713 return;
Douglas Gregor44b43212008-12-11 16:49:14 +0000714 }
Chris Lattner91942502009-02-20 00:55:03 +0000715
Chris Lattnerbdc3d002009-02-20 01:10:07 +0000716 // If it is possible that this is a redeclaration, check to see if there is
717 // already a decl for which declarationReplaces returns true. If there is
718 // one, just replace it and return.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000719 if (DeclNameEntries.HandleRedeclaration(Context, D))
Chris Lattner67762a32009-02-20 01:44:05 +0000720 return;
Chris Lattner91942502009-02-20 00:55:03 +0000721
Chris Lattnerbd6c8002009-02-19 07:00:44 +0000722 // Put this declaration into the appropriate slot.
Chris Lattner67762a32009-02-20 01:44:05 +0000723 DeclNameEntries.AddSubsequentDecl(D);
Douglas Gregor44b43212008-12-11 16:49:14 +0000724}
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000725
726/// Returns iterator range [First, Last) of UsingDirectiveDecls stored within
727/// this context.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000728DeclContext::udir_iterator_range
729DeclContext::getUsingDirectives(ASTContext &Context) const {
730 lookup_const_result Result = lookup(Context, UsingDirectiveDecl::getName());
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000731 return udir_iterator_range(reinterpret_cast<udir_iterator>(Result.first),
732 reinterpret_cast<udir_iterator>(Result.second));
733}
Douglas Gregor2cf26342009-04-09 22:27:44 +0000734
735void StoredDeclsList::materializeDecls(ASTContext &Context) {
736 if (isNull())
737 return;
738
739 switch ((DataKind)(Data & 0x03)) {
740 case DK_Decl:
741 case DK_Decl_Vector:
742 break;
743
744 case DK_DeclID: {
745 // Resolve this declaration ID to an actual declaration by
746 // querying the external AST source.
747 unsigned DeclID = Data >> 2;
748
749 ExternalASTSource *Source = Context.getExternalSource();
750 assert(Source && "No external AST source available!");
751
752 Data = reinterpret_cast<uintptr_t>(Source->GetDecl(DeclID));
753 break;
754 }
755
756 case DK_ID_Vector: {
757 // We have a vector of declaration IDs. Resolve all of them to
758 // actual declarations.
759 VectorTy &Vector = *getAsVector();
760 ExternalASTSource *Source = Context.getExternalSource();
761 assert(Source && "No external AST source available!");
762
763 for (unsigned I = 0, N = Vector.size(); I != N; ++I)
764 Vector[I] = reinterpret_cast<uintptr_t>(Source->GetDecl(Vector[I]));
765
766 Data = (Data & ~0x03) | DK_Decl_Vector;
767 break;
768 }
769 }
770}