blob: cd5b0063a63ed1869b8dcde84696ae0f2f7aca2c [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;
John McCall3f9a8a62009-08-11 06:59:38 +0000201
John McCallc48fbdf2009-08-11 21:13:21 +0000202 case FriendClass:
John McCall3f9a8a62009-08-11 06:59:38 +0000203 case FriendFunction:
204 return IDNS_Friend;
Chris Lattner769dbdf2009-03-27 20:18:19 +0000205
206 case ObjCProtocol:
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000207 return IDNS_ObjCProtocol;
Chris Lattner769dbdf2009-03-27 20:18:19 +0000208
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000209 case ObjCImplementation:
210 return IDNS_ObjCImplementation;
211
212 case ObjCCategoryImpl:
213 return IDNS_ObjCCategoryImpl;
214
Chris Lattner769dbdf2009-03-27 20:18:19 +0000215 case Field:
216 case ObjCAtDefsField:
217 case ObjCIvar:
218 return IDNS_Member;
219
220 case Record:
221 case CXXRecord:
222 case Enum:
223 case TemplateTypeParm:
224 return IDNS_Tag;
225
226 case Namespace:
227 case Template:
228 case FunctionTemplate:
229 case ClassTemplate:
230 case TemplateTemplateParm:
Anders Carlssonfaf0e872009-03-28 23:02:53 +0000231 case NamespaceAlias:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000232 return IDNS_Tag | IDNS_Ordinary;
233
234 // Never have names.
235 case LinkageSpec:
236 case FileScopeAsm:
237 case StaticAssert:
238 case ObjCClass:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000239 case ObjCPropertyImpl:
240 case ObjCForwardProtocol:
241 case Block:
242 case TranslationUnit:
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000243
Chris Lattner769dbdf2009-03-27 20:18:19 +0000244 // Aren't looked up?
245 case UsingDirective:
246 case ClassTemplateSpecialization:
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000247 case ClassTemplatePartialSpecialization:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000248 return 0;
249 }
Eli Friedman56d29372008-06-07 16:52:53 +0000250}
251
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000252void Decl::addAttr(Attr *NewAttr) {
253 Attr *&ExistingAttr = getASTContext().getDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000254
255 NewAttr->setNext(ExistingAttr);
256 ExistingAttr = NewAttr;
257
258 HasAttrs = true;
259}
260
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000261void Decl::invalidateAttrs() {
Eli Friedman56d29372008-06-07 16:52:53 +0000262 if (!HasAttrs) return;
Douglas Gregor68584ed2009-06-18 16:11:24 +0000263
Eli Friedman56d29372008-06-07 16:52:53 +0000264 HasAttrs = false;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000265 getASTContext().eraseDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000266}
267
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000268const Attr *Decl::getAttrsImpl() const {
Chris Lattner81abbdd2009-03-21 06:27:31 +0000269 assert(HasAttrs && "getAttrs() should verify this!");
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000270 return getASTContext().getDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000271}
272
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000273void Decl::swapAttrs(Decl *RHS) {
Eli Friedman56d29372008-06-07 16:52:53 +0000274 bool HasLHSAttr = this->HasAttrs;
275 bool HasRHSAttr = RHS->HasAttrs;
276
277 // Usually, neither decl has attrs, nothing to do.
278 if (!HasLHSAttr && !HasRHSAttr) return;
279
280 // If 'this' has no attrs, swap the other way.
281 if (!HasLHSAttr)
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000282 return RHS->swapAttrs(this);
283
284 ASTContext &Context = getASTContext();
Eli Friedman56d29372008-06-07 16:52:53 +0000285
286 // Handle the case when both decls have attrs.
287 if (HasRHSAttr) {
Douglas Gregor68584ed2009-06-18 16:11:24 +0000288 std::swap(Context.getDeclAttrs(this), Context.getDeclAttrs(RHS));
Eli Friedman56d29372008-06-07 16:52:53 +0000289 return;
290 }
291
292 // Otherwise, LHS has an attr and RHS doesn't.
Douglas Gregor68584ed2009-06-18 16:11:24 +0000293 Context.getDeclAttrs(RHS) = Context.getDeclAttrs(this);
294 Context.eraseDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000295 this->HasAttrs = false;
296 RHS->HasAttrs = true;
297}
298
299
Chris Lattnercc581472009-03-04 06:05:19 +0000300void Decl::Destroy(ASTContext &C) {
301 // Free attributes for this decl.
302 if (HasAttrs) {
Douglas Gregor68584ed2009-06-18 16:11:24 +0000303 C.getDeclAttrs(this)->Destroy(C);
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000304 invalidateAttrs();
Chris Lattnercc581472009-03-04 06:05:19 +0000305 HasAttrs = false;
306 }
307
Douglas Gregora0fc55f2009-01-13 19:47:12 +0000308#if 0
Douglas Gregor00ad0ef2009-01-20 04:25:11 +0000309 // FIXME: Once ownership is fully understood, we can enable this code
310 if (DeclContext *DC = dyn_cast<DeclContext>(this))
311 DC->decls_begin()->Destroy(C);
Eli Friedman56d29372008-06-07 16:52:53 +0000312
Chris Lattner244a67d2009-03-28 06:04:26 +0000313 // Observe the unrolled recursion. By setting N->NextDeclInContext = 0x0
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000314 // within the loop, only the Destroy method for the first Decl
315 // will deallocate all of the Decls in a chain.
316
Chris Lattner244a67d2009-03-28 06:04:26 +0000317 Decl* N = getNextDeclInContext();
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000318
319 while (N) {
Chris Lattner244a67d2009-03-28 06:04:26 +0000320 Decl* Tmp = N->getNextDeclInContext();
321 N->NextDeclInContext = 0;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000322 N->Destroy(C);
323 N = Tmp;
Eli Friedman56d29372008-06-07 16:52:53 +0000324 }
Douglas Gregora0fc55f2009-01-13 19:47:12 +0000325
Eli Friedman56d29372008-06-07 16:52:53 +0000326 this->~Decl();
Steve Naroff3e970492009-01-27 21:25:57 +0000327 C.Deallocate((void *)this);
Douglas Gregor00ad0ef2009-01-20 04:25:11 +0000328#endif
Eli Friedman56d29372008-06-07 16:52:53 +0000329}
330
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000331Decl *Decl::castFromDeclContext (const DeclContext *D) {
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000332 Decl::Kind DK = D->getDeclKind();
333 switch(DK) {
334#define DECL_CONTEXT(Name) \
335 case Decl::Name: \
336 return static_cast<Name##Decl*>(const_cast<DeclContext*>(D));
337#define DECL_CONTEXT_BASE(Name)
338#include "clang/AST/DeclNodes.def"
339 default:
340#define DECL_CONTEXT_BASE(Name) \
341 if (DK >= Decl::Name##First && DK <= Decl::Name##Last) \
342 return static_cast<Name##Decl*>(const_cast<DeclContext*>(D));
343#include "clang/AST/DeclNodes.def"
344 assert(false && "a decl that inherits DeclContext isn't handled");
345 return 0;
346 }
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000347}
348
349DeclContext *Decl::castToDeclContext(const Decl *D) {
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000350 Decl::Kind DK = D->getKind();
351 switch(DK) {
352#define DECL_CONTEXT(Name) \
353 case Decl::Name: \
354 return static_cast<Name##Decl*>(const_cast<Decl*>(D));
355#define DECL_CONTEXT_BASE(Name)
356#include "clang/AST/DeclNodes.def"
357 default:
358#define DECL_CONTEXT_BASE(Name) \
359 if (DK >= Decl::Name##First && DK <= Decl::Name##Last) \
360 return static_cast<Name##Decl*>(const_cast<Decl*>(D));
361#include "clang/AST/DeclNodes.def"
362 assert(false && "a decl that inherits DeclContext isn't handled");
363 return 0;
364 }
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000365}
366
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000367CompoundStmt* Decl::getCompoundBody() const {
368 return dyn_cast_or_null<CompoundStmt>(getBody());
Sebastian Redld3a413d2009-04-26 20:35:05 +0000369}
370
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000371SourceLocation Decl::getBodyRBrace() const {
372 Stmt *Body = getBody();
Sebastian Redld3a413d2009-04-26 20:35:05 +0000373 if (!Body)
374 return SourceLocation();
375 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Body))
376 return CS->getRBracLoc();
377 assert(isa<CXXTryStmt>(Body) &&
378 "Body can only be CompoundStmt or CXXTryStmt");
379 return cast<CXXTryStmt>(Body)->getSourceRange().getEnd();
380}
381
Anders Carlsson1329c272009-03-25 23:38:06 +0000382#ifndef NDEBUG
383void Decl::CheckAccessDeclContext() const {
Douglas Gregor5c27f2b2009-04-07 20:58:25 +0000384 assert((Access != AS_none || isa<TranslationUnitDecl>(this) ||
385 !isa<CXXRecordDecl>(getDeclContext())) &&
Anders Carlsson1329c272009-03-25 23:38:06 +0000386 "Access specifier is AS_none inside a record decl");
387}
388
389#endif
390
Eli Friedman56d29372008-06-07 16:52:53 +0000391//===----------------------------------------------------------------------===//
392// DeclContext Implementation
393//===----------------------------------------------------------------------===//
394
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000395bool DeclContext::classof(const Decl *D) {
396 switch (D->getKind()) {
397#define DECL_CONTEXT(Name) case Decl::Name:
398#define DECL_CONTEXT_BASE(Name)
399#include "clang/AST/DeclNodes.def"
400 return true;
401 default:
402#define DECL_CONTEXT_BASE(Name) \
403 if (D->getKind() >= Decl::Name##First && \
404 D->getKind() <= Decl::Name##Last) \
405 return true;
406#include "clang/AST/DeclNodes.def"
407 return false;
408 }
409}
410
Douglas Gregor44b43212008-12-11 16:49:14 +0000411DeclContext::~DeclContext() {
Douglas Gregorc36c5402009-04-09 17:29:08 +0000412 delete static_cast<StoredDeclsMap*>(LookupPtr);
Douglas Gregor44b43212008-12-11 16:49:14 +0000413}
414
415void DeclContext::DestroyDecls(ASTContext &C) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000416 for (decl_iterator D = decls_begin(); D != decls_end(); )
Douglas Gregor00ad0ef2009-01-20 04:25:11 +0000417 (*D++)->Destroy(C);
Douglas Gregor44b43212008-12-11 16:49:14 +0000418}
419
Douglas Gregorbc221632009-05-28 16:34:51 +0000420bool DeclContext::isDependentContext() const {
421 if (isFileContext())
422 return false;
423
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000424 if (isa<ClassTemplatePartialSpecializationDecl>(this))
425 return true;
426
Douglas Gregorbc221632009-05-28 16:34:51 +0000427 if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this))
428 if (Record->getDescribedClassTemplate())
429 return true;
430
431 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this))
432 if (Function->getDescribedFunctionTemplate())
433 return true;
434
435 return getParent() && getParent()->isDependentContext();
436}
437
Douglas Gregor074149e2009-01-05 19:45:36 +0000438bool DeclContext::isTransparentContext() const {
439 if (DeclKind == Decl::Enum)
440 return true; // FIXME: Check for C++0x scoped enums
441 else if (DeclKind == Decl::LinkageSpec)
442 return true;
Douglas Gregor65100792009-02-26 00:02:51 +0000443 else if (DeclKind >= Decl::RecordFirst && DeclKind <= Decl::RecordLast)
Douglas Gregorbcbffc42009-01-07 00:43:41 +0000444 return cast<RecordDecl>(this)->isAnonymousStructOrUnion();
Douglas Gregor074149e2009-01-05 19:45:36 +0000445 else if (DeclKind == Decl::Namespace)
446 return false; // FIXME: Check for C++0x inline namespaces
447
448 return false;
449}
450
Douglas Gregor6dd38da2009-08-27 06:03:53 +0000451bool DeclContext::Encloses(DeclContext *DC) {
452 if (getPrimaryContext() != this)
453 return getPrimaryContext()->Encloses(DC);
454
455 for (; DC; DC = DC->getParent())
456 if (DC->getPrimaryContext() == this)
457 return true;
458 return false;
459}
460
Steve Naroff0701bbb2009-01-08 17:28:14 +0000461DeclContext *DeclContext::getPrimaryContext() {
Douglas Gregor44b43212008-12-11 16:49:14 +0000462 switch (DeclKind) {
Douglas Gregor44b43212008-12-11 16:49:14 +0000463 case Decl::TranslationUnit:
Douglas Gregor074149e2009-01-05 19:45:36 +0000464 case Decl::LinkageSpec:
465 case Decl::Block:
Douglas Gregor44b43212008-12-11 16:49:14 +0000466 // There is only one DeclContext for these entities.
467 return this;
468
469 case Decl::Namespace:
470 // The original namespace is our primary context.
471 return static_cast<NamespaceDecl*>(this)->getOriginalNamespace();
472
Douglas Gregor44b43212008-12-11 16:49:14 +0000473 case Decl::ObjCMethod:
474 return this;
475
476 case Decl::ObjCInterface:
Steve Naroff0701bbb2009-01-08 17:28:14 +0000477 case Decl::ObjCProtocol:
478 case Decl::ObjCCategory:
Douglas Gregor44b43212008-12-11 16:49:14 +0000479 // FIXME: Can Objective-C interfaces be forward-declared?
480 return this;
481
Steve Naroff0701bbb2009-01-08 17:28:14 +0000482 case Decl::ObjCImplementation:
483 case Decl::ObjCCategoryImpl:
484 return this;
485
Douglas Gregor44b43212008-12-11 16:49:14 +0000486 default:
Douglas Gregorcc636682009-02-17 23:15:12 +0000487 if (DeclKind >= Decl::TagFirst && DeclKind <= Decl::TagLast) {
488 // If this is a tag type that has a definition or is currently
489 // being defined, that definition is our primary context.
Ted Kremenek6217b802009-07-29 21:53:49 +0000490 if (const TagType *TagT =cast<TagDecl>(this)->TypeForDecl->getAs<TagType>())
Douglas Gregorcc636682009-02-17 23:15:12 +0000491 if (TagT->isBeingDefined() ||
492 (TagT->getDecl() && TagT->getDecl()->isDefinition()))
493 return TagT->getDecl();
494 return this;
495 }
496
Douglas Gregor44b43212008-12-11 16:49:14 +0000497 assert(DeclKind >= Decl::FunctionFirst && DeclKind <= Decl::FunctionLast &&
498 "Unknown DeclContext kind");
499 return this;
500 }
501}
502
503DeclContext *DeclContext::getNextContext() {
504 switch (DeclKind) {
Douglas Gregor44b43212008-12-11 16:49:14 +0000505 case Decl::Namespace:
506 // Return the next namespace
507 return static_cast<NamespaceDecl*>(this)->getNextNamespace();
508
509 default:
Douglas Gregor44b43212008-12-11 16:49:14 +0000510 return 0;
511 }
512}
513
Douglas Gregor2cf26342009-04-09 22:27:44 +0000514/// \brief Load the declarations within this lexical storage from an
515/// external source.
516void
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000517DeclContext::LoadLexicalDeclsFromExternalStorage() const {
518 ExternalASTSource *Source = getParentASTContext().getExternalSource();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000519 assert(hasExternalLexicalStorage() && Source && "No external storage?");
520
Eli Friedmanb0156ea2009-04-27 23:43:36 +0000521 llvm::SmallVector<uint32_t, 64> Decls;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000522 if (Source->ReadDeclsLexicallyInContext(const_cast<DeclContext *>(this),
523 Decls))
524 return;
525
526 // There is no longer any lexical storage in this context
527 ExternalLexicalStorage = false;
528
529 if (Decls.empty())
530 return;
531
532 // Resolve all of the declaration IDs into declarations, building up
533 // a chain of declarations via the Decl::NextDeclInContext field.
534 Decl *FirstNewDecl = 0;
535 Decl *PrevDecl = 0;
536 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
537 Decl *D = Source->GetDecl(Decls[I]);
538 if (PrevDecl)
539 PrevDecl->NextDeclInContext = D;
540 else
541 FirstNewDecl = D;
542
543 PrevDecl = D;
544 }
545
546 // Splice the newly-read declarations into the beginning of the list
547 // of declarations.
548 PrevDecl->NextDeclInContext = FirstDecl;
549 FirstDecl = FirstNewDecl;
550 if (!LastDecl)
551 LastDecl = PrevDecl;
552}
553
554void
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000555DeclContext::LoadVisibleDeclsFromExternalStorage() const {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000556 DeclContext *This = const_cast<DeclContext *>(this);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000557 ExternalASTSource *Source = getParentASTContext().getExternalSource();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000558 assert(hasExternalVisibleStorage() && Source && "No external storage?");
559
560 llvm::SmallVector<VisibleDeclaration, 64> Decls;
561 if (Source->ReadDeclsVisibleInContext(This, Decls))
562 return;
563
564 // There is no longer any visible storage in this context
565 ExternalVisibleStorage = false;
566
567 // Load the declaration IDs for all of the names visible in this
568 // context.
569 assert(!LookupPtr && "Have a lookup map before de-serialization?");
570 StoredDeclsMap *Map = new StoredDeclsMap;
571 LookupPtr = Map;
572 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
573 (*Map)[Decls[I].Name].setFromDeclIDs(Decls[I].Declarations);
574 }
575}
576
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000577DeclContext::decl_iterator DeclContext::decls_begin() const {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000578 if (hasExternalLexicalStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000579 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000580
581 // FIXME: Check whether we need to load some declarations from
582 // external storage.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000583 return decl_iterator(FirstDecl);
584}
585
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000586DeclContext::decl_iterator DeclContext::decls_end() const {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000587 if (hasExternalLexicalStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000588 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000589
Douglas Gregor6ab35242009-04-09 21:40:53 +0000590 return decl_iterator();
591}
592
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000593bool DeclContext::decls_empty() const {
Douglas Gregor8038d512009-04-10 17:25:41 +0000594 if (hasExternalLexicalStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000595 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor8038d512009-04-10 17:25:41 +0000596
597 return !FirstDecl;
598}
599
John McCall3f9a8a62009-08-11 06:59:38 +0000600void DeclContext::addHiddenDecl(Decl *D) {
Chris Lattner7f0be132009-02-20 00:56:18 +0000601 assert(D->getLexicalDeclContext() == this &&
602 "Decl inserted into wrong lexical context");
Chris Lattner244a67d2009-03-28 06:04:26 +0000603 assert(!D->getNextDeclInContext() && D != LastDecl &&
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000604 "Decl already inserted into a DeclContext");
605
606 if (FirstDecl) {
Chris Lattner244a67d2009-03-28 06:04:26 +0000607 LastDecl->NextDeclInContext = D;
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000608 LastDecl = D;
609 } else {
610 FirstDecl = LastDecl = D;
611 }
John McCall3f9a8a62009-08-11 06:59:38 +0000612}
613
614void DeclContext::addDecl(Decl *D) {
615 addHiddenDecl(D);
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000616
617 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000618 ND->getDeclContext()->makeDeclVisibleInContext(ND);
Douglas Gregor44b43212008-12-11 16:49:14 +0000619}
620
Douglas Gregor074149e2009-01-05 19:45:36 +0000621/// buildLookup - Build the lookup data structure with all of the
622/// declarations in DCtx (and any other contexts linked to it or
623/// transparent contexts nested within it).
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000624void DeclContext::buildLookup(DeclContext *DCtx) {
Douglas Gregor074149e2009-01-05 19:45:36 +0000625 for (; DCtx; DCtx = DCtx->getNextContext()) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000626 for (decl_iterator D = DCtx->decls_begin(),
627 DEnd = DCtx->decls_end();
Douglas Gregor4f3b8f82009-01-06 07:17:58 +0000628 D != DEnd; ++D) {
John McCall3f9a8a62009-08-11 06:59:38 +0000629 // Insert this declaration into the lookup structure, but only
630 // if it's semantically in its decl context. During non-lazy
631 // lookup building, this is implicitly enforced by addDecl.
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000632 if (NamedDecl *ND = dyn_cast<NamedDecl>(*D))
John McCall3f9a8a62009-08-11 06:59:38 +0000633 if (D->getDeclContext() == DCtx)
634 makeDeclVisibleInContextImpl(ND);
Douglas Gregor074149e2009-01-05 19:45:36 +0000635
636 // If this declaration is itself a transparent declaration context,
637 // add its members (recursively).
638 if (DeclContext *InnerCtx = dyn_cast<DeclContext>(*D))
639 if (InnerCtx->isTransparentContext())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000640 buildLookup(InnerCtx->getPrimaryContext());
Douglas Gregor074149e2009-01-05 19:45:36 +0000641 }
642 }
643}
644
Douglas Gregor44b43212008-12-11 16:49:14 +0000645DeclContext::lookup_result
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000646DeclContext::lookup(DeclarationName Name) {
Steve Naroff0701bbb2009-01-08 17:28:14 +0000647 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor44b43212008-12-11 16:49:14 +0000648 if (PrimaryContext != this)
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000649 return PrimaryContext->lookup(Name);
Douglas Gregor44b43212008-12-11 16:49:14 +0000650
Douglas Gregor2cf26342009-04-09 22:27:44 +0000651 if (hasExternalVisibleStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000652 LoadVisibleDeclsFromExternalStorage();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000653
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000654 /// If there is no lookup data structure, build one now by walking
Douglas Gregor44b43212008-12-11 16:49:14 +0000655 /// all of the linked DeclContexts (in declaration order!) and
656 /// inserting their values.
Douglas Gregorc36c5402009-04-09 17:29:08 +0000657 if (!LookupPtr) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000658 buildLookup(this);
Douglas Gregor44b43212008-12-11 16:49:14 +0000659
Douglas Gregorc36c5402009-04-09 17:29:08 +0000660 if (!LookupPtr)
Chris Lattner91942502009-02-20 00:55:03 +0000661 return lookup_result(0, 0);
Douglas Gregorc36c5402009-04-09 17:29:08 +0000662 }
Douglas Gregor44b43212008-12-11 16:49:14 +0000663
Douglas Gregorc36c5402009-04-09 17:29:08 +0000664 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(LookupPtr);
665 StoredDeclsMap::iterator Pos = Map->find(Name);
666 if (Pos == Map->end())
667 return lookup_result(0, 0);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000668 return Pos->second.getLookupResult(getParentASTContext());
Douglas Gregor44b43212008-12-11 16:49:14 +0000669}
670
671DeclContext::lookup_const_result
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000672DeclContext::lookup(DeclarationName Name) const {
673 return const_cast<DeclContext*>(this)->lookup(Name);
Douglas Gregor44b43212008-12-11 16:49:14 +0000674}
675
Chris Lattner0cf2b192009-03-27 19:19:59 +0000676DeclContext *DeclContext::getLookupContext() {
677 DeclContext *Ctx = this;
Douglas Gregor72de6672009-01-08 20:45:30 +0000678 // Skip through transparent contexts.
Douglas Gregorce356072009-01-06 23:51:29 +0000679 while (Ctx->isTransparentContext())
680 Ctx = Ctx->getParent();
681 return Ctx;
682}
683
Douglas Gregor88b70942009-02-25 22:02:03 +0000684DeclContext *DeclContext::getEnclosingNamespaceContext() {
685 DeclContext *Ctx = this;
686 // Skip through non-namespace, non-translation-unit contexts.
687 while (!Ctx->isFileContext() || Ctx->isTransparentContext())
688 Ctx = Ctx->getParent();
689 return Ctx->getPrimaryContext();
690}
691
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000692void DeclContext::makeDeclVisibleInContext(NamedDecl *D) {
Douglas Gregorcc636682009-02-17 23:15:12 +0000693 // FIXME: This feels like a hack. Should DeclarationName support
694 // template-ids, or is there a better way to keep specializations
695 // from being visible?
696 if (isa<ClassTemplateSpecializationDecl>(D))
697 return;
698
Steve Naroff0701bbb2009-01-08 17:28:14 +0000699 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor44b43212008-12-11 16:49:14 +0000700 if (PrimaryContext != this) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000701 PrimaryContext->makeDeclVisibleInContext(D);
Douglas Gregor44b43212008-12-11 16:49:14 +0000702 return;
703 }
704
705 // If we already have a lookup data structure, perform the insertion
706 // into it. Otherwise, be lazy and don't build that structure until
707 // someone asks for it.
Douglas Gregorc36c5402009-04-09 17:29:08 +0000708 if (LookupPtr)
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000709 makeDeclVisibleInContextImpl(D);
Douglas Gregor074149e2009-01-05 19:45:36 +0000710
Douglas Gregor074149e2009-01-05 19:45:36 +0000711 // If we are a transparent context, insert into our parent context,
712 // too. This operation is recursive.
713 if (isTransparentContext())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000714 getParent()->makeDeclVisibleInContext(D);
Douglas Gregor44b43212008-12-11 16:49:14 +0000715}
716
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000717void DeclContext::makeDeclVisibleInContextImpl(NamedDecl *D) {
Douglas Gregor074149e2009-01-05 19:45:36 +0000718 // Skip unnamed declarations.
719 if (!D->getDeclName())
720 return;
721
Douglas Gregorcc636682009-02-17 23:15:12 +0000722 // FIXME: This feels like a hack. Should DeclarationName support
723 // template-ids, or is there a better way to keep specializations
724 // from being visible?
725 if (isa<ClassTemplateSpecializationDecl>(D))
726 return;
727
Douglas Gregorc36c5402009-04-09 17:29:08 +0000728 if (!LookupPtr)
729 LookupPtr = new StoredDeclsMap;
Douglas Gregor44b43212008-12-11 16:49:14 +0000730
731 // Insert this declaration into the map.
Douglas Gregorc36c5402009-04-09 17:29:08 +0000732 StoredDeclsMap &Map = *static_cast<StoredDeclsMap*>(LookupPtr);
Chris Lattner67762a32009-02-20 01:44:05 +0000733 StoredDeclsList &DeclNameEntries = Map[D->getDeclName()];
734 if (DeclNameEntries.isNull()) {
735 DeclNameEntries.setOnlyValue(D);
Chris Lattnerbd6c8002009-02-19 07:00:44 +0000736 return;
Douglas Gregor44b43212008-12-11 16:49:14 +0000737 }
Chris Lattner91942502009-02-20 00:55:03 +0000738
Chris Lattnerbdc3d002009-02-20 01:10:07 +0000739 // If it is possible that this is a redeclaration, check to see if there is
740 // already a decl for which declarationReplaces returns true. If there is
741 // one, just replace it and return.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000742 if (DeclNameEntries.HandleRedeclaration(getParentASTContext(), D))
Chris Lattner67762a32009-02-20 01:44:05 +0000743 return;
Chris Lattner91942502009-02-20 00:55:03 +0000744
Chris Lattnerbd6c8002009-02-19 07:00:44 +0000745 // Put this declaration into the appropriate slot.
Chris Lattner67762a32009-02-20 01:44:05 +0000746 DeclNameEntries.AddSubsequentDecl(D);
Douglas Gregor44b43212008-12-11 16:49:14 +0000747}
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000748
749/// Returns iterator range [First, Last) of UsingDirectiveDecls stored within
750/// this context.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000751DeclContext::udir_iterator_range
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000752DeclContext::getUsingDirectives() const {
753 lookup_const_result Result = lookup(UsingDirectiveDecl::getName());
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000754 return udir_iterator_range(reinterpret_cast<udir_iterator>(Result.first),
755 reinterpret_cast<udir_iterator>(Result.second));
756}
Douglas Gregor2cf26342009-04-09 22:27:44 +0000757
758void StoredDeclsList::materializeDecls(ASTContext &Context) {
759 if (isNull())
760 return;
761
762 switch ((DataKind)(Data & 0x03)) {
763 case DK_Decl:
764 case DK_Decl_Vector:
765 break;
766
767 case DK_DeclID: {
768 // Resolve this declaration ID to an actual declaration by
769 // querying the external AST source.
770 unsigned DeclID = Data >> 2;
771
772 ExternalASTSource *Source = Context.getExternalSource();
773 assert(Source && "No external AST source available!");
774
775 Data = reinterpret_cast<uintptr_t>(Source->GetDecl(DeclID));
776 break;
777 }
778
779 case DK_ID_Vector: {
780 // We have a vector of declaration IDs. Resolve all of them to
781 // actual declarations.
782 VectorTy &Vector = *getAsVector();
783 ExternalASTSource *Source = Context.getExternalSource();
784 assert(Source && "No external AST source available!");
785
786 for (unsigned I = 0, N = Vector.size(); I != N; ++I)
787 Vector[I] = reinterpret_cast<uintptr_t>(Source->GetDecl(Vector[I]));
788
789 Data = (Data & ~0x03) | DK_Decl_Vector;
790 break;
791 }
792 }
793}