blob: a4609d45ad1a5cd830dcb23729636f769a10acd3 [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() {
Argyrios Kyrtzidis9b346692009-06-30 02:34:53 +0000161 if (TranslationUnitDecl *TUD = dyn_cast<TranslationUnitDecl>(this))
162 return TUD;
163
Argyrios Kyrtzidis3708b3d2009-06-29 17:38:40 +0000164 DeclContext *DC = getDeclContext();
165 assert(DC && "This decl is not contained in a translation unit!");
166
167 while (!DC->isTranslationUnit()) {
168 DC = DC->getParent();
169 assert(DC && "This decl is not contained in a translation unit!");
170 }
171
172 return cast<TranslationUnitDecl>(DC);
173}
174
175ASTContext &Decl::getASTContext() const {
176 return getTranslationUnitDecl()->getASTContext();
177}
178
Chris Lattner769dbdf2009-03-27 20:18:19 +0000179unsigned Decl::getIdentifierNamespaceForKind(Kind DeclKind) {
180 switch (DeclKind) {
181 default:
182 if (DeclKind >= FunctionFirst && DeclKind <= FunctionLast)
183 return IDNS_Ordinary;
184 assert(0 && "Unknown decl kind!");
185 case OverloadedFunction:
186 case Typedef:
187 case EnumConstant:
188 case Var:
189 case ImplicitParam:
190 case ParmVar:
191 case OriginalParmVar:
192 case NonTypeTemplateParm:
Douglas Gregor9cfbe482009-06-20 00:51:54 +0000193 case Using:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000194 case ObjCMethod:
195 case ObjCContainer:
196 case ObjCCategory:
197 case ObjCInterface:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000198 case ObjCProperty:
199 case ObjCCompatibleAlias:
200 return IDNS_Ordinary;
201
202 case ObjCProtocol:
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000203 return IDNS_ObjCProtocol;
Chris Lattner769dbdf2009-03-27 20:18:19 +0000204
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000205 case ObjCImplementation:
206 return IDNS_ObjCImplementation;
207
208 case ObjCCategoryImpl:
209 return IDNS_ObjCCategoryImpl;
210
Chris Lattner769dbdf2009-03-27 20:18:19 +0000211 case Field:
212 case ObjCAtDefsField:
213 case ObjCIvar:
214 return IDNS_Member;
215
216 case Record:
217 case CXXRecord:
218 case Enum:
219 case TemplateTypeParm:
220 return IDNS_Tag;
221
222 case Namespace:
223 case Template:
224 case FunctionTemplate:
225 case ClassTemplate:
226 case TemplateTemplateParm:
Anders Carlssonfaf0e872009-03-28 23:02:53 +0000227 case NamespaceAlias:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000228 return IDNS_Tag | IDNS_Ordinary;
229
230 // Never have names.
231 case LinkageSpec:
232 case FileScopeAsm:
233 case StaticAssert:
234 case ObjCClass:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000235 case ObjCPropertyImpl:
236 case ObjCForwardProtocol:
237 case Block:
238 case TranslationUnit:
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000239
Chris Lattner769dbdf2009-03-27 20:18:19 +0000240 // Aren't looked up?
241 case UsingDirective:
242 case ClassTemplateSpecialization:
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000243 case ClassTemplatePartialSpecialization:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000244 return 0;
245 }
Eli Friedman56d29372008-06-07 16:52:53 +0000246}
247
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000248void Decl::addAttr(Attr *NewAttr) {
249 Attr *&ExistingAttr = getASTContext().getDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000250
251 NewAttr->setNext(ExistingAttr);
252 ExistingAttr = NewAttr;
253
254 HasAttrs = true;
255}
256
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000257void Decl::invalidateAttrs() {
Eli Friedman56d29372008-06-07 16:52:53 +0000258 if (!HasAttrs) return;
Douglas Gregor68584ed2009-06-18 16:11:24 +0000259
Eli Friedman56d29372008-06-07 16:52:53 +0000260 HasAttrs = false;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000261 getASTContext().eraseDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000262}
263
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000264const Attr *Decl::getAttrsImpl() const {
Chris Lattner81abbdd2009-03-21 06:27:31 +0000265 assert(HasAttrs && "getAttrs() should verify this!");
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000266 return getASTContext().getDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000267}
268
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000269void Decl::swapAttrs(Decl *RHS) {
Eli Friedman56d29372008-06-07 16:52:53 +0000270 bool HasLHSAttr = this->HasAttrs;
271 bool HasRHSAttr = RHS->HasAttrs;
272
273 // Usually, neither decl has attrs, nothing to do.
274 if (!HasLHSAttr && !HasRHSAttr) return;
275
276 // If 'this' has no attrs, swap the other way.
277 if (!HasLHSAttr)
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000278 return RHS->swapAttrs(this);
279
280 ASTContext &Context = getASTContext();
Eli Friedman56d29372008-06-07 16:52:53 +0000281
282 // Handle the case when both decls have attrs.
283 if (HasRHSAttr) {
Douglas Gregor68584ed2009-06-18 16:11:24 +0000284 std::swap(Context.getDeclAttrs(this), Context.getDeclAttrs(RHS));
Eli Friedman56d29372008-06-07 16:52:53 +0000285 return;
286 }
287
288 // Otherwise, LHS has an attr and RHS doesn't.
Douglas Gregor68584ed2009-06-18 16:11:24 +0000289 Context.getDeclAttrs(RHS) = Context.getDeclAttrs(this);
290 Context.eraseDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000291 this->HasAttrs = false;
292 RHS->HasAttrs = true;
293}
294
295
Chris Lattnercc581472009-03-04 06:05:19 +0000296void Decl::Destroy(ASTContext &C) {
297 // Free attributes for this decl.
298 if (HasAttrs) {
Douglas Gregor68584ed2009-06-18 16:11:24 +0000299 C.getDeclAttrs(this)->Destroy(C);
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000300 invalidateAttrs();
Chris Lattnercc581472009-03-04 06:05:19 +0000301 HasAttrs = false;
302 }
303
Douglas Gregora0fc55f2009-01-13 19:47:12 +0000304#if 0
Douglas Gregor00ad0ef2009-01-20 04:25:11 +0000305 // FIXME: Once ownership is fully understood, we can enable this code
306 if (DeclContext *DC = dyn_cast<DeclContext>(this))
307 DC->decls_begin()->Destroy(C);
Eli Friedman56d29372008-06-07 16:52:53 +0000308
Chris Lattner244a67d2009-03-28 06:04:26 +0000309 // Observe the unrolled recursion. By setting N->NextDeclInContext = 0x0
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000310 // within the loop, only the Destroy method for the first Decl
311 // will deallocate all of the Decls in a chain.
312
Chris Lattner244a67d2009-03-28 06:04:26 +0000313 Decl* N = getNextDeclInContext();
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000314
315 while (N) {
Chris Lattner244a67d2009-03-28 06:04:26 +0000316 Decl* Tmp = N->getNextDeclInContext();
317 N->NextDeclInContext = 0;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000318 N->Destroy(C);
319 N = Tmp;
Eli Friedman56d29372008-06-07 16:52:53 +0000320 }
Douglas Gregora0fc55f2009-01-13 19:47:12 +0000321
Eli Friedman56d29372008-06-07 16:52:53 +0000322 this->~Decl();
Steve Naroff3e970492009-01-27 21:25:57 +0000323 C.Deallocate((void *)this);
Douglas Gregor00ad0ef2009-01-20 04:25:11 +0000324#endif
Eli Friedman56d29372008-06-07 16:52:53 +0000325}
326
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000327Decl *Decl::castFromDeclContext (const DeclContext *D) {
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000328 Decl::Kind DK = D->getDeclKind();
329 switch(DK) {
330#define DECL_CONTEXT(Name) \
331 case Decl::Name: \
332 return static_cast<Name##Decl*>(const_cast<DeclContext*>(D));
333#define DECL_CONTEXT_BASE(Name)
334#include "clang/AST/DeclNodes.def"
335 default:
336#define DECL_CONTEXT_BASE(Name) \
337 if (DK >= Decl::Name##First && DK <= Decl::Name##Last) \
338 return static_cast<Name##Decl*>(const_cast<DeclContext*>(D));
339#include "clang/AST/DeclNodes.def"
340 assert(false && "a decl that inherits DeclContext isn't handled");
341 return 0;
342 }
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000343}
344
345DeclContext *Decl::castToDeclContext(const Decl *D) {
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000346 Decl::Kind DK = D->getKind();
347 switch(DK) {
348#define DECL_CONTEXT(Name) \
349 case Decl::Name: \
350 return static_cast<Name##Decl*>(const_cast<Decl*>(D));
351#define DECL_CONTEXT_BASE(Name)
352#include "clang/AST/DeclNodes.def"
353 default:
354#define DECL_CONTEXT_BASE(Name) \
355 if (DK >= Decl::Name##First && DK <= Decl::Name##Last) \
356 return static_cast<Name##Decl*>(const_cast<Decl*>(D));
357#include "clang/AST/DeclNodes.def"
358 assert(false && "a decl that inherits DeclContext isn't handled");
359 return 0;
360 }
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000361}
362
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000363CompoundStmt* Decl::getCompoundBody() const {
364 return dyn_cast_or_null<CompoundStmt>(getBody());
Sebastian Redld3a413d2009-04-26 20:35:05 +0000365}
366
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000367SourceLocation Decl::getBodyRBrace() const {
368 Stmt *Body = getBody();
Sebastian Redld3a413d2009-04-26 20:35:05 +0000369 if (!Body)
370 return SourceLocation();
371 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Body))
372 return CS->getRBracLoc();
373 assert(isa<CXXTryStmt>(Body) &&
374 "Body can only be CompoundStmt or CXXTryStmt");
375 return cast<CXXTryStmt>(Body)->getSourceRange().getEnd();
376}
377
Anders Carlsson1329c272009-03-25 23:38:06 +0000378#ifndef NDEBUG
379void Decl::CheckAccessDeclContext() const {
Douglas Gregor5c27f2b2009-04-07 20:58:25 +0000380 assert((Access != AS_none || isa<TranslationUnitDecl>(this) ||
381 !isa<CXXRecordDecl>(getDeclContext())) &&
Anders Carlsson1329c272009-03-25 23:38:06 +0000382 "Access specifier is AS_none inside a record decl");
383}
384
385#endif
386
Eli Friedman56d29372008-06-07 16:52:53 +0000387//===----------------------------------------------------------------------===//
388// DeclContext Implementation
389//===----------------------------------------------------------------------===//
390
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000391bool DeclContext::classof(const Decl *D) {
392 switch (D->getKind()) {
393#define DECL_CONTEXT(Name) case Decl::Name:
394#define DECL_CONTEXT_BASE(Name)
395#include "clang/AST/DeclNodes.def"
396 return true;
397 default:
398#define DECL_CONTEXT_BASE(Name) \
399 if (D->getKind() >= Decl::Name##First && \
400 D->getKind() <= Decl::Name##Last) \
401 return true;
402#include "clang/AST/DeclNodes.def"
403 return false;
404 }
405}
406
Douglas Gregor44b43212008-12-11 16:49:14 +0000407DeclContext::~DeclContext() {
Douglas Gregorc36c5402009-04-09 17:29:08 +0000408 delete static_cast<StoredDeclsMap*>(LookupPtr);
Douglas Gregor44b43212008-12-11 16:49:14 +0000409}
410
411void DeclContext::DestroyDecls(ASTContext &C) {
Douglas Gregor6ab35242009-04-09 21:40:53 +0000412 for (decl_iterator D = decls_begin(C); D != decls_end(C); )
Douglas Gregor00ad0ef2009-01-20 04:25:11 +0000413 (*D++)->Destroy(C);
Douglas Gregor44b43212008-12-11 16:49:14 +0000414}
415
Douglas Gregorbc221632009-05-28 16:34:51 +0000416bool DeclContext::isDependentContext() const {
417 if (isFileContext())
418 return false;
419
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000420 if (isa<ClassTemplatePartialSpecializationDecl>(this))
421 return true;
422
Douglas Gregorbc221632009-05-28 16:34:51 +0000423 if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this))
424 if (Record->getDescribedClassTemplate())
425 return true;
426
427 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this))
428 if (Function->getDescribedFunctionTemplate())
429 return true;
430
431 return getParent() && getParent()->isDependentContext();
432}
433
Douglas Gregor074149e2009-01-05 19:45:36 +0000434bool DeclContext::isTransparentContext() const {
435 if (DeclKind == Decl::Enum)
436 return true; // FIXME: Check for C++0x scoped enums
437 else if (DeclKind == Decl::LinkageSpec)
438 return true;
Douglas Gregor65100792009-02-26 00:02:51 +0000439 else if (DeclKind >= Decl::RecordFirst && DeclKind <= Decl::RecordLast)
Douglas Gregorbcbffc42009-01-07 00:43:41 +0000440 return cast<RecordDecl>(this)->isAnonymousStructOrUnion();
Douglas Gregor074149e2009-01-05 19:45:36 +0000441 else if (DeclKind == Decl::Namespace)
442 return false; // FIXME: Check for C++0x inline namespaces
443
444 return false;
445}
446
Steve Naroff0701bbb2009-01-08 17:28:14 +0000447DeclContext *DeclContext::getPrimaryContext() {
Douglas Gregor44b43212008-12-11 16:49:14 +0000448 switch (DeclKind) {
Douglas Gregor44b43212008-12-11 16:49:14 +0000449 case Decl::TranslationUnit:
Douglas Gregor074149e2009-01-05 19:45:36 +0000450 case Decl::LinkageSpec:
451 case Decl::Block:
Douglas Gregor44b43212008-12-11 16:49:14 +0000452 // There is only one DeclContext for these entities.
453 return this;
454
455 case Decl::Namespace:
456 // The original namespace is our primary context.
457 return static_cast<NamespaceDecl*>(this)->getOriginalNamespace();
458
Douglas Gregor44b43212008-12-11 16:49:14 +0000459 case Decl::ObjCMethod:
460 return this;
461
462 case Decl::ObjCInterface:
Steve Naroff0701bbb2009-01-08 17:28:14 +0000463 case Decl::ObjCProtocol:
464 case Decl::ObjCCategory:
Douglas Gregor44b43212008-12-11 16:49:14 +0000465 // FIXME: Can Objective-C interfaces be forward-declared?
466 return this;
467
Steve Naroff0701bbb2009-01-08 17:28:14 +0000468 case Decl::ObjCImplementation:
469 case Decl::ObjCCategoryImpl:
470 return this;
471
Douglas Gregor44b43212008-12-11 16:49:14 +0000472 default:
Douglas Gregorcc636682009-02-17 23:15:12 +0000473 if (DeclKind >= Decl::TagFirst && DeclKind <= Decl::TagLast) {
474 // If this is a tag type that has a definition or is currently
475 // being defined, that definition is our primary context.
Chris Lattner244a67d2009-03-28 06:04:26 +0000476 if (const TagType *TagT =cast<TagDecl>(this)->TypeForDecl->getAsTagType())
Douglas Gregorcc636682009-02-17 23:15:12 +0000477 if (TagT->isBeingDefined() ||
478 (TagT->getDecl() && TagT->getDecl()->isDefinition()))
479 return TagT->getDecl();
480 return this;
481 }
482
Douglas Gregor44b43212008-12-11 16:49:14 +0000483 assert(DeclKind >= Decl::FunctionFirst && DeclKind <= Decl::FunctionLast &&
484 "Unknown DeclContext kind");
485 return this;
486 }
487}
488
489DeclContext *DeclContext::getNextContext() {
490 switch (DeclKind) {
Douglas Gregor44b43212008-12-11 16:49:14 +0000491 case Decl::Namespace:
492 // Return the next namespace
493 return static_cast<NamespaceDecl*>(this)->getNextNamespace();
494
495 default:
Douglas Gregor44b43212008-12-11 16:49:14 +0000496 return 0;
497 }
498}
499
Douglas Gregor2cf26342009-04-09 22:27:44 +0000500/// \brief Load the declarations within this lexical storage from an
501/// external source.
502void
503DeclContext::LoadLexicalDeclsFromExternalStorage(ASTContext &Context) const {
504 ExternalASTSource *Source = Context.getExternalSource();
505 assert(hasExternalLexicalStorage() && Source && "No external storage?");
506
Eli Friedmanb0156ea2009-04-27 23:43:36 +0000507 llvm::SmallVector<uint32_t, 64> Decls;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000508 if (Source->ReadDeclsLexicallyInContext(const_cast<DeclContext *>(this),
509 Decls))
510 return;
511
512 // There is no longer any lexical storage in this context
513 ExternalLexicalStorage = false;
514
515 if (Decls.empty())
516 return;
517
518 // Resolve all of the declaration IDs into declarations, building up
519 // a chain of declarations via the Decl::NextDeclInContext field.
520 Decl *FirstNewDecl = 0;
521 Decl *PrevDecl = 0;
522 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
523 Decl *D = Source->GetDecl(Decls[I]);
524 if (PrevDecl)
525 PrevDecl->NextDeclInContext = D;
526 else
527 FirstNewDecl = D;
528
529 PrevDecl = D;
530 }
531
532 // Splice the newly-read declarations into the beginning of the list
533 // of declarations.
534 PrevDecl->NextDeclInContext = FirstDecl;
535 FirstDecl = FirstNewDecl;
536 if (!LastDecl)
537 LastDecl = PrevDecl;
538}
539
540void
541DeclContext::LoadVisibleDeclsFromExternalStorage(ASTContext &Context) const {
542 DeclContext *This = const_cast<DeclContext *>(this);
543 ExternalASTSource *Source = Context.getExternalSource();
544 assert(hasExternalVisibleStorage() && Source && "No external storage?");
545
546 llvm::SmallVector<VisibleDeclaration, 64> Decls;
547 if (Source->ReadDeclsVisibleInContext(This, Decls))
548 return;
549
550 // There is no longer any visible storage in this context
551 ExternalVisibleStorage = false;
552
553 // Load the declaration IDs for all of the names visible in this
554 // context.
555 assert(!LookupPtr && "Have a lookup map before de-serialization?");
556 StoredDeclsMap *Map = new StoredDeclsMap;
557 LookupPtr = Map;
558 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
559 (*Map)[Decls[I].Name].setFromDeclIDs(Decls[I].Declarations);
560 }
561}
562
Douglas Gregor6ab35242009-04-09 21:40:53 +0000563DeclContext::decl_iterator DeclContext::decls_begin(ASTContext &Context) const {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000564 if (hasExternalLexicalStorage())
565 LoadLexicalDeclsFromExternalStorage(Context);
566
567 // FIXME: Check whether we need to load some declarations from
568 // external storage.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000569 return decl_iterator(FirstDecl);
570}
571
572DeclContext::decl_iterator DeclContext::decls_end(ASTContext &Context) const {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000573 if (hasExternalLexicalStorage())
574 LoadLexicalDeclsFromExternalStorage(Context);
575
Douglas Gregor6ab35242009-04-09 21:40:53 +0000576 return decl_iterator();
577}
578
Douglas Gregor8038d512009-04-10 17:25:41 +0000579bool DeclContext::decls_empty(ASTContext &Context) const {
580 if (hasExternalLexicalStorage())
581 LoadLexicalDeclsFromExternalStorage(Context);
582
583 return !FirstDecl;
584}
585
Douglas Gregor6ab35242009-04-09 21:40:53 +0000586void DeclContext::addDecl(ASTContext &Context, Decl *D) {
Chris Lattner7f0be132009-02-20 00:56:18 +0000587 assert(D->getLexicalDeclContext() == this &&
588 "Decl inserted into wrong lexical context");
Chris Lattner244a67d2009-03-28 06:04:26 +0000589 assert(!D->getNextDeclInContext() && D != LastDecl &&
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000590 "Decl already inserted into a DeclContext");
591
592 if (FirstDecl) {
Chris Lattner244a67d2009-03-28 06:04:26 +0000593 LastDecl->NextDeclInContext = D;
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000594 LastDecl = D;
595 } else {
596 FirstDecl = LastDecl = D;
597 }
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000598
599 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
Douglas Gregor6ab35242009-04-09 21:40:53 +0000600 ND->getDeclContext()->makeDeclVisibleInContext(Context, ND);
Douglas Gregor44b43212008-12-11 16:49:14 +0000601}
602
Douglas Gregor074149e2009-01-05 19:45:36 +0000603/// buildLookup - Build the lookup data structure with all of the
604/// declarations in DCtx (and any other contexts linked to it or
605/// transparent contexts nested within it).
Douglas Gregor6ab35242009-04-09 21:40:53 +0000606void DeclContext::buildLookup(ASTContext &Context, DeclContext *DCtx) {
Douglas Gregor074149e2009-01-05 19:45:36 +0000607 for (; DCtx; DCtx = DCtx->getNextContext()) {
Douglas Gregor6ab35242009-04-09 21:40:53 +0000608 for (decl_iterator D = DCtx->decls_begin(Context),
609 DEnd = DCtx->decls_end(Context);
Douglas Gregor4f3b8f82009-01-06 07:17:58 +0000610 D != DEnd; ++D) {
Douglas Gregor074149e2009-01-05 19:45:36 +0000611 // Insert this declaration into the lookup structure
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000612 if (NamedDecl *ND = dyn_cast<NamedDecl>(*D))
Douglas Gregor6ab35242009-04-09 21:40:53 +0000613 makeDeclVisibleInContextImpl(Context, ND);
Douglas Gregor074149e2009-01-05 19:45:36 +0000614
615 // If this declaration is itself a transparent declaration context,
616 // add its members (recursively).
617 if (DeclContext *InnerCtx = dyn_cast<DeclContext>(*D))
618 if (InnerCtx->isTransparentContext())
Douglas Gregor6ab35242009-04-09 21:40:53 +0000619 buildLookup(Context, InnerCtx->getPrimaryContext());
Douglas Gregor074149e2009-01-05 19:45:36 +0000620 }
621 }
622}
623
Douglas Gregor44b43212008-12-11 16:49:14 +0000624DeclContext::lookup_result
Douglas Gregor6ab35242009-04-09 21:40:53 +0000625DeclContext::lookup(ASTContext &Context, DeclarationName Name) {
Steve Naroff0701bbb2009-01-08 17:28:14 +0000626 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor44b43212008-12-11 16:49:14 +0000627 if (PrimaryContext != this)
Douglas Gregor6ab35242009-04-09 21:40:53 +0000628 return PrimaryContext->lookup(Context, Name);
Douglas Gregor44b43212008-12-11 16:49:14 +0000629
Douglas Gregor2cf26342009-04-09 22:27:44 +0000630 if (hasExternalVisibleStorage())
631 LoadVisibleDeclsFromExternalStorage(Context);
632
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000633 /// If there is no lookup data structure, build one now by walking
Douglas Gregor44b43212008-12-11 16:49:14 +0000634 /// all of the linked DeclContexts (in declaration order!) and
635 /// inserting their values.
Douglas Gregorc36c5402009-04-09 17:29:08 +0000636 if (!LookupPtr) {
Douglas Gregor6ab35242009-04-09 21:40:53 +0000637 buildLookup(Context, this);
Douglas Gregor44b43212008-12-11 16:49:14 +0000638
Douglas Gregorc36c5402009-04-09 17:29:08 +0000639 if (!LookupPtr)
Chris Lattner91942502009-02-20 00:55:03 +0000640 return lookup_result(0, 0);
Douglas Gregorc36c5402009-04-09 17:29:08 +0000641 }
Douglas Gregor44b43212008-12-11 16:49:14 +0000642
Douglas Gregorc36c5402009-04-09 17:29:08 +0000643 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(LookupPtr);
644 StoredDeclsMap::iterator Pos = Map->find(Name);
645 if (Pos == Map->end())
646 return lookup_result(0, 0);
Douglas Gregor6ab35242009-04-09 21:40:53 +0000647 return Pos->second.getLookupResult(Context);
Douglas Gregor44b43212008-12-11 16:49:14 +0000648}
649
650DeclContext::lookup_const_result
Douglas Gregor6ab35242009-04-09 21:40:53 +0000651DeclContext::lookup(ASTContext &Context, DeclarationName Name) const {
652 return const_cast<DeclContext*>(this)->lookup(Context, Name);
Douglas Gregor44b43212008-12-11 16:49:14 +0000653}
654
Chris Lattner0cf2b192009-03-27 19:19:59 +0000655DeclContext *DeclContext::getLookupContext() {
656 DeclContext *Ctx = this;
Douglas Gregor72de6672009-01-08 20:45:30 +0000657 // Skip through transparent contexts.
Douglas Gregorce356072009-01-06 23:51:29 +0000658 while (Ctx->isTransparentContext())
659 Ctx = Ctx->getParent();
660 return Ctx;
661}
662
Douglas Gregor88b70942009-02-25 22:02:03 +0000663DeclContext *DeclContext::getEnclosingNamespaceContext() {
664 DeclContext *Ctx = this;
665 // Skip through non-namespace, non-translation-unit contexts.
666 while (!Ctx->isFileContext() || Ctx->isTransparentContext())
667 Ctx = Ctx->getParent();
668 return Ctx->getPrimaryContext();
669}
670
Douglas Gregor6ab35242009-04-09 21:40:53 +0000671void DeclContext::makeDeclVisibleInContext(ASTContext &Context, NamedDecl *D) {
Douglas Gregorcc636682009-02-17 23:15:12 +0000672 // FIXME: This feels like a hack. Should DeclarationName support
673 // template-ids, or is there a better way to keep specializations
674 // from being visible?
675 if (isa<ClassTemplateSpecializationDecl>(D))
676 return;
677
Steve Naroff0701bbb2009-01-08 17:28:14 +0000678 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor44b43212008-12-11 16:49:14 +0000679 if (PrimaryContext != this) {
Douglas Gregor6ab35242009-04-09 21:40:53 +0000680 PrimaryContext->makeDeclVisibleInContext(Context, D);
Douglas Gregor44b43212008-12-11 16:49:14 +0000681 return;
682 }
683
684 // If we already have a lookup data structure, perform the insertion
685 // into it. Otherwise, be lazy and don't build that structure until
686 // someone asks for it.
Douglas Gregorc36c5402009-04-09 17:29:08 +0000687 if (LookupPtr)
Douglas Gregor6ab35242009-04-09 21:40:53 +0000688 makeDeclVisibleInContextImpl(Context, D);
Douglas Gregor074149e2009-01-05 19:45:36 +0000689
Douglas Gregor074149e2009-01-05 19:45:36 +0000690 // If we are a transparent context, insert into our parent context,
691 // too. This operation is recursive.
692 if (isTransparentContext())
Douglas Gregor6ab35242009-04-09 21:40:53 +0000693 getParent()->makeDeclVisibleInContext(Context, D);
Douglas Gregor44b43212008-12-11 16:49:14 +0000694}
695
Douglas Gregor6ab35242009-04-09 21:40:53 +0000696void DeclContext::makeDeclVisibleInContextImpl(ASTContext &Context,
697 NamedDecl *D) {
Douglas Gregor074149e2009-01-05 19:45:36 +0000698 // Skip unnamed declarations.
699 if (!D->getDeclName())
700 return;
701
Douglas Gregorcc636682009-02-17 23:15:12 +0000702 // FIXME: This feels like a hack. Should DeclarationName support
703 // template-ids, or is there a better way to keep specializations
704 // from being visible?
705 if (isa<ClassTemplateSpecializationDecl>(D))
706 return;
707
Douglas Gregorc36c5402009-04-09 17:29:08 +0000708 if (!LookupPtr)
709 LookupPtr = new StoredDeclsMap;
Douglas Gregor44b43212008-12-11 16:49:14 +0000710
711 // Insert this declaration into the map.
Douglas Gregorc36c5402009-04-09 17:29:08 +0000712 StoredDeclsMap &Map = *static_cast<StoredDeclsMap*>(LookupPtr);
Chris Lattner67762a32009-02-20 01:44:05 +0000713 StoredDeclsList &DeclNameEntries = Map[D->getDeclName()];
714 if (DeclNameEntries.isNull()) {
715 DeclNameEntries.setOnlyValue(D);
Chris Lattnerbd6c8002009-02-19 07:00:44 +0000716 return;
Douglas Gregor44b43212008-12-11 16:49:14 +0000717 }
Chris Lattner91942502009-02-20 00:55:03 +0000718
Chris Lattnerbdc3d002009-02-20 01:10:07 +0000719 // If it is possible that this is a redeclaration, check to see if there is
720 // already a decl for which declarationReplaces returns true. If there is
721 // one, just replace it and return.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000722 if (DeclNameEntries.HandleRedeclaration(Context, D))
Chris Lattner67762a32009-02-20 01:44:05 +0000723 return;
Chris Lattner91942502009-02-20 00:55:03 +0000724
Chris Lattnerbd6c8002009-02-19 07:00:44 +0000725 // Put this declaration into the appropriate slot.
Chris Lattner67762a32009-02-20 01:44:05 +0000726 DeclNameEntries.AddSubsequentDecl(D);
Douglas Gregor44b43212008-12-11 16:49:14 +0000727}
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000728
729/// Returns iterator range [First, Last) of UsingDirectiveDecls stored within
730/// this context.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000731DeclContext::udir_iterator_range
732DeclContext::getUsingDirectives(ASTContext &Context) const {
733 lookup_const_result Result = lookup(Context, UsingDirectiveDecl::getName());
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000734 return udir_iterator_range(reinterpret_cast<udir_iterator>(Result.first),
735 reinterpret_cast<udir_iterator>(Result.second));
736}
Douglas Gregor2cf26342009-04-09 22:27:44 +0000737
738void StoredDeclsList::materializeDecls(ASTContext &Context) {
739 if (isNull())
740 return;
741
742 switch ((DataKind)(Data & 0x03)) {
743 case DK_Decl:
744 case DK_Decl_Vector:
745 break;
746
747 case DK_DeclID: {
748 // Resolve this declaration ID to an actual declaration by
749 // querying the external AST source.
750 unsigned DeclID = Data >> 2;
751
752 ExternalASTSource *Source = Context.getExternalSource();
753 assert(Source && "No external AST source available!");
754
755 Data = reinterpret_cast<uintptr_t>(Source->GetDecl(DeclID));
756 break;
757 }
758
759 case DK_ID_Vector: {
760 // We have a vector of declaration IDs. Resolve all of them to
761 // actual declarations.
762 VectorTy &Vector = *getAsVector();
763 ExternalASTSource *Source = Context.getExternalSource();
764 assert(Source && "No external AST source available!");
765
766 for (unsigned I = 0, N = Vector.size(); I != N; ++I)
767 Vector[I] = reinterpret_cast<uintptr_t>(Source->GetDecl(Vector[I]));
768
769 Data = (Data & ~0x03) | DK_Decl_Vector;
770 break;
771 }
772 }
773}