blob: 28d543785fa259da13be99eafffb6b9d3f23b2d8 [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
John McCall7be34f42009-08-11 21:13:21 +0000203 case FriendClass:
John McCall36493082009-08-11 06:59:38 +0000204 case FriendFunction:
205 return IDNS_Friend;
Chris Lattner8752fe02009-03-27 20:18:19 +0000206
207 case ObjCProtocol:
Douglas Gregorafd5eb32009-04-24 00:11:27 +0000208 return IDNS_ObjCProtocol;
Chris Lattner8752fe02009-03-27 20:18:19 +0000209
Douglas Gregorafd5eb32009-04-24 00:11:27 +0000210 case ObjCImplementation:
211 return IDNS_ObjCImplementation;
212
213 case ObjCCategoryImpl:
214 return IDNS_ObjCCategoryImpl;
215
Chris Lattner8752fe02009-03-27 20:18:19 +0000216 case Field:
217 case ObjCAtDefsField:
218 case ObjCIvar:
219 return IDNS_Member;
220
221 case Record:
222 case CXXRecord:
223 case Enum:
224 case TemplateTypeParm:
225 return IDNS_Tag;
226
227 case Namespace:
228 case Template:
229 case FunctionTemplate:
230 case ClassTemplate:
231 case TemplateTemplateParm:
Anders Carlsson1bf91fb2009-03-28 23:02:53 +0000232 case NamespaceAlias:
Chris Lattner8752fe02009-03-27 20:18:19 +0000233 return IDNS_Tag | IDNS_Ordinary;
234
235 // Never have names.
236 case LinkageSpec:
237 case FileScopeAsm:
238 case StaticAssert:
239 case ObjCClass:
Chris Lattner8752fe02009-03-27 20:18:19 +0000240 case ObjCPropertyImpl:
241 case ObjCForwardProtocol:
242 case Block:
243 case TranslationUnit:
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000244
Chris Lattner8752fe02009-03-27 20:18:19 +0000245 // Aren't looked up?
246 case UsingDirective:
247 case ClassTemplateSpecialization:
Douglas Gregor58944ac2009-05-31 09:31:02 +0000248 case ClassTemplatePartialSpecialization:
Chris Lattner8752fe02009-03-27 20:18:19 +0000249 return 0;
250 }
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000251}
252
Argiris Kirtzidisfe5f9732009-06-30 02:34:44 +0000253void Decl::addAttr(Attr *NewAttr) {
254 Attr *&ExistingAttr = getASTContext().getDeclAttrs(this);
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000255
256 NewAttr->setNext(ExistingAttr);
257 ExistingAttr = NewAttr;
258
259 HasAttrs = true;
260}
261
Argiris Kirtzidisfe5f9732009-06-30 02:34:44 +0000262void Decl::invalidateAttrs() {
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000263 if (!HasAttrs) return;
Douglas Gregor98da6ae2009-06-18 16:11:24 +0000264
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000265 HasAttrs = false;
Argiris Kirtzidisfe5f9732009-06-30 02:34:44 +0000266 getASTContext().eraseDeclAttrs(this);
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000267}
268
Argiris Kirtzidisfe5f9732009-06-30 02:34:44 +0000269const Attr *Decl::getAttrsImpl() const {
Chris Lattner004a9bb2009-03-21 06:27:31 +0000270 assert(HasAttrs && "getAttrs() should verify this!");
Argiris Kirtzidisfe5f9732009-06-30 02:34:44 +0000271 return getASTContext().getDeclAttrs(this);
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000272}
273
Argiris Kirtzidisfe5f9732009-06-30 02:34:44 +0000274void Decl::swapAttrs(Decl *RHS) {
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000275 bool HasLHSAttr = this->HasAttrs;
276 bool HasRHSAttr = RHS->HasAttrs;
277
278 // Usually, neither decl has attrs, nothing to do.
279 if (!HasLHSAttr && !HasRHSAttr) return;
280
281 // If 'this' has no attrs, swap the other way.
282 if (!HasLHSAttr)
Argiris Kirtzidisfe5f9732009-06-30 02:34:44 +0000283 return RHS->swapAttrs(this);
284
285 ASTContext &Context = getASTContext();
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000286
287 // Handle the case when both decls have attrs.
288 if (HasRHSAttr) {
Douglas Gregor98da6ae2009-06-18 16:11:24 +0000289 std::swap(Context.getDeclAttrs(this), Context.getDeclAttrs(RHS));
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000290 return;
291 }
292
293 // Otherwise, LHS has an attr and RHS doesn't.
Douglas Gregor98da6ae2009-06-18 16:11:24 +0000294 Context.getDeclAttrs(RHS) = Context.getDeclAttrs(this);
295 Context.eraseDeclAttrs(this);
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000296 this->HasAttrs = false;
297 RHS->HasAttrs = true;
298}
299
300
Chris Lattner0fc6d642009-03-04 06:05:19 +0000301void Decl::Destroy(ASTContext &C) {
302 // Free attributes for this decl.
303 if (HasAttrs) {
Douglas Gregor98da6ae2009-06-18 16:11:24 +0000304 C.getDeclAttrs(this)->Destroy(C);
Argiris Kirtzidisfe5f9732009-06-30 02:34:44 +0000305 invalidateAttrs();
Chris Lattner0fc6d642009-03-04 06:05:19 +0000306 HasAttrs = false;
307 }
308
Douglas Gregor8314d742009-01-13 19:47:12 +0000309#if 0
Douglas Gregorf4006be2009-01-20 04:25:11 +0000310 // FIXME: Once ownership is fully understood, we can enable this code
311 if (DeclContext *DC = dyn_cast<DeclContext>(this))
312 DC->decls_begin()->Destroy(C);
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000313
Chris Lattnerfb8416a2009-03-28 06:04:26 +0000314 // Observe the unrolled recursion. By setting N->NextDeclInContext = 0x0
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000315 // within the loop, only the Destroy method for the first Decl
316 // will deallocate all of the Decls in a chain.
317
Chris Lattnerfb8416a2009-03-28 06:04:26 +0000318 Decl* N = getNextDeclInContext();
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000319
320 while (N) {
Chris Lattnerfb8416a2009-03-28 06:04:26 +0000321 Decl* Tmp = N->getNextDeclInContext();
322 N->NextDeclInContext = 0;
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000323 N->Destroy(C);
324 N = Tmp;
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000325 }
Douglas Gregor8314d742009-01-13 19:47:12 +0000326
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000327 this->~Decl();
Steve Naroff5abb0282009-01-27 21:25:57 +0000328 C.Deallocate((void *)this);
Douglas Gregorf4006be2009-01-20 04:25:11 +0000329#endif
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000330}
331
Argiris Kirtzidis4b662ea2008-10-12 16:14:48 +0000332Decl *Decl::castFromDeclContext (const DeclContext *D) {
Argiris Kirtzidis981aa132009-02-16 14:29:28 +0000333 Decl::Kind DK = D->getDeclKind();
334 switch(DK) {
335#define DECL_CONTEXT(Name) \
336 case Decl::Name: \
337 return static_cast<Name##Decl*>(const_cast<DeclContext*>(D));
338#define DECL_CONTEXT_BASE(Name)
339#include "clang/AST/DeclNodes.def"
340 default:
341#define DECL_CONTEXT_BASE(Name) \
342 if (DK >= Decl::Name##First && DK <= Decl::Name##Last) \
343 return static_cast<Name##Decl*>(const_cast<DeclContext*>(D));
344#include "clang/AST/DeclNodes.def"
345 assert(false && "a decl that inherits DeclContext isn't handled");
346 return 0;
347 }
Argiris Kirtzidis4b662ea2008-10-12 16:14:48 +0000348}
349
350DeclContext *Decl::castToDeclContext(const Decl *D) {
Argiris Kirtzidis981aa132009-02-16 14:29:28 +0000351 Decl::Kind DK = D->getKind();
352 switch(DK) {
353#define DECL_CONTEXT(Name) \
354 case Decl::Name: \
355 return static_cast<Name##Decl*>(const_cast<Decl*>(D));
356#define DECL_CONTEXT_BASE(Name)
357#include "clang/AST/DeclNodes.def"
358 default:
359#define DECL_CONTEXT_BASE(Name) \
360 if (DK >= Decl::Name##First && DK <= Decl::Name##Last) \
361 return static_cast<Name##Decl*>(const_cast<Decl*>(D));
362#include "clang/AST/DeclNodes.def"
363 assert(false && "a decl that inherits DeclContext isn't handled");
364 return 0;
365 }
Argiris Kirtzidis4b662ea2008-10-12 16:14:48 +0000366}
367
Argiris Kirtzidisccb9efe2009-06-30 02:35:26 +0000368CompoundStmt* Decl::getCompoundBody() const {
369 return dyn_cast_or_null<CompoundStmt>(getBody());
Sebastian Redlbc9ef252009-04-26 20:35:05 +0000370}
371
Argiris Kirtzidisccb9efe2009-06-30 02:35:26 +0000372SourceLocation Decl::getBodyRBrace() const {
373 Stmt *Body = getBody();
Sebastian Redlbc9ef252009-04-26 20:35:05 +0000374 if (!Body)
375 return SourceLocation();
376 if (CompoundStmt *CS = dyn_cast<CompoundStmt>(Body))
377 return CS->getRBracLoc();
378 assert(isa<CXXTryStmt>(Body) &&
379 "Body can only be CompoundStmt or CXXTryStmt");
380 return cast<CXXTryStmt>(Body)->getSourceRange().getEnd();
381}
382
Anders Carlsson8ea6a322009-03-25 23:38:06 +0000383#ifndef NDEBUG
384void Decl::CheckAccessDeclContext() const {
Douglas Gregora470cea2009-04-07 20:58:25 +0000385 assert((Access != AS_none || isa<TranslationUnitDecl>(this) ||
386 !isa<CXXRecordDecl>(getDeclContext())) &&
Anders Carlsson8ea6a322009-03-25 23:38:06 +0000387 "Access specifier is AS_none inside a record decl");
388}
389
390#endif
391
Eli Friedmana8a09ac2008-06-07 16:52:53 +0000392//===----------------------------------------------------------------------===//
393// DeclContext Implementation
394//===----------------------------------------------------------------------===//
395
Argiris Kirtzidis981aa132009-02-16 14:29:28 +0000396bool DeclContext::classof(const Decl *D) {
397 switch (D->getKind()) {
398#define DECL_CONTEXT(Name) case Decl::Name:
399#define DECL_CONTEXT_BASE(Name)
400#include "clang/AST/DeclNodes.def"
401 return true;
402 default:
403#define DECL_CONTEXT_BASE(Name) \
404 if (D->getKind() >= Decl::Name##First && \
405 D->getKind() <= Decl::Name##Last) \
406 return true;
407#include "clang/AST/DeclNodes.def"
408 return false;
409 }
410}
411
Douglas Gregor8acb7272008-12-11 16:49:14 +0000412DeclContext::~DeclContext() {
Douglas Gregor13c27cd2009-04-09 17:29:08 +0000413 delete static_cast<StoredDeclsMap*>(LookupPtr);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000414}
415
416void DeclContext::DestroyDecls(ASTContext &C) {
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000417 for (decl_iterator D = decls_begin(); D != decls_end(); )
Douglas Gregorf4006be2009-01-20 04:25:11 +0000418 (*D++)->Destroy(C);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000419}
420
Douglas Gregor1a6b88e2009-05-28 16:34:51 +0000421bool DeclContext::isDependentContext() const {
422 if (isFileContext())
423 return false;
424
Douglas Gregor58944ac2009-05-31 09:31:02 +0000425 if (isa<ClassTemplatePartialSpecializationDecl>(this))
426 return true;
427
Douglas Gregor1a6b88e2009-05-28 16:34:51 +0000428 if (const CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this))
429 if (Record->getDescribedClassTemplate())
430 return true;
431
432 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(this))
433 if (Function->getDescribedFunctionTemplate())
434 return true;
435
436 return getParent() && getParent()->isDependentContext();
437}
438
Douglas Gregord8028382009-01-05 19:45:36 +0000439bool DeclContext::isTransparentContext() const {
440 if (DeclKind == Decl::Enum)
441 return true; // FIXME: Check for C++0x scoped enums
442 else if (DeclKind == Decl::LinkageSpec)
443 return true;
Douglas Gregor1db12932009-02-26 00:02:51 +0000444 else if (DeclKind >= Decl::RecordFirst && DeclKind <= Decl::RecordLast)
Douglas Gregor723d3332009-01-07 00:43:41 +0000445 return cast<RecordDecl>(this)->isAnonymousStructOrUnion();
Douglas Gregord8028382009-01-05 19:45:36 +0000446 else if (DeclKind == Decl::Namespace)
447 return false; // FIXME: Check for C++0x inline namespaces
448
449 return false;
450}
451
Douglas Gregor430390d2009-08-27 06:03:53 +0000452bool DeclContext::Encloses(DeclContext *DC) {
453 if (getPrimaryContext() != this)
454 return getPrimaryContext()->Encloses(DC);
455
456 for (; DC; DC = DC->getParent())
457 if (DC->getPrimaryContext() == this)
458 return true;
459 return false;
460}
461
Steve Naroffab63fd62009-01-08 17:28:14 +0000462DeclContext *DeclContext::getPrimaryContext() {
Douglas Gregor8acb7272008-12-11 16:49:14 +0000463 switch (DeclKind) {
Douglas Gregor8acb7272008-12-11 16:49:14 +0000464 case Decl::TranslationUnit:
Douglas Gregord8028382009-01-05 19:45:36 +0000465 case Decl::LinkageSpec:
466 case Decl::Block:
Douglas Gregor8acb7272008-12-11 16:49:14 +0000467 // There is only one DeclContext for these entities.
468 return this;
469
470 case Decl::Namespace:
471 // The original namespace is our primary context.
472 return static_cast<NamespaceDecl*>(this)->getOriginalNamespace();
473
Douglas Gregor8acb7272008-12-11 16:49:14 +0000474 case Decl::ObjCMethod:
475 return this;
476
477 case Decl::ObjCInterface:
Steve Naroffab63fd62009-01-08 17:28:14 +0000478 case Decl::ObjCProtocol:
479 case Decl::ObjCCategory:
Douglas Gregor8acb7272008-12-11 16:49:14 +0000480 // FIXME: Can Objective-C interfaces be forward-declared?
481 return this;
482
Steve Naroffab63fd62009-01-08 17:28:14 +0000483 case Decl::ObjCImplementation:
484 case Decl::ObjCCategoryImpl:
485 return this;
486
Douglas Gregor8acb7272008-12-11 16:49:14 +0000487 default:
Douglas Gregora08b6c72009-02-17 23:15:12 +0000488 if (DeclKind >= Decl::TagFirst && DeclKind <= Decl::TagLast) {
489 // If this is a tag type that has a definition or is currently
490 // being defined, that definition is our primary context.
Ted Kremenekd00cd9e2009-07-29 21:53:49 +0000491 if (const TagType *TagT =cast<TagDecl>(this)->TypeForDecl->getAs<TagType>())
Douglas Gregora08b6c72009-02-17 23:15:12 +0000492 if (TagT->isBeingDefined() ||
493 (TagT->getDecl() && TagT->getDecl()->isDefinition()))
494 return TagT->getDecl();
495 return this;
496 }
497
Douglas Gregor8acb7272008-12-11 16:49:14 +0000498 assert(DeclKind >= Decl::FunctionFirst && DeclKind <= Decl::FunctionLast &&
499 "Unknown DeclContext kind");
500 return this;
501 }
502}
503
504DeclContext *DeclContext::getNextContext() {
505 switch (DeclKind) {
Douglas Gregor8acb7272008-12-11 16:49:14 +0000506 case Decl::Namespace:
507 // Return the next namespace
508 return static_cast<NamespaceDecl*>(this)->getNextNamespace();
509
510 default:
Douglas Gregor8acb7272008-12-11 16:49:14 +0000511 return 0;
512 }
513}
514
Douglas Gregorc34897d2009-04-09 22:27:44 +0000515/// \brief Load the declarations within this lexical storage from an
516/// external source.
517void
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000518DeclContext::LoadLexicalDeclsFromExternalStorage() const {
519 ExternalASTSource *Source = getParentASTContext().getExternalSource();
Douglas Gregorc34897d2009-04-09 22:27:44 +0000520 assert(hasExternalLexicalStorage() && Source && "No external storage?");
521
Eli Friedmancdf59742009-04-27 23:43:36 +0000522 llvm::SmallVector<uint32_t, 64> Decls;
Douglas Gregorc34897d2009-04-09 22:27:44 +0000523 if (Source->ReadDeclsLexicallyInContext(const_cast<DeclContext *>(this),
524 Decls))
525 return;
526
527 // There is no longer any lexical storage in this context
528 ExternalLexicalStorage = false;
529
530 if (Decls.empty())
531 return;
532
533 // Resolve all of the declaration IDs into declarations, building up
534 // a chain of declarations via the Decl::NextDeclInContext field.
535 Decl *FirstNewDecl = 0;
536 Decl *PrevDecl = 0;
537 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
538 Decl *D = Source->GetDecl(Decls[I]);
539 if (PrevDecl)
540 PrevDecl->NextDeclInContext = D;
541 else
542 FirstNewDecl = D;
543
544 PrevDecl = D;
545 }
546
547 // Splice the newly-read declarations into the beginning of the list
548 // of declarations.
549 PrevDecl->NextDeclInContext = FirstDecl;
550 FirstDecl = FirstNewDecl;
551 if (!LastDecl)
552 LastDecl = PrevDecl;
553}
554
555void
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000556DeclContext::LoadVisibleDeclsFromExternalStorage() const {
Douglas Gregorc34897d2009-04-09 22:27:44 +0000557 DeclContext *This = const_cast<DeclContext *>(this);
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000558 ExternalASTSource *Source = getParentASTContext().getExternalSource();
Douglas Gregorc34897d2009-04-09 22:27:44 +0000559 assert(hasExternalVisibleStorage() && Source && "No external storage?");
560
561 llvm::SmallVector<VisibleDeclaration, 64> Decls;
562 if (Source->ReadDeclsVisibleInContext(This, Decls))
563 return;
564
565 // There is no longer any visible storage in this context
566 ExternalVisibleStorage = false;
567
568 // Load the declaration IDs for all of the names visible in this
569 // context.
570 assert(!LookupPtr && "Have a lookup map before de-serialization?");
571 StoredDeclsMap *Map = new StoredDeclsMap;
572 LookupPtr = Map;
573 for (unsigned I = 0, N = Decls.size(); I != N; ++I) {
574 (*Map)[Decls[I].Name].setFromDeclIDs(Decls[I].Declarations);
575 }
576}
577
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000578DeclContext::decl_iterator DeclContext::decls_begin() const {
Douglas Gregorc34897d2009-04-09 22:27:44 +0000579 if (hasExternalLexicalStorage())
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000580 LoadLexicalDeclsFromExternalStorage();
Douglas Gregorc34897d2009-04-09 22:27:44 +0000581
582 // FIXME: Check whether we need to load some declarations from
583 // external storage.
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000584 return decl_iterator(FirstDecl);
585}
586
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000587DeclContext::decl_iterator DeclContext::decls_end() const {
Douglas Gregorc34897d2009-04-09 22:27:44 +0000588 if (hasExternalLexicalStorage())
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000589 LoadLexicalDeclsFromExternalStorage();
Douglas Gregorc34897d2009-04-09 22:27:44 +0000590
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000591 return decl_iterator();
592}
593
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000594bool DeclContext::decls_empty() const {
Douglas Gregorac8f2802009-04-10 17:25:41 +0000595 if (hasExternalLexicalStorage())
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000596 LoadLexicalDeclsFromExternalStorage();
Douglas Gregorac8f2802009-04-10 17:25:41 +0000597
598 return !FirstDecl;
599}
600
John McCall36493082009-08-11 06:59:38 +0000601void DeclContext::addHiddenDecl(Decl *D) {
Chris Lattner53602072009-02-20 00:56:18 +0000602 assert(D->getLexicalDeclContext() == this &&
603 "Decl inserted into wrong lexical context");
Chris Lattnerfb8416a2009-03-28 06:04:26 +0000604 assert(!D->getNextDeclInContext() && D != LastDecl &&
Douglas Gregord1675382009-01-09 19:42:16 +0000605 "Decl already inserted into a DeclContext");
606
607 if (FirstDecl) {
Chris Lattnerfb8416a2009-03-28 06:04:26 +0000608 LastDecl->NextDeclInContext = D;
Douglas Gregord1675382009-01-09 19:42:16 +0000609 LastDecl = D;
610 } else {
611 FirstDecl = LastDecl = D;
612 }
John McCall36493082009-08-11 06:59:38 +0000613}
614
615void DeclContext::addDecl(Decl *D) {
616 addHiddenDecl(D);
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000617
618 if (NamedDecl *ND = dyn_cast<NamedDecl>(D))
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000619 ND->getDeclContext()->makeDeclVisibleInContext(ND);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000620}
621
Douglas Gregord8028382009-01-05 19:45:36 +0000622/// buildLookup - Build the lookup data structure with all of the
623/// declarations in DCtx (and any other contexts linked to it or
624/// transparent contexts nested within it).
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000625void DeclContext::buildLookup(DeclContext *DCtx) {
Douglas Gregord8028382009-01-05 19:45:36 +0000626 for (; DCtx; DCtx = DCtx->getNextContext()) {
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000627 for (decl_iterator D = DCtx->decls_begin(),
628 DEnd = DCtx->decls_end();
Douglas Gregorf0464ec2009-01-06 07:17:58 +0000629 D != DEnd; ++D) {
John McCall36493082009-08-11 06:59:38 +0000630 // Insert this declaration into the lookup structure, but only
631 // if it's semantically in its decl context. During non-lazy
632 // lookup building, this is implicitly enforced by addDecl.
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000633 if (NamedDecl *ND = dyn_cast<NamedDecl>(*D))
John McCall36493082009-08-11 06:59:38 +0000634 if (D->getDeclContext() == DCtx)
635 makeDeclVisibleInContextImpl(ND);
Douglas Gregord8028382009-01-05 19:45:36 +0000636
637 // If this declaration is itself a transparent declaration context,
638 // add its members (recursively).
639 if (DeclContext *InnerCtx = dyn_cast<DeclContext>(*D))
640 if (InnerCtx->isTransparentContext())
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000641 buildLookup(InnerCtx->getPrimaryContext());
Douglas Gregord8028382009-01-05 19:45:36 +0000642 }
643 }
644}
645
Douglas Gregor8acb7272008-12-11 16:49:14 +0000646DeclContext::lookup_result
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000647DeclContext::lookup(DeclarationName Name) {
Steve Naroffab63fd62009-01-08 17:28:14 +0000648 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor8acb7272008-12-11 16:49:14 +0000649 if (PrimaryContext != this)
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000650 return PrimaryContext->lookup(Name);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000651
Douglas Gregorc34897d2009-04-09 22:27:44 +0000652 if (hasExternalVisibleStorage())
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000653 LoadVisibleDeclsFromExternalStorage();
Douglas Gregorc34897d2009-04-09 22:27:44 +0000654
Douglas Gregorddfd9d52008-12-23 00:26:44 +0000655 /// If there is no lookup data structure, build one now by walking
Douglas Gregor8acb7272008-12-11 16:49:14 +0000656 /// all of the linked DeclContexts (in declaration order!) and
657 /// inserting their values.
Douglas Gregor13c27cd2009-04-09 17:29:08 +0000658 if (!LookupPtr) {
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000659 buildLookup(this);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000660
Douglas Gregor13c27cd2009-04-09 17:29:08 +0000661 if (!LookupPtr)
Chris Lattner265f01d2009-02-20 00:55:03 +0000662 return lookup_result(0, 0);
Douglas Gregor13c27cd2009-04-09 17:29:08 +0000663 }
Douglas Gregor8acb7272008-12-11 16:49:14 +0000664
Douglas Gregor13c27cd2009-04-09 17:29:08 +0000665 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(LookupPtr);
666 StoredDeclsMap::iterator Pos = Map->find(Name);
667 if (Pos == Map->end())
668 return lookup_result(0, 0);
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000669 return Pos->second.getLookupResult(getParentASTContext());
Douglas Gregor8acb7272008-12-11 16:49:14 +0000670}
671
672DeclContext::lookup_const_result
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000673DeclContext::lookup(DeclarationName Name) const {
674 return const_cast<DeclContext*>(this)->lookup(Name);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000675}
676
Chris Lattner9a502bd2009-03-27 19:19:59 +0000677DeclContext *DeclContext::getLookupContext() {
678 DeclContext *Ctx = this;
Douglas Gregordb568cf2009-01-08 20:45:30 +0000679 // Skip through transparent contexts.
Douglas Gregor69e781f2009-01-06 23:51:29 +0000680 while (Ctx->isTransparentContext())
681 Ctx = Ctx->getParent();
682 return Ctx;
683}
684
Douglas Gregor0d93f692009-02-25 22:02:03 +0000685DeclContext *DeclContext::getEnclosingNamespaceContext() {
686 DeclContext *Ctx = this;
687 // Skip through non-namespace, non-translation-unit contexts.
688 while (!Ctx->isFileContext() || Ctx->isTransparentContext())
689 Ctx = Ctx->getParent();
690 return Ctx->getPrimaryContext();
691}
692
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000693void DeclContext::makeDeclVisibleInContext(NamedDecl *D) {
Douglas Gregora08b6c72009-02-17 23:15:12 +0000694 // FIXME: This feels like a hack. Should DeclarationName support
695 // template-ids, or is there a better way to keep specializations
696 // from being visible?
697 if (isa<ClassTemplateSpecializationDecl>(D))
698 return;
699
Steve Naroffab63fd62009-01-08 17:28:14 +0000700 DeclContext *PrimaryContext = getPrimaryContext();
Douglas Gregor8acb7272008-12-11 16:49:14 +0000701 if (PrimaryContext != this) {
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000702 PrimaryContext->makeDeclVisibleInContext(D);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000703 return;
704 }
705
706 // If we already have a lookup data structure, perform the insertion
707 // into it. Otherwise, be lazy and don't build that structure until
708 // someone asks for it.
Douglas Gregor13c27cd2009-04-09 17:29:08 +0000709 if (LookupPtr)
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000710 makeDeclVisibleInContextImpl(D);
Douglas Gregord8028382009-01-05 19:45:36 +0000711
Douglas Gregord8028382009-01-05 19:45:36 +0000712 // If we are a transparent context, insert into our parent context,
713 // too. This operation is recursive.
714 if (isTransparentContext())
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000715 getParent()->makeDeclVisibleInContext(D);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000716}
717
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000718void DeclContext::makeDeclVisibleInContextImpl(NamedDecl *D) {
Douglas Gregord8028382009-01-05 19:45:36 +0000719 // Skip unnamed declarations.
720 if (!D->getDeclName())
721 return;
722
Douglas Gregora08b6c72009-02-17 23:15:12 +0000723 // FIXME: This feels like a hack. Should DeclarationName support
724 // template-ids, or is there a better way to keep specializations
725 // from being visible?
726 if (isa<ClassTemplateSpecializationDecl>(D))
727 return;
728
Douglas Gregor13c27cd2009-04-09 17:29:08 +0000729 if (!LookupPtr)
730 LookupPtr = new StoredDeclsMap;
Douglas Gregor8acb7272008-12-11 16:49:14 +0000731
732 // Insert this declaration into the map.
Douglas Gregor13c27cd2009-04-09 17:29:08 +0000733 StoredDeclsMap &Map = *static_cast<StoredDeclsMap*>(LookupPtr);
Chris Lattner77820d52009-02-20 01:44:05 +0000734 StoredDeclsList &DeclNameEntries = Map[D->getDeclName()];
735 if (DeclNameEntries.isNull()) {
736 DeclNameEntries.setOnlyValue(D);
Chris Lattner6719b1f2009-02-19 07:00:44 +0000737 return;
Douglas Gregor8acb7272008-12-11 16:49:14 +0000738 }
Chris Lattner265f01d2009-02-20 00:55:03 +0000739
Chris Lattnerba589d42009-02-20 01:10:07 +0000740 // If it is possible that this is a redeclaration, check to see if there is
741 // already a decl for which declarationReplaces returns true. If there is
742 // one, just replace it and return.
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000743 if (DeclNameEntries.HandleRedeclaration(getParentASTContext(), D))
Chris Lattner77820d52009-02-20 01:44:05 +0000744 return;
Chris Lattner265f01d2009-02-20 00:55:03 +0000745
Chris Lattner6719b1f2009-02-19 07:00:44 +0000746 // Put this declaration into the appropriate slot.
Chris Lattner77820d52009-02-20 01:44:05 +0000747 DeclNameEntries.AddSubsequentDecl(D);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000748}
Douglas Gregor7a7be652009-02-03 19:21:40 +0000749
750/// Returns iterator range [First, Last) of UsingDirectiveDecls stored within
751/// this context.
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000752DeclContext::udir_iterator_range
Argiris Kirtzidisab6e38a2009-06-30 02:36:12 +0000753DeclContext::getUsingDirectives() const {
754 lookup_const_result Result = lookup(UsingDirectiveDecl::getName());
Douglas Gregor7a7be652009-02-03 19:21:40 +0000755 return udir_iterator_range(reinterpret_cast<udir_iterator>(Result.first),
756 reinterpret_cast<udir_iterator>(Result.second));
757}
Douglas Gregorc34897d2009-04-09 22:27:44 +0000758
759void StoredDeclsList::materializeDecls(ASTContext &Context) {
760 if (isNull())
761 return;
762
763 switch ((DataKind)(Data & 0x03)) {
764 case DK_Decl:
765 case DK_Decl_Vector:
766 break;
767
768 case DK_DeclID: {
769 // Resolve this declaration ID to an actual declaration by
770 // querying the external AST source.
771 unsigned DeclID = Data >> 2;
772
773 ExternalASTSource *Source = Context.getExternalSource();
774 assert(Source && "No external AST source available!");
775
776 Data = reinterpret_cast<uintptr_t>(Source->GetDecl(DeclID));
777 break;
778 }
779
780 case DK_ID_Vector: {
781 // We have a vector of declaration IDs. Resolve all of them to
782 // actual declarations.
783 VectorTy &Vector = *getAsVector();
784 ExternalASTSource *Source = Context.getExternalSource();
785 assert(Source && "No external AST source available!");
786
787 for (unsigned I = 0, N = Vector.size(); I != N; ++I)
788 Vector[I] = reinterpret_cast<uintptr_t>(Source->GetDecl(Vector[I]));
789
790 Data = (Data & ~0x03) | DK_Decl_Vector;
791 break;
792 }
793 }
794}