blob: c24dac9104253c205368ca0cf7de96260eef7826 [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:
Anders Carlsson665b49c2009-08-28 05:30:28 +0000194 case UnresolvedUsing:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000195 case ObjCMethod:
196 case ObjCContainer:
197 case ObjCCategory:
198 case ObjCInterface:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000199 case ObjCProperty:
200 case ObjCCompatibleAlias:
201 return IDNS_Ordinary;
John McCall3f9a8a62009-08-11 06:59:38 +0000202
Chris Lattner769dbdf2009-03-27 20:18:19 +0000203 case ObjCProtocol:
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000204 return IDNS_ObjCProtocol;
Chris Lattner769dbdf2009-03-27 20:18:19 +0000205
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000206 case ObjCImplementation:
207 return IDNS_ObjCImplementation;
208
209 case ObjCCategoryImpl:
210 return IDNS_ObjCCategoryImpl;
211
Chris Lattner769dbdf2009-03-27 20:18:19 +0000212 case Field:
213 case ObjCAtDefsField:
214 case ObjCIvar:
215 return IDNS_Member;
216
217 case Record:
218 case CXXRecord:
219 case Enum:
220 case TemplateTypeParm:
221 return IDNS_Tag;
222
223 case Namespace:
224 case Template:
225 case FunctionTemplate:
226 case ClassTemplate:
227 case TemplateTemplateParm:
Anders Carlssonfaf0e872009-03-28 23:02:53 +0000228 case NamespaceAlias:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000229 return IDNS_Tag | IDNS_Ordinary;
230
231 // Never have names.
John McCall02cace72009-08-28 07:59:38 +0000232 case Friend:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000233 case LinkageSpec:
234 case FileScopeAsm:
235 case StaticAssert:
236 case ObjCClass:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000237 case ObjCPropertyImpl:
238 case ObjCForwardProtocol:
239 case Block:
240 case TranslationUnit:
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000241
Chris Lattner769dbdf2009-03-27 20:18:19 +0000242 // Aren't looked up?
243 case UsingDirective:
244 case ClassTemplateSpecialization:
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000245 case ClassTemplatePartialSpecialization:
Chris Lattner769dbdf2009-03-27 20:18:19 +0000246 return 0;
247 }
Eli Friedman56d29372008-06-07 16:52:53 +0000248}
249
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000250void Decl::addAttr(Attr *NewAttr) {
251 Attr *&ExistingAttr = getASTContext().getDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000252
253 NewAttr->setNext(ExistingAttr);
254 ExistingAttr = NewAttr;
255
256 HasAttrs = true;
257}
258
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000259void Decl::invalidateAttrs() {
Eli Friedman56d29372008-06-07 16:52:53 +0000260 if (!HasAttrs) return;
Douglas Gregor68584ed2009-06-18 16:11:24 +0000261
Eli Friedman56d29372008-06-07 16:52:53 +0000262 HasAttrs = false;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000263 getASTContext().eraseDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000264}
265
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000266const Attr *Decl::getAttrsImpl() const {
Chris Lattner81abbdd2009-03-21 06:27:31 +0000267 assert(HasAttrs && "getAttrs() should verify this!");
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000268 return getASTContext().getDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000269}
270
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000271void Decl::swapAttrs(Decl *RHS) {
Eli Friedman56d29372008-06-07 16:52:53 +0000272 bool HasLHSAttr = this->HasAttrs;
273 bool HasRHSAttr = RHS->HasAttrs;
274
275 // Usually, neither decl has attrs, nothing to do.
276 if (!HasLHSAttr && !HasRHSAttr) return;
277
278 // If 'this' has no attrs, swap the other way.
279 if (!HasLHSAttr)
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000280 return RHS->swapAttrs(this);
281
282 ASTContext &Context = getASTContext();
Eli Friedman56d29372008-06-07 16:52:53 +0000283
284 // Handle the case when both decls have attrs.
285 if (HasRHSAttr) {
Douglas Gregor68584ed2009-06-18 16:11:24 +0000286 std::swap(Context.getDeclAttrs(this), Context.getDeclAttrs(RHS));
Eli Friedman56d29372008-06-07 16:52:53 +0000287 return;
288 }
289
290 // Otherwise, LHS has an attr and RHS doesn't.
Douglas Gregor68584ed2009-06-18 16:11:24 +0000291 Context.getDeclAttrs(RHS) = Context.getDeclAttrs(this);
292 Context.eraseDeclAttrs(this);
Eli Friedman56d29372008-06-07 16:52:53 +0000293 this->HasAttrs = false;
294 RHS->HasAttrs = true;
295}
296
297
Chris Lattnercc581472009-03-04 06:05:19 +0000298void Decl::Destroy(ASTContext &C) {
299 // Free attributes for this decl.
300 if (HasAttrs) {
Douglas Gregor68584ed2009-06-18 16:11:24 +0000301 C.getDeclAttrs(this)->Destroy(C);
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000302 invalidateAttrs();
Chris Lattnercc581472009-03-04 06:05:19 +0000303 HasAttrs = false;
304 }
305
Douglas Gregora0fc55f2009-01-13 19:47:12 +0000306#if 0
Douglas Gregor00ad0ef2009-01-20 04:25:11 +0000307 // FIXME: Once ownership is fully understood, we can enable this code
308 if (DeclContext *DC = dyn_cast<DeclContext>(this))
309 DC->decls_begin()->Destroy(C);
Eli Friedman56d29372008-06-07 16:52:53 +0000310
Chris Lattner244a67d2009-03-28 06:04:26 +0000311 // Observe the unrolled recursion. By setting N->NextDeclInContext = 0x0
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000312 // within the loop, only the Destroy method for the first Decl
313 // will deallocate all of the Decls in a chain.
314
Chris Lattner244a67d2009-03-28 06:04:26 +0000315 Decl* N = getNextDeclInContext();
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000316
317 while (N) {
Chris Lattner244a67d2009-03-28 06:04:26 +0000318 Decl* Tmp = N->getNextDeclInContext();
319 N->NextDeclInContext = 0;
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000320 N->Destroy(C);
321 N = Tmp;
Eli Friedman56d29372008-06-07 16:52:53 +0000322 }
Douglas Gregora0fc55f2009-01-13 19:47:12 +0000323
Eli Friedman56d29372008-06-07 16:52:53 +0000324 this->~Decl();
Steve Naroff3e970492009-01-27 21:25:57 +0000325 C.Deallocate((void *)this);
Douglas Gregor00ad0ef2009-01-20 04:25:11 +0000326#endif
Eli Friedman56d29372008-06-07 16:52:53 +0000327}
328
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000329Decl *Decl::castFromDeclContext (const DeclContext *D) {
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000330 Decl::Kind DK = D->getDeclKind();
331 switch(DK) {
332#define DECL_CONTEXT(Name) \
333 case Decl::Name: \
334 return static_cast<Name##Decl*>(const_cast<DeclContext*>(D));
335#define DECL_CONTEXT_BASE(Name)
336#include "clang/AST/DeclNodes.def"
337 default:
338#define DECL_CONTEXT_BASE(Name) \
339 if (DK >= Decl::Name##First && DK <= Decl::Name##Last) \
340 return static_cast<Name##Decl*>(const_cast<DeclContext*>(D));
341#include "clang/AST/DeclNodes.def"
342 assert(false && "a decl that inherits DeclContext isn't handled");
343 return 0;
344 }
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000345}
346
347DeclContext *Decl::castToDeclContext(const Decl *D) {
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000348 Decl::Kind DK = D->getKind();
349 switch(DK) {
350#define DECL_CONTEXT(Name) \
351 case Decl::Name: \
352 return static_cast<Name##Decl*>(const_cast<Decl*>(D));
353#define DECL_CONTEXT_BASE(Name)
354#include "clang/AST/DeclNodes.def"
355 default:
356#define DECL_CONTEXT_BASE(Name) \
357 if (DK >= Decl::Name##First && DK <= Decl::Name##Last) \
358 return static_cast<Name##Decl*>(const_cast<Decl*>(D));
359#include "clang/AST/DeclNodes.def"
360 assert(false && "a decl that inherits DeclContext isn't handled");
361 return 0;
362 }
Argyrios Kyrtzidis42220c52008-10-12 16:14:48 +0000363}
364
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000365CompoundStmt* Decl::getCompoundBody() const {
366 return dyn_cast_or_null<CompoundStmt>(getBody());
Sebastian Redld3a413d2009-04-26 20:35:05 +0000367}
368
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000369SourceLocation Decl::getBodyRBrace() const {
370 Stmt *Body = getBody();
Sebastian Redld3a413d2009-04-26 20:35:05 +0000371 if (!Body)
372 return SourceLocation();
373 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Body))
374 return CS->getRBracLoc();
375 assert(isa<CXXTryStmt>(Body) &&
376 "Body can only be CompoundStmt or CXXTryStmt");
377 return cast<CXXTryStmt>(Body)->getSourceRange().getEnd();
378}
379
Anders Carlsson1329c272009-03-25 23:38:06 +0000380#ifndef NDEBUG
381void Decl::CheckAccessDeclContext() const {
Anders Carlsson35eda442009-08-29 20:47:47 +0000382 // If the decl is the toplevel translation unit or if we're not in a
383 // record decl context, we don't need to check anything.
384 if (isa<TranslationUnitDecl>(this) ||
385 !isa<CXXRecordDecl>(getDeclContext()))
386 return;
387
388 // FIXME: This check should not be necessary - If a friend decl refers to an
389 // undeclared decl, then that decl shouldn't be in any decl context.
390 if (getFriendObjectKind() == FOK_Undeclared)
391 return;
392
393 assert(Access != AS_none &&
Anders Carlsson1329c272009-03-25 23:38:06 +0000394 "Access specifier is AS_none inside a record decl");
395}
396
397#endif
398
Eli Friedman56d29372008-06-07 16:52:53 +0000399//===----------------------------------------------------------------------===//
400// DeclContext Implementation
401//===----------------------------------------------------------------------===//
402
Argyrios Kyrtzidis3d7641e2009-02-16 14:29:28 +0000403bool DeclContext::classof(const Decl *D) {
404 switch (D->getKind()) {
405#define DECL_CONTEXT(Name) case Decl::Name:
406#define DECL_CONTEXT_BASE(Name)
407#include "clang/AST/DeclNodes.def"
408 return true;
409 default:
410#define DECL_CONTEXT_BASE(Name) \
411 if (D->getKind() >= Decl::Name##First && \
412 D->getKind() <= Decl::Name##Last) \
413 return true;
414#include "clang/AST/DeclNodes.def"
415 return false;
416 }
417}
418
Douglas Gregor44b43212008-12-11 16:49:14 +0000419DeclContext::~DeclContext() {
Douglas Gregorc36c5402009-04-09 17:29:08 +0000420 delete static_cast<StoredDeclsMap*>(LookupPtr);
Douglas Gregor44b43212008-12-11 16:49:14 +0000421}
422
423void DeclContext::DestroyDecls(ASTContext &C) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000424 for (decl_iterator D = decls_begin(); D != decls_end(); )
Douglas Gregor00ad0ef2009-01-20 04:25:11 +0000425 (*D++)->Destroy(C);
Douglas Gregor44b43212008-12-11 16:49:14 +0000426}
427
Douglas Gregorbc221632009-05-28 16:34:51 +0000428bool DeclContext::isDependentContext() const {
429 if (isFileContext())
430 return false;
431
Douglas Gregorc8ab2562009-05-31 09:31:02 +0000432 if (isa<ClassTemplatePartialSpecializationDecl>(this))
433 return true;
434
Douglas Gregorbc221632009-05-28 16:34:51 +0000435 if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this))
436 if (Record->getDescribedClassTemplate())
437 return true;
438
439 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this))
440 if (Function->getDescribedFunctionTemplate())
441 return true;
442
443 return getParent() && getParent()->isDependentContext();
444}
445
Douglas Gregor074149e2009-01-05 19:45:36 +0000446bool DeclContext::isTransparentContext() const {
447 if (DeclKind == Decl::Enum)
448 return true; // FIXME: Check for C++0x scoped enums
449 else if (DeclKind == Decl::LinkageSpec)
450 return true;
Douglas Gregor65100792009-02-26 00:02:51 +0000451 else if (DeclKind >= Decl::RecordFirst && DeclKind <= Decl::RecordLast)
Douglas Gregorbcbffc42009-01-07 00:43:41 +0000452 return cast<RecordDecl>(this)->isAnonymousStructOrUnion();
Douglas Gregor074149e2009-01-05 19:45:36 +0000453 else if (DeclKind == Decl::Namespace)
454 return false; // FIXME: Check for C++0x inline namespaces
455
456 return false;
457}
458
Douglas Gregor6dd38da2009-08-27 06:03:53 +0000459bool DeclContext::Encloses(DeclContext *DC) {
460 if (getPrimaryContext() != this)
461 return getPrimaryContext()->Encloses(DC);
462
463 for (; DC; DC = DC->getParent())
464 if (DC->getPrimaryContext() == this)
465 return true;
466 return false;
467}
468
Steve Naroff0701bbb2009-01-08 17:28:14 +0000469DeclContext *DeclContext::getPrimaryContext() {
Douglas Gregor44b43212008-12-11 16:49:14 +0000470 switch (DeclKind) {
Douglas Gregor44b43212008-12-11 16:49:14 +0000471 case Decl::TranslationUnit:
Douglas Gregor074149e2009-01-05 19:45:36 +0000472 case Decl::LinkageSpec:
473 case Decl::Block:
Douglas Gregor44b43212008-12-11 16:49:14 +0000474 // There is only one DeclContext for these entities.
475 return this;
476
477 case Decl::Namespace:
478 // The original namespace is our primary context.
479 return static_cast<NamespaceDecl*>(this)->getOriginalNamespace();
480
Douglas Gregor44b43212008-12-11 16:49:14 +0000481 case Decl::ObjCMethod:
482 return this;
483
484 case Decl::ObjCInterface:
Steve Naroff0701bbb2009-01-08 17:28:14 +0000485 case Decl::ObjCProtocol:
486 case Decl::ObjCCategory:
Douglas Gregor44b43212008-12-11 16:49:14 +0000487 // FIXME: Can Objective-C interfaces be forward-declared?
488 return this;
489
Steve Naroff0701bbb2009-01-08 17:28:14 +0000490 case Decl::ObjCImplementation:
491 case Decl::ObjCCategoryImpl:
492 return this;
493
Douglas Gregor44b43212008-12-11 16:49:14 +0000494 default:
Douglas Gregorcc636682009-02-17 23:15:12 +0000495 if (DeclKind >= Decl::TagFirst && DeclKind <= Decl::TagLast) {
496 // If this is a tag type that has a definition or is currently
497 // being defined, that definition is our primary context.
Ted Kremenek6217b802009-07-29 21:53:49 +0000498 if (const TagType *TagT =cast<TagDecl>(this)->TypeForDecl->getAs<TagType>())
Douglas Gregorcc636682009-02-17 23:15:12 +0000499 if (TagT->isBeingDefined() ||
500 (TagT->getDecl() && TagT->getDecl()->isDefinition()))
501 return TagT->getDecl();
502 return this;
503 }
504
Douglas Gregor44b43212008-12-11 16:49:14 +0000505 assert(DeclKind >= Decl::FunctionFirst && DeclKind <= Decl::FunctionLast &&
506 "Unknown DeclContext kind");
507 return this;
508 }
509}
510
511DeclContext *DeclContext::getNextContext() {
512 switch (DeclKind) {
Douglas Gregor44b43212008-12-11 16:49:14 +0000513 case Decl::Namespace:
514 // Return the next namespace
515 return static_cast<NamespaceDecl*>(this)->getNextNamespace();
516
517 default:
Douglas Gregor44b43212008-12-11 16:49:14 +0000518 return 0;
519 }
520}
521
Douglas Gregor2cf26342009-04-09 22:27:44 +0000522/// \brief Load the declarations within this lexical storage from an
523/// external source.
524void
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000525DeclContext::LoadLexicalDeclsFromExternalStorage() const {
526 ExternalASTSource *Source = getParentASTContext().getExternalSource();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000527 assert(hasExternalLexicalStorage() && Source && "No external storage?");
528
Eli Friedmanb0156ea2009-04-27 23:43:36 +0000529 llvm::SmallVector<uint32_t, 64> Decls;
Douglas Gregor2cf26342009-04-09 22:27:44 +0000530 if (Source->ReadDeclsLexicallyInContext(const_cast<DeclContext *>(this),
531 Decls))
532 return;
533
534 // There is no longer any lexical storage in this context
535 ExternalLexicalStorage = false;
536
537 if (Decls.empty())
538 return;
539
540 // Resolve all of the declaration IDs into declarations, building up
541 // a chain of declarations via the Decl::NextDeclInContext field.
542 Decl *FirstNewDecl = 0;
543 Decl *PrevDecl = 0;
544 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
545 Decl *D = Source->GetDecl(Decls[I]);
546 if (PrevDecl)
547 PrevDecl->NextDeclInContext = D;
548 else
549 FirstNewDecl = D;
550
551 PrevDecl = D;
552 }
553
554 // Splice the newly-read declarations into the beginning of the list
555 // of declarations.
556 PrevDecl->NextDeclInContext = FirstDecl;
557 FirstDecl = FirstNewDecl;
558 if (!LastDecl)
559 LastDecl = PrevDecl;
560}
561
562void
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000563DeclContext::LoadVisibleDeclsFromExternalStorage() const {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000564 DeclContext *This = const_cast<DeclContext *>(this);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000565 ExternalASTSource *Source = getParentASTContext().getExternalSource();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000566 assert(hasExternalVisibleStorage() && Source && "No external storage?");
567
568 llvm::SmallVector<VisibleDeclaration, 64> Decls;
569 if (Source->ReadDeclsVisibleInContext(This, Decls))
570 return;
571
572 // There is no longer any visible storage in this context
573 ExternalVisibleStorage = false;
574
575 // Load the declaration IDs for all of the names visible in this
576 // context.
577 assert(!LookupPtr && "Have a lookup map before de-serialization?");
578 StoredDeclsMap *Map = new StoredDeclsMap;
579 LookupPtr = Map;
580 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
581 (*Map)[Decls[I].Name].setFromDeclIDs(Decls[I].Declarations);
582 }
583}
584
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000585DeclContext::decl_iterator DeclContext::decls_begin() const {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000586 if (hasExternalLexicalStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000587 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000588
589 // FIXME: Check whether we need to load some declarations from
590 // external storage.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000591 return decl_iterator(FirstDecl);
592}
593
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000594DeclContext::decl_iterator DeclContext::decls_end() const {
Douglas Gregor2cf26342009-04-09 22:27:44 +0000595 if (hasExternalLexicalStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000596 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000597
Douglas Gregor6ab35242009-04-09 21:40:53 +0000598 return decl_iterator();
599}
600
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000601bool DeclContext::decls_empty() const {
Douglas Gregor8038d512009-04-10 17:25:41 +0000602 if (hasExternalLexicalStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000603 LoadLexicalDeclsFromExternalStorage();
Douglas Gregor8038d512009-04-10 17:25:41 +0000604
605 return !FirstDecl;
606}
607
John McCall3f9a8a62009-08-11 06:59:38 +0000608void DeclContext::addHiddenDecl(Decl *D) {
Chris Lattner7f0be132009-02-20 00:56:18 +0000609 assert(D->getLexicalDeclContext() == this &&
610 "Decl inserted into wrong lexical context");
Chris Lattner244a67d2009-03-28 06:04:26 +0000611 assert(!D->getNextDeclInContext() && D != LastDecl &&
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000612 "Decl already inserted into a DeclContext");
613
614 if (FirstDecl) {
Chris Lattner244a67d2009-03-28 06:04:26 +0000615 LastDecl->NextDeclInContext = D;
Douglas Gregor6037fcb2009-01-09 19:42:16 +0000616 LastDecl = D;
617 } else {
618 FirstDecl = LastDecl = D;
619 }
John McCall3f9a8a62009-08-11 06:59:38 +0000620}
621
622void DeclContext::addDecl(Decl *D) {
623 addHiddenDecl(D);
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000624
625 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000626 ND->getDeclContext()->makeDeclVisibleInContext(ND);
Douglas Gregor44b43212008-12-11 16:49:14 +0000627}
628
Douglas Gregor074149e2009-01-05 19:45:36 +0000629/// buildLookup - Build the lookup data structure with all of the
630/// declarations in DCtx (and any other contexts linked to it or
631/// transparent contexts nested within it).
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000632void DeclContext::buildLookup(DeclContext *DCtx) {
Douglas Gregor074149e2009-01-05 19:45:36 +0000633 for (; DCtx; DCtx = DCtx->getNextContext()) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000634 for (decl_iterator D = DCtx->decls_begin(),
635 DEnd = DCtx->decls_end();
Douglas Gregor4f3b8f82009-01-06 07:17:58 +0000636 D != DEnd; ++D) {
John McCall3f9a8a62009-08-11 06:59:38 +0000637 // Insert this declaration into the lookup structure, but only
638 // if it's semantically in its decl context. During non-lazy
639 // lookup building, this is implicitly enforced by addDecl.
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000640 if (NamedDecl *ND = dyn_cast<NamedDecl>(*D))
John McCall3f9a8a62009-08-11 06:59:38 +0000641 if (D->getDeclContext() == DCtx)
642 makeDeclVisibleInContextImpl(ND);
Douglas Gregor074149e2009-01-05 19:45:36 +0000643
644 // If this declaration is itself a transparent declaration context,
645 // add its members (recursively).
646 if (DeclContext *InnerCtx = dyn_cast<DeclContext>(*D))
647 if (InnerCtx->isTransparentContext())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000648 buildLookup(InnerCtx->getPrimaryContext());
Douglas Gregor074149e2009-01-05 19:45:36 +0000649 }
650 }
651}
652
Douglas Gregor44b43212008-12-11 16:49:14 +0000653DeclContext::lookup_result
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000654DeclContext::lookup(DeclarationName Name) {
Steve Naroff0701bbb2009-01-08 17:28:14 +0000655 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor44b43212008-12-11 16:49:14 +0000656 if (PrimaryContext != this)
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000657 return PrimaryContext->lookup(Name);
Douglas Gregor44b43212008-12-11 16:49:14 +0000658
Douglas Gregor2cf26342009-04-09 22:27:44 +0000659 if (hasExternalVisibleStorage())
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000660 LoadVisibleDeclsFromExternalStorage();
Douglas Gregor2cf26342009-04-09 22:27:44 +0000661
Douglas Gregor3fc749d2008-12-23 00:26:44 +0000662 /// If there is no lookup data structure, build one now by walking
Douglas Gregor44b43212008-12-11 16:49:14 +0000663 /// all of the linked DeclContexts (in declaration order!) and
664 /// inserting their values.
Douglas Gregorc36c5402009-04-09 17:29:08 +0000665 if (!LookupPtr) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000666 buildLookup(this);
Douglas Gregor44b43212008-12-11 16:49:14 +0000667
Douglas Gregorc36c5402009-04-09 17:29:08 +0000668 if (!LookupPtr)
Chris Lattner91942502009-02-20 00:55:03 +0000669 return lookup_result(0, 0);
Douglas Gregorc36c5402009-04-09 17:29:08 +0000670 }
Douglas Gregor44b43212008-12-11 16:49:14 +0000671
Douglas Gregorc36c5402009-04-09 17:29:08 +0000672 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(LookupPtr);
673 StoredDeclsMap::iterator Pos = Map->find(Name);
674 if (Pos == Map->end())
675 return lookup_result(0, 0);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000676 return Pos->second.getLookupResult(getParentASTContext());
Douglas Gregor44b43212008-12-11 16:49:14 +0000677}
678
679DeclContext::lookup_const_result
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000680DeclContext::lookup(DeclarationName Name) const {
681 return const_cast<DeclContext*>(this)->lookup(Name);
Douglas Gregor44b43212008-12-11 16:49:14 +0000682}
683
Chris Lattner0cf2b192009-03-27 19:19:59 +0000684DeclContext *DeclContext::getLookupContext() {
685 DeclContext *Ctx = this;
Douglas Gregor72de6672009-01-08 20:45:30 +0000686 // Skip through transparent contexts.
Douglas Gregorce356072009-01-06 23:51:29 +0000687 while (Ctx->isTransparentContext())
688 Ctx = Ctx->getParent();
689 return Ctx;
690}
691
Douglas Gregor88b70942009-02-25 22:02:03 +0000692DeclContext *DeclContext::getEnclosingNamespaceContext() {
693 DeclContext *Ctx = this;
694 // Skip through non-namespace, non-translation-unit contexts.
695 while (!Ctx->isFileContext() || Ctx->isTransparentContext())
696 Ctx = Ctx->getParent();
697 return Ctx->getPrimaryContext();
698}
699
John McCallab88d972009-08-31 22:39:49 +0000700void DeclContext::makeDeclVisibleInContext(NamedDecl *D, bool Recoverable) {
Douglas Gregorcc636682009-02-17 23:15:12 +0000701 // FIXME: This feels like a hack. Should DeclarationName support
702 // template-ids, or is there a better way to keep specializations
703 // from being visible?
704 if (isa<ClassTemplateSpecializationDecl>(D))
705 return;
706
Steve Naroff0701bbb2009-01-08 17:28:14 +0000707 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor44b43212008-12-11 16:49:14 +0000708 if (PrimaryContext != this) {
John McCallab88d972009-08-31 22:39:49 +0000709 PrimaryContext->makeDeclVisibleInContext(D, Recoverable);
Douglas Gregor44b43212008-12-11 16:49:14 +0000710 return;
711 }
712
713 // If we already have a lookup data structure, perform the insertion
714 // into it. Otherwise, be lazy and don't build that structure until
715 // someone asks for it.
John McCallab88d972009-08-31 22:39:49 +0000716 if (LookupPtr || !Recoverable)
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000717 makeDeclVisibleInContextImpl(D);
Douglas Gregor074149e2009-01-05 19:45:36 +0000718
Douglas Gregor074149e2009-01-05 19:45:36 +0000719 // If we are a transparent context, insert into our parent context,
720 // too. This operation is recursive.
721 if (isTransparentContext())
John McCallab88d972009-08-31 22:39:49 +0000722 getParent()->makeDeclVisibleInContext(D, Recoverable);
Douglas Gregor44b43212008-12-11 16:49:14 +0000723}
724
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000725void DeclContext::makeDeclVisibleInContextImpl(NamedDecl *D) {
Douglas Gregor074149e2009-01-05 19:45:36 +0000726 // Skip unnamed declarations.
727 if (!D->getDeclName())
728 return;
729
Douglas Gregorcc636682009-02-17 23:15:12 +0000730 // FIXME: This feels like a hack. Should DeclarationName support
731 // template-ids, or is there a better way to keep specializations
732 // from being visible?
733 if (isa<ClassTemplateSpecializationDecl>(D))
734 return;
735
Douglas Gregorc36c5402009-04-09 17:29:08 +0000736 if (!LookupPtr)
737 LookupPtr = new StoredDeclsMap;
Douglas Gregor44b43212008-12-11 16:49:14 +0000738
739 // Insert this declaration into the map.
Douglas Gregorc36c5402009-04-09 17:29:08 +0000740 StoredDeclsMap &Map = *static_cast<StoredDeclsMap*>(LookupPtr);
Chris Lattner67762a32009-02-20 01:44:05 +0000741 StoredDeclsList &DeclNameEntries = Map[D->getDeclName()];
742 if (DeclNameEntries.isNull()) {
743 DeclNameEntries.setOnlyValue(D);
Chris Lattnerbd6c8002009-02-19 07:00:44 +0000744 return;
Douglas Gregor44b43212008-12-11 16:49:14 +0000745 }
Chris Lattner91942502009-02-20 00:55:03 +0000746
Chris Lattnerbdc3d002009-02-20 01:10:07 +0000747 // If it is possible that this is a redeclaration, check to see if there is
748 // already a decl for which declarationReplaces returns true. If there is
749 // one, just replace it and return.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000750 if (DeclNameEntries.HandleRedeclaration(getParentASTContext(), D))
Chris Lattner67762a32009-02-20 01:44:05 +0000751 return;
Chris Lattner91942502009-02-20 00:55:03 +0000752
Chris Lattnerbd6c8002009-02-19 07:00:44 +0000753 // Put this declaration into the appropriate slot.
Chris Lattner67762a32009-02-20 01:44:05 +0000754 DeclNameEntries.AddSubsequentDecl(D);
Douglas Gregor44b43212008-12-11 16:49:14 +0000755}
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000756
757/// Returns iterator range [First, Last) of UsingDirectiveDecls stored within
758/// this context.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000759DeclContext::udir_iterator_range
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000760DeclContext::getUsingDirectives() const {
761 lookup_const_result Result = lookup(UsingDirectiveDecl::getName());
Douglas Gregor2a3009a2009-02-03 19:21:40 +0000762 return udir_iterator_range(reinterpret_cast<udir_iterator>(Result.first),
763 reinterpret_cast<udir_iterator>(Result.second));
764}
Douglas Gregor2cf26342009-04-09 22:27:44 +0000765
766void StoredDeclsList::materializeDecls(ASTContext &Context) {
767 if (isNull())
768 return;
769
770 switch ((DataKind)(Data & 0x03)) {
771 case DK_Decl:
772 case DK_Decl_Vector:
773 break;
774
775 case DK_DeclID: {
776 // Resolve this declaration ID to an actual declaration by
777 // querying the external AST source.
778 unsigned DeclID = Data >> 2;
779
780 ExternalASTSource *Source = Context.getExternalSource();
781 assert(Source && "No external AST source available!");
782
783 Data = reinterpret_cast<uintptr_t>(Source->GetDecl(DeclID));
784 break;
785 }
786
787 case DK_ID_Vector: {
788 // We have a vector of declaration IDs. Resolve all of them to
789 // actual declarations.
790 VectorTy &Vector = *getAsVector();
791 ExternalASTSource *Source = Context.getExternalSource();
792 assert(Source && "No external AST source available!");
793
794 for (unsigned I = 0, N = Vector.size(); I != N; ++I)
795 Vector[I] = reinterpret_cast<uintptr_t>(Source->GetDecl(Vector[I]));
796
797 Data = (Data & ~0x03) | DK_Decl_Vector;
798 break;
799 }
800 }
801}