blob: 5d30795abd710c3e52d197fab73ff25980f3066d [file] [log] [blame]
Eli Friedmana8a09ac2008-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 Gregor469fc9a2009-02-02 23:39:07 +000015#include "clang/AST/Decl.h"
Douglas Gregord2b6edc2009-04-07 17:20:56 +000016#include "clang/AST/DeclContextInternals.h"
Argiris Kirtzidis6df1fc02008-06-09 21:05:31 +000017#include "clang/AST/DeclCXX.h"
Douglas Gregor279272e2009-02-04 19:02:06 +000018#include "clang/AST/DeclObjC.h"
19#include "clang/AST/DeclTemplate.h"
Douglas Gregorc34897d2009-04-09 22:27:44 +000020#include "clang/AST/ExternalASTSource.h"
Eli Friedmana8a09ac2008-06-07 16:52:53 +000021#include "clang/AST/ASTContext.h"
Douglas Gregor8acb7272008-12-11 16:49:14 +000022#include "clang/AST/Type.h"
Sebastian Redlbc9ef252009-04-26 20:35:05 +000023#include "clang/AST/Stmt.h"
24#include "clang/AST/StmtCXX.h"
Eli Friedmana8a09ac2008-06-07 16:52:53 +000025#include "llvm/ADT/DenseMap.h"
Chris Lattnerc309ade2009-03-05 08:00:35 +000026#include "llvm/Support/raw_ostream.h"
Douglas Gregor6e71edc2008-12-23 21:05:05 +000027#include <algorithm>
Chris Lattner7d6220c2009-03-02 22:20:04 +000028#include <cstdio>
Douglas Gregorddfd9d52008-12-23 00:26:44 +000029#include <vector>
Eli Friedmana8a09ac2008-06-07 16:52:53 +000030using namespace clang;
31
32//===----------------------------------------------------------------------===//
33// Statistics
34//===----------------------------------------------------------------------===//
35
Douglas Gregor469fc9a2009-02-02 23:39:07 +000036#define DECL(Derived, Base) static int n##Derived##s = 0;
37#include "clang/AST/DeclNodes.def"
Eli Friedmana8a09ac2008-06-07 16:52:53 +000038
39static bool StatSwitch = false;
40
Eli Friedmana8a09ac2008-06-07 16:52:53 +000041const char *Decl::getDeclKindName() const {
42 switch (DeclKind) {
Douglas Gregor469fc9a2009-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 Friedmana8a09ac2008-06-07 16:52:53 +000046 }
47}
48
Steve Naroffe5f128a2009-01-20 19:53:53 +000049const char *DeclContext::getDeclKindName() const {
50 switch (DeclKind) {
Douglas Gregor469fc9a2009-02-02 23:39:07 +000051 default: assert(0 && "Declaration context not in DeclNodes.def!");
Argiris Kirtzidis2d9b7612009-02-16 14:28:33 +000052#define DECL(Derived, Base) case Decl::Derived: return #Derived;
Douglas Gregor469fc9a2009-02-02 23:39:07 +000053#include "clang/AST/DeclNodes.def"
Steve Naroffe5f128a2009-01-20 19:53:53 +000054 }
55}
56
Eli Friedmana8a09ac2008-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 Friedmana8a09ac2008-06-07 16:52:53 +000065
Douglas Gregor469fc9a2009-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 Friedmana8a09ac2008-06-07 16:52:53 +000080
Douglas Gregor469fc9a2009-02-02 23:39:07 +000081 fprintf(stderr, "Total bytes = %d\n", totalBytes);
Eli Friedmana8a09ac2008-06-07 16:52:53 +000082}
83
84void Decl::addDeclKind(Kind k) {
85 switch (k) {
Douglas Gregor469fc9a2009-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 Friedmana8a09ac2008-06-07 16:52:53 +000089 }
90}
91
Anders Carlssonc2018a72009-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 Gregorb60eb752009-06-25 22:08:12 +000099bool Decl::isFunctionOrFunctionTemplate() const {
Anders Carlsson5847da52009-06-26 05:26:50 +0000100 if (const UsingDecl *UD = dyn_cast<UsingDecl>(this))
101 return UD->getTargetDecl()->isFunctionOrFunctionTemplate();
102
Douglas Gregorb60eb752009-06-25 22:08:12 +0000103 return isa<FunctionDecl>(this) || isa<FunctionTemplateDecl>(this);
104}
105
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000106//===----------------------------------------------------------------------===//
Chris Lattnerc309ade2009-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 Friedmana8a09ac2008-06-07 16:52:53 +0000128// Decl Implementation
129//===----------------------------------------------------------------------===//
130
Chris Lattner8752fe02009-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 Gregoraf8ad2b2009-01-20 01:17:11 +0000139void Decl::setDeclContext(DeclContext *DC) {
140 if (isOutOfSemaDC())
141 delete getMultipleDC();
142
Chris Lattner5ce4f6d2009-03-29 06:06:59 +0000143 DeclCtx = DC;
Douglas Gregoraf8ad2b2009-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 Lattner5ce4f6d2009-03-29 06:06:59 +0000154 DeclCtx = MDC;
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000155 } else {
156 getMultipleDC()->LexicalDC = DC;
157 }
158}
159
Argiris Kirtzidis2a6dca12009-06-29 17:38:40 +0000160TranslationUnitDecl *Decl::getTranslationUnitDecl() {
Argiris Kirtzidisf7993cb2009-06-30 02:34:53 +0000161 if (TranslationUnitDecl *TUD = dyn_cast<TranslationUnitDecl>(this))
162 return TUD;
163
Argiris Kirtzidis2a6dca12009-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 Lattner8752fe02009-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 Gregor683a1142009-06-20 00:51:54 +0000193 case Using:
Anders Carlsson4e750062009-08-28 05:30:28 +0000194 case UnresolvedUsing:
Chris Lattner8752fe02009-03-27 20:18:19 +0000195 case ObjCMethod:
196 case ObjCContainer:
197 case ObjCCategory:
198 case ObjCInterface:
Chris Lattner8752fe02009-03-27 20:18:19 +0000199 case ObjCProperty:
200 case ObjCCompatibleAlias:
201 return IDNS_Ordinary;
John McCall36493082009-08-11 06:59:38 +0000202
Chris Lattner8752fe02009-03-27 20:18:19 +0000203 case ObjCProtocol:
Douglas Gregorafd5eb32009-04-24 00:11:27 +0000204 return IDNS_ObjCProtocol;
Chris Lattner8752fe02009-03-27 20:18:19 +0000205
Douglas Gregorafd5eb32009-04-24 00:11:27 +0000206 case ObjCImplementation:
207 return IDNS_ObjCImplementation;
208
209 case ObjCCategoryImpl:
210 return IDNS_ObjCCategoryImpl;
211
Chris Lattner8752fe02009-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 Carlsson1bf91fb2009-03-28 23:02:53 +0000228 case NamespaceAlias:
Chris Lattner8752fe02009-03-27 20:18:19 +0000229 return IDNS_Tag | IDNS_Ordinary;
230
231 // Never have names.
John McCall7de15912009-08-28 07:59:38 +0000232 case Friend:
Chris Lattner8752fe02009-03-27 20:18:19 +0000233 case LinkageSpec:
234 case FileScopeAsm:
235 case StaticAssert:
236 case ObjCClass:
Chris Lattner8752fe02009-03-27 20:18:19 +0000237 case ObjCPropertyImpl:
238 case ObjCForwardProtocol:
239 case Block:
240 case TranslationUnit:
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000241
Chris Lattner8752fe02009-03-27 20:18:19 +0000242 // Aren't looked up?
243 case UsingDirective:
244 case ClassTemplateSpecialization:
Douglas Gregor58944ac2009-05-31 09:31:02 +0000245 case ClassTemplatePartialSpecialization:
Chris Lattner8752fe02009-03-27 20:18:19 +0000246 return 0;
247 }
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000248}
249
Argiris Kirtzidisfe5f9732009-06-30 02:34:44 +0000250void Decl::addAttr(Attr *NewAttr) {
251 Attr *&ExistingAttr = getASTContext().getDeclAttrs(this);
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000252
253 NewAttr->setNext(ExistingAttr);
254 ExistingAttr = NewAttr;
255
256 HasAttrs = true;
257}
258
Argiris Kirtzidisfe5f9732009-06-30 02:34:44 +0000259void Decl::invalidateAttrs() {
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000260 if (!HasAttrs) return;
Douglas Gregor98da6ae2009-06-18 16:11:24 +0000261
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000262 HasAttrs = false;
Argiris Kirtzidisfe5f9732009-06-30 02:34:44 +0000263 getASTContext().eraseDeclAttrs(this);
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000264}
265
Argiris Kirtzidisfe5f9732009-06-30 02:34:44 +0000266const Attr *Decl::getAttrsImpl() const {
Chris Lattner004a9bb2009-03-21 06:27:31 +0000267 assert(HasAttrs && "getAttrs() should verify this!");
Argiris Kirtzidisfe5f9732009-06-30 02:34:44 +0000268 return getASTContext().getDeclAttrs(this);
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000269}
270
Argiris Kirtzidisfe5f9732009-06-30 02:34:44 +0000271void Decl::swapAttrs(Decl *RHS) {
Eli Friedmana8a09ac2008-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)
Argiris Kirtzidisfe5f9732009-06-30 02:34:44 +0000280 return RHS->swapAttrs(this);
281
282 ASTContext &Context = getASTContext();
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000283
284 // Handle the case when both decls have attrs.
285 if (HasRHSAttr) {
Douglas Gregor98da6ae2009-06-18 16:11:24 +0000286 std::swap(Context.getDeclAttrs(this), Context.getDeclAttrs(RHS));
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000287 return;
288 }
289
290 // Otherwise, LHS has an attr and RHS doesn't.
Douglas Gregor98da6ae2009-06-18 16:11:24 +0000291 Context.getDeclAttrs(RHS) = Context.getDeclAttrs(this);
292 Context.eraseDeclAttrs(this);
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000293 this->HasAttrs = false;
294 RHS->HasAttrs = true;
295}
296
297
Chris Lattner0fc6d642009-03-04 06:05:19 +0000298void Decl::Destroy(ASTContext &C) {
299 // Free attributes for this decl.
300 if (HasAttrs) {
Douglas Gregor98da6ae2009-06-18 16:11:24 +0000301 C.getDeclAttrs(this)->Destroy(C);
Argiris Kirtzidisfe5f9732009-06-30 02:34:44 +0000302 invalidateAttrs();
Chris Lattner0fc6d642009-03-04 06:05:19 +0000303 HasAttrs = false;
304 }
305
Douglas Gregor8314d742009-01-13 19:47:12 +0000306#if 0
Douglas Gregorf4006be2009-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 Friedmana8a09ac2008-06-07 16:52:53 +0000310
Chris Lattnerfb8416a2009-03-28 06:04:26 +0000311 // Observe the unrolled recursion. By setting N->NextDeclInContext = 0x0
Douglas Gregoraf8ad2b2009-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 Lattnerfb8416a2009-03-28 06:04:26 +0000315 Decl* N = getNextDeclInContext();
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000316
317 while (N) {
Chris Lattnerfb8416a2009-03-28 06:04:26 +0000318 Decl* Tmp = N->getNextDeclInContext();
319 N->NextDeclInContext = 0;
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000320 N->Destroy(C);
321 N = Tmp;
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000322 }
Douglas Gregor8314d742009-01-13 19:47:12 +0000323
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000324 this->~Decl();
Steve Naroff5abb0282009-01-27 21:25:57 +0000325 C.Deallocate((void *)this);
Douglas Gregorf4006be2009-01-20 04:25:11 +0000326#endif
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000327}
328
Argiris Kirtzidis4b662ea2008-10-12 16:14:48 +0000329Decl *Decl::castFromDeclContext (const DeclContext *D) {
Argiris Kirtzidis981aa132009-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 }
Argiris Kirtzidis4b662ea2008-10-12 16:14:48 +0000345}
346
347DeclContext *Decl::castToDeclContext(const Decl *D) {
Argiris Kirtzidis981aa132009-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 }
Argiris Kirtzidis4b662ea2008-10-12 16:14:48 +0000363}
364
Argiris Kirtzidisccb9efe2009-06-30 02:35:26 +0000365CompoundStmt* Decl::getCompoundBody() const {
366 return dyn_cast_or_null<CompoundStmt>(getBody());
Sebastian Redlbc9ef252009-04-26 20:35:05 +0000367}
368
Argiris Kirtzidisccb9efe2009-06-30 02:35:26 +0000369SourceLocation Decl::getBodyRBrace() const {
370 Stmt *Body = getBody();
Sebastian Redlbc9ef252009-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 Carlsson8ea6a322009-03-25 23:38:06 +0000380#ifndef NDEBUG
381void Decl::CheckAccessDeclContext() const {
Anders Carlssone6fe83e2009-08-29 01:13:02 +0000382 if (isa<TranslationUnitDecl>(this) ||
383 !isa<CXXRecordDecl>(getDeclContext()))
384 return;
385
386 // FIXME: Should friend declarations have access specifiers?
387 if (isa<FriendDecl>(this) ||
388 getFriendObjectKind() != FOK_None)
389 return;
390
391 assert(Access != AS_none &&
Anders Carlsson8ea6a322009-03-25 23:38:06 +0000392 "Access specifier is AS_none inside a record decl");
393}
394
395#endif
396
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000397//===----------------------------------------------------------------------===//
398// DeclContext Implementation
399//===----------------------------------------------------------------------===//
400
Argiris Kirtzidis981aa132009-02-16 14:29:28 +0000401bool DeclContext::classof(const Decl *D) {
402 switch (D->getKind()) {
403#define DECL_CONTEXT(Name) case Decl::Name:
404#define DECL_CONTEXT_BASE(Name)
405#include "clang/AST/DeclNodes.def"
406 return true;
407 default:
408#define DECL_CONTEXT_BASE(Name) \
409 if (D->getKind() >= Decl::Name##First && \
410 D->getKind() <= Decl::Name##Last) \
411 return true;
412#include "clang/AST/DeclNodes.def"
413 return false;
414 }
415}
416
Douglas Gregor8acb7272008-12-11 16:49:14 +0000417DeclContext::~DeclContext() {
Douglas Gregor13c27cd2009-04-09 17:29:08 +0000418 delete static_cast<StoredDeclsMap*>(LookupPtr);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000419}
420
421void DeclContext::DestroyDecls(ASTContext &C) {
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000422 for (decl_iterator D = decls_begin(); D != decls_end(); )
Douglas Gregorf4006be2009-01-20 04:25:11 +0000423 (*D++)->Destroy(C);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000424}
425
Douglas Gregor1a6b88e2009-05-28 16:34:51 +0000426bool DeclContext::isDependentContext() const {
427 if (isFileContext())
428 return false;
429
Douglas Gregor58944ac2009-05-31 09:31:02 +0000430 if (isa<ClassTemplatePartialSpecializationDecl>(this))
431 return true;
432
Douglas Gregor1a6b88e2009-05-28 16:34:51 +0000433 if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this))
434 if (Record->getDescribedClassTemplate())
435 return true;
436
437 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this))
438 if (Function->getDescribedFunctionTemplate())
439 return true;
440
441 return getParent() && getParent()->isDependentContext();
442}
443
Douglas Gregord8028382009-01-05 19:45:36 +0000444bool DeclContext::isTransparentContext() const {
445 if (DeclKind == Decl::Enum)
446 return true; // FIXME: Check for C++0x scoped enums
447 else if (DeclKind == Decl::LinkageSpec)
448 return true;
Douglas Gregor1db12932009-02-26 00:02:51 +0000449 else if (DeclKind >= Decl::RecordFirst && DeclKind <= Decl::RecordLast)
Douglas Gregor723d3332009-01-07 00:43:41 +0000450 return cast<RecordDecl>(this)->isAnonymousStructOrUnion();
Douglas Gregord8028382009-01-05 19:45:36 +0000451 else if (DeclKind == Decl::Namespace)
452 return false; // FIXME: Check for C++0x inline namespaces
453
454 return false;
455}
456
Douglas Gregor430390d2009-08-27 06:03:53 +0000457bool DeclContext::Encloses(DeclContext *DC) {
458 if (getPrimaryContext() != this)
459 return getPrimaryContext()->Encloses(DC);
460
461 for (; DC; DC = DC->getParent())
462 if (DC->getPrimaryContext() == this)
463 return true;
464 return false;
465}
466
Steve Naroffab63fd62009-01-08 17:28:14 +0000467DeclContext *DeclContext::getPrimaryContext() {
Douglas Gregor8acb7272008-12-11 16:49:14 +0000468 switch (DeclKind) {
Douglas Gregor8acb7272008-12-11 16:49:14 +0000469 case Decl::TranslationUnit:
Douglas Gregord8028382009-01-05 19:45:36 +0000470 case Decl::LinkageSpec:
471 case Decl::Block:
Douglas Gregor8acb7272008-12-11 16:49:14 +0000472 // There is only one DeclContext for these entities.
473 return this;
474
475 case Decl::Namespace:
476 // The original namespace is our primary context.
477 return static_cast<NamespaceDecl*>(this)->getOriginalNamespace();
478
Douglas Gregor8acb7272008-12-11 16:49:14 +0000479 case Decl::ObjCMethod:
480 return this;
481
482 case Decl::ObjCInterface:
Steve Naroffab63fd62009-01-08 17:28:14 +0000483 case Decl::ObjCProtocol:
484 case Decl::ObjCCategory:
Douglas Gregor8acb7272008-12-11 16:49:14 +0000485 // FIXME: Can Objective-C interfaces be forward-declared?
486 return this;
487
Steve Naroffab63fd62009-01-08 17:28:14 +0000488 case Decl::ObjCImplementation:
489 case Decl::ObjCCategoryImpl:
490 return this;
491
Douglas Gregor8acb7272008-12-11 16:49:14 +0000492 default:
Douglas Gregora08b6c72009-02-17 23:15:12 +0000493 if (DeclKind >= Decl::TagFirst && DeclKind <= Decl::TagLast) {
494 // If this is a tag type that has a definition or is currently
495 // being defined, that definition is our primary context.
Ted Kremenekd00cd9e2009-07-29 21:53:49 +0000496 if (const TagType *TagT =cast<TagDecl>(this)->TypeForDecl->getAs<TagType>())
Douglas Gregora08b6c72009-02-17 23:15:12 +0000497 if (TagT->isBeingDefined() ||
498 (TagT->getDecl() && TagT->getDecl()->isDefinition()))
499 return TagT->getDecl();
500 return this;
501 }
502
Douglas Gregor8acb7272008-12-11 16:49:14 +0000503 assert(DeclKind >= Decl::FunctionFirst && DeclKind <= Decl::FunctionLast &&
504 "Unknown DeclContext kind");
505 return this;
506 }
507}
508
509DeclContext *DeclContext::getNextContext() {
510 switch (DeclKind) {
Douglas Gregor8acb7272008-12-11 16:49:14 +0000511 case Decl::Namespace:
512 // Return the next namespace
513 return static_cast<NamespaceDecl*>(this)->getNextNamespace();
514
515 default:
Douglas Gregor8acb7272008-12-11 16:49:14 +0000516 return 0;
517 }
518}
519
Douglas Gregorc34897d2009-04-09 22:27:44 +0000520/// \brief Load the declarations within this lexical storage from an
521/// external source.
522void
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000523DeclContext::LoadLexicalDeclsFromExternalStorage() const {
524 ExternalASTSource *Source = getParentASTContext().getExternalSource();
Douglas Gregorc34897d2009-04-09 22:27:44 +0000525 assert(hasExternalLexicalStorage() && Source && "No external storage?");
526
Eli Friedmancdf59742009-04-27 23:43:36 +0000527 llvm::SmallVector<uint32_t, 64> Decls;
Douglas Gregorc34897d2009-04-09 22:27:44 +0000528 if (Source->ReadDeclsLexicallyInContext(const_cast<DeclContext *>(this),
529 Decls))
530 return;
531
532 // There is no longer any lexical storage in this context
533 ExternalLexicalStorage = false;
534
535 if (Decls.empty())
536 return;
537
538 // Resolve all of the declaration IDs into declarations, building up
539 // a chain of declarations via the Decl::NextDeclInContext field.
540 Decl *FirstNewDecl = 0;
541 Decl *PrevDecl = 0;
542 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
543 Decl *D = Source->GetDecl(Decls[I]);
544 if (PrevDecl)
545 PrevDecl->NextDeclInContext = D;
546 else
547 FirstNewDecl = D;
548
549 PrevDecl = D;
550 }
551
552 // Splice the newly-read declarations into the beginning of the list
553 // of declarations.
554 PrevDecl->NextDeclInContext = FirstDecl;
555 FirstDecl = FirstNewDecl;
556 if (!LastDecl)
557 LastDecl = PrevDecl;
558}
559
560void
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000561DeclContext::LoadVisibleDeclsFromExternalStorage() const {
Douglas Gregorc34897d2009-04-09 22:27:44 +0000562 DeclContext *This = const_cast<DeclContext *>(this);
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000563 ExternalASTSource *Source = getParentASTContext().getExternalSource();
Douglas Gregorc34897d2009-04-09 22:27:44 +0000564 assert(hasExternalVisibleStorage() && Source && "No external storage?");
565
566 llvm::SmallVector<VisibleDeclaration, 64> Decls;
567 if (Source->ReadDeclsVisibleInContext(This, Decls))
568 return;
569
570 // There is no longer any visible storage in this context
571 ExternalVisibleStorage = false;
572
573 // Load the declaration IDs for all of the names visible in this
574 // context.
575 assert(!LookupPtr && "Have a lookup map before de-serialization?");
576 StoredDeclsMap *Map = new StoredDeclsMap;
577 LookupPtr = Map;
578 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
579 (*Map)[Decls[I].Name].setFromDeclIDs(Decls[I].Declarations);
580 }
581}
582
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000583DeclContext::decl_iterator DeclContext::decls_begin() const {
Douglas Gregorc34897d2009-04-09 22:27:44 +0000584 if (hasExternalLexicalStorage())
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000585 LoadLexicalDeclsFromExternalStorage();
Douglas Gregorc34897d2009-04-09 22:27:44 +0000586
587 // FIXME: Check whether we need to load some declarations from
588 // external storage.
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000589 return decl_iterator(FirstDecl);
590}
591
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000592DeclContext::decl_iterator DeclContext::decls_end() const {
Douglas Gregorc34897d2009-04-09 22:27:44 +0000593 if (hasExternalLexicalStorage())
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000594 LoadLexicalDeclsFromExternalStorage();
Douglas Gregorc34897d2009-04-09 22:27:44 +0000595
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000596 return decl_iterator();
597}
598
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000599bool DeclContext::decls_empty() const {
Douglas Gregorac8f2802009-04-10 17:25:41 +0000600 if (hasExternalLexicalStorage())
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000601 LoadLexicalDeclsFromExternalStorage();
Douglas Gregorac8f2802009-04-10 17:25:41 +0000602
603 return !FirstDecl;
604}
605
John McCall36493082009-08-11 06:59:38 +0000606void DeclContext::addHiddenDecl(Decl *D) {
Chris Lattner53602072009-02-20 00:56:18 +0000607 assert(D->getLexicalDeclContext() == this &&
608 "Decl inserted into wrong lexical context");
Chris Lattnerfb8416a2009-03-28 06:04:26 +0000609 assert(!D->getNextDeclInContext() && D != LastDecl &&
Douglas Gregord1675382009-01-09 19:42:16 +0000610 "Decl already inserted into a DeclContext");
611
612 if (FirstDecl) {
Chris Lattnerfb8416a2009-03-28 06:04:26 +0000613 LastDecl->NextDeclInContext = D;
Douglas Gregord1675382009-01-09 19:42:16 +0000614 LastDecl = D;
615 } else {
616 FirstDecl = LastDecl = D;
617 }
John McCall36493082009-08-11 06:59:38 +0000618}
619
620void DeclContext::addDecl(Decl *D) {
621 addHiddenDecl(D);
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000622
623 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000624 ND->getDeclContext()->makeDeclVisibleInContext(ND);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000625}
626
Douglas Gregord8028382009-01-05 19:45:36 +0000627/// buildLookup - Build the lookup data structure with all of the
628/// declarations in DCtx (and any other contexts linked to it or
629/// transparent contexts nested within it).
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000630void DeclContext::buildLookup(DeclContext *DCtx) {
Douglas Gregord8028382009-01-05 19:45:36 +0000631 for (; DCtx; DCtx = DCtx->getNextContext()) {
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000632 for (decl_iterator D = DCtx->decls_begin(),
633 DEnd = DCtx->decls_end();
Douglas Gregorf0464ec2009-01-06 07:17:58 +0000634 D != DEnd; ++D) {
John McCall36493082009-08-11 06:59:38 +0000635 // Insert this declaration into the lookup structure, but only
636 // if it's semantically in its decl context. During non-lazy
637 // lookup building, this is implicitly enforced by addDecl.
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000638 if (NamedDecl *ND = dyn_cast<NamedDecl>(*D))
John McCall36493082009-08-11 06:59:38 +0000639 if (D->getDeclContext() == DCtx)
640 makeDeclVisibleInContextImpl(ND);
Douglas Gregord8028382009-01-05 19:45:36 +0000641
642 // If this declaration is itself a transparent declaration context,
643 // add its members (recursively).
644 if (DeclContext *InnerCtx = dyn_cast<DeclContext>(*D))
645 if (InnerCtx->isTransparentContext())
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000646 buildLookup(InnerCtx->getPrimaryContext());
Douglas Gregord8028382009-01-05 19:45:36 +0000647 }
648 }
649}
650
Douglas Gregor8acb7272008-12-11 16:49:14 +0000651DeclContext::lookup_result
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000652DeclContext::lookup(DeclarationName Name) {
Steve Naroffab63fd62009-01-08 17:28:14 +0000653 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor8acb7272008-12-11 16:49:14 +0000654 if (PrimaryContext != this)
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000655 return PrimaryContext->lookup(Name);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000656
Douglas Gregorc34897d2009-04-09 22:27:44 +0000657 if (hasExternalVisibleStorage())
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000658 LoadVisibleDeclsFromExternalStorage();
Douglas Gregorc34897d2009-04-09 22:27:44 +0000659
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000660 /// If there is no lookup data structure, build one now by walking
Douglas Gregor8acb7272008-12-11 16:49:14 +0000661 /// all of the linked DeclContexts (in declaration order!) and
662 /// inserting their values.
Douglas Gregor13c27cd2009-04-09 17:29:08 +0000663 if (!LookupPtr) {
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000664 buildLookup(this);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000665
Douglas Gregor13c27cd2009-04-09 17:29:08 +0000666 if (!LookupPtr)
Chris Lattner265f01d2009-02-20 00:55:03 +0000667 return lookup_result(0, 0);
Douglas Gregor13c27cd2009-04-09 17:29:08 +0000668 }
Douglas Gregor8acb7272008-12-11 16:49:14 +0000669
Douglas Gregor13c27cd2009-04-09 17:29:08 +0000670 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(LookupPtr);
671 StoredDeclsMap::iterator Pos = Map->find(Name);
672 if (Pos == Map->end())
673 return lookup_result(0, 0);
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000674 return Pos->second.getLookupResult(getParentASTContext());
Douglas Gregor8acb7272008-12-11 16:49:14 +0000675}
676
677DeclContext::lookup_const_result
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000678DeclContext::lookup(DeclarationName Name) const {
679 return const_cast<DeclContext*>(this)->lookup(Name);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000680}
681
Chris Lattner9a502bd2009-03-27 19:19:59 +0000682DeclContext *DeclContext::getLookupContext() {
683 DeclContext *Ctx = this;
Douglas Gregordb568cf2009-01-08 20:45:30 +0000684 // Skip through transparent contexts.
Douglas Gregor69e781f2009-01-06 23:51:29 +0000685 while (Ctx->isTransparentContext())
686 Ctx = Ctx->getParent();
687 return Ctx;
688}
689
Douglas Gregor0d93f692009-02-25 22:02:03 +0000690DeclContext *DeclContext::getEnclosingNamespaceContext() {
691 DeclContext *Ctx = this;
692 // Skip through non-namespace, non-translation-unit contexts.
693 while (!Ctx->isFileContext() || Ctx->isTransparentContext())
694 Ctx = Ctx->getParent();
695 return Ctx->getPrimaryContext();
696}
697
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000698void DeclContext::makeDeclVisibleInContext(NamedDecl *D) {
Douglas Gregora08b6c72009-02-17 23:15:12 +0000699 // FIXME: This feels like a hack. Should DeclarationName support
700 // template-ids, or is there a better way to keep specializations
701 // from being visible?
702 if (isa<ClassTemplateSpecializationDecl>(D))
703 return;
704
Steve Naroffab63fd62009-01-08 17:28:14 +0000705 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor8acb7272008-12-11 16:49:14 +0000706 if (PrimaryContext != this) {
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000707 PrimaryContext->makeDeclVisibleInContext(D);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000708 return;
709 }
710
711 // If we already have a lookup data structure, perform the insertion
712 // into it. Otherwise, be lazy and don't build that structure until
713 // someone asks for it.
Douglas Gregor13c27cd2009-04-09 17:29:08 +0000714 if (LookupPtr)
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000715 makeDeclVisibleInContextImpl(D);
Douglas Gregord8028382009-01-05 19:45:36 +0000716
Douglas Gregord8028382009-01-05 19:45:36 +0000717 // If we are a transparent context, insert into our parent context,
718 // too. This operation is recursive.
719 if (isTransparentContext())
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000720 getParent()->makeDeclVisibleInContext(D);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000721}
722
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000723void DeclContext::makeDeclVisibleInContextImpl(NamedDecl *D) {
Douglas Gregord8028382009-01-05 19:45:36 +0000724 // Skip unnamed declarations.
725 if (!D->getDeclName())
726 return;
727
Douglas Gregora08b6c72009-02-17 23:15:12 +0000728 // FIXME: This feels like a hack. Should DeclarationName support
729 // template-ids, or is there a better way to keep specializations
730 // from being visible?
731 if (isa<ClassTemplateSpecializationDecl>(D))
732 return;
733
Douglas Gregor13c27cd2009-04-09 17:29:08 +0000734 if (!LookupPtr)
735 LookupPtr = new StoredDeclsMap;
Douglas Gregor8acb7272008-12-11 16:49:14 +0000736
737 // Insert this declaration into the map.
Douglas Gregor13c27cd2009-04-09 17:29:08 +0000738 StoredDeclsMap &Map = *static_cast<StoredDeclsMap*>(LookupPtr);
Chris Lattner77820d52009-02-20 01:44:05 +0000739 StoredDeclsList &DeclNameEntries = Map[D->getDeclName()];
740 if (DeclNameEntries.isNull()) {
741 DeclNameEntries.setOnlyValue(D);
Chris Lattner6719b1f2009-02-19 07:00:44 +0000742 return;
Douglas Gregor8acb7272008-12-11 16:49:14 +0000743 }
Chris Lattner265f01d2009-02-20 00:55:03 +0000744
Chris Lattnerba589d42009-02-20 01:10:07 +0000745 // If it is possible that this is a redeclaration, check to see if there is
746 // already a decl for which declarationReplaces returns true. If there is
747 // one, just replace it and return.
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000748 if (DeclNameEntries.HandleRedeclaration(getParentASTContext(), D))
Chris Lattner77820d52009-02-20 01:44:05 +0000749 return;
Chris Lattner265f01d2009-02-20 00:55:03 +0000750
Chris Lattner6719b1f2009-02-19 07:00:44 +0000751 // Put this declaration into the appropriate slot.
Chris Lattner77820d52009-02-20 01:44:05 +0000752 DeclNameEntries.AddSubsequentDecl(D);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000753}
Douglas Gregor7a7be652009-02-03 19:21:40 +0000754
755/// Returns iterator range [First, Last) of UsingDirectiveDecls stored within
756/// this context.
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000757DeclContext::udir_iterator_range
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000758DeclContext::getUsingDirectives() const {
759 lookup_const_result Result = lookup(UsingDirectiveDecl::getName());
Douglas Gregor7a7be652009-02-03 19:21:40 +0000760 return udir_iterator_range(reinterpret_cast<udir_iterator>(Result.first),
761 reinterpret_cast<udir_iterator>(Result.second));
762}
Douglas Gregorc34897d2009-04-09 22:27:44 +0000763
764void StoredDeclsList::materializeDecls(ASTContext &Context) {
765 if (isNull())
766 return;
767
768 switch ((DataKind)(Data & 0x03)) {
769 case DK_Decl:
770 case DK_Decl_Vector:
771 break;
772
773 case DK_DeclID: {
774 // Resolve this declaration ID to an actual declaration by
775 // querying the external AST source.
776 unsigned DeclID = Data >> 2;
777
778 ExternalASTSource *Source = Context.getExternalSource();
779 assert(Source && "No external AST source available!");
780
781 Data = reinterpret_cast<uintptr_t>(Source->GetDecl(DeclID));
782 break;
783 }
784
785 case DK_ID_Vector: {
786 // We have a vector of declaration IDs. Resolve all of them to
787 // actual declarations.
788 VectorTy &Vector = *getAsVector();
789 ExternalASTSource *Source = Context.getExternalSource();
790 assert(Source && "No external AST source available!");
791
792 for (unsigned I = 0, N = Vector.size(); I != N; ++I)
793 Vector[I] = reinterpret_cast<uintptr_t>(Source->GetDecl(Vector[I]));
794
795 Data = (Data & ~0x03) | DK_Decl_Vector;
796 break;
797 }
798 }
799}